sdhci-of-arasan.c 24 KB

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