1 /* $NetBSD: ixp425.c,v 1.10 2005/12/11 12:16:51 christos Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
7 * Ichiro FUKUHARA <ichiro@ichiro.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Ichiro FUKUHARA.
21 * 4. The name of the company nor the name of the author may be used to
22 * endorse or promote products derived from this software without specific
23 * prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #define _ARM32_BUS_DMA_PRIVATE
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
51 #include <machine/armreg.h>
52 #include <machine/bus.h>
53 #include <machine/intr.h>
57 #include <arm/xscale/ixp425/ixp425reg.h>
58 #include <arm/xscale/ixp425/ixp425var.h>
59 #include <arm/xscale/ixp425/ixp425_intr.h>
61 #include <dev/pci/pcireg.h>
63 volatile uint32_t intr_enabled;
64 uint32_t intr_steer = 0;
66 /* ixp43x et. al have +32 IRQ's */
67 volatile uint32_t intr_enabled2;
68 uint32_t intr_steer2 = 0;
70 struct ixp425_softc *ixp425_softc = NULL;
72 struct mtx ixp425_gpio_mtx;
74 static int ixp425_probe(device_t);
75 static void ixp425_identify(driver_t *, device_t);
76 static int ixp425_attach(device_t);
79 * Return a mask of the "fuse" bits that identify
80 * which h/w features are present.
81 * NB: assumes the expansion bus is mapped.
84 ixp4xx_read_feature_bits(void)
86 uint32_t bits = ~IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET);
87 bits &= ~EXP_FCTRL_RESVD;
89 bits &= ~EXP_FCTRL_IXP46X_ONLY;
94 ixp4xx_write_feature_bits(uint32_t v)
96 IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET) = ~v;
99 struct arm32_dma_range *
100 bus_dma_get_range(void)
106 bus_dma_get_range_nb(void)
111 static const uint8_t int2gpio[32] __attribute__ ((aligned(32))) = {
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#0 -> INT#5 */
113 0x00, 0x01, /* GPIO#0 -> GPIO#1 */
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#8 -> INT#13 */
115 0xff, 0xff, 0xff, 0xff, 0xff, /* INT#14 -> INT#18 */
116 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* GPIO#2 -> GPIO#7 */
117 0x08, 0x09, 0x0a, 0x0b, 0x0c, /* GPIO#8 -> GPIO#12 */
118 0xff, 0xff /* INT#30 -> INT#31 */
121 static __inline uint32_t
122 ixp425_irq2gpio_bit(int irq)
124 return (1U << int2gpio[irq]);
130 DB_SHOW_COMMAND(gpio, db_show_gpio)
132 static const char *itype[8] = {
133 [GPIO_TYPE_ACT_HIGH] = "act-high",
134 [GPIO_TYPE_ACT_LOW] = "act-low",
135 [GPIO_TYPE_EDG_RISING] = "edge-rising",
136 [GPIO_TYPE_EDG_FALLING] = "edge-falling",
137 [GPIO_TYPE_TRANSITIONAL]= "transitional",
138 [5] = "type-5", [6] = "type-6", [7] = "type-7"
140 uint32_t gpoutr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
141 uint32_t gpoer = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
142 uint32_t gpinr = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
143 uint32_t gpit1r = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPIT1R);
144 uint32_t gpit2r = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPIT2R);
147 db_printf("GPOUTR %08x GPINR %08x GPOER %08x GPISR %08x\n",
148 gpoutr, gpinr, gpoer,
149 GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPISR));
150 db_printf("GPIT1R %08x GPIT2R %08x GPCLKR %08x\n",
151 gpit1r, gpit2r, GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPCLKR));
152 for (i = 0; i < 16; i++) {
153 db_printf("[%2d] out %u in %u %-3s", i,
154 (gpoutr>>i)&1, (gpinr>>i)&1, (gpoer>>i)&1 ? "in" : "out");
155 for (j = 0; j < 32; j++)
156 if (int2gpio[j] == i) {
157 db_printf(" irq %2u %s", j, itype[
158 (((i & 8) ? gpit2r : gpit1r) >> (3*(i&7)))
168 ixp425_set_gpio(struct ixp425_softc *sc, int pin, int type)
170 uint32_t gpiotr = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(pin));
173 /* clear interrupt type */
174 GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(pin),
175 gpiotr &~ GPIO_TYPE(pin, GPIO_TYPE_MASK));
176 /* clear any pending interrupt */
177 GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR, (1<<pin));
178 /* set new interrupt type */
179 GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(pin),
180 gpiotr | GPIO_TYPE(pin, type));
182 /* configure gpio line as an input */
183 GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER,
184 GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER) | (1<<pin));
185 IXP4XX_GPIO_UNLOCK();
189 ixp425_gpio_ack(int irq)
191 if (irq < 32 && ((1 << irq) & IXP425_INT_GPIOMASK))
192 IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
193 ixp425_irq2gpio_bit(irq);
197 ixp425_post_filter(void *arg)
199 uintptr_t irq = (uintptr_t) arg;
200 ixp425_gpio_ack(irq);
204 arm_mask_irq(uintptr_t nb)
208 i = disable_interrupts(PSR_I);
210 intr_enabled &= ~(1 << nb);
211 ixp425_set_intrmask();
213 intr_enabled2 &= ~(1 << (nb - 32));
214 ixp435_set_intrmask();
216 restore_interrupts(i);
217 /*XXX; If it's a GPIO interrupt, ACK it know. Can it be a problem ?*/
222 arm_unmask_irq(uintptr_t nb)
226 i = disable_interrupts(PSR_I);
228 intr_enabled |= (1 << nb);
229 ixp425_set_intrmask();
231 intr_enabled2 |= (1 << (nb - 32));
232 ixp435_set_intrmask();
234 restore_interrupts(i);
237 static __inline uint32_t
238 ixp425_irq_read(void)
240 return IXPREG(IXP425_INT_STATUS) & intr_enabled;
243 static __inline uint32_t
244 ixp435_irq_read(void)
246 return IXPREG(IXP435_INT_STATUS2) & intr_enabled2;
250 arm_get_next_irq(int last)
254 last += 1; /* always advance fwd, NB: handles -1 */
256 mask = ixp425_irq_read() >> last;
257 for (; mask != 0; mask >>= 1, last++) {
263 if (cpu_is_ixp43x()) {
264 mask = ixp435_irq_read() >> (32-last);
265 for (; mask != 0; mask >>= 1, last++) {
277 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
278 IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
279 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
281 bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
282 IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA |
283 OST_WDOG_ENAB_CNT_ENA);
284 printf("Reset failed!\n");
289 ixp425_identify(driver_t *driver, device_t parent)
291 BUS_ADD_CHILD(parent, 0, "ixp", 0);
295 ixp425_probe(device_t dev)
297 device_set_desc(dev, "Intel IXP4XX");
302 ixp425_attach(device_t dev)
304 struct ixp425_softc *sc;
306 device_printf(dev, "%b\n", ixp4xx_read_feature_bits(), EXP_FCTRL_BITS);
308 sc = device_get_softc(dev);
309 sc->sc_iot = &ixp425_bs_tag;
310 KASSERT(ixp425_softc == NULL, ("%s called twice?", __func__));
314 ixp425_set_intrmask();
315 ixp425_set_intrsteer();
316 if (cpu_is_ixp43x()) {
318 ixp435_set_intrmask();
319 ixp435_set_intrsteer();
321 arm_post_filter = ixp425_post_filter;
323 mtx_init(&ixp425_gpio_mtx, "gpio", NULL, MTX_DEF);
324 if (bus_space_map(sc->sc_iot, IXP425_GPIO_HWBASE, IXP425_GPIO_SIZE,
325 0, &sc->sc_gpio_ioh))
326 panic("%s: unable to map GPIO registers", __func__);
327 if (bus_space_map(sc->sc_iot, IXP425_EXP_HWBASE, IXP425_EXP_SIZE,
329 panic("%s: unable to map Expansion Bus registers", __func__);
331 /* XXX belongs in platform init */
333 cambria_exp_bus_init(sc);
335 if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
336 BUS_SPACE_MAXADDR, NULL, NULL, 0xffffffff, 0xff, 0xffffffff, 0,
337 NULL, NULL, &sc->sc_dmat))
338 panic("%s: failed to create dma tag", __func__);
340 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
341 sc->sc_irq_rman.rm_descr = "IXP4XX IRQs";
342 if (rman_init(&sc->sc_irq_rman) != 0 ||
343 rman_manage_region(&sc->sc_irq_rman, 0, cpu_is_ixp43x() ? 63 : 31) != 0)
344 panic("%s: failed to set up IRQ rman", __func__);
346 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
347 sc->sc_mem_rman.rm_descr = "IXP4XX Memory";
348 if (rman_init(&sc->sc_mem_rman) != 0 ||
349 rman_manage_region(&sc->sc_mem_rman, 0, ~0) != 0)
350 panic("%s: failed to set up memory rman", __func__);
352 BUS_ADD_CHILD(dev, 0, "pcib", 0);
353 BUS_ADD_CHILD(dev, 0, "ixpclk", 0);
354 BUS_ADD_CHILD(dev, 0, "ixpiic", 0);
355 /* XXX move to hints? */
356 BUS_ADD_CHILD(dev, 0, "ixpwdog", 0);
358 /* attach wired devices via hints */
359 bus_enumerate_hinted_children(dev);
361 bus_generic_probe(dev);
362 bus_generic_attach(dev);
368 ixp425_hinted_child(device_t bus, const char *dname, int dunit)
371 struct ixp425_ivar *ivar;
373 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
374 ivar = IXP425_IVAR(child);
375 resource_int_value(dname, dunit, "addr", &ivar->addr);
376 resource_int_value(dname, dunit, "irq", &ivar->irq);
380 ixp425_add_child(device_t dev, u_int order, const char *name, int unit)
383 struct ixp425_ivar *ivar;
385 child = device_add_child_ordered(dev, order, name, unit);
388 ivar = malloc(sizeof(struct ixp425_ivar), M_DEVBUF, M_NOWAIT);
390 device_delete_child(dev, child);
395 device_set_ivars(child, ivar);
400 ixp425_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
402 struct ixp425_ivar *ivar = IXP425_IVAR(child);
405 case IXP425_IVAR_ADDR:
406 if (ivar->addr != 0) {
407 *(uint32_t *)result = ivar->addr;
411 case IXP425_IVAR_IRQ:
412 if (ivar->irq != -1) {
413 *(int *)result = ivar->irq;
422 * NB: This table handles P->V translations for regions setup with
423 * static mappings in initarm. This is used solely for calls to
424 * bus_alloc_resource_any; anything done with bus_space_map is
425 * handled elsewhere and does not require an entry here.
427 * XXX this table is also used by uart_cpu_getdev via getvbase
428 * (hence the public api)
434 int isa4x; /* XXX needs special bus space tag */
435 int isslow; /* XXX needs special bus space tag */
438 static const struct hwvtrans *
439 gethwvtrans(uint32_t hwbase, uint32_t size)
441 static const struct hwvtrans hwvtrans[] = {
442 /* NB: needed only for uart_cpu_getdev */
443 { .hwbase = IXP425_UART0_HWBASE,
444 .size = IXP425_REG_SIZE,
445 .vbase = IXP425_UART0_VBASE,
447 { .hwbase = IXP425_UART1_HWBASE,
448 .size = IXP425_REG_SIZE,
449 .vbase = IXP425_UART1_VBASE,
451 { .hwbase = IXP425_PCI_HWBASE,
452 .size = IXP425_PCI_SIZE,
453 .vbase = IXP425_PCI_VBASE },
454 { .hwbase = IXP425_PCI_MEM_HWBASE,
455 .size = IXP425_PCI_MEM_SIZE,
456 .vbase = IXP425_PCI_MEM_VBASE },
457 { .hwbase = IXP425_EXP_BUS_CS0_HWBASE,
458 .size = IXP425_EXP_BUS_CS0_SIZE,
459 .vbase = IXP425_EXP_BUS_CS0_VBASE },
460 /* NB: needed for ixp435 ehci controllers */
461 { .hwbase = IXP435_USB1_HWBASE,
462 .size = IXP435_USB1_SIZE,
463 .vbase = IXP435_USB1_VBASE },
464 { .hwbase = IXP435_USB2_HWBASE,
465 .size = IXP435_USB2_SIZE,
466 .vbase = IXP435_USB2_VBASE },
467 { .hwbase = CAMBRIA_GPS_HWBASE,
468 .size = CAMBRIA_GPS_SIZE,
469 .vbase = CAMBRIA_GPS_VBASE,
471 { .hwbase = CAMBRIA_RS485_HWBASE,
472 .size = CAMBRIA_RS485_SIZE,
473 .vbase = CAMBRIA_RS485_VBASE,
478 for (i = 0; i < nitems(hwvtrans); i++) {
479 if (hwbase >= hwvtrans[i].hwbase &&
480 hwbase + size <= hwvtrans[i].hwbase + hwvtrans[i].size)
486 /* XXX for uart_cpu_getdev */
488 getvbase(uint32_t hwbase, uint32_t size, uint32_t *vbase)
490 const struct hwvtrans *hw;
492 hw = gethwvtrans(hwbase, size);
495 *vbase = hwbase - hw->hwbase + hw->vbase;
499 static struct resource *
500 ixp425_alloc_resource(device_t dev, device_t child, int type, int *rid,
501 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
503 struct ixp425_softc *sc = device_get_softc(dev);
504 const struct hwvtrans *vtrans;
507 int needactivate = flags & RF_ACTIVE;
513 /* override per hints */
514 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_IRQ, &irq) == 0)
516 rv = rman_reserve_resource(&sc->sc_irq_rman, start, end, count,
519 rman_set_rid(rv, *rid);
523 /* override per hints */
524 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_ADDR, &addr) == 0) {
526 /* XXX use nominal window to check for mapping */
527 vtrans = gethwvtrans(start, 0x1000);
528 if (vtrans != NULL) {
530 * Assign the entire mapped region; this may
531 * not be correct but without more info from
532 * the caller we cannot tell.
534 end = start + vtrans->size -
535 (start - vtrans->hwbase);
538 "%s: assign 0x%jx:0x%jx%s\n",
539 __func__, start, end - start,
540 vtrans->isa4x ? " A4X" :
541 vtrans->isslow ? " SLOW" : "");
544 vtrans = gethwvtrans(start, end - start);
545 if (vtrans == NULL) {
546 /* likely means above table needs to be updated */
547 device_printf(child, "%s: no mapping for 0x%jx:0x%jx\n",
548 __func__, start, end - start);
551 rv = rman_reserve_resource(&sc->sc_mem_rman, start, end,
552 end - start, flags, child);
554 device_printf(child, "%s: cannot reserve 0x%jx:0x%jx\n",
555 __func__, start, end - start);
558 rman_set_rid(rv, *rid);
564 if (rv != NULL && needactivate) {
565 if (bus_activate_resource(child, type, *rid, rv)) {
566 rman_release_resource(rv);
574 ixp425_release_resource(device_t bus, device_t child, int type, int rid,
577 /* NB: no private resources, just release */
578 return rman_release_resource(r);
582 ixp425_activate_resource(device_t dev, device_t child, int type, int rid,
585 struct ixp425_softc *sc = device_get_softc(dev);
586 const struct hwvtrans *vtrans;
588 if (type == SYS_RES_MEMORY) {
589 vtrans = gethwvtrans(rman_get_start(r), rman_get_size(r));
590 if (vtrans == NULL) { /* NB: should not happen */
591 device_printf(child, "%s: no mapping for 0x%jx:0x%jx\n",
592 __func__, rman_get_start(r), rman_get_size(r));
596 rman_set_bustag(r, &ixp425_a4x_bs_tag);
597 else if (vtrans->isslow)
598 rman_set_bustag(r, &cambria_exp_bs_tag);
600 rman_set_bustag(r, sc->sc_iot);
601 rman_set_bushandle(r, vtrans->vbase);
603 return (rman_activate_resource(r));
607 ixp425_deactivate_resource(device_t bus, device_t child, int type, int rid,
610 /* NB: no private resources, just deactive */
611 return (rman_deactivate_resource(r));
615 get_masks(struct resource *res, uint32_t *mask, uint32_t *mask2)
620 for (i = rman_get_start(res); i < 32 && i <= rman_get_end(res); i++)
623 for (; i <= rman_get_end(res); i++)
624 *mask2 |= 1 << (i - 32);
628 update_masks(uint32_t mask, uint32_t mask2)
632 ixp425_set_intrmask();
633 if (cpu_is_ixp43x()) {
634 intr_enabled2 = mask2;
635 ixp435_set_intrmask();
640 ixp425_setup_intr(device_t dev, device_t child,
641 struct resource *res, int flags, driver_filter_t *filt,
642 driver_intr_t *intr, void *arg, void **cookiep)
644 uint32_t mask, mask2;
647 error = BUS_SETUP_INTR(device_get_parent(dev), child, res, flags,
648 filt, intr, arg, cookiep);
652 get_masks(res, &mask, &mask2);
653 update_masks(intr_enabled | mask, intr_enabled2 | mask2);
659 ixp425_teardown_intr(device_t dev, device_t child, struct resource *res,
662 uint32_t mask, mask2;
664 get_masks(res, &mask, &mask2);
665 update_masks(intr_enabled &~ mask, intr_enabled2 &~ mask2);
667 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
670 static device_method_t ixp425_methods[] = {
671 /* Device interface */
672 DEVMETHOD(device_probe, ixp425_probe),
673 DEVMETHOD(device_attach, ixp425_attach),
674 DEVMETHOD(device_identify, ixp425_identify),
677 DEVMETHOD(bus_add_child, ixp425_add_child),
678 DEVMETHOD(bus_hinted_child, ixp425_hinted_child),
679 DEVMETHOD(bus_read_ivar, ixp425_read_ivar),
681 DEVMETHOD(bus_alloc_resource, ixp425_alloc_resource),
682 DEVMETHOD(bus_release_resource, ixp425_release_resource),
683 DEVMETHOD(bus_activate_resource, ixp425_activate_resource),
684 DEVMETHOD(bus_deactivate_resource, ixp425_deactivate_resource),
685 DEVMETHOD(bus_setup_intr, ixp425_setup_intr),
686 DEVMETHOD(bus_teardown_intr, ixp425_teardown_intr),
691 static driver_t ixp425_driver = {
694 sizeof(struct ixp425_softc),
696 static devclass_t ixp425_devclass;
698 DRIVER_MODULE(ixp, nexus, ixp425_driver, ixp425_devclass, 0, 0);