blob: 4a8776f8afdd690048c69de91e755d35d2c884a5 [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
985 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
986 (i++ < limit))
987 cpu_relax();
988
989 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
990 dev_err(mmc_dev(host->mmc),
991 "Timeout waiting on controller reset in %s\n",
992 __func__);
993}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100994
Adrian Hunterb4175772010-05-26 14:42:06 -0700995static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700998 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999
Adrian Hunterb4175772010-05-26 14:42:06 -07001000 if (!host->req_in_progress) {
1001 do {
1002 OMAP_HSMMC_WRITE(host->base, STAT, status);
1003 /* Flush posted write */
1004 status = OMAP_HSMMC_READ(host->base, STAT);
1005 } while (status & INT_EN_MASK);
1006 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001007 }
1008
1009 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1011
1012 if (status & ERR) {
1013#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001014 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001015#endif
1016 if ((status & CMD_TIMEOUT) ||
1017 (status & CMD_CRC)) {
1018 if (host->cmd) {
1019 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001020 omap_hsmmc_reset_controller_fsm(host,
1021 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 host->cmd->error = -ETIMEDOUT;
1023 } else {
1024 host->cmd->error = -EILSEQ;
1025 }
1026 end_cmd = 1;
1027 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001028 if (host->data || host->response_busy) {
1029 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001030 omap_hsmmc_dma_cleanup(host,
1031 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001032 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001033 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001034 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001035 }
1036 if ((status & DATA_TIMEOUT) ||
1037 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001038 if (host->data || host->response_busy) {
1039 int err = (status & DATA_TIMEOUT) ?
1040 -ETIMEDOUT : -EILSEQ;
1041
1042 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001043 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001044 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001045 host->mrq->cmd->error = err;
1046 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001047 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048 end_trans = 1;
1049 }
1050 }
1051 if (status & CARD_ERR) {
1052 dev_dbg(mmc_dev(host->mmc),
1053 "Ignoring card err CMD%d\n", host->cmd->opcode);
1054 if (host->cmd)
1055 end_cmd = 1;
1056 if (host->data)
1057 end_trans = 1;
1058 }
1059 }
1060
1061 OMAP_HSMMC_WRITE(host->base, STAT, status);
1062
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001063 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001064 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001065 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001066 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001067}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068
Adrian Hunterb4175772010-05-26 14:42:06 -07001069/*
1070 * MMC controller IRQ handler
1071 */
1072static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1073{
1074 struct omap_hsmmc_host *host = dev_id;
1075 int status;
1076
1077 status = OMAP_HSMMC_READ(host->base, STAT);
1078 do {
1079 omap_hsmmc_do_irq(host, status);
1080 /* Flush posted write */
1081 status = OMAP_HSMMC_READ(host->base, STAT);
1082 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001083
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001084 return IRQ_HANDLED;
1085}
1086
Denis Karpov70a33412009-09-22 16:44:59 -07001087static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001088{
1089 unsigned long i;
1090
1091 OMAP_HSMMC_WRITE(host->base, HCTL,
1092 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1093 for (i = 0; i < loops_per_jiffy; i++) {
1094 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1095 break;
1096 cpu_relax();
1097 }
1098}
1099
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100/*
David Brownelleb250822009-02-17 14:49:01 -08001101 * Switch MMC interface voltage ... only relevant for MMC1.
1102 *
1103 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1104 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1105 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001106 */
Denis Karpov70a33412009-09-22 16:44:59 -07001107static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001108{
1109 u32 reg_val = 0;
1110 int ret;
1111
1112 /* Disable the clocks */
1113 clk_disable(host->fclk);
1114 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001115 if (host->got_dbclk)
1116 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117
1118 /* Turn the power off */
1119 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001120
1121 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001122 if (!ret)
1123 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1124 vdd);
1125 clk_enable(host->iclk);
1126 clk_enable(host->fclk);
1127 if (host->got_dbclk)
1128 clk_enable(host->dbclk);
1129
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001130 if (ret != 0)
1131 goto err;
1132
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133 OMAP_HSMMC_WRITE(host->base, HCTL,
1134 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1135 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001136
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137 /*
1138 * If a MMC dual voltage card is detected, the set_ios fn calls
1139 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001140 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141 *
David Brownelleb250822009-02-17 14:49:01 -08001142 * Cope with a bit of slop in the range ... per data sheets:
1143 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1144 * but recommended values are 1.71V to 1.89V
1145 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1146 * but recommended values are 2.7V to 3.3V
1147 *
1148 * Board setup code shouldn't permit anything very out-of-range.
1149 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1150 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151 */
David Brownelleb250822009-02-17 14:49:01 -08001152 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001153 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001154 else
1155 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156
1157 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001158 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159
1160 return 0;
1161err:
1162 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1163 return ret;
1164}
1165
Adrian Hunterb62f6222009-09-22 16:45:01 -07001166/* Protect the card while the cover is open */
1167static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1168{
1169 if (!mmc_slot(host).get_cover_state)
1170 return;
1171
1172 host->reqs_blocked = 0;
1173 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1174 if (host->protect_card) {
1175 printk(KERN_INFO "%s: cover is closed, "
1176 "card is now accessible\n",
1177 mmc_hostname(host->mmc));
1178 host->protect_card = 0;
1179 }
1180 } else {
1181 if (!host->protect_card) {
1182 printk(KERN_INFO "%s: cover is open, "
1183 "card is now inaccessible\n",
1184 mmc_hostname(host->mmc));
1185 host->protect_card = 1;
1186 }
1187 }
1188}
1189
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001190/*
1191 * Work Item to notify the core about card insertion/removal
1192 */
Denis Karpov70a33412009-09-22 16:44:59 -07001193static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001194{
Denis Karpov70a33412009-09-22 16:44:59 -07001195 struct omap_hsmmc_host *host =
1196 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001197 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001198 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001199
Adrian Huntera6b22402009-09-22 16:44:45 -07001200 if (host->suspended)
1201 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001202
1203 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001204
Denis Karpov191d1f12009-09-22 16:44:55 -07001205 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001206 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001207 else {
1208 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001209 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001210 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001211
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001212 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001213 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001214 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001215 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216}
1217
1218/*
1219 * ISR for handling card insertion and removal
1220 */
Denis Karpov70a33412009-09-22 16:44:59 -07001221static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001222{
Denis Karpov70a33412009-09-22 16:44:59 -07001223 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224
Adrian Huntera6b22402009-09-22 16:44:45 -07001225 if (host->suspended)
1226 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001227 schedule_work(&host->mmc_carddetect_work);
1228
1229 return IRQ_HANDLED;
1230}
1231
Denis Karpov70a33412009-09-22 16:44:59 -07001232static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001233 struct mmc_data *data)
1234{
1235 int sync_dev;
1236
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001237 if (data->flags & MMC_DATA_WRITE)
1238 sync_dev = host->dma_line_tx;
1239 else
1240 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001241 return sync_dev;
1242}
1243
Denis Karpov70a33412009-09-22 16:44:59 -07001244static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001245 struct mmc_data *data,
1246 struct scatterlist *sgl)
1247{
1248 int blksz, nblk, dma_ch;
1249
1250 dma_ch = host->dma_ch;
1251 if (data->flags & MMC_DATA_WRITE) {
1252 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1253 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1254 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1255 sg_dma_address(sgl), 0, 0);
1256 } else {
1257 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001258 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001259 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1260 sg_dma_address(sgl), 0, 0);
1261 }
1262
1263 blksz = host->data->blksz;
1264 nblk = sg_dma_len(sgl) / blksz;
1265
1266 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1267 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001268 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001269 !(data->flags & MMC_DATA_WRITE));
1270
1271 omap_start_dma(dma_ch);
1272}
1273
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274/*
1275 * DMA call back function
1276 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001277static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278{
Adrian Hunterb4175772010-05-26 14:42:06 -07001279 struct omap_hsmmc_host *host = cb_data;
1280 struct mmc_data *data = host->mrq->data;
1281 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282
Venkatraman Sf3584e52010-08-10 18:01:54 -07001283 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1284 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1285 ch_status);
1286 return;
1287 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001288
Adrian Hunterb4175772010-05-26 14:42:06 -07001289 spin_lock(&host->irq_lock);
1290 if (host->dma_ch < 0) {
1291 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001293 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001295 host->dma_sg_idx++;
1296 if (host->dma_sg_idx < host->dma_len) {
1297 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001298 omap_hsmmc_config_dma_params(host, data,
1299 data->sg + host->dma_sg_idx);
1300 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001301 return;
1302 }
1303
Adrian Hunterb4175772010-05-26 14:42:06 -07001304 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
1305 omap_hsmmc_get_dma_dir(host, data));
1306
1307 req_in_progress = host->req_in_progress;
1308 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001309 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001310 spin_unlock(&host->irq_lock);
1311
1312 omap_free_dma(dma_ch);
1313
1314 /* If DMA has finished after TC, complete the request */
1315 if (!req_in_progress) {
1316 struct mmc_request *mrq = host->mrq;
1317
1318 host->mrq = NULL;
1319 mmc_request_done(host->mmc, mrq);
1320 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001321}
1322
1323/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324 * Routine to configure and start DMA for the MMC card
1325 */
Denis Karpov70a33412009-09-22 16:44:59 -07001326static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1327 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001328{
Adrian Hunterb4175772010-05-26 14:42:06 -07001329 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001330 struct mmc_data *data = req->data;
1331
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001332 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001333 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001334 struct scatterlist *sgl;
1335
1336 sgl = data->sg + i;
1337 if (sgl->length % data->blksz)
1338 return -EINVAL;
1339 }
1340 if ((data->blksz % 4) != 0)
1341 /* REVISIT: The MMC buffer increments only when MSB is written.
1342 * Return error for blksz which is non multiple of four.
1343 */
1344 return -EINVAL;
1345
Adrian Hunterb4175772010-05-26 14:42:06 -07001346 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347
Denis Karpov70a33412009-09-22 16:44:59 -07001348 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1349 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001350 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001351 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352 "%s: omap_request_dma() failed with %d\n",
1353 mmc_hostname(host->mmc), ret);
1354 return ret;
1355 }
1356
1357 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001358 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001359 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001360 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361
Denis Karpov70a33412009-09-22 16:44:59 -07001362 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001364 return 0;
1365}
1366
Denis Karpov70a33412009-09-22 16:44:59 -07001367static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001368 unsigned int timeout_ns,
1369 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370{
1371 unsigned int timeout, cycle_ns;
1372 uint32_t reg, clkd, dto = 0;
1373
1374 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1375 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1376 if (clkd == 0)
1377 clkd = 1;
1378
1379 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001380 timeout = timeout_ns / cycle_ns;
1381 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001382 if (timeout) {
1383 while ((timeout & 0x80000000) == 0) {
1384 dto += 1;
1385 timeout <<= 1;
1386 }
1387 dto = 31 - dto;
1388 timeout <<= 1;
1389 if (timeout && dto)
1390 dto += 1;
1391 if (dto >= 13)
1392 dto -= 13;
1393 else
1394 dto = 0;
1395 if (dto > 14)
1396 dto = 14;
1397 }
1398
1399 reg &= ~DTO_MASK;
1400 reg |= dto << DTO_SHIFT;
1401 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1402}
1403
1404/*
1405 * Configure block length for MMC/SD cards and initiate the transfer.
1406 */
1407static int
Denis Karpov70a33412009-09-22 16:44:59 -07001408omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409{
1410 int ret;
1411 host->data = req->data;
1412
1413 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001415 /*
1416 * Set an arbitrary 100ms data timeout for commands with
1417 * busy signal.
1418 */
1419 if (req->cmd->flags & MMC_RSP_BUSY)
1420 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421 return 0;
1422 }
1423
1424 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1425 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001426 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001427
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001428 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001429 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001430 if (ret != 0) {
1431 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1432 return ret;
1433 }
1434 }
1435 return 0;
1436}
1437
1438/*
1439 * Request function. for read/write operation
1440 */
Denis Karpov70a33412009-09-22 16:44:59 -07001441static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442{
Denis Karpov70a33412009-09-22 16:44:59 -07001443 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001444 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445
Adrian Hunterb4175772010-05-26 14:42:06 -07001446 BUG_ON(host->req_in_progress);
1447 BUG_ON(host->dma_ch != -1);
1448 if (host->protect_card) {
1449 if (host->reqs_blocked < 3) {
1450 /*
1451 * Ensure the controller is left in a consistent
1452 * state by resetting the command and data state
1453 * machines.
1454 */
1455 omap_hsmmc_reset_controller_fsm(host, SRD);
1456 omap_hsmmc_reset_controller_fsm(host, SRC);
1457 host->reqs_blocked += 1;
1458 }
1459 req->cmd->error = -EBADF;
1460 if (req->data)
1461 req->data->error = -EBADF;
1462 req->cmd->retries = 0;
1463 mmc_request_done(mmc, req);
1464 return;
1465 } else if (host->reqs_blocked)
1466 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467 WARN_ON(host->mrq != NULL);
1468 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001469 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001470 if (err) {
1471 req->cmd->error = err;
1472 if (req->data)
1473 req->data->error = err;
1474 host->mrq = NULL;
1475 mmc_request_done(mmc, req);
1476 return;
1477 }
1478
Denis Karpov70a33412009-09-22 16:44:59 -07001479 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001480}
1481
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001483static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001484{
Denis Karpov70a33412009-09-22 16:44:59 -07001485 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486 u16 dsor = 0;
1487 unsigned long regval;
1488 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001489 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001490 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001492 mmc_host_enable(host->mmc);
1493
Adrian Huntera3621462009-09-22 16:44:42 -07001494 if (ios->power_mode != host->power_mode) {
1495 switch (ios->power_mode) {
1496 case MMC_POWER_OFF:
1497 mmc_slot(host).set_power(host->dev, host->slot_id,
1498 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001499 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001500 break;
1501 case MMC_POWER_UP:
1502 mmc_slot(host).set_power(host->dev, host->slot_id,
1503 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001504 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001505 break;
1506 case MMC_POWER_ON:
1507 do_send_init_stream = 1;
1508 break;
1509 }
1510 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511 }
1512
Denis Karpovdd498ef2009-09-22 16:44:49 -07001513 /* FIXME: set registers based only on changes to ios */
1514
Jarkko Lavinen73153012008-11-21 16:49:54 +02001515 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001517 case MMC_BUS_WIDTH_8:
1518 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1519 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001521 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001522 OMAP_HSMMC_WRITE(host->base, HCTL,
1523 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1524 break;
1525 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001526 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527 OMAP_HSMMC_WRITE(host->base, HCTL,
1528 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1529 break;
1530 }
1531
1532 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001533 /* Only MMC1 can interface at 3V without some flavor
1534 * of external transceiver; but they all handle 1.8V.
1535 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001536 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1537 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1538 /*
1539 * The mmc_select_voltage fn of the core does
1540 * not seem to set the power_mode to
1541 * MMC_POWER_UP upon recalculating the voltage.
1542 * vdd 1.8v.
1543 */
Denis Karpov70a33412009-09-22 16:44:59 -07001544 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1545 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001546 "Switch operation failed\n");
1547 }
1548 }
1549
1550 if (ios->clock) {
1551 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1552 if (dsor < 1)
1553 dsor = 1;
1554
1555 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1556 dsor++;
1557
1558 if (dsor > 250)
1559 dsor = 250;
1560 }
Denis Karpov70a33412009-09-22 16:44:59 -07001561 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1563 regval = regval & ~(CLKD_MASK);
1564 regval = regval | (dsor << 6) | (DTO << 16);
1565 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1566 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1567 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1568
1569 /* Wait till the ICS bit is set */
1570 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001571 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001572 && time_before(jiffies, timeout))
1573 msleep(1);
1574
1575 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1576 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1577
Adrian Huntera3621462009-09-22 16:44:42 -07001578 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001579 send_init_stream(host);
1580
Denis Karpovabb28e72009-09-22 16:44:44 -07001581 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001583 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1584 else
1585 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001586
Denis Karpovdd498ef2009-09-22 16:44:49 -07001587 if (host->power_mode == MMC_POWER_OFF)
1588 mmc_host_disable(host->mmc);
1589 else
1590 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001591}
1592
1593static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1594{
Denis Karpov70a33412009-09-22 16:44:59 -07001595 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596
Denis Karpov191d1f12009-09-22 16:44:55 -07001597 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001599 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600}
1601
1602static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1603{
Denis Karpov70a33412009-09-22 16:44:59 -07001604 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605
Denis Karpov191d1f12009-09-22 16:44:55 -07001606 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001608 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609}
1610
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001611static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1612{
1613 struct omap_hsmmc_host *host = mmc_priv(mmc);
1614
1615 if (mmc_slot(host).init_card)
1616 mmc_slot(host).init_card(card);
1617}
1618
Denis Karpov70a33412009-09-22 16:44:59 -07001619static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001620{
1621 u32 hctl, capa, value;
1622
1623 /* Only MMC1 supports 3.0V */
1624 if (host->id == OMAP_MMC1_DEVID) {
1625 hctl = SDVS30;
1626 capa = VS30 | VS18;
1627 } else {
1628 hctl = SDVS18;
1629 capa = VS18;
1630 }
1631
1632 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1633 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1634
1635 value = OMAP_HSMMC_READ(host->base, CAPA);
1636 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1637
1638 /* Set the controller to AUTO IDLE mode */
1639 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1640 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1641
1642 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001643 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001644}
1645
Denis Karpovdd498ef2009-09-22 16:44:49 -07001646/*
1647 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001648 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1649 * ^___________| | |
1650 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001651 *
1652 * ENABLED: mmc host is fully functional
1653 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001654 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1655 * REGSLEEP: fclk is off, voltage regulator is asleep
1656 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001657 *
1658 * Transition handlers return the timeout for the next state transition
1659 * or negative error.
1660 */
1661
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001662enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001663
1664/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001665static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001666{
Denis Karpov70a33412009-09-22 16:44:59 -07001667 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001668 clk_disable(host->fclk);
1669 host->dpm_state = DISABLED;
1670
1671 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1672
1673 if (host->power_mode == MMC_POWER_OFF)
1674 return 0;
1675
Adrian Hunter4380eea2010-02-15 10:03:34 -08001676 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001677}
1678
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001679/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001680static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001681{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001682 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001683
1684 if (!mmc_try_claim_host(host->mmc))
1685 return 0;
1686
1687 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001688 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001689 if (mmc_card_can_sleep(host->mmc)) {
1690 err = mmc_card_sleep(host->mmc);
1691 if (err < 0) {
1692 clk_disable(host->fclk);
1693 mmc_release_host(host->mmc);
1694 return err;
1695 }
1696 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001697 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001698 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001699 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001700 if (mmc_slot(host).set_sleep)
1701 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1702 new_state == CARDSLEEP);
1703 /* FIXME: turn off bus power and perhaps interrupts too */
1704 clk_disable(host->fclk);
1705 host->dpm_state = new_state;
1706
1707 mmc_release_host(host->mmc);
1708
1709 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1710 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001711
Adrian Hunter1df58db2010-02-15 10:03:34 -08001712 if (mmc_slot(host).no_off)
1713 return 0;
1714
Denis Karpovdd498ef2009-09-22 16:44:49 -07001715 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1716 mmc_slot(host).card_detect ||
1717 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001718 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea2010-02-15 10:03:34 -08001719 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001720
1721 return 0;
1722}
1723
1724/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001725static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001726{
1727 if (!mmc_try_claim_host(host->mmc))
1728 return 0;
1729
Adrian Hunter1df58db2010-02-15 10:03:34 -08001730 if (mmc_slot(host).no_off)
1731 return 0;
1732
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001733 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1734 mmc_slot(host).card_detect ||
1735 (mmc_slot(host).get_cover_state &&
1736 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1737 mmc_release_host(host->mmc);
1738 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001739 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001740
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001741 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1742 host->vdd = 0;
1743 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001744
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001745 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1746 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001747
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001748 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001749
1750 mmc_release_host(host->mmc);
1751
1752 return 0;
1753}
1754
1755/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001756static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001757{
1758 int err;
1759
1760 err = clk_enable(host->fclk);
1761 if (err < 0)
1762 return err;
1763
Denis Karpov70a33412009-09-22 16:44:59 -07001764 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001765 host->dpm_state = ENABLED;
1766
1767 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1768
1769 return 0;
1770}
1771
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001772/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001773static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001774{
1775 if (!mmc_try_claim_host(host->mmc))
1776 return 0;
1777
1778 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001779 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001780 if (mmc_slot(host).set_sleep)
1781 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1782 host->vdd, host->dpm_state == CARDSLEEP);
1783 if (mmc_card_can_sleep(host->mmc))
1784 mmc_card_awake(host->mmc);
1785
1786 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1787 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1788
1789 host->dpm_state = ENABLED;
1790
1791 mmc_release_host(host->mmc);
1792
1793 return 0;
1794}
1795
Denis Karpovdd498ef2009-09-22 16:44:49 -07001796/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001797static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001798{
1799 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001800
Denis Karpov70a33412009-09-22 16:44:59 -07001801 omap_hsmmc_context_restore(host);
1802 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001803 mmc_power_restore_host(host->mmc);
1804
1805 host->dpm_state = ENABLED;
1806
1807 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1808
1809 return 0;
1810}
1811
1812/*
1813 * Bring MMC host to ENABLED from any other PM state.
1814 */
Denis Karpov70a33412009-09-22 16:44:59 -07001815static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001816{
Denis Karpov70a33412009-09-22 16:44:59 -07001817 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818
1819 switch (host->dpm_state) {
1820 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001821 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001822 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001823 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001824 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001825 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001826 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001827 default:
1828 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1829 return -EINVAL;
1830 }
1831}
1832
1833/*
1834 * Bring MMC host in PM state (one level deeper).
1835 */
Denis Karpov70a33412009-09-22 16:44:59 -07001836static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001837{
Denis Karpov70a33412009-09-22 16:44:59 -07001838 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001839
1840 switch (host->dpm_state) {
1841 case ENABLED: {
1842 int delay;
1843
Denis Karpov70a33412009-09-22 16:44:59 -07001844 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001845 if (lazy || delay < 0)
1846 return delay;
1847 return 0;
1848 }
1849 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001850 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001851 case CARDSLEEP:
1852 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001853 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001854 default:
1855 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1856 return -EINVAL;
1857 }
1858}
1859
Denis Karpov70a33412009-09-22 16:44:59 -07001860static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001861{
Denis Karpov70a33412009-09-22 16:44:59 -07001862 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001863 int err;
1864
1865 err = clk_enable(host->fclk);
1866 if (err)
1867 return err;
1868 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001869 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001870 return 0;
1871}
1872
Denis Karpov70a33412009-09-22 16:44:59 -07001873static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001874{
Denis Karpov70a33412009-09-22 16:44:59 -07001875 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001876
Denis Karpov70a33412009-09-22 16:44:59 -07001877 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001878 clk_disable(host->fclk);
1879 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1880 return 0;
1881}
1882
Denis Karpov70a33412009-09-22 16:44:59 -07001883static const struct mmc_host_ops omap_hsmmc_ops = {
1884 .enable = omap_hsmmc_enable_fclk,
1885 .disable = omap_hsmmc_disable_fclk,
1886 .request = omap_hsmmc_request,
1887 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001888 .get_cd = omap_hsmmc_get_cd,
1889 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001890 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001891 /* NYET -- enable_sdio_irq */
1892};
1893
Denis Karpov70a33412009-09-22 16:44:59 -07001894static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1895 .enable = omap_hsmmc_enable,
1896 .disable = omap_hsmmc_disable,
1897 .request = omap_hsmmc_request,
1898 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001899 .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,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001902 /* NYET -- enable_sdio_irq */
1903};
1904
Denis Karpovd900f712009-09-22 16:44:38 -07001905#ifdef CONFIG_DEBUG_FS
1906
Denis Karpov70a33412009-09-22 16:44:59 -07001907static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001908{
1909 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001910 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001911 int context_loss = 0;
1912
Denis Karpov70a33412009-09-22 16:44:59 -07001913 if (host->pdata->get_context_loss_count)
1914 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001915
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001916 seq_printf(s, "mmc%d:\n"
1917 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001918 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001919 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001920 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001921 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001922 mmc->index, mmc->enabled ? 1 : 0,
1923 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001924 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001925
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001926 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001927 seq_printf(s, "host suspended, can't read registers\n");
1928 return 0;
1929 }
1930
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001931 if (clk_enable(host->fclk) != 0) {
1932 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001933 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001934 }
Denis Karpovd900f712009-09-22 16:44:38 -07001935
1936 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1937 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1938 seq_printf(s, "CON:\t\t0x%08x\n",
1939 OMAP_HSMMC_READ(host->base, CON));
1940 seq_printf(s, "HCTL:\t\t0x%08x\n",
1941 OMAP_HSMMC_READ(host->base, HCTL));
1942 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1943 OMAP_HSMMC_READ(host->base, SYSCTL));
1944 seq_printf(s, "IE:\t\t0x%08x\n",
1945 OMAP_HSMMC_READ(host->base, IE));
1946 seq_printf(s, "ISE:\t\t0x%08x\n",
1947 OMAP_HSMMC_READ(host->base, ISE));
1948 seq_printf(s, "CAPA:\t\t0x%08x\n",
1949 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001950
1951 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001952
Denis Karpovd900f712009-09-22 16:44:38 -07001953 return 0;
1954}
1955
Denis Karpov70a33412009-09-22 16:44:59 -07001956static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001957{
Denis Karpov70a33412009-09-22 16:44:59 -07001958 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001959}
1960
1961static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001962 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001963 .read = seq_read,
1964 .llseek = seq_lseek,
1965 .release = single_release,
1966};
1967
Denis Karpov70a33412009-09-22 16:44:59 -07001968static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001969{
1970 if (mmc->debugfs_root)
1971 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1972 mmc, &mmc_regs_fops);
1973}
1974
1975#else
1976
Denis Karpov70a33412009-09-22 16:44:59 -07001977static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001978{
1979}
1980
1981#endif
1982
Denis Karpov70a33412009-09-22 16:44:59 -07001983static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984{
1985 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1986 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001987 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001988 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001989 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990
1991 if (pdata == NULL) {
1992 dev_err(&pdev->dev, "Platform Data is missing\n");
1993 return -ENXIO;
1994 }
1995
1996 if (pdata->nr_slots == 0) {
1997 dev_err(&pdev->dev, "No Slots\n");
1998 return -ENXIO;
1999 }
2000
2001 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2002 irq = platform_get_irq(pdev, 0);
2003 if (res == NULL || irq < 0)
2004 return -ENXIO;
2005
2006 res = request_mem_region(res->start, res->end - res->start + 1,
2007 pdev->name);
2008 if (res == NULL)
2009 return -EBUSY;
2010
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002011 ret = omap_hsmmc_gpio_init(pdata);
2012 if (ret)
2013 goto err;
2014
Denis Karpov70a33412009-09-22 16:44:59 -07002015 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002016 if (!mmc) {
2017 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002018 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019 }
2020
2021 host = mmc_priv(mmc);
2022 host->mmc = mmc;
2023 host->pdata = pdata;
2024 host->dev = &pdev->dev;
2025 host->use_dma = 1;
2026 host->dev->dma_mask = &pdata->dma_mask;
2027 host->dma_ch = -1;
2028 host->irq = irq;
2029 host->id = pdev->id;
2030 host->slot_id = 0;
2031 host->mapbase = res->start;
2032 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002033 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002034
2035 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002036 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037
Denis Karpov191d1f12009-09-22 16:44:55 -07002038 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002039 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002040 else
Denis Karpov70a33412009-09-22 16:44:59 -07002041 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002042
Adrian Huntere0eb2422010-02-15 10:03:34 -08002043 /*
2044 * If regulator_disable can only put vcc_aux to sleep then there is
2045 * no off state.
2046 */
2047 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2048 mmc_slot(host).no_off = 1;
2049
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002050 mmc->f_min = 400000;
2051 mmc->f_max = 52000000;
2052
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002053 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054
Russell King6f7607c2009-01-28 10:22:50 +00002055 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002056 if (IS_ERR(host->iclk)) {
2057 ret = PTR_ERR(host->iclk);
2058 host->iclk = NULL;
2059 goto err1;
2060 }
Russell King6f7607c2009-01-28 10:22:50 +00002061 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062 if (IS_ERR(host->fclk)) {
2063 ret = PTR_ERR(host->fclk);
2064 host->fclk = NULL;
2065 clk_put(host->iclk);
2066 goto err1;
2067 }
2068
Denis Karpov70a33412009-09-22 16:44:59 -07002069 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002070
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002071 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002072 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2073 /* we start off in DISABLED state */
2074 host->dpm_state = DISABLED;
2075
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002076 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077 clk_put(host->iclk);
2078 clk_put(host->fclk);
2079 goto err1;
2080 }
2081
2082 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002083 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 clk_put(host->iclk);
2085 clk_put(host->fclk);
2086 goto err1;
2087 }
2088
Adrian Hunter2bec0892009-09-22 16:45:02 -07002089 if (cpu_is_omap2430()) {
2090 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2091 /*
2092 * MMC can still work without debounce clock.
2093 */
2094 if (IS_ERR(host->dbclk))
2095 dev_warn(mmc_dev(host->mmc),
2096 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002098 host->got_dbclk = 1;
2099
2100 if (host->got_dbclk)
2101 if (clk_enable(host->dbclk) != 0)
2102 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2103 " clk failed\n");
2104 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002106 /* Since we do only SG emulation, we can have as many segs
2107 * as we want. */
2108 mmc->max_phys_segs = 1024;
2109 mmc->max_hw_segs = 1024;
2110
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2112 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2113 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2114 mmc->max_seg_size = mmc->max_req_size;
2115
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002116 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002117 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002118
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002119 switch (mmc_slot(host).wires) {
2120 case 8:
Jarkko Lavinen73153012008-11-21 16:49:54 +02002121 mmc->caps |= MMC_CAP_8_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002122 /* Fall through */
2123 case 4:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002124 mmc->caps |= MMC_CAP_4_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002125 break;
2126 case 1:
2127 /* Nothing to crib here */
2128 case 0:
2129 /* Assuming nothing was given by board, Core use's 1-Bit */
2130 break;
2131 default:
2132 /* Completely unexpected.. Core goes with 1-Bit Width */
2133 dev_crit(mmc_dev(host->mmc), "Invalid width %d\n used!"
2134 "using 1 instead\n", mmc_slot(host).wires);
2135 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002136
Denis Karpov191d1f12009-09-22 16:44:55 -07002137 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002138 mmc->caps |= MMC_CAP_NONREMOVABLE;
2139
Denis Karpov70a33412009-09-22 16:44:59 -07002140 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002142 /* Select DMA lines */
2143 switch (host->id) {
2144 case OMAP_MMC1_DEVID:
2145 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2146 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2147 break;
2148 case OMAP_MMC2_DEVID:
2149 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2150 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2151 break;
2152 case OMAP_MMC3_DEVID:
2153 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2154 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2155 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002156 case OMAP_MMC4_DEVID:
2157 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2158 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2159 break;
2160 case OMAP_MMC5_DEVID:
2161 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2162 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2163 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002164 default:
2165 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2166 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002167 }
2168
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002170 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171 mmc_hostname(mmc), host);
2172 if (ret) {
2173 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2174 goto err_irq;
2175 }
2176
2177 if (pdata->init != NULL) {
2178 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002179 dev_dbg(mmc_dev(host->mmc),
2180 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002181 goto err_irq_cd_init;
2182 }
2183 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002184
Adrian Hunterb702b102010-02-15 10:03:35 -08002185 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002186 ret = omap_hsmmc_reg_get(host);
2187 if (ret)
2188 goto err_reg;
2189 host->use_reg = 1;
2190 }
2191
David Brownellb583f262009-05-28 14:04:03 -07002192 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193
2194 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002195 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002196 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002197 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2199 | IRQF_DISABLED,
2200 mmc_hostname(mmc), host);
2201 if (ret) {
2202 dev_dbg(mmc_dev(host->mmc),
2203 "Unable to grab MMC CD IRQ\n");
2204 goto err_irq_cd;
2205 }
2206 }
2207
Adrian Hunterb4175772010-05-26 14:42:06 -07002208 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002209
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002210 mmc_host_lazy_disable(host->mmc);
2211
Adrian Hunterb62f6222009-09-22 16:45:01 -07002212 omap_hsmmc_protect_card(host);
2213
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214 mmc_add_host(mmc);
2215
Denis Karpov191d1f12009-09-22 16:44:55 -07002216 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2218 if (ret < 0)
2219 goto err_slot_name;
2220 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002221 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222 ret = device_create_file(&mmc->class_dev,
2223 &dev_attr_cover_switch);
2224 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002225 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002226 }
2227
Denis Karpov70a33412009-09-22 16:44:59 -07002228 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002229
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230 return 0;
2231
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232err_slot_name:
2233 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002235err_irq_cd:
2236 if (host->use_reg)
2237 omap_hsmmc_reg_put(host);
2238err_reg:
2239 if (host->pdata->cleanup)
2240 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002241err_irq_cd_init:
2242 free_irq(host->irq, host);
2243err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002244 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245 clk_disable(host->iclk);
2246 clk_put(host->fclk);
2247 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002248 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 clk_disable(host->dbclk);
2250 clk_put(host->dbclk);
2251 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252err1:
2253 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002254 platform_set_drvdata(pdev, NULL);
2255 mmc_free_host(mmc);
2256err_alloc:
2257 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002258err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002259 release_mem_region(res->start, res->end - res->start + 1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002260 return ret;
2261}
2262
Denis Karpov70a33412009-09-22 16:44:59 -07002263static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002264{
Denis Karpov70a33412009-09-22 16:44:59 -07002265 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266 struct resource *res;
2267
2268 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002269 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002270 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002271 if (host->use_reg)
2272 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273 if (host->pdata->cleanup)
2274 host->pdata->cleanup(&pdev->dev);
2275 free_irq(host->irq, host);
2276 if (mmc_slot(host).card_detect_irq)
2277 free_irq(mmc_slot(host).card_detect_irq, host);
2278 flush_scheduled_work();
2279
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002280 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002281 clk_disable(host->iclk);
2282 clk_put(host->fclk);
2283 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002284 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002285 clk_disable(host->dbclk);
2286 clk_put(host->dbclk);
2287 }
2288
2289 mmc_free_host(host->mmc);
2290 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002291 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002292 }
2293
2294 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2295 if (res)
2296 release_mem_region(res->start, res->end - res->start + 1);
2297 platform_set_drvdata(pdev, NULL);
2298
2299 return 0;
2300}
2301
2302#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002303static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002304{
2305 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002306 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002307 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Kevin Hilmana791daa2010-05-26 14:42:07 -07002308 pm_message_t state = PMSG_SUSPEND; /* unused by MMC core */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002309
2310 if (host && host->suspended)
2311 return 0;
2312
2313 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002314 host->suspended = 1;
2315 if (host->pdata->suspend) {
2316 ret = host->pdata->suspend(&pdev->dev,
2317 host->slot_id);
2318 if (ret) {
2319 dev_dbg(mmc_dev(host->mmc),
2320 "Unable to handle MMC board"
2321 " level suspend\n");
2322 host->suspended = 0;
2323 return ret;
2324 }
2325 }
2326 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002327 mmc_host_enable(host->mmc);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002328 ret = mmc_suspend_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002329 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002330 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002331 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002332 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002333 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002334 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002335 if (host->got_dbclk)
2336 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002337 } else {
2338 host->suspended = 0;
2339 if (host->pdata->resume) {
2340 ret = host->pdata->resume(&pdev->dev,
2341 host->slot_id);
2342 if (ret)
2343 dev_dbg(mmc_dev(host->mmc),
2344 "Unmask interrupt failed\n");
2345 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002346 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002347 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002348
2349 }
2350 return ret;
2351}
2352
2353/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002354static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002355{
2356 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002357 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002358 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002359
2360 if (host && !host->suspended)
2361 return 0;
2362
2363 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002364 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002365 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002366 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002367
Denis Karpov11dd62a2009-09-22 16:44:43 -07002368 if (mmc_host_enable(host->mmc) != 0) {
2369 clk_disable(host->iclk);
2370 goto clk_en_err;
2371 }
2372
Adrian Hunter2bec0892009-09-22 16:45:02 -07002373 if (host->got_dbclk)
2374 clk_enable(host->dbclk);
2375
Denis Karpov70a33412009-09-22 16:44:59 -07002376 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002377
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002378 if (host->pdata->resume) {
2379 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2380 if (ret)
2381 dev_dbg(mmc_dev(host->mmc),
2382 "Unmask interrupt failed\n");
2383 }
2384
Adrian Hunterb62f6222009-09-22 16:45:01 -07002385 omap_hsmmc_protect_card(host);
2386
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002387 /* Notify the core to resume the host */
2388 ret = mmc_resume_host(host->mmc);
2389 if (ret == 0)
2390 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002391
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002392 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002393 }
2394
2395 return ret;
2396
2397clk_en_err:
2398 dev_dbg(mmc_dev(host->mmc),
2399 "Failed to enable MMC clocks during resume\n");
2400 return ret;
2401}
2402
2403#else
Denis Karpov70a33412009-09-22 16:44:59 -07002404#define omap_hsmmc_suspend NULL
2405#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002406#endif
2407
Kevin Hilmana791daa2010-05-26 14:42:07 -07002408static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002409 .suspend = omap_hsmmc_suspend,
2410 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002411};
2412
2413static struct platform_driver omap_hsmmc_driver = {
2414 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002415 .driver = {
2416 .name = DRIVER_NAME,
2417 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002418 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002419 },
2420};
2421
Denis Karpov70a33412009-09-22 16:44:59 -07002422static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002423{
2424 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002425 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002426}
2427
Denis Karpov70a33412009-09-22 16:44:59 -07002428static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002429{
2430 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002431 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002432}
2433
Denis Karpov70a33412009-09-22 16:44:59 -07002434module_init(omap_hsmmc_init);
2435module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002436
2437MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2438MODULE_LICENSE("GPL");
2439MODULE_ALIAS("platform:" DRIVER_NAME);
2440MODULE_AUTHOR("Texas Instruments Inc");