blob: d0d707de6c6cf7c6d6dc1287adb44c52160d9e76 [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 Juhos908339e2013-02-03 09:58:38 +000012#include <linux/spinlock.h>
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010013#include <linux/irq.h>
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000014#include <linux/pci.h>
Gabor Juhos58d2e9b2013-02-02 11:40:42 +000015#include <linux/module.h>
16#include <linux/platform_device.h>
Gabor Juhos6015a852012-03-14 10:36:05 +010017#include <asm/mach-ath79/ath79.h>
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010018#include <asm/mach-ath79/ar71xx_regs.h>
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000019
Gabor Juhosa1dca312012-08-23 15:35:26 +020020#define AR724X_PCI_REG_RESET 0x18
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010021#define AR724X_PCI_REG_INT_STATUS 0x4c
22#define AR724X_PCI_REG_INT_MASK 0x50
23
Gabor Juhosa1dca312012-08-23 15:35:26 +020024#define AR724X_PCI_RESET_LINK_UP BIT(0)
25
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010026#define AR724X_PCI_INT_DEV0 BIT(14)
27
28#define AR724X_PCI_IRQ_COUNT 1
29
Gabor Juhos6015a852012-03-14 10:36:05 +010030#define AR7240_BAR0_WAR_VALUE 0xffff
31
Gabor Juhos908339e2013-02-03 09:58:38 +000032struct ar724x_pci_controller {
33 void __iomem *devcfg_base;
34 void __iomem *ctrl_base;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000035
Gabor Juhos908339e2013-02-03 09:58:38 +000036 int irq;
Gabor Juhosa1dca312012-08-23 15:35:26 +020037
Gabor Juhos908339e2013-02-03 09:58:38 +000038 bool link_up;
39 bool bar0_is_cached;
40 u32 bar0_value;
41
42 spinlock_t lock;
43
44 struct pci_controller pci_controller;
Gabor Juhos34b134a2013-02-03 09:59:45 +000045 struct resource io_res;
46 struct resource mem_res;
Gabor Juhos908339e2013-02-03 09:58:38 +000047};
48
49static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc)
Gabor Juhosa1dca312012-08-23 15:35:26 +020050{
51 u32 reset;
52
Gabor Juhos908339e2013-02-03 09:58:38 +000053 reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET);
Gabor Juhosa1dca312012-08-23 15:35:26 +020054 return reset & AR724X_PCI_RESET_LINK_UP;
55}
Gabor Juhos6015a852012-03-14 10:36:05 +010056
Gabor Juhos908339e2013-02-03 09:58:38 +000057static inline struct ar724x_pci_controller *
58pci_bus_to_ar724x_controller(struct pci_bus *bus)
59{
60 struct pci_controller *hose;
61
62 hose = (struct pci_controller *) bus->sysdata;
63 return container_of(hose, struct ar724x_pci_controller, pci_controller);
64}
65
Gabor Juhosd624bd32012-03-14 10:29:26 +010066static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000067 int size, uint32_t *value)
68{
Gabor Juhos908339e2013-02-03 09:58:38 +000069 struct ar724x_pci_controller *apc;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010070 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +010071 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010072 u32 data;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000073
Gabor Juhos908339e2013-02-03 09:58:38 +000074 apc = pci_bus_to_ar724x_controller(bus);
75 if (!apc->link_up)
Gabor Juhosa1dca312012-08-23 15:35:26 +020076 return PCIBIOS_DEVICE_NOT_FOUND;
77
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000078 if (devfn)
79 return PCIBIOS_DEVICE_NOT_FOUND;
80
Gabor Juhos908339e2013-02-03 09:58:38 +000081 base = apc->devcfg_base;
Gabor Juhosc1984412012-03-14 10:29:27 +010082
Gabor Juhos908339e2013-02-03 09:58:38 +000083 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +010084 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000085
86 switch (size) {
87 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010088 if (where & 1)
89 data >>= 8;
90 if (where & 2)
91 data >>= 16;
92 data &= 0xff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000093 break;
94 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010095 if (where & 2)
96 data >>= 16;
97 data &= 0xffff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000098 break;
99 case 4:
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000100 break;
101 default:
Gabor Juhos908339e2013-02-03 09:58:38 +0000102 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000103
104 return PCIBIOS_BAD_REGISTER_NUMBER;
105 }
106
Gabor Juhos908339e2013-02-03 09:58:38 +0000107 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhos6015a852012-03-14 10:36:05 +0100108
109 if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
Gabor Juhos908339e2013-02-03 09:58:38 +0000110 apc->bar0_is_cached) {
Gabor Juhos6015a852012-03-14 10:36:05 +0100111 /* use the cached value */
Gabor Juhos908339e2013-02-03 09:58:38 +0000112 *value = apc->bar0_value;
Gabor Juhos6015a852012-03-14 10:36:05 +0100113 } else {
114 *value = data;
115 }
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000116
117 return PCIBIOS_SUCCESSFUL;
118}
119
Gabor Juhosd624bd32012-03-14 10:29:26 +0100120static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000121 int size, uint32_t value)
122{
Gabor Juhos908339e2013-02-03 09:58:38 +0000123 struct ar724x_pci_controller *apc;
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100124 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +0100125 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100126 u32 data;
127 int s;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000128
Gabor Juhos908339e2013-02-03 09:58:38 +0000129 apc = pci_bus_to_ar724x_controller(bus);
130 if (!apc->link_up)
Gabor Juhosa1dca312012-08-23 15:35:26 +0200131 return PCIBIOS_DEVICE_NOT_FOUND;
132
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000133 if (devfn)
134 return PCIBIOS_DEVICE_NOT_FOUND;
135
Gabor Juhos6015a852012-03-14 10:36:05 +0100136 if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
137 if (value != 0xffffffff) {
138 /*
139 * WAR for a hw issue. If the BAR0 register of the
140 * device is set to the proper base address, the
141 * memory space of the device is not accessible.
142 *
143 * Cache the intended value so it can be read back,
144 * and write a SoC specific constant value to the
145 * BAR0 register in order to make the device memory
146 * accessible.
147 */
Gabor Juhos908339e2013-02-03 09:58:38 +0000148 apc->bar0_is_cached = true;
149 apc->bar0_value = value;
Gabor Juhos6015a852012-03-14 10:36:05 +0100150
151 value = AR7240_BAR0_WAR_VALUE;
152 } else {
Gabor Juhos908339e2013-02-03 09:58:38 +0000153 apc->bar0_is_cached = false;
Gabor Juhos6015a852012-03-14 10:36:05 +0100154 }
155 }
156
Gabor Juhos908339e2013-02-03 09:58:38 +0000157 base = apc->devcfg_base;
Gabor Juhosc1984412012-03-14 10:29:27 +0100158
Gabor Juhos908339e2013-02-03 09:58:38 +0000159 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100160 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000161
162 switch (size) {
163 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100164 s = ((where & 3) * 8);
165 data &= ~(0xff << s);
166 data |= ((value & 0xff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000167 break;
168 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100169 s = ((where & 2) * 8);
170 data &= ~(0xffff << s);
171 data |= ((value & 0xffff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000172 break;
173 case 4:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100174 data = value;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000175 break;
176 default:
Gabor Juhos908339e2013-02-03 09:58:38 +0000177 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000178
179 return PCIBIOS_BAD_REGISTER_NUMBER;
180 }
181
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100182 __raw_writel(data, base + (where & ~3));
183 /* flush write */
184 __raw_readl(base + (where & ~3));
Gabor Juhos908339e2013-02-03 09:58:38 +0000185 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000186
187 return PCIBIOS_SUCCESSFUL;
188}
189
Gabor Juhosd624bd32012-03-14 10:29:26 +0100190static struct pci_ops ar724x_pci_ops = {
191 .read = ar724x_pci_read,
192 .write = ar724x_pci_write,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000193};
194
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100195static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000196{
Gabor Juhos908339e2013-02-03 09:58:38 +0000197 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100198 void __iomem *base;
199 u32 pending;
200
Gabor Juhos908339e2013-02-03 09:58:38 +0000201 apc = irq_get_handler_data(irq);
202 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100203
204 pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
205 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
206
207 if (pending & AR724X_PCI_INT_DEV0)
208 generic_handle_irq(ATH79_PCI_IRQ(0));
209
210 else
211 spurious_interrupt();
212}
213
214static void ar724x_pci_irq_unmask(struct irq_data *d)
215{
Gabor Juhos908339e2013-02-03 09:58:38 +0000216 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100217 void __iomem *base;
218 u32 t;
219
Gabor Juhos908339e2013-02-03 09:58:38 +0000220 apc = irq_data_get_irq_chip_data(d);
221 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100222
223 switch (d->irq) {
224 case ATH79_PCI_IRQ(0):
225 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
226 __raw_writel(t | AR724X_PCI_INT_DEV0,
227 base + AR724X_PCI_REG_INT_MASK);
228 /* flush write */
229 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
230 }
231}
232
233static void ar724x_pci_irq_mask(struct irq_data *d)
234{
Gabor Juhos908339e2013-02-03 09:58:38 +0000235 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100236 void __iomem *base;
237 u32 t;
238
Gabor Juhos908339e2013-02-03 09:58:38 +0000239 apc = irq_data_get_irq_chip_data(d);
240 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100241
242 switch (d->irq) {
243 case ATH79_PCI_IRQ(0):
244 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
245 __raw_writel(t & ~AR724X_PCI_INT_DEV0,
246 base + AR724X_PCI_REG_INT_MASK);
247
248 /* flush write */
249 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
250
251 t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
252 __raw_writel(t | AR724X_PCI_INT_DEV0,
253 base + AR724X_PCI_REG_INT_STATUS);
254
255 /* flush write */
256 __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
257 }
258}
259
260static struct irq_chip ar724x_pci_irq_chip = {
261 .name = "AR724X PCI ",
262 .irq_mask = ar724x_pci_irq_mask,
263 .irq_unmask = ar724x_pci_irq_unmask,
264 .irq_mask_ack = ar724x_pci_irq_mask,
265};
266
Gabor Juhos908339e2013-02-03 09:58:38 +0000267static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc)
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100268{
269 void __iomem *base;
270 int i;
271
Gabor Juhos908339e2013-02-03 09:58:38 +0000272 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100273
274 __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
275 __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
276
277 BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
278
279 for (i = ATH79_PCI_IRQ_BASE;
Gabor Juhos908339e2013-02-03 09:58:38 +0000280 i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++) {
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100281 irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
282 handle_level_irq);
Gabor Juhos908339e2013-02-03 09:58:38 +0000283 irq_set_chip_data(i, apc);
284 }
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100285
Gabor Juhos908339e2013-02-03 09:58:38 +0000286 irq_set_handler_data(apc->irq, apc);
287 irq_set_chained_handler(apc->irq, ar724x_pci_irq_handler);
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100288}
289
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000290static int ar724x_pci_probe(struct platform_device *pdev)
291{
Gabor Juhos908339e2013-02-03 09:58:38 +0000292 struct ar724x_pci_controller *apc;
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000293 struct resource *res;
Gabor Juhos908339e2013-02-03 09:58:38 +0000294
295 apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller),
296 GFP_KERNEL);
297 if (!apc)
298 return -ENOMEM;
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000299
300 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base");
301 if (!res)
302 return -EINVAL;
303
Gabor Juhos908339e2013-02-03 09:58:38 +0000304 apc->ctrl_base = devm_request_and_ioremap(&pdev->dev, res);
305 if (apc->ctrl_base == NULL)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000306 return -EBUSY;
307
308 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
309 if (!res)
310 return -EINVAL;
311
Gabor Juhos908339e2013-02-03 09:58:38 +0000312 apc->devcfg_base = devm_request_and_ioremap(&pdev->dev, res);
313 if (!apc->devcfg_base)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000314 return -EBUSY;
315
Gabor Juhos908339e2013-02-03 09:58:38 +0000316 apc->irq = platform_get_irq(pdev, 0);
317 if (apc->irq < 0)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000318 return -EINVAL;
319
Gabor Juhos908339e2013-02-03 09:58:38 +0000320 spin_lock_init(&apc->lock);
321
Gabor Juhos34b134a2013-02-03 09:59:45 +0000322 res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base");
323 if (!res)
324 return -EINVAL;
325
326 apc->io_res.parent = res;
327 apc->io_res.name = "PCI IO space";
328 apc->io_res.start = res->start;
329 apc->io_res.end = res->end;
330 apc->io_res.flags = IORESOURCE_IO;
331
332 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base");
333 if (!res)
334 return -EINVAL;
335
336 apc->mem_res.parent = res;
337 apc->mem_res.name = "PCI memory space";
338 apc->mem_res.start = res->start;
339 apc->mem_res.end = res->end;
340 apc->mem_res.flags = IORESOURCE_MEM;
341
Gabor Juhos908339e2013-02-03 09:58:38 +0000342 apc->pci_controller.pci_ops = &ar724x_pci_ops;
Gabor Juhos34b134a2013-02-03 09:59:45 +0000343 apc->pci_controller.io_resource = &apc->io_res;
344 apc->pci_controller.mem_resource = &apc->mem_res;
Gabor Juhos908339e2013-02-03 09:58:38 +0000345
346 apc->link_up = ar724x_pci_check_link(apc);
347 if (!apc->link_up)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000348 dev_warn(&pdev->dev, "PCIe link is down\n");
349
Gabor Juhos908339e2013-02-03 09:58:38 +0000350 ar724x_pci_irq_init(apc);
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000351
Gabor Juhos908339e2013-02-03 09:58:38 +0000352 register_pci_controller(&apc->pci_controller);
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000353
354 return 0;
355}
356
357static struct platform_driver ar724x_pci_driver = {
358 .probe = ar724x_pci_probe,
359 .driver = {
360 .name = "ar724x-pci",
361 .owner = THIS_MODULE,
362 },
363};
364
365static int __init ar724x_pci_init(void)
366{
367 return platform_driver_register(&ar724x_pci_driver);
368}
369
370postcore_initcall(ar724x_pci_init);