sdhci-of-arasan.c 25 KB

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