Loading...
1/*
2 * linux/drivers/mmc/host/tmio_mmc.c
3 *
4 * Copyright (C) 2007 Ian Molton
5 * Copyright (C) 2004 Ian Molton
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Driver for the MMC / SD / SDIO cell found in:
12 *
13 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
14 */
15
16#include <linux/device.h>
17#include <linux/mfd/core.h>
18#include <linux/mfd/tmio.h>
19#include <linux/mmc/host.h>
20#include <linux/module.h>
21#include <linux/pagemap.h>
22#include <linux/scatterlist.h>
23
24#include "tmio_mmc.h"
25
26#ifdef CONFIG_PM_SLEEP
27static int tmio_mmc_suspend(struct device *dev)
28{
29 struct platform_device *pdev = to_platform_device(dev);
30 const struct mfd_cell *cell = mfd_get_cell(pdev);
31 int ret;
32
33 ret = pm_runtime_force_suspend(dev);
34
35 /* Tell MFD core it can disable us now.*/
36 if (!ret && cell->disable)
37 cell->disable(pdev);
38
39 return ret;
40}
41
42static int tmio_mmc_resume(struct device *dev)
43{
44 struct platform_device *pdev = to_platform_device(dev);
45 const struct mfd_cell *cell = mfd_get_cell(pdev);
46 int ret = 0;
47
48 /* Tell the MFD core we are ready to be enabled */
49 if (cell->resume)
50 ret = cell->resume(pdev);
51
52 if (!ret)
53 ret = pm_runtime_force_resume(dev);
54
55 return ret;
56}
57#endif
58
59static int tmio_mmc_probe(struct platform_device *pdev)
60{
61 const struct mfd_cell *cell = mfd_get_cell(pdev);
62 struct tmio_mmc_data *pdata;
63 struct tmio_mmc_host *host;
64 struct resource *res;
65 int ret = -EINVAL, irq;
66
67 if (pdev->num_resources != 2)
68 goto out;
69
70 pdata = pdev->dev.platform_data;
71 if (!pdata || !pdata->hclk)
72 goto out;
73
74 irq = platform_get_irq(pdev, 0);
75 if (irq < 0) {
76 ret = irq;
77 goto out;
78 }
79
80 /* Tell the MFD core we are ready to be enabled */
81 if (cell->enable) {
82 ret = cell->enable(pdev);
83 if (ret)
84 goto out;
85 }
86
87 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
88 if (!res) {
89 ret = -EINVAL;
90 goto cell_disable;
91 }
92
93 pdata->flags |= TMIO_MMC_HAVE_HIGH_REG;
94
95 host = tmio_mmc_host_alloc(pdev);
96 if (!host)
97 goto cell_disable;
98
99 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
100 host->bus_shift = resource_size(res) >> 10;
101
102 ret = tmio_mmc_host_probe(host, pdata);
103 if (ret)
104 goto host_free;
105
106 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
107 IRQF_TRIGGER_FALLING,
108 dev_name(&pdev->dev), host);
109 if (ret)
110 goto host_remove;
111
112 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
113 (unsigned long)host->ctl, irq);
114
115 return 0;
116
117host_remove:
118 tmio_mmc_host_remove(host);
119host_free:
120 tmio_mmc_host_free(host);
121cell_disable:
122 if (cell->disable)
123 cell->disable(pdev);
124out:
125 return ret;
126}
127
128static int tmio_mmc_remove(struct platform_device *pdev)
129{
130 const struct mfd_cell *cell = mfd_get_cell(pdev);
131 struct mmc_host *mmc = platform_get_drvdata(pdev);
132
133 if (mmc) {
134 struct tmio_mmc_host *host = mmc_priv(mmc);
135 tmio_mmc_host_remove(host);
136 if (cell->disable)
137 cell->disable(pdev);
138 }
139
140 return 0;
141}
142
143/* ------------------- device registration ----------------------- */
144
145static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
146 SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
147 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
148 tmio_mmc_host_runtime_resume,
149 NULL)
150};
151
152static struct platform_driver tmio_mmc_driver = {
153 .driver = {
154 .name = "tmio-mmc",
155 .pm = &tmio_mmc_dev_pm_ops,
156 },
157 .probe = tmio_mmc_probe,
158 .remove = tmio_mmc_remove,
159};
160
161module_platform_driver(tmio_mmc_driver);
162
163MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
164MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
165MODULE_LICENSE("GPL v2");
166MODULE_ALIAS("platform:tmio-mmc");
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
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>
21
22#include "tmio_mmc.h"
23
24/* 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
29static 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{
53 unsigned int divisor;
54 u32 clk = 0;
55 int clk_sel;
56
57 if (new_clock == 0) {
58 tmio_mmc_clk_stop(host);
59 return;
60 }
61
62 divisor = host->pdata->hclk / new_clock;
63
64 /* 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);
67
68 host->pdata->set_clk_div(host->pdev, clk_sel);
69
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
78static 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
94#ifdef CONFIG_PM_SLEEP
95static int tmio_mmc_suspend(struct device *dev)
96{
97 struct platform_device *pdev = to_platform_device(dev);
98 const struct mfd_cell *cell = mfd_get_cell(pdev);
99 int ret;
100
101 ret = pm_runtime_force_suspend(dev);
102
103 /* Tell MFD core it can disable us now.*/
104 if (!ret && cell->disable)
105 cell->disable(pdev);
106
107 return ret;
108}
109
110static int tmio_mmc_resume(struct device *dev)
111{
112 struct platform_device *pdev = to_platform_device(dev);
113 const struct mfd_cell *cell = mfd_get_cell(pdev);
114 int ret = 0;
115
116 /* Tell the MFD core we are ready to be enabled */
117 if (cell->resume)
118 ret = cell->resume(pdev);
119
120 if (!ret)
121 ret = pm_runtime_force_resume(dev);
122
123 return ret;
124}
125#endif
126
127static int tmio_mmc_probe(struct platform_device *pdev)
128{
129 const struct mfd_cell *cell = mfd_get_cell(pdev);
130 struct tmio_mmc_data *pdata;
131 struct tmio_mmc_host *host;
132 struct resource *res;
133 int ret = -EINVAL, irq;
134
135 if (pdev->num_resources != 2)
136 goto out;
137
138 pdata = pdev->dev.platform_data;
139 if (!pdata || !pdata->hclk)
140 goto out;
141
142 irq = platform_get_irq(pdev, 0);
143 if (irq < 0) {
144 ret = irq;
145 goto out;
146 }
147
148 /* Tell the MFD core we are ready to be enabled */
149 if (cell->enable) {
150 ret = cell->enable(pdev);
151 if (ret)
152 goto out;
153 }
154
155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
156 if (!res) {
157 ret = -EINVAL;
158 goto cell_disable;
159 }
160
161 host = tmio_mmc_host_alloc(pdev, pdata);
162 if (IS_ERR(host)) {
163 ret = PTR_ERR(host);
164 goto cell_disable;
165 }
166
167 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
168 host->bus_shift = resource_size(res) >> 10;
169 host->set_clock = tmio_mmc_set_clock;
170 host->reset = tmio_mmc_reset;
171
172 host->mmc->f_max = pdata->hclk;
173 host->mmc->f_min = pdata->hclk / 512;
174
175 ret = tmio_mmc_host_probe(host);
176 if (ret)
177 goto host_free;
178
179 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq,
180 IRQF_TRIGGER_FALLING,
181 dev_name(&pdev->dev), host);
182 if (ret)
183 goto host_remove;
184
185 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
186 (unsigned long)host->ctl, irq);
187
188 return 0;
189
190host_remove:
191 tmio_mmc_host_remove(host);
192host_free:
193 tmio_mmc_host_free(host);
194cell_disable:
195 if (cell->disable)
196 cell->disable(pdev);
197out:
198 return ret;
199}
200
201static int tmio_mmc_remove(struct platform_device *pdev)
202{
203 const struct mfd_cell *cell = mfd_get_cell(pdev);
204 struct tmio_mmc_host *host = platform_get_drvdata(pdev);
205
206 tmio_mmc_host_remove(host);
207 if (cell->disable)
208 cell->disable(pdev);
209
210 return 0;
211}
212
213/* ------------------- device registration ----------------------- */
214
215static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
216 SET_SYSTEM_SLEEP_PM_OPS(tmio_mmc_suspend, tmio_mmc_resume)
217 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
218 tmio_mmc_host_runtime_resume, NULL)
219};
220
221static struct platform_driver tmio_mmc_driver = {
222 .driver = {
223 .name = "tmio-mmc",
224 .pm = &tmio_mmc_dev_pm_ops,
225 },
226 .probe = tmio_mmc_probe,
227 .remove = tmio_mmc_remove,
228};
229
230module_platform_driver(tmio_mmc_driver);
231
232MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
233MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
234MODULE_LICENSE("GPL v2");
235MODULE_ALIAS("platform:tmio-mmc");