pcie-histb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for HiSilicon STB SoCs
  4. *
  5. * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
  6. *
  7. * Authors: Ruqiang Ju <juruqiang@hisilicon.com>
  8. * Jianguo Sun <sunjianguo1@huawei.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/pci.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/resource.h>
  21. #include <linux/reset.h>
  22. #include "pcie-designware.h"
  23. #define to_histb_pcie(x) dev_get_drvdata((x)->dev)
  24. #define PCIE_SYS_CTRL0 0x0000
  25. #define PCIE_SYS_CTRL1 0x0004
  26. #define PCIE_SYS_CTRL7 0x001C
  27. #define PCIE_SYS_CTRL13 0x0034
  28. #define PCIE_SYS_CTRL15 0x003C
  29. #define PCIE_SYS_CTRL16 0x0040
  30. #define PCIE_SYS_CTRL17 0x0044
  31. #define PCIE_SYS_STAT0 0x0100
  32. #define PCIE_SYS_STAT4 0x0110
  33. #define PCIE_RDLH_LINK_UP BIT(5)
  34. #define PCIE_XMLH_LINK_UP BIT(15)
  35. #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
  36. #define PCIE_APP_LTSSM_ENABLE BIT(11)
  37. #define PCIE_DEVICE_TYPE_MASK GENMASK(31, 28)
  38. #define PCIE_WM_EP 0
  39. #define PCIE_WM_LEGACY BIT(1)
  40. #define PCIE_WM_RC BIT(30)
  41. #define PCIE_LTSSM_STATE_MASK GENMASK(5, 0)
  42. #define PCIE_LTSSM_STATE_ACTIVE 0x11
  43. struct histb_pcie {
  44. struct dw_pcie *pci;
  45. struct clk *aux_clk;
  46. struct clk *pipe_clk;
  47. struct clk *sys_clk;
  48. struct clk *bus_clk;
  49. struct phy *phy;
  50. struct reset_control *soft_reset;
  51. struct reset_control *sys_reset;
  52. struct reset_control *bus_reset;
  53. void __iomem *ctrl;
  54. struct gpio_desc *reset_gpio;
  55. struct regulator *vpcie;
  56. };
  57. static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
  58. {
  59. return readl(histb_pcie->ctrl + reg);
  60. }
  61. static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
  62. {
  63. writel(val, histb_pcie->ctrl + reg);
  64. }
  65. static void histb_pcie_dbi_w_mode(struct dw_pcie_rp *pp, bool enable)
  66. {
  67. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  68. struct histb_pcie *hipcie = to_histb_pcie(pci);
  69. u32 val;
  70. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  71. if (enable)
  72. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  73. else
  74. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  75. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
  76. }
  77. static void histb_pcie_dbi_r_mode(struct dw_pcie_rp *pp, bool enable)
  78. {
  79. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  80. struct histb_pcie *hipcie = to_histb_pcie(pci);
  81. u32 val;
  82. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
  83. if (enable)
  84. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  85. else
  86. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  87. histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
  88. }
  89. static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  90. u32 reg, size_t size)
  91. {
  92. u32 val;
  93. histb_pcie_dbi_r_mode(&pci->pp, true);
  94. dw_pcie_read(base + reg, size, &val);
  95. histb_pcie_dbi_r_mode(&pci->pp, false);
  96. return val;
  97. }
  98. static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  99. u32 reg, size_t size, u32 val)
  100. {
  101. histb_pcie_dbi_w_mode(&pci->pp, true);
  102. dw_pcie_write(base + reg, size, val);
  103. histb_pcie_dbi_w_mode(&pci->pp, false);
  104. }
  105. static int histb_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
  106. int where, int size, u32 *val)
  107. {
  108. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  109. if (PCI_SLOT(devfn))
  110. return PCIBIOS_DEVICE_NOT_FOUND;
  111. *val = dw_pcie_read_dbi(pci, where, size);
  112. return PCIBIOS_SUCCESSFUL;
  113. }
  114. static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
  115. int where, int size, u32 val)
  116. {
  117. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  118. if (PCI_SLOT(devfn))
  119. return PCIBIOS_DEVICE_NOT_FOUND;
  120. dw_pcie_write_dbi(pci, where, size, val);
  121. return PCIBIOS_SUCCESSFUL;
  122. }
  123. static struct pci_ops histb_pci_ops = {
  124. .read = histb_pcie_rd_own_conf,
  125. .write = histb_pcie_wr_own_conf,
  126. };
  127. static bool histb_pcie_link_up(struct dw_pcie *pci)
  128. {
  129. struct histb_pcie *hipcie = to_histb_pcie(pci);
  130. u32 regval;
  131. u32 status;
  132. regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
  133. status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
  134. status &= PCIE_LTSSM_STATE_MASK;
  135. return ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
  136. (status == PCIE_LTSSM_STATE_ACTIVE));
  137. }
  138. static int histb_pcie_start_link(struct dw_pcie *pci)
  139. {
  140. struct histb_pcie *hipcie = to_histb_pcie(pci);
  141. u32 regval;
  142. /* assert LTSSM enable */
  143. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
  144. regval |= PCIE_APP_LTSSM_ENABLE;
  145. histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
  146. return 0;
  147. }
  148. static int histb_pcie_host_init(struct dw_pcie_rp *pp)
  149. {
  150. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  151. struct histb_pcie *hipcie = to_histb_pcie(pci);
  152. u32 regval;
  153. pp->bridge->ops = &histb_pci_ops;
  154. /* PCIe RC work mode */
  155. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  156. regval &= ~PCIE_DEVICE_TYPE_MASK;
  157. regval |= PCIE_WM_RC;
  158. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
  159. return 0;
  160. }
  161. static const struct dw_pcie_host_ops histb_pcie_host_ops = {
  162. .init = histb_pcie_host_init,
  163. };
  164. static void histb_pcie_host_disable(struct histb_pcie *hipcie)
  165. {
  166. reset_control_assert(hipcie->soft_reset);
  167. reset_control_assert(hipcie->sys_reset);
  168. reset_control_assert(hipcie->bus_reset);
  169. clk_disable_unprepare(hipcie->aux_clk);
  170. clk_disable_unprepare(hipcie->pipe_clk);
  171. clk_disable_unprepare(hipcie->sys_clk);
  172. clk_disable_unprepare(hipcie->bus_clk);
  173. if (hipcie->reset_gpio)
  174. gpiod_set_value_cansleep(hipcie->reset_gpio, 1);
  175. if (hipcie->vpcie)
  176. regulator_disable(hipcie->vpcie);
  177. }
  178. static int histb_pcie_host_enable(struct dw_pcie_rp *pp)
  179. {
  180. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  181. struct histb_pcie *hipcie = to_histb_pcie(pci);
  182. struct device *dev = pci->dev;
  183. int ret;
  184. /* power on PCIe device if have */
  185. if (hipcie->vpcie) {
  186. ret = regulator_enable(hipcie->vpcie);
  187. if (ret) {
  188. dev_err(dev, "failed to enable regulator: %d\n", ret);
  189. return ret;
  190. }
  191. }
  192. if (hipcie->reset_gpio)
  193. gpiod_set_value_cansleep(hipcie->reset_gpio, 0);
  194. ret = clk_prepare_enable(hipcie->bus_clk);
  195. if (ret) {
  196. dev_err(dev, "cannot prepare/enable bus clk\n");
  197. goto err_bus_clk;
  198. }
  199. ret = clk_prepare_enable(hipcie->sys_clk);
  200. if (ret) {
  201. dev_err(dev, "cannot prepare/enable sys clk\n");
  202. goto err_sys_clk;
  203. }
  204. ret = clk_prepare_enable(hipcie->pipe_clk);
  205. if (ret) {
  206. dev_err(dev, "cannot prepare/enable pipe clk\n");
  207. goto err_pipe_clk;
  208. }
  209. ret = clk_prepare_enable(hipcie->aux_clk);
  210. if (ret) {
  211. dev_err(dev, "cannot prepare/enable aux clk\n");
  212. goto err_aux_clk;
  213. }
  214. reset_control_assert(hipcie->soft_reset);
  215. reset_control_deassert(hipcie->soft_reset);
  216. reset_control_assert(hipcie->sys_reset);
  217. reset_control_deassert(hipcie->sys_reset);
  218. reset_control_assert(hipcie->bus_reset);
  219. reset_control_deassert(hipcie->bus_reset);
  220. return 0;
  221. err_aux_clk:
  222. clk_disable_unprepare(hipcie->pipe_clk);
  223. err_pipe_clk:
  224. clk_disable_unprepare(hipcie->sys_clk);
  225. err_sys_clk:
  226. clk_disable_unprepare(hipcie->bus_clk);
  227. err_bus_clk:
  228. if (hipcie->vpcie)
  229. regulator_disable(hipcie->vpcie);
  230. return ret;
  231. }
  232. static const struct dw_pcie_ops dw_pcie_ops = {
  233. .read_dbi = histb_pcie_read_dbi,
  234. .write_dbi = histb_pcie_write_dbi,
  235. .link_up = histb_pcie_link_up,
  236. .start_link = histb_pcie_start_link,
  237. };
  238. static int histb_pcie_probe(struct platform_device *pdev)
  239. {
  240. struct histb_pcie *hipcie;
  241. struct dw_pcie *pci;
  242. struct dw_pcie_rp *pp;
  243. struct device *dev = &pdev->dev;
  244. int ret;
  245. hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
  246. if (!hipcie)
  247. return -ENOMEM;
  248. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  249. if (!pci)
  250. return -ENOMEM;
  251. hipcie->pci = pci;
  252. pp = &pci->pp;
  253. pci->dev = dev;
  254. pci->ops = &dw_pcie_ops;
  255. hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
  256. if (IS_ERR(hipcie->ctrl)) {
  257. dev_err(dev, "cannot get control reg base\n");
  258. return PTR_ERR(hipcie->ctrl);
  259. }
  260. pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
  261. if (IS_ERR(pci->dbi_base)) {
  262. dev_err(dev, "cannot get rc-dbi base\n");
  263. return PTR_ERR(pci->dbi_base);
  264. }
  265. hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
  266. if (IS_ERR(hipcie->vpcie)) {
  267. if (PTR_ERR(hipcie->vpcie) != -ENODEV)
  268. return PTR_ERR(hipcie->vpcie);
  269. hipcie->vpcie = NULL;
  270. }
  271. hipcie->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  272. GPIOD_OUT_HIGH);
  273. ret = PTR_ERR_OR_ZERO(hipcie->reset_gpio);
  274. if (ret) {
  275. dev_err(dev, "unable to request reset gpio: %d\n", ret);
  276. return ret;
  277. }
  278. ret = gpiod_set_consumer_name(hipcie->reset_gpio,
  279. "PCIe device power control");
  280. if (ret) {
  281. dev_err(dev, "unable to set reset gpio name: %d\n", ret);
  282. return ret;
  283. }
  284. hipcie->aux_clk = devm_clk_get(dev, "aux");
  285. if (IS_ERR(hipcie->aux_clk)) {
  286. dev_err(dev, "Failed to get PCIe aux clk\n");
  287. return PTR_ERR(hipcie->aux_clk);
  288. }
  289. hipcie->pipe_clk = devm_clk_get(dev, "pipe");
  290. if (IS_ERR(hipcie->pipe_clk)) {
  291. dev_err(dev, "Failed to get PCIe pipe clk\n");
  292. return PTR_ERR(hipcie->pipe_clk);
  293. }
  294. hipcie->sys_clk = devm_clk_get(dev, "sys");
  295. if (IS_ERR(hipcie->sys_clk)) {
  296. dev_err(dev, "Failed to get PCIEe sys clk\n");
  297. return PTR_ERR(hipcie->sys_clk);
  298. }
  299. hipcie->bus_clk = devm_clk_get(dev, "bus");
  300. if (IS_ERR(hipcie->bus_clk)) {
  301. dev_err(dev, "Failed to get PCIe bus clk\n");
  302. return PTR_ERR(hipcie->bus_clk);
  303. }
  304. hipcie->soft_reset = devm_reset_control_get(dev, "soft");
  305. if (IS_ERR(hipcie->soft_reset)) {
  306. dev_err(dev, "couldn't get soft reset\n");
  307. return PTR_ERR(hipcie->soft_reset);
  308. }
  309. hipcie->sys_reset = devm_reset_control_get(dev, "sys");
  310. if (IS_ERR(hipcie->sys_reset)) {
  311. dev_err(dev, "couldn't get sys reset\n");
  312. return PTR_ERR(hipcie->sys_reset);
  313. }
  314. hipcie->bus_reset = devm_reset_control_get(dev, "bus");
  315. if (IS_ERR(hipcie->bus_reset)) {
  316. dev_err(dev, "couldn't get bus reset\n");
  317. return PTR_ERR(hipcie->bus_reset);
  318. }
  319. hipcie->phy = devm_phy_get(dev, "phy");
  320. if (IS_ERR(hipcie->phy)) {
  321. dev_info(dev, "no pcie-phy found\n");
  322. hipcie->phy = NULL;
  323. /* fall through here!
  324. * if no pcie-phy found, phy init
  325. * should be done under boot!
  326. */
  327. } else {
  328. phy_init(hipcie->phy);
  329. }
  330. pp->ops = &histb_pcie_host_ops;
  331. platform_set_drvdata(pdev, hipcie);
  332. ret = histb_pcie_host_enable(pp);
  333. if (ret) {
  334. dev_err(dev, "failed to enable host\n");
  335. goto err_exit_phy;
  336. }
  337. ret = dw_pcie_host_init(pp);
  338. if (ret) {
  339. dev_err(dev, "failed to initialize host\n");
  340. goto err_exit_phy;
  341. }
  342. return 0;
  343. err_exit_phy:
  344. phy_exit(hipcie->phy);
  345. return ret;
  346. }
  347. static void histb_pcie_remove(struct platform_device *pdev)
  348. {
  349. struct histb_pcie *hipcie = platform_get_drvdata(pdev);
  350. histb_pcie_host_disable(hipcie);
  351. phy_exit(hipcie->phy);
  352. }
  353. static const struct of_device_id histb_pcie_of_match[] = {
  354. { .compatible = "hisilicon,hi3798cv200-pcie", },
  355. {},
  356. };
  357. MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
  358. static struct platform_driver histb_pcie_platform_driver = {
  359. .probe = histb_pcie_probe,
  360. .remove = histb_pcie_remove,
  361. .driver = {
  362. .name = "histb-pcie",
  363. .of_match_table = histb_pcie_of_match,
  364. },
  365. };
  366. module_platform_driver(histb_pcie_platform_driver);
  367. MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");