tmio_mmc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the MMC / SD / SDIO cell found in:
  4. *
  5. * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
  6. *
  7. * Copyright (C) 2017 Renesas Electronics Corporation
  8. * Copyright (C) 2017 Horms Solutions, Simon Horman
  9. * Copyright (C) 2007 Ian Molton
  10. * Copyright (C) 2004 Ian Molton
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/tmio.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/module.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/scatterlist.h>
  20. #include "tmio_mmc.h"
  21. /* Registers specific to this variant */
  22. #define CTL_SDIO_REGS 0x100
  23. #define CTL_CLK_AND_WAIT_CTL 0x138
  24. #define CTL_RESET_SDIO 0x1e0
  25. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  26. {
  27. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  28. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  29. usleep_range(10000, 11000);
  30. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  31. usleep_range(10000, 11000);
  32. }
  33. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  34. {
  35. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  36. usleep_range(10000, 11000);
  37. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  38. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  39. usleep_range(10000, 11000);
  40. }
  41. static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
  42. unsigned int new_clock)
  43. {
  44. unsigned int divisor;
  45. u32 clk = 0;
  46. int clk_sel;
  47. if (new_clock == 0) {
  48. tmio_mmc_clk_stop(host);
  49. return;
  50. }
  51. divisor = host->pdata->hclk / new_clock;
  52. /* bit7 set: 1/512, ... bit0 set: 1/4, all bits clear: 1/2 */
  53. clk_sel = (divisor <= 1);
  54. clk = clk_sel ? 0 : (roundup_pow_of_two(divisor) >> 2);
  55. host->pdata->set_clk_div(host->pdev, clk_sel);
  56. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  57. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  58. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
  59. usleep_range(10000, 11000);
  60. tmio_mmc_clk_start(host);
  61. }
  62. static void tmio_mmc_reset(struct tmio_mmc_host *host, bool preserve)
  63. {
  64. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  65. usleep_range(10000, 11000);
  66. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  67. usleep_range(10000, 11000);
  68. }
  69. #ifdef CONFIG_PM_SLEEP
  70. static int tmio_mmc_suspend(struct device *dev)
  71. {
  72. struct platform_device *pdev = to_platform_device(dev);
  73. const struct mfd_cell *cell = mfd_get_cell(pdev);
  74. int ret;
  75. ret = pm_runtime_force_suspend(dev);
  76. /* Tell MFD core it can disable us now.*/
  77. if (!ret && cell->disable)
  78. cell->disable(pdev);
  79. return ret;
  80. }
  81. static int tmio_mmc_resume(struct device *dev)
  82. {
  83. struct platform_device *pdev = to_platform_device(dev);
  84. const struct mfd_cell *cell = mfd_get_cell(pdev);
  85. int ret = 0;
  86. /* Tell the MFD core we are ready to be enabled */
  87. if (cell->resume)
  88. ret = cell->resume(pdev);
  89. if (!ret)
  90. ret = pm_runtime_force_resume(dev);
  91. return ret;
  92. }
  93. #endif
  94. static int tmio_mmc_probe(struct platform_device *pdev)
  95. {
  96. const struct mfd_cell *cell = mfd_get_cell(pdev);
  97. struct tmio_mmc_data *pdata;
  98. struct tmio_mmc_host *host;
  99. struct resource *res;
  100. int ret = -EINVAL, irq;
  101. if (pdev->num_resources != 2)
  102. goto out;
  103. pdata = pdev->dev.platform_data;
  104. if (!pdata || !pdata->hclk)
  105. goto out;
  106. irq = platform_get_irq(pdev, 0);
  107. if (irq < 0) {
  108. ret = irq;
  109. goto out;
  110. }
  111. /* Tell the MFD core we are ready to be enabled */
  112. if (cell->enable) {
  113. ret = cell->enable(pdev);
  114. if (ret)
  115. goto out;
  116. }
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. if (!res) {
  119. ret = -EINVAL;
  120. goto cell_disable;
  121. }
  122. host = tmio_mmc_host_alloc(pdev, pdata);
  123. if (IS_ERR(host)) {
  124. ret = PTR_ERR(host);
  125. goto cell_disable;
  126. }
  127. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  128. host->bus_shift = resource_size(res) >> 10;
  129. host->set_clock = tmio_mmc_set_clock;
  130. host->reset = tmio_mmc_reset;
  131. host->mmc->f_max = pdata->hclk;
  132. host->mmc->f_min = pdata->hclk / 512;
  133. ret = tmio_mmc_host_probe(host);
  134. if (ret)
  135. goto host_free;
  136. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
  137. IRQF_TRIGGER_FALLING,
  138. dev_name(&pdev->dev), host);
  139. if (ret)
  140. goto host_remove;
  141. pr_info("%s at 0x%p irq %d\n", mmc_hostname(host->mmc), host->ctl, irq);
  142. return 0;
  143. host_remove:
  144. tmio_mmc_host_remove(host);
  145. host_free:
  146. tmio_mmc_host_free(host);
  147. cell_disable:
  148. if (cell->disable)
  149. cell->disable(pdev);
  150. out:
  151. return ret;
  152. }
  153. static int tmio_mmc_remove(struct platform_device *pdev)
  154. {
  155. const struct mfd_cell *cell = mfd_get_cell(pdev);
  156. struct tmio_mmc_host *host = platform_get_drvdata(pdev);
  157. tmio_mmc_host_remove(host);
  158. if (cell->disable)
  159. cell->disable(pdev);
  160. return 0;
  161. }
  162. /* ------------------- device registration ----------------------- */
  163. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  164. SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
  165. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  166. tmio_mmc_host_runtime_resume, NULL)
  167. };
  168. static struct platform_driver tmio_mmc_driver = {
  169. .driver = {
  170. .name = "tmio-mmc",
  171. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  172. .pm = &tmio_mmc_dev_pm_ops,
  173. },
  174. .probe = tmio_mmc_probe,
  175. .remove = tmio_mmc_remove,
  176. };
  177. module_platform_driver(tmio_mmc_driver);
  178. MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
  179. MODULE_AUTHOR("Ian Molton <[email protected]>");
  180. MODULE_LICENSE("GPL v2");
  181. MODULE_ALIAS("platform:tmio-mmc");