pcie-rcar.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. struct rcar_msi {
  97. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  98. struct irq_domain *domain;
  99. struct msi_controller 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_controller *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), PCIEPAUR(win));
  287. rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
  288. PCIEPALR(win));
  289. /* First resource is for IO */
  290. mask = PAR_ENABLE;
  291. if (res->flags & IORESOURCE_IO)
  292. mask |= IO_SPACE;
  293. rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
  294. }
  295. static int rcar_pcie_setup(int nr, struct pci_sys_data *sys)
  296. {
  297. struct rcar_pcie *pcie = sys_to_pcie(sys);
  298. struct resource *res;
  299. int i;
  300. pcie->root_bus_nr = -1;
  301. /* Setup PCI resources */
  302. for (i = 0; i < RCAR_PCI_MAX_RESOURCES; i++) {
  303. res = &pcie->res[i];
  304. if (!res->flags)
  305. continue;
  306. rcar_pcie_setup_window(i, pcie);
  307. if (res->flags & IORESOURCE_IO) {
  308. phys_addr_t io_start = pci_pio_to_address(res->start);
  309. pci_ioremap_io(nr * SZ_64K, io_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 struct hw_pci rcar_pci = {
  317. .setup = rcar_pcie_setup,
  318. .map_irq = of_irq_parse_and_map_pci,
  319. .ops = &rcar_pcie_ops,
  320. };
  321. static void rcar_pcie_enable(struct rcar_pcie *pcie)
  322. {
  323. struct platform_device *pdev = to_platform_device(pcie->dev);
  324. rcar_pci.nr_controllers = 1;
  325. rcar_pci.private_data = (void **)&pcie;
  326. #ifdef CONFIG_PCI_MSI
  327. rcar_pci.msi_ctrl = &pcie->msi.chip;
  328. #endif
  329. pci_common_init_dev(&pdev->dev, &rcar_pci);
  330. }
  331. static int phy_wait_for_ack(struct rcar_pcie *pcie)
  332. {
  333. unsigned int timeout = 100;
  334. while (timeout--) {
  335. if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
  336. return 0;
  337. udelay(100);
  338. }
  339. dev_err(pcie->dev, "Access to PCIe phy timed out\n");
  340. return -ETIMEDOUT;
  341. }
  342. static void phy_write_reg(struct rcar_pcie *pcie,
  343. unsigned int rate, unsigned int addr,
  344. unsigned int lane, unsigned int data)
  345. {
  346. unsigned long phyaddr;
  347. phyaddr = WRITE_CMD |
  348. ((rate & 1) << RATE_POS) |
  349. ((lane & 0xf) << LANE_POS) |
  350. ((addr & 0xff) << ADR_POS);
  351. /* Set write data */
  352. rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
  353. rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
  354. /* Ignore errors as they will be dealt with if the data link is down */
  355. phy_wait_for_ack(pcie);
  356. /* Clear command */
  357. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
  358. rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
  359. /* Ignore errors as they will be dealt with if the data link is down */
  360. phy_wait_for_ack(pcie);
  361. }
  362. static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
  363. {
  364. unsigned int timeout = 10;
  365. while (timeout--) {
  366. if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
  367. return 0;
  368. msleep(5);
  369. }
  370. return -ETIMEDOUT;
  371. }
  372. static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
  373. {
  374. int err;
  375. /* Begin initialization */
  376. rcar_pci_write_reg(pcie, 0, PCIETCTLR);
  377. /* Set mode */
  378. rcar_pci_write_reg(pcie, 1, PCIEMSR);
  379. /*
  380. * Initial header for port config space is type 1, set the device
  381. * class to match. Hardware takes care of propagating the IDSETR
  382. * settings, so there is no need to bother with a quirk.
  383. */
  384. rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
  385. /*
  386. * Setup Secondary Bus Number & Subordinate Bus Number, even though
  387. * they aren't used, to avoid bridge being detected as broken.
  388. */
  389. rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
  390. rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
  391. /* Initialize default capabilities. */
  392. rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
  393. rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
  394. PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
  395. rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
  396. PCI_HEADER_TYPE_BRIDGE);
  397. /* Enable data link layer active state reporting */
  398. rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
  399. PCI_EXP_LNKCAP_DLLLARC);
  400. /* Write out the physical slot number = 0 */
  401. rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
  402. /* Set the completion timer timeout to the maximum 50ms. */
  403. rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
  404. /* Terminate list of capabilities (Next Capability Offset=0) */
  405. rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
  406. /* Enable MSI */
  407. if (IS_ENABLED(CONFIG_PCI_MSI))
  408. rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
  409. /* Finish initialization - establish a PCI Express link */
  410. rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
  411. /* This will timeout if we don't have a link. */
  412. err = rcar_pcie_wait_for_dl(pcie);
  413. if (err)
  414. return err;
  415. /* Enable INTx interrupts */
  416. rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);
  417. wmb();
  418. return 0;
  419. }
  420. static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
  421. {
  422. unsigned int timeout = 10;
  423. /* Initialize the phy */
  424. phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
  425. phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
  426. phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
  427. phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
  428. phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
  429. phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
  430. phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
  431. phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
  432. phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
  433. phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
  434. phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
  435. phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);
  436. phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
  437. phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
  438. phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);
  439. while (timeout--) {
  440. if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
  441. return rcar_pcie_hw_init(pcie);
  442. msleep(5);
  443. }
  444. return -ETIMEDOUT;
  445. }
  446. static int rcar_msi_alloc(struct rcar_msi *chip)
  447. {
  448. int msi;
  449. mutex_lock(&chip->lock);
  450. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  451. if (msi < INT_PCI_MSI_NR)
  452. set_bit(msi, chip->used);
  453. else
  454. msi = -ENOSPC;
  455. mutex_unlock(&chip->lock);
  456. return msi;
  457. }
  458. static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
  459. {
  460. mutex_lock(&chip->lock);
  461. clear_bit(irq, chip->used);
  462. mutex_unlock(&chip->lock);
  463. }
  464. static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
  465. {
  466. struct rcar_pcie *pcie = data;
  467. struct rcar_msi *msi = &pcie->msi;
  468. unsigned long reg;
  469. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  470. /* MSI & INTx share an interrupt - we only handle MSI here */
  471. if (!reg)
  472. return IRQ_NONE;
  473. while (reg) {
  474. unsigned int index = find_first_bit(&reg, 32);
  475. unsigned int irq;
  476. /* clear the interrupt */
  477. rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
  478. irq = irq_find_mapping(msi->domain, index);
  479. if (irq) {
  480. if (test_bit(index, msi->used))
  481. generic_handle_irq(irq);
  482. else
  483. dev_info(pcie->dev, "unhandled MSI\n");
  484. } else {
  485. /* Unknown MSI, just clear it */
  486. dev_dbg(pcie->dev, "unexpected MSI\n");
  487. }
  488. /* see if there's any more pending in this vector */
  489. reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
  490. }
  491. return IRQ_HANDLED;
  492. }
  493. static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
  494. struct msi_desc *desc)
  495. {
  496. struct rcar_msi *msi = to_rcar_msi(chip);
  497. struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
  498. struct msi_msg msg;
  499. unsigned int irq;
  500. int hwirq;
  501. hwirq = rcar_msi_alloc(msi);
  502. if (hwirq < 0)
  503. return hwirq;
  504. irq = irq_create_mapping(msi->domain, hwirq);
  505. if (!irq) {
  506. rcar_msi_free(msi, hwirq);
  507. return -EINVAL;
  508. }
  509. irq_set_msi_desc(irq, desc);
  510. msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
  511. msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
  512. msg.data = hwirq;
  513. pci_write_msi_msg(irq, &msg);
  514. return 0;
  515. }
  516. static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
  517. {
  518. struct rcar_msi *msi = to_rcar_msi(chip);
  519. struct irq_data *d = irq_get_irq_data(irq);
  520. rcar_msi_free(msi, d->hwirq);
  521. }
  522. static struct irq_chip rcar_msi_irq_chip = {
  523. .name = "R-Car PCIe MSI",
  524. .irq_enable = pci_msi_unmask_irq,
  525. .irq_disable = pci_msi_mask_irq,
  526. .irq_mask = pci_msi_mask_irq,
  527. .irq_unmask = pci_msi_unmask_irq,
  528. };
  529. static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
  530. irq_hw_number_t hwirq)
  531. {
  532. irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
  533. irq_set_chip_data(irq, domain->host_data);
  534. return 0;
  535. }
  536. static const struct irq_domain_ops msi_domain_ops = {
  537. .map = rcar_msi_map,
  538. };
  539. static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
  540. {
  541. struct platform_device *pdev = to_platform_device(pcie->dev);
  542. struct rcar_msi *msi = &pcie->msi;
  543. unsigned long base;
  544. int err;
  545. mutex_init(&msi->lock);
  546. msi->chip.dev = pcie->dev;
  547. msi->chip.setup_irq = rcar_msi_setup_irq;
  548. msi->chip.teardown_irq = rcar_msi_teardown_irq;
  549. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  550. &msi_domain_ops, &msi->chip);
  551. if (!msi->domain) {
  552. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  553. return -ENOMEM;
  554. }
  555. /* Two irqs are for MSI, but they are also used for non-MSI irqs */
  556. err = devm_request_irq(&pdev->dev, msi->irq1, rcar_pcie_msi_irq,
  557. IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
  558. if (err < 0) {
  559. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  560. goto err;
  561. }
  562. err = devm_request_irq(&pdev->dev, msi->irq2, rcar_pcie_msi_irq,
  563. IRQF_SHARED, rcar_msi_irq_chip.name, pcie);
  564. if (err < 0) {
  565. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  566. goto err;
  567. }
  568. /* setup MSI data target */
  569. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  570. base = virt_to_phys((void *)msi->pages);
  571. rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
  572. rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
  573. /* enable all MSI interrupts */
  574. rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
  575. return 0;
  576. err:
  577. irq_domain_remove(msi->domain);
  578. return err;
  579. }
  580. static int rcar_pcie_get_resources(struct platform_device *pdev,
  581. struct rcar_pcie *pcie)
  582. {
  583. struct resource res;
  584. int err, i;
  585. err = of_address_to_resource(pdev->dev.of_node, 0, &res);
  586. if (err)
  587. return err;
  588. pcie->clk = devm_clk_get(&pdev->dev, "pcie");
  589. if (IS_ERR(pcie->clk)) {
  590. dev_err(pcie->dev, "cannot get platform clock\n");
  591. return PTR_ERR(pcie->clk);
  592. }
  593. err = clk_prepare_enable(pcie->clk);
  594. if (err)
  595. goto fail_clk;
  596. pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
  597. if (IS_ERR(pcie->bus_clk)) {
  598. dev_err(pcie->dev, "cannot get pcie bus clock\n");
  599. err = PTR_ERR(pcie->bus_clk);
  600. goto fail_clk;
  601. }
  602. err = clk_prepare_enable(pcie->bus_clk);
  603. if (err)
  604. goto err_map_reg;
  605. i = irq_of_parse_and_map(pdev->dev.of_node, 0);
  606. if (!i) {
  607. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  608. err = -ENOENT;
  609. goto err_map_reg;
  610. }
  611. pcie->msi.irq1 = i;
  612. i = irq_of_parse_and_map(pdev->dev.of_node, 1);
  613. if (!i) {
  614. dev_err(pcie->dev, "cannot get platform resources for msi interrupt\n");
  615. err = -ENOENT;
  616. goto err_map_reg;
  617. }
  618. pcie->msi.irq2 = i;
  619. pcie->base = devm_ioremap_resource(&pdev->dev, &res);
  620. if (IS_ERR(pcie->base)) {
  621. err = PTR_ERR(pcie->base);
  622. goto err_map_reg;
  623. }
  624. return 0;
  625. err_map_reg:
  626. clk_disable_unprepare(pcie->bus_clk);
  627. fail_clk:
  628. clk_disable_unprepare(pcie->clk);
  629. return err;
  630. }
  631. static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
  632. struct of_pci_range *range,
  633. int *index)
  634. {
  635. u64 restype = range->flags;
  636. u64 cpu_addr = range->cpu_addr;
  637. u64 cpu_end = range->cpu_addr + range->size;
  638. u64 pci_addr = range->pci_addr;
  639. u32 flags = LAM_64BIT | LAR_ENABLE;
  640. u64 mask;
  641. u64 size;
  642. int idx = *index;
  643. if (restype & IORESOURCE_PREFETCH)
  644. flags |= LAM_PREFETCH;
  645. /*
  646. * If the size of the range is larger than the alignment of the start
  647. * address, we have to use multiple entries to perform the mapping.
  648. */
  649. if (cpu_addr > 0) {
  650. unsigned long nr_zeros = __ffs64(cpu_addr);
  651. u64 alignment = 1ULL << nr_zeros;
  652. size = min(range->size, alignment);
  653. } else {
  654. size = range->size;
  655. }
  656. /* Hardware supports max 4GiB inbound region */
  657. size = min(size, 1ULL << 32);
  658. mask = roundup_pow_of_two(size) - 1;
  659. mask &= ~0xf;
  660. while (cpu_addr < cpu_end) {
  661. /*
  662. * Set up 64-bit inbound regions as the range parser doesn't
  663. * distinguish between 32 and 64-bit types.
  664. */
  665. rcar_pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx));
  666. rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
  667. rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx));
  668. rcar_pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1));
  669. rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1));
  670. rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
  671. pci_addr += size;
  672. cpu_addr += size;
  673. idx += 2;
  674. if (idx > MAX_NR_INBOUND_MAPS) {
  675. dev_err(pcie->dev, "Failed to map inbound regions!\n");
  676. return -EINVAL;
  677. }
  678. }
  679. *index = idx;
  680. return 0;
  681. }
  682. static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  683. struct device_node *node)
  684. {
  685. const int na = 3, ns = 2;
  686. int rlen;
  687. parser->node = node;
  688. parser->pna = of_n_addr_cells(node);
  689. parser->np = parser->pna + na + ns;
  690. parser->range = of_get_property(node, "dma-ranges", &rlen);
  691. if (!parser->range)
  692. return -ENOENT;
  693. parser->end = parser->range + rlen / sizeof(__be32);
  694. return 0;
  695. }
  696. static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
  697. struct device_node *np)
  698. {
  699. struct of_pci_range range;
  700. struct of_pci_range_parser parser;
  701. int index = 0;
  702. int err;
  703. if (pci_dma_range_parser_init(&parser, np))
  704. return -EINVAL;
  705. /* Get the dma-ranges from DT */
  706. for_each_of_pci_range(&parser, &range) {
  707. u64 end = range.cpu_addr + range.size - 1;
  708. dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
  709. range.flags, range.cpu_addr, end, range.pci_addr);
  710. err = rcar_pcie_inbound_ranges(pcie, &range, &index);
  711. if (err)
  712. return err;
  713. }
  714. return 0;
  715. }
  716. static const struct of_device_id rcar_pcie_of_match[] = {
  717. { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
  718. { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init },
  719. { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init },
  720. {},
  721. };
  722. MODULE_DEVICE_TABLE(of, rcar_pcie_of_match);
  723. static int rcar_pcie_probe(struct platform_device *pdev)
  724. {
  725. struct rcar_pcie *pcie;
  726. unsigned int data;
  727. struct of_pci_range range;
  728. struct of_pci_range_parser parser;
  729. const struct of_device_id *of_id;
  730. int err, win = 0;
  731. int (*hw_init_fn)(struct rcar_pcie *);
  732. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  733. if (!pcie)
  734. return -ENOMEM;
  735. pcie->dev = &pdev->dev;
  736. platform_set_drvdata(pdev, pcie);
  737. /* Get the bus range */
  738. if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) {
  739. dev_err(&pdev->dev, "failed to parse bus-range property\n");
  740. return -EINVAL;
  741. }
  742. if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) {
  743. dev_err(&pdev->dev, "missing ranges property\n");
  744. return -EINVAL;
  745. }
  746. err = rcar_pcie_get_resources(pdev, pcie);
  747. if (err < 0) {
  748. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  749. return err;
  750. }
  751. for_each_of_pci_range(&parser, &range) {
  752. err = of_pci_range_to_resource(&range, pdev->dev.of_node,
  753. &pcie->res[win++]);
  754. if (err < 0)
  755. return err;
  756. if (win > RCAR_PCI_MAX_RESOURCES)
  757. break;
  758. }
  759. err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node);
  760. if (err)
  761. return err;
  762. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  763. err = rcar_pcie_enable_msi(pcie);
  764. if (err < 0) {
  765. dev_err(&pdev->dev,
  766. "failed to enable MSI support: %d\n",
  767. err);
  768. return err;
  769. }
  770. }
  771. of_id = of_match_device(rcar_pcie_of_match, pcie->dev);
  772. if (!of_id || !of_id->data)
  773. return -EINVAL;
  774. hw_init_fn = of_id->data;
  775. /* Failure to get a link might just be that no cards are inserted */
  776. err = hw_init_fn(pcie);
  777. if (err) {
  778. dev_info(&pdev->dev, "PCIe link down\n");
  779. return 0;
  780. }
  781. data = rcar_pci_read_reg(pcie, MACSR);
  782. dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
  783. rcar_pcie_enable(pcie);
  784. return 0;
  785. }
  786. static struct platform_driver rcar_pcie_driver = {
  787. .driver = {
  788. .name = DRV_NAME,
  789. .of_match_table = rcar_pcie_of_match,
  790. .suppress_bind_attrs = true,
  791. },
  792. .probe = rcar_pcie_probe,
  793. };
  794. module_platform_driver(rcar_pcie_driver);
  795. MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>");
  796. MODULE_DESCRIPTION("Renesas R-Car PCIe driver");
  797. MODULE_LICENSE("GPL v2");