pcie_dw_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2021 BayLibre, SAS
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. *
  6. * Copyright (c) 2021 Rockchip, Inc.
  7. *
  8. * Copyright (C) 2018 Texas Instruments, Inc
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <log.h>
  13. #include <pci.h>
  14. #include <dm/device_compat.h>
  15. #include <asm/io.h>
  16. #include <linux/delay.h>
  17. #include "pcie_dw_common.h"
  18. int pcie_dw_get_link_speed(struct pcie_dw *pci)
  19. {
  20. return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
  21. PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
  22. }
  23. int pcie_dw_get_link_width(struct pcie_dw *pci)
  24. {
  25. return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
  26. PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
  27. }
  28. static void dw_pcie_writel_ob_unroll(struct pcie_dw *pci, u32 index, u32 reg,
  29. u32 val)
  30. {
  31. u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
  32. void __iomem *base = pci->atu_base;
  33. writel(val, base + offset + reg);
  34. }
  35. static u32 dw_pcie_readl_ob_unroll(struct pcie_dw *pci, u32 index, u32 reg)
  36. {
  37. u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
  38. void __iomem *base = pci->atu_base;
  39. return readl(base + offset + reg);
  40. }
  41. /**
  42. * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
  43. *
  44. * @pcie: Pointer to the PCI controller state
  45. * @index: ATU region index
  46. * @type: ATU accsess type
  47. * @cpu_addr: the physical address for the translation entry
  48. * @pci_addr: the pcie bus address for the translation entry
  49. * @size: the size of the translation entry
  50. *
  51. * Return: 0 is successful and -1 is failure
  52. */
  53. int pcie_dw_prog_outbound_atu_unroll(struct pcie_dw *pci, int index,
  54. int type, u64 cpu_addr,
  55. u64 pci_addr, u32 size)
  56. {
  57. u32 retries, val;
  58. dev_dbg(pci->dev, "ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
  59. index, type, cpu_addr, pci_addr, size);
  60. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
  61. lower_32_bits(cpu_addr));
  62. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
  63. upper_32_bits(cpu_addr));
  64. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
  65. lower_32_bits(cpu_addr + size - 1));
  66. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_LIMIT,
  67. upper_32_bits(cpu_addr + size - 1));
  68. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
  69. lower_32_bits(pci_addr));
  70. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
  71. upper_32_bits(pci_addr));
  72. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
  73. type);
  74. dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
  75. PCIE_ATU_ENABLE);
  76. /*
  77. * Make sure ATU enable takes effect before any subsequent config
  78. * and I/O accesses.
  79. */
  80. for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
  81. val = dw_pcie_readl_ob_unroll(pci, index,
  82. PCIE_ATU_UNR_REGION_CTRL2);
  83. if (val & PCIE_ATU_ENABLE)
  84. return 0;
  85. udelay(LINK_WAIT_IATU);
  86. }
  87. dev_err(pci->dev, "outbound iATU is not being enabled\n");
  88. return -1;
  89. }
  90. /**
  91. * set_cfg_address() - Configure the PCIe controller config space access
  92. *
  93. * @pcie: Pointer to the PCI controller state
  94. * @d: PCI device to access
  95. * @where: Offset in the configuration space
  96. *
  97. * Configures the PCIe controller to access the configuration space of
  98. * a specific PCIe device and returns the address to use for this
  99. * access.
  100. *
  101. * Return: Address that can be used to access the configation space
  102. * of the requested device / offset
  103. */
  104. static uintptr_t set_cfg_address(struct pcie_dw *pcie,
  105. pci_dev_t d, uint where)
  106. {
  107. int bus = PCI_BUS(d) - pcie->first_busno;
  108. uintptr_t va_address;
  109. u32 atu_type;
  110. int ret;
  111. /* Use dbi_base for own configuration read and write */
  112. if (!bus) {
  113. va_address = (uintptr_t)pcie->dbi_base;
  114. goto out;
  115. }
  116. if (bus == 1)
  117. /*
  118. * For local bus whose primary bus number is root bridge,
  119. * change TLP Type field to 4.
  120. */
  121. atu_type = PCIE_ATU_TYPE_CFG0;
  122. else
  123. /* Otherwise, change TLP Type field to 5. */
  124. atu_type = PCIE_ATU_TYPE_CFG1;
  125. /*
  126. * Not accessing root port configuration space?
  127. * Region #1 is used for Outbound CFG space access.
  128. * Direction = Outbound
  129. * Region Index = 1
  130. */
  131. d = PCI_MASK_BUS(d);
  132. d = PCI_ADD_BUS(bus, d);
  133. ret = pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
  134. atu_type, (u64)pcie->cfg_base,
  135. d << 8, pcie->cfg_size);
  136. if (ret)
  137. return (uintptr_t)ret;
  138. va_address = (uintptr_t)pcie->cfg_base;
  139. out:
  140. va_address += where & ~0x3;
  141. return va_address;
  142. }
  143. /**
  144. * pcie_dw_addr_valid() - Check for valid bus address
  145. *
  146. * @d: The PCI device to access
  147. * @first_busno: Bus number of the PCIe controller root complex
  148. *
  149. * Return 1 (true) if the PCI device can be accessed by this controller.
  150. *
  151. * Return: 1 on valid, 0 on invalid
  152. */
  153. static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
  154. {
  155. if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
  156. return 0;
  157. if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
  158. return 0;
  159. return 1;
  160. }
  161. /**
  162. * pcie_dw_read_config() - Read from configuration space
  163. *
  164. * @bus: Pointer to the PCI bus
  165. * @bdf: Identifies the PCIe device to access
  166. * @offset: The offset into the device's configuration space
  167. * @valuep: A pointer at which to store the read value
  168. * @size: Indicates the size of access to perform
  169. *
  170. * Read a value of size @size from offset @offset within the configuration
  171. * space of the device identified by the bus, device & function numbers in @bdf
  172. * on the PCI bus @bus.
  173. *
  174. * Return: 0 on success
  175. */
  176. int pcie_dw_read_config(const struct udevice *bus, pci_dev_t bdf,
  177. uint offset, ulong *valuep,
  178. enum pci_size_t size)
  179. {
  180. struct pcie_dw *pcie = dev_get_priv(bus);
  181. uintptr_t va_address;
  182. ulong value;
  183. dev_dbg(pcie->dev, "PCIE CFG read: bdf=%2x:%2x:%2x ",
  184. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  185. if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
  186. debug("- out of range\n");
  187. *valuep = pci_get_ff(size);
  188. return 0;
  189. }
  190. va_address = set_cfg_address(pcie, bdf, offset);
  191. value = readl((void __iomem *)va_address);
  192. debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
  193. *valuep = pci_conv_32_to_size(value, offset, size);
  194. return pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
  195. PCIE_ATU_TYPE_IO, pcie->io.phys_start,
  196. pcie->io.bus_start, pcie->io.size);
  197. }
  198. /**
  199. * pcie_dw_write_config() - Write to configuration space
  200. *
  201. * @bus: Pointer to the PCI bus
  202. * @bdf: Identifies the PCIe device to access
  203. * @offset: The offset into the device's configuration space
  204. * @value: The value to write
  205. * @size: Indicates the size of access to perform
  206. *
  207. * Write the value @value of size @size from offset @offset within the
  208. * configuration space of the device identified by the bus, device & function
  209. * numbers in @bdf on the PCI bus @bus.
  210. *
  211. * Return: 0 on success
  212. */
  213. int pcie_dw_write_config(struct udevice *bus, pci_dev_t bdf,
  214. uint offset, ulong value,
  215. enum pci_size_t size)
  216. {
  217. struct pcie_dw *pcie = dev_get_priv(bus);
  218. uintptr_t va_address;
  219. ulong old;
  220. dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
  221. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  222. dev_dbg(pcie->dev, "(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
  223. if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
  224. debug("- out of range\n");
  225. return 0;
  226. }
  227. va_address = set_cfg_address(pcie, bdf, offset);
  228. old = readl((void __iomem *)va_address);
  229. value = pci_conv_size_to_32(old, value, offset, size);
  230. writel(value, (void __iomem *)va_address);
  231. return pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
  232. PCIE_ATU_TYPE_IO, pcie->io.phys_start,
  233. pcie->io.bus_start, pcie->io.size);
  234. }
  235. /**
  236. * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion
  237. *
  238. * @pcie: Pointer to the PCI controller state
  239. *
  240. * Configure the host BARs of the PCIe controller root port so that
  241. * PCI(e) devices may access the system memory.
  242. */
  243. void pcie_dw_setup_host(struct pcie_dw *pci)
  244. {
  245. struct udevice *ctlr = pci_get_controller(pci->dev);
  246. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  247. u32 ret;
  248. if (!pci->atu_base)
  249. pci->atu_base = pci->dbi_base + DEFAULT_DBI_ATU_OFFSET;
  250. /* setup RC BARs */
  251. writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
  252. pci->dbi_base + PCI_BASE_ADDRESS_0);
  253. writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1);
  254. /* setup interrupt pins */
  255. clrsetbits_le32(pci->dbi_base + PCI_INTERRUPT_LINE,
  256. 0xff00, 0x100);
  257. /* setup bus numbers */
  258. clrsetbits_le32(pci->dbi_base + PCI_PRIMARY_BUS,
  259. 0xffffff, 0x00ff0100);
  260. /* setup command register */
  261. clrsetbits_le32(pci->dbi_base + PCI_PRIMARY_BUS,
  262. 0xffff,
  263. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  264. PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  265. /* Enable write permission for the DBI read-only register */
  266. dw_pcie_dbi_write_enable(pci, true);
  267. /* program correct class for RC */
  268. writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
  269. /* Better disable write permission right after the update */
  270. dw_pcie_dbi_write_enable(pci, false);
  271. setbits_le32(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL,
  272. PORT_LOGIC_SPEED_CHANGE);
  273. for (ret = 0; ret < hose->region_count; ret++) {
  274. if (hose->regions[ret].flags == PCI_REGION_IO) {
  275. pci->io.phys_start = hose->regions[ret].phys_start; /* IO base */
  276. pci->io.bus_start = hose->regions[ret].bus_start; /* IO_bus_addr */
  277. pci->io.size = hose->regions[ret].size; /* IO size */
  278. } else if (hose->regions[ret].flags == PCI_REGION_MEM) {
  279. pci->mem.phys_start = hose->regions[ret].phys_start; /* MEM base */
  280. pci->mem.bus_start = hose->regions[ret].bus_start; /* MEM_bus_addr */
  281. pci->mem.size = hose->regions[ret].size; /* MEM size */
  282. } else if (hose->regions[ret].flags == PCI_REGION_PREFETCH) {
  283. pci->prefetch.phys_start = hose->regions[ret].phys_start; /* PREFETCH base */
  284. pci->prefetch.bus_start = hose->regions[ret].bus_start; /* PREFETCH_bus_addr */
  285. pci->prefetch.size = hose->regions[ret].size; /* PREFETCH size */
  286. } else if (hose->regions[ret].flags == PCI_REGION_SYS_MEMORY) {
  287. if (!pci->cfg_base) {
  288. pci->cfg_base = (void *)(pci->io.phys_start - pci->io.size);
  289. pci->cfg_size = pci->io.size;
  290. }
  291. } else {
  292. dev_err(pci->dev, "invalid flags type!\n");
  293. }
  294. }
  295. dev_dbg(pci->dev, "Config space: [0x%llx - 0x%llx, size 0x%llx]\n",
  296. (u64)pci->cfg_base, (u64)pci->cfg_base + pci->cfg_size,
  297. (u64)pci->cfg_size);
  298. dev_dbg(pci->dev, "IO space: [0x%llx - 0x%llx, size 0x%llx]\n",
  299. (u64)pci->io.phys_start, (u64)pci->io.phys_start + pci->io.size,
  300. (u64)pci->io.size);
  301. dev_dbg(pci->dev, "IO bus: [0x%llx - 0x%llx, size 0x%llx]\n",
  302. (u64)pci->io.bus_start, (u64)pci->io.bus_start + pci->io.size,
  303. (u64)pci->io.size);
  304. dev_dbg(pci->dev, "MEM space: [0x%llx - 0x%llx, size 0x%llx]\n",
  305. (u64)pci->mem.phys_start,
  306. (u64)pci->mem.phys_start + pci->mem.size,
  307. (u64)pci->mem.size);
  308. dev_dbg(pci->dev, "MEM bus: [0x%llx - 0x%llx, size 0x%llx]\n",
  309. (u64)pci->mem.bus_start,
  310. (u64)pci->mem.bus_start + pci->mem.size,
  311. (u64)pci->mem.size);
  312. if (pci->prefetch.size) {
  313. dev_dbg(pci->dev, "PREFETCH space: [0x%llx - 0x%llx, size 0x%llx]\n",
  314. (u64)pci->prefetch.phys_start,
  315. (u64)pci->prefetch.phys_start + pci->prefetch.size,
  316. (u64)pci->prefetch.size);
  317. dev_dbg(pci->dev, "PREFETCH bus: [0x%llx - 0x%llx, size 0x%llx]\n",
  318. (u64)pci->prefetch.bus_start,
  319. (u64)pci->prefetch.bus_start + pci->prefetch.size,
  320. (u64)pci->prefetch.size);
  321. }
  322. }