blob: 93ab8778ce1050a677e1bdb980a0985fb32c358b [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;
45};
46
47static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc)
Gabor Juhosa1dca312012-08-23 15:35:26 +020048{
49 u32 reset;
50
Gabor Juhos908339e2013-02-03 09:58:38 +000051 reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET);
Gabor Juhosa1dca312012-08-23 15:35:26 +020052 return reset & AR724X_PCI_RESET_LINK_UP;
53}
Gabor Juhos6015a852012-03-14 10:36:05 +010054
Gabor Juhos908339e2013-02-03 09:58:38 +000055static inline struct ar724x_pci_controller *
56pci_bus_to_ar724x_controller(struct pci_bus *bus)
57{
58 struct pci_controller *hose;
59
60 hose = (struct pci_controller *) bus->sysdata;
61 return container_of(hose, struct ar724x_pci_controller, pci_controller);
62}
63
Gabor Juhosd624bd32012-03-14 10:29:26 +010064static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000065 int size, uint32_t *value)
66{
Gabor Juhos908339e2013-02-03 09:58:38 +000067 struct ar724x_pci_controller *apc;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010068 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +010069 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +010070 u32 data;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000071
Gabor Juhos908339e2013-02-03 09:58:38 +000072 apc = pci_bus_to_ar724x_controller(bus);
73 if (!apc->link_up)
Gabor Juhosa1dca312012-08-23 15:35:26 +020074 return PCIBIOS_DEVICE_NOT_FOUND;
75
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000076 if (devfn)
77 return PCIBIOS_DEVICE_NOT_FOUND;
78
Gabor Juhos908339e2013-02-03 09:58:38 +000079 base = apc->devcfg_base;
Gabor Juhosc1984412012-03-14 10:29:27 +010080
Gabor Juhos908339e2013-02-03 09:58:38 +000081 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +010082 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000083
84 switch (size) {
85 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010086 if (where & 1)
87 data >>= 8;
88 if (where & 2)
89 data >>= 16;
90 data &= 0xff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000091 break;
92 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +010093 if (where & 2)
94 data >>= 16;
95 data &= 0xffff;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000096 break;
97 case 4:
Rene Bolldorf4ff40d52011-11-17 14:25:09 +000098 break;
99 default:
Gabor Juhos908339e2013-02-03 09:58:38 +0000100 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000101
102 return PCIBIOS_BAD_REGISTER_NUMBER;
103 }
104
Gabor Juhos908339e2013-02-03 09:58:38 +0000105 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhos6015a852012-03-14 10:36:05 +0100106
107 if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
Gabor Juhos908339e2013-02-03 09:58:38 +0000108 apc->bar0_is_cached) {
Gabor Juhos6015a852012-03-14 10:36:05 +0100109 /* use the cached value */
Gabor Juhos908339e2013-02-03 09:58:38 +0000110 *value = apc->bar0_value;
Gabor Juhos6015a852012-03-14 10:36:05 +0100111 } else {
112 *value = data;
113 }
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000114
115 return PCIBIOS_SUCCESSFUL;
116}
117
Gabor Juhosd624bd32012-03-14 10:29:26 +0100118static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000119 int size, uint32_t value)
120{
Gabor Juhos908339e2013-02-03 09:58:38 +0000121 struct ar724x_pci_controller *apc;
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100122 unsigned long flags;
Gabor Juhosc1984412012-03-14 10:29:27 +0100123 void __iomem *base;
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100124 u32 data;
125 int s;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000126
Gabor Juhos908339e2013-02-03 09:58:38 +0000127 apc = pci_bus_to_ar724x_controller(bus);
128 if (!apc->link_up)
Gabor Juhosa1dca312012-08-23 15:35:26 +0200129 return PCIBIOS_DEVICE_NOT_FOUND;
130
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000131 if (devfn)
132 return PCIBIOS_DEVICE_NOT_FOUND;
133
Gabor Juhos6015a852012-03-14 10:36:05 +0100134 if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
135 if (value != 0xffffffff) {
136 /*
137 * WAR for a hw issue. If the BAR0 register of the
138 * device is set to the proper base address, the
139 * memory space of the device is not accessible.
140 *
141 * Cache the intended value so it can be read back,
142 * and write a SoC specific constant value to the
143 * BAR0 register in order to make the device memory
144 * accessible.
145 */
Gabor Juhos908339e2013-02-03 09:58:38 +0000146 apc->bar0_is_cached = true;
147 apc->bar0_value = value;
Gabor Juhos6015a852012-03-14 10:36:05 +0100148
149 value = AR7240_BAR0_WAR_VALUE;
150 } else {
Gabor Juhos908339e2013-02-03 09:58:38 +0000151 apc->bar0_is_cached = false;
Gabor Juhos6015a852012-03-14 10:36:05 +0100152 }
153 }
154
Gabor Juhos908339e2013-02-03 09:58:38 +0000155 base = apc->devcfg_base;
Gabor Juhosc1984412012-03-14 10:29:27 +0100156
Gabor Juhos908339e2013-02-03 09:58:38 +0000157 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100158 data = __raw_readl(base + (where & ~3));
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000159
160 switch (size) {
161 case 1:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100162 s = ((where & 3) * 8);
163 data &= ~(0xff << s);
164 data |= ((value & 0xff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000165 break;
166 case 2:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100167 s = ((where & 2) * 8);
168 data &= ~(0xffff << s);
169 data |= ((value & 0xffff) << s);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000170 break;
171 case 4:
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100172 data = value;
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000173 break;
174 default:
Gabor Juhos908339e2013-02-03 09:58:38 +0000175 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000176
177 return PCIBIOS_BAD_REGISTER_NUMBER;
178 }
179
Gabor Juhos64adb6b2012-03-14 10:36:04 +0100180 __raw_writel(data, base + (where & ~3));
181 /* flush write */
182 __raw_readl(base + (where & ~3));
Gabor Juhos908339e2013-02-03 09:58:38 +0000183 spin_unlock_irqrestore(&apc->lock, flags);
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000184
185 return PCIBIOS_SUCCESSFUL;
186}
187
Gabor Juhosd624bd32012-03-14 10:29:26 +0100188static struct pci_ops ar724x_pci_ops = {
189 .read = ar724x_pci_read,
190 .write = ar724x_pci_write,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000191};
192
Gabor Juhosd624bd32012-03-14 10:29:26 +0100193static struct resource ar724x_io_resource = {
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000194 .name = "PCI IO space",
195 .start = 0,
196 .end = 0,
197 .flags = IORESOURCE_IO,
198};
199
Gabor Juhosd624bd32012-03-14 10:29:26 +0100200static struct resource ar724x_mem_resource = {
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000201 .name = "PCI memory space",
Gabor Juhosd624bd32012-03-14 10:29:26 +0100202 .start = AR724X_PCI_MEM_BASE,
203 .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000204 .flags = IORESOURCE_MEM,
205};
206
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100207static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
Rene Bolldorf4ff40d52011-11-17 14:25:09 +0000208{
Gabor Juhos908339e2013-02-03 09:58:38 +0000209 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100210 void __iomem *base;
211 u32 pending;
212
Gabor Juhos908339e2013-02-03 09:58:38 +0000213 apc = irq_get_handler_data(irq);
214 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100215
216 pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
217 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
218
219 if (pending & AR724X_PCI_INT_DEV0)
220 generic_handle_irq(ATH79_PCI_IRQ(0));
221
222 else
223 spurious_interrupt();
224}
225
226static void ar724x_pci_irq_unmask(struct irq_data *d)
227{
Gabor Juhos908339e2013-02-03 09:58:38 +0000228 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100229 void __iomem *base;
230 u32 t;
231
Gabor Juhos908339e2013-02-03 09:58:38 +0000232 apc = irq_data_get_irq_chip_data(d);
233 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100234
235 switch (d->irq) {
236 case ATH79_PCI_IRQ(0):
237 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
238 __raw_writel(t | AR724X_PCI_INT_DEV0,
239 base + AR724X_PCI_REG_INT_MASK);
240 /* flush write */
241 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
242 }
243}
244
245static void ar724x_pci_irq_mask(struct irq_data *d)
246{
Gabor Juhos908339e2013-02-03 09:58:38 +0000247 struct ar724x_pci_controller *apc;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100248 void __iomem *base;
249 u32 t;
250
Gabor Juhos908339e2013-02-03 09:58:38 +0000251 apc = irq_data_get_irq_chip_data(d);
252 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100253
254 switch (d->irq) {
255 case ATH79_PCI_IRQ(0):
256 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
257 __raw_writel(t & ~AR724X_PCI_INT_DEV0,
258 base + AR724X_PCI_REG_INT_MASK);
259
260 /* flush write */
261 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
262
263 t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
264 __raw_writel(t | AR724X_PCI_INT_DEV0,
265 base + AR724X_PCI_REG_INT_STATUS);
266
267 /* flush write */
268 __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
269 }
270}
271
272static struct irq_chip ar724x_pci_irq_chip = {
273 .name = "AR724X PCI ",
274 .irq_mask = ar724x_pci_irq_mask,
275 .irq_unmask = ar724x_pci_irq_unmask,
276 .irq_mask_ack = ar724x_pci_irq_mask,
277};
278
Gabor Juhos908339e2013-02-03 09:58:38 +0000279static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc)
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100280{
281 void __iomem *base;
282 int i;
283
Gabor Juhos908339e2013-02-03 09:58:38 +0000284 base = apc->ctrl_base;
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100285
286 __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
287 __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
288
289 BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT);
290
291 for (i = ATH79_PCI_IRQ_BASE;
Gabor Juhos908339e2013-02-03 09:58:38 +0000292 i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++) {
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100293 irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
294 handle_level_irq);
Gabor Juhos908339e2013-02-03 09:58:38 +0000295 irq_set_chip_data(i, apc);
296 }
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100297
Gabor Juhos908339e2013-02-03 09:58:38 +0000298 irq_set_handler_data(apc->irq, apc);
299 irq_set_chained_handler(apc->irq, ar724x_pci_irq_handler);
Gabor Juhos4c07c7d2012-03-14 10:36:07 +0100300}
301
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000302static int ar724x_pci_probe(struct platform_device *pdev)
303{
Gabor Juhos908339e2013-02-03 09:58:38 +0000304 struct ar724x_pci_controller *apc;
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000305 struct resource *res;
Gabor Juhos908339e2013-02-03 09:58:38 +0000306
307 apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller),
308 GFP_KERNEL);
309 if (!apc)
310 return -ENOMEM;
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000311
312 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base");
313 if (!res)
314 return -EINVAL;
315
Gabor Juhos908339e2013-02-03 09:58:38 +0000316 apc->ctrl_base = devm_request_and_ioremap(&pdev->dev, res);
317 if (apc->ctrl_base == NULL)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000318 return -EBUSY;
319
320 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
321 if (!res)
322 return -EINVAL;
323
Gabor Juhos908339e2013-02-03 09:58:38 +0000324 apc->devcfg_base = devm_request_and_ioremap(&pdev->dev, res);
325 if (!apc->devcfg_base)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000326 return -EBUSY;
327
Gabor Juhos908339e2013-02-03 09:58:38 +0000328 apc->irq = platform_get_irq(pdev, 0);
329 if (apc->irq < 0)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000330 return -EINVAL;
331
Gabor Juhos908339e2013-02-03 09:58:38 +0000332 spin_lock_init(&apc->lock);
333
334 apc->pci_controller.pci_ops = &ar724x_pci_ops;
335 apc->pci_controller.io_resource = &ar724x_io_resource;
336 apc->pci_controller.mem_resource = &ar724x_mem_resource;
337
338 apc->link_up = ar724x_pci_check_link(apc);
339 if (!apc->link_up)
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000340 dev_warn(&pdev->dev, "PCIe link is down\n");
341
Gabor Juhos908339e2013-02-03 09:58:38 +0000342 ar724x_pci_irq_init(apc);
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000343
Gabor Juhos908339e2013-02-03 09:58:38 +0000344 register_pci_controller(&apc->pci_controller);
Gabor Juhos58d2e9b2013-02-02 11:40:42 +0000345
346 return 0;
347}
348
349static struct platform_driver ar724x_pci_driver = {
350 .probe = ar724x_pci_probe,
351 .driver = {
352 .name = "ar724x-pci",
353 .owner = THIS_MODULE,
354 },
355};
356
357static int __init ar724x_pci_init(void)
358{
359 return platform_driver_register(&ar724x_pci_driver);
360}
361
362postcore_initcall(ar724x_pci_init);