2 * linux/drivers/mmc/at91_mci.c - ATMEL AT91 MCI Driver
4 * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
6 * Copyright (C) 2006 Malcolm Noyes
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.
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.
18 The three entry points are at91_mci_request, at91_mci_set_ios
22 This configures the device to put it into the correct mode and clock speed
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.
30 There are three main types of request, commands, reads and writes.
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.
37 Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38 controller to manage the transfers.
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.
44 The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
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.
50 The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
53 Gets the status of the write protect pin, if available.
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>
68 #include <linux/mmc/host.h>
69 #include <linux/mmc/protocol.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 #include <asm/arch/at91_pdc.h>
80 #define DRIVER_NAME "at91_mci"
84 #define FL_SENT_COMMAND (1 << 0)
85 #define FL_SENT_STOP (1 << 1)
87 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
88 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
89 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
91 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
92 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
96 * Low level type for this driver
100 struct mmc_host *mmc;
101 struct mmc_command *cmd;
102 struct mmc_request *request;
104 void __iomem *baseaddr;
107 struct at91_mmc_data *board;
113 * Flag indicating when the command has been sent. This is used to
114 * work out whether or not to send the stop
117 /* flag for current bus settings */
120 /* DMA buffer used for transmitting */
121 unsigned int* buffer;
122 dma_addr_t physical_address;
123 unsigned int total_length;
125 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
128 /* Latest in the scatterlist that has been enabled for transfer */
133 * Copy from sg to a dma block - used for transfers
135 static inline void at91mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
137 unsigned int len, i, size;
138 unsigned *dmabuf = host->buffer;
140 size = host->total_length;
144 * Just loop through all entries. Size might not
145 * be the entire list though so make sure that
146 * we do not transfer too much.
148 for (i = 0; i < len; i++) {
149 struct scatterlist *sg;
151 unsigned int *sgbuffer;
155 sgbuffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
156 amount = min(size, sg->length);
159 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
162 for (index = 0; index < (amount / 4); index++)
163 *dmabuf++ = swab32(sgbuffer[index]);
166 memcpy(dmabuf, sgbuffer, amount);
168 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
175 * Check that we didn't get a request to transfer
176 * more data than can fit into the SG list.
184 static void at91mci_pre_dma_read(struct at91mci_host *host)
187 struct scatterlist *sg;
188 struct mmc_command *cmd;
189 struct mmc_data *data;
191 pr_debug("pre dma read\n");
195 pr_debug("no command\n");
201 pr_debug("no data\n");
205 for (i = 0; i < 2; i++) {
206 /* nothing left to transfer */
207 if (host->transfer_index >= data->sg_len) {
208 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
212 /* Check to see if this needs filling */
214 if (at91_mci_read(host, AT91_PDC_RCR) != 0) {
215 pr_debug("Transfer active in current\n");
220 if (at91_mci_read(host, AT91_PDC_RNCR) != 0) {
221 pr_debug("Transfer active in next\n");
226 /* Setup the next transfer */
227 pr_debug("Using transfer index %d\n", host->transfer_index);
229 sg = &data->sg[host->transfer_index++];
230 pr_debug("sg = %p\n", sg);
232 sg->dma_address = dma_map_page(NULL, sg->page, sg->offset, sg->length, DMA_FROM_DEVICE);
234 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
237 at91_mci_write(host, AT91_PDC_RPR, sg->dma_address);
238 at91_mci_write(host, AT91_PDC_RCR, sg->length / 4);
241 at91_mci_write(host, AT91_PDC_RNPR, sg->dma_address);
242 at91_mci_write(host, AT91_PDC_RNCR, sg->length / 4);
246 pr_debug("pre dma read done\n");
250 * Handle after a dma read
252 static void at91mci_post_dma_read(struct at91mci_host *host)
254 struct mmc_command *cmd;
255 struct mmc_data *data;
257 pr_debug("post dma read\n");
261 pr_debug("no command\n");
267 pr_debug("no data\n");
271 while (host->in_use_index < host->transfer_index) {
272 unsigned int *buffer;
274 struct scatterlist *sg;
276 pr_debug("finishing index %d\n", host->in_use_index);
278 sg = &data->sg[host->in_use_index++];
280 pr_debug("Unmapping page %08X\n", sg->dma_address);
282 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
284 /* Swap the contents of the buffer */
285 buffer = kmap_atomic(sg->page, KM_BIO_SRC_IRQ) + sg->offset;
286 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
288 data->bytes_xfered += sg->length;
290 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
293 for (index = 0; index < (sg->length / 4); index++)
294 buffer[index] = swab32(buffer[index]);
297 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
298 flush_dcache_page(sg->page);
301 /* Is there another transfer to trigger? */
302 if (host->transfer_index < data->sg_len)
303 at91mci_pre_dma_read(host);
305 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
306 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
309 pr_debug("post dma read done\n");
313 * Handle transmitted data
315 static void at91_mci_handle_transmitted(struct at91mci_host *host)
317 struct mmc_command *cmd;
318 struct mmc_data *data;
320 pr_debug("Handling the transmit\n");
322 /* Disable the transfer */
323 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
325 /* Now wait for cmd ready */
326 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
327 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
335 data->bytes_xfered = host->total_length;
339 * Enable the controller
341 static void at91_mci_enable(struct at91mci_host *host)
343 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
344 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
345 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
346 at91_mci_write(host, AT91_MCI_MR, AT91_MCI_PDCMODE | 0x34a);
348 /* use Slot A or B (only one at same time) */
349 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
353 * Disable the controller
355 static void at91_mci_disable(struct at91mci_host *host)
357 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
362 * return the interrupts to enable
364 static unsigned int at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
366 unsigned int cmdr, mr;
367 unsigned int block_length;
368 struct mmc_data *data = cmd->data;
371 unsigned int ier = 0;
375 /* Not sure if this is needed */
377 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
378 pr_debug("Clearing timeout\n");
379 at91_mci_write(host, AT91_MCI_ARGR, 0);
380 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
381 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
383 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
389 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
390 cmdr |= AT91_MCI_RSPTYP_NONE;
392 /* if a response is expected then allow maximum response latancy */
393 cmdr |= AT91_MCI_MAXLAT;
394 /* set 136 bit response for R2, 48 bit response otherwise */
395 if (mmc_resp_type(cmd) == MMC_RSP_R2)
396 cmdr |= AT91_MCI_RSPTYP_136;
398 cmdr |= AT91_MCI_RSPTYP_48;
402 block_length = data->blksz;
403 blocks = data->blocks;
405 /* always set data start - also set direction flag for read */
406 if (data->flags & MMC_DATA_READ)
407 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
408 else if (data->flags & MMC_DATA_WRITE)
409 cmdr |= AT91_MCI_TRCMD_START;
411 if (data->flags & MMC_DATA_STREAM)
412 cmdr |= AT91_MCI_TRTYP_STREAM;
413 if (data->flags & MMC_DATA_MULTI)
414 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
421 if (cmd->opcode == MMC_STOP_TRANSMISSION)
422 cmdr |= AT91_MCI_TRCMD_STOP;
424 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
425 cmdr |= AT91_MCI_OPDCMD;
428 * Set the arguments and send the command
430 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
431 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
434 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTDIS | AT91_PDC_RXTDIS);
435 at91_mci_write(host, AT91_PDC_RPR, 0);
436 at91_mci_write(host, AT91_PDC_RCR, 0);
437 at91_mci_write(host, AT91_PDC_RNPR, 0);
438 at91_mci_write(host, AT91_PDC_RNCR, 0);
439 at91_mci_write(host, AT91_PDC_TPR, 0);
440 at91_mci_write(host, AT91_PDC_TCR, 0);
441 at91_mci_write(host, AT91_PDC_TNPR, 0);
442 at91_mci_write(host, AT91_PDC_TNCR, 0);
444 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
445 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
446 return AT91_MCI_CMDRDY;
449 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; /* zero block length and PDC mode */
450 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
453 * Disable the PDC controller
455 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTDIS | AT91_PDC_TXTDIS);
457 if (cmdr & AT91_MCI_TRCMD_START) {
458 data->bytes_xfered = 0;
459 host->transfer_index = 0;
460 host->in_use_index = 0;
461 if (cmdr & AT91_MCI_TRDIR) {
466 host->total_length = 0;
468 at91mci_pre_dma_read(host);
469 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
475 host->total_length = block_length * blocks;
476 host->buffer = dma_alloc_coherent(NULL,
478 &host->physical_address, GFP_KERNEL);
480 at91mci_sg_to_dma(host, data);
482 pr_debug("Transmitting %d bytes\n", host->total_length);
484 at91_mci_write(host, AT91_PDC_TPR, host->physical_address);
485 at91_mci_write(host, AT91_PDC_TCR, host->total_length / 4);
486 ier = AT91_MCI_TXBUFE;
491 * Send the command and then enable the PDC - not the other way round as
492 * the data sheet says
495 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
496 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
498 if (cmdr & AT91_MCI_TRCMD_START) {
499 if (cmdr & AT91_MCI_TRDIR)
500 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_RXTEN);
502 at91_mci_write(host, AT91_PDC_PTCR, AT91_PDC_TXTEN);
508 * Wait for a command to complete
510 static void at91mci_process_command(struct at91mci_host *host, struct mmc_command *cmd)
514 ier = at91_mci_send_command(host, cmd);
516 pr_debug("setting ier to %08X\n", ier);
518 /* Stop on errors or the required value */
519 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
523 * Process the next step in the request
525 static void at91mci_process_next(struct at91mci_host *host)
527 if (!(host->flags & FL_SENT_COMMAND)) {
528 host->flags |= FL_SENT_COMMAND;
529 at91mci_process_command(host, host->request->cmd);
531 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
532 host->flags |= FL_SENT_STOP;
533 at91mci_process_command(host, host->request->stop);
536 mmc_request_done(host->mmc, host->request);
540 * Handle a command that has been completed
542 static void at91mci_completed_command(struct at91mci_host *host)
544 struct mmc_command *cmd = host->cmd;
547 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
549 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
550 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
551 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
552 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
555 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
559 status = at91_mci_read(host, AT91_MCI_SR);
561 pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
562 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
564 if (status & (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE |
565 AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
566 AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
567 if ((status & AT91_MCI_RCRCE) &&
568 ((cmd->opcode == MMC_SEND_OP_COND) || (cmd->opcode == SD_APP_OP_COND))) {
569 cmd->error = MMC_ERR_NONE;
572 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
573 cmd->error = MMC_ERR_TIMEOUT;
574 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
575 cmd->error = MMC_ERR_BADCRC;
576 else if (status & (AT91_MCI_OVRE | AT91_MCI_UNRE))
577 cmd->error = MMC_ERR_FIFO;
579 cmd->error = MMC_ERR_FAILED;
581 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
582 cmd->error, cmd->opcode, cmd->retries);
586 cmd->error = MMC_ERR_NONE;
588 at91mci_process_next(host);
592 * Handle an MMC request
594 static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
596 struct at91mci_host *host = mmc_priv(mmc);
600 at91mci_process_next(host);
606 static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
609 struct at91mci_host *host = mmc_priv(mmc);
610 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
612 host->bus_mode = ios->bus_mode;
614 if (ios->clock == 0) {
615 /* Disable the MCI controller */
616 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
620 /* Enable the MCI controller */
621 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
623 if ((at91_master_clock % (ios->clock * 2)) == 0)
624 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
626 clkdiv = (at91_master_clock / ios->clock) / 2;
628 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
629 at91_master_clock / (2 * (clkdiv + 1)));
631 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
632 pr_debug("MMC: Setting controller bus width to 4\n");
633 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
636 pr_debug("MMC: Setting controller bus width to 1\n");
637 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
640 /* Set the clock divider */
641 at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
643 /* maybe switch power to the card */
644 if (host->board->vcc_pin) {
645 switch (ios->power_mode) {
647 at91_set_gpio_value(host->board->vcc_pin, 0);
651 at91_set_gpio_value(host->board->vcc_pin, 1);
658 * Handle an interrupt
660 static irqreturn_t at91_mci_irq(int irq, void *devid)
662 struct at91mci_host *host = devid;
664 unsigned int int_status, int_mask;
666 int_status = at91_mci_read(host, AT91_MCI_SR);
667 int_mask = at91_mci_read(host, AT91_MCI_IMR);
669 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
670 int_status & int_mask);
672 int_status = int_status & int_mask;
674 if (int_status & AT91_MCI_ERRORS) {
677 if (int_status & AT91_MCI_UNRE)
678 pr_debug("MMC: Underrun error\n");
679 if (int_status & AT91_MCI_OVRE)
680 pr_debug("MMC: Overrun error\n");
681 if (int_status & AT91_MCI_DTOE)
682 pr_debug("MMC: Data timeout\n");
683 if (int_status & AT91_MCI_DCRCE)
684 pr_debug("MMC: CRC error in data\n");
685 if (int_status & AT91_MCI_RTOE)
686 pr_debug("MMC: Response timeout\n");
687 if (int_status & AT91_MCI_RENDE)
688 pr_debug("MMC: Response end bit error\n");
689 if (int_status & AT91_MCI_RCRCE)
690 pr_debug("MMC: Response CRC error\n");
691 if (int_status & AT91_MCI_RDIRE)
692 pr_debug("MMC: Response direction error\n");
693 if (int_status & AT91_MCI_RINDE)
694 pr_debug("MMC: Response index error\n");
696 /* Only continue processing if no errors */
698 if (int_status & AT91_MCI_TXBUFE) {
699 pr_debug("TX buffer empty\n");
700 at91_mci_handle_transmitted(host);
703 if (int_status & AT91_MCI_RXBUFF) {
704 pr_debug("RX buffer full\n");
705 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
708 if (int_status & AT91_MCI_ENDTX)
709 pr_debug("Transmit has ended\n");
711 if (int_status & AT91_MCI_ENDRX) {
712 pr_debug("Receive has ended\n");
713 at91mci_post_dma_read(host);
716 if (int_status & AT91_MCI_NOTBUSY) {
717 pr_debug("Card is ready\n");
718 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_CMDRDY);
721 if (int_status & AT91_MCI_DTIP)
722 pr_debug("Data transfer in progress\n");
724 if (int_status & AT91_MCI_BLKE)
725 pr_debug("Block transfer has ended\n");
727 if (int_status & AT91_MCI_TXRDY)
728 pr_debug("Ready to transmit\n");
730 if (int_status & AT91_MCI_RXRDY)
731 pr_debug("Ready to receive\n");
733 if (int_status & AT91_MCI_CMDRDY) {
734 pr_debug("Command ready\n");
740 pr_debug("Completed command\n");
741 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
742 at91mci_completed_command(host);
744 at91_mci_write(host, AT91_MCI_IDR, int_status);
749 static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
751 struct at91mci_host *host = _host;
752 int present = !at91_get_gpio_value(irq);
755 * we expect this irq on both insert and remove,
756 * and use a short delay to debounce.
758 if (present != host->present) {
759 host->present = present;
760 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
761 present ? "insert" : "remove");
763 pr_debug("****** Resetting SD-card bus width ******\n");
764 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
766 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
771 int at91_mci_get_ro(struct mmc_host *mmc)
774 struct at91mci_host *host = mmc_priv(mmc);
776 if (host->board->wp_pin) {
777 read_only = at91_get_gpio_value(host->board->wp_pin);
778 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
779 (read_only ? "read-only" : "read-write") );
782 printk(KERN_WARNING "%s: host does not support reading read-only "
783 "switch. Assuming write-enable.\n", mmc_hostname(mmc));
788 static const struct mmc_host_ops at91_mci_ops = {
789 .request = at91_mci_request,
790 .set_ios = at91_mci_set_ios,
791 .get_ro = at91_mci_get_ro,
795 * Probe for the device
797 static int at91_mci_probe(struct platform_device *pdev)
799 struct mmc_host *mmc;
800 struct at91mci_host *host;
801 struct resource *res;
804 pr_debug("Probe MCI devices\n");
806 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
810 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
813 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
815 pr_debug("Failed to allocate mmc host\n");
816 release_mem_region(res->start, res->end - res->start + 1);
820 mmc->ops = &at91_mci_ops;
822 mmc->f_max = 25000000;
823 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
824 mmc->caps = MMC_CAP_BYTEBLOCK;
826 host = mmc_priv(mmc);
830 host->board = pdev->dev.platform_data;
831 if (host->board->wire4) {
833 mmc->caps |= MMC_CAP_4_BIT_DATA;
835 printk("AT91 MMC: 4 wire bus mode not supported by this driver - using 1 wire\n");
842 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
843 if (IS_ERR(host->mci_clk)) {
844 printk(KERN_ERR "AT91 MMC: no clock defined.\n");
846 release_mem_region(res->start, res->end - res->start + 1);
853 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
854 if (!host->baseaddr) {
855 clk_put(host->mci_clk);
857 release_mem_region(res->start, res->end - res->start + 1);
864 clk_enable(host->mci_clk); /* Enable the peripheral clock */
865 at91_mci_disable(host);
866 at91_mci_enable(host);
869 * Allocate the MCI interrupt
871 host->irq = platform_get_irq(pdev, 0);
872 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED, DRIVER_NAME, host);
874 printk(KERN_ERR "AT91 MMC: Failed to request MCI interrupt\n");
875 clk_disable(host->mci_clk);
876 clk_put(host->mci_clk);
878 iounmap(host->baseaddr);
879 release_mem_region(res->start, res->end - res->start + 1);
883 platform_set_drvdata(pdev, mmc);
886 * Add host to MMC layer
888 if (host->board->det_pin)
889 host->present = !at91_get_gpio_value(host->board->det_pin);
896 * monitor card insertion/removal if we can
898 if (host->board->det_pin) {
899 ret = request_irq(host->board->det_pin, at91_mmc_det_irq,
900 0, DRIVER_NAME, host);
902 printk(KERN_ERR "AT91 MMC: Couldn't allocate MMC detect irq\n");
905 pr_debug("Added MCI driver\n");
913 static int at91_mci_remove(struct platform_device *pdev)
915 struct mmc_host *mmc = platform_get_drvdata(pdev);
916 struct at91mci_host *host;
917 struct resource *res;
922 host = mmc_priv(mmc);
924 if (host->present != -1) {
925 free_irq(host->board->det_pin, host);
926 cancel_delayed_work(&host->mmc->detect);
929 at91_mci_disable(host);
930 mmc_remove_host(mmc);
931 free_irq(host->irq, host);
933 clk_disable(host->mci_clk); /* Disable the peripheral clock */
934 clk_put(host->mci_clk);
936 iounmap(host->baseaddr);
937 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
938 release_mem_region(res->start, res->end - res->start + 1);
941 platform_set_drvdata(pdev, NULL);
942 pr_debug("MCI Removed\n");
948 static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
950 struct mmc_host *mmc = platform_get_drvdata(pdev);
954 ret = mmc_suspend_host(mmc, state);
959 static int at91_mci_resume(struct platform_device *pdev)
961 struct mmc_host *mmc = platform_get_drvdata(pdev);
965 ret = mmc_resume_host(mmc);
970 #define at91_mci_suspend NULL
971 #define at91_mci_resume NULL
974 static struct platform_driver at91_mci_driver = {
975 .probe = at91_mci_probe,
976 .remove = at91_mci_remove,
977 .suspend = at91_mci_suspend,
978 .resume = at91_mci_resume,
981 .owner = THIS_MODULE,
985 static int __init at91_mci_init(void)
987 return platform_driver_register(&at91_mci_driver);
990 static void __exit at91_mci_exit(void)
992 platform_driver_unregister(&at91_mci_driver);
995 module_init(at91_mci_init);
996 module_exit(at91_mci_exit);
998 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
999 MODULE_AUTHOR("Nick Randell");
1000 MODULE_LICENSE("GPL");