sdhci-msm.c 47 KB

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