]> Pileus Git - ~andy/linux/blob - arch/mips/pci/pci-ar724x.c
93ab8778ce1050a677e1bdb980a0985fb32c358b
[~andy/linux] / arch / mips / pci / pci-ar724x.c
1 /*
2  *  Atheros AR724X PCI host controller driver
3  *
4  *  Copyright (C) 2011 RenĂ© Bolldorf <xsecute@googlemail.com>
5  *  Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
6  *
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
12 #include <linux/spinlock.h>
13 #include <linux/irq.h>
14 #include <linux/pci.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <asm/mach-ath79/ath79.h>
18 #include <asm/mach-ath79/ar71xx_regs.h>
19
20 #define AR724X_PCI_REG_RESET            0x18
21 #define AR724X_PCI_REG_INT_STATUS       0x4c
22 #define AR724X_PCI_REG_INT_MASK         0x50
23
24 #define AR724X_PCI_RESET_LINK_UP        BIT(0)
25
26 #define AR724X_PCI_INT_DEV0             BIT(14)
27
28 #define AR724X_PCI_IRQ_COUNT            1
29
30 #define AR7240_BAR0_WAR_VALUE   0xffff
31
32 struct ar724x_pci_controller {
33         void __iomem *devcfg_base;
34         void __iomem *ctrl_base;
35
36         int irq;
37
38         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
47 static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc)
48 {
49         u32 reset;
50
51         reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET);
52         return reset & AR724X_PCI_RESET_LINK_UP;
53 }
54
55 static inline struct ar724x_pci_controller *
56 pci_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
64 static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
65                             int size, uint32_t *value)
66 {
67         struct ar724x_pci_controller *apc;
68         unsigned long flags;
69         void __iomem *base;
70         u32 data;
71
72         apc = pci_bus_to_ar724x_controller(bus);
73         if (!apc->link_up)
74                 return PCIBIOS_DEVICE_NOT_FOUND;
75
76         if (devfn)
77                 return PCIBIOS_DEVICE_NOT_FOUND;
78
79         base = apc->devcfg_base;
80
81         spin_lock_irqsave(&apc->lock, flags);
82         data = __raw_readl(base + (where & ~3));
83
84         switch (size) {
85         case 1:
86                 if (where & 1)
87                         data >>= 8;
88                 if (where & 2)
89                         data >>= 16;
90                 data &= 0xff;
91                 break;
92         case 2:
93                 if (where & 2)
94                         data >>= 16;
95                 data &= 0xffff;
96                 break;
97         case 4:
98                 break;
99         default:
100                 spin_unlock_irqrestore(&apc->lock, flags);
101
102                 return PCIBIOS_BAD_REGISTER_NUMBER;
103         }
104
105         spin_unlock_irqrestore(&apc->lock, flags);
106
107         if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
108             apc->bar0_is_cached) {
109                 /* use the cached value */
110                 *value = apc->bar0_value;
111         } else {
112                 *value = data;
113         }
114
115         return PCIBIOS_SUCCESSFUL;
116 }
117
118 static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
119                              int size, uint32_t value)
120 {
121         struct ar724x_pci_controller *apc;
122         unsigned long flags;
123         void __iomem *base;
124         u32 data;
125         int s;
126
127         apc = pci_bus_to_ar724x_controller(bus);
128         if (!apc->link_up)
129                 return PCIBIOS_DEVICE_NOT_FOUND;
130
131         if (devfn)
132                 return PCIBIOS_DEVICE_NOT_FOUND;
133
134         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                          */
146                         apc->bar0_is_cached = true;
147                         apc->bar0_value = value;
148
149                         value = AR7240_BAR0_WAR_VALUE;
150                 } else {
151                         apc->bar0_is_cached = false;
152                 }
153         }
154
155         base = apc->devcfg_base;
156
157         spin_lock_irqsave(&apc->lock, flags);
158         data = __raw_readl(base + (where & ~3));
159
160         switch (size) {
161         case 1:
162                 s = ((where & 3) * 8);
163                 data &= ~(0xff << s);
164                 data |= ((value & 0xff) << s);
165                 break;
166         case 2:
167                 s = ((where & 2) * 8);
168                 data &= ~(0xffff << s);
169                 data |= ((value & 0xffff) << s);
170                 break;
171         case 4:
172                 data = value;
173                 break;
174         default:
175                 spin_unlock_irqrestore(&apc->lock, flags);
176
177                 return PCIBIOS_BAD_REGISTER_NUMBER;
178         }
179
180         __raw_writel(data, base + (where & ~3));
181         /* flush write */
182         __raw_readl(base + (where & ~3));
183         spin_unlock_irqrestore(&apc->lock, flags);
184
185         return PCIBIOS_SUCCESSFUL;
186 }
187
188 static struct pci_ops ar724x_pci_ops = {
189         .read   = ar724x_pci_read,
190         .write  = ar724x_pci_write,
191 };
192
193 static struct resource ar724x_io_resource = {
194         .name   = "PCI IO space",
195         .start  = 0,
196         .end    = 0,
197         .flags  = IORESOURCE_IO,
198 };
199
200 static struct resource ar724x_mem_resource = {
201         .name   = "PCI memory space",
202         .start  = AR724X_PCI_MEM_BASE,
203         .end    = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
204         .flags  = IORESOURCE_MEM,
205 };
206
207 static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc)
208 {
209         struct ar724x_pci_controller *apc;
210         void __iomem *base;
211         u32 pending;
212
213         apc = irq_get_handler_data(irq);
214         base = apc->ctrl_base;
215
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
226 static void ar724x_pci_irq_unmask(struct irq_data *d)
227 {
228         struct ar724x_pci_controller *apc;
229         void __iomem *base;
230         u32 t;
231
232         apc = irq_data_get_irq_chip_data(d);
233         base = apc->ctrl_base;
234
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
245 static void ar724x_pci_irq_mask(struct irq_data *d)
246 {
247         struct ar724x_pci_controller *apc;
248         void __iomem *base;
249         u32 t;
250
251         apc = irq_data_get_irq_chip_data(d);
252         base = apc->ctrl_base;
253
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
272 static 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
279 static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc)
280 {
281         void __iomem *base;
282         int i;
283
284         base = apc->ctrl_base;
285
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;
292              i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++) {
293                 irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
294                                          handle_level_irq);
295                 irq_set_chip_data(i, apc);
296         }
297
298         irq_set_handler_data(apc->irq, apc);
299         irq_set_chained_handler(apc->irq, ar724x_pci_irq_handler);
300 }
301
302 static int ar724x_pci_probe(struct platform_device *pdev)
303 {
304         struct ar724x_pci_controller *apc;
305         struct resource *res;
306
307         apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller),
308                             GFP_KERNEL);
309         if (!apc)
310                 return -ENOMEM;
311
312         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base");
313         if (!res)
314                 return -EINVAL;
315
316         apc->ctrl_base = devm_request_and_ioremap(&pdev->dev, res);
317         if (apc->ctrl_base == NULL)
318                 return -EBUSY;
319
320         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
321         if (!res)
322                 return -EINVAL;
323
324         apc->devcfg_base = devm_request_and_ioremap(&pdev->dev, res);
325         if (!apc->devcfg_base)
326                 return -EBUSY;
327
328         apc->irq = platform_get_irq(pdev, 0);
329         if (apc->irq < 0)
330                 return -EINVAL;
331
332         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)
340                 dev_warn(&pdev->dev, "PCIe link is down\n");
341
342         ar724x_pci_irq_init(apc);
343
344         register_pci_controller(&apc->pci_controller);
345
346         return 0;
347 }
348
349 static struct platform_driver ar724x_pci_driver = {
350         .probe = ar724x_pci_probe,
351         .driver = {
352                 .name = "ar724x-pci",
353                 .owner = THIS_MODULE,
354         },
355 };
356
357 static int __init ar724x_pci_init(void)
358 {
359         return platform_driver_register(&ar724x_pci_driver);
360 }
361
362 postcore_initcall(ar724x_pci_init);