blob: 93e83ad25976e756bdb04408b51c506925b02534 [file] [log] [blame]
Wolfram Sangf7070792018-08-22 00:02:17 +02001// SPDX-License-Identifier: GPL-2.0
Ian Molton4a489982008-07-15 16:02:21 +01002/*
Simon Hormanb21f13d2017-05-10 11:25:25 +02003 * Driver for the MMC / SD / SDIO cell found in:
4 *
5 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
Ian Molton4a489982008-07-15 16:02:21 +01006 *
Simon Horman87317c42017-05-30 14:50:52 +02007 * Copyright (C) 2017 Renesas Electronics Corporation
8 * Copyright (C) 2017 Horms Solutions, Simon Horman
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01009 * Copyright (C) 2007 Ian Molton
10 * Copyright (C) 2004 Ian Molton
Ian Molton4a489982008-07-15 16:02:21 +010011 */
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010012
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090013#include <linux/delay.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010014#include <linux/device.h>
Ian Molton4a489982008-07-15 16:02:21 +010015#include <linux/mfd/core.h>
16#include <linux/mfd/tmio.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010017#include <linux/mmc/host.h>
18#include <linux/module.h>
19#include <linux/pagemap.h>
20#include <linux/scatterlist.h>
Ian Molton4a489982008-07-15 16:02:21 +010021
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010022#include "tmio_mmc.h"
Ian Molton4a489982008-07-15 16:02:21 +010023
Masahiro Yamada722fb612018-10-10 12:51:32 +090024/* Registers specific to this variant */
25#define CTL_SDIO_REGS 0x100
26#define CTL_CLK_AND_WAIT_CTL 0x138
27#define CTL_RESET_SDIO 0x1e0
28
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090029static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
30{
31 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
32 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
33
34 usleep_range(10000, 11000);
35 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
36 usleep_range(10000, 11000);
37}
38
39static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
40{
41 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
42 usleep_range(10000, 11000);
43
44 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
45 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
46
47 usleep_range(10000, 11000);
48}
49
50static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
51 unsigned int new_clock)
52{
Wolfram Sang75586bb2018-08-30 14:16:03 +020053 unsigned int divisor;
Masahiro Yamadadb4cea92018-08-23 13:44:20 +090054 u32 clk = 0;
55 int clk_sel;
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090056
57 if (new_clock == 0) {
58 tmio_mmc_clk_stop(host);
59 return;
60 }
61
Masahiro Yamadadb4cea92018-08-23 13:44:20 +090062 divisor = host->pdata->hclk / new_clock;
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090063
Wolfram Sang4c595c02018-08-30 14:14:38 +020064 /* bit7 set: 1/512, ... bit0 set: 1/4, all bits clear: 1/2 */
65 clk_sel = (divisor <= 1);
66 clk = clk_sel ? 0 : (roundup_pow_of_two(divisor) >> 2);
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090067
Masahiro Yamadadb4cea92018-08-23 13:44:20 +090068 host->pdata->set_clk_div(host->pdev, clk_sel);
Masahiro Yamada0196c8d2018-08-23 13:44:16 +090069
70 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
71 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
72 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
73 usleep_range(10000, 11000);
74
75 tmio_mmc_clk_start(host);
76}
77
Masahiro Yamadaacb9fce2018-10-10 12:51:31 +090078static void tmio_mmc_reset(struct tmio_mmc_host *host)
79{
80 /* FIXME - should we set stop clock reg here */
81 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
82 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
83 usleep_range(10000, 11000);
84 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
85 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
86 usleep_range(10000, 11000);
87
88 if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
89 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
90 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
91 }
92}
93
Ulf Hanssonc8964482013-10-23 14:57:50 +020094#ifdef CONFIG_PM_SLEEP
95static int tmio_mmc_suspend(struct device *dev)
Ian Molton4a489982008-07-15 16:02:21 +010096{
Ulf Hanssonc8964482013-10-23 14:57:50 +020097 struct platform_device *pdev = to_platform_device(dev);
98 const struct mfd_cell *cell = mfd_get_cell(pdev);
Ian Molton4a489982008-07-15 16:02:21 +010099 int ret;
100
Ulf Hansson70a15e12014-08-25 12:28:20 +0200101 ret = pm_runtime_force_suspend(dev);
Ian Molton4a489982008-07-15 16:02:21 +0100102
103 /* Tell MFD core it can disable us now.*/
104 if (!ret && cell->disable)
Ulf Hanssonc8964482013-10-23 14:57:50 +0200105 cell->disable(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100106
107 return ret;
108}
109
Ulf Hanssonc8964482013-10-23 14:57:50 +0200110static int tmio_mmc_resume(struct device *dev)
Ian Molton4a489982008-07-15 16:02:21 +0100111{
Ulf Hanssonc8964482013-10-23 14:57:50 +0200112 struct platform_device *pdev = to_platform_device(dev);
113 const struct mfd_cell *cell = mfd_get_cell(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100114 int ret = 0;
115
Ian Molton4a489982008-07-15 16:02:21 +0100116 /* Tell the MFD core we are ready to be enabled */
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000117 if (cell->resume)
Ulf Hanssonc8964482013-10-23 14:57:50 +0200118 ret = cell->resume(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100119
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000120 if (!ret)
Ulf Hansson70a15e12014-08-25 12:28:20 +0200121 ret = pm_runtime_force_resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +0100122
Ian Molton4a489982008-07-15 16:02:21 +0100123 return ret;
124}
Ian Molton4a489982008-07-15 16:02:21 +0100125#endif
126
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500127static int tmio_mmc_probe(struct platform_device *pdev)
Ian Molton4a489982008-07-15 16:02:21 +0100128{
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100129 const struct mfd_cell *cell = mfd_get_cell(pdev);
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200130 struct tmio_mmc_data *pdata;
Ian Molton4a489982008-07-15 16:02:21 +0100131 struct tmio_mmc_host *host;
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800132 struct resource *res;
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000133 int ret = -EINVAL, irq;
Ian Molton4a489982008-07-15 16:02:21 +0100134
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100135 if (pdev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +0100136 goto out;
137
Samuel Ortizec719742011-04-06 11:38:14 +0200138 pdata = pdev->dev.platform_data;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +0200139 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200140 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +0200141
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000142 irq = platform_get_irq(pdev, 0);
143 if (irq < 0) {
144 ret = irq;
145 goto out;
146 }
147
Ian Molton4a489982008-07-15 16:02:21 +0100148 /* Tell the MFD core we are ready to be enabled */
149 if (cell->enable) {
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100150 ret = cell->enable(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100151 if (ret)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100152 goto out;
Ian Molton4a489982008-07-15 16:02:21 +0100153 }
154
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ian Molton25db67e2015-04-27 00:01:06 +0100156 if (!res) {
157 ret = -EINVAL;
158 goto cell_disable;
159 }
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800160
Masahiro Yamadab21fc292018-01-18 01:28:02 +0900161 host = tmio_mmc_host_alloc(pdev, pdata);
Masahiro Yamada8d09a132018-01-18 01:28:01 +0900162 if (IS_ERR(host)) {
163 ret = PTR_ERR(host);
Magnus Damm7ee422d2010-02-17 16:38:23 +0900164 goto cell_disable;
Masahiro Yamada8d09a132018-01-18 01:28:01 +0900165 }
Ian Molton4a489982008-07-15 16:02:21 +0100166
Kuninori Morimoto7445bf92015-01-13 04:58:20 +0000167 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
168 host->bus_shift = resource_size(res) >> 10;
Masahiro Yamada0196c8d2018-08-23 13:44:16 +0900169 host->set_clock = tmio_mmc_set_clock;
Masahiro Yamadaacb9fce2018-10-10 12:51:31 +0900170 host->reset = tmio_mmc_reset;
Kuninori Morimoto7445bf92015-01-13 04:58:20 +0000171
Masahiro Yamadab21fc292018-01-18 01:28:02 +0900172 host->mmc->f_max = pdata->hclk;
173 host->mmc->f_min = pdata->hclk / 512;
174
Masahiro Yamadabc457192018-01-18 01:28:04 +0900175 ret = tmio_mmc_host_probe(host);
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000176 if (ret)
177 goto host_free;
178
Ian Moltonde501af2015-04-27 00:01:36 +0100179 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
Simon Hormanf2218db2017-06-16 18:11:03 +0200180 IRQF_TRIGGER_FALLING,
181 dev_name(&pdev->dev), host);
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000182 if (ret)
183 goto host_remove;
184
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000185 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000186 (unsigned long)host->ctl, irq);
Ian Molton4a489982008-07-15 16:02:21 +0100187
Ian Molton4a489982008-07-15 16:02:21 +0100188 return 0;
189
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000190host_remove:
191 tmio_mmc_host_remove(host);
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000192host_free:
193 tmio_mmc_host_free(host);
Magnus Damm7ee422d2010-02-17 16:38:23 +0900194cell_disable:
195 if (cell->disable)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100196 cell->disable(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100197out:
198 return ret;
199}
200
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500201static int tmio_mmc_remove(struct platform_device *pdev)
Ian Molton4a489982008-07-15 16:02:21 +0100202{
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100203 const struct mfd_cell *cell = mfd_get_cell(pdev);
Masahiro Yamadaa3b053732017-11-25 01:24:39 +0900204 struct tmio_mmc_host *host = platform_get_drvdata(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100205
Masahiro Yamadaa3b053732017-11-25 01:24:39 +0900206 tmio_mmc_host_remove(host);
207 if (cell->disable)
208 cell->disable(pdev);
Ian Molton4a489982008-07-15 16:02:21 +0100209
210 return 0;
211}
212
213/* ------------------- device registration ----------------------- */
214
Ulf Hanssonc8964482013-10-23 14:57:50 +0200215static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
216 SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +0100217 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
Simon Hormanf2218db2017-06-16 18:11:03 +0200218 tmio_mmc_host_runtime_resume, NULL)
Ulf Hanssonc8964482013-10-23 14:57:50 +0200219};
220
Ian Molton4a489982008-07-15 16:02:21 +0100221static struct platform_driver tmio_mmc_driver = {
222 .driver = {
223 .name = "tmio-mmc",
Ulf Hanssonc8964482013-10-23 14:57:50 +0200224 .pm = &tmio_mmc_dev_pm_ops,
Ian Molton4a489982008-07-15 16:02:21 +0100225 },
226 .probe = tmio_mmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500227 .remove = tmio_mmc_remove,
Ian Molton4a489982008-07-15 16:02:21 +0100228};
229
Axel Lind1f81a62011-11-26 12:55:43 +0800230module_platform_driver(tmio_mmc_driver);
Ian Molton4a489982008-07-15 16:02:21 +0100231
232MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
233MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
234MODULE_LICENSE("GPL v2");
235MODULE_ALIAS("platform:tmio-mmc");