sh_mobile_sdhi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "tmio_mmc.h"
  29. struct sh_mobile_sdhi {
  30. struct clk *clk;
  31. struct tmio_mmc_data mmc_data;
  32. struct sh_dmae_slave param_tx;
  33. struct sh_dmae_slave param_rx;
  34. struct tmio_mmc_dma dma_priv;
  35. };
  36. static void sh_mobile_sdhi_set_pwr(struct platform_device *pdev, int state)
  37. {
  38. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  39. if (p && p->set_pwr)
  40. p->set_pwr(pdev, state);
  41. }
  42. static int sh_mobile_sdhi_get_cd(struct platform_device *pdev)
  43. {
  44. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  45. if (p && p->get_cd)
  46. return p->get_cd(pdev);
  47. else
  48. return -ENOSYS;
  49. }
  50. static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
  51. {
  52. struct sh_mobile_sdhi *priv;
  53. struct tmio_mmc_data *mmc_data;
  54. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  55. struct tmio_mmc_host *host;
  56. char clk_name[8];
  57. int i, irq, ret;
  58. priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  59. if (priv == NULL) {
  60. dev_err(&pdev->dev, "kzalloc failed\n");
  61. return -ENOMEM;
  62. }
  63. mmc_data = &priv->mmc_data;
  64. p->pdata = mmc_data;
  65. snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
  66. priv->clk = clk_get(&pdev->dev, clk_name);
  67. if (IS_ERR(priv->clk)) {
  68. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  69. ret = PTR_ERR(priv->clk);
  70. goto eclkget;
  71. }
  72. clk_enable(priv->clk);
  73. mmc_data->hclk = clk_get_rate(priv->clk);
  74. mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
  75. mmc_data->get_cd = sh_mobile_sdhi_get_cd;
  76. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  77. if (p) {
  78. mmc_data->flags = p->tmio_flags;
  79. mmc_data->ocr_mask = p->tmio_ocr_mask;
  80. mmc_data->capabilities |= p->tmio_caps;
  81. if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0) {
  82. priv->param_tx.slave_id = p->dma_slave_tx;
  83. priv->param_rx.slave_id = p->dma_slave_rx;
  84. priv->dma_priv.chan_priv_tx = &priv->param_tx;
  85. priv->dma_priv.chan_priv_rx = &priv->param_rx;
  86. priv->dma_priv.alignment_shift = 1; /* 2-byte alignment */
  87. mmc_data->dma = &priv->dma_priv;
  88. }
  89. }
  90. /*
  91. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  92. * bus width mode.
  93. */
  94. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  95. /*
  96. * All SDHI blocks support SDIO IRQ signalling.
  97. */
  98. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  99. ret = tmio_mmc_host_probe(&host, pdev, mmc_data);
  100. if (ret < 0)
  101. goto eprobe;
  102. for (i = 0; i < 3; i++) {
  103. irq = platform_get_irq(pdev, i);
  104. if (irq < 0) {
  105. if (i) {
  106. continue;
  107. } else {
  108. ret = irq;
  109. goto eirq;
  110. }
  111. }
  112. ret = request_irq(irq, tmio_mmc_irq, 0,
  113. dev_name(&pdev->dev), host);
  114. if (ret) {
  115. while (i--) {
  116. irq = platform_get_irq(pdev, i);
  117. if (irq >= 0)
  118. free_irq(irq, host);
  119. }
  120. goto eirq;
  121. }
  122. }
  123. dev_info(&pdev->dev, "%s base at 0x%08lx clock rate %u MHz\n",
  124. mmc_hostname(host->mmc), (unsigned long)
  125. (platform_get_resource(pdev,IORESOURCE_MEM, 0)->start),
  126. mmc_data->hclk / 1000000);
  127. return ret;
  128. eirq:
  129. tmio_mmc_host_remove(host);
  130. eprobe:
  131. clk_disable(priv->clk);
  132. clk_put(priv->clk);
  133. eclkget:
  134. kfree(priv);
  135. return ret;
  136. }
  137. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  138. {
  139. struct mmc_host *mmc = platform_get_drvdata(pdev);
  140. struct tmio_mmc_host *host = mmc_priv(mmc);
  141. struct sh_mobile_sdhi *priv = container_of(host->pdata, struct sh_mobile_sdhi, mmc_data);
  142. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  143. int i, irq;
  144. p->pdata = NULL;
  145. tmio_mmc_host_remove(host);
  146. for (i = 0; i < 3; i++) {
  147. irq = platform_get_irq(pdev, i);
  148. if (irq >= 0)
  149. free_irq(irq, host);
  150. }
  151. clk_disable(priv->clk);
  152. clk_put(priv->clk);
  153. kfree(priv);
  154. return 0;
  155. }
  156. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  157. .suspend = tmio_mmc_host_suspend,
  158. .resume = tmio_mmc_host_resume,
  159. .runtime_suspend = tmio_mmc_host_runtime_suspend,
  160. .runtime_resume = tmio_mmc_host_runtime_resume,
  161. };
  162. static struct platform_driver sh_mobile_sdhi_driver = {
  163. .driver = {
  164. .name = "sh_mobile_sdhi",
  165. .owner = THIS_MODULE,
  166. .pm = &tmio_mmc_dev_pm_ops,
  167. },
  168. .probe = sh_mobile_sdhi_probe,
  169. .remove = __devexit_p(sh_mobile_sdhi_remove),
  170. };
  171. static int __init sh_mobile_sdhi_init(void)
  172. {
  173. return platform_driver_register(&sh_mobile_sdhi_driver);
  174. }
  175. static void __exit sh_mobile_sdhi_exit(void)
  176. {
  177. platform_driver_unregister(&sh_mobile_sdhi_driver);
  178. }
  179. module_init(sh_mobile_sdhi_init);
  180. module_exit(sh_mobile_sdhi_exit);
  181. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  182. MODULE_AUTHOR("Magnus Damm");
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_ALIAS("platform:sh_mobile_sdhi");