pcie-mediatek.c 34 KB

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