sdhci-iproc.c 7.4 KB

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