sh_mobile_sdhi.c 21 KB

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