pcie-rcar.c 25 KB

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