pci-exynos.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Samsung Exynos SoCs
  4. *
  5. * Copyright (C) 2013-2020 Samsung Electronics Co., Ltd.
  6. * https://www.samsung.com
  7. *
  8. * Author: Jingoo Han <[email protected]>
  9. * Jaehoon Chung <[email protected]>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/module.h>
  22. #include "pcie-designware.h"
  23. #define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
  24. /* PCIe ELBI registers */
  25. #define PCIE_IRQ_PULSE 0x000
  26. #define IRQ_INTA_ASSERT BIT(0)
  27. #define IRQ_INTB_ASSERT BIT(2)
  28. #define IRQ_INTC_ASSERT BIT(4)
  29. #define IRQ_INTD_ASSERT BIT(6)
  30. #define PCIE_IRQ_LEVEL 0x004
  31. #define PCIE_IRQ_SPECIAL 0x008
  32. #define PCIE_IRQ_EN_PULSE 0x00c
  33. #define PCIE_IRQ_EN_LEVEL 0x010
  34. #define PCIE_IRQ_EN_SPECIAL 0x014
  35. #define PCIE_SW_WAKE 0x018
  36. #define PCIE_BUS_EN BIT(1)
  37. #define PCIE_CORE_RESET 0x01c
  38. #define PCIE_CORE_RESET_ENABLE BIT(0)
  39. #define PCIE_STICKY_RESET 0x020
  40. #define PCIE_NONSTICKY_RESET 0x024
  41. #define PCIE_APP_INIT_RESET 0x028
  42. #define PCIE_APP_LTSSM_ENABLE 0x02c
  43. #define PCIE_ELBI_RDLH_LINKUP 0x074
  44. #define PCIE_ELBI_XMLH_LINKUP BIT(4)
  45. #define PCIE_ELBI_LTSSM_ENABLE 0x1
  46. #define PCIE_ELBI_SLV_AWMISC 0x11c
  47. #define PCIE_ELBI_SLV_ARMISC 0x120
  48. #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
  49. struct exynos_pcie {
  50. struct dw_pcie pci;
  51. void __iomem *elbi_base;
  52. struct clk *clk;
  53. struct clk *bus_clk;
  54. struct phy *phy;
  55. struct regulator_bulk_data supplies[2];
  56. };
  57. static int exynos_pcie_init_clk_resources(struct exynos_pcie *ep)
  58. {
  59. struct device *dev = ep->pci.dev;
  60. int ret;
  61. ret = clk_prepare_enable(ep->clk);
  62. if (ret) {
  63. dev_err(dev, "cannot enable pcie rc clock");
  64. return ret;
  65. }
  66. ret = clk_prepare_enable(ep->bus_clk);
  67. if (ret) {
  68. dev_err(dev, "cannot enable pcie bus clock");
  69. goto err_bus_clk;
  70. }
  71. return 0;
  72. err_bus_clk:
  73. clk_disable_unprepare(ep->clk);
  74. return ret;
  75. }
  76. static void exynos_pcie_deinit_clk_resources(struct exynos_pcie *ep)
  77. {
  78. clk_disable_unprepare(ep->bus_clk);
  79. clk_disable_unprepare(ep->clk);
  80. }
  81. static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
  82. {
  83. writel(val, base + reg);
  84. }
  85. static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
  86. {
  87. return readl(base + reg);
  88. }
  89. static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
  90. {
  91. u32 val;
  92. val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_AWMISC);
  93. if (on)
  94. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  95. else
  96. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  97. exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
  98. }
  99. static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
  100. {
  101. u32 val;
  102. val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_ARMISC);
  103. if (on)
  104. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  105. else
  106. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  107. exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
  108. }
  109. static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
  110. {
  111. u32 val;
  112. val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
  113. val &= ~PCIE_CORE_RESET_ENABLE;
  114. exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
  115. exynos_pcie_writel(ep->elbi_base, 0, PCIE_STICKY_RESET);
  116. exynos_pcie_writel(ep->elbi_base, 0, PCIE_NONSTICKY_RESET);
  117. }
  118. static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
  119. {
  120. u32 val;
  121. val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
  122. val |= PCIE_CORE_RESET_ENABLE;
  123. exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
  124. exynos_pcie_writel(ep->elbi_base, 1, PCIE_STICKY_RESET);
  125. exynos_pcie_writel(ep->elbi_base, 1, PCIE_NONSTICKY_RESET);
  126. exynos_pcie_writel(ep->elbi_base, 1, PCIE_APP_INIT_RESET);
  127. exynos_pcie_writel(ep->elbi_base, 0, PCIE_APP_INIT_RESET);
  128. }
  129. static int exynos_pcie_start_link(struct dw_pcie *pci)
  130. {
  131. struct exynos_pcie *ep = to_exynos_pcie(pci);
  132. u32 val;
  133. val = exynos_pcie_readl(ep->elbi_base, PCIE_SW_WAKE);
  134. val &= ~PCIE_BUS_EN;
  135. exynos_pcie_writel(ep->elbi_base, val, PCIE_SW_WAKE);
  136. /* assert LTSSM enable */
  137. exynos_pcie_writel(ep->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
  138. PCIE_APP_LTSSM_ENABLE);
  139. return 0;
  140. }
  141. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
  142. {
  143. u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_IRQ_PULSE);
  144. exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_PULSE);
  145. }
  146. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  147. {
  148. struct exynos_pcie *ep = arg;
  149. exynos_pcie_clear_irq_pulse(ep);
  150. return IRQ_HANDLED;
  151. }
  152. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
  153. {
  154. u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  155. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  156. exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_EN_PULSE);
  157. exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
  158. exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
  159. }
  160. static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  161. u32 reg, size_t size)
  162. {
  163. struct exynos_pcie *ep = to_exynos_pcie(pci);
  164. u32 val;
  165. exynos_pcie_sideband_dbi_r_mode(ep, true);
  166. dw_pcie_read(base + reg, size, &val);
  167. exynos_pcie_sideband_dbi_r_mode(ep, false);
  168. return val;
  169. }
  170. static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  171. u32 reg, size_t size, u32 val)
  172. {
  173. struct exynos_pcie *ep = to_exynos_pcie(pci);
  174. exynos_pcie_sideband_dbi_w_mode(ep, true);
  175. dw_pcie_write(base + reg, size, val);
  176. exynos_pcie_sideband_dbi_w_mode(ep, false);
  177. }
  178. static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
  179. int where, int size, u32 *val)
  180. {
  181. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  182. if (PCI_SLOT(devfn))
  183. return PCIBIOS_DEVICE_NOT_FOUND;
  184. *val = dw_pcie_read_dbi(pci, where, size);
  185. return PCIBIOS_SUCCESSFUL;
  186. }
  187. static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
  188. int where, int size, u32 val)
  189. {
  190. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  191. if (PCI_SLOT(devfn))
  192. return PCIBIOS_DEVICE_NOT_FOUND;
  193. dw_pcie_write_dbi(pci, where, size, val);
  194. return PCIBIOS_SUCCESSFUL;
  195. }
  196. static struct pci_ops exynos_pci_ops = {
  197. .read = exynos_pcie_rd_own_conf,
  198. .write = exynos_pcie_wr_own_conf,
  199. };
  200. static int exynos_pcie_link_up(struct dw_pcie *pci)
  201. {
  202. struct exynos_pcie *ep = to_exynos_pcie(pci);
  203. u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_RDLH_LINKUP);
  204. return (val & PCIE_ELBI_XMLH_LINKUP);
  205. }
  206. static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
  207. {
  208. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  209. struct exynos_pcie *ep = to_exynos_pcie(pci);
  210. pp->bridge->ops = &exynos_pci_ops;
  211. exynos_pcie_assert_core_reset(ep);
  212. phy_init(ep->phy);
  213. phy_power_on(ep->phy);
  214. exynos_pcie_deassert_core_reset(ep);
  215. exynos_pcie_enable_irq_pulse(ep);
  216. return 0;
  217. }
  218. static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
  219. .host_init = exynos_pcie_host_init,
  220. };
  221. static int exynos_add_pcie_port(struct exynos_pcie *ep,
  222. struct platform_device *pdev)
  223. {
  224. struct dw_pcie *pci = &ep->pci;
  225. struct dw_pcie_rp *pp = &pci->pp;
  226. struct device *dev = &pdev->dev;
  227. int ret;
  228. pp->irq = platform_get_irq(pdev, 0);
  229. if (pp->irq < 0)
  230. return pp->irq;
  231. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  232. IRQF_SHARED, "exynos-pcie", ep);
  233. if (ret) {
  234. dev_err(dev, "failed to request irq\n");
  235. return ret;
  236. }
  237. pp->ops = &exynos_pcie_host_ops;
  238. pp->msi_irq[0] = -ENODEV;
  239. ret = dw_pcie_host_init(pp);
  240. if (ret) {
  241. dev_err(dev, "failed to initialize host\n");
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. static const struct dw_pcie_ops dw_pcie_ops = {
  247. .read_dbi = exynos_pcie_read_dbi,
  248. .write_dbi = exynos_pcie_write_dbi,
  249. .link_up = exynos_pcie_link_up,
  250. .start_link = exynos_pcie_start_link,
  251. };
  252. static int exynos_pcie_probe(struct platform_device *pdev)
  253. {
  254. struct device *dev = &pdev->dev;
  255. struct exynos_pcie *ep;
  256. struct device_node *np = dev->of_node;
  257. int ret;
  258. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  259. if (!ep)
  260. return -ENOMEM;
  261. ep->pci.dev = dev;
  262. ep->pci.ops = &dw_pcie_ops;
  263. ep->phy = devm_of_phy_get(dev, np, NULL);
  264. if (IS_ERR(ep->phy))
  265. return PTR_ERR(ep->phy);
  266. /* External Local Bus interface (ELBI) registers */
  267. ep->elbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
  268. if (IS_ERR(ep->elbi_base))
  269. return PTR_ERR(ep->elbi_base);
  270. ep->clk = devm_clk_get(dev, "pcie");
  271. if (IS_ERR(ep->clk)) {
  272. dev_err(dev, "Failed to get pcie rc clock\n");
  273. return PTR_ERR(ep->clk);
  274. }
  275. ep->bus_clk = devm_clk_get(dev, "pcie_bus");
  276. if (IS_ERR(ep->bus_clk)) {
  277. dev_err(dev, "Failed to get pcie bus clock\n");
  278. return PTR_ERR(ep->bus_clk);
  279. }
  280. ep->supplies[0].supply = "vdd18";
  281. ep->supplies[1].supply = "vdd10";
  282. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
  283. ep->supplies);
  284. if (ret)
  285. return ret;
  286. ret = exynos_pcie_init_clk_resources(ep);
  287. if (ret)
  288. return ret;
  289. ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
  290. if (ret)
  291. return ret;
  292. platform_set_drvdata(pdev, ep);
  293. ret = exynos_add_pcie_port(ep, pdev);
  294. if (ret < 0)
  295. goto fail_probe;
  296. return 0;
  297. fail_probe:
  298. phy_exit(ep->phy);
  299. exynos_pcie_deinit_clk_resources(ep);
  300. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  301. return ret;
  302. }
  303. static int exynos_pcie_remove(struct platform_device *pdev)
  304. {
  305. struct exynos_pcie *ep = platform_get_drvdata(pdev);
  306. dw_pcie_host_deinit(&ep->pci.pp);
  307. exynos_pcie_assert_core_reset(ep);
  308. phy_power_off(ep->phy);
  309. phy_exit(ep->phy);
  310. exynos_pcie_deinit_clk_resources(ep);
  311. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  312. return 0;
  313. }
  314. static int exynos_pcie_suspend_noirq(struct device *dev)
  315. {
  316. struct exynos_pcie *ep = dev_get_drvdata(dev);
  317. exynos_pcie_assert_core_reset(ep);
  318. phy_power_off(ep->phy);
  319. phy_exit(ep->phy);
  320. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  321. return 0;
  322. }
  323. static int exynos_pcie_resume_noirq(struct device *dev)
  324. {
  325. struct exynos_pcie *ep = dev_get_drvdata(dev);
  326. struct dw_pcie *pci = &ep->pci;
  327. struct dw_pcie_rp *pp = &pci->pp;
  328. int ret;
  329. ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
  330. if (ret)
  331. return ret;
  332. /* exynos_pcie_host_init controls ep->phy */
  333. exynos_pcie_host_init(pp);
  334. dw_pcie_setup_rc(pp);
  335. exynos_pcie_start_link(pci);
  336. return dw_pcie_wait_for_link(pci);
  337. }
  338. static const struct dev_pm_ops exynos_pcie_pm_ops = {
  339. NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
  340. exynos_pcie_resume_noirq)
  341. };
  342. static const struct of_device_id exynos_pcie_of_match[] = {
  343. { .compatible = "samsung,exynos5433-pcie", },
  344. { },
  345. };
  346. static struct platform_driver exynos_pcie_driver = {
  347. .probe = exynos_pcie_probe,
  348. .remove = exynos_pcie_remove,
  349. .driver = {
  350. .name = "exynos-pcie",
  351. .of_match_table = exynos_pcie_of_match,
  352. .pm = &exynos_pcie_pm_ops,
  353. },
  354. };
  355. module_platform_driver(exynos_pcie_driver);
  356. MODULE_LICENSE("GPL v2");
  357. MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);