pcie-rockchip-host.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Rockchip AXI PCIe host controller driver
  4. *
  5. * Copyright (c) 2016 Rockchip, Inc.
  6. *
  7. * Author: Shawn Lin <shawn.lin@rock-chips.com>
  8. * Wenrui Li <wenrui.li@rock-chips.com>
  9. *
  10. * Bits taken from Synopsys DesignWare Host controller driver and
  11. * ARM PCI Host generic driver.
  12. */
  13. #include <linux/bitrev.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqchip/chained_irq.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mfd/syscon.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_pci.h>
  28. #include <linux/pci.h>
  29. #include <linux/pci_ids.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/reset.h>
  33. #include <linux/regmap.h>
  34. #include "../pci.h"
  35. #include "pcie-rockchip.h"
  36. static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
  37. {
  38. u32 status;
  39. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  40. status |= (PCI_EXP_LNKCTL_LBMIE | PCI_EXP_LNKCTL_LABIE);
  41. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  42. }
  43. static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
  44. {
  45. u32 status;
  46. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  47. status |= (PCI_EXP_LNKSTA_LBMS | PCI_EXP_LNKSTA_LABS) << 16;
  48. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  49. }
  50. static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
  51. {
  52. u32 val;
  53. /* Update Tx credit maximum update interval */
  54. val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
  55. val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
  56. val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */
  57. rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
  58. }
  59. static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
  60. struct pci_bus *bus, int dev)
  61. {
  62. /*
  63. * Access only one slot on each root port.
  64. * Do not read more than one device on the bus directly attached
  65. * to RC's downstream side.
  66. */
  67. if (pci_is_root_bus(bus) || pci_is_root_bus(bus->parent))
  68. return dev == 0;
  69. return 1;
  70. }
  71. static u8 rockchip_pcie_lane_map(struct rockchip_pcie *rockchip)
  72. {
  73. u32 val;
  74. u8 map;
  75. if (rockchip->legacy_phy)
  76. return GENMASK(MAX_LANE_NUM - 1, 0);
  77. val = rockchip_pcie_read(rockchip, PCIE_CORE_LANE_MAP);
  78. map = val & PCIE_CORE_LANE_MAP_MASK;
  79. /* The link may be using a reverse-indexed mapping. */
  80. if (val & PCIE_CORE_LANE_MAP_REVERSE)
  81. map = bitrev8(map) >> 4;
  82. return map;
  83. }
  84. static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
  85. int where, int size, u32 *val)
  86. {
  87. void __iomem *addr;
  88. addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + where;
  89. if (!IS_ALIGNED((uintptr_t)addr, size)) {
  90. *val = 0;
  91. return PCIBIOS_BAD_REGISTER_NUMBER;
  92. }
  93. if (size == 4) {
  94. *val = readl(addr);
  95. } else if (size == 2) {
  96. *val = readw(addr);
  97. } else if (size == 1) {
  98. *val = readb(addr);
  99. } else {
  100. *val = 0;
  101. return PCIBIOS_BAD_REGISTER_NUMBER;
  102. }
  103. return PCIBIOS_SUCCESSFUL;
  104. }
  105. static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
  106. int where, int size, u32 val)
  107. {
  108. u32 mask, tmp, offset;
  109. void __iomem *addr;
  110. offset = where & ~0x3;
  111. addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + offset;
  112. if (size == 4) {
  113. writel(val, addr);
  114. return PCIBIOS_SUCCESSFUL;
  115. }
  116. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  117. /*
  118. * N.B. This read/modify/write isn't safe in general because it can
  119. * corrupt RW1C bits in adjacent registers. But the hardware
  120. * doesn't support smaller writes.
  121. */
  122. tmp = readl(addr) & mask;
  123. tmp |= val << ((where & 0x3) * 8);
  124. writel(tmp, addr);
  125. return PCIBIOS_SUCCESSFUL;
  126. }
  127. static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
  128. struct pci_bus *bus, u32 devfn,
  129. int where, int size, u32 *val)
  130. {
  131. void __iomem *addr;
  132. addr = rockchip->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
  133. if (!IS_ALIGNED((uintptr_t)addr, size)) {
  134. *val = 0;
  135. return PCIBIOS_BAD_REGISTER_NUMBER;
  136. }
  137. if (pci_is_root_bus(bus->parent))
  138. rockchip_pcie_cfg_configuration_accesses(rockchip,
  139. AXI_WRAPPER_TYPE0_CFG);
  140. else
  141. rockchip_pcie_cfg_configuration_accesses(rockchip,
  142. AXI_WRAPPER_TYPE1_CFG);
  143. if (size == 4) {
  144. *val = readl(addr);
  145. } else if (size == 2) {
  146. *val = readw(addr);
  147. } else if (size == 1) {
  148. *val = readb(addr);
  149. } else {
  150. *val = 0;
  151. return PCIBIOS_BAD_REGISTER_NUMBER;
  152. }
  153. return PCIBIOS_SUCCESSFUL;
  154. }
  155. static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
  156. struct pci_bus *bus, u32 devfn,
  157. int where, int size, u32 val)
  158. {
  159. void __iomem *addr;
  160. addr = rockchip->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
  161. if (!IS_ALIGNED((uintptr_t)addr, size))
  162. return PCIBIOS_BAD_REGISTER_NUMBER;
  163. if (pci_is_root_bus(bus->parent))
  164. rockchip_pcie_cfg_configuration_accesses(rockchip,
  165. AXI_WRAPPER_TYPE0_CFG);
  166. else
  167. rockchip_pcie_cfg_configuration_accesses(rockchip,
  168. AXI_WRAPPER_TYPE1_CFG);
  169. if (size == 4)
  170. writel(val, addr);
  171. else if (size == 2)
  172. writew(val, addr);
  173. else if (size == 1)
  174. writeb(val, addr);
  175. else
  176. return PCIBIOS_BAD_REGISTER_NUMBER;
  177. return PCIBIOS_SUCCESSFUL;
  178. }
  179. static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  180. int size, u32 *val)
  181. {
  182. struct rockchip_pcie *rockchip = bus->sysdata;
  183. if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
  184. return PCIBIOS_DEVICE_NOT_FOUND;
  185. if (pci_is_root_bus(bus))
  186. return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
  187. return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size,
  188. val);
  189. }
  190. static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  191. int where, int size, u32 val)
  192. {
  193. struct rockchip_pcie *rockchip = bus->sysdata;
  194. if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
  195. return PCIBIOS_DEVICE_NOT_FOUND;
  196. if (pci_is_root_bus(bus))
  197. return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
  198. return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size,
  199. val);
  200. }
  201. static struct pci_ops rockchip_pcie_ops = {
  202. .read = rockchip_pcie_rd_conf,
  203. .write = rockchip_pcie_wr_conf,
  204. };
  205. static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
  206. {
  207. int curr;
  208. u32 status, scale, power;
  209. if (IS_ERR(rockchip->vpcie3v3))
  210. return;
  211. /*
  212. * Set RC's captured slot power limit and scale if
  213. * vpcie3v3 available. The default values are both zero
  214. * which means the software should set these two according
  215. * to the actual power supply.
  216. */
  217. curr = regulator_get_current_limit(rockchip->vpcie3v3);
  218. if (curr <= 0)
  219. return;
  220. scale = 3; /* 0.001x */
  221. curr = curr / 1000; /* convert to mA */
  222. power = (curr * 3300) / 1000; /* milliwatt */
  223. while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
  224. if (!scale) {
  225. dev_warn(rockchip->dev, "invalid power supply\n");
  226. return;
  227. }
  228. scale--;
  229. power = power / 10;
  230. }
  231. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
  232. status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
  233. (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
  234. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
  235. }
  236. /**
  237. * rockchip_pcie_host_init_port - Initialize hardware
  238. * @rockchip: PCIe port information
  239. */
  240. static int rockchip_pcie_host_init_port(struct rockchip_pcie *rockchip)
  241. {
  242. struct device *dev = rockchip->dev;
  243. int err, i = MAX_LANE_NUM;
  244. u32 status;
  245. gpiod_set_value_cansleep(rockchip->ep_gpio, 0);
  246. err = rockchip_pcie_init_port(rockchip);
  247. if (err)
  248. return err;
  249. /* Fix the transmitted FTS count desired to exit from L0s. */
  250. status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
  251. status = (status & ~PCIE_CORE_CTRL_PLC1_FTS_MASK) |
  252. (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
  253. rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
  254. rockchip_pcie_set_power_limit(rockchip);
  255. /* Set RC's clock architecture as common clock */
  256. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  257. status |= PCI_EXP_LNKSTA_SLC << 16;
  258. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  259. /* Set RC's RCB to 128 */
  260. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  261. status |= PCI_EXP_LNKCTL_RCB;
  262. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  263. /* Enable Gen1 training */
  264. rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
  265. PCIE_CLIENT_CONFIG);
  266. msleep(PCIE_T_PVPERL_MS);
  267. gpiod_set_value_cansleep(rockchip->ep_gpio, 1);
  268. msleep(PCIE_T_RRS_READY_MS);
  269. /* 500ms timeout value should be enough for Gen1/2 training */
  270. err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_BASIC_STATUS1,
  271. status, PCIE_LINK_UP(status), 20,
  272. 500 * USEC_PER_MSEC);
  273. if (err) {
  274. dev_err(dev, "PCIe link training gen1 timeout!\n");
  275. goto err_power_off_phy;
  276. }
  277. if (rockchip->link_gen == 2) {
  278. /*
  279. * Enable retrain for gen2. This should be configured only after
  280. * gen1 finished.
  281. */
  282. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  283. status |= PCI_EXP_LNKCTL_RL;
  284. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  285. err = readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
  286. status, PCIE_LINK_IS_GEN2(status), 20,
  287. 500 * USEC_PER_MSEC);
  288. if (err)
  289. dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
  290. }
  291. /* Check the final link width from negotiated lane counter from MGMT */
  292. status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
  293. status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
  294. PCIE_CORE_PL_CONF_LANE_SHIFT);
  295. dev_dbg(dev, "current link width is x%d\n", status);
  296. /* Power off unused lane(s) */
  297. rockchip->lanes_map = rockchip_pcie_lane_map(rockchip);
  298. for (i = 0; i < MAX_LANE_NUM; i++) {
  299. if (!(rockchip->lanes_map & BIT(i))) {
  300. dev_dbg(dev, "idling lane %d\n", i);
  301. phy_power_off(rockchip->phys[i]);
  302. }
  303. }
  304. rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
  305. PCIE_CORE_CONFIG_VENDOR);
  306. rockchip_pcie_write(rockchip,
  307. PCI_CLASS_BRIDGE_PCI_NORMAL << 8,
  308. PCIE_RC_CONFIG_RID_CCR);
  309. /* Clear THP cap's next cap pointer to remove L1 substate cap */
  310. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_THP_CAP);
  311. status &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK;
  312. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_THP_CAP);
  313. /* Clear L0s from RC's link cap */
  314. if (of_property_read_bool(dev->of_node, "aspm-no-l0s")) {
  315. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LINK_CAP);
  316. status &= ~PCIE_RC_CONFIG_LINK_CAP_L0S;
  317. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LINK_CAP);
  318. }
  319. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCSR);
  320. status &= ~PCIE_RC_CONFIG_DCSR_MPS_MASK;
  321. status |= PCIE_RC_CONFIG_DCSR_MPS_256;
  322. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCSR);
  323. return 0;
  324. err_power_off_phy:
  325. while (i--)
  326. phy_power_off(rockchip->phys[i]);
  327. i = MAX_LANE_NUM;
  328. while (i--)
  329. phy_exit(rockchip->phys[i]);
  330. return err;
  331. }
  332. static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
  333. {
  334. struct rockchip_pcie *rockchip = arg;
  335. struct device *dev = rockchip->dev;
  336. u32 reg;
  337. u32 sub_reg;
  338. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  339. if (reg & PCIE_CLIENT_INT_LOCAL) {
  340. dev_dbg(dev, "local interrupt received\n");
  341. sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
  342. if (sub_reg & PCIE_CORE_INT_PRFPE)
  343. dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
  344. if (sub_reg & PCIE_CORE_INT_CRFPE)
  345. dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
  346. if (sub_reg & PCIE_CORE_INT_RRPE)
  347. dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
  348. if (sub_reg & PCIE_CORE_INT_PRFO)
  349. dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
  350. if (sub_reg & PCIE_CORE_INT_CRFO)
  351. dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
  352. if (sub_reg & PCIE_CORE_INT_RT)
  353. dev_dbg(dev, "replay timer timed out\n");
  354. if (sub_reg & PCIE_CORE_INT_RTR)
  355. dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
  356. if (sub_reg & PCIE_CORE_INT_PE)
  357. dev_dbg(dev, "phy error detected on receive side\n");
  358. if (sub_reg & PCIE_CORE_INT_MTR)
  359. dev_dbg(dev, "malformed TLP received from the link\n");
  360. if (sub_reg & PCIE_CORE_INT_UCR)
  361. dev_dbg(dev, "malformed TLP received from the link\n");
  362. if (sub_reg & PCIE_CORE_INT_FCE)
  363. dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
  364. if (sub_reg & PCIE_CORE_INT_CT)
  365. dev_dbg(dev, "a request timed out waiting for completion\n");
  366. if (sub_reg & PCIE_CORE_INT_UTC)
  367. dev_dbg(dev, "unmapped TC error\n");
  368. if (sub_reg & PCIE_CORE_INT_MMVC)
  369. dev_dbg(dev, "MSI mask register changes\n");
  370. rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
  371. } else if (reg & PCIE_CLIENT_INT_PHY) {
  372. dev_dbg(dev, "phy link changes\n");
  373. rockchip_pcie_update_txcredit_mui(rockchip);
  374. rockchip_pcie_clr_bw_int(rockchip);
  375. }
  376. rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
  377. PCIE_CLIENT_INT_STATUS);
  378. return IRQ_HANDLED;
  379. }
  380. static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
  381. {
  382. struct rockchip_pcie *rockchip = arg;
  383. struct device *dev = rockchip->dev;
  384. u32 reg;
  385. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  386. if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
  387. dev_dbg(dev, "legacy done interrupt received\n");
  388. if (reg & PCIE_CLIENT_INT_MSG)
  389. dev_dbg(dev, "message done interrupt received\n");
  390. if (reg & PCIE_CLIENT_INT_HOT_RST)
  391. dev_dbg(dev, "hot reset interrupt received\n");
  392. if (reg & PCIE_CLIENT_INT_DPA)
  393. dev_dbg(dev, "dpa interrupt received\n");
  394. if (reg & PCIE_CLIENT_INT_FATAL_ERR)
  395. dev_dbg(dev, "fatal error interrupt received\n");
  396. if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
  397. dev_dbg(dev, "no fatal error interrupt received\n");
  398. if (reg & PCIE_CLIENT_INT_CORR_ERR)
  399. dev_dbg(dev, "correctable error interrupt received\n");
  400. if (reg & PCIE_CLIENT_INT_PHY)
  401. dev_dbg(dev, "phy interrupt received\n");
  402. rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
  403. PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
  404. PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
  405. PCIE_CLIENT_INT_NFATAL_ERR |
  406. PCIE_CLIENT_INT_CORR_ERR |
  407. PCIE_CLIENT_INT_PHY),
  408. PCIE_CLIENT_INT_STATUS);
  409. return IRQ_HANDLED;
  410. }
  411. static void rockchip_pcie_intx_handler(struct irq_desc *desc)
  412. {
  413. struct irq_chip *chip = irq_desc_get_chip(desc);
  414. struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
  415. struct device *dev = rockchip->dev;
  416. u32 reg;
  417. u32 hwirq;
  418. int ret;
  419. chained_irq_enter(chip, desc);
  420. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  421. reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
  422. while (reg) {
  423. hwirq = ffs(reg) - 1;
  424. reg &= ~BIT(hwirq);
  425. ret = generic_handle_domain_irq(rockchip->irq_domain, hwirq);
  426. if (ret)
  427. dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
  428. }
  429. chained_irq_exit(chip, desc);
  430. }
  431. static int rockchip_pcie_setup_irq(struct rockchip_pcie *rockchip)
  432. {
  433. int irq, err;
  434. struct device *dev = rockchip->dev;
  435. struct platform_device *pdev = to_platform_device(dev);
  436. irq = platform_get_irq_byname(pdev, "sys");
  437. if (irq < 0)
  438. return irq;
  439. err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
  440. IRQF_SHARED, "pcie-sys", rockchip);
  441. if (err) {
  442. dev_err(dev, "failed to request PCIe subsystem IRQ\n");
  443. return err;
  444. }
  445. irq = platform_get_irq_byname(pdev, "legacy");
  446. if (irq < 0)
  447. return irq;
  448. irq_set_chained_handler_and_data(irq,
  449. rockchip_pcie_intx_handler,
  450. rockchip);
  451. irq = platform_get_irq_byname(pdev, "client");
  452. if (irq < 0)
  453. return irq;
  454. err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
  455. IRQF_SHARED, "pcie-client", rockchip);
  456. if (err) {
  457. dev_err(dev, "failed to request PCIe client IRQ\n");
  458. return err;
  459. }
  460. return 0;
  461. }
  462. /**
  463. * rockchip_pcie_parse_host_dt - Parse Device Tree
  464. * @rockchip: PCIe port information
  465. *
  466. * Return: '0' on success and error value on failure
  467. */
  468. static int rockchip_pcie_parse_host_dt(struct rockchip_pcie *rockchip)
  469. {
  470. struct device *dev = rockchip->dev;
  471. int err;
  472. err = rockchip_pcie_parse_dt(rockchip);
  473. if (err)
  474. return err;
  475. rockchip->vpcie12v = devm_regulator_get_optional(dev, "vpcie12v");
  476. if (IS_ERR(rockchip->vpcie12v)) {
  477. if (PTR_ERR(rockchip->vpcie12v) != -ENODEV)
  478. return PTR_ERR(rockchip->vpcie12v);
  479. dev_info(dev, "no vpcie12v regulator found\n");
  480. }
  481. rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
  482. if (IS_ERR(rockchip->vpcie3v3)) {
  483. if (PTR_ERR(rockchip->vpcie3v3) != -ENODEV)
  484. return PTR_ERR(rockchip->vpcie3v3);
  485. dev_info(dev, "no vpcie3v3 regulator found\n");
  486. }
  487. rockchip->vpcie1v8 = devm_regulator_get(dev, "vpcie1v8");
  488. if (IS_ERR(rockchip->vpcie1v8))
  489. return PTR_ERR(rockchip->vpcie1v8);
  490. rockchip->vpcie0v9 = devm_regulator_get(dev, "vpcie0v9");
  491. if (IS_ERR(rockchip->vpcie0v9))
  492. return PTR_ERR(rockchip->vpcie0v9);
  493. return 0;
  494. }
  495. static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
  496. {
  497. struct device *dev = rockchip->dev;
  498. int err;
  499. if (!IS_ERR(rockchip->vpcie12v)) {
  500. err = regulator_enable(rockchip->vpcie12v);
  501. if (err) {
  502. dev_err(dev, "fail to enable vpcie12v regulator\n");
  503. goto err_out;
  504. }
  505. }
  506. if (!IS_ERR(rockchip->vpcie3v3)) {
  507. err = regulator_enable(rockchip->vpcie3v3);
  508. if (err) {
  509. dev_err(dev, "fail to enable vpcie3v3 regulator\n");
  510. goto err_disable_12v;
  511. }
  512. }
  513. err = regulator_enable(rockchip->vpcie1v8);
  514. if (err) {
  515. dev_err(dev, "fail to enable vpcie1v8 regulator\n");
  516. goto err_disable_3v3;
  517. }
  518. err = regulator_enable(rockchip->vpcie0v9);
  519. if (err) {
  520. dev_err(dev, "fail to enable vpcie0v9 regulator\n");
  521. goto err_disable_1v8;
  522. }
  523. return 0;
  524. err_disable_1v8:
  525. regulator_disable(rockchip->vpcie1v8);
  526. err_disable_3v3:
  527. if (!IS_ERR(rockchip->vpcie3v3))
  528. regulator_disable(rockchip->vpcie3v3);
  529. err_disable_12v:
  530. if (!IS_ERR(rockchip->vpcie12v))
  531. regulator_disable(rockchip->vpcie12v);
  532. err_out:
  533. return err;
  534. }
  535. static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
  536. {
  537. rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
  538. (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
  539. rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
  540. PCIE_CORE_INT_MASK);
  541. rockchip_pcie_enable_bw_int(rockchip);
  542. }
  543. static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  544. irq_hw_number_t hwirq)
  545. {
  546. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  547. irq_set_chip_data(irq, domain->host_data);
  548. return 0;
  549. }
  550. static const struct irq_domain_ops intx_domain_ops = {
  551. .map = rockchip_pcie_intx_map,
  552. };
  553. static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
  554. {
  555. struct device *dev = rockchip->dev;
  556. struct device_node *intc = of_get_next_child(dev->of_node, NULL);
  557. if (!intc) {
  558. dev_err(dev, "missing child interrupt-controller node\n");
  559. return -EINVAL;
  560. }
  561. rockchip->irq_domain = irq_domain_add_linear(intc, PCI_NUM_INTX,
  562. &intx_domain_ops, rockchip);
  563. of_node_put(intc);
  564. if (!rockchip->irq_domain) {
  565. dev_err(dev, "failed to get a INTx IRQ domain\n");
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
  571. int region_no, int type, u8 num_pass_bits,
  572. u32 lower_addr, u32 upper_addr)
  573. {
  574. u32 ob_addr_0;
  575. u32 ob_addr_1;
  576. u32 ob_desc_0;
  577. u32 aw_offset;
  578. if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
  579. return -EINVAL;
  580. if (num_pass_bits + 1 < 8)
  581. return -EINVAL;
  582. if (num_pass_bits > 63)
  583. return -EINVAL;
  584. if (region_no == 0) {
  585. if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
  586. return -EINVAL;
  587. }
  588. if (region_no != 0) {
  589. if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
  590. return -EINVAL;
  591. }
  592. aw_offset = (region_no << OB_REG_SIZE_SHIFT);
  593. ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
  594. ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
  595. ob_addr_1 = upper_addr;
  596. ob_desc_0 = (1 << 23 | type);
  597. rockchip_pcie_write(rockchip, ob_addr_0,
  598. PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
  599. rockchip_pcie_write(rockchip, ob_addr_1,
  600. PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
  601. rockchip_pcie_write(rockchip, ob_desc_0,
  602. PCIE_CORE_OB_REGION_DESC0 + aw_offset);
  603. rockchip_pcie_write(rockchip, 0,
  604. PCIE_CORE_OB_REGION_DESC1 + aw_offset);
  605. return 0;
  606. }
  607. static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
  608. int region_no, u8 num_pass_bits,
  609. u32 lower_addr, u32 upper_addr)
  610. {
  611. u32 ib_addr_0;
  612. u32 ib_addr_1;
  613. u32 aw_offset;
  614. if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
  615. return -EINVAL;
  616. if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
  617. return -EINVAL;
  618. if (num_pass_bits > 63)
  619. return -EINVAL;
  620. aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
  621. ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
  622. ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
  623. ib_addr_1 = upper_addr;
  624. rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
  625. rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
  626. return 0;
  627. }
  628. static int rockchip_pcie_cfg_atu(struct rockchip_pcie *rockchip)
  629. {
  630. struct device *dev = rockchip->dev;
  631. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(rockchip);
  632. struct resource_entry *entry;
  633. u64 pci_addr, size;
  634. int offset;
  635. int err;
  636. int reg_no;
  637. rockchip_pcie_cfg_configuration_accesses(rockchip,
  638. AXI_WRAPPER_TYPE0_CFG);
  639. entry = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
  640. if (!entry)
  641. return -ENODEV;
  642. size = resource_size(entry->res);
  643. pci_addr = entry->res->start - entry->offset;
  644. rockchip->msg_bus_addr = pci_addr;
  645. for (reg_no = 0; reg_no < (size >> 20); reg_no++) {
  646. err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
  647. AXI_WRAPPER_MEM_WRITE,
  648. 20 - 1,
  649. pci_addr + (reg_no << 20),
  650. 0);
  651. if (err) {
  652. dev_err(dev, "program RC mem outbound ATU failed\n");
  653. return err;
  654. }
  655. }
  656. err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
  657. if (err) {
  658. dev_err(dev, "program RC mem inbound ATU failed\n");
  659. return err;
  660. }
  661. entry = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
  662. if (!entry)
  663. return -ENODEV;
  664. /* store the register number offset to program RC io outbound ATU */
  665. offset = size >> 20;
  666. size = resource_size(entry->res);
  667. pci_addr = entry->res->start - entry->offset;
  668. for (reg_no = 0; reg_no < (size >> 20); reg_no++) {
  669. err = rockchip_pcie_prog_ob_atu(rockchip,
  670. reg_no + 1 + offset,
  671. AXI_WRAPPER_IO_WRITE,
  672. 20 - 1,
  673. pci_addr + (reg_no << 20),
  674. 0);
  675. if (err) {
  676. dev_err(dev, "program RC io outbound ATU failed\n");
  677. return err;
  678. }
  679. }
  680. /* assign message regions */
  681. rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
  682. AXI_WRAPPER_NOR_MSG,
  683. 20 - 1, 0, 0);
  684. rockchip->msg_bus_addr += ((reg_no + offset) << 20);
  685. return err;
  686. }
  687. static int rockchip_pcie_wait_l2(struct rockchip_pcie *rockchip)
  688. {
  689. u32 value;
  690. int err;
  691. /* send PME_TURN_OFF message */
  692. writel(0x0, rockchip->msg_region + PCIE_RC_SEND_PME_OFF);
  693. /* read LTSSM and wait for falling into L2 link state */
  694. err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_DEBUG_OUT_0,
  695. value, PCIE_LINK_IS_L2(value), 20,
  696. jiffies_to_usecs(5 * HZ));
  697. if (err) {
  698. dev_err(rockchip->dev, "PCIe link enter L2 timeout!\n");
  699. return err;
  700. }
  701. return 0;
  702. }
  703. static int rockchip_pcie_suspend_noirq(struct device *dev)
  704. {
  705. struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
  706. int ret;
  707. /* disable core and cli int since we don't need to ack PME_ACK */
  708. rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) |
  709. PCIE_CLIENT_INT_CLI, PCIE_CLIENT_INT_MASK);
  710. rockchip_pcie_write(rockchip, (u32)PCIE_CORE_INT, PCIE_CORE_INT_MASK);
  711. ret = rockchip_pcie_wait_l2(rockchip);
  712. if (ret) {
  713. rockchip_pcie_enable_interrupts(rockchip);
  714. return ret;
  715. }
  716. rockchip_pcie_deinit_phys(rockchip);
  717. rockchip_pcie_disable_clocks(rockchip);
  718. regulator_disable(rockchip->vpcie0v9);
  719. return ret;
  720. }
  721. static int rockchip_pcie_resume_noirq(struct device *dev)
  722. {
  723. struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
  724. int err;
  725. err = regulator_enable(rockchip->vpcie0v9);
  726. if (err) {
  727. dev_err(dev, "fail to enable vpcie0v9 regulator\n");
  728. return err;
  729. }
  730. err = rockchip_pcie_enable_clocks(rockchip);
  731. if (err)
  732. goto err_disable_0v9;
  733. err = rockchip_pcie_host_init_port(rockchip);
  734. if (err)
  735. goto err_pcie_resume;
  736. err = rockchip_pcie_cfg_atu(rockchip);
  737. if (err)
  738. goto err_err_deinit_port;
  739. /* Need this to enter L1 again */
  740. rockchip_pcie_update_txcredit_mui(rockchip);
  741. rockchip_pcie_enable_interrupts(rockchip);
  742. return 0;
  743. err_err_deinit_port:
  744. rockchip_pcie_deinit_phys(rockchip);
  745. err_pcie_resume:
  746. rockchip_pcie_disable_clocks(rockchip);
  747. err_disable_0v9:
  748. regulator_disable(rockchip->vpcie0v9);
  749. return err;
  750. }
  751. static int rockchip_pcie_probe(struct platform_device *pdev)
  752. {
  753. struct rockchip_pcie *rockchip;
  754. struct device *dev = &pdev->dev;
  755. struct pci_host_bridge *bridge;
  756. int err;
  757. if (!dev->of_node)
  758. return -ENODEV;
  759. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rockchip));
  760. if (!bridge)
  761. return -ENOMEM;
  762. rockchip = pci_host_bridge_priv(bridge);
  763. platform_set_drvdata(pdev, rockchip);
  764. rockchip->dev = dev;
  765. rockchip->is_rc = true;
  766. err = rockchip_pcie_parse_host_dt(rockchip);
  767. if (err)
  768. return err;
  769. err = rockchip_pcie_enable_clocks(rockchip);
  770. if (err)
  771. return err;
  772. err = rockchip_pcie_set_vpcie(rockchip);
  773. if (err) {
  774. dev_err(dev, "failed to set vpcie regulator\n");
  775. goto err_set_vpcie;
  776. }
  777. err = rockchip_pcie_host_init_port(rockchip);
  778. if (err)
  779. goto err_vpcie;
  780. err = rockchip_pcie_init_irq_domain(rockchip);
  781. if (err < 0)
  782. goto err_deinit_port;
  783. err = rockchip_pcie_cfg_atu(rockchip);
  784. if (err)
  785. goto err_remove_irq_domain;
  786. rockchip->msg_region = devm_ioremap(dev, rockchip->msg_bus_addr, SZ_1M);
  787. if (!rockchip->msg_region) {
  788. err = -ENOMEM;
  789. goto err_remove_irq_domain;
  790. }
  791. bridge->sysdata = rockchip;
  792. bridge->ops = &rockchip_pcie_ops;
  793. err = rockchip_pcie_setup_irq(rockchip);
  794. if (err)
  795. goto err_remove_irq_domain;
  796. rockchip_pcie_enable_interrupts(rockchip);
  797. err = pci_host_probe(bridge);
  798. if (err < 0)
  799. goto err_remove_irq_domain;
  800. return 0;
  801. err_remove_irq_domain:
  802. irq_domain_remove(rockchip->irq_domain);
  803. err_deinit_port:
  804. rockchip_pcie_deinit_phys(rockchip);
  805. err_vpcie:
  806. if (!IS_ERR(rockchip->vpcie12v))
  807. regulator_disable(rockchip->vpcie12v);
  808. if (!IS_ERR(rockchip->vpcie3v3))
  809. regulator_disable(rockchip->vpcie3v3);
  810. regulator_disable(rockchip->vpcie1v8);
  811. regulator_disable(rockchip->vpcie0v9);
  812. err_set_vpcie:
  813. rockchip_pcie_disable_clocks(rockchip);
  814. return err;
  815. }
  816. static void rockchip_pcie_remove(struct platform_device *pdev)
  817. {
  818. struct device *dev = &pdev->dev;
  819. struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
  820. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(rockchip);
  821. pci_stop_root_bus(bridge->bus);
  822. pci_remove_root_bus(bridge->bus);
  823. irq_domain_remove(rockchip->irq_domain);
  824. rockchip_pcie_deinit_phys(rockchip);
  825. rockchip_pcie_disable_clocks(rockchip);
  826. if (!IS_ERR(rockchip->vpcie12v))
  827. regulator_disable(rockchip->vpcie12v);
  828. if (!IS_ERR(rockchip->vpcie3v3))
  829. regulator_disable(rockchip->vpcie3v3);
  830. regulator_disable(rockchip->vpcie1v8);
  831. regulator_disable(rockchip->vpcie0v9);
  832. }
  833. static const struct dev_pm_ops rockchip_pcie_pm_ops = {
  834. NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_pcie_suspend_noirq,
  835. rockchip_pcie_resume_noirq)
  836. };
  837. static const struct of_device_id rockchip_pcie_of_match[] = {
  838. { .compatible = "rockchip,rk3399-pcie", },
  839. {}
  840. };
  841. MODULE_DEVICE_TABLE(of, rockchip_pcie_of_match);
  842. static struct platform_driver rockchip_pcie_driver = {
  843. .driver = {
  844. .name = "rockchip-pcie",
  845. .of_match_table = rockchip_pcie_of_match,
  846. .pm = &rockchip_pcie_pm_ops,
  847. },
  848. .probe = rockchip_pcie_probe,
  849. .remove_new = rockchip_pcie_remove,
  850. };
  851. module_platform_driver(rockchip_pcie_driver);
  852. MODULE_AUTHOR("Rockchip Inc");
  853. MODULE_DESCRIPTION("Rockchip AXI PCIe driver");
  854. MODULE_LICENSE("GPL v2");