sdhci-iproc.c 10 KB

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