blob: 3d59c5d81b3d1e5e282e046b81250a347640a2fe [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 */
Jarkko Lavinen7584d272008-03-26 16:09:42 -040097#define OMAP_MMC_COVER_POLL_DELAY 500
Carlos Aguiar730c9b72006-03-29 09:21:00 +010098
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
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400109 struct tasklet_struct cover_tasklet;
110 struct timer_list cover_timer;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400111 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
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400137 struct work_struct cmd_abort_work;
138 unsigned abort:1;
139 struct timer_list cmd_abort_timer;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400140
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100141 unsigned int sg_len;
142 int sg_idx;
143 u16 * buffer;
144 u32 buffer_bytes_left;
145 u32 total_bytes_left;
146
147 unsigned use_dma:1;
148 unsigned brs_received:1, dma_done:1;
149 unsigned dma_is_read:1;
150 unsigned dma_in_use:1;
151 int dma_ch;
152 spinlock_t dma_lock;
153 struct timer_list dma_timer;
154 unsigned dma_len;
155
156 short power_pin;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100157
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400158 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
159 struct mmc_omap_slot *current_slot;
160 spinlock_t slot_lock;
161 wait_queue_head_t slot_wq;
162 int nr_slots;
163
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400164 struct timer_list clk_timer;
165 spinlock_t clk_lock; /* for changing enabled state */
166 unsigned int fclk_enabled:1;
167
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400168 struct omap_mmc_platform_data *pdata;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100169};
170
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400171void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
172{
173 unsigned long tick_ns;
174
175 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
176 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
177 ndelay(8 * tick_ns);
178 }
179}
180
181void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&host->clk_lock, flags);
186 if (host->fclk_enabled != enable) {
187 host->fclk_enabled = enable;
188 if (enable)
189 clk_enable(host->fclk);
190 else
191 clk_disable(host->fclk);
192 }
193 spin_unlock_irqrestore(&host->clk_lock, flags);
194}
195
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400196static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
197{
198 struct mmc_omap_host *host = slot->host;
199 unsigned long flags;
200
201 if (claimed)
202 goto no_claim;
203 spin_lock_irqsave(&host->slot_lock, flags);
204 while (host->mmc != NULL) {
205 spin_unlock_irqrestore(&host->slot_lock, flags);
206 wait_event(host->slot_wq, host->mmc == NULL);
207 spin_lock_irqsave(&host->slot_lock, flags);
208 }
209 host->mmc = slot->mmc;
210 spin_unlock_irqrestore(&host->slot_lock, flags);
211no_claim:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400212 del_timer(&host->clk_timer);
213 if (host->current_slot != slot || !claimed)
214 mmc_omap_fclk_offdelay(host->current_slot);
215
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400216 if (host->current_slot != slot) {
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400217 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400218 if (host->pdata->switch_slot != NULL)
219 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
220 host->current_slot = slot;
221 }
222
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400223 if (claimed) {
224 mmc_omap_fclk_enable(host, 1);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400225
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400226 /* Doing the dummy read here seems to work around some bug
227 * at least in OMAP24xx silicon where the command would not
228 * start after writing the CMD register. Sigh. */
229 OMAP_MMC_READ(host, CON);
230
231 OMAP_MMC_WRITE(host, CON, slot->saved_con);
232 } else
233 mmc_omap_fclk_enable(host, 0);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400234}
235
236static void mmc_omap_start_request(struct mmc_omap_host *host,
237 struct mmc_request *req);
238
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400239static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400240{
241 struct mmc_omap_host *host = slot->host;
242 unsigned long flags;
243 int i;
244
245 BUG_ON(slot == NULL || host->mmc == NULL);
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400246
247 if (clk_enabled)
248 /* Keeps clock running for at least 8 cycles on valid freq */
249 mod_timer(&host->clk_timer, jiffies + HZ/10);
250 else {
251 del_timer(&host->clk_timer);
252 mmc_omap_fclk_offdelay(slot);
253 mmc_omap_fclk_enable(host, 0);
254 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400255
256 spin_lock_irqsave(&host->slot_lock, flags);
257 /* Check for any pending requests */
258 for (i = 0; i < host->nr_slots; i++) {
259 struct mmc_omap_slot *new_slot;
260 struct mmc_request *rq;
261
262 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
263 continue;
264
265 new_slot = host->slots[i];
266 /* The current slot should not have a request in queue */
267 BUG_ON(new_slot == host->current_slot);
268
269 host->mmc = new_slot->mmc;
270 spin_unlock_irqrestore(&host->slot_lock, flags);
271 mmc_omap_select_slot(new_slot, 1);
272 rq = new_slot->mrq;
273 new_slot->mrq = NULL;
274 mmc_omap_start_request(host, rq);
275 return;
276 }
277
278 host->mmc = NULL;
279 wake_up(&host->slot_wq);
280 spin_unlock_irqrestore(&host->slot_lock, flags);
281}
282
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400283static inline
284int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
285{
Kyungmin Park8348f002008-03-26 16:09:38 -0400286 if (slot->pdata->get_cover_state)
287 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
288 slot->id);
289 return 0;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400290}
291
292static ssize_t
293mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
294 char *buf)
295{
296 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
297 struct mmc_omap_slot *slot = mmc_priv(mmc);
298
299 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
300 "closed");
301}
302
303static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
304
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400305static ssize_t
306mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
307 char *buf)
308{
309 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
310 struct mmc_omap_slot *slot = mmc_priv(mmc);
311
312 return sprintf(buf, "%s\n", slot->pdata->name);
313}
314
315static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
316
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100317static void
318mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
319{
320 u32 cmdreg;
321 u32 resptype;
322 u32 cmdtype;
323
324 host->cmd = cmd;
325
326 resptype = 0;
327 cmdtype = 0;
328
329 /* Our hardware needs to know exact type */
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100330 switch (mmc_resp_type(cmd)) {
331 case MMC_RSP_NONE:
332 break;
333 case MMC_RSP_R1:
334 case MMC_RSP_R1B:
Philip Langdale6f949902007-01-04 07:04:47 -0800335 /* resp 1, 1b, 6, 7 */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100336 resptype = 1;
337 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100338 case MMC_RSP_R2:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100339 resptype = 2;
340 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100341 case MMC_RSP_R3:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100342 resptype = 3;
343 break;
344 default:
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100345 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100346 break;
347 }
348
349 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
350 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
351 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
352 cmdtype = OMAP_MMC_CMDTYPE_BC;
353 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
354 cmdtype = OMAP_MMC_CMDTYPE_BCR;
355 } else {
356 cmdtype = OMAP_MMC_CMDTYPE_AC;
357 }
358
359 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
360
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400361 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100362 cmdreg |= 1 << 6;
363
364 if (cmd->flags & MMC_RSP_BUSY)
365 cmdreg |= 1 << 11;
366
367 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
368 cmdreg |= 1 << 15;
369
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400370 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400371
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100372 OMAP_MMC_WRITE(host, CTO, 200);
373 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
374 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
375 OMAP_MMC_WRITE(host, IE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100376 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
377 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
378 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
379 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
380 OMAP_MMC_STAT_END_OF_DATA);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100381 OMAP_MMC_WRITE(host, CMD, cmdreg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100382}
383
384static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400385mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
386 int abort)
387{
388 enum dma_data_direction dma_data_dir;
389
390 BUG_ON(host->dma_ch < 0);
391 if (data->error)
392 omap_stop_dma(host->dma_ch);
393 /* Release DMA channel lazily */
394 mod_timer(&host->dma_timer, jiffies + HZ);
395 if (data->flags & MMC_DATA_WRITE)
396 dma_data_dir = DMA_TO_DEVICE;
397 else
398 dma_data_dir = DMA_FROM_DEVICE;
399 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
400 dma_data_dir);
401}
402
403static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100404mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
405{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400406 if (host->dma_in_use)
407 mmc_omap_release_dma(host, data, data->error);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100408
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100409 host->data = NULL;
410 host->sg_len = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100411
412 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
413 * dozens of requests until the card finishes writing data.
414 * It'd be cheaper to just wait till an EOFB interrupt arrives...
415 */
416
417 if (!data->stop) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400418 struct mmc_host *mmc;
419
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100420 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400421 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400422 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400423 mmc_request_done(mmc, data->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100424 return;
425 }
426
427 mmc_omap_start_command(host, data->stop);
428}
429
430static void
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400431mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400432{
433 struct mmc_omap_slot *slot = host->current_slot;
434 unsigned int restarts, passes, timeout;
435 u16 stat = 0;
436
437 /* Sending abort takes 80 clocks. Have some extra and round up */
438 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
439 restarts = 0;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400440 while (restarts < maxloops) {
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400441 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
442 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
443
444 passes = 0;
445 while (passes < timeout) {
446 stat = OMAP_MMC_READ(host, STAT);
447 if (stat & OMAP_MMC_STAT_END_OF_CMD)
448 goto out;
449 udelay(1);
450 passes++;
451 }
452
453 restarts++;
454 }
455out:
456 OMAP_MMC_WRITE(host, STAT, stat);
457}
458
459static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400460mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
461{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400462 if (host->dma_in_use)
463 mmc_omap_release_dma(host, data, 1);
464
465 host->data = NULL;
466 host->sg_len = 0;
467
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400468 mmc_omap_send_abort(host, 10000);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400469}
470
471static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100472mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
473{
474 unsigned long flags;
475 int done;
476
477 if (!host->dma_in_use) {
478 mmc_omap_xfer_done(host, data);
479 return;
480 }
481 done = 0;
482 spin_lock_irqsave(&host->dma_lock, flags);
483 if (host->dma_done)
484 done = 1;
485 else
486 host->brs_received = 1;
487 spin_unlock_irqrestore(&host->dma_lock, flags);
488 if (done)
489 mmc_omap_xfer_done(host, data);
490}
491
492static void
493mmc_omap_dma_timer(unsigned long data)
494{
495 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
496
497 BUG_ON(host->dma_ch < 0);
498 omap_free_dma(host->dma_ch);
499 host->dma_ch = -1;
500}
501
502static void
503mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
504{
505 unsigned long flags;
506 int done;
507
508 done = 0;
509 spin_lock_irqsave(&host->dma_lock, flags);
510 if (host->brs_received)
511 done = 1;
512 else
513 host->dma_done = 1;
514 spin_unlock_irqrestore(&host->dma_lock, flags);
515 if (done)
516 mmc_omap_xfer_done(host, data);
517}
518
519static void
520mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
521{
522 host->cmd = NULL;
523
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400524 del_timer(&host->cmd_abort_timer);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400525
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100526 if (cmd->flags & MMC_RSP_PRESENT) {
527 if (cmd->flags & MMC_RSP_136) {
528 /* response type 2 */
529 cmd->resp[3] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100530 OMAP_MMC_READ(host, RSP0) |
531 (OMAP_MMC_READ(host, RSP1) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100532 cmd->resp[2] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100533 OMAP_MMC_READ(host, RSP2) |
534 (OMAP_MMC_READ(host, RSP3) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100535 cmd->resp[1] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100536 OMAP_MMC_READ(host, RSP4) |
537 (OMAP_MMC_READ(host, RSP5) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100538 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100539 OMAP_MMC_READ(host, RSP6) |
540 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100541 } else {
542 /* response types 1, 1b, 3, 4, 5, 6 */
543 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100544 OMAP_MMC_READ(host, RSP6) |
545 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100546 }
547 }
548
Pierre Ossman17b04292007-07-22 22:18:46 +0200549 if (host->data == NULL || cmd->error) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400550 struct mmc_host *mmc;
551
552 if (host->data != NULL)
553 mmc_omap_abort_xfer(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100554 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400555 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400556 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400557 mmc_request_done(mmc, cmd->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100558 }
559}
560
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400561/*
562 * Abort stuck command. Can occur when card is removed while it is being
563 * read.
564 */
565static void mmc_omap_abort_command(struct work_struct *work)
566{
567 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400568 cmd_abort_work);
569 BUG_ON(!host->cmd);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400570
571 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
572 host->cmd->opcode);
573
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400574 if (host->cmd->error == 0)
575 host->cmd->error = -ETIMEDOUT;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400576
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400577 if (host->data == NULL) {
578 struct mmc_command *cmd;
579 struct mmc_host *mmc;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400580
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400581 cmd = host->cmd;
582 host->cmd = NULL;
583 mmc_omap_send_abort(host, 10000);
584
585 host->mrq = NULL;
586 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400587 mmc_omap_release_slot(host->current_slot, 1);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400588 mmc_request_done(mmc, cmd->mrq);
589 } else
590 mmc_omap_cmd_done(host, host->cmd);
591
592 host->abort = 0;
593 enable_irq(host->irq);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400594}
595
596static void
597mmc_omap_cmd_timer(unsigned long data)
598{
599 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400600 unsigned long flags;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400601
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400602 spin_lock_irqsave(&host->slot_lock, flags);
603 if (host->cmd != NULL && !host->abort) {
604 OMAP_MMC_WRITE(host, IE, 0);
605 disable_irq(host->irq);
606 host->abort = 1;
607 schedule_work(&host->cmd_abort_work);
608 }
609 spin_unlock_irqrestore(&host->slot_lock, flags);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400610}
611
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100612/* PIO only */
613static void
614mmc_omap_sg_to_buf(struct mmc_omap_host *host)
615{
616 struct scatterlist *sg;
617
618 sg = host->data->sg + host->sg_idx;
619 host->buffer_bytes_left = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +0200620 host->buffer = sg_virt(sg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100621 if (host->buffer_bytes_left > host->total_bytes_left)
622 host->buffer_bytes_left = host->total_bytes_left;
623}
624
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400625static void
626mmc_omap_clk_timer(unsigned long data)
627{
628 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
629
630 mmc_omap_fclk_enable(host, 0);
631}
632
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100633/* PIO only */
634static void
635mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
636{
637 int n;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100638
639 if (host->buffer_bytes_left == 0) {
640 host->sg_idx++;
641 BUG_ON(host->sg_idx == host->sg_len);
642 mmc_omap_sg_to_buf(host);
643 }
644 n = 64;
645 if (n > host->buffer_bytes_left)
646 n = host->buffer_bytes_left;
647 host->buffer_bytes_left -= n;
648 host->total_bytes_left -= n;
649 host->data->bytes_xfered += n;
650
651 if (write) {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100652 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100653 } else {
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100654 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100655 }
656}
657
658static inline void mmc_omap_report_irq(u16 status)
659{
660 static const char *mmc_omap_status_bits[] = {
661 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
662 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
663 };
664 int i, c = 0;
665
666 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
667 if (status & (1 << i)) {
668 if (c)
669 printk(" ");
670 printk("%s", mmc_omap_status_bits[i]);
671 c++;
672 }
673}
674
David Howells7d12e782006-10-05 14:55:46 +0100675static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100676{
677 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
678 u16 status;
679 int end_command;
680 int end_transfer;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400681 int transfer_error, cmd_error;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100682
683 if (host->cmd == NULL && host->data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100684 status = OMAP_MMC_READ(host, STAT);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400685 dev_info(mmc_dev(host->slots[0]->mmc),
686 "Spurious IRQ 0x%04x\n", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100687 if (status != 0) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100688 OMAP_MMC_WRITE(host, STAT, status);
689 OMAP_MMC_WRITE(host, IE, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100690 }
691 return IRQ_HANDLED;
692 }
693
694 end_command = 0;
695 end_transfer = 0;
696 transfer_error = 0;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400697 cmd_error = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100698
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100699 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400700 int cmd;
701
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100702 OMAP_MMC_WRITE(host, STAT, status);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400703 if (host->cmd != NULL)
704 cmd = host->cmd->opcode;
705 else
706 cmd = -1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100707#ifdef CONFIG_MMC_DEBUG
708 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400709 status, cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100710 mmc_omap_report_irq(status);
711 printk("\n");
712#endif
713 if (host->total_bytes_left) {
714 if ((status & OMAP_MMC_STAT_A_FULL) ||
715 (status & OMAP_MMC_STAT_END_OF_DATA))
716 mmc_omap_xfer_data(host, 0);
717 if (status & OMAP_MMC_STAT_A_EMPTY)
718 mmc_omap_xfer_data(host, 1);
719 }
720
Juha Yrjola2a50b882008-03-26 16:09:26 -0400721 if (status & OMAP_MMC_STAT_END_OF_DATA)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100722 end_transfer = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100723
724 if (status & OMAP_MMC_STAT_DATA_TOUT) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400725 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
726 cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100727 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200728 host->data->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100729 transfer_error = 1;
730 }
731 }
732
733 if (status & OMAP_MMC_STAT_DATA_CRC) {
734 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200735 host->data->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100736 dev_dbg(mmc_dev(host->mmc),
737 "data CRC error, bytes left %d\n",
738 host->total_bytes_left);
739 transfer_error = 1;
740 } else {
741 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
742 }
743 }
744
745 if (status & OMAP_MMC_STAT_CMD_TOUT) {
746 /* Timeouts are routine with some commands */
747 if (host->cmd) {
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400748 struct mmc_omap_slot *slot =
749 host->current_slot;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400750 if (slot == NULL ||
751 !mmc_omap_cover_is_open(slot))
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400752 dev_err(mmc_dev(host->mmc),
Juha Yrjola2a50b882008-03-26 16:09:26 -0400753 "command timeout (CMD%d)\n",
754 cmd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200755 host->cmd->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100756 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400757 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100758 }
759 }
760
761 if (status & OMAP_MMC_STAT_CMD_CRC) {
762 if (host->cmd) {
763 dev_err(mmc_dev(host->mmc),
764 "command CRC error (CMD%d, arg 0x%08x)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400765 cmd, host->cmd->arg);
Pierre Ossman17b04292007-07-22 22:18:46 +0200766 host->cmd->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100767 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400768 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100769 } else
770 dev_err(mmc_dev(host->mmc),
771 "command CRC error without cmd?\n");
772 }
773
774 if (status & OMAP_MMC_STAT_CARD_ERR) {
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200775 dev_dbg(mmc_dev(host->mmc),
776 "ignoring card status error (CMD%d)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400777 cmd);
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200778 end_command = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100779 }
780
781 /*
782 * NOTE: On 1610 the END_OF_CMD may come too early when
Juha Yrjola2a50b882008-03-26 16:09:26 -0400783 * starting a write
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100784 */
785 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
786 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
787 end_command = 1;
788 }
789 }
790
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400791 if (cmd_error && host->data) {
792 del_timer(&host->cmd_abort_timer);
793 host->abort = 1;
794 OMAP_MMC_WRITE(host, IE, 0);
795 disable_irq(host->irq);
796 schedule_work(&host->cmd_abort_work);
797 return IRQ_HANDLED;
798 }
799
Juha Yrjola2a50b882008-03-26 16:09:26 -0400800 if (end_command)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100801 mmc_omap_cmd_done(host, host->cmd);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400802 if (host->data != NULL) {
803 if (transfer_error)
804 mmc_omap_xfer_done(host, host->data);
805 else if (end_transfer)
806 mmc_omap_end_of_data(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100807 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100808
809 return IRQ_HANDLED;
810}
811
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400812void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400813{
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400814 int cover_open;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400815 struct mmc_omap_host *host = dev_get_drvdata(dev);
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400816 struct mmc_omap_slot *slot = host->slots[num];
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400817
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400818 BUG_ON(num >= host->nr_slots);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400819
820 /* Other subsystems can call in here before we're initialised. */
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400821 if (host->nr_slots == 0 || !host->slots[num])
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400822 return;
823
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400824 cover_open = mmc_omap_cover_is_open(slot);
825 if (cover_open != slot->cover_open) {
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400826 slot->cover_open = cover_open;
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400827 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400828 }
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400829
830 tasklet_hi_schedule(&slot->cover_tasklet);
831}
832
833static void mmc_omap_cover_timer(unsigned long arg)
834{
835 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
836 tasklet_schedule(&slot->cover_tasklet);
837}
838
839static void mmc_omap_cover_handler(unsigned long param)
840{
841 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
842 int cover_open = mmc_omap_cover_is_open(slot);
843
844 mmc_detect_change(slot->mmc, 0);
845 if (!cover_open)
846 return;
847
848 /*
849 * If no card is inserted, we postpone polling until
850 * the cover has been closed.
851 */
852 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
853 return;
854
855 mod_timer(&slot->cover_timer,
856 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400857}
858
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100859/* Prepare to transfer the next segment of a scatterlist */
860static void
861mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
862{
863 int dma_ch = host->dma_ch;
864 unsigned long data_addr;
865 u16 buf, frame;
866 u32 count;
867 struct scatterlist *sg = &data->sg[host->sg_idx];
868 int src_port = 0;
869 int dst_port = 0;
870 int sync_dev = 0;
871
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100872 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
Russell Kinga3fd4a12006-06-04 17:51:15 +0100873 frame = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100874 count = sg_dma_len(sg);
875
Russell Kinga3fd4a12006-06-04 17:51:15 +0100876 if ((data->blocks == 1) && (count > data->blksz))
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100877 count = frame;
878
879 host->dma_len = count;
880
881 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
882 * Use 16 or 32 word frames when the blocksize is at least that large.
883 * Blocksize is usually 512 bytes; but not for some SD reads.
884 */
885 if (cpu_is_omap15xx() && frame > 32)
886 frame = 32;
887 else if (frame > 64)
888 frame = 64;
889 count /= frame;
890 frame >>= 1;
891
892 if (!(data->flags & MMC_DATA_WRITE)) {
893 buf = 0x800f | ((frame - 1) << 8);
894
895 if (cpu_class_is_omap1()) {
896 src_port = OMAP_DMA_PORT_TIPB;
897 dst_port = OMAP_DMA_PORT_EMIFF;
898 }
899 if (cpu_is_omap24xx())
900 sync_dev = OMAP24XX_DMA_MMC1_RX;
901
902 omap_set_dma_src_params(dma_ch, src_port,
903 OMAP_DMA_AMODE_CONSTANT,
904 data_addr, 0, 0);
905 omap_set_dma_dest_params(dma_ch, dst_port,
906 OMAP_DMA_AMODE_POST_INC,
907 sg_dma_address(sg), 0, 0);
908 omap_set_dma_dest_data_pack(dma_ch, 1);
909 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
910 } else {
911 buf = 0x0f80 | ((frame - 1) << 0);
912
913 if (cpu_class_is_omap1()) {
914 src_port = OMAP_DMA_PORT_EMIFF;
915 dst_port = OMAP_DMA_PORT_TIPB;
916 }
917 if (cpu_is_omap24xx())
918 sync_dev = OMAP24XX_DMA_MMC1_TX;
919
920 omap_set_dma_dest_params(dma_ch, dst_port,
921 OMAP_DMA_AMODE_CONSTANT,
922 data_addr, 0, 0);
923 omap_set_dma_src_params(dma_ch, src_port,
924 OMAP_DMA_AMODE_POST_INC,
925 sg_dma_address(sg), 0, 0);
926 omap_set_dma_src_data_pack(dma_ch, 1);
927 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
928 }
929
930 /* Max limit for DMA frame count is 0xffff */
Eric Sesterhennd99c5902006-11-30 05:27:38 +0100931 BUG_ON(count > 0xffff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100932
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100933 OMAP_MMC_WRITE(host, BUF, buf);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100934 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
935 frame, count, OMAP_DMA_SYNC_FRAME,
936 sync_dev, 0);
937}
938
939/* A scatterlist segment completed */
940static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
941{
942 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
943 struct mmc_data *mmcdat = host->data;
944
945 if (unlikely(host->dma_ch < 0)) {
Tony Lindgrence9c1a82006-07-01 19:56:44 +0100946 dev_err(mmc_dev(host->mmc),
947 "DMA callback while DMA not enabled\n");
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100948 return;
949 }
950 /* FIXME: We really should do something to _handle_ the errors */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700951 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100952 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
953 return;
954 }
955 if (ch_status & OMAP_DMA_DROP_IRQ) {
956 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
957 return;
958 }
959 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
960 return;
961 }
962 mmcdat->bytes_xfered += host->dma_len;
963 host->sg_idx++;
964 if (host->sg_idx < host->sg_len) {
965 mmc_omap_prepare_dma(host, host->data);
966 omap_start_dma(host->dma_ch);
967 } else
968 mmc_omap_dma_done(host, host->data);
969}
970
971static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
972{
973 const char *dev_name;
974 int sync_dev, dma_ch, is_read, r;
975
976 is_read = !(data->flags & MMC_DATA_WRITE);
977 del_timer_sync(&host->dma_timer);
978 if (host->dma_ch >= 0) {
979 if (is_read == host->dma_is_read)
980 return 0;
981 omap_free_dma(host->dma_ch);
982 host->dma_ch = -1;
983 }
984
985 if (is_read) {
986 if (host->id == 1) {
987 sync_dev = OMAP_DMA_MMC_RX;
988 dev_name = "MMC1 read";
989 } else {
990 sync_dev = OMAP_DMA_MMC2_RX;
991 dev_name = "MMC2 read";
992 }
993 } else {
994 if (host->id == 1) {
995 sync_dev = OMAP_DMA_MMC_TX;
996 dev_name = "MMC1 write";
997 } else {
998 sync_dev = OMAP_DMA_MMC2_TX;
999 dev_name = "MMC2 write";
1000 }
1001 }
1002 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
1003 host, &dma_ch);
1004 if (r != 0) {
1005 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
1006 return r;
1007 }
1008 host->dma_ch = dma_ch;
1009 host->dma_is_read = is_read;
1010
1011 return 0;
1012}
1013
1014static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1015{
1016 u16 reg;
1017
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001018 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001019 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001020 OMAP_MMC_WRITE(host, SDIO, reg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001021 /* Set maximum timeout */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001022 OMAP_MMC_WRITE(host, CTO, 0xff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001023}
1024
1025static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1026{
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -04001027 unsigned int timeout, cycle_ns;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001028 u16 reg;
1029
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -04001030 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
1031 timeout = req->data->timeout_ns / cycle_ns;
1032 timeout += req->data->timeout_clks;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001033
1034 /* Check if we need to use timeout multiplier register */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001035 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001036 if (timeout > 0xffff) {
1037 reg |= (1 << 5);
1038 timeout /= 1024;
1039 } else
1040 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001041 OMAP_MMC_WRITE(host, SDIO, reg);
1042 OMAP_MMC_WRITE(host, DTO, timeout);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001043}
1044
1045static void
1046mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
1047{
1048 struct mmc_data *data = req->data;
1049 int i, use_dma, block_size;
1050 unsigned sg_len;
1051
1052 host->data = data;
1053 if (data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001054 OMAP_MMC_WRITE(host, BLEN, 0);
1055 OMAP_MMC_WRITE(host, NBLK, 0);
1056 OMAP_MMC_WRITE(host, BUF, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001057 host->dma_in_use = 0;
1058 set_cmd_timeout(host, req);
1059 return;
1060 }
1061
Russell Kinga3fd4a12006-06-04 17:51:15 +01001062 block_size = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001063
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001064 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
1065 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001066 set_data_timeout(host, req);
1067
1068 /* cope with calling layer confusion; it issues "single
1069 * block" writes using multi-block scatterlists.
1070 */
1071 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
1072
1073 /* Only do DMA for entire blocks */
1074 use_dma = host->use_dma;
1075 if (use_dma) {
1076 for (i = 0; i < sg_len; i++) {
1077 if ((data->sg[i].length % block_size) != 0) {
1078 use_dma = 0;
1079 break;
1080 }
1081 }
1082 }
1083
1084 host->sg_idx = 0;
1085 if (use_dma) {
1086 if (mmc_omap_get_dma_channel(host, data) == 0) {
1087 enum dma_data_direction dma_data_dir;
1088
1089 if (data->flags & MMC_DATA_WRITE)
1090 dma_data_dir = DMA_TO_DEVICE;
1091 else
1092 dma_data_dir = DMA_FROM_DEVICE;
1093
1094 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1095 sg_len, dma_data_dir);
1096 host->total_bytes_left = 0;
1097 mmc_omap_prepare_dma(host, req->data);
1098 host->brs_received = 0;
1099 host->dma_done = 0;
1100 host->dma_in_use = 1;
1101 } else
1102 use_dma = 0;
1103 }
1104
1105 /* Revert to PIO? */
1106 if (!use_dma) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001107 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001108 host->total_bytes_left = data->blocks * block_size;
1109 host->sg_len = sg_len;
1110 mmc_omap_sg_to_buf(host);
1111 host->dma_in_use = 0;
1112 }
1113}
1114
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001115static void mmc_omap_start_request(struct mmc_omap_host *host,
1116 struct mmc_request *req)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001117{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001118 BUG_ON(host->mrq != NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001119
1120 host->mrq = req;
1121
1122 /* only touch fifo AFTER the controller readies it */
1123 mmc_omap_prepare_data(host, req);
1124 mmc_omap_start_command(host, req->cmd);
1125 if (host->dma_in_use)
1126 omap_start_dma(host->dma_ch);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001127 BUG_ON(irqs_disabled());
1128}
1129
1130static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1131{
1132 struct mmc_omap_slot *slot = mmc_priv(mmc);
1133 struct mmc_omap_host *host = slot->host;
1134 unsigned long flags;
1135
1136 spin_lock_irqsave(&host->slot_lock, flags);
1137 if (host->mmc != NULL) {
1138 BUG_ON(slot->mrq != NULL);
1139 slot->mrq = req;
1140 spin_unlock_irqrestore(&host->slot_lock, flags);
1141 return;
1142 } else
1143 host->mmc = mmc;
1144 spin_unlock_irqrestore(&host->slot_lock, flags);
1145 mmc_omap_select_slot(slot, 1);
1146 mmc_omap_start_request(host, req);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001147}
1148
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001149static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1150 int vdd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001151{
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001152 struct mmc_omap_host *host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001153
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001154 host = slot->host;
1155
1156 if (slot->pdata->set_power != NULL)
1157 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1158 vdd);
1159
1160 if (cpu_is_omap24xx()) {
1161 u16 w;
1162
1163 if (power_on) {
1164 w = OMAP_MMC_READ(host, CON);
1165 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1166 } else {
1167 w = OMAP_MMC_READ(host, CON);
1168 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1169 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001170 }
1171}
1172
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001173static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1174{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001175 struct mmc_omap_slot *slot = mmc_priv(mmc);
1176 struct mmc_omap_host *host = slot->host;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001177 int func_clk_rate = clk_get_rate(host->fclk);
1178 int dsor;
1179
1180 if (ios->clock == 0)
1181 return 0;
1182
1183 dsor = func_clk_rate / ios->clock;
1184 if (dsor < 1)
1185 dsor = 1;
1186
1187 if (func_clk_rate / dsor > ios->clock)
1188 dsor++;
1189
1190 if (dsor > 250)
1191 dsor = 250;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001192
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001193 slot->fclk_freq = func_clk_rate / dsor;
1194
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001195 if (ios->bus_width == MMC_BUS_WIDTH_4)
1196 dsor |= 1 << 15;
1197
1198 return dsor;
1199}
1200
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001201static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1202{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001203 struct mmc_omap_slot *slot = mmc_priv(mmc);
1204 struct mmc_omap_host *host = slot->host;
1205 int i, dsor;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001206 int clk_enabled;
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001207
1208 mmc_omap_select_slot(slot, 0);
1209
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001210 dsor = mmc_omap_calc_divisor(mmc, ios);
1211
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001212 if (ios->vdd != slot->vdd)
1213 slot->vdd = ios->vdd;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001214
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001215 clk_enabled = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001216 switch (ios->power_mode) {
1217 case MMC_POWER_OFF:
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001218 mmc_omap_set_power(slot, 0, ios->vdd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001219 break;
1220 case MMC_POWER_UP:
Tony Lindgren46a67302007-05-01 16:34:16 +02001221 /* Cannot touch dsor yet, just power up MMC */
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001222 mmc_omap_set_power(slot, 1, ios->vdd);
1223 goto exit;
Tony Lindgren46a67302007-05-01 16:34:16 +02001224 case MMC_POWER_ON:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001225 mmc_omap_fclk_enable(host, 1);
1226 clk_enabled = 1;
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001227 dsor |= 1 << 11;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001228 break;
1229 }
1230
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001231 if (slot->bus_mode != ios->bus_mode) {
1232 if (slot->pdata->set_bus_mode != NULL)
1233 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1234 ios->bus_mode);
1235 slot->bus_mode = ios->bus_mode;
1236 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001237
1238 /* On insanely high arm_per frequencies something sometimes
1239 * goes somehow out of sync, and the POW bit is not being set,
1240 * which results in the while loop below getting stuck.
1241 * Writing to the CON register twice seems to do the trick. */
1242 for (i = 0; i < 2; i++)
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001243 OMAP_MMC_WRITE(host, CON, dsor);
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001244 slot->saved_con = dsor;
Tony Lindgren46a67302007-05-01 16:34:16 +02001245 if (ios->power_mode == MMC_POWER_ON) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001246 /* Send clock cycles, poll completion */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001247 OMAP_MMC_WRITE(host, IE, 0);
1248 OMAP_MMC_WRITE(host, STAT, 0xffff);
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001249 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1250 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001251 OMAP_MMC_WRITE(host, STAT, 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001252 }
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001253
1254exit:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001255 mmc_omap_release_slot(slot, clk_enabled);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001256}
1257
David Brownellab7aefd2006-11-12 17:55:30 -08001258static const struct mmc_host_ops mmc_omap_ops = {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001259 .request = mmc_omap_request,
1260 .set_ios = mmc_omap_set_ios,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001261};
1262
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001263static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1264{
1265 struct mmc_omap_slot *slot = NULL;
1266 struct mmc_host *mmc;
1267 int r;
1268
1269 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1270 if (mmc == NULL)
1271 return -ENOMEM;
1272
1273 slot = mmc_priv(mmc);
1274 slot->host = host;
1275 slot->mmc = mmc;
1276 slot->id = id;
1277 slot->pdata = &host->pdata->slots[id];
1278
1279 host->slots[id] = slot;
1280
1281 mmc->caps = MMC_CAP_MULTIWRITE;
1282 if (host->pdata->conf.wire4)
1283 mmc->caps |= MMC_CAP_4_BIT_DATA;
1284
1285 mmc->ops = &mmc_omap_ops;
1286 mmc->f_min = 400000;
1287
1288 if (cpu_class_is_omap2())
1289 mmc->f_max = 48000000;
1290 else
1291 mmc->f_max = 24000000;
1292 if (host->pdata->max_freq)
1293 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1294 mmc->ocr_avail = slot->pdata->ocr_mask;
1295
1296 /* Use scatterlist DMA to reduce per-transfer costs.
1297 * NOTE max_seg_size assumption that small blocks aren't
1298 * normally used (except e.g. for reading SD registers).
1299 */
1300 mmc->max_phys_segs = 32;
1301 mmc->max_hw_segs = 32;
1302 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1303 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1304 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1305 mmc->max_seg_size = mmc->max_req_size;
1306
1307 r = mmc_add_host(mmc);
1308 if (r < 0)
1309 goto err_remove_host;
1310
1311 if (slot->pdata->name != NULL) {
1312 r = device_create_file(&mmc->class_dev,
1313 &dev_attr_slot_name);
1314 if (r < 0)
1315 goto err_remove_host;
1316 }
1317
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001318 if (slot->pdata->get_cover_state != NULL) {
1319 r = device_create_file(&mmc->class_dev,
1320 &dev_attr_cover_switch);
1321 if (r < 0)
1322 goto err_remove_slot_name;
1323
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001324 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1325 (unsigned long)slot);
1326 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1327 (unsigned long)slot);
1328 tasklet_schedule(&slot->cover_tasklet);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001329 }
1330
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001331 return 0;
1332
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001333err_remove_slot_name:
1334 if (slot->pdata->name != NULL)
1335 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001336err_remove_host:
1337 mmc_remove_host(mmc);
1338 mmc_free_host(mmc);
1339 return r;
1340}
1341
1342static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1343{
1344 struct mmc_host *mmc = slot->mmc;
1345
1346 if (slot->pdata->name != NULL)
1347 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001348 if (slot->pdata->get_cover_state != NULL)
1349 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1350
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001351 tasklet_kill(&slot->cover_tasklet);
1352 del_timer_sync(&slot->cover_timer);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001353 flush_scheduled_work();
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001354
1355 mmc_remove_host(mmc);
1356 mmc_free_host(mmc);
1357}
1358
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001359static int __init mmc_omap_probe(struct platform_device *pdev)
1360{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001361 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001362 struct mmc_omap_host *host = NULL;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001363 struct resource *res;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001364 int i, ret = 0;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001365 int irq;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001366
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001367 if (pdata == NULL) {
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001368 dev_err(&pdev->dev, "platform data missing\n");
1369 return -ENXIO;
1370 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001371 if (pdata->nr_slots == 0) {
1372 dev_err(&pdev->dev, "no slots\n");
1373 return -ENXIO;
1374 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001375
1376 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001377 irq = platform_get_irq(pdev, 0);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001378 if (res == NULL || irq < 0)
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001379 return -ENXIO;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001380
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001381 res = request_mem_region(res->start, res->end - res->start + 1,
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001382 pdev->name);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001383 if (res == NULL)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001384 return -EBUSY;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001385
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001386 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1387 if (host == NULL) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001388 ret = -ENOMEM;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001389 goto err_free_mem_region;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001390 }
1391
Jarkko Lavinen0fb47232008-03-26 16:09:48 -04001392 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1393 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1394 (unsigned long) host);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -04001395
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001396 spin_lock_init(&host->clk_lock);
1397 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1398
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001399 spin_lock_init(&host->dma_lock);
Carlos Eduardo Aguiar01e77e12008-03-26 16:09:34 -04001400 setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001401 spin_lock_init(&host->slot_lock);
1402 init_waitqueue_head(&host->slot_wq);
1403
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001404 host->pdata = pdata;
1405 host->dev = &pdev->dev;
1406 platform_set_drvdata(pdev, host);
1407
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001408 host->id = pdev->id;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001409 host->mem_res = res;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001410 host->irq = irq;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001411
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001412 host->use_dma = 1;
1413 host->dma_ch = -1;
1414
1415 host->irq = irq;
1416 host->phys_base = host->mem_res->start;
1417 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1418
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001419 if (cpu_is_omap24xx()) {
1420 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1421 if (IS_ERR(host->iclk))
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001422 goto err_free_mmc_host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001423 clk_enable(host->iclk);
1424 }
1425
1426 if (!cpu_is_omap24xx())
1427 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1428 else
1429 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1430
1431 if (IS_ERR(host->fclk)) {
1432 ret = PTR_ERR(host->fclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001433 goto err_free_iclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001434 }
1435
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001436 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1437 if (ret)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001438 goto err_free_fclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001439
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001440 if (pdata->init != NULL) {
1441 ret = pdata->init(&pdev->dev);
1442 if (ret < 0)
1443 goto err_free_irq;
1444 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001445
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001446 host->nr_slots = pdata->nr_slots;
1447 for (i = 0; i < pdata->nr_slots; i++) {
1448 ret = mmc_omap_new_slot(host, i);
1449 if (ret < 0) {
1450 while (--i >= 0)
1451 mmc_omap_remove_slot(host->slots[i]);
1452
1453 goto err_plat_cleanup;
1454 }
1455 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001456
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001457 return 0;
1458
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001459err_plat_cleanup:
1460 if (pdata->cleanup)
1461 pdata->cleanup(&pdev->dev);
1462err_free_irq:
1463 free_irq(host->irq, host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001464err_free_fclk:
1465 clk_put(host->fclk);
1466err_free_iclk:
1467 if (host->iclk != NULL) {
1468 clk_disable(host->iclk);
1469 clk_put(host->iclk);
1470 }
1471err_free_mmc_host:
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001472 kfree(host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001473err_free_mem_region:
1474 release_mem_region(res->start, res->end - res->start + 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001475 return ret;
1476}
1477
1478static int mmc_omap_remove(struct platform_device *pdev)
1479{
1480 struct mmc_omap_host *host = platform_get_drvdata(pdev);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001481 int i;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001482
1483 platform_set_drvdata(pdev, NULL);
1484
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001485 BUG_ON(host == NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001486
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001487 for (i = 0; i < host->nr_slots; i++)
1488 mmc_omap_remove_slot(host->slots[i]);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001489
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001490 if (host->pdata->cleanup)
1491 host->pdata->cleanup(&pdev->dev);
1492
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001493 if (host->iclk && !IS_ERR(host->iclk))
1494 clk_put(host->iclk);
1495 if (host->fclk && !IS_ERR(host->fclk))
1496 clk_put(host->fclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001497
1498 release_mem_region(pdev->resource[0].start,
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001499 pdev->resource[0].end - pdev->resource[0].start + 1);
1500
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001501 kfree(host);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001502
1503 return 0;
1504}
1505
1506#ifdef CONFIG_PM
1507static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1508{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001509 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001510 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1511
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001512 if (host == NULL || host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001513 return 0;
1514
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001515 for (i = 0; i < host->nr_slots; i++) {
1516 struct mmc_omap_slot *slot;
1517
1518 slot = host->slots[i];
1519 ret = mmc_suspend_host(slot->mmc, mesg);
1520 if (ret < 0) {
1521 while (--i >= 0) {
1522 slot = host->slots[i];
1523 mmc_resume_host(slot->mmc);
1524 }
1525 return ret;
1526 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001527 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001528 host->suspended = 1;
1529 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001530}
1531
1532static int mmc_omap_resume(struct platform_device *pdev)
1533{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001534 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001535 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1536
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001537 if (host == NULL || !host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001538 return 0;
1539
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001540 for (i = 0; i < host->nr_slots; i++) {
1541 struct mmc_omap_slot *slot;
1542 slot = host->slots[i];
1543 ret = mmc_resume_host(slot->mmc);
1544 if (ret < 0)
1545 return ret;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001546
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001547 host->suspended = 0;
1548 }
1549 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001550}
1551#else
1552#define mmc_omap_suspend NULL
1553#define mmc_omap_resume NULL
1554#endif
1555
1556static struct platform_driver mmc_omap_driver = {
1557 .probe = mmc_omap_probe,
1558 .remove = mmc_omap_remove,
1559 .suspend = mmc_omap_suspend,
1560 .resume = mmc_omap_resume,
1561 .driver = {
1562 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001563 .owner = THIS_MODULE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001564 },
1565};
1566
1567static int __init mmc_omap_init(void)
1568{
1569 return platform_driver_register(&mmc_omap_driver);
1570}
1571
1572static void __exit mmc_omap_exit(void)
1573{
1574 platform_driver_unregister(&mmc_omap_driver);
1575}
1576
1577module_init(mmc_omap_init);
1578module_exit(mmc_omap_exit);
1579
1580MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1581MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001582MODULE_ALIAS("platform:" DRIVER_NAME);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001583MODULE_AUTHOR("Juha Yrjölä");