sdhci-of-arasan.c 21 KB

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