blob: e48dddbb491984e7402be6ab76c9f452198a5f3e [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;
Gabor Juhos42cb60d2013-02-07 19:28:15 +000056 struct resource io_res;
57 struct resource mem_res;
Gabor Juhosf18118a2013-02-07 19:28:14 +000058};
Gabor Juhosf8365ec2012-03-14 10:36:10 +010059
60/* Byte lane enable bits */
61static const u8 ar71xx_pci_ble_table[4][4] = {
62 {0x0, 0xf, 0xf, 0xf},
63 {0xe, 0xd, 0xb, 0x7},
64 {0xc, 0xf, 0x3, 0xf},
65 {0xf, 0xf, 0xf, 0xf},
66};
67
68static const u32 ar71xx_pci_read_mask[8] = {
69 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0
70};
71
72static inline u32 ar71xx_pci_get_ble(int where, int size, int local)
73{
74 u32 t;
75
76 t = ar71xx_pci_ble_table[size & 3][where & 3];
77 BUG_ON(t == 0xf);
78 t <<= (local) ? 20 : 4;
79
80 return t;
81}
82
83static inline u32 ar71xx_pci_bus_addr(struct pci_bus *bus, unsigned int devfn,
84 int where)
85{
86 u32 ret;
87
88 if (!bus->number) {
89 /* type 0 */
90 ret = (1 << PCI_SLOT(devfn)) | (PCI_FUNC(devfn) << 8) |
91 (where & ~3);
92 } else {
93 /* type 1 */
94 ret = (bus->number << 16) | (PCI_SLOT(devfn) << 11) |
95 (PCI_FUNC(devfn) << 8) | (where & ~3) | 1;
96 }
97
98 return ret;
99}
100
Gabor Juhosf18118a2013-02-07 19:28:14 +0000101static inline struct ar71xx_pci_controller *
102pci_bus_to_ar71xx_controller(struct pci_bus *bus)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100103{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000104 struct pci_controller *hose;
105
106 hose = (struct pci_controller *) bus->sysdata;
107 return container_of(hose, struct ar71xx_pci_controller, pci_ctrl);
108}
109
110static int ar71xx_pci_check_error(struct ar71xx_pci_controller *apc, int quiet)
111{
112 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100113 u32 pci_err;
114 u32 ahb_err;
115
116 pci_err = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR) & 3;
117 if (pci_err) {
118 if (!quiet) {
119 u32 addr;
120
121 addr = __raw_readl(base + AR71XX_PCI_REG_PCI_ERR_ADDR);
122 pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
123 "PCI", pci_err, addr);
124 }
125
126 /* clear PCI error status */
127 __raw_writel(pci_err, base + AR71XX_PCI_REG_PCI_ERR);
128 }
129
130 ahb_err = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR) & 1;
131 if (ahb_err) {
132 if (!quiet) {
133 u32 addr;
134
135 addr = __raw_readl(base + AR71XX_PCI_REG_AHB_ERR_ADDR);
136 pr_crit("ar71xx: %s bus error %d at addr 0x%x\n",
137 "AHB", ahb_err, addr);
138 }
139
140 /* clear AHB error status */
141 __raw_writel(ahb_err, base + AR71XX_PCI_REG_AHB_ERR);
142 }
143
144 return !!(ahb_err | pci_err);
145}
146
Gabor Juhosf18118a2013-02-07 19:28:14 +0000147static inline void ar71xx_pci_local_write(struct ar71xx_pci_controller *apc,
148 int where, int size, u32 value)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100149{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000150 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100151 u32 ad_cbe;
152
153 value = value << (8 * (where & 3));
154
155 ad_cbe = AR71XX_PCI_CRP_CMD_WRITE | (where & ~3);
156 ad_cbe |= ar71xx_pci_get_ble(where, size, 1);
157
158 __raw_writel(ad_cbe, base + AR71XX_PCI_REG_CRP_AD_CBE);
159 __raw_writel(value, base + AR71XX_PCI_REG_CRP_WRDATA);
160}
161
162static inline int ar71xx_pci_set_cfgaddr(struct pci_bus *bus,
163 unsigned int devfn,
164 int where, int size, u32 cmd)
165{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000166 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
167 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100168 u32 addr;
169
170 addr = ar71xx_pci_bus_addr(bus, devfn, where);
171
172 __raw_writel(addr, base + AR71XX_PCI_REG_CFG_AD);
173 __raw_writel(cmd | ar71xx_pci_get_ble(where, size, 0),
174 base + AR71XX_PCI_REG_CFG_CBE);
175
Gabor Juhosf18118a2013-02-07 19:28:14 +0000176 return ar71xx_pci_check_error(apc, 1);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100177}
178
179static int ar71xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
180 int where, int size, u32 *value)
181{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000182 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
183 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100184 unsigned long flags;
185 u32 data;
186 int err;
187 int ret;
188
189 ret = PCIBIOS_SUCCESSFUL;
190 data = ~0;
191
Gabor Juhosf18118a2013-02-07 19:28:14 +0000192 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100193
194 err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
195 AR71XX_PCI_CFG_CMD_READ);
196 if (err)
197 ret = PCIBIOS_DEVICE_NOT_FOUND;
198 else
199 data = __raw_readl(base + AR71XX_PCI_REG_CFG_RDDATA);
200
Gabor Juhosf18118a2013-02-07 19:28:14 +0000201 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100202
203 *value = (data >> (8 * (where & 3))) & ar71xx_pci_read_mask[size & 7];
204
205 return ret;
206}
207
208static int ar71xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
209 int where, int size, u32 value)
210{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000211 struct ar71xx_pci_controller *apc = pci_bus_to_ar71xx_controller(bus);
212 void __iomem *base = apc->cfg_base;
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100213 unsigned long flags;
214 int err;
215 int ret;
216
217 value = value << (8 * (where & 3));
218 ret = PCIBIOS_SUCCESSFUL;
219
Gabor Juhosf18118a2013-02-07 19:28:14 +0000220 spin_lock_irqsave(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100221
222 err = ar71xx_pci_set_cfgaddr(bus, devfn, where, size,
223 AR71XX_PCI_CFG_CMD_WRITE);
224 if (err)
225 ret = PCIBIOS_DEVICE_NOT_FOUND;
226 else
227 __raw_writel(value, base + AR71XX_PCI_REG_CFG_WRDATA);
228
Gabor Juhosf18118a2013-02-07 19:28:14 +0000229 spin_unlock_irqrestore(&apc->lock, flags);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100230
231 return ret;
232}
233
234static struct pci_ops ar71xx_pci_ops = {
235 .read = ar71xx_pci_read_config,
236 .write = ar71xx_pci_write_config,
237};
238
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100239static void ar71xx_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
240{
241 void __iomem *base = ath79_reset_base;
242 u32 pending;
243
244 pending = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_STATUS) &
245 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
246
247 if (pending & AR71XX_PCI_INT_DEV0)
248 generic_handle_irq(ATH79_PCI_IRQ(0));
249
250 else if (pending & AR71XX_PCI_INT_DEV1)
251 generic_handle_irq(ATH79_PCI_IRQ(1));
252
253 else if (pending & AR71XX_PCI_INT_DEV2)
254 generic_handle_irq(ATH79_PCI_IRQ(2));
255
256 else if (pending & AR71XX_PCI_INT_CORE)
257 generic_handle_irq(ATH79_PCI_IRQ(4));
258
259 else
260 spurious_interrupt();
261}
262
263static void ar71xx_pci_irq_unmask(struct irq_data *d)
264{
265 unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
266 void __iomem *base = ath79_reset_base;
267 u32 t;
268
269 t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
270 __raw_writel(t | (1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
271
272 /* flush write */
273 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
274}
275
276static void ar71xx_pci_irq_mask(struct irq_data *d)
277{
278 unsigned int irq = d->irq - ATH79_PCI_IRQ_BASE;
279 void __iomem *base = ath79_reset_base;
280 u32 t;
281
282 t = __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
283 __raw_writel(t & ~(1 << irq), base + AR71XX_RESET_REG_PCI_INT_ENABLE);
284
285 /* flush write */
286 __raw_readl(base + AR71XX_RESET_REG_PCI_INT_ENABLE);
287}
288
289static struct irq_chip ar71xx_pci_irq_chip = {
290 .name = "AR71XX PCI",
291 .irq_mask = ar71xx_pci_irq_mask,
292 .irq_unmask = ar71xx_pci_irq_unmask,
293 .irq_mask_ack = ar71xx_pci_irq_mask,
294};
295
Gabor Juhosf18118a2013-02-07 19:28:14 +0000296static void ar71xx_pci_irq_init(struct ar71xx_pci_controller *apc)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100297{
298 void __iomem *base = ath79_reset_base;
299 int i;
300
301 __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_ENABLE);
302 __raw_writel(0, base + AR71XX_RESET_REG_PCI_INT_STATUS);
303
304 BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR71XX_PCI_IRQ_COUNT);
305
306 for (i = ATH79_PCI_IRQ_BASE;
307 i < ATH79_PCI_IRQ_BASE + AR71XX_PCI_IRQ_COUNT; i++)
308 irq_set_chip_and_handler(i, &ar71xx_pci_irq_chip,
309 handle_level_irq);
310
Gabor Juhosf18118a2013-02-07 19:28:14 +0000311 irq_set_chained_handler(apc->irq, ar71xx_pci_irq_handler);
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100312}
313
Gabor Juhosfb167e82013-02-02 11:40:43 +0000314static void ar71xx_pci_reset(void)
Gabor Juhosf8365ec2012-03-14 10:36:10 +0100315{
316 void __iomem *ddr_base = ath79_ddr_base;
317
318 ath79_device_reset_set(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
319 mdelay(100);
320
321 ath79_device_reset_clear(AR71XX_RESET_PCI_BUS | AR71XX_RESET_PCI_CORE);
322 mdelay(100);
323
324 __raw_writel(AR71XX_PCI_WIN0_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN0);
325 __raw_writel(AR71XX_PCI_WIN1_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN1);
326 __raw_writel(AR71XX_PCI_WIN2_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN2);
327 __raw_writel(AR71XX_PCI_WIN3_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN3);
328 __raw_writel(AR71XX_PCI_WIN4_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN4);
329 __raw_writel(AR71XX_PCI_WIN5_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN5);
330 __raw_writel(AR71XX_PCI_WIN6_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN6);
331 __raw_writel(AR71XX_PCI_WIN7_OFFS, ddr_base + AR71XX_DDR_REG_PCI_WIN7);
332
333 mdelay(100);
334}
335
Gabor Juhosfb167e82013-02-02 11:40:43 +0000336static int ar71xx_pci_probe(struct platform_device *pdev)
337{
Gabor Juhosf18118a2013-02-07 19:28:14 +0000338 struct ar71xx_pci_controller *apc;
Gabor Juhosfb167e82013-02-02 11:40:43 +0000339 struct resource *res;
Gabor Juhosfb167e82013-02-02 11:40:43 +0000340 u32 t;
341
Gabor Juhosf18118a2013-02-07 19:28:14 +0000342 apc = devm_kzalloc(&pdev->dev, sizeof(struct ar71xx_pci_controller),
343 GFP_KERNEL);
344 if (!apc)
345 return -ENOMEM;
346
347 spin_lock_init(&apc->lock);
348
Gabor Juhosfb167e82013-02-02 11:40:43 +0000349 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
350 if (!res)
351 return -EINVAL;
352
Gabor Juhosf18118a2013-02-07 19:28:14 +0000353 apc->cfg_base = devm_request_and_ioremap(&pdev->dev, res);
354 if (!apc->cfg_base)
Gabor Juhosfb167e82013-02-02 11:40:43 +0000355 return -ENOMEM;
356
Gabor Juhosf18118a2013-02-07 19:28:14 +0000357 apc->irq = platform_get_irq(pdev, 0);
358 if (apc->irq < 0)
Gabor Juhosfb167e82013-02-02 11:40:43 +0000359 return -EINVAL;
360
Gabor Juhos42cb60d2013-02-07 19:28:15 +0000361 res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base");
362 if (!res)
363 return -EINVAL;
364
365 apc->io_res.parent = res;
366 apc->io_res.name = "PCI IO space";
367 apc->io_res.start = res->start;
368 apc->io_res.end = res->end;
369 apc->io_res.flags = IORESOURCE_IO;
370
371 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base");
372 if (!res)
373 return -EINVAL;
374
375 apc->mem_res.parent = res;
376 apc->mem_res.name = "PCI memory space";
377 apc->mem_res.start = res->start;
378 apc->mem_res.end = res->end;
379 apc->mem_res.flags = IORESOURCE_MEM;
380
Gabor Juhosfb167e82013-02-02 11:40:43 +0000381 ar71xx_pci_reset();
382
383 /* setup COMMAND register */
384 t = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
385 | PCI_COMMAND_PARITY | PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK;
Gabor Juhosf18118a2013-02-07 19:28:14 +0000386 ar71xx_pci_local_write(apc, PCI_COMMAND, 4, t);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000387
388 /* clear bus errors */
Gabor Juhosf18118a2013-02-07 19:28:14 +0000389 ar71xx_pci_check_error(apc, 1);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000390
Gabor Juhosf18118a2013-02-07 19:28:14 +0000391 ar71xx_pci_irq_init(apc);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000392
Gabor Juhosf18118a2013-02-07 19:28:14 +0000393 apc->pci_ctrl.pci_ops = &ar71xx_pci_ops;
Gabor Juhos42cb60d2013-02-07 19:28:15 +0000394 apc->pci_ctrl.mem_resource = &apc->mem_res;
395 apc->pci_ctrl.io_resource = &apc->io_res;
Gabor Juhosf18118a2013-02-07 19:28:14 +0000396
397 register_pci_controller(&apc->pci_ctrl);
Gabor Juhosfb167e82013-02-02 11:40:43 +0000398
399 return 0;
400}
401
402static struct platform_driver ar71xx_pci_driver = {
403 .probe = ar71xx_pci_probe,
404 .driver = {
405 .name = "ar71xx-pci",
406 .owner = THIS_MODULE,
407 },
408};
409
410static int __init ar71xx_pci_init(void)
411{
412 return platform_driver_register(&ar71xx_pci_driver);
413}
414
415postcore_initcall(ar71xx_pci_init);