pcie-rcar-host.c 29 KB

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