]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/host/at91_mci.c
mmc: remove confusing flag
[linux-2.6-omap-h63xx.git] / drivers / mmc / host / at91_mci.c
1 /*
2  *  linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 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 AT91 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 AT91RM9200 controller, when a read is completed, all the words are byte
42      swapped in the scatterlist buffers.  AT91SAM926x are not affected by this bug.
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 #include <linux/atmel_pdc.h>
68
69 #include <linux/mmc/host.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/cpu.h>
76 #include <asm/arch/gpio.h>
77 #include <asm/arch/at91_mci.h>
78
79 #define DRIVER_NAME "at91_mci"
80
81 #define FL_SENT_COMMAND (1 << 0)
82 #define FL_SENT_STOP    (1 << 1)
83
84 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE       \
85                 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE               \
86                 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
87
88 #define at91_mci_read(host, reg)        __raw_readl((host)->baseaddr + (reg))
89 #define at91_mci_write(host, reg, val)  __raw_writel((val), (host)->baseaddr + (reg))
90
91
92 /*
93  * Low level type for this driver
94  */
95 struct at91mci_host
96 {
97         struct mmc_host *mmc;
98         struct mmc_command *cmd;
99         struct mmc_request *request;
100
101         void __iomem *baseaddr;
102         int irq;
103
104         struct at91_mmc_data *board;
105         int present;
106
107         struct clk *mci_clk;
108
109         /*
110          * Flag indicating when the command has been sent. This is used to
111          * work out whether or not to send the stop
112          */
113         unsigned int flags;
114         /* flag for current bus settings */
115         u32 bus_mode;
116
117         /* DMA buffer used for transmitting */
118         unsigned int* buffer;
119         dma_addr_t physical_address;
120         unsigned int total_length;
121
122         /* Latest in the scatterlist that has been enabled for transfer, but not freed */
123         int in_use_index;
124
125         /* Latest in the scatterlist that has been enabled for transfer */
126         int transfer_index;
127 };
128
129 /*
130  * Copy from sg to a dma block - used for transfers
131  */
132 static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
133 {
134         unsigned int len, i, size;
135         unsigned *dmabuf = host->buffer;
136
137         size = host->total_length;
138         len = data->sg_len;
139
140         /*
141          * Just loop through all entries. Size might not
142          * be the entire list though so make sure that
143          * we do not transfer too much.
144          */
145         for (i = 0; i < len; i++) {
146                 struct scatterlist *sg;
147                 int amount;
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
156                 if (cpu_is_at91rm9200()) {      /* AT91RM9200 errata */
157                         int index;
158
159                         for (index = 0; index < (amount / 4); index++)
160                                 *dmabuf++ = swab32(sgbuffer[index]);
161                 }
162                 else
163                         memcpy(dmabuf, sgbuffer, amount);
164
165                 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
166
167                 if (size == 0)
168                         break;
169         }
170
171         /*
172          * Check that we didn't get a request to transfer
173          * more data than can fit into the SG list.
174          */
175         BUG_ON(size != 0);
176 }
177
178 /*
179  * Prepare a dma read
180  */
181 static void at91_mci_pre_dma_read(struct at91mci_host *host)
182 {
183         int i;
184         struct scatterlist *sg;
185         struct mmc_command *cmd;
186         struct mmc_data *data;
187
188         pr_debug("pre dma read\n");
189
190         cmd = host->cmd;
191         if (!cmd) {
192                 pr_debug("no command\n");
193                 return;
194         }
195
196         data = cmd->data;
197         if (!data) {
198                 pr_debug("no data\n");
199                 return;
200         }
201
202         for (i = 0; i < 2; i++) {
203                 /* nothing left to transfer */
204                 if (host->transfer_index >= data->sg_len) {
205                         pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
206                         break;
207                 }
208
209                 /* Check to see if this needs filling */
210                 if (i == 0) {
211                         if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
212                                 pr_debug("Transfer active in current\n");
213                                 continue;
214                         }
215                 }
216                 else {
217                         if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
218                                 pr_debug("Transfer active in next\n");
219                                 continue;
220                         }
221                 }
222
223                 /* Setup the next transfer */
224                 pr_debug("Using transfer index %d\n", host->transfer_index);
225
226                 sg = &data->sg[host->transfer_index++];
227                 pr_debug("sg = %p\n", sg);
228
229                 sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
230
231                 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
232
233                 if (i == 0) {
234                         at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
235                         at91_mci_write(host, ATMEL_PDC_RCR, sg->length / 4);
236                 }
237                 else {
238                         at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
239                         at91_mci_write(host, ATMEL_PDC_RNCR, sg->length / 4);
240                 }
241         }
242
243         pr_debug("pre dma read done\n");
244 }
245
246 /*
247  * Handle after a dma read
248  */
249 static void at91_mci_post_dma_read(struct at91mci_host *host)
250 {
251         struct mmc_command *cmd;
252         struct mmc_data *data;
253
254         pr_debug("post dma read\n");
255
256         cmd = host->cmd;
257         if (!cmd) {
258                 pr_debug("no command\n");
259                 return;
260         }
261
262         data = cmd->data;
263         if (!data) {
264                 pr_debug("no data\n");
265                 return;
266         }
267
268         while (host->in_use_index < host->transfer_index) {
269                 struct scatterlist *sg;
270
271                 pr_debug("finishing index %d\n", host->in_use_index);
272
273                 sg = &data->sg[host->in_use_index++];
274
275                 pr_debug("Unmapping page %08X\n", sg->dma_address);
276
277                 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
278
279                 data->bytes_xfered += sg->length;
280
281                 if (cpu_is_at91rm9200()) {      /* AT91RM9200 errata */
282                         unsigned int *buffer;
283                         int index;
284
285                         /* Swap the contents of the buffer */
286                         buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
287                         pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
288
289                         for (index = 0; index < (sg->length / 4); index++)
290                                 buffer[index] = swab32(buffer[index]);
291
292                         kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
293                 }
294
295                 flush_dcache_page(sg->page);
296         }
297
298         /* Is there another transfer to trigger? */
299         if (host->transfer_index < data->sg_len)
300                 at91_mci_pre_dma_read(host);
301         else {
302                 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
303                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
304         }
305
306         pr_debug("post dma read done\n");
307 }
308
309 /*
310  * Handle transmitted data
311  */
312 static void at91_mci_handle_transmitted(struct at91mci_host *host)
313 {
314         struct mmc_command *cmd;
315         struct mmc_data *data;
316
317         pr_debug("Handling the transmit\n");
318
319         /* Disable the transfer */
320         at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
321
322         /* Now wait for cmd ready */
323         at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
324
325         cmd = host->cmd;
326         if (!cmd) return;
327
328         data = cmd->data;
329         if (!data) return;
330
331         if (cmd->data->blocks > 1) {
332                 pr_debug("multiple write : wait for BLKE...\n");
333                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
334         } else
335                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
336
337         data->bytes_xfered = host->total_length;
338 }
339
340 /*Handle after command sent ready*/
341 static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
342 {
343         if (!host->cmd)
344                 return 1;
345         else if (!host->cmd->data) {
346                 if (host->flags & FL_SENT_STOP) {
347                         /*After multi block write, we must wait for NOTBUSY*/
348                         at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
349                 } else return 1;
350         } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
351                 /*After sendding multi-block-write command, start DMA transfer*/
352                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE);
353                 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
354                 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
355         }
356
357         /* command not completed, have to wait */
358         return 0;
359 }
360
361
362 /*
363  * Enable the controller
364  */
365 static void at91_mci_enable(struct at91mci_host *host)
366 {
367         unsigned int mr;
368
369         at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
370         at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
371         at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
372         mr = AT91_MCI_PDCMODE | 0x34a;
373
374         if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
375                 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
376
377         at91_mci_write(host, AT91_MCI_MR, mr);
378
379         /* use Slot A or B (only one at same time) */
380         at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
381 }
382
383 /*
384  * Disable the controller
385  */
386 static void at91_mci_disable(struct at91mci_host *host)
387 {
388         at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
389 }
390
391 /*
392  * Send a command
393  */
394 static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
395 {
396         unsigned int cmdr, mr;
397         unsigned int block_length;
398         struct mmc_data *data = cmd->data;
399
400         unsigned int blocks;
401         unsigned int ier = 0;
402
403         host->cmd = cmd;
404
405         /* Needed for leaving busy state before CMD1 */
406         if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
407                 pr_debug("Clearing timeout\n");
408                 at91_mci_write(host, AT91_MCI_ARGR, 0);
409                 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
410                 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
411                         /* spin */
412                         pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
413                 }
414         }
415
416         cmdr = cmd->opcode;
417
418         if (mmc_resp_type(cmd) == MMC_RSP_NONE)
419                 cmdr |= AT91_MCI_RSPTYP_NONE;
420         else {
421                 /* if a response is expected then allow maximum response latancy */
422                 cmdr |= AT91_MCI_MAXLAT;
423                 /* set 136 bit response for R2, 48 bit response otherwise */
424                 if (mmc_resp_type(cmd) == MMC_RSP_R2)
425                         cmdr |= AT91_MCI_RSPTYP_136;
426                 else
427                         cmdr |= AT91_MCI_RSPTYP_48;
428         }
429
430         if (data) {
431                 block_length = data->blksz;
432                 blocks = data->blocks;
433
434                 /* always set data start - also set direction flag for read */
435                 if (data->flags & MMC_DATA_READ)
436                         cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
437                 else if (data->flags & MMC_DATA_WRITE)
438                         cmdr |= AT91_MCI_TRCMD_START;
439
440                 if (data->flags & MMC_DATA_STREAM)
441                         cmdr |= AT91_MCI_TRTYP_STREAM;
442                 if (data->blocks > 1)
443                         cmdr |= AT91_MCI_TRTYP_MULTIPLE;
444         }
445         else {
446                 block_length = 0;
447                 blocks = 0;
448         }
449
450         if (host->flags & FL_SENT_STOP)
451                 cmdr |= AT91_MCI_TRCMD_STOP;
452
453         if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
454                 cmdr |= AT91_MCI_OPDCMD;
455
456         /*
457          * Set the arguments and send the command
458          */
459         pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
460                 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
461
462         if (!data) {
463                 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
464                 at91_mci_write(host, ATMEL_PDC_RPR, 0);
465                 at91_mci_write(host, ATMEL_PDC_RCR, 0);
466                 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
467                 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
468                 at91_mci_write(host, ATMEL_PDC_TPR, 0);
469                 at91_mci_write(host, ATMEL_PDC_TCR, 0);
470                 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
471                 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
472                 ier = AT91_MCI_CMDRDY;
473         } else {
474                 /* zero block length and PDC mode */
475                 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
476                 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
477
478                 /*
479                  * Disable the PDC controller
480                  */
481                 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
482
483                 if (cmdr & AT91_MCI_TRCMD_START) {
484                         data->bytes_xfered = 0;
485                         host->transfer_index = 0;
486                         host->in_use_index = 0;
487                         if (cmdr & AT91_MCI_TRDIR) {
488                                 /*
489                                  * Handle a read
490                                  */
491                                 host->buffer = NULL;
492                                 host->total_length = 0;
493
494                                 at91_mci_pre_dma_read(host);
495                                 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
496                         }
497                         else {
498                                 /*
499                                  * Handle a write
500                                  */
501                                 host->total_length = block_length * blocks;
502                                 host->buffer = dma_alloc_coherent(NULL,
503                                                 host->total_length,
504                                                 &host->physical_address, GFP_KERNEL);
505
506                                 at91_mci_sg_to_dma(host, data);
507
508                                 pr_debug("Transmitting %d bytes\n", host->total_length);
509
510                                 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
511                                 at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
512                                 ier = AT91_MCI_CMDRDY;
513                         }
514                 }
515         }
516
517         /*
518          * Send the command and then enable the PDC - not the other way round as
519          * the data sheet says
520          */
521
522         at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
523         at91_mci_write(host, AT91_MCI_CMDR, cmdr);
524
525         if (cmdr & AT91_MCI_TRCMD_START) {
526                 if (cmdr & AT91_MCI_TRDIR)
527                         at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
528         }
529
530         /* Enable selected interrupts */
531         at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
532 }
533
534 /*
535  * Process the next step in the request
536  */
537 static void at91_mci_process_next(struct at91mci_host *host)
538 {
539         if (!(host->flags & FL_SENT_COMMAND)) {
540                 host->flags |= FL_SENT_COMMAND;
541                 at91_mci_send_command(host, host->request->cmd);
542         }
543         else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
544                 host->flags |= FL_SENT_STOP;
545                 at91_mci_send_command(host, host->request->stop);
546         }
547         else
548                 mmc_request_done(host->mmc, host->request);
549 }
550
551 /*
552  * Handle a command that has been completed
553  */
554 static void at91_mci_completed_command(struct at91mci_host *host)
555 {
556         struct mmc_command *cmd = host->cmd;
557         unsigned int status;
558
559         at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
560
561         cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
562         cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
563         cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
564         cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
565
566         if (host->buffer) {
567                 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
568                 host->buffer = NULL;
569         }
570
571         status = at91_mci_read(host, AT91_MCI_SR);
572
573         pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
574                  status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
575
576         if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
577                         AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
578                         AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
579                 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
580                         cmd->error = 0;
581                 }
582                 else {
583                         if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
584                                 cmd->error = -ETIMEDOUT;
585                         else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
586                                 cmd->error = -EILSEQ;
587                         else
588                                 cmd->error = -EIO;
589
590                         pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
591                                  cmd->error, cmd->opcode, cmd->retries);
592                 }
593         }
594         else
595                 cmd->error = 0;
596
597         at91_mci_process_next(host);
598 }
599
600 /*
601  * Handle an MMC request
602  */
603 static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
604 {
605         struct at91mci_host *host = mmc_priv(mmc);
606         host->request = mrq;
607         host->flags = 0;
608
609         at91_mci_process_next(host);
610 }
611
612 /*
613  * Set the IOS
614  */
615 static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
616 {
617         int clkdiv;
618         struct at91mci_host *host = mmc_priv(mmc);
619         unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
620
621         host->bus_mode = ios->bus_mode;
622
623         if (ios->clock == 0) {
624                 /* Disable the MCI controller */
625                 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
626                 clkdiv = 0;
627         }
628         else {
629                 /* Enable the MCI controller */
630                 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
631
632                 if ((at91_master_clock % (ios->clock * 2)) == 0)
633                         clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
634                 else
635                         clkdiv = (at91_master_clock / ios->clock) / 2;
636
637                 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
638                         at91_master_clock / (2 * (clkdiv + 1)));
639         }
640         if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
641                 pr_debug("MMC: Setting controller bus width to 4\n");
642                 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
643         }
644         else {
645                 pr_debug("MMC: Setting controller bus width to 1\n");
646                 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
647         }
648
649         /* Set the clock divider */
650         at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
651
652         /* maybe switch power to the card */
653         if (host->board->vcc_pin) {
654                 switch (ios->power_mode) {
655                         case MMC_POWER_OFF:
656                                 at91_set_gpio_value(host->board->vcc_pin, 0);
657                                 break;
658                         case MMC_POWER_UP:
659                         case MMC_POWER_ON:
660                                 at91_set_gpio_value(host->board->vcc_pin, 1);
661                                 break;
662                 }
663         }
664 }
665
666 /*
667  * Handle an interrupt
668  */
669 static irqreturn_t at91_mci_irq(int irq, void *devid)
670 {
671         struct at91mci_host *host = devid;
672         int completed = 0;
673         unsigned int int_status, int_mask;
674
675         int_status = at91_mci_read(host, AT91_MCI_SR);
676         int_mask = at91_mci_read(host, AT91_MCI_IMR);
677
678         pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
679                 int_status & int_mask);
680
681         int_status = int_status & int_mask;
682
683         if (int_status & AT91_MCI_ERRORS) {
684                 completed = 1;
685
686                 if (int_status & AT91_MCI_UNRE)
687                         pr_debug("MMC: Underrun error\n");
688                 if (int_status & AT91_MCI_OVRE)
689                         pr_debug("MMC: Overrun error\n");
690                 if (int_status & AT91_MCI_DTOE)
691                         pr_debug("MMC: Data timeout\n");
692                 if (int_status & AT91_MCI_DCRCE)
693                         pr_debug("MMC: CRC error in data\n");
694                 if (int_status & AT91_MCI_RTOE)
695                         pr_debug("MMC: Response timeout\n");
696                 if (int_status & AT91_MCI_RENDE)
697                         pr_debug("MMC: Response end bit error\n");
698                 if (int_status & AT91_MCI_RCRCE)
699                         pr_debug("MMC: Response CRC error\n");
700                 if (int_status & AT91_MCI_RDIRE)
701                         pr_debug("MMC: Response direction error\n");
702                 if (int_status & AT91_MCI_RINDE)
703                         pr_debug("MMC: Response index error\n");
704         } else {
705                 /* Only continue processing if no errors */
706
707                 if (int_status & AT91_MCI_TXBUFE) {
708                         pr_debug("TX buffer empty\n");
709                         at91_mci_handle_transmitted(host);
710                 }
711
712                 if (int_status & AT91_MCI_ENDRX) {
713                         pr_debug("ENDRX\n");
714                         at91_mci_post_dma_read(host);
715                 }
716
717                 if (int_status & AT91_MCI_RXBUFF) {
718                         pr_debug("RX buffer full\n");
719                         at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
720                         at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
721                         completed = 1;
722                 }
723
724                 if (int_status & AT91_MCI_ENDTX)
725                         pr_debug("Transmit has ended\n");
726
727                 if (int_status & AT91_MCI_NOTBUSY) {
728                         pr_debug("Card is ready\n");
729                         completed = 1;
730                 }
731
732                 if (int_status & AT91_MCI_DTIP)
733                         pr_debug("Data transfer in progress\n");
734
735                 if (int_status & AT91_MCI_BLKE) {
736                         pr_debug("Block transfer has ended\n");
737                         completed = 1;
738                 }
739
740                 if (int_status & AT91_MCI_TXRDY)
741                         pr_debug("Ready to transmit\n");
742
743                 if (int_status & AT91_MCI_RXRDY)
744                         pr_debug("Ready to receive\n");
745
746                 if (int_status & AT91_MCI_CMDRDY) {
747                         pr_debug("Command ready\n");
748                         completed = at91_mci_handle_cmdrdy(host);
749                 }
750         }
751
752         if (completed) {
753                 pr_debug("Completed command\n");
754                 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
755                 at91_mci_completed_command(host);
756         } else
757                 at91_mci_write(host, AT91_MCI_IDR, int_status);
758
759         return IRQ_HANDLED;
760 }
761
762 static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
763 {
764         struct at91mci_host *host = _host;
765         int present = !at91_get_gpio_value(irq);
766
767         /*
768          * we expect this irq on both insert and remove,
769          * and use a short delay to debounce.
770          */
771         if (present != host->present) {
772                 host->present = present;
773                 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
774                         present ? "insert" : "remove");
775                 if (!present) {
776                         pr_debug("****** Resetting SD-card bus width ******\n");
777                         at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
778                 }
779                 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
780         }
781         return IRQ_HANDLED;
782 }
783
784 static int at91_mci_get_ro(struct mmc_host *mmc)
785 {
786         int read_only = 0;
787         struct at91mci_host *host = mmc_priv(mmc);
788
789         if (host->board->wp_pin) {
790                 read_only = at91_get_gpio_value(host->board->wp_pin);
791                 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
792                                 (read_only ? "read-only" : "read-write") );
793         }
794         else {
795                 printk(KERN_WARNING "%s: host does not support reading read-only "
796                                 "switch.  Assuming write-enable.\n", mmc_hostname(mmc));
797         }
798         return read_only;
799 }
800
801 static const struct mmc_host_ops at91_mci_ops = {
802         .request        = at91_mci_request,
803         .set_ios        = at91_mci_set_ios,
804         .get_ro         = at91_mci_get_ro,
805 };
806
807 /*
808  * Probe for the device
809  */
810 static int __init at91_mci_probe(struct platform_device *pdev)
811 {
812         struct mmc_host *mmc;
813         struct at91mci_host *host;
814         struct resource *res;
815         int ret;
816
817         pr_debug("Probe MCI devices\n");
818
819         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
820         if (!res)
821                 return -ENXIO;
822
823         if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
824                 return -EBUSY;
825
826         mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
827         if (!mmc) {
828                 pr_debug("Failed to allocate mmc host\n");
829                 release_mem_region(res->start, res->end - res->start + 1);
830                 return -ENOMEM;
831         }
832
833         mmc->ops = &at91_mci_ops;
834         mmc->f_min = 375000;
835         mmc->f_max = 25000000;
836         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
837
838         mmc->max_blk_size = 4095;
839         mmc->max_blk_count = mmc->max_req_size;
840
841         host = mmc_priv(mmc);
842         host->mmc = mmc;
843         host->buffer = NULL;
844         host->bus_mode = 0;
845         host->board = pdev->dev.platform_data;
846         if (host->board->wire4) {
847                 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
848                         mmc->caps |= MMC_CAP_4_BIT_DATA;
849                 else
850                         printk("AT91 MMC: 4 wire bus mode not supported"
851                                 " - using 1 wire\n");
852         }
853
854         /*
855          * Get Clock
856          */
857         host->mci_clk = clk_get(&pdev->dev, "mci_clk");
858         if (IS_ERR(host->mci_clk)) {
859                 printk(KERN_ERR "AT91 MMC: no clock defined.\n");
860                 mmc_free_host(mmc);
861                 release_mem_region(res->start, res->end - res->start + 1);
862                 return -ENODEV;
863         }
864
865         /*
866          * Map I/O region
867          */
868         host->baseaddr = ioremap(res->start, res->end - res->start + 1);
869         if (!host->baseaddr) {
870                 clk_put(host->mci_clk);
871                 mmc_free_host(mmc);
872                 release_mem_region(res->start, res->end - res->start + 1);
873                 return -ENOMEM;
874         }
875
876         /*
877          * Reset hardware
878          */
879         clk_enable(host->mci_clk);              /* Enable the peripheral clock */
880         at91_mci_disable(host);
881         at91_mci_enable(host);
882
883         /*
884          * Allocate the MCI interrupt
885          */
886         host->irq = platform_get_irq(pdev, 0);
887         ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED, DRIVER_NAME, host);
888         if (ret) {
889                 printk(KERN_ERR "AT91 MMC: Failed to request MCI interrupt\n");
890                 clk_disable(host->mci_clk);
891                 clk_put(host->mci_clk);
892                 mmc_free_host(mmc);
893                 iounmap(host->baseaddr);
894                 release_mem_region(res->start, res->end - res->start + 1);
895                 return ret;
896         }
897
898         platform_set_drvdata(pdev, mmc);
899
900         /*
901          * Add host to MMC layer
902          */
903         if (host->board->det_pin) {
904                 host->present = !at91_get_gpio_value(host->board->det_pin);
905                 device_init_wakeup(&pdev->dev, 1);
906         }
907         else
908                 host->present = -1;
909
910         mmc_add_host(mmc);
911
912         /*
913          * monitor card insertion/removal if we can
914          */
915         if (host->board->det_pin) {
916                 ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
917                                 0, DRIVER_NAME, host);
918                 if (ret)
919                         printk(KERN_ERR "AT91 MMC: Couldn't allocate MMC detect irq\n");
920         }
921
922         pr_debug("Added MCI driver\n");
923
924         return 0;
925 }
926
927 /*
928  * Remove a device
929  */
930 static int __exit at91_mci_remove(struct platform_device *pdev)
931 {
932         struct mmc_host *mmc = platform_get_drvdata(pdev);
933         struct at91mci_host *host;
934         struct resource *res;
935
936         if (!mmc)
937                 return -1;
938
939         host = mmc_priv(mmc);
940
941         if (host->board->det_pin) {
942                 device_init_wakeup(&pdev->dev, 0);
943                 free_irq(host->board->det_pin, host);
944                 cancel_delayed_work(&host->mmc->detect);
945         }
946
947         at91_mci_disable(host);
948         mmc_remove_host(mmc);
949         free_irq(host->irq, host);
950
951         clk_disable(host->mci_clk);                     /* Disable the peripheral clock */
952         clk_put(host->mci_clk);
953
954         iounmap(host->baseaddr);
955         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
956         release_mem_region(res->start, res->end - res->start + 1);
957
958         mmc_free_host(mmc);
959         platform_set_drvdata(pdev, NULL);
960         pr_debug("MCI Removed\n");
961
962         return 0;
963 }
964
965 #ifdef CONFIG_PM
966 static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
967 {
968         struct mmc_host *mmc = platform_get_drvdata(pdev);
969         struct at91mci_host *host = mmc_priv(mmc);
970         int ret = 0;
971
972         if (host->board->det_pin && device_may_wakeup(&pdev->dev))
973                 enable_irq_wake(host->board->det_pin);
974
975         if (mmc)
976                 ret = mmc_suspend_host(mmc, state);
977
978         return ret;
979 }
980
981 static int at91_mci_resume(struct platform_device *pdev)
982 {
983         struct mmc_host *mmc = platform_get_drvdata(pdev);
984         struct at91mci_host *host = mmc_priv(mmc);
985         int ret = 0;
986
987         if (host->board->det_pin && device_may_wakeup(&pdev->dev))
988                 disable_irq_wake(host->board->det_pin);
989
990         if (mmc)
991                 ret = mmc_resume_host(mmc);
992
993         return ret;
994 }
995 #else
996 #define at91_mci_suspend        NULL
997 #define at91_mci_resume         NULL
998 #endif
999
1000 static struct platform_driver at91_mci_driver = {
1001         .remove         = __exit_p(at91_mci_remove),
1002         .suspend        = at91_mci_suspend,
1003         .resume         = at91_mci_resume,
1004         .driver         = {
1005                 .name   = DRIVER_NAME,
1006                 .owner  = THIS_MODULE,
1007         },
1008 };
1009
1010 static int __init at91_mci_init(void)
1011 {
1012         return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
1013 }
1014
1015 static void __exit at91_mci_exit(void)
1016 {
1017         platform_driver_unregister(&at91_mci_driver);
1018 }
1019
1020 module_init(at91_mci_init);
1021 module_exit(at91_mci_exit);
1022
1023 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1024 MODULE_AUTHOR("Nick Randell");
1025 MODULE_LICENSE("GPL");