sdhci-sprd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include "sdhci-pltfm.h"
  19. /* SDHCI_ARGUMENT2 register high 16bit */
  20. #define SDHCI_SPRD_ARG2_STUFF GENMASK(31, 16)
  21. #define SDHCI_SPRD_REG_32_DLL_DLY_OFFSET 0x208
  22. #define SDHCIBSPRD_IT_WR_DLY_INV BIT(5)
  23. #define SDHCI_SPRD_BIT_CMD_DLY_INV BIT(13)
  24. #define SDHCI_SPRD_BIT_POSRD_DLY_INV BIT(21)
  25. #define SDHCI_SPRD_BIT_NEGRD_DLY_INV BIT(29)
  26. #define SDHCI_SPRD_REG_32_BUSY_POSI 0x250
  27. #define SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN BIT(25)
  28. #define SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN BIT(24)
  29. #define SDHCI_SPRD_REG_DEBOUNCE 0x28C
  30. #define SDHCI_SPRD_BIT_DLL_BAK BIT(0)
  31. #define SDHCI_SPRD_BIT_DLL_VAL BIT(1)
  32. #define SDHCI_SPRD_INT_SIGNAL_MASK 0x1B7F410B
  33. /* SDHCI_HOST_CONTROL2 */
  34. #define SDHCI_SPRD_CTRL_HS200 0x0005
  35. #define SDHCI_SPRD_CTRL_HS400 0x0006
  36. /*
  37. * According to the standard specification, BIT(3) of SDHCI_SOFTWARE_RESET is
  38. * reserved, and only used on Spreadtrum's design, the hardware cannot work
  39. * if this bit is cleared.
  40. * 1 : normal work
  41. * 0 : hardware reset
  42. */
  43. #define SDHCI_HW_RESET_CARD BIT(3)
  44. #define SDHCI_SPRD_MAX_CUR 0xFFFFFF
  45. #define SDHCI_SPRD_CLK_MAX_DIV 1023
  46. #define SDHCI_SPRD_CLK_DEF_RATE 26000000
  47. struct sdhci_sprd_host {
  48. u32 version;
  49. struct clk *clk_sdio;
  50. struct clk *clk_enable;
  51. u32 base_rate;
  52. int flags; /* backup of host attribute */
  53. };
  54. #define TO_SPRD_HOST(host) sdhci_pltfm_priv(sdhci_priv(host))
  55. static void sdhci_sprd_init_config(struct sdhci_host *host)
  56. {
  57. u16 val;
  58. /* set dll backup mode */
  59. val = sdhci_readl(host, SDHCI_SPRD_REG_DEBOUNCE);
  60. val |= SDHCI_SPRD_BIT_DLL_BAK | SDHCI_SPRD_BIT_DLL_VAL;
  61. sdhci_writel(host, val, SDHCI_SPRD_REG_DEBOUNCE);
  62. }
  63. static inline u32 sdhci_sprd_readl(struct sdhci_host *host, int reg)
  64. {
  65. if (unlikely(reg == SDHCI_MAX_CURRENT))
  66. return SDHCI_SPRD_MAX_CUR;
  67. return readl_relaxed(host->ioaddr + reg);
  68. }
  69. static inline void sdhci_sprd_writel(struct sdhci_host *host, u32 val, int reg)
  70. {
  71. /* SDHCI_MAX_CURRENT is reserved on Spreadtrum's platform */
  72. if (unlikely(reg == SDHCI_MAX_CURRENT))
  73. return;
  74. if (unlikely(reg == SDHCI_SIGNAL_ENABLE || reg == SDHCI_INT_ENABLE))
  75. val = val & SDHCI_SPRD_INT_SIGNAL_MASK;
  76. writel_relaxed(val, host->ioaddr + reg);
  77. }
  78. static inline void sdhci_sprd_writew(struct sdhci_host *host, u16 val, int reg)
  79. {
  80. /* SDHCI_BLOCK_COUNT is Read Only on Spreadtrum's platform */
  81. if (unlikely(reg == SDHCI_BLOCK_COUNT))
  82. return;
  83. writew_relaxed(val, host->ioaddr + reg);
  84. }
  85. static inline void sdhci_sprd_writeb(struct sdhci_host *host, u8 val, int reg)
  86. {
  87. /*
  88. * Since BIT(3) of SDHCI_SOFTWARE_RESET is reserved according to the
  89. * standard specification, sdhci_reset() write this register directly
  90. * without checking other reserved bits, that will clear BIT(3) which
  91. * is defined as hardware reset on Spreadtrum's platform and clearing
  92. * it by mistake will lead the card not work. So here we need to work
  93. * around it.
  94. */
  95. if (unlikely(reg == SDHCI_SOFTWARE_RESET)) {
  96. if (readb_relaxed(host->ioaddr + reg) & SDHCI_HW_RESET_CARD)
  97. val |= SDHCI_HW_RESET_CARD;
  98. }
  99. writeb_relaxed(val, host->ioaddr + reg);
  100. }
  101. static inline void sdhci_sprd_sd_clk_off(struct sdhci_host *host)
  102. {
  103. u16 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  104. ctrl &= ~SDHCI_CLOCK_CARD_EN;
  105. sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
  106. }
  107. static inline void
  108. sdhci_sprd_set_dll_invert(struct sdhci_host *host, u32 mask, bool en)
  109. {
  110. u32 dll_dly_offset;
  111. dll_dly_offset = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
  112. if (en)
  113. dll_dly_offset |= mask;
  114. else
  115. dll_dly_offset &= ~mask;
  116. sdhci_writel(host, dll_dly_offset, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
  117. }
  118. static inline u32 sdhci_sprd_calc_div(u32 base_clk, u32 clk)
  119. {
  120. u32 div;
  121. /* select 2x clock source */
  122. if (base_clk <= clk * 2)
  123. return 0;
  124. div = (u32) (base_clk / (clk * 2));
  125. if ((base_clk / div) > (clk * 2))
  126. div++;
  127. if (div > SDHCI_SPRD_CLK_MAX_DIV)
  128. div = SDHCI_SPRD_CLK_MAX_DIV;
  129. if (div % 2)
  130. div = (div + 1) / 2;
  131. else
  132. div = div / 2;
  133. return div;
  134. }
  135. static inline void _sdhci_sprd_set_clock(struct sdhci_host *host,
  136. unsigned int clk)
  137. {
  138. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  139. u32 div, val, mask;
  140. div = sdhci_sprd_calc_div(sprd_host->base_rate, clk);
  141. clk |= ((div & 0x300) >> 2) | ((div & 0xFF) << 8);
  142. sdhci_enable_clk(host, clk);
  143. /* enable auto gate sdhc_enable_auto_gate */
  144. val = sdhci_readl(host, SDHCI_SPRD_REG_32_BUSY_POSI);
  145. mask = SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN |
  146. SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN;
  147. if (mask != (val & mask)) {
  148. val |= mask;
  149. sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
  150. }
  151. }
  152. static void sdhci_sprd_set_clock(struct sdhci_host *host, unsigned int clock)
  153. {
  154. bool en = false;
  155. if (clock == 0) {
  156. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  157. } else if (clock != host->clock) {
  158. sdhci_sprd_sd_clk_off(host);
  159. _sdhci_sprd_set_clock(host, clock);
  160. if (clock <= 400000)
  161. en = true;
  162. sdhci_sprd_set_dll_invert(host, SDHCI_SPRD_BIT_CMD_DLY_INV |
  163. SDHCI_SPRD_BIT_POSRD_DLY_INV, en);
  164. } else {
  165. _sdhci_sprd_set_clock(host, clock);
  166. }
  167. }
  168. static unsigned int sdhci_sprd_get_max_clock(struct sdhci_host *host)
  169. {
  170. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  171. return clk_round_rate(sprd_host->clk_sdio, ULONG_MAX);
  172. }
  173. static unsigned int sdhci_sprd_get_min_clock(struct sdhci_host *host)
  174. {
  175. return 400000;
  176. }
  177. static void sdhci_sprd_set_uhs_signaling(struct sdhci_host *host,
  178. unsigned int timing)
  179. {
  180. u16 ctrl_2;
  181. if (timing == host->timing)
  182. return;
  183. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  184. /* Select Bus Speed Mode for host */
  185. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  186. switch (timing) {
  187. case MMC_TIMING_UHS_SDR12:
  188. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  189. break;
  190. case MMC_TIMING_MMC_HS:
  191. case MMC_TIMING_SD_HS:
  192. case MMC_TIMING_UHS_SDR25:
  193. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  194. break;
  195. case MMC_TIMING_UHS_SDR50:
  196. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  197. break;
  198. case MMC_TIMING_UHS_SDR104:
  199. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  200. break;
  201. case MMC_TIMING_UHS_DDR50:
  202. case MMC_TIMING_MMC_DDR52:
  203. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  204. break;
  205. case MMC_TIMING_MMC_HS200:
  206. ctrl_2 |= SDHCI_SPRD_CTRL_HS200;
  207. break;
  208. case MMC_TIMING_MMC_HS400:
  209. ctrl_2 |= SDHCI_SPRD_CTRL_HS400;
  210. break;
  211. default:
  212. break;
  213. }
  214. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  215. }
  216. static void sdhci_sprd_hw_reset(struct sdhci_host *host)
  217. {
  218. int val;
  219. /*
  220. * Note: don't use sdhci_writeb() API here since it is redirected to
  221. * sdhci_sprd_writeb() in which we have a workaround for
  222. * SDHCI_SOFTWARE_RESET which would make bit SDHCI_HW_RESET_CARD can
  223. * not be cleared.
  224. */
  225. val = readb_relaxed(host->ioaddr + SDHCI_SOFTWARE_RESET);
  226. val &= ~SDHCI_HW_RESET_CARD;
  227. writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
  228. /* wait for 10 us */
  229. usleep_range(10, 20);
  230. val |= SDHCI_HW_RESET_CARD;
  231. writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
  232. usleep_range(300, 500);
  233. }
  234. static struct sdhci_ops sdhci_sprd_ops = {
  235. .read_l = sdhci_sprd_readl,
  236. .write_l = sdhci_sprd_writel,
  237. .write_b = sdhci_sprd_writeb,
  238. .set_clock = sdhci_sprd_set_clock,
  239. .get_max_clock = sdhci_sprd_get_max_clock,
  240. .get_min_clock = sdhci_sprd_get_min_clock,
  241. .set_bus_width = sdhci_set_bus_width,
  242. .reset = sdhci_reset,
  243. .set_uhs_signaling = sdhci_sprd_set_uhs_signaling,
  244. .hw_reset = sdhci_sprd_hw_reset,
  245. };
  246. static void sdhci_sprd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  247. {
  248. struct sdhci_host *host = mmc_priv(mmc);
  249. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  250. host->flags |= sprd_host->flags & SDHCI_AUTO_CMD23;
  251. /*
  252. * From version 4.10 onward, ARGUMENT2 register is also as 32-bit
  253. * block count register which doesn't support stuff bits of
  254. * CMD23 argument on Spreadtrum's sd host controller.
  255. */
  256. if (host->version >= SDHCI_SPEC_410 &&
  257. mrq->sbc && (mrq->sbc->arg & SDHCI_SPRD_ARG2_STUFF) &&
  258. (host->flags & SDHCI_AUTO_CMD23))
  259. host->flags &= ~SDHCI_AUTO_CMD23;
  260. sdhci_request(mmc, mrq);
  261. }
  262. static const struct sdhci_pltfm_data sdhci_sprd_pdata = {
  263. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
  264. .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
  265. SDHCI_QUIRK2_USE_32BIT_BLK_CNT,
  266. .ops = &sdhci_sprd_ops,
  267. };
  268. static int sdhci_sprd_probe(struct platform_device *pdev)
  269. {
  270. struct sdhci_host *host;
  271. struct sdhci_sprd_host *sprd_host;
  272. struct clk *clk;
  273. int ret = 0;
  274. host = sdhci_pltfm_init(pdev, &sdhci_sprd_pdata, sizeof(*sprd_host));
  275. if (IS_ERR(host))
  276. return PTR_ERR(host);
  277. host->dma_mask = DMA_BIT_MASK(64);
  278. pdev->dev.dma_mask = &host->dma_mask;
  279. host->mmc_host_ops.request = sdhci_sprd_request;
  280. host->mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  281. MMC_CAP_ERASE | MMC_CAP_CMD23;
  282. ret = mmc_of_parse(host->mmc);
  283. if (ret)
  284. goto pltfm_free;
  285. sprd_host = TO_SPRD_HOST(host);
  286. clk = devm_clk_get(&pdev->dev, "sdio");
  287. if (IS_ERR(clk)) {
  288. ret = PTR_ERR(clk);
  289. goto pltfm_free;
  290. }
  291. sprd_host->clk_sdio = clk;
  292. sprd_host->base_rate = clk_get_rate(sprd_host->clk_sdio);
  293. if (!sprd_host->base_rate)
  294. sprd_host->base_rate = SDHCI_SPRD_CLK_DEF_RATE;
  295. clk = devm_clk_get(&pdev->dev, "enable");
  296. if (IS_ERR(clk)) {
  297. ret = PTR_ERR(clk);
  298. goto pltfm_free;
  299. }
  300. sprd_host->clk_enable = clk;
  301. ret = clk_prepare_enable(sprd_host->clk_sdio);
  302. if (ret)
  303. goto pltfm_free;
  304. clk_prepare_enable(sprd_host->clk_enable);
  305. if (ret)
  306. goto clk_disable;
  307. sdhci_sprd_init_config(host);
  308. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  309. sprd_host->version = ((host->version & SDHCI_VENDOR_VER_MASK) >>
  310. SDHCI_VENDOR_VER_SHIFT);
  311. pm_runtime_get_noresume(&pdev->dev);
  312. pm_runtime_set_active(&pdev->dev);
  313. pm_runtime_enable(&pdev->dev);
  314. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  315. pm_runtime_use_autosuspend(&pdev->dev);
  316. pm_suspend_ignore_children(&pdev->dev, 1);
  317. sdhci_enable_v4_mode(host);
  318. ret = sdhci_setup_host(host);
  319. if (ret)
  320. goto pm_runtime_disable;
  321. sprd_host->flags = host->flags;
  322. ret = __sdhci_add_host(host);
  323. if (ret)
  324. goto err_cleanup_host;
  325. pm_runtime_mark_last_busy(&pdev->dev);
  326. pm_runtime_put_autosuspend(&pdev->dev);
  327. return 0;
  328. err_cleanup_host:
  329. sdhci_cleanup_host(host);
  330. pm_runtime_disable:
  331. pm_runtime_disable(&pdev->dev);
  332. pm_runtime_set_suspended(&pdev->dev);
  333. clk_disable_unprepare(sprd_host->clk_enable);
  334. clk_disable:
  335. clk_disable_unprepare(sprd_host->clk_sdio);
  336. pltfm_free:
  337. sdhci_pltfm_free(pdev);
  338. return ret;
  339. }
  340. static int sdhci_sprd_remove(struct platform_device *pdev)
  341. {
  342. struct sdhci_host *host = platform_get_drvdata(pdev);
  343. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  344. struct mmc_host *mmc = host->mmc;
  345. mmc_remove_host(mmc);
  346. clk_disable_unprepare(sprd_host->clk_sdio);
  347. clk_disable_unprepare(sprd_host->clk_enable);
  348. mmc_free_host(mmc);
  349. return 0;
  350. }
  351. static const struct of_device_id sdhci_sprd_of_match[] = {
  352. { .compatible = "sprd,sdhci-r11", },
  353. { }
  354. };
  355. MODULE_DEVICE_TABLE(of, sdhci_sprd_of_match);
  356. #ifdef CONFIG_PM
  357. static int sdhci_sprd_runtime_suspend(struct device *dev)
  358. {
  359. struct sdhci_host *host = dev_get_drvdata(dev);
  360. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  361. sdhci_runtime_suspend_host(host);
  362. clk_disable_unprepare(sprd_host->clk_sdio);
  363. clk_disable_unprepare(sprd_host->clk_enable);
  364. return 0;
  365. }
  366. static int sdhci_sprd_runtime_resume(struct device *dev)
  367. {
  368. struct sdhci_host *host = dev_get_drvdata(dev);
  369. struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
  370. int ret;
  371. ret = clk_prepare_enable(sprd_host->clk_enable);
  372. if (ret)
  373. return ret;
  374. ret = clk_prepare_enable(sprd_host->clk_sdio);
  375. if (ret) {
  376. clk_disable_unprepare(sprd_host->clk_enable);
  377. return ret;
  378. }
  379. sdhci_runtime_resume_host(host);
  380. return 0;
  381. }
  382. #endif
  383. static const struct dev_pm_ops sdhci_sprd_pm_ops = {
  384. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  385. pm_runtime_force_resume)
  386. SET_RUNTIME_PM_OPS(sdhci_sprd_runtime_suspend,
  387. sdhci_sprd_runtime_resume, NULL)
  388. };
  389. static struct platform_driver sdhci_sprd_driver = {
  390. .probe = sdhci_sprd_probe,
  391. .remove = sdhci_sprd_remove,
  392. .driver = {
  393. .name = "sdhci_sprd_r11",
  394. .of_match_table = of_match_ptr(sdhci_sprd_of_match),
  395. .pm = &sdhci_sprd_pm_ops,
  396. },
  397. };
  398. module_platform_driver(sdhci_sprd_driver);
  399. MODULE_DESCRIPTION("Spreadtrum sdio host controller r11 driver");
  400. MODULE_LICENSE("GPL v2");
  401. MODULE_ALIAS("platform:sdhci-sprd-r11");