blob: a20c38385d2a6c16502e4ae46b5599f368497f72 [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>
30#include <linux/io.h>
31#include <linux/semaphore.h>
32#include <mach/dma.h>
33#include <mach/hardware.h>
34#include <mach/board.h>
35#include <mach/mmc.h>
36#include <mach/cpu.h>
37
38/* OMAP HSMMC Host Controller Registers */
39#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070040#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041#define OMAP_HSMMC_CON 0x002C
42#define OMAP_HSMMC_BLK 0x0104
43#define OMAP_HSMMC_ARG 0x0108
44#define OMAP_HSMMC_CMD 0x010C
45#define OMAP_HSMMC_RSP10 0x0110
46#define OMAP_HSMMC_RSP32 0x0114
47#define OMAP_HSMMC_RSP54 0x0118
48#define OMAP_HSMMC_RSP76 0x011C
49#define OMAP_HSMMC_DATA 0x0120
50#define OMAP_HSMMC_HCTL 0x0128
51#define OMAP_HSMMC_SYSCTL 0x012C
52#define OMAP_HSMMC_STAT 0x0130
53#define OMAP_HSMMC_IE 0x0134
54#define OMAP_HSMMC_ISE 0x0138
55#define OMAP_HSMMC_CAPA 0x0140
56
57#define VS18 (1 << 26)
58#define VS30 (1 << 25)
59#define SDVS18 (0x5 << 9)
60#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080061#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010062#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010063#define SDVSCLR 0xFFFFF1FF
64#define SDVSDET 0x00000400
65#define AUTOIDLE 0x1
66#define SDBP (1 << 8)
67#define DTO 0xe
68#define ICE 0x1
69#define ICS 0x2
70#define CEN (1 << 2)
71#define CLKD_MASK 0x0000FFC0
72#define CLKD_SHIFT 6
73#define DTO_MASK 0x000F0000
74#define DTO_SHIFT 16
75#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070076#define BWR_ENABLE (1 << 4)
77#define BRR_ENABLE (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010078#define INIT_STREAM (1 << 1)
79#define DP_SELECT (1 << 21)
80#define DDIR (1 << 4)
81#define DMA_EN 0x1
82#define MSBS (1 << 5)
83#define BCE (1 << 1)
84#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020085#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define CC 0x1
87#define TC 0x02
88#define OD 0x1
89#define ERR (1 << 15)
90#define CMD_TIMEOUT (1 << 16)
91#define DATA_TIMEOUT (1 << 20)
92#define CMD_CRC (1 << 17)
93#define DATA_CRC (1 << 21)
94#define CARD_ERR (1 << 28)
95#define STAT_CLEAR 0xFFFFFFFF
96#define INIT_STREAM_CMD 0x00000000
97#define DUAL_VOLT_OCR_BIT 7
98#define SRC (1 << 25)
99#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700100#define SOFTRESET (1 << 1)
101#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100102
103/*
104 * FIXME: Most likely all the data using these _DEVID defines should come
105 * from the platform_data, or implemented in controller and slot specific
106 * functions.
107 */
108#define OMAP_MMC1_DEVID 0
109#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000110#define OMAP_MMC3_DEVID 2
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100112#define MMC_TIMEOUT_MS 20
113#define OMAP_MMC_MASTER_CLOCK 96000000
114#define DRIVER_NAME "mmci-omap-hs"
115
116/*
117 * One controller can have multiple slots, like on some omap boards using
118 * omap.c controller driver. Luckily this is not currently done on any known
119 * omap_hsmmc.c device.
120 */
121#define mmc_slot(host) (host->pdata->slots[host->slot_id])
122
123/*
124 * MMC Host controller read/write API's
125 */
126#define OMAP_HSMMC_READ(base, reg) \
127 __raw_readl((base) + OMAP_HSMMC_##reg)
128
129#define OMAP_HSMMC_WRITE(base, reg, val) \
130 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
131
132struct mmc_omap_host {
133 struct device *dev;
134 struct mmc_host *mmc;
135 struct mmc_request *mrq;
136 struct mmc_command *cmd;
137 struct mmc_data *data;
138 struct clk *fclk;
139 struct clk *iclk;
140 struct clk *dbclk;
141 struct semaphore sem;
142 struct work_struct mmc_carddetect_work;
143 void __iomem *base;
144 resource_size_t mapbase;
145 unsigned int id;
146 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200147 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100148 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700149 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150 u32 *buffer;
151 u32 bytesleft;
152 int suspended;
153 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000155 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 int slot_id;
157 int dbclk_enabled;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200158 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700159 int context_loss;
160
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161 struct omap_mmc_platform_data *pdata;
162};
163
164/*
165 * Stop clock to the card
166 */
167static void omap_mmc_stop_clock(struct mmc_omap_host *host)
168{
169 OMAP_HSMMC_WRITE(host->base, SYSCTL,
170 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
171 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
172 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
173}
174
Denis Karpov11dd62a2009-09-22 16:44:43 -0700175#ifdef CONFIG_PM
176
177/*
178 * Restore the MMC host context, if it was lost as result of a
179 * power state change.
180 */
181static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
182{
183 struct mmc_ios *ios = &host->mmc->ios;
184 struct omap_mmc_platform_data *pdata = host->pdata;
185 int context_loss = 0;
186 u32 hctl, capa, con;
187 u16 dsor = 0;
188 unsigned long timeout;
189
190 if (pdata->get_context_loss_count) {
191 context_loss = pdata->get_context_loss_count(host->dev);
192 if (context_loss < 0)
193 return 1;
194 }
195
196 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
197 context_loss == host->context_loss ? "not " : "");
198 if (host->context_loss == context_loss)
199 return 1;
200
201 /* Wait for hardware reset */
202 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
203 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
204 && time_before(jiffies, timeout))
205 ;
206
207 /* Do software reset */
208 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
209 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
210 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
211 && time_before(jiffies, timeout))
212 ;
213
214 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
215 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
216
217 if (host->id == OMAP_MMC1_DEVID) {
218 if (host->power_mode != MMC_POWER_OFF &&
219 (1 << ios->vdd) <= MMC_VDD_23_24)
220 hctl = SDVS18;
221 else
222 hctl = SDVS30;
223 capa = VS30 | VS18;
224 } else {
225 hctl = SDVS18;
226 capa = VS18;
227 }
228
229 OMAP_HSMMC_WRITE(host->base, HCTL,
230 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
231
232 OMAP_HSMMC_WRITE(host->base, CAPA,
233 OMAP_HSMMC_READ(host->base, CAPA) | capa);
234
235 OMAP_HSMMC_WRITE(host->base, HCTL,
236 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
237
238 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
239 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
240 && time_before(jiffies, timeout))
241 ;
242
243 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
244 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
245 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
246
247 /* Do not initialize card-specific things if the power is off */
248 if (host->power_mode == MMC_POWER_OFF)
249 goto out;
250
251 con = OMAP_HSMMC_READ(host->base, CON);
252 switch (ios->bus_width) {
253 case MMC_BUS_WIDTH_8:
254 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
255 break;
256 case MMC_BUS_WIDTH_4:
257 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
258 OMAP_HSMMC_WRITE(host->base, HCTL,
259 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
260 break;
261 case MMC_BUS_WIDTH_1:
262 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
263 OMAP_HSMMC_WRITE(host->base, HCTL,
264 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
265 break;
266 }
267
268 if (ios->clock) {
269 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
270 if (dsor < 1)
271 dsor = 1;
272
273 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
274 dsor++;
275
276 if (dsor > 250)
277 dsor = 250;
278 }
279
280 OMAP_HSMMC_WRITE(host->base, SYSCTL,
281 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
282 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
283 OMAP_HSMMC_WRITE(host->base, SYSCTL,
284 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
285
286 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
287 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
288 && time_before(jiffies, timeout))
289 ;
290
291 OMAP_HSMMC_WRITE(host->base, SYSCTL,
292 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
293
294 con = OMAP_HSMMC_READ(host->base, CON);
295 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
296 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
297 else
298 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
299out:
300 host->context_loss = context_loss;
301
302 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
303 return 0;
304}
305
306/*
307 * Save the MMC host context (store the number of power state changes so far).
308 */
309static void omap_mmc_save_ctx(struct mmc_omap_host *host)
310{
311 struct omap_mmc_platform_data *pdata = host->pdata;
312 int context_loss;
313
314 if (pdata->get_context_loss_count) {
315 context_loss = pdata->get_context_loss_count(host->dev);
316 if (context_loss < 0)
317 return;
318 host->context_loss = context_loss;
319 }
320}
321
322#else
323
324static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
325{
326 return 0;
327}
328
329static void omap_mmc_save_ctx(struct mmc_omap_host *host)
330{
331}
332
333#endif
334
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100335/*
336 * Send init stream sequence to card
337 * before sending IDLE command
338 */
339static void send_init_stream(struct mmc_omap_host *host)
340{
341 int reg = 0;
342 unsigned long timeout;
343
344 disable_irq(host->irq);
345 OMAP_HSMMC_WRITE(host->base, CON,
346 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
347 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
348
349 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
350 while ((reg != CC) && time_before(jiffies, timeout))
351 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
352
353 OMAP_HSMMC_WRITE(host->base, CON,
354 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
355 enable_irq(host->irq);
356}
357
358static inline
359int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
360{
361 int r = 1;
362
363 if (host->pdata->slots[host->slot_id].get_cover_state)
364 r = host->pdata->slots[host->slot_id].get_cover_state(host->dev,
365 host->slot_id);
366 return r;
367}
368
369static ssize_t
370mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
373 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
374 struct mmc_omap_host *host = mmc_priv(mmc);
375
376 return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
377 "open");
378}
379
380static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
381
382static ssize_t
383mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
384 char *buf)
385{
386 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
387 struct mmc_omap_host *host = mmc_priv(mmc);
388 struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
389
Adrian Huntere68fdab2009-01-30 10:59:31 +0200390 return sprintf(buf, "%s\n", slot.name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100391}
392
393static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
394
395/*
396 * Configure the response type and send the cmd.
397 */
398static void
399mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
400 struct mmc_data *data)
401{
402 int cmdreg = 0, resptype = 0, cmdtype = 0;
403
404 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
405 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
406 host->cmd = cmd;
407
408 /*
409 * Clear status bits and enable interrupts
410 */
411 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
412 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -0700413
414 if (host->use_dma)
415 OMAP_HSMMC_WRITE(host->base, IE,
416 INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
417 else
418 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100419
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200420 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100421 if (cmd->flags & MMC_RSP_PRESENT) {
422 if (cmd->flags & MMC_RSP_136)
423 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200424 else if (cmd->flags & MMC_RSP_BUSY) {
425 resptype = 3;
426 host->response_busy = 1;
427 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100428 resptype = 2;
429 }
430
431 /*
432 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
433 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
434 * a val of 0x3, rest 0x0.
435 */
436 if (cmd == host->mrq->stop)
437 cmdtype = 0x3;
438
439 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
440
441 if (data) {
442 cmdreg |= DP_SELECT | MSBS | BCE;
443 if (data->flags & MMC_DATA_READ)
444 cmdreg |= DDIR;
445 else
446 cmdreg &= ~(DDIR);
447 }
448
449 if (host->use_dma)
450 cmdreg |= DMA_EN;
451
452 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
453 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
454}
455
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200456static int
457mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
458{
459 if (data->flags & MMC_DATA_WRITE)
460 return DMA_TO_DEVICE;
461 else
462 return DMA_FROM_DEVICE;
463}
464
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100465/*
466 * Notify the transfer complete to MMC core
467 */
468static void
469mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
470{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200471 if (!data) {
472 struct mmc_request *mrq = host->mrq;
473
474 host->mrq = NULL;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200475 mmc_request_done(host->mmc, mrq);
476 return;
477 }
478
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100479 host->data = NULL;
480
481 if (host->use_dma && host->dma_ch != -1)
482 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200483 mmc_omap_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100484
485 if (!data->error)
486 data->bytes_xfered += data->blocks * (data->blksz);
487 else
488 data->bytes_xfered = 0;
489
490 if (!data->stop) {
491 host->mrq = NULL;
492 mmc_request_done(host->mmc, data->mrq);
493 return;
494 }
495 mmc_omap_start_command(host, data->stop, NULL);
496}
497
498/*
499 * Notify the core about command completion
500 */
501static void
502mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
503{
504 host->cmd = NULL;
505
506 if (cmd->flags & MMC_RSP_PRESENT) {
507 if (cmd->flags & MMC_RSP_136) {
508 /* response type 2 */
509 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
510 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
511 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
512 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
513 } else {
514 /* response types 1, 1b, 3, 4, 5, 6 */
515 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
516 }
517 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200518 if ((host->data == NULL && !host->response_busy) || cmd->error) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100519 host->mrq = NULL;
520 mmc_request_done(host->mmc, cmd->mrq);
521 }
522}
523
524/*
525 * DMA clean up for command errors
526 */
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200527static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100528{
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200529 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100530
531 if (host->use_dma && host->dma_ch != -1) {
532 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200533 mmc_omap_get_dma_dir(host, host->data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100534 omap_free_dma(host->dma_ch);
535 host->dma_ch = -1;
536 up(&host->sem);
537 }
538 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100539}
540
541/*
542 * Readable error output
543 */
544#ifdef CONFIG_MMC_DEBUG
545static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
546{
547 /* --- means reserved bit without definition at documentation */
548 static const char *mmc_omap_status_bits[] = {
549 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
550 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
551 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
552 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
553 };
554 char res[256];
555 char *buf = res;
556 int len, i;
557
558 len = sprintf(buf, "MMC IRQ 0x%x :", status);
559 buf += len;
560
561 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
562 if (status & (1 << i)) {
563 len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
564 buf += len;
565 }
566
567 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
568}
569#endif /* CONFIG_MMC_DEBUG */
570
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100571/*
572 * MMC controller internal state machines reset
573 *
574 * Used to reset command or data internal state machines, using respectively
575 * SRC or SRD bit of SYSCTL register
576 * Can be called from interrupt context
577 */
578static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
579 unsigned long bit)
580{
581 unsigned long i = 0;
582 unsigned long limit = (loops_per_jiffy *
583 msecs_to_jiffies(MMC_TIMEOUT_MS));
584
585 OMAP_HSMMC_WRITE(host->base, SYSCTL,
586 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
587
588 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
589 (i++ < limit))
590 cpu_relax();
591
592 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
593 dev_err(mmc_dev(host->mmc),
594 "Timeout waiting on controller reset in %s\n",
595 __func__);
596}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100597
598/*
599 * MMC controller IRQ handler
600 */
601static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
602{
603 struct mmc_omap_host *host = dev_id;
604 struct mmc_data *data;
605 int end_cmd = 0, end_trans = 0, status;
606
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200607 if (host->mrq == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100608 OMAP_HSMMC_WRITE(host->base, STAT,
609 OMAP_HSMMC_READ(host->base, STAT));
Kevin Hilman00adadc2009-04-06 15:01:19 +0300610 /* Flush posted write */
611 OMAP_HSMMC_READ(host->base, STAT);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100612 return IRQ_HANDLED;
613 }
614
615 data = host->data;
616 status = OMAP_HSMMC_READ(host->base, STAT);
617 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
618
619 if (status & ERR) {
620#ifdef CONFIG_MMC_DEBUG
621 mmc_omap_report_irq(host, status);
622#endif
623 if ((status & CMD_TIMEOUT) ||
624 (status & CMD_CRC)) {
625 if (host->cmd) {
626 if (status & CMD_TIMEOUT) {
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100627 mmc_omap_reset_controller_fsm(host, SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100628 host->cmd->error = -ETIMEDOUT;
629 } else {
630 host->cmd->error = -EILSEQ;
631 }
632 end_cmd = 1;
633 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200634 if (host->data || host->response_busy) {
635 if (host->data)
636 mmc_dma_cleanup(host, -ETIMEDOUT);
637 host->response_busy = 0;
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100638 mmc_omap_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800639 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100640 }
641 if ((status & DATA_TIMEOUT) ||
642 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200643 if (host->data || host->response_busy) {
644 int err = (status & DATA_TIMEOUT) ?
645 -ETIMEDOUT : -EILSEQ;
646
647 if (host->data)
648 mmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100649 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200650 host->mrq->cmd->error = err;
651 host->response_busy = 0;
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100652 mmc_omap_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100653 end_trans = 1;
654 }
655 }
656 if (status & CARD_ERR) {
657 dev_dbg(mmc_dev(host->mmc),
658 "Ignoring card err CMD%d\n", host->cmd->opcode);
659 if (host->cmd)
660 end_cmd = 1;
661 if (host->data)
662 end_trans = 1;
663 }
664 }
665
666 OMAP_HSMMC_WRITE(host->base, STAT, status);
Kevin Hilman00adadc2009-04-06 15:01:19 +0300667 /* Flush posted write */
668 OMAP_HSMMC_READ(host->base, STAT);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100669
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +0300670 if (end_cmd || ((status & CC) && host->cmd))
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100671 mmc_omap_cmd_done(host, host->cmd);
672 if (end_trans || (status & TC))
673 mmc_omap_xfer_done(host, data);
674
675 return IRQ_HANDLED;
676}
677
Adrian Huntere13bb302009-03-12 17:08:26 +0200678static void set_sd_bus_power(struct mmc_omap_host *host)
679{
680 unsigned long i;
681
682 OMAP_HSMMC_WRITE(host->base, HCTL,
683 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
684 for (i = 0; i < loops_per_jiffy; i++) {
685 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
686 break;
687 cpu_relax();
688 }
689}
690
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100691/*
David Brownelleb250822009-02-17 14:49:01 -0800692 * Switch MMC interface voltage ... only relevant for MMC1.
693 *
694 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
695 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
696 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100697 */
698static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
699{
700 u32 reg_val = 0;
701 int ret;
702
703 /* Disable the clocks */
704 clk_disable(host->fclk);
705 clk_disable(host->iclk);
706 clk_disable(host->dbclk);
707
708 /* Turn the power off */
709 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
710 if (ret != 0)
711 goto err;
712
713 /* Turn the power ON with given VDD 1.8 or 3.0v */
714 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
715 if (ret != 0)
716 goto err;
717
718 clk_enable(host->fclk);
719 clk_enable(host->iclk);
720 clk_enable(host->dbclk);
721
722 OMAP_HSMMC_WRITE(host->base, HCTL,
723 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
724 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -0800725
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726 /*
727 * If a MMC dual voltage card is detected, the set_ios fn calls
728 * this fn with VDD bit set for 1.8V. Upon card removal from the
729 * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
730 *
David Brownelleb250822009-02-17 14:49:01 -0800731 * Cope with a bit of slop in the range ... per data sheets:
732 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
733 * but recommended values are 1.71V to 1.89V
734 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
735 * but recommended values are 2.7V to 3.3V
736 *
737 * Board setup code shouldn't permit anything very out-of-range.
738 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
739 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740 */
David Brownelleb250822009-02-17 14:49:01 -0800741 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100742 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -0800743 else
744 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745
746 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +0200747 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748
749 return 0;
750err:
751 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
752 return ret;
753}
754
755/*
756 * Work Item to notify the core about card insertion/removal
757 */
758static void mmc_omap_detect(struct work_struct *work)
759{
760 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
761 mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -0800762 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -0700763 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -0800764
Adrian Huntera6b22402009-09-22 16:44:45 -0700765 if (host->suspended)
766 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100767
768 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -0700769
770 if (mmc_slot(host).card_detect)
771 carddetect = slot->card_detect(slot->card_detect_irq);
772 else
773 carddetect = -ENOSYS;
774
775 if (carddetect) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
777 } else {
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700778 mmc_host_enable(host->mmc);
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100779 mmc_omap_reset_controller_fsm(host, SRD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700780 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
782 }
783}
784
785/*
786 * ISR for handling card insertion and removal
787 */
788static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
789{
790 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
791
Adrian Huntera6b22402009-09-22 16:44:45 -0700792 if (host->suspended)
793 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100794 schedule_work(&host->mmc_carddetect_work);
795
796 return IRQ_HANDLED;
797}
798
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200799static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
800 struct mmc_data *data)
801{
802 int sync_dev;
803
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000804 if (data->flags & MMC_DATA_WRITE)
805 sync_dev = host->dma_line_tx;
806 else
807 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200808 return sync_dev;
809}
810
811static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
812 struct mmc_data *data,
813 struct scatterlist *sgl)
814{
815 int blksz, nblk, dma_ch;
816
817 dma_ch = host->dma_ch;
818 if (data->flags & MMC_DATA_WRITE) {
819 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
820 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
821 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
822 sg_dma_address(sgl), 0, 0);
823 } else {
824 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
825 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
826 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
827 sg_dma_address(sgl), 0, 0);
828 }
829
830 blksz = host->data->blksz;
831 nblk = sg_dma_len(sgl) / blksz;
832
833 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
834 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
835 mmc_omap_get_dma_sync_dev(host, data),
836 !(data->flags & MMC_DATA_WRITE));
837
838 omap_start_dma(dma_ch);
839}
840
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100841/*
842 * DMA call back function
843 */
844static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
845{
846 struct mmc_omap_host *host = data;
847
848 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
849 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
850
851 if (host->dma_ch < 0)
852 return;
853
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200854 host->dma_sg_idx++;
855 if (host->dma_sg_idx < host->dma_len) {
856 /* Fire up the next transfer. */
857 mmc_omap_config_dma_params(host, host->data,
858 host->data->sg + host->dma_sg_idx);
859 return;
860 }
861
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100862 omap_free_dma(host->dma_ch);
863 host->dma_ch = -1;
864 /*
865 * DMA Callback: run in interrupt context.
Anand Gadiyar85b84322009-04-15 17:44:58 +0530866 * mutex_unlock will throw a kernel warning if used.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867 */
868 up(&host->sem);
869}
870
871/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100872 * Routine to configure and start DMA for the MMC card
873 */
874static int
875mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
876{
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200877 int dma_ch = 0, ret = 0, err = 1, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878 struct mmc_data *data = req->data;
879
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200880 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -0700881 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200882 struct scatterlist *sgl;
883
884 sgl = data->sg + i;
885 if (sgl->length % data->blksz)
886 return -EINVAL;
887 }
888 if ((data->blksz % 4) != 0)
889 /* REVISIT: The MMC buffer increments only when MSB is written.
890 * Return error for blksz which is non multiple of four.
891 */
892 return -EINVAL;
893
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100894 /*
895 * If for some reason the DMA transfer is still active,
896 * we wait for timeout period and free the dma
897 */
898 if (host->dma_ch != -1) {
899 set_current_state(TASK_UNINTERRUPTIBLE);
900 schedule_timeout(100);
901 if (down_trylock(&host->sem)) {
902 omap_free_dma(host->dma_ch);
903 host->dma_ch = -1;
904 up(&host->sem);
905 return err;
906 }
907 } else {
908 if (down_trylock(&host->sem))
909 return err;
910 }
911
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200912 ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
913 mmc_omap_dma_cb,host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200915 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100916 "%s: omap_request_dma() failed with %d\n",
917 mmc_hostname(host->mmc), ret);
918 return ret;
919 }
920
921 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200922 data->sg_len, mmc_omap_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100923 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200924 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100925
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200926 mmc_omap_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100928 return 0;
929}
930
931static void set_data_timeout(struct mmc_omap_host *host,
932 struct mmc_request *req)
933{
934 unsigned int timeout, cycle_ns;
935 uint32_t reg, clkd, dto = 0;
936
937 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
938 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
939 if (clkd == 0)
940 clkd = 1;
941
942 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
943 timeout = req->data->timeout_ns / cycle_ns;
944 timeout += req->data->timeout_clks;
945 if (timeout) {
946 while ((timeout & 0x80000000) == 0) {
947 dto += 1;
948 timeout <<= 1;
949 }
950 dto = 31 - dto;
951 timeout <<= 1;
952 if (timeout && dto)
953 dto += 1;
954 if (dto >= 13)
955 dto -= 13;
956 else
957 dto = 0;
958 if (dto > 14)
959 dto = 14;
960 }
961
962 reg &= ~DTO_MASK;
963 reg |= dto << DTO_SHIFT;
964 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
965}
966
967/*
968 * Configure block length for MMC/SD cards and initiate the transfer.
969 */
970static int
971mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
972{
973 int ret;
974 host->data = req->data;
975
976 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977 OMAP_HSMMC_WRITE(host->base, BLK, 0);
978 return 0;
979 }
980
981 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
982 | (req->data->blocks << 16));
983 set_data_timeout(host, req);
984
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985 if (host->use_dma) {
986 ret = mmc_omap_start_dma_transfer(host, req);
987 if (ret != 0) {
988 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
989 return ret;
990 }
991 }
992 return 0;
993}
994
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700995static int omap_mmc_enable(struct mmc_host *mmc)
996{
997 struct mmc_omap_host *host = mmc_priv(mmc);
998 int err;
999
1000 err = clk_enable(host->fclk);
1001 if (err)
1002 return err;
1003 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov11dd62a2009-09-22 16:44:43 -07001004 omap_mmc_restore_ctx(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001005 return 0;
1006}
1007
1008static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
1009{
1010 struct mmc_omap_host *host = mmc_priv(mmc);
1011
Denis Karpov11dd62a2009-09-22 16:44:43 -07001012 omap_mmc_save_ctx(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001013 clk_disable(host->fclk);
1014 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1015 return 0;
1016}
1017
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001018/*
1019 * Request function. for read/write operation
1020 */
1021static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1022{
1023 struct mmc_omap_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001024 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001025
1026 WARN_ON(host->mrq != NULL);
1027 host->mrq = req;
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001028 err = mmc_omap_prepare_data(host, req);
1029 if (err) {
1030 req->cmd->error = err;
1031 if (req->data)
1032 req->data->error = err;
1033 host->mrq = NULL;
1034 mmc_request_done(mmc, req);
1035 return;
1036 }
1037
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001038 mmc_omap_start_command(host, req->cmd, req->data);
1039}
1040
1041
1042/* Routine to configure clock values. Exposed API to core */
1043static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1044{
1045 struct mmc_omap_host *host = mmc_priv(mmc);
1046 u16 dsor = 0;
1047 unsigned long regval;
1048 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001049 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001050 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001052 mmc_host_enable(host->mmc);
1053
Adrian Huntera3621462009-09-22 16:44:42 -07001054 if (ios->power_mode != host->power_mode) {
1055 switch (ios->power_mode) {
1056 case MMC_POWER_OFF:
1057 mmc_slot(host).set_power(host->dev, host->slot_id,
1058 0, 0);
1059 break;
1060 case MMC_POWER_UP:
1061 mmc_slot(host).set_power(host->dev, host->slot_id,
1062 1, ios->vdd);
1063 break;
1064 case MMC_POWER_ON:
1065 do_send_init_stream = 1;
1066 break;
1067 }
1068 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001069 }
1070
Jarkko Lavinen73153012008-11-21 16:49:54 +02001071 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001073 case MMC_BUS_WIDTH_8:
1074 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1075 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001076 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001077 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001078 OMAP_HSMMC_WRITE(host->base, HCTL,
1079 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1080 break;
1081 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001082 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001083 OMAP_HSMMC_WRITE(host->base, HCTL,
1084 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1085 break;
1086 }
1087
1088 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001089 /* Only MMC1 can interface at 3V without some flavor
1090 * of external transceiver; but they all handle 1.8V.
1091 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1093 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1094 /*
1095 * The mmc_select_voltage fn of the core does
1096 * not seem to set the power_mode to
1097 * MMC_POWER_UP upon recalculating the voltage.
1098 * vdd 1.8v.
1099 */
1100 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
1101 dev_dbg(mmc_dev(host->mmc),
1102 "Switch operation failed\n");
1103 }
1104 }
1105
1106 if (ios->clock) {
1107 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1108 if (dsor < 1)
1109 dsor = 1;
1110
1111 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1112 dsor++;
1113
1114 if (dsor > 250)
1115 dsor = 250;
1116 }
1117 omap_mmc_stop_clock(host);
1118 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1119 regval = regval & ~(CLKD_MASK);
1120 regval = regval | (dsor << 6) | (DTO << 16);
1121 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1122 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1123 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1124
1125 /* Wait till the ICS bit is set */
1126 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001127 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128 && time_before(jiffies, timeout))
1129 msleep(1);
1130
1131 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1132 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1133
Adrian Huntera3621462009-09-22 16:44:42 -07001134 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 send_init_stream(host);
1136
Denis Karpovabb28e72009-09-22 16:44:44 -07001137 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001139 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1140 else
1141 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001142
1143 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144}
1145
1146static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1147{
1148 struct mmc_omap_host *host = mmc_priv(mmc);
1149 struct omap_mmc_platform_data *pdata = host->pdata;
1150
1151 if (!pdata->slots[0].card_detect)
1152 return -ENOSYS;
1153 return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
1154}
1155
1156static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1157{
1158 struct mmc_omap_host *host = mmc_priv(mmc);
1159 struct omap_mmc_platform_data *pdata = host->pdata;
1160
1161 if (!pdata->slots[0].get_ro)
1162 return -ENOSYS;
1163 return pdata->slots[0].get_ro(host->dev, 0);
1164}
1165
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001166static void omap_hsmmc_init(struct mmc_omap_host *host)
1167{
1168 u32 hctl, capa, value;
1169
1170 /* Only MMC1 supports 3.0V */
1171 if (host->id == OMAP_MMC1_DEVID) {
1172 hctl = SDVS30;
1173 capa = VS30 | VS18;
1174 } else {
1175 hctl = SDVS18;
1176 capa = VS18;
1177 }
1178
1179 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1180 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1181
1182 value = OMAP_HSMMC_READ(host->base, CAPA);
1183 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1184
1185 /* Set the controller to AUTO IDLE mode */
1186 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1187 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1188
1189 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001190 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001191}
1192
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193static struct mmc_host_ops mmc_omap_ops = {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001194 .enable = omap_mmc_enable,
1195 .disable = omap_mmc_disable,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 .request = omap_mmc_request,
1197 .set_ios = omap_mmc_set_ios,
1198 .get_cd = omap_hsmmc_get_cd,
1199 .get_ro = omap_hsmmc_get_ro,
1200 /* NYET -- enable_sdio_irq */
1201};
1202
Denis Karpovd900f712009-09-22 16:44:38 -07001203#ifdef CONFIG_DEBUG_FS
1204
1205static int mmc_regs_show(struct seq_file *s, void *data)
1206{
1207 struct mmc_host *mmc = s->private;
1208 struct mmc_omap_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001209 struct omap_mmc_platform_data *pdata = host->pdata;
1210 int context_loss = 0;
1211
1212 if (pdata->get_context_loss_count)
1213 context_loss = pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001214
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001215 seq_printf(s, "mmc%d:\n"
1216 " enabled:\t%d\n"
1217 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001218 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001219 "\nregs:\n",
Denis Karpov11dd62a2009-09-22 16:44:43 -07001220 mmc->index, mmc->enabled ? 1 : 0, mmc->nesting_cnt,
1221 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001222
1223 if (clk_enable(host->fclk) != 0) {
1224 seq_printf(s, "can't read the regs\n");
1225 goto err;
1226 }
Denis Karpovd900f712009-09-22 16:44:38 -07001227
1228 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1229 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1230 seq_printf(s, "CON:\t\t0x%08x\n",
1231 OMAP_HSMMC_READ(host->base, CON));
1232 seq_printf(s, "HCTL:\t\t0x%08x\n",
1233 OMAP_HSMMC_READ(host->base, HCTL));
1234 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1235 OMAP_HSMMC_READ(host->base, SYSCTL));
1236 seq_printf(s, "IE:\t\t0x%08x\n",
1237 OMAP_HSMMC_READ(host->base, IE));
1238 seq_printf(s, "ISE:\t\t0x%08x\n",
1239 OMAP_HSMMC_READ(host->base, ISE));
1240 seq_printf(s, "CAPA:\t\t0x%08x\n",
1241 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001242
1243 clk_disable(host->fclk);
1244err:
Denis Karpovd900f712009-09-22 16:44:38 -07001245 return 0;
1246}
1247
1248static int mmc_regs_open(struct inode *inode, struct file *file)
1249{
1250 return single_open(file, mmc_regs_show, inode->i_private);
1251}
1252
1253static const struct file_operations mmc_regs_fops = {
1254 .open = mmc_regs_open,
1255 .read = seq_read,
1256 .llseek = seq_lseek,
1257 .release = single_release,
1258};
1259
1260static void omap_mmc_debugfs(struct mmc_host *mmc)
1261{
1262 if (mmc->debugfs_root)
1263 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1264 mmc, &mmc_regs_fops);
1265}
1266
1267#else
1268
1269static void omap_mmc_debugfs(struct mmc_host *mmc)
1270{
1271}
1272
1273#endif
1274
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275static int __init omap_mmc_probe(struct platform_device *pdev)
1276{
1277 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1278 struct mmc_host *mmc;
1279 struct mmc_omap_host *host = NULL;
1280 struct resource *res;
1281 int ret = 0, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282
1283 if (pdata == NULL) {
1284 dev_err(&pdev->dev, "Platform Data is missing\n");
1285 return -ENXIO;
1286 }
1287
1288 if (pdata->nr_slots == 0) {
1289 dev_err(&pdev->dev, "No Slots\n");
1290 return -ENXIO;
1291 }
1292
1293 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1294 irq = platform_get_irq(pdev, 0);
1295 if (res == NULL || irq < 0)
1296 return -ENXIO;
1297
1298 res = request_mem_region(res->start, res->end - res->start + 1,
1299 pdev->name);
1300 if (res == NULL)
1301 return -EBUSY;
1302
1303 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
1304 if (!mmc) {
1305 ret = -ENOMEM;
1306 goto err;
1307 }
1308
1309 host = mmc_priv(mmc);
1310 host->mmc = mmc;
1311 host->pdata = pdata;
1312 host->dev = &pdev->dev;
1313 host->use_dma = 1;
1314 host->dev->dma_mask = &pdata->dma_mask;
1315 host->dma_ch = -1;
1316 host->irq = irq;
1317 host->id = pdev->id;
1318 host->slot_id = 0;
1319 host->mapbase = res->start;
1320 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Huntera3621462009-09-22 16:44:42 -07001321 host->power_mode = -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322
1323 platform_set_drvdata(pdev, host);
1324 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
1325
1326 mmc->ops = &mmc_omap_ops;
1327 mmc->f_min = 400000;
1328 mmc->f_max = 52000000;
1329
1330 sema_init(&host->sem, 1);
1331
Russell King6f7607c2009-01-28 10:22:50 +00001332 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333 if (IS_ERR(host->iclk)) {
1334 ret = PTR_ERR(host->iclk);
1335 host->iclk = NULL;
1336 goto err1;
1337 }
Russell King6f7607c2009-01-28 10:22:50 +00001338 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339 if (IS_ERR(host->fclk)) {
1340 ret = PTR_ERR(host->fclk);
1341 host->fclk = NULL;
1342 clk_put(host->iclk);
1343 goto err1;
1344 }
1345
Denis Karpov11dd62a2009-09-22 16:44:43 -07001346 omap_mmc_save_ctx(host);
1347
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001348 mmc->caps |= MMC_CAP_DISABLE;
1349 mmc_set_disable_delay(mmc, 100);
1350 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351 clk_put(host->iclk);
1352 clk_put(host->fclk);
1353 goto err1;
1354 }
1355
1356 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001357 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358 clk_put(host->iclk);
1359 clk_put(host->fclk);
1360 goto err1;
1361 }
1362
1363 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1364 /*
1365 * MMC can still work without debounce clock.
1366 */
1367 if (IS_ERR(host->dbclk))
1368 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
1369 else
1370 if (clk_enable(host->dbclk) != 0)
1371 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1372 " clk failed\n");
1373 else
1374 host->dbclk_enabled = 1;
1375
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001376 /* Since we do only SG emulation, we can have as many segs
1377 * as we want. */
1378 mmc->max_phys_segs = 1024;
1379 mmc->max_hw_segs = 1024;
1380
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1382 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1383 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1384 mmc->max_seg_size = mmc->max_req_size;
1385
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1387
Jarkko Lavinen73153012008-11-21 16:49:54 +02001388 if (pdata->slots[host->slot_id].wires >= 8)
1389 mmc->caps |= MMC_CAP_8_BIT_DATA;
1390 else if (pdata->slots[host->slot_id].wires >= 4)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001391 mmc->caps |= MMC_CAP_4_BIT_DATA;
1392
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001393 if (pdata->slots[host->slot_id].nonremovable)
1394 mmc->caps |= MMC_CAP_NONREMOVABLE;
1395
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001396 omap_hsmmc_init(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001398 /* Select DMA lines */
1399 switch (host->id) {
1400 case OMAP_MMC1_DEVID:
1401 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1402 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1403 break;
1404 case OMAP_MMC2_DEVID:
1405 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1406 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
1407 break;
1408 case OMAP_MMC3_DEVID:
1409 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
1410 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
1411 break;
1412 default:
1413 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
1414 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415 }
1416
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001417 /* Request IRQ for MMC operations */
1418 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
1419 mmc_hostname(mmc), host);
1420 if (ret) {
1421 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1422 goto err_irq;
1423 }
1424
David Brownellb583f262009-05-28 14:04:03 -07001425 /* initialize power supplies, gpios, etc */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001426 if (pdata->init != NULL) {
1427 if (pdata->init(&pdev->dev) != 0) {
David Brownellb583f262009-05-28 14:04:03 -07001428 dev_dbg(mmc_dev(host->mmc), "late init error\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001429 goto err_irq_cd_init;
1430 }
1431 }
David Brownellb583f262009-05-28 14:04:03 -07001432 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433
1434 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001435 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001436 ret = request_irq(mmc_slot(host).card_detect_irq,
1437 omap_mmc_cd_handler,
1438 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1439 | IRQF_DISABLED,
1440 mmc_hostname(mmc), host);
1441 if (ret) {
1442 dev_dbg(mmc_dev(host->mmc),
1443 "Unable to grab MMC CD IRQ\n");
1444 goto err_irq_cd;
1445 }
1446 }
1447
1448 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1449 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1450
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001451 mmc_host_lazy_disable(host->mmc);
1452
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001453 mmc_add_host(mmc);
1454
1455 if (host->pdata->slots[host->slot_id].name != NULL) {
1456 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1457 if (ret < 0)
1458 goto err_slot_name;
1459 }
Adrian Huntere1a55f52009-01-26 13:17:25 +02001460 if (mmc_slot(host).card_detect_irq &&
1461 host->pdata->slots[host->slot_id].get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001462 ret = device_create_file(&mmc->class_dev,
1463 &dev_attr_cover_switch);
1464 if (ret < 0)
1465 goto err_cover_switch;
1466 }
1467
Denis Karpovd900f712009-09-22 16:44:38 -07001468 omap_mmc_debugfs(mmc);
1469
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001470 return 0;
1471
1472err_cover_switch:
1473 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1474err_slot_name:
1475 mmc_remove_host(mmc);
1476err_irq_cd:
1477 free_irq(mmc_slot(host).card_detect_irq, host);
1478err_irq_cd_init:
1479 free_irq(host->irq, host);
1480err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001481 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482 clk_disable(host->iclk);
1483 clk_put(host->fclk);
1484 clk_put(host->iclk);
1485 if (host->dbclk_enabled) {
1486 clk_disable(host->dbclk);
1487 clk_put(host->dbclk);
1488 }
1489
1490err1:
1491 iounmap(host->base);
1492err:
1493 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1494 release_mem_region(res->start, res->end - res->start + 1);
1495 if (host)
1496 mmc_free_host(mmc);
1497 return ret;
1498}
1499
1500static int omap_mmc_remove(struct platform_device *pdev)
1501{
1502 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1503 struct resource *res;
1504
1505 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001506 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001507 mmc_remove_host(host->mmc);
1508 if (host->pdata->cleanup)
1509 host->pdata->cleanup(&pdev->dev);
1510 free_irq(host->irq, host);
1511 if (mmc_slot(host).card_detect_irq)
1512 free_irq(mmc_slot(host).card_detect_irq, host);
1513 flush_scheduled_work();
1514
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001515 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 clk_disable(host->iclk);
1517 clk_put(host->fclk);
1518 clk_put(host->iclk);
1519 if (host->dbclk_enabled) {
1520 clk_disable(host->dbclk);
1521 clk_put(host->dbclk);
1522 }
1523
1524 mmc_free_host(host->mmc);
1525 iounmap(host->base);
1526 }
1527
1528 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1529 if (res)
1530 release_mem_region(res->start, res->end - res->start + 1);
1531 platform_set_drvdata(pdev, NULL);
1532
1533 return 0;
1534}
1535
1536#ifdef CONFIG_PM
1537static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1538{
1539 int ret = 0;
1540 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1541
1542 if (host && host->suspended)
1543 return 0;
1544
1545 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07001546 host->suspended = 1;
1547 if (host->pdata->suspend) {
1548 ret = host->pdata->suspend(&pdev->dev,
1549 host->slot_id);
1550 if (ret) {
1551 dev_dbg(mmc_dev(host->mmc),
1552 "Unable to handle MMC board"
1553 " level suspend\n");
1554 host->suspended = 0;
1555 return ret;
1556 }
1557 }
1558 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001559 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560 ret = mmc_suspend_host(host->mmc, state);
1561 if (ret == 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1563 OMAP_HSMMC_WRITE(host->base, IE, 0);
1564
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565
Jarkko Lavinen0683af42009-03-12 15:30:58 +02001566 OMAP_HSMMC_WRITE(host->base, HCTL,
1567 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001568 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569 clk_disable(host->iclk);
1570 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07001571 } else {
1572 host->suspended = 0;
1573 if (host->pdata->resume) {
1574 ret = host->pdata->resume(&pdev->dev,
1575 host->slot_id);
1576 if (ret)
1577 dev_dbg(mmc_dev(host->mmc),
1578 "Unmask interrupt failed\n");
1579 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001580 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07001581 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582
1583 }
1584 return ret;
1585}
1586
1587/* Routine to resume the MMC device */
1588static int omap_mmc_resume(struct platform_device *pdev)
1589{
1590 int ret = 0;
1591 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1592
1593 if (host && !host->suspended)
1594 return 0;
1595
1596 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001597 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001598 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001599 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600
1601 if (clk_enable(host->dbclk) != 0)
1602 dev_dbg(mmc_dev(host->mmc),
1603 "Enabling debounce clk failed\n");
1604
Denis Karpov11dd62a2009-09-22 16:44:43 -07001605 if (mmc_host_enable(host->mmc) != 0) {
1606 clk_disable(host->iclk);
1607 goto clk_en_err;
1608 }
1609
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001610 omap_hsmmc_init(host);
1611
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612 if (host->pdata->resume) {
1613 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1614 if (ret)
1615 dev_dbg(mmc_dev(host->mmc),
1616 "Unmask interrupt failed\n");
1617 }
1618
1619 /* Notify the core to resume the host */
1620 ret = mmc_resume_host(host->mmc);
1621 if (ret == 0)
1622 host->suspended = 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001623 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624 }
1625
1626 return ret;
1627
1628clk_en_err:
1629 dev_dbg(mmc_dev(host->mmc),
1630 "Failed to enable MMC clocks during resume\n");
1631 return ret;
1632}
1633
1634#else
1635#define omap_mmc_suspend NULL
1636#define omap_mmc_resume NULL
1637#endif
1638
1639static struct platform_driver omap_mmc_driver = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640 .remove = omap_mmc_remove,
1641 .suspend = omap_mmc_suspend,
1642 .resume = omap_mmc_resume,
1643 .driver = {
1644 .name = DRIVER_NAME,
1645 .owner = THIS_MODULE,
1646 },
1647};
1648
1649static int __init omap_mmc_init(void)
1650{
1651 /* Register the MMC driver */
Uwe Kleine-Königf400cd82009-09-22 16:44:26 -07001652 return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001653}
1654
1655static void __exit omap_mmc_cleanup(void)
1656{
1657 /* Unregister MMC driver */
1658 platform_driver_unregister(&omap_mmc_driver);
1659}
1660
1661module_init(omap_mmc_init);
1662module_exit(omap_mmc_cleanup);
1663
1664MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1665MODULE_LICENSE("GPL");
1666MODULE_ALIAS("platform:" DRIVER_NAME);
1667MODULE_AUTHOR("Texas Instruments Inc");