sh_mobile_sdhi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * SuperH Mobile SDHI
  3. *
  4. * Copyright (C) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on "Compaq ASIC3 support":
  11. *
  12. * Copyright 2001 Compaq Computer Corporation.
  13. * Copyright 2004-2005 Phil Blundell
  14. * Copyright 2007-2008 OpenedHand Ltd.
  15. *
  16. * Authors: Phil Blundell <pb@handhelds.org>,
  17. * Samuel Ortiz <sameo@openedhand.com>
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/module.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/sh_mobile_sdhi.h>
  29. #include <linux/mfd/tmio.h>
  30. #include <linux/sh_dma.h>
  31. #include <linux/delay.h>
  32. #include "tmio_mmc.h"
  33. struct sh_mobile_sdhi_of_data {
  34. unsigned long tmio_flags;
  35. };
  36. static const struct sh_mobile_sdhi_of_data sh_mobile_sdhi_of_cfg[] = {
  37. {
  38. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
  39. },
  40. };
  41. struct sh_mobile_sdhi {
  42. struct clk *clk;
  43. struct tmio_mmc_data mmc_data;
  44. struct tmio_mmc_dma dma_priv;
  45. };
  46. static int sh_mobile_sdhi_clk_enable(struct platform_device *pdev, unsigned int *f)
  47. {
  48. struct mmc_host *mmc = platform_get_drvdata(pdev);
  49. struct tmio_mmc_host *host = mmc_priv(mmc);
  50. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  51. int ret = clk_enable(priv->clk);
  52. if (ret < 0)
  53. return ret;
  54. *f = clk_get_rate(priv->clk);
  55. return 0;
  56. }
  57. static void sh_mobile_sdhi_clk_disable(struct platform_device *pdev)
  58. {
  59. struct mmc_host *mmc = platform_get_drvdata(pdev);
  60. struct tmio_mmc_host *host = mmc_priv(mmc);
  61. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  62. clk_disable(priv->clk);
  63. }
  64. static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
  65. {
  66. int timeout = 1000;
  67. while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
  68. udelay(1);
  69. if (!timeout) {
  70. dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n");
  71. return -EBUSY;
  72. }
  73. return 0;
  74. }
  75. static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  76. {
  77. switch (addr)
  78. {
  79. case CTL_SD_CMD:
  80. case CTL_STOP_INTERNAL_ACTION:
  81. case CTL_XFER_BLK_COUNT:
  82. case CTL_SD_CARD_CLK_CTL:
  83. case CTL_SD_XFER_LEN:
  84. case CTL_SD_MEM_CARD_OPT:
  85. case CTL_TRANSACTION_CTL:
  86. case CTL_DMA_ENABLE:
  87. return sh_mobile_sdhi_wait_idle(host);
  88. }
  89. return 0;
  90. }
  91. static void sh_mobile_sdhi_cd_wakeup(const struct platform_device *pdev)
  92. {
  93. mmc_detect_change(platform_get_drvdata(pdev), msecs_to_jiffies(100));
  94. }
  95. static const struct sh_mobile_sdhi_ops sdhi_ops = {
  96. .cd_wakeup = sh_mobile_sdhi_cd_wakeup,
  97. };
  98. static const struct of_device_id sh_mobile_sdhi_of_match[] = {
  99. { .compatible = "renesas,shmobile-sdhi" },
  100. { .compatible = "renesas,sh7372-sdhi" },
  101. { .compatible = "renesas,r8a7740-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  102. {},
  103. };
  104. MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
  105. static int sh_mobile_sdhi_probe(struct platform_device *pdev)
  106. {
  107. const struct of_device_id *of_id =
  108. of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
  109. struct sh_mobile_sdhi *priv;
  110. struct tmio_mmc_data *mmc_data;
  111. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  112. struct tmio_mmc_host *host;
  113. int irq, ret, i = 0;
  114. bool multiplexed_isr = true;
  115. struct tmio_mmc_dma *dma_priv;
  116. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  117. if (priv == NULL) {
  118. dev_err(&pdev->dev, "kzalloc failed\n");
  119. return -ENOMEM;
  120. }
  121. mmc_data = &priv->mmc_data;
  122. dma_priv = &priv->dma_priv;
  123. if (p) {
  124. if (p->init) {
  125. ret = p->init(pdev, &sdhi_ops);
  126. if (ret)
  127. return ret;
  128. }
  129. }
  130. priv->clk = devm_clk_get(&pdev->dev, NULL);
  131. if (IS_ERR(priv->clk)) {
  132. ret = PTR_ERR(priv->clk);
  133. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  134. goto eclkget;
  135. }
  136. mmc_data->clk_enable = sh_mobile_sdhi_clk_enable;
  137. mmc_data->clk_disable = sh_mobile_sdhi_clk_disable;
  138. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  139. mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
  140. if (p) {
  141. mmc_data->flags = p->tmio_flags;
  142. mmc_data->ocr_mask = p->tmio_ocr_mask;
  143. mmc_data->capabilities |= p->tmio_caps;
  144. mmc_data->capabilities2 |= p->tmio_caps2;
  145. mmc_data->cd_gpio = p->cd_gpio;
  146. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
  147. /*
  148. * Yes, we have to provide slave IDs twice to TMIO:
  149. * once as a filter parameter and once for channel
  150. * configuration as an explicit slave ID
  151. */
  152. dma_priv->chan_priv_tx = (void *)p->dma_slave_tx;
  153. dma_priv->chan_priv_rx = (void *)p->dma_slave_rx;
  154. dma_priv->slave_id_tx = p->dma_slave_tx;
  155. dma_priv->slave_id_rx = p->dma_slave_rx;
  156. }
  157. }
  158. dma_priv->alignment_shift = 1; /* 2-byte alignment */
  159. dma_priv->filter = shdma_chan_filter;
  160. mmc_data->dma = dma_priv;
  161. /*
  162. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  163. * bus width mode.
  164. */
  165. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  166. /*
  167. * All SDHI blocks support SDIO IRQ signalling.
  168. */
  169. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  170. if (of_id && of_id->data) {
  171. const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
  172. mmc_data->flags |= of_data->tmio_flags;
  173. }
  174. ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
  175. if (ret < 0)
  176. goto eprobe;
  177. /*
  178. * Allow one or more specific (named) ISRs or
  179. * one or more multiplexed (un-named) ISRs.
  180. */
  181. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  182. if (irq >= 0) {
  183. multiplexed_isr = false;
  184. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
  185. dev_name(&pdev->dev), host);
  186. if (ret)
  187. goto eirq;
  188. }
  189. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  190. if (irq >= 0) {
  191. multiplexed_isr = false;
  192. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
  193. dev_name(&pdev->dev), host);
  194. if (ret)
  195. goto eirq;
  196. }
  197. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
  198. if (irq >= 0) {
  199. multiplexed_isr = false;
  200. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
  201. dev_name(&pdev->dev), host);
  202. if (ret)
  203. goto eirq;
  204. } else if (!multiplexed_isr) {
  205. dev_err(&pdev->dev,
  206. "Principal SD-card IRQ is missing among named interrupts\n");
  207. ret = irq;
  208. goto eirq;
  209. }
  210. if (multiplexed_isr) {
  211. while (1) {
  212. irq = platform_get_irq(pdev, i);
  213. if (irq < 0)
  214. break;
  215. i++;
  216. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  217. dev_name(&pdev->dev), host);
  218. if (ret)
  219. goto eirq;
  220. }
  221. /* There must be at least one IRQ source */
  222. if (!i) {
  223. ret = irq;
  224. goto eirq;
  225. }
  226. }
  227. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  228. mmc_hostname(host->mmc), (unsigned long)
  229. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  230. host->mmc->f_max / 1000000);
  231. return ret;
  232. eirq:
  233. tmio_mmc_host_remove(host);
  234. eprobe:
  235. eclkget:
  236. if (p && p->cleanup)
  237. p->cleanup(pdev);
  238. return ret;
  239. }
  240. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  241. {
  242. struct mmc_host *mmc = platform_get_drvdata(pdev);
  243. struct tmio_mmc_host *host = mmc_priv(mmc);
  244. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  245. tmio_mmc_host_remove(host);
  246. if (p && p->cleanup)
  247. p->cleanup(pdev);
  248. return 0;
  249. }
  250. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  251. .suspend = tmio_mmc_host_suspend,
  252. .resume = tmio_mmc_host_resume,
  253. .runtime_suspend = tmio_mmc_host_runtime_suspend,
  254. .runtime_resume = tmio_mmc_host_runtime_resume,
  255. };
  256. static struct platform_driver sh_mobile_sdhi_driver = {
  257. .driver = {
  258. .name = "sh_mobile_sdhi",
  259. .owner = THIS_MODULE,
  260. .pm = &tmio_mmc_dev_pm_ops,
  261. .of_match_table = sh_mobile_sdhi_of_match,
  262. },
  263. .probe = sh_mobile_sdhi_probe,
  264. .remove = sh_mobile_sdhi_remove,
  265. };
  266. module_platform_driver(sh_mobile_sdhi_driver);
  267. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  268. MODULE_AUTHOR("Magnus Damm");
  269. MODULE_LICENSE("GPL v2");
  270. MODULE_ALIAS("platform:sh_mobile_sdhi");