sdhci-of-arasan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. SDHCI_QUIRK2_STOP_WITH_TC,
  260. };
  261. static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
  262. {
  263. int cmd_error = 0;
  264. int data_error = 0;
  265. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  266. return intmask;
  267. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  268. return 0;
  269. }
  270. static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
  271. {
  272. sdhci_dumpregs(mmc_priv(mmc));
  273. }
  274. static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
  275. {
  276. struct sdhci_host *host = mmc_priv(mmc);
  277. u32 reg;
  278. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  279. while (reg & SDHCI_DATA_AVAILABLE) {
  280. sdhci_readl(host, SDHCI_BUFFER);
  281. reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
  282. }
  283. sdhci_cqe_enable(mmc);
  284. }
  285. static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
  286. .enable = sdhci_arasan_cqe_enable,
  287. .disable = sdhci_cqe_disable,
  288. .dumpregs = sdhci_arasan_dumpregs,
  289. };
  290. static const struct sdhci_ops sdhci_arasan_cqe_ops = {
  291. .set_clock = sdhci_arasan_set_clock,
  292. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  293. .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
  294. .set_bus_width = sdhci_set_bus_width,
  295. .reset = sdhci_arasan_reset,
  296. .set_uhs_signaling = sdhci_set_uhs_signaling,
  297. .set_power = sdhci_arasan_set_power,
  298. .irq = sdhci_arasan_cqhci_irq,
  299. };
  300. static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
  301. .ops = &sdhci_arasan_cqe_ops,
  302. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  303. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  304. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
  305. };
  306. #ifdef CONFIG_PM_SLEEP
  307. /**
  308. * sdhci_arasan_suspend - Suspend method for the driver
  309. * @dev: Address of the device structure
  310. * Returns 0 on success and error value on error
  311. *
  312. * Put the device in a low power state.
  313. */
  314. static int sdhci_arasan_suspend(struct device *dev)
  315. {
  316. struct sdhci_host *host = dev_get_drvdata(dev);
  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 sdhci_host *host = dev_get_drvdata(dev);
  353. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  354. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  355. int ret;
  356. ret = clk_enable(sdhci_arasan->clk_ahb);
  357. if (ret) {
  358. dev_err(dev, "Cannot enable AHB clock.\n");
  359. return ret;
  360. }
  361. ret = clk_enable(pltfm_host->clk);
  362. if (ret) {
  363. dev_err(dev, "Cannot enable SD clock.\n");
  364. return ret;
  365. }
  366. if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
  367. ret = phy_power_on(sdhci_arasan->phy);
  368. if (ret) {
  369. dev_err(dev, "Cannot power on phy.\n");
  370. return ret;
  371. }
  372. sdhci_arasan->is_phy_on = true;
  373. }
  374. ret = sdhci_resume_host(host);
  375. if (ret) {
  376. dev_err(dev, "Cannot resume host.\n");
  377. return ret;
  378. }
  379. if (sdhci_arasan->has_cqe)
  380. return cqhci_resume(host->mmc);
  381. return 0;
  382. }
  383. #endif /* ! CONFIG_PM_SLEEP */
  384. static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
  385. sdhci_arasan_resume);
  386. static const struct of_device_id sdhci_arasan_of_match[] = {
  387. /* SoC-specific compatible strings w/ soc_ctl_map */
  388. {
  389. .compatible = "rockchip,rk3399-sdhci-5.1",
  390. .data = &rk3399_soc_ctl_map,
  391. },
  392. /* Generic compatible below here */
  393. { .compatible = "arasan,sdhci-8.9a" },
  394. { .compatible = "arasan,sdhci-5.1" },
  395. { .compatible = "arasan,sdhci-4.9a" },
  396. { /* sentinel */ }
  397. };
  398. MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
  399. /**
  400. * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
  401. *
  402. * Return the current actual rate of the SD card clock. This can be used
  403. * to communicate with out PHY.
  404. *
  405. * @hw: Pointer to the hardware clock structure.
  406. * @parent_rate The parent rate (should be rate of clk_xin).
  407. * Returns the card clock rate.
  408. */
  409. static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
  410. unsigned long parent_rate)
  411. {
  412. struct sdhci_arasan_data *sdhci_arasan =
  413. container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
  414. struct sdhci_host *host = sdhci_arasan->host;
  415. return host->mmc->actual_clock;
  416. }
  417. static const struct clk_ops arasan_sdcardclk_ops = {
  418. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  419. };
  420. /**
  421. * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
  422. *
  423. * The corecfg_clockmultiplier is supposed to contain clock multiplier
  424. * value of programmable clock generator.
  425. *
  426. * NOTES:
  427. * - Many existing devices don't seem to do this and work fine. To keep
  428. * compatibility for old hardware where the device tree doesn't provide a
  429. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  430. * for this platform.
  431. * - The value of corecfg_clockmultiplier should sync with that of corresponding
  432. * value reading from sdhci_capability_register. So this function is called
  433. * once at probe time and never called again.
  434. *
  435. * @host: The sdhci_host
  436. */
  437. static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
  438. u32 value)
  439. {
  440. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  441. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  442. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  443. sdhci_arasan->soc_ctl_map;
  444. /* Having a map is optional */
  445. if (!soc_ctl_map)
  446. return;
  447. /* If we have a map, we expect to have a syscon */
  448. if (!sdhci_arasan->soc_ctl_base) {
  449. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  450. mmc_hostname(host->mmc));
  451. return;
  452. }
  453. sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
  454. }
  455. /**
  456. * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
  457. *
  458. * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
  459. * function can be used to make that happen.
  460. *
  461. * NOTES:
  462. * - Many existing devices don't seem to do this and work fine. To keep
  463. * compatibility for old hardware where the device tree doesn't provide a
  464. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  465. * for this platform.
  466. * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
  467. * to achieve lower clock rates. That means that this function is called once
  468. * at probe time and never called again.
  469. *
  470. * @host: The sdhci_host
  471. */
  472. static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
  473. {
  474. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  475. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  476. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  477. sdhci_arasan->soc_ctl_map;
  478. u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
  479. /* Having a map is optional */
  480. if (!soc_ctl_map)
  481. return;
  482. /* If we have a map, we expect to have a syscon */
  483. if (!sdhci_arasan->soc_ctl_base) {
  484. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  485. mmc_hostname(host->mmc));
  486. return;
  487. }
  488. sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
  489. }
  490. /**
  491. * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
  492. *
  493. * Some PHY devices need to know what the actual card clock is. In order for
  494. * them to find out, we'll provide a clock through the common clock framework
  495. * for them to query.
  496. *
  497. * Note: without seriously re-architecting SDHCI's clock code and testing on
  498. * all platforms, there's no way to create a totally beautiful clock here
  499. * with all clock ops implemented. Instead, we'll just create a clock that can
  500. * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
  501. * framework that we're doing things behind its back. This should be sufficient
  502. * to create nice clean device tree bindings and later (if needed) we can try
  503. * re-architecting SDHCI if we see some benefit to it.
  504. *
  505. * @sdhci_arasan: Our private data structure.
  506. * @clk_xin: Pointer to the functional clock
  507. * @dev: Pointer to our struct device.
  508. * Returns 0 on success and error value on error
  509. */
  510. static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
  511. struct clk *clk_xin,
  512. struct device *dev)
  513. {
  514. struct device_node *np = dev->of_node;
  515. struct clk_init_data sdcardclk_init;
  516. const char *parent_clk_name;
  517. int ret;
  518. /* Providing a clock to the PHY is optional; no error if missing */
  519. if (!of_find_property(np, "#clock-cells", NULL))
  520. return 0;
  521. ret = of_property_read_string_index(np, "clock-output-names", 0,
  522. &sdcardclk_init.name);
  523. if (ret) {
  524. dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
  525. return ret;
  526. }
  527. parent_clk_name = __clk_get_name(clk_xin);
  528. sdcardclk_init.parent_names = &parent_clk_name;
  529. sdcardclk_init.num_parents = 1;
  530. sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
  531. sdcardclk_init.ops = &arasan_sdcardclk_ops;
  532. sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
  533. sdhci_arasan->sdcardclk =
  534. devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
  535. sdhci_arasan->sdcardclk_hw.init = NULL;
  536. ret = of_clk_add_provider(np, of_clk_src_simple_get,
  537. sdhci_arasan->sdcardclk);
  538. if (ret)
  539. dev_err(dev, "Failed to add clock provider\n");
  540. return ret;
  541. }
  542. /**
  543. * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
  544. *
  545. * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
  546. * returned success.
  547. *
  548. * @dev: Pointer to our struct device.
  549. */
  550. static void sdhci_arasan_unregister_sdclk(struct device *dev)
  551. {
  552. struct device_node *np = dev->of_node;
  553. if (!of_find_property(np, "#clock-cells", NULL))
  554. return;
  555. of_clk_del_provider(dev->of_node);
  556. }
  557. static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
  558. {
  559. struct sdhci_host *host = sdhci_arasan->host;
  560. struct cqhci_host *cq_host;
  561. bool dma64;
  562. int ret;
  563. if (!sdhci_arasan->has_cqe)
  564. return sdhci_add_host(host);
  565. ret = sdhci_setup_host(host);
  566. if (ret)
  567. return ret;
  568. cq_host = devm_kzalloc(host->mmc->parent,
  569. sizeof(*cq_host), GFP_KERNEL);
  570. if (!cq_host) {
  571. ret = -ENOMEM;
  572. goto cleanup;
  573. }
  574. cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
  575. cq_host->ops = &sdhci_arasan_cqhci_ops;
  576. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  577. if (dma64)
  578. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  579. ret = cqhci_init(cq_host, host->mmc, dma64);
  580. if (ret)
  581. goto cleanup;
  582. ret = __sdhci_add_host(host);
  583. if (ret)
  584. goto cleanup;
  585. return 0;
  586. cleanup:
  587. sdhci_cleanup_host(host);
  588. return ret;
  589. }
  590. static int sdhci_arasan_probe(struct platform_device *pdev)
  591. {
  592. int ret;
  593. const struct of_device_id *match;
  594. struct device_node *node;
  595. struct clk *clk_xin;
  596. struct sdhci_host *host;
  597. struct sdhci_pltfm_host *pltfm_host;
  598. struct sdhci_arasan_data *sdhci_arasan;
  599. struct device_node *np = pdev->dev.of_node;
  600. const struct sdhci_pltfm_data *pdata;
  601. if (of_device_is_compatible(pdev->dev.of_node, "arasan,sdhci-5.1"))
  602. pdata = &sdhci_arasan_cqe_pdata;
  603. else
  604. pdata = &sdhci_arasan_pdata;
  605. host = sdhci_pltfm_init(pdev, pdata, sizeof(*sdhci_arasan));
  606. if (IS_ERR(host))
  607. return PTR_ERR(host);
  608. pltfm_host = sdhci_priv(host);
  609. sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  610. sdhci_arasan->host = host;
  611. match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
  612. sdhci_arasan->soc_ctl_map = match->data;
  613. node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
  614. if (node) {
  615. sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
  616. of_node_put(node);
  617. if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
  618. ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
  619. if (ret != -EPROBE_DEFER)
  620. dev_err(&pdev->dev, "Can't get syscon: %d\n",
  621. ret);
  622. goto err_pltfm_free;
  623. }
  624. }
  625. sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
  626. if (IS_ERR(sdhci_arasan->clk_ahb)) {
  627. dev_err(&pdev->dev, "clk_ahb clock not found.\n");
  628. ret = PTR_ERR(sdhci_arasan->clk_ahb);
  629. goto err_pltfm_free;
  630. }
  631. clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
  632. if (IS_ERR(clk_xin)) {
  633. dev_err(&pdev->dev, "clk_xin clock not found.\n");
  634. ret = PTR_ERR(clk_xin);
  635. goto err_pltfm_free;
  636. }
  637. ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
  638. if (ret) {
  639. dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
  640. goto err_pltfm_free;
  641. }
  642. ret = clk_prepare_enable(clk_xin);
  643. if (ret) {
  644. dev_err(&pdev->dev, "Unable to enable SD clock.\n");
  645. goto clk_dis_ahb;
  646. }
  647. sdhci_get_of_property(pdev);
  648. if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
  649. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
  650. pltfm_host->clk = clk_xin;
  651. if (of_device_is_compatible(pdev->dev.of_node,
  652. "rockchip,rk3399-sdhci-5.1"))
  653. sdhci_arasan_update_clockmultiplier(host, 0x0);
  654. sdhci_arasan_update_baseclkfreq(host);
  655. ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
  656. if (ret)
  657. goto clk_disable_all;
  658. ret = mmc_of_parse(host->mmc);
  659. if (ret) {
  660. dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
  661. goto unreg_clk;
  662. }
  663. sdhci_arasan->phy = ERR_PTR(-ENODEV);
  664. if (of_device_is_compatible(pdev->dev.of_node,
  665. "arasan,sdhci-5.1")) {
  666. sdhci_arasan->phy = devm_phy_get(&pdev->dev,
  667. "phy_arasan");
  668. if (IS_ERR(sdhci_arasan->phy)) {
  669. ret = PTR_ERR(sdhci_arasan->phy);
  670. dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
  671. goto unreg_clk;
  672. }
  673. ret = phy_init(sdhci_arasan->phy);
  674. if (ret < 0) {
  675. dev_err(&pdev->dev, "phy_init err.\n");
  676. goto unreg_clk;
  677. }
  678. host->mmc_host_ops.hs400_enhanced_strobe =
  679. sdhci_arasan_hs400_enhanced_strobe;
  680. host->mmc_host_ops.start_signal_voltage_switch =
  681. sdhci_arasan_voltage_switch;
  682. sdhci_arasan->has_cqe = true;
  683. host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
  684. }
  685. ret = sdhci_arasan_add_host(sdhci_arasan);
  686. if (ret)
  687. goto err_add_host;
  688. return 0;
  689. err_add_host:
  690. if (!IS_ERR(sdhci_arasan->phy))
  691. phy_exit(sdhci_arasan->phy);
  692. unreg_clk:
  693. sdhci_arasan_unregister_sdclk(&pdev->dev);
  694. clk_disable_all:
  695. clk_disable_unprepare(clk_xin);
  696. clk_dis_ahb:
  697. clk_disable_unprepare(sdhci_arasan->clk_ahb);
  698. err_pltfm_free:
  699. sdhci_pltfm_free(pdev);
  700. return ret;
  701. }
  702. static int sdhci_arasan_remove(struct platform_device *pdev)
  703. {
  704. int ret;
  705. struct sdhci_host *host = platform_get_drvdata(pdev);
  706. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  707. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  708. struct clk *clk_ahb = sdhci_arasan->clk_ahb;
  709. if (!IS_ERR(sdhci_arasan->phy)) {
  710. if (sdhci_arasan->is_phy_on)
  711. phy_power_off(sdhci_arasan->phy);
  712. phy_exit(sdhci_arasan->phy);
  713. }
  714. sdhci_arasan_unregister_sdclk(&pdev->dev);
  715. ret = sdhci_pltfm_unregister(pdev);
  716. clk_disable_unprepare(clk_ahb);
  717. return ret;
  718. }
  719. static struct platform_driver sdhci_arasan_driver = {
  720. .driver = {
  721. .name = "sdhci-arasan",
  722. .of_match_table = sdhci_arasan_of_match,
  723. .pm = &sdhci_arasan_dev_pm_ops,
  724. },
  725. .probe = sdhci_arasan_probe,
  726. .remove = sdhci_arasan_remove,
  727. };
  728. module_platform_driver(sdhci_arasan_driver);
  729. MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
  730. MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
  731. MODULE_LICENSE("GPL");