md: avoid endless recovery loop when waiting for fail device to complete.
[pandora-kernel.git] / drivers / mmc / host / tmio_mmc_pio.c
1 /*
2  * linux/drivers/mmc/host/tmio_mmc_pio.c
3  *
4  * Copyright (C) 2011 Guennadi Liakhovetski
5  * Copyright (C) 2007 Ian Molton
6  * Copyright (C) 2004 Ian Molton
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Driver for the MMC / SD / SDIO IP found in:
13  *
14  * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
15  *
16  * This driver draws mainly on scattered spec sheets, Reverse engineering
17  * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
18  * support). (Further 4 bit support from a later datasheet).
19  *
20  * TODO:
21  *   Investigate using a workqueue for PIO transfers
22  *   Eliminate FIXMEs
23  *   SDIO support
24  *   Better Power management
25  *   Handle MMC errors better
26  *   double buffer support
27  *
28  */
29
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/highmem.h>
33 #include <linux/interrupt.h>
34 #include <linux/io.h>
35 #include <linux/irq.h>
36 #include <linux/mfd/tmio.h>
37 #include <linux/mmc/host.h>
38 #include <linux/mmc/tmio.h>
39 #include <linux/module.h>
40 #include <linux/pagemap.h>
41 #include <linux/platform_device.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/scatterlist.h>
44 #include <linux/workqueue.h>
45 #include <linux/spinlock.h>
46
47 #include "tmio_mmc.h"
48
49 static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
50 {
51         return readw(host->ctl + (addr << host->bus_shift));
52 }
53
54 static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
55                 u16 *buf, int count)
56 {
57         readsw(host->ctl + (addr << host->bus_shift), buf, count);
58 }
59
60 static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
61 {
62         return readw(host->ctl + (addr << host->bus_shift)) |
63                readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
64 }
65
66 static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
67 {
68         writew(val, host->ctl + (addr << host->bus_shift));
69 }
70
71 static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
72                 u16 *buf, int count)
73 {
74         writesw(host->ctl + (addr << host->bus_shift), buf, count);
75 }
76
77 static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
78 {
79         writew(val, host->ctl + (addr << host->bus_shift));
80         writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
81 }
82
83 void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
84 {
85         u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) & ~(i & TMIO_MASK_IRQ);
86         sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
87 }
88
89 void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
90 {
91         u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) | (i & TMIO_MASK_IRQ);
92         sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
93 }
94
95 static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
96 {
97         sd_ctrl_write32(host, CTL_STATUS, ~i);
98 }
99
100 static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
101 {
102         host->sg_len = data->sg_len;
103         host->sg_ptr = data->sg;
104         host->sg_orig = data->sg;
105         host->sg_off = 0;
106 }
107
108 static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
109 {
110         host->sg_ptr = sg_next(host->sg_ptr);
111         host->sg_off = 0;
112         return --host->sg_len;
113 }
114
115 #ifdef CONFIG_MMC_DEBUG
116
117 #define STATUS_TO_TEXT(a, status, i) \
118         do { \
119                 if (status & TMIO_STAT_##a) { \
120                         if (i++) \
121                                 printk(" | "); \
122                         printk(#a); \
123                 } \
124         } while (0)
125
126 static void pr_debug_status(u32 status)
127 {
128         int i = 0;
129         printk(KERN_DEBUG "status: %08x = ", status);
130         STATUS_TO_TEXT(CARD_REMOVE, status, i);
131         STATUS_TO_TEXT(CARD_INSERT, status, i);
132         STATUS_TO_TEXT(SIGSTATE, status, i);
133         STATUS_TO_TEXT(WRPROTECT, status, i);
134         STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
135         STATUS_TO_TEXT(CARD_INSERT_A, status, i);
136         STATUS_TO_TEXT(SIGSTATE_A, status, i);
137         STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
138         STATUS_TO_TEXT(STOPBIT_ERR, status, i);
139         STATUS_TO_TEXT(ILL_FUNC, status, i);
140         STATUS_TO_TEXT(CMD_BUSY, status, i);
141         STATUS_TO_TEXT(CMDRESPEND, status, i);
142         STATUS_TO_TEXT(DATAEND, status, i);
143         STATUS_TO_TEXT(CRCFAIL, status, i);
144         STATUS_TO_TEXT(DATATIMEOUT, status, i);
145         STATUS_TO_TEXT(CMDTIMEOUT, status, i);
146         STATUS_TO_TEXT(RXOVERFLOW, status, i);
147         STATUS_TO_TEXT(TXUNDERRUN, status, i);
148         STATUS_TO_TEXT(RXRDY, status, i);
149         STATUS_TO_TEXT(TXRQ, status, i);
150         STATUS_TO_TEXT(ILL_ACCESS, status, i);
151         printk("\n");
152 }
153
154 #else
155 #define pr_debug_status(s)  do { } while (0)
156 #endif
157
158 static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
159 {
160         struct tmio_mmc_host *host = mmc_priv(mmc);
161
162         if (enable) {
163                 host->sdio_irq_enabled = 1;
164                 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
165                 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
166                         (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
167         } else {
168                 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
169                 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
170                 host->sdio_irq_enabled = 0;
171         }
172 }
173
174 static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
175 {
176         u32 clk = 0, clock;
177
178         if (new_clock) {
179                 for (clock = host->mmc->f_min, clk = 0x80000080;
180                         new_clock >= (clock<<1); clk >>= 1)
181                         clock <<= 1;
182                 clk |= 0x100;
183         }
184
185         if (host->set_clk_div)
186                 host->set_clk_div(host->pdev, (clk>>22) & 1);
187
188         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
189 }
190
191 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
192 {
193         struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
194
195         /* implicit BUG_ON(!res) */
196         if (resource_size(res) > 0x100) {
197                 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
198                 msleep(10);
199         }
200
201         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
202                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
203         msleep(10);
204 }
205
206 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
207 {
208         struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
209
210         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
211                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
212         msleep(10);
213
214         /* implicit BUG_ON(!res) */
215         if (resource_size(res) > 0x100) {
216                 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
217                 msleep(10);
218         }
219 }
220
221 static void tmio_mmc_reset(struct tmio_mmc_host *host)
222 {
223         struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
224
225         /* FIXME - should we set stop clock reg here */
226         sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
227         /* implicit BUG_ON(!res) */
228         if (resource_size(res) > 0x100)
229                 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
230         msleep(10);
231         sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
232         if (resource_size(res) > 0x100)
233                 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
234         msleep(10);
235 }
236
237 static void tmio_mmc_reset_work(struct work_struct *work)
238 {
239         struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
240                                                   delayed_reset_work.work);
241         struct mmc_request *mrq;
242         unsigned long flags;
243
244         spin_lock_irqsave(&host->lock, flags);
245         mrq = host->mrq;
246
247         /*
248          * is request already finished? Since we use a non-blocking
249          * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
250          * us, so, have to check for IS_ERR(host->mrq)
251          */
252         if (IS_ERR_OR_NULL(mrq)
253             || time_is_after_jiffies(host->last_req_ts +
254                 msecs_to_jiffies(2000))) {
255                 spin_unlock_irqrestore(&host->lock, flags);
256                 return;
257         }
258
259         dev_warn(&host->pdev->dev,
260                 "timeout waiting for hardware interrupt (CMD%u)\n",
261                 mrq->cmd->opcode);
262
263         if (host->data)
264                 host->data->error = -ETIMEDOUT;
265         else if (host->cmd)
266                 host->cmd->error = -ETIMEDOUT;
267         else
268                 mrq->cmd->error = -ETIMEDOUT;
269
270         host->cmd = NULL;
271         host->data = NULL;
272         host->force_pio = false;
273
274         spin_unlock_irqrestore(&host->lock, flags);
275
276         tmio_mmc_reset(host);
277
278         /* Ready for new calls */
279         host->mrq = NULL;
280
281         mmc_request_done(host->mmc, mrq);
282 }
283
284 /* called with host->lock held, interrupts disabled */
285 static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
286 {
287         struct mmc_request *mrq = host->mrq;
288
289         if (!mrq)
290                 return;
291
292         host->cmd = NULL;
293         host->data = NULL;
294         host->force_pio = false;
295
296         cancel_delayed_work(&host->delayed_reset_work);
297
298         host->mrq = NULL;
299
300         /* FIXME: mmc_request_done() can schedule! */
301         mmc_request_done(host->mmc, mrq);
302 }
303
304 /* These are the bitmasks the tmio chip requires to implement the MMC response
305  * types. Note that R1 and R6 are the same in this scheme. */
306 #define APP_CMD        0x0040
307 #define RESP_NONE      0x0300
308 #define RESP_R1        0x0400
309 #define RESP_R1B       0x0500
310 #define RESP_R2        0x0600
311 #define RESP_R3        0x0700
312 #define DATA_PRESENT   0x0800
313 #define TRANSFER_READ  0x1000
314 #define TRANSFER_MULTI 0x2000
315 #define SECURITY_CMD   0x4000
316
317 static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
318 {
319         struct mmc_data *data = host->data;
320         int c = cmd->opcode;
321
322         /* Command 12 is handled by hardware */
323         if (cmd->opcode == 12 && !cmd->arg) {
324                 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
325                 return 0;
326         }
327
328         switch (mmc_resp_type(cmd)) {
329         case MMC_RSP_NONE: c |= RESP_NONE; break;
330         case MMC_RSP_R1:   c |= RESP_R1;   break;
331         case MMC_RSP_R1B:  c |= RESP_R1B;  break;
332         case MMC_RSP_R2:   c |= RESP_R2;   break;
333         case MMC_RSP_R3:   c |= RESP_R3;   break;
334         default:
335                 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
336                 return -EINVAL;
337         }
338
339         host->cmd = cmd;
340
341 /* FIXME - this seems to be ok commented out but the spec suggest this bit
342  *         should be set when issuing app commands.
343  *      if(cmd->flags & MMC_FLAG_ACMD)
344  *              c |= APP_CMD;
345  */
346         if (data) {
347                 c |= DATA_PRESENT;
348                 if (data->blocks > 1) {
349                         sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
350                         c |= TRANSFER_MULTI;
351                 }
352                 if (data->flags & MMC_DATA_READ)
353                         c |= TRANSFER_READ;
354         }
355
356         tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
357
358         /* Fire off the command */
359         sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
360         sd_ctrl_write16(host, CTL_SD_CMD, c);
361
362         return 0;
363 }
364
365 /*
366  * This chip always returns (at least?) as much data as you ask for.
367  * I'm unsure what happens if you ask for less than a block. This should be
368  * looked into to ensure that a funny length read doesn't hose the controller.
369  */
370 static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
371 {
372         struct mmc_data *data = host->data;
373         void *sg_virt;
374         unsigned short *buf;
375         unsigned int count;
376         unsigned long flags;
377
378         if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
379                 pr_err("PIO IRQ in DMA mode!\n");
380                 return;
381         } else if (!data) {
382                 pr_debug("Spurious PIO IRQ\n");
383                 return;
384         }
385
386         sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
387         buf = (unsigned short *)(sg_virt + host->sg_off);
388
389         count = host->sg_ptr->length - host->sg_off;
390         if (count > data->blksz)
391                 count = data->blksz;
392
393         pr_debug("count: %08x offset: %08x flags %08x\n",
394                  count, host->sg_off, data->flags);
395
396         /* Transfer the data */
397         if (data->flags & MMC_DATA_READ)
398                 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
399         else
400                 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
401
402         host->sg_off += count;
403
404         tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
405
406         if (host->sg_off == host->sg_ptr->length)
407                 tmio_mmc_next_sg(host);
408
409         return;
410 }
411
412 static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
413 {
414         if (host->sg_ptr == &host->bounce_sg) {
415                 unsigned long flags;
416                 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
417                 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
418                 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
419         }
420 }
421
422 /* needs to be called with host->lock held */
423 void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
424 {
425         struct mmc_data *data = host->data;
426         struct mmc_command *stop;
427
428         host->data = NULL;
429
430         if (!data) {
431                 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
432                 return;
433         }
434         stop = data->stop;
435
436         /* FIXME - return correct transfer count on errors */
437         if (!data->error)
438                 data->bytes_xfered = data->blocks * data->blksz;
439         else
440                 data->bytes_xfered = 0;
441
442         pr_debug("Completed data request\n");
443
444         /*
445          * FIXME: other drivers allow an optional stop command of any given type
446          *        which we dont do, as the chip can auto generate them.
447          *        Perhaps we can be smarter about when to use auto CMD12 and
448          *        only issue the auto request when we know this is the desired
449          *        stop command, allowing fallback to the stop command the
450          *        upper layers expect. For now, we do what works.
451          */
452
453         if (data->flags & MMC_DATA_READ) {
454                 if (host->chan_rx && !host->force_pio)
455                         tmio_mmc_check_bounce_buffer(host);
456                 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
457                         host->mrq);
458         } else {
459                 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
460                         host->mrq);
461         }
462
463         if (stop) {
464                 if (stop->opcode == 12 && !stop->arg)
465                         sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
466                 else
467                         BUG();
468         }
469
470         tmio_mmc_finish_request(host);
471 }
472
473 static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
474 {
475         struct mmc_data *data;
476         spin_lock(&host->lock);
477         data = host->data;
478
479         if (!data)
480                 goto out;
481
482         if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
483                 /*
484                  * Has all data been written out yet? Testing on SuperH showed,
485                  * that in most cases the first interrupt comes already with the
486                  * BUSY status bit clear, but on some operations, like mount or
487                  * in the beginning of a write / sync / umount, there is one
488                  * DATAEND interrupt with the BUSY bit set, in this cases
489                  * waiting for one more interrupt fixes the problem.
490                  */
491                 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
492                         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
493                         tasklet_schedule(&host->dma_complete);
494                 }
495         } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
496                 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
497                 tasklet_schedule(&host->dma_complete);
498         } else {
499                 tmio_mmc_do_data_irq(host);
500                 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
501         }
502 out:
503         spin_unlock(&host->lock);
504 }
505
506 static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
507         unsigned int stat)
508 {
509         struct mmc_command *cmd = host->cmd;
510         int i, addr;
511
512         spin_lock(&host->lock);
513
514         if (!host->cmd) {
515                 pr_debug("Spurious CMD irq\n");
516                 goto out;
517         }
518
519         host->cmd = NULL;
520
521         /* This controller is sicker than the PXA one. Not only do we need to
522          * drop the top 8 bits of the first response word, we also need to
523          * modify the order of the response for short response command types.
524          */
525
526         for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
527                 cmd->resp[i] = sd_ctrl_read32(host, addr);
528
529         if (cmd->flags &  MMC_RSP_136) {
530                 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
531                 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
532                 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
533                 cmd->resp[3] <<= 8;
534         } else if (cmd->flags & MMC_RSP_R3) {
535                 cmd->resp[0] = cmd->resp[3];
536         }
537
538         if (stat & TMIO_STAT_CMDTIMEOUT)
539                 cmd->error = -ETIMEDOUT;
540         else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
541                 cmd->error = -EILSEQ;
542
543         /* If there is data to handle we enable data IRQs here, and
544          * we will ultimatley finish the request in the data_end handler.
545          * If theres no data or we encountered an error, finish now.
546          */
547         if (host->data && !cmd->error) {
548                 if (host->data->flags & MMC_DATA_READ) {
549                         if (host->force_pio || !host->chan_rx)
550                                 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
551                         else
552                                 tasklet_schedule(&host->dma_issue);
553                 } else {
554                         if (host->force_pio || !host->chan_tx)
555                                 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
556                         else
557                                 tasklet_schedule(&host->dma_issue);
558                 }
559         } else {
560                 tmio_mmc_finish_request(host);
561         }
562
563 out:
564         spin_unlock(&host->lock);
565 }
566
567 irqreturn_t tmio_mmc_irq(int irq, void *devid)
568 {
569         struct tmio_mmc_host *host = devid;
570         struct tmio_mmc_data *pdata = host->pdata;
571         unsigned int ireg, irq_mask, status;
572         unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
573
574         pr_debug("MMC IRQ begin\n");
575
576         status = sd_ctrl_read32(host, CTL_STATUS);
577         irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
578         ireg = status & TMIO_MASK_IRQ & ~irq_mask;
579
580         sdio_ireg = 0;
581         if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
582                 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
583                 sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
584                 sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
585
586                 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
587
588                 if (sdio_ireg && !host->sdio_irq_enabled) {
589                         pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
590                                    sdio_status, sdio_irq_mask, sdio_ireg);
591                         tmio_mmc_enable_sdio_irq(host->mmc, 0);
592                         goto out;
593                 }
594
595                 if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
596                         sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
597                         mmc_signal_sdio_irq(host->mmc);
598
599                 if (sdio_ireg)
600                         goto out;
601         }
602
603         pr_debug_status(status);
604         pr_debug_status(ireg);
605
606         if (!ireg) {
607                 tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask);
608
609                 pr_warning("tmio_mmc: Spurious irq, disabling! "
610                         "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
611                 pr_debug_status(status);
612
613                 goto out;
614         }
615
616         while (ireg) {
617                 /* Card insert / remove attempts */
618                 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
619                         tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
620                                 TMIO_STAT_CARD_REMOVE);
621                         mmc_detect_change(host->mmc, msecs_to_jiffies(100));
622                 }
623
624                 /* CRC and other errors */
625 /*              if (ireg & TMIO_STAT_ERR_IRQ)
626  *                      handled |= tmio_error_irq(host, irq, stat);
627  */
628
629                 /* Command completion */
630                 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
631                         tmio_mmc_ack_mmc_irqs(host,
632                                      TMIO_STAT_CMDRESPEND |
633                                      TMIO_STAT_CMDTIMEOUT);
634                         tmio_mmc_cmd_irq(host, status);
635                 }
636
637                 /* Data transfer */
638                 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
639                         tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
640                         tmio_mmc_pio_irq(host);
641                 }
642
643                 /* Data transfer completion */
644                 if (ireg & TMIO_STAT_DATAEND) {
645                         tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
646                         tmio_mmc_data_irq(host);
647                 }
648
649                 /* Check status - keep going until we've handled it all */
650                 status = sd_ctrl_read32(host, CTL_STATUS);
651                 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
652                 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
653
654                 pr_debug("Status at end of loop: %08x\n", status);
655                 pr_debug_status(status);
656         }
657         pr_debug("MMC IRQ end\n");
658
659 out:
660         return IRQ_HANDLED;
661 }
662 EXPORT_SYMBOL(tmio_mmc_irq);
663
664 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
665         struct mmc_data *data)
666 {
667         struct tmio_mmc_data *pdata = host->pdata;
668
669         pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
670                  data->blksz, data->blocks);
671
672         /* Some hardware cannot perform 2 byte requests in 4 bit mode */
673         if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
674                 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
675
676                 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
677                         pr_err("%s: %d byte block unsupported in 4 bit mode\n",
678                                mmc_hostname(host->mmc), data->blksz);
679                         return -EINVAL;
680                 }
681         }
682
683         tmio_mmc_init_sg(host, data);
684         host->data = data;
685
686         /* Set transfer length / blocksize */
687         sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
688         sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
689
690         tmio_mmc_start_dma(host, data);
691
692         return 0;
693 }
694
695 /* Process requests from the MMC layer */
696 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
697 {
698         struct tmio_mmc_host *host = mmc_priv(mmc);
699         unsigned long flags;
700         int ret;
701
702         spin_lock_irqsave(&host->lock, flags);
703
704         if (host->mrq) {
705                 pr_debug("request not null\n");
706                 if (IS_ERR(host->mrq)) {
707                         spin_unlock_irqrestore(&host->lock, flags);
708                         mrq->cmd->error = -EAGAIN;
709                         mmc_request_done(mmc, mrq);
710                         return;
711                 }
712         }
713
714         host->last_req_ts = jiffies;
715         wmb();
716         host->mrq = mrq;
717
718         spin_unlock_irqrestore(&host->lock, flags);
719
720         if (mrq->data) {
721                 ret = tmio_mmc_start_data(host, mrq->data);
722                 if (ret)
723                         goto fail;
724         }
725
726         ret = tmio_mmc_start_command(host, mrq->cmd);
727         if (!ret) {
728                 schedule_delayed_work(&host->delayed_reset_work,
729                                       msecs_to_jiffies(2000));
730                 return;
731         }
732
733 fail:
734         host->force_pio = false;
735         host->mrq = NULL;
736         mrq->cmd->error = ret;
737         mmc_request_done(mmc, mrq);
738 }
739
740 /* Set MMC clock / power.
741  * Note: This controller uses a simple divider scheme therefore it cannot
742  * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
743  * MMC wont run that fast, it has to be clocked at 12MHz which is the next
744  * slowest setting.
745  */
746 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
747 {
748         struct tmio_mmc_host *host = mmc_priv(mmc);
749         struct tmio_mmc_data *pdata = host->pdata;
750         unsigned long flags;
751
752         spin_lock_irqsave(&host->lock, flags);
753         if (host->mrq) {
754                 if (IS_ERR(host->mrq)) {
755                         dev_dbg(&host->pdev->dev,
756                                 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
757                                 current->comm, task_pid_nr(current),
758                                 ios->clock, ios->power_mode);
759                         host->mrq = ERR_PTR(-EINTR);
760                 } else {
761                         dev_dbg(&host->pdev->dev,
762                                 "%s.%d: CMD%u active since %lu, now %lu!\n",
763                                 current->comm, task_pid_nr(current),
764                                 host->mrq->cmd->opcode, host->last_req_ts, jiffies);
765                 }
766                 spin_unlock_irqrestore(&host->lock, flags);
767                 return;
768         }
769
770         host->mrq = ERR_PTR(-EBUSY);
771
772         spin_unlock_irqrestore(&host->lock, flags);
773
774         if (ios->clock)
775                 tmio_mmc_set_clock(host, ios->clock);
776
777         /* Power sequence - OFF -> UP -> ON */
778         if (ios->power_mode == MMC_POWER_UP) {
779                 if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) && !pdata->power) {
780                         pm_runtime_get_sync(&host->pdev->dev);
781                         pdata->power = true;
782                 }
783                 /* power up SD bus */
784                 if (host->set_pwr)
785                         host->set_pwr(host->pdev, 1);
786         } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
787                 /* power down SD bus */
788                 if (ios->power_mode == MMC_POWER_OFF) {
789                         if (host->set_pwr)
790                                 host->set_pwr(host->pdev, 0);
791                         if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) &&
792                             pdata->power) {
793                                 pdata->power = false;
794                                 pm_runtime_put(&host->pdev->dev);
795                         }
796                 }
797                 tmio_mmc_clk_stop(host);
798         } else {
799                 /* start bus clock */
800                 tmio_mmc_clk_start(host);
801         }
802
803         switch (ios->bus_width) {
804         case MMC_BUS_WIDTH_1:
805                 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
806         break;
807         case MMC_BUS_WIDTH_4:
808                 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
809         break;
810         }
811
812         /* Let things settle. delay taken from winCE driver */
813         udelay(140);
814         if (PTR_ERR(host->mrq) == -EINTR)
815                 dev_dbg(&host->pdev->dev,
816                         "%s.%d: IOS interrupted: clk %u, mode %u",
817                         current->comm, task_pid_nr(current),
818                         ios->clock, ios->power_mode);
819         host->mrq = NULL;
820 }
821
822 static int tmio_mmc_get_ro(struct mmc_host *mmc)
823 {
824         struct tmio_mmc_host *host = mmc_priv(mmc);
825         struct tmio_mmc_data *pdata = host->pdata;
826
827         return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
828                 !(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
829 }
830
831 static int tmio_mmc_get_cd(struct mmc_host *mmc)
832 {
833         struct tmio_mmc_host *host = mmc_priv(mmc);
834         struct tmio_mmc_data *pdata = host->pdata;
835
836         if (!pdata->get_cd)
837                 return -ENOSYS;
838         else
839                 return pdata->get_cd(host->pdev);
840 }
841
842 static const struct mmc_host_ops tmio_mmc_ops = {
843         .request        = tmio_mmc_request,
844         .set_ios        = tmio_mmc_set_ios,
845         .get_ro         = tmio_mmc_get_ro,
846         .get_cd         = tmio_mmc_get_cd,
847         .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
848 };
849
850 int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
851                                   struct platform_device *pdev,
852                                   struct tmio_mmc_data *pdata)
853 {
854         struct tmio_mmc_host *_host;
855         struct mmc_host *mmc;
856         struct resource *res_ctl;
857         int ret;
858         u32 irq_mask = TMIO_MASK_CMD;
859
860         res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
861         if (!res_ctl)
862                 return -EINVAL;
863
864         mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
865         if (!mmc)
866                 return -ENOMEM;
867
868         pdata->dev = &pdev->dev;
869         _host = mmc_priv(mmc);
870         _host->pdata = pdata;
871         _host->mmc = mmc;
872         _host->pdev = pdev;
873         platform_set_drvdata(pdev, mmc);
874
875         _host->set_pwr = pdata->set_pwr;
876         _host->set_clk_div = pdata->set_clk_div;
877
878         /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
879         _host->bus_shift = resource_size(res_ctl) >> 10;
880
881         _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
882         if (!_host->ctl) {
883                 ret = -ENOMEM;
884                 goto host_free;
885         }
886
887         mmc->ops = &tmio_mmc_ops;
888         mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
889         mmc->f_max = pdata->hclk;
890         mmc->f_min = mmc->f_max / 512;
891         mmc->max_segs = 32;
892         mmc->max_blk_size = 512;
893         mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
894                 mmc->max_segs;
895         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
896         mmc->max_seg_size = mmc->max_req_size;
897         if (pdata->ocr_mask)
898                 mmc->ocr_avail = pdata->ocr_mask;
899         else
900                 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
901
902         pdata->power = false;
903         pm_runtime_enable(&pdev->dev);
904         ret = pm_runtime_resume(&pdev->dev);
905         if (ret < 0)
906                 goto pm_disable;
907
908         tmio_mmc_clk_stop(_host);
909         tmio_mmc_reset(_host);
910
911         tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
912         if (pdata->flags & TMIO_MMC_SDIO_IRQ)
913                 tmio_mmc_enable_sdio_irq(mmc, 0);
914
915         spin_lock_init(&_host->lock);
916
917         /* Init delayed work for request timeouts */
918         INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
919
920         /* See if we also get DMA */
921         tmio_mmc_request_dma(_host, pdata);
922
923         /* We have to keep the device powered for its card detection to work */
924         if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD))
925                 pm_runtime_get_noresume(&pdev->dev);
926
927         mmc_add_host(mmc);
928
929         /* Unmask the IRQs we want to know about */
930         if (!_host->chan_rx)
931                 irq_mask |= TMIO_MASK_READOP;
932         if (!_host->chan_tx)
933                 irq_mask |= TMIO_MASK_WRITEOP;
934
935         tmio_mmc_enable_mmc_irqs(_host, irq_mask);
936
937         *host = _host;
938
939         return 0;
940
941 pm_disable:
942         pm_runtime_disable(&pdev->dev);
943         iounmap(_host->ctl);
944 host_free:
945         mmc_free_host(mmc);
946
947         return ret;
948 }
949 EXPORT_SYMBOL(tmio_mmc_host_probe);
950
951 void tmio_mmc_host_remove(struct tmio_mmc_host *host)
952 {
953         struct platform_device *pdev = host->pdev;
954
955         /*
956          * We don't have to manipulate pdata->power here: if there is a card in
957          * the slot, the runtime PM is active and our .runtime_resume() will not
958          * be run. If there is no card in the slot and the platform can suspend
959          * the controller, the runtime PM is suspended and pdata->power == false,
960          * so, our .runtime_resume() will not try to detect a card in the slot.
961          */
962         if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD)
963                 pm_runtime_get_sync(&pdev->dev);
964
965         mmc_remove_host(host->mmc);
966         cancel_delayed_work_sync(&host->delayed_reset_work);
967         tmio_mmc_release_dma(host);
968
969         pm_runtime_put_sync(&pdev->dev);
970         pm_runtime_disable(&pdev->dev);
971
972         iounmap(host->ctl);
973         mmc_free_host(host->mmc);
974 }
975 EXPORT_SYMBOL(tmio_mmc_host_remove);
976
977 #ifdef CONFIG_PM
978 int tmio_mmc_host_suspend(struct device *dev)
979 {
980         struct mmc_host *mmc = dev_get_drvdata(dev);
981         struct tmio_mmc_host *host = mmc_priv(mmc);
982         int ret = mmc_suspend_host(mmc);
983
984         if (!ret)
985                 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
986
987         host->pm_error = pm_runtime_put_sync(dev);
988
989         return ret;
990 }
991 EXPORT_SYMBOL(tmio_mmc_host_suspend);
992
993 int tmio_mmc_host_resume(struct device *dev)
994 {
995         struct mmc_host *mmc = dev_get_drvdata(dev);
996         struct tmio_mmc_host *host = mmc_priv(mmc);
997
998         /* The MMC core will perform the complete set up */
999         host->pdata->power = false;
1000
1001         if (!host->pm_error)
1002                 pm_runtime_get_sync(dev);
1003
1004         tmio_mmc_reset(mmc_priv(mmc));
1005         tmio_mmc_request_dma(host, host->pdata);
1006
1007         return mmc_resume_host(mmc);
1008 }
1009 EXPORT_SYMBOL(tmio_mmc_host_resume);
1010
1011 #endif  /* CONFIG_PM */
1012
1013 int tmio_mmc_host_runtime_suspend(struct device *dev)
1014 {
1015         return 0;
1016 }
1017 EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
1018
1019 int tmio_mmc_host_runtime_resume(struct device *dev)
1020 {
1021         struct mmc_host *mmc = dev_get_drvdata(dev);
1022         struct tmio_mmc_host *host = mmc_priv(mmc);
1023         struct tmio_mmc_data *pdata = host->pdata;
1024
1025         tmio_mmc_reset(host);
1026
1027         if (pdata->power) {
1028                 /* Only entered after a card-insert interrupt */
1029                 tmio_mmc_set_ios(mmc, &mmc->ios);
1030                 mmc_detect_change(mmc, msecs_to_jiffies(100));
1031         }
1032
1033         return 0;
1034 }
1035 EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
1036
1037 MODULE_LICENSE("GPL v2");