blob: e13b5c3b074081d647e0dea5d44fb5c3b587a52e [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Denis Karpovd900f712009-09-22 16:44:38 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010022#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/clk.h>
29#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070030#include <linux/mmc/core.h>
Adrian Hunter93caf8e2010-08-11 14:17:48 -070031#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/io.h>
33#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080034#include <linux/gpio.h>
35#include <linux/regulator/consumer.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070038#include <plat/board.h>
39#include <plat/mmc.h>
40#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041
42/* OMAP HSMMC Host Controller Registers */
43#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070044#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045#define OMAP_HSMMC_CON 0x002C
46#define OMAP_HSMMC_BLK 0x0104
47#define OMAP_HSMMC_ARG 0x0108
48#define OMAP_HSMMC_CMD 0x010C
49#define OMAP_HSMMC_RSP10 0x0110
50#define OMAP_HSMMC_RSP32 0x0114
51#define OMAP_HSMMC_RSP54 0x0118
52#define OMAP_HSMMC_RSP76 0x011C
53#define OMAP_HSMMC_DATA 0x0120
54#define OMAP_HSMMC_HCTL 0x0128
55#define OMAP_HSMMC_SYSCTL 0x012C
56#define OMAP_HSMMC_STAT 0x0130
57#define OMAP_HSMMC_IE 0x0134
58#define OMAP_HSMMC_ISE 0x0138
59#define OMAP_HSMMC_CAPA 0x0140
60
61#define VS18 (1 << 26)
62#define VS30 (1 << 25)
63#define SDVS18 (0x5 << 9)
64#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080065#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010066#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define SDVSCLR 0xFFFFF1FF
68#define SDVSDET 0x00000400
69#define AUTOIDLE 0x1
70#define SDBP (1 << 8)
71#define DTO 0xe
72#define ICE 0x1
73#define ICS 0x2
74#define CEN (1 << 2)
75#define CLKD_MASK 0x0000FFC0
76#define CLKD_SHIFT 6
77#define DTO_MASK 0x000F0000
78#define DTO_SHIFT 16
79#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070080#define BWR_ENABLE (1 << 4)
81#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e2010-08-11 14:17:48 -070082#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define INIT_STREAM (1 << 1)
84#define DP_SELECT (1 << 21)
85#define DDIR (1 << 4)
86#define DMA_EN 0x1
87#define MSBS (1 << 5)
88#define BCE (1 << 1)
89#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020090#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define CC 0x1
92#define TC 0x02
93#define OD 0x1
94#define ERR (1 << 15)
95#define CMD_TIMEOUT (1 << 16)
96#define DATA_TIMEOUT (1 << 20)
97#define CMD_CRC (1 << 17)
98#define DATA_CRC (1 << 21)
99#define CARD_ERR (1 << 28)
100#define STAT_CLEAR 0xFFFFFFFF
101#define INIT_STREAM_CMD 0x00000000
102#define DUAL_VOLT_OCR_BIT 7
103#define SRC (1 << 25)
104#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700105#define SOFTRESET (1 << 1)
106#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100107
108/*
109 * FIXME: Most likely all the data using these _DEVID defines should come
110 * from the platform_data, or implemented in controller and slot specific
111 * functions.
112 */
113#define OMAP_MMC1_DEVID 0
114#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000115#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700116#define OMAP_MMC4_DEVID 3
117#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119#define MMC_TIMEOUT_MS 20
120#define OMAP_MMC_MASTER_CLOCK 96000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530121#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100122
Denis Karpovdd498ef2009-09-22 16:44:49 -0700123/* Timeouts for entering power saving states on inactivity, msec */
124#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700125#define OMAP_MMC_SLEEP_TIMEOUT 1000
126#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100128/*
129 * One controller can have multiple slots, like on some omap boards using
130 * omap.c controller driver. Luckily this is not currently done on any known
131 * omap_hsmmc.c device.
132 */
133#define mmc_slot(host) (host->pdata->slots[host->slot_id])
134
135/*
136 * MMC Host controller read/write API's
137 */
138#define OMAP_HSMMC_READ(base, reg) \
139 __raw_readl((base) + OMAP_HSMMC_##reg)
140
141#define OMAP_HSMMC_WRITE(base, reg, val) \
142 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
143
Denis Karpov70a33412009-09-22 16:44:59 -0700144struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100145 struct device *dev;
146 struct mmc_host *mmc;
147 struct mmc_request *mrq;
148 struct mmc_command *cmd;
149 struct mmc_data *data;
150 struct clk *fclk;
151 struct clk *iclk;
152 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800153 /*
154 * vcc == configured supply
155 * vcc_aux == optional
156 * - MMC1, supply for DAT4..DAT7
157 * - MMC2/MMC2, external level shifter voltage supply, for
158 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
159 */
160 struct regulator *vcc;
161 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 struct work_struct mmc_carddetect_work;
163 void __iomem *base;
164 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700165 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 unsigned int id;
167 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200168 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700170 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 u32 *buffer;
172 u32 bytesleft;
173 int suspended;
174 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000176 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700178 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200179 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700181 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700182 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700183 int protect_card;
184 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800185 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700186 int req_in_progress;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700187
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 struct omap_mmc_platform_data *pdata;
189};
190
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800191static int omap_hsmmc_card_detect(struct device *dev, int slot)
192{
193 struct omap_mmc_platform_data *mmc = dev->platform_data;
194
195 /* NOTE: assumes card detect signal is active-low */
196 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
197}
198
199static int omap_hsmmc_get_wp(struct device *dev, int slot)
200{
201 struct omap_mmc_platform_data *mmc = dev->platform_data;
202
203 /* NOTE: assumes write protect signal is active-high */
204 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
205}
206
207static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
208{
209 struct omap_mmc_platform_data *mmc = dev->platform_data;
210
211 /* NOTE: assumes card detect signal is active-low */
212 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
213}
214
215#ifdef CONFIG_PM
216
217static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
218{
219 struct omap_mmc_platform_data *mmc = dev->platform_data;
220
221 disable_irq(mmc->slots[0].card_detect_irq);
222 return 0;
223}
224
225static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
226{
227 struct omap_mmc_platform_data *mmc = dev->platform_data;
228
229 enable_irq(mmc->slots[0].card_detect_irq);
230 return 0;
231}
232
233#else
234
235#define omap_hsmmc_suspend_cdirq NULL
236#define omap_hsmmc_resume_cdirq NULL
237
238#endif
239
Adrian Hunterb702b102010-02-15 10:03:35 -0800240#ifdef CONFIG_REGULATOR
241
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
243 int vdd)
244{
245 struct omap_hsmmc_host *host =
246 platform_get_drvdata(to_platform_device(dev));
247 int ret;
248
249 if (mmc_slot(host).before_set_reg)
250 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
251
252 if (power_on)
Linus Walleij99fc5132010-09-29 01:08:27 -0400253 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800254 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400255 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256
257 if (mmc_slot(host).after_set_reg)
258 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
259
260 return ret;
261}
262
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500263static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800264 int vdd)
265{
266 struct omap_hsmmc_host *host =
267 platform_get_drvdata(to_platform_device(dev));
268 int ret = 0;
269
270 /*
271 * If we don't see a Vcc regulator, assume it's a fixed
272 * voltage always-on regulator.
273 */
274 if (!host->vcc)
275 return 0;
276
277 if (mmc_slot(host).before_set_reg)
278 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
279
280 /*
281 * Assume Vcc regulator is used only to power the card ... OMAP
282 * VDDS is used to power the pins, optionally with a transceiver to
283 * support cards using voltages other than VDDS (1.8V nominal). When a
284 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
285 *
286 * In some cases this regulator won't support enable/disable;
287 * e.g. it's a fixed rail for a WLAN chip.
288 *
289 * In other cases vcc_aux switches interface power. Example, for
290 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
291 * chips/cards need an interface voltage rail too.
292 */
293 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400294 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800295 /* Enable interface voltage rail, if needed */
296 if (ret == 0 && host->vcc_aux) {
297 ret = regulator_enable(host->vcc_aux);
298 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400299 ret = mmc_regulator_set_ocr(host->mmc,
300 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 }
302 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400303 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800304 if (host->vcc_aux)
305 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400306 if (!ret) {
307 /* Then proceed to shut down the local regulator */
308 ret = mmc_regulator_set_ocr(host->mmc,
309 host->vcc, 0);
310 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311 }
312
313 if (mmc_slot(host).after_set_reg)
314 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
315
316 return ret;
317}
318
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500319static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
320 int vdd)
321{
322 return 0;
323}
324
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800325static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
326 int vdd, int cardsleep)
327{
328 struct omap_hsmmc_host *host =
329 platform_get_drvdata(to_platform_device(dev));
330 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
331
332 return regulator_set_mode(host->vcc, mode);
333}
334
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500335static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800336 int vdd, int cardsleep)
337{
338 struct omap_hsmmc_host *host =
339 platform_get_drvdata(to_platform_device(dev));
340 int err, mode;
341
342 /*
343 * If we don't see a Vcc regulator, assume it's a fixed
344 * voltage always-on regulator.
345 */
346 if (!host->vcc)
347 return 0;
348
349 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
350
351 if (!host->vcc_aux)
352 return regulator_set_mode(host->vcc, mode);
353
354 if (cardsleep) {
355 /* VCC can be turned off if card is asleep */
356 if (sleep)
Linus Walleij99fc5132010-09-29 01:08:27 -0400357 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800358 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400359 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 } else
361 err = regulator_set_mode(host->vcc, mode);
362 if (err)
363 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800364
365 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
366 return regulator_set_mode(host->vcc_aux, mode);
367
368 if (sleep)
369 return regulator_disable(host->vcc_aux);
370 else
371 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800372}
373
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500374static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
375 int vdd, int cardsleep)
376{
377 return 0;
378}
379
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800380static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
381{
382 struct regulator *reg;
383 int ret = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700384 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800385
386 switch (host->id) {
387 case OMAP_MMC1_DEVID:
388 /* On-chip level shifting via PBIAS0/PBIAS1 */
389 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
390 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
391 break;
392 case OMAP_MMC2_DEVID:
393 case OMAP_MMC3_DEVID:
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500394 case OMAP_MMC5_DEVID:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800395 /* Off-chip level shifting, or none */
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500396 mmc_slot(host).set_power = omap_hsmmc_235_set_power;
397 mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800398 break;
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500399 case OMAP_MMC4_DEVID:
400 mmc_slot(host).set_power = omap_hsmmc_4_set_power;
401 mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800402 default:
403 pr_err("MMC%d configuration not supported!\n", host->id);
404 return -EINVAL;
405 }
406
407 reg = regulator_get(host->dev, "vmmc");
408 if (IS_ERR(reg)) {
409 dev_dbg(host->dev, "vmmc regulator missing\n");
410 /*
411 * HACK: until fixed.c regulator is usable,
412 * we don't require a main regulator
413 * for MMC2 or MMC3
414 */
415 if (host->id == OMAP_MMC1_DEVID) {
416 ret = PTR_ERR(reg);
417 goto err;
418 }
419 } else {
420 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700421 ocr_value = mmc_regulator_get_ocrmask(reg);
422 if (!mmc_slot(host).ocr_mask) {
423 mmc_slot(host).ocr_mask = ocr_value;
424 } else {
425 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
426 pr_err("MMC%d ocrmask %x is not supported\n",
427 host->id, mmc_slot(host).ocr_mask);
428 mmc_slot(host).ocr_mask = 0;
429 return -EINVAL;
430 }
431 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800432
433 /* Allow an aux regulator */
434 reg = regulator_get(host->dev, "vmmc_aux");
435 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
436
Balaji T Kb1c1df72011-05-30 19:55:34 +0530437 /* For eMMC do not power off when not in sleep state */
438 if (mmc_slot(host).no_regulator_off_init)
439 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800440 /*
441 * UGLY HACK: workaround regulator framework bugs.
442 * When the bootloader leaves a supply active, it's
443 * initialized with zero usecount ... and we can't
444 * disable it without first enabling it. Until the
445 * framework is fixed, we need a workaround like this
446 * (which is safe for MMC, but not in general).
447 */
448 if (regulator_is_enabled(host->vcc) > 0) {
449 regulator_enable(host->vcc);
450 regulator_disable(host->vcc);
451 }
452 if (host->vcc_aux) {
453 if (regulator_is_enabled(reg) > 0) {
454 regulator_enable(reg);
455 regulator_disable(reg);
456 }
457 }
458 }
459
460 return 0;
461
462err:
463 mmc_slot(host).set_power = NULL;
464 mmc_slot(host).set_sleep = NULL;
465 return ret;
466}
467
468static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
469{
470 regulator_put(host->vcc);
471 regulator_put(host->vcc_aux);
472 mmc_slot(host).set_power = NULL;
473 mmc_slot(host).set_sleep = NULL;
474}
475
Adrian Hunterb702b102010-02-15 10:03:35 -0800476static inline int omap_hsmmc_have_reg(void)
477{
478 return 1;
479}
480
481#else
482
483static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
484{
485 return -EINVAL;
486}
487
488static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
489{
490}
491
492static inline int omap_hsmmc_have_reg(void)
493{
494 return 0;
495}
496
497#endif
498
499static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
500{
501 int ret;
502
503 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800504 if (pdata->slots[0].cover)
505 pdata->slots[0].get_cover_state =
506 omap_hsmmc_get_cover_state;
507 else
508 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
509 pdata->slots[0].card_detect_irq =
510 gpio_to_irq(pdata->slots[0].switch_pin);
511 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
512 if (ret)
513 return ret;
514 ret = gpio_direction_input(pdata->slots[0].switch_pin);
515 if (ret)
516 goto err_free_sp;
517 } else
518 pdata->slots[0].switch_pin = -EINVAL;
519
520 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
521 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
522 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
523 if (ret)
524 goto err_free_cd;
525 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
526 if (ret)
527 goto err_free_wp;
528 } else
529 pdata->slots[0].gpio_wp = -EINVAL;
530
531 return 0;
532
533err_free_wp:
534 gpio_free(pdata->slots[0].gpio_wp);
535err_free_cd:
536 if (gpio_is_valid(pdata->slots[0].switch_pin))
537err_free_sp:
538 gpio_free(pdata->slots[0].switch_pin);
539 return ret;
540}
541
542static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
543{
544 if (gpio_is_valid(pdata->slots[0].gpio_wp))
545 gpio_free(pdata->slots[0].gpio_wp);
546 if (gpio_is_valid(pdata->slots[0].switch_pin))
547 gpio_free(pdata->slots[0].switch_pin);
548}
549
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100550/*
551 * Stop clock to the card
552 */
Denis Karpov70a33412009-09-22 16:44:59 -0700553static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100554{
555 OMAP_HSMMC_WRITE(host->base, SYSCTL,
556 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
557 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
558 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
559}
560
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700561static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
562 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700563{
564 unsigned int irq_mask;
565
566 if (host->use_dma)
567 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
568 else
569 irq_mask = INT_EN_MASK;
570
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700571 /* Disable timeout for erases */
572 if (cmd->opcode == MMC_ERASE)
573 irq_mask &= ~DTO_ENABLE;
574
Adrian Hunterb4175772010-05-26 14:42:06 -0700575 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
576 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
577 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
578}
579
580static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
581{
582 OMAP_HSMMC_WRITE(host->base, ISE, 0);
583 OMAP_HSMMC_WRITE(host->base, IE, 0);
584 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
585}
586
Denis Karpov11dd62a2009-09-22 16:44:43 -0700587#ifdef CONFIG_PM
588
589/*
590 * Restore the MMC host context, if it was lost as result of a
591 * power state change.
592 */
Denis Karpov70a33412009-09-22 16:44:59 -0700593static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700594{
595 struct mmc_ios *ios = &host->mmc->ios;
596 struct omap_mmc_platform_data *pdata = host->pdata;
597 int context_loss = 0;
598 u32 hctl, capa, con;
599 u16 dsor = 0;
600 unsigned long timeout;
601
602 if (pdata->get_context_loss_count) {
603 context_loss = pdata->get_context_loss_count(host->dev);
604 if (context_loss < 0)
605 return 1;
606 }
607
608 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
609 context_loss == host->context_loss ? "not " : "");
610 if (host->context_loss == context_loss)
611 return 1;
612
613 /* Wait for hardware reset */
614 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
615 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
616 && time_before(jiffies, timeout))
617 ;
618
619 /* Do software reset */
620 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
621 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
622 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
623 && time_before(jiffies, timeout))
624 ;
625
626 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
627 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
628
629 if (host->id == OMAP_MMC1_DEVID) {
630 if (host->power_mode != MMC_POWER_OFF &&
631 (1 << ios->vdd) <= MMC_VDD_23_24)
632 hctl = SDVS18;
633 else
634 hctl = SDVS30;
635 capa = VS30 | VS18;
636 } else {
637 hctl = SDVS18;
638 capa = VS18;
639 }
640
641 OMAP_HSMMC_WRITE(host->base, HCTL,
642 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
643
644 OMAP_HSMMC_WRITE(host->base, CAPA,
645 OMAP_HSMMC_READ(host->base, CAPA) | capa);
646
647 OMAP_HSMMC_WRITE(host->base, HCTL,
648 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
649
650 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
651 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
652 && time_before(jiffies, timeout))
653 ;
654
Adrian Hunterb4175772010-05-26 14:42:06 -0700655 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700656
657 /* Do not initialize card-specific things if the power is off */
658 if (host->power_mode == MMC_POWER_OFF)
659 goto out;
660
661 con = OMAP_HSMMC_READ(host->base, CON);
662 switch (ios->bus_width) {
663 case MMC_BUS_WIDTH_8:
664 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
665 break;
666 case MMC_BUS_WIDTH_4:
667 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
668 OMAP_HSMMC_WRITE(host->base, HCTL,
669 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
670 break;
671 case MMC_BUS_WIDTH_1:
672 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
673 OMAP_HSMMC_WRITE(host->base, HCTL,
674 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
675 break;
676 }
677
678 if (ios->clock) {
679 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
680 if (dsor < 1)
681 dsor = 1;
682
683 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
684 dsor++;
685
686 if (dsor > 250)
687 dsor = 250;
688 }
689
690 OMAP_HSMMC_WRITE(host->base, SYSCTL,
691 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
692 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
693 OMAP_HSMMC_WRITE(host->base, SYSCTL,
694 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
695
696 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
697 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
698 && time_before(jiffies, timeout))
699 ;
700
701 OMAP_HSMMC_WRITE(host->base, SYSCTL,
702 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
703
704 con = OMAP_HSMMC_READ(host->base, CON);
705 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
706 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
707 else
708 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
709out:
710 host->context_loss = context_loss;
711
712 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
713 return 0;
714}
715
716/*
717 * Save the MMC host context (store the number of power state changes so far).
718 */
Denis Karpov70a33412009-09-22 16:44:59 -0700719static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720{
721 struct omap_mmc_platform_data *pdata = host->pdata;
722 int context_loss;
723
724 if (pdata->get_context_loss_count) {
725 context_loss = pdata->get_context_loss_count(host->dev);
726 if (context_loss < 0)
727 return;
728 host->context_loss = context_loss;
729 }
730}
731
732#else
733
Denis Karpov70a33412009-09-22 16:44:59 -0700734static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700735{
736 return 0;
737}
738
Denis Karpov70a33412009-09-22 16:44:59 -0700739static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700740{
741}
742
743#endif
744
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745/*
746 * Send init stream sequence to card
747 * before sending IDLE command
748 */
Denis Karpov70a33412009-09-22 16:44:59 -0700749static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750{
751 int reg = 0;
752 unsigned long timeout;
753
Adrian Hunterb62f6222009-09-22 16:45:01 -0700754 if (host->protect_card)
755 return;
756
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700758
759 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760 OMAP_HSMMC_WRITE(host->base, CON,
761 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
762 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
763
764 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
765 while ((reg != CC) && time_before(jiffies, timeout))
766 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
767
768 OMAP_HSMMC_WRITE(host->base, CON,
769 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700770
771 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
772 OMAP_HSMMC_READ(host->base, STAT);
773
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774 enable_irq(host->irq);
775}
776
777static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700778int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779{
780 int r = 1;
781
Denis Karpov191d1f12009-09-22 16:44:55 -0700782 if (mmc_slot(host).get_cover_state)
783 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100784 return r;
785}
786
787static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700788omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100789 char *buf)
790{
791 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700792 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793
Denis Karpov70a33412009-09-22 16:44:59 -0700794 return sprintf(buf, "%s\n",
795 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100796}
797
Denis Karpov70a33412009-09-22 16:44:59 -0700798static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100799
800static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700801omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802 char *buf)
803{
804 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700805 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806
Denis Karpov191d1f12009-09-22 16:44:55 -0700807 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808}
809
Denis Karpov70a33412009-09-22 16:44:59 -0700810static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100811
812/*
813 * Configure the response type and send the cmd.
814 */
815static void
Denis Karpov70a33412009-09-22 16:44:59 -0700816omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817 struct mmc_data *data)
818{
819 int cmdreg = 0, resptype = 0, cmdtype = 0;
820
821 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
822 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
823 host->cmd = cmd;
824
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700825 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200827 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828 if (cmd->flags & MMC_RSP_PRESENT) {
829 if (cmd->flags & MMC_RSP_136)
830 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200831 else if (cmd->flags & MMC_RSP_BUSY) {
832 resptype = 3;
833 host->response_busy = 1;
834 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835 resptype = 2;
836 }
837
838 /*
839 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
840 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
841 * a val of 0x3, rest 0x0.
842 */
843 if (cmd == host->mrq->stop)
844 cmdtype = 0x3;
845
846 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
847
848 if (data) {
849 cmdreg |= DP_SELECT | MSBS | BCE;
850 if (data->flags & MMC_DATA_READ)
851 cmdreg |= DDIR;
852 else
853 cmdreg &= ~(DDIR);
854 }
855
856 if (host->use_dma)
857 cmdreg |= DMA_EN;
858
Adrian Hunterb4175772010-05-26 14:42:06 -0700859 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700860
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100861 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
862 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
863}
864
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200865static int
Denis Karpov70a33412009-09-22 16:44:59 -0700866omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200867{
868 if (data->flags & MMC_DATA_WRITE)
869 return DMA_TO_DEVICE;
870 else
871 return DMA_FROM_DEVICE;
872}
873
Adrian Hunterb4175772010-05-26 14:42:06 -0700874static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
875{
876 int dma_ch;
877
878 spin_lock(&host->irq_lock);
879 host->req_in_progress = 0;
880 dma_ch = host->dma_ch;
881 spin_unlock(&host->irq_lock);
882
883 omap_hsmmc_disable_irq(host);
884 /* Do not complete the request if DMA is still in progress */
885 if (mrq->data && host->use_dma && dma_ch != -1)
886 return;
887 host->mrq = NULL;
888 mmc_request_done(host->mmc, mrq);
889}
890
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891/*
892 * Notify the transfer complete to MMC core
893 */
894static void
Denis Karpov70a33412009-09-22 16:44:59 -0700895omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100896{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200897 if (!data) {
898 struct mmc_request *mrq = host->mrq;
899
Adrian Hunter23050102009-09-22 16:44:57 -0700900 /* TC before CC from CMD6 - don't know why, but it happens */
901 if (host->cmd && host->cmd->opcode == 6 &&
902 host->response_busy) {
903 host->response_busy = 0;
904 return;
905 }
906
Adrian Hunterb4175772010-05-26 14:42:06 -0700907 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200908 return;
909 }
910
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911 host->data = NULL;
912
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913 if (!data->error)
914 data->bytes_xfered += data->blocks * (data->blksz);
915 else
916 data->bytes_xfered = 0;
917
918 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700919 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920 return;
921 }
Denis Karpov70a33412009-09-22 16:44:59 -0700922 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100923}
924
925/*
926 * Notify the core about command completion
927 */
928static void
Denis Karpov70a33412009-09-22 16:44:59 -0700929omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100930{
931 host->cmd = NULL;
932
933 if (cmd->flags & MMC_RSP_PRESENT) {
934 if (cmd->flags & MMC_RSP_136) {
935 /* response type 2 */
936 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
937 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
938 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
939 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
940 } else {
941 /* response types 1, 1b, 3, 4, 5, 6 */
942 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
943 }
944 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700945 if ((host->data == NULL && !host->response_busy) || cmd->error)
946 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100947}
948
949/*
950 * DMA clean up for command errors
951 */
Denis Karpov70a33412009-09-22 16:44:59 -0700952static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953{
Adrian Hunterb4175772010-05-26 14:42:06 -0700954 int dma_ch;
955
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200956 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100957
Adrian Hunterb4175772010-05-26 14:42:06 -0700958 spin_lock(&host->irq_lock);
959 dma_ch = host->dma_ch;
960 host->dma_ch = -1;
961 spin_unlock(&host->irq_lock);
962
963 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200964 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
965 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700966 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700967 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100968 }
969 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100970}
971
972/*
973 * Readable error output
974 */
975#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700976static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977{
978 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700979 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
981 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
982 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
983 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
984 };
985 char res[256];
986 char *buf = res;
987 int len, i;
988
989 len = sprintf(buf, "MMC IRQ 0x%x :", status);
990 buf += len;
991
Denis Karpov70a33412009-09-22 16:44:59 -0700992 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100993 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700994 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 buf += len;
996 }
997
998 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
999}
1000#endif /* CONFIG_MMC_DEBUG */
1001
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001002/*
1003 * MMC controller internal state machines reset
1004 *
1005 * Used to reset command or data internal state machines, using respectively
1006 * SRC or SRD bit of SYSCTL register
1007 * Can be called from interrupt context
1008 */
Denis Karpov70a33412009-09-22 16:44:59 -07001009static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1010 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001011{
1012 unsigned long i = 0;
1013 unsigned long limit = (loops_per_jiffy *
1014 msecs_to_jiffies(MMC_TIMEOUT_MS));
1015
1016 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1017 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1018
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001019 /*
1020 * OMAP4 ES2 and greater has an updated reset logic.
1021 * Monitor a 0->1 transition first
1022 */
1023 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001024 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001025 && (i++ < limit))
1026 cpu_relax();
1027 }
1028 i = 0;
1029
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001030 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1031 (i++ < limit))
1032 cpu_relax();
1033
1034 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1035 dev_err(mmc_dev(host->mmc),
1036 "Timeout waiting on controller reset in %s\n",
1037 __func__);
1038}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001039
Adrian Hunterb4175772010-05-26 14:42:06 -07001040static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001041{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001042 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001043 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001044
Adrian Hunterb4175772010-05-26 14:42:06 -07001045 if (!host->req_in_progress) {
1046 do {
1047 OMAP_HSMMC_WRITE(host->base, STAT, status);
1048 /* Flush posted write */
1049 status = OMAP_HSMMC_READ(host->base, STAT);
1050 } while (status & INT_EN_MASK);
1051 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001052 }
1053
1054 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1056
1057 if (status & ERR) {
1058#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001059 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001060#endif
1061 if ((status & CMD_TIMEOUT) ||
1062 (status & CMD_CRC)) {
1063 if (host->cmd) {
1064 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001065 omap_hsmmc_reset_controller_fsm(host,
1066 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067 host->cmd->error = -ETIMEDOUT;
1068 } else {
1069 host->cmd->error = -EILSEQ;
1070 }
1071 end_cmd = 1;
1072 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001073 if (host->data || host->response_busy) {
1074 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001075 omap_hsmmc_dma_cleanup(host,
1076 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001077 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001078 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001079 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001080 }
1081 if ((status & DATA_TIMEOUT) ||
1082 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001083 if (host->data || host->response_busy) {
1084 int err = (status & DATA_TIMEOUT) ?
1085 -ETIMEDOUT : -EILSEQ;
1086
1087 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001088 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001090 host->mrq->cmd->error = err;
1091 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001092 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093 end_trans = 1;
1094 }
1095 }
1096 if (status & CARD_ERR) {
1097 dev_dbg(mmc_dev(host->mmc),
1098 "Ignoring card err CMD%d\n", host->cmd->opcode);
1099 if (host->cmd)
1100 end_cmd = 1;
1101 if (host->data)
1102 end_trans = 1;
1103 }
1104 }
1105
1106 OMAP_HSMMC_WRITE(host->base, STAT, status);
1107
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001108 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001109 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001110 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001111 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001112}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113
Adrian Hunterb4175772010-05-26 14:42:06 -07001114/*
1115 * MMC controller IRQ handler
1116 */
1117static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1118{
1119 struct omap_hsmmc_host *host = dev_id;
1120 int status;
1121
1122 status = OMAP_HSMMC_READ(host->base, STAT);
1123 do {
1124 omap_hsmmc_do_irq(host, status);
1125 /* Flush posted write */
1126 status = OMAP_HSMMC_READ(host->base, STAT);
1127 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001128
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001129 return IRQ_HANDLED;
1130}
1131
Denis Karpov70a33412009-09-22 16:44:59 -07001132static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001133{
1134 unsigned long i;
1135
1136 OMAP_HSMMC_WRITE(host->base, HCTL,
1137 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1138 for (i = 0; i < loops_per_jiffy; i++) {
1139 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1140 break;
1141 cpu_relax();
1142 }
1143}
1144
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001145/*
David Brownelleb250822009-02-17 14:49:01 -08001146 * Switch MMC interface voltage ... only relevant for MMC1.
1147 *
1148 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1149 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1150 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151 */
Denis Karpov70a33412009-09-22 16:44:59 -07001152static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001153{
1154 u32 reg_val = 0;
1155 int ret;
1156
1157 /* Disable the clocks */
1158 clk_disable(host->fclk);
1159 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001160 if (host->got_dbclk)
1161 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162
1163 /* Turn the power off */
1164 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165
1166 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001167 if (!ret)
1168 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1169 vdd);
1170 clk_enable(host->iclk);
1171 clk_enable(host->fclk);
1172 if (host->got_dbclk)
1173 clk_enable(host->dbclk);
1174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175 if (ret != 0)
1176 goto err;
1177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178 OMAP_HSMMC_WRITE(host->base, HCTL,
1179 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1180 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001181
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182 /*
1183 * If a MMC dual voltage card is detected, the set_ios fn calls
1184 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001185 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186 *
David Brownelleb250822009-02-17 14:49:01 -08001187 * Cope with a bit of slop in the range ... per data sheets:
1188 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1189 * but recommended values are 1.71V to 1.89V
1190 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1191 * but recommended values are 2.7V to 3.3V
1192 *
1193 * Board setup code shouldn't permit anything very out-of-range.
1194 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1195 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 */
David Brownelleb250822009-02-17 14:49:01 -08001197 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001199 else
1200 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201
1202 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001203 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 return 0;
1206err:
1207 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1208 return ret;
1209}
1210
Adrian Hunterb62f6222009-09-22 16:45:01 -07001211/* Protect the card while the cover is open */
1212static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1213{
1214 if (!mmc_slot(host).get_cover_state)
1215 return;
1216
1217 host->reqs_blocked = 0;
1218 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1219 if (host->protect_card) {
1220 printk(KERN_INFO "%s: cover is closed, "
1221 "card is now accessible\n",
1222 mmc_hostname(host->mmc));
1223 host->protect_card = 0;
1224 }
1225 } else {
1226 if (!host->protect_card) {
1227 printk(KERN_INFO "%s: cover is open, "
1228 "card is now inaccessible\n",
1229 mmc_hostname(host->mmc));
1230 host->protect_card = 1;
1231 }
1232 }
1233}
1234
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235/*
1236 * Work Item to notify the core about card insertion/removal
1237 */
Denis Karpov70a33412009-09-22 16:44:59 -07001238static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001239{
Denis Karpov70a33412009-09-22 16:44:59 -07001240 struct omap_hsmmc_host *host =
1241 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001242 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001243 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001244
Adrian Huntera6b22402009-09-22 16:44:45 -07001245 if (host->suspended)
1246 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001247
1248 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001249
Denis Karpov191d1f12009-09-22 16:44:55 -07001250 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001251 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001252 else {
1253 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001254 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001255 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001256
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001257 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001259 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001261}
1262
1263/*
1264 * ISR for handling card insertion and removal
1265 */
Denis Karpov70a33412009-09-22 16:44:59 -07001266static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267{
Denis Karpov70a33412009-09-22 16:44:59 -07001268 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
Adrian Huntera6b22402009-09-22 16:44:45 -07001270 if (host->suspended)
1271 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272 schedule_work(&host->mmc_carddetect_work);
1273
1274 return IRQ_HANDLED;
1275}
1276
Denis Karpov70a33412009-09-22 16:44:59 -07001277static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001278 struct mmc_data *data)
1279{
1280 int sync_dev;
1281
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001282 if (data->flags & MMC_DATA_WRITE)
1283 sync_dev = host->dma_line_tx;
1284 else
1285 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001286 return sync_dev;
1287}
1288
Denis Karpov70a33412009-09-22 16:44:59 -07001289static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001290 struct mmc_data *data,
1291 struct scatterlist *sgl)
1292{
1293 int blksz, nblk, dma_ch;
1294
1295 dma_ch = host->dma_ch;
1296 if (data->flags & MMC_DATA_WRITE) {
1297 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1298 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1299 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1300 sg_dma_address(sgl), 0, 0);
1301 } else {
1302 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001303 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001304 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1305 sg_dma_address(sgl), 0, 0);
1306 }
1307
1308 blksz = host->data->blksz;
1309 nblk = sg_dma_len(sgl) / blksz;
1310
1311 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1312 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001313 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001314 !(data->flags & MMC_DATA_WRITE));
1315
1316 omap_start_dma(dma_ch);
1317}
1318
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319/*
1320 * DMA call back function
1321 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001322static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323{
Adrian Hunterb4175772010-05-26 14:42:06 -07001324 struct omap_hsmmc_host *host = cb_data;
1325 struct mmc_data *data = host->mrq->data;
1326 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001327
Venkatraman Sf3584e52010-08-10 18:01:54 -07001328 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1329 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1330 ch_status);
1331 return;
1332 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333
Adrian Hunterb4175772010-05-26 14:42:06 -07001334 spin_lock(&host->irq_lock);
1335 if (host->dma_ch < 0) {
1336 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001338 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001340 host->dma_sg_idx++;
1341 if (host->dma_sg_idx < host->dma_len) {
1342 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001343 omap_hsmmc_config_dma_params(host, data,
1344 data->sg + host->dma_sg_idx);
1345 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001346 return;
1347 }
1348
Per Forlina9120c32011-06-17 20:14:21 +02001349 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Adrian Hunterb4175772010-05-26 14:42:06 -07001350 omap_hsmmc_get_dma_dir(host, data));
1351
1352 req_in_progress = host->req_in_progress;
1353 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001355 spin_unlock(&host->irq_lock);
1356
1357 omap_free_dma(dma_ch);
1358
1359 /* If DMA has finished after TC, complete the request */
1360 if (!req_in_progress) {
1361 struct mmc_request *mrq = host->mrq;
1362
1363 host->mrq = NULL;
1364 mmc_request_done(host->mmc, mrq);
1365 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001366}
1367
1368/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001369 * Routine to configure and start DMA for the MMC card
1370 */
Denis Karpov70a33412009-09-22 16:44:59 -07001371static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1372 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373{
Adrian Hunterb4175772010-05-26 14:42:06 -07001374 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375 struct mmc_data *data = req->data;
1376
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001377 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001378 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001379 struct scatterlist *sgl;
1380
1381 sgl = data->sg + i;
1382 if (sgl->length % data->blksz)
1383 return -EINVAL;
1384 }
1385 if ((data->blksz % 4) != 0)
1386 /* REVISIT: The MMC buffer increments only when MSB is written.
1387 * Return error for blksz which is non multiple of four.
1388 */
1389 return -EINVAL;
1390
Adrian Hunterb4175772010-05-26 14:42:06 -07001391 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001392
Denis Karpov70a33412009-09-22 16:44:59 -07001393 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1394 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001395 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001396 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397 "%s: omap_request_dma() failed with %d\n",
1398 mmc_hostname(host->mmc), ret);
1399 return ret;
1400 }
1401
1402 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001403 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001405 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001406
Denis Karpov70a33412009-09-22 16:44:59 -07001407 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001408
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409 return 0;
1410}
1411
Denis Karpov70a33412009-09-22 16:44:59 -07001412static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001413 unsigned int timeout_ns,
1414 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415{
1416 unsigned int timeout, cycle_ns;
1417 uint32_t reg, clkd, dto = 0;
1418
1419 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1420 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1421 if (clkd == 0)
1422 clkd = 1;
1423
1424 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001425 timeout = timeout_ns / cycle_ns;
1426 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001427 if (timeout) {
1428 while ((timeout & 0x80000000) == 0) {
1429 dto += 1;
1430 timeout <<= 1;
1431 }
1432 dto = 31 - dto;
1433 timeout <<= 1;
1434 if (timeout && dto)
1435 dto += 1;
1436 if (dto >= 13)
1437 dto -= 13;
1438 else
1439 dto = 0;
Ulf Hansson6075fff2012-03-05 15:52:43 +01001440 /* Use the maximum timeout value allowed in the standard of 14
1441 or 0xE */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442 if (dto > 14)
1443 dto = 14;
1444 }
1445
1446 reg &= ~DTO_MASK;
1447 reg |= dto << DTO_SHIFT;
1448 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1449}
1450
1451/*
1452 * Configure block length for MMC/SD cards and initiate the transfer.
1453 */
1454static int
Denis Karpov70a33412009-09-22 16:44:59 -07001455omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001456{
1457 int ret;
1458 host->data = req->data;
1459
1460 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001462 /*
1463 * Set an arbitrary 100ms data timeout for commands with
1464 * busy signal.
1465 */
1466 if (req->cmd->flags & MMC_RSP_BUSY)
1467 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001468 return 0;
1469 }
1470
1471 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1472 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001473 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001475 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001476 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001477 if (ret != 0) {
1478 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1479 return ret;
1480 }
1481 }
1482 return 0;
1483}
1484
1485/*
1486 * Request function. for read/write operation
1487 */
Denis Karpov70a33412009-09-22 16:44:59 -07001488static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489{
Denis Karpov70a33412009-09-22 16:44:59 -07001490 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001491 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001492
Adrian Hunterb4175772010-05-26 14:42:06 -07001493 BUG_ON(host->req_in_progress);
1494 BUG_ON(host->dma_ch != -1);
1495 if (host->protect_card) {
1496 if (host->reqs_blocked < 3) {
1497 /*
1498 * Ensure the controller is left in a consistent
1499 * state by resetting the command and data state
1500 * machines.
1501 */
1502 omap_hsmmc_reset_controller_fsm(host, SRD);
1503 omap_hsmmc_reset_controller_fsm(host, SRC);
1504 host->reqs_blocked += 1;
1505 }
1506 req->cmd->error = -EBADF;
1507 if (req->data)
1508 req->data->error = -EBADF;
1509 req->cmd->retries = 0;
1510 mmc_request_done(mmc, req);
1511 return;
1512 } else if (host->reqs_blocked)
1513 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514 WARN_ON(host->mrq != NULL);
1515 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001516 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001517 if (err) {
1518 req->cmd->error = err;
1519 if (req->data)
1520 req->data->error = err;
1521 host->mrq = NULL;
1522 mmc_request_done(mmc, req);
1523 return;
1524 }
1525
Denis Karpov70a33412009-09-22 16:44:59 -07001526 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527}
1528
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001529/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001530static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531{
Denis Karpov70a33412009-09-22 16:44:59 -07001532 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533 u16 dsor = 0;
1534 unsigned long regval;
1535 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001536 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001537 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001539 mmc_host_enable(host->mmc);
1540
Adrian Huntera3621462009-09-22 16:44:42 -07001541 if (ios->power_mode != host->power_mode) {
1542 switch (ios->power_mode) {
1543 case MMC_POWER_OFF:
1544 mmc_slot(host).set_power(host->dev, host->slot_id,
1545 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001546 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001547 break;
1548 case MMC_POWER_UP:
1549 mmc_slot(host).set_power(host->dev, host->slot_id,
1550 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001551 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001552 break;
1553 case MMC_POWER_ON:
1554 do_send_init_stream = 1;
1555 break;
1556 }
1557 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558 }
1559
Denis Karpovdd498ef2009-09-22 16:44:49 -07001560 /* FIXME: set registers based only on changes to ios */
1561
Jarkko Lavinen73153012008-11-21 16:49:54 +02001562 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001564 case MMC_BUS_WIDTH_8:
1565 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1566 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001568 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569 OMAP_HSMMC_WRITE(host->base, HCTL,
1570 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1571 break;
1572 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001573 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574 OMAP_HSMMC_WRITE(host->base, HCTL,
1575 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1576 break;
1577 }
1578
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301579 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001580 /* Only MMC1 can interface at 3V without some flavor
1581 * of external transceiver; but they all handle 1.8V.
1582 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001583 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1584 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1585 /*
1586 * The mmc_select_voltage fn of the core does
1587 * not seem to set the power_mode to
1588 * MMC_POWER_UP upon recalculating the voltage.
1589 * vdd 1.8v.
1590 */
Denis Karpov70a33412009-09-22 16:44:59 -07001591 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1592 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593 "Switch operation failed\n");
1594 }
1595 }
1596
1597 if (ios->clock) {
1598 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1599 if (dsor < 1)
1600 dsor = 1;
1601
1602 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1603 dsor++;
1604
1605 if (dsor > 250)
1606 dsor = 250;
1607 }
Denis Karpov70a33412009-09-22 16:44:59 -07001608 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1610 regval = regval & ~(CLKD_MASK);
1611 regval = regval | (dsor << 6) | (DTO << 16);
1612 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1613 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1614 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1615
1616 /* Wait till the ICS bit is set */
1617 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001618 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001619 && time_before(jiffies, timeout))
1620 msleep(1);
1621
1622 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1623 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1624
Adrian Huntera3621462009-09-22 16:44:42 -07001625 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001626 send_init_stream(host);
1627
Denis Karpovabb28e72009-09-22 16:44:44 -07001628 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001630 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1631 else
1632 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001633
Denis Karpovdd498ef2009-09-22 16:44:49 -07001634 if (host->power_mode == MMC_POWER_OFF)
1635 mmc_host_disable(host->mmc);
1636 else
1637 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001638}
1639
1640static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1641{
Denis Karpov70a33412009-09-22 16:44:59 -07001642 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001643
Denis Karpov191d1f12009-09-22 16:44:55 -07001644 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001645 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001646 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647}
1648
1649static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1650{
Denis Karpov70a33412009-09-22 16:44:59 -07001651 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001652
Denis Karpov191d1f12009-09-22 16:44:55 -07001653 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001654 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001655 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001656}
1657
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001658static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1659{
1660 struct omap_hsmmc_host *host = mmc_priv(mmc);
1661
1662 if (mmc_slot(host).init_card)
1663 mmc_slot(host).init_card(card);
1664}
1665
Denis Karpov70a33412009-09-22 16:44:59 -07001666static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001667{
1668 u32 hctl, capa, value;
1669
1670 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301671 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001672 hctl = SDVS30;
1673 capa = VS30 | VS18;
1674 } else {
1675 hctl = SDVS18;
1676 capa = VS18;
1677 }
1678
1679 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1680 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1681
1682 value = OMAP_HSMMC_READ(host->base, CAPA);
1683 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1684
1685 /* Set the controller to AUTO IDLE mode */
1686 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1687 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1688
1689 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001690 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001691}
1692
Denis Karpovdd498ef2009-09-22 16:44:49 -07001693/*
1694 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001695 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1696 * ^___________| | |
1697 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001698 *
1699 * ENABLED: mmc host is fully functional
1700 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001701 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1702 * REGSLEEP: fclk is off, voltage regulator is asleep
1703 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001704 *
1705 * Transition handlers return the timeout for the next state transition
1706 * or negative error.
1707 */
1708
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001709enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001710
1711/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001712static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001713{
Denis Karpov70a33412009-09-22 16:44:59 -07001714 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001715 clk_disable(host->fclk);
1716 host->dpm_state = DISABLED;
1717
1718 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1719
1720 if (host->power_mode == MMC_POWER_OFF)
1721 return 0;
1722
Adrian Hunter4380eea2010-02-15 10:03:34 -08001723 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001724}
1725
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001726/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001727static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001728{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001729 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001730
1731 if (!mmc_try_claim_host(host->mmc))
1732 return 0;
1733
1734 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001735 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001736 if (mmc_card_can_sleep(host->mmc)) {
1737 err = mmc_card_sleep(host->mmc);
1738 if (err < 0) {
1739 clk_disable(host->fclk);
1740 mmc_release_host(host->mmc);
1741 return err;
1742 }
1743 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001744 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001745 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001746 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001747 if (mmc_slot(host).set_sleep)
1748 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1749 new_state == CARDSLEEP);
1750 /* FIXME: turn off bus power and perhaps interrupts too */
1751 clk_disable(host->fclk);
1752 host->dpm_state = new_state;
1753
1754 mmc_release_host(host->mmc);
1755
1756 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1757 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001758
Adrian Hunter1df58db2010-02-15 10:03:34 -08001759 if (mmc_slot(host).no_off)
1760 return 0;
1761
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1763 mmc_slot(host).card_detect ||
1764 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001765 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea2010-02-15 10:03:34 -08001766 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001767
1768 return 0;
1769}
1770
1771/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001772static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001773{
1774 if (!mmc_try_claim_host(host->mmc))
1775 return 0;
1776
Adrian Hunter1df58db2010-02-15 10:03:34 -08001777 if (mmc_slot(host).no_off)
1778 return 0;
1779
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001780 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1781 mmc_slot(host).card_detect ||
1782 (mmc_slot(host).get_cover_state &&
1783 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1784 mmc_release_host(host->mmc);
1785 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001786 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001787
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001788 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1789 host->vdd = 0;
1790 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001791
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001792 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1793 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001794
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001795 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001796
1797 mmc_release_host(host->mmc);
1798
1799 return 0;
1800}
1801
1802/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001803static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001804{
1805 int err;
1806
1807 err = clk_enable(host->fclk);
1808 if (err < 0)
1809 return err;
1810
Denis Karpov70a33412009-09-22 16:44:59 -07001811 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001812 host->dpm_state = ENABLED;
1813
1814 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1815
1816 return 0;
1817}
1818
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001819/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001820static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001821{
1822 if (!mmc_try_claim_host(host->mmc))
1823 return 0;
1824
1825 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001826 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001827 if (mmc_slot(host).set_sleep)
1828 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1829 host->vdd, host->dpm_state == CARDSLEEP);
1830 if (mmc_card_can_sleep(host->mmc))
1831 mmc_card_awake(host->mmc);
1832
1833 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1834 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1835
1836 host->dpm_state = ENABLED;
1837
1838 mmc_release_host(host->mmc);
1839
1840 return 0;
1841}
1842
Denis Karpovdd498ef2009-09-22 16:44:49 -07001843/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001844static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001845{
1846 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001847
Denis Karpov70a33412009-09-22 16:44:59 -07001848 omap_hsmmc_context_restore(host);
1849 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001850 mmc_power_restore_host(host->mmc);
1851
1852 host->dpm_state = ENABLED;
1853
1854 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1855
1856 return 0;
1857}
1858
1859/*
1860 * Bring MMC host to ENABLED from any other PM state.
1861 */
Denis Karpov70a33412009-09-22 16:44:59 -07001862static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001863{
Denis Karpov70a33412009-09-22 16:44:59 -07001864 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001865
1866 switch (host->dpm_state) {
1867 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001868 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001869 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001870 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001871 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001872 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001873 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001874 default:
1875 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1876 return -EINVAL;
1877 }
1878}
1879
1880/*
1881 * Bring MMC host in PM state (one level deeper).
1882 */
Denis Karpov70a33412009-09-22 16:44:59 -07001883static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001884{
Denis Karpov70a33412009-09-22 16:44:59 -07001885 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001886
1887 switch (host->dpm_state) {
1888 case ENABLED: {
1889 int delay;
1890
Denis Karpov70a33412009-09-22 16:44:59 -07001891 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001892 if (lazy || delay < 0)
1893 return delay;
1894 return 0;
1895 }
1896 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001897 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001898 case CARDSLEEP:
1899 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001900 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001901 default:
1902 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1903 return -EINVAL;
1904 }
1905}
1906
Denis Karpov70a33412009-09-22 16:44:59 -07001907static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001908{
Denis Karpov70a33412009-09-22 16:44:59 -07001909 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001910 int err;
1911
1912 err = clk_enable(host->fclk);
1913 if (err)
1914 return err;
1915 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001916 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001917 return 0;
1918}
1919
Denis Karpov70a33412009-09-22 16:44:59 -07001920static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001921{
Denis Karpov70a33412009-09-22 16:44:59 -07001922 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001923
Denis Karpov70a33412009-09-22 16:44:59 -07001924 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001925 clk_disable(host->fclk);
1926 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1927 return 0;
1928}
1929
Denis Karpov70a33412009-09-22 16:44:59 -07001930static const struct mmc_host_ops omap_hsmmc_ops = {
1931 .enable = omap_hsmmc_enable_fclk,
1932 .disable = omap_hsmmc_disable_fclk,
1933 .request = omap_hsmmc_request,
1934 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001935 .get_cd = omap_hsmmc_get_cd,
1936 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001937 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001938 /* NYET -- enable_sdio_irq */
1939};
1940
Denis Karpov70a33412009-09-22 16:44:59 -07001941static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1942 .enable = omap_hsmmc_enable,
1943 .disable = omap_hsmmc_disable,
1944 .request = omap_hsmmc_request,
1945 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001946 .get_cd = omap_hsmmc_get_cd,
1947 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001948 .init_card = omap_hsmmc_init_card,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 /* NYET -- enable_sdio_irq */
1950};
1951
Denis Karpovd900f712009-09-22 16:44:38 -07001952#ifdef CONFIG_DEBUG_FS
1953
Denis Karpov70a33412009-09-22 16:44:59 -07001954static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001955{
1956 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001957 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001958 int context_loss = 0;
1959
Denis Karpov70a33412009-09-22 16:44:59 -07001960 if (host->pdata->get_context_loss_count)
1961 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001962
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001963 seq_printf(s, "mmc%d:\n"
1964 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001965 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001966 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001967 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001968 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001969 mmc->index, mmc->enabled ? 1 : 0,
1970 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001971 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001972
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001973 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001974 seq_printf(s, "host suspended, can't read registers\n");
1975 return 0;
1976 }
1977
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001978 if (clk_enable(host->fclk) != 0) {
1979 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001980 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001981 }
Denis Karpovd900f712009-09-22 16:44:38 -07001982
1983 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1984 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1985 seq_printf(s, "CON:\t\t0x%08x\n",
1986 OMAP_HSMMC_READ(host->base, CON));
1987 seq_printf(s, "HCTL:\t\t0x%08x\n",
1988 OMAP_HSMMC_READ(host->base, HCTL));
1989 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1990 OMAP_HSMMC_READ(host->base, SYSCTL));
1991 seq_printf(s, "IE:\t\t0x%08x\n",
1992 OMAP_HSMMC_READ(host->base, IE));
1993 seq_printf(s, "ISE:\t\t0x%08x\n",
1994 OMAP_HSMMC_READ(host->base, ISE));
1995 seq_printf(s, "CAPA:\t\t0x%08x\n",
1996 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001997
1998 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001999
Denis Karpovd900f712009-09-22 16:44:38 -07002000 return 0;
2001}
2002
Denis Karpov70a33412009-09-22 16:44:59 -07002003static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07002004{
Denis Karpov70a33412009-09-22 16:44:59 -07002005 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07002006}
2007
2008static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002009 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07002010 .read = seq_read,
2011 .llseek = seq_lseek,
2012 .release = single_release,
2013};
2014
Denis Karpov70a33412009-09-22 16:44:59 -07002015static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07002016{
2017 if (mmc->debugfs_root)
2018 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
2019 mmc, &mmc_regs_fops);
2020}
2021
2022#else
2023
Denis Karpov70a33412009-09-22 16:44:59 -07002024static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07002025{
2026}
2027
2028#endif
2029
Denis Karpov70a33412009-09-22 16:44:59 -07002030static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002031{
2032 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
2033 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002034 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002036 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037
2038 if (pdata == NULL) {
2039 dev_err(&pdev->dev, "Platform Data is missing\n");
2040 return -ENXIO;
2041 }
2042
2043 if (pdata->nr_slots == 0) {
2044 dev_err(&pdev->dev, "No Slots\n");
2045 return -ENXIO;
2046 }
2047
2048 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2049 irq = platform_get_irq(pdev, 0);
2050 if (res == NULL || irq < 0)
2051 return -ENXIO;
2052
kishore kadiyala91a0b082010-10-01 16:35:28 -07002053 res->start += pdata->reg_offset;
2054 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07002055 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002056 if (res == NULL)
2057 return -EBUSY;
2058
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002059 ret = omap_hsmmc_gpio_init(pdata);
2060 if (ret)
2061 goto err;
2062
Denis Karpov70a33412009-09-22 16:44:59 -07002063 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 if (!mmc) {
2065 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002066 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 }
2068
2069 host = mmc_priv(mmc);
2070 host->mmc = mmc;
2071 host->pdata = pdata;
2072 host->dev = &pdev->dev;
2073 host->use_dma = 1;
2074 host->dev->dma_mask = &pdata->dma_mask;
2075 host->dma_ch = -1;
2076 host->irq = irq;
2077 host->id = pdev->id;
2078 host->slot_id = 0;
2079 host->mapbase = res->start;
2080 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002081 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002082
2083 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002084 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085
Denis Karpov191d1f12009-09-22 16:44:55 -07002086 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002087 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002088 else
Denis Karpov70a33412009-09-22 16:44:59 -07002089 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002090
Adrian Huntere0eb2422010-02-15 10:03:34 -08002091 /*
2092 * If regulator_disable can only put vcc_aux to sleep then there is
2093 * no off state.
2094 */
2095 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2096 mmc_slot(host).no_off = 1;
2097
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002098 mmc->f_min = 400000;
2099 mmc->f_max = 52000000;
2100
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002101 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102
Russell King6f7607c2009-01-28 10:22:50 +00002103 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104 if (IS_ERR(host->iclk)) {
2105 ret = PTR_ERR(host->iclk);
2106 host->iclk = NULL;
2107 goto err1;
2108 }
Russell King6f7607c2009-01-28 10:22:50 +00002109 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110 if (IS_ERR(host->fclk)) {
2111 ret = PTR_ERR(host->fclk);
2112 host->fclk = NULL;
2113 clk_put(host->iclk);
2114 goto err1;
2115 }
2116
Denis Karpov70a33412009-09-22 16:44:59 -07002117 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002118
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002119 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002120 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2121 /* we start off in DISABLED state */
2122 host->dpm_state = DISABLED;
2123
Paul Walmsleya05dcdb2011-02-25 18:22:08 -07002124 if (clk_enable(host->iclk) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002125 clk_put(host->iclk);
2126 clk_put(host->fclk);
2127 goto err1;
2128 }
2129
Paul Walmsleya05dcdb2011-02-25 18:22:08 -07002130 if (mmc_host_enable(host->mmc) != 0) {
2131 clk_disable(host->iclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002132 clk_put(host->iclk);
2133 clk_put(host->fclk);
2134 goto err1;
2135 }
2136
Adrian Hunter2bec0892009-09-22 16:45:02 -07002137 if (cpu_is_omap2430()) {
2138 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2139 /*
2140 * MMC can still work without debounce clock.
2141 */
2142 if (IS_ERR(host->dbclk))
2143 dev_warn(mmc_dev(host->mmc),
2144 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002145 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002146 host->got_dbclk = 1;
2147
2148 if (host->got_dbclk)
2149 if (clk_enable(host->dbclk) != 0)
2150 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2151 " clk failed\n");
2152 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002154 /* Since we do only SG emulation, we can have as many segs
2155 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002156 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002157
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2159 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2160 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2161 mmc->max_seg_size = mmc->max_req_size;
2162
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002163 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e2010-08-11 14:17:48 -07002164 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002165
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002166 mmc->caps |= mmc_slot(host).caps;
2167 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002168 mmc->caps |= MMC_CAP_4_BIT_DATA;
2169
Denis Karpov191d1f12009-09-22 16:44:55 -07002170 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002171 mmc->caps |= MMC_CAP_NONREMOVABLE;
2172
Denis Karpov70a33412009-09-22 16:44:59 -07002173 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002175 /* Select DMA lines */
2176 switch (host->id) {
2177 case OMAP_MMC1_DEVID:
2178 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2179 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2180 break;
2181 case OMAP_MMC2_DEVID:
2182 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2183 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2184 break;
2185 case OMAP_MMC3_DEVID:
2186 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2187 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2188 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002189 case OMAP_MMC4_DEVID:
2190 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2191 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2192 break;
2193 case OMAP_MMC5_DEVID:
2194 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2195 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2196 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002197 default:
2198 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2199 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002200 }
2201
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002203 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204 mmc_hostname(mmc), host);
2205 if (ret) {
2206 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2207 goto err_irq;
2208 }
2209
2210 if (pdata->init != NULL) {
2211 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002212 dev_dbg(mmc_dev(host->mmc),
2213 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214 goto err_irq_cd_init;
2215 }
2216 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002217
Adrian Hunterb702b102010-02-15 10:03:35 -08002218 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002219 ret = omap_hsmmc_reg_get(host);
2220 if (ret)
2221 goto err_reg;
2222 host->use_reg = 1;
2223 }
2224
David Brownellb583f262009-05-28 14:04:03 -07002225 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002226
2227 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002228 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002230 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2232 | IRQF_DISABLED,
2233 mmc_hostname(mmc), host);
2234 if (ret) {
2235 dev_dbg(mmc_dev(host->mmc),
2236 "Unable to grab MMC CD IRQ\n");
2237 goto err_irq_cd;
2238 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002239 pdata->suspend = omap_hsmmc_suspend_cdirq;
2240 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002241 }
2242
Adrian Hunterb4175772010-05-26 14:42:06 -07002243 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002244
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002245 mmc_host_lazy_disable(host->mmc);
2246
Adrian Hunterb62f6222009-09-22 16:45:01 -07002247 omap_hsmmc_protect_card(host);
2248
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 mmc_add_host(mmc);
2250
Denis Karpov191d1f12009-09-22 16:44:55 -07002251 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2253 if (ret < 0)
2254 goto err_slot_name;
2255 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002256 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002257 ret = device_create_file(&mmc->class_dev,
2258 &dev_attr_cover_switch);
2259 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002260 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002261 }
2262
Denis Karpov70a33412009-09-22 16:44:59 -07002263 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002264
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002265 return 0;
2266
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267err_slot_name:
2268 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002270err_irq_cd:
2271 if (host->use_reg)
2272 omap_hsmmc_reg_put(host);
2273err_reg:
2274 if (host->pdata->cleanup)
2275 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002276err_irq_cd_init:
2277 free_irq(host->irq, host);
2278err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002279 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002280 clk_disable(host->iclk);
2281 clk_put(host->fclk);
2282 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002283 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002284 clk_disable(host->dbclk);
2285 clk_put(host->dbclk);
2286 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002287err1:
2288 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002289 platform_set_drvdata(pdev, NULL);
2290 mmc_free_host(mmc);
2291err_alloc:
2292 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002293err:
Chris Ball984b2032011-03-22 16:34:42 -07002294 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295 return ret;
2296}
2297
Denis Karpov70a33412009-09-22 16:44:59 -07002298static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002299{
Denis Karpov70a33412009-09-22 16:44:59 -07002300 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002301 struct resource *res;
2302
2303 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002304 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002305 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002306 if (host->use_reg)
2307 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002308 if (host->pdata->cleanup)
2309 host->pdata->cleanup(&pdev->dev);
2310 free_irq(host->irq, host);
2311 if (mmc_slot(host).card_detect_irq)
2312 free_irq(mmc_slot(host).card_detect_irq, host);
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002313 flush_work_sync(&host->mmc_carddetect_work);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002314
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002315 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002316 clk_disable(host->iclk);
2317 clk_put(host->fclk);
2318 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002319 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002320 clk_disable(host->dbclk);
2321 clk_put(host->dbclk);
2322 }
2323
2324 mmc_free_host(host->mmc);
2325 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002326 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002327 }
2328
2329 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2330 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002331 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002332 platform_set_drvdata(pdev, NULL);
2333
2334 return 0;
2335}
2336
2337#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002338static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002339{
2340 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002341 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002342 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002343
2344 if (host && host->suspended)
2345 return 0;
2346
2347 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002348 host->suspended = 1;
2349 if (host->pdata->suspend) {
2350 ret = host->pdata->suspend(&pdev->dev,
2351 host->slot_id);
2352 if (ret) {
2353 dev_dbg(mmc_dev(host->mmc),
2354 "Unable to handle MMC board"
2355 " level suspend\n");
2356 host->suspended = 0;
2357 return ret;
2358 }
2359 }
2360 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002361 ret = mmc_suspend_host(host->mmc);
Ethan Due7cb7562010-09-09 16:37:49 -07002362 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002363 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002364 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002365 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002366 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002367 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002368 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002369 if (host->got_dbclk)
2370 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002371 } else {
2372 host->suspended = 0;
2373 if (host->pdata->resume) {
2374 ret = host->pdata->resume(&pdev->dev,
2375 host->slot_id);
2376 if (ret)
2377 dev_dbg(mmc_dev(host->mmc),
2378 "Unmask interrupt failed\n");
2379 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002380 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002381 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002382
2383 }
2384 return ret;
2385}
2386
2387/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002388static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002389{
2390 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002391 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002392 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002393
2394 if (host && !host->suspended)
2395 return 0;
2396
2397 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002398 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002399 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002400 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002401
Denis Karpov11dd62a2009-09-22 16:44:43 -07002402 if (mmc_host_enable(host->mmc) != 0) {
2403 clk_disable(host->iclk);
2404 goto clk_en_err;
2405 }
2406
Adrian Hunter2bec0892009-09-22 16:45:02 -07002407 if (host->got_dbclk)
2408 clk_enable(host->dbclk);
2409
Denis Karpov70a33412009-09-22 16:44:59 -07002410 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002411
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002412 if (host->pdata->resume) {
2413 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2414 if (ret)
2415 dev_dbg(mmc_dev(host->mmc),
2416 "Unmask interrupt failed\n");
2417 }
2418
Adrian Hunterb62f6222009-09-22 16:45:01 -07002419 omap_hsmmc_protect_card(host);
2420
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002421 /* Notify the core to resume the host */
2422 ret = mmc_resume_host(host->mmc);
2423 if (ret == 0)
2424 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002425
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002426 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002427 }
2428
2429 return ret;
2430
2431clk_en_err:
2432 dev_dbg(mmc_dev(host->mmc),
2433 "Failed to enable MMC clocks during resume\n");
2434 return ret;
2435}
2436
2437#else
Denis Karpov70a33412009-09-22 16:44:59 -07002438#define omap_hsmmc_suspend NULL
2439#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002440#endif
2441
Kevin Hilmana791daa2010-05-26 14:42:07 -07002442static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002443 .suspend = omap_hsmmc_suspend,
2444 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002445};
2446
2447static struct platform_driver omap_hsmmc_driver = {
2448 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002449 .driver = {
2450 .name = DRIVER_NAME,
2451 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002452 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002453 },
2454};
2455
Denis Karpov70a33412009-09-22 16:44:59 -07002456static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002457{
2458 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002459 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002460}
2461
Denis Karpov70a33412009-09-22 16:44:59 -07002462static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002463{
2464 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002465 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002466}
2467
Denis Karpov70a33412009-09-22 16:44:59 -07002468module_init(omap_hsmmc_init);
2469module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002470
2471MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2472MODULE_LICENSE("GPL");
2473MODULE_ALIAS("platform:" DRIVER_NAME);
2474MODULE_AUTHOR("Texas Instruments Inc");