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