at91.c revision 257370
1/*-
2 * Copyright (c) 2005 Olivier Houchard.  All rights reserved.
3 * Copyright (c) 2010 Greg Ansley.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/arm/at91/at91.c 257370 2013-10-29 23:55:17Z nwhitehorn $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36
37#include <vm/vm.h>
38#include <vm/vm_kern.h>
39#include <vm/pmap.h>
40#include <vm/vm_page.h>
41#include <vm/vm_extern.h>
42
43#define	_ARM32_BUS_DMA_PRIVATE
44#include <machine/bus.h>
45#include <machine/intr.h>
46
47#include <arm/at91/at91var.h>
48#include <arm/at91/at91_pmcvar.h>
49#include <arm/at91/at91_aicreg.h>
50
51static struct at91_softc *at91_softc;
52
53static void at91_eoi(void *);
54
55extern const struct pmap_devmap at91_devmap[];
56
57uint32_t at91_master_clock;
58
59static int
60at91_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
61    bus_space_handle_t *bshp)
62{
63	vm_paddr_t pa, endpa;
64
65	pa = trunc_page(bpa);
66	if (pa >= AT91_PA_BASE + 0xff00000) {
67		*bshp = pa - AT91_PA_BASE + AT91_BASE;
68		return (0);
69	}
70	if (pa >= AT91_BASE + 0xff00000)
71		return (0);
72	endpa = round_page(bpa + size);
73
74	*bshp = (vm_offset_t)pmap_mapdev(pa, endpa - pa);
75
76	return (0);
77}
78
79static void
80at91_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
81{
82	vm_offset_t va, endva;
83
84	va = trunc_page((vm_offset_t)t);
85	endva = va + round_page(size);
86
87	/* Free the kernel virtual mapping. */
88	kva_free(va, endva - va);
89}
90
91static int
92at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
93    bus_size_t size, bus_space_handle_t *nbshp)
94{
95
96	*nbshp = bsh + offset;
97	return (0);
98}
99
100static void
101at91_barrier(void *t, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
102    int a)
103{
104}
105
106struct arm32_dma_range *
107bus_dma_get_range(void)
108{
109
110	return (NULL);
111}
112
113int
114bus_dma_get_range_nb(void)
115{
116	return (0);
117}
118
119bs_protos(generic);
120bs_protos(generic_armv4);
121
122struct bus_space at91_bs_tag = {
123	/* cookie */
124	(void *) 0,
125
126	/* mapping/unmapping */
127	at91_bs_map,
128	at91_bs_unmap,
129	at91_bs_subregion,
130
131	/* allocation/deallocation */
132	NULL,
133	NULL,
134
135	/* barrier */
136	at91_barrier,
137
138	/* read (single) */
139	generic_bs_r_1,
140	generic_armv4_bs_r_2,
141	generic_bs_r_4,
142	NULL,
143
144	/* read multiple */
145	generic_bs_rm_1,
146	generic_armv4_bs_rm_2,
147	generic_bs_rm_4,
148	NULL,
149
150	/* read region */
151	generic_bs_rr_1,
152	generic_armv4_bs_rr_2,
153	generic_bs_rr_4,
154	NULL,
155
156	/* write (single) */
157	generic_bs_w_1,
158	generic_armv4_bs_w_2,
159	generic_bs_w_4,
160	NULL,
161
162	/* write multiple */
163	generic_bs_wm_1,
164	generic_armv4_bs_wm_2,
165	generic_bs_wm_4,
166	NULL,
167
168	/* write region */
169	NULL,
170	generic_armv4_bs_wr_2,
171	generic_bs_wr_4,
172	NULL,
173
174	/* set multiple */
175	NULL,
176	NULL,
177	NULL,
178	NULL,
179
180	/* set region */
181	NULL,
182	generic_armv4_bs_sr_2,
183	generic_bs_sr_4,
184	NULL,
185
186	/* copy */
187	NULL,
188	generic_armv4_bs_c_2,
189	NULL,
190	NULL,
191
192	/* read (single) stream */
193	generic_bs_r_1,
194	generic_armv4_bs_r_2,
195	generic_bs_r_4,
196	NULL,
197
198	/* read multiple stream */
199	generic_bs_rm_1,
200	generic_armv4_bs_rm_2,
201	generic_bs_rm_4,
202	NULL,
203
204	/* read region stream */
205	generic_bs_rr_1,
206	generic_armv4_bs_rr_2,
207	generic_bs_rr_4,
208	NULL,
209
210	/* write (single) stream */
211	generic_bs_w_1,
212	generic_armv4_bs_w_2,
213	generic_bs_w_4,
214	NULL,
215
216	/* write multiple stream */
217	generic_bs_wm_1,
218	generic_armv4_bs_wm_2,
219	generic_bs_wm_4,
220	NULL,
221
222	/* write region stream */
223	NULL,
224	generic_armv4_bs_wr_2,
225	generic_bs_wr_4,
226	NULL,
227};
228
229static int
230at91_probe(device_t dev)
231{
232
233	device_set_desc(dev, "AT91 device bus");
234	return (BUS_PROBE_NOWILDCARD);
235}
236
237static void
238at91_identify(driver_t *drv, device_t parent)
239{
240
241	BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
242}
243
244static void
245at91_cpu_add_builtin_children(device_t dev, const struct cpu_devs *walker)
246{
247	int i;
248
249	for (i = 1; walker->name; i++, walker++) {
250		at91_add_child(dev, i, walker->name, walker->unit,
251		    walker->mem_base, walker->mem_len, walker->irq0,
252		    walker->irq1, walker->irq2);
253	}
254}
255
256static int
257at91_attach(device_t dev)
258{
259	struct at91_softc *sc = device_get_softc(dev);
260	const struct pmap_devmap *pdevmap;
261	int i;
262
263	arm_post_filter = at91_eoi;
264
265	at91_softc = sc;
266	sc->sc_st = &at91_bs_tag;
267	sc->sc_sh = AT91_BASE;
268	sc->sc_aic_sh = AT91_BASE + AT91_SYS_BASE;
269	sc->dev = dev;
270
271	sc->sc_irq_rman.rm_type = RMAN_ARRAY;
272	sc->sc_irq_rman.rm_descr = "AT91 IRQs";
273	if (rman_init(&sc->sc_irq_rman) != 0 ||
274	    rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
275		panic("at91_attach: failed to set up IRQ rman");
276
277	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
278	sc->sc_mem_rman.rm_descr = "AT91 Memory";
279	if (rman_init(&sc->sc_mem_rman) != 0)
280		panic("at91_attach: failed to set up memory rman");
281	for (pdevmap = at91_devmap; pdevmap->pd_va != 0; pdevmap++) {
282		if (rman_manage_region(&sc->sc_mem_rman, pdevmap->pd_va,
283		    pdevmap->pd_va + pdevmap->pd_size - 1) != 0)
284			panic("at91_attach: failed to set up memory rman");
285	}
286
287	/*
288	 * Setup the interrupt table.
289	 */
290	if (soc_info.soc_data == NULL || soc_info.soc_data->soc_irq_prio == NULL)
291		panic("Interrupt priority table missing\n");
292	for (i = 0; i < 32; i++) {
293		bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SVR +
294		    i * 4, i);
295		/* Priority. */
296		bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SMR + i * 4,
297		    soc_info.soc_data->soc_irq_prio[i]);
298		if (i < 8)
299			bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_EOICR,
300			    1);
301	}
302
303	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SPU, 32);
304	/* No debug. */
305	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_DCR, 0);
306	/* Disable and clear all interrupts. */
307	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR, 0xffffffff);
308	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_ICCR, 0xffffffff);
309
310        /*
311         * Add this device's children...
312         */
313	at91_cpu_add_builtin_children(dev, soc_info.soc_data->soc_children);
314	soc_info.soc_data->soc_clock_init();
315
316	bus_generic_probe(dev);
317	bus_generic_attach(dev);
318	enable_interrupts(I32_bit | F32_bit);
319	return (0);
320}
321
322static struct resource *
323at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
324    u_long start, u_long end, u_long count, u_int flags)
325{
326	struct at91_softc *sc = device_get_softc(dev);
327	struct resource_list_entry *rle;
328	struct at91_ivar *ivar = device_get_ivars(child);
329	struct resource_list *rl = &ivar->resources;
330
331	if (device_get_parent(child) != dev)
332		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
333		    type, rid, start, end, count, flags));
334
335	rle = resource_list_find(rl, type, *rid);
336	if (rle == NULL)
337		return (NULL);
338	if (rle->res)
339		panic("Resource rid %d type %d already in use", *rid, type);
340	if (start == 0UL && end == ~0UL) {
341		start = rle->start;
342		count = ulmax(count, rle->count);
343		end = ulmax(rle->end, start + count - 1);
344	}
345	switch (type)
346	{
347	case SYS_RES_IRQ:
348		rle->res = rman_reserve_resource(&sc->sc_irq_rman,
349		    start, end, count, flags, child);
350		break;
351	case SYS_RES_MEMORY:
352		rle->res = rman_reserve_resource(&sc->sc_mem_rman,
353		    start, end, count, flags, child);
354		if (rle->res != NULL) {
355			rman_set_bustag(rle->res, &at91_bs_tag);
356			rman_set_bushandle(rle->res, start);
357		}
358		break;
359	}
360	if (rle->res) {
361		rle->start = rman_get_start(rle->res);
362		rle->end = rman_get_end(rle->res);
363		rle->count = count;
364		rman_set_rid(rle->res, *rid);
365	}
366	return (rle->res);
367}
368
369static struct resource_list *
370at91_get_resource_list(device_t dev, device_t child)
371{
372	struct at91_ivar *ivar;
373
374	ivar = device_get_ivars(child);
375	return (&(ivar->resources));
376}
377
378static int
379at91_release_resource(device_t dev, device_t child, int type,
380    int rid, struct resource *r)
381{
382	struct resource_list *rl;
383	struct resource_list_entry *rle;
384
385	rl = at91_get_resource_list(dev, child);
386	if (rl == NULL)
387		return (EINVAL);
388	rle = resource_list_find(rl, type, rid);
389	if (rle == NULL)
390		return (EINVAL);
391	rman_release_resource(r);
392	rle->res = NULL;
393	return (0);
394}
395
396static int
397at91_setup_intr(device_t dev, device_t child,
398    struct resource *ires, int flags, driver_filter_t *filt,
399    driver_intr_t *intr, void *arg, void **cookiep)
400{
401	int error;
402
403	if (rman_get_start(ires) == AT91_IRQ_SYSTEM && filt == NULL)
404		panic("All system interrupt ISRs must be FILTER");
405	error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
406	    filt, intr, arg, cookiep);
407	if (error)
408		return (error);
409
410	return (0);
411}
412
413static int
414at91_teardown_intr(device_t dev, device_t child, struct resource *res,
415    void *cookie)
416{
417	struct at91_softc *sc = device_get_softc(dev);
418
419	bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR,
420	    1 << rman_get_start(res));
421	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
422}
423
424static int
425at91_activate_resource(device_t bus, device_t child, int type, int rid,
426    struct resource *r)
427{
428#if 0
429	u_long p;
430	int error;
431
432	if (type == SYS_RES_MEMORY) {
433		error = bus_space_map(rman_get_bustag(r),
434		    rman_get_bushandle(r), rman_get_size(r), 0, &p);
435		if (error)
436			return (error);
437		rman_set_bushandle(r, p);
438	}
439#endif
440	return (rman_activate_resource(r));
441}
442
443static int
444at91_print_child(device_t dev, device_t child)
445{
446	struct at91_ivar *ivars;
447	struct resource_list *rl;
448	int retval = 0;
449
450	ivars = device_get_ivars(child);
451	rl = &ivars->resources;
452
453	retval += bus_print_child_header(dev, child);
454
455	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
456	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
457	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
458	if (device_get_flags(dev))
459		retval += printf(" flags %#x", device_get_flags(dev));
460
461	retval += bus_print_child_footer(dev, child);
462
463	return (retval);
464}
465
466void
467arm_mask_irq(uintptr_t nb)
468{
469
470	bus_space_write_4(at91_softc->sc_st,
471	    at91_softc->sc_aic_sh, IC_IDCR, 1 << nb);
472}
473
474int
475arm_get_next_irq(int last __unused)
476{
477	int status;
478	int irq;
479
480	irq = bus_space_read_4(at91_softc->sc_st,
481	    at91_softc->sc_aic_sh, IC_IVR);
482	status = bus_space_read_4(at91_softc->sc_st,
483	    at91_softc->sc_aic_sh, IC_ISR);
484	if (status == 0) {
485		bus_space_write_4(at91_softc->sc_st,
486		    at91_softc->sc_aic_sh, IC_EOICR, 1);
487		return (-1);
488	}
489	return (irq);
490}
491
492void
493arm_unmask_irq(uintptr_t nb)
494{
495
496	bus_space_write_4(at91_softc->sc_st,
497	at91_softc->sc_aic_sh, IC_IECR, 1 << nb);
498	bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
499	    IC_EOICR, 0);
500}
501
502static void
503at91_eoi(void *unused)
504{
505	bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
506	    IC_EOICR, 0);
507}
508
509void
510at91_add_child(device_t dev, int prio, const char *name, int unit,
511    bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
512{
513	device_t kid;
514	struct at91_ivar *ivar;
515
516	kid = device_add_child_ordered(dev, prio, name, unit);
517	if (kid == NULL) {
518	    printf("Can't add child %s%d ordered\n", name, unit);
519	    return;
520	}
521	ivar = malloc(sizeof(*ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
522	if (ivar == NULL) {
523		device_delete_child(dev, kid);
524		printf("Can't add alloc ivar\n");
525		return;
526	}
527	device_set_ivars(kid, ivar);
528	resource_list_init(&ivar->resources);
529	if (irq0 != -1) {
530		bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
531		if (irq0 != AT91_IRQ_SYSTEM)
532			at91_pmc_clock_add(device_get_nameunit(kid), irq0, 0);
533	}
534	if (irq1 != 0)
535		bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
536	if (irq2 != 0)
537		bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
538	if (addr != 0 && addr < AT91_BASE)
539		addr += AT91_BASE;
540	if (addr != 0)
541		bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
542}
543
544static device_method_t at91_methods[] = {
545	DEVMETHOD(device_probe, at91_probe),
546	DEVMETHOD(device_attach, at91_attach),
547	DEVMETHOD(device_identify, at91_identify),
548
549	DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
550	DEVMETHOD(bus_setup_intr, at91_setup_intr),
551	DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
552	DEVMETHOD(bus_activate_resource, at91_activate_resource),
553	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
554	DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
555	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
556	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
557	DEVMETHOD(bus_release_resource,	at91_release_resource),
558	DEVMETHOD(bus_print_child,	at91_print_child),
559
560	{0, 0},
561};
562
563static driver_t at91_driver = {
564	"atmelarm",
565	at91_methods,
566	sizeof(struct at91_softc),
567};
568
569static devclass_t at91_devclass;
570
571DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);
572