sdhci-of-arasan.c 60 KB

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