blob: b0c69104c863d2ef38aa643c7d53ad2c7c1d052a [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
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530121#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100122
Denis Karpovdd498ef2009-09-22 16:44:49 -0700123/* Timeouts for entering power saving states on inactivity, msec */
124#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700125#define OMAP_MMC_SLEEP_TIMEOUT 1000
126#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100128/*
129 * One controller can have multiple slots, like on some omap boards using
130 * omap.c controller driver. Luckily this is not currently done on any known
131 * omap_hsmmc.c device.
132 */
133#define mmc_slot(host) (host->pdata->slots[host->slot_id])
134
135/*
136 * MMC Host controller read/write API's
137 */
138#define OMAP_HSMMC_READ(base, reg) \
139 __raw_readl((base) + OMAP_HSMMC_##reg)
140
141#define OMAP_HSMMC_WRITE(base, reg, val) \
142 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
143
Per Forlin9782aff2011-07-01 18:55:23 +0200144struct omap_hsmmc_next {
145 unsigned int dma_len;
146 s32 cookie;
147};
148
Denis Karpov70a33412009-09-22 16:44:59 -0700149struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150 struct device *dev;
151 struct mmc_host *mmc;
152 struct mmc_request *mrq;
153 struct mmc_command *cmd;
154 struct mmc_data *data;
155 struct clk *fclk;
156 struct clk *iclk;
157 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800158 /*
159 * vcc == configured supply
160 * vcc_aux == optional
161 * - MMC1, supply for DAT4..DAT7
162 * - MMC2/MMC2, external level shifter voltage supply, for
163 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
164 */
165 struct regulator *vcc;
166 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100167 struct work_struct mmc_carddetect_work;
168 void __iomem *base;
169 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700170 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 unsigned int id;
172 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200173 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100174 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700175 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 u32 *buffer;
177 u32 bytesleft;
178 int suspended;
179 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100180 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000181 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100182 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700183 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200184 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700185 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700186 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700187 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700188 int protect_card;
189 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800190 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700191 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200192 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700193
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 struct omap_mmc_platform_data *pdata;
195};
196
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800197static int omap_hsmmc_card_detect(struct device *dev, int slot)
198{
199 struct omap_mmc_platform_data *mmc = dev->platform_data;
200
201 /* NOTE: assumes card detect signal is active-low */
202 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
203}
204
205static int omap_hsmmc_get_wp(struct device *dev, int slot)
206{
207 struct omap_mmc_platform_data *mmc = dev->platform_data;
208
209 /* NOTE: assumes write protect signal is active-high */
210 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
211}
212
213static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
214{
215 struct omap_mmc_platform_data *mmc = dev->platform_data;
216
217 /* NOTE: assumes card detect signal is active-low */
218 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
219}
220
221#ifdef CONFIG_PM
222
223static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
224{
225 struct omap_mmc_platform_data *mmc = dev->platform_data;
226
227 disable_irq(mmc->slots[0].card_detect_irq);
228 return 0;
229}
230
231static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
232{
233 struct omap_mmc_platform_data *mmc = dev->platform_data;
234
235 enable_irq(mmc->slots[0].card_detect_irq);
236 return 0;
237}
238
239#else
240
241#define omap_hsmmc_suspend_cdirq NULL
242#define omap_hsmmc_resume_cdirq NULL
243
244#endif
245
Adrian Hunterb702b102010-02-15 10:03:35 -0800246#ifdef CONFIG_REGULATOR
247
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800248static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
249 int vdd)
250{
251 struct omap_hsmmc_host *host =
252 platform_get_drvdata(to_platform_device(dev));
253 int ret;
254
255 if (mmc_slot(host).before_set_reg)
256 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
257
258 if (power_on)
Linus Walleij99fc5132010-09-29 01:08:27 -0400259 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400261 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800262
263 if (mmc_slot(host).after_set_reg)
264 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
265
266 return ret;
267}
268
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500269static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800270 int vdd)
271{
272 struct omap_hsmmc_host *host =
273 platform_get_drvdata(to_platform_device(dev));
274 int ret = 0;
275
276 /*
277 * If we don't see a Vcc regulator, assume it's a fixed
278 * voltage always-on regulator.
279 */
280 if (!host->vcc)
281 return 0;
282
283 if (mmc_slot(host).before_set_reg)
284 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
285
286 /*
287 * Assume Vcc regulator is used only to power the card ... OMAP
288 * VDDS is used to power the pins, optionally with a transceiver to
289 * support cards using voltages other than VDDS (1.8V nominal). When a
290 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
291 *
292 * In some cases this regulator won't support enable/disable;
293 * e.g. it's a fixed rail for a WLAN chip.
294 *
295 * In other cases vcc_aux switches interface power. Example, for
296 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
297 * chips/cards need an interface voltage rail too.
298 */
299 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400300 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 /* Enable interface voltage rail, if needed */
302 if (ret == 0 && host->vcc_aux) {
303 ret = regulator_enable(host->vcc_aux);
304 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400305 ret = mmc_regulator_set_ocr(host->mmc,
306 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800307 }
308 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400309 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800310 if (host->vcc_aux)
311 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400312 if (!ret) {
313 /* Then proceed to shut down the local regulator */
314 ret = mmc_regulator_set_ocr(host->mmc,
315 host->vcc, 0);
316 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800317 }
318
319 if (mmc_slot(host).after_set_reg)
320 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
321
322 return ret;
323}
324
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500325static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
326 int vdd)
327{
328 return 0;
329}
330
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
332 int vdd, int cardsleep)
333{
334 struct omap_hsmmc_host *host =
335 platform_get_drvdata(to_platform_device(dev));
336 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
337
338 return regulator_set_mode(host->vcc, mode);
339}
340
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500341static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342 int vdd, int cardsleep)
343{
344 struct omap_hsmmc_host *host =
345 platform_get_drvdata(to_platform_device(dev));
346 int err, mode;
347
348 /*
349 * If we don't see a Vcc regulator, assume it's a fixed
350 * voltage always-on regulator.
351 */
352 if (!host->vcc)
353 return 0;
354
355 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
356
357 if (!host->vcc_aux)
358 return regulator_set_mode(host->vcc, mode);
359
360 if (cardsleep) {
361 /* VCC can be turned off if card is asleep */
362 if (sleep)
Linus Walleij99fc5132010-09-29 01:08:27 -0400363 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400365 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366 } else
367 err = regulator_set_mode(host->vcc, mode);
368 if (err)
369 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800370
371 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
372 return regulator_set_mode(host->vcc_aux, mode);
373
374 if (sleep)
375 return regulator_disable(host->vcc_aux);
376 else
377 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800378}
379
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500380static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
381 int vdd, int cardsleep)
382{
383 return 0;
384}
385
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800386static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
387{
388 struct regulator *reg;
389 int ret = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700390 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800391
392 switch (host->id) {
393 case OMAP_MMC1_DEVID:
394 /* On-chip level shifting via PBIAS0/PBIAS1 */
395 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
396 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
397 break;
398 case OMAP_MMC2_DEVID:
399 case OMAP_MMC3_DEVID:
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500400 case OMAP_MMC5_DEVID:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800401 /* Off-chip level shifting, or none */
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500402 mmc_slot(host).set_power = omap_hsmmc_235_set_power;
403 mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800404 break;
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500405 case OMAP_MMC4_DEVID:
406 mmc_slot(host).set_power = omap_hsmmc_4_set_power;
407 mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800408 default:
409 pr_err("MMC%d configuration not supported!\n", host->id);
410 return -EINVAL;
411 }
412
413 reg = regulator_get(host->dev, "vmmc");
414 if (IS_ERR(reg)) {
415 dev_dbg(host->dev, "vmmc regulator missing\n");
416 /*
417 * HACK: until fixed.c regulator is usable,
418 * we don't require a main regulator
419 * for MMC2 or MMC3
420 */
421 if (host->id == OMAP_MMC1_DEVID) {
422 ret = PTR_ERR(reg);
423 goto err;
424 }
425 } else {
426 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700427 ocr_value = mmc_regulator_get_ocrmask(reg);
428 if (!mmc_slot(host).ocr_mask) {
429 mmc_slot(host).ocr_mask = ocr_value;
430 } else {
431 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
432 pr_err("MMC%d ocrmask %x is not supported\n",
433 host->id, mmc_slot(host).ocr_mask);
434 mmc_slot(host).ocr_mask = 0;
435 return -EINVAL;
436 }
437 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800438
439 /* Allow an aux regulator */
440 reg = regulator_get(host->dev, "vmmc_aux");
441 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
442
Balaji T Kb1c1df72011-05-30 19:55:34 +0530443 /* For eMMC do not power off when not in sleep state */
444 if (mmc_slot(host).no_regulator_off_init)
445 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800446 /*
447 * UGLY HACK: workaround regulator framework bugs.
448 * When the bootloader leaves a supply active, it's
449 * initialized with zero usecount ... and we can't
450 * disable it without first enabling it. Until the
451 * framework is fixed, we need a workaround like this
452 * (which is safe for MMC, but not in general).
453 */
454 if (regulator_is_enabled(host->vcc) > 0) {
455 regulator_enable(host->vcc);
456 regulator_disable(host->vcc);
457 }
458 if (host->vcc_aux) {
459 if (regulator_is_enabled(reg) > 0) {
460 regulator_enable(reg);
461 regulator_disable(reg);
462 }
463 }
464 }
465
466 return 0;
467
468err:
469 mmc_slot(host).set_power = NULL;
470 mmc_slot(host).set_sleep = NULL;
471 return ret;
472}
473
474static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
475{
476 regulator_put(host->vcc);
477 regulator_put(host->vcc_aux);
478 mmc_slot(host).set_power = NULL;
479 mmc_slot(host).set_sleep = NULL;
480}
481
Adrian Hunterb702b102010-02-15 10:03:35 -0800482static inline int omap_hsmmc_have_reg(void)
483{
484 return 1;
485}
486
487#else
488
489static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
490{
491 return -EINVAL;
492}
493
494static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
495{
496}
497
498static inline int omap_hsmmc_have_reg(void)
499{
500 return 0;
501}
502
503#endif
504
505static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
506{
507 int ret;
508
509 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800510 if (pdata->slots[0].cover)
511 pdata->slots[0].get_cover_state =
512 omap_hsmmc_get_cover_state;
513 else
514 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
515 pdata->slots[0].card_detect_irq =
516 gpio_to_irq(pdata->slots[0].switch_pin);
517 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
518 if (ret)
519 return ret;
520 ret = gpio_direction_input(pdata->slots[0].switch_pin);
521 if (ret)
522 goto err_free_sp;
523 } else
524 pdata->slots[0].switch_pin = -EINVAL;
525
526 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
527 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
528 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
529 if (ret)
530 goto err_free_cd;
531 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
532 if (ret)
533 goto err_free_wp;
534 } else
535 pdata->slots[0].gpio_wp = -EINVAL;
536
537 return 0;
538
539err_free_wp:
540 gpio_free(pdata->slots[0].gpio_wp);
541err_free_cd:
542 if (gpio_is_valid(pdata->slots[0].switch_pin))
543err_free_sp:
544 gpio_free(pdata->slots[0].switch_pin);
545 return ret;
546}
547
548static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
549{
550 if (gpio_is_valid(pdata->slots[0].gpio_wp))
551 gpio_free(pdata->slots[0].gpio_wp);
552 if (gpio_is_valid(pdata->slots[0].switch_pin))
553 gpio_free(pdata->slots[0].switch_pin);
554}
555
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100556/*
557 * Stop clock to the card
558 */
Denis Karpov70a33412009-09-22 16:44:59 -0700559static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100560{
561 OMAP_HSMMC_WRITE(host->base, SYSCTL,
562 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
563 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
564 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
565}
566
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700567static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
568 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700569{
570 unsigned int irq_mask;
571
572 if (host->use_dma)
573 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
574 else
575 irq_mask = INT_EN_MASK;
576
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700577 /* Disable timeout for erases */
578 if (cmd->opcode == MMC_ERASE)
579 irq_mask &= ~DTO_ENABLE;
580
Adrian Hunterb4175772010-05-26 14:42:06 -0700581 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
582 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
583 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
584}
585
586static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
587{
588 OMAP_HSMMC_WRITE(host->base, ISE, 0);
589 OMAP_HSMMC_WRITE(host->base, IE, 0);
590 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
591}
592
Denis Karpov11dd62a2009-09-22 16:44:43 -0700593#ifdef CONFIG_PM
594
595/*
596 * Restore the MMC host context, if it was lost as result of a
597 * power state change.
598 */
Denis Karpov70a33412009-09-22 16:44:59 -0700599static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700600{
601 struct mmc_ios *ios = &host->mmc->ios;
602 struct omap_mmc_platform_data *pdata = host->pdata;
603 int context_loss = 0;
604 u32 hctl, capa, con;
605 u16 dsor = 0;
606 unsigned long timeout;
607
608 if (pdata->get_context_loss_count) {
609 context_loss = pdata->get_context_loss_count(host->dev);
610 if (context_loss < 0)
611 return 1;
612 }
613
614 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
615 context_loss == host->context_loss ? "not " : "");
616 if (host->context_loss == context_loss)
617 return 1;
618
619 /* Wait for hardware reset */
620 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
621 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
622 && time_before(jiffies, timeout))
623 ;
624
625 /* Do software reset */
626 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
627 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
628 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
629 && time_before(jiffies, timeout))
630 ;
631
632 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
633 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
634
635 if (host->id == OMAP_MMC1_DEVID) {
636 if (host->power_mode != MMC_POWER_OFF &&
637 (1 << ios->vdd) <= MMC_VDD_23_24)
638 hctl = SDVS18;
639 else
640 hctl = SDVS30;
641 capa = VS30 | VS18;
642 } else {
643 hctl = SDVS18;
644 capa = VS18;
645 }
646
647 OMAP_HSMMC_WRITE(host->base, HCTL,
648 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
649
650 OMAP_HSMMC_WRITE(host->base, CAPA,
651 OMAP_HSMMC_READ(host->base, CAPA) | capa);
652
653 OMAP_HSMMC_WRITE(host->base, HCTL,
654 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
655
656 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
657 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
658 && time_before(jiffies, timeout))
659 ;
660
Adrian Hunterb4175772010-05-26 14:42:06 -0700661 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700662
663 /* Do not initialize card-specific things if the power is off */
664 if (host->power_mode == MMC_POWER_OFF)
665 goto out;
666
667 con = OMAP_HSMMC_READ(host->base, CON);
668 switch (ios->bus_width) {
669 case MMC_BUS_WIDTH_8:
670 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
671 break;
672 case MMC_BUS_WIDTH_4:
673 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
674 OMAP_HSMMC_WRITE(host->base, HCTL,
675 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
676 break;
677 case MMC_BUS_WIDTH_1:
678 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
679 OMAP_HSMMC_WRITE(host->base, HCTL,
680 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
681 break;
682 }
683
684 if (ios->clock) {
685 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
686 if (dsor < 1)
687 dsor = 1;
688
689 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
690 dsor++;
691
692 if (dsor > 250)
693 dsor = 250;
694 }
695
696 OMAP_HSMMC_WRITE(host->base, SYSCTL,
697 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
698 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
699 OMAP_HSMMC_WRITE(host->base, SYSCTL,
700 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
701
702 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
703 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
704 && time_before(jiffies, timeout))
705 ;
706
707 OMAP_HSMMC_WRITE(host->base, SYSCTL,
708 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
709
710 con = OMAP_HSMMC_READ(host->base, CON);
711 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
712 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
713 else
714 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
715out:
716 host->context_loss = context_loss;
717
718 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
719 return 0;
720}
721
722/*
723 * Save the MMC host context (store the number of power state changes so far).
724 */
Denis Karpov70a33412009-09-22 16:44:59 -0700725static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700726{
727 struct omap_mmc_platform_data *pdata = host->pdata;
728 int context_loss;
729
730 if (pdata->get_context_loss_count) {
731 context_loss = pdata->get_context_loss_count(host->dev);
732 if (context_loss < 0)
733 return;
734 host->context_loss = context_loss;
735 }
736}
737
738#else
739
Denis Karpov70a33412009-09-22 16:44:59 -0700740static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700741{
742 return 0;
743}
744
Denis Karpov70a33412009-09-22 16:44:59 -0700745static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700746{
747}
748
749#endif
750
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100751/*
752 * Send init stream sequence to card
753 * before sending IDLE command
754 */
Denis Karpov70a33412009-09-22 16:44:59 -0700755static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756{
757 int reg = 0;
758 unsigned long timeout;
759
Adrian Hunterb62f6222009-09-22 16:45:01 -0700760 if (host->protect_card)
761 return;
762
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700764
765 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766 OMAP_HSMMC_WRITE(host->base, CON,
767 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
768 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
769
770 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
771 while ((reg != CC) && time_before(jiffies, timeout))
772 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
773
774 OMAP_HSMMC_WRITE(host->base, CON,
775 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700776
777 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
778 OMAP_HSMMC_READ(host->base, STAT);
779
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100780 enable_irq(host->irq);
781}
782
783static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700784int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100785{
786 int r = 1;
787
Denis Karpov191d1f12009-09-22 16:44:55 -0700788 if (mmc_slot(host).get_cover_state)
789 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790 return r;
791}
792
793static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700794omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795 char *buf)
796{
797 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700798 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100799
Denis Karpov70a33412009-09-22 16:44:59 -0700800 return sprintf(buf, "%s\n",
801 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802}
803
Denis Karpov70a33412009-09-22 16:44:59 -0700804static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805
806static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700807omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808 char *buf)
809{
810 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700811 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100812
Denis Karpov191d1f12009-09-22 16:44:55 -0700813 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100814}
815
Denis Karpov70a33412009-09-22 16:44:59 -0700816static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817
818/*
819 * Configure the response type and send the cmd.
820 */
821static void
Denis Karpov70a33412009-09-22 16:44:59 -0700822omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100823 struct mmc_data *data)
824{
825 int cmdreg = 0, resptype = 0, cmdtype = 0;
826
827 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
828 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
829 host->cmd = cmd;
830
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700831 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200833 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100834 if (cmd->flags & MMC_RSP_PRESENT) {
835 if (cmd->flags & MMC_RSP_136)
836 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200837 else if (cmd->flags & MMC_RSP_BUSY) {
838 resptype = 3;
839 host->response_busy = 1;
840 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100841 resptype = 2;
842 }
843
844 /*
845 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
846 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
847 * a val of 0x3, rest 0x0.
848 */
849 if (cmd == host->mrq->stop)
850 cmdtype = 0x3;
851
852 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
853
854 if (data) {
855 cmdreg |= DP_SELECT | MSBS | BCE;
856 if (data->flags & MMC_DATA_READ)
857 cmdreg |= DDIR;
858 else
859 cmdreg &= ~(DDIR);
860 }
861
862 if (host->use_dma)
863 cmdreg |= DMA_EN;
864
Adrian Hunterb4175772010-05-26 14:42:06 -0700865 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700866
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
868 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
869}
870
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200871static int
Denis Karpov70a33412009-09-22 16:44:59 -0700872omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200873{
874 if (data->flags & MMC_DATA_WRITE)
875 return DMA_TO_DEVICE;
876 else
877 return DMA_FROM_DEVICE;
878}
879
Adrian Hunterb4175772010-05-26 14:42:06 -0700880static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
881{
882 int dma_ch;
883
884 spin_lock(&host->irq_lock);
885 host->req_in_progress = 0;
886 dma_ch = host->dma_ch;
887 spin_unlock(&host->irq_lock);
888
889 omap_hsmmc_disable_irq(host);
890 /* Do not complete the request if DMA is still in progress */
891 if (mrq->data && host->use_dma && dma_ch != -1)
892 return;
893 host->mrq = NULL;
894 mmc_request_done(host->mmc, mrq);
895}
896
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100897/*
898 * Notify the transfer complete to MMC core
899 */
900static void
Denis Karpov70a33412009-09-22 16:44:59 -0700901omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200903 if (!data) {
904 struct mmc_request *mrq = host->mrq;
905
Adrian Hunter23050102009-09-22 16:44:57 -0700906 /* TC before CC from CMD6 - don't know why, but it happens */
907 if (host->cmd && host->cmd->opcode == 6 &&
908 host->response_busy) {
909 host->response_busy = 0;
910 return;
911 }
912
Adrian Hunterb4175772010-05-26 14:42:06 -0700913 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200914 return;
915 }
916
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917 host->data = NULL;
918
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919 if (!data->error)
920 data->bytes_xfered += data->blocks * (data->blksz);
921 else
922 data->bytes_xfered = 0;
923
924 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700925 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100926 return;
927 }
Denis Karpov70a33412009-09-22 16:44:59 -0700928 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929}
930
931/*
932 * Notify the core about command completion
933 */
934static void
Denis Karpov70a33412009-09-22 16:44:59 -0700935omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936{
937 host->cmd = NULL;
938
939 if (cmd->flags & MMC_RSP_PRESENT) {
940 if (cmd->flags & MMC_RSP_136) {
941 /* response type 2 */
942 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
943 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
944 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
945 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
946 } else {
947 /* response types 1, 1b, 3, 4, 5, 6 */
948 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
949 }
950 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700951 if ((host->data == NULL && !host->response_busy) || cmd->error)
952 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953}
954
955/*
956 * DMA clean up for command errors
957 */
Denis Karpov70a33412009-09-22 16:44:59 -0700958static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959{
Adrian Hunterb4175772010-05-26 14:42:06 -0700960 int dma_ch;
961
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200962 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963
Adrian Hunterb4175772010-05-26 14:42:06 -0700964 spin_lock(&host->irq_lock);
965 dma_ch = host->dma_ch;
966 host->dma_ch = -1;
967 spin_unlock(&host->irq_lock);
968
969 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200970 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
971 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700972 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700973 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100974 }
975 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100976}
977
978/*
979 * Readable error output
980 */
981#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700982static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983{
984 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700985 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
987 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
988 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
989 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
990 };
991 char res[256];
992 char *buf = res;
993 int len, i;
994
995 len = sprintf(buf, "MMC IRQ 0x%x :", status);
996 buf += len;
997
Denis Karpov70a33412009-09-22 16:44:59 -0700998 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001000 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001001 buf += len;
1002 }
1003
1004 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
1005}
1006#endif /* CONFIG_MMC_DEBUG */
1007
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001008/*
1009 * MMC controller internal state machines reset
1010 *
1011 * Used to reset command or data internal state machines, using respectively
1012 * SRC or SRD bit of SYSCTL register
1013 * Can be called from interrupt context
1014 */
Denis Karpov70a33412009-09-22 16:44:59 -07001015static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1016 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001017{
1018 unsigned long i = 0;
1019 unsigned long limit = (loops_per_jiffy *
1020 msecs_to_jiffies(MMC_TIMEOUT_MS));
1021
1022 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1023 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1024
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001025 /*
1026 * OMAP4 ES2 and greater has an updated reset logic.
1027 * Monitor a 0->1 transition first
1028 */
1029 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001030 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001031 && (i++ < limit))
1032 cpu_relax();
1033 }
1034 i = 0;
1035
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001036 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1037 (i++ < limit))
1038 cpu_relax();
1039
1040 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1041 dev_err(mmc_dev(host->mmc),
1042 "Timeout waiting on controller reset in %s\n",
1043 __func__);
1044}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001045
Adrian Hunterb4175772010-05-26 14:42:06 -07001046static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001047{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001049 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001050
Adrian Hunterb4175772010-05-26 14:42:06 -07001051 if (!host->req_in_progress) {
1052 do {
1053 OMAP_HSMMC_WRITE(host->base, STAT, status);
1054 /* Flush posted write */
1055 status = OMAP_HSMMC_READ(host->base, STAT);
1056 } while (status & INT_EN_MASK);
1057 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001058 }
1059
1060 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1062
1063 if (status & ERR) {
1064#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001065 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001066#endif
1067 if ((status & CMD_TIMEOUT) ||
1068 (status & CMD_CRC)) {
1069 if (host->cmd) {
1070 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001071 omap_hsmmc_reset_controller_fsm(host,
1072 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001073 host->cmd->error = -ETIMEDOUT;
1074 } else {
1075 host->cmd->error = -EILSEQ;
1076 }
1077 end_cmd = 1;
1078 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001079 if (host->data || host->response_busy) {
1080 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001081 omap_hsmmc_dma_cleanup(host,
1082 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001083 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001084 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001085 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001086 }
1087 if ((status & DATA_TIMEOUT) ||
1088 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001089 if (host->data || host->response_busy) {
1090 int err = (status & DATA_TIMEOUT) ?
1091 -ETIMEDOUT : -EILSEQ;
1092
1093 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001096 host->mrq->cmd->error = err;
1097 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001098 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099 end_trans = 1;
1100 }
1101 }
1102 if (status & CARD_ERR) {
1103 dev_dbg(mmc_dev(host->mmc),
1104 "Ignoring card err CMD%d\n", host->cmd->opcode);
1105 if (host->cmd)
1106 end_cmd = 1;
1107 if (host->data)
1108 end_trans = 1;
1109 }
1110 }
1111
1112 OMAP_HSMMC_WRITE(host->base, STAT, status);
1113
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001114 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001115 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001116 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001117 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001118}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119
Adrian Hunterb4175772010-05-26 14:42:06 -07001120/*
1121 * MMC controller IRQ handler
1122 */
1123static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1124{
1125 struct omap_hsmmc_host *host = dev_id;
1126 int status;
1127
1128 status = OMAP_HSMMC_READ(host->base, STAT);
1129 do {
1130 omap_hsmmc_do_irq(host, status);
1131 /* Flush posted write */
1132 status = OMAP_HSMMC_READ(host->base, STAT);
1133 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001134
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 return IRQ_HANDLED;
1136}
1137
Denis Karpov70a33412009-09-22 16:44:59 -07001138static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001139{
1140 unsigned long i;
1141
1142 OMAP_HSMMC_WRITE(host->base, HCTL,
1143 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1144 for (i = 0; i < loops_per_jiffy; i++) {
1145 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1146 break;
1147 cpu_relax();
1148 }
1149}
1150
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151/*
David Brownelleb250822009-02-17 14:49:01 -08001152 * Switch MMC interface voltage ... only relevant for MMC1.
1153 *
1154 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1155 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1156 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157 */
Denis Karpov70a33412009-09-22 16:44:59 -07001158static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159{
1160 u32 reg_val = 0;
1161 int ret;
1162
1163 /* Disable the clocks */
1164 clk_disable(host->fclk);
1165 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001166 if (host->got_dbclk)
1167 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001168
1169 /* Turn the power off */
1170 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171
1172 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001173 if (!ret)
1174 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1175 vdd);
1176 clk_enable(host->iclk);
1177 clk_enable(host->fclk);
1178 if (host->got_dbclk)
1179 clk_enable(host->dbclk);
1180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 if (ret != 0)
1182 goto err;
1183
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184 OMAP_HSMMC_WRITE(host->base, HCTL,
1185 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1186 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001187
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001188 /*
1189 * If a MMC dual voltage card is detected, the set_ios fn calls
1190 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001191 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192 *
David Brownelleb250822009-02-17 14:49:01 -08001193 * Cope with a bit of slop in the range ... per data sheets:
1194 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1195 * but recommended values are 1.71V to 1.89V
1196 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1197 * but recommended values are 2.7V to 3.3V
1198 *
1199 * Board setup code shouldn't permit anything very out-of-range.
1200 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1201 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001202 */
David Brownelleb250822009-02-17 14:49:01 -08001203 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001205 else
1206 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
1208 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001209 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001210
1211 return 0;
1212err:
1213 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1214 return ret;
1215}
1216
Adrian Hunterb62f6222009-09-22 16:45:01 -07001217/* Protect the card while the cover is open */
1218static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1219{
1220 if (!mmc_slot(host).get_cover_state)
1221 return;
1222
1223 host->reqs_blocked = 0;
1224 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1225 if (host->protect_card) {
1226 printk(KERN_INFO "%s: cover is closed, "
1227 "card is now accessible\n",
1228 mmc_hostname(host->mmc));
1229 host->protect_card = 0;
1230 }
1231 } else {
1232 if (!host->protect_card) {
1233 printk(KERN_INFO "%s: cover is open, "
1234 "card is now inaccessible\n",
1235 mmc_hostname(host->mmc));
1236 host->protect_card = 1;
1237 }
1238 }
1239}
1240
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001241/*
1242 * Work Item to notify the core about card insertion/removal
1243 */
Denis Karpov70a33412009-09-22 16:44:59 -07001244static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001245{
Denis Karpov70a33412009-09-22 16:44:59 -07001246 struct omap_hsmmc_host *host =
1247 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001248 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001249 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001250
Adrian Huntera6b22402009-09-22 16:44:45 -07001251 if (host->suspended)
1252 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253
1254 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001255
Denis Karpov191d1f12009-09-22 16:44:55 -07001256 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001257 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001258 else {
1259 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001260 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001261 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001262
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001263 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001265 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001266 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267}
1268
1269/*
1270 * ISR for handling card insertion and removal
1271 */
Denis Karpov70a33412009-09-22 16:44:59 -07001272static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273{
Denis Karpov70a33412009-09-22 16:44:59 -07001274 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275
Adrian Huntera6b22402009-09-22 16:44:45 -07001276 if (host->suspended)
1277 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278 schedule_work(&host->mmc_carddetect_work);
1279
1280 return IRQ_HANDLED;
1281}
1282
Denis Karpov70a33412009-09-22 16:44:59 -07001283static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001284 struct mmc_data *data)
1285{
1286 int sync_dev;
1287
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001288 if (data->flags & MMC_DATA_WRITE)
1289 sync_dev = host->dma_line_tx;
1290 else
1291 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001292 return sync_dev;
1293}
1294
Denis Karpov70a33412009-09-22 16:44:59 -07001295static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001296 struct mmc_data *data,
1297 struct scatterlist *sgl)
1298{
1299 int blksz, nblk, dma_ch;
1300
1301 dma_ch = host->dma_ch;
1302 if (data->flags & MMC_DATA_WRITE) {
1303 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1304 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1305 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1306 sg_dma_address(sgl), 0, 0);
1307 } else {
1308 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001309 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001310 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1311 sg_dma_address(sgl), 0, 0);
1312 }
1313
1314 blksz = host->data->blksz;
1315 nblk = sg_dma_len(sgl) / blksz;
1316
1317 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1318 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001319 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001320 !(data->flags & MMC_DATA_WRITE));
1321
1322 omap_start_dma(dma_ch);
1323}
1324
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001325/*
1326 * DMA call back function
1327 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001328static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329{
Adrian Hunterb4175772010-05-26 14:42:06 -07001330 struct omap_hsmmc_host *host = cb_data;
1331 struct mmc_data *data = host->mrq->data;
1332 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333
Venkatraman Sf3584e52010-08-10 18:01:54 -07001334 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1335 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1336 ch_status);
1337 return;
1338 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339
Adrian Hunterb4175772010-05-26 14:42:06 -07001340 spin_lock(&host->irq_lock);
1341 if (host->dma_ch < 0) {
1342 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001343 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001344 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001345
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001346 host->dma_sg_idx++;
1347 if (host->dma_sg_idx < host->dma_len) {
1348 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001349 omap_hsmmc_config_dma_params(host, data,
1350 data->sg + host->dma_sg_idx);
1351 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001352 return;
1353 }
1354
Per Forlin9782aff2011-07-01 18:55:23 +02001355 if (!data->host_cookie)
1356 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1357 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001358
1359 req_in_progress = host->req_in_progress;
1360 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001362 spin_unlock(&host->irq_lock);
1363
1364 omap_free_dma(dma_ch);
1365
1366 /* If DMA has finished after TC, complete the request */
1367 if (!req_in_progress) {
1368 struct mmc_request *mrq = host->mrq;
1369
1370 host->mrq = NULL;
1371 mmc_request_done(host->mmc, mrq);
1372 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373}
1374
Per Forlin9782aff2011-07-01 18:55:23 +02001375static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1376 struct mmc_data *data,
1377 struct omap_hsmmc_next *next)
1378{
1379 int dma_len;
1380
1381 if (!next && data->host_cookie &&
1382 data->host_cookie != host->next_data.cookie) {
1383 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
1384 " host->next_data.cookie %d\n",
1385 __func__, data->host_cookie, host->next_data.cookie);
1386 data->host_cookie = 0;
1387 }
1388
1389 /* Check if next job is already prepared */
1390 if (next ||
1391 (!next && data->host_cookie != host->next_data.cookie)) {
1392 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1393 data->sg_len,
1394 omap_hsmmc_get_dma_dir(host, data));
1395
1396 } else {
1397 dma_len = host->next_data.dma_len;
1398 host->next_data.dma_len = 0;
1399 }
1400
1401
1402 if (dma_len == 0)
1403 return -EINVAL;
1404
1405 if (next) {
1406 next->dma_len = dma_len;
1407 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1408 } else
1409 host->dma_len = dma_len;
1410
1411 return 0;
1412}
1413
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415 * Routine to configure and start DMA for the MMC card
1416 */
Denis Karpov70a33412009-09-22 16:44:59 -07001417static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1418 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001419{
Adrian Hunterb4175772010-05-26 14:42:06 -07001420 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421 struct mmc_data *data = req->data;
1422
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001423 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001424 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001425 struct scatterlist *sgl;
1426
1427 sgl = data->sg + i;
1428 if (sgl->length % data->blksz)
1429 return -EINVAL;
1430 }
1431 if ((data->blksz % 4) != 0)
1432 /* REVISIT: The MMC buffer increments only when MSB is written.
1433 * Return error for blksz which is non multiple of four.
1434 */
1435 return -EINVAL;
1436
Adrian Hunterb4175772010-05-26 14:42:06 -07001437 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438
Denis Karpov70a33412009-09-22 16:44:59 -07001439 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1440 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001442 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443 "%s: omap_request_dma() failed with %d\n",
1444 mmc_hostname(host->mmc), ret);
1445 return ret;
1446 }
Per Forlin9782aff2011-07-01 18:55:23 +02001447 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1448 if (ret)
1449 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001452 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001453
Denis Karpov70a33412009-09-22 16:44:59 -07001454 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001455
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001456 return 0;
1457}
1458
Denis Karpov70a33412009-09-22 16:44:59 -07001459static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001460 unsigned int timeout_ns,
1461 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001462{
1463 unsigned int timeout, cycle_ns;
1464 uint32_t reg, clkd, dto = 0;
1465
1466 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1467 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1468 if (clkd == 0)
1469 clkd = 1;
1470
1471 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001472 timeout = timeout_ns / cycle_ns;
1473 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474 if (timeout) {
1475 while ((timeout & 0x80000000) == 0) {
1476 dto += 1;
1477 timeout <<= 1;
1478 }
1479 dto = 31 - dto;
1480 timeout <<= 1;
1481 if (timeout && dto)
1482 dto += 1;
1483 if (dto >= 13)
1484 dto -= 13;
1485 else
1486 dto = 0;
1487 if (dto > 14)
1488 dto = 14;
1489 }
1490
1491 reg &= ~DTO_MASK;
1492 reg |= dto << DTO_SHIFT;
1493 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1494}
1495
1496/*
1497 * Configure block length for MMC/SD cards and initiate the transfer.
1498 */
1499static int
Denis Karpov70a33412009-09-22 16:44:59 -07001500omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001501{
1502 int ret;
1503 host->data = req->data;
1504
1505 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001506 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001507 /*
1508 * Set an arbitrary 100ms data timeout for commands with
1509 * busy signal.
1510 */
1511 if (req->cmd->flags & MMC_RSP_BUSY)
1512 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513 return 0;
1514 }
1515
1516 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1517 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001518 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001521 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001522 if (ret != 0) {
1523 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1524 return ret;
1525 }
1526 }
1527 return 0;
1528}
1529
Per Forlin9782aff2011-07-01 18:55:23 +02001530static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1531 int err)
1532{
1533 struct omap_hsmmc_host *host = mmc_priv(mmc);
1534 struct mmc_data *data = mrq->data;
1535
1536 if (host->use_dma) {
1537 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1538 omap_hsmmc_get_dma_dir(host, data));
1539 data->host_cookie = 0;
1540 }
1541}
1542
1543static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1544 bool is_first_req)
1545{
1546 struct omap_hsmmc_host *host = mmc_priv(mmc);
1547
1548 if (mrq->data->host_cookie) {
1549 mrq->data->host_cookie = 0;
1550 return ;
1551 }
1552
1553 if (host->use_dma)
1554 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1555 &host->next_data))
1556 mrq->data->host_cookie = 0;
1557}
1558
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001559/*
1560 * Request function. for read/write operation
1561 */
Denis Karpov70a33412009-09-22 16:44:59 -07001562static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563{
Denis Karpov70a33412009-09-22 16:44:59 -07001564 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001565 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001566
Adrian Hunterb4175772010-05-26 14:42:06 -07001567 BUG_ON(host->req_in_progress);
1568 BUG_ON(host->dma_ch != -1);
1569 if (host->protect_card) {
1570 if (host->reqs_blocked < 3) {
1571 /*
1572 * Ensure the controller is left in a consistent
1573 * state by resetting the command and data state
1574 * machines.
1575 */
1576 omap_hsmmc_reset_controller_fsm(host, SRD);
1577 omap_hsmmc_reset_controller_fsm(host, SRC);
1578 host->reqs_blocked += 1;
1579 }
1580 req->cmd->error = -EBADF;
1581 if (req->data)
1582 req->data->error = -EBADF;
1583 req->cmd->retries = 0;
1584 mmc_request_done(mmc, req);
1585 return;
1586 } else if (host->reqs_blocked)
1587 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001588 WARN_ON(host->mrq != NULL);
1589 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001590 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001591 if (err) {
1592 req->cmd->error = err;
1593 if (req->data)
1594 req->data->error = err;
1595 host->mrq = NULL;
1596 mmc_request_done(mmc, req);
1597 return;
1598 }
1599
Denis Karpov70a33412009-09-22 16:44:59 -07001600 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601}
1602
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001604static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605{
Denis Karpov70a33412009-09-22 16:44:59 -07001606 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607 u16 dsor = 0;
1608 unsigned long regval;
1609 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001610 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001611 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001613 mmc_host_enable(host->mmc);
1614
Adrian Huntera3621462009-09-22 16:44:42 -07001615 if (ios->power_mode != host->power_mode) {
1616 switch (ios->power_mode) {
1617 case MMC_POWER_OFF:
1618 mmc_slot(host).set_power(host->dev, host->slot_id,
1619 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001620 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001621 break;
1622 case MMC_POWER_UP:
1623 mmc_slot(host).set_power(host->dev, host->slot_id,
1624 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001625 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001626 break;
1627 case MMC_POWER_ON:
1628 do_send_init_stream = 1;
1629 break;
1630 }
1631 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001632 }
1633
Denis Karpovdd498ef2009-09-22 16:44:49 -07001634 /* FIXME: set registers based only on changes to ios */
1635
Jarkko Lavinen73153012008-11-21 16:49:54 +02001636 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001637 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001638 case MMC_BUS_WIDTH_8:
1639 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1640 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001641 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001642 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001643 OMAP_HSMMC_WRITE(host->base, HCTL,
1644 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1645 break;
1646 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001647 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001648 OMAP_HSMMC_WRITE(host->base, HCTL,
1649 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1650 break;
1651 }
1652
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301653 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001654 /* Only MMC1 can interface at 3V without some flavor
1655 * of external transceiver; but they all handle 1.8V.
1656 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001657 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1658 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1659 /*
1660 * The mmc_select_voltage fn of the core does
1661 * not seem to set the power_mode to
1662 * MMC_POWER_UP upon recalculating the voltage.
1663 * vdd 1.8v.
1664 */
Denis Karpov70a33412009-09-22 16:44:59 -07001665 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1666 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001667 "Switch operation failed\n");
1668 }
1669 }
1670
1671 if (ios->clock) {
1672 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1673 if (dsor < 1)
1674 dsor = 1;
1675
1676 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1677 dsor++;
1678
1679 if (dsor > 250)
1680 dsor = 250;
1681 }
Denis Karpov70a33412009-09-22 16:44:59 -07001682 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001683 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1684 regval = regval & ~(CLKD_MASK);
1685 regval = regval | (dsor << 6) | (DTO << 16);
1686 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1687 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1688 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1689
1690 /* Wait till the ICS bit is set */
1691 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001692 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001693 && time_before(jiffies, timeout))
1694 msleep(1);
1695
1696 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1697 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1698
Adrian Huntera3621462009-09-22 16:44:42 -07001699 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001700 send_init_stream(host);
1701
Denis Karpovabb28e72009-09-22 16:44:44 -07001702 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001703 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001704 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1705 else
1706 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001707
Denis Karpovdd498ef2009-09-22 16:44:49 -07001708 if (host->power_mode == MMC_POWER_OFF)
1709 mmc_host_disable(host->mmc);
1710 else
1711 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001712}
1713
1714static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1715{
Denis Karpov70a33412009-09-22 16:44:59 -07001716 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001717
Denis Karpov191d1f12009-09-22 16:44:55 -07001718 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001719 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001720 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001721}
1722
1723static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1724{
Denis Karpov70a33412009-09-22 16:44:59 -07001725 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001726
Denis Karpov191d1f12009-09-22 16:44:55 -07001727 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001728 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001729 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001730}
1731
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001732static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1733{
1734 struct omap_hsmmc_host *host = mmc_priv(mmc);
1735
1736 if (mmc_slot(host).init_card)
1737 mmc_slot(host).init_card(card);
1738}
1739
Denis Karpov70a33412009-09-22 16:44:59 -07001740static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001741{
1742 u32 hctl, capa, value;
1743
1744 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301745 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001746 hctl = SDVS30;
1747 capa = VS30 | VS18;
1748 } else {
1749 hctl = SDVS18;
1750 capa = VS18;
1751 }
1752
1753 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1754 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1755
1756 value = OMAP_HSMMC_READ(host->base, CAPA);
1757 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1758
1759 /* Set the controller to AUTO IDLE mode */
1760 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1761 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1762
1763 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001764 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001765}
1766
Denis Karpovdd498ef2009-09-22 16:44:49 -07001767/*
1768 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001769 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1770 * ^___________| | |
1771 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001772 *
1773 * ENABLED: mmc host is fully functional
1774 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001775 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1776 * REGSLEEP: fclk is off, voltage regulator is asleep
1777 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001778 *
1779 * Transition handlers return the timeout for the next state transition
1780 * or negative error.
1781 */
1782
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001783enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001784
1785/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001786static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001787{
Denis Karpov70a33412009-09-22 16:44:59 -07001788 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789 clk_disable(host->fclk);
1790 host->dpm_state = DISABLED;
1791
1792 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1793
1794 if (host->power_mode == MMC_POWER_OFF)
1795 return 0;
1796
Adrian Hunter4380eea22010-02-15 10:03:34 -08001797 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001798}
1799
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001800/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001801static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001802{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001803 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001804
1805 if (!mmc_try_claim_host(host->mmc))
1806 return 0;
1807
1808 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001809 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001810 if (mmc_card_can_sleep(host->mmc)) {
1811 err = mmc_card_sleep(host->mmc);
1812 if (err < 0) {
1813 clk_disable(host->fclk);
1814 mmc_release_host(host->mmc);
1815 return err;
1816 }
1817 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001818 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001819 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001820 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001821 if (mmc_slot(host).set_sleep)
1822 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1823 new_state == CARDSLEEP);
1824 /* FIXME: turn off bus power and perhaps interrupts too */
1825 clk_disable(host->fclk);
1826 host->dpm_state = new_state;
1827
1828 mmc_release_host(host->mmc);
1829
1830 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1831 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001832
Adrian Hunter1df58db2010-02-15 10:03:34 -08001833 if (mmc_slot(host).no_off)
1834 return 0;
1835
Denis Karpovdd498ef2009-09-22 16:44:49 -07001836 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1837 mmc_slot(host).card_detect ||
1838 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001839 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea22010-02-15 10:03:34 -08001840 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001841
1842 return 0;
1843}
1844
1845/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001846static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001847{
1848 if (!mmc_try_claim_host(host->mmc))
1849 return 0;
1850
Adrian Hunter1df58db2010-02-15 10:03:34 -08001851 if (mmc_slot(host).no_off)
1852 return 0;
1853
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001854 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1855 mmc_slot(host).card_detect ||
1856 (mmc_slot(host).get_cover_state &&
1857 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1858 mmc_release_host(host->mmc);
1859 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001860 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001861
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001862 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1863 host->vdd = 0;
1864 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001865
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001866 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1867 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001868
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001869 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001870
1871 mmc_release_host(host->mmc);
1872
1873 return 0;
1874}
1875
1876/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001877static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001878{
1879 int err;
1880
1881 err = clk_enable(host->fclk);
1882 if (err < 0)
1883 return err;
1884
Denis Karpov70a33412009-09-22 16:44:59 -07001885 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001886 host->dpm_state = ENABLED;
1887
1888 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1889
1890 return 0;
1891}
1892
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001893/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001894static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001895{
1896 if (!mmc_try_claim_host(host->mmc))
1897 return 0;
1898
1899 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001900 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001901 if (mmc_slot(host).set_sleep)
1902 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1903 host->vdd, host->dpm_state == CARDSLEEP);
1904 if (mmc_card_can_sleep(host->mmc))
1905 mmc_card_awake(host->mmc);
1906
1907 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1908 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1909
1910 host->dpm_state = ENABLED;
1911
1912 mmc_release_host(host->mmc);
1913
1914 return 0;
1915}
1916
Denis Karpovdd498ef2009-09-22 16:44:49 -07001917/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001918static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001919{
1920 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001921
Denis Karpov70a33412009-09-22 16:44:59 -07001922 omap_hsmmc_context_restore(host);
1923 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001924 mmc_power_restore_host(host->mmc);
1925
1926 host->dpm_state = ENABLED;
1927
1928 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1929
1930 return 0;
1931}
1932
1933/*
1934 * Bring MMC host to ENABLED from any other PM state.
1935 */
Denis Karpov70a33412009-09-22 16:44:59 -07001936static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001937{
Denis Karpov70a33412009-09-22 16:44:59 -07001938 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001939
1940 switch (host->dpm_state) {
1941 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001942 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001943 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001944 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001945 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001946 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001947 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001948 default:
1949 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1950 return -EINVAL;
1951 }
1952}
1953
1954/*
1955 * Bring MMC host in PM state (one level deeper).
1956 */
Denis Karpov70a33412009-09-22 16:44:59 -07001957static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001958{
Denis Karpov70a33412009-09-22 16:44:59 -07001959 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001960
1961 switch (host->dpm_state) {
1962 case ENABLED: {
1963 int delay;
1964
Denis Karpov70a33412009-09-22 16:44:59 -07001965 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001966 if (lazy || delay < 0)
1967 return delay;
1968 return 0;
1969 }
1970 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001971 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001972 case CARDSLEEP:
1973 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001974 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001975 default:
1976 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1977 return -EINVAL;
1978 }
1979}
1980
Denis Karpov70a33412009-09-22 16:44:59 -07001981static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001982{
Denis Karpov70a33412009-09-22 16:44:59 -07001983 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001984 int err;
1985
1986 err = clk_enable(host->fclk);
1987 if (err)
1988 return err;
1989 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001990 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001991 return 0;
1992}
1993
Denis Karpov70a33412009-09-22 16:44:59 -07001994static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001995{
Denis Karpov70a33412009-09-22 16:44:59 -07001996 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001997
Denis Karpov70a33412009-09-22 16:44:59 -07001998 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001999 clk_disable(host->fclk);
2000 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
2001 return 0;
2002}
2003
Denis Karpov70a33412009-09-22 16:44:59 -07002004static const struct mmc_host_ops omap_hsmmc_ops = {
2005 .enable = omap_hsmmc_enable_fclk,
2006 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02002007 .post_req = omap_hsmmc_post_req,
2008 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07002009 .request = omap_hsmmc_request,
2010 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07002011 .get_cd = omap_hsmmc_get_cd,
2012 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07002013 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07002014 /* NYET -- enable_sdio_irq */
2015};
2016
Denis Karpov70a33412009-09-22 16:44:59 -07002017static const struct mmc_host_ops omap_hsmmc_ps_ops = {
2018 .enable = omap_hsmmc_enable,
2019 .disable = omap_hsmmc_disable,
2020 .request = omap_hsmmc_request,
2021 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022 .get_cd = omap_hsmmc_get_cd,
2023 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07002024 .init_card = omap_hsmmc_init_card,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025 /* NYET -- enable_sdio_irq */
2026};
2027
Denis Karpovd900f712009-09-22 16:44:38 -07002028#ifdef CONFIG_DEBUG_FS
2029
Denis Karpov70a33412009-09-22 16:44:59 -07002030static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07002031{
2032 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07002033 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002034 int context_loss = 0;
2035
Denis Karpov70a33412009-09-22 16:44:59 -07002036 if (host->pdata->get_context_loss_count)
2037 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002038
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002039 seq_printf(s, "mmc%d:\n"
2040 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07002041 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002042 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07002043 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002044 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07002045 mmc->index, mmc->enabled ? 1 : 0,
2046 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07002047 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002048
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002049 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07002050 seq_printf(s, "host suspended, can't read registers\n");
2051 return 0;
2052 }
2053
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002054 if (clk_enable(host->fclk) != 0) {
2055 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07002056 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002057 }
Denis Karpovd900f712009-09-22 16:44:38 -07002058
2059 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
2060 OMAP_HSMMC_READ(host->base, SYSCONFIG));
2061 seq_printf(s, "CON:\t\t0x%08x\n",
2062 OMAP_HSMMC_READ(host->base, CON));
2063 seq_printf(s, "HCTL:\t\t0x%08x\n",
2064 OMAP_HSMMC_READ(host->base, HCTL));
2065 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
2066 OMAP_HSMMC_READ(host->base, SYSCTL));
2067 seq_printf(s, "IE:\t\t0x%08x\n",
2068 OMAP_HSMMC_READ(host->base, IE));
2069 seq_printf(s, "ISE:\t\t0x%08x\n",
2070 OMAP_HSMMC_READ(host->base, ISE));
2071 seq_printf(s, "CAPA:\t\t0x%08x\n",
2072 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002073
2074 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07002075
Denis Karpovd900f712009-09-22 16:44:38 -07002076 return 0;
2077}
2078
Denis Karpov70a33412009-09-22 16:44:59 -07002079static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07002080{
Denis Karpov70a33412009-09-22 16:44:59 -07002081 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07002082}
2083
2084static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002085 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07002086 .read = seq_read,
2087 .llseek = seq_lseek,
2088 .release = single_release,
2089};
2090
Denis Karpov70a33412009-09-22 16:44:59 -07002091static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07002092{
2093 if (mmc->debugfs_root)
2094 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
2095 mmc, &mmc_regs_fops);
2096}
2097
2098#else
2099
Denis Karpov70a33412009-09-22 16:44:59 -07002100static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07002101{
2102}
2103
2104#endif
2105
Denis Karpov70a33412009-09-22 16:44:59 -07002106static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107{
2108 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
2109 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002110 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002112 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113
2114 if (pdata == NULL) {
2115 dev_err(&pdev->dev, "Platform Data is missing\n");
2116 return -ENXIO;
2117 }
2118
2119 if (pdata->nr_slots == 0) {
2120 dev_err(&pdev->dev, "No Slots\n");
2121 return -ENXIO;
2122 }
2123
2124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2125 irq = platform_get_irq(pdev, 0);
2126 if (res == NULL || irq < 0)
2127 return -ENXIO;
2128
kishore kadiyala91a0b082010-10-01 16:35:28 -07002129 res->start += pdata->reg_offset;
2130 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07002131 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002132 if (res == NULL)
2133 return -EBUSY;
2134
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002135 ret = omap_hsmmc_gpio_init(pdata);
2136 if (ret)
2137 goto err;
2138
Denis Karpov70a33412009-09-22 16:44:59 -07002139 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140 if (!mmc) {
2141 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002142 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143 }
2144
2145 host = mmc_priv(mmc);
2146 host->mmc = mmc;
2147 host->pdata = pdata;
2148 host->dev = &pdev->dev;
2149 host->use_dma = 1;
2150 host->dev->dma_mask = &pdata->dma_mask;
2151 host->dma_ch = -1;
2152 host->irq = irq;
2153 host->id = pdev->id;
2154 host->slot_id = 0;
2155 host->mapbase = res->start;
2156 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002157 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002158 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159
2160 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002161 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162
Denis Karpov191d1f12009-09-22 16:44:55 -07002163 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002164 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002165 else
Denis Karpov70a33412009-09-22 16:44:59 -07002166 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002167
Adrian Huntere0eb2422010-02-15 10:03:34 -08002168 /*
2169 * If regulator_disable can only put vcc_aux to sleep then there is
2170 * no off state.
2171 */
2172 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2173 mmc_slot(host).no_off = 1;
2174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175 mmc->f_min = 400000;
2176 mmc->f_max = 52000000;
2177
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002178 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179
Russell King6f7607c2009-01-28 10:22:50 +00002180 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002181 if (IS_ERR(host->iclk)) {
2182 ret = PTR_ERR(host->iclk);
2183 host->iclk = NULL;
2184 goto err1;
2185 }
Russell King6f7607c2009-01-28 10:22:50 +00002186 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002187 if (IS_ERR(host->fclk)) {
2188 ret = PTR_ERR(host->fclk);
2189 host->fclk = NULL;
2190 clk_put(host->iclk);
2191 goto err1;
2192 }
2193
Denis Karpov70a33412009-09-22 16:44:59 -07002194 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002195
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002196 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002197 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2198 /* we start off in DISABLED state */
2199 host->dpm_state = DISABLED;
2200
Paul Walmsleya05dcdb2011-02-25 18:22:08 -07002201 if (clk_enable(host->iclk) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202 clk_put(host->iclk);
2203 clk_put(host->fclk);
2204 goto err1;
2205 }
2206
Paul Walmsleya05dcdb2011-02-25 18:22:08 -07002207 if (mmc_host_enable(host->mmc) != 0) {
2208 clk_disable(host->iclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002209 clk_put(host->iclk);
2210 clk_put(host->fclk);
2211 goto err1;
2212 }
2213
Adrian Hunter2bec0892009-09-22 16:45:02 -07002214 if (cpu_is_omap2430()) {
2215 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2216 /*
2217 * MMC can still work without debounce clock.
2218 */
2219 if (IS_ERR(host->dbclk))
2220 dev_warn(mmc_dev(host->mmc),
2221 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002223 host->got_dbclk = 1;
2224
2225 if (host->got_dbclk)
2226 if (clk_enable(host->dbclk) != 0)
2227 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2228 " clk failed\n");
2229 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002231 /* Since we do only SG emulation, we can have as many segs
2232 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002233 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002234
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002235 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2236 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2237 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2238 mmc->max_seg_size = mmc->max_req_size;
2239
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002240 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002241 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002243 mmc->caps |= mmc_slot(host).caps;
2244 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245 mmc->caps |= MMC_CAP_4_BIT_DATA;
2246
Denis Karpov191d1f12009-09-22 16:44:55 -07002247 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002248 mmc->caps |= MMC_CAP_NONREMOVABLE;
2249
Denis Karpov70a33412009-09-22 16:44:59 -07002250 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002252 /* Select DMA lines */
2253 switch (host->id) {
2254 case OMAP_MMC1_DEVID:
2255 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2256 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2257 break;
2258 case OMAP_MMC2_DEVID:
2259 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2260 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2261 break;
2262 case OMAP_MMC3_DEVID:
2263 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2264 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2265 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002266 case OMAP_MMC4_DEVID:
2267 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2268 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2269 break;
2270 case OMAP_MMC5_DEVID:
2271 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2272 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2273 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002274 default:
2275 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2276 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002277 }
2278
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002279 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002280 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002281 mmc_hostname(mmc), host);
2282 if (ret) {
2283 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2284 goto err_irq;
2285 }
2286
2287 if (pdata->init != NULL) {
2288 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002289 dev_dbg(mmc_dev(host->mmc),
2290 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002291 goto err_irq_cd_init;
2292 }
2293 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002294
Adrian Hunterb702b102010-02-15 10:03:35 -08002295 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002296 ret = omap_hsmmc_reg_get(host);
2297 if (ret)
2298 goto err_reg;
2299 host->use_reg = 1;
2300 }
2301
David Brownellb583f262009-05-28 14:04:03 -07002302 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002303
2304 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002305 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002306 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002307 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002308 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2309 | IRQF_DISABLED,
2310 mmc_hostname(mmc), host);
2311 if (ret) {
2312 dev_dbg(mmc_dev(host->mmc),
2313 "Unable to grab MMC CD IRQ\n");
2314 goto err_irq_cd;
2315 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002316 pdata->suspend = omap_hsmmc_suspend_cdirq;
2317 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002318 }
2319
Adrian Hunterb4175772010-05-26 14:42:06 -07002320 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002321
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002322 mmc_host_lazy_disable(host->mmc);
2323
Adrian Hunterb62f6222009-09-22 16:45:01 -07002324 omap_hsmmc_protect_card(host);
2325
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002326 mmc_add_host(mmc);
2327
Denis Karpov191d1f12009-09-22 16:44:55 -07002328 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002329 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2330 if (ret < 0)
2331 goto err_slot_name;
2332 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002333 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002334 ret = device_create_file(&mmc->class_dev,
2335 &dev_attr_cover_switch);
2336 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002337 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002338 }
2339
Denis Karpov70a33412009-09-22 16:44:59 -07002340 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002341
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002342 return 0;
2343
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002344err_slot_name:
2345 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002346 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002347err_irq_cd:
2348 if (host->use_reg)
2349 omap_hsmmc_reg_put(host);
2350err_reg:
2351 if (host->pdata->cleanup)
2352 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002353err_irq_cd_init:
2354 free_irq(host->irq, host);
2355err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002356 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002357 clk_disable(host->iclk);
2358 clk_put(host->fclk);
2359 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002360 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002361 clk_disable(host->dbclk);
2362 clk_put(host->dbclk);
2363 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002364err1:
2365 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002366 platform_set_drvdata(pdev, NULL);
2367 mmc_free_host(mmc);
2368err_alloc:
2369 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002370err:
Chris Ball984b2032011-03-22 16:34:42 -07002371 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002372 return ret;
2373}
2374
Denis Karpov70a33412009-09-22 16:44:59 -07002375static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002376{
Denis Karpov70a33412009-09-22 16:44:59 -07002377 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002378 struct resource *res;
2379
2380 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002381 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002382 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002383 if (host->use_reg)
2384 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002385 if (host->pdata->cleanup)
2386 host->pdata->cleanup(&pdev->dev);
2387 free_irq(host->irq, host);
2388 if (mmc_slot(host).card_detect_irq)
2389 free_irq(mmc_slot(host).card_detect_irq, host);
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002390 flush_work_sync(&host->mmc_carddetect_work);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002391
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002392 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002393 clk_disable(host->iclk);
2394 clk_put(host->fclk);
2395 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002396 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002397 clk_disable(host->dbclk);
2398 clk_put(host->dbclk);
2399 }
2400
2401 mmc_free_host(host->mmc);
2402 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002403 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002404 }
2405
2406 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2407 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002408 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002409 platform_set_drvdata(pdev, NULL);
2410
2411 return 0;
2412}
2413
2414#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002415static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002416{
2417 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002418 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002419 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002420
2421 if (host && host->suspended)
2422 return 0;
2423
2424 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002425 host->suspended = 1;
2426 if (host->pdata->suspend) {
2427 ret = host->pdata->suspend(&pdev->dev,
2428 host->slot_id);
2429 if (ret) {
2430 dev_dbg(mmc_dev(host->mmc),
2431 "Unable to handle MMC board"
2432 " level suspend\n");
2433 host->suspended = 0;
2434 return ret;
2435 }
2436 }
2437 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002438 ret = mmc_suspend_host(host->mmc);
Ethan Due7cb7562010-09-09 16:37:49 -07002439 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002440 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002441 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002442 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002443 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002444 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002445 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002446 if (host->got_dbclk)
2447 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002448 } else {
2449 host->suspended = 0;
2450 if (host->pdata->resume) {
2451 ret = host->pdata->resume(&pdev->dev,
2452 host->slot_id);
2453 if (ret)
2454 dev_dbg(mmc_dev(host->mmc),
2455 "Unmask interrupt failed\n");
2456 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002457 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002458 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002459
2460 }
2461 return ret;
2462}
2463
2464/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002465static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002466{
2467 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002468 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002469 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002470
2471 if (host && !host->suspended)
2472 return 0;
2473
2474 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002475 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002476 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002477 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002478
Denis Karpov11dd62a2009-09-22 16:44:43 -07002479 if (mmc_host_enable(host->mmc) != 0) {
2480 clk_disable(host->iclk);
2481 goto clk_en_err;
2482 }
2483
Adrian Hunter2bec0892009-09-22 16:45:02 -07002484 if (host->got_dbclk)
2485 clk_enable(host->dbclk);
2486
Denis Karpov70a33412009-09-22 16:44:59 -07002487 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002488
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002489 if (host->pdata->resume) {
2490 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2491 if (ret)
2492 dev_dbg(mmc_dev(host->mmc),
2493 "Unmask interrupt failed\n");
2494 }
2495
Adrian Hunterb62f6222009-09-22 16:45:01 -07002496 omap_hsmmc_protect_card(host);
2497
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002498 /* Notify the core to resume the host */
2499 ret = mmc_resume_host(host->mmc);
2500 if (ret == 0)
2501 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002502
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002503 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002504 }
2505
2506 return ret;
2507
2508clk_en_err:
2509 dev_dbg(mmc_dev(host->mmc),
2510 "Failed to enable MMC clocks during resume\n");
2511 return ret;
2512}
2513
2514#else
Denis Karpov70a33412009-09-22 16:44:59 -07002515#define omap_hsmmc_suspend NULL
2516#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002517#endif
2518
Kevin Hilmana791daa2010-05-26 14:42:07 -07002519static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002520 .suspend = omap_hsmmc_suspend,
2521 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002522};
2523
2524static struct platform_driver omap_hsmmc_driver = {
2525 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002526 .driver = {
2527 .name = DRIVER_NAME,
2528 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002529 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002530 },
2531};
2532
Denis Karpov70a33412009-09-22 16:44:59 -07002533static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002534{
2535 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002536 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002537}
2538
Denis Karpov70a33412009-09-22 16:44:59 -07002539static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002540{
2541 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002542 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002543}
2544
Denis Karpov70a33412009-09-22 16:44:59 -07002545module_init(omap_hsmmc_init);
2546module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002547
2548MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2549MODULE_LICENSE("GPL");
2550MODULE_ALIAS("platform:" DRIVER_NAME);
2551MODULE_AUTHOR("Texas Instruments Inc");