pcie-rockchip-host.c 30 KB

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