sdhci-msm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * drivers/mmc/host/sdhci-msm.c - Qualcomm SDHCI Platform driver
  3. *
  4. * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/delay.h>
  20. #include <linux/mmc/mmc.h>
  21. #include <linux/slab.h>
  22. #include "sdhci-pltfm.h"
  23. #define CORE_HC_MODE 0x78
  24. #define HC_MODE_EN 0x1
  25. #define CORE_POWER 0x0
  26. #define CORE_SW_RST BIT(7)
  27. #define MAX_PHASES 16
  28. #define CORE_DLL_LOCK BIT(7)
  29. #define CORE_DLL_EN BIT(16)
  30. #define CORE_CDR_EN BIT(17)
  31. #define CORE_CK_OUT_EN BIT(18)
  32. #define CORE_CDR_EXT_EN BIT(19)
  33. #define CORE_DLL_PDN BIT(29)
  34. #define CORE_DLL_RST BIT(30)
  35. #define CORE_DLL_CONFIG 0x100
  36. #define CORE_DLL_STATUS 0x108
  37. #define CORE_VENDOR_SPEC 0x10c
  38. #define CORE_CLK_PWRSAVE BIT(1)
  39. #define CDR_SELEXT_SHIFT 20
  40. #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
  41. #define CMUX_SHIFT_PHASE_SHIFT 24
  42. #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
  43. static const u32 tuning_block_64[] = {
  44. 0x00ff0fff, 0xccc3ccff, 0xffcc3cc3, 0xeffefffe,
  45. 0xddffdfff, 0xfbfffbff, 0xff7fffbf, 0xefbdf777,
  46. 0xf0fff0ff, 0x3cccfc0f, 0xcfcc33cc, 0xeeffefff,
  47. 0xfdfffdff, 0xffbfffdf, 0xfff7ffbb, 0xde7b7ff7
  48. };
  49. static const u32 tuning_block_128[] = {
  50. 0xff00ffff, 0x0000ffff, 0xccccffff, 0xcccc33cc,
  51. 0xcc3333cc, 0xffffcccc, 0xffffeeff, 0xffeeeeff,
  52. 0xffddffff, 0xddddffff, 0xbbffffff, 0xbbffffff,
  53. 0xffffffbb, 0xffffff77, 0x77ff7777, 0xffeeddbb,
  54. 0x00ffffff, 0x00ffffff, 0xccffff00, 0xcc33cccc,
  55. 0x3333cccc, 0xffcccccc, 0xffeeffff, 0xeeeeffff,
  56. 0xddffffff, 0xddffffff, 0xffffffdd, 0xffffffbb,
  57. 0xffffbbbb, 0xffff77ff, 0xff7777ff, 0xeeddbb77
  58. };
  59. struct sdhci_msm_host {
  60. struct platform_device *pdev;
  61. void __iomem *core_mem; /* MSM SDCC mapped address */
  62. struct clk *clk; /* main SD/MMC bus clock */
  63. struct clk *pclk; /* SDHC peripheral bus clock */
  64. struct clk *bus_clk; /* SDHC bus voter clock */
  65. struct mmc_host *mmc;
  66. struct sdhci_pltfm_data sdhci_msm_pdata;
  67. };
  68. /* Platform specific tuning */
  69. static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
  70. {
  71. u32 wait_cnt = 50;
  72. u8 ck_out_en;
  73. struct mmc_host *mmc = host->mmc;
  74. /* Poll for CK_OUT_EN bit. max. poll time = 50us */
  75. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  76. CORE_CK_OUT_EN);
  77. while (ck_out_en != poll) {
  78. if (--wait_cnt == 0) {
  79. dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
  80. mmc_hostname(mmc), poll);
  81. return -ETIMEDOUT;
  82. }
  83. udelay(1);
  84. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  85. CORE_CK_OUT_EN);
  86. }
  87. return 0;
  88. }
  89. static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  90. {
  91. int rc;
  92. static const u8 grey_coded_phase_table[] = {
  93. 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  94. 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
  95. };
  96. unsigned long flags;
  97. u32 config;
  98. struct mmc_host *mmc = host->mmc;
  99. spin_lock_irqsave(&host->lock, flags);
  100. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  101. config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  102. config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  103. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  104. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  105. rc = msm_dll_poll_ck_out_en(host, 0);
  106. if (rc)
  107. goto err_out;
  108. /*
  109. * Write the selected DLL clock output phase (0 ... 15)
  110. * to CDR_SELEXT bit field of DLL_CONFIG register.
  111. */
  112. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  113. config &= ~CDR_SELEXT_MASK;
  114. config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
  115. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  116. /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
  117. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  118. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  119. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  120. rc = msm_dll_poll_ck_out_en(host, 1);
  121. if (rc)
  122. goto err_out;
  123. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  124. config |= CORE_CDR_EN;
  125. config &= ~CORE_CDR_EXT_EN;
  126. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  127. goto out;
  128. err_out:
  129. dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
  130. mmc_hostname(mmc), phase);
  131. out:
  132. spin_unlock_irqrestore(&host->lock, flags);
  133. return rc;
  134. }
  135. /*
  136. * Find out the greatest range of consecuitive selected
  137. * DLL clock output phases that can be used as sampling
  138. * setting for SD3.0 UHS-I card read operation (in SDR104
  139. * timing mode) or for eMMC4.5 card read operation (in HS200
  140. * timing mode).
  141. * Select the 3/4 of the range and configure the DLL with the
  142. * selected DLL clock output phase.
  143. */
  144. static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  145. u8 *phase_table, u8 total_phases)
  146. {
  147. int ret;
  148. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  149. u8 phases_per_row[MAX_PHASES] = { 0 };
  150. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  151. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  152. bool phase_0_found = false, phase_15_found = false;
  153. struct mmc_host *mmc = host->mmc;
  154. if (!total_phases || (total_phases > MAX_PHASES)) {
  155. dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
  156. mmc_hostname(mmc), total_phases);
  157. return -EINVAL;
  158. }
  159. for (cnt = 0; cnt < total_phases; cnt++) {
  160. ranges[row_index][col_index] = phase_table[cnt];
  161. phases_per_row[row_index] += 1;
  162. col_index++;
  163. if ((cnt + 1) == total_phases) {
  164. continue;
  165. /* check if next phase in phase_table is consecutive or not */
  166. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  167. row_index++;
  168. col_index = 0;
  169. }
  170. }
  171. if (row_index >= MAX_PHASES)
  172. return -EINVAL;
  173. /* Check if phase-0 is present in first valid window? */
  174. if (!ranges[0][0]) {
  175. phase_0_found = true;
  176. phase_0_raw_index = 0;
  177. /* Check if cycle exist between 2 valid windows */
  178. for (cnt = 1; cnt <= row_index; cnt++) {
  179. if (phases_per_row[cnt]) {
  180. for (i = 0; i < phases_per_row[cnt]; i++) {
  181. if (ranges[cnt][i] == 15) {
  182. phase_15_found = true;
  183. phase_15_raw_index = cnt;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. /* If 2 valid windows form cycle then merge them as single window */
  191. if (phase_0_found && phase_15_found) {
  192. /* number of phases in raw where phase 0 is present */
  193. u8 phases_0 = phases_per_row[phase_0_raw_index];
  194. /* number of phases in raw where phase 15 is present */
  195. u8 phases_15 = phases_per_row[phase_15_raw_index];
  196. if (phases_0 + phases_15 >= MAX_PHASES)
  197. /*
  198. * If there are more than 1 phase windows then total
  199. * number of phases in both the windows should not be
  200. * more than or equal to MAX_PHASES.
  201. */
  202. return -EINVAL;
  203. /* Merge 2 cyclic windows */
  204. i = phases_15;
  205. for (cnt = 0; cnt < phases_0; cnt++) {
  206. ranges[phase_15_raw_index][i] =
  207. ranges[phase_0_raw_index][cnt];
  208. if (++i >= MAX_PHASES)
  209. break;
  210. }
  211. phases_per_row[phase_0_raw_index] = 0;
  212. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  213. }
  214. for (cnt = 0; cnt <= row_index; cnt++) {
  215. if (phases_per_row[cnt] > curr_max) {
  216. curr_max = phases_per_row[cnt];
  217. selected_row_index = cnt;
  218. }
  219. }
  220. i = (curr_max * 3) / 4;
  221. if (i)
  222. i--;
  223. ret = ranges[selected_row_index][i];
  224. if (ret >= MAX_PHASES) {
  225. ret = -EINVAL;
  226. dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
  227. mmc_hostname(mmc), ret);
  228. }
  229. return ret;
  230. }
  231. static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  232. {
  233. u32 mclk_freq = 0, config;
  234. /* Program the MCLK value to MCLK_FREQ bit field */
  235. if (host->clock <= 112000000)
  236. mclk_freq = 0;
  237. else if (host->clock <= 125000000)
  238. mclk_freq = 1;
  239. else if (host->clock <= 137000000)
  240. mclk_freq = 2;
  241. else if (host->clock <= 150000000)
  242. mclk_freq = 3;
  243. else if (host->clock <= 162000000)
  244. mclk_freq = 4;
  245. else if (host->clock <= 175000000)
  246. mclk_freq = 5;
  247. else if (host->clock <= 187000000)
  248. mclk_freq = 6;
  249. else if (host->clock <= 200000000)
  250. mclk_freq = 7;
  251. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  252. config &= ~CMUX_SHIFT_PHASE_MASK;
  253. config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
  254. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  255. }
  256. /* Initialize the DLL (Programmable Delay Line) */
  257. static int msm_init_cm_dll(struct sdhci_host *host)
  258. {
  259. struct mmc_host *mmc = host->mmc;
  260. int wait_cnt = 50;
  261. unsigned long flags;
  262. spin_lock_irqsave(&host->lock, flags);
  263. /*
  264. * Make sure that clock is always enabled when DLL
  265. * tuning is in progress. Keeping PWRSAVE ON may
  266. * turn off the clock.
  267. */
  268. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  269. & ~CORE_CLK_PWRSAVE), host->ioaddr + CORE_VENDOR_SPEC);
  270. /* Write 1 to DLL_RST bit of DLL_CONFIG register */
  271. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  272. | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  273. /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  274. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  275. | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  276. msm_cm_dll_set_freq(host);
  277. /* Write 0 to DLL_RST bit of DLL_CONFIG register */
  278. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  279. & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  280. /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
  281. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  282. & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  283. /* Set DLL_EN bit to 1. */
  284. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  285. | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
  286. /* Set CK_OUT_EN bit to 1. */
  287. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  288. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  289. /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  290. while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
  291. CORE_DLL_LOCK)) {
  292. /* max. wait for 50us sec for LOCK bit to be set */
  293. if (--wait_cnt == 0) {
  294. dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
  295. mmc_hostname(mmc));
  296. spin_unlock_irqrestore(&host->lock, flags);
  297. return -ETIMEDOUT;
  298. }
  299. udelay(1);
  300. }
  301. spin_unlock_irqrestore(&host->lock, flags);
  302. return 0;
  303. }
  304. static int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
  305. {
  306. int tuning_seq_cnt = 3;
  307. u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
  308. const u32 *tuning_block_pattern = tuning_block_64;
  309. int size = sizeof(tuning_block_64); /* Pattern size in bytes */
  310. int rc;
  311. struct mmc_host *mmc = host->mmc;
  312. struct mmc_ios ios = host->mmc->ios;
  313. /*
  314. * Tuning is required for SDR104, HS200 and HS400 cards and
  315. * if clock frequency is greater than 100MHz in these modes.
  316. */
  317. if (host->clock <= 100 * 1000 * 1000 ||
  318. !((ios.timing == MMC_TIMING_MMC_HS200) ||
  319. (ios.timing == MMC_TIMING_UHS_SDR104)))
  320. return 0;
  321. if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
  322. (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
  323. tuning_block_pattern = tuning_block_128;
  324. size = sizeof(tuning_block_128);
  325. }
  326. data_buf = kmalloc(size, GFP_KERNEL);
  327. if (!data_buf)
  328. return -ENOMEM;
  329. retry:
  330. /* First of all reset the tuning block */
  331. rc = msm_init_cm_dll(host);
  332. if (rc)
  333. goto out;
  334. phase = 0;
  335. do {
  336. struct mmc_command cmd = { 0 };
  337. struct mmc_data data = { 0 };
  338. struct mmc_request mrq = {
  339. .cmd = &cmd,
  340. .data = &data
  341. };
  342. struct scatterlist sg;
  343. /* Set the phase in delay line hw block */
  344. rc = msm_config_cm_dll_phase(host, phase);
  345. if (rc)
  346. goto out;
  347. cmd.opcode = opcode;
  348. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  349. data.blksz = size;
  350. data.blocks = 1;
  351. data.flags = MMC_DATA_READ;
  352. data.timeout_ns = NSEC_PER_SEC; /* 1 second */
  353. data.sg = &sg;
  354. data.sg_len = 1;
  355. sg_init_one(&sg, data_buf, size);
  356. memset(data_buf, 0, size);
  357. mmc_wait_for_req(mmc, &mrq);
  358. if (!cmd.error && !data.error &&
  359. !memcmp(data_buf, tuning_block_pattern, size)) {
  360. /* Tuning is successful at this tuning point */
  361. tuned_phases[tuned_phase_cnt++] = phase;
  362. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  363. mmc_hostname(mmc), phase);
  364. }
  365. } while (++phase < ARRAY_SIZE(tuned_phases));
  366. if (tuned_phase_cnt) {
  367. rc = msm_find_most_appropriate_phase(host, tuned_phases,
  368. tuned_phase_cnt);
  369. if (rc < 0)
  370. goto out;
  371. else
  372. phase = rc;
  373. /*
  374. * Finally set the selected phase in delay
  375. * line hw block.
  376. */
  377. rc = msm_config_cm_dll_phase(host, phase);
  378. if (rc)
  379. goto out;
  380. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  381. mmc_hostname(mmc), phase);
  382. } else {
  383. if (--tuning_seq_cnt)
  384. goto retry;
  385. /* Tuning failed */
  386. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  387. mmc_hostname(mmc));
  388. rc = -EIO;
  389. }
  390. out:
  391. kfree(data_buf);
  392. return rc;
  393. }
  394. static const struct of_device_id sdhci_msm_dt_match[] = {
  395. { .compatible = "qcom,sdhci-msm-v4" },
  396. {},
  397. };
  398. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  399. static struct sdhci_ops sdhci_msm_ops = {
  400. .platform_execute_tuning = sdhci_msm_execute_tuning,
  401. .reset = sdhci_reset,
  402. .set_clock = sdhci_set_clock,
  403. .set_bus_width = sdhci_set_bus_width,
  404. .set_uhs_signaling = sdhci_set_uhs_signaling,
  405. };
  406. static int sdhci_msm_probe(struct platform_device *pdev)
  407. {
  408. struct sdhci_host *host;
  409. struct sdhci_pltfm_host *pltfm_host;
  410. struct sdhci_msm_host *msm_host;
  411. struct resource *core_memres;
  412. int ret;
  413. u16 host_version;
  414. msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
  415. if (!msm_host)
  416. return -ENOMEM;
  417. msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
  418. host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
  419. if (IS_ERR(host))
  420. return PTR_ERR(host);
  421. pltfm_host = sdhci_priv(host);
  422. pltfm_host->priv = msm_host;
  423. msm_host->mmc = host->mmc;
  424. msm_host->pdev = pdev;
  425. ret = mmc_of_parse(host->mmc);
  426. if (ret)
  427. goto pltfm_free;
  428. sdhci_get_of_property(pdev);
  429. /* Setup SDCC bus voter clock. */
  430. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
  431. if (!IS_ERR(msm_host->bus_clk)) {
  432. /* Vote for max. clk rate for max. performance */
  433. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  434. if (ret)
  435. goto pltfm_free;
  436. ret = clk_prepare_enable(msm_host->bus_clk);
  437. if (ret)
  438. goto pltfm_free;
  439. }
  440. /* Setup main peripheral bus clock */
  441. msm_host->pclk = devm_clk_get(&pdev->dev, "iface");
  442. if (IS_ERR(msm_host->pclk)) {
  443. ret = PTR_ERR(msm_host->pclk);
  444. dev_err(&pdev->dev, "Perpheral clk setup failed (%d)\n", ret);
  445. goto bus_clk_disable;
  446. }
  447. ret = clk_prepare_enable(msm_host->pclk);
  448. if (ret)
  449. goto bus_clk_disable;
  450. /* Setup SDC MMC clock */
  451. msm_host->clk = devm_clk_get(&pdev->dev, "core");
  452. if (IS_ERR(msm_host->clk)) {
  453. ret = PTR_ERR(msm_host->clk);
  454. dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
  455. goto pclk_disable;
  456. }
  457. ret = clk_prepare_enable(msm_host->clk);
  458. if (ret)
  459. goto pclk_disable;
  460. core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  461. msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres);
  462. if (IS_ERR(msm_host->core_mem)) {
  463. dev_err(&pdev->dev, "Failed to remap registers\n");
  464. ret = PTR_ERR(msm_host->core_mem);
  465. goto clk_disable;
  466. }
  467. /* Reset the core and Enable SDHC mode */
  468. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
  469. CORE_SW_RST, msm_host->core_mem + CORE_POWER);
  470. /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
  471. usleep_range(1000, 5000);
  472. if (readl(msm_host->core_mem + CORE_POWER) & CORE_SW_RST) {
  473. dev_err(&pdev->dev, "Stuck in reset\n");
  474. ret = -ETIMEDOUT;
  475. goto clk_disable;
  476. }
  477. /* Set HC_MODE_EN bit in HC_MODE register */
  478. writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
  479. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  480. host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
  481. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  482. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  483. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  484. SDHCI_VENDOR_VER_SHIFT));
  485. ret = sdhci_add_host(host);
  486. if (ret)
  487. goto clk_disable;
  488. return 0;
  489. clk_disable:
  490. clk_disable_unprepare(msm_host->clk);
  491. pclk_disable:
  492. clk_disable_unprepare(msm_host->pclk);
  493. bus_clk_disable:
  494. if (!IS_ERR(msm_host->bus_clk))
  495. clk_disable_unprepare(msm_host->bus_clk);
  496. pltfm_free:
  497. sdhci_pltfm_free(pdev);
  498. return ret;
  499. }
  500. static int sdhci_msm_remove(struct platform_device *pdev)
  501. {
  502. struct sdhci_host *host = platform_get_drvdata(pdev);
  503. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  504. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  505. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  506. 0xffffffff);
  507. sdhci_remove_host(host, dead);
  508. sdhci_pltfm_free(pdev);
  509. clk_disable_unprepare(msm_host->clk);
  510. clk_disable_unprepare(msm_host->pclk);
  511. if (!IS_ERR(msm_host->bus_clk))
  512. clk_disable_unprepare(msm_host->bus_clk);
  513. return 0;
  514. }
  515. static struct platform_driver sdhci_msm_driver = {
  516. .probe = sdhci_msm_probe,
  517. .remove = sdhci_msm_remove,
  518. .driver = {
  519. .name = "sdhci_msm",
  520. .owner = THIS_MODULE,
  521. .of_match_table = sdhci_msm_dt_match,
  522. },
  523. };
  524. module_platform_driver(sdhci_msm_driver);
  525. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  526. MODULE_LICENSE("GPL v2");