sdhci-iproc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
  143. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN,
  144. .ops = &sdhci_iproc_ops,
  145. };
  146. static const struct sdhci_iproc_data iproc_data = {
  147. .pdata = &sdhci_iproc_pltfm_data,
  148. .caps = ((0x1 << SDHCI_MAX_BLOCK_SHIFT)
  149. & SDHCI_MAX_BLOCK_MASK) |
  150. SDHCI_CAN_VDD_330 |
  151. SDHCI_CAN_VDD_180 |
  152. SDHCI_CAN_DO_SUSPEND |
  153. SDHCI_CAN_DO_HISPD |
  154. SDHCI_CAN_DO_ADMA2 |
  155. SDHCI_CAN_DO_SDMA,
  156. .caps1 = SDHCI_DRIVER_TYPE_C |
  157. SDHCI_DRIVER_TYPE_D |
  158. SDHCI_SUPPORT_DDR50,
  159. .mmc_caps = MMC_CAP_1_8V_DDR,
  160. };
  161. static const struct sdhci_pltfm_data sdhci_bcm2835_pltfm_data = {
  162. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  163. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  164. SDHCI_QUIRK_MISSING_CAPS,
  165. .ops = &sdhci_iproc_ops,
  166. };
  167. static const struct sdhci_iproc_data bcm2835_data = {
  168. .pdata = &sdhci_bcm2835_pltfm_data,
  169. .caps = SDHCI_CAN_VDD_330,
  170. .caps1 = 0x00000000,
  171. .mmc_caps = 0x00000000,
  172. };
  173. static const struct of_device_id sdhci_iproc_of_match[] = {
  174. { .compatible = "brcm,bcm2835-sdhci", .data = &bcm2835_data },
  175. { .compatible = "brcm,sdhci-iproc-cygnus", .data = &iproc_data },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(of, sdhci_iproc_of_match);
  179. static int sdhci_iproc_probe(struct platform_device *pdev)
  180. {
  181. const struct of_device_id *match;
  182. const struct sdhci_iproc_data *iproc_data;
  183. struct sdhci_host *host;
  184. struct sdhci_iproc_host *iproc_host;
  185. struct sdhci_pltfm_host *pltfm_host;
  186. int ret;
  187. match = of_match_device(sdhci_iproc_of_match, &pdev->dev);
  188. if (!match)
  189. return -EINVAL;
  190. iproc_data = match->data;
  191. host = sdhci_pltfm_init(pdev, iproc_data->pdata, sizeof(*iproc_host));
  192. if (IS_ERR(host))
  193. return PTR_ERR(host);
  194. pltfm_host = sdhci_priv(host);
  195. iproc_host = sdhci_pltfm_priv(pltfm_host);
  196. iproc_host->data = iproc_data;
  197. mmc_of_parse(host->mmc);
  198. sdhci_get_of_property(pdev);
  199. host->mmc->caps |= iproc_host->data->mmc_caps;
  200. pltfm_host->clk = devm_clk_get(&pdev->dev, NULL);
  201. if (IS_ERR(pltfm_host->clk)) {
  202. ret = PTR_ERR(pltfm_host->clk);
  203. goto err;
  204. }
  205. ret = clk_prepare_enable(pltfm_host->clk);
  206. if (ret) {
  207. dev_err(&pdev->dev, "failed to enable host clk\n");
  208. goto err;
  209. }
  210. if (iproc_host->data->pdata->quirks & SDHCI_QUIRK_MISSING_CAPS) {
  211. host->caps = iproc_host->data->caps;
  212. host->caps1 = iproc_host->data->caps1;
  213. }
  214. ret = sdhci_add_host(host);
  215. if (ret)
  216. goto err_clk;
  217. return 0;
  218. err_clk:
  219. clk_disable_unprepare(pltfm_host->clk);
  220. err:
  221. sdhci_pltfm_free(pdev);
  222. return ret;
  223. }
  224. static struct platform_driver sdhci_iproc_driver = {
  225. .driver = {
  226. .name = "sdhci-iproc",
  227. .of_match_table = sdhci_iproc_of_match,
  228. .pm = &sdhci_pltfm_pmops,
  229. },
  230. .probe = sdhci_iproc_probe,
  231. .remove = sdhci_pltfm_unregister,
  232. };
  233. module_platform_driver(sdhci_iproc_driver);
  234. MODULE_AUTHOR("Broadcom");
  235. MODULE_DESCRIPTION("IPROC SDHCI driver");
  236. MODULE_LICENSE("GPL v2");