1 /* $NetBSD: ixp425.c,v 1.10 2005/12/11 12:16:51 christos Exp $ */
5 * Ichiro FUKUHARA <ichiro@ichiro.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Ichiro FUKUHARA.
19 * 4. The name of the company nor the name of the author may be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #define _ARM32_BUS_DMA_PRIVATE
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/malloc.h>
49 #include <machine/bus.h>
50 #include <machine/intr.h>
54 #include <arm/xscale/ixp425/ixp425reg.h>
55 #include <arm/xscale/ixp425/ixp425var.h>
56 #include <arm/xscale/ixp425/ixp425_intr.h>
58 #include <dev/pci/pcireg.h>
60 volatile uint32_t intr_enabled;
61 uint32_t intr_steer = 0;
63 /* ixp43x et. al have +32 IRQ's */
64 volatile uint32_t intr_enabled2;
65 uint32_t intr_steer2 = 0;
67 struct ixp425_softc *ixp425_softc = NULL;
69 struct mtx ixp425_gpio_mtx;
71 static int ixp425_probe(device_t);
72 static void ixp425_identify(driver_t *, device_t);
73 static int ixp425_attach(device_t);
76 * Return a mask of the "fuse" bits that identify
77 * which h/w features are present.
78 * NB: assumes the expansion bus is mapped.
81 ixp4xx_read_feature_bits(void)
83 uint32_t bits = ~IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET);
84 bits &= ~EXP_FCTRL_RESVD;
86 bits &= ~EXP_FCTRL_IXP46X_ONLY;
91 ixp4xx_write_feature_bits(uint32_t v)
93 IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET) = ~v;
96 struct arm32_dma_range *
97 bus_dma_get_range(void)
103 bus_dma_get_range_nb(void)
108 static const uint8_t int2gpio[32] __attribute__ ((aligned(32))) = {
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#0 -> INT#5 */
110 0x00, 0x01, /* GPIO#0 -> GPIO#1 */
111 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#8 -> INT#13 */
112 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#14 -> INT#18 */
113 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* GPIO#2 -> GPIO#7 */
114 0x08, 0x09, 0x0a, 0x0b, 0x0c, /* GPIO#8 -> GPIO#12 */
115 0xff, 0xff /* INT#30 -> INT#31 */
118 static __inline uint32_t
119 ixp425_irq2gpio_bit(int irq)
121 return (1U << int2gpio[irq]);
127 DB_SHOW_COMMAND(gpio, db_show_gpio)
129 static const char *itype[8] = {
130 [GPIO_TYPE_ACT_HIGH] = "act-high",
131 [GPIO_TYPE_ACT_LOW] = "act-low",
132 [GPIO_TYPE_EDG_RISING] = "edge-rising",
133 [GPIO_TYPE_EDG_FALLING] = "edge-falling",
134 [GPIO_TYPE_TRANSITIONAL]= "transitional",
135 [5] = "type-5", [6] = "type-6", [7] = "type-7"
137 uint32_t gpoutr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
138 uint32_t gpoer = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
139 uint32_t gpinr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
140 uint32_t gpit1r = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPIT1R);
141 uint32_t gpit2r = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPIT2R);
144 db_printf("GPOUTR %08x GPINR %08x GPOER %08x GPISR %08x\n",
145 gpoutr, gpinr, gpoer,
146 GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPISR));
147 db_printf("GPIT1R %08x GPIT2R %08x GPCLKR %08x\n",
148 gpit1r, gpit2r, GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPCLKR));
149 for (i = 0; i < 16; i++) {
150 db_printf("[%2d] out %u in %u %-3s", i,
151 (gpoutr>>i)&1, (gpinr>>i)&1, (gpoer>>i)&1 ? "in" : "out");
152 for (j = 0; j < 32; j++)
153 if (int2gpio[j] == i) {
154 db_printf(" irq %2u %s", j, itype[
155 (((i & 8) ? gpit2r : gpit1r) >> (3*(i&7)))
165 ixp425_set_gpio(struct ixp425_softc *sc, int pin, int type)
167 uint32_t gpiotr = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(pin));
170 /* clear interrupt type */
171 GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(pin),
172 gpiotr &~ GPIO_TYPE(pin, GPIO_TYPE_MASK));
173 /* clear any pending interrupt */
174 GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR, (1<<pin));
175 /* set new interrupt type */
176 GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(pin),
177 gpiotr | GPIO_TYPE(pin, type));
179 /* configure gpio line as an input */
180 GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER,
181 GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER) | (1<<pin));
182 IXP4XX_GPIO_UNLOCK();
186 ixp425_gpio_ack(int irq)
188 if (irq < 32 && ((1 << irq) & IXP425_INT_GPIOMASK))
189 IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
190 ixp425_irq2gpio_bit(irq);
194 ixp425_post_filter(void *arg)
196 uintptr_t irq = (uintptr_t) arg;
197 ixp425_gpio_ack(irq);
201 arm_mask_irq(uintptr_t nb)
205 i = disable_interrupts(I32_bit);
207 intr_enabled &= ~(1 << nb);
208 ixp425_set_intrmask();
210 intr_enabled2 &= ~(1 << (nb - 32));
211 ixp435_set_intrmask();
213 restore_interrupts(i);
214 /*XXX; If it's a GPIO interrupt, ACK it know. Can it be a problem ?*/
219 arm_unmask_irq(uintptr_t nb)
223 i = disable_interrupts(I32_bit);
225 intr_enabled |= (1 << nb);
226 ixp425_set_intrmask();
228 intr_enabled2 |= (1 << (nb - 32));
229 ixp435_set_intrmask();
231 restore_interrupts(i);
234 static __inline uint32_t
235 ixp425_irq_read(void)
237 return IXPREG(IXP425_INT_STATUS) & intr_enabled;
240 static __inline uint32_t
241 ixp435_irq_read(void)
243 return IXPREG(IXP435_INT_STATUS2) & intr_enabled2;
247 arm_get_next_irq(int last)
251 last += 1; /* always advance fwd, NB: handles -1 */
253 mask = ixp425_irq_read() >> last;
254 for (; mask != 0; mask >>= 1, last++) {
260 if (cpu_is_ixp43x()) {
261 mask = ixp435_irq_read() >> (32-last);
262 for (; mask != 0; mask >>= 1, last++) {
274 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
275 IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
276 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
278 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
279 IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA |
280 OST_WDOG_ENAB_CNT_ENA);
281 printf("Reset failed!\n");
286 ixp425_identify(driver_t *driver, device_t parent)
288 BUS_ADD_CHILD(parent, 0, "ixp", 0);
292 ixp425_probe(device_t dev)
294 device_set_desc(dev, "Intel IXP4XX");
299 ixp425_attach(device_t dev)
301 struct ixp425_softc *sc;
303 device_printf(dev, "%b\n", ixp4xx_read_feature_bits(), EXP_FCTRL_BITS);
305 sc = device_get_softc(dev);
306 sc->sc_iot = &ixp425_bs_tag;
307 KASSERT(ixp425_softc == NULL, ("%s called twice?", __func__));
311 ixp425_set_intrmask();
312 ixp425_set_intrsteer();
313 if (cpu_is_ixp43x()) {
315 ixp435_set_intrmask();
316 ixp435_set_intrsteer();
318 arm_post_filter = ixp425_post_filter;
320 mtx_init(&ixp425_gpio_mtx, "gpio", NULL, MTX_DEF);
321 if (bus_space_map(sc->sc_iot, IXP425_GPIO_HWBASE, IXP425_GPIO_SIZE,
322 0, &sc->sc_gpio_ioh))
323 panic("%s: unable to map GPIO registers", __func__);
324 if (bus_space_map(sc->sc_iot, IXP425_EXP_HWBASE, IXP425_EXP_SIZE,
326 panic("%s: unable to map Expansion Bus registers", __func__);
328 /* XXX belongs in platform init */
330 cambria_exp_bus_init(sc);
332 if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
333 BUS_SPACE_MAXADDR, NULL, NULL, 0xffffffff, 0xff, 0xffffffff, 0,
334 NULL, NULL, &sc->sc_dmat))
335 panic("%s: failed to create dma tag", __func__);
337 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
338 sc->sc_irq_rman.rm_descr = "IXP4XX IRQs";
339 if (rman_init(&sc->sc_irq_rman) != 0 ||
340 rman_manage_region(&sc->sc_irq_rman, 0, cpu_is_ixp43x() ? 63 : 31) != 0)
341 panic("%s: failed to set up IRQ rman", __func__);
343 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
344 sc->sc_mem_rman.rm_descr = "IXP4XX Memory";
345 if (rman_init(&sc->sc_mem_rman) != 0 ||
346 rman_manage_region(&sc->sc_mem_rman, 0, ~0) != 0)
347 panic("%s: failed to set up memory rman", __func__);
349 BUS_ADD_CHILD(dev, 0, "pcib", 0);
350 BUS_ADD_CHILD(dev, 0, "ixpclk", 0);
351 BUS_ADD_CHILD(dev, 0, "ixpiic", 0);
352 /* XXX move to hints? */
353 BUS_ADD_CHILD(dev, 0, "ixpwdog", 0);
355 /* attach wired devices via hints */
356 bus_enumerate_hinted_children(dev);
358 bus_generic_probe(dev);
359 bus_generic_attach(dev);
365 ixp425_hinted_child(device_t bus, const char *dname, int dunit)
368 struct ixp425_ivar *ivar;
370 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
371 ivar = IXP425_IVAR(child);
372 resource_int_value(dname, dunit, "addr", &ivar->addr);
373 resource_int_value(dname, dunit, "irq", &ivar->irq);
377 ixp425_add_child(device_t dev, u_int order, const char *name, int unit)
380 struct ixp425_ivar *ivar;
382 child = device_add_child_ordered(dev, order, name, unit);
385 ivar = malloc(sizeof(struct ixp425_ivar), M_DEVBUF, M_NOWAIT);
387 device_delete_child(dev, child);
392 device_set_ivars(child, ivar);
397 ixp425_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
399 struct ixp425_ivar *ivar = IXP425_IVAR(child);
402 case IXP425_IVAR_ADDR:
403 if (ivar->addr != 0) {
404 *(uint32_t *)result = ivar->addr;
408 case IXP425_IVAR_IRQ:
409 if (ivar->irq != -1) {
410 *(int *)result = ivar->irq;
419 * NB: This table handles P->V translations for regions setup with
420 * static mappings in initarm. This is used solely for calls to
421 * bus_alloc_resource_any; anything done with bus_space_map is
422 * handled elsewhere and does not require an entry here.
424 * XXX this table is also used by uart_cpu_getdev via getvbase
425 * (hence the public api)
431 int isa4x; /* XXX needs special bus space tag */
432 int isslow; /* XXX needs special bus space tag */
435 static const struct hwvtrans *
436 gethwvtrans(uint32_t hwbase, uint32_t size)
438 static const struct hwvtrans hwvtrans[] = {
439 /* NB: needed only for uart_cpu_getdev */
440 { .hwbase = IXP425_UART0_HWBASE,
441 .size = IXP425_REG_SIZE,
442 .vbase = IXP425_UART0_VBASE,
444 { .hwbase = IXP425_UART1_HWBASE,
445 .size = IXP425_REG_SIZE,
446 .vbase = IXP425_UART1_VBASE,
448 { .hwbase = IXP425_PCI_HWBASE,
449 .size = IXP425_PCI_SIZE,
450 .vbase = IXP425_PCI_VBASE },
451 { .hwbase = IXP425_PCI_MEM_HWBASE,
452 .size = IXP425_PCI_MEM_SIZE,
453 .vbase = IXP425_PCI_MEM_VBASE },
454 { .hwbase = IXP425_EXP_BUS_CS0_HWBASE,
455 .size = IXP425_EXP_BUS_CS0_SIZE,
456 .vbase = IXP425_EXP_BUS_CS0_VBASE },
457 /* NB: needed for ixp435 ehci controllers */
458 { .hwbase = IXP435_USB1_HWBASE,
459 .size = IXP435_USB1_SIZE,
460 .vbase = IXP435_USB1_VBASE },
461 { .hwbase = IXP435_USB2_HWBASE,
462 .size = IXP435_USB2_SIZE,
463 .vbase = IXP435_USB2_VBASE },
464 { .hwbase = CAMBRIA_GPS_HWBASE,
465 .size = CAMBRIA_GPS_SIZE,
466 .vbase = CAMBRIA_GPS_VBASE,
468 { .hwbase = CAMBRIA_RS485_HWBASE,
469 .size = CAMBRIA_RS485_SIZE,
470 .vbase = CAMBRIA_RS485_VBASE,
475 for (i = 0; i < sizeof hwvtrans / sizeof *hwvtrans; i++) {
476 if (hwbase >= hwvtrans[i].hwbase &&
477 hwbase + size <= hwvtrans[i].hwbase + hwvtrans[i].size)
483 /* XXX for uart_cpu_getdev */
485 getvbase(uint32_t hwbase, uint32_t size, uint32_t *vbase)
487 const struct hwvtrans *hw;
489 hw = gethwvtrans(hwbase, size);
492 *vbase = hwbase - hw->hwbase + hw->vbase;
496 static struct resource *
497 ixp425_alloc_resource(device_t dev, device_t child, int type, int *rid,
498 u_long start, u_long end, u_long count, u_int flags)
500 struct ixp425_softc *sc = device_get_softc(dev);
501 const struct hwvtrans *vtrans;
504 int needactivate = flags & RF_ACTIVE;
510 /* override per hints */
511 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_IRQ, &irq) == 0)
513 rv = rman_reserve_resource(&sc->sc_irq_rman, start, end, count,
516 rman_set_rid(rv, *rid);
520 /* override per hints */
521 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_ADDR, &addr) == 0) {
523 /* XXX use nominal window to check for mapping */
524 vtrans = gethwvtrans(start, 0x1000);
525 if (vtrans != NULL) {
527 * Assign the entire mapped region; this may
528 * not be correct but without more info from
529 * the caller we cannot tell.
531 end = start + vtrans->size -
532 (start - vtrans->hwbase);
535 "%s: assign 0x%lx:0x%lx%s\n",
536 __func__, start, end - start,
537 vtrans->isa4x ? " A4X" :
538 vtrans->isslow ? " SLOW" : "");
541 vtrans = gethwvtrans(start, end - start);
542 if (vtrans == NULL) {
543 /* likely means above table needs to be updated */
544 device_printf(child, "%s: no mapping for 0x%lx:0x%lx\n",
545 __func__, start, end - start);
548 rv = rman_reserve_resource(&sc->sc_mem_rman, start, end,
549 end - start, flags, child);
551 device_printf(child, "%s: cannot reserve 0x%lx:0x%lx\n",
552 __func__, start, end - start);
555 rman_set_rid(rv, *rid);
561 if (rv != NULL && needactivate) {
562 if (bus_activate_resource(child, type, *rid, rv)) {
563 rman_release_resource(rv);
571 ixp425_release_resource(device_t bus, device_t child, int type, int rid,
574 /* NB: no private resources, just release */
575 return rman_release_resource(r);
579 ixp425_activate_resource(device_t dev, device_t child, int type, int rid,
582 struct ixp425_softc *sc = device_get_softc(dev);
583 const struct hwvtrans *vtrans;
585 if (type == SYS_RES_MEMORY) {
586 vtrans = gethwvtrans(rman_get_start(r), rman_get_size(r));
587 if (vtrans == NULL) { /* NB: should not happen */
588 device_printf(child, "%s: no mapping for 0x%lx:0x%lx\n",
589 __func__, rman_get_start(r), rman_get_size(r));
593 rman_set_bustag(r, &ixp425_a4x_bs_tag);
594 else if (vtrans->isslow)
595 rman_set_bustag(r, &cambria_exp_bs_tag);
597 rman_set_bustag(r, sc->sc_iot);
598 rman_set_bushandle(r, vtrans->vbase);
600 return (rman_activate_resource(r));
604 ixp425_deactivate_resource(device_t bus, device_t child, int type, int rid,
607 /* NB: no private resources, just deactive */
608 return (rman_deactivate_resource(r));
612 get_masks(struct resource *res, uint32_t *mask, uint32_t *mask2)
617 for (i = rman_get_start(res); i < 32 && i <= rman_get_end(res); i++)
620 for (; i <= rman_get_end(res); i++)
621 *mask2 |= 1 << (i - 32);
625 update_masks(uint32_t mask, uint32_t mask2)
629 ixp425_set_intrmask();
630 if (cpu_is_ixp43x()) {
631 intr_enabled2 = mask2;
632 ixp435_set_intrmask();
637 ixp425_setup_intr(device_t dev, device_t child,
638 struct resource *res, int flags, driver_filter_t *filt,
639 driver_intr_t *intr, void *arg, void **cookiep)
641 uint32_t mask, mask2;
643 BUS_SETUP_INTR(device_get_parent(dev), child, res, flags, filt, intr,
646 get_masks(res, &mask, &mask2);
647 update_masks(intr_enabled | mask, intr_enabled2 | mask2);
653 ixp425_teardown_intr(device_t dev, device_t child, struct resource *res,
656 uint32_t mask, mask2;
658 get_masks(res, &mask, &mask2);
659 update_masks(intr_enabled &~ mask, intr_enabled2 &~ mask2);
661 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
664 static device_method_t ixp425_methods[] = {
665 /* Device interface */
666 DEVMETHOD(device_probe, ixp425_probe),
667 DEVMETHOD(device_attach, ixp425_attach),
668 DEVMETHOD(device_identify, ixp425_identify),
671 DEVMETHOD(bus_add_child, ixp425_add_child),
672 DEVMETHOD(bus_hinted_child, ixp425_hinted_child),
673 DEVMETHOD(bus_read_ivar, ixp425_read_ivar),
675 DEVMETHOD(bus_alloc_resource, ixp425_alloc_resource),
676 DEVMETHOD(bus_release_resource, ixp425_release_resource),
677 DEVMETHOD(bus_activate_resource, ixp425_activate_resource),
678 DEVMETHOD(bus_deactivate_resource, ixp425_deactivate_resource),
679 DEVMETHOD(bus_setup_intr, ixp425_setup_intr),
680 DEVMETHOD(bus_teardown_intr, ixp425_teardown_intr),
685 static driver_t ixp425_driver = {
688 sizeof(struct ixp425_softc),
690 static devclass_t ixp425_devclass;
692 DRIVER_MODULE(ixp, nexus, ixp425_driver, ixp425_devclass, 0, 0);