summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc/shared/gpio/grgpio.c
blob: e9a5426d88f710ff426d47fdd727277f69a2c2f0 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*  GRGPIO GPIO 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 <bsp.h>
#include <rtems/libio.h>
#include <stdlib.h>
#include <assert.h>
#include <rtems/bspIo.h>
#include <string.h>
#include <stdio.h>

#include <drvmgr/drvmgr.h>
#include <drvmgr/ambapp_bus.h>
#include <grgpio.h>
#include <gpiolib.h>
#include <ambapp.h>
#include <grlib.h>

/*#define DEBUG 1*/

#ifdef DEBUG
#define DBG(x...) printk(x)
#define STATIC
#else
#define DBG(x...) 
#define STATIC static
#endif

struct grgpio_isr {
	drvmgr_isr isr;
	void *arg;
};

struct grgpio_priv {
	struct drvmgr_dev	*dev;
	struct grgpio_regs		*regs;
	int				irq;
	int				minor;

	/* Driver implementation */
	int				port_cnt;
	unsigned char			port_handles[32];
	struct grgpio_isr		isrs[32];
	struct gpiolib_drv		gpiolib_desc;
	unsigned int			bypass;
	unsigned int			imask;
};

/******************* Driver Manager Part ***********************/

int grgpio_device_init(struct grgpio_priv *priv);

int grgpio_init1(struct drvmgr_dev *dev);
int grgpio_init2(struct drvmgr_dev *dev);

struct drvmgr_drv_ops grgpio_ops = 
{
	.init = {grgpio_init1, NULL, NULL, NULL},
	.remove = NULL,
	.info = NULL
};

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

struct amba_drv_info grgpio_drv_info =
{
	{
		DRVMGR_OBJ_DRV,				/* Driver */
		NULL,					/* Next driver */
		NULL,					/* Device list */
		DRIVER_AMBAPP_GAISLER_GRGPIO_ID,	/* Driver ID */
		"GRGPIO_DRV",				/* Driver Name */
		DRVMGR_BUS_TYPE_AMBAPP,			/* Bus Type */
		&grgpio_ops,
		NULL,					/* Funcs */
		0,					/* No devices yet */
		0,
	},
	&grgpio_ids[0]
};

void grgpio_register_drv (void)
{
	DBG("Registering GRGPIO driver\n");
	drvmgr_drv_register(&grgpio_drv_info.general);
}

/* Register GRGPIO pins as quick as possible to the GPIO library,
 * other drivers may depend upon them in INIT LEVEL 2.
 * Note that since IRQ may not be available in init1, it is assumed
 * that the GPIOLibrary does not request IRQ routines until LEVEL 2.
 */
int grgpio_init1(struct drvmgr_dev *dev)
{
	struct grgpio_priv *priv;
	int status, port;

	DBG("GRGPIO[%d] on bus %s\n", dev->minor_drv, dev->parent->dev->name);

	/* This core will not find other cores, but other driver may depend upon 
	 * the GPIO library to function. So, we set up GPIO right away.
	 */

	/* Initialize library if not already done */
	status = gpiolib_initialize();
	if ( status < 0 )
		return DRVMGR_FAIL;

	priv = dev->priv = malloc(sizeof(struct grgpio_priv));
	if ( !priv )
		return DRVMGR_NOMEM;
	memset(priv, 0, sizeof(*priv));
	priv->dev = dev;

	if ( grgpio_device_init(priv) ) {
		free(dev->priv);
		dev->priv = NULL;
		return DRVMGR_FAIL;
	}

	/* Register all ports available on this core as GPIO port to 
	 * upper layer
	 */
	for(port=0; port<priv->port_cnt; port++) {
		priv->port_handles[port] = port;
		gpiolib_drv_register(&priv->gpiolib_desc,
					&priv->port_handles[port]);
	}

	return DRVMGR_OK;
}

/******************* Driver Implementation ***********************/

/* Find port from handle, returns -1 if not found */
static int grgpio_find_port(void *handle, struct grgpio_priv **priv)
{
	unsigned char portnr;
	
	portnr = *(unsigned char *)handle;
	if ( portnr > 31 )
		return -1;
	*priv = (struct grgpio_priv *)
		(((unsigned int)handle - portnr*sizeof(unsigned char)) - 
		offsetof(struct grgpio_priv, port_handles));
	return portnr;
}

static int grgpio_gpiolib_open(void *handle)
{
	struct grgpio_priv *priv;
	int portnr;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		DBG("GRGPIO: FAILED OPENING HANDLE 0x%08x\n", handle);
		return -1;
	}
	DBG("GRGPIO[0x%08x][%d]: OPENING\n", priv->regs, portnr);

	/* Open the device, nothing to be done... */

	return 0;
}

static int grgpio_grpiolib_config(void *handle, struct gpiolib_config *cfg)
{
	struct grgpio_priv *priv;
	int portnr;
	unsigned int mask;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		return -1;
	}
	DBG("GRGPIO[0x%08x][%d]: CONFIG\n", priv->regs, portnr);

	/* Configure the device. And check that operation is supported,
	 * not all I/O Pins have IRQ support.
	 */
	mask = (1<<portnr);

	/* Return error when IRQ not supported by this I/O Line and it
	 * is beeing enabled by user.
	 */
	if ( ((mask & priv->imask) == 0) && cfg->mask )
		return -1;

	priv->regs->imask &= ~mask; /* Disable interrupt temporarily */

	/* Configure settings before enabling interrupt */
	priv->regs->ipol = (priv->regs->ipol & ~mask) | (cfg->irq_polarity ? mask : 0);
	priv->regs->iedge = (priv->regs->iedge & ~mask) | (cfg->irq_level ? 0 : mask);
	priv->regs->imask |= cfg->mask ? mask : 0;

	return 0;
}

static int grgpio_grpiolib_get(void *handle, int *inval)
{
	struct grgpio_priv *priv;
	int portnr;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		return -1;
	}
	DBG("GRGPIO[0x%08x][%d]: GET\n", priv->regs, portnr);

	/* Get current status of the port */
	if ( inval )
		*inval = (priv->regs->data >> portnr) & 0x1;

	return 0;
}

static int grgpio_grpiolib_irq_opts(void *handle, unsigned int options)
{
	struct grgpio_priv *priv;
	int portnr;
	drvmgr_isr isr;
	void *arg;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		return -1;
	}
	DBG("GRGPIO[0x%08x][%d]: IRQ OPTS 0x%x\n", priv->regs, portnr, options);

	if ( options & GPIOLIB_IRQ_FORCE )
		return -1;

	isr = priv->isrs[portnr].isr;
	arg = priv->isrs[portnr].arg;

	if ( options & GPIOLIB_IRQ_DISABLE ) {
		/* Disable interrupt at interrupt controller */
		if ( drvmgr_interrupt_unregister(priv->dev, portnr, isr, arg) ) {
			return -1;
		}
	}
	if ( options & GPIOLIB_IRQ_CLEAR ) {
		/* Clear interrupt at interrupt controller */
		if ( drvmgr_interrupt_clear(priv->dev, portnr) ) {
			return -1;
		}
	}
	if ( options & GPIOLIB_IRQ_ENABLE ) {
		/* Enable interrupt at interrupt controller */
		if ( drvmgr_interrupt_register(priv->dev, portnr, "grgpio", isr, arg) ) {
			return -1;
		}
	}
	if ( options & GPIOLIB_IRQ_MASK ) {
		/* Mask (disable) interrupt at interrupt controller */
		if ( drvmgr_interrupt_mask(priv->dev, portnr) ) {
			return -1;
		}
	}
	if ( options & GPIOLIB_IRQ_UNMASK ) {
		/* Unmask (enable) interrupt at interrupt controller */
		if ( drvmgr_interrupt_unmask(priv->dev, portnr) ) {
			return -1;
		}
	}

	return 0;
}

static int grgpio_grpiolib_irq_register(void *handle, void *func, void *arg)
{
	struct grgpio_priv *priv;
	int portnr;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		DBG("GRGPIO: FAILED OPENING HANDLE 0x%08x\n", handle);
		return -1;
	}
	DBG("GRGPIO: OPENING %d at [0x%08x]\n", portnr, priv->regs);

	/* Since the user doesn't provide the ISR and argument, we must... */
	priv->isrs[portnr].isr = func;
	priv->isrs[portnr].arg = arg;

	return 0;
}

static int grgpio_grpiolib_set(void *handle, int dir, int outval)
{
	struct grgpio_priv *priv;
	int portnr;
	unsigned int mask;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		DBG("GRGPIO: FAILED OPENING HANDLE 0x%08x\n", handle);
		return -1;
	}
	DBG("GRGPIO: OPENING %d at [0x%08x]\n", portnr, priv->regs);

	/* Set Direction and Output */
	mask = 1<<portnr;
	priv->regs->dir = (priv->regs->dir & ~mask) | (dir ? mask : 0);
	priv->regs->output = (priv->regs->output & ~mask) | (outval ? mask : 0);

	return 0;
}

static int grgpio_gpiolib_show(void *handle)
{
	struct grgpio_priv *priv;
	int portnr, i, regs[7];
	volatile unsigned int *reg;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		DBG("GRGPIO: FAILED SHOWING HANDLE 0x%08x\n", handle);
		return -1;
	}
	for (i=0, reg=&priv->regs->data; i<7; i++, reg++) {
		regs[i] = ( *reg >> portnr) & 1;
	}
	printf("GRGPIO[%p] PORT[%d]: IN/OUT/DIR: [%d,%d,%d], MASK/POL/EDGE: [%d,%d,%d], BYPASS: %d\n",
		priv->regs, portnr, regs[0], regs[1], regs[2], regs[3], regs[4], regs[5], regs[6]);
	return 0;
}

static int grgpio_gpiolib_get_info(void *handle, struct gpiolib_info *pinfo)
{
	struct grgpio_priv *priv;
	int portnr;
	char prefix[48];
	struct drvmgr_dev *dev;

	if ( !pinfo )
		return -1;

	portnr = grgpio_find_port(handle, &priv);
	if ( portnr < 0 ) {
		DBG("GRGPIO: FAILED GET_INFO HANDLE 0x%08x\n", handle);
		return -1;
	}

	/* Get Filesystem name prefix */
	dev = priv->dev;
	prefix[0] = '\0';
	if ( drvmgr_get_dev_prefix(dev, prefix) ) {
		/* Failed to get prefix, make sure of a unique FS name
		 * by using the driver minor.
		 */
		snprintf(pinfo->devName, 64, "/dev/grgpio%d/%d", dev->minor_drv, portnr);
	} else {
		/* Got special prefix, this means we have a bus prefix
		 * And we should use our "bus minor"
		 */
		snprintf(pinfo->devName, 64, "/dev/%sgrgpio%d/%d", prefix, dev->minor_bus, portnr);
	}

	return 0;
}

static struct gpiolib_drv_ops grgpio_gpiolib_ops = 
{
	.config		= grgpio_grpiolib_config,
	.get		= grgpio_grpiolib_get,
	.irq_opts	= grgpio_grpiolib_irq_opts,
	.irq_register	= grgpio_grpiolib_irq_register,
	.open		= grgpio_gpiolib_open,
	.set		= grgpio_grpiolib_set,
	.show		= grgpio_gpiolib_show,
	.get_info	= grgpio_gpiolib_get_info,
};

int grgpio_device_init(struct grgpio_priv *priv)
{
	struct amba_dev_info *ambadev;
	struct ambapp_core *pnpinfo;
	union drvmgr_key_value *value;
	unsigned int mask;
	int port_cnt;

	/* Get device information from AMBA PnP information */
	ambadev = (struct amba_dev_info *)priv->dev->businfo;
	if ( ambadev == NULL ) {
		return -1;
	}
	pnpinfo = &ambadev->info;
	priv->irq = pnpinfo->irq;
	priv->regs = (struct grgpio_regs *)pnpinfo->apb_slv->start;

	DBG("GRGPIO: 0x%08x irq %d\n", (unsigned int)priv->regs, priv->irq);

	/* Mask all Interrupts */
	priv->regs->imask = 0;

	/* Make IRQ Rising edge triggered default */
	priv->regs->ipol = 0xfffffffe;
	priv->regs->iedge = 0xfffffffe;

	/* Read what I/O lines have IRQ support */
	priv->imask = priv->regs->ipol;

	/* Let the user configure the port count, this might be needed
	 * when the GPIO lines must not be changed (assigned during bootup)
	 */
	value = drvmgr_dev_key_get(priv->dev, "nBits", KEY_TYPE_INT);
	if ( value ) {
		priv->port_cnt = value->i;
	} else {
		/* Auto detect number of GPIO ports */
		priv->regs->dir = 0;
		priv->regs->output = 0xffffffff;
		mask = priv->regs->output;
		priv->regs->output = 0;

		for(port_cnt=0; port_cnt<32; port_cnt++)
			if ( (mask & (1<<port_cnt)) == 0 )
				break;
		priv->port_cnt = port_cnt;
	}

	/* Let the user configure the BYPASS register, this might be needed
	 * to select which cores can do I/O on a pin.
	 */
	value = drvmgr_dev_key_get(priv->dev, "bypass", KEY_TYPE_INT);
	if ( value ) {
		priv->bypass = value->i;
	} else {
		priv->bypass = 0;
	}
	priv->regs->bypass = priv->bypass;

	/* Prepare GPIOLIB layer */
	priv->gpiolib_desc.ops = &grgpio_gpiolib_ops;

	return 0;
}