tmio_mmc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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)
  63. {
  64. /* FIXME - should we set stop clock reg here */
  65. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  66. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  67. usleep_range(10000, 11000);
  68. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  69. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  70. usleep_range(10000, 11000);
  71. if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
  72. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
  73. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
  74. }
  75. }
  76. #ifdef CONFIG_PM_SLEEP
  77. static int tmio_mmc_suspend(struct device *dev)
  78. {
  79. struct platform_device *pdev = to_platform_device(dev);
  80. const struct mfd_cell *cell = mfd_get_cell(pdev);
  81. int ret;
  82. ret = pm_runtime_force_suspend(dev);
  83. /* Tell MFD core it can disable us now.*/
  84. if (!ret && cell->disable)
  85. cell->disable(pdev);
  86. return ret;
  87. }
  88. static int tmio_mmc_resume(struct device *dev)
  89. {
  90. struct platform_device *pdev = to_platform_device(dev);
  91. const struct mfd_cell *cell = mfd_get_cell(pdev);
  92. int ret = 0;
  93. /* Tell the MFD core we are ready to be enabled */
  94. if (cell->resume)
  95. ret = cell->resume(pdev);
  96. if (!ret)
  97. ret = pm_runtime_force_resume(dev);
  98. return ret;
  99. }
  100. #endif
  101. static int tmio_mmc_probe(struct platform_device *pdev)
  102. {
  103. const struct mfd_cell *cell = mfd_get_cell(pdev);
  104. struct tmio_mmc_data *pdata;
  105. struct tmio_mmc_host *host;
  106. struct resource *res;
  107. int ret = -EINVAL, irq;
  108. if (pdev->num_resources != 2)
  109. goto out;
  110. pdata = pdev->dev.platform_data;
  111. if (!pdata || !pdata->hclk)
  112. goto out;
  113. irq = platform_get_irq(pdev, 0);
  114. if (irq < 0) {
  115. ret = irq;
  116. goto out;
  117. }
  118. /* Tell the MFD core we are ready to be enabled */
  119. if (cell->enable) {
  120. ret = cell->enable(pdev);
  121. if (ret)
  122. goto out;
  123. }
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (!res) {
  126. ret = -EINVAL;
  127. goto cell_disable;
  128. }
  129. host = tmio_mmc_host_alloc(pdev, pdata);
  130. if (IS_ERR(host)) {
  131. ret = PTR_ERR(host);
  132. goto cell_disable;
  133. }
  134. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  135. host->bus_shift = resource_size(res) >> 10;
  136. host->set_clock = tmio_mmc_set_clock;
  137. host->reset = tmio_mmc_reset;
  138. host->mmc->f_max = pdata->hclk;
  139. host->mmc->f_min = pdata->hclk / 512;
  140. ret = tmio_mmc_host_probe(host);
  141. if (ret)
  142. goto host_free;
  143. ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
  144. IRQF_TRIGGER_FALLING,
  145. dev_name(&pdev->dev), host);
  146. if (ret)
  147. goto host_remove;
  148. pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
  149. (unsigned long)host->ctl, irq);
  150. return 0;
  151. host_remove:
  152. tmio_mmc_host_remove(host);
  153. host_free:
  154. tmio_mmc_host_free(host);
  155. cell_disable:
  156. if (cell->disable)
  157. cell->disable(pdev);
  158. out:
  159. return ret;
  160. }
  161. static int tmio_mmc_remove(struct platform_device *pdev)
  162. {
  163. const struct mfd_cell *cell = mfd_get_cell(pdev);
  164. struct tmio_mmc_host *host = platform_get_drvdata(pdev);
  165. tmio_mmc_host_remove(host);
  166. if (cell->disable)
  167. cell->disable(pdev);
  168. return 0;
  169. }
  170. /* ------------------- device registration ----------------------- */
  171. static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
  172. SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
  173. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  174. tmio_mmc_host_runtime_resume, NULL)
  175. };
  176. static struct platform_driver tmio_mmc_driver = {
  177. .driver = {
  178. .name = "tmio-mmc",
  179. .pm = &tmio_mmc_dev_pm_ops,
  180. },
  181. .probe = tmio_mmc_probe,
  182. .remove = tmio_mmc_remove,
  183. };
  184. module_platform_driver(tmio_mmc_driver);
  185. MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
  186. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
  187. MODULE_LICENSE("GPL v2");
  188. MODULE_ALIAS("platform:tmio-mmc");