aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc/host/omap.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mmc/host/omap.c')
-rw-r--r--drivers/mmc/host/omap.c995
1 files changed, 671 insertions, 324 deletions
diff --git a/drivers/mmc/host/omap.c b/drivers/mmc/host/omap.c
index 90c358b57d1c..14759e9f42ad 100644
--- a/drivers/mmc/host/omap.c
+++ b/drivers/mmc/host/omap.c
@@ -32,6 +32,7 @@
32#include <asm/mach-types.h> 32#include <asm/mach-types.h>
33 33
34#include <asm/arch/board.h> 34#include <asm/arch/board.h>
35#include <asm/arch/mmc.h>
35#include <asm/arch/gpio.h> 36#include <asm/arch/gpio.h>
36#include <asm/arch/dma.h> 37#include <asm/arch/dma.h>
37#include <asm/arch/mux.h> 38#include <asm/arch/mux.h>
@@ -93,9 +94,27 @@
93 94
94/* Specifies how often in millisecs to poll for card status changes 95/* Specifies how often in millisecs to poll for card status changes
95 * when the cover switch is open */ 96 * when the cover switch is open */
96#define OMAP_MMC_SWITCH_POLL_DELAY 500 97#define OMAP_MMC_COVER_POLL_DELAY 500
97 98
98static int mmc_omap_enable_poll = 1; 99struct 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 tasklet_struct cover_tasklet;
110 struct timer_list cover_timer;
111 unsigned cover_open;
112
113 struct mmc_request *mrq;
114 struct mmc_omap_host *host;
115 struct mmc_host *mmc;
116 struct omap_mmc_slot_data *pdata;
117};
99 118
100struct mmc_omap_host { 119struct mmc_omap_host {
101 int initialized; 120 int initialized;
@@ -115,6 +134,15 @@ struct mmc_omap_host {
115 unsigned char bus_mode; 134 unsigned char bus_mode;
116 unsigned char hw_bus_mode; 135 unsigned char hw_bus_mode;
117 136
137 struct work_struct cmd_abort_work;
138 unsigned abort:1;
139 struct timer_list cmd_abort_timer;
140
141 struct work_struct slot_release_work;
142 struct mmc_omap_slot *next_slot;
143 struct work_struct send_stop_work;
144 struct mmc_data *stop_data;
145
118 unsigned int sg_len; 146 unsigned int sg_len;
119 int sg_idx; 147 int sg_idx;
120 u16 * buffer; 148 u16 * buffer;
@@ -131,63 +159,178 @@ struct mmc_omap_host {
131 unsigned dma_len; 159 unsigned dma_len;
132 160
133 short power_pin; 161 short power_pin;
134 short wp_pin;
135 162
136 int switch_pin; 163 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
137 struct work_struct switch_work; 164 struct mmc_omap_slot *current_slot;
138 struct timer_list switch_timer; 165 spinlock_t slot_lock;
139 int switch_last_state; 166 wait_queue_head_t slot_wq;
167 int nr_slots;
168
169 struct timer_list clk_timer;
170 spinlock_t clk_lock; /* for changing enabled state */
171 unsigned int fclk_enabled:1;
172
173 struct omap_mmc_platform_data *pdata;
140}; 174};
141 175
142static inline int 176void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
143mmc_omap_cover_is_open(struct mmc_omap_host *host)
144{ 177{
145 if (host->switch_pin < 0) 178 unsigned long tick_ns;
146 return 0; 179
147 return omap_get_gpio_datain(host->switch_pin); 180 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
181 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
182 ndelay(8 * tick_ns);
183 }
148} 184}
149 185
150static ssize_t 186void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
151mmc_omap_show_cover_switch(struct device *dev,
152 struct device_attribute *attr, char *buf)
153{ 187{
154 struct mmc_omap_host *host = dev_get_drvdata(dev); 188 unsigned long flags;
155 189
156 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(host) ? "open" : 190 spin_lock_irqsave(&host->clk_lock, flags);
157 "closed"); 191 if (host->fclk_enabled != enable) {
192 host->fclk_enabled = enable;
193 if (enable)
194 clk_enable(host->fclk);
195 else
196 clk_disable(host->fclk);
197 }
198 spin_unlock_irqrestore(&host->clk_lock, flags);
158} 199}
159 200
160static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL); 201static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
202{
203 struct mmc_omap_host *host = slot->host;
204 unsigned long flags;
161 205
162static ssize_t 206 if (claimed)
163mmc_omap_show_enable_poll(struct device *dev, 207 goto no_claim;
164 struct device_attribute *attr, char *buf) 208 spin_lock_irqsave(&host->slot_lock, flags);
209 while (host->mmc != NULL) {
210 spin_unlock_irqrestore(&host->slot_lock, flags);
211 wait_event(host->slot_wq, host->mmc == NULL);
212 spin_lock_irqsave(&host->slot_lock, flags);
213 }
214 host->mmc = slot->mmc;
215 spin_unlock_irqrestore(&host->slot_lock, flags);
216no_claim:
217 del_timer(&host->clk_timer);
218 if (host->current_slot != slot || !claimed)
219 mmc_omap_fclk_offdelay(host->current_slot);
220
221 if (host->current_slot != slot) {
222 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
223 if (host->pdata->switch_slot != NULL)
224 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
225 host->current_slot = slot;
226 }
227
228 if (claimed) {
229 mmc_omap_fclk_enable(host, 1);
230
231 /* Doing the dummy read here seems to work around some bug
232 * at least in OMAP24xx silicon where the command would not
233 * start after writing the CMD register. Sigh. */
234 OMAP_MMC_READ(host, CON);
235
236 OMAP_MMC_WRITE(host, CON, slot->saved_con);
237 } else
238 mmc_omap_fclk_enable(host, 0);
239}
240
241static void mmc_omap_start_request(struct mmc_omap_host *host,
242 struct mmc_request *req);
243
244static void mmc_omap_slot_release_work(struct work_struct *work)
165{ 245{
166 return snprintf(buf, PAGE_SIZE, "%d\n", mmc_omap_enable_poll); 246 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
247 slot_release_work);
248 struct mmc_omap_slot *next_slot = host->next_slot;
249 struct mmc_request *rq;
250
251 host->next_slot = NULL;
252 mmc_omap_select_slot(next_slot, 1);
253
254 rq = next_slot->mrq;
255 next_slot->mrq = NULL;
256 mmc_omap_start_request(host, rq);
167} 257}
168 258
169static ssize_t 259static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
170mmc_omap_store_enable_poll(struct device *dev,
171 struct device_attribute *attr, const char *buf,
172 size_t size)
173{ 260{
174 int enable_poll; 261 struct mmc_omap_host *host = slot->host;
262 unsigned long flags;
263 int i;
264
265 BUG_ON(slot == NULL || host->mmc == NULL);
266
267 if (clk_enabled)
268 /* Keeps clock running for at least 8 cycles on valid freq */
269 mod_timer(&host->clk_timer, jiffies + HZ/10);
270 else {
271 del_timer(&host->clk_timer);
272 mmc_omap_fclk_offdelay(slot);
273 mmc_omap_fclk_enable(host, 0);
274 }
175 275
176 if (sscanf(buf, "%10d", &enable_poll) != 1) 276 spin_lock_irqsave(&host->slot_lock, flags);
177 return -EINVAL; 277 /* Check for any pending requests */
278 for (i = 0; i < host->nr_slots; i++) {
279 struct mmc_omap_slot *new_slot;
178 280
179 if (enable_poll != mmc_omap_enable_poll) { 281 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
180 struct mmc_omap_host *host = dev_get_drvdata(dev); 282 continue;
181 283
182 mmc_omap_enable_poll = enable_poll; 284 BUG_ON(host->next_slot != NULL);
183 if (enable_poll && host->switch_pin >= 0) 285 new_slot = host->slots[i];
184 schedule_work(&host->switch_work); 286 /* The current slot should not have a request in queue */
287 BUG_ON(new_slot == host->current_slot);
288
289 host->next_slot = new_slot;
290 host->mmc = new_slot->mmc;
291 spin_unlock_irqrestore(&host->slot_lock, flags);
292 schedule_work(&host->slot_release_work);
293 return;
185 } 294 }
186 return size; 295
296 host->mmc = NULL;
297 wake_up(&host->slot_wq);
298 spin_unlock_irqrestore(&host->slot_lock, flags);
299}
300
301static inline
302int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
303{
304 if (slot->pdata->get_cover_state)
305 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
306 slot->id);
307 return 0;
308}
309
310static ssize_t
311mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
312 char *buf)
313{
314 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
315 struct mmc_omap_slot *slot = mmc_priv(mmc);
316
317 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
318 "closed");
187} 319}
188 320
189static DEVICE_ATTR(enable_poll, 0664, 321static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
190 mmc_omap_show_enable_poll, mmc_omap_store_enable_poll); 322
323static ssize_t
324mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
325 char *buf)
326{
327 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
328 struct mmc_omap_slot *slot = mmc_priv(mmc);
329
330 return sprintf(buf, "%s\n", slot->pdata->name);
331}
332
333static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
191 334
192static void 335static void
193mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd) 336mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
@@ -233,7 +376,7 @@ mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
233 376
234 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12); 377 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
235 378
236 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN) 379 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
237 cmdreg |= 1 << 6; 380 cmdreg |= 1 << 6;
238 381
239 if (cmd->flags & MMC_RSP_BUSY) 382 if (cmd->flags & MMC_RSP_BUSY)
@@ -242,7 +385,7 @@ mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
242 if (host->data && !(host->data->flags & MMC_DATA_WRITE)) 385 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
243 cmdreg |= 1 << 15; 386 cmdreg |= 1 << 15;
244 387
245 clk_enable(host->fclk); 388 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
246 389
247 OMAP_MMC_WRITE(host, CTO, 200); 390 OMAP_MMC_WRITE(host, CTO, 200);
248 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff); 391 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
@@ -257,26 +400,46 @@ mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
257} 400}
258 401
259static void 402static void
403mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
404 int abort)
405{
406 enum dma_data_direction dma_data_dir;
407
408 BUG_ON(host->dma_ch < 0);
409 if (data->error)
410 omap_stop_dma(host->dma_ch);
411 /* Release DMA channel lazily */
412 mod_timer(&host->dma_timer, jiffies + HZ);
413 if (data->flags & MMC_DATA_WRITE)
414 dma_data_dir = DMA_TO_DEVICE;
415 else
416 dma_data_dir = DMA_FROM_DEVICE;
417 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
418 dma_data_dir);
419}
420
421static void mmc_omap_send_stop_work(struct work_struct *work)
422{
423 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
424 send_stop_work);
425 struct mmc_omap_slot *slot = host->current_slot;
426 struct mmc_data *data = host->stop_data;
427 unsigned long tick_ns;
428
429 tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
430 ndelay(8*tick_ns);
431
432 mmc_omap_start_command(host, data->stop);
433}
434
435static void
260mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data) 436mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
261{ 437{
262 if (host->dma_in_use) { 438 if (host->dma_in_use)
263 enum dma_data_direction dma_data_dir; 439 mmc_omap_release_dma(host, data, data->error);
264 440
265 BUG_ON(host->dma_ch < 0);
266 if (data->error)
267 omap_stop_dma(host->dma_ch);
268 /* Release DMA channel lazily */
269 mod_timer(&host->dma_timer, jiffies + HZ);
270 if (data->flags & MMC_DATA_WRITE)
271 dma_data_dir = DMA_TO_DEVICE;
272 else
273 dma_data_dir = DMA_FROM_DEVICE;
274 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
275 dma_data_dir);
276 }
277 host->data = NULL; 441 host->data = NULL;
278 host->sg_len = 0; 442 host->sg_len = 0;
279 clk_disable(host->fclk);
280 443
281 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing 444 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
282 * dozens of requests until the card finishes writing data. 445 * dozens of requests until the card finishes writing data.
@@ -284,12 +447,58 @@ mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
284 */ 447 */
285 448
286 if (!data->stop) { 449 if (!data->stop) {
450 struct mmc_host *mmc;
451
287 host->mrq = NULL; 452 host->mrq = NULL;
288 mmc_request_done(host->mmc, data->mrq); 453 mmc = host->mmc;
454 mmc_omap_release_slot(host->current_slot, 1);
455 mmc_request_done(mmc, data->mrq);
289 return; 456 return;
290 } 457 }
291 458
292 mmc_omap_start_command(host, data->stop); 459 host->stop_data = data;
460 schedule_work(&host->send_stop_work);
461}
462
463static void
464mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
465{
466 struct mmc_omap_slot *slot = host->current_slot;
467 unsigned int restarts, passes, timeout;
468 u16 stat = 0;
469
470 /* Sending abort takes 80 clocks. Have some extra and round up */
471 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
472 restarts = 0;
473 while (restarts < maxloops) {
474 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
475 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
476
477 passes = 0;
478 while (passes < timeout) {
479 stat = OMAP_MMC_READ(host, STAT);
480 if (stat & OMAP_MMC_STAT_END_OF_CMD)
481 goto out;
482 udelay(1);
483 passes++;
484 }
485
486 restarts++;
487 }
488out:
489 OMAP_MMC_WRITE(host, STAT, stat);
490}
491
492static void
493mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
494{
495 if (host->dma_in_use)
496 mmc_omap_release_dma(host, data, 1);
497
498 host->data = NULL;
499 host->sg_len = 0;
500
501 mmc_omap_send_abort(host, 10000);
293} 502}
294 503
295static void 504static void
@@ -345,6 +554,8 @@ mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
345{ 554{
346 host->cmd = NULL; 555 host->cmd = NULL;
347 556
557 del_timer(&host->cmd_abort_timer);
558
348 if (cmd->flags & MMC_RSP_PRESENT) { 559 if (cmd->flags & MMC_RSP_PRESENT) {
349 if (cmd->flags & MMC_RSP_136) { 560 if (cmd->flags & MMC_RSP_136) {
350 /* response type 2 */ 561 /* response type 2 */
@@ -369,10 +580,66 @@ mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
369 } 580 }
370 581
371 if (host->data == NULL || cmd->error) { 582 if (host->data == NULL || cmd->error) {
583 struct mmc_host *mmc;
584
585 if (host->data != NULL)
586 mmc_omap_abort_xfer(host, host->data);
372 host->mrq = NULL; 587 host->mrq = NULL;
373 clk_disable(host->fclk); 588 mmc = host->mmc;
374 mmc_request_done(host->mmc, cmd->mrq); 589 mmc_omap_release_slot(host->current_slot, 1);
590 mmc_request_done(mmc, cmd->mrq);
591 }
592}
593
594/*
595 * Abort stuck command. Can occur when card is removed while it is being
596 * read.
597 */
598static void mmc_omap_abort_command(struct work_struct *work)
599{
600 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
601 cmd_abort_work);
602 BUG_ON(!host->cmd);
603
604 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
605 host->cmd->opcode);
606
607 if (host->cmd->error == 0)
608 host->cmd->error = -ETIMEDOUT;
609
610 if (host->data == NULL) {
611 struct mmc_command *cmd;
612 struct mmc_host *mmc;
613
614 cmd = host->cmd;
615 host->cmd = NULL;
616 mmc_omap_send_abort(host, 10000);
617
618 host->mrq = NULL;
619 mmc = host->mmc;
620 mmc_omap_release_slot(host->current_slot, 1);
621 mmc_request_done(mmc, cmd->mrq);
622 } else
623 mmc_omap_cmd_done(host, host->cmd);
624
625 host->abort = 0;
626 enable_irq(host->irq);
627}
628
629static void
630mmc_omap_cmd_timer(unsigned long data)
631{
632 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
633 unsigned long flags;
634
635 spin_lock_irqsave(&host->slot_lock, flags);
636 if (host->cmd != NULL && !host->abort) {
637 OMAP_MMC_WRITE(host, IE, 0);
638 disable_irq(host->irq);
639 host->abort = 1;
640 schedule_work(&host->cmd_abort_work);
375 } 641 }
642 spin_unlock_irqrestore(&host->slot_lock, flags);
376} 643}
377 644
378/* PIO only */ 645/* PIO only */
@@ -388,6 +655,14 @@ mmc_omap_sg_to_buf(struct mmc_omap_host *host)
388 host->buffer_bytes_left = host->total_bytes_left; 655 host->buffer_bytes_left = host->total_bytes_left;
389} 656}
390 657
658static void
659mmc_omap_clk_timer(unsigned long data)
660{
661 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
662
663 mmc_omap_fclk_enable(host, 0);
664}
665
391/* PIO only */ 666/* PIO only */
392static void 667static void
393mmc_omap_xfer_data(struct mmc_omap_host *host, int write) 668mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
@@ -436,11 +711,12 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
436 u16 status; 711 u16 status;
437 int end_command; 712 int end_command;
438 int end_transfer; 713 int end_transfer;
439 int transfer_error; 714 int transfer_error, cmd_error;
440 715
441 if (host->cmd == NULL && host->data == NULL) { 716 if (host->cmd == NULL && host->data == NULL) {
442 status = OMAP_MMC_READ(host, STAT); 717 status = OMAP_MMC_READ(host, STAT);
443 dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status); 718 dev_info(mmc_dev(host->slots[0]->mmc),
719 "Spurious IRQ 0x%04x\n", status);
444 if (status != 0) { 720 if (status != 0) {
445 OMAP_MMC_WRITE(host, STAT, status); 721 OMAP_MMC_WRITE(host, STAT, status);
446 OMAP_MMC_WRITE(host, IE, 0); 722 OMAP_MMC_WRITE(host, IE, 0);
@@ -451,12 +727,19 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
451 end_command = 0; 727 end_command = 0;
452 end_transfer = 0; 728 end_transfer = 0;
453 transfer_error = 0; 729 transfer_error = 0;
730 cmd_error = 0;
454 731
455 while ((status = OMAP_MMC_READ(host, STAT)) != 0) { 732 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
733 int cmd;
734
456 OMAP_MMC_WRITE(host, STAT, status); 735 OMAP_MMC_WRITE(host, STAT, status);
736 if (host->cmd != NULL)
737 cmd = host->cmd->opcode;
738 else
739 cmd = -1;
457#ifdef CONFIG_MMC_DEBUG 740#ifdef CONFIG_MMC_DEBUG
458 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ", 741 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
459 status, host->cmd != NULL ? host->cmd->opcode : -1); 742 status, cmd);
460 mmc_omap_report_irq(status); 743 mmc_omap_report_irq(status);
461 printk("\n"); 744 printk("\n");
462#endif 745#endif
@@ -468,12 +751,12 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
468 mmc_omap_xfer_data(host, 1); 751 mmc_omap_xfer_data(host, 1);
469 } 752 }
470 753
471 if (status & OMAP_MMC_STAT_END_OF_DATA) { 754 if (status & OMAP_MMC_STAT_END_OF_DATA)
472 end_transfer = 1; 755 end_transfer = 1;
473 }
474 756
475 if (status & OMAP_MMC_STAT_DATA_TOUT) { 757 if (status & OMAP_MMC_STAT_DATA_TOUT) {
476 dev_dbg(mmc_dev(host->mmc), "data timeout\n"); 758 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
759 cmd);
477 if (host->data) { 760 if (host->data) {
478 host->data->error = -ETIMEDOUT; 761 host->data->error = -ETIMEDOUT;
479 transfer_error = 1; 762 transfer_error = 1;
@@ -495,17 +778,16 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
495 if (status & OMAP_MMC_STAT_CMD_TOUT) { 778 if (status & OMAP_MMC_STAT_CMD_TOUT) {
496 /* Timeouts are routine with some commands */ 779 /* Timeouts are routine with some commands */
497 if (host->cmd) { 780 if (host->cmd) {
498 if (host->cmd->opcode != MMC_ALL_SEND_CID && 781 struct mmc_omap_slot *slot =
499 host->cmd->opcode != 782 host->current_slot;
500 MMC_SEND_OP_COND && 783 if (slot == NULL ||
501 host->cmd->opcode != 784 !mmc_omap_cover_is_open(slot))
502 MMC_APP_CMD &&
503 !mmc_omap_cover_is_open(host))
504 dev_err(mmc_dev(host->mmc), 785 dev_err(mmc_dev(host->mmc),
505 "command timeout, CMD %d\n", 786 "command timeout (CMD%d)\n",
506 host->cmd->opcode); 787 cmd);
507 host->cmd->error = -ETIMEDOUT; 788 host->cmd->error = -ETIMEDOUT;
508 end_command = 1; 789 end_command = 1;
790 cmd_error = 1;
509 } 791 }
510 } 792 }
511 793
@@ -513,9 +795,10 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
513 if (host->cmd) { 795 if (host->cmd) {
514 dev_err(mmc_dev(host->mmc), 796 dev_err(mmc_dev(host->mmc),
515 "command CRC error (CMD%d, arg 0x%08x)\n", 797 "command CRC error (CMD%d, arg 0x%08x)\n",
516 host->cmd->opcode, host->cmd->arg); 798 cmd, host->cmd->arg);
517 host->cmd->error = -EILSEQ; 799 host->cmd->error = -EILSEQ;
518 end_command = 1; 800 end_command = 1;
801 cmd_error = 1;
519 } else 802 } else
520 dev_err(mmc_dev(host->mmc), 803 dev_err(mmc_dev(host->mmc),
521 "command CRC error without cmd?\n"); 804 "command CRC error without cmd?\n");
@@ -524,13 +807,13 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
524 if (status & OMAP_MMC_STAT_CARD_ERR) { 807 if (status & OMAP_MMC_STAT_CARD_ERR) {
525 dev_dbg(mmc_dev(host->mmc), 808 dev_dbg(mmc_dev(host->mmc),
526 "ignoring card status error (CMD%d)\n", 809 "ignoring card status error (CMD%d)\n",
527 host->cmd->opcode); 810 cmd);
528 end_command = 1; 811 end_command = 1;
529 } 812 }
530 813
531 /* 814 /*
532 * NOTE: On 1610 the END_OF_CMD may come too early when 815 * NOTE: On 1610 the END_OF_CMD may come too early when
533 * starting a write 816 * starting a write
534 */ 817 */
535 if ((status & OMAP_MMC_STAT_END_OF_CMD) && 818 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
536 (!(status & OMAP_MMC_STAT_A_EMPTY))) { 819 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
@@ -538,63 +821,72 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
538 } 821 }
539 } 822 }
540 823
541 if (end_command) { 824 if (cmd_error && host->data) {
825 del_timer(&host->cmd_abort_timer);
826 host->abort = 1;
827 OMAP_MMC_WRITE(host, IE, 0);
828 disable_irq(host->irq);
829 schedule_work(&host->cmd_abort_work);
830 return IRQ_HANDLED;
831 }
832
833 if (end_command)
542 mmc_omap_cmd_done(host, host->cmd); 834 mmc_omap_cmd_done(host, host->cmd);
835 if (host->data != NULL) {
836 if (transfer_error)
837 mmc_omap_xfer_done(host, host->data);
838 else if (end_transfer)
839 mmc_omap_end_of_data(host, host->data);
543 } 840 }
544 if (transfer_error)
545 mmc_omap_xfer_done(host, host->data);
546 else if (end_transfer)
547 mmc_omap_end_of_data(host, host->data);
548 841
549 return IRQ_HANDLED; 842 return IRQ_HANDLED;
550} 843}
551 844
552static irqreturn_t mmc_omap_switch_irq(int irq, void *dev_id) 845void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
553{ 846{
554 struct mmc_omap_host *host = (struct mmc_omap_host *) dev_id; 847 int cover_open;
848 struct mmc_omap_host *host = dev_get_drvdata(dev);
849 struct mmc_omap_slot *slot = host->slots[num];
555 850
556 schedule_work(&host->switch_work); 851 BUG_ON(num >= host->nr_slots);
557 852
558 return IRQ_HANDLED; 853 /* Other subsystems can call in here before we're initialised. */
854 if (host->nr_slots == 0 || !host->slots[num])
855 return;
856
857 cover_open = mmc_omap_cover_is_open(slot);
858 if (cover_open != slot->cover_open) {
859 slot->cover_open = cover_open;
860 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
861 }
862
863 tasklet_hi_schedule(&slot->cover_tasklet);
559} 864}
560 865
561static void mmc_omap_switch_timer(unsigned long arg) 866static void mmc_omap_cover_timer(unsigned long arg)
562{ 867{
563 struct mmc_omap_host *host = (struct mmc_omap_host *) arg; 868 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
564 869 tasklet_schedule(&slot->cover_tasklet);
565 schedule_work(&host->switch_work);
566} 870}
567 871
568static void mmc_omap_switch_handler(struct work_struct *work) 872static void mmc_omap_cover_handler(unsigned long param)
569{ 873{
570 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host, switch_work); 874 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
571 struct mmc_card *card; 875 int cover_open = mmc_omap_cover_is_open(slot);
572 static int complained = 0;
573 int cards = 0, cover_open;
574 876
575 if (host->switch_pin == -1) 877 mmc_detect_change(slot->mmc, 0);
878 if (!cover_open)
576 return; 879 return;
577 cover_open = mmc_omap_cover_is_open(host); 880
578 if (cover_open != host->switch_last_state) { 881 /*
579 kobject_uevent(&host->dev->kobj, KOBJ_CHANGE); 882 * If no card is inserted, we postpone polling until
580 host->switch_last_state = cover_open; 883 * the cover has been closed.
581 } 884 */
582 mmc_detect_change(host->mmc, 0); 885 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
583 list_for_each_entry(card, &host->mmc->cards, node) { 886 return;
584 if (mmc_card_present(card)) 887
585 cards++; 888 mod_timer(&slot->cover_timer,
586 } 889 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
587 if (mmc_omap_cover_is_open(host)) {
588 if (!complained) {
589 dev_info(mmc_dev(host->mmc), "cover is open\n");
590 complained = 1;
591 }
592 if (mmc_omap_enable_poll)
593 mod_timer(&host->switch_timer, jiffies +
594 msecs_to_jiffies(OMAP_MMC_SWITCH_POLL_DELAY));
595 } else {
596 complained = 0;
597 }
598} 890}
599 891
600/* Prepare to transfer the next segment of a scatterlist */ 892/* Prepare to transfer the next segment of a scatterlist */
@@ -765,13 +1057,12 @@ static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_reques
765 1057
766static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req) 1058static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
767{ 1059{
768 int timeout; 1060 unsigned int timeout, cycle_ns;
769 u16 reg; 1061 u16 reg;
770 1062
771 /* Convert ns to clock cycles by assuming 20MHz frequency 1063 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
772 * 1 cycle at 20MHz = 500 ns 1064 timeout = req->data->timeout_ns / cycle_ns;
773 */ 1065 timeout += req->data->timeout_clks;
774 timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
775 1066
776 /* Check if we need to use timeout multiplier register */ 1067 /* Check if we need to use timeout multiplier register */
777 reg = OMAP_MMC_READ(host, SDIO); 1068 reg = OMAP_MMC_READ(host, SDIO);
@@ -854,11 +1145,10 @@ mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
854 } 1145 }
855} 1146}
856 1147
857static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req) 1148static void mmc_omap_start_request(struct mmc_omap_host *host,
1149 struct mmc_request *req)
858{ 1150{
859 struct mmc_omap_host *host = mmc_priv(mmc); 1151 BUG_ON(host->mrq != NULL);
860
861 WARN_ON(host->mrq != NULL);
862 1152
863 host->mrq = req; 1153 host->mrq = req;
864 1154
@@ -867,60 +1157,56 @@ static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
867 mmc_omap_start_command(host, req->cmd); 1157 mmc_omap_start_command(host, req->cmd);
868 if (host->dma_in_use) 1158 if (host->dma_in_use)
869 omap_start_dma(host->dma_ch); 1159 omap_start_dma(host->dma_ch);
1160 BUG_ON(irqs_disabled());
870} 1161}
871 1162
872static void innovator_fpga_socket_power(int on) 1163static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
873{ 1164{
874#if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX) 1165 struct mmc_omap_slot *slot = mmc_priv(mmc);
875 if (on) { 1166 struct mmc_omap_host *host = slot->host;
876 fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3), 1167 unsigned long flags;
877 OMAP1510_FPGA_POWER); 1168
878 } else { 1169 spin_lock_irqsave(&host->slot_lock, flags);
879 fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3), 1170 if (host->mmc != NULL) {
880 OMAP1510_FPGA_POWER); 1171 BUG_ON(slot->mrq != NULL);
881 } 1172 slot->mrq = req;
882#endif 1173 spin_unlock_irqrestore(&host->slot_lock, flags);
1174 return;
1175 } else
1176 host->mmc = mmc;
1177 spin_unlock_irqrestore(&host->slot_lock, flags);
1178 mmc_omap_select_slot(slot, 1);
1179 mmc_omap_start_request(host, req);
883} 1180}
884 1181
885/* 1182static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
886 * Turn the socket power on/off. Innovator uses FPGA, most boards 1183 int vdd)
887 * probably use GPIO.
888 */
889static void mmc_omap_power(struct mmc_omap_host *host, int on)
890{ 1184{
891 if (on) { 1185 struct mmc_omap_host *host;
892 if (machine_is_omap_innovator()) 1186
893 innovator_fpga_socket_power(1); 1187 host = slot->host;
894 else if (machine_is_omap_h2()) 1188
895 tps65010_set_gpio_out_value(GPIO3, HIGH); 1189 if (slot->pdata->set_power != NULL)
896 else if (machine_is_omap_h3()) 1190 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
897 /* GPIO 4 of TPS65010 sends SD_EN signal */ 1191 vdd);
898 tps65010_set_gpio_out_value(GPIO4, HIGH); 1192
899 else if (cpu_is_omap24xx()) { 1193 if (cpu_is_omap24xx()) {
900 u16 reg = OMAP_MMC_READ(host, CON); 1194 u16 w;
901 OMAP_MMC_WRITE(host, CON, reg | (1 << 11)); 1195
902 } else 1196 if (power_on) {
903 if (host->power_pin >= 0) 1197 w = OMAP_MMC_READ(host, CON);
904 omap_set_gpio_dataout(host->power_pin, 1); 1198 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
905 } else { 1199 } else {
906 if (machine_is_omap_innovator()) 1200 w = OMAP_MMC_READ(host, CON);
907 innovator_fpga_socket_power(0); 1201 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
908 else if (machine_is_omap_h2()) 1202 }
909 tps65010_set_gpio_out_value(GPIO3, LOW);
910 else if (machine_is_omap_h3())
911 tps65010_set_gpio_out_value(GPIO4, LOW);
912 else if (cpu_is_omap24xx()) {
913 u16 reg = OMAP_MMC_READ(host, CON);
914 OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
915 } else
916 if (host->power_pin >= 0)
917 omap_set_gpio_dataout(host->power_pin, 0);
918 } 1203 }
919} 1204}
920 1205
921static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios) 1206static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
922{ 1207{
923 struct mmc_omap_host *host = mmc_priv(mmc); 1208 struct mmc_omap_slot *slot = mmc_priv(mmc);
1209 struct mmc_omap_host *host = slot->host;
924 int func_clk_rate = clk_get_rate(host->fclk); 1210 int func_clk_rate = clk_get_rate(host->fclk);
925 int dsor; 1211 int dsor;
926 1212
@@ -936,7 +1222,8 @@ static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
936 1222
937 if (dsor > 250) 1223 if (dsor > 250)
938 dsor = 250; 1224 dsor = 250;
939 dsor++; 1225
1226 slot->fclk_freq = func_clk_rate / dsor;
940 1227
941 if (ios->bus_width == MMC_BUS_WIDTH_4) 1228 if (ios->bus_width == MMC_BUS_WIDTH_4)
942 dsor |= 1 << 15; 1229 dsor |= 1 << 15;
@@ -946,28 +1233,40 @@ static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
946 1233
947static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 1234static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
948{ 1235{
949 struct mmc_omap_host *host = mmc_priv(mmc); 1236 struct mmc_omap_slot *slot = mmc_priv(mmc);
950 int dsor; 1237 struct mmc_omap_host *host = slot->host;
951 int i; 1238 int i, dsor;
1239 int clk_enabled;
1240
1241 mmc_omap_select_slot(slot, 0);
952 1242
953 dsor = mmc_omap_calc_divisor(mmc, ios); 1243 dsor = mmc_omap_calc_divisor(mmc, ios);
954 host->bus_mode = ios->bus_mode;
955 host->hw_bus_mode = host->bus_mode;
956 1244
1245 if (ios->vdd != slot->vdd)
1246 slot->vdd = ios->vdd;
1247
1248 clk_enabled = 0;
957 switch (ios->power_mode) { 1249 switch (ios->power_mode) {
958 case MMC_POWER_OFF: 1250 case MMC_POWER_OFF:
959 mmc_omap_power(host, 0); 1251 mmc_omap_set_power(slot, 0, ios->vdd);
960 break; 1252 break;
961 case MMC_POWER_UP: 1253 case MMC_POWER_UP:
962 /* Cannot touch dsor yet, just power up MMC */ 1254 /* Cannot touch dsor yet, just power up MMC */
963 mmc_omap_power(host, 1); 1255 mmc_omap_set_power(slot, 1, ios->vdd);
964 return; 1256 goto exit;
965 case MMC_POWER_ON: 1257 case MMC_POWER_ON:
1258 mmc_omap_fclk_enable(host, 1);
1259 clk_enabled = 1;
966 dsor |= 1 << 11; 1260 dsor |= 1 << 11;
967 break; 1261 break;
968 } 1262 }
969 1263
970 clk_enable(host->fclk); 1264 if (slot->bus_mode != ios->bus_mode) {
1265 if (slot->pdata->set_bus_mode != NULL)
1266 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1267 ios->bus_mode);
1268 slot->bus_mode = ios->bus_mode;
1269 }
971 1270
972 /* On insanely high arm_per frequencies something sometimes 1271 /* On insanely high arm_per frequencies something sometimes
973 * goes somehow out of sync, and the POW bit is not being set, 1272 * goes somehow out of sync, and the POW bit is not being set,
@@ -975,43 +1274,143 @@ static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
975 * Writing to the CON register twice seems to do the trick. */ 1274 * Writing to the CON register twice seems to do the trick. */
976 for (i = 0; i < 2; i++) 1275 for (i = 0; i < 2; i++)
977 OMAP_MMC_WRITE(host, CON, dsor); 1276 OMAP_MMC_WRITE(host, CON, dsor);
1277 slot->saved_con = dsor;
978 if (ios->power_mode == MMC_POWER_ON) { 1278 if (ios->power_mode == MMC_POWER_ON) {
1279 /* worst case at 400kHz, 80 cycles makes 200 microsecs */
1280 int usecs = 250;
1281
979 /* Send clock cycles, poll completion */ 1282 /* Send clock cycles, poll completion */
980 OMAP_MMC_WRITE(host, IE, 0); 1283 OMAP_MMC_WRITE(host, IE, 0);
981 OMAP_MMC_WRITE(host, STAT, 0xffff); 1284 OMAP_MMC_WRITE(host, STAT, 0xffff);
982 OMAP_MMC_WRITE(host, CMD, 1 << 7); 1285 OMAP_MMC_WRITE(host, CMD, 1 << 7);
983 while ((OMAP_MMC_READ(host, STAT) & 1) == 0); 1286 while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
1287 udelay(1);
1288 usecs--;
1289 }
984 OMAP_MMC_WRITE(host, STAT, 1); 1290 OMAP_MMC_WRITE(host, STAT, 1);
985 } 1291 }
986 clk_disable(host->fclk);
987}
988
989static int mmc_omap_get_ro(struct mmc_host *mmc)
990{
991 struct mmc_omap_host *host = mmc_priv(mmc);
992 1292
993 return host->wp_pin && omap_get_gpio_datain(host->wp_pin); 1293exit:
1294 mmc_omap_release_slot(slot, clk_enabled);
994} 1295}
995 1296
996static const struct mmc_host_ops mmc_omap_ops = { 1297static const struct mmc_host_ops mmc_omap_ops = {
997 .request = mmc_omap_request, 1298 .request = mmc_omap_request,
998 .set_ios = mmc_omap_set_ios, 1299 .set_ios = mmc_omap_set_ios,
999 .get_ro = mmc_omap_get_ro,
1000}; 1300};
1001 1301
1002static int __init mmc_omap_probe(struct platform_device *pdev) 1302static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1003{ 1303{
1004 struct omap_mmc_conf *minfo = pdev->dev.platform_data; 1304 struct mmc_omap_slot *slot = NULL;
1005 struct mmc_host *mmc; 1305 struct mmc_host *mmc;
1306 int r;
1307
1308 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1309 if (mmc == NULL)
1310 return -ENOMEM;
1311
1312 slot = mmc_priv(mmc);
1313 slot->host = host;
1314 slot->mmc = mmc;
1315 slot->id = id;
1316 slot->pdata = &host->pdata->slots[id];
1317
1318 host->slots[id] = slot;
1319
1320 mmc->caps = MMC_CAP_MULTIWRITE;
1321 if (host->pdata->conf.wire4)
1322 mmc->caps |= MMC_CAP_4_BIT_DATA;
1323
1324 mmc->ops = &mmc_omap_ops;
1325 mmc->f_min = 400000;
1326
1327 if (cpu_class_is_omap2())
1328 mmc->f_max = 48000000;
1329 else
1330 mmc->f_max = 24000000;
1331 if (host->pdata->max_freq)
1332 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1333 mmc->ocr_avail = slot->pdata->ocr_mask;
1334
1335 /* Use scatterlist DMA to reduce per-transfer costs.
1336 * NOTE max_seg_size assumption that small blocks aren't
1337 * normally used (except e.g. for reading SD registers).
1338 */
1339 mmc->max_phys_segs = 32;
1340 mmc->max_hw_segs = 32;
1341 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1342 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1343 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1344 mmc->max_seg_size = mmc->max_req_size;
1345
1346 r = mmc_add_host(mmc);
1347 if (r < 0)
1348 goto err_remove_host;
1349
1350 if (slot->pdata->name != NULL) {
1351 r = device_create_file(&mmc->class_dev,
1352 &dev_attr_slot_name);
1353 if (r < 0)
1354 goto err_remove_host;
1355 }
1356
1357 if (slot->pdata->get_cover_state != NULL) {
1358 r = device_create_file(&mmc->class_dev,
1359 &dev_attr_cover_switch);
1360 if (r < 0)
1361 goto err_remove_slot_name;
1362
1363 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1364 (unsigned long)slot);
1365 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1366 (unsigned long)slot);
1367 tasklet_schedule(&slot->cover_tasklet);
1368 }
1369
1370 return 0;
1371
1372err_remove_slot_name:
1373 if (slot->pdata->name != NULL)
1374 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1375err_remove_host:
1376 mmc_remove_host(mmc);
1377 mmc_free_host(mmc);
1378 return r;
1379}
1380
1381static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1382{
1383 struct mmc_host *mmc = slot->mmc;
1384
1385 if (slot->pdata->name != NULL)
1386 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1387 if (slot->pdata->get_cover_state != NULL)
1388 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1389
1390 tasklet_kill(&slot->cover_tasklet);
1391 del_timer_sync(&slot->cover_timer);
1392 flush_scheduled_work();
1393
1394 mmc_remove_host(mmc);
1395 mmc_free_host(mmc);
1396}
1397
1398static int __init mmc_omap_probe(struct platform_device *pdev)
1399{
1400 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1006 struct mmc_omap_host *host = NULL; 1401 struct mmc_omap_host *host = NULL;
1007 struct resource *res; 1402 struct resource *res;
1008 int ret = 0; 1403 int i, ret = 0;
1009 int irq; 1404 int irq;
1010 1405
1011 if (minfo == NULL) { 1406 if (pdata == NULL) {
1012 dev_err(&pdev->dev, "platform data missing\n"); 1407 dev_err(&pdev->dev, "platform data missing\n");
1013 return -ENXIO; 1408 return -ENXIO;
1014 } 1409 }
1410 if (pdata->nr_slots == 0) {
1411 dev_err(&pdev->dev, "no slots\n");
1412 return -ENXIO;
1413 }
1015 1414
1016 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1415 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1017 irq = platform_get_irq(pdev, 0); 1416 irq = platform_get_irq(pdev, 0);
@@ -1019,28 +1418,46 @@ static int __init mmc_omap_probe(struct platform_device *pdev)
1019 return -ENXIO; 1418 return -ENXIO;
1020 1419
1021 res = request_mem_region(res->start, res->end - res->start + 1, 1420 res = request_mem_region(res->start, res->end - res->start + 1,
1022 pdev->name); 1421 pdev->name);
1023 if (res == NULL) 1422 if (res == NULL)
1024 return -EBUSY; 1423 return -EBUSY;
1025 1424
1026 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev); 1425 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1027 if (mmc == NULL) { 1426 if (host == NULL) {
1028 ret = -ENOMEM; 1427 ret = -ENOMEM;
1029 goto err_free_mem_region; 1428 goto err_free_mem_region;
1030 } 1429 }
1031 1430
1032 host = mmc_priv(mmc); 1431 INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
1033 host->mmc = mmc; 1432 INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
1433
1434 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1435 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1436 (unsigned long) host);
1437
1438 spin_lock_init(&host->clk_lock);
1439 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1034 1440
1035 spin_lock_init(&host->dma_lock); 1441 spin_lock_init(&host->dma_lock);
1036 init_timer(&host->dma_timer); 1442 setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
1037 host->dma_timer.function = mmc_omap_dma_timer; 1443 spin_lock_init(&host->slot_lock);
1038 host->dma_timer.data = (unsigned long) host; 1444 init_waitqueue_head(&host->slot_wq);
1445
1446 host->pdata = pdata;
1447 host->dev = &pdev->dev;
1448 platform_set_drvdata(pdev, host);
1039 1449
1040 host->id = pdev->id; 1450 host->id = pdev->id;
1041 host->mem_res = res; 1451 host->mem_res = res;
1042 host->irq = irq; 1452 host->irq = irq;
1043 1453
1454 host->use_dma = 1;
1455 host->dma_ch = -1;
1456
1457 host->irq = irq;
1458 host->phys_base = host->mem_res->start;
1459 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1460
1044 if (cpu_is_omap24xx()) { 1461 if (cpu_is_omap24xx()) {
1045 host->iclk = clk_get(&pdev->dev, "mmc_ick"); 1462 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1046 if (IS_ERR(host->iclk)) 1463 if (IS_ERR(host->iclk))
@@ -1058,109 +1475,34 @@ static int __init mmc_omap_probe(struct platform_device *pdev)
1058 goto err_free_iclk; 1475 goto err_free_iclk;
1059 } 1476 }
1060 1477
1061 /* REVISIT:
1062 * Also, use minfo->cover to decide how to manage
1063 * the card detect sensing.
1064 */
1065 host->power_pin = minfo->power_pin;
1066 host->switch_pin = minfo->switch_pin;
1067 host->wp_pin = minfo->wp_pin;
1068 host->use_dma = 1;
1069 host->dma_ch = -1;
1070
1071 host->irq = irq;
1072 host->phys_base = host->mem_res->start;
1073 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1074
1075 mmc->ops = &mmc_omap_ops;
1076 mmc->f_min = 400000;
1077 mmc->f_max = 24000000;
1078 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1079 mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1080
1081 if (minfo->wire4)
1082 mmc->caps |= MMC_CAP_4_BIT_DATA;
1083
1084 /* Use scatterlist DMA to reduce per-transfer costs.
1085 * NOTE max_seg_size assumption that small blocks aren't
1086 * normally used (except e.g. for reading SD registers).
1087 */
1088 mmc->max_phys_segs = 32;
1089 mmc->max_hw_segs = 32;
1090 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1091 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1092 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1093 mmc->max_seg_size = mmc->max_req_size;
1094
1095 if (host->power_pin >= 0) {
1096 if ((ret = omap_request_gpio(host->power_pin)) != 0) {
1097 dev_err(mmc_dev(host->mmc),
1098 "Unable to get GPIO pin for MMC power\n");
1099 goto err_free_fclk;
1100 }
1101 omap_set_gpio_direction(host->power_pin, 0);
1102 }
1103
1104 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host); 1478 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1105 if (ret) 1479 if (ret)
1106 goto err_free_power_gpio; 1480 goto err_free_fclk;
1107 1481
1108 host->dev = &pdev->dev; 1482 if (pdata->init != NULL) {
1109 platform_set_drvdata(pdev, host); 1483 ret = pdata->init(&pdev->dev);
1484 if (ret < 0)
1485 goto err_free_irq;
1486 }
1110 1487
1111 if (host->switch_pin >= 0) { 1488 host->nr_slots = pdata->nr_slots;
1112 INIT_WORK(&host->switch_work, mmc_omap_switch_handler); 1489 for (i = 0; i < pdata->nr_slots; i++) {
1113 init_timer(&host->switch_timer); 1490 ret = mmc_omap_new_slot(host, i);
1114 host->switch_timer.function = mmc_omap_switch_timer; 1491 if (ret < 0) {
1115 host->switch_timer.data = (unsigned long) host; 1492 while (--i >= 0)
1116 if (omap_request_gpio(host->switch_pin) != 0) { 1493 mmc_omap_remove_slot(host->slots[i]);
1117 dev_warn(mmc_dev(host->mmc), "Unable to get GPIO pin for MMC cover switch\n");
1118 host->switch_pin = -1;
1119 goto no_switch;
1120 }
1121 1494
1122 omap_set_gpio_direction(host->switch_pin, 1); 1495 goto err_plat_cleanup;
1123 ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
1124 mmc_omap_switch_irq, IRQF_TRIGGER_RISING, DRIVER_NAME, host);
1125 if (ret) {
1126 dev_warn(mmc_dev(host->mmc), "Unable to get IRQ for MMC cover switch\n");
1127 omap_free_gpio(host->switch_pin);
1128 host->switch_pin = -1;
1129 goto no_switch;
1130 }
1131 ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
1132 if (ret == 0) {
1133 ret = device_create_file(&pdev->dev, &dev_attr_enable_poll);
1134 if (ret != 0)
1135 device_remove_file(&pdev->dev, &dev_attr_cover_switch);
1136 } 1496 }
1137 if (ret) {
1138 dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
1139 free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
1140 omap_free_gpio(host->switch_pin);
1141 host->switch_pin = -1;
1142 goto no_switch;
1143 }
1144 if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
1145 schedule_work(&host->switch_work);
1146 } 1497 }
1147 1498
1148 mmc_add_host(mmc);
1149
1150 return 0; 1499 return 0;
1151 1500
1152no_switch: 1501err_plat_cleanup:
1153 /* FIXME: Free other resources too. */ 1502 if (pdata->cleanup)
1154 if (host) { 1503 pdata->cleanup(&pdev->dev);
1155 if (host->iclk && !IS_ERR(host->iclk)) 1504err_free_irq:
1156 clk_put(host->iclk); 1505 free_irq(host->irq, host);
1157 if (host->fclk && !IS_ERR(host->fclk))
1158 clk_put(host->fclk);
1159 mmc_free_host(host->mmc);
1160 }
1161err_free_power_gpio:
1162 if (host->power_pin >= 0)
1163 omap_free_gpio(host->power_pin);
1164err_free_fclk: 1506err_free_fclk:
1165 clk_put(host->fclk); 1507 clk_put(host->fclk);
1166err_free_iclk: 1508err_free_iclk:
@@ -1169,7 +1511,7 @@ err_free_iclk:
1169 clk_put(host->iclk); 1511 clk_put(host->iclk);
1170 } 1512 }
1171err_free_mmc_host: 1513err_free_mmc_host:
1172 mmc_free_host(host->mmc); 1514 kfree(host);
1173err_free_mem_region: 1515err_free_mem_region:
1174 release_mem_region(res->start, res->end - res->start + 1); 1516 release_mem_region(res->start, res->end - res->start + 1);
1175 return ret; 1517 return ret;
@@ -1178,25 +1520,18 @@ err_free_mem_region:
1178static int mmc_omap_remove(struct platform_device *pdev) 1520static int mmc_omap_remove(struct platform_device *pdev)
1179{ 1521{
1180 struct mmc_omap_host *host = platform_get_drvdata(pdev); 1522 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1523 int i;
1181 1524
1182 platform_set_drvdata(pdev, NULL); 1525 platform_set_drvdata(pdev, NULL);
1183 1526
1184 BUG_ON(host == NULL); 1527 BUG_ON(host == NULL);
1185 1528
1186 mmc_remove_host(host->mmc); 1529 for (i = 0; i < host->nr_slots; i++)
1187 free_irq(host->irq, host); 1530 mmc_omap_remove_slot(host->slots[i]);
1531
1532 if (host->pdata->cleanup)
1533 host->pdata->cleanup(&pdev->dev);
1188 1534
1189 if (host->power_pin >= 0)
1190 omap_free_gpio(host->power_pin);
1191 if (host->switch_pin >= 0) {
1192 device_remove_file(&pdev->dev, &dev_attr_enable_poll);
1193 device_remove_file(&pdev->dev, &dev_attr_cover_switch);
1194 free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
1195 omap_free_gpio(host->switch_pin);
1196 host->switch_pin = -1;
1197 del_timer_sync(&host->switch_timer);
1198 flush_scheduled_work();
1199 }
1200 if (host->iclk && !IS_ERR(host->iclk)) 1535 if (host->iclk && !IS_ERR(host->iclk))
1201 clk_put(host->iclk); 1536 clk_put(host->iclk);
1202 if (host->fclk && !IS_ERR(host->fclk)) 1537 if (host->fclk && !IS_ERR(host->fclk))
@@ -1205,7 +1540,7 @@ static int mmc_omap_remove(struct platform_device *pdev)
1205 release_mem_region(pdev->resource[0].start, 1540 release_mem_region(pdev->resource[0].start,
1206 pdev->resource[0].end - pdev->resource[0].start + 1); 1541 pdev->resource[0].end - pdev->resource[0].start + 1);
1207 1542
1208 mmc_free_host(host->mmc); 1543 kfree(host);
1209 1544
1210 return 0; 1545 return 0;
1211} 1546}
@@ -1213,35 +1548,47 @@ static int mmc_omap_remove(struct platform_device *pdev)
1213#ifdef CONFIG_PM 1548#ifdef CONFIG_PM
1214static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg) 1549static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1215{ 1550{
1216 int ret = 0; 1551 int i, ret = 0;
1217 struct mmc_omap_host *host = platform_get_drvdata(pdev); 1552 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1218 1553
1219 if (host && host->suspended) 1554 if (host == NULL || host->suspended)
1220 return 0; 1555 return 0;
1221 1556
1222 if (host) { 1557 for (i = 0; i < host->nr_slots; i++) {
1223 ret = mmc_suspend_host(host->mmc, mesg); 1558 struct mmc_omap_slot *slot;
1224 if (ret == 0) 1559
1225 host->suspended = 1; 1560 slot = host->slots[i];
1561 ret = mmc_suspend_host(slot->mmc, mesg);
1562 if (ret < 0) {
1563 while (--i >= 0) {
1564 slot = host->slots[i];
1565 mmc_resume_host(slot->mmc);
1566 }
1567 return ret;
1568 }
1226 } 1569 }
1227 return ret; 1570 host->suspended = 1;
1571 return 0;
1228} 1572}
1229 1573
1230static int mmc_omap_resume(struct platform_device *pdev) 1574static int mmc_omap_resume(struct platform_device *pdev)
1231{ 1575{
1232 int ret = 0; 1576 int i, ret = 0;
1233 struct mmc_omap_host *host = platform_get_drvdata(pdev); 1577 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1234 1578
1235 if (host && !host->suspended) 1579 if (host == NULL || !host->suspended)
1236 return 0; 1580 return 0;
1237 1581
1238 if (host) { 1582 for (i = 0; i < host->nr_slots; i++) {
1239 ret = mmc_resume_host(host->mmc); 1583 struct mmc_omap_slot *slot;
1240 if (ret == 0) 1584 slot = host->slots[i];
1241 host->suspended = 0; 1585 ret = mmc_resume_host(slot->mmc);
1242 } 1586 if (ret < 0)
1587 return ret;
1243 1588
1244 return ret; 1589 host->suspended = 0;
1590 }
1591 return 0;
1245} 1592}
1246#else 1593#else
1247#define mmc_omap_suspend NULL 1594#define mmc_omap_suspend NULL