sh_mobile_sdhi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_of_data {
  45. unsigned long tmio_flags;
  46. unsigned long capabilities;
  47. unsigned long capabilities2;
  48. enum dma_slave_buswidth dma_buswidth;
  49. dma_addr_t dma_rx_offset;
  50. unsigned bus_shift;
  51. };
  52. static const struct sh_mobile_sdhi_of_data of_default_cfg = {
  53. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
  54. };
  55. static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = {
  56. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  57. TMIO_MMC_CLK_ACTUAL,
  58. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  59. };
  60. static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = {
  61. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  62. TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2,
  63. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  64. .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
  65. .dma_rx_offset = 0x2000,
  66. };
  67. static const struct sh_mobile_sdhi_of_data of_rcar_gen3_compatible = {
  68. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  69. TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2,
  70. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  71. .bus_shift = 2,
  72. };
  73. static const struct of_device_id sh_mobile_sdhi_of_match[] = {
  74. { .compatible = "renesas,sdhi-shmobile" },
  75. { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
  76. { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
  77. { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
  78. { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
  79. { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
  80. { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
  81. { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
  82. { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
  83. { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
  84. { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
  85. { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
  86. { .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_gen3_compatible, },
  87. {},
  88. };
  89. MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
  90. struct sh_mobile_sdhi {
  91. struct clk *clk;
  92. struct tmio_mmc_data mmc_data;
  93. struct tmio_mmc_dma dma_priv;
  94. struct pinctrl *pinctrl;
  95. struct pinctrl_state *pins_default, *pins_uhs;
  96. };
  97. static void sh_mobile_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
  98. {
  99. u32 val;
  100. /*
  101. * see also
  102. * sh_mobile_sdhi_of_data :: dma_buswidth
  103. */
  104. switch (sd_ctrl_read16(host, CTL_VERSION)) {
  105. case SDHI_VER_GEN2_SDR50:
  106. val = (width == 32) ? 0x0001 : 0x0000;
  107. break;
  108. case SDHI_VER_GEN2_SDR104:
  109. val = (width == 32) ? 0x0000 : 0x0001;
  110. break;
  111. case SDHI_VER_GEN3_SD:
  112. case SDHI_VER_GEN3_SDMMC:
  113. if (width == 64)
  114. val = 0x0000;
  115. else if (width == 32)
  116. val = 0x0101;
  117. else
  118. val = 0x0001;
  119. break;
  120. default:
  121. /* nothing to do */
  122. return;
  123. }
  124. sd_ctrl_write16(host, EXT_ACC, val);
  125. }
  126. static int sh_mobile_sdhi_clk_enable(struct tmio_mmc_host *host)
  127. {
  128. struct mmc_host *mmc = host->mmc;
  129. struct sh_mobile_sdhi *priv = host_to_priv(host);
  130. int ret = clk_prepare_enable(priv->clk);
  131. if (ret < 0)
  132. return ret;
  133. /*
  134. * The clock driver may not know what maximum frequency
  135. * actually works, so it should be set with the max-frequency
  136. * property which will already have been read to f_max. If it
  137. * was missing, assume the current frequency is the maximum.
  138. */
  139. if (!mmc->f_max)
  140. mmc->f_max = clk_get_rate(priv->clk);
  141. /*
  142. * Minimum frequency is the minimum input clock frequency
  143. * divided by our maximum divider.
  144. */
  145. mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
  146. /* enable 16bit data access on SDBUF as default */
  147. sh_mobile_sdhi_sdbuf_width(host, 16);
  148. return 0;
  149. }
  150. static unsigned int sh_mobile_sdhi_clk_update(struct tmio_mmc_host *host,
  151. unsigned int new_clock)
  152. {
  153. struct sh_mobile_sdhi *priv = host_to_priv(host);
  154. unsigned int freq, diff, best_freq = 0, diff_min = ~0;
  155. int i, ret;
  156. /* tested only on RCar Gen2+ currently; may work for others */
  157. if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
  158. return clk_get_rate(priv->clk);
  159. /*
  160. * We want the bus clock to be as close as possible to, but no
  161. * greater than, new_clock. As we can divide by 1 << i for
  162. * any i in [0, 9] we want the input clock to be as close as
  163. * possible, but no greater than, new_clock << i.
  164. */
  165. for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
  166. freq = clk_round_rate(priv->clk, new_clock << i);
  167. if (freq > (new_clock << i)) {
  168. /* Too fast; look for a slightly slower option */
  169. freq = clk_round_rate(priv->clk,
  170. (new_clock << i) / 4 * 3);
  171. if (freq > (new_clock << i))
  172. continue;
  173. }
  174. diff = new_clock - (freq >> i);
  175. if (diff <= diff_min) {
  176. best_freq = freq;
  177. diff_min = diff;
  178. }
  179. }
  180. ret = clk_set_rate(priv->clk, best_freq);
  181. return ret == 0 ? best_freq : clk_get_rate(priv->clk);
  182. }
  183. static void sh_mobile_sdhi_clk_disable(struct tmio_mmc_host *host)
  184. {
  185. struct sh_mobile_sdhi *priv = host_to_priv(host);
  186. clk_disable_unprepare(priv->clk);
  187. }
  188. static int sh_mobile_sdhi_card_busy(struct mmc_host *mmc)
  189. {
  190. struct tmio_mmc_host *host = mmc_priv(mmc);
  191. return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_DAT0);
  192. }
  193. static int sh_mobile_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
  194. struct mmc_ios *ios)
  195. {
  196. struct tmio_mmc_host *host = mmc_priv(mmc);
  197. struct sh_mobile_sdhi *priv = host_to_priv(host);
  198. struct pinctrl_state *pin_state;
  199. int ret;
  200. switch (ios->signal_voltage) {
  201. case MMC_SIGNAL_VOLTAGE_330:
  202. pin_state = priv->pins_default;
  203. break;
  204. case MMC_SIGNAL_VOLTAGE_180:
  205. pin_state = priv->pins_uhs;
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. /*
  211. * If anything is missing, assume signal voltage is fixed at
  212. * 3.3V and succeed/fail accordingly.
  213. */
  214. if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
  215. return ios->signal_voltage ==
  216. MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
  217. ret = mmc_regulator_set_vqmmc(host->mmc, ios);
  218. if (ret)
  219. return ret;
  220. return pinctrl_select_state(priv->pinctrl, pin_state);
  221. }
  222. static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
  223. {
  224. int timeout = 1000;
  225. while (--timeout && !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS)
  226. & TMIO_STAT_SCLKDIVEN))
  227. udelay(1);
  228. if (!timeout) {
  229. dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
  230. return -EBUSY;
  231. }
  232. return 0;
  233. }
  234. static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  235. {
  236. switch (addr)
  237. {
  238. case CTL_SD_CMD:
  239. case CTL_STOP_INTERNAL_ACTION:
  240. case CTL_XFER_BLK_COUNT:
  241. case CTL_SD_CARD_CLK_CTL:
  242. case CTL_SD_XFER_LEN:
  243. case CTL_SD_MEM_CARD_OPT:
  244. case CTL_TRANSACTION_CTL:
  245. case CTL_DMA_ENABLE:
  246. case EXT_ACC:
  247. return sh_mobile_sdhi_wait_idle(host);
  248. }
  249. return 0;
  250. }
  251. static int sh_mobile_sdhi_multi_io_quirk(struct mmc_card *card,
  252. unsigned int direction, int blk_size)
  253. {
  254. /*
  255. * In Renesas controllers, when performing a
  256. * multiple block read of one or two blocks,
  257. * depending on the timing with which the
  258. * response register is read, the response
  259. * value may not be read properly.
  260. * Use single block read for this HW bug
  261. */
  262. if ((direction == MMC_DATA_READ) &&
  263. blk_size == 2)
  264. return 1;
  265. return blk_size;
  266. }
  267. static void sh_mobile_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
  268. {
  269. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
  270. /* enable 32bit access if DMA mode if possibile */
  271. sh_mobile_sdhi_sdbuf_width(host, enable ? 32 : 16);
  272. }
  273. static int sh_mobile_sdhi_probe(struct platform_device *pdev)
  274. {
  275. const struct of_device_id *of_id =
  276. of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
  277. struct sh_mobile_sdhi *priv;
  278. struct tmio_mmc_data *mmc_data;
  279. struct tmio_mmc_data *mmd = pdev->dev.platform_data;
  280. struct tmio_mmc_host *host;
  281. struct resource *res;
  282. int irq, ret, i = 0;
  283. struct tmio_mmc_dma *dma_priv;
  284. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. if (!res)
  286. return -EINVAL;
  287. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  288. if (!priv)
  289. return -ENOMEM;
  290. mmc_data = &priv->mmc_data;
  291. dma_priv = &priv->dma_priv;
  292. priv->clk = devm_clk_get(&pdev->dev, NULL);
  293. if (IS_ERR(priv->clk)) {
  294. ret = PTR_ERR(priv->clk);
  295. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  296. goto eprobe;
  297. }
  298. priv->pinctrl = devm_pinctrl_get(&pdev->dev);
  299. if (!IS_ERR(priv->pinctrl)) {
  300. priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
  301. PINCTRL_STATE_DEFAULT);
  302. priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
  303. "state_uhs");
  304. }
  305. host = tmio_mmc_host_alloc(pdev);
  306. if (!host) {
  307. ret = -ENOMEM;
  308. goto eprobe;
  309. }
  310. if (of_id && of_id->data) {
  311. const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
  312. mmc_data->flags |= of_data->tmio_flags;
  313. mmc_data->capabilities |= of_data->capabilities;
  314. mmc_data->capabilities2 |= of_data->capabilities2;
  315. mmc_data->dma_rx_offset = of_data->dma_rx_offset;
  316. dma_priv->dma_buswidth = of_data->dma_buswidth;
  317. host->bus_shift = of_data->bus_shift;
  318. }
  319. host->dma = dma_priv;
  320. host->write16_hook = sh_mobile_sdhi_write16_hook;
  321. host->clk_enable = sh_mobile_sdhi_clk_enable;
  322. host->clk_update = sh_mobile_sdhi_clk_update;
  323. host->clk_disable = sh_mobile_sdhi_clk_disable;
  324. host->multi_io_quirk = sh_mobile_sdhi_multi_io_quirk;
  325. /* SDR speeds are only available on Gen2+ */
  326. if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
  327. /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
  328. host->card_busy = sh_mobile_sdhi_card_busy;
  329. host->start_signal_voltage_switch =
  330. sh_mobile_sdhi_start_signal_voltage_switch;
  331. }
  332. /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
  333. if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
  334. host->bus_shift = 1;
  335. if (mmd)
  336. *mmc_data = *mmd;
  337. dma_priv->filter = shdma_chan_filter;
  338. dma_priv->enable = sh_mobile_sdhi_enable_dma;
  339. mmc_data->alignment_shift = 1; /* 2-byte alignment */
  340. mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
  341. /*
  342. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  343. * bus width mode.
  344. */
  345. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  346. /*
  347. * All SDHI blocks support SDIO IRQ signalling.
  348. */
  349. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  350. /*
  351. * All SDHI have CMD12 controll bit
  352. */
  353. mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
  354. /*
  355. * All SDHI need SDIO_INFO1 reserved bit
  356. */
  357. mmc_data->flags |= TMIO_MMC_SDIO_STATUS_QUIRK;
  358. ret = tmio_mmc_host_probe(host, mmc_data);
  359. if (ret < 0)
  360. goto efree;
  361. while (1) {
  362. irq = platform_get_irq(pdev, i);
  363. if (irq < 0)
  364. break;
  365. i++;
  366. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  367. dev_name(&pdev->dev), host);
  368. if (ret)
  369. goto eirq;
  370. }
  371. /* There must be at least one IRQ source */
  372. if (!i) {
  373. ret = irq;
  374. goto eirq;
  375. }
  376. dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
  377. mmc_hostname(host->mmc), (unsigned long)
  378. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  379. host->mmc->f_max / 1000000);
  380. return ret;
  381. eirq:
  382. tmio_mmc_host_remove(host);
  383. efree:
  384. tmio_mmc_host_free(host);
  385. eprobe:
  386. return ret;
  387. }
  388. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  389. {
  390. struct mmc_host *mmc = platform_get_drvdata(pdev);
  391. struct tmio_mmc_host *host = mmc_priv(mmc);
  392. tmio_mmc_host_remove(host);
  393. return 0;
  394. }
  395. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  396. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  397. pm_runtime_force_resume)
  398. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  399. tmio_mmc_host_runtime_resume,
  400. NULL)
  401. };
  402. static struct platform_driver sh_mobile_sdhi_driver = {
  403. .driver = {
  404. .name = "sh_mobile_sdhi",
  405. .pm = &tmio_mmc_dev_pm_ops,
  406. .of_match_table = sh_mobile_sdhi_of_match,
  407. },
  408. .probe = sh_mobile_sdhi_probe,
  409. .remove = sh_mobile_sdhi_remove,
  410. };
  411. module_platform_driver(sh_mobile_sdhi_driver);
  412. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  413. MODULE_AUTHOR("Magnus Damm");
  414. MODULE_LICENSE("GPL v2");
  415. MODULE_ALIAS("platform:sh_mobile_sdhi");