2 * Atheros AR724X PCI host controller driver
4 * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
5 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
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.
12 #include <linux/irq.h>
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <asm/mach-ath79/ath79.h>
18 #include <asm/mach-ath79/ar71xx_regs.h>
20 #define AR724X_PCI_REG_APP 0x00
21 #define AR724X_PCI_REG_RESET 0x18
22 #define AR724X_PCI_REG_INT_STATUS 0x4c
23 #define AR724X_PCI_REG_INT_MASK 0x50
25 #define AR724X_PCI_APP_LTSSM_ENABLE BIT(0)
27 #define AR724X_PCI_RESET_LINK_UP BIT(0)
29 #define AR724X_PCI_INT_DEV0 BIT(14)
31 #define AR724X_PCI_IRQ_COUNT 1
33 #define AR7240_BAR0_WAR_VALUE 0xffff
35 #define AR724X_PCI_CMD_INIT (PCI_COMMAND_MEMORY | \
36 PCI_COMMAND_MASTER | \
37 PCI_COMMAND_INVALIDATE | \
38 PCI_COMMAND_PARITY | \
40 PCI_COMMAND_FAST_BACK)
42 struct ar724x_pci_controller {
43 void __iomem *devcfg_base;
44 void __iomem *ctrl_base;
45 void __iomem *crp_base;
54 struct pci_controller pci_controller;
55 struct resource io_res;
56 struct resource mem_res;
59 static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc)
63 reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET);
64 return reset & AR724X_PCI_RESET_LINK_UP;
67 static inline struct ar724x_pci_controller *
68 pci_bus_to_ar724x_controller(struct pci_bus *bus)
70 struct pci_controller *hose;
72 hose = (struct pci_controller *) bus->sysdata;
73 return container_of(hose, struct ar724x_pci_controller, pci_controller);
76 static int ar724x_pci_local_write(struct ar724x_pci_controller *apc,
77 int where, int size, u32 value)
83 WARN_ON(where & (size - 1));
86 return PCIBIOS_DEVICE_NOT_FOUND;
89 data = __raw_readl(base + (where & ~3));
93 s = ((where & 3) * 8);
95 data |= ((value & 0xff) << s);
98 s = ((where & 2) * 8);
99 data &= ~(0xffff << s);
100 data |= ((value & 0xffff) << s);
106 return PCIBIOS_BAD_REGISTER_NUMBER;
109 __raw_writel(data, base + (where & ~3));
111 __raw_readl(base + (where & ~3));
113 return PCIBIOS_SUCCESSFUL;
116 static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
117 int size, uint32_t *value)
119 struct ar724x_pci_controller *apc;
123 apc = pci_bus_to_ar724x_controller(bus);
125 return PCIBIOS_DEVICE_NOT_FOUND;
128 return PCIBIOS_DEVICE_NOT_FOUND;
130 base = apc->devcfg_base;
131 data = __raw_readl(base + (where & ~3));
149 return PCIBIOS_BAD_REGISTER_NUMBER;
152 if (where == PCI_BASE_ADDRESS_0 && size == 4 &&
153 apc->bar0_is_cached) {
154 /* use the cached value */
155 *value = apc->bar0_value;
160 return PCIBIOS_SUCCESSFUL;
163 static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
164 int size, uint32_t value)
166 struct ar724x_pci_controller *apc;
171 apc = pci_bus_to_ar724x_controller(bus);
173 return PCIBIOS_DEVICE_NOT_FOUND;
176 return PCIBIOS_DEVICE_NOT_FOUND;
178 if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) {
179 if (value != 0xffffffff) {
181 * WAR for a hw issue. If the BAR0 register of the
182 * device is set to the proper base address, the
183 * memory space of the device is not accessible.
185 * Cache the intended value so it can be read back,
186 * and write a SoC specific constant value to the
187 * BAR0 register in order to make the device memory
190 apc->bar0_is_cached = true;
191 apc->bar0_value = value;
193 value = AR7240_BAR0_WAR_VALUE;
195 apc->bar0_is_cached = false;
199 base = apc->devcfg_base;
200 data = __raw_readl(base + (where & ~3));
204 s = ((where & 3) * 8);
205 data &= ~(0xff << s);
206 data |= ((value & 0xff) << s);
209 s = ((where & 2) * 8);
210 data &= ~(0xffff << s);
211 data |= ((value & 0xffff) << s);
217 return PCIBIOS_BAD_REGISTER_NUMBER;
220 __raw_writel(data, base + (where & ~3));
222 __raw_readl(base + (where & ~3));
224 return PCIBIOS_SUCCESSFUL;
227 static struct pci_ops ar724x_pci_ops = {
228 .read = ar724x_pci_read,
229 .write = ar724x_pci_write,
232 static void ar724x_pci_irq_handler(struct irq_desc *desc)
234 struct ar724x_pci_controller *apc;
238 apc = irq_desc_get_handler_data(desc);
239 base = apc->ctrl_base;
241 pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) &
242 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
244 if (pending & AR724X_PCI_INT_DEV0)
245 generic_handle_irq(apc->irq_base + 0);
248 spurious_interrupt();
251 static void ar724x_pci_irq_unmask(struct irq_data *d)
253 struct ar724x_pci_controller *apc;
258 apc = irq_data_get_irq_chip_data(d);
259 base = apc->ctrl_base;
260 offset = apc->irq_base - d->irq;
264 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
265 __raw_writel(t | AR724X_PCI_INT_DEV0,
266 base + AR724X_PCI_REG_INT_MASK);
268 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
272 static void ar724x_pci_irq_mask(struct irq_data *d)
274 struct ar724x_pci_controller *apc;
279 apc = irq_data_get_irq_chip_data(d);
280 base = apc->ctrl_base;
281 offset = apc->irq_base - d->irq;
285 t = __raw_readl(base + AR724X_PCI_REG_INT_MASK);
286 __raw_writel(t & ~AR724X_PCI_INT_DEV0,
287 base + AR724X_PCI_REG_INT_MASK);
290 __raw_readl(base + AR724X_PCI_REG_INT_MASK);
292 t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
293 __raw_writel(t | AR724X_PCI_INT_DEV0,
294 base + AR724X_PCI_REG_INT_STATUS);
297 __raw_readl(base + AR724X_PCI_REG_INT_STATUS);
301 static struct irq_chip ar724x_pci_irq_chip = {
302 .name = "AR724X PCI ",
303 .irq_mask = ar724x_pci_irq_mask,
304 .irq_unmask = ar724x_pci_irq_unmask,
305 .irq_mask_ack = ar724x_pci_irq_mask,
308 static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc,
314 base = apc->ctrl_base;
316 __raw_writel(0, base + AR724X_PCI_REG_INT_MASK);
317 __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS);
319 apc->irq_base = ATH79_PCI_IRQ_BASE + (id * AR724X_PCI_IRQ_COUNT);
321 for (i = apc->irq_base;
322 i < apc->irq_base + AR724X_PCI_IRQ_COUNT; i++) {
323 irq_set_chip_and_handler(i, &ar724x_pci_irq_chip,
325 irq_set_chip_data(i, apc);
328 irq_set_chained_handler_and_data(apc->irq, ar724x_pci_irq_handler,
332 static void ar724x_pci_hw_init(struct ar724x_pci_controller *apc)
337 /* deassert PCIe host controller and PCIe PHY reset */
338 ath79_device_reset_clear(AR724X_RESET_PCIE);
339 ath79_device_reset_clear(AR724X_RESET_PCIE_PHY);
341 /* remove the reset of the PCIE PLL */
342 ppl = ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG);
343 ppl &= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_RESET;
344 ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG, ppl);
346 /* deassert bypass for the PCIE PLL */
347 ppl = ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG);
348 ppl &= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_BYPASS;
349 ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG, ppl);
351 /* set PCIE Application Control to ready */
352 app = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_APP);
353 app |= AR724X_PCI_APP_LTSSM_ENABLE;
354 __raw_writel(app, apc->ctrl_base + AR724X_PCI_REG_APP);
356 /* wait up to 100ms for PHY link up */
360 } while (wait < 10 && !ar724x_pci_check_link(apc));
363 static int ar724x_pci_probe(struct platform_device *pdev)
365 struct ar724x_pci_controller *apc;
366 struct resource *res;
373 apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller),
378 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base");
379 apc->ctrl_base = devm_ioremap_resource(&pdev->dev, res);
380 if (IS_ERR(apc->ctrl_base))
381 return PTR_ERR(apc->ctrl_base);
383 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base");
384 apc->devcfg_base = devm_ioremap_resource(&pdev->dev, res);
385 if (IS_ERR(apc->devcfg_base))
386 return PTR_ERR(apc->devcfg_base);
388 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "crp_base");
389 apc->crp_base = devm_ioremap_resource(&pdev->dev, res);
390 if (IS_ERR(apc->crp_base))
391 return PTR_ERR(apc->crp_base);
393 apc->irq = platform_get_irq(pdev, 0);
397 res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base");
401 apc->io_res.parent = res;
402 apc->io_res.name = "PCI IO space";
403 apc->io_res.start = res->start;
404 apc->io_res.end = res->end;
405 apc->io_res.flags = IORESOURCE_IO;
407 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base");
411 apc->mem_res.parent = res;
412 apc->mem_res.name = "PCI memory space";
413 apc->mem_res.start = res->start;
414 apc->mem_res.end = res->end;
415 apc->mem_res.flags = IORESOURCE_MEM;
417 apc->pci_controller.pci_ops = &ar724x_pci_ops;
418 apc->pci_controller.io_resource = &apc->io_res;
419 apc->pci_controller.mem_resource = &apc->mem_res;
422 * Do the full PCIE Root Complex Initialization Sequence if the PCIe
423 * host controller is in reset.
425 if (ath79_reset_rr(AR724X_RESET_REG_RESET_MODULE) & AR724X_RESET_PCIE)
426 ar724x_pci_hw_init(apc);
428 apc->link_up = ar724x_pci_check_link(apc);
430 dev_warn(&pdev->dev, "PCIe link is down\n");
432 ar724x_pci_irq_init(apc, id);
434 ar724x_pci_local_write(apc, PCI_COMMAND, 4, AR724X_PCI_CMD_INIT);
436 register_pci_controller(&apc->pci_controller);
441 static struct platform_driver ar724x_pci_driver = {
442 .probe = ar724x_pci_probe,
444 .name = "ar724x-pci",
448 static int __init ar724x_pci_init(void)
450 return platform_driver_register(&ar724x_pci_driver);
453 postcore_initcall(ar724x_pci_init);