pcie-bt1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Vadim Vlasov <Vadim.Vlasov@baikalelectronics.ru>
  7. * Serge Semin <Sergey.Semin@baikalelectronics.ru>
  8. *
  9. * Baikal-T1 PCIe controller driver
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/bits.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <linux/types.h>
  24. #include "pcie-designware.h"
  25. /* Baikal-T1 System CCU control registers */
  26. #define BT1_CCU_PCIE_CLKC 0x140
  27. #define BT1_CCU_PCIE_REQ_PCS_CLK BIT(16)
  28. #define BT1_CCU_PCIE_REQ_MAC_CLK BIT(17)
  29. #define BT1_CCU_PCIE_REQ_PIPE_CLK BIT(18)
  30. #define BT1_CCU_PCIE_RSTC 0x144
  31. #define BT1_CCU_PCIE_REQ_LINK_RST BIT(13)
  32. #define BT1_CCU_PCIE_REQ_SMLH_RST BIT(14)
  33. #define BT1_CCU_PCIE_REQ_PHY_RST BIT(16)
  34. #define BT1_CCU_PCIE_REQ_CORE_RST BIT(24)
  35. #define BT1_CCU_PCIE_REQ_STICKY_RST BIT(26)
  36. #define BT1_CCU_PCIE_REQ_NSTICKY_RST BIT(27)
  37. #define BT1_CCU_PCIE_PMSC 0x148
  38. #define BT1_CCU_PCIE_LTSSM_STATE_MASK GENMASK(5, 0)
  39. #define BT1_CCU_PCIE_LTSSM_DET_QUIET 0x00
  40. #define BT1_CCU_PCIE_LTSSM_DET_ACT 0x01
  41. #define BT1_CCU_PCIE_LTSSM_POLL_ACT 0x02
  42. #define BT1_CCU_PCIE_LTSSM_POLL_COMP 0x03
  43. #define BT1_CCU_PCIE_LTSSM_POLL_CONF 0x04
  44. #define BT1_CCU_PCIE_LTSSM_PRE_DET_QUIET 0x05
  45. #define BT1_CCU_PCIE_LTSSM_DET_WAIT 0x06
  46. #define BT1_CCU_PCIE_LTSSM_CFG_LNKWD_START 0x07
  47. #define BT1_CCU_PCIE_LTSSM_CFG_LNKWD_ACEPT 0x08
  48. #define BT1_CCU_PCIE_LTSSM_CFG_LNNUM_WAIT 0x09
  49. #define BT1_CCU_PCIE_LTSSM_CFG_LNNUM_ACEPT 0x0a
  50. #define BT1_CCU_PCIE_LTSSM_CFG_COMPLETE 0x0b
  51. #define BT1_CCU_PCIE_LTSSM_CFG_IDLE 0x0c
  52. #define BT1_CCU_PCIE_LTSSM_RCVR_LOCK 0x0d
  53. #define BT1_CCU_PCIE_LTSSM_RCVR_SPEED 0x0e
  54. #define BT1_CCU_PCIE_LTSSM_RCVR_RCVRCFG 0x0f
  55. #define BT1_CCU_PCIE_LTSSM_RCVR_IDLE 0x10
  56. #define BT1_CCU_PCIE_LTSSM_L0 0x11
  57. #define BT1_CCU_PCIE_LTSSM_L0S 0x12
  58. #define BT1_CCU_PCIE_LTSSM_L123_SEND_IDLE 0x13
  59. #define BT1_CCU_PCIE_LTSSM_L1_IDLE 0x14
  60. #define BT1_CCU_PCIE_LTSSM_L2_IDLE 0x15
  61. #define BT1_CCU_PCIE_LTSSM_L2_WAKE 0x16
  62. #define BT1_CCU_PCIE_LTSSM_DIS_ENTRY 0x17
  63. #define BT1_CCU_PCIE_LTSSM_DIS_IDLE 0x18
  64. #define BT1_CCU_PCIE_LTSSM_DISABLE 0x19
  65. #define BT1_CCU_PCIE_LTSSM_LPBK_ENTRY 0x1a
  66. #define BT1_CCU_PCIE_LTSSM_LPBK_ACTIVE 0x1b
  67. #define BT1_CCU_PCIE_LTSSM_LPBK_EXIT 0x1c
  68. #define BT1_CCU_PCIE_LTSSM_LPBK_EXIT_TOUT 0x1d
  69. #define BT1_CCU_PCIE_LTSSM_HOT_RST_ENTRY 0x1e
  70. #define BT1_CCU_PCIE_LTSSM_HOT_RST 0x1f
  71. #define BT1_CCU_PCIE_LTSSM_RCVR_EQ0 0x20
  72. #define BT1_CCU_PCIE_LTSSM_RCVR_EQ1 0x21
  73. #define BT1_CCU_PCIE_LTSSM_RCVR_EQ2 0x22
  74. #define BT1_CCU_PCIE_LTSSM_RCVR_EQ3 0x23
  75. #define BT1_CCU_PCIE_SMLH_LINKUP BIT(6)
  76. #define BT1_CCU_PCIE_RDLH_LINKUP BIT(7)
  77. #define BT1_CCU_PCIE_PM_LINKSTATE_L0S BIT(8)
  78. #define BT1_CCU_PCIE_PM_LINKSTATE_L1 BIT(9)
  79. #define BT1_CCU_PCIE_PM_LINKSTATE_L2 BIT(10)
  80. #define BT1_CCU_PCIE_L1_PENDING BIT(12)
  81. #define BT1_CCU_PCIE_REQ_EXIT_L1 BIT(14)
  82. #define BT1_CCU_PCIE_LTSSM_RCVR_EQ BIT(15)
  83. #define BT1_CCU_PCIE_PM_DSTAT_MASK GENMASK(18, 16)
  84. #define BT1_CCU_PCIE_PM_PME_EN BIT(20)
  85. #define BT1_CCU_PCIE_PM_PME_STATUS BIT(21)
  86. #define BT1_CCU_PCIE_AUX_PM_EN BIT(22)
  87. #define BT1_CCU_PCIE_AUX_PWR_DET BIT(23)
  88. #define BT1_CCU_PCIE_WAKE_DET BIT(24)
  89. #define BT1_CCU_PCIE_TURNOFF_REQ BIT(30)
  90. #define BT1_CCU_PCIE_TURNOFF_ACK BIT(31)
  91. #define BT1_CCU_PCIE_GENC 0x14c
  92. #define BT1_CCU_PCIE_LTSSM_EN BIT(1)
  93. #define BT1_CCU_PCIE_DBI2_MODE BIT(2)
  94. #define BT1_CCU_PCIE_MGMT_EN BIT(3)
  95. #define BT1_CCU_PCIE_RXLANE_FLIP_EN BIT(16)
  96. #define BT1_CCU_PCIE_TXLANE_FLIP_EN BIT(17)
  97. #define BT1_CCU_PCIE_SLV_XFER_PEND BIT(24)
  98. #define BT1_CCU_PCIE_RCV_XFER_PEND BIT(25)
  99. #define BT1_CCU_PCIE_DBI_XFER_PEND BIT(26)
  100. #define BT1_CCU_PCIE_DMA_XFER_PEND BIT(27)
  101. #define BT1_CCU_PCIE_LTSSM_LINKUP(_pmsc) \
  102. ({ \
  103. int __state = FIELD_GET(BT1_CCU_PCIE_LTSSM_STATE_MASK, _pmsc); \
  104. __state >= BT1_CCU_PCIE_LTSSM_L0 && __state <= BT1_CCU_PCIE_LTSSM_L2_WAKE; \
  105. })
  106. /* Baikal-T1 PCIe specific control registers */
  107. #define BT1_PCIE_AXI2MGM_LANENUM 0xd04
  108. #define BT1_PCIE_AXI2MGM_LANESEL_MASK GENMASK(3, 0)
  109. #define BT1_PCIE_AXI2MGM_ADDRCTL 0xd08
  110. #define BT1_PCIE_AXI2MGM_PHYREG_ADDR_MASK GENMASK(20, 0)
  111. #define BT1_PCIE_AXI2MGM_READ_FLAG BIT(29)
  112. #define BT1_PCIE_AXI2MGM_DONE BIT(30)
  113. #define BT1_PCIE_AXI2MGM_BUSY BIT(31)
  114. #define BT1_PCIE_AXI2MGM_WRITEDATA 0xd0c
  115. #define BT1_PCIE_AXI2MGM_WDATA GENMASK(15, 0)
  116. #define BT1_PCIE_AXI2MGM_READDATA 0xd10
  117. #define BT1_PCIE_AXI2MGM_RDATA GENMASK(15, 0)
  118. /* Generic Baikal-T1 PCIe interface resources */
  119. #define BT1_PCIE_NUM_APP_CLKS ARRAY_SIZE(bt1_pcie_app_clks)
  120. #define BT1_PCIE_NUM_CORE_CLKS ARRAY_SIZE(bt1_pcie_core_clks)
  121. #define BT1_PCIE_NUM_APP_RSTS ARRAY_SIZE(bt1_pcie_app_rsts)
  122. #define BT1_PCIE_NUM_CORE_RSTS ARRAY_SIZE(bt1_pcie_core_rsts)
  123. /* PCIe bus setup delays and timeouts */
  124. #define BT1_PCIE_RST_DELAY_MS 100
  125. #define BT1_PCIE_RUN_DELAY_US 100
  126. #define BT1_PCIE_REQ_DELAY_US 1
  127. #define BT1_PCIE_REQ_TIMEOUT_US 1000
  128. #define BT1_PCIE_LNK_DELAY_US 1000
  129. #define BT1_PCIE_LNK_TIMEOUT_US 1000000
  130. static const enum dw_pcie_app_clk bt1_pcie_app_clks[] = {
  131. DW_PCIE_DBI_CLK, DW_PCIE_MSTR_CLK, DW_PCIE_SLV_CLK,
  132. };
  133. static const enum dw_pcie_core_clk bt1_pcie_core_clks[] = {
  134. DW_PCIE_REF_CLK,
  135. };
  136. static const enum dw_pcie_app_rst bt1_pcie_app_rsts[] = {
  137. DW_PCIE_MSTR_RST, DW_PCIE_SLV_RST,
  138. };
  139. static const enum dw_pcie_core_rst bt1_pcie_core_rsts[] = {
  140. DW_PCIE_NON_STICKY_RST, DW_PCIE_STICKY_RST, DW_PCIE_CORE_RST,
  141. DW_PCIE_PIPE_RST, DW_PCIE_PHY_RST, DW_PCIE_HOT_RST, DW_PCIE_PWR_RST,
  142. };
  143. struct bt1_pcie {
  144. struct dw_pcie dw;
  145. struct platform_device *pdev;
  146. struct regmap *sys_regs;
  147. };
  148. #define to_bt1_pcie(_dw) container_of(_dw, struct bt1_pcie, dw)
  149. /*
  150. * Baikal-T1 MMIO space must be read/written by the dword-aligned
  151. * instructions. Note the methods are optimized to have the dword operations
  152. * performed with minimum overhead as the most frequently used ones.
  153. */
  154. static int bt1_pcie_read_mmio(void __iomem *addr, int size, u32 *val)
  155. {
  156. unsigned int ofs = (uintptr_t)addr & 0x3;
  157. if (!IS_ALIGNED((uintptr_t)addr, size))
  158. return -EINVAL;
  159. *val = readl(addr - ofs) >> ofs * BITS_PER_BYTE;
  160. if (size == 4) {
  161. return 0;
  162. } else if (size == 2) {
  163. *val &= 0xffff;
  164. return 0;
  165. } else if (size == 1) {
  166. *val &= 0xff;
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }
  171. static int bt1_pcie_write_mmio(void __iomem *addr, int size, u32 val)
  172. {
  173. unsigned int ofs = (uintptr_t)addr & 0x3;
  174. u32 tmp, mask;
  175. if (!IS_ALIGNED((uintptr_t)addr, size))
  176. return -EINVAL;
  177. if (size == 4) {
  178. writel(val, addr);
  179. return 0;
  180. } else if (size == 2 || size == 1) {
  181. mask = GENMASK(size * BITS_PER_BYTE - 1, 0);
  182. tmp = readl(addr - ofs) & ~(mask << ofs * BITS_PER_BYTE);
  183. tmp |= (val & mask) << ofs * BITS_PER_BYTE;
  184. writel(tmp, addr - ofs);
  185. return 0;
  186. }
  187. return -EINVAL;
  188. }
  189. static u32 bt1_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  190. size_t size)
  191. {
  192. int ret;
  193. u32 val;
  194. ret = bt1_pcie_read_mmio(base + reg, size, &val);
  195. if (ret) {
  196. dev_err(pci->dev, "Read DBI address failed\n");
  197. return ~0U;
  198. }
  199. return val;
  200. }
  201. static void bt1_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
  202. size_t size, u32 val)
  203. {
  204. int ret;
  205. ret = bt1_pcie_write_mmio(base + reg, size, val);
  206. if (ret)
  207. dev_err(pci->dev, "Write DBI address failed\n");
  208. }
  209. static void bt1_pcie_write_dbi2(struct dw_pcie *pci, void __iomem *base, u32 reg,
  210. size_t size, u32 val)
  211. {
  212. struct bt1_pcie *btpci = to_bt1_pcie(pci);
  213. int ret;
  214. regmap_update_bits(btpci->sys_regs, BT1_CCU_PCIE_GENC,
  215. BT1_CCU_PCIE_DBI2_MODE, BT1_CCU_PCIE_DBI2_MODE);
  216. ret = bt1_pcie_write_mmio(base + reg, size, val);
  217. if (ret)
  218. dev_err(pci->dev, "Write DBI2 address failed\n");
  219. regmap_update_bits(btpci->sys_regs, BT1_CCU_PCIE_GENC,
  220. BT1_CCU_PCIE_DBI2_MODE, 0);
  221. }
  222. static int bt1_pcie_start_link(struct dw_pcie *pci)
  223. {
  224. struct bt1_pcie *btpci = to_bt1_pcie(pci);
  225. u32 val;
  226. int ret;
  227. /*
  228. * Enable LTSSM and make sure it was able to establish both PHY and
  229. * data links. This procedure shall work fine to reach 2.5 GT/s speed.
  230. */
  231. regmap_update_bits(btpci->sys_regs, BT1_CCU_PCIE_GENC,
  232. BT1_CCU_PCIE_LTSSM_EN, BT1_CCU_PCIE_LTSSM_EN);
  233. ret = regmap_read_poll_timeout(btpci->sys_regs, BT1_CCU_PCIE_PMSC, val,
  234. (val & BT1_CCU_PCIE_SMLH_LINKUP),
  235. BT1_PCIE_LNK_DELAY_US, BT1_PCIE_LNK_TIMEOUT_US);
  236. if (ret) {
  237. dev_err(pci->dev, "LTSSM failed to set PHY link up\n");
  238. return ret;
  239. }
  240. ret = regmap_read_poll_timeout(btpci->sys_regs, BT1_CCU_PCIE_PMSC, val,
  241. (val & BT1_CCU_PCIE_RDLH_LINKUP),
  242. BT1_PCIE_LNK_DELAY_US, BT1_PCIE_LNK_TIMEOUT_US);
  243. if (ret) {
  244. dev_err(pci->dev, "LTSSM failed to set data link up\n");
  245. return ret;
  246. }
  247. /*
  248. * Activate direct speed change after the link is established in an
  249. * attempt to reach a higher bus performance (up to Gen.3 - 8.0 GT/s).
  250. * This is required at least to get 8.0 GT/s speed.
  251. */
  252. val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  253. val |= PORT_LOGIC_SPEED_CHANGE;
  254. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
  255. ret = regmap_read_poll_timeout(btpci->sys_regs, BT1_CCU_PCIE_PMSC, val,
  256. BT1_CCU_PCIE_LTSSM_LINKUP(val),
  257. BT1_PCIE_LNK_DELAY_US, BT1_PCIE_LNK_TIMEOUT_US);
  258. if (ret)
  259. dev_err(pci->dev, "LTSSM failed to get into L0 state\n");
  260. return ret;
  261. }
  262. static void bt1_pcie_stop_link(struct dw_pcie *pci)
  263. {
  264. struct bt1_pcie *btpci = to_bt1_pcie(pci);
  265. regmap_update_bits(btpci->sys_regs, BT1_CCU_PCIE_GENC,
  266. BT1_CCU_PCIE_LTSSM_EN, 0);
  267. }
  268. static const struct dw_pcie_ops bt1_pcie_ops = {
  269. .read_dbi = bt1_pcie_read_dbi,
  270. .write_dbi = bt1_pcie_write_dbi,
  271. .write_dbi2 = bt1_pcie_write_dbi2,
  272. .start_link = bt1_pcie_start_link,
  273. .stop_link = bt1_pcie_stop_link,
  274. };
  275. static struct pci_ops bt1_pci_ops = {
  276. .map_bus = dw_pcie_own_conf_map_bus,
  277. .read = pci_generic_config_read32,
  278. .write = pci_generic_config_write32,
  279. };
  280. static int bt1_pcie_get_resources(struct bt1_pcie *btpci)
  281. {
  282. struct device *dev = btpci->dw.dev;
  283. int i;
  284. /* DBI access is supposed to be performed by the dword-aligned IOs */
  285. btpci->dw.pp.bridge->ops = &bt1_pci_ops;
  286. /* These CSRs are in MMIO so we won't check the regmap-methods status */
  287. btpci->sys_regs =
  288. syscon_regmap_lookup_by_phandle(dev->of_node, "baikal,bt1-syscon");
  289. if (IS_ERR(btpci->sys_regs))
  290. return dev_err_probe(dev, PTR_ERR(btpci->sys_regs),
  291. "Failed to get syscon\n");
  292. /* Make sure all the required resources have been specified */
  293. for (i = 0; i < BT1_PCIE_NUM_APP_CLKS; i++) {
  294. if (!btpci->dw.app_clks[bt1_pcie_app_clks[i]].clk) {
  295. dev_err(dev, "App clocks set is incomplete\n");
  296. return -ENOENT;
  297. }
  298. }
  299. for (i = 0; i < BT1_PCIE_NUM_CORE_CLKS; i++) {
  300. if (!btpci->dw.core_clks[bt1_pcie_core_clks[i]].clk) {
  301. dev_err(dev, "Core clocks set is incomplete\n");
  302. return -ENOENT;
  303. }
  304. }
  305. for (i = 0; i < BT1_PCIE_NUM_APP_RSTS; i++) {
  306. if (!btpci->dw.app_rsts[bt1_pcie_app_rsts[i]].rstc) {
  307. dev_err(dev, "App resets set is incomplete\n");
  308. return -ENOENT;
  309. }
  310. }
  311. for (i = 0; i < BT1_PCIE_NUM_CORE_RSTS; i++) {
  312. if (!btpci->dw.core_rsts[bt1_pcie_core_rsts[i]].rstc) {
  313. dev_err(dev, "Core resets set is incomplete\n");
  314. return -ENOENT;
  315. }
  316. }
  317. return 0;
  318. }
  319. static void bt1_pcie_full_stop_bus(struct bt1_pcie *btpci, bool init)
  320. {
  321. struct device *dev = btpci->dw.dev;
  322. struct dw_pcie *pci = &btpci->dw;
  323. int ret;
  324. /* Disable LTSSM for sure */
  325. regmap_update_bits(btpci->sys_regs, BT1_CCU_PCIE_GENC,
  326. BT1_CCU_PCIE_LTSSM_EN, 0);
  327. /*
  328. * Application reset controls are trigger-based so assert the core
  329. * resets only.
  330. */
  331. ret = reset_control_bulk_assert(DW_PCIE_NUM_CORE_RSTS, pci->core_rsts);
  332. if (ret)
  333. dev_err(dev, "Failed to assert core resets\n");
  334. /*
  335. * Clocks are disabled by default at least in accordance with the clk
  336. * enable counter value on init stage.
  337. */
  338. if (!init) {
  339. clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, pci->core_clks);
  340. clk_bulk_disable_unprepare(DW_PCIE_NUM_APP_CLKS, pci->app_clks);
  341. }
  342. /* The peripheral devices are unavailable anyway so reset them too */
  343. gpiod_set_value_cansleep(pci->pe_rst, 1);
  344. /* Make sure all the resets are settled */
  345. msleep(BT1_PCIE_RST_DELAY_MS);
  346. }
  347. /*
  348. * Implements the cold reset procedure in accordance with the reference manual
  349. * and available PM signals.
  350. */
  351. static int bt1_pcie_cold_start_bus(struct bt1_pcie *btpci)
  352. {
  353. struct device *dev = btpci->dw.dev;
  354. struct dw_pcie *pci = &btpci->dw;
  355. u32 val;
  356. int ret;
  357. /* First get out of the Power/Hot reset state */
  358. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_PWR_RST].rstc);
  359. if (ret) {
  360. dev_err(dev, "Failed to deassert PHY reset\n");
  361. return ret;
  362. }
  363. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_HOT_RST].rstc);
  364. if (ret) {
  365. dev_err(dev, "Failed to deassert hot reset\n");
  366. goto err_assert_pwr_rst;
  367. }
  368. /* Wait for the PM-core to stop requesting the PHY reset */
  369. ret = regmap_read_poll_timeout(btpci->sys_regs, BT1_CCU_PCIE_RSTC, val,
  370. !(val & BT1_CCU_PCIE_REQ_PHY_RST),
  371. BT1_PCIE_REQ_DELAY_US, BT1_PCIE_REQ_TIMEOUT_US);
  372. if (ret) {
  373. dev_err(dev, "Timed out waiting for PM to stop PHY resetting\n");
  374. goto err_assert_hot_rst;
  375. }
  376. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_PHY_RST].rstc);
  377. if (ret) {
  378. dev_err(dev, "Failed to deassert PHY reset\n");
  379. goto err_assert_hot_rst;
  380. }
  381. /* Clocks can be now enabled, but the ref one is crucial at this stage */
  382. ret = clk_bulk_prepare_enable(DW_PCIE_NUM_APP_CLKS, pci->app_clks);
  383. if (ret) {
  384. dev_err(dev, "Failed to enable app clocks\n");
  385. goto err_assert_phy_rst;
  386. }
  387. ret = clk_bulk_prepare_enable(DW_PCIE_NUM_CORE_CLKS, pci->core_clks);
  388. if (ret) {
  389. dev_err(dev, "Failed to enable ref clocks\n");
  390. goto err_disable_app_clk;
  391. }
  392. /* Wait for the PM to stop requesting the controller core reset */
  393. ret = regmap_read_poll_timeout(btpci->sys_regs, BT1_CCU_PCIE_RSTC, val,
  394. !(val & BT1_CCU_PCIE_REQ_CORE_RST),
  395. BT1_PCIE_REQ_DELAY_US, BT1_PCIE_REQ_TIMEOUT_US);
  396. if (ret) {
  397. dev_err(dev, "Timed out waiting for PM to stop core resetting\n");
  398. goto err_disable_core_clk;
  399. }
  400. /* PCS-PIPE interface and controller core can be now activated */
  401. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_PIPE_RST].rstc);
  402. if (ret) {
  403. dev_err(dev, "Failed to deassert PIPE reset\n");
  404. goto err_disable_core_clk;
  405. }
  406. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_CORE_RST].rstc);
  407. if (ret) {
  408. dev_err(dev, "Failed to deassert core reset\n");
  409. goto err_assert_pipe_rst;
  410. }
  411. /* It's recommended to reset the core and application logic together */
  412. ret = reset_control_bulk_reset(DW_PCIE_NUM_APP_RSTS, pci->app_rsts);
  413. if (ret) {
  414. dev_err(dev, "Failed to reset app domain\n");
  415. goto err_assert_core_rst;
  416. }
  417. /* Sticky/Non-sticky CSR flags can be now unreset too */
  418. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_STICKY_RST].rstc);
  419. if (ret) {
  420. dev_err(dev, "Failed to deassert sticky reset\n");
  421. goto err_assert_core_rst;
  422. }
  423. ret = reset_control_deassert(pci->core_rsts[DW_PCIE_NON_STICKY_RST].rstc);
  424. if (ret) {
  425. dev_err(dev, "Failed to deassert non-sticky reset\n");
  426. goto err_assert_sticky_rst;
  427. }
  428. /* Activate the PCIe bus peripheral devices */
  429. gpiod_set_value_cansleep(pci->pe_rst, 0);
  430. /* Make sure the state is settled (LTSSM is still disabled though) */
  431. usleep_range(BT1_PCIE_RUN_DELAY_US, BT1_PCIE_RUN_DELAY_US + 100);
  432. return 0;
  433. err_assert_sticky_rst:
  434. reset_control_assert(pci->core_rsts[DW_PCIE_STICKY_RST].rstc);
  435. err_assert_core_rst:
  436. reset_control_assert(pci->core_rsts[DW_PCIE_CORE_RST].rstc);
  437. err_assert_pipe_rst:
  438. reset_control_assert(pci->core_rsts[DW_PCIE_PIPE_RST].rstc);
  439. err_disable_core_clk:
  440. clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, pci->core_clks);
  441. err_disable_app_clk:
  442. clk_bulk_disable_unprepare(DW_PCIE_NUM_APP_CLKS, pci->app_clks);
  443. err_assert_phy_rst:
  444. reset_control_assert(pci->core_rsts[DW_PCIE_PHY_RST].rstc);
  445. err_assert_hot_rst:
  446. reset_control_assert(pci->core_rsts[DW_PCIE_HOT_RST].rstc);
  447. err_assert_pwr_rst:
  448. reset_control_assert(pci->core_rsts[DW_PCIE_PWR_RST].rstc);
  449. return ret;
  450. }
  451. static int bt1_pcie_host_init(struct dw_pcie_rp *pp)
  452. {
  453. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  454. struct bt1_pcie *btpci = to_bt1_pcie(pci);
  455. int ret;
  456. ret = bt1_pcie_get_resources(btpci);
  457. if (ret)
  458. return ret;
  459. bt1_pcie_full_stop_bus(btpci, true);
  460. return bt1_pcie_cold_start_bus(btpci);
  461. }
  462. static void bt1_pcie_host_deinit(struct dw_pcie_rp *pp)
  463. {
  464. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  465. struct bt1_pcie *btpci = to_bt1_pcie(pci);
  466. bt1_pcie_full_stop_bus(btpci, false);
  467. }
  468. static const struct dw_pcie_host_ops bt1_pcie_host_ops = {
  469. .init = bt1_pcie_host_init,
  470. .deinit = bt1_pcie_host_deinit,
  471. };
  472. static struct bt1_pcie *bt1_pcie_create_data(struct platform_device *pdev)
  473. {
  474. struct bt1_pcie *btpci;
  475. btpci = devm_kzalloc(&pdev->dev, sizeof(*btpci), GFP_KERNEL);
  476. if (!btpci)
  477. return ERR_PTR(-ENOMEM);
  478. btpci->pdev = pdev;
  479. platform_set_drvdata(pdev, btpci);
  480. return btpci;
  481. }
  482. static int bt1_pcie_add_port(struct bt1_pcie *btpci)
  483. {
  484. struct device *dev = &btpci->pdev->dev;
  485. int ret;
  486. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  487. if (ret)
  488. return ret;
  489. btpci->dw.version = DW_PCIE_VER_460A;
  490. btpci->dw.dev = dev;
  491. btpci->dw.ops = &bt1_pcie_ops;
  492. btpci->dw.pp.num_vectors = MAX_MSI_IRQS;
  493. btpci->dw.pp.ops = &bt1_pcie_host_ops;
  494. dw_pcie_cap_set(&btpci->dw, REQ_RES);
  495. ret = dw_pcie_host_init(&btpci->dw.pp);
  496. return dev_err_probe(dev, ret, "Failed to initialize DWC PCIe host\n");
  497. }
  498. static void bt1_pcie_del_port(struct bt1_pcie *btpci)
  499. {
  500. dw_pcie_host_deinit(&btpci->dw.pp);
  501. }
  502. static int bt1_pcie_probe(struct platform_device *pdev)
  503. {
  504. struct bt1_pcie *btpci;
  505. btpci = bt1_pcie_create_data(pdev);
  506. if (IS_ERR(btpci))
  507. return PTR_ERR(btpci);
  508. return bt1_pcie_add_port(btpci);
  509. }
  510. static void bt1_pcie_remove(struct platform_device *pdev)
  511. {
  512. struct bt1_pcie *btpci = platform_get_drvdata(pdev);
  513. bt1_pcie_del_port(btpci);
  514. }
  515. static const struct of_device_id bt1_pcie_of_match[] = {
  516. { .compatible = "baikal,bt1-pcie" },
  517. {},
  518. };
  519. MODULE_DEVICE_TABLE(of, bt1_pcie_of_match);
  520. static struct platform_driver bt1_pcie_driver = {
  521. .probe = bt1_pcie_probe,
  522. .remove = bt1_pcie_remove,
  523. .driver = {
  524. .name = "bt1-pcie",
  525. .of_match_table = bt1_pcie_of_match,
  526. },
  527. };
  528. module_platform_driver(bt1_pcie_driver);
  529. MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
  530. MODULE_DESCRIPTION("Baikal-T1 PCIe driver");
  531. MODULE_LICENSE("GPL");