pcie-rockchip-host.c 28 KB

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