pcie-designware.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Synopsys DesignWare PCIe host controller driver
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * https://www.samsung.com
  7. *
  8. * Author: Jingoo Han <[email protected]>
  9. */
  10. #include <linux/align.h>
  11. #include <linux/bitops.h>
  12. #include <linux/delay.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/sizes.h>
  16. #include <linux/types.h>
  17. #include "../../pci.h"
  18. #include "pcie-designware.h"
  19. void dw_pcie_version_detect(struct dw_pcie *pci)
  20. {
  21. u32 ver;
  22. /* The content of the CSR is zero on DWC PCIe older than v4.70a */
  23. ver = dw_pcie_readl_dbi(pci, PCIE_VERSION_NUMBER);
  24. if (!ver)
  25. return;
  26. if (pci->version && pci->version != ver)
  27. dev_warn(pci->dev, "Versions don't match (%08x != %08x)\n",
  28. pci->version, ver);
  29. else
  30. pci->version = ver;
  31. ver = dw_pcie_readl_dbi(pci, PCIE_VERSION_TYPE);
  32. if (pci->type && pci->type != ver)
  33. dev_warn(pci->dev, "Types don't match (%08x != %08x)\n",
  34. pci->type, ver);
  35. else
  36. pci->type = ver;
  37. }
  38. /*
  39. * These interfaces resemble the pci_find_*capability() interfaces, but these
  40. * are for configuring host controllers, which are bridges *to* PCI devices but
  41. * are not PCI devices themselves.
  42. */
  43. static u8 __dw_pcie_find_next_cap(struct dw_pcie *pci, u8 cap_ptr,
  44. u8 cap)
  45. {
  46. u8 cap_id, next_cap_ptr;
  47. u16 reg;
  48. if (!cap_ptr)
  49. return 0;
  50. reg = dw_pcie_readw_dbi(pci, cap_ptr);
  51. cap_id = (reg & 0x00ff);
  52. if (cap_id > PCI_CAP_ID_MAX)
  53. return 0;
  54. if (cap_id == cap)
  55. return cap_ptr;
  56. next_cap_ptr = (reg & 0xff00) >> 8;
  57. return __dw_pcie_find_next_cap(pci, next_cap_ptr, cap);
  58. }
  59. u8 dw_pcie_find_capability(struct dw_pcie *pci, u8 cap)
  60. {
  61. u8 next_cap_ptr;
  62. u16 reg;
  63. reg = dw_pcie_readw_dbi(pci, PCI_CAPABILITY_LIST);
  64. next_cap_ptr = (reg & 0x00ff);
  65. return __dw_pcie_find_next_cap(pci, next_cap_ptr, cap);
  66. }
  67. EXPORT_SYMBOL_GPL(dw_pcie_find_capability);
  68. static u16 dw_pcie_find_next_ext_capability(struct dw_pcie *pci, u16 start,
  69. u8 cap)
  70. {
  71. u32 header;
  72. int ttl;
  73. int pos = PCI_CFG_SPACE_SIZE;
  74. /* minimum 8 bytes per capability */
  75. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  76. if (start)
  77. pos = start;
  78. header = dw_pcie_readl_dbi(pci, pos);
  79. /*
  80. * If we have no capabilities, this is indicated by cap ID,
  81. * cap version and next pointer all being 0.
  82. */
  83. if (header == 0)
  84. return 0;
  85. while (ttl-- > 0) {
  86. if (PCI_EXT_CAP_ID(header) == cap && pos != start)
  87. return pos;
  88. pos = PCI_EXT_CAP_NEXT(header);
  89. if (pos < PCI_CFG_SPACE_SIZE)
  90. break;
  91. header = dw_pcie_readl_dbi(pci, pos);
  92. }
  93. return 0;
  94. }
  95. u16 dw_pcie_find_ext_capability(struct dw_pcie *pci, u8 cap)
  96. {
  97. return dw_pcie_find_next_ext_capability(pci, 0, cap);
  98. }
  99. EXPORT_SYMBOL_GPL(dw_pcie_find_ext_capability);
  100. int dw_pcie_read(void __iomem *addr, int size, u32 *val)
  101. {
  102. if (!IS_ALIGNED((uintptr_t)addr, size)) {
  103. *val = 0;
  104. return PCIBIOS_BAD_REGISTER_NUMBER;
  105. }
  106. if (size == 4) {
  107. *val = readl(addr);
  108. } else if (size == 2) {
  109. *val = readw(addr);
  110. } else if (size == 1) {
  111. *val = readb(addr);
  112. } else {
  113. *val = 0;
  114. return PCIBIOS_BAD_REGISTER_NUMBER;
  115. }
  116. return PCIBIOS_SUCCESSFUL;
  117. }
  118. EXPORT_SYMBOL_GPL(dw_pcie_read);
  119. int dw_pcie_write(void __iomem *addr, int size, u32 val)
  120. {
  121. if (!IS_ALIGNED((uintptr_t)addr, size))
  122. return PCIBIOS_BAD_REGISTER_NUMBER;
  123. if (size == 4)
  124. writel(val, addr);
  125. else if (size == 2)
  126. writew(val, addr);
  127. else if (size == 1)
  128. writeb(val, addr);
  129. else
  130. return PCIBIOS_BAD_REGISTER_NUMBER;
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. EXPORT_SYMBOL_GPL(dw_pcie_write);
  134. u32 dw_pcie_read_dbi(struct dw_pcie *pci, u32 reg, size_t size)
  135. {
  136. int ret;
  137. u32 val;
  138. if (pci->ops && pci->ops->read_dbi)
  139. return pci->ops->read_dbi(pci, pci->dbi_base, reg, size);
  140. ret = dw_pcie_read(pci->dbi_base + reg, size, &val);
  141. if (ret)
  142. dev_err(pci->dev, "Read DBI address failed\n");
  143. return val;
  144. }
  145. EXPORT_SYMBOL_GPL(dw_pcie_read_dbi);
  146. void dw_pcie_write_dbi(struct dw_pcie *pci, u32 reg, size_t size, u32 val)
  147. {
  148. int ret;
  149. if (pci->ops && pci->ops->write_dbi) {
  150. pci->ops->write_dbi(pci, pci->dbi_base, reg, size, val);
  151. return;
  152. }
  153. ret = dw_pcie_write(pci->dbi_base + reg, size, val);
  154. if (ret)
  155. dev_err(pci->dev, "Write DBI address failed\n");
  156. }
  157. EXPORT_SYMBOL_GPL(dw_pcie_write_dbi);
  158. void dw_pcie_write_dbi2(struct dw_pcie *pci, u32 reg, size_t size, u32 val)
  159. {
  160. int ret;
  161. if (pci->ops && pci->ops->write_dbi2) {
  162. pci->ops->write_dbi2(pci, pci->dbi_base2, reg, size, val);
  163. return;
  164. }
  165. ret = dw_pcie_write(pci->dbi_base2 + reg, size, val);
  166. if (ret)
  167. dev_err(pci->dev, "write DBI address failed\n");
  168. }
  169. static inline void __iomem *dw_pcie_select_atu(struct dw_pcie *pci, u32 dir,
  170. u32 index)
  171. {
  172. if (pci->iatu_unroll_enabled)
  173. return pci->atu_base + PCIE_ATU_UNROLL_BASE(dir, index);
  174. dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, dir | index);
  175. return pci->atu_base;
  176. }
  177. static u32 dw_pcie_readl_atu(struct dw_pcie *pci, u32 dir, u32 index, u32 reg)
  178. {
  179. void __iomem *base;
  180. int ret;
  181. u32 val;
  182. base = dw_pcie_select_atu(pci, dir, index);
  183. if (pci->ops && pci->ops->read_dbi)
  184. return pci->ops->read_dbi(pci, base, reg, 4);
  185. ret = dw_pcie_read(base + reg, 4, &val);
  186. if (ret)
  187. dev_err(pci->dev, "Read ATU address failed\n");
  188. return val;
  189. }
  190. static void dw_pcie_writel_atu(struct dw_pcie *pci, u32 dir, u32 index,
  191. u32 reg, u32 val)
  192. {
  193. void __iomem *base;
  194. int ret;
  195. base = dw_pcie_select_atu(pci, dir, index);
  196. if (pci->ops && pci->ops->write_dbi) {
  197. pci->ops->write_dbi(pci, base, reg, 4, val);
  198. return;
  199. }
  200. ret = dw_pcie_write(base + reg, 4, val);
  201. if (ret)
  202. dev_err(pci->dev, "Write ATU address failed\n");
  203. }
  204. static inline u32 dw_pcie_readl_atu_ob(struct dw_pcie *pci, u32 index, u32 reg)
  205. {
  206. return dw_pcie_readl_atu(pci, PCIE_ATU_REGION_DIR_OB, index, reg);
  207. }
  208. static inline void dw_pcie_writel_atu_ob(struct dw_pcie *pci, u32 index, u32 reg,
  209. u32 val)
  210. {
  211. dw_pcie_writel_atu(pci, PCIE_ATU_REGION_DIR_OB, index, reg, val);
  212. }
  213. static inline u32 dw_pcie_enable_ecrc(u32 val)
  214. {
  215. /*
  216. * DesignWare core version 4.90A has a design issue where the 'TD'
  217. * bit in the Control register-1 of the ATU outbound region acts
  218. * like an override for the ECRC setting, i.e., the presence of TLP
  219. * Digest (ECRC) in the outgoing TLPs is solely determined by this
  220. * bit. This is contrary to the PCIe spec which says that the
  221. * enablement of the ECRC is solely determined by the AER
  222. * registers.
  223. *
  224. * Because of this, even when the ECRC is enabled through AER
  225. * registers, the transactions going through ATU won't have TLP
  226. * Digest as there is no way the PCI core AER code could program
  227. * the TD bit which is specific to the DesignWare core.
  228. *
  229. * The best way to handle this scenario is to program the TD bit
  230. * always. It affects only the traffic from root port to downstream
  231. * devices.
  232. *
  233. * At this point,
  234. * When ECRC is enabled in AER registers, everything works normally
  235. * When ECRC is NOT enabled in AER registers, then,
  236. * on Root Port:- TLP Digest (DWord size) gets appended to each packet
  237. * even through it is not required. Since downstream
  238. * TLPs are mostly for configuration accesses and BAR
  239. * accesses, they are not in critical path and won't
  240. * have much negative effect on the performance.
  241. * on End Point:- TLP Digest is received for some/all the packets coming
  242. * from the root port. TLP Digest is ignored because,
  243. * as per the PCIe Spec r5.0 v1.0 section 2.2.3
  244. * "TLP Digest Rules", when an endpoint receives TLP
  245. * Digest when its ECRC check functionality is disabled
  246. * in AER registers, received TLP Digest is just ignored.
  247. * Since there is no issue or error reported either side, best way to
  248. * handle the scenario is to program TD bit by default.
  249. */
  250. return val | PCIE_ATU_TD;
  251. }
  252. static int __dw_pcie_prog_outbound_atu(struct dw_pcie *pci, u8 func_no,
  253. int index, int type, u64 cpu_addr,
  254. u64 pci_addr, u64 size)
  255. {
  256. u32 retries, val;
  257. u64 limit_addr;
  258. if (pci->ops && pci->ops->cpu_addr_fixup)
  259. cpu_addr = pci->ops->cpu_addr_fixup(pci, cpu_addr);
  260. limit_addr = cpu_addr + size - 1;
  261. if ((limit_addr & ~pci->region_limit) != (cpu_addr & ~pci->region_limit) ||
  262. !IS_ALIGNED(cpu_addr, pci->region_align) ||
  263. !IS_ALIGNED(pci_addr, pci->region_align) || !size) {
  264. return -EINVAL;
  265. }
  266. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_LOWER_BASE,
  267. lower_32_bits(cpu_addr));
  268. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_UPPER_BASE,
  269. upper_32_bits(cpu_addr));
  270. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_LIMIT,
  271. lower_32_bits(limit_addr));
  272. if (dw_pcie_ver_is_ge(pci, 460A))
  273. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_UPPER_LIMIT,
  274. upper_32_bits(limit_addr));
  275. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_LOWER_TARGET,
  276. lower_32_bits(pci_addr));
  277. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_UPPER_TARGET,
  278. upper_32_bits(pci_addr));
  279. val = type | PCIE_ATU_FUNC_NUM(func_no);
  280. if (upper_32_bits(limit_addr) > upper_32_bits(cpu_addr) &&
  281. dw_pcie_ver_is_ge(pci, 460A))
  282. val |= PCIE_ATU_INCREASE_REGION_SIZE;
  283. if (dw_pcie_ver_is(pci, 490A))
  284. val = dw_pcie_enable_ecrc(val);
  285. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_REGION_CTRL1, val);
  286. dw_pcie_writel_atu_ob(pci, index, PCIE_ATU_REGION_CTRL2, PCIE_ATU_ENABLE);
  287. /*
  288. * Make sure ATU enable takes effect before any subsequent config
  289. * and I/O accesses.
  290. */
  291. for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
  292. val = dw_pcie_readl_atu_ob(pci, index, PCIE_ATU_REGION_CTRL2);
  293. if (val & PCIE_ATU_ENABLE)
  294. return 0;
  295. mdelay(LINK_WAIT_IATU);
  296. }
  297. dev_err(pci->dev, "Outbound iATU is not being enabled\n");
  298. return -ETIMEDOUT;
  299. }
  300. int dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index, int type,
  301. u64 cpu_addr, u64 pci_addr, u64 size)
  302. {
  303. return __dw_pcie_prog_outbound_atu(pci, 0, index, type,
  304. cpu_addr, pci_addr, size);
  305. }
  306. int dw_pcie_prog_ep_outbound_atu(struct dw_pcie *pci, u8 func_no, int index,
  307. int type, u64 cpu_addr, u64 pci_addr,
  308. u64 size)
  309. {
  310. return __dw_pcie_prog_outbound_atu(pci, func_no, index, type,
  311. cpu_addr, pci_addr, size);
  312. }
  313. static inline u32 dw_pcie_readl_atu_ib(struct dw_pcie *pci, u32 index, u32 reg)
  314. {
  315. return dw_pcie_readl_atu(pci, PCIE_ATU_REGION_DIR_IB, index, reg);
  316. }
  317. static inline void dw_pcie_writel_atu_ib(struct dw_pcie *pci, u32 index, u32 reg,
  318. u32 val)
  319. {
  320. dw_pcie_writel_atu(pci, PCIE_ATU_REGION_DIR_IB, index, reg, val);
  321. }
  322. int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, u8 func_no, int index,
  323. int type, u64 cpu_addr, u8 bar)
  324. {
  325. u32 retries, val;
  326. if (!IS_ALIGNED(cpu_addr, pci->region_align))
  327. return -EINVAL;
  328. dw_pcie_writel_atu_ib(pci, index, PCIE_ATU_LOWER_TARGET,
  329. lower_32_bits(cpu_addr));
  330. dw_pcie_writel_atu_ib(pci, index, PCIE_ATU_UPPER_TARGET,
  331. upper_32_bits(cpu_addr));
  332. dw_pcie_writel_atu_ib(pci, index, PCIE_ATU_REGION_CTRL1, type |
  333. PCIE_ATU_FUNC_NUM(func_no));
  334. dw_pcie_writel_atu_ib(pci, index, PCIE_ATU_REGION_CTRL2,
  335. PCIE_ATU_ENABLE | PCIE_ATU_FUNC_NUM_MATCH_EN |
  336. PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
  337. /*
  338. * Make sure ATU enable takes effect before any subsequent config
  339. * and I/O accesses.
  340. */
  341. for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
  342. val = dw_pcie_readl_atu_ib(pci, index, PCIE_ATU_REGION_CTRL2);
  343. if (val & PCIE_ATU_ENABLE)
  344. return 0;
  345. mdelay(LINK_WAIT_IATU);
  346. }
  347. dev_err(pci->dev, "Inbound iATU is not being enabled\n");
  348. return -ETIMEDOUT;
  349. }
  350. void dw_pcie_disable_atu(struct dw_pcie *pci, u32 dir, int index)
  351. {
  352. dw_pcie_writel_atu(pci, dir, index, PCIE_ATU_REGION_CTRL2, 0);
  353. }
  354. int dw_pcie_wait_for_link(struct dw_pcie *pci)
  355. {
  356. u32 offset, val;
  357. int retries;
  358. /* Check if the link is up or not */
  359. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  360. if (dw_pcie_link_up(pci))
  361. break;
  362. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  363. }
  364. if (retries >= LINK_WAIT_MAX_RETRIES) {
  365. dev_err(pci->dev, "Phy link never came up\n");
  366. return -ETIMEDOUT;
  367. }
  368. offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
  369. val = dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKSTA);
  370. dev_info(pci->dev, "PCIe Gen.%u x%u link up\n",
  371. FIELD_GET(PCI_EXP_LNKSTA_CLS, val),
  372. FIELD_GET(PCI_EXP_LNKSTA_NLW, val));
  373. return 0;
  374. }
  375. EXPORT_SYMBOL_GPL(dw_pcie_wait_for_link);
  376. int dw_pcie_link_up(struct dw_pcie *pci)
  377. {
  378. u32 val;
  379. if (pci->ops && pci->ops->link_up)
  380. return pci->ops->link_up(pci);
  381. val = dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1);
  382. return ((val & PCIE_PORT_DEBUG1_LINK_UP) &&
  383. (!(val & PCIE_PORT_DEBUG1_LINK_IN_TRAINING)));
  384. }
  385. EXPORT_SYMBOL_GPL(dw_pcie_link_up);
  386. void dw_pcie_upconfig_setup(struct dw_pcie *pci)
  387. {
  388. u32 val;
  389. val = dw_pcie_readl_dbi(pci, PCIE_PORT_MULTI_LANE_CTRL);
  390. val |= PORT_MLTI_UPCFG_SUPPORT;
  391. dw_pcie_writel_dbi(pci, PCIE_PORT_MULTI_LANE_CTRL, val);
  392. }
  393. EXPORT_SYMBOL_GPL(dw_pcie_upconfig_setup);
  394. static void dw_pcie_link_set_max_speed(struct dw_pcie *pci, u32 link_gen)
  395. {
  396. u32 cap, ctrl2, link_speed;
  397. u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
  398. cap = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
  399. ctrl2 = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCTL2);
  400. ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
  401. switch (pcie_link_speed[link_gen]) {
  402. case PCIE_SPEED_2_5GT:
  403. link_speed = PCI_EXP_LNKCTL2_TLS_2_5GT;
  404. break;
  405. case PCIE_SPEED_5_0GT:
  406. link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT;
  407. break;
  408. case PCIE_SPEED_8_0GT:
  409. link_speed = PCI_EXP_LNKCTL2_TLS_8_0GT;
  410. break;
  411. case PCIE_SPEED_16_0GT:
  412. link_speed = PCI_EXP_LNKCTL2_TLS_16_0GT;
  413. break;
  414. default:
  415. /* Use hardware capability */
  416. link_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, cap);
  417. ctrl2 &= ~PCI_EXP_LNKCTL2_HASD;
  418. break;
  419. }
  420. dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCTL2, ctrl2 | link_speed);
  421. cap &= ~((u32)PCI_EXP_LNKCAP_SLS);
  422. dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, cap | link_speed);
  423. }
  424. static bool dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci)
  425. {
  426. u32 val;
  427. val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT);
  428. if (val == 0xffffffff)
  429. return true;
  430. return false;
  431. }
  432. static void dw_pcie_iatu_detect_regions(struct dw_pcie *pci)
  433. {
  434. int max_region, ob, ib;
  435. u32 val, min, dir;
  436. u64 max;
  437. if (pci->iatu_unroll_enabled) {
  438. max_region = min((int)pci->atu_size / 512, 256);
  439. } else {
  440. dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, 0xFF);
  441. max_region = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT) + 1;
  442. }
  443. for (ob = 0; ob < max_region; ob++) {
  444. dw_pcie_writel_atu_ob(pci, ob, PCIE_ATU_LOWER_TARGET, 0x11110000);
  445. val = dw_pcie_readl_atu_ob(pci, ob, PCIE_ATU_LOWER_TARGET);
  446. if (val != 0x11110000)
  447. break;
  448. }
  449. for (ib = 0; ib < max_region; ib++) {
  450. dw_pcie_writel_atu_ib(pci, ib, PCIE_ATU_LOWER_TARGET, 0x11110000);
  451. val = dw_pcie_readl_atu_ib(pci, ib, PCIE_ATU_LOWER_TARGET);
  452. if (val != 0x11110000)
  453. break;
  454. }
  455. if (ob) {
  456. dir = PCIE_ATU_REGION_DIR_OB;
  457. } else if (ib) {
  458. dir = PCIE_ATU_REGION_DIR_IB;
  459. } else {
  460. dev_err(pci->dev, "No iATU regions found\n");
  461. return;
  462. }
  463. dw_pcie_writel_atu(pci, dir, 0, PCIE_ATU_LIMIT, 0x0);
  464. min = dw_pcie_readl_atu(pci, dir, 0, PCIE_ATU_LIMIT);
  465. if (dw_pcie_ver_is_ge(pci, 460A)) {
  466. dw_pcie_writel_atu(pci, dir, 0, PCIE_ATU_UPPER_LIMIT, 0xFFFFFFFF);
  467. max = dw_pcie_readl_atu(pci, dir, 0, PCIE_ATU_UPPER_LIMIT);
  468. } else {
  469. max = 0;
  470. }
  471. pci->num_ob_windows = ob;
  472. pci->num_ib_windows = ib;
  473. pci->region_align = 1 << fls(min);
  474. pci->region_limit = (max << 32) | (SZ_4G - 1);
  475. }
  476. void dw_pcie_iatu_detect(struct dw_pcie *pci)
  477. {
  478. struct platform_device *pdev = to_platform_device(pci->dev);
  479. pci->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pci);
  480. if (pci->iatu_unroll_enabled) {
  481. if (!pci->atu_base) {
  482. struct resource *res =
  483. platform_get_resource_byname(pdev, IORESOURCE_MEM, "atu");
  484. if (res) {
  485. pci->atu_size = resource_size(res);
  486. pci->atu_base = devm_ioremap_resource(pci->dev, res);
  487. }
  488. if (!pci->atu_base || IS_ERR(pci->atu_base))
  489. pci->atu_base = pci->dbi_base + DEFAULT_DBI_ATU_OFFSET;
  490. }
  491. if (!pci->atu_size)
  492. /* Pick a minimal default, enough for 8 in and 8 out windows */
  493. pci->atu_size = SZ_4K;
  494. } else {
  495. pci->atu_base = pci->dbi_base + PCIE_ATU_VIEWPORT_BASE;
  496. pci->atu_size = PCIE_ATU_VIEWPORT_SIZE;
  497. }
  498. dw_pcie_iatu_detect_regions(pci);
  499. dev_info(pci->dev, "iATU unroll: %s\n", pci->iatu_unroll_enabled ?
  500. "enabled" : "disabled");
  501. dev_info(pci->dev, "iATU regions: %u ob, %u ib, align %uK, limit %lluG\n",
  502. pci->num_ob_windows, pci->num_ib_windows,
  503. pci->region_align / SZ_1K, (pci->region_limit + 1) / SZ_1G);
  504. }
  505. void dw_pcie_setup(struct dw_pcie *pci)
  506. {
  507. struct device_node *np = pci->dev->of_node;
  508. u32 val;
  509. if (pci->link_gen > 0)
  510. dw_pcie_link_set_max_speed(pci, pci->link_gen);
  511. /* Configure Gen1 N_FTS */
  512. if (pci->n_fts[0]) {
  513. val = dw_pcie_readl_dbi(pci, PCIE_PORT_AFR);
  514. val &= ~(PORT_AFR_N_FTS_MASK | PORT_AFR_CC_N_FTS_MASK);
  515. val |= PORT_AFR_N_FTS(pci->n_fts[0]);
  516. val |= PORT_AFR_CC_N_FTS(pci->n_fts[0]);
  517. dw_pcie_writel_dbi(pci, PCIE_PORT_AFR, val);
  518. }
  519. /* Configure Gen2+ N_FTS */
  520. if (pci->n_fts[1]) {
  521. val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  522. val &= ~PORT_LOGIC_N_FTS_MASK;
  523. val |= pci->n_fts[1];
  524. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
  525. }
  526. if (of_property_read_bool(np, "snps,enable-cdm-check")) {
  527. val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS);
  528. val |= PCIE_PL_CHK_REG_CHK_REG_CONTINUOUS |
  529. PCIE_PL_CHK_REG_CHK_REG_START;
  530. dw_pcie_writel_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS, val);
  531. }
  532. val = dw_pcie_readl_dbi(pci, PCIE_PORT_LINK_CONTROL);
  533. val &= ~PORT_LINK_FAST_LINK_MODE;
  534. val |= PORT_LINK_DLL_LINK_EN;
  535. dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val);
  536. of_property_read_u32(np, "num-lanes", &pci->num_lanes);
  537. if (!pci->num_lanes) {
  538. dev_dbg(pci->dev, "Using h/w default number of lanes\n");
  539. return;
  540. }
  541. /* Set the number of lanes */
  542. val &= ~PORT_LINK_FAST_LINK_MODE;
  543. val &= ~PORT_LINK_MODE_MASK;
  544. switch (pci->num_lanes) {
  545. case 1:
  546. val |= PORT_LINK_MODE_1_LANES;
  547. break;
  548. case 2:
  549. val |= PORT_LINK_MODE_2_LANES;
  550. break;
  551. case 4:
  552. val |= PORT_LINK_MODE_4_LANES;
  553. break;
  554. case 8:
  555. val |= PORT_LINK_MODE_8_LANES;
  556. break;
  557. default:
  558. dev_err(pci->dev, "num-lanes %u: invalid value\n", pci->num_lanes);
  559. return;
  560. }
  561. dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val);
  562. /* Set link width speed control register */
  563. val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  564. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  565. switch (pci->num_lanes) {
  566. case 1:
  567. val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
  568. break;
  569. case 2:
  570. val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
  571. break;
  572. case 4:
  573. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  574. break;
  575. case 8:
  576. val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
  577. break;
  578. }
  579. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
  580. }