sh_mobile_sdhi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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,sh73a0-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  102. { .compatible = "renesas,r8a73a4-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  103. { .compatible = "renesas,r8a7740-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  104. { .compatible = "renesas,r8a7778-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  105. { .compatible = "renesas,r8a7779-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  106. { .compatible = "renesas,r8a7790-sdhi", .data = &sh_mobile_sdhi_of_cfg[0], },
  107. {},
  108. };
  109. MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
  110. static int sh_mobile_sdhi_probe(struct platform_device *pdev)
  111. {
  112. const struct of_device_id *of_id =
  113. of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
  114. struct sh_mobile_sdhi *priv;
  115. struct tmio_mmc_data *mmc_data;
  116. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  117. struct tmio_mmc_host *host;
  118. int irq, ret, i = 0;
  119. bool multiplexed_isr = true;
  120. struct tmio_mmc_dma *dma_priv;
  121. priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  122. if (priv == NULL) {
  123. dev_err(&pdev->dev, "kzalloc failed\n");
  124. return -ENOMEM;
  125. }
  126. mmc_data = &priv->mmc_data;
  127. dma_priv = &priv->dma_priv;
  128. if (p) {
  129. if (p->init) {
  130. ret = p->init(pdev, &sdhi_ops);
  131. if (ret)
  132. return ret;
  133. }
  134. }
  135. priv->clk = devm_clk_get(&pdev->dev, NULL);
  136. if (IS_ERR(priv->clk)) {
  137. ret = PTR_ERR(priv->clk);
  138. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  139. goto eclkget;
  140. }
  141. mmc_data->clk_enable = sh_mobile_sdhi_clk_enable;
  142. mmc_data->clk_disable = sh_mobile_sdhi_clk_disable;
  143. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  144. mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
  145. if (p) {
  146. mmc_data->flags = p->tmio_flags;
  147. mmc_data->ocr_mask = p->tmio_ocr_mask;
  148. mmc_data->capabilities |= p->tmio_caps;
  149. mmc_data->capabilities2 |= p->tmio_caps2;
  150. mmc_data->cd_gpio = p->cd_gpio;
  151. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
  152. /*
  153. * Yes, we have to provide slave IDs twice to TMIO:
  154. * once as a filter parameter and once for channel
  155. * configuration as an explicit slave ID
  156. */
  157. dma_priv->chan_priv_tx = (void *)p->dma_slave_tx;
  158. dma_priv->chan_priv_rx = (void *)p->dma_slave_rx;
  159. dma_priv->slave_id_tx = p->dma_slave_tx;
  160. dma_priv->slave_id_rx = p->dma_slave_rx;
  161. }
  162. }
  163. dma_priv->alignment_shift = 1; /* 2-byte alignment */
  164. dma_priv->filter = shdma_chan_filter;
  165. mmc_data->dma = dma_priv;
  166. /*
  167. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  168. * bus width mode.
  169. */
  170. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  171. /*
  172. * All SDHI blocks support SDIO IRQ signalling.
  173. */
  174. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  175. if (of_id && of_id->data) {
  176. const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
  177. mmc_data->flags |= of_data->tmio_flags;
  178. }
  179. ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
  180. if (ret < 0)
  181. goto eprobe;
  182. /*
  183. * Allow one or more specific (named) ISRs or
  184. * one or more multiplexed (un-named) ISRs.
  185. */
  186. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  187. if (irq >= 0) {
  188. multiplexed_isr = false;
  189. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_card_detect_irq, 0,
  190. dev_name(&pdev->dev), host);
  191. if (ret)
  192. goto eirq;
  193. }
  194. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  195. if (irq >= 0) {
  196. multiplexed_isr = false;
  197. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdio_irq, 0,
  198. dev_name(&pdev->dev), host);
  199. if (ret)
  200. goto eirq;
  201. }
  202. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
  203. if (irq >= 0) {
  204. multiplexed_isr = false;
  205. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_sdcard_irq, 0,
  206. dev_name(&pdev->dev), host);
  207. if (ret)
  208. goto eirq;
  209. } else if (!multiplexed_isr) {
  210. dev_err(&pdev->dev,
  211. "Principal SD-card IRQ is missing among named interrupts\n");
  212. ret = irq;
  213. goto eirq;
  214. }
  215. if (multiplexed_isr) {
  216. while (1) {
  217. irq = platform_get_irq(pdev, i);
  218. if (irq < 0)
  219. break;
  220. i++;
  221. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
  222. dev_name(&pdev->dev), host);
  223. if (ret)
  224. goto eirq;
  225. }
  226. /* There must be at least one IRQ source */
  227. if (!i) {
  228. ret = irq;
  229. goto eirq;
  230. }
  231. }
  232. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  233. mmc_hostname(host->mmc), (unsigned long)
  234. (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
  235. host->mmc->f_max / 1000000);
  236. return ret;
  237. eirq:
  238. tmio_mmc_host_remove(host);
  239. eprobe:
  240. eclkget:
  241. if (p && p->cleanup)
  242. p->cleanup(pdev);
  243. return ret;
  244. }
  245. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  246. {
  247. struct mmc_host *mmc = platform_get_drvdata(pdev);
  248. struct tmio_mmc_host *host = mmc_priv(mmc);
  249. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  250. tmio_mmc_host_remove(host);
  251. if (p && p->cleanup)
  252. p->cleanup(pdev);
  253. return 0;
  254. }
  255. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  256. .suspend = tmio_mmc_host_suspend,
  257. .resume = tmio_mmc_host_resume,
  258. .runtime_suspend = tmio_mmc_host_runtime_suspend,
  259. .runtime_resume = tmio_mmc_host_runtime_resume,
  260. };
  261. static struct platform_driver sh_mobile_sdhi_driver = {
  262. .driver = {
  263. .name = "sh_mobile_sdhi",
  264. .owner = THIS_MODULE,
  265. .pm = &tmio_mmc_dev_pm_ops,
  266. .of_match_table = sh_mobile_sdhi_of_match,
  267. },
  268. .probe = sh_mobile_sdhi_probe,
  269. .remove = sh_mobile_sdhi_remove,
  270. };
  271. module_platform_driver(sh_mobile_sdhi_driver);
  272. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  273. MODULE_AUTHOR("Magnus Damm");
  274. MODULE_LICENSE("GPL v2");
  275. MODULE_ALIAS("platform:sh_mobile_sdhi");