renesas_sdhi_core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Renesas SDHI
  3. *
  4. * Copyright (C) 2015-17 Renesas Electronics Corporation
  5. * Copyright (C) 2016-17 Sang Engineering, Wolfram Sang
  6. * Copyright (C) 2016-17 Horms Solutions, Simon Horman
  7. * Copyright (C) 2009 Magnus Damm
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Based on "Compaq ASIC3 support":
  14. *
  15. * Copyright 2001 Compaq Computer Corporation.
  16. * Copyright 2004-2005 Phil Blundell
  17. * Copyright 2007-2008 OpenedHand Ltd.
  18. *
  19. * Authors: Phil Blundell <pb@handhelds.org>,
  20. * Samuel Ortiz <sameo@openedhand.com>
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/clk.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mmc/host.h>
  30. #include <linux/mmc/slot-gpio.h>
  31. #include <linux/mfd/tmio.h>
  32. #include <linux/sh_dma.h>
  33. #include <linux/delay.h>
  34. #include <linux/pinctrl/consumer.h>
  35. #include <linux/pinctrl/pinctrl-state.h>
  36. #include <linux/regulator/consumer.h>
  37. #include "renesas_sdhi.h"
  38. #include "tmio_mmc.h"
  39. #define HOST_MODE 0xe4
  40. #define SDHI_VER_GEN2_SDR50 0x490c
  41. #define SDHI_VER_RZ_A1 0x820b
  42. /* very old datasheets said 0x490c for SDR104, too. They are wrong! */
  43. #define SDHI_VER_GEN2_SDR104 0xcb0d
  44. #define SDHI_VER_GEN3_SD 0xcc10
  45. #define SDHI_VER_GEN3_SDMMC 0xcd10
  46. static void renesas_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
  47. {
  48. u32 val;
  49. /*
  50. * see also
  51. * renesas_sdhi_of_data :: dma_buswidth
  52. */
  53. switch (sd_ctrl_read16(host, CTL_VERSION)) {
  54. case SDHI_VER_GEN2_SDR50:
  55. val = (width == 32) ? 0x0001 : 0x0000;
  56. break;
  57. case SDHI_VER_GEN2_SDR104:
  58. val = (width == 32) ? 0x0000 : 0x0001;
  59. break;
  60. case SDHI_VER_GEN3_SD:
  61. case SDHI_VER_GEN3_SDMMC:
  62. if (width == 64)
  63. val = 0x0000;
  64. else if (width == 32)
  65. val = 0x0101;
  66. else
  67. val = 0x0001;
  68. break;
  69. default:
  70. /* nothing to do */
  71. return;
  72. }
  73. sd_ctrl_write16(host, HOST_MODE, val);
  74. }
  75. static int renesas_sdhi_clk_enable(struct tmio_mmc_host *host)
  76. {
  77. struct mmc_host *mmc = host->mmc;
  78. struct renesas_sdhi *priv = host_to_priv(host);
  79. int ret = clk_prepare_enable(priv->clk);
  80. if (ret < 0)
  81. return ret;
  82. ret = clk_prepare_enable(priv->clk_cd);
  83. if (ret < 0) {
  84. clk_disable_unprepare(priv->clk);
  85. return ret;
  86. }
  87. /*
  88. * The clock driver may not know what maximum frequency
  89. * actually works, so it should be set with the max-frequency
  90. * property which will already have been read to f_max. If it
  91. * was missing, assume the current frequency is the maximum.
  92. */
  93. if (!mmc->f_max)
  94. mmc->f_max = clk_get_rate(priv->clk);
  95. /*
  96. * Minimum frequency is the minimum input clock frequency
  97. * divided by our maximum divider.
  98. */
  99. mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
  100. /* enable 16bit data access on SDBUF as default */
  101. renesas_sdhi_sdbuf_width(host, 16);
  102. return 0;
  103. }
  104. static unsigned int renesas_sdhi_clk_update(struct tmio_mmc_host *host,
  105. unsigned int new_clock)
  106. {
  107. struct renesas_sdhi *priv = host_to_priv(host);
  108. unsigned int freq, diff, best_freq = 0, diff_min = ~0;
  109. int i, ret;
  110. /* tested only on R-Car Gen2+ currently; may work for others */
  111. if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
  112. return clk_get_rate(priv->clk);
  113. /*
  114. * We want the bus clock to be as close as possible to, but no
  115. * greater than, new_clock. As we can divide by 1 << i for
  116. * any i in [0, 9] we want the input clock to be as close as
  117. * possible, but no greater than, new_clock << i.
  118. */
  119. for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
  120. freq = clk_round_rate(priv->clk, new_clock << i);
  121. if (freq > (new_clock << i)) {
  122. /* Too fast; look for a slightly slower option */
  123. freq = clk_round_rate(priv->clk,
  124. (new_clock << i) / 4 * 3);
  125. if (freq > (new_clock << i))
  126. continue;
  127. }
  128. diff = new_clock - (freq >> i);
  129. if (diff <= diff_min) {
  130. best_freq = freq;
  131. diff_min = diff;
  132. }
  133. }
  134. ret = clk_set_rate(priv->clk, best_freq);
  135. return ret == 0 ? best_freq : clk_get_rate(priv->clk);
  136. }
  137. static void renesas_sdhi_clk_disable(struct tmio_mmc_host *host)
  138. {
  139. struct renesas_sdhi *priv = host_to_priv(host);
  140. clk_disable_unprepare(priv->clk);
  141. clk_disable_unprepare(priv->clk_cd);
  142. }
  143. static int renesas_sdhi_card_busy(struct mmc_host *mmc)
  144. {
  145. struct tmio_mmc_host *host = mmc_priv(mmc);
  146. return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
  147. TMIO_STAT_DAT0);
  148. }
  149. static int renesas_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
  150. struct mmc_ios *ios)
  151. {
  152. struct tmio_mmc_host *host = mmc_priv(mmc);
  153. struct renesas_sdhi *priv = host_to_priv(host);
  154. struct pinctrl_state *pin_state;
  155. int ret;
  156. switch (ios->signal_voltage) {
  157. case MMC_SIGNAL_VOLTAGE_330:
  158. pin_state = priv->pins_default;
  159. break;
  160. case MMC_SIGNAL_VOLTAGE_180:
  161. pin_state = priv->pins_uhs;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. /*
  167. * If anything is missing, assume signal voltage is fixed at
  168. * 3.3V and succeed/fail accordingly.
  169. */
  170. if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
  171. return ios->signal_voltage ==
  172. MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
  173. ret = mmc_regulator_set_vqmmc(host->mmc, ios);
  174. if (ret)
  175. return ret;
  176. return pinctrl_select_state(priv->pinctrl, pin_state);
  177. }
  178. /* SCC registers */
  179. #define SH_MOBILE_SDHI_SCC_DTCNTL 0x000
  180. #define SH_MOBILE_SDHI_SCC_TAPSET 0x002
  181. #define SH_MOBILE_SDHI_SCC_DT2FF 0x004
  182. #define SH_MOBILE_SDHI_SCC_CKSEL 0x006
  183. #define SH_MOBILE_SDHI_SCC_RVSCNTL 0x008
  184. #define SH_MOBILE_SDHI_SCC_RVSREQ 0x00A
  185. #define SH_MOBILE_SDHI_SCC_TMPPORT2 0x00E
  186. /* Definitions for values the SH_MOBILE_SDHI_SCC_DTCNTL register */
  187. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN BIT(0)
  188. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
  189. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
  190. /* Definitions for values the SH_MOBILE_SDHI_SCC_CKSEL register */
  191. #define SH_MOBILE_SDHI_SCC_CKSEL_DTSEL BIT(0)
  192. /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSCNTL register */
  193. #define SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
  194. /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSREQ register */
  195. #define SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR BIT(2)
  196. /* Definitions for values the SH_MOBILE_SDHI_SCC_TMPPORT2 register */
  197. #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL BIT(4)
  198. #define SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN BIT(31)
  199. static inline u32 sd_scc_read32(struct tmio_mmc_host *host,
  200. struct renesas_sdhi *priv, int addr)
  201. {
  202. return readl(priv->scc_ctl + (addr << host->bus_shift));
  203. }
  204. static inline void sd_scc_write32(struct tmio_mmc_host *host,
  205. struct renesas_sdhi *priv,
  206. int addr, u32 val)
  207. {
  208. writel(val, priv->scc_ctl + (addr << host->bus_shift));
  209. }
  210. static unsigned int renesas_sdhi_init_tuning(struct tmio_mmc_host *host)
  211. {
  212. struct renesas_sdhi *priv;
  213. priv = host_to_priv(host);
  214. /* Initialize SCC */
  215. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, 0x0);
  216. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  217. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  218. /* set sampling clock selection range */
  219. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
  220. SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
  221. 0x8 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
  222. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
  223. SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
  224. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
  225. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  226. ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
  227. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  228. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF, priv->scc_tappos);
  229. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  230. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  231. /* Read TAPNUM */
  232. return (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL) >>
  233. SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
  234. SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK;
  235. }
  236. static void renesas_sdhi_prepare_tuning(struct tmio_mmc_host *host,
  237. unsigned long tap)
  238. {
  239. struct renesas_sdhi *priv = host_to_priv(host);
  240. /* Set sampling clock position */
  241. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, tap);
  242. }
  243. static void renesas_sdhi_hs400_complete(struct tmio_mmc_host *host)
  244. {
  245. struct renesas_sdhi *priv = host_to_priv(host);
  246. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  247. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  248. /* Set HS400 mode */
  249. sd_ctrl_write16(host, CTL_SDIF_MODE, 0x0001 |
  250. sd_ctrl_read16(host, CTL_SDIF_MODE));
  251. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
  252. (SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
  253. SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) |
  254. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
  255. /* Set the sampling clock selection range of HS400 mode */
  256. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
  257. SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
  258. 0x4 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
  259. if (host->pdata->flags & TMIO_MMC_HAVE_4TAP_HS400)
  260. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET,
  261. host->tap_set / 2);
  262. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
  263. SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
  264. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
  265. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  266. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  267. }
  268. static void renesas_sdhi_reset_scc(struct tmio_mmc_host *host,
  269. struct renesas_sdhi *priv)
  270. {
  271. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  272. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  273. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL,
  274. ~SH_MOBILE_SDHI_SCC_CKSEL_DTSEL &
  275. sd_scc_read32(host, priv,
  276. SH_MOBILE_SDHI_SCC_CKSEL));
  277. }
  278. static void renesas_sdhi_disable_scc(struct tmio_mmc_host *host)
  279. {
  280. struct renesas_sdhi *priv = host_to_priv(host);
  281. renesas_sdhi_reset_scc(host, priv);
  282. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
  283. ~SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN &
  284. sd_scc_read32(host, priv,
  285. SH_MOBILE_SDHI_SCC_DTCNTL));
  286. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  287. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  288. }
  289. static void renesas_sdhi_reset_hs400_mode(struct tmio_mmc_host *host,
  290. struct renesas_sdhi *priv)
  291. {
  292. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  293. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  294. /* Reset HS400 mode */
  295. sd_ctrl_write16(host, CTL_SDIF_MODE, ~0x0001 &
  296. sd_ctrl_read16(host, CTL_SDIF_MODE));
  297. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2,
  298. ~(SH_MOBILE_SDHI_SCC_TMPPORT2_HS400EN |
  299. SH_MOBILE_SDHI_SCC_TMPPORT2_HS400OSEL) &
  300. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_TMPPORT2));
  301. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  302. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  303. }
  304. static void renesas_sdhi_prepare_hs400_tuning(struct tmio_mmc_host *host)
  305. {
  306. renesas_sdhi_reset_hs400_mode(host, host_to_priv(host));
  307. }
  308. #define SH_MOBILE_SDHI_MAX_TAP 3
  309. static int renesas_sdhi_select_tuning(struct tmio_mmc_host *host)
  310. {
  311. struct renesas_sdhi *priv = host_to_priv(host);
  312. unsigned long tap_cnt; /* counter of tuning success */
  313. unsigned long tap_start;/* start position of tuning success */
  314. unsigned long tap_end; /* end position of tuning success */
  315. unsigned long ntap; /* temporary counter of tuning success */
  316. unsigned long i;
  317. /* Clear SCC_RVSREQ */
  318. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
  319. /*
  320. * When tuning CMD19 is issued twice for each tap, merge the
  321. * result requiring the tap to be good in both runs before
  322. * considering it for tuning selection.
  323. */
  324. for (i = 0; i < host->tap_num * 2; i++) {
  325. int offset = host->tap_num * (i < host->tap_num ? 1 : -1);
  326. if (!test_bit(i, host->taps))
  327. clear_bit(i + offset, host->taps);
  328. }
  329. /*
  330. * Find the longest consecutive run of successful probes. If that
  331. * is more than SH_MOBILE_SDHI_MAX_TAP probes long then use the
  332. * center index as the tap.
  333. */
  334. tap_cnt = 0;
  335. ntap = 0;
  336. tap_start = 0;
  337. tap_end = 0;
  338. for (i = 0; i < host->tap_num * 2; i++) {
  339. if (test_bit(i, host->taps)) {
  340. ntap++;
  341. } else {
  342. if (ntap > tap_cnt) {
  343. tap_start = i - ntap;
  344. tap_end = i - 1;
  345. tap_cnt = ntap;
  346. }
  347. ntap = 0;
  348. }
  349. }
  350. if (ntap > tap_cnt) {
  351. tap_start = i - ntap;
  352. tap_end = i - 1;
  353. tap_cnt = ntap;
  354. }
  355. if (tap_cnt >= SH_MOBILE_SDHI_MAX_TAP)
  356. host->tap_set = (tap_start + tap_end) / 2 % host->tap_num;
  357. else
  358. return -EIO;
  359. /* Set SCC */
  360. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, host->tap_set);
  361. /* Enable auto re-tuning */
  362. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  363. SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN |
  364. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  365. return 0;
  366. }
  367. static bool renesas_sdhi_check_scc_error(struct tmio_mmc_host *host)
  368. {
  369. struct renesas_sdhi *priv = host_to_priv(host);
  370. /* Check SCC error */
  371. if (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL) &
  372. SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &&
  373. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ) &
  374. SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR) {
  375. /* Clear SCC error */
  376. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
  377. return true;
  378. }
  379. return false;
  380. }
  381. static void renesas_sdhi_hw_reset(struct tmio_mmc_host *host)
  382. {
  383. struct renesas_sdhi *priv;
  384. priv = host_to_priv(host);
  385. renesas_sdhi_reset_scc(host, priv);
  386. renesas_sdhi_reset_hs400_mode(host, priv);
  387. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  388. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  389. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  390. ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
  391. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  392. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  393. ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
  394. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  395. }
  396. static int renesas_sdhi_wait_idle(struct tmio_mmc_host *host, u32 bit)
  397. {
  398. int timeout = 1000;
  399. /* CBSY is set when busy, SCLKDIVEN is cleared when busy */
  400. u32 wait_state = (bit == TMIO_STAT_CMD_BUSY ? TMIO_STAT_CMD_BUSY : 0);
  401. while (--timeout && (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS)
  402. & bit) == wait_state)
  403. udelay(1);
  404. if (!timeout) {
  405. dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
  406. return -EBUSY;
  407. }
  408. return 0;
  409. }
  410. static int renesas_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  411. {
  412. u32 bit = TMIO_STAT_SCLKDIVEN;
  413. switch (addr) {
  414. case CTL_SD_CMD:
  415. case CTL_STOP_INTERNAL_ACTION:
  416. case CTL_XFER_BLK_COUNT:
  417. case CTL_SD_XFER_LEN:
  418. case CTL_SD_MEM_CARD_OPT:
  419. case CTL_TRANSACTION_CTL:
  420. case CTL_DMA_ENABLE:
  421. case HOST_MODE:
  422. if (host->pdata->flags & TMIO_MMC_HAVE_CBSY)
  423. bit = TMIO_STAT_CMD_BUSY;
  424. /* fallthrough */
  425. case CTL_SD_CARD_CLK_CTL:
  426. return renesas_sdhi_wait_idle(host, bit);
  427. }
  428. return 0;
  429. }
  430. static int renesas_sdhi_multi_io_quirk(struct mmc_card *card,
  431. unsigned int direction, int blk_size)
  432. {
  433. /*
  434. * In Renesas controllers, when performing a
  435. * multiple block read of one or two blocks,
  436. * depending on the timing with which the
  437. * response register is read, the response
  438. * value may not be read properly.
  439. * Use single block read for this HW bug
  440. */
  441. if ((direction == MMC_DATA_READ) &&
  442. blk_size == 2)
  443. return 1;
  444. return blk_size;
  445. }
  446. static void renesas_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
  447. {
  448. /* Iff regs are 8 byte apart, sdbuf is 64 bit. Otherwise always 32. */
  449. int width = (host->bus_shift == 2) ? 64 : 32;
  450. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? DMA_ENABLE_DMASDRW : 0);
  451. renesas_sdhi_sdbuf_width(host, enable ? width : 16);
  452. }
  453. int renesas_sdhi_probe(struct platform_device *pdev,
  454. const struct tmio_mmc_dma_ops *dma_ops)
  455. {
  456. struct tmio_mmc_data *mmd = pdev->dev.platform_data;
  457. const struct renesas_sdhi_of_data *of_data;
  458. struct tmio_mmc_data *mmc_data;
  459. struct tmio_mmc_dma *dma_priv;
  460. struct tmio_mmc_host *host;
  461. struct renesas_sdhi *priv;
  462. struct resource *res;
  463. int irq, ret, i;
  464. of_data = of_device_get_match_data(&pdev->dev);
  465. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  466. if (!res)
  467. return -EINVAL;
  468. priv = devm_kzalloc(&pdev->dev, sizeof(struct renesas_sdhi),
  469. GFP_KERNEL);
  470. if (!priv)
  471. return -ENOMEM;
  472. mmc_data = &priv->mmc_data;
  473. dma_priv = &priv->dma_priv;
  474. priv->clk = devm_clk_get(&pdev->dev, NULL);
  475. if (IS_ERR(priv->clk)) {
  476. ret = PTR_ERR(priv->clk);
  477. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  478. return ret;
  479. }
  480. /*
  481. * Some controllers provide a 2nd clock just to run the internal card
  482. * detection logic. Unfortunately, the existing driver architecture does
  483. * not support a separation of clocks for runtime PM usage. When
  484. * native hotplug is used, the tmio driver assumes that the core
  485. * must continue to run for card detect to stay active, so we cannot
  486. * disable it.
  487. * Additionally, it is prohibited to supply a clock to the core but not
  488. * to the card detect circuit. That leaves us with if separate clocks
  489. * are presented, we must treat them both as virtually 1 clock.
  490. */
  491. priv->clk_cd = devm_clk_get(&pdev->dev, "cd");
  492. if (IS_ERR(priv->clk_cd))
  493. priv->clk_cd = NULL;
  494. priv->pinctrl = devm_pinctrl_get(&pdev->dev);
  495. if (!IS_ERR(priv->pinctrl)) {
  496. priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
  497. PINCTRL_STATE_DEFAULT);
  498. priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
  499. "state_uhs");
  500. }
  501. host = tmio_mmc_host_alloc(pdev, mmc_data);
  502. if (IS_ERR(host))
  503. return PTR_ERR(host);
  504. if (of_data) {
  505. mmc_data->flags |= of_data->tmio_flags;
  506. mmc_data->ocr_mask = of_data->tmio_ocr_mask;
  507. mmc_data->capabilities |= of_data->capabilities;
  508. mmc_data->capabilities2 |= of_data->capabilities2;
  509. mmc_data->dma_rx_offset = of_data->dma_rx_offset;
  510. mmc_data->max_blk_count = of_data->max_blk_count;
  511. mmc_data->max_segs = of_data->max_segs;
  512. dma_priv->dma_buswidth = of_data->dma_buswidth;
  513. host->bus_shift = of_data->bus_shift;
  514. }
  515. host->write16_hook = renesas_sdhi_write16_hook;
  516. host->clk_enable = renesas_sdhi_clk_enable;
  517. host->clk_update = renesas_sdhi_clk_update;
  518. host->clk_disable = renesas_sdhi_clk_disable;
  519. host->multi_io_quirk = renesas_sdhi_multi_io_quirk;
  520. host->dma_ops = dma_ops;
  521. /* For some SoC, we disable internal WP. GPIO may override this */
  522. if (mmc_can_gpio_ro(host->mmc))
  523. mmc_data->capabilities2 &= ~MMC_CAP2_NO_WRITE_PROTECT;
  524. /* SDR speeds are only available on Gen2+ */
  525. if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
  526. /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
  527. host->ops.card_busy = renesas_sdhi_card_busy;
  528. host->ops.start_signal_voltage_switch =
  529. renesas_sdhi_start_signal_voltage_switch;
  530. }
  531. /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
  532. if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
  533. host->bus_shift = 1;
  534. if (mmd)
  535. *mmc_data = *mmd;
  536. dma_priv->filter = shdma_chan_filter;
  537. dma_priv->enable = renesas_sdhi_enable_dma;
  538. mmc_data->alignment_shift = 1; /* 2-byte alignment */
  539. mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
  540. /*
  541. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  542. * bus width mode.
  543. */
  544. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  545. /*
  546. * All SDHI blocks support SDIO IRQ signalling.
  547. */
  548. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  549. /* All SDHI have CMD12 control bit */
  550. mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
  551. /* All SDHI have SDIO status bits which must be 1 */
  552. mmc_data->flags |= TMIO_MMC_SDIO_STATUS_SETBITS;
  553. ret = renesas_sdhi_clk_enable(host);
  554. if (ret)
  555. goto efree;
  556. ret = tmio_mmc_host_probe(host);
  557. if (ret < 0)
  558. goto edisclk;
  559. /* One Gen2 SDHI incarnation does NOT have a CBSY bit */
  560. if (sd_ctrl_read16(host, CTL_VERSION) == SDHI_VER_GEN2_SDR50)
  561. mmc_data->flags &= ~TMIO_MMC_HAVE_CBSY;
  562. /* Enable tuning iff we have an SCC and a supported mode */
  563. if (of_data && of_data->scc_offset &&
  564. (host->mmc->caps & MMC_CAP_UHS_SDR104 ||
  565. host->mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR |
  566. MMC_CAP2_HS400_1_8V))) {
  567. const struct renesas_sdhi_scc *taps = of_data->taps;
  568. bool hit = false;
  569. host->mmc->caps |= MMC_CAP_HW_RESET;
  570. for (i = 0; i < of_data->taps_num; i++) {
  571. if (taps[i].clk_rate == 0 ||
  572. taps[i].clk_rate == host->mmc->f_max) {
  573. priv->scc_tappos = taps->tap;
  574. hit = true;
  575. break;
  576. }
  577. }
  578. if (!hit)
  579. dev_warn(&host->pdev->dev, "Unknown clock rate for SDR104\n");
  580. priv->scc_ctl = host->ctl + of_data->scc_offset;
  581. host->init_tuning = renesas_sdhi_init_tuning;
  582. host->prepare_tuning = renesas_sdhi_prepare_tuning;
  583. host->select_tuning = renesas_sdhi_select_tuning;
  584. host->check_scc_error = renesas_sdhi_check_scc_error;
  585. host->hw_reset = renesas_sdhi_hw_reset;
  586. host->prepare_hs400_tuning =
  587. renesas_sdhi_prepare_hs400_tuning;
  588. host->hs400_downgrade = renesas_sdhi_disable_scc;
  589. host->hs400_complete = renesas_sdhi_hs400_complete;
  590. }
  591. i = 0;
  592. while (1) {
  593. irq = platform_get_irq(pdev, i);
  594. if (irq < 0)
  595. break;
  596. i++;
  597. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  598. dev_name(&pdev->dev), host);
  599. if (ret)
  600. goto eirq;
  601. }
  602. /* There must be at least one IRQ source */
  603. if (!i) {
  604. ret = irq;
  605. goto eirq;
  606. }
  607. dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
  608. mmc_hostname(host->mmc), (unsigned long)
  609. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  610. host->mmc->f_max / 1000000);
  611. return ret;
  612. eirq:
  613. tmio_mmc_host_remove(host);
  614. edisclk:
  615. renesas_sdhi_clk_disable(host);
  616. efree:
  617. tmio_mmc_host_free(host);
  618. return ret;
  619. }
  620. EXPORT_SYMBOL_GPL(renesas_sdhi_probe);
  621. int renesas_sdhi_remove(struct platform_device *pdev)
  622. {
  623. struct tmio_mmc_host *host = platform_get_drvdata(pdev);
  624. tmio_mmc_host_remove(host);
  625. renesas_sdhi_clk_disable(host);
  626. return 0;
  627. }
  628. EXPORT_SYMBOL_GPL(renesas_sdhi_remove);
  629. MODULE_LICENSE("GPL v2");