AT91 MMC 1: Pass host structure.
[pandora-kernel.git] / drivers / mmc / at91_mci.c
1 /*
2  *  linux/drivers/mmc/at91_mci.c - ATMEL AT91RM9200 MCI Driver
3  *
4  *  Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
5  *
6  *  Copyright (C) 2006 Malcolm Noyes
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14    This is the AT91RM9200 MCI driver that has been tested with both MMC cards
15    and SD-cards.  Boards that support write protect are now supported.
16    The CCAT91SBC001 board does not support SD cards.
17
18    The three entry points are at91_mci_request, at91_mci_set_ios
19    and at91_mci_get_ro.
20
21    SET IOS
22      This configures the device to put it into the correct mode and clock speed
23      required.
24
25    MCI REQUEST
26      MCI request processes the commands sent in the mmc_request structure. This
27      can consist of a processing command and a stop command in the case of
28      multiple block transfers.
29
30      There are three main types of request, commands, reads and writes.
31
32      Commands are straight forward. The command is submitted to the controller and
33      the request function returns. When the controller generates an interrupt to indicate
34      the command is finished, the response to the command are read and the mmc_request_done
35      function called to end the request.
36
37      Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38      controller to manage the transfers.
39
40      A read is done from the controller directly to the scatterlist passed in from the request.
41      Due to a bug in the controller, when a read is completed, all the words are byte
42      swapped in the scatterlist buffers.
43
44      The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
45
46      A write is slightly different in that the bytes to write are read from the scatterlist
47      into a dma memory buffer (this is in case the source buffer should be read only). The
48      entire write buffer is then done from this single dma memory buffer.
49
50      The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
51
52    GET RO
53      Gets the status of the write protect pin, if available.
54 */
55
56 #include <linux/module.h>
57 #include <linux/moduleparam.h>
58 #include <linux/init.h>
59 #include <linux/ioport.h>
60 #include <linux/platform_device.h>
61 #include <linux/interrupt.h>
62 #include <linux/blkdev.h>
63 #include <linux/delay.h>
64 #include <linux/err.h>
65 #include <linux/dma-mapping.h>
66 #include <linux/clk.h>
67
68 #include <linux/mmc/host.h>
69 #include <linux/mmc/protocol.h>
70
71 #include <asm/io.h>
72 #include <asm/irq.h>
73 #include <asm/mach/mmc.h>
74 #include <asm/arch/board.h>
75 #include <asm/arch/gpio.h>
76 #include <asm/arch/at91_mci.h>
77 #include <asm/arch/at91_pdc.h>
78
79 #define DRIVER_NAME "at91_mci"
80
81 #undef  SUPPORT_4WIRE
82
83 static struct clk *mci_clk;
84
85 #define FL_SENT_COMMAND (1 << 0)
86 #define FL_SENT_STOP (1 << 1)
87
88
89
90 #define at91_mci_read(host, reg)        __raw_readl((host)->baseaddr + (reg))
91 #define at91_mci_write(host, reg, val)  __raw_writel((val), (host)->baseaddr + (reg))
92
93
94 /*
95  * Low level type for this driver
96  */
97 struct at91mci_host
98 {
99         struct mmc_host *mmc;
100         struct mmc_command *cmd;
101         struct mmc_request *request;
102
103         void __iomem *baseaddr;
104
105         struct at91_mmc_data *board;
106         int present;
107
108         /*
109          * Flag indicating when the command has been sent. This is used to
110          * work out whether or not to send the stop
111          */
112         unsigned int flags;
113         /* flag for current bus settings */
114         u32 bus_mode;
115
116         /* DMA buffer used for transmitting */
117         unsigned int* buffer;
118         dma_addr_t physical_address;
119         unsigned int total_length;
120
121         /* Latest in the scatterlist that has been enabled for transfer, but not freed */
122         int in_use_index;
123
124         /* Latest in the scatterlist that has been enabled for transfer */
125         int transfer_index;
126 };
127
128 /*
129  * Copy from sg to a dma block - used for transfers
130  */
131 static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
132 {
133         unsigned int len, i, size;
134         unsigned *dmabuf = host->buffer;
135
136         size = host->total_length;
137         len = data->sg_len;
138
139         /*
140          * Just loop through all entries. Size might not
141          * be the entire list though so make sure that
142          * we do not transfer too much.
143          */
144         for (i = 0; i < len; i++) {
145                 struct scatterlist *sg;
146                 int amount;
147                 int index;
148                 unsigned int *sgbuffer;
149
150                 sg = &data->sg[i];
151
152                 sgbuffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
153                 amount = min(size, sg->length);
154                 size -= amount;
155                 amount /= 4;
156
157                 for (index = 0; index < amount; index++)
158                         *dmabuf++ = swab32(sgbuffer[index]);
159
160                 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
161
162                 if (size == 0)
163                         break;
164         }
165
166         /*
167          * Check that we didn't get a request to transfer
168          * more data than can fit into the SG list.
169          */
170         BUG_ON(size != 0);
171 }
172
173 /*
174  * Prepare a dma read
175  */
176 static void at91mci_pre_dma_read(struct at91mci_host *host)
177 {
178         int i;
179         struct scatterlist *sg;
180         struct mmc_command *cmd;
181         struct mmc_data *data;
182
183         pr_debug("pre dma read\n");
184
185         cmd = host->cmd;
186         if (!cmd) {
187                 pr_debug("no command\n");
188                 return;
189         }
190
191         data = cmd->data;
192         if (!data) {
193                 pr_debug("no data\n");
194                 return;
195         }
196
197         for (i = 0; i < 2; i++) {
198                 /* nothing left to transfer */
199                 if (host->transfer_index >= data->sg_len) {
200                         pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
201                         break;
202                 }
203
204                 /* Check to see if this needs filling */
205                 if (i == 0) {
206                         if (at91_mci_read(host, AT91_PDC_RCR) != 0) {
207                                 pr_debug("Transfer active in current\n");
208                                 continue;
209                         }
210                 }
211                 else {
212                         if (at91_mci_read(host, AT91_PDC_RNCR) != 0) {
213                                 pr_debug("Transfer active in next\n");
214                                 continue;
215                         }
216                 }
217
218                 /* Setup the next transfer */
219                 pr_debug("Using transfer index %d\n", host->transfer_index);
220
221                 sg = &data->sg[host->transfer_index++];
222                 pr_debug("sg = %p\n", sg);
223
224                 sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
225
226                 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
227
228                 if (i == 0) {
229                         at91_mci_write(host, AT91_PDC_RPR, sg->dma_address);
230                         at91_mci_write(host, AT91_PDC_RCR, sg->length / 4);
231                 }
232                 else {
233                         at91_mci_write(host, AT91_PDC_RNPR, sg->dma_address);
234                         at91_mci_write(host, AT91_PDC_RNCR, sg->length / 4);
235                 }
236         }
237
238         pr_debug("pre dma read done\n");
239 }
240
241 /*
242  * Handle after a dma read
243  */
244 static void at91mci_post_dma_read(struct at91mci_host *host)
245 {
246         struct mmc_command *cmd;
247         struct mmc_data *data;
248
249         pr_debug("post dma read\n");
250
251         cmd = host->cmd;
252         if (!cmd) {
253                 pr_debug("no command\n");
254                 return;
255         }
256
257         data = cmd->data;
258         if (!data) {
259                 pr_debug("no data\n");
260                 return;
261         }
262
263         while (host->in_use_index < host->transfer_index) {
264                 unsigned int *buffer;
265                 int index;
266                 int len;
267
268                 struct scatterlist *sg;
269
270                 pr_debug("finishing index %d\n", host->in_use_index);
271
272                 sg = &data->sg[host->in_use_index++];
273
274                 pr_debug("Unmapping page %08X\n", sg->dma_address);
275
276                 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
277
278                 /* Swap the contents of the buffer */
279                 buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
280                 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
281
282                 data->bytes_xfered += sg->length;
283
284                 len = sg->length / 4;
285
286                 for (index = 0; index < len; index++) {
287                         buffer[index] = swab32(buffer[index]);
288                 }
289                 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
290                 flush_dcache_page(sg->page);
291         }
292
293         /* Is there another transfer to trigger? */
294         if (host->transfer_index < data->sg_len)
295                 at91mci_pre_dma_read(host);
296         else {
297                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
298                 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
299         }
300
301         pr_debug("post dma read done\n");
302 }
303
304 /*
305  * Handle transmitted data
306  */
307 static void at91_mci_handle_transmitted(struct at91mci_host *host)
308 {
309         struct mmc_command *cmd;
310         struct mmc_data *data;
311
312         pr_debug("Handling the transmit\n");
313
314         /* Disable the transfer */
315         at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
316
317         /* Now wait for cmd ready */
318         at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
319         at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
320
321         cmd = host->cmd;
322         if (!cmd) return;
323
324         data = cmd->data;
325         if (!data) return;
326
327         data->bytes_xfered = host->total_length;
328 }
329
330 /*
331  * Enable the controller
332  */
333 static void at91_mci_enable(struct at91mci_host *host)
334 {
335         at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
336         at91_mci_write(host, AT91_MCI_IDR, 0xFFFFFFFF);
337         at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
338         at91_mci_write(host, AT91_MCI_MR, 0x834A);
339         at91_mci_write(host, AT91_MCI_SDCR, 0x0);
340 }
341
342 /*
343  * Disable the controller
344  */
345 static void at91_mci_disable(struct at91mci_host *host)
346 {
347         at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
348 }
349
350 /*
351  * Send a command
352  * return the interrupts to enable
353  */
354 static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
355 {
356         unsigned int cmdr, mr;
357         unsigned int block_length;
358         struct mmc_data *data = cmd->data;
359
360         unsigned int blocks;
361         unsigned int ier = 0;
362
363         host->cmd = cmd;
364
365         /* Not sure if this is needed */
366 #if 0
367         if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
368                 pr_debug("Clearing timeout\n");
369                 at91_mci_write(host, AT91_MCI_ARGR, 0);
370                 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
371                 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
372                         /* spin */
373                         pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
374                 }
375         }
376 #endif
377         cmdr = cmd->opcode;
378
379         if (mmc_resp_type(cmd) == MMC_RSP_NONE)
380                 cmdr |= AT91_MCI_RSPTYP_NONE;
381         else {
382                 /* if a response is expected then allow maximum response latancy */
383                 cmdr |= AT91_MCI_MAXLAT;
384                 /* set 136 bit response for R2, 48 bit response otherwise */
385                 if (mmc_resp_type(cmd) == MMC_RSP_R2)
386                         cmdr |= AT91_MCI_RSPTYP_136;
387                 else
388                         cmdr |= AT91_MCI_RSPTYP_48;
389         }
390
391         if (data) {
392                 block_length = data->blksz;
393                 blocks = data->blocks;
394
395                 /* always set data start - also set direction flag for read */
396                 if (data->flags & MMC_DATA_READ)
397                         cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
398                 else if (data->flags & MMC_DATA_WRITE)
399                         cmdr |= AT91_MCI_TRCMD_START;
400
401                 if (data->flags & MMC_DATA_STREAM)
402                         cmdr |= AT91_MCI_TRTYP_STREAM;
403                 if (data->flags & MMC_DATA_MULTI)
404                         cmdr |= AT91_MCI_TRTYP_MULTIPLE;
405         }
406         else {
407                 block_length = 0;
408                 blocks = 0;
409         }
410
411         if (cmd->opcode == MMC_STOP_TRANSMISSION)
412                 cmdr |= AT91_MCI_TRCMD_STOP;
413
414         if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
415                 cmdr |= AT91_MCI_OPDCMD;
416
417         /*
418          * Set the arguments and send the command
419          */
420         pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08lX)\n",
421                 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
422
423         if (!data) {
424                 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
425                 at91_mci_write(host, AT91_PDC_RPR, 0);
426                 at91_mci_write(host, AT91_PDC_RCR, 0);
427                 at91_mci_write(host, AT91_PDC_RNPR, 0);
428                 at91_mci_write(host, AT91_PDC_RNCR, 0);
429                 at91_mci_write(host, AT91_PDC_TPR, 0);
430                 at91_mci_write(host, AT91_PDC_TCR, 0);
431                 at91_mci_write(host, AT91_PDC_TNPR, 0);
432                 at91_mci_write(host, AT91_PDC_TNCR, 0);
433
434                 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
435                 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
436                 return AT91_MCI_CMDRDY;
437         }
438
439         mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; /* zero block length and PDC mode */
440         at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
441
442         /*
443          * Disable the PDC controller
444          */
445         at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
446
447         if (cmdr & AT91_MCI_TRCMD_START) {
448                 data->bytes_xfered = 0;
449                 host->transfer_index = 0;
450                 host->in_use_index = 0;
451                 if (cmdr & AT91_MCI_TRDIR) {
452                         /*
453                          * Handle a read
454                          */
455                         host->buffer = NULL;
456                         host->total_length = 0;
457
458                         at91mci_pre_dma_read(host);
459                         ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
460                 }
461                 else {
462                         /*
463                          * Handle a write
464                          */
465                         host->total_length = block_length * blocks;
466                         host->buffer = dma_alloc_coherent(NULL,
467                                                   host->total_length,
468                                                   &host->physical_address, GFP_KERNEL);
469
470                         at91mci_sg_to_dma(host, data);
471
472                         pr_debug("Transmitting %d bytes\n", host->total_length);
473
474                         at91_mci_write(host, AT91_PDC_TPR, host->physical_address);
475                         at91_mci_write(host, AT91_PDC_TCR, host->total_length / 4);
476                         ier = AT91_MCI_TXBUFE;
477                 }
478         }
479
480         /*
481          * Send the command and then enable the PDC - not the other way round as
482          * the data sheet says
483          */
484
485         at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
486         at91_mci_write(host, AT91_MCI_CMDR, cmdr);
487
488         if (cmdr & AT91_MCI_TRCMD_START) {
489                 if (cmdr & AT91_MCI_TRDIR)
490                         at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTEN);
491                 else
492                         at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTEN);
493         }
494         return ier;
495 }
496
497 /*
498  * Wait for a command to complete
499  */
500 static void at91mci_process_command(struct at91mci_host *host, struct mmc_command *cmd)
501 {
502         unsigned int ier;
503
504         ier = at91_mci_send_command(host, cmd);
505
506         pr_debug("setting ier to %08X\n", ier);
507
508         /* Stop on errors or the required value */
509         at91_mci_write(host, AT91_MCI_IER, 0xffff0000 | ier);
510 }
511
512 /*
513  * Process the next step in the request
514  */
515 static void at91mci_process_next(struct at91mci_host *host)
516 {
517         if (!(host->flags & FL_SENT_COMMAND)) {
518                 host->flags |= FL_SENT_COMMAND;
519                 at91mci_process_command(host, host->request->cmd);
520         }
521         else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
522                 host->flags |= FL_SENT_STOP;
523                 at91mci_process_command(host, host->request->stop);
524         }
525         else
526                 mmc_request_done(host->mmc, host->request);
527 }
528
529 /*
530  * Handle a command that has been completed
531  */
532 static void at91mci_completed_command(struct at91mci_host *host)
533 {
534         struct mmc_command *cmd = host->cmd;
535         unsigned int status;
536
537         at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
538
539         cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
540         cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
541         cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
542         cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
543
544         if (host->buffer) {
545                 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
546                 host->buffer = NULL;
547         }
548
549         status = at91_mci_read(host, AT91_MCI_SR);
550
551         pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
552                  status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
553
554         if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
555                         AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
556                         AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
557                 if ((status & AT91_MCI_RCRCE) &&
558                         ((cmd->opcode == MMC_SEND_OP_COND) || (cmd->opcode == SD_APP_OP_COND))) {
559                         cmd->error = MMC_ERR_NONE;
560                 }
561                 else {
562                         if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
563                                 cmd->error = MMC_ERR_TIMEOUT;
564                         else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
565                                 cmd->error = MMC_ERR_BADCRC;
566                         else if (status & (AT91_MCI_OVRE | AT91_MCI_UNRE))
567                                 cmd->error = MMC_ERR_FIFO;
568                         else
569                                 cmd->error = MMC_ERR_FAILED;
570
571                         pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
572                                  cmd->error, cmd->opcode, cmd->retries);
573                 }
574         }
575         else
576                 cmd->error = MMC_ERR_NONE;
577
578         at91mci_process_next(host);
579 }
580
581 /*
582  * Handle an MMC request
583  */
584 static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
585 {
586         struct at91mci_host *host = mmc_priv(mmc);
587         host->request = mrq;
588         host->flags = 0;
589
590         at91mci_process_next(host);
591 }
592
593 /*
594  * Set the IOS
595  */
596 static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
597 {
598         int clkdiv;
599         struct at91mci_host *host = mmc_priv(mmc);
600         unsigned long at91_master_clock = clk_get_rate(mci_clk);
601
602         host->bus_mode = ios->bus_mode;
603
604         if (ios->clock == 0) {
605                 /* Disable the MCI controller */
606                 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
607                 clkdiv = 0;
608         }
609         else {
610                 /* Enable the MCI controller */
611                 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
612
613                 if ((at91_master_clock % (ios->clock * 2)) == 0)
614                         clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
615                 else
616                         clkdiv = (at91_master_clock / ios->clock) / 2;
617
618                 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
619                         at91_master_clock / (2 * (clkdiv + 1)));
620         }
621         if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
622                 pr_debug("MMC: Setting controller bus width to 4\n");
623                 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
624         }
625         else {
626                 pr_debug("MMC: Setting controller bus width to 1\n");
627                 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
628         }
629
630         /* Set the clock divider */
631         at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
632
633         /* maybe switch power to the card */
634         if (host->board->vcc_pin) {
635                 switch (ios->power_mode) {
636                         case MMC_POWER_OFF:
637                                 at91_set_gpio_output(host->board->vcc_pin, 0);
638                                 break;
639                         case MMC_POWER_UP:
640                         case MMC_POWER_ON:
641                                 at91_set_gpio_output(host->board->vcc_pin, 1);
642                                 break;
643                 }
644         }
645 }
646
647 /*
648  * Handle an interrupt
649  */
650 static irqreturn_t at91_mci_irq(int irq, void *devid)
651 {
652         struct at91mci_host *host = devid;
653         int completed = 0;
654
655         unsigned int int_status;
656
657         int_status = at91_mci_read(host, AT91_MCI_SR);
658         pr_debug("MCI irq: status = %08X, %08lX, %08lX\n", int_status, at91_mci_read(host, AT91_MCI_IMR),
659                 int_status & at91_mci_read(host, AT91_MCI_IMR));
660
661         if ((int_status & at91_mci_read(host, AT91_MCI_IMR)) & 0xffff0000)
662                 completed = 1;
663
664         int_status &= at91_mci_read(host, AT91_MCI_IMR);
665
666         if (int_status & AT91_MCI_UNRE)
667                 pr_debug("MMC: Underrun error\n");
668         if (int_status & AT91_MCI_OVRE)
669                 pr_debug("MMC: Overrun error\n");
670         if (int_status & AT91_MCI_DTOE)
671                 pr_debug("MMC: Data timeout\n");
672         if (int_status & AT91_MCI_DCRCE)
673                 pr_debug("MMC: CRC error in data\n");
674         if (int_status & AT91_MCI_RTOE)
675                 pr_debug("MMC: Response timeout\n");
676         if (int_status & AT91_MCI_RENDE)
677                 pr_debug("MMC: Response end bit error\n");
678         if (int_status & AT91_MCI_RCRCE)
679                 pr_debug("MMC: Response CRC error\n");
680         if (int_status & AT91_MCI_RDIRE)
681                 pr_debug("MMC: Response direction error\n");
682         if (int_status & AT91_MCI_RINDE)
683                 pr_debug("MMC: Response index error\n");
684
685         /* Only continue processing if no errors */
686         if (!completed) {
687                 if (int_status & AT91_MCI_TXBUFE) {
688                         pr_debug("TX buffer empty\n");
689                         at91_mci_handle_transmitted(host);
690                 }
691
692                 if (int_status & AT91_MCI_RXBUFF) {
693                         pr_debug("RX buffer full\n");
694                         at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
695                 }
696
697                 if (int_status & AT91_MCI_ENDTX) {
698                         pr_debug("Transmit has ended\n");
699                 }
700
701                 if (int_status & AT91_MCI_ENDRX) {
702                         pr_debug("Receive has ended\n");
703                         at91mci_post_dma_read(host);
704                 }
705
706                 if (int_status & AT91_MCI_NOTBUSY) {
707                         pr_debug("Card is ready\n");
708                         at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
709                 }
710
711                 if (int_status & AT91_MCI_DTIP) {
712                         pr_debug("Data transfer in progress\n");
713                 }
714
715                 if (int_status & AT91_MCI_BLKE) {
716                         pr_debug("Block transfer has ended\n");
717                 }
718
719                 if (int_status & AT91_MCI_TXRDY) {
720                         pr_debug("Ready to transmit\n");
721                 }
722
723                 if (int_status & AT91_MCI_RXRDY) {
724                         pr_debug("Ready to receive\n");
725                 }
726
727                 if (int_status & AT91_MCI_CMDRDY) {
728                         pr_debug("Command ready\n");
729                         completed = 1;
730                 }
731         }
732         at91_mci_write(host, AT91_MCI_IDR, int_status);
733
734         if (completed) {
735                 pr_debug("Completed command\n");
736                 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
737                 at91mci_completed_command(host);
738         }
739
740         return IRQ_HANDLED;
741 }
742
743 static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
744 {
745         struct at91mci_host *host = _host;
746         int present = !at91_get_gpio_value(irq);
747
748         /*
749          * we expect this irq on both insert and remove,
750          * and use a short delay to debounce.
751          */
752         if (present != host->present) {
753                 host->present = present;
754                 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
755                         present ? "insert" : "remove");
756                 if (!present) {
757                         pr_debug("****** Resetting SD-card bus width ******\n");
758                         at91_mci_write(host, AT91_MCI_SDCR, 0);
759                 }
760                 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
761         }
762         return IRQ_HANDLED;
763 }
764
765 int at91_mci_get_ro(struct mmc_host *mmc)
766 {
767         int read_only = 0;
768         struct at91mci_host *host = mmc_priv(mmc);
769
770         if (host->board->wp_pin) {
771                 read_only = at91_get_gpio_value(host->board->wp_pin);
772                 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
773                                 (read_only ? "read-only" : "read-write") );
774         }
775         else {
776                 printk(KERN_WARNING "%s: host does not support reading read-only "
777                                 "switch.  Assuming write-enable.\n", mmc_hostname(mmc));
778         }
779         return read_only;
780 }
781
782 static const struct mmc_host_ops at91_mci_ops = {
783         .request        = at91_mci_request,
784         .set_ios        = at91_mci_set_ios,
785         .get_ro         = at91_mci_get_ro,
786 };
787
788 /*
789  * Probe for the device
790  */
791 static int at91_mci_probe(struct platform_device *pdev)
792 {
793         struct mmc_host *mmc;
794         struct at91mci_host *host;
795         int ret;
796
797         pr_debug("Probe MCI devices\n");
798
799         mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
800         if (!mmc) {
801                 pr_debug("Failed to allocate mmc host\n");
802                 return -ENOMEM;
803         }
804
805         mmc->ops = &at91_mci_ops;
806         mmc->f_min = 375000;
807         mmc->f_max = 25000000;
808         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
809         mmc->caps = MMC_CAP_BYTEBLOCK;
810
811         host = mmc_priv(mmc);
812         host->mmc = mmc;
813         host->buffer = NULL;
814         host->bus_mode = 0;
815         host->board = pdev->dev.platform_data;
816         if (host->board->wire4) {
817 #ifdef SUPPORT_4WIRE
818                 mmc->caps |= MMC_CAP_4_BIT_DATA;
819 #else
820                 printk("MMC: 4 wire bus mode not supported by this driver - using 1 wire\n");
821 #endif
822         }
823
824         /*
825          * Get Clock
826          */
827         mci_clk = clk_get(&pdev->dev, "mci_clk");
828         if (IS_ERR(mci_clk)) {
829                 printk(KERN_ERR "AT91 MMC: no clock defined.\n");
830                 mmc_free_host(mmc);
831                 return -ENODEV;
832         }
833         clk_enable(mci_clk);                    /* Enable the peripheral clock */
834
835         host->baseaddr = (void __iomem *)AT91_VA_BASE_MCI;
836
837         /*
838          * Reset hardware
839          */
840         at91_mci_disable(host);
841         at91_mci_enable(host);
842
843         /*
844          * Allocate the MCI interrupt
845          */
846         ret = request_irq(AT91RM9200_ID_MCI, at91_mci_irq, IRQF_SHARED, DRIVER_NAME, host);
847         if (ret) {
848                 printk(KERN_ERR "Failed to request MCI interrupt\n");
849                 clk_disable(mci_clk);
850                 clk_put(mci_clk);
851                 mmc_free_host(mmc);
852                 return ret;
853         }
854
855         platform_set_drvdata(pdev, mmc);
856
857         /*
858          * Add host to MMC layer
859          */
860         if (host->board->det_pin)
861                 host->present = !at91_get_gpio_value(host->board->det_pin);
862         else
863                 host->present = -1;
864
865         mmc_add_host(mmc);
866
867         /*
868          * monitor card insertion/removal if we can
869          */
870         if (host->board->det_pin) {
871                 ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
872                                 0, DRIVER_NAME, host);
873                 if (ret)
874                         printk(KERN_ERR "couldn't allocate MMC detect irq\n");
875         }
876
877         pr_debug(KERN_INFO "Added MCI driver\n");
878
879         return 0;
880 }
881
882 /*
883  * Remove a device
884  */
885 static int at91_mci_remove(struct platform_device *pdev)
886 {
887         struct mmc_host *mmc = platform_get_drvdata(pdev);
888         struct at91mci_host *host;
889
890         if (!mmc)
891                 return -1;
892
893         host = mmc_priv(mmc);
894
895         if (host->present != -1) {
896                 free_irq(host->board->det_pin, host);
897                 cancel_delayed_work(&host->mmc->detect);
898         }
899
900         mmc_remove_host(mmc);
901         at91_mci_disable(host);
902         free_irq(AT91RM9200_ID_MCI, host);
903         mmc_free_host(mmc);
904
905         clk_disable(mci_clk);                           /* Disable the peripheral clock */
906         clk_put(mci_clk);
907
908         platform_set_drvdata(pdev, NULL);
909
910         pr_debug("MCI Removed\n");
911
912         return 0;
913 }
914
915 #ifdef CONFIG_PM
916 static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
917 {
918         struct mmc_host *mmc = platform_get_drvdata(pdev);
919         int ret = 0;
920
921         if (mmc)
922                 ret = mmc_suspend_host(mmc, state);
923
924         return ret;
925 }
926
927 static int at91_mci_resume(struct platform_device *pdev)
928 {
929         struct mmc_host *mmc = platform_get_drvdata(pdev);
930         int ret = 0;
931
932         if (mmc)
933                 ret = mmc_resume_host(mmc);
934
935         return ret;
936 }
937 #else
938 #define at91_mci_suspend        NULL
939 #define at91_mci_resume         NULL
940 #endif
941
942 static struct platform_driver at91_mci_driver = {
943         .probe          = at91_mci_probe,
944         .remove         = at91_mci_remove,
945         .suspend        = at91_mci_suspend,
946         .resume         = at91_mci_resume,
947         .driver         = {
948                 .name   = DRIVER_NAME,
949                 .owner  = THIS_MODULE,
950         },
951 };
952
953 static int __init at91_mci_init(void)
954 {
955         return platform_driver_register(&at91_mci_driver);
956 }
957
958 static void __exit at91_mci_exit(void)
959 {
960         platform_driver_unregister(&at91_mci_driver);
961 }
962
963 module_init(at91_mci_init);
964 module_exit(at91_mci_exit);
965
966 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
967 MODULE_AUTHOR("Nick Randell");
968 MODULE_LICENSE("GPL");