blob: dc57ef6aef4f5b9ab86c72a3c83f2376f5f6f764 [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>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010031#include <linux/io.h>
32#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080033#include <linux/gpio.h>
34#include <linux/regulator/consumer.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/board.h>
38#include <plat/mmc.h>
39#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040
41/* OMAP HSMMC Host Controller Registers */
42#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070043#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010044#define OMAP_HSMMC_CON 0x002C
45#define OMAP_HSMMC_BLK 0x0104
46#define OMAP_HSMMC_ARG 0x0108
47#define OMAP_HSMMC_CMD 0x010C
48#define OMAP_HSMMC_RSP10 0x0110
49#define OMAP_HSMMC_RSP32 0x0114
50#define OMAP_HSMMC_RSP54 0x0118
51#define OMAP_HSMMC_RSP76 0x011C
52#define OMAP_HSMMC_DATA 0x0120
53#define OMAP_HSMMC_HCTL 0x0128
54#define OMAP_HSMMC_SYSCTL 0x012C
55#define OMAP_HSMMC_STAT 0x0130
56#define OMAP_HSMMC_IE 0x0134
57#define OMAP_HSMMC_ISE 0x0138
58#define OMAP_HSMMC_CAPA 0x0140
59
60#define VS18 (1 << 26)
61#define VS30 (1 << 25)
62#define SDVS18 (0x5 << 9)
63#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080064#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010065#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define SDVSCLR 0xFFFFF1FF
67#define SDVSDET 0x00000400
68#define AUTOIDLE 0x1
69#define SDBP (1 << 8)
70#define DTO 0xe
71#define ICE 0x1
72#define ICS 0x2
73#define CEN (1 << 2)
74#define CLKD_MASK 0x0000FFC0
75#define CLKD_SHIFT 6
76#define DTO_MASK 0x000F0000
77#define DTO_SHIFT 16
78#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070079#define BWR_ENABLE (1 << 4)
80#define BRR_ENABLE (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010081#define INIT_STREAM (1 << 1)
82#define DP_SELECT (1 << 21)
83#define DDIR (1 << 4)
84#define DMA_EN 0x1
85#define MSBS (1 << 5)
86#define BCE (1 << 1)
87#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020088#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define CC 0x1
90#define TC 0x02
91#define OD 0x1
92#define ERR (1 << 15)
93#define CMD_TIMEOUT (1 << 16)
94#define DATA_TIMEOUT (1 << 20)
95#define CMD_CRC (1 << 17)
96#define DATA_CRC (1 << 21)
97#define CARD_ERR (1 << 28)
98#define STAT_CLEAR 0xFFFFFFFF
99#define INIT_STREAM_CMD 0x00000000
100#define DUAL_VOLT_OCR_BIT 7
101#define SRC (1 << 25)
102#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700103#define SOFTRESET (1 << 1)
104#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100105
106/*
107 * FIXME: Most likely all the data using these _DEVID defines should come
108 * from the platform_data, or implemented in controller and slot specific
109 * functions.
110 */
111#define OMAP_MMC1_DEVID 0
112#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000113#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700114#define OMAP_MMC4_DEVID 3
115#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100117#define MMC_TIMEOUT_MS 20
118#define OMAP_MMC_MASTER_CLOCK 96000000
119#define DRIVER_NAME "mmci-omap-hs"
120
Denis Karpovdd498ef2009-09-22 16:44:49 -0700121/* Timeouts for entering power saving states on inactivity, msec */
122#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700123#define OMAP_MMC_SLEEP_TIMEOUT 1000
124#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700125
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100126/*
127 * One controller can have multiple slots, like on some omap boards using
128 * omap.c controller driver. Luckily this is not currently done on any known
129 * omap_hsmmc.c device.
130 */
131#define mmc_slot(host) (host->pdata->slots[host->slot_id])
132
133/*
134 * MMC Host controller read/write API's
135 */
136#define OMAP_HSMMC_READ(base, reg) \
137 __raw_readl((base) + OMAP_HSMMC_##reg)
138
139#define OMAP_HSMMC_WRITE(base, reg, val) \
140 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
141
Denis Karpov70a33412009-09-22 16:44:59 -0700142struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100143 struct device *dev;
144 struct mmc_host *mmc;
145 struct mmc_request *mrq;
146 struct mmc_command *cmd;
147 struct mmc_data *data;
148 struct clk *fclk;
149 struct clk *iclk;
150 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800151 /*
152 * vcc == configured supply
153 * vcc_aux == optional
154 * - MMC1, supply for DAT4..DAT7
155 * - MMC2/MMC2, external level shifter voltage supply, for
156 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
157 */
158 struct regulator *vcc;
159 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 struct work_struct mmc_carddetect_work;
161 void __iomem *base;
162 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700163 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 unsigned int id;
165 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200166 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100167 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700168 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 u32 *buffer;
170 u32 bytesleft;
171 int suspended;
172 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100173 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000174 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700176 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200177 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700178 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700179 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700180 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700181 int protect_card;
182 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700184 int req_in_progress;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700185
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100186 struct omap_mmc_platform_data *pdata;
187};
188
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800189static int omap_hsmmc_card_detect(struct device *dev, int slot)
190{
191 struct omap_mmc_platform_data *mmc = dev->platform_data;
192
193 /* NOTE: assumes card detect signal is active-low */
194 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
195}
196
197static int omap_hsmmc_get_wp(struct device *dev, int slot)
198{
199 struct omap_mmc_platform_data *mmc = dev->platform_data;
200
201 /* NOTE: assumes write protect signal is active-high */
202 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
203}
204
205static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
206{
207 struct omap_mmc_platform_data *mmc = dev->platform_data;
208
209 /* NOTE: assumes card detect signal is active-low */
210 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
211}
212
213#ifdef CONFIG_PM
214
215static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
216{
217 struct omap_mmc_platform_data *mmc = dev->platform_data;
218
219 disable_irq(mmc->slots[0].card_detect_irq);
220 return 0;
221}
222
223static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
224{
225 struct omap_mmc_platform_data *mmc = dev->platform_data;
226
227 enable_irq(mmc->slots[0].card_detect_irq);
228 return 0;
229}
230
231#else
232
233#define omap_hsmmc_suspend_cdirq NULL
234#define omap_hsmmc_resume_cdirq NULL
235
236#endif
237
Adrian Hunterb702b102010-02-15 10:03:35 -0800238#ifdef CONFIG_REGULATOR
239
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800240static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
241 int vdd)
242{
243 struct omap_hsmmc_host *host =
244 platform_get_drvdata(to_platform_device(dev));
245 int ret;
246
247 if (mmc_slot(host).before_set_reg)
248 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
249
250 if (power_on)
251 ret = mmc_regulator_set_ocr(host->vcc, vdd);
252 else
253 ret = mmc_regulator_set_ocr(host->vcc, 0);
254
255 if (mmc_slot(host).after_set_reg)
256 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
257
258 return ret;
259}
260
261static int omap_hsmmc_23_set_power(struct device *dev, int slot, int power_on,
262 int vdd)
263{
264 struct omap_hsmmc_host *host =
265 platform_get_drvdata(to_platform_device(dev));
266 int ret = 0;
267
268 /*
269 * If we don't see a Vcc regulator, assume it's a fixed
270 * voltage always-on regulator.
271 */
272 if (!host->vcc)
273 return 0;
274
275 if (mmc_slot(host).before_set_reg)
276 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
277
278 /*
279 * Assume Vcc regulator is used only to power the card ... OMAP
280 * VDDS is used to power the pins, optionally with a transceiver to
281 * support cards using voltages other than VDDS (1.8V nominal). When a
282 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
283 *
284 * In some cases this regulator won't support enable/disable;
285 * e.g. it's a fixed rail for a WLAN chip.
286 *
287 * In other cases vcc_aux switches interface power. Example, for
288 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
289 * chips/cards need an interface voltage rail too.
290 */
291 if (power_on) {
292 ret = mmc_regulator_set_ocr(host->vcc, vdd);
293 /* Enable interface voltage rail, if needed */
294 if (ret == 0 && host->vcc_aux) {
295 ret = regulator_enable(host->vcc_aux);
296 if (ret < 0)
297 ret = mmc_regulator_set_ocr(host->vcc, 0);
298 }
299 } else {
Adrian Hunter6da20c82010-02-15 10:03:34 -0800300 if (host->vcc_aux)
301 ret = regulator_disable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800302 if (ret == 0)
303 ret = mmc_regulator_set_ocr(host->vcc, 0);
304 }
305
306 if (mmc_slot(host).after_set_reg)
307 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
308
309 return ret;
310}
311
312static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
313 int vdd, int cardsleep)
314{
315 struct omap_hsmmc_host *host =
316 platform_get_drvdata(to_platform_device(dev));
317 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
318
319 return regulator_set_mode(host->vcc, mode);
320}
321
322static int omap_hsmmc_23_set_sleep(struct device *dev, int slot, int sleep,
323 int vdd, int cardsleep)
324{
325 struct omap_hsmmc_host *host =
326 platform_get_drvdata(to_platform_device(dev));
327 int err, mode;
328
329 /*
330 * If we don't see a Vcc regulator, assume it's a fixed
331 * voltage always-on regulator.
332 */
333 if (!host->vcc)
334 return 0;
335
336 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
337
338 if (!host->vcc_aux)
339 return regulator_set_mode(host->vcc, mode);
340
341 if (cardsleep) {
342 /* VCC can be turned off if card is asleep */
343 if (sleep)
344 err = mmc_regulator_set_ocr(host->vcc, 0);
345 else
346 err = mmc_regulator_set_ocr(host->vcc, vdd);
347 } else
348 err = regulator_set_mode(host->vcc, mode);
349 if (err)
350 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800351
352 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
353 return regulator_set_mode(host->vcc_aux, mode);
354
355 if (sleep)
356 return regulator_disable(host->vcc_aux);
357 else
358 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800359}
360
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800361static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
362{
363 struct regulator *reg;
364 int ret = 0;
365
366 switch (host->id) {
367 case OMAP_MMC1_DEVID:
368 /* On-chip level shifting via PBIAS0/PBIAS1 */
369 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
370 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
371 break;
372 case OMAP_MMC2_DEVID:
373 case OMAP_MMC3_DEVID:
374 /* Off-chip level shifting, or none */
375 mmc_slot(host).set_power = omap_hsmmc_23_set_power;
376 mmc_slot(host).set_sleep = omap_hsmmc_23_set_sleep;
377 break;
378 default:
379 pr_err("MMC%d configuration not supported!\n", host->id);
380 return -EINVAL;
381 }
382
383 reg = regulator_get(host->dev, "vmmc");
384 if (IS_ERR(reg)) {
385 dev_dbg(host->dev, "vmmc regulator missing\n");
386 /*
387 * HACK: until fixed.c regulator is usable,
388 * we don't require a main regulator
389 * for MMC2 or MMC3
390 */
391 if (host->id == OMAP_MMC1_DEVID) {
392 ret = PTR_ERR(reg);
393 goto err;
394 }
395 } else {
396 host->vcc = reg;
397 mmc_slot(host).ocr_mask = mmc_regulator_get_ocrmask(reg);
398
399 /* Allow an aux regulator */
400 reg = regulator_get(host->dev, "vmmc_aux");
401 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
402
403 /*
404 * UGLY HACK: workaround regulator framework bugs.
405 * When the bootloader leaves a supply active, it's
406 * initialized with zero usecount ... and we can't
407 * disable it without first enabling it. Until the
408 * framework is fixed, we need a workaround like this
409 * (which is safe for MMC, but not in general).
410 */
411 if (regulator_is_enabled(host->vcc) > 0) {
412 regulator_enable(host->vcc);
413 regulator_disable(host->vcc);
414 }
415 if (host->vcc_aux) {
416 if (regulator_is_enabled(reg) > 0) {
417 regulator_enable(reg);
418 regulator_disable(reg);
419 }
420 }
421 }
422
423 return 0;
424
425err:
426 mmc_slot(host).set_power = NULL;
427 mmc_slot(host).set_sleep = NULL;
428 return ret;
429}
430
431static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
432{
433 regulator_put(host->vcc);
434 regulator_put(host->vcc_aux);
435 mmc_slot(host).set_power = NULL;
436 mmc_slot(host).set_sleep = NULL;
437}
438
Adrian Hunterb702b102010-02-15 10:03:35 -0800439static inline int omap_hsmmc_have_reg(void)
440{
441 return 1;
442}
443
444#else
445
446static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
447{
448 return -EINVAL;
449}
450
451static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
452{
453}
454
455static inline int omap_hsmmc_have_reg(void)
456{
457 return 0;
458}
459
460#endif
461
462static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
463{
464 int ret;
465
466 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
467 pdata->suspend = omap_hsmmc_suspend_cdirq;
468 pdata->resume = omap_hsmmc_resume_cdirq;
469 if (pdata->slots[0].cover)
470 pdata->slots[0].get_cover_state =
471 omap_hsmmc_get_cover_state;
472 else
473 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
474 pdata->slots[0].card_detect_irq =
475 gpio_to_irq(pdata->slots[0].switch_pin);
476 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
477 if (ret)
478 return ret;
479 ret = gpio_direction_input(pdata->slots[0].switch_pin);
480 if (ret)
481 goto err_free_sp;
482 } else
483 pdata->slots[0].switch_pin = -EINVAL;
484
485 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
486 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
487 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
488 if (ret)
489 goto err_free_cd;
490 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
491 if (ret)
492 goto err_free_wp;
493 } else
494 pdata->slots[0].gpio_wp = -EINVAL;
495
496 return 0;
497
498err_free_wp:
499 gpio_free(pdata->slots[0].gpio_wp);
500err_free_cd:
501 if (gpio_is_valid(pdata->slots[0].switch_pin))
502err_free_sp:
503 gpio_free(pdata->slots[0].switch_pin);
504 return ret;
505}
506
507static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
508{
509 if (gpio_is_valid(pdata->slots[0].gpio_wp))
510 gpio_free(pdata->slots[0].gpio_wp);
511 if (gpio_is_valid(pdata->slots[0].switch_pin))
512 gpio_free(pdata->slots[0].switch_pin);
513}
514
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100515/*
516 * Stop clock to the card
517 */
Denis Karpov70a33412009-09-22 16:44:59 -0700518static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100519{
520 OMAP_HSMMC_WRITE(host->base, SYSCTL,
521 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
522 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
523 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
524}
525
Adrian Hunterb4175772010-05-26 14:42:06 -0700526static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host)
527{
528 unsigned int irq_mask;
529
530 if (host->use_dma)
531 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
532 else
533 irq_mask = INT_EN_MASK;
534
535 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
536 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
537 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
538}
539
540static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
541{
542 OMAP_HSMMC_WRITE(host->base, ISE, 0);
543 OMAP_HSMMC_WRITE(host->base, IE, 0);
544 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
545}
546
Denis Karpov11dd62a2009-09-22 16:44:43 -0700547#ifdef CONFIG_PM
548
549/*
550 * Restore the MMC host context, if it was lost as result of a
551 * power state change.
552 */
Denis Karpov70a33412009-09-22 16:44:59 -0700553static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700554{
555 struct mmc_ios *ios = &host->mmc->ios;
556 struct omap_mmc_platform_data *pdata = host->pdata;
557 int context_loss = 0;
558 u32 hctl, capa, con;
559 u16 dsor = 0;
560 unsigned long timeout;
561
562 if (pdata->get_context_loss_count) {
563 context_loss = pdata->get_context_loss_count(host->dev);
564 if (context_loss < 0)
565 return 1;
566 }
567
568 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
569 context_loss == host->context_loss ? "not " : "");
570 if (host->context_loss == context_loss)
571 return 1;
572
573 /* Wait for hardware reset */
574 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
575 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
576 && time_before(jiffies, timeout))
577 ;
578
579 /* Do software reset */
580 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
581 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
582 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
583 && time_before(jiffies, timeout))
584 ;
585
586 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
587 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
588
589 if (host->id == OMAP_MMC1_DEVID) {
590 if (host->power_mode != MMC_POWER_OFF &&
591 (1 << ios->vdd) <= MMC_VDD_23_24)
592 hctl = SDVS18;
593 else
594 hctl = SDVS30;
595 capa = VS30 | VS18;
596 } else {
597 hctl = SDVS18;
598 capa = VS18;
599 }
600
601 OMAP_HSMMC_WRITE(host->base, HCTL,
602 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
603
604 OMAP_HSMMC_WRITE(host->base, CAPA,
605 OMAP_HSMMC_READ(host->base, CAPA) | capa);
606
607 OMAP_HSMMC_WRITE(host->base, HCTL,
608 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
609
610 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
611 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
612 && time_before(jiffies, timeout))
613 ;
614
Adrian Hunterb4175772010-05-26 14:42:06 -0700615 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700616
617 /* Do not initialize card-specific things if the power is off */
618 if (host->power_mode == MMC_POWER_OFF)
619 goto out;
620
621 con = OMAP_HSMMC_READ(host->base, CON);
622 switch (ios->bus_width) {
623 case MMC_BUS_WIDTH_8:
624 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
625 break;
626 case MMC_BUS_WIDTH_4:
627 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
628 OMAP_HSMMC_WRITE(host->base, HCTL,
629 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
630 break;
631 case MMC_BUS_WIDTH_1:
632 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
633 OMAP_HSMMC_WRITE(host->base, HCTL,
634 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
635 break;
636 }
637
638 if (ios->clock) {
639 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
640 if (dsor < 1)
641 dsor = 1;
642
643 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
644 dsor++;
645
646 if (dsor > 250)
647 dsor = 250;
648 }
649
650 OMAP_HSMMC_WRITE(host->base, SYSCTL,
651 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
652 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
653 OMAP_HSMMC_WRITE(host->base, SYSCTL,
654 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
655
656 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
657 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
658 && time_before(jiffies, timeout))
659 ;
660
661 OMAP_HSMMC_WRITE(host->base, SYSCTL,
662 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
663
664 con = OMAP_HSMMC_READ(host->base, CON);
665 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
666 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
667 else
668 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
669out:
670 host->context_loss = context_loss;
671
672 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
673 return 0;
674}
675
676/*
677 * Save the MMC host context (store the number of power state changes so far).
678 */
Denis Karpov70a33412009-09-22 16:44:59 -0700679static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700680{
681 struct omap_mmc_platform_data *pdata = host->pdata;
682 int context_loss;
683
684 if (pdata->get_context_loss_count) {
685 context_loss = pdata->get_context_loss_count(host->dev);
686 if (context_loss < 0)
687 return;
688 host->context_loss = context_loss;
689 }
690}
691
692#else
693
Denis Karpov70a33412009-09-22 16:44:59 -0700694static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700695{
696 return 0;
697}
698
Denis Karpov70a33412009-09-22 16:44:59 -0700699static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700700{
701}
702
703#endif
704
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100705/*
706 * Send init stream sequence to card
707 * before sending IDLE command
708 */
Denis Karpov70a33412009-09-22 16:44:59 -0700709static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100710{
711 int reg = 0;
712 unsigned long timeout;
713
Adrian Hunterb62f6222009-09-22 16:45:01 -0700714 if (host->protect_card)
715 return;
716
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700718
719 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100720 OMAP_HSMMC_WRITE(host->base, CON,
721 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
722 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
723
724 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
725 while ((reg != CC) && time_before(jiffies, timeout))
726 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
727
728 OMAP_HSMMC_WRITE(host->base, CON,
729 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700730
731 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
732 OMAP_HSMMC_READ(host->base, STAT);
733
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100734 enable_irq(host->irq);
735}
736
737static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700738int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100739{
740 int r = 1;
741
Denis Karpov191d1f12009-09-22 16:44:55 -0700742 if (mmc_slot(host).get_cover_state)
743 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100744 return r;
745}
746
747static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700748omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100749 char *buf)
750{
751 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700752 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753
Denis Karpov70a33412009-09-22 16:44:59 -0700754 return sprintf(buf, "%s\n",
755 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756}
757
Denis Karpov70a33412009-09-22 16:44:59 -0700758static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759
760static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700761omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762 char *buf)
763{
764 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700765 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766
Denis Karpov191d1f12009-09-22 16:44:55 -0700767 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768}
769
Denis Karpov70a33412009-09-22 16:44:59 -0700770static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100771
772/*
773 * Configure the response type and send the cmd.
774 */
775static void
Denis Karpov70a33412009-09-22 16:44:59 -0700776omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100777 struct mmc_data *data)
778{
779 int cmdreg = 0, resptype = 0, cmdtype = 0;
780
781 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
782 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
783 host->cmd = cmd;
784
Adrian Hunterb4175772010-05-26 14:42:06 -0700785 omap_hsmmc_enable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200787 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 if (cmd->flags & MMC_RSP_PRESENT) {
789 if (cmd->flags & MMC_RSP_136)
790 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200791 else if (cmd->flags & MMC_RSP_BUSY) {
792 resptype = 3;
793 host->response_busy = 1;
794 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795 resptype = 2;
796 }
797
798 /*
799 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
800 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
801 * a val of 0x3, rest 0x0.
802 */
803 if (cmd == host->mrq->stop)
804 cmdtype = 0x3;
805
806 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
807
808 if (data) {
809 cmdreg |= DP_SELECT | MSBS | BCE;
810 if (data->flags & MMC_DATA_READ)
811 cmdreg |= DDIR;
812 else
813 cmdreg &= ~(DDIR);
814 }
815
816 if (host->use_dma)
817 cmdreg |= DMA_EN;
818
Adrian Hunterb4175772010-05-26 14:42:06 -0700819 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700820
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100821 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
822 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
823}
824
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200825static int
Denis Karpov70a33412009-09-22 16:44:59 -0700826omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200827{
828 if (data->flags & MMC_DATA_WRITE)
829 return DMA_TO_DEVICE;
830 else
831 return DMA_FROM_DEVICE;
832}
833
Adrian Hunterb4175772010-05-26 14:42:06 -0700834static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
835{
836 int dma_ch;
837
838 spin_lock(&host->irq_lock);
839 host->req_in_progress = 0;
840 dma_ch = host->dma_ch;
841 spin_unlock(&host->irq_lock);
842
843 omap_hsmmc_disable_irq(host);
844 /* Do not complete the request if DMA is still in progress */
845 if (mrq->data && host->use_dma && dma_ch != -1)
846 return;
847 host->mrq = NULL;
848 mmc_request_done(host->mmc, mrq);
849}
850
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100851/*
852 * Notify the transfer complete to MMC core
853 */
854static void
Denis Karpov70a33412009-09-22 16:44:59 -0700855omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100856{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200857 if (!data) {
858 struct mmc_request *mrq = host->mrq;
859
Adrian Hunter23050102009-09-22 16:44:57 -0700860 /* TC before CC from CMD6 - don't know why, but it happens */
861 if (host->cmd && host->cmd->opcode == 6 &&
862 host->response_busy) {
863 host->response_busy = 0;
864 return;
865 }
866
Adrian Hunterb4175772010-05-26 14:42:06 -0700867 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200868 return;
869 }
870
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100871 host->data = NULL;
872
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100873 if (!data->error)
874 data->bytes_xfered += data->blocks * (data->blksz);
875 else
876 data->bytes_xfered = 0;
877
878 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700879 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100880 return;
881 }
Denis Karpov70a33412009-09-22 16:44:59 -0700882 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883}
884
885/*
886 * Notify the core about command completion
887 */
888static void
Denis Karpov70a33412009-09-22 16:44:59 -0700889omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100890{
891 host->cmd = NULL;
892
893 if (cmd->flags & MMC_RSP_PRESENT) {
894 if (cmd->flags & MMC_RSP_136) {
895 /* response type 2 */
896 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
897 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
898 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
899 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
900 } else {
901 /* response types 1, 1b, 3, 4, 5, 6 */
902 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
903 }
904 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700905 if ((host->data == NULL && !host->response_busy) || cmd->error)
906 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907}
908
909/*
910 * DMA clean up for command errors
911 */
Denis Karpov70a33412009-09-22 16:44:59 -0700912static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913{
Adrian Hunterb4175772010-05-26 14:42:06 -0700914 int dma_ch;
915
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200916 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917
Adrian Hunterb4175772010-05-26 14:42:06 -0700918 spin_lock(&host->irq_lock);
919 dma_ch = host->dma_ch;
920 host->dma_ch = -1;
921 spin_unlock(&host->irq_lock);
922
923 if (host->use_dma && dma_ch != -1) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100924 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700925 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700926 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 }
928 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929}
930
931/*
932 * Readable error output
933 */
934#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700935static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936{
937 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700938 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
940 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
941 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
942 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
943 };
944 char res[256];
945 char *buf = res;
946 int len, i;
947
948 len = sprintf(buf, "MMC IRQ 0x%x :", status);
949 buf += len;
950
Denis Karpov70a33412009-09-22 16:44:59 -0700951 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100952 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700953 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100954 buf += len;
955 }
956
957 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
958}
959#endif /* CONFIG_MMC_DEBUG */
960
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100961/*
962 * MMC controller internal state machines reset
963 *
964 * Used to reset command or data internal state machines, using respectively
965 * SRC or SRD bit of SYSCTL register
966 * Can be called from interrupt context
967 */
Denis Karpov70a33412009-09-22 16:44:59 -0700968static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
969 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100970{
971 unsigned long i = 0;
972 unsigned long limit = (loops_per_jiffy *
973 msecs_to_jiffies(MMC_TIMEOUT_MS));
974
975 OMAP_HSMMC_WRITE(host->base, SYSCTL,
976 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
977
978 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
979 (i++ < limit))
980 cpu_relax();
981
982 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
983 dev_err(mmc_dev(host->mmc),
984 "Timeout waiting on controller reset in %s\n",
985 __func__);
986}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100987
Adrian Hunterb4175772010-05-26 14:42:06 -0700988static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100989{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100990 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700991 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992
Adrian Hunterb4175772010-05-26 14:42:06 -0700993 if (!host->req_in_progress) {
994 do {
995 OMAP_HSMMC_WRITE(host->base, STAT, status);
996 /* Flush posted write */
997 status = OMAP_HSMMC_READ(host->base, STAT);
998 } while (status & INT_EN_MASK);
999 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000 }
1001
1002 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1004
1005 if (status & ERR) {
1006#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001007 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001008#endif
1009 if ((status & CMD_TIMEOUT) ||
1010 (status & CMD_CRC)) {
1011 if (host->cmd) {
1012 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001013 omap_hsmmc_reset_controller_fsm(host,
1014 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001015 host->cmd->error = -ETIMEDOUT;
1016 } else {
1017 host->cmd->error = -EILSEQ;
1018 }
1019 end_cmd = 1;
1020 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001021 if (host->data || host->response_busy) {
1022 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001023 omap_hsmmc_dma_cleanup(host,
1024 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001025 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001026 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001027 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001028 }
1029 if ((status & DATA_TIMEOUT) ||
1030 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001031 if (host->data || host->response_busy) {
1032 int err = (status & DATA_TIMEOUT) ?
1033 -ETIMEDOUT : -EILSEQ;
1034
1035 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001036 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001037 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001038 host->mrq->cmd->error = err;
1039 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001040 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001041 end_trans = 1;
1042 }
1043 }
1044 if (status & CARD_ERR) {
1045 dev_dbg(mmc_dev(host->mmc),
1046 "Ignoring card err CMD%d\n", host->cmd->opcode);
1047 if (host->cmd)
1048 end_cmd = 1;
1049 if (host->data)
1050 end_trans = 1;
1051 }
1052 }
1053
1054 OMAP_HSMMC_WRITE(host->base, STAT, status);
1055
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001056 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001057 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001058 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001059 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001060}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061
Adrian Hunterb4175772010-05-26 14:42:06 -07001062/*
1063 * MMC controller IRQ handler
1064 */
1065static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1066{
1067 struct omap_hsmmc_host *host = dev_id;
1068 int status;
1069
1070 status = OMAP_HSMMC_READ(host->base, STAT);
1071 do {
1072 omap_hsmmc_do_irq(host, status);
1073 /* Flush posted write */
1074 status = OMAP_HSMMC_READ(host->base, STAT);
1075 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001076
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001077 return IRQ_HANDLED;
1078}
1079
Denis Karpov70a33412009-09-22 16:44:59 -07001080static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001081{
1082 unsigned long i;
1083
1084 OMAP_HSMMC_WRITE(host->base, HCTL,
1085 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1086 for (i = 0; i < loops_per_jiffy; i++) {
1087 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1088 break;
1089 cpu_relax();
1090 }
1091}
1092
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093/*
David Brownelleb250822009-02-17 14:49:01 -08001094 * Switch MMC interface voltage ... only relevant for MMC1.
1095 *
1096 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1097 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1098 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099 */
Denis Karpov70a33412009-09-22 16:44:59 -07001100static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101{
1102 u32 reg_val = 0;
1103 int ret;
1104
1105 /* Disable the clocks */
1106 clk_disable(host->fclk);
1107 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001108 if (host->got_dbclk)
1109 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110
1111 /* Turn the power off */
1112 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113
1114 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001115 if (!ret)
1116 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1117 vdd);
1118 clk_enable(host->iclk);
1119 clk_enable(host->fclk);
1120 if (host->got_dbclk)
1121 clk_enable(host->dbclk);
1122
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001123 if (ret != 0)
1124 goto err;
1125
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001126 OMAP_HSMMC_WRITE(host->base, HCTL,
1127 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1128 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001129
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001130 /*
1131 * If a MMC dual voltage card is detected, the set_ios fn calls
1132 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001133 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001134 *
David Brownelleb250822009-02-17 14:49:01 -08001135 * Cope with a bit of slop in the range ... per data sheets:
1136 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1137 * but recommended values are 1.71V to 1.89V
1138 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1139 * but recommended values are 2.7V to 3.3V
1140 *
1141 * Board setup code shouldn't permit anything very out-of-range.
1142 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1143 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144 */
David Brownelleb250822009-02-17 14:49:01 -08001145 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001146 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001147 else
1148 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001149
1150 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001151 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152
1153 return 0;
1154err:
1155 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1156 return ret;
1157}
1158
Adrian Hunterb62f6222009-09-22 16:45:01 -07001159/* Protect the card while the cover is open */
1160static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1161{
1162 if (!mmc_slot(host).get_cover_state)
1163 return;
1164
1165 host->reqs_blocked = 0;
1166 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1167 if (host->protect_card) {
1168 printk(KERN_INFO "%s: cover is closed, "
1169 "card is now accessible\n",
1170 mmc_hostname(host->mmc));
1171 host->protect_card = 0;
1172 }
1173 } else {
1174 if (!host->protect_card) {
1175 printk(KERN_INFO "%s: cover is open, "
1176 "card is now inaccessible\n",
1177 mmc_hostname(host->mmc));
1178 host->protect_card = 1;
1179 }
1180 }
1181}
1182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183/*
1184 * Work Item to notify the core about card insertion/removal
1185 */
Denis Karpov70a33412009-09-22 16:44:59 -07001186static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001187{
Denis Karpov70a33412009-09-22 16:44:59 -07001188 struct omap_hsmmc_host *host =
1189 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001190 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001191 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001192
Adrian Huntera6b22402009-09-22 16:44:45 -07001193 if (host->suspended)
1194 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195
1196 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001197
Denis Karpov191d1f12009-09-22 16:44:55 -07001198 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001199 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001200 else {
1201 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001202 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001203 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001204
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001205 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001207 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001208 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001209}
1210
1211/*
1212 * ISR for handling card insertion and removal
1213 */
Denis Karpov70a33412009-09-22 16:44:59 -07001214static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001215{
Denis Karpov70a33412009-09-22 16:44:59 -07001216 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001217
Adrian Huntera6b22402009-09-22 16:44:45 -07001218 if (host->suspended)
1219 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 schedule_work(&host->mmc_carddetect_work);
1221
1222 return IRQ_HANDLED;
1223}
1224
Denis Karpov70a33412009-09-22 16:44:59 -07001225static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001226 struct mmc_data *data)
1227{
1228 int sync_dev;
1229
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001230 if (data->flags & MMC_DATA_WRITE)
1231 sync_dev = host->dma_line_tx;
1232 else
1233 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001234 return sync_dev;
1235}
1236
Denis Karpov70a33412009-09-22 16:44:59 -07001237static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001238 struct mmc_data *data,
1239 struct scatterlist *sgl)
1240{
1241 int blksz, nblk, dma_ch;
1242
1243 dma_ch = host->dma_ch;
1244 if (data->flags & MMC_DATA_WRITE) {
1245 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1246 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1247 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1248 sg_dma_address(sgl), 0, 0);
1249 } else {
1250 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001251 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001252 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1253 sg_dma_address(sgl), 0, 0);
1254 }
1255
1256 blksz = host->data->blksz;
1257 nblk = sg_dma_len(sgl) / blksz;
1258
1259 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1260 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001261 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001262 !(data->flags & MMC_DATA_WRITE));
1263
1264 omap_start_dma(dma_ch);
1265}
1266
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267/*
1268 * DMA call back function
1269 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001270static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271{
Adrian Hunterb4175772010-05-26 14:42:06 -07001272 struct omap_hsmmc_host *host = cb_data;
1273 struct mmc_data *data = host->mrq->data;
1274 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275
Venkatraman Sf3584e52010-08-10 18:01:54 -07001276 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1277 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1278 ch_status);
1279 return;
1280 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281
Adrian Hunterb4175772010-05-26 14:42:06 -07001282 spin_lock(&host->irq_lock);
1283 if (host->dma_ch < 0) {
1284 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001285 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001286 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001288 host->dma_sg_idx++;
1289 if (host->dma_sg_idx < host->dma_len) {
1290 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001291 omap_hsmmc_config_dma_params(host, data,
1292 data->sg + host->dma_sg_idx);
1293 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001294 return;
1295 }
1296
Adrian Hunterb4175772010-05-26 14:42:06 -07001297 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
1298 omap_hsmmc_get_dma_dir(host, data));
1299
1300 req_in_progress = host->req_in_progress;
1301 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001302 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001303 spin_unlock(&host->irq_lock);
1304
1305 omap_free_dma(dma_ch);
1306
1307 /* If DMA has finished after TC, complete the request */
1308 if (!req_in_progress) {
1309 struct mmc_request *mrq = host->mrq;
1310
1311 host->mrq = NULL;
1312 mmc_request_done(host->mmc, mrq);
1313 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001314}
1315
1316/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001317 * Routine to configure and start DMA for the MMC card
1318 */
Denis Karpov70a33412009-09-22 16:44:59 -07001319static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1320 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001321{
Adrian Hunterb4175772010-05-26 14:42:06 -07001322 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323 struct mmc_data *data = req->data;
1324
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001325 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001326 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001327 struct scatterlist *sgl;
1328
1329 sgl = data->sg + i;
1330 if (sgl->length % data->blksz)
1331 return -EINVAL;
1332 }
1333 if ((data->blksz % 4) != 0)
1334 /* REVISIT: The MMC buffer increments only when MSB is written.
1335 * Return error for blksz which is non multiple of four.
1336 */
1337 return -EINVAL;
1338
Adrian Hunterb4175772010-05-26 14:42:06 -07001339 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001340
Denis Karpov70a33412009-09-22 16:44:59 -07001341 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1342 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001343 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001344 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001345 "%s: omap_request_dma() failed with %d\n",
1346 mmc_hostname(host->mmc), ret);
1347 return ret;
1348 }
1349
1350 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001351 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001353 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354
Denis Karpov70a33412009-09-22 16:44:59 -07001355 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001356
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001357 return 0;
1358}
1359
Denis Karpov70a33412009-09-22 16:44:59 -07001360static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001361 unsigned int timeout_ns,
1362 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363{
1364 unsigned int timeout, cycle_ns;
1365 uint32_t reg, clkd, dto = 0;
1366
1367 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1368 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1369 if (clkd == 0)
1370 clkd = 1;
1371
1372 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001373 timeout = timeout_ns / cycle_ns;
1374 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375 if (timeout) {
1376 while ((timeout & 0x80000000) == 0) {
1377 dto += 1;
1378 timeout <<= 1;
1379 }
1380 dto = 31 - dto;
1381 timeout <<= 1;
1382 if (timeout && dto)
1383 dto += 1;
1384 if (dto >= 13)
1385 dto -= 13;
1386 else
1387 dto = 0;
1388 if (dto > 14)
1389 dto = 14;
1390 }
1391
1392 reg &= ~DTO_MASK;
1393 reg |= dto << DTO_SHIFT;
1394 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1395}
1396
1397/*
1398 * Configure block length for MMC/SD cards and initiate the transfer.
1399 */
1400static int
Denis Karpov70a33412009-09-22 16:44:59 -07001401omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402{
1403 int ret;
1404 host->data = req->data;
1405
1406 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001407 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001408 /*
1409 * Set an arbitrary 100ms data timeout for commands with
1410 * busy signal.
1411 */
1412 if (req->cmd->flags & MMC_RSP_BUSY)
1413 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414 return 0;
1415 }
1416
1417 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1418 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001419 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001422 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001423 if (ret != 0) {
1424 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1425 return ret;
1426 }
1427 }
1428 return 0;
1429}
1430
1431/*
1432 * Request function. for read/write operation
1433 */
Denis Karpov70a33412009-09-22 16:44:59 -07001434static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435{
Denis Karpov70a33412009-09-22 16:44:59 -07001436 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001437 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438
Adrian Hunterb4175772010-05-26 14:42:06 -07001439 BUG_ON(host->req_in_progress);
1440 BUG_ON(host->dma_ch != -1);
1441 if (host->protect_card) {
1442 if (host->reqs_blocked < 3) {
1443 /*
1444 * Ensure the controller is left in a consistent
1445 * state by resetting the command and data state
1446 * machines.
1447 */
1448 omap_hsmmc_reset_controller_fsm(host, SRD);
1449 omap_hsmmc_reset_controller_fsm(host, SRC);
1450 host->reqs_blocked += 1;
1451 }
1452 req->cmd->error = -EBADF;
1453 if (req->data)
1454 req->data->error = -EBADF;
1455 req->cmd->retries = 0;
1456 mmc_request_done(mmc, req);
1457 return;
1458 } else if (host->reqs_blocked)
1459 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001460 WARN_ON(host->mrq != NULL);
1461 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001462 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001463 if (err) {
1464 req->cmd->error = err;
1465 if (req->data)
1466 req->data->error = err;
1467 host->mrq = NULL;
1468 mmc_request_done(mmc, req);
1469 return;
1470 }
1471
Denis Karpov70a33412009-09-22 16:44:59 -07001472 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473}
1474
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001475/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001476static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001477{
Denis Karpov70a33412009-09-22 16:44:59 -07001478 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001479 u16 dsor = 0;
1480 unsigned long regval;
1481 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001482 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001483 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001484
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001485 mmc_host_enable(host->mmc);
1486
Adrian Huntera3621462009-09-22 16:44:42 -07001487 if (ios->power_mode != host->power_mode) {
1488 switch (ios->power_mode) {
1489 case MMC_POWER_OFF:
1490 mmc_slot(host).set_power(host->dev, host->slot_id,
1491 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001492 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001493 break;
1494 case MMC_POWER_UP:
1495 mmc_slot(host).set_power(host->dev, host->slot_id,
1496 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001497 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001498 break;
1499 case MMC_POWER_ON:
1500 do_send_init_stream = 1;
1501 break;
1502 }
1503 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001504 }
1505
Denis Karpovdd498ef2009-09-22 16:44:49 -07001506 /* FIXME: set registers based only on changes to ios */
1507
Jarkko Lavinen73153012008-11-21 16:49:54 +02001508 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001510 case MMC_BUS_WIDTH_8:
1511 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1512 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001514 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515 OMAP_HSMMC_WRITE(host->base, HCTL,
1516 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1517 break;
1518 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001519 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 OMAP_HSMMC_WRITE(host->base, HCTL,
1521 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1522 break;
1523 }
1524
1525 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001526 /* Only MMC1 can interface at 3V without some flavor
1527 * of external transceiver; but they all handle 1.8V.
1528 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001529 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1530 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1531 /*
1532 * The mmc_select_voltage fn of the core does
1533 * not seem to set the power_mode to
1534 * MMC_POWER_UP upon recalculating the voltage.
1535 * vdd 1.8v.
1536 */
Denis Karpov70a33412009-09-22 16:44:59 -07001537 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1538 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001539 "Switch operation failed\n");
1540 }
1541 }
1542
1543 if (ios->clock) {
1544 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1545 if (dsor < 1)
1546 dsor = 1;
1547
1548 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1549 dsor++;
1550
1551 if (dsor > 250)
1552 dsor = 250;
1553 }
Denis Karpov70a33412009-09-22 16:44:59 -07001554 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1556 regval = regval & ~(CLKD_MASK);
1557 regval = regval | (dsor << 6) | (DTO << 16);
1558 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1559 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1560 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1561
1562 /* Wait till the ICS bit is set */
1563 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001564 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565 && time_before(jiffies, timeout))
1566 msleep(1);
1567
1568 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1569 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1570
Adrian Huntera3621462009-09-22 16:44:42 -07001571 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001572 send_init_stream(host);
1573
Denis Karpovabb28e72009-09-22 16:44:44 -07001574 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001575 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001576 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1577 else
1578 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001579
Denis Karpovdd498ef2009-09-22 16:44:49 -07001580 if (host->power_mode == MMC_POWER_OFF)
1581 mmc_host_disable(host->mmc);
1582 else
1583 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584}
1585
1586static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1587{
Denis Karpov70a33412009-09-22 16:44:59 -07001588 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001589
Denis Karpov191d1f12009-09-22 16:44:55 -07001590 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001591 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001592 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593}
1594
1595static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1596{
Denis Karpov70a33412009-09-22 16:44:59 -07001597 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598
Denis Karpov191d1f12009-09-22 16:44:55 -07001599 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001601 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602}
1603
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001604static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1605{
1606 struct omap_hsmmc_host *host = mmc_priv(mmc);
1607
1608 if (mmc_slot(host).init_card)
1609 mmc_slot(host).init_card(card);
1610}
1611
Denis Karpov70a33412009-09-22 16:44:59 -07001612static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001613{
1614 u32 hctl, capa, value;
1615
1616 /* Only MMC1 supports 3.0V */
1617 if (host->id == OMAP_MMC1_DEVID) {
1618 hctl = SDVS30;
1619 capa = VS30 | VS18;
1620 } else {
1621 hctl = SDVS18;
1622 capa = VS18;
1623 }
1624
1625 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1626 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1627
1628 value = OMAP_HSMMC_READ(host->base, CAPA);
1629 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1630
1631 /* Set the controller to AUTO IDLE mode */
1632 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1633 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1634
1635 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001636 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001637}
1638
Denis Karpovdd498ef2009-09-22 16:44:49 -07001639/*
1640 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001641 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1642 * ^___________| | |
1643 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001644 *
1645 * ENABLED: mmc host is fully functional
1646 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001647 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1648 * REGSLEEP: fclk is off, voltage regulator is asleep
1649 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001650 *
1651 * Transition handlers return the timeout for the next state transition
1652 * or negative error.
1653 */
1654
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001655enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001656
1657/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001658static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001659{
Denis Karpov70a33412009-09-22 16:44:59 -07001660 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001661 clk_disable(host->fclk);
1662 host->dpm_state = DISABLED;
1663
1664 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1665
1666 if (host->power_mode == MMC_POWER_OFF)
1667 return 0;
1668
Adrian Hunter4380eea2010-02-15 10:03:34 -08001669 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001670}
1671
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001672/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001673static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001674{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001675 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001676
1677 if (!mmc_try_claim_host(host->mmc))
1678 return 0;
1679
1680 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001681 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001682 if (mmc_card_can_sleep(host->mmc)) {
1683 err = mmc_card_sleep(host->mmc);
1684 if (err < 0) {
1685 clk_disable(host->fclk);
1686 mmc_release_host(host->mmc);
1687 return err;
1688 }
1689 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001690 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001691 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001692 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001693 if (mmc_slot(host).set_sleep)
1694 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1695 new_state == CARDSLEEP);
1696 /* FIXME: turn off bus power and perhaps interrupts too */
1697 clk_disable(host->fclk);
1698 host->dpm_state = new_state;
1699
1700 mmc_release_host(host->mmc);
1701
1702 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1703 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001704
Adrian Hunter1df58db2010-02-15 10:03:34 -08001705 if (mmc_slot(host).no_off)
1706 return 0;
1707
Denis Karpovdd498ef2009-09-22 16:44:49 -07001708 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1709 mmc_slot(host).card_detect ||
1710 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001711 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea2010-02-15 10:03:34 -08001712 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001713
1714 return 0;
1715}
1716
1717/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001718static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001719{
1720 if (!mmc_try_claim_host(host->mmc))
1721 return 0;
1722
Adrian Hunter1df58db2010-02-15 10:03:34 -08001723 if (mmc_slot(host).no_off)
1724 return 0;
1725
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001726 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1727 mmc_slot(host).card_detect ||
1728 (mmc_slot(host).get_cover_state &&
1729 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1730 mmc_release_host(host->mmc);
1731 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001732 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001733
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001734 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1735 host->vdd = 0;
1736 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001737
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001738 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1739 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001740
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001741 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001742
1743 mmc_release_host(host->mmc);
1744
1745 return 0;
1746}
1747
1748/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001749static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001750{
1751 int err;
1752
1753 err = clk_enable(host->fclk);
1754 if (err < 0)
1755 return err;
1756
Denis Karpov70a33412009-09-22 16:44:59 -07001757 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001758 host->dpm_state = ENABLED;
1759
1760 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1761
1762 return 0;
1763}
1764
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001765/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001766static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001767{
1768 if (!mmc_try_claim_host(host->mmc))
1769 return 0;
1770
1771 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001772 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001773 if (mmc_slot(host).set_sleep)
1774 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1775 host->vdd, host->dpm_state == CARDSLEEP);
1776 if (mmc_card_can_sleep(host->mmc))
1777 mmc_card_awake(host->mmc);
1778
1779 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1780 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1781
1782 host->dpm_state = ENABLED;
1783
1784 mmc_release_host(host->mmc);
1785
1786 return 0;
1787}
1788
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001790static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001791{
1792 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001793
Denis Karpov70a33412009-09-22 16:44:59 -07001794 omap_hsmmc_context_restore(host);
1795 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001796 mmc_power_restore_host(host->mmc);
1797
1798 host->dpm_state = ENABLED;
1799
1800 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1801
1802 return 0;
1803}
1804
1805/*
1806 * Bring MMC host to ENABLED from any other PM state.
1807 */
Denis Karpov70a33412009-09-22 16:44:59 -07001808static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001809{
Denis Karpov70a33412009-09-22 16:44:59 -07001810 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001811
1812 switch (host->dpm_state) {
1813 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001814 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001815 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001816 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001817 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001819 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001820 default:
1821 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1822 return -EINVAL;
1823 }
1824}
1825
1826/*
1827 * Bring MMC host in PM state (one level deeper).
1828 */
Denis Karpov70a33412009-09-22 16:44:59 -07001829static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001830{
Denis Karpov70a33412009-09-22 16:44:59 -07001831 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001832
1833 switch (host->dpm_state) {
1834 case ENABLED: {
1835 int delay;
1836
Denis Karpov70a33412009-09-22 16:44:59 -07001837 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838 if (lazy || delay < 0)
1839 return delay;
1840 return 0;
1841 }
1842 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001843 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001844 case CARDSLEEP:
1845 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001846 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001847 default:
1848 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1849 return -EINVAL;
1850 }
1851}
1852
Denis Karpov70a33412009-09-22 16:44:59 -07001853static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001854{
Denis Karpov70a33412009-09-22 16:44:59 -07001855 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001856 int err;
1857
1858 err = clk_enable(host->fclk);
1859 if (err)
1860 return err;
1861 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001862 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001863 return 0;
1864}
1865
Denis Karpov70a33412009-09-22 16:44:59 -07001866static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001867{
Denis Karpov70a33412009-09-22 16:44:59 -07001868 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001869
Denis Karpov70a33412009-09-22 16:44:59 -07001870 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001871 clk_disable(host->fclk);
1872 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1873 return 0;
1874}
1875
Denis Karpov70a33412009-09-22 16:44:59 -07001876static const struct mmc_host_ops omap_hsmmc_ops = {
1877 .enable = omap_hsmmc_enable_fclk,
1878 .disable = omap_hsmmc_disable_fclk,
1879 .request = omap_hsmmc_request,
1880 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001881 .get_cd = omap_hsmmc_get_cd,
1882 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001883 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001884 /* NYET -- enable_sdio_irq */
1885};
1886
Denis Karpov70a33412009-09-22 16:44:59 -07001887static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1888 .enable = omap_hsmmc_enable,
1889 .disable = omap_hsmmc_disable,
1890 .request = omap_hsmmc_request,
1891 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892 .get_cd = omap_hsmmc_get_cd,
1893 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001894 .init_card = omap_hsmmc_init_card,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001895 /* NYET -- enable_sdio_irq */
1896};
1897
Denis Karpovd900f712009-09-22 16:44:38 -07001898#ifdef CONFIG_DEBUG_FS
1899
Denis Karpov70a33412009-09-22 16:44:59 -07001900static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001901{
1902 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001903 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001904 int context_loss = 0;
1905
Denis Karpov70a33412009-09-22 16:44:59 -07001906 if (host->pdata->get_context_loss_count)
1907 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001908
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001909 seq_printf(s, "mmc%d:\n"
1910 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001911 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001912 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001913 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001914 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001915 mmc->index, mmc->enabled ? 1 : 0,
1916 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001917 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001918
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001919 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001920 seq_printf(s, "host suspended, can't read registers\n");
1921 return 0;
1922 }
1923
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001924 if (clk_enable(host->fclk) != 0) {
1925 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001926 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001927 }
Denis Karpovd900f712009-09-22 16:44:38 -07001928
1929 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1930 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1931 seq_printf(s, "CON:\t\t0x%08x\n",
1932 OMAP_HSMMC_READ(host->base, CON));
1933 seq_printf(s, "HCTL:\t\t0x%08x\n",
1934 OMAP_HSMMC_READ(host->base, HCTL));
1935 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1936 OMAP_HSMMC_READ(host->base, SYSCTL));
1937 seq_printf(s, "IE:\t\t0x%08x\n",
1938 OMAP_HSMMC_READ(host->base, IE));
1939 seq_printf(s, "ISE:\t\t0x%08x\n",
1940 OMAP_HSMMC_READ(host->base, ISE));
1941 seq_printf(s, "CAPA:\t\t0x%08x\n",
1942 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001943
1944 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001945
Denis Karpovd900f712009-09-22 16:44:38 -07001946 return 0;
1947}
1948
Denis Karpov70a33412009-09-22 16:44:59 -07001949static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001950{
Denis Karpov70a33412009-09-22 16:44:59 -07001951 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001952}
1953
1954static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001955 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001956 .read = seq_read,
1957 .llseek = seq_lseek,
1958 .release = single_release,
1959};
1960
Denis Karpov70a33412009-09-22 16:44:59 -07001961static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001962{
1963 if (mmc->debugfs_root)
1964 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1965 mmc, &mmc_regs_fops);
1966}
1967
1968#else
1969
Denis Karpov70a33412009-09-22 16:44:59 -07001970static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001971{
1972}
1973
1974#endif
1975
Denis Karpov70a33412009-09-22 16:44:59 -07001976static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001977{
1978 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1979 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001980 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001982 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983
1984 if (pdata == NULL) {
1985 dev_err(&pdev->dev, "Platform Data is missing\n");
1986 return -ENXIO;
1987 }
1988
1989 if (pdata->nr_slots == 0) {
1990 dev_err(&pdev->dev, "No Slots\n");
1991 return -ENXIO;
1992 }
1993
1994 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1995 irq = platform_get_irq(pdev, 0);
1996 if (res == NULL || irq < 0)
1997 return -ENXIO;
1998
1999 res = request_mem_region(res->start, res->end - res->start + 1,
2000 pdev->name);
2001 if (res == NULL)
2002 return -EBUSY;
2003
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002004 ret = omap_hsmmc_gpio_init(pdata);
2005 if (ret)
2006 goto err;
2007
Denis Karpov70a33412009-09-22 16:44:59 -07002008 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002009 if (!mmc) {
2010 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002011 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002012 }
2013
2014 host = mmc_priv(mmc);
2015 host->mmc = mmc;
2016 host->pdata = pdata;
2017 host->dev = &pdev->dev;
2018 host->use_dma = 1;
2019 host->dev->dma_mask = &pdata->dma_mask;
2020 host->dma_ch = -1;
2021 host->irq = irq;
2022 host->id = pdev->id;
2023 host->slot_id = 0;
2024 host->mapbase = res->start;
2025 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002026 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027
2028 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002029 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030
Denis Karpov191d1f12009-09-22 16:44:55 -07002031 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002032 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002033 else
Denis Karpov70a33412009-09-22 16:44:59 -07002034 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002035
Adrian Huntere0eb2422010-02-15 10:03:34 -08002036 /*
2037 * If regulator_disable can only put vcc_aux to sleep then there is
2038 * no off state.
2039 */
2040 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2041 mmc_slot(host).no_off = 1;
2042
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002043 mmc->f_min = 400000;
2044 mmc->f_max = 52000000;
2045
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002046 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047
Russell King6f7607c2009-01-28 10:22:50 +00002048 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049 if (IS_ERR(host->iclk)) {
2050 ret = PTR_ERR(host->iclk);
2051 host->iclk = NULL;
2052 goto err1;
2053 }
Russell King6f7607c2009-01-28 10:22:50 +00002054 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 if (IS_ERR(host->fclk)) {
2056 ret = PTR_ERR(host->fclk);
2057 host->fclk = NULL;
2058 clk_put(host->iclk);
2059 goto err1;
2060 }
2061
Denis Karpov70a33412009-09-22 16:44:59 -07002062 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002063
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002064 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002065 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2066 /* we start off in DISABLED state */
2067 host->dpm_state = DISABLED;
2068
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002069 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070 clk_put(host->iclk);
2071 clk_put(host->fclk);
2072 goto err1;
2073 }
2074
2075 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002076 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077 clk_put(host->iclk);
2078 clk_put(host->fclk);
2079 goto err1;
2080 }
2081
Adrian Hunter2bec0892009-09-22 16:45:02 -07002082 if (cpu_is_omap2430()) {
2083 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2084 /*
2085 * MMC can still work without debounce clock.
2086 */
2087 if (IS_ERR(host->dbclk))
2088 dev_warn(mmc_dev(host->mmc),
2089 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002090 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002091 host->got_dbclk = 1;
2092
2093 if (host->got_dbclk)
2094 if (clk_enable(host->dbclk) != 0)
2095 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2096 " clk failed\n");
2097 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002098
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002099 /* Since we do only SG emulation, we can have as many segs
2100 * as we want. */
2101 mmc->max_phys_segs = 1024;
2102 mmc->max_hw_segs = 1024;
2103
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2105 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2106 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2107 mmc->max_seg_size = mmc->max_req_size;
2108
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002109 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
2110 MMC_CAP_WAIT_WHILE_BUSY;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002112 switch (mmc_slot(host).wires) {
2113 case 8:
Jarkko Lavinen73153012008-11-21 16:49:54 +02002114 mmc->caps |= MMC_CAP_8_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002115 /* Fall through */
2116 case 4:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117 mmc->caps |= MMC_CAP_4_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002118 break;
2119 case 1:
2120 /* Nothing to crib here */
2121 case 0:
2122 /* Assuming nothing was given by board, Core use's 1-Bit */
2123 break;
2124 default:
2125 /* Completely unexpected.. Core goes with 1-Bit Width */
2126 dev_crit(mmc_dev(host->mmc), "Invalid width %d\n used!"
2127 "using 1 instead\n", mmc_slot(host).wires);
2128 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129
Denis Karpov191d1f12009-09-22 16:44:55 -07002130 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002131 mmc->caps |= MMC_CAP_NONREMOVABLE;
2132
Denis Karpov70a33412009-09-22 16:44:59 -07002133 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002135 /* Select DMA lines */
2136 switch (host->id) {
2137 case OMAP_MMC1_DEVID:
2138 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2139 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2140 break;
2141 case OMAP_MMC2_DEVID:
2142 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2143 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2144 break;
2145 case OMAP_MMC3_DEVID:
2146 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2147 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2148 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002149 case OMAP_MMC4_DEVID:
2150 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2151 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2152 break;
2153 case OMAP_MMC5_DEVID:
2154 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2155 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2156 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002157 default:
2158 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2159 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160 }
2161
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002163 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002164 mmc_hostname(mmc), host);
2165 if (ret) {
2166 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2167 goto err_irq;
2168 }
2169
2170 if (pdata->init != NULL) {
2171 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002172 dev_dbg(mmc_dev(host->mmc),
2173 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174 goto err_irq_cd_init;
2175 }
2176 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002177
Adrian Hunterb702b102010-02-15 10:03:35 -08002178 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002179 ret = omap_hsmmc_reg_get(host);
2180 if (ret)
2181 goto err_reg;
2182 host->use_reg = 1;
2183 }
2184
David Brownellb583f262009-05-28 14:04:03 -07002185 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002186
2187 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002188 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002189 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002190 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002191 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2192 | IRQF_DISABLED,
2193 mmc_hostname(mmc), host);
2194 if (ret) {
2195 dev_dbg(mmc_dev(host->mmc),
2196 "Unable to grab MMC CD IRQ\n");
2197 goto err_irq_cd;
2198 }
2199 }
2200
Adrian Hunterb4175772010-05-26 14:42:06 -07002201 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002203 mmc_host_lazy_disable(host->mmc);
2204
Adrian Hunterb62f6222009-09-22 16:45:01 -07002205 omap_hsmmc_protect_card(host);
2206
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207 mmc_add_host(mmc);
2208
Denis Karpov191d1f12009-09-22 16:44:55 -07002209 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2211 if (ret < 0)
2212 goto err_slot_name;
2213 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002214 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002215 ret = device_create_file(&mmc->class_dev,
2216 &dev_attr_cover_switch);
2217 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002218 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219 }
2220
Denis Karpov70a33412009-09-22 16:44:59 -07002221 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002222
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002223 return 0;
2224
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002225err_slot_name:
2226 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002228err_irq_cd:
2229 if (host->use_reg)
2230 omap_hsmmc_reg_put(host);
2231err_reg:
2232 if (host->pdata->cleanup)
2233 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234err_irq_cd_init:
2235 free_irq(host->irq, host);
2236err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002237 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002238 clk_disable(host->iclk);
2239 clk_put(host->fclk);
2240 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002241 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242 clk_disable(host->dbclk);
2243 clk_put(host->dbclk);
2244 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245err1:
2246 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002247 platform_set_drvdata(pdev, NULL);
2248 mmc_free_host(mmc);
2249err_alloc:
2250 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252 release_mem_region(res->start, res->end - res->start + 1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002253 return ret;
2254}
2255
Denis Karpov70a33412009-09-22 16:44:59 -07002256static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002257{
Denis Karpov70a33412009-09-22 16:44:59 -07002258 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002259 struct resource *res;
2260
2261 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002262 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002264 if (host->use_reg)
2265 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266 if (host->pdata->cleanup)
2267 host->pdata->cleanup(&pdev->dev);
2268 free_irq(host->irq, host);
2269 if (mmc_slot(host).card_detect_irq)
2270 free_irq(mmc_slot(host).card_detect_irq, host);
2271 flush_scheduled_work();
2272
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002273 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274 clk_disable(host->iclk);
2275 clk_put(host->fclk);
2276 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002277 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002278 clk_disable(host->dbclk);
2279 clk_put(host->dbclk);
2280 }
2281
2282 mmc_free_host(host->mmc);
2283 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002284 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002285 }
2286
2287 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2288 if (res)
2289 release_mem_region(res->start, res->end - res->start + 1);
2290 platform_set_drvdata(pdev, NULL);
2291
2292 return 0;
2293}
2294
2295#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002296static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002297{
2298 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002299 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002300 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Kevin Hilmana791daa2010-05-26 14:42:07 -07002301 pm_message_t state = PMSG_SUSPEND; /* unused by MMC core */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002302
2303 if (host && host->suspended)
2304 return 0;
2305
2306 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002307 host->suspended = 1;
2308 if (host->pdata->suspend) {
2309 ret = host->pdata->suspend(&pdev->dev,
2310 host->slot_id);
2311 if (ret) {
2312 dev_dbg(mmc_dev(host->mmc),
2313 "Unable to handle MMC board"
2314 " level suspend\n");
2315 host->suspended = 0;
2316 return ret;
2317 }
2318 }
2319 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002320 mmc_host_enable(host->mmc);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002321 ret = mmc_suspend_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002322 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002323 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002324 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002325 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002326 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002327 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002328 if (host->got_dbclk)
2329 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002330 } else {
2331 host->suspended = 0;
2332 if (host->pdata->resume) {
2333 ret = host->pdata->resume(&pdev->dev,
2334 host->slot_id);
2335 if (ret)
2336 dev_dbg(mmc_dev(host->mmc),
2337 "Unmask interrupt failed\n");
2338 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002339 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002340 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002341
2342 }
2343 return ret;
2344}
2345
2346/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002347static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002348{
2349 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002350 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002351 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002352
2353 if (host && !host->suspended)
2354 return 0;
2355
2356 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002357 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002358 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002359 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002360
Denis Karpov11dd62a2009-09-22 16:44:43 -07002361 if (mmc_host_enable(host->mmc) != 0) {
2362 clk_disable(host->iclk);
2363 goto clk_en_err;
2364 }
2365
Adrian Hunter2bec0892009-09-22 16:45:02 -07002366 if (host->got_dbclk)
2367 clk_enable(host->dbclk);
2368
Denis Karpov70a33412009-09-22 16:44:59 -07002369 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002370
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002371 if (host->pdata->resume) {
2372 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2373 if (ret)
2374 dev_dbg(mmc_dev(host->mmc),
2375 "Unmask interrupt failed\n");
2376 }
2377
Adrian Hunterb62f6222009-09-22 16:45:01 -07002378 omap_hsmmc_protect_card(host);
2379
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002380 /* Notify the core to resume the host */
2381 ret = mmc_resume_host(host->mmc);
2382 if (ret == 0)
2383 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002384
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002385 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002386 }
2387
2388 return ret;
2389
2390clk_en_err:
2391 dev_dbg(mmc_dev(host->mmc),
2392 "Failed to enable MMC clocks during resume\n");
2393 return ret;
2394}
2395
2396#else
Denis Karpov70a33412009-09-22 16:44:59 -07002397#define omap_hsmmc_suspend NULL
2398#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002399#endif
2400
Kevin Hilmana791daa2010-05-26 14:42:07 -07002401static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002402 .suspend = omap_hsmmc_suspend,
2403 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002404};
2405
2406static struct platform_driver omap_hsmmc_driver = {
2407 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002408 .driver = {
2409 .name = DRIVER_NAME,
2410 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002411 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002412 },
2413};
2414
Denis Karpov70a33412009-09-22 16:44:59 -07002415static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002416{
2417 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002418 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002419}
2420
Denis Karpov70a33412009-09-22 16:44:59 -07002421static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002422{
2423 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002424 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002425}
2426
Denis Karpov70a33412009-09-22 16:44:59 -07002427module_init(omap_hsmmc_init);
2428module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002429
2430MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2431MODULE_LICENSE("GPL");
2432MODULE_ALIAS("platform:" DRIVER_NAME);
2433MODULE_AUTHOR("Texas Instruments Inc");