sh_mobile_sdhi.c 6.9 KB

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