0032-PCI-MIPS-adds-mt7620a-pcie-driver.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. From 307b7a71a634ae3848fb7c5c05759d647e140e12 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sat, 18 May 2013 22:06:15 +0200
  4. Subject: [PATCH 32/57] PCI: MIPS: adds mt7620a pcie driver
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. arch/mips/pci/Makefile | 1 +
  8. arch/mips/pci/pci-mt7620.c | 363 +++++++++++++++++++++++++++++++++++++++++++
  9. arch/mips/ralink/Kconfig | 1 +
  10. 3 files changed, 365 insertions(+)
  11. create mode 100644 arch/mips/pci/pci-mt7620.c
  12. --- a/arch/mips/pci/Makefile
  13. +++ b/arch/mips/pci/Makefile
  14. @@ -43,6 +43,7 @@ obj-$(CONFIG_SNI_RM) += fixup-sni.o ops
  15. obj-$(CONFIG_LANTIQ) += fixup-lantiq.o
  16. obj-$(CONFIG_PCI_LANTIQ) += pci-lantiq.o ops-lantiq.o
  17. obj-$(CONFIG_SOC_MT7621) += pci-mt7621.o
  18. +obj-$(CONFIG_SOC_MT7620) += pci-mt7620.o
  19. obj-$(CONFIG_SOC_RT2880) += pci-rt2880.o
  20. obj-$(CONFIG_SOC_RT3883) += pci-rt3883.o
  21. obj-$(CONFIG_TANBAC_TB0219) += fixup-tb0219.o
  22. --- /dev/null
  23. +++ b/arch/mips/pci/pci-mt7620.c
  24. @@ -0,0 +1,396 @@
  25. +/*
  26. + * Ralink MT7620A SoC PCI support
  27. + *
  28. + * Copyright (C) 2007-2013 Bruce Chang
  29. + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  30. + *
  31. + * This program is free software; you can redistribute it and/or modify it
  32. + * under the terms of the GNU General Public License version 2 as published
  33. + * by the Free Software Foundation.
  34. + */
  35. +
  36. +#include <linux/types.h>
  37. +#include <linux/pci.h>
  38. +#include <linux/io.h>
  39. +#include <linux/init.h>
  40. +#include <linux/delay.h>
  41. +#include <linux/interrupt.h>
  42. +#include <linux/module.h>
  43. +#include <linux/of.h>
  44. +#include <linux/of_irq.h>
  45. +#include <linux/of_pci.h>
  46. +#include <linux/reset.h>
  47. +#include <linux/platform_device.h>
  48. +
  49. +#include <asm/mach-ralink/ralink_regs.h>
  50. +#include <asm/mach-ralink/mt7620.h>
  51. +
  52. +#define RALINK_PCI_MM_MAP_BASE 0x20000000
  53. +#define RALINK_PCI_IO_MAP_BASE 0x10160000
  54. +
  55. +#define RALINK_INT_PCIE0 4
  56. +#define RALINK_SYSCFG1 0x14
  57. +#define RALINK_CLKCFG1 0x30
  58. +#define RALINK_GPIOMODE 0x60
  59. +#define RALINK_PCIE_CLK_GEN 0x7c
  60. +#define RALINK_PCIE_CLK_GEN1 0x80
  61. +#define PCIEPHY0_CFG 0x90
  62. +#define PPLL_CFG1 0x9c
  63. +#define PPLL_DRV 0xa0
  64. +#define PDRV_SW_SET (1<<31)
  65. +#define LC_CKDRVPD_ (1<<19)
  66. +
  67. +#define RALINK_PCI_CONFIG_ADDR 0x20
  68. +#define RALINK_PCI_CONFIG_DATA_VIRT_REG 0x24
  69. +#define MEMORY_BASE 0x0
  70. +#define RALINK_PCIE0_RST (1<<26)
  71. +#define RALINK_PCI_BASE 0xB0140000
  72. +#define RALINK_PCI_MEMBASE 0x28
  73. +#define RALINK_PCI_IOBASE 0x2C
  74. +
  75. +#define RT6855_PCIE0_OFFSET 0x2000
  76. +
  77. +#define RALINK_PCI_PCICFG_ADDR 0x00
  78. +#define RALINK_PCI0_BAR0SETUP_ADDR 0x10
  79. +#define RALINK_PCI0_IMBASEBAR0_ADDR 0x18
  80. +#define RALINK_PCI0_ID 0x30
  81. +#define RALINK_PCI0_CLASS 0x34
  82. +#define RALINK_PCI0_SUBID 0x38
  83. +#define RALINK_PCI0_STATUS 0x50
  84. +#define RALINK_PCI_PCIMSK_ADDR 0x0C
  85. +
  86. +#define RALINK_PCIEPHY_P0_CTL_OFFSET 0x7498
  87. +#define RALINK_PCIE0_CLK_EN (1 << 26)
  88. +
  89. +#define BUSY 0x80000000
  90. +#define WAITRETRY_MAX 10
  91. +#define WRITE_MODE (1UL << 23)
  92. +#define DATA_SHIFT 0
  93. +#define ADDR_SHIFT 8
  94. +
  95. +static void __iomem *bridge_base;
  96. +static void __iomem *pcie_base;
  97. +
  98. +static struct reset_control *rstpcie0;
  99. +
  100. +static inline void bridge_w32(u32 val, unsigned reg)
  101. +{
  102. + iowrite32(val, bridge_base + reg);
  103. +}
  104. +
  105. +static inline u32 bridge_r32(unsigned reg)
  106. +{
  107. + return ioread32(bridge_base + reg);
  108. +}
  109. +
  110. +static inline void pcie_w32(u32 val, unsigned reg)
  111. +{
  112. + iowrite32(val, pcie_base + reg);
  113. +}
  114. +
  115. +static inline u32 pcie_r32(unsigned reg)
  116. +{
  117. + return ioread32(pcie_base + reg);
  118. +}
  119. +
  120. +static inline void pcie_m32(u32 clr, u32 set, unsigned reg)
  121. +{
  122. + u32 val = pcie_r32(reg);
  123. +
  124. + val &= ~clr;
  125. + val |= set;
  126. + pcie_w32(val, reg);
  127. +}
  128. +
  129. +static int wait_pciephy_busy(void)
  130. +{
  131. + unsigned long reg_value = 0x0, retry = 0;
  132. +
  133. + while (1) {
  134. + reg_value = pcie_r32(PCIEPHY0_CFG);
  135. +
  136. + if (reg_value & BUSY)
  137. + mdelay(100);
  138. + else
  139. + break;
  140. + if (retry++ > WAITRETRY_MAX){
  141. + printk("PCIE-PHY retry failed.\n");
  142. + return -1;
  143. + }
  144. + }
  145. + return 0;
  146. +}
  147. +
  148. +static void pcie_phy(unsigned long addr, unsigned long val)
  149. +{
  150. + wait_pciephy_busy();
  151. + pcie_w32(WRITE_MODE | (val << DATA_SHIFT) | (addr << ADDR_SHIFT), PCIEPHY0_CFG);
  152. + mdelay(1);
  153. + wait_pciephy_busy();
  154. +}
  155. +
  156. +static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
  157. +{
  158. + unsigned int slot = PCI_SLOT(devfn);
  159. + u8 func = PCI_FUNC(devfn);
  160. + u32 address;
  161. + u32 data;
  162. + u32 num = 0;
  163. +
  164. + if (bus)
  165. + num = bus->number;
  166. +
  167. + address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 0x80000000;
  168. + bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  169. + data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  170. +
  171. + switch (size) {
  172. + case 1:
  173. + *val = (data >> ((where & 3) << 3)) & 0xff;
  174. + break;
  175. + case 2:
  176. + *val = (data >> ((where & 3) << 3)) & 0xffff;
  177. + break;
  178. + case 4:
  179. + *val = data;
  180. + break;
  181. + }
  182. +
  183. + return PCIBIOS_SUCCESSFUL;
  184. +}
  185. +
  186. +static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
  187. +{
  188. + unsigned int slot = PCI_SLOT(devfn);
  189. + u8 func = PCI_FUNC(devfn);
  190. + u32 address;
  191. + u32 data;
  192. + u32 num = 0;
  193. +
  194. + if (bus)
  195. + num = bus->number;
  196. +
  197. + address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 0x80000000;
  198. + bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
  199. + data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRT_REG);
  200. +
  201. + switch (size) {
  202. + case 1:
  203. + data = (data & ~(0xff << ((where & 3) << 3))) |
  204. + (val << ((where & 3) << 3));
  205. + break;
  206. + case 2:
  207. + data = (data & ~(0xffff << ((where & 3) << 3))) |
  208. + (val << ((where & 3) << 3));
  209. + break;
  210. + case 4:
  211. + data = val;
  212. + break;
  213. + }
  214. +
  215. + bridge_w32(data, RALINK_PCI_CONFIG_DATA_VIRT_REG);
  216. +
  217. + return PCIBIOS_SUCCESSFUL;
  218. +}
  219. +
  220. +struct pci_ops mt7620_pci_ops= {
  221. + .read = pci_config_read,
  222. + .write = pci_config_write,
  223. +};
  224. +
  225. +static struct resource mt7620_res_pci_mem1;
  226. +static struct resource mt7620_res_pci_io1;
  227. +struct pci_controller mt7620_controller = {
  228. + .pci_ops = &mt7620_pci_ops,
  229. + .mem_resource = &mt7620_res_pci_mem1,
  230. + .mem_offset = 0x00000000UL,
  231. + .io_resource = &mt7620_res_pci_io1,
  232. + .io_offset = 0x00000000UL,
  233. + .io_map_base = 0xa0000000,
  234. +};
  235. +
  236. +static int mt7620_pci_hw_init(struct platform_device *pdev) {
  237. + /* PCIE: bypass PCIe DLL */
  238. + pcie_phy(0x0, 0x80);
  239. + pcie_phy(0x1, 0x04);
  240. +
  241. + /* PCIE: Elastic buffer control */
  242. + pcie_phy(0x68, 0xB4);
  243. +
  244. + pcie_m32(0, BIT(1), RALINK_PCI_PCICFG_ADDR);
  245. +
  246. + reset_control_assert(rstpcie0);
  247. +
  248. + rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  249. + rt_sysc_m32(BIT(19), BIT(31), PPLL_DRV);
  250. +
  251. + reset_control_deassert(rstpcie0);
  252. + rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  253. +
  254. + mdelay(100);
  255. +
  256. + if (!(rt_sysc_r32(PPLL_CFG1) & BIT(23))) {
  257. + dev_err(&pdev->dev, "MT7620 PPLL unlock\n");
  258. + reset_control_assert(rstpcie0);
  259. + rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  260. + return -1;
  261. + }
  262. + rt_sysc_m32(BIT(18) | BIT(17), BIT(19) | BIT(31), PPLL_DRV);
  263. +
  264. + return 0;
  265. +}
  266. +
  267. +static int mt7628_pci_hw_init(struct platform_device *pdev) {
  268. + u32 val = 0;
  269. +
  270. + rt_sysc_m32(BIT(16), 0, RALINK_GPIOMODE);
  271. + reset_control_deassert(rstpcie0);
  272. + rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
  273. + mdelay(100);
  274. +
  275. + pcie_m32(~0xff, 0x5, RALINK_PCIEPHY_P0_CTL_OFFSET);
  276. +
  277. + pci_config_read(NULL, 0, 0x70c, 4, &val);
  278. + val &= ~(0xff) << 8;
  279. + val |= 0x50 << 8;
  280. + pci_config_write(NULL, 0, 0x70c, 4, val);
  281. +
  282. + pci_config_read(NULL, 0, 0x70c, 4, &val);
  283. + dev_err(&pdev->dev, "Port 0 N_FTS = %x\n", (unsigned int) val);
  284. +
  285. + return 0;
  286. +}
  287. +
  288. +static int mt7620_pci_probe(struct platform_device *pdev)
  289. +{
  290. + struct resource *bridge_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  291. + struct resource *pcie_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  292. + u32 val = 0;
  293. +
  294. + rstpcie0 = devm_reset_control_get(&pdev->dev, "pcie0");
  295. + if (IS_ERR(rstpcie0))
  296. + return PTR_ERR(rstpcie0);
  297. +
  298. + bridge_base = devm_ioremap_resource(&pdev->dev, bridge_res);
  299. + if (!bridge_base)
  300. + return -ENOMEM;
  301. +
  302. + pcie_base = devm_ioremap_resource(&pdev->dev, pcie_res);
  303. + if (!pcie_base)
  304. + return -ENOMEM;
  305. +
  306. + iomem_resource.start = 0;
  307. + iomem_resource.end = ~0;
  308. + ioport_resource.start = 0;
  309. + ioport_resource.end = ~0;
  310. +
  311. + /* bring up the pci core */
  312. + switch (ralink_soc) {
  313. + case MT762X_SOC_MT7620A:
  314. + if (mt7620_pci_hw_init(pdev))
  315. + return -1;
  316. + break;
  317. +
  318. + case MT762X_SOC_MT7628AN:
  319. + if (mt7628_pci_hw_init(pdev))
  320. + return -1;
  321. + break;
  322. +
  323. + default:
  324. + dev_err(&pdev->dev, "pcie is not supported on this hardware\n");
  325. + return -1;
  326. + }
  327. + mdelay(50);
  328. +
  329. + /* enable write access */
  330. + pcie_m32(BIT(1), 0, RALINK_PCI_PCICFG_ADDR);
  331. + mdelay(100);
  332. +
  333. + /* check if there is a card present */
  334. + if ((pcie_r32(RALINK_PCI0_STATUS) & 0x1) == 0) {
  335. + reset_control_assert(rstpcie0);
  336. + rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
  337. + if (ralink_soc == MT762X_SOC_MT7620A)
  338. + rt_sysc_m32(LC_CKDRVPD_, PDRV_SW_SET, PPLL_DRV);
  339. + dev_err(&pdev->dev, "PCIE0 no card, disable it(RST&CLK)\n");
  340. + return -1;
  341. + }
  342. +
  343. + /* setup ranges */
  344. + bridge_w32(0xffffffff, RALINK_PCI_MEMBASE);
  345. + bridge_w32(RALINK_PCI_IO_MAP_BASE, RALINK_PCI_IOBASE);
  346. +
  347. + pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR);
  348. + pcie_w32(MEMORY_BASE, RALINK_PCI0_IMBASEBAR0_ADDR);
  349. + pcie_w32(0x06040001, RALINK_PCI0_CLASS);
  350. +
  351. + /* enable interrupts */
  352. + pcie_m32(0, BIT(20), RALINK_PCI_PCIMSK_ADDR);
  353. +
  354. + /* voodoo from the SDK driver */
  355. + pci_config_read(NULL, 0, 4, 4, &val);
  356. + pci_config_write(NULL, 0, 4, 4, val | 0x7);
  357. +
  358. + pci_load_of_ranges(&mt7620_controller, pdev->dev.of_node);
  359. + register_pci_controller(&mt7620_controller);
  360. +
  361. + return 0;
  362. +}
  363. +
  364. +int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  365. +{
  366. + u16 cmd;
  367. + u32 val;
  368. + int irq = 0;
  369. +
  370. + if ((dev->bus->number == 0) && (slot == 0)) {
  371. + pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR); //open 7FFF:2G; ENABLE
  372. + pci_config_write(dev->bus, 0, PCI_BASE_ADDRESS_0, 4, MEMORY_BASE);
  373. + pci_config_read(dev->bus, 0, PCI_BASE_ADDRESS_0, 4, &val);
  374. + } else if ((dev->bus->number == 1) && (slot == 0x0)) {
  375. + irq = RALINK_INT_PCIE0;
  376. + } else {
  377. + dev_err(&dev->dev, "no irq found - bus=0x%x, slot = 0x%x\n", dev->bus->number, slot);
  378. + return 0;
  379. + }
  380. + dev_err(&dev->dev, "card - bus=0x%x, slot = 0x%x irq=%d\n", dev->bus->number, slot, irq);
  381. +
  382. + pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14); //configure cache line size 0x14
  383. + pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xFF); //configure latency timer 0x10
  384. + pci_read_config_word(dev, PCI_COMMAND, &cmd);
  385. +
  386. + // FIXME
  387. + cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  388. + pci_write_config_word(dev, PCI_COMMAND, cmd);
  389. + pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  390. + //pci_write_config_byte(dev, PCI_INTERRUPT_PIN, dev->irq);
  391. +
  392. + return irq;
  393. +}
  394. +
  395. +int pcibios_plat_dev_init(struct pci_dev *dev)
  396. +{
  397. + return 0;
  398. +}
  399. +
  400. +static const struct of_device_id mt7620_pci_ids[] = {
  401. + { .compatible = "mediatek,mt7620-pci" },
  402. + {},
  403. +};
  404. +MODULE_DEVICE_TABLE(of, mt7620_pci_ids);
  405. +
  406. +static struct platform_driver mt7620_pci_driver = {
  407. + .probe = mt7620_pci_probe,
  408. + .driver = {
  409. + .name = "mt7620-pci",
  410. + .owner = THIS_MODULE,
  411. + .of_match_table = of_match_ptr(mt7620_pci_ids),
  412. + },
  413. +};
  414. +
  415. +static int __init mt7620_pci_init(void)
  416. +{
  417. + return platform_driver_register(&mt7620_pci_driver);
  418. +}
  419. +
  420. +arch_initcall(mt7620_pci_init);
  421. --- a/arch/mips/ralink/Kconfig
  422. +++ b/arch/mips/ralink/Kconfig
  423. @@ -43,6 +43,7 @@ choice
  424. config SOC_MT7620
  425. bool "MT7620/8"
  426. + select HW_HAS_PCI
  427. config SOC_MT7621
  428. bool "MT7621"
  429. --- a/arch/mips/include/asm/mach-ralink/mt7620.h
  430. +++ b/arch/mips/include/asm/mach-ralink/mt7620.h
  431. @@ -19,6 +19,7 @@ enum mt762x_soc_type {
  432. MT762X_SOC_MT7620N,
  433. MT762X_SOC_MT7628AN,
  434. };
  435. +extern enum mt762x_soc_type mt762x_soc;
  436. #define MT7620_SYSC_BASE 0x10000000