sdhci-msm.c 57 KB

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