atmel-mci: Don't stop the clock between transfers
[safe/jmp/linux-2.6] / drivers / mmc / host / atmel-mci.c
1 /*
2  * Atmel MultiMedia Card Interface driver
3  *
4  * Copyright (C) 2004-2008 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/blkdev.h>
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/seq_file.h>
23 #include <linux/stat.h>
24
25 #include <linux/mmc/host.h>
26
27 #include <asm/atmel-mci.h>
28 #include <asm/io.h>
29 #include <asm/unaligned.h>
30
31 #include <mach/board.h>
32
33 #include "atmel-mci-regs.h"
34
35 #define ATMCI_DATA_ERROR_FLAGS  (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
36
37 enum {
38         EVENT_CMD_COMPLETE = 0,
39         EVENT_XFER_COMPLETE,
40         EVENT_DATA_COMPLETE,
41         EVENT_DATA_ERROR,
42 };
43
44 enum atmel_mci_state {
45         STATE_SENDING_CMD = 0,
46         STATE_SENDING_DATA,
47         STATE_DATA_BUSY,
48         STATE_SENDING_STOP,
49         STATE_DATA_ERROR,
50 };
51
52 struct atmel_mci {
53         struct mmc_host         *mmc;
54         void __iomem            *regs;
55
56         struct scatterlist      *sg;
57         unsigned int            pio_offset;
58
59         struct mmc_request      *mrq;
60         struct mmc_command      *cmd;
61         struct mmc_data         *data;
62
63         u32                     cmd_status;
64         u32                     data_status;
65         u32                     stop_cmdr;
66
67         u32                     mode_reg;
68         u32                     sdc_reg;
69
70         struct tasklet_struct   tasklet;
71         unsigned long           pending_events;
72         unsigned long           completed_events;
73         enum atmel_mci_state    state;
74
75         int                     present;
76         int                     detect_pin;
77         int                     wp_pin;
78
79         /* For detect pin debouncing */
80         struct timer_list       detect_timer;
81
82         unsigned long           bus_hz;
83         unsigned long           mapbase;
84         struct clk              *mck;
85         struct platform_device  *pdev;
86 };
87
88 #define atmci_test_and_clear_pending(host, event)               \
89         test_and_clear_bit(event, &host->pending_events)
90 #define atmci_set_completed(host, event)                        \
91         set_bit(event, &host->completed_events)
92 #define atmci_set_pending(host, event)                          \
93         set_bit(event, &host->pending_events)
94
95 /*
96  * The debugfs stuff below is mostly optimized away when
97  * CONFIG_DEBUG_FS is not set.
98  */
99 static int atmci_req_show(struct seq_file *s, void *v)
100 {
101         struct atmel_mci        *host = s->private;
102         struct mmc_request      *mrq = host->mrq;
103         struct mmc_command      *cmd;
104         struct mmc_command      *stop;
105         struct mmc_data         *data;
106
107         /* Make sure we get a consistent snapshot */
108         spin_lock_irq(&host->mmc->lock);
109
110         if (mrq) {
111                 cmd = mrq->cmd;
112                 data = mrq->data;
113                 stop = mrq->stop;
114
115                 if (cmd)
116                         seq_printf(s,
117                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
118                                 cmd->opcode, cmd->arg, cmd->flags,
119                                 cmd->resp[0], cmd->resp[1], cmd->resp[2],
120                                 cmd->resp[2], cmd->error);
121                 if (data)
122                         seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
123                                 data->bytes_xfered, data->blocks,
124                                 data->blksz, data->flags, data->error);
125                 if (stop)
126                         seq_printf(s,
127                                 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
128                                 stop->opcode, stop->arg, stop->flags,
129                                 stop->resp[0], stop->resp[1], stop->resp[2],
130                                 stop->resp[2], stop->error);
131         }
132
133         spin_unlock_irq(&host->mmc->lock);
134
135         return 0;
136 }
137
138 static int atmci_req_open(struct inode *inode, struct file *file)
139 {
140         return single_open(file, atmci_req_show, inode->i_private);
141 }
142
143 static const struct file_operations atmci_req_fops = {
144         .owner          = THIS_MODULE,
145         .open           = atmci_req_open,
146         .read           = seq_read,
147         .llseek         = seq_lseek,
148         .release        = single_release,
149 };
150
151 static void atmci_show_status_reg(struct seq_file *s,
152                 const char *regname, u32 value)
153 {
154         static const char       *sr_bit[] = {
155                 [0]     = "CMDRDY",
156                 [1]     = "RXRDY",
157                 [2]     = "TXRDY",
158                 [3]     = "BLKE",
159                 [4]     = "DTIP",
160                 [5]     = "NOTBUSY",
161                 [8]     = "SDIOIRQA",
162                 [9]     = "SDIOIRQB",
163                 [16]    = "RINDE",
164                 [17]    = "RDIRE",
165                 [18]    = "RCRCE",
166                 [19]    = "RENDE",
167                 [20]    = "RTOE",
168                 [21]    = "DCRCE",
169                 [22]    = "DTOE",
170                 [30]    = "OVRE",
171                 [31]    = "UNRE",
172         };
173         unsigned int            i;
174
175         seq_printf(s, "%s:\t0x%08x", regname, value);
176         for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
177                 if (value & (1 << i)) {
178                         if (sr_bit[i])
179                                 seq_printf(s, " %s", sr_bit[i]);
180                         else
181                                 seq_puts(s, " UNKNOWN");
182                 }
183         }
184         seq_putc(s, '\n');
185 }
186
187 static int atmci_regs_show(struct seq_file *s, void *v)
188 {
189         struct atmel_mci        *host = s->private;
190         u32                     *buf;
191
192         buf = kmalloc(MCI_REGS_SIZE, GFP_KERNEL);
193         if (!buf)
194                 return -ENOMEM;
195
196         /* Grab a more or less consistent snapshot */
197         spin_lock_irq(&host->mmc->lock);
198         clk_enable(host->mck);
199         memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
200         clk_disable(host->mck);
201         spin_unlock_irq(&host->mmc->lock);
202
203         seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
204                         buf[MCI_MR / 4],
205                         buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
206                         buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
207                         buf[MCI_MR / 4] & 0xff);
208         seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
209         seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
210         seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
211         seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
212                         buf[MCI_BLKR / 4],
213                         buf[MCI_BLKR / 4] & 0xffff,
214                         (buf[MCI_BLKR / 4] >> 16) & 0xffff);
215
216         /* Don't read RSPR and RDR; it will consume the data there */
217
218         atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
219         atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
220
221         kfree(buf);
222
223         return 0;
224 }
225
226 static int atmci_regs_open(struct inode *inode, struct file *file)
227 {
228         return single_open(file, atmci_regs_show, inode->i_private);
229 }
230
231 static const struct file_operations atmci_regs_fops = {
232         .owner          = THIS_MODULE,
233         .open           = atmci_regs_open,
234         .read           = seq_read,
235         .llseek         = seq_lseek,
236         .release        = single_release,
237 };
238
239 static void atmci_init_debugfs(struct atmel_mci *host)
240 {
241         struct mmc_host *mmc;
242         struct dentry   *root;
243         struct dentry   *node;
244
245         mmc = host->mmc;
246         root = mmc->debugfs_root;
247         if (!root)
248                 return;
249
250         node = debugfs_create_file("regs", S_IRUSR, root, host,
251                         &atmci_regs_fops);
252         if (IS_ERR(node))
253                 return;
254         if (!node)
255                 goto err;
256
257         node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops);
258         if (!node)
259                 goto err;
260
261         node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
262         if (!node)
263                 goto err;
264
265         node = debugfs_create_x32("pending_events", S_IRUSR, root,
266                                      (u32 *)&host->pending_events);
267         if (!node)
268                 goto err;
269
270         node = debugfs_create_x32("completed_events", S_IRUSR, root,
271                                      (u32 *)&host->completed_events);
272         if (!node)
273                 goto err;
274
275         return;
276
277 err:
278         dev_err(&host->pdev->dev,
279                 "failed to initialize debugfs for controller\n");
280 }
281
282 static inline unsigned int ns_to_clocks(struct atmel_mci *host,
283                                         unsigned int ns)
284 {
285         return (ns * (host->bus_hz / 1000000) + 999) / 1000;
286 }
287
288 static void atmci_set_timeout(struct atmel_mci *host,
289                               struct mmc_data *data)
290 {
291         static unsigned dtomul_to_shift[] = {
292                 0, 4, 7, 8, 10, 12, 16, 20
293         };
294         unsigned        timeout;
295         unsigned        dtocyc;
296         unsigned        dtomul;
297
298         timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
299
300         for (dtomul = 0; dtomul < 8; dtomul++) {
301                 unsigned shift = dtomul_to_shift[dtomul];
302                 dtocyc = (timeout + (1 << shift) - 1) >> shift;
303                 if (dtocyc < 15)
304                         break;
305         }
306
307         if (dtomul >= 8) {
308                 dtomul = 7;
309                 dtocyc = 15;
310         }
311
312         dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
313                         dtocyc << dtomul_to_shift[dtomul]);
314         mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
315 }
316
317 /*
318  * Return mask with command flags to be enabled for this command.
319  */
320 static u32 atmci_prepare_command(struct mmc_host *mmc,
321                                  struct mmc_command *cmd)
322 {
323         struct mmc_data *data;
324         u32             cmdr;
325
326         cmd->error = -EINPROGRESS;
327
328         cmdr = MCI_CMDR_CMDNB(cmd->opcode);
329
330         if (cmd->flags & MMC_RSP_PRESENT) {
331                 if (cmd->flags & MMC_RSP_136)
332                         cmdr |= MCI_CMDR_RSPTYP_136BIT;
333                 else
334                         cmdr |= MCI_CMDR_RSPTYP_48BIT;
335         }
336
337         /*
338          * This should really be MAXLAT_5 for CMD2 and ACMD41, but
339          * it's too difficult to determine whether this is an ACMD or
340          * not. Better make it 64.
341          */
342         cmdr |= MCI_CMDR_MAXLAT_64CYC;
343
344         if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
345                 cmdr |= MCI_CMDR_OPDCMD;
346
347         data = cmd->data;
348         if (data) {
349                 cmdr |= MCI_CMDR_START_XFER;
350                 if (data->flags & MMC_DATA_STREAM)
351                         cmdr |= MCI_CMDR_STREAM;
352                 else if (data->blocks > 1)
353                         cmdr |= MCI_CMDR_MULTI_BLOCK;
354                 else
355                         cmdr |= MCI_CMDR_BLOCK;
356
357                 if (data->flags & MMC_DATA_READ)
358                         cmdr |= MCI_CMDR_TRDIR_READ;
359         }
360
361         return cmdr;
362 }
363
364 static void atmci_start_command(struct atmel_mci *host,
365                                 struct mmc_command *cmd,
366                                 u32 cmd_flags)
367 {
368         WARN_ON(host->cmd);
369         host->cmd = cmd;
370
371         dev_vdbg(&host->mmc->class_dev,
372                         "start command: ARGR=0x%08x CMDR=0x%08x\n",
373                         cmd->arg, cmd_flags);
374
375         mci_writel(host, ARGR, cmd->arg);
376         mci_writel(host, CMDR, cmd_flags);
377 }
378
379 static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
380 {
381         struct atmel_mci *host = mmc_priv(mmc);
382
383         atmci_start_command(host, data->stop, host->stop_cmdr);
384         mci_writel(host, IER, MCI_CMDRDY);
385 }
386
387 static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
388 {
389         struct atmel_mci *host = mmc_priv(mmc);
390
391         WARN_ON(host->cmd || host->data);
392         host->mrq = NULL;
393
394         mmc_request_done(mmc, mrq);
395 }
396
397 /*
398  * Returns a mask of interrupt flags to be enabled after the whole
399  * request has been prepared.
400  */
401 static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
402 {
403         struct atmel_mci        *host = mmc_priv(mmc);
404         u32                     iflags;
405
406         data->error = -EINPROGRESS;
407
408         WARN_ON(host->data);
409         host->sg = NULL;
410         host->data = data;
411
412         dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
413                         MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
414
415         iflags = ATMCI_DATA_ERROR_FLAGS;
416         host->sg = data->sg;
417         host->pio_offset = 0;
418         if (data->flags & MMC_DATA_READ)
419                 iflags |= MCI_RXRDY;
420         else
421                 iflags |= MCI_TXRDY;
422
423         return iflags;
424 }
425
426 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
427 {
428         struct atmel_mci        *host = mmc_priv(mmc);
429         struct mmc_data         *data;
430         struct mmc_command      *cmd;
431         u32                     iflags;
432         u32                     cmdflags = 0;
433
434         iflags = mci_readl(host, IMR);
435         if (iflags)
436                 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
437                                 mci_readl(host, IMR));
438
439         WARN_ON(host->mrq != NULL);
440
441         /*
442          * We may "know" the card is gone even though there's still an
443          * electrical connection. If so, we really need to communicate
444          * this to the MMC core since there won't be any more
445          * interrupts as the card is completely removed. Otherwise,
446          * the MMC core might believe the card is still there even
447          * though the card was just removed very slowly.
448          */
449         if (!host->present) {
450                 mrq->cmd->error = -ENOMEDIUM;
451                 mmc_request_done(mmc, mrq);
452                 return;
453         }
454
455         host->mrq = mrq;
456         host->pending_events = 0;
457         host->completed_events = 0;
458         host->state = STATE_SENDING_CMD;
459
460         /* We don't support multiple blocks of weird lengths. */
461         data = mrq->data;
462         if (data) {
463                 if (data->blocks > 1 && data->blksz & 3)
464                         goto fail;
465                 atmci_set_timeout(host, data);
466
467                 /* Must set block count/size before sending command */
468                 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
469                                 | MCI_BLKLEN(data->blksz));
470         }
471
472         iflags = MCI_CMDRDY;
473         cmd = mrq->cmd;
474         cmdflags = atmci_prepare_command(mmc, cmd);
475         atmci_start_command(host, cmd, cmdflags);
476
477         if (data)
478                 iflags |= atmci_submit_data(mmc, data);
479
480         if (mrq->stop) {
481                 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
482                 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
483                 if (!(data->flags & MMC_DATA_WRITE))
484                         host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
485                 if (data->flags & MMC_DATA_STREAM)
486                         host->stop_cmdr |= MCI_CMDR_STREAM;
487                 else
488                         host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
489         }
490
491         /*
492          * We could have enabled interrupts earlier, but I suspect
493          * that would open up a nice can of interesting race
494          * conditions (e.g. command and data complete, but stop not
495          * prepared yet.)
496          */
497         mci_writel(host, IER, iflags);
498
499         return;
500
501 fail:
502         host->mrq = NULL;
503         mrq->cmd->error = -EINVAL;
504         mmc_request_done(mmc, mrq);
505 }
506
507 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
508 {
509         struct atmel_mci        *host = mmc_priv(mmc);
510
511         switch (ios->bus_width) {
512         case MMC_BUS_WIDTH_1:
513                 host->sdc_reg = 0;
514                 break;
515         case MMC_BUS_WIDTH_4:
516                 host->sdc_reg = MCI_SDCBUS_4BIT;
517                 break;
518         }
519
520         if (ios->clock) {
521                 u32 clkdiv;
522
523                 if (!host->mode_reg)
524                         clk_enable(host->mck);
525
526                 /* Set clock rate */
527                 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
528                 if (clkdiv > 255) {
529                         dev_warn(&mmc->class_dev,
530                                 "clock %u too slow; using %lu\n",
531                                 ios->clock, host->bus_hz / (2 * 256));
532                         clkdiv = 255;
533                 }
534
535                 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
536                                         | MCI_MR_RDPROOF;
537
538                 mci_writel(host, CR, MCI_CR_MCIEN);
539                 mci_writel(host, MR, host->mode_reg);
540                 mci_writel(host, SDCR, host->sdc_reg);
541         } else {
542                 mci_writel(host, CR, MCI_CR_MCIDIS);
543                 if (host->mode_reg) {
544                         mci_readl(host, MR);
545                         clk_disable(host->mck);
546                 }
547                 host->mode_reg = 0;
548         }
549
550         switch (ios->power_mode) {
551         default:
552                 /*
553                  * TODO: None of the currently available AVR32-based
554                  * boards allow MMC power to be turned off. Implement
555                  * power control when this can be tested properly.
556                  */
557                 break;
558         }
559 }
560
561 static int atmci_get_ro(struct mmc_host *mmc)
562 {
563         int                     read_only = 0;
564         struct atmel_mci        *host = mmc_priv(mmc);
565
566         if (gpio_is_valid(host->wp_pin)) {
567                 read_only = gpio_get_value(host->wp_pin);
568                 dev_dbg(&mmc->class_dev, "card is %s\n",
569                                 read_only ? "read-only" : "read-write");
570         } else {
571                 dev_dbg(&mmc->class_dev,
572                         "no pin for checking read-only switch."
573                         " Assuming write-enable.\n");
574         }
575
576         return read_only;
577 }
578
579 static struct mmc_host_ops atmci_ops = {
580         .request        = atmci_request,
581         .set_ios        = atmci_set_ios,
582         .get_ro         = atmci_get_ro,
583 };
584
585 static void atmci_command_complete(struct atmel_mci *host,
586                         struct mmc_command *cmd)
587 {
588         u32             status = host->cmd_status;
589
590         /* Read the response from the card (up to 16 bytes) */
591         cmd->resp[0] = mci_readl(host, RSPR);
592         cmd->resp[1] = mci_readl(host, RSPR);
593         cmd->resp[2] = mci_readl(host, RSPR);
594         cmd->resp[3] = mci_readl(host, RSPR);
595
596         if (status & MCI_RTOE)
597                 cmd->error = -ETIMEDOUT;
598         else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
599                 cmd->error = -EILSEQ;
600         else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
601                 cmd->error = -EIO;
602         else
603                 cmd->error = 0;
604
605         if (cmd->error) {
606                 dev_dbg(&host->mmc->class_dev,
607                         "command error: status=0x%08x\n", status);
608
609                 if (cmd->data) {
610                         host->data = NULL;
611                         mci_writel(host, IDR, MCI_NOTBUSY
612                                         | MCI_TXRDY | MCI_RXRDY
613                                         | ATMCI_DATA_ERROR_FLAGS);
614                 }
615         }
616 }
617
618 static void atmci_detect_change(unsigned long data)
619 {
620         struct atmel_mci *host = (struct atmel_mci *)data;
621         struct mmc_request *mrq = host->mrq;
622         int present;
623
624         /*
625          * atmci_remove() sets detect_pin to -1 before freeing the
626          * interrupt. We must not re-enable the interrupt if it has
627          * been freed.
628          */
629         smp_rmb();
630         if (!gpio_is_valid(host->detect_pin))
631                 return;
632
633         enable_irq(gpio_to_irq(host->detect_pin));
634         present = !gpio_get_value(host->detect_pin);
635
636         dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
637                         present, host->present);
638
639         if (present != host->present) {
640                 dev_dbg(&host->mmc->class_dev, "card %s\n",
641                         present ? "inserted" : "removed");
642                 host->present = present;
643
644                 /* Reset controller if card is gone */
645                 if (!present) {
646                         mci_writel(host, CR, MCI_CR_SWRST);
647                         mci_writel(host, IDR, ~0UL);
648                         mci_writel(host, CR, MCI_CR_MCIEN);
649                 }
650
651                 /* Clean up queue if present */
652                 if (mrq) {
653                         /*
654                          * Reset controller to terminate any ongoing
655                          * commands or data transfers.
656                          */
657                         mci_writel(host, CR, MCI_CR_SWRST);
658                         mci_readl(host, SR);
659
660                         host->data = NULL;
661                         host->cmd = NULL;
662
663                         switch (host->state) {
664                         case STATE_SENDING_CMD:
665                                 mrq->cmd->error = -ENOMEDIUM;
666                                 if (!mrq->data)
667                                         break;
668                                 /* fall through */
669                         case STATE_SENDING_DATA:
670                                 mrq->data->error = -ENOMEDIUM;
671                                 break;
672                         case STATE_DATA_BUSY:
673                         case STATE_DATA_ERROR:
674                                 if (mrq->data->error == -EINPROGRESS)
675                                         mrq->data->error = -ENOMEDIUM;
676                                 if (!mrq->stop)
677                                         break;
678                                 /* fall through */
679                         case STATE_SENDING_STOP:
680                                 mrq->stop->error = -ENOMEDIUM;
681                                 break;
682                         }
683
684                         atmci_request_end(host->mmc, mrq);
685                 }
686
687                 mmc_detect_change(host->mmc, 0);
688         }
689 }
690
691 static void atmci_tasklet_func(unsigned long priv)
692 {
693         struct mmc_host         *mmc = (struct mmc_host *)priv;
694         struct atmel_mci        *host = mmc_priv(mmc);
695         struct mmc_request      *mrq = host->mrq;
696         struct mmc_data         *data = host->data;
697         struct mmc_command      *cmd = host->cmd;
698         enum atmel_mci_state    state = host->state;
699         enum atmel_mci_state    prev_state;
700         u32                     status;
701
702         state = host->state;
703
704         dev_vdbg(&mmc->class_dev,
705                 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
706                 state, host->pending_events, host->completed_events,
707                 mci_readl(host, IMR));
708
709         do {
710                 prev_state = state;
711
712                 switch (state) {
713                 case STATE_SENDING_CMD:
714                         if (!atmci_test_and_clear_pending(host,
715                                                 EVENT_CMD_COMPLETE))
716                                 break;
717
718                         host->cmd = NULL;
719                         atmci_set_completed(host, EVENT_CMD_COMPLETE);
720                         atmci_command_complete(host, mrq->cmd);
721                         if (!mrq->data || cmd->error) {
722                                 atmci_request_end(mmc, host->mrq);
723                                 break;
724                         }
725
726                         prev_state = state = STATE_SENDING_DATA;
727                         /* fall through */
728
729                 case STATE_SENDING_DATA:
730                         if (atmci_test_and_clear_pending(host,
731                                                 EVENT_DATA_ERROR)) {
732                                 if (data->stop)
733                                         send_stop_cmd(host->mmc, data);
734                                 state = STATE_DATA_ERROR;
735                                 break;
736                         }
737
738                         if (!atmci_test_and_clear_pending(host,
739                                                 EVENT_XFER_COMPLETE))
740                                 break;
741
742                         atmci_set_completed(host, EVENT_XFER_COMPLETE);
743                         prev_state = state = STATE_DATA_BUSY;
744                         /* fall through */
745
746                 case STATE_DATA_BUSY:
747                         if (!atmci_test_and_clear_pending(host,
748                                                 EVENT_DATA_COMPLETE))
749                                 break;
750
751                         host->data = NULL;
752                         atmci_set_completed(host, EVENT_DATA_COMPLETE);
753                         status = host->data_status;
754                         if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
755                                 if (status & MCI_DTOE) {
756                                         dev_dbg(&mmc->class_dev,
757                                                         "data timeout error\n");
758                                         data->error = -ETIMEDOUT;
759                                 } else if (status & MCI_DCRCE) {
760                                         dev_dbg(&mmc->class_dev,
761                                                         "data CRC error\n");
762                                         data->error = -EILSEQ;
763                                 } else {
764                                         dev_dbg(&mmc->class_dev,
765                                                 "data FIFO error (status=%08x)\n",
766                                                 status);
767                                         data->error = -EIO;
768                                 }
769                         } else {
770                                 data->bytes_xfered = data->blocks * data->blksz;
771                                 data->error = 0;
772                         }
773
774                         if (!data->stop) {
775                                 atmci_request_end(mmc, host->mrq);
776                                 prev_state = state;
777                                 break;
778                         }
779
780                         prev_state = state = STATE_SENDING_STOP;
781                         if (!data->error)
782                                 send_stop_cmd(host->mmc, data);
783                         /* fall through */
784
785                 case STATE_SENDING_STOP:
786                         if (!atmci_test_and_clear_pending(host,
787                                                 EVENT_CMD_COMPLETE))
788                                 break;
789
790                         host->cmd = NULL;
791                         atmci_command_complete(host, mrq->stop);
792                         atmci_request_end(mmc, host->mrq);
793                         prev_state = state;
794                         break;
795
796                 case STATE_DATA_ERROR:
797                         if (!atmci_test_and_clear_pending(host,
798                                                 EVENT_XFER_COMPLETE))
799                                 break;
800
801                         state = STATE_DATA_BUSY;
802                         break;
803                 }
804         } while (state != prev_state);
805
806         host->state = state;
807 }
808
809 static void atmci_read_data_pio(struct atmel_mci *host)
810 {
811         struct scatterlist      *sg = host->sg;
812         void                    *buf = sg_virt(sg);
813         unsigned int            offset = host->pio_offset;
814         struct mmc_data         *data = host->data;
815         u32                     value;
816         u32                     status;
817         unsigned int            nbytes = 0;
818
819         do {
820                 value = mci_readl(host, RDR);
821                 if (likely(offset + 4 <= sg->length)) {
822                         put_unaligned(value, (u32 *)(buf + offset));
823
824                         offset += 4;
825                         nbytes += 4;
826
827                         if (offset == sg->length) {
828                                 host->sg = sg = sg_next(sg);
829                                 if (!sg)
830                                         goto done;
831
832                                 offset = 0;
833                                 buf = sg_virt(sg);
834                         }
835                 } else {
836                         unsigned int remaining = sg->length - offset;
837                         memcpy(buf + offset, &value, remaining);
838                         nbytes += remaining;
839
840                         flush_dcache_page(sg_page(sg));
841                         host->sg = sg = sg_next(sg);
842                         if (!sg)
843                                 goto done;
844
845                         offset = 4 - remaining;
846                         buf = sg_virt(sg);
847                         memcpy(buf, (u8 *)&value + remaining, offset);
848                         nbytes += offset;
849                 }
850
851                 status = mci_readl(host, SR);
852                 if (status & ATMCI_DATA_ERROR_FLAGS) {
853                         mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
854                                                 | ATMCI_DATA_ERROR_FLAGS));
855                         host->data_status = status;
856                         atmci_set_pending(host, EVENT_DATA_ERROR);
857                         tasklet_schedule(&host->tasklet);
858                         break;
859                 }
860         } while (status & MCI_RXRDY);
861
862         host->pio_offset = offset;
863         data->bytes_xfered += nbytes;
864
865         return;
866
867 done:
868         mci_writel(host, IDR, MCI_RXRDY);
869         mci_writel(host, IER, MCI_NOTBUSY);
870         data->bytes_xfered += nbytes;
871         atmci_set_pending(host, EVENT_XFER_COMPLETE);
872 }
873
874 static void atmci_write_data_pio(struct atmel_mci *host)
875 {
876         struct scatterlist      *sg = host->sg;
877         void                    *buf = sg_virt(sg);
878         unsigned int            offset = host->pio_offset;
879         struct mmc_data         *data = host->data;
880         u32                     value;
881         u32                     status;
882         unsigned int            nbytes = 0;
883
884         do {
885                 if (likely(offset + 4 <= sg->length)) {
886                         value = get_unaligned((u32 *)(buf + offset));
887                         mci_writel(host, TDR, value);
888
889                         offset += 4;
890                         nbytes += 4;
891                         if (offset == sg->length) {
892                                 host->sg = sg = sg_next(sg);
893                                 if (!sg)
894                                         goto done;
895
896                                 offset = 0;
897                                 buf = sg_virt(sg);
898                         }
899                 } else {
900                         unsigned int remaining = sg->length - offset;
901
902                         value = 0;
903                         memcpy(&value, buf + offset, remaining);
904                         nbytes += remaining;
905
906                         host->sg = sg = sg_next(sg);
907                         if (!sg) {
908                                 mci_writel(host, TDR, value);
909                                 goto done;
910                         }
911
912                         offset = 4 - remaining;
913                         buf = sg_virt(sg);
914                         memcpy((u8 *)&value + remaining, buf, offset);
915                         mci_writel(host, TDR, value);
916                         nbytes += offset;
917                 }
918
919                 status = mci_readl(host, SR);
920                 if (status & ATMCI_DATA_ERROR_FLAGS) {
921                         mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
922                                                 | ATMCI_DATA_ERROR_FLAGS));
923                         host->data_status = status;
924                         atmci_set_pending(host, EVENT_DATA_ERROR);
925                         tasklet_schedule(&host->tasklet);
926                         break;
927                 }
928         } while (status & MCI_TXRDY);
929
930         host->pio_offset = offset;
931         data->bytes_xfered += nbytes;
932
933         return;
934
935 done:
936         mci_writel(host, IDR, MCI_TXRDY);
937         mci_writel(host, IER, MCI_NOTBUSY);
938         data->bytes_xfered += nbytes;
939         atmci_set_pending(host, EVENT_XFER_COMPLETE);
940 }
941
942 static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
943 {
944         struct atmel_mci        *host = mmc_priv(mmc);
945
946         mci_writel(host, IDR, MCI_CMDRDY);
947
948         host->cmd_status = status;
949         atmci_set_pending(host, EVENT_CMD_COMPLETE);
950         tasklet_schedule(&host->tasklet);
951 }
952
953 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
954 {
955         struct mmc_host         *mmc = dev_id;
956         struct atmel_mci        *host = mmc_priv(mmc);
957         u32                     status, mask, pending;
958         unsigned int            pass_count = 0;
959
960         spin_lock(&mmc->lock);
961
962         do {
963                 status = mci_readl(host, SR);
964                 mask = mci_readl(host, IMR);
965                 pending = status & mask;
966                 if (!pending)
967                         break;
968
969                 if (pending & ATMCI_DATA_ERROR_FLAGS) {
970                         mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
971                                         | MCI_RXRDY | MCI_TXRDY);
972                         pending &= mci_readl(host, IMR);
973                         host->data_status = status;
974                         atmci_set_pending(host, EVENT_DATA_ERROR);
975                         tasklet_schedule(&host->tasklet);
976                 }
977                 if (pending & MCI_NOTBUSY) {
978                         mci_writel(host, IDR,
979                                         ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY);
980                         host->data_status = status;
981                         atmci_set_pending(host, EVENT_DATA_COMPLETE);
982                         tasklet_schedule(&host->tasklet);
983                 }
984                 if (pending & MCI_RXRDY)
985                         atmci_read_data_pio(host);
986                 if (pending & MCI_TXRDY)
987                         atmci_write_data_pio(host);
988
989                 if (pending & MCI_CMDRDY)
990                         atmci_cmd_interrupt(mmc, status);
991         } while (pass_count++ < 5);
992
993         spin_unlock(&mmc->lock);
994
995         return pass_count ? IRQ_HANDLED : IRQ_NONE;
996 }
997
998 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
999 {
1000         struct mmc_host         *mmc = dev_id;
1001         struct atmel_mci        *host = mmc_priv(mmc);
1002
1003         /*
1004          * Disable interrupts until the pin has stabilized and check
1005          * the state then. Use mod_timer() since we may be in the
1006          * middle of the timer routine when this interrupt triggers.
1007          */
1008         disable_irq_nosync(irq);
1009         mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
1010
1011         return IRQ_HANDLED;
1012 }
1013
1014 static int __init atmci_probe(struct platform_device *pdev)
1015 {
1016         struct mci_platform_data        *pdata;
1017         struct atmel_mci *host;
1018         struct mmc_host *mmc;
1019         struct resource *regs;
1020         int irq;
1021         int ret;
1022
1023         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1024         if (!regs)
1025                 return -ENXIO;
1026         pdata = pdev->dev.platform_data;
1027         if (!pdata)
1028                 return -ENXIO;
1029         irq = platform_get_irq(pdev, 0);
1030         if (irq < 0)
1031                 return irq;
1032
1033         mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1034         if (!mmc)
1035                 return -ENOMEM;
1036
1037         host = mmc_priv(mmc);
1038         host->pdev = pdev;
1039         host->mmc = mmc;
1040         host->detect_pin = pdata->detect_pin;
1041         host->wp_pin = pdata->wp_pin;
1042
1043         host->mck = clk_get(&pdev->dev, "mci_clk");
1044         if (IS_ERR(host->mck)) {
1045                 ret = PTR_ERR(host->mck);
1046                 goto err_clk_get;
1047         }
1048
1049         ret = -ENOMEM;
1050         host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1051         if (!host->regs)
1052                 goto err_ioremap;
1053
1054         clk_enable(host->mck);
1055         mci_writel(host, CR, MCI_CR_SWRST);
1056         host->bus_hz = clk_get_rate(host->mck);
1057         clk_disable(host->mck);
1058
1059         host->mapbase = regs->start;
1060
1061         mmc->ops = &atmci_ops;
1062         mmc->f_min = (host->bus_hz + 511) / 512;
1063         mmc->f_max = host->bus_hz / 2;
1064         mmc->ocr_avail  = MMC_VDD_32_33 | MMC_VDD_33_34;
1065         mmc->caps |= MMC_CAP_4_BIT_DATA;
1066
1067         mmc->max_hw_segs = 64;
1068         mmc->max_phys_segs = 64;
1069         mmc->max_req_size = 32768 * 512;
1070         mmc->max_blk_size = 32768;
1071         mmc->max_blk_count = 512;
1072
1073         tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1074
1075         ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
1076         if (ret)
1077                 goto err_request_irq;
1078
1079         /* Assume card is present if we don't have a detect pin */
1080         host->present = 1;
1081         if (gpio_is_valid(host->detect_pin)) {
1082                 if (gpio_request(host->detect_pin, "mmc_detect")) {
1083                         dev_dbg(&mmc->class_dev, "no detect pin available\n");
1084                         host->detect_pin = -1;
1085                 } else {
1086                         host->present = !gpio_get_value(host->detect_pin);
1087                 }
1088         }
1089
1090         if (!gpio_is_valid(host->detect_pin))
1091                 mmc->caps |= MMC_CAP_NEEDS_POLL;
1092
1093         if (gpio_is_valid(host->wp_pin)) {
1094                 if (gpio_request(host->wp_pin, "mmc_wp")) {
1095                         dev_dbg(&mmc->class_dev, "no WP pin available\n");
1096                         host->wp_pin = -1;
1097                 }
1098         }
1099
1100         platform_set_drvdata(pdev, host);
1101
1102         mmc_add_host(mmc);
1103
1104         if (gpio_is_valid(host->detect_pin)) {
1105                 setup_timer(&host->detect_timer, atmci_detect_change,
1106                                 (unsigned long)host);
1107
1108                 ret = request_irq(gpio_to_irq(host->detect_pin),
1109                                 atmci_detect_interrupt,
1110                                 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1111                                 "mmc-detect", mmc);
1112                 if (ret) {
1113                         dev_dbg(&mmc->class_dev,
1114                                 "could not request IRQ %d for detect pin\n",
1115                                 gpio_to_irq(host->detect_pin));
1116                         gpio_free(host->detect_pin);
1117                         host->detect_pin = -1;
1118                 }
1119         }
1120
1121         dev_info(&mmc->class_dev,
1122                         "Atmel MCI controller at 0x%08lx irq %d\n",
1123                         host->mapbase, irq);
1124
1125         atmci_init_debugfs(host);
1126
1127         return 0;
1128
1129 err_request_irq:
1130         iounmap(host->regs);
1131 err_ioremap:
1132         clk_put(host->mck);
1133 err_clk_get:
1134         mmc_free_host(mmc);
1135         return ret;
1136 }
1137
1138 static int __exit atmci_remove(struct platform_device *pdev)
1139 {
1140         struct atmel_mci *host = platform_get_drvdata(pdev);
1141
1142         platform_set_drvdata(pdev, NULL);
1143
1144         if (host) {
1145                 /* Debugfs stuff is cleaned up by mmc core */
1146
1147                 if (gpio_is_valid(host->detect_pin)) {
1148                         int pin = host->detect_pin;
1149
1150                         /* Make sure the timer doesn't enable the interrupt */
1151                         host->detect_pin = -1;
1152                         smp_wmb();
1153
1154                         free_irq(gpio_to_irq(pin), host->mmc);
1155                         del_timer_sync(&host->detect_timer);
1156                         gpio_free(pin);
1157                 }
1158
1159                 mmc_remove_host(host->mmc);
1160
1161                 clk_enable(host->mck);
1162                 mci_writel(host, IDR, ~0UL);
1163                 mci_writel(host, CR, MCI_CR_MCIDIS);
1164                 mci_readl(host, SR);
1165                 clk_disable(host->mck);
1166
1167                 if (gpio_is_valid(host->wp_pin))
1168                         gpio_free(host->wp_pin);
1169
1170                 free_irq(platform_get_irq(pdev, 0), host->mmc);
1171                 iounmap(host->regs);
1172
1173                 clk_put(host->mck);
1174
1175                 mmc_free_host(host->mmc);
1176         }
1177         return 0;
1178 }
1179
1180 static struct platform_driver atmci_driver = {
1181         .remove         = __exit_p(atmci_remove),
1182         .driver         = {
1183                 .name           = "atmel_mci",
1184         },
1185 };
1186
1187 static int __init atmci_init(void)
1188 {
1189         return platform_driver_probe(&atmci_driver, atmci_probe);
1190 }
1191
1192 static void __exit atmci_exit(void)
1193 {
1194         platform_driver_unregister(&atmci_driver);
1195 }
1196
1197 module_init(atmci_init);
1198 module_exit(atmci_exit);
1199
1200 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1201 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1202 MODULE_LICENSE("GPL v2");