sdhci-pci-gli.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Genesys Logic, Inc.
  4. *
  5. * Authors: Ben Chuang <[email protected]>
  6. *
  7. * Version: v0.9.0 (2019-08-08)
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/pci.h>
  12. #include <linux/mmc/mmc.h>
  13. #include <linux/delay.h>
  14. #include <linux/of.h>
  15. #include <linux/iopoll.h>
  16. #include "sdhci.h"
  17. #include "sdhci-pci.h"
  18. #include "cqhci.h"
  19. /* Genesys Logic extra registers */
  20. #define SDHCI_GLI_9750_WT 0x800
  21. #define SDHCI_GLI_9750_WT_EN BIT(0)
  22. #define GLI_9750_WT_EN_ON 0x1
  23. #define GLI_9750_WT_EN_OFF 0x0
  24. #define PCI_GLI_9750_PM_CTRL 0xFC
  25. #define PCI_GLI_9750_PM_STATE GENMASK(1, 0)
  26. #define PCI_GLI_9750_CORRERR_MASK 0x214
  27. #define PCI_GLI_9750_CORRERR_MASK_REPLAY_TIMER_TIMEOUT BIT(12)
  28. #define SDHCI_GLI_9750_CFG2 0x848
  29. #define SDHCI_GLI_9750_CFG2_L1DLY GENMASK(28, 24)
  30. #define GLI_9750_CFG2_L1DLY_VALUE 0x1F
  31. #define SDHCI_GLI_9750_DRIVING 0x860
  32. #define SDHCI_GLI_9750_DRIVING_1 GENMASK(11, 0)
  33. #define SDHCI_GLI_9750_DRIVING_2 GENMASK(27, 26)
  34. #define GLI_9750_DRIVING_1_VALUE 0xFFF
  35. #define GLI_9750_DRIVING_2_VALUE 0x3
  36. #define SDHCI_GLI_9750_SEL_1 BIT(29)
  37. #define SDHCI_GLI_9750_SEL_2 BIT(31)
  38. #define SDHCI_GLI_9750_ALL_RST (BIT(24)|BIT(25)|BIT(28)|BIT(30))
  39. #define SDHCI_GLI_9750_PLL 0x864
  40. #define SDHCI_GLI_9750_PLL_LDIV GENMASK(9, 0)
  41. #define SDHCI_GLI_9750_PLL_PDIV GENMASK(14, 12)
  42. #define SDHCI_GLI_9750_PLL_DIR BIT(15)
  43. #define SDHCI_GLI_9750_PLL_TX2_INV BIT(23)
  44. #define SDHCI_GLI_9750_PLL_TX2_DLY GENMASK(22, 20)
  45. #define GLI_9750_PLL_TX2_INV_VALUE 0x1
  46. #define GLI_9750_PLL_TX2_DLY_VALUE 0x0
  47. #define SDHCI_GLI_9750_PLLSSC_STEP GENMASK(28, 24)
  48. #define SDHCI_GLI_9750_PLLSSC_EN BIT(31)
  49. #define SDHCI_GLI_9750_PLLSSC 0x86C
  50. #define SDHCI_GLI_9750_PLLSSC_PPM GENMASK(31, 16)
  51. #define SDHCI_GLI_9750_SW_CTRL 0x874
  52. #define SDHCI_GLI_9750_SW_CTRL_4 GENMASK(7, 6)
  53. #define GLI_9750_SW_CTRL_4_VALUE 0x3
  54. #define SDHCI_GLI_9750_MISC 0x878
  55. #define SDHCI_GLI_9750_MISC_TX1_INV BIT(2)
  56. #define SDHCI_GLI_9750_MISC_RX_INV BIT(3)
  57. #define SDHCI_GLI_9750_MISC_TX1_DLY GENMASK(6, 4)
  58. #define GLI_9750_MISC_TX1_INV_VALUE 0x0
  59. #define GLI_9750_MISC_RX_INV_ON 0x1
  60. #define GLI_9750_MISC_RX_INV_OFF 0x0
  61. #define GLI_9750_MISC_RX_INV_VALUE GLI_9750_MISC_RX_INV_OFF
  62. #define GLI_9750_MISC_TX1_DLY_VALUE 0x5
  63. #define SDHCI_GLI_9750_MISC_SSC_OFF BIT(26)
  64. #define SDHCI_GLI_9750_TUNING_CONTROL 0x540
  65. #define SDHCI_GLI_9750_TUNING_CONTROL_EN BIT(4)
  66. #define GLI_9750_TUNING_CONTROL_EN_ON 0x1
  67. #define GLI_9750_TUNING_CONTROL_EN_OFF 0x0
  68. #define SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1 BIT(16)
  69. #define SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2 GENMASK(20, 19)
  70. #define GLI_9750_TUNING_CONTROL_GLITCH_1_VALUE 0x1
  71. #define GLI_9750_TUNING_CONTROL_GLITCH_2_VALUE 0x2
  72. #define SDHCI_GLI_9750_TUNING_PARAMETERS 0x544
  73. #define SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY GENMASK(2, 0)
  74. #define GLI_9750_TUNING_PARAMETERS_RX_DLY_VALUE 0x1
  75. #define SDHCI_GLI_9763E_CTRL_HS400 0x7
  76. #define SDHCI_GLI_9763E_HS400_ES_REG 0x52C
  77. #define SDHCI_GLI_9763E_HS400_ES_BIT BIT(8)
  78. #define PCIE_GLI_9763E_VHS 0x884
  79. #define GLI_9763E_VHS_REV GENMASK(19, 16)
  80. #define GLI_9763E_VHS_REV_R 0x0
  81. #define GLI_9763E_VHS_REV_M 0x1
  82. #define GLI_9763E_VHS_REV_W 0x2
  83. #define PCIE_GLI_9763E_MB 0x888
  84. #define GLI_9763E_MB_CMDQ_OFF BIT(19)
  85. #define GLI_9763E_MB_ERP_ON BIT(7)
  86. #define PCIE_GLI_9763E_SCR 0x8E0
  87. #define GLI_9763E_SCR_AXI_REQ BIT(9)
  88. #define PCIE_GLI_9763E_CFG 0x8A0
  89. #define GLI_9763E_CFG_LPSN_DIS BIT(12)
  90. #define PCIE_GLI_9763E_CFG2 0x8A4
  91. #define GLI_9763E_CFG2_L1DLY GENMASK(28, 19)
  92. #define GLI_9763E_CFG2_L1DLY_MID 0x54
  93. #define PCIE_GLI_9763E_MMC_CTRL 0x960
  94. #define GLI_9763E_HS400_SLOW BIT(3)
  95. #define PCIE_GLI_9763E_CLKRXDLY 0x934
  96. #define GLI_9763E_HS400_RXDLY GENMASK(31, 28)
  97. #define GLI_9763E_HS400_RXDLY_5 0x5
  98. #define SDHCI_GLI_9763E_CQE_BASE_ADDR 0x200
  99. #define GLI_9763E_CQE_TRNS_MODE (SDHCI_TRNS_MULTI | \
  100. SDHCI_TRNS_BLK_CNT_EN | \
  101. SDHCI_TRNS_DMA)
  102. #define PCI_GLI_9755_WT 0x800
  103. #define PCI_GLI_9755_WT_EN BIT(0)
  104. #define GLI_9755_WT_EN_ON 0x1
  105. #define GLI_9755_WT_EN_OFF 0x0
  106. #define PCI_GLI_9755_PECONF 0x44
  107. #define PCI_GLI_9755_LFCLK GENMASK(14, 12)
  108. #define PCI_GLI_9755_DMACLK BIT(29)
  109. #define PCI_GLI_9755_INVERT_CD BIT(30)
  110. #define PCI_GLI_9755_INVERT_WP BIT(31)
  111. #define PCI_GLI_9755_CFG2 0x48
  112. #define PCI_GLI_9755_CFG2_L1DLY GENMASK(28, 24)
  113. #define GLI_9755_CFG2_L1DLY_VALUE 0x1F
  114. #define PCI_GLI_9755_PLL 0x64
  115. #define PCI_GLI_9755_PLL_LDIV GENMASK(9, 0)
  116. #define PCI_GLI_9755_PLL_PDIV GENMASK(14, 12)
  117. #define PCI_GLI_9755_PLL_DIR BIT(15)
  118. #define PCI_GLI_9755_PLLSSC_STEP GENMASK(28, 24)
  119. #define PCI_GLI_9755_PLLSSC_EN BIT(31)
  120. #define PCI_GLI_9755_PLLSSC 0x68
  121. #define PCI_GLI_9755_PLLSSC_PPM GENMASK(15, 0)
  122. #define PCI_GLI_9755_SerDes 0x70
  123. #define PCI_GLI_9755_SCP_DIS BIT(19)
  124. #define PCI_GLI_9755_MISC 0x78
  125. #define PCI_GLI_9755_MISC_SSC_OFF BIT(26)
  126. #define PCI_GLI_9755_PM_CTRL 0xFC
  127. #define PCI_GLI_9755_PM_STATE GENMASK(1, 0)
  128. #define PCI_GLI_9755_CORRERR_MASK 0x214
  129. #define PCI_GLI_9755_CORRERR_MASK_REPLAY_TIMER_TIMEOUT BIT(12)
  130. #define GLI_MAX_TUNING_LOOP 40
  131. /* Genesys Logic chipset */
  132. static inline void gl9750_wt_on(struct sdhci_host *host)
  133. {
  134. u32 wt_value;
  135. u32 wt_enable;
  136. wt_value = sdhci_readl(host, SDHCI_GLI_9750_WT);
  137. wt_enable = FIELD_GET(SDHCI_GLI_9750_WT_EN, wt_value);
  138. if (wt_enable == GLI_9750_WT_EN_ON)
  139. return;
  140. wt_value &= ~SDHCI_GLI_9750_WT_EN;
  141. wt_value |= FIELD_PREP(SDHCI_GLI_9750_WT_EN, GLI_9750_WT_EN_ON);
  142. sdhci_writel(host, wt_value, SDHCI_GLI_9750_WT);
  143. }
  144. static inline void gl9750_wt_off(struct sdhci_host *host)
  145. {
  146. u32 wt_value;
  147. u32 wt_enable;
  148. wt_value = sdhci_readl(host, SDHCI_GLI_9750_WT);
  149. wt_enable = FIELD_GET(SDHCI_GLI_9750_WT_EN, wt_value);
  150. if (wt_enable == GLI_9750_WT_EN_OFF)
  151. return;
  152. wt_value &= ~SDHCI_GLI_9750_WT_EN;
  153. wt_value |= FIELD_PREP(SDHCI_GLI_9750_WT_EN, GLI_9750_WT_EN_OFF);
  154. sdhci_writel(host, wt_value, SDHCI_GLI_9750_WT);
  155. }
  156. static void gli_set_9750(struct sdhci_host *host)
  157. {
  158. u32 driving_value;
  159. u32 pll_value;
  160. u32 sw_ctrl_value;
  161. u32 misc_value;
  162. u32 parameter_value;
  163. u32 control_value;
  164. u16 ctrl2;
  165. gl9750_wt_on(host);
  166. driving_value = sdhci_readl(host, SDHCI_GLI_9750_DRIVING);
  167. pll_value = sdhci_readl(host, SDHCI_GLI_9750_PLL);
  168. sw_ctrl_value = sdhci_readl(host, SDHCI_GLI_9750_SW_CTRL);
  169. misc_value = sdhci_readl(host, SDHCI_GLI_9750_MISC);
  170. parameter_value = sdhci_readl(host, SDHCI_GLI_9750_TUNING_PARAMETERS);
  171. control_value = sdhci_readl(host, SDHCI_GLI_9750_TUNING_CONTROL);
  172. driving_value &= ~(SDHCI_GLI_9750_DRIVING_1);
  173. driving_value &= ~(SDHCI_GLI_9750_DRIVING_2);
  174. driving_value |= FIELD_PREP(SDHCI_GLI_9750_DRIVING_1,
  175. GLI_9750_DRIVING_1_VALUE);
  176. driving_value |= FIELD_PREP(SDHCI_GLI_9750_DRIVING_2,
  177. GLI_9750_DRIVING_2_VALUE);
  178. driving_value &= ~(SDHCI_GLI_9750_SEL_1|SDHCI_GLI_9750_SEL_2|SDHCI_GLI_9750_ALL_RST);
  179. driving_value |= SDHCI_GLI_9750_SEL_2;
  180. sdhci_writel(host, driving_value, SDHCI_GLI_9750_DRIVING);
  181. sw_ctrl_value &= ~SDHCI_GLI_9750_SW_CTRL_4;
  182. sw_ctrl_value |= FIELD_PREP(SDHCI_GLI_9750_SW_CTRL_4,
  183. GLI_9750_SW_CTRL_4_VALUE);
  184. sdhci_writel(host, sw_ctrl_value, SDHCI_GLI_9750_SW_CTRL);
  185. /* reset the tuning flow after reinit and before starting tuning */
  186. pll_value &= ~SDHCI_GLI_9750_PLL_TX2_INV;
  187. pll_value &= ~SDHCI_GLI_9750_PLL_TX2_DLY;
  188. pll_value |= FIELD_PREP(SDHCI_GLI_9750_PLL_TX2_INV,
  189. GLI_9750_PLL_TX2_INV_VALUE);
  190. pll_value |= FIELD_PREP(SDHCI_GLI_9750_PLL_TX2_DLY,
  191. GLI_9750_PLL_TX2_DLY_VALUE);
  192. misc_value &= ~SDHCI_GLI_9750_MISC_TX1_INV;
  193. misc_value &= ~SDHCI_GLI_9750_MISC_RX_INV;
  194. misc_value &= ~SDHCI_GLI_9750_MISC_TX1_DLY;
  195. misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_TX1_INV,
  196. GLI_9750_MISC_TX1_INV_VALUE);
  197. misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
  198. GLI_9750_MISC_RX_INV_VALUE);
  199. misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_TX1_DLY,
  200. GLI_9750_MISC_TX1_DLY_VALUE);
  201. parameter_value &= ~SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY;
  202. parameter_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY,
  203. GLI_9750_TUNING_PARAMETERS_RX_DLY_VALUE);
  204. control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1;
  205. control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2;
  206. control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1,
  207. GLI_9750_TUNING_CONTROL_GLITCH_1_VALUE);
  208. control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2,
  209. GLI_9750_TUNING_CONTROL_GLITCH_2_VALUE);
  210. sdhci_writel(host, pll_value, SDHCI_GLI_9750_PLL);
  211. sdhci_writel(host, misc_value, SDHCI_GLI_9750_MISC);
  212. /* disable tuned clk */
  213. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  214. ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
  215. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  216. /* enable tuning parameters control */
  217. control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_EN;
  218. control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_EN,
  219. GLI_9750_TUNING_CONTROL_EN_ON);
  220. sdhci_writel(host, control_value, SDHCI_GLI_9750_TUNING_CONTROL);
  221. /* write tuning parameters */
  222. sdhci_writel(host, parameter_value, SDHCI_GLI_9750_TUNING_PARAMETERS);
  223. /* disable tuning parameters control */
  224. control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_EN;
  225. control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_EN,
  226. GLI_9750_TUNING_CONTROL_EN_OFF);
  227. sdhci_writel(host, control_value, SDHCI_GLI_9750_TUNING_CONTROL);
  228. /* clear tuned clk */
  229. ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  230. ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
  231. sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
  232. gl9750_wt_off(host);
  233. }
  234. static void gli_set_9750_rx_inv(struct sdhci_host *host, bool b)
  235. {
  236. u32 misc_value;
  237. gl9750_wt_on(host);
  238. misc_value = sdhci_readl(host, SDHCI_GLI_9750_MISC);
  239. misc_value &= ~SDHCI_GLI_9750_MISC_RX_INV;
  240. if (b) {
  241. misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
  242. GLI_9750_MISC_RX_INV_ON);
  243. } else {
  244. misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
  245. GLI_9750_MISC_RX_INV_OFF);
  246. }
  247. sdhci_writel(host, misc_value, SDHCI_GLI_9750_MISC);
  248. gl9750_wt_off(host);
  249. }
  250. static int __sdhci_execute_tuning_9750(struct sdhci_host *host, u32 opcode)
  251. {
  252. int i;
  253. int rx_inv;
  254. for (rx_inv = 0; rx_inv < 2; rx_inv++) {
  255. gli_set_9750_rx_inv(host, !!rx_inv);
  256. sdhci_start_tuning(host);
  257. for (i = 0; i < GLI_MAX_TUNING_LOOP; i++) {
  258. u16 ctrl;
  259. sdhci_send_tuning(host, opcode);
  260. if (!host->tuning_done) {
  261. sdhci_abort_tuning(host, opcode);
  262. break;
  263. }
  264. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  265. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  266. if (ctrl & SDHCI_CTRL_TUNED_CLK)
  267. return 0; /* Success! */
  268. break;
  269. }
  270. }
  271. }
  272. if (!host->tuning_done) {
  273. pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
  274. mmc_hostname(host->mmc));
  275. return -ETIMEDOUT;
  276. }
  277. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  278. mmc_hostname(host->mmc));
  279. sdhci_reset_tuning(host);
  280. return -EAGAIN;
  281. }
  282. static int gl9750_execute_tuning(struct sdhci_host *host, u32 opcode)
  283. {
  284. host->mmc->retune_period = 0;
  285. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  286. host->mmc->retune_period = host->tuning_count;
  287. gli_set_9750(host);
  288. host->tuning_err = __sdhci_execute_tuning_9750(host, opcode);
  289. sdhci_end_tuning(host);
  290. return 0;
  291. }
  292. static void gl9750_disable_ssc_pll(struct sdhci_host *host)
  293. {
  294. u32 pll;
  295. gl9750_wt_on(host);
  296. pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
  297. pll &= ~(SDHCI_GLI_9750_PLL_DIR | SDHCI_GLI_9750_PLLSSC_EN);
  298. sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
  299. gl9750_wt_off(host);
  300. }
  301. static void gl9750_set_pll(struct sdhci_host *host, u8 dir, u16 ldiv, u8 pdiv)
  302. {
  303. u32 pll;
  304. gl9750_wt_on(host);
  305. pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
  306. pll &= ~(SDHCI_GLI_9750_PLL_LDIV |
  307. SDHCI_GLI_9750_PLL_PDIV |
  308. SDHCI_GLI_9750_PLL_DIR);
  309. pll |= FIELD_PREP(SDHCI_GLI_9750_PLL_LDIV, ldiv) |
  310. FIELD_PREP(SDHCI_GLI_9750_PLL_PDIV, pdiv) |
  311. FIELD_PREP(SDHCI_GLI_9750_PLL_DIR, dir);
  312. sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
  313. gl9750_wt_off(host);
  314. /* wait for pll stable */
  315. mdelay(1);
  316. }
  317. static bool gl9750_ssc_enable(struct sdhci_host *host)
  318. {
  319. u32 misc;
  320. u8 off;
  321. gl9750_wt_on(host);
  322. misc = sdhci_readl(host, SDHCI_GLI_9750_MISC);
  323. off = FIELD_GET(SDHCI_GLI_9750_MISC_SSC_OFF, misc);
  324. gl9750_wt_off(host);
  325. return !off;
  326. }
  327. static void gl9750_set_ssc(struct sdhci_host *host, u8 enable, u8 step, u16 ppm)
  328. {
  329. u32 pll;
  330. u32 ssc;
  331. gl9750_wt_on(host);
  332. pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
  333. ssc = sdhci_readl(host, SDHCI_GLI_9750_PLLSSC);
  334. pll &= ~(SDHCI_GLI_9750_PLLSSC_STEP |
  335. SDHCI_GLI_9750_PLLSSC_EN);
  336. ssc &= ~SDHCI_GLI_9750_PLLSSC_PPM;
  337. pll |= FIELD_PREP(SDHCI_GLI_9750_PLLSSC_STEP, step) |
  338. FIELD_PREP(SDHCI_GLI_9750_PLLSSC_EN, enable);
  339. ssc |= FIELD_PREP(SDHCI_GLI_9750_PLLSSC_PPM, ppm);
  340. sdhci_writel(host, ssc, SDHCI_GLI_9750_PLLSSC);
  341. sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
  342. gl9750_wt_off(host);
  343. }
  344. static void gl9750_set_ssc_pll_205mhz(struct sdhci_host *host)
  345. {
  346. bool enable = gl9750_ssc_enable(host);
  347. /* set pll to 205MHz and ssc */
  348. gl9750_set_ssc(host, enable, 0xF, 0x5A1D);
  349. gl9750_set_pll(host, 0x1, 0x246, 0x0);
  350. }
  351. static void gl9750_set_ssc_pll_100mhz(struct sdhci_host *host)
  352. {
  353. bool enable = gl9750_ssc_enable(host);
  354. /* set pll to 100MHz and ssc */
  355. gl9750_set_ssc(host, enable, 0xE, 0x51EC);
  356. gl9750_set_pll(host, 0x1, 0x244, 0x1);
  357. }
  358. static void gl9750_set_ssc_pll_50mhz(struct sdhci_host *host)
  359. {
  360. bool enable = gl9750_ssc_enable(host);
  361. /* set pll to 50MHz and ssc */
  362. gl9750_set_ssc(host, enable, 0xE, 0x51EC);
  363. gl9750_set_pll(host, 0x1, 0x244, 0x3);
  364. }
  365. static void sdhci_gl9750_set_clock(struct sdhci_host *host, unsigned int clock)
  366. {
  367. struct mmc_ios *ios = &host->mmc->ios;
  368. u16 clk;
  369. host->mmc->actual_clock = 0;
  370. gl9750_disable_ssc_pll(host);
  371. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  372. if (clock == 0)
  373. return;
  374. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  375. if (clock == 200000000 && ios->timing == MMC_TIMING_UHS_SDR104) {
  376. host->mmc->actual_clock = 205000000;
  377. gl9750_set_ssc_pll_205mhz(host);
  378. } else if (clock == 100000000) {
  379. gl9750_set_ssc_pll_100mhz(host);
  380. } else if (clock == 50000000) {
  381. gl9750_set_ssc_pll_50mhz(host);
  382. }
  383. sdhci_enable_clk(host, clk);
  384. }
  385. static void gl9750_hw_setting(struct sdhci_host *host)
  386. {
  387. struct sdhci_pci_slot *slot = sdhci_priv(host);
  388. struct pci_dev *pdev;
  389. u32 value;
  390. pdev = slot->chip->pdev;
  391. gl9750_wt_on(host);
  392. value = sdhci_readl(host, SDHCI_GLI_9750_CFG2);
  393. value &= ~SDHCI_GLI_9750_CFG2_L1DLY;
  394. /* set ASPM L1 entry delay to 7.9us */
  395. value |= FIELD_PREP(SDHCI_GLI_9750_CFG2_L1DLY,
  396. GLI_9750_CFG2_L1DLY_VALUE);
  397. sdhci_writel(host, value, SDHCI_GLI_9750_CFG2);
  398. /* toggle PM state to allow GL9750 to enter ASPM L1.2 */
  399. pci_read_config_dword(pdev, PCI_GLI_9750_PM_CTRL, &value);
  400. value |= PCI_GLI_9750_PM_STATE;
  401. pci_write_config_dword(pdev, PCI_GLI_9750_PM_CTRL, value);
  402. value &= ~PCI_GLI_9750_PM_STATE;
  403. pci_write_config_dword(pdev, PCI_GLI_9750_PM_CTRL, value);
  404. /* mask the replay timer timeout of AER */
  405. pci_read_config_dword(pdev, PCI_GLI_9750_CORRERR_MASK, &value);
  406. value |= PCI_GLI_9750_CORRERR_MASK_REPLAY_TIMER_TIMEOUT;
  407. pci_write_config_dword(pdev, PCI_GLI_9750_CORRERR_MASK, value);
  408. gl9750_wt_off(host);
  409. }
  410. static void gli_pcie_enable_msi(struct sdhci_pci_slot *slot)
  411. {
  412. int ret;
  413. ret = pci_alloc_irq_vectors(slot->chip->pdev, 1, 1,
  414. PCI_IRQ_MSI | PCI_IRQ_MSIX);
  415. if (ret < 0) {
  416. pr_warn("%s: enable PCI MSI failed, error=%d\n",
  417. mmc_hostname(slot->host->mmc), ret);
  418. return;
  419. }
  420. slot->host->irq = pci_irq_vector(slot->chip->pdev, 0);
  421. }
  422. static inline void gl9755_wt_on(struct pci_dev *pdev)
  423. {
  424. u32 wt_value;
  425. u32 wt_enable;
  426. pci_read_config_dword(pdev, PCI_GLI_9755_WT, &wt_value);
  427. wt_enable = FIELD_GET(PCI_GLI_9755_WT_EN, wt_value);
  428. if (wt_enable == GLI_9755_WT_EN_ON)
  429. return;
  430. wt_value &= ~PCI_GLI_9755_WT_EN;
  431. wt_value |= FIELD_PREP(PCI_GLI_9755_WT_EN, GLI_9755_WT_EN_ON);
  432. pci_write_config_dword(pdev, PCI_GLI_9755_WT, wt_value);
  433. }
  434. static inline void gl9755_wt_off(struct pci_dev *pdev)
  435. {
  436. u32 wt_value;
  437. u32 wt_enable;
  438. pci_read_config_dword(pdev, PCI_GLI_9755_WT, &wt_value);
  439. wt_enable = FIELD_GET(PCI_GLI_9755_WT_EN, wt_value);
  440. if (wt_enable == GLI_9755_WT_EN_OFF)
  441. return;
  442. wt_value &= ~PCI_GLI_9755_WT_EN;
  443. wt_value |= FIELD_PREP(PCI_GLI_9755_WT_EN, GLI_9755_WT_EN_OFF);
  444. pci_write_config_dword(pdev, PCI_GLI_9755_WT, wt_value);
  445. }
  446. static void gl9755_disable_ssc_pll(struct pci_dev *pdev)
  447. {
  448. u32 pll;
  449. gl9755_wt_on(pdev);
  450. pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
  451. pll &= ~(PCI_GLI_9755_PLL_DIR | PCI_GLI_9755_PLLSSC_EN);
  452. pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
  453. gl9755_wt_off(pdev);
  454. }
  455. static void gl9755_set_pll(struct pci_dev *pdev, u8 dir, u16 ldiv, u8 pdiv)
  456. {
  457. u32 pll;
  458. gl9755_wt_on(pdev);
  459. pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
  460. pll &= ~(PCI_GLI_9755_PLL_LDIV |
  461. PCI_GLI_9755_PLL_PDIV |
  462. PCI_GLI_9755_PLL_DIR);
  463. pll |= FIELD_PREP(PCI_GLI_9755_PLL_LDIV, ldiv) |
  464. FIELD_PREP(PCI_GLI_9755_PLL_PDIV, pdiv) |
  465. FIELD_PREP(PCI_GLI_9755_PLL_DIR, dir);
  466. pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
  467. gl9755_wt_off(pdev);
  468. /* wait for pll stable */
  469. mdelay(1);
  470. }
  471. static bool gl9755_ssc_enable(struct pci_dev *pdev)
  472. {
  473. u32 misc;
  474. u8 off;
  475. gl9755_wt_on(pdev);
  476. pci_read_config_dword(pdev, PCI_GLI_9755_MISC, &misc);
  477. off = FIELD_GET(PCI_GLI_9755_MISC_SSC_OFF, misc);
  478. gl9755_wt_off(pdev);
  479. return !off;
  480. }
  481. static void gl9755_set_ssc(struct pci_dev *pdev, u8 enable, u8 step, u16 ppm)
  482. {
  483. u32 pll;
  484. u32 ssc;
  485. gl9755_wt_on(pdev);
  486. pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
  487. pci_read_config_dword(pdev, PCI_GLI_9755_PLLSSC, &ssc);
  488. pll &= ~(PCI_GLI_9755_PLLSSC_STEP |
  489. PCI_GLI_9755_PLLSSC_EN);
  490. ssc &= ~PCI_GLI_9755_PLLSSC_PPM;
  491. pll |= FIELD_PREP(PCI_GLI_9755_PLLSSC_STEP, step) |
  492. FIELD_PREP(PCI_GLI_9755_PLLSSC_EN, enable);
  493. ssc |= FIELD_PREP(PCI_GLI_9755_PLLSSC_PPM, ppm);
  494. pci_write_config_dword(pdev, PCI_GLI_9755_PLLSSC, ssc);
  495. pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
  496. gl9755_wt_off(pdev);
  497. }
  498. static void gl9755_set_ssc_pll_205mhz(struct pci_dev *pdev)
  499. {
  500. bool enable = gl9755_ssc_enable(pdev);
  501. /* set pll to 205MHz and ssc */
  502. gl9755_set_ssc(pdev, enable, 0xF, 0x5A1D);
  503. gl9755_set_pll(pdev, 0x1, 0x246, 0x0);
  504. }
  505. static void gl9755_set_ssc_pll_100mhz(struct pci_dev *pdev)
  506. {
  507. bool enable = gl9755_ssc_enable(pdev);
  508. /* set pll to 100MHz and ssc */
  509. gl9755_set_ssc(pdev, enable, 0xE, 0x51EC);
  510. gl9755_set_pll(pdev, 0x1, 0x244, 0x1);
  511. }
  512. static void gl9755_set_ssc_pll_50mhz(struct pci_dev *pdev)
  513. {
  514. bool enable = gl9755_ssc_enable(pdev);
  515. /* set pll to 50MHz and ssc */
  516. gl9755_set_ssc(pdev, enable, 0xE, 0x51EC);
  517. gl9755_set_pll(pdev, 0x1, 0x244, 0x3);
  518. }
  519. static void sdhci_gl9755_set_clock(struct sdhci_host *host, unsigned int clock)
  520. {
  521. struct sdhci_pci_slot *slot = sdhci_priv(host);
  522. struct mmc_ios *ios = &host->mmc->ios;
  523. struct pci_dev *pdev;
  524. u16 clk;
  525. pdev = slot->chip->pdev;
  526. host->mmc->actual_clock = 0;
  527. gl9755_disable_ssc_pll(pdev);
  528. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  529. if (clock == 0)
  530. return;
  531. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  532. if (clock == 200000000 && ios->timing == MMC_TIMING_UHS_SDR104) {
  533. host->mmc->actual_clock = 205000000;
  534. gl9755_set_ssc_pll_205mhz(pdev);
  535. } else if (clock == 100000000) {
  536. gl9755_set_ssc_pll_100mhz(pdev);
  537. } else if (clock == 50000000) {
  538. gl9755_set_ssc_pll_50mhz(pdev);
  539. }
  540. sdhci_enable_clk(host, clk);
  541. }
  542. static void gl9755_hw_setting(struct sdhci_pci_slot *slot)
  543. {
  544. struct pci_dev *pdev = slot->chip->pdev;
  545. u32 value;
  546. gl9755_wt_on(pdev);
  547. pci_read_config_dword(pdev, PCI_GLI_9755_PECONF, &value);
  548. /*
  549. * Apple ARM64 platforms using these chips may have
  550. * inverted CD/WP detection.
  551. */
  552. if (of_property_read_bool(pdev->dev.of_node, "cd-inverted"))
  553. value |= PCI_GLI_9755_INVERT_CD;
  554. if (of_property_read_bool(pdev->dev.of_node, "wp-inverted"))
  555. value |= PCI_GLI_9755_INVERT_WP;
  556. value &= ~PCI_GLI_9755_LFCLK;
  557. value &= ~PCI_GLI_9755_DMACLK;
  558. pci_write_config_dword(pdev, PCI_GLI_9755_PECONF, value);
  559. /* enable short circuit protection */
  560. pci_read_config_dword(pdev, PCI_GLI_9755_SerDes, &value);
  561. value &= ~PCI_GLI_9755_SCP_DIS;
  562. pci_write_config_dword(pdev, PCI_GLI_9755_SerDes, value);
  563. pci_read_config_dword(pdev, PCI_GLI_9755_CFG2, &value);
  564. value &= ~PCI_GLI_9755_CFG2_L1DLY;
  565. /* set ASPM L1 entry delay to 7.9us */
  566. value |= FIELD_PREP(PCI_GLI_9755_CFG2_L1DLY,
  567. GLI_9755_CFG2_L1DLY_VALUE);
  568. pci_write_config_dword(pdev, PCI_GLI_9755_CFG2, value);
  569. /* toggle PM state to allow GL9755 to enter ASPM L1.2 */
  570. pci_read_config_dword(pdev, PCI_GLI_9755_PM_CTRL, &value);
  571. value |= PCI_GLI_9755_PM_STATE;
  572. pci_write_config_dword(pdev, PCI_GLI_9755_PM_CTRL, value);
  573. value &= ~PCI_GLI_9755_PM_STATE;
  574. pci_write_config_dword(pdev, PCI_GLI_9755_PM_CTRL, value);
  575. /* mask the replay timer timeout of AER */
  576. pci_read_config_dword(pdev, PCI_GLI_9755_CORRERR_MASK, &value);
  577. value |= PCI_GLI_9755_CORRERR_MASK_REPLAY_TIMER_TIMEOUT;
  578. pci_write_config_dword(pdev, PCI_GLI_9755_CORRERR_MASK, value);
  579. gl9755_wt_off(pdev);
  580. }
  581. static int gli_probe_slot_gl9750(struct sdhci_pci_slot *slot)
  582. {
  583. struct sdhci_host *host = slot->host;
  584. gl9750_hw_setting(host);
  585. gli_pcie_enable_msi(slot);
  586. slot->host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
  587. sdhci_enable_v4_mode(host);
  588. return 0;
  589. }
  590. static int gli_probe_slot_gl9755(struct sdhci_pci_slot *slot)
  591. {
  592. struct sdhci_host *host = slot->host;
  593. gl9755_hw_setting(slot);
  594. gli_pcie_enable_msi(slot);
  595. slot->host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
  596. sdhci_enable_v4_mode(host);
  597. return 0;
  598. }
  599. static void sdhci_gli_voltage_switch(struct sdhci_host *host)
  600. {
  601. /*
  602. * According to Section 3.6.1 signal voltage switch procedure in
  603. * SD Host Controller Simplified Spec. 4.20, steps 6~8 are as
  604. * follows:
  605. * (6) Set 1.8V Signal Enable in the Host Control 2 register.
  606. * (7) Wait 5ms. 1.8V voltage regulator shall be stable within this
  607. * period.
  608. * (8) If 1.8V Signal Enable is cleared by Host Controller, go to
  609. * step (12).
  610. *
  611. * Wait 5ms after set 1.8V signal enable in Host Control 2 register
  612. * to ensure 1.8V signal enable bit is set by GL9750/GL9755.
  613. *
  614. * ...however, the controller in the NUC10i3FNK4 (a 9755) requires
  615. * slightly longer than 5ms before the control register reports that
  616. * 1.8V is ready, and far longer still before the card will actually
  617. * work reliably.
  618. */
  619. usleep_range(100000, 110000);
  620. }
  621. static void sdhci_gl9750_reset(struct sdhci_host *host, u8 mask)
  622. {
  623. sdhci_reset(host, mask);
  624. gli_set_9750(host);
  625. }
  626. static u32 sdhci_gl9750_readl(struct sdhci_host *host, int reg)
  627. {
  628. u32 value;
  629. value = readl(host->ioaddr + reg);
  630. if (unlikely(reg == SDHCI_MAX_CURRENT && !(value & 0xff)))
  631. value |= 0xc8;
  632. return value;
  633. }
  634. static void gl9763e_hs400_enhanced_strobe(struct mmc_host *mmc,
  635. struct mmc_ios *ios)
  636. {
  637. struct sdhci_host *host = mmc_priv(mmc);
  638. u32 val;
  639. val = sdhci_readl(host, SDHCI_GLI_9763E_HS400_ES_REG);
  640. if (ios->enhanced_strobe)
  641. val |= SDHCI_GLI_9763E_HS400_ES_BIT;
  642. else
  643. val &= ~SDHCI_GLI_9763E_HS400_ES_BIT;
  644. sdhci_writel(host, val, SDHCI_GLI_9763E_HS400_ES_REG);
  645. }
  646. static void gl9763e_set_low_power_negotiation(struct sdhci_pci_slot *slot,
  647. bool enable)
  648. {
  649. struct pci_dev *pdev = slot->chip->pdev;
  650. u32 value;
  651. pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
  652. value &= ~GLI_9763E_VHS_REV;
  653. value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_W);
  654. pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
  655. pci_read_config_dword(pdev, PCIE_GLI_9763E_CFG, &value);
  656. if (enable)
  657. value &= ~GLI_9763E_CFG_LPSN_DIS;
  658. else
  659. value |= GLI_9763E_CFG_LPSN_DIS;
  660. pci_write_config_dword(pdev, PCIE_GLI_9763E_CFG, value);
  661. pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
  662. value &= ~GLI_9763E_VHS_REV;
  663. value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_R);
  664. pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
  665. }
  666. static void sdhci_set_gl9763e_signaling(struct sdhci_host *host,
  667. unsigned int timing)
  668. {
  669. u16 ctrl_2;
  670. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  671. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  672. if (timing == MMC_TIMING_MMC_HS200)
  673. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  674. else if (timing == MMC_TIMING_MMC_HS)
  675. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  676. else if (timing == MMC_TIMING_MMC_DDR52)
  677. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  678. else if (timing == MMC_TIMING_MMC_HS400)
  679. ctrl_2 |= SDHCI_GLI_9763E_CTRL_HS400;
  680. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  681. }
  682. static void sdhci_gl9763e_dumpregs(struct mmc_host *mmc)
  683. {
  684. sdhci_dumpregs(mmc_priv(mmc));
  685. }
  686. static void sdhci_gl9763e_cqe_pre_enable(struct mmc_host *mmc)
  687. {
  688. struct cqhci_host *cq_host = mmc->cqe_private;
  689. u32 value;
  690. value = cqhci_readl(cq_host, CQHCI_CFG);
  691. value |= CQHCI_ENABLE;
  692. cqhci_writel(cq_host, value, CQHCI_CFG);
  693. }
  694. static void sdhci_gl9763e_cqe_enable(struct mmc_host *mmc)
  695. {
  696. struct sdhci_host *host = mmc_priv(mmc);
  697. sdhci_writew(host, GLI_9763E_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE);
  698. sdhci_cqe_enable(mmc);
  699. }
  700. static u32 sdhci_gl9763e_cqhci_irq(struct sdhci_host *host, u32 intmask)
  701. {
  702. int cmd_error = 0;
  703. int data_error = 0;
  704. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  705. return intmask;
  706. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  707. return 0;
  708. }
  709. static void sdhci_gl9763e_cqe_post_disable(struct mmc_host *mmc)
  710. {
  711. struct sdhci_host *host = mmc_priv(mmc);
  712. struct cqhci_host *cq_host = mmc->cqe_private;
  713. u32 value;
  714. value = cqhci_readl(cq_host, CQHCI_CFG);
  715. value &= ~CQHCI_ENABLE;
  716. cqhci_writel(cq_host, value, CQHCI_CFG);
  717. sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
  718. }
  719. static const struct cqhci_host_ops sdhci_gl9763e_cqhci_ops = {
  720. .enable = sdhci_gl9763e_cqe_enable,
  721. .disable = sdhci_cqe_disable,
  722. .dumpregs = sdhci_gl9763e_dumpregs,
  723. .pre_enable = sdhci_gl9763e_cqe_pre_enable,
  724. .post_disable = sdhci_gl9763e_cqe_post_disable,
  725. };
  726. static int gl9763e_add_host(struct sdhci_pci_slot *slot)
  727. {
  728. struct device *dev = &slot->chip->pdev->dev;
  729. struct sdhci_host *host = slot->host;
  730. struct cqhci_host *cq_host;
  731. bool dma64;
  732. int ret;
  733. ret = sdhci_setup_host(host);
  734. if (ret)
  735. return ret;
  736. cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
  737. if (!cq_host) {
  738. ret = -ENOMEM;
  739. goto cleanup;
  740. }
  741. cq_host->mmio = host->ioaddr + SDHCI_GLI_9763E_CQE_BASE_ADDR;
  742. cq_host->ops = &sdhci_gl9763e_cqhci_ops;
  743. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  744. if (dma64)
  745. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  746. ret = cqhci_init(cq_host, host->mmc, dma64);
  747. if (ret)
  748. goto cleanup;
  749. ret = __sdhci_add_host(host);
  750. if (ret)
  751. goto cleanup;
  752. /* Disable LPM negotiation to avoid entering L1 state. */
  753. gl9763e_set_low_power_negotiation(slot, false);
  754. return 0;
  755. cleanup:
  756. sdhci_cleanup_host(host);
  757. return ret;
  758. }
  759. static void sdhci_gl9763e_reset(struct sdhci_host *host, u8 mask)
  760. {
  761. if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL) &&
  762. host->mmc->cqe_private)
  763. cqhci_deactivate(host->mmc);
  764. sdhci_reset(host, mask);
  765. }
  766. static void gli_set_gl9763e(struct sdhci_pci_slot *slot)
  767. {
  768. struct pci_dev *pdev = slot->chip->pdev;
  769. u32 value;
  770. pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
  771. value &= ~GLI_9763E_VHS_REV;
  772. value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_W);
  773. pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
  774. pci_read_config_dword(pdev, PCIE_GLI_9763E_SCR, &value);
  775. value |= GLI_9763E_SCR_AXI_REQ;
  776. pci_write_config_dword(pdev, PCIE_GLI_9763E_SCR, value);
  777. pci_read_config_dword(pdev, PCIE_GLI_9763E_MMC_CTRL, &value);
  778. value &= ~GLI_9763E_HS400_SLOW;
  779. pci_write_config_dword(pdev, PCIE_GLI_9763E_MMC_CTRL, value);
  780. pci_read_config_dword(pdev, PCIE_GLI_9763E_CFG2, &value);
  781. value &= ~GLI_9763E_CFG2_L1DLY;
  782. /* set ASPM L1 entry delay to 21us */
  783. value |= FIELD_PREP(GLI_9763E_CFG2_L1DLY, GLI_9763E_CFG2_L1DLY_MID);
  784. pci_write_config_dword(pdev, PCIE_GLI_9763E_CFG2, value);
  785. pci_read_config_dword(pdev, PCIE_GLI_9763E_CLKRXDLY, &value);
  786. value &= ~GLI_9763E_HS400_RXDLY;
  787. value |= FIELD_PREP(GLI_9763E_HS400_RXDLY, GLI_9763E_HS400_RXDLY_5);
  788. pci_write_config_dword(pdev, PCIE_GLI_9763E_CLKRXDLY, value);
  789. pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
  790. value &= ~GLI_9763E_VHS_REV;
  791. value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_R);
  792. pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
  793. }
  794. #ifdef CONFIG_PM
  795. static int gl9763e_runtime_suspend(struct sdhci_pci_chip *chip)
  796. {
  797. struct sdhci_pci_slot *slot = chip->slots[0];
  798. struct sdhci_host *host = slot->host;
  799. u16 clock;
  800. /* Enable LPM negotiation to allow entering L1 state */
  801. gl9763e_set_low_power_negotiation(slot, true);
  802. clock = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  803. clock &= ~(SDHCI_CLOCK_PLL_EN | SDHCI_CLOCK_CARD_EN);
  804. sdhci_writew(host, clock, SDHCI_CLOCK_CONTROL);
  805. return 0;
  806. }
  807. static int gl9763e_runtime_resume(struct sdhci_pci_chip *chip)
  808. {
  809. struct sdhci_pci_slot *slot = chip->slots[0];
  810. struct sdhci_host *host = slot->host;
  811. u16 clock;
  812. if (host->mmc->ios.power_mode != MMC_POWER_ON)
  813. return 0;
  814. clock = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  815. clock |= SDHCI_CLOCK_PLL_EN;
  816. clock &= ~SDHCI_CLOCK_INT_STABLE;
  817. sdhci_writew(host, clock, SDHCI_CLOCK_CONTROL);
  818. /* Wait max 150 ms */
  819. if (read_poll_timeout(sdhci_readw, clock, (clock & SDHCI_CLOCK_INT_STABLE),
  820. 1000, 150000, false, host, SDHCI_CLOCK_CONTROL)) {
  821. pr_err("%s: PLL clock never stabilised.\n",
  822. mmc_hostname(host->mmc));
  823. sdhci_dumpregs(host);
  824. }
  825. clock |= SDHCI_CLOCK_CARD_EN;
  826. sdhci_writew(host, clock, SDHCI_CLOCK_CONTROL);
  827. /* Disable LPM negotiation to avoid entering L1 state. */
  828. gl9763e_set_low_power_negotiation(slot, false);
  829. return 0;
  830. }
  831. #endif
  832. #ifdef CONFIG_PM_SLEEP
  833. static int sdhci_pci_gli_resume(struct sdhci_pci_chip *chip)
  834. {
  835. struct sdhci_pci_slot *slot = chip->slots[0];
  836. pci_free_irq_vectors(slot->chip->pdev);
  837. gli_pcie_enable_msi(slot);
  838. return sdhci_pci_resume_host(chip);
  839. }
  840. static int gl9763e_resume(struct sdhci_pci_chip *chip)
  841. {
  842. struct sdhci_pci_slot *slot = chip->slots[0];
  843. int ret;
  844. ret = sdhci_pci_gli_resume(chip);
  845. if (ret)
  846. return ret;
  847. ret = cqhci_resume(slot->host->mmc);
  848. if (ret)
  849. return ret;
  850. /*
  851. * Disable LPM negotiation to bring device back in sync
  852. * with its runtime_pm state.
  853. */
  854. gl9763e_set_low_power_negotiation(slot, false);
  855. return 0;
  856. }
  857. static int gl9763e_suspend(struct sdhci_pci_chip *chip)
  858. {
  859. struct sdhci_pci_slot *slot = chip->slots[0];
  860. int ret;
  861. /*
  862. * Certain SoCs can suspend only with the bus in low-
  863. * power state, notably x86 SoCs when using S0ix.
  864. * Re-enable LPM negotiation to allow entering L1 state
  865. * and entering system suspend.
  866. */
  867. gl9763e_set_low_power_negotiation(slot, true);
  868. ret = cqhci_suspend(slot->host->mmc);
  869. if (ret)
  870. goto err_suspend;
  871. ret = sdhci_suspend_host(slot->host);
  872. if (ret)
  873. goto err_suspend_host;
  874. return 0;
  875. err_suspend_host:
  876. cqhci_resume(slot->host->mmc);
  877. err_suspend:
  878. gl9763e_set_low_power_negotiation(slot, false);
  879. return ret;
  880. }
  881. #endif
  882. static int gli_probe_slot_gl9763e(struct sdhci_pci_slot *slot)
  883. {
  884. struct pci_dev *pdev = slot->chip->pdev;
  885. struct sdhci_host *host = slot->host;
  886. u32 value;
  887. host->mmc->caps |= MMC_CAP_8_BIT_DATA |
  888. MMC_CAP_1_8V_DDR |
  889. MMC_CAP_NONREMOVABLE;
  890. host->mmc->caps2 |= MMC_CAP2_HS200_1_8V_SDR |
  891. MMC_CAP2_HS400_1_8V |
  892. MMC_CAP2_HS400_ES |
  893. MMC_CAP2_NO_SDIO |
  894. MMC_CAP2_NO_SD;
  895. pci_read_config_dword(pdev, PCIE_GLI_9763E_MB, &value);
  896. if (!(value & GLI_9763E_MB_CMDQ_OFF))
  897. if (value & GLI_9763E_MB_ERP_ON)
  898. host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
  899. gli_pcie_enable_msi(slot);
  900. host->mmc_host_ops.hs400_enhanced_strobe =
  901. gl9763e_hs400_enhanced_strobe;
  902. gli_set_gl9763e(slot);
  903. sdhci_enable_v4_mode(host);
  904. return 0;
  905. }
  906. #define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
  907. static u16 sdhci_gli_readw(struct sdhci_host *host, int reg)
  908. {
  909. u32 val = readl(host->ioaddr + (reg & ~3));
  910. u16 word;
  911. word = (val >> REG_OFFSET_IN_BITS(reg)) & 0xffff;
  912. return word;
  913. }
  914. static u8 sdhci_gli_readb(struct sdhci_host *host, int reg)
  915. {
  916. u32 val = readl(host->ioaddr + (reg & ~3));
  917. u8 byte = (val >> REG_OFFSET_IN_BITS(reg)) & 0xff;
  918. return byte;
  919. }
  920. static const struct sdhci_ops sdhci_gl9755_ops = {
  921. .read_w = sdhci_gli_readw,
  922. .read_b = sdhci_gli_readb,
  923. .set_clock = sdhci_gl9755_set_clock,
  924. .enable_dma = sdhci_pci_enable_dma,
  925. .set_bus_width = sdhci_set_bus_width,
  926. .reset = sdhci_reset,
  927. .set_uhs_signaling = sdhci_set_uhs_signaling,
  928. .voltage_switch = sdhci_gli_voltage_switch,
  929. };
  930. const struct sdhci_pci_fixes sdhci_gl9755 = {
  931. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  932. .quirks2 = SDHCI_QUIRK2_BROKEN_DDR50,
  933. .probe_slot = gli_probe_slot_gl9755,
  934. .ops = &sdhci_gl9755_ops,
  935. #ifdef CONFIG_PM_SLEEP
  936. .resume = sdhci_pci_gli_resume,
  937. #endif
  938. };
  939. static const struct sdhci_ops sdhci_gl9750_ops = {
  940. .read_w = sdhci_gli_readw,
  941. .read_b = sdhci_gli_readb,
  942. .read_l = sdhci_gl9750_readl,
  943. .set_clock = sdhci_gl9750_set_clock,
  944. .enable_dma = sdhci_pci_enable_dma,
  945. .set_bus_width = sdhci_set_bus_width,
  946. .reset = sdhci_gl9750_reset,
  947. .set_uhs_signaling = sdhci_set_uhs_signaling,
  948. .voltage_switch = sdhci_gli_voltage_switch,
  949. .platform_execute_tuning = gl9750_execute_tuning,
  950. };
  951. const struct sdhci_pci_fixes sdhci_gl9750 = {
  952. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  953. .quirks2 = SDHCI_QUIRK2_BROKEN_DDR50,
  954. .probe_slot = gli_probe_slot_gl9750,
  955. .ops = &sdhci_gl9750_ops,
  956. #ifdef CONFIG_PM_SLEEP
  957. .resume = sdhci_pci_gli_resume,
  958. #endif
  959. };
  960. static const struct sdhci_ops sdhci_gl9763e_ops = {
  961. .set_clock = sdhci_set_clock,
  962. .enable_dma = sdhci_pci_enable_dma,
  963. .set_bus_width = sdhci_set_bus_width,
  964. .reset = sdhci_gl9763e_reset,
  965. .set_uhs_signaling = sdhci_set_gl9763e_signaling,
  966. .voltage_switch = sdhci_gli_voltage_switch,
  967. .irq = sdhci_gl9763e_cqhci_irq,
  968. };
  969. const struct sdhci_pci_fixes sdhci_gl9763e = {
  970. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  971. .probe_slot = gli_probe_slot_gl9763e,
  972. .ops = &sdhci_gl9763e_ops,
  973. #ifdef CONFIG_PM_SLEEP
  974. .resume = gl9763e_resume,
  975. .suspend = gl9763e_suspend,
  976. #endif
  977. #ifdef CONFIG_PM
  978. .runtime_suspend = gl9763e_runtime_suspend,
  979. .runtime_resume = gl9763e_runtime_resume,
  980. .allow_runtime_pm = true,
  981. #endif
  982. .add_host = gl9763e_add_host,
  983. };