pcie_dw_mvebu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright (C) 2015 Marvell International Ltd.
  3. *
  4. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  5. *
  6. * Based on:
  7. * - drivers/pci/pcie_imx.c
  8. * - drivers/pci/pci_mvebu.c
  9. * - drivers/pci/pcie_xilinx.c
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <pci.h>
  16. #include <asm/io.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* PCI Config space registers */
  19. #define PCIE_CONFIG_BAR0 0x10
  20. #define PCIE_LINK_STATUS_REG 0x80
  21. #define PCIE_LINK_STATUS_SPEED_OFF 16
  22. #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
  23. #define PCIE_LINK_STATUS_WIDTH_OFF 20
  24. #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
  25. /* Resizable bar capability registers */
  26. #define RESIZABLE_BAR_CAP 0x250
  27. #define RESIZABLE_BAR_CTL0 0x254
  28. #define RESIZABLE_BAR_CTL1 0x258
  29. /* iATU registers */
  30. #define PCIE_ATU_VIEWPORT 0x900
  31. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  32. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  33. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  34. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  35. #define PCIE_ATU_CR1 0x904
  36. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  37. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  38. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  39. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  40. #define PCIE_ATU_CR2 0x908
  41. #define PCIE_ATU_ENABLE (0x1 << 31)
  42. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  43. #define PCIE_ATU_LOWER_BASE 0x90C
  44. #define PCIE_ATU_UPPER_BASE 0x910
  45. #define PCIE_ATU_LIMIT 0x914
  46. #define PCIE_ATU_LOWER_TARGET 0x918
  47. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  48. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  49. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  50. #define PCIE_ATU_UPPER_TARGET 0x91C
  51. #define PCIE_LINK_CAPABILITY 0x7C
  52. #define PCIE_LINK_CTL_2 0xA0
  53. #define TARGET_LINK_SPEED_MASK 0xF
  54. #define LINK_SPEED_GEN_1 0x1
  55. #define LINK_SPEED_GEN_2 0x2
  56. #define LINK_SPEED_GEN_3 0x3
  57. #define PCIE_GEN3_RELATED 0x890
  58. #define GEN3_EQU_DISABLE (1 << 16)
  59. #define GEN3_ZRXDC_NON_COMP (1 << 0)
  60. #define PCIE_GEN3_EQU_CTRL 0x8A8
  61. #define GEN3_EQU_EVAL_2MS_DISABLE (1 << 5)
  62. #define PCIE_ROOT_COMPLEX_MODE_MASK (0xF << 4)
  63. #define PCIE_LINK_UP_TIMEOUT_MS 100
  64. #define PCIE_GLOBAL_CONTROL 0x8000
  65. #define PCIE_APP_LTSSM_EN (1 << 2)
  66. #define PCIE_DEVICE_TYPE_OFFSET (4)
  67. #define PCIE_DEVICE_TYPE_MASK (0xF)
  68. #define PCIE_DEVICE_TYPE_EP (0x0) /* Endpoint */
  69. #define PCIE_DEVICE_TYPE_LEP (0x1) /* Legacy endpoint */
  70. #define PCIE_DEVICE_TYPE_RC (0x4) /* Root complex */
  71. #define PCIE_GLOBAL_STATUS 0x8008
  72. #define PCIE_GLB_STS_RDLH_LINK_UP (1 << 1)
  73. #define PCIE_GLB_STS_PHY_LINK_UP (1 << 9)
  74. #define PCIE_ARCACHE_TRC 0x8050
  75. #define PCIE_AWCACHE_TRC 0x8054
  76. #define ARCACHE_SHAREABLE_CACHEABLE 0x3511
  77. #define AWCACHE_SHAREABLE_CACHEABLE 0x5311
  78. #define LINK_SPEED_GEN_1 0x1
  79. #define LINK_SPEED_GEN_2 0x2
  80. #define LINK_SPEED_GEN_3 0x3
  81. /**
  82. * struct pcie_dw_mvebu - MVEBU DW PCIe controller state
  83. *
  84. * @ctrl_base: The base address of the register space
  85. * @cfg_base: The base address of the configuration space
  86. * @cfg_size: The size of the configuration space which is needed
  87. * as it gets written into the PCIE_ATU_LIMIT register
  88. * @first_busno: This driver supports multiple PCIe controllers.
  89. * first_busno stores the bus number of the PCIe root-port
  90. * number which may vary depending on the PCIe setup
  91. * (PEX switches etc).
  92. */
  93. struct pcie_dw_mvebu {
  94. void *ctrl_base;
  95. void *cfg_base;
  96. fdt_size_t cfg_size;
  97. int first_busno;
  98. };
  99. static int pcie_dw_get_link_speed(const void *regs_base)
  100. {
  101. return (readl(regs_base + PCIE_LINK_STATUS_REG) &
  102. PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
  103. }
  104. static int pcie_dw_get_link_width(const void *regs_base)
  105. {
  106. return (readl(regs_base + PCIE_LINK_STATUS_REG) &
  107. PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
  108. }
  109. /**
  110. * set_cfg_address() - Configure the PCIe controller config space access
  111. *
  112. * @pcie: Pointer to the PCI controller state
  113. * @d: PCI device to access
  114. * @where: Offset in the configuration space
  115. *
  116. * Configures the PCIe controller to access the configuration space of
  117. * a specific PCIe device and returns the address to use for this
  118. * access.
  119. *
  120. * Return: Address that can be used to access the configation space
  121. * of the requested device / offset
  122. */
  123. static uintptr_t set_cfg_address(struct pcie_dw_mvebu *pcie,
  124. pci_dev_t d, uint where)
  125. {
  126. uintptr_t va_address;
  127. /*
  128. * Region #0 is used for Outbound CFG space access.
  129. * Direction = Outbound
  130. * Region Index = 0
  131. */
  132. writel(0, pcie->ctrl_base + PCIE_ATU_VIEWPORT);
  133. if (PCI_BUS(d) == (pcie->first_busno + 1))
  134. /* For local bus, change TLP Type field to 4. */
  135. writel(PCIE_ATU_TYPE_CFG0, pcie->ctrl_base + PCIE_ATU_CR1);
  136. else
  137. /* Otherwise, change TLP Type field to 5. */
  138. writel(PCIE_ATU_TYPE_CFG1, pcie->ctrl_base + PCIE_ATU_CR1);
  139. if (PCI_BUS(d) == pcie->first_busno) {
  140. /* Accessing root port configuration space. */
  141. va_address = (uintptr_t)pcie->ctrl_base;
  142. } else {
  143. writel(d << 8, pcie->ctrl_base + PCIE_ATU_LOWER_TARGET);
  144. va_address = (uintptr_t)pcie->cfg_base;
  145. }
  146. va_address += where & ~0x3;
  147. return va_address;
  148. }
  149. /**
  150. * pcie_dw_addr_valid() - Check for valid bus address
  151. *
  152. * @d: The PCI device to access
  153. * @first_busno: Bus number of the PCIe controller root complex
  154. *
  155. * Return 1 (true) if the PCI device can be accessed by this controller.
  156. *
  157. * Return: 1 on valid, 0 on invalid
  158. */
  159. static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
  160. {
  161. if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
  162. return 0;
  163. if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
  164. return 0;
  165. return 1;
  166. }
  167. /**
  168. * pcie_dw_mvebu_read_config() - Read from configuration space
  169. *
  170. * @bus: Pointer to the PCI bus
  171. * @bdf: Identifies the PCIe device to access
  172. * @offset: The offset into the device's configuration space
  173. * @valuep: A pointer at which to store the read value
  174. * @size: Indicates the size of access to perform
  175. *
  176. * Read a value of size @size from offset @offset within the configuration
  177. * space of the device identified by the bus, device & function numbers in @bdf
  178. * on the PCI bus @bus.
  179. *
  180. * Return: 0 on success
  181. */
  182. static int pcie_dw_mvebu_read_config(struct udevice *bus, pci_dev_t bdf,
  183. uint offset, ulong *valuep,
  184. enum pci_size_t size)
  185. {
  186. struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
  187. uintptr_t va_address;
  188. ulong value;
  189. debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
  190. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  191. if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
  192. debug("- out of range\n");
  193. *valuep = pci_get_ff(size);
  194. return 0;
  195. }
  196. va_address = set_cfg_address(pcie, bdf, offset);
  197. value = readl(va_address);
  198. debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
  199. *valuep = pci_conv_32_to_size(value, offset, size);
  200. return 0;
  201. }
  202. /**
  203. * pcie_dw_mvebu_write_config() - Write to configuration space
  204. *
  205. * @bus: Pointer to the PCI bus
  206. * @bdf: Identifies the PCIe device to access
  207. * @offset: The offset into the device's configuration space
  208. * @value: The value to write
  209. * @size: Indicates the size of access to perform
  210. *
  211. * Write the value @value of size @size from offset @offset within the
  212. * configuration space of the device identified by the bus, device & function
  213. * numbers in @bdf on the PCI bus @bus.
  214. *
  215. * Return: 0 on success
  216. */
  217. static int pcie_dw_mvebu_write_config(struct udevice *bus, pci_dev_t bdf,
  218. uint offset, ulong value,
  219. enum pci_size_t size)
  220. {
  221. struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
  222. uintptr_t va_address;
  223. ulong old;
  224. debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
  225. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  226. debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
  227. if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
  228. debug("- out of range\n");
  229. return 0;
  230. }
  231. va_address = set_cfg_address(pcie, bdf, offset);
  232. old = readl(va_address);
  233. value = pci_conv_size_to_32(old, value, offset, size);
  234. writel(value, va_address);
  235. return 0;
  236. }
  237. /**
  238. * pcie_dw_configure() - Configure link capabilities and speed
  239. *
  240. * @regs_base: A pointer to the PCIe controller registers
  241. * @cap_speed: The capabilities and speed to configure
  242. *
  243. * Configure the link capabilities and speed in the PCIe root complex.
  244. */
  245. static void pcie_dw_configure(const void *regs_base, u32 cap_speed)
  246. {
  247. /*
  248. * TODO (shadi@marvell.com, sr@denx.de):
  249. * Need to read the serdes speed from the dts and according to it
  250. * configure the PCIe gen
  251. */
  252. /* Set link to GEN 3 */
  253. clrsetbits_le32(regs_base + PCIE_LINK_CTL_2,
  254. TARGET_LINK_SPEED_MASK, cap_speed);
  255. clrsetbits_le32(regs_base + PCIE_LINK_CAPABILITY,
  256. TARGET_LINK_SPEED_MASK, cap_speed);
  257. setbits_le32(regs_base + PCIE_GEN3_EQU_CTRL, GEN3_EQU_EVAL_2MS_DISABLE);
  258. }
  259. /**
  260. * is_link_up() - Return the link state
  261. *
  262. * @regs_base: A pointer to the PCIe controller registers
  263. *
  264. * Return: 1 (true) for active line and 0 (false) for no link
  265. */
  266. static int is_link_up(const void *regs_base)
  267. {
  268. u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
  269. u32 reg;
  270. reg = readl(regs_base + PCIE_GLOBAL_STATUS);
  271. if ((reg & mask) == mask)
  272. return 1;
  273. return 0;
  274. }
  275. /**
  276. * wait_link_up() - Wait for the link to come up
  277. *
  278. * @regs_base: A pointer to the PCIe controller registers
  279. *
  280. * Return: 1 (true) for active line and 0 (false) for no link (timeout)
  281. */
  282. static int wait_link_up(const void *regs_base)
  283. {
  284. unsigned long timeout;
  285. timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
  286. while (!is_link_up(regs_base)) {
  287. if (get_timer(0) > timeout)
  288. return 0;
  289. };
  290. return 1;
  291. }
  292. /**
  293. * pcie_dw_mvebu_pcie_link_up() - Configure the PCIe root port
  294. *
  295. * @regs_base: A pointer to the PCIe controller registers
  296. * @cap_speed: The capabilities and speed to configure
  297. *
  298. * Configure the PCIe controller root complex depending on the
  299. * requested link capabilities and speed.
  300. *
  301. * Return: 1 (true) for active line and 0 (false) for no link
  302. */
  303. static int pcie_dw_mvebu_pcie_link_up(const void *regs_base, u32 cap_speed)
  304. {
  305. if (!is_link_up(regs_base)) {
  306. /* Disable LTSSM state machine to enable configuration */
  307. clrbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
  308. PCIE_APP_LTSSM_EN);
  309. }
  310. clrsetbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
  311. PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_OFFSET,
  312. PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_OFFSET);
  313. /* Set the PCIe master AXI attributes */
  314. writel(ARCACHE_SHAREABLE_CACHEABLE, regs_base + PCIE_ARCACHE_TRC);
  315. writel(AWCACHE_SHAREABLE_CACHEABLE, regs_base + PCIE_AWCACHE_TRC);
  316. /* DW pre link configurations */
  317. pcie_dw_configure(regs_base, cap_speed);
  318. if (!is_link_up(regs_base)) {
  319. /* Configuration done. Start LTSSM */
  320. setbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
  321. PCIE_APP_LTSSM_EN);
  322. }
  323. /* Check that link was established */
  324. if (!wait_link_up(regs_base))
  325. return 0;
  326. /*
  327. * Link can be established in Gen 1. still need to wait
  328. * till MAC nagaotiation is completed
  329. */
  330. udelay(100);
  331. return 1;
  332. }
  333. /**
  334. * pcie_dw_regions_setup() - iATU region setup
  335. *
  336. * @pcie: Pointer to the PCI controller state
  337. *
  338. * Configure the iATU regions in the PCIe controller for outbound access.
  339. */
  340. static void pcie_dw_regions_setup(struct pcie_dw_mvebu *pcie)
  341. {
  342. /*
  343. * Region #0 is used for Outbound CFG space access.
  344. * Direction = Outbound
  345. * Region Index = 0
  346. */
  347. writel(0, pcie->ctrl_base + PCIE_ATU_VIEWPORT);
  348. writel((u32)(uintptr_t)pcie->cfg_base, pcie->ctrl_base
  349. + PCIE_ATU_LOWER_BASE);
  350. writel(0, pcie->ctrl_base + PCIE_ATU_UPPER_BASE);
  351. writel((u32)(uintptr_t)pcie->cfg_base + pcie->cfg_size,
  352. pcie->ctrl_base + PCIE_ATU_LIMIT);
  353. writel(0, pcie->ctrl_base + PCIE_ATU_LOWER_TARGET);
  354. writel(0, pcie->ctrl_base + PCIE_ATU_UPPER_TARGET);
  355. writel(PCIE_ATU_TYPE_CFG0, pcie->ctrl_base + PCIE_ATU_CR1);
  356. writel(PCIE_ATU_ENABLE, pcie->ctrl_base + PCIE_ATU_CR2);
  357. }
  358. /**
  359. * pcie_dw_set_host_bars() - Configure the host BARs
  360. *
  361. * @regs_base: A pointer to the PCIe controller registers
  362. *
  363. * Configure the host BARs of the PCIe controller root port so that
  364. * PCI(e) devices may access the system memory.
  365. */
  366. static void pcie_dw_set_host_bars(const void *regs_base)
  367. {
  368. u32 size = gd->ram_size;
  369. u64 max_size;
  370. u32 reg;
  371. u32 bar0;
  372. /* Verify the maximal BAR size */
  373. reg = readl(regs_base + RESIZABLE_BAR_CAP);
  374. max_size = 1ULL << (5 + (reg + (1 << 4)));
  375. if (size > max_size) {
  376. size = max_size;
  377. printf("Warning: PCIe BARs can't map all DRAM space\n");
  378. }
  379. /* Set the BAR base and size towards DDR */
  380. bar0 = CONFIG_SYS_SDRAM_BASE & ~0xf;
  381. bar0 |= PCI_BASE_ADDRESS_MEM_TYPE_32;
  382. writel(CONFIG_SYS_SDRAM_BASE, regs_base + PCIE_CONFIG_BAR0);
  383. reg = ((size >> 20) - 1) << 12;
  384. writel(size, regs_base + RESIZABLE_BAR_CTL0);
  385. }
  386. /**
  387. * pcie_dw_mvebu_probe() - Probe the PCIe bus for active link
  388. *
  389. * @dev: A pointer to the device being operated on
  390. *
  391. * Probe for an active link on the PCIe bus and configure the controller
  392. * to enable this port.
  393. *
  394. * Return: 0 on success, else -ENODEV
  395. */
  396. static int pcie_dw_mvebu_probe(struct udevice *dev)
  397. {
  398. struct pcie_dw_mvebu *pcie = dev_get_priv(dev);
  399. struct udevice *ctlr = pci_get_controller(dev);
  400. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  401. pcie->first_busno = dev->seq;
  402. /* Don't register host if link is down */
  403. if (!pcie_dw_mvebu_pcie_link_up(pcie->ctrl_base, LINK_SPEED_GEN_3)) {
  404. printf("PCIE-%d: Link down\n", dev->seq);
  405. return -ENODEV;
  406. }
  407. printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev->seq,
  408. pcie_dw_get_link_speed(pcie->ctrl_base),
  409. pcie_dw_get_link_width(pcie->ctrl_base), hose->first_busno);
  410. pcie_dw_regions_setup(pcie);
  411. /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI */
  412. clrsetbits_le32(pcie->ctrl_base + PCI_CLASS_REVISION,
  413. 0xffff << 16, PCI_CLASS_BRIDGE_PCI << 16);
  414. pcie_dw_set_host_bars(pcie->ctrl_base);
  415. return 0;
  416. }
  417. /**
  418. * pcie_dw_mvebu_ofdata_to_platdata() - Translate from DT to device state
  419. *
  420. * @dev: A pointer to the device being operated on
  421. *
  422. * Translate relevant data from the device tree pertaining to device @dev into
  423. * state that the driver will later make use of. This state is stored in the
  424. * device's private data structure.
  425. *
  426. * Return: 0 on success, else -EINVAL
  427. */
  428. static int pcie_dw_mvebu_ofdata_to_platdata(struct udevice *dev)
  429. {
  430. struct pcie_dw_mvebu *pcie = dev_get_priv(dev);
  431. /* Get the controller base address */
  432. pcie->ctrl_base = (void *)dev_get_addr_index(dev, 0);
  433. if ((fdt_addr_t)pcie->ctrl_base == FDT_ADDR_T_NONE)
  434. return -EINVAL;
  435. /* Get the config space base address and size */
  436. pcie->cfg_base = (void *)dev_get_addr_size_index(dev, 1,
  437. &pcie->cfg_size);
  438. if ((fdt_addr_t)pcie->cfg_base == FDT_ADDR_T_NONE)
  439. return -EINVAL;
  440. return 0;
  441. }
  442. static const struct dm_pci_ops pcie_dw_mvebu_ops = {
  443. .read_config = pcie_dw_mvebu_read_config,
  444. .write_config = pcie_dw_mvebu_write_config,
  445. };
  446. static const struct udevice_id pcie_dw_mvebu_ids[] = {
  447. { .compatible = "marvell,armada8k-pcie" },
  448. { }
  449. };
  450. U_BOOT_DRIVER(pcie_dw_mvebu) = {
  451. .name = "pcie_dw_mvebu",
  452. .id = UCLASS_PCI,
  453. .of_match = pcie_dw_mvebu_ids,
  454. .ops = &pcie_dw_mvebu_ops,
  455. .ofdata_to_platdata = pcie_dw_mvebu_ofdata_to_platdata,
  456. .probe = pcie_dw_mvebu_probe,
  457. .priv_auto_alloc_size = sizeof(struct pcie_dw_mvebu),
  458. };