blob: 44dc5bf720c048526b115c9f02dc11d0e447becf [file] [log] [blame]
Gabor Juhosf8365ec2012-03-14 10:36:10 +01001/*
2 * Atheros AR71xx PCI host controller driver
3 *
4 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Parts of this file are based on Atheros' 2.6.15 BSP
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 */
13
14#include <linux/resource.h>
15#include <linux/types.h>
16#include <linux/delay.h>
17#include <linux/bitops.h>
18#include <linux/pci.h>
19#include <linux/pci_regs.h>
20#include <linux/interrupt.h>
Gabor Juhosfb167e82013-02-02 11:40:43 +000021#include <linux/module.h>
22#include <linux/platform_device.h>
Gabor Juhosf8365ec2012-03-14 10:36:10 +010023
24#include <asm/mach-ath79/ar71xx_regs.h>
25#include <asm/mach-ath79/ath79.h>
Gabor Juhosf8365ec2012-03-14 10:36:10 +010026
Gabor Juhosf8365ec2012-03-14 10:36:10 +010027#define AR71XX_PCI_REG_CRP_AD_CBE 0x00
28#define AR71XX_PCI_REG_CRP_WRDATA 0x04
29#define AR71XX_PCI_REG_CRP_RDDATA 0x08
30#define AR71XX_PCI_REG_CFG_AD 0x0c
31#define AR71XX_PCI_REG_CFG_CBE 0x10
32#define AR71XX_PCI_REG_CFG_WRDATA 0x14
33#define AR71XX_PCI_REG_CFG_RDDATA 0x18
34#define AR71XX_PCI_REG_PCI_ERR 0x1c
35#define AR71XX_PCI_REG_PCI_ERR_ADDR 0x20
36#define AR71XX_PCI_REG_AHB_ERR 0x24
37#define AR71XX_PCI_REG_AHB_ERR_ADDR 0x28
38
39#define AR71XX_PCI_CRP_CMD_WRITE 0x00010000
40#define AR71XX_PCI_CRP_CMD_READ 0x00000000
41#define AR71XX_PCI_CFG_CMD_READ 0x0000000a
42#define AR71XX_PCI_CFG_CMD_WRITE 0x0000000b
43
44#define AR71XX_PCI_INT_CORE BIT(4)
45#define AR71XX_PCI_INT_DEV2 BIT(2)
46#define AR71XX_PCI_INT_DEV1 BIT(1)
47#define AR71XX_PCI_INT_DEV0 BIT(0)
48
49#define AR71XX_PCI_IRQ_COUNT 5
50
Gabor Juhosf18118a2013-02-07 19:28:14 +000051struct ar71xx_pci_controller {
52 void __iomem *cfg_base;
53 spinlock_t lock;
54 int irq;
55 struct pci_controller pci_ctrl;
56};
Gabor Juhosf8365ec2012-03-14 10:36:10 +010057
58/* Byte lane enable bits */
59static const u8 ar71xx_pci_ble_table[4][4] = {
60 {0x0, 0xf, 0xf, 0xf},
61 {0xe, 0xd, 0xb, 0x7},
62 {0xc, 0xf, 0x3, 0xf},
63 {0xf, 0xf, 0xf, 0xf},
64};
65
66static const u32 ar71xx_pci_read_mask[8] = {
67 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
68};
69
70static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
71{
72 u32 t;
73
74 t = ar71xx_pci_ble_table[size & 3][where & 3];
75 BUG_ON(t == 0xf);
76 t <<= (local) ? 20 : 4;
77
78 return t;
79}
80
81static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
82 int where)
83{
84 u32 ret;
85
86 if (!bus->number) {
87 /* type 0 */
88 ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
89 (where & ~3);
90 } else {
91 /* type 1 */
92 ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
93 (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
94 }
95
96 return ret;
97}
98
Gabor Juhosf18118a2013-02-07 19:28:14 +000099static inline struct ar71xx_pci_controller *
100pci_bus_to_ar71xx_controller(struct pci_bus *bus)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100101{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000102 struct pci_controller *hose;
103
104 hose = (struct pci_controller *) bus->sysdata;
105 return container_of(hose, struct ar71xx_pci_controller, pci_ctrl);
106}
107
108static int ar71xx_pci_check_error(struct ar71xx_pci_controller *apc, int quiet)
109{
110 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100111 u32 pci_err;
112 u32 ahb_err;
113
114 pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
115 if (pci_err) {
116 if (!quiet) {
117 u32 addr;
118
119 addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
120 pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
121 "PCI", pci_err, addr);
122 }
123
124 /* clear PCI error status */
125 __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
126 }
127
128 ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
129 if (ahb_err) {
130 if (!quiet) {
131 u32 addr;
132
133 addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
134 pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
135 "AHB", ahb_err, addr);
136 }
137
138 /* clear AHB error status */
139 __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
140 }
141
142 return !!(ahb_err | pci_err);
143}
144
Gabor Juhosf18118a2013-02-07 19:28:14 +0000145static inline void ar71xx_pci_local_write(struct ar71xx_pci_controller *apc,
146 int where, int size, u32 value)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100147{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000148 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100149 u32 ad_cbe;
150
151 value = value << (8 * (where & 3));
152
153 ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
154 ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
155
156 __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
157 __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
158}
159
160static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
161 unsigned int devfn,
162 int where, int size, u32 cmd)
163{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000164 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
165 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100166 u32 addr;
167
168 addr = ar71xx_pci_bus_addr(bus, devfn, where);
169
170 __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
171 __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
172 base + AR71XX_PCI_REG_CFG_CBE);
173
Gabor Juhosf18118a2013-02-07 19:28:14 +0000174 return ar71xx_pci_check_error(apc, 1);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100175}
176
177static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
178 int where, int size, u32 *value)
179{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000180 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
181 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100182 unsigned long flags;
183 u32 data;
184 int err;
185 int ret;
186
187 ret = PCIBIOS_SUCCESSFUL;
188 data = ~0;
189
Gabor Juhosf18118a2013-02-07 19:28:14 +0000190 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100191
192 err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
193 AR71XX_PCI_CFG_CMD_READ);
194 if (err)
195 ret = PCIBIOS_DEVICE_NOT_FOUND;
196 else
197 data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
198
Gabor Juhosf18118a2013-02-07 19:28:14 +0000199 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100200
201 *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
202
203 return ret;
204}
205
206static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
207 int where, int size, u32 value)
208{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000209 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
210 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100211 unsigned long flags;
212 int err;
213 int ret;
214
215 value = value << (8 * (where & 3));
216 ret = PCIBIOS_SUCCESSFUL;
217
Gabor Juhosf18118a2013-02-07 19:28:14 +0000218 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100219
220 err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
221 AR71XX_PCI_CFG_CMD_WRITE);
222 if (err)
223 ret = PCIBIOS_DEVICE_NOT_FOUND;
224 else
225 __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
226
Gabor Juhosf18118a2013-02-07 19:28:14 +0000227 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100228
229 return ret;
230}
231
232static struct pci_ops ar71xx_pci_ops = {
233 .read = ar71xx_pci_read_config,
234 .write = ar71xx_pci_write_config,
235};
236
237static struct resource ar71xx_pci_io_resource = {
238 .name = "PCI IO space",
239 .start = 0,
240 .end = 0,
241 .flags = IORESOURCE_IO,
242};
243
244static struct resource ar71xx_pci_mem_resource = {
245 .name = "PCI memory space",
246 .start = AR71XX_PCI_MEM_BASE,
247 .end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1,
248 .flags = IORESOURCE_MEM
249};
250
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100251static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
252{
253 void __iomem *base = ath79_reset_base;
254 u32 pending;
255
256 pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
257 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
258
259 if (pending & AR71XX_PCI_INT_DEV0)
260 generic_handle_irq(ATH79_PCI_IRQ(0));
261
262 else if (pending & AR71XX_PCI_INT_DEV1)
263 generic_handle_irq(ATH79_PCI_IRQ(1));
264
265 else if (pending & AR71XX_PCI_INT_DEV2)
266 generic_handle_irq(ATH79_PCI_IRQ(2));
267
268 else if (pending & AR71XX_PCI_INT_CORE)
269 generic_handle_irq(ATH79_PCI_IRQ(4));
270
271 else
272 spurious_interrupt();
273}
274
275static void ar71xx_pci_irq_unmask(struct irq_data *d)
276{
277 unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
278 void __iomem *base = ath79_reset_base;
279 u32 t;
280
281 t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
282 __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
283
284 /* flush write */
285 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
286}
287
288static void ar71xx_pci_irq_mask(struct irq_data *d)
289{
290 unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
291 void __iomem *base = ath79_reset_base;
292 u32 t;
293
294 t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
295 __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
296
297 /* flush write */
298 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
299}
300
301static struct irq_chip ar71xx_pci_irq_chip = {
302 .name = "AR71XX PCI",
303 .irq_mask = ar71xx_pci_irq_mask,
304 .irq_unmask = ar71xx_pci_irq_unmask,
305 .irq_mask_ack = ar71xx_pci_irq_mask,
306};
307
Gabor Juhosf18118a2013-02-07 19:28:14 +0000308static void ar71xx_pci_irq_init(struct ar71xx_pci_controller *apc)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100309{
310 void __iomem *base = ath79_reset_base;
311 int i;
312
313 __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
314 __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
315
316 BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
317
318 for (i = ATH79_PCI_IRQ_BASE;
319 i < ATH79_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++)
320 irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
321 handle_level_irq);
322
Gabor Juhosf18118a2013-02-07 19:28:14 +0000323 irq_set_chained_handler(apc->irq, ar71xx_pci_irq_handler);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100324}
325
Gabor Juhosfb167e82013-02-02 11:40:43 +0000326static void ar71xx_pci_reset(void)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100327{
328 void __iomem *ddr_base = ath79_ddr_base;
329
330 ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
331 mdelay(100);
332
333 ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
334 mdelay(100);
335
336 __raw_writel(AR71XX_PCI_WIN0_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN0);
337 __raw_writel(AR71XX_PCI_WIN1_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN1);
338 __raw_writel(AR71XX_PCI_WIN2_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN2);
339 __raw_writel(AR71XX_PCI_WIN3_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN3);
340 __raw_writel(AR71XX_PCI_WIN4_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN4);
341 __raw_writel(AR71XX_PCI_WIN5_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN5);
342 __raw_writel(AR71XX_PCI_WIN6_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN6);
343 __raw_writel(AR71XX_PCI_WIN7_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN7);
344
345 mdelay(100);
346}
347
Gabor Juhosfb167e82013-02-02 11:40:43 +0000348static int ar71xx_pci_probe(struct platform_device *pdev)
349{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000350 struct ar71xx_pci_controller *apc;
Gabor Juhosfb167e82013-02-02 11:40:43 +0000351 struct resource *res;
Gabor Juhosfb167e82013-02-02 11:40:43 +0000352 u32 t;
353
Gabor Juhosf18118a2013-02-07 19:28:14 +0000354 apc = devm_kzalloc(&pdev->dev, sizeof(struct ar71xx_pci_controller),
355 GFP_KERNEL);
356 if (!apc)
357 return -ENOMEM;
358
359 spin_lock_init(&apc->lock);
360
Gabor Juhosfb167e82013-02-02 11:40:43 +0000361 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
362 if (!res)
363 return -EINVAL;
364
Gabor Juhosf18118a2013-02-07 19:28:14 +0000365 apc->cfg_base = devm_request_and_ioremap(&pdev->dev, res);
366 if (!apc->cfg_base)
Gabor Juhosfb167e82013-02-02 11:40:43 +0000367 return -ENOMEM;
368
Gabor Juhosf18118a2013-02-07 19:28:14 +0000369 apc->irq = platform_get_irq(pdev, 0);
370 if (apc->irq < 0)
Gabor Juhosfb167e82013-02-02 11:40:43 +0000371 return -EINVAL;
372
373 ar71xx_pci_reset();
374
375 /* setup COMMAND register */
376 t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
377 | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
Gabor Juhosf18118a2013-02-07 19:28:14 +0000378 ar71xx_pci_local_write(apc, PCI_COMMAND, 4, t);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000379
380 /* clear bus errors */
Gabor Juhosf18118a2013-02-07 19:28:14 +0000381 ar71xx_pci_check_error(apc, 1);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000382
Gabor Juhosf18118a2013-02-07 19:28:14 +0000383 ar71xx_pci_irq_init(apc);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000384
Gabor Juhosf18118a2013-02-07 19:28:14 +0000385 apc->pci_ctrl.pci_ops = &ar71xx_pci_ops;
386 apc->pci_ctrl.mem_resource = &ar71xx_pci_mem_resource;
387 apc->pci_ctrl.io_resource = &ar71xx_pci_io_resource;
388
389 register_pci_controller(&apc->pci_ctrl);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000390
391 return 0;
392}
393
394static struct platform_driver ar71xx_pci_driver = {
395 .probe = ar71xx_pci_probe,
396 .driver = {
397 .name = "ar71xx-pci",
398 .owner = THIS_MODULE,
399 },
400};
401
402static int __init ar71xx_pci_init(void)
403{
404 return platform_driver_register(&ar71xx_pci_driver);
405}
406
407postcore_initcall(ar71xx_pci_init);