]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/malta/gt_pci.c
MFV r368464:
[FreeBSD/FreeBSD.git] / sys / mips / malta / gt_pci.c
1 /*      $NetBSD: gt_pci.c,v 1.4 2003/07/15 00:24:54 lukem Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
7  * All rights reserved.
8  *
9  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed for the NetBSD Project by
22  *      Wasabi Systems, Inc.
23  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24  *    or promote products derived from this software without specific prior
25  *    written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 /*
41  * PCI configuration support for gt I/O Processor chip.
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49
50 #include <sys/bus.h>
51 #include <sys/endian.h>
52 #include <sys/interrupt.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/module.h>
56 #include <sys/rman.h>
57
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_extern.h>
61
62 #include <machine/bus.h>
63 #include <machine/cpu.h>
64
65 #include <mips/malta/maltareg.h>
66
67 #include <mips/malta/gtreg.h>
68 #include <mips/malta/gtvar.h>
69
70 #include <isa/isareg.h>
71 #include <dev/ic/i8259.h>
72
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcivar.h>
75
76 #include <dev/pci/pcib_private.h>
77 #include "pcib_if.h"
78
79 #include <mips/malta/gt_pci_bus_space.h>
80
81 #define ICU_LEN         16      /* number of ISA IRQs */
82
83 /*
84  * XXX: These defines are from NetBSD's <dev/ic/i8259reg.h>. Respective file
85  * from FreeBSD src tree <dev/ic/i8259.h> lacks some definitions.
86  */
87 #define PIC_OCW1        1
88 #define PIC_OCW2        0
89 #define PIC_OCW3        0
90
91 #define OCW2_SELECT     0
92 #define OCW2_ILS(x)     ((x) << 0)      /* interrupt level select */
93
94 #define OCW3_POLL_IRQ(x) ((x) & 0x7f)
95 #define OCW3_POLL_PENDING (1U << 7)
96
97 /*
98  * Galileo controller's registers are LE so convert to then
99  * to/from native byte order. We rely on boot loader or emulator
100  * to set "swap bytes" configuration correctly for us
101  */
102 #define GT_PCI_DATA(v)  htole32((v))
103 #define GT_HOST_DATA(v) le32toh((v))
104
105 struct gt_pci_softc;
106
107 struct gt_pci_intr_cookie {
108         int irq;
109         struct gt_pci_softc *sc;
110 };
111
112 struct gt_pci_softc {
113         device_t                sc_dev;
114         bus_space_tag_t         sc_st;
115         bus_space_handle_t      sc_ioh_icu1;
116         bus_space_handle_t      sc_ioh_icu2;
117         bus_space_handle_t      sc_ioh_elcr;
118
119         int                     sc_busno;
120         struct rman             sc_mem_rman;
121         struct rman             sc_io_rman;
122         struct rman             sc_irq_rman;
123         unsigned long           sc_mem;
124         bus_space_handle_t      sc_io;
125
126         struct resource         *sc_irq;
127         struct intr_event       *sc_eventstab[ICU_LEN];
128         struct gt_pci_intr_cookie       sc_intr_cookies[ICU_LEN];
129         uint16_t                sc_imask;
130         uint16_t                sc_elcr;
131
132         uint16_t                sc_reserved;
133
134         void                    *sc_ih;
135 };
136
137 static void gt_pci_set_icus(struct gt_pci_softc *);
138 static int gt_pci_intr(void *v);
139 static int gt_pci_probe(device_t);
140 static int gt_pci_attach(device_t);
141 static int gt_pci_activate_resource(device_t, device_t, int, int, 
142     struct resource *);
143 static int gt_pci_setup_intr(device_t, device_t, struct resource *, 
144     int, driver_filter_t *, driver_intr_t *, void *, void **);
145 static int gt_pci_teardown_intr(device_t, device_t, struct resource *, void*);
146 static int gt_pci_maxslots(device_t );
147 static int gt_pci_conf_setup(struct gt_pci_softc *, int, int, int, int, 
148     uint32_t *);
149 static uint32_t gt_pci_read_config(device_t, u_int, u_int, u_int, u_int, int);
150 static void gt_pci_write_config(device_t, u_int, u_int, u_int, u_int, 
151     uint32_t, int);
152 static int gt_pci_route_interrupt(device_t pcib, device_t dev, int pin);
153 static struct resource * gt_pci_alloc_resource(device_t, device_t, int, 
154     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
155
156 static void
157 gt_pci_mask_irq(void *source)
158 {
159         struct gt_pci_intr_cookie *cookie = source;
160         struct gt_pci_softc *sc = cookie->sc;
161         int irq = cookie->irq;
162
163         sc->sc_imask |= (1 << irq);
164         sc->sc_elcr |= (1 << irq);
165
166         gt_pci_set_icus(sc);
167 }
168
169 static void
170 gt_pci_unmask_irq(void *source)
171 {
172         struct gt_pci_intr_cookie *cookie = source;
173         struct gt_pci_softc *sc = cookie->sc;
174         int irq = cookie->irq;
175
176         /* Enable it, set trigger mode. */
177         sc->sc_imask &= ~(1 << irq);
178         sc->sc_elcr &= ~(1 << irq);
179
180         gt_pci_set_icus(sc);
181 }
182
183 static void
184 gt_pci_set_icus(struct gt_pci_softc *sc)
185 {
186         /* Enable the cascade IRQ (2) if 8-15 is enabled. */
187         if ((sc->sc_imask & 0xff00) != 0xff00)
188                 sc->sc_imask &= ~(1U << 2);
189         else
190                 sc->sc_imask |= (1U << 2);
191
192         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW1,
193             sc->sc_imask & 0xff);
194         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, PIC_OCW1,
195             (sc->sc_imask >> 8) & 0xff);
196
197         bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0,
198             sc->sc_elcr & 0xff);
199         bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1,
200             (sc->sc_elcr >> 8) & 0xff);
201 }
202
203 static int
204 gt_pci_intr(void *v)
205 {
206         struct gt_pci_softc *sc = v;
207         struct intr_event *event;
208         int irq;
209
210         for (;;) {
211                 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3,
212                     OCW3_SEL | OCW3_P);
213                 irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3);
214                 if ((irq & OCW3_POLL_PENDING) == 0)
215                 {
216                         return FILTER_HANDLED;
217                 }
218
219                 irq = OCW3_POLL_IRQ(irq);
220
221                 if (irq == 2) {
222                         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2,
223                             PIC_OCW3, OCW3_SEL | OCW3_P);
224                         irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu2,
225                             PIC_OCW3);
226                         if (irq & OCW3_POLL_PENDING)
227                                 irq = OCW3_POLL_IRQ(irq) + 8;
228                         else
229                                 irq = 2;
230                 }
231
232                 event = sc->sc_eventstab[irq];
233
234                 if (!event || CK_SLIST_EMPTY(&event->ie_handlers))
235                         continue;
236
237                 /* TODO: frame instead of NULL? */
238                 intr_event_handle(event, NULL);
239                 /* XXX: Log stray IRQs */
240
241                 /* Send a specific EOI to the 8259. */
242                 if (irq > 7) {
243                         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2,
244                             PIC_OCW2, OCW2_SELECT | OCW2_EOI | OCW2_SL |
245                             OCW2_ILS(irq & 7));
246                         irq = 2;
247                 }
248
249                 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW2,
250                     OCW2_SELECT | OCW2_EOI | OCW2_SL | OCW2_ILS(irq));
251         }
252
253         return FILTER_HANDLED;
254 }
255
256 static int
257 gt_pci_probe(device_t dev)
258 {
259         device_set_desc(dev, "GT64120 PCI bridge");
260         return (0);
261 }
262
263 static int
264 gt_pci_attach(device_t dev)
265 {
266
267         uint32_t busno;                                
268         struct gt_pci_softc *sc = device_get_softc(dev);
269         int rid;
270
271         busno = 0;
272         sc->sc_dev = dev;
273         sc->sc_busno = busno;
274         sc->sc_st = mips_bus_space_generic;
275
276         /* Use KSEG1 to access IO ports for it is uncached */
277         sc->sc_io = MALTA_PCI0_IO_BASE;
278         sc->sc_io_rman.rm_type = RMAN_ARRAY;
279         sc->sc_io_rman.rm_descr = "GT64120 PCI I/O Ports";
280         /* 
281          * First 256 bytes are ISA's registers: e.g. i8259's
282          * So do not use them for general purpose PCI I/O window
283          */
284         if (rman_init(&sc->sc_io_rman) != 0 ||
285             rman_manage_region(&sc->sc_io_rman, 0x100, 0xffff) != 0) {
286                 panic("gt_pci_attach: failed to set up I/O rman");
287         }
288
289         /* Use KSEG1 to access PCI memory for it is uncached */
290         sc->sc_mem = MALTA_PCIMEM1_BASE;
291         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
292         sc->sc_mem_rman.rm_descr = "GT64120 PCI Memory";
293         if (rman_init(&sc->sc_mem_rman) != 0 ||
294             rman_manage_region(&sc->sc_mem_rman, 
295             sc->sc_mem, sc->sc_mem + MALTA_PCIMEM1_SIZE) != 0) {
296                 panic("gt_pci_attach: failed to set up memory rman");
297         }
298         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
299         sc->sc_irq_rman.rm_descr = "GT64120 PCI IRQs";
300         if (rman_init(&sc->sc_irq_rman) != 0 ||
301             rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
302                 panic("gt_pci_attach: failed to set up IRQ rman");
303
304         /*
305          * Map the PIC/ELCR registers.
306          */
307 #if 0
308         if (bus_space_map(sc->sc_st, 0x4d0, 2, 0, &sc->sc_ioh_elcr) != 0)
309                 device_printf(dev, "unable to map ELCR registers\n");
310         if (bus_space_map(sc->sc_st, IO_ICU1, 2, 0, &sc->sc_ioh_icu1) != 0)
311                 device_printf(dev, "unable to map ICU1 registers\n");
312         if (bus_space_map(sc->sc_st, IO_ICU2, 2, 0, &sc->sc_ioh_icu2) != 0)
313                 device_printf(dev, "unable to map ICU2 registers\n");
314 #else
315         sc->sc_ioh_elcr = MIPS_PHYS_TO_KSEG1(sc->sc_io + 0x4d0);
316         sc->sc_ioh_icu1 = MIPS_PHYS_TO_KSEG1(sc->sc_io + IO_ICU1);
317         sc->sc_ioh_icu2 = MIPS_PHYS_TO_KSEG1(sc->sc_io + IO_ICU2);
318 #endif  
319
320         /* All interrupts default to "masked off". */
321         sc->sc_imask = 0xffff;
322
323         /* All interrupts default to edge-triggered. */
324         sc->sc_elcr = 0;
325
326         /*
327          * Initialize the 8259s.
328          */
329         /* reset, program device, 4 bytes */
330         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
331             ICW1_RESET | ICW1_IC4);
332         /*
333          * XXX: values from NetBSD's <dev/ic/i8259reg.h>
334          */      
335         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
336             0/*XXX*/);
337         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
338             1 << 2);
339         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
340             ICW4_8086);
341
342         /* mask all interrupts */
343         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
344             sc->sc_imask & 0xff);
345
346         /* enable special mask mode */
347         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
348             OCW3_SEL | OCW3_ESMM | OCW3_SMM);
349
350         /* read IRR by default */
351         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
352             OCW3_SEL | OCW3_RR);
353
354         /* reset, program device, 4 bytes */
355         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
356             ICW1_RESET | ICW1_IC4);
357         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
358             0/*XXX*/);
359         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
360             1 << 2);
361         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
362             ICW4_8086);
363
364         /* mask all interrupts */
365         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
366             sc->sc_imask & 0xff);
367
368         /* enable special mask mode */
369         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
370             OCW3_SEL | OCW3_ESMM | OCW3_SMM);
371
372         /* read IRR by default */
373         bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
374             OCW3_SEL | OCW3_RR);
375
376         /*
377          * Default all interrupts to edge-triggered.
378          */
379         bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0,
380             sc->sc_elcr & 0xff);
381         bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1,
382             (sc->sc_elcr >> 8) & 0xff);
383
384         /*
385          * Some ISA interrupts are reserved for devices that
386          * we know are hard-wired to certain IRQs.
387          */
388         sc->sc_reserved =
389                 (1U << 0) |     /* timer */
390                 (1U << 1) |     /* keyboard controller (keyboard) */
391                 (1U << 2) |     /* PIC cascade */
392                 (1U << 3) |     /* COM 2 */
393                 (1U << 4) |     /* COM 1 */
394                 (1U << 6) |     /* floppy */
395                 (1U << 7) |     /* centronics */
396                 (1U << 8) |     /* RTC */
397                 (1U << 9) |     /* I2C */
398                 (1U << 12) |    /* keyboard controller (mouse) */
399                 (1U << 14) |    /* IDE primary */
400                 (1U << 15);     /* IDE secondary */
401
402         /* Hook up our interrupt handler. */
403         if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 
404             MALTA_SOUTHBRIDGE_INTR, MALTA_SOUTHBRIDGE_INTR, 1, 
405             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
406                 device_printf(dev, "unable to allocate IRQ resource\n");
407                 return ENXIO;
408         }
409
410         if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
411                             gt_pci_intr, NULL, sc, &sc->sc_ih))) {
412                 device_printf(dev, 
413                     "WARNING: unable to register interrupt handler\n");
414                 return ENXIO;
415         }
416
417         /* Initialize memory and i/o rmans. */
418         device_add_child(dev, "pci", -1);
419         return (bus_generic_attach(dev));
420 }
421
422 static int
423 gt_pci_maxslots(device_t dev)
424 {
425         return (PCI_SLOTMAX);
426 }
427
428 static int
429 gt_pci_conf_setup(struct gt_pci_softc *sc, int bus, int slot, int func,
430     int reg, uint32_t *addr)
431 {
432         *addr = (bus << 16) | (slot << 11) | (func << 8) | reg;
433
434         return (0);
435 }
436
437 static uint32_t
438 gt_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
439     int bytes)
440 {
441         struct gt_pci_softc *sc = device_get_softc(dev);
442         uint32_t data;
443         uint32_t addr;
444         uint32_t shift, mask;
445
446         if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
447                 return (uint32_t)(-1);
448
449         /* Clear cause register bits. */
450         GT_REGVAL(GT_INTR_CAUSE) = GT_PCI_DATA(0);
451         GT_REGVAL(GT_PCI0_CFG_ADDR) = GT_PCI_DATA((1U << 31) | addr);
452         /* 
453          * Galileo system controller is special
454          */
455         if ((bus == 0) && (slot == 0))
456                 data = GT_PCI_DATA(GT_REGVAL(GT_PCI0_CFG_DATA));
457         else
458                 data = GT_REGVAL(GT_PCI0_CFG_DATA);
459
460         /* Check for master abort. */
461         if (GT_HOST_DATA(GT_REGVAL(GT_INTR_CAUSE)) & (GTIC_MASABORT0 | GTIC_TARABORT0))
462                 data = (uint32_t) -1;
463
464         switch(reg % 4)
465         {
466         case 3:
467                 shift = 24;
468                 break;
469         case 2:
470                 shift = 16;
471                 break;
472         case 1:
473                 shift = 8;
474                 break;
475         default:
476                 shift = 0;
477                 break;
478         }       
479
480         switch(bytes)
481         {
482         case 1:
483                 mask = 0xff;
484                 data = (data >> shift) & mask;
485                 break;
486         case 2:
487                 mask = 0xffff;
488                 if(reg % 4 == 0)
489                         data = data & mask;
490                 else
491                         data = (data >> 16) & mask;
492                 break;
493         case 4:
494                 break;
495         default:
496                 panic("gt_pci_readconfig: wrong bytes count");
497                 break;
498         }
499 #if 0
500         printf("PCICONF_READ(%02x:%02x.%02x[%04x] -> %02x(%d)\n", 
501           bus, slot, func, reg, data, bytes);
502 #endif
503
504         return (data);
505 }
506
507 static void
508 gt_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
509     uint32_t data, int bytes)
510 {
511         struct gt_pci_softc *sc = device_get_softc(dev);
512         uint32_t addr;
513         uint32_t reg_data;
514         uint32_t shift, mask;
515
516         if(bytes != 4)
517         {
518                 reg_data = gt_pci_read_config(dev, bus, slot, func, reg, 4);
519
520                 shift = 8 * (reg & 3);
521
522                 switch(bytes)
523                 {
524                 case 1:
525                         mask = 0xff;
526                         data = (reg_data & ~ (mask << shift)) | (data << shift);
527                         break;
528                 case 2:
529                         mask = 0xffff;
530                         if(reg % 4 == 0)
531                                 data = (reg_data & ~mask) | data;
532                         else
533                                 data = (reg_data & ~ (mask << shift)) | 
534                                     (data << shift);
535                         break;
536                 case 4:
537                         break;
538                 default:
539                         panic("gt_pci_readconfig: wrong bytes count");
540                         break;
541                 }
542         }
543
544         if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
545                 return;
546
547         /* The galileo has problems accessing device 31. */
548         if (bus == 0 && slot == 31)
549                 return;
550
551         /* XXX: no support for bus > 0 yet */
552         if (bus > 0)
553                 return;
554
555         /* Clear cause register bits. */
556         GT_REGVAL(GT_INTR_CAUSE) = GT_PCI_DATA(0);
557
558         GT_REGVAL(GT_PCI0_CFG_ADDR) = GT_PCI_DATA((1U << 31) | addr);
559
560         /* 
561          * Galileo system controller is special
562          */
563         if ((bus == 0) && (slot == 0))
564                 GT_REGVAL(GT_PCI0_CFG_DATA) = GT_PCI_DATA(data);
565         else
566                 GT_REGVAL(GT_PCI0_CFG_DATA) = data;
567
568 #if 0
569         printf("PCICONF_WRITE(%02x:%02x.%02x[%04x] -> %02x(%d)\n", 
570           bus, slot, func, reg, data, bytes);
571 #endif
572
573 }
574
575 static int
576 gt_pci_route_interrupt(device_t pcib, device_t dev, int pin)
577 {
578         int bus;
579         int device;
580         int func;
581         /* struct gt_pci_softc *sc = device_get_softc(pcib); */
582         bus = pci_get_bus(dev);
583         device = pci_get_slot(dev);
584         func = pci_get_function(dev);
585         /* 
586          * XXXMIPS: We need routing logic. This is just a stub .
587          */
588         switch (device) {
589         case 9: /*
590                  * PIIX4 IDE adapter. HW IRQ0
591                  */
592                 return 0;
593         case 11: /* Ethernet */
594                 return 10;
595         default:
596                 device_printf(pcib, "no IRQ mapping for %d/%d/%d/%d\n", bus, device, func, pin);
597                 
598         }
599         return (0);
600
601 }
602
603 static int
604 gt_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
605 {
606         struct gt_pci_softc *sc = device_get_softc(dev);
607         switch (which) {
608         case PCIB_IVAR_DOMAIN:
609                 *result = 0;
610                 return (0);
611         case PCIB_IVAR_BUS:
612                 *result = sc->sc_busno;
613                 return (0);
614                 
615         }
616         return (ENOENT);
617 }
618
619 static int
620 gt_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
621 {
622         struct gt_pci_softc * sc = device_get_softc(dev);
623
624         switch (which) {
625         case PCIB_IVAR_BUS:
626                 sc->sc_busno = result;
627                 return (0);
628         }
629         return (ENOENT);
630 }
631
632 static struct resource *
633 gt_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
634     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
635 {
636         struct gt_pci_softc *sc = device_get_softc(bus);        
637         struct resource *rv = NULL;
638         struct rman *rm;
639         bus_space_handle_t bh = 0;
640
641         switch (type) {
642         case SYS_RES_IRQ:
643                 rm = &sc->sc_irq_rman;
644                 break;
645         case SYS_RES_MEMORY:
646                 rm = &sc->sc_mem_rman;
647                 bh = sc->sc_mem;
648                 break;
649         case SYS_RES_IOPORT:
650                 rm = &sc->sc_io_rman;
651                 bh = sc->sc_io;
652                 break;
653         default:
654                 return (NULL);
655         }
656
657         rv = rman_reserve_resource(rm, start, end, count, flags, child);
658         if (rv == NULL)
659                 return (NULL);
660         rman_set_rid(rv, *rid);
661         if (type != SYS_RES_IRQ) {
662                 bh += (rman_get_start(rv));
663
664                 rman_set_bustag(rv, gt_pci_bus_space);
665                 rman_set_bushandle(rv, bh);
666                 if (flags & RF_ACTIVE) {
667                         if (bus_activate_resource(child, type, *rid, rv)) {
668                                 rman_release_resource(rv);
669                                 return (NULL);
670                         }
671                 } 
672         }
673         return (rv);
674 }
675
676 static int
677 gt_pci_activate_resource(device_t bus, device_t child, int type, int rid,
678     struct resource *r)
679 {
680         bus_space_handle_t p;
681         int error;
682
683         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
684                 error = bus_space_map(rman_get_bustag(r),
685                     rman_get_bushandle(r), rman_get_size(r), 0, &p);
686                 if (error) 
687                         return (error);
688                 rman_set_bushandle(r, p);
689         }
690         return (rman_activate_resource(r));
691 }
692
693 static int
694 gt_pci_setup_intr(device_t dev, device_t child, struct resource *ires, 
695                 int flags, driver_filter_t *filt, driver_intr_t *handler, 
696                 void *arg, void **cookiep)
697 {
698         struct gt_pci_softc *sc = device_get_softc(dev);
699         struct intr_event *event;
700         int irq, error;
701
702         irq = rman_get_start(ires);
703         if (irq >= ICU_LEN || irq == 2)
704                 panic("%s: bad irq or type", __func__);
705
706         event = sc->sc_eventstab[irq];
707         sc->sc_intr_cookies[irq].irq = irq;
708         sc->sc_intr_cookies[irq].sc = sc;
709         if (event == NULL) {
710                 error = intr_event_create(&event, 
711                     (void *)&sc->sc_intr_cookies[irq], 0, irq,
712                     gt_pci_mask_irq, gt_pci_unmask_irq,
713                     NULL, NULL, "gt_pci intr%d:", irq);
714                 if (error)
715                         return 0;
716                 sc->sc_eventstab[irq] = event;
717         }
718
719         intr_event_add_handler(event, device_get_nameunit(child), filt, 
720             handler, arg, intr_priority(flags), flags, cookiep);
721
722         gt_pci_unmask_irq((void *)&sc->sc_intr_cookies[irq]);
723         return 0;
724 }
725
726 static int
727 gt_pci_teardown_intr(device_t dev, device_t child, struct resource *res,
728     void *cookie)
729 {
730         struct gt_pci_softc *sc = device_get_softc(dev);
731         int irq;
732
733         irq = rman_get_start(res);
734         gt_pci_mask_irq((void *)&sc->sc_intr_cookies[irq]);
735
736         return (intr_event_remove_handler(cookie));
737 }
738
739 static device_method_t gt_pci_methods[] = {
740         /* Device interface */
741         DEVMETHOD(device_probe,         gt_pci_probe),
742         DEVMETHOD(device_attach,        gt_pci_attach),
743         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
744         DEVMETHOD(device_suspend,       bus_generic_suspend),
745         DEVMETHOD(device_resume,        bus_generic_resume),
746
747         /* Bus interface */
748         DEVMETHOD(bus_read_ivar,        gt_read_ivar),
749         DEVMETHOD(bus_write_ivar,       gt_write_ivar),
750         DEVMETHOD(bus_alloc_resource,   gt_pci_alloc_resource),
751         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
752         DEVMETHOD(bus_activate_resource, gt_pci_activate_resource),
753         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
754         DEVMETHOD(bus_setup_intr,       gt_pci_setup_intr),
755         DEVMETHOD(bus_teardown_intr,    gt_pci_teardown_intr),
756
757         /* pcib interface */
758         DEVMETHOD(pcib_maxslots,        gt_pci_maxslots),
759         DEVMETHOD(pcib_read_config,     gt_pci_read_config),
760         DEVMETHOD(pcib_write_config,    gt_pci_write_config),
761         DEVMETHOD(pcib_route_interrupt, gt_pci_route_interrupt),
762         DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
763
764         DEVMETHOD_END
765 };
766
767 static driver_t gt_pci_driver = {
768         "pcib",
769         gt_pci_methods,
770         sizeof(struct gt_pci_softc),
771 };
772
773 static devclass_t gt_pci_devclass;
774
775 DRIVER_MODULE(gt_pci, gt, gt_pci_driver, gt_pci_devclass, 0, 0);