sdhci-sprd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Secure Digital Host Controller
  4. //
  5. // Copyright (C) 2018 Spreadtrum, Inc.
  6. // Author: Chunyan Zhang <chunyan.zhang@unisoc.com>
  7. #include <linux/delay.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/highmem.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/mmc/mmc.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include "sdhci-pltfm.h"
  21. #include "mmc_hsq.h"
  22. /* SDHCI_ARGUMENT2 register high 16bit */
  23. #define SDHCI_SPRD_ARG2_STUFF GENMASK(31, 16)
  24. #define SDHCI_SPRD_REG_32_DLL_CFG 0x200
  25. #define SDHCI_SPRD_DLL_ALL_CPST_EN (BIT(18) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
  26. #define SDHCI_SPRD_DLL_EN BIT(21)
  27. #define SDHCI_SPRD_DLL_SEARCH_MODE BIT(16)
  28. #define SDHCI_SPRD_DLL_INIT_COUNT 0xc00
  29. #define SDHCI_SPRD_DLL_PHASE_INTERNAL 0x3
  30. #define SDHCI_SPRD_REG_32_DLL_DLY 0x204
  31. #define SDHCI_SPRD_REG_32_DLL_DLY_OFFSET 0x208
  32. #define SDHCIBSPRD_IT_WR_DLY_INV BIT(5)
  33. #define SDHCI_SPRD_BIT_CMD_DLY_INV BIT(13)
  34. #define SDHCI_SPRD_BIT_POSRD_DLY_INV BIT(21)
  35. #define SDHCI_SPRD_BIT_NEGRD_DLY_INV BIT(29)
  36. #define SDHCI_SPRD_REG_32_DLL_STS0 0x210
  37. #define SDHCI_SPRD_DLL_LOCKED BIT(18)
  38. #define SDHCI_SPRD_REG_32_BUSY_POSI 0x250
  39. #define SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN BIT(25)
  40. #define SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN BIT(24)
  41. #define SDHCI_SPRD_REG_DEBOUNCE 0x28C
  42. #define SDHCI_SPRD_BIT_DLL_BAK BIT(0)
  43. #define SDHCI_SPRD_BIT_DLL_VAL BIT(1)
  44. #define SDHCI_SPRD_INT_SIGNAL_MASK 0x1B7F410B
  45. /* SDHCI_HOST_CONTROL2 */
  46. #define SDHCI_SPRD_CTRL_HS200 0x0005
  47. #define SDHCI_SPRD_CTRL_HS400 0x0006
  48. #define SDHCI_SPRD_CTRL_HS400ES 0x0007
  49. /*
  50. * According to the standard specification, BIT(3) of SDHCI_SOFTWARE_RESET is
  51. * reserved, and only used on Spreadtrum's design, the hardware cannot work
  52. * if this bit is cleared.
  53. * 1 : normal work
  54. * 0 : hardware reset
  55. */
  56. #define SDHCI_HW_RESET_CARD BIT(3)
  57. #define SDHCI_SPRD_MAX_CUR 0xFFFFFF
  58. #define SDHCI_SPRD_CLK_MAX_DIV 1023
  59. #define SDHCI_SPRD_CLK_DEF_RATE 26000000
  60. #define SDHCI_SPRD_PHY_DLL_CLK 52000000
  61. #define SDHCI_SPRD_MAX_RANGE 0xff
  62. #define SDHCI_SPRD_CMD_DLY_MASK GENMASK(15, 8)
  63. #define SDHCI_SPRD_POSRD_DLY_MASK GENMASK(23, 16)
  64. #define SDHCI_SPRD_CPST_EN GENMASK(27, 24)
  65. struct sdhci_sprd_host {
  66. u32 version;
  67. struct clk *clk_sdio;
  68. struct clk *clk_enable;
  69. struct clk *clk_2x_enable;
  70. struct pinctrl *pinctrl;
  71. struct pinctrl_state *pins_uhs;
  72. struct pinctrl_state *pins_default;
  73. u32 base_rate;
  74. int flags; /* backup of host attribute */
  75. u32 phy_delay[MMC_TIMING_MMC_HS400 + 2];
  76. };
  77. enum sdhci_sprd_tuning_type {
  78. SDHCI_SPRD_TUNING_SD_HS_CMD,
  79. SDHCI_SPRD_TUNING_SD_HS_DATA,
  80. };
  81. struct sdhci_sprd_phy_cfg {
  82. const char *property;
  83. u8 timing;
  84. };
  85. static const struct sdhci_sprd_phy_cfg sdhci_sprd_phy_cfgs[] = {
  86. { "sprd,phy-delay-legacy", MMC_TIMING_LEGACY, },
  87. { "sprd,phy-delay-sd-highspeed", MMC_TIMING_SD_HS, },
  88. { "sprd,phy-delay-sd-uhs-sdr50", MMC_TIMING_UHS_SDR50, },
  89. { "sprd,phy-delay-sd-uhs-sdr104", MMC_TIMING_UHS_SDR104, },
  90. { "sprd,phy-delay-mmc-highspeed", MMC_TIMING_MMC_HS, },
  91. { "sprd,phy-delay-mmc-ddr52", MMC_TIMING_MMC_DDR52, },
  92. { "sprd,phy-delay-mmc-hs200", MMC_TIMING_MMC_HS200, },
  93. { "sprd,phy-delay-mmc-hs400", MMC_TIMING_MMC_HS400, },
  94. { "sprd,phy-delay-mmc-hs400es", MMC_TIMING_MMC_HS400 + 1, },
  95. };
  96. #define TO_SPRD_HOST(host) sdhci_pltfm_priv(sdhci_priv(host))
  97. static void sdhci_sprd_init_config(struct sdhci_host *host)
  98. {
  99. u16 val;
  100. /* set dll backup mode */
  101. val = sdhci_readl(host, SDHCI_SPRD_REG_DEBOUNCE);
  102. val |= SDHCI_SPRD_BIT_DLL_BAK | SDHCI_SPRD_BIT_DLL_VAL;
  103. sdhci_writel(host, val, SDHCI_SPRD_REG_DEBOUNCE);
  104. }
  105. static inline u32 sdhci_sprd_readl(struct sdhci_host *host, int reg)
  106. {
  107. if (unlikely(reg == SDHCI_MAX_CURRENT))
  108. return SDHCI_SPRD_MAX_CUR;
  109. return readl_relaxed(host->ioaddr + reg);
  110. }
  111. static inline void sdhci_sprd_writel(struct sdhci_host *host, u32 val, int reg)
  112. {
  113. /* SDHCI_MAX_CURRENT is reserved on Spreadtrum's platform */
  114. if (unlikely(reg == SDHCI_MAX_CURRENT))
  115. return;
  116. if (unlikely(reg == SDHCI_SIGNAL_ENABLE || reg == SDHCI_INT_ENABLE))
  117. val = val & SDHCI_SPRD_INT_SIGNAL_MASK;
  118. writel_relaxed(val, host->ioaddr + reg);
  119. }
  120. static inline void sdhci_sprd_writew(struct sdhci_host *host, u16 val, int reg)
  121. {
  122. /* SDHCI_BLOCK_COUNT is Read Only on Spreadtrum's platform */
  123. if (unlikely(reg == SDHCI_BLOCK_COUNT))
  124. return;
  125. writew_relaxed(val, host->ioaddr + reg);
  126. }
  127. static inline void sdhci_sprd_writeb(struct sdhci_host *host, u8 val, int reg)
  128. {
  129. /*
  130. * Since BIT(3) of SDHCI_SOFTWARE_RESET is reserved according to the
  131. * standard specification, sdhci_reset() write this register directly
  132. * without checking other reserved bits, that will clear BIT(3) which
  133. * is defined as hardware reset on Spreadtrum's platform and clearing
  134. * it by mistake will lead the card not work. So here we need to work
  135. * around it.
  136. */
  137. if (unlikely(reg == SDHCI_SOFTWARE_RESET)) {
  138. if (readb_relaxed(host->ioaddr + reg) & SDHCI_HW_RESET_CARD)
  139. val |= SDHCI_HW_RESET_CARD;
  140. }
  141. writeb_relaxed(val, host->ioaddr + reg);
  142. }
  143. static inline void sdhci_sprd_sd_clk_off(struct sdhci_host *host)
  144. {
  145. u16 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  146. ctrl &= ~SDHCI_CLOCK_CARD_EN;
  147. sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
  148. }
  149. static inline void sdhci_sprd_sd_clk_on(struct sdhci_host *host)
  150. {
  151. u16 ctrl;
  152. ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  153. ctrl |= SDHCI_CLOCK_CARD_EN;
  154. sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
  155. }
  156. static inline void
  157. sdhci_sprd_set_dll_invert(struct sdhci_host *host, u32 mask, bool en)
  158. {
  159. u32 dll_dly_offset;
  160. dll_dly_offset = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
  161. if (en)
  162. dll_dly_offset |= mask;
  163. else
  164. dll_dly_offset &= ~mask;
  165. sdhci_writel(host, dll_dly_offset, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
  166. }
  167. static inline u32 sdhci_sprd_calc_div(u32 base_clk, u32 clk)
  168. {
  169. u32 div;
  170. /* select 2x clock source */
  171. if (base_clk <= clk * 2)
  172. return 0;
  173. div = (u32) (base_clk / (clk * 2));
  174. if ((base_clk / div) > (clk * 2))
  175. div++;
  176. if (div % 2)
  177. div = (div + 1) / 2;
  178. else
  179. div = div / 2;
  180. if (div > SDHCI_SPRD_CLK_MAX_DIV)
  181. div = SDHCI_SPRD_CLK_MAX_DIV;
  182. return div;
  183. }
  184. static inline void _sdhci_sprd_set_clock(struct sdhci_host *host,
  185. unsigned int clk)
  186. {
  187. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  188. u32 div, val, mask;
  189. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  190. div = sdhci_sprd_calc_div(sprd_host->base_rate, clk);
  191. div = ((div & 0x300) >> 2) | ((div & 0xFF) << 8);
  192. sdhci_enable_clk(host, div);
  193. val = sdhci_readl(host, SDHCI_SPRD_REG_32_BUSY_POSI);
  194. mask = SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN | SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN;
  195. /* Enable CLK_AUTO when the clock is greater than 400K. */
  196. if (clk > 400000) {
  197. if (mask != (val & mask)) {
  198. val |= mask;
  199. sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
  200. }
  201. } else {
  202. if (val & mask) {
  203. val &= ~mask;
  204. sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
  205. }
  206. }
  207. }
  208. static void sdhci_sprd_enable_phy_dll(struct sdhci_host *host)
  209. {
  210. u32 tmp;
  211. tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
  212. tmp &= ~(SDHCI_SPRD_DLL_EN | SDHCI_SPRD_DLL_ALL_CPST_EN);
  213. sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
  214. /* wait 1ms */
  215. usleep_range(1000, 1250);
  216. tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
  217. tmp |= SDHCI_SPRD_DLL_ALL_CPST_EN | SDHCI_SPRD_DLL_SEARCH_MODE |
  218. SDHCI_SPRD_DLL_INIT_COUNT | SDHCI_SPRD_DLL_PHASE_INTERNAL;
  219. sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
  220. /* wait 1ms */
  221. usleep_range(1000, 1250);
  222. tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
  223. tmp |= SDHCI_SPRD_DLL_EN;
  224. sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
  225. /* wait 1ms */
  226. usleep_range(1000, 1250);
  227. if (read_poll_timeout(sdhci_readl, tmp, (tmp & SDHCI_SPRD_DLL_LOCKED),
  228. 2000, USEC_PER_SEC, false, host, SDHCI_SPRD_REG_32_DLL_STS0)) {
  229. pr_err("%s: DLL locked fail!\n", mmc_hostname(host->mmc));
  230. pr_info("%s: DLL_STS0 : 0x%x, DLL_CFG : 0x%x\n",
  231. mmc_hostname(host->mmc),
  232. sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_STS0),
  233. sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG));
  234. }
  235. }
  236. static void sdhci_sprd_set_clock(struct sdhci_host *host, unsigned int clock)
  237. {
  238. bool en = false, clk_changed = false;
  239. if (clock == 0) {
  240. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  241. } else if (clock != host->clock) {
  242. sdhci_sprd_sd_clk_off(host);
  243. _sdhci_sprd_set_clock(host, clock);
  244. if (clock <= 400000)
  245. en = true;
  246. sdhci_sprd_set_dll_invert(host, SDHCI_SPRD_BIT_CMD_DLY_INV |
  247. SDHCI_SPRD_BIT_POSRD_DLY_INV, en);
  248. clk_changed = true;
  249. } else {
  250. _sdhci_sprd_set_clock(host, clock);
  251. }
  252. /*
  253. * According to the Spreadtrum SD host specification, when we changed
  254. * the clock to be more than 52M, we should enable the PHY DLL which
  255. * is used to track the clock frequency to make the clock work more
  256. * stable. Otherwise deviation may occur of the higher clock.
  257. */
  258. if (clk_changed && clock > SDHCI_SPRD_PHY_DLL_CLK)
  259. sdhci_sprd_enable_phy_dll(host);
  260. }
  261. static unsigned int sdhci_sprd_get_max_clock(struct sdhci_host *host)
  262. {
  263. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  264. return clk_round_rate(sprd_host->clk_sdio, ULONG_MAX);
  265. }
  266. static unsigned int sdhci_sprd_get_min_clock(struct sdhci_host *host)
  267. {
  268. return 100000;
  269. }
  270. static void sdhci_sprd_set_uhs_signaling(struct sdhci_host *host,
  271. unsigned int timing)
  272. {
  273. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  274. struct mmc_host *mmc = host->mmc;
  275. u32 *p = sprd_host->phy_delay;
  276. u16 ctrl_2;
  277. if (timing == host->timing)
  278. return;
  279. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  280. /* Select Bus Speed Mode for host */
  281. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  282. switch (timing) {
  283. case MMC_TIMING_UHS_SDR12:
  284. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  285. break;
  286. case MMC_TIMING_MMC_HS:
  287. case MMC_TIMING_SD_HS:
  288. case MMC_TIMING_UHS_SDR25:
  289. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  290. break;
  291. case MMC_TIMING_UHS_SDR50:
  292. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  293. break;
  294. case MMC_TIMING_UHS_SDR104:
  295. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  296. break;
  297. case MMC_TIMING_UHS_DDR50:
  298. case MMC_TIMING_MMC_DDR52:
  299. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  300. break;
  301. case MMC_TIMING_MMC_HS200:
  302. ctrl_2 |= SDHCI_SPRD_CTRL_HS200;
  303. break;
  304. case MMC_TIMING_MMC_HS400:
  305. ctrl_2 |= SDHCI_SPRD_CTRL_HS400;
  306. break;
  307. default:
  308. break;
  309. }
  310. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  311. if (!mmc->ios.enhanced_strobe)
  312. sdhci_writel(host, p[timing], SDHCI_SPRD_REG_32_DLL_DLY);
  313. }
  314. static void sdhci_sprd_hw_reset(struct sdhci_host *host)
  315. {
  316. int val;
  317. /*
  318. * Note: don't use sdhci_writeb() API here since it is redirected to
  319. * sdhci_sprd_writeb() in which we have a workaround for
  320. * SDHCI_SOFTWARE_RESET which would make bit SDHCI_HW_RESET_CARD can
  321. * not be cleared.
  322. */
  323. val = readb_relaxed(host->ioaddr + SDHCI_SOFTWARE_RESET);
  324. val &= ~SDHCI_HW_RESET_CARD;
  325. writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
  326. /* wait for 10 us */
  327. usleep_range(10, 20);
  328. val |= SDHCI_HW_RESET_CARD;
  329. writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
  330. usleep_range(300, 500);
  331. }
  332. static unsigned int sdhci_sprd_get_max_timeout_count(struct sdhci_host *host)
  333. {
  334. /* The Spredtrum controller actual maximum timeout count is 1 << 31 */
  335. return 1 << 31;
  336. }
  337. static unsigned int sdhci_sprd_get_ro(struct sdhci_host *host)
  338. {
  339. return 0;
  340. }
  341. static void sdhci_sprd_request_done(struct sdhci_host *host,
  342. struct mmc_request *mrq)
  343. {
  344. /* Validate if the request was from software queue firstly. */
  345. if (mmc_hsq_finalize_request(host->mmc, mrq))
  346. return;
  347. mmc_request_done(host->mmc, mrq);
  348. }
  349. static void sdhci_sprd_set_power(struct sdhci_host *host, unsigned char mode,
  350. unsigned short vdd)
  351. {
  352. struct mmc_host *mmc = host->mmc;
  353. switch (mode) {
  354. case MMC_POWER_OFF:
  355. mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, 0);
  356. mmc_regulator_disable_vqmmc(mmc);
  357. break;
  358. case MMC_POWER_ON:
  359. mmc_regulator_enable_vqmmc(mmc);
  360. break;
  361. case MMC_POWER_UP:
  362. mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, vdd);
  363. break;
  364. }
  365. }
  366. static const struct sdhci_ops sdhci_sprd_ops = {
  367. .read_l = sdhci_sprd_readl,
  368. .write_l = sdhci_sprd_writel,
  369. .write_w = sdhci_sprd_writew,
  370. .write_b = sdhci_sprd_writeb,
  371. .set_clock = sdhci_sprd_set_clock,
  372. .set_power = sdhci_sprd_set_power,
  373. .get_max_clock = sdhci_sprd_get_max_clock,
  374. .get_min_clock = sdhci_sprd_get_min_clock,
  375. .set_bus_width = sdhci_set_bus_width,
  376. .reset = sdhci_reset,
  377. .set_uhs_signaling = sdhci_sprd_set_uhs_signaling,
  378. .hw_reset = sdhci_sprd_hw_reset,
  379. .get_max_timeout_count = sdhci_sprd_get_max_timeout_count,
  380. .get_ro = sdhci_sprd_get_ro,
  381. .request_done = sdhci_sprd_request_done,
  382. };
  383. static void sdhci_sprd_check_auto_cmd23(struct mmc_host *mmc,
  384. struct mmc_request *mrq)
  385. {
  386. struct sdhci_host *host = mmc_priv(mmc);
  387. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  388. host->flags |= sprd_host->flags & SDHCI_AUTO_CMD23;
  389. /*
  390. * From version 4.10 onward, ARGUMENT2 register is also as 32-bit
  391. * block count register which doesn't support stuff bits of
  392. * CMD23 argument on Spreadtrum's sd host controller.
  393. */
  394. if (host->version >= SDHCI_SPEC_410 &&
  395. mrq->sbc && (mrq->sbc->arg & SDHCI_SPRD_ARG2_STUFF) &&
  396. (host->flags & SDHCI_AUTO_CMD23))
  397. host->flags &= ~SDHCI_AUTO_CMD23;
  398. }
  399. static void sdhci_sprd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  400. {
  401. sdhci_sprd_check_auto_cmd23(mmc, mrq);
  402. sdhci_request(mmc, mrq);
  403. }
  404. static int sdhci_sprd_request_atomic(struct mmc_host *mmc,
  405. struct mmc_request *mrq)
  406. {
  407. sdhci_sprd_check_auto_cmd23(mmc, mrq);
  408. return sdhci_request_atomic(mmc, mrq);
  409. }
  410. static int sdhci_sprd_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  411. {
  412. struct sdhci_host *host = mmc_priv(mmc);
  413. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  414. int ret;
  415. if (!IS_ERR(mmc->supply.vqmmc)) {
  416. ret = mmc_regulator_set_vqmmc(mmc, ios);
  417. if (ret < 0) {
  418. pr_err("%s: Switching signalling voltage failed\n",
  419. mmc_hostname(mmc));
  420. return ret;
  421. }
  422. }
  423. if (IS_ERR(sprd_host->pinctrl))
  424. goto reset;
  425. switch (ios->signal_voltage) {
  426. case MMC_SIGNAL_VOLTAGE_180:
  427. ret = pinctrl_select_state(sprd_host->pinctrl,
  428. sprd_host->pins_uhs);
  429. if (ret) {
  430. pr_err("%s: failed to select uhs pin state\n",
  431. mmc_hostname(mmc));
  432. return ret;
  433. }
  434. break;
  435. default:
  436. fallthrough;
  437. case MMC_SIGNAL_VOLTAGE_330:
  438. ret = pinctrl_select_state(sprd_host->pinctrl,
  439. sprd_host->pins_default);
  440. if (ret) {
  441. pr_err("%s: failed to select default pin state\n",
  442. mmc_hostname(mmc));
  443. return ret;
  444. }
  445. break;
  446. }
  447. /* Wait for 300 ~ 500 us for pin state stable */
  448. usleep_range(300, 500);
  449. reset:
  450. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  451. return 0;
  452. }
  453. static void sdhci_sprd_hs400_enhanced_strobe(struct mmc_host *mmc,
  454. struct mmc_ios *ios)
  455. {
  456. struct sdhci_host *host = mmc_priv(mmc);
  457. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  458. u32 *p = sprd_host->phy_delay;
  459. u16 ctrl_2;
  460. if (!ios->enhanced_strobe)
  461. return;
  462. sdhci_sprd_sd_clk_off(host);
  463. /* Set HS400 enhanced strobe mode */
  464. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  465. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  466. ctrl_2 |= SDHCI_SPRD_CTRL_HS400ES;
  467. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  468. sdhci_sprd_sd_clk_on(host);
  469. /* Set the PHY DLL delay value for HS400 enhanced strobe mode */
  470. sdhci_writel(host, p[MMC_TIMING_MMC_HS400 + 1],
  471. SDHCI_SPRD_REG_32_DLL_DLY);
  472. }
  473. static int mmc_send_tuning_cmd(struct mmc_card *card)
  474. {
  475. return mmc_send_status(card, NULL);
  476. }
  477. static int mmc_send_tuning_data(struct mmc_card *card)
  478. {
  479. u8 *status;
  480. int ret;
  481. status = kmalloc(64, GFP_KERNEL);
  482. if (!status)
  483. return -ENOMEM;
  484. ret = mmc_sd_switch(card, 0, 0, 0, status);
  485. kfree(status);
  486. return ret;
  487. }
  488. static int sdhci_sprd_get_best_clk_sample(struct mmc_host *mmc, u8 *value)
  489. {
  490. int range_end = SDHCI_SPRD_MAX_RANGE;
  491. int range_length = 0;
  492. int middle_range = 0;
  493. int count = 0;
  494. int i;
  495. for (i = 0; i <= SDHCI_SPRD_MAX_RANGE; i++) {
  496. if (value[i]) {
  497. pr_debug("%s: tuning ok: %d\n", mmc_hostname(mmc), i);
  498. count++;
  499. } else {
  500. pr_debug("%s: tuning fail: %d\n", mmc_hostname(mmc), i);
  501. if (range_length < count) {
  502. range_length = count;
  503. range_end = i - 1;
  504. count = 0;
  505. }
  506. }
  507. }
  508. if (!count)
  509. return -EIO;
  510. if (count > range_length) {
  511. range_length = count;
  512. range_end = i - 1;
  513. }
  514. middle_range = range_end - (range_length - 1) / 2;
  515. return middle_range;
  516. }
  517. static int sdhci_sprd_tuning(struct mmc_host *mmc, struct mmc_card *card,
  518. enum sdhci_sprd_tuning_type type)
  519. {
  520. struct sdhci_host *host = mmc_priv(mmc);
  521. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  522. u32 *p = sprd_host->phy_delay;
  523. u32 dll_cfg, dll_dly;
  524. int best_clk_sample;
  525. int err = 0;
  526. u8 *value;
  527. int i;
  528. value = kmalloc(SDHCI_SPRD_MAX_RANGE + 1, GFP_KERNEL);
  529. if (!value)
  530. return -ENOMEM;
  531. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  532. dll_cfg = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
  533. dll_cfg &= ~SDHCI_SPRD_CPST_EN;
  534. sdhci_writel(host, dll_cfg, SDHCI_SPRD_REG_32_DLL_CFG);
  535. dll_dly = p[mmc->ios.timing];
  536. for (i = 0; i <= SDHCI_SPRD_MAX_RANGE; i++) {
  537. if (type == SDHCI_SPRD_TUNING_SD_HS_CMD) {
  538. dll_dly &= ~SDHCI_SPRD_CMD_DLY_MASK;
  539. dll_dly |= ((i << 8) & SDHCI_SPRD_CMD_DLY_MASK);
  540. } else {
  541. dll_dly &= ~SDHCI_SPRD_POSRD_DLY_MASK;
  542. dll_dly |= ((i << 16) & SDHCI_SPRD_POSRD_DLY_MASK);
  543. }
  544. sdhci_writel(host, dll_dly, SDHCI_SPRD_REG_32_DLL_DLY);
  545. if (type == SDHCI_SPRD_TUNING_SD_HS_CMD)
  546. value[i] = !mmc_send_tuning_cmd(card);
  547. else
  548. value[i] = !mmc_send_tuning_data(card);
  549. }
  550. best_clk_sample = sdhci_sprd_get_best_clk_sample(mmc, value);
  551. if (best_clk_sample < 0) {
  552. dev_err(mmc_dev(host->mmc), "all tuning phase fail!\n");
  553. err = best_clk_sample;
  554. goto out;
  555. }
  556. if (type == SDHCI_SPRD_TUNING_SD_HS_CMD) {
  557. p[mmc->ios.timing] &= ~SDHCI_SPRD_CMD_DLY_MASK;
  558. p[mmc->ios.timing] |= ((best_clk_sample << 8) & SDHCI_SPRD_CMD_DLY_MASK);
  559. } else {
  560. p[mmc->ios.timing] &= ~(SDHCI_SPRD_POSRD_DLY_MASK);
  561. p[mmc->ios.timing] |= ((best_clk_sample << 16) & SDHCI_SPRD_POSRD_DLY_MASK);
  562. }
  563. pr_debug("%s: the best clk sample %d, delay value 0x%08x\n",
  564. mmc_hostname(host->mmc), best_clk_sample, p[mmc->ios.timing]);
  565. out:
  566. sdhci_writel(host, p[mmc->ios.timing], SDHCI_SPRD_REG_32_DLL_DLY);
  567. kfree(value);
  568. return err;
  569. }
  570. static int sdhci_sprd_prepare_sd_hs_cmd_tuning(struct mmc_host *mmc, struct mmc_card *card)
  571. {
  572. return sdhci_sprd_tuning(mmc, card, SDHCI_SPRD_TUNING_SD_HS_CMD);
  573. }
  574. static int sdhci_sprd_execute_sd_hs_data_tuning(struct mmc_host *mmc, struct mmc_card *card)
  575. {
  576. return sdhci_sprd_tuning(mmc, card, SDHCI_SPRD_TUNING_SD_HS_DATA);
  577. }
  578. static void sdhci_sprd_phy_param_parse(struct sdhci_sprd_host *sprd_host,
  579. struct device_node *np)
  580. {
  581. u32 *p = sprd_host->phy_delay;
  582. int ret, i, index;
  583. u32 val[4];
  584. for (i = 0; i < ARRAY_SIZE(sdhci_sprd_phy_cfgs); i++) {
  585. ret = of_property_read_u32_array(np,
  586. sdhci_sprd_phy_cfgs[i].property, val, 4);
  587. if (ret)
  588. continue;
  589. index = sdhci_sprd_phy_cfgs[i].timing;
  590. p[index] = val[0] | (val[1] << 8) | (val[2] << 16) | (val[3] << 24);
  591. }
  592. }
  593. static const struct sdhci_pltfm_data sdhci_sprd_pdata = {
  594. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  595. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
  596. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
  597. SDHCI_QUIRK2_USE_32BIT_BLK_CNT |
  598. SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  599. .ops = &sdhci_sprd_ops,
  600. };
  601. static int sdhci_sprd_probe(struct platform_device *pdev)
  602. {
  603. struct sdhci_host *host;
  604. struct sdhci_sprd_host *sprd_host;
  605. struct mmc_hsq *hsq;
  606. struct clk *clk;
  607. int ret = 0;
  608. host = sdhci_pltfm_init(pdev, &sdhci_sprd_pdata, sizeof(*sprd_host));
  609. if (IS_ERR(host))
  610. return PTR_ERR(host);
  611. host->dma_mask = DMA_BIT_MASK(64);
  612. pdev->dev.dma_mask = &host->dma_mask;
  613. host->mmc_host_ops.request = sdhci_sprd_request;
  614. host->mmc_host_ops.hs400_enhanced_strobe =
  615. sdhci_sprd_hs400_enhanced_strobe;
  616. host->mmc_host_ops.prepare_sd_hs_tuning =
  617. sdhci_sprd_prepare_sd_hs_cmd_tuning;
  618. host->mmc_host_ops.execute_sd_hs_tuning =
  619. sdhci_sprd_execute_sd_hs_data_tuning;
  620. /*
  621. * We can not use the standard ops to change and detect the voltage
  622. * signal for Spreadtrum SD host controller, since our voltage regulator
  623. * for I/O is fixed in hardware, that means we do not need control
  624. * the standard SD host controller to change the I/O voltage.
  625. */
  626. host->mmc_host_ops.start_signal_voltage_switch =
  627. sdhci_sprd_voltage_switch;
  628. host->mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  629. MMC_CAP_WAIT_WHILE_BUSY;
  630. ret = mmc_of_parse(host->mmc);
  631. if (ret)
  632. goto pltfm_free;
  633. if (!mmc_card_is_removable(host->mmc))
  634. host->mmc_host_ops.request_atomic = sdhci_sprd_request_atomic;
  635. else
  636. host->always_defer_done = true;
  637. sprd_host = TO_SPRD_HOST(host);
  638. sdhci_sprd_phy_param_parse(sprd_host, pdev->dev.of_node);
  639. sprd_host->pinctrl = devm_pinctrl_get(&pdev->dev);
  640. if (!IS_ERR(sprd_host->pinctrl)) {
  641. sprd_host->pins_uhs =
  642. pinctrl_lookup_state(sprd_host->pinctrl, "state_uhs");
  643. if (IS_ERR(sprd_host->pins_uhs)) {
  644. ret = PTR_ERR(sprd_host->pins_uhs);
  645. goto pltfm_free;
  646. }
  647. sprd_host->pins_default =
  648. pinctrl_lookup_state(sprd_host->pinctrl, "default");
  649. if (IS_ERR(sprd_host->pins_default)) {
  650. ret = PTR_ERR(sprd_host->pins_default);
  651. goto pltfm_free;
  652. }
  653. }
  654. clk = devm_clk_get(&pdev->dev, "sdio");
  655. if (IS_ERR(clk)) {
  656. ret = PTR_ERR(clk);
  657. goto pltfm_free;
  658. }
  659. sprd_host->clk_sdio = clk;
  660. sprd_host->base_rate = clk_get_rate(sprd_host->clk_sdio);
  661. if (!sprd_host->base_rate)
  662. sprd_host->base_rate = SDHCI_SPRD_CLK_DEF_RATE;
  663. clk = devm_clk_get(&pdev->dev, "enable");
  664. if (IS_ERR(clk)) {
  665. ret = PTR_ERR(clk);
  666. goto pltfm_free;
  667. }
  668. sprd_host->clk_enable = clk;
  669. clk = devm_clk_get(&pdev->dev, "2x_enable");
  670. if (!IS_ERR(clk))
  671. sprd_host->clk_2x_enable = clk;
  672. ret = clk_prepare_enable(sprd_host->clk_sdio);
  673. if (ret)
  674. goto pltfm_free;
  675. ret = clk_prepare_enable(sprd_host->clk_enable);
  676. if (ret)
  677. goto clk_disable;
  678. ret = clk_prepare_enable(sprd_host->clk_2x_enable);
  679. if (ret)
  680. goto clk_disable2;
  681. sdhci_sprd_init_config(host);
  682. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  683. sprd_host->version = ((host->version & SDHCI_VENDOR_VER_MASK) >>
  684. SDHCI_VENDOR_VER_SHIFT);
  685. pm_runtime_get_noresume(&pdev->dev);
  686. pm_runtime_set_active(&pdev->dev);
  687. pm_runtime_enable(&pdev->dev);
  688. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  689. pm_runtime_use_autosuspend(&pdev->dev);
  690. pm_suspend_ignore_children(&pdev->dev, 1);
  691. sdhci_enable_v4_mode(host);
  692. /*
  693. * Supply the existing CAPS, but clear the UHS-I modes. This
  694. * will allow these modes to be specified only by device
  695. * tree properties through mmc_of_parse().
  696. */
  697. sdhci_read_caps(host);
  698. host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
  699. SDHCI_SUPPORT_DDR50);
  700. ret = mmc_regulator_get_supply(host->mmc);
  701. if (ret)
  702. goto pm_runtime_disable;
  703. ret = sdhci_setup_host(host);
  704. if (ret)
  705. goto pm_runtime_disable;
  706. sprd_host->flags = host->flags;
  707. hsq = devm_kzalloc(&pdev->dev, sizeof(*hsq), GFP_KERNEL);
  708. if (!hsq) {
  709. ret = -ENOMEM;
  710. goto err_cleanup_host;
  711. }
  712. ret = mmc_hsq_init(hsq, host->mmc);
  713. if (ret)
  714. goto err_cleanup_host;
  715. ret = __sdhci_add_host(host);
  716. if (ret)
  717. goto err_cleanup_host;
  718. pm_runtime_mark_last_busy(&pdev->dev);
  719. pm_runtime_put_autosuspend(&pdev->dev);
  720. return 0;
  721. err_cleanup_host:
  722. sdhci_cleanup_host(host);
  723. pm_runtime_disable:
  724. pm_runtime_put_noidle(&pdev->dev);
  725. pm_runtime_disable(&pdev->dev);
  726. pm_runtime_set_suspended(&pdev->dev);
  727. clk_disable_unprepare(sprd_host->clk_2x_enable);
  728. clk_disable2:
  729. clk_disable_unprepare(sprd_host->clk_enable);
  730. clk_disable:
  731. clk_disable_unprepare(sprd_host->clk_sdio);
  732. pltfm_free:
  733. sdhci_pltfm_free(pdev);
  734. return ret;
  735. }
  736. static void sdhci_sprd_remove(struct platform_device *pdev)
  737. {
  738. struct sdhci_host *host = platform_get_drvdata(pdev);
  739. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  740. sdhci_remove_host(host, 0);
  741. clk_disable_unprepare(sprd_host->clk_sdio);
  742. clk_disable_unprepare(sprd_host->clk_enable);
  743. clk_disable_unprepare(sprd_host->clk_2x_enable);
  744. sdhci_pltfm_free(pdev);
  745. }
  746. static const struct of_device_id sdhci_sprd_of_match[] = {
  747. { .compatible = "sprd,sdhci-r11", },
  748. { }
  749. };
  750. MODULE_DEVICE_TABLE(of, sdhci_sprd_of_match);
  751. #ifdef CONFIG_PM
  752. static int sdhci_sprd_runtime_suspend(struct device *dev)
  753. {
  754. struct sdhci_host *host = dev_get_drvdata(dev);
  755. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  756. mmc_hsq_suspend(host->mmc);
  757. sdhci_runtime_suspend_host(host);
  758. clk_disable_unprepare(sprd_host->clk_sdio);
  759. clk_disable_unprepare(sprd_host->clk_enable);
  760. clk_disable_unprepare(sprd_host->clk_2x_enable);
  761. return 0;
  762. }
  763. static int sdhci_sprd_runtime_resume(struct device *dev)
  764. {
  765. struct sdhci_host *host = dev_get_drvdata(dev);
  766. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  767. int ret;
  768. ret = clk_prepare_enable(sprd_host->clk_2x_enable);
  769. if (ret)
  770. return ret;
  771. ret = clk_prepare_enable(sprd_host->clk_enable);
  772. if (ret)
  773. goto clk_2x_disable;
  774. ret = clk_prepare_enable(sprd_host->clk_sdio);
  775. if (ret)
  776. goto clk_disable;
  777. sdhci_runtime_resume_host(host, 1);
  778. mmc_hsq_resume(host->mmc);
  779. return 0;
  780. clk_disable:
  781. clk_disable_unprepare(sprd_host->clk_enable);
  782. clk_2x_disable:
  783. clk_disable_unprepare(sprd_host->clk_2x_enable);
  784. return ret;
  785. }
  786. #endif
  787. static const struct dev_pm_ops sdhci_sprd_pm_ops = {
  788. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  789. pm_runtime_force_resume)
  790. SET_RUNTIME_PM_OPS(sdhci_sprd_runtime_suspend,
  791. sdhci_sprd_runtime_resume, NULL)
  792. };
  793. static struct platform_driver sdhci_sprd_driver = {
  794. .probe = sdhci_sprd_probe,
  795. .remove_new = sdhci_sprd_remove,
  796. .driver = {
  797. .name = "sdhci_sprd_r11",
  798. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  799. .of_match_table = sdhci_sprd_of_match,
  800. .pm = &sdhci_sprd_pm_ops,
  801. },
  802. };
  803. module_platform_driver(sdhci_sprd_driver);
  804. MODULE_DESCRIPTION("Spreadtrum sdio host controller r11 driver");
  805. MODULE_LICENSE("GPL v2");
  806. MODULE_ALIAS("platform:sdhci-sprd-r11");