sdhci-pltfm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * sdhci-pltfm.c Support for SDHCI platform devices
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  6. * Copyright (c) 2009 MontaVista Software, Inc.
  7. *
  8. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  9. * Anton Vorontsov <avorontsov@ru.mvista.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. /* Supports:
  25. * SDHCI platform devices
  26. *
  27. * Inspired by sdhci-pci.c, by Pierre Ossman
  28. */
  29. #include <linux/err.h>
  30. #include <linux/of.h>
  31. #ifdef CONFIG_PPC
  32. #include <asm/machdep.h>
  33. #endif
  34. #include "sdhci.h"
  35. #include "sdhci-pltfm.h"
  36. static struct sdhci_ops sdhci_pltfm_ops = {
  37. };
  38. #ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
  39. /*
  40. * These accessors are designed for big endian hosts doing I/O to
  41. * little endian controllers incorporating a 32-bit hardware byte swapper.
  42. */
  43. u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
  44. {
  45. return in_be32(host->ioaddr + reg);
  46. }
  47. u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
  48. {
  49. return in_be16(host->ioaddr + (reg ^ 0x2));
  50. }
  51. u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
  52. {
  53. return in_8(host->ioaddr + (reg ^ 0x3));
  54. }
  55. void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
  56. {
  57. out_be32(host->ioaddr + reg, val);
  58. }
  59. void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg)
  60. {
  61. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  62. int base = reg & ~0x3;
  63. int shift = (reg & 0x2) * 8;
  64. switch (reg) {
  65. case SDHCI_TRANSFER_MODE:
  66. /*
  67. * Postpone this write, we must do it together with a
  68. * command write that is down below.
  69. */
  70. pltfm_host->xfer_mode_shadow = val;
  71. return;
  72. case SDHCI_COMMAND:
  73. sdhci_be32bs_writel(host,
  74. val << 16 | pltfm_host->xfer_mode_shadow,
  75. SDHCI_TRANSFER_MODE);
  76. return;
  77. }
  78. clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
  79. }
  80. void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
  81. {
  82. int base = reg & ~0x3;
  83. int shift = (reg & 0x3) * 8;
  84. clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
  85. }
  86. #endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */
  87. #ifdef CONFIG_OF
  88. static bool sdhci_of_wp_inverted(struct device_node *np)
  89. {
  90. if (of_get_property(np, "sdhci,wp-inverted", NULL))
  91. return true;
  92. /* Old device trees don't have the wp-inverted property. */
  93. #ifdef CONFIG_PPC
  94. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  95. #else
  96. return false;
  97. #endif /* CONFIG_PPC */
  98. }
  99. void sdhci_get_of_property(struct platform_device *pdev)
  100. {
  101. struct device_node *np = pdev->dev.of_node;
  102. struct sdhci_host *host = platform_get_drvdata(pdev);
  103. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  104. const __be32 *clk;
  105. int size;
  106. if (of_device_is_available(np)) {
  107. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  108. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  109. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  110. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  111. if (sdhci_of_wp_inverted(np))
  112. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  113. clk = of_get_property(np, "clock-frequency", &size);
  114. if (clk && size == sizeof(*clk) && *clk)
  115. pltfm_host->clock = be32_to_cpup(clk);
  116. }
  117. }
  118. #else
  119. void sdhci_get_of_property(struct platform_device *pdev) {}
  120. #endif /* CONFIG_OF */
  121. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  122. struct sdhci_pltfm_data *pdata)
  123. {
  124. struct sdhci_host *host;
  125. struct sdhci_pltfm_host *pltfm_host;
  126. struct resource *iomem;
  127. int ret;
  128. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. if (!iomem) {
  130. ret = -ENOMEM;
  131. goto err;
  132. }
  133. if (resource_size(iomem) < 0x100)
  134. dev_err(&pdev->dev, "Invalid iomem size!\n");
  135. /* Some PCI-based MFD need the parent here */
  136. if (pdev->dev.parent != &platform_bus)
  137. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  138. else
  139. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  140. if (IS_ERR(host)) {
  141. ret = PTR_ERR(host);
  142. goto err;
  143. }
  144. pltfm_host = sdhci_priv(host);
  145. host->hw_name = dev_name(&pdev->dev);
  146. if (pdata && pdata->ops)
  147. host->ops = pdata->ops;
  148. else
  149. host->ops = &sdhci_pltfm_ops;
  150. if (pdata)
  151. host->quirks = pdata->quirks;
  152. host->irq = platform_get_irq(pdev, 0);
  153. if (!request_mem_region(iomem->start, resource_size(iomem),
  154. mmc_hostname(host->mmc))) {
  155. dev_err(&pdev->dev, "cannot request region\n");
  156. ret = -EBUSY;
  157. goto err_request;
  158. }
  159. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  160. if (!host->ioaddr) {
  161. dev_err(&pdev->dev, "failed to remap registers\n");
  162. ret = -ENOMEM;
  163. goto err_remap;
  164. }
  165. platform_set_drvdata(pdev, host);
  166. return host;
  167. err_remap:
  168. release_mem_region(iomem->start, resource_size(iomem));
  169. err_request:
  170. sdhci_free_host(host);
  171. err:
  172. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  173. return ERR_PTR(ret);
  174. }
  175. void sdhci_pltfm_free(struct platform_device *pdev)
  176. {
  177. struct sdhci_host *host = platform_get_drvdata(pdev);
  178. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  179. iounmap(host->ioaddr);
  180. release_mem_region(iomem->start, resource_size(iomem));
  181. sdhci_free_host(host);
  182. platform_set_drvdata(pdev, NULL);
  183. }
  184. int sdhci_pltfm_register(struct platform_device *pdev,
  185. struct sdhci_pltfm_data *pdata)
  186. {
  187. struct sdhci_host *host;
  188. int ret = 0;
  189. host = sdhci_pltfm_init(pdev, pdata);
  190. if (IS_ERR(host))
  191. return PTR_ERR(host);
  192. sdhci_get_of_property(pdev);
  193. ret = sdhci_add_host(host);
  194. if (ret)
  195. sdhci_pltfm_free(pdev);
  196. return ret;
  197. }
  198. int sdhci_pltfm_unregister(struct platform_device *pdev)
  199. {
  200. struct sdhci_host *host = platform_get_drvdata(pdev);
  201. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  202. sdhci_remove_host(host, dead);
  203. sdhci_pltfm_free(pdev);
  204. return 0;
  205. }
  206. #ifdef CONFIG_PM
  207. int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
  208. {
  209. struct sdhci_host *host = platform_get_drvdata(dev);
  210. return sdhci_suspend_host(host, state);
  211. }
  212. int sdhci_pltfm_resume(struct platform_device *dev)
  213. {
  214. struct sdhci_host *host = platform_get_drvdata(dev);
  215. return sdhci_resume_host(host);
  216. }
  217. #endif /* CONFIG_PM */