1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the MMC / SD / SDIO cell found in:
5 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
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
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/mfd/core.h>
16 #include <linux/mfd/tmio.h>
17 #include <linux/mmc/host.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/scatterlist.h>
24 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
26 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
27 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);
34 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
36 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
37 usleep_range(10000, 11000);
39 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
40 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
42 usleep_range(10000, 11000);
45 static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
46 unsigned int new_clock)
48 unsigned int clock, divisor;
53 tmio_mmc_clk_stop(host);
57 divisor = host->pdata->hclk / new_clock;
59 /* bit7 set: 1/512, ... bit0 set: 1/4, all bits clear: 1/2 */
60 clk_sel = (divisor <= 1);
61 clk = clk_sel ? 0 : (roundup_pow_of_two(divisor) >> 2);
63 host->pdata->set_clk_div(host->pdev, clk_sel);
65 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
66 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
67 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
68 usleep_range(10000, 11000);
70 tmio_mmc_clk_start(host);
73 #ifdef CONFIG_PM_SLEEP
74 static int tmio_mmc_suspend(struct device *dev)
76 struct platform_device *pdev = to_platform_device(dev);
77 const struct mfd_cell *cell = mfd_get_cell(pdev);
80 ret = pm_runtime_force_suspend(dev);
82 /* Tell MFD core it can disable us now.*/
83 if (!ret && cell->disable)
89 static int tmio_mmc_resume(struct device *dev)
91 struct platform_device *pdev = to_platform_device(dev);
92 const struct mfd_cell *cell = mfd_get_cell(pdev);
95 /* Tell the MFD core we are ready to be enabled */
97 ret = cell->resume(pdev);
100 ret = pm_runtime_force_resume(dev);
106 static int tmio_mmc_probe(struct platform_device *pdev)
108 const struct mfd_cell *cell = mfd_get_cell(pdev);
109 struct tmio_mmc_data *pdata;
110 struct tmio_mmc_host *host;
111 struct resource *res;
112 int ret = -EINVAL, irq;
114 if (pdev->num_resources != 2)
117 pdata = pdev->dev.platform_data;
118 if (!pdata || !pdata->hclk)
121 irq = platform_get_irq(pdev, 0);
127 /* Tell the MFD core we are ready to be enabled */
129 ret = cell->enable(pdev);
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
140 pdata->flags |= TMIO_MMC_HAVE_HIGH_REG;
142 host = tmio_mmc_host_alloc(pdev, pdata);
148 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
149 host->bus_shift = resource_size(res) >> 10;
150 host->set_clock = tmio_mmc_set_clock;
152 host->mmc->f_max = pdata->hclk;
153 host->mmc->f_min = pdata->hclk / 512;
155 ret = tmio_mmc_host_probe(host);
159 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
160 IRQF_TRIGGER_FALLING,
161 dev_name(&pdev->dev), host);
165 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
166 (unsigned long)host->ctl, irq);
171 tmio_mmc_host_remove(host);
173 tmio_mmc_host_free(host);
181 static int tmio_mmc_remove(struct platform_device *pdev)
183 const struct mfd_cell *cell = mfd_get_cell(pdev);
184 struct tmio_mmc_host *host = platform_get_drvdata(pdev);
186 tmio_mmc_host_remove(host);
193 /* ------------------- device registration ----------------------- */
195 static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
196 SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
197 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
198 tmio_mmc_host_runtime_resume, NULL)
201 static struct platform_driver tmio_mmc_driver = {
204 .pm = &tmio_mmc_dev_pm_ops,
206 .probe = tmio_mmc_probe,
207 .remove = tmio_mmc_remove,
210 module_platform_driver(tmio_mmc_driver);
212 MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
213 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
214 MODULE_LICENSE("GPL v2");
215 MODULE_ALIAS("platform:tmio-mmc");