0025-PCI-mediatek-add-support-for-PCIe-found-on-MT7623-MT.patch 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. From 8ab1d4e0a9a68e03f472dee1c036a01d0198c20c Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Tue, 5 Jan 2016 20:20:04 +0100
  4. Subject: [PATCH 025/102] PCI: mediatek: add support for PCIe found on
  5. MT7623/MT2701
  6. Add PCIe controller support on MediaTek MT2701/MT7623. The driver supports
  7. a single Root complex (RC) with 3 Root Ports. The SoCs supports a Gen2
  8. 1-lan Link on each port.
  9. Signed-off-by: John Crispin <blogic@openwrt.org>
  10. ---
  11. arch/arm/mach-mediatek/Kconfig | 1 +
  12. drivers/pci/host/Kconfig | 11 +
  13. drivers/pci/host/Makefile | 1 +
  14. drivers/pci/host/pcie-mediatek.c | 641 ++++++++++++++++++++++++++++++++++++++
  15. 4 files changed, 654 insertions(+)
  16. create mode 100644 drivers/pci/host/pcie-mediatek.c
  17. --- a/arch/arm/mach-mediatek/Kconfig
  18. +++ b/arch/arm/mach-mediatek/Kconfig
  19. @@ -24,6 +24,7 @@ config MACH_MT6592
  20. config MACH_MT7623
  21. bool "MediaTek MT7623 SoCs support"
  22. default ARCH_MEDIATEK
  23. + select MIGHT_HAVE_PCI
  24. config MACH_MT8127
  25. bool "MediaTek MT8127 SoCs support"
  26. --- a/drivers/pci/host/Kconfig
  27. +++ b/drivers/pci/host/Kconfig
  28. @@ -173,4 +173,15 @@ config PCI_HISI
  29. help
  30. Say Y here if you want PCIe controller support on HiSilicon HIP05 SoC
  31. +config PCIE_MTK
  32. + bool "Mediatek PCIe Controller"
  33. + depends on MACH_MT2701 || MACH_MT7623
  34. + depends on OF
  35. + depends on PCI
  36. + help
  37. + Say Y here if you want to enable PCI controller support on Mediatek MT7623.
  38. + MT7623 PCIe supports single Root complex (RC) with 3 Root Ports.
  39. + Each port supports a Gen2 1-lan Link.
  40. + PCIe include one Host/PCI bridge and 3 PCIe MAC.
  41. +
  42. endmenu
  43. --- a/drivers/pci/host/Makefile
  44. +++ b/drivers/pci/host/Makefile
  45. @@ -20,3 +20,4 @@ obj-$(CONFIG_PCIE_IPROC_BCMA) += pcie-ip
  46. obj-$(CONFIG_PCIE_ALTERA) += pcie-altera.o
  47. obj-$(CONFIG_PCIE_ALTERA_MSI) += pcie-altera-msi.o
  48. obj-$(CONFIG_PCI_HISI) += pcie-hisi.o
  49. +obj-$(CONFIG_PCIE_MTK) += pcie-mediatek.o
  50. --- /dev/null
  51. +++ b/drivers/pci/host/pcie-mediatek.c
  52. @@ -0,0 +1,641 @@
  53. +/*
  54. + * Mediatek MT2701/MT7623 SoC PCIE support
  55. + *
  56. + * Copyright (C) 2015 Mediatek
  57. + * Copyright (C) 2015 Ziv Huang <ziv.huang@mediatek.com>
  58. + * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
  59. + *
  60. + * This program is free software; you can redistribute it and/or modify it
  61. + * under the terms of the GNU General Public License version 2 as published
  62. + * by the Free Software Foundation.
  63. + */
  64. +
  65. +#include <linux/kernel.h>
  66. +#include <linux/pci.h>
  67. +#include <linux/ioport.h>
  68. +#include <linux/interrupt.h>
  69. +#include <linux/spinlock.h>
  70. +#include <linux/init.h>
  71. +#include <linux/device.h>
  72. +#include <linux/io.h>
  73. +#include <linux/delay.h>
  74. +#include <asm/irq.h>
  75. +#include <asm/mach/pci.h>
  76. +#include <linux/module.h>
  77. +#include <linux/of.h>
  78. +#include <linux/of_address.h>
  79. +#include <linux/of_pci.h>
  80. +#include <linux/of_platform.h>
  81. +#include <linux/of_irq.h>
  82. +#include <linux/reset.h>
  83. +#include <linux/platform_device.h>
  84. +#include <linux/regulator/consumer.h>
  85. +#include <linux/pm_runtime.h>
  86. +#include <linux/clk.h>
  87. +#include <linux/regmap.h>
  88. +#include <linux/mfd/syscon.h>
  89. +
  90. +#define MEMORY_BASE 0x80000000
  91. +
  92. +/* PCIE Registers */
  93. +#define PCICFG 0x00
  94. +#define PCIINT 0x08
  95. +#define PCIENA 0x0c
  96. +#define CFGADDR 0x20
  97. +#define CFGDATA 0x24
  98. +#define MEMBASE 0x28
  99. +#define IOBASE 0x2c
  100. +
  101. +/* per Port Registers */
  102. +#define BAR0SETUP 0x10
  103. +#define IMBASEBAR0 0x18
  104. +#define PCIE_CLASS 0x34
  105. +#define PCIE_SISTAT 0x50
  106. +
  107. +#define MTK_PCIE_HIGH_PERF BIT(14)
  108. +#define PCIEP0_BASE 0x2000
  109. +#define PCIEP1_BASE 0x3000
  110. +#define PCIEP2_BASE 0x4000
  111. +
  112. +#define PHY_P0_CTL 0x9000
  113. +#define PHY_P1_CTL 0xa000
  114. +#define PHY_P2_CTL 0x4000
  115. +
  116. +#define RSTCTL_PCIE0_RST BIT(24)
  117. +#define RSTCTL_PCIE1_RST BIT(25)
  118. +#define RSTCTL_PCIE2_RST BIT(26)
  119. +
  120. +#define HIFSYS_SYSCFG1 0x14
  121. +#define HIFSYS_SYSCFG1_PHY2_MASK (0x3 << 20)
  122. +
  123. +#define MTK_PHY_CLK 0xb00
  124. +#define MTK_PHY_CLKDRV_OFFSET BIT(2)
  125. +#define MTK_PHY_CLKDRV_OFFSET_MASK 0xe
  126. +#define MTK_PHY_PLL 0xb04
  127. +#define MTK_PHY_CLKDRV_AMP BIT(30)
  128. +#define MTK_PHY_CLKDRV_AMP_MASK 0xe0000000
  129. +#define MTK_PHY_REFCLK_SEL 0xc00
  130. +#define MTK_PHY_XTAL_EXT_EN (BIT(17) | BIT(12))
  131. +#define MTK_PHY_XTAL_EXT_EN_MASK 0x33000
  132. +#define MTK_PHY_PLL_BC 0xc08
  133. +#define MTK_PHY_PLL_BC_PE2H 0xc0
  134. +#define MTK_PHY_PLL_BC_PE2H_MASK 0x380000
  135. +#define MTK_PHY_PLL_IC 0xc0c
  136. +#define MTK_PHY_PLL_IC_BR_PE2H BIT(28)
  137. +#define MTK_PHY_PLL_IC_BR_PE2H_MASK 0x30000000
  138. +#define MTK_PHY_PLL_IC_PE2H BIT(12)
  139. +#define MTK_PHY_PLL_IC_PE2H_MASK 0xf000
  140. +#define MTK_PHY_PLL_IR 0xc10
  141. +#define MTK_PHY_PLL_IR_PE2H BIT(17)
  142. +#define MTK_PHY_PLL_IR_PE2H_MASK 0xf0000
  143. +#define MTK_PHY_PLL_BP 0xc14
  144. +#define MTK_PHY_PLL_BP_PE2H (BIT(19) | BIT(17))
  145. +#define MTK_PHY_PLL_BP_PE2H_MASK 0xf0000
  146. +#define MTK_PHY_SSC_DELTA1 0xc3c
  147. +#define MTK_PHY_SSC_DELTA1_PE2H (0x3c << 16)
  148. +#define MTK_PHY_SSC_DELTA1_PE2H_MASK 0xffff0000
  149. +#define MTK_PHY_SSC_DELTA 0xc48
  150. +#define MTK_PHY_SSC_DELTA_PE2H 0x36
  151. +#define MTK_PHY_SSC_DELTA_PE2H_MASK 0xffff
  152. +
  153. +#define MAX_PORT_NUM 3
  154. +
  155. +struct mtk_pcie_port {
  156. + int id;
  157. + int enable;
  158. + int irq;
  159. + u32 link;
  160. + void __iomem *phy_base;
  161. + struct reset_control *rstc;
  162. +};
  163. +
  164. +#define mtk_foreach_port(pcie, p) \
  165. + for ((p) = pcie->port; \
  166. + (p) != &pcie->port[MAX_PORT_NUM]; (p)++)
  167. +
  168. +struct mtk_pcie {
  169. + struct device *dev;
  170. + void __iomem *pcie_base;
  171. + struct regmap *hifsys;
  172. +
  173. + struct resource io;
  174. + struct resource pio;
  175. + struct resource mem;
  176. + struct resource prefetch;
  177. + struct resource busn;
  178. +
  179. + u32 io_bus_addr;
  180. + u32 mem_bus_addr;
  181. +
  182. + struct clk *clk;
  183. +
  184. + struct mtk_pcie_port port[MAX_PORT_NUM];
  185. + int pcie_card_link;
  186. +};
  187. +
  188. +static struct mtk_pcie_port_data {
  189. + u32 base;
  190. + u32 perst_n;
  191. + u32 interrupt_en;
  192. +} mtk_pcie_port_data[MAX_PORT_NUM] = {
  193. + { PCIEP0_BASE, BIT(1), BIT(20) },
  194. + { PCIEP1_BASE, BIT(2), BIT(21) },
  195. + { PCIEP2_BASE, BIT(3), BIT(22) },
  196. +};
  197. +
  198. +static const struct mtk_phy_init {
  199. + uint32_t reg;
  200. + uint32_t mask;
  201. + uint32_t val;
  202. +} mtk_phy_init[] = {
  203. + { MTK_PHY_REFCLK_SEL, MTK_PHY_XTAL_EXT_EN_MASK, MTK_PHY_XTAL_EXT_EN },
  204. + { MTK_PHY_PLL, MTK_PHY_CLKDRV_AMP_MASK, MTK_PHY_CLKDRV_AMP },
  205. + { MTK_PHY_CLK, MTK_PHY_CLKDRV_OFFSET_MASK, MTK_PHY_CLKDRV_OFFSET },
  206. + { MTK_PHY_SSC_DELTA1, MTK_PHY_SSC_DELTA1_PE2H_MASK, MTK_PHY_SSC_DELTA1_PE2H },
  207. + { MTK_PHY_SSC_DELTA, MTK_PHY_SSC_DELTA_PE2H_MASK, MTK_PHY_SSC_DELTA_PE2H },
  208. + { MTK_PHY_PLL_IC, MTK_PHY_PLL_IC_BR_PE2H_MASK, MTK_PHY_PLL_IC_BR_PE2H },
  209. + { MTK_PHY_PLL_BC, MTK_PHY_PLL_BC_PE2H_MASK, MTK_PHY_PLL_BC_PE2H },
  210. + { MTK_PHY_PLL_IR, MTK_PHY_PLL_IR_PE2H_MASK, MTK_PHY_PLL_IR_PE2H },
  211. + { MTK_PHY_PLL_IC, MTK_PHY_PLL_IC_PE2H_MASK, MTK_PHY_PLL_IC_PE2H },
  212. + { MTK_PHY_PLL_BP, MTK_PHY_PLL_BP_PE2H_MASK, MTK_PHY_PLL_BP_PE2H },
  213. +};
  214. +
  215. +static struct mtk_pcie *sys_to_pcie(struct pci_sys_data *sys)
  216. +{
  217. + return sys->private_data;
  218. +}
  219. +
  220. +static void pcie_w32(struct mtk_pcie *pcie, u32 val, unsigned reg)
  221. +{
  222. + iowrite32(val, pcie->pcie_base + reg);
  223. +}
  224. +
  225. +static u32 pcie_r32(struct mtk_pcie *pcie, unsigned reg)
  226. +{
  227. + return ioread32(pcie->pcie_base + reg);
  228. +}
  229. +
  230. +static void pcie_m32(struct mtk_pcie *pcie, u32 mask, u32 val, unsigned reg)
  231. +{
  232. + u32 v = pcie_r32(pcie, reg);
  233. +
  234. + v &= mask;
  235. + v |= val;
  236. + pcie_w32(pcie, v, reg);
  237. +}
  238. +
  239. +static int pcie_config_read(struct pci_bus *bus, unsigned int devfn, int where,
  240. + int size, u32 *val)
  241. +{
  242. + struct mtk_pcie *pcie = sys_to_pcie(bus->sysdata);
  243. + unsigned int slot = PCI_SLOT(devfn);
  244. + u8 func = PCI_FUNC(devfn);
  245. + u32 address;
  246. + u32 data;
  247. + u32 num = 0;
  248. +
  249. + if (bus)
  250. + num = bus->number;
  251. +
  252. + address = (((where & 0xf00) >> 8) << 24) |
  253. + (num << 16) |
  254. + (slot << 11) |
  255. + (func << 8) |
  256. + (where & 0xfc);
  257. +
  258. + pcie_w32(pcie, address, CFGADDR);
  259. + data = pcie_r32(pcie, CFGDATA);
  260. +
  261. + switch (size) {
  262. + case 1:
  263. + *val = (data >> ((where & 3) << 3)) & 0xff;
  264. + break;
  265. + case 2:
  266. + *val = (data >> ((where & 3) << 3)) & 0xffff;
  267. + break;
  268. + case 4:
  269. + *val = data;
  270. + break;
  271. + }
  272. +
  273. + return PCIBIOS_SUCCESSFUL;
  274. +}
  275. +
  276. +static int pcie_config_write(struct pci_bus *bus, unsigned int devfn, int where,
  277. + int size, u32 val)
  278. +{
  279. + struct mtk_pcie *pcie = sys_to_pcie(bus->sysdata);
  280. + unsigned int slot = PCI_SLOT(devfn);
  281. + u8 func = PCI_FUNC(devfn);
  282. + u32 address;
  283. + u32 data;
  284. + u32 num = 0;
  285. +
  286. + if (bus)
  287. + num = bus->number;
  288. +
  289. + address = (((where & 0xf00) >> 8) << 24) |
  290. + (num << 16) | (slot << 11) | (func << 8) | (where & 0xfc);
  291. + pcie_w32(pcie, address, CFGADDR);
  292. + data = pcie_r32(pcie, CFGDATA);
  293. +
  294. + switch (size) {
  295. + case 1:
  296. + data = (data & ~(0xff << ((where & 3) << 3))) |
  297. + (val << ((where & 3) << 3));
  298. + break;
  299. + case 2:
  300. + data = (data & ~(0xffff << ((where & 3) << 3))) |
  301. + (val << ((where & 3) << 3));
  302. + break;
  303. + case 4:
  304. + data = val;
  305. + break;
  306. + }
  307. + pcie_w32(pcie, data, CFGDATA);
  308. +
  309. + return PCIBIOS_SUCCESSFUL;
  310. +}
  311. +
  312. +static struct pci_ops mtk_pcie_ops = {
  313. + .read = pcie_config_read,
  314. + .write = pcie_config_write,
  315. +};
  316. +
  317. +static int __init mtk_pcie_setup(int nr, struct pci_sys_data *sys)
  318. +{
  319. + struct mtk_pcie *pcie = sys_to_pcie(sys);
  320. +
  321. + request_resource(&ioport_resource, &pcie->pio);
  322. + request_resource(&iomem_resource, &pcie->mem);
  323. +
  324. + pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  325. + pci_add_resource_offset(&sys->resources, &pcie->pio, sys->io_offset);
  326. + pci_add_resource(&sys->resources, &pcie->busn);
  327. +
  328. + return 1;
  329. +}
  330. +
  331. +static struct pci_bus * __init mtk_pcie_scan_bus(int nr,
  332. + struct pci_sys_data *sys)
  333. +{
  334. + struct mtk_pcie *pcie = sys_to_pcie(sys);
  335. + struct pci_bus *bus;
  336. +
  337. + bus = pci_create_root_bus(pcie->dev, sys->busnr, &mtk_pcie_ops, sys,
  338. + &sys->resources);
  339. + if (!bus)
  340. + return NULL;
  341. +
  342. + pci_scan_child_bus(bus);
  343. +
  344. + return bus;
  345. +}
  346. +
  347. +static int __init mtk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  348. +{
  349. + struct mtk_pcie *pcie = sys_to_pcie(dev->bus->sysdata);
  350. + struct mtk_pcie_port *port;
  351. + int irq = -1;
  352. +
  353. + mtk_foreach_port(pcie, port)
  354. + if (port->id == slot)
  355. + irq = port->irq;
  356. +
  357. + return irq;
  358. +}
  359. +
  360. +static void mtk_pcie_configure_phy(struct mtk_pcie *pcie,
  361. + struct mtk_pcie_port *port)
  362. +{
  363. + int i;
  364. +
  365. + for (i = 0; i < ARRAY_SIZE(mtk_phy_init); i++) {
  366. + void __iomem *phy_addr = port->phy_base + mtk_phy_init[i].reg;
  367. + u32 val = ioread32(phy_addr);
  368. +
  369. + val &= ~mtk_phy_init[i].mask;
  370. + val |= mtk_phy_init[i].val;
  371. + iowrite32(val, phy_addr);
  372. + }
  373. + usleep_range(5000, 6000);
  374. +}
  375. +
  376. +static void mtk_pcie_configure_rc(struct mtk_pcie *pcie,
  377. + struct mtk_pcie_port *port,
  378. + struct pci_bus *bus)
  379. +{
  380. + u32 val = 0;
  381. +
  382. + pcie_config_write(bus,
  383. + port->id << 3,
  384. + PCI_BASE_ADDRESS_0, 4, MEMORY_BASE);
  385. +
  386. + pcie_config_read(bus,
  387. + port->id << 3, PCI_BASE_ADDRESS_0, 4, &val);
  388. +
  389. + /* Configure RC Credit */
  390. + pcie_config_read(bus, port->id << 3, 0x73c, 4, &val);
  391. + val &= ~(0x9fff) << 16;
  392. + val |= 0x806c << 16;
  393. + pcie_config_write(bus, port->id << 3, 0x73c, 4, val);
  394. +
  395. + /* Configure RC FTS number */
  396. + pcie_config_read(bus, port->id << 3, 0x70c, 4, &val);
  397. + val &= ~(0xff3) << 8;
  398. + val |= 0x50 << 8;
  399. + pcie_config_write(bus, port->id << 3, 0x70c, 4, val);
  400. +}
  401. +
  402. +static int mtk_pcie_preinit(struct mtk_pcie *pcie)
  403. +{
  404. + struct mtk_pcie_port *port;
  405. + u32 val = 0;
  406. + struct pci_bus bus;
  407. + struct pci_sys_data sys;
  408. +
  409. + memset(&bus, 0, sizeof(bus));
  410. + memset(&sys, 0, sizeof(sys));
  411. + bus.sysdata = (void *)&sys;
  412. + sys.private_data = (void *)pcie;
  413. +
  414. + pcibios_min_io = 0;
  415. + pcibios_min_mem = 0;
  416. +
  417. + /* The PHY on Port 2 is shared with USB */
  418. + if (pcie->port[2].enable)
  419. + regmap_update_bits(pcie->hifsys, HIFSYS_SYSCFG1,
  420. + HIFSYS_SYSCFG1_PHY2_MASK, 0x0);
  421. +
  422. + /* PCIe RC Reset */
  423. + mtk_foreach_port(pcie, port)
  424. + if (port->enable)
  425. + reset_control_assert(port->rstc);
  426. + usleep_range(1000, 2000);
  427. + mtk_foreach_port(pcie, port)
  428. + if (port->enable)
  429. + reset_control_deassert(port->rstc);
  430. + usleep_range(1000, 2000);
  431. +
  432. + /* Configure PCIe PHY */
  433. + mtk_foreach_port(pcie, port)
  434. + if (port->enable)
  435. + mtk_pcie_configure_phy(pcie, port);
  436. +
  437. + /* PCIe EP reset */
  438. + val = 0;
  439. + mtk_foreach_port(pcie, port)
  440. + if (port->enable)
  441. + val |= mtk_pcie_port_data[port->id].perst_n;
  442. + pcie_w32(pcie, pcie_r32(pcie, PCICFG) | val, PCICFG);
  443. + usleep_range(1000, 2000);
  444. + pcie_w32(pcie, pcie_r32(pcie, PCICFG) & ~val, PCICFG);
  445. + usleep_range(1000, 2000);
  446. + msleep(100);
  447. +
  448. + /* check the link status */
  449. + val = 0;
  450. + mtk_foreach_port(pcie, port) {
  451. + if (port->enable) {
  452. + u32 base = mtk_pcie_port_data[port->id].base;
  453. +
  454. + if ((pcie_r32(pcie, base + PCIE_SISTAT) & 0x1))
  455. + port->link = 1;
  456. + else
  457. + reset_control_assert(port->rstc);
  458. + }
  459. + }
  460. +
  461. + mtk_foreach_port(pcie, port)
  462. + if (port->link)
  463. + pcie->pcie_card_link++;
  464. +
  465. + if (!pcie->pcie_card_link)
  466. + return -ENODEV;
  467. +
  468. + pcie_w32(pcie, pcie->mem_bus_addr, MEMBASE);
  469. + pcie_w32(pcie, pcie->io_bus_addr, IOBASE);
  470. +
  471. + mtk_foreach_port(pcie, port) {
  472. + if (port->link) {
  473. + u32 base = mtk_pcie_port_data[port->id].base;
  474. + u32 inte = mtk_pcie_port_data[port->id].interrupt_en;
  475. +
  476. + pcie_m32(pcie, 0, inte, PCIENA);
  477. + pcie_w32(pcie, 0x7fff0001, base + BAR0SETUP);
  478. + pcie_w32(pcie, MEMORY_BASE, base + IMBASEBAR0);
  479. + pcie_w32(pcie, 0x06040001, base + PCIE_CLASS);
  480. + }
  481. + }
  482. +
  483. + mtk_foreach_port(pcie, port)
  484. + if (port->link)
  485. + mtk_pcie_configure_rc(pcie, port, &bus);
  486. +
  487. + return 0;
  488. +}
  489. +
  490. +static int mtk_pcie_parse_dt(struct mtk_pcie *pcie)
  491. +{
  492. + struct device_node *np = pcie->dev->of_node, *port;
  493. + struct of_pci_range_parser parser;
  494. + struct of_pci_range range;
  495. + struct resource res;
  496. + int err;
  497. +
  498. + pcie->hifsys = syscon_regmap_lookup_by_phandle(np, "mediatek,hifsys");
  499. + if (IS_ERR(pcie->hifsys)) {
  500. + dev_err(pcie->dev, "missing \"mediatek,hifsys\" phandle\n");
  501. + return PTR_ERR(pcie->hifsys);
  502. + }
  503. +
  504. + if (of_pci_range_parser_init(&parser, np)) {
  505. + dev_err(pcie->dev, "missing \"ranges\" property\n");
  506. + return -EINVAL;
  507. + }
  508. +
  509. + for_each_of_pci_range(&parser, &range) {
  510. + err = of_pci_range_to_resource(&range, np, &res);
  511. + if (err < 0) {
  512. + dev_err(pcie->dev, "failed to read resource range\n");
  513. + return err;
  514. + }
  515. +
  516. + switch (res.flags & IORESOURCE_TYPE_BITS) {
  517. + case IORESOURCE_IO:
  518. + memcpy(&pcie->pio, &res, sizeof(res));
  519. + pcie->pio.start = (resource_size_t)range.pci_addr;
  520. + pcie->pio.end = (resource_size_t)
  521. + (range.pci_addr + range.size - 1);
  522. + pcie->io_bus_addr = (resource_size_t)range.cpu_addr;
  523. + break;
  524. +
  525. + case IORESOURCE_MEM:
  526. + if (res.flags & IORESOURCE_PREFETCH) {
  527. + memcpy(&pcie->prefetch, &res, sizeof(res));
  528. + pcie->prefetch.name = "prefetchable";
  529. + pcie->prefetch.start =
  530. + (resource_size_t)range.pci_addr;
  531. + pcie->prefetch.end = (resource_size_t)
  532. + (range.pci_addr + range.size - 1);
  533. + } else {
  534. + memcpy(&pcie->mem, &res, sizeof(res));
  535. + pcie->mem.name = "non-prefetchable";
  536. + pcie->mem.start = (resource_size_t)
  537. + range.pci_addr;
  538. + pcie->prefetch.end = (resource_size_t)
  539. + (range.pci_addr + range.size - 1);
  540. + pcie->mem_bus_addr = (resource_size_t)
  541. + range.cpu_addr;
  542. + }
  543. + break;
  544. + }
  545. + }
  546. +
  547. + err = of_pci_parse_bus_range(np, &pcie->busn);
  548. + if (err < 0) {
  549. + dev_err(pcie->dev, "failed to parse ranges property: %d\n",
  550. + err);
  551. + pcie->busn.name = np->name;
  552. + pcie->busn.start = 0;
  553. + pcie->busn.end = 0xff;
  554. + pcie->busn.flags = IORESOURCE_BUS;
  555. + }
  556. +
  557. + /* parse root ports */
  558. + for_each_child_of_node(np, port) {
  559. + unsigned int index;
  560. + char rst[] = "pcie0";
  561. +
  562. + err = of_pci_get_devfn(port);
  563. + if (err < 0) {
  564. + dev_err(pcie->dev, "failed to parse address: %d\n",
  565. + err);
  566. + return err;
  567. + }
  568. +
  569. + index = PCI_SLOT(err);
  570. + if (index > MAX_PORT_NUM) {
  571. + dev_err(pcie->dev, "invalid port number: %d\n", index);
  572. + continue;
  573. + }
  574. + index--;
  575. + pcie->port[index].id = index;
  576. +
  577. + if (!of_device_is_available(port))
  578. + continue;
  579. +
  580. + rst[4] += index;
  581. + pcie->port[index].rstc = devm_reset_control_get(pcie->dev,
  582. + rst);
  583. + if (!IS_ERR(pcie->port[index].rstc))
  584. + pcie->port[index].enable = 1;
  585. + }
  586. + return 0;
  587. +}
  588. +
  589. +static int mtk_pcie_get_resources(struct mtk_pcie *pcie)
  590. +{
  591. + struct platform_device *pdev = to_platform_device(pcie->dev);
  592. + struct mtk_pcie_port *port;
  593. + struct resource *res;
  594. +
  595. + pcie->clk = devm_clk_get(&pdev->dev, "pcie");
  596. + if (IS_ERR(pcie->clk)) {
  597. + dev_err(&pdev->dev, "Failed to get pcie clk\n");
  598. + return PTR_ERR(pcie->clk);
  599. + }
  600. +
  601. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  602. + pcie->pcie_base = devm_ioremap_resource(&pdev->dev, res);
  603. + if (IS_ERR(pcie->pcie_base)) {
  604. + dev_err(&pdev->dev, "Failed to get pcie range\n");
  605. + return PTR_ERR(pcie->pcie_base);
  606. + }
  607. +
  608. + mtk_foreach_port(pcie, port) {
  609. + if (!port->enable)
  610. + continue;
  611. + res = platform_get_resource(pdev, IORESOURCE_MEM, port->id + 1);
  612. + port->phy_base = devm_ioremap_resource(&pdev->dev, res);
  613. + if (IS_ERR(port->phy_base)) {
  614. + dev_err(&pdev->dev, "Failed to get pcie phy%d range %p\n",
  615. + port->id, port->phy_base);
  616. + return PTR_ERR(port->phy_base);
  617. + }
  618. + port->irq = platform_get_irq(pdev, port->id);
  619. + }
  620. +
  621. + return clk_prepare_enable(pcie->clk);
  622. +}
  623. +
  624. +static int mtk_pcie_probe(struct platform_device *pdev)
  625. +{
  626. + struct mtk_pcie *pcie;
  627. + struct hw_pci hw;
  628. + int ret;
  629. +
  630. + pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  631. + if (!pcie)
  632. + return -ENOMEM;
  633. +
  634. + pcie->dev = &pdev->dev;
  635. + ret = mtk_pcie_parse_dt(pcie);
  636. + if (ret < 0)
  637. + return ret;
  638. +
  639. + pm_runtime_enable(&pdev->dev);
  640. + pm_runtime_get_sync(&pdev->dev);
  641. +
  642. + ret = mtk_pcie_get_resources(pcie);
  643. + if (ret < 0) {
  644. + dev_err(&pdev->dev, "failed to request resources: %d\n", ret);
  645. + goto err_out;
  646. + }
  647. +
  648. + ret = mtk_pcie_preinit(pcie);
  649. + if (ret)
  650. + return ret;
  651. +
  652. + memset(&hw, 0, sizeof(hw));
  653. + hw.nr_controllers = 1;
  654. + hw.private_data = (void **)&pcie;
  655. + hw.setup = mtk_pcie_setup;
  656. + hw.map_irq = mtk_pcie_map_irq;
  657. + hw.scan = mtk_pcie_scan_bus;
  658. +
  659. + pci_common_init_dev(pcie->dev, &hw);
  660. + platform_set_drvdata(pdev, pcie);
  661. +
  662. + return 0;
  663. +
  664. +err_out:
  665. + clk_disable_unprepare(pcie->clk);
  666. + pm_runtime_put_sync(&pdev->dev);
  667. + pm_runtime_disable(&pdev->dev);
  668. +
  669. + return ret;
  670. +}
  671. +
  672. +static const struct of_device_id mtk_pcie_ids[] = {
  673. + { .compatible = "mediatek,mt2701-pcie" },
  674. + { .compatible = "mediatek,mt7623-pcie" },
  675. + {},
  676. +};
  677. +MODULE_DEVICE_TABLE(of, mtk_pcie_ids);
  678. +
  679. +static struct platform_driver mtk_pcie_driver = {
  680. + .probe = mtk_pcie_probe,
  681. + .driver = {
  682. + .name = "mediatek-pcie",
  683. + .owner = THIS_MODULE,
  684. + .of_match_table = of_match_ptr(mtk_pcie_ids),
  685. + },
  686. +};
  687. +
  688. +static int __init mtk_pcie_init(void)
  689. +{
  690. + return platform_driver_register(&mtk_pcie_driver);
  691. +}
  692. +
  693. +module_init(mtk_pcie_init);