sdhci-xenon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /*
  242. * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
  243. * Thus SDHCI_CTRL_VDD_180 bit might not work then.
  244. * Skip the standard voltage switch to avoid any issue.
  245. */
  246. if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
  247. return 0;
  248. return sdhci_start_signal_voltage_switch(mmc, ios);
  249. }
  250. /*
  251. * Update card type.
  252. * priv->init_card_type will be used in PHY timing adjustment.
  253. */
  254. static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
  255. {
  256. struct sdhci_host *host = mmc_priv(mmc);
  257. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  258. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  259. /* Update card type*/
  260. priv->init_card_type = card->type;
  261. }
  262. static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
  263. {
  264. struct sdhci_host *host = mmc_priv(mmc);
  265. if (host->timing == MMC_TIMING_UHS_DDR50)
  266. return 0;
  267. /*
  268. * Currently force Xenon driver back to support mode 1 only,
  269. * even though Xenon might claim to support mode 2 or mode 3.
  270. * It requires more time to test mode 2/mode 3 on more platforms.
  271. */
  272. if (host->tuning_mode != SDHCI_TUNING_MODE_1)
  273. xenon_retune_setup(host);
  274. return sdhci_execute_tuning(mmc, opcode);
  275. }
  276. static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
  277. {
  278. struct sdhci_host *host = mmc_priv(mmc);
  279. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  280. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  281. u32 reg;
  282. u8 sdhc_id = priv->sdhc_id;
  283. sdhci_enable_sdio_irq(mmc, enable);
  284. if (enable) {
  285. /*
  286. * Set SDIO Card Inserted indication
  287. * to enable detecting SDIO async irq.
  288. */
  289. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  290. reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  291. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  292. } else {
  293. /* Clear SDIO Card Inserted indication */
  294. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  295. reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  296. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  297. }
  298. }
  299. static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
  300. {
  301. host->mmc_host_ops.set_ios = xenon_set_ios;
  302. host->mmc_host_ops.start_signal_voltage_switch =
  303. xenon_start_signal_voltage_switch;
  304. host->mmc_host_ops.init_card = xenon_init_card;
  305. host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
  306. host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
  307. }
  308. /*
  309. * Parse Xenon specific DT properties:
  310. * sdhc-id: the index of current SDHC.
  311. * Refer to XENON_SYS_CFG_INFO register
  312. * tun-count: the interval between re-tuning
  313. */
  314. static int xenon_probe_dt(struct platform_device *pdev)
  315. {
  316. struct device_node *np = pdev->dev.of_node;
  317. struct sdhci_host *host = platform_get_drvdata(pdev);
  318. struct mmc_host *mmc = host->mmc;
  319. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  320. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  321. u32 sdhc_id, nr_sdhc;
  322. u32 tuning_count;
  323. /* Disable HS200 on Armada AP806 */
  324. if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
  325. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  326. sdhc_id = 0x0;
  327. if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
  328. nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
  329. nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
  330. if (unlikely(sdhc_id > nr_sdhc)) {
  331. dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
  332. sdhc_id, nr_sdhc);
  333. return -EINVAL;
  334. }
  335. }
  336. priv->sdhc_id = sdhc_id;
  337. tuning_count = XENON_DEF_TUNING_COUNT;
  338. if (!of_property_read_u32(np, "marvell,xenon-tun-count",
  339. &tuning_count)) {
  340. if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
  341. dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
  342. XENON_DEF_TUNING_COUNT);
  343. tuning_count = XENON_DEF_TUNING_COUNT;
  344. }
  345. }
  346. priv->tuning_count = tuning_count;
  347. return xenon_phy_parse_dt(np, host);
  348. }
  349. static int xenon_sdhc_prepare(struct sdhci_host *host)
  350. {
  351. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  352. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  353. u8 sdhc_id = priv->sdhc_id;
  354. /* Enable SDHC */
  355. xenon_enable_sdhc(host, sdhc_id);
  356. /* Enable ACG */
  357. xenon_set_acg(host, true);
  358. /* Enable Parallel Transfer Mode */
  359. xenon_enable_sdhc_parallel_tran(host, sdhc_id);
  360. /* Disable SDCLK-Off-While-Idle before card init */
  361. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  362. xenon_mask_cmd_conflict_err(host);
  363. return 0;
  364. }
  365. static void xenon_sdhc_unprepare(struct sdhci_host *host)
  366. {
  367. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  368. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  369. u8 sdhc_id = priv->sdhc_id;
  370. /* disable SDHC */
  371. xenon_disable_sdhc(host, sdhc_id);
  372. }
  373. static int xenon_probe(struct platform_device *pdev)
  374. {
  375. struct sdhci_pltfm_host *pltfm_host;
  376. struct sdhci_host *host;
  377. struct xenon_priv *priv;
  378. int err;
  379. host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
  380. sizeof(struct xenon_priv));
  381. if (IS_ERR(host))
  382. return PTR_ERR(host);
  383. pltfm_host = sdhci_priv(host);
  384. priv = sdhci_pltfm_priv(pltfm_host);
  385. /*
  386. * Link Xenon specific mmc_host_ops function,
  387. * to replace standard ones in sdhci_ops.
  388. */
  389. xenon_replace_mmc_host_ops(host);
  390. pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
  391. if (IS_ERR(pltfm_host->clk)) {
  392. err = PTR_ERR(pltfm_host->clk);
  393. dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
  394. goto free_pltfm;
  395. }
  396. err = clk_prepare_enable(pltfm_host->clk);
  397. if (err)
  398. goto free_pltfm;
  399. err = mmc_of_parse(host->mmc);
  400. if (err)
  401. goto err_clk;
  402. sdhci_get_of_property(pdev);
  403. xenon_set_acg(host, false);
  404. /* Xenon specific dt parse */
  405. err = xenon_probe_dt(pdev);
  406. if (err)
  407. goto err_clk;
  408. err = xenon_sdhc_prepare(host);
  409. if (err)
  410. goto clean_phy_param;
  411. err = sdhci_add_host(host);
  412. if (err)
  413. goto remove_sdhc;
  414. return 0;
  415. remove_sdhc:
  416. xenon_sdhc_unprepare(host);
  417. clean_phy_param:
  418. xenon_clean_phy(host);
  419. err_clk:
  420. clk_disable_unprepare(pltfm_host->clk);
  421. free_pltfm:
  422. sdhci_pltfm_free(pdev);
  423. return err;
  424. }
  425. static int xenon_remove(struct platform_device *pdev)
  426. {
  427. struct sdhci_host *host = platform_get_drvdata(pdev);
  428. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  429. xenon_clean_phy(host);
  430. xenon_sdhc_unprepare(host);
  431. sdhci_remove_host(host, 0);
  432. clk_disable_unprepare(pltfm_host->clk);
  433. sdhci_pltfm_free(pdev);
  434. return 0;
  435. }
  436. static const struct of_device_id sdhci_xenon_dt_ids[] = {
  437. { .compatible = "marvell,armada-ap806-sdhci",},
  438. { .compatible = "marvell,armada-cp110-sdhci",},
  439. { .compatible = "marvell,armada-3700-sdhci",},
  440. {}
  441. };
  442. MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
  443. static struct platform_driver sdhci_xenon_driver = {
  444. .driver = {
  445. .name = "xenon-sdhci",
  446. .of_match_table = sdhci_xenon_dt_ids,
  447. .pm = &sdhci_pltfm_pmops,
  448. },
  449. .probe = xenon_probe,
  450. .remove = xenon_remove,
  451. };
  452. module_platform_driver(sdhci_xenon_driver);
  453. MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
  454. MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
  455. MODULE_LICENSE("GPL v2");