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