sdhci-msm.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/iopoll.h>
  23. #include "sdhci-pltfm.h"
  24. #define CORE_MCI_VERSION 0x50
  25. #define CORE_VERSION_MAJOR_SHIFT 28
  26. #define CORE_VERSION_MAJOR_MASK (0xf << CORE_VERSION_MAJOR_SHIFT)
  27. #define CORE_VERSION_MINOR_MASK 0xff
  28. #define CORE_HC_MODE 0x78
  29. #define HC_MODE_EN 0x1
  30. #define CORE_POWER 0x0
  31. #define CORE_SW_RST BIT(7)
  32. #define FF_CLK_SW_RST_DIS BIT(13)
  33. #define CORE_PWRCTL_STATUS 0xdc
  34. #define CORE_PWRCTL_MASK 0xe0
  35. #define CORE_PWRCTL_CLEAR 0xe4
  36. #define CORE_PWRCTL_CTL 0xe8
  37. #define CORE_PWRCTL_BUS_OFF BIT(0)
  38. #define CORE_PWRCTL_BUS_ON BIT(1)
  39. #define CORE_PWRCTL_IO_LOW BIT(2)
  40. #define CORE_PWRCTL_IO_HIGH BIT(3)
  41. #define CORE_PWRCTL_BUS_SUCCESS BIT(0)
  42. #define CORE_PWRCTL_IO_SUCCESS BIT(2)
  43. #define REQ_BUS_OFF BIT(0)
  44. #define REQ_BUS_ON BIT(1)
  45. #define REQ_IO_LOW BIT(2)
  46. #define REQ_IO_HIGH BIT(3)
  47. #define INT_MASK 0xf
  48. #define MAX_PHASES 16
  49. #define CORE_DLL_LOCK BIT(7)
  50. #define CORE_DDR_DLL_LOCK BIT(11)
  51. #define CORE_DLL_EN BIT(16)
  52. #define CORE_CDR_EN BIT(17)
  53. #define CORE_CK_OUT_EN BIT(18)
  54. #define CORE_CDR_EXT_EN BIT(19)
  55. #define CORE_DLL_PDN BIT(29)
  56. #define CORE_DLL_RST BIT(30)
  57. #define CORE_DLL_CONFIG 0x100
  58. #define CORE_CMD_DAT_TRACK_SEL BIT(0)
  59. #define CORE_DLL_STATUS 0x108
  60. #define CORE_DLL_CONFIG_2 0x1b4
  61. #define CORE_DDR_CAL_EN BIT(0)
  62. #define CORE_FLL_CYCLE_CNT BIT(18)
  63. #define CORE_DLL_CLOCK_DISABLE BIT(21)
  64. #define CORE_VENDOR_SPEC 0x10c
  65. #define CORE_VENDOR_SPEC_POR_VAL 0xa1c
  66. #define CORE_CLK_PWRSAVE BIT(1)
  67. #define CORE_HC_MCLK_SEL_DFLT (2 << 8)
  68. #define CORE_HC_MCLK_SEL_HS400 (3 << 8)
  69. #define CORE_HC_MCLK_SEL_MASK (3 << 8)
  70. #define CORE_HC_SELECT_IN_EN BIT(18)
  71. #define CORE_HC_SELECT_IN_HS400 (6 << 19)
  72. #define CORE_HC_SELECT_IN_MASK (7 << 19)
  73. #define CORE_CSR_CDC_CTLR_CFG0 0x130
  74. #define CORE_SW_TRIG_FULL_CALIB BIT(16)
  75. #define CORE_HW_AUTOCAL_ENA BIT(17)
  76. #define CORE_CSR_CDC_CTLR_CFG1 0x134
  77. #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
  78. #define CORE_TIMER_ENA BIT(16)
  79. #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
  80. #define CORE_CSR_CDC_REFCOUNT_CFG 0x140
  81. #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
  82. #define CORE_CDC_OFFSET_CFG 0x14C
  83. #define CORE_CSR_CDC_DELAY_CFG 0x150
  84. #define CORE_CDC_SLAVE_DDA_CFG 0x160
  85. #define CORE_CSR_CDC_STATUS0 0x164
  86. #define CORE_CALIBRATION_DONE BIT(0)
  87. #define CORE_CDC_ERROR_CODE_MASK 0x7000000
  88. #define CORE_CSR_CDC_GEN_CFG 0x178
  89. #define CORE_CDC_SWITCH_BYPASS_OFF BIT(0)
  90. #define CORE_CDC_SWITCH_RC_EN BIT(1)
  91. #define CORE_DDR_200_CFG 0x184
  92. #define CORE_CDC_T4_DLY_SEL BIT(0)
  93. #define CORE_CMDIN_RCLK_EN BIT(1)
  94. #define CORE_START_CDC_TRAFFIC BIT(6)
  95. #define CORE_VENDOR_SPEC3 0x1b0
  96. #define CORE_PWRSAVE_DLL BIT(3)
  97. #define CORE_DDR_CONFIG 0x1b8
  98. #define DDR_CONFIG_POR_VAL 0x80040853
  99. #define CORE_VENDOR_SPEC_CAPABILITIES0 0x11c
  100. #define INVALID_TUNING_PHASE -1
  101. #define SDHCI_MSM_MIN_CLOCK 400000
  102. #define CORE_FREQ_100MHZ (100 * 1000 * 1000)
  103. #define CDR_SELEXT_SHIFT 20
  104. #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
  105. #define CMUX_SHIFT_PHASE_SHIFT 24
  106. #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
  107. #define MSM_MMC_AUTOSUSPEND_DELAY_MS 50
  108. struct sdhci_msm_host {
  109. struct platform_device *pdev;
  110. void __iomem *core_mem; /* MSM SDCC mapped address */
  111. int pwr_irq; /* power irq */
  112. struct clk *clk; /* main SD/MMC bus clock */
  113. struct clk *pclk; /* SDHC peripheral bus clock */
  114. struct clk *bus_clk; /* SDHC bus voter clock */
  115. struct clk *xo_clk; /* TCXO clk needed for FLL feature of cm_dll*/
  116. unsigned long clk_rate;
  117. struct mmc_host *mmc;
  118. bool use_14lpp_dll_reset;
  119. bool tuning_done;
  120. bool calibration_done;
  121. u8 saved_tuning_phase;
  122. bool use_cdclp533;
  123. };
  124. static unsigned int msm_get_clock_rate_for_bus_mode(struct sdhci_host *host,
  125. unsigned int clock)
  126. {
  127. struct mmc_ios ios = host->mmc->ios;
  128. /*
  129. * The SDHC requires internal clock frequency to be double the
  130. * actual clock that will be set for DDR mode. The controller
  131. * uses the faster clock(100/400MHz) for some of its parts and
  132. * send the actual required clock (50/200MHz) to the card.
  133. */
  134. if (ios.timing == MMC_TIMING_UHS_DDR50 ||
  135. ios.timing == MMC_TIMING_MMC_DDR52 ||
  136. ios.timing == MMC_TIMING_MMC_HS400 ||
  137. host->flags & SDHCI_HS400_TUNING)
  138. clock *= 2;
  139. return clock;
  140. }
  141. static void msm_set_clock_rate_for_bus_mode(struct sdhci_host *host,
  142. unsigned int clock)
  143. {
  144. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  145. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  146. struct mmc_ios curr_ios = host->mmc->ios;
  147. int rc;
  148. clock = msm_get_clock_rate_for_bus_mode(host, clock);
  149. rc = clk_set_rate(msm_host->clk, clock);
  150. if (rc) {
  151. pr_err("%s: Failed to set clock at rate %u at timing %d\n",
  152. mmc_hostname(host->mmc), clock,
  153. curr_ios.timing);
  154. return;
  155. }
  156. msm_host->clk_rate = clock;
  157. pr_debug("%s: Setting clock at rate %lu at timing %d\n",
  158. mmc_hostname(host->mmc), clk_get_rate(msm_host->clk),
  159. curr_ios.timing);
  160. }
  161. /* Platform specific tuning */
  162. static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
  163. {
  164. u32 wait_cnt = 50;
  165. u8 ck_out_en;
  166. struct mmc_host *mmc = host->mmc;
  167. /* Poll for CK_OUT_EN bit. max. poll time = 50us */
  168. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  169. CORE_CK_OUT_EN);
  170. while (ck_out_en != poll) {
  171. if (--wait_cnt == 0) {
  172. dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
  173. mmc_hostname(mmc), poll);
  174. return -ETIMEDOUT;
  175. }
  176. udelay(1);
  177. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  178. CORE_CK_OUT_EN);
  179. }
  180. return 0;
  181. }
  182. static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  183. {
  184. int rc;
  185. static const u8 grey_coded_phase_table[] = {
  186. 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  187. 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
  188. };
  189. unsigned long flags;
  190. u32 config;
  191. struct mmc_host *mmc = host->mmc;
  192. if (phase > 0xf)
  193. return -EINVAL;
  194. spin_lock_irqsave(&host->lock, flags);
  195. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  196. config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  197. config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  198. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  199. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  200. rc = msm_dll_poll_ck_out_en(host, 0);
  201. if (rc)
  202. goto err_out;
  203. /*
  204. * Write the selected DLL clock output phase (0 ... 15)
  205. * to CDR_SELEXT bit field of DLL_CONFIG register.
  206. */
  207. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  208. config &= ~CDR_SELEXT_MASK;
  209. config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
  210. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  211. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  212. config |= CORE_CK_OUT_EN;
  213. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  214. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  215. rc = msm_dll_poll_ck_out_en(host, 1);
  216. if (rc)
  217. goto err_out;
  218. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  219. config |= CORE_CDR_EN;
  220. config &= ~CORE_CDR_EXT_EN;
  221. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  222. goto out;
  223. err_out:
  224. dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
  225. mmc_hostname(mmc), phase);
  226. out:
  227. spin_unlock_irqrestore(&host->lock, flags);
  228. return rc;
  229. }
  230. /*
  231. * Find out the greatest range of consecuitive selected
  232. * DLL clock output phases that can be used as sampling
  233. * setting for SD3.0 UHS-I card read operation (in SDR104
  234. * timing mode) or for eMMC4.5 card read operation (in
  235. * HS400/HS200 timing mode).
  236. * Select the 3/4 of the range and configure the DLL with the
  237. * selected DLL clock output phase.
  238. */
  239. static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  240. u8 *phase_table, u8 total_phases)
  241. {
  242. int ret;
  243. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  244. u8 phases_per_row[MAX_PHASES] = { 0 };
  245. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  246. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  247. bool phase_0_found = false, phase_15_found = false;
  248. struct mmc_host *mmc = host->mmc;
  249. if (!total_phases || (total_phases > MAX_PHASES)) {
  250. dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
  251. mmc_hostname(mmc), total_phases);
  252. return -EINVAL;
  253. }
  254. for (cnt = 0; cnt < total_phases; cnt++) {
  255. ranges[row_index][col_index] = phase_table[cnt];
  256. phases_per_row[row_index] += 1;
  257. col_index++;
  258. if ((cnt + 1) == total_phases) {
  259. continue;
  260. /* check if next phase in phase_table is consecutive or not */
  261. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  262. row_index++;
  263. col_index = 0;
  264. }
  265. }
  266. if (row_index >= MAX_PHASES)
  267. return -EINVAL;
  268. /* Check if phase-0 is present in first valid window? */
  269. if (!ranges[0][0]) {
  270. phase_0_found = true;
  271. phase_0_raw_index = 0;
  272. /* Check if cycle exist between 2 valid windows */
  273. for (cnt = 1; cnt <= row_index; cnt++) {
  274. if (phases_per_row[cnt]) {
  275. for (i = 0; i < phases_per_row[cnt]; i++) {
  276. if (ranges[cnt][i] == 15) {
  277. phase_15_found = true;
  278. phase_15_raw_index = cnt;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /* If 2 valid windows form cycle then merge them as single window */
  286. if (phase_0_found && phase_15_found) {
  287. /* number of phases in raw where phase 0 is present */
  288. u8 phases_0 = phases_per_row[phase_0_raw_index];
  289. /* number of phases in raw where phase 15 is present */
  290. u8 phases_15 = phases_per_row[phase_15_raw_index];
  291. if (phases_0 + phases_15 >= MAX_PHASES)
  292. /*
  293. * If there are more than 1 phase windows then total
  294. * number of phases in both the windows should not be
  295. * more than or equal to MAX_PHASES.
  296. */
  297. return -EINVAL;
  298. /* Merge 2 cyclic windows */
  299. i = phases_15;
  300. for (cnt = 0; cnt < phases_0; cnt++) {
  301. ranges[phase_15_raw_index][i] =
  302. ranges[phase_0_raw_index][cnt];
  303. if (++i >= MAX_PHASES)
  304. break;
  305. }
  306. phases_per_row[phase_0_raw_index] = 0;
  307. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  308. }
  309. for (cnt = 0; cnt <= row_index; cnt++) {
  310. if (phases_per_row[cnt] > curr_max) {
  311. curr_max = phases_per_row[cnt];
  312. selected_row_index = cnt;
  313. }
  314. }
  315. i = (curr_max * 3) / 4;
  316. if (i)
  317. i--;
  318. ret = ranges[selected_row_index][i];
  319. if (ret >= MAX_PHASES) {
  320. ret = -EINVAL;
  321. dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
  322. mmc_hostname(mmc), ret);
  323. }
  324. return ret;
  325. }
  326. static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  327. {
  328. u32 mclk_freq = 0, config;
  329. /* Program the MCLK value to MCLK_FREQ bit field */
  330. if (host->clock <= 112000000)
  331. mclk_freq = 0;
  332. else if (host->clock <= 125000000)
  333. mclk_freq = 1;
  334. else if (host->clock <= 137000000)
  335. mclk_freq = 2;
  336. else if (host->clock <= 150000000)
  337. mclk_freq = 3;
  338. else if (host->clock <= 162000000)
  339. mclk_freq = 4;
  340. else if (host->clock <= 175000000)
  341. mclk_freq = 5;
  342. else if (host->clock <= 187000000)
  343. mclk_freq = 6;
  344. else if (host->clock <= 200000000)
  345. mclk_freq = 7;
  346. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  347. config &= ~CMUX_SHIFT_PHASE_MASK;
  348. config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
  349. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  350. }
  351. /* Initialize the DLL (Programmable Delay Line) */
  352. static int msm_init_cm_dll(struct sdhci_host *host)
  353. {
  354. struct mmc_host *mmc = host->mmc;
  355. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  356. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  357. int wait_cnt = 50;
  358. unsigned long flags;
  359. u32 config;
  360. spin_lock_irqsave(&host->lock, flags);
  361. /*
  362. * Make sure that clock is always enabled when DLL
  363. * tuning is in progress. Keeping PWRSAVE ON may
  364. * turn off the clock.
  365. */
  366. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  367. config &= ~CORE_CLK_PWRSAVE;
  368. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
  369. if (msm_host->use_14lpp_dll_reset) {
  370. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  371. config &= ~CORE_CK_OUT_EN;
  372. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  373. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
  374. config |= CORE_DLL_CLOCK_DISABLE;
  375. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
  376. }
  377. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  378. config |= CORE_DLL_RST;
  379. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  380. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  381. config |= CORE_DLL_PDN;
  382. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  383. msm_cm_dll_set_freq(host);
  384. if (msm_host->use_14lpp_dll_reset &&
  385. !IS_ERR_OR_NULL(msm_host->xo_clk)) {
  386. u32 mclk_freq = 0;
  387. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
  388. config &= CORE_FLL_CYCLE_CNT;
  389. if (config)
  390. mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 8),
  391. clk_get_rate(msm_host->xo_clk));
  392. else
  393. mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 4),
  394. clk_get_rate(msm_host->xo_clk));
  395. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
  396. config &= ~(0xFF << 10);
  397. config |= mclk_freq << 10;
  398. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
  399. /* wait for 5us before enabling DLL clock */
  400. udelay(5);
  401. }
  402. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  403. config &= ~CORE_DLL_RST;
  404. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  405. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  406. config &= ~CORE_DLL_PDN;
  407. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  408. if (msm_host->use_14lpp_dll_reset) {
  409. msm_cm_dll_set_freq(host);
  410. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
  411. config &= ~CORE_DLL_CLOCK_DISABLE;
  412. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
  413. }
  414. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  415. config |= CORE_DLL_EN;
  416. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  417. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  418. config |= CORE_CK_OUT_EN;
  419. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  420. /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  421. while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
  422. CORE_DLL_LOCK)) {
  423. /* max. wait for 50us sec for LOCK bit to be set */
  424. if (--wait_cnt == 0) {
  425. dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
  426. mmc_hostname(mmc));
  427. spin_unlock_irqrestore(&host->lock, flags);
  428. return -ETIMEDOUT;
  429. }
  430. udelay(1);
  431. }
  432. spin_unlock_irqrestore(&host->lock, flags);
  433. return 0;
  434. }
  435. static void msm_hc_select_default(struct sdhci_host *host)
  436. {
  437. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  438. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  439. u32 config;
  440. if (!msm_host->use_cdclp533) {
  441. config = readl_relaxed(host->ioaddr +
  442. CORE_VENDOR_SPEC3);
  443. config &= ~CORE_PWRSAVE_DLL;
  444. writel_relaxed(config, host->ioaddr +
  445. CORE_VENDOR_SPEC3);
  446. }
  447. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  448. config &= ~CORE_HC_MCLK_SEL_MASK;
  449. config |= CORE_HC_MCLK_SEL_DFLT;
  450. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
  451. /*
  452. * Disable HC_SELECT_IN to be able to use the UHS mode select
  453. * configuration from Host Control2 register for all other
  454. * modes.
  455. * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
  456. * in VENDOR_SPEC_FUNC
  457. */
  458. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  459. config &= ~CORE_HC_SELECT_IN_EN;
  460. config &= ~CORE_HC_SELECT_IN_MASK;
  461. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
  462. /*
  463. * Make sure above writes impacting free running MCLK are completed
  464. * before changing the clk_rate at GCC.
  465. */
  466. wmb();
  467. }
  468. static void msm_hc_select_hs400(struct sdhci_host *host)
  469. {
  470. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  471. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  472. struct mmc_ios ios = host->mmc->ios;
  473. u32 config, dll_lock;
  474. int rc;
  475. /* Select the divided clock (free running MCLK/2) */
  476. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  477. config &= ~CORE_HC_MCLK_SEL_MASK;
  478. config |= CORE_HC_MCLK_SEL_HS400;
  479. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
  480. /*
  481. * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
  482. * register
  483. */
  484. if ((msm_host->tuning_done || ios.enhanced_strobe) &&
  485. !msm_host->calibration_done) {
  486. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  487. config |= CORE_HC_SELECT_IN_HS400;
  488. config |= CORE_HC_SELECT_IN_EN;
  489. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
  490. }
  491. if (!msm_host->clk_rate && !msm_host->use_cdclp533) {
  492. /*
  493. * Poll on DLL_LOCK or DDR_DLL_LOCK bits in
  494. * CORE_DLL_STATUS to be set. This should get set
  495. * within 15 us at 200 MHz.
  496. */
  497. rc = readl_relaxed_poll_timeout(host->ioaddr +
  498. CORE_DLL_STATUS,
  499. dll_lock,
  500. (dll_lock &
  501. (CORE_DLL_LOCK |
  502. CORE_DDR_DLL_LOCK)), 10,
  503. 1000);
  504. if (rc == -ETIMEDOUT)
  505. pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
  506. mmc_hostname(host->mmc), dll_lock);
  507. }
  508. /*
  509. * Make sure above writes impacting free running MCLK are completed
  510. * before changing the clk_rate at GCC.
  511. */
  512. wmb();
  513. }
  514. /*
  515. * sdhci_msm_hc_select_mode :- In general all timing modes are
  516. * controlled via UHS mode select in Host Control2 register.
  517. * eMMC specific HS200/HS400 doesn't have their respective modes
  518. * defined here, hence we use these values.
  519. *
  520. * HS200 - SDR104 (Since they both are equivalent in functionality)
  521. * HS400 - This involves multiple configurations
  522. * Initially SDR104 - when tuning is required as HS200
  523. * Then when switching to DDR @ 400MHz (HS400) we use
  524. * the vendor specific HC_SELECT_IN to control the mode.
  525. *
  526. * In addition to controlling the modes we also need to select the
  527. * correct input clock for DLL depending on the mode.
  528. *
  529. * HS400 - divided clock (free running MCLK/2)
  530. * All other modes - default (free running MCLK)
  531. */
  532. static void sdhci_msm_hc_select_mode(struct sdhci_host *host)
  533. {
  534. struct mmc_ios ios = host->mmc->ios;
  535. if (ios.timing == MMC_TIMING_MMC_HS400 ||
  536. host->flags & SDHCI_HS400_TUNING)
  537. msm_hc_select_hs400(host);
  538. else
  539. msm_hc_select_default(host);
  540. }
  541. static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
  542. {
  543. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  544. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  545. u32 config, calib_done;
  546. int ret;
  547. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  548. /*
  549. * Retuning in HS400 (DDR mode) will fail, just reset the
  550. * tuning block and restore the saved tuning phase.
  551. */
  552. ret = msm_init_cm_dll(host);
  553. if (ret)
  554. goto out;
  555. /* Set the selected phase in delay line hw block */
  556. ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
  557. if (ret)
  558. goto out;
  559. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  560. config |= CORE_CMD_DAT_TRACK_SEL;
  561. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  562. config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
  563. config &= ~CORE_CDC_T4_DLY_SEL;
  564. writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
  565. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  566. config &= ~CORE_CDC_SWITCH_BYPASS_OFF;
  567. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  568. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  569. config |= CORE_CDC_SWITCH_RC_EN;
  570. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  571. config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
  572. config &= ~CORE_START_CDC_TRAFFIC;
  573. writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
  574. /* Perform CDC Register Initialization Sequence */
  575. writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  576. writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
  577. writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  578. writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
  579. writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
  580. writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
  581. writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
  582. writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
  583. writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
  584. /* CDC HW Calibration */
  585. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  586. config |= CORE_SW_TRIG_FULL_CALIB;
  587. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  588. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  589. config &= ~CORE_SW_TRIG_FULL_CALIB;
  590. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  591. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  592. config |= CORE_HW_AUTOCAL_ENA;
  593. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  594. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  595. config |= CORE_TIMER_ENA;
  596. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  597. ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
  598. calib_done,
  599. (calib_done & CORE_CALIBRATION_DONE),
  600. 1, 50);
  601. if (ret == -ETIMEDOUT) {
  602. pr_err("%s: %s: CDC calibration was not completed\n",
  603. mmc_hostname(host->mmc), __func__);
  604. goto out;
  605. }
  606. ret = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
  607. & CORE_CDC_ERROR_CODE_MASK;
  608. if (ret) {
  609. pr_err("%s: %s: CDC error code %d\n",
  610. mmc_hostname(host->mmc), __func__, ret);
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
  615. config |= CORE_START_CDC_TRAFFIC;
  616. writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
  617. out:
  618. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  619. __func__, ret);
  620. return ret;
  621. }
  622. static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
  623. {
  624. struct mmc_host *mmc = host->mmc;
  625. u32 dll_status, config;
  626. int ret;
  627. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  628. /*
  629. * Currently the CORE_DDR_CONFIG register defaults to desired
  630. * configuration on reset. Currently reprogramming the power on
  631. * reset (POR) value in case it might have been modified by
  632. * bootloaders. In the future, if this changes, then the desired
  633. * values will need to be programmed appropriately.
  634. */
  635. writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
  636. if (mmc->ios.enhanced_strobe) {
  637. config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
  638. config |= CORE_CMDIN_RCLK_EN;
  639. writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
  640. }
  641. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
  642. config |= CORE_DDR_CAL_EN;
  643. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
  644. ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
  645. dll_status,
  646. (dll_status & CORE_DDR_DLL_LOCK),
  647. 10, 1000);
  648. if (ret == -ETIMEDOUT) {
  649. pr_err("%s: %s: CM_DLL_SDC4 calibration was not completed\n",
  650. mmc_hostname(host->mmc), __func__);
  651. goto out;
  652. }
  653. config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3);
  654. config |= CORE_PWRSAVE_DLL;
  655. writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC3);
  656. /*
  657. * Drain writebuffer to ensure above DLL calibration
  658. * and PWRSAVE DLL is enabled.
  659. */
  660. wmb();
  661. out:
  662. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  663. __func__, ret);
  664. return ret;
  665. }
  666. static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
  667. {
  668. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  669. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  670. struct mmc_host *mmc = host->mmc;
  671. int ret;
  672. u32 config;
  673. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  674. /*
  675. * Retuning in HS400 (DDR mode) will fail, just reset the
  676. * tuning block and restore the saved tuning phase.
  677. */
  678. ret = msm_init_cm_dll(host);
  679. if (ret)
  680. goto out;
  681. if (!mmc->ios.enhanced_strobe) {
  682. /* Set the selected phase in delay line hw block */
  683. ret = msm_config_cm_dll_phase(host,
  684. msm_host->saved_tuning_phase);
  685. if (ret)
  686. goto out;
  687. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  688. config |= CORE_CMD_DAT_TRACK_SEL;
  689. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  690. }
  691. if (msm_host->use_cdclp533)
  692. ret = sdhci_msm_cdclp533_calibration(host);
  693. else
  694. ret = sdhci_msm_cm_dll_sdc4_calibration(host);
  695. out:
  696. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  697. __func__, ret);
  698. return ret;
  699. }
  700. static int sdhci_msm_execute_tuning(struct mmc_host *mmc, u32 opcode)
  701. {
  702. struct sdhci_host *host = mmc_priv(mmc);
  703. int tuning_seq_cnt = 3;
  704. u8 phase, tuned_phases[16], tuned_phase_cnt = 0;
  705. int rc;
  706. struct mmc_ios ios = host->mmc->ios;
  707. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  708. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  709. /*
  710. * Tuning is required for SDR104, HS200 and HS400 cards and
  711. * if clock frequency is greater than 100MHz in these modes.
  712. */
  713. if (host->clock <= CORE_FREQ_100MHZ ||
  714. !(ios.timing == MMC_TIMING_MMC_HS400 ||
  715. ios.timing == MMC_TIMING_MMC_HS200 ||
  716. ios.timing == MMC_TIMING_UHS_SDR104))
  717. return 0;
  718. /*
  719. * For HS400 tuning in HS200 timing requires:
  720. * - select MCLK/2 in VENDOR_SPEC
  721. * - program MCLK to 400MHz (or nearest supported) in GCC
  722. */
  723. if (host->flags & SDHCI_HS400_TUNING) {
  724. sdhci_msm_hc_select_mode(host);
  725. msm_set_clock_rate_for_bus_mode(host, ios.clock);
  726. host->flags &= ~SDHCI_HS400_TUNING;
  727. }
  728. retry:
  729. /* First of all reset the tuning block */
  730. rc = msm_init_cm_dll(host);
  731. if (rc)
  732. return rc;
  733. phase = 0;
  734. do {
  735. /* Set the phase in delay line hw block */
  736. rc = msm_config_cm_dll_phase(host, phase);
  737. if (rc)
  738. return rc;
  739. msm_host->saved_tuning_phase = phase;
  740. rc = mmc_send_tuning(mmc, opcode, NULL);
  741. if (!rc) {
  742. /* Tuning is successful at this tuning point */
  743. tuned_phases[tuned_phase_cnt++] = phase;
  744. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  745. mmc_hostname(mmc), phase);
  746. }
  747. } while (++phase < ARRAY_SIZE(tuned_phases));
  748. if (tuned_phase_cnt) {
  749. rc = msm_find_most_appropriate_phase(host, tuned_phases,
  750. tuned_phase_cnt);
  751. if (rc < 0)
  752. return rc;
  753. else
  754. phase = rc;
  755. /*
  756. * Finally set the selected phase in delay
  757. * line hw block.
  758. */
  759. rc = msm_config_cm_dll_phase(host, phase);
  760. if (rc)
  761. return rc;
  762. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  763. mmc_hostname(mmc), phase);
  764. } else {
  765. if (--tuning_seq_cnt)
  766. goto retry;
  767. /* Tuning failed */
  768. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  769. mmc_hostname(mmc));
  770. rc = -EIO;
  771. }
  772. if (!rc)
  773. msm_host->tuning_done = true;
  774. return rc;
  775. }
  776. /*
  777. * sdhci_msm_hs400 - Calibrate the DLL for HS400 bus speed mode operation.
  778. * This needs to be done for both tuning and enhanced_strobe mode.
  779. * DLL operation is only needed for clock > 100MHz. For clock <= 100MHz
  780. * fixed feedback clock is used.
  781. */
  782. static void sdhci_msm_hs400(struct sdhci_host *host, struct mmc_ios *ios)
  783. {
  784. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  785. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  786. int ret;
  787. if (host->clock > CORE_FREQ_100MHZ &&
  788. (msm_host->tuning_done || ios->enhanced_strobe) &&
  789. !msm_host->calibration_done) {
  790. ret = sdhci_msm_hs400_dll_calibration(host);
  791. if (!ret)
  792. msm_host->calibration_done = true;
  793. else
  794. pr_err("%s: Failed to calibrate DLL for hs400 mode (%d)\n",
  795. mmc_hostname(host->mmc), ret);
  796. }
  797. }
  798. static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
  799. unsigned int uhs)
  800. {
  801. struct mmc_host *mmc = host->mmc;
  802. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  803. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  804. u16 ctrl_2;
  805. u32 config;
  806. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  807. /* Select Bus Speed Mode for host */
  808. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  809. switch (uhs) {
  810. case MMC_TIMING_UHS_SDR12:
  811. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  812. break;
  813. case MMC_TIMING_UHS_SDR25:
  814. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  815. break;
  816. case MMC_TIMING_UHS_SDR50:
  817. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  818. break;
  819. case MMC_TIMING_MMC_HS400:
  820. case MMC_TIMING_MMC_HS200:
  821. case MMC_TIMING_UHS_SDR104:
  822. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  823. break;
  824. case MMC_TIMING_UHS_DDR50:
  825. case MMC_TIMING_MMC_DDR52:
  826. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  827. break;
  828. }
  829. /*
  830. * When clock frequency is less than 100MHz, the feedback clock must be
  831. * provided and DLL must not be used so that tuning can be skipped. To
  832. * provide feedback clock, the mode selection can be any value less
  833. * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
  834. */
  835. if (host->clock <= CORE_FREQ_100MHZ) {
  836. if (uhs == MMC_TIMING_MMC_HS400 ||
  837. uhs == MMC_TIMING_MMC_HS200 ||
  838. uhs == MMC_TIMING_UHS_SDR104)
  839. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  840. /*
  841. * DLL is not required for clock <= 100MHz
  842. * Thus, make sure DLL it is disabled when not required
  843. */
  844. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  845. config |= CORE_DLL_RST;
  846. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  847. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  848. config |= CORE_DLL_PDN;
  849. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  850. /*
  851. * The DLL needs to be restored and CDCLP533 recalibrated
  852. * when the clock frequency is set back to 400MHz.
  853. */
  854. msm_host->calibration_done = false;
  855. }
  856. dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n",
  857. mmc_hostname(host->mmc), host->clock, uhs, ctrl_2);
  858. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  859. if (mmc->ios.timing == MMC_TIMING_MMC_HS400)
  860. sdhci_msm_hs400(host, &mmc->ios);
  861. }
  862. static void sdhci_msm_voltage_switch(struct sdhci_host *host)
  863. {
  864. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  865. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  866. u32 irq_status, irq_ack = 0;
  867. irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  868. irq_status &= INT_MASK;
  869. writel_relaxed(irq_status, msm_host->core_mem + CORE_PWRCTL_CLEAR);
  870. if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
  871. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  872. if (irq_status & (CORE_PWRCTL_IO_LOW | CORE_PWRCTL_IO_HIGH))
  873. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  874. /*
  875. * The driver has to acknowledge the interrupt, switch voltages and
  876. * report back if it succeded or not to this register. The voltage
  877. * switches are handled by the sdhci core, so just report success.
  878. */
  879. writel_relaxed(irq_ack, msm_host->core_mem + CORE_PWRCTL_CTL);
  880. }
  881. static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
  882. {
  883. struct sdhci_host *host = (struct sdhci_host *)data;
  884. sdhci_msm_voltage_switch(host);
  885. return IRQ_HANDLED;
  886. }
  887. static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
  888. {
  889. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  890. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  891. return clk_round_rate(msm_host->clk, ULONG_MAX);
  892. }
  893. static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
  894. {
  895. return SDHCI_MSM_MIN_CLOCK;
  896. }
  897. /**
  898. * __sdhci_msm_set_clock - sdhci_msm clock control.
  899. *
  900. * Description:
  901. * MSM controller does not use internal divider and
  902. * instead directly control the GCC clock as per
  903. * HW recommendation.
  904. **/
  905. static void __sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  906. {
  907. u16 clk;
  908. /*
  909. * Keep actual_clock as zero -
  910. * - since there is no divider used so no need of having actual_clock.
  911. * - MSM controller uses SDCLK for data timeout calculation. If
  912. * actual_clock is zero, host->clock is taken for calculation.
  913. */
  914. host->mmc->actual_clock = 0;
  915. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  916. if (clock == 0)
  917. return;
  918. /*
  919. * MSM controller do not use clock divider.
  920. * Thus read SDHCI_CLOCK_CONTROL and only enable
  921. * clock with no divider value programmed.
  922. */
  923. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  924. sdhci_enable_clk(host, clk);
  925. }
  926. /* sdhci_msm_set_clock - Called with (host->lock) spinlock held. */
  927. static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  928. {
  929. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  930. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  931. if (!clock) {
  932. msm_host->clk_rate = clock;
  933. goto out;
  934. }
  935. sdhci_msm_hc_select_mode(host);
  936. msm_set_clock_rate_for_bus_mode(host, clock);
  937. out:
  938. __sdhci_msm_set_clock(host, clock);
  939. }
  940. static const struct of_device_id sdhci_msm_dt_match[] = {
  941. { .compatible = "qcom,sdhci-msm-v4" },
  942. {},
  943. };
  944. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  945. static const struct sdhci_ops sdhci_msm_ops = {
  946. .reset = sdhci_reset,
  947. .set_clock = sdhci_msm_set_clock,
  948. .get_min_clock = sdhci_msm_get_min_clock,
  949. .get_max_clock = sdhci_msm_get_max_clock,
  950. .set_bus_width = sdhci_set_bus_width,
  951. .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
  952. .voltage_switch = sdhci_msm_voltage_switch,
  953. };
  954. static const struct sdhci_pltfm_data sdhci_msm_pdata = {
  955. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  956. SDHCI_QUIRK_NO_CARD_NO_RESET |
  957. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  958. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  959. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  960. .ops = &sdhci_msm_ops,
  961. };
  962. static int sdhci_msm_probe(struct platform_device *pdev)
  963. {
  964. struct sdhci_host *host;
  965. struct sdhci_pltfm_host *pltfm_host;
  966. struct sdhci_msm_host *msm_host;
  967. struct resource *core_memres;
  968. int ret;
  969. u16 host_version, core_minor;
  970. u32 core_version, config;
  971. u8 core_major;
  972. host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
  973. if (IS_ERR(host))
  974. return PTR_ERR(host);
  975. host->sdma_boundary = 0;
  976. pltfm_host = sdhci_priv(host);
  977. msm_host = sdhci_pltfm_priv(pltfm_host);
  978. msm_host->mmc = host->mmc;
  979. msm_host->pdev = pdev;
  980. ret = mmc_of_parse(host->mmc);
  981. if (ret)
  982. goto pltfm_free;
  983. sdhci_get_of_property(pdev);
  984. msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
  985. /* Setup SDCC bus voter clock. */
  986. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
  987. if (!IS_ERR(msm_host->bus_clk)) {
  988. /* Vote for max. clk rate for max. performance */
  989. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  990. if (ret)
  991. goto pltfm_free;
  992. ret = clk_prepare_enable(msm_host->bus_clk);
  993. if (ret)
  994. goto pltfm_free;
  995. }
  996. /* Setup main peripheral bus clock */
  997. msm_host->pclk = devm_clk_get(&pdev->dev, "iface");
  998. if (IS_ERR(msm_host->pclk)) {
  999. ret = PTR_ERR(msm_host->pclk);
  1000. dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
  1001. goto bus_clk_disable;
  1002. }
  1003. ret = clk_prepare_enable(msm_host->pclk);
  1004. if (ret)
  1005. goto bus_clk_disable;
  1006. /* Setup SDC MMC clock */
  1007. msm_host->clk = devm_clk_get(&pdev->dev, "core");
  1008. if (IS_ERR(msm_host->clk)) {
  1009. ret = PTR_ERR(msm_host->clk);
  1010. dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
  1011. goto pclk_disable;
  1012. }
  1013. /*
  1014. * xo clock is needed for FLL feature of cm_dll.
  1015. * In case if xo clock is not mentioned in DT, warn and proceed.
  1016. */
  1017. msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo");
  1018. if (IS_ERR(msm_host->xo_clk)) {
  1019. ret = PTR_ERR(msm_host->xo_clk);
  1020. dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret);
  1021. }
  1022. /* Vote for maximum clock rate for maximum performance */
  1023. ret = clk_set_rate(msm_host->clk, INT_MAX);
  1024. if (ret)
  1025. dev_warn(&pdev->dev, "core clock boost failed\n");
  1026. ret = clk_prepare_enable(msm_host->clk);
  1027. if (ret)
  1028. goto pclk_disable;
  1029. core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1030. msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres);
  1031. if (IS_ERR(msm_host->core_mem)) {
  1032. dev_err(&pdev->dev, "Failed to remap registers\n");
  1033. ret = PTR_ERR(msm_host->core_mem);
  1034. goto clk_disable;
  1035. }
  1036. /* Reset the vendor spec register to power on reset state */
  1037. writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
  1038. host->ioaddr + CORE_VENDOR_SPEC);
  1039. /* Set HC_MODE_EN bit in HC_MODE register */
  1040. writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
  1041. config = readl_relaxed(msm_host->core_mem + CORE_HC_MODE);
  1042. config |= FF_CLK_SW_RST_DIS;
  1043. writel_relaxed(config, msm_host->core_mem + CORE_HC_MODE);
  1044. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  1045. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  1046. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  1047. SDHCI_VENDOR_VER_SHIFT));
  1048. core_version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
  1049. core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
  1050. CORE_VERSION_MAJOR_SHIFT;
  1051. core_minor = core_version & CORE_VERSION_MINOR_MASK;
  1052. dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
  1053. core_version, core_major, core_minor);
  1054. if (core_major == 1 && core_minor >= 0x42)
  1055. msm_host->use_14lpp_dll_reset = true;
  1056. /*
  1057. * SDCC 5 controller with major version 1, minor version 0x34 and later
  1058. * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
  1059. */
  1060. if (core_major == 1 && core_minor < 0x34)
  1061. msm_host->use_cdclp533 = true;
  1062. /*
  1063. * Support for some capabilities is not advertised by newer
  1064. * controller versions and must be explicitly enabled.
  1065. */
  1066. if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
  1067. config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
  1068. config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
  1069. writel_relaxed(config, host->ioaddr +
  1070. CORE_VENDOR_SPEC_CAPABILITIES0);
  1071. }
  1072. /* Setup IRQ for handling power/voltage tasks with PMIC */
  1073. msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
  1074. if (msm_host->pwr_irq < 0) {
  1075. dev_err(&pdev->dev, "Get pwr_irq failed (%d)\n",
  1076. msm_host->pwr_irq);
  1077. ret = msm_host->pwr_irq;
  1078. goto clk_disable;
  1079. }
  1080. ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
  1081. sdhci_msm_pwr_irq, IRQF_ONESHOT,
  1082. dev_name(&pdev->dev), host);
  1083. if (ret) {
  1084. dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
  1085. goto clk_disable;
  1086. }
  1087. pm_runtime_get_noresume(&pdev->dev);
  1088. pm_runtime_set_active(&pdev->dev);
  1089. pm_runtime_enable(&pdev->dev);
  1090. pm_runtime_set_autosuspend_delay(&pdev->dev,
  1091. MSM_MMC_AUTOSUSPEND_DELAY_MS);
  1092. pm_runtime_use_autosuspend(&pdev->dev);
  1093. host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning;
  1094. ret = sdhci_add_host(host);
  1095. if (ret)
  1096. goto pm_runtime_disable;
  1097. pm_runtime_mark_last_busy(&pdev->dev);
  1098. pm_runtime_put_autosuspend(&pdev->dev);
  1099. return 0;
  1100. pm_runtime_disable:
  1101. pm_runtime_disable(&pdev->dev);
  1102. pm_runtime_set_suspended(&pdev->dev);
  1103. pm_runtime_put_noidle(&pdev->dev);
  1104. clk_disable:
  1105. clk_disable_unprepare(msm_host->clk);
  1106. pclk_disable:
  1107. clk_disable_unprepare(msm_host->pclk);
  1108. bus_clk_disable:
  1109. if (!IS_ERR(msm_host->bus_clk))
  1110. clk_disable_unprepare(msm_host->bus_clk);
  1111. pltfm_free:
  1112. sdhci_pltfm_free(pdev);
  1113. return ret;
  1114. }
  1115. static int sdhci_msm_remove(struct platform_device *pdev)
  1116. {
  1117. struct sdhci_host *host = platform_get_drvdata(pdev);
  1118. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1119. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1120. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  1121. 0xffffffff);
  1122. sdhci_remove_host(host, dead);
  1123. pm_runtime_get_sync(&pdev->dev);
  1124. pm_runtime_disable(&pdev->dev);
  1125. pm_runtime_put_noidle(&pdev->dev);
  1126. clk_disable_unprepare(msm_host->clk);
  1127. clk_disable_unprepare(msm_host->pclk);
  1128. if (!IS_ERR(msm_host->bus_clk))
  1129. clk_disable_unprepare(msm_host->bus_clk);
  1130. sdhci_pltfm_free(pdev);
  1131. return 0;
  1132. }
  1133. #ifdef CONFIG_PM
  1134. static int sdhci_msm_runtime_suspend(struct device *dev)
  1135. {
  1136. struct sdhci_host *host = dev_get_drvdata(dev);
  1137. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1138. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1139. clk_disable_unprepare(msm_host->clk);
  1140. clk_disable_unprepare(msm_host->pclk);
  1141. return 0;
  1142. }
  1143. static int sdhci_msm_runtime_resume(struct device *dev)
  1144. {
  1145. struct sdhci_host *host = dev_get_drvdata(dev);
  1146. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1147. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1148. int ret;
  1149. ret = clk_prepare_enable(msm_host->clk);
  1150. if (ret) {
  1151. dev_err(dev, "clk_enable failed for core_clk: %d\n", ret);
  1152. return ret;
  1153. }
  1154. ret = clk_prepare_enable(msm_host->pclk);
  1155. if (ret) {
  1156. dev_err(dev, "clk_enable failed for iface_clk: %d\n", ret);
  1157. clk_disable_unprepare(msm_host->clk);
  1158. return ret;
  1159. }
  1160. return 0;
  1161. }
  1162. #endif
  1163. static const struct dev_pm_ops sdhci_msm_pm_ops = {
  1164. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1165. pm_runtime_force_resume)
  1166. SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend,
  1167. sdhci_msm_runtime_resume,
  1168. NULL)
  1169. };
  1170. static struct platform_driver sdhci_msm_driver = {
  1171. .probe = sdhci_msm_probe,
  1172. .remove = sdhci_msm_remove,
  1173. .driver = {
  1174. .name = "sdhci_msm",
  1175. .of_match_table = sdhci_msm_dt_match,
  1176. .pm = &sdhci_msm_pm_ops,
  1177. },
  1178. };
  1179. module_platform_driver(sdhci_msm_driver);
  1180. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  1181. MODULE_LICENSE("GPL v2");