pcie-mediatek.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MediaTek PCIe host controller driver.
  4. *
  5. * Copyright (c) 2017 MediaTek Inc.
  6. * Author: Ryder Lee <ryder.lee@mediatek.com>
  7. * Honghui Zhang <honghui.zhang@mediatek.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip/chained_irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/msi.h>
  18. #include <linux/module.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/pci.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regmap.h>
  27. #include <linux/reset.h>
  28. #include "../pci.h"
  29. /* PCIe shared registers */
  30. #define PCIE_SYS_CFG 0x00
  31. #define PCIE_INT_ENABLE 0x0c
  32. #define PCIE_CFG_ADDR 0x20
  33. #define PCIE_CFG_DATA 0x24
  34. /* PCIe per port registers */
  35. #define PCIE_BAR0_SETUP 0x10
  36. #define PCIE_CLASS 0x34
  37. #define PCIE_LINK_STATUS 0x50
  38. #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
  39. #define PCIE_PORT_PERST(x) BIT(1 + (x))
  40. #define PCIE_PORT_LINKUP BIT(0)
  41. #define PCIE_BAR_MAP_MAX GENMASK(31, 16)
  42. #define PCIE_BAR_ENABLE BIT(0)
  43. #define PCIE_REVISION_ID BIT(0)
  44. #define PCIE_CLASS_CODE (0x60400 << 8)
  45. #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
  46. ((((regn) >> 8) & GENMASK(3, 0)) << 24))
  47. #define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
  48. #define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
  49. #define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
  50. #define PCIE_CONF_ADDR(regn, fun, dev, bus) \
  51. (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
  52. PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
  53. /* MediaTek specific configuration registers */
  54. #define PCIE_FTS_NUM 0x70c
  55. #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
  56. #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
  57. #define PCIE_FC_CREDIT 0x73c
  58. #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
  59. #define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
  60. /* PCIe V2 share registers */
  61. #define PCIE_SYS_CFG_V2 0x0
  62. #define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
  63. #define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
  64. /* PCIe V2 per-port registers */
  65. #define PCIE_MSI_VECTOR 0x0c0
  66. #define PCIE_CONF_VEND_ID 0x100
  67. #define PCIE_CONF_DEVICE_ID 0x102
  68. #define PCIE_CONF_CLASS_ID 0x106
  69. #define PCIE_INT_MASK 0x420
  70. #define INTX_MASK GENMASK(19, 16)
  71. #define INTX_SHIFT 16
  72. #define PCIE_INT_STATUS 0x424
  73. #define MSI_STATUS BIT(23)
  74. #define PCIE_IMSI_STATUS 0x42c
  75. #define PCIE_IMSI_ADDR 0x430
  76. #define MSI_MASK BIT(23)
  77. #define MTK_MSI_IRQS_NUM 32
  78. #define PCIE_AHB_TRANS_BASE0_L 0x438
  79. #define PCIE_AHB_TRANS_BASE0_H 0x43c
  80. #define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
  81. #define PCIE_AXI_WINDOW0 0x448
  82. #define WIN_ENABLE BIT(7)
  83. /*
  84. * Define PCIe to AHB window size as 2^33 to support max 8GB address space
  85. * translate, support least 4GB DRAM size access from EP DMA(physical DRAM
  86. * start from 0x40000000).
  87. */
  88. #define PCIE2AHB_SIZE 0x21
  89. /* PCIe V2 configuration transaction header */
  90. #define PCIE_CFG_HEADER0 0x460
  91. #define PCIE_CFG_HEADER1 0x464
  92. #define PCIE_CFG_HEADER2 0x468
  93. #define PCIE_CFG_WDATA 0x470
  94. #define PCIE_APP_TLP_REQ 0x488
  95. #define PCIE_CFG_RDATA 0x48c
  96. #define APP_CFG_REQ BIT(0)
  97. #define APP_CPL_STATUS GENMASK(7, 5)
  98. #define CFG_WRRD_TYPE_0 4
  99. #define CFG_WR_FMT 2
  100. #define CFG_RD_FMT 0
  101. #define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
  102. #define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
  103. #define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
  104. #define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
  105. #define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
  106. #define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
  107. #define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
  108. #define CFG_HEADER_DW0(type, fmt) \
  109. (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
  110. #define CFG_HEADER_DW1(where, size) \
  111. (GENMASK(((size) - 1), 0) << ((where) & 0x3))
  112. #define CFG_HEADER_DW2(regn, fun, dev, bus) \
  113. (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
  114. CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
  115. #define PCIE_RST_CTRL 0x510
  116. #define PCIE_PHY_RSTB BIT(0)
  117. #define PCIE_PIPE_SRSTB BIT(1)
  118. #define PCIE_MAC_SRSTB BIT(2)
  119. #define PCIE_CRSTB BIT(3)
  120. #define PCIE_PERSTB BIT(8)
  121. #define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
  122. #define PCIE_LINK_STATUS_V2 0x804
  123. #define PCIE_PORT_LINKUP_V2 BIT(10)
  124. struct mtk_pcie_port;
  125. /**
  126. * struct mtk_pcie_soc - differentiate between host generations
  127. * @need_fix_class_id: whether this host's class ID needed to be fixed or not
  128. * @need_fix_device_id: whether this host's device ID needed to be fixed or not
  129. * @no_msi: Bridge has no MSI support, and relies on an external block
  130. * @device_id: device ID which this host need to be fixed
  131. * @ops: pointer to configuration access functions
  132. * @startup: pointer to controller setting functions
  133. * @setup_irq: pointer to initialize IRQ functions
  134. */
  135. struct mtk_pcie_soc {
  136. bool need_fix_class_id;
  137. bool need_fix_device_id;
  138. bool no_msi;
  139. unsigned int device_id;
  140. struct pci_ops *ops;
  141. int (*startup)(struct mtk_pcie_port *port);
  142. int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
  143. };
  144. /**
  145. * struct mtk_pcie_port - PCIe port information
  146. * @base: IO mapped register base
  147. * @list: port list
  148. * @pcie: pointer to PCIe host info
  149. * @reset: pointer to port reset control
  150. * @sys_ck: pointer to transaction/data link layer clock
  151. * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
  152. * and RC initiated MMIO access
  153. * @axi_ck: pointer to application layer MMIO channel operating clock
  154. * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
  155. * when pcie_mac_ck/pcie_pipe_ck is turned off
  156. * @obff_ck: pointer to OBFF functional block operating clock
  157. * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
  158. * @phy: pointer to PHY control block
  159. * @slot: port slot
  160. * @irq: GIC irq
  161. * @irq_domain: legacy INTx IRQ domain
  162. * @inner_domain: inner IRQ domain
  163. * @msi_domain: MSI IRQ domain
  164. * @lock: protect the msi_irq_in_use bitmap
  165. * @msi_irq_in_use: bit map for assigned MSI IRQ
  166. */
  167. struct mtk_pcie_port {
  168. void __iomem *base;
  169. struct list_head list;
  170. struct mtk_pcie *pcie;
  171. struct reset_control *reset;
  172. struct clk *sys_ck;
  173. struct clk *ahb_ck;
  174. struct clk *axi_ck;
  175. struct clk *aux_ck;
  176. struct clk *obff_ck;
  177. struct clk *pipe_ck;
  178. struct phy *phy;
  179. u32 slot;
  180. int irq;
  181. struct irq_domain *irq_domain;
  182. struct irq_domain *inner_domain;
  183. struct irq_domain *msi_domain;
  184. struct mutex lock;
  185. DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
  186. };
  187. /**
  188. * struct mtk_pcie - PCIe host information
  189. * @dev: pointer to PCIe device
  190. * @base: IO mapped register base
  191. * @cfg: IO mapped register map for PCIe config
  192. * @free_ck: free-run reference clock
  193. * @ports: pointer to PCIe port information
  194. * @soc: pointer to SoC-dependent operations
  195. */
  196. struct mtk_pcie {
  197. struct device *dev;
  198. void __iomem *base;
  199. struct regmap *cfg;
  200. struct clk *free_ck;
  201. struct list_head ports;
  202. const struct mtk_pcie_soc *soc;
  203. };
  204. static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie)
  205. {
  206. struct device *dev = pcie->dev;
  207. clk_disable_unprepare(pcie->free_ck);
  208. pm_runtime_put_sync(dev);
  209. pm_runtime_disable(dev);
  210. }
  211. static void mtk_pcie_port_free(struct mtk_pcie_port *port)
  212. {
  213. struct mtk_pcie *pcie = port->pcie;
  214. struct device *dev = pcie->dev;
  215. devm_iounmap(dev, port->base);
  216. list_del(&port->list);
  217. devm_kfree(dev, port);
  218. }
  219. static void mtk_pcie_put_resources(struct mtk_pcie *pcie)
  220. {
  221. struct mtk_pcie_port *port, *tmp;
  222. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  223. phy_power_off(port->phy);
  224. phy_exit(port->phy);
  225. clk_disable_unprepare(port->pipe_ck);
  226. clk_disable_unprepare(port->obff_ck);
  227. clk_disable_unprepare(port->axi_ck);
  228. clk_disable_unprepare(port->aux_ck);
  229. clk_disable_unprepare(port->ahb_ck);
  230. clk_disable_unprepare(port->sys_ck);
  231. mtk_pcie_port_free(port);
  232. }
  233. mtk_pcie_subsys_powerdown(pcie);
  234. }
  235. static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port)
  236. {
  237. u32 val;
  238. int err;
  239. err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val,
  240. !(val & APP_CFG_REQ), 10,
  241. 100 * USEC_PER_MSEC);
  242. if (err)
  243. return PCIBIOS_SET_FAILED;
  244. if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS)
  245. return PCIBIOS_SET_FAILED;
  246. return PCIBIOS_SUCCESSFUL;
  247. }
  248. static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
  249. int where, int size, u32 *val)
  250. {
  251. u32 tmp;
  252. /* Write PCIe configuration transaction header for Cfgrd */
  253. writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT),
  254. port->base + PCIE_CFG_HEADER0);
  255. writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
  256. writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
  257. port->base + PCIE_CFG_HEADER2);
  258. /* Trigger h/w to transmit Cfgrd TLP */
  259. tmp = readl(port->base + PCIE_APP_TLP_REQ);
  260. tmp |= APP_CFG_REQ;
  261. writel(tmp, port->base + PCIE_APP_TLP_REQ);
  262. /* Check completion status */
  263. if (mtk_pcie_check_cfg_cpld(port))
  264. return PCIBIOS_SET_FAILED;
  265. /* Read cpld payload of Cfgrd */
  266. *val = readl(port->base + PCIE_CFG_RDATA);
  267. if (size == 1)
  268. *val = (*val >> (8 * (where & 3))) & 0xff;
  269. else if (size == 2)
  270. *val = (*val >> (8 * (where & 3))) & 0xffff;
  271. return PCIBIOS_SUCCESSFUL;
  272. }
  273. static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
  274. int where, int size, u32 val)
  275. {
  276. /* Write PCIe configuration transaction header for Cfgwr */
  277. writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT),
  278. port->base + PCIE_CFG_HEADER0);
  279. writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
  280. writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
  281. port->base + PCIE_CFG_HEADER2);
  282. /* Write Cfgwr data */
  283. val = val << 8 * (where & 3);
  284. writel(val, port->base + PCIE_CFG_WDATA);
  285. /* Trigger h/w to transmit Cfgwr TLP */
  286. val = readl(port->base + PCIE_APP_TLP_REQ);
  287. val |= APP_CFG_REQ;
  288. writel(val, port->base + PCIE_APP_TLP_REQ);
  289. /* Check completion status */
  290. return mtk_pcie_check_cfg_cpld(port);
  291. }
  292. static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus,
  293. unsigned int devfn)
  294. {
  295. struct mtk_pcie *pcie = bus->sysdata;
  296. struct mtk_pcie_port *port;
  297. struct pci_dev *dev = NULL;
  298. /*
  299. * Walk the bus hierarchy to get the devfn value
  300. * of the port in the root bus.
  301. */
  302. while (bus && bus->number) {
  303. dev = bus->self;
  304. bus = dev->bus;
  305. devfn = dev->devfn;
  306. }
  307. list_for_each_entry(port, &pcie->ports, list)
  308. if (port->slot == PCI_SLOT(devfn))
  309. return port;
  310. return NULL;
  311. }
  312. static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
  313. int where, int size, u32 *val)
  314. {
  315. struct mtk_pcie_port *port;
  316. u32 bn = bus->number;
  317. port = mtk_pcie_find_port(bus, devfn);
  318. if (!port)
  319. return PCIBIOS_DEVICE_NOT_FOUND;
  320. return mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val);
  321. }
  322. static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
  323. int where, int size, u32 val)
  324. {
  325. struct mtk_pcie_port *port;
  326. u32 bn = bus->number;
  327. port = mtk_pcie_find_port(bus, devfn);
  328. if (!port)
  329. return PCIBIOS_DEVICE_NOT_FOUND;
  330. return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val);
  331. }
  332. static struct pci_ops mtk_pcie_ops_v2 = {
  333. .read = mtk_pcie_config_read,
  334. .write = mtk_pcie_config_write,
  335. };
  336. static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  337. {
  338. struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
  339. phys_addr_t addr;
  340. /* MT2712/MT7622 only support 32-bit MSI addresses */
  341. addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
  342. msg->address_hi = 0;
  343. msg->address_lo = lower_32_bits(addr);
  344. msg->data = data->hwirq;
  345. dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n",
  346. (int)data->hwirq, msg->address_hi, msg->address_lo);
  347. }
  348. static void mtk_msi_ack_irq(struct irq_data *data)
  349. {
  350. struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
  351. u32 hwirq = data->hwirq;
  352. writel(1 << hwirq, port->base + PCIE_IMSI_STATUS);
  353. }
  354. static struct irq_chip mtk_msi_bottom_irq_chip = {
  355. .name = "MTK MSI",
  356. .irq_compose_msi_msg = mtk_compose_msi_msg,
  357. .irq_ack = mtk_msi_ack_irq,
  358. };
  359. static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  360. unsigned int nr_irqs, void *args)
  361. {
  362. struct mtk_pcie_port *port = domain->host_data;
  363. unsigned long bit;
  364. WARN_ON(nr_irqs != 1);
  365. mutex_lock(&port->lock);
  366. bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
  367. if (bit >= MTK_MSI_IRQS_NUM) {
  368. mutex_unlock(&port->lock);
  369. return -ENOSPC;
  370. }
  371. __set_bit(bit, port->msi_irq_in_use);
  372. mutex_unlock(&port->lock);
  373. irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip,
  374. domain->host_data, handle_edge_irq,
  375. NULL, NULL);
  376. return 0;
  377. }
  378. static void mtk_pcie_irq_domain_free(struct irq_domain *domain,
  379. unsigned int virq, unsigned int nr_irqs)
  380. {
  381. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  382. struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d);
  383. mutex_lock(&port->lock);
  384. if (!test_bit(d->hwirq, port->msi_irq_in_use))
  385. dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n",
  386. d->hwirq);
  387. else
  388. __clear_bit(d->hwirq, port->msi_irq_in_use);
  389. mutex_unlock(&port->lock);
  390. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  391. }
  392. static const struct irq_domain_ops msi_domain_ops = {
  393. .alloc = mtk_pcie_irq_domain_alloc,
  394. .free = mtk_pcie_irq_domain_free,
  395. };
  396. static struct irq_chip mtk_msi_irq_chip = {
  397. .name = "MTK PCIe MSI",
  398. .irq_ack = irq_chip_ack_parent,
  399. .irq_mask = pci_msi_mask_irq,
  400. .irq_unmask = pci_msi_unmask_irq,
  401. };
  402. static struct msi_domain_info mtk_msi_domain_info = {
  403. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  404. MSI_FLAG_NO_AFFINITY | MSI_FLAG_PCI_MSIX,
  405. .chip = &mtk_msi_irq_chip,
  406. };
  407. static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port)
  408. {
  409. struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node);
  410. mutex_init(&port->lock);
  411. port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM,
  412. &msi_domain_ops, port);
  413. if (!port->inner_domain) {
  414. dev_err(port->pcie->dev, "failed to create IRQ domain\n");
  415. return -ENOMEM;
  416. }
  417. port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info,
  418. port->inner_domain);
  419. if (!port->msi_domain) {
  420. dev_err(port->pcie->dev, "failed to create MSI domain\n");
  421. irq_domain_remove(port->inner_domain);
  422. return -ENOMEM;
  423. }
  424. return 0;
  425. }
  426. static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
  427. {
  428. u32 val;
  429. phys_addr_t msg_addr;
  430. msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
  431. val = lower_32_bits(msg_addr);
  432. writel(val, port->base + PCIE_IMSI_ADDR);
  433. val = readl(port->base + PCIE_INT_MASK);
  434. val &= ~MSI_MASK;
  435. writel(val, port->base + PCIE_INT_MASK);
  436. }
  437. static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie)
  438. {
  439. struct mtk_pcie_port *port, *tmp;
  440. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  441. irq_set_chained_handler_and_data(port->irq, NULL, NULL);
  442. if (port->irq_domain)
  443. irq_domain_remove(port->irq_domain);
  444. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  445. if (port->msi_domain)
  446. irq_domain_remove(port->msi_domain);
  447. if (port->inner_domain)
  448. irq_domain_remove(port->inner_domain);
  449. }
  450. irq_dispose_mapping(port->irq);
  451. }
  452. }
  453. static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  454. irq_hw_number_t hwirq)
  455. {
  456. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  457. irq_set_chip_data(irq, domain->host_data);
  458. return 0;
  459. }
  460. static const struct irq_domain_ops intx_domain_ops = {
  461. .map = mtk_pcie_intx_map,
  462. };
  463. static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
  464. struct device_node *node)
  465. {
  466. struct device *dev = port->pcie->dev;
  467. struct device_node *pcie_intc_node;
  468. int ret;
  469. /* Setup INTx */
  470. pcie_intc_node = of_get_next_child(node, NULL);
  471. if (!pcie_intc_node) {
  472. dev_err(dev, "no PCIe Intc node found\n");
  473. return -ENODEV;
  474. }
  475. port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
  476. &intx_domain_ops, port);
  477. of_node_put(pcie_intc_node);
  478. if (!port->irq_domain) {
  479. dev_err(dev, "failed to get INTx IRQ domain\n");
  480. return -ENODEV;
  481. }
  482. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  483. ret = mtk_pcie_allocate_msi_domains(port);
  484. if (ret)
  485. return ret;
  486. }
  487. return 0;
  488. }
  489. static void mtk_pcie_intr_handler(struct irq_desc *desc)
  490. {
  491. struct mtk_pcie_port *port = irq_desc_get_handler_data(desc);
  492. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  493. unsigned long status;
  494. u32 bit = INTX_SHIFT;
  495. chained_irq_enter(irqchip, desc);
  496. status = readl(port->base + PCIE_INT_STATUS);
  497. if (status & INTX_MASK) {
  498. for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) {
  499. /* Clear the INTx */
  500. writel(1 << bit, port->base + PCIE_INT_STATUS);
  501. generic_handle_domain_irq(port->irq_domain,
  502. bit - INTX_SHIFT);
  503. }
  504. }
  505. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  506. if (status & MSI_STATUS){
  507. unsigned long imsi_status;
  508. /*
  509. * The interrupt status can be cleared even if the
  510. * MSI status remains pending. As such, given the
  511. * edge-triggered interrupt type, its status should
  512. * be cleared before being dispatched to the
  513. * handler of the underlying device.
  514. */
  515. writel(MSI_STATUS, port->base + PCIE_INT_STATUS);
  516. while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) {
  517. for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM)
  518. generic_handle_domain_irq(port->inner_domain, bit);
  519. }
  520. }
  521. }
  522. chained_irq_exit(irqchip, desc);
  523. }
  524. static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
  525. struct device_node *node)
  526. {
  527. struct mtk_pcie *pcie = port->pcie;
  528. struct device *dev = pcie->dev;
  529. struct platform_device *pdev = to_platform_device(dev);
  530. int err;
  531. err = mtk_pcie_init_irq_domain(port, node);
  532. if (err) {
  533. dev_err(dev, "failed to init PCIe IRQ domain\n");
  534. return err;
  535. }
  536. if (of_property_present(dev->of_node, "interrupt-names"))
  537. port->irq = platform_get_irq_byname(pdev, "pcie_irq");
  538. else
  539. port->irq = platform_get_irq(pdev, port->slot);
  540. if (port->irq < 0)
  541. return port->irq;
  542. irq_set_chained_handler_and_data(port->irq,
  543. mtk_pcie_intr_handler, port);
  544. return 0;
  545. }
  546. static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
  547. {
  548. struct mtk_pcie *pcie = port->pcie;
  549. struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
  550. struct resource *mem = NULL;
  551. struct resource_entry *entry;
  552. const struct mtk_pcie_soc *soc = port->pcie->soc;
  553. u32 val;
  554. int err;
  555. entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
  556. if (entry)
  557. mem = entry->res;
  558. if (!mem)
  559. return -EINVAL;
  560. /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
  561. if (pcie->base) {
  562. val = readl(pcie->base + PCIE_SYS_CFG_V2);
  563. val |= PCIE_CSR_LTSSM_EN(port->slot) |
  564. PCIE_CSR_ASPM_L1_EN(port->slot);
  565. writel(val, pcie->base + PCIE_SYS_CFG_V2);
  566. } else if (pcie->cfg) {
  567. val = PCIE_CSR_LTSSM_EN(port->slot) |
  568. PCIE_CSR_ASPM_L1_EN(port->slot);
  569. regmap_update_bits(pcie->cfg, PCIE_SYS_CFG_V2, val, val);
  570. }
  571. /* Assert all reset signals */
  572. writel(0, port->base + PCIE_RST_CTRL);
  573. /*
  574. * Enable PCIe link down reset, if link status changed from link up to
  575. * link down, this will reset MAC control registers and configuration
  576. * space.
  577. */
  578. writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL);
  579. /*
  580. * Described in PCIe CEM specification sections 2.2 (PERST# Signal) and
  581. * 2.2.1 (Initial Power-Up (G3 to S0)). The deassertion of PERST# should
  582. * be delayed 100ms (TPVPERL) for the power and clock to become stable.
  583. */
  584. msleep(100);
  585. /* De-assert PHY, PE, PIPE, MAC and configuration reset */
  586. val = readl(port->base + PCIE_RST_CTRL);
  587. val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB |
  588. PCIE_MAC_SRSTB | PCIE_CRSTB;
  589. writel(val, port->base + PCIE_RST_CTRL);
  590. /* Set up vendor ID and class code */
  591. if (soc->need_fix_class_id) {
  592. val = PCI_VENDOR_ID_MEDIATEK;
  593. writew(val, port->base + PCIE_CONF_VEND_ID);
  594. val = PCI_CLASS_BRIDGE_PCI;
  595. writew(val, port->base + PCIE_CONF_CLASS_ID);
  596. }
  597. if (soc->need_fix_device_id)
  598. writew(soc->device_id, port->base + PCIE_CONF_DEVICE_ID);
  599. /* 100ms timeout value should be enough for Gen1/2 training */
  600. err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val,
  601. !!(val & PCIE_PORT_LINKUP_V2), 20,
  602. 100 * USEC_PER_MSEC);
  603. if (err)
  604. return -ETIMEDOUT;
  605. /* Set INTx mask */
  606. val = readl(port->base + PCIE_INT_MASK);
  607. val &= ~INTX_MASK;
  608. writel(val, port->base + PCIE_INT_MASK);
  609. if (IS_ENABLED(CONFIG_PCI_MSI))
  610. mtk_pcie_enable_msi(port);
  611. /* Set AHB to PCIe translation windows */
  612. val = lower_32_bits(mem->start) |
  613. AHB2PCIE_SIZE(fls(resource_size(mem)));
  614. writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
  615. val = upper_32_bits(mem->start);
  616. writel(val, port->base + PCIE_AHB_TRANS_BASE0_H);
  617. /* Set PCIe to AXI translation memory space.*/
  618. val = PCIE2AHB_SIZE | WIN_ENABLE;
  619. writel(val, port->base + PCIE_AXI_WINDOW0);
  620. return 0;
  621. }
  622. static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus,
  623. unsigned int devfn, int where)
  624. {
  625. struct mtk_pcie *pcie = bus->sysdata;
  626. writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn),
  627. bus->number), pcie->base + PCIE_CFG_ADDR);
  628. return pcie->base + PCIE_CFG_DATA + (where & 3);
  629. }
  630. static struct pci_ops mtk_pcie_ops = {
  631. .map_bus = mtk_pcie_map_bus,
  632. .read = pci_generic_config_read,
  633. .write = pci_generic_config_write,
  634. };
  635. static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
  636. {
  637. struct mtk_pcie *pcie = port->pcie;
  638. u32 func = PCI_FUNC(port->slot);
  639. u32 slot = PCI_SLOT(port->slot << 3);
  640. u32 val;
  641. int err;
  642. /* assert port PERST_N */
  643. val = readl(pcie->base + PCIE_SYS_CFG);
  644. val |= PCIE_PORT_PERST(port->slot);
  645. writel(val, pcie->base + PCIE_SYS_CFG);
  646. /* de-assert port PERST_N */
  647. val = readl(pcie->base + PCIE_SYS_CFG);
  648. val &= ~PCIE_PORT_PERST(port->slot);
  649. writel(val, pcie->base + PCIE_SYS_CFG);
  650. /* 100ms timeout value should be enough for Gen1/2 training */
  651. err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
  652. !!(val & PCIE_PORT_LINKUP), 20,
  653. 100 * USEC_PER_MSEC);
  654. if (err)
  655. return -ETIMEDOUT;
  656. /* enable interrupt */
  657. val = readl(pcie->base + PCIE_INT_ENABLE);
  658. val |= PCIE_PORT_INT_EN(port->slot);
  659. writel(val, pcie->base + PCIE_INT_ENABLE);
  660. /* map to all DDR region. We need to set it before cfg operation. */
  661. writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
  662. port->base + PCIE_BAR0_SETUP);
  663. /* configure class code and revision ID */
  664. writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
  665. /* configure FC credit */
  666. writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
  667. pcie->base + PCIE_CFG_ADDR);
  668. val = readl(pcie->base + PCIE_CFG_DATA);
  669. val &= ~PCIE_FC_CREDIT_MASK;
  670. val |= PCIE_FC_CREDIT_VAL(0x806c);
  671. writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
  672. pcie->base + PCIE_CFG_ADDR);
  673. writel(val, pcie->base + PCIE_CFG_DATA);
  674. /* configure RC FTS number to 250 when it leaves L0s */
  675. writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
  676. pcie->base + PCIE_CFG_ADDR);
  677. val = readl(pcie->base + PCIE_CFG_DATA);
  678. val &= ~PCIE_FTS_NUM_MASK;
  679. val |= PCIE_FTS_NUM_L0(0x50);
  680. writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
  681. pcie->base + PCIE_CFG_ADDR);
  682. writel(val, pcie->base + PCIE_CFG_DATA);
  683. return 0;
  684. }
  685. static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
  686. {
  687. struct mtk_pcie *pcie = port->pcie;
  688. struct device *dev = pcie->dev;
  689. int err;
  690. err = clk_prepare_enable(port->sys_ck);
  691. if (err) {
  692. dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot);
  693. goto err_sys_clk;
  694. }
  695. err = clk_prepare_enable(port->ahb_ck);
  696. if (err) {
  697. dev_err(dev, "failed to enable ahb_ck%d\n", port->slot);
  698. goto err_ahb_clk;
  699. }
  700. err = clk_prepare_enable(port->aux_ck);
  701. if (err) {
  702. dev_err(dev, "failed to enable aux_ck%d\n", port->slot);
  703. goto err_aux_clk;
  704. }
  705. err = clk_prepare_enable(port->axi_ck);
  706. if (err) {
  707. dev_err(dev, "failed to enable axi_ck%d\n", port->slot);
  708. goto err_axi_clk;
  709. }
  710. err = clk_prepare_enable(port->obff_ck);
  711. if (err) {
  712. dev_err(dev, "failed to enable obff_ck%d\n", port->slot);
  713. goto err_obff_clk;
  714. }
  715. err = clk_prepare_enable(port->pipe_ck);
  716. if (err) {
  717. dev_err(dev, "failed to enable pipe_ck%d\n", port->slot);
  718. goto err_pipe_clk;
  719. }
  720. reset_control_assert(port->reset);
  721. reset_control_deassert(port->reset);
  722. err = phy_init(port->phy);
  723. if (err) {
  724. dev_err(dev, "failed to initialize port%d phy\n", port->slot);
  725. goto err_phy_init;
  726. }
  727. err = phy_power_on(port->phy);
  728. if (err) {
  729. dev_err(dev, "failed to power on port%d phy\n", port->slot);
  730. goto err_phy_on;
  731. }
  732. if (!pcie->soc->startup(port))
  733. return;
  734. dev_info(dev, "Port%d link down\n", port->slot);
  735. phy_power_off(port->phy);
  736. err_phy_on:
  737. phy_exit(port->phy);
  738. err_phy_init:
  739. clk_disable_unprepare(port->pipe_ck);
  740. err_pipe_clk:
  741. clk_disable_unprepare(port->obff_ck);
  742. err_obff_clk:
  743. clk_disable_unprepare(port->axi_ck);
  744. err_axi_clk:
  745. clk_disable_unprepare(port->aux_ck);
  746. err_aux_clk:
  747. clk_disable_unprepare(port->ahb_ck);
  748. err_ahb_clk:
  749. clk_disable_unprepare(port->sys_ck);
  750. err_sys_clk:
  751. mtk_pcie_port_free(port);
  752. }
  753. static int mtk_pcie_parse_port(struct mtk_pcie *pcie,
  754. struct device_node *node,
  755. int slot)
  756. {
  757. struct mtk_pcie_port *port;
  758. struct device *dev = pcie->dev;
  759. struct platform_device *pdev = to_platform_device(dev);
  760. char name[10];
  761. int err;
  762. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  763. if (!port)
  764. return -ENOMEM;
  765. snprintf(name, sizeof(name), "port%d", slot);
  766. port->base = devm_platform_ioremap_resource_byname(pdev, name);
  767. if (IS_ERR(port->base)) {
  768. dev_err(dev, "failed to map port%d base\n", slot);
  769. return PTR_ERR(port->base);
  770. }
  771. snprintf(name, sizeof(name), "sys_ck%d", slot);
  772. port->sys_ck = devm_clk_get(dev, name);
  773. if (IS_ERR(port->sys_ck)) {
  774. dev_err(dev, "failed to get sys_ck%d clock\n", slot);
  775. return PTR_ERR(port->sys_ck);
  776. }
  777. /* sys_ck might be divided into the following parts in some chips */
  778. snprintf(name, sizeof(name), "ahb_ck%d", slot);
  779. port->ahb_ck = devm_clk_get_optional(dev, name);
  780. if (IS_ERR(port->ahb_ck))
  781. return PTR_ERR(port->ahb_ck);
  782. snprintf(name, sizeof(name), "axi_ck%d", slot);
  783. port->axi_ck = devm_clk_get_optional(dev, name);
  784. if (IS_ERR(port->axi_ck))
  785. return PTR_ERR(port->axi_ck);
  786. snprintf(name, sizeof(name), "aux_ck%d", slot);
  787. port->aux_ck = devm_clk_get_optional(dev, name);
  788. if (IS_ERR(port->aux_ck))
  789. return PTR_ERR(port->aux_ck);
  790. snprintf(name, sizeof(name), "obff_ck%d", slot);
  791. port->obff_ck = devm_clk_get_optional(dev, name);
  792. if (IS_ERR(port->obff_ck))
  793. return PTR_ERR(port->obff_ck);
  794. snprintf(name, sizeof(name), "pipe_ck%d", slot);
  795. port->pipe_ck = devm_clk_get_optional(dev, name);
  796. if (IS_ERR(port->pipe_ck))
  797. return PTR_ERR(port->pipe_ck);
  798. snprintf(name, sizeof(name), "pcie-rst%d", slot);
  799. port->reset = devm_reset_control_get_optional_exclusive(dev, name);
  800. if (PTR_ERR(port->reset) == -EPROBE_DEFER)
  801. return PTR_ERR(port->reset);
  802. /* some platforms may use default PHY setting */
  803. snprintf(name, sizeof(name), "pcie-phy%d", slot);
  804. port->phy = devm_phy_optional_get(dev, name);
  805. if (IS_ERR(port->phy))
  806. return PTR_ERR(port->phy);
  807. port->slot = slot;
  808. port->pcie = pcie;
  809. if (pcie->soc->setup_irq) {
  810. err = pcie->soc->setup_irq(port, node);
  811. if (err)
  812. return err;
  813. }
  814. INIT_LIST_HEAD(&port->list);
  815. list_add_tail(&port->list, &pcie->ports);
  816. return 0;
  817. }
  818. static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
  819. {
  820. struct device *dev = pcie->dev;
  821. struct platform_device *pdev = to_platform_device(dev);
  822. struct resource *regs;
  823. struct device_node *cfg_node;
  824. int err;
  825. /* get shared registers, which are optional */
  826. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys");
  827. if (regs) {
  828. pcie->base = devm_ioremap_resource(dev, regs);
  829. if (IS_ERR(pcie->base))
  830. return PTR_ERR(pcie->base);
  831. }
  832. cfg_node = of_find_compatible_node(NULL, NULL,
  833. "mediatek,generic-pciecfg");
  834. if (cfg_node) {
  835. pcie->cfg = syscon_node_to_regmap(cfg_node);
  836. of_node_put(cfg_node);
  837. if (IS_ERR(pcie->cfg))
  838. return PTR_ERR(pcie->cfg);
  839. }
  840. pcie->free_ck = devm_clk_get(dev, "free_ck");
  841. if (IS_ERR(pcie->free_ck)) {
  842. if (PTR_ERR(pcie->free_ck) == -EPROBE_DEFER)
  843. return -EPROBE_DEFER;
  844. pcie->free_ck = NULL;
  845. }
  846. pm_runtime_enable(dev);
  847. pm_runtime_get_sync(dev);
  848. /* enable top level clock */
  849. err = clk_prepare_enable(pcie->free_ck);
  850. if (err) {
  851. dev_err(dev, "failed to enable free_ck\n");
  852. goto err_free_ck;
  853. }
  854. return 0;
  855. err_free_ck:
  856. pm_runtime_put_sync(dev);
  857. pm_runtime_disable(dev);
  858. return err;
  859. }
  860. static int mtk_pcie_setup(struct mtk_pcie *pcie)
  861. {
  862. struct device *dev = pcie->dev;
  863. struct device_node *node = dev->of_node, *child;
  864. struct mtk_pcie_port *port, *tmp;
  865. int err, slot;
  866. slot = of_get_pci_domain_nr(dev->of_node);
  867. if (slot < 0) {
  868. for_each_available_child_of_node(node, child) {
  869. err = of_pci_get_devfn(child);
  870. if (err < 0) {
  871. dev_err(dev, "failed to get devfn: %d\n", err);
  872. goto error_put_node;
  873. }
  874. slot = PCI_SLOT(err);
  875. err = mtk_pcie_parse_port(pcie, child, slot);
  876. if (err)
  877. goto error_put_node;
  878. }
  879. } else {
  880. err = mtk_pcie_parse_port(pcie, node, slot);
  881. if (err)
  882. return err;
  883. }
  884. err = mtk_pcie_subsys_powerup(pcie);
  885. if (err)
  886. return err;
  887. /* enable each port, and then check link status */
  888. list_for_each_entry_safe(port, tmp, &pcie->ports, list)
  889. mtk_pcie_enable_port(port);
  890. /* power down PCIe subsys if slots are all empty (link down) */
  891. if (list_empty(&pcie->ports))
  892. mtk_pcie_subsys_powerdown(pcie);
  893. return 0;
  894. error_put_node:
  895. of_node_put(child);
  896. return err;
  897. }
  898. static int mtk_pcie_probe(struct platform_device *pdev)
  899. {
  900. struct device *dev = &pdev->dev;
  901. struct mtk_pcie *pcie;
  902. struct pci_host_bridge *host;
  903. int err;
  904. host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  905. if (!host)
  906. return -ENOMEM;
  907. pcie = pci_host_bridge_priv(host);
  908. pcie->dev = dev;
  909. pcie->soc = of_device_get_match_data(dev);
  910. platform_set_drvdata(pdev, pcie);
  911. INIT_LIST_HEAD(&pcie->ports);
  912. err = mtk_pcie_setup(pcie);
  913. if (err)
  914. return err;
  915. host->ops = pcie->soc->ops;
  916. host->sysdata = pcie;
  917. host->msi_domain = pcie->soc->no_msi;
  918. err = pci_host_probe(host);
  919. if (err)
  920. goto put_resources;
  921. return 0;
  922. put_resources:
  923. if (!list_empty(&pcie->ports))
  924. mtk_pcie_put_resources(pcie);
  925. return err;
  926. }
  927. static void mtk_pcie_free_resources(struct mtk_pcie *pcie)
  928. {
  929. struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
  930. struct list_head *windows = &host->windows;
  931. pci_free_resource_list(windows);
  932. }
  933. static void mtk_pcie_remove(struct platform_device *pdev)
  934. {
  935. struct mtk_pcie *pcie = platform_get_drvdata(pdev);
  936. struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
  937. pci_stop_root_bus(host->bus);
  938. pci_remove_root_bus(host->bus);
  939. mtk_pcie_free_resources(pcie);
  940. mtk_pcie_irq_teardown(pcie);
  941. mtk_pcie_put_resources(pcie);
  942. }
  943. static int mtk_pcie_suspend_noirq(struct device *dev)
  944. {
  945. struct mtk_pcie *pcie = dev_get_drvdata(dev);
  946. struct mtk_pcie_port *port;
  947. if (list_empty(&pcie->ports))
  948. return 0;
  949. list_for_each_entry(port, &pcie->ports, list) {
  950. clk_disable_unprepare(port->pipe_ck);
  951. clk_disable_unprepare(port->obff_ck);
  952. clk_disable_unprepare(port->axi_ck);
  953. clk_disable_unprepare(port->aux_ck);
  954. clk_disable_unprepare(port->ahb_ck);
  955. clk_disable_unprepare(port->sys_ck);
  956. phy_power_off(port->phy);
  957. phy_exit(port->phy);
  958. }
  959. clk_disable_unprepare(pcie->free_ck);
  960. return 0;
  961. }
  962. static int mtk_pcie_resume_noirq(struct device *dev)
  963. {
  964. struct mtk_pcie *pcie = dev_get_drvdata(dev);
  965. struct mtk_pcie_port *port, *tmp;
  966. if (list_empty(&pcie->ports))
  967. return 0;
  968. clk_prepare_enable(pcie->free_ck);
  969. list_for_each_entry_safe(port, tmp, &pcie->ports, list)
  970. mtk_pcie_enable_port(port);
  971. /* In case of EP was removed while system suspend. */
  972. if (list_empty(&pcie->ports))
  973. clk_disable_unprepare(pcie->free_ck);
  974. return 0;
  975. }
  976. static const struct dev_pm_ops mtk_pcie_pm_ops = {
  977. NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq,
  978. mtk_pcie_resume_noirq)
  979. };
  980. static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
  981. .no_msi = true,
  982. .ops = &mtk_pcie_ops,
  983. .startup = mtk_pcie_startup_port,
  984. };
  985. static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
  986. .ops = &mtk_pcie_ops_v2,
  987. .startup = mtk_pcie_startup_port_v2,
  988. .setup_irq = mtk_pcie_setup_irq,
  989. };
  990. static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = {
  991. .need_fix_class_id = true,
  992. .ops = &mtk_pcie_ops_v2,
  993. .startup = mtk_pcie_startup_port_v2,
  994. .setup_irq = mtk_pcie_setup_irq,
  995. };
  996. static const struct mtk_pcie_soc mtk_pcie_soc_mt7629 = {
  997. .need_fix_class_id = true,
  998. .need_fix_device_id = true,
  999. .device_id = PCI_DEVICE_ID_MEDIATEK_7629,
  1000. .ops = &mtk_pcie_ops_v2,
  1001. .startup = mtk_pcie_startup_port_v2,
  1002. .setup_irq = mtk_pcie_setup_irq,
  1003. };
  1004. static const struct of_device_id mtk_pcie_ids[] = {
  1005. { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 },
  1006. { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 },
  1007. { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_mt2712 },
  1008. { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_mt7622 },
  1009. { .compatible = "mediatek,mt7629-pcie", .data = &mtk_pcie_soc_mt7629 },
  1010. {},
  1011. };
  1012. MODULE_DEVICE_TABLE(of, mtk_pcie_ids);
  1013. static struct platform_driver mtk_pcie_driver = {
  1014. .probe = mtk_pcie_probe,
  1015. .remove_new = mtk_pcie_remove,
  1016. .driver = {
  1017. .name = "mtk-pcie",
  1018. .of_match_table = mtk_pcie_ids,
  1019. .suppress_bind_attrs = true,
  1020. .pm = &mtk_pcie_pm_ops,
  1021. },
  1022. };
  1023. module_platform_driver(mtk_pcie_driver);
  1024. MODULE_DESCRIPTION("MediaTek PCIe host controller driver");
  1025. MODULE_LICENSE("GPL v2");