pcie_intel_fpga.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel FPGA PCIe host controller driver
  4. *
  5. * Copyright (C) 2013-2018 Intel Corporation. All rights reserved
  6. *
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <pci.h>
  11. #include <asm/io.h>
  12. #define RP_TX_REG0 0x2000
  13. #define RP_TX_CNTRL 0x2004
  14. #define RP_TX_SOP BIT(0)
  15. #define RP_TX_EOP BIT(1)
  16. #define RP_RXCPL_STATUS 0x200C
  17. #define RP_RXCPL_SOP BIT(0)
  18. #define RP_RXCPL_EOP BIT(1)
  19. #define RP_RXCPL_REG 0x2008
  20. #define P2A_INT_STATUS 0x3060
  21. #define P2A_INT_STS_ALL 0xf
  22. #define P2A_INT_ENABLE 0x3070
  23. #define RP_CAP_OFFSET 0x70
  24. /* TLP configuration type 0 and 1 */
  25. #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
  26. #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
  27. #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
  28. #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
  29. #define TLP_PAYLOAD_SIZE 0x01
  30. #define TLP_READ_TAG 0x1d
  31. #define TLP_WRITE_TAG 0x10
  32. #define RP_DEVFN 0
  33. #define RP_CFG_ADDR(pcie, reg) \
  34. ((pcie->hip_base) + (reg) + (1 << 20))
  35. #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
  36. #define TLP_CFGRD_DW0(pcie, bus) \
  37. ((((bus != pcie->first_busno) ? TLP_FMTTYPE_CFGRD0 \
  38. : TLP_FMTTYPE_CFGRD1) << 24) | \
  39. TLP_PAYLOAD_SIZE)
  40. #define TLP_CFGWR_DW0(pcie, bus) \
  41. ((((bus != pcie->first_busno) ? TLP_FMTTYPE_CFGWR0 \
  42. : TLP_FMTTYPE_CFGWR1) << 24) | \
  43. TLP_PAYLOAD_SIZE)
  44. #define TLP_CFG_DW1(pcie, tag, be) \
  45. (((TLP_REQ_ID(pcie->first_busno, RP_DEVFN)) << 16) | (tag << 8) | (be))
  46. #define TLP_CFG_DW2(bus, dev, fn, offset) \
  47. (((bus) << 24) | ((dev) << 19) | ((fn) << 16) | (offset))
  48. #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
  49. #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
  50. #define TLP_HDR_SIZE 3
  51. #define TLP_LOOP 500
  52. #define DWORD_MASK 3
  53. #define IS_ROOT_PORT(pcie, bdf) \
  54. ((PCI_BUS(bdf) == pcie->first_busno) ? true : false)
  55. #define PCI_EXP_LNKSTA 18 /* Link Status */
  56. #define PCI_EXP_LNKSTA_DLLLA 0x2000 /* Data Link Layer Link Active */
  57. /**
  58. * struct intel_fpga_pcie - Intel FPGA PCIe controller state
  59. * @bus: Pointer to the PCI bus
  60. * @cra_base: The base address of CRA register space
  61. * @hip_base: The base address of Rootport configuration space
  62. * @first_busno: This driver supports multiple PCIe controllers.
  63. * first_busno stores the bus number of the PCIe root-port
  64. * number which may vary depending on the PCIe setup.
  65. */
  66. struct intel_fpga_pcie {
  67. struct udevice *bus;
  68. void __iomem *cra_base;
  69. void __iomem *hip_base;
  70. int first_busno;
  71. };
  72. /**
  73. * Intel FPGA PCIe port uses BAR0 of RC's configuration space as the
  74. * translation from PCI bus to native BUS. Entire DDR region is mapped
  75. * into PCIe space using these registers, so it can be reached by DMA from
  76. * EP devices.
  77. * The BAR0 of bridge should be hidden during enumeration to avoid the
  78. * sizing and resource allocation by PCIe core.
  79. */
  80. static bool intel_fpga_pcie_hide_rc_bar(struct intel_fpga_pcie *pcie,
  81. pci_dev_t bdf, int offset)
  82. {
  83. if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) == 0 &&
  84. PCI_FUNC(bdf) == 0 && offset == PCI_BASE_ADDRESS_0)
  85. return true;
  86. return false;
  87. }
  88. static inline void cra_writel(struct intel_fpga_pcie *pcie, const u32 value,
  89. const u32 reg)
  90. {
  91. writel(value, pcie->cra_base + reg);
  92. }
  93. static inline u32 cra_readl(struct intel_fpga_pcie *pcie, const u32 reg)
  94. {
  95. return readl(pcie->cra_base + reg);
  96. }
  97. static bool intel_fpga_pcie_link_up(struct intel_fpga_pcie *pcie)
  98. {
  99. return !!(readw(RP_CFG_ADDR(pcie, RP_CAP_OFFSET + PCI_EXP_LNKSTA))
  100. & PCI_EXP_LNKSTA_DLLLA);
  101. }
  102. static bool intel_fpga_pcie_addr_valid(struct intel_fpga_pcie *pcie,
  103. pci_dev_t bdf)
  104. {
  105. /* If there is no link, then there is no device */
  106. if (!IS_ROOT_PORT(pcie, bdf) && !intel_fpga_pcie_link_up(pcie))
  107. return false;
  108. /* access only one slot on each root port */
  109. if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) > 0)
  110. return false;
  111. if ((PCI_BUS(bdf) == pcie->first_busno + 1) && PCI_DEV(bdf) > 0)
  112. return false;
  113. return true;
  114. }
  115. static void tlp_write_tx(struct intel_fpga_pcie *pcie, u32 reg0, u32 ctrl)
  116. {
  117. cra_writel(pcie, reg0, RP_TX_REG0);
  118. cra_writel(pcie, ctrl, RP_TX_CNTRL);
  119. }
  120. static int tlp_read_packet(struct intel_fpga_pcie *pcie, u32 *value)
  121. {
  122. int i;
  123. u32 ctrl;
  124. u32 comp_status;
  125. u32 dw[4];
  126. u32 count = 0;
  127. for (i = 0; i < TLP_LOOP; i++) {
  128. ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
  129. if (!(ctrl & RP_RXCPL_SOP))
  130. continue;
  131. /* read first DW */
  132. dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
  133. /* Poll for EOP */
  134. for (i = 0; i < TLP_LOOP; i++) {
  135. ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
  136. dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
  137. if (ctrl & RP_RXCPL_EOP) {
  138. comp_status = TLP_COMP_STATUS(dw[1]);
  139. if (comp_status)
  140. return -EFAULT;
  141. if (value &&
  142. TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
  143. count >= 3)
  144. *value = dw[3];
  145. return 0;
  146. }
  147. }
  148. udelay(5);
  149. }
  150. dev_err(pcie->dev, "read TLP packet timed out\n");
  151. return -ENODEV;
  152. }
  153. static void tlp_write_packet(struct intel_fpga_pcie *pcie, u32 *headers,
  154. u32 data)
  155. {
  156. tlp_write_tx(pcie, headers[0], RP_TX_SOP);
  157. tlp_write_tx(pcie, headers[1], 0);
  158. tlp_write_tx(pcie, headers[2], 0);
  159. tlp_write_tx(pcie, data, RP_TX_EOP);
  160. }
  161. static int tlp_cfg_dword_read(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
  162. int offset, u8 byte_en, u32 *value)
  163. {
  164. u32 headers[TLP_HDR_SIZE];
  165. u8 busno = PCI_BUS(bdf);
  166. headers[0] = TLP_CFGRD_DW0(pcie, busno);
  167. headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
  168. headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  169. tlp_write_packet(pcie, headers, 0);
  170. return tlp_read_packet(pcie, value);
  171. }
  172. static int tlp_cfg_dword_write(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
  173. int offset, u8 byte_en, u32 value)
  174. {
  175. u32 headers[TLP_HDR_SIZE];
  176. u8 busno = PCI_BUS(bdf);
  177. headers[0] = TLP_CFGWR_DW0(pcie, busno);
  178. headers[1] = TLP_CFG_DW1(pcie, TLP_WRITE_TAG, byte_en);
  179. headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  180. tlp_write_packet(pcie, headers, value);
  181. return tlp_read_packet(pcie, NULL);
  182. }
  183. int intel_fpga_rp_conf_addr(struct udevice *bus, pci_dev_t bdf,
  184. uint offset, void **paddress)
  185. {
  186. struct intel_fpga_pcie *pcie = dev_get_priv(bus);
  187. *paddress = RP_CFG_ADDR(pcie, offset);
  188. return 0;
  189. }
  190. static int intel_fpga_pcie_rp_rd_conf(struct udevice *bus, pci_dev_t bdf,
  191. uint offset, ulong *valuep,
  192. enum pci_size_t size)
  193. {
  194. return pci_generic_mmap_read_config(bus, intel_fpga_rp_conf_addr,
  195. bdf, offset, valuep, size);
  196. }
  197. static int intel_fpga_pcie_rp_wr_conf(struct udevice *bus, pci_dev_t bdf,
  198. uint offset, ulong value,
  199. enum pci_size_t size)
  200. {
  201. int ret;
  202. struct intel_fpga_pcie *pcie = dev_get_priv(bus);
  203. ret = pci_generic_mmap_write_config(bus, intel_fpga_rp_conf_addr,
  204. bdf, offset, value, size);
  205. if (!ret) {
  206. /* Monitor changes to PCI_PRIMARY_BUS register on root port
  207. * and update local copy of root bus number accordingly.
  208. */
  209. if (offset == PCI_PRIMARY_BUS)
  210. pcie->first_busno = (u8)(value);
  211. }
  212. return ret;
  213. }
  214. static u8 pcie_get_byte_en(uint offset, enum pci_size_t size)
  215. {
  216. switch (size) {
  217. case PCI_SIZE_8:
  218. return 1 << (offset & 3);
  219. case PCI_SIZE_16:
  220. return 3 << (offset & 3);
  221. default:
  222. return 0xf;
  223. }
  224. }
  225. static int _pcie_intel_fpga_read_config(struct intel_fpga_pcie *pcie,
  226. pci_dev_t bdf, uint offset,
  227. ulong *valuep, enum pci_size_t size)
  228. {
  229. int ret;
  230. u32 data;
  231. u8 byte_en;
  232. /* Uses memory mapped method to read rootport config registers */
  233. if (IS_ROOT_PORT(pcie, bdf))
  234. return intel_fpga_pcie_rp_rd_conf(pcie->bus, bdf,
  235. offset, valuep, size);
  236. byte_en = pcie_get_byte_en(offset, size);
  237. ret = tlp_cfg_dword_read(pcie, bdf, offset & ~DWORD_MASK,
  238. byte_en, &data);
  239. if (ret)
  240. return ret;
  241. dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
  242. offset, size, data);
  243. *valuep = pci_conv_32_to_size(data, offset, size);
  244. return 0;
  245. }
  246. static int _pcie_intel_fpga_write_config(struct intel_fpga_pcie *pcie,
  247. pci_dev_t bdf, uint offset,
  248. ulong value, enum pci_size_t size)
  249. {
  250. u32 data;
  251. u8 byte_en;
  252. dev_dbg(pcie->dev, "PCIE CFG write: (b.d.f)=(%02d.%02d.%02d)\n",
  253. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  254. dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
  255. offset, size, value);
  256. /* Uses memory mapped method to read rootport config registers */
  257. if (IS_ROOT_PORT(pcie, bdf))
  258. return intel_fpga_pcie_rp_wr_conf(pcie->bus, bdf, offset,
  259. value, size);
  260. byte_en = pcie_get_byte_en(offset, size);
  261. data = pci_conv_size_to_32(0, value, offset, size);
  262. return tlp_cfg_dword_write(pcie, bdf, offset & ~DWORD_MASK,
  263. byte_en, data);
  264. }
  265. static int pcie_intel_fpga_read_config(struct udevice *bus, pci_dev_t bdf,
  266. uint offset, ulong *valuep,
  267. enum pci_size_t size)
  268. {
  269. struct intel_fpga_pcie *pcie = dev_get_priv(bus);
  270. dev_dbg(pcie->dev, "PCIE CFG read: (b.d.f)=(%02d.%02d.%02d)\n",
  271. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  272. if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset)) {
  273. *valuep = (u32)pci_get_ff(size);
  274. return 0;
  275. }
  276. if (!intel_fpga_pcie_addr_valid(pcie, bdf)) {
  277. *valuep = (u32)pci_get_ff(size);
  278. return 0;
  279. }
  280. return _pcie_intel_fpga_read_config(pcie, bdf, offset, valuep, size);
  281. }
  282. static int pcie_intel_fpga_write_config(struct udevice *bus, pci_dev_t bdf,
  283. uint offset, ulong value,
  284. enum pci_size_t size)
  285. {
  286. struct intel_fpga_pcie *pcie = dev_get_priv(bus);
  287. if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset))
  288. return 0;
  289. if (!intel_fpga_pcie_addr_valid(pcie, bdf))
  290. return 0;
  291. return _pcie_intel_fpga_write_config(pcie, bdf, offset, value,
  292. size);
  293. }
  294. static int pcie_intel_fpga_probe(struct udevice *dev)
  295. {
  296. struct intel_fpga_pcie *pcie = dev_get_priv(dev);
  297. pcie->bus = pci_get_controller(dev);
  298. pcie->first_busno = dev->seq;
  299. /* clear all interrupts */
  300. cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
  301. /* disable all interrupts */
  302. cra_writel(pcie, 0, P2A_INT_ENABLE);
  303. return 0;
  304. }
  305. static int pcie_intel_fpga_ofdata_to_platdata(struct udevice *dev)
  306. {
  307. struct intel_fpga_pcie *pcie = dev_get_priv(dev);
  308. struct fdt_resource reg_res;
  309. int node = dev_of_offset(dev);
  310. int ret;
  311. DECLARE_GLOBAL_DATA_PTR;
  312. ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
  313. "Cra", &reg_res);
  314. if (ret) {
  315. dev_err(dev, "resource \"Cra\" not found\n");
  316. return ret;
  317. }
  318. pcie->cra_base = map_physmem(reg_res.start,
  319. fdt_resource_size(&reg_res),
  320. MAP_NOCACHE);
  321. ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
  322. "Hip", &reg_res);
  323. if (ret) {
  324. dev_err(dev, "resource \"Hip\" not found\n");
  325. return ret;
  326. }
  327. pcie->hip_base = map_physmem(reg_res.start,
  328. fdt_resource_size(&reg_res),
  329. MAP_NOCACHE);
  330. return 0;
  331. }
  332. static const struct dm_pci_ops pcie_intel_fpga_ops = {
  333. .read_config = pcie_intel_fpga_read_config,
  334. .write_config = pcie_intel_fpga_write_config,
  335. };
  336. static const struct udevice_id pcie_intel_fpga_ids[] = {
  337. { .compatible = "altr,pcie-root-port-2.0" },
  338. {},
  339. };
  340. U_BOOT_DRIVER(pcie_intel_fpga) = {
  341. .name = "pcie_intel_fpga",
  342. .id = UCLASS_PCI,
  343. .of_match = pcie_intel_fpga_ids,
  344. .ops = &pcie_intel_fpga_ops,
  345. .ofdata_to_platdata = pcie_intel_fpga_ofdata_to_platdata,
  346. .probe = pcie_intel_fpga_probe,
  347. .priv_auto_alloc_size = sizeof(struct intel_fpga_pcie),
  348. };