pci-exynos.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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_gpio.h>
  20. #include <linux/pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/resource.h>
  23. #include <linux/signal.h>
  24. #include <linux/types.h>
  25. #include "pcie-designware.h"
  26. #define to_exynos_pcie(x) container_of(x, struct exynos_pcie, pp)
  27. struct exynos_pcie {
  28. struct pcie_port pp;
  29. void __iomem *elbi_base; /* DT 0th resource */
  30. void __iomem *phy_base; /* DT 1st resource */
  31. void __iomem *block_base; /* DT 2nd resource */
  32. int reset_gpio;
  33. struct clk *clk;
  34. struct clk *bus_clk;
  35. };
  36. /* PCIe ELBI registers */
  37. #define PCIE_IRQ_PULSE 0x000
  38. #define IRQ_INTA_ASSERT (0x1 << 0)
  39. #define IRQ_INTB_ASSERT (0x1 << 2)
  40. #define IRQ_INTC_ASSERT (0x1 << 4)
  41. #define IRQ_INTD_ASSERT (0x1 << 6)
  42. #define PCIE_IRQ_LEVEL 0x004
  43. #define PCIE_IRQ_SPECIAL 0x008
  44. #define PCIE_IRQ_EN_PULSE 0x00c
  45. #define PCIE_IRQ_EN_LEVEL 0x010
  46. #define IRQ_MSI_ENABLE (0x1 << 2)
  47. #define PCIE_IRQ_EN_SPECIAL 0x014
  48. #define PCIE_PWR_RESET 0x018
  49. #define PCIE_CORE_RESET 0x01c
  50. #define PCIE_CORE_RESET_ENABLE (0x1 << 0)
  51. #define PCIE_STICKY_RESET 0x020
  52. #define PCIE_NONSTICKY_RESET 0x024
  53. #define PCIE_APP_INIT_RESET 0x028
  54. #define PCIE_APP_LTSSM_ENABLE 0x02c
  55. #define PCIE_ELBI_RDLH_LINKUP 0x064
  56. #define PCIE_ELBI_LTSSM_ENABLE 0x1
  57. #define PCIE_ELBI_SLV_AWMISC 0x11c
  58. #define PCIE_ELBI_SLV_ARMISC 0x120
  59. #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
  60. /* PCIe Purple registers */
  61. #define PCIE_PHY_GLOBAL_RESET 0x000
  62. #define PCIE_PHY_COMMON_RESET 0x004
  63. #define PCIE_PHY_CMN_REG 0x008
  64. #define PCIE_PHY_MAC_RESET 0x00c
  65. #define PCIE_PHY_PLL_LOCKED 0x010
  66. #define PCIE_PHY_TRSVREG_RESET 0x020
  67. #define PCIE_PHY_TRSV_RESET 0x024
  68. /* PCIe PHY registers */
  69. #define PCIE_PHY_IMPEDANCE 0x004
  70. #define PCIE_PHY_PLL_DIV_0 0x008
  71. #define PCIE_PHY_PLL_BIAS 0x00c
  72. #define PCIE_PHY_DCC_FEEDBACK 0x014
  73. #define PCIE_PHY_PLL_DIV_1 0x05c
  74. #define PCIE_PHY_COMMON_POWER 0x064
  75. #define PCIE_PHY_COMMON_PD_CMN (0x1 << 3)
  76. #define PCIE_PHY_TRSV0_EMP_LVL 0x084
  77. #define PCIE_PHY_TRSV0_DRV_LVL 0x088
  78. #define PCIE_PHY_TRSV0_RXCDR 0x0ac
  79. #define PCIE_PHY_TRSV0_POWER 0x0c4
  80. #define PCIE_PHY_TRSV0_PD_TSV (0x1 << 7)
  81. #define PCIE_PHY_TRSV0_LVCC 0x0dc
  82. #define PCIE_PHY_TRSV1_EMP_LVL 0x144
  83. #define PCIE_PHY_TRSV1_RXCDR 0x16c
  84. #define PCIE_PHY_TRSV1_POWER 0x184
  85. #define PCIE_PHY_TRSV1_PD_TSV (0x1 << 7)
  86. #define PCIE_PHY_TRSV1_LVCC 0x19c
  87. #define PCIE_PHY_TRSV2_EMP_LVL 0x204
  88. #define PCIE_PHY_TRSV2_RXCDR 0x22c
  89. #define PCIE_PHY_TRSV2_POWER 0x244
  90. #define PCIE_PHY_TRSV2_PD_TSV (0x1 << 7)
  91. #define PCIE_PHY_TRSV2_LVCC 0x25c
  92. #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
  93. #define PCIE_PHY_TRSV3_RXCDR 0x2ec
  94. #define PCIE_PHY_TRSV3_POWER 0x304
  95. #define PCIE_PHY_TRSV3_PD_TSV (0x1 << 7)
  96. #define PCIE_PHY_TRSV3_LVCC 0x31c
  97. static void exynos_elb_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
  98. {
  99. writel(val, exynos_pcie->elbi_base + reg);
  100. }
  101. static u32 exynos_elb_readl(struct exynos_pcie *exynos_pcie, u32 reg)
  102. {
  103. return readl(exynos_pcie->elbi_base + reg);
  104. }
  105. static void exynos_phy_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
  106. {
  107. writel(val, exynos_pcie->phy_base + reg);
  108. }
  109. static u32 exynos_phy_readl(struct exynos_pcie *exynos_pcie, u32 reg)
  110. {
  111. return readl(exynos_pcie->phy_base + reg);
  112. }
  113. static void exynos_blk_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
  114. {
  115. writel(val, exynos_pcie->block_base + reg);
  116. }
  117. static u32 exynos_blk_readl(struct exynos_pcie *exynos_pcie, u32 reg)
  118. {
  119. return readl(exynos_pcie->block_base + reg);
  120. }
  121. static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *exynos_pcie,
  122. bool on)
  123. {
  124. u32 val;
  125. if (on) {
  126. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
  127. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  128. exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
  129. } else {
  130. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
  131. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  132. exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
  133. }
  134. }
  135. static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *exynos_pcie,
  136. bool on)
  137. {
  138. u32 val;
  139. if (on) {
  140. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
  141. val |= PCIE_ELBI_SLV_DBI_ENABLE;
  142. exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
  143. } else {
  144. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
  145. val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
  146. exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
  147. }
  148. }
  149. static void exynos_pcie_assert_core_reset(struct exynos_pcie *exynos_pcie)
  150. {
  151. u32 val;
  152. val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
  153. val &= ~PCIE_CORE_RESET_ENABLE;
  154. exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
  155. exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
  156. exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
  157. exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
  158. }
  159. static void exynos_pcie_deassert_core_reset(struct exynos_pcie *exynos_pcie)
  160. {
  161. u32 val;
  162. val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
  163. val |= PCIE_CORE_RESET_ENABLE;
  164. exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
  165. exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
  166. exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
  167. exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
  168. exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
  169. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
  170. }
  171. static void exynos_pcie_assert_phy_reset(struct exynos_pcie *exynos_pcie)
  172. {
  173. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
  174. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
  175. }
  176. static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *exynos_pcie)
  177. {
  178. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
  179. exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
  180. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
  181. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
  182. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
  183. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
  184. }
  185. static void exynos_pcie_power_on_phy(struct exynos_pcie *exynos_pcie)
  186. {
  187. u32 val;
  188. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
  189. val &= ~PCIE_PHY_COMMON_PD_CMN;
  190. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
  191. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
  192. val &= ~PCIE_PHY_TRSV0_PD_TSV;
  193. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
  194. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
  195. val &= ~PCIE_PHY_TRSV1_PD_TSV;
  196. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
  197. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
  198. val &= ~PCIE_PHY_TRSV2_PD_TSV;
  199. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
  200. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
  201. val &= ~PCIE_PHY_TRSV3_PD_TSV;
  202. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
  203. }
  204. static void exynos_pcie_power_off_phy(struct exynos_pcie *exynos_pcie)
  205. {
  206. u32 val;
  207. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
  208. val |= PCIE_PHY_COMMON_PD_CMN;
  209. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
  210. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
  211. val |= PCIE_PHY_TRSV0_PD_TSV;
  212. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
  213. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
  214. val |= PCIE_PHY_TRSV1_PD_TSV;
  215. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
  216. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
  217. val |= PCIE_PHY_TRSV2_PD_TSV;
  218. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
  219. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
  220. val |= PCIE_PHY_TRSV3_PD_TSV;
  221. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
  222. }
  223. static void exynos_pcie_init_phy(struct exynos_pcie *exynos_pcie)
  224. {
  225. /* DCC feedback control off */
  226. exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
  227. /* set TX/RX impedance */
  228. exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
  229. /* set 50Mhz PHY clock */
  230. exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
  231. exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
  232. /* set TX Differential output for lane 0 */
  233. exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
  234. /* set TX Pre-emphasis Level Control for lane 0 to minimum */
  235. exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
  236. /* set RX clock and data recovery bandwidth */
  237. exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
  238. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
  239. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
  240. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
  241. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
  242. /* change TX Pre-emphasis Level Control for lanes */
  243. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
  244. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
  245. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
  246. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
  247. /* set LVCC */
  248. exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
  249. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
  250. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
  251. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
  252. }
  253. static void exynos_pcie_assert_reset(struct exynos_pcie *exynos_pcie)
  254. {
  255. struct pcie_port *pp = &exynos_pcie->pp;
  256. struct device *dev = pp->dev;
  257. if (exynos_pcie->reset_gpio >= 0)
  258. devm_gpio_request_one(dev, exynos_pcie->reset_gpio,
  259. GPIOF_OUT_INIT_HIGH, "RESET");
  260. }
  261. static int exynos_pcie_establish_link(struct exynos_pcie *exynos_pcie)
  262. {
  263. struct pcie_port *pp = &exynos_pcie->pp;
  264. struct device *dev = pp->dev;
  265. u32 val;
  266. if (dw_pcie_link_up(pp)) {
  267. dev_err(dev, "Link already up\n");
  268. return 0;
  269. }
  270. exynos_pcie_assert_core_reset(exynos_pcie);
  271. exynos_pcie_assert_phy_reset(exynos_pcie);
  272. exynos_pcie_deassert_phy_reset(exynos_pcie);
  273. exynos_pcie_power_on_phy(exynos_pcie);
  274. exynos_pcie_init_phy(exynos_pcie);
  275. /* pulse for common reset */
  276. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
  277. udelay(500);
  278. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
  279. exynos_pcie_deassert_core_reset(exynos_pcie);
  280. dw_pcie_setup_rc(pp);
  281. exynos_pcie_assert_reset(exynos_pcie);
  282. /* assert LTSSM enable */
  283. exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
  284. PCIE_APP_LTSSM_ENABLE);
  285. /* check if the link is up or not */
  286. if (!dw_pcie_wait_for_link(pp))
  287. return 0;
  288. while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
  289. val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
  290. dev_info(dev, "PLL Locked: 0x%x\n", val);
  291. }
  292. exynos_pcie_power_off_phy(exynos_pcie);
  293. return -ETIMEDOUT;
  294. }
  295. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *exynos_pcie)
  296. {
  297. u32 val;
  298. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
  299. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
  300. }
  301. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *exynos_pcie)
  302. {
  303. u32 val;
  304. /* enable INTX interrupt */
  305. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  306. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  307. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
  308. }
  309. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  310. {
  311. struct exynos_pcie *exynos_pcie = arg;
  312. exynos_pcie_clear_irq_pulse(exynos_pcie);
  313. return IRQ_HANDLED;
  314. }
  315. static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
  316. {
  317. struct exynos_pcie *exynos_pcie = arg;
  318. struct pcie_port *pp = &exynos_pcie->pp;
  319. return dw_handle_msi_irq(pp);
  320. }
  321. static void exynos_pcie_msi_init(struct exynos_pcie *exynos_pcie)
  322. {
  323. struct pcie_port *pp = &exynos_pcie->pp;
  324. u32 val;
  325. dw_pcie_msi_init(pp);
  326. /* enable MSI interrupt */
  327. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
  328. val |= IRQ_MSI_ENABLE;
  329. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
  330. }
  331. static void exynos_pcie_enable_interrupts(struct exynos_pcie *exynos_pcie)
  332. {
  333. exynos_pcie_enable_irq_pulse(exynos_pcie);
  334. if (IS_ENABLED(CONFIG_PCI_MSI))
  335. exynos_pcie_msi_init(exynos_pcie);
  336. }
  337. static u32 exynos_pcie_readl_rc(struct pcie_port *pp, u32 reg)
  338. {
  339. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  340. u32 val;
  341. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
  342. val = readl(pp->dbi_base + reg);
  343. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
  344. return val;
  345. }
  346. static void exynos_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val)
  347. {
  348. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  349. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
  350. writel(val, pp->dbi_base + reg);
  351. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
  352. }
  353. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  354. u32 *val)
  355. {
  356. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  357. int ret;
  358. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
  359. ret = dw_pcie_cfg_read(pp->dbi_base + where, size, val);
  360. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
  361. return ret;
  362. }
  363. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  364. u32 val)
  365. {
  366. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  367. int ret;
  368. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
  369. ret = dw_pcie_cfg_write(pp->dbi_base + where, size, val);
  370. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
  371. return ret;
  372. }
  373. static int exynos_pcie_link_up(struct pcie_port *pp)
  374. {
  375. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  376. u32 val;
  377. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
  378. if (val == PCIE_ELBI_LTSSM_ENABLE)
  379. return 1;
  380. return 0;
  381. }
  382. static void exynos_pcie_host_init(struct pcie_port *pp)
  383. {
  384. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  385. exynos_pcie_establish_link(exynos_pcie);
  386. exynos_pcie_enable_interrupts(exynos_pcie);
  387. }
  388. static struct pcie_host_ops exynos_pcie_host_ops = {
  389. .readl_rc = exynos_pcie_readl_rc,
  390. .writel_rc = exynos_pcie_writel_rc,
  391. .rd_own_conf = exynos_pcie_rd_own_conf,
  392. .wr_own_conf = exynos_pcie_wr_own_conf,
  393. .link_up = exynos_pcie_link_up,
  394. .host_init = exynos_pcie_host_init,
  395. };
  396. static int __init exynos_add_pcie_port(struct exynos_pcie *exynos_pcie,
  397. struct platform_device *pdev)
  398. {
  399. struct pcie_port *pp = &exynos_pcie->pp;
  400. struct device *dev = pp->dev;
  401. int ret;
  402. pp->irq = platform_get_irq(pdev, 1);
  403. if (!pp->irq) {
  404. dev_err(dev, "failed to get irq\n");
  405. return -ENODEV;
  406. }
  407. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  408. IRQF_SHARED, "exynos-pcie", exynos_pcie);
  409. if (ret) {
  410. dev_err(dev, "failed to request irq\n");
  411. return ret;
  412. }
  413. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  414. pp->msi_irq = platform_get_irq(pdev, 0);
  415. if (!pp->msi_irq) {
  416. dev_err(dev, "failed to get msi irq\n");
  417. return -ENODEV;
  418. }
  419. ret = devm_request_irq(dev, pp->msi_irq,
  420. exynos_pcie_msi_irq_handler,
  421. IRQF_SHARED | IRQF_NO_THREAD,
  422. "exynos-pcie", exynos_pcie);
  423. if (ret) {
  424. dev_err(dev, "failed to request msi irq\n");
  425. return ret;
  426. }
  427. }
  428. pp->root_bus_nr = -1;
  429. pp->ops = &exynos_pcie_host_ops;
  430. ret = dw_pcie_host_init(pp);
  431. if (ret) {
  432. dev_err(dev, "failed to initialize host\n");
  433. return ret;
  434. }
  435. return 0;
  436. }
  437. static int __init exynos_pcie_probe(struct platform_device *pdev)
  438. {
  439. struct device *dev = &pdev->dev;
  440. struct exynos_pcie *exynos_pcie;
  441. struct pcie_port *pp;
  442. struct device_node *np = dev->of_node;
  443. struct resource *elbi_base;
  444. struct resource *phy_base;
  445. struct resource *block_base;
  446. int ret;
  447. exynos_pcie = devm_kzalloc(dev, sizeof(*exynos_pcie), GFP_KERNEL);
  448. if (!exynos_pcie)
  449. return -ENOMEM;
  450. pp = &exynos_pcie->pp;
  451. pp->dev = dev;
  452. exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  453. exynos_pcie->clk = devm_clk_get(dev, "pcie");
  454. if (IS_ERR(exynos_pcie->clk)) {
  455. dev_err(dev, "Failed to get pcie rc clock\n");
  456. return PTR_ERR(exynos_pcie->clk);
  457. }
  458. ret = clk_prepare_enable(exynos_pcie->clk);
  459. if (ret)
  460. return ret;
  461. exynos_pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
  462. if (IS_ERR(exynos_pcie->bus_clk)) {
  463. dev_err(dev, "Failed to get pcie bus clock\n");
  464. ret = PTR_ERR(exynos_pcie->bus_clk);
  465. goto fail_clk;
  466. }
  467. ret = clk_prepare_enable(exynos_pcie->bus_clk);
  468. if (ret)
  469. goto fail_clk;
  470. elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  471. exynos_pcie->elbi_base = devm_ioremap_resource(dev, elbi_base);
  472. if (IS_ERR(exynos_pcie->elbi_base)) {
  473. ret = PTR_ERR(exynos_pcie->elbi_base);
  474. goto fail_bus_clk;
  475. }
  476. phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  477. exynos_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
  478. if (IS_ERR(exynos_pcie->phy_base)) {
  479. ret = PTR_ERR(exynos_pcie->phy_base);
  480. goto fail_bus_clk;
  481. }
  482. block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  483. exynos_pcie->block_base = devm_ioremap_resource(dev, block_base);
  484. if (IS_ERR(exynos_pcie->block_base)) {
  485. ret = PTR_ERR(exynos_pcie->block_base);
  486. goto fail_bus_clk;
  487. }
  488. ret = exynos_add_pcie_port(exynos_pcie, pdev);
  489. if (ret < 0)
  490. goto fail_bus_clk;
  491. platform_set_drvdata(pdev, exynos_pcie);
  492. return 0;
  493. fail_bus_clk:
  494. clk_disable_unprepare(exynos_pcie->bus_clk);
  495. fail_clk:
  496. clk_disable_unprepare(exynos_pcie->clk);
  497. return ret;
  498. }
  499. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  500. {
  501. struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
  502. clk_disable_unprepare(exynos_pcie->bus_clk);
  503. clk_disable_unprepare(exynos_pcie->clk);
  504. return 0;
  505. }
  506. static const struct of_device_id exynos_pcie_of_match[] = {
  507. { .compatible = "samsung,exynos5440-pcie", },
  508. {},
  509. };
  510. static struct platform_driver exynos_pcie_driver = {
  511. .remove = __exit_p(exynos_pcie_remove),
  512. .driver = {
  513. .name = "exynos-pcie",
  514. .of_match_table = exynos_pcie_of_match,
  515. },
  516. };
  517. /* Exynos PCIe driver does not allow module unload */
  518. static int __init exynos_pcie_init(void)
  519. {
  520. return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  521. }
  522. subsys_initcall(exynos_pcie_init);