pcie-rcar.c 25 KB

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