pcie-rcar.c 25 KB

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