tmio_mmc.c 5.55 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Ian Molton's avatar
Ian Molton committed
2
/*
3
4
5
 * Driver for the MMC / SD / SDIO cell found in:
 *
 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
Ian Molton's avatar
Ian Molton committed
6
 *
7
8
 * Copyright (C) 2017 Renesas Electronics Corporation
 * Copyright (C) 2017 Horms Solutions, Simon Horman
9
10
 * Copyright (C) 2007 Ian Molton
 * Copyright (C) 2004 Ian Molton
Ian Molton's avatar
Ian Molton committed
11
 */
12

13
#include <linux/delay.h>
14
#include <linux/device.h>
Ian Molton's avatar
Ian Molton committed
15
16
#include <linux/mfd/core.h>
#include <linux/mfd/tmio.h>
17
18
19
20
#include <linux/mmc/host.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/scatterlist.h>
21

22
#include "tmio_mmc.h"
Ian Molton's avatar
Ian Molton committed
23

24
25
26
27
28
/* Registers specific to this variant */
#define CTL_SDIO_REGS		0x100
#define CTL_CLK_AND_WAIT_CTL	0x138
#define CTL_RESET_SDIO		0x1e0

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
{
	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));

	usleep_range(10000, 11000);
	sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
	usleep_range(10000, 11000);
}

static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
{
	sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
	usleep_range(10000, 11000);

	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
		sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));

	usleep_range(10000, 11000);
}

static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
			       unsigned int new_clock)
{
53
	unsigned int divisor;
54
55
	u32 clk = 0;
	int clk_sel;
56
57
58
59
60
61

	if (new_clock == 0) {
		tmio_mmc_clk_stop(host);
		return;
	}

62
	divisor = host->pdata->hclk / new_clock;
63

64
65
66
	/* bit7 set: 1/512, ... bit0 set: 1/4, all bits clear: 1/2 */
	clk_sel = (divisor <= 1);
	clk = clk_sel ? 0 : (roundup_pow_of_two(divisor) >> 2);
67

68
	host->pdata->set_clk_div(host->pdev, clk_sel);
69
70
71
72
73
74
75
76
77

	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
			sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
	sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
	usleep_range(10000, 11000);

	tmio_mmc_clk_start(host);
}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
static void tmio_mmc_reset(struct tmio_mmc_host *host)
{
	/* FIXME - should we set stop clock reg here */
	sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
	sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
	usleep_range(10000, 11000);
	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
	sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
	usleep_range(10000, 11000);

	if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
		sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
	}
}

94
95
#ifdef CONFIG_PM_SLEEP
static int tmio_mmc_suspend(struct device *dev)
Ian Molton's avatar
Ian Molton committed
96
{
97
98
	struct platform_device *pdev = to_platform_device(dev);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
Ian Molton's avatar
Ian Molton committed
99
100
	int ret;

101
	ret = pm_runtime_force_suspend(dev);
Ian Molton's avatar
Ian Molton committed
102
103
104

	/* Tell MFD core it can disable us now.*/
	if (!ret && cell->disable)
105
		cell->disable(pdev);
Ian Molton's avatar
Ian Molton committed
106
107
108
109

	return ret;
}

110
static int tmio_mmc_resume(struct device *dev)
Ian Molton's avatar
Ian Molton committed
111
{
112
113
	struct platform_device *pdev = to_platform_device(dev);
	const struct mfd_cell *cell = mfd_get_cell(pdev);
Ian Molton's avatar
Ian Molton committed
114
115
116
	int ret = 0;

	/* Tell the MFD core we are ready to be enabled */
117
	if (cell->resume)
118
		ret = cell->resume(pdev);
Ian Molton's avatar
Ian Molton committed
119

120
	if (!ret)
121
		ret = pm_runtime_force_resume(dev);
Ian Molton's avatar
Ian Molton committed
122
123
124
125
126

	return ret;
}
#endif

Bill Pemberton's avatar
Bill Pemberton committed
127
static int tmio_mmc_probe(struct platform_device *pdev)
Ian Molton's avatar
Ian Molton committed
128
{
129
	const struct mfd_cell *cell = mfd_get_cell(pdev);
130
	struct tmio_mmc_data *pdata;
Ian Molton's avatar
Ian Molton committed
131
	struct tmio_mmc_host *host;
132
	struct resource *res;
133
	int ret = -EINVAL, irq;
Ian Molton's avatar
Ian Molton committed
134

135
	if (pdev->num_resources != 2)
Ian Molton's avatar
Ian Molton committed
136
137
		goto out;

138
	pdata = pdev->dev.platform_data;
139
	if (!pdata || !pdata->hclk)
140
		goto out;
141

142
143
144
145
146
147
	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		ret = irq;
		goto out;
	}

Ian Molton's avatar
Ian Molton committed
148
149
	/* Tell the MFD core we are ready to be enabled */
	if (cell->enable) {
150
		ret = cell->enable(pdev);
Ian Molton's avatar
Ian Molton committed
151
		if (ret)
152
			goto out;
Ian Molton's avatar
Ian Molton committed
153
154
	}

155
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
156
157
158
159
	if (!res) {
		ret = -EINVAL;
		goto cell_disable;
	}
160

161
	host = tmio_mmc_host_alloc(pdev, pdata);
162
163
	if (IS_ERR(host)) {
		ret = PTR_ERR(host);
164
		goto cell_disable;
165
	}
Ian Molton's avatar
Ian Molton committed
166

167
168
	/* SD control register space size is 0x200, 0x400 for bus_shift=1 */
	host->bus_shift = resource_size(res) >> 10;
169
	host->set_clock = tmio_mmc_set_clock;
170
	host->reset = tmio_mmc_reset;
171

172
173
174
	host->mmc->f_max = pdata->hclk;
	host->mmc->f_min = pdata->hclk / 512;

175
176
	pm_runtime_enable(&pdev->dev);

177
	ret = tmio_mmc_host_probe(host);
178
179
180
	if (ret)
		goto host_free;

Ian Molton's avatar
Ian Molton committed
181
	ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
182
183
			       IRQF_TRIGGER_FALLING,
			       dev_name(&pdev->dev), host);
184
185
186
	if (ret)
		goto host_remove;

187
	pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
188
		(unsigned long)host->ctl, irq);
Ian Molton's avatar
Ian Molton committed
189
190
191

	return 0;

192
193
host_remove:
	tmio_mmc_host_remove(host);
194
195
host_free:
	tmio_mmc_host_free(host);
196
	pm_runtime_disable(&pdev->dev);
197
198
cell_disable:
	if (cell->disable)
199
		cell->disable(pdev);
Ian Molton's avatar
Ian Molton committed
200
201
202
203
out:
	return ret;
}

Bill Pemberton's avatar
Bill Pemberton committed
204
static int tmio_mmc_remove(struct platform_device *pdev)
Ian Molton's avatar
Ian Molton committed
205
{
206
	const struct mfd_cell *cell = mfd_get_cell(pdev);
207
	struct tmio_mmc_host *host = platform_get_drvdata(pdev);
Ian Molton's avatar
Ian Molton committed
208

209
210
211
	tmio_mmc_host_remove(host);
	if (cell->disable)
		cell->disable(pdev);
Ian Molton's avatar
Ian Molton committed
212

213
214
	pm_runtime_disable(&pdev->dev);

Ian Molton's avatar
Ian Molton committed
215
216
217
218
219
	return 0;
}

/* ------------------- device registration ----------------------- */

220
221
static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
222
	SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
223
			   tmio_mmc_host_runtime_resume, NULL)
224
225
};

Ian Molton's avatar
Ian Molton committed
226
227
228
static struct platform_driver tmio_mmc_driver = {
	.driver = {
		.name = "tmio-mmc",
229
		.pm = &tmio_mmc_dev_pm_ops,
Ian Molton's avatar
Ian Molton committed
230
231
	},
	.probe = tmio_mmc_probe,
232
	.remove = tmio_mmc_remove,
Ian Molton's avatar
Ian Molton committed
233
234
};

235
module_platform_driver(tmio_mmc_driver);
Ian Molton's avatar
Ian Molton committed
236
237
238
239
240

MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:tmio-mmc");