sdhci-msm.c 50 KB

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