sh_mobile_sdhi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 clk *clk_cd;
  131. struct tmio_mmc_data mmc_data;
  132. struct tmio_mmc_dma dma_priv;
  133. struct pinctrl *pinctrl;
  134. struct pinctrl_state *pins_default, *pins_uhs;
  135. void __iomem *scc_ctl;
  136. };
  137. static void sh_mobile_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
  138. {
  139. u32 val;
  140. /*
  141. * see also
  142. * sh_mobile_sdhi_of_data :: dma_buswidth
  143. */
  144. switch (sd_ctrl_read16(host, CTL_VERSION)) {
  145. case SDHI_VER_GEN2_SDR50:
  146. val = (width == 32) ? 0x0001 : 0x0000;
  147. break;
  148. case SDHI_VER_GEN2_SDR104:
  149. val = (width == 32) ? 0x0000 : 0x0001;
  150. break;
  151. case SDHI_VER_GEN3_SD:
  152. case SDHI_VER_GEN3_SDMMC:
  153. if (width == 64)
  154. val = 0x0000;
  155. else if (width == 32)
  156. val = 0x0101;
  157. else
  158. val = 0x0001;
  159. break;
  160. default:
  161. /* nothing to do */
  162. return;
  163. }
  164. sd_ctrl_write16(host, EXT_ACC, val);
  165. }
  166. static int sh_mobile_sdhi_clk_enable(struct tmio_mmc_host *host)
  167. {
  168. struct mmc_host *mmc = host->mmc;
  169. struct sh_mobile_sdhi *priv = host_to_priv(host);
  170. int ret = clk_prepare_enable(priv->clk);
  171. if (ret < 0)
  172. return ret;
  173. ret = clk_prepare_enable(priv->clk_cd);
  174. if (ret < 0) {
  175. clk_disable_unprepare(priv->clk);
  176. return ret;
  177. }
  178. /*
  179. * The clock driver may not know what maximum frequency
  180. * actually works, so it should be set with the max-frequency
  181. * property which will already have been read to f_max. If it
  182. * was missing, assume the current frequency is the maximum.
  183. */
  184. if (!mmc->f_max)
  185. mmc->f_max = clk_get_rate(priv->clk);
  186. /*
  187. * Minimum frequency is the minimum input clock frequency
  188. * divided by our maximum divider.
  189. */
  190. mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
  191. /* enable 16bit data access on SDBUF as default */
  192. sh_mobile_sdhi_sdbuf_width(host, 16);
  193. return 0;
  194. }
  195. static unsigned int sh_mobile_sdhi_clk_update(struct tmio_mmc_host *host,
  196. unsigned int new_clock)
  197. {
  198. struct sh_mobile_sdhi *priv = host_to_priv(host);
  199. unsigned int freq, diff, best_freq = 0, diff_min = ~0;
  200. int i, ret;
  201. /* tested only on RCar Gen2+ currently; may work for others */
  202. if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
  203. return clk_get_rate(priv->clk);
  204. /*
  205. * We want the bus clock to be as close as possible to, but no
  206. * greater than, new_clock. As we can divide by 1 << i for
  207. * any i in [0, 9] we want the input clock to be as close as
  208. * possible, but no greater than, new_clock << i.
  209. */
  210. for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
  211. freq = clk_round_rate(priv->clk, new_clock << i);
  212. if (freq > (new_clock << i)) {
  213. /* Too fast; look for a slightly slower option */
  214. freq = clk_round_rate(priv->clk,
  215. (new_clock << i) / 4 * 3);
  216. if (freq > (new_clock << i))
  217. continue;
  218. }
  219. diff = new_clock - (freq >> i);
  220. if (diff <= diff_min) {
  221. best_freq = freq;
  222. diff_min = diff;
  223. }
  224. }
  225. ret = clk_set_rate(priv->clk, best_freq);
  226. return ret == 0 ? best_freq : clk_get_rate(priv->clk);
  227. }
  228. static void sh_mobile_sdhi_clk_disable(struct tmio_mmc_host *host)
  229. {
  230. struct sh_mobile_sdhi *priv = host_to_priv(host);
  231. clk_disable_unprepare(priv->clk);
  232. clk_disable_unprepare(priv->clk_cd);
  233. }
  234. static int sh_mobile_sdhi_card_busy(struct mmc_host *mmc)
  235. {
  236. struct tmio_mmc_host *host = mmc_priv(mmc);
  237. return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_DAT0);
  238. }
  239. static int sh_mobile_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
  240. struct mmc_ios *ios)
  241. {
  242. struct tmio_mmc_host *host = mmc_priv(mmc);
  243. struct sh_mobile_sdhi *priv = host_to_priv(host);
  244. struct pinctrl_state *pin_state;
  245. int ret;
  246. switch (ios->signal_voltage) {
  247. case MMC_SIGNAL_VOLTAGE_330:
  248. pin_state = priv->pins_default;
  249. break;
  250. case MMC_SIGNAL_VOLTAGE_180:
  251. pin_state = priv->pins_uhs;
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. /*
  257. * If anything is missing, assume signal voltage is fixed at
  258. * 3.3V and succeed/fail accordingly.
  259. */
  260. if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
  261. return ios->signal_voltage ==
  262. MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
  263. ret = mmc_regulator_set_vqmmc(host->mmc, ios);
  264. if (ret)
  265. return ret;
  266. return pinctrl_select_state(priv->pinctrl, pin_state);
  267. }
  268. /* SCC registers */
  269. #define SH_MOBILE_SDHI_SCC_DTCNTL 0x000
  270. #define SH_MOBILE_SDHI_SCC_TAPSET 0x002
  271. #define SH_MOBILE_SDHI_SCC_DT2FF 0x004
  272. #define SH_MOBILE_SDHI_SCC_CKSEL 0x006
  273. #define SH_MOBILE_SDHI_SCC_RVSCNTL 0x008
  274. #define SH_MOBILE_SDHI_SCC_RVSREQ 0x00A
  275. /* Definitions for values the SH_MOBILE_SDHI_SCC_DTCNTL register */
  276. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN BIT(0)
  277. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT 16
  278. #define SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK 0xff
  279. /* Definitions for values the SH_MOBILE_SDHI_SCC_CKSEL register */
  280. #define SH_MOBILE_SDHI_SCC_CKSEL_DTSEL BIT(0)
  281. /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSCNTL register */
  282. #define SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN BIT(0)
  283. /* Definitions for values the SH_MOBILE_SDHI_SCC_RVSREQ register */
  284. #define SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR BIT(2)
  285. static inline u32 sd_scc_read32(struct tmio_mmc_host *host,
  286. struct sh_mobile_sdhi *priv, int addr)
  287. {
  288. return readl(priv->scc_ctl + (addr << host->bus_shift));
  289. }
  290. static inline void sd_scc_write32(struct tmio_mmc_host *host,
  291. struct sh_mobile_sdhi *priv,
  292. int addr, u32 val)
  293. {
  294. writel(val, priv->scc_ctl + (addr << host->bus_shift));
  295. }
  296. static unsigned int sh_mobile_sdhi_init_tuning(struct tmio_mmc_host *host)
  297. {
  298. struct sh_mobile_sdhi *priv;
  299. priv = host_to_priv(host);
  300. /* set sampling clock selection range */
  301. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
  302. 0x8 << SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT);
  303. /* Initialize SCC */
  304. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, 0x0);
  305. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL,
  306. SH_MOBILE_SDHI_SCC_DTCNTL_TAPEN |
  307. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL));
  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_CKSEL,
  311. SH_MOBILE_SDHI_SCC_CKSEL_DTSEL |
  312. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_CKSEL));
  313. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  314. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  315. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  316. ~SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &
  317. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  318. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_DT2FF, host->scc_tappos);
  319. /* Read TAPNUM */
  320. return (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_DTCNTL) >>
  321. SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_SHIFT) &
  322. SH_MOBILE_SDHI_SCC_DTCNTL_TAPNUM_MASK;
  323. }
  324. static void sh_mobile_sdhi_prepare_tuning(struct tmio_mmc_host *host,
  325. unsigned long tap)
  326. {
  327. struct sh_mobile_sdhi *priv = host_to_priv(host);
  328. /* Set sampling clock position */
  329. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, tap);
  330. }
  331. #define SH_MOBILE_SDHI_MAX_TAP 3
  332. static int sh_mobile_sdhi_select_tuning(struct tmio_mmc_host *host)
  333. {
  334. struct sh_mobile_sdhi *priv = host_to_priv(host);
  335. unsigned long tap_cnt; /* counter of tuning success */
  336. unsigned long tap_set; /* tap position */
  337. unsigned long tap_start;/* start position of tuning success */
  338. unsigned long tap_end; /* end position of tuning success */
  339. unsigned long ntap; /* temporary counter of tuning success */
  340. unsigned long i;
  341. /* Clear SCC_RVSREQ */
  342. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
  343. /*
  344. * Find the longest consecutive run of successful probes. If that
  345. * is more than SH_MOBILE_SDHI_MAX_TAP probes long then use the
  346. * center index as the tap.
  347. */
  348. tap_cnt = 0;
  349. ntap = 0;
  350. tap_start = 0;
  351. tap_end = 0;
  352. for (i = 0; i < host->tap_num * 2; i++) {
  353. if (test_bit(i, host->taps))
  354. ntap++;
  355. else {
  356. if (ntap > tap_cnt) {
  357. tap_start = i - ntap;
  358. tap_end = i - 1;
  359. tap_cnt = ntap;
  360. }
  361. ntap = 0;
  362. }
  363. }
  364. if (ntap > tap_cnt) {
  365. tap_start = i - ntap;
  366. tap_end = i - 1;
  367. tap_cnt = ntap;
  368. }
  369. if (tap_cnt >= SH_MOBILE_SDHI_MAX_TAP)
  370. tap_set = (tap_start + tap_end) / 2 % host->tap_num;
  371. else
  372. return -EIO;
  373. /* Set SCC */
  374. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, tap_set);
  375. /* Enable auto re-tuning */
  376. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL,
  377. SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN |
  378. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL));
  379. return 0;
  380. }
  381. static bool sh_mobile_sdhi_check_scc_error(struct tmio_mmc_host *host)
  382. {
  383. struct sh_mobile_sdhi *priv = host_to_priv(host);
  384. /* Check SCC error */
  385. if (sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSCNTL) &
  386. SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN &&
  387. sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ) &
  388. SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR) {
  389. /* Clear SCC error */
  390. sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_RVSREQ, 0);
  391. return true;
  392. }
  393. return false;
  394. }
  395. static void sh_mobile_sdhi_hw_reset(struct tmio_mmc_host *host)
  396. {
  397. struct sh_mobile_sdhi *priv;
  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 sh_mobile_sdhi_of_data *of_data = of_device_get_match_data(&pdev->dev);
  468. struct sh_mobile_sdhi *priv;
  469. struct tmio_mmc_data *mmc_data;
  470. struct tmio_mmc_data *mmd = pdev->dev.platform_data;
  471. struct tmio_mmc_host *host;
  472. struct resource *res;
  473. int irq, ret, i;
  474. struct tmio_mmc_dma *dma_priv;
  475. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  476. if (!res)
  477. return -EINVAL;
  478. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  479. if (!priv)
  480. return -ENOMEM;
  481. mmc_data = &priv->mmc_data;
  482. dma_priv = &priv->dma_priv;
  483. priv->clk = devm_clk_get(&pdev->dev, NULL);
  484. if (IS_ERR(priv->clk)) {
  485. ret = PTR_ERR(priv->clk);
  486. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  487. goto eprobe;
  488. }
  489. /*
  490. * Some controllers provide a 2nd clock just to run the internal card
  491. * detection logic. Unfortunately, the existing driver architecture does
  492. * not support a separation of clocks for runtime PM usage. When
  493. * native hotplug is used, the tmio driver assumes that the core
  494. * must continue to run for card detect to stay active, so we cannot
  495. * disable it.
  496. * Additionally, it is prohibited to supply a clock to the core but not
  497. * to the card detect circuit. That leaves us with if separate clocks
  498. * are presented, we must treat them both as virtually 1 clock.
  499. */
  500. priv->clk_cd = devm_clk_get(&pdev->dev, "cd");
  501. if (IS_ERR(priv->clk_cd))
  502. priv->clk_cd = NULL;
  503. priv->pinctrl = devm_pinctrl_get(&pdev->dev);
  504. if (!IS_ERR(priv->pinctrl)) {
  505. priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
  506. PINCTRL_STATE_DEFAULT);
  507. priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
  508. "state_uhs");
  509. }
  510. host = tmio_mmc_host_alloc(pdev);
  511. if (!host) {
  512. ret = -ENOMEM;
  513. goto eprobe;
  514. }
  515. if (of_data) {
  516. mmc_data->flags |= of_data->tmio_flags;
  517. mmc_data->ocr_mask = of_data->tmio_ocr_mask;
  518. mmc_data->capabilities |= of_data->capabilities;
  519. mmc_data->capabilities2 |= of_data->capabilities2;
  520. mmc_data->dma_rx_offset = of_data->dma_rx_offset;
  521. dma_priv->dma_buswidth = of_data->dma_buswidth;
  522. host->bus_shift = of_data->bus_shift;
  523. }
  524. host->dma = dma_priv;
  525. host->write16_hook = sh_mobile_sdhi_write16_hook;
  526. host->clk_enable = sh_mobile_sdhi_clk_enable;
  527. host->clk_update = sh_mobile_sdhi_clk_update;
  528. host->clk_disable = sh_mobile_sdhi_clk_disable;
  529. host->multi_io_quirk = sh_mobile_sdhi_multi_io_quirk;
  530. /* SDR speeds are only available on Gen2+ */
  531. if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
  532. /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
  533. host->card_busy = sh_mobile_sdhi_card_busy;
  534. host->start_signal_voltage_switch =
  535. sh_mobile_sdhi_start_signal_voltage_switch;
  536. }
  537. /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
  538. if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
  539. host->bus_shift = 1;
  540. if (mmd)
  541. *mmc_data = *mmd;
  542. dma_priv->filter = shdma_chan_filter;
  543. dma_priv->enable = sh_mobile_sdhi_enable_dma;
  544. mmc_data->alignment_shift = 1; /* 2-byte alignment */
  545. mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
  546. /*
  547. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  548. * bus width mode.
  549. */
  550. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  551. /*
  552. * All SDHI blocks support SDIO IRQ signalling.
  553. */
  554. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  555. /*
  556. * All SDHI have CMD12 controll bit
  557. */
  558. mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
  559. /* All SDHI have SDIO status bits which must be 1 */
  560. mmc_data->flags |= TMIO_MMC_SDIO_STATUS_SETBITS;
  561. ret = tmio_mmc_host_probe(host, mmc_data);
  562. if (ret < 0)
  563. goto efree;
  564. /* Enable tuning iff we have an SCC and a supported mode */
  565. if (of_data && of_data->scc_offset &&
  566. (host->mmc->caps & MMC_CAP_UHS_SDR104 ||
  567. host->mmc->caps2 & MMC_CAP2_HS200_1_8V_SDR)) {
  568. const struct sh_mobile_sdhi_scc *taps = of_data->taps;
  569. bool hit = false;
  570. host->mmc->caps |= MMC_CAP_HW_RESET;
  571. for (i = 0; i < of_data->taps_num; i++) {
  572. if (taps[i].clk_rate == 0 ||
  573. taps[i].clk_rate == host->mmc->f_max) {
  574. host->scc_tappos = taps->tap;
  575. hit = true;
  576. break;
  577. }
  578. }
  579. if (!hit)
  580. dev_warn(&host->pdev->dev, "Unknown clock rate for SDR104\n");
  581. priv->scc_ctl = host->ctl + of_data->scc_offset;
  582. host->init_tuning = sh_mobile_sdhi_init_tuning;
  583. host->prepare_tuning = sh_mobile_sdhi_prepare_tuning;
  584. host->select_tuning = sh_mobile_sdhi_select_tuning;
  585. host->check_scc_error = sh_mobile_sdhi_check_scc_error;
  586. host->hw_reset = sh_mobile_sdhi_hw_reset;
  587. }
  588. i = 0;
  589. while (1) {
  590. irq = platform_get_irq(pdev, i);
  591. if (irq < 0)
  592. break;
  593. i++;
  594. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  595. dev_name(&pdev->dev), host);
  596. if (ret)
  597. goto eirq;
  598. }
  599. /* There must be at least one IRQ source */
  600. if (!i) {
  601. ret = irq;
  602. goto eirq;
  603. }
  604. dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
  605. mmc_hostname(host->mmc), (unsigned long)
  606. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  607. host->mmc->f_max / 1000000);
  608. return ret;
  609. eirq:
  610. tmio_mmc_host_remove(host);
  611. efree:
  612. tmio_mmc_host_free(host);
  613. eprobe:
  614. return ret;
  615. }
  616. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  617. {
  618. struct mmc_host *mmc = platform_get_drvdata(pdev);
  619. struct tmio_mmc_host *host = mmc_priv(mmc);
  620. tmio_mmc_host_remove(host);
  621. return 0;
  622. }
  623. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  624. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  625. pm_runtime_force_resume)
  626. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  627. tmio_mmc_host_runtime_resume,
  628. NULL)
  629. };
  630. static struct platform_driver sh_mobile_sdhi_driver = {
  631. .driver = {
  632. .name = "sh_mobile_sdhi",
  633. .pm = &tmio_mmc_dev_pm_ops,
  634. .of_match_table = sh_mobile_sdhi_of_match,
  635. },
  636. .probe = sh_mobile_sdhi_probe,
  637. .remove = sh_mobile_sdhi_remove,
  638. };
  639. module_platform_driver(sh_mobile_sdhi_driver);
  640. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  641. MODULE_AUTHOR("Magnus Damm");
  642. MODULE_LICENSE("GPL v2");
  643. MODULE_ALIAS("platform:sh_mobile_sdhi");