pcie-rockchip.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * Rockchip AXI PCIe host controller driver
  3. *
  4. * Copyright (c) 2016 Rockchip, Inc.
  5. *
  6. * Author: Shawn Lin <shawn.lin@rock-chips.com>
  7. * Wenrui Li <wenrui.li@rock-chips.com>
  8. *
  9. * Bits taken from Synopsys Designware Host controller driver and
  10. * ARM PCI Host generic driver.
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/irqchip/chained_irq.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_pci.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/pci.h>
  33. #include <linux/pci_ids.h>
  34. #include <linux/phy/phy.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/reset.h>
  37. #include <linux/regmap.h>
  38. /*
  39. * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16
  40. * bits. This allows atomic updates of the register without locking.
  41. */
  42. #define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val))
  43. #define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val)
  44. #define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4)
  45. #define PCIE_CLIENT_BASE 0x0
  46. #define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00)
  47. #define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001)
  48. #define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002)
  49. #define PCIE_CLIENT_ARI_ENABLE HIWORD_UPDATE_BIT(0x0008)
  50. #define PCIE_CLIENT_CONF_LANE_NUM(x) HIWORD_UPDATE(0x0030, ENCODE_LANES(x))
  51. #define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040)
  52. #define PCIE_CLIENT_GEN_SEL_2 HIWORD_UPDATE_BIT(0x0080)
  53. #define PCIE_CLIENT_BASIC_STATUS1 (PCIE_CLIENT_BASE + 0x48)
  54. #define PCIE_CLIENT_LINK_STATUS_UP 0x00300000
  55. #define PCIE_CLIENT_LINK_STATUS_MASK 0x00300000
  56. #define PCIE_CLIENT_INT_MASK (PCIE_CLIENT_BASE + 0x4c)
  57. #define PCIE_CLIENT_INT_STATUS (PCIE_CLIENT_BASE + 0x50)
  58. #define PCIE_CLIENT_INTR_MASK GENMASK(8, 5)
  59. #define PCIE_CLIENT_INTR_SHIFT 5
  60. #define PCIE_CLIENT_INT_LEGACY_DONE BIT(15)
  61. #define PCIE_CLIENT_INT_MSG BIT(14)
  62. #define PCIE_CLIENT_INT_HOT_RST BIT(13)
  63. #define PCIE_CLIENT_INT_DPA BIT(12)
  64. #define PCIE_CLIENT_INT_FATAL_ERR BIT(11)
  65. #define PCIE_CLIENT_INT_NFATAL_ERR BIT(10)
  66. #define PCIE_CLIENT_INT_CORR_ERR BIT(9)
  67. #define PCIE_CLIENT_INT_INTD BIT(8)
  68. #define PCIE_CLIENT_INT_INTC BIT(7)
  69. #define PCIE_CLIENT_INT_INTB BIT(6)
  70. #define PCIE_CLIENT_INT_INTA BIT(5)
  71. #define PCIE_CLIENT_INT_LOCAL BIT(4)
  72. #define PCIE_CLIENT_INT_UDMA BIT(3)
  73. #define PCIE_CLIENT_INT_PHY BIT(2)
  74. #define PCIE_CLIENT_INT_HOT_PLUG BIT(1)
  75. #define PCIE_CLIENT_INT_PWR_STCG BIT(0)
  76. #define PCIE_CLIENT_INT_LEGACY \
  77. (PCIE_CLIENT_INT_INTA | PCIE_CLIENT_INT_INTB | \
  78. PCIE_CLIENT_INT_INTC | PCIE_CLIENT_INT_INTD)
  79. #define PCIE_CLIENT_INT_CLI \
  80. (PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | \
  81. PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | \
  82. PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | \
  83. PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_LEGACY | \
  84. PCIE_CLIENT_INT_PHY)
  85. #define PCIE_CORE_CTRL_MGMT_BASE 0x900000
  86. #define PCIE_CORE_CTRL (PCIE_CORE_CTRL_MGMT_BASE + 0x000)
  87. #define PCIE_CORE_PL_CONF_SPEED_5G 0x00000008
  88. #define PCIE_CORE_PL_CONF_SPEED_MASK 0x00000018
  89. #define PCIE_CORE_PL_CONF_LANE_MASK 0x00000006
  90. #define PCIE_CORE_PL_CONF_LANE_SHIFT 1
  91. #define PCIE_CORE_CTRL_PLC1 (PCIE_CORE_CTRL_MGMT_BASE + 0x004)
  92. #define PCIE_CORE_CTRL_PLC1_FTS_MASK GENMASK(23, 8)
  93. #define PCIE_CORE_CTRL_PLC1_FTS_SHIFT 8
  94. #define PCIE_CORE_CTRL_PLC1_FTS_CNT 0xffff
  95. #define PCIE_CORE_TXCREDIT_CFG1 (PCIE_CORE_CTRL_MGMT_BASE + 0x020)
  96. #define PCIE_CORE_TXCREDIT_CFG1_MUI_MASK 0xFFFF0000
  97. #define PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT 16
  98. #define PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(x) \
  99. (((x) >> 3) << PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT)
  100. #define PCIE_CORE_INT_STATUS (PCIE_CORE_CTRL_MGMT_BASE + 0x20c)
  101. #define PCIE_CORE_INT_PRFPE BIT(0)
  102. #define PCIE_CORE_INT_CRFPE BIT(1)
  103. #define PCIE_CORE_INT_RRPE BIT(2)
  104. #define PCIE_CORE_INT_PRFO BIT(3)
  105. #define PCIE_CORE_INT_CRFO BIT(4)
  106. #define PCIE_CORE_INT_RT BIT(5)
  107. #define PCIE_CORE_INT_RTR BIT(6)
  108. #define PCIE_CORE_INT_PE BIT(7)
  109. #define PCIE_CORE_INT_MTR BIT(8)
  110. #define PCIE_CORE_INT_UCR BIT(9)
  111. #define PCIE_CORE_INT_FCE BIT(10)
  112. #define PCIE_CORE_INT_CT BIT(11)
  113. #define PCIE_CORE_INT_UTC BIT(18)
  114. #define PCIE_CORE_INT_MMVC BIT(19)
  115. #define PCIE_CORE_INT_MASK (PCIE_CORE_CTRL_MGMT_BASE + 0x210)
  116. #define PCIE_RC_BAR_CONF (PCIE_CORE_CTRL_MGMT_BASE + 0x300)
  117. #define PCIE_CORE_INT \
  118. (PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | \
  119. PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | \
  120. PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | \
  121. PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | \
  122. PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | \
  123. PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \
  124. PCIE_CORE_INT_MMVC)
  125. #define PCIE_RC_CONFIG_NORMAL_BASE 0x800000
  126. #define PCIE_RC_CONFIG_BASE 0xa00000
  127. #define PCIE_RC_CONFIG_VENDOR (PCIE_RC_CONFIG_BASE + 0x00)
  128. #define PCIE_RC_CONFIG_RID_CCR (PCIE_RC_CONFIG_BASE + 0x08)
  129. #define PCIE_RC_CONFIG_SCC_SHIFT 16
  130. #define PCIE_RC_CONFIG_LCS (PCIE_RC_CONFIG_BASE + 0xd0)
  131. #define PCIE_RC_CONFIG_LCS_RETRAIN_LINK BIT(5)
  132. #define PCIE_RC_CONFIG_LCS_LBMIE BIT(10)
  133. #define PCIE_RC_CONFIG_LCS_LABIE BIT(11)
  134. #define PCIE_RC_CONFIG_LCS_LBMS BIT(30)
  135. #define PCIE_RC_CONFIG_LCS_LAMS BIT(31)
  136. #define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
  137. #define PCIE_CORE_AXI_CONF_BASE 0xc00000
  138. #define PCIE_CORE_OB_REGION_ADDR0 (PCIE_CORE_AXI_CONF_BASE + 0x0)
  139. #define PCIE_CORE_OB_REGION_ADDR0_NUM_BITS 0x3f
  140. #define PCIE_CORE_OB_REGION_ADDR0_LO_ADDR 0xffffff00
  141. #define PCIE_CORE_OB_REGION_ADDR1 (PCIE_CORE_AXI_CONF_BASE + 0x4)
  142. #define PCIE_CORE_OB_REGION_DESC0 (PCIE_CORE_AXI_CONF_BASE + 0x8)
  143. #define PCIE_CORE_OB_REGION_DESC1 (PCIE_CORE_AXI_CONF_BASE + 0xc)
  144. #define PCIE_CORE_AXI_INBOUND_BASE 0xc00800
  145. #define PCIE_RP_IB_ADDR0 (PCIE_CORE_AXI_INBOUND_BASE + 0x0)
  146. #define PCIE_CORE_IB_REGION_ADDR0_NUM_BITS 0x3f
  147. #define PCIE_CORE_IB_REGION_ADDR0_LO_ADDR 0xffffff00
  148. #define PCIE_RP_IB_ADDR1 (PCIE_CORE_AXI_INBOUND_BASE + 0x4)
  149. /* Size of one AXI Region (not Region 0) */
  150. #define AXI_REGION_SIZE BIT(20)
  151. /* Size of Region 0, equal to sum of sizes of other regions */
  152. #define AXI_REGION_0_SIZE (32 * (0x1 << 20))
  153. #define OB_REG_SIZE_SHIFT 5
  154. #define IB_ROOT_PORT_REG_SIZE_SHIFT 3
  155. #define AXI_WRAPPER_IO_WRITE 0x6
  156. #define AXI_WRAPPER_MEM_WRITE 0x2
  157. #define MAX_AXI_IB_ROOTPORT_REGION_NUM 3
  158. #define MIN_AXI_ADDR_BITS_PASSED 8
  159. #define ROCKCHIP_VENDOR_ID 0x1d87
  160. #define PCIE_ECAM_BUS(x) (((x) & 0xff) << 20)
  161. #define PCIE_ECAM_DEV(x) (((x) & 0x1f) << 15)
  162. #define PCIE_ECAM_FUNC(x) (((x) & 0x7) << 12)
  163. #define PCIE_ECAM_REG(x) (((x) & 0xfff) << 0)
  164. #define PCIE_ECAM_ADDR(bus, dev, func, reg) \
  165. (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
  166. PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
  167. #define RC_REGION_0_ADDR_TRANS_H 0x00000000
  168. #define RC_REGION_0_ADDR_TRANS_L 0x00000000
  169. #define RC_REGION_0_PASS_BITS (25 - 1)
  170. #define MAX_AXI_WRAPPER_REGION_NUM 33
  171. struct rockchip_pcie {
  172. void __iomem *reg_base; /* DT axi-base */
  173. void __iomem *apb_base; /* DT apb-base */
  174. struct phy *phy;
  175. struct reset_control *core_rst;
  176. struct reset_control *mgmt_rst;
  177. struct reset_control *mgmt_sticky_rst;
  178. struct reset_control *pipe_rst;
  179. struct reset_control *pm_rst;
  180. struct reset_control *aclk_rst;
  181. struct reset_control *pclk_rst;
  182. struct clk *aclk_pcie;
  183. struct clk *aclk_perf_pcie;
  184. struct clk *hclk_pcie;
  185. struct clk *clk_pcie_pm;
  186. struct regulator *vpcie3v3; /* 3.3V power supply */
  187. struct regulator *vpcie1v8; /* 1.8V power supply */
  188. struct regulator *vpcie0v9; /* 0.9V power supply */
  189. struct gpio_desc *ep_gpio;
  190. u32 lanes;
  191. u8 root_bus_nr;
  192. struct device *dev;
  193. struct irq_domain *irq_domain;
  194. };
  195. static u32 rockchip_pcie_read(struct rockchip_pcie *rockchip, u32 reg)
  196. {
  197. return readl(rockchip->apb_base + reg);
  198. }
  199. static void rockchip_pcie_write(struct rockchip_pcie *rockchip, u32 val,
  200. u32 reg)
  201. {
  202. writel(val, rockchip->apb_base + reg);
  203. }
  204. static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
  205. {
  206. u32 status;
  207. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  208. status |= (PCIE_RC_CONFIG_LCS_LBMIE | PCIE_RC_CONFIG_LCS_LABIE);
  209. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  210. }
  211. static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
  212. {
  213. u32 status;
  214. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  215. status |= (PCIE_RC_CONFIG_LCS_LBMS | PCIE_RC_CONFIG_LCS_LAMS);
  216. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  217. }
  218. static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
  219. {
  220. u32 val;
  221. /* Update Tx credit maximum update interval */
  222. val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
  223. val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
  224. val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */
  225. rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
  226. }
  227. static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
  228. struct pci_bus *bus, int dev)
  229. {
  230. /* access only one slot on each root port */
  231. if (bus->number == rockchip->root_bus_nr && dev > 0)
  232. return 0;
  233. /*
  234. * do not read more than one device on the bus directly attached
  235. * to RC's downstream side.
  236. */
  237. if (bus->primary == rockchip->root_bus_nr && dev > 0)
  238. return 0;
  239. return 1;
  240. }
  241. static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
  242. int where, int size, u32 *val)
  243. {
  244. void __iomem *addr;
  245. addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + where;
  246. if (!IS_ALIGNED((uintptr_t)addr, size)) {
  247. *val = 0;
  248. return PCIBIOS_BAD_REGISTER_NUMBER;
  249. }
  250. if (size == 4) {
  251. *val = readl(addr);
  252. } else if (size == 2) {
  253. *val = readw(addr);
  254. } else if (size == 1) {
  255. *val = readb(addr);
  256. } else {
  257. *val = 0;
  258. return PCIBIOS_BAD_REGISTER_NUMBER;
  259. }
  260. return PCIBIOS_SUCCESSFUL;
  261. }
  262. static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
  263. int where, int size, u32 val)
  264. {
  265. u32 mask, tmp, offset;
  266. void __iomem *addr;
  267. offset = where & ~0x3;
  268. addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + offset;
  269. if (size == 4) {
  270. writel(val, addr);
  271. return PCIBIOS_SUCCESSFUL;
  272. }
  273. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  274. /*
  275. * N.B. This read/modify/write isn't safe in general because it can
  276. * corrupt RW1C bits in adjacent registers. But the hardware
  277. * doesn't support smaller writes.
  278. */
  279. tmp = readl(addr) & mask;
  280. tmp |= val << ((where & 0x3) * 8);
  281. writel(tmp, addr);
  282. return PCIBIOS_SUCCESSFUL;
  283. }
  284. static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
  285. struct pci_bus *bus, u32 devfn,
  286. int where, int size, u32 *val)
  287. {
  288. u32 busdev;
  289. busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
  290. PCI_FUNC(devfn), where);
  291. if (!IS_ALIGNED(busdev, size)) {
  292. *val = 0;
  293. return PCIBIOS_BAD_REGISTER_NUMBER;
  294. }
  295. if (size == 4) {
  296. *val = readl(rockchip->reg_base + busdev);
  297. } else if (size == 2) {
  298. *val = readw(rockchip->reg_base + busdev);
  299. } else if (size == 1) {
  300. *val = readb(rockchip->reg_base + busdev);
  301. } else {
  302. *val = 0;
  303. return PCIBIOS_BAD_REGISTER_NUMBER;
  304. }
  305. return PCIBIOS_SUCCESSFUL;
  306. }
  307. static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
  308. struct pci_bus *bus, u32 devfn,
  309. int where, int size, u32 val)
  310. {
  311. u32 busdev;
  312. busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
  313. PCI_FUNC(devfn), where);
  314. if (!IS_ALIGNED(busdev, size))
  315. return PCIBIOS_BAD_REGISTER_NUMBER;
  316. if (size == 4)
  317. writel(val, rockchip->reg_base + busdev);
  318. else if (size == 2)
  319. writew(val, rockchip->reg_base + busdev);
  320. else if (size == 1)
  321. writeb(val, rockchip->reg_base + busdev);
  322. else
  323. return PCIBIOS_BAD_REGISTER_NUMBER;
  324. return PCIBIOS_SUCCESSFUL;
  325. }
  326. static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  327. int size, u32 *val)
  328. {
  329. struct rockchip_pcie *rockchip = bus->sysdata;
  330. if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) {
  331. *val = 0xffffffff;
  332. return PCIBIOS_DEVICE_NOT_FOUND;
  333. }
  334. if (bus->number == rockchip->root_bus_nr)
  335. return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
  336. return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size, val);
  337. }
  338. static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  339. int where, int size, u32 val)
  340. {
  341. struct rockchip_pcie *rockchip = bus->sysdata;
  342. if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
  343. return PCIBIOS_DEVICE_NOT_FOUND;
  344. if (bus->number == rockchip->root_bus_nr)
  345. return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
  346. return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size, val);
  347. }
  348. static struct pci_ops rockchip_pcie_ops = {
  349. .read = rockchip_pcie_rd_conf,
  350. .write = rockchip_pcie_wr_conf,
  351. };
  352. /**
  353. * rockchip_pcie_init_port - Initialize hardware
  354. * @rockchip: PCIe port information
  355. */
  356. static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
  357. {
  358. struct device *dev = rockchip->dev;
  359. int err;
  360. u32 status;
  361. unsigned long timeout;
  362. gpiod_set_value(rockchip->ep_gpio, 0);
  363. err = reset_control_assert(rockchip->aclk_rst);
  364. if (err) {
  365. dev_err(dev, "assert aclk_rst err %d\n", err);
  366. return err;
  367. }
  368. err = reset_control_assert(rockchip->pclk_rst);
  369. if (err) {
  370. dev_err(dev, "assert pclk_rst err %d\n", err);
  371. return err;
  372. }
  373. err = reset_control_assert(rockchip->pm_rst);
  374. if (err) {
  375. dev_err(dev, "assert pm_rst err %d\n", err);
  376. return err;
  377. }
  378. udelay(10);
  379. err = reset_control_deassert(rockchip->pm_rst);
  380. if (err) {
  381. dev_err(dev, "deassert pm_rst err %d\n", err);
  382. return err;
  383. }
  384. err = reset_control_deassert(rockchip->aclk_rst);
  385. if (err) {
  386. dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
  387. return err;
  388. }
  389. err = reset_control_deassert(rockchip->pclk_rst);
  390. if (err) {
  391. dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
  392. return err;
  393. }
  394. err = phy_init(rockchip->phy);
  395. if (err < 0) {
  396. dev_err(dev, "fail to init phy, err %d\n", err);
  397. return err;
  398. }
  399. err = reset_control_assert(rockchip->core_rst);
  400. if (err) {
  401. dev_err(dev, "assert core_rst err %d\n", err);
  402. return err;
  403. }
  404. err = reset_control_assert(rockchip->mgmt_rst);
  405. if (err) {
  406. dev_err(dev, "assert mgmt_rst err %d\n", err);
  407. return err;
  408. }
  409. err = reset_control_assert(rockchip->mgmt_sticky_rst);
  410. if (err) {
  411. dev_err(dev, "assert mgmt_sticky_rst err %d\n", err);
  412. return err;
  413. }
  414. err = reset_control_assert(rockchip->pipe_rst);
  415. if (err) {
  416. dev_err(dev, "assert pipe_rst err %d\n", err);
  417. return err;
  418. }
  419. rockchip_pcie_write(rockchip,
  420. PCIE_CLIENT_CONF_ENABLE |
  421. PCIE_CLIENT_LINK_TRAIN_ENABLE |
  422. PCIE_CLIENT_ARI_ENABLE |
  423. PCIE_CLIENT_CONF_LANE_NUM(rockchip->lanes) |
  424. PCIE_CLIENT_MODE_RC |
  425. PCIE_CLIENT_GEN_SEL_2,
  426. PCIE_CLIENT_CONFIG);
  427. err = phy_power_on(rockchip->phy);
  428. if (err) {
  429. dev_err(dev, "fail to power on phy, err %d\n", err);
  430. return err;
  431. }
  432. /*
  433. * Please don't reorder the deassert sequence of the following
  434. * four reset pins.
  435. */
  436. err = reset_control_deassert(rockchip->mgmt_sticky_rst);
  437. if (err) {
  438. dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
  439. return err;
  440. }
  441. err = reset_control_deassert(rockchip->core_rst);
  442. if (err) {
  443. dev_err(dev, "deassert core_rst err %d\n", err);
  444. return err;
  445. }
  446. err = reset_control_deassert(rockchip->mgmt_rst);
  447. if (err) {
  448. dev_err(dev, "deassert mgmt_rst err %d\n", err);
  449. return err;
  450. }
  451. err = reset_control_deassert(rockchip->pipe_rst);
  452. if (err) {
  453. dev_err(dev, "deassert pipe_rst err %d\n", err);
  454. return err;
  455. }
  456. /*
  457. * We need to read/write PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 before
  458. * enabling ASPM. Otherwise L1PwrOnSc and L1PwrOnVal isn't
  459. * reliable and enabling ASPM doesn't work. This is a controller
  460. * bug we need to work around.
  461. */
  462. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
  463. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
  464. /* Fix the transmitted FTS count desired to exit from L0s. */
  465. status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
  466. status = (status & ~PCIE_CORE_CTRL_PLC1_FTS_MASK) |
  467. (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
  468. rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
  469. /* Enable Gen1 training */
  470. rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
  471. PCIE_CLIENT_CONFIG);
  472. gpiod_set_value(rockchip->ep_gpio, 1);
  473. /* 500ms timeout value should be enough for Gen1/2 training */
  474. timeout = jiffies + msecs_to_jiffies(500);
  475. for (;;) {
  476. status = rockchip_pcie_read(rockchip,
  477. PCIE_CLIENT_BASIC_STATUS1);
  478. if ((status & PCIE_CLIENT_LINK_STATUS_MASK) ==
  479. PCIE_CLIENT_LINK_STATUS_UP) {
  480. dev_dbg(dev, "PCIe link training gen1 pass!\n");
  481. break;
  482. }
  483. if (time_after(jiffies, timeout)) {
  484. dev_err(dev, "PCIe link training gen1 timeout!\n");
  485. return -ETIMEDOUT;
  486. }
  487. msleep(20);
  488. }
  489. /*
  490. * Enable retrain for gen2. This should be configured only after
  491. * gen1 finished.
  492. */
  493. status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
  494. status |= PCIE_RC_CONFIG_LCS_RETRAIN_LINK;
  495. rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
  496. timeout = jiffies + msecs_to_jiffies(500);
  497. for (;;) {
  498. status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
  499. if ((status & PCIE_CORE_PL_CONF_SPEED_MASK) ==
  500. PCIE_CORE_PL_CONF_SPEED_5G) {
  501. dev_dbg(dev, "PCIe link training gen2 pass!\n");
  502. break;
  503. }
  504. if (time_after(jiffies, timeout)) {
  505. dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
  506. break;
  507. }
  508. msleep(20);
  509. }
  510. /* Check the final link width from negotiated lane counter from MGMT */
  511. status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
  512. status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
  513. PCIE_CORE_PL_CONF_LANE_SHIFT);
  514. dev_dbg(dev, "current link width is x%d\n", status);
  515. rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
  516. PCIE_RC_CONFIG_VENDOR);
  517. rockchip_pcie_write(rockchip,
  518. PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT,
  519. PCIE_RC_CONFIG_RID_CCR);
  520. rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
  521. rockchip_pcie_write(rockchip,
  522. (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
  523. PCIE_CORE_OB_REGION_ADDR0);
  524. rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
  525. PCIE_CORE_OB_REGION_ADDR1);
  526. rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
  527. rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
  528. return 0;
  529. }
  530. static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
  531. {
  532. struct rockchip_pcie *rockchip = arg;
  533. struct device *dev = rockchip->dev;
  534. u32 reg;
  535. u32 sub_reg;
  536. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  537. if (reg & PCIE_CLIENT_INT_LOCAL) {
  538. dev_dbg(dev, "local interrupt received\n");
  539. sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
  540. if (sub_reg & PCIE_CORE_INT_PRFPE)
  541. dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
  542. if (sub_reg & PCIE_CORE_INT_CRFPE)
  543. dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
  544. if (sub_reg & PCIE_CORE_INT_RRPE)
  545. dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
  546. if (sub_reg & PCIE_CORE_INT_PRFO)
  547. dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
  548. if (sub_reg & PCIE_CORE_INT_CRFO)
  549. dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
  550. if (sub_reg & PCIE_CORE_INT_RT)
  551. dev_dbg(dev, "replay timer timed out\n");
  552. if (sub_reg & PCIE_CORE_INT_RTR)
  553. dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
  554. if (sub_reg & PCIE_CORE_INT_PE)
  555. dev_dbg(dev, "phy error detected on receive side\n");
  556. if (sub_reg & PCIE_CORE_INT_MTR)
  557. dev_dbg(dev, "malformed TLP received from the link\n");
  558. if (sub_reg & PCIE_CORE_INT_UCR)
  559. dev_dbg(dev, "malformed TLP received from the link\n");
  560. if (sub_reg & PCIE_CORE_INT_FCE)
  561. dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
  562. if (sub_reg & PCIE_CORE_INT_CT)
  563. dev_dbg(dev, "a request timed out waiting for completion\n");
  564. if (sub_reg & PCIE_CORE_INT_UTC)
  565. dev_dbg(dev, "unmapped TC error\n");
  566. if (sub_reg & PCIE_CORE_INT_MMVC)
  567. dev_dbg(dev, "MSI mask register changes\n");
  568. rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
  569. } else if (reg & PCIE_CLIENT_INT_PHY) {
  570. dev_dbg(dev, "phy link changes\n");
  571. rockchip_pcie_update_txcredit_mui(rockchip);
  572. rockchip_pcie_clr_bw_int(rockchip);
  573. }
  574. rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
  575. PCIE_CLIENT_INT_STATUS);
  576. return IRQ_HANDLED;
  577. }
  578. static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
  579. {
  580. struct rockchip_pcie *rockchip = arg;
  581. struct device *dev = rockchip->dev;
  582. u32 reg;
  583. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  584. if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
  585. dev_dbg(dev, "legacy done interrupt received\n");
  586. if (reg & PCIE_CLIENT_INT_MSG)
  587. dev_dbg(dev, "message done interrupt received\n");
  588. if (reg & PCIE_CLIENT_INT_HOT_RST)
  589. dev_dbg(dev, "hot reset interrupt received\n");
  590. if (reg & PCIE_CLIENT_INT_DPA)
  591. dev_dbg(dev, "dpa interrupt received\n");
  592. if (reg & PCIE_CLIENT_INT_FATAL_ERR)
  593. dev_dbg(dev, "fatal error interrupt received\n");
  594. if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
  595. dev_dbg(dev, "no fatal error interrupt received\n");
  596. if (reg & PCIE_CLIENT_INT_CORR_ERR)
  597. dev_dbg(dev, "correctable error interrupt received\n");
  598. if (reg & PCIE_CLIENT_INT_PHY)
  599. dev_dbg(dev, "phy interrupt received\n");
  600. rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
  601. PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
  602. PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
  603. PCIE_CLIENT_INT_NFATAL_ERR |
  604. PCIE_CLIENT_INT_CORR_ERR |
  605. PCIE_CLIENT_INT_PHY),
  606. PCIE_CLIENT_INT_STATUS);
  607. return IRQ_HANDLED;
  608. }
  609. static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
  610. {
  611. struct irq_chip *chip = irq_desc_get_chip(desc);
  612. struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
  613. struct device *dev = rockchip->dev;
  614. u32 reg;
  615. u32 hwirq;
  616. u32 virq;
  617. chained_irq_enter(chip, desc);
  618. reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
  619. reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
  620. while (reg) {
  621. hwirq = ffs(reg) - 1;
  622. reg &= ~BIT(hwirq);
  623. virq = irq_find_mapping(rockchip->irq_domain, hwirq);
  624. if (virq)
  625. generic_handle_irq(virq);
  626. else
  627. dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
  628. }
  629. chained_irq_exit(chip, desc);
  630. }
  631. /**
  632. * rockchip_pcie_parse_dt - Parse Device Tree
  633. * @rockchip: PCIe port information
  634. *
  635. * Return: '0' on success and error value on failure
  636. */
  637. static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
  638. {
  639. struct device *dev = rockchip->dev;
  640. struct platform_device *pdev = to_platform_device(dev);
  641. struct device_node *node = dev->of_node;
  642. struct resource *regs;
  643. int irq;
  644. int err;
  645. regs = platform_get_resource_byname(pdev,
  646. IORESOURCE_MEM,
  647. "axi-base");
  648. rockchip->reg_base = devm_ioremap_resource(dev, regs);
  649. if (IS_ERR(rockchip->reg_base))
  650. return PTR_ERR(rockchip->reg_base);
  651. regs = platform_get_resource_byname(pdev,
  652. IORESOURCE_MEM,
  653. "apb-base");
  654. rockchip->apb_base = devm_ioremap_resource(dev, regs);
  655. if (IS_ERR(rockchip->apb_base))
  656. return PTR_ERR(rockchip->apb_base);
  657. rockchip->phy = devm_phy_get(dev, "pcie-phy");
  658. if (IS_ERR(rockchip->phy)) {
  659. if (PTR_ERR(rockchip->phy) != -EPROBE_DEFER)
  660. dev_err(dev, "missing phy\n");
  661. return PTR_ERR(rockchip->phy);
  662. }
  663. rockchip->lanes = 1;
  664. err = of_property_read_u32(node, "num-lanes", &rockchip->lanes);
  665. if (!err && (rockchip->lanes == 0 ||
  666. rockchip->lanes == 3 ||
  667. rockchip->lanes > 4)) {
  668. dev_warn(dev, "invalid num-lanes, default to use one lane\n");
  669. rockchip->lanes = 1;
  670. }
  671. rockchip->core_rst = devm_reset_control_get(dev, "core");
  672. if (IS_ERR(rockchip->core_rst)) {
  673. if (PTR_ERR(rockchip->core_rst) != -EPROBE_DEFER)
  674. dev_err(dev, "missing core reset property in node\n");
  675. return PTR_ERR(rockchip->core_rst);
  676. }
  677. rockchip->mgmt_rst = devm_reset_control_get(dev, "mgmt");
  678. if (IS_ERR(rockchip->mgmt_rst)) {
  679. if (PTR_ERR(rockchip->mgmt_rst) != -EPROBE_DEFER)
  680. dev_err(dev, "missing mgmt reset property in node\n");
  681. return PTR_ERR(rockchip->mgmt_rst);
  682. }
  683. rockchip->mgmt_sticky_rst = devm_reset_control_get(dev, "mgmt-sticky");
  684. if (IS_ERR(rockchip->mgmt_sticky_rst)) {
  685. if (PTR_ERR(rockchip->mgmt_sticky_rst) != -EPROBE_DEFER)
  686. dev_err(dev, "missing mgmt-sticky reset property in node\n");
  687. return PTR_ERR(rockchip->mgmt_sticky_rst);
  688. }
  689. rockchip->pipe_rst = devm_reset_control_get(dev, "pipe");
  690. if (IS_ERR(rockchip->pipe_rst)) {
  691. if (PTR_ERR(rockchip->pipe_rst) != -EPROBE_DEFER)
  692. dev_err(dev, "missing pipe reset property in node\n");
  693. return PTR_ERR(rockchip->pipe_rst);
  694. }
  695. rockchip->pm_rst = devm_reset_control_get(dev, "pm");
  696. if (IS_ERR(rockchip->pm_rst)) {
  697. if (PTR_ERR(rockchip->pm_rst) != -EPROBE_DEFER)
  698. dev_err(dev, "missing pm reset property in node\n");
  699. return PTR_ERR(rockchip->pm_rst);
  700. }
  701. rockchip->pclk_rst = devm_reset_control_get(dev, "pclk");
  702. if (IS_ERR(rockchip->pclk_rst)) {
  703. if (PTR_ERR(rockchip->pclk_rst) != -EPROBE_DEFER)
  704. dev_err(dev, "missing pclk reset property in node\n");
  705. return PTR_ERR(rockchip->pclk_rst);
  706. }
  707. rockchip->aclk_rst = devm_reset_control_get(dev, "aclk");
  708. if (IS_ERR(rockchip->aclk_rst)) {
  709. if (PTR_ERR(rockchip->aclk_rst) != -EPROBE_DEFER)
  710. dev_err(dev, "missing aclk reset property in node\n");
  711. return PTR_ERR(rockchip->aclk_rst);
  712. }
  713. rockchip->ep_gpio = devm_gpiod_get(dev, "ep", GPIOD_OUT_HIGH);
  714. if (IS_ERR(rockchip->ep_gpio)) {
  715. dev_err(dev, "missing ep-gpios property in node\n");
  716. return PTR_ERR(rockchip->ep_gpio);
  717. }
  718. rockchip->aclk_pcie = devm_clk_get(dev, "aclk");
  719. if (IS_ERR(rockchip->aclk_pcie)) {
  720. dev_err(dev, "aclk clock not found\n");
  721. return PTR_ERR(rockchip->aclk_pcie);
  722. }
  723. rockchip->aclk_perf_pcie = devm_clk_get(dev, "aclk-perf");
  724. if (IS_ERR(rockchip->aclk_perf_pcie)) {
  725. dev_err(dev, "aclk_perf clock not found\n");
  726. return PTR_ERR(rockchip->aclk_perf_pcie);
  727. }
  728. rockchip->hclk_pcie = devm_clk_get(dev, "hclk");
  729. if (IS_ERR(rockchip->hclk_pcie)) {
  730. dev_err(dev, "hclk clock not found\n");
  731. return PTR_ERR(rockchip->hclk_pcie);
  732. }
  733. rockchip->clk_pcie_pm = devm_clk_get(dev, "pm");
  734. if (IS_ERR(rockchip->clk_pcie_pm)) {
  735. dev_err(dev, "pm clock not found\n");
  736. return PTR_ERR(rockchip->clk_pcie_pm);
  737. }
  738. irq = platform_get_irq_byname(pdev, "sys");
  739. if (irq < 0) {
  740. dev_err(dev, "missing sys IRQ resource\n");
  741. return -EINVAL;
  742. }
  743. err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
  744. IRQF_SHARED, "pcie-sys", rockchip);
  745. if (err) {
  746. dev_err(dev, "failed to request PCIe subsystem IRQ\n");
  747. return err;
  748. }
  749. irq = platform_get_irq_byname(pdev, "legacy");
  750. if (irq < 0) {
  751. dev_err(dev, "missing legacy IRQ resource\n");
  752. return -EINVAL;
  753. }
  754. irq_set_chained_handler_and_data(irq,
  755. rockchip_pcie_legacy_int_handler,
  756. rockchip);
  757. irq = platform_get_irq_byname(pdev, "client");
  758. if (irq < 0) {
  759. dev_err(dev, "missing client IRQ resource\n");
  760. return -EINVAL;
  761. }
  762. err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
  763. IRQF_SHARED, "pcie-client", rockchip);
  764. if (err) {
  765. dev_err(dev, "failed to request PCIe client IRQ\n");
  766. return err;
  767. }
  768. rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
  769. if (IS_ERR(rockchip->vpcie3v3)) {
  770. if (PTR_ERR(rockchip->vpcie3v3) == -EPROBE_DEFER)
  771. return -EPROBE_DEFER;
  772. dev_info(dev, "no vpcie3v3 regulator found\n");
  773. }
  774. rockchip->vpcie1v8 = devm_regulator_get_optional(dev, "vpcie1v8");
  775. if (IS_ERR(rockchip->vpcie1v8)) {
  776. if (PTR_ERR(rockchip->vpcie1v8) == -EPROBE_DEFER)
  777. return -EPROBE_DEFER;
  778. dev_info(dev, "no vpcie1v8 regulator found\n");
  779. }
  780. rockchip->vpcie0v9 = devm_regulator_get_optional(dev, "vpcie0v9");
  781. if (IS_ERR(rockchip->vpcie0v9)) {
  782. if (PTR_ERR(rockchip->vpcie0v9) == -EPROBE_DEFER)
  783. return -EPROBE_DEFER;
  784. dev_info(dev, "no vpcie0v9 regulator found\n");
  785. }
  786. return 0;
  787. }
  788. static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
  789. {
  790. struct device *dev = rockchip->dev;
  791. int err;
  792. if (!IS_ERR(rockchip->vpcie3v3)) {
  793. err = regulator_enable(rockchip->vpcie3v3);
  794. if (err) {
  795. dev_err(dev, "fail to enable vpcie3v3 regulator\n");
  796. goto err_out;
  797. }
  798. }
  799. if (!IS_ERR(rockchip->vpcie1v8)) {
  800. err = regulator_enable(rockchip->vpcie1v8);
  801. if (err) {
  802. dev_err(dev, "fail to enable vpcie1v8 regulator\n");
  803. goto err_disable_3v3;
  804. }
  805. }
  806. if (!IS_ERR(rockchip->vpcie0v9)) {
  807. err = regulator_enable(rockchip->vpcie0v9);
  808. if (err) {
  809. dev_err(dev, "fail to enable vpcie0v9 regulator\n");
  810. goto err_disable_1v8;
  811. }
  812. }
  813. return 0;
  814. err_disable_1v8:
  815. if (!IS_ERR(rockchip->vpcie1v8))
  816. regulator_disable(rockchip->vpcie1v8);
  817. err_disable_3v3:
  818. if (!IS_ERR(rockchip->vpcie3v3))
  819. regulator_disable(rockchip->vpcie3v3);
  820. err_out:
  821. return err;
  822. }
  823. static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
  824. {
  825. rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
  826. (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
  827. rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
  828. PCIE_CORE_INT_MASK);
  829. rockchip_pcie_enable_bw_int(rockchip);
  830. }
  831. static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  832. irq_hw_number_t hwirq)
  833. {
  834. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  835. irq_set_chip_data(irq, domain->host_data);
  836. return 0;
  837. }
  838. static const struct irq_domain_ops intx_domain_ops = {
  839. .map = rockchip_pcie_intx_map,
  840. };
  841. static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
  842. {
  843. struct device *dev = rockchip->dev;
  844. struct device_node *intc = of_get_next_child(dev->of_node, NULL);
  845. if (!intc) {
  846. dev_err(dev, "missing child interrupt-controller node\n");
  847. return -EINVAL;
  848. }
  849. rockchip->irq_domain = irq_domain_add_linear(intc, 4,
  850. &intx_domain_ops, rockchip);
  851. if (!rockchip->irq_domain) {
  852. dev_err(dev, "failed to get a INTx IRQ domain\n");
  853. return -EINVAL;
  854. }
  855. return 0;
  856. }
  857. static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
  858. int region_no, int type, u8 num_pass_bits,
  859. u32 lower_addr, u32 upper_addr)
  860. {
  861. u32 ob_addr_0;
  862. u32 ob_addr_1;
  863. u32 ob_desc_0;
  864. u32 aw_offset;
  865. if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
  866. return -EINVAL;
  867. if (num_pass_bits + 1 < 8)
  868. return -EINVAL;
  869. if (num_pass_bits > 63)
  870. return -EINVAL;
  871. if (region_no == 0) {
  872. if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
  873. return -EINVAL;
  874. }
  875. if (region_no != 0) {
  876. if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
  877. return -EINVAL;
  878. }
  879. aw_offset = (region_no << OB_REG_SIZE_SHIFT);
  880. ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
  881. ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
  882. ob_addr_1 = upper_addr;
  883. ob_desc_0 = (1 << 23 | type);
  884. rockchip_pcie_write(rockchip, ob_addr_0,
  885. PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
  886. rockchip_pcie_write(rockchip, ob_addr_1,
  887. PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
  888. rockchip_pcie_write(rockchip, ob_desc_0,
  889. PCIE_CORE_OB_REGION_DESC0 + aw_offset);
  890. rockchip_pcie_write(rockchip, 0,
  891. PCIE_CORE_OB_REGION_DESC1 + aw_offset);
  892. return 0;
  893. }
  894. static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
  895. int region_no, u8 num_pass_bits,
  896. u32 lower_addr, u32 upper_addr)
  897. {
  898. u32 ib_addr_0;
  899. u32 ib_addr_1;
  900. u32 aw_offset;
  901. if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
  902. return -EINVAL;
  903. if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
  904. return -EINVAL;
  905. if (num_pass_bits > 63)
  906. return -EINVAL;
  907. aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
  908. ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
  909. ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
  910. ib_addr_1 = upper_addr;
  911. rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
  912. rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
  913. return 0;
  914. }
  915. static int rockchip_pcie_probe(struct platform_device *pdev)
  916. {
  917. struct rockchip_pcie *rockchip;
  918. struct device *dev = &pdev->dev;
  919. struct pci_bus *bus, *child;
  920. struct resource_entry *win;
  921. resource_size_t io_base;
  922. struct resource *mem;
  923. struct resource *io;
  924. phys_addr_t io_bus_addr = 0;
  925. u32 io_size;
  926. phys_addr_t mem_bus_addr = 0;
  927. u32 mem_size = 0;
  928. int reg_no;
  929. int err;
  930. int offset;
  931. LIST_HEAD(res);
  932. if (!dev->of_node)
  933. return -ENODEV;
  934. rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
  935. if (!rockchip)
  936. return -ENOMEM;
  937. rockchip->dev = dev;
  938. err = rockchip_pcie_parse_dt(rockchip);
  939. if (err)
  940. return err;
  941. err = clk_prepare_enable(rockchip->aclk_pcie);
  942. if (err) {
  943. dev_err(dev, "unable to enable aclk_pcie clock\n");
  944. goto err_aclk_pcie;
  945. }
  946. err = clk_prepare_enable(rockchip->aclk_perf_pcie);
  947. if (err) {
  948. dev_err(dev, "unable to enable aclk_perf_pcie clock\n");
  949. goto err_aclk_perf_pcie;
  950. }
  951. err = clk_prepare_enable(rockchip->hclk_pcie);
  952. if (err) {
  953. dev_err(dev, "unable to enable hclk_pcie clock\n");
  954. goto err_hclk_pcie;
  955. }
  956. err = clk_prepare_enable(rockchip->clk_pcie_pm);
  957. if (err) {
  958. dev_err(dev, "unable to enable hclk_pcie clock\n");
  959. goto err_pcie_pm;
  960. }
  961. err = rockchip_pcie_set_vpcie(rockchip);
  962. if (err) {
  963. dev_err(dev, "failed to set vpcie regulator\n");
  964. goto err_set_vpcie;
  965. }
  966. err = rockchip_pcie_init_port(rockchip);
  967. if (err)
  968. goto err_vpcie;
  969. rockchip_pcie_enable_interrupts(rockchip);
  970. err = rockchip_pcie_init_irq_domain(rockchip);
  971. if (err < 0)
  972. goto err_vpcie;
  973. err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
  974. &res, &io_base);
  975. if (err)
  976. goto err_vpcie;
  977. err = devm_request_pci_bus_resources(dev, &res);
  978. if (err)
  979. goto err_vpcie;
  980. /* Get the I/O and memory ranges from DT */
  981. io_size = 0;
  982. resource_list_for_each_entry(win, &res) {
  983. switch (resource_type(win->res)) {
  984. case IORESOURCE_IO:
  985. io = win->res;
  986. io->name = "I/O";
  987. io_size = resource_size(io);
  988. io_bus_addr = io->start - win->offset;
  989. err = pci_remap_iospace(io, io_base);
  990. if (err) {
  991. dev_warn(dev, "error %d: failed to map resource %pR\n",
  992. err, io);
  993. continue;
  994. }
  995. break;
  996. case IORESOURCE_MEM:
  997. mem = win->res;
  998. mem->name = "MEM";
  999. mem_size = resource_size(mem);
  1000. mem_bus_addr = mem->start - win->offset;
  1001. break;
  1002. case IORESOURCE_BUS:
  1003. rockchip->root_bus_nr = win->res->start;
  1004. break;
  1005. default:
  1006. continue;
  1007. }
  1008. }
  1009. if (mem_size) {
  1010. for (reg_no = 0; reg_no < (mem_size >> 20); reg_no++) {
  1011. err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
  1012. AXI_WRAPPER_MEM_WRITE,
  1013. 20 - 1,
  1014. mem_bus_addr +
  1015. (reg_no << 20),
  1016. 0);
  1017. if (err) {
  1018. dev_err(dev, "program RC mem outbound ATU failed\n");
  1019. goto err_vpcie;
  1020. }
  1021. }
  1022. }
  1023. err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
  1024. if (err) {
  1025. dev_err(dev, "program RC mem inbound ATU failed\n");
  1026. goto err_vpcie;
  1027. }
  1028. offset = mem_size >> 20;
  1029. if (io_size) {
  1030. for (reg_no = 0; reg_no < (io_size >> 20); reg_no++) {
  1031. err = rockchip_pcie_prog_ob_atu(rockchip,
  1032. reg_no + 1 + offset,
  1033. AXI_WRAPPER_IO_WRITE,
  1034. 20 - 1,
  1035. io_bus_addr +
  1036. (reg_no << 20),
  1037. 0);
  1038. if (err) {
  1039. dev_err(dev, "program RC io outbound ATU failed\n");
  1040. goto err_vpcie;
  1041. }
  1042. }
  1043. }
  1044. bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
  1045. if (!bus) {
  1046. err = -ENOMEM;
  1047. goto err_vpcie;
  1048. }
  1049. pci_bus_size_bridges(bus);
  1050. pci_bus_assign_resources(bus);
  1051. list_for_each_entry(child, &bus->children, node)
  1052. pcie_bus_configure_settings(child);
  1053. pci_bus_add_devices(bus);
  1054. dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
  1055. return err;
  1056. err_vpcie:
  1057. if (!IS_ERR(rockchip->vpcie3v3))
  1058. regulator_disable(rockchip->vpcie3v3);
  1059. if (!IS_ERR(rockchip->vpcie1v8))
  1060. regulator_disable(rockchip->vpcie1v8);
  1061. if (!IS_ERR(rockchip->vpcie0v9))
  1062. regulator_disable(rockchip->vpcie0v9);
  1063. err_set_vpcie:
  1064. clk_disable_unprepare(rockchip->clk_pcie_pm);
  1065. err_pcie_pm:
  1066. clk_disable_unprepare(rockchip->hclk_pcie);
  1067. err_hclk_pcie:
  1068. clk_disable_unprepare(rockchip->aclk_perf_pcie);
  1069. err_aclk_perf_pcie:
  1070. clk_disable_unprepare(rockchip->aclk_pcie);
  1071. err_aclk_pcie:
  1072. return err;
  1073. }
  1074. static const struct of_device_id rockchip_pcie_of_match[] = {
  1075. { .compatible = "rockchip,rk3399-pcie", },
  1076. {}
  1077. };
  1078. static struct platform_driver rockchip_pcie_driver = {
  1079. .driver = {
  1080. .name = "rockchip-pcie",
  1081. .of_match_table = rockchip_pcie_of_match,
  1082. },
  1083. .probe = rockchip_pcie_probe,
  1084. };
  1085. builtin_platform_driver(rockchip_pcie_driver);