renesas_sdhi_core.c 18 KB

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