sh_mobile_sdhi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/mmc/sh_mobile_sdhi.h>
  31. #include <linux/mfd/tmio.h>
  32. #include <linux/sh_dma.h>
  33. #include <linux/delay.h>
  34. #include "tmio_mmc.h"
  35. #define EXT_ACC 0xe4
  36. #define host_to_priv(host) container_of((host)->pdata, struct sh_mobile_sdhi, mmc_data)
  37. struct sh_mobile_sdhi_of_data {
  38. unsigned long tmio_flags;
  39. unsigned long capabilities;
  40. unsigned long capabilities2;
  41. enum dma_slave_buswidth dma_buswidth;
  42. dma_addr_t dma_rx_offset;
  43. unsigned bus_shift;
  44. };
  45. static const struct sh_mobile_sdhi_of_data sh_mobile_sdhi_of_cfg[] = {
  46. {
  47. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
  48. },
  49. };
  50. static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = {
  51. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  52. TMIO_MMC_CLK_ACTUAL,
  53. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  54. };
  55. static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = {
  56. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  57. TMIO_MMC_CLK_ACTUAL | TMIO_MMC_FAST_CLK_CHG,
  58. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
  59. .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
  60. .dma_rx_offset = 0x2000,
  61. };
  62. static const struct sh_mobile_sdhi_of_data of_rcar_gen3_compatible = {
  63. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  64. TMIO_MMC_CLK_ACTUAL | TMIO_MMC_FAST_CLK_CHG,
  65. .capabilities = MMC_CAP_SD_HIGHSPEED,
  66. .bus_shift = 2,
  67. };
  68. static const struct of_device_id sh_mobile_sdhi_of_match[] = {
  69. { .compatible = "renesas,sdhi-shmobile" },
  70. { .compatible = "renesas,sdhi-sh7372" },
  71. { .compatible = "renesas,sdhi-sh73a0", .data = &sh_mobile_sdhi_of_cfg[0], },
  72. { .compatible = "renesas,sdhi-r8a73a4", .data = &sh_mobile_sdhi_of_cfg[0], },
  73. { .compatible = "renesas,sdhi-r8a7740", .data = &sh_mobile_sdhi_of_cfg[0], },
  74. { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
  75. { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
  76. { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
  77. { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
  78. { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
  79. { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
  80. { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
  81. { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
  82. {},
  83. };
  84. MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
  85. struct sh_mobile_sdhi {
  86. struct clk *clk;
  87. struct tmio_mmc_data mmc_data;
  88. struct tmio_mmc_dma dma_priv;
  89. };
  90. static void sh_mobile_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
  91. {
  92. u32 val;
  93. /*
  94. * see also
  95. * sh_mobile_sdhi_of_data :: dma_buswidth
  96. */
  97. switch (sd_ctrl_read16(host, CTL_VERSION)) {
  98. case 0x490C:
  99. val = (width == 32) ? 0x0001 : 0x0000;
  100. break;
  101. case 0xCB0D:
  102. val = (width == 32) ? 0x0000 : 0x0001;
  103. break;
  104. case 0xCC10: /* Gen3, SD only */
  105. case 0xCD10: /* Gen3, SD + MMC */
  106. if (width == 64)
  107. val = 0x0000;
  108. else if (width == 32)
  109. val = 0x0101;
  110. else
  111. val = 0x0001;
  112. break;
  113. default:
  114. /* nothing to do */
  115. return;
  116. }
  117. sd_ctrl_write16(host, EXT_ACC, val);
  118. }
  119. static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
  120. {
  121. struct mmc_host *mmc = platform_get_drvdata(pdev);
  122. struct tmio_mmc_host *host = mmc_priv(mmc);
  123. struct sh_mobile_sdhi *priv = host_to_priv(host);
  124. int ret = clk_prepare_enable(priv->clk);
  125. if (ret < 0)
  126. return ret;
  127. *f = clk_get_rate(priv->clk);
  128. /* enable 16bit data access on SDBUF as default */
  129. sh_mobile_sdhi_sdbuf_width(host, 16);
  130. return 0;
  131. }
  132. static void sh_mobile_sdhi_clk_disable(struct platform_device *pdev)
  133. {
  134. struct mmc_host *mmc = platform_get_drvdata(pdev);
  135. struct tmio_mmc_host *host = mmc_priv(mmc);
  136. struct sh_mobile_sdhi *priv = host_to_priv(host);
  137. clk_disable_unprepare(priv->clk);
  138. }
  139. static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
  140. {
  141. int timeout = 1000;
  142. while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
  143. udelay(1);
  144. if (!timeout) {
  145. dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
  146. return -EBUSY;
  147. }
  148. return 0;
  149. }
  150. static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  151. {
  152. switch (addr)
  153. {
  154. case CTL_SD_CMD:
  155. case CTL_STOP_INTERNAL_ACTION:
  156. case CTL_XFER_BLK_COUNT:
  157. case CTL_SD_CARD_CLK_CTL:
  158. case CTL_SD_XFER_LEN:
  159. case CTL_SD_MEM_CARD_OPT:
  160. case CTL_TRANSACTION_CTL:
  161. case CTL_DMA_ENABLE:
  162. case EXT_ACC:
  163. return sh_mobile_sdhi_wait_idle(host);
  164. }
  165. return 0;
  166. }
  167. static int sh_mobile_sdhi_multi_io_quirk(struct mmc_card *card,
  168. unsigned int direction, int blk_size)
  169. {
  170. /*
  171. * In Renesas controllers, when performing a
  172. * multiple block read of one or two blocks,
  173. * depending on the timing with which the
  174. * response register is read, the response
  175. * value may not be read properly.
  176. * Use single block read for this HW bug
  177. */
  178. if ((direction == MMC_DATA_READ) &&
  179. blk_size == 2)
  180. return 1;
  181. return blk_size;
  182. }
  183. static void sh_mobile_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
  184. {
  185. sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
  186. /* enable 32bit access if DMA mode if possibile */
  187. sh_mobile_sdhi_sdbuf_width(host, enable ? 32 : 16);
  188. }
  189. static int sh_mobile_sdhi_probe(struct platform_device *pdev)
  190. {
  191. const struct of_device_id *of_id =
  192. of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
  193. struct sh_mobile_sdhi *priv;
  194. struct tmio_mmc_data *mmc_data;
  195. struct tmio_mmc_data *mmd = pdev->dev.platform_data;
  196. struct tmio_mmc_host *host;
  197. struct resource *res;
  198. int irq, ret, i = 0;
  199. bool multiplexed_isr = true;
  200. struct tmio_mmc_dma *dma_priv;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. if (!res)
  203. return -EINVAL;
  204. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  205. if (!priv)
  206. return -ENOMEM;
  207. mmc_data = &priv->mmc_data;
  208. dma_priv = &priv->dma_priv;
  209. priv->clk = devm_clk_get(&pdev->dev, NULL);
  210. if (IS_ERR(priv->clk)) {
  211. ret = PTR_ERR(priv->clk);
  212. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  213. goto eprobe;
  214. }
  215. host = tmio_mmc_host_alloc(pdev);
  216. if (!host) {
  217. ret = -ENOMEM;
  218. goto eprobe;
  219. }
  220. if (of_id && of_id->data) {
  221. const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
  222. mmc_data->flags |= of_data->tmio_flags;
  223. mmc_data->capabilities |= of_data->capabilities;
  224. mmc_data->capabilities2 |= of_data->capabilities2;
  225. mmc_data->dma_rx_offset = of_data->dma_rx_offset;
  226. dma_priv->dma_buswidth = of_data->dma_buswidth;
  227. host->bus_shift = of_data->bus_shift;
  228. }
  229. host->dma = dma_priv;
  230. host->write16_hook = sh_mobile_sdhi_write16_hook;
  231. host->clk_enable = sh_mobile_sdhi_clk_enable;
  232. host->clk_disable = sh_mobile_sdhi_clk_disable;
  233. host->multi_io_quirk = sh_mobile_sdhi_multi_io_quirk;
  234. /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
  235. if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
  236. host->bus_shift = 1;
  237. if (mmd)
  238. *mmc_data = *mmd;
  239. dma_priv->filter = shdma_chan_filter;
  240. dma_priv->enable = sh_mobile_sdhi_enable_dma;
  241. mmc_data->alignment_shift = 1; /* 2-byte alignment */
  242. mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
  243. /*
  244. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  245. * bus width mode.
  246. */
  247. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  248. /*
  249. * All SDHI blocks support SDIO IRQ signalling.
  250. */
  251. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  252. /*
  253. * All SDHI have CMD12 controll bit
  254. */
  255. mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
  256. /*
  257. * All SDHI need SDIO_INFO1 reserved bit
  258. */
  259. mmc_data->flags |= TMIO_MMC_SDIO_STATUS_QUIRK;
  260. ret = tmio_mmc_host_probe(host, mmc_data);
  261. if (ret < 0)
  262. goto efree;
  263. /*
  264. * Allow one or more specific (named) ISRs or
  265. * one or more multiplexed (un-named) ISRs.
  266. */
  267. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  268. if (irq >= 0) {
  269. multiplexed_isr = false;
  270. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
  271. dev_name(&pdev->dev), host);
  272. if (ret)
  273. goto eirq;
  274. }
  275. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  276. if (irq >= 0) {
  277. multiplexed_isr = false;
  278. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
  279. dev_name(&pdev->dev), host);
  280. if (ret)
  281. goto eirq;
  282. }
  283. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
  284. if (irq >= 0) {
  285. multiplexed_isr = false;
  286. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
  287. dev_name(&pdev->dev), host);
  288. if (ret)
  289. goto eirq;
  290. } else if (!multiplexed_isr) {
  291. dev_err(&pdev->dev,
  292. "Principal SD-card IRQ is missing among named interrupts\n");
  293. ret = irq;
  294. goto eirq;
  295. }
  296. if (multiplexed_isr) {
  297. while (1) {
  298. irq = platform_get_irq(pdev, i);
  299. if (irq < 0)
  300. break;
  301. i++;
  302. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  303. dev_name(&pdev->dev), host);
  304. if (ret)
  305. goto eirq;
  306. }
  307. /* There must be at least one IRQ source */
  308. if (!i) {
  309. ret = irq;
  310. goto eirq;
  311. }
  312. }
  313. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  314. mmc_hostname(host->mmc), (unsigned long)
  315. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  316. host->mmc->f_max / 1000000);
  317. return ret;
  318. eirq:
  319. tmio_mmc_host_remove(host);
  320. efree:
  321. tmio_mmc_host_free(host);
  322. eprobe:
  323. return ret;
  324. }
  325. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  326. {
  327. struct mmc_host *mmc = platform_get_drvdata(pdev);
  328. struct tmio_mmc_host *host = mmc_priv(mmc);
  329. tmio_mmc_host_remove(host);
  330. return 0;
  331. }
  332. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  333. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  334. pm_runtime_force_resume)
  335. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  336. tmio_mmc_host_runtime_resume,
  337. NULL)
  338. };
  339. static struct platform_driver sh_mobile_sdhi_driver = {
  340. .driver = {
  341. .name = "sh_mobile_sdhi",
  342. .pm = &tmio_mmc_dev_pm_ops,
  343. .of_match_table = sh_mobile_sdhi_of_match,
  344. },
  345. .probe = sh_mobile_sdhi_probe,
  346. .remove = sh_mobile_sdhi_remove,
  347. };
  348. module_platform_driver(sh_mobile_sdhi_driver);
  349. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  350. MODULE_AUTHOR("Magnus Damm");
  351. MODULE_LICENSE("GPL v2");
  352. MODULE_ALIAS("platform:sh_mobile_sdhi");