sdhci-xenon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 <linux/pm.h>
  21. #include <linux/pm_runtime.h>
  22. #include "sdhci-pltfm.h"
  23. #include "sdhci-xenon.h"
  24. static int xenon_enable_internal_clk(struct sdhci_host *host)
  25. {
  26. u32 reg;
  27. ktime_t timeout;
  28. reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
  29. reg |= SDHCI_CLOCK_INT_EN;
  30. sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
  31. /* Wait max 20 ms */
  32. timeout = ktime_add_ms(ktime_get(), 20);
  33. while (!((reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  34. & SDHCI_CLOCK_INT_STABLE)) {
  35. if (ktime_after(ktime_get(), timeout)) {
  36. dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
  37. return -ETIMEDOUT;
  38. }
  39. usleep_range(900, 1100);
  40. }
  41. return 0;
  42. }
  43. /* Set SDCLK-off-while-idle */
  44. static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
  45. unsigned char sdhc_id, bool enable)
  46. {
  47. u32 reg;
  48. u32 mask;
  49. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  50. /* Get the bit shift basing on the SDHC index */
  51. mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
  52. if (enable)
  53. reg |= mask;
  54. else
  55. reg &= ~mask;
  56. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  57. }
  58. /* Enable/Disable the Auto Clock Gating function */
  59. static void xenon_set_acg(struct sdhci_host *host, bool enable)
  60. {
  61. u32 reg;
  62. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  63. if (enable)
  64. reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
  65. else
  66. reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
  67. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  68. }
  69. /* Enable this SDHC */
  70. static void xenon_enable_sdhc(struct sdhci_host *host,
  71. unsigned char sdhc_id)
  72. {
  73. u32 reg;
  74. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  75. reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
  76. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  77. host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  78. /*
  79. * Force to clear BUS_TEST to
  80. * skip bus_test_pre and bus_test_post
  81. */
  82. host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
  83. }
  84. /* Disable this SDHC */
  85. static void xenon_disable_sdhc(struct sdhci_host *host,
  86. unsigned char sdhc_id)
  87. {
  88. u32 reg;
  89. reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
  90. reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
  91. sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
  92. }
  93. /* Enable Parallel Transfer Mode */
  94. static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
  95. unsigned char sdhc_id)
  96. {
  97. u32 reg;
  98. reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
  99. reg |= BIT(sdhc_id);
  100. sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
  101. }
  102. /* Mask command conflict error */
  103. static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
  104. {
  105. u32 reg;
  106. reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
  107. reg |= XENON_MASK_CMD_CONFLICT_ERR;
  108. sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
  109. }
  110. static void xenon_retune_setup(struct sdhci_host *host)
  111. {
  112. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  113. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  114. u32 reg;
  115. /* Disable the Re-Tuning Request functionality */
  116. reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
  117. reg &= ~XENON_RETUNING_COMPATIBLE;
  118. sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
  119. /* Disable the Re-tuning Interrupt */
  120. reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
  121. reg &= ~SDHCI_INT_RETUNE;
  122. sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
  123. reg = sdhci_readl(host, SDHCI_INT_ENABLE);
  124. reg &= ~SDHCI_INT_RETUNE;
  125. sdhci_writel(host, reg, SDHCI_INT_ENABLE);
  126. /* Force to use Tuning Mode 1 */
  127. host->tuning_mode = SDHCI_TUNING_MODE_1;
  128. /* Set re-tuning period */
  129. host->tuning_count = 1 << (priv->tuning_count - 1);
  130. }
  131. /*
  132. * Operations inside struct sdhci_ops
  133. */
  134. /* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
  135. static void xenon_reset_exit(struct sdhci_host *host,
  136. unsigned char sdhc_id, u8 mask)
  137. {
  138. /* Only SOFTWARE RESET ALL will clear the register setting */
  139. if (!(mask & SDHCI_RESET_ALL))
  140. return;
  141. /* Disable tuning request and auto-retuning again */
  142. xenon_retune_setup(host);
  143. xenon_set_acg(host, true);
  144. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  145. xenon_mask_cmd_conflict_err(host);
  146. }
  147. static void xenon_reset(struct sdhci_host *host, u8 mask)
  148. {
  149. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  150. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  151. sdhci_reset(host, mask);
  152. xenon_reset_exit(host, priv->sdhc_id, mask);
  153. }
  154. /*
  155. * Xenon defines different values for HS200 and HS400
  156. * in Host_Control_2
  157. */
  158. static void xenon_set_uhs_signaling(struct sdhci_host *host,
  159. unsigned int timing)
  160. {
  161. u16 ctrl_2;
  162. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  163. /* Select Bus Speed Mode for host */
  164. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  165. if (timing == MMC_TIMING_MMC_HS200)
  166. ctrl_2 |= XENON_CTRL_HS200;
  167. else if (timing == MMC_TIMING_UHS_SDR104)
  168. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  169. else if (timing == MMC_TIMING_UHS_SDR12)
  170. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  171. else if (timing == MMC_TIMING_UHS_SDR25)
  172. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  173. else if (timing == MMC_TIMING_UHS_SDR50)
  174. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  175. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  176. (timing == MMC_TIMING_MMC_DDR52))
  177. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  178. else if (timing == MMC_TIMING_MMC_HS400)
  179. ctrl_2 |= XENON_CTRL_HS400;
  180. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  181. }
  182. static void xenon_set_power(struct sdhci_host *host, unsigned char mode,
  183. unsigned short vdd)
  184. {
  185. struct mmc_host *mmc = host->mmc;
  186. u8 pwr = host->pwr;
  187. sdhci_set_power_noreg(host, mode, vdd);
  188. if (host->pwr == pwr)
  189. return;
  190. if (host->pwr == 0)
  191. vdd = 0;
  192. if (!IS_ERR(mmc->supply.vmmc))
  193. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  194. }
  195. static void xenon_voltage_switch(struct sdhci_host *host)
  196. {
  197. /* Wait for 5ms after set 1.8V signal enable bit */
  198. usleep_range(5000, 5500);
  199. }
  200. static const struct sdhci_ops sdhci_xenon_ops = {
  201. .voltage_switch = xenon_voltage_switch,
  202. .set_clock = sdhci_set_clock,
  203. .set_power = xenon_set_power,
  204. .set_bus_width = sdhci_set_bus_width,
  205. .reset = xenon_reset,
  206. .set_uhs_signaling = xenon_set_uhs_signaling,
  207. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  208. };
  209. static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
  210. .ops = &sdhci_xenon_ops,
  211. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  212. SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  213. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  214. };
  215. /*
  216. * Xenon Specific Operations in mmc_host_ops
  217. */
  218. static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  219. {
  220. struct sdhci_host *host = mmc_priv(mmc);
  221. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  222. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  223. u32 reg;
  224. /*
  225. * HS400/HS200/eMMC HS doesn't have Preset Value register.
  226. * However, sdhci_set_ios will read HS400/HS200 Preset register.
  227. * Disable Preset Value register for HS400/HS200.
  228. * eMMC HS with preset_enabled set will trigger a bug in
  229. * get_preset_value().
  230. */
  231. if ((ios->timing == MMC_TIMING_MMC_HS400) ||
  232. (ios->timing == MMC_TIMING_MMC_HS200) ||
  233. (ios->timing == MMC_TIMING_MMC_HS)) {
  234. host->preset_enabled = false;
  235. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  236. host->flags &= ~SDHCI_PV_ENABLED;
  237. reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  238. reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  239. sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
  240. } else {
  241. host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  242. }
  243. sdhci_set_ios(mmc, ios);
  244. xenon_phy_adj(host, ios);
  245. if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
  246. xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
  247. }
  248. static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
  249. struct mmc_ios *ios)
  250. {
  251. struct sdhci_host *host = mmc_priv(mmc);
  252. /*
  253. * Before SD/SDIO set signal voltage, SD bus clock should be
  254. * disabled. However, sdhci_set_clock will also disable the Internal
  255. * clock in mmc_set_signal_voltage().
  256. * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
  257. * Thus here manually enable internal clock.
  258. *
  259. * After switch completes, it is unnecessary to disable internal clock,
  260. * since keeping internal clock active obeys SD spec.
  261. */
  262. xenon_enable_internal_clk(host);
  263. xenon_soc_pad_ctrl(host, ios->signal_voltage);
  264. /*
  265. * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
  266. * Thus SDHCI_CTRL_VDD_180 bit might not work then.
  267. * Skip the standard voltage switch to avoid any issue.
  268. */
  269. if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
  270. return 0;
  271. return sdhci_start_signal_voltage_switch(mmc, ios);
  272. }
  273. /*
  274. * Update card type.
  275. * priv->init_card_type will be used in PHY timing adjustment.
  276. */
  277. static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
  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. /* Update card type*/
  283. priv->init_card_type = card->type;
  284. }
  285. static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
  286. {
  287. struct sdhci_host *host = mmc_priv(mmc);
  288. if (host->timing == MMC_TIMING_UHS_DDR50 ||
  289. host->timing == MMC_TIMING_MMC_DDR52)
  290. return 0;
  291. /*
  292. * Currently force Xenon driver back to support mode 1 only,
  293. * even though Xenon might claim to support mode 2 or mode 3.
  294. * It requires more time to test mode 2/mode 3 on more platforms.
  295. */
  296. if (host->tuning_mode != SDHCI_TUNING_MODE_1)
  297. xenon_retune_setup(host);
  298. return sdhci_execute_tuning(mmc, opcode);
  299. }
  300. static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
  301. {
  302. struct sdhci_host *host = mmc_priv(mmc);
  303. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  304. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  305. u32 reg;
  306. u8 sdhc_id = priv->sdhc_id;
  307. sdhci_enable_sdio_irq(mmc, enable);
  308. if (enable) {
  309. /*
  310. * Set SDIO Card Inserted indication
  311. * to enable detecting SDIO async irq.
  312. */
  313. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  314. reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  315. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  316. } else {
  317. /* Clear SDIO Card Inserted indication */
  318. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  319. reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  320. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  321. }
  322. }
  323. static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
  324. {
  325. host->mmc_host_ops.set_ios = xenon_set_ios;
  326. host->mmc_host_ops.start_signal_voltage_switch =
  327. xenon_start_signal_voltage_switch;
  328. host->mmc_host_ops.init_card = xenon_init_card;
  329. host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
  330. host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
  331. }
  332. /*
  333. * Parse Xenon specific DT properties:
  334. * sdhc-id: the index of current SDHC.
  335. * Refer to XENON_SYS_CFG_INFO register
  336. * tun-count: the interval between re-tuning
  337. */
  338. static int xenon_probe_dt(struct platform_device *pdev)
  339. {
  340. struct device_node *np = pdev->dev.of_node;
  341. struct sdhci_host *host = platform_get_drvdata(pdev);
  342. struct mmc_host *mmc = host->mmc;
  343. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  344. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  345. u32 sdhc_id, nr_sdhc;
  346. u32 tuning_count;
  347. /* Disable HS200 on Armada AP806 */
  348. if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
  349. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  350. sdhc_id = 0x0;
  351. if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
  352. nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
  353. nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
  354. if (unlikely(sdhc_id > nr_sdhc)) {
  355. dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
  356. sdhc_id, nr_sdhc);
  357. return -EINVAL;
  358. }
  359. }
  360. priv->sdhc_id = sdhc_id;
  361. tuning_count = XENON_DEF_TUNING_COUNT;
  362. if (!of_property_read_u32(np, "marvell,xenon-tun-count",
  363. &tuning_count)) {
  364. if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
  365. dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
  366. XENON_DEF_TUNING_COUNT);
  367. tuning_count = XENON_DEF_TUNING_COUNT;
  368. }
  369. }
  370. priv->tuning_count = tuning_count;
  371. return xenon_phy_parse_dt(np, host);
  372. }
  373. static int xenon_sdhc_prepare(struct sdhci_host *host)
  374. {
  375. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  376. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  377. u8 sdhc_id = priv->sdhc_id;
  378. /* Enable SDHC */
  379. xenon_enable_sdhc(host, sdhc_id);
  380. /* Enable ACG */
  381. xenon_set_acg(host, true);
  382. /* Enable Parallel Transfer Mode */
  383. xenon_enable_sdhc_parallel_tran(host, sdhc_id);
  384. /* Disable SDCLK-Off-While-Idle before card init */
  385. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  386. xenon_mask_cmd_conflict_err(host);
  387. return 0;
  388. }
  389. static void xenon_sdhc_unprepare(struct sdhci_host *host)
  390. {
  391. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  392. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  393. u8 sdhc_id = priv->sdhc_id;
  394. /* disable SDHC */
  395. xenon_disable_sdhc(host, sdhc_id);
  396. }
  397. static int xenon_probe(struct platform_device *pdev)
  398. {
  399. struct sdhci_pltfm_host *pltfm_host;
  400. struct sdhci_host *host;
  401. struct xenon_priv *priv;
  402. int err;
  403. host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
  404. sizeof(struct xenon_priv));
  405. if (IS_ERR(host))
  406. return PTR_ERR(host);
  407. pltfm_host = sdhci_priv(host);
  408. priv = sdhci_pltfm_priv(pltfm_host);
  409. /*
  410. * Link Xenon specific mmc_host_ops function,
  411. * to replace standard ones in sdhci_ops.
  412. */
  413. xenon_replace_mmc_host_ops(host);
  414. pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
  415. if (IS_ERR(pltfm_host->clk)) {
  416. err = PTR_ERR(pltfm_host->clk);
  417. dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
  418. goto free_pltfm;
  419. }
  420. err = clk_prepare_enable(pltfm_host->clk);
  421. if (err)
  422. goto free_pltfm;
  423. priv->axi_clk = devm_clk_get(&pdev->dev, "axi");
  424. if (IS_ERR(priv->axi_clk)) {
  425. err = PTR_ERR(priv->axi_clk);
  426. if (err == -EPROBE_DEFER)
  427. goto err_clk;
  428. } else {
  429. err = clk_prepare_enable(priv->axi_clk);
  430. if (err)
  431. goto err_clk;
  432. }
  433. err = mmc_of_parse(host->mmc);
  434. if (err)
  435. goto err_clk_axi;
  436. sdhci_get_of_property(pdev);
  437. xenon_set_acg(host, false);
  438. /* Xenon specific dt parse */
  439. err = xenon_probe_dt(pdev);
  440. if (err)
  441. goto err_clk_axi;
  442. err = xenon_sdhc_prepare(host);
  443. if (err)
  444. goto err_clk_axi;
  445. pm_runtime_get_noresume(&pdev->dev);
  446. pm_runtime_set_active(&pdev->dev);
  447. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  448. pm_runtime_use_autosuspend(&pdev->dev);
  449. pm_runtime_enable(&pdev->dev);
  450. pm_suspend_ignore_children(&pdev->dev, 1);
  451. err = sdhci_add_host(host);
  452. if (err)
  453. goto remove_sdhc;
  454. pm_runtime_put_autosuspend(&pdev->dev);
  455. return 0;
  456. remove_sdhc:
  457. pm_runtime_disable(&pdev->dev);
  458. pm_runtime_put_noidle(&pdev->dev);
  459. xenon_sdhc_unprepare(host);
  460. err_clk_axi:
  461. clk_disable_unprepare(priv->axi_clk);
  462. err_clk:
  463. clk_disable_unprepare(pltfm_host->clk);
  464. free_pltfm:
  465. sdhci_pltfm_free(pdev);
  466. return err;
  467. }
  468. static int xenon_remove(struct platform_device *pdev)
  469. {
  470. struct sdhci_host *host = platform_get_drvdata(pdev);
  471. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  472. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  473. pm_runtime_get_sync(&pdev->dev);
  474. pm_runtime_disable(&pdev->dev);
  475. pm_runtime_put_noidle(&pdev->dev);
  476. sdhci_remove_host(host, 0);
  477. xenon_sdhc_unprepare(host);
  478. clk_disable_unprepare(priv->axi_clk);
  479. clk_disable_unprepare(pltfm_host->clk);
  480. sdhci_pltfm_free(pdev);
  481. return 0;
  482. }
  483. #ifdef CONFIG_PM_SLEEP
  484. static int xenon_suspend(struct device *dev)
  485. {
  486. struct sdhci_host *host = dev_get_drvdata(dev);
  487. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  488. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  489. int ret;
  490. ret = pm_runtime_force_suspend(dev);
  491. priv->restore_needed = true;
  492. return ret;
  493. }
  494. #endif
  495. #ifdef CONFIG_PM
  496. static int xenon_runtime_suspend(struct device *dev)
  497. {
  498. struct sdhci_host *host = dev_get_drvdata(dev);
  499. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  500. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  501. int ret;
  502. ret = sdhci_runtime_suspend_host(host);
  503. if (ret)
  504. return ret;
  505. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  506. mmc_retune_needed(host->mmc);
  507. clk_disable_unprepare(pltfm_host->clk);
  508. /*
  509. * Need to update the priv->clock here, or when runtime resume
  510. * back, phy don't aware the clock change and won't adjust phy
  511. * which will cause cmd err
  512. */
  513. priv->clock = 0;
  514. return 0;
  515. }
  516. static int xenon_runtime_resume(struct device *dev)
  517. {
  518. struct sdhci_host *host = dev_get_drvdata(dev);
  519. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  520. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  521. int ret;
  522. ret = clk_prepare_enable(pltfm_host->clk);
  523. if (ret) {
  524. dev_err(dev, "can't enable mainck\n");
  525. return ret;
  526. }
  527. if (priv->restore_needed) {
  528. ret = xenon_sdhc_prepare(host);
  529. if (ret)
  530. goto out;
  531. priv->restore_needed = false;
  532. }
  533. ret = sdhci_runtime_resume_host(host);
  534. if (ret)
  535. goto out;
  536. return 0;
  537. out:
  538. clk_disable_unprepare(pltfm_host->clk);
  539. return ret;
  540. }
  541. #endif /* CONFIG_PM */
  542. static const struct dev_pm_ops sdhci_xenon_dev_pm_ops = {
  543. SET_SYSTEM_SLEEP_PM_OPS(xenon_suspend,
  544. pm_runtime_force_resume)
  545. SET_RUNTIME_PM_OPS(xenon_runtime_suspend,
  546. xenon_runtime_resume,
  547. NULL)
  548. };
  549. static const struct of_device_id sdhci_xenon_dt_ids[] = {
  550. { .compatible = "marvell,armada-ap806-sdhci",},
  551. { .compatible = "marvell,armada-cp110-sdhci",},
  552. { .compatible = "marvell,armada-3700-sdhci",},
  553. {}
  554. };
  555. MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
  556. static struct platform_driver sdhci_xenon_driver = {
  557. .driver = {
  558. .name = "xenon-sdhci",
  559. .of_match_table = sdhci_xenon_dt_ids,
  560. .pm = &sdhci_xenon_dev_pm_ops,
  561. },
  562. .probe = xenon_probe,
  563. .remove = xenon_remove,
  564. };
  565. module_platform_driver(sdhci_xenon_driver);
  566. MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
  567. MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
  568. MODULE_LICENSE("GPL v2");