sdhci-of-arasan.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Arasan Secure Digital Host Controller Interface.
  4. * Copyright (C) 2011 - 2012 Michal Simek <[email protected]>
  5. * Copyright (c) 2012 Wind River Systems, Inc.
  6. * Copyright (C) 2013 Pengutronix e.K.
  7. * Copyright (C) 2013 Xilinx Inc.
  8. *
  9. * Based on sdhci-of-esdhc.c
  10. *
  11. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  12. * Copyright (c) 2009 MontaVista Software, Inc.
  13. *
  14. * Authors: Xiaobo Xie <[email protected]>
  15. * Anton Vorontsov <[email protected]>
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/regmap.h>
  23. #include <linux/of.h>
  24. #include <linux/firmware/xlnx-zynqmp.h>
  25. #include "cqhci.h"
  26. #include "sdhci-cqhci.h"
  27. #include "sdhci-pltfm.h"
  28. #define SDHCI_ARASAN_VENDOR_REGISTER 0x78
  29. #define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
  30. #define SDHCI_ARASAN_ITAPDLY_SEL_MASK 0xFF
  31. #define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
  32. #define SDHCI_ARASAN_OTAPDLY_SEL_MASK 0x3F
  33. #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
  34. #define VENDOR_ENHANCED_STROBE BIT(0)
  35. #define PHY_CLK_TOO_SLOW_HZ 400000
  36. #define SDHCI_ITAPDLY_CHGWIN 0x200
  37. #define SDHCI_ITAPDLY_ENABLE 0x100
  38. #define SDHCI_OTAPDLY_ENABLE 0x40
  39. /* Default settings for ZynqMP Clock Phases */
  40. #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0}
  41. #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
  42. #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
  43. #define VERSAL_OCLK_PHASE {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
  44. /*
  45. * On some SoCs the syscon area has a feature where the upper 16-bits of
  46. * each 32-bit register act as a write mask for the lower 16-bits. This allows
  47. * atomic updates of the register without locking. This macro is used on SoCs
  48. * that have that feature.
  49. */
  50. #define HIWORD_UPDATE(val, mask, shift) \
  51. ((val) << (shift) | (mask) << ((shift) + 16))
  52. /**
  53. * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
  54. *
  55. * @reg: Offset within the syscon of the register containing this field
  56. * @width: Number of bits for this field
  57. * @shift: Bit offset within @reg of this field (or -1 if not avail)
  58. */
  59. struct sdhci_arasan_soc_ctl_field {
  60. u32 reg;
  61. u16 width;
  62. s16 shift;
  63. };
  64. /**
  65. * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
  66. *
  67. * @baseclkfreq: Where to find corecfg_baseclkfreq
  68. * @clockmultiplier: Where to find corecfg_clockmultiplier
  69. * @support64b: Where to find SUPPORT64B bit
  70. * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
  71. *
  72. * It's up to the licensee of the Arsan IP block to make these available
  73. * somewhere if needed. Presumably these will be scattered somewhere that's
  74. * accessible via the syscon API.
  75. */
  76. struct sdhci_arasan_soc_ctl_map {
  77. struct sdhci_arasan_soc_ctl_field baseclkfreq;
  78. struct sdhci_arasan_soc_ctl_field clockmultiplier;
  79. struct sdhci_arasan_soc_ctl_field support64b;
  80. bool hiword_update;
  81. };
  82. /**
  83. * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
  84. *
  85. * @sdcardclk_ops: The output clock related operations
  86. * @sampleclk_ops: The sample clock related operations
  87. */
  88. struct sdhci_arasan_clk_ops {
  89. const struct clk_ops *sdcardclk_ops;
  90. const struct clk_ops *sampleclk_ops;
  91. };
  92. /**
  93. * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
  94. *
  95. * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
  96. * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
  97. * @sampleclk_hw: Struct for the clock we might provide to a PHY.
  98. * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw.
  99. * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes
  100. * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes
  101. * @set_clk_delays: Function pointer for setting Clock Delays
  102. * @clk_of_data: Platform specific runtime clock data storage pointer
  103. */
  104. struct sdhci_arasan_clk_data {
  105. struct clk_hw sdcardclk_hw;
  106. struct clk *sdcardclk;
  107. struct clk_hw sampleclk_hw;
  108. struct clk *sampleclk;
  109. int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
  110. int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
  111. void (*set_clk_delays)(struct sdhci_host *host);
  112. void *clk_of_data;
  113. };
  114. /**
  115. * struct sdhci_arasan_data - Arasan Controller Data
  116. *
  117. * @host: Pointer to the main SDHCI host structure.
  118. * @clk_ahb: Pointer to the AHB clock
  119. * @phy: Pointer to the generic phy
  120. * @is_phy_on: True if the PHY is on; false if not.
  121. * @has_cqe: True if controller has command queuing engine.
  122. * @clk_data: Struct for the Arasan Controller Clock Data.
  123. * @clk_ops: Struct for the Arasan Controller Clock Operations.
  124. * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
  125. * @soc_ctl_map: Map to get offsets into soc_ctl registers.
  126. * @quirks: Arasan deviations from spec.
  127. */
  128. struct sdhci_arasan_data {
  129. struct sdhci_host *host;
  130. struct clk *clk_ahb;
  131. struct phy *phy;
  132. bool is_phy_on;
  133. bool has_cqe;
  134. struct sdhci_arasan_clk_data clk_data;
  135. const struct sdhci_arasan_clk_ops *clk_ops;
  136. struct regmap *soc_ctl_base;
  137. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  138. unsigned int quirks;
  139. /* Controller does not have CD wired and will not function normally without */
  140. #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
  141. /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
  142. * internal clock even when the clock isn't stable */
  143. #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
  144. /*
  145. * Some of the Arasan variations might not have timing requirements
  146. * met at 25MHz for Default Speed mode, those controllers work at
  147. * 19MHz instead
  148. */
  149. #define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
  150. };
  151. struct sdhci_arasan_of_data {
  152. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  153. const struct sdhci_pltfm_data *pdata;
  154. const struct sdhci_arasan_clk_ops *clk_ops;
  155. };
  156. static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
  157. .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
  158. .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
  159. .hiword_update = true,
  160. };
  161. static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
  162. .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
  163. .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
  164. .hiword_update = false,
  165. };
  166. static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
  167. .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
  168. .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
  169. .hiword_update = false,
  170. };
  171. static const struct sdhci_arasan_soc_ctl_map thunderbay_soc_ctl_map = {
  172. .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
  173. .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
  174. .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
  175. .hiword_update = false,
  176. };
  177. static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
  178. .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
  179. .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
  180. .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
  181. .hiword_update = false,
  182. };
  183. /**
  184. * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
  185. *
  186. * @host: The sdhci_host
  187. * @fld: The field to write to
  188. * @val: The value to write
  189. *
  190. * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
  191. * Note that if a field is specified as not available (shift < 0) then
  192. * this function will silently return an error code. It will be noisy
  193. * and print errors for any other (unexpected) errors.
  194. *
  195. * Return: 0 on success and error value on error
  196. */
  197. static int sdhci_arasan_syscon_write(struct sdhci_host *host,
  198. const struct sdhci_arasan_soc_ctl_field *fld,
  199. u32 val)
  200. {
  201. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  202. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  203. struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
  204. u32 reg = fld->reg;
  205. u16 width = fld->width;
  206. s16 shift = fld->shift;
  207. int ret;
  208. /*
  209. * Silently return errors for shift < 0 so caller doesn't have
  210. * to check for fields which are optional. For fields that
  211. * are required then caller needs to do something special
  212. * anyway.
  213. */
  214. if (shift < 0)
  215. return -EINVAL;
  216. if (sdhci_arasan->soc_ctl_map->hiword_update)
  217. ret = regmap_write(soc_ctl_base, reg,
  218. HIWORD_UPDATE(val, GENMASK(width, 0),
  219. shift));
  220. else
  221. ret = regmap_update_bits(soc_ctl_base, reg,
  222. GENMASK(shift + width, shift),
  223. val << shift);
  224. /* Yell about (unexpected) regmap errors */
  225. if (ret)
  226. pr_warn("%s: Regmap write fail: %d\n",
  227. mmc_hostname(host->mmc), ret);
  228. return ret;
  229. }
  230. static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
  231. {
  232. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  233. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  234. struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
  235. bool ctrl_phy = false;
  236. if (!IS_ERR(sdhci_arasan->phy)) {
  237. if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
  238. /*
  239. * If PHY off, set clock to max speed and power PHY on.
  240. *
  241. * Although PHY docs apparently suggest power cycling
  242. * when changing the clock the PHY doesn't like to be
  243. * powered on while at low speeds like those used in ID
  244. * mode. Even worse is powering the PHY on while the
  245. * clock is off.
  246. *
  247. * To workaround the PHY limitations, the best we can
  248. * do is to power it on at a faster speed and then slam
  249. * through low speeds without power cycling.
  250. */
  251. sdhci_set_clock(host, host->max_clk);
  252. if (phy_power_on(sdhci_arasan->phy)) {
  253. pr_err("%s: Cannot power on phy.\n",
  254. mmc_hostname(host->mmc));
  255. return;
  256. }
  257. sdhci_arasan->is_phy_on = true;
  258. /*
  259. * We'll now fall through to the below case with
  260. * ctrl_phy = false (so we won't turn off/on). The
  261. * sdhci_set_clock() will set the real clock.
  262. */
  263. } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
  264. /*
  265. * At higher clock speeds the PHY is fine being power
  266. * cycled and docs say you _should_ power cycle when
  267. * changing clock speeds.
  268. */
  269. ctrl_phy = true;
  270. }
  271. }
  272. if (ctrl_phy && sdhci_arasan->is_phy_on) {
  273. phy_power_off(sdhci_arasan->phy);
  274. sdhci_arasan->is_phy_on = false;
  275. }
  276. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
  277. /*
  278. * Some of the Arasan variations might not have timing
  279. * requirements met at 25MHz for Default Speed mode,
  280. * those controllers work at 19MHz instead.
  281. */
  282. if (clock == DEFAULT_SPEED_MAX_DTR)
  283. clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
  284. }
  285. /* Set the Input and Output Clock Phase Delays */
  286. if (clk_data->set_clk_delays)
  287. clk_data->set_clk_delays(host);
  288. sdhci_set_clock(host, clock);
  289. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
  290. /*
  291. * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
  292. * after enabling the clock even though the clock is not
  293. * stable. Trying to use a clock without waiting here results
  294. * in EILSEQ while detecting some older/slower cards. The
  295. * chosen delay is the maximum delay from sdhci_set_clock.
  296. */
  297. msleep(20);
  298. if (ctrl_phy) {
  299. if (phy_power_on(sdhci_arasan->phy)) {
  300. pr_err("%s: Cannot power on phy.\n",
  301. mmc_hostname(host->mmc));
  302. return;
  303. }
  304. sdhci_arasan->is_phy_on = true;
  305. }
  306. }
  307. static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
  308. struct mmc_ios *ios)
  309. {
  310. u32 vendor;
  311. struct sdhci_host *host = mmc_priv(mmc);
  312. vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
  313. if (ios->enhanced_strobe)
  314. vendor |= VENDOR_ENHANCED_STROBE;
  315. else
  316. vendor &= ~VENDOR_ENHANCED_STROBE;
  317. sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
  318. }
  319. static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
  320. {
  321. u8 ctrl;
  322. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  323. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  324. sdhci_and_cqhci_reset(host, mask);
  325. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
  326. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  327. ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
  328. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  329. }
  330. }
  331. static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
  332. struct mmc_ios *ios)
  333. {
  334. switch (ios->signal_voltage) {
  335. case MMC_SIGNAL_VOLTAGE_180:
  336. /*
  337. * Plese don't switch to 1V8 as arasan,5.1 doesn't
  338. * actually refer to this setting to indicate the
  339. * signal voltage and the state machine will be broken
  340. * actually if we force to enable 1V8. That's something
  341. * like broken quirk but we could work around here.
  342. */
  343. return 0;
  344. case MMC_SIGNAL_VOLTAGE_330:
  345. case MMC_SIGNAL_VOLTAGE_120:
  346. /* We don't support 3V3 and 1V2 */
  347. break;
  348. }
  349. return -EINVAL;
  350. }
  351. static const struct sdhci_ops sdhci_arasan_ops = {
  352. .set_clock = sdhci_arasan_set_clock,
  353. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  354. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  355. .set_bus_width = sdhci_set_bus_width,
  356. .reset = sdhci_arasan_reset,
  357. .set_uhs_signaling = sdhci_set_uhs_signaling,
  358. .set_power = sdhci_set_power_and_bus_voltage,
  359. };
  360. static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
  361. {
  362. int cmd_error = 0;
  363. int data_error = 0;
  364. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  365. return intmask;
  366. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  367. return 0;
  368. }
  369. static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
  370. {
  371. sdhci_dumpregs(mmc_priv(mmc));
  372. }
  373. static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
  374. {
  375. struct sdhci_host *host = mmc_priv(mmc);
  376. u32 reg;
  377. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  378. while (reg & SDHCI_DATA_AVAILABLE) {
  379. sdhci_readl(host, SDHCI_BUFFER);
  380. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  381. }
  382. sdhci_cqe_enable(mmc);
  383. }
  384. static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
  385. .enable = sdhci_arasan_cqe_enable,
  386. .disable = sdhci_cqe_disable,
  387. .dumpregs = sdhci_arasan_dumpregs,
  388. };
  389. static const struct sdhci_ops sdhci_arasan_cqe_ops = {
  390. .set_clock = sdhci_arasan_set_clock,
  391. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  392. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  393. .set_bus_width = sdhci_set_bus_width,
  394. .reset = sdhci_arasan_reset,
  395. .set_uhs_signaling = sdhci_set_uhs_signaling,
  396. .set_power = sdhci_set_power_and_bus_voltage,
  397. .irq = sdhci_arasan_cqhci_irq,
  398. };
  399. static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
  400. .ops = &sdhci_arasan_cqe_ops,
  401. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  402. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  403. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
  404. };
  405. static const struct sdhci_pltfm_data sdhci_arasan_thunderbay_pdata = {
  406. .ops = &sdhci_arasan_cqe_ops,
  407. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  408. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  409. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  410. SDHCI_QUIRK2_STOP_WITH_TC |
  411. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
  412. };
  413. #ifdef CONFIG_PM_SLEEP
  414. /**
  415. * sdhci_arasan_suspend - Suspend method for the driver
  416. * @dev: Address of the device structure
  417. *
  418. * Put the device in a low power state.
  419. *
  420. * Return: 0 on success and error value on error
  421. */
  422. static int sdhci_arasan_suspend(struct device *dev)
  423. {
  424. struct sdhci_host *host = dev_get_drvdata(dev);
  425. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  426. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  427. int ret;
  428. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  429. mmc_retune_needed(host->mmc);
  430. if (sdhci_arasan->has_cqe) {
  431. ret = cqhci_suspend(host->mmc);
  432. if (ret)
  433. return ret;
  434. }
  435. ret = sdhci_suspend_host(host);
  436. if (ret)
  437. return ret;
  438. if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
  439. ret = phy_power_off(sdhci_arasan->phy);
  440. if (ret) {
  441. dev_err(dev, "Cannot power off phy.\n");
  442. if (sdhci_resume_host(host))
  443. dev_err(dev, "Cannot resume host.\n");
  444. return ret;
  445. }
  446. sdhci_arasan->is_phy_on = false;
  447. }
  448. clk_disable(pltfm_host->clk);
  449. clk_disable(sdhci_arasan->clk_ahb);
  450. return 0;
  451. }
  452. /**
  453. * sdhci_arasan_resume - Resume method for the driver
  454. * @dev: Address of the device structure
  455. *
  456. * Resume operation after suspend
  457. *
  458. * Return: 0 on success and error value on error
  459. */
  460. static int sdhci_arasan_resume(struct device *dev)
  461. {
  462. struct sdhci_host *host = dev_get_drvdata(dev);
  463. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  464. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  465. int ret;
  466. ret = clk_enable(sdhci_arasan->clk_ahb);
  467. if (ret) {
  468. dev_err(dev, "Cannot enable AHB clock.\n");
  469. return ret;
  470. }
  471. ret = clk_enable(pltfm_host->clk);
  472. if (ret) {
  473. dev_err(dev, "Cannot enable SD clock.\n");
  474. return ret;
  475. }
  476. if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
  477. ret = phy_power_on(sdhci_arasan->phy);
  478. if (ret) {
  479. dev_err(dev, "Cannot power on phy.\n");
  480. return ret;
  481. }
  482. sdhci_arasan->is_phy_on = true;
  483. }
  484. ret = sdhci_resume_host(host);
  485. if (ret) {
  486. dev_err(dev, "Cannot resume host.\n");
  487. return ret;
  488. }
  489. if (sdhci_arasan->has_cqe)
  490. return cqhci_resume(host->mmc);
  491. return 0;
  492. }
  493. #endif /* ! CONFIG_PM_SLEEP */
  494. static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
  495. sdhci_arasan_resume);
  496. /**
  497. * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
  498. *
  499. * @hw: Pointer to the hardware clock structure.
  500. * @parent_rate: The parent rate (should be rate of clk_xin).
  501. *
  502. * Return the current actual rate of the SD card clock. This can be used
  503. * to communicate with out PHY.
  504. *
  505. * Return: The card clock rate.
  506. */
  507. static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
  508. unsigned long parent_rate)
  509. {
  510. struct sdhci_arasan_clk_data *clk_data =
  511. container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
  512. struct sdhci_arasan_data *sdhci_arasan =
  513. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  514. struct sdhci_host *host = sdhci_arasan->host;
  515. return host->mmc->actual_clock;
  516. }
  517. static const struct clk_ops arasan_sdcardclk_ops = {
  518. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  519. };
  520. /**
  521. * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
  522. *
  523. * @hw: Pointer to the hardware clock structure.
  524. * @parent_rate: The parent rate (should be rate of clk_xin).
  525. *
  526. * Return the current actual rate of the sampling clock. This can be used
  527. * to communicate with out PHY.
  528. *
  529. * Return: The sample clock rate.
  530. */
  531. static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
  532. unsigned long parent_rate)
  533. {
  534. struct sdhci_arasan_clk_data *clk_data =
  535. container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
  536. struct sdhci_arasan_data *sdhci_arasan =
  537. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  538. struct sdhci_host *host = sdhci_arasan->host;
  539. return host->mmc->actual_clock;
  540. }
  541. static const struct clk_ops arasan_sampleclk_ops = {
  542. .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
  543. };
  544. /**
  545. * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
  546. *
  547. * @hw: Pointer to the hardware clock structure.
  548. * @degrees: The clock phase shift between 0 - 359.
  549. *
  550. * Set the SD Output Clock Tap Delays for Output path
  551. *
  552. * Return: 0 on success and error value on error
  553. */
  554. static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
  555. {
  556. struct sdhci_arasan_clk_data *clk_data =
  557. container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
  558. struct sdhci_arasan_data *sdhci_arasan =
  559. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  560. struct sdhci_host *host = sdhci_arasan->host;
  561. const char *clk_name = clk_hw_get_name(hw);
  562. u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
  563. u8 tap_delay, tap_max = 0;
  564. int ret;
  565. /* This is applicable for SDHCI_SPEC_300 and above */
  566. if (host->version < SDHCI_SPEC_300)
  567. return 0;
  568. switch (host->timing) {
  569. case MMC_TIMING_MMC_HS:
  570. case MMC_TIMING_SD_HS:
  571. case MMC_TIMING_UHS_SDR25:
  572. case MMC_TIMING_UHS_DDR50:
  573. case MMC_TIMING_MMC_DDR52:
  574. /* For 50MHz clock, 30 Taps are available */
  575. tap_max = 30;
  576. break;
  577. case MMC_TIMING_UHS_SDR50:
  578. /* For 100MHz clock, 15 Taps are available */
  579. tap_max = 15;
  580. break;
  581. case MMC_TIMING_UHS_SDR104:
  582. case MMC_TIMING_MMC_HS200:
  583. /* For 200MHz clock, 8 Taps are available */
  584. tap_max = 8;
  585. break;
  586. default:
  587. break;
  588. }
  589. tap_delay = (degrees * tap_max) / 360;
  590. /* Set the Clock Phase */
  591. ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
  592. if (ret)
  593. pr_err("Error setting Output Tap Delay\n");
  594. /* Release DLL Reset */
  595. zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
  596. return ret;
  597. }
  598. static const struct clk_ops zynqmp_sdcardclk_ops = {
  599. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  600. .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
  601. };
  602. /**
  603. * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
  604. *
  605. * @hw: Pointer to the hardware clock structure.
  606. * @degrees: The clock phase shift between 0 - 359.
  607. *
  608. * Set the SD Input Clock Tap Delays for Input path
  609. *
  610. * Return: 0 on success and error value on error
  611. */
  612. static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
  613. {
  614. struct sdhci_arasan_clk_data *clk_data =
  615. container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
  616. struct sdhci_arasan_data *sdhci_arasan =
  617. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  618. struct sdhci_host *host = sdhci_arasan->host;
  619. const char *clk_name = clk_hw_get_name(hw);
  620. u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
  621. u8 tap_delay, tap_max = 0;
  622. int ret;
  623. /* This is applicable for SDHCI_SPEC_300 and above */
  624. if (host->version < SDHCI_SPEC_300)
  625. return 0;
  626. /* Assert DLL Reset */
  627. zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
  628. switch (host->timing) {
  629. case MMC_TIMING_MMC_HS:
  630. case MMC_TIMING_SD_HS:
  631. case MMC_TIMING_UHS_SDR25:
  632. case MMC_TIMING_UHS_DDR50:
  633. case MMC_TIMING_MMC_DDR52:
  634. /* For 50MHz clock, 120 Taps are available */
  635. tap_max = 120;
  636. break;
  637. case MMC_TIMING_UHS_SDR50:
  638. /* For 100MHz clock, 60 Taps are available */
  639. tap_max = 60;
  640. break;
  641. case MMC_TIMING_UHS_SDR104:
  642. case MMC_TIMING_MMC_HS200:
  643. /* For 200MHz clock, 30 Taps are available */
  644. tap_max = 30;
  645. break;
  646. default:
  647. break;
  648. }
  649. tap_delay = (degrees * tap_max) / 360;
  650. /* Set the Clock Phase */
  651. ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
  652. if (ret)
  653. pr_err("Error setting Input Tap Delay\n");
  654. return ret;
  655. }
  656. static const struct clk_ops zynqmp_sampleclk_ops = {
  657. .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
  658. .set_phase = sdhci_zynqmp_sampleclk_set_phase,
  659. };
  660. /**
  661. * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
  662. *
  663. * @hw: Pointer to the hardware clock structure.
  664. * @degrees: The clock phase shift between 0 - 359.
  665. *
  666. * Set the SD Output Clock Tap Delays for Output path
  667. *
  668. * Return: 0 on success and error value on error
  669. */
  670. static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
  671. {
  672. struct sdhci_arasan_clk_data *clk_data =
  673. container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
  674. struct sdhci_arasan_data *sdhci_arasan =
  675. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  676. struct sdhci_host *host = sdhci_arasan->host;
  677. u8 tap_delay, tap_max = 0;
  678. /* This is applicable for SDHCI_SPEC_300 and above */
  679. if (host->version < SDHCI_SPEC_300)
  680. return 0;
  681. switch (host->timing) {
  682. case MMC_TIMING_MMC_HS:
  683. case MMC_TIMING_SD_HS:
  684. case MMC_TIMING_UHS_SDR25:
  685. case MMC_TIMING_UHS_DDR50:
  686. case MMC_TIMING_MMC_DDR52:
  687. /* For 50MHz clock, 30 Taps are available */
  688. tap_max = 30;
  689. break;
  690. case MMC_TIMING_UHS_SDR50:
  691. /* For 100MHz clock, 15 Taps are available */
  692. tap_max = 15;
  693. break;
  694. case MMC_TIMING_UHS_SDR104:
  695. case MMC_TIMING_MMC_HS200:
  696. /* For 200MHz clock, 8 Taps are available */
  697. tap_max = 8;
  698. break;
  699. default:
  700. break;
  701. }
  702. tap_delay = (degrees * tap_max) / 360;
  703. /* Set the Clock Phase */
  704. if (tap_delay) {
  705. u32 regval;
  706. regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
  707. regval |= SDHCI_OTAPDLY_ENABLE;
  708. sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
  709. regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
  710. regval |= tap_delay;
  711. sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
  712. }
  713. return 0;
  714. }
  715. static const struct clk_ops versal_sdcardclk_ops = {
  716. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  717. .set_phase = sdhci_versal_sdcardclk_set_phase,
  718. };
  719. /**
  720. * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
  721. *
  722. * @hw: Pointer to the hardware clock structure.
  723. * @degrees: The clock phase shift between 0 - 359.
  724. *
  725. * Set the SD Input Clock Tap Delays for Input path
  726. *
  727. * Return: 0 on success and error value on error
  728. */
  729. static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
  730. {
  731. struct sdhci_arasan_clk_data *clk_data =
  732. container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
  733. struct sdhci_arasan_data *sdhci_arasan =
  734. container_of(clk_data, struct sdhci_arasan_data, clk_data);
  735. struct sdhci_host *host = sdhci_arasan->host;
  736. u8 tap_delay, tap_max = 0;
  737. /* This is applicable for SDHCI_SPEC_300 and above */
  738. if (host->version < SDHCI_SPEC_300)
  739. return 0;
  740. switch (host->timing) {
  741. case MMC_TIMING_MMC_HS:
  742. case MMC_TIMING_SD_HS:
  743. case MMC_TIMING_UHS_SDR25:
  744. case MMC_TIMING_UHS_DDR50:
  745. case MMC_TIMING_MMC_DDR52:
  746. /* For 50MHz clock, 120 Taps are available */
  747. tap_max = 120;
  748. break;
  749. case MMC_TIMING_UHS_SDR50:
  750. /* For 100MHz clock, 60 Taps are available */
  751. tap_max = 60;
  752. break;
  753. case MMC_TIMING_UHS_SDR104:
  754. case MMC_TIMING_MMC_HS200:
  755. /* For 200MHz clock, 30 Taps are available */
  756. tap_max = 30;
  757. break;
  758. default:
  759. break;
  760. }
  761. tap_delay = (degrees * tap_max) / 360;
  762. /* Set the Clock Phase */
  763. if (tap_delay) {
  764. u32 regval;
  765. regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
  766. regval |= SDHCI_ITAPDLY_CHGWIN;
  767. sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
  768. regval |= SDHCI_ITAPDLY_ENABLE;
  769. sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
  770. regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
  771. regval |= tap_delay;
  772. sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
  773. regval &= ~SDHCI_ITAPDLY_CHGWIN;
  774. sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
  775. }
  776. return 0;
  777. }
  778. static const struct clk_ops versal_sampleclk_ops = {
  779. .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
  780. .set_phase = sdhci_versal_sampleclk_set_phase,
  781. };
  782. static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
  783. {
  784. u16 clk;
  785. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  786. clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
  787. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  788. /* Issue DLL Reset */
  789. zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
  790. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  791. sdhci_enable_clk(host, clk);
  792. }
  793. static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
  794. {
  795. struct sdhci_host *host = mmc_priv(mmc);
  796. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  797. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  798. struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
  799. const char *clk_name = clk_hw_get_name(hw);
  800. u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
  801. NODE_SD_1;
  802. int err;
  803. /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
  804. if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
  805. return 0;
  806. arasan_zynqmp_dll_reset(host, device_id);
  807. err = sdhci_execute_tuning(mmc, opcode);
  808. if (err)
  809. return err;
  810. arasan_zynqmp_dll_reset(host, device_id);
  811. return 0;
  812. }
  813. /**
  814. * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
  815. *
  816. * @host: The sdhci_host
  817. * @value: The value to write
  818. *
  819. * The corecfg_clockmultiplier is supposed to contain clock multiplier
  820. * value of programmable clock generator.
  821. *
  822. * NOTES:
  823. * - Many existing devices don't seem to do this and work fine. To keep
  824. * compatibility for old hardware where the device tree doesn't provide a
  825. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  826. * for this platform.
  827. * - The value of corecfg_clockmultiplier should sync with that of corresponding
  828. * value reading from sdhci_capability_register. So this function is called
  829. * once at probe time and never called again.
  830. */
  831. static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
  832. u32 value)
  833. {
  834. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  835. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  836. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  837. sdhci_arasan->soc_ctl_map;
  838. /* Having a map is optional */
  839. if (!soc_ctl_map)
  840. return;
  841. /* If we have a map, we expect to have a syscon */
  842. if (!sdhci_arasan->soc_ctl_base) {
  843. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  844. mmc_hostname(host->mmc));
  845. return;
  846. }
  847. sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
  848. }
  849. /**
  850. * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
  851. *
  852. * @host: The sdhci_host
  853. *
  854. * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
  855. * function can be used to make that happen.
  856. *
  857. * NOTES:
  858. * - Many existing devices don't seem to do this and work fine. To keep
  859. * compatibility for old hardware where the device tree doesn't provide a
  860. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  861. * for this platform.
  862. * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
  863. * to achieve lower clock rates. That means that this function is called once
  864. * at probe time and never called again.
  865. */
  866. static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
  867. {
  868. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  869. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  870. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  871. sdhci_arasan->soc_ctl_map;
  872. u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
  873. /* Having a map is optional */
  874. if (!soc_ctl_map)
  875. return;
  876. /* If we have a map, we expect to have a syscon */
  877. if (!sdhci_arasan->soc_ctl_base) {
  878. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  879. mmc_hostname(host->mmc));
  880. return;
  881. }
  882. sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
  883. }
  884. static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
  885. {
  886. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  887. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  888. struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
  889. clk_set_phase(clk_data->sampleclk,
  890. clk_data->clk_phase_in[host->timing]);
  891. clk_set_phase(clk_data->sdcardclk,
  892. clk_data->clk_phase_out[host->timing]);
  893. }
  894. static void arasan_dt_read_clk_phase(struct device *dev,
  895. struct sdhci_arasan_clk_data *clk_data,
  896. unsigned int timing, const char *prop)
  897. {
  898. struct device_node *np = dev->of_node;
  899. u32 clk_phase[2] = {0};
  900. int ret;
  901. /*
  902. * Read Tap Delay values from DT, if the DT does not contain the
  903. * Tap Values then use the pre-defined values.
  904. */
  905. ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
  906. 2, 0);
  907. if (ret < 0) {
  908. dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
  909. prop, clk_data->clk_phase_in[timing],
  910. clk_data->clk_phase_out[timing]);
  911. return;
  912. }
  913. /* The values read are Input and Output Clock Delays in order */
  914. clk_data->clk_phase_in[timing] = clk_phase[0];
  915. clk_data->clk_phase_out[timing] = clk_phase[1];
  916. }
  917. /**
  918. * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
  919. *
  920. * @dev: Pointer to our struct device.
  921. * @clk_data: Pointer to the Clock Data structure
  922. *
  923. * Called at initialization to parse the values of Clock Delays.
  924. */
  925. static void arasan_dt_parse_clk_phases(struct device *dev,
  926. struct sdhci_arasan_clk_data *clk_data)
  927. {
  928. u32 mio_bank = 0;
  929. int i;
  930. /*
  931. * This has been kept as a pointer and is assigned a function here.
  932. * So that different controller variants can assign their own handling
  933. * function.
  934. */
  935. clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
  936. if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
  937. u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
  938. ZYNQMP_ICLK_PHASE;
  939. u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
  940. ZYNQMP_OCLK_PHASE;
  941. of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
  942. if (mio_bank == 2) {
  943. zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
  944. zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
  945. }
  946. for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
  947. clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
  948. clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
  949. }
  950. }
  951. if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
  952. u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
  953. VERSAL_ICLK_PHASE;
  954. u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
  955. VERSAL_OCLK_PHASE;
  956. for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
  957. clk_data->clk_phase_in[i] = versal_iclk_phase[i];
  958. clk_data->clk_phase_out[i] = versal_oclk_phase[i];
  959. }
  960. }
  961. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
  962. "clk-phase-legacy");
  963. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
  964. "clk-phase-mmc-hs");
  965. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
  966. "clk-phase-sd-hs");
  967. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
  968. "clk-phase-uhs-sdr12");
  969. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
  970. "clk-phase-uhs-sdr25");
  971. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
  972. "clk-phase-uhs-sdr50");
  973. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
  974. "clk-phase-uhs-sdr104");
  975. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
  976. "clk-phase-uhs-ddr50");
  977. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
  978. "clk-phase-mmc-ddr52");
  979. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
  980. "clk-phase-mmc-hs200");
  981. arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
  982. "clk-phase-mmc-hs400");
  983. }
  984. static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
  985. .ops = &sdhci_arasan_ops,
  986. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  987. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  988. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  989. SDHCI_QUIRK2_STOP_WITH_TC,
  990. };
  991. static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
  992. .sdcardclk_ops = &arasan_sdcardclk_ops,
  993. .sampleclk_ops = &arasan_sampleclk_ops,
  994. };
  995. static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
  996. .pdata = &sdhci_arasan_pdata,
  997. .clk_ops = &arasan_clk_ops,
  998. };
  999. static const struct sdhci_arasan_of_data sdhci_arasan_thunderbay_data = {
  1000. .soc_ctl_map = &thunderbay_soc_ctl_map,
  1001. .pdata = &sdhci_arasan_thunderbay_pdata,
  1002. .clk_ops = &arasan_clk_ops,
  1003. };
  1004. static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
  1005. .ops = &sdhci_arasan_cqe_ops,
  1006. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  1007. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  1008. SDHCI_QUIRK_NO_LED |
  1009. SDHCI_QUIRK_32BIT_DMA_ADDR |
  1010. SDHCI_QUIRK_32BIT_DMA_SIZE |
  1011. SDHCI_QUIRK_32BIT_ADMA_SIZE,
  1012. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1013. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  1014. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
  1015. SDHCI_QUIRK2_STOP_WITH_TC |
  1016. SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  1017. };
  1018. static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
  1019. .ops = &sdhci_arasan_ops,
  1020. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  1021. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  1022. SDHCI_QUIRK_NO_LED |
  1023. SDHCI_QUIRK_32BIT_DMA_ADDR |
  1024. SDHCI_QUIRK_32BIT_DMA_SIZE |
  1025. SDHCI_QUIRK_32BIT_ADMA_SIZE,
  1026. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1027. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  1028. SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  1029. SDHCI_QUIRK2_STOP_WITH_TC |
  1030. SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  1031. };
  1032. static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
  1033. .ops = &sdhci_arasan_ops,
  1034. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  1035. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  1036. SDHCI_QUIRK_NO_LED |
  1037. SDHCI_QUIRK_32BIT_DMA_ADDR |
  1038. SDHCI_QUIRK_32BIT_DMA_SIZE |
  1039. SDHCI_QUIRK_32BIT_ADMA_SIZE,
  1040. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1041. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  1042. SDHCI_QUIRK2_HOST_OFF_CARD_ON |
  1043. SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  1044. };
  1045. static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
  1046. .soc_ctl_map = &rk3399_soc_ctl_map,
  1047. .pdata = &sdhci_arasan_cqe_pdata,
  1048. .clk_ops = &arasan_clk_ops,
  1049. };
  1050. static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
  1051. .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
  1052. .pdata = &sdhci_arasan_cqe_pdata,
  1053. .clk_ops = &arasan_clk_ops,
  1054. };
  1055. static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
  1056. .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
  1057. .pdata = &sdhci_arasan_cqe_pdata,
  1058. .clk_ops = &arasan_clk_ops,
  1059. };
  1060. static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
  1061. .ops = &sdhci_arasan_ops,
  1062. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  1063. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  1064. SDHCI_QUIRK2_STOP_WITH_TC,
  1065. };
  1066. static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
  1067. .sdcardclk_ops = &zynqmp_sdcardclk_ops,
  1068. .sampleclk_ops = &zynqmp_sampleclk_ops,
  1069. };
  1070. static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
  1071. .pdata = &sdhci_arasan_zynqmp_pdata,
  1072. .clk_ops = &zynqmp_clk_ops,
  1073. };
  1074. static const struct sdhci_arasan_clk_ops versal_clk_ops = {
  1075. .sdcardclk_ops = &versal_sdcardclk_ops,
  1076. .sampleclk_ops = &versal_sampleclk_ops,
  1077. };
  1078. static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
  1079. .pdata = &sdhci_arasan_zynqmp_pdata,
  1080. .clk_ops = &versal_clk_ops,
  1081. };
  1082. static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
  1083. .soc_ctl_map = &intel_keembay_soc_ctl_map,
  1084. .pdata = &sdhci_keembay_emmc_pdata,
  1085. .clk_ops = &arasan_clk_ops,
  1086. };
  1087. static struct sdhci_arasan_of_data intel_keembay_sd_data = {
  1088. .soc_ctl_map = &intel_keembay_soc_ctl_map,
  1089. .pdata = &sdhci_keembay_sd_pdata,
  1090. .clk_ops = &arasan_clk_ops,
  1091. };
  1092. static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
  1093. .soc_ctl_map = &intel_keembay_soc_ctl_map,
  1094. .pdata = &sdhci_keembay_sdio_pdata,
  1095. .clk_ops = &arasan_clk_ops,
  1096. };
  1097. static const struct of_device_id sdhci_arasan_of_match[] = {
  1098. /* SoC-specific compatible strings w/ soc_ctl_map */
  1099. {
  1100. .compatible = "rockchip,rk3399-sdhci-5.1",
  1101. .data = &sdhci_arasan_rk3399_data,
  1102. },
  1103. {
  1104. .compatible = "intel,lgm-sdhci-5.1-emmc",
  1105. .data = &intel_lgm_emmc_data,
  1106. },
  1107. {
  1108. .compatible = "intel,lgm-sdhci-5.1-sdxc",
  1109. .data = &intel_lgm_sdxc_data,
  1110. },
  1111. {
  1112. .compatible = "intel,keembay-sdhci-5.1-emmc",
  1113. .data = &intel_keembay_emmc_data,
  1114. },
  1115. {
  1116. .compatible = "intel,keembay-sdhci-5.1-sd",
  1117. .data = &intel_keembay_sd_data,
  1118. },
  1119. {
  1120. .compatible = "intel,keembay-sdhci-5.1-sdio",
  1121. .data = &intel_keembay_sdio_data,
  1122. },
  1123. {
  1124. .compatible = "intel,thunderbay-sdhci-5.1",
  1125. .data = &sdhci_arasan_thunderbay_data,
  1126. },
  1127. /* Generic compatible below here */
  1128. {
  1129. .compatible = "arasan,sdhci-8.9a",
  1130. .data = &sdhci_arasan_generic_data,
  1131. },
  1132. {
  1133. .compatible = "arasan,sdhci-5.1",
  1134. .data = &sdhci_arasan_generic_data,
  1135. },
  1136. {
  1137. .compatible = "arasan,sdhci-4.9a",
  1138. .data = &sdhci_arasan_generic_data,
  1139. },
  1140. {
  1141. .compatible = "xlnx,zynqmp-8.9a",
  1142. .data = &sdhci_arasan_zynqmp_data,
  1143. },
  1144. {
  1145. .compatible = "xlnx,versal-8.9a",
  1146. .data = &sdhci_arasan_versal_data,
  1147. },
  1148. { /* sentinel */ }
  1149. };
  1150. MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
  1151. /**
  1152. * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
  1153. *
  1154. * @sdhci_arasan: Our private data structure.
  1155. * @clk_xin: Pointer to the functional clock
  1156. * @dev: Pointer to our struct device.
  1157. *
  1158. * Some PHY devices need to know what the actual card clock is. In order for
  1159. * them to find out, we'll provide a clock through the common clock framework
  1160. * for them to query.
  1161. *
  1162. * Return: 0 on success and error value on error
  1163. */
  1164. static int
  1165. sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
  1166. struct clk *clk_xin,
  1167. struct device *dev)
  1168. {
  1169. struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
  1170. struct device_node *np = dev->of_node;
  1171. struct clk_init_data sdcardclk_init;
  1172. const char *parent_clk_name;
  1173. int ret;
  1174. ret = of_property_read_string_index(np, "clock-output-names", 0,
  1175. &sdcardclk_init.name);
  1176. if (ret) {
  1177. dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
  1178. return ret;
  1179. }
  1180. parent_clk_name = __clk_get_name(clk_xin);
  1181. sdcardclk_init.parent_names = &parent_clk_name;
  1182. sdcardclk_init.num_parents = 1;
  1183. sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
  1184. sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
  1185. clk_data->sdcardclk_hw.init = &sdcardclk_init;
  1186. clk_data->sdcardclk =
  1187. devm_clk_register(dev, &clk_data->sdcardclk_hw);
  1188. if (IS_ERR(clk_data->sdcardclk))
  1189. return PTR_ERR(clk_data->sdcardclk);
  1190. clk_data->sdcardclk_hw.init = NULL;
  1191. ret = of_clk_add_provider(np, of_clk_src_simple_get,
  1192. clk_data->sdcardclk);
  1193. if (ret)
  1194. dev_err(dev, "Failed to add sdcard clock provider\n");
  1195. return ret;
  1196. }
  1197. /**
  1198. * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
  1199. *
  1200. * @sdhci_arasan: Our private data structure.
  1201. * @clk_xin: Pointer to the functional clock
  1202. * @dev: Pointer to our struct device.
  1203. *
  1204. * Some PHY devices need to know what the actual card clock is. In order for
  1205. * them to find out, we'll provide a clock through the common clock framework
  1206. * for them to query.
  1207. *
  1208. * Return: 0 on success and error value on error
  1209. */
  1210. static int
  1211. sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
  1212. struct clk *clk_xin,
  1213. struct device *dev)
  1214. {
  1215. struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
  1216. struct device_node *np = dev->of_node;
  1217. struct clk_init_data sampleclk_init;
  1218. const char *parent_clk_name;
  1219. int ret;
  1220. ret = of_property_read_string_index(np, "clock-output-names", 1,
  1221. &sampleclk_init.name);
  1222. if (ret) {
  1223. dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
  1224. return ret;
  1225. }
  1226. parent_clk_name = __clk_get_name(clk_xin);
  1227. sampleclk_init.parent_names = &parent_clk_name;
  1228. sampleclk_init.num_parents = 1;
  1229. sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
  1230. sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
  1231. clk_data->sampleclk_hw.init = &sampleclk_init;
  1232. clk_data->sampleclk =
  1233. devm_clk_register(dev, &clk_data->sampleclk_hw);
  1234. if (IS_ERR(clk_data->sampleclk))
  1235. return PTR_ERR(clk_data->sampleclk);
  1236. clk_data->sampleclk_hw.init = NULL;
  1237. ret = of_clk_add_provider(np, of_clk_src_simple_get,
  1238. clk_data->sampleclk);
  1239. if (ret)
  1240. dev_err(dev, "Failed to add sample clock provider\n");
  1241. return ret;
  1242. }
  1243. /**
  1244. * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
  1245. *
  1246. * @dev: Pointer to our struct device.
  1247. *
  1248. * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
  1249. * returned success.
  1250. */
  1251. static void sdhci_arasan_unregister_sdclk(struct device *dev)
  1252. {
  1253. struct device_node *np = dev->of_node;
  1254. if (!of_find_property(np, "#clock-cells", NULL))
  1255. return;
  1256. of_clk_del_provider(dev->of_node);
  1257. }
  1258. /**
  1259. * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
  1260. * @host: The sdhci_host
  1261. * @value: The value to write
  1262. *
  1263. * This should be set based on the System Address Bus.
  1264. * 0: the Core supports only 32-bit System Address Bus.
  1265. * 1: the Core supports 64-bit System Address Bus.
  1266. *
  1267. * NOTE:
  1268. * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
  1269. * Keem Bay does not support 64-bit access.
  1270. */
  1271. static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
  1272. {
  1273. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1274. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  1275. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  1276. /* Having a map is optional */
  1277. soc_ctl_map = sdhci_arasan->soc_ctl_map;
  1278. if (!soc_ctl_map)
  1279. return;
  1280. /* If we have a map, we expect to have a syscon */
  1281. if (!sdhci_arasan->soc_ctl_base) {
  1282. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  1283. mmc_hostname(host->mmc));
  1284. return;
  1285. }
  1286. sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
  1287. }
  1288. /**
  1289. * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
  1290. *
  1291. * @sdhci_arasan: Our private data structure.
  1292. * @clk_xin: Pointer to the functional clock
  1293. * @dev: Pointer to our struct device.
  1294. *
  1295. * Some PHY devices need to know what the actual card clock is. In order for
  1296. * them to find out, we'll provide a clock through the common clock framework
  1297. * for them to query.
  1298. *
  1299. * Note: without seriously re-architecting SDHCI's clock code and testing on
  1300. * all platforms, there's no way to create a totally beautiful clock here
  1301. * with all clock ops implemented. Instead, we'll just create a clock that can
  1302. * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
  1303. * framework that we're doing things behind its back. This should be sufficient
  1304. * to create nice clean device tree bindings and later (if needed) we can try
  1305. * re-architecting SDHCI if we see some benefit to it.
  1306. *
  1307. * Return: 0 on success and error value on error
  1308. */
  1309. static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
  1310. struct clk *clk_xin,
  1311. struct device *dev)
  1312. {
  1313. struct device_node *np = dev->of_node;
  1314. u32 num_clks = 0;
  1315. int ret;
  1316. /* Providing a clock to the PHY is optional; no error if missing */
  1317. if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
  1318. return 0;
  1319. ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
  1320. if (ret)
  1321. return ret;
  1322. if (num_clks) {
  1323. ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
  1324. dev);
  1325. if (ret) {
  1326. sdhci_arasan_unregister_sdclk(dev);
  1327. return ret;
  1328. }
  1329. }
  1330. return 0;
  1331. }
  1332. static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
  1333. {
  1334. struct sdhci_host *host = sdhci_arasan->host;
  1335. struct cqhci_host *cq_host;
  1336. bool dma64;
  1337. int ret;
  1338. if (!sdhci_arasan->has_cqe)
  1339. return sdhci_add_host(host);
  1340. ret = sdhci_setup_host(host);
  1341. if (ret)
  1342. return ret;
  1343. cq_host = devm_kzalloc(host->mmc->parent,
  1344. sizeof(*cq_host), GFP_KERNEL);
  1345. if (!cq_host) {
  1346. ret = -ENOMEM;
  1347. goto cleanup;
  1348. }
  1349. cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
  1350. cq_host->ops = &sdhci_arasan_cqhci_ops;
  1351. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  1352. if (dma64)
  1353. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  1354. ret = cqhci_init(cq_host, host->mmc, dma64);
  1355. if (ret)
  1356. goto cleanup;
  1357. ret = __sdhci_add_host(host);
  1358. if (ret)
  1359. goto cleanup;
  1360. return 0;
  1361. cleanup:
  1362. sdhci_cleanup_host(host);
  1363. return ret;
  1364. }
  1365. static int sdhci_arasan_probe(struct platform_device *pdev)
  1366. {
  1367. int ret;
  1368. struct device_node *node;
  1369. struct clk *clk_xin;
  1370. struct sdhci_host *host;
  1371. struct sdhci_pltfm_host *pltfm_host;
  1372. struct device *dev = &pdev->dev;
  1373. struct device_node *np = dev->of_node;
  1374. struct sdhci_arasan_data *sdhci_arasan;
  1375. const struct sdhci_arasan_of_data *data;
  1376. data = of_device_get_match_data(dev);
  1377. if (!data)
  1378. return -EINVAL;
  1379. host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
  1380. if (IS_ERR(host))
  1381. return PTR_ERR(host);
  1382. pltfm_host = sdhci_priv(host);
  1383. sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  1384. sdhci_arasan->host = host;
  1385. sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
  1386. sdhci_arasan->clk_ops = data->clk_ops;
  1387. node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
  1388. if (node) {
  1389. sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
  1390. of_node_put(node);
  1391. if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
  1392. ret = dev_err_probe(dev,
  1393. PTR_ERR(sdhci_arasan->soc_ctl_base),
  1394. "Can't get syscon\n");
  1395. goto err_pltfm_free;
  1396. }
  1397. }
  1398. sdhci_get_of_property(pdev);
  1399. sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
  1400. if (IS_ERR(sdhci_arasan->clk_ahb)) {
  1401. ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
  1402. "clk_ahb clock not found.\n");
  1403. goto err_pltfm_free;
  1404. }
  1405. clk_xin = devm_clk_get(dev, "clk_xin");
  1406. if (IS_ERR(clk_xin)) {
  1407. ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
  1408. goto err_pltfm_free;
  1409. }
  1410. ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
  1411. if (ret) {
  1412. dev_err(dev, "Unable to enable AHB clock.\n");
  1413. goto err_pltfm_free;
  1414. }
  1415. /* If clock-frequency property is set, use the provided value */
  1416. if (pltfm_host->clock &&
  1417. pltfm_host->clock != clk_get_rate(clk_xin)) {
  1418. ret = clk_set_rate(clk_xin, pltfm_host->clock);
  1419. if (ret) {
  1420. dev_err(&pdev->dev, "Failed to set SD clock rate\n");
  1421. goto clk_dis_ahb;
  1422. }
  1423. }
  1424. ret = clk_prepare_enable(clk_xin);
  1425. if (ret) {
  1426. dev_err(dev, "Unable to enable SD clock.\n");
  1427. goto clk_dis_ahb;
  1428. }
  1429. if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
  1430. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
  1431. if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
  1432. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
  1433. pltfm_host->clk = clk_xin;
  1434. if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
  1435. sdhci_arasan_update_clockmultiplier(host, 0x0);
  1436. if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
  1437. of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
  1438. of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio") ||
  1439. of_device_is_compatible(np, "intel,thunderbay-sdhci-5.1")) {
  1440. sdhci_arasan_update_clockmultiplier(host, 0x0);
  1441. sdhci_arasan_update_support64b(host, 0x0);
  1442. host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1443. }
  1444. sdhci_arasan_update_baseclkfreq(host);
  1445. ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
  1446. if (ret)
  1447. goto clk_disable_all;
  1448. if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
  1449. host->mmc_host_ops.execute_tuning =
  1450. arasan_zynqmp_execute_tuning;
  1451. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
  1452. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  1453. }
  1454. arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
  1455. ret = mmc_of_parse(host->mmc);
  1456. if (ret) {
  1457. ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
  1458. goto unreg_clk;
  1459. }
  1460. sdhci_arasan->phy = ERR_PTR(-ENODEV);
  1461. if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
  1462. sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
  1463. if (IS_ERR(sdhci_arasan->phy)) {
  1464. ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
  1465. "No phy for arasan,sdhci-5.1.\n");
  1466. goto unreg_clk;
  1467. }
  1468. ret = phy_init(sdhci_arasan->phy);
  1469. if (ret < 0) {
  1470. dev_err(dev, "phy_init err.\n");
  1471. goto unreg_clk;
  1472. }
  1473. host->mmc_host_ops.hs400_enhanced_strobe =
  1474. sdhci_arasan_hs400_enhanced_strobe;
  1475. host->mmc_host_ops.start_signal_voltage_switch =
  1476. sdhci_arasan_voltage_switch;
  1477. sdhci_arasan->has_cqe = true;
  1478. host->mmc->caps2 |= MMC_CAP2_CQE;
  1479. if (!of_property_read_bool(np, "disable-cqe-dcmd"))
  1480. host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
  1481. }
  1482. ret = sdhci_arasan_add_host(sdhci_arasan);
  1483. if (ret)
  1484. goto err_add_host;
  1485. return 0;
  1486. err_add_host:
  1487. if (!IS_ERR(sdhci_arasan->phy))
  1488. phy_exit(sdhci_arasan->phy);
  1489. unreg_clk:
  1490. sdhci_arasan_unregister_sdclk(dev);
  1491. clk_disable_all:
  1492. clk_disable_unprepare(clk_xin);
  1493. clk_dis_ahb:
  1494. clk_disable_unprepare(sdhci_arasan->clk_ahb);
  1495. err_pltfm_free:
  1496. sdhci_pltfm_free(pdev);
  1497. return ret;
  1498. }
  1499. static int sdhci_arasan_remove(struct platform_device *pdev)
  1500. {
  1501. struct sdhci_host *host = platform_get_drvdata(pdev);
  1502. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1503. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  1504. struct clk *clk_ahb = sdhci_arasan->clk_ahb;
  1505. if (!IS_ERR(sdhci_arasan->phy)) {
  1506. if (sdhci_arasan->is_phy_on)
  1507. phy_power_off(sdhci_arasan->phy);
  1508. phy_exit(sdhci_arasan->phy);
  1509. }
  1510. sdhci_arasan_unregister_sdclk(&pdev->dev);
  1511. sdhci_pltfm_unregister(pdev);
  1512. clk_disable_unprepare(clk_ahb);
  1513. return 0;
  1514. }
  1515. static struct platform_driver sdhci_arasan_driver = {
  1516. .driver = {
  1517. .name = "sdhci-arasan",
  1518. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1519. .of_match_table = sdhci_arasan_of_match,
  1520. .pm = &sdhci_arasan_dev_pm_ops,
  1521. },
  1522. .probe = sdhci_arasan_probe,
  1523. .remove = sdhci_arasan_remove,
  1524. };
  1525. module_platform_driver(sdhci_arasan_driver);
  1526. MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
  1527. MODULE_AUTHOR("Soeren Brinkmann <[email protected]>");
  1528. MODULE_LICENSE("GPL");