sdhci-of-arasan.c 21 KB

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