]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/xscale/ixp425/ixp425.c
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / sys / arm / xscale / ixp425 / ixp425.c
1 /*      $NetBSD: ixp425.c,v 1.10 2005/12/11 12:16:51 christos Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2003
7  *      Ichiro FUKUHARA <ichiro@ichiro.org>.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_ddb.h"
42
43 #define _ARM32_BUS_DMA_PRIVATE
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
50 #include <sys/rman.h>
51 #include <machine/armreg.h>
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <arm/xscale/ixp425/ixp425reg.h>
58 #include <arm/xscale/ixp425/ixp425var.h>
59 #include <arm/xscale/ixp425/ixp425_intr.h>
60
61 #include <dev/pci/pcireg.h>
62
63 volatile uint32_t intr_enabled;
64 uint32_t intr_steer = 0;
65
66 /* ixp43x et. al have +32 IRQ's */
67 volatile uint32_t intr_enabled2;
68 uint32_t intr_steer2 = 0;
69
70 struct  ixp425_softc *ixp425_softc = NULL;
71
72 struct mtx ixp425_gpio_mtx;
73
74 static int      ixp425_probe(device_t);
75 static void     ixp425_identify(driver_t *, device_t);
76 static int      ixp425_attach(device_t);
77
78 /*
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.
82  */
83 uint32_t
84 ixp4xx_read_feature_bits(void)
85 {
86         uint32_t bits = ~IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET);
87         bits &= ~EXP_FCTRL_RESVD;
88         if (!cpu_is_ixp46x())
89                 bits &= ~EXP_FCTRL_IXP46X_ONLY;
90         return bits;
91 }
92
93 void
94 ixp4xx_write_feature_bits(uint32_t v)
95 {
96         IXPREG(IXP425_EXP_VBASE + EXP_FCTRL_OFFSET) = ~v;
97 }
98
99 struct arm32_dma_range *
100 bus_dma_get_range(void)
101 {
102         return (NULL);
103 }
104
105 int
106 bus_dma_get_range_nb(void)
107 {
108         return (0);
109 }
110
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 */
119 };
120
121 static __inline uint32_t
122 ixp425_irq2gpio_bit(int irq)
123 {
124         return (1U << int2gpio[irq]);
125 }
126
127 #ifdef DDB
128 #include <ddb/ddb.h>
129
130 DB_SHOW_COMMAND(gpio, db_show_gpio)
131 {
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"
139         };
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);
145         int i, j;
146
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)))
159                                         & 7]);
160                                 break;
161                         }
162                 db_printf("\n");
163         }
164 }
165 #endif
166
167 void
168 ixp425_set_gpio(struct ixp425_softc *sc, int pin, int type)
169 {
170         uint32_t gpiotr = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(pin));
171
172         IXP4XX_GPIO_LOCK();
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));
181
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();
186 }
187
188 static __inline void
189 ixp425_gpio_ack(int irq)
190 {
191         if (irq < 32 && ((1 << irq) & IXP425_INT_GPIOMASK))
192                 IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
193                     ixp425_irq2gpio_bit(irq);
194 }
195
196 static void
197 ixp425_post_filter(void *arg)
198 {
199         uintptr_t irq = (uintptr_t) arg;
200         ixp425_gpio_ack(irq);
201 }
202
203 void
204 arm_mask_irq(uintptr_t nb)
205 {
206         int i;
207
208         i = disable_interrupts(PSR_I);
209         if (nb < 32) {
210                 intr_enabled &= ~(1 << nb);
211                 ixp425_set_intrmask();
212         } else {
213                 intr_enabled2 &= ~(1 << (nb - 32));
214                 ixp435_set_intrmask();
215         }
216         restore_interrupts(i);
217         /*XXX; If it's a GPIO interrupt, ACK it know. Can it be a problem ?*/
218         ixp425_gpio_ack(nb);
219 }
220
221 void
222 arm_unmask_irq(uintptr_t nb)
223 {
224         int i;
225
226         i = disable_interrupts(PSR_I);
227         if (nb < 32) {
228                 intr_enabled |= (1 << nb);
229                 ixp425_set_intrmask();
230         } else {
231                 intr_enabled2 |= (1 << (nb - 32));
232                 ixp435_set_intrmask();
233         }
234         restore_interrupts(i);
235 }
236
237 static __inline uint32_t
238 ixp425_irq_read(void)
239 {
240         return IXPREG(IXP425_INT_STATUS) & intr_enabled;
241 }
242
243 static __inline uint32_t
244 ixp435_irq_read(void)
245 {
246         return IXPREG(IXP435_INT_STATUS2) & intr_enabled2;
247 }
248
249 int
250 arm_get_next_irq(int last)
251 {
252         uint32_t mask;
253
254         last += 1;              /* always advance fwd, NB: handles -1 */
255         if (last < 32) {
256                 mask = ixp425_irq_read() >> last;
257                 for (; mask != 0; mask >>= 1, last++) {
258                         if (mask & 1)
259                                 return last;
260                 }
261                 last = 32;
262         }
263         if (cpu_is_ixp43x()) {
264                 mask = ixp435_irq_read() >> (32-last);
265                 for (; mask != 0; mask >>= 1, last++) {
266                         if (mask & 1)
267                                 return last;
268                 }
269         }
270         return -1;
271 }
272
273 void
274 cpu_reset(void)
275 {
276
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,
280             IXP425_OST_WDOG, 0);
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");
285         for(;;);
286 }
287
288 static void
289 ixp425_identify(driver_t *driver, device_t parent)
290 {
291         BUS_ADD_CHILD(parent, 0, "ixp", 0);
292 }
293
294 static int
295 ixp425_probe(device_t dev)
296 {
297         device_set_desc(dev, "Intel IXP4XX");
298         return (0);
299 }
300
301 static int
302 ixp425_attach(device_t dev)
303 {
304         struct ixp425_softc *sc;
305
306         device_printf(dev, "%b\n", ixp4xx_read_feature_bits(), EXP_FCTRL_BITS);
307
308         sc = device_get_softc(dev);
309         sc->sc_iot = &ixp425_bs_tag;
310         KASSERT(ixp425_softc == NULL, ("%s called twice?", __func__));
311         ixp425_softc = sc;
312
313         intr_enabled = 0;
314         ixp425_set_intrmask();
315         ixp425_set_intrsteer();
316         if (cpu_is_ixp43x()) {
317                 intr_enabled2 = 0;
318                 ixp435_set_intrmask();
319                 ixp435_set_intrsteer();
320         }
321         arm_post_filter = ixp425_post_filter;
322
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,
328             0, &sc->sc_exp_ioh))
329                 panic("%s: unable to map Expansion Bus registers", __func__);
330
331         /* XXX belongs in platform init */
332         if (cpu_is_ixp43x())
333                 cambria_exp_bus_init(sc);
334
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__);
339
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__);
345
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__);
351
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);
357
358         /* attach wired devices via hints */
359         bus_enumerate_hinted_children(dev);
360
361         bus_generic_probe(dev);
362         bus_generic_attach(dev);
363
364         return (0);
365 }
366
367 static void
368 ixp425_hinted_child(device_t bus, const char *dname, int dunit)
369 {
370         device_t child;
371         struct ixp425_ivar *ivar;
372
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);
377 }
378
379 static device_t
380 ixp425_add_child(device_t dev, u_int order, const char *name, int unit)
381 {
382         device_t child;
383         struct ixp425_ivar *ivar;
384
385         child = device_add_child_ordered(dev, order, name, unit);
386         if (child == NULL)
387                 return NULL;
388         ivar = malloc(sizeof(struct ixp425_ivar), M_DEVBUF, M_NOWAIT);
389         if (ivar == NULL) {
390                 device_delete_child(dev, child);
391                 return NULL;
392         }
393         ivar->addr = 0;
394         ivar->irq = -1;
395         device_set_ivars(child, ivar);
396         return child;
397 }
398
399 static int
400 ixp425_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
401 {
402         struct ixp425_ivar *ivar = IXP425_IVAR(child);
403
404         switch (which) {
405         case IXP425_IVAR_ADDR:
406                 if (ivar->addr != 0) {
407                         *(uint32_t *)result = ivar->addr;
408                         return 0;
409                 }
410                 break;
411         case IXP425_IVAR_IRQ:
412                 if (ivar->irq != -1) {
413                         *(int *)result = ivar->irq;
414                         return 0;
415                 }
416                 break;
417         }
418         return EINVAL;
419 }
420
421 /*
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.
426  *
427  * XXX this table is also used by uart_cpu_getdev via getvbase
428  *    (hence the public api)
429  */
430 struct hwvtrans {
431         uint32_t        hwbase;
432         uint32_t        size;
433         uint32_t        vbase;
434         int             isa4x;  /* XXX needs special bus space tag */
435         int             isslow; /* XXX needs special bus space tag */
436 };
437
438 static const struct hwvtrans *
439 gethwvtrans(uint32_t hwbase, uint32_t size)
440 {
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,
446               .isa4x    = 1 },
447             { .hwbase   = IXP425_UART1_HWBASE,
448               .size     = IXP425_REG_SIZE,
449               .vbase    = IXP425_UART1_VBASE,
450               .isa4x    = 1 },
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,
470               .isslow   = 1 },
471             { .hwbase   = CAMBRIA_RS485_HWBASE,
472               .size     = CAMBRIA_RS485_SIZE,
473               .vbase    = CAMBRIA_RS485_VBASE,
474               .isslow   = 1 },
475         };
476         int i;
477
478         for (i = 0; i < nitems(hwvtrans); i++) {
479                 if (hwbase >= hwvtrans[i].hwbase &&
480                     hwbase + size <= hwvtrans[i].hwbase + hwvtrans[i].size)
481                         return &hwvtrans[i];
482         }
483         return NULL;
484 }
485
486 /* XXX for uart_cpu_getdev */
487 int
488 getvbase(uint32_t hwbase, uint32_t size, uint32_t *vbase)
489 {
490         const struct hwvtrans *hw;
491
492         hw = gethwvtrans(hwbase, size);
493         if (hw == NULL)
494                 return (ENOENT);
495         *vbase = hwbase - hw->hwbase + hw->vbase;
496         return (0);
497 }
498
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)
502 {
503         struct ixp425_softc *sc = device_get_softc(dev);
504         const struct hwvtrans *vtrans;
505         struct resource *rv;
506         uint32_t addr;
507         int needactivate = flags & RF_ACTIVE;
508         int irq;
509
510         flags &= ~RF_ACTIVE;
511         switch (type) {
512         case SYS_RES_IRQ:
513                 /* override per hints */
514                 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_IRQ, &irq) == 0)
515                         start = end = irq;
516                 rv = rman_reserve_resource(&sc->sc_irq_rman, start, end, count,
517                     flags, child);
518                 if (rv != NULL)
519                         rman_set_rid(rv, *rid);
520                 break;
521
522         case SYS_RES_MEMORY:
523                 /* override per hints */
524                 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_ADDR, &addr) == 0) {
525                         start = addr;
526                         /* XXX use nominal window to check for mapping */
527                         vtrans = gethwvtrans(start, 0x1000);
528                         if (vtrans != NULL) {
529                                 /*
530                                  * Assign the entire mapped region; this may
531                                  * not be correct but without more info from
532                                  * the caller we cannot tell.
533                                  */
534                                 end = start + vtrans->size -
535                                     (start - vtrans->hwbase);
536                                 if (bootverbose)
537                                         device_printf(child,
538                                             "%s: assign 0x%jx:0x%jx%s\n",
539                                             __func__, start, end - start,
540                                             vtrans->isa4x ? " A4X" :
541                                             vtrans->isslow ? " SLOW" : "");
542                         }
543                 } else
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);
549                         return NULL;
550                 }
551                 rv = rman_reserve_resource(&sc->sc_mem_rman, start, end,
552                     end - start, flags, child);
553                 if (rv == NULL) {
554                         device_printf(child, "%s: cannot reserve 0x%jx:0x%jx\n",
555                             __func__, start, end - start);
556                         return NULL;
557                 }
558                 rman_set_rid(rv, *rid);
559                 break;
560         default:
561                 rv = NULL;
562                 break;
563         }
564         if (rv != NULL && needactivate) {
565                 if (bus_activate_resource(child, type, *rid, rv)) {
566                         rman_release_resource(rv);
567                         return (NULL);
568                 }
569         }
570         return (rv);
571 }
572
573 static int
574 ixp425_release_resource(device_t bus, device_t child, int type, int rid,
575     struct resource *r)
576 {
577         /* NB: no private resources, just release */
578         return rman_release_resource(r);
579 }
580
581 static int
582 ixp425_activate_resource(device_t dev, device_t child, int type, int rid,
583     struct resource *r)
584 {
585         struct ixp425_softc *sc = device_get_softc(dev);
586         const struct hwvtrans *vtrans;
587
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));
593                         return (ENOENT);
594                 }
595                 if (vtrans->isa4x)
596                         rman_set_bustag(r, &ixp425_a4x_bs_tag);
597                 else if (vtrans->isslow)
598                         rman_set_bustag(r, &cambria_exp_bs_tag);
599                 else
600                         rman_set_bustag(r, sc->sc_iot);
601                 rman_set_bushandle(r, vtrans->vbase);
602         }
603         return (rman_activate_resource(r));
604 }
605
606 static int
607 ixp425_deactivate_resource(device_t bus, device_t child, int type, int rid,
608     struct resource *r)
609 {
610         /* NB: no private resources, just deactive */
611         return (rman_deactivate_resource(r));
612 }
613
614 static __inline void
615 get_masks(struct resource *res, uint32_t *mask, uint32_t *mask2)
616 {
617         int i;
618
619         *mask = 0;
620         for (i = rman_get_start(res); i < 32 && i <= rman_get_end(res); i++)
621                 *mask |= 1 << i;
622         *mask2 = 0;
623         for (; i <= rman_get_end(res); i++)
624                 *mask2 |= 1 << (i - 32);
625 }
626
627 static __inline void
628 update_masks(uint32_t mask, uint32_t mask2)
629 {
630
631         intr_enabled = mask;
632         ixp425_set_intrmask();
633         if (cpu_is_ixp43x()) {
634                 intr_enabled2 = mask2;
635                 ixp435_set_intrmask();
636         }
637 }
638
639 static int
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)
643 {
644         uint32_t mask, mask2;
645         int error;
646
647         error = BUS_SETUP_INTR(device_get_parent(dev), child, res, flags,
648             filt, intr, arg, cookiep);
649         if (error)
650                 return (error);
651
652         get_masks(res, &mask, &mask2);
653         update_masks(intr_enabled | mask, intr_enabled2 | mask2);
654
655         return (0);
656 }
657
658 static int
659 ixp425_teardown_intr(device_t dev, device_t child, struct resource *res,
660     void *cookie)
661 {
662         uint32_t mask, mask2;
663
664         get_masks(res, &mask, &mask2);
665         update_masks(intr_enabled &~ mask, intr_enabled2 &~ mask2);
666
667         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
668 }
669
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),
675
676         /* Bus interface */
677         DEVMETHOD(bus_add_child,                ixp425_add_child),
678         DEVMETHOD(bus_hinted_child,             ixp425_hinted_child),
679         DEVMETHOD(bus_read_ivar,                ixp425_read_ivar),
680
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),
687
688         {0, 0},
689 };
690
691 static driver_t ixp425_driver = {
692         "ixp",
693         ixp425_methods,
694         sizeof(struct ixp425_softc),
695 };
696 static devclass_t ixp425_devclass;
697
698 DRIVER_MODULE(ixp, nexus, ixp425_driver, ixp425_devclass, 0, 0);