2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2011, Luiz Otavio O Souza.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/interrupt.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
44 #include <vm/vm_extern.h>
46 #include <machine/bus.h>
47 #include <machine/cpu.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/pmap.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcib_private.h>
57 #include <mips/atheros/ar71xxreg.h>
58 #include <mips/atheros/ar724xreg.h>
59 #include <mips/atheros/ar71xx_setup.h>
60 #include <mips/atheros/ar71xx_pci_bus_space.h>
62 #include <mips/atheros/ar71xx_cpudef.h>
64 #undef AR724X_PCI_DEBUG
65 #ifdef AR724X_PCI_DEBUG
66 #define dprintf printf
68 #define dprintf(x, arg...)
71 struct ar71xx_pci_softc {
75 struct rman sc_mem_rman;
76 struct rman sc_irq_rman;
78 struct intr_event *sc_eventstab[AR71XX_PCI_NIRQS];
79 mips_intrcnt_t sc_intr_counter[AR71XX_PCI_NIRQS];
80 struct resource *sc_irq;
84 static int ar724x_pci_setup_intr(device_t, device_t, struct resource *, int,
85 driver_filter_t *, driver_intr_t *, void *, void **);
86 static int ar724x_pci_teardown_intr(device_t, device_t, struct resource *,
88 static int ar724x_pci_intr(void *);
91 ar724x_pci_write(uint32_t reg, uint32_t offset, uint32_t data, int bytes)
93 uint32_t val, mask, shift;
95 /* Register access is 32-bit aligned */
96 shift = (offset & 3) * 8;
98 mask = (1 << (bytes * 8)) - 1;
102 val = ATH_READ_REG(reg + (offset & ~3));
103 val &= ~(mask << shift);
104 val |= ((data & mask) << shift);
105 ATH_WRITE_REG(reg + (offset & ~3), val);
107 dprintf("%s: %#x/%#x addr=%#x, data=%#x(%#x), bytes=%d\n", __func__,
108 reg, reg + (offset & ~3), offset, data, val, bytes);
112 ar724x_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func,
113 u_int reg, int bytes)
115 uint32_t data, shift, mask;
117 /* Register access is 32-bit aligned */
118 shift = (reg & 3) * 8;
120 mask = (1 << shift) - 1;
124 dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot,
127 if ((bus == 0) && (slot == 0) && (func == 0))
128 data = ATH_READ_REG(AR724X_PCI_CFG_BASE + (reg & ~3));
132 /* Get request bytes from 32-bit word */
133 data = (data >> shift) & mask;
135 dprintf("%s: read 0x%x\n", __func__, data);
141 ar724x_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
142 u_int reg, uint32_t data, int bytes)
145 dprintf("%s: tag (%x, %x, %x) reg %d(%d): %x\n", __func__, bus, slot,
146 func, reg, bytes, data);
148 if ((bus != 0) || (slot != 0) || (func != 0))
152 * WAR for BAR issue on AR7240 - We are unable to access the PCI device
153 * space if we set the BAR with proper base address.
155 if (reg == PCIR_BAR(0) && bytes == 4 && ar71xx_soc == AR71XX_SOC_AR7240)
156 ar724x_pci_write(AR724X_PCI_CFG_BASE, reg, 0xffff, bytes);
158 ar724x_pci_write(AR724X_PCI_CFG_BASE, reg, data, bytes);
162 ar724x_pci_mask_irq(void *source)
165 unsigned int irq = (unsigned int)source;
167 /* XXX - Only one interrupt ? Only one device ? */
168 if (irq != AR71XX_PCI_IRQ_START)
171 /* Update the interrupt mask reg */
172 reg = ATH_READ_REG(AR724X_PCI_INTR_MASK);
173 ATH_WRITE_REG(AR724X_PCI_INTR_MASK,
174 reg & ~AR724X_PCI_INTR_DEV0);
176 /* Clear any pending interrupt */
177 reg = ATH_READ_REG(AR724X_PCI_INTR_STATUS);
178 ATH_WRITE_REG(AR724X_PCI_INTR_STATUS,
179 reg | AR724X_PCI_INTR_DEV0);
183 ar724x_pci_unmask_irq(void *source)
186 unsigned int irq = (unsigned int)source;
189 if (irq != AR71XX_PCI_IRQ_START)
192 /* Update the interrupt mask reg */
193 reg = ATH_READ_REG(AR724X_PCI_INTR_MASK);
194 ATH_WRITE_REG(AR724X_PCI_INTR_MASK,
195 reg | AR724X_PCI_INTR_DEV0);
199 ar724x_pci_setup(device_t dev)
203 /* setup COMMAND register */
204 reg = PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN | PCIM_CMD_SERRESPEN |
205 PCIM_CMD_BACKTOBACK | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN;
207 ar724x_pci_write(AR724X_PCI_CRP_BASE, PCIR_COMMAND, reg, 2);
208 ar724x_pci_write(AR724X_PCI_CRP_BASE, 0x20, 0x1ff01000, 4);
209 ar724x_pci_write(AR724X_PCI_CRP_BASE, 0x24, 0x1ff01000, 4);
211 reg = ATH_READ_REG(AR724X_PCI_RESET);
214 ATH_WRITE_REG(AR724X_PCI_RESET, 0);
216 ATH_WRITE_REG(AR724X_PCI_RESET, 4);
220 if (ar71xx_soc == AR71XX_SOC_AR7240)
221 reg = AR724X_PCI_APP_LTSSM_ENABLE;
224 ATH_WRITE_REG(AR724X_PCI_APP, reg);
226 (void) ATH_READ_REG(AR724X_PCI_APP);
230 reg = ATH_READ_REG(AR724X_PCI_RESET);
231 if ((reg & AR724X_PCI_RESET_LINK_UP) == 0) {
232 device_printf(dev, "no PCIe controller found\n");
236 if (ar71xx_soc == AR71XX_SOC_AR7241 ||
237 ar71xx_soc == AR71XX_SOC_AR7242) {
238 reg = ATH_READ_REG(AR724X_PCI_APP);
240 ATH_WRITE_REG(AR724X_PCI_APP, reg);
246 #define AR5416_EEPROM_MAGIC 0xa55a
249 * XXX - This should not be here ! And this looks like Atheros (if_ath) only.
252 ar724x_load_eeprom_data(device_t dev)
254 uint32_t bar0, hint, reg, val;
255 uint16_t *data = NULL;
257 /* Search for a hint of eeprom data offset */
258 if (resource_int_value(device_get_name(dev),
259 device_get_unit(dev), "eepromdata", &hint) != 0)
262 device_printf(dev, "Loading the eeprom fixup data from %#x\n", hint);
263 data = (uint16_t *)MIPS_PHYS_TO_KSEG1(hint);
265 if (*data != AR5416_EEPROM_MAGIC) {
266 device_printf(dev, "Invalid calibration data from %#x\n", hint);
270 /* Save bar(0) address - just to flush bar(0) (SoC WAR) ? */
271 bar0 = ar724x_pci_read_config(dev, 0, 0, 0, PCIR_BAR(0), 4);
273 val = ar724x_pci_read_config(dev, 0, 0, 0, PCIR_COMMAND, 2);
274 val |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
275 ar724x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND, val, 2);
277 /* set pointer to first reg address */
279 while (*data != 0xffff) {
282 val |= (*data++) << 16;
284 /* Write eeprom fixup data to device memory */
285 ATH_WRITE_REG(AR71XX_PCI_MEM_BASE + reg, val);
289 val = ar724x_pci_read_config(dev, 0, 0, 0, PCIR_COMMAND, 2);
290 val &= ~(PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN);
291 ar724x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND, val, 2);
293 /* Write the saved bar(0) address */
294 ar724x_pci_write_config(dev, 0, 0, 0, PCIR_BAR(0), bar0, 4);
297 #undef AR5416_EEPROM_MAGIC
300 ar724x_pci_probe(device_t dev)
307 ar724x_pci_attach(device_t dev)
309 struct ar71xx_pci_softc *sc = device_get_softc(dev);
313 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
314 sc->sc_mem_rman.rm_descr = "ar724x PCI memory window";
315 if (rman_init(&sc->sc_mem_rman) != 0 ||
316 rman_manage_region(&sc->sc_mem_rman, AR71XX_PCI_MEM_BASE,
317 AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1) != 0) {
318 panic("ar724x_pci_attach: failed to set up I/O rman");
321 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
322 sc->sc_irq_rman.rm_descr = "ar724x PCI IRQs";
323 if (rman_init(&sc->sc_irq_rman) != 0 ||
324 rman_manage_region(&sc->sc_irq_rman, AR71XX_PCI_IRQ_START,
325 AR71XX_PCI_IRQ_END) != 0)
326 panic("ar724x_pci_attach: failed to set up IRQ rman");
328 /* Disable interrupts */
329 ATH_WRITE_REG(AR724X_PCI_INTR_STATUS, 0);
330 ATH_WRITE_REG(AR724X_PCI_INTR_MASK, 0);
332 /* Hook up our interrupt handler. */
333 if ((sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
334 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
335 device_printf(dev, "unable to allocate IRQ resource\n");
339 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
340 ar724x_pci_intr, NULL, sc, &sc->sc_ih))) {
342 "WARNING: unable to register interrupt handler\n");
346 /* Reset PCIe core and PCIe PHY */
347 ar71xx_device_stop(AR724X_RESET_PCIE);
348 ar71xx_device_stop(AR724X_RESET_PCIE_PHY);
349 ar71xx_device_stop(AR724X_RESET_PCIE_PHY_SERIAL);
352 ar71xx_device_start(AR724X_RESET_PCIE_PHY_SERIAL);
354 ar71xx_device_start(AR724X_RESET_PCIE_PHY);
355 ar71xx_device_start(AR724X_RESET_PCIE);
357 if (ar724x_pci_setup(dev))
360 /* XXX - Load eeprom fixup data */
361 ar724x_load_eeprom_data(dev);
363 /* Fixup internal PCI bridge */
364 ar724x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND,
365 PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN
366 | PCIM_CMD_SERRESPEN | PCIM_CMD_BACKTOBACK
367 | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN, 2);
369 device_add_child(dev, "pci", busno);
370 return (bus_generic_attach(dev));
374 ar724x_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
376 struct ar71xx_pci_softc *sc = device_get_softc(dev);
379 case PCIB_IVAR_DOMAIN:
383 *result = sc->sc_busno;
391 ar724x_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
393 struct ar71xx_pci_softc * sc = device_get_softc(dev);
397 sc->sc_busno = result;
404 static struct resource *
405 ar724x_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
406 u_long start, u_long end, u_long count, u_int flags)
408 struct ar71xx_pci_softc *sc = device_get_softc(bus);
414 rm = &sc->sc_irq_rman;
417 rm = &sc->sc_mem_rman;
423 rv = rman_reserve_resource(rm, start, end, count, flags, child);
428 rman_set_rid(rv, *rid);
430 if (flags & RF_ACTIVE) {
431 if (bus_activate_resource(child, type, *rid, rv)) {
432 rman_release_resource(rv);
442 ar724x_pci_activate_resource(device_t bus, device_t child, int type, int rid,
445 int res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
446 child, type, rid, r));
453 rman_set_bustag(r, ar71xx_bus_space_pcimem);
462 ar724x_pci_setup_intr(device_t bus, device_t child, struct resource *ires,
463 int flags, driver_filter_t *filt, driver_intr_t *handler,
464 void *arg, void **cookiep)
466 struct ar71xx_pci_softc *sc = device_get_softc(bus);
467 struct intr_event *event;
470 irq = rman_get_start(ires);
471 if (irq > AR71XX_PCI_IRQ_END)
472 panic("%s: bad irq %d", __func__, irq);
474 event = sc->sc_eventstab[irq];
476 error = intr_event_create(&event, (void *)irq, 0, irq,
477 ar724x_pci_mask_irq, ar724x_pci_unmask_irq, NULL, NULL,
481 sc->sc_eventstab[irq] = event;
482 sc->sc_intr_counter[irq] =
483 mips_intrcnt_create(event->ie_name);
489 intr_event_add_handler(event, device_get_nameunit(child), filt,
490 handler, arg, intr_priority(flags), flags, cookiep);
491 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
493 ar724x_pci_unmask_irq((void*)irq);
499 ar724x_pci_teardown_intr(device_t dev, device_t child, struct resource *ires,
502 struct ar71xx_pci_softc *sc = device_get_softc(dev);
505 irq = rman_get_start(ires);
506 if (irq > AR71XX_PCI_IRQ_END)
507 panic("%s: bad irq %d", __func__, irq);
509 if (sc->sc_eventstab[irq] == NULL)
510 panic("Trying to teardown unoccupied IRQ");
512 ar724x_pci_mask_irq((void*)irq);
514 result = intr_event_remove_handler(cookie);
516 sc->sc_eventstab[irq] = NULL;
522 ar724x_pci_intr(void *arg)
524 struct ar71xx_pci_softc *sc = arg;
525 struct intr_event *event;
526 uint32_t reg, irq, mask;
528 ar71xx_device_ddr_flush_ip2();
530 reg = ATH_READ_REG(AR724X_PCI_INTR_STATUS);
531 mask = ATH_READ_REG(AR724X_PCI_INTR_MASK);
533 * Handle only unmasked interrupts
536 if (reg & AR724X_PCI_INTR_DEV0) {
538 irq = AR71XX_PCI_IRQ_START;
539 event = sc->sc_eventstab[irq];
540 if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
541 printf("Stray IRQ %d\n", irq);
542 return (FILTER_STRAY);
545 /* TODO: frame instead of NULL? */
546 intr_event_handle(event, NULL);
547 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
550 return (FILTER_HANDLED);
554 ar724x_pci_maxslots(device_t dev)
557 return (PCI_SLOTMAX);
561 ar724x_pci_route_interrupt(device_t pcib, device_t device, int pin)
564 return (pci_get_slot(device));
567 static device_method_t ar724x_pci_methods[] = {
568 /* Device interface */
569 DEVMETHOD(device_probe, ar724x_pci_probe),
570 DEVMETHOD(device_attach, ar724x_pci_attach),
571 DEVMETHOD(device_shutdown, bus_generic_shutdown),
572 DEVMETHOD(device_suspend, bus_generic_suspend),
573 DEVMETHOD(device_resume, bus_generic_resume),
576 DEVMETHOD(bus_read_ivar, ar724x_pci_read_ivar),
577 DEVMETHOD(bus_write_ivar, ar724x_pci_write_ivar),
578 DEVMETHOD(bus_alloc_resource, ar724x_pci_alloc_resource),
579 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
580 DEVMETHOD(bus_activate_resource, ar724x_pci_activate_resource),
581 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
582 DEVMETHOD(bus_setup_intr, ar724x_pci_setup_intr),
583 DEVMETHOD(bus_teardown_intr, ar724x_pci_teardown_intr),
586 DEVMETHOD(pcib_maxslots, ar724x_pci_maxslots),
587 DEVMETHOD(pcib_read_config, ar724x_pci_read_config),
588 DEVMETHOD(pcib_write_config, ar724x_pci_write_config),
589 DEVMETHOD(pcib_route_interrupt, ar724x_pci_route_interrupt),
594 static driver_t ar724x_pci_driver = {
597 sizeof(struct ar71xx_pci_softc),
600 static devclass_t ar724x_pci_devclass;
602 DRIVER_MODULE(ar724x_pci, nexus, ar724x_pci_driver, ar724x_pci_devclass, 0, 0);