sdhci-msm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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/delay.h>
  19. #include <linux/mmc/mmc.h>
  20. #include <linux/slab.h>
  21. #include "sdhci-pltfm.h"
  22. #define CORE_MCI_VERSION 0x50
  23. #define CORE_VERSION_MAJOR_SHIFT 28
  24. #define CORE_VERSION_MAJOR_MASK (0xf << CORE_VERSION_MAJOR_SHIFT)
  25. #define CORE_VERSION_MINOR_MASK 0xff
  26. #define CORE_HC_MODE 0x78
  27. #define HC_MODE_EN 0x1
  28. #define CORE_POWER 0x0
  29. #define CORE_SW_RST BIT(7)
  30. #define CORE_PWRCTL_STATUS 0xdc
  31. #define CORE_PWRCTL_MASK 0xe0
  32. #define CORE_PWRCTL_CLEAR 0xe4
  33. #define CORE_PWRCTL_CTL 0xe8
  34. #define CORE_PWRCTL_BUS_OFF BIT(0)
  35. #define CORE_PWRCTL_BUS_ON BIT(1)
  36. #define CORE_PWRCTL_IO_LOW BIT(2)
  37. #define CORE_PWRCTL_IO_HIGH BIT(3)
  38. #define CORE_PWRCTL_BUS_SUCCESS BIT(0)
  39. #define CORE_PWRCTL_IO_SUCCESS BIT(2)
  40. #define REQ_BUS_OFF BIT(0)
  41. #define REQ_BUS_ON BIT(1)
  42. #define REQ_IO_LOW BIT(2)
  43. #define REQ_IO_HIGH BIT(3)
  44. #define INT_MASK 0xf
  45. #define MAX_PHASES 16
  46. #define CORE_DLL_LOCK BIT(7)
  47. #define CORE_DLL_EN BIT(16)
  48. #define CORE_CDR_EN BIT(17)
  49. #define CORE_CK_OUT_EN BIT(18)
  50. #define CORE_CDR_EXT_EN BIT(19)
  51. #define CORE_DLL_PDN BIT(29)
  52. #define CORE_DLL_RST BIT(30)
  53. #define CORE_DLL_CONFIG 0x100
  54. #define CORE_DLL_STATUS 0x108
  55. #define CORE_VENDOR_SPEC 0x10c
  56. #define CORE_CLK_PWRSAVE BIT(1)
  57. #define CORE_VENDOR_SPEC_CAPABILITIES0 0x11c
  58. #define CDR_SELEXT_SHIFT 20
  59. #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
  60. #define CMUX_SHIFT_PHASE_SHIFT 24
  61. #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
  62. struct sdhci_msm_host {
  63. struct platform_device *pdev;
  64. void __iomem *core_mem; /* MSM SDCC mapped address */
  65. int pwr_irq; /* power irq */
  66. struct clk *clk; /* main SD/MMC bus clock */
  67. struct clk *pclk; /* SDHC peripheral bus clock */
  68. struct clk *bus_clk; /* SDHC bus voter clock */
  69. struct mmc_host *mmc;
  70. };
  71. /* Platform specific tuning */
  72. static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
  73. {
  74. u32 wait_cnt = 50;
  75. u8 ck_out_en;
  76. struct mmc_host *mmc = host->mmc;
  77. /* Poll for CK_OUT_EN bit. max. poll time = 50us */
  78. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  79. CORE_CK_OUT_EN);
  80. while (ck_out_en != poll) {
  81. if (--wait_cnt == 0) {
  82. dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
  83. mmc_hostname(mmc), poll);
  84. return -ETIMEDOUT;
  85. }
  86. udelay(1);
  87. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  88. CORE_CK_OUT_EN);
  89. }
  90. return 0;
  91. }
  92. static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  93. {
  94. int rc;
  95. static const u8 grey_coded_phase_table[] = {
  96. 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  97. 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
  98. };
  99. unsigned long flags;
  100. u32 config;
  101. struct mmc_host *mmc = host->mmc;
  102. spin_lock_irqsave(&host->lock, flags);
  103. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  104. config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  105. config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  106. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  107. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  108. rc = msm_dll_poll_ck_out_en(host, 0);
  109. if (rc)
  110. goto err_out;
  111. /*
  112. * Write the selected DLL clock output phase (0 ... 15)
  113. * to CDR_SELEXT bit field of DLL_CONFIG register.
  114. */
  115. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  116. config &= ~CDR_SELEXT_MASK;
  117. config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
  118. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  119. /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
  120. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  121. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  122. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  123. rc = msm_dll_poll_ck_out_en(host, 1);
  124. if (rc)
  125. goto err_out;
  126. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  127. config |= CORE_CDR_EN;
  128. config &= ~CORE_CDR_EXT_EN;
  129. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  130. goto out;
  131. err_out:
  132. dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
  133. mmc_hostname(mmc), phase);
  134. out:
  135. spin_unlock_irqrestore(&host->lock, flags);
  136. return rc;
  137. }
  138. /*
  139. * Find out the greatest range of consecuitive selected
  140. * DLL clock output phases that can be used as sampling
  141. * setting for SD3.0 UHS-I card read operation (in SDR104
  142. * timing mode) or for eMMC4.5 card read operation (in HS200
  143. * timing mode).
  144. * Select the 3/4 of the range and configure the DLL with the
  145. * selected DLL clock output phase.
  146. */
  147. static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  148. u8 *phase_table, u8 total_phases)
  149. {
  150. int ret;
  151. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  152. u8 phases_per_row[MAX_PHASES] = { 0 };
  153. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  154. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  155. bool phase_0_found = false, phase_15_found = false;
  156. struct mmc_host *mmc = host->mmc;
  157. if (!total_phases || (total_phases > MAX_PHASES)) {
  158. dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
  159. mmc_hostname(mmc), total_phases);
  160. return -EINVAL;
  161. }
  162. for (cnt = 0; cnt < total_phases; cnt++) {
  163. ranges[row_index][col_index] = phase_table[cnt];
  164. phases_per_row[row_index] += 1;
  165. col_index++;
  166. if ((cnt + 1) == total_phases) {
  167. continue;
  168. /* check if next phase in phase_table is consecutive or not */
  169. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  170. row_index++;
  171. col_index = 0;
  172. }
  173. }
  174. if (row_index >= MAX_PHASES)
  175. return -EINVAL;
  176. /* Check if phase-0 is present in first valid window? */
  177. if (!ranges[0][0]) {
  178. phase_0_found = true;
  179. phase_0_raw_index = 0;
  180. /* Check if cycle exist between 2 valid windows */
  181. for (cnt = 1; cnt <= row_index; cnt++) {
  182. if (phases_per_row[cnt]) {
  183. for (i = 0; i < phases_per_row[cnt]; i++) {
  184. if (ranges[cnt][i] == 15) {
  185. phase_15_found = true;
  186. phase_15_raw_index = cnt;
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. /* If 2 valid windows form cycle then merge them as single window */
  194. if (phase_0_found && phase_15_found) {
  195. /* number of phases in raw where phase 0 is present */
  196. u8 phases_0 = phases_per_row[phase_0_raw_index];
  197. /* number of phases in raw where phase 15 is present */
  198. u8 phases_15 = phases_per_row[phase_15_raw_index];
  199. if (phases_0 + phases_15 >= MAX_PHASES)
  200. /*
  201. * If there are more than 1 phase windows then total
  202. * number of phases in both the windows should not be
  203. * more than or equal to MAX_PHASES.
  204. */
  205. return -EINVAL;
  206. /* Merge 2 cyclic windows */
  207. i = phases_15;
  208. for (cnt = 0; cnt < phases_0; cnt++) {
  209. ranges[phase_15_raw_index][i] =
  210. ranges[phase_0_raw_index][cnt];
  211. if (++i >= MAX_PHASES)
  212. break;
  213. }
  214. phases_per_row[phase_0_raw_index] = 0;
  215. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  216. }
  217. for (cnt = 0; cnt <= row_index; cnt++) {
  218. if (phases_per_row[cnt] > curr_max) {
  219. curr_max = phases_per_row[cnt];
  220. selected_row_index = cnt;
  221. }
  222. }
  223. i = (curr_max * 3) / 4;
  224. if (i)
  225. i--;
  226. ret = ranges[selected_row_index][i];
  227. if (ret >= MAX_PHASES) {
  228. ret = -EINVAL;
  229. dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
  230. mmc_hostname(mmc), ret);
  231. }
  232. return ret;
  233. }
  234. static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  235. {
  236. u32 mclk_freq = 0, config;
  237. /* Program the MCLK value to MCLK_FREQ bit field */
  238. if (host->clock <= 112000000)
  239. mclk_freq = 0;
  240. else if (host->clock <= 125000000)
  241. mclk_freq = 1;
  242. else if (host->clock <= 137000000)
  243. mclk_freq = 2;
  244. else if (host->clock <= 150000000)
  245. mclk_freq = 3;
  246. else if (host->clock <= 162000000)
  247. mclk_freq = 4;
  248. else if (host->clock <= 175000000)
  249. mclk_freq = 5;
  250. else if (host->clock <= 187000000)
  251. mclk_freq = 6;
  252. else if (host->clock <= 200000000)
  253. mclk_freq = 7;
  254. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  255. config &= ~CMUX_SHIFT_PHASE_MASK;
  256. config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
  257. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  258. }
  259. /* Initialize the DLL (Programmable Delay Line) */
  260. static int msm_init_cm_dll(struct sdhci_host *host)
  261. {
  262. struct mmc_host *mmc = host->mmc;
  263. int wait_cnt = 50;
  264. unsigned long flags;
  265. spin_lock_irqsave(&host->lock, flags);
  266. /*
  267. * Make sure that clock is always enabled when DLL
  268. * tuning is in progress. Keeping PWRSAVE ON may
  269. * turn off the clock.
  270. */
  271. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  272. & ~CORE_CLK_PWRSAVE), host->ioaddr + CORE_VENDOR_SPEC);
  273. /* Write 1 to DLL_RST bit of DLL_CONFIG register */
  274. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  275. | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  276. /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  277. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  278. | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  279. msm_cm_dll_set_freq(host);
  280. /* Write 0 to DLL_RST bit of DLL_CONFIG register */
  281. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  282. & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  283. /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
  284. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  285. & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  286. /* Set DLL_EN bit to 1. */
  287. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  288. | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
  289. /* Set CK_OUT_EN bit to 1. */
  290. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  291. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  292. /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  293. while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
  294. CORE_DLL_LOCK)) {
  295. /* max. wait for 50us sec for LOCK bit to be set */
  296. if (--wait_cnt == 0) {
  297. dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
  298. mmc_hostname(mmc));
  299. spin_unlock_irqrestore(&host->lock, flags);
  300. return -ETIMEDOUT;
  301. }
  302. udelay(1);
  303. }
  304. spin_unlock_irqrestore(&host->lock, flags);
  305. return 0;
  306. }
  307. static int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
  308. {
  309. int tuning_seq_cnt = 3;
  310. u8 phase, tuned_phases[16], tuned_phase_cnt = 0;
  311. int rc;
  312. struct mmc_host *mmc = host->mmc;
  313. struct mmc_ios ios = host->mmc->ios;
  314. /*
  315. * Tuning is required for SDR104, HS200 and HS400 cards and
  316. * if clock frequency is greater than 100MHz in these modes.
  317. */
  318. if (host->clock <= 100 * 1000 * 1000 ||
  319. !((ios.timing == MMC_TIMING_MMC_HS200) ||
  320. (ios.timing == MMC_TIMING_UHS_SDR104)))
  321. return 0;
  322. retry:
  323. /* First of all reset the tuning block */
  324. rc = msm_init_cm_dll(host);
  325. if (rc)
  326. return rc;
  327. phase = 0;
  328. do {
  329. /* Set the phase in delay line hw block */
  330. rc = msm_config_cm_dll_phase(host, phase);
  331. if (rc)
  332. return rc;
  333. rc = mmc_send_tuning(mmc, opcode, NULL);
  334. if (!rc) {
  335. /* Tuning is successful at this tuning point */
  336. tuned_phases[tuned_phase_cnt++] = phase;
  337. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  338. mmc_hostname(mmc), phase);
  339. }
  340. } while (++phase < ARRAY_SIZE(tuned_phases));
  341. if (tuned_phase_cnt) {
  342. rc = msm_find_most_appropriate_phase(host, tuned_phases,
  343. tuned_phase_cnt);
  344. if (rc < 0)
  345. return rc;
  346. else
  347. phase = rc;
  348. /*
  349. * Finally set the selected phase in delay
  350. * line hw block.
  351. */
  352. rc = msm_config_cm_dll_phase(host, phase);
  353. if (rc)
  354. return rc;
  355. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  356. mmc_hostname(mmc), phase);
  357. } else {
  358. if (--tuning_seq_cnt)
  359. goto retry;
  360. /* Tuning failed */
  361. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  362. mmc_hostname(mmc));
  363. rc = -EIO;
  364. }
  365. return rc;
  366. }
  367. static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
  368. unsigned int uhs)
  369. {
  370. struct mmc_host *mmc = host->mmc;
  371. u16 ctrl_2;
  372. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  373. /* Select Bus Speed Mode for host */
  374. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  375. switch (uhs) {
  376. case MMC_TIMING_UHS_SDR12:
  377. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  378. break;
  379. case MMC_TIMING_UHS_SDR25:
  380. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  381. break;
  382. case MMC_TIMING_UHS_SDR50:
  383. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  384. break;
  385. case MMC_TIMING_MMC_HS200:
  386. case MMC_TIMING_UHS_SDR104:
  387. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  388. break;
  389. case MMC_TIMING_UHS_DDR50:
  390. case MMC_TIMING_MMC_DDR52:
  391. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  392. break;
  393. }
  394. /*
  395. * When clock frequency is less than 100MHz, the feedback clock must be
  396. * provided and DLL must not be used so that tuning can be skipped. To
  397. * provide feedback clock, the mode selection can be any value less
  398. * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
  399. */
  400. if (host->clock <= 100000000 &&
  401. (uhs == MMC_TIMING_MMC_HS400 ||
  402. uhs == MMC_TIMING_MMC_HS200 ||
  403. uhs == MMC_TIMING_UHS_SDR104))
  404. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  405. dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n",
  406. mmc_hostname(host->mmc), host->clock, uhs, ctrl_2);
  407. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  408. }
  409. static void sdhci_msm_voltage_switch(struct sdhci_host *host)
  410. {
  411. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  412. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  413. u32 irq_status, irq_ack = 0;
  414. irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  415. irq_status &= INT_MASK;
  416. writel_relaxed(irq_status, msm_host->core_mem + CORE_PWRCTL_CLEAR);
  417. if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
  418. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  419. if (irq_status & (CORE_PWRCTL_IO_LOW | CORE_PWRCTL_IO_HIGH))
  420. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  421. /*
  422. * The driver has to acknowledge the interrupt, switch voltages and
  423. * report back if it succeded or not to this register. The voltage
  424. * switches are handled by the sdhci core, so just report success.
  425. */
  426. writel_relaxed(irq_ack, msm_host->core_mem + CORE_PWRCTL_CTL);
  427. }
  428. static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
  429. {
  430. struct sdhci_host *host = (struct sdhci_host *)data;
  431. sdhci_msm_voltage_switch(host);
  432. return IRQ_HANDLED;
  433. }
  434. static const struct of_device_id sdhci_msm_dt_match[] = {
  435. { .compatible = "qcom,sdhci-msm-v4" },
  436. {},
  437. };
  438. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  439. static const struct sdhci_ops sdhci_msm_ops = {
  440. .platform_execute_tuning = sdhci_msm_execute_tuning,
  441. .reset = sdhci_reset,
  442. .set_clock = sdhci_set_clock,
  443. .set_bus_width = sdhci_set_bus_width,
  444. .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
  445. .voltage_switch = sdhci_msm_voltage_switch,
  446. };
  447. static const struct sdhci_pltfm_data sdhci_msm_pdata = {
  448. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  449. SDHCI_QUIRK_NO_CARD_NO_RESET |
  450. SDHCI_QUIRK_SINGLE_POWER_WRITE,
  451. .ops = &sdhci_msm_ops,
  452. };
  453. static int sdhci_msm_probe(struct platform_device *pdev)
  454. {
  455. struct sdhci_host *host;
  456. struct sdhci_pltfm_host *pltfm_host;
  457. struct sdhci_msm_host *msm_host;
  458. struct resource *core_memres;
  459. int ret;
  460. u16 host_version, core_minor;
  461. u32 core_version, caps;
  462. u8 core_major;
  463. host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
  464. if (IS_ERR(host))
  465. return PTR_ERR(host);
  466. pltfm_host = sdhci_priv(host);
  467. msm_host = sdhci_pltfm_priv(pltfm_host);
  468. msm_host->mmc = host->mmc;
  469. msm_host->pdev = pdev;
  470. ret = mmc_of_parse(host->mmc);
  471. if (ret)
  472. goto pltfm_free;
  473. sdhci_get_of_property(pdev);
  474. /* Setup SDCC bus voter clock. */
  475. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
  476. if (!IS_ERR(msm_host->bus_clk)) {
  477. /* Vote for max. clk rate for max. performance */
  478. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  479. if (ret)
  480. goto pltfm_free;
  481. ret = clk_prepare_enable(msm_host->bus_clk);
  482. if (ret)
  483. goto pltfm_free;
  484. }
  485. /* Setup main peripheral bus clock */
  486. msm_host->pclk = devm_clk_get(&pdev->dev, "iface");
  487. if (IS_ERR(msm_host->pclk)) {
  488. ret = PTR_ERR(msm_host->pclk);
  489. dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
  490. goto bus_clk_disable;
  491. }
  492. ret = clk_prepare_enable(msm_host->pclk);
  493. if (ret)
  494. goto bus_clk_disable;
  495. /* Setup SDC MMC clock */
  496. msm_host->clk = devm_clk_get(&pdev->dev, "core");
  497. if (IS_ERR(msm_host->clk)) {
  498. ret = PTR_ERR(msm_host->clk);
  499. dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
  500. goto pclk_disable;
  501. }
  502. /* Vote for maximum clock rate for maximum performance */
  503. ret = clk_set_rate(msm_host->clk, INT_MAX);
  504. if (ret)
  505. dev_warn(&pdev->dev, "core clock boost failed\n");
  506. ret = clk_prepare_enable(msm_host->clk);
  507. if (ret)
  508. goto pclk_disable;
  509. core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  510. msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres);
  511. if (IS_ERR(msm_host->core_mem)) {
  512. dev_err(&pdev->dev, "Failed to remap registers\n");
  513. ret = PTR_ERR(msm_host->core_mem);
  514. goto clk_disable;
  515. }
  516. /* Reset the core and Enable SDHC mode */
  517. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
  518. CORE_SW_RST, msm_host->core_mem + CORE_POWER);
  519. /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
  520. usleep_range(1000, 5000);
  521. if (readl(msm_host->core_mem + CORE_POWER) & CORE_SW_RST) {
  522. dev_err(&pdev->dev, "Stuck in reset\n");
  523. ret = -ETIMEDOUT;
  524. goto clk_disable;
  525. }
  526. /* Set HC_MODE_EN bit in HC_MODE register */
  527. writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
  528. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  529. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  530. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  531. SDHCI_VENDOR_VER_SHIFT));
  532. core_version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
  533. core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
  534. CORE_VERSION_MAJOR_SHIFT;
  535. core_minor = core_version & CORE_VERSION_MINOR_MASK;
  536. dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
  537. core_version, core_major, core_minor);
  538. /*
  539. * Support for some capabilities is not advertised by newer
  540. * controller versions and must be explicitly enabled.
  541. */
  542. if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
  543. caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
  544. caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
  545. writel_relaxed(caps, host->ioaddr +
  546. CORE_VENDOR_SPEC_CAPABILITIES0);
  547. }
  548. /*
  549. * Power on reset state may trigger power irq if previous status of
  550. * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq
  551. * interrupt in GIC, any pending power irq interrupt should be
  552. * acknowledged. Otherwise power irq interrupt handler would be
  553. * fired prematurely.
  554. */
  555. sdhci_msm_voltage_switch(host);
  556. /*
  557. * Ensure that above writes are propogated before interrupt enablement
  558. * in GIC.
  559. */
  560. mb();
  561. /* Setup IRQ for handling power/voltage tasks with PMIC */
  562. msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
  563. if (msm_host->pwr_irq < 0) {
  564. dev_err(&pdev->dev, "Get pwr_irq failed (%d)\n",
  565. msm_host->pwr_irq);
  566. ret = msm_host->pwr_irq;
  567. goto clk_disable;
  568. }
  569. /* Enable pwr irq interrupts */
  570. writel_relaxed(INT_MASK, msm_host->core_mem + CORE_PWRCTL_MASK);
  571. ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
  572. sdhci_msm_pwr_irq, IRQF_ONESHOT,
  573. dev_name(&pdev->dev), host);
  574. if (ret) {
  575. dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
  576. goto clk_disable;
  577. }
  578. ret = sdhci_add_host(host);
  579. if (ret)
  580. goto clk_disable;
  581. return 0;
  582. clk_disable:
  583. clk_disable_unprepare(msm_host->clk);
  584. pclk_disable:
  585. clk_disable_unprepare(msm_host->pclk);
  586. bus_clk_disable:
  587. if (!IS_ERR(msm_host->bus_clk))
  588. clk_disable_unprepare(msm_host->bus_clk);
  589. pltfm_free:
  590. sdhci_pltfm_free(pdev);
  591. return ret;
  592. }
  593. static int sdhci_msm_remove(struct platform_device *pdev)
  594. {
  595. struct sdhci_host *host = platform_get_drvdata(pdev);
  596. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  597. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  598. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  599. 0xffffffff);
  600. sdhci_remove_host(host, dead);
  601. clk_disable_unprepare(msm_host->clk);
  602. clk_disable_unprepare(msm_host->pclk);
  603. if (!IS_ERR(msm_host->bus_clk))
  604. clk_disable_unprepare(msm_host->bus_clk);
  605. sdhci_pltfm_free(pdev);
  606. return 0;
  607. }
  608. static struct platform_driver sdhci_msm_driver = {
  609. .probe = sdhci_msm_probe,
  610. .remove = sdhci_msm_remove,
  611. .driver = {
  612. .name = "sdhci_msm",
  613. .of_match_table = sdhci_msm_dt_match,
  614. },
  615. };
  616. module_platform_driver(sdhci_msm_driver);
  617. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  618. MODULE_LICENSE("GPL v2");