sdhci-xenon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 const struct sdhci_ops sdhci_xenon_ops = {
  196. .set_clock = sdhci_set_clock,
  197. .set_power = xenon_set_power,
  198. .set_bus_width = sdhci_set_bus_width,
  199. .reset = xenon_reset,
  200. .set_uhs_signaling = xenon_set_uhs_signaling,
  201. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  202. };
  203. static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
  204. .ops = &sdhci_xenon_ops,
  205. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  206. SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  207. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  208. };
  209. /*
  210. * Xenon Specific Operations in mmc_host_ops
  211. */
  212. static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  213. {
  214. struct sdhci_host *host = mmc_priv(mmc);
  215. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  216. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  217. u32 reg;
  218. /*
  219. * HS400/HS200/eMMC HS doesn't have Preset Value register.
  220. * However, sdhci_set_ios will read HS400/HS200 Preset register.
  221. * Disable Preset Value register for HS400/HS200.
  222. * eMMC HS with preset_enabled set will trigger a bug in
  223. * get_preset_value().
  224. */
  225. if ((ios->timing == MMC_TIMING_MMC_HS400) ||
  226. (ios->timing == MMC_TIMING_MMC_HS200) ||
  227. (ios->timing == MMC_TIMING_MMC_HS)) {
  228. host->preset_enabled = false;
  229. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  230. host->flags &= ~SDHCI_PV_ENABLED;
  231. reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  232. reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  233. sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
  234. } else {
  235. host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  236. }
  237. sdhci_set_ios(mmc, ios);
  238. xenon_phy_adj(host, ios);
  239. if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
  240. xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
  241. }
  242. static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
  243. struct mmc_ios *ios)
  244. {
  245. struct sdhci_host *host = mmc_priv(mmc);
  246. /*
  247. * Before SD/SDIO set signal voltage, SD bus clock should be
  248. * disabled. However, sdhci_set_clock will also disable the Internal
  249. * clock in mmc_set_signal_voltage().
  250. * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
  251. * Thus here manually enable internal clock.
  252. *
  253. * After switch completes, it is unnecessary to disable internal clock,
  254. * since keeping internal clock active obeys SD spec.
  255. */
  256. xenon_enable_internal_clk(host);
  257. xenon_soc_pad_ctrl(host, ios->signal_voltage);
  258. /*
  259. * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
  260. * Thus SDHCI_CTRL_VDD_180 bit might not work then.
  261. * Skip the standard voltage switch to avoid any issue.
  262. */
  263. if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
  264. return 0;
  265. return sdhci_start_signal_voltage_switch(mmc, ios);
  266. }
  267. /*
  268. * Update card type.
  269. * priv->init_card_type will be used in PHY timing adjustment.
  270. */
  271. static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
  272. {
  273. struct sdhci_host *host = mmc_priv(mmc);
  274. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  275. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  276. /* Update card type*/
  277. priv->init_card_type = card->type;
  278. }
  279. static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
  280. {
  281. struct sdhci_host *host = mmc_priv(mmc);
  282. if (host->timing == MMC_TIMING_UHS_DDR50 ||
  283. host->timing == MMC_TIMING_MMC_DDR52)
  284. return 0;
  285. /*
  286. * Currently force Xenon driver back to support mode 1 only,
  287. * even though Xenon might claim to support mode 2 or mode 3.
  288. * It requires more time to test mode 2/mode 3 on more platforms.
  289. */
  290. if (host->tuning_mode != SDHCI_TUNING_MODE_1)
  291. xenon_retune_setup(host);
  292. return sdhci_execute_tuning(mmc, opcode);
  293. }
  294. static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
  295. {
  296. struct sdhci_host *host = mmc_priv(mmc);
  297. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  298. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  299. u32 reg;
  300. u8 sdhc_id = priv->sdhc_id;
  301. sdhci_enable_sdio_irq(mmc, enable);
  302. if (enable) {
  303. /*
  304. * Set SDIO Card Inserted indication
  305. * to enable detecting SDIO async irq.
  306. */
  307. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  308. reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  309. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  310. } else {
  311. /* Clear SDIO Card Inserted indication */
  312. reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
  313. reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
  314. sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
  315. }
  316. }
  317. static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
  318. {
  319. host->mmc_host_ops.set_ios = xenon_set_ios;
  320. host->mmc_host_ops.start_signal_voltage_switch =
  321. xenon_start_signal_voltage_switch;
  322. host->mmc_host_ops.init_card = xenon_init_card;
  323. host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
  324. host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
  325. }
  326. /*
  327. * Parse Xenon specific DT properties:
  328. * sdhc-id: the index of current SDHC.
  329. * Refer to XENON_SYS_CFG_INFO register
  330. * tun-count: the interval between re-tuning
  331. */
  332. static int xenon_probe_dt(struct platform_device *pdev)
  333. {
  334. struct device_node *np = pdev->dev.of_node;
  335. struct sdhci_host *host = platform_get_drvdata(pdev);
  336. struct mmc_host *mmc = host->mmc;
  337. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  338. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  339. u32 sdhc_id, nr_sdhc;
  340. u32 tuning_count;
  341. /* Disable HS200 on Armada AP806 */
  342. if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
  343. host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
  344. sdhc_id = 0x0;
  345. if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
  346. nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
  347. nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
  348. if (unlikely(sdhc_id > nr_sdhc)) {
  349. dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
  350. sdhc_id, nr_sdhc);
  351. return -EINVAL;
  352. }
  353. }
  354. priv->sdhc_id = sdhc_id;
  355. tuning_count = XENON_DEF_TUNING_COUNT;
  356. if (!of_property_read_u32(np, "marvell,xenon-tun-count",
  357. &tuning_count)) {
  358. if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
  359. dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
  360. XENON_DEF_TUNING_COUNT);
  361. tuning_count = XENON_DEF_TUNING_COUNT;
  362. }
  363. }
  364. priv->tuning_count = tuning_count;
  365. return xenon_phy_parse_dt(np, host);
  366. }
  367. static int xenon_sdhc_prepare(struct sdhci_host *host)
  368. {
  369. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  370. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  371. u8 sdhc_id = priv->sdhc_id;
  372. /* Enable SDHC */
  373. xenon_enable_sdhc(host, sdhc_id);
  374. /* Enable ACG */
  375. xenon_set_acg(host, true);
  376. /* Enable Parallel Transfer Mode */
  377. xenon_enable_sdhc_parallel_tran(host, sdhc_id);
  378. /* Disable SDCLK-Off-While-Idle before card init */
  379. xenon_set_sdclk_off_idle(host, sdhc_id, false);
  380. xenon_mask_cmd_conflict_err(host);
  381. return 0;
  382. }
  383. static void xenon_sdhc_unprepare(struct sdhci_host *host)
  384. {
  385. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  386. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  387. u8 sdhc_id = priv->sdhc_id;
  388. /* disable SDHC */
  389. xenon_disable_sdhc(host, sdhc_id);
  390. }
  391. static int xenon_probe(struct platform_device *pdev)
  392. {
  393. struct sdhci_pltfm_host *pltfm_host;
  394. struct sdhci_host *host;
  395. struct xenon_priv *priv;
  396. int err;
  397. host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
  398. sizeof(struct xenon_priv));
  399. if (IS_ERR(host))
  400. return PTR_ERR(host);
  401. pltfm_host = sdhci_priv(host);
  402. priv = sdhci_pltfm_priv(pltfm_host);
  403. /*
  404. * Link Xenon specific mmc_host_ops function,
  405. * to replace standard ones in sdhci_ops.
  406. */
  407. xenon_replace_mmc_host_ops(host);
  408. pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
  409. if (IS_ERR(pltfm_host->clk)) {
  410. err = PTR_ERR(pltfm_host->clk);
  411. dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
  412. goto free_pltfm;
  413. }
  414. err = clk_prepare_enable(pltfm_host->clk);
  415. if (err)
  416. goto free_pltfm;
  417. priv->axi_clk = devm_clk_get(&pdev->dev, "axi");
  418. if (IS_ERR(priv->axi_clk)) {
  419. err = PTR_ERR(priv->axi_clk);
  420. if (err == -EPROBE_DEFER)
  421. goto err_clk;
  422. } else {
  423. err = clk_prepare_enable(priv->axi_clk);
  424. if (err)
  425. goto err_clk;
  426. }
  427. err = mmc_of_parse(host->mmc);
  428. if (err)
  429. goto err_clk_axi;
  430. sdhci_get_of_property(pdev);
  431. xenon_set_acg(host, false);
  432. /* Xenon specific dt parse */
  433. err = xenon_probe_dt(pdev);
  434. if (err)
  435. goto err_clk_axi;
  436. err = xenon_sdhc_prepare(host);
  437. if (err)
  438. goto err_clk_axi;
  439. pm_runtime_get_noresume(&pdev->dev);
  440. pm_runtime_set_active(&pdev->dev);
  441. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  442. pm_runtime_use_autosuspend(&pdev->dev);
  443. pm_runtime_enable(&pdev->dev);
  444. pm_suspend_ignore_children(&pdev->dev, 1);
  445. err = sdhci_add_host(host);
  446. if (err)
  447. goto remove_sdhc;
  448. pm_runtime_put_autosuspend(&pdev->dev);
  449. return 0;
  450. remove_sdhc:
  451. pm_runtime_disable(&pdev->dev);
  452. pm_runtime_put_noidle(&pdev->dev);
  453. xenon_sdhc_unprepare(host);
  454. err_clk_axi:
  455. clk_disable_unprepare(priv->axi_clk);
  456. err_clk:
  457. clk_disable_unprepare(pltfm_host->clk);
  458. free_pltfm:
  459. sdhci_pltfm_free(pdev);
  460. return err;
  461. }
  462. static int xenon_remove(struct platform_device *pdev)
  463. {
  464. struct sdhci_host *host = platform_get_drvdata(pdev);
  465. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  466. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  467. pm_runtime_get_sync(&pdev->dev);
  468. pm_runtime_disable(&pdev->dev);
  469. pm_runtime_put_noidle(&pdev->dev);
  470. sdhci_remove_host(host, 0);
  471. xenon_sdhc_unprepare(host);
  472. clk_disable_unprepare(priv->axi_clk);
  473. clk_disable_unprepare(pltfm_host->clk);
  474. sdhci_pltfm_free(pdev);
  475. return 0;
  476. }
  477. #ifdef CONFIG_PM_SLEEP
  478. static int xenon_suspend(struct device *dev)
  479. {
  480. struct sdhci_host *host = dev_get_drvdata(dev);
  481. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  482. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  483. int ret;
  484. ret = pm_runtime_force_suspend(dev);
  485. priv->restore_needed = true;
  486. return ret;
  487. }
  488. #endif
  489. #ifdef CONFIG_PM
  490. static int xenon_runtime_suspend(struct device *dev)
  491. {
  492. struct sdhci_host *host = dev_get_drvdata(dev);
  493. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  494. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  495. int ret;
  496. ret = sdhci_runtime_suspend_host(host);
  497. if (ret)
  498. return ret;
  499. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  500. mmc_retune_needed(host->mmc);
  501. clk_disable_unprepare(pltfm_host->clk);
  502. /*
  503. * Need to update the priv->clock here, or when runtime resume
  504. * back, phy don't aware the clock change and won't adjust phy
  505. * which will cause cmd err
  506. */
  507. priv->clock = 0;
  508. return 0;
  509. }
  510. static int xenon_runtime_resume(struct device *dev)
  511. {
  512. struct sdhci_host *host = dev_get_drvdata(dev);
  513. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  514. struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
  515. int ret;
  516. ret = clk_prepare_enable(pltfm_host->clk);
  517. if (ret) {
  518. dev_err(dev, "can't enable mainck\n");
  519. return ret;
  520. }
  521. if (priv->restore_needed) {
  522. ret = xenon_sdhc_prepare(host);
  523. if (ret)
  524. goto out;
  525. priv->restore_needed = false;
  526. }
  527. ret = sdhci_runtime_resume_host(host);
  528. if (ret)
  529. goto out;
  530. return 0;
  531. out:
  532. clk_disable_unprepare(pltfm_host->clk);
  533. return ret;
  534. }
  535. #endif /* CONFIG_PM */
  536. static const struct dev_pm_ops sdhci_xenon_dev_pm_ops = {
  537. SET_SYSTEM_SLEEP_PM_OPS(xenon_suspend,
  538. pm_runtime_force_resume)
  539. SET_RUNTIME_PM_OPS(xenon_runtime_suspend,
  540. xenon_runtime_resume,
  541. NULL)
  542. };
  543. static const struct of_device_id sdhci_xenon_dt_ids[] = {
  544. { .compatible = "marvell,armada-ap806-sdhci",},
  545. { .compatible = "marvell,armada-cp110-sdhci",},
  546. { .compatible = "marvell,armada-3700-sdhci",},
  547. {}
  548. };
  549. MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
  550. static struct platform_driver sdhci_xenon_driver = {
  551. .driver = {
  552. .name = "xenon-sdhci",
  553. .of_match_table = sdhci_xenon_dt_ids,
  554. .pm = &sdhci_xenon_dev_pm_ops,
  555. },
  556. .probe = xenon_probe,
  557. .remove = xenon_remove,
  558. };
  559. module_platform_driver(sdhci_xenon_driver);
  560. MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
  561. MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
  562. MODULE_LICENSE("GPL v2");