blob: 49edff7fee49bc2b48f924d6e17469a140d416e8 [file] [log] [blame]
Magnus Damma87d5632009-10-02 02:22:09 +00001/*
2 * SuperH Mobile SDHI
3 *
Wolfram Sanga72e8b12016-02-15 16:01:48 +01004 * Copyright (C) 2016 Sang Engineering, Wolfram Sang
5 * Copyright (C) 2015-16 Renesas Electronics Corporation
Magnus Damma87d5632009-10-02 02:22:09 +00006 * Copyright (C) 2009 Magnus Damm
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Based on "Compaq ASIC3 support":
13 *
14 * Copyright 2001 Compaq Computer Corporation.
15 * Copyright 2004-2005 Phil Blundell
16 * Copyright 2007-2008 OpenedHand Ltd.
17 *
18 * Authors: Phil Blundell <pb@handhelds.org>,
19 * Samuel Ortiz <sameo@openedhand.com>
20 *
21 */
22
23#include <linux/kernel.h>
24#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Guennadi Liakhovetskic7bb4482012-05-01 18:03:43 +020026#include <linux/mod_devicetable.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040027#include <linux/module.h>
Guennadi Liakhovetski5a00a972013-02-15 16:13:56 +010028#include <linux/of_device.h>
Magnus Damma87d5632009-10-02 02:22:09 +000029#include <linux/platform_device.h>
Magnus Damm3c49e812010-02-22 13:37:09 +090030#include <linux/mmc/host.h>
Magnus Damma87d5632009-10-02 02:22:09 +000031#include <linux/mfd/tmio.h>
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +000032#include <linux/sh_dma.h>
Simon Horman973ed3a2011-06-21 08:00:10 +090033#include <linux/delay.h>
Wolfram Sang057a4592016-04-01 17:44:37 +020034#include <linux/pinctrl/consumer.h>
35#include <linux/pinctrl/pinctrl-state.h>
36#include <linux/regulator/consumer.h>
Magnus Damma87d5632009-10-02 02:22:09 +000037
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +010038#include "tmio_mmc.h"
39
Kuninori Morimotoe3c418f2013-11-20 00:31:13 -080040#define EXT_ACC 0xe4
41
Wolfram Sanga2a16c72016-05-11 14:46:00 +020042#define SDHI_VER_GEN2_SDR50 0x490c
43/* very old datasheets said 0x490c for SDR104, too. They are wrong! */
44#define SDHI_VER_GEN2_SDR104 0xcb0d
45#define SDHI_VER_GEN3_SD 0xcc10
46#define SDHI_VER_GEN3_SDMMC 0xcd10
47
Kuninori Morimoto16935252015-01-13 04:59:43 +000048#define host_to_priv(host) container_of((host)->pdata, struct sh_mobile_sdhi, mmc_data)
49
Guennadi Liakhovetski5a00a972013-02-15 16:13:56 +010050struct sh_mobile_sdhi_of_data {
51 unsigned long tmio_flags;
Kuninori Morimotob3a5d4c2014-02-20 16:54:58 -080052 unsigned long capabilities;
Kuninori Morimoto423f6c22014-02-20 16:55:27 -080053 unsigned long capabilities2;
Kuninori Morimotof45394d2015-01-13 05:00:07 +000054 enum dma_slave_buswidth dma_buswidth;
Kuninori Morimoto384b2cb2014-08-24 19:58:48 -070055 dma_addr_t dma_rx_offset;
Wolfram Sanga72e8b12016-02-15 16:01:48 +010056 unsigned bus_shift;
Guennadi Liakhovetski5a00a972013-02-15 16:13:56 +010057};
58
Wolfram Sang13bbd8a2016-04-26 17:55:22 +020059static const struct sh_mobile_sdhi_of_data of_default_cfg = {
60 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
Guennadi Liakhovetski5a00a972013-02-15 16:13:56 +010061};
62
Kuninori Morimotob3a5d4c2014-02-20 16:54:58 -080063static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = {
Shinobu Ueharada29fe22014-08-24 20:03:00 -070064 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
65 TMIO_MMC_CLK_ACTUAL,
Kuninori Morimotob3a5d4c2014-02-20 16:54:58 -080066 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
67};
68
Kuninori Morimoto423f6c22014-02-20 16:55:27 -080069static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = {
Shinobu Ueharada29fe22014-08-24 20:03:00 -070070 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
Wolfram Sang3d376fb2016-05-02 22:25:40 +020071 TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2,
Kuninori Morimoto423f6c22014-02-20 16:55:27 -080072 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
Kuninori Morimotof45394d2015-01-13 05:00:07 +000073 .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
Kuninori Morimoto384b2cb2014-08-24 19:58:48 -070074 .dma_rx_offset = 0x2000,
Kuninori Morimoto423f6c22014-02-20 16:55:27 -080075};
76
Wolfram Sanga72e8b12016-02-15 16:01:48 +010077static const struct sh_mobile_sdhi_of_data of_rcar_gen3_compatible = {
78 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
Wolfram Sang3d376fb2016-05-02 22:25:40 +020079 TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2,
Wolfram Sang685d29e2016-05-09 17:01:07 +020080 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
Wolfram Sanga72e8b12016-02-15 16:01:48 +010081 .bus_shift = 2,
82};
83
Kuninori Morimoto2772ef32014-02-20 16:54:44 -080084static const struct of_device_id sh_mobile_sdhi_of_match[] = {
85 { .compatible = "renesas,sdhi-shmobile" },
Wolfram Sang13bbd8a2016-04-26 17:55:22 +020086 { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
87 { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
88 { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
Kuninori Morimotob3a5d4c2014-02-20 16:54:58 -080089 { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
Kuninori Morimoto81bbbc72014-02-20 16:55:12 -080090 { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
Kuninori Morimoto423f6c22014-02-20 16:55:27 -080091 { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
Kuninori Morimoto81918d22014-02-20 16:55:40 -080092 { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
Geert Uytterhoevena6386402014-08-28 10:07:19 +020093 { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
94 { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
95 { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
Wolfram Sanga72e8b12016-02-15 16:01:48 +010096 { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
Ai Kyuse7428e0b2016-09-06 12:38:38 +020097 { .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_gen3_compatible, },
Kuninori Morimoto2772ef32014-02-20 16:54:44 -080098 {},
99};
100MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match);
101
Magnus Damma87d5632009-10-02 02:22:09 +0000102struct sh_mobile_sdhi {
103 struct clk *clk;
104 struct tmio_mmc_data mmc_data;
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000105 struct tmio_mmc_dma dma_priv;
Wolfram Sang057a4592016-04-01 17:44:37 +0200106 struct pinctrl *pinctrl;
107 struct pinctrl_state *pins_default, *pins_uhs;
Magnus Damma87d5632009-10-02 02:22:09 +0000108};
109
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000110static void sh_mobile_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width)
111{
112 u32 val;
113
114 /*
115 * see also
116 * sh_mobile_sdhi_of_data :: dma_buswidth
117 */
118 switch (sd_ctrl_read16(host, CTL_VERSION)) {
Wolfram Sanga2a16c72016-05-11 14:46:00 +0200119 case SDHI_VER_GEN2_SDR50:
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000120 val = (width == 32) ? 0x0001 : 0x0000;
121 break;
Wolfram Sanga2a16c72016-05-11 14:46:00 +0200122 case SDHI_VER_GEN2_SDR104:
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000123 val = (width == 32) ? 0x0000 : 0x0001;
124 break;
Wolfram Sanga2a16c72016-05-11 14:46:00 +0200125 case SDHI_VER_GEN3_SD:
126 case SDHI_VER_GEN3_SDMMC:
Wolfram Sanga72e8b12016-02-15 16:01:48 +0100127 if (width == 64)
128 val = 0x0000;
129 else if (width == 32)
130 val = 0x0101;
131 else
132 val = 0x0001;
133 break;
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000134 default:
135 /* nothing to do */
136 return;
137 }
138
139 sd_ctrl_write16(host, EXT_ACC, val);
140}
141
Ben Hutchings0ea28212016-04-01 17:44:31 +0200142static int sh_mobile_sdhi_clk_enable(struct tmio_mmc_host *host)
Guennadi Liakhovetski56c49282012-06-20 19:10:32 +0200143{
Ben Hutchings0ea28212016-04-01 17:44:31 +0200144 struct mmc_host *mmc = host->mmc;
Kuninori Morimoto16935252015-01-13 04:59:43 +0000145 struct sh_mobile_sdhi *priv = host_to_priv(host);
Laurent Pinchart00fb3d22013-10-28 23:49:25 +0100146 int ret = clk_prepare_enable(priv->clk);
Guennadi Liakhovetski56c49282012-06-20 19:10:32 +0200147 if (ret < 0)
148 return ret;
149
Ben Hutchings2fb55952016-04-01 17:44:32 +0200150 /*
151 * The clock driver may not know what maximum frequency
152 * actually works, so it should be set with the max-frequency
153 * property which will already have been read to f_max. If it
154 * was missing, assume the current frequency is the maximum.
155 */
156 if (!mmc->f_max)
157 mmc->f_max = clk_get_rate(priv->clk);
158
159 /*
160 * Minimum frequency is the minimum input clock frequency
161 * divided by our maximum divider.
162 */
163 mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L);
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000164
165 /* enable 16bit data access on SDBUF as default */
166 sh_mobile_sdhi_sdbuf_width(host, 16);
167
Guennadi Liakhovetski56c49282012-06-20 19:10:32 +0200168 return 0;
169}
170
Ben Hutchings2fb55952016-04-01 17:44:32 +0200171static unsigned int sh_mobile_sdhi_clk_update(struct tmio_mmc_host *host,
172 unsigned int new_clock)
173{
174 struct sh_mobile_sdhi *priv = host_to_priv(host);
Wolfram Sang3072ba82016-05-02 22:25:39 +0200175 unsigned int freq, diff, best_freq = 0, diff_min = ~0;
Wolfram Sangf3f44d52016-05-02 22:25:42 +0200176 int i, ret;
Ben Hutchings2fb55952016-04-01 17:44:32 +0200177
Wolfram Sang8fc00992016-05-02 22:25:41 +0200178 /* tested only on RCar Gen2+ currently; may work for others */
179 if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
180 return clk_get_rate(priv->clk);
181
Ben Hutchings2fb55952016-04-01 17:44:32 +0200182 /*
183 * We want the bus clock to be as close as possible to, but no
184 * greater than, new_clock. As we can divide by 1 << i for
185 * any i in [0, 9] we want the input clock to be as close as
186 * possible, but no greater than, new_clock << i.
187 */
188 for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) {
189 freq = clk_round_rate(priv->clk, new_clock << i);
190 if (freq > (new_clock << i)) {
191 /* Too fast; look for a slightly slower option */
192 freq = clk_round_rate(priv->clk,
193 (new_clock << i) / 4 * 3);
194 if (freq > (new_clock << i))
195 continue;
196 }
197
198 diff = new_clock - (freq >> i);
199 if (diff <= diff_min) {
200 best_freq = freq;
201 diff_min = diff;
202 }
203 }
204
Wolfram Sangf3f44d52016-05-02 22:25:42 +0200205 ret = clk_set_rate(priv->clk, best_freq);
Ben Hutchings2fb55952016-04-01 17:44:32 +0200206
Wolfram Sangf3f44d52016-05-02 22:25:42 +0200207 return ret == 0 ? best_freq : clk_get_rate(priv->clk);
Ben Hutchings2fb55952016-04-01 17:44:32 +0200208}
209
Ben Hutchings0ea28212016-04-01 17:44:31 +0200210static void sh_mobile_sdhi_clk_disable(struct tmio_mmc_host *host)
Guennadi Liakhovetski56c49282012-06-20 19:10:32 +0200211{
Kuninori Morimoto16935252015-01-13 04:59:43 +0000212 struct sh_mobile_sdhi *priv = host_to_priv(host);
Ben Hutchings0ea28212016-04-01 17:44:31 +0200213
Laurent Pinchart00fb3d22013-10-28 23:49:25 +0100214 clk_disable_unprepare(priv->clk);
Guennadi Liakhovetski56c49282012-06-20 19:10:32 +0200215}
216
Wolfram Sang6a4679f2016-08-24 11:34:37 +0200217static int sh_mobile_sdhi_card_busy(struct mmc_host *mmc)
218{
219 struct tmio_mmc_host *host = mmc_priv(mmc);
220
221 return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_DAT0);
222}
223
Wolfram Sang057a4592016-04-01 17:44:37 +0200224static int sh_mobile_sdhi_start_signal_voltage_switch(struct mmc_host *mmc,
225 struct mmc_ios *ios)
226{
227 struct tmio_mmc_host *host = mmc_priv(mmc);
228 struct sh_mobile_sdhi *priv = host_to_priv(host);
229 struct pinctrl_state *pin_state;
230 int ret;
231
232 switch (ios->signal_voltage) {
233 case MMC_SIGNAL_VOLTAGE_330:
234 pin_state = priv->pins_default;
235 break;
236 case MMC_SIGNAL_VOLTAGE_180:
237 pin_state = priv->pins_uhs;
238 break;
239 default:
240 return -EINVAL;
241 }
242
243 /*
244 * If anything is missing, assume signal voltage is fixed at
245 * 3.3V and succeed/fail accordingly.
246 */
247 if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state))
248 return ios->signal_voltage ==
249 MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL;
250
251 ret = mmc_regulator_set_vqmmc(host->mmc, ios);
252 if (ret)
253 return ret;
254
Wolfram Sang2272c842016-04-26 17:55:27 +0200255 return pinctrl_select_state(priv->pinctrl, pin_state);
Wolfram Sang057a4592016-04-01 17:44:37 +0200256}
257
Simon Horman973ed3a2011-06-21 08:00:10 +0900258static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host)
259{
260 int timeout = 1000;
261
Wolfram Sanga21553c2016-04-27 18:51:26 +0200262 while (--timeout && !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS)
263 & TMIO_STAT_SCLKDIVEN))
Simon Horman973ed3a2011-06-21 08:00:10 +0900264 udelay(1);
265
266 if (!timeout) {
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000267 dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n");
Simon Horman973ed3a2011-06-21 08:00:10 +0900268 return -EBUSY;
269 }
270
271 return 0;
272}
273
274static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr)
275{
276 switch (addr)
277 {
278 case CTL_SD_CMD:
279 case CTL_STOP_INTERNAL_ACTION:
280 case CTL_XFER_BLK_COUNT:
281 case CTL_SD_CARD_CLK_CTL:
282 case CTL_SD_XFER_LEN:
283 case CTL_SD_MEM_CARD_OPT:
284 case CTL_TRANSACTION_CTL:
285 case CTL_DMA_ENABLE:
Shinobu Ueharaff741cf2014-04-25 18:14:17 +0900286 case EXT_ACC:
Simon Horman973ed3a2011-06-21 08:00:10 +0900287 return sh_mobile_sdhi_wait_idle(host);
288 }
289
290 return 0;
291}
292
Kuninori Morimoto8b4efe22014-09-08 23:45:46 -0700293static int sh_mobile_sdhi_multi_io_quirk(struct mmc_card *card,
294 unsigned int direction, int blk_size)
295{
296 /*
297 * In Renesas controllers, when performing a
298 * multiple block read of one or two blocks,
299 * depending on the timing with which the
300 * response register is read, the response
301 * value may not be read properly.
302 * Use single block read for this HW bug
303 */
304 if ((direction == MMC_DATA_READ) &&
305 blk_size == 2)
306 return 1;
307
308 return blk_size;
309}
310
Kuninori Morimoto0c47f6a2015-01-13 04:59:56 +0000311static void sh_mobile_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable)
312{
313 sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0);
Kuninori Morimotof45394d2015-01-13 05:00:07 +0000314
315 /* enable 32bit access if DMA mode if possibile */
316 sh_mobile_sdhi_sdbuf_width(host, enable ? 32 : 16);
Kuninori Morimoto0c47f6a2015-01-13 04:59:56 +0000317}
318
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500319static int sh_mobile_sdhi_probe(struct platform_device *pdev)
Magnus Damma87d5632009-10-02 02:22:09 +0000320{
Guennadi Liakhovetski5a00a972013-02-15 16:13:56 +0100321 const struct of_device_id *of_id =
322 of_match_device(sh_mobile_sdhi_of_match, &pdev->dev);
Magnus Damma87d5632009-10-02 02:22:09 +0000323 struct sh_mobile_sdhi *priv;
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000324 struct tmio_mmc_data *mmc_data;
Kuninori Morimotof33c9d62015-02-24 02:06:43 +0000325 struct tmio_mmc_data *mmd = pdev->dev.platform_data;
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100326 struct tmio_mmc_host *host;
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800327 struct resource *res;
Simon Hormand5098cb2011-08-26 10:42:39 +0200328 int irq, ret, i = 0;
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200329 struct tmio_mmc_dma *dma_priv;
Magnus Damma87d5632009-10-02 02:22:09 +0000330
Kuninori Morimoto3b159a62013-11-20 00:30:55 -0800331 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
332 if (!res)
333 return -EINVAL;
334
Guennadi Liakhovetskiac51b962013-02-15 16:13:58 +0100335 priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
Wolfram Sang2bf8ab62016-01-19 11:42:22 +0100336 if (!priv)
Magnus Damma87d5632009-10-02 02:22:09 +0000337 return -ENOMEM;
Magnus Damma87d5632009-10-02 02:22:09 +0000338
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000339 mmc_data = &priv->mmc_data;
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200340 dma_priv = &priv->dma_priv;
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000341
Guennadi Liakhovetskiac51b962013-02-15 16:13:58 +0100342 priv->clk = devm_clk_get(&pdev->dev, NULL);
Magnus Damma87d5632009-10-02 02:22:09 +0000343 if (IS_ERR(priv->clk)) {
Magnus Damma87d5632009-10-02 02:22:09 +0000344 ret = PTR_ERR(priv->clk);
Guennadi Liakhovetski56ae1ad2012-11-28 10:24:21 +0100345 dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
Kuninori Morimoto010f4aa2015-01-13 04:59:24 +0000346 goto eprobe;
Magnus Damma87d5632009-10-02 02:22:09 +0000347 }
348
Wolfram Sang057a4592016-04-01 17:44:37 +0200349 priv->pinctrl = devm_pinctrl_get(&pdev->dev);
350 if (!IS_ERR(priv->pinctrl)) {
351 priv->pins_default = pinctrl_lookup_state(priv->pinctrl,
352 PINCTRL_STATE_DEFAULT);
353 priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl,
354 "state_uhs");
355 }
356
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000357 host = tmio_mmc_host_alloc(pdev);
358 if (!host) {
359 ret = -ENOMEM;
360 goto eprobe;
361 }
362
Wolfram Sanga72e8b12016-02-15 16:01:48 +0100363 if (of_id && of_id->data) {
364 const struct sh_mobile_sdhi_of_data *of_data = of_id->data;
365
366 mmc_data->flags |= of_data->tmio_flags;
367 mmc_data->capabilities |= of_data->capabilities;
368 mmc_data->capabilities2 |= of_data->capabilities2;
369 mmc_data->dma_rx_offset = of_data->dma_rx_offset;
370 dma_priv->dma_buswidth = of_data->dma_buswidth;
371 host->bus_shift = of_data->bus_shift;
372 }
373
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000374 host->dma = dma_priv;
Kuninori Morimotodfe9a222015-01-13 04:57:42 +0000375 host->write16_hook = sh_mobile_sdhi_write16_hook;
Kuninori Morimoto4fe2ec52015-01-13 04:57:52 +0000376 host->clk_enable = sh_mobile_sdhi_clk_enable;
Ben Hutchings2fb55952016-04-01 17:44:32 +0200377 host->clk_update = sh_mobile_sdhi_clk_update;
Kuninori Morimoto00452c12015-01-13 04:58:01 +0000378 host->clk_disable = sh_mobile_sdhi_clk_disable;
Kuninori Morimoto85c02dd2015-01-13 04:58:10 +0000379 host->multi_io_quirk = sh_mobile_sdhi_multi_io_quirk;
Wolfram Sangff026092016-08-24 11:34:38 +0200380
381 /* SDR speeds are only available on Gen2+ */
382 if (mmc_data->flags & TMIO_MMC_MIN_RCAR2) {
383 /* card_busy caused issues on r8a73a4 (pre-Gen2) CD-less SDHI */
384 host->card_busy = sh_mobile_sdhi_card_busy;
385 host->start_signal_voltage_switch =
386 sh_mobile_sdhi_start_signal_voltage_switch;
387 }
Wolfram Sanga72e8b12016-02-15 16:01:48 +0100388
389 /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */
390 if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */
Kuninori Morimoto95a7dc32015-01-13 04:59:33 +0000391 host->bus_shift = 1;
Kuninori Morimoto7ecc09b2015-01-13 04:57:33 +0000392
Kuninori Morimoto84f11d52015-02-24 02:07:07 +0000393 if (mmd)
Kuninori Morimotof33c9d62015-02-24 02:06:43 +0000394 *mmc_data = *mmd;
Kuninori Morimoto84f11d52015-02-24 02:07:07 +0000395
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200396 dma_priv->filter = shdma_chan_filter;
Kuninori Morimoto0c47f6a2015-01-13 04:59:56 +0000397 dma_priv->enable = sh_mobile_sdhi_enable_dma;
Guennadi Liakhovetski87ae7bb2013-04-26 17:47:19 +0200398
Kuninori Morimotoe471df02015-01-13 04:58:46 +0000399 mmc_data->alignment_shift = 1; /* 2-byte alignment */
Kuninori Morimotof33c9d62015-02-24 02:06:43 +0000400 mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED;
Kuninori Morimotoe471df02015-01-13 04:58:46 +0000401
Yusuke Godaf1334fb2010-08-30 11:50:19 +0100402 /*
403 * All SDHI blocks support 2-byte and larger block sizes in 4-bit
404 * bus width mode.
405 */
406 mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES;
407
Arnd Hannemann23b66072010-12-28 23:22:32 +0100408 /*
409 * All SDHI blocks support SDIO IRQ signalling.
410 */
411 mmc_data->flags |= TMIO_MMC_SDIO_IRQ;
412
Shinobu Ueharab8d11962014-08-24 20:00:25 -0700413 /*
414 * All SDHI have CMD12 controll bit
415 */
416 mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL;
417
Shinobu Uehara6b987572014-08-24 20:00:52 -0700418 /*
419 * All SDHI need SDIO_INFO1 reserved bit
420 */
421 mmc_data->flags |= TMIO_MMC_SDIO_STATUS_QUIRK;
422
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000423 ret = tmio_mmc_host_probe(host, mmc_data);
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100424 if (ret < 0)
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000425 goto efree;
Magnus Damma87d5632009-10-02 02:22:09 +0000426
Wolfram Sangadcbc942016-04-26 17:55:23 +0200427 while (1) {
428 irq = platform_get_irq(pdev, i);
429 if (irq < 0)
430 break;
431 i++;
432 ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0,
Magnus Dammd6a1f862011-05-06 11:02:45 +0000433 dev_name(&pdev->dev), host);
Simon Hormand5098cb2011-08-26 10:42:39 +0200434 if (ret)
Guennadi Liakhovetskiac51b962013-02-15 16:13:58 +0100435 goto eirq;
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000436 }
Simon Hormand5098cb2011-08-26 10:42:39 +0200437
Wolfram Sangadcbc942016-04-26 17:55:23 +0200438 /* There must be at least one IRQ source */
439 if (!i) {
Simon Hormand5098cb2011-08-26 10:42:39 +0200440 ret = irq;
Guennadi Liakhovetskiac51b962013-02-15 16:13:58 +0100441 goto eirq;
Simon Hormand5098cb2011-08-26 10:42:39 +0200442 }
443
Ben Hutchings2fb55952016-04-01 17:44:32 +0200444 dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n",
Magnus Damm1f7d6812011-05-06 11:02:41 +0000445 mmc_hostname(host->mmc), (unsigned long)
Guennadi Liakhovetski58126c82012-02-09 22:57:10 +0100446 (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start),
Guennadi Liakhovetski369213b2012-11-28 10:24:13 +0100447 host->mmc->f_max / 1000000);
Magnus Damma87d5632009-10-02 02:22:09 +0000448
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100449 return ret;
Magnus Damma87d5632009-10-02 02:22:09 +0000450
Guennadi Liakhovetskiac51b962013-02-15 16:13:58 +0100451eirq:
Magnus Damm8e7bfdb2011-05-06 11:02:33 +0000452 tmio_mmc_host_remove(host);
Kuninori Morimoto94b110a2015-01-13 04:57:22 +0000453efree:
454 tmio_mmc_host_free(host);
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100455eprobe:
Magnus Damma87d5632009-10-02 02:22:09 +0000456 return ret;
457}
458
459static int sh_mobile_sdhi_remove(struct platform_device *pdev)
460{
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100461 struct mmc_host *mmc = platform_get_drvdata(pdev);
462 struct tmio_mmc_host *host = mmc_priv(mmc);
Magnus Damma87d5632009-10-02 02:22:09 +0000463
Guennadi Liakhovetski742a0c72011-05-27 16:54:05 +0200464 tmio_mmc_host_remove(host);
465
Magnus Damma87d5632009-10-02 02:22:09 +0000466 return 0;
467}
468
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000469static const struct dev_pm_ops tmio_mmc_dev_pm_ops = {
Ulf Hansson753a6882014-08-25 12:18:26 +0200470 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
471 pm_runtime_force_resume)
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +0100472 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
Ulf Hansson4e262d72013-10-23 14:51:31 +0200473 tmio_mmc_host_runtime_resume,
474 NULL)
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000475};
476
Magnus Damma87d5632009-10-02 02:22:09 +0000477static struct platform_driver sh_mobile_sdhi_driver = {
478 .driver = {
479 .name = "sh_mobile_sdhi",
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000480 .pm = &tmio_mmc_dev_pm_ops,
Guennadi Liakhovetskic7bb4482012-05-01 18:03:43 +0200481 .of_match_table = sh_mobile_sdhi_of_match,
Magnus Damma87d5632009-10-02 02:22:09 +0000482 },
483 .probe = sh_mobile_sdhi_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500484 .remove = sh_mobile_sdhi_remove,
Magnus Damma87d5632009-10-02 02:22:09 +0000485};
486
Axel Lind1f81a62011-11-26 12:55:43 +0800487module_platform_driver(sh_mobile_sdhi_driver);
Magnus Damma87d5632009-10-02 02:22:09 +0000488
489MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
490MODULE_AUTHOR("Magnus Damm");
491MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski42051e82011-03-14 09:52:33 +0100492MODULE_ALIAS("platform:sh_mobile_sdhi");