1 // SPDX-License-Identifier: GPL-2.0-only
3 * Atheros AR724X PCI host controller driver
5 * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
6 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
10 #include <linux/pci.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <asm/mach-ath79/ath79.h>
15 #include <asm/mach-ath79/ar71xx_regs.h>
17 #define AR724X_PCI_REG_APP 0x00
18 #define AR724X_PCI_REG_RESET 0x18
19 #define AR724X_PCI_REG_INT_STATUS 0x4c
20 #define AR724X_PCI_REG_INT_MASK 0x50
22 #define AR724X_PCI_APP_LTSSM_ENABLE BIT(0)
24 #define AR724X_PCI_RESET_LINK_UP BIT(0)
26 #define AR724X_PCI_INT_DEV0 BIT(14)
28 #define AR724X_PCI_IRQ_COUNT 1
30 #define AR7240_BAR0_WAR_VALUE 0xffff
32 #define AR724X_PCI_CMD_INIT (PCI_COMMAND_MEMORY | \
33 PCI_COMMAND_MASTER | \
34 PCI_COMMAND_INVALIDATE | \
35 PCI_COMMAND_PARITY | \
37 PCI_COMMAND_FAST_BACK)
39 struct ar724x_pci_controller
{
40 void __iomem
*devcfg_base
;
41 void __iomem
*ctrl_base
;
42 void __iomem
*crp_base
;
51 struct pci_controller pci_controller
;
52 struct resource io_res
;
53 struct resource mem_res
;
56 static inline bool ar724x_pci_check_link(struct ar724x_pci_controller
*apc
)
60 reset
= __raw_readl(apc
->ctrl_base
+ AR724X_PCI_REG_RESET
);
61 return reset
& AR724X_PCI_RESET_LINK_UP
;
64 static inline struct ar724x_pci_controller
*
65 pci_bus_to_ar724x_controller(struct pci_bus
*bus
)
67 struct pci_controller
*hose
;
69 hose
= (struct pci_controller
*) bus
->sysdata
;
70 return container_of(hose
, struct ar724x_pci_controller
, pci_controller
);
73 static int ar724x_pci_local_write(struct ar724x_pci_controller
*apc
,
74 int where
, int size
, u32 value
)
80 WARN_ON(where
& (size
- 1));
83 return PCIBIOS_DEVICE_NOT_FOUND
;
86 data
= __raw_readl(base
+ (where
& ~3));
90 s
= ((where
& 3) * 8);
92 data
|= ((value
& 0xff) << s
);
95 s
= ((where
& 2) * 8);
96 data
&= ~(0xffff << s
);
97 data
|= ((value
& 0xffff) << s
);
103 return PCIBIOS_BAD_REGISTER_NUMBER
;
106 __raw_writel(data
, base
+ (where
& ~3));
108 __raw_readl(base
+ (where
& ~3));
110 return PCIBIOS_SUCCESSFUL
;
113 static int ar724x_pci_read(struct pci_bus
*bus
, unsigned int devfn
, int where
,
114 int size
, uint32_t *value
)
116 struct ar724x_pci_controller
*apc
;
120 apc
= pci_bus_to_ar724x_controller(bus
);
122 return PCIBIOS_DEVICE_NOT_FOUND
;
125 return PCIBIOS_DEVICE_NOT_FOUND
;
127 base
= apc
->devcfg_base
;
128 data
= __raw_readl(base
+ (where
& ~3));
146 return PCIBIOS_BAD_REGISTER_NUMBER
;
149 if (where
== PCI_BASE_ADDRESS_0
&& size
== 4 &&
150 apc
->bar0_is_cached
) {
151 /* use the cached value */
152 *value
= apc
->bar0_value
;
157 return PCIBIOS_SUCCESSFUL
;
160 static int ar724x_pci_write(struct pci_bus
*bus
, unsigned int devfn
, int where
,
161 int size
, uint32_t value
)
163 struct ar724x_pci_controller
*apc
;
168 apc
= pci_bus_to_ar724x_controller(bus
);
170 return PCIBIOS_DEVICE_NOT_FOUND
;
173 return PCIBIOS_DEVICE_NOT_FOUND
;
175 if (soc_is_ar7240() && where
== PCI_BASE_ADDRESS_0
&& size
== 4) {
176 if (value
!= 0xffffffff) {
178 * WAR for a hw issue. If the BAR0 register of the
179 * device is set to the proper base address, the
180 * memory space of the device is not accessible.
182 * Cache the intended value so it can be read back,
183 * and write a SoC specific constant value to the
184 * BAR0 register in order to make the device memory
187 apc
->bar0_is_cached
= true;
188 apc
->bar0_value
= value
;
190 value
= AR7240_BAR0_WAR_VALUE
;
192 apc
->bar0_is_cached
= false;
196 base
= apc
->devcfg_base
;
197 data
= __raw_readl(base
+ (where
& ~3));
201 s
= ((where
& 3) * 8);
202 data
&= ~(0xff << s
);
203 data
|= ((value
& 0xff) << s
);
206 s
= ((where
& 2) * 8);
207 data
&= ~(0xffff << s
);
208 data
|= ((value
& 0xffff) << s
);
214 return PCIBIOS_BAD_REGISTER_NUMBER
;
217 __raw_writel(data
, base
+ (where
& ~3));
219 __raw_readl(base
+ (where
& ~3));
221 return PCIBIOS_SUCCESSFUL
;
224 static struct pci_ops ar724x_pci_ops
= {
225 .read
= ar724x_pci_read
,
226 .write
= ar724x_pci_write
,
229 static void ar724x_pci_irq_handler(struct irq_desc
*desc
)
231 struct ar724x_pci_controller
*apc
;
235 apc
= irq_desc_get_handler_data(desc
);
236 base
= apc
->ctrl_base
;
238 pending
= __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
) &
239 __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
241 if (pending
& AR724X_PCI_INT_DEV0
)
242 generic_handle_irq(apc
->irq_base
+ 0);
245 spurious_interrupt();
248 static void ar724x_pci_irq_unmask(struct irq_data
*d
)
250 struct ar724x_pci_controller
*apc
;
255 apc
= irq_data_get_irq_chip_data(d
);
256 base
= apc
->ctrl_base
;
257 offset
= apc
->irq_base
- d
->irq
;
261 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
262 __raw_writel(t
| AR724X_PCI_INT_DEV0
,
263 base
+ AR724X_PCI_REG_INT_MASK
);
265 __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
269 static void ar724x_pci_irq_mask(struct irq_data
*d
)
271 struct ar724x_pci_controller
*apc
;
276 apc
= irq_data_get_irq_chip_data(d
);
277 base
= apc
->ctrl_base
;
278 offset
= apc
->irq_base
- d
->irq
;
282 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
283 __raw_writel(t
& ~AR724X_PCI_INT_DEV0
,
284 base
+ AR724X_PCI_REG_INT_MASK
);
287 __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
289 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
);
290 __raw_writel(t
| AR724X_PCI_INT_DEV0
,
291 base
+ AR724X_PCI_REG_INT_STATUS
);
294 __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
);
298 static struct irq_chip ar724x_pci_irq_chip
= {
299 .name
= "AR724X PCI ",
300 .irq_mask
= ar724x_pci_irq_mask
,
301 .irq_unmask
= ar724x_pci_irq_unmask
,
302 .irq_mask_ack
= ar724x_pci_irq_mask
,
305 static void ar724x_pci_irq_init(struct ar724x_pci_controller
*apc
,
311 base
= apc
->ctrl_base
;
313 __raw_writel(0, base
+ AR724X_PCI_REG_INT_MASK
);
314 __raw_writel(0, base
+ AR724X_PCI_REG_INT_STATUS
);
316 apc
->irq_base
= ATH79_PCI_IRQ_BASE
+ (id
* AR724X_PCI_IRQ_COUNT
);
318 for (i
= apc
->irq_base
;
319 i
< apc
->irq_base
+ AR724X_PCI_IRQ_COUNT
; i
++) {
320 irq_set_chip_and_handler(i
, &ar724x_pci_irq_chip
,
322 irq_set_chip_data(i
, apc
);
325 irq_set_chained_handler_and_data(apc
->irq
, ar724x_pci_irq_handler
,
329 static void ar724x_pci_hw_init(struct ar724x_pci_controller
*apc
)
334 /* deassert PCIe host controller and PCIe PHY reset */
335 ath79_device_reset_clear(AR724X_RESET_PCIE
);
336 ath79_device_reset_clear(AR724X_RESET_PCIE_PHY
);
338 /* remove the reset of the PCIE PLL */
339 ppl
= ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG
);
340 ppl
&= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_RESET
;
341 ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG
, ppl
);
343 /* deassert bypass for the PCIE PLL */
344 ppl
= ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG
);
345 ppl
&= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_BYPASS
;
346 ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG
, ppl
);
348 /* set PCIE Application Control to ready */
349 app
= __raw_readl(apc
->ctrl_base
+ AR724X_PCI_REG_APP
);
350 app
|= AR724X_PCI_APP_LTSSM_ENABLE
;
351 __raw_writel(app
, apc
->ctrl_base
+ AR724X_PCI_REG_APP
);
353 /* wait up to 100ms for PHY link up */
357 } while (wait
< 10 && !ar724x_pci_check_link(apc
));
360 static int ar724x_pci_probe(struct platform_device
*pdev
)
362 struct ar724x_pci_controller
*apc
;
363 struct resource
*res
;
370 apc
= devm_kzalloc(&pdev
->dev
, sizeof(struct ar724x_pci_controller
),
375 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ctrl_base");
376 apc
->ctrl_base
= devm_ioremap_resource(&pdev
->dev
, res
);
377 if (IS_ERR(apc
->ctrl_base
))
378 return PTR_ERR(apc
->ctrl_base
);
380 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cfg_base");
381 apc
->devcfg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
382 if (IS_ERR(apc
->devcfg_base
))
383 return PTR_ERR(apc
->devcfg_base
);
385 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "crp_base");
386 apc
->crp_base
= devm_ioremap_resource(&pdev
->dev
, res
);
387 if (IS_ERR(apc
->crp_base
))
388 return PTR_ERR(apc
->crp_base
);
390 apc
->irq
= platform_get_irq(pdev
, 0);
394 res
= platform_get_resource_byname(pdev
, IORESOURCE_IO
, "io_base");
398 apc
->io_res
.parent
= res
;
399 apc
->io_res
.name
= "PCI IO space";
400 apc
->io_res
.start
= res
->start
;
401 apc
->io_res
.end
= res
->end
;
402 apc
->io_res
.flags
= IORESOURCE_IO
;
404 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem_base");
408 apc
->mem_res
.parent
= res
;
409 apc
->mem_res
.name
= "PCI memory space";
410 apc
->mem_res
.start
= res
->start
;
411 apc
->mem_res
.end
= res
->end
;
412 apc
->mem_res
.flags
= IORESOURCE_MEM
;
414 apc
->pci_controller
.pci_ops
= &ar724x_pci_ops
;
415 apc
->pci_controller
.io_resource
= &apc
->io_res
;
416 apc
->pci_controller
.mem_resource
= &apc
->mem_res
;
419 * Do the full PCIE Root Complex Initialization Sequence if the PCIe
420 * host controller is in reset.
422 if (ath79_reset_rr(AR724X_RESET_REG_RESET_MODULE
) & AR724X_RESET_PCIE
)
423 ar724x_pci_hw_init(apc
);
425 apc
->link_up
= ar724x_pci_check_link(apc
);
427 dev_warn(&pdev
->dev
, "PCIe link is down\n");
429 ar724x_pci_irq_init(apc
, id
);
431 ar724x_pci_local_write(apc
, PCI_COMMAND
, 4, AR724X_PCI_CMD_INIT
);
433 register_pci_controller(&apc
->pci_controller
);
438 static struct platform_driver ar724x_pci_driver
= {
439 .probe
= ar724x_pci_probe
,
441 .name
= "ar724x-pci",
445 static int __init
ar724x_pci_init(void)
447 return platform_driver_register(&ar724x_pci_driver
);
450 postcore_initcall(ar724x_pci_init
);