sh_mobile_sdhi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/mfd/core.h>
  26. #include <linux/mfd/tmio.h>
  27. #include <linux/mfd/sh_mobile_sdhi.h>
  28. #include <linux/sh_dma.h>
  29. struct sh_mobile_sdhi {
  30. struct clk *clk;
  31. struct tmio_mmc_data mmc_data;
  32. struct mfd_cell cell_mmc;
  33. struct sh_dmae_slave param_tx;
  34. struct sh_dmae_slave param_rx;
  35. struct tmio_mmc_dma dma_priv;
  36. };
  37. static struct resource sh_mobile_sdhi_resources[] = {
  38. {
  39. .start = 0x000,
  40. .end = 0x1ff,
  41. .flags = IORESOURCE_MEM,
  42. },
  43. {
  44. .start = 0,
  45. .end = 0,
  46. .flags = IORESOURCE_IRQ,
  47. },
  48. };
  49. static struct mfd_cell sh_mobile_sdhi_cell = {
  50. .name = "tmio-mmc",
  51. .num_resources = ARRAY_SIZE(sh_mobile_sdhi_resources),
  52. .resources = sh_mobile_sdhi_resources,
  53. };
  54. static void sh_mobile_sdhi_set_pwr(struct platform_device *tmio, int state)
  55. {
  56. struct platform_device *pdev = to_platform_device(tmio->dev.parent);
  57. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  58. if (p && p->set_pwr)
  59. p->set_pwr(pdev, state);
  60. }
  61. static int sh_mobile_sdhi_get_cd(struct platform_device *tmio)
  62. {
  63. struct platform_device *pdev = to_platform_device(tmio->dev.parent);
  64. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  65. if (p && p->get_cd)
  66. return p->get_cd(pdev);
  67. else
  68. return -ENOSYS;
  69. }
  70. static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev)
  71. {
  72. struct sh_mobile_sdhi *priv;
  73. struct tmio_mmc_data *mmc_data;
  74. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  75. struct resource *mem;
  76. char clk_name[8];
  77. int ret, irq;
  78. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  79. if (!mem)
  80. dev_err(&pdev->dev, "missing MEM resource\n");
  81. irq = platform_get_irq(pdev, 0);
  82. if (irq < 0)
  83. dev_err(&pdev->dev, "missing IRQ resource\n");
  84. if (!mem || (irq < 0))
  85. return -EINVAL;
  86. priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  87. if (priv == NULL) {
  88. dev_err(&pdev->dev, "kzalloc failed\n");
  89. return -ENOMEM;
  90. }
  91. mmc_data = &priv->mmc_data;
  92. snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
  93. priv->clk = clk_get(&pdev->dev, clk_name);
  94. if (IS_ERR(priv->clk)) {
  95. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  96. ret = PTR_ERR(priv->clk);
  97. kfree(priv);
  98. return ret;
  99. }
  100. clk_enable(priv->clk);
  101. mmc_data->hclk = clk_get_rate(priv->clk);
  102. mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
  103. mmc_data->get_cd = sh_mobile_sdhi_get_cd;
  104. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  105. if (p) {
  106. mmc_data->flags = p->tmio_flags;
  107. mmc_data->ocr_mask = p->tmio_ocr_mask;
  108. mmc_data->capabilities |= p->tmio_caps;
  109. }
  110. /*
  111. * All SDHI blocks support 2-byte and larger block sizes in 4-bit
  112. * bus width mode.
  113. */
  114. mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
  115. /*
  116. * All SDHI blocks support SDIO IRQ signalling.
  117. */
  118. mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
  119. if (p && p->dma_slave_tx >= 0 && p->dma_slave_rx >= 0) {
  120. priv->param_tx.slave_id = p->dma_slave_tx;
  121. priv->param_rx.slave_id = p->dma_slave_rx;
  122. priv->dma_priv.chan_priv_tx = &priv->param_tx;
  123. priv->dma_priv.chan_priv_rx = &priv->param_rx;
  124. priv->dma_priv.alignment_shift = 1; /* 2-byte alignment */
  125. mmc_data->dma = &priv->dma_priv;
  126. }
  127. memcpy(&priv->cell_mmc, &sh_mobile_sdhi_cell, sizeof(priv->cell_mmc));
  128. priv->cell_mmc.driver_data = mmc_data;
  129. priv->cell_mmc.platform_data = &priv->cell_mmc;
  130. priv->cell_mmc.data_size = sizeof(priv->cell_mmc);
  131. platform_set_drvdata(pdev, priv);
  132. ret = mfd_add_devices(&pdev->dev, pdev->id,
  133. &priv->cell_mmc, 1, mem, irq);
  134. if (ret) {
  135. clk_disable(priv->clk);
  136. clk_put(priv->clk);
  137. kfree(priv);
  138. }
  139. return ret;
  140. }
  141. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  142. {
  143. struct sh_mobile_sdhi *priv = platform_get_drvdata(pdev);
  144. mfd_remove_devices(&pdev->dev);
  145. clk_disable(priv->clk);
  146. clk_put(priv->clk);
  147. kfree(priv);
  148. return 0;
  149. }
  150. static struct platform_driver sh_mobile_sdhi_driver = {
  151. .driver = {
  152. .name = "sh_mobile_sdhi",
  153. .owner = THIS_MODULE,
  154. },
  155. .probe = sh_mobile_sdhi_probe,
  156. .remove = __devexit_p(sh_mobile_sdhi_remove),
  157. };
  158. static int __init sh_mobile_sdhi_init(void)
  159. {
  160. return platform_driver_register(&sh_mobile_sdhi_driver);
  161. }
  162. static void __exit sh_mobile_sdhi_exit(void)
  163. {
  164. platform_driver_unregister(&sh_mobile_sdhi_driver);
  165. }
  166. module_init(sh_mobile_sdhi_init);
  167. module_exit(sh_mobile_sdhi_exit);
  168. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  169. MODULE_AUTHOR("Magnus Damm");
  170. MODULE_LICENSE("GPL v2");