pcie-histb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/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 pcie_port *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 pcie_port *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 pcie_port *pp, int where,
  106. int size, u32 *val)
  107. {
  108. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  109. int ret;
  110. histb_pcie_dbi_r_mode(pp, true);
  111. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  112. histb_pcie_dbi_r_mode(pp, false);
  113. return ret;
  114. }
  115. static int histb_pcie_wr_own_conf(struct pcie_port *pp, int where,
  116. int size, u32 val)
  117. {
  118. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  119. int ret;
  120. histb_pcie_dbi_w_mode(pp, true);
  121. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  122. histb_pcie_dbi_w_mode(pp, false);
  123. return ret;
  124. }
  125. static int histb_pcie_link_up(struct dw_pcie *pci)
  126. {
  127. struct histb_pcie *hipcie = to_histb_pcie(pci);
  128. u32 regval;
  129. u32 status;
  130. regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
  131. status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
  132. status &= PCIE_LTSSM_STATE_MASK;
  133. if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
  134. (status == PCIE_LTSSM_STATE_ACTIVE))
  135. return 1;
  136. return 0;
  137. }
  138. static int histb_pcie_establish_link(struct pcie_port *pp)
  139. {
  140. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  141. struct histb_pcie *hipcie = to_histb_pcie(pci);
  142. u32 regval;
  143. if (dw_pcie_link_up(pci)) {
  144. dev_info(pci->dev, "Link already up\n");
  145. return 0;
  146. }
  147. /* PCIe RC work mode */
  148. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  149. regval &= ~PCIE_DEVICE_TYPE_MASK;
  150. regval |= PCIE_WM_RC;
  151. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
  152. /* setup root complex */
  153. dw_pcie_setup_rc(pp);
  154. /* assert LTSSM enable */
  155. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
  156. regval |= PCIE_APP_LTSSM_ENABLE;
  157. histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
  158. return dw_pcie_wait_for_link(pci);
  159. }
  160. static int histb_pcie_host_init(struct pcie_port *pp)
  161. {
  162. histb_pcie_establish_link(pp);
  163. if (IS_ENABLED(CONFIG_PCI_MSI))
  164. dw_pcie_msi_init(pp);
  165. return 0;
  166. }
  167. static struct dw_pcie_host_ops histb_pcie_host_ops = {
  168. .rd_own_conf = histb_pcie_rd_own_conf,
  169. .wr_own_conf = histb_pcie_wr_own_conf,
  170. .host_init = histb_pcie_host_init,
  171. };
  172. static void histb_pcie_host_disable(struct histb_pcie *hipcie)
  173. {
  174. reset_control_assert(hipcie->soft_reset);
  175. reset_control_assert(hipcie->sys_reset);
  176. reset_control_assert(hipcie->bus_reset);
  177. clk_disable_unprepare(hipcie->aux_clk);
  178. clk_disable_unprepare(hipcie->pipe_clk);
  179. clk_disable_unprepare(hipcie->sys_clk);
  180. clk_disable_unprepare(hipcie->bus_clk);
  181. if (gpio_is_valid(hipcie->reset_gpio))
  182. gpio_set_value_cansleep(hipcie->reset_gpio, 0);
  183. if (hipcie->vpcie)
  184. regulator_disable(hipcie->vpcie);
  185. }
  186. static int histb_pcie_host_enable(struct pcie_port *pp)
  187. {
  188. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  189. struct histb_pcie *hipcie = to_histb_pcie(pci);
  190. struct device *dev = pci->dev;
  191. int ret;
  192. /* power on PCIe device if have */
  193. if (hipcie->vpcie) {
  194. ret = regulator_enable(hipcie->vpcie);
  195. if (ret) {
  196. dev_err(dev, "failed to enable regulator: %d\n", ret);
  197. return ret;
  198. }
  199. }
  200. if (gpio_is_valid(hipcie->reset_gpio))
  201. gpio_set_value_cansleep(hipcie->reset_gpio, 1);
  202. ret = clk_prepare_enable(hipcie->bus_clk);
  203. if (ret) {
  204. dev_err(dev, "cannot prepare/enable bus clk\n");
  205. goto err_bus_clk;
  206. }
  207. ret = clk_prepare_enable(hipcie->sys_clk);
  208. if (ret) {
  209. dev_err(dev, "cannot prepare/enable sys clk\n");
  210. goto err_sys_clk;
  211. }
  212. ret = clk_prepare_enable(hipcie->pipe_clk);
  213. if (ret) {
  214. dev_err(dev, "cannot prepare/enable pipe clk\n");
  215. goto err_pipe_clk;
  216. }
  217. ret = clk_prepare_enable(hipcie->aux_clk);
  218. if (ret) {
  219. dev_err(dev, "cannot prepare/enable aux clk\n");
  220. goto err_aux_clk;
  221. }
  222. reset_control_assert(hipcie->soft_reset);
  223. reset_control_deassert(hipcie->soft_reset);
  224. reset_control_assert(hipcie->sys_reset);
  225. reset_control_deassert(hipcie->sys_reset);
  226. reset_control_assert(hipcie->bus_reset);
  227. reset_control_deassert(hipcie->bus_reset);
  228. return 0;
  229. err_aux_clk:
  230. clk_disable_unprepare(hipcie->pipe_clk);
  231. err_pipe_clk:
  232. clk_disable_unprepare(hipcie->sys_clk);
  233. err_sys_clk:
  234. clk_disable_unprepare(hipcie->bus_clk);
  235. err_bus_clk:
  236. if (hipcie->vpcie)
  237. regulator_disable(hipcie->vpcie);
  238. return ret;
  239. }
  240. static const struct dw_pcie_ops dw_pcie_ops = {
  241. .read_dbi = histb_pcie_read_dbi,
  242. .write_dbi = histb_pcie_write_dbi,
  243. .link_up = histb_pcie_link_up,
  244. };
  245. static int histb_pcie_probe(struct platform_device *pdev)
  246. {
  247. struct histb_pcie *hipcie;
  248. struct dw_pcie *pci;
  249. struct pcie_port *pp;
  250. struct resource *res;
  251. struct device_node *np = pdev->dev.of_node;
  252. struct device *dev = &pdev->dev;
  253. enum of_gpio_flags of_flags;
  254. unsigned long flag = GPIOF_DIR_OUT;
  255. int ret;
  256. hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
  257. if (!hipcie)
  258. return -ENOMEM;
  259. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  260. if (!pci)
  261. return -ENOMEM;
  262. hipcie->pci = pci;
  263. pp = &pci->pp;
  264. pci->dev = dev;
  265. pci->ops = &dw_pcie_ops;
  266. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control");
  267. hipcie->ctrl = devm_ioremap_resource(dev, res);
  268. if (IS_ERR(hipcie->ctrl)) {
  269. dev_err(dev, "cannot get control reg base\n");
  270. return PTR_ERR(hipcie->ctrl);
  271. }
  272. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc-dbi");
  273. pci->dbi_base = devm_ioremap_resource(dev, res);
  274. if (IS_ERR(pci->dbi_base)) {
  275. dev_err(dev, "cannot get rc-dbi base\n");
  276. return PTR_ERR(pci->dbi_base);
  277. }
  278. hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
  279. if (IS_ERR(hipcie->vpcie)) {
  280. if (PTR_ERR(hipcie->vpcie) != -ENODEV)
  281. return PTR_ERR(hipcie->vpcie);
  282. hipcie->vpcie = NULL;
  283. }
  284. hipcie->reset_gpio = of_get_named_gpio_flags(np,
  285. "reset-gpios", 0, &of_flags);
  286. if (of_flags & OF_GPIO_ACTIVE_LOW)
  287. flag |= GPIOF_ACTIVE_LOW;
  288. if (gpio_is_valid(hipcie->reset_gpio)) {
  289. ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
  290. flag, "PCIe device power control");
  291. if (ret) {
  292. dev_err(dev, "unable to request gpio\n");
  293. return ret;
  294. }
  295. }
  296. hipcie->aux_clk = devm_clk_get(dev, "aux");
  297. if (IS_ERR(hipcie->aux_clk)) {
  298. dev_err(dev, "Failed to get PCIe aux clk\n");
  299. return PTR_ERR(hipcie->aux_clk);
  300. }
  301. hipcie->pipe_clk = devm_clk_get(dev, "pipe");
  302. if (IS_ERR(hipcie->pipe_clk)) {
  303. dev_err(dev, "Failed to get PCIe pipe clk\n");
  304. return PTR_ERR(hipcie->pipe_clk);
  305. }
  306. hipcie->sys_clk = devm_clk_get(dev, "sys");
  307. if (IS_ERR(hipcie->sys_clk)) {
  308. dev_err(dev, "Failed to get PCIEe sys clk\n");
  309. return PTR_ERR(hipcie->sys_clk);
  310. }
  311. hipcie->bus_clk = devm_clk_get(dev, "bus");
  312. if (IS_ERR(hipcie->bus_clk)) {
  313. dev_err(dev, "Failed to get PCIe bus clk\n");
  314. return PTR_ERR(hipcie->bus_clk);
  315. }
  316. hipcie->soft_reset = devm_reset_control_get(dev, "soft");
  317. if (IS_ERR(hipcie->soft_reset)) {
  318. dev_err(dev, "couldn't get soft reset\n");
  319. return PTR_ERR(hipcie->soft_reset);
  320. }
  321. hipcie->sys_reset = devm_reset_control_get(dev, "sys");
  322. if (IS_ERR(hipcie->sys_reset)) {
  323. dev_err(dev, "couldn't get sys reset\n");
  324. return PTR_ERR(hipcie->sys_reset);
  325. }
  326. hipcie->bus_reset = devm_reset_control_get(dev, "bus");
  327. if (IS_ERR(hipcie->bus_reset)) {
  328. dev_err(dev, "couldn't get bus reset\n");
  329. return PTR_ERR(hipcie->bus_reset);
  330. }
  331. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  332. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  333. if (pp->msi_irq < 0) {
  334. dev_err(dev, "Failed to get MSI IRQ\n");
  335. return pp->msi_irq;
  336. }
  337. }
  338. hipcie->phy = devm_phy_get(dev, "phy");
  339. if (IS_ERR(hipcie->phy)) {
  340. dev_info(dev, "no pcie-phy found\n");
  341. hipcie->phy = NULL;
  342. /* fall through here!
  343. * if no pcie-phy found, phy init
  344. * should be done under boot!
  345. */
  346. } else {
  347. phy_init(hipcie->phy);
  348. }
  349. pp->ops = &histb_pcie_host_ops;
  350. platform_set_drvdata(pdev, hipcie);
  351. ret = histb_pcie_host_enable(pp);
  352. if (ret) {
  353. dev_err(dev, "failed to enable host\n");
  354. return ret;
  355. }
  356. ret = dw_pcie_host_init(pp);
  357. if (ret) {
  358. dev_err(dev, "failed to initialize host\n");
  359. return ret;
  360. }
  361. return 0;
  362. }
  363. static int histb_pcie_remove(struct platform_device *pdev)
  364. {
  365. struct histb_pcie *hipcie = platform_get_drvdata(pdev);
  366. histb_pcie_host_disable(hipcie);
  367. if (hipcie->phy)
  368. phy_exit(hipcie->phy);
  369. return 0;
  370. }
  371. static const struct of_device_id histb_pcie_of_match[] = {
  372. { .compatible = "hisilicon,hi3798cv200-pcie", },
  373. {},
  374. };
  375. MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
  376. static struct platform_driver histb_pcie_platform_driver = {
  377. .probe = histb_pcie_probe,
  378. .remove = histb_pcie_remove,
  379. .driver = {
  380. .name = "histb-pcie",
  381. .of_match_table = histb_pcie_of_match,
  382. },
  383. };
  384. module_platform_driver(histb_pcie_platform_driver);
  385. MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
  386. MODULE_LICENSE("GPL v2");