1 From 9f6d46372cf2a493eaeeffbefe0a796379f838fa Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg@openwrt.org>
3 Date: Tue, 22 Nov 2011 22:54:32 +0100
4 Subject: [PATCH 16/35] 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>
14 v2: - add missing pci-ar71xx.c
16 arch/mips/ath79/Kconfig | 1 +
17 arch/mips/include/asm/mach-ath79/pci.h | 6 +
18 arch/mips/pci/Makefile | 1 +
19 arch/mips/pci/pci-ar71xx.c | 375 ++++++++++++++++++++++++++++++++
20 4 files changed, 383 insertions(+), 0 deletions(-)
21 create mode 100644 arch/mips/pci/pci-ar71xx.c
23 --- a/arch/mips/ath79/Kconfig
24 +++ b/arch/mips/ath79/Kconfig
25 @@ -52,6 +52,7 @@ endmenu
27 select USB_ARCH_HAS_EHCI
28 select USB_ARCH_HAS_OHCI
33 --- a/arch/mips/include/asm/mach-ath79/pci.h
34 +++ b/arch/mips/include/asm/mach-ath79/pci.h
36 #ifndef __ASM_MACH_ATH79_PCI_H
37 #define __ASM_MACH_ATH79_PCI_H
39 +#if defined(CONFIG_PCI) && defined(CONFIG_SOC_AR71XX)
40 +int ar71xx_pcibios_init(void);
42 +static inline int ar71xx_pcibios_init(void) { return 0 };
45 #if defined(CONFIG_PCI) && defined(CONFIG_SOC_AR724X)
46 int ar724x_pcibios_init(int irq);
48 --- a/arch/mips/pci/Makefile
49 +++ b/arch/mips/pci/Makefile
50 @@ -19,6 +19,7 @@ obj-$(CONFIG_BCM47XX) += pci-bcm47xx.o
51 obj-$(CONFIG_BCM63XX) += pci-bcm63xx.o fixup-bcm63xx.o \
53 obj-$(CONFIG_MIPS_ALCHEMY) += pci-alchemy.o
54 +obj-$(CONFIG_SOC_AR71XX) += pci-ar71xx.o
55 obj-$(CONFIG_SOC_AR724X) += pci-ar724x.o
59 +++ b/arch/mips/pci/pci-ar71xx.c
62 + * Atheros AR71xx PCI host controller driver
64 + * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
65 + * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
67 + * Parts of this file are based on Atheros' 2.6.15 BSP
69 + * This program is free software; you can redistribute it and/or modify it
70 + * under the terms of the GNU General Public License version 2 as published
71 + * by the Free Software Foundation.
74 +#include <linux/resource.h>
75 +#include <linux/types.h>
76 +#include <linux/delay.h>
77 +#include <linux/bitops.h>
78 +#include <linux/pci.h>
79 +#include <linux/pci_regs.h>
80 +#include <linux/interrupt.h>
82 +#include <asm/mach-ath79/ar71xx_regs.h>
83 +#include <asm/mach-ath79/ath79.h>
84 +#include <asm/mach-ath79/pci.h>
86 +#define AR71XX_PCI_MEM_BASE 0x10000000
87 +#define AR71XX_PCI_MEM_SIZE 0x08000000
89 +#define AR71XX_PCI_WIN0_OFFS 0x10000000
90 +#define AR71XX_PCI_WIN1_OFFS 0x11000000
91 +#define AR71XX_PCI_WIN2_OFFS 0x12000000
92 +#define AR71XX_PCI_WIN3_OFFS 0x13000000
93 +#define AR71XX_PCI_WIN4_OFFS 0x14000000
94 +#define AR71XX_PCI_WIN5_OFFS 0x15000000
95 +#define AR71XX_PCI_WIN6_OFFS 0x16000000
96 +#define AR71XX_PCI_WIN7_OFFS 0x07000000
98 +#define AR71XX_PCI_CFG_BASE \
99 + (AR71XX_PCI_MEM_BASE + AR71XX_PCI_WIN7_OFFS + 0x10000)
100 +#define AR71XX_PCI_CFG_SIZE 0x100
102 +#define AR71XX_PCI_REG_CRP_AD_CBE 0x00
103 +#define AR71XX_PCI_REG_CRP_WRDATA 0x04
104 +#define AR71XX_PCI_REG_CRP_RDDATA 0x08
105 +#define AR71XX_PCI_REG_CFG_AD 0x0c
106 +#define AR71XX_PCI_REG_CFG_CBE 0x10
107 +#define AR71XX_PCI_REG_CFG_WRDATA 0x14
108 +#define AR71XX_PCI_REG_CFG_RDDATA 0x18
109 +#define AR71XX_PCI_REG_PCI_ERR 0x1c
110 +#define AR71XX_PCI_REG_PCI_ERR_ADDR 0x20
111 +#define AR71XX_PCI_REG_AHB_ERR 0x24
112 +#define AR71XX_PCI_REG_AHB_ERR_ADDR 0x28
114 +#define AR71XX_PCI_CRP_CMD_WRITE 0x00010000
115 +#define AR71XX_PCI_CRP_CMD_READ 0x00000000
116 +#define AR71XX_PCI_CFG_CMD_READ 0x0000000a
117 +#define AR71XX_PCI_CFG_CMD_WRITE 0x0000000b
119 +#define AR71XX_PCI_INT_CORE BIT(4)
120 +#define AR71XX_PCI_INT_DEV2 BIT(2)
121 +#define AR71XX_PCI_INT_DEV1 BIT(1)
122 +#define AR71XX_PCI_INT_DEV0 BIT(0)
124 +#define AR71XX_PCI_IRQ_COUNT 5
126 +static DEFINE_SPINLOCK(ar71xx_pci_lock);
127 +static void __iomem *ar71xx_pcicfg_base;
129 +/* Byte lane enable bits */
130 +static const u8 ar71xx_pci_ble_table[4][4] = {
131 + {0x0, 0xf, 0xf, 0xf},
132 + {0xe, 0xd, 0xb, 0x7},
133 + {0xc, 0xf, 0x3, 0xf},
134 + {0xf, 0xf, 0xf, 0xf},
137 +static const u32 ar71xx_pci_read_mask[8] = {
138 + 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
141 +static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
145 + t = ar71xx_pci_ble_table[size & 3][where & 3];
147 + t <<= (local) ? 20 : 4;
152 +static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
157 + if (!bus->number) {
159 + ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
163 + ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
164 + (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
170 +static int ar71xx_pci_check_error(int quiet)
172 + void __iomem *base = ar71xx_pcicfg_base;
176 + pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
181 + addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
182 + pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
183 + "PCI", pci_err, addr);
186 + /* clear PCI error status */
187 + __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
190 + ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
195 + addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
196 + pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
197 + "AHB", ahb_err, addr);
200 + /* clear AHB error status */
201 + __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
204 + return !!(ahb_err | pci_err);
207 +static inline void ar71xx_pci_local_write(int where, int size, u32 value)
209 + void __iomem *base = ar71xx_pcicfg_base;
212 + value = value << (8 * (where & 3));
214 + ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
215 + ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
217 + __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
218 + __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
221 +static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
222 + unsigned int devfn,
223 + int where, int size, u32 cmd)
225 + void __iomem *base = ar71xx_pcicfg_base;
228 + addr = ar71xx_pci_bus_addr(bus, devfn, where);
230 + __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
231 + __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
232 + base + AR71XX_PCI_REG_CFG_CBE);
234 + return ar71xx_pci_check_error(1);
237 +static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
238 + int where, int size, u32 *value)
240 + void __iomem *base = ar71xx_pcicfg_base;
241 + unsigned long flags;
246 + ret = PCIBIOS_SUCCESSFUL;
249 + spin_lock_irqsave(&ar71xx_pci_lock, flags);
251 + err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
252 + AR71XX_PCI_CFG_CMD_READ);
254 + ret = PCIBIOS_DEVICE_NOT_FOUND;
256 + data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
258 + spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
260 + *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
265 +static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
266 + int where, int size, u32 value)
268 + void __iomem *base = ar71xx_pcicfg_base;
269 + unsigned long flags;
273 + value = value << (8 * (where & 3));
274 + ret = PCIBIOS_SUCCESSFUL;
276 + spin_lock_irqsave(&ar71xx_pci_lock, flags);
278 + err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
279 + AR71XX_PCI_CFG_CMD_WRITE);
281 + ret = PCIBIOS_DEVICE_NOT_FOUND;
283 + __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
285 + spin_unlock_irqrestore(&ar71xx_pci_lock, flags);
290 +static struct pci_ops ar71xx_pci_ops = {
291 + .read = ar71xx_pci_read_config,
292 + .write = ar71xx_pci_write_config,
295 +static struct resource ar71xx_pci_io_resource = {
296 + .name = "PCI IO space",
299 + .flags = IORESOURCE_IO,
302 +static struct resource ar71xx_pci_mem_resource = {
303 + .name = "PCI memory space",
304 + .start = AR71XX_PCI_MEM_BASE,
305 + .end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1,
306 + .flags = IORESOURCE_MEM
309 +static struct pci_controller ar71xx_pci_controller = {
310 + .pci_ops = &ar71xx_pci_ops,
311 + .mem_resource = &ar71xx_pci_mem_resource,
312 + .io_resource = &ar71xx_pci_io_resource,
315 +static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
317 + void __iomem *base = ath79_reset_base;
320 + pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
321 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
323 + if (pending & AR71XX_PCI_INT_DEV0)
324 + generic_handle_irq(ATH79_PCI_IRQ(0));
326 + else if (pending & AR71XX_PCI_INT_DEV1)
327 + generic_handle_irq(ATH79_PCI_IRQ(1));
329 + else if (pending & AR71XX_PCI_INT_DEV2)
330 + generic_handle_irq(ATH79_PCI_IRQ(2));
332 + else if (pending & AR71XX_PCI_INT_CORE)
333 + generic_handle_irq(ATH79_PCI_IRQ(4));
336 + spurious_interrupt();
339 +static void ar71xx_pci_irq_unmask(struct irq_data *d)
341 + unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
342 + void __iomem *base = ath79_reset_base;
345 + t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
346 + __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
349 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
352 +static void ar71xx_pci_irq_mask(struct irq_data *d)
354 + unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
355 + void __iomem *base = ath79_reset_base;
358 + t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
359 + __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
362 + __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
365 +static struct irq_chip ar71xx_pci_irq_chip = {
366 + .name = "AR71XX PCI",
367 + .irq_mask = ar71xx_pci_irq_mask,
368 + .irq_unmask = ar71xx_pci_irq_unmask,
369 + .irq_mask_ack = ar71xx_pci_irq_mask,
372 +static __init void ar71xx_pci_irq_init(void)
374 + void __iomem *base = ath79_reset_base;
377 + __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
378 + __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
380 + BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
382 + for (i = ATH79_PCI_IRQ_BASE;
383 + i < ATH79_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++)
384 + irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
387 + irq_set_chained_handler(ATH79_CPU_IRQ_IP2, ar71xx_pci_irq_handler);
390 +static __init void ar71xx_pci_reset(void)
392 + void __iomem *ddr_base = ath79_ddr_base;
394 + ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
397 + ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
400 + __raw_writel(AR71XX_PCI_WIN0_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN0);
401 + __raw_writel(AR71XX_PCI_WIN1_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN1);
402 + __raw_writel(AR71XX_PCI_WIN2_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN2);
403 + __raw_writel(AR71XX_PCI_WIN3_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN3);
404 + __raw_writel(AR71XX_PCI_WIN4_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN4);
405 + __raw_writel(AR71XX_PCI_WIN5_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN5);
406 + __raw_writel(AR71XX_PCI_WIN6_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN6);
407 + __raw_writel(AR71XX_PCI_WIN7_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN7);
412 +__init int ar71xx_pcibios_init(void)
416 + ar71xx_pcicfg_base = ioremap(AR71XX_PCI_CFG_BASE, AR71XX_PCI_CFG_SIZE);
417 + if (ar71xx_pcicfg_base == NULL)
420 + ar71xx_pci_reset();
422 + /* setup COMMAND register */
423 + t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
424 + | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
425 + ar71xx_pci_local_write(PCI_COMMAND, 4, t);
427 + /* clear bus errors */
428 + ar71xx_pci_check_error(1);
430 + ar71xx_pci_irq_init();
432 + register_pci_controller(&ar71xx_pci_controller);