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