pcie-rcar-host.c 29 KB

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