renesas_sdhi_core.c 18 KB

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