pci-exynos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Samsung EXYNOS SoCs
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com
  7. *
  8. * Author: Jingoo Han <jg1.han@samsung.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio.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/of_gpio.h>
  18. #include <linux/pci.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/resource.h>
  22. #include <linux/signal.h>
  23. #include <linux/types.h>
  24. #include "pcie-designware.h"
  25. #define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
  26. /* PCIe ELBI registers */
  27. #define PCIE_IRQ_PULSE 0x000
  28. #define IRQ_INTA_ASSERT BIT(0)
  29. #define IRQ_INTB_ASSERT BIT(2)
  30. #define IRQ_INTC_ASSERT BIT(4)
  31. #define IRQ_INTD_ASSERT BIT(6)
  32. #define PCIE_IRQ_LEVEL 0x004
  33. #define PCIE_IRQ_SPECIAL 0x008
  34. #define PCIE_IRQ_EN_PULSE 0x00c
  35. #define PCIE_IRQ_EN_LEVEL 0x010
  36. #define IRQ_MSI_ENABLE BIT(2)
  37. #define PCIE_IRQ_EN_SPECIAL 0x014
  38. #define PCIE_PWR_RESET 0x018
  39. #define PCIE_CORE_RESET 0x01c
  40. #define PCIE_CORE_RESET_ENABLE BIT(0)
  41. #define PCIE_STICKY_RESET 0x020
  42. #define PCIE_NONSTICKY_RESET 0x024
  43. #define PCIE_APP_INIT_RESET 0x028
  44. #define PCIE_APP_LTSSM_ENABLE 0x02c
  45. #define PCIE_ELBI_RDLH_LINKUP 0x064
  46. #define PCIE_ELBI_LTSSM_ENABLE 0x1
  47. #define PCIE_ELBI_SLV_AWMISC 0x11c
  48. #define PCIE_ELBI_SLV_ARMISC 0x120
  49. #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
  50. struct exynos_pcie_mem_res {
  51. void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
  52. };
  53. struct exynos_pcie_clk_res {
  54. struct clk *clk;
  55. struct clk *bus_clk;
  56. };
  57. struct exynos_pcie {
  58. struct dw_pcie *pci;
  59. struct exynos_pcie_mem_res *mem_res;
  60. struct exynos_pcie_clk_res *clk_res;
  61. const struct exynos_pcie_ops *ops;
  62. int reset_gpio;
  63. struct phy *phy;
  64. };
  65. struct exynos_pcie_ops {
  66. int (*get_mem_resources)(struct platform_device *pdev,
  67. struct exynos_pcie *ep);
  68. int (*get_clk_resources)(struct exynos_pcie *ep);
  69. int (*init_clk_resources)(struct exynos_pcie *ep);
  70. void (*deinit_clk_resources)(struct exynos_pcie *ep);
  71. };
  72. static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
  73. struct exynos_pcie *ep)
  74. {
  75. struct dw_pcie *pci = ep->pci;
  76. struct device *dev = pci->dev;
  77. struct resource *res;
  78. ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
  79. if (!ep->mem_res)
  80. return -ENOMEM;
  81. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  82. ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
  83. if (IS_ERR(ep->mem_res->elbi_base))
  84. return PTR_ERR(ep->mem_res->elbi_base);
  85. return 0;
  86. }
  87. static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
  88. {
  89. struct dw_pcie *pci = ep->pci;
  90. struct device *dev = pci->dev;
  91. ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
  92. if (!ep->clk_res)
  93. return -ENOMEM;
  94. ep->clk_res->clk = devm_clk_get(dev, "pcie");
  95. if (IS_ERR(ep->clk_res->clk)) {
  96. dev_err(dev, "Failed to get pcie rc clock\n");
  97. return PTR_ERR(ep->clk_res->clk);
  98. }
  99. ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
  100. if (IS_ERR(ep->clk_res->bus_clk)) {
  101. dev_err(dev, "Failed to get pcie bus clock\n");
  102. return PTR_ERR(ep->clk_res->bus_clk);
  103. }
  104. return 0;
  105. }
  106. static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
  107. {
  108. struct dw_pcie *pci = ep->pci;
  109. struct device *dev = pci->dev;
  110. int ret;
  111. ret = clk_prepare_enable(ep->clk_res->clk);
  112. if (ret) {
  113. dev_err(dev, "cannot enable pcie rc clock");
  114. return ret;
  115. }
  116. ret = clk_prepare_enable(ep->clk_res->bus_clk);
  117. if (ret) {
  118. dev_err(dev, "cannot enable pcie bus clock");
  119. goto err_bus_clk;
  120. }
  121. return 0;
  122. err_bus_clk:
  123. clk_disable_unprepare(ep->clk_res->clk);
  124. return ret;
  125. }
  126. static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
  127. {
  128. clk_disable_unprepare(ep->clk_res->bus_clk);
  129. clk_disable_unprepare(ep->clk_res->clk);
  130. }
  131. static const struct exynos_pcie_ops exynos5440_pcie_ops = {
  132. .get_mem_resources = exynos5440_pcie_get_mem_resources,
  133. .get_clk_resources = exynos5440_pcie_get_clk_resources,
  134. .init_clk_resources = exynos5440_pcie_init_clk_resources,
  135. .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources,
  136. };
  137. static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
  138. {
  139. writel(val, base + reg);
  140. }
  141. static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
  142. {
  143. return readl(base + reg);
  144. }
  145. static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
  146. {
  147. u32 val;
  148. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
  149. if (on)
  150. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  151. else
  152. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  153. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
  154. }
  155. static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
  156. {
  157. u32 val;
  158. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
  159. if (on)
  160. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  161. else
  162. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  163. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
  164. }
  165. static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
  166. {
  167. u32 val;
  168. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
  169. val &= ~PCIE_CORE_RESET_ENABLE;
  170. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
  171. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
  172. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
  173. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
  174. }
  175. static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
  176. {
  177. u32 val;
  178. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
  179. val |= PCIE_CORE_RESET_ENABLE;
  180. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
  181. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
  182. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
  183. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
  184. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
  185. }
  186. static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
  187. {
  188. struct dw_pcie *pci = ep->pci;
  189. struct device *dev = pci->dev;
  190. if (ep->reset_gpio >= 0)
  191. devm_gpio_request_one(dev, ep->reset_gpio,
  192. GPIOF_OUT_INIT_HIGH, "RESET");
  193. }
  194. static int exynos_pcie_establish_link(struct exynos_pcie *ep)
  195. {
  196. struct dw_pcie *pci = ep->pci;
  197. struct pcie_port *pp = &pci->pp;
  198. struct device *dev = pci->dev;
  199. if (dw_pcie_link_up(pci)) {
  200. dev_err(dev, "Link already up\n");
  201. return 0;
  202. }
  203. exynos_pcie_assert_core_reset(ep);
  204. phy_reset(ep->phy);
  205. exynos_pcie_writel(ep->mem_res->elbi_base, 1,
  206. PCIE_PWR_RESET);
  207. phy_power_on(ep->phy);
  208. phy_init(ep->phy);
  209. exynos_pcie_deassert_core_reset(ep);
  210. dw_pcie_setup_rc(pp);
  211. exynos_pcie_assert_reset(ep);
  212. /* assert LTSSM enable */
  213. exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
  214. PCIE_APP_LTSSM_ENABLE);
  215. /* check if the link is up or not */
  216. if (!dw_pcie_wait_for_link(pci))
  217. return 0;
  218. phy_power_off(ep->phy);
  219. return -ETIMEDOUT;
  220. }
  221. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
  222. {
  223. u32 val;
  224. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
  225. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
  226. }
  227. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
  228. {
  229. u32 val;
  230. /* enable INTX interrupt */
  231. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  232. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  233. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
  234. }
  235. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  236. {
  237. struct exynos_pcie *ep = arg;
  238. exynos_pcie_clear_irq_pulse(ep);
  239. return IRQ_HANDLED;
  240. }
  241. static void exynos_pcie_msi_init(struct exynos_pcie *ep)
  242. {
  243. struct dw_pcie *pci = ep->pci;
  244. struct pcie_port *pp = &pci->pp;
  245. u32 val;
  246. dw_pcie_msi_init(pp);
  247. /* enable MSI interrupt */
  248. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
  249. val |= IRQ_MSI_ENABLE;
  250. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
  251. }
  252. static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
  253. {
  254. exynos_pcie_enable_irq_pulse(ep);
  255. if (IS_ENABLED(CONFIG_PCI_MSI))
  256. exynos_pcie_msi_init(ep);
  257. }
  258. static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  259. u32 reg, size_t size)
  260. {
  261. struct exynos_pcie *ep = to_exynos_pcie(pci);
  262. u32 val;
  263. exynos_pcie_sideband_dbi_r_mode(ep, true);
  264. dw_pcie_read(base + reg, size, &val);
  265. exynos_pcie_sideband_dbi_r_mode(ep, false);
  266. return val;
  267. }
  268. static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  269. u32 reg, size_t size, u32 val)
  270. {
  271. struct exynos_pcie *ep = to_exynos_pcie(pci);
  272. exynos_pcie_sideband_dbi_w_mode(ep, true);
  273. dw_pcie_write(base + reg, size, val);
  274. exynos_pcie_sideband_dbi_w_mode(ep, false);
  275. }
  276. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  277. u32 *val)
  278. {
  279. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  280. struct exynos_pcie *ep = to_exynos_pcie(pci);
  281. int ret;
  282. exynos_pcie_sideband_dbi_r_mode(ep, true);
  283. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  284. exynos_pcie_sideband_dbi_r_mode(ep, false);
  285. return ret;
  286. }
  287. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  288. u32 val)
  289. {
  290. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  291. struct exynos_pcie *ep = to_exynos_pcie(pci);
  292. int ret;
  293. exynos_pcie_sideband_dbi_w_mode(ep, true);
  294. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  295. exynos_pcie_sideband_dbi_w_mode(ep, false);
  296. return ret;
  297. }
  298. static int exynos_pcie_link_up(struct dw_pcie *pci)
  299. {
  300. struct exynos_pcie *ep = to_exynos_pcie(pci);
  301. u32 val;
  302. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
  303. if (val == PCIE_ELBI_LTSSM_ENABLE)
  304. return 1;
  305. return 0;
  306. }
  307. static int exynos_pcie_host_init(struct pcie_port *pp)
  308. {
  309. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  310. struct exynos_pcie *ep = to_exynos_pcie(pci);
  311. exynos_pcie_establish_link(ep);
  312. exynos_pcie_enable_interrupts(ep);
  313. return 0;
  314. }
  315. static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
  316. .rd_own_conf = exynos_pcie_rd_own_conf,
  317. .wr_own_conf = exynos_pcie_wr_own_conf,
  318. .host_init = exynos_pcie_host_init,
  319. };
  320. static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
  321. struct platform_device *pdev)
  322. {
  323. struct dw_pcie *pci = ep->pci;
  324. struct pcie_port *pp = &pci->pp;
  325. struct device *dev = &pdev->dev;
  326. int ret;
  327. pp->irq = platform_get_irq(pdev, 1);
  328. if (pp->irq < 0) {
  329. dev_err(dev, "failed to get irq\n");
  330. return pp->irq;
  331. }
  332. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  333. IRQF_SHARED, "exynos-pcie", ep);
  334. if (ret) {
  335. dev_err(dev, "failed to request irq\n");
  336. return ret;
  337. }
  338. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  339. pp->msi_irq = platform_get_irq(pdev, 0);
  340. if (pp->msi_irq < 0) {
  341. dev_err(dev, "failed to get msi irq\n");
  342. return pp->msi_irq;
  343. }
  344. }
  345. pp->ops = &exynos_pcie_host_ops;
  346. ret = dw_pcie_host_init(pp);
  347. if (ret) {
  348. dev_err(dev, "failed to initialize host\n");
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. static const struct dw_pcie_ops dw_pcie_ops = {
  354. .read_dbi = exynos_pcie_read_dbi,
  355. .write_dbi = exynos_pcie_write_dbi,
  356. .link_up = exynos_pcie_link_up,
  357. };
  358. static int __init exynos_pcie_probe(struct platform_device *pdev)
  359. {
  360. struct device *dev = &pdev->dev;
  361. struct dw_pcie *pci;
  362. struct exynos_pcie *ep;
  363. struct device_node *np = dev->of_node;
  364. int ret;
  365. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  366. if (!ep)
  367. return -ENOMEM;
  368. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  369. if (!pci)
  370. return -ENOMEM;
  371. pci->dev = dev;
  372. pci->ops = &dw_pcie_ops;
  373. ep->pci = pci;
  374. ep->ops = (const struct exynos_pcie_ops *)
  375. of_device_get_match_data(dev);
  376. ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  377. ep->phy = devm_of_phy_get(dev, np, NULL);
  378. if (IS_ERR(ep->phy)) {
  379. if (PTR_ERR(ep->phy) != -ENODEV)
  380. return PTR_ERR(ep->phy);
  381. ep->phy = NULL;
  382. }
  383. if (ep->ops && ep->ops->get_mem_resources) {
  384. ret = ep->ops->get_mem_resources(pdev, ep);
  385. if (ret)
  386. return ret;
  387. }
  388. if (ep->ops && ep->ops->get_clk_resources &&
  389. ep->ops->init_clk_resources) {
  390. ret = ep->ops->get_clk_resources(ep);
  391. if (ret)
  392. return ret;
  393. ret = ep->ops->init_clk_resources(ep);
  394. if (ret)
  395. return ret;
  396. }
  397. platform_set_drvdata(pdev, ep);
  398. ret = exynos_add_pcie_port(ep, pdev);
  399. if (ret < 0)
  400. goto fail_probe;
  401. return 0;
  402. fail_probe:
  403. phy_exit(ep->phy);
  404. if (ep->ops && ep->ops->deinit_clk_resources)
  405. ep->ops->deinit_clk_resources(ep);
  406. return ret;
  407. }
  408. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  409. {
  410. struct exynos_pcie *ep = platform_get_drvdata(pdev);
  411. if (ep->ops && ep->ops->deinit_clk_resources)
  412. ep->ops->deinit_clk_resources(ep);
  413. return 0;
  414. }
  415. static const struct of_device_id exynos_pcie_of_match[] = {
  416. {
  417. .compatible = "samsung,exynos5440-pcie",
  418. .data = &exynos5440_pcie_ops
  419. },
  420. {},
  421. };
  422. static struct platform_driver exynos_pcie_driver = {
  423. .remove = __exit_p(exynos_pcie_remove),
  424. .driver = {
  425. .name = "exynos-pcie",
  426. .of_match_table = exynos_pcie_of_match,
  427. },
  428. };
  429. /* Exynos PCIe driver does not allow module unload */
  430. static int __init exynos_pcie_init(void)
  431. {
  432. return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  433. }
  434. subsys_initcall(exynos_pcie_init);