blob: b0b52ced45540f36a4525b8f44859a7849a140d9 [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>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053036#include <linux/pm_runtime.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010038#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/board.h>
40#include <plat/mmc.h>
41#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042
43/* OMAP HSMMC Host Controller Registers */
44#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070045#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046#define OMAP_HSMMC_CON 0x002C
47#define OMAP_HSMMC_BLK 0x0104
48#define OMAP_HSMMC_ARG 0x0108
49#define OMAP_HSMMC_CMD 0x010C
50#define OMAP_HSMMC_RSP10 0x0110
51#define OMAP_HSMMC_RSP32 0x0114
52#define OMAP_HSMMC_RSP54 0x0118
53#define OMAP_HSMMC_RSP76 0x011C
54#define OMAP_HSMMC_DATA 0x0120
55#define OMAP_HSMMC_HCTL 0x0128
56#define OMAP_HSMMC_SYSCTL 0x012C
57#define OMAP_HSMMC_STAT 0x0130
58#define OMAP_HSMMC_IE 0x0134
59#define OMAP_HSMMC_ISE 0x0138
60#define OMAP_HSMMC_CAPA 0x0140
61
62#define VS18 (1 << 26)
63#define VS30 (1 << 25)
64#define SDVS18 (0x5 << 9)
65#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080066#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010067#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define SDVSCLR 0xFFFFF1FF
69#define SDVSDET 0x00000400
70#define AUTOIDLE 0x1
71#define SDBP (1 << 8)
72#define DTO 0xe
73#define ICE 0x1
74#define ICS 0x2
75#define CEN (1 << 2)
76#define CLKD_MASK 0x0000FFC0
77#define CLKD_SHIFT 6
78#define DTO_MASK 0x000F0000
79#define DTO_SHIFT 16
80#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070081#define BWR_ENABLE (1 << 4)
82#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070083#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define INIT_STREAM (1 << 1)
85#define DP_SELECT (1 << 21)
86#define DDIR (1 << 4)
87#define DMA_EN 0x1
88#define MSBS (1 << 5)
89#define BCE (1 << 1)
90#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020091#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define CC 0x1
93#define TC 0x02
94#define OD 0x1
95#define ERR (1 << 15)
96#define CMD_TIMEOUT (1 << 16)
97#define DATA_TIMEOUT (1 << 20)
98#define CMD_CRC (1 << 17)
99#define DATA_CRC (1 << 21)
100#define CARD_ERR (1 << 28)
101#define STAT_CLEAR 0xFFFFFFFF
102#define INIT_STREAM_CMD 0x00000000
103#define DUAL_VOLT_OCR_BIT 7
104#define SRC (1 << 25)
105#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700106#define SOFTRESET (1 << 1)
107#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100108
109/*
110 * FIXME: Most likely all the data using these _DEVID defines should come
111 * from the platform_data, or implemented in controller and slot specific
112 * functions.
113 */
114#define OMAP_MMC1_DEVID 0
115#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000116#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700117#define OMAP_MMC4_DEVID 3
118#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530120#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100121#define MMC_TIMEOUT_MS 20
122#define OMAP_MMC_MASTER_CLOCK 96000000
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400123#define OMAP_MMC_MIN_CLOCK 400000
124#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530125#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100126
127/*
128 * One controller can have multiple slots, like on some omap boards using
129 * omap.c controller driver. Luckily this is not currently done on any known
130 * omap_hsmmc.c device.
131 */
132#define mmc_slot(host) (host->pdata->slots[host->slot_id])
133
134/*
135 * MMC Host controller read/write API's
136 */
137#define OMAP_HSMMC_READ(base, reg) \
138 __raw_readl((base) + OMAP_HSMMC_##reg)
139
140#define OMAP_HSMMC_WRITE(base, reg, val) \
141 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
142
Per Forlin9782aff2011-07-01 18:55:23 +0200143struct omap_hsmmc_next {
144 unsigned int dma_len;
145 s32 cookie;
146};
147
Denis Karpov70a33412009-09-22 16:44:59 -0700148struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149 struct device *dev;
150 struct mmc_host *mmc;
151 struct mmc_request *mrq;
152 struct mmc_command *cmd;
153 struct mmc_data *data;
154 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100155 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800156 /*
157 * vcc == configured supply
158 * vcc_aux == optional
159 * - MMC1, supply for DAT4..DAT7
160 * - MMC2/MMC2, external level shifter voltage supply, for
161 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
162 */
163 struct regulator *vcc;
164 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100165 struct work_struct mmc_carddetect_work;
166 void __iomem *base;
167 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700168 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned int id;
170 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200171 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700173 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100174 u32 *buffer;
175 u32 bytesleft;
176 int suspended;
177 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000179 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100180 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700181 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200182 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700183 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700184 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700185 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700186 int protect_card;
187 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800188 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700189 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200190 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700191
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100192 struct omap_mmc_platform_data *pdata;
193};
194
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800195static int omap_hsmmc_card_detect(struct device *dev, int slot)
196{
197 struct omap_mmc_platform_data *mmc = dev->platform_data;
198
199 /* NOTE: assumes card detect signal is active-low */
200 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
201}
202
203static int omap_hsmmc_get_wp(struct device *dev, int slot)
204{
205 struct omap_mmc_platform_data *mmc = dev->platform_data;
206
207 /* NOTE: assumes write protect signal is active-high */
208 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
209}
210
211static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
212{
213 struct omap_mmc_platform_data *mmc = dev->platform_data;
214
215 /* NOTE: assumes card detect signal is active-low */
216 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
217}
218
219#ifdef CONFIG_PM
220
221static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
222{
223 struct omap_mmc_platform_data *mmc = dev->platform_data;
224
225 disable_irq(mmc->slots[0].card_detect_irq);
226 return 0;
227}
228
229static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
230{
231 struct omap_mmc_platform_data *mmc = dev->platform_data;
232
233 enable_irq(mmc->slots[0].card_detect_irq);
234 return 0;
235}
236
237#else
238
239#define omap_hsmmc_suspend_cdirq NULL
240#define omap_hsmmc_resume_cdirq NULL
241
242#endif
243
Adrian Hunterb702b102010-02-15 10:03:35 -0800244#ifdef CONFIG_REGULATOR
245
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
247 int vdd)
248{
249 struct omap_hsmmc_host *host =
250 platform_get_drvdata(to_platform_device(dev));
251 int ret;
252
253 if (mmc_slot(host).before_set_reg)
254 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
255
256 if (power_on)
Linus Walleij99fc5132010-09-29 01:08:27 -0400257 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400259 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260
261 if (mmc_slot(host).after_set_reg)
262 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
263
264 return ret;
265}
266
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500267static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800268 int vdd)
269{
270 struct omap_hsmmc_host *host =
271 platform_get_drvdata(to_platform_device(dev));
272 int ret = 0;
273
274 /*
275 * If we don't see a Vcc regulator, assume it's a fixed
276 * voltage always-on regulator.
277 */
278 if (!host->vcc)
279 return 0;
280
281 if (mmc_slot(host).before_set_reg)
282 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
283
284 /*
285 * Assume Vcc regulator is used only to power the card ... OMAP
286 * VDDS is used to power the pins, optionally with a transceiver to
287 * support cards using voltages other than VDDS (1.8V nominal). When a
288 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
289 *
290 * In some cases this regulator won't support enable/disable;
291 * e.g. it's a fixed rail for a WLAN chip.
292 *
293 * In other cases vcc_aux switches interface power. Example, for
294 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
295 * chips/cards need an interface voltage rail too.
296 */
297 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400298 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800299 /* Enable interface voltage rail, if needed */
300 if (ret == 0 && host->vcc_aux) {
301 ret = regulator_enable(host->vcc_aux);
302 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400303 ret = mmc_regulator_set_ocr(host->mmc,
304 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305 }
306 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400307 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800308 if (host->vcc_aux)
309 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400310 if (!ret) {
311 /* Then proceed to shut down the local regulator */
312 ret = mmc_regulator_set_ocr(host->mmc,
313 host->vcc, 0);
314 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800315 }
316
317 if (mmc_slot(host).after_set_reg)
318 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
319
320 return ret;
321}
322
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500323static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
324 int vdd)
325{
326 return 0;
327}
328
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800329static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
330 int vdd, int cardsleep)
331{
332 struct omap_hsmmc_host *host =
333 platform_get_drvdata(to_platform_device(dev));
334 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
335
336 return regulator_set_mode(host->vcc, mode);
337}
338
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500339static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800340 int vdd, int cardsleep)
341{
342 struct omap_hsmmc_host *host =
343 platform_get_drvdata(to_platform_device(dev));
344 int err, mode;
345
346 /*
347 * If we don't see a Vcc regulator, assume it's a fixed
348 * voltage always-on regulator.
349 */
350 if (!host->vcc)
351 return 0;
352
353 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
354
355 if (!host->vcc_aux)
356 return regulator_set_mode(host->vcc, mode);
357
358 if (cardsleep) {
359 /* VCC can be turned off if card is asleep */
360 if (sleep)
Linus Walleij99fc5132010-09-29 01:08:27 -0400361 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800362 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400363 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364 } else
365 err = regulator_set_mode(host->vcc, mode);
366 if (err)
367 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800368
369 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
370 return regulator_set_mode(host->vcc_aux, mode);
371
372 if (sleep)
373 return regulator_disable(host->vcc_aux);
374 else
375 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800376}
377
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500378static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
379 int vdd, int cardsleep)
380{
381 return 0;
382}
383
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800384static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
385{
386 struct regulator *reg;
387 int ret = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700388 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800389
390 switch (host->id) {
391 case OMAP_MMC1_DEVID:
392 /* On-chip level shifting via PBIAS0/PBIAS1 */
393 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
394 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
395 break;
396 case OMAP_MMC2_DEVID:
397 case OMAP_MMC3_DEVID:
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500398 case OMAP_MMC5_DEVID:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800399 /* Off-chip level shifting, or none */
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500400 mmc_slot(host).set_power = omap_hsmmc_235_set_power;
401 mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800402 break;
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500403 case OMAP_MMC4_DEVID:
404 mmc_slot(host).set_power = omap_hsmmc_4_set_power;
405 mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800406 default:
407 pr_err("MMC%d configuration not supported!\n", host->id);
408 return -EINVAL;
409 }
410
411 reg = regulator_get(host->dev, "vmmc");
412 if (IS_ERR(reg)) {
413 dev_dbg(host->dev, "vmmc regulator missing\n");
414 /*
415 * HACK: until fixed.c regulator is usable,
416 * we don't require a main regulator
417 * for MMC2 or MMC3
418 */
419 if (host->id == OMAP_MMC1_DEVID) {
420 ret = PTR_ERR(reg);
421 goto err;
422 }
423 } else {
424 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700425 ocr_value = mmc_regulator_get_ocrmask(reg);
426 if (!mmc_slot(host).ocr_mask) {
427 mmc_slot(host).ocr_mask = ocr_value;
428 } else {
429 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
430 pr_err("MMC%d ocrmask %x is not supported\n",
431 host->id, mmc_slot(host).ocr_mask);
432 mmc_slot(host).ocr_mask = 0;
433 return -EINVAL;
434 }
435 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800436
437 /* Allow an aux regulator */
438 reg = regulator_get(host->dev, "vmmc_aux");
439 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
440
Balaji T Kb1c1df72011-05-30 19:55:34 +0530441 /* For eMMC do not power off when not in sleep state */
442 if (mmc_slot(host).no_regulator_off_init)
443 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800444 /*
445 * UGLY HACK: workaround regulator framework bugs.
446 * When the bootloader leaves a supply active, it's
447 * initialized with zero usecount ... and we can't
448 * disable it without first enabling it. Until the
449 * framework is fixed, we need a workaround like this
450 * (which is safe for MMC, but not in general).
451 */
452 if (regulator_is_enabled(host->vcc) > 0) {
453 regulator_enable(host->vcc);
454 regulator_disable(host->vcc);
455 }
456 if (host->vcc_aux) {
457 if (regulator_is_enabled(reg) > 0) {
458 regulator_enable(reg);
459 regulator_disable(reg);
460 }
461 }
462 }
463
464 return 0;
465
466err:
467 mmc_slot(host).set_power = NULL;
468 mmc_slot(host).set_sleep = NULL;
469 return ret;
470}
471
472static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
473{
474 regulator_put(host->vcc);
475 regulator_put(host->vcc_aux);
476 mmc_slot(host).set_power = NULL;
477 mmc_slot(host).set_sleep = NULL;
478}
479
Adrian Hunterb702b102010-02-15 10:03:35 -0800480static inline int omap_hsmmc_have_reg(void)
481{
482 return 1;
483}
484
485#else
486
487static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
488{
489 return -EINVAL;
490}
491
492static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
493{
494}
495
496static inline int omap_hsmmc_have_reg(void)
497{
498 return 0;
499}
500
501#endif
502
503static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
504{
505 int ret;
506
507 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800508 if (pdata->slots[0].cover)
509 pdata->slots[0].get_cover_state =
510 omap_hsmmc_get_cover_state;
511 else
512 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
513 pdata->slots[0].card_detect_irq =
514 gpio_to_irq(pdata->slots[0].switch_pin);
515 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
516 if (ret)
517 return ret;
518 ret = gpio_direction_input(pdata->slots[0].switch_pin);
519 if (ret)
520 goto err_free_sp;
521 } else
522 pdata->slots[0].switch_pin = -EINVAL;
523
524 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
525 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
526 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
527 if (ret)
528 goto err_free_cd;
529 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
530 if (ret)
531 goto err_free_wp;
532 } else
533 pdata->slots[0].gpio_wp = -EINVAL;
534
535 return 0;
536
537err_free_wp:
538 gpio_free(pdata->slots[0].gpio_wp);
539err_free_cd:
540 if (gpio_is_valid(pdata->slots[0].switch_pin))
541err_free_sp:
542 gpio_free(pdata->slots[0].switch_pin);
543 return ret;
544}
545
546static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
547{
548 if (gpio_is_valid(pdata->slots[0].gpio_wp))
549 gpio_free(pdata->slots[0].gpio_wp);
550 if (gpio_is_valid(pdata->slots[0].switch_pin))
551 gpio_free(pdata->slots[0].switch_pin);
552}
553
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100554/*
555 * Stop clock to the card
556 */
Denis Karpov70a33412009-09-22 16:44:59 -0700557static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100558{
559 OMAP_HSMMC_WRITE(host->base, SYSCTL,
560 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
561 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
562 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
563}
564
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700565static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
566 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700567{
568 unsigned int irq_mask;
569
570 if (host->use_dma)
571 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
572 else
573 irq_mask = INT_EN_MASK;
574
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700575 /* Disable timeout for erases */
576 if (cmd->opcode == MMC_ERASE)
577 irq_mask &= ~DTO_ENABLE;
578
Adrian Hunterb4175772010-05-26 14:42:06 -0700579 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
580 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
581 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
582}
583
584static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
585{
586 OMAP_HSMMC_WRITE(host->base, ISE, 0);
587 OMAP_HSMMC_WRITE(host->base, IE, 0);
588 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
589}
590
Denis Karpov11dd62a2009-09-22 16:44:43 -0700591#ifdef CONFIG_PM
592
593/*
594 * Restore the MMC host context, if it was lost as result of a
595 * power state change.
596 */
Denis Karpov70a33412009-09-22 16:44:59 -0700597static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700598{
599 struct mmc_ios *ios = &host->mmc->ios;
600 struct omap_mmc_platform_data *pdata = host->pdata;
601 int context_loss = 0;
602 u32 hctl, capa, con;
603 u16 dsor = 0;
604 unsigned long timeout;
605
606 if (pdata->get_context_loss_count) {
607 context_loss = pdata->get_context_loss_count(host->dev);
608 if (context_loss < 0)
609 return 1;
610 }
611
612 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
613 context_loss == host->context_loss ? "not " : "");
614 if (host->context_loss == context_loss)
615 return 1;
616
617 /* Wait for hardware reset */
618 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
619 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
620 && time_before(jiffies, timeout))
621 ;
622
623 /* Do software reset */
624 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
625 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
626 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
627 && time_before(jiffies, timeout))
628 ;
629
630 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
631 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
632
633 if (host->id == OMAP_MMC1_DEVID) {
634 if (host->power_mode != MMC_POWER_OFF &&
635 (1 << ios->vdd) <= MMC_VDD_23_24)
636 hctl = SDVS18;
637 else
638 hctl = SDVS30;
639 capa = VS30 | VS18;
640 } else {
641 hctl = SDVS18;
642 capa = VS18;
643 }
644
645 OMAP_HSMMC_WRITE(host->base, HCTL,
646 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
647
648 OMAP_HSMMC_WRITE(host->base, CAPA,
649 OMAP_HSMMC_READ(host->base, CAPA) | capa);
650
651 OMAP_HSMMC_WRITE(host->base, HCTL,
652 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
653
654 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
655 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
656 && time_before(jiffies, timeout))
657 ;
658
Adrian Hunterb4175772010-05-26 14:42:06 -0700659 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700660
661 /* Do not initialize card-specific things if the power is off */
662 if (host->power_mode == MMC_POWER_OFF)
663 goto out;
664
665 con = OMAP_HSMMC_READ(host->base, CON);
666 switch (ios->bus_width) {
667 case MMC_BUS_WIDTH_8:
668 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
669 break;
670 case MMC_BUS_WIDTH_4:
671 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
672 OMAP_HSMMC_WRITE(host->base, HCTL,
673 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
674 break;
675 case MMC_BUS_WIDTH_1:
676 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
677 OMAP_HSMMC_WRITE(host->base, HCTL,
678 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
679 break;
680 }
681
682 if (ios->clock) {
683 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
684 if (dsor < 1)
685 dsor = 1;
686
687 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
688 dsor++;
689
690 if (dsor > 250)
691 dsor = 250;
692 }
693
694 OMAP_HSMMC_WRITE(host->base, SYSCTL,
695 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
696 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
697 OMAP_HSMMC_WRITE(host->base, SYSCTL,
698 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
699
700 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
701 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
702 && time_before(jiffies, timeout))
703 ;
704
705 OMAP_HSMMC_WRITE(host->base, SYSCTL,
706 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
707
708 con = OMAP_HSMMC_READ(host->base, CON);
709 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
710 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
711 else
712 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
713out:
714 host->context_loss = context_loss;
715
716 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
717 return 0;
718}
719
720/*
721 * Save the MMC host context (store the number of power state changes so far).
722 */
Denis Karpov70a33412009-09-22 16:44:59 -0700723static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700724{
725 struct omap_mmc_platform_data *pdata = host->pdata;
726 int context_loss;
727
728 if (pdata->get_context_loss_count) {
729 context_loss = pdata->get_context_loss_count(host->dev);
730 if (context_loss < 0)
731 return;
732 host->context_loss = context_loss;
733 }
734}
735
736#else
737
Denis Karpov70a33412009-09-22 16:44:59 -0700738static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700739{
740 return 0;
741}
742
Denis Karpov70a33412009-09-22 16:44:59 -0700743static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700744{
745}
746
747#endif
748
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100749/*
750 * Send init stream sequence to card
751 * before sending IDLE command
752 */
Denis Karpov70a33412009-09-22 16:44:59 -0700753static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754{
755 int reg = 0;
756 unsigned long timeout;
757
Adrian Hunterb62f6222009-09-22 16:45:01 -0700758 if (host->protect_card)
759 return;
760
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100761 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700762
763 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 OMAP_HSMMC_WRITE(host->base, CON,
765 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
766 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
767
768 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
769 while ((reg != CC) && time_before(jiffies, timeout))
770 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
771
772 OMAP_HSMMC_WRITE(host->base, CON,
773 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700774
775 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
776 OMAP_HSMMC_READ(host->base, STAT);
777
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778 enable_irq(host->irq);
779}
780
781static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700782int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783{
784 int r = 1;
785
Denis Karpov191d1f12009-09-22 16:44:55 -0700786 if (mmc_slot(host).get_cover_state)
787 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 return r;
789}
790
791static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700792omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793 char *buf)
794{
795 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700796 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100797
Denis Karpov70a33412009-09-22 16:44:59 -0700798 return sprintf(buf, "%s\n",
799 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100800}
801
Denis Karpov70a33412009-09-22 16:44:59 -0700802static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803
804static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700805omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806 char *buf)
807{
808 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700809 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810
Denis Karpov191d1f12009-09-22 16:44:55 -0700811 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100812}
813
Denis Karpov70a33412009-09-22 16:44:59 -0700814static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815
816/*
817 * Configure the response type and send the cmd.
818 */
819static void
Denis Karpov70a33412009-09-22 16:44:59 -0700820omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100821 struct mmc_data *data)
822{
823 int cmdreg = 0, resptype = 0, cmdtype = 0;
824
825 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
826 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
827 host->cmd = cmd;
828
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700829 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200831 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832 if (cmd->flags & MMC_RSP_PRESENT) {
833 if (cmd->flags & MMC_RSP_136)
834 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200835 else if (cmd->flags & MMC_RSP_BUSY) {
836 resptype = 3;
837 host->response_busy = 1;
838 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100839 resptype = 2;
840 }
841
842 /*
843 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
844 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
845 * a val of 0x3, rest 0x0.
846 */
847 if (cmd == host->mrq->stop)
848 cmdtype = 0x3;
849
850 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
851
852 if (data) {
853 cmdreg |= DP_SELECT | MSBS | BCE;
854 if (data->flags & MMC_DATA_READ)
855 cmdreg |= DDIR;
856 else
857 cmdreg &= ~(DDIR);
858 }
859
860 if (host->use_dma)
861 cmdreg |= DMA_EN;
862
Adrian Hunterb4175772010-05-26 14:42:06 -0700863 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700864
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100865 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
866 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
867}
868
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200869static int
Denis Karpov70a33412009-09-22 16:44:59 -0700870omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200871{
872 if (data->flags & MMC_DATA_WRITE)
873 return DMA_TO_DEVICE;
874 else
875 return DMA_FROM_DEVICE;
876}
877
Adrian Hunterb4175772010-05-26 14:42:06 -0700878static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
879{
880 int dma_ch;
881
882 spin_lock(&host->irq_lock);
883 host->req_in_progress = 0;
884 dma_ch = host->dma_ch;
885 spin_unlock(&host->irq_lock);
886
887 omap_hsmmc_disable_irq(host);
888 /* Do not complete the request if DMA is still in progress */
889 if (mrq->data && host->use_dma && dma_ch != -1)
890 return;
891 host->mrq = NULL;
892 mmc_request_done(host->mmc, mrq);
893}
894
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100895/*
896 * Notify the transfer complete to MMC core
897 */
898static void
Denis Karpov70a33412009-09-22 16:44:59 -0700899omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200901 if (!data) {
902 struct mmc_request *mrq = host->mrq;
903
Adrian Hunter23050102009-09-22 16:44:57 -0700904 /* TC before CC from CMD6 - don't know why, but it happens */
905 if (host->cmd && host->cmd->opcode == 6 &&
906 host->response_busy) {
907 host->response_busy = 0;
908 return;
909 }
910
Adrian Hunterb4175772010-05-26 14:42:06 -0700911 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200912 return;
913 }
914
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915 host->data = NULL;
916
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917 if (!data->error)
918 data->bytes_xfered += data->blocks * (data->blksz);
919 else
920 data->bytes_xfered = 0;
921
922 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700923 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100924 return;
925 }
Denis Karpov70a33412009-09-22 16:44:59 -0700926 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927}
928
929/*
930 * Notify the core about command completion
931 */
932static void
Denis Karpov70a33412009-09-22 16:44:59 -0700933omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934{
935 host->cmd = NULL;
936
937 if (cmd->flags & MMC_RSP_PRESENT) {
938 if (cmd->flags & MMC_RSP_136) {
939 /* response type 2 */
940 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
941 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
942 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
943 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
944 } else {
945 /* response types 1, 1b, 3, 4, 5, 6 */
946 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
947 }
948 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700949 if ((host->data == NULL && !host->response_busy) || cmd->error)
950 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100951}
952
953/*
954 * DMA clean up for command errors
955 */
Denis Karpov70a33412009-09-22 16:44:59 -0700956static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100957{
Adrian Hunterb4175772010-05-26 14:42:06 -0700958 int dma_ch;
959
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200960 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961
Adrian Hunterb4175772010-05-26 14:42:06 -0700962 spin_lock(&host->irq_lock);
963 dma_ch = host->dma_ch;
964 host->dma_ch = -1;
965 spin_unlock(&host->irq_lock);
966
967 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200968 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
969 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700970 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700971 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100972 }
973 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100974}
975
976/*
977 * Readable error output
978 */
979#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300980static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981{
982 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700983 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300984 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
985 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
986 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
987 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100988 };
989 char res[256];
990 char *buf = res;
991 int len, i;
992
993 len = sprintf(buf, "MMC IRQ 0x%x :", status);
994 buf += len;
995
Denis Karpov70a33412009-09-22 16:44:59 -0700996 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700998 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999 buf += len;
1000 }
1001
1002 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
1003}
Adrian Hunter699b9582011-05-06 12:14:01 +03001004#else
1005static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1006 u32 status)
1007{
1008}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001009#endif /* CONFIG_MMC_DEBUG */
1010
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001011/*
1012 * MMC controller internal state machines reset
1013 *
1014 * Used to reset command or data internal state machines, using respectively
1015 * SRC or SRD bit of SYSCTL register
1016 * Can be called from interrupt context
1017 */
Denis Karpov70a33412009-09-22 16:44:59 -07001018static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1019 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001020{
1021 unsigned long i = 0;
1022 unsigned long limit = (loops_per_jiffy *
1023 msecs_to_jiffies(MMC_TIMEOUT_MS));
1024
1025 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1026 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1027
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001028 /*
1029 * OMAP4 ES2 and greater has an updated reset logic.
1030 * Monitor a 0->1 transition first
1031 */
1032 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001033 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001034 && (i++ < limit))
1035 cpu_relax();
1036 }
1037 i = 0;
1038
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001039 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1040 (i++ < limit))
1041 cpu_relax();
1042
1043 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1044 dev_err(mmc_dev(host->mmc),
1045 "Timeout waiting on controller reset in %s\n",
1046 __func__);
1047}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048
Adrian Hunterb4175772010-05-26 14:42:06 -07001049static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001050{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001052 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053
Adrian Hunterb4175772010-05-26 14:42:06 -07001054 if (!host->req_in_progress) {
1055 do {
1056 OMAP_HSMMC_WRITE(host->base, STAT, status);
1057 /* Flush posted write */
1058 status = OMAP_HSMMC_READ(host->base, STAT);
1059 } while (status & INT_EN_MASK);
1060 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061 }
1062
1063 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001064 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1065
1066 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001067 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 if ((status & CMD_TIMEOUT) ||
1069 (status & CMD_CRC)) {
1070 if (host->cmd) {
1071 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001072 omap_hsmmc_reset_controller_fsm(host,
1073 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001074 host->cmd->error = -ETIMEDOUT;
1075 } else {
1076 host->cmd->error = -EILSEQ;
1077 }
1078 end_cmd = 1;
1079 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001080 if (host->data || host->response_busy) {
1081 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001082 omap_hsmmc_dma_cleanup(host,
1083 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001084 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001085 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001086 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087 }
1088 if ((status & DATA_TIMEOUT) ||
1089 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001090 if (host->data || host->response_busy) {
1091 int err = (status & DATA_TIMEOUT) ?
1092 -ETIMEDOUT : -EILSEQ;
1093
1094 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001095 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001097 host->mrq->cmd->error = err;
1098 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001099 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100 end_trans = 1;
1101 }
1102 }
1103 if (status & CARD_ERR) {
1104 dev_dbg(mmc_dev(host->mmc),
1105 "Ignoring card err CMD%d\n", host->cmd->opcode);
1106 if (host->cmd)
1107 end_cmd = 1;
1108 if (host->data)
1109 end_trans = 1;
1110 }
1111 }
1112
1113 OMAP_HSMMC_WRITE(host->base, STAT, status);
1114
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001115 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001116 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001117 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001118 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001119}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001120
Adrian Hunterb4175772010-05-26 14:42:06 -07001121/*
1122 * MMC controller IRQ handler
1123 */
1124static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1125{
1126 struct omap_hsmmc_host *host = dev_id;
1127 int status;
1128
1129 status = OMAP_HSMMC_READ(host->base, STAT);
1130 do {
1131 omap_hsmmc_do_irq(host, status);
1132 /* Flush posted write */
1133 status = OMAP_HSMMC_READ(host->base, STAT);
1134 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001135
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136 return IRQ_HANDLED;
1137}
1138
Denis Karpov70a33412009-09-22 16:44:59 -07001139static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001140{
1141 unsigned long i;
1142
1143 OMAP_HSMMC_WRITE(host->base, HCTL,
1144 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1145 for (i = 0; i < loops_per_jiffy; i++) {
1146 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1147 break;
1148 cpu_relax();
1149 }
1150}
1151
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152/*
David Brownelleb250822009-02-17 14:49:01 -08001153 * Switch MMC interface voltage ... only relevant for MMC1.
1154 *
1155 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1156 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1157 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158 */
Denis Karpov70a33412009-09-22 16:44:59 -07001159static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160{
1161 u32 reg_val = 0;
1162 int ret;
1163
1164 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301165 pm_runtime_put_sync(host->dev);
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);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301176 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001177 if (host->got_dbclk)
1178 clk_enable(host->dbclk);
1179
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001180 if (ret != 0)
1181 goto err;
1182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183 OMAP_HSMMC_WRITE(host->base, HCTL,
1184 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1185 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001186
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001187 /*
1188 * If a MMC dual voltage card is detected, the set_ios fn calls
1189 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001190 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001191 *
David Brownelleb250822009-02-17 14:49:01 -08001192 * Cope with a bit of slop in the range ... per data sheets:
1193 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1194 * but recommended values are 1.71V to 1.89V
1195 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1196 * but recommended values are 2.7V to 3.3V
1197 *
1198 * Board setup code shouldn't permit anything very out-of-range.
1199 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1200 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201 */
David Brownelleb250822009-02-17 14:49:01 -08001202 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001203 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001204 else
1205 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206
1207 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001208 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001209
1210 return 0;
1211err:
1212 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1213 return ret;
1214}
1215
Adrian Hunterb62f6222009-09-22 16:45:01 -07001216/* Protect the card while the cover is open */
1217static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1218{
1219 if (!mmc_slot(host).get_cover_state)
1220 return;
1221
1222 host->reqs_blocked = 0;
1223 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1224 if (host->protect_card) {
1225 printk(KERN_INFO "%s: cover is closed, "
1226 "card is now accessible\n",
1227 mmc_hostname(host->mmc));
1228 host->protect_card = 0;
1229 }
1230 } else {
1231 if (!host->protect_card) {
1232 printk(KERN_INFO "%s: cover is open, "
1233 "card is now inaccessible\n",
1234 mmc_hostname(host->mmc));
1235 host->protect_card = 1;
1236 }
1237 }
1238}
1239
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240/*
1241 * Work Item to notify the core about card insertion/removal
1242 */
Denis Karpov70a33412009-09-22 16:44:59 -07001243static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244{
Denis Karpov70a33412009-09-22 16:44:59 -07001245 struct omap_hsmmc_host *host =
1246 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001247 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001248 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001249
Adrian Huntera6b22402009-09-22 16:44:45 -07001250 if (host->suspended)
1251 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252
1253 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001254
Denis Karpov191d1f12009-09-22 16:44:55 -07001255 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001256 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001257 else {
1258 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001259 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001260 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001261
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001262 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001263 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001264 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001265 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001266}
1267
1268/*
1269 * ISR for handling card insertion and removal
1270 */
Denis Karpov70a33412009-09-22 16:44:59 -07001271static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272{
Denis Karpov70a33412009-09-22 16:44:59 -07001273 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274
Adrian Huntera6b22402009-09-22 16:44:45 -07001275 if (host->suspended)
1276 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277 schedule_work(&host->mmc_carddetect_work);
1278
1279 return IRQ_HANDLED;
1280}
1281
Denis Karpov70a33412009-09-22 16:44:59 -07001282static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001283 struct mmc_data *data)
1284{
1285 int sync_dev;
1286
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001287 if (data->flags & MMC_DATA_WRITE)
1288 sync_dev = host->dma_line_tx;
1289 else
1290 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001291 return sync_dev;
1292}
1293
Denis Karpov70a33412009-09-22 16:44:59 -07001294static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001295 struct mmc_data *data,
1296 struct scatterlist *sgl)
1297{
1298 int blksz, nblk, dma_ch;
1299
1300 dma_ch = host->dma_ch;
1301 if (data->flags & MMC_DATA_WRITE) {
1302 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1303 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1304 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1305 sg_dma_address(sgl), 0, 0);
1306 } else {
1307 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001308 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001309 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1310 sg_dma_address(sgl), 0, 0);
1311 }
1312
1313 blksz = host->data->blksz;
1314 nblk = sg_dma_len(sgl) / blksz;
1315
1316 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1317 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001318 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001319 !(data->flags & MMC_DATA_WRITE));
1320
1321 omap_start_dma(dma_ch);
1322}
1323
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324/*
1325 * DMA call back function
1326 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001327static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001328{
Adrian Hunterb4175772010-05-26 14:42:06 -07001329 struct omap_hsmmc_host *host = cb_data;
1330 struct mmc_data *data = host->mrq->data;
1331 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001332
Venkatraman Sf3584e52010-08-10 18:01:54 -07001333 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1334 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1335 ch_status);
1336 return;
1337 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001338
Adrian Hunterb4175772010-05-26 14:42:06 -07001339 spin_lock(&host->irq_lock);
1340 if (host->dma_ch < 0) {
1341 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001343 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001344
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001345 host->dma_sg_idx++;
1346 if (host->dma_sg_idx < host->dma_len) {
1347 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001348 omap_hsmmc_config_dma_params(host, data,
1349 data->sg + host->dma_sg_idx);
1350 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001351 return;
1352 }
1353
Per Forlin9782aff2011-07-01 18:55:23 +02001354 if (!data->host_cookie)
1355 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1356 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001357
1358 req_in_progress = host->req_in_progress;
1359 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001360 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001361 spin_unlock(&host->irq_lock);
1362
1363 omap_free_dma(dma_ch);
1364
1365 /* If DMA has finished after TC, complete the request */
1366 if (!req_in_progress) {
1367 struct mmc_request *mrq = host->mrq;
1368
1369 host->mrq = NULL;
1370 mmc_request_done(host->mmc, mrq);
1371 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372}
1373
Per Forlin9782aff2011-07-01 18:55:23 +02001374static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1375 struct mmc_data *data,
1376 struct omap_hsmmc_next *next)
1377{
1378 int dma_len;
1379
1380 if (!next && data->host_cookie &&
1381 data->host_cookie != host->next_data.cookie) {
1382 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
1383 " host->next_data.cookie %d\n",
1384 __func__, data->host_cookie, host->next_data.cookie);
1385 data->host_cookie = 0;
1386 }
1387
1388 /* Check if next job is already prepared */
1389 if (next ||
1390 (!next && data->host_cookie != host->next_data.cookie)) {
1391 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1392 data->sg_len,
1393 omap_hsmmc_get_dma_dir(host, data));
1394
1395 } else {
1396 dma_len = host->next_data.dma_len;
1397 host->next_data.dma_len = 0;
1398 }
1399
1400
1401 if (dma_len == 0)
1402 return -EINVAL;
1403
1404 if (next) {
1405 next->dma_len = dma_len;
1406 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1407 } else
1408 host->dma_len = dma_len;
1409
1410 return 0;
1411}
1412
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001413/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414 * Routine to configure and start DMA for the MMC card
1415 */
Denis Karpov70a33412009-09-22 16:44:59 -07001416static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1417 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001418{
Adrian Hunterb4175772010-05-26 14:42:06 -07001419 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420 struct mmc_data *data = req->data;
1421
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001422 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001423 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001424 struct scatterlist *sgl;
1425
1426 sgl = data->sg + i;
1427 if (sgl->length % data->blksz)
1428 return -EINVAL;
1429 }
1430 if ((data->blksz % 4) != 0)
1431 /* REVISIT: The MMC buffer increments only when MSB is written.
1432 * Return error for blksz which is non multiple of four.
1433 */
1434 return -EINVAL;
1435
Adrian Hunterb4175772010-05-26 14:42:06 -07001436 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001437
Denis Karpov70a33412009-09-22 16:44:59 -07001438 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1439 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001440 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001441 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442 "%s: omap_request_dma() failed with %d\n",
1443 mmc_hostname(host->mmc), ret);
1444 return ret;
1445 }
Per Forlin9782aff2011-07-01 18:55:23 +02001446 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1447 if (ret)
1448 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001451 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001452
Denis Karpov70a33412009-09-22 16:44:59 -07001453 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001454
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001455 return 0;
1456}
1457
Denis Karpov70a33412009-09-22 16:44:59 -07001458static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001459 unsigned int timeout_ns,
1460 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461{
1462 unsigned int timeout, cycle_ns;
1463 uint32_t reg, clkd, dto = 0;
1464
1465 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1466 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1467 if (clkd == 0)
1468 clkd = 1;
1469
1470 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001471 timeout = timeout_ns / cycle_ns;
1472 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473 if (timeout) {
1474 while ((timeout & 0x80000000) == 0) {
1475 dto += 1;
1476 timeout <<= 1;
1477 }
1478 dto = 31 - dto;
1479 timeout <<= 1;
1480 if (timeout && dto)
1481 dto += 1;
1482 if (dto >= 13)
1483 dto -= 13;
1484 else
1485 dto = 0;
1486 if (dto > 14)
1487 dto = 14;
1488 }
1489
1490 reg &= ~DTO_MASK;
1491 reg |= dto << DTO_SHIFT;
1492 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1493}
1494
1495/*
1496 * Configure block length for MMC/SD cards and initiate the transfer.
1497 */
1498static int
Denis Karpov70a33412009-09-22 16:44:59 -07001499omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001500{
1501 int ret;
1502 host->data = req->data;
1503
1504 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001506 /*
1507 * Set an arbitrary 100ms data timeout for commands with
1508 * busy signal.
1509 */
1510 if (req->cmd->flags & MMC_RSP_BUSY)
1511 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001512 return 0;
1513 }
1514
1515 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1516 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001517 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001518
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001520 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521 if (ret != 0) {
1522 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1523 return ret;
1524 }
1525 }
1526 return 0;
1527}
1528
Per Forlin9782aff2011-07-01 18:55:23 +02001529static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1530 int err)
1531{
1532 struct omap_hsmmc_host *host = mmc_priv(mmc);
1533 struct mmc_data *data = mrq->data;
1534
1535 if (host->use_dma) {
1536 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1537 omap_hsmmc_get_dma_dir(host, data));
1538 data->host_cookie = 0;
1539 }
1540}
1541
1542static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1543 bool is_first_req)
1544{
1545 struct omap_hsmmc_host *host = mmc_priv(mmc);
1546
1547 if (mrq->data->host_cookie) {
1548 mrq->data->host_cookie = 0;
1549 return ;
1550 }
1551
1552 if (host->use_dma)
1553 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1554 &host->next_data))
1555 mrq->data->host_cookie = 0;
1556}
1557
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558/*
1559 * Request function. for read/write operation
1560 */
Denis Karpov70a33412009-09-22 16:44:59 -07001561static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562{
Denis Karpov70a33412009-09-22 16:44:59 -07001563 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001564 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565
Adrian Hunterb4175772010-05-26 14:42:06 -07001566 BUG_ON(host->req_in_progress);
1567 BUG_ON(host->dma_ch != -1);
1568 if (host->protect_card) {
1569 if (host->reqs_blocked < 3) {
1570 /*
1571 * Ensure the controller is left in a consistent
1572 * state by resetting the command and data state
1573 * machines.
1574 */
1575 omap_hsmmc_reset_controller_fsm(host, SRD);
1576 omap_hsmmc_reset_controller_fsm(host, SRC);
1577 host->reqs_blocked += 1;
1578 }
1579 req->cmd->error = -EBADF;
1580 if (req->data)
1581 req->data->error = -EBADF;
1582 req->cmd->retries = 0;
1583 mmc_request_done(mmc, req);
1584 return;
1585 } else if (host->reqs_blocked)
1586 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001587 WARN_ON(host->mrq != NULL);
1588 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001589 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001590 if (err) {
1591 req->cmd->error = err;
1592 if (req->data)
1593 req->data->error = err;
1594 host->mrq = NULL;
1595 mmc_request_done(mmc, req);
1596 return;
1597 }
1598
Denis Karpov70a33412009-09-22 16:44:59 -07001599 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600}
1601
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001603static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001604{
Denis Karpov70a33412009-09-22 16:44:59 -07001605 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606 u16 dsor = 0;
1607 unsigned long regval;
1608 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001609 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001610 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001611
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301612 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001613
Adrian Huntera3621462009-09-22 16:44:42 -07001614 if (ios->power_mode != host->power_mode) {
1615 switch (ios->power_mode) {
1616 case MMC_POWER_OFF:
1617 mmc_slot(host).set_power(host->dev, host->slot_id,
1618 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001619 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001620 break;
1621 case MMC_POWER_UP:
1622 mmc_slot(host).set_power(host->dev, host->slot_id,
1623 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001624 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001625 break;
1626 case MMC_POWER_ON:
1627 do_send_init_stream = 1;
1628 break;
1629 }
1630 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001631 }
1632
Denis Karpovdd498ef2009-09-22 16:44:49 -07001633 /* FIXME: set registers based only on changes to ios */
1634
Jarkko Lavinen73153012008-11-21 16:49:54 +02001635 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001636 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001637 case MMC_BUS_WIDTH_8:
1638 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1639 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001641 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001642 OMAP_HSMMC_WRITE(host->base, HCTL,
1643 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1644 break;
1645 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001646 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647 OMAP_HSMMC_WRITE(host->base, HCTL,
1648 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1649 break;
1650 }
1651
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301652 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001653 /* Only MMC1 can interface at 3V without some flavor
1654 * of external transceiver; but they all handle 1.8V.
1655 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001656 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1657 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1658 /*
1659 * The mmc_select_voltage fn of the core does
1660 * not seem to set the power_mode to
1661 * MMC_POWER_UP upon recalculating the voltage.
1662 * vdd 1.8v.
1663 */
Denis Karpov70a33412009-09-22 16:44:59 -07001664 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1665 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001666 "Switch operation failed\n");
1667 }
1668 }
1669
1670 if (ios->clock) {
1671 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1672 if (dsor < 1)
1673 dsor = 1;
1674
1675 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1676 dsor++;
1677
1678 if (dsor > 250)
1679 dsor = 250;
1680 }
Denis Karpov70a33412009-09-22 16:44:59 -07001681 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001682 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1683 regval = regval & ~(CLKD_MASK);
1684 regval = regval | (dsor << 6) | (DTO << 16);
1685 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1686 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1687 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1688
1689 /* Wait till the ICS bit is set */
1690 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001691 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001692 && time_before(jiffies, timeout))
1693 msleep(1);
1694
1695 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1696 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1697
Adrian Huntera3621462009-09-22 16:44:42 -07001698 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001699 send_init_stream(host);
1700
Denis Karpovabb28e72009-09-22 16:44:44 -07001701 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001702 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001703 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1704 else
1705 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001706
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301707 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001708}
1709
1710static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1711{
Denis Karpov70a33412009-09-22 16:44:59 -07001712 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001713
Denis Karpov191d1f12009-09-22 16:44:55 -07001714 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001715 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001716 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001717}
1718
1719static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1720{
Denis Karpov70a33412009-09-22 16:44:59 -07001721 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001722
Denis Karpov191d1f12009-09-22 16:44:55 -07001723 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001724 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001725 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001726}
1727
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001728static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1729{
1730 struct omap_hsmmc_host *host = mmc_priv(mmc);
1731
1732 if (mmc_slot(host).init_card)
1733 mmc_slot(host).init_card(card);
1734}
1735
Denis Karpov70a33412009-09-22 16:44:59 -07001736static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001737{
1738 u32 hctl, capa, value;
1739
1740 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301741 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001742 hctl = SDVS30;
1743 capa = VS30 | VS18;
1744 } else {
1745 hctl = SDVS18;
1746 capa = VS18;
1747 }
1748
1749 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1750 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1751
1752 value = OMAP_HSMMC_READ(host->base, CAPA);
1753 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1754
1755 /* Set the controller to AUTO IDLE mode */
1756 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1757 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1758
1759 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001760 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001761}
1762
Denis Karpov70a33412009-09-22 16:44:59 -07001763static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001764{
Denis Karpov70a33412009-09-22 16:44:59 -07001765 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001766
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301767 pm_runtime_get_sync(host->dev);
1768
Denis Karpovdd498ef2009-09-22 16:44:49 -07001769 return 0;
1770}
1771
Denis Karpov70a33412009-09-22 16:44:59 -07001772static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001773{
Denis Karpov70a33412009-09-22 16:44:59 -07001774 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001775
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301776 pm_runtime_mark_last_busy(host->dev);
1777 pm_runtime_put_autosuspend(host->dev);
1778
Denis Karpovdd498ef2009-09-22 16:44:49 -07001779 return 0;
1780}
1781
Denis Karpov70a33412009-09-22 16:44:59 -07001782static const struct mmc_host_ops omap_hsmmc_ops = {
1783 .enable = omap_hsmmc_enable_fclk,
1784 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001785 .post_req = omap_hsmmc_post_req,
1786 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001787 .request = omap_hsmmc_request,
1788 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789 .get_cd = omap_hsmmc_get_cd,
1790 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001791 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001792 /* NYET -- enable_sdio_irq */
1793};
1794
Denis Karpovd900f712009-09-22 16:44:38 -07001795#ifdef CONFIG_DEBUG_FS
1796
Denis Karpov70a33412009-09-22 16:44:59 -07001797static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001798{
1799 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001800 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001801 int context_loss = 0;
1802
Denis Karpov70a33412009-09-22 16:44:59 -07001803 if (host->pdata->get_context_loss_count)
1804 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001805
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001806 seq_printf(s, "mmc%d:\n"
1807 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001808 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001809 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001810 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001811 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001812 mmc->index, mmc->enabled ? 1 : 0,
1813 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001814 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001815
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301816 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001817 seq_printf(s, "host suspended, can't read registers\n");
1818 return 0;
1819 }
1820
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301821 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001822
1823 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1824 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1825 seq_printf(s, "CON:\t\t0x%08x\n",
1826 OMAP_HSMMC_READ(host->base, CON));
1827 seq_printf(s, "HCTL:\t\t0x%08x\n",
1828 OMAP_HSMMC_READ(host->base, HCTL));
1829 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1830 OMAP_HSMMC_READ(host->base, SYSCTL));
1831 seq_printf(s, "IE:\t\t0x%08x\n",
1832 OMAP_HSMMC_READ(host->base, IE));
1833 seq_printf(s, "ISE:\t\t0x%08x\n",
1834 OMAP_HSMMC_READ(host->base, ISE));
1835 seq_printf(s, "CAPA:\t\t0x%08x\n",
1836 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001837
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301838 pm_runtime_mark_last_busy(host->dev);
1839 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001840
Denis Karpovd900f712009-09-22 16:44:38 -07001841 return 0;
1842}
1843
Denis Karpov70a33412009-09-22 16:44:59 -07001844static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001845{
Denis Karpov70a33412009-09-22 16:44:59 -07001846 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001847}
1848
1849static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001850 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001851 .read = seq_read,
1852 .llseek = seq_lseek,
1853 .release = single_release,
1854};
1855
Denis Karpov70a33412009-09-22 16:44:59 -07001856static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001857{
1858 if (mmc->debugfs_root)
1859 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1860 mmc, &mmc_regs_fops);
1861}
1862
1863#else
1864
Denis Karpov70a33412009-09-22 16:44:59 -07001865static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001866{
1867}
1868
1869#endif
1870
Denis Karpov70a33412009-09-22 16:44:59 -07001871static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001872{
1873 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1874 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001875 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001876 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001877 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001878
1879 if (pdata == NULL) {
1880 dev_err(&pdev->dev, "Platform Data is missing\n");
1881 return -ENXIO;
1882 }
1883
1884 if (pdata->nr_slots == 0) {
1885 dev_err(&pdev->dev, "No Slots\n");
1886 return -ENXIO;
1887 }
1888
1889 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1890 irq = platform_get_irq(pdev, 0);
1891 if (res == NULL || irq < 0)
1892 return -ENXIO;
1893
kishore kadiyala91a0b082010-10-01 16:35:28 -07001894 res->start += pdata->reg_offset;
1895 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07001896 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001897 if (res == NULL)
1898 return -EBUSY;
1899
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001900 ret = omap_hsmmc_gpio_init(pdata);
1901 if (ret)
1902 goto err;
1903
Denis Karpov70a33412009-09-22 16:44:59 -07001904 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905 if (!mmc) {
1906 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001907 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001908 }
1909
1910 host = mmc_priv(mmc);
1911 host->mmc = mmc;
1912 host->pdata = pdata;
1913 host->dev = &pdev->dev;
1914 host->use_dma = 1;
1915 host->dev->dma_mask = &pdata->dma_mask;
1916 host->dma_ch = -1;
1917 host->irq = irq;
1918 host->id = pdev->id;
1919 host->slot_id = 0;
1920 host->mapbase = res->start;
1921 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001922 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001923 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001924
1925 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07001926 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001927
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301928 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001929
Adrian Huntere0eb2422010-02-15 10:03:34 -08001930 /*
1931 * If regulator_disable can only put vcc_aux to sleep then there is
1932 * no off state.
1933 */
1934 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1935 mmc_slot(host).no_off = 1;
1936
Andy Shevchenko6b206ef2011-07-13 11:16:29 -04001937 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1938 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001939
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001940 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941
Russell King6f7607c2009-01-28 10:22:50 +00001942 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001943 if (IS_ERR(host->fclk)) {
1944 ret = PTR_ERR(host->fclk);
1945 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001946 goto err1;
1947 }
1948
Denis Karpov70a33412009-09-22 16:44:59 -07001949 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001950
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001951 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001952
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301953 pm_runtime_enable(host->dev);
1954 pm_runtime_get_sync(host->dev);
1955 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1956 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001957
Adrian Hunter2bec0892009-09-22 16:45:02 -07001958 if (cpu_is_omap2430()) {
1959 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1960 /*
1961 * MMC can still work without debounce clock.
1962 */
1963 if (IS_ERR(host->dbclk))
1964 dev_warn(mmc_dev(host->mmc),
1965 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001967 host->got_dbclk = 1;
1968
1969 if (host->got_dbclk)
1970 if (clk_enable(host->dbclk) != 0)
1971 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1972 " clk failed\n");
1973 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001974
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001975 /* Since we do only SG emulation, we can have as many segs
1976 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001977 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001978
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1980 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1981 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1982 mmc->max_seg_size = mmc->max_req_size;
1983
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001984 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001985 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001987 mmc->caps |= mmc_slot(host).caps;
1988 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989 mmc->caps |= MMC_CAP_4_BIT_DATA;
1990
Denis Karpov191d1f12009-09-22 16:44:55 -07001991 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001992 mmc->caps |= MMC_CAP_NONREMOVABLE;
1993
Denis Karpov70a33412009-09-22 16:44:59 -07001994 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001996 /* Select DMA lines */
1997 switch (host->id) {
1998 case OMAP_MMC1_DEVID:
1999 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2000 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2001 break;
2002 case OMAP_MMC2_DEVID:
2003 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2004 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2005 break;
2006 case OMAP_MMC3_DEVID:
2007 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2008 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2009 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002010 case OMAP_MMC4_DEVID:
2011 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2012 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2013 break;
2014 case OMAP_MMC5_DEVID:
2015 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2016 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2017 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002018 default:
2019 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2020 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 }
2022
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002024 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025 mmc_hostname(mmc), host);
2026 if (ret) {
2027 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2028 goto err_irq;
2029 }
2030
2031 if (pdata->init != NULL) {
2032 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002033 dev_dbg(mmc_dev(host->mmc),
2034 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 goto err_irq_cd_init;
2036 }
2037 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002038
Adrian Hunterb702b102010-02-15 10:03:35 -08002039 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002040 ret = omap_hsmmc_reg_get(host);
2041 if (ret)
2042 goto err_reg;
2043 host->use_reg = 1;
2044 }
2045
David Brownellb583f262009-05-28 14:04:03 -07002046 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047
2048 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002049 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002050 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002051 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2053 | IRQF_DISABLED,
2054 mmc_hostname(mmc), host);
2055 if (ret) {
2056 dev_dbg(mmc_dev(host->mmc),
2057 "Unable to grab MMC CD IRQ\n");
2058 goto err_irq_cd;
2059 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002060 pdata->suspend = omap_hsmmc_suspend_cdirq;
2061 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062 }
2063
Adrian Hunterb4175772010-05-26 14:42:06 -07002064 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002065
Adrian Hunterb62f6222009-09-22 16:45:01 -07002066 omap_hsmmc_protect_card(host);
2067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002068 mmc_add_host(mmc);
2069
Denis Karpov191d1f12009-09-22 16:44:55 -07002070 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2072 if (ret < 0)
2073 goto err_slot_name;
2074 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002075 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002076 ret = device_create_file(&mmc->class_dev,
2077 &dev_attr_cover_switch);
2078 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002079 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080 }
2081
Denis Karpov70a33412009-09-22 16:44:59 -07002082 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302083 pm_runtime_mark_last_busy(host->dev);
2084 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002085
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086 return 0;
2087
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088err_slot_name:
2089 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002090 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002091err_irq_cd:
2092 if (host->use_reg)
2093 omap_hsmmc_reg_put(host);
2094err_reg:
2095 if (host->pdata->cleanup)
2096 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097err_irq_cd_init:
2098 free_irq(host->irq, host);
2099err_irq:
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302100 pm_runtime_mark_last_busy(host->dev);
2101 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002103 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104 clk_disable(host->dbclk);
2105 clk_put(host->dbclk);
2106 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107err1:
2108 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002109 platform_set_drvdata(pdev, NULL);
2110 mmc_free_host(mmc);
2111err_alloc:
2112 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113err:
Chris Ball984b2032011-03-22 16:34:42 -07002114 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115 return ret;
2116}
2117
Denis Karpov70a33412009-09-22 16:44:59 -07002118static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119{
Denis Karpov70a33412009-09-22 16:44:59 -07002120 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121 struct resource *res;
2122
2123 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302124 pm_runtime_get_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002125 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002126 if (host->use_reg)
2127 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002128 if (host->pdata->cleanup)
2129 host->pdata->cleanup(&pdev->dev);
2130 free_irq(host->irq, host);
2131 if (mmc_slot(host).card_detect_irq)
2132 free_irq(mmc_slot(host).card_detect_irq, host);
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002133 flush_work_sync(&host->mmc_carddetect_work);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302135 pm_runtime_put_sync(host->dev);
2136 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002138 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139 clk_disable(host->dbclk);
2140 clk_put(host->dbclk);
2141 }
2142
2143 mmc_free_host(host->mmc);
2144 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002145 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 }
2147
2148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2149 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002150 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002151 platform_set_drvdata(pdev, NULL);
2152
2153 return 0;
2154}
2155
2156#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002157static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158{
2159 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002160 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002161 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162
2163 if (host && host->suspended)
2164 return 0;
2165
2166 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302167 pm_runtime_get_sync(host->dev);
Adrian Huntera6b22402009-09-22 16:44:45 -07002168 host->suspended = 1;
2169 if (host->pdata->suspend) {
2170 ret = host->pdata->suspend(&pdev->dev,
2171 host->slot_id);
2172 if (ret) {
2173 dev_dbg(mmc_dev(host->mmc),
2174 "Unable to handle MMC board"
2175 " level suspend\n");
2176 host->suspended = 0;
2177 return ret;
2178 }
2179 }
2180 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002181 ret = mmc_suspend_host(host->mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002184 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002185 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002186 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002187 if (host->got_dbclk)
2188 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002189 } else {
2190 host->suspended = 0;
2191 if (host->pdata->resume) {
2192 ret = host->pdata->resume(&pdev->dev,
2193 host->slot_id);
2194 if (ret)
2195 dev_dbg(mmc_dev(host->mmc),
2196 "Unmask interrupt failed\n");
2197 }
Adrian Huntera6b22402009-09-22 16:44:45 -07002198 }
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302199 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002200 }
2201 return ret;
2202}
2203
2204/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002205static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206{
2207 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002208 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002209 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210
2211 if (host && !host->suspended)
2212 return 0;
2213
2214 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302215 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002216
Adrian Hunter2bec0892009-09-22 16:45:02 -07002217 if (host->got_dbclk)
2218 clk_enable(host->dbclk);
2219
Denis Karpov70a33412009-09-22 16:44:59 -07002220 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002221
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222 if (host->pdata->resume) {
2223 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2224 if (ret)
2225 dev_dbg(mmc_dev(host->mmc),
2226 "Unmask interrupt failed\n");
2227 }
2228
Adrian Hunterb62f6222009-09-22 16:45:01 -07002229 omap_hsmmc_protect_card(host);
2230
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 /* Notify the core to resume the host */
2232 ret = mmc_resume_host(host->mmc);
2233 if (ret == 0)
2234 host->suspended = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302235
2236 pm_runtime_mark_last_busy(host->dev);
2237 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002238 }
2239
2240 return ret;
2241
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242}
2243
2244#else
Denis Karpov70a33412009-09-22 16:44:59 -07002245#define omap_hsmmc_suspend NULL
2246#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002247#endif
2248
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302249static int omap_hsmmc_runtime_suspend(struct device *dev)
2250{
2251 struct omap_hsmmc_host *host;
2252
2253 host = platform_get_drvdata(to_platform_device(dev));
2254 omap_hsmmc_context_save(host);
2255 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2256
2257 return 0;
2258}
2259
2260static int omap_hsmmc_runtime_resume(struct device *dev)
2261{
2262 struct omap_hsmmc_host *host;
2263
2264 host = platform_get_drvdata(to_platform_device(dev));
2265 omap_hsmmc_context_restore(host);
2266 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2267
2268 return 0;
2269}
2270
Kevin Hilmana791daa2010-05-26 14:42:07 -07002271static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002272 .suspend = omap_hsmmc_suspend,
2273 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302274 .runtime_suspend = omap_hsmmc_runtime_suspend,
2275 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002276};
2277
2278static struct platform_driver omap_hsmmc_driver = {
2279 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002280 .driver = {
2281 .name = DRIVER_NAME,
2282 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002283 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002284 },
2285};
2286
Denis Karpov70a33412009-09-22 16:44:59 -07002287static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002288{
2289 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002290 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002291}
2292
Denis Karpov70a33412009-09-22 16:44:59 -07002293static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002294{
2295 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002296 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002297}
2298
Denis Karpov70a33412009-09-22 16:44:59 -07002299module_init(omap_hsmmc_init);
2300module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002301
2302MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2303MODULE_LICENSE("GPL");
2304MODULE_ALIAS("platform:" DRIVER_NAME);
2305MODULE_AUTHOR("Texas Instruments Inc");