sdhci-of-arasan.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 "sdhci-pltfm.h"
  28. #include <linux/of.h>
  29. #define SDHCI_ARASAN_CLK_CTRL_OFFSET 0x2c
  30. #define SDHCI_ARASAN_VENDOR_REGISTER 0x78
  31. #define VENDOR_ENHANCED_STROBE BIT(0)
  32. #define CLK_CTRL_TIMEOUT_SHIFT 16
  33. #define CLK_CTRL_TIMEOUT_MASK (0xf << CLK_CTRL_TIMEOUT_SHIFT)
  34. #define CLK_CTRL_TIMEOUT_MIN_EXP 13
  35. #define PHY_CLK_TOO_SLOW_HZ 400000
  36. /*
  37. * On some SoCs the syscon area has a feature where the upper 16-bits of
  38. * each 32-bit register act as a write mask for the lower 16-bits. This allows
  39. * atomic updates of the register without locking. This macro is used on SoCs
  40. * that have that feature.
  41. */
  42. #define HIWORD_UPDATE(val, mask, shift) \
  43. ((val) << (shift) | (mask) << ((shift) + 16))
  44. /**
  45. * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
  46. *
  47. * @reg: Offset within the syscon of the register containing this field
  48. * @width: Number of bits for this field
  49. * @shift: Bit offset within @reg of this field (or -1 if not avail)
  50. */
  51. struct sdhci_arasan_soc_ctl_field {
  52. u32 reg;
  53. u16 width;
  54. s16 shift;
  55. };
  56. /**
  57. * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
  58. *
  59. * It's up to the licensee of the Arsan IP block to make these available
  60. * somewhere if needed. Presumably these will be scattered somewhere that's
  61. * accessible via the syscon API.
  62. *
  63. * @baseclkfreq: Where to find corecfg_baseclkfreq
  64. * @clockmultiplier: Where to find corecfg_clockmultiplier
  65. * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
  66. */
  67. struct sdhci_arasan_soc_ctl_map {
  68. struct sdhci_arasan_soc_ctl_field baseclkfreq;
  69. struct sdhci_arasan_soc_ctl_field clockmultiplier;
  70. bool hiword_update;
  71. };
  72. /**
  73. * struct sdhci_arasan_data
  74. * @host: Pointer to the main SDHCI host structure.
  75. * @clk_ahb: Pointer to the AHB clock
  76. * @phy: Pointer to the generic phy
  77. * @is_phy_on: True if the PHY is on; false if not.
  78. * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
  79. * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
  80. * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
  81. * @soc_ctl_map: Map to get offsets into soc_ctl registers.
  82. */
  83. struct sdhci_arasan_data {
  84. struct sdhci_host *host;
  85. struct clk *clk_ahb;
  86. struct phy *phy;
  87. bool is_phy_on;
  88. struct clk_hw sdcardclk_hw;
  89. struct clk *sdcardclk;
  90. struct regmap *soc_ctl_base;
  91. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
  92. unsigned int quirks; /* Arasan deviations from spec */
  93. /* Controller does not have CD wired and will not function normally without */
  94. #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
  95. };
  96. static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
  97. .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
  98. .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
  99. .hiword_update = true,
  100. };
  101. /**
  102. * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
  103. *
  104. * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
  105. * Note that if a field is specified as not available (shift < 0) then
  106. * this function will silently return an error code. It will be noisy
  107. * and print errors for any other (unexpected) errors.
  108. *
  109. * @host: The sdhci_host
  110. * @fld: The field to write to
  111. * @val: The value to write
  112. */
  113. static int sdhci_arasan_syscon_write(struct sdhci_host *host,
  114. const struct sdhci_arasan_soc_ctl_field *fld,
  115. u32 val)
  116. {
  117. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  118. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  119. struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
  120. u32 reg = fld->reg;
  121. u16 width = fld->width;
  122. s16 shift = fld->shift;
  123. int ret;
  124. /*
  125. * Silently return errors for shift < 0 so caller doesn't have
  126. * to check for fields which are optional. For fields that
  127. * are required then caller needs to do something special
  128. * anyway.
  129. */
  130. if (shift < 0)
  131. return -EINVAL;
  132. if (sdhci_arasan->soc_ctl_map->hiword_update)
  133. ret = regmap_write(soc_ctl_base, reg,
  134. HIWORD_UPDATE(val, GENMASK(width, 0),
  135. shift));
  136. else
  137. ret = regmap_update_bits(soc_ctl_base, reg,
  138. GENMASK(shift + width, shift),
  139. val << shift);
  140. /* Yell about (unexpected) regmap errors */
  141. if (ret)
  142. pr_warn("%s: Regmap write fail: %d\n",
  143. mmc_hostname(host->mmc), ret);
  144. return ret;
  145. }
  146. static unsigned int sdhci_arasan_get_timeout_clock(struct sdhci_host *host)
  147. {
  148. u32 div;
  149. unsigned long freq;
  150. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  151. div = readl(host->ioaddr + SDHCI_ARASAN_CLK_CTRL_OFFSET);
  152. div = (div & CLK_CTRL_TIMEOUT_MASK) >> CLK_CTRL_TIMEOUT_SHIFT;
  153. freq = clk_get_rate(pltfm_host->clk);
  154. freq /= 1 << (CLK_CTRL_TIMEOUT_MIN_EXP + div);
  155. return freq;
  156. }
  157. static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
  158. {
  159. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  160. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  161. bool ctrl_phy = false;
  162. if (!IS_ERR(sdhci_arasan->phy)) {
  163. if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
  164. /*
  165. * If PHY off, set clock to max speed and power PHY on.
  166. *
  167. * Although PHY docs apparently suggest power cycling
  168. * when changing the clock the PHY doesn't like to be
  169. * powered on while at low speeds like those used in ID
  170. * mode. Even worse is powering the PHY on while the
  171. * clock is off.
  172. *
  173. * To workaround the PHY limitations, the best we can
  174. * do is to power it on at a faster speed and then slam
  175. * through low speeds without power cycling.
  176. */
  177. sdhci_set_clock(host, host->max_clk);
  178. spin_unlock_irq(&host->lock);
  179. phy_power_on(sdhci_arasan->phy);
  180. spin_lock_irq(&host->lock);
  181. sdhci_arasan->is_phy_on = true;
  182. /*
  183. * We'll now fall through to the below case with
  184. * ctrl_phy = false (so we won't turn off/on). The
  185. * sdhci_set_clock() will set the real clock.
  186. */
  187. } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
  188. /*
  189. * At higher clock speeds the PHY is fine being power
  190. * cycled and docs say you _should_ power cycle when
  191. * changing clock speeds.
  192. */
  193. ctrl_phy = true;
  194. }
  195. }
  196. if (ctrl_phy && sdhci_arasan->is_phy_on) {
  197. spin_unlock_irq(&host->lock);
  198. phy_power_off(sdhci_arasan->phy);
  199. spin_lock_irq(&host->lock);
  200. sdhci_arasan->is_phy_on = false;
  201. }
  202. sdhci_set_clock(host, clock);
  203. if (ctrl_phy) {
  204. spin_unlock_irq(&host->lock);
  205. phy_power_on(sdhci_arasan->phy);
  206. spin_lock_irq(&host->lock);
  207. sdhci_arasan->is_phy_on = true;
  208. }
  209. }
  210. static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
  211. struct mmc_ios *ios)
  212. {
  213. u32 vendor;
  214. struct sdhci_host *host = mmc_priv(mmc);
  215. vendor = readl(host->ioaddr + SDHCI_ARASAN_VENDOR_REGISTER);
  216. if (ios->enhanced_strobe)
  217. vendor |= VENDOR_ENHANCED_STROBE;
  218. else
  219. vendor &= ~VENDOR_ENHANCED_STROBE;
  220. writel(vendor, host->ioaddr + SDHCI_ARASAN_VENDOR_REGISTER);
  221. }
  222. static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
  223. {
  224. u8 ctrl;
  225. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  226. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  227. sdhci_reset(host, mask);
  228. if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
  229. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  230. ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
  231. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  232. }
  233. }
  234. static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
  235. struct mmc_ios *ios)
  236. {
  237. switch (ios->signal_voltage) {
  238. case MMC_SIGNAL_VOLTAGE_180:
  239. /*
  240. * Plese don't switch to 1V8 as arasan,5.1 doesn't
  241. * actually refer to this setting to indicate the
  242. * signal voltage and the state machine will be broken
  243. * actually if we force to enable 1V8. That's something
  244. * like broken quirk but we could work around here.
  245. */
  246. return 0;
  247. case MMC_SIGNAL_VOLTAGE_330:
  248. case MMC_SIGNAL_VOLTAGE_120:
  249. /* We don't support 3V3 and 1V2 */
  250. break;
  251. }
  252. return -EINVAL;
  253. }
  254. static struct sdhci_ops sdhci_arasan_ops = {
  255. .set_clock = sdhci_arasan_set_clock,
  256. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  257. .get_timeout_clock = sdhci_arasan_get_timeout_clock,
  258. .set_bus_width = sdhci_set_bus_width,
  259. .reset = sdhci_arasan_reset,
  260. .set_uhs_signaling = sdhci_set_uhs_signaling,
  261. };
  262. static struct sdhci_pltfm_data sdhci_arasan_pdata = {
  263. .ops = &sdhci_arasan_ops,
  264. .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  265. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  266. SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
  267. };
  268. #ifdef CONFIG_PM_SLEEP
  269. /**
  270. * sdhci_arasan_suspend - Suspend method for the driver
  271. * @dev: Address of the device structure
  272. * Returns 0 on success and error value on error
  273. *
  274. * Put the device in a low power state.
  275. */
  276. static int sdhci_arasan_suspend(struct device *dev)
  277. {
  278. struct platform_device *pdev = to_platform_device(dev);
  279. struct sdhci_host *host = platform_get_drvdata(pdev);
  280. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  281. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  282. int ret;
  283. ret = sdhci_suspend_host(host);
  284. if (ret)
  285. return ret;
  286. if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
  287. ret = phy_power_off(sdhci_arasan->phy);
  288. if (ret) {
  289. dev_err(dev, "Cannot power off phy.\n");
  290. sdhci_resume_host(host);
  291. return ret;
  292. }
  293. sdhci_arasan->is_phy_on = false;
  294. }
  295. clk_disable(pltfm_host->clk);
  296. clk_disable(sdhci_arasan->clk_ahb);
  297. return 0;
  298. }
  299. /**
  300. * sdhci_arasan_resume - Resume method for the driver
  301. * @dev: Address of the device structure
  302. * Returns 0 on success and error value on error
  303. *
  304. * Resume operation after suspend
  305. */
  306. static int sdhci_arasan_resume(struct device *dev)
  307. {
  308. struct platform_device *pdev = to_platform_device(dev);
  309. struct sdhci_host *host = platform_get_drvdata(pdev);
  310. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  311. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  312. int ret;
  313. ret = clk_enable(sdhci_arasan->clk_ahb);
  314. if (ret) {
  315. dev_err(dev, "Cannot enable AHB clock.\n");
  316. return ret;
  317. }
  318. ret = clk_enable(pltfm_host->clk);
  319. if (ret) {
  320. dev_err(dev, "Cannot enable SD clock.\n");
  321. return ret;
  322. }
  323. if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
  324. ret = phy_power_on(sdhci_arasan->phy);
  325. if (ret) {
  326. dev_err(dev, "Cannot power on phy.\n");
  327. return ret;
  328. }
  329. sdhci_arasan->is_phy_on = true;
  330. }
  331. return sdhci_resume_host(host);
  332. }
  333. #endif /* ! CONFIG_PM_SLEEP */
  334. static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
  335. sdhci_arasan_resume);
  336. static const struct of_device_id sdhci_arasan_of_match[] = {
  337. /* SoC-specific compatible strings w/ soc_ctl_map */
  338. {
  339. .compatible = "rockchip,rk3399-sdhci-5.1",
  340. .data = &rk3399_soc_ctl_map,
  341. },
  342. /* Generic compatible below here */
  343. { .compatible = "arasan,sdhci-8.9a" },
  344. { .compatible = "arasan,sdhci-5.1" },
  345. { .compatible = "arasan,sdhci-4.9a" },
  346. { /* sentinel */ }
  347. };
  348. MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
  349. /**
  350. * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
  351. *
  352. * Return the current actual rate of the SD card clock. This can be used
  353. * to communicate with out PHY.
  354. *
  355. * @hw: Pointer to the hardware clock structure.
  356. * @parent_rate The parent rate (should be rate of clk_xin).
  357. * Returns the card clock rate.
  358. */
  359. static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
  360. unsigned long parent_rate)
  361. {
  362. struct sdhci_arasan_data *sdhci_arasan =
  363. container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
  364. struct sdhci_host *host = sdhci_arasan->host;
  365. return host->mmc->actual_clock;
  366. }
  367. static const struct clk_ops arasan_sdcardclk_ops = {
  368. .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
  369. };
  370. /**
  371. * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
  372. *
  373. * The corecfg_clockmultiplier is supposed to contain clock multiplier
  374. * value of programmable clock generator.
  375. *
  376. * NOTES:
  377. * - Many existing devices don't seem to do this and work fine. To keep
  378. * compatibility for old hardware where the device tree doesn't provide a
  379. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  380. * for this platform.
  381. * - The value of corecfg_clockmultiplier should sync with that of corresponding
  382. * value reading from sdhci_capability_register. So this function is called
  383. * once at probe time and never called again.
  384. *
  385. * @host: The sdhci_host
  386. */
  387. static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
  388. u32 value)
  389. {
  390. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  391. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  392. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  393. sdhci_arasan->soc_ctl_map;
  394. /* Having a map is optional */
  395. if (!soc_ctl_map)
  396. return;
  397. /* If we have a map, we expect to have a syscon */
  398. if (!sdhci_arasan->soc_ctl_base) {
  399. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  400. mmc_hostname(host->mmc));
  401. return;
  402. }
  403. sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
  404. }
  405. /**
  406. * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
  407. *
  408. * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
  409. * function can be used to make that happen.
  410. *
  411. * NOTES:
  412. * - Many existing devices don't seem to do this and work fine. To keep
  413. * compatibility for old hardware where the device tree doesn't provide a
  414. * register map, this function is a noop if a soc_ctl_map hasn't been provided
  415. * for this platform.
  416. * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
  417. * to achieve lower clock rates. That means that this function is called once
  418. * at probe time and never called again.
  419. *
  420. * @host: The sdhci_host
  421. */
  422. static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
  423. {
  424. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  425. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  426. const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
  427. sdhci_arasan->soc_ctl_map;
  428. u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
  429. /* Having a map is optional */
  430. if (!soc_ctl_map)
  431. return;
  432. /* If we have a map, we expect to have a syscon */
  433. if (!sdhci_arasan->soc_ctl_base) {
  434. pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
  435. mmc_hostname(host->mmc));
  436. return;
  437. }
  438. sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
  439. }
  440. /**
  441. * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
  442. *
  443. * Some PHY devices need to know what the actual card clock is. In order for
  444. * them to find out, we'll provide a clock through the common clock framework
  445. * for them to query.
  446. *
  447. * Note: without seriously re-architecting SDHCI's clock code and testing on
  448. * all platforms, there's no way to create a totally beautiful clock here
  449. * with all clock ops implemented. Instead, we'll just create a clock that can
  450. * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
  451. * framework that we're doing things behind its back. This should be sufficient
  452. * to create nice clean device tree bindings and later (if needed) we can try
  453. * re-architecting SDHCI if we see some benefit to it.
  454. *
  455. * @sdhci_arasan: Our private data structure.
  456. * @clk_xin: Pointer to the functional clock
  457. * @dev: Pointer to our struct device.
  458. * Returns 0 on success and error value on error
  459. */
  460. static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
  461. struct clk *clk_xin,
  462. struct device *dev)
  463. {
  464. struct device_node *np = dev->of_node;
  465. struct clk_init_data sdcardclk_init;
  466. const char *parent_clk_name;
  467. int ret;
  468. /* Providing a clock to the PHY is optional; no error if missing */
  469. if (!of_find_property(np, "#clock-cells", NULL))
  470. return 0;
  471. ret = of_property_read_string_index(np, "clock-output-names", 0,
  472. &sdcardclk_init.name);
  473. if (ret) {
  474. dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
  475. return ret;
  476. }
  477. parent_clk_name = __clk_get_name(clk_xin);
  478. sdcardclk_init.parent_names = &parent_clk_name;
  479. sdcardclk_init.num_parents = 1;
  480. sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
  481. sdcardclk_init.ops = &arasan_sdcardclk_ops;
  482. sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
  483. sdhci_arasan->sdcardclk =
  484. devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
  485. sdhci_arasan->sdcardclk_hw.init = NULL;
  486. ret = of_clk_add_provider(np, of_clk_src_simple_get,
  487. sdhci_arasan->sdcardclk);
  488. if (ret)
  489. dev_err(dev, "Failed to add clock provider\n");
  490. return ret;
  491. }
  492. /**
  493. * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
  494. *
  495. * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
  496. * returned success.
  497. *
  498. * @dev: Pointer to our struct device.
  499. */
  500. static void sdhci_arasan_unregister_sdclk(struct device *dev)
  501. {
  502. struct device_node *np = dev->of_node;
  503. if (!of_find_property(np, "#clock-cells", NULL))
  504. return;
  505. of_clk_del_provider(dev->of_node);
  506. }
  507. static int sdhci_arasan_probe(struct platform_device *pdev)
  508. {
  509. int ret;
  510. const struct of_device_id *match;
  511. struct device_node *node;
  512. struct clk *clk_xin;
  513. struct sdhci_host *host;
  514. struct sdhci_pltfm_host *pltfm_host;
  515. struct sdhci_arasan_data *sdhci_arasan;
  516. struct device_node *np = pdev->dev.of_node;
  517. host = sdhci_pltfm_init(pdev, &sdhci_arasan_pdata,
  518. sizeof(*sdhci_arasan));
  519. if (IS_ERR(host))
  520. return PTR_ERR(host);
  521. pltfm_host = sdhci_priv(host);
  522. sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  523. sdhci_arasan->host = host;
  524. match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
  525. sdhci_arasan->soc_ctl_map = match->data;
  526. node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
  527. if (node) {
  528. sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
  529. of_node_put(node);
  530. if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
  531. ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
  532. if (ret != -EPROBE_DEFER)
  533. dev_err(&pdev->dev, "Can't get syscon: %d\n",
  534. ret);
  535. goto err_pltfm_free;
  536. }
  537. }
  538. sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
  539. if (IS_ERR(sdhci_arasan->clk_ahb)) {
  540. dev_err(&pdev->dev, "clk_ahb clock not found.\n");
  541. ret = PTR_ERR(sdhci_arasan->clk_ahb);
  542. goto err_pltfm_free;
  543. }
  544. clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
  545. if (IS_ERR(clk_xin)) {
  546. dev_err(&pdev->dev, "clk_xin clock not found.\n");
  547. ret = PTR_ERR(clk_xin);
  548. goto err_pltfm_free;
  549. }
  550. ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
  551. if (ret) {
  552. dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
  553. goto err_pltfm_free;
  554. }
  555. ret = clk_prepare_enable(clk_xin);
  556. if (ret) {
  557. dev_err(&pdev->dev, "Unable to enable SD clock.\n");
  558. goto clk_dis_ahb;
  559. }
  560. sdhci_get_of_property(pdev);
  561. if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
  562. sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
  563. pltfm_host->clk = clk_xin;
  564. if (of_device_is_compatible(pdev->dev.of_node,
  565. "rockchip,rk3399-sdhci-5.1"))
  566. sdhci_arasan_update_clockmultiplier(host, 0x0);
  567. sdhci_arasan_update_baseclkfreq(host);
  568. ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
  569. if (ret)
  570. goto clk_disable_all;
  571. ret = mmc_of_parse(host->mmc);
  572. if (ret) {
  573. dev_err(&pdev->dev, "parsing dt failed (%u)\n", ret);
  574. goto unreg_clk;
  575. }
  576. sdhci_arasan->phy = ERR_PTR(-ENODEV);
  577. if (of_device_is_compatible(pdev->dev.of_node,
  578. "arasan,sdhci-5.1")) {
  579. sdhci_arasan->phy = devm_phy_get(&pdev->dev,
  580. "phy_arasan");
  581. if (IS_ERR(sdhci_arasan->phy)) {
  582. ret = PTR_ERR(sdhci_arasan->phy);
  583. dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
  584. goto unreg_clk;
  585. }
  586. ret = phy_init(sdhci_arasan->phy);
  587. if (ret < 0) {
  588. dev_err(&pdev->dev, "phy_init err.\n");
  589. goto unreg_clk;
  590. }
  591. host->mmc_host_ops.hs400_enhanced_strobe =
  592. sdhci_arasan_hs400_enhanced_strobe;
  593. host->mmc_host_ops.start_signal_voltage_switch =
  594. sdhci_arasan_voltage_switch;
  595. }
  596. ret = sdhci_add_host(host);
  597. if (ret)
  598. goto err_add_host;
  599. return 0;
  600. err_add_host:
  601. if (!IS_ERR(sdhci_arasan->phy))
  602. phy_exit(sdhci_arasan->phy);
  603. unreg_clk:
  604. sdhci_arasan_unregister_sdclk(&pdev->dev);
  605. clk_disable_all:
  606. clk_disable_unprepare(clk_xin);
  607. clk_dis_ahb:
  608. clk_disable_unprepare(sdhci_arasan->clk_ahb);
  609. err_pltfm_free:
  610. sdhci_pltfm_free(pdev);
  611. return ret;
  612. }
  613. static int sdhci_arasan_remove(struct platform_device *pdev)
  614. {
  615. int ret;
  616. struct sdhci_host *host = platform_get_drvdata(pdev);
  617. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  618. struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
  619. struct clk *clk_ahb = sdhci_arasan->clk_ahb;
  620. if (!IS_ERR(sdhci_arasan->phy)) {
  621. if (sdhci_arasan->is_phy_on)
  622. phy_power_off(sdhci_arasan->phy);
  623. phy_exit(sdhci_arasan->phy);
  624. }
  625. sdhci_arasan_unregister_sdclk(&pdev->dev);
  626. ret = sdhci_pltfm_unregister(pdev);
  627. clk_disable_unprepare(clk_ahb);
  628. return ret;
  629. }
  630. static struct platform_driver sdhci_arasan_driver = {
  631. .driver = {
  632. .name = "sdhci-arasan",
  633. .of_match_table = sdhci_arasan_of_match,
  634. .pm = &sdhci_arasan_dev_pm_ops,
  635. },
  636. .probe = sdhci_arasan_probe,
  637. .remove = sdhci_arasan_remove,
  638. };
  639. module_platform_driver(sdhci_arasan_driver);
  640. MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
  641. MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
  642. MODULE_LICENSE("GPL");