2 * Ralink MT7620A SoC PCI support
4 * Copyright (C) 2007-2013 Bruce Chang (Mediatek)
5 * Copyright (C) 2013-2016 John Crispin <blogic@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/types.h>
13 #include <linux/pci.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_pci.h>
22 #include <linux/reset.h>
23 #include <linux/platform_device.h>
25 #include <asm/mach-ralink/ralink_regs.h>
26 #include <asm/mach-ralink/mt7620.h>
28 #define RALINK_PCI_IO_MAP_BASE 0x10160000
29 #define RALINK_PCI_MEMORY_BASE 0x0
31 #define RALINK_INT_PCIE0 4
33 #define RALINK_CLKCFG1 0x30
34 #define RALINK_GPIOMODE 0x60
36 #define PPLL_CFG1 0x9c
37 #define PDRV_SW_SET BIT(23)
40 #define PDRV_SW_SET (1<<31)
41 #define LC_CKDRVPD (1<<19)
42 #define LC_CKDRVOHZ (1<<18)
43 #define LC_CKDRVHZ (1<<17)
44 #define LC_CKTEST (1<<16)
46 /* PCI Bridge registers */
47 #define RALINK_PCI_PCICFG_ADDR 0x00
50 #define RALINK_PCI_PCIENA 0x0C
51 #define PCIINT2 BIT(20)
53 #define RALINK_PCI_CONFIG_ADDR 0x20
54 #define RALINK_PCI_CONFIG_DATA_VIRT_REG 0x24
55 #define RALINK_PCI_MEMBASE 0x28
56 #define RALINK_PCI_IOBASE 0x2C
58 /* PCI RC registers */
59 #define RALINK_PCI0_BAR0SETUP_ADDR 0x10
60 #define RALINK_PCI0_IMBASEBAR0_ADDR 0x18
61 #define RALINK_PCI0_ID 0x30
62 #define RALINK_PCI0_CLASS 0x34
63 #define RALINK_PCI0_SUBID 0x38
64 #define RALINK_PCI0_STATUS 0x50
65 #define PCIE_LINK_UP_ST BIT(0)
67 #define PCIEPHY0_CFG 0x90
69 #define RALINK_PCIEPHY_P0_CTL_OFFSET 0x7498
70 #define RALINK_PCIE0_CLK_EN (1 << 26)
72 #define BUSY 0x80000000
73 #define WAITRETRY_MAX 10
74 #define WRITE_MODE (1UL << 23)
79 static void __iomem
*bridge_base
;
80 static void __iomem
*pcie_base
;
82 static struct reset_control
*rstpcie0
;
84 static inline void bridge_w32(u32 val
, unsigned reg
)
86 iowrite32(val
, bridge_base
+ reg
);
89 static inline u32
bridge_r32(unsigned reg
)
91 return ioread32(bridge_base
+ reg
);
94 static inline void pcie_w32(u32 val
, unsigned reg
)
96 iowrite32(val
, pcie_base
+ reg
);
99 static inline u32
pcie_r32(unsigned reg
)
101 return ioread32(pcie_base
+ reg
);
104 static inline void pcie_m32(u32 clr
, u32 set
, unsigned reg
)
106 u32 val
= pcie_r32(reg
);
113 static int wait_pciephy_busy(void)
115 unsigned long reg_value
= 0x0, retry
= 0;
118 reg_value
= pcie_r32(PCIEPHY0_CFG
);
120 if (reg_value
& BUSY
)
124 if (retry
++ > WAITRETRY_MAX
) {
125 printk(KERN_WARN
"PCIE-PHY retry failed.\n");
132 static void pcie_phy(unsigned long addr
, unsigned long val
)
135 pcie_w32(WRITE_MODE
| (val
<< DATA_SHIFT
) | (addr
<< ADDR_SHIFT
),
141 static int pci_config_read(struct pci_bus
*bus
, unsigned int devfn
, int where
,
144 unsigned int slot
= PCI_SLOT(devfn
);
145 u8 func
= PCI_FUNC(devfn
);
153 address
= (((where
& 0xF00) >> 8) << 24) | (num
<< 16) | (slot
<< 11) |
154 (func
<< 8) | (where
& 0xfc) | 0x80000000;
155 bridge_w32(address
, RALINK_PCI_CONFIG_ADDR
);
156 data
= bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG
);
160 *val
= (data
>> ((where
& 3) << 3)) & 0xff;
163 *val
= (data
>> ((where
& 3) << 3)) & 0xffff;
170 return PCIBIOS_SUCCESSFUL
;
173 static int pci_config_write(struct pci_bus
*bus
, unsigned int devfn
, int where
,
176 unsigned int slot
= PCI_SLOT(devfn
);
177 u8 func
= PCI_FUNC(devfn
);
185 address
= (((where
& 0xF00) >> 8) << 24) | (num
<< 16) | (slot
<< 11) |
186 (func
<< 8) | (where
& 0xfc) | 0x80000000;
187 bridge_w32(address
, RALINK_PCI_CONFIG_ADDR
);
188 data
= bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG
);
192 data
= (data
& ~(0xff << ((where
& 3) << 3))) |
193 (val
<< ((where
& 3) << 3));
196 data
= (data
& ~(0xffff << ((where
& 3) << 3))) |
197 (val
<< ((where
& 3) << 3));
204 bridge_w32(data
, RALINK_PCI_CONFIG_DATA_VIRT_REG
);
206 return PCIBIOS_SUCCESSFUL
;
209 struct pci_ops mt7620_pci_ops
= {
210 .read
= pci_config_read
,
211 .write
= pci_config_write
,
214 static struct resource mt7620_res_pci_mem1
;
215 static struct resource mt7620_res_pci_io1
;
216 struct pci_controller mt7620_controller
= {
217 .pci_ops
= &mt7620_pci_ops
,
218 .mem_resource
= &mt7620_res_pci_mem1
,
219 .mem_offset
= 0x00000000UL
,
220 .io_resource
= &mt7620_res_pci_io1
,
221 .io_offset
= 0x00000000UL
,
222 .io_map_base
= 0xa0000000,
225 static int mt7620_pci_hw_init(struct platform_device
*pdev
)
227 /* bypass PCIe DLL */
231 /* Elastic buffer control */
232 pcie_phy(0x68, 0xB4);
234 /* put core into reset */
235 pcie_m32(0, PCIRST
, RALINK_PCI_PCICFG_ADDR
);
236 reset_control_assert(rstpcie0
);
238 /* disable power and all clocks */
239 rt_sysc_m32(RALINK_PCIE0_CLK_EN
, 0, RALINK_CLKCFG1
);
240 rt_sysc_m32(LC_CKDRVPD
, PDRV_SW_SET
, PPLL_DRV
);
242 /* bring core out of reset */
243 reset_control_deassert(rstpcie0
);
244 rt_sysc_m32(0, RALINK_PCIE0_CLK_EN
, RALINK_CLKCFG1
);
247 if (!(rt_sysc_r32(PPLL_CFG1
) & PDRV_SW_SET
)) {
248 dev_err(&pdev
->dev
, "MT7620 PPLL unlock\n");
249 reset_control_assert(rstpcie0
);
250 rt_sysc_m32(RALINK_PCIE0_CLK_EN
, 0, RALINK_CLKCFG1
);
254 /* power up the bus */
255 rt_sysc_m32(LC_CKDRVHZ
| LC_CKDRVOHZ
, LC_CKDRVPD
| PDRV_SW_SET
,
261 static int mt7628_pci_hw_init(struct platform_device
*pdev
)
265 /* bring the core out of reset */
266 rt_sysc_m32(BIT(16), 0, RALINK_GPIOMODE
);
267 reset_control_deassert(rstpcie0
);
269 /* enable the pci clk */
270 rt_sysc_m32(0, RALINK_PCIE0_CLK_EN
, RALINK_CLKCFG1
);
273 /* voodoo from the SDK driver */
274 pcie_m32(~0xff, 0x5, RALINK_PCIEPHY_P0_CTL_OFFSET
);
276 pci_config_read(NULL
, 0, 0x70c, 4, &val
);
279 pci_config_write(NULL
, 0, 0x70c, 4, val
);
281 pci_config_read(NULL
, 0, 0x70c, 4, &val
);
282 dev_err(&pdev
->dev
, "Port 0 N_FTS = %x\n", (unsigned int) val
);
287 static int mt7620_pci_probe(struct platform_device
*pdev
)
289 struct resource
*bridge_res
= platform_get_resource(pdev
,
291 struct resource
*pcie_res
= platform_get_resource(pdev
,
295 rstpcie0
= devm_reset_control_get(&pdev
->dev
, "pcie0");
296 if (IS_ERR(rstpcie0
))
297 return PTR_ERR(rstpcie0
);
299 bridge_base
= devm_ioremap_resource(&pdev
->dev
, bridge_res
);
300 if (IS_ERR(bridge_base
))
301 return PTR_ERR(bridge_base
);
303 pcie_base
= devm_ioremap_resource(&pdev
->dev
, pcie_res
);
304 if (IS_ERR(pcie_base
))
305 return PTR_ERR(pcie_base
);
307 iomem_resource
.start
= 0;
308 iomem_resource
.end
= ~0;
309 ioport_resource
.start
= 0;
310 ioport_resource
.end
= ~0;
312 /* bring up the pci core */
313 switch (ralink_soc
) {
314 case MT762X_SOC_MT7620A
:
315 if (mt7620_pci_hw_init(pdev
))
319 case MT762X_SOC_MT7628AN
:
320 if (mt7628_pci_hw_init(pdev
))
325 dev_err(&pdev
->dev
, "pcie is not supported on this hardware\n");
330 /* enable write access */
331 pcie_m32(PCIRST
, 0, RALINK_PCI_PCICFG_ADDR
);
334 /* check if there is a card present */
335 if ((pcie_r32(RALINK_PCI0_STATUS
) & PCIE_LINK_UP_ST
) == 0) {
336 reset_control_assert(rstpcie0
);
337 rt_sysc_m32(RALINK_PCIE0_CLK_EN
, 0, RALINK_CLKCFG1
);
338 if (ralink_soc
== MT762X_SOC_MT7620A
)
339 rt_sysc_m32(LC_CKDRVPD
, PDRV_SW_SET
, PPLL_DRV
);
340 dev_err(&pdev
->dev
, "PCIE0 no card, disable it(RST&CLK)\n");
345 bridge_w32(0xffffffff, RALINK_PCI_MEMBASE
);
346 bridge_w32(RALINK_PCI_IO_MAP_BASE
, RALINK_PCI_IOBASE
);
348 pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR
);
349 pcie_w32(RALINK_PCI_MEMORY_BASE
, RALINK_PCI0_IMBASEBAR0_ADDR
);
350 pcie_w32(0x06040001, RALINK_PCI0_CLASS
);
352 /* enable interrupts */
353 pcie_m32(0, PCIINT2
, RALINK_PCI_PCIENA
);
355 /* voodoo from the SDK driver */
356 pci_config_read(NULL
, 0, 4, 4, &val
);
357 pci_config_write(NULL
, 0, 4, 4, val
| 0x7);
359 pci_load_of_ranges(&mt7620_controller
, pdev
->dev
.of_node
);
360 register_pci_controller(&mt7620_controller
);
365 int __init
pcibios_map_irq(const struct pci_dev
*dev
, u8 slot
, u8 pin
)
371 if ((dev
->bus
->number
== 0) && (slot
== 0)) {
372 pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR
);
373 pci_config_write(dev
->bus
, 0, PCI_BASE_ADDRESS_0
, 4,
374 RALINK_PCI_MEMORY_BASE
);
375 pci_config_read(dev
->bus
, 0, PCI_BASE_ADDRESS_0
, 4, &val
);
376 } else if ((dev
->bus
->number
== 1) && (slot
== 0x0)) {
377 irq
= RALINK_INT_PCIE0
;
379 dev_err(&dev
->dev
, "no irq found - bus=0x%x, slot = 0x%x\n",
380 dev
->bus
->number
, slot
);
383 dev_err(&dev
->dev
, "card - bus=0x%x, slot = 0x%x irq=%d\n",
384 dev
->bus
->number
, slot
, irq
);
386 /* configure the cache line size to 0x14 */
387 pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
, 0x14);
389 /* configure latency timer to 0xff */
390 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0xff);
391 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
394 cmd
= cmd
| PCI_COMMAND_MASTER
| PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
395 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
396 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, dev
->irq
);
401 int pcibios_plat_dev_init(struct pci_dev
*dev
)
406 static const struct of_device_id mt7620_pci_ids
[] = {
407 { .compatible
= "mediatek,mt7620-pci" },
410 MODULE_DEVICE_TABLE(of
, mt7620_pci_ids
);
412 static struct platform_driver mt7620_pci_driver
= {
413 .probe
= mt7620_pci_probe
,
415 .name
= "mt7620-pci",
416 .owner
= THIS_MODULE
,
417 .of_match_table
= of_match_ptr(mt7620_pci_ids
),
421 static int __init
mt7620_pci_init(void)
423 return platform_driver_register(&mt7620_pci_driver
);
426 arch_initcall(mt7620_pci_init
);