1 From 4201b6aeb059b481571c241a2fc96fd3f41032e9 Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg@openwrt.org>
3 Date: Wed, 14 Mar 2012 10:36:10 +0100
4 Subject: [PATCH 19/47] MIPS: ath79: add support for the PCI host controller of the AR71XX SoCs
6 The Atheros AR71XX SoCs have a built-in PCI Host Controller.
7 This patch adds a driver for that, and modifies the relevant
8 files in order to allow to register the PCI controller from
11 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
12 Signed-off-by: Imre Kaloz <kaloz@openwrt.org>
13 Cc: linux-mips@linux-mips.org
14 Patchwork: https://patchwork.linux-mips.org/patch/3498/
15 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
17 arch/mips/ath79/Kconfig | 1 +
18 arch/mips/include/asm/mach-ath79/pci.h | 6 +
19 arch/mips/pci/Makefile | 1 +
20 arch/mips/pci/pci-ar71xx.c | 375 ++++++++++++++++++++++++++++++++
21 4 files changed, 383 insertions(+), 0 deletions(-)
22 create mode 100644 arch/mips/pci/pci-ar71xx.c
24 --- a/arch/mips/ath79/Kconfig
25 +++ b/arch/mips/ath79/Kconfig
26 @@ -52,6 +52,7 @@ endmenu
28 select USB_ARCH_HAS_EHCI
29 select USB_ARCH_HAS_OHCI
34 --- a/arch/mips/include/asm/mach-ath79/pci.h
35 +++ b/arch/mips/include/asm/mach-ath79/pci.h
37 #ifndef __ASM_MACH_ATH79_PCI_H
38 #define __ASM_MACH_ATH79_PCI_H
40 +#if defined(CONFIG_PCI) && defined(CONFIG_SOC_AR71XX)
41 +int ar71xx_pcibios_init(void);
43 +static inline int ar71xx_pcibios_init(void) { return 0; }
46 #if defined(CONFIG_PCI) && defined(CONFIG_SOC_AR724X)
47 int ar724x_pcibios_init(int irq);
49 --- a/arch/mips/pci/Makefile
50 +++ b/arch/mips/pci/Makefile
51 @@ -19,6 +19,7 @@ obj-$(CONFIG_BCM47XX) += pci-bcm47xx.o
52 obj-$(CONFIG_BCM63XX) += pci-bcm63xx.o fixup-bcm63xx.o \
54 obj-$(CONFIG_MIPS_ALCHEMY) += pci-alchemy.o
55 +obj-$(CONFIG_SOC_AR71XX) += pci-ar71xx.o
56 obj-$(CONFIG_SOC_AR724X) += pci-ar724x.o
60 +++ b/arch/mips/pci/pci-ar71xx.c
63 + * Atheros AR71xx PCI host controller driver
65 + * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
66 + * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
68 + * Parts of this file are based on Atheros' 2.6.15 BSP
70 + * This program is free software; you can redistribute it and/or modify it
71 + * under the terms of the GNU General Public License version 2 as published
72 + * by the Free Software Foundation.
75 +#include <linux/resource.h>
76 +#include <linux/types.h>
77 +#include <linux/delay.h>
78 +#include <linux/bitops.h>
79 +#include <linux/pci.h>
80 +#include <linux/pci_regs.h>
81 +#include <linux/interrupt.h>
83 +#include <asm/mach-ath79/ar71xx_regs.h>
84 +#include <asm/mach-ath79/ath79.h>
85 +#include <asm/mach-ath79/pci.h>
87 +#define AR71XX_PCI_MEM_BASE 0x10000000
88 +#define AR71XX_PCI_MEM_SIZE 0x08000000
90 +#define AR71XX_PCI_WIN0_OFFS 0x10000000
91 +#define AR71XX_PCI_WIN1_OFFS 0x11000000
92 +#define AR71XX_PCI_WIN2_OFFS 0x12000000
93 +#define AR71XX_PCI_WIN3_OFFS 0x13000000
94 +#define AR71XX_PCI_WIN4_OFFS 0x14000000
95 +#define AR71XX_PCI_WIN5_OFFS 0x15000000
96 +#define AR71XX_PCI_WIN6_OFFS 0x16000000
97 +#define AR71XX_PCI_WIN7_OFFS 0x07000000
99 +#define AR71XX_PCI_CFG_BASE \
100 + (AR71XX_PCI_MEM_BASE + AR71XX_PCI_WIN7_OFFS + 0x10000)
101 +#define AR71XX_PCI_CFG_SIZE 0x100
103 +#define AR71XX_PCI_REG_CRP_AD_CBE 0x00
104 +#define AR71XX_PCI_REG_CRP_WRDATA 0x04
105 +#define AR71XX_PCI_REG_CRP_RDDATA 0x08
106 +#define AR71XX_PCI_REG_CFG_AD 0x0c
107 +#define AR71XX_PCI_REG_CFG_CBE 0x10
108 +#define AR71XX_PCI_REG_CFG_WRDATA 0x14
109 +#define AR71XX_PCI_REG_CFG_RDDATA 0x18
110 +#define AR71XX_PCI_REG_PCI_ERR 0x1c
111 +#define AR71XX_PCI_REG_PCI_ERR_ADDR 0x20
112 +#define AR71XX_PCI_REG_AHB_ERR 0x24
113 +#define AR71XX_PCI_REG_AHB_ERR_ADDR 0x28
115 +#define AR71XX_PCI_CRP_CMD_WRITE 0x00010000
116 +#define AR71XX_PCI_CRP_CMD_READ 0x00000000
117 +#define AR71XX_PCI_CFG_CMD_READ 0x0000000a
118 +#define AR71XX_PCI_CFG_CMD_WRITE 0x0000000b
120 +#define AR71XX_PCI_INT_CORE BIT(4)
121 +#define AR71XX_PCI_INT_DEV2 BIT(2)
122 +#define AR71XX_PCI_INT_DEV1 BIT(1)
123 +#define AR71XX_PCI_INT_DEV0 BIT(0)
125 +#define AR71XX_PCI_IRQ_COUNT 5
127 +static DEFINE_SPINLOCK(ar71xx_pci_lock);
128 +static void __iomem *ar71xx_pcicfg_base;
130 +/* Byte lane enable bits */
131 +static const u8 ar71xx_pci_ble_table[4][4] = {
132 + {0x0, 0xf, 0xf, 0xf},
133 + {0xe, 0xd, 0xb, 0x7},
134 + {0xc, 0xf, 0x3, 0xf},
135 + {0xf, 0xf, 0xf, 0xf},
138 +static const u32 ar71xx_pci_read_mask[8] = {
139 + 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
142 +static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
146 + t = ar71xx_pci_ble_table[size & 3][where & 3];
148 + t <<= (local) ? 20 : 4;
153 +static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
158 + if (!bus->number) {
160 + ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
164 + ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
165 + (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
171 +static int ar71xx_pci_check_error(int quiet)
173 + void __iomem *base = ar71xx_pcicfg_base;
177 + pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
182 + addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
183 + pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
184 + "PCI", pci_err, addr);
187 + /* clear PCI error status */
188 + __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
191 + ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
196 + addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
197 + pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
198 + "AHB", ahb_err, addr);
201 + /* clear AHB error status */
202 + __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
205 + return !!(ahb_err | pci_err);
208 +static inline void ar71xx_pci_local_write(int where, int size, u32 value)
210 + void __iomem *base = ar71xx_pcicfg_base;
213 + value = value << (8 * (where & 3));
215 + ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
216 + ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
218 + __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
219 + __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
222 +static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
223 + unsigned int devfn,
224 + int where, int size, u32 cmd)
226 + void __iomem *base = ar71xx_pcicfg_base;
229 + addr = ar71xx_pci_bus_addr(bus, devfn, where);
231 + __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
232 + __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
233 + base + AR71XX_PCI_REG_CFG_CBE);
235 + return ar71xx_pci_check_error(1);
238 +static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
239 + int where, int size, u32 *value)
241 + void __iomem *base = ar71xx_pcicfg_base;
242 + unsigned long flags;
247 + ret = PCIBIOS_SUCCESSFUL;
250 + spin_lock_irqsave(&ar71xx_pci_lock, flags);
252 + err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
253 + AR71XX_PCI_CFG_CMD_READ);
255 + ret = PCIBIOS_DEVICE_NOT_FOUND;
257 + data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
259 + spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
261 + *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
266 +static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
267 + int where, int size, u32 value)
269 + void __iomem *base = ar71xx_pcicfg_base;
270 + unsigned long flags;
274 + value = value << (8 * (where & 3));
275 + ret = PCIBIOS_SUCCESSFUL;
277 + spin_lock_irqsave(&ar71xx_pci_lock, flags);
279 + err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
280 + AR71XX_PCI_CFG_CMD_WRITE);
282 + ret = PCIBIOS_DEVICE_NOT_FOUND;
284 + __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
286 + spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
291 +static struct pci_ops ar71xx_pci_ops = {
292 + .read = ar71xx_pci_read_config,
293 + .write = ar71xx_pci_write_config,
296 +static struct resource ar71xx_pci_io_resource = {
297 + .name = "PCI IO space",
300 + .flags = IORESOURCE_IO,
303 +static struct resource ar71xx_pci_mem_resource = {
304 + .name = "PCI memory space",
305 + .start = AR71XX_PCI_MEM_BASE,
306 + .end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1,
307 + .flags = IORESOURCE_MEM
310 +static struct pci_controller ar71xx_pci_controller = {
311 + .pci_ops = &ar71xx_pci_ops,
312 + .mem_resource = &ar71xx_pci_mem_resource,
313 + .io_resource = &ar71xx_pci_io_resource,
316 +static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
318 + void __iomem *base = ath79_reset_base;
321 + pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
322 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
324 + if (pending & AR71XX_PCI_INT_DEV0)
325 + generic_handle_irq(ATH79_PCI_IRQ(0));
327 + else if (pending & AR71XX_PCI_INT_DEV1)
328 + generic_handle_irq(ATH79_PCI_IRQ(1));
330 + else if (pending & AR71XX_PCI_INT_DEV2)
331 + generic_handle_irq(ATH79_PCI_IRQ(2));
333 + else if (pending & AR71XX_PCI_INT_CORE)
334 + generic_handle_irq(ATH79_PCI_IRQ(4));
337 + spurious_interrupt();
340 +static void ar71xx_pci_irq_unmask(struct irq_data *d)
342 + unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
343 + void __iomem *base = ath79_reset_base;
346 + t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
347 + __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
350 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
353 +static void ar71xx_pci_irq_mask(struct irq_data *d)
355 + unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
356 + void __iomem *base = ath79_reset_base;
359 + t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
360 + __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
363 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
366 +static struct irq_chip ar71xx_pci_irq_chip = {
367 + .name = "AR71XX PCI",
368 + .irq_mask = ar71xx_pci_irq_mask,
369 + .irq_unmask = ar71xx_pci_irq_unmask,
370 + .irq_mask_ack = ar71xx_pci_irq_mask,
373 +static __init void ar71xx_pci_irq_init(void)
375 + void __iomem *base = ath79_reset_base;
378 + __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
379 + __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
381 + BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
383 + for (i = ATH79_PCI_IRQ_BASE;
384 + i < ATH79_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++)
385 + irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
388 + irq_set_chained_handler(ATH79_CPU_IRQ_IP2, ar71xx_pci_irq_handler);
391 +static __init void ar71xx_pci_reset(void)
393 + void __iomem *ddr_base = ath79_ddr_base;
395 + ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
398 + ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
401 + __raw_writel(AR71XX_PCI_WIN0_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN0);
402 + __raw_writel(AR71XX_PCI_WIN1_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN1);
403 + __raw_writel(AR71XX_PCI_WIN2_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN2);
404 + __raw_writel(AR71XX_PCI_WIN3_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN3);
405 + __raw_writel(AR71XX_PCI_WIN4_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN4);
406 + __raw_writel(AR71XX_PCI_WIN5_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN5);
407 + __raw_writel(AR71XX_PCI_WIN6_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN6);
408 + __raw_writel(AR71XX_PCI_WIN7_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN7);
413 +__init int ar71xx_pcibios_init(void)
417 + ar71xx_pcicfg_base = ioremap(AR71XX_PCI_CFG_BASE, AR71XX_PCI_CFG_SIZE);
418 + if (ar71xx_pcicfg_base == NULL)
421 + ar71xx_pci_reset();
423 + /* setup COMMAND register */
424 + t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
425 + | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
426 + ar71xx_pci_local_write(PCI_COMMAND, 4, t);
428 + /* clear bus errors */
429 + ar71xx_pci_check_error(1);
431 + ar71xx_pci_irq_init();
433 + register_pci_controller(&ar71xx_pci_controller);