]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar71xx_pci.c
MFV r294491: ntp 4.2.8p6.
[FreeBSD/FreeBSD.git] / sys / mips / atheros / ar71xx_pci.c
1 /*-
2  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_ar71xx.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35
36 #include <sys/bus.h>
37 #include <sys/interrupt.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_extern.h>
48
49 #include <machine/bus.h>
50 #include <machine/cpu.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/pmap.h>
53
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcireg.h>
56
57 #include <dev/pci/pcib_private.h>
58 #include "pcib_if.h"
59
60 #include <mips/atheros/ar71xxreg.h>
61 #include <mips/atheros/ar71xx_pci_bus_space.h>
62
63 #include <mips/atheros/ar71xx_cpudef.h>
64
65 #ifdef  AR71XX_ATH_EEPROM
66 #include <mips/atheros/ar71xx_fixup.h>
67 #endif  /* AR71XX_ATH_EEPROM */
68
69 #undef  AR71XX_PCI_DEBUG
70 #ifdef  AR71XX_PCI_DEBUG
71 #define dprintf printf
72 #else
73 #define dprintf(x, arg...)
74 #endif
75
76 struct mtx ar71xx_pci_mtx;
77 MTX_SYSINIT(ar71xx_pci_mtx, &ar71xx_pci_mtx, "ar71xx PCI space mutex",
78     MTX_SPIN);
79
80 struct ar71xx_pci_softc {
81         device_t                sc_dev;
82
83         int                     sc_busno;
84         int                     sc_baseslot;
85         struct rman             sc_mem_rman;
86         struct rman             sc_irq_rman;
87
88         struct intr_event       *sc_eventstab[AR71XX_PCI_NIRQS];        
89         mips_intrcnt_t          sc_intr_counter[AR71XX_PCI_NIRQS];      
90         struct resource         *sc_irq;
91         void                    *sc_ih;
92 };
93
94 static int ar71xx_pci_setup_intr(device_t, device_t, struct resource *, int, 
95                     driver_filter_t *, driver_intr_t *, void *, void **);
96 static int ar71xx_pci_teardown_intr(device_t, device_t, struct resource *,
97                     void *);
98 static int ar71xx_pci_intr(void *);
99
100 static void
101 ar71xx_pci_mask_irq(void *source)
102 {
103         uint32_t reg;
104         unsigned int irq = (unsigned int)source;
105
106         /* XXX is the PCI lock required here? */
107         reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK);
108         /* flush */
109         reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK);
110         ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg & ~(1 << irq));
111 }
112
113 static void
114 ar71xx_pci_unmask_irq(void *source)
115 {
116         uint32_t reg;
117         unsigned int irq = (unsigned int)source;
118
119         /* XXX is the PCI lock required here? */
120         reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK);
121         ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg | (1 << irq));
122         /* flush */
123         reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK);
124 }
125
126 /*
127  * get bitmask for bytes of interest:
128  *   0 - we want this byte, 1 - ignore it. e.g: we read 1 byte
129  *   from register 7. Bitmask would be: 0111
130  */
131 static uint32_t
132 ar71xx_get_bytes_to_read(int reg, int bytes)
133 {
134         uint32_t bytes_to_read = 0;
135
136         if ((bytes % 4) == 0)
137                 bytes_to_read = 0;
138         else if ((bytes % 4) == 1)
139                 bytes_to_read = (~(1 << (reg % 4))) & 0xf;
140         else if ((bytes % 4) == 2)
141                 bytes_to_read = (~(3 << (reg % 4))) & 0xf;
142         else
143                 panic("%s: wrong combination", __func__);
144
145         return (bytes_to_read);
146 }
147
148 static int
149 ar71xx_pci_check_bus_error(void)
150 {
151         uint32_t error, addr, has_errors = 0;
152
153         mtx_assert(&ar71xx_pci_mtx, MA_OWNED);
154
155         error = ATH_READ_REG(AR71XX_PCI_ERROR) & 0x3;
156         dprintf("%s: PCI error = %02x\n", __func__, error);
157         if (error) {
158                 addr = ATH_READ_REG(AR71XX_PCI_ERROR_ADDR);
159
160                 /* Do not report it yet */
161 #if 0
162                 printf("PCI bus error %d at addr 0x%08x\n", error, addr);
163 #endif
164                 ATH_WRITE_REG(AR71XX_PCI_ERROR, error);
165                 has_errors = 1;
166         }
167
168         error = ATH_READ_REG(AR71XX_PCI_AHB_ERROR) & 0x1;
169         dprintf("%s: AHB error = %02x\n", __func__, error);
170         if (error) {
171                 addr = ATH_READ_REG(AR71XX_PCI_AHB_ERROR_ADDR);
172                 /* Do not report it yet */
173 #if 0
174                 printf("AHB bus error %d at addr 0x%08x\n", error, addr);
175 #endif
176                 ATH_WRITE_REG(AR71XX_PCI_AHB_ERROR, error);
177                 has_errors = 1;
178         }
179
180         return (has_errors);
181 }
182
183 static uint32_t
184 ar71xx_pci_make_addr(int bus, int slot, int func, int reg)
185 {
186         if (bus == 0) {
187                 return ((1 << slot) | (func << 8) | (reg & ~3));
188         } else {
189                 return ((bus << 16) | (slot << 11) | (func << 8)
190                     | (reg  & ~3) | 1);
191         }
192 }
193
194 static int
195 ar71xx_pci_conf_setup(int bus, int slot, int func, int reg, int bytes,
196     uint32_t cmd)
197 {
198         uint32_t addr = ar71xx_pci_make_addr(bus, slot, func, (reg & ~3));
199
200         mtx_assert(&ar71xx_pci_mtx, MA_OWNED);
201
202         cmd |= (ar71xx_get_bytes_to_read(reg, bytes) << 4);
203         ATH_WRITE_REG(AR71XX_PCI_CONF_ADDR, addr);
204         ATH_WRITE_REG(AR71XX_PCI_CONF_CMD, cmd);
205
206         dprintf("%s: tag (%x, %x, %x) %d/%d addr=%08x, cmd=%08x\n", __func__, 
207             bus, slot, func, reg, bytes, addr, cmd);
208
209         return ar71xx_pci_check_bus_error();
210 }
211
212 static uint32_t
213 ar71xx_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func,
214     u_int reg, int bytes)
215 {
216         uint32_t data;
217         uint32_t shift, mask;
218
219         /* register access is 32-bit aligned */
220         shift = (reg & 3) * 8;
221
222         /* Create a mask based on the width, post-shift */
223         if (bytes == 2)
224                 mask = 0xffff;
225         else if (bytes == 1)
226                 mask = 0xff;
227         else
228                 mask = 0xffffffff;
229
230         dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 
231             func, reg, bytes);
232
233         mtx_lock_spin(&ar71xx_pci_mtx);
234          if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes, 
235              PCI_CONF_CMD_READ) == 0)
236                  data = ATH_READ_REG(AR71XX_PCI_CONF_READ_DATA);
237          else
238                  data = -1;
239         mtx_unlock_spin(&ar71xx_pci_mtx);
240
241         /* get request bytes from 32-bit word */
242         data = (data >> shift) & mask;
243
244         dprintf("%s: read 0x%x\n", __func__, data);
245
246         return (data);
247 }
248
249 static void
250 ar71xx_pci_local_write(device_t dev, uint32_t reg, uint32_t data, int bytes)
251 {
252         uint32_t cmd;
253
254         dprintf("%s: local write reg %d(%d)\n", __func__, reg, bytes);
255
256         data = data << (8*(reg % 4));
257         cmd = PCI_LCONF_CMD_WRITE | (reg & ~3);
258         cmd |= (ar71xx_get_bytes_to_read(reg, bytes) << 20);
259         mtx_lock_spin(&ar71xx_pci_mtx);
260         ATH_WRITE_REG(AR71XX_PCI_LCONF_CMD, cmd);
261         ATH_WRITE_REG(AR71XX_PCI_LCONF_WRITE_DATA, data);
262         mtx_unlock_spin(&ar71xx_pci_mtx);
263 }
264
265 static void
266 ar71xx_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
267     u_int reg, uint32_t data, int bytes)
268 {
269
270         dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot,
271             func, reg, bytes);
272
273         data = data << (8*(reg % 4));
274         mtx_lock_spin(&ar71xx_pci_mtx);
275          if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes,
276              PCI_CONF_CMD_WRITE) == 0)
277                  ATH_WRITE_REG(AR71XX_PCI_CONF_WRITE_DATA, data);
278         mtx_unlock_spin(&ar71xx_pci_mtx);
279 }
280
281 #ifdef  AR71XX_ATH_EEPROM
282 /*
283  * Some embedded boards (eg AP94) have the MAC attached via PCI but they
284  * don't have the MAC-attached EEPROM.  The register initialisation
285  * values and calibration data are stored in the on-board flash.
286  * This routine initialises the NIC via the EEPROM register contents
287  * before the probe/attach routines get a go at things.
288  */
289 static void
290 ar71xx_pci_fixup(device_t dev, u_int bus, u_int slot, u_int func,
291     long flash_addr, int len)
292 {
293         uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr);
294         uint32_t reg, val, bar0;
295
296         if (bootverbose)
297                 device_printf(dev, "%s: flash_addr=%lx, cal_data=%p\n",
298                     __func__, flash_addr, cal_data);
299
300         /* XXX check 0xa55a */
301         /* Save bar(0) address - just to flush bar(0) (SoC WAR) ? */
302         bar0 = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_BAR(0), 4);
303         ar71xx_pci_write_config(dev, bus, slot, func, PCIR_BAR(0),
304             AR71XX_PCI_MEM_BASE, 4);
305
306         val = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_COMMAND, 2);
307         val |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
308         ar71xx_pci_write_config(dev, bus, slot, func, PCIR_COMMAND, val, 2); 
309
310         cal_data += 3;
311         while (*cal_data != 0xffff) {
312                 reg = *cal_data++;
313                 val = *cal_data++;
314                 val |= (*cal_data++) << 16;
315                 if (bootverbose)
316                         printf("  reg: %x, val=%x\n", reg, val);
317
318                 /* Write eeprom fixup data to device memory */
319                 ATH_WRITE_REG(AR71XX_PCI_MEM_BASE + reg, val);
320                 DELAY(100);
321         }
322
323         val = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_COMMAND, 2);
324         val &= ~(PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
325         ar71xx_pci_write_config(dev, bus, slot, func, PCIR_COMMAND, val, 2);
326
327         /* Write the saved bar(0) address */
328         ar71xx_pci_write_config(dev, bus, slot, func, PCIR_BAR(0), bar0, 4);
329 }
330
331 static void
332 ar71xx_pci_slot_fixup(device_t dev, u_int bus, u_int slot, u_int func)
333 {
334         long int flash_addr;
335         char buf[64];
336         int size;
337
338         /*
339          * Check whether the given slot has a hint to poke.
340          */
341         if (bootverbose)
342         device_printf(dev, "%s: checking dev %s, %d/%d/%d\n",
343             __func__, device_get_nameunit(dev), bus, slot, func);
344
345         snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_addr",
346             bus, slot, func);
347
348         if (resource_long_value(device_get_name(dev), device_get_unit(dev),
349             buf, &flash_addr) == 0) {
350                 snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size",
351                     bus, slot, func);
352                 if (resource_int_value(device_get_name(dev),
353                     device_get_unit(dev), buf, &size) != 0) {
354                         device_printf(dev,
355                             "%s: missing hint '%s', aborting EEPROM\n",
356                             __func__, buf);
357                         return;
358                 }
359
360
361                 device_printf(dev, "found EEPROM at 0x%lx on %d.%d.%d\n",
362                     flash_addr, bus, slot, func);
363                 ar71xx_pci_fixup(dev, bus, slot, func, flash_addr, size);
364                 ar71xx_pci_slot_create_eeprom_firmware(dev, bus, slot, func,
365                     flash_addr, size);
366         }
367 }
368 #endif  /* AR71XX_ATH_EEPROM */
369
370 static int
371 ar71xx_pci_probe(device_t dev)
372 {
373
374         return (BUS_PROBE_NOWILDCARD);
375 }
376
377 static int
378 ar71xx_pci_attach(device_t dev)
379 {
380         int rid = 0;
381         struct ar71xx_pci_softc *sc = device_get_softc(dev);
382
383         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
384         sc->sc_mem_rman.rm_descr = "ar71xx PCI memory window";
385         if (rman_init(&sc->sc_mem_rman) != 0 || 
386             rman_manage_region(&sc->sc_mem_rman, AR71XX_PCI_MEM_BASE, 
387                 AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1) != 0) {
388                 panic("ar71xx_pci_attach: failed to set up I/O rman");
389         }
390
391         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
392         sc->sc_irq_rman.rm_descr = "ar71xx PCI IRQs";
393         if (rman_init(&sc->sc_irq_rman) != 0 ||
394             rman_manage_region(&sc->sc_irq_rman, AR71XX_PCI_IRQ_START, 
395                 AR71XX_PCI_IRQ_END) != 0)
396                 panic("ar71xx_pci_attach: failed to set up IRQ rman");
397
398         /*
399          * Check if there is a base slot hint. Otherwise use default value.
400          */
401         if (resource_int_value(device_get_name(dev),
402             device_get_unit(dev), "baseslot", &sc->sc_baseslot) != 0) {
403                 device_printf(dev,
404                     "%s: missing hint '%s', default to AR71XX_PCI_BASE_SLOT\n",
405                     __func__, "baseslot");
406                 sc->sc_baseslot = AR71XX_PCI_BASE_SLOT;
407         }
408
409         ATH_WRITE_REG(AR71XX_PCI_INTR_STATUS, 0);
410         ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, 0);
411
412         /* Hook up our interrupt handler. */
413         if ((sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
414             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
415                 device_printf(dev, "unable to allocate IRQ resource\n");
416                 return ENXIO;
417         }
418
419         if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
420                             ar71xx_pci_intr, NULL, sc, &sc->sc_ih))) {
421                 device_printf(dev,
422                     "WARNING: unable to register interrupt handler\n");
423                 return ENXIO;
424         }
425
426         /* reset PCI core and PCI bus */
427         ar71xx_device_stop(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS);
428         DELAY(100000);
429
430         ar71xx_device_start(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS);
431         DELAY(100000);
432
433         /* Init PCI windows */
434         ATH_WRITE_REG(AR71XX_PCI_WINDOW0, PCI_WINDOW0_ADDR);
435         ATH_WRITE_REG(AR71XX_PCI_WINDOW1, PCI_WINDOW1_ADDR);
436         ATH_WRITE_REG(AR71XX_PCI_WINDOW2, PCI_WINDOW2_ADDR);
437         ATH_WRITE_REG(AR71XX_PCI_WINDOW3, PCI_WINDOW3_ADDR);
438         ATH_WRITE_REG(AR71XX_PCI_WINDOW4, PCI_WINDOW4_ADDR);
439         ATH_WRITE_REG(AR71XX_PCI_WINDOW5, PCI_WINDOW5_ADDR);
440         ATH_WRITE_REG(AR71XX_PCI_WINDOW6, PCI_WINDOW6_ADDR);
441         ATH_WRITE_REG(AR71XX_PCI_WINDOW7, PCI_WINDOW7_CONF_ADDR);
442         DELAY(100000);
443
444         mtx_lock_spin(&ar71xx_pci_mtx);
445         ar71xx_pci_check_bus_error();
446         mtx_unlock_spin(&ar71xx_pci_mtx);
447
448         /* Fixup internal PCI bridge */
449         ar71xx_pci_local_write(dev, PCIR_COMMAND,
450             PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN
451             | PCIM_CMD_SERRESPEN | PCIM_CMD_BACKTOBACK
452             | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN, 4);
453
454 #ifdef  AR71XX_ATH_EEPROM
455         /*
456          * Hard-code a check for slot 17 and 18 - these are
457          * the two PCI slots which may have a PCI device that
458          * requires "fixing".
459          */
460         ar71xx_pci_slot_fixup(dev, 0, 17, 0);
461         ar71xx_pci_slot_fixup(dev, 0, 18, 0);
462 #endif  /* AR71XX_ATH_EEPROM */
463
464         device_add_child(dev, "pci", -1);
465         return (bus_generic_attach(dev));
466 }
467
468 static int
469 ar71xx_pci_read_ivar(device_t dev, device_t child, int which,
470     uintptr_t *result)
471 {
472         struct ar71xx_pci_softc *sc = device_get_softc(dev);
473
474         switch (which) {
475         case PCIB_IVAR_DOMAIN:
476                 *result = 0;
477                 return (0);
478         case PCIB_IVAR_BUS:
479                 *result = sc->sc_busno;
480                 return (0);
481         }
482
483         return (ENOENT);
484 }
485
486 static int
487 ar71xx_pci_write_ivar(device_t dev, device_t child, int which,
488     uintptr_t result)
489 {
490         struct ar71xx_pci_softc * sc = device_get_softc(dev);
491
492         switch (which) {
493         case PCIB_IVAR_BUS:
494                 sc->sc_busno = result;
495                 return (0);
496         }
497
498         return (ENOENT);
499 }
500
501 static struct resource *
502 ar71xx_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
503     u_long start, u_long end, u_long count, u_int flags)
504 {
505
506         struct ar71xx_pci_softc *sc = device_get_softc(bus);
507         struct resource *rv;
508         struct rman *rm;
509
510         switch (type) {
511         case SYS_RES_IRQ:
512                 rm = &sc->sc_irq_rman;
513                 break;
514         case SYS_RES_MEMORY:
515                 rm = &sc->sc_mem_rman;
516                 break;
517         default:
518                 return (NULL);
519         }
520
521         rv = rman_reserve_resource(rm, start, end, count, flags, child);
522
523         if (rv == NULL)
524                 return (NULL);
525
526         rman_set_rid(rv, *rid);
527
528         if (flags & RF_ACTIVE) {
529                 if (bus_activate_resource(child, type, *rid, rv)) {
530                         rman_release_resource(rv);
531                         return (NULL);
532                 }
533         }
534         return (rv);
535 }
536
537 static int
538 ar71xx_pci_activate_resource(device_t bus, device_t child, int type, int rid,
539     struct resource *r)
540 {
541         int res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
542             child, type, rid, r));
543
544         if (!res) {
545                 switch(type) {
546                 case SYS_RES_MEMORY:
547                 case SYS_RES_IOPORT:
548                         rman_set_bustag(r, ar71xx_bus_space_pcimem);
549                         break;
550                 }
551         }
552         return (res);
553 }
554
555 static int
556 ar71xx_pci_setup_intr(device_t bus, device_t child, struct resource *ires,
557             int flags, driver_filter_t *filt, driver_intr_t *handler,
558             void *arg, void **cookiep)
559 {
560         struct ar71xx_pci_softc *sc = device_get_softc(bus);
561         struct intr_event *event;
562         int irq, error;
563
564         irq = rman_get_start(ires);
565
566         if (irq > AR71XX_PCI_IRQ_END)
567                 panic("%s: bad irq %d", __func__, irq);
568
569         event = sc->sc_eventstab[irq];
570         if (event == NULL) {
571                 error = intr_event_create(&event, (void *)irq, 0, irq, 
572                     ar71xx_pci_mask_irq, ar71xx_pci_unmask_irq, NULL, NULL,
573                     "pci intr%d:", irq);
574
575                 if (error == 0) {
576                         sc->sc_eventstab[irq] = event;
577                         sc->sc_intr_counter[irq] =
578                             mips_intrcnt_create(event->ie_name);
579                 }
580                 else
581                         return (error);
582         }
583
584         intr_event_add_handler(event, device_get_nameunit(child), filt,
585             handler, arg, intr_priority(flags), flags, cookiep);
586         mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
587
588         ar71xx_pci_unmask_irq((void*)irq);
589
590         return (0);
591 }
592
593 static int
594 ar71xx_pci_teardown_intr(device_t dev, device_t child, struct resource *ires,
595     void *cookie)
596 {
597         struct ar71xx_pci_softc *sc = device_get_softc(dev);
598         int irq, result;
599
600         irq = rman_get_start(ires);
601         if (irq > AR71XX_PCI_IRQ_END)
602                 panic("%s: bad irq %d", __func__, irq);
603
604         if (sc->sc_eventstab[irq] == NULL)
605                 panic("Trying to teardown unoccupied IRQ");
606
607         ar71xx_pci_mask_irq((void*)irq);
608
609         result = intr_event_remove_handler(cookie);
610         if (!result)
611                 sc->sc_eventstab[irq] = NULL;
612
613         return (result);
614 }
615
616 static int
617 ar71xx_pci_intr(void *arg)
618 {
619         struct ar71xx_pci_softc *sc = arg;
620         struct intr_event *event;
621         uint32_t reg, irq, mask;
622
623         reg = ATH_READ_REG(AR71XX_PCI_INTR_STATUS);
624         mask = ATH_READ_REG(AR71XX_PCI_INTR_MASK);
625         /*
626          * Handle only unmasked interrupts
627          */
628         reg &= mask;
629         for (irq = AR71XX_PCI_IRQ_START; irq <= AR71XX_PCI_IRQ_END; irq++) {
630                 if (reg & (1 << irq)) {
631                         event = sc->sc_eventstab[irq];
632                         if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
633                                 /* Ignore timer interrupts */
634                                 if (irq != 0)
635                                         printf("Stray IRQ %d\n", irq);
636                                 continue;
637                         }
638
639                         /* Flush DDR FIFO for PCI/PCIe */
640                         ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_PCIE);
641
642                         /* TODO: frame instead of NULL? */
643                         intr_event_handle(event, NULL);
644                         mips_intrcnt_inc(sc->sc_intr_counter[irq]);
645                 }
646         }
647
648         return (FILTER_HANDLED);
649 }
650
651 static int
652 ar71xx_pci_maxslots(device_t dev)
653 {
654
655         return (PCI_SLOTMAX);
656 }
657
658 static int
659 ar71xx_pci_route_interrupt(device_t pcib, device_t device, int pin)
660 {
661         struct ar71xx_pci_softc *sc = device_get_softc(pcib);
662         
663         if (pci_get_slot(device) < sc->sc_baseslot)
664                 panic("%s: PCI slot %d is less then AR71XX_PCI_BASE_SLOT",
665                     __func__, pci_get_slot(device));
666
667         return (pci_get_slot(device) - sc->sc_baseslot);
668 }
669
670 static device_method_t ar71xx_pci_methods[] = {
671         /* Device interface */
672         DEVMETHOD(device_probe,         ar71xx_pci_probe),
673         DEVMETHOD(device_attach,        ar71xx_pci_attach),
674         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
675         DEVMETHOD(device_suspend,       bus_generic_suspend),
676         DEVMETHOD(device_resume,        bus_generic_resume),
677
678         /* Bus interface */
679         DEVMETHOD(bus_read_ivar,        ar71xx_pci_read_ivar),
680         DEVMETHOD(bus_write_ivar,       ar71xx_pci_write_ivar),
681         DEVMETHOD(bus_alloc_resource,   ar71xx_pci_alloc_resource),
682         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
683         DEVMETHOD(bus_activate_resource, ar71xx_pci_activate_resource),
684         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
685         DEVMETHOD(bus_setup_intr,       ar71xx_pci_setup_intr),
686         DEVMETHOD(bus_teardown_intr,    ar71xx_pci_teardown_intr),
687
688         /* pcib interface */
689         DEVMETHOD(pcib_maxslots,        ar71xx_pci_maxslots),
690         DEVMETHOD(pcib_read_config,     ar71xx_pci_read_config),
691         DEVMETHOD(pcib_write_config,    ar71xx_pci_write_config),
692         DEVMETHOD(pcib_route_interrupt, ar71xx_pci_route_interrupt),
693
694         DEVMETHOD_END
695 };
696
697 static driver_t ar71xx_pci_driver = {
698         "pcib",
699         ar71xx_pci_methods,
700         sizeof(struct ar71xx_pci_softc),
701 };
702
703 static devclass_t ar71xx_pci_devclass;
704
705 DRIVER_MODULE(ar71xx_pci, nexus, ar71xx_pci_driver, ar71xx_pci_devclass, 0, 0);