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