blob: 69858e75020381325f5a75b3c26ade9e140b2836 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Denis Karpovd900f712009-09-22 16:44:38 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010022#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/clk.h>
29#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070030#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070031#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/io.h>
33#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080034#include <linux/gpio.h>
35#include <linux/regulator/consumer.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070038#include <plat/board.h>
39#include <plat/mmc.h>
40#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041
42/* OMAP HSMMC Host Controller Registers */
43#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070044#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045#define OMAP_HSMMC_CON 0x002C
46#define OMAP_HSMMC_BLK 0x0104
47#define OMAP_HSMMC_ARG 0x0108
48#define OMAP_HSMMC_CMD 0x010C
49#define OMAP_HSMMC_RSP10 0x0110
50#define OMAP_HSMMC_RSP32 0x0114
51#define OMAP_HSMMC_RSP54 0x0118
52#define OMAP_HSMMC_RSP76 0x011C
53#define OMAP_HSMMC_DATA 0x0120
54#define OMAP_HSMMC_HCTL 0x0128
55#define OMAP_HSMMC_SYSCTL 0x012C
56#define OMAP_HSMMC_STAT 0x0130
57#define OMAP_HSMMC_IE 0x0134
58#define OMAP_HSMMC_ISE 0x0138
59#define OMAP_HSMMC_CAPA 0x0140
60
61#define VS18 (1 << 26)
62#define VS30 (1 << 25)
63#define SDVS18 (0x5 << 9)
64#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080065#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010066#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define SDVSCLR 0xFFFFF1FF
68#define SDVSDET 0x00000400
69#define AUTOIDLE 0x1
70#define SDBP (1 << 8)
71#define DTO 0xe
72#define ICE 0x1
73#define ICS 0x2
74#define CEN (1 << 2)
75#define CLKD_MASK 0x0000FFC0
76#define CLKD_SHIFT 6
77#define DTO_MASK 0x000F0000
78#define DTO_SHIFT 16
79#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070080#define BWR_ENABLE (1 << 4)
81#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070082#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define INIT_STREAM (1 << 1)
84#define DP_SELECT (1 << 21)
85#define DDIR (1 << 4)
86#define DMA_EN 0x1
87#define MSBS (1 << 5)
88#define BCE (1 << 1)
89#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020090#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define CC 0x1
92#define TC 0x02
93#define OD 0x1
94#define ERR (1 << 15)
95#define CMD_TIMEOUT (1 << 16)
96#define DATA_TIMEOUT (1 << 20)
97#define CMD_CRC (1 << 17)
98#define DATA_CRC (1 << 21)
99#define CARD_ERR (1 << 28)
100#define STAT_CLEAR 0xFFFFFFFF
101#define INIT_STREAM_CMD 0x00000000
102#define DUAL_VOLT_OCR_BIT 7
103#define SRC (1 << 25)
104#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700105#define SOFTRESET (1 << 1)
106#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100107
108/*
109 * FIXME: Most likely all the data using these _DEVID defines should come
110 * from the platform_data, or implemented in controller and slot specific
111 * functions.
112 */
113#define OMAP_MMC1_DEVID 0
114#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000115#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700116#define OMAP_MMC4_DEVID 3
117#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119#define MMC_TIMEOUT_MS 20
120#define OMAP_MMC_MASTER_CLOCK 96000000
121#define DRIVER_NAME "mmci-omap-hs"
122
Denis Karpovdd498ef2009-09-22 16:44:49 -0700123/* Timeouts for entering power saving states on inactivity, msec */
124#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700125#define OMAP_MMC_SLEEP_TIMEOUT 1000
126#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100128/*
129 * One controller can have multiple slots, like on some omap boards using
130 * omap.c controller driver. Luckily this is not currently done on any known
131 * omap_hsmmc.c device.
132 */
133#define mmc_slot(host) (host->pdata->slots[host->slot_id])
134
135/*
136 * MMC Host controller read/write API's
137 */
138#define OMAP_HSMMC_READ(base, reg) \
139 __raw_readl((base) + OMAP_HSMMC_##reg)
140
141#define OMAP_HSMMC_WRITE(base, reg, val) \
142 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
143
Denis Karpov70a33412009-09-22 16:44:59 -0700144struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100145 struct device *dev;
146 struct mmc_host *mmc;
147 struct mmc_request *mrq;
148 struct mmc_command *cmd;
149 struct mmc_data *data;
150 struct clk *fclk;
151 struct clk *iclk;
152 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800153 /*
154 * vcc == configured supply
155 * vcc_aux == optional
156 * - MMC1, supply for DAT4..DAT7
157 * - MMC2/MMC2, external level shifter voltage supply, for
158 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
159 */
160 struct regulator *vcc;
161 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 struct work_struct mmc_carddetect_work;
163 void __iomem *base;
164 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700165 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 unsigned int id;
167 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200168 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700170 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 u32 *buffer;
172 u32 bytesleft;
173 int suspended;
174 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000176 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700178 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200179 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700181 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700182 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700183 int protect_card;
184 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800185 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700186 int req_in_progress;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700187
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 struct omap_mmc_platform_data *pdata;
189};
190
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800191static int omap_hsmmc_card_detect(struct device *dev, int slot)
192{
193 struct omap_mmc_platform_data *mmc = dev->platform_data;
194
195 /* NOTE: assumes card detect signal is active-low */
196 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
197}
198
199static int omap_hsmmc_get_wp(struct device *dev, int slot)
200{
201 struct omap_mmc_platform_data *mmc = dev->platform_data;
202
203 /* NOTE: assumes write protect signal is active-high */
204 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
205}
206
207static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
208{
209 struct omap_mmc_platform_data *mmc = dev->platform_data;
210
211 /* NOTE: assumes card detect signal is active-low */
212 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
213}
214
215#ifdef CONFIG_PM
216
217static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
218{
219 struct omap_mmc_platform_data *mmc = dev->platform_data;
220
221 disable_irq(mmc->slots[0].card_detect_irq);
222 return 0;
223}
224
225static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
226{
227 struct omap_mmc_platform_data *mmc = dev->platform_data;
228
229 enable_irq(mmc->slots[0].card_detect_irq);
230 return 0;
231}
232
233#else
234
235#define omap_hsmmc_suspend_cdirq NULL
236#define omap_hsmmc_resume_cdirq NULL
237
238#endif
239
Adrian Hunterb702b102010-02-15 10:03:35 -0800240#ifdef CONFIG_REGULATOR
241
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
243 int vdd)
244{
245 struct omap_hsmmc_host *host =
246 platform_get_drvdata(to_platform_device(dev));
247 int ret;
248
249 if (mmc_slot(host).before_set_reg)
250 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
251
252 if (power_on)
253 ret = mmc_regulator_set_ocr(host->vcc, vdd);
254 else
255 ret = mmc_regulator_set_ocr(host->vcc, 0);
256
257 if (mmc_slot(host).after_set_reg)
258 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
259
260 return ret;
261}
262
263static int omap_hsmmc_23_set_power(struct device *dev, int slot, int power_on,
264 int vdd)
265{
266 struct omap_hsmmc_host *host =
267 platform_get_drvdata(to_platform_device(dev));
268 int ret = 0;
269
270 /*
271 * If we don't see a Vcc regulator, assume it's a fixed
272 * voltage always-on regulator.
273 */
274 if (!host->vcc)
275 return 0;
276
277 if (mmc_slot(host).before_set_reg)
278 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
279
280 /*
281 * Assume Vcc regulator is used only to power the card ... OMAP
282 * VDDS is used to power the pins, optionally with a transceiver to
283 * support cards using voltages other than VDDS (1.8V nominal). When a
284 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
285 *
286 * In some cases this regulator won't support enable/disable;
287 * e.g. it's a fixed rail for a WLAN chip.
288 *
289 * In other cases vcc_aux switches interface power. Example, for
290 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
291 * chips/cards need an interface voltage rail too.
292 */
293 if (power_on) {
294 ret = mmc_regulator_set_ocr(host->vcc, vdd);
295 /* Enable interface voltage rail, if needed */
296 if (ret == 0 && host->vcc_aux) {
297 ret = regulator_enable(host->vcc_aux);
298 if (ret < 0)
299 ret = mmc_regulator_set_ocr(host->vcc, 0);
300 }
301 } else {
Adrian Hunter6da20c82010-02-15 10:03:34 -0800302 if (host->vcc_aux)
303 ret = regulator_disable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800304 if (ret == 0)
305 ret = mmc_regulator_set_ocr(host->vcc, 0);
306 }
307
308 if (mmc_slot(host).after_set_reg)
309 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
310
311 return ret;
312}
313
314static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
315 int vdd, int cardsleep)
316{
317 struct omap_hsmmc_host *host =
318 platform_get_drvdata(to_platform_device(dev));
319 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
320
321 return regulator_set_mode(host->vcc, mode);
322}
323
324static int omap_hsmmc_23_set_sleep(struct device *dev, int slot, int sleep,
325 int vdd, int cardsleep)
326{
327 struct omap_hsmmc_host *host =
328 platform_get_drvdata(to_platform_device(dev));
329 int err, mode;
330
331 /*
332 * If we don't see a Vcc regulator, assume it's a fixed
333 * voltage always-on regulator.
334 */
335 if (!host->vcc)
336 return 0;
337
338 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
339
340 if (!host->vcc_aux)
341 return regulator_set_mode(host->vcc, mode);
342
343 if (cardsleep) {
344 /* VCC can be turned off if card is asleep */
345 if (sleep)
346 err = mmc_regulator_set_ocr(host->vcc, 0);
347 else
348 err = mmc_regulator_set_ocr(host->vcc, vdd);
349 } else
350 err = regulator_set_mode(host->vcc, mode);
351 if (err)
352 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800353
354 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
355 return regulator_set_mode(host->vcc_aux, mode);
356
357 if (sleep)
358 return regulator_disable(host->vcc_aux);
359 else
360 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800361}
362
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800363static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
364{
365 struct regulator *reg;
366 int ret = 0;
367
368 switch (host->id) {
369 case OMAP_MMC1_DEVID:
370 /* On-chip level shifting via PBIAS0/PBIAS1 */
371 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
372 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
373 break;
374 case OMAP_MMC2_DEVID:
375 case OMAP_MMC3_DEVID:
376 /* Off-chip level shifting, or none */
377 mmc_slot(host).set_power = omap_hsmmc_23_set_power;
378 mmc_slot(host).set_sleep = omap_hsmmc_23_set_sleep;
379 break;
380 default:
381 pr_err("MMC%d configuration not supported!\n", host->id);
382 return -EINVAL;
383 }
384
385 reg = regulator_get(host->dev, "vmmc");
386 if (IS_ERR(reg)) {
387 dev_dbg(host->dev, "vmmc regulator missing\n");
388 /*
389 * HACK: until fixed.c regulator is usable,
390 * we don't require a main regulator
391 * for MMC2 or MMC3
392 */
393 if (host->id == OMAP_MMC1_DEVID) {
394 ret = PTR_ERR(reg);
395 goto err;
396 }
397 } else {
398 host->vcc = reg;
399 mmc_slot(host).ocr_mask = mmc_regulator_get_ocrmask(reg);
400
401 /* Allow an aux regulator */
402 reg = regulator_get(host->dev, "vmmc_aux");
403 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
404
405 /*
406 * UGLY HACK: workaround regulator framework bugs.
407 * When the bootloader leaves a supply active, it's
408 * initialized with zero usecount ... and we can't
409 * disable it without first enabling it. Until the
410 * framework is fixed, we need a workaround like this
411 * (which is safe for MMC, but not in general).
412 */
413 if (regulator_is_enabled(host->vcc) > 0) {
414 regulator_enable(host->vcc);
415 regulator_disable(host->vcc);
416 }
417 if (host->vcc_aux) {
418 if (regulator_is_enabled(reg) > 0) {
419 regulator_enable(reg);
420 regulator_disable(reg);
421 }
422 }
423 }
424
425 return 0;
426
427err:
428 mmc_slot(host).set_power = NULL;
429 mmc_slot(host).set_sleep = NULL;
430 return ret;
431}
432
433static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
434{
435 regulator_put(host->vcc);
436 regulator_put(host->vcc_aux);
437 mmc_slot(host).set_power = NULL;
438 mmc_slot(host).set_sleep = NULL;
439}
440
Adrian Hunterb702b102010-02-15 10:03:35 -0800441static inline int omap_hsmmc_have_reg(void)
442{
443 return 1;
444}
445
446#else
447
448static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
449{
450 return -EINVAL;
451}
452
453static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
454{
455}
456
457static inline int omap_hsmmc_have_reg(void)
458{
459 return 0;
460}
461
462#endif
463
464static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
465{
466 int ret;
467
468 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
469 pdata->suspend = omap_hsmmc_suspend_cdirq;
470 pdata->resume = omap_hsmmc_resume_cdirq;
471 if (pdata->slots[0].cover)
472 pdata->slots[0].get_cover_state =
473 omap_hsmmc_get_cover_state;
474 else
475 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
476 pdata->slots[0].card_detect_irq =
477 gpio_to_irq(pdata->slots[0].switch_pin);
478 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
479 if (ret)
480 return ret;
481 ret = gpio_direction_input(pdata->slots[0].switch_pin);
482 if (ret)
483 goto err_free_sp;
484 } else
485 pdata->slots[0].switch_pin = -EINVAL;
486
487 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
488 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
489 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
490 if (ret)
491 goto err_free_cd;
492 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
493 if (ret)
494 goto err_free_wp;
495 } else
496 pdata->slots[0].gpio_wp = -EINVAL;
497
498 return 0;
499
500err_free_wp:
501 gpio_free(pdata->slots[0].gpio_wp);
502err_free_cd:
503 if (gpio_is_valid(pdata->slots[0].switch_pin))
504err_free_sp:
505 gpio_free(pdata->slots[0].switch_pin);
506 return ret;
507}
508
509static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
510{
511 if (gpio_is_valid(pdata->slots[0].gpio_wp))
512 gpio_free(pdata->slots[0].gpio_wp);
513 if (gpio_is_valid(pdata->slots[0].switch_pin))
514 gpio_free(pdata->slots[0].switch_pin);
515}
516
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100517/*
518 * Stop clock to the card
519 */
Denis Karpov70a33412009-09-22 16:44:59 -0700520static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100521{
522 OMAP_HSMMC_WRITE(host->base, SYSCTL,
523 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
524 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
525 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
526}
527
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700528static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
529 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700530{
531 unsigned int irq_mask;
532
533 if (host->use_dma)
534 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
535 else
536 irq_mask = INT_EN_MASK;
537
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700538 /* Disable timeout for erases */
539 if (cmd->opcode == MMC_ERASE)
540 irq_mask &= ~DTO_ENABLE;
541
Adrian Hunterb4175772010-05-26 14:42:06 -0700542 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
543 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
544 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
545}
546
547static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
548{
549 OMAP_HSMMC_WRITE(host->base, ISE, 0);
550 OMAP_HSMMC_WRITE(host->base, IE, 0);
551 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
552}
553
Denis Karpov11dd62a2009-09-22 16:44:43 -0700554#ifdef CONFIG_PM
555
556/*
557 * Restore the MMC host context, if it was lost as result of a
558 * power state change.
559 */
Denis Karpov70a33412009-09-22 16:44:59 -0700560static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700561{
562 struct mmc_ios *ios = &host->mmc->ios;
563 struct omap_mmc_platform_data *pdata = host->pdata;
564 int context_loss = 0;
565 u32 hctl, capa, con;
566 u16 dsor = 0;
567 unsigned long timeout;
568
569 if (pdata->get_context_loss_count) {
570 context_loss = pdata->get_context_loss_count(host->dev);
571 if (context_loss < 0)
572 return 1;
573 }
574
575 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
576 context_loss == host->context_loss ? "not " : "");
577 if (host->context_loss == context_loss)
578 return 1;
579
580 /* Wait for hardware reset */
581 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
582 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
583 && time_before(jiffies, timeout))
584 ;
585
586 /* Do software reset */
587 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
588 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
589 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
590 && time_before(jiffies, timeout))
591 ;
592
593 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
594 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
595
596 if (host->id == OMAP_MMC1_DEVID) {
597 if (host->power_mode != MMC_POWER_OFF &&
598 (1 << ios->vdd) <= MMC_VDD_23_24)
599 hctl = SDVS18;
600 else
601 hctl = SDVS30;
602 capa = VS30 | VS18;
603 } else {
604 hctl = SDVS18;
605 capa = VS18;
606 }
607
608 OMAP_HSMMC_WRITE(host->base, HCTL,
609 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
610
611 OMAP_HSMMC_WRITE(host->base, CAPA,
612 OMAP_HSMMC_READ(host->base, CAPA) | capa);
613
614 OMAP_HSMMC_WRITE(host->base, HCTL,
615 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
616
617 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
618 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
619 && time_before(jiffies, timeout))
620 ;
621
Adrian Hunterb4175772010-05-26 14:42:06 -0700622 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700623
624 /* Do not initialize card-specific things if the power is off */
625 if (host->power_mode == MMC_POWER_OFF)
626 goto out;
627
628 con = OMAP_HSMMC_READ(host->base, CON);
629 switch (ios->bus_width) {
630 case MMC_BUS_WIDTH_8:
631 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
632 break;
633 case MMC_BUS_WIDTH_4:
634 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
635 OMAP_HSMMC_WRITE(host->base, HCTL,
636 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
637 break;
638 case MMC_BUS_WIDTH_1:
639 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
640 OMAP_HSMMC_WRITE(host->base, HCTL,
641 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
642 break;
643 }
644
645 if (ios->clock) {
646 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
647 if (dsor < 1)
648 dsor = 1;
649
650 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
651 dsor++;
652
653 if (dsor > 250)
654 dsor = 250;
655 }
656
657 OMAP_HSMMC_WRITE(host->base, SYSCTL,
658 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
659 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
660 OMAP_HSMMC_WRITE(host->base, SYSCTL,
661 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
662
663 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
664 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
665 && time_before(jiffies, timeout))
666 ;
667
668 OMAP_HSMMC_WRITE(host->base, SYSCTL,
669 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
670
671 con = OMAP_HSMMC_READ(host->base, CON);
672 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
673 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
674 else
675 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
676out:
677 host->context_loss = context_loss;
678
679 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
680 return 0;
681}
682
683/*
684 * Save the MMC host context (store the number of power state changes so far).
685 */
Denis Karpov70a33412009-09-22 16:44:59 -0700686static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700687{
688 struct omap_mmc_platform_data *pdata = host->pdata;
689 int context_loss;
690
691 if (pdata->get_context_loss_count) {
692 context_loss = pdata->get_context_loss_count(host->dev);
693 if (context_loss < 0)
694 return;
695 host->context_loss = context_loss;
696 }
697}
698
699#else
700
Denis Karpov70a33412009-09-22 16:44:59 -0700701static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700702{
703 return 0;
704}
705
Denis Karpov70a33412009-09-22 16:44:59 -0700706static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700707{
708}
709
710#endif
711
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100712/*
713 * Send init stream sequence to card
714 * before sending IDLE command
715 */
Denis Karpov70a33412009-09-22 16:44:59 -0700716static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717{
718 int reg = 0;
719 unsigned long timeout;
720
Adrian Hunterb62f6222009-09-22 16:45:01 -0700721 if (host->protect_card)
722 return;
723
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100724 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700725
726 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100727 OMAP_HSMMC_WRITE(host->base, CON,
728 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
729 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
730
731 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
732 while ((reg != CC) && time_before(jiffies, timeout))
733 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
734
735 OMAP_HSMMC_WRITE(host->base, CON,
736 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700737
738 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
739 OMAP_HSMMC_READ(host->base, STAT);
740
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741 enable_irq(host->irq);
742}
743
744static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700745int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746{
747 int r = 1;
748
Denis Karpov191d1f12009-09-22 16:44:55 -0700749 if (mmc_slot(host).get_cover_state)
750 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100751 return r;
752}
753
754static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700755omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756 char *buf)
757{
758 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700759 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760
Denis Karpov70a33412009-09-22 16:44:59 -0700761 return sprintf(buf, "%s\n",
762 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763}
764
Denis Karpov70a33412009-09-22 16:44:59 -0700765static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766
767static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700768omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 char *buf)
770{
771 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700772 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773
Denis Karpov191d1f12009-09-22 16:44:55 -0700774 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100775}
776
Denis Karpov70a33412009-09-22 16:44:59 -0700777static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778
779/*
780 * Configure the response type and send the cmd.
781 */
782static void
Denis Karpov70a33412009-09-22 16:44:59 -0700783omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100784 struct mmc_data *data)
785{
786 int cmdreg = 0, resptype = 0, cmdtype = 0;
787
788 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
789 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
790 host->cmd = cmd;
791
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700792 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200794 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795 if (cmd->flags & MMC_RSP_PRESENT) {
796 if (cmd->flags & MMC_RSP_136)
797 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200798 else if (cmd->flags & MMC_RSP_BUSY) {
799 resptype = 3;
800 host->response_busy = 1;
801 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802 resptype = 2;
803 }
804
805 /*
806 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
807 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
808 * a val of 0x3, rest 0x0.
809 */
810 if (cmd == host->mrq->stop)
811 cmdtype = 0x3;
812
813 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
814
815 if (data) {
816 cmdreg |= DP_SELECT | MSBS | BCE;
817 if (data->flags & MMC_DATA_READ)
818 cmdreg |= DDIR;
819 else
820 cmdreg &= ~(DDIR);
821 }
822
823 if (host->use_dma)
824 cmdreg |= DMA_EN;
825
Adrian Hunterb4175772010-05-26 14:42:06 -0700826 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700827
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
829 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
830}
831
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200832static int
Denis Karpov70a33412009-09-22 16:44:59 -0700833omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200834{
835 if (data->flags & MMC_DATA_WRITE)
836 return DMA_TO_DEVICE;
837 else
838 return DMA_FROM_DEVICE;
839}
840
Adrian Hunterb4175772010-05-26 14:42:06 -0700841static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
842{
843 int dma_ch;
844
845 spin_lock(&host->irq_lock);
846 host->req_in_progress = 0;
847 dma_ch = host->dma_ch;
848 spin_unlock(&host->irq_lock);
849
850 omap_hsmmc_disable_irq(host);
851 /* Do not complete the request if DMA is still in progress */
852 if (mrq->data && host->use_dma && dma_ch != -1)
853 return;
854 host->mrq = NULL;
855 mmc_request_done(host->mmc, mrq);
856}
857
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100858/*
859 * Notify the transfer complete to MMC core
860 */
861static void
Denis Karpov70a33412009-09-22 16:44:59 -0700862omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100863{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200864 if (!data) {
865 struct mmc_request *mrq = host->mrq;
866
Adrian Hunter23050102009-09-22 16:44:57 -0700867 /* TC before CC from CMD6 - don't know why, but it happens */
868 if (host->cmd && host->cmd->opcode == 6 &&
869 host->response_busy) {
870 host->response_busy = 0;
871 return;
872 }
873
Adrian Hunterb4175772010-05-26 14:42:06 -0700874 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200875 return;
876 }
877
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878 host->data = NULL;
879
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100880 if (!data->error)
881 data->bytes_xfered += data->blocks * (data->blksz);
882 else
883 data->bytes_xfered = 0;
884
885 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700886 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100887 return;
888 }
Denis Karpov70a33412009-09-22 16:44:59 -0700889 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100890}
891
892/*
893 * Notify the core about command completion
894 */
895static void
Denis Karpov70a33412009-09-22 16:44:59 -0700896omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100897{
898 host->cmd = NULL;
899
900 if (cmd->flags & MMC_RSP_PRESENT) {
901 if (cmd->flags & MMC_RSP_136) {
902 /* response type 2 */
903 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
904 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
905 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
906 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
907 } else {
908 /* response types 1, 1b, 3, 4, 5, 6 */
909 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
910 }
911 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700912 if ((host->data == NULL && !host->response_busy) || cmd->error)
913 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914}
915
916/*
917 * DMA clean up for command errors
918 */
Denis Karpov70a33412009-09-22 16:44:59 -0700919static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920{
Adrian Hunterb4175772010-05-26 14:42:06 -0700921 int dma_ch;
922
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200923 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100924
Adrian Hunterb4175772010-05-26 14:42:06 -0700925 spin_lock(&host->irq_lock);
926 dma_ch = host->dma_ch;
927 host->dma_ch = -1;
928 spin_unlock(&host->irq_lock);
929
930 if (host->use_dma && dma_ch != -1) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100931 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700932 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700933 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934 }
935 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936}
937
938/*
939 * Readable error output
940 */
941#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700942static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943{
944 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700945 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100946 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
947 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
948 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
949 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
950 };
951 char res[256];
952 char *buf = res;
953 int len, i;
954
955 len = sprintf(buf, "MMC IRQ 0x%x :", status);
956 buf += len;
957
Denis Karpov70a33412009-09-22 16:44:59 -0700958 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700960 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961 buf += len;
962 }
963
964 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
965}
966#endif /* CONFIG_MMC_DEBUG */
967
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100968/*
969 * MMC controller internal state machines reset
970 *
971 * Used to reset command or data internal state machines, using respectively
972 * SRC or SRD bit of SYSCTL register
973 * Can be called from interrupt context
974 */
Denis Karpov70a33412009-09-22 16:44:59 -0700975static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
976 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100977{
978 unsigned long i = 0;
979 unsigned long limit = (loops_per_jiffy *
980 msecs_to_jiffies(MMC_TIMEOUT_MS));
981
982 OMAP_HSMMC_WRITE(host->base, SYSCTL,
983 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
984
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700985 /*
986 * OMAP4 ES2 and greater has an updated reset logic.
987 * Monitor a 0->1 transition first
988 */
989 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
990 while ((!(OMAP_HSMMC_READ(host, SYSCTL) & bit))
991 && (i++ < limit))
992 cpu_relax();
993 }
994 i = 0;
995
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100996 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
997 (i++ < limit))
998 cpu_relax();
999
1000 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1001 dev_err(mmc_dev(host->mmc),
1002 "Timeout waiting on controller reset in %s\n",
1003 __func__);
1004}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001005
Adrian Hunterb4175772010-05-26 14:42:06 -07001006static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001007{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001008 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001009 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010
Adrian Hunterb4175772010-05-26 14:42:06 -07001011 if (!host->req_in_progress) {
1012 do {
1013 OMAP_HSMMC_WRITE(host->base, STAT, status);
1014 /* Flush posted write */
1015 status = OMAP_HSMMC_READ(host->base, STAT);
1016 } while (status & INT_EN_MASK);
1017 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001018 }
1019
1020 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1022
1023 if (status & ERR) {
1024#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001025 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026#endif
1027 if ((status & CMD_TIMEOUT) ||
1028 (status & CMD_CRC)) {
1029 if (host->cmd) {
1030 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001031 omap_hsmmc_reset_controller_fsm(host,
1032 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033 host->cmd->error = -ETIMEDOUT;
1034 } else {
1035 host->cmd->error = -EILSEQ;
1036 }
1037 end_cmd = 1;
1038 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001039 if (host->data || host->response_busy) {
1040 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001041 omap_hsmmc_dma_cleanup(host,
1042 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001043 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001044 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001045 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046 }
1047 if ((status & DATA_TIMEOUT) ||
1048 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001049 if (host->data || host->response_busy) {
1050 int err = (status & DATA_TIMEOUT) ?
1051 -ETIMEDOUT : -EILSEQ;
1052
1053 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001054 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001056 host->mrq->cmd->error = err;
1057 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001058 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059 end_trans = 1;
1060 }
1061 }
1062 if (status & CARD_ERR) {
1063 dev_dbg(mmc_dev(host->mmc),
1064 "Ignoring card err CMD%d\n", host->cmd->opcode);
1065 if (host->cmd)
1066 end_cmd = 1;
1067 if (host->data)
1068 end_trans = 1;
1069 }
1070 }
1071
1072 OMAP_HSMMC_WRITE(host->base, STAT, status);
1073
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001074 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001075 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001076 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001077 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001078}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001079
Adrian Hunterb4175772010-05-26 14:42:06 -07001080/*
1081 * MMC controller IRQ handler
1082 */
1083static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1084{
1085 struct omap_hsmmc_host *host = dev_id;
1086 int status;
1087
1088 status = OMAP_HSMMC_READ(host->base, STAT);
1089 do {
1090 omap_hsmmc_do_irq(host, status);
1091 /* Flush posted write */
1092 status = OMAP_HSMMC_READ(host->base, STAT);
1093 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001094
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095 return IRQ_HANDLED;
1096}
1097
Denis Karpov70a33412009-09-22 16:44:59 -07001098static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001099{
1100 unsigned long i;
1101
1102 OMAP_HSMMC_WRITE(host->base, HCTL,
1103 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1104 for (i = 0; i < loops_per_jiffy; i++) {
1105 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1106 break;
1107 cpu_relax();
1108 }
1109}
1110
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001111/*
David Brownelleb250822009-02-17 14:49:01 -08001112 * Switch MMC interface voltage ... only relevant for MMC1.
1113 *
1114 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1115 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1116 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 */
Denis Karpov70a33412009-09-22 16:44:59 -07001118static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119{
1120 u32 reg_val = 0;
1121 int ret;
1122
1123 /* Disable the clocks */
1124 clk_disable(host->fclk);
1125 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001126 if (host->got_dbclk)
1127 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128
1129 /* Turn the power off */
1130 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001131
1132 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001133 if (!ret)
1134 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1135 vdd);
1136 clk_enable(host->iclk);
1137 clk_enable(host->fclk);
1138 if (host->got_dbclk)
1139 clk_enable(host->dbclk);
1140
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141 if (ret != 0)
1142 goto err;
1143
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144 OMAP_HSMMC_WRITE(host->base, HCTL,
1145 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1146 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001147
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001148 /*
1149 * If a MMC dual voltage card is detected, the set_ios fn calls
1150 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001151 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152 *
David Brownelleb250822009-02-17 14:49:01 -08001153 * Cope with a bit of slop in the range ... per data sheets:
1154 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1155 * but recommended values are 1.71V to 1.89V
1156 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1157 * but recommended values are 2.7V to 3.3V
1158 *
1159 * Board setup code shouldn't permit anything very out-of-range.
1160 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1161 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162 */
David Brownelleb250822009-02-17 14:49:01 -08001163 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001164 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001165 else
1166 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
1168 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001169 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170
1171 return 0;
1172err:
1173 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1174 return ret;
1175}
1176
Adrian Hunterb62f6222009-09-22 16:45:01 -07001177/* Protect the card while the cover is open */
1178static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1179{
1180 if (!mmc_slot(host).get_cover_state)
1181 return;
1182
1183 host->reqs_blocked = 0;
1184 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1185 if (host->protect_card) {
1186 printk(KERN_INFO "%s: cover is closed, "
1187 "card is now accessible\n",
1188 mmc_hostname(host->mmc));
1189 host->protect_card = 0;
1190 }
1191 } else {
1192 if (!host->protect_card) {
1193 printk(KERN_INFO "%s: cover is open, "
1194 "card is now inaccessible\n",
1195 mmc_hostname(host->mmc));
1196 host->protect_card = 1;
1197 }
1198 }
1199}
1200
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201/*
1202 * Work Item to notify the core about card insertion/removal
1203 */
Denis Karpov70a33412009-09-22 16:44:59 -07001204static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205{
Denis Karpov70a33412009-09-22 16:44:59 -07001206 struct omap_hsmmc_host *host =
1207 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001208 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001209 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001210
Adrian Huntera6b22402009-09-22 16:44:45 -07001211 if (host->suspended)
1212 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001213
1214 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001215
Denis Karpov191d1f12009-09-22 16:44:55 -07001216 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001217 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001218 else {
1219 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001220 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001221 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001222
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001223 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001225 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001226 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001227}
1228
1229/*
1230 * ISR for handling card insertion and removal
1231 */
Denis Karpov70a33412009-09-22 16:44:59 -07001232static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001233{
Denis Karpov70a33412009-09-22 16:44:59 -07001234 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235
Adrian Huntera6b22402009-09-22 16:44:45 -07001236 if (host->suspended)
1237 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238 schedule_work(&host->mmc_carddetect_work);
1239
1240 return IRQ_HANDLED;
1241}
1242
Denis Karpov70a33412009-09-22 16:44:59 -07001243static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001244 struct mmc_data *data)
1245{
1246 int sync_dev;
1247
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001248 if (data->flags & MMC_DATA_WRITE)
1249 sync_dev = host->dma_line_tx;
1250 else
1251 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001252 return sync_dev;
1253}
1254
Denis Karpov70a33412009-09-22 16:44:59 -07001255static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001256 struct mmc_data *data,
1257 struct scatterlist *sgl)
1258{
1259 int blksz, nblk, dma_ch;
1260
1261 dma_ch = host->dma_ch;
1262 if (data->flags & MMC_DATA_WRITE) {
1263 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1264 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1265 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1266 sg_dma_address(sgl), 0, 0);
1267 } else {
1268 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001269 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001270 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1271 sg_dma_address(sgl), 0, 0);
1272 }
1273
1274 blksz = host->data->blksz;
1275 nblk = sg_dma_len(sgl) / blksz;
1276
1277 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1278 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001279 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001280 !(data->flags & MMC_DATA_WRITE));
1281
1282 omap_start_dma(dma_ch);
1283}
1284
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001285/*
1286 * DMA call back function
1287 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001288static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289{
Adrian Hunterb4175772010-05-26 14:42:06 -07001290 struct omap_hsmmc_host *host = cb_data;
1291 struct mmc_data *data = host->mrq->data;
1292 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293
Venkatraman Sf3584e52010-08-10 18:01:54 -07001294 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1295 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1296 ch_status);
1297 return;
1298 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299
Adrian Hunterb4175772010-05-26 14:42:06 -07001300 spin_lock(&host->irq_lock);
1301 if (host->dma_ch < 0) {
1302 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001304 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001306 host->dma_sg_idx++;
1307 if (host->dma_sg_idx < host->dma_len) {
1308 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001309 omap_hsmmc_config_dma_params(host, data,
1310 data->sg + host->dma_sg_idx);
1311 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001312 return;
1313 }
1314
Adrian Hunterb4175772010-05-26 14:42:06 -07001315 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
1316 omap_hsmmc_get_dma_dir(host, data));
1317
1318 req_in_progress = host->req_in_progress;
1319 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001320 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001321 spin_unlock(&host->irq_lock);
1322
1323 omap_free_dma(dma_ch);
1324
1325 /* If DMA has finished after TC, complete the request */
1326 if (!req_in_progress) {
1327 struct mmc_request *mrq = host->mrq;
1328
1329 host->mrq = NULL;
1330 mmc_request_done(host->mmc, mrq);
1331 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001332}
1333
1334/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335 * Routine to configure and start DMA for the MMC card
1336 */
Denis Karpov70a33412009-09-22 16:44:59 -07001337static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1338 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339{
Adrian Hunterb4175772010-05-26 14:42:06 -07001340 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341 struct mmc_data *data = req->data;
1342
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001343 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001344 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001345 struct scatterlist *sgl;
1346
1347 sgl = data->sg + i;
1348 if (sgl->length % data->blksz)
1349 return -EINVAL;
1350 }
1351 if ((data->blksz % 4) != 0)
1352 /* REVISIT: The MMC buffer increments only when MSB is written.
1353 * Return error for blksz which is non multiple of four.
1354 */
1355 return -EINVAL;
1356
Adrian Hunterb4175772010-05-26 14:42:06 -07001357 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358
Denis Karpov70a33412009-09-22 16:44:59 -07001359 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1360 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001362 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363 "%s: omap_request_dma() failed with %d\n",
1364 mmc_hostname(host->mmc), ret);
1365 return ret;
1366 }
1367
1368 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001369 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001371 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372
Denis Karpov70a33412009-09-22 16:44:59 -07001373 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001374
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375 return 0;
1376}
1377
Denis Karpov70a33412009-09-22 16:44:59 -07001378static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001379 unsigned int timeout_ns,
1380 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381{
1382 unsigned int timeout, cycle_ns;
1383 uint32_t reg, clkd, dto = 0;
1384
1385 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1386 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1387 if (clkd == 0)
1388 clkd = 1;
1389
1390 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001391 timeout = timeout_ns / cycle_ns;
1392 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001393 if (timeout) {
1394 while ((timeout & 0x80000000) == 0) {
1395 dto += 1;
1396 timeout <<= 1;
1397 }
1398 dto = 31 - dto;
1399 timeout <<= 1;
1400 if (timeout && dto)
1401 dto += 1;
1402 if (dto >= 13)
1403 dto -= 13;
1404 else
1405 dto = 0;
1406 if (dto > 14)
1407 dto = 14;
1408 }
1409
1410 reg &= ~DTO_MASK;
1411 reg |= dto << DTO_SHIFT;
1412 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1413}
1414
1415/*
1416 * Configure block length for MMC/SD cards and initiate the transfer.
1417 */
1418static int
Denis Karpov70a33412009-09-22 16:44:59 -07001419omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420{
1421 int ret;
1422 host->data = req->data;
1423
1424 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001425 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001426 /*
1427 * Set an arbitrary 100ms data timeout for commands with
1428 * busy signal.
1429 */
1430 if (req->cmd->flags & MMC_RSP_BUSY)
1431 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001432 return 0;
1433 }
1434
1435 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1436 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001437 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001439 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001440 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441 if (ret != 0) {
1442 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1443 return ret;
1444 }
1445 }
1446 return 0;
1447}
1448
1449/*
1450 * Request function. for read/write operation
1451 */
Denis Karpov70a33412009-09-22 16:44:59 -07001452static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001453{
Denis Karpov70a33412009-09-22 16:44:59 -07001454 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001455 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001456
Adrian Hunterb4175772010-05-26 14:42:06 -07001457 BUG_ON(host->req_in_progress);
1458 BUG_ON(host->dma_ch != -1);
1459 if (host->protect_card) {
1460 if (host->reqs_blocked < 3) {
1461 /*
1462 * Ensure the controller is left in a consistent
1463 * state by resetting the command and data state
1464 * machines.
1465 */
1466 omap_hsmmc_reset_controller_fsm(host, SRD);
1467 omap_hsmmc_reset_controller_fsm(host, SRC);
1468 host->reqs_blocked += 1;
1469 }
1470 req->cmd->error = -EBADF;
1471 if (req->data)
1472 req->data->error = -EBADF;
1473 req->cmd->retries = 0;
1474 mmc_request_done(mmc, req);
1475 return;
1476 } else if (host->reqs_blocked)
1477 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001478 WARN_ON(host->mrq != NULL);
1479 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001480 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001481 if (err) {
1482 req->cmd->error = err;
1483 if (req->data)
1484 req->data->error = err;
1485 host->mrq = NULL;
1486 mmc_request_done(mmc, req);
1487 return;
1488 }
1489
Denis Karpov70a33412009-09-22 16:44:59 -07001490 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491}
1492
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001494static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001495{
Denis Karpov70a33412009-09-22 16:44:59 -07001496 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001497 u16 dsor = 0;
1498 unsigned long regval;
1499 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001500 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001501 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001502
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001503 mmc_host_enable(host->mmc);
1504
Adrian Huntera3621462009-09-22 16:44:42 -07001505 if (ios->power_mode != host->power_mode) {
1506 switch (ios->power_mode) {
1507 case MMC_POWER_OFF:
1508 mmc_slot(host).set_power(host->dev, host->slot_id,
1509 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001510 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001511 break;
1512 case MMC_POWER_UP:
1513 mmc_slot(host).set_power(host->dev, host->slot_id,
1514 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001515 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001516 break;
1517 case MMC_POWER_ON:
1518 do_send_init_stream = 1;
1519 break;
1520 }
1521 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001522 }
1523
Denis Karpovdd498ef2009-09-22 16:44:49 -07001524 /* FIXME: set registers based only on changes to ios */
1525
Jarkko Lavinen73153012008-11-21 16:49:54 +02001526 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001528 case MMC_BUS_WIDTH_8:
1529 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1530 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001532 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533 OMAP_HSMMC_WRITE(host->base, HCTL,
1534 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1535 break;
1536 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001537 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538 OMAP_HSMMC_WRITE(host->base, HCTL,
1539 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1540 break;
1541 }
1542
1543 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001544 /* Only MMC1 can interface at 3V without some flavor
1545 * of external transceiver; but they all handle 1.8V.
1546 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001547 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1548 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1549 /*
1550 * The mmc_select_voltage fn of the core does
1551 * not seem to set the power_mode to
1552 * MMC_POWER_UP upon recalculating the voltage.
1553 * vdd 1.8v.
1554 */
Denis Karpov70a33412009-09-22 16:44:59 -07001555 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1556 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557 "Switch operation failed\n");
1558 }
1559 }
1560
1561 if (ios->clock) {
1562 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1563 if (dsor < 1)
1564 dsor = 1;
1565
1566 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1567 dsor++;
1568
1569 if (dsor > 250)
1570 dsor = 250;
1571 }
Denis Karpov70a33412009-09-22 16:44:59 -07001572 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001573 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1574 regval = regval & ~(CLKD_MASK);
1575 regval = regval | (dsor << 6) | (DTO << 16);
1576 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1577 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1578 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1579
1580 /* Wait till the ICS bit is set */
1581 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001582 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001583 && time_before(jiffies, timeout))
1584 msleep(1);
1585
1586 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1587 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1588
Adrian Huntera3621462009-09-22 16:44:42 -07001589 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001590 send_init_stream(host);
1591
Denis Karpovabb28e72009-09-22 16:44:44 -07001592 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001594 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1595 else
1596 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001597
Denis Karpovdd498ef2009-09-22 16:44:49 -07001598 if (host->power_mode == MMC_POWER_OFF)
1599 mmc_host_disable(host->mmc);
1600 else
1601 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602}
1603
1604static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1605{
Denis Karpov70a33412009-09-22 16:44:59 -07001606 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607
Denis Karpov191d1f12009-09-22 16:44:55 -07001608 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001610 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001611}
1612
1613static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1614{
Denis Karpov70a33412009-09-22 16:44:59 -07001615 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001616
Denis Karpov191d1f12009-09-22 16:44:55 -07001617 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001618 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001619 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001620}
1621
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001622static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1623{
1624 struct omap_hsmmc_host *host = mmc_priv(mmc);
1625
1626 if (mmc_slot(host).init_card)
1627 mmc_slot(host).init_card(card);
1628}
1629
Denis Karpov70a33412009-09-22 16:44:59 -07001630static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001631{
1632 u32 hctl, capa, value;
1633
1634 /* Only MMC1 supports 3.0V */
1635 if (host->id == OMAP_MMC1_DEVID) {
1636 hctl = SDVS30;
1637 capa = VS30 | VS18;
1638 } else {
1639 hctl = SDVS18;
1640 capa = VS18;
1641 }
1642
1643 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1644 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1645
1646 value = OMAP_HSMMC_READ(host->base, CAPA);
1647 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1648
1649 /* Set the controller to AUTO IDLE mode */
1650 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1651 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1652
1653 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001654 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001655}
1656
Denis Karpovdd498ef2009-09-22 16:44:49 -07001657/*
1658 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001659 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1660 * ^___________| | |
1661 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001662 *
1663 * ENABLED: mmc host is fully functional
1664 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001665 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1666 * REGSLEEP: fclk is off, voltage regulator is asleep
1667 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001668 *
1669 * Transition handlers return the timeout for the next state transition
1670 * or negative error.
1671 */
1672
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001673enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001674
1675/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001676static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001677{
Denis Karpov70a33412009-09-22 16:44:59 -07001678 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001679 clk_disable(host->fclk);
1680 host->dpm_state = DISABLED;
1681
1682 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1683
1684 if (host->power_mode == MMC_POWER_OFF)
1685 return 0;
1686
Adrian Hunter4380eea2010-02-15 10:03:34 -08001687 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001688}
1689
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001690/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001691static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001692{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001693 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001694
1695 if (!mmc_try_claim_host(host->mmc))
1696 return 0;
1697
1698 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001699 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001700 if (mmc_card_can_sleep(host->mmc)) {
1701 err = mmc_card_sleep(host->mmc);
1702 if (err < 0) {
1703 clk_disable(host->fclk);
1704 mmc_release_host(host->mmc);
1705 return err;
1706 }
1707 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001708 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001709 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001710 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001711 if (mmc_slot(host).set_sleep)
1712 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1713 new_state == CARDSLEEP);
1714 /* FIXME: turn off bus power and perhaps interrupts too */
1715 clk_disable(host->fclk);
1716 host->dpm_state = new_state;
1717
1718 mmc_release_host(host->mmc);
1719
1720 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1721 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001722
Adrian Hunter1df58db2010-02-15 10:03:34 -08001723 if (mmc_slot(host).no_off)
1724 return 0;
1725
Denis Karpovdd498ef2009-09-22 16:44:49 -07001726 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1727 mmc_slot(host).card_detect ||
1728 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001729 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea2010-02-15 10:03:34 -08001730 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001731
1732 return 0;
1733}
1734
1735/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001736static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001737{
1738 if (!mmc_try_claim_host(host->mmc))
1739 return 0;
1740
Adrian Hunter1df58db2010-02-15 10:03:34 -08001741 if (mmc_slot(host).no_off)
1742 return 0;
1743
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001744 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1745 mmc_slot(host).card_detect ||
1746 (mmc_slot(host).get_cover_state &&
1747 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1748 mmc_release_host(host->mmc);
1749 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001750 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001751
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001752 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1753 host->vdd = 0;
1754 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001755
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001756 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1757 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001758
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001759 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001760
1761 mmc_release_host(host->mmc);
1762
1763 return 0;
1764}
1765
1766/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001767static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001768{
1769 int err;
1770
1771 err = clk_enable(host->fclk);
1772 if (err < 0)
1773 return err;
1774
Denis Karpov70a33412009-09-22 16:44:59 -07001775 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001776 host->dpm_state = ENABLED;
1777
1778 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1779
1780 return 0;
1781}
1782
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001783/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001784static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001785{
1786 if (!mmc_try_claim_host(host->mmc))
1787 return 0;
1788
1789 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001790 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001791 if (mmc_slot(host).set_sleep)
1792 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1793 host->vdd, host->dpm_state == CARDSLEEP);
1794 if (mmc_card_can_sleep(host->mmc))
1795 mmc_card_awake(host->mmc);
1796
1797 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1798 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1799
1800 host->dpm_state = ENABLED;
1801
1802 mmc_release_host(host->mmc);
1803
1804 return 0;
1805}
1806
Denis Karpovdd498ef2009-09-22 16:44:49 -07001807/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001808static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001809{
1810 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001811
Denis Karpov70a33412009-09-22 16:44:59 -07001812 omap_hsmmc_context_restore(host);
1813 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001814 mmc_power_restore_host(host->mmc);
1815
1816 host->dpm_state = ENABLED;
1817
1818 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1819
1820 return 0;
1821}
1822
1823/*
1824 * Bring MMC host to ENABLED from any other PM state.
1825 */
Denis Karpov70a33412009-09-22 16:44:59 -07001826static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001827{
Denis Karpov70a33412009-09-22 16:44:59 -07001828 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001829
1830 switch (host->dpm_state) {
1831 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001832 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001833 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001834 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001835 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001836 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001837 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838 default:
1839 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1840 return -EINVAL;
1841 }
1842}
1843
1844/*
1845 * Bring MMC host in PM state (one level deeper).
1846 */
Denis Karpov70a33412009-09-22 16:44:59 -07001847static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001848{
Denis Karpov70a33412009-09-22 16:44:59 -07001849 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001850
1851 switch (host->dpm_state) {
1852 case ENABLED: {
1853 int delay;
1854
Denis Karpov70a33412009-09-22 16:44:59 -07001855 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001856 if (lazy || delay < 0)
1857 return delay;
1858 return 0;
1859 }
1860 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001861 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001862 case CARDSLEEP:
1863 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001864 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001865 default:
1866 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1867 return -EINVAL;
1868 }
1869}
1870
Denis Karpov70a33412009-09-22 16:44:59 -07001871static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001872{
Denis Karpov70a33412009-09-22 16:44:59 -07001873 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001874 int err;
1875
1876 err = clk_enable(host->fclk);
1877 if (err)
1878 return err;
1879 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001880 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001881 return 0;
1882}
1883
Denis Karpov70a33412009-09-22 16:44:59 -07001884static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001885{
Denis Karpov70a33412009-09-22 16:44:59 -07001886 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001887
Denis Karpov70a33412009-09-22 16:44:59 -07001888 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001889 clk_disable(host->fclk);
1890 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1891 return 0;
1892}
1893
Denis Karpov70a33412009-09-22 16:44:59 -07001894static const struct mmc_host_ops omap_hsmmc_ops = {
1895 .enable = omap_hsmmc_enable_fclk,
1896 .disable = omap_hsmmc_disable_fclk,
1897 .request = omap_hsmmc_request,
1898 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001899 .get_cd = omap_hsmmc_get_cd,
1900 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001901 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001902 /* NYET -- enable_sdio_irq */
1903};
1904
Denis Karpov70a33412009-09-22 16:44:59 -07001905static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1906 .enable = omap_hsmmc_enable,
1907 .disable = omap_hsmmc_disable,
1908 .request = omap_hsmmc_request,
1909 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001910 .get_cd = omap_hsmmc_get_cd,
1911 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001912 .init_card = omap_hsmmc_init_card,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 /* NYET -- enable_sdio_irq */
1914};
1915
Denis Karpovd900f712009-09-22 16:44:38 -07001916#ifdef CONFIG_DEBUG_FS
1917
Denis Karpov70a33412009-09-22 16:44:59 -07001918static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001919{
1920 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001921 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001922 int context_loss = 0;
1923
Denis Karpov70a33412009-09-22 16:44:59 -07001924 if (host->pdata->get_context_loss_count)
1925 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001926
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001927 seq_printf(s, "mmc%d:\n"
1928 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001929 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001930 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001931 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001932 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001933 mmc->index, mmc->enabled ? 1 : 0,
1934 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001935 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001936
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001937 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001938 seq_printf(s, "host suspended, can't read registers\n");
1939 return 0;
1940 }
1941
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001942 if (clk_enable(host->fclk) != 0) {
1943 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001944 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001945 }
Denis Karpovd900f712009-09-22 16:44:38 -07001946
1947 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1948 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1949 seq_printf(s, "CON:\t\t0x%08x\n",
1950 OMAP_HSMMC_READ(host->base, CON));
1951 seq_printf(s, "HCTL:\t\t0x%08x\n",
1952 OMAP_HSMMC_READ(host->base, HCTL));
1953 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1954 OMAP_HSMMC_READ(host->base, SYSCTL));
1955 seq_printf(s, "IE:\t\t0x%08x\n",
1956 OMAP_HSMMC_READ(host->base, IE));
1957 seq_printf(s, "ISE:\t\t0x%08x\n",
1958 OMAP_HSMMC_READ(host->base, ISE));
1959 seq_printf(s, "CAPA:\t\t0x%08x\n",
1960 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001961
1962 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001963
Denis Karpovd900f712009-09-22 16:44:38 -07001964 return 0;
1965}
1966
Denis Karpov70a33412009-09-22 16:44:59 -07001967static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001968{
Denis Karpov70a33412009-09-22 16:44:59 -07001969 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001970}
1971
1972static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001973 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001974 .read = seq_read,
1975 .llseek = seq_lseek,
1976 .release = single_release,
1977};
1978
Denis Karpov70a33412009-09-22 16:44:59 -07001979static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001980{
1981 if (mmc->debugfs_root)
1982 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1983 mmc, &mmc_regs_fops);
1984}
1985
1986#else
1987
Denis Karpov70a33412009-09-22 16:44:59 -07001988static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001989{
1990}
1991
1992#endif
1993
Denis Karpov70a33412009-09-22 16:44:59 -07001994static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995{
1996 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1997 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001998 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002000 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002001
2002 if (pdata == NULL) {
2003 dev_err(&pdev->dev, "Platform Data is missing\n");
2004 return -ENXIO;
2005 }
2006
2007 if (pdata->nr_slots == 0) {
2008 dev_err(&pdev->dev, "No Slots\n");
2009 return -ENXIO;
2010 }
2011
2012 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2013 irq = platform_get_irq(pdev, 0);
2014 if (res == NULL || irq < 0)
2015 return -ENXIO;
2016
2017 res = request_mem_region(res->start, res->end - res->start + 1,
2018 pdev->name);
2019 if (res == NULL)
2020 return -EBUSY;
2021
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002022 ret = omap_hsmmc_gpio_init(pdata);
2023 if (ret)
2024 goto err;
2025
Denis Karpov70a33412009-09-22 16:44:59 -07002026 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027 if (!mmc) {
2028 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002029 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030 }
2031
2032 host = mmc_priv(mmc);
2033 host->mmc = mmc;
2034 host->pdata = pdata;
2035 host->dev = &pdev->dev;
2036 host->use_dma = 1;
2037 host->dev->dma_mask = &pdata->dma_mask;
2038 host->dma_ch = -1;
2039 host->irq = irq;
2040 host->id = pdev->id;
2041 host->slot_id = 0;
2042 host->mapbase = res->start;
2043 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002044 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045
2046 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002047 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048
Denis Karpov191d1f12009-09-22 16:44:55 -07002049 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002050 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002051 else
Denis Karpov70a33412009-09-22 16:44:59 -07002052 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002053
Adrian Huntere0eb2422010-02-15 10:03:34 -08002054 /*
2055 * If regulator_disable can only put vcc_aux to sleep then there is
2056 * no off state.
2057 */
2058 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2059 mmc_slot(host).no_off = 1;
2060
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002061 mmc->f_min = 400000;
2062 mmc->f_max = 52000000;
2063
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002064 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002065
Russell King6f7607c2009-01-28 10:22:50 +00002066 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 if (IS_ERR(host->iclk)) {
2068 ret = PTR_ERR(host->iclk);
2069 host->iclk = NULL;
2070 goto err1;
2071 }
Russell King6f7607c2009-01-28 10:22:50 +00002072 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073 if (IS_ERR(host->fclk)) {
2074 ret = PTR_ERR(host->fclk);
2075 host->fclk = NULL;
2076 clk_put(host->iclk);
2077 goto err1;
2078 }
2079
Denis Karpov70a33412009-09-22 16:44:59 -07002080 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002081
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002082 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002083 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2084 /* we start off in DISABLED state */
2085 host->dpm_state = DISABLED;
2086
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002087 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088 clk_put(host->iclk);
2089 clk_put(host->fclk);
2090 goto err1;
2091 }
2092
2093 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002094 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002095 clk_put(host->iclk);
2096 clk_put(host->fclk);
2097 goto err1;
2098 }
2099
Adrian Hunter2bec0892009-09-22 16:45:02 -07002100 if (cpu_is_omap2430()) {
2101 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2102 /*
2103 * MMC can still work without debounce clock.
2104 */
2105 if (IS_ERR(host->dbclk))
2106 dev_warn(mmc_dev(host->mmc),
2107 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002109 host->got_dbclk = 1;
2110
2111 if (host->got_dbclk)
2112 if (clk_enable(host->dbclk) != 0)
2113 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2114 " clk failed\n");
2115 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002117 /* Since we do only SG emulation, we can have as many segs
2118 * as we want. */
2119 mmc->max_phys_segs = 1024;
2120 mmc->max_hw_segs = 1024;
2121
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002122 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2123 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2124 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2125 mmc->max_seg_size = mmc->max_req_size;
2126
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002127 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002128 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002130 mmc->caps |= mmc_slot(host).caps;
2131 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002132 mmc->caps |= MMC_CAP_4_BIT_DATA;
2133
Denis Karpov191d1f12009-09-22 16:44:55 -07002134 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002135 mmc->caps |= MMC_CAP_NONREMOVABLE;
2136
Denis Karpov70a33412009-09-22 16:44:59 -07002137 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002139 /* Select DMA lines */
2140 switch (host->id) {
2141 case OMAP_MMC1_DEVID:
2142 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2143 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2144 break;
2145 case OMAP_MMC2_DEVID:
2146 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2147 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2148 break;
2149 case OMAP_MMC3_DEVID:
2150 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2151 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2152 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002153 case OMAP_MMC4_DEVID:
2154 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2155 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2156 break;
2157 case OMAP_MMC5_DEVID:
2158 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2159 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2160 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002161 default:
2162 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2163 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002164 }
2165
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002166 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002167 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002168 mmc_hostname(mmc), host);
2169 if (ret) {
2170 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2171 goto err_irq;
2172 }
2173
2174 if (pdata->init != NULL) {
2175 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002176 dev_dbg(mmc_dev(host->mmc),
2177 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178 goto err_irq_cd_init;
2179 }
2180 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002181
Adrian Hunterb702b102010-02-15 10:03:35 -08002182 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002183 ret = omap_hsmmc_reg_get(host);
2184 if (ret)
2185 goto err_reg;
2186 host->use_reg = 1;
2187 }
2188
David Brownellb583f262009-05-28 14:04:03 -07002189 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002190
2191 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002192 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002194 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002195 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2196 | IRQF_DISABLED,
2197 mmc_hostname(mmc), host);
2198 if (ret) {
2199 dev_dbg(mmc_dev(host->mmc),
2200 "Unable to grab MMC CD IRQ\n");
2201 goto err_irq_cd;
2202 }
2203 }
2204
Adrian Hunterb4175772010-05-26 14:42:06 -07002205 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002207 mmc_host_lazy_disable(host->mmc);
2208
Adrian Hunterb62f6222009-09-22 16:45:01 -07002209 omap_hsmmc_protect_card(host);
2210
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211 mmc_add_host(mmc);
2212
Denis Karpov191d1f12009-09-22 16:44:55 -07002213 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2215 if (ret < 0)
2216 goto err_slot_name;
2217 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002218 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219 ret = device_create_file(&mmc->class_dev,
2220 &dev_attr_cover_switch);
2221 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002222 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002223 }
2224
Denis Karpov70a33412009-09-22 16:44:59 -07002225 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002226
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 return 0;
2228
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229err_slot_name:
2230 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002232err_irq_cd:
2233 if (host->use_reg)
2234 omap_hsmmc_reg_put(host);
2235err_reg:
2236 if (host->pdata->cleanup)
2237 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002238err_irq_cd_init:
2239 free_irq(host->irq, host);
2240err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002241 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242 clk_disable(host->iclk);
2243 clk_put(host->fclk);
2244 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002245 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002246 clk_disable(host->dbclk);
2247 clk_put(host->dbclk);
2248 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249err1:
2250 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002251 platform_set_drvdata(pdev, NULL);
2252 mmc_free_host(mmc);
2253err_alloc:
2254 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002255err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256 release_mem_region(res->start, res->end - res->start + 1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002257 return ret;
2258}
2259
Denis Karpov70a33412009-09-22 16:44:59 -07002260static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002261{
Denis Karpov70a33412009-09-22 16:44:59 -07002262 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263 struct resource *res;
2264
2265 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002266 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002268 if (host->use_reg)
2269 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002270 if (host->pdata->cleanup)
2271 host->pdata->cleanup(&pdev->dev);
2272 free_irq(host->irq, host);
2273 if (mmc_slot(host).card_detect_irq)
2274 free_irq(mmc_slot(host).card_detect_irq, host);
2275 flush_scheduled_work();
2276
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002277 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002278 clk_disable(host->iclk);
2279 clk_put(host->fclk);
2280 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002281 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002282 clk_disable(host->dbclk);
2283 clk_put(host->dbclk);
2284 }
2285
2286 mmc_free_host(host->mmc);
2287 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002288 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289 }
2290
2291 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2292 if (res)
2293 release_mem_region(res->start, res->end - res->start + 1);
2294 platform_set_drvdata(pdev, NULL);
2295
2296 return 0;
2297}
2298
2299#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002300static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002301{
2302 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002303 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002304 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002305
2306 if (host && host->suspended)
2307 return 0;
2308
2309 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002310 host->suspended = 1;
2311 if (host->pdata->suspend) {
2312 ret = host->pdata->suspend(&pdev->dev,
2313 host->slot_id);
2314 if (ret) {
2315 dev_dbg(mmc_dev(host->mmc),
2316 "Unable to handle MMC board"
2317 " level suspend\n");
2318 host->suspended = 0;
2319 return ret;
2320 }
2321 }
2322 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002323 ret = mmc_suspend_host(host->mmc);
Ethan Due7cb7562010-09-09 16:37:49 -07002324 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002325 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002326 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002327 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002328 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002329 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002330 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002331 if (host->got_dbclk)
2332 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002333 } else {
2334 host->suspended = 0;
2335 if (host->pdata->resume) {
2336 ret = host->pdata->resume(&pdev->dev,
2337 host->slot_id);
2338 if (ret)
2339 dev_dbg(mmc_dev(host->mmc),
2340 "Unmask interrupt failed\n");
2341 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002342 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002343 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002344
2345 }
2346 return ret;
2347}
2348
2349/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002350static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002351{
2352 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002353 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002354 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002355
2356 if (host && !host->suspended)
2357 return 0;
2358
2359 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002360 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002361 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002362 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002363
Denis Karpov11dd62a2009-09-22 16:44:43 -07002364 if (mmc_host_enable(host->mmc) != 0) {
2365 clk_disable(host->iclk);
2366 goto clk_en_err;
2367 }
2368
Adrian Hunter2bec0892009-09-22 16:45:02 -07002369 if (host->got_dbclk)
2370 clk_enable(host->dbclk);
2371
Denis Karpov70a33412009-09-22 16:44:59 -07002372 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002373
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002374 if (host->pdata->resume) {
2375 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2376 if (ret)
2377 dev_dbg(mmc_dev(host->mmc),
2378 "Unmask interrupt failed\n");
2379 }
2380
Adrian Hunterb62f6222009-09-22 16:45:01 -07002381 omap_hsmmc_protect_card(host);
2382
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002383 /* Notify the core to resume the host */
2384 ret = mmc_resume_host(host->mmc);
2385 if (ret == 0)
2386 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002387
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002388 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002389 }
2390
2391 return ret;
2392
2393clk_en_err:
2394 dev_dbg(mmc_dev(host->mmc),
2395 "Failed to enable MMC clocks during resume\n");
2396 return ret;
2397}
2398
2399#else
Denis Karpov70a33412009-09-22 16:44:59 -07002400#define omap_hsmmc_suspend NULL
2401#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002402#endif
2403
Kevin Hilmana791daa2010-05-26 14:42:07 -07002404static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002405 .suspend = omap_hsmmc_suspend,
2406 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002407};
2408
2409static struct platform_driver omap_hsmmc_driver = {
2410 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002411 .driver = {
2412 .name = DRIVER_NAME,
2413 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002414 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002415 },
2416};
2417
Denis Karpov70a33412009-09-22 16:44:59 -07002418static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002419{
2420 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002421 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002422}
2423
Denis Karpov70a33412009-09-22 16:44:59 -07002424static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002425{
2426 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002427 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002428}
2429
Denis Karpov70a33412009-09-22 16:44:59 -07002430module_init(omap_hsmmc_init);
2431module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002432
2433MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2434MODULE_LICENSE("GPL");
2435MODULE_ALIAS("platform:" DRIVER_NAME);
2436MODULE_AUTHOR("Texas Instruments Inc");