2 * Copyright (c) 2005 Olivier Houchard. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
36 #include <vm/vm_kern.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm_extern.h>
41 #define _ARM32_BUS_DMA_PRIVATE
42 #include <machine/bus.h>
43 #include <machine/intr.h>
45 #include <arm/at91/at91rm92reg.h>
46 #include <arm/at91/at91var.h>
48 static struct at91_softc *at91_softc;
51 at91_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
52 bus_space_handle_t *bshp)
57 if (pa >= 0xfff00000) {
58 *bshp = pa - 0xf0000000 + 0xd0000000;
63 endpa = round_page(bpa + size);
65 *bshp = (vm_offset_t)pmap_mapdev(pa, endpa - pa);
71 at91_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
73 vm_offset_t va, endva;
75 va = trunc_page((vm_offset_t)t);
76 endva = va + round_page(size);
78 /* Free the kernel virtual mapping. */
79 kmem_free(kernel_map, va, endva - va);
83 at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
84 bus_size_t size, bus_space_handle_t *nbshp)
87 *nbshp = bsh + offset;
92 at91_barrier(void *t, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
98 bs_protos(generic_armv4);
100 struct bus_space at91_bs_tag = {
104 /* mapping/unmapping */
109 /* allocation/deallocation */
118 generic_armv4_bs_r_2,
124 generic_armv4_bs_rm_2,
130 generic_armv4_bs_rr_2,
136 generic_armv4_bs_w_2,
142 generic_armv4_bs_wm_2,
148 generic_armv4_bs_wr_2,
160 generic_armv4_bs_sr_2,
166 generic_armv4_bs_c_2,
172 at91_probe(device_t dev)
174 device_set_desc(dev, "AT91 device bus");
179 at91_identify(driver_t *drv, device_t parent)
182 BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
185 struct arm32_dma_range *
186 bus_dma_get_range(void)
193 bus_dma_get_range_nb(void)
198 extern void irq_entry(void);
201 at91_add_child(device_t dev, int prio, const char *name, int unit,
202 bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
205 struct at91_ivar *ivar;
207 kid = device_add_child_ordered(dev, prio, name, unit);
209 printf("Can't add child %s%d ordered\n", name, unit);
212 ivar = malloc(sizeof(*ivar), M_DEVBUF, M_WAITOK | M_ZERO);
214 device_delete_child(dev, kid);
215 printf("Can't add alloc ivar\n");
218 device_set_ivars(kid, ivar);
219 resource_list_init(&ivar->resources);
221 bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
223 bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
225 bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
227 bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
241 struct cpu_devs at91rm9200_devs[] =
243 // All the "system" devices
246 AT91RM92_BASE + AT91RM92_ST_BASE, AT91RM92_ST_SIZE,
251 AT91RM92_BASE + AT91RM92_PIOA_BASE, AT91RM92_PIO_SIZE,
256 AT91RM92_BASE + AT91RM92_PIOB_BASE, AT91RM92_PIO_SIZE,
261 AT91RM92_BASE + AT91RM92_PIOC_BASE, AT91RM92_PIO_SIZE,
266 AT91RM92_BASE + AT91RM92_PIOD_BASE, AT91RM92_PIO_SIZE,
271 AT91RM92_BASE + AT91RM92_PMC_BASE, AT91RM92_PMC_SIZE,
276 AT91RM92_BASE + AT91RM92_AIC_BASE, AT91RM92_AIC_SIZE,
277 0 // Interrupt controller has no interrupts!
281 AT91RM92_BASE + AT91RM92_RTC_BASE, AT91RM92_RTC_SIZE,
286 AT91RM92_BASE + AT91RM92_MC_BASE, AT91RM92_MC_SIZE,
293 AT91RM92_BASE + AT91RM92_TC0_BASE, AT91RM92_TC_SIZE,
294 AT91RM92_IRQ_TC0, AT91RM92_IRQ_TC1, AT91RM92_IRQ_TC2
298 AT91RM92_BASE + AT91RM92_TC1_BASE, AT91RM92_TC_SIZE,
299 AT91RM92_IRQ_TC3, AT91RM92_IRQ_TC4, AT91RM92_IRQ_TC5
303 AT91RM92_BASE + AT91RM92_UDP_BASE, AT91RM92_UDP_SIZE,
308 AT91RM92_BASE + AT91RM92_MCI_BASE, AT91RM92_MCI_SIZE,
313 AT91RM92_BASE + AT91RM92_TWI_BASE, AT91RM92_TWI_SIZE,
318 AT91RM92_BASE + AT91RM92_EMAC_BASE, AT91RM92_EMAC_SIZE,
321 #ifndef SKYEYE_WORKAROUNDS
324 AT91RM92_BASE + AT91RM92_DBGU_BASE, AT91RM92_DBGU_SIZE,
329 AT91RM92_BASE + AT91RM92_USART0_BASE, AT91RM92_USART_SIZE,
334 AT91RM92_BASE + AT91RM92_USART1_BASE, AT91RM92_USART_SIZE,
339 AT91RM92_BASE + AT91RM92_USART2_BASE, AT91RM92_USART_SIZE,
344 AT91RM92_BASE + AT91RM92_USART3_BASE, AT91RM92_USART_SIZE,
350 AT91RM92_BASE + AT91RM92_USART0_BASE, AT91RM92_USART_SIZE,
356 AT91RM92_BASE + AT91RM92_SSC0_BASE, AT91RM92_SSC_SIZE,
361 AT91RM92_BASE + AT91RM92_SSC1_BASE, AT91RM92_SSC_SIZE,
366 AT91RM92_BASE + AT91RM92_SSC2_BASE, AT91RM92_SSC_SIZE,
371 AT91RM92_BASE + AT91RM92_SPI_BASE, AT91RM92_SPI_SIZE,
376 AT91RM92_OHCI_BASE, AT91RM92_OHCI_SIZE,
383 at91_cpu_add_builtin_children(device_t dev, struct at91_softc *sc)
386 struct cpu_devs *walker;
388 // XXX should look at the device id in the DBGU register and
389 // XXX based on the CPU load in these devices
390 for (i = 0, walker = at91rm9200_devs; walker->name; i++, walker++) {
391 at91_add_child(dev, i, walker->name, walker->unit,
392 walker->mem_base, walker->mem_len, walker->irq0,
393 walker->irq1, walker->irq2);
400 at91_attach(device_t dev)
402 struct at91_softc *sc = device_get_softc(dev);
406 sc->sc_st = &at91_bs_tag;
407 sc->sc_sh = AT91RM92_BASE;
409 if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91RM92_SYS_BASE,
410 AT91RM92_SYS_SIZE, &sc->sc_sys_sh) != 0)
411 panic("Enable to map IRQ registers");
412 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
413 sc->sc_irq_rman.rm_descr = "AT91 IRQs";
414 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
415 sc->sc_mem_rman.rm_descr = "AT91 Memory";
417 sc->sc_usbmem_rman.rm_type = RMAN_ARRAY;
418 sc->sc_usbmem_rman.rm_descr = "AT91RM9200 USB Memory-mapped regs";
420 if (rman_init(&sc->sc_irq_rman) != 0 ||
421 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
422 panic("at91_attach: failed to set up IRQ rman");
423 if (rman_init(&sc->sc_mem_rman) != 0 ||
424 rman_manage_region(&sc->sc_mem_rman, 0xdff00000ul,
426 panic("at91_attach: failed to set up memory rman");
427 if (rman_manage_region(&sc->sc_mem_rman, AT91RM92_OHCI_BASE,
428 AT91RM92_OHCI_BASE + AT91RM92_OHCI_SIZE - 1) != 0)
429 panic("at91_attach: failed to set up ohci memory");
431 for (i = 0; i < 32; i++) {
432 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SVR +
435 /* XXX: Give better priorities to IRQs */
436 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SMR + i * 4,
439 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_EOICR,
443 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SPU, 32);
445 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_DCR, 0);
446 /* Disable and clear all interrupts. */
447 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IDCR, 0xffffffff);
448 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_ICCR, 0xffffffff);
451 /* Disable all interrupts for RTC (0xe24 == RTC_IDR) */
452 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0xe24, 0xffffffff);
453 /* DIsable all interrupts for DBGU */
454 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0x20c, 0xffffffff);
455 /* Disable all interrupts for the SDRAM controller */
456 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0xfa8, 0xffffffff);
458 at91_cpu_add_builtin_children(dev, sc);
460 bus_generic_probe(dev);
461 bus_generic_attach(dev);
462 enable_interrupts(I32_bit | F32_bit);
466 static struct resource *
467 at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
468 u_long start, u_long end, u_long count, u_int flags)
470 struct at91_softc *sc = device_get_softc(dev);
471 struct resource_list_entry *rle;
472 struct at91_ivar *ivar = device_get_ivars(child);
473 struct resource_list *rl = &ivar->resources;
475 if (device_get_parent(child) != dev)
476 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
477 type, rid, start, end, count, flags));
479 rle = resource_list_find(rl, type, *rid);
483 panic("Resource rid %d type %d already in use", *rid, type);
484 if (start == 0UL && end == ~0UL) {
486 count = ulmax(count, rle->count);
487 end = ulmax(rle->end, start + count - 1);
492 rle->res = rman_reserve_resource(&sc->sc_irq_rman,
493 start, end, count, flags, child);
497 if (start >= 0x00300000 && start <= 0x003fffff)
498 rle->res = rman_reserve_resource(&sc->sc_usbmem_rman,
499 start, end, count, flags, child);
502 rle->res = rman_reserve_resource(&sc->sc_mem_rman,
503 start, end, count, flags, child);
504 rman_set_bustag(rle->res, &at91_bs_tag);
505 rman_set_bushandle(rle->res, start);
509 rle->start = rman_get_start(rle->res);
510 rle->end = rman_get_end(rle->res);
512 rman_set_rid(rle->res, *rid);
517 static struct resource_list *
518 at91_get_resource_list(device_t dev, device_t child)
520 struct at91_ivar *ivar;
522 ivar = device_get_ivars(child);
523 return (&(ivar->resources));
527 at91_release_resource(device_t dev, device_t child, int type,
528 int rid, struct resource *r)
530 struct resource_list *rl;
531 struct resource_list_entry *rle;
533 rl = at91_get_resource_list(dev, child);
536 rle = resource_list_find(rl, type, rid);
539 rman_release_resource(r);
545 at91_setup_intr(device_t dev, device_t child,
546 struct resource *ires, int flags, driver_filter_t *filt,
547 driver_intr_t *intr, void *arg, void **cookiep)
549 struct at91_softc *sc = device_get_softc(dev);
551 if (rman_get_start(ires) == AT91RM92_IRQ_SYSTEM && filt == NULL)
552 panic("All system interrupt ISRs must be FILTER");
553 BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt,
555 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IECR,
556 1 << rman_get_start(ires));
561 at91_teardown_intr(device_t dev, device_t child, struct resource *res,
564 struct at91_softc *sc = device_get_softc(dev);
566 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IDCR,
567 1 << rman_get_start(res));
568 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
572 at91_activate_resource(device_t bus, device_t child, int type, int rid,
579 if (type == SYS_RES_MEMORY) {
580 error = bus_space_map(rman_get_bustag(r),
581 rman_get_bushandle(r), rman_get_size(r), 0, &p);
584 rman_set_bushandle(r, p);
587 return (rman_activate_resource(r));
591 at91_print_child(device_t dev, device_t child)
593 struct at91_ivar *ivars;
594 struct resource_list *rl;
597 ivars = device_get_ivars(child);
598 rl = &ivars->resources;
600 retval += bus_print_child_header(dev, child);
602 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
603 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
604 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
605 if (device_get_flags(dev))
606 retval += printf(" flags %#x", device_get_flags(dev));
608 retval += bus_print_child_footer(dev, child);
614 arm_mask_irq(uintptr_t nb)
617 bus_space_write_4(at91_softc->sc_st,
618 at91_softc->sc_sys_sh, IC_IDCR, 1 << nb);
629 irq = bus_space_read_4(at91_softc->sc_st,
630 at91_softc->sc_sys_sh, IC_IVR);
631 status = bus_space_read_4(at91_softc->sc_st,
632 at91_softc->sc_sys_sh, IC_ISR);
634 bus_space_write_4(at91_softc->sc_st,
635 at91_softc->sc_sys_sh, IC_EOICR, 1);
642 arm_unmask_irq(uintptr_t nb)
645 bus_space_write_4(at91_softc->sc_st,
646 at91_softc->sc_sys_sh, IC_IECR, 1 << nb);
647 bus_space_write_4(at91_softc->sc_st, at91_softc->sc_sys_sh,
652 static device_method_t at91_methods[] = {
653 DEVMETHOD(device_probe, at91_probe),
654 DEVMETHOD(device_attach, at91_attach),
655 DEVMETHOD(device_identify, at91_identify),
657 DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
658 DEVMETHOD(bus_setup_intr, at91_setup_intr),
659 DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
660 DEVMETHOD(bus_activate_resource, at91_activate_resource),
661 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
662 DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
663 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
664 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
665 DEVMETHOD(bus_release_resource, at91_release_resource),
666 DEVMETHOD(bus_print_child, at91_print_child),
671 static driver_t at91_driver = {
674 sizeof(struct at91_softc),
676 static devclass_t at91_devclass;
678 DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);