summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc/shared/drvmgr/leon2_amba_bus.c
blob: aca776c753e32725e8b9ff5aecbbaf56db7b2f2b (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
/*  LEON2 Hardcoded bus driver.
 *
 *  COPYRIGHT (c) 2008.
 *  Cobham Gaisler AB.
 *
 *  Bus driver for a hardcoded setup. LEON2 systems have some
 *  cores always present, here called "Standard Cores". In 
 *  addtion to the standard cores there are often extra cores
 *  that can be defined using the "Custom Cores" mechanism.
 *
 *  A Core is described by assigning a base register and 
 *  IRQ0..IRQ15 using the leon2_core structure.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <drvmgr/drvmgr.h>
#include <drvmgr/leon2_amba_bus.h>

#include <bsp.h>
#include <rtems/bspIo.h>

#define DBG(args...)
/*#define DBG(args...) printk(args)*/

struct drvmgr_drv leon2_bus_drv;

int leon2_amba_bus_init1(struct drvmgr_bus *bus);
int leon2_amba_unite(struct drvmgr_drv *drv, struct drvmgr_dev *dev);
int leon2_amba_int_register(
	struct drvmgr_dev *dev,
	int index,
	const char *info,
	drvmgr_isr isr,
	void *arg);
int leon2_amba_int_unregister(
	struct drvmgr_dev *dev,
	int index,
	drvmgr_isr isr,
	void *arg);
int leon2_amba_int_clear(
	struct drvmgr_dev *dev,
	int index);
int leon2_amba_int_mask(
	struct drvmgr_dev *dev,
	int index);
int leon2_amba_int_unmask(
	struct drvmgr_dev *dev,
	int index);

/* LEON2 bus operations */
struct drvmgr_bus_ops leon2_amba_bus_ops =
{
	.init = {
		leon2_amba_bus_init1,
		NULL,
		NULL,
		NULL
	},
	.remove = NULL,
	.unite		= leon2_amba_unite,
	.int_register	= leon2_amba_int_register,
	.int_unregister	= leon2_amba_int_unregister,
	.int_clear	= leon2_amba_int_clear,
	.int_mask	= leon2_amba_int_mask,
	.int_unmask	= leon2_amba_int_unmask,
	.get_params	= NULL,
};

struct leon2_isr_handler {
	void (*handler)(int irq, void *arg);
	void *arg;
};

/* Standard LEON2 configuration */

struct drvmgr_key leon2_timers[] =
{
	{"REG0", DRVMGR_KT_INT, {0x80000040}},
	{"IRQ0", DRVMGR_KT_INT, {8}},
	{"IRQ1", DRVMGR_KT_INT, {9}},
	DRVMGR_KEY_EMPTY
};

struct drvmgr_key leon2_uart1[] =
{
	{"REG0", DRVMGR_KT_INT, {0x80000070}},
	{"IRQ0", DRVMGR_KT_INT, {3}},
	DRVMGR_KEY_EMPTY
};

struct drvmgr_key leon2_uart2[] =
{
	{"REG0", DRVMGR_KT_INT, {0x80000080}},
	{"IRQ0", DRVMGR_KT_INT, {2}},
	DRVMGR_KEY_EMPTY
};

struct drvmgr_key leon2_irqctrl[] =
{
	{"REG0", DRVMGR_KT_INT, {0x80000090}},
	DRVMGR_KEY_EMPTY
};

struct drvmgr_key leon2_gpio0[] =
{
	{"REG0", DRVMGR_KT_INT, {0x800000A0}},
	{"IRQ0", DRVMGR_KT_INT, {4}},
	{"IRQ1", DRVMGR_KT_INT, {5}},
	{"IRQ2", DRVMGR_KT_INT, {6}},
	{"IRQ3", DRVMGR_KT_INT, {7}},
	DRVMGR_KEY_EMPTY
};

struct leon2_core leon2_std_cores[] = 
{
	{{LEON2_AMBA_TIMER_ID}, "Timers", &leon2_timers[0]},
	{{LEON2_AMBA_UART_ID}, "Uart1", &leon2_uart1[0]},
	{{LEON2_AMBA_UART_ID}, "Uart2", &leon2_uart2[0]},
	{{LEON2_AMBA_IRQCTRL_ID}, "IRQCtrl", &leon2_irqctrl[0]},
	{{LEON2_AMBA_GPIO_ID}, "GPIO", &leon2_gpio0[0]},
	EMPTY_LEON2_CORE
};

static struct leon2_bus *leon2_bus_config = NULL;
static struct drvmgr_bus_res *leon2_bus_res = NULL;

int leon2_root_register(
	struct leon2_bus *bus_config,
	struct drvmgr_bus_res *resources)
{
	/* Save the configuration for later */
	leon2_bus_config = bus_config;
	leon2_bus_res = resources;

	/* Register root device driver */
	drvmgr_root_drv_register(&leon2_bus_drv);

	return 0;
}

static int leon2_amba_dev_register(
	struct drvmgr_bus *bus,
	struct leon2_core *core,
	int index)
{
	struct drvmgr_dev *newdev;
	struct leon2_amba_dev_info *info;
	union drvmgr_key_value *value;
	char irq_name[8];
	int i;

	/* Allocate new device and businfo */
	drvmgr_alloc_dev(&newdev, sizeof(struct leon2_amba_dev_info));
	info = (struct leon2_amba_dev_info *)(newdev + 1);

	/* Set Core ID */
	info->core_id = core->id.core_id;

	/* Get information from bus configuration */
	value = drvmgr_key_val_get(core->keys, "REG0", DRVMGR_KT_INT);
	if ( !value ) {
		printk("leon2_amba_dev_register: Failed getting resource REG0\n");
		info->reg_base = 0x00000000;
	} else {
		DBG("leon2_amba_dev_register: REG0: 0x%08x\n", value->i);
		info->reg_base = value->i;
	}

	strcpy(irq_name, "IRQ");
	for(i=0; i<16; i++){
		if ( i < 10 ){
			irq_name[3] = '0' + i;
			irq_name[4] = '\0';
		} else {
			irq_name[3] = '1';
			irq_name[4] = '0' + (i-10);
			irq_name[5] = '\0';
		}

		value = drvmgr_key_val_get(core->keys, irq_name, DRVMGR_KT_INT);
		if ( !value ) {
			DBG("leon2_amba_dev_register: Failed getting resource IRQ%d for REG 0x%x\n", i, info->reg_base);
			info->irqs[i] = 0;
		} else {
			DBG("leon2_amba_dev_register: IRQ%d: %d\n", i, value->i);
			info->irqs[i] = value->i;
		}
	}

	/* Init new device */
	newdev->next = NULL;
	newdev->parent = bus; /* Ourselfs */
	newdev->minor_drv = 0;
	newdev->minor_bus = 0;
	newdev->businfo = (void *)info;
	newdev->priv = NULL;
	newdev->drv = NULL;
	newdev->name = core->name;
	newdev->next_in_drv = NULL;
	newdev->bus = NULL;

	/* Register new device */
	drvmgr_dev_register(newdev);

	return 0;
}

static int leon2_amba_init1(struct drvmgr_dev *dev)
{
	/* Init our own device */
	dev->priv = NULL;
	dev->name = "LEON2 AMBA";

	/* Init the bus */
	drvmgr_alloc_bus(&dev->bus, 0);
	dev->bus->bus_type = DRVMGR_BUS_TYPE_LEON2_AMBA;
	dev->bus->next = NULL;
	dev->bus->dev = dev;
	dev->bus->priv = NULL;
	dev->bus->children = NULL;
	dev->bus->ops = &leon2_amba_bus_ops;
	dev->bus->dev_cnt = 0;
	dev->bus->reslist = NULL;
	dev->bus->maps_up = leon2_bus_config->maps_up;
	dev->bus->maps_down = leon2_bus_config->maps_down;
	drvmgr_bus_register(dev->bus);

	return DRVMGR_OK;
}

static int leon2_amba_init2(struct drvmgr_dev *dev)
{
	return DRVMGR_OK;
}

static int leon2_amba_remove(struct drvmgr_dev *dev)
{
	return DRVMGR_OK;
}

int leon2_amba_bus_init1(struct drvmgr_bus *bus)
{
	struct leon2_core *core;
	int i;

	if ( leon2_bus_res )
		drvmgr_bus_res_add(bus, leon2_bus_res);

	/**** REGISTER NEW DEVICES ****/
	i=0;
	core = leon2_bus_config->std_cores;
	if ( core ) {
		while ( core->id.core_id ) {
			if ( leon2_amba_dev_register(bus, core, i) ) {
				return RTEMS_UNSATISFIED;
			}
			i++;
			core++;
		}
	}
	core = leon2_bus_config->custom_cores;
	if ( core ) {
		while ( core->id.core_id ) {
			if ( leon2_amba_dev_register(bus, core, i) ) {
				return RTEMS_UNSATISFIED;
			}
			i++;
			core++;
		}
	}

	return 0;
}

int leon2_amba_unite(struct drvmgr_drv *drv, struct drvmgr_dev *dev)
{
	struct leon2_amba_dev_info *info;
	struct leon2_amba_drv_info *adrv;
	struct leon2_amba_dev_id *id;

	if ( !drv || !dev || !dev->parent )
		return 0;

	if ( (drv->bus_type!=DRVMGR_BUS_TYPE_LEON2_AMBA) || (dev->parent->bus_type != DRVMGR_BUS_TYPE_LEON2_AMBA) ) {
		return 0;
	}

	info = (struct leon2_amba_dev_info *)dev->businfo;
	if ( !info ) 
		return 0;

	/* Get LEON2 AMBA driver info */
	adrv = (struct leon2_amba_drv_info *)drv;
	id = adrv->ids;
	if ( !id )
		return 0;

	while ( id->core_id ) {
		if ( id->core_id == info->core_id ) {
			/* Driver is suitable for device, Unite them */
			return 1;
		}
		id++;
	}

	return 0;
}

static int leon2_amba_get_irq(struct drvmgr_dev *dev, int index)
{
	int irq;
	struct leon2_amba_dev_info *info;

	if ( !dev || (index > 15) )
		return -1;

	/* Relative (positive) or absolute (negative) IRQ number */
	if ( index >= 0 ) {
		/* IRQ Index relative to Cores base IRQ */

		/* Get IRQ array configured by user */
		info = (struct leon2_amba_dev_info *)dev->businfo;
		irq = info->irqs[index];
		if ( irq == 0 )
			return -1;
	} else {
		/* Absolute IRQ number */
		irq = -index;
	}
	return irq;
}

int leon2_amba_int_register
	(
	struct drvmgr_dev *dev,
	int index,
	const char *info,
	drvmgr_isr isr,
	void *arg
	)
{
	int irq;

	irq = leon2_amba_get_irq(dev, index);
	if ( irq < 0 )
		return -1;

	DBG("Registering IRQ %d to func 0x%x arg 0x%x\n", irq, (unsigned int)isr, (unsigned int)arg);

	return BSP_shared_interrupt_register(irq, info, isr, arg);
}

int leon2_amba_int_unregister
	(
	struct drvmgr_dev *dev,
	int index,
	drvmgr_isr isr,
	void *arg
	)
{
	int irq;

	irq = leon2_amba_get_irq(dev, index);
	if ( irq < 0 )
		return -1;

	DBG("Unregistering IRQ %d to func 0x%x arg 0x%x\n", irq, (unsigned int)handler, (unsigned int)arg);

	return BSP_shared_interrupt_unregister(irq, isr, arg);
}

int leon2_amba_int_clear
	(
	struct drvmgr_dev *dev,
	int index
	)
{
	int irq;

	irq = leon2_amba_get_irq(dev, index);
	if ( irq < 0 )
		return -1;

	BSP_shared_interrupt_clear(irq);

	return DRVMGR_OK;
}

int leon2_amba_int_mask
	(
	struct drvmgr_dev *dev,
	int index
	)
{
	int irq;

	irq = leon2_amba_get_irq(dev, index);
	if ( irq < 0 )
		return -1;

	BSP_shared_interrupt_mask(irq);

	return DRVMGR_OK;
}

int leon2_amba_int_unmask
	(
	struct drvmgr_dev *dev,
	int index
	)
{
	int irq;

	irq = leon2_amba_get_irq(dev, index);
	if ( irq < 0 )
		return -1;

	BSP_shared_interrupt_unmask(irq);

	return DRVMGR_OK;
}

struct drvmgr_drv_ops leon2_amba_ops = 
{
	.init = {leon2_amba_init1, leon2_amba_init2, NULL, NULL},
	.remove = leon2_amba_remove,
	.info = NULL
};

struct drvmgr_drv leon2_bus_drv =
{
	DRVMGR_OBJ_DRV,			/* Driver */
	NULL,				/* Next driver */
	NULL,				/* Device list */
	DRIVER_LEON2_AMBA_ID,		/* Driver ID */
	"LEON2_AMBA_DRV",		/* Must be placed at top bus */
	DRVMGR_BUS_TYPE_ROOT,		/* Bus Type */
	&leon2_amba_ops,		/* Bus Operations */
	NULL,				/* Funcs */
	0,				/* Device Count */
	0,				/* Private structure size */
};