pcie-histb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. };
  56. static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
  57. {
  58. return readl(histb_pcie->ctrl + reg);
  59. }
  60. static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
  61. {
  62. writel(val, histb_pcie->ctrl + reg);
  63. }
  64. static void histb_pcie_dbi_w_mode(struct pcie_port *pp, bool enable)
  65. {
  66. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  67. struct histb_pcie *hipcie = to_histb_pcie(pci);
  68. u32 val;
  69. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  70. if (enable)
  71. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  72. else
  73. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  74. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
  75. }
  76. static void histb_pcie_dbi_r_mode(struct pcie_port *pp, bool enable)
  77. {
  78. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  79. struct histb_pcie *hipcie = to_histb_pcie(pci);
  80. u32 val;
  81. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
  82. if (enable)
  83. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  84. else
  85. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  86. histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
  87. }
  88. static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  89. u32 reg, size_t size)
  90. {
  91. u32 val;
  92. histb_pcie_dbi_r_mode(&pci->pp, true);
  93. dw_pcie_read(base + reg, size, &val);
  94. histb_pcie_dbi_r_mode(&pci->pp, false);
  95. return val;
  96. }
  97. static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  98. u32 reg, size_t size, u32 val)
  99. {
  100. histb_pcie_dbi_w_mode(&pci->pp, true);
  101. dw_pcie_write(base + reg, size, val);
  102. histb_pcie_dbi_w_mode(&pci->pp, false);
  103. }
  104. static int histb_pcie_rd_own_conf(struct pcie_port *pp, int where,
  105. int size, u32 *val)
  106. {
  107. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  108. int ret;
  109. histb_pcie_dbi_r_mode(pp, true);
  110. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  111. histb_pcie_dbi_r_mode(pp, false);
  112. return ret;
  113. }
  114. static int histb_pcie_wr_own_conf(struct pcie_port *pp, int where,
  115. int size, u32 val)
  116. {
  117. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  118. int ret;
  119. histb_pcie_dbi_w_mode(pp, true);
  120. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  121. histb_pcie_dbi_w_mode(pp, false);
  122. return ret;
  123. }
  124. static int histb_pcie_link_up(struct dw_pcie *pci)
  125. {
  126. struct histb_pcie *hipcie = to_histb_pcie(pci);
  127. u32 regval;
  128. u32 status;
  129. regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
  130. status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
  131. status &= PCIE_LTSSM_STATE_MASK;
  132. if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
  133. (status == PCIE_LTSSM_STATE_ACTIVE))
  134. return 1;
  135. return 0;
  136. }
  137. static int histb_pcie_establish_link(struct pcie_port *pp)
  138. {
  139. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  140. struct histb_pcie *hipcie = to_histb_pcie(pci);
  141. u32 regval;
  142. if (dw_pcie_link_up(pci)) {
  143. dev_info(pci->dev, "Link already up\n");
  144. return 0;
  145. }
  146. /* PCIe RC work mode */
  147. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  148. regval &= ~PCIE_DEVICE_TYPE_MASK;
  149. regval |= PCIE_WM_RC;
  150. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
  151. /* setup root complex */
  152. dw_pcie_setup_rc(pp);
  153. /* assert LTSSM enable */
  154. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
  155. regval |= PCIE_APP_LTSSM_ENABLE;
  156. histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
  157. return dw_pcie_wait_for_link(pci);
  158. }
  159. static int histb_pcie_host_init(struct pcie_port *pp)
  160. {
  161. histb_pcie_establish_link(pp);
  162. if (IS_ENABLED(CONFIG_PCI_MSI))
  163. dw_pcie_msi_init(pp);
  164. return 0;
  165. }
  166. static struct dw_pcie_host_ops histb_pcie_host_ops = {
  167. .rd_own_conf = histb_pcie_rd_own_conf,
  168. .wr_own_conf = histb_pcie_wr_own_conf,
  169. .host_init = histb_pcie_host_init,
  170. };
  171. static irqreturn_t histb_pcie_msi_irq_handler(int irq, void *arg)
  172. {
  173. struct pcie_port *pp = arg;
  174. return dw_handle_msi_irq(pp);
  175. }
  176. static void histb_pcie_host_disable(struct histb_pcie *hipcie)
  177. {
  178. reset_control_assert(hipcie->soft_reset);
  179. reset_control_assert(hipcie->sys_reset);
  180. reset_control_assert(hipcie->bus_reset);
  181. clk_disable_unprepare(hipcie->aux_clk);
  182. clk_disable_unprepare(hipcie->pipe_clk);
  183. clk_disable_unprepare(hipcie->sys_clk);
  184. clk_disable_unprepare(hipcie->bus_clk);
  185. if (gpio_is_valid(hipcie->reset_gpio))
  186. gpio_set_value_cansleep(hipcie->reset_gpio, 0);
  187. }
  188. static int histb_pcie_host_enable(struct pcie_port *pp)
  189. {
  190. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  191. struct histb_pcie *hipcie = to_histb_pcie(pci);
  192. struct device *dev = pci->dev;
  193. int ret;
  194. /* power on PCIe device if have */
  195. if (gpio_is_valid(hipcie->reset_gpio))
  196. gpio_set_value_cansleep(hipcie->reset_gpio, 1);
  197. ret = clk_prepare_enable(hipcie->bus_clk);
  198. if (ret) {
  199. dev_err(dev, "cannot prepare/enable bus clk\n");
  200. goto err_bus_clk;
  201. }
  202. ret = clk_prepare_enable(hipcie->sys_clk);
  203. if (ret) {
  204. dev_err(dev, "cannot prepare/enable sys clk\n");
  205. goto err_sys_clk;
  206. }
  207. ret = clk_prepare_enable(hipcie->pipe_clk);
  208. if (ret) {
  209. dev_err(dev, "cannot prepare/enable pipe clk\n");
  210. goto err_pipe_clk;
  211. }
  212. ret = clk_prepare_enable(hipcie->aux_clk);
  213. if (ret) {
  214. dev_err(dev, "cannot prepare/enable aux clk\n");
  215. goto err_aux_clk;
  216. }
  217. reset_control_assert(hipcie->soft_reset);
  218. reset_control_deassert(hipcie->soft_reset);
  219. reset_control_assert(hipcie->sys_reset);
  220. reset_control_deassert(hipcie->sys_reset);
  221. reset_control_assert(hipcie->bus_reset);
  222. reset_control_deassert(hipcie->bus_reset);
  223. return 0;
  224. err_aux_clk:
  225. clk_disable_unprepare(hipcie->aux_clk);
  226. err_pipe_clk:
  227. clk_disable_unprepare(hipcie->pipe_clk);
  228. err_sys_clk:
  229. clk_disable_unprepare(hipcie->sys_clk);
  230. err_bus_clk:
  231. clk_disable_unprepare(hipcie->bus_clk);
  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. };
  239. static int histb_pcie_probe(struct platform_device *pdev)
  240. {
  241. struct histb_pcie *hipcie;
  242. struct dw_pcie *pci;
  243. struct pcie_port *pp;
  244. struct resource *res;
  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. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control");
  261. hipcie->ctrl = devm_ioremap_resource(dev, res);
  262. if (IS_ERR(hipcie->ctrl)) {
  263. dev_err(dev, "cannot get control reg base\n");
  264. return PTR_ERR(hipcie->ctrl);
  265. }
  266. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc-dbi");
  267. pci->dbi_base = devm_ioremap_resource(dev, res);
  268. if (IS_ERR(pci->dbi_base)) {
  269. dev_err(dev, "cannot get rc-dbi base\n");
  270. return PTR_ERR(pci->dbi_base);
  271. }
  272. hipcie->reset_gpio = of_get_named_gpio_flags(np,
  273. "reset-gpios", 0, &of_flags);
  274. if (of_flags & OF_GPIO_ACTIVE_LOW)
  275. flag |= GPIOF_ACTIVE_LOW;
  276. if (gpio_is_valid(hipcie->reset_gpio)) {
  277. ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
  278. flag, "PCIe device power control");
  279. if (ret) {
  280. dev_err(dev, "unable to request gpio\n");
  281. return ret;
  282. }
  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. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  320. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  321. if (pp->msi_irq < 0) {
  322. dev_err(dev, "Failed to get MSI IRQ\n");
  323. return pp->msi_irq;
  324. }
  325. ret = devm_request_irq(dev, pp->msi_irq,
  326. histb_pcie_msi_irq_handler,
  327. IRQF_SHARED, "histb-pcie-msi", pp);
  328. if (ret) {
  329. dev_err(dev, "cannot request MSI IRQ\n");
  330. return ret;
  331. }
  332. }
  333. hipcie->phy = devm_phy_get(dev, "phy");
  334. if (IS_ERR(hipcie->phy)) {
  335. dev_info(dev, "no pcie-phy found\n");
  336. hipcie->phy = NULL;
  337. /* fall through here!
  338. * if no pcie-phy found, phy init
  339. * should be done under boot!
  340. */
  341. } else {
  342. phy_init(hipcie->phy);
  343. }
  344. pp->root_bus_nr = -1;
  345. pp->ops = &histb_pcie_host_ops;
  346. platform_set_drvdata(pdev, hipcie);
  347. ret = histb_pcie_host_enable(pp);
  348. if (ret) {
  349. dev_err(dev, "failed to enable host\n");
  350. return ret;
  351. }
  352. ret = dw_pcie_host_init(pp);
  353. if (ret) {
  354. dev_err(dev, "failed to initialize host\n");
  355. return ret;
  356. }
  357. return 0;
  358. }
  359. static int histb_pcie_remove(struct platform_device *pdev)
  360. {
  361. struct histb_pcie *hipcie = platform_get_drvdata(pdev);
  362. histb_pcie_host_disable(hipcie);
  363. if (hipcie->phy)
  364. phy_exit(hipcie->phy);
  365. return 0;
  366. }
  367. static const struct of_device_id histb_pcie_of_match[] = {
  368. { .compatible = "hisilicon,hi3798cv200-pcie", },
  369. {},
  370. };
  371. MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
  372. static struct platform_driver histb_pcie_platform_driver = {
  373. .probe = histb_pcie_probe,
  374. .remove = histb_pcie_remove,
  375. .driver = {
  376. .name = "histb-pcie",
  377. .of_match_table = histb_pcie_of_match,
  378. },
  379. };
  380. module_platform_driver(histb_pcie_platform_driver);
  381. MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
  382. MODULE_LICENSE("GPL v2");