blob: 414a7459858d4e06b837a08d375eccbc96804dae [file] [log] [blame]
Rene Bolldorf4ff40d52011-11-17 14:25:09 +00001/*
Gabor Juhose9b62e82012-03-14 10:36:14 +01002 * Atheros AR724X PCI host controller driver
Rene Bolldorf4ff40d52011-11-17 14:25:09 +00003 *
4 * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
Gabor Juhose9b62e82012-03-14 10:36:14 +01005 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
Rene Bolldorf4ff40d52011-11-17 14:25:09 +00006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 */
11
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010012#include <linux/irq.h>
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000013#include <linux/pci.h>
Gabor Juhos6015a852012-03-14 10:36:05 +010014#include <asm/mach-ath79/ath79.h>
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010015#include <asm/mach-ath79/ar71xx_regs.h>
Gabor Juhos659243c2012-03-14 10:29:23 +010016#include <asm/mach-ath79/pci.h>
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000017
Gabor Juhosc1984412012-03-14 10:29:27 +010018#define AR724X_PCI_CFG_BASE 0x14000000
19#define AR724X_PCI_CFG_SIZE 0x1000
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010020#define AR724X_PCI_CTRL_BASE (AR71XX_APB_BASE + 0x000f0000)
21#define AR724X_PCI_CTRL_SIZE 0x100
22
Gabor Juhosd624bd32012-03-14 10:29:26 +010023#define AR724X_PCI_MEM_BASE 0x10000000
24#define AR724X_PCI_MEM_SIZE 0x08000000
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000025
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010026#define AR724X_PCI_REG_INT_STATUS 0x4c
27#define AR724X_PCI_REG_INT_MASK 0x50
28
29#define AR724X_PCI_INT_DEV0 BIT(14)
30
31#define AR724X_PCI_IRQ_COUNT 1
32
Gabor Juhos6015a852012-03-14 10:36:05 +010033#define AR7240_BAR0_WAR_VALUE 0xffff
34
Gabor Juhosd624bd32012-03-14 10:29:26 +010035static DEFINE_SPINLOCK(ar724x_pci_lock);
Gabor Juhosc1984412012-03-14 10:29:27 +010036static void __iomem *ar724x_pci_devcfg_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010037static void __iomem *ar724x_pci_ctrl_base;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000038
Gabor Juhos6015a852012-03-14 10:36:05 +010039static u32 ar724x_pci_bar0_value;
40static bool ar724x_pci_bar0_is_cached;
41
Gabor Juhosd624bd32012-03-14 10:29:26 +010042static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000043 int size, uint32_t *value)
44{
Gabor Juhos64adb6b2012-03-14 10:36:04 +010045 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +010046 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010047 u32 data;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000048
49 if (devfn)
50 return PCIBIOS_DEVICE_NOT_FOUND;
51
Gabor Juhosc1984412012-03-14 10:29:27 +010052 base = ar724x_pci_devcfg_base;
53
Gabor Juhosd624bd32012-03-14 10:29:26 +010054 spin_lock_irqsave(&ar724x_pci_lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +010055 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000056
57 switch (size) {
58 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010059 if (where & 1)
60 data >>= 8;
61 if (where & 2)
62 data >>= 16;
63 data &= 0xff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000064 break;
65 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010066 if (where & 2)
67 data >>= 16;
68 data &= 0xffff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000069 break;
70 case 4:
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000071 break;
72 default:
Gabor Juhosd624bd32012-03-14 10:29:26 +010073 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000074
75 return PCIBIOS_BAD_REGISTER_NUMBER;
76 }
77
Gabor Juhosd624bd32012-03-14 10:29:26 +010078 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
Gabor Juhos6015a852012-03-14 10:36:05 +010079
80 if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
81 ar724x_pci_bar0_is_cached) {
82 /* use the cached value */
83 *value = ar724x_pci_bar0_value;
84 } else {
85 *value = data;
86 }
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000087
88 return PCIBIOS_SUCCESSFUL;
89}
90
Gabor Juhosd624bd32012-03-14 10:29:26 +010091static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000092 int size, uint32_t value)
93{
Gabor Juhos64adb6b2012-03-14 10:36:04 +010094 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +010095 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010096 u32 data;
97 int s;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000098
99 if (devfn)
100 return PCIBIOS_DEVICE_NOT_FOUND;
101
Gabor Juhos6015a852012-03-14 10:36:05 +0100102 if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
103 if (value != 0xffffffff) {
104 /*
105 * WAR for a hw issue. If the BAR0 register of the
106 * device is set to the proper base address, the
107 * memory space of the device is not accessible.
108 *
109 * Cache the intended value so it can be read back,
110 * and write a SoC specific constant value to the
111 * BAR0 register in order to make the device memory
112 * accessible.
113 */
114 ar724x_pci_bar0_is_cached = true;
115 ar724x_pci_bar0_value = value;
116
117 value = AR7240_BAR0_WAR_VALUE;
118 } else {
119 ar724x_pci_bar0_is_cached = false;
120 }
121 }
122
Gabor Juhosc1984412012-03-14 10:29:27 +0100123 base = ar724x_pci_devcfg_base;
124
Gabor Juhosd624bd32012-03-14 10:29:26 +0100125 spin_lock_irqsave(&ar724x_pci_lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100126 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000127
128 switch (size) {
129 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100130 s = ((where & 3) * 8);
131 data &= ~(0xff << s);
132 data |= ((value & 0xff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000133 break;
134 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100135 s = ((where & 2) * 8);
136 data &= ~(0xffff << s);
137 data |= ((value & 0xffff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000138 break;
139 case 4:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100140 data = value;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000141 break;
142 default:
Gabor Juhosd624bd32012-03-14 10:29:26 +0100143 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000144
145 return PCIBIOS_BAD_REGISTER_NUMBER;
146 }
147
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100148 __raw_writel(data, base + (where & ~3));
149 /* flush write */
150 __raw_readl(base + (where & ~3));
Gabor Juhosd624bd32012-03-14 10:29:26 +0100151 spin_unlock_irqrestore(&ar724x_pci_lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000152
153 return PCIBIOS_SUCCESSFUL;
154}
155
Gabor Juhosd624bd32012-03-14 10:29:26 +0100156static struct pci_ops ar724x_pci_ops = {
157 .read = ar724x_pci_read,
158 .write = ar724x_pci_write,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000159};
160
Gabor Juhosd624bd32012-03-14 10:29:26 +0100161static struct resource ar724x_io_resource = {
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000162 .name = "PCI IO space",
163 .start = 0,
164 .end = 0,
165 .flags = IORESOURCE_IO,
166};
167
Gabor Juhosd624bd32012-03-14 10:29:26 +0100168static struct resource ar724x_mem_resource = {
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000169 .name = "PCI memory space",
Gabor Juhosd624bd32012-03-14 10:29:26 +0100170 .start = AR724X_PCI_MEM_BASE,
171 .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000172 .flags = IORESOURCE_MEM,
173};
174
Gabor Juhosd624bd32012-03-14 10:29:26 +0100175static struct pci_controller ar724x_pci_controller = {
176 .pci_ops = &ar724x_pci_ops,
177 .io_resource = &ar724x_io_resource,
178 .mem_resource = &ar724x_mem_resource,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000179};
180
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100181static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000182{
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100183 void __iomem *base;
184 u32 pending;
185
186 base = ar724x_pci_ctrl_base;
187
188 pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
189 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
190
191 if (pending & AR724X_PCI_INT_DEV0)
192 generic_handle_irq(ATH79_PCI_IRQ(0));
193
194 else
195 spurious_interrupt();
196}
197
198static void ar724x_pci_irq_unmask(struct irq_data *d)
199{
200 void __iomem *base;
201 u32 t;
202
203 base = ar724x_pci_ctrl_base;
204
205 switch (d->irq) {
206 case ATH79_PCI_IRQ(0):
207 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
208 __raw_writel(t | AR724X_PCI_INT_DEV0,
209 base + AR724X_PCI_REG_INT_MASK);
210 /* flush write */
211 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
212 }
213}
214
215static void ar724x_pci_irq_mask(struct irq_data *d)
216{
217 void __iomem *base;
218 u32 t;
219
220 base = ar724x_pci_ctrl_base;
221
222 switch (d->irq) {
223 case ATH79_PCI_IRQ(0):
224 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
225 __raw_writel(t & ~AR724X_PCI_INT_DEV0,
226 base + AR724X_PCI_REG_INT_MASK);
227
228 /* flush write */
229 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
230
231 t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
232 __raw_writel(t | AR724X_PCI_INT_DEV0,
233 base + AR724X_PCI_REG_INT_STATUS);
234
235 /* flush write */
236 __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
237 }
238}
239
240static struct irq_chip ar724x_pci_irq_chip = {
241 .name = "AR724X PCI ",
242 .irq_mask = ar724x_pci_irq_mask,
243 .irq_unmask = ar724x_pci_irq_unmask,
244 .irq_mask_ack = ar724x_pci_irq_mask,
245};
246
247static void __init ar724x_pci_irq_init(int irq)
248{
249 void __iomem *base;
250 int i;
251
252 base = ar724x_pci_ctrl_base;
253
254 __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
255 __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
256
257 BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
258
259 for (i = ATH79_PCI_IRQ_BASE;
260 i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++)
261 irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
262 handle_level_irq);
263
264 irq_set_chained_handler(irq, ar724x_pci_irq_handler);
265}
266
267int __init ar724x_pcibios_init(int irq)
268{
269 int ret;
270
271 ret = -ENOMEM;
272
Gabor Juhosc1984412012-03-14 10:29:27 +0100273 ar724x_pci_devcfg_base = ioremap(AR724X_PCI_CFG_BASE,
274 AR724X_PCI_CFG_SIZE);
275 if (ar724x_pci_devcfg_base == NULL)
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100276 goto err;
Gabor Juhosc1984412012-03-14 10:29:27 +0100277
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100278 ar724x_pci_ctrl_base = ioremap(AR724X_PCI_CTRL_BASE,
279 AR724X_PCI_CTRL_SIZE);
280 if (ar724x_pci_ctrl_base == NULL)
281 goto err_unmap_devcfg;
282
283 ar724x_pci_irq_init(irq);
Gabor Juhosd624bd32012-03-14 10:29:26 +0100284 register_pci_controller(&ar724x_pci_controller);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000285
286 return PCIBIOS_SUCCESSFUL;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100287
288err_unmap_devcfg:
289 iounmap(ar724x_pci_devcfg_base);
290err:
291 return ret;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000292}