summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc/shared/time/grctm.c
blob: c756a430ae5af4f133623d0871b0fdf8c51c1483 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*  GRCTM - CCSDS Time Manager - register driver interface.
 *
 *  COPYRIGHT (c) 2009.
 *  Cobham Gaisler AB.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#include <drvmgr/drvmgr.h>
#include <drvmgr/ambapp_bus.h>
#include <stdlib.h>

#include <grctm.h>

/* Private structure of GRCTM driver */
struct grctm_priv {
	struct drvmgr_dev *dev;
	struct grctm_regs *regs;
	int open;

	grctm_isr_t user_isr;
	void *user_isr_arg;

	struct grctm_stats stats;
};

void grctm_isr(void *data);

struct amba_drv_info grctm_drv_info;

void *grctm_open(int minor)
{
	struct grctm_priv *priv;
	struct drvmgr_dev *dev;

	/* Get Device from Minor */
	if ( drvmgr_get_dev(&grctm_drv_info.general, minor, &dev) ) {
		return NULL;
	}

	priv = dev->priv;
	if ( (priv == NULL) || priv->open )
		return NULL;

	/* Set initial state of software */
	priv->open = 1;

	/* Clear Statistics */
	grctm_clr_stats(priv);
	priv->user_isr = NULL;
	priv->user_isr_arg = NULL;

	return priv;
}

void grctm_close(void *grctm)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	if ( priv->open == 0 )
		return;

	/* Reset Hardware */
	grctm_reset(priv);

	priv->open = 0;
}

/* Hardware Reset of GRCTM */
int grctm_reset(void *grctm)
{
	struct grctm_priv *priv = grctm;
	struct grctm_regs *r = priv->regs;

	r->grr = 0x55000001;

	int i = 1000;
	while ((r->grr & 1) && i > 0) {
		i--;
	}

	return i ? 0 : -1;
}

void grctm_int_enable(void *grctm)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	/* Register and Enable Interrupt at Interrupt controller */
	drvmgr_interrupt_register(priv->dev, 0, "grctm", grctm_isr, priv);
}

void grctm_int_disable(void *grctm)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	/* Enable Interrupt at Interrupt controller */
	drvmgr_interrupt_unregister(priv->dev, 0, grctm_isr, priv);
}

void grctm_clr_stats(void *grctm)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	memset(&priv->stats, 0, sizeof(priv->stats));
}

void grctm_get_stats(void *grctm, struct grctm_stats *stats)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	memcpy(stats, &priv->stats, sizeof(priv->stats));
}

/* Enable external synchronisation (from grctm) */
void grctm_enable_ext_sync(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr |= 0x55<<24 | 1<<9;
}

/* Disable external synchronisation (from grctm) */
void grctm_disable_ext_sync(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr &= ~((0xAA<<24) | 1<<9);
}

/* Enable TimeWire synchronisation */
void grctm_enable_tw_sync(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr |= 0x55<<24 | 1<<8;
}

/* Disable TimeWire synchronisation */
void grctm_disable_tw_sync(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr &= ~((0xAA<<24) | 1<<8);
}

/* Disable frequency synthesizer from driving ET */
void grctm_disable_fs(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr |= 0x55<<24 | 1<<7;
}

/* Enable frequency synthesizer to drive ET */
void grctm_enable_fs(void *grctm)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr &= ~((0xAA<<24) | 1<<7);
}

/* Return elapsed coarse time */
unsigned int grctm_get_et_coarse(void *grctm)
{
	struct grctm_priv *priv = grctm;

	return priv->regs->etcr;
}

/* Return elapsed fine time */
unsigned int grctm_get_et_fine(void *grctm)
{
	struct grctm_priv *priv = grctm;

	return (priv->regs->etfr & 0xffffff00) >> 8;
}

/* Return elapsed time (coarse and fine) */
unsigned long long grctm_get_et(void *grctm)
{
	return (((unsigned long)grctm_get_et_coarse(grctm)) << 24) | grctm_get_et_fine(grctm);
}


/* Return 1 if specified datation has been latched */
int grctm_is_dat_latched(void *grctm, int dat)
{
	struct grctm_priv *priv = grctm;

	return (priv->regs->gsr >> dat) & 1;
}

/* Set triggering edge of datation input */
void grctm_set_dat_edge(void *grctm, int dat, int edge)
{
	struct grctm_priv *priv = grctm;

	priv->regs->gcr &= ~((0xAA<<24) | 1 << (10+dat));
	priv->regs->gcr |= 0x55<<24 | (edge&1) << (10+dat);
}

/* Return latched datation coarse time */
unsigned int grctm_get_dat_coarse(void *grctm, int dat)
{
	struct grctm_priv *priv = grctm;

	switch (dat) {
	case 0 : return priv->regs->dcr0; 
	case 1 : return priv->regs->dcr1;
	case 2 : return priv->regs->dcr2;
	default: return -1;
	}
}

/* Return latched datation fine time */
unsigned int grctm_get_dat_fine(void *grctm, int dat)
{
	struct grctm_priv *priv = grctm;

	switch (dat) {
	case 0 : return (priv->regs->dfr0 & 0xffffff00) >> 8; 
	case 1 : return (priv->regs->dfr1 & 0xffffff00) >> 8;
	case 2 : return (priv->regs->dfr2 & 0xffffff00) >> 8;
	default: return -1;
	}
}


/* Return latched datation ET */
unsigned long long grctm_get_dat_et(void *grctm, int dat)
{
	return (((unsigned long)grctm_get_dat_coarse(grctm, dat)) << 24) | 
		grctm_get_dat_fine(grctm, dat);
}


/* Return current pulse configuration */
unsigned int grctm_get_pulse_reg(void *grctm, int pulse)
{
	struct grctm_priv *priv = grctm;

	return priv->regs->pdr[pulse];
}

/* Set pulse register */
void grctm_set_pulse_reg(void *grctm, int pulse, unsigned int val)
{
	struct grctm_priv *priv = grctm;

	priv->regs->pdr[pulse] = val;
}

/* Configure pulse: pp = period, pw = width, pl = level, en = enable */
void grctm_cfg_pulse(void *grctm, int pulse, int pp, int pw, int pl, int en)
{
	grctm_set_pulse_reg(grctm, pulse, (pp&0xf)<<20 | (pw&0xf)<<16 | (pl&1)<<10 | (en&1)<<1);
}

/* Enable pulse output */
void grctm_enable_pulse(void *grctm, int pulse)
{
	struct grctm_priv *priv = grctm;

	priv->regs->pdr[pulse] |= 0x2;
}

/* Disable pulse output */
void grctm_disable_pulse(void *grctm, int pulse)
{
	struct grctm_priv *priv = grctm;

	priv->regs->pdr[pulse] &= ~0x2;
}

/* Clear interrupts */
void grctm_clear_irqs(void *grctm, int irqs)
{
	struct grctm_priv *priv = grctm;

	priv->regs->picr = irqs;
}

/* Enable interrupts */
void grctm_enable_irqs(void *grctm, int irqs)
{
	struct grctm_priv *priv = grctm;

	priv->regs->imr  = irqs;
}

/* Set Frequency synthesizer increment */
void grctm_set_fs_incr(void *grctm, int incr)
{
	struct grctm_priv *priv = grctm;

	priv->regs->fsir  = incr;
}

/* Set ET increment */
void grctm_set_et_incr(void *grctm, int incr)
{
	struct grctm_priv *priv = grctm;

	priv->regs->etir  = incr;
}


void grctm_isr(void *data)
{
	struct grctm_priv *priv = data;
	struct grctm_stats *stats = &priv->stats;
	unsigned int pimr = priv->regs->pimr;

	if ( pimr == 0 )
		return;

	stats->nirqs++;
	if (pimr & PULSE0_IRQ )
		stats->pulse++;

	/* Let user Handle Interrupt */
	if ( priv->user_isr )
		priv->user_isr(pimr, priv->user_isr_arg);
}

struct grctm_regs *grctm_get_regs(void *grctm)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	return priv->regs;
}

void grctm_int_register(void *grctm, grctm_isr_t func, void *data)
{
	struct grctm_priv *priv = (struct grctm_priv *)grctm;

	priv->user_isr = func;
	priv->user_isr_arg = data;
}

/*** INTERFACE TO DRIVER MANAGER ***/

static int grctm_init2(struct drvmgr_dev *dev)
{
	struct amba_dev_info *ambadev;
	struct ambapp_core *pnpinfo;
	struct grctm_priv *priv;
	struct grctm_regs *regs;

	priv = (struct grctm_priv *)malloc(sizeof(*priv));
	if ( priv == NULL )
		return -1;
	memset(priv, 0, sizeof(*priv));
	priv->dev = dev;
	dev->priv = priv;

	/* Get device information from AMBA PnP information */
	ambadev = (struct amba_dev_info *)dev->businfo;
	if ( ambadev == NULL ) {
		return -1;
	}
	pnpinfo = &ambadev->info;
	regs = (struct grctm_regs *)pnpinfo->ahb_slv->start[0];

	priv->regs = regs;

	grctm_reset(priv);

	return 0;
}

struct drvmgr_drv_ops grctm_ops =
{
	{NULL, grctm_init2, NULL, NULL},
	NULL,
	NULL
};

struct amba_dev_id grctm_ids[] =
{
	{VENDOR_GAISLER, GAISLER_GRCTM},
	{0, 0}	/* Mark end of table */
};

struct amba_drv_info grctm_drv_info =
{
	{
		DRVMGR_OBJ_DRV,			/* Driver */
		NULL,				/* Next driver */
		NULL,				/* Device list */
		DRIVER_AMBAPP_GAISLER_GRCTM_ID,	/* Driver ID */
		"GRCTM_DRV",			/* Driver Name */
		DRVMGR_BUS_TYPE_AMBAPP,		/* Bus Type */
		&grctm_ops,
		NULL,				/* Funcs */
		0,				/* No devices yet */
		0,
	},
	&grctm_ids[0]
};

/* Register the grctm Driver */
void grctm_register(void)
{
	drvmgr_drv_register(&grctm_drv_info.general);
}