pcie-cadence-ep.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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/of_device.h>
  9. #include <linux/pci-epc.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/sizes.h>
  13. #include "pcie-cadence.h"
  14. #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */
  15. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE 0x1
  16. #define CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY 0x3
  17. /**
  18. * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
  19. * @pcie: Cadence PCIe controller
  20. * @dev: pointer to PCIe EP device
  21. * @max_regions: maximum number of regions supported by hardware
  22. * @ob_region_map: bitmask of mapped outbound regions
  23. * @ob_addr: base addresses in the AXI bus where the outbound regions start
  24. * @irq_phys_addr: base address on the AXI bus where the MSI/legacy IRQ
  25. * dedicated outbound regions is mapped.
  26. * @irq_cpu_addr: base address in the CPU space where a write access triggers
  27. * the sending of a memory write (MSI) / normal message (legacy
  28. * IRQ) TLP through the PCIe bus.
  29. * @irq_pci_addr: used to save the current mapping of the MSI/legacy IRQ
  30. * dedicated outbound region.
  31. * @irq_pci_fn: the latest PCI function that has updated the mapping of
  32. * the MSI/legacy IRQ dedicated outbound region.
  33. * @irq_pending: bitmask of asserted legacy IRQs.
  34. */
  35. struct cdns_pcie_ep {
  36. struct cdns_pcie pcie;
  37. struct device *dev;
  38. u32 max_regions;
  39. unsigned long ob_region_map;
  40. phys_addr_t *ob_addr;
  41. phys_addr_t irq_phys_addr;
  42. void __iomem *irq_cpu_addr;
  43. u64 irq_pci_addr;
  44. u8 irq_pci_fn;
  45. u8 irq_pending;
  46. };
  47. static int cdns_pcie_ep_epf_init(struct pci_epc *epc, struct pci_epf *epf)
  48. {
  49. return pci_epf_init_dma_chan(epf);
  50. }
  51. static void cdns_pcie_ep_epf_exit(struct pci_epc *epc, struct pci_epf *epf)
  52. {
  53. pci_epf_clean_dma_chan(epf);
  54. }
  55. static int cdns_pcie_ep_data_transfer(struct pci_epc *epc, struct pci_epf *epf,
  56. dma_addr_t dma_dst, dma_addr_t dma_src,
  57. size_t len)
  58. {
  59. return pci_epf_data_transfer(epf, dma_dst, dma_src, len);
  60. }
  61. static int cdns_pcie_ep_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
  62. struct pci_epf_header *hdr)
  63. {
  64. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  65. u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  66. struct cdns_pcie *pcie = &ep->pcie;
  67. u32 reg;
  68. if (vfn > 1) {
  69. dev_dbg(&epc->dev, "Only Virtual Function #1 has deviceID\n");
  70. return 0;
  71. } else if (vfn == 1) {
  72. reg = cap + PCI_SRIOV_VF_DID;
  73. cdns_pcie_ep_fn_writew(pcie, fn, reg, hdr->deviceid);
  74. return 0;
  75. }
  76. cdns_pcie_ep_fn_writew(pcie, fn, PCI_DEVICE_ID, hdr->deviceid);
  77. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_REVISION_ID, hdr->revid);
  78. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CLASS_PROG, hdr->progif_code);
  79. cdns_pcie_ep_fn_writew(pcie, fn, PCI_CLASS_DEVICE,
  80. hdr->subclass_code | hdr->baseclass_code << 8);
  81. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_CACHE_LINE_SIZE,
  82. hdr->cache_line_size);
  83. cdns_pcie_ep_fn_writew(pcie, fn, PCI_SUBSYSTEM_ID, hdr->subsys_id);
  84. cdns_pcie_ep_fn_writeb(pcie, fn, PCI_INTERRUPT_PIN, hdr->interrupt_pin);
  85. /*
  86. * Vendor ID can only be modified from function 0, all other functions
  87. * use the same vendor ID as function 0.
  88. */
  89. if (fn == 0) {
  90. /* Update the vendor IDs. */
  91. u32 id = CDNS_PCIE_LM_ID_VENDOR(hdr->vendorid) |
  92. CDNS_PCIE_LM_ID_SUBSYS(hdr->subsys_vendor_id);
  93. cdns_pcie_writel(pcie, CDNS_PCIE_LM_ID, id);
  94. }
  95. return 0;
  96. }
  97. static int cdns_pcie_ep_set_bar(struct pci_epc *epc, u8 fn, u8 vfn,
  98. struct pci_epf_bar *epf_bar)
  99. {
  100. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  101. u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  102. struct cdns_pcie *pcie = &ep->pcie;
  103. dma_addr_t bar_phys = epf_bar->phys_addr;
  104. enum pci_barno bar = epf_bar->barno;
  105. int flags = epf_bar->flags;
  106. u32 addr0, addr1, reg, cfg, b, aperture, ctrl;
  107. u32 first_vf_offset, stride;
  108. u64 sz;
  109. /* BAR size is 2^(aperture + 7) */
  110. sz = max_t(size_t, epf_bar->size, CDNS_PCIE_EP_MIN_APERTURE);
  111. /*
  112. * roundup_pow_of_two() returns an unsigned long, which is not suited
  113. * for 64bit values.
  114. */
  115. sz = 1ULL << fls64(sz - 1);
  116. aperture = ilog2(sz) - 7; /* 128B -> 0, 256B -> 1, 512B -> 2, ... */
  117. if ((flags & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  118. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS;
  119. } else {
  120. bool is_prefetch = !!(flags & PCI_BASE_ADDRESS_MEM_PREFETCH);
  121. bool is_64bits = sz > SZ_2G;
  122. if (is_64bits && (bar & 1))
  123. return -EINVAL;
  124. if (is_64bits && !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
  125. epf_bar->flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  126. if (is_64bits && is_prefetch)
  127. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS;
  128. else if (is_prefetch)
  129. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS;
  130. else if (is_64bits)
  131. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS;
  132. else
  133. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS;
  134. }
  135. addr0 = lower_32_bits(bar_phys);
  136. addr1 = upper_32_bits(bar_phys);
  137. if (vfn == 1) {
  138. if (bar < BAR_4) {
  139. reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG0(fn);
  140. b = bar;
  141. } else {
  142. reg = CDNS_PCIE_LM_EP_VFUNC_BAR_CFG1(fn);
  143. b = bar - BAR_4;
  144. }
  145. } else {
  146. if (bar < BAR_4) {
  147. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  148. b = bar;
  149. } else {
  150. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  151. b = bar - BAR_4;
  152. }
  153. }
  154. if (vfn > 0) {
  155. first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, cap +
  156. PCI_SRIOV_VF_OFFSET);
  157. stride = cdns_pcie_ep_fn_readw(pcie, fn, cap +
  158. PCI_SRIOV_VF_STRIDE);
  159. fn = fn + first_vf_offset + ((vfn - 1) * stride);
  160. }
  161. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar),
  162. addr0);
  163. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar),
  164. addr1);
  165. cfg = cdns_pcie_readl(pcie, reg);
  166. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  167. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  168. cfg |= (CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, aperture) |
  169. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl));
  170. cdns_pcie_writel(pcie, reg, cfg);
  171. return 0;
  172. }
  173. static void cdns_pcie_ep_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
  174. struct pci_epf_bar *epf_bar)
  175. {
  176. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  177. struct cdns_pcie *pcie = &ep->pcie;
  178. enum pci_barno bar = epf_bar->barno;
  179. u32 reg, cfg, b, ctrl;
  180. if (bar < BAR_4) {
  181. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn);
  182. b = bar;
  183. } else {
  184. reg = CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn);
  185. b = bar - BAR_4;
  186. }
  187. ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED;
  188. cfg = cdns_pcie_readl(pcie, reg);
  189. cfg &= ~(CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) |
  190. CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b));
  191. cfg |= CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, ctrl);
  192. cdns_pcie_writel(pcie, reg, cfg);
  193. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar), 0);
  194. cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar), 0);
  195. }
  196. static int cdns_pcie_ep_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
  197. phys_addr_t addr, u64 pci_addr, size_t size)
  198. {
  199. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  200. struct cdns_pcie *pcie = &ep->pcie;
  201. u32 cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  202. u32 first_vf_offset, stride;
  203. u32 r;
  204. if (vfn > 0) {
  205. first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, cap +
  206. PCI_SRIOV_VF_OFFSET);
  207. stride = cdns_pcie_ep_fn_readw(pcie, fn, cap +
  208. PCI_SRIOV_VF_STRIDE);
  209. fn = fn + first_vf_offset + ((vfn - 1) * stride);
  210. }
  211. r = find_first_zero_bit(&ep->ob_region_map,
  212. sizeof(ep->ob_region_map) * BITS_PER_LONG);
  213. if (r >= ep->max_regions - 1) {
  214. dev_err(&epc->dev, "no free outbound region\n");
  215. return -EINVAL;
  216. }
  217. cdns_pcie_set_outbound_region(pcie, fn, r, false, addr, pci_addr, size);
  218. set_bit(r, &ep->ob_region_map);
  219. ep->ob_addr[r] = addr;
  220. return 0;
  221. }
  222. static void cdns_pcie_ep_unmap_addr(struct pci_epc *epc, u8 fn, u8 vfn,
  223. phys_addr_t addr)
  224. {
  225. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  226. struct cdns_pcie *pcie = &ep->pcie;
  227. u32 r;
  228. for (r = 0; r < ep->max_regions - 1; r++)
  229. if (ep->ob_addr[r] == addr)
  230. break;
  231. if (r == ep->max_regions - 1)
  232. return;
  233. cdns_pcie_reset_outbound_region(pcie, r);
  234. ep->ob_addr[r] = 0;
  235. clear_bit(r, &ep->ob_region_map);
  236. }
  237. static int cdns_pcie_ep_set_msi(struct pci_epc *epc, u8 fn, u8 vfn, u8 mmc)
  238. {
  239. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  240. struct cdns_pcie *pcie = &ep->pcie;
  241. u32 sriov_cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  242. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  243. u32 first_vf_offset, stride;
  244. u16 flags;
  245. if (vfn > 0) {
  246. first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  247. PCI_SRIOV_VF_OFFSET);
  248. stride = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  249. PCI_SRIOV_VF_STRIDE);
  250. fn = fn + first_vf_offset + ((vfn - 1) * stride);
  251. }
  252. /*
  253. * Set the Multiple Message Capable bitfield into the Message Control
  254. * register.
  255. */
  256. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  257. flags = (flags & ~PCI_MSI_FLAGS_QMASK) | (mmc << 1);
  258. flags |= PCI_MSI_FLAGS_64BIT;
  259. flags &= ~PCI_MSI_FLAGS_MASKBIT;
  260. cdns_pcie_ep_fn_writew(pcie, fn, cap + PCI_MSI_FLAGS, flags);
  261. return 0;
  262. }
  263. static int cdns_pcie_ep_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
  264. {
  265. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  266. struct cdns_pcie *pcie = &ep->pcie;
  267. u32 sriov_cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  268. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  269. u32 first_vf_offset, stride;
  270. u16 flags, mme;
  271. if (vfn > 0) {
  272. first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  273. PCI_SRIOV_VF_OFFSET);
  274. stride = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  275. PCI_SRIOV_VF_STRIDE);
  276. fn = fn + first_vf_offset + ((vfn - 1) * stride);
  277. }
  278. /* Validate that the MSI feature is actually enabled. */
  279. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  280. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  281. return -EINVAL;
  282. /*
  283. * Get the Multiple Message Enable bitfield from the Message Control
  284. * register.
  285. */
  286. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  287. return mme;
  288. }
  289. static void cdns_pcie_ep_assert_intx(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
  290. u8 intx, bool is_asserted)
  291. {
  292. struct cdns_pcie *pcie = &ep->pcie;
  293. u32 offset;
  294. u16 status;
  295. u8 msg_code;
  296. intx &= 3;
  297. /* Set the outbound region if needed. */
  298. if (unlikely(ep->irq_pci_addr != CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY ||
  299. ep->irq_pci_fn != fn)) {
  300. /* First region was reserved for IRQ writes. */
  301. cdns_pcie_set_outbound_region_for_normal_msg(pcie, fn, 0,
  302. ep->irq_phys_addr);
  303. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_LEGACY;
  304. ep->irq_pci_fn = fn;
  305. }
  306. if (is_asserted) {
  307. ep->irq_pending |= BIT(intx);
  308. msg_code = MSG_CODE_ASSERT_INTA + intx;
  309. } else {
  310. ep->irq_pending &= ~BIT(intx);
  311. msg_code = MSG_CODE_DEASSERT_INTA + intx;
  312. }
  313. status = cdns_pcie_ep_fn_readw(pcie, fn, PCI_STATUS);
  314. if (((status & PCI_STATUS_INTERRUPT) != 0) ^ (ep->irq_pending != 0)) {
  315. status ^= PCI_STATUS_INTERRUPT;
  316. cdns_pcie_ep_fn_writew(pcie, fn, PCI_STATUS, status);
  317. }
  318. offset = CDNS_PCIE_NORMAL_MSG_ROUTING(MSG_ROUTING_LOCAL) |
  319. CDNS_PCIE_NORMAL_MSG_CODE(msg_code) |
  320. CDNS_PCIE_MSG_NO_DATA;
  321. writel(0, ep->irq_cpu_addr + offset);
  322. }
  323. static int cdns_pcie_ep_send_legacy_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
  324. u8 intx)
  325. {
  326. u16 cmd;
  327. cmd = cdns_pcie_ep_fn_readw(&ep->pcie, fn, PCI_COMMAND);
  328. if (cmd & PCI_COMMAND_INTX_DISABLE)
  329. return -EINVAL;
  330. cdns_pcie_ep_assert_intx(ep, fn, vfn, intx, true);
  331. /*
  332. * The mdelay() value was taken from dra7xx_pcie_raise_legacy_irq()
  333. * from drivers/pci/dwc/pci-dra7xx.c
  334. */
  335. mdelay(1);
  336. cdns_pcie_ep_assert_intx(ep, fn, vfn, intx, false);
  337. return 0;
  338. }
  339. static int cdns_pcie_ep_send_msi_irq(struct cdns_pcie_ep *ep, u8 fn, u8 vfn,
  340. u8 interrupt_num)
  341. {
  342. struct cdns_pcie *pcie = &ep->pcie;
  343. u32 sriov_cap = CDNS_PCIE_EP_FUNC_SRIOV_CAP_OFFSET;
  344. u32 cap = CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET;
  345. u16 flags, mme, data, data_mask;
  346. u8 msi_count;
  347. u64 pci_addr, pci_addr_mask = 0xff;
  348. u32 first_vf_offset, stride;
  349. if (vfn > 0) {
  350. first_vf_offset = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  351. PCI_SRIOV_VF_OFFSET);
  352. stride = cdns_pcie_ep_fn_readw(pcie, fn, sriov_cap +
  353. PCI_SRIOV_VF_STRIDE);
  354. fn = fn + first_vf_offset + ((vfn - 1) * stride);
  355. }
  356. /* Check whether the MSI feature has been enabled by the PCI host. */
  357. flags = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_FLAGS);
  358. if (!(flags & PCI_MSI_FLAGS_ENABLE))
  359. return -EINVAL;
  360. /* Get the number of enabled MSIs */
  361. mme = (flags & PCI_MSI_FLAGS_QSIZE) >> 4;
  362. msi_count = 1 << mme;
  363. if (!interrupt_num || interrupt_num > msi_count)
  364. return -EINVAL;
  365. /* Check whether MSI is masked */
  366. data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_MASK_64);
  367. if (data & (1 << (interrupt_num - 1)))
  368. return -EINVAL;
  369. /* Compute the data value to be written. */
  370. data_mask = msi_count - 1;
  371. data = cdns_pcie_ep_fn_readw(pcie, fn, cap + PCI_MSI_DATA_64);
  372. data = (data & ~data_mask) | ((interrupt_num - 1) & data_mask);
  373. /* Get the PCI address where to write the data into. */
  374. pci_addr = cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_HI);
  375. pci_addr <<= 32;
  376. pci_addr |= cdns_pcie_ep_fn_readl(pcie, fn, cap + PCI_MSI_ADDRESS_LO);
  377. pci_addr &= GENMASK_ULL(63, 2);
  378. /* Set the outbound region if needed. */
  379. if (unlikely(ep->irq_pci_addr != (pci_addr & ~pci_addr_mask) ||
  380. ep->irq_pci_fn != fn)) {
  381. /* First region was reserved for IRQ writes. */
  382. cdns_pcie_set_outbound_region(pcie, fn, 0,
  383. false,
  384. ep->irq_phys_addr,
  385. pci_addr & ~pci_addr_mask,
  386. pci_addr_mask + 1);
  387. ep->irq_pci_addr = (pci_addr & ~pci_addr_mask);
  388. ep->irq_pci_fn = fn;
  389. }
  390. writew(data, ep->irq_cpu_addr + (pci_addr & pci_addr_mask));
  391. return 0;
  392. }
  393. static int cdns_pcie_ep_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
  394. enum pci_epc_irq_type type,
  395. u16 interrupt_num)
  396. {
  397. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  398. switch (type) {
  399. case PCI_EPC_IRQ_LEGACY:
  400. return cdns_pcie_ep_send_legacy_irq(ep, fn, vfn, 0);
  401. case PCI_EPC_IRQ_MSI:
  402. return cdns_pcie_ep_send_msi_irq(ep, fn, vfn, interrupt_num);
  403. default:
  404. break;
  405. }
  406. return -EINVAL;
  407. }
  408. static int cdns_pcie_ep_start(struct pci_epc *epc)
  409. {
  410. struct cdns_pcie_ep *ep = epc_get_drvdata(epc);
  411. struct cdns_pcie *pcie = &ep->pcie;
  412. struct pci_epf *epf;
  413. int ret = 0;
  414. u32 cfg;
  415. /*
  416. * BIT(0) is hardwired to 1, hence function 0 is always enabled
  417. * and can't be disabled anyway.
  418. */
  419. cfg = BIT(0);
  420. list_for_each_entry(epf, &epc->pci_epf, list)
  421. cfg |= BIT(epf->func_no);
  422. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, cfg);
  423. ret = cdns_pcie_start_link(pcie, true);
  424. if (ret)
  425. dev_err(ep->dev, "Failed to start link\n");
  426. return ret;
  427. }
  428. static const struct pci_epc_features cdns_pcie_epc_features = {
  429. .linkup_notifier = false,
  430. .msi_capable = true,
  431. .msix_capable = false,
  432. .align = 256,
  433. };
  434. static const struct pci_epc_features cdns_pcie_epc_vf_features = {
  435. .linkup_notifier = false,
  436. .msi_capable = true,
  437. .msix_capable = true,
  438. .align = 65536,
  439. };
  440. static const struct pci_epc_features*
  441. cdns_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
  442. {
  443. if (!vfunc_no)
  444. return &cdns_pcie_epc_features;
  445. return &cdns_pcie_epc_vf_features;
  446. }
  447. static const struct pci_epc_ops cdns_pcie_epc_ops = {
  448. .epf_init = cdns_pcie_ep_epf_init,
  449. .epf_exit = cdns_pcie_ep_epf_exit,
  450. .data_transfer = cdns_pcie_ep_data_transfer,
  451. .write_header = cdns_pcie_ep_write_header,
  452. .set_bar = cdns_pcie_ep_set_bar,
  453. .clear_bar = cdns_pcie_ep_clear_bar,
  454. .map_addr = cdns_pcie_ep_map_addr,
  455. .unmap_addr = cdns_pcie_ep_unmap_addr,
  456. .set_msi = cdns_pcie_ep_set_msi,
  457. .get_msi = cdns_pcie_ep_get_msi,
  458. .raise_irq = cdns_pcie_ep_raise_irq,
  459. .start = cdns_pcie_ep_start,
  460. .get_features = cdns_pcie_ep_get_features,
  461. };
  462. static struct cdns_pcie_ep_data cdns_ti_pcie_ep_data = {
  463. .read = cdns_pcie_read32,
  464. .write = cdns_pcie_write32,
  465. };
  466. static const struct of_device_id cdns_pcie_ep_of_match[] = {
  467. { .compatible = "cdns,cdns-pcie-ep",
  468. },
  469. { .compatible = "ti,j721e-cdns-pcie-ep",
  470. .data = &cdns_ti_pcie_ep_data,
  471. },
  472. { },
  473. };
  474. static int cdns_pcie_ep_probe(struct platform_device *pdev)
  475. {
  476. struct device *dev = &pdev->dev;
  477. struct device_node *np = dev->of_node;
  478. const struct cdns_pcie_ep_data *data;
  479. const struct of_device_id *match;
  480. struct cdns_pcie_ep *ep;
  481. struct cdns_pcie *pcie;
  482. struct pci_epc *epc;
  483. struct resource *res;
  484. int ret;
  485. int phy_count;
  486. match = of_match_device(of_match_ptr(cdns_pcie_ep_of_match), dev);
  487. if (!match)
  488. return -EINVAL;
  489. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  490. if (!ep)
  491. return -ENOMEM;
  492. ep->dev = dev;
  493. pcie = &ep->pcie;
  494. pcie->is_rc = false;
  495. pcie->plat_data = pdev->dev.platform_data;
  496. data = (struct cdns_pcie_ep_data *)match->data;
  497. if (data) {
  498. if (data->read)
  499. pcie->read = data->read;
  500. if (data->write)
  501. pcie->write = data->write;
  502. }
  503. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg");
  504. pcie->reg_base = devm_ioremap_resource(dev, res);
  505. if (IS_ERR(pcie->reg_base)) {
  506. dev_err(dev, "missing \"reg\"\n");
  507. return PTR_ERR(pcie->reg_base);
  508. }
  509. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  510. if (!res)
  511. dev_dbg(dev, "missing \"mem\"\n");
  512. pcie->mem_res = res;
  513. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
  514. if (!res) {
  515. dev_err(dev, "missing \"addr_space\"\n");
  516. return -EINVAL;
  517. }
  518. pcie->addr_res = res;
  519. ret = of_property_read_u32(np, "cdns,max-outbound-regions",
  520. &ep->max_regions);
  521. if (ret < 0) {
  522. dev_err(dev, "missing \"cdns,max-outbound-regions\"\n");
  523. return ret;
  524. }
  525. ep->ob_addr = devm_kcalloc(dev,
  526. ep->max_regions, sizeof(*ep->ob_addr),
  527. GFP_KERNEL);
  528. if (!ep->ob_addr)
  529. return -ENOMEM;
  530. ret = cdns_pcie_init_phy(dev, pcie);
  531. if (ret) {
  532. dev_err(dev, "failed to init phy\n");
  533. return ret;
  534. }
  535. platform_set_drvdata(pdev, pcie);
  536. pm_runtime_enable(dev);
  537. ret = pm_runtime_get_sync(dev);
  538. if (ret < 0) {
  539. dev_err(dev, "pm_runtime_get_sync() failed\n");
  540. goto err_get_sync;
  541. }
  542. /* Disable all but function 0 (anyway BIT(0) is hardwired to 1). */
  543. cdns_pcie_writel(pcie, CDNS_PCIE_LM_EP_FUNC_CFG, BIT(0));
  544. epc = devm_pci_epc_create(dev, &cdns_pcie_epc_ops);
  545. if (IS_ERR(epc)) {
  546. dev_err(dev, "failed to create epc device\n");
  547. ret = PTR_ERR(epc);
  548. goto err_init;
  549. }
  550. epc_set_drvdata(epc, ep);
  551. if (of_property_read_u8(np, "max-functions", &epc->max_functions) < 0)
  552. epc->max_functions = 1;
  553. ret = pci_epc_mem_init(epc, pcie->addr_res->start,
  554. resource_size(pcie->addr_res));
  555. if (ret < 0) {
  556. dev_err(dev, "failed to initialize the memory space\n");
  557. goto err_init;
  558. }
  559. ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
  560. SZ_128K);
  561. if (!ep->irq_cpu_addr) {
  562. dev_err(dev, "failed to reserve memory space for MSI\n");
  563. ret = -ENOMEM;
  564. goto free_epc_mem;
  565. }
  566. ep->irq_pci_addr = CDNS_PCIE_EP_IRQ_PCI_ADDR_NONE;
  567. /* Reserve region 0 for IRQs */
  568. set_bit(0, &ep->ob_region_map);
  569. return 0;
  570. free_epc_mem:
  571. pci_epc_mem_exit(epc);
  572. err_init:
  573. pm_runtime_put_sync(dev);
  574. err_get_sync:
  575. pm_runtime_disable(dev);
  576. cdns_pcie_disable_phy(pcie);
  577. phy_count = pcie->phy_count;
  578. while (phy_count--)
  579. device_link_del(pcie->link[phy_count]);
  580. return ret;
  581. }
  582. static void cdns_pcie_ep_shutdown(struct platform_device *pdev)
  583. {
  584. struct device *dev = &pdev->dev;
  585. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  586. int ret;
  587. ret = pm_runtime_put_sync(dev);
  588. if (ret < 0)
  589. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  590. pm_runtime_disable(dev);
  591. cdns_pcie_disable_phy(pcie);
  592. }
  593. static struct platform_driver cdns_pcie_ep_driver = {
  594. .driver = {
  595. .name = "cdns-pcie-ep",
  596. .of_match_table = cdns_pcie_ep_of_match,
  597. .pm = &cdns_pcie_pm_ops,
  598. },
  599. .probe = cdns_pcie_ep_probe,
  600. .shutdown = cdns_pcie_ep_shutdown,
  601. };
  602. builtin_platform_driver(cdns_pcie_ep_driver);