pci-mt7620.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Ralink MT7620A SoC PCI support
  3. *
  4. * Copyright (C) 2007-2013 Bruce Chang (Mediatek)
  5. * Copyright (C) 2013-2016 John Crispin <john@phrozen.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. #include <linux/types.h>
  12. #include <linux/pci.h>
  13. #include <linux/io.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/reset.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/mach-ralink/ralink_regs.h>
  24. #include <asm/mach-ralink/mt7620.h>
  25. #define RALINK_PCI_IO_MAP_BASE 0x10160000
  26. #define RALINK_PCI_MEMORY_BASE 0x0
  27. #define RALINK_INT_PCIE0 4
  28. #define RALINK_CLKCFG1 0x30
  29. #define RALINK_GPIOMODE 0x60
  30. #define PPLL_CFG1 0x9c
  31. #define PDRV_SW_SET BIT(23)
  32. #define PPLL_DRV 0xa0
  33. #define PDRV_SW_SET (1<<31)
  34. #define LC_CKDRVPD (1<<19)
  35. #define LC_CKDRVOHZ (1<<18)
  36. #define LC_CKDRVHZ (1<<17)
  37. #define LC_CKTEST (1<<16)
  38. /* PCI Bridge registers */
  39. #define RALINK_PCI_PCICFG_ADDR 0x00
  40. #define PCIRST BIT(1)
  41. #define RALINK_PCI_PCIENA 0x0C
  42. #define PCIINT2 BIT(20)
  43. #define RALINK_PCI_CONFIG_ADDR 0x20
  44. #define RALINK_PCI_CONFIG_DATA_VIRT_REG 0x24
  45. #define RALINK_PCI_MEMBASE 0x28
  46. #define RALINK_PCI_IOBASE 0x2C
  47. /* PCI RC registers */
  48. #define RALINK_PCI0_BAR0SETUP_ADDR 0x10
  49. #define RALINK_PCI0_IMBASEBAR0_ADDR 0x18
  50. #define RALINK_PCI0_ID 0x30
  51. #define RALINK_PCI0_CLASS 0x34
  52. #define RALINK_PCI0_SUBID 0x38
  53. #define RALINK_PCI0_STATUS 0x50
  54. #define PCIE_LINK_UP_ST BIT(0)
  55. #define PCIEPHY0_CFG 0x90
  56. #define RALINK_PCIEPHY_P0_CTL_OFFSET 0x7498
  57. #define RALINK_PCIE0_CLK_EN (1 << 26)
  58. #define BUSY 0x80000000
  59. #define WAITRETRY_MAX 10
  60. #define WRITE_MODE (1UL << 23)
  61. #define DATA_SHIFT 0
  62. #define ADDR_SHIFT 8
  63. static void __iomem *bridge_base;
  64. static void __iomem *pcie_base;
  65. static struct reset_control *rstpcie0;
  66. static inline void bridge_w32(u32 val, unsigned reg)
  67. {
  68. iowrite32(val, bridge_base + reg);
  69. }
  70. static inline u32 bridge_r32(unsigned reg)
  71. {
  72. return ioread32(bridge_base + reg);
  73. }
  74. static inline void pcie_w32(u32 val, unsigned reg)
  75. {
  76. iowrite32(val, pcie_base + reg);
  77. }
  78. static inline u32 pcie_r32(unsigned reg)
  79. {
  80. return ioread32(pcie_base + reg);
  81. }
  82. static inline void pcie_m32(u32 clr, u32 set, unsigned reg)
  83. {
  84. u32 val = pcie_r32(reg);
  85. val &= ~clr;
  86. val |= set;
  87. pcie_w32(val, reg);
  88. }
  89. static int wait_pciephy_busy(void)
  90. {
  91. unsigned long reg_value = 0x0, retry = 0;
  92. while (1) {
  93. reg_value = pcie_r32(PCIEPHY0_CFG);
  94. if (reg_value & BUSY)
  95. mdelay(100);
  96. else
  97. break;
  98. if (retry++ > WAITRETRY_MAX) {
  99. printk(KERN_WARN "PCIE-PHY retry failed.\n");
  100. return -1;
  101. }
  102. }
  103. return 0;
  104. }
  105. static void pcie_phy(unsigned long addr, unsigned long val)
  106. {
  107. wait_pciephy_busy();
  108. pcie_w32(WRITE_MODE | (val << DATA_SHIFT) | (addr << ADDR_SHIFT),
  109. PCIEPHY0_CFG);
  110. mdelay(1);
  111. wait_pciephy_busy();
  112. }
  113. static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where,
  114. int size, u32 *val)
  115. {
  116. unsigned int slot = PCI_SLOT(devfn);
  117. u8 func = PCI_FUNC(devfn);
  118. u32 address;
  119. u32 data;
  120. u32 num = 0;
  121. if (bus)
  122. num = bus->number;
  123. address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) |
  124. (func << 8) | (where & 0xfc) | 0x80000000;
  125. bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  126. data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  127. switch (size) {
  128. case 1:
  129. *val = (data >> ((where & 3) << 3)) & 0xff;
  130. break;
  131. case 2:
  132. *val = (data >> ((where & 3) << 3)) & 0xffff;
  133. break;
  134. case 4:
  135. *val = data;
  136. break;
  137. }
  138. return PCIBIOS_SUCCESSFUL;
  139. }
  140. static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where,
  141. int size, u32 val)
  142. {
  143. unsigned int slot = PCI_SLOT(devfn);
  144. u8 func = PCI_FUNC(devfn);
  145. u32 address;
  146. u32 data;
  147. u32 num = 0;
  148. if (bus)
  149. num = bus->number;
  150. address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) |
  151. (func << 8) | (where & 0xfc) | 0x80000000;
  152. bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  153. data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  154. switch (size) {
  155. case 1:
  156. data = (data & ~(0xff << ((where & 3) << 3))) |
  157. (val << ((where & 3) << 3));
  158. break;
  159. case 2:
  160. data = (data & ~(0xffff << ((where & 3) << 3))) |
  161. (val << ((where & 3) << 3));
  162. break;
  163. case 4:
  164. data = val;
  165. break;
  166. }
  167. bridge_w32(data, RALINK_PCI_CONFIG_DATA_VIRT_REG);
  168. return PCIBIOS_SUCCESSFUL;
  169. }
  170. struct pci_ops mt7620_pci_ops = {
  171. .read = pci_config_read,
  172. .write = pci_config_write,
  173. };
  174. static struct resource mt7620_res_pci_mem1;
  175. static struct resource mt7620_res_pci_io1;
  176. struct pci_controller mt7620_controller = {
  177. .pci_ops = &mt7620_pci_ops,
  178. .mem_resource = &mt7620_res_pci_mem1,
  179. .mem_offset = 0x00000000UL,
  180. .io_resource = &mt7620_res_pci_io1,
  181. .io_offset = 0x00000000UL,
  182. .io_map_base = 0xa0000000,
  183. };
  184. static int mt7620_pci_hw_init(struct platform_device *pdev)
  185. {
  186. /* bypass PCIe DLL */
  187. pcie_phy(0x0, 0x80);
  188. pcie_phy(0x1, 0x04);
  189. /* Elastic buffer control */
  190. pcie_phy(0x68, 0xB4);
  191. /* put core into reset */
  192. pcie_m32(0, PCIRST, RALINK_PCI_PCICFG_ADDR);
  193. reset_control_assert(rstpcie0);
  194. /* disable power and all clocks */
  195. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  196. rt_sysc_m32(LC_CKDRVPD, PDRV_SW_SET, PPLL_DRV);
  197. /* bring core out of reset */
  198. reset_control_deassert(rstpcie0);
  199. rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  200. mdelay(100);
  201. if (!(rt_sysc_r32(PPLL_CFG1) & PDRV_SW_SET)) {
  202. dev_err(&pdev->dev, "MT7620 PPLL unlock\n");
  203. reset_control_assert(rstpcie0);
  204. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  205. return -1;
  206. }
  207. /* power up the bus */
  208. rt_sysc_m32(LC_CKDRVHZ | LC_CKDRVOHZ, LC_CKDRVPD | PDRV_SW_SET,
  209. PPLL_DRV);
  210. return 0;
  211. }
  212. static int mt7628_pci_hw_init(struct platform_device *pdev)
  213. {
  214. u32 val = 0;
  215. /* bring the core out of reset */
  216. rt_sysc_m32(BIT(16), 0, RALINK_GPIOMODE);
  217. reset_control_deassert(rstpcie0);
  218. /* enable the pci clk */
  219. rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  220. mdelay(100);
  221. /* voodoo from the SDK driver */
  222. pcie_m32(~0xff, 0x5, RALINK_PCIEPHY_P0_CTL_OFFSET);
  223. pci_config_read(NULL, 0, 0x70c, 4, &val);
  224. val &= ~(0xff) << 8;
  225. val |= 0x50 << 8;
  226. pci_config_write(NULL, 0, 0x70c, 4, val);
  227. pci_config_read(NULL, 0, 0x70c, 4, &val);
  228. dev_err(&pdev->dev, "Port 0 N_FTS = %x\n", (unsigned int) val);
  229. return 0;
  230. }
  231. static int mt7620_pci_probe(struct platform_device *pdev)
  232. {
  233. struct resource *bridge_res = platform_get_resource(pdev,
  234. IORESOURCE_MEM, 0);
  235. struct resource *pcie_res = platform_get_resource(pdev,
  236. IORESOURCE_MEM, 1);
  237. u32 val = 0;
  238. rstpcie0 = devm_reset_control_get(&pdev->dev, "pcie0");
  239. if (IS_ERR(rstpcie0))
  240. return PTR_ERR(rstpcie0);
  241. bridge_base = devm_ioremap_resource(&pdev->dev, bridge_res);
  242. if (IS_ERR(bridge_base))
  243. return PTR_ERR(bridge_base);
  244. pcie_base = devm_ioremap_resource(&pdev->dev, pcie_res);
  245. if (IS_ERR(pcie_base))
  246. return PTR_ERR(pcie_base);
  247. iomem_resource.start = 0;
  248. iomem_resource.end = ~0;
  249. ioport_resource.start = 0;
  250. ioport_resource.end = ~0;
  251. /* bring up the pci core */
  252. switch (ralink_soc) {
  253. case MT762X_SOC_MT7620A:
  254. if (mt7620_pci_hw_init(pdev))
  255. return -1;
  256. break;
  257. case MT762X_SOC_MT7628AN:
  258. if (mt7628_pci_hw_init(pdev))
  259. return -1;
  260. break;
  261. default:
  262. dev_err(&pdev->dev, "pcie is not supported on this hardware\n");
  263. return -1;
  264. }
  265. mdelay(50);
  266. /* enable write access */
  267. pcie_m32(PCIRST, 0, RALINK_PCI_PCICFG_ADDR);
  268. mdelay(100);
  269. /* check if there is a card present */
  270. if ((pcie_r32(RALINK_PCI0_STATUS) & PCIE_LINK_UP_ST) == 0) {
  271. reset_control_assert(rstpcie0);
  272. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  273. if (ralink_soc == MT762X_SOC_MT7620A)
  274. rt_sysc_m32(LC_CKDRVPD, PDRV_SW_SET, PPLL_DRV);
  275. dev_err(&pdev->dev, "PCIE0 no card, disable it(RST&CLK)\n");
  276. return -1;
  277. }
  278. /* setup ranges */
  279. bridge_w32(0xffffffff, RALINK_PCI_MEMBASE);
  280. bridge_w32(RALINK_PCI_IO_MAP_BASE, RALINK_PCI_IOBASE);
  281. pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR);
  282. pcie_w32(RALINK_PCI_MEMORY_BASE, RALINK_PCI0_IMBASEBAR0_ADDR);
  283. pcie_w32(0x06040001, RALINK_PCI0_CLASS);
  284. /* enable interrupts */
  285. pcie_m32(0, PCIINT2, RALINK_PCI_PCIENA);
  286. /* voodoo from the SDK driver */
  287. pci_config_read(NULL, 0, 4, 4, &val);
  288. pci_config_write(NULL, 0, 4, 4, val | 0x7);
  289. pci_load_of_ranges(&mt7620_controller, pdev->dev.of_node);
  290. register_pci_controller(&mt7620_controller);
  291. return 0;
  292. }
  293. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  294. {
  295. u16 cmd;
  296. u32 val;
  297. int irq = 0;
  298. if ((dev->bus->number == 0) && (slot == 0)) {
  299. pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR);
  300. pci_config_write(dev->bus, 0, PCI_BASE_ADDRESS_0, 4,
  301. RALINK_PCI_MEMORY_BASE);
  302. pci_config_read(dev->bus, 0, PCI_BASE_ADDRESS_0, 4, &val);
  303. } else if ((dev->bus->number == 1) && (slot == 0x0)) {
  304. irq = RALINK_INT_PCIE0;
  305. } else {
  306. dev_err(&dev->dev, "no irq found - bus=0x%x, slot = 0x%x\n",
  307. dev->bus->number, slot);
  308. return 0;
  309. }
  310. dev_err(&dev->dev, "card - bus=0x%x, slot = 0x%x irq=%d\n",
  311. dev->bus->number, slot, irq);
  312. /* configure the cache line size to 0x14 */
  313. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14);
  314. /* configure latency timer to 0xff */
  315. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xff);
  316. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  317. /* setup the slot */
  318. cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  319. pci_write_config_word(dev, PCI_COMMAND, cmd);
  320. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  321. return irq;
  322. }
  323. int pcibios_plat_dev_init(struct pci_dev *dev)
  324. {
  325. return 0;
  326. }
  327. static const struct of_device_id mt7620_pci_ids[] = {
  328. { .compatible = "mediatek,mt7620-pci" },
  329. {},
  330. };
  331. MODULE_DEVICE_TABLE(of, mt7620_pci_ids);
  332. static struct platform_driver mt7620_pci_driver = {
  333. .probe = mt7620_pci_probe,
  334. .driver = {
  335. .name = "mt7620-pci",
  336. .owner = THIS_MODULE,
  337. .of_match_table = of_match_ptr(mt7620_pci_ids),
  338. },
  339. };
  340. static int __init mt7620_pci_init(void)
  341. {
  342. return platform_driver_register(&mt7620_pci_driver);
  343. }
  344. arch_initcall(mt7620_pci_init);