pcie-cadence-ep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017 Cadence
  3. // Cadence PCIe endpoint controller driver.
  4. // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
  5. #include <linux/delay.h>
  6. #include <linux/kernel.h>
  7. #include <linux/of.h>
  8. #include <linux/pci-epc.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/sizes.h>
  12. #include "pcie-cadence.h"
  13. #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
  14. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
  15. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
  16. /**
  17. * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
  18. * @pcie: Cadence PCIe controller
  19. * @max_regions: maximum number of regions supported by hardware
  20. * @ob_region_map: bitmask of mapped outbound regions
  21. * @ob_addr: base addresses in the AXI bus where the outbound regions start
  22. * @irq_phys_addr: base address on the AXI bus where the MSI/legacy IRQ
  23. * dedicated outbound regions is mapped.
  24. * @irq_cpu_addr: base address in the CPU space where a write access triggers
  25. * the sending of a memory write (MSI) / normal message (legacy
  26. * IRQ) TLP through the PCIe bus.
  27. * @irq_pci_addr: used to save the current mapping of the MSI/legacy IRQ
  28. * dedicated outbound region.
  29. * @irq_pci_fn: the latest PCI function that has updated the mapping of
  30. * the MSI/legacy IRQ dedicated outbound region.
  31. * @irq_pending: bitmask of asserted legacy IRQs.
  32. */
  33. struct cdns_pcie_ep {
  34. struct cdns_pcie pcie;
  35. u32 max_regions;
  36. unsigned long ob_region_map;
  37. phys_addr_t *ob_addr;
  38. phys_addr_t irq_phys_addr;
  39. void __iomem *irq_cpu_addr;
  40. u64 irq_pci_addr;
  41. u8 irq_pci_fn;
  42. u8 irq_pending;
  43. };
  44. static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn,
  45. struct pci_epf_header *hdr)
  46. {
  47. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  48. struct cdns_pcie *pcie = &ep->pcie;
  49. cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
  50. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
  51. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
  52. cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
  53. hdr->subclass_code | hdr->baseclass_code << 8);
  54. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
  55. hdr->cache_line_size);
  56. cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
  57. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
  58. /*
  59. * Vendor ID can only be modified from function 0, all other functions
  60. * use the same vendor ID as function 0.
  61. */
  62. if (fn == 0) {
  63. /* Update the vendor IDs. */
  64. u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
  65. CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
  66. cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
  67. }
  68. return 0;
  69. }
  70. static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn,
  71. struct pci_epf_bar *epf_bar)
  72. {
  73. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  74. struct cdns_pcie *pcie = &ep->pcie;
  75. dma_addr_t bar_phys = epf_bar->phys_addr;
  76. enum pci_barno bar = epf_bar->barno;
  77. int flags = epf_bar->flags;
  78. u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
  79. u64 sz;
  80. /* BAR size is 2^(aperture + 7) */
  81. sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
  82. /*
  83. * roundup_pow_of_two() returns an unsigned long, which is not suited
  84. * for 64bit values.
  85. */
  86. sz = 1ULL << fls64(sz - 1);
  87. aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
  88. if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  89. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
  90. } else {
  91. bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
  92. bool is_64bits = sz > SZ_2G;
  93. if (is_64bits && (bar & 1))
  94. return -EINVAL;
  95. if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
  96. epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  97. if (is_64bits && is_prefetch)
  98. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
  99. else if (is_prefetch)
  100. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
  101. else if (is_64bits)
  102. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
  103. else
  104. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
  105. }
  106. addr0 = lower_32_bits(bar_phys);
  107. addr1 = upper_32_bits(bar_phys);
  108. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
  109. addr0);
  110. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
  111. addr1);
  112. if (bar < BAR_4) {
  113. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  114. b = bar;
  115. } else {
  116. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  117. b = bar - BAR_4;
  118. }
  119. cfg = cdns_pcie_readl(pcie, reg);
  120. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  121. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  122. cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
  123. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
  124. cdns_pcie_writel(pcie, reg, cfg);
  125. return 0;
  126. }
  127. static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn,
  128. struct pci_epf_bar *epf_bar)
  129. {
  130. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  131. struct cdns_pcie *pcie = &ep->pcie;
  132. enum pci_barno bar = epf_bar->barno;
  133. u32 reg, cfg, b, ctrl;
  134. if (bar < BAR_4) {
  135. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  136. b = bar;
  137. } else {
  138. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  139. b = bar - BAR_4;
  140. }
  141. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
  142. cfg = cdns_pcie_readl(pcie, reg);
  143. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  144. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  145. cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
  146. cdns_pcie_writel(pcie, reg, cfg);
  147. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
  148. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
  149. }
  150. static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, phys_addr_t addr,
  151. u64 pci_addr, size_t size)
  152. {
  153. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  154. struct cdns_pcie *pcie = &ep->pcie;
  155. u32 r;
  156. r = find_first_zero_bit(&ep->ob_region_map,
  157. sizeof(ep->ob_region_map) * BITS_PER_LONG);
  158. if (r >= ep->max_regions - 1) {
  159. dev_err(&epc->dev, "no free outbound region\n");
  160. return -EINVAL;
  161. }
  162. cdns_pcie_set_outbound_region(pcie, fn, r, false, addr, pci_addr, size);
  163. set_bit(r, &ep->ob_region_map);
  164. ep->ob_addr[r] = addr;
  165. return 0;
  166. }
  167. static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn,
  168. phys_addr_t addr)
  169. {
  170. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  171. struct cdns_pcie *pcie = &ep->pcie;
  172. u32 r;
  173. for (r = 0; r < ep->max_regions - 1; r++)
  174. if (ep->ob_addr[r] == addr)
  175. break;
  176. if (r == ep->max_regions - 1)
  177. return;
  178. cdns_pcie_reset_outbound_region(pcie, r);
  179. ep->ob_addr[r] = 0;
  180. clear_bit(r, &ep->ob_region_map);
  181. }
  182. static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 mmc)
  183. {
  184. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  185. struct cdns_pcie *pcie = &ep->pcie;
  186. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  187. u16 flags;
  188. /*
  189. * Set the Multiple Message Capable bitfield into the Message Control
  190. * register.
  191. */
  192. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  193. flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
  194. flags |= PCI_MSI_FLAGS_64BIT;
  195. flags &= ~PCI_MSI_FLAGS_MASKBIT;
  196. cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
  197. return 0;
  198. }
  199. static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn)
  200. {
  201. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  202. struct cdns_pcie *pcie = &ep->pcie;
  203. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  204. u16 flags, mme;
  205. /* Validate that the MSI feature is actually enabled. */
  206. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  207. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  208. return -EINVAL;
  209. /*
  210. * Get the Multiple Message Enable bitfield from the Message Control
  211. * register.
  212. */
  213. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  214. return mme;
  215. }
  216. static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn,
  217. u8 intx, bool is_asserted)
  218. {
  219. struct cdns_pcie *pcie = &ep->pcie;
  220. u32 r = ep->max_regions - 1;
  221. u32 offset;
  222. u16 status;
  223. u8 msg_code;
  224. intx &= 3;
  225. /* Set the outbound region if needed. */
  226. if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
  227. ep->irq_pci_fn != fn)) {
  228. /* Last region was reserved for IRQ writes. */
  229. cdns_pcie_set_outbound_region_for_normal_msg(pcie, fn, r,
  230. ep->irq_phys_addr);
  231. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
  232. ep->irq_pci_fn = fn;
  233. }
  234. if (is_asserted) {
  235. ep->irq_pending |= BIT(intx);
  236. msg_code = MSG_CODE_ASSERT_INTA + intx;
  237. } else {
  238. ep->irq_pending &= ~BIT(intx);
  239. msg_code = MSG_CODE_DEASSERT_INTA + intx;
  240. }
  241. status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
  242. if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
  243. status ^= PCI_STATUS_INTERRUPT;
  244. cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
  245. }
  246. offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
  247. CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
  248. CDNS_PCIE_MSG_NO_DATA;
  249. writel(0, ep->irq_cpu_addr + offset);
  250. }
  251. static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
  252. {
  253. u16 cmd;
  254. cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
  255. if (cmd & PCI_COMMAND_INTX_DISABLE)
  256. return -EINVAL;
  257. cdns_pcie_ep_assert_intx(ep, fn, intx, true);
  258. /*
  259. * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
  260. * from drivers/pci/dwc/pci-dra7xx.c
  261. */
  262. mdelay(1);
  263. cdns_pcie_ep_assert_intx(ep, fn, intx, false);
  264. return 0;
  265. }
  266. static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn,
  267. u8 interrupt_num)
  268. {
  269. struct cdns_pcie *pcie = &ep->pcie;
  270. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  271. u16 flags, mme, data, data_mask;
  272. u8 msi_count;
  273. u64 pci_addr, pci_addr_mask = 0xff;
  274. /* Check whether the MSI feature has been enabled by the PCI host. */
  275. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  276. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  277. return -EINVAL;
  278. /* Get the number of enabled MSIs */
  279. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  280. msi_count = 1 << mme;
  281. if (!interrupt_num || interrupt_num > msi_count)
  282. return -EINVAL;
  283. /* Compute the data value to be written. */
  284. data_mask = msi_count - 1;
  285. data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
  286. data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
  287. /* Get the PCI address where to write the data into. */
  288. pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
  289. pci_addr <<= 32;
  290. pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
  291. pci_addr &= GENMASK_ULL(63, 2);
  292. /* Set the outbound region if needed. */
  293. if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
  294. ep->irq_pci_fn != fn)) {
  295. /* Last region was reserved for IRQ writes. */
  296. cdns_pcie_set_outbound_region(pcie, fn, ep->max_regions - 1,
  297. false,
  298. ep->irq_phys_addr,
  299. pci_addr & ~pci_addr_mask,
  300. pci_addr_mask + 1);
  301. ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
  302. ep->irq_pci_fn = fn;
  303. }
  304. writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
  305. return 0;
  306. }
  307. static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn,
  308. enum pci_epc_irq_type type,
  309. u16 interrupt_num)
  310. {
  311. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  312. switch (type) {
  313. case PCI_EPC_IRQ_LEGACY:
  314. return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
  315. case PCI_EPC_IRQ_MSI:
  316. return cdns_pcie_ep_send_msi_irq(ep, fn, interrupt_num);
  317. default:
  318. break;
  319. }
  320. return -EINVAL;
  321. }
  322. static int cdns_pcie_ep_start(struct pci_epc *epc)
  323. {
  324. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  325. struct cdns_pcie *pcie = &ep->pcie;
  326. struct pci_epf *epf;
  327. u32 cfg;
  328. /*
  329. * BIT(0) is hardwired to 1, hence function 0 is always enabled
  330. * and can't be disabled anyway.
  331. */
  332. cfg = BIT(0);
  333. list_for_each_entry(epf, &epc->pci_epf, list)
  334. cfg |= BIT(epf->func_no);
  335. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
  336. /*
  337. * The PCIe links are automatically established by the controller
  338. * once for all at powerup: the software can neither start nor stop
  339. * those links later at runtime.
  340. *
  341. * Then we only have to notify the EP core that our links are already
  342. * established. However we don't call directly pci_epc_linkup() because
  343. * we've already locked the epc->lock.
  344. */
  345. list_for_each_entry(epf, &epc->pci_epf, list)
  346. pci_epf_linkup(epf);
  347. return 0;
  348. }
  349. static const struct pci_epc_ops cdns_pcie_epc_ops = {
  350. .write_header = cdns_pcie_ep_write_header,
  351. .set_bar = cdns_pcie_ep_set_bar,
  352. .clear_bar = cdns_pcie_ep_clear_bar,
  353. .map_addr = cdns_pcie_ep_map_addr,
  354. .unmap_addr = cdns_pcie_ep_unmap_addr,
  355. .set_msi = cdns_pcie_ep_set_msi,
  356. .get_msi = cdns_pcie_ep_get_msi,
  357. .raise_irq = cdns_pcie_ep_raise_irq,
  358. .start = cdns_pcie_ep_start,
  359. };
  360. static const struct of_device_id cdns_pcie_ep_of_match[] = {
  361. { .compatible = "cdns,cdns-pcie-ep" },
  362. { },
  363. };
  364. static int cdns_pcie_ep_probe(struct platform_device *pdev)
  365. {
  366. struct device *dev = &pdev->dev;
  367. struct device_node *np = dev->of_node;
  368. struct cdns_pcie_ep *ep;
  369. struct cdns_pcie *pcie;
  370. struct pci_epc *epc;
  371. struct resource *res;
  372. int ret;
  373. int phy_count;
  374. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  375. if (!ep)
  376. return -ENOMEM;
  377. pcie = &ep->pcie;
  378. pcie->is_rc = false;
  379. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  380. pcie->reg_base = devm_ioremap_resource(dev, res);
  381. if (IS_ERR(pcie->reg_base)) {
  382. dev_err(dev, "missing \"reg\"\n");
  383. return PTR_ERR(pcie->reg_base);
  384. }
  385. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  386. if (!res) {
  387. dev_err(dev, "missing \"mem\"\n");
  388. return -EINVAL;
  389. }
  390. pcie->mem_res = res;
  391. ret = of_property_read_u32(np, "cdns,max-outbound-regions",
  392. &ep->max_regions);
  393. if (ret < 0) {
  394. dev_err(dev, "missing \"cdns,max-outbound-regions\"\n");
  395. return ret;
  396. }
  397. ep->ob_addr = devm_kcalloc(dev,
  398. ep->max_regions, sizeof(*ep->ob_addr),
  399. GFP_KERNEL);
  400. if (!ep->ob_addr)
  401. return -ENOMEM;
  402. ret = cdns_pcie_init_phy(dev, pcie);
  403. if (ret) {
  404. dev_err(dev, "failed to init phy\n");
  405. return ret;
  406. }
  407. platform_set_drvdata(pdev, pcie);
  408. pm_runtime_enable(dev);
  409. ret = pm_runtime_get_sync(dev);
  410. if (ret < 0) {
  411. dev_err(dev, "pm_runtime_get_sync() failed\n");
  412. goto err_get_sync;
  413. }
  414. /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
  415. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
  416. epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
  417. if (IS_ERR(epc)) {
  418. dev_err(dev, "failed to create epc device\n");
  419. ret = PTR_ERR(epc);
  420. goto err_init;
  421. }
  422. epc_set_drvdata(epc, ep);
  423. if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
  424. epc->max_functions = 1;
  425. ret = pci_epc_mem_init(epc, pcie->mem_res->start,
  426. resource_size(pcie->mem_res));
  427. if (ret < 0) {
  428. dev_err(dev, "failed to initialize the memory space\n");
  429. goto err_init;
  430. }
  431. ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
  432. SZ_128K);
  433. if (!ep->irq_cpu_addr) {
  434. dev_err(dev, "failed to reserve memory space for MSI\n");
  435. ret = -ENOMEM;
  436. goto free_epc_mem;
  437. }
  438. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
  439. return 0;
  440. free_epc_mem:
  441. pci_epc_mem_exit(epc);
  442. err_init:
  443. pm_runtime_put_sync(dev);
  444. err_get_sync:
  445. pm_runtime_disable(dev);
  446. cdns_pcie_disable_phy(pcie);
  447. phy_count = pcie->phy_count;
  448. while (phy_count--)
  449. device_link_del(pcie->link[phy_count]);
  450. return ret;
  451. }
  452. static void cdns_pcie_ep_shutdown(struct platform_device *pdev)
  453. {
  454. struct device *dev = &pdev->dev;
  455. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  456. int ret;
  457. ret = pm_runtime_put_sync(dev);
  458. if (ret < 0)
  459. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  460. pm_runtime_disable(dev);
  461. cdns_pcie_disable_phy(pcie);
  462. }
  463. static struct platform_driver cdns_pcie_ep_driver = {
  464. .driver = {
  465. .name = "cdns-pcie-ep",
  466. .of_match_table = cdns_pcie_ep_of_match,
  467. .pm = &cdns_pcie_pm_ops,
  468. },
  469. .probe = cdns_pcie_ep_probe,
  470. .shutdown = cdns_pcie_ep_shutdown,
  471. };
  472. builtin_platform_driver(cdns_pcie_ep_driver);