pcie-histb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <[email protected]>
  8. * Jianguo Sun <[email protected]>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_gpio.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. int 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 int 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. if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
  136. (status == PCIE_LTSSM_STATE_ACTIVE))
  137. return 1;
  138. return 0;
  139. }
  140. static int histb_pcie_start_link(struct dw_pcie *pci)
  141. {
  142. struct histb_pcie *hipcie = to_histb_pcie(pci);
  143. u32 regval;
  144. /* assert LTSSM enable */
  145. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
  146. regval |= PCIE_APP_LTSSM_ENABLE;
  147. histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
  148. return 0;
  149. }
  150. static int histb_pcie_host_init(struct dw_pcie_rp *pp)
  151. {
  152. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  153. struct histb_pcie *hipcie = to_histb_pcie(pci);
  154. u32 regval;
  155. pp->bridge->ops = &histb_pci_ops;
  156. /* PCIe RC work mode */
  157. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  158. regval &= ~PCIE_DEVICE_TYPE_MASK;
  159. regval |= PCIE_WM_RC;
  160. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
  161. return 0;
  162. }
  163. static const struct dw_pcie_host_ops histb_pcie_host_ops = {
  164. .host_init = histb_pcie_host_init,
  165. };
  166. static void histb_pcie_host_disable(struct histb_pcie *hipcie)
  167. {
  168. reset_control_assert(hipcie->soft_reset);
  169. reset_control_assert(hipcie->sys_reset);
  170. reset_control_assert(hipcie->bus_reset);
  171. clk_disable_unprepare(hipcie->aux_clk);
  172. clk_disable_unprepare(hipcie->pipe_clk);
  173. clk_disable_unprepare(hipcie->sys_clk);
  174. clk_disable_unprepare(hipcie->bus_clk);
  175. if (gpio_is_valid(hipcie->reset_gpio))
  176. gpio_set_value_cansleep(hipcie->reset_gpio, 0);
  177. if (hipcie->vpcie)
  178. regulator_disable(hipcie->vpcie);
  179. }
  180. static int histb_pcie_host_enable(struct dw_pcie_rp *pp)
  181. {
  182. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  183. struct histb_pcie *hipcie = to_histb_pcie(pci);
  184. struct device *dev = pci->dev;
  185. int ret;
  186. /* power on PCIe device if have */
  187. if (hipcie->vpcie) {
  188. ret = regulator_enable(hipcie->vpcie);
  189. if (ret) {
  190. dev_err(dev, "failed to enable regulator: %d\n", ret);
  191. return ret;
  192. }
  193. }
  194. if (gpio_is_valid(hipcie->reset_gpio))
  195. gpio_set_value_cansleep(hipcie->reset_gpio, 1);
  196. ret = clk_prepare_enable(hipcie->bus_clk);
  197. if (ret) {
  198. dev_err(dev, "cannot prepare/enable bus clk\n");
  199. goto err_bus_clk;
  200. }
  201. ret = clk_prepare_enable(hipcie->sys_clk);
  202. if (ret) {
  203. dev_err(dev, "cannot prepare/enable sys clk\n");
  204. goto err_sys_clk;
  205. }
  206. ret = clk_prepare_enable(hipcie->pipe_clk);
  207. if (ret) {
  208. dev_err(dev, "cannot prepare/enable pipe clk\n");
  209. goto err_pipe_clk;
  210. }
  211. ret = clk_prepare_enable(hipcie->aux_clk);
  212. if (ret) {
  213. dev_err(dev, "cannot prepare/enable aux clk\n");
  214. goto err_aux_clk;
  215. }
  216. reset_control_assert(hipcie->soft_reset);
  217. reset_control_deassert(hipcie->soft_reset);
  218. reset_control_assert(hipcie->sys_reset);
  219. reset_control_deassert(hipcie->sys_reset);
  220. reset_control_assert(hipcie->bus_reset);
  221. reset_control_deassert(hipcie->bus_reset);
  222. return 0;
  223. err_aux_clk:
  224. clk_disable_unprepare(hipcie->pipe_clk);
  225. err_pipe_clk:
  226. clk_disable_unprepare(hipcie->sys_clk);
  227. err_sys_clk:
  228. clk_disable_unprepare(hipcie->bus_clk);
  229. err_bus_clk:
  230. if (hipcie->vpcie)
  231. regulator_disable(hipcie->vpcie);
  232. return ret;
  233. }
  234. static const struct dw_pcie_ops dw_pcie_ops = {
  235. .read_dbi = histb_pcie_read_dbi,
  236. .write_dbi = histb_pcie_write_dbi,
  237. .link_up = histb_pcie_link_up,
  238. .start_link = histb_pcie_start_link,
  239. };
  240. static int histb_pcie_probe(struct platform_device *pdev)
  241. {
  242. struct histb_pcie *hipcie;
  243. struct dw_pcie *pci;
  244. struct dw_pcie_rp *pp;
  245. struct device_node *np = pdev->dev.of_node;
  246. struct device *dev = &pdev->dev;
  247. enum of_gpio_flags of_flags;
  248. unsigned long flag = GPIOF_DIR_OUT;
  249. int ret;
  250. hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
  251. if (!hipcie)
  252. return -ENOMEM;
  253. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  254. if (!pci)
  255. return -ENOMEM;
  256. hipcie->pci = pci;
  257. pp = &pci->pp;
  258. pci->dev = dev;
  259. pci->ops = &dw_pcie_ops;
  260. hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
  261. if (IS_ERR(hipcie->ctrl)) {
  262. dev_err(dev, "cannot get control reg base\n");
  263. return PTR_ERR(hipcie->ctrl);
  264. }
  265. pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
  266. if (IS_ERR(pci->dbi_base)) {
  267. dev_err(dev, "cannot get rc-dbi base\n");
  268. return PTR_ERR(pci->dbi_base);
  269. }
  270. hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
  271. if (IS_ERR(hipcie->vpcie)) {
  272. if (PTR_ERR(hipcie->vpcie) != -ENODEV)
  273. return PTR_ERR(hipcie->vpcie);
  274. hipcie->vpcie = NULL;
  275. }
  276. hipcie->reset_gpio = of_get_named_gpio_flags(np,
  277. "reset-gpios", 0, &of_flags);
  278. if (of_flags & OF_GPIO_ACTIVE_LOW)
  279. flag |= GPIOF_ACTIVE_LOW;
  280. if (gpio_is_valid(hipcie->reset_gpio)) {
  281. ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
  282. flag, "PCIe device power control");
  283. if (ret) {
  284. dev_err(dev, "unable to request gpio\n");
  285. return ret;
  286. }
  287. }
  288. hipcie->aux_clk = devm_clk_get(dev, "aux");
  289. if (IS_ERR(hipcie->aux_clk)) {
  290. dev_err(dev, "Failed to get PCIe aux clk\n");
  291. return PTR_ERR(hipcie->aux_clk);
  292. }
  293. hipcie->pipe_clk = devm_clk_get(dev, "pipe");
  294. if (IS_ERR(hipcie->pipe_clk)) {
  295. dev_err(dev, "Failed to get PCIe pipe clk\n");
  296. return PTR_ERR(hipcie->pipe_clk);
  297. }
  298. hipcie->sys_clk = devm_clk_get(dev, "sys");
  299. if (IS_ERR(hipcie->sys_clk)) {
  300. dev_err(dev, "Failed to get PCIEe sys clk\n");
  301. return PTR_ERR(hipcie->sys_clk);
  302. }
  303. hipcie->bus_clk = devm_clk_get(dev, "bus");
  304. if (IS_ERR(hipcie->bus_clk)) {
  305. dev_err(dev, "Failed to get PCIe bus clk\n");
  306. return PTR_ERR(hipcie->bus_clk);
  307. }
  308. hipcie->soft_reset = devm_reset_control_get(dev, "soft");
  309. if (IS_ERR(hipcie->soft_reset)) {
  310. dev_err(dev, "couldn't get soft reset\n");
  311. return PTR_ERR(hipcie->soft_reset);
  312. }
  313. hipcie->sys_reset = devm_reset_control_get(dev, "sys");
  314. if (IS_ERR(hipcie->sys_reset)) {
  315. dev_err(dev, "couldn't get sys reset\n");
  316. return PTR_ERR(hipcie->sys_reset);
  317. }
  318. hipcie->bus_reset = devm_reset_control_get(dev, "bus");
  319. if (IS_ERR(hipcie->bus_reset)) {
  320. dev_err(dev, "couldn't get bus reset\n");
  321. return PTR_ERR(hipcie->bus_reset);
  322. }
  323. hipcie->phy = devm_phy_get(dev, "phy");
  324. if (IS_ERR(hipcie->phy)) {
  325. dev_info(dev, "no pcie-phy found\n");
  326. hipcie->phy = NULL;
  327. /* fall through here!
  328. * if no pcie-phy found, phy init
  329. * should be done under boot!
  330. */
  331. } else {
  332. phy_init(hipcie->phy);
  333. }
  334. pp->ops = &histb_pcie_host_ops;
  335. platform_set_drvdata(pdev, hipcie);
  336. ret = histb_pcie_host_enable(pp);
  337. if (ret) {
  338. dev_err(dev, "failed to enable host\n");
  339. return ret;
  340. }
  341. ret = dw_pcie_host_init(pp);
  342. if (ret) {
  343. dev_err(dev, "failed to initialize host\n");
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. static int histb_pcie_remove(struct platform_device *pdev)
  349. {
  350. struct histb_pcie *hipcie = platform_get_drvdata(pdev);
  351. histb_pcie_host_disable(hipcie);
  352. if (hipcie->phy)
  353. phy_exit(hipcie->phy);
  354. return 0;
  355. }
  356. static const struct of_device_id histb_pcie_of_match[] = {
  357. { .compatible = "hisilicon,hi3798cv200-pcie", },
  358. {},
  359. };
  360. MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
  361. static struct platform_driver histb_pcie_platform_driver = {
  362. .probe = histb_pcie_probe,
  363. .remove = histb_pcie_remove,
  364. .driver = {
  365. .name = "histb-pcie",
  366. .of_match_table = histb_pcie_of_match,
  367. },
  368. };
  369. module_platform_driver(histb_pcie_platform_driver);
  370. MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
  371. MODULE_LICENSE("GPL v2");