pci-exynos.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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) dev_get_drvdata((x)->dev)
  27. struct exynos_pcie {
  28. struct dw_pcie *pci;
  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 dw_pcie *pci = exynos_pcie->pci;
  256. struct device *dev = pci->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 dw_pcie *pci = exynos_pcie->pci;
  264. struct pcie_port *pp = &pci->pp;
  265. struct device *dev = pci->dev;
  266. u32 val;
  267. if (dw_pcie_link_up(pci)) {
  268. dev_err(dev, "Link already up\n");
  269. return 0;
  270. }
  271. exynos_pcie_assert_core_reset(exynos_pcie);
  272. exynos_pcie_assert_phy_reset(exynos_pcie);
  273. exynos_pcie_deassert_phy_reset(exynos_pcie);
  274. exynos_pcie_power_on_phy(exynos_pcie);
  275. exynos_pcie_init_phy(exynos_pcie);
  276. /* pulse for common reset */
  277. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
  278. udelay(500);
  279. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
  280. exynos_pcie_deassert_core_reset(exynos_pcie);
  281. dw_pcie_setup_rc(pp);
  282. exynos_pcie_assert_reset(exynos_pcie);
  283. /* assert LTSSM enable */
  284. exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
  285. PCIE_APP_LTSSM_ENABLE);
  286. /* check if the link is up or not */
  287. if (!dw_pcie_wait_for_link(pci))
  288. return 0;
  289. while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
  290. val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
  291. dev_info(dev, "PLL Locked: 0x%x\n", val);
  292. }
  293. exynos_pcie_power_off_phy(exynos_pcie);
  294. return -ETIMEDOUT;
  295. }
  296. static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *exynos_pcie)
  297. {
  298. u32 val;
  299. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
  300. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
  301. }
  302. static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *exynos_pcie)
  303. {
  304. u32 val;
  305. /* enable INTX interrupt */
  306. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  307. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  308. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
  309. }
  310. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  311. {
  312. struct exynos_pcie *exynos_pcie = arg;
  313. exynos_pcie_clear_irq_pulse(exynos_pcie);
  314. return IRQ_HANDLED;
  315. }
  316. static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
  317. {
  318. struct exynos_pcie *exynos_pcie = arg;
  319. struct dw_pcie *pci = exynos_pcie->pci;
  320. struct pcie_port *pp = &pci->pp;
  321. return dw_handle_msi_irq(pp);
  322. }
  323. static void exynos_pcie_msi_init(struct exynos_pcie *exynos_pcie)
  324. {
  325. struct dw_pcie *pci = exynos_pcie->pci;
  326. struct pcie_port *pp = &pci->pp;
  327. u32 val;
  328. dw_pcie_msi_init(pp);
  329. /* enable MSI interrupt */
  330. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
  331. val |= IRQ_MSI_ENABLE;
  332. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
  333. }
  334. static void exynos_pcie_enable_interrupts(struct exynos_pcie *exynos_pcie)
  335. {
  336. exynos_pcie_enable_irq_pulse(exynos_pcie);
  337. if (IS_ENABLED(CONFIG_PCI_MSI))
  338. exynos_pcie_msi_init(exynos_pcie);
  339. }
  340. static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
  341. u32 reg, size_t size)
  342. {
  343. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  344. u32 val;
  345. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
  346. dw_pcie_read(base + reg, size, &val);
  347. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
  348. return val;
  349. }
  350. static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
  351. u32 reg, size_t size, u32 val)
  352. {
  353. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  354. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
  355. dw_pcie_write(base + reg, size, val);
  356. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
  357. }
  358. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  359. u32 *val)
  360. {
  361. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  362. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  363. int ret;
  364. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
  365. ret = dw_pcie_read(pci->dbi_base + where, size, val);
  366. exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
  367. return ret;
  368. }
  369. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  370. u32 val)
  371. {
  372. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  373. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  374. int ret;
  375. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
  376. ret = dw_pcie_write(pci->dbi_base + where, size, val);
  377. exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
  378. return ret;
  379. }
  380. static int exynos_pcie_link_up(struct dw_pcie *pci)
  381. {
  382. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  383. u32 val;
  384. val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
  385. if (val == PCIE_ELBI_LTSSM_ENABLE)
  386. return 1;
  387. return 0;
  388. }
  389. static void exynos_pcie_host_init(struct pcie_port *pp)
  390. {
  391. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  392. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pci);
  393. exynos_pcie_establish_link(exynos_pcie);
  394. exynos_pcie_enable_interrupts(exynos_pcie);
  395. }
  396. static struct dw_pcie_host_ops exynos_pcie_host_ops = {
  397. .rd_own_conf = exynos_pcie_rd_own_conf,
  398. .wr_own_conf = exynos_pcie_wr_own_conf,
  399. .host_init = exynos_pcie_host_init,
  400. };
  401. static int __init exynos_add_pcie_port(struct exynos_pcie *exynos_pcie,
  402. struct platform_device *pdev)
  403. {
  404. struct dw_pcie *pci = exynos_pcie->pci;
  405. struct pcie_port *pp = &pci->pp;
  406. struct device *dev = &pdev->dev;
  407. int ret;
  408. pp->irq = platform_get_irq(pdev, 1);
  409. if (!pp->irq) {
  410. dev_err(dev, "failed to get irq\n");
  411. return -ENODEV;
  412. }
  413. ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
  414. IRQF_SHARED, "exynos-pcie", exynos_pcie);
  415. if (ret) {
  416. dev_err(dev, "failed to request irq\n");
  417. return ret;
  418. }
  419. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  420. pp->msi_irq = platform_get_irq(pdev, 0);
  421. if (!pp->msi_irq) {
  422. dev_err(dev, "failed to get msi irq\n");
  423. return -ENODEV;
  424. }
  425. ret = devm_request_irq(dev, pp->msi_irq,
  426. exynos_pcie_msi_irq_handler,
  427. IRQF_SHARED | IRQF_NO_THREAD,
  428. "exynos-pcie", exynos_pcie);
  429. if (ret) {
  430. dev_err(dev, "failed to request msi irq\n");
  431. return ret;
  432. }
  433. }
  434. pp->root_bus_nr = -1;
  435. pp->ops = &exynos_pcie_host_ops;
  436. ret = dw_pcie_host_init(pp);
  437. if (ret) {
  438. dev_err(dev, "failed to initialize host\n");
  439. return ret;
  440. }
  441. return 0;
  442. }
  443. static const struct dw_pcie_ops dw_pcie_ops = {
  444. .read_dbi = exynos_pcie_read_dbi,
  445. .write_dbi = exynos_pcie_write_dbi,
  446. .link_up = exynos_pcie_link_up,
  447. };
  448. static int __init exynos_pcie_probe(struct platform_device *pdev)
  449. {
  450. struct device *dev = &pdev->dev;
  451. struct dw_pcie *pci;
  452. struct exynos_pcie *exynos_pcie;
  453. struct device_node *np = dev->of_node;
  454. struct resource *elbi_base;
  455. struct resource *phy_base;
  456. struct resource *block_base;
  457. int ret;
  458. exynos_pcie = devm_kzalloc(dev, sizeof(*exynos_pcie), GFP_KERNEL);
  459. if (!exynos_pcie)
  460. return -ENOMEM;
  461. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  462. if (!pci)
  463. return -ENOMEM;
  464. pci->dev = dev;
  465. pci->ops = &dw_pcie_ops;
  466. exynos_pcie->pci = pci;
  467. exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  468. exynos_pcie->clk = devm_clk_get(dev, "pcie");
  469. if (IS_ERR(exynos_pcie->clk)) {
  470. dev_err(dev, "Failed to get pcie rc clock\n");
  471. return PTR_ERR(exynos_pcie->clk);
  472. }
  473. ret = clk_prepare_enable(exynos_pcie->clk);
  474. if (ret)
  475. return ret;
  476. exynos_pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
  477. if (IS_ERR(exynos_pcie->bus_clk)) {
  478. dev_err(dev, "Failed to get pcie bus clock\n");
  479. ret = PTR_ERR(exynos_pcie->bus_clk);
  480. goto fail_clk;
  481. }
  482. ret = clk_prepare_enable(exynos_pcie->bus_clk);
  483. if (ret)
  484. goto fail_clk;
  485. elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  486. exynos_pcie->elbi_base = devm_ioremap_resource(dev, elbi_base);
  487. if (IS_ERR(exynos_pcie->elbi_base)) {
  488. ret = PTR_ERR(exynos_pcie->elbi_base);
  489. goto fail_bus_clk;
  490. }
  491. phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  492. exynos_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
  493. if (IS_ERR(exynos_pcie->phy_base)) {
  494. ret = PTR_ERR(exynos_pcie->phy_base);
  495. goto fail_bus_clk;
  496. }
  497. block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  498. exynos_pcie->block_base = devm_ioremap_resource(dev, block_base);
  499. if (IS_ERR(exynos_pcie->block_base)) {
  500. ret = PTR_ERR(exynos_pcie->block_base);
  501. goto fail_bus_clk;
  502. }
  503. platform_set_drvdata(pdev, exynos_pcie);
  504. ret = exynos_add_pcie_port(exynos_pcie, pdev);
  505. if (ret < 0)
  506. goto fail_bus_clk;
  507. return 0;
  508. fail_bus_clk:
  509. clk_disable_unprepare(exynos_pcie->bus_clk);
  510. fail_clk:
  511. clk_disable_unprepare(exynos_pcie->clk);
  512. return ret;
  513. }
  514. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  515. {
  516. struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
  517. clk_disable_unprepare(exynos_pcie->bus_clk);
  518. clk_disable_unprepare(exynos_pcie->clk);
  519. return 0;
  520. }
  521. static const struct of_device_id exynos_pcie_of_match[] = {
  522. { .compatible = "samsung,exynos5440-pcie", },
  523. {},
  524. };
  525. static struct platform_driver exynos_pcie_driver = {
  526. .remove = __exit_p(exynos_pcie_remove),
  527. .driver = {
  528. .name = "exynos-pcie",
  529. .of_match_table = exynos_pcie_of_match,
  530. },
  531. };
  532. /* Exynos PCIe driver does not allow module unload */
  533. static int __init exynos_pcie_init(void)
  534. {
  535. return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  536. }
  537. subsys_initcall(exynos_pcie_init);