renesas_sdhi_core.c 18 KB

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