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)
59 endpa = round_page(bpa + size);
61 *bshp = (vm_offset_t)pmap_mapdev(pa, endpa - pa);
67 at91_bs_unmap(void *t, bus_size_t size)
69 vm_offset_t va, endva;
71 va = trunc_page((vm_offset_t)t);
72 endva = va + round_page(size);
74 /* Free the kernel virtual mapping. */
75 kmem_free(kernel_map, va, endva - va);
79 at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
80 bus_size_t size, bus_space_handle_t *nbshp)
83 *nbshp = bsh + offset;
88 at91_barrier(void *t, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
94 bs_protos(generic_armv4);
96 struct bus_space at91_bs_tag = {
100 /* mapping/unmapping */
105 /* allocation/deallocation */
114 generic_armv4_bs_r_2,
120 generic_armv4_bs_rm_2,
126 generic_armv4_bs_rr_2,
132 generic_armv4_bs_w_2,
138 generic_armv4_bs_wm_2,
144 generic_armv4_bs_wr_2,
156 generic_armv4_bs_sr_2,
162 generic_armv4_bs_c_2,
168 at91_probe(device_t dev)
170 device_set_desc(dev, "AT91RM9200 device bus");
175 at91_identify(driver_t *drv, device_t parent)
178 BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
181 struct arm32_dma_range *
182 bus_dma_get_range(void)
189 bus_dma_get_range_nb(void)
194 extern void irq_entry(void);
197 at91_add_child(device_t dev, int prio, const char *name, int unit,
198 bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
201 struct at91_ivar *ivar;
203 kid = device_add_child_ordered(dev, prio, name, unit);
205 printf("Can't add child %s%d ordered\n", name, unit);
208 ivar = malloc(sizeof(*ivar), M_DEVBUF, M_WAITOK | M_ZERO);
210 device_delete_child(dev, kid);
211 printf("Can't add alloc ivar\n");
214 device_set_ivars(kid, ivar);
215 resource_list_init(&ivar->resources);
217 bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
219 bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
221 bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
223 bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
237 struct cpu_devs at91rm9200_devs[] =
239 // All the "system" devices
242 AT91RM92_BASE + AT91RM92_ST_BASE, AT91RM92_ST_SIZE,
247 AT91RM92_BASE + AT91RM92_PIOA_BASE, AT91RM92_PIO_SIZE,
252 AT91RM92_BASE + AT91RM92_PIOB_BASE, AT91RM92_PIO_SIZE,
257 AT91RM92_BASE + AT91RM92_PIOC_BASE, AT91RM92_PIO_SIZE,
262 AT91RM92_BASE + AT91RM92_PIOD_BASE, AT91RM92_PIO_SIZE,
267 AT91RM92_BASE + AT91RM92_PMC_BASE, AT91RM92_PMC_SIZE,
272 AT91RM92_BASE + AT91RM92_AIC_BASE, AT91RM92_AIC_SIZE,
273 0 // Interrupt controller has no interrupts!
277 AT91RM92_BASE + AT91RM92_RTC_BASE, AT91RM92_RTC_SIZE,
282 AT91RM92_BASE + AT91RM92_MC_BASE, AT91RM92_MC_SIZE,
289 AT91RM92_BASE + AT91RM92_TC0_BASE, AT91RM92_TC_SIZE,
290 AT91RM92_IRQ_TC0, AT91RM92_IRQ_TC1, AT91RM92_IRQ_TC2
294 AT91RM92_BASE + AT91RM92_TC1_BASE, AT91RM92_TC_SIZE,
295 AT91RM92_IRQ_TC3, AT91RM92_IRQ_TC4, AT91RM92_IRQ_TC5
299 AT91RM92_BASE + AT91RM92_UDP_BASE, AT91RM92_UDP_SIZE,
304 AT91RM92_BASE + AT91RM92_MCI_BASE, AT91RM92_MCI_SIZE,
309 AT91RM92_BASE + AT91RM92_TWI_BASE, AT91RM92_TWI_SIZE,
314 AT91RM92_BASE + AT91RM92_EMAC_BASE, AT91RM92_EMAC_SIZE,
317 #ifndef SKYEYE_WORKAROUNDS
320 AT91RM92_BASE + AT91RM92_DBGU_BASE, AT91RM92_DBGU_SIZE,
325 AT91RM92_BASE + AT91RM92_USART0_BASE, AT91RM92_USART_SIZE,
330 AT91RM92_BASE + AT91RM92_USART1_BASE, AT91RM92_USART_SIZE,
335 AT91RM92_BASE + AT91RM92_USART2_BASE, AT91RM92_USART_SIZE,
340 AT91RM92_BASE + AT91RM92_USART3_BASE, AT91RM92_USART_SIZE,
346 AT91RM92_BASE + AT91RM92_USART0_BASE, AT91RM92_USART_SIZE,
352 AT91RM92_BASE + AT91RM92_SSC0_BASE, AT91RM92_SSC_SIZE,
357 AT91RM92_BASE + AT91RM92_SSC1_BASE, AT91RM92_SSC_SIZE,
362 AT91RM92_BASE + AT91RM92_SSC2_BASE, AT91RM92_SSC_SIZE,
367 AT91RM92_BASE + AT91RM92_SPI_BASE, AT91RM92_SPI_SIZE,
372 AT91RM92_OHCI_BASE, AT91RM92_OHCI_SIZE,
379 at91_cpu_add_builtin_children(device_t dev, struct at91_softc *sc)
382 struct cpu_devs *walker;
384 // XXX should look at the device id in the DBGU register and
385 // XXX based on the CPU load in these devices
386 for (i = 0, walker = at91rm9200_devs; walker->name; i++, walker++) {
387 at91_add_child(dev, i, walker->name, walker->unit,
388 walker->mem_base, walker->mem_len, walker->irq0,
389 walker->irq1, walker->irq2);
396 at91_attach(device_t dev)
398 struct at91_softc *sc = device_get_softc(dev);
402 sc->sc_st = &at91_bs_tag;
403 sc->sc_sh = AT91RM92_BASE;
405 if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91RM92_SYS_BASE,
406 AT91RM92_SYS_SIZE, &sc->sc_sys_sh) != 0)
407 panic("Enable to map IRQ registers");
408 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
409 sc->sc_irq_rman.rm_descr = "AT91RM92 IRQs";
410 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
411 sc->sc_mem_rman.rm_descr = "AT91RM92 Memory";
413 sc->sc_usbmem_rman.rm_type = RMAN_ARRAY;
414 sc->sc_usbmem_rman.rm_descr = "AT91RM92 USB Memory-mapped regs";
416 if (rman_init(&sc->sc_irq_rman) != 0 ||
417 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
418 panic("at91_attach: failed to set up IRQ rman");
419 if (rman_init(&sc->sc_mem_rman) != 0 ||
420 rman_manage_region(&sc->sc_mem_rman, 0xfff00000ul,
422 panic("at91_attach: failed to set up memory rman");
423 if (rman_manage_region(&sc->sc_mem_rman, AT91RM92_OHCI_BASE,
424 AT91RM92_OHCI_BASE + AT91RM92_OHCI_SIZE - 1) != 0)
425 panic("at91_attach: failed to set up ohci memory");
427 for (i = 0; i < 32; i++) {
428 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SVR +
431 /* XXX: Give better priorities to IRQs */
432 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SMR + i * 4,
435 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_EOICR,
439 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_SPU, 32);
441 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_DCR, 0);
442 /* Disable and clear all interrupts. */
443 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IDCR, 0xffffffff);
444 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_ICCR, 0xffffffff);
447 /* Disable all interrupts for RTC (0xe24 == RTC_IDR) */
448 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0xe24, 0xffffffff);
449 /* DIsable all interrupts for DBGU */
450 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0x20c, 0xffffffff);
451 /* Disable all interrupts for the SDRAM controller */
452 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, 0xfa8, 0xffffffff);
454 at91_cpu_add_builtin_children(dev, sc);
456 bus_generic_probe(dev);
457 bus_generic_attach(dev);
458 enable_interrupts(I32_bit | F32_bit);
462 static struct resource *
463 at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
464 u_long start, u_long end, u_long count, u_int flags)
466 struct at91_softc *sc = device_get_softc(dev);
467 struct resource_list_entry *rle;
468 struct at91_ivar *ivar = device_get_ivars(child);
469 struct resource_list *rl = &ivar->resources;
471 if (device_get_parent(child) != dev)
472 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
473 type, rid, start, end, count, flags));
475 rle = resource_list_find(rl, type, *rid);
479 panic("Resource rid %d type %d already in use", *rid, type);
480 if (start == 0UL && end == ~0UL) {
482 count = ulmax(count, rle->count);
483 end = ulmax(rle->end, start + count - 1);
488 rle->res = rman_reserve_resource(&sc->sc_irq_rman,
489 start, end, count, flags, child);
493 if (start >= 0x00300000 && start <= 0x003fffff)
494 rle->res = rman_reserve_resource(&sc->sc_usbmem_rman,
495 start, end, count, flags, child);
498 rle->res = rman_reserve_resource(&sc->sc_mem_rman,
499 start, end, count, flags, child);
500 rman_set_bustag(rle->res, &at91_bs_tag);
501 rman_set_bushandle(rle->res, start);
505 rle->start = rman_get_start(rle->res);
506 rle->end = rman_get_end(rle->res);
508 rman_set_rid(rle->res, *rid);
513 static struct resource_list *
514 at91_get_resource_list(device_t dev, device_t child)
516 struct at91_ivar *ivar;
518 ivar = device_get_ivars(child);
519 return (&(ivar->resources));
523 at91_release_resource(device_t dev, device_t child, int type,
524 int rid, struct resource *r)
526 struct resource_list *rl;
527 struct resource_list_entry *rle;
529 rl = at91_get_resource_list(dev, child);
532 rle = resource_list_find(rl, type, rid);
535 rman_release_resource(r);
541 at91_setup_intr(device_t dev, device_t child,
542 struct resource *ires, int flags, driver_intr_t *intr, void *arg,
545 struct at91_softc *sc = device_get_softc(dev);
547 if (rman_get_start(ires) == AT91RM92_IRQ_SYSTEM && !(flags & INTR_FAST))
548 panic("All system interrupt ISRs must be type INTR_FAST");
549 BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, intr, arg,
551 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IECR,
552 1 << rman_get_start(ires));
557 at91_teardown_intr(device_t dev, device_t child, struct resource *res,
560 struct at91_softc *sc = device_get_softc(dev);
562 bus_space_write_4(sc->sc_st, sc->sc_sys_sh, IC_IDCR,
563 1 << rman_get_start(res));
564 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
568 at91_activate_resource(device_t bus, device_t child, int type, int rid,
575 if (type == SYS_RES_MEMORY) {
576 error = bus_space_map(rman_get_bustag(r),
577 rman_get_bushandle(r), rman_get_size(r), 0, &p);
580 rman_set_bushandle(r, p);
583 return (rman_activate_resource(r));
587 at91_print_child(device_t dev, device_t child)
589 struct at91_ivar *ivars;
590 struct resource_list *rl;
593 ivars = device_get_ivars(child);
594 rl = &ivars->resources;
596 retval += bus_print_child_header(dev, child);
598 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
599 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
600 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
601 if (device_get_flags(dev))
602 retval += printf(" flags %#x", device_get_flags(dev));
604 retval += bus_print_child_footer(dev, child);
610 arm_mask_irq(uintptr_t nb)
613 bus_space_write_4(at91_softc->sc_st,
614 at91_softc->sc_sys_sh, IC_IDCR, 1 << nb);
625 irq = bus_space_read_4(at91_softc->sc_st,
626 at91_softc->sc_sys_sh, IC_IVR);
627 status = bus_space_read_4(at91_softc->sc_st,
628 at91_softc->sc_sys_sh, IC_ISR);
630 bus_space_write_4(at91_softc->sc_st,
631 at91_softc->sc_sys_sh, IC_EOICR, 1);
638 arm_unmask_irq(uintptr_t nb)
641 bus_space_write_4(at91_softc->sc_st,
642 at91_softc->sc_sys_sh, IC_IECR, 1 << nb);
643 bus_space_write_4(at91_softc->sc_st, at91_softc->sc_sys_sh,
648 static device_method_t at91_methods[] = {
649 DEVMETHOD(device_probe, at91_probe),
650 DEVMETHOD(device_attach, at91_attach),
651 DEVMETHOD(device_identify, at91_identify),
653 DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
654 DEVMETHOD(bus_setup_intr, at91_setup_intr),
655 DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
656 DEVMETHOD(bus_activate_resource, at91_activate_resource),
657 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
658 DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
659 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
660 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
661 DEVMETHOD(bus_release_resource, at91_release_resource),
662 DEVMETHOD(bus_print_child, at91_print_child),
667 static driver_t at91_driver = {
670 sizeof(struct at91_softc),
672 static devclass_t at91_devclass;
674 DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);