pcie-rcar-host.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe driver for Renesas R-Car SoCs
  4. * Copyright (C) 2014-2020 Renesas Electronics Europe Ltd
  5. *
  6. * Based on:
  7. * arch/sh/drivers/pci/pcie-sh7786.c
  8. * arch/sh/drivers/pci/ops-sh7786.c
  9. * Copyright (C) 2009 - 2011 Paul Mundt
  10. *
  11. * Author: Phil Edworthy <phil.edworthy@renesas.com>
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/clk.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/msi.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/pci.h>
  28. #include <linux/phy/phy.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/regulator/consumer.h>
  32. #include "pcie-rcar.h"
  33. struct rcar_msi {
  34. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  35. struct irq_domain *domain;
  36. struct mutex map_lock;
  37. spinlock_t mask_lock;
  38. int irq1;
  39. int irq2;
  40. };
  41. /* Structure representing the PCIe interface */
  42. struct rcar_pcie_host {
  43. struct rcar_pcie pcie;
  44. struct phy *phy;
  45. struct clk *bus_clk;
  46. struct rcar_msi msi;
  47. int (*phy_init_fn)(struct rcar_pcie_host *host);
  48. };
  49. static DEFINE_SPINLOCK(pmsr_lock);
  50. static int rcar_pcie_wakeup(struct device *pcie_dev, void __iomem *pcie_base)
  51. {
  52. unsigned long flags;
  53. u32 pmsr, val;
  54. int ret = 0;
  55. spin_lock_irqsave(&pmsr_lock, flags);
  56. if (!pcie_base || pm_runtime_suspended(pcie_dev)) {
  57. ret = -EINVAL;
  58. goto unlock_exit;
  59. }
  60. pmsr = readl(pcie_base + PMSR);
  61. /*
  62. * Test if the PCIe controller received PM_ENTER_L1 DLLP and
  63. * the PCIe controller is not in L1 link state. If true, apply
  64. * fix, which will put the controller into L1 link state, from
  65. * which it can return to L0s/L0 on its own.
  66. */
  67. if ((pmsr & PMEL1RX) && ((pmsr & PMSTATE) != PMSTATE_L1)) {
  68. writel(L1IATN, pcie_base + PMCTLR);
  69. ret = readl_poll_timeout_atomic(pcie_base + PMSR, val,
  70. val & L1FAEG, 10, 1000);
  71. if (ret) {
  72. dev_warn_ratelimited(pcie_dev,
  73. "Timeout waiting for L1 link state, ret=%d\n",
  74. ret);
  75. }
  76. writel(L1FAEG | PMEL1RX, pcie_base + PMSR);
  77. }
  78. unlock_exit:
  79. spin_unlock_irqrestore(&pmsr_lock, flags);
  80. return ret;
  81. }
  82. static struct rcar_pcie_host *msi_to_host(struct rcar_msi *msi)
  83. {
  84. return container_of(msi, struct rcar_pcie_host, msi);
  85. }
  86. static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
  87. {
  88. unsigned int shift = BITS_PER_BYTE * (where & 3);
  89. u32 val = rcar_pci_read_reg(pcie, where & ~3);
  90. return val >> shift;
  91. }
  92. #ifdef CONFIG_ARM
  93. #define __rcar_pci_rw_reg_workaround(instr) \
  94. " .arch armv7-a\n" \
  95. "1: " instr " %1, [%2]\n" \
  96. "2: isb\n" \
  97. "3: .pushsection .text.fixup,\"ax\"\n" \
  98. " .align 2\n" \
  99. "4: mov %0, #" __stringify(PCIBIOS_SET_FAILED) "\n" \
  100. " b 3b\n" \
  101. " .popsection\n" \
  102. " .pushsection __ex_table,\"a\"\n" \
  103. " .align 3\n" \
  104. " .long 1b, 4b\n" \
  105. " .long 2b, 4b\n" \
  106. " .popsection\n"
  107. #endif
  108. static int rcar_pci_write_reg_workaround(struct rcar_pcie *pcie, u32 val,
  109. unsigned int reg)
  110. {
  111. int error = PCIBIOS_SUCCESSFUL;
  112. #ifdef CONFIG_ARM
  113. asm volatile(
  114. __rcar_pci_rw_reg_workaround("str")
  115. : "+r"(error):"r"(val), "r"(pcie->base + reg) : "memory");
  116. #else
  117. rcar_pci_write_reg(pcie, val, reg);
  118. #endif
  119. return error;
  120. }
  121. static int rcar_pci_read_reg_workaround(struct rcar_pcie *pcie, u32 *val,
  122. unsigned int reg)
  123. {
  124. int error = PCIBIOS_SUCCESSFUL;
  125. #ifdef CONFIG_ARM
  126. asm volatile(
  127. __rcar_pci_rw_reg_workaround("ldr")
  128. : "+r"(error), "=r"(*val) : "r"(pcie->base + reg) : "memory");
  129. if (error != PCIBIOS_SUCCESSFUL)
  130. PCI_SET_ERROR_RESPONSE(val);
  131. #else
  132. *val = rcar_pci_read_reg(pcie, reg);
  133. #endif
  134. return error;
  135. }
  136. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  137. static int rcar_pcie_config_access(struct rcar_pcie_host *host,
  138. unsigned char access_type, struct pci_bus *bus,
  139. unsigned int devfn, int where, u32 *data)
  140. {
  141. struct rcar_pcie *pcie = &host->pcie;
  142. unsigned int dev, func, reg, index;
  143. int ret;
  144. /* Wake the bus up in case it is in L1 state. */
  145. ret = rcar_pcie_wakeup(pcie->dev, pcie->base);
  146. if (ret) {
  147. PCI_SET_ERROR_RESPONSE(data);
  148. return PCIBIOS_SET_FAILED;
  149. }
  150. dev = PCI_SLOT(devfn);
  151. func = PCI_FUNC(devfn);
  152. reg = where & ~3;
  153. index = reg / 4;
  154. /*
  155. * While each channel has its own memory-mapped extended config
  156. * space, it's generally only accessible when in endpoint mode.
  157. * When in root complex mode, the controller is unable to target
  158. * itself with either type 0 or type 1 accesses, and indeed, any
  159. * controller initiated target transfer to its own config space
  160. * result in a completer abort.
  161. *
  162. * Each channel effectively only supports a single device, but as
  163. * the same channel <-> device access works for any PCI_SLOT()
  164. * value, we cheat a bit here and bind the controller's config
  165. * space to devfn 0 in order to enable self-enumeration. In this
  166. * case the regular ECAR/ECDR path is sidelined and the mangled
  167. * config access itself is initiated as an internal bus transaction.
  168. */
  169. if (pci_is_root_bus(bus)) {
  170. if (dev != 0)
  171. return PCIBIOS_DEVICE_NOT_FOUND;
  172. if (access_type == RCAR_PCI_ACCESS_READ)
  173. *data = rcar_pci_read_reg(pcie, PCICONF(index));
  174. else
  175. rcar_pci_write_reg(pcie, *data, PCICONF(index));
  176. return PCIBIOS_SUCCESSFUL;
  177. }
  178. /* Clear errors */
  179. rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
  180. /* Set the PIO address */
  181. rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
  182. PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
  183. /* Enable the configuration access */
  184. if (pci_is_root_bus(bus->parent))
  185. rcar_pci_write_reg(pcie, PCIECCTLR_CCIE | TYPE0, PCIECCTLR);
  186. else
  187. rcar_pci_write_reg(pcie, PCIECCTLR_CCIE | TYPE1, PCIECCTLR);
  188. /* Check for errors */
  189. if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
  190. return PCIBIOS_DEVICE_NOT_FOUND;
  191. /* Check for master and target aborts */
  192. if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
  193. (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
  194. return PCIBIOS_DEVICE_NOT_FOUND;
  195. if (access_type == RCAR_PCI_ACCESS_READ)
  196. ret = rcar_pci_read_reg_workaround(pcie, data, PCIECDR);
  197. else
  198. ret = rcar_pci_write_reg_workaround(pcie, *data, PCIECDR);
  199. /* Disable the configuration access */
  200. rcar_pci_write_reg(pcie, 0, PCIECCTLR);
  201. return ret;
  202. }
  203. static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  204. int where, int size, u32 *val)
  205. {
  206. struct rcar_pcie_host *host = bus->sysdata;
  207. int ret;
  208. ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
  209. bus, devfn, where, val);
  210. if (ret != PCIBIOS_SUCCESSFUL)
  211. return ret;
  212. if (size == 1)
  213. *val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
  214. else if (size == 2)
  215. *val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
  216. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
  217. bus->number, devfn, where, size, *val);
  218. return ret;
  219. }
  220. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  221. static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  222. int where, int size, u32 val)
  223. {
  224. struct rcar_pcie_host *host = bus->sysdata;
  225. unsigned int shift;
  226. u32 data;
  227. int ret;
  228. ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
  229. bus, devfn, where, &data);
  230. if (ret != PCIBIOS_SUCCESSFUL)
  231. return ret;
  232. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
  233. bus->number, devfn, where, size, val);
  234. if (size == 1) {
  235. shift = BITS_PER_BYTE * (where & 3);
  236. data &= ~(0xff << shift);
  237. data |= ((val & 0xff) << shift);
  238. } else if (size == 2) {
  239. shift = BITS_PER_BYTE * (where & 2);
  240. data &= ~(0xffff << shift);
  241. data |= ((val & 0xffff) << shift);
  242. } else
  243. data = val;
  244. ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
  245. bus, devfn, where, &data);
  246. return ret;
  247. }
  248. static struct pci_ops rcar_pcie_ops = {
  249. .read = rcar_pcie_read_conf,
  250. .write = rcar_pcie_write_conf,
  251. };
  252. static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
  253. {
  254. struct device *dev = pcie->dev;
  255. unsigned int timeout = 1000;
  256. u32 macsr;
  257. if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
  258. return;
  259. if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
  260. dev_err(dev, "Speed change already in progress\n");
  261. return;
  262. }
  263. macsr = rcar_pci_read_reg(pcie, MACSR);
  264. if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
  265. goto done;
  266. /* Set target link speed to 5.0 GT/s */
  267. rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
  268. PCI_EXP_LNKSTA_CLS_5_0GB);
  269. /* Set speed change reason as intentional factor */
  270. rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);
  271. /* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
  272. if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
  273. rcar_pci_write_reg(pcie, macsr, MACSR);
  274. /* Start link speed change */
  275. rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);
  276. while (timeout--) {
  277. macsr = rcar_pci_read_reg(pcie, MACSR);
  278. if (macsr & SPCHGFIN) {
  279. /* Clear the interrupt bits */
  280. rcar_pci_write_reg(pcie, macsr, MACSR);
  281. if (macsr & SPCHGFAIL)
  282. dev_err(dev, "Speed change failed\n");
  283. goto done;
  284. }
  285. msleep(1);
  286. }
  287. dev_err(dev, "Speed change timed out\n");
  288. done:
  289. dev_info(dev, "Current link speed is %s GT/s\n",
  290. (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
  291. }
  292. static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
  293. {
  294. struct rcar_pcie *pcie = &host->pcie;
  295. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
  296. struct resource_entry *win;
  297. LIST_HEAD(res);
  298. int i = 0;
  299. /* Try setting 5 GT/s link speed */
  300. rcar_pcie_force_speedup(pcie);
  301. /* Setup PCI resources */
  302. resource_list_for_each_entry(win, &bridge->windows) {
  303. struct resource *res = win->res;
  304. if (!res->flags)
  305. continue;
  306. switch (resource_type(res)) {
  307. case IORESOURCE_IO:
  308. case IORESOURCE_MEM:
  309. rcar_pcie_set_outbound(pcie, i, win);
  310. i++;
  311. break;
  312. }
  313. }
  314. }
  315. static int rcar_pcie_enable(struct rcar_pcie_host *host)
  316. {
  317. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
  318. rcar_pcie_hw_enable(host);
  319. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  320. bridge->sysdata = host;
  321. bridge->ops = &rcar_pcie_ops;
  322. return pci_host_probe(bridge);
  323. }
  324. static int phy_wait_for_ack(struct rcar_pcie *pcie)
  325. {
  326. struct device *dev = pcie->dev;
  327. unsigned int timeout = 100;
  328. while (timeout--) {
  329. if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
  330. return 0;
  331. udelay(100);
  332. }
  333. dev_err(dev, "Access to PCIe phy timed out\n");
  334. return -ETIMEDOUT;
  335. }
  336. static void phy_write_reg(struct rcar_pcie *pcie,
  337. unsigned int rate, u32 addr,
  338. unsigned int lane, u32 data)
  339. {
  340. u32 phyaddr;
  341. phyaddr = WRITE_CMD |
  342. ((rate & 1) << RATE_POS) |
  343. ((lane & 0xf) << LANE_POS) |
  344. ((addr & 0xff) << ADR_POS);
  345. /* Set write data */
  346. rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
  347. rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
  348. /* Ignore errors as they will be dealt with if the data link is down */
  349. phy_wait_for_ack(pcie);
  350. /* Clear command */
  351. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
  352. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
  353. /* Ignore errors as they will be dealt with if the data link is down */
  354. phy_wait_for_ack(pcie);
  355. }
  356. static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
  357. {
  358. int err;
  359. /* Begin initialization */
  360. rcar_pci_write_reg(pcie, 0, PCIETCTLR);
  361. /* Set mode */
  362. rcar_pci_write_reg(pcie, 1, PCIEMSR);
  363. err = rcar_pcie_wait_for_phyrdy(pcie);
  364. if (err)
  365. return err;
  366. /*
  367. * Initial header for port config space is type 1, set the device
  368. * class to match. Hardware takes care of propagating the IDSETR
  369. * settings, so there is no need to bother with a quirk.
  370. */
  371. rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI_NORMAL << 8, IDSETR1);
  372. /*
  373. * Setup Secondary Bus Number & Subordinate Bus Number, even though
  374. * they aren't used, to avoid bridge being detected as broken.
  375. */
  376. rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
  377. rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
  378. /* Initialize default capabilities. */
  379. rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
  380. rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
  381. PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
  382. rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), PCI_HEADER_TYPE_MASK,
  383. PCI_HEADER_TYPE_BRIDGE);
  384. /* Enable data link layer active state reporting */
  385. rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
  386. PCI_EXP_LNKCAP_DLLLARC);
  387. /* Write out the physical slot number = 0 */
  388. rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
  389. /* Set the completion timer timeout to the maximum 50ms. */
  390. rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
  391. /* Terminate list of capabilities (Next Capability Offset=0) */
  392. rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
  393. /* Enable MSI */
  394. if (IS_ENABLED(CONFIG_PCI_MSI))
  395. rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
  396. rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
  397. /* Finish initialization - establish a PCI Express link */
  398. rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
  399. /* This will timeout if we don't have a link. */
  400. err = rcar_pcie_wait_for_dl(pcie);
  401. if (err)
  402. return err;
  403. /* Enable INTx interrupts */
  404. rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
  405. wmb();
  406. return 0;
  407. }
  408. static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
  409. {
  410. struct rcar_pcie *pcie = &host->pcie;
  411. /* Initialize the phy */
  412. phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
  413. phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
  414. phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
  415. phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
  416. phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
  417. phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
  418. phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
  419. phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
  420. phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
  421. phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
  422. phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
  423. phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
  424. phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
  425. phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
  426. phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
  427. return 0;
  428. }
  429. static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
  430. {
  431. struct rcar_pcie *pcie = &host->pcie;
  432. /*
  433. * These settings come from the R-Car Series, 2nd Generation User's
  434. * Manual, section 50.3.1 (2) Initialization of the physical layer.
  435. */
  436. rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
  437. rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
  438. rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
  439. rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
  440. rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
  441. /* The following value is for DC connection, no termination resistor */
  442. rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
  443. rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
  444. rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);
  445. return 0;
  446. }
  447. static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
  448. {
  449. int err;
  450. err = phy_init(host->phy);
  451. if (err)
  452. return err;
  453. err = phy_power_on(host->phy);
  454. if (err)
  455. phy_exit(host->phy);
  456. return err;
  457. }
  458. static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
  459. {
  460. struct rcar_pcie_host *host = data;
  461. struct rcar_pcie *pcie = &host->pcie;
  462. struct rcar_msi *msi = &host->msi;
  463. struct device *dev = pcie->dev;
  464. unsigned long reg;
  465. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  466. /* MSI & INTx share an interrupt - we only handle MSI here */
  467. if (!reg)
  468. return IRQ_NONE;
  469. while (reg) {
  470. unsigned int index = find_first_bit(&reg, 32);
  471. int ret;
  472. ret = generic_handle_domain_irq(msi->domain->parent, index);
  473. if (ret) {
  474. /* Unknown MSI, just clear it */
  475. dev_dbg(dev, "unexpected MSI\n");
  476. rcar_pci_write_reg(pcie, BIT(index), PCIEMSIFR);
  477. }
  478. /* see if there's any more pending in this vector */
  479. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  480. }
  481. return IRQ_HANDLED;
  482. }
  483. static void rcar_msi_top_irq_ack(struct irq_data *d)
  484. {
  485. irq_chip_ack_parent(d);
  486. }
  487. static void rcar_msi_top_irq_mask(struct irq_data *d)
  488. {
  489. pci_msi_mask_irq(d);
  490. irq_chip_mask_parent(d);
  491. }
  492. static void rcar_msi_top_irq_unmask(struct irq_data *d)
  493. {
  494. pci_msi_unmask_irq(d);
  495. irq_chip_unmask_parent(d);
  496. }
  497. static struct irq_chip rcar_msi_top_chip = {
  498. .name = "PCIe MSI",
  499. .irq_ack = rcar_msi_top_irq_ack,
  500. .irq_mask = rcar_msi_top_irq_mask,
  501. .irq_unmask = rcar_msi_top_irq_unmask,
  502. };
  503. static void rcar_msi_irq_ack(struct irq_data *d)
  504. {
  505. struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
  506. struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
  507. /* clear the interrupt */
  508. rcar_pci_write_reg(pcie, BIT(d->hwirq), PCIEMSIFR);
  509. }
  510. static void rcar_msi_irq_mask(struct irq_data *d)
  511. {
  512. struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
  513. struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
  514. unsigned long flags;
  515. u32 value;
  516. spin_lock_irqsave(&msi->mask_lock, flags);
  517. value = rcar_pci_read_reg(pcie, PCIEMSIIER);
  518. value &= ~BIT(d->hwirq);
  519. rcar_pci_write_reg(pcie, value, PCIEMSIIER);
  520. spin_unlock_irqrestore(&msi->mask_lock, flags);
  521. }
  522. static void rcar_msi_irq_unmask(struct irq_data *d)
  523. {
  524. struct rcar_msi *msi = irq_data_get_irq_chip_data(d);
  525. struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
  526. unsigned long flags;
  527. u32 value;
  528. spin_lock_irqsave(&msi->mask_lock, flags);
  529. value = rcar_pci_read_reg(pcie, PCIEMSIIER);
  530. value |= BIT(d->hwirq);
  531. rcar_pci_write_reg(pcie, value, PCIEMSIIER);
  532. spin_unlock_irqrestore(&msi->mask_lock, flags);
  533. }
  534. static void rcar_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  535. {
  536. struct rcar_msi *msi = irq_data_get_irq_chip_data(data);
  537. struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
  538. msg->address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
  539. msg->address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
  540. msg->data = data->hwirq;
  541. }
  542. static struct irq_chip rcar_msi_bottom_chip = {
  543. .name = "R-Car MSI",
  544. .irq_ack = rcar_msi_irq_ack,
  545. .irq_mask = rcar_msi_irq_mask,
  546. .irq_unmask = rcar_msi_irq_unmask,
  547. .irq_compose_msi_msg = rcar_compose_msi_msg,
  548. };
  549. static int rcar_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  550. unsigned int nr_irqs, void *args)
  551. {
  552. struct rcar_msi *msi = domain->host_data;
  553. unsigned int i;
  554. int hwirq;
  555. mutex_lock(&msi->map_lock);
  556. hwirq = bitmap_find_free_region(msi->used, INT_PCI_MSI_NR, order_base_2(nr_irqs));
  557. mutex_unlock(&msi->map_lock);
  558. if (hwirq < 0)
  559. return -ENOSPC;
  560. for (i = 0; i < nr_irqs; i++)
  561. irq_domain_set_info(domain, virq + i, hwirq + i,
  562. &rcar_msi_bottom_chip, domain->host_data,
  563. handle_edge_irq, NULL, NULL);
  564. return 0;
  565. }
  566. static void rcar_msi_domain_free(struct irq_domain *domain, unsigned int virq,
  567. unsigned int nr_irqs)
  568. {
  569. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  570. struct rcar_msi *msi = domain->host_data;
  571. mutex_lock(&msi->map_lock);
  572. bitmap_release_region(msi->used, d->hwirq, order_base_2(nr_irqs));
  573. mutex_unlock(&msi->map_lock);
  574. }
  575. static const struct irq_domain_ops rcar_msi_domain_ops = {
  576. .alloc = rcar_msi_domain_alloc,
  577. .free = rcar_msi_domain_free,
  578. };
  579. static struct msi_domain_info rcar_msi_info = {
  580. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  581. MSI_FLAG_NO_AFFINITY | MSI_FLAG_MULTI_PCI_MSI,
  582. .chip = &rcar_msi_top_chip,
  583. };
  584. static int rcar_allocate_domains(struct rcar_msi *msi)
  585. {
  586. struct rcar_pcie *pcie = &msi_to_host(msi)->pcie;
  587. struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
  588. struct irq_domain *parent;
  589. parent = irq_domain_create_linear(fwnode, INT_PCI_MSI_NR,
  590. &rcar_msi_domain_ops, msi);
  591. if (!parent) {
  592. dev_err(pcie->dev, "failed to create IRQ domain\n");
  593. return -ENOMEM;
  594. }
  595. irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
  596. msi->domain = pci_msi_create_irq_domain(fwnode, &rcar_msi_info, parent);
  597. if (!msi->domain) {
  598. dev_err(pcie->dev, "failed to create MSI domain\n");
  599. irq_domain_remove(parent);
  600. return -ENOMEM;
  601. }
  602. return 0;
  603. }
  604. static void rcar_free_domains(struct rcar_msi *msi)
  605. {
  606. struct irq_domain *parent = msi->domain->parent;
  607. irq_domain_remove(msi->domain);
  608. irq_domain_remove(parent);
  609. }
  610. static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
  611. {
  612. struct rcar_pcie *pcie = &host->pcie;
  613. struct device *dev = pcie->dev;
  614. struct rcar_msi *msi = &host->msi;
  615. struct resource res;
  616. int err;
  617. mutex_init(&msi->map_lock);
  618. spin_lock_init(&msi->mask_lock);
  619. err = of_address_to_resource(dev->of_node, 0, &res);
  620. if (err)
  621. return err;
  622. err = rcar_allocate_domains(msi);
  623. if (err)
  624. return err;
  625. /* Two irqs are for MSI, but they are also used for non-MSI irqs */
  626. err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
  627. IRQF_SHARED | IRQF_NO_THREAD,
  628. rcar_msi_bottom_chip.name, host);
  629. if (err < 0) {
  630. dev_err(dev, "failed to request IRQ: %d\n", err);
  631. goto err;
  632. }
  633. err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
  634. IRQF_SHARED | IRQF_NO_THREAD,
  635. rcar_msi_bottom_chip.name, host);
  636. if (err < 0) {
  637. dev_err(dev, "failed to request IRQ: %d\n", err);
  638. goto err;
  639. }
  640. /* disable all MSIs */
  641. rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
  642. /*
  643. * Setup MSI data target using RC base address address, which
  644. * is guaranteed to be in the low 32bit range on any R-Car HW.
  645. */
  646. rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
  647. rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
  648. return 0;
  649. err:
  650. rcar_free_domains(msi);
  651. return err;
  652. }
  653. static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
  654. {
  655. struct rcar_pcie *pcie = &host->pcie;
  656. /* Disable all MSI interrupts */
  657. rcar_pci_write_reg(pcie, 0, PCIEMSIIER);
  658. /* Disable address decoding of the MSI interrupt, MSIFE */
  659. rcar_pci_write_reg(pcie, 0, PCIEMSIALR);
  660. rcar_free_domains(&host->msi);
  661. }
  662. static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
  663. {
  664. struct rcar_pcie *pcie = &host->pcie;
  665. struct device *dev = pcie->dev;
  666. struct resource res;
  667. int err, i;
  668. host->phy = devm_phy_optional_get(dev, "pcie");
  669. if (IS_ERR(host->phy))
  670. return PTR_ERR(host->phy);
  671. err = of_address_to_resource(dev->of_node, 0, &res);
  672. if (err)
  673. return err;
  674. pcie->base = devm_ioremap_resource(dev, &res);
  675. if (IS_ERR(pcie->base))
  676. return PTR_ERR(pcie->base);
  677. host->bus_clk = devm_clk_get(dev, "pcie_bus");
  678. if (IS_ERR(host->bus_clk)) {
  679. dev_err(dev, "cannot get pcie bus clock\n");
  680. return PTR_ERR(host->bus_clk);
  681. }
  682. i = irq_of_parse_and_map(dev->of_node, 0);
  683. if (!i) {
  684. dev_err(dev, "cannot get platform resources for msi interrupt\n");
  685. err = -ENOENT;
  686. goto err_irq1;
  687. }
  688. host->msi.irq1 = i;
  689. i = irq_of_parse_and_map(dev->of_node, 1);
  690. if (!i) {
  691. dev_err(dev, "cannot get platform resources for msi interrupt\n");
  692. err = -ENOENT;
  693. goto err_irq2;
  694. }
  695. host->msi.irq2 = i;
  696. return 0;
  697. err_irq2:
  698. irq_dispose_mapping(host->msi.irq1);
  699. err_irq1:
  700. return err;
  701. }
  702. static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
  703. struct resource_entry *entry,
  704. int *index)
  705. {
  706. u64 restype = entry->res->flags;
  707. u64 cpu_addr = entry->res->start;
  708. u64 cpu_end = entry->res->end;
  709. u64 pci_addr = entry->res->start - entry->offset;
  710. u32 flags = LAM_64BIT | LAR_ENABLE;
  711. u64 mask;
  712. u64 size = resource_size(entry->res);
  713. int idx = *index;
  714. if (restype & IORESOURCE_PREFETCH)
  715. flags |= LAM_PREFETCH;
  716. while (cpu_addr < cpu_end) {
  717. if (idx >= MAX_NR_INBOUND_MAPS - 1) {
  718. dev_err(pcie->dev, "Failed to map inbound regions!\n");
  719. return -EINVAL;
  720. }
  721. /*
  722. * If the size of the range is larger than the alignment of
  723. * the start address, we have to use multiple entries to
  724. * perform the mapping.
  725. */
  726. if (cpu_addr > 0) {
  727. unsigned long nr_zeros = __ffs64(cpu_addr);
  728. u64 alignment = 1ULL << nr_zeros;
  729. size = min(size, alignment);
  730. }
  731. /* Hardware supports max 4GiB inbound region */
  732. size = min(size, 1ULL << 32);
  733. mask = roundup_pow_of_two(size) - 1;
  734. mask &= ~0xf;
  735. rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
  736. lower_32_bits(mask) | flags, idx, true);
  737. pci_addr += size;
  738. cpu_addr += size;
  739. idx += 2;
  740. }
  741. *index = idx;
  742. return 0;
  743. }
  744. static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
  745. {
  746. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
  747. struct resource_entry *entry;
  748. int index = 0, err = 0;
  749. resource_list_for_each_entry(entry, &bridge->dma_ranges) {
  750. err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
  751. if (err)
  752. break;
  753. }
  754. return err;
  755. }
  756. static const struct of_device_id rcar_pcie_of_match[] = {
  757. { .compatible = "renesas,pcie-r8a7779",
  758. .data = rcar_pcie_phy_init_h1 },
  759. { .compatible = "renesas,pcie-r8a7790",
  760. .data = rcar_pcie_phy_init_gen2 },
  761. { .compatible = "renesas,pcie-r8a7791",
  762. .data = rcar_pcie_phy_init_gen2 },
  763. { .compatible = "renesas,pcie-rcar-gen2",
  764. .data = rcar_pcie_phy_init_gen2 },
  765. { .compatible = "renesas,pcie-r8a7795",
  766. .data = rcar_pcie_phy_init_gen3 },
  767. { .compatible = "renesas,pcie-rcar-gen3",
  768. .data = rcar_pcie_phy_init_gen3 },
  769. {},
  770. };
  771. /* Design note 346 from Linear Technology says order is not important. */
  772. static const char * const rcar_pcie_supplies[] = {
  773. "vpcie1v5",
  774. "vpcie3v3",
  775. "vpcie12v",
  776. };
  777. static int rcar_pcie_probe(struct platform_device *pdev)
  778. {
  779. struct device *dev = &pdev->dev;
  780. struct pci_host_bridge *bridge;
  781. struct rcar_pcie_host *host;
  782. struct rcar_pcie *pcie;
  783. unsigned int i;
  784. u32 data;
  785. int err;
  786. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
  787. if (!bridge)
  788. return -ENOMEM;
  789. host = pci_host_bridge_priv(bridge);
  790. pcie = &host->pcie;
  791. pcie->dev = dev;
  792. platform_set_drvdata(pdev, host);
  793. for (i = 0; i < ARRAY_SIZE(rcar_pcie_supplies); i++) {
  794. err = devm_regulator_get_enable_optional(dev, rcar_pcie_supplies[i]);
  795. if (err < 0 && err != -ENODEV)
  796. return dev_err_probe(dev, err, "failed to enable regulator: %s\n",
  797. rcar_pcie_supplies[i]);
  798. }
  799. pm_runtime_enable(pcie->dev);
  800. err = pm_runtime_get_sync(pcie->dev);
  801. if (err < 0) {
  802. dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
  803. goto err_pm_put;
  804. }
  805. err = rcar_pcie_get_resources(host);
  806. if (err < 0) {
  807. dev_err(dev, "failed to request resources: %d\n", err);
  808. goto err_pm_put;
  809. }
  810. err = clk_prepare_enable(host->bus_clk);
  811. if (err) {
  812. dev_err(dev, "failed to enable bus clock: %d\n", err);
  813. goto err_unmap_msi_irqs;
  814. }
  815. err = rcar_pcie_parse_map_dma_ranges(host);
  816. if (err)
  817. goto err_clk_disable;
  818. host->phy_init_fn = of_device_get_match_data(dev);
  819. err = host->phy_init_fn(host);
  820. if (err) {
  821. dev_err(dev, "failed to init PCIe PHY\n");
  822. goto err_clk_disable;
  823. }
  824. /* Failure to get a link might just be that no cards are inserted */
  825. if (rcar_pcie_hw_init(pcie)) {
  826. dev_info(dev, "PCIe link down\n");
  827. err = -ENODEV;
  828. goto err_phy_shutdown;
  829. }
  830. data = rcar_pci_read_reg(pcie, MACSR);
  831. dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
  832. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  833. err = rcar_pcie_enable_msi(host);
  834. if (err < 0) {
  835. dev_err(dev,
  836. "failed to enable MSI support: %d\n",
  837. err);
  838. goto err_phy_shutdown;
  839. }
  840. }
  841. err = rcar_pcie_enable(host);
  842. if (err)
  843. goto err_msi_teardown;
  844. return 0;
  845. err_msi_teardown:
  846. if (IS_ENABLED(CONFIG_PCI_MSI))
  847. rcar_pcie_teardown_msi(host);
  848. err_phy_shutdown:
  849. if (host->phy) {
  850. phy_power_off(host->phy);
  851. phy_exit(host->phy);
  852. }
  853. err_clk_disable:
  854. clk_disable_unprepare(host->bus_clk);
  855. err_unmap_msi_irqs:
  856. irq_dispose_mapping(host->msi.irq2);
  857. irq_dispose_mapping(host->msi.irq1);
  858. err_pm_put:
  859. pm_runtime_put(dev);
  860. pm_runtime_disable(dev);
  861. return err;
  862. }
  863. static int rcar_pcie_resume(struct device *dev)
  864. {
  865. struct rcar_pcie_host *host = dev_get_drvdata(dev);
  866. struct rcar_pcie *pcie = &host->pcie;
  867. unsigned int data;
  868. int err;
  869. err = rcar_pcie_parse_map_dma_ranges(host);
  870. if (err)
  871. return 0;
  872. /* Failure to get a link might just be that no cards are inserted */
  873. err = host->phy_init_fn(host);
  874. if (err) {
  875. dev_info(dev, "PCIe link down\n");
  876. return 0;
  877. }
  878. data = rcar_pci_read_reg(pcie, MACSR);
  879. dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
  880. /* Enable MSI */
  881. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  882. struct resource res;
  883. u32 val;
  884. of_address_to_resource(dev->of_node, 0, &res);
  885. rcar_pci_write_reg(pcie, upper_32_bits(res.start), PCIEMSIAUR);
  886. rcar_pci_write_reg(pcie, lower_32_bits(res.start) | MSIFE, PCIEMSIALR);
  887. bitmap_to_arr32(&val, host->msi.used, INT_PCI_MSI_NR);
  888. rcar_pci_write_reg(pcie, val, PCIEMSIIER);
  889. }
  890. rcar_pcie_hw_enable(host);
  891. return 0;
  892. }
  893. static int rcar_pcie_resume_noirq(struct device *dev)
  894. {
  895. struct rcar_pcie_host *host = dev_get_drvdata(dev);
  896. struct rcar_pcie *pcie = &host->pcie;
  897. if (rcar_pci_read_reg(pcie, PMSR) &&
  898. !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
  899. return 0;
  900. /* Re-establish the PCIe link */
  901. rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
  902. rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
  903. return rcar_pcie_wait_for_dl(pcie);
  904. }
  905. static const struct dev_pm_ops rcar_pcie_pm_ops = {
  906. SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
  907. .resume_noirq = rcar_pcie_resume_noirq,
  908. };
  909. static struct platform_driver rcar_pcie_driver = {
  910. .driver = {
  911. .name = "rcar-pcie",
  912. .of_match_table = rcar_pcie_of_match,
  913. .pm = &rcar_pcie_pm_ops,
  914. .suppress_bind_attrs = true,
  915. },
  916. .probe = rcar_pcie_probe,
  917. };
  918. #ifdef CONFIG_ARM
  919. static int rcar_pcie_aarch32_abort_handler(unsigned long addr,
  920. unsigned int fsr, struct pt_regs *regs)
  921. {
  922. return !fixup_exception(regs);
  923. }
  924. static const struct of_device_id rcar_pcie_abort_handler_of_match[] __initconst = {
  925. { .compatible = "renesas,pcie-r8a7779" },
  926. { .compatible = "renesas,pcie-r8a7790" },
  927. { .compatible = "renesas,pcie-r8a7791" },
  928. { .compatible = "renesas,pcie-rcar-gen2" },
  929. {},
  930. };
  931. static int __init rcar_pcie_init(void)
  932. {
  933. if (of_find_matching_node(NULL, rcar_pcie_abort_handler_of_match)) {
  934. #ifdef CONFIG_ARM_LPAE
  935. hook_fault_code(17, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
  936. "asynchronous external abort");
  937. #else
  938. hook_fault_code(22, rcar_pcie_aarch32_abort_handler, SIGBUS, 0,
  939. "imprecise external abort");
  940. #endif
  941. }
  942. return platform_driver_register(&rcar_pcie_driver);
  943. }
  944. device_initcall(rcar_pcie_init);
  945. #else
  946. builtin_platform_driver(rcar_pcie_driver);
  947. #endif