sdhci-msm.c 40 KB

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