pci-mt7620.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 PDRV_SW_SET BIT(23)
  31. #define PPLL_DRV 0xa0
  32. #define PDRV_SW_SET (1<<31)
  33. #define LC_CKDRVPD (1<<19)
  34. #define LC_CKDRVOHZ (1<<18)
  35. #define LC_CKDRVHZ (1<<17)
  36. #define LC_CKTEST (1<<16)
  37. /* PCI Bridge registers */
  38. #define RALINK_PCI_PCICFG_ADDR 0x00
  39. #define PCIRST BIT(1)
  40. #define RALINK_PCI_PCIENA 0x0C
  41. #define PCIINT2 BIT(20)
  42. #define RALINK_PCI_CONFIG_ADDR 0x20
  43. #define RALINK_PCI_CONFIG_DATA_VIRT_REG 0x24
  44. #define RALINK_PCI_MEMBASE 0x28
  45. #define RALINK_PCI_IOBASE 0x2C
  46. /* PCI RC registers */
  47. #define RALINK_PCI0_BAR0SETUP_ADDR 0x10
  48. #define RALINK_PCI0_IMBASEBAR0_ADDR 0x18
  49. #define RALINK_PCI0_ID 0x30
  50. #define RALINK_PCI0_CLASS 0x34
  51. #define RALINK_PCI0_SUBID 0x38
  52. #define RALINK_PCI0_STATUS 0x50
  53. #define PCIE_LINK_UP_ST BIT(0)
  54. #define PCIEPHY0_CFG 0x90
  55. #define RALINK_PCIEPHY_P0_CTL_OFFSET 0x7498
  56. #define RALINK_PCIE0_CLK_EN (1 << 26)
  57. #define BUSY 0x80000000
  58. #define WAITRETRY_MAX 10
  59. #define WRITE_MODE (1UL << 23)
  60. #define DATA_SHIFT 0
  61. #define ADDR_SHIFT 8
  62. static void __iomem *bridge_base;
  63. static void __iomem *pcie_base;
  64. static struct reset_control *rstpcie0;
  65. static inline void bridge_w32(u32 val, unsigned reg)
  66. {
  67. iowrite32(val, bridge_base + reg);
  68. }
  69. static inline u32 bridge_r32(unsigned reg)
  70. {
  71. return ioread32(bridge_base + reg);
  72. }
  73. static inline void pcie_w32(u32 val, unsigned reg)
  74. {
  75. iowrite32(val, pcie_base + reg);
  76. }
  77. static inline u32 pcie_r32(unsigned reg)
  78. {
  79. return ioread32(pcie_base + reg);
  80. }
  81. static inline void pcie_m32(u32 clr, u32 set, unsigned reg)
  82. {
  83. u32 val = pcie_r32(reg);
  84. val &= ~clr;
  85. val |= set;
  86. pcie_w32(val, reg);
  87. }
  88. static int wait_pciephy_busy(void)
  89. {
  90. unsigned long reg_value = 0x0, retry = 0;
  91. while (1) {
  92. reg_value = pcie_r32(PCIEPHY0_CFG);
  93. if (reg_value & BUSY)
  94. mdelay(100);
  95. else
  96. break;
  97. if (retry++ > WAITRETRY_MAX) {
  98. pr_warn("PCIE-PHY retry failed.\n");
  99. return -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static void pcie_phy(unsigned long addr, unsigned long val)
  105. {
  106. wait_pciephy_busy();
  107. pcie_w32(WRITE_MODE | (val << DATA_SHIFT) | (addr << ADDR_SHIFT),
  108. PCIEPHY0_CFG);
  109. mdelay(1);
  110. wait_pciephy_busy();
  111. }
  112. static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where,
  113. int size, u32 *val)
  114. {
  115. unsigned int slot = PCI_SLOT(devfn);
  116. u8 func = PCI_FUNC(devfn);
  117. u32 address;
  118. u32 data;
  119. u32 num = 0;
  120. if (bus)
  121. num = bus->number;
  122. address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) |
  123. (func << 8) | (where & 0xfc) | 0x80000000;
  124. bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  125. data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  126. switch (size) {
  127. case 1:
  128. *val = (data >> ((where & 3) << 3)) & 0xff;
  129. break;
  130. case 2:
  131. *val = (data >> ((where & 3) << 3)) & 0xffff;
  132. break;
  133. case 4:
  134. *val = data;
  135. break;
  136. }
  137. return PCIBIOS_SUCCESSFUL;
  138. }
  139. static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where,
  140. int size, u32 val)
  141. {
  142. unsigned int slot = PCI_SLOT(devfn);
  143. u8 func = PCI_FUNC(devfn);
  144. u32 address;
  145. u32 data;
  146. u32 num = 0;
  147. if (bus)
  148. num = bus->number;
  149. address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) |
  150. (func << 8) | (where & 0xfc) | 0x80000000;
  151. bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  152. data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  153. switch (size) {
  154. case 1:
  155. data = (data & ~(0xff << ((where & 3) << 3))) |
  156. (val << ((where & 3) << 3));
  157. break;
  158. case 2:
  159. data = (data & ~(0xffff << ((where & 3) << 3))) |
  160. (val << ((where & 3) << 3));
  161. break;
  162. case 4:
  163. data = val;
  164. break;
  165. }
  166. bridge_w32(data, RALINK_PCI_CONFIG_DATA_VIRT_REG);
  167. return PCIBIOS_SUCCESSFUL;
  168. }
  169. struct pci_ops mt7620_pci_ops = {
  170. .read = pci_config_read,
  171. .write = pci_config_write,
  172. };
  173. static struct resource mt7620_res_pci_mem1;
  174. static struct resource mt7620_res_pci_io1;
  175. struct pci_controller mt7620_controller = {
  176. .pci_ops = &mt7620_pci_ops,
  177. .mem_resource = &mt7620_res_pci_mem1,
  178. .mem_offset = 0x00000000UL,
  179. .io_resource = &mt7620_res_pci_io1,
  180. .io_offset = 0x00000000UL,
  181. .io_map_base = 0xa0000000,
  182. };
  183. static int mt7620_pci_hw_init(struct platform_device *pdev)
  184. {
  185. /* bypass PCIe DLL */
  186. pcie_phy(0x0, 0x80);
  187. pcie_phy(0x1, 0x04);
  188. /* Elastic buffer control */
  189. pcie_phy(0x68, 0xB4);
  190. /* put core into reset */
  191. pcie_m32(0, PCIRST, RALINK_PCI_PCICFG_ADDR);
  192. reset_control_assert(rstpcie0);
  193. /* disable power and all clocks */
  194. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  195. rt_sysc_m32(LC_CKDRVPD, PDRV_SW_SET, PPLL_DRV);
  196. /* bring core out of reset */
  197. reset_control_deassert(rstpcie0);
  198. rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  199. mdelay(100);
  200. if (!(rt_sysc_r32(PPLL_CFG1) & PDRV_SW_SET)) {
  201. dev_err(&pdev->dev, "MT7620 PPLL unlock\n");
  202. reset_control_assert(rstpcie0);
  203. rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  204. return -1;
  205. }
  206. /* power up the bus */
  207. rt_sysc_m32(LC_CKDRVHZ | LC_CKDRVOHZ, LC_CKDRVPD | PDRV_SW_SET,
  208. PPLL_DRV);
  209. return 0;
  210. }
  211. static int mt7628_pci_hw_init(struct platform_device *pdev)
  212. {
  213. u32 val = 0;
  214. /* bring the core out of reset */
  215. rt_sysc_m32(BIT(16), 0, RALINK_GPIOMODE);
  216. reset_control_deassert(rstpcie0);
  217. /* enable the pci clk */
  218. rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  219. mdelay(100);
  220. /* voodoo from the SDK driver */
  221. pcie_m32(~0xff, 0x5, RALINK_PCIEPHY_P0_CTL_OFFSET);
  222. pci_config_read(NULL, 0, 0x70c, 4, &val);
  223. val &= ~(0xff) << 8;
  224. val |= 0x50 << 8;
  225. pci_config_write(NULL, 0, 0x70c, 4, val);
  226. pci_config_read(NULL, 0, 0x70c, 4, &val);
  227. dev_err(&pdev->dev, "Port 0 N_FTS = %x\n", (unsigned int) val);
  228. return 0;
  229. }
  230. static int mt7620_pci_probe(struct platform_device *pdev)
  231. {
  232. struct resource *bridge_res = platform_get_resource(pdev,
  233. IORESOURCE_MEM, 0);
  234. struct resource *pcie_res = platform_get_resource(pdev,
  235. IORESOURCE_MEM, 1);
  236. u32 val = 0;
  237. rstpcie0 = devm_reset_control_get(&pdev->dev, "pcie0");
  238. if (IS_ERR(rstpcie0))
  239. return PTR_ERR(rstpcie0);
  240. bridge_base = devm_ioremap_resource(&pdev->dev, bridge_res);
  241. if (IS_ERR(bridge_base))
  242. return PTR_ERR(bridge_base);
  243. pcie_base = devm_ioremap_resource(&pdev->dev, pcie_res);
  244. if (IS_ERR(pcie_base))
  245. return PTR_ERR(pcie_base);
  246. iomem_resource.start = 0;
  247. iomem_resource.end = ~0;
  248. ioport_resource.start = 0;
  249. ioport_resource.end = ~0;
  250. /* bring up the pci core */
  251. switch (ralink_soc) {
  252. case MT762X_SOC_MT7620A:
  253. if (mt7620_pci_hw_init(pdev))
  254. return -1;
  255. break;
  256. case MT762X_SOC_MT7628AN:
  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 __init 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);