sdhci-msm.c 38 KB

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