2 * Atmel MultiMedia Card Interface driver
4 * Copyright (C) 2004-2008 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/blkdev.h>
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/scatterlist.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/stat.h>
27 #include <linux/types.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/sdio.h>
32 #include <mach/atmel-mci.h>
33 #include <linux/atmel-mci.h>
34 #include <linux/atmel_pdc.h>
37 #include <asm/unaligned.h>
40 #include <mach/board.h>
42 #include "atmel-mci-regs.h"
44 #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
45 #define ATMCI_DMA_THRESHOLD 16
54 enum atmel_mci_state {
58 STATE_WAITING_NOTBUSY,
73 struct atmel_mci_caps {
81 bool has_bad_data_ordering;
82 bool need_reset_after_xfer;
83 bool need_blksz_mul_4;
84 bool need_notbusy_for_read_ops;
87 struct atmel_mci_dma {
88 struct dma_chan *chan;
89 struct dma_async_tx_descriptor *data_desc;
93 * struct atmel_mci - MMC controller state shared between all slots
94 * @lock: Spinlock protecting the queue and associated data.
95 * @regs: Pointer to MMIO registers.
96 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
97 * @pio_offset: Offset into the current scatterlist entry.
98 * @buffer: Buffer used if we don't have the r/w proof capability. We
99 * don't have the time to switch pdc buffers so we have to use only
100 * one buffer for the full transaction.
101 * @buf_size: size of the buffer.
102 * @phys_buf_addr: buffer address needed for pdc.
103 * @cur_slot: The slot which is currently using the controller.
104 * @mrq: The request currently being processed on @cur_slot,
105 * or NULL if the controller is idle.
106 * @cmd: The command currently being sent to the card, or NULL.
107 * @data: The data currently being transferred, or NULL if no data
108 * transfer is in progress.
109 * @data_size: just data->blocks * data->blksz.
110 * @dma: DMA client state.
111 * @data_chan: DMA channel being used for the current data transfer.
112 * @cmd_status: Snapshot of SR taken upon completion of the current
113 * command. Only valid when EVENT_CMD_COMPLETE is pending.
114 * @data_status: Snapshot of SR taken upon completion of the current
115 * data transfer. Only valid when EVENT_DATA_COMPLETE or
116 * EVENT_DATA_ERROR is pending.
117 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
119 * @tasklet: Tasklet running the request state machine.
120 * @pending_events: Bitmask of events flagged by the interrupt handler
121 * to be processed by the tasklet.
122 * @completed_events: Bitmask of events which the state machine has
124 * @state: Tasklet state.
125 * @queue: List of slots waiting for access to the controller.
126 * @need_clock_update: Update the clock rate before the next request.
127 * @need_reset: Reset controller before next request.
128 * @timer: Timer to balance the data timeout error flag which cannot rise.
129 * @mode_reg: Value of the MR register.
130 * @cfg_reg: Value of the CFG register.
131 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
132 * rate and timeout calculations.
133 * @mapbase: Physical address of the MMIO registers.
134 * @mck: The peripheral bus clock hooked up to the MMC controller.
135 * @pdev: Platform device associated with the MMC controller.
136 * @slot: Slots sharing this MMC controller.
137 * @caps: MCI capabilities depending on MCI version.
138 * @prepare_data: function to setup MCI before data transfer which
139 * depends on MCI capabilities.
140 * @submit_data: function to start data transfer which depends on MCI
142 * @stop_transfer: function to stop data transfer which depends on MCI
148 * @lock is a softirq-safe spinlock protecting @queue as well as
149 * @cur_slot, @mrq and @state. These must always be updated
150 * at the same time while holding @lock.
152 * @lock also protects mode_reg and need_clock_update since these are
153 * used to synchronize mode register updates with the queue
156 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
157 * and must always be written at the same time as the slot is added to
160 * @pending_events and @completed_events are accessed using atomic bit
161 * operations, so they don't need any locking.
163 * None of the fields touched by the interrupt handler need any
164 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
165 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
166 * interrupts must be disabled and @data_status updated with a
167 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
168 * CMDRDY interrupt must be disabled and @cmd_status updated with a
169 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
170 * bytes_xfered field of @data must be written. This is ensured by
177 struct scatterlist *sg;
178 unsigned int pio_offset;
179 unsigned int *buffer;
180 unsigned int buf_size;
181 dma_addr_t buf_phys_addr;
183 struct atmel_mci_slot *cur_slot;
184 struct mmc_request *mrq;
185 struct mmc_command *cmd;
186 struct mmc_data *data;
187 unsigned int data_size;
189 struct atmel_mci_dma dma;
190 struct dma_chan *data_chan;
191 struct dma_slave_config dma_conf;
197 struct tasklet_struct tasklet;
198 unsigned long pending_events;
199 unsigned long completed_events;
200 enum atmel_mci_state state;
201 struct list_head queue;
203 bool need_clock_update;
205 struct timer_list timer;
208 unsigned long bus_hz;
209 unsigned long mapbase;
211 struct platform_device *pdev;
213 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
215 struct atmel_mci_caps caps;
217 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
218 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
219 void (*stop_transfer)(struct atmel_mci *host);
223 * struct atmel_mci_slot - MMC slot state
224 * @mmc: The mmc_host representing this slot.
225 * @host: The MMC controller this slot is using.
226 * @sdc_reg: Value of SDCR to be written before using this slot.
227 * @sdio_irq: SDIO irq mask for this slot.
228 * @mrq: mmc_request currently being processed or waiting to be
229 * processed, or NULL when the slot is idle.
230 * @queue_node: List node for placing this node in the @queue list of
232 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
233 * @flags: Random state bits associated with the slot.
234 * @detect_pin: GPIO pin used for card detection, or negative if not
236 * @wp_pin: GPIO pin used for card write protect sending, or negative
238 * @detect_is_active_high: The state of the detect pin when it is active.
239 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
241 struct atmel_mci_slot {
242 struct mmc_host *mmc;
243 struct atmel_mci *host;
248 struct mmc_request *mrq;
249 struct list_head queue_node;
253 #define ATMCI_CARD_PRESENT 0
254 #define ATMCI_CARD_NEED_INIT 1
255 #define ATMCI_SHUTDOWN 2
256 #define ATMCI_SUSPENDED 3
260 bool detect_is_active_high;
262 struct timer_list detect_timer;
265 #define atmci_test_and_clear_pending(host, event) \
266 test_and_clear_bit(event, &host->pending_events)
267 #define atmci_set_completed(host, event) \
268 set_bit(event, &host->completed_events)
269 #define atmci_set_pending(host, event) \
270 set_bit(event, &host->pending_events)
273 * The debugfs stuff below is mostly optimized away when
274 * CONFIG_DEBUG_FS is not set.
276 static int atmci_req_show(struct seq_file *s, void *v)
278 struct atmel_mci_slot *slot = s->private;
279 struct mmc_request *mrq;
280 struct mmc_command *cmd;
281 struct mmc_command *stop;
282 struct mmc_data *data;
284 /* Make sure we get a consistent snapshot */
285 spin_lock_bh(&slot->host->lock);
295 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
296 cmd->opcode, cmd->arg, cmd->flags,
297 cmd->resp[0], cmd->resp[1], cmd->resp[2],
298 cmd->resp[3], cmd->error);
300 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
301 data->bytes_xfered, data->blocks,
302 data->blksz, data->flags, data->error);
305 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
306 stop->opcode, stop->arg, stop->flags,
307 stop->resp[0], stop->resp[1], stop->resp[2],
308 stop->resp[3], stop->error);
311 spin_unlock_bh(&slot->host->lock);
316 static int atmci_req_open(struct inode *inode, struct file *file)
318 return single_open(file, atmci_req_show, inode->i_private);
321 static const struct file_operations atmci_req_fops = {
322 .owner = THIS_MODULE,
323 .open = atmci_req_open,
326 .release = single_release,
329 static void atmci_show_status_reg(struct seq_file *s,
330 const char *regname, u32 value)
332 static const char *sr_bit[] = {
363 seq_printf(s, "%s:\t0x%08x", regname, value);
364 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
365 if (value & (1 << i)) {
367 seq_printf(s, " %s", sr_bit[i]);
369 seq_puts(s, " UNKNOWN");
375 static int atmci_regs_show(struct seq_file *s, void *v)
377 struct atmel_mci *host = s->private;
380 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
385 * Grab a more or less consistent snapshot. Note that we're
386 * not disabling interrupts, so IMR and SR may not be
389 spin_lock_bh(&host->lock);
390 clk_enable(host->mck);
391 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
392 clk_disable(host->mck);
393 spin_unlock_bh(&host->lock);
395 seq_printf(s, "MR:\t0x%08x%s%s ",
397 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
398 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
399 if (host->caps.has_odd_clk_div)
400 seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
401 ((buf[ATMCI_MR / 4] & 0xff) << 1)
402 | ((buf[ATMCI_MR / 4] >> 16) & 1));
404 seq_printf(s, "CLKDIV=%u\n",
405 (buf[ATMCI_MR / 4] & 0xff));
406 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
407 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
408 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
409 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
411 buf[ATMCI_BLKR / 4] & 0xffff,
412 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
413 if (host->caps.has_cstor_reg)
414 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
416 /* Don't read RSPR and RDR; it will consume the data there */
418 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
419 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
421 if (host->caps.has_dma) {
424 val = buf[ATMCI_DMA / 4];
425 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
428 1 << (((val >> 4) & 3) + 1) : 1,
429 val & ATMCI_DMAEN ? " DMAEN" : "");
431 if (host->caps.has_cfg_reg) {
434 val = buf[ATMCI_CFG / 4];
435 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
437 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
438 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
439 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
440 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
448 static int atmci_regs_open(struct inode *inode, struct file *file)
450 return single_open(file, atmci_regs_show, inode->i_private);
453 static const struct file_operations atmci_regs_fops = {
454 .owner = THIS_MODULE,
455 .open = atmci_regs_open,
458 .release = single_release,
461 static void atmci_init_debugfs(struct atmel_mci_slot *slot)
463 struct mmc_host *mmc = slot->mmc;
464 struct atmel_mci *host = slot->host;
468 root = mmc->debugfs_root;
472 node = debugfs_create_file("regs", S_IRUSR, root, host,
479 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
483 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
487 node = debugfs_create_x32("pending_events", S_IRUSR, root,
488 (u32 *)&host->pending_events);
492 node = debugfs_create_x32("completed_events", S_IRUSR, root,
493 (u32 *)&host->completed_events);
500 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
503 static inline unsigned int atmci_get_version(struct atmel_mci *host)
505 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
508 static void atmci_timeout_timer(unsigned long data)
510 struct atmel_mci *host;
512 host = (struct atmel_mci *)data;
514 dev_dbg(&host->pdev->dev, "software timeout\n");
516 if (host->mrq->cmd->data) {
517 host->mrq->cmd->data->error = -ETIMEDOUT;
520 host->mrq->cmd->error = -ETIMEDOUT;
523 host->need_reset = 1;
524 host->state = STATE_END_REQUEST;
526 tasklet_schedule(&host->tasklet);
529 static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
533 * It is easier here to use us instead of ns for the timeout,
534 * it prevents from overflows during calculation.
536 unsigned int us = DIV_ROUND_UP(ns, 1000);
538 /* Maximum clock frequency is host->bus_hz/2 */
539 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
542 static void atmci_set_timeout(struct atmel_mci *host,
543 struct atmel_mci_slot *slot, struct mmc_data *data)
545 static unsigned dtomul_to_shift[] = {
546 0, 4, 7, 8, 10, 12, 16, 20
552 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
553 + data->timeout_clks;
555 for (dtomul = 0; dtomul < 8; dtomul++) {
556 unsigned shift = dtomul_to_shift[dtomul];
557 dtocyc = (timeout + (1 << shift) - 1) >> shift;
567 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
568 dtocyc << dtomul_to_shift[dtomul]);
569 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
573 * Return mask with command flags to be enabled for this command.
575 static u32 atmci_prepare_command(struct mmc_host *mmc,
576 struct mmc_command *cmd)
578 struct mmc_data *data;
581 cmd->error = -EINPROGRESS;
583 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
585 if (cmd->flags & MMC_RSP_PRESENT) {
586 if (cmd->flags & MMC_RSP_136)
587 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
589 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
593 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
594 * it's too difficult to determine whether this is an ACMD or
595 * not. Better make it 64.
597 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
599 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
600 cmdr |= ATMCI_CMDR_OPDCMD;
604 cmdr |= ATMCI_CMDR_START_XFER;
606 if (cmd->opcode == SD_IO_RW_EXTENDED) {
607 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
609 if (data->flags & MMC_DATA_STREAM)
610 cmdr |= ATMCI_CMDR_STREAM;
611 else if (data->blocks > 1)
612 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
614 cmdr |= ATMCI_CMDR_BLOCK;
617 if (data->flags & MMC_DATA_READ)
618 cmdr |= ATMCI_CMDR_TRDIR_READ;
624 static void atmci_send_command(struct atmel_mci *host,
625 struct mmc_command *cmd, u32 cmd_flags)
630 dev_vdbg(&host->pdev->dev,
631 "start command: ARGR=0x%08x CMDR=0x%08x\n",
632 cmd->arg, cmd_flags);
634 atmci_writel(host, ATMCI_ARGR, cmd->arg);
635 atmci_writel(host, ATMCI_CMDR, cmd_flags);
638 static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
640 dev_dbg(&host->pdev->dev, "send stop command\n");
641 atmci_send_command(host, data->stop, host->stop_cmdr);
642 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
646 * Configure given PDC buffer taking care of alignement issues.
647 * Update host->data_size and host->sg.
649 static void atmci_pdc_set_single_buf(struct atmel_mci *host,
650 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
652 u32 pointer_reg, counter_reg;
653 unsigned int buf_size;
655 if (dir == XFER_RECEIVE) {
656 pointer_reg = ATMEL_PDC_RPR;
657 counter_reg = ATMEL_PDC_RCR;
659 pointer_reg = ATMEL_PDC_TPR;
660 counter_reg = ATMEL_PDC_TCR;
663 if (buf_nb == PDC_SECOND_BUF) {
664 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
665 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
668 if (!host->caps.has_rwproof) {
669 buf_size = host->buf_size;
670 atmci_writel(host, pointer_reg, host->buf_phys_addr);
672 buf_size = sg_dma_len(host->sg);
673 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
676 if (host->data_size <= buf_size) {
677 if (host->data_size & 0x3) {
678 /* If size is different from modulo 4, transfer bytes */
679 atmci_writel(host, counter_reg, host->data_size);
680 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
682 /* Else transfer 32-bits words */
683 atmci_writel(host, counter_reg, host->data_size / 4);
687 /* We assume the size of a page is 32-bits aligned */
688 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
689 host->data_size -= sg_dma_len(host->sg);
691 host->sg = sg_next(host->sg);
696 * Configure PDC buffer according to the data size ie configuring one or two
697 * buffers. Don't use this function if you want to configure only the second
698 * buffer. In this case, use atmci_pdc_set_single_buf.
700 static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
702 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
704 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
708 * Unmap sg lists, called when transfer is finished.
710 static void atmci_pdc_cleanup(struct atmel_mci *host)
712 struct mmc_data *data = host->data;
715 dma_unmap_sg(&host->pdev->dev,
716 data->sg, data->sg_len,
717 ((data->flags & MMC_DATA_WRITE)
718 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
722 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
723 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
724 * interrupt needed for both transfer directions.
726 static void atmci_pdc_complete(struct atmel_mci *host)
728 int transfer_size = host->data->blocks * host->data->blksz;
731 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
733 if ((!host->caps.has_rwproof)
734 && (host->data->flags & MMC_DATA_READ)) {
735 if (host->caps.has_bad_data_ordering)
736 for (i = 0; i < transfer_size; i++)
737 host->buffer[i] = swab32(host->buffer[i]);
738 sg_copy_from_buffer(host->data->sg, host->data->sg_len,
739 host->buffer, transfer_size);
742 atmci_pdc_cleanup(host);
745 * If the card was removed, data will be NULL. No point trying
746 * to send the stop command or waiting for NBUSY in this case.
749 dev_dbg(&host->pdev->dev,
750 "(%s) set pending xfer complete\n", __func__);
751 atmci_set_pending(host, EVENT_XFER_COMPLETE);
752 tasklet_schedule(&host->tasklet);
756 static void atmci_dma_cleanup(struct atmel_mci *host)
758 struct mmc_data *data = host->data;
761 dma_unmap_sg(host->dma.chan->device->dev,
762 data->sg, data->sg_len,
763 ((data->flags & MMC_DATA_WRITE)
764 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
768 * This function is called by the DMA driver from tasklet context.
770 static void atmci_dma_complete(void *arg)
772 struct atmel_mci *host = arg;
773 struct mmc_data *data = host->data;
775 dev_vdbg(&host->pdev->dev, "DMA complete\n");
777 if (host->caps.has_dma)
778 /* Disable DMA hardware handshaking on MCI */
779 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
781 atmci_dma_cleanup(host);
784 * If the card was removed, data will be NULL. No point trying
785 * to send the stop command or waiting for NBUSY in this case.
788 dev_dbg(&host->pdev->dev,
789 "(%s) set pending xfer complete\n", __func__);
790 atmci_set_pending(host, EVENT_XFER_COMPLETE);
791 tasklet_schedule(&host->tasklet);
794 * Regardless of what the documentation says, we have
795 * to wait for NOTBUSY even after block read
798 * When the DMA transfer is complete, the controller
799 * may still be reading the CRC from the card, i.e.
800 * the data transfer is still in progress and we
801 * haven't seen all the potential error bits yet.
803 * The interrupt handler will schedule a different
804 * tasklet to finish things up when the data transfer
805 * is completely done.
807 * We may not complete the mmc request here anyway
808 * because the mmc layer may call back and cause us to
809 * violate the "don't submit new operations from the
810 * completion callback" rule of the dma engine
813 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
818 * Returns a mask of interrupt flags to be enabled after the whole
819 * request has been prepared.
821 static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
825 data->error = -EINPROGRESS;
829 host->data_chan = NULL;
831 iflags = ATMCI_DATA_ERROR_FLAGS;
834 * Errata: MMC data write operation with less than 12
835 * bytes is impossible.
837 * Errata: MCI Transmit Data Register (TDR) FIFO
838 * corruption when length is not multiple of 4.
840 if (data->blocks * data->blksz < 12
841 || (data->blocks * data->blksz) & 3)
842 host->need_reset = true;
844 host->pio_offset = 0;
845 if (data->flags & MMC_DATA_READ)
846 iflags |= ATMCI_RXRDY;
848 iflags |= ATMCI_TXRDY;
854 * Set interrupt flags and set block length into the MCI mode register even
855 * if this value is also accessible in the MCI block register. It seems to be
856 * necessary before the High Speed MCI version. It also map sg and configure
860 atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
864 enum dma_data_direction dir;
867 data->error = -EINPROGRESS;
871 iflags = ATMCI_DATA_ERROR_FLAGS;
873 /* Enable pdc mode */
874 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
876 if (data->flags & MMC_DATA_READ) {
877 dir = DMA_FROM_DEVICE;
878 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
881 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
885 tmp = atmci_readl(host, ATMCI_MR);
887 tmp |= ATMCI_BLKLEN(data->blksz);
888 atmci_writel(host, ATMCI_MR, tmp);
891 host->data_size = data->blocks * data->blksz;
892 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
894 if ((!host->caps.has_rwproof)
895 && (host->data->flags & MMC_DATA_WRITE)) {
896 sg_copy_to_buffer(host->data->sg, host->data->sg_len,
897 host->buffer, host->data_size);
898 if (host->caps.has_bad_data_ordering)
899 for (i = 0; i < host->data_size; i++)
900 host->buffer[i] = swab32(host->buffer[i]);
904 atmci_pdc_set_both_buf(host,
905 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
911 atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
913 struct dma_chan *chan;
914 struct dma_async_tx_descriptor *desc;
915 struct scatterlist *sg;
917 enum dma_data_direction direction;
918 enum dma_transfer_direction slave_dirn;
923 data->error = -EINPROGRESS;
929 iflags = ATMCI_DATA_ERROR_FLAGS;
932 * We don't do DMA on "complex" transfers, i.e. with
933 * non-word-aligned buffers or lengths. Also, we don't bother
934 * with all the DMA setup overhead for short transfers.
936 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
937 return atmci_prepare_data(host, data);
939 return atmci_prepare_data(host, data);
941 for_each_sg(data->sg, sg, data->sg_len, i) {
942 if (sg->offset & 3 || sg->length & 3)
943 return atmci_prepare_data(host, data);
946 /* If we don't have a channel, we can't do DMA */
947 chan = host->dma.chan;
949 host->data_chan = chan;
954 if (data->flags & MMC_DATA_READ) {
955 direction = DMA_FROM_DEVICE;
956 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
957 maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
959 direction = DMA_TO_DEVICE;
960 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
961 maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
964 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) | ATMCI_DMAEN);
966 sglen = dma_map_sg(chan->device->dev, data->sg,
967 data->sg_len, direction);
969 dmaengine_slave_config(chan, &host->dma_conf);
970 desc = dmaengine_prep_slave_sg(chan,
971 data->sg, sglen, slave_dirn,
972 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
976 host->dma.data_desc = desc;
977 desc->callback = atmci_dma_complete;
978 desc->callback_param = host;
982 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
987 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
993 * Start PDC according to transfer direction.
996 atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
998 if (data->flags & MMC_DATA_READ)
999 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
1001 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1005 atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
1007 struct dma_chan *chan = host->data_chan;
1008 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
1011 dmaengine_submit(desc);
1012 dma_async_issue_pending(chan);
1016 static void atmci_stop_transfer(struct atmel_mci *host)
1018 dev_dbg(&host->pdev->dev,
1019 "(%s) set pending xfer complete\n", __func__);
1020 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1021 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1025 * Stop data transfer because error(s) occurred.
1027 static void atmci_stop_transfer_pdc(struct atmel_mci *host)
1029 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
1032 static void atmci_stop_transfer_dma(struct atmel_mci *host)
1034 struct dma_chan *chan = host->data_chan;
1037 dmaengine_terminate_all(chan);
1038 atmci_dma_cleanup(host);
1040 /* Data transfer was stopped by the interrupt handler */
1041 dev_dbg(&host->pdev->dev,
1042 "(%s) set pending xfer complete\n", __func__);
1043 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1044 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1049 * Start a request: prepare data if needed, prepare the command and activate
1052 static void atmci_start_request(struct atmel_mci *host,
1053 struct atmel_mci_slot *slot)
1055 struct mmc_request *mrq;
1056 struct mmc_command *cmd;
1057 struct mmc_data *data;
1062 host->cur_slot = slot;
1065 host->pending_events = 0;
1066 host->completed_events = 0;
1067 host->cmd_status = 0;
1068 host->data_status = 0;
1070 dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
1072 if (host->need_reset || host->caps.need_reset_after_xfer) {
1073 iflags = atmci_readl(host, ATMCI_IMR);
1074 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
1075 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1076 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1077 atmci_writel(host, ATMCI_MR, host->mode_reg);
1078 if (host->caps.has_cfg_reg)
1079 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1080 atmci_writel(host, ATMCI_IER, iflags);
1081 host->need_reset = false;
1083 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
1085 iflags = atmci_readl(host, ATMCI_IMR);
1086 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
1087 dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
1090 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1091 /* Send init sequence (74 clock cycles) */
1092 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1093 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
1099 atmci_set_timeout(host, slot, data);
1101 /* Must set block count/size before sending command */
1102 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
1103 | ATMCI_BLKLEN(data->blksz));
1104 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
1105 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
1107 iflags |= host->prepare_data(host, data);
1110 iflags |= ATMCI_CMDRDY;
1112 cmdflags = atmci_prepare_command(slot->mmc, cmd);
1113 atmci_send_command(host, cmd, cmdflags);
1116 host->submit_data(host, data);
1119 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
1120 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
1121 if (!(data->flags & MMC_DATA_WRITE))
1122 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
1123 if (data->flags & MMC_DATA_STREAM)
1124 host->stop_cmdr |= ATMCI_CMDR_STREAM;
1126 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
1130 * We could have enabled interrupts earlier, but I suspect
1131 * that would open up a nice can of interesting race
1132 * conditions (e.g. command and data complete, but stop not
1135 atmci_writel(host, ATMCI_IER, iflags);
1137 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
1140 static void atmci_queue_request(struct atmel_mci *host,
1141 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1143 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1146 spin_lock_bh(&host->lock);
1148 if (host->state == STATE_IDLE) {
1149 host->state = STATE_SENDING_CMD;
1150 atmci_start_request(host, slot);
1152 dev_dbg(&host->pdev->dev, "queue request\n");
1153 list_add_tail(&slot->queue_node, &host->queue);
1155 spin_unlock_bh(&host->lock);
1158 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1160 struct atmel_mci_slot *slot = mmc_priv(mmc);
1161 struct atmel_mci *host = slot->host;
1162 struct mmc_data *data;
1165 dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
1168 * We may "know" the card is gone even though there's still an
1169 * electrical connection. If so, we really need to communicate
1170 * this to the MMC core since there won't be any more
1171 * interrupts as the card is completely removed. Otherwise,
1172 * the MMC core might believe the card is still there even
1173 * though the card was just removed very slowly.
1175 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1176 mrq->cmd->error = -ENOMEDIUM;
1177 mmc_request_done(mmc, mrq);
1181 /* We don't support multiple blocks of weird lengths. */
1183 if (data && data->blocks > 1 && data->blksz & 3) {
1184 mrq->cmd->error = -EINVAL;
1185 mmc_request_done(mmc, mrq);
1188 atmci_queue_request(host, slot, mrq);
1191 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1193 struct atmel_mci_slot *slot = mmc_priv(mmc);
1194 struct atmel_mci *host = slot->host;
1197 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
1198 switch (ios->bus_width) {
1199 case MMC_BUS_WIDTH_1:
1200 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
1202 case MMC_BUS_WIDTH_4:
1203 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
1208 unsigned int clock_min = ~0U;
1211 spin_lock_bh(&host->lock);
1212 if (!host->mode_reg) {
1213 clk_enable(host->mck);
1214 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1215 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1216 if (host->caps.has_cfg_reg)
1217 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1221 * Use mirror of ios->clock to prevent race with mmc
1222 * core ios update when finding the minimum.
1224 slot->clock = ios->clock;
1225 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1226 if (host->slot[i] && host->slot[i]->clock
1227 && host->slot[i]->clock < clock_min)
1228 clock_min = host->slot[i]->clock;
1231 /* Calculate clock divider */
1232 if (host->caps.has_odd_clk_div) {
1233 clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
1235 dev_warn(&mmc->class_dev,
1236 "clock %u too slow; using %lu\n",
1237 clock_min, host->bus_hz / (511 + 2));
1240 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
1241 | ATMCI_MR_CLKODD(clkdiv & 1);
1243 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1245 dev_warn(&mmc->class_dev,
1246 "clock %u too slow; using %lu\n",
1247 clock_min, host->bus_hz / (2 * 256));
1250 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
1254 * WRPROOF and RDPROOF prevent overruns/underruns by
1255 * stopping the clock when the FIFO is full/empty.
1256 * This state is not expected to last for long.
1258 if (host->caps.has_rwproof)
1259 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
1261 if (host->caps.has_cfg_reg) {
1262 /* setup High Speed mode in relation with card capacity */
1263 if (ios->timing == MMC_TIMING_SD_HS)
1264 host->cfg_reg |= ATMCI_CFG_HSMODE;
1266 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
1269 if (list_empty(&host->queue)) {
1270 atmci_writel(host, ATMCI_MR, host->mode_reg);
1271 if (host->caps.has_cfg_reg)
1272 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1274 host->need_clock_update = true;
1277 spin_unlock_bh(&host->lock);
1279 bool any_slot_active = false;
1281 spin_lock_bh(&host->lock);
1283 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1284 if (host->slot[i] && host->slot[i]->clock) {
1285 any_slot_active = true;
1289 if (!any_slot_active) {
1290 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
1291 if (host->mode_reg) {
1292 atmci_readl(host, ATMCI_MR);
1293 clk_disable(host->mck);
1297 spin_unlock_bh(&host->lock);
1300 switch (ios->power_mode) {
1302 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1306 * TODO: None of the currently available AVR32-based
1307 * boards allow MMC power to be turned off. Implement
1308 * power control when this can be tested properly.
1310 * We also need to hook this into the clock management
1311 * somehow so that newly inserted cards aren't
1312 * subjected to a fast clock before we have a chance
1313 * to figure out what the maximum rate is. Currently,
1314 * there's no way to avoid this, and there never will
1315 * be for boards that don't support power control.
1321 static int atmci_get_ro(struct mmc_host *mmc)
1323 int read_only = -ENOSYS;
1324 struct atmel_mci_slot *slot = mmc_priv(mmc);
1326 if (gpio_is_valid(slot->wp_pin)) {
1327 read_only = gpio_get_value(slot->wp_pin);
1328 dev_dbg(&mmc->class_dev, "card is %s\n",
1329 read_only ? "read-only" : "read-write");
1335 static int atmci_get_cd(struct mmc_host *mmc)
1337 int present = -ENOSYS;
1338 struct atmel_mci_slot *slot = mmc_priv(mmc);
1340 if (gpio_is_valid(slot->detect_pin)) {
1341 present = !(gpio_get_value(slot->detect_pin) ^
1342 slot->detect_is_active_high);
1343 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1344 present ? "" : "not ");
1350 static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1352 struct atmel_mci_slot *slot = mmc_priv(mmc);
1353 struct atmel_mci *host = slot->host;
1356 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
1358 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
1361 static const struct mmc_host_ops atmci_ops = {
1362 .request = atmci_request,
1363 .set_ios = atmci_set_ios,
1364 .get_ro = atmci_get_ro,
1365 .get_cd = atmci_get_cd,
1366 .enable_sdio_irq = atmci_enable_sdio_irq,
1369 /* Called with host->lock held */
1370 static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1371 __releases(&host->lock)
1372 __acquires(&host->lock)
1374 struct atmel_mci_slot *slot = NULL;
1375 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1377 WARN_ON(host->cmd || host->data);
1380 * Update the MMC clock rate if necessary. This may be
1381 * necessary if set_ios() is called when a different slot is
1382 * busy transferring data.
1384 if (host->need_clock_update) {
1385 atmci_writel(host, ATMCI_MR, host->mode_reg);
1386 if (host->caps.has_cfg_reg)
1387 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1390 host->cur_slot->mrq = NULL;
1392 if (!list_empty(&host->queue)) {
1393 slot = list_entry(host->queue.next,
1394 struct atmel_mci_slot, queue_node);
1395 list_del(&slot->queue_node);
1396 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1397 mmc_hostname(slot->mmc));
1398 host->state = STATE_SENDING_CMD;
1399 atmci_start_request(host, slot);
1401 dev_vdbg(&host->pdev->dev, "list empty\n");
1402 host->state = STATE_IDLE;
1405 del_timer(&host->timer);
1407 spin_unlock(&host->lock);
1408 mmc_request_done(prev_mmc, mrq);
1409 spin_lock(&host->lock);
1412 static void atmci_command_complete(struct atmel_mci *host,
1413 struct mmc_command *cmd)
1415 u32 status = host->cmd_status;
1417 /* Read the response from the card (up to 16 bytes) */
1418 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1419 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1420 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1421 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
1423 if (status & ATMCI_RTOE)
1424 cmd->error = -ETIMEDOUT;
1425 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
1426 cmd->error = -EILSEQ;
1427 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
1429 else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
1430 if (host->caps.need_blksz_mul_4) {
1431 cmd->error = -EINVAL;
1432 host->need_reset = 1;
1438 static void atmci_detect_change(unsigned long data)
1440 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1445 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1446 * freeing the interrupt. We must not re-enable the interrupt
1447 * if it has been freed, and if we're shutting down, it
1448 * doesn't really matter whether the card is present or not.
1451 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
1454 enable_irq(gpio_to_irq(slot->detect_pin));
1455 present = !(gpio_get_value(slot->detect_pin) ^
1456 slot->detect_is_active_high);
1457 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
1459 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1460 present, present_old);
1462 if (present != present_old) {
1463 struct atmel_mci *host = slot->host;
1464 struct mmc_request *mrq;
1466 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1467 present ? "inserted" : "removed");
1469 spin_lock(&host->lock);
1472 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1474 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1476 /* Clean up queue if present */
1479 if (mrq == host->mrq) {
1481 * Reset controller to terminate any ongoing
1482 * commands or data transfers.
1484 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1485 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1486 atmci_writel(host, ATMCI_MR, host->mode_reg);
1487 if (host->caps.has_cfg_reg)
1488 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1493 switch (host->state) {
1496 case STATE_SENDING_CMD:
1497 mrq->cmd->error = -ENOMEDIUM;
1499 host->stop_transfer(host);
1501 case STATE_DATA_XFER:
1502 mrq->data->error = -ENOMEDIUM;
1503 host->stop_transfer(host);
1505 case STATE_WAITING_NOTBUSY:
1506 mrq->data->error = -ENOMEDIUM;
1508 case STATE_SENDING_STOP:
1509 mrq->stop->error = -ENOMEDIUM;
1511 case STATE_END_REQUEST:
1515 atmci_request_end(host, mrq);
1517 list_del(&slot->queue_node);
1518 mrq->cmd->error = -ENOMEDIUM;
1520 mrq->data->error = -ENOMEDIUM;
1522 mrq->stop->error = -ENOMEDIUM;
1524 spin_unlock(&host->lock);
1525 mmc_request_done(slot->mmc, mrq);
1526 spin_lock(&host->lock);
1529 spin_unlock(&host->lock);
1531 mmc_detect_change(slot->mmc, 0);
1535 static void atmci_tasklet_func(unsigned long priv)
1537 struct atmel_mci *host = (struct atmel_mci *)priv;
1538 struct mmc_request *mrq = host->mrq;
1539 struct mmc_data *data = host->data;
1540 enum atmel_mci_state state = host->state;
1541 enum atmel_mci_state prev_state;
1544 spin_lock(&host->lock);
1546 state = host->state;
1548 dev_vdbg(&host->pdev->dev,
1549 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1550 state, host->pending_events, host->completed_events,
1551 atmci_readl(host, ATMCI_IMR));
1555 dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
1561 case STATE_SENDING_CMD:
1563 * Command has been sent, we are waiting for command
1564 * ready. Then we have three next states possible:
1565 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1566 * command needing it or DATA_XFER if there is data.
1568 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1569 if (!atmci_test_and_clear_pending(host,
1573 dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
1575 atmci_set_completed(host, EVENT_CMD_RDY);
1576 atmci_command_complete(host, mrq->cmd);
1578 dev_dbg(&host->pdev->dev,
1579 "command with data transfer");
1581 * If there is a command error don't start
1584 if (mrq->cmd->error) {
1585 host->stop_transfer(host);
1587 atmci_writel(host, ATMCI_IDR,
1588 ATMCI_TXRDY | ATMCI_RXRDY
1589 | ATMCI_DATA_ERROR_FLAGS);
1590 state = STATE_END_REQUEST;
1592 state = STATE_DATA_XFER;
1593 } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
1594 dev_dbg(&host->pdev->dev,
1595 "command response need waiting notbusy");
1596 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1597 state = STATE_WAITING_NOTBUSY;
1599 state = STATE_END_REQUEST;
1603 case STATE_DATA_XFER:
1604 if (atmci_test_and_clear_pending(host,
1605 EVENT_DATA_ERROR)) {
1606 dev_dbg(&host->pdev->dev, "set completed data error\n");
1607 atmci_set_completed(host, EVENT_DATA_ERROR);
1608 state = STATE_END_REQUEST;
1613 * A data transfer is in progress. The event expected
1614 * to move to the next state depends of data transfer
1615 * type (PDC or DMA). Once transfer done we can move
1616 * to the next step which is WAITING_NOTBUSY in write
1617 * case and directly SENDING_STOP in read case.
1619 dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
1620 if (!atmci_test_and_clear_pending(host,
1621 EVENT_XFER_COMPLETE))
1624 dev_dbg(&host->pdev->dev,
1625 "(%s) set completed xfer complete\n",
1627 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1629 if (host->caps.need_notbusy_for_read_ops ||
1630 (host->data->flags & MMC_DATA_WRITE)) {
1631 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1632 state = STATE_WAITING_NOTBUSY;
1633 } else if (host->mrq->stop) {
1634 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
1635 atmci_send_stop_cmd(host, data);
1636 state = STATE_SENDING_STOP;
1639 data->bytes_xfered = data->blocks * data->blksz;
1641 state = STATE_END_REQUEST;
1645 case STATE_WAITING_NOTBUSY:
1647 * We can be in the state for two reasons: a command
1648 * requiring waiting not busy signal (stop command
1649 * included) or a write operation. In the latest case,
1650 * we need to send a stop command.
1652 dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
1653 if (!atmci_test_and_clear_pending(host,
1657 dev_dbg(&host->pdev->dev, "set completed not busy\n");
1658 atmci_set_completed(host, EVENT_NOTBUSY);
1662 * For some commands such as CMD53, even if
1663 * there is data transfer, there is no stop
1666 if (host->mrq->stop) {
1667 atmci_writel(host, ATMCI_IER,
1669 atmci_send_stop_cmd(host, data);
1670 state = STATE_SENDING_STOP;
1673 data->bytes_xfered = data->blocks
1676 state = STATE_END_REQUEST;
1679 state = STATE_END_REQUEST;
1682 case STATE_SENDING_STOP:
1684 * In this state, it is important to set host->data to
1685 * NULL (which is tested in the waiting notbusy state)
1686 * in order to go to the end request state instead of
1687 * sending stop again.
1689 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1690 if (!atmci_test_and_clear_pending(host,
1694 dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
1696 data->bytes_xfered = data->blocks * data->blksz;
1698 atmci_command_complete(host, mrq->stop);
1699 if (mrq->stop->error) {
1700 host->stop_transfer(host);
1701 atmci_writel(host, ATMCI_IDR,
1702 ATMCI_TXRDY | ATMCI_RXRDY
1703 | ATMCI_DATA_ERROR_FLAGS);
1704 state = STATE_END_REQUEST;
1706 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1707 state = STATE_WAITING_NOTBUSY;
1712 case STATE_END_REQUEST:
1713 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
1714 | ATMCI_DATA_ERROR_FLAGS);
1715 status = host->data_status;
1716 if (unlikely(status)) {
1717 host->stop_transfer(host);
1719 if (status & ATMCI_DTOE) {
1720 data->error = -ETIMEDOUT;
1721 } else if (status & ATMCI_DCRCE) {
1722 data->error = -EILSEQ;
1728 atmci_request_end(host, host->mrq);
1732 } while (state != prev_state);
1734 host->state = state;
1736 spin_unlock(&host->lock);
1739 static void atmci_read_data_pio(struct atmel_mci *host)
1741 struct scatterlist *sg = host->sg;
1742 void *buf = sg_virt(sg);
1743 unsigned int offset = host->pio_offset;
1744 struct mmc_data *data = host->data;
1747 unsigned int nbytes = 0;
1750 value = atmci_readl(host, ATMCI_RDR);
1751 if (likely(offset + 4 <= sg->length)) {
1752 put_unaligned(value, (u32 *)(buf + offset));
1757 if (offset == sg->length) {
1758 flush_dcache_page(sg_page(sg));
1759 host->sg = sg = sg_next(sg);
1767 unsigned int remaining = sg->length - offset;
1768 memcpy(buf + offset, &value, remaining);
1769 nbytes += remaining;
1771 flush_dcache_page(sg_page(sg));
1772 host->sg = sg = sg_next(sg);
1776 offset = 4 - remaining;
1778 memcpy(buf, (u8 *)&value + remaining, offset);
1782 status = atmci_readl(host, ATMCI_SR);
1783 if (status & ATMCI_DATA_ERROR_FLAGS) {
1784 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
1785 | ATMCI_DATA_ERROR_FLAGS));
1786 host->data_status = status;
1787 data->bytes_xfered += nbytes;
1790 } while (status & ATMCI_RXRDY);
1792 host->pio_offset = offset;
1793 data->bytes_xfered += nbytes;
1798 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1799 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1800 data->bytes_xfered += nbytes;
1802 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1805 static void atmci_write_data_pio(struct atmel_mci *host)
1807 struct scatterlist *sg = host->sg;
1808 void *buf = sg_virt(sg);
1809 unsigned int offset = host->pio_offset;
1810 struct mmc_data *data = host->data;
1813 unsigned int nbytes = 0;
1816 if (likely(offset + 4 <= sg->length)) {
1817 value = get_unaligned((u32 *)(buf + offset));
1818 atmci_writel(host, ATMCI_TDR, value);
1822 if (offset == sg->length) {
1823 host->sg = sg = sg_next(sg);
1831 unsigned int remaining = sg->length - offset;
1834 memcpy(&value, buf + offset, remaining);
1835 nbytes += remaining;
1837 host->sg = sg = sg_next(sg);
1839 atmci_writel(host, ATMCI_TDR, value);
1843 offset = 4 - remaining;
1845 memcpy((u8 *)&value + remaining, buf, offset);
1846 atmci_writel(host, ATMCI_TDR, value);
1850 status = atmci_readl(host, ATMCI_SR);
1851 if (status & ATMCI_DATA_ERROR_FLAGS) {
1852 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
1853 | ATMCI_DATA_ERROR_FLAGS));
1854 host->data_status = status;
1855 data->bytes_xfered += nbytes;
1858 } while (status & ATMCI_TXRDY);
1860 host->pio_offset = offset;
1861 data->bytes_xfered += nbytes;
1866 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1867 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1868 data->bytes_xfered += nbytes;
1870 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1873 static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1877 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1878 struct atmel_mci_slot *slot = host->slot[i];
1879 if (slot && (status & slot->sdio_irq)) {
1880 mmc_signal_sdio_irq(slot->mmc);
1886 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1888 struct atmel_mci *host = dev_id;
1889 u32 status, mask, pending;
1890 unsigned int pass_count = 0;
1893 status = atmci_readl(host, ATMCI_SR);
1894 mask = atmci_readl(host, ATMCI_IMR);
1895 pending = status & mask;
1899 if (pending & ATMCI_DATA_ERROR_FLAGS) {
1900 dev_dbg(&host->pdev->dev, "IRQ: data error\n");
1901 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
1902 | ATMCI_RXRDY | ATMCI_TXRDY
1903 | ATMCI_ENDRX | ATMCI_ENDTX
1904 | ATMCI_RXBUFF | ATMCI_TXBUFE);
1906 host->data_status = status;
1907 dev_dbg(&host->pdev->dev, "set pending data error\n");
1909 atmci_set_pending(host, EVENT_DATA_ERROR);
1910 tasklet_schedule(&host->tasklet);
1913 if (pending & ATMCI_TXBUFE) {
1914 dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
1915 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
1916 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1918 * We can receive this interruption before having configured
1919 * the second pdc buffer, so we need to reconfigure first and
1920 * second buffers again
1922 if (host->data_size) {
1923 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
1924 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1925 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1927 atmci_pdc_complete(host);
1929 } else if (pending & ATMCI_ENDTX) {
1930 dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
1931 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1933 if (host->data_size) {
1934 atmci_pdc_set_single_buf(host,
1935 XFER_TRANSMIT, PDC_SECOND_BUF);
1936 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1940 if (pending & ATMCI_RXBUFF) {
1941 dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
1942 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
1943 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1945 * We can receive this interruption before having configured
1946 * the second pdc buffer, so we need to reconfigure first and
1947 * second buffers again
1949 if (host->data_size) {
1950 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
1951 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1952 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
1954 atmci_pdc_complete(host);
1956 } else if (pending & ATMCI_ENDRX) {
1957 dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
1958 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1960 if (host->data_size) {
1961 atmci_pdc_set_single_buf(host,
1962 XFER_RECEIVE, PDC_SECOND_BUF);
1963 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1968 * First mci IPs, so mainly the ones having pdc, have some
1969 * issues with the notbusy signal. You can't get it after
1970 * data transmission if you have not sent a stop command.
1971 * The appropriate workaround is to use the BLKE signal.
1973 if (pending & ATMCI_BLKE) {
1974 dev_dbg(&host->pdev->dev, "IRQ: blke\n");
1975 atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
1977 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
1978 atmci_set_pending(host, EVENT_NOTBUSY);
1979 tasklet_schedule(&host->tasklet);
1982 if (pending & ATMCI_NOTBUSY) {
1983 dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
1984 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
1986 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
1987 atmci_set_pending(host, EVENT_NOTBUSY);
1988 tasklet_schedule(&host->tasklet);
1991 if (pending & ATMCI_RXRDY)
1992 atmci_read_data_pio(host);
1993 if (pending & ATMCI_TXRDY)
1994 atmci_write_data_pio(host);
1996 if (pending & ATMCI_CMDRDY) {
1997 dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
1998 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
1999 host->cmd_status = status;
2001 dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
2002 atmci_set_pending(host, EVENT_CMD_RDY);
2003 tasklet_schedule(&host->tasklet);
2006 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
2007 atmci_sdio_interrupt(host, status);
2009 } while (pass_count++ < 5);
2011 return pass_count ? IRQ_HANDLED : IRQ_NONE;
2014 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
2016 struct atmel_mci_slot *slot = dev_id;
2019 * Disable interrupts until the pin has stabilized and check
2020 * the state then. Use mod_timer() since we may be in the
2021 * middle of the timer routine when this interrupt triggers.
2023 disable_irq_nosync(irq);
2024 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
2029 static int __init atmci_init_slot(struct atmel_mci *host,
2030 struct mci_slot_pdata *slot_data, unsigned int id,
2031 u32 sdc_reg, u32 sdio_irq)
2033 struct mmc_host *mmc;
2034 struct atmel_mci_slot *slot;
2036 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
2040 slot = mmc_priv(mmc);
2043 slot->detect_pin = slot_data->detect_pin;
2044 slot->wp_pin = slot_data->wp_pin;
2045 slot->detect_is_active_high = slot_data->detect_is_active_high;
2046 slot->sdc_reg = sdc_reg;
2047 slot->sdio_irq = sdio_irq;
2049 mmc->ops = &atmci_ops;
2050 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
2051 mmc->f_max = host->bus_hz / 2;
2052 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2054 mmc->caps |= MMC_CAP_SDIO_IRQ;
2055 if (host->caps.has_highspeed)
2056 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2058 * Without the read/write proof capability, it is strongly suggested to
2059 * use only one bit for data to prevent fifo underruns and overruns
2060 * which will corrupt data.
2062 if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
2063 mmc->caps |= MMC_CAP_4_BIT_DATA;
2065 if (atmci_get_version(host) < 0x200) {
2066 mmc->max_segs = 256;
2067 mmc->max_blk_size = 4095;
2068 mmc->max_blk_count = 256;
2069 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2070 mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
2073 mmc->max_req_size = 32768 * 512;
2074 mmc->max_blk_size = 32768;
2075 mmc->max_blk_count = 512;
2078 /* Assume card is present initially */
2079 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
2080 if (gpio_is_valid(slot->detect_pin)) {
2081 if (gpio_request(slot->detect_pin, "mmc_detect")) {
2082 dev_dbg(&mmc->class_dev, "no detect pin available\n");
2083 slot->detect_pin = -EBUSY;
2084 } else if (gpio_get_value(slot->detect_pin) ^
2085 slot->detect_is_active_high) {
2086 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
2090 if (!gpio_is_valid(slot->detect_pin))
2091 mmc->caps |= MMC_CAP_NEEDS_POLL;
2093 if (gpio_is_valid(slot->wp_pin)) {
2094 if (gpio_request(slot->wp_pin, "mmc_wp")) {
2095 dev_dbg(&mmc->class_dev, "no WP pin available\n");
2096 slot->wp_pin = -EBUSY;
2100 host->slot[id] = slot;
2103 if (gpio_is_valid(slot->detect_pin)) {
2106 setup_timer(&slot->detect_timer, atmci_detect_change,
2107 (unsigned long)slot);
2109 ret = request_irq(gpio_to_irq(slot->detect_pin),
2110 atmci_detect_interrupt,
2111 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2112 "mmc-detect", slot);
2114 dev_dbg(&mmc->class_dev,
2115 "could not request IRQ %d for detect pin\n",
2116 gpio_to_irq(slot->detect_pin));
2117 gpio_free(slot->detect_pin);
2118 slot->detect_pin = -EBUSY;
2122 atmci_init_debugfs(slot);
2127 static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
2130 /* Debugfs stuff is cleaned up by mmc core */
2132 set_bit(ATMCI_SHUTDOWN, &slot->flags);
2135 mmc_remove_host(slot->mmc);
2137 if (gpio_is_valid(slot->detect_pin)) {
2138 int pin = slot->detect_pin;
2140 free_irq(gpio_to_irq(pin), slot);
2141 del_timer_sync(&slot->detect_timer);
2144 if (gpio_is_valid(slot->wp_pin))
2145 gpio_free(slot->wp_pin);
2147 slot->host->slot[id] = NULL;
2148 mmc_free_host(slot->mmc);
2151 static bool atmci_filter(struct dma_chan *chan, void *slave)
2153 struct mci_dma_data *sl = slave;
2155 if (sl && find_slave_dev(sl) == chan->device->dev) {
2156 chan->private = slave_data_ptr(sl);
2163 static bool atmci_configure_dma(struct atmel_mci *host)
2165 struct mci_platform_data *pdata;
2170 pdata = host->pdev->dev.platform_data;
2172 if (pdata && find_slave_dev(pdata->dma_slave)) {
2173 dma_cap_mask_t mask;
2175 /* Try to grab a DMA channel */
2177 dma_cap_set(DMA_SLAVE, mask);
2179 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
2181 if (!host->dma.chan) {
2182 dev_warn(&host->pdev->dev, "no DMA channel available\n");
2185 dev_info(&host->pdev->dev,
2186 "using %s for DMA transfers\n",
2187 dma_chan_name(host->dma.chan));
2189 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
2190 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2191 host->dma_conf.src_maxburst = 1;
2192 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
2193 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2194 host->dma_conf.dst_maxburst = 1;
2195 host->dma_conf.device_fc = false;
2201 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2202 * HSMCI provides DMA support and a new config register but no more supports
2205 static void __init atmci_get_cap(struct atmel_mci *host)
2207 unsigned int version;
2209 version = atmci_get_version(host);
2210 dev_info(&host->pdev->dev,
2211 "version: 0x%x\n", version);
2213 host->caps.has_dma = 0;
2214 host->caps.has_pdc = 1;
2215 host->caps.has_cfg_reg = 0;
2216 host->caps.has_cstor_reg = 0;
2217 host->caps.has_highspeed = 0;
2218 host->caps.has_rwproof = 0;
2219 host->caps.has_odd_clk_div = 0;
2220 host->caps.has_bad_data_ordering = 1;
2221 host->caps.need_reset_after_xfer = 1;
2222 host->caps.need_blksz_mul_4 = 1;
2223 host->caps.need_notbusy_for_read_ops = 0;
2225 /* keep only major version number */
2226 switch (version & 0xf00) {
2228 host->caps.has_odd_clk_div = 1;
2231 #ifdef CONFIG_AT_HDMAC
2232 host->caps.has_dma = 1;
2234 dev_info(&host->pdev->dev,
2235 "has dma capability but dma engine is not selected, then use pio\n");
2237 host->caps.has_pdc = 0;
2238 host->caps.has_cfg_reg = 1;
2239 host->caps.has_cstor_reg = 1;
2240 host->caps.has_highspeed = 1;
2242 host->caps.has_rwproof = 1;
2243 host->caps.need_blksz_mul_4 = 0;
2244 host->caps.need_notbusy_for_read_ops = 1;
2246 host->caps.has_bad_data_ordering = 0;
2247 host->caps.need_reset_after_xfer = 0;
2251 host->caps.has_pdc = 0;
2252 dev_warn(&host->pdev->dev,
2253 "Unmanaged mci version, set minimum capabilities\n");
2258 static int __init atmci_probe(struct platform_device *pdev)
2260 struct mci_platform_data *pdata;
2261 struct atmel_mci *host;
2262 struct resource *regs;
2263 unsigned int nr_slots;
2267 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2270 pdata = pdev->dev.platform_data;
2273 irq = platform_get_irq(pdev, 0);
2277 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2282 spin_lock_init(&host->lock);
2283 INIT_LIST_HEAD(&host->queue);
2285 host->mck = clk_get(&pdev->dev, "mci_clk");
2286 if (IS_ERR(host->mck)) {
2287 ret = PTR_ERR(host->mck);
2292 host->regs = ioremap(regs->start, resource_size(regs));
2296 clk_enable(host->mck);
2297 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
2298 host->bus_hz = clk_get_rate(host->mck);
2299 clk_disable(host->mck);
2301 host->mapbase = regs->start;
2303 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
2305 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
2307 goto err_request_irq;
2309 /* Get MCI capabilities and set operations according to it */
2310 atmci_get_cap(host);
2311 if (host->caps.has_dma && atmci_configure_dma(host)) {
2312 host->prepare_data = &atmci_prepare_data_dma;
2313 host->submit_data = &atmci_submit_data_dma;
2314 host->stop_transfer = &atmci_stop_transfer_dma;
2315 } else if (host->caps.has_pdc) {
2316 dev_info(&pdev->dev, "using PDC\n");
2317 host->prepare_data = &atmci_prepare_data_pdc;
2318 host->submit_data = &atmci_submit_data_pdc;
2319 host->stop_transfer = &atmci_stop_transfer_pdc;
2321 dev_info(&pdev->dev, "using PIO\n");
2322 host->prepare_data = &atmci_prepare_data;
2323 host->submit_data = &atmci_submit_data;
2324 host->stop_transfer = &atmci_stop_transfer;
2327 platform_set_drvdata(pdev, host);
2329 setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
2331 /* We need at least one slot to succeed */
2334 if (pdata->slot[0].bus_width) {
2335 ret = atmci_init_slot(host, &pdata->slot[0],
2336 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
2339 host->buf_size = host->slot[0]->mmc->max_req_size;
2342 if (pdata->slot[1].bus_width) {
2343 ret = atmci_init_slot(host, &pdata->slot[1],
2344 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
2347 if (host->slot[1]->mmc->max_req_size > host->buf_size)
2349 host->slot[1]->mmc->max_req_size;
2354 dev_err(&pdev->dev, "init failed: no slot defined\n");
2358 if (!host->caps.has_rwproof) {
2359 host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
2360 &host->buf_phys_addr,
2362 if (!host->buffer) {
2364 dev_err(&pdev->dev, "buffer allocation failed\n");
2369 dev_info(&pdev->dev,
2370 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2371 host->mapbase, irq, nr_slots);
2377 dma_release_channel(host->dma.chan);
2378 free_irq(irq, host);
2380 iounmap(host->regs);
2388 static int __exit atmci_remove(struct platform_device *pdev)
2390 struct atmel_mci *host = platform_get_drvdata(pdev);
2393 platform_set_drvdata(pdev, NULL);
2396 dma_free_coherent(&pdev->dev, host->buf_size,
2397 host->buffer, host->buf_phys_addr);
2399 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2401 atmci_cleanup_slot(host->slot[i], i);
2404 clk_enable(host->mck);
2405 atmci_writel(host, ATMCI_IDR, ~0UL);
2406 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2407 atmci_readl(host, ATMCI_SR);
2408 clk_disable(host->mck);
2410 #ifdef CONFIG_MMC_ATMELMCI_DMA
2412 dma_release_channel(host->dma.chan);
2415 free_irq(platform_get_irq(pdev, 0), host);
2416 iounmap(host->regs);
2425 static int atmci_suspend(struct device *dev)
2427 struct atmel_mci *host = dev_get_drvdata(dev);
2430 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2431 struct atmel_mci_slot *slot = host->slot[i];
2436 ret = mmc_suspend_host(slot->mmc);
2439 slot = host->slot[i];
2441 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2442 mmc_resume_host(host->slot[i]->mmc);
2443 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2448 set_bit(ATMCI_SUSPENDED, &slot->flags);
2455 static int atmci_resume(struct device *dev)
2457 struct atmel_mci *host = dev_get_drvdata(dev);
2461 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2462 struct atmel_mci_slot *slot = host->slot[i];
2465 slot = host->slot[i];
2468 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2470 err = mmc_resume_host(slot->mmc);
2474 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2479 static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2480 #define ATMCI_PM_OPS (&atmci_pm)
2482 #define ATMCI_PM_OPS NULL
2485 static struct platform_driver atmci_driver = {
2486 .remove = __exit_p(atmci_remove),
2488 .name = "atmel_mci",
2493 static int __init atmci_init(void)
2495 return platform_driver_probe(&atmci_driver, atmci_probe);
2498 static void __exit atmci_exit(void)
2500 platform_driver_unregister(&atmci_driver);
2503 late_initcall(atmci_init); /* try to load after dma driver when built-in */
2504 module_exit(atmci_exit);
2506 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
2507 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2508 MODULE_LICENSE("GPL v2");