sh_mobile_sdhi.c 13 KB

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