sdhci-of-arasan.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Arasan Secure Digital Host Controller Interface.
  3. * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
  4. * Copyright (c) 2012 Wind River Systems, Inc.
  5. * Copyright (C) 2013 Pengutronix e.K.
  6. * Copyright (C) 2013 Xilinx Inc.
  7. *
  8. * Based on sdhci-of-esdhc.c
  9. *
  10. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  11. * Copyright (c) 2009 MontaVista Software, Inc.
  12. *
  13. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  14. * Anton Vorontsov <avorontsov@ru.mvista.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or (at
  19. * your option) any later version.
  20. */
  21. #include <linux/clk-provider.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/regmap.h>
  27. #include <linux/of.h>
  28. #include "cqhci.h"
  29. #include "sdhci-pltfm.h"
  30. #define SDHCI_ARASAN_VENDOR_REGISTER 0x78
  31. #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
  32. #define VENDOR_ENHANCED_STROBE BIT(0)
  33. #define PHY_CLK_TOO_SLOW_HZ 400000
  34. /*
  35. * On some SoCs the syscon area has a feature where the upper 16-bits of
  36. * each 32-bit register act as a write mask for the lower 16-bits. This allows
  37. * atomic updates of the register without locking. This macro is used on SoCs
  38. * that have that feature.
  39. */
  40. #define HIWORD_UPDATE(val, mask, shift) \
  41. ((val) << (shift) | (mask) << ((shift) + 16))
  42. /**
  43. * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
  44. *
  45. * @reg: Offset within the syscon of the register containing this field
  46. * @width: Number of bits for this field
  47. * @shift: Bit offset within @reg of this field (or -1 if not avail)
  48. */
  49. struct sdhci_arasan_soc_ctl_field {
  50. u32 reg;
  51. u16 width;
  52. s16 shift;
  53. };
  54. /**
  55. * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
  56. *
  57. * It's up to the licensee of the Arsan IP block to make these available
  58. * somewhere if needed. Presumably these will be scattered somewhere that's
  59. * accessible via the syscon API.
  60. *
  61. * @baseclkfreq: Where to find corecfg_baseclkfreq
  62. * @clockmultiplier: Where to find corecfg_clockmultiplier
  63. * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
  64. */
  65. struct sdhci_arasan_soc_ctl_map {
  66. struct sdhci_arasan_soc_ctl_field baseclkfreq;
  67. struct sdhci_arasan_soc_ctl_field clockmultiplier;
  68. bool hiword_update;
  69. };
  70. /**
  71. * struct sdhci_arasan_data
  72. * @host: Pointer to the main SDHCI host structure.
  73. * @clk_ahb: Pointer to the AHB clock
  74. * @phy: Pointer to the generic phy
  75. * @is_phy_on: True if the PHY is on; false if not.
  76. * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
  77. * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
  78. * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
  79. * @soc_ctl_map: Map to get offsets into soc_ctl registers.
  80. */
  81. struct sdhci_arasan_data {
  82. struct sdhci_host *host;
  83. struct clk *clk_ahb;
  84. struct phy *phy;
  85. bool is_phy_on;
  86. bool has_cqe;
  87. struct clk_hw sdcardclk_hw;
  88. struct clk *sdcardclk;
  89. struct regmap *soc_ctl_base;
  90. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  91. unsigned int quirks; /* Arasan deviations from spec */
  92. /* Controller does not have CD wired and will not function normally without */
  93. #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
  94. /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
  95. * internal clock even when the clock isn't stable */
  96. #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
  97. };
  98. struct sdhci_arasan_of_data {
  99. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  100. const struct sdhci_pltfm_data *pdata;
  101. };
  102. static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
  103. .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
  104. .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
  105. .hiword_update = true,
  106. };
  107. /**
  108. * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
  109. *
  110. * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
  111. * Note that if a field is specified as not available (shift < 0) then
  112. * this function will silently return an error code. It will be noisy
  113. * and print errors for any other (unexpected) errors.
  114. *
  115. * @host: The sdhci_host
  116. * @fld: The field to write to
  117. * @val: The value to write
  118. */
  119. static int sdhci_arasan_syscon_write(struct sdhci_host *host,
  120. const struct sdhci_arasan_soc_ctl_field *fld,
  121. u32 val)
  122. {
  123. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  124. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  125. struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
  126. u32 reg = fld->reg;
  127. u16 width = fld->width;
  128. s16 shift = fld->shift;
  129. int ret;
  130. /*
  131. * Silently return errors for shift < 0 so caller doesn't have
  132. * to check for fields which are optional. For fields that
  133. * are required then caller needs to do something special
  134. * anyway.
  135. */
  136. if (shift < 0)
  137. return -EINVAL;
  138. if (sdhci_arasan->soc_ctl_map->hiword_update)
  139. ret = regmap_write(soc_ctl_base, reg,
  140. HIWORD_UPDATE(val, GENMASK(width, 0),
  141. shift));
  142. else
  143. ret = regmap_update_bits(soc_ctl_base, reg,
  144. GENMASK(shift + width, shift),
  145. val << shift);
  146. /* Yell about (unexpected) regmap errors */
  147. if (ret)
  148. pr_warn("%s: Regmap write fail: %d\n",
  149. mmc_hostname(host->mmc), ret);
  150. return ret;
  151. }
  152. static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
  153. {
  154. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  155. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  156. bool ctrl_phy = false;
  157. if (!IS_ERR(sdhci_arasan->phy)) {
  158. if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
  159. /*
  160. * If PHY off, set clock to max speed and power PHY on.
  161. *
  162. * Although PHY docs apparently suggest power cycling
  163. * when changing the clock the PHY doesn't like to be
  164. * powered on while at low speeds like those used in ID
  165. * mode. Even worse is powering the PHY on while the
  166. * clock is off.
  167. *
  168. * To workaround the PHY limitations, the best we can
  169. * do is to power it on at a faster speed and then slam
  170. * through low speeds without power cycling.
  171. */
  172. sdhci_set_clock(host, host->max_clk);
  173. phy_power_on(sdhci_arasan->phy);
  174. sdhci_arasan->is_phy_on = true;
  175. /*
  176. * We'll now fall through to the below case with
  177. * ctrl_phy = false (so we won't turn off/on). The
  178. * sdhci_set_clock() will set the real clock.
  179. */
  180. } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
  181. /*
  182. * At higher clock speeds the PHY is fine being power
  183. * cycled and docs say you _should_ power cycle when
  184. * changing clock speeds.
  185. */
  186. ctrl_phy = true;
  187. }
  188. }
  189. if (ctrl_phy && sdhci_arasan->is_phy_on) {
  190. phy_power_off(sdhci_arasan->phy);
  191. sdhci_arasan->is_phy_on = false;
  192. }
  193. sdhci_set_clock(host, clock);
  194. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
  195. /*
  196. * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
  197. * after enabling the clock even though the clock is not
  198. * stable. Trying to use a clock without waiting here results
  199. * in EILSEQ while detecting some older/slower cards. The
  200. * chosen delay is the maximum delay from sdhci_set_clock.
  201. */
  202. msleep(20);
  203. if (ctrl_phy) {
  204. phy_power_on(sdhci_arasan->phy);
  205. sdhci_arasan->is_phy_on = true;
  206. }
  207. }
  208. static void sdhci_arasan_am654_set_clock(struct sdhci_host *host,
  209. unsigned int clock)
  210. {
  211. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  212. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  213. if (sdhci_arasan->is_phy_on) {
  214. phy_power_off(sdhci_arasan->phy);
  215. sdhci_arasan->is_phy_on = false;
  216. }
  217. sdhci_set_clock(host, clock);
  218. if (clock > PHY_CLK_TOO_SLOW_HZ) {
  219. phy_power_on(sdhci_arasan->phy);
  220. sdhci_arasan->is_phy_on = true;
  221. }
  222. }
  223. static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
  224. struct mmc_ios *ios)
  225. {
  226. u32 vendor;
  227. struct sdhci_host *host = mmc_priv(mmc);
  228. vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
  229. if (ios->enhanced_strobe)
  230. vendor |= VENDOR_ENHANCED_STROBE;
  231. else
  232. vendor &= ~VENDOR_ENHANCED_STROBE;
  233. sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
  234. }
  235. static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
  236. {
  237. u8 ctrl;
  238. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  239. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  240. sdhci_reset(host, mask);
  241. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
  242. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  243. ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
  244. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  245. }
  246. }
  247. static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
  248. struct mmc_ios *ios)
  249. {
  250. switch (ios->signal_voltage) {
  251. case MMC_SIGNAL_VOLTAGE_180:
  252. /*
  253. * Plese don't switch to 1V8 as arasan,5.1 doesn't
  254. * actually refer to this setting to indicate the
  255. * signal voltage and the state machine will be broken
  256. * actually if we force to enable 1V8. That's something
  257. * like broken quirk but we could work around here.
  258. */
  259. return 0;
  260. case MMC_SIGNAL_VOLTAGE_330:
  261. case MMC_SIGNAL_VOLTAGE_120:
  262. /* We don't support 3V3 and 1V2 */
  263. break;
  264. }
  265. return -EINVAL;
  266. }
  267. static void sdhci_arasan_set_power(struct sdhci_host *host, unsigned char mode,
  268. unsigned short vdd)
  269. {
  270. if (!IS_ERR(host->mmc->supply.vmmc)) {
  271. struct mmc_host *mmc = host->mmc;
  272. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  273. }
  274. sdhci_set_power_noreg(host, mode, vdd);
  275. }
  276. static const struct sdhci_ops sdhci_arasan_ops = {
  277. .set_clock = sdhci_arasan_set_clock,
  278. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  279. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  280. .set_bus_width = sdhci_set_bus_width,
  281. .reset = sdhci_arasan_reset,
  282. .set_uhs_signaling = sdhci_set_uhs_signaling,
  283. .set_power = sdhci_arasan_set_power,
  284. };
  285. static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
  286. .ops = &sdhci_arasan_ops,
  287. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  288. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  289. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  290. SDHCI_QUIRK2_STOP_WITH_TC,
  291. };
  292. static struct sdhci_arasan_of_data sdhci_arasan_data = {
  293. .pdata = &sdhci_arasan_pdata,
  294. };
  295. static const struct sdhci_ops sdhci_arasan_am654_ops = {
  296. .set_clock = sdhci_arasan_am654_set_clock,
  297. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  298. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  299. .set_bus_width = sdhci_set_bus_width,
  300. .reset = sdhci_arasan_reset,
  301. .set_uhs_signaling = sdhci_set_uhs_signaling,
  302. };
  303. static const struct sdhci_pltfm_data sdhci_arasan_am654_pdata = {
  304. .ops = &sdhci_arasan_am654_ops,
  305. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  306. SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
  307. SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
  308. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  309. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
  310. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
  311. };
  312. static const struct sdhci_arasan_of_data sdhci_arasan_am654_data = {
  313. .pdata = &sdhci_arasan_am654_pdata,
  314. };
  315. static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
  316. {
  317. int cmd_error = 0;
  318. int data_error = 0;
  319. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  320. return intmask;
  321. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  322. return 0;
  323. }
  324. static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
  325. {
  326. sdhci_dumpregs(mmc_priv(mmc));
  327. }
  328. static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
  329. {
  330. struct sdhci_host *host = mmc_priv(mmc);
  331. u32 reg;
  332. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  333. while (reg & SDHCI_DATA_AVAILABLE) {
  334. sdhci_readl(host, SDHCI_BUFFER);
  335. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  336. }
  337. sdhci_cqe_enable(mmc);
  338. }
  339. static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
  340. .enable = sdhci_arasan_cqe_enable,
  341. .disable = sdhci_cqe_disable,
  342. .dumpregs = sdhci_arasan_dumpregs,
  343. };
  344. static const struct sdhci_ops sdhci_arasan_cqe_ops = {
  345. .set_clock = sdhci_arasan_set_clock,
  346. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  347. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  348. .set_bus_width = sdhci_set_bus_width,
  349. .reset = sdhci_arasan_reset,
  350. .set_uhs_signaling = sdhci_set_uhs_signaling,
  351. .set_power = sdhci_arasan_set_power,
  352. .irq = sdhci_arasan_cqhci_irq,
  353. };
  354. static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
  355. .ops = &sdhci_arasan_cqe_ops,
  356. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  357. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  358. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
  359. };
  360. static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
  361. .soc_ctl_map = &rk3399_soc_ctl_map,
  362. .pdata = &sdhci_arasan_cqe_pdata,
  363. };
  364. #ifdef CONFIG_PM_SLEEP
  365. /**
  366. * sdhci_arasan_suspend - Suspend method for the driver
  367. * @dev: Address of the device structure
  368. * Returns 0 on success and error value on error
  369. *
  370. * Put the device in a low power state.
  371. */
  372. static int sdhci_arasan_suspend(struct device *dev)
  373. {
  374. struct sdhci_host *host = dev_get_drvdata(dev);
  375. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  376. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  377. int ret;
  378. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  379. mmc_retune_needed(host->mmc);
  380. if (sdhci_arasan->has_cqe) {
  381. ret = cqhci_suspend(host->mmc);
  382. if (ret)
  383. return ret;
  384. }
  385. ret = sdhci_suspend_host(host);
  386. if (ret)
  387. return ret;
  388. if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
  389. ret = phy_power_off(sdhci_arasan->phy);
  390. if (ret) {
  391. dev_err(dev, "Cannot power off phy.\n");
  392. sdhci_resume_host(host);
  393. return ret;
  394. }
  395. sdhci_arasan->is_phy_on = false;
  396. }
  397. clk_disable(pltfm_host->clk);
  398. clk_disable(sdhci_arasan->clk_ahb);
  399. return 0;
  400. }
  401. /**
  402. * sdhci_arasan_resume - Resume method for the driver
  403. * @dev: Address of the device structure
  404. * Returns 0 on success and error value on error
  405. *
  406. * Resume operation after suspend
  407. */
  408. static int sdhci_arasan_resume(struct device *dev)
  409. {
  410. struct sdhci_host *host = dev_get_drvdata(dev);
  411. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  412. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  413. int ret;
  414. ret = clk_enable(sdhci_arasan->clk_ahb);
  415. if (ret) {
  416. dev_err(dev, "Cannot enable AHB clock.\n");
  417. return ret;
  418. }
  419. ret = clk_enable(pltfm_host->clk);
  420. if (ret) {
  421. dev_err(dev, "Cannot enable SD clock.\n");
  422. return ret;
  423. }
  424. if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
  425. ret = phy_power_on(sdhci_arasan->phy);
  426. if (ret) {
  427. dev_err(dev, "Cannot power on phy.\n");
  428. return ret;
  429. }
  430. sdhci_arasan->is_phy_on = true;
  431. }
  432. ret = sdhci_resume_host(host);
  433. if (ret) {
  434. dev_err(dev, "Cannot resume host.\n");
  435. return ret;
  436. }
  437. if (sdhci_arasan->has_cqe)
  438. return cqhci_resume(host->mmc);
  439. return 0;
  440. }
  441. #endif /* ! CONFIG_PM_SLEEP */
  442. static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
  443. sdhci_arasan_resume);
  444. static const struct of_device_id sdhci_arasan_of_match[] = {
  445. /* SoC-specific compatible strings w/ soc_ctl_map */
  446. {
  447. .compatible = "rockchip,rk3399-sdhci-5.1",
  448. .data = &sdhci_arasan_rk3399_data,
  449. },
  450. {
  451. .compatible = "ti,am654-sdhci-5.1",
  452. .data = &sdhci_arasan_am654_data,
  453. },
  454. /* Generic compatible below here */
  455. {
  456. .compatible = "arasan,sdhci-8.9a",
  457. .data = &sdhci_arasan_data,
  458. },
  459. {
  460. .compatible = "arasan,sdhci-5.1",
  461. .data = &sdhci_arasan_data,
  462. },
  463. {
  464. .compatible = "arasan,sdhci-4.9a",
  465. .data = &sdhci_arasan_data,
  466. },
  467. { /* sentinel */ }
  468. };
  469. MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
  470. /**
  471. * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
  472. *
  473. * Return the current actual rate of the SD card clock. This can be used
  474. * to communicate with out PHY.
  475. *
  476. * @hw: Pointer to the hardware clock structure.
  477. * @parent_rate The parent rate (should be rate of clk_xin).
  478. * Returns the card clock rate.
  479. */
  480. static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
  481. unsigned long parent_rate)
  482. {
  483. struct sdhci_arasan_data *sdhci_arasan =
  484. container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
  485. struct sdhci_host *host = sdhci_arasan->host;
  486. return host->mmc->actual_clock;
  487. }
  488. static const struct clk_ops arasan_sdcardclk_ops = {
  489. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  490. };
  491. /**
  492. * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
  493. *
  494. * The corecfg_clockmultiplier is supposed to contain clock multiplier
  495. * value of programmable clock generator.
  496. *
  497. * NOTES:
  498. * - Many existing devices don't seem to do this and work fine. To keep
  499. * compatibility for old hardware where the device tree doesn't provide a
  500. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  501. * for this platform.
  502. * - The value of corecfg_clockmultiplier should sync with that of corresponding
  503. * value reading from sdhci_capability_register. So this function is called
  504. * once at probe time and never called again.
  505. *
  506. * @host: The sdhci_host
  507. */
  508. static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
  509. u32 value)
  510. {
  511. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  512. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  513. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  514. sdhci_arasan->soc_ctl_map;
  515. /* Having a map is optional */
  516. if (!soc_ctl_map)
  517. return;
  518. /* If we have a map, we expect to have a syscon */
  519. if (!sdhci_arasan->soc_ctl_base) {
  520. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  521. mmc_hostname(host->mmc));
  522. return;
  523. }
  524. sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
  525. }
  526. /**
  527. * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
  528. *
  529. * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
  530. * function can be used to make that happen.
  531. *
  532. * NOTES:
  533. * - Many existing devices don't seem to do this and work fine. To keep
  534. * compatibility for old hardware where the device tree doesn't provide a
  535. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  536. * for this platform.
  537. * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
  538. * to achieve lower clock rates. That means that this function is called once
  539. * at probe time and never called again.
  540. *
  541. * @host: The sdhci_host
  542. */
  543. static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
  544. {
  545. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  546. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  547. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  548. sdhci_arasan->soc_ctl_map;
  549. u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
  550. /* Having a map is optional */
  551. if (!soc_ctl_map)
  552. return;
  553. /* If we have a map, we expect to have a syscon */
  554. if (!sdhci_arasan->soc_ctl_base) {
  555. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  556. mmc_hostname(host->mmc));
  557. return;
  558. }
  559. sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
  560. }
  561. /**
  562. * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
  563. *
  564. * Some PHY devices need to know what the actual card clock is. In order for
  565. * them to find out, we'll provide a clock through the common clock framework
  566. * for them to query.
  567. *
  568. * Note: without seriously re-architecting SDHCI's clock code and testing on
  569. * all platforms, there's no way to create a totally beautiful clock here
  570. * with all clock ops implemented. Instead, we'll just create a clock that can
  571. * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
  572. * framework that we're doing things behind its back. This should be sufficient
  573. * to create nice clean device tree bindings and later (if needed) we can try
  574. * re-architecting SDHCI if we see some benefit to it.
  575. *
  576. * @sdhci_arasan: Our private data structure.
  577. * @clk_xin: Pointer to the functional clock
  578. * @dev: Pointer to our struct device.
  579. * Returns 0 on success and error value on error
  580. */
  581. static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
  582. struct clk *clk_xin,
  583. struct device *dev)
  584. {
  585. struct device_node *np = dev->of_node;
  586. struct clk_init_data sdcardclk_init;
  587. const char *parent_clk_name;
  588. int ret;
  589. /* Providing a clock to the PHY is optional; no error if missing */
  590. if (!of_find_property(np, "#clock-cells", NULL))
  591. return 0;
  592. ret = of_property_read_string_index(np, "clock-output-names", 0,
  593. &sdcardclk_init.name);
  594. if (ret) {
  595. dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
  596. return ret;
  597. }
  598. parent_clk_name = __clk_get_name(clk_xin);
  599. sdcardclk_init.parent_names = &parent_clk_name;
  600. sdcardclk_init.num_parents = 1;
  601. sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
  602. sdcardclk_init.ops = &arasan_sdcardclk_ops;
  603. sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
  604. sdhci_arasan->sdcardclk =
  605. devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
  606. sdhci_arasan->sdcardclk_hw.init = NULL;
  607. ret = of_clk_add_provider(np, of_clk_src_simple_get,
  608. sdhci_arasan->sdcardclk);
  609. if (ret)
  610. dev_err(dev, "Failed to add clock provider\n");
  611. return ret;
  612. }
  613. /**
  614. * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
  615. *
  616. * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
  617. * returned success.
  618. *
  619. * @dev: Pointer to our struct device.
  620. */
  621. static void sdhci_arasan_unregister_sdclk(struct device *dev)
  622. {
  623. struct device_node *np = dev->of_node;
  624. if (!of_find_property(np, "#clock-cells", NULL))
  625. return;
  626. of_clk_del_provider(dev->of_node);
  627. }
  628. static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
  629. {
  630. struct sdhci_host *host = sdhci_arasan->host;
  631. struct cqhci_host *cq_host;
  632. bool dma64;
  633. int ret;
  634. if (!sdhci_arasan->has_cqe)
  635. return sdhci_add_host(host);
  636. ret = sdhci_setup_host(host);
  637. if (ret)
  638. return ret;
  639. cq_host = devm_kzalloc(host->mmc->parent,
  640. sizeof(*cq_host), GFP_KERNEL);
  641. if (!cq_host) {
  642. ret = -ENOMEM;
  643. goto cleanup;
  644. }
  645. cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
  646. cq_host->ops = &sdhci_arasan_cqhci_ops;
  647. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  648. if (dma64)
  649. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  650. ret = cqhci_init(cq_host, host->mmc, dma64);
  651. if (ret)
  652. goto cleanup;
  653. ret = __sdhci_add_host(host);
  654. if (ret)
  655. goto cleanup;
  656. return 0;
  657. cleanup:
  658. sdhci_cleanup_host(host);
  659. return ret;
  660. }
  661. static int sdhci_arasan_probe(struct platform_device *pdev)
  662. {
  663. int ret;
  664. const struct of_device_id *match;
  665. struct device_node *node;
  666. struct clk *clk_xin;
  667. struct sdhci_host *host;
  668. struct sdhci_pltfm_host *pltfm_host;
  669. struct sdhci_arasan_data *sdhci_arasan;
  670. struct device_node *np = pdev->dev.of_node;
  671. const struct sdhci_arasan_of_data *data;
  672. match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
  673. data = match->data;
  674. host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
  675. if (IS_ERR(host))
  676. return PTR_ERR(host);
  677. pltfm_host = sdhci_priv(host);
  678. sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  679. sdhci_arasan->host = host;
  680. sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
  681. node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
  682. if (node) {
  683. sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
  684. of_node_put(node);
  685. if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
  686. ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
  687. if (ret != -EPROBE_DEFER)
  688. dev_err(&pdev->dev, "Can't get syscon: %d\n",
  689. ret);
  690. goto err_pltfm_free;
  691. }
  692. }
  693. sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
  694. if (IS_ERR(sdhci_arasan->clk_ahb)) {
  695. dev_err(&pdev->dev, "clk_ahb clock not found.\n");
  696. ret = PTR_ERR(sdhci_arasan->clk_ahb);
  697. goto err_pltfm_free;
  698. }
  699. clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
  700. if (IS_ERR(clk_xin)) {
  701. dev_err(&pdev->dev, "clk_xin clock not found.\n");
  702. ret = PTR_ERR(clk_xin);
  703. goto err_pltfm_free;
  704. }
  705. ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
  706. if (ret) {
  707. dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
  708. goto err_pltfm_free;
  709. }
  710. ret = clk_prepare_enable(clk_xin);
  711. if (ret) {
  712. dev_err(&pdev->dev, "Unable to enable SD clock.\n");
  713. goto clk_dis_ahb;
  714. }
  715. sdhci_get_of_property(pdev);
  716. if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
  717. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
  718. if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
  719. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
  720. pltfm_host->clk = clk_xin;
  721. if (of_device_is_compatible(pdev->dev.of_node,
  722. "rockchip,rk3399-sdhci-5.1"))
  723. sdhci_arasan_update_clockmultiplier(host, 0x0);
  724. sdhci_arasan_update_baseclkfreq(host);
  725. ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
  726. if (ret)
  727. goto clk_disable_all;
  728. ret = mmc_of_parse(host->mmc);
  729. if (ret) {
  730. if (ret != -EPROBE_DEFER)
  731. dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
  732. goto unreg_clk;
  733. }
  734. sdhci_arasan->phy = ERR_PTR(-ENODEV);
  735. if (of_device_is_compatible(pdev->dev.of_node,
  736. "arasan,sdhci-5.1")) {
  737. sdhci_arasan->phy = devm_phy_get(&pdev->dev,
  738. "phy_arasan");
  739. if (IS_ERR(sdhci_arasan->phy)) {
  740. ret = PTR_ERR(sdhci_arasan->phy);
  741. dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
  742. goto unreg_clk;
  743. }
  744. ret = phy_init(sdhci_arasan->phy);
  745. if (ret < 0) {
  746. dev_err(&pdev->dev, "phy_init err.\n");
  747. goto unreg_clk;
  748. }
  749. host->mmc_host_ops.hs400_enhanced_strobe =
  750. sdhci_arasan_hs400_enhanced_strobe;
  751. host->mmc_host_ops.start_signal_voltage_switch =
  752. sdhci_arasan_voltage_switch;
  753. sdhci_arasan->has_cqe = true;
  754. host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
  755. }
  756. ret = sdhci_arasan_add_host(sdhci_arasan);
  757. if (ret)
  758. goto err_add_host;
  759. return 0;
  760. err_add_host:
  761. if (!IS_ERR(sdhci_arasan->phy))
  762. phy_exit(sdhci_arasan->phy);
  763. unreg_clk:
  764. sdhci_arasan_unregister_sdclk(&pdev->dev);
  765. clk_disable_all:
  766. clk_disable_unprepare(clk_xin);
  767. clk_dis_ahb:
  768. clk_disable_unprepare(sdhci_arasan->clk_ahb);
  769. err_pltfm_free:
  770. sdhci_pltfm_free(pdev);
  771. return ret;
  772. }
  773. static int sdhci_arasan_remove(struct platform_device *pdev)
  774. {
  775. int ret;
  776. struct sdhci_host *host = platform_get_drvdata(pdev);
  777. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  778. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  779. struct clk *clk_ahb = sdhci_arasan->clk_ahb;
  780. if (!IS_ERR(sdhci_arasan->phy)) {
  781. if (sdhci_arasan->is_phy_on)
  782. phy_power_off(sdhci_arasan->phy);
  783. phy_exit(sdhci_arasan->phy);
  784. }
  785. sdhci_arasan_unregister_sdclk(&pdev->dev);
  786. ret = sdhci_pltfm_unregister(pdev);
  787. clk_disable_unprepare(clk_ahb);
  788. return ret;
  789. }
  790. static struct platform_driver sdhci_arasan_driver = {
  791. .driver = {
  792. .name = "sdhci-arasan",
  793. .of_match_table = sdhci_arasan_of_match,
  794. .pm = &sdhci_arasan_dev_pm_ops,
  795. },
  796. .probe = sdhci_arasan_probe,
  797. .remove = sdhci_arasan_remove,
  798. };
  799. module_platform_driver(sdhci_arasan_driver);
  800. MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
  801. MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
  802. MODULE_LICENSE("GPL");