pcie-histb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * PCIe host controller driver for HiSilicon STB SoCs
  3. *
  4. * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
  5. *
  6. * Authors: Ruqiang Ju <juruqiang@hisilicon.com>
  7. * Jianguo Sun <sunjianguo1@huawei.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/pci.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/resource.h>
  24. #include <linux/reset.h>
  25. #include "pcie-designware.h"
  26. #define to_histb_pcie(x) dev_get_drvdata((x)->dev)
  27. #define PCIE_SYS_CTRL0 0x0000
  28. #define PCIE_SYS_CTRL1 0x0004
  29. #define PCIE_SYS_CTRL7 0x001C
  30. #define PCIE_SYS_CTRL13 0x0034
  31. #define PCIE_SYS_CTRL15 0x003C
  32. #define PCIE_SYS_CTRL16 0x0040
  33. #define PCIE_SYS_CTRL17 0x0044
  34. #define PCIE_SYS_STAT0 0x0100
  35. #define PCIE_SYS_STAT4 0x0110
  36. #define PCIE_RDLH_LINK_UP BIT(5)
  37. #define PCIE_XMLH_LINK_UP BIT(15)
  38. #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
  39. #define PCIE_APP_LTSSM_ENABLE BIT(11)
  40. #define PCIE_DEVICE_TYPE_MASK GENMASK(31, 28)
  41. #define PCIE_WM_EP 0
  42. #define PCIE_WM_LEGACY BIT(1)
  43. #define PCIE_WM_RC BIT(30)
  44. #define PCIE_LTSSM_STATE_MASK GENMASK(5, 0)
  45. #define PCIE_LTSSM_STATE_ACTIVE 0x11
  46. struct histb_pcie {
  47. struct dw_pcie *pci;
  48. struct clk *aux_clk;
  49. struct clk *pipe_clk;
  50. struct clk *sys_clk;
  51. struct clk *bus_clk;
  52. struct phy *phy;
  53. struct reset_control *soft_reset;
  54. struct reset_control *sys_reset;
  55. struct reset_control *bus_reset;
  56. void __iomem *ctrl;
  57. int reset_gpio;
  58. };
  59. static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
  60. {
  61. return readl(histb_pcie->ctrl + reg);
  62. }
  63. static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
  64. {
  65. writel(val, histb_pcie->ctrl + reg);
  66. }
  67. static void histb_pcie_dbi_w_mode(struct pcie_port *pp, bool enable)
  68. {
  69. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  70. struct histb_pcie *hipcie = to_histb_pcie(pci);
  71. u32 val;
  72. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  73. if (enable)
  74. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  75. else
  76. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  77. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
  78. }
  79. static void histb_pcie_dbi_r_mode(struct pcie_port *pp, bool enable)
  80. {
  81. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  82. struct histb_pcie *hipcie = to_histb_pcie(pci);
  83. u32 val;
  84. val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
  85. if (enable)
  86. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  87. else
  88. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  89. histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
  90. }
  91. static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  92. u32 reg, size_t size)
  93. {
  94. u32 val;
  95. histb_pcie_dbi_r_mode(&pci->pp, true);
  96. dw_pcie_read(base + reg, size, &val);
  97. histb_pcie_dbi_r_mode(&pci->pp, false);
  98. return val;
  99. }
  100. static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  101. u32 reg, size_t size, u32 val)
  102. {
  103. histb_pcie_dbi_w_mode(&pci->pp, true);
  104. dw_pcie_write(base + reg, size, val);
  105. histb_pcie_dbi_w_mode(&pci->pp, false);
  106. }
  107. static int histb_pcie_rd_own_conf(struct pcie_port *pp, int where,
  108. int size, u32 *val)
  109. {
  110. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  111. int ret;
  112. histb_pcie_dbi_r_mode(pp, true);
  113. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  114. histb_pcie_dbi_r_mode(pp, false);
  115. return ret;
  116. }
  117. static int histb_pcie_wr_own_conf(struct pcie_port *pp, int where,
  118. int size, u32 val)
  119. {
  120. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  121. int ret;
  122. histb_pcie_dbi_w_mode(pp, true);
  123. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  124. histb_pcie_dbi_w_mode(pp, false);
  125. return ret;
  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_establish_link(struct pcie_port *pp)
  141. {
  142. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  143. struct histb_pcie *hipcie = to_histb_pcie(pci);
  144. u32 regval;
  145. if (dw_pcie_link_up(pci)) {
  146. dev_info(pci->dev, "Link already up\n");
  147. return 0;
  148. }
  149. /* PCIe RC work mode */
  150. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
  151. regval &= ~PCIE_DEVICE_TYPE_MASK;
  152. regval |= PCIE_WM_RC;
  153. histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
  154. /* setup root complex */
  155. dw_pcie_setup_rc(pp);
  156. /* assert LTSSM enable */
  157. regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
  158. regval |= PCIE_APP_LTSSM_ENABLE;
  159. histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
  160. return dw_pcie_wait_for_link(pci);
  161. }
  162. static int histb_pcie_host_init(struct pcie_port *pp)
  163. {
  164. histb_pcie_establish_link(pp);
  165. if (IS_ENABLED(CONFIG_PCI_MSI))
  166. dw_pcie_msi_init(pp);
  167. return 0;
  168. }
  169. static struct dw_pcie_host_ops histb_pcie_host_ops = {
  170. .rd_own_conf = histb_pcie_rd_own_conf,
  171. .wr_own_conf = histb_pcie_wr_own_conf,
  172. .host_init = histb_pcie_host_init,
  173. };
  174. static irqreturn_t histb_pcie_msi_irq_handler(int irq, void *arg)
  175. {
  176. struct pcie_port *pp = arg;
  177. return dw_handle_msi_irq(pp);
  178. }
  179. static void histb_pcie_host_disable(struct histb_pcie *hipcie)
  180. {
  181. reset_control_assert(hipcie->soft_reset);
  182. reset_control_assert(hipcie->sys_reset);
  183. reset_control_assert(hipcie->bus_reset);
  184. clk_disable_unprepare(hipcie->aux_clk);
  185. clk_disable_unprepare(hipcie->pipe_clk);
  186. clk_disable_unprepare(hipcie->sys_clk);
  187. clk_disable_unprepare(hipcie->bus_clk);
  188. if (gpio_is_valid(hipcie->reset_gpio))
  189. gpio_set_value_cansleep(hipcie->reset_gpio, 0);
  190. }
  191. static int histb_pcie_host_enable(struct pcie_port *pp)
  192. {
  193. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  194. struct histb_pcie *hipcie = to_histb_pcie(pci);
  195. struct device *dev = pci->dev;
  196. int ret;
  197. /* power on PCIe device if have */
  198. if (gpio_is_valid(hipcie->reset_gpio))
  199. gpio_set_value_cansleep(hipcie->reset_gpio, 1);
  200. ret = clk_prepare_enable(hipcie->bus_clk);
  201. if (ret) {
  202. dev_err(dev, "cannot prepare/enable bus clk\n");
  203. goto err_bus_clk;
  204. }
  205. ret = clk_prepare_enable(hipcie->sys_clk);
  206. if (ret) {
  207. dev_err(dev, "cannot prepare/enable sys clk\n");
  208. goto err_sys_clk;
  209. }
  210. ret = clk_prepare_enable(hipcie->pipe_clk);
  211. if (ret) {
  212. dev_err(dev, "cannot prepare/enable pipe clk\n");
  213. goto err_pipe_clk;
  214. }
  215. ret = clk_prepare_enable(hipcie->aux_clk);
  216. if (ret) {
  217. dev_err(dev, "cannot prepare/enable aux clk\n");
  218. goto err_aux_clk;
  219. }
  220. reset_control_assert(hipcie->soft_reset);
  221. reset_control_deassert(hipcie->soft_reset);
  222. reset_control_assert(hipcie->sys_reset);
  223. reset_control_deassert(hipcie->sys_reset);
  224. reset_control_assert(hipcie->bus_reset);
  225. reset_control_deassert(hipcie->bus_reset);
  226. return 0;
  227. err_aux_clk:
  228. clk_disable_unprepare(hipcie->aux_clk);
  229. err_pipe_clk:
  230. clk_disable_unprepare(hipcie->pipe_clk);
  231. err_sys_clk:
  232. clk_disable_unprepare(hipcie->sys_clk);
  233. err_bus_clk:
  234. clk_disable_unprepare(hipcie->bus_clk);
  235. return ret;
  236. }
  237. static const struct dw_pcie_ops dw_pcie_ops = {
  238. .read_dbi = histb_pcie_read_dbi,
  239. .write_dbi = histb_pcie_write_dbi,
  240. .link_up = histb_pcie_link_up,
  241. };
  242. static int histb_pcie_probe(struct platform_device *pdev)
  243. {
  244. struct histb_pcie *hipcie;
  245. struct dw_pcie *pci;
  246. struct pcie_port *pp;
  247. struct resource *res;
  248. struct device_node *np = pdev->dev.of_node;
  249. struct device *dev = &pdev->dev;
  250. enum of_gpio_flags of_flags;
  251. unsigned long flag = GPIOF_DIR_OUT;
  252. int ret;
  253. hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
  254. if (!hipcie)
  255. return -ENOMEM;
  256. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  257. if (!pci)
  258. return -ENOMEM;
  259. hipcie->pci = pci;
  260. pp = &pci->pp;
  261. pci->dev = dev;
  262. pci->ops = &dw_pcie_ops;
  263. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control");
  264. hipcie->ctrl = devm_ioremap_resource(dev, res);
  265. if (IS_ERR(hipcie->ctrl)) {
  266. dev_err(dev, "cannot get control reg base\n");
  267. return PTR_ERR(hipcie->ctrl);
  268. }
  269. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc-dbi");
  270. pci->dbi_base = devm_ioremap_resource(dev, res);
  271. if (IS_ERR(pci->dbi_base)) {
  272. dev_err(dev, "cannot get rc-dbi base\n");
  273. return PTR_ERR(pci->dbi_base);
  274. }
  275. hipcie->reset_gpio = of_get_named_gpio_flags(np,
  276. "reset-gpios", 0, &of_flags);
  277. if (of_flags & OF_GPIO_ACTIVE_LOW)
  278. flag |= GPIOF_ACTIVE_LOW;
  279. if (gpio_is_valid(hipcie->reset_gpio)) {
  280. ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
  281. flag, "PCIe device power control");
  282. if (ret) {
  283. dev_err(dev, "unable to request gpio\n");
  284. return ret;
  285. }
  286. }
  287. hipcie->aux_clk = devm_clk_get(dev, "aux");
  288. if (IS_ERR(hipcie->aux_clk)) {
  289. dev_err(dev, "Failed to get PCIe aux clk\n");
  290. return PTR_ERR(hipcie->aux_clk);
  291. }
  292. hipcie->pipe_clk = devm_clk_get(dev, "pipe");
  293. if (IS_ERR(hipcie->pipe_clk)) {
  294. dev_err(dev, "Failed to get PCIe pipe clk\n");
  295. return PTR_ERR(hipcie->pipe_clk);
  296. }
  297. hipcie->sys_clk = devm_clk_get(dev, "sys");
  298. if (IS_ERR(hipcie->sys_clk)) {
  299. dev_err(dev, "Failed to get PCIEe sys clk\n");
  300. return PTR_ERR(hipcie->sys_clk);
  301. }
  302. hipcie->bus_clk = devm_clk_get(dev, "bus");
  303. if (IS_ERR(hipcie->bus_clk)) {
  304. dev_err(dev, "Failed to get PCIe bus clk\n");
  305. return PTR_ERR(hipcie->bus_clk);
  306. }
  307. hipcie->soft_reset = devm_reset_control_get(dev, "soft");
  308. if (IS_ERR(hipcie->soft_reset)) {
  309. dev_err(dev, "couldn't get soft reset\n");
  310. return PTR_ERR(hipcie->soft_reset);
  311. }
  312. hipcie->sys_reset = devm_reset_control_get(dev, "sys");
  313. if (IS_ERR(hipcie->sys_reset)) {
  314. dev_err(dev, "couldn't get sys reset\n");
  315. return PTR_ERR(hipcie->sys_reset);
  316. }
  317. hipcie->bus_reset = devm_reset_control_get(dev, "bus");
  318. if (IS_ERR(hipcie->bus_reset)) {
  319. dev_err(dev, "couldn't get bus reset\n");
  320. return PTR_ERR(hipcie->bus_reset);
  321. }
  322. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  323. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  324. if (pp->msi_irq < 0) {
  325. dev_err(dev, "Failed to get MSI IRQ\n");
  326. return pp->msi_irq;
  327. }
  328. ret = devm_request_irq(dev, pp->msi_irq,
  329. histb_pcie_msi_irq_handler,
  330. IRQF_SHARED, "histb-pcie-msi", pp);
  331. if (ret) {
  332. dev_err(dev, "cannot request MSI IRQ\n");
  333. return ret;
  334. }
  335. }
  336. hipcie->phy = devm_phy_get(dev, "phy");
  337. if (IS_ERR(hipcie->phy)) {
  338. dev_info(dev, "no pcie-phy found\n");
  339. hipcie->phy = NULL;
  340. /* fall through here!
  341. * if no pcie-phy found, phy init
  342. * should be done under boot!
  343. */
  344. } else {
  345. phy_init(hipcie->phy);
  346. }
  347. pp->root_bus_nr = -1;
  348. pp->ops = &histb_pcie_host_ops;
  349. platform_set_drvdata(pdev, hipcie);
  350. ret = histb_pcie_host_enable(pp);
  351. if (ret) {
  352. dev_err(dev, "failed to enable host\n");
  353. return ret;
  354. }
  355. ret = dw_pcie_host_init(pp);
  356. if (ret) {
  357. dev_err(dev, "failed to initialize host\n");
  358. return ret;
  359. }
  360. return 0;
  361. }
  362. static int histb_pcie_remove(struct platform_device *pdev)
  363. {
  364. struct histb_pcie *hipcie = platform_get_drvdata(pdev);
  365. histb_pcie_host_disable(hipcie);
  366. if (hipcie->phy)
  367. phy_exit(hipcie->phy);
  368. return 0;
  369. }
  370. static const struct of_device_id histb_pcie_of_match[] = {
  371. { .compatible = "hisilicon,hi3798cv200-pcie", },
  372. {},
  373. };
  374. MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
  375. static struct platform_driver histb_pcie_platform_driver = {
  376. .probe = histb_pcie_probe,
  377. .remove = histb_pcie_remove,
  378. .driver = {
  379. .name = "histb-pcie",
  380. .of_match_table = histb_pcie_of_match,
  381. },
  382. };
  383. module_platform_driver(histb_pcie_platform_driver);
  384. MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
  385. MODULE_LICENSE("GPL v2");