blob: d2fad587f3710e2d2e083386e7fe9d534169a550 [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 semaphore sem;
161 struct work_struct mmc_carddetect_work;
162 void __iomem *base;
163 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700164 spinlock_t irq_lock; /* Prevent races with irq handler */
165 unsigned long flags;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 unsigned int id;
167 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200168 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700170 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 u32 *buffer;
172 u32 bytesleft;
173 int suspended;
174 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000176 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700178 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200179 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700181 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700182 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700183 int protect_card;
184 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800185 int use_reg;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700186
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100187 struct omap_mmc_platform_data *pdata;
188};
189
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800190static int omap_hsmmc_card_detect(struct device *dev, int slot)
191{
192 struct omap_mmc_platform_data *mmc = dev->platform_data;
193
194 /* NOTE: assumes card detect signal is active-low */
195 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
196}
197
198static int omap_hsmmc_get_wp(struct device *dev, int slot)
199{
200 struct omap_mmc_platform_data *mmc = dev->platform_data;
201
202 /* NOTE: assumes write protect signal is active-high */
203 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
204}
205
206static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
207{
208 struct omap_mmc_platform_data *mmc = dev->platform_data;
209
210 /* NOTE: assumes card detect signal is active-low */
211 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
212}
213
214#ifdef CONFIG_PM
215
216static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
217{
218 struct omap_mmc_platform_data *mmc = dev->platform_data;
219
220 disable_irq(mmc->slots[0].card_detect_irq);
221 return 0;
222}
223
224static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
225{
226 struct omap_mmc_platform_data *mmc = dev->platform_data;
227
228 enable_irq(mmc->slots[0].card_detect_irq);
229 return 0;
230}
231
232#else
233
234#define omap_hsmmc_suspend_cdirq NULL
235#define omap_hsmmc_resume_cdirq NULL
236
237#endif
238
239static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
240 int vdd)
241{
242 struct omap_hsmmc_host *host =
243 platform_get_drvdata(to_platform_device(dev));
244 int ret;
245
246 if (mmc_slot(host).before_set_reg)
247 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
248
249 if (power_on)
250 ret = mmc_regulator_set_ocr(host->vcc, vdd);
251 else
252 ret = mmc_regulator_set_ocr(host->vcc, 0);
253
254 if (mmc_slot(host).after_set_reg)
255 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
256
257 return ret;
258}
259
260static int omap_hsmmc_23_set_power(struct device *dev, int slot, int power_on,
261 int vdd)
262{
263 struct omap_hsmmc_host *host =
264 platform_get_drvdata(to_platform_device(dev));
265 int ret = 0;
266
267 /*
268 * If we don't see a Vcc regulator, assume it's a fixed
269 * voltage always-on regulator.
270 */
271 if (!host->vcc)
272 return 0;
273
274 if (mmc_slot(host).before_set_reg)
275 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
276
277 /*
278 * Assume Vcc regulator is used only to power the card ... OMAP
279 * VDDS is used to power the pins, optionally with a transceiver to
280 * support cards using voltages other than VDDS (1.8V nominal). When a
281 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
282 *
283 * In some cases this regulator won't support enable/disable;
284 * e.g. it's a fixed rail for a WLAN chip.
285 *
286 * In other cases vcc_aux switches interface power. Example, for
287 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
288 * chips/cards need an interface voltage rail too.
289 */
290 if (power_on) {
291 ret = mmc_regulator_set_ocr(host->vcc, vdd);
292 /* Enable interface voltage rail, if needed */
293 if (ret == 0 && host->vcc_aux) {
294 ret = regulator_enable(host->vcc_aux);
295 if (ret < 0)
296 ret = mmc_regulator_set_ocr(host->vcc, 0);
297 }
298 } else {
Adrian Hunter6da20c82010-02-15 10:03:34 -0800299 if (host->vcc_aux)
300 ret = regulator_disable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 if (ret == 0)
302 ret = mmc_regulator_set_ocr(host->vcc, 0);
303 }
304
305 if (mmc_slot(host).after_set_reg)
306 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
307
308 return ret;
309}
310
311static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
312 int vdd, int cardsleep)
313{
314 struct omap_hsmmc_host *host =
315 platform_get_drvdata(to_platform_device(dev));
316 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
317
318 return regulator_set_mode(host->vcc, mode);
319}
320
321static int omap_hsmmc_23_set_sleep(struct device *dev, int slot, int sleep,
322 int vdd, int cardsleep)
323{
324 struct omap_hsmmc_host *host =
325 platform_get_drvdata(to_platform_device(dev));
326 int err, mode;
327
328 /*
329 * If we don't see a Vcc regulator, assume it's a fixed
330 * voltage always-on regulator.
331 */
332 if (!host->vcc)
333 return 0;
334
335 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
336
337 if (!host->vcc_aux)
338 return regulator_set_mode(host->vcc, mode);
339
340 if (cardsleep) {
341 /* VCC can be turned off if card is asleep */
342 if (sleep)
343 err = mmc_regulator_set_ocr(host->vcc, 0);
344 else
345 err = mmc_regulator_set_ocr(host->vcc, vdd);
346 } else
347 err = regulator_set_mode(host->vcc, mode);
348 if (err)
349 return err;
350 return regulator_set_mode(host->vcc_aux, mode);
351}
352
353static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
354{
355 int ret;
356
357 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
358 pdata->suspend = omap_hsmmc_suspend_cdirq;
359 pdata->resume = omap_hsmmc_resume_cdirq;
360 if (pdata->slots[0].cover)
361 pdata->slots[0].get_cover_state =
362 omap_hsmmc_get_cover_state;
363 else
364 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
365 pdata->slots[0].card_detect_irq =
366 gpio_to_irq(pdata->slots[0].switch_pin);
367 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
368 if (ret)
369 return ret;
370 ret = gpio_direction_input(pdata->slots[0].switch_pin);
371 if (ret)
372 goto err_free_sp;
373 } else
374 pdata->slots[0].switch_pin = -EINVAL;
375
376 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
377 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
378 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
379 if (ret)
380 goto err_free_cd;
381 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
382 if (ret)
383 goto err_free_wp;
384 } else
385 pdata->slots[0].gpio_wp = -EINVAL;
386
387 return 0;
388
389err_free_wp:
390 gpio_free(pdata->slots[0].gpio_wp);
391err_free_cd:
392 if (gpio_is_valid(pdata->slots[0].switch_pin))
393err_free_sp:
394 gpio_free(pdata->slots[0].switch_pin);
395 return ret;
396}
397
398static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
399{
400 if (gpio_is_valid(pdata->slots[0].gpio_wp))
401 gpio_free(pdata->slots[0].gpio_wp);
402 if (gpio_is_valid(pdata->slots[0].switch_pin))
403 gpio_free(pdata->slots[0].switch_pin);
404}
405
406static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
407{
408 struct regulator *reg;
409 int ret = 0;
410
411 switch (host->id) {
412 case OMAP_MMC1_DEVID:
413 /* On-chip level shifting via PBIAS0/PBIAS1 */
414 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
415 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
416 break;
417 case OMAP_MMC2_DEVID:
418 case OMAP_MMC3_DEVID:
419 /* Off-chip level shifting, or none */
420 mmc_slot(host).set_power = omap_hsmmc_23_set_power;
421 mmc_slot(host).set_sleep = omap_hsmmc_23_set_sleep;
422 break;
423 default:
424 pr_err("MMC%d configuration not supported!\n", host->id);
425 return -EINVAL;
426 }
427
428 reg = regulator_get(host->dev, "vmmc");
429 if (IS_ERR(reg)) {
430 dev_dbg(host->dev, "vmmc regulator missing\n");
431 /*
432 * HACK: until fixed.c regulator is usable,
433 * we don't require a main regulator
434 * for MMC2 or MMC3
435 */
436 if (host->id == OMAP_MMC1_DEVID) {
437 ret = PTR_ERR(reg);
438 goto err;
439 }
440 } else {
441 host->vcc = reg;
442 mmc_slot(host).ocr_mask = mmc_regulator_get_ocrmask(reg);
443
444 /* Allow an aux regulator */
445 reg = regulator_get(host->dev, "vmmc_aux");
446 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
447
448 /*
449 * UGLY HACK: workaround regulator framework bugs.
450 * When the bootloader leaves a supply active, it's
451 * initialized with zero usecount ... and we can't
452 * disable it without first enabling it. Until the
453 * framework is fixed, we need a workaround like this
454 * (which is safe for MMC, but not in general).
455 */
456 if (regulator_is_enabled(host->vcc) > 0) {
457 regulator_enable(host->vcc);
458 regulator_disable(host->vcc);
459 }
460 if (host->vcc_aux) {
461 if (regulator_is_enabled(reg) > 0) {
462 regulator_enable(reg);
463 regulator_disable(reg);
464 }
465 }
466 }
467
468 return 0;
469
470err:
471 mmc_slot(host).set_power = NULL;
472 mmc_slot(host).set_sleep = NULL;
473 return ret;
474}
475
476static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
477{
478 regulator_put(host->vcc);
479 regulator_put(host->vcc_aux);
480 mmc_slot(host).set_power = NULL;
481 mmc_slot(host).set_sleep = NULL;
482}
483
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100484/*
485 * Stop clock to the card
486 */
Denis Karpov70a33412009-09-22 16:44:59 -0700487static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100488{
489 OMAP_HSMMC_WRITE(host->base, SYSCTL,
490 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
491 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
492 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
493}
494
Denis Karpov11dd62a2009-09-22 16:44:43 -0700495#ifdef CONFIG_PM
496
497/*
498 * Restore the MMC host context, if it was lost as result of a
499 * power state change.
500 */
Denis Karpov70a33412009-09-22 16:44:59 -0700501static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700502{
503 struct mmc_ios *ios = &host->mmc->ios;
504 struct omap_mmc_platform_data *pdata = host->pdata;
505 int context_loss = 0;
506 u32 hctl, capa, con;
507 u16 dsor = 0;
508 unsigned long timeout;
509
510 if (pdata->get_context_loss_count) {
511 context_loss = pdata->get_context_loss_count(host->dev);
512 if (context_loss < 0)
513 return 1;
514 }
515
516 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
517 context_loss == host->context_loss ? "not " : "");
518 if (host->context_loss == context_loss)
519 return 1;
520
521 /* Wait for hardware reset */
522 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
523 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
524 && time_before(jiffies, timeout))
525 ;
526
527 /* Do software reset */
528 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
529 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
530 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
531 && time_before(jiffies, timeout))
532 ;
533
534 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
535 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
536
537 if (host->id == OMAP_MMC1_DEVID) {
538 if (host->power_mode != MMC_POWER_OFF &&
539 (1 << ios->vdd) <= MMC_VDD_23_24)
540 hctl = SDVS18;
541 else
542 hctl = SDVS30;
543 capa = VS30 | VS18;
544 } else {
545 hctl = SDVS18;
546 capa = VS18;
547 }
548
549 OMAP_HSMMC_WRITE(host->base, HCTL,
550 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
551
552 OMAP_HSMMC_WRITE(host->base, CAPA,
553 OMAP_HSMMC_READ(host->base, CAPA) | capa);
554
555 OMAP_HSMMC_WRITE(host->base, HCTL,
556 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
557
558 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
559 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
560 && time_before(jiffies, timeout))
561 ;
562
563 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
564 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
565 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
566
567 /* Do not initialize card-specific things if the power is off */
568 if (host->power_mode == MMC_POWER_OFF)
569 goto out;
570
571 con = OMAP_HSMMC_READ(host->base, CON);
572 switch (ios->bus_width) {
573 case MMC_BUS_WIDTH_8:
574 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
575 break;
576 case MMC_BUS_WIDTH_4:
577 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
578 OMAP_HSMMC_WRITE(host->base, HCTL,
579 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
580 break;
581 case MMC_BUS_WIDTH_1:
582 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
583 OMAP_HSMMC_WRITE(host->base, HCTL,
584 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
585 break;
586 }
587
588 if (ios->clock) {
589 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
590 if (dsor < 1)
591 dsor = 1;
592
593 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
594 dsor++;
595
596 if (dsor > 250)
597 dsor = 250;
598 }
599
600 OMAP_HSMMC_WRITE(host->base, SYSCTL,
601 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
602 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
603 OMAP_HSMMC_WRITE(host->base, SYSCTL,
604 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
605
606 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
607 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
608 && time_before(jiffies, timeout))
609 ;
610
611 OMAP_HSMMC_WRITE(host->base, SYSCTL,
612 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
613
614 con = OMAP_HSMMC_READ(host->base, CON);
615 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
616 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
617 else
618 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
619out:
620 host->context_loss = context_loss;
621
622 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
623 return 0;
624}
625
626/*
627 * Save the MMC host context (store the number of power state changes so far).
628 */
Denis Karpov70a33412009-09-22 16:44:59 -0700629static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700630{
631 struct omap_mmc_platform_data *pdata = host->pdata;
632 int context_loss;
633
634 if (pdata->get_context_loss_count) {
635 context_loss = pdata->get_context_loss_count(host->dev);
636 if (context_loss < 0)
637 return;
638 host->context_loss = context_loss;
639 }
640}
641
642#else
643
Denis Karpov70a33412009-09-22 16:44:59 -0700644static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700645{
646 return 0;
647}
648
Denis Karpov70a33412009-09-22 16:44:59 -0700649static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650{
651}
652
653#endif
654
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100655/*
656 * Send init stream sequence to card
657 * before sending IDLE command
658 */
Denis Karpov70a33412009-09-22 16:44:59 -0700659static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100660{
661 int reg = 0;
662 unsigned long timeout;
663
Adrian Hunterb62f6222009-09-22 16:45:01 -0700664 if (host->protect_card)
665 return;
666
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100667 disable_irq(host->irq);
668 OMAP_HSMMC_WRITE(host->base, CON,
669 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
670 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
671
672 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
673 while ((reg != CC) && time_before(jiffies, timeout))
674 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
675
676 OMAP_HSMMC_WRITE(host->base, CON,
677 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700678
679 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
680 OMAP_HSMMC_READ(host->base, STAT);
681
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100682 enable_irq(host->irq);
683}
684
685static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700686int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100687{
688 int r = 1;
689
Denis Karpov191d1f12009-09-22 16:44:55 -0700690 if (mmc_slot(host).get_cover_state)
691 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100692 return r;
693}
694
695static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700696omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100697 char *buf)
698{
699 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700700 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100701
Denis Karpov70a33412009-09-22 16:44:59 -0700702 return sprintf(buf, "%s\n",
703 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704}
705
Denis Karpov70a33412009-09-22 16:44:59 -0700706static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100707
708static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700709omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100710 char *buf)
711{
712 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700713 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714
Denis Karpov191d1f12009-09-22 16:44:55 -0700715 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100716}
717
Denis Karpov70a33412009-09-22 16:44:59 -0700718static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100719
720/*
721 * Configure the response type and send the cmd.
722 */
723static void
Denis Karpov70a33412009-09-22 16:44:59 -0700724omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725 struct mmc_data *data)
726{
727 int cmdreg = 0, resptype = 0, cmdtype = 0;
728
729 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
730 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
731 host->cmd = cmd;
732
733 /*
734 * Clear status bits and enable interrupts
735 */
736 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
737 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -0700738
739 if (host->use_dma)
740 OMAP_HSMMC_WRITE(host->base, IE,
741 INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
742 else
743 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100744
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200745 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746 if (cmd->flags & MMC_RSP_PRESENT) {
747 if (cmd->flags & MMC_RSP_136)
748 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200749 else if (cmd->flags & MMC_RSP_BUSY) {
750 resptype = 3;
751 host->response_busy = 1;
752 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753 resptype = 2;
754 }
755
756 /*
757 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
758 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
759 * a val of 0x3, rest 0x0.
760 */
761 if (cmd == host->mrq->stop)
762 cmdtype = 0x3;
763
764 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
765
766 if (data) {
767 cmdreg |= DP_SELECT | MSBS | BCE;
768 if (data->flags & MMC_DATA_READ)
769 cmdreg |= DDIR;
770 else
771 cmdreg &= ~(DDIR);
772 }
773
774 if (host->use_dma)
775 cmdreg |= DMA_EN;
776
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700777 /*
778 * In an interrupt context (i.e. STOP command), the spinlock is unlocked
779 * by the interrupt handler, otherwise (i.e. for a new request) it is
780 * unlocked here.
781 */
782 if (!in_interrupt())
783 spin_unlock_irqrestore(&host->irq_lock, host->flags);
784
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100785 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
786 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
787}
788
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200789static int
Denis Karpov70a33412009-09-22 16:44:59 -0700790omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200791{
792 if (data->flags & MMC_DATA_WRITE)
793 return DMA_TO_DEVICE;
794 else
795 return DMA_FROM_DEVICE;
796}
797
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798/*
799 * Notify the transfer complete to MMC core
800 */
801static void
Denis Karpov70a33412009-09-22 16:44:59 -0700802omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200804 if (!data) {
805 struct mmc_request *mrq = host->mrq;
806
Adrian Hunter23050102009-09-22 16:44:57 -0700807 /* TC before CC from CMD6 - don't know why, but it happens */
808 if (host->cmd && host->cmd->opcode == 6 &&
809 host->response_busy) {
810 host->response_busy = 0;
811 return;
812 }
813
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200814 host->mrq = NULL;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200815 mmc_request_done(host->mmc, mrq);
816 return;
817 }
818
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100819 host->data = NULL;
820
821 if (host->use_dma && host->dma_ch != -1)
822 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700823 omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824
825 if (!data->error)
826 data->bytes_xfered += data->blocks * (data->blksz);
827 else
828 data->bytes_xfered = 0;
829
830 if (!data->stop) {
831 host->mrq = NULL;
832 mmc_request_done(host->mmc, data->mrq);
833 return;
834 }
Denis Karpov70a33412009-09-22 16:44:59 -0700835 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100836}
837
838/*
839 * Notify the core about command completion
840 */
841static void
Denis Karpov70a33412009-09-22 16:44:59 -0700842omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843{
844 host->cmd = NULL;
845
846 if (cmd->flags & MMC_RSP_PRESENT) {
847 if (cmd->flags & MMC_RSP_136) {
848 /* response type 2 */
849 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
850 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
851 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
852 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
853 } else {
854 /* response types 1, 1b, 3, 4, 5, 6 */
855 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
856 }
857 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200858 if ((host->data == NULL && !host->response_busy) || cmd->error) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859 host->mrq = NULL;
860 mmc_request_done(host->mmc, cmd->mrq);
861 }
862}
863
864/*
865 * DMA clean up for command errors
866 */
Denis Karpov70a33412009-09-22 16:44:59 -0700867static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868{
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200869 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100870
871 if (host->use_dma && host->dma_ch != -1) {
872 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700873 omap_hsmmc_get_dma_dir(host, host->data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874 omap_free_dma(host->dma_ch);
875 host->dma_ch = -1;
876 up(&host->sem);
877 }
878 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100879}
880
881/*
882 * Readable error output
883 */
884#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700885static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100886{
887 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700888 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
890 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
891 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
892 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
893 };
894 char res[256];
895 char *buf = res;
896 int len, i;
897
898 len = sprintf(buf, "MMC IRQ 0x%x :", status);
899 buf += len;
900
Denis Karpov70a33412009-09-22 16:44:59 -0700901 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700903 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100904 buf += len;
905 }
906
907 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
908}
909#endif /* CONFIG_MMC_DEBUG */
910
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100911/*
912 * MMC controller internal state machines reset
913 *
914 * Used to reset command or data internal state machines, using respectively
915 * SRC or SRD bit of SYSCTL register
916 * Can be called from interrupt context
917 */
Denis Karpov70a33412009-09-22 16:44:59 -0700918static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
919 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100920{
921 unsigned long i = 0;
922 unsigned long limit = (loops_per_jiffy *
923 msecs_to_jiffies(MMC_TIMEOUT_MS));
924
925 OMAP_HSMMC_WRITE(host->base, SYSCTL,
926 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
927
928 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
929 (i++ < limit))
930 cpu_relax();
931
932 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
933 dev_err(mmc_dev(host->mmc),
934 "Timeout waiting on controller reset in %s\n",
935 __func__);
936}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100937
938/*
939 * MMC controller IRQ handler
940 */
Denis Karpov70a33412009-09-22 16:44:59 -0700941static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100942{
Denis Karpov70a33412009-09-22 16:44:59 -0700943 struct omap_hsmmc_host *host = dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944 struct mmc_data *data;
945 int end_cmd = 0, end_trans = 0, status;
946
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700947 spin_lock(&host->irq_lock);
948
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200949 if (host->mrq == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100950 OMAP_HSMMC_WRITE(host->base, STAT,
951 OMAP_HSMMC_READ(host->base, STAT));
Kevin Hilman00adadc2009-04-06 15:01:19 +0300952 /* Flush posted write */
953 OMAP_HSMMC_READ(host->base, STAT);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700954 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955 return IRQ_HANDLED;
956 }
957
958 data = host->data;
959 status = OMAP_HSMMC_READ(host->base, STAT);
960 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
961
962 if (status & ERR) {
963#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700964 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100965#endif
966 if ((status & CMD_TIMEOUT) ||
967 (status & CMD_CRC)) {
968 if (host->cmd) {
969 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -0700970 omap_hsmmc_reset_controller_fsm(host,
971 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100972 host->cmd->error = -ETIMEDOUT;
973 } else {
974 host->cmd->error = -EILSEQ;
975 }
976 end_cmd = 1;
977 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200978 if (host->data || host->response_busy) {
979 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700980 omap_hsmmc_dma_cleanup(host,
981 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200982 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700983 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800984 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985 }
986 if ((status & DATA_TIMEOUT) ||
987 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200988 if (host->data || host->response_busy) {
989 int err = (status & DATA_TIMEOUT) ?
990 -ETIMEDOUT : -EILSEQ;
991
992 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700993 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100994 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200995 host->mrq->cmd->error = err;
996 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700997 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998 end_trans = 1;
999 }
1000 }
1001 if (status & CARD_ERR) {
1002 dev_dbg(mmc_dev(host->mmc),
1003 "Ignoring card err CMD%d\n", host->cmd->opcode);
1004 if (host->cmd)
1005 end_cmd = 1;
1006 if (host->data)
1007 end_trans = 1;
1008 }
1009 }
1010
1011 OMAP_HSMMC_WRITE(host->base, STAT, status);
Kevin Hilman00adadc2009-04-06 15:01:19 +03001012 /* Flush posted write */
1013 OMAP_HSMMC_READ(host->base, STAT);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001014
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001015 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001016 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001017 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001018 omap_hsmmc_xfer_done(host, data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001019
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001020 spin_unlock(&host->irq_lock);
1021
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 return IRQ_HANDLED;
1023}
1024
Denis Karpov70a33412009-09-22 16:44:59 -07001025static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001026{
1027 unsigned long i;
1028
1029 OMAP_HSMMC_WRITE(host->base, HCTL,
1030 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1031 for (i = 0; i < loops_per_jiffy; i++) {
1032 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1033 break;
1034 cpu_relax();
1035 }
1036}
1037
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001038/*
David Brownelleb250822009-02-17 14:49:01 -08001039 * Switch MMC interface voltage ... only relevant for MMC1.
1040 *
1041 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1042 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1043 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001044 */
Denis Karpov70a33412009-09-22 16:44:59 -07001045static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046{
1047 u32 reg_val = 0;
1048 int ret;
1049
1050 /* Disable the clocks */
1051 clk_disable(host->fclk);
1052 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001053 if (host->got_dbclk)
1054 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055
1056 /* Turn the power off */
1057 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001058
1059 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001060 if (!ret)
1061 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1062 vdd);
1063 clk_enable(host->iclk);
1064 clk_enable(host->fclk);
1065 if (host->got_dbclk)
1066 clk_enable(host->dbclk);
1067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 if (ret != 0)
1069 goto err;
1070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071 OMAP_HSMMC_WRITE(host->base, HCTL,
1072 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1073 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001074
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001075 /*
1076 * If a MMC dual voltage card is detected, the set_ios fn calls
1077 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001078 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001079 *
David Brownelleb250822009-02-17 14:49:01 -08001080 * Cope with a bit of slop in the range ... per data sheets:
1081 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1082 * but recommended values are 1.71V to 1.89V
1083 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1084 * but recommended values are 2.7V to 3.3V
1085 *
1086 * Board setup code shouldn't permit anything very out-of-range.
1087 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1088 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 */
David Brownelleb250822009-02-17 14:49:01 -08001090 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001092 else
1093 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094
1095 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001096 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001097
1098 return 0;
1099err:
1100 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1101 return ret;
1102}
1103
Adrian Hunterb62f6222009-09-22 16:45:01 -07001104/* Protect the card while the cover is open */
1105static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1106{
1107 if (!mmc_slot(host).get_cover_state)
1108 return;
1109
1110 host->reqs_blocked = 0;
1111 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1112 if (host->protect_card) {
1113 printk(KERN_INFO "%s: cover is closed, "
1114 "card is now accessible\n",
1115 mmc_hostname(host->mmc));
1116 host->protect_card = 0;
1117 }
1118 } else {
1119 if (!host->protect_card) {
1120 printk(KERN_INFO "%s: cover is open, "
1121 "card is now inaccessible\n",
1122 mmc_hostname(host->mmc));
1123 host->protect_card = 1;
1124 }
1125 }
1126}
1127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128/*
1129 * Work Item to notify the core about card insertion/removal
1130 */
Denis Karpov70a33412009-09-22 16:44:59 -07001131static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001132{
Denis Karpov70a33412009-09-22 16:44:59 -07001133 struct omap_hsmmc_host *host =
1134 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001135 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001136 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001137
Adrian Huntera6b22402009-09-22 16:44:45 -07001138 if (host->suspended)
1139 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001140
1141 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001142
Denis Karpov191d1f12009-09-22 16:44:55 -07001143 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001144 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001145 else {
1146 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001147 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001148 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001149
1150 if (carddetect) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1152 } else {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001153 mmc_host_enable(host->mmc);
Denis Karpov70a33412009-09-22 16:44:59 -07001154 omap_hsmmc_reset_controller_fsm(host, SRD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001155 mmc_host_lazy_disable(host->mmc);
Denis Karpov70a33412009-09-22 16:44:59 -07001156
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1158 }
1159}
1160
1161/*
1162 * ISR for handling card insertion and removal
1163 */
Denis Karpov70a33412009-09-22 16:44:59 -07001164static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165{
Denis Karpov70a33412009-09-22 16:44:59 -07001166 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
Adrian Huntera6b22402009-09-22 16:44:45 -07001168 if (host->suspended)
1169 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170 schedule_work(&host->mmc_carddetect_work);
1171
1172 return IRQ_HANDLED;
1173}
1174
Denis Karpov70a33412009-09-22 16:44:59 -07001175static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001176 struct mmc_data *data)
1177{
1178 int sync_dev;
1179
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001180 if (data->flags & MMC_DATA_WRITE)
1181 sync_dev = host->dma_line_tx;
1182 else
1183 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001184 return sync_dev;
1185}
1186
Denis Karpov70a33412009-09-22 16:44:59 -07001187static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001188 struct mmc_data *data,
1189 struct scatterlist *sgl)
1190{
1191 int blksz, nblk, dma_ch;
1192
1193 dma_ch = host->dma_ch;
1194 if (data->flags & MMC_DATA_WRITE) {
1195 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1196 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1197 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1198 sg_dma_address(sgl), 0, 0);
1199 } else {
1200 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001201 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001202 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1203 sg_dma_address(sgl), 0, 0);
1204 }
1205
1206 blksz = host->data->blksz;
1207 nblk = sg_dma_len(sgl) / blksz;
1208
1209 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1210 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001211 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001212 !(data->flags & MMC_DATA_WRITE));
1213
1214 omap_start_dma(dma_ch);
1215}
1216
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001217/*
1218 * DMA call back function
1219 */
Denis Karpov70a33412009-09-22 16:44:59 -07001220static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221{
Denis Karpov70a33412009-09-22 16:44:59 -07001222 struct omap_hsmmc_host *host = data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001223
1224 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
1225 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
1226
1227 if (host->dma_ch < 0)
1228 return;
1229
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001230 host->dma_sg_idx++;
1231 if (host->dma_sg_idx < host->dma_len) {
1232 /* Fire up the next transfer. */
Denis Karpov70a33412009-09-22 16:44:59 -07001233 omap_hsmmc_config_dma_params(host, host->data,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001234 host->data->sg + host->dma_sg_idx);
1235 return;
1236 }
1237
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238 omap_free_dma(host->dma_ch);
1239 host->dma_ch = -1;
1240 /*
1241 * DMA Callback: run in interrupt context.
Anand Gadiyar85b84322009-04-15 17:44:58 +05301242 * mutex_unlock will throw a kernel warning if used.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243 */
1244 up(&host->sem);
1245}
1246
1247/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248 * Routine to configure and start DMA for the MMC card
1249 */
Denis Karpov70a33412009-09-22 16:44:59 -07001250static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1251 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252{
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001253 int dma_ch = 0, ret = 0, err = 1, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254 struct mmc_data *data = req->data;
1255
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001256 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001257 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001258 struct scatterlist *sgl;
1259
1260 sgl = data->sg + i;
1261 if (sgl->length % data->blksz)
1262 return -EINVAL;
1263 }
1264 if ((data->blksz % 4) != 0)
1265 /* REVISIT: The MMC buffer increments only when MSB is written.
1266 * Return error for blksz which is non multiple of four.
1267 */
1268 return -EINVAL;
1269
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270 /*
1271 * If for some reason the DMA transfer is still active,
1272 * we wait for timeout period and free the dma
1273 */
1274 if (host->dma_ch != -1) {
1275 set_current_state(TASK_UNINTERRUPTIBLE);
1276 schedule_timeout(100);
1277 if (down_trylock(&host->sem)) {
1278 omap_free_dma(host->dma_ch);
1279 host->dma_ch = -1;
1280 up(&host->sem);
1281 return err;
1282 }
1283 } else {
1284 if (down_trylock(&host->sem))
1285 return err;
1286 }
1287
Denis Karpov70a33412009-09-22 16:44:59 -07001288 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1289 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001290 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001291 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 "%s: omap_request_dma() failed with %d\n",
1293 mmc_hostname(host->mmc), ret);
1294 return ret;
1295 }
1296
1297 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001298 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001300 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001301
Denis Karpov70a33412009-09-22 16:44:59 -07001302 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001304 return 0;
1305}
1306
Denis Karpov70a33412009-09-22 16:44:59 -07001307static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001308 unsigned int timeout_ns,
1309 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310{
1311 unsigned int timeout, cycle_ns;
1312 uint32_t reg, clkd, dto = 0;
1313
1314 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1315 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1316 if (clkd == 0)
1317 clkd = 1;
1318
1319 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001320 timeout = timeout_ns / cycle_ns;
1321 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322 if (timeout) {
1323 while ((timeout & 0x80000000) == 0) {
1324 dto += 1;
1325 timeout <<= 1;
1326 }
1327 dto = 31 - dto;
1328 timeout <<= 1;
1329 if (timeout && dto)
1330 dto += 1;
1331 if (dto >= 13)
1332 dto -= 13;
1333 else
1334 dto = 0;
1335 if (dto > 14)
1336 dto = 14;
1337 }
1338
1339 reg &= ~DTO_MASK;
1340 reg |= dto << DTO_SHIFT;
1341 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1342}
1343
1344/*
1345 * Configure block length for MMC/SD cards and initiate the transfer.
1346 */
1347static int
Denis Karpov70a33412009-09-22 16:44:59 -07001348omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001349{
1350 int ret;
1351 host->data = req->data;
1352
1353 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001355 /*
1356 * Set an arbitrary 100ms data timeout for commands with
1357 * busy signal.
1358 */
1359 if (req->cmd->flags & MMC_RSP_BUSY)
1360 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361 return 0;
1362 }
1363
1364 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1365 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001366 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001367
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001368 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001369 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370 if (ret != 0) {
1371 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1372 return ret;
1373 }
1374 }
1375 return 0;
1376}
1377
1378/*
1379 * Request function. for read/write operation
1380 */
Denis Karpov70a33412009-09-22 16:44:59 -07001381static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001382{
Denis Karpov70a33412009-09-22 16:44:59 -07001383 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001384 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001385
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001386 /*
1387 * Prevent races with the interrupt handler because of unexpected
1388 * interrupts, but not if we are already in interrupt context i.e.
1389 * retries.
1390 */
Adrian Hunterb62f6222009-09-22 16:45:01 -07001391 if (!in_interrupt()) {
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001392 spin_lock_irqsave(&host->irq_lock, host->flags);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001393 /*
1394 * Protect the card from I/O if there is a possibility
1395 * it can be removed.
1396 */
1397 if (host->protect_card) {
1398 if (host->reqs_blocked < 3) {
1399 /*
1400 * Ensure the controller is left in a consistent
1401 * state by resetting the command and data state
1402 * machines.
1403 */
1404 omap_hsmmc_reset_controller_fsm(host, SRD);
1405 omap_hsmmc_reset_controller_fsm(host, SRC);
1406 host->reqs_blocked += 1;
1407 }
1408 req->cmd->error = -EBADF;
1409 if (req->data)
1410 req->data->error = -EBADF;
1411 spin_unlock_irqrestore(&host->irq_lock, host->flags);
1412 mmc_request_done(mmc, req);
1413 return;
1414 } else if (host->reqs_blocked)
1415 host->reqs_blocked = 0;
1416 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001417 WARN_ON(host->mrq != NULL);
1418 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001419 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001420 if (err) {
1421 req->cmd->error = err;
1422 if (req->data)
1423 req->data->error = err;
1424 host->mrq = NULL;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001425 if (!in_interrupt())
1426 spin_unlock_irqrestore(&host->irq_lock, host->flags);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001427 mmc_request_done(mmc, req);
1428 return;
1429 }
1430
Denis Karpov70a33412009-09-22 16:44:59 -07001431 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001432}
1433
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001435static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001436{
Denis Karpov70a33412009-09-22 16:44:59 -07001437 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438 u16 dsor = 0;
1439 unsigned long regval;
1440 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001441 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001442 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001444 mmc_host_enable(host->mmc);
1445
Adrian Huntera3621462009-09-22 16:44:42 -07001446 if (ios->power_mode != host->power_mode) {
1447 switch (ios->power_mode) {
1448 case MMC_POWER_OFF:
1449 mmc_slot(host).set_power(host->dev, host->slot_id,
1450 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001451 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001452 break;
1453 case MMC_POWER_UP:
1454 mmc_slot(host).set_power(host->dev, host->slot_id,
1455 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001456 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001457 break;
1458 case MMC_POWER_ON:
1459 do_send_init_stream = 1;
1460 break;
1461 }
1462 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463 }
1464
Denis Karpovdd498ef2009-09-22 16:44:49 -07001465 /* FIXME: set registers based only on changes to ios */
1466
Jarkko Lavinen73153012008-11-21 16:49:54 +02001467 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001468 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001469 case MMC_BUS_WIDTH_8:
1470 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1471 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001473 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474 OMAP_HSMMC_WRITE(host->base, HCTL,
1475 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1476 break;
1477 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001478 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001479 OMAP_HSMMC_WRITE(host->base, HCTL,
1480 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1481 break;
1482 }
1483
1484 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001485 /* Only MMC1 can interface at 3V without some flavor
1486 * of external transceiver; but they all handle 1.8V.
1487 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1489 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1490 /*
1491 * The mmc_select_voltage fn of the core does
1492 * not seem to set the power_mode to
1493 * MMC_POWER_UP upon recalculating the voltage.
1494 * vdd 1.8v.
1495 */
Denis Karpov70a33412009-09-22 16:44:59 -07001496 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1497 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498 "Switch operation failed\n");
1499 }
1500 }
1501
1502 if (ios->clock) {
1503 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1504 if (dsor < 1)
1505 dsor = 1;
1506
1507 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1508 dsor++;
1509
1510 if (dsor > 250)
1511 dsor = 250;
1512 }
Denis Karpov70a33412009-09-22 16:44:59 -07001513 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1515 regval = regval & ~(CLKD_MASK);
1516 regval = regval | (dsor << 6) | (DTO << 16);
1517 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1518 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1519 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1520
1521 /* Wait till the ICS bit is set */
1522 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001523 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001524 && time_before(jiffies, timeout))
1525 msleep(1);
1526
1527 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1528 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1529
Adrian Huntera3621462009-09-22 16:44:42 -07001530 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531 send_init_stream(host);
1532
Denis Karpovabb28e72009-09-22 16:44:44 -07001533 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001534 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001535 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1536 else
1537 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001538
Denis Karpovdd498ef2009-09-22 16:44:49 -07001539 if (host->power_mode == MMC_POWER_OFF)
1540 mmc_host_disable(host->mmc);
1541 else
1542 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001543}
1544
1545static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1546{
Denis Karpov70a33412009-09-22 16:44:59 -07001547 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548
Denis Karpov191d1f12009-09-22 16:44:55 -07001549 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001550 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001551 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001552}
1553
1554static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1555{
Denis Karpov70a33412009-09-22 16:44:59 -07001556 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557
Denis Karpov191d1f12009-09-22 16:44:55 -07001558 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001559 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001560 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561}
1562
Denis Karpov70a33412009-09-22 16:44:59 -07001563static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001564{
1565 u32 hctl, capa, value;
1566
1567 /* Only MMC1 supports 3.0V */
1568 if (host->id == OMAP_MMC1_DEVID) {
1569 hctl = SDVS30;
1570 capa = VS30 | VS18;
1571 } else {
1572 hctl = SDVS18;
1573 capa = VS18;
1574 }
1575
1576 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1577 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1578
1579 value = OMAP_HSMMC_READ(host->base, CAPA);
1580 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1581
1582 /* Set the controller to AUTO IDLE mode */
1583 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1584 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1585
1586 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001587 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001588}
1589
Denis Karpovdd498ef2009-09-22 16:44:49 -07001590/*
1591 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001592 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1593 * ^___________| | |
1594 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001595 *
1596 * ENABLED: mmc host is fully functional
1597 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001598 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1599 * REGSLEEP: fclk is off, voltage regulator is asleep
1600 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001601 *
1602 * Transition handlers return the timeout for the next state transition
1603 * or negative error.
1604 */
1605
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001606enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001607
1608/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001609static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610{
Denis Karpov70a33412009-09-22 16:44:59 -07001611 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001612 clk_disable(host->fclk);
1613 host->dpm_state = DISABLED;
1614
1615 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1616
1617 if (host->power_mode == MMC_POWER_OFF)
1618 return 0;
1619
Adrian Hunter4380eea2010-02-15 10:03:34 -08001620 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001621}
1622
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001623/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001624static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001626 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001627
1628 if (!mmc_try_claim_host(host->mmc))
1629 return 0;
1630
1631 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001632 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001633 if (mmc_card_can_sleep(host->mmc)) {
1634 err = mmc_card_sleep(host->mmc);
1635 if (err < 0) {
1636 clk_disable(host->fclk);
1637 mmc_release_host(host->mmc);
1638 return err;
1639 }
1640 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001641 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001642 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001643 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001644 if (mmc_slot(host).set_sleep)
1645 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1646 new_state == CARDSLEEP);
1647 /* FIXME: turn off bus power and perhaps interrupts too */
1648 clk_disable(host->fclk);
1649 host->dpm_state = new_state;
1650
1651 mmc_release_host(host->mmc);
1652
1653 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1654 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001655
Adrian Hunter1df58db2010-02-15 10:03:34 -08001656 if (mmc_slot(host).no_off)
1657 return 0;
1658
Denis Karpovdd498ef2009-09-22 16:44:49 -07001659 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1660 mmc_slot(host).card_detect ||
1661 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001662 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea2010-02-15 10:03:34 -08001663 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001664
1665 return 0;
1666}
1667
1668/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001669static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001670{
1671 if (!mmc_try_claim_host(host->mmc))
1672 return 0;
1673
Adrian Hunter1df58db2010-02-15 10:03:34 -08001674 if (mmc_slot(host).no_off)
1675 return 0;
1676
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001677 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1678 mmc_slot(host).card_detect ||
1679 (mmc_slot(host).get_cover_state &&
1680 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1681 mmc_release_host(host->mmc);
1682 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001683 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001684
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001685 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1686 host->vdd = 0;
1687 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001688
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001689 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1690 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001691
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001692 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001693
1694 mmc_release_host(host->mmc);
1695
1696 return 0;
1697}
1698
1699/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001700static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001701{
1702 int err;
1703
1704 err = clk_enable(host->fclk);
1705 if (err < 0)
1706 return err;
1707
Denis Karpov70a33412009-09-22 16:44:59 -07001708 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001709 host->dpm_state = ENABLED;
1710
1711 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1712
1713 return 0;
1714}
1715
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001716/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001717static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001718{
1719 if (!mmc_try_claim_host(host->mmc))
1720 return 0;
1721
1722 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001723 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001724 if (mmc_slot(host).set_sleep)
1725 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1726 host->vdd, host->dpm_state == CARDSLEEP);
1727 if (mmc_card_can_sleep(host->mmc))
1728 mmc_card_awake(host->mmc);
1729
1730 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1731 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1732
1733 host->dpm_state = ENABLED;
1734
1735 mmc_release_host(host->mmc);
1736
1737 return 0;
1738}
1739
Denis Karpovdd498ef2009-09-22 16:44:49 -07001740/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001741static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001742{
1743 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001744
Denis Karpov70a33412009-09-22 16:44:59 -07001745 omap_hsmmc_context_restore(host);
1746 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001747 mmc_power_restore_host(host->mmc);
1748
1749 host->dpm_state = ENABLED;
1750
1751 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1752
1753 return 0;
1754}
1755
1756/*
1757 * Bring MMC host to ENABLED from any other PM state.
1758 */
Denis Karpov70a33412009-09-22 16:44:59 -07001759static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001760{
Denis Karpov70a33412009-09-22 16:44:59 -07001761 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762
1763 switch (host->dpm_state) {
1764 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001765 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001766 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001767 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001768 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001769 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001770 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001771 default:
1772 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1773 return -EINVAL;
1774 }
1775}
1776
1777/*
1778 * Bring MMC host in PM state (one level deeper).
1779 */
Denis Karpov70a33412009-09-22 16:44:59 -07001780static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001781{
Denis Karpov70a33412009-09-22 16:44:59 -07001782 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001783
1784 switch (host->dpm_state) {
1785 case ENABLED: {
1786 int delay;
1787
Denis Karpov70a33412009-09-22 16:44:59 -07001788 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789 if (lazy || delay < 0)
1790 return delay;
1791 return 0;
1792 }
1793 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001794 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001795 case CARDSLEEP:
1796 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001797 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001798 default:
1799 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1800 return -EINVAL;
1801 }
1802}
1803
Denis Karpov70a33412009-09-22 16:44:59 -07001804static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001805{
Denis Karpov70a33412009-09-22 16:44:59 -07001806 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001807 int err;
1808
1809 err = clk_enable(host->fclk);
1810 if (err)
1811 return err;
1812 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001813 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001814 return 0;
1815}
1816
Denis Karpov70a33412009-09-22 16:44:59 -07001817static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818{
Denis Karpov70a33412009-09-22 16:44:59 -07001819 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001820
Denis Karpov70a33412009-09-22 16:44:59 -07001821 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001822 clk_disable(host->fclk);
1823 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1824 return 0;
1825}
1826
Denis Karpov70a33412009-09-22 16:44:59 -07001827static const struct mmc_host_ops omap_hsmmc_ops = {
1828 .enable = omap_hsmmc_enable_fclk,
1829 .disable = omap_hsmmc_disable_fclk,
1830 .request = omap_hsmmc_request,
1831 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001832 .get_cd = omap_hsmmc_get_cd,
1833 .get_ro = omap_hsmmc_get_ro,
1834 /* NYET -- enable_sdio_irq */
1835};
1836
Denis Karpov70a33412009-09-22 16:44:59 -07001837static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1838 .enable = omap_hsmmc_enable,
1839 .disable = omap_hsmmc_disable,
1840 .request = omap_hsmmc_request,
1841 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001842 .get_cd = omap_hsmmc_get_cd,
1843 .get_ro = omap_hsmmc_get_ro,
1844 /* NYET -- enable_sdio_irq */
1845};
1846
Denis Karpovd900f712009-09-22 16:44:38 -07001847#ifdef CONFIG_DEBUG_FS
1848
Denis Karpov70a33412009-09-22 16:44:59 -07001849static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001850{
1851 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001852 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001853 int context_loss = 0;
1854
Denis Karpov70a33412009-09-22 16:44:59 -07001855 if (host->pdata->get_context_loss_count)
1856 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001857
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001858 seq_printf(s, "mmc%d:\n"
1859 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001860 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001861 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001862 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001863 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001864 mmc->index, mmc->enabled ? 1 : 0,
1865 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001866 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001867
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001868 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001869 seq_printf(s, "host suspended, can't read registers\n");
1870 return 0;
1871 }
1872
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001873 if (clk_enable(host->fclk) != 0) {
1874 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001875 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001876 }
Denis Karpovd900f712009-09-22 16:44:38 -07001877
1878 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1879 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1880 seq_printf(s, "CON:\t\t0x%08x\n",
1881 OMAP_HSMMC_READ(host->base, CON));
1882 seq_printf(s, "HCTL:\t\t0x%08x\n",
1883 OMAP_HSMMC_READ(host->base, HCTL));
1884 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1885 OMAP_HSMMC_READ(host->base, SYSCTL));
1886 seq_printf(s, "IE:\t\t0x%08x\n",
1887 OMAP_HSMMC_READ(host->base, IE));
1888 seq_printf(s, "ISE:\t\t0x%08x\n",
1889 OMAP_HSMMC_READ(host->base, ISE));
1890 seq_printf(s, "CAPA:\t\t0x%08x\n",
1891 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001892
1893 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001894
Denis Karpovd900f712009-09-22 16:44:38 -07001895 return 0;
1896}
1897
Denis Karpov70a33412009-09-22 16:44:59 -07001898static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001899{
Denis Karpov70a33412009-09-22 16:44:59 -07001900 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001901}
1902
1903static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001904 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001905 .read = seq_read,
1906 .llseek = seq_lseek,
1907 .release = single_release,
1908};
1909
Denis Karpov70a33412009-09-22 16:44:59 -07001910static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001911{
1912 if (mmc->debugfs_root)
1913 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1914 mmc, &mmc_regs_fops);
1915}
1916
1917#else
1918
Denis Karpov70a33412009-09-22 16:44:59 -07001919static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001920{
1921}
1922
1923#endif
1924
Denis Karpov70a33412009-09-22 16:44:59 -07001925static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001926{
1927 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1928 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001929 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001930 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001931 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932
1933 if (pdata == NULL) {
1934 dev_err(&pdev->dev, "Platform Data is missing\n");
1935 return -ENXIO;
1936 }
1937
1938 if (pdata->nr_slots == 0) {
1939 dev_err(&pdev->dev, "No Slots\n");
1940 return -ENXIO;
1941 }
1942
1943 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1944 irq = platform_get_irq(pdev, 0);
1945 if (res == NULL || irq < 0)
1946 return -ENXIO;
1947
1948 res = request_mem_region(res->start, res->end - res->start + 1,
1949 pdev->name);
1950 if (res == NULL)
1951 return -EBUSY;
1952
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001953 ret = omap_hsmmc_gpio_init(pdata);
1954 if (ret)
1955 goto err;
1956
Denis Karpov70a33412009-09-22 16:44:59 -07001957 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958 if (!mmc) {
1959 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001960 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 }
1962
1963 host = mmc_priv(mmc);
1964 host->mmc = mmc;
1965 host->pdata = pdata;
1966 host->dev = &pdev->dev;
1967 host->use_dma = 1;
1968 host->dev->dma_mask = &pdata->dma_mask;
1969 host->dma_ch = -1;
1970 host->irq = irq;
1971 host->id = pdev->id;
1972 host->slot_id = 0;
1973 host->mapbase = res->start;
1974 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001975 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976
1977 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07001978 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979
Denis Karpov191d1f12009-09-22 16:44:55 -07001980 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07001981 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001982 else
Denis Karpov70a33412009-09-22 16:44:59 -07001983 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001984
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001985 mmc->f_min = 400000;
1986 mmc->f_max = 52000000;
1987
1988 sema_init(&host->sem, 1);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001989 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990
Russell King6f7607c2009-01-28 10:22:50 +00001991 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992 if (IS_ERR(host->iclk)) {
1993 ret = PTR_ERR(host->iclk);
1994 host->iclk = NULL;
1995 goto err1;
1996 }
Russell King6f7607c2009-01-28 10:22:50 +00001997 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998 if (IS_ERR(host->fclk)) {
1999 ret = PTR_ERR(host->fclk);
2000 host->fclk = NULL;
2001 clk_put(host->iclk);
2002 goto err1;
2003 }
2004
Denis Karpov70a33412009-09-22 16:44:59 -07002005 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002006
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002007 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002008 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2009 /* we start off in DISABLED state */
2010 host->dpm_state = DISABLED;
2011
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002012 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002013 clk_put(host->iclk);
2014 clk_put(host->fclk);
2015 goto err1;
2016 }
2017
2018 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002019 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 clk_put(host->iclk);
2021 clk_put(host->fclk);
2022 goto err1;
2023 }
2024
Adrian Hunter2bec0892009-09-22 16:45:02 -07002025 if (cpu_is_omap2430()) {
2026 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2027 /*
2028 * MMC can still work without debounce clock.
2029 */
2030 if (IS_ERR(host->dbclk))
2031 dev_warn(mmc_dev(host->mmc),
2032 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002034 host->got_dbclk = 1;
2035
2036 if (host->got_dbclk)
2037 if (clk_enable(host->dbclk) != 0)
2038 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2039 " clk failed\n");
2040 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002041
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002042 /* Since we do only SG emulation, we can have as many segs
2043 * as we want. */
2044 mmc->max_phys_segs = 1024;
2045 mmc->max_hw_segs = 1024;
2046
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2048 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2049 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2050 mmc->max_seg_size = mmc->max_req_size;
2051
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002052 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
2053 MMC_CAP_WAIT_WHILE_BUSY;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054
Denis Karpov191d1f12009-09-22 16:44:55 -07002055 if (mmc_slot(host).wires >= 8)
Jarkko Lavinen73153012008-11-21 16:49:54 +02002056 mmc->caps |= MMC_CAP_8_BIT_DATA;
Denis Karpov191d1f12009-09-22 16:44:55 -07002057 else if (mmc_slot(host).wires >= 4)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058 mmc->caps |= MMC_CAP_4_BIT_DATA;
2059
Denis Karpov191d1f12009-09-22 16:44:55 -07002060 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002061 mmc->caps |= MMC_CAP_NONREMOVABLE;
2062
Denis Karpov70a33412009-09-22 16:44:59 -07002063 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002065 /* Select DMA lines */
2066 switch (host->id) {
2067 case OMAP_MMC1_DEVID:
2068 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2069 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2070 break;
2071 case OMAP_MMC2_DEVID:
2072 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2073 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2074 break;
2075 case OMAP_MMC3_DEVID:
2076 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2077 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2078 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002079 case OMAP_MMC4_DEVID:
2080 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2081 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2082 break;
2083 case OMAP_MMC5_DEVID:
2084 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2085 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2086 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002087 default:
2088 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2089 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002090 }
2091
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002092 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002093 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002094 mmc_hostname(mmc), host);
2095 if (ret) {
2096 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2097 goto err_irq;
2098 }
2099
2100 if (pdata->init != NULL) {
2101 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002102 dev_dbg(mmc_dev(host->mmc),
2103 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104 goto err_irq_cd_init;
2105 }
2106 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002107
2108 if (!mmc_slot(host).set_power) {
2109 ret = omap_hsmmc_reg_get(host);
2110 if (ret)
2111 goto err_reg;
2112 host->use_reg = 1;
2113 }
2114
David Brownellb583f262009-05-28 14:04:03 -07002115 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116
2117 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002118 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002120 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2122 | IRQF_DISABLED,
2123 mmc_hostname(mmc), host);
2124 if (ret) {
2125 dev_dbg(mmc_dev(host->mmc),
2126 "Unable to grab MMC CD IRQ\n");
2127 goto err_irq_cd;
2128 }
2129 }
2130
2131 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
2132 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
2133
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002134 mmc_host_lazy_disable(host->mmc);
2135
Adrian Hunterb62f6222009-09-22 16:45:01 -07002136 omap_hsmmc_protect_card(host);
2137
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138 mmc_add_host(mmc);
2139
Denis Karpov191d1f12009-09-22 16:44:55 -07002140 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2142 if (ret < 0)
2143 goto err_slot_name;
2144 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002145 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 ret = device_create_file(&mmc->class_dev,
2147 &dev_attr_cover_switch);
2148 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002149 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 }
2151
Denis Karpov70a33412009-09-22 16:44:59 -07002152 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002154 return 0;
2155
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156err_slot_name:
2157 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002159err_irq_cd:
2160 if (host->use_reg)
2161 omap_hsmmc_reg_put(host);
2162err_reg:
2163 if (host->pdata->cleanup)
2164 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002165err_irq_cd_init:
2166 free_irq(host->irq, host);
2167err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002168 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169 clk_disable(host->iclk);
2170 clk_put(host->fclk);
2171 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002172 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173 clk_disable(host->dbclk);
2174 clk_put(host->dbclk);
2175 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002176err1:
2177 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002178 platform_set_drvdata(pdev, NULL);
2179 mmc_free_host(mmc);
2180err_alloc:
2181 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183 release_mem_region(res->start, res->end - res->start + 1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002184 return ret;
2185}
2186
Denis Karpov70a33412009-09-22 16:44:59 -07002187static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002188{
Denis Karpov70a33412009-09-22 16:44:59 -07002189 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002190 struct resource *res;
2191
2192 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002193 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002194 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002195 if (host->use_reg)
2196 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002197 if (host->pdata->cleanup)
2198 host->pdata->cleanup(&pdev->dev);
2199 free_irq(host->irq, host);
2200 if (mmc_slot(host).card_detect_irq)
2201 free_irq(mmc_slot(host).card_detect_irq, host);
2202 flush_scheduled_work();
2203
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002204 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205 clk_disable(host->iclk);
2206 clk_put(host->fclk);
2207 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002208 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002209 clk_disable(host->dbclk);
2210 clk_put(host->dbclk);
2211 }
2212
2213 mmc_free_host(host->mmc);
2214 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002215 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216 }
2217
2218 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2219 if (res)
2220 release_mem_region(res->start, res->end - res->start + 1);
2221 platform_set_drvdata(pdev, NULL);
2222
2223 return 0;
2224}
2225
2226#ifdef CONFIG_PM
Denis Karpov70a33412009-09-22 16:44:59 -07002227static int omap_hsmmc_suspend(struct platform_device *pdev, pm_message_t state)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228{
2229 int ret = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002230 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231
2232 if (host && host->suspended)
2233 return 0;
2234
2235 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002236 host->suspended = 1;
2237 if (host->pdata->suspend) {
2238 ret = host->pdata->suspend(&pdev->dev,
2239 host->slot_id);
2240 if (ret) {
2241 dev_dbg(mmc_dev(host->mmc),
2242 "Unable to handle MMC board"
2243 " level suspend\n");
2244 host->suspended = 0;
2245 return ret;
2246 }
2247 }
2248 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002249 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002250 ret = mmc_suspend_host(host->mmc, state);
2251 if (ret == 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2253 OMAP_HSMMC_WRITE(host->base, IE, 0);
2254
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002255
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002256 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002257 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002258 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002259 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002260 if (host->got_dbclk)
2261 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002262 } else {
2263 host->suspended = 0;
2264 if (host->pdata->resume) {
2265 ret = host->pdata->resume(&pdev->dev,
2266 host->slot_id);
2267 if (ret)
2268 dev_dbg(mmc_dev(host->mmc),
2269 "Unmask interrupt failed\n");
2270 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002271 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002272 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273
2274 }
2275 return ret;
2276}
2277
2278/* Routine to resume the MMC device */
Denis Karpov70a33412009-09-22 16:44:59 -07002279static int omap_hsmmc_resume(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002280{
2281 int ret = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002282 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002283
2284 if (host && !host->suspended)
2285 return 0;
2286
2287 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002288 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002289 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002291
Denis Karpov11dd62a2009-09-22 16:44:43 -07002292 if (mmc_host_enable(host->mmc) != 0) {
2293 clk_disable(host->iclk);
2294 goto clk_en_err;
2295 }
2296
Adrian Hunter2bec0892009-09-22 16:45:02 -07002297 if (host->got_dbclk)
2298 clk_enable(host->dbclk);
2299
Denis Karpov70a33412009-09-22 16:44:59 -07002300 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002301
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002302 if (host->pdata->resume) {
2303 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2304 if (ret)
2305 dev_dbg(mmc_dev(host->mmc),
2306 "Unmask interrupt failed\n");
2307 }
2308
Adrian Hunterb62f6222009-09-22 16:45:01 -07002309 omap_hsmmc_protect_card(host);
2310
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002311 /* Notify the core to resume the host */
2312 ret = mmc_resume_host(host->mmc);
2313 if (ret == 0)
2314 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002315
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002316 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002317 }
2318
2319 return ret;
2320
2321clk_en_err:
2322 dev_dbg(mmc_dev(host->mmc),
2323 "Failed to enable MMC clocks during resume\n");
2324 return ret;
2325}
2326
2327#else
Denis Karpov70a33412009-09-22 16:44:59 -07002328#define omap_hsmmc_suspend NULL
2329#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002330#endif
2331
Denis Karpov70a33412009-09-22 16:44:59 -07002332static struct platform_driver omap_hsmmc_driver = {
2333 .remove = omap_hsmmc_remove,
2334 .suspend = omap_hsmmc_suspend,
2335 .resume = omap_hsmmc_resume,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002336 .driver = {
2337 .name = DRIVER_NAME,
2338 .owner = THIS_MODULE,
2339 },
2340};
2341
Denis Karpov70a33412009-09-22 16:44:59 -07002342static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002343{
2344 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002345 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002346}
2347
Denis Karpov70a33412009-09-22 16:44:59 -07002348static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002349{
2350 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002351 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002352}
2353
Denis Karpov70a33412009-09-22 16:44:59 -07002354module_init(omap_hsmmc_init);
2355module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002356
2357MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2358MODULE_LICENSE("GPL");
2359MODULE_ALIAS("platform:" DRIVER_NAME);
2360MODULE_AUTHOR("Texas Instruments Inc");