pci-mt7620.c 9.8 KB

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