sh_mobile_sdhi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/platform_device.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/sh_mobile_sdhi.h>
  26. #include <linux/mfd/tmio.h>
  27. #include <linux/sh_dma.h>
  28. #include <linux/delay.h>
  29. #include "tmio_mmc.h"
  30. struct sh_mobile_sdhi {
  31. struct clk *clk;
  32. struct tmio_mmc_data mmc_data;
  33. struct sh_dmae_slave param_tx;
  34. struct sh_dmae_slave param_rx;
  35. struct tmio_mmc_dma dma_priv;
  36. };
  37. static void sh_mobile_sdhi_set_pwr(struct platform_device *pdev, int state)
  38. {
  39. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  40. if (p && p->set_pwr)
  41. p->set_pwr(pdev, state);
  42. }
  43. static int sh_mobile_sdhi_get_cd(struct platform_device *pdev)
  44. {
  45. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  46. if (p && p->get_cd)
  47. return p->get_cd(pdev);
  48. else
  49. return -ENOSYS;
  50. }
  51. static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
  52. {
  53. int timeout = 1000;
  54. while (--timeout && !(sd_ctrl_read16(host, CTL_STATUS2) & (1 << 13)))
  55. udelay(1);
  56. if (!timeout) {
  57. dev_warn(host->pdata->dev, "timeout waiting for SD bus idle\n");
  58. return -EBUSY;
  59. }
  60. return 0;
  61. }
  62. static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
  63. {
  64. switch (addr)
  65. {
  66. case CTL_SD_CMD:
  67. case CTL_STOP_INTERNAL_ACTION:
  68. case CTL_XFER_BLK_COUNT:
  69. case CTL_SD_CARD_CLK_CTL:
  70. case CTL_SD_XFER_LEN:
  71. case CTL_SD_MEM_CARD_OPT:
  72. case CTL_TRANSACTION_CTL:
  73. case CTL_DMA_ENABLE:
  74. return sh_mobile_sdhi_wait_idle(host);
  75. }
  76. return 0;
  77. }
  78. static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
  79. {
  80. struct sh_mobile_sdhi *priv;
  81. struct tmio_mmc_data *mmc_data;
  82. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  83. struct tmio_mmc_host *host;
  84. char clk_name[8];
  85. int irq, ret, i = 0;
  86. bool multiplexed_isr = true;
  87. priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  88. if (priv == NULL) {
  89. dev_err(&pdev->dev, "kzalloc failed\n");
  90. return -ENOMEM;
  91. }
  92. mmc_data = &priv->mmc_data;
  93. p->pdata = mmc_data;
  94. snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
  95. priv->clk = clk_get(&pdev->dev, clk_name);
  96. if (IS_ERR(priv->clk)) {
  97. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  98. ret = PTR_ERR(priv->clk);
  99. goto eclkget;
  100. }
  101. clk_enable(priv->clk);
  102. mmc_data->hclk = clk_get_rate(priv->clk);
  103. mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
  104. mmc_data->get_cd = sh_mobile_sdhi_get_cd;
  105. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  106. if (p) {
  107. mmc_data->flags = p->tmio_flags;
  108. if (mmc_data->flags & TMIO_MMC_HAS_IDLE_WAIT)
  109. mmc_data->write16_hook = sh_mobile_sdhi_write16_hook;
  110. mmc_data->ocr_mask = p->tmio_ocr_mask;
  111. mmc_data->capabilities |= p->tmio_caps;
  112. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
  113. priv->param_tx.slave_id = p->dma_slave_tx;
  114. priv->param_rx.slave_id = p->dma_slave_rx;
  115. priv->dma_priv.chan_priv_tx = &priv->param_tx;
  116. priv->dma_priv.chan_priv_rx = &priv->param_rx;
  117. priv->dma_priv.alignment_shift = 1; /* 2-byte alignment */
  118. mmc_data->dma = &priv->dma_priv;
  119. }
  120. }
  121. /*
  122. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  123. * bus width mode.
  124. */
  125. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  126. /*
  127. * All SDHI blocks support SDIO IRQ signalling.
  128. */
  129. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  130. ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
  131. if (ret < 0)
  132. goto eprobe;
  133. /*
  134. * Allow one or more specific (named) ISRs or
  135. * one or more multiplexed (un-named) ISRs.
  136. */
  137. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  138. if (irq >= 0) {
  139. multiplexed_isr = false;
  140. ret = request_irq(irq, tmio_mmc_card_detect_irq, 0,
  141. dev_name(&pdev->dev), host);
  142. if (ret)
  143. goto eirq_card_detect;
  144. }
  145. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  146. if (irq >= 0) {
  147. multiplexed_isr = false;
  148. ret = request_irq(irq, tmio_mmc_sdio_irq, 0,
  149. dev_name(&pdev->dev), host);
  150. if (ret)
  151. goto eirq_sdio;
  152. }
  153. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDCARD);
  154. if (irq >= 0) {
  155. multiplexed_isr = false;
  156. ret = request_irq(irq, tmio_mmc_sdcard_irq, 0,
  157. dev_name(&pdev->dev), host);
  158. if (ret)
  159. goto eirq_sdcard;
  160. } else if (!multiplexed_isr) {
  161. dev_err(&pdev->dev,
  162. "Principal SD-card IRQ is missing among named interrupts\n");
  163. ret = irq;
  164. goto eirq_sdcard;
  165. }
  166. if (multiplexed_isr) {
  167. while (1) {
  168. irq = platform_get_irq(pdev, i);
  169. if (irq < 0)
  170. break;
  171. i++;
  172. ret = request_irq(irq, tmio_mmc_irq, 0,
  173. dev_name(&pdev->dev), host);
  174. if (ret)
  175. goto eirq_multiplexed;
  176. }
  177. /* There must be at least one IRQ source */
  178. if (!i)
  179. goto eirq_multiplexed;
  180. }
  181. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  182. mmc_hostname(host->mmc), (unsigned long)
  183. (platform_get_resource(pdev,IORESOURCE_MEM, 0)->start),
  184. mmc_data->hclk / 1000000);
  185. return ret;
  186. eirq_multiplexed:
  187. while (i--) {
  188. irq = platform_get_irq(pdev, i);
  189. free_irq(irq, host);
  190. }
  191. eirq_sdcard:
  192. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_SDIO);
  193. if (irq >= 0)
  194. free_irq(irq, host);
  195. eirq_sdio:
  196. irq = platform_get_irq_byname(pdev, SH_MOBILE_SDHI_IRQ_CARD_DETECT);
  197. if (irq >= 0)
  198. free_irq(irq, host);
  199. eirq_card_detect:
  200. tmio_mmc_host_remove(host);
  201. eprobe:
  202. clk_disable(priv->clk);
  203. clk_put(priv->clk);
  204. eclkget:
  205. kfree(priv);
  206. return ret;
  207. }
  208. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  209. {
  210. struct mmc_host *mmc = platform_get_drvdata(pdev);
  211. struct tmio_mmc_host *host = mmc_priv(mmc);
  212. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  213. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  214. int i = 0, irq;
  215. p->pdata = NULL;
  216. tmio_mmc_host_remove(host);
  217. while (1) {
  218. irq = platform_get_irq(pdev, i++);
  219. if (irq < 0)
  220. break;
  221. free_irq(irq, host);
  222. }
  223. clk_disable(priv->clk);
  224. clk_put(priv->clk);
  225. kfree(priv);
  226. return 0;
  227. }
  228. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  229. .suspend = tmio_mmc_host_suspend,
  230. .resume = tmio_mmc_host_resume,
  231. .runtime_suspend = tmio_mmc_host_runtime_suspend,
  232. .runtime_resume = tmio_mmc_host_runtime_resume,
  233. };
  234. static struct platform_driver sh_mobile_sdhi_driver = {
  235. .driver = {
  236. .name = "sh_mobile_sdhi",
  237. .owner = THIS_MODULE,
  238. .pm = &tmio_mmc_dev_pm_ops,
  239. },
  240. .probe = sh_mobile_sdhi_probe,
  241. .remove = __devexit_p(sh_mobile_sdhi_remove),
  242. };
  243. static int __init sh_mobile_sdhi_init(void)
  244. {
  245. return platform_driver_register(&sh_mobile_sdhi_driver);
  246. }
  247. static void __exit sh_mobile_sdhi_exit(void)
  248. {
  249. platform_driver_unregister(&sh_mobile_sdhi_driver);
  250. }
  251. module_init(sh_mobile_sdhi_init);
  252. module_exit(sh_mobile_sdhi_exit);
  253. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  254. MODULE_AUTHOR("Magnus Damm");
  255. MODULE_LICENSE("GPL v2");
  256. MODULE_ALIAS("platform:sh_mobile_sdhi");