blob: 535499f11e7c2cabbb6cf67ad60bac16f217c78b [file] [log] [blame]
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/omap.c
Carlos Aguiar730c9b72006-03-29 09:21:00 +01003 *
4 * Copyright (C) 2004 Nokia Corporation
5 * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
6 * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
7 * Other hacks (DMA, SD, etc) by David Brownell
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Carlos Aguiar730c9b72006-03-29 09:21:00 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/ioport.h>
18#include <linux/platform_device.h>
19#include <linux/interrupt.h>
20#include <linux/dma-mapping.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/timer.h>
24#include <linux/mmc/host.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010025#include <linux/mmc/card.h>
26#include <linux/clk.h>
Jens Axboe45711f12007-10-22 21:19:53 +020027#include <linux/scatterlist.h>
David Brownell6d16bfb2008-01-27 18:14:49 +010028#include <linux/i2c/tps65010.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010029
30#include <asm/io.h>
31#include <asm/irq.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010032#include <asm/mach-types.h>
33
34#include <asm/arch/board.h>
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -040035#include <asm/arch/mmc.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010036#include <asm/arch/gpio.h>
37#include <asm/arch/dma.h>
38#include <asm/arch/mux.h>
39#include <asm/arch/fpga.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010040
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010041#define OMAP_MMC_REG_CMD 0x00
42#define OMAP_MMC_REG_ARGL 0x04
43#define OMAP_MMC_REG_ARGH 0x08
44#define OMAP_MMC_REG_CON 0x0c
45#define OMAP_MMC_REG_STAT 0x10
46#define OMAP_MMC_REG_IE 0x14
47#define OMAP_MMC_REG_CTO 0x18
48#define OMAP_MMC_REG_DTO 0x1c
49#define OMAP_MMC_REG_DATA 0x20
50#define OMAP_MMC_REG_BLEN 0x24
51#define OMAP_MMC_REG_NBLK 0x28
52#define OMAP_MMC_REG_BUF 0x2c
53#define OMAP_MMC_REG_SDIO 0x34
54#define OMAP_MMC_REG_REV 0x3c
55#define OMAP_MMC_REG_RSP0 0x40
56#define OMAP_MMC_REG_RSP1 0x44
57#define OMAP_MMC_REG_RSP2 0x48
58#define OMAP_MMC_REG_RSP3 0x4c
59#define OMAP_MMC_REG_RSP4 0x50
60#define OMAP_MMC_REG_RSP5 0x54
61#define OMAP_MMC_REG_RSP6 0x58
62#define OMAP_MMC_REG_RSP7 0x5c
63#define OMAP_MMC_REG_IOSR 0x60
64#define OMAP_MMC_REG_SYSC 0x64
65#define OMAP_MMC_REG_SYSS 0x68
66
67#define OMAP_MMC_STAT_CARD_ERR (1 << 14)
68#define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
69#define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
70#define OMAP_MMC_STAT_A_EMPTY (1 << 11)
71#define OMAP_MMC_STAT_A_FULL (1 << 10)
72#define OMAP_MMC_STAT_CMD_CRC (1 << 8)
73#define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
74#define OMAP_MMC_STAT_DATA_CRC (1 << 6)
75#define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
76#define OMAP_MMC_STAT_END_BUSY (1 << 4)
77#define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
78#define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
79#define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
80
81#define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
82#define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
83
84/*
85 * Command types
86 */
87#define OMAP_MMC_CMDTYPE_BC 0
88#define OMAP_MMC_CMDTYPE_BCR 1
89#define OMAP_MMC_CMDTYPE_AC 2
90#define OMAP_MMC_CMDTYPE_ADTC 3
91
Carlos Aguiar730c9b72006-03-29 09:21:00 +010092
93#define DRIVER_NAME "mmci-omap"
Carlos Aguiar730c9b72006-03-29 09:21:00 +010094
95/* Specifies how often in millisecs to poll for card status changes
96 * when the cover switch is open */
97#define OMAP_MMC_SWITCH_POLL_DELAY 500
98
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -040099struct mmc_omap_host;
100
101struct mmc_omap_slot {
102 int id;
103 unsigned int vdd;
104 u16 saved_con;
105 u16 bus_mode;
106 unsigned int fclk_freq;
107 unsigned powered:1;
108
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400109 struct work_struct switch_work;
110 struct timer_list switch_timer;
111 unsigned cover_open;
112
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400113 struct mmc_request *mrq;
114 struct mmc_omap_host *host;
115 struct mmc_host *mmc;
116 struct omap_mmc_slot_data *pdata;
117};
118
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100119struct mmc_omap_host {
120 int initialized;
121 int suspended;
122 struct mmc_request * mrq;
123 struct mmc_command * cmd;
124 struct mmc_data * data;
125 struct mmc_host * mmc;
126 struct device * dev;
127 unsigned char id; /* 16xx chips have 2 MMC blocks */
128 struct clk * iclk;
129 struct clk * fclk;
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100130 struct resource *mem_res;
131 void __iomem *virt_base;
132 unsigned int phys_base;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100133 int irq;
134 unsigned char bus_mode;
135 unsigned char hw_bus_mode;
136
137 unsigned int sg_len;
138 int sg_idx;
139 u16 * buffer;
140 u32 buffer_bytes_left;
141 u32 total_bytes_left;
142
143 unsigned use_dma:1;
144 unsigned brs_received:1, dma_done:1;
145 unsigned dma_is_read:1;
146 unsigned dma_in_use:1;
147 int dma_ch;
148 spinlock_t dma_lock;
149 struct timer_list dma_timer;
150 unsigned dma_len;
151
152 short power_pin;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100153
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400154 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
155 struct mmc_omap_slot *current_slot;
156 spinlock_t slot_lock;
157 wait_queue_head_t slot_wq;
158 int nr_slots;
159
160 struct omap_mmc_platform_data *pdata;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100161};
162
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400163static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
164{
165 struct mmc_omap_host *host = slot->host;
166 unsigned long flags;
167
168 if (claimed)
169 goto no_claim;
170 spin_lock_irqsave(&host->slot_lock, flags);
171 while (host->mmc != NULL) {
172 spin_unlock_irqrestore(&host->slot_lock, flags);
173 wait_event(host->slot_wq, host->mmc == NULL);
174 spin_lock_irqsave(&host->slot_lock, flags);
175 }
176 host->mmc = slot->mmc;
177 spin_unlock_irqrestore(&host->slot_lock, flags);
178no_claim:
179 clk_enable(host->fclk);
180 if (host->current_slot != slot) {
181 if (host->pdata->switch_slot != NULL)
182 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
183 host->current_slot = slot;
184 }
185
186 /* Doing the dummy read here seems to work around some bug
187 * at least in OMAP24xx silicon where the command would not
188 * start after writing the CMD register. Sigh. */
189 OMAP_MMC_READ(host, CON);
190
191 OMAP_MMC_WRITE(host, CON, slot->saved_con);
192}
193
194static void mmc_omap_start_request(struct mmc_omap_host *host,
195 struct mmc_request *req);
196
197static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
198{
199 struct mmc_omap_host *host = slot->host;
200 unsigned long flags;
201 int i;
202
203 BUG_ON(slot == NULL || host->mmc == NULL);
204 clk_disable(host->fclk);
205
206 spin_lock_irqsave(&host->slot_lock, flags);
207 /* Check for any pending requests */
208 for (i = 0; i < host->nr_slots; i++) {
209 struct mmc_omap_slot *new_slot;
210 struct mmc_request *rq;
211
212 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
213 continue;
214
215 new_slot = host->slots[i];
216 /* The current slot should not have a request in queue */
217 BUG_ON(new_slot == host->current_slot);
218
219 host->mmc = new_slot->mmc;
220 spin_unlock_irqrestore(&host->slot_lock, flags);
221 mmc_omap_select_slot(new_slot, 1);
222 rq = new_slot->mrq;
223 new_slot->mrq = NULL;
224 mmc_omap_start_request(host, rq);
225 return;
226 }
227
228 host->mmc = NULL;
229 wake_up(&host->slot_wq);
230 spin_unlock_irqrestore(&host->slot_lock, flags);
231}
232
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400233static inline
234int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
235{
236 return slot->pdata->get_cover_state(mmc_dev(slot->mmc), slot->id);
237}
238
239static ssize_t
240mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
241 char *buf)
242{
243 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
244 struct mmc_omap_slot *slot = mmc_priv(mmc);
245
246 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
247 "closed");
248}
249
250static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
251
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400252static ssize_t
253mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
254 char *buf)
255{
256 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
257 struct mmc_omap_slot *slot = mmc_priv(mmc);
258
259 return sprintf(buf, "%s\n", slot->pdata->name);
260}
261
262static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
263
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100264static void
265mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
266{
267 u32 cmdreg;
268 u32 resptype;
269 u32 cmdtype;
270
271 host->cmd = cmd;
272
273 resptype = 0;
274 cmdtype = 0;
275
276 /* Our hardware needs to know exact type */
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100277 switch (mmc_resp_type(cmd)) {
278 case MMC_RSP_NONE:
279 break;
280 case MMC_RSP_R1:
281 case MMC_RSP_R1B:
Philip Langdale6f949902007-01-04 07:04:47 -0800282 /* resp 1, 1b, 6, 7 */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100283 resptype = 1;
284 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100285 case MMC_RSP_R2:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100286 resptype = 2;
287 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100288 case MMC_RSP_R3:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100289 resptype = 3;
290 break;
291 default:
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100292 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100293 break;
294 }
295
296 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
297 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
298 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
299 cmdtype = OMAP_MMC_CMDTYPE_BC;
300 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
301 cmdtype = OMAP_MMC_CMDTYPE_BCR;
302 } else {
303 cmdtype = OMAP_MMC_CMDTYPE_AC;
304 }
305
306 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
307
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400308 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100309 cmdreg |= 1 << 6;
310
311 if (cmd->flags & MMC_RSP_BUSY)
312 cmdreg |= 1 << 11;
313
314 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
315 cmdreg |= 1 << 15;
316
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100317 OMAP_MMC_WRITE(host, CTO, 200);
318 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
319 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
320 OMAP_MMC_WRITE(host, IE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100321 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
322 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
323 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
324 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
325 OMAP_MMC_STAT_END_OF_DATA);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100326 OMAP_MMC_WRITE(host, CMD, cmdreg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100327}
328
329static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400330mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
331 int abort)
332{
333 enum dma_data_direction dma_data_dir;
334
335 BUG_ON(host->dma_ch < 0);
336 if (data->error)
337 omap_stop_dma(host->dma_ch);
338 /* Release DMA channel lazily */
339 mod_timer(&host->dma_timer, jiffies + HZ);
340 if (data->flags & MMC_DATA_WRITE)
341 dma_data_dir = DMA_TO_DEVICE;
342 else
343 dma_data_dir = DMA_FROM_DEVICE;
344 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
345 dma_data_dir);
346}
347
348static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100349mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
350{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400351 if (host->dma_in_use)
352 mmc_omap_release_dma(host, data, data->error);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100353
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100354 host->data = NULL;
355 host->sg_len = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100356
357 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
358 * dozens of requests until the card finishes writing data.
359 * It'd be cheaper to just wait till an EOFB interrupt arrives...
360 */
361
362 if (!data->stop) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400363 struct mmc_host *mmc;
364
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100365 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400366 mmc = host->mmc;
367 mmc_omap_release_slot(host->current_slot);
368 mmc_request_done(mmc, data->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100369 return;
370 }
371
372 mmc_omap_start_command(host, data->stop);
373}
374
375static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400376mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
377{
378 int loops;
379 u16 ie;
380
381 if (host->dma_in_use)
382 mmc_omap_release_dma(host, data, 1);
383
384 host->data = NULL;
385 host->sg_len = 0;
386
387 ie = OMAP_MMC_READ(host, IE);
388 OMAP_MMC_WRITE(host, IE, 0);
389 OMAP_MMC_WRITE(host, CMD, 1 << 7);
390 loops = 0;
391 while (!(OMAP_MMC_READ(host, STAT) & OMAP_MMC_STAT_END_OF_CMD)) {
392 udelay(1);
393 loops++;
394 if (loops == 100000)
395 break;
396 }
397 OMAP_MMC_WRITE(host, STAT, OMAP_MMC_STAT_END_OF_CMD);
398 OMAP_MMC_WRITE(host, IE, ie);
399}
400
401static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100402mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
403{
404 unsigned long flags;
405 int done;
406
407 if (!host->dma_in_use) {
408 mmc_omap_xfer_done(host, data);
409 return;
410 }
411 done = 0;
412 spin_lock_irqsave(&host->dma_lock, flags);
413 if (host->dma_done)
414 done = 1;
415 else
416 host->brs_received = 1;
417 spin_unlock_irqrestore(&host->dma_lock, flags);
418 if (done)
419 mmc_omap_xfer_done(host, data);
420}
421
422static void
423mmc_omap_dma_timer(unsigned long data)
424{
425 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
426
427 BUG_ON(host->dma_ch < 0);
428 omap_free_dma(host->dma_ch);
429 host->dma_ch = -1;
430}
431
432static void
433mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
434{
435 unsigned long flags;
436 int done;
437
438 done = 0;
439 spin_lock_irqsave(&host->dma_lock, flags);
440 if (host->brs_received)
441 done = 1;
442 else
443 host->dma_done = 1;
444 spin_unlock_irqrestore(&host->dma_lock, flags);
445 if (done)
446 mmc_omap_xfer_done(host, data);
447}
448
449static void
450mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
451{
452 host->cmd = NULL;
453
454 if (cmd->flags & MMC_RSP_PRESENT) {
455 if (cmd->flags & MMC_RSP_136) {
456 /* response type 2 */
457 cmd->resp[3] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100458 OMAP_MMC_READ(host, RSP0) |
459 (OMAP_MMC_READ(host, RSP1) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100460 cmd->resp[2] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100461 OMAP_MMC_READ(host, RSP2) |
462 (OMAP_MMC_READ(host, RSP3) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100463 cmd->resp[1] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100464 OMAP_MMC_READ(host, RSP4) |
465 (OMAP_MMC_READ(host, RSP5) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100466 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100467 OMAP_MMC_READ(host, RSP6) |
468 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100469 } else {
470 /* response types 1, 1b, 3, 4, 5, 6 */
471 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100472 OMAP_MMC_READ(host, RSP6) |
473 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100474 }
475 }
476
Pierre Ossman17b04292007-07-22 22:18:46 +0200477 if (host->data == NULL || cmd->error) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400478 struct mmc_host *mmc;
479
480 if (host->data != NULL)
481 mmc_omap_abort_xfer(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100482 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400483 mmc = host->mmc;
484 mmc_omap_release_slot(host->current_slot);
485 mmc_request_done(mmc, cmd->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100486 }
487}
488
489/* PIO only */
490static void
491mmc_omap_sg_to_buf(struct mmc_omap_host *host)
492{
493 struct scatterlist *sg;
494
495 sg = host->data->sg + host->sg_idx;
496 host->buffer_bytes_left = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +0200497 host->buffer = sg_virt(sg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100498 if (host->buffer_bytes_left > host->total_bytes_left)
499 host->buffer_bytes_left = host->total_bytes_left;
500}
501
502/* PIO only */
503static void
504mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
505{
506 int n;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100507
508 if (host->buffer_bytes_left == 0) {
509 host->sg_idx++;
510 BUG_ON(host->sg_idx == host->sg_len);
511 mmc_omap_sg_to_buf(host);
512 }
513 n = 64;
514 if (n > host->buffer_bytes_left)
515 n = host->buffer_bytes_left;
516 host->buffer_bytes_left -= n;
517 host->total_bytes_left -= n;
518 host->data->bytes_xfered += n;
519
520 if (write) {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100521 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100522 } else {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100523 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100524 }
525}
526
527static inline void mmc_omap_report_irq(u16 status)
528{
529 static const char *mmc_omap_status_bits[] = {
530 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
531 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
532 };
533 int i, c = 0;
534
535 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
536 if (status & (1 << i)) {
537 if (c)
538 printk(" ");
539 printk("%s", mmc_omap_status_bits[i]);
540 c++;
541 }
542}
543
David Howells7d12e782006-10-05 14:55:46 +0100544static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100545{
546 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
547 u16 status;
548 int end_command;
549 int end_transfer;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400550 int transfer_error, cmd_error;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100551
552 if (host->cmd == NULL && host->data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100553 status = OMAP_MMC_READ(host, STAT);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400554 dev_info(mmc_dev(host->slots[0]->mmc),
555 "Spurious IRQ 0x%04x\n", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100556 if (status != 0) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100557 OMAP_MMC_WRITE(host, STAT, status);
558 OMAP_MMC_WRITE(host, IE, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100559 }
560 return IRQ_HANDLED;
561 }
562
563 end_command = 0;
564 end_transfer = 0;
565 transfer_error = 0;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400566 cmd_error = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100567
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100568 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400569 int cmd;
570
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100571 OMAP_MMC_WRITE(host, STAT, status);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400572 if (host->cmd != NULL)
573 cmd = host->cmd->opcode;
574 else
575 cmd = -1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100576#ifdef CONFIG_MMC_DEBUG
577 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400578 status, cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100579 mmc_omap_report_irq(status);
580 printk("\n");
581#endif
582 if (host->total_bytes_left) {
583 if ((status & OMAP_MMC_STAT_A_FULL) ||
584 (status & OMAP_MMC_STAT_END_OF_DATA))
585 mmc_omap_xfer_data(host, 0);
586 if (status & OMAP_MMC_STAT_A_EMPTY)
587 mmc_omap_xfer_data(host, 1);
588 }
589
Juha Yrjola2a50b882008-03-26 16:09:26 -0400590 if (status & OMAP_MMC_STAT_END_OF_DATA)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100591 end_transfer = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100592
593 if (status & OMAP_MMC_STAT_DATA_TOUT) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400594 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
595 cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100596 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200597 host->data->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100598 transfer_error = 1;
599 }
600 }
601
602 if (status & OMAP_MMC_STAT_DATA_CRC) {
603 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200604 host->data->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100605 dev_dbg(mmc_dev(host->mmc),
606 "data CRC error, bytes left %d\n",
607 host->total_bytes_left);
608 transfer_error = 1;
609 } else {
610 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
611 }
612 }
613
614 if (status & OMAP_MMC_STAT_CMD_TOUT) {
615 /* Timeouts are routine with some commands */
616 if (host->cmd) {
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400617 struct mmc_omap_slot *slot =
618 host->current_slot;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400619 if (slot == NULL ||
620 !mmc_omap_cover_is_open(slot))
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400621 dev_err(mmc_dev(host->mmc),
Juha Yrjola2a50b882008-03-26 16:09:26 -0400622 "command timeout (CMD%d)\n",
623 cmd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200624 host->cmd->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100625 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400626 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100627 }
628 }
629
630 if (status & OMAP_MMC_STAT_CMD_CRC) {
631 if (host->cmd) {
632 dev_err(mmc_dev(host->mmc),
633 "command CRC error (CMD%d, arg 0x%08x)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400634 cmd, host->cmd->arg);
Pierre Ossman17b04292007-07-22 22:18:46 +0200635 host->cmd->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100636 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400637 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100638 } else
639 dev_err(mmc_dev(host->mmc),
640 "command CRC error without cmd?\n");
641 }
642
643 if (status & OMAP_MMC_STAT_CARD_ERR) {
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200644 dev_dbg(mmc_dev(host->mmc),
645 "ignoring card status error (CMD%d)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400646 cmd);
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200647 end_command = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100648 }
649
650 /*
651 * NOTE: On 1610 the END_OF_CMD may come too early when
Juha Yrjola2a50b882008-03-26 16:09:26 -0400652 * starting a write
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100653 */
654 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
655 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
656 end_command = 1;
657 }
658 }
659
Juha Yrjola2a50b882008-03-26 16:09:26 -0400660 if (end_command)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100661 mmc_omap_cmd_done(host, host->cmd);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400662 if (host->data != NULL) {
663 if (transfer_error)
664 mmc_omap_xfer_done(host, host->data);
665 else if (end_transfer)
666 mmc_omap_end_of_data(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100667 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100668
669 return IRQ_HANDLED;
670}
671
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400672void omap_mmc_notify_cover_event(struct device *dev, int slot, int is_closed)
673{
674 struct mmc_omap_host *host = dev_get_drvdata(dev);
675
676 BUG_ON(slot >= host->nr_slots);
677
678 /* Other subsystems can call in here before we're initialised. */
679 if (host->nr_slots == 0 || !host->slots[slot])
680 return;
681
682 schedule_work(&host->slots[slot]->switch_work);
683}
684
685static void mmc_omap_switch_timer(unsigned long arg)
686{
687 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
688
689 schedule_work(&slot->switch_work);
690}
691
692static void mmc_omap_cover_handler(struct work_struct *work)
693{
694 struct mmc_omap_slot *slot = container_of(work, struct mmc_omap_slot,
695 switch_work);
696 int cover_open;
697
698 cover_open = mmc_omap_cover_is_open(slot);
699 if (cover_open != slot->cover_open) {
700 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
701 slot->cover_open = cover_open;
702 dev_info(mmc_dev(slot->mmc), "cover is now %s\n",
703 cover_open ? "open" : "closed");
704 }
705 mmc_detect_change(slot->mmc, slot->id);
706}
707
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100708/* Prepare to transfer the next segment of a scatterlist */
709static void
710mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
711{
712 int dma_ch = host->dma_ch;
713 unsigned long data_addr;
714 u16 buf, frame;
715 u32 count;
716 struct scatterlist *sg = &data->sg[host->sg_idx];
717 int src_port = 0;
718 int dst_port = 0;
719 int sync_dev = 0;
720
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100721 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
Russell Kinga3fd4a12006-06-04 17:51:15 +0100722 frame = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100723 count = sg_dma_len(sg);
724
Russell Kinga3fd4a12006-06-04 17:51:15 +0100725 if ((data->blocks == 1) && (count > data->blksz))
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100726 count = frame;
727
728 host->dma_len = count;
729
730 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
731 * Use 16 or 32 word frames when the blocksize is at least that large.
732 * Blocksize is usually 512 bytes; but not for some SD reads.
733 */
734 if (cpu_is_omap15xx() && frame > 32)
735 frame = 32;
736 else if (frame > 64)
737 frame = 64;
738 count /= frame;
739 frame >>= 1;
740
741 if (!(data->flags & MMC_DATA_WRITE)) {
742 buf = 0x800f | ((frame - 1) << 8);
743
744 if (cpu_class_is_omap1()) {
745 src_port = OMAP_DMA_PORT_TIPB;
746 dst_port = OMAP_DMA_PORT_EMIFF;
747 }
748 if (cpu_is_omap24xx())
749 sync_dev = OMAP24XX_DMA_MMC1_RX;
750
751 omap_set_dma_src_params(dma_ch, src_port,
752 OMAP_DMA_AMODE_CONSTANT,
753 data_addr, 0, 0);
754 omap_set_dma_dest_params(dma_ch, dst_port,
755 OMAP_DMA_AMODE_POST_INC,
756 sg_dma_address(sg), 0, 0);
757 omap_set_dma_dest_data_pack(dma_ch, 1);
758 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
759 } else {
760 buf = 0x0f80 | ((frame - 1) << 0);
761
762 if (cpu_class_is_omap1()) {
763 src_port = OMAP_DMA_PORT_EMIFF;
764 dst_port = OMAP_DMA_PORT_TIPB;
765 }
766 if (cpu_is_omap24xx())
767 sync_dev = OMAP24XX_DMA_MMC1_TX;
768
769 omap_set_dma_dest_params(dma_ch, dst_port,
770 OMAP_DMA_AMODE_CONSTANT,
771 data_addr, 0, 0);
772 omap_set_dma_src_params(dma_ch, src_port,
773 OMAP_DMA_AMODE_POST_INC,
774 sg_dma_address(sg), 0, 0);
775 omap_set_dma_src_data_pack(dma_ch, 1);
776 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
777 }
778
779 /* Max limit for DMA frame count is 0xffff */
Eric Sesterhennd99c5902006-11-30 05:27:38 +0100780 BUG_ON(count > 0xffff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100781
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100782 OMAP_MMC_WRITE(host, BUF, buf);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100783 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
784 frame, count, OMAP_DMA_SYNC_FRAME,
785 sync_dev, 0);
786}
787
788/* A scatterlist segment completed */
789static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
790{
791 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
792 struct mmc_data *mmcdat = host->data;
793
794 if (unlikely(host->dma_ch < 0)) {
Tony Lindgrence9c1a82006-07-01 19:56:44 +0100795 dev_err(mmc_dev(host->mmc),
796 "DMA callback while DMA not enabled\n");
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100797 return;
798 }
799 /* FIXME: We really should do something to _handle_ the errors */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700800 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100801 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
802 return;
803 }
804 if (ch_status & OMAP_DMA_DROP_IRQ) {
805 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
806 return;
807 }
808 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
809 return;
810 }
811 mmcdat->bytes_xfered += host->dma_len;
812 host->sg_idx++;
813 if (host->sg_idx < host->sg_len) {
814 mmc_omap_prepare_dma(host, host->data);
815 omap_start_dma(host->dma_ch);
816 } else
817 mmc_omap_dma_done(host, host->data);
818}
819
820static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
821{
822 const char *dev_name;
823 int sync_dev, dma_ch, is_read, r;
824
825 is_read = !(data->flags & MMC_DATA_WRITE);
826 del_timer_sync(&host->dma_timer);
827 if (host->dma_ch >= 0) {
828 if (is_read == host->dma_is_read)
829 return 0;
830 omap_free_dma(host->dma_ch);
831 host->dma_ch = -1;
832 }
833
834 if (is_read) {
835 if (host->id == 1) {
836 sync_dev = OMAP_DMA_MMC_RX;
837 dev_name = "MMC1 read";
838 } else {
839 sync_dev = OMAP_DMA_MMC2_RX;
840 dev_name = "MMC2 read";
841 }
842 } else {
843 if (host->id == 1) {
844 sync_dev = OMAP_DMA_MMC_TX;
845 dev_name = "MMC1 write";
846 } else {
847 sync_dev = OMAP_DMA_MMC2_TX;
848 dev_name = "MMC2 write";
849 }
850 }
851 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
852 host, &dma_ch);
853 if (r != 0) {
854 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
855 return r;
856 }
857 host->dma_ch = dma_ch;
858 host->dma_is_read = is_read;
859
860 return 0;
861}
862
863static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
864{
865 u16 reg;
866
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100867 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100868 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100869 OMAP_MMC_WRITE(host, SDIO, reg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100870 /* Set maximum timeout */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100871 OMAP_MMC_WRITE(host, CTO, 0xff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100872}
873
874static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
875{
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400876 unsigned int timeout, cycle_ns;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100877 u16 reg;
878
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400879 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
880 timeout = req->data->timeout_ns / cycle_ns;
881 timeout += req->data->timeout_clks;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100882
883 /* Check if we need to use timeout multiplier register */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100884 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100885 if (timeout > 0xffff) {
886 reg |= (1 << 5);
887 timeout /= 1024;
888 } else
889 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100890 OMAP_MMC_WRITE(host, SDIO, reg);
891 OMAP_MMC_WRITE(host, DTO, timeout);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100892}
893
894static void
895mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
896{
897 struct mmc_data *data = req->data;
898 int i, use_dma, block_size;
899 unsigned sg_len;
900
901 host->data = data;
902 if (data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100903 OMAP_MMC_WRITE(host, BLEN, 0);
904 OMAP_MMC_WRITE(host, NBLK, 0);
905 OMAP_MMC_WRITE(host, BUF, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100906 host->dma_in_use = 0;
907 set_cmd_timeout(host, req);
908 return;
909 }
910
Russell Kinga3fd4a12006-06-04 17:51:15 +0100911 block_size = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100912
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100913 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
914 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100915 set_data_timeout(host, req);
916
917 /* cope with calling layer confusion; it issues "single
918 * block" writes using multi-block scatterlists.
919 */
920 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
921
922 /* Only do DMA for entire blocks */
923 use_dma = host->use_dma;
924 if (use_dma) {
925 for (i = 0; i < sg_len; i++) {
926 if ((data->sg[i].length % block_size) != 0) {
927 use_dma = 0;
928 break;
929 }
930 }
931 }
932
933 host->sg_idx = 0;
934 if (use_dma) {
935 if (mmc_omap_get_dma_channel(host, data) == 0) {
936 enum dma_data_direction dma_data_dir;
937
938 if (data->flags & MMC_DATA_WRITE)
939 dma_data_dir = DMA_TO_DEVICE;
940 else
941 dma_data_dir = DMA_FROM_DEVICE;
942
943 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
944 sg_len, dma_data_dir);
945 host->total_bytes_left = 0;
946 mmc_omap_prepare_dma(host, req->data);
947 host->brs_received = 0;
948 host->dma_done = 0;
949 host->dma_in_use = 1;
950 } else
951 use_dma = 0;
952 }
953
954 /* Revert to PIO? */
955 if (!use_dma) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100956 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100957 host->total_bytes_left = data->blocks * block_size;
958 host->sg_len = sg_len;
959 mmc_omap_sg_to_buf(host);
960 host->dma_in_use = 0;
961 }
962}
963
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400964static void mmc_omap_start_request(struct mmc_omap_host *host,
965 struct mmc_request *req)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100966{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400967 BUG_ON(host->mrq != NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100968
969 host->mrq = req;
970
971 /* only touch fifo AFTER the controller readies it */
972 mmc_omap_prepare_data(host, req);
973 mmc_omap_start_command(host, req->cmd);
974 if (host->dma_in_use)
975 omap_start_dma(host->dma_ch);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400976 BUG_ON(irqs_disabled());
977}
978
979static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
980{
981 struct mmc_omap_slot *slot = mmc_priv(mmc);
982 struct mmc_omap_host *host = slot->host;
983 unsigned long flags;
984
985 spin_lock_irqsave(&host->slot_lock, flags);
986 if (host->mmc != NULL) {
987 BUG_ON(slot->mrq != NULL);
988 slot->mrq = req;
989 spin_unlock_irqrestore(&host->slot_lock, flags);
990 return;
991 } else
992 host->mmc = mmc;
993 spin_unlock_irqrestore(&host->slot_lock, flags);
994 mmc_omap_select_slot(slot, 1);
995 mmc_omap_start_request(host, req);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100996}
997
Juha Yrjola65b5b6e2008-03-26 16:09:22 -0400998static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
999 int vdd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001000{
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001001 struct mmc_omap_host *host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001002
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001003 host = slot->host;
1004
1005 if (slot->pdata->set_power != NULL)
1006 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1007 vdd);
1008
1009 if (cpu_is_omap24xx()) {
1010 u16 w;
1011
1012 if (power_on) {
1013 w = OMAP_MMC_READ(host, CON);
1014 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1015 } else {
1016 w = OMAP_MMC_READ(host, CON);
1017 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1018 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001019 }
1020}
1021
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001022static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1023{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001024 struct mmc_omap_slot *slot = mmc_priv(mmc);
1025 struct mmc_omap_host *host = slot->host;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001026 int func_clk_rate = clk_get_rate(host->fclk);
1027 int dsor;
1028
1029 if (ios->clock == 0)
1030 return 0;
1031
1032 dsor = func_clk_rate / ios->clock;
1033 if (dsor < 1)
1034 dsor = 1;
1035
1036 if (func_clk_rate / dsor > ios->clock)
1037 dsor++;
1038
1039 if (dsor > 250)
1040 dsor = 250;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001041
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001042 slot->fclk_freq = func_clk_rate / dsor;
1043
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001044 if (ios->bus_width == MMC_BUS_WIDTH_4)
1045 dsor |= 1 << 15;
1046
1047 return dsor;
1048}
1049
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001050static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1051{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001052 struct mmc_omap_slot *slot = mmc_priv(mmc);
1053 struct mmc_omap_host *host = slot->host;
1054 int i, dsor;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001055
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001056 dsor = mmc_omap_calc_divisor(mmc, ios);
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001057
1058 mmc_omap_select_slot(slot, 0);
1059
1060 if (ios->vdd != slot->vdd)
1061 slot->vdd = ios->vdd;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001062
1063 switch (ios->power_mode) {
1064 case MMC_POWER_OFF:
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001065 mmc_omap_set_power(slot, 0, ios->vdd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001066 break;
1067 case MMC_POWER_UP:
Tony Lindgren46a67302007-05-01 16:34:16 +02001068 /* Cannot touch dsor yet, just power up MMC */
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001069 mmc_omap_set_power(slot, 1, ios->vdd);
1070 goto exit;
Tony Lindgren46a67302007-05-01 16:34:16 +02001071 case MMC_POWER_ON:
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001072 dsor |= 1 << 11;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001073 break;
1074 }
1075
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001076 if (slot->bus_mode != ios->bus_mode) {
1077 if (slot->pdata->set_bus_mode != NULL)
1078 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1079 ios->bus_mode);
1080 slot->bus_mode = ios->bus_mode;
1081 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001082
1083 /* On insanely high arm_per frequencies something sometimes
1084 * goes somehow out of sync, and the POW bit is not being set,
1085 * which results in the while loop below getting stuck.
1086 * Writing to the CON register twice seems to do the trick. */
1087 for (i = 0; i < 2; i++)
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001088 OMAP_MMC_WRITE(host, CON, dsor);
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001089 slot->saved_con = dsor;
Tony Lindgren46a67302007-05-01 16:34:16 +02001090 if (ios->power_mode == MMC_POWER_ON) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001091 /* Send clock cycles, poll completion */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001092 OMAP_MMC_WRITE(host, IE, 0);
1093 OMAP_MMC_WRITE(host, STAT, 0xffff);
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001094 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1095 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001096 OMAP_MMC_WRITE(host, STAT, 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001097 }
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001098
1099exit:
1100 mmc_omap_release_slot(slot);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001101}
1102
David Brownellab7aefd2006-11-12 17:55:30 -08001103static const struct mmc_host_ops mmc_omap_ops = {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001104 .request = mmc_omap_request,
1105 .set_ios = mmc_omap_set_ios,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001106};
1107
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001108static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1109{
1110 struct mmc_omap_slot *slot = NULL;
1111 struct mmc_host *mmc;
1112 int r;
1113
1114 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1115 if (mmc == NULL)
1116 return -ENOMEM;
1117
1118 slot = mmc_priv(mmc);
1119 slot->host = host;
1120 slot->mmc = mmc;
1121 slot->id = id;
1122 slot->pdata = &host->pdata->slots[id];
1123
1124 host->slots[id] = slot;
1125
1126 mmc->caps = MMC_CAP_MULTIWRITE;
1127 if (host->pdata->conf.wire4)
1128 mmc->caps |= MMC_CAP_4_BIT_DATA;
1129
1130 mmc->ops = &mmc_omap_ops;
1131 mmc->f_min = 400000;
1132
1133 if (cpu_class_is_omap2())
1134 mmc->f_max = 48000000;
1135 else
1136 mmc->f_max = 24000000;
1137 if (host->pdata->max_freq)
1138 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1139 mmc->ocr_avail = slot->pdata->ocr_mask;
1140
1141 /* Use scatterlist DMA to reduce per-transfer costs.
1142 * NOTE max_seg_size assumption that small blocks aren't
1143 * normally used (except e.g. for reading SD registers).
1144 */
1145 mmc->max_phys_segs = 32;
1146 mmc->max_hw_segs = 32;
1147 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1148 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1149 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1150 mmc->max_seg_size = mmc->max_req_size;
1151
1152 r = mmc_add_host(mmc);
1153 if (r < 0)
1154 goto err_remove_host;
1155
1156 if (slot->pdata->name != NULL) {
1157 r = device_create_file(&mmc->class_dev,
1158 &dev_attr_slot_name);
1159 if (r < 0)
1160 goto err_remove_host;
1161 }
1162
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001163 if (slot->pdata->get_cover_state != NULL) {
1164 r = device_create_file(&mmc->class_dev,
1165 &dev_attr_cover_switch);
1166 if (r < 0)
1167 goto err_remove_slot_name;
1168
1169 INIT_WORK(&slot->switch_work, mmc_omap_cover_handler);
1170 init_timer(&slot->switch_timer);
1171 slot->switch_timer.function = mmc_omap_switch_timer;
1172 slot->switch_timer.data = (unsigned long) slot;
1173 schedule_work(&slot->switch_work);
1174 }
1175
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001176 return 0;
1177
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001178err_remove_slot_name:
1179 if (slot->pdata->name != NULL)
1180 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001181err_remove_host:
1182 mmc_remove_host(mmc);
1183 mmc_free_host(mmc);
1184 return r;
1185}
1186
1187static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1188{
1189 struct mmc_host *mmc = slot->mmc;
1190
1191 if (slot->pdata->name != NULL)
1192 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001193 if (slot->pdata->get_cover_state != NULL)
1194 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1195
1196 del_timer_sync(&slot->switch_timer);
1197 flush_scheduled_work();
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001198
1199 mmc_remove_host(mmc);
1200 mmc_free_host(mmc);
1201}
1202
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001203static int __init mmc_omap_probe(struct platform_device *pdev)
1204{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001205 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001206 struct mmc_omap_host *host = NULL;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001207 struct resource *res;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001208 int i, ret = 0;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001209 int irq;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001210
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001211 if (pdata == NULL) {
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001212 dev_err(&pdev->dev, "platform data missing\n");
1213 return -ENXIO;
1214 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001215 if (pdata->nr_slots == 0) {
1216 dev_err(&pdev->dev, "no slots\n");
1217 return -ENXIO;
1218 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001219
1220 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001221 irq = platform_get_irq(pdev, 0);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001222 if (res == NULL || irq < 0)
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001223 return -ENXIO;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001224
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001225 res = request_mem_region(res->start, res->end - res->start + 1,
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001226 pdev->name);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001227 if (res == NULL)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001228 return -EBUSY;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001229
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001230 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1231 if (host == NULL) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001232 ret = -ENOMEM;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001233 goto err_free_mem_region;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001234 }
1235
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001236 spin_lock_init(&host->dma_lock);
1237 init_timer(&host->dma_timer);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001238 spin_lock_init(&host->slot_lock);
1239 init_waitqueue_head(&host->slot_wq);
1240
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001241 host->dma_timer.function = mmc_omap_dma_timer;
1242 host->dma_timer.data = (unsigned long) host;
1243
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001244 host->pdata = pdata;
1245 host->dev = &pdev->dev;
1246 platform_set_drvdata(pdev, host);
1247
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001248 host->id = pdev->id;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001249 host->mem_res = res;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001250 host->irq = irq;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001251
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001252 host->use_dma = 1;
1253 host->dma_ch = -1;
1254
1255 host->irq = irq;
1256 host->phys_base = host->mem_res->start;
1257 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1258
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001259 if (cpu_is_omap24xx()) {
1260 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1261 if (IS_ERR(host->iclk))
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001262 goto err_free_mmc_host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001263 clk_enable(host->iclk);
1264 }
1265
1266 if (!cpu_is_omap24xx())
1267 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1268 else
1269 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1270
1271 if (IS_ERR(host->fclk)) {
1272 ret = PTR_ERR(host->fclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001273 goto err_free_iclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001274 }
1275
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001276 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1277 if (ret)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001278 goto err_free_fclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001279
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001280 if (pdata->init != NULL) {
1281 ret = pdata->init(&pdev->dev);
1282 if (ret < 0)
1283 goto err_free_irq;
1284 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001285
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001286 host->nr_slots = pdata->nr_slots;
1287 for (i = 0; i < pdata->nr_slots; i++) {
1288 ret = mmc_omap_new_slot(host, i);
1289 if (ret < 0) {
1290 while (--i >= 0)
1291 mmc_omap_remove_slot(host->slots[i]);
1292
1293 goto err_plat_cleanup;
1294 }
1295 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001296
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001297 return 0;
1298
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001299err_plat_cleanup:
1300 if (pdata->cleanup)
1301 pdata->cleanup(&pdev->dev);
1302err_free_irq:
1303 free_irq(host->irq, host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001304err_free_fclk:
1305 clk_put(host->fclk);
1306err_free_iclk:
1307 if (host->iclk != NULL) {
1308 clk_disable(host->iclk);
1309 clk_put(host->iclk);
1310 }
1311err_free_mmc_host:
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001312 kfree(host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001313err_free_mem_region:
1314 release_mem_region(res->start, res->end - res->start + 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001315 return ret;
1316}
1317
1318static int mmc_omap_remove(struct platform_device *pdev)
1319{
1320 struct mmc_omap_host *host = platform_get_drvdata(pdev);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001321 int i;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001322
1323 platform_set_drvdata(pdev, NULL);
1324
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001325 BUG_ON(host == NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001326
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001327 for (i = 0; i < host->nr_slots; i++)
1328 mmc_omap_remove_slot(host->slots[i]);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001329
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001330 if (host->pdata->cleanup)
1331 host->pdata->cleanup(&pdev->dev);
1332
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001333 if (host->iclk && !IS_ERR(host->iclk))
1334 clk_put(host->iclk);
1335 if (host->fclk && !IS_ERR(host->fclk))
1336 clk_put(host->fclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001337
1338 release_mem_region(pdev->resource[0].start,
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001339 pdev->resource[0].end - pdev->resource[0].start + 1);
1340
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001341 kfree(host);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001342
1343 return 0;
1344}
1345
1346#ifdef CONFIG_PM
1347static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1348{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001349 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001350 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1351
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001352 if (host == NULL || host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001353 return 0;
1354
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001355 for (i = 0; i < host->nr_slots; i++) {
1356 struct mmc_omap_slot *slot;
1357
1358 slot = host->slots[i];
1359 ret = mmc_suspend_host(slot->mmc, mesg);
1360 if (ret < 0) {
1361 while (--i >= 0) {
1362 slot = host->slots[i];
1363 mmc_resume_host(slot->mmc);
1364 }
1365 return ret;
1366 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001367 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001368 host->suspended = 1;
1369 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001370}
1371
1372static int mmc_omap_resume(struct platform_device *pdev)
1373{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001374 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001375 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1376
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001377 if (host == NULL || !host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001378 return 0;
1379
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001380 for (i = 0; i < host->nr_slots; i++) {
1381 struct mmc_omap_slot *slot;
1382 slot = host->slots[i];
1383 ret = mmc_resume_host(slot->mmc);
1384 if (ret < 0)
1385 return ret;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001386
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001387 host->suspended = 0;
1388 }
1389 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001390}
1391#else
1392#define mmc_omap_suspend NULL
1393#define mmc_omap_resume NULL
1394#endif
1395
1396static struct platform_driver mmc_omap_driver = {
1397 .probe = mmc_omap_probe,
1398 .remove = mmc_omap_remove,
1399 .suspend = mmc_omap_suspend,
1400 .resume = mmc_omap_resume,
1401 .driver = {
1402 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001403 .owner = THIS_MODULE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001404 },
1405};
1406
1407static int __init mmc_omap_init(void)
1408{
1409 return platform_driver_register(&mmc_omap_driver);
1410}
1411
1412static void __exit mmc_omap_exit(void)
1413{
1414 platform_driver_unregister(&mmc_omap_driver);
1415}
1416
1417module_init(mmc_omap_init);
1418module_exit(mmc_omap_exit);
1419
1420MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1421MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001422MODULE_ALIAS("platform:" DRIVER_NAME);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001423MODULE_AUTHOR("Juha Yrjölä");