sdhci-iproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2014 Broadcom Corporation
  3. /*
  4. * iProc SDHCI platform driver
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/delay.h>
  8. #include <linux/module.h>
  9. #include <linux/mmc/host.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include "sdhci-pltfm.h"
  13. struct sdhci_iproc_data {
  14. const struct sdhci_pltfm_data *pdata;
  15. u32 caps;
  16. u32 caps1;
  17. u32 mmc_caps;
  18. };
  19. struct sdhci_iproc_host {
  20. const struct sdhci_iproc_data *data;
  21. u32 shadow_cmd;
  22. u32 shadow_blk;
  23. bool is_cmd_shadowed;
  24. bool is_blk_shadowed;
  25. };
  26. #define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
  27. static inline u32 sdhci_iproc_readl(struct sdhci_host *host, int reg)
  28. {
  29. u32 val = readl(host->ioaddr + reg);
  30. pr_debug("%s: readl [0x%02x] 0x%08x\n",
  31. mmc_hostname(host->mmc), reg, val);
  32. return val;
  33. }
  34. static u16 sdhci_iproc_readw(struct sdhci_host *host, int reg)
  35. {
  36. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  37. struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
  38. u32 val;
  39. u16 word;
  40. if ((reg == SDHCI_TRANSFER_MODE) && iproc_host->is_cmd_shadowed) {
  41. /* Get the saved transfer mode */
  42. val = iproc_host->shadow_cmd;
  43. } else if ((reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) &&
  44. iproc_host->is_blk_shadowed) {
  45. /* Get the saved block info */
  46. val = iproc_host->shadow_blk;
  47. } else {
  48. val = sdhci_iproc_readl(host, (reg & ~3));
  49. }
  50. word = val >> REG_OFFSET_IN_BITS(reg) & 0xffff;
  51. return word;
  52. }
  53. static u8 sdhci_iproc_readb(struct sdhci_host *host, int reg)
  54. {
  55. u32 val = sdhci_iproc_readl(host, (reg & ~3));
  56. u8 byte = val >> REG_OFFSET_IN_BITS(reg) & 0xff;
  57. return byte;
  58. }
  59. static inline void sdhci_iproc_writel(struct sdhci_host *host, u32 val, int reg)
  60. {
  61. pr_debug("%s: writel [0x%02x] 0x%08x\n",
  62. mmc_hostname(host->mmc), reg, val);
  63. writel(val, host->ioaddr + reg);
  64. if (host->clock <= 400000) {
  65. /* Round up to micro-second four SD clock delay */
  66. if (host->clock)
  67. udelay((4 * 1000000 + host->clock - 1) / host->clock);
  68. else
  69. udelay(10);
  70. }
  71. }
  72. /*
  73. * The Arasan has a bugette whereby it may lose the content of successive
  74. * writes to the same register that are within two SD-card clock cycles of
  75. * each other (a clock domain crossing problem). The data
  76. * register does not have this problem, which is just as well - otherwise we'd
  77. * have to nobble the DMA engine too.
  78. *
  79. * This wouldn't be a problem with the code except that we can only write the
  80. * controller with 32-bit writes. So two different 16-bit registers are
  81. * written back to back creates the problem.
  82. *
  83. * In reality, this only happens when SDHCI_BLOCK_SIZE and SDHCI_BLOCK_COUNT
  84. * are written followed by SDHCI_TRANSFER_MODE and SDHCI_COMMAND.
  85. * The BLOCK_SIZE and BLOCK_COUNT are meaningless until a command issued so
  86. * the work around can be further optimized. We can keep shadow values of
  87. * BLOCK_SIZE, BLOCK_COUNT, and TRANSFER_MODE until a COMMAND is issued.
  88. * Then, write the BLOCK_SIZE+BLOCK_COUNT in a single 32-bit write followed
  89. * by the TRANSFER+COMMAND in another 32-bit write.
  90. */
  91. static void sdhci_iproc_writew(struct sdhci_host *host, u16 val, int reg)
  92. {
  93. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  94. struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
  95. u32 word_shift = REG_OFFSET_IN_BITS(reg);
  96. u32 mask = 0xffff << word_shift;
  97. u32 oldval, newval;
  98. if (reg == SDHCI_COMMAND) {
  99. /* Write the block now as we are issuing a command */
  100. if (iproc_host->is_blk_shadowed) {
  101. sdhci_iproc_writel(host, iproc_host->shadow_blk,
  102. SDHCI_BLOCK_SIZE);
  103. iproc_host->is_blk_shadowed = false;
  104. }
  105. oldval = iproc_host->shadow_cmd;
  106. iproc_host->is_cmd_shadowed = false;
  107. } else if ((reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) &&
  108. iproc_host->is_blk_shadowed) {
  109. /* Block size and count are stored in shadow reg */
  110. oldval = iproc_host->shadow_blk;
  111. } else {
  112. /* Read reg, all other registers are not shadowed */
  113. oldval = sdhci_iproc_readl(host, (reg & ~3));
  114. }
  115. newval = (oldval & ~mask) | (val << word_shift);
  116. if (reg == SDHCI_TRANSFER_MODE) {
  117. /* Save the transfer mode until the command is issued */
  118. iproc_host->shadow_cmd = newval;
  119. iproc_host->is_cmd_shadowed = true;
  120. } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
  121. /* Save the block info until the command is issued */
  122. iproc_host->shadow_blk = newval;
  123. iproc_host->is_blk_shadowed = true;
  124. } else {
  125. /* Command or other regular 32-bit write */
  126. sdhci_iproc_writel(host, newval, reg & ~3);
  127. }
  128. }
  129. static void sdhci_iproc_writeb(struct sdhci_host *host, u8 val, int reg)
  130. {
  131. u32 oldval = sdhci_iproc_readl(host, (reg & ~3));
  132. u32 byte_shift = REG_OFFSET_IN_BITS(reg);
  133. u32 mask = 0xff << byte_shift;
  134. u32 newval = (oldval & ~mask) | (val << byte_shift);
  135. sdhci_iproc_writel(host, newval, reg & ~3);
  136. }
  137. static unsigned int sdhci_iproc_get_max_clock(struct sdhci_host *host)
  138. {
  139. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  140. if (pltfm_host->clk)
  141. return sdhci_pltfm_clk_get_max_clock(host);
  142. else
  143. return pltfm_host->clock;
  144. }
  145. /*
  146. * There is a known bug on BCM2711's SDHCI core integration where the
  147. * controller will hang when the difference between the core clock and the bus
  148. * clock is too great. Specifically this can be reproduced under the following
  149. * conditions:
  150. *
  151. * - No SD card plugged in, polling thread is running, probing cards at
  152. * 100 kHz.
  153. * - BCM2711's core clock configured at 500MHz or more
  154. *
  155. * So we set 200kHz as the minimum clock frequency available for that SoC.
  156. */
  157. static unsigned int sdhci_iproc_bcm2711_get_min_clock(struct sdhci_host *host)
  158. {
  159. return 200000;
  160. }
  161. static const struct sdhci_ops sdhci_iproc_ops = {
  162. .set_clock = sdhci_set_clock,
  163. .get_max_clock = sdhci_iproc_get_max_clock,
  164. .set_bus_width = sdhci_set_bus_width,
  165. .reset = sdhci_reset,
  166. .set_uhs_signaling = sdhci_set_uhs_signaling,
  167. };
  168. static const struct sdhci_ops sdhci_iproc_32only_ops = {
  169. .read_l = sdhci_iproc_readl,
  170. .read_w = sdhci_iproc_readw,
  171. .read_b = sdhci_iproc_readb,
  172. .write_l = sdhci_iproc_writel,
  173. .write_w = sdhci_iproc_writew,
  174. .write_b = sdhci_iproc_writeb,
  175. .set_clock = sdhci_set_clock,
  176. .get_max_clock = sdhci_iproc_get_max_clock,
  177. .set_bus_width = sdhci_set_bus_width,
  178. .reset = sdhci_reset,
  179. .set_uhs_signaling = sdhci_set_uhs_signaling,
  180. };
  181. static const struct sdhci_pltfm_data sdhci_iproc_cygnus_pltfm_data = {
  182. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  183. SDHCI_QUIRK_NO_HISPD_BIT,
  184. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN | SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  185. .ops = &sdhci_iproc_32only_ops,
  186. };
  187. static const struct sdhci_iproc_data iproc_cygnus_data = {
  188. .pdata = &sdhci_iproc_cygnus_pltfm_data,
  189. .caps = ((0x1 << SDHCI_MAX_BLOCK_SHIFT)
  190. & SDHCI_MAX_BLOCK_MASK) |
  191. SDHCI_CAN_VDD_330 |
  192. SDHCI_CAN_VDD_180 |
  193. SDHCI_CAN_DO_SUSPEND |
  194. SDHCI_CAN_DO_HISPD |
  195. SDHCI_CAN_DO_ADMA2 |
  196. SDHCI_CAN_DO_SDMA,
  197. .caps1 = SDHCI_DRIVER_TYPE_C |
  198. SDHCI_DRIVER_TYPE_D |
  199. SDHCI_SUPPORT_DDR50,
  200. .mmc_caps = MMC_CAP_1_8V_DDR,
  201. };
  202. static const struct sdhci_pltfm_data sdhci_iproc_pltfm_data = {
  203. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  204. SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12 |
  205. SDHCI_QUIRK_NO_HISPD_BIT,
  206. .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN,
  207. .ops = &sdhci_iproc_ops,
  208. };
  209. static const struct sdhci_iproc_data iproc_data = {
  210. .pdata = &sdhci_iproc_pltfm_data,
  211. .caps = ((0x1 << SDHCI_MAX_BLOCK_SHIFT)
  212. & SDHCI_MAX_BLOCK_MASK) |
  213. SDHCI_CAN_VDD_330 |
  214. SDHCI_CAN_VDD_180 |
  215. SDHCI_CAN_DO_SUSPEND |
  216. SDHCI_CAN_DO_HISPD |
  217. SDHCI_CAN_DO_ADMA2 |
  218. SDHCI_CAN_DO_SDMA,
  219. .caps1 = SDHCI_DRIVER_TYPE_C |
  220. SDHCI_DRIVER_TYPE_D |
  221. SDHCI_SUPPORT_DDR50,
  222. };
  223. static const struct sdhci_pltfm_data sdhci_bcm2835_pltfm_data = {
  224. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  225. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  226. SDHCI_QUIRK_MISSING_CAPS |
  227. SDHCI_QUIRK_NO_HISPD_BIT,
  228. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  229. .ops = &sdhci_iproc_32only_ops,
  230. };
  231. static const struct sdhci_iproc_data bcm2835_data = {
  232. .pdata = &sdhci_bcm2835_pltfm_data,
  233. .caps = ((0x1 << SDHCI_MAX_BLOCK_SHIFT)
  234. & SDHCI_MAX_BLOCK_MASK) |
  235. SDHCI_CAN_VDD_330 |
  236. SDHCI_CAN_DO_HISPD,
  237. .caps1 = SDHCI_DRIVER_TYPE_A |
  238. SDHCI_DRIVER_TYPE_C,
  239. .mmc_caps = 0x00000000,
  240. };
  241. static const struct sdhci_ops sdhci_iproc_bcm2711_ops = {
  242. .read_l = sdhci_iproc_readl,
  243. .read_w = sdhci_iproc_readw,
  244. .read_b = sdhci_iproc_readb,
  245. .write_l = sdhci_iproc_writel,
  246. .write_w = sdhci_iproc_writew,
  247. .write_b = sdhci_iproc_writeb,
  248. .set_clock = sdhci_set_clock,
  249. .set_power = sdhci_set_power_and_bus_voltage,
  250. .get_max_clock = sdhci_iproc_get_max_clock,
  251. .get_min_clock = sdhci_iproc_bcm2711_get_min_clock,
  252. .set_bus_width = sdhci_set_bus_width,
  253. .reset = sdhci_reset,
  254. .set_uhs_signaling = sdhci_set_uhs_signaling,
  255. };
  256. static const struct sdhci_pltfm_data sdhci_bcm2711_pltfm_data = {
  257. .quirks = SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
  258. .ops = &sdhci_iproc_bcm2711_ops,
  259. };
  260. static const struct sdhci_iproc_data bcm2711_data = {
  261. .pdata = &sdhci_bcm2711_pltfm_data,
  262. .mmc_caps = MMC_CAP_3_3V_DDR,
  263. };
  264. static const struct sdhci_pltfm_data sdhci_bcm7211a0_pltfm_data = {
  265. .quirks = SDHCI_QUIRK_MISSING_CAPS |
  266. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  267. SDHCI_QUIRK_BROKEN_DMA |
  268. SDHCI_QUIRK_BROKEN_ADMA,
  269. .ops = &sdhci_iproc_ops,
  270. };
  271. #define BCM7211A0_BASE_CLK_MHZ 100
  272. static const struct sdhci_iproc_data bcm7211a0_data = {
  273. .pdata = &sdhci_bcm7211a0_pltfm_data,
  274. .caps = ((BCM7211A0_BASE_CLK_MHZ / 2) << SDHCI_TIMEOUT_CLK_SHIFT) |
  275. (BCM7211A0_BASE_CLK_MHZ << SDHCI_CLOCK_BASE_SHIFT) |
  276. ((0x2 << SDHCI_MAX_BLOCK_SHIFT)
  277. & SDHCI_MAX_BLOCK_MASK) |
  278. SDHCI_CAN_VDD_330 |
  279. SDHCI_CAN_VDD_180 |
  280. SDHCI_CAN_DO_SUSPEND |
  281. SDHCI_CAN_DO_HISPD,
  282. .caps1 = SDHCI_DRIVER_TYPE_C |
  283. SDHCI_DRIVER_TYPE_D,
  284. };
  285. static const struct of_device_id sdhci_iproc_of_match[] = {
  286. { .compatible = "brcm,bcm2835-sdhci", .data = &bcm2835_data },
  287. { .compatible = "brcm,bcm2711-emmc2", .data = &bcm2711_data },
  288. { .compatible = "brcm,sdhci-iproc-cygnus", .data = &iproc_cygnus_data},
  289. { .compatible = "brcm,sdhci-iproc", .data = &iproc_data },
  290. { .compatible = "brcm,bcm7211a0-sdhci", .data = &bcm7211a0_data },
  291. { }
  292. };
  293. MODULE_DEVICE_TABLE(of, sdhci_iproc_of_match);
  294. #ifdef CONFIG_ACPI
  295. /*
  296. * This is a duplicate of bcm2835_(pltfrm_)data without caps quirks
  297. * which are provided by the ACPI table.
  298. */
  299. static const struct sdhci_pltfm_data sdhci_bcm_arasan_data = {
  300. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  301. SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  302. SDHCI_QUIRK_NO_HISPD_BIT,
  303. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  304. .ops = &sdhci_iproc_32only_ops,
  305. };
  306. static const struct sdhci_iproc_data bcm_arasan_data = {
  307. .pdata = &sdhci_bcm_arasan_data,
  308. };
  309. static const struct acpi_device_id sdhci_iproc_acpi_ids[] = {
  310. { .id = "BRCM5871", .driver_data = (kernel_ulong_t)&iproc_cygnus_data },
  311. { .id = "BRCM5872", .driver_data = (kernel_ulong_t)&iproc_data },
  312. { .id = "BCM2847", .driver_data = (kernel_ulong_t)&bcm_arasan_data },
  313. { .id = "BRCME88C", .driver_data = (kernel_ulong_t)&bcm2711_data },
  314. { /* sentinel */ }
  315. };
  316. MODULE_DEVICE_TABLE(acpi, sdhci_iproc_acpi_ids);
  317. #endif
  318. static int sdhci_iproc_probe(struct platform_device *pdev)
  319. {
  320. struct device *dev = &pdev->dev;
  321. const struct sdhci_iproc_data *iproc_data = NULL;
  322. struct sdhci_host *host;
  323. struct sdhci_iproc_host *iproc_host;
  324. struct sdhci_pltfm_host *pltfm_host;
  325. int ret;
  326. iproc_data = device_get_match_data(dev);
  327. if (!iproc_data)
  328. return -ENODEV;
  329. host = sdhci_pltfm_init(pdev, iproc_data->pdata, sizeof(*iproc_host));
  330. if (IS_ERR(host))
  331. return PTR_ERR(host);
  332. pltfm_host = sdhci_priv(host);
  333. iproc_host = sdhci_pltfm_priv(pltfm_host);
  334. iproc_host->data = iproc_data;
  335. ret = mmc_of_parse(host->mmc);
  336. if (ret)
  337. goto err;
  338. sdhci_get_property(pdev);
  339. host->mmc->caps |= iproc_host->data->mmc_caps;
  340. if (dev->of_node) {
  341. pltfm_host->clk = devm_clk_get(dev, NULL);
  342. if (IS_ERR(pltfm_host->clk)) {
  343. ret = PTR_ERR(pltfm_host->clk);
  344. goto err;
  345. }
  346. ret = clk_prepare_enable(pltfm_host->clk);
  347. if (ret) {
  348. dev_err(dev, "failed to enable host clk\n");
  349. goto err;
  350. }
  351. }
  352. if (iproc_host->data->pdata->quirks & SDHCI_QUIRK_MISSING_CAPS) {
  353. host->caps = iproc_host->data->caps;
  354. host->caps1 = iproc_host->data->caps1;
  355. }
  356. ret = sdhci_add_host(host);
  357. if (ret)
  358. goto err_clk;
  359. return 0;
  360. err_clk:
  361. if (dev->of_node)
  362. clk_disable_unprepare(pltfm_host->clk);
  363. err:
  364. sdhci_pltfm_free(pdev);
  365. return ret;
  366. }
  367. static void sdhci_iproc_shutdown(struct platform_device *pdev)
  368. {
  369. sdhci_pltfm_suspend(&pdev->dev);
  370. }
  371. static struct platform_driver sdhci_iproc_driver = {
  372. .driver = {
  373. .name = "sdhci-iproc",
  374. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  375. .of_match_table = sdhci_iproc_of_match,
  376. .acpi_match_table = ACPI_PTR(sdhci_iproc_acpi_ids),
  377. .pm = &sdhci_pltfm_pmops,
  378. },
  379. .probe = sdhci_iproc_probe,
  380. .remove = sdhci_pltfm_unregister,
  381. .shutdown = sdhci_iproc_shutdown,
  382. };
  383. module_platform_driver(sdhci_iproc_driver);
  384. MODULE_AUTHOR("Broadcom");
  385. MODULE_DESCRIPTION("IPROC SDHCI driver");
  386. MODULE_LICENSE("GPL v2");