pci-exynos.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * PCIe host controller driver for Samsung EXYNOS SoCs
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Author: Jingoo Han <jg1.han@samsung.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/gpio.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/resource.h>
  25. #include <linux/signal.h>
  26. #include <linux/types.h>
  27. #include "pcie-designware.h"
  28. #define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
  29. /* PCIe ELBI registers */
  30. #define PCIE_IRQ_PULSE 0x000
  31. #define IRQ_INTA_ASSERT BIT(0)
  32. #define IRQ_INTB_ASSERT BIT(2)
  33. #define IRQ_INTC_ASSERT BIT(4)
  34. #define IRQ_INTD_ASSERT BIT(6)
  35. #define PCIE_IRQ_LEVEL 0x004
  36. #define PCIE_IRQ_SPECIAL 0x008
  37. #define PCIE_IRQ_EN_PULSE 0x00c
  38. #define PCIE_IRQ_EN_LEVEL 0x010
  39. #define IRQ_MSI_ENABLE BIT(2)
  40. #define PCIE_IRQ_EN_SPECIAL 0x014
  41. #define PCIE_PWR_RESET 0x018
  42. #define PCIE_CORE_RESET 0x01c
  43. #define PCIE_CORE_RESET_ENABLE BIT(0)
  44. #define PCIE_STICKY_RESET 0x020
  45. #define PCIE_NONSTICKY_RESET 0x024
  46. #define PCIE_APP_INIT_RESET 0x028
  47. #define PCIE_APP_LTSSM_ENABLE 0x02c
  48. #define PCIE_ELBI_RDLH_LINKUP 0x064
  49. #define PCIE_ELBI_LTSSM_ENABLE 0x1
  50. #define PCIE_ELBI_SLV_AWMISC 0x11c
  51. #define PCIE_ELBI_SLV_ARMISC 0x120
  52. #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
  53. /* PCIe Purple registers */
  54. #define PCIE_PHY_GLOBAL_RESET 0x000
  55. #define PCIE_PHY_COMMON_RESET 0x004
  56. #define PCIE_PHY_CMN_REG 0x008
  57. #define PCIE_PHY_MAC_RESET 0x00c
  58. #define PCIE_PHY_PLL_LOCKED 0x010
  59. #define PCIE_PHY_TRSVREG_RESET 0x020
  60. #define PCIE_PHY_TRSV_RESET 0x024
  61. /* PCIe PHY registers */
  62. #define PCIE_PHY_IMPEDANCE 0x004
  63. #define PCIE_PHY_PLL_DIV_0 0x008
  64. #define PCIE_PHY_PLL_BIAS 0x00c
  65. #define PCIE_PHY_DCC_FEEDBACK 0x014
  66. #define PCIE_PHY_PLL_DIV_1 0x05c
  67. #define PCIE_PHY_COMMON_POWER 0x064
  68. #define PCIE_PHY_COMMON_PD_CMN BIT(3)
  69. #define PCIE_PHY_TRSV0_EMP_LVL 0x084
  70. #define PCIE_PHY_TRSV0_DRV_LVL 0x088
  71. #define PCIE_PHY_TRSV0_RXCDR 0x0ac
  72. #define PCIE_PHY_TRSV0_POWER 0x0c4
  73. #define PCIE_PHY_TRSV0_PD_TSV BIT(7)
  74. #define PCIE_PHY_TRSV0_LVCC 0x0dc
  75. #define PCIE_PHY_TRSV1_EMP_LVL 0x144
  76. #define PCIE_PHY_TRSV1_RXCDR 0x16c
  77. #define PCIE_PHY_TRSV1_POWER 0x184
  78. #define PCIE_PHY_TRSV1_PD_TSV BIT(7)
  79. #define PCIE_PHY_TRSV1_LVCC 0x19c
  80. #define PCIE_PHY_TRSV2_EMP_LVL 0x204
  81. #define PCIE_PHY_TRSV2_RXCDR 0x22c
  82. #define PCIE_PHY_TRSV2_POWER 0x244
  83. #define PCIE_PHY_TRSV2_PD_TSV BIT(7)
  84. #define PCIE_PHY_TRSV2_LVCC 0x25c
  85. #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
  86. #define PCIE_PHY_TRSV3_RXCDR 0x2ec
  87. #define PCIE_PHY_TRSV3_POWER 0x304
  88. #define PCIE_PHY_TRSV3_PD_TSV BIT(7)
  89. #define PCIE_PHY_TRSV3_LVCC 0x31c
  90. struct exynos_pcie_mem_res {
  91. void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
  92. void __iomem *phy_base; /* DT 1st resource: PHY CTRL */
  93. void __iomem *block_base; /* DT 2nd resource: PHY ADDITIONAL CTRL */
  94. };
  95. struct exynos_pcie_clk_res {
  96. struct clk *clk;
  97. struct clk *bus_clk;
  98. };
  99. struct exynos_pcie {
  100. struct dw_pcie *pci;
  101. struct exynos_pcie_mem_res *mem_res;
  102. struct exynos_pcie_clk_res *clk_res;
  103. const struct exynos_pcie_ops *ops;
  104. int reset_gpio;
  105. /* For Generic PHY Framework */
  106. bool using_phy;
  107. struct phy *phy;
  108. };
  109. struct exynos_pcie_ops {
  110. int (*get_mem_resources)(struct platform_device *pdev,
  111. struct exynos_pcie *ep);
  112. int (*get_clk_resources)(struct exynos_pcie *ep);
  113. int (*init_clk_resources)(struct exynos_pcie *ep);
  114. void (*deinit_clk_resources)(struct exynos_pcie *ep);
  115. };
  116. static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
  117. struct exynos_pcie *ep)
  118. {
  119. struct dw_pcie *pci = ep->pci;
  120. struct device *dev = pci->dev;
  121. struct resource *res;
  122. ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
  123. if (!ep->mem_res)
  124. return -ENOMEM;
  125. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  126. ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
  127. if (IS_ERR(ep->mem_res->elbi_base))
  128. return PTR_ERR(ep->mem_res->elbi_base);
  129. /* If using the PHY framework, doesn't need to get other resource */
  130. if (ep->using_phy)
  131. return 0;
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  133. ep->mem_res->phy_base = devm_ioremap_resource(dev, res);
  134. if (IS_ERR(ep->mem_res->phy_base))
  135. return PTR_ERR(ep->mem_res->phy_base);
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  137. ep->mem_res->block_base = devm_ioremap_resource(dev, res);
  138. if (IS_ERR(ep->mem_res->block_base))
  139. return PTR_ERR(ep->mem_res->block_base);
  140. return 0;
  141. }
  142. static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
  143. {
  144. struct dw_pcie *pci = ep->pci;
  145. struct device *dev = pci->dev;
  146. ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
  147. if (!ep->clk_res)
  148. return -ENOMEM;
  149. ep->clk_res->clk = devm_clk_get(dev, "pcie");
  150. if (IS_ERR(ep->clk_res->clk)) {
  151. dev_err(dev, "Failed to get pcie rc clock\n");
  152. return PTR_ERR(ep->clk_res->clk);
  153. }
  154. ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
  155. if (IS_ERR(ep->clk_res->bus_clk)) {
  156. dev_err(dev, "Failed to get pcie bus clock\n");
  157. return PTR_ERR(ep->clk_res->bus_clk);
  158. }
  159. return 0;
  160. }
  161. static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
  162. {
  163. struct dw_pcie *pci = ep->pci;
  164. struct device *dev = pci->dev;
  165. int ret;
  166. ret = clk_prepare_enable(ep->clk_res->clk);
  167. if (ret) {
  168. dev_err(dev, "cannot enable pcie rc clock");
  169. return ret;
  170. }
  171. ret = clk_prepare_enable(ep->clk_res->bus_clk);
  172. if (ret) {
  173. dev_err(dev, "cannot enable pcie bus clock");
  174. goto err_bus_clk;
  175. }
  176. return 0;
  177. err_bus_clk:
  178. clk_disable_unprepare(ep->clk_res->clk);
  179. return ret;
  180. }
  181. static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
  182. {
  183. clk_disable_unprepare(ep->clk_res->bus_clk);
  184. clk_disable_unprepare(ep->clk_res->clk);
  185. }
  186. static const struct exynos_pcie_ops exynos5440_pcie_ops = {
  187. .get_mem_resources = exynos5440_pcie_get_mem_resources,
  188. .get_clk_resources = exynos5440_pcie_get_clk_resources,
  189. .init_clk_resources = exynos5440_pcie_init_clk_resources,
  190. .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources,
  191. };
  192. static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
  193. {
  194. writel(val, base + reg);
  195. }
  196. static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
  197. {
  198. return readl(base + reg);
  199. }
  200. static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
  201. {
  202. u32 val;
  203. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
  204. if (on)
  205. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  206. else
  207. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  208. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
  209. }
  210. static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
  211. {
  212. u32 val;
  213. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
  214. if (on)
  215. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  216. else
  217. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  218. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
  219. }
  220. static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
  221. {
  222. u32 val;
  223. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
  224. val &= ~PCIE_CORE_RESET_ENABLE;
  225. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
  226. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
  227. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
  228. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
  229. }
  230. static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
  231. {
  232. u32 val;
  233. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
  234. val |= PCIE_CORE_RESET_ENABLE;
  235. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
  236. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
  237. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
  238. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
  239. exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
  240. exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_MAC_RESET);
  241. }
  242. static void exynos_pcie_assert_phy_reset(struct exynos_pcie *ep)
  243. {
  244. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_MAC_RESET);
  245. exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_GLOBAL_RESET);
  246. }
  247. static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *ep)
  248. {
  249. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_GLOBAL_RESET);
  250. exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_PWR_RESET);
  251. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
  252. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_CMN_REG);
  253. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSVREG_RESET);
  254. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSV_RESET);
  255. }
  256. static void exynos_pcie_power_on_phy(struct exynos_pcie *ep)
  257. {
  258. u32 val;
  259. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
  260. val &= ~PCIE_PHY_COMMON_PD_CMN;
  261. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
  262. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
  263. val &= ~PCIE_PHY_TRSV0_PD_TSV;
  264. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
  265. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
  266. val &= ~PCIE_PHY_TRSV1_PD_TSV;
  267. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
  268. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
  269. val &= ~PCIE_PHY_TRSV2_PD_TSV;
  270. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
  271. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
  272. val &= ~PCIE_PHY_TRSV3_PD_TSV;
  273. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
  274. }
  275. static void exynos_pcie_power_off_phy(struct exynos_pcie *ep)
  276. {
  277. u32 val;
  278. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
  279. val |= PCIE_PHY_COMMON_PD_CMN;
  280. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
  281. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
  282. val |= PCIE_PHY_TRSV0_PD_TSV;
  283. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
  284. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
  285. val |= PCIE_PHY_TRSV1_PD_TSV;
  286. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
  287. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
  288. val |= PCIE_PHY_TRSV2_PD_TSV;
  289. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
  290. val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
  291. val |= PCIE_PHY_TRSV3_PD_TSV;
  292. exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
  293. }
  294. static void exynos_pcie_init_phy(struct exynos_pcie *ep)
  295. {
  296. /* DCC feedback control off */
  297. exynos_pcie_writel(ep->mem_res->phy_base, 0x29, PCIE_PHY_DCC_FEEDBACK);
  298. /* set TX/RX impedance */
  299. exynos_pcie_writel(ep->mem_res->phy_base, 0xd5, PCIE_PHY_IMPEDANCE);
  300. /* set 50Mhz PHY clock */
  301. exynos_pcie_writel(ep->mem_res->phy_base, 0x14, PCIE_PHY_PLL_DIV_0);
  302. exynos_pcie_writel(ep->mem_res->phy_base, 0x12, PCIE_PHY_PLL_DIV_1);
  303. /* set TX Differential output for lane 0 */
  304. exynos_pcie_writel(ep->mem_res->phy_base, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
  305. /* set TX Pre-emphasis Level Control for lane 0 to minimum */
  306. exynos_pcie_writel(ep->mem_res->phy_base, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
  307. /* set RX clock and data recovery bandwidth */
  308. exynos_pcie_writel(ep->mem_res->phy_base, 0xe7, PCIE_PHY_PLL_BIAS);
  309. exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV0_RXCDR);
  310. exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV1_RXCDR);
  311. exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV2_RXCDR);
  312. exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV3_RXCDR);
  313. /* change TX Pre-emphasis Level Control for lanes */
  314. exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
  315. exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
  316. exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
  317. exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
  318. /* set LVCC */
  319. exynos_pcie_writel(ep->mem_res->phy_base, 0x20, PCIE_PHY_TRSV0_LVCC);
  320. exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV1_LVCC);
  321. exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV2_LVCC);
  322. exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV3_LVCC);
  323. }
  324. static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
  325. {
  326. struct dw_pcie *pci = ep->pci;
  327. struct device *dev = pci->dev;
  328. if (ep->reset_gpio >= 0)
  329. devm_gpio_request_one(dev, ep->reset_gpio,
  330. GPIOF_OUT_INIT_HIGH, "RESET");
  331. }
  332. static int exynos_pcie_establish_link(struct exynos_pcie *ep)
  333. {
  334. struct dw_pcie *pci = ep->pci;
  335. struct pcie_port *pp = &pci->pp;
  336. struct device *dev = pci->dev;
  337. u32 val;
  338. if (dw_pcie_link_up(pci)) {
  339. dev_err(dev, "Link already up\n");
  340. return 0;
  341. }
  342. exynos_pcie_assert_core_reset(ep);
  343. if (ep->using_phy) {
  344. phy_reset(ep->phy);
  345. exynos_pcie_writel(ep->mem_res->elbi_base, 1,
  346. PCIE_PWR_RESET);
  347. phy_power_on(ep->phy);
  348. phy_init(ep->phy);
  349. } else {
  350. exynos_pcie_assert_phy_reset(ep);
  351. exynos_pcie_deassert_phy_reset(ep);
  352. exynos_pcie_power_on_phy(ep);
  353. exynos_pcie_init_phy(ep);
  354. /* pulse for common reset */
  355. exynos_pcie_writel(ep->mem_res->block_base, 1,
  356. PCIE_PHY_COMMON_RESET);
  357. udelay(500);
  358. exynos_pcie_writel(ep->mem_res->block_base, 0,
  359. PCIE_PHY_COMMON_RESET);
  360. }
  361. /* pulse for common reset */
  362. exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_COMMON_RESET);
  363. udelay(500);
  364. exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
  365. exynos_pcie_deassert_core_reset(ep);
  366. dw_pcie_setup_rc(pp);
  367. exynos_pcie_assert_reset(ep);
  368. /* assert LTSSM enable */
  369. exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
  370. PCIE_APP_LTSSM_ENABLE);
  371. /* check if the link is up or not */
  372. if (!dw_pcie_wait_for_link(pci))
  373. return 0;
  374. if (ep->using_phy) {
  375. phy_power_off(ep->phy);
  376. return -ETIMEDOUT;
  377. }
  378. while (exynos_pcie_readl(ep->mem_res->phy_base,
  379. PCIE_PHY_PLL_LOCKED) == 0) {
  380. val = exynos_pcie_readl(ep->mem_res->block_base,
  381. PCIE_PHY_PLL_LOCKED);
  382. dev_info(dev, "PLL Locked: 0x%x\n", val);
  383. }
  384. exynos_pcie_power_off_phy(ep);
  385. return -ETIMEDOUT;
  386. }
  387. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
  388. {
  389. u32 val;
  390. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
  391. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
  392. }
  393. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
  394. {
  395. u32 val;
  396. /* enable INTX interrupt */
  397. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  398. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  399. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
  400. }
  401. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  402. {
  403. struct exynos_pcie *ep = arg;
  404. exynos_pcie_clear_irq_pulse(ep);
  405. return IRQ_HANDLED;
  406. }
  407. static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
  408. {
  409. struct exynos_pcie *ep = arg;
  410. struct dw_pcie *pci = ep->pci;
  411. struct pcie_port *pp = &pci->pp;
  412. return dw_handle_msi_irq(pp);
  413. }
  414. static void exynos_pcie_msi_init(struct exynos_pcie *ep)
  415. {
  416. struct dw_pcie *pci = ep->pci;
  417. struct pcie_port *pp = &pci->pp;
  418. u32 val;
  419. dw_pcie_msi_init(pp);
  420. /* enable MSI interrupt */
  421. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
  422. val |= IRQ_MSI_ENABLE;
  423. exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
  424. }
  425. static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
  426. {
  427. exynos_pcie_enable_irq_pulse(ep);
  428. if (IS_ENABLED(CONFIG_PCI_MSI))
  429. exynos_pcie_msi_init(ep);
  430. }
  431. static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  432. u32 reg, size_t size)
  433. {
  434. struct exynos_pcie *ep = to_exynos_pcie(pci);
  435. u32 val;
  436. exynos_pcie_sideband_dbi_r_mode(ep, true);
  437. dw_pcie_read(base + reg, size, &val);
  438. exynos_pcie_sideband_dbi_r_mode(ep, false);
  439. return val;
  440. }
  441. static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  442. u32 reg, size_t size, u32 val)
  443. {
  444. struct exynos_pcie *ep = to_exynos_pcie(pci);
  445. exynos_pcie_sideband_dbi_w_mode(ep, true);
  446. dw_pcie_write(base + reg, size, val);
  447. exynos_pcie_sideband_dbi_w_mode(ep, false);
  448. }
  449. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  450. u32 *val)
  451. {
  452. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  453. struct exynos_pcie *ep = to_exynos_pcie(pci);
  454. int ret;
  455. exynos_pcie_sideband_dbi_r_mode(ep, true);
  456. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  457. exynos_pcie_sideband_dbi_r_mode(ep, false);
  458. return ret;
  459. }
  460. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  461. u32 val)
  462. {
  463. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  464. struct exynos_pcie *ep = to_exynos_pcie(pci);
  465. int ret;
  466. exynos_pcie_sideband_dbi_w_mode(ep, true);
  467. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  468. exynos_pcie_sideband_dbi_w_mode(ep, false);
  469. return ret;
  470. }
  471. static int exynos_pcie_link_up(struct dw_pcie *pci)
  472. {
  473. struct exynos_pcie *ep = to_exynos_pcie(pci);
  474. u32 val;
  475. val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
  476. if (val == PCIE_ELBI_LTSSM_ENABLE)
  477. return 1;
  478. return 0;
  479. }
  480. static int exynos_pcie_host_init(struct pcie_port *pp)
  481. {
  482. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  483. struct exynos_pcie *ep = to_exynos_pcie(pci);
  484. exynos_pcie_establish_link(ep);
  485. exynos_pcie_enable_interrupts(ep);
  486. return 0;
  487. }
  488. static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
  489. .rd_own_conf = exynos_pcie_rd_own_conf,
  490. .wr_own_conf = exynos_pcie_wr_own_conf,
  491. .host_init = exynos_pcie_host_init,
  492. };
  493. static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
  494. struct platform_device *pdev)
  495. {
  496. struct dw_pcie *pci = ep->pci;
  497. struct pcie_port *pp = &pci->pp;
  498. struct device *dev = &pdev->dev;
  499. int ret;
  500. pp->irq = platform_get_irq(pdev, 1);
  501. if (pp->irq < 0) {
  502. dev_err(dev, "failed to get irq\n");
  503. return pp->irq;
  504. }
  505. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  506. IRQF_SHARED, "exynos-pcie", ep);
  507. if (ret) {
  508. dev_err(dev, "failed to request irq\n");
  509. return ret;
  510. }
  511. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  512. pp->msi_irq = platform_get_irq(pdev, 0);
  513. if (pp->msi_irq < 0) {
  514. dev_err(dev, "failed to get msi irq\n");
  515. return pp->msi_irq;
  516. }
  517. ret = devm_request_irq(dev, pp->msi_irq,
  518. exynos_pcie_msi_irq_handler,
  519. IRQF_SHARED | IRQF_NO_THREAD,
  520. "exynos-pcie", ep);
  521. if (ret) {
  522. dev_err(dev, "failed to request msi irq\n");
  523. return ret;
  524. }
  525. }
  526. pp->root_bus_nr = -1;
  527. pp->ops = &exynos_pcie_host_ops;
  528. ret = dw_pcie_host_init(pp);
  529. if (ret) {
  530. dev_err(dev, "failed to initialize host\n");
  531. return ret;
  532. }
  533. return 0;
  534. }
  535. static const struct dw_pcie_ops dw_pcie_ops = {
  536. .read_dbi = exynos_pcie_read_dbi,
  537. .write_dbi = exynos_pcie_write_dbi,
  538. .link_up = exynos_pcie_link_up,
  539. };
  540. static int __init exynos_pcie_probe(struct platform_device *pdev)
  541. {
  542. struct device *dev = &pdev->dev;
  543. struct dw_pcie *pci;
  544. struct exynos_pcie *ep;
  545. struct device_node *np = dev->of_node;
  546. int ret;
  547. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  548. if (!ep)
  549. return -ENOMEM;
  550. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  551. if (!pci)
  552. return -ENOMEM;
  553. pci->dev = dev;
  554. pci->ops = &dw_pcie_ops;
  555. ep->pci = pci;
  556. ep->ops = (const struct exynos_pcie_ops *)
  557. of_device_get_match_data(dev);
  558. ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  559. /* Assume that controller doesn't use the PHY framework */
  560. ep->using_phy = false;
  561. ep->phy = devm_of_phy_get(dev, np, NULL);
  562. if (IS_ERR(ep->phy)) {
  563. if (PTR_ERR(ep->phy) == -EPROBE_DEFER)
  564. return PTR_ERR(ep->phy);
  565. dev_warn(dev, "Use the 'phy' property. Current DT of pci-exynos was deprecated!!\n");
  566. } else
  567. ep->using_phy = true;
  568. if (ep->ops && ep->ops->get_mem_resources) {
  569. ret = ep->ops->get_mem_resources(pdev, ep);
  570. if (ret)
  571. return ret;
  572. }
  573. if (ep->ops && ep->ops->get_clk_resources) {
  574. ret = ep->ops->get_clk_resources(ep);
  575. if (ret)
  576. return ret;
  577. ret = ep->ops->init_clk_resources(ep);
  578. if (ret)
  579. return ret;
  580. }
  581. platform_set_drvdata(pdev, ep);
  582. ret = exynos_add_pcie_port(ep, pdev);
  583. if (ret < 0)
  584. goto fail_probe;
  585. return 0;
  586. fail_probe:
  587. if (ep->using_phy)
  588. phy_exit(ep->phy);
  589. if (ep->ops && ep->ops->deinit_clk_resources)
  590. ep->ops->deinit_clk_resources(ep);
  591. return ret;
  592. }
  593. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  594. {
  595. struct exynos_pcie *ep = platform_get_drvdata(pdev);
  596. if (ep->ops && ep->ops->deinit_clk_resources)
  597. ep->ops->deinit_clk_resources(ep);
  598. return 0;
  599. }
  600. static const struct of_device_id exynos_pcie_of_match[] = {
  601. {
  602. .compatible = "samsung,exynos5440-pcie",
  603. .data = &exynos5440_pcie_ops
  604. },
  605. {},
  606. };
  607. static struct platform_driver exynos_pcie_driver = {
  608. .remove = __exit_p(exynos_pcie_remove),
  609. .driver = {
  610. .name = "exynos-pcie",
  611. .of_match_table = exynos_pcie_of_match,
  612. },
  613. };
  614. /* Exynos PCIe driver does not allow module unload */
  615. static int __init exynos_pcie_init(void)
  616. {
  617. return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  618. }
  619. subsys_initcall(exynos_pcie_init);