2 * Atheros AR724x PCI host controller driver
4 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
6 * Parts of this file are based on Atheros' 2.6.15 BSP
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/resource.h>
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/bitops.h>
17 #include <linux/pci.h>
18 #include <linux/pci_regs.h>
19 #include <linux/interrupt.h>
21 #include <asm/mach-ar71xx/ar71xx.h>
22 #include <asm/mach-ar71xx/pci.h>
26 #define DBG(fmt, args...) printk(KERN_INFO fmt, ## args)
28 #define DBG(fmt, args...)
31 static void __iomem *ar724x_pci_localcfg_base;
32 static void __iomem *ar724x_pci_devcfg_base;
33 static int ar724x_pci_fixup_enable;
35 static DEFINE_SPINLOCK(ar724x_pci_lock);
37 static void ar724x_pci_read(void __iomem *base, int where, int size, u32 *value)
42 spin_lock_irqsave(&ar724x_pci_lock, flags);
43 data = __raw_readl(base + (where & ~3));
61 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
64 static void ar724x_pci_write(void __iomem *base, int where, int size, u32 value)
70 spin_lock_irqsave(&ar724x_pci_lock, flags);
71 data = __raw_readl(base + (where & ~3));
75 s = ((where & 3) << 3);
77 data |= ((value & 0xFF) << s);
80 s = ((where & 2) << 3);
81 data &= ~(0xFFFF << s);
82 data |= ((value & 0xFFFF) << s);
89 __raw_writel(data, base + (where & ~3));
91 (void)__raw_readl(base + (where & ~3));
92 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
95 static int ar724x_pci_read_config(struct pci_bus *bus, unsigned int devfn,
96 int where, int size, u32 *value)
99 if (bus->number != 0 || devfn != 0)
100 return PCIBIOS_DEVICE_NOT_FOUND;
102 ar724x_pci_read(ar724x_pci_devcfg_base, where, size, value);
104 DBG("PCI: read config: %02x:%02x.%01x/%02x:%01d, value=%08x\n",
105 bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
106 where, size, *value);
109 * WAR for BAR issue - We are unable to access the PCI device space
110 * if we set the BAR with proper base address
112 if ((where == 0x10) && (size == 4))
113 ar724x_pci_write(ar724x_pci_devcfg_base, where, size, 0xffff);
115 return PCIBIOS_SUCCESSFUL;
118 static int ar724x_pci_write_config(struct pci_bus *bus, unsigned int devfn,
119 int where, int size, u32 value)
121 if (bus->number != 0 || devfn != 0)
122 return PCIBIOS_DEVICE_NOT_FOUND;
124 DBG("PCI: write config: %02x:%02x.%01x/%02x:%01d, value=%08x\n",
125 bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
128 ar724x_pci_write(ar724x_pci_devcfg_base, where, size, value);
130 return PCIBIOS_SUCCESSFUL;
133 static void ar724x_pci_fixup(struct pci_dev *dev)
137 if (!ar724x_pci_fixup_enable)
140 if (dev->bus->number != 0 || dev->devfn != 0)
143 /* setup COMMAND register */
144 pci_read_config_word(dev, PCI_COMMAND, &cmd);
145 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
146 PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR |
147 PCI_COMMAND_FAST_BACK;
149 pci_write_config_word(dev, PCI_COMMAND, cmd);
151 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, ar724x_pci_fixup);
153 int __init ar724x_pcibios_map_irq(const struct pci_dev *dev, uint8_t slot,
159 for (i = 0; i < ar71xx_pci_nr_irqs; i++) {
160 struct ar71xx_pci_irq *entry;
161 entry = &ar71xx_pci_irq_map[i];
163 if (entry->slot == slot && entry->pin == pin) {
170 printk(KERN_ALERT "PCI: no irq found for pin%u@%s\n",
171 pin, pci_name((struct pci_dev *)dev));
173 printk(KERN_INFO "PCI: mapping irq %d to pin%u@%s\n",
174 irq, pin, pci_name((struct pci_dev *)dev));
179 static struct pci_ops ar724x_pci_ops = {
180 .read = ar724x_pci_read_config,
181 .write = ar724x_pci_write_config,
184 static struct resource ar724x_pci_io_resource = {
185 .name = "PCI IO space",
188 .flags = IORESOURCE_IO,
191 static struct resource ar724x_pci_mem_resource = {
192 .name = "PCI memory space",
193 .start = AR71XX_PCI_MEM_BASE,
194 .end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1,
195 .flags = IORESOURCE_MEM
198 static struct pci_controller ar724x_pci_controller = {
199 .pci_ops = &ar724x_pci_ops,
200 .mem_resource = &ar724x_pci_mem_resource,
201 .io_resource = &ar724x_pci_io_resource,
204 static void __init ar724x_pci_reset(void)
206 ar71xx_device_stop(AR724X_RESET_PCIE);
207 ar71xx_device_stop(AR724X_RESET_PCIE_PHY);
208 ar71xx_device_stop(AR724X_RESET_PCIE_PHY_SERIAL);
211 ar71xx_device_start(AR724X_RESET_PCIE_PHY_SERIAL);
213 ar71xx_device_start(AR724X_RESET_PCIE_PHY);
214 ar71xx_device_start(AR724X_RESET_PCIE);
217 static int __init ar724x_pci_setup(void)
221 /* setup COMMAND register */
222 t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE |
223 PCI_COMMAND_PARITY|PCI_COMMAND_SERR|PCI_COMMAND_FAST_BACK;
225 ar724x_pci_write(ar724x_pci_localcfg_base, PCI_COMMAND, 4, t);
226 ar724x_pci_write(ar724x_pci_localcfg_base, 0x20, 4, 0x1ff01000);
227 ar724x_pci_write(ar724x_pci_localcfg_base, 0x24, 4, 0x1ff01000);
229 t = ar724x_pci_rr(AR724X_PCI_REG_RESET);
232 ar724x_pci_wr_nf(AR724X_PCI_REG_RESET, 0);
234 ar724x_pci_wr_nf(AR724X_PCI_REG_RESET, 4);
238 ar724x_pci_wr(AR724X_PCI_REG_APP, AR724X_PCI_APP_LTSSM_ENABLE);
241 t = ar724x_pci_rr(AR724X_PCI_REG_APP);
242 if ((t & AR724X_PCI_APP_LTSSM_ENABLE) == 0x0) {
243 printk(KERN_WARNING "PCI: no PCIe module found\n");
250 static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
254 pending = ar724x_pci_rr(AR724X_PCI_REG_INT_STATUS) &
255 ar724x_pci_rr(AR724X_PCI_REG_INT_MASK);
257 if (pending & AR724X_PCI_INT_DEV0)
258 generic_handle_irq(AR71XX_PCI_IRQ_DEV0);
261 spurious_interrupt();
264 static void ar724x_pci_irq_unmask(unsigned int irq)
267 case AR71XX_PCI_IRQ_DEV0:
268 irq -= AR71XX_PCI_IRQ_BASE;
269 ar724x_pci_wr(AR724X_PCI_REG_INT_MASK,
270 ar724x_pci_rr(AR724X_PCI_REG_INT_MASK) |
271 AR724X_PCI_INT_DEV0);
273 ar724x_pci_rr(AR724X_PCI_REG_INT_MASK);
277 static void ar724x_pci_irq_mask(unsigned int irq)
280 case AR71XX_PCI_IRQ_DEV0:
281 irq -= AR71XX_PCI_IRQ_BASE;
282 ar724x_pci_wr(AR724X_PCI_REG_INT_MASK,
283 ar724x_pci_rr(AR724X_PCI_REG_INT_MASK) &
284 ~AR724X_PCI_INT_DEV0);
286 ar724x_pci_rr(AR724X_PCI_REG_INT_MASK);
288 ar724x_pci_wr(AR724X_PCI_REG_INT_STATUS,
289 ar724x_pci_rr(AR724X_PCI_REG_INT_STATUS) |
290 AR724X_PCI_INT_DEV0);
292 ar724x_pci_rr(AR724X_PCI_REG_INT_STATUS);
296 static struct irq_chip ar724x_pci_irq_chip = {
297 .name = "AR724X PCI ",
298 .mask = ar724x_pci_irq_mask,
299 .unmask = ar724x_pci_irq_unmask,
300 .mask_ack = ar724x_pci_irq_mask,
303 static void __init ar724x_pci_irq_init(void)
308 t = ar71xx_reset_rr(AR724X_RESET_REG_RESET_MODULE);
309 if (t & (AR724X_RESET_PCIE | AR724X_RESET_PCIE_PHY |
310 AR724X_RESET_PCIE_PHY_SERIAL)) {
314 ar724x_pci_wr(AR724X_PCI_REG_INT_MASK, 0);
315 ar724x_pci_wr(AR724X_PCI_REG_INT_STATUS, 0);
317 for (i = AR71XX_PCI_IRQ_BASE;
318 i < AR71XX_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++) {
319 irq_desc[i].status = IRQ_DISABLED;
320 set_irq_chip_and_handler(i, &ar724x_pci_irq_chip,
324 set_irq_chained_handler(AR71XX_CPU_IRQ_IP2, ar724x_pci_irq_handler);
327 int __init ar724x_pcibios_init(void)
331 ar724x_pci_localcfg_base = ioremap_nocache(AR724X_PCI_CRP_BASE,
332 AR724X_PCI_CRP_SIZE);
334 ar724x_pci_devcfg_base = ioremap_nocache(AR724X_PCI_CFG_BASE,
335 AR724X_PCI_CFG_SIZE);
338 ret = ar724x_pci_setup();
342 ar724x_pci_fixup_enable = 1;
343 ar724x_pci_irq_init();
344 register_pci_controller(&ar724x_pci_controller);