blob: 59eac7211842a9502699e4d0928ec12164762144 [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
109 struct mmc_request *mrq;
110 struct mmc_omap_host *host;
111 struct mmc_host *mmc;
112 struct omap_mmc_slot_data *pdata;
113};
114
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100115struct mmc_omap_host {
116 int initialized;
117 int suspended;
118 struct mmc_request * mrq;
119 struct mmc_command * cmd;
120 struct mmc_data * data;
121 struct mmc_host * mmc;
122 struct device * dev;
123 unsigned char id; /* 16xx chips have 2 MMC blocks */
124 struct clk * iclk;
125 struct clk * fclk;
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100126 struct resource *mem_res;
127 void __iomem *virt_base;
128 unsigned int phys_base;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100129 int irq;
130 unsigned char bus_mode;
131 unsigned char hw_bus_mode;
132
133 unsigned int sg_len;
134 int sg_idx;
135 u16 * buffer;
136 u32 buffer_bytes_left;
137 u32 total_bytes_left;
138
139 unsigned use_dma:1;
140 unsigned brs_received:1, dma_done:1;
141 unsigned dma_is_read:1;
142 unsigned dma_in_use:1;
143 int dma_ch;
144 spinlock_t dma_lock;
145 struct timer_list dma_timer;
146 unsigned dma_len;
147
148 short power_pin;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100149
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400150 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
151 struct mmc_omap_slot *current_slot;
152 spinlock_t slot_lock;
153 wait_queue_head_t slot_wq;
154 int nr_slots;
155
156 struct omap_mmc_platform_data *pdata;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100157};
158
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400159static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
160{
161 struct mmc_omap_host *host = slot->host;
162 unsigned long flags;
163
164 if (claimed)
165 goto no_claim;
166 spin_lock_irqsave(&host->slot_lock, flags);
167 while (host->mmc != NULL) {
168 spin_unlock_irqrestore(&host->slot_lock, flags);
169 wait_event(host->slot_wq, host->mmc == NULL);
170 spin_lock_irqsave(&host->slot_lock, flags);
171 }
172 host->mmc = slot->mmc;
173 spin_unlock_irqrestore(&host->slot_lock, flags);
174no_claim:
175 clk_enable(host->fclk);
176 if (host->current_slot != slot) {
177 if (host->pdata->switch_slot != NULL)
178 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
179 host->current_slot = slot;
180 }
181
182 /* Doing the dummy read here seems to work around some bug
183 * at least in OMAP24xx silicon where the command would not
184 * start after writing the CMD register. Sigh. */
185 OMAP_MMC_READ(host, CON);
186
187 OMAP_MMC_WRITE(host, CON, slot->saved_con);
188}
189
190static void mmc_omap_start_request(struct mmc_omap_host *host,
191 struct mmc_request *req);
192
193static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
194{
195 struct mmc_omap_host *host = slot->host;
196 unsigned long flags;
197 int i;
198
199 BUG_ON(slot == NULL || host->mmc == NULL);
200 clk_disable(host->fclk);
201
202 spin_lock_irqsave(&host->slot_lock, flags);
203 /* Check for any pending requests */
204 for (i = 0; i < host->nr_slots; i++) {
205 struct mmc_omap_slot *new_slot;
206 struct mmc_request *rq;
207
208 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
209 continue;
210
211 new_slot = host->slots[i];
212 /* The current slot should not have a request in queue */
213 BUG_ON(new_slot == host->current_slot);
214
215 host->mmc = new_slot->mmc;
216 spin_unlock_irqrestore(&host->slot_lock, flags);
217 mmc_omap_select_slot(new_slot, 1);
218 rq = new_slot->mrq;
219 new_slot->mrq = NULL;
220 mmc_omap_start_request(host, rq);
221 return;
222 }
223
224 host->mmc = NULL;
225 wake_up(&host->slot_wq);
226 spin_unlock_irqrestore(&host->slot_lock, flags);
227}
228
229static ssize_t
230mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
231 char *buf)
232{
233 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
234 struct mmc_omap_slot *slot = mmc_priv(mmc);
235
236 return sprintf(buf, "%s\n", slot->pdata->name);
237}
238
239static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
240
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100241static void
242mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
243{
244 u32 cmdreg;
245 u32 resptype;
246 u32 cmdtype;
247
248 host->cmd = cmd;
249
250 resptype = 0;
251 cmdtype = 0;
252
253 /* Our hardware needs to know exact type */
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100254 switch (mmc_resp_type(cmd)) {
255 case MMC_RSP_NONE:
256 break;
257 case MMC_RSP_R1:
258 case MMC_RSP_R1B:
Philip Langdale6f949902007-01-04 07:04:47 -0800259 /* resp 1, 1b, 6, 7 */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100260 resptype = 1;
261 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100262 case MMC_RSP_R2:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100263 resptype = 2;
264 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100265 case MMC_RSP_R3:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100266 resptype = 3;
267 break;
268 default:
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100269 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100270 break;
271 }
272
273 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
274 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
275 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
276 cmdtype = OMAP_MMC_CMDTYPE_BC;
277 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
278 cmdtype = OMAP_MMC_CMDTYPE_BCR;
279 } else {
280 cmdtype = OMAP_MMC_CMDTYPE_AC;
281 }
282
283 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
284
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400285 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100286 cmdreg |= 1 << 6;
287
288 if (cmd->flags & MMC_RSP_BUSY)
289 cmdreg |= 1 << 11;
290
291 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
292 cmdreg |= 1 << 15;
293
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100294 OMAP_MMC_WRITE(host, CTO, 200);
295 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
296 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
297 OMAP_MMC_WRITE(host, IE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100298 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
299 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
300 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
301 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
302 OMAP_MMC_STAT_END_OF_DATA);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100303 OMAP_MMC_WRITE(host, CMD, cmdreg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100304}
305
306static void
307mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
308{
309 if (host->dma_in_use) {
310 enum dma_data_direction dma_data_dir;
311
312 BUG_ON(host->dma_ch < 0);
Pierre Ossman17b04292007-07-22 22:18:46 +0200313 if (data->error)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100314 omap_stop_dma(host->dma_ch);
315 /* Release DMA channel lazily */
316 mod_timer(&host->dma_timer, jiffies + HZ);
317 if (data->flags & MMC_DATA_WRITE)
318 dma_data_dir = DMA_TO_DEVICE;
319 else
320 dma_data_dir = DMA_FROM_DEVICE;
321 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
322 dma_data_dir);
323 }
324 host->data = NULL;
325 host->sg_len = 0;
326 clk_disable(host->fclk);
327
328 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
329 * dozens of requests until the card finishes writing data.
330 * It'd be cheaper to just wait till an EOFB interrupt arrives...
331 */
332
333 if (!data->stop) {
334 host->mrq = NULL;
335 mmc_request_done(host->mmc, data->mrq);
336 return;
337 }
338
339 mmc_omap_start_command(host, data->stop);
340}
341
342static void
343mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
344{
345 unsigned long flags;
346 int done;
347
348 if (!host->dma_in_use) {
349 mmc_omap_xfer_done(host, data);
350 return;
351 }
352 done = 0;
353 spin_lock_irqsave(&host->dma_lock, flags);
354 if (host->dma_done)
355 done = 1;
356 else
357 host->brs_received = 1;
358 spin_unlock_irqrestore(&host->dma_lock, flags);
359 if (done)
360 mmc_omap_xfer_done(host, data);
361}
362
363static void
364mmc_omap_dma_timer(unsigned long data)
365{
366 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
367
368 BUG_ON(host->dma_ch < 0);
369 omap_free_dma(host->dma_ch);
370 host->dma_ch = -1;
371}
372
373static void
374mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
375{
376 unsigned long flags;
377 int done;
378
379 done = 0;
380 spin_lock_irqsave(&host->dma_lock, flags);
381 if (host->brs_received)
382 done = 1;
383 else
384 host->dma_done = 1;
385 spin_unlock_irqrestore(&host->dma_lock, flags);
386 if (done)
387 mmc_omap_xfer_done(host, data);
388}
389
390static void
391mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
392{
393 host->cmd = NULL;
394
395 if (cmd->flags & MMC_RSP_PRESENT) {
396 if (cmd->flags & MMC_RSP_136) {
397 /* response type 2 */
398 cmd->resp[3] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100399 OMAP_MMC_READ(host, RSP0) |
400 (OMAP_MMC_READ(host, RSP1) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100401 cmd->resp[2] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100402 OMAP_MMC_READ(host, RSP2) |
403 (OMAP_MMC_READ(host, RSP3) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100404 cmd->resp[1] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100405 OMAP_MMC_READ(host, RSP4) |
406 (OMAP_MMC_READ(host, RSP5) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100407 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100408 OMAP_MMC_READ(host, RSP6) |
409 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100410 } else {
411 /* response types 1, 1b, 3, 4, 5, 6 */
412 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100413 OMAP_MMC_READ(host, RSP6) |
414 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100415 }
416 }
417
Pierre Ossman17b04292007-07-22 22:18:46 +0200418 if (host->data == NULL || cmd->error) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100419 host->mrq = NULL;
420 clk_disable(host->fclk);
421 mmc_request_done(host->mmc, cmd->mrq);
422 }
423}
424
425/* PIO only */
426static void
427mmc_omap_sg_to_buf(struct mmc_omap_host *host)
428{
429 struct scatterlist *sg;
430
431 sg = host->data->sg + host->sg_idx;
432 host->buffer_bytes_left = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +0200433 host->buffer = sg_virt(sg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100434 if (host->buffer_bytes_left > host->total_bytes_left)
435 host->buffer_bytes_left = host->total_bytes_left;
436}
437
438/* PIO only */
439static void
440mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
441{
442 int n;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100443
444 if (host->buffer_bytes_left == 0) {
445 host->sg_idx++;
446 BUG_ON(host->sg_idx == host->sg_len);
447 mmc_omap_sg_to_buf(host);
448 }
449 n = 64;
450 if (n > host->buffer_bytes_left)
451 n = host->buffer_bytes_left;
452 host->buffer_bytes_left -= n;
453 host->total_bytes_left -= n;
454 host->data->bytes_xfered += n;
455
456 if (write) {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100457 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100458 } else {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100459 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100460 }
461}
462
463static inline void mmc_omap_report_irq(u16 status)
464{
465 static const char *mmc_omap_status_bits[] = {
466 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
467 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
468 };
469 int i, c = 0;
470
471 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
472 if (status & (1 << i)) {
473 if (c)
474 printk(" ");
475 printk("%s", mmc_omap_status_bits[i]);
476 c++;
477 }
478}
479
David Howells7d12e782006-10-05 14:55:46 +0100480static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100481{
482 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
483 u16 status;
484 int end_command;
485 int end_transfer;
486 int transfer_error;
487
488 if (host->cmd == NULL && host->data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100489 status = OMAP_MMC_READ(host, STAT);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100490 dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
491 if (status != 0) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100492 OMAP_MMC_WRITE(host, STAT, status);
493 OMAP_MMC_WRITE(host, IE, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100494 }
495 return IRQ_HANDLED;
496 }
497
498 end_command = 0;
499 end_transfer = 0;
500 transfer_error = 0;
501
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100502 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
503 OMAP_MMC_WRITE(host, STAT, status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100504#ifdef CONFIG_MMC_DEBUG
505 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
506 status, host->cmd != NULL ? host->cmd->opcode : -1);
507 mmc_omap_report_irq(status);
508 printk("\n");
509#endif
510 if (host->total_bytes_left) {
511 if ((status & OMAP_MMC_STAT_A_FULL) ||
512 (status & OMAP_MMC_STAT_END_OF_DATA))
513 mmc_omap_xfer_data(host, 0);
514 if (status & OMAP_MMC_STAT_A_EMPTY)
515 mmc_omap_xfer_data(host, 1);
516 }
517
518 if (status & OMAP_MMC_STAT_END_OF_DATA) {
519 end_transfer = 1;
520 }
521
522 if (status & OMAP_MMC_STAT_DATA_TOUT) {
523 dev_dbg(mmc_dev(host->mmc), "data timeout\n");
524 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200525 host->data->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100526 transfer_error = 1;
527 }
528 }
529
530 if (status & OMAP_MMC_STAT_DATA_CRC) {
531 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200532 host->data->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100533 dev_dbg(mmc_dev(host->mmc),
534 "data CRC error, bytes left %d\n",
535 host->total_bytes_left);
536 transfer_error = 1;
537 } else {
538 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
539 }
540 }
541
542 if (status & OMAP_MMC_STAT_CMD_TOUT) {
543 /* Timeouts are routine with some commands */
544 if (host->cmd) {
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400545 struct mmc_omap_slot *slot =
546 host->current_slot;
Tony Lindgren4bc9e352008-03-26 16:08:53 -0400547 dev_err(mmc_dev(host->mmc),
Carlos Eduardo Aguiar5ec21b12008-03-26 16:08:41 -0400548 "command timeout, CMD %d\n",
549 host->cmd->opcode);
Pierre Ossman17b04292007-07-22 22:18:46 +0200550 host->cmd->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100551 end_command = 1;
552 }
553 }
554
555 if (status & OMAP_MMC_STAT_CMD_CRC) {
556 if (host->cmd) {
557 dev_err(mmc_dev(host->mmc),
558 "command CRC error (CMD%d, arg 0x%08x)\n",
559 host->cmd->opcode, host->cmd->arg);
Pierre Ossman17b04292007-07-22 22:18:46 +0200560 host->cmd->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100561 end_command = 1;
562 } else
563 dev_err(mmc_dev(host->mmc),
564 "command CRC error without cmd?\n");
565 }
566
567 if (status & OMAP_MMC_STAT_CARD_ERR) {
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200568 dev_dbg(mmc_dev(host->mmc),
569 "ignoring card status error (CMD%d)\n",
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100570 host->cmd->opcode);
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200571 end_command = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100572 }
573
574 /*
575 * NOTE: On 1610 the END_OF_CMD may come too early when
576 * starting a write
577 */
578 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
579 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
580 end_command = 1;
581 }
582 }
583
584 if (end_command) {
585 mmc_omap_cmd_done(host, host->cmd);
586 }
587 if (transfer_error)
588 mmc_omap_xfer_done(host, host->data);
589 else if (end_transfer)
590 mmc_omap_end_of_data(host, host->data);
591
592 return IRQ_HANDLED;
593}
594
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100595/* Prepare to transfer the next segment of a scatterlist */
596static void
597mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
598{
599 int dma_ch = host->dma_ch;
600 unsigned long data_addr;
601 u16 buf, frame;
602 u32 count;
603 struct scatterlist *sg = &data->sg[host->sg_idx];
604 int src_port = 0;
605 int dst_port = 0;
606 int sync_dev = 0;
607
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100608 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
Russell Kinga3fd4a12006-06-04 17:51:15 +0100609 frame = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100610 count = sg_dma_len(sg);
611
Russell Kinga3fd4a12006-06-04 17:51:15 +0100612 if ((data->blocks == 1) && (count > data->blksz))
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100613 count = frame;
614
615 host->dma_len = count;
616
617 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
618 * Use 16 or 32 word frames when the blocksize is at least that large.
619 * Blocksize is usually 512 bytes; but not for some SD reads.
620 */
621 if (cpu_is_omap15xx() && frame > 32)
622 frame = 32;
623 else if (frame > 64)
624 frame = 64;
625 count /= frame;
626 frame >>= 1;
627
628 if (!(data->flags & MMC_DATA_WRITE)) {
629 buf = 0x800f | ((frame - 1) << 8);
630
631 if (cpu_class_is_omap1()) {
632 src_port = OMAP_DMA_PORT_TIPB;
633 dst_port = OMAP_DMA_PORT_EMIFF;
634 }
635 if (cpu_is_omap24xx())
636 sync_dev = OMAP24XX_DMA_MMC1_RX;
637
638 omap_set_dma_src_params(dma_ch, src_port,
639 OMAP_DMA_AMODE_CONSTANT,
640 data_addr, 0, 0);
641 omap_set_dma_dest_params(dma_ch, dst_port,
642 OMAP_DMA_AMODE_POST_INC,
643 sg_dma_address(sg), 0, 0);
644 omap_set_dma_dest_data_pack(dma_ch, 1);
645 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
646 } else {
647 buf = 0x0f80 | ((frame - 1) << 0);
648
649 if (cpu_class_is_omap1()) {
650 src_port = OMAP_DMA_PORT_EMIFF;
651 dst_port = OMAP_DMA_PORT_TIPB;
652 }
653 if (cpu_is_omap24xx())
654 sync_dev = OMAP24XX_DMA_MMC1_TX;
655
656 omap_set_dma_dest_params(dma_ch, dst_port,
657 OMAP_DMA_AMODE_CONSTANT,
658 data_addr, 0, 0);
659 omap_set_dma_src_params(dma_ch, src_port,
660 OMAP_DMA_AMODE_POST_INC,
661 sg_dma_address(sg), 0, 0);
662 omap_set_dma_src_data_pack(dma_ch, 1);
663 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
664 }
665
666 /* Max limit for DMA frame count is 0xffff */
Eric Sesterhennd99c5902006-11-30 05:27:38 +0100667 BUG_ON(count > 0xffff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100668
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100669 OMAP_MMC_WRITE(host, BUF, buf);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100670 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
671 frame, count, OMAP_DMA_SYNC_FRAME,
672 sync_dev, 0);
673}
674
675/* A scatterlist segment completed */
676static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
677{
678 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
679 struct mmc_data *mmcdat = host->data;
680
681 if (unlikely(host->dma_ch < 0)) {
Tony Lindgrence9c1a82006-07-01 19:56:44 +0100682 dev_err(mmc_dev(host->mmc),
683 "DMA callback while DMA not enabled\n");
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100684 return;
685 }
686 /* FIXME: We really should do something to _handle_ the errors */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700687 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100688 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
689 return;
690 }
691 if (ch_status & OMAP_DMA_DROP_IRQ) {
692 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
693 return;
694 }
695 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
696 return;
697 }
698 mmcdat->bytes_xfered += host->dma_len;
699 host->sg_idx++;
700 if (host->sg_idx < host->sg_len) {
701 mmc_omap_prepare_dma(host, host->data);
702 omap_start_dma(host->dma_ch);
703 } else
704 mmc_omap_dma_done(host, host->data);
705}
706
707static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
708{
709 const char *dev_name;
710 int sync_dev, dma_ch, is_read, r;
711
712 is_read = !(data->flags & MMC_DATA_WRITE);
713 del_timer_sync(&host->dma_timer);
714 if (host->dma_ch >= 0) {
715 if (is_read == host->dma_is_read)
716 return 0;
717 omap_free_dma(host->dma_ch);
718 host->dma_ch = -1;
719 }
720
721 if (is_read) {
722 if (host->id == 1) {
723 sync_dev = OMAP_DMA_MMC_RX;
724 dev_name = "MMC1 read";
725 } else {
726 sync_dev = OMAP_DMA_MMC2_RX;
727 dev_name = "MMC2 read";
728 }
729 } else {
730 if (host->id == 1) {
731 sync_dev = OMAP_DMA_MMC_TX;
732 dev_name = "MMC1 write";
733 } else {
734 sync_dev = OMAP_DMA_MMC2_TX;
735 dev_name = "MMC2 write";
736 }
737 }
738 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
739 host, &dma_ch);
740 if (r != 0) {
741 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
742 return r;
743 }
744 host->dma_ch = dma_ch;
745 host->dma_is_read = is_read;
746
747 return 0;
748}
749
750static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
751{
752 u16 reg;
753
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100754 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100755 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100756 OMAP_MMC_WRITE(host, SDIO, reg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100757 /* Set maximum timeout */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100758 OMAP_MMC_WRITE(host, CTO, 0xff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100759}
760
761static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
762{
763 int timeout;
764 u16 reg;
765
766 /* Convert ns to clock cycles by assuming 20MHz frequency
767 * 1 cycle at 20MHz = 500 ns
768 */
769 timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
770
771 /* Check if we need to use timeout multiplier register */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100772 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100773 if (timeout > 0xffff) {
774 reg |= (1 << 5);
775 timeout /= 1024;
776 } else
777 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100778 OMAP_MMC_WRITE(host, SDIO, reg);
779 OMAP_MMC_WRITE(host, DTO, timeout);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100780}
781
782static void
783mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
784{
785 struct mmc_data *data = req->data;
786 int i, use_dma, block_size;
787 unsigned sg_len;
788
789 host->data = data;
790 if (data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100791 OMAP_MMC_WRITE(host, BLEN, 0);
792 OMAP_MMC_WRITE(host, NBLK, 0);
793 OMAP_MMC_WRITE(host, BUF, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100794 host->dma_in_use = 0;
795 set_cmd_timeout(host, req);
796 return;
797 }
798
Russell Kinga3fd4a12006-06-04 17:51:15 +0100799 block_size = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100800
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100801 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
802 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100803 set_data_timeout(host, req);
804
805 /* cope with calling layer confusion; it issues "single
806 * block" writes using multi-block scatterlists.
807 */
808 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
809
810 /* Only do DMA for entire blocks */
811 use_dma = host->use_dma;
812 if (use_dma) {
813 for (i = 0; i < sg_len; i++) {
814 if ((data->sg[i].length % block_size) != 0) {
815 use_dma = 0;
816 break;
817 }
818 }
819 }
820
821 host->sg_idx = 0;
822 if (use_dma) {
823 if (mmc_omap_get_dma_channel(host, data) == 0) {
824 enum dma_data_direction dma_data_dir;
825
826 if (data->flags & MMC_DATA_WRITE)
827 dma_data_dir = DMA_TO_DEVICE;
828 else
829 dma_data_dir = DMA_FROM_DEVICE;
830
831 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
832 sg_len, dma_data_dir);
833 host->total_bytes_left = 0;
834 mmc_omap_prepare_dma(host, req->data);
835 host->brs_received = 0;
836 host->dma_done = 0;
837 host->dma_in_use = 1;
838 } else
839 use_dma = 0;
840 }
841
842 /* Revert to PIO? */
843 if (!use_dma) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100844 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100845 host->total_bytes_left = data->blocks * block_size;
846 host->sg_len = sg_len;
847 mmc_omap_sg_to_buf(host);
848 host->dma_in_use = 0;
849 }
850}
851
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400852static void mmc_omap_start_request(struct mmc_omap_host *host,
853 struct mmc_request *req)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100854{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400855 BUG_ON(host->mrq != NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100856
857 host->mrq = req;
858
859 /* only touch fifo AFTER the controller readies it */
860 mmc_omap_prepare_data(host, req);
861 mmc_omap_start_command(host, req->cmd);
862 if (host->dma_in_use)
863 omap_start_dma(host->dma_ch);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400864 BUG_ON(irqs_disabled());
865}
866
867static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
868{
869 struct mmc_omap_slot *slot = mmc_priv(mmc);
870 struct mmc_omap_host *host = slot->host;
871 unsigned long flags;
872
873 spin_lock_irqsave(&host->slot_lock, flags);
874 if (host->mmc != NULL) {
875 BUG_ON(slot->mrq != NULL);
876 slot->mrq = req;
877 spin_unlock_irqrestore(&host->slot_lock, flags);
878 return;
879 } else
880 host->mmc = mmc;
881 spin_unlock_irqrestore(&host->slot_lock, flags);
882 mmc_omap_select_slot(slot, 1);
883 mmc_omap_start_request(host, req);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100884}
885
886static void innovator_fpga_socket_power(int on)
887{
888#if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100889 if (on) {
890 fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
891 OMAP1510_FPGA_POWER);
892 } else {
893 fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
894 OMAP1510_FPGA_POWER);
895 }
896#endif
897}
898
899/*
900 * Turn the socket power on/off. Innovator uses FPGA, most boards
901 * probably use GPIO.
902 */
903static void mmc_omap_power(struct mmc_omap_host *host, int on)
904{
905 if (on) {
906 if (machine_is_omap_innovator())
907 innovator_fpga_socket_power(1);
908 else if (machine_is_omap_h2())
909 tps65010_set_gpio_out_value(GPIO3, HIGH);
910 else if (machine_is_omap_h3())
911 /* GPIO 4 of TPS65010 sends SD_EN signal */
912 tps65010_set_gpio_out_value(GPIO4, HIGH);
913 else if (cpu_is_omap24xx()) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100914 u16 reg = OMAP_MMC_READ(host, CON);
915 OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100916 } else
917 if (host->power_pin >= 0)
918 omap_set_gpio_dataout(host->power_pin, 1);
919 } else {
920 if (machine_is_omap_innovator())
921 innovator_fpga_socket_power(0);
922 else if (machine_is_omap_h2())
923 tps65010_set_gpio_out_value(GPIO3, LOW);
924 else if (machine_is_omap_h3())
925 tps65010_set_gpio_out_value(GPIO4, LOW);
926 else if (cpu_is_omap24xx()) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100927 u16 reg = OMAP_MMC_READ(host, CON);
928 OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100929 } else
930 if (host->power_pin >= 0)
931 omap_set_gpio_dataout(host->power_pin, 0);
932 }
933}
934
Tony Lindgrend3af5ab2007-05-01 16:36:00 +0200935static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
936{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400937 struct mmc_omap_slot *slot = mmc_priv(mmc);
938 struct mmc_omap_host *host = slot->host;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +0200939 int func_clk_rate = clk_get_rate(host->fclk);
940 int dsor;
941
942 if (ios->clock == 0)
943 return 0;
944
945 dsor = func_clk_rate / ios->clock;
946 if (dsor < 1)
947 dsor = 1;
948
949 if (func_clk_rate / dsor > ios->clock)
950 dsor++;
951
952 if (dsor > 250)
953 dsor = 250;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +0200954
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400955 slot->fclk_freq = func_clk_rate / dsor;
956
Tony Lindgrend3af5ab2007-05-01 16:36:00 +0200957 if (ios->bus_width == MMC_BUS_WIDTH_4)
958 dsor |= 1 << 15;
959
960 return dsor;
961}
962
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100963static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
964{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400965 struct mmc_omap_slot *slot = mmc_priv(mmc);
966 struct mmc_omap_host *host = slot->host;
967 int i, dsor;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100968
Tony Lindgrend3af5ab2007-05-01 16:36:00 +0200969 dsor = mmc_omap_calc_divisor(mmc, ios);
970 host->bus_mode = ios->bus_mode;
971 host->hw_bus_mode = host->bus_mode;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100972
973 switch (ios->power_mode) {
974 case MMC_POWER_OFF:
975 mmc_omap_power(host, 0);
976 break;
977 case MMC_POWER_UP:
Tony Lindgren46a67302007-05-01 16:34:16 +0200978 /* Cannot touch dsor yet, just power up MMC */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100979 mmc_omap_power(host, 1);
Tony Lindgren46a67302007-05-01 16:34:16 +0200980 return;
981 case MMC_POWER_ON:
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +0100982 dsor |= 1 << 11;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100983 break;
984 }
985
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100986 clk_enable(host->fclk);
987
988 /* On insanely high arm_per frequencies something sometimes
989 * goes somehow out of sync, and the POW bit is not being set,
990 * which results in the while loop below getting stuck.
991 * Writing to the CON register twice seems to do the trick. */
992 for (i = 0; i < 2; i++)
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100993 OMAP_MMC_WRITE(host, CON, dsor);
Tony Lindgren46a67302007-05-01 16:34:16 +0200994 if (ios->power_mode == MMC_POWER_ON) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100995 /* Send clock cycles, poll completion */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100996 OMAP_MMC_WRITE(host, IE, 0);
997 OMAP_MMC_WRITE(host, STAT, 0xffff);
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +0100998 OMAP_MMC_WRITE(host, CMD, 1 << 7);
999 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001000 OMAP_MMC_WRITE(host, STAT, 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001001 }
1002 clk_disable(host->fclk);
1003}
1004
David Brownellab7aefd2006-11-12 17:55:30 -08001005static const struct mmc_host_ops mmc_omap_ops = {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001006 .request = mmc_omap_request,
1007 .set_ios = mmc_omap_set_ios,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001008};
1009
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001010static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1011{
1012 struct mmc_omap_slot *slot = NULL;
1013 struct mmc_host *mmc;
1014 int r;
1015
1016 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1017 if (mmc == NULL)
1018 return -ENOMEM;
1019
1020 slot = mmc_priv(mmc);
1021 slot->host = host;
1022 slot->mmc = mmc;
1023 slot->id = id;
1024 slot->pdata = &host->pdata->slots[id];
1025
1026 host->slots[id] = slot;
1027
1028 mmc->caps = MMC_CAP_MULTIWRITE;
1029 if (host->pdata->conf.wire4)
1030 mmc->caps |= MMC_CAP_4_BIT_DATA;
1031
1032 mmc->ops = &mmc_omap_ops;
1033 mmc->f_min = 400000;
1034
1035 if (cpu_class_is_omap2())
1036 mmc->f_max = 48000000;
1037 else
1038 mmc->f_max = 24000000;
1039 if (host->pdata->max_freq)
1040 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1041 mmc->ocr_avail = slot->pdata->ocr_mask;
1042
1043 /* Use scatterlist DMA to reduce per-transfer costs.
1044 * NOTE max_seg_size assumption that small blocks aren't
1045 * normally used (except e.g. for reading SD registers).
1046 */
1047 mmc->max_phys_segs = 32;
1048 mmc->max_hw_segs = 32;
1049 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1050 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1051 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1052 mmc->max_seg_size = mmc->max_req_size;
1053
1054 r = mmc_add_host(mmc);
1055 if (r < 0)
1056 goto err_remove_host;
1057
1058 if (slot->pdata->name != NULL) {
1059 r = device_create_file(&mmc->class_dev,
1060 &dev_attr_slot_name);
1061 if (r < 0)
1062 goto err_remove_host;
1063 }
1064
1065 return 0;
1066
1067err_remove_host:
1068 mmc_remove_host(mmc);
1069 mmc_free_host(mmc);
1070 return r;
1071}
1072
1073static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1074{
1075 struct mmc_host *mmc = slot->mmc;
1076
1077 if (slot->pdata->name != NULL)
1078 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1079
1080 mmc_remove_host(mmc);
1081 mmc_free_host(mmc);
1082}
1083
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001084static int __init mmc_omap_probe(struct platform_device *pdev)
1085{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001086 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001087 struct mmc_omap_host *host = NULL;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001088 struct resource *res;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001089 int i, ret = 0;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001090 int irq;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001091
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001092 if (pdata == NULL) {
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001093 dev_err(&pdev->dev, "platform data missing\n");
1094 return -ENXIO;
1095 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001096 if (pdata->nr_slots == 0) {
1097 dev_err(&pdev->dev, "no slots\n");
1098 return -ENXIO;
1099 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001100
1101 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001102 irq = platform_get_irq(pdev, 0);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001103 if (res == NULL || irq < 0)
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001104 return -ENXIO;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001105
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001106 res = request_mem_region(res->start, res->end - res->start + 1,
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001107 pdev->name);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001108 if (res == NULL)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001109 return -EBUSY;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001110
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001111 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1112 if (host == NULL) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001113 ret = -ENOMEM;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001114 goto err_free_mem_region;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001115 }
1116
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001117 spin_lock_init(&host->dma_lock);
1118 init_timer(&host->dma_timer);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001119 spin_lock_init(&host->slot_lock);
1120 init_waitqueue_head(&host->slot_wq);
1121
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001122 host->dma_timer.function = mmc_omap_dma_timer;
1123 host->dma_timer.data = (unsigned long) host;
1124
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001125 host->pdata = pdata;
1126 host->dev = &pdev->dev;
1127 platform_set_drvdata(pdev, host);
1128
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001129 host->id = pdev->id;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001130 host->mem_res = res;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001131 host->irq = irq;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001132
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001133 host->use_dma = 1;
1134 host->dma_ch = -1;
1135
1136 host->irq = irq;
1137 host->phys_base = host->mem_res->start;
1138 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1139
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001140 if (cpu_is_omap24xx()) {
1141 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1142 if (IS_ERR(host->iclk))
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001143 goto err_free_mmc_host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001144 clk_enable(host->iclk);
1145 }
1146
1147 if (!cpu_is_omap24xx())
1148 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1149 else
1150 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1151
1152 if (IS_ERR(host->fclk)) {
1153 ret = PTR_ERR(host->fclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001154 goto err_free_iclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001155 }
1156
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001157 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1158 if (ret)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001159 goto err_free_fclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001160
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001161 if (pdata->init != NULL) {
1162 ret = pdata->init(&pdev->dev);
1163 if (ret < 0)
1164 goto err_free_irq;
1165 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001166
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001167 host->nr_slots = pdata->nr_slots;
1168 for (i = 0; i < pdata->nr_slots; i++) {
1169 ret = mmc_omap_new_slot(host, i);
1170 if (ret < 0) {
1171 while (--i >= 0)
1172 mmc_omap_remove_slot(host->slots[i]);
1173
1174 goto err_plat_cleanup;
1175 }
1176 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001177
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001178 return 0;
1179
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001180err_plat_cleanup:
1181 if (pdata->cleanup)
1182 pdata->cleanup(&pdev->dev);
1183err_free_irq:
1184 free_irq(host->irq, host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001185err_free_fclk:
1186 clk_put(host->fclk);
1187err_free_iclk:
1188 if (host->iclk != NULL) {
1189 clk_disable(host->iclk);
1190 clk_put(host->iclk);
1191 }
1192err_free_mmc_host:
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001193 kfree(host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001194err_free_mem_region:
1195 release_mem_region(res->start, res->end - res->start + 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001196 return ret;
1197}
1198
1199static int mmc_omap_remove(struct platform_device *pdev)
1200{
1201 struct mmc_omap_host *host = platform_get_drvdata(pdev);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001202 int i;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001203
1204 platform_set_drvdata(pdev, NULL);
1205
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001206 BUG_ON(host == NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001207
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001208 for (i = 0; i < host->nr_slots; i++)
1209 mmc_omap_remove_slot(host->slots[i]);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001210
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001211 if (host->pdata->cleanup)
1212 host->pdata->cleanup(&pdev->dev);
1213
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001214 if (host->iclk && !IS_ERR(host->iclk))
1215 clk_put(host->iclk);
1216 if (host->fclk && !IS_ERR(host->fclk))
1217 clk_put(host->fclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001218
1219 release_mem_region(pdev->resource[0].start,
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001220 pdev->resource[0].end - pdev->resource[0].start + 1);
1221
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001222 kfree(host);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001223
1224 return 0;
1225}
1226
1227#ifdef CONFIG_PM
1228static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1229{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001230 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001231 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1232
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001233 if (host == NULL || host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001234 return 0;
1235
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001236 for (i = 0; i < host->nr_slots; i++) {
1237 struct mmc_omap_slot *slot;
1238
1239 slot = host->slots[i];
1240 ret = mmc_suspend_host(slot->mmc, mesg);
1241 if (ret < 0) {
1242 while (--i >= 0) {
1243 slot = host->slots[i];
1244 mmc_resume_host(slot->mmc);
1245 }
1246 return ret;
1247 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001248 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001249 host->suspended = 1;
1250 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001251}
1252
1253static int mmc_omap_resume(struct platform_device *pdev)
1254{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001255 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001256 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1257
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001258 if (host == NULL || !host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001259 return 0;
1260
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001261 for (i = 0; i < host->nr_slots; i++) {
1262 struct mmc_omap_slot *slot;
1263 slot = host->slots[i];
1264 ret = mmc_resume_host(slot->mmc);
1265 if (ret < 0)
1266 return ret;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001267
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001268 host->suspended = 0;
1269 }
1270 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001271}
1272#else
1273#define mmc_omap_suspend NULL
1274#define mmc_omap_resume NULL
1275#endif
1276
1277static struct platform_driver mmc_omap_driver = {
1278 .probe = mmc_omap_probe,
1279 .remove = mmc_omap_remove,
1280 .suspend = mmc_omap_suspend,
1281 .resume = mmc_omap_resume,
1282 .driver = {
1283 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001284 .owner = THIS_MODULE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001285 },
1286};
1287
1288static int __init mmc_omap_init(void)
1289{
1290 return platform_driver_register(&mmc_omap_driver);
1291}
1292
1293static void __exit mmc_omap_exit(void)
1294{
1295 platform_driver_unregister(&mmc_omap_driver);
1296}
1297
1298module_init(mmc_omap_init);
1299module_exit(mmc_omap_exit);
1300
1301MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1302MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001303MODULE_ALIAS("platform:" DRIVER_NAME);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001304MODULE_AUTHOR("Juha Yrjölä");