pci-exynos.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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/module.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. void __iomem *elbi_base;
  29. void __iomem *phy_base;
  30. void __iomem *block_base;
  31. int reset_gpio;
  32. struct clk *clk;
  33. struct clk *bus_clk;
  34. struct pcie_port pp;
  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 inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
  98. {
  99. writel(val, pcie->elbi_base + reg);
  100. }
  101. static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg)
  102. {
  103. return readl(pcie->elbi_base + reg);
  104. }
  105. static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
  106. {
  107. writel(val, pcie->phy_base + reg);
  108. }
  109. static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg)
  110. {
  111. return readl(pcie->phy_base + reg);
  112. }
  113. static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
  114. {
  115. writel(val, pcie->block_base + reg);
  116. }
  117. static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg)
  118. {
  119. return readl(pcie->block_base + reg);
  120. }
  121. static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
  122. {
  123. u32 val;
  124. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  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 pcie_port *pp, bool on)
  136. {
  137. u32 val;
  138. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  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 pcie_port *pp)
  150. {
  151. u32 val;
  152. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  153. val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
  154. val &= ~PCIE_CORE_RESET_ENABLE;
  155. exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
  156. exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
  157. exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
  158. exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
  159. }
  160. static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
  161. {
  162. u32 val;
  163. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  164. val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
  165. val |= PCIE_CORE_RESET_ENABLE;
  166. exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
  167. exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
  168. exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
  169. exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
  170. exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
  171. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
  172. }
  173. static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
  174. {
  175. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  176. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
  177. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
  178. }
  179. static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
  180. {
  181. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  182. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
  183. exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
  184. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
  185. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
  186. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
  187. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
  188. }
  189. static void exynos_pcie_power_on_phy(struct pcie_port *pp)
  190. {
  191. u32 val;
  192. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  193. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
  194. val &= ~PCIE_PHY_COMMON_PD_CMN;
  195. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
  196. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
  197. val &= ~PCIE_PHY_TRSV0_PD_TSV;
  198. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
  199. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
  200. val &= ~PCIE_PHY_TRSV1_PD_TSV;
  201. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
  202. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
  203. val &= ~PCIE_PHY_TRSV2_PD_TSV;
  204. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
  205. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
  206. val &= ~PCIE_PHY_TRSV3_PD_TSV;
  207. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
  208. }
  209. static void exynos_pcie_power_off_phy(struct pcie_port *pp)
  210. {
  211. u32 val;
  212. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  213. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
  214. val |= PCIE_PHY_COMMON_PD_CMN;
  215. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
  216. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
  217. val |= PCIE_PHY_TRSV0_PD_TSV;
  218. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
  219. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
  220. val |= PCIE_PHY_TRSV1_PD_TSV;
  221. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
  222. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
  223. val |= PCIE_PHY_TRSV2_PD_TSV;
  224. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
  225. val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
  226. val |= PCIE_PHY_TRSV3_PD_TSV;
  227. exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
  228. }
  229. static void exynos_pcie_init_phy(struct pcie_port *pp)
  230. {
  231. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  232. /* DCC feedback control off */
  233. exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
  234. /* set TX/RX impedance */
  235. exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
  236. /* set 50Mhz PHY clock */
  237. exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
  238. exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
  239. /* set TX Differential output for lane 0 */
  240. exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
  241. /* set TX Pre-emphasis Level Control for lane 0 to minimum */
  242. exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
  243. /* set RX clock and data recovery bandwidth */
  244. exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
  245. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
  246. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
  247. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
  248. exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
  249. /* change TX Pre-emphasis Level Control for lanes */
  250. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
  251. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
  252. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
  253. exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
  254. /* set LVCC */
  255. exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
  256. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
  257. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
  258. exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
  259. }
  260. static void exynos_pcie_assert_reset(struct pcie_port *pp)
  261. {
  262. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  263. if (exynos_pcie->reset_gpio >= 0)
  264. devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio,
  265. GPIOF_OUT_INIT_HIGH, "RESET");
  266. }
  267. static int exynos_pcie_establish_link(struct pcie_port *pp)
  268. {
  269. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  270. u32 val;
  271. unsigned int retries;
  272. if (dw_pcie_link_up(pp)) {
  273. dev_err(pp->dev, "Link already up\n");
  274. return 0;
  275. }
  276. /* assert reset signals */
  277. exynos_pcie_assert_core_reset(pp);
  278. exynos_pcie_assert_phy_reset(pp);
  279. /* de-assert phy reset */
  280. exynos_pcie_deassert_phy_reset(pp);
  281. /* power on phy */
  282. exynos_pcie_power_on_phy(pp);
  283. /* initialize phy */
  284. exynos_pcie_init_phy(pp);
  285. /* pulse for common reset */
  286. exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
  287. udelay(500);
  288. exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
  289. /* de-assert core reset */
  290. exynos_pcie_deassert_core_reset(pp);
  291. /* setup root complex */
  292. dw_pcie_setup_rc(pp);
  293. /* assert reset signal */
  294. exynos_pcie_assert_reset(pp);
  295. /* assert LTSSM enable */
  296. exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
  297. PCIE_APP_LTSSM_ENABLE);
  298. /* check if the link is up or not */
  299. for (retries = 0; retries < 10; retries++) {
  300. if (dw_pcie_link_up(pp)) {
  301. dev_info(pp->dev, "Link up\n");
  302. return 0;
  303. }
  304. mdelay(100);
  305. }
  306. while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
  307. val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
  308. dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
  309. }
  310. /* power off phy */
  311. exynos_pcie_power_off_phy(pp);
  312. dev_err(pp->dev, "PCIe Link Fail\n");
  313. return -EINVAL;
  314. }
  315. static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
  316. {
  317. u32 val;
  318. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  319. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
  320. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
  321. }
  322. static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
  323. {
  324. u32 val;
  325. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  326. /* enable INTX interrupt */
  327. val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
  328. IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
  329. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
  330. }
  331. static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
  332. {
  333. struct pcie_port *pp = arg;
  334. exynos_pcie_clear_irq_pulse(pp);
  335. return IRQ_HANDLED;
  336. }
  337. static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
  338. {
  339. struct pcie_port *pp = arg;
  340. return dw_handle_msi_irq(pp);
  341. }
  342. static void exynos_pcie_msi_init(struct pcie_port *pp)
  343. {
  344. u32 val;
  345. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  346. dw_pcie_msi_init(pp);
  347. /* enable MSI interrupt */
  348. val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
  349. val |= IRQ_MSI_ENABLE;
  350. exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
  351. }
  352. static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
  353. {
  354. exynos_pcie_enable_irq_pulse(pp);
  355. if (IS_ENABLED(CONFIG_PCI_MSI))
  356. exynos_pcie_msi_init(pp);
  357. }
  358. static inline void exynos_pcie_readl_rc(struct pcie_port *pp,
  359. void __iomem *dbi_base, u32 *val)
  360. {
  361. exynos_pcie_sideband_dbi_r_mode(pp, true);
  362. *val = readl(dbi_base);
  363. exynos_pcie_sideband_dbi_r_mode(pp, false);
  364. }
  365. static inline void exynos_pcie_writel_rc(struct pcie_port *pp,
  366. u32 val, void __iomem *dbi_base)
  367. {
  368. exynos_pcie_sideband_dbi_w_mode(pp, true);
  369. writel(val, dbi_base);
  370. exynos_pcie_sideband_dbi_w_mode(pp, false);
  371. }
  372. static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  373. u32 *val)
  374. {
  375. int ret;
  376. exynos_pcie_sideband_dbi_r_mode(pp, true);
  377. ret = dw_pcie_cfg_read(pp->dbi_base + where, size, val);
  378. exynos_pcie_sideband_dbi_r_mode(pp, false);
  379. return ret;
  380. }
  381. static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  382. u32 val)
  383. {
  384. int ret;
  385. exynos_pcie_sideband_dbi_w_mode(pp, true);
  386. ret = dw_pcie_cfg_write(pp->dbi_base + where, size, val);
  387. exynos_pcie_sideband_dbi_w_mode(pp, false);
  388. return ret;
  389. }
  390. static int exynos_pcie_link_up(struct pcie_port *pp)
  391. {
  392. struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
  393. u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
  394. if (val == PCIE_ELBI_LTSSM_ENABLE)
  395. return 1;
  396. return 0;
  397. }
  398. static void exynos_pcie_host_init(struct pcie_port *pp)
  399. {
  400. exynos_pcie_establish_link(pp);
  401. exynos_pcie_enable_interrupts(pp);
  402. }
  403. static struct pcie_host_ops exynos_pcie_host_ops = {
  404. .readl_rc = exynos_pcie_readl_rc,
  405. .writel_rc = exynos_pcie_writel_rc,
  406. .rd_own_conf = exynos_pcie_rd_own_conf,
  407. .wr_own_conf = exynos_pcie_wr_own_conf,
  408. .link_up = exynos_pcie_link_up,
  409. .host_init = exynos_pcie_host_init,
  410. };
  411. static int __init exynos_add_pcie_port(struct pcie_port *pp,
  412. struct platform_device *pdev)
  413. {
  414. int ret;
  415. pp->irq = platform_get_irq(pdev, 1);
  416. if (!pp->irq) {
  417. dev_err(&pdev->dev, "failed to get irq\n");
  418. return -ENODEV;
  419. }
  420. ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
  421. IRQF_SHARED, "exynos-pcie", pp);
  422. if (ret) {
  423. dev_err(&pdev->dev, "failed to request irq\n");
  424. return ret;
  425. }
  426. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  427. pp->msi_irq = platform_get_irq(pdev, 0);
  428. if (!pp->msi_irq) {
  429. dev_err(&pdev->dev, "failed to get msi irq\n");
  430. return -ENODEV;
  431. }
  432. ret = devm_request_irq(&pdev->dev, pp->msi_irq,
  433. exynos_pcie_msi_irq_handler,
  434. IRQF_SHARED | IRQF_NO_THREAD,
  435. "exynos-pcie", pp);
  436. if (ret) {
  437. dev_err(&pdev->dev, "failed to request msi irq\n");
  438. return ret;
  439. }
  440. }
  441. pp->root_bus_nr = -1;
  442. pp->ops = &exynos_pcie_host_ops;
  443. ret = dw_pcie_host_init(pp);
  444. if (ret) {
  445. dev_err(&pdev->dev, "failed to initialize host\n");
  446. return ret;
  447. }
  448. return 0;
  449. }
  450. static int __init exynos_pcie_probe(struct platform_device *pdev)
  451. {
  452. struct exynos_pcie *exynos_pcie;
  453. struct pcie_port *pp;
  454. struct device_node *np = pdev->dev.of_node;
  455. struct resource *elbi_base;
  456. struct resource *phy_base;
  457. struct resource *block_base;
  458. int ret;
  459. exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
  460. GFP_KERNEL);
  461. if (!exynos_pcie)
  462. return -ENOMEM;
  463. pp = &exynos_pcie->pp;
  464. pp->dev = &pdev->dev;
  465. exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  466. exynos_pcie->clk = devm_clk_get(&pdev->dev, "pcie");
  467. if (IS_ERR(exynos_pcie->clk)) {
  468. dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
  469. return PTR_ERR(exynos_pcie->clk);
  470. }
  471. ret = clk_prepare_enable(exynos_pcie->clk);
  472. if (ret)
  473. return ret;
  474. exynos_pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
  475. if (IS_ERR(exynos_pcie->bus_clk)) {
  476. dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
  477. ret = PTR_ERR(exynos_pcie->bus_clk);
  478. goto fail_clk;
  479. }
  480. ret = clk_prepare_enable(exynos_pcie->bus_clk);
  481. if (ret)
  482. goto fail_clk;
  483. elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. exynos_pcie->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
  485. if (IS_ERR(exynos_pcie->elbi_base)) {
  486. ret = PTR_ERR(exynos_pcie->elbi_base);
  487. goto fail_bus_clk;
  488. }
  489. phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  490. exynos_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
  491. if (IS_ERR(exynos_pcie->phy_base)) {
  492. ret = PTR_ERR(exynos_pcie->phy_base);
  493. goto fail_bus_clk;
  494. }
  495. block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  496. exynos_pcie->block_base = devm_ioremap_resource(&pdev->dev, block_base);
  497. if (IS_ERR(exynos_pcie->block_base)) {
  498. ret = PTR_ERR(exynos_pcie->block_base);
  499. goto fail_bus_clk;
  500. }
  501. ret = exynos_add_pcie_port(pp, pdev);
  502. if (ret < 0)
  503. goto fail_bus_clk;
  504. platform_set_drvdata(pdev, exynos_pcie);
  505. return 0;
  506. fail_bus_clk:
  507. clk_disable_unprepare(exynos_pcie->bus_clk);
  508. fail_clk:
  509. clk_disable_unprepare(exynos_pcie->clk);
  510. return ret;
  511. }
  512. static int __exit exynos_pcie_remove(struct platform_device *pdev)
  513. {
  514. struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
  515. clk_disable_unprepare(exynos_pcie->bus_clk);
  516. clk_disable_unprepare(exynos_pcie->clk);
  517. return 0;
  518. }
  519. static const struct of_device_id exynos_pcie_of_match[] = {
  520. { .compatible = "samsung,exynos5440-pcie", },
  521. {},
  522. };
  523. MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
  524. static struct platform_driver exynos_pcie_driver = {
  525. .remove = __exit_p(exynos_pcie_remove),
  526. .driver = {
  527. .name = "exynos-pcie",
  528. .of_match_table = exynos_pcie_of_match,
  529. },
  530. };
  531. /* Exynos PCIe driver does not allow module unload */
  532. static int __init exynos_pcie_init(void)
  533. {
  534. return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
  535. }
  536. subsys_initcall(exynos_pcie_init);
  537. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  538. MODULE_DESCRIPTION("Samsung PCIe host controller driver");
  539. MODULE_LICENSE("GPL v2");