pcie-rcar-ep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe endpoint driver for Renesas R-Car SoCs
  4. * Copyright (c) 2020 Renesas Electronics Europe GmbH
  5. *
  6. * Author: Lad Prabhakar <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/pci.h>
  12. #include <linux/pci-epc.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include "pcie-rcar.h"
  16. #define RCAR_EPC_MAX_FUNCTIONS 1
  17. /* Structure representing the PCIe interface */
  18. struct rcar_pcie_endpoint {
  19. struct rcar_pcie pcie;
  20. phys_addr_t *ob_mapped_addr;
  21. struct pci_epc_mem_window *ob_window;
  22. u8 max_functions;
  23. unsigned int bar_to_atu[MAX_NR_INBOUND_MAPS];
  24. unsigned long *ib_window_map;
  25. u32 num_ib_windows;
  26. u32 num_ob_windows;
  27. };
  28. static void rcar_pcie_ep_hw_init(struct rcar_pcie *pcie)
  29. {
  30. u32 val;
  31. rcar_pci_write_reg(pcie, 0, PCIETCTLR);
  32. /* Set endpoint mode */
  33. rcar_pci_write_reg(pcie, 0, PCIEMSR);
  34. /* Initialize default capabilities. */
  35. rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
  36. rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
  37. PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ENDPOINT << 4);
  38. rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
  39. PCI_HEADER_TYPE_NORMAL);
  40. /* Write out the physical slot number = 0 */
  41. rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);
  42. val = rcar_pci_read_reg(pcie, EXPCAP(1));
  43. /* device supports fixed 128 bytes MPSS */
  44. val &= ~GENMASK(2, 0);
  45. rcar_pci_write_reg(pcie, val, EXPCAP(1));
  46. val = rcar_pci_read_reg(pcie, EXPCAP(2));
  47. /* read requests size 128 bytes */
  48. val &= ~GENMASK(14, 12);
  49. /* payload size 128 bytes */
  50. val &= ~GENMASK(7, 5);
  51. rcar_pci_write_reg(pcie, val, EXPCAP(2));
  52. /* Set target link speed to 5.0 GT/s */
  53. rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
  54. PCI_EXP_LNKSTA_CLS_5_0GB);
  55. /* Set the completion timer timeout to the maximum 50ms. */
  56. rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
  57. /* Terminate list of capabilities (Next Capability Offset=0) */
  58. rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
  59. /* flush modifications */
  60. wmb();
  61. }
  62. static int rcar_pcie_ep_get_window(struct rcar_pcie_endpoint *ep,
  63. phys_addr_t addr)
  64. {
  65. int i;
  66. for (i = 0; i < ep->num_ob_windows; i++)
  67. if (ep->ob_window[i].phys_base == addr)
  68. return i;
  69. return -EINVAL;
  70. }
  71. static int rcar_pcie_parse_outbound_ranges(struct rcar_pcie_endpoint *ep,
  72. struct platform_device *pdev)
  73. {
  74. struct rcar_pcie *pcie = &ep->pcie;
  75. char outbound_name[10];
  76. struct resource *res;
  77. unsigned int i = 0;
  78. ep->num_ob_windows = 0;
  79. for (i = 0; i < RCAR_PCI_MAX_RESOURCES; i++) {
  80. sprintf(outbound_name, "memory%u", i);
  81. res = platform_get_resource_byname(pdev,
  82. IORESOURCE_MEM,
  83. outbound_name);
  84. if (!res) {
  85. dev_err(pcie->dev, "missing outbound window %u\n", i);
  86. return -EINVAL;
  87. }
  88. if (!devm_request_mem_region(&pdev->dev, res->start,
  89. resource_size(res),
  90. outbound_name)) {
  91. dev_err(pcie->dev, "Cannot request memory region %s.\n",
  92. outbound_name);
  93. return -EIO;
  94. }
  95. ep->ob_window[i].phys_base = res->start;
  96. ep->ob_window[i].size = resource_size(res);
  97. /* controller doesn't support multiple allocation
  98. * from same window, so set page_size to window size
  99. */
  100. ep->ob_window[i].page_size = resource_size(res);
  101. }
  102. ep->num_ob_windows = i;
  103. return 0;
  104. }
  105. static int rcar_pcie_ep_get_pdata(struct rcar_pcie_endpoint *ep,
  106. struct platform_device *pdev)
  107. {
  108. struct rcar_pcie *pcie = &ep->pcie;
  109. struct pci_epc_mem_window *window;
  110. struct device *dev = pcie->dev;
  111. struct resource res;
  112. int err;
  113. err = of_address_to_resource(dev->of_node, 0, &res);
  114. if (err)
  115. return err;
  116. pcie->base = devm_ioremap_resource(dev, &res);
  117. if (IS_ERR(pcie->base))
  118. return PTR_ERR(pcie->base);
  119. ep->ob_window = devm_kcalloc(dev, RCAR_PCI_MAX_RESOURCES,
  120. sizeof(*window), GFP_KERNEL);
  121. if (!ep->ob_window)
  122. return -ENOMEM;
  123. rcar_pcie_parse_outbound_ranges(ep, pdev);
  124. err = of_property_read_u8(dev->of_node, "max-functions",
  125. &ep->max_functions);
  126. if (err < 0 || ep->max_functions > RCAR_EPC_MAX_FUNCTIONS)
  127. ep->max_functions = RCAR_EPC_MAX_FUNCTIONS;
  128. return 0;
  129. }
  130. static int rcar_pcie_ep_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
  131. struct pci_epf_header *hdr)
  132. {
  133. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  134. struct rcar_pcie *pcie = &ep->pcie;
  135. u32 val;
  136. if (!fn)
  137. val = hdr->vendorid;
  138. else
  139. val = rcar_pci_read_reg(pcie, IDSETR0);
  140. val |= hdr->deviceid << 16;
  141. rcar_pci_write_reg(pcie, val, IDSETR0);
  142. val = hdr->revid;
  143. val |= hdr->progif_code << 8;
  144. val |= hdr->subclass_code << 16;
  145. val |= hdr->baseclass_code << 24;
  146. rcar_pci_write_reg(pcie, val, IDSETR1);
  147. if (!fn)
  148. val = hdr->subsys_vendor_id;
  149. else
  150. val = rcar_pci_read_reg(pcie, SUBIDSETR);
  151. val |= hdr->subsys_id << 16;
  152. rcar_pci_write_reg(pcie, val, SUBIDSETR);
  153. if (hdr->interrupt_pin > PCI_INTERRUPT_INTA)
  154. return -EINVAL;
  155. val = rcar_pci_read_reg(pcie, PCICONF(15));
  156. val |= (hdr->interrupt_pin << 8);
  157. rcar_pci_write_reg(pcie, val, PCICONF(15));
  158. return 0;
  159. }
  160. static int rcar_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  161. struct pci_epf_bar *epf_bar)
  162. {
  163. int flags = epf_bar->flags | LAR_ENABLE | LAM_64BIT;
  164. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  165. u64 size = 1ULL << fls64(epf_bar->size - 1);
  166. dma_addr_t cpu_addr = epf_bar->phys_addr;
  167. enum pci_barno bar = epf_bar->barno;
  168. struct rcar_pcie *pcie = &ep->pcie;
  169. u32 mask;
  170. int idx;
  171. int err;
  172. idx = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
  173. if (idx >= ep->num_ib_windows) {
  174. dev_err(pcie->dev, "no free inbound window\n");
  175. return -EINVAL;
  176. }
  177. if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
  178. flags |= IO_SPACE;
  179. ep->bar_to_atu[bar] = idx;
  180. /* use 64-bit BARs */
  181. set_bit(idx, ep->ib_window_map);
  182. set_bit(idx + 1, ep->ib_window_map);
  183. if (cpu_addr > 0) {
  184. unsigned long nr_zeros = __ffs64(cpu_addr);
  185. u64 alignment = 1ULL << nr_zeros;
  186. size = min(size, alignment);
  187. }
  188. size = min(size, 1ULL << 32);
  189. mask = roundup_pow_of_two(size) - 1;
  190. mask &= ~0xf;
  191. rcar_pcie_set_inbound(pcie, cpu_addr,
  192. 0x0, mask | flags, idx, false);
  193. err = rcar_pcie_wait_for_phyrdy(pcie);
  194. if (err) {
  195. dev_err(pcie->dev, "phy not ready\n");
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static void rcar_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
  201. struct pci_epf_bar *epf_bar)
  202. {
  203. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  204. enum pci_barno bar = epf_bar->barno;
  205. u32 atu_index = ep->bar_to_atu[bar];
  206. rcar_pcie_set_inbound(&ep->pcie, 0x0, 0x0, 0x0, bar, false);
  207. clear_bit(atu_index, ep->ib_window_map);
  208. clear_bit(atu_index + 1, ep->ib_window_map);
  209. }
  210. static int rcar_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 vfn,
  211. u8 interrupts)
  212. {
  213. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  214. struct rcar_pcie *pcie = &ep->pcie;
  215. u32 flags;
  216. flags = rcar_pci_read_reg(pcie, MSICAP(fn));
  217. flags |= interrupts << MSICAP0_MMESCAP_OFFSET;
  218. rcar_pci_write_reg(pcie, flags, MSICAP(fn));
  219. return 0;
  220. }
  221. static int rcar_pcie_ep_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
  222. {
  223. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  224. struct rcar_pcie *pcie = &ep->pcie;
  225. u32 flags;
  226. flags = rcar_pci_read_reg(pcie, MSICAP(fn));
  227. if (!(flags & MSICAP0_MSIE))
  228. return -EINVAL;
  229. return ((flags & MSICAP0_MMESE_MASK) >> MSICAP0_MMESE_OFFSET);
  230. }
  231. static int rcar_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
  232. phys_addr_t addr, u64 pci_addr, size_t size)
  233. {
  234. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  235. struct rcar_pcie *pcie = &ep->pcie;
  236. struct resource_entry win;
  237. struct resource res;
  238. int window;
  239. int err;
  240. /* check if we have a link. */
  241. err = rcar_pcie_wait_for_dl(pcie);
  242. if (err) {
  243. dev_err(pcie->dev, "link not up\n");
  244. return err;
  245. }
  246. window = rcar_pcie_ep_get_window(ep, addr);
  247. if (window < 0) {
  248. dev_err(pcie->dev, "failed to get corresponding window\n");
  249. return -EINVAL;
  250. }
  251. memset(&win, 0x0, sizeof(win));
  252. memset(&res, 0x0, sizeof(res));
  253. res.start = pci_addr;
  254. res.end = pci_addr + size - 1;
  255. res.flags = IORESOURCE_MEM;
  256. win.res = &res;
  257. rcar_pcie_set_outbound(pcie, window, &win);
  258. ep->ob_mapped_addr[window] = addr;
  259. return 0;
  260. }
  261. static void rcar_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn, u8 vfn,
  262. phys_addr_t addr)
  263. {
  264. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  265. struct resource_entry win;
  266. struct resource res;
  267. int idx;
  268. for (idx = 0; idx < ep->num_ob_windows; idx++)
  269. if (ep->ob_mapped_addr[idx] == addr)
  270. break;
  271. if (idx >= ep->num_ob_windows)
  272. return;
  273. memset(&win, 0x0, sizeof(win));
  274. memset(&res, 0x0, sizeof(res));
  275. win.res = &res;
  276. rcar_pcie_set_outbound(&ep->pcie, idx, &win);
  277. ep->ob_mapped_addr[idx] = 0;
  278. }
  279. static int rcar_pcie_ep_assert_intx(struct rcar_pcie_endpoint *ep,
  280. u8 fn, u8 intx)
  281. {
  282. struct rcar_pcie *pcie = &ep->pcie;
  283. u32 val;
  284. val = rcar_pci_read_reg(pcie, PCIEMSITXR);
  285. if ((val & PCI_MSI_FLAGS_ENABLE)) {
  286. dev_err(pcie->dev, "MSI is enabled, cannot assert INTx\n");
  287. return -EINVAL;
  288. }
  289. val = rcar_pci_read_reg(pcie, PCICONF(1));
  290. if ((val & INTDIS)) {
  291. dev_err(pcie->dev, "INTx message transmission is disabled\n");
  292. return -EINVAL;
  293. }
  294. val = rcar_pci_read_reg(pcie, PCIEINTXR);
  295. if ((val & ASTINTX)) {
  296. dev_err(pcie->dev, "INTx is already asserted\n");
  297. return -EINVAL;
  298. }
  299. val |= ASTINTX;
  300. rcar_pci_write_reg(pcie, val, PCIEINTXR);
  301. usleep_range(1000, 1001);
  302. val = rcar_pci_read_reg(pcie, PCIEINTXR);
  303. val &= ~ASTINTX;
  304. rcar_pci_write_reg(pcie, val, PCIEINTXR);
  305. return 0;
  306. }
  307. static int rcar_pcie_ep_assert_msi(struct rcar_pcie *pcie,
  308. u8 fn, u8 interrupt_num)
  309. {
  310. u16 msi_count;
  311. u32 val;
  312. /* Check MSI enable bit */
  313. val = rcar_pci_read_reg(pcie, MSICAP(fn));
  314. if (!(val & MSICAP0_MSIE))
  315. return -EINVAL;
  316. /* Get MSI numbers from MME */
  317. msi_count = ((val & MSICAP0_MMESE_MASK) >> MSICAP0_MMESE_OFFSET);
  318. msi_count = 1 << msi_count;
  319. if (!interrupt_num || interrupt_num > msi_count)
  320. return -EINVAL;
  321. val = rcar_pci_read_reg(pcie, PCIEMSITXR);
  322. rcar_pci_write_reg(pcie, val | (interrupt_num - 1), PCIEMSITXR);
  323. return 0;
  324. }
  325. static int rcar_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
  326. enum pci_epc_irq_type type,
  327. u16 interrupt_num)
  328. {
  329. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  330. switch (type) {
  331. case PCI_EPC_IRQ_LEGACY:
  332. return rcar_pcie_ep_assert_intx(ep, fn, 0);
  333. case PCI_EPC_IRQ_MSI:
  334. return rcar_pcie_ep_assert_msi(&ep->pcie, fn, interrupt_num);
  335. default:
  336. return -EINVAL;
  337. }
  338. }
  339. static int rcar_pcie_ep_start(struct pci_epc *epc)
  340. {
  341. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  342. rcar_pci_write_reg(&ep->pcie, MACCTLR_INIT_VAL, MACCTLR);
  343. rcar_pci_write_reg(&ep->pcie, CFINIT, PCIETCTLR);
  344. return 0;
  345. }
  346. static void rcar_pcie_ep_stop(struct pci_epc *epc)
  347. {
  348. struct rcar_pcie_endpoint *ep = epc_get_drvdata(epc);
  349. rcar_pci_write_reg(&ep->pcie, 0, PCIETCTLR);
  350. }
  351. static const struct pci_epc_features rcar_pcie_epc_features = {
  352. .linkup_notifier = false,
  353. .msi_capable = true,
  354. .msix_capable = false,
  355. /* use 64-bit BARs so mark BAR[1,3,5] as reserved */
  356. .reserved_bar = 1 << BAR_1 | 1 << BAR_3 | 1 << BAR_5,
  357. .bar_fixed_64bit = 1 << BAR_0 | 1 << BAR_2 | 1 << BAR_4,
  358. .bar_fixed_size[0] = 128,
  359. .bar_fixed_size[2] = 256,
  360. .bar_fixed_size[4] = 256,
  361. };
  362. static const struct pci_epc_features*
  363. rcar_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
  364. {
  365. return &rcar_pcie_epc_features;
  366. }
  367. static const struct pci_epc_ops rcar_pcie_epc_ops = {
  368. .write_header = rcar_pcie_ep_write_header,
  369. .set_bar = rcar_pcie_ep_set_bar,
  370. .clear_bar = rcar_pcie_ep_clear_bar,
  371. .set_msi = rcar_pcie_ep_set_msi,
  372. .get_msi = rcar_pcie_ep_get_msi,
  373. .map_addr = rcar_pcie_ep_map_addr,
  374. .unmap_addr = rcar_pcie_ep_unmap_addr,
  375. .raise_irq = rcar_pcie_ep_raise_irq,
  376. .start = rcar_pcie_ep_start,
  377. .stop = rcar_pcie_ep_stop,
  378. .get_features = rcar_pcie_ep_get_features,
  379. };
  380. static const struct of_device_id rcar_pcie_ep_of_match[] = {
  381. { .compatible = "renesas,r8a774c0-pcie-ep", },
  382. { .compatible = "renesas,rcar-gen3-pcie-ep" },
  383. { },
  384. };
  385. static int rcar_pcie_ep_probe(struct platform_device *pdev)
  386. {
  387. struct device *dev = &pdev->dev;
  388. struct rcar_pcie_endpoint *ep;
  389. struct rcar_pcie *pcie;
  390. struct pci_epc *epc;
  391. int err;
  392. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  393. if (!ep)
  394. return -ENOMEM;
  395. pcie = &ep->pcie;
  396. pcie->dev = dev;
  397. pm_runtime_enable(dev);
  398. err = pm_runtime_resume_and_get(dev);
  399. if (err < 0) {
  400. dev_err(dev, "pm_runtime_resume_and_get failed\n");
  401. goto err_pm_disable;
  402. }
  403. err = rcar_pcie_ep_get_pdata(ep, pdev);
  404. if (err < 0) {
  405. dev_err(dev, "failed to request resources: %d\n", err);
  406. goto err_pm_put;
  407. }
  408. ep->num_ib_windows = MAX_NR_INBOUND_MAPS;
  409. ep->ib_window_map =
  410. devm_kcalloc(dev, BITS_TO_LONGS(ep->num_ib_windows),
  411. sizeof(long), GFP_KERNEL);
  412. if (!ep->ib_window_map) {
  413. err = -ENOMEM;
  414. dev_err(dev, "failed to allocate memory for inbound map\n");
  415. goto err_pm_put;
  416. }
  417. ep->ob_mapped_addr = devm_kcalloc(dev, ep->num_ob_windows,
  418. sizeof(*ep->ob_mapped_addr),
  419. GFP_KERNEL);
  420. if (!ep->ob_mapped_addr) {
  421. err = -ENOMEM;
  422. dev_err(dev, "failed to allocate memory for outbound memory pointers\n");
  423. goto err_pm_put;
  424. }
  425. epc = devm_pci_epc_create(dev, &rcar_pcie_epc_ops);
  426. if (IS_ERR(epc)) {
  427. dev_err(dev, "failed to create epc device\n");
  428. err = PTR_ERR(epc);
  429. goto err_pm_put;
  430. }
  431. epc->max_functions = ep->max_functions;
  432. epc_set_drvdata(epc, ep);
  433. rcar_pcie_ep_hw_init(pcie);
  434. err = pci_epc_multi_mem_init(epc, ep->ob_window, ep->num_ob_windows);
  435. if (err < 0) {
  436. dev_err(dev, "failed to initialize the epc memory space\n");
  437. goto err_pm_put;
  438. }
  439. return 0;
  440. err_pm_put:
  441. pm_runtime_put(dev);
  442. err_pm_disable:
  443. pm_runtime_disable(dev);
  444. return err;
  445. }
  446. static struct platform_driver rcar_pcie_ep_driver = {
  447. .driver = {
  448. .name = "rcar-pcie-ep",
  449. .of_match_table = rcar_pcie_ep_of_match,
  450. .suppress_bind_attrs = true,
  451. },
  452. .probe = rcar_pcie_ep_probe,
  453. };
  454. builtin_platform_driver(rcar_pcie_ep_driver);