sdhci-iproc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. /*
  14. * iProc SDHCI platform driver
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include "sdhci-pltfm.h"
  22. struct sdhci_iproc_data {
  23. const struct sdhci_pltfm_data *pdata;
  24. u32 caps;
  25. u32 caps1;
  26. };
  27. struct sdhci_iproc_host {
  28. const struct sdhci_iproc_data *data;
  29. u32 shadow_cmd;
  30. u32 shadow_blk;
  31. };
  32. #define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
  33. static inline u32 sdhci_iproc_readl(struct sdhci_host *host, int reg)
  34. {
  35. u32 val = readl(host->ioaddr + reg);
  36. pr_debug("%s: readl [0x%02x] 0x%08x\n",
  37. mmc_hostname(host->mmc), reg, val);
  38. return val;
  39. }
  40. static u16 sdhci_iproc_readw(struct sdhci_host *host, int reg)
  41. {
  42. u32 val = sdhci_iproc_readl(host, (reg & ~3));
  43. u16 word = val >> REG_OFFSET_IN_BITS(reg) & 0xffff;
  44. return word;
  45. }
  46. static u8 sdhci_iproc_readb(struct sdhci_host *host, int reg)
  47. {
  48. u32 val = sdhci_iproc_readl(host, (reg & ~3));
  49. u8 byte = val >> REG_OFFSET_IN_BITS(reg) & 0xff;
  50. return byte;
  51. }
  52. static inline void sdhci_iproc_writel(struct sdhci_host *host, u32 val, int reg)
  53. {
  54. pr_debug("%s: writel [0x%02x] 0x%08x\n",
  55. mmc_hostname(host->mmc), reg, val);
  56. writel(val, host->ioaddr + reg);
  57. if (host->clock <= 400000) {
  58. /* Round up to micro-second four SD clock delay */
  59. if (host->clock)
  60. udelay((4 * 1000000 + host->clock - 1) / host->clock);
  61. else
  62. udelay(10);
  63. }
  64. }
  65. /*
  66. * The Arasan has a bugette whereby it may lose the content of successive
  67. * writes to the same register that are within two SD-card clock cycles of
  68. * each other (a clock domain crossing problem). The data
  69. * register does not have this problem, which is just as well - otherwise we'd
  70. * have to nobble the DMA engine too.
  71. *
  72. * This wouldn't be a problem with the code except that we can only write the
  73. * controller with 32-bit writes. So two different 16-bit registers are
  74. * written back to back creates the problem.
  75. *
  76. * In reality, this only happens when SDHCI_BLOCK_SIZE and SDHCI_BLOCK_COUNT
  77. * are written followed by SDHCI_TRANSFER_MODE and SDHCI_COMMAND.
  78. * The BLOCK_SIZE and BLOCK_COUNT are meaningless until a command issued so
  79. * the work around can be further optimized. We can keep shadow values of
  80. * BLOCK_SIZE, BLOCK_COUNT, and TRANSFER_MODE until a COMMAND is issued.
  81. * Then, write the BLOCK_SIZE+BLOCK_COUNT in a single 32-bit write followed
  82. * by the TRANSFER+COMMAND in another 32-bit write.
  83. */
  84. static void sdhci_iproc_writew(struct sdhci_host *host, u16 val, int reg)
  85. {
  86. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  87. struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
  88. u32 word_shift = REG_OFFSET_IN_BITS(reg);
  89. u32 mask = 0xffff << word_shift;
  90. u32 oldval, newval;
  91. if (reg == SDHCI_COMMAND) {
  92. /* Write the block now as we are issuing a command */
  93. if (iproc_host->shadow_blk != 0) {
  94. sdhci_iproc_writel(host, iproc_host->shadow_blk,
  95. SDHCI_BLOCK_SIZE);
  96. iproc_host->shadow_blk = 0;
  97. }
  98. oldval = iproc_host->shadow_cmd;
  99. } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
  100. /* Block size and count are stored in shadow reg */
  101. oldval = iproc_host->shadow_blk;
  102. } else {
  103. /* Read reg, all other registers are not shadowed */
  104. oldval = sdhci_iproc_readl(host, (reg & ~3));
  105. }
  106. newval = (oldval & ~mask) | (val << word_shift);
  107. if (reg == SDHCI_TRANSFER_MODE) {
  108. /* Save the transfer mode until the command is issued */
  109. iproc_host->shadow_cmd = newval;
  110. } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
  111. /* Save the block info until the command is issued */
  112. iproc_host->shadow_blk = newval;
  113. } else {
  114. /* Command or other regular 32-bit write */
  115. sdhci_iproc_writel(host, newval, reg & ~3);
  116. }
  117. }
  118. static void sdhci_iproc_writeb(struct sdhci_host *host, u8 val, int reg)
  119. {
  120. u32 oldval = sdhci_iproc_readl(host, (reg & ~3));
  121. u32 byte_shift = REG_OFFSET_IN_BITS(reg);
  122. u32 mask = 0xff << byte_shift;
  123. u32 newval = (oldval & ~mask) | (val << byte_shift);
  124. sdhci_iproc_writel(host, newval, reg & ~3);
  125. }
  126. static const struct sdhci_ops sdhci_iproc_ops = {
  127. .read_l = sdhci_iproc_readl,
  128. .read_w = sdhci_iproc_readw,
  129. .read_b = sdhci_iproc_readb,
  130. .write_l = sdhci_iproc_writel,
  131. .write_w = sdhci_iproc_writew,
  132. .write_b = sdhci_iproc_writeb,
  133. .set_clock = sdhci_set_clock,
  134. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  135. .set_bus_width = sdhci_set_bus_width,
  136. .reset = sdhci_reset,
  137. .set_uhs_signaling = sdhci_set_uhs_signaling,
  138. };
  139. static const struct sdhci_pltfm_data sdhci_iproc_pltfm_data = {
  140. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
  141. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN,
  142. .ops = &sdhci_iproc_ops,
  143. };
  144. static const struct sdhci_iproc_data iproc_data = {
  145. .pdata = &sdhci_iproc_pltfm_data,
  146. .caps = 0x05E90000,
  147. .caps1 = 0x00000064,
  148. };
  149. static const struct of_device_id sdhci_iproc_of_match[] = {
  150. { .compatible = "brcm,sdhci-iproc-cygnus", .data = &iproc_data },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(of, sdhci_iproc_of_match);
  154. static int sdhci_iproc_probe(struct platform_device *pdev)
  155. {
  156. const struct of_device_id *match;
  157. const struct sdhci_iproc_data *iproc_data;
  158. struct sdhci_host *host;
  159. struct sdhci_iproc_host *iproc_host;
  160. struct sdhci_pltfm_host *pltfm_host;
  161. int ret;
  162. match = of_match_device(sdhci_iproc_of_match, &pdev->dev);
  163. if (!match)
  164. return -EINVAL;
  165. iproc_data = match->data;
  166. host = sdhci_pltfm_init(pdev, iproc_data->pdata, sizeof(*iproc_host));
  167. if (IS_ERR(host))
  168. return PTR_ERR(host);
  169. pltfm_host = sdhci_priv(host);
  170. iproc_host = sdhci_pltfm_priv(pltfm_host);
  171. iproc_host->data = iproc_data;
  172. mmc_of_parse(host->mmc);
  173. sdhci_get_of_property(pdev);
  174. /* Enable EMMC 1/8V DDR capable */
  175. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  176. pltfm_host->clk = devm_clk_get(&pdev->dev, NULL);
  177. if (IS_ERR(pltfm_host->clk)) {
  178. ret = PTR_ERR(pltfm_host->clk);
  179. goto err;
  180. }
  181. if (iproc_host->data->pdata->quirks & SDHCI_QUIRK_MISSING_CAPS) {
  182. host->caps = iproc_host->data->caps;
  183. host->caps1 = iproc_host->data->caps1;
  184. }
  185. return sdhci_add_host(host);
  186. err:
  187. sdhci_pltfm_free(pdev);
  188. return ret;
  189. }
  190. static int sdhci_iproc_remove(struct platform_device *pdev)
  191. {
  192. return sdhci_pltfm_unregister(pdev);
  193. }
  194. static struct platform_driver sdhci_iproc_driver = {
  195. .driver = {
  196. .name = "sdhci-iproc",
  197. .of_match_table = sdhci_iproc_of_match,
  198. .pm = SDHCI_PLTFM_PMOPS,
  199. },
  200. .probe = sdhci_iproc_probe,
  201. .remove = sdhci_iproc_remove,
  202. };
  203. module_platform_driver(sdhci_iproc_driver);
  204. MODULE_AUTHOR("Broadcom");
  205. MODULE_DESCRIPTION("IPROC SDHCI driver");
  206. MODULE_LICENSE("GPL v2");