sdhci-xenon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Driver for Marvell Xenon SDHC as a platform device
  3. *
  4. * Copyright (C) 2016 Marvell, All Rights Reserved.
  5. *
  6. * Author: Hu Ziji <huziji@marvell.com>
  7. * Date: 2016-8-24
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation version 2.
  12. *
  13. * Inspired by Jisheng Zhang <jszhang@marvell.com>
  14. * Special thanks to Video BG4 project team.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/ktime.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include "sdhci-pltfm.h"
  21. #include "sdhci-xenon.h"
  22. static int xenon_enable_internal_clk(struct sdhci_host *host)
  23. {
  24. u32 reg;
  25. ktime_t timeout;
  26. reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
  27. reg |= SDHCI_CLOCK_INT_EN;
  28. sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
  29. /* Wait max 20 ms */
  30. timeout = ktime_add_ms(ktime_get(), 20);
  31. while (!((reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  32. & SDHCI_CLOCK_INT_STABLE)) {
  33. if (ktime_after(ktime_get(), timeout)) {
  34. dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
  35. return -ETIMEDOUT;
  36. }
  37. usleep_range(900, 1100);
  38. }
  39. return 0;
  40. }
  41. /* Set SDCLK-off-while-idle */
  42. static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
  43. unsigned char sdhc_id, bool enable)
  44. {
  45. u32 reg;
  46. u32 mask;
  47. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  48. /* Get the bit shift basing on the SDHC index */
  49. mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
  50. if (enable)
  51. reg |= mask;
  52. else
  53. reg &= ~mask;
  54. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  55. }
  56. /* Enable/Disable the Auto Clock Gating function */
  57. static void xenon_set_acg(struct sdhci_host *host, bool enable)
  58. {
  59. u32 reg;
  60. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  61. if (enable)
  62. reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
  63. else
  64. reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
  65. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  66. }
  67. /* Enable this SDHC */
  68. static void xenon_enable_sdhc(struct sdhci_host *host,
  69. unsigned char sdhc_id)
  70. {
  71. u32 reg;
  72. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  73. reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
  74. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  75. host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  76. /*
  77. * Force to clear BUS_TEST to
  78. * skip bus_test_pre and bus_test_post
  79. */
  80. host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
  81. }
  82. /* Disable this SDHC */
  83. static void xenon_disable_sdhc(struct sdhci_host *host,
  84. unsigned char sdhc_id)
  85. {
  86. u32 reg;
  87. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  88. reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
  89. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  90. }
  91. /* Enable Parallel Transfer Mode */
  92. static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
  93. unsigned char sdhc_id)
  94. {
  95. u32 reg;
  96. reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
  97. reg |= BIT(sdhc_id);
  98. sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
  99. }
  100. /* Mask command conflict error */
  101. static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
  102. {
  103. u32 reg;
  104. reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
  105. reg |= XENON_MASK_CMD_CONFLICT_ERR;
  106. sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
  107. }
  108. static void xenon_retune_setup(struct sdhci_host *host)
  109. {
  110. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  111. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  112. u32 reg;
  113. /* Disable the Re-Tuning Request functionality */
  114. reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
  115. reg &= ~XENON_RETUNING_COMPATIBLE;
  116. sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
  117. /* Disable the Re-tuning Interrupt */
  118. reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
  119. reg &= ~SDHCI_INT_RETUNE;
  120. sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
  121. reg = sdhci_readl(host, SDHCI_INT_ENABLE);
  122. reg &= ~SDHCI_INT_RETUNE;
  123. sdhci_writel(host, reg, SDHCI_INT_ENABLE);
  124. /* Force to use Tuning Mode 1 */
  125. host->tuning_mode = SDHCI_TUNING_MODE_1;
  126. /* Set re-tuning period */
  127. host->tuning_count = 1 << (priv->tuning_count - 1);
  128. }
  129. /*
  130. * Operations inside struct sdhci_ops
  131. */
  132. /* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
  133. static void xenon_reset_exit(struct sdhci_host *host,
  134. unsigned char sdhc_id, u8 mask)
  135. {
  136. /* Only SOFTWARE RESET ALL will clear the register setting */
  137. if (!(mask & SDHCI_RESET_ALL))
  138. return;
  139. /* Disable tuning request and auto-retuning again */
  140. xenon_retune_setup(host);
  141. xenon_set_acg(host, true);
  142. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  143. xenon_mask_cmd_conflict_err(host);
  144. }
  145. static void xenon_reset(struct sdhci_host *host, u8 mask)
  146. {
  147. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  148. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  149. sdhci_reset(host, mask);
  150. xenon_reset_exit(host, priv->sdhc_id, mask);
  151. }
  152. /*
  153. * Xenon defines different values for HS200 and HS400
  154. * in Host_Control_2
  155. */
  156. static void xenon_set_uhs_signaling(struct sdhci_host *host,
  157. unsigned int timing)
  158. {
  159. u16 ctrl_2;
  160. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  161. /* Select Bus Speed Mode for host */
  162. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  163. if (timing == MMC_TIMING_MMC_HS200)
  164. ctrl_2 |= XENON_CTRL_HS200;
  165. else if (timing == MMC_TIMING_UHS_SDR104)
  166. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  167. else if (timing == MMC_TIMING_UHS_SDR12)
  168. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  169. else if (timing == MMC_TIMING_UHS_SDR25)
  170. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  171. else if (timing == MMC_TIMING_UHS_SDR50)
  172. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  173. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  174. (timing == MMC_TIMING_MMC_DDR52))
  175. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  176. else if (timing == MMC_TIMING_MMC_HS400)
  177. ctrl_2 |= XENON_CTRL_HS400;
  178. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  179. }
  180. static const struct sdhci_ops sdhci_xenon_ops = {
  181. .set_clock = sdhci_set_clock,
  182. .set_bus_width = sdhci_set_bus_width,
  183. .reset = xenon_reset,
  184. .set_uhs_signaling = xenon_set_uhs_signaling,
  185. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  186. };
  187. static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
  188. .ops = &sdhci_xenon_ops,
  189. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  190. SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  191. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  192. };
  193. /*
  194. * Xenon Specific Operations in mmc_host_ops
  195. */
  196. static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  197. {
  198. struct sdhci_host *host = mmc_priv(mmc);
  199. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  200. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  201. u32 reg;
  202. /*
  203. * HS400/HS200/eMMC HS doesn't have Preset Value register.
  204. * However, sdhci_set_ios will read HS400/HS200 Preset register.
  205. * Disable Preset Value register for HS400/HS200.
  206. * eMMC HS with preset_enabled set will trigger a bug in
  207. * get_preset_value().
  208. */
  209. if ((ios->timing == MMC_TIMING_MMC_HS400) ||
  210. (ios->timing == MMC_TIMING_MMC_HS200) ||
  211. (ios->timing == MMC_TIMING_MMC_HS)) {
  212. host->preset_enabled = false;
  213. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  214. host->flags &= ~SDHCI_PV_ENABLED;
  215. reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  216. reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  217. sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
  218. } else {
  219. host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  220. }
  221. sdhci_set_ios(mmc, ios);
  222. xenon_phy_adj(host, ios);
  223. if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
  224. xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
  225. }
  226. static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
  227. struct mmc_ios *ios)
  228. {
  229. struct sdhci_host *host = mmc_priv(mmc);
  230. /*
  231. * Before SD/SDIO set signal voltage, SD bus clock should be
  232. * disabled. However, sdhci_set_clock will also disable the Internal
  233. * clock in mmc_set_signal_voltage().
  234. * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
  235. * Thus here manually enable internal clock.
  236. *
  237. * After switch completes, it is unnecessary to disable internal clock,
  238. * since keeping internal clock active obeys SD spec.
  239. */
  240. xenon_enable_internal_clk(host);
  241. xenon_soc_pad_ctrl(host, ios->signal_voltage);
  242. /*
  243. * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
  244. * Thus SDHCI_CTRL_VDD_180 bit might not work then.
  245. * Skip the standard voltage switch to avoid any issue.
  246. */
  247. if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
  248. return 0;
  249. return sdhci_start_signal_voltage_switch(mmc, ios);
  250. }
  251. /*
  252. * Update card type.
  253. * priv->init_card_type will be used in PHY timing adjustment.
  254. */
  255. static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
  256. {
  257. struct sdhci_host *host = mmc_priv(mmc);
  258. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  259. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  260. /* Update card type*/
  261. priv->init_card_type = card->type;
  262. }
  263. static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
  264. {
  265. struct sdhci_host *host = mmc_priv(mmc);
  266. if (host->timing == MMC_TIMING_UHS_DDR50)
  267. return 0;
  268. /*
  269. * Currently force Xenon driver back to support mode 1 only,
  270. * even though Xenon might claim to support mode 2 or mode 3.
  271. * It requires more time to test mode 2/mode 3 on more platforms.
  272. */
  273. if (host->tuning_mode != SDHCI_TUNING_MODE_1)
  274. xenon_retune_setup(host);
  275. return sdhci_execute_tuning(mmc, opcode);
  276. }
  277. static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
  278. {
  279. struct sdhci_host *host = mmc_priv(mmc);
  280. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  281. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  282. u32 reg;
  283. u8 sdhc_id = priv->sdhc_id;
  284. sdhci_enable_sdio_irq(mmc, enable);
  285. if (enable) {
  286. /*
  287. * Set SDIO Card Inserted indication
  288. * to enable detecting SDIO async irq.
  289. */
  290. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  291. reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  292. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  293. } else {
  294. /* Clear SDIO Card Inserted indication */
  295. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  296. reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  297. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  298. }
  299. }
  300. static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
  301. {
  302. host->mmc_host_ops.set_ios = xenon_set_ios;
  303. host->mmc_host_ops.start_signal_voltage_switch =
  304. xenon_start_signal_voltage_switch;
  305. host->mmc_host_ops.init_card = xenon_init_card;
  306. host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
  307. host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
  308. }
  309. /*
  310. * Parse Xenon specific DT properties:
  311. * sdhc-id: the index of current SDHC.
  312. * Refer to XENON_SYS_CFG_INFO register
  313. * tun-count: the interval between re-tuning
  314. */
  315. static int xenon_probe_dt(struct platform_device *pdev)
  316. {
  317. struct device_node *np = pdev->dev.of_node;
  318. struct sdhci_host *host = platform_get_drvdata(pdev);
  319. struct mmc_host *mmc = host->mmc;
  320. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  321. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  322. u32 sdhc_id, nr_sdhc;
  323. u32 tuning_count;
  324. /* Disable HS200 on Armada AP806 */
  325. if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
  326. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  327. sdhc_id = 0x0;
  328. if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
  329. nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
  330. nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
  331. if (unlikely(sdhc_id > nr_sdhc)) {
  332. dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
  333. sdhc_id, nr_sdhc);
  334. return -EINVAL;
  335. }
  336. }
  337. priv->sdhc_id = sdhc_id;
  338. tuning_count = XENON_DEF_TUNING_COUNT;
  339. if (!of_property_read_u32(np, "marvell,xenon-tun-count",
  340. &tuning_count)) {
  341. if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
  342. dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
  343. XENON_DEF_TUNING_COUNT);
  344. tuning_count = XENON_DEF_TUNING_COUNT;
  345. }
  346. }
  347. priv->tuning_count = tuning_count;
  348. return xenon_phy_parse_dt(np, host);
  349. }
  350. static int xenon_sdhc_prepare(struct sdhci_host *host)
  351. {
  352. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  353. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  354. u8 sdhc_id = priv->sdhc_id;
  355. /* Enable SDHC */
  356. xenon_enable_sdhc(host, sdhc_id);
  357. /* Enable ACG */
  358. xenon_set_acg(host, true);
  359. /* Enable Parallel Transfer Mode */
  360. xenon_enable_sdhc_parallel_tran(host, sdhc_id);
  361. /* Disable SDCLK-Off-While-Idle before card init */
  362. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  363. xenon_mask_cmd_conflict_err(host);
  364. return 0;
  365. }
  366. static void xenon_sdhc_unprepare(struct sdhci_host *host)
  367. {
  368. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  369. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  370. u8 sdhc_id = priv->sdhc_id;
  371. /* disable SDHC */
  372. xenon_disable_sdhc(host, sdhc_id);
  373. }
  374. static int xenon_probe(struct platform_device *pdev)
  375. {
  376. struct sdhci_pltfm_host *pltfm_host;
  377. struct sdhci_host *host;
  378. struct xenon_priv *priv;
  379. int err;
  380. host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
  381. sizeof(struct xenon_priv));
  382. if (IS_ERR(host))
  383. return PTR_ERR(host);
  384. pltfm_host = sdhci_priv(host);
  385. priv = sdhci_pltfm_priv(pltfm_host);
  386. /*
  387. * Link Xenon specific mmc_host_ops function,
  388. * to replace standard ones in sdhci_ops.
  389. */
  390. xenon_replace_mmc_host_ops(host);
  391. pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
  392. if (IS_ERR(pltfm_host->clk)) {
  393. err = PTR_ERR(pltfm_host->clk);
  394. dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
  395. goto free_pltfm;
  396. }
  397. err = clk_prepare_enable(pltfm_host->clk);
  398. if (err)
  399. goto free_pltfm;
  400. err = mmc_of_parse(host->mmc);
  401. if (err)
  402. goto err_clk;
  403. sdhci_get_of_property(pdev);
  404. xenon_set_acg(host, false);
  405. /* Xenon specific dt parse */
  406. err = xenon_probe_dt(pdev);
  407. if (err)
  408. goto err_clk;
  409. err = xenon_sdhc_prepare(host);
  410. if (err)
  411. goto err_clk;
  412. err = sdhci_add_host(host);
  413. if (err)
  414. goto remove_sdhc;
  415. return 0;
  416. remove_sdhc:
  417. xenon_sdhc_unprepare(host);
  418. err_clk:
  419. clk_disable_unprepare(pltfm_host->clk);
  420. free_pltfm:
  421. sdhci_pltfm_free(pdev);
  422. return err;
  423. }
  424. static int xenon_remove(struct platform_device *pdev)
  425. {
  426. struct sdhci_host *host = platform_get_drvdata(pdev);
  427. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  428. sdhci_remove_host(host, 0);
  429. xenon_sdhc_unprepare(host);
  430. clk_disable_unprepare(pltfm_host->clk);
  431. sdhci_pltfm_free(pdev);
  432. return 0;
  433. }
  434. static const struct of_device_id sdhci_xenon_dt_ids[] = {
  435. { .compatible = "marvell,armada-ap806-sdhci",},
  436. { .compatible = "marvell,armada-cp110-sdhci",},
  437. { .compatible = "marvell,armada-3700-sdhci",},
  438. {}
  439. };
  440. MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
  441. static struct platform_driver sdhci_xenon_driver = {
  442. .driver = {
  443. .name = "xenon-sdhci",
  444. .of_match_table = sdhci_xenon_dt_ids,
  445. .pm = &sdhci_pltfm_pmops,
  446. },
  447. .probe = xenon_probe,
  448. .remove = xenon_remove,
  449. };
  450. module_platform_driver(sdhci_xenon_driver);
  451. MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
  452. MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
  453. MODULE_LICENSE("GPL v2");