pci-exynos.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 <jg1.han@samsung.com>
  9. * Jaehoon Chung <jh80.chung@samsung.com>
  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/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/mod_devicetable.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. struct clk_bulk_data *clks;
  52. struct phy *phy;
  53. struct regulator_bulk_data supplies[2];
  54. };
  55. static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
  56. {
  57. writel(val, base + reg);
  58. }
  59. static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
  60. {
  61. return readl(base + reg);
  62. }
  63. static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
  64. {
  65. struct dw_pcie *pci = &ep->pci;
  66. u32 val;
  67. val = exynos_pcie_readl(pci->elbi_base, PCIE_ELBI_SLV_AWMISC);
  68. if (on)
  69. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  70. else
  71. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  72. exynos_pcie_writel(pci->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
  73. }
  74. static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
  75. {
  76. struct dw_pcie *pci = &ep->pci;
  77. u32 val;
  78. val = exynos_pcie_readl(pci->elbi_base, PCIE_ELBI_SLV_ARMISC);
  79. if (on)
  80. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  81. else
  82. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  83. exynos_pcie_writel(pci->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
  84. }
  85. static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
  86. {
  87. struct dw_pcie *pci = &ep->pci;
  88. u32 val;
  89. val = exynos_pcie_readl(pci->elbi_base, PCIE_CORE_RESET);
  90. val &= ~PCIE_CORE_RESET_ENABLE;
  91. exynos_pcie_writel(pci->elbi_base, val, PCIE_CORE_RESET);
  92. exynos_pcie_writel(pci->elbi_base, 0, PCIE_STICKY_RESET);
  93. exynos_pcie_writel(pci->elbi_base, 0, PCIE_NONSTICKY_RESET);
  94. }
  95. static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
  96. {
  97. struct dw_pcie *pci = &ep->pci;
  98. u32 val;
  99. val = exynos_pcie_readl(pci->elbi_base, PCIE_CORE_RESET);
  100. val |= PCIE_CORE_RESET_ENABLE;
  101. exynos_pcie_writel(pci->elbi_base, val, PCIE_CORE_RESET);
  102. exynos_pcie_writel(pci->elbi_base, 1, PCIE_STICKY_RESET);
  103. exynos_pcie_writel(pci->elbi_base, 1, PCIE_NONSTICKY_RESET);
  104. exynos_pcie_writel(pci->elbi_base, 1, PCIE_APP_INIT_RESET);
  105. exynos_pcie_writel(pci->elbi_base, 0, PCIE_APP_INIT_RESET);
  106. }
  107. static int exynos_pcie_start_link(struct dw_pcie *pci)
  108. {
  109. u32 val;
  110. val = exynos_pcie_readl(pci->elbi_base, PCIE_SW_WAKE);
  111. val &= ~PCIE_BUS_EN;
  112. exynos_pcie_writel(pci->elbi_base, val, PCIE_SW_WAKE);
  113. /* assert LTSSM enable */
  114. exynos_pcie_writel(pci->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
  115. PCIE_APP_LTSSM_ENABLE);
  116. return 0;
  117. }
  118. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
  119. {
  120. struct dw_pcie *pci = &ep->pci;
  121. u32 val = exynos_pcie_readl(pci->elbi_base, PCIE_IRQ_PULSE);
  122. exynos_pcie_writel(pci->elbi_base, val, PCIE_IRQ_PULSE);
  123. }
  124. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  125. {
  126. struct exynos_pcie *ep = arg;
  127. exynos_pcie_clear_irq_pulse(ep);
  128. return IRQ_HANDLED;
  129. }
  130. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
  131. {
  132. struct dw_pcie *pci = &ep->pci;
  133. u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  134. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  135. exynos_pcie_writel(pci->elbi_base, val, PCIE_IRQ_EN_PULSE);
  136. exynos_pcie_writel(pci->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
  137. exynos_pcie_writel(pci->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
  138. }
  139. static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  140. u32 reg, size_t size)
  141. {
  142. struct exynos_pcie *ep = to_exynos_pcie(pci);
  143. u32 val;
  144. exynos_pcie_sideband_dbi_r_mode(ep, true);
  145. dw_pcie_read(base + reg, size, &val);
  146. exynos_pcie_sideband_dbi_r_mode(ep, false);
  147. return val;
  148. }
  149. static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  150. u32 reg, size_t size, u32 val)
  151. {
  152. struct exynos_pcie *ep = to_exynos_pcie(pci);
  153. exynos_pcie_sideband_dbi_w_mode(ep, true);
  154. dw_pcie_write(base + reg, size, val);
  155. exynos_pcie_sideband_dbi_w_mode(ep, false);
  156. }
  157. static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
  158. int where, int size, u32 *val)
  159. {
  160. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  161. if (PCI_SLOT(devfn))
  162. return PCIBIOS_DEVICE_NOT_FOUND;
  163. *val = dw_pcie_read_dbi(pci, where, size);
  164. return PCIBIOS_SUCCESSFUL;
  165. }
  166. static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
  167. int where, int size, u32 val)
  168. {
  169. struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
  170. if (PCI_SLOT(devfn))
  171. return PCIBIOS_DEVICE_NOT_FOUND;
  172. dw_pcie_write_dbi(pci, where, size, val);
  173. return PCIBIOS_SUCCESSFUL;
  174. }
  175. static struct pci_ops exynos_pci_ops = {
  176. .read = exynos_pcie_rd_own_conf,
  177. .write = exynos_pcie_wr_own_conf,
  178. };
  179. static bool exynos_pcie_link_up(struct dw_pcie *pci)
  180. {
  181. u32 val = exynos_pcie_readl(pci->elbi_base, PCIE_ELBI_RDLH_LINKUP);
  182. return val & PCIE_ELBI_XMLH_LINKUP;
  183. }
  184. static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
  185. {
  186. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  187. struct exynos_pcie *ep = to_exynos_pcie(pci);
  188. pp->bridge->ops = &exynos_pci_ops;
  189. exynos_pcie_assert_core_reset(ep);
  190. phy_init(ep->phy);
  191. phy_power_on(ep->phy);
  192. exynos_pcie_deassert_core_reset(ep);
  193. exynos_pcie_enable_irq_pulse(ep);
  194. return 0;
  195. }
  196. static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
  197. .init = exynos_pcie_host_init,
  198. };
  199. static int exynos_add_pcie_port(struct exynos_pcie *ep,
  200. struct platform_device *pdev)
  201. {
  202. struct dw_pcie *pci = &ep->pci;
  203. struct dw_pcie_rp *pp = &pci->pp;
  204. struct device *dev = &pdev->dev;
  205. int ret;
  206. pp->irq = platform_get_irq(pdev, 0);
  207. if (pp->irq < 0)
  208. return pp->irq;
  209. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  210. IRQF_SHARED, "exynos-pcie", ep);
  211. if (ret) {
  212. dev_err(dev, "failed to request irq\n");
  213. return ret;
  214. }
  215. pp->ops = &exynos_pcie_host_ops;
  216. pp->msi_irq[0] = -ENODEV;
  217. ret = dw_pcie_host_init(pp);
  218. if (ret) {
  219. dev_err(dev, "failed to initialize host\n");
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static const struct dw_pcie_ops dw_pcie_ops = {
  225. .read_dbi = exynos_pcie_read_dbi,
  226. .write_dbi = exynos_pcie_write_dbi,
  227. .link_up = exynos_pcie_link_up,
  228. .start_link = exynos_pcie_start_link,
  229. };
  230. static int exynos_pcie_probe(struct platform_device *pdev)
  231. {
  232. struct device *dev = &pdev->dev;
  233. struct exynos_pcie *ep;
  234. struct device_node *np = dev->of_node;
  235. int ret;
  236. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  237. if (!ep)
  238. return -ENOMEM;
  239. ep->pci.dev = dev;
  240. ep->pci.ops = &dw_pcie_ops;
  241. ep->phy = devm_of_phy_get(dev, np, NULL);
  242. if (IS_ERR(ep->phy))
  243. return PTR_ERR(ep->phy);
  244. ret = devm_clk_bulk_get_all_enabled(dev, &ep->clks);
  245. if (ret < 0)
  246. return ret;
  247. ep->supplies[0].supply = "vdd18";
  248. ep->supplies[1].supply = "vdd10";
  249. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
  250. ep->supplies);
  251. if (ret)
  252. return ret;
  253. ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
  254. if (ret)
  255. return ret;
  256. platform_set_drvdata(pdev, ep);
  257. ret = exynos_add_pcie_port(ep, pdev);
  258. if (ret < 0)
  259. goto fail_probe;
  260. return 0;
  261. fail_probe:
  262. phy_exit(ep->phy);
  263. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  264. return ret;
  265. }
  266. static void exynos_pcie_remove(struct platform_device *pdev)
  267. {
  268. struct exynos_pcie *ep = platform_get_drvdata(pdev);
  269. dw_pcie_host_deinit(&ep->pci.pp);
  270. exynos_pcie_assert_core_reset(ep);
  271. phy_power_off(ep->phy);
  272. phy_exit(ep->phy);
  273. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  274. }
  275. static int exynos_pcie_suspend_noirq(struct device *dev)
  276. {
  277. struct exynos_pcie *ep = dev_get_drvdata(dev);
  278. exynos_pcie_assert_core_reset(ep);
  279. phy_power_off(ep->phy);
  280. phy_exit(ep->phy);
  281. regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
  282. return 0;
  283. }
  284. static int exynos_pcie_resume_noirq(struct device *dev)
  285. {
  286. struct exynos_pcie *ep = dev_get_drvdata(dev);
  287. struct dw_pcie *pci = &ep->pci;
  288. struct dw_pcie_rp *pp = &pci->pp;
  289. int ret;
  290. ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
  291. if (ret)
  292. return ret;
  293. /* exynos_pcie_host_init controls ep->phy */
  294. exynos_pcie_host_init(pp);
  295. dw_pcie_setup_rc(pp);
  296. exynos_pcie_start_link(pci);
  297. return dw_pcie_wait_for_link(pci);
  298. }
  299. static const struct dev_pm_ops exynos_pcie_pm_ops = {
  300. NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
  301. exynos_pcie_resume_noirq)
  302. };
  303. static const struct of_device_id exynos_pcie_of_match[] = {
  304. { .compatible = "samsung,exynos5433-pcie", },
  305. { },
  306. };
  307. static struct platform_driver exynos_pcie_driver = {
  308. .probe = exynos_pcie_probe,
  309. .remove = exynos_pcie_remove,
  310. .driver = {
  311. .name = "exynos-pcie",
  312. .of_match_table = exynos_pcie_of_match,
  313. .pm = &exynos_pcie_pm_ops,
  314. },
  315. };
  316. module_platform_driver(exynos_pcie_driver);
  317. MODULE_DESCRIPTION("Samsung Exynos PCIe host controller driver");
  318. MODULE_LICENSE("GPL v2");
  319. MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);