pcie-rcar.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * PCIe driver for Renesas R-Car SoCs
  3. * Copyright (C) 2014 Renesas Electronics Europe Ltd
  4. *
  5. * Based on:
  6. * arch/sh/drivers/pci/pcie-sh7786.c
  7. * arch/sh/drivers/pci/ops-sh7786.c
  8. * Copyright (C) 2009 - 2011 Paul Mundt
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/pci.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #define DRV_NAME "rcar-pcie"
  30. #define PCIECAR 0x000010
  31. #define PCIECCTLR 0x000018
  32. #define CONFIG_SEND_ENABLE (1 << 31)
  33. #define TYPE0 (0 << 8)
  34. #define TYPE1 (1 << 8)
  35. #define PCIECDR 0x000020
  36. #define PCIEMSR 0x000028
  37. #define PCIEINTXR 0x000400
  38. #define PCIEMSITXR 0x000840
  39. /* Transfer control */
  40. #define PCIETCTLR 0x02000
  41. #define CFINIT 1
  42. #define PCIETSTR 0x02004
  43. #define DATA_LINK_ACTIVE 1
  44. #define PCIEERRFR 0x02020
  45. #define UNSUPPORTED_REQUEST (1 << 4)
  46. #define PCIEMSIFR 0x02044
  47. #define PCIEMSIALR 0x02048
  48. #define MSIFE 1
  49. #define PCIEMSIAUR 0x0204c
  50. #define PCIEMSIIER 0x02050
  51. /* root port address */
  52. #define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
  53. /* local address reg & mask */
  54. #define PCIELAR(x) (0x02200 + ((x) * 0x20))
  55. #define PCIELAMR(x) (0x02208 + ((x) * 0x20))
  56. #define LAM_PREFETCH (1 << 3)
  57. #define LAM_64BIT (1 << 2)
  58. #define LAR_ENABLE (1 << 1)
  59. /* PCIe address reg & mask */
  60. #define PCIEPARL(x) (0x03400 + ((x) * 0x20))
  61. #define PCIEPARH(x) (0x03404 + ((x) * 0x20))
  62. #define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
  63. #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
  64. #define PAR_ENABLE (1 << 31)
  65. #define IO_SPACE (1 << 8)
  66. /* Configuration */
  67. #define PCICONF(x) (0x010000 + ((x) * 0x4))
  68. #define PMCAP(x) (0x010040 + ((x) * 0x4))
  69. #define EXPCAP(x) (0x010070 + ((x) * 0x4))
  70. #define VCCAP(x) (0x010100 + ((x) * 0x4))
  71. /* link layer */
  72. #define IDSETR1 0x011004
  73. #define TLCTLR 0x011048
  74. #define MACSR 0x011054
  75. #define MACCTLR 0x011058
  76. #define SCRAMBLE_DISABLE (1 << 27)
  77. /* R-Car H1 PHY */
  78. #define H1_PCIEPHYADRR 0x04000c
  79. #define WRITE_CMD (1 << 16)
  80. #define PHY_ACK (1 << 24)
  81. #define RATE_POS 12
  82. #define LANE_POS 8
  83. #define ADR_POS 0
  84. #define H1_PCIEPHYDOUTR 0x040014
  85. #define H1_PCIEPHYSR 0x040018
  86. #define INT_PCI_MSI_NR 32
  87. #define RCONF(x) (PCICONF(0)+(x))
  88. #define RPMCAP(x) (PMCAP(0)+(x))
  89. #define REXPCAP(x) (EXPCAP(0)+(x))
  90. #define RVCCAP(x) (VCCAP(0)+(x))
  91. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
  92. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
  93. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
  94. #define PCI_MAX_RESOURCES 4
  95. #define MAX_NR_INBOUND_MAPS 6
  96. struct rcar_msi {
  97. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  98. struct irq_domain *domain;
  99. struct msi_chip chip;
  100. unsigned long pages;
  101. struct mutex lock;
  102. int irq1;
  103. int irq2;
  104. };
  105. static inline struct rcar_msi *to_rcar_msi(struct msi_chip *chip)
  106. {
  107. return container_of(chip, struct rcar_msi, chip);
  108. }
  109. /* Structure representing the PCIe interface */
  110. struct rcar_pcie {
  111. struct device *dev;
  112. void __iomem *base;
  113. struct resource res[PCI_MAX_RESOURCES];
  114. struct resource busn;
  115. int root_bus_nr;
  116. struct clk *clk;
  117. struct clk *bus_clk;
  118. struct rcar_msi msi;
  119. };
  120. static inline struct rcar_pcie *sys_to_pcie(struct pci_sys_data *sys)
  121. {
  122. return sys->private_data;
  123. }
  124. static void pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
  125. unsigned long reg)
  126. {
  127. writel(val, pcie->base + reg);
  128. }
  129. static unsigned long pci_read_reg(struct rcar_pcie *pcie, unsigned long reg)
  130. {
  131. return readl(pcie->base + reg);
  132. }
  133. enum {
  134. PCI_ACCESS_READ,
  135. PCI_ACCESS_WRITE,
  136. };
  137. static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
  138. {
  139. int shift = 8 * (where & 3);
  140. u32 val = pci_read_reg(pcie, where & ~3);
  141. val &= ~(mask << shift);
  142. val |= data << shift;
  143. pci_write_reg(pcie, val, where & ~3);
  144. }
  145. static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
  146. {
  147. int shift = 8 * (where & 3);
  148. u32 val = pci_read_reg(pcie, where & ~3);
  149. return val >> shift;
  150. }
  151. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  152. static int rcar_pcie_config_access(struct rcar_pcie *pcie,
  153. unsigned char access_type, struct pci_bus *bus,
  154. unsigned int devfn, int where, u32 *data)
  155. {
  156. int dev, func, reg, index;
  157. dev = PCI_SLOT(devfn);
  158. func = PCI_FUNC(devfn);
  159. reg = where & ~3;
  160. index = reg / 4;
  161. /*
  162. * While each channel has its own memory-mapped extended config
  163. * space, it's generally only accessible when in endpoint mode.
  164. * When in root complex mode, the controller is unable to target
  165. * itself with either type 0 or type 1 accesses, and indeed, any
  166. * controller initiated target transfer to its own config space
  167. * result in a completer abort.
  168. *
  169. * Each channel effectively only supports a single device, but as
  170. * the same channel <-> device access works for any PCI_SLOT()
  171. * value, we cheat a bit here and bind the controller's config
  172. * space to devfn 0 in order to enable self-enumeration. In this
  173. * case the regular ECAR/ECDR path is sidelined and the mangled
  174. * config access itself is initiated as an internal bus transaction.
  175. */
  176. if (pci_is_root_bus(bus)) {
  177. if (dev != 0)
  178. return PCIBIOS_DEVICE_NOT_FOUND;
  179. if (access_type == PCI_ACCESS_READ) {
  180. *data = pci_read_reg(pcie, PCICONF(index));
  181. } else {
  182. /* Keep an eye out for changes to the root bus number */
  183. if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
  184. pcie->root_bus_nr = *data & 0xff;
  185. pci_write_reg(pcie, *data, PCICONF(index));
  186. }
  187. return PCIBIOS_SUCCESSFUL;
  188. }
  189. if (pcie->root_bus_nr < 0)
  190. return PCIBIOS_DEVICE_NOT_FOUND;
  191. /* Clear errors */
  192. pci_write_reg(pcie, pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
  193. /* Set the PIO address */
  194. pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) | PCIE_CONF_DEV(dev) |
  195. PCIE_CONF_FUNC(func) | reg, PCIECAR);
  196. /* Enable the configuration access */
  197. if (bus->parent->number == pcie->root_bus_nr)
  198. pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
  199. else
  200. pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
  201. /* Check for errors */
  202. if (pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
  203. return PCIBIOS_DEVICE_NOT_FOUND;
  204. /* Check for master and target aborts */
  205. if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
  206. (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
  207. return PCIBIOS_DEVICE_NOT_FOUND;
  208. if (access_type == PCI_ACCESS_READ)
  209. *data = pci_read_reg(pcie, PCIECDR);
  210. else
  211. pci_write_reg(pcie, *data, PCIECDR);
  212. /* Disable the configuration access */
  213. pci_write_reg(pcie, 0, PCIECCTLR);
  214. return PCIBIOS_SUCCESSFUL;
  215. }
  216. static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  217. int where, int size, u32 *val)
  218. {
  219. struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
  220. int ret;
  221. if ((size == 2) && (where & 1))
  222. return PCIBIOS_BAD_REGISTER_NUMBER;
  223. else if ((size == 4) && (where & 3))
  224. return PCIBIOS_BAD_REGISTER_NUMBER;
  225. ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ,
  226. bus, devfn, where, val);
  227. if (ret != PCIBIOS_SUCCESSFUL) {
  228. *val = 0xffffffff;
  229. return ret;
  230. }
  231. if (size == 1)
  232. *val = (*val >> (8 * (where & 3))) & 0xff;
  233. else if (size == 2)
  234. *val = (*val >> (8 * (where & 2))) & 0xffff;
  235. dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
  236. bus->number, devfn, where, size, (unsigned long)*val);
  237. return ret;
  238. }
  239. /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  240. static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  241. int where, int size, u32 val)
  242. {
  243. struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
  244. int shift, ret;
  245. u32 data;
  246. if ((size == 2) && (where & 1))
  247. return PCIBIOS_BAD_REGISTER_NUMBER;
  248. else if ((size == 4) && (where & 3))
  249. return PCIBIOS_BAD_REGISTER_NUMBER;
  250. ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ,
  251. bus, devfn, where, &data);
  252. if (ret != PCIBIOS_SUCCESSFUL)
  253. return ret;
  254. dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx\n",
  255. bus->number, devfn, where, size, (unsigned long)val);
  256. if (size == 1) {
  257. shift = 8 * (where & 3);
  258. data &= ~(0xff << shift);
  259. data |= ((val & 0xff) << shift);
  260. } else if (size == 2) {
  261. shift = 8 * (where & 2);
  262. data &= ~(0xffff << shift);
  263. data |= ((val & 0xffff) << shift);
  264. } else
  265. data = val;
  266. ret = rcar_pcie_config_access(pcie, PCI_ACCESS_WRITE,
  267. bus, devfn, where, &data);
  268. return ret;
  269. }
  270. static struct pci_ops rcar_pcie_ops = {
  271. .read = rcar_pcie_read_conf,
  272. .write = rcar_pcie_write_conf,
  273. };
  274. static void rcar_pcie_setup_window(int win, struct resource *res,
  275. struct rcar_pcie *pcie)
  276. {
  277. /* Setup PCIe address space mappings for each resource */
  278. resource_size_t size;
  279. u32 mask;
  280. pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
  281. /*
  282. * The PAMR mask is calculated in units of 128Bytes, which
  283. * keeps things pretty simple.
  284. */
  285. size = resource_size(res);
  286. mask = (roundup_pow_of_two(size) / SZ_128) - 1;
  287. pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
  288. pci_write_reg(pcie, upper_32_bits(res->start), PCIEPARH(win));
  289. pci_write_reg(pcie, lower_32_bits(res->start), PCIEPARL(win));
  290. /* First resource is for IO */
  291. mask = PAR_ENABLE;
  292. if (res->flags & IORESOURCE_IO)
  293. mask |= IO_SPACE;
  294. pci_write_reg(pcie, mask, PCIEPTCTLR(win));
  295. }
  296. static int rcar_pcie_setup(int nr, struct pci_sys_data *sys)
  297. {
  298. struct rcar_pcie *pcie = sys_to_pcie(sys);
  299. struct resource *res;
  300. int i;
  301. pcie->root_bus_nr = -1;
  302. /* Setup PCI resources */
  303. for (i = 0; i < PCI_MAX_RESOURCES; i++) {
  304. res = &pcie->res[i];
  305. if (!res->flags)
  306. continue;
  307. rcar_pcie_setup_window(i, res, pcie);
  308. if (res->flags & IORESOURCE_IO)
  309. pci_ioremap_io(nr * SZ_64K, res->start);
  310. else
  311. pci_add_resource(&sys->resources, res);
  312. }
  313. pci_add_resource(&sys->resources, &pcie->busn);
  314. return 1;
  315. }
  316. static void rcar_pcie_add_bus(struct pci_bus *bus)
  317. {
  318. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  319. struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata);
  320. bus->msi = &pcie->msi.chip;
  321. }
  322. }
  323. struct hw_pci rcar_pci = {
  324. .setup = rcar_pcie_setup,
  325. .map_irq = of_irq_parse_and_map_pci,
  326. .ops = &rcar_pcie_ops,
  327. .add_bus = rcar_pcie_add_bus,
  328. };
  329. static void rcar_pcie_enable(struct rcar_pcie *pcie)
  330. {
  331. struct platform_device *pdev = to_platform_device(pcie->dev);
  332. rcar_pci.nr_controllers = 1;
  333. rcar_pci.private_data = (void **)&pcie;
  334. pci_common_init_dev(&pdev->dev, &rcar_pci);
  335. #ifdef CONFIG_PCI_DOMAINS
  336. rcar_pci.domain++;
  337. #endif
  338. }
  339. static int phy_wait_for_ack(struct rcar_pcie *pcie)
  340. {
  341. unsigned int timeout = 100;
  342. while (timeout--) {
  343. if (pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
  344. return 0;
  345. udelay(100);
  346. }
  347. dev_err(pcie->dev, "Access to PCIe phy timed out\n");
  348. return -ETIMEDOUT;
  349. }
  350. static void phy_write_reg(struct rcar_pcie *pcie,
  351. unsigned int rate, unsigned int addr,
  352. unsigned int lane, unsigned int data)
  353. {
  354. unsigned long phyaddr;
  355. phyaddr = WRITE_CMD |
  356. ((rate & 1) << RATE_POS) |
  357. ((lane & 0xf) << LANE_POS) |
  358. ((addr & 0xff) << ADR_POS);
  359. /* Set write data */
  360. pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
  361. pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
  362. /* Ignore errors as they will be dealt with if the data link is down */
  363. phy_wait_for_ack(pcie);
  364. /* Clear command */
  365. pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
  366. pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
  367. /* Ignore errors as they will be dealt with if the data link is down */
  368. phy_wait_for_ack(pcie);
  369. }
  370. static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
  371. {
  372. unsigned int timeout = 10;
  373. while (timeout--) {
  374. if ((pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
  375. return 0;
  376. msleep(5);
  377. }
  378. return -ETIMEDOUT;
  379. }
  380. static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
  381. {
  382. int err;
  383. /* Begin initialization */
  384. pci_write_reg(pcie, 0, PCIETCTLR);
  385. /* Set mode */
  386. pci_write_reg(pcie, 1, PCIEMSR);
  387. /*
  388. * Initial header for port config space is type 1, set the device
  389. * class to match. Hardware takes care of propagating the IDSETR
  390. * settings, so there is no need to bother with a quirk.
  391. */
  392. pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
  393. /*
  394. * Setup Secondary Bus Number & Subordinate Bus Number, even though
  395. * they aren't used, to avoid bridge being detected as broken.
  396. */
  397. rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
  398. rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
  399. /* Initialize default capabilities. */
  400. rcar_rmw32(pcie, REXPCAP(0), 0, PCI_CAP_ID_EXP);
  401. rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
  402. PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
  403. rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
  404. PCI_HEADER_TYPE_BRIDGE);
  405. /* Enable data link layer active state reporting */
  406. rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), 0, PCI_EXP_LNKCAP_DLLLARC);
  407. /* Write out the physical slot number = 0 */
  408. rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
  409. /* Set the completion timer timeout to the maximum 50ms. */
  410. rcar_rmw32(pcie, TLCTLR+1, 0x3f, 50);
  411. /* Terminate list of capabilities (Next Capability Offset=0) */
  412. rcar_rmw32(pcie, RVCCAP(0), 0xfff0, 0);
  413. /* Enable MAC data scrambling. */
  414. rcar_rmw32(pcie, MACCTLR, SCRAMBLE_DISABLE, 0);
  415. /* Enable MSI */
  416. if (IS_ENABLED(CONFIG_PCI_MSI))
  417. pci_write_reg(pcie, 0x101f0000, PCIEMSITXR);
  418. /* Finish initialization - establish a PCI Express link */
  419. pci_write_reg(pcie, CFINIT, PCIETCTLR);
  420. /* This will timeout if we don't have a link. */
  421. err = rcar_pcie_wait_for_dl(pcie);
  422. if (err)
  423. return err;
  424. /* Enable INTx interrupts */
  425. rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
  426. /* Enable slave Bus Mastering */
  427. rcar_rmw32(pcie, RCONF(PCI_STATUS), PCI_STATUS_DEVSEL_MASK,
  428. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  429. PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST);
  430. wmb();
  431. return 0;
  432. }
  433. static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
  434. {
  435. unsigned int timeout = 10;
  436. /* Initialize the phy */
  437. phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
  438. phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
  439. phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
  440. phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
  441. phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
  442. phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
  443. phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
  444. phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
  445. phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
  446. phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
  447. phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
  448. phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
  449. phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
  450. phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
  451. phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
  452. while (timeout--) {
  453. if (pci_read_reg(pcie, H1_PCIEPHYSR))
  454. return rcar_pcie_hw_init(pcie);
  455. msleep(5);
  456. }
  457. return -ETIMEDOUT;
  458. }
  459. static int rcar_msi_alloc(struct rcar_msi *chip)
  460. {
  461. int msi;
  462. mutex_lock(&chip->lock);
  463. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  464. if (msi < INT_PCI_MSI_NR)
  465. set_bit(msi, chip->used);
  466. else
  467. msi = -ENOSPC;
  468. mutex_unlock(&chip->lock);
  469. return msi;
  470. }
  471. static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
  472. {
  473. mutex_lock(&chip->lock);
  474. clear_bit(irq, chip->used);
  475. mutex_unlock(&chip->lock);
  476. }
  477. static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
  478. {
  479. struct rcar_pcie *pcie = data;
  480. struct rcar_msi *msi = &pcie->msi;
  481. unsigned long reg;
  482. reg = pci_read_reg(pcie, PCIEMSIFR);
  483. /* MSI & INTx share an interrupt - we only handle MSI here */
  484. if (!reg)
  485. return IRQ_NONE;
  486. while (reg) {
  487. unsigned int index = find_first_bit(&reg, 32);
  488. unsigned int irq;
  489. /* clear the interrupt */
  490. pci_write_reg(pcie, 1 << index, PCIEMSIFR);
  491. irq = irq_find_mapping(msi->domain, index);
  492. if (irq) {
  493. if (test_bit(index, msi->used))
  494. generic_handle_irq(irq);
  495. else
  496. dev_info(pcie->dev, "unhandled MSI\n");
  497. } else {
  498. /* Unknown MSI, just clear it */
  499. dev_dbg(pcie->dev, "unexpected MSI\n");
  500. }
  501. /* see if there's any more pending in this vector */
  502. reg = pci_read_reg(pcie, PCIEMSIFR);
  503. }
  504. return IRQ_HANDLED;
  505. }
  506. static int rcar_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
  507. struct msi_desc *desc)
  508. {
  509. struct rcar_msi *msi = to_rcar_msi(chip);
  510. struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
  511. struct msi_msg msg;
  512. unsigned int irq;
  513. int hwirq;
  514. hwirq = rcar_msi_alloc(msi);
  515. if (hwirq < 0)
  516. return hwirq;
  517. irq = irq_create_mapping(msi->domain, hwirq);
  518. if (!irq) {
  519. rcar_msi_free(msi, hwirq);
  520. return -EINVAL;
  521. }
  522. irq_set_msi_desc(irq, desc);
  523. msg.address_lo = pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
  524. msg.address_hi = pci_read_reg(pcie, PCIEMSIAUR);
  525. msg.data = hwirq;
  526. write_msi_msg(irq, &msg);
  527. return 0;
  528. }
  529. static void rcar_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
  530. {
  531. struct rcar_msi *msi = to_rcar_msi(chip);
  532. struct irq_data *d = irq_get_irq_data(irq);
  533. rcar_msi_free(msi, d->hwirq);
  534. }
  535. static struct irq_chip rcar_msi_irq_chip = {
  536. .name = "R-Car PCIe MSI",
  537. .irq_enable = unmask_msi_irq,
  538. .irq_disable = mask_msi_irq,
  539. .irq_mask = mask_msi_irq,
  540. .irq_unmask = unmask_msi_irq,
  541. };
  542. static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
  543. irq_hw_number_t hwirq)
  544. {
  545. irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
  546. irq_set_chip_data(irq, domain->host_data);
  547. set_irq_flags(irq, IRQF_VALID);
  548. return 0;
  549. }
  550. static const struct irq_domain_ops msi_domain_ops = {
  551. .map = rcar_msi_map,
  552. };
  553. static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
  554. {
  555. struct platform_device *pdev = to_platform_device(pcie->dev);
  556. struct rcar_msi *msi = &pcie->msi;
  557. unsigned long base;
  558. int err;
  559. mutex_init(&msi->lock);
  560. msi->chip.dev = pcie->dev;
  561. msi->chip.setup_irq = rcar_msi_setup_irq;
  562. msi->chip.teardown_irq = rcar_msi_teardown_irq;
  563. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  564. &msi_domain_ops, &msi->chip);
  565. if (!msi->domain) {
  566. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  567. return -ENOMEM;
  568. }
  569. /* Two irqs are for MSI, but they are also used for non-MSI irqs */
  570. err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
  571. IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
  572. if (err < 0) {
  573. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  574. goto err;
  575. }
  576. err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
  577. IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
  578. if (err < 0) {
  579. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  580. goto err;
  581. }
  582. /* setup MSI data target */
  583. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  584. base = virt_to_phys((void *)msi->pages);
  585. pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
  586. pci_write_reg(pcie, 0, PCIEMSIAUR);
  587. /* enable all MSI interrupts */
  588. pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
  589. return 0;
  590. err:
  591. irq_domain_remove(msi->domain);
  592. return err;
  593. }
  594. static int rcar_pcie_get_resources(struct platform_device *pdev,
  595. struct rcar_pcie *pcie)
  596. {
  597. struct resource res;
  598. int err, i;
  599. err = of_address_to_resource(pdev->dev.of_node, 0, &res);
  600. if (err)
  601. return err;
  602. pcie->clk = devm_clk_get(&pdev->dev, "pcie");
  603. if (IS_ERR(pcie->clk)) {
  604. dev_err(pcie->dev, "cannot get platform clock\n");
  605. return PTR_ERR(pcie->clk);
  606. }
  607. err = clk_prepare_enable(pcie->clk);
  608. if (err)
  609. goto fail_clk;
  610. pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
  611. if (IS_ERR(pcie->bus_clk)) {
  612. dev_err(pcie->dev, "cannot get pcie bus clock\n");
  613. err = PTR_ERR(pcie->bus_clk);
  614. goto fail_clk;
  615. }
  616. err = clk_prepare_enable(pcie->bus_clk);
  617. if (err)
  618. goto err_map_reg;
  619. i = irq_of_parse_and_map(pdev->dev.of_node, 0);
  620. if (i < 0) {
  621. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  622. err = -ENOENT;
  623. goto err_map_reg;
  624. }
  625. pcie->msi.irq1 = i;
  626. i = irq_of_parse_and_map(pdev->dev.of_node, 1);
  627. if (i < 0) {
  628. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  629. err = -ENOENT;
  630. goto err_map_reg;
  631. }
  632. pcie->msi.irq2 = i;
  633. pcie->base = devm_ioremap_resource(&pdev->dev, &res);
  634. if (IS_ERR(pcie->base)) {
  635. err = PTR_ERR(pcie->base);
  636. goto err_map_reg;
  637. }
  638. return 0;
  639. err_map_reg:
  640. clk_disable_unprepare(pcie->bus_clk);
  641. fail_clk:
  642. clk_disable_unprepare(pcie->clk);
  643. return err;
  644. }
  645. static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
  646. struct of_pci_range *range,
  647. int *index)
  648. {
  649. u64 restype = range->flags;
  650. u64 cpu_addr = range->cpu_addr;
  651. u64 cpu_end = range->cpu_addr + range->size;
  652. u64 pci_addr = range->pci_addr;
  653. u32 flags = LAM_64BIT | LAR_ENABLE;
  654. u64 mask;
  655. u64 size;
  656. int idx = *index;
  657. if (restype & IORESOURCE_PREFETCH)
  658. flags |= LAM_PREFETCH;
  659. /*
  660. * If the size of the range is larger than the alignment of the start
  661. * address, we have to use multiple entries to perform the mapping.
  662. */
  663. if (cpu_addr > 0) {
  664. unsigned long nr_zeros = __ffs64(cpu_addr);
  665. u64 alignment = 1ULL << nr_zeros;
  666. size = min(range->size, alignment);
  667. } else {
  668. size = range->size;
  669. }
  670. /* Hardware supports max 4GiB inbound region */
  671. size = min(size, 1ULL << 32);
  672. mask = roundup_pow_of_two(size) - 1;
  673. mask &= ~0xf;
  674. while (cpu_addr < cpu_end) {
  675. /*
  676. * Set up 64-bit inbound regions as the range parser doesn't
  677. * distinguish between 32 and 64-bit types.
  678. */
  679. pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
  680. pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
  681. pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
  682. pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
  683. pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
  684. pci_write_reg(pcie, 0, PCIELAMR(idx+1));
  685. pci_addr += size;
  686. cpu_addr += size;
  687. idx += 2;
  688. if (idx > MAX_NR_INBOUND_MAPS) {
  689. dev_err(pcie->dev, "Failed to map inbound regions!\n");
  690. return -EINVAL;
  691. }
  692. }
  693. *index = idx;
  694. return 0;
  695. }
  696. static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  697. struct device_node *node)
  698. {
  699. const int na = 3, ns = 2;
  700. int rlen;
  701. parser->node = node;
  702. parser->pna = of_n_addr_cells(node);
  703. parser->np = parser->pna + na + ns;
  704. parser->range = of_get_property(node, "dma-ranges", &rlen);
  705. if (!parser->range)
  706. return -ENOENT;
  707. parser->end = parser->range + rlen / sizeof(__be32);
  708. return 0;
  709. }
  710. static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
  711. struct device_node *np)
  712. {
  713. struct of_pci_range range;
  714. struct of_pci_range_parser parser;
  715. int index = 0;
  716. int err;
  717. if (pci_dma_range_parser_init(&parser, np))
  718. return -EINVAL;
  719. /* Get the dma-ranges from DT */
  720. for_each_of_pci_range(&parser, &range) {
  721. u64 end = range.cpu_addr + range.size - 1;
  722. dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
  723. range.flags, range.cpu_addr, end, range.pci_addr);
  724. err = rcar_pcie_inbound_ranges(pcie, &range, &index);
  725. if (err)
  726. return err;
  727. }
  728. return 0;
  729. }
  730. static const struct of_device_id rcar_pcie_of_match[] = {
  731. { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
  732. { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init },
  733. { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init },
  734. {},
  735. };
  736. MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
  737. static int rcar_pcie_probe(struct platform_device *pdev)
  738. {
  739. struct rcar_pcie *pcie;
  740. unsigned int data;
  741. struct of_pci_range range;
  742. struct of_pci_range_parser parser;
  743. const struct of_device_id *of_id;
  744. int err, win = 0;
  745. int (*hw_init_fn)(struct rcar_pcie *);
  746. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  747. if (!pcie)
  748. return -ENOMEM;
  749. pcie->dev = &pdev->dev;
  750. platform_set_drvdata(pdev, pcie);
  751. /* Get the bus range */
  752. if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) {
  753. dev_err(&pdev->dev, "failed to parse bus-range property\n");
  754. return -EINVAL;
  755. }
  756. if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) {
  757. dev_err(&pdev->dev, "missing ranges property\n");
  758. return -EINVAL;
  759. }
  760. err = rcar_pcie_get_resources(pdev, pcie);
  761. if (err < 0) {
  762. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  763. return err;
  764. }
  765. for_each_of_pci_range(&parser, &range) {
  766. of_pci_range_to_resource(&range, pdev->dev.of_node,
  767. &pcie->res[win++]);
  768. if (win > PCI_MAX_RESOURCES)
  769. break;
  770. }
  771. err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
  772. if (err)
  773. return err;
  774. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  775. err = rcar_pcie_enable_msi(pcie);
  776. if (err < 0) {
  777. dev_err(&pdev->dev,
  778. "failed to enable MSI support: %d\n",
  779. err);
  780. return err;
  781. }
  782. }
  783. of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
  784. if (!of_id || !of_id->data)
  785. return -EINVAL;
  786. hw_init_fn = of_id->data;
  787. /* Failure to get a link might just be that no cards are inserted */
  788. err = hw_init_fn(pcie);
  789. if (err) {
  790. dev_info(&pdev->dev, "PCIe link down\n");
  791. return 0;
  792. }
  793. data = pci_read_reg(pcie, MACSR);
  794. dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
  795. rcar_pcie_enable(pcie);
  796. return 0;
  797. }
  798. static struct platform_driver rcar_pcie_driver = {
  799. .driver = {
  800. .name = DRV_NAME,
  801. .owner = THIS_MODULE,
  802. .of_match_table = rcar_pcie_of_match,
  803. .suppress_bind_attrs = true,
  804. },
  805. .probe = rcar_pcie_probe,
  806. };
  807. module_platform_driver(rcar_pcie_driver);
  808. MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
  809. MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
  810. MODULE_LICENSE("GPLv2");