sdhci-iproc.c 8.3 KB

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