pcie-cadence-ep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 offset;
  221. u16 status;
  222. u8 msg_code;
  223. intx &= 3;
  224. /* Set the outbound region if needed. */
  225. if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
  226. ep->irq_pci_fn != fn)) {
  227. /* First region was reserved for IRQ writes. */
  228. cdns_pcie_set_outbound_region_for_normal_msg(pcie, fn, 0,
  229. ep->irq_phys_addr);
  230. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
  231. ep->irq_pci_fn = fn;
  232. }
  233. if (is_asserted) {
  234. ep->irq_pending |= BIT(intx);
  235. msg_code = MSG_CODE_ASSERT_INTA + intx;
  236. } else {
  237. ep->irq_pending &= ~BIT(intx);
  238. msg_code = MSG_CODE_DEASSERT_INTA + intx;
  239. }
  240. status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
  241. if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
  242. status ^= PCI_STATUS_INTERRUPT;
  243. cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
  244. }
  245. offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
  246. CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
  247. CDNS_PCIE_MSG_NO_DATA;
  248. writel(0, ep->irq_cpu_addr + offset);
  249. }
  250. static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 intx)
  251. {
  252. u16 cmd;
  253. cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
  254. if (cmd & PCI_COMMAND_INTX_DISABLE)
  255. return -EINVAL;
  256. cdns_pcie_ep_assert_intx(ep, fn, intx, true);
  257. /*
  258. * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
  259. * from drivers/pci/dwc/pci-dra7xx.c
  260. */
  261. mdelay(1);
  262. cdns_pcie_ep_assert_intx(ep, fn, intx, false);
  263. return 0;
  264. }
  265. static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn,
  266. u8 interrupt_num)
  267. {
  268. struct cdns_pcie *pcie = &ep->pcie;
  269. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  270. u16 flags, mme, data, data_mask;
  271. u8 msi_count;
  272. u64 pci_addr, pci_addr_mask = 0xff;
  273. /* Check whether the MSI feature has been enabled by the PCI host. */
  274. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  275. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  276. return -EINVAL;
  277. /* Get the number of enabled MSIs */
  278. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  279. msi_count = 1 << mme;
  280. if (!interrupt_num || interrupt_num > msi_count)
  281. return -EINVAL;
  282. /* Compute the data value to be written. */
  283. data_mask = msi_count - 1;
  284. data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
  285. data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
  286. /* Get the PCI address where to write the data into. */
  287. pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
  288. pci_addr <<= 32;
  289. pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
  290. pci_addr &= GENMASK_ULL(63, 2);
  291. /* Set the outbound region if needed. */
  292. if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
  293. ep->irq_pci_fn != fn)) {
  294. /* First region was reserved for IRQ writes. */
  295. cdns_pcie_set_outbound_region(pcie, fn, 0,
  296. false,
  297. ep->irq_phys_addr,
  298. pci_addr & ~pci_addr_mask,
  299. pci_addr_mask + 1);
  300. ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
  301. ep->irq_pci_fn = fn;
  302. }
  303. writel(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
  304. return 0;
  305. }
  306. static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn,
  307. enum pci_epc_irq_type type,
  308. u16 interrupt_num)
  309. {
  310. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  311. switch (type) {
  312. case PCI_EPC_IRQ_LEGACY:
  313. return cdns_pcie_ep_send_legacy_irq(ep, fn, 0);
  314. case PCI_EPC_IRQ_MSI:
  315. return cdns_pcie_ep_send_msi_irq(ep, fn, interrupt_num);
  316. default:
  317. break;
  318. }
  319. return -EINVAL;
  320. }
  321. static int cdns_pcie_ep_start(struct pci_epc *epc)
  322. {
  323. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  324. struct cdns_pcie *pcie = &ep->pcie;
  325. struct pci_epf *epf;
  326. u32 cfg;
  327. /*
  328. * BIT(0) is hardwired to 1, hence function 0 is always enabled
  329. * and can't be disabled anyway.
  330. */
  331. cfg = BIT(0);
  332. list_for_each_entry(epf, &epc->pci_epf, list)
  333. cfg |= BIT(epf->func_no);
  334. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
  335. /*
  336. * The PCIe links are automatically established by the controller
  337. * once for all at powerup: the software can neither start nor stop
  338. * those links later at runtime.
  339. *
  340. * Then we only have to notify the EP core that our links are already
  341. * established. However we don't call directly pci_epc_linkup() because
  342. * we've already locked the epc->lock.
  343. */
  344. list_for_each_entry(epf, &epc->pci_epf, list)
  345. pci_epf_linkup(epf);
  346. return 0;
  347. }
  348. static const struct pci_epc_ops cdns_pcie_epc_ops = {
  349. .write_header = cdns_pcie_ep_write_header,
  350. .set_bar = cdns_pcie_ep_set_bar,
  351. .clear_bar = cdns_pcie_ep_clear_bar,
  352. .map_addr = cdns_pcie_ep_map_addr,
  353. .unmap_addr = cdns_pcie_ep_unmap_addr,
  354. .set_msi = cdns_pcie_ep_set_msi,
  355. .get_msi = cdns_pcie_ep_get_msi,
  356. .raise_irq = cdns_pcie_ep_raise_irq,
  357. .start = cdns_pcie_ep_start,
  358. };
  359. static const struct of_device_id cdns_pcie_ep_of_match[] = {
  360. { .compatible = "cdns,cdns-pcie-ep" },
  361. { },
  362. };
  363. static int cdns_pcie_ep_probe(struct platform_device *pdev)
  364. {
  365. struct device *dev = &pdev->dev;
  366. struct device_node *np = dev->of_node;
  367. struct cdns_pcie_ep *ep;
  368. struct cdns_pcie *pcie;
  369. struct pci_epc *epc;
  370. struct resource *res;
  371. int ret;
  372. int phy_count;
  373. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  374. if (!ep)
  375. return -ENOMEM;
  376. pcie = &ep->pcie;
  377. pcie->is_rc = false;
  378. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  379. pcie->reg_base = devm_ioremap_resource(dev, res);
  380. if (IS_ERR(pcie->reg_base)) {
  381. dev_err(dev, "missing \"reg\"\n");
  382. return PTR_ERR(pcie->reg_base);
  383. }
  384. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  385. if (!res) {
  386. dev_err(dev, "missing \"mem\"\n");
  387. return -EINVAL;
  388. }
  389. pcie->mem_res = res;
  390. ret = of_property_read_u32(np, "cdns,max-outbound-regions",
  391. &ep->max_regions);
  392. if (ret < 0) {
  393. dev_err(dev, "missing \"cdns,max-outbound-regions\"\n");
  394. return ret;
  395. }
  396. ep->ob_addr = devm_kcalloc(dev,
  397. ep->max_regions, sizeof(*ep->ob_addr),
  398. GFP_KERNEL);
  399. if (!ep->ob_addr)
  400. return -ENOMEM;
  401. ret = cdns_pcie_init_phy(dev, pcie);
  402. if (ret) {
  403. dev_err(dev, "failed to init phy\n");
  404. return ret;
  405. }
  406. platform_set_drvdata(pdev, pcie);
  407. pm_runtime_enable(dev);
  408. ret = pm_runtime_get_sync(dev);
  409. if (ret < 0) {
  410. dev_err(dev, "pm_runtime_get_sync() failed\n");
  411. goto err_get_sync;
  412. }
  413. /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
  414. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
  415. epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
  416. if (IS_ERR(epc)) {
  417. dev_err(dev, "failed to create epc device\n");
  418. ret = PTR_ERR(epc);
  419. goto err_init;
  420. }
  421. epc_set_drvdata(epc, ep);
  422. if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
  423. epc->max_functions = 1;
  424. ret = pci_epc_mem_init(epc, pcie->mem_res->start,
  425. resource_size(pcie->mem_res));
  426. if (ret < 0) {
  427. dev_err(dev, "failed to initialize the memory space\n");
  428. goto err_init;
  429. }
  430. ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
  431. SZ_128K);
  432. if (!ep->irq_cpu_addr) {
  433. dev_err(dev, "failed to reserve memory space for MSI\n");
  434. ret = -ENOMEM;
  435. goto free_epc_mem;
  436. }
  437. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
  438. /* Reserve region 0 for IRQs */
  439. set_bit(0, &ep->ob_region_map);
  440. return 0;
  441. free_epc_mem:
  442. pci_epc_mem_exit(epc);
  443. err_init:
  444. pm_runtime_put_sync(dev);
  445. err_get_sync:
  446. pm_runtime_disable(dev);
  447. cdns_pcie_disable_phy(pcie);
  448. phy_count = pcie->phy_count;
  449. while (phy_count--)
  450. device_link_del(pcie->link[phy_count]);
  451. return ret;
  452. }
  453. static void cdns_pcie_ep_shutdown(struct platform_device *pdev)
  454. {
  455. struct device *dev = &pdev->dev;
  456. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  457. int ret;
  458. ret = pm_runtime_put_sync(dev);
  459. if (ret < 0)
  460. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  461. pm_runtime_disable(dev);
  462. cdns_pcie_disable_phy(pcie);
  463. }
  464. static struct platform_driver cdns_pcie_ep_driver = {
  465. .driver = {
  466. .name = "cdns-pcie-ep",
  467. .of_match_table = cdns_pcie_ep_of_match,
  468. .pm = &cdns_pcie_pm_ops,
  469. },
  470. .probe = cdns_pcie_ep_probe,
  471. .shutdown = cdns_pcie_ep_shutdown,
  472. };
  473. builtin_platform_driver(cdns_pcie_ep_driver);