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
48 EVENT_CMD_COMPLETE = 0,
54 enum atmel_mci_state {
73 struct atmel_mci_caps {
82 struct atmel_mci_dma {
83 struct dma_chan *chan;
84 struct dma_async_tx_descriptor *data_desc;
88 * struct atmel_mci - MMC controller state shared between all slots
89 * @lock: Spinlock protecting the queue and associated data.
90 * @regs: Pointer to MMIO registers.
91 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
92 * @pio_offset: Offset into the current scatterlist entry.
93 * @cur_slot: The slot which is currently using the controller.
94 * @mrq: The request currently being processed on @cur_slot,
95 * or NULL if the controller is idle.
96 * @cmd: The command currently being sent to the card, or NULL.
97 * @data: The data currently being transferred, or NULL if no data
98 * transfer is in progress.
99 * @data_size: just data->blocks * data->blksz.
100 * @dma: DMA client state.
101 * @data_chan: DMA channel being used for the current data transfer.
102 * @cmd_status: Snapshot of SR taken upon completion of the current
103 * command. Only valid when EVENT_CMD_COMPLETE is pending.
104 * @data_status: Snapshot of SR taken upon completion of the current
105 * data transfer. Only valid when EVENT_DATA_COMPLETE or
106 * EVENT_DATA_ERROR is pending.
107 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
109 * @tasklet: Tasklet running the request state machine.
110 * @pending_events: Bitmask of events flagged by the interrupt handler
111 * to be processed by the tasklet.
112 * @completed_events: Bitmask of events which the state machine has
114 * @state: Tasklet state.
115 * @queue: List of slots waiting for access to the controller.
116 * @need_clock_update: Update the clock rate before the next request.
117 * @need_reset: Reset controller before next request.
118 * @mode_reg: Value of the MR register.
119 * @cfg_reg: Value of the CFG register.
120 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
121 * rate and timeout calculations.
122 * @mapbase: Physical address of the MMIO registers.
123 * @mck: The peripheral bus clock hooked up to the MMC controller.
124 * @pdev: Platform device associated with the MMC controller.
125 * @slot: Slots sharing this MMC controller.
126 * @caps: MCI capabilities depending on MCI version.
127 * @prepare_data: function to setup MCI before data transfer which
128 * depends on MCI capabilities.
129 * @submit_data: function to start data transfer which depends on MCI
131 * @stop_transfer: function to stop data transfer which depends on MCI
137 * @lock is a softirq-safe spinlock protecting @queue as well as
138 * @cur_slot, @mrq and @state. These must always be updated
139 * at the same time while holding @lock.
141 * @lock also protects mode_reg and need_clock_update since these are
142 * used to synchronize mode register updates with the queue
145 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
146 * and must always be written at the same time as the slot is added to
149 * @pending_events and @completed_events are accessed using atomic bit
150 * operations, so they don't need any locking.
152 * None of the fields touched by the interrupt handler need any
153 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
154 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
155 * interrupts must be disabled and @data_status updated with a
156 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
157 * CMDRDY interrupt must be disabled and @cmd_status updated with a
158 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
159 * bytes_xfered field of @data must be written. This is ensured by
166 struct scatterlist *sg;
167 unsigned int pio_offset;
169 struct atmel_mci_slot *cur_slot;
170 struct mmc_request *mrq;
171 struct mmc_command *cmd;
172 struct mmc_data *data;
173 unsigned int data_size;
175 struct atmel_mci_dma dma;
176 struct dma_chan *data_chan;
177 struct dma_slave_config dma_conf;
183 struct tasklet_struct tasklet;
184 unsigned long pending_events;
185 unsigned long completed_events;
186 enum atmel_mci_state state;
187 struct list_head queue;
189 bool need_clock_update;
193 unsigned long bus_hz;
194 unsigned long mapbase;
196 struct platform_device *pdev;
198 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
200 struct atmel_mci_caps caps;
202 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
203 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
204 void (*stop_transfer)(struct atmel_mci *host);
208 * struct atmel_mci_slot - MMC slot state
209 * @mmc: The mmc_host representing this slot.
210 * @host: The MMC controller this slot is using.
211 * @sdc_reg: Value of SDCR to be written before using this slot.
212 * @sdio_irq: SDIO irq mask for this slot.
213 * @mrq: mmc_request currently being processed or waiting to be
214 * processed, or NULL when the slot is idle.
215 * @queue_node: List node for placing this node in the @queue list of
217 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
218 * @flags: Random state bits associated with the slot.
219 * @detect_pin: GPIO pin used for card detection, or negative if not
221 * @wp_pin: GPIO pin used for card write protect sending, or negative
223 * @detect_is_active_high: The state of the detect pin when it is active.
224 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
226 struct atmel_mci_slot {
227 struct mmc_host *mmc;
228 struct atmel_mci *host;
233 struct mmc_request *mrq;
234 struct list_head queue_node;
238 #define ATMCI_CARD_PRESENT 0
239 #define ATMCI_CARD_NEED_INIT 1
240 #define ATMCI_SHUTDOWN 2
241 #define ATMCI_SUSPENDED 3
245 bool detect_is_active_high;
247 struct timer_list detect_timer;
250 #define atmci_test_and_clear_pending(host, event) \
251 test_and_clear_bit(event, &host->pending_events)
252 #define atmci_set_completed(host, event) \
253 set_bit(event, &host->completed_events)
254 #define atmci_set_pending(host, event) \
255 set_bit(event, &host->pending_events)
258 * The debugfs stuff below is mostly optimized away when
259 * CONFIG_DEBUG_FS is not set.
261 static int atmci_req_show(struct seq_file *s, void *v)
263 struct atmel_mci_slot *slot = s->private;
264 struct mmc_request *mrq;
265 struct mmc_command *cmd;
266 struct mmc_command *stop;
267 struct mmc_data *data;
269 /* Make sure we get a consistent snapshot */
270 spin_lock_bh(&slot->host->lock);
280 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
281 cmd->opcode, cmd->arg, cmd->flags,
282 cmd->resp[0], cmd->resp[1], cmd->resp[2],
283 cmd->resp[3], cmd->error);
285 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
286 data->bytes_xfered, data->blocks,
287 data->blksz, data->flags, data->error);
290 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
291 stop->opcode, stop->arg, stop->flags,
292 stop->resp[0], stop->resp[1], stop->resp[2],
293 stop->resp[3], stop->error);
296 spin_unlock_bh(&slot->host->lock);
301 static int atmci_req_open(struct inode *inode, struct file *file)
303 return single_open(file, atmci_req_show, inode->i_private);
306 static const struct file_operations atmci_req_fops = {
307 .owner = THIS_MODULE,
308 .open = atmci_req_open,
311 .release = single_release,
314 static void atmci_show_status_reg(struct seq_file *s,
315 const char *regname, u32 value)
317 static const char *sr_bit[] = {
348 seq_printf(s, "%s:\t0x%08x", regname, value);
349 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
350 if (value & (1 << i)) {
352 seq_printf(s, " %s", sr_bit[i]);
354 seq_puts(s, " UNKNOWN");
360 static int atmci_regs_show(struct seq_file *s, void *v)
362 struct atmel_mci *host = s->private;
365 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
370 * Grab a more or less consistent snapshot. Note that we're
371 * not disabling interrupts, so IMR and SR may not be
374 spin_lock_bh(&host->lock);
375 clk_enable(host->mck);
376 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
377 clk_disable(host->mck);
378 spin_unlock_bh(&host->lock);
380 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
382 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
383 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "",
384 buf[ATMCI_MR / 4] & 0xff);
385 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
386 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
387 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
388 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
390 buf[ATMCI_BLKR / 4] & 0xffff,
391 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
392 if (host->caps.has_cstor_reg)
393 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
395 /* Don't read RSPR and RDR; it will consume the data there */
397 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
398 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
400 if (host->caps.has_dma) {
403 val = buf[ATMCI_DMA / 4];
404 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
407 1 << (((val >> 4) & 3) + 1) : 1,
408 val & ATMCI_DMAEN ? " DMAEN" : "");
410 if (host->caps.has_cfg_reg) {
413 val = buf[ATMCI_CFG / 4];
414 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
416 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
417 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
418 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
419 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
427 static int atmci_regs_open(struct inode *inode, struct file *file)
429 return single_open(file, atmci_regs_show, inode->i_private);
432 static const struct file_operations atmci_regs_fops = {
433 .owner = THIS_MODULE,
434 .open = atmci_regs_open,
437 .release = single_release,
440 static void atmci_init_debugfs(struct atmel_mci_slot *slot)
442 struct mmc_host *mmc = slot->mmc;
443 struct atmel_mci *host = slot->host;
447 root = mmc->debugfs_root;
451 node = debugfs_create_file("regs", S_IRUSR, root, host,
458 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
462 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
466 node = debugfs_create_x32("pending_events", S_IRUSR, root,
467 (u32 *)&host->pending_events);
471 node = debugfs_create_x32("completed_events", S_IRUSR, root,
472 (u32 *)&host->completed_events);
479 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
482 static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
485 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
488 static void atmci_set_timeout(struct atmel_mci *host,
489 struct atmel_mci_slot *slot, struct mmc_data *data)
491 static unsigned dtomul_to_shift[] = {
492 0, 4, 7, 8, 10, 12, 16, 20
498 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
499 + data->timeout_clks;
501 for (dtomul = 0; dtomul < 8; dtomul++) {
502 unsigned shift = dtomul_to_shift[dtomul];
503 dtocyc = (timeout + (1 << shift) - 1) >> shift;
513 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
514 dtocyc << dtomul_to_shift[dtomul]);
515 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
519 * Return mask with command flags to be enabled for this command.
521 static u32 atmci_prepare_command(struct mmc_host *mmc,
522 struct mmc_command *cmd)
524 struct mmc_data *data;
527 cmd->error = -EINPROGRESS;
529 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
531 if (cmd->flags & MMC_RSP_PRESENT) {
532 if (cmd->flags & MMC_RSP_136)
533 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
535 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
539 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
540 * it's too difficult to determine whether this is an ACMD or
541 * not. Better make it 64.
543 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
545 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
546 cmdr |= ATMCI_CMDR_OPDCMD;
550 cmdr |= ATMCI_CMDR_START_XFER;
552 if (cmd->opcode == SD_IO_RW_EXTENDED) {
553 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
555 if (data->flags & MMC_DATA_STREAM)
556 cmdr |= ATMCI_CMDR_STREAM;
557 else if (data->blocks > 1)
558 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
560 cmdr |= ATMCI_CMDR_BLOCK;
563 if (data->flags & MMC_DATA_READ)
564 cmdr |= ATMCI_CMDR_TRDIR_READ;
570 static void atmci_send_command(struct atmel_mci *host,
571 struct mmc_command *cmd, u32 cmd_flags)
576 dev_vdbg(&host->pdev->dev,
577 "start command: ARGR=0x%08x CMDR=0x%08x\n",
578 cmd->arg, cmd_flags);
580 atmci_writel(host, ATMCI_ARGR, cmd->arg);
581 atmci_writel(host, ATMCI_CMDR, cmd_flags);
584 static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
586 atmci_send_command(host, data->stop, host->stop_cmdr);
587 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
591 * Configure given PDC buffer taking care of alignement issues.
592 * Update host->data_size and host->sg.
594 static void atmci_pdc_set_single_buf(struct atmel_mci *host,
595 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
597 u32 pointer_reg, counter_reg;
599 if (dir == XFER_RECEIVE) {
600 pointer_reg = ATMEL_PDC_RPR;
601 counter_reg = ATMEL_PDC_RCR;
603 pointer_reg = ATMEL_PDC_TPR;
604 counter_reg = ATMEL_PDC_TCR;
607 if (buf_nb == PDC_SECOND_BUF) {
608 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
609 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
612 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
613 if (host->data_size <= sg_dma_len(host->sg)) {
614 if (host->data_size & 0x3) {
615 /* If size is different from modulo 4, transfer bytes */
616 atmci_writel(host, counter_reg, host->data_size);
617 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
619 /* Else transfer 32-bits words */
620 atmci_writel(host, counter_reg, host->data_size / 4);
624 /* We assume the size of a page is 32-bits aligned */
625 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
626 host->data_size -= sg_dma_len(host->sg);
628 host->sg = sg_next(host->sg);
633 * Configure PDC buffer according to the data size ie configuring one or two
634 * buffers. Don't use this function if you want to configure only the second
635 * buffer. In this case, use atmci_pdc_set_single_buf.
637 static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
639 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
641 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
645 * Unmap sg lists, called when transfer is finished.
647 static void atmci_pdc_cleanup(struct atmel_mci *host)
649 struct mmc_data *data = host->data;
652 dma_unmap_sg(&host->pdev->dev,
653 data->sg, data->sg_len,
654 ((data->flags & MMC_DATA_WRITE)
655 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
659 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
660 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
661 * interrupt needed for both transfer directions.
663 static void atmci_pdc_complete(struct atmel_mci *host)
665 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
666 atmci_pdc_cleanup(host);
669 * If the card was removed, data will be NULL. No point trying
670 * to send the stop command or waiting for NBUSY in this case.
673 atmci_set_pending(host, EVENT_XFER_COMPLETE);
674 tasklet_schedule(&host->tasklet);
675 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
679 static void atmci_dma_cleanup(struct atmel_mci *host)
681 struct mmc_data *data = host->data;
684 dma_unmap_sg(host->dma.chan->device->dev,
685 data->sg, data->sg_len,
686 ((data->flags & MMC_DATA_WRITE)
687 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
691 * This function is called by the DMA driver from tasklet context.
693 static void atmci_dma_complete(void *arg)
695 struct atmel_mci *host = arg;
696 struct mmc_data *data = host->data;
698 dev_vdbg(&host->pdev->dev, "DMA complete\n");
700 if (host->caps.has_dma)
701 /* Disable DMA hardware handshaking on MCI */
702 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
704 atmci_dma_cleanup(host);
707 * If the card was removed, data will be NULL. No point trying
708 * to send the stop command or waiting for NBUSY in this case.
711 atmci_set_pending(host, EVENT_XFER_COMPLETE);
712 tasklet_schedule(&host->tasklet);
715 * Regardless of what the documentation says, we have
716 * to wait for NOTBUSY even after block read
719 * When the DMA transfer is complete, the controller
720 * may still be reading the CRC from the card, i.e.
721 * the data transfer is still in progress and we
722 * haven't seen all the potential error bits yet.
724 * The interrupt handler will schedule a different
725 * tasklet to finish things up when the data transfer
726 * is completely done.
728 * We may not complete the mmc request here anyway
729 * because the mmc layer may call back and cause us to
730 * violate the "don't submit new operations from the
731 * completion callback" rule of the dma engine
734 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
739 * Returns a mask of interrupt flags to be enabled after the whole
740 * request has been prepared.
742 static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
746 data->error = -EINPROGRESS;
750 host->data_chan = NULL;
752 iflags = ATMCI_DATA_ERROR_FLAGS;
755 * Errata: MMC data write operation with less than 12
756 * bytes is impossible.
758 * Errata: MCI Transmit Data Register (TDR) FIFO
759 * corruption when length is not multiple of 4.
761 if (data->blocks * data->blksz < 12
762 || (data->blocks * data->blksz) & 3)
763 host->need_reset = true;
765 host->pio_offset = 0;
766 if (data->flags & MMC_DATA_READ)
767 iflags |= ATMCI_RXRDY;
769 iflags |= ATMCI_TXRDY;
775 * Set interrupt flags and set block length into the MCI mode register even
776 * if this value is also accessible in the MCI block register. It seems to be
777 * necessary before the High Speed MCI version. It also map sg and configure
781 atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
785 enum dma_data_direction dir;
787 data->error = -EINPROGRESS;
791 iflags = ATMCI_DATA_ERROR_FLAGS;
793 /* Enable pdc mode */
794 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
796 if (data->flags & MMC_DATA_READ) {
797 dir = DMA_FROM_DEVICE;
798 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
801 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE;
805 tmp = atmci_readl(host, ATMCI_MR);
807 tmp |= ATMCI_BLKLEN(data->blksz);
808 atmci_writel(host, ATMCI_MR, tmp);
811 host->data_size = data->blocks * data->blksz;
812 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
814 atmci_pdc_set_both_buf(host,
815 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
821 atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
823 struct dma_chan *chan;
824 struct dma_async_tx_descriptor *desc;
825 struct scatterlist *sg;
827 enum dma_data_direction direction;
828 enum dma_transfer_direction slave_dirn;
832 data->error = -EINPROGRESS;
838 iflags = ATMCI_DATA_ERROR_FLAGS;
841 * We don't do DMA on "complex" transfers, i.e. with
842 * non-word-aligned buffers or lengths. Also, we don't bother
843 * with all the DMA setup overhead for short transfers.
845 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
846 return atmci_prepare_data(host, data);
848 return atmci_prepare_data(host, data);
850 for_each_sg(data->sg, sg, data->sg_len, i) {
851 if (sg->offset & 3 || sg->length & 3)
852 return atmci_prepare_data(host, data);
855 /* If we don't have a channel, we can't do DMA */
856 chan = host->dma.chan;
858 host->data_chan = chan;
863 if (host->caps.has_dma)
864 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN);
866 if (data->flags & MMC_DATA_READ) {
867 direction = DMA_FROM_DEVICE;
868 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
870 direction = DMA_TO_DEVICE;
871 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
874 sglen = dma_map_sg(chan->device->dev, data->sg,
875 data->sg_len, direction);
877 dmaengine_slave_config(chan, &host->dma_conf);
878 desc = dmaengine_prep_slave_sg(chan,
879 data->sg, sglen, slave_dirn,
880 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
884 host->dma.data_desc = desc;
885 desc->callback = atmci_dma_complete;
886 desc->callback_param = host;
890 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
895 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
901 * Start PDC according to transfer direction.
904 atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
906 if (data->flags & MMC_DATA_READ)
907 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
909 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
913 atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
915 struct dma_chan *chan = host->data_chan;
916 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
919 dmaengine_submit(desc);
920 dma_async_issue_pending(chan);
924 static void atmci_stop_transfer(struct atmel_mci *host)
926 atmci_set_pending(host, EVENT_XFER_COMPLETE);
927 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
931 * Stop data transfer because error(s) occured.
933 static void atmci_stop_transfer_pdc(struct atmel_mci *host)
935 atmci_set_pending(host, EVENT_XFER_COMPLETE);
936 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
939 static void atmci_stop_transfer_dma(struct atmel_mci *host)
941 struct dma_chan *chan = host->data_chan;
944 dmaengine_terminate_all(chan);
945 atmci_dma_cleanup(host);
947 /* Data transfer was stopped by the interrupt handler */
948 atmci_set_pending(host, EVENT_XFER_COMPLETE);
949 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
954 * Start a request: prepare data if needed, prepare the command and activate
957 static void atmci_start_request(struct atmel_mci *host,
958 struct atmel_mci_slot *slot)
960 struct mmc_request *mrq;
961 struct mmc_command *cmd;
962 struct mmc_data *data;
967 host->cur_slot = slot;
970 host->pending_events = 0;
971 host->completed_events = 0;
972 host->data_status = 0;
974 if (host->need_reset) {
975 iflags = atmci_readl(host, ATMCI_IMR);
976 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
977 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
978 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
979 atmci_writel(host, ATMCI_MR, host->mode_reg);
980 if (host->caps.has_cfg_reg)
981 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
982 atmci_writel(host, ATMCI_IER, iflags);
983 host->need_reset = false;
985 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
987 iflags = atmci_readl(host, ATMCI_IMR);
988 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
989 dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
992 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
993 /* Send init sequence (74 clock cycles) */
994 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
995 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
1001 atmci_set_timeout(host, slot, data);
1003 /* Must set block count/size before sending command */
1004 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
1005 | ATMCI_BLKLEN(data->blksz));
1006 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
1007 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
1009 iflags |= host->prepare_data(host, data);
1012 iflags |= ATMCI_CMDRDY;
1014 cmdflags = atmci_prepare_command(slot->mmc, cmd);
1015 atmci_send_command(host, cmd, cmdflags);
1018 host->submit_data(host, data);
1021 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
1022 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
1023 if (!(data->flags & MMC_DATA_WRITE))
1024 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
1025 if (data->flags & MMC_DATA_STREAM)
1026 host->stop_cmdr |= ATMCI_CMDR_STREAM;
1028 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
1032 * We could have enabled interrupts earlier, but I suspect
1033 * that would open up a nice can of interesting race
1034 * conditions (e.g. command and data complete, but stop not
1037 atmci_writel(host, ATMCI_IER, iflags);
1040 static void atmci_queue_request(struct atmel_mci *host,
1041 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1043 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1046 spin_lock_bh(&host->lock);
1048 if (host->state == STATE_IDLE) {
1049 host->state = STATE_SENDING_CMD;
1050 atmci_start_request(host, slot);
1052 list_add_tail(&slot->queue_node, &host->queue);
1054 spin_unlock_bh(&host->lock);
1057 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1059 struct atmel_mci_slot *slot = mmc_priv(mmc);
1060 struct atmel_mci *host = slot->host;
1061 struct mmc_data *data;
1066 * We may "know" the card is gone even though there's still an
1067 * electrical connection. If so, we really need to communicate
1068 * this to the MMC core since there won't be any more
1069 * interrupts as the card is completely removed. Otherwise,
1070 * the MMC core might believe the card is still there even
1071 * though the card was just removed very slowly.
1073 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1074 mrq->cmd->error = -ENOMEDIUM;
1075 mmc_request_done(mmc, mrq);
1079 /* We don't support multiple blocks of weird lengths. */
1081 if (data && data->blocks > 1 && data->blksz & 3) {
1082 mrq->cmd->error = -EINVAL;
1083 mmc_request_done(mmc, mrq);
1086 atmci_queue_request(host, slot, mrq);
1089 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1091 struct atmel_mci_slot *slot = mmc_priv(mmc);
1092 struct atmel_mci *host = slot->host;
1095 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
1096 switch (ios->bus_width) {
1097 case MMC_BUS_WIDTH_1:
1098 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
1100 case MMC_BUS_WIDTH_4:
1101 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
1106 unsigned int clock_min = ~0U;
1109 spin_lock_bh(&host->lock);
1110 if (!host->mode_reg) {
1111 clk_enable(host->mck);
1112 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1113 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1114 if (host->caps.has_cfg_reg)
1115 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1119 * Use mirror of ios->clock to prevent race with mmc
1120 * core ios update when finding the minimum.
1122 slot->clock = ios->clock;
1123 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1124 if (host->slot[i] && host->slot[i]->clock
1125 && host->slot[i]->clock < clock_min)
1126 clock_min = host->slot[i]->clock;
1129 /* Calculate clock divider */
1130 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1132 dev_warn(&mmc->class_dev,
1133 "clock %u too slow; using %lu\n",
1134 clock_min, host->bus_hz / (2 * 256));
1138 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
1141 * WRPROOF and RDPROOF prevent overruns/underruns by
1142 * stopping the clock when the FIFO is full/empty.
1143 * This state is not expected to last for long.
1145 if (host->caps.has_rwproof)
1146 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
1148 if (host->caps.has_cfg_reg) {
1149 /* setup High Speed mode in relation with card capacity */
1150 if (ios->timing == MMC_TIMING_SD_HS)
1151 host->cfg_reg |= ATMCI_CFG_HSMODE;
1153 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
1156 if (list_empty(&host->queue)) {
1157 atmci_writel(host, ATMCI_MR, host->mode_reg);
1158 if (host->caps.has_cfg_reg)
1159 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1161 host->need_clock_update = true;
1164 spin_unlock_bh(&host->lock);
1166 bool any_slot_active = false;
1168 spin_lock_bh(&host->lock);
1170 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1171 if (host->slot[i] && host->slot[i]->clock) {
1172 any_slot_active = true;
1176 if (!any_slot_active) {
1177 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
1178 if (host->mode_reg) {
1179 atmci_readl(host, ATMCI_MR);
1180 clk_disable(host->mck);
1184 spin_unlock_bh(&host->lock);
1187 switch (ios->power_mode) {
1189 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1193 * TODO: None of the currently available AVR32-based
1194 * boards allow MMC power to be turned off. Implement
1195 * power control when this can be tested properly.
1197 * We also need to hook this into the clock management
1198 * somehow so that newly inserted cards aren't
1199 * subjected to a fast clock before we have a chance
1200 * to figure out what the maximum rate is. Currently,
1201 * there's no way to avoid this, and there never will
1202 * be for boards that don't support power control.
1208 static int atmci_get_ro(struct mmc_host *mmc)
1210 int read_only = -ENOSYS;
1211 struct atmel_mci_slot *slot = mmc_priv(mmc);
1213 if (gpio_is_valid(slot->wp_pin)) {
1214 read_only = gpio_get_value(slot->wp_pin);
1215 dev_dbg(&mmc->class_dev, "card is %s\n",
1216 read_only ? "read-only" : "read-write");
1222 static int atmci_get_cd(struct mmc_host *mmc)
1224 int present = -ENOSYS;
1225 struct atmel_mci_slot *slot = mmc_priv(mmc);
1227 if (gpio_is_valid(slot->detect_pin)) {
1228 present = !(gpio_get_value(slot->detect_pin) ^
1229 slot->detect_is_active_high);
1230 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1231 present ? "" : "not ");
1237 static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1239 struct atmel_mci_slot *slot = mmc_priv(mmc);
1240 struct atmel_mci *host = slot->host;
1243 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
1245 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
1248 static const struct mmc_host_ops atmci_ops = {
1249 .request = atmci_request,
1250 .set_ios = atmci_set_ios,
1251 .get_ro = atmci_get_ro,
1252 .get_cd = atmci_get_cd,
1253 .enable_sdio_irq = atmci_enable_sdio_irq,
1256 /* Called with host->lock held */
1257 static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1258 __releases(&host->lock)
1259 __acquires(&host->lock)
1261 struct atmel_mci_slot *slot = NULL;
1262 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1264 WARN_ON(host->cmd || host->data);
1267 * Update the MMC clock rate if necessary. This may be
1268 * necessary if set_ios() is called when a different slot is
1269 * busy transferring data.
1271 if (host->need_clock_update) {
1272 atmci_writel(host, ATMCI_MR, host->mode_reg);
1273 if (host->caps.has_cfg_reg)
1274 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1277 host->cur_slot->mrq = NULL;
1279 if (!list_empty(&host->queue)) {
1280 slot = list_entry(host->queue.next,
1281 struct atmel_mci_slot, queue_node);
1282 list_del(&slot->queue_node);
1283 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1284 mmc_hostname(slot->mmc));
1285 host->state = STATE_SENDING_CMD;
1286 atmci_start_request(host, slot);
1288 dev_vdbg(&host->pdev->dev, "list empty\n");
1289 host->state = STATE_IDLE;
1292 spin_unlock(&host->lock);
1293 mmc_request_done(prev_mmc, mrq);
1294 spin_lock(&host->lock);
1297 static void atmci_command_complete(struct atmel_mci *host,
1298 struct mmc_command *cmd)
1300 u32 status = host->cmd_status;
1302 /* Read the response from the card (up to 16 bytes) */
1303 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1304 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1305 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1306 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
1308 if (status & ATMCI_RTOE)
1309 cmd->error = -ETIMEDOUT;
1310 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
1311 cmd->error = -EILSEQ;
1312 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
1318 dev_dbg(&host->pdev->dev,
1319 "command error: status=0x%08x\n", status);
1322 host->stop_transfer(host);
1324 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY
1325 | ATMCI_TXRDY | ATMCI_RXRDY
1326 | ATMCI_DATA_ERROR_FLAGS);
1331 static void atmci_detect_change(unsigned long data)
1333 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1338 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1339 * freeing the interrupt. We must not re-enable the interrupt
1340 * if it has been freed, and if we're shutting down, it
1341 * doesn't really matter whether the card is present or not.
1344 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
1347 enable_irq(gpio_to_irq(slot->detect_pin));
1348 present = !(gpio_get_value(slot->detect_pin) ^
1349 slot->detect_is_active_high);
1350 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
1352 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1353 present, present_old);
1355 if (present != present_old) {
1356 struct atmel_mci *host = slot->host;
1357 struct mmc_request *mrq;
1359 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1360 present ? "inserted" : "removed");
1362 spin_lock(&host->lock);
1365 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1367 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1369 /* Clean up queue if present */
1372 if (mrq == host->mrq) {
1374 * Reset controller to terminate any ongoing
1375 * commands or data transfers.
1377 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1378 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1379 atmci_writel(host, ATMCI_MR, host->mode_reg);
1380 if (host->caps.has_cfg_reg)
1381 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1386 switch (host->state) {
1389 case STATE_SENDING_CMD:
1390 mrq->cmd->error = -ENOMEDIUM;
1394 case STATE_SENDING_DATA:
1395 mrq->data->error = -ENOMEDIUM;
1396 host->stop_transfer(host);
1398 case STATE_DATA_BUSY:
1399 case STATE_DATA_ERROR:
1400 if (mrq->data->error == -EINPROGRESS)
1401 mrq->data->error = -ENOMEDIUM;
1405 case STATE_SENDING_STOP:
1406 mrq->stop->error = -ENOMEDIUM;
1410 atmci_request_end(host, mrq);
1412 list_del(&slot->queue_node);
1413 mrq->cmd->error = -ENOMEDIUM;
1415 mrq->data->error = -ENOMEDIUM;
1417 mrq->stop->error = -ENOMEDIUM;
1419 spin_unlock(&host->lock);
1420 mmc_request_done(slot->mmc, mrq);
1421 spin_lock(&host->lock);
1424 spin_unlock(&host->lock);
1426 mmc_detect_change(slot->mmc, 0);
1430 static void atmci_tasklet_func(unsigned long priv)
1432 struct atmel_mci *host = (struct atmel_mci *)priv;
1433 struct mmc_request *mrq = host->mrq;
1434 struct mmc_data *data = host->data;
1435 struct mmc_command *cmd = host->cmd;
1436 enum atmel_mci_state state = host->state;
1437 enum atmel_mci_state prev_state;
1440 spin_lock(&host->lock);
1442 state = host->state;
1444 dev_vdbg(&host->pdev->dev,
1445 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1446 state, host->pending_events, host->completed_events,
1447 atmci_readl(host, ATMCI_IMR));
1456 case STATE_SENDING_CMD:
1457 if (!atmci_test_and_clear_pending(host,
1458 EVENT_CMD_COMPLETE))
1462 atmci_set_completed(host, EVENT_CMD_COMPLETE);
1463 atmci_command_complete(host, mrq->cmd);
1464 if (!mrq->data || cmd->error) {
1465 atmci_request_end(host, host->mrq);
1469 prev_state = state = STATE_SENDING_DATA;
1472 case STATE_SENDING_DATA:
1473 if (atmci_test_and_clear_pending(host,
1474 EVENT_DATA_ERROR)) {
1475 host->stop_transfer(host);
1477 atmci_send_stop_cmd(host, data);
1478 state = STATE_DATA_ERROR;
1482 if (!atmci_test_and_clear_pending(host,
1483 EVENT_XFER_COMPLETE))
1486 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1487 prev_state = state = STATE_DATA_BUSY;
1490 case STATE_DATA_BUSY:
1491 if (!atmci_test_and_clear_pending(host,
1492 EVENT_DATA_COMPLETE))
1496 atmci_set_completed(host, EVENT_DATA_COMPLETE);
1497 status = host->data_status;
1498 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
1499 if (status & ATMCI_DTOE) {
1500 dev_dbg(&host->pdev->dev,
1501 "data timeout error\n");
1502 data->error = -ETIMEDOUT;
1503 } else if (status & ATMCI_DCRCE) {
1504 dev_dbg(&host->pdev->dev,
1505 "data CRC error\n");
1506 data->error = -EILSEQ;
1508 dev_dbg(&host->pdev->dev,
1509 "data FIFO error (status=%08x)\n",
1514 data->bytes_xfered = data->blocks * data->blksz;
1516 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS);
1520 atmci_request_end(host, host->mrq);
1524 prev_state = state = STATE_SENDING_STOP;
1526 atmci_send_stop_cmd(host, data);
1529 case STATE_SENDING_STOP:
1530 if (!atmci_test_and_clear_pending(host,
1531 EVENT_CMD_COMPLETE))
1535 atmci_command_complete(host, mrq->stop);
1536 atmci_request_end(host, host->mrq);
1539 case STATE_DATA_ERROR:
1540 if (!atmci_test_and_clear_pending(host,
1541 EVENT_XFER_COMPLETE))
1544 state = STATE_DATA_BUSY;
1547 } while (state != prev_state);
1549 host->state = state;
1552 spin_unlock(&host->lock);
1555 static void atmci_read_data_pio(struct atmel_mci *host)
1557 struct scatterlist *sg = host->sg;
1558 void *buf = sg_virt(sg);
1559 unsigned int offset = host->pio_offset;
1560 struct mmc_data *data = host->data;
1563 unsigned int nbytes = 0;
1566 value = atmci_readl(host, ATMCI_RDR);
1567 if (likely(offset + 4 <= sg->length)) {
1568 put_unaligned(value, (u32 *)(buf + offset));
1573 if (offset == sg->length) {
1574 flush_dcache_page(sg_page(sg));
1575 host->sg = sg = sg_next(sg);
1583 unsigned int remaining = sg->length - offset;
1584 memcpy(buf + offset, &value, remaining);
1585 nbytes += remaining;
1587 flush_dcache_page(sg_page(sg));
1588 host->sg = sg = sg_next(sg);
1592 offset = 4 - remaining;
1594 memcpy(buf, (u8 *)&value + remaining, offset);
1598 status = atmci_readl(host, ATMCI_SR);
1599 if (status & ATMCI_DATA_ERROR_FLAGS) {
1600 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
1601 | ATMCI_DATA_ERROR_FLAGS));
1602 host->data_status = status;
1603 data->bytes_xfered += nbytes;
1605 atmci_set_pending(host, EVENT_DATA_ERROR);
1606 tasklet_schedule(&host->tasklet);
1609 } while (status & ATMCI_RXRDY);
1611 host->pio_offset = offset;
1612 data->bytes_xfered += nbytes;
1617 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1618 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1619 data->bytes_xfered += nbytes;
1621 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1624 static void atmci_write_data_pio(struct atmel_mci *host)
1626 struct scatterlist *sg = host->sg;
1627 void *buf = sg_virt(sg);
1628 unsigned int offset = host->pio_offset;
1629 struct mmc_data *data = host->data;
1632 unsigned int nbytes = 0;
1635 if (likely(offset + 4 <= sg->length)) {
1636 value = get_unaligned((u32 *)(buf + offset));
1637 atmci_writel(host, ATMCI_TDR, value);
1641 if (offset == sg->length) {
1642 host->sg = sg = sg_next(sg);
1650 unsigned int remaining = sg->length - offset;
1653 memcpy(&value, buf + offset, remaining);
1654 nbytes += remaining;
1656 host->sg = sg = sg_next(sg);
1658 atmci_writel(host, ATMCI_TDR, value);
1662 offset = 4 - remaining;
1664 memcpy((u8 *)&value + remaining, buf, offset);
1665 atmci_writel(host, ATMCI_TDR, value);
1669 status = atmci_readl(host, ATMCI_SR);
1670 if (status & ATMCI_DATA_ERROR_FLAGS) {
1671 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
1672 | ATMCI_DATA_ERROR_FLAGS));
1673 host->data_status = status;
1674 data->bytes_xfered += nbytes;
1676 atmci_set_pending(host, EVENT_DATA_ERROR);
1677 tasklet_schedule(&host->tasklet);
1680 } while (status & ATMCI_TXRDY);
1682 host->pio_offset = offset;
1683 data->bytes_xfered += nbytes;
1688 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1689 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1690 data->bytes_xfered += nbytes;
1692 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1695 static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
1697 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
1699 host->cmd_status = status;
1701 atmci_set_pending(host, EVENT_CMD_COMPLETE);
1702 tasklet_schedule(&host->tasklet);
1705 static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1709 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1710 struct atmel_mci_slot *slot = host->slot[i];
1711 if (slot && (status & slot->sdio_irq)) {
1712 mmc_signal_sdio_irq(slot->mmc);
1718 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1720 struct atmel_mci *host = dev_id;
1721 u32 status, mask, pending;
1722 unsigned int pass_count = 0;
1725 status = atmci_readl(host, ATMCI_SR);
1726 mask = atmci_readl(host, ATMCI_IMR);
1727 pending = status & mask;
1731 if (pending & ATMCI_DATA_ERROR_FLAGS) {
1732 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
1733 | ATMCI_RXRDY | ATMCI_TXRDY);
1734 pending &= atmci_readl(host, ATMCI_IMR);
1736 host->data_status = status;
1738 atmci_set_pending(host, EVENT_DATA_ERROR);
1739 tasklet_schedule(&host->tasklet);
1742 if (pending & ATMCI_TXBUFE) {
1743 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
1744 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1746 * We can receive this interruption before having configured
1747 * the second pdc buffer, so we need to reconfigure first and
1748 * second buffers again
1750 if (host->data_size) {
1751 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
1752 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1753 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1755 atmci_pdc_complete(host);
1757 } else if (pending & ATMCI_ENDTX) {
1758 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1760 if (host->data_size) {
1761 atmci_pdc_set_single_buf(host,
1762 XFER_TRANSMIT, PDC_SECOND_BUF);
1763 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1767 if (pending & ATMCI_RXBUFF) {
1768 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
1769 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1771 * We can receive this interruption before having configured
1772 * the second pdc buffer, so we need to reconfigure first and
1773 * second buffers again
1775 if (host->data_size) {
1776 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
1777 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1778 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
1780 atmci_pdc_complete(host);
1782 } else if (pending & ATMCI_ENDRX) {
1783 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1785 if (host->data_size) {
1786 atmci_pdc_set_single_buf(host,
1787 XFER_RECEIVE, PDC_SECOND_BUF);
1788 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1793 if (pending & ATMCI_NOTBUSY) {
1794 atmci_writel(host, ATMCI_IDR,
1795 ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY);
1796 if (!host->data_status)
1797 host->data_status = status;
1799 atmci_set_pending(host, EVENT_DATA_COMPLETE);
1800 tasklet_schedule(&host->tasklet);
1802 if (pending & ATMCI_RXRDY)
1803 atmci_read_data_pio(host);
1804 if (pending & ATMCI_TXRDY)
1805 atmci_write_data_pio(host);
1807 if (pending & ATMCI_CMDRDY)
1808 atmci_cmd_interrupt(host, status);
1810 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
1811 atmci_sdio_interrupt(host, status);
1813 } while (pass_count++ < 5);
1815 return pass_count ? IRQ_HANDLED : IRQ_NONE;
1818 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1820 struct atmel_mci_slot *slot = dev_id;
1823 * Disable interrupts until the pin has stabilized and check
1824 * the state then. Use mod_timer() since we may be in the
1825 * middle of the timer routine when this interrupt triggers.
1827 disable_irq_nosync(irq);
1828 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
1833 static int __init atmci_init_slot(struct atmel_mci *host,
1834 struct mci_slot_pdata *slot_data, unsigned int id,
1835 u32 sdc_reg, u32 sdio_irq)
1837 struct mmc_host *mmc;
1838 struct atmel_mci_slot *slot;
1840 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1844 slot = mmc_priv(mmc);
1847 slot->detect_pin = slot_data->detect_pin;
1848 slot->wp_pin = slot_data->wp_pin;
1849 slot->detect_is_active_high = slot_data->detect_is_active_high;
1850 slot->sdc_reg = sdc_reg;
1851 slot->sdio_irq = sdio_irq;
1853 mmc->ops = &atmci_ops;
1854 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
1855 mmc->f_max = host->bus_hz / 2;
1856 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1858 mmc->caps |= MMC_CAP_SDIO_IRQ;
1859 if (host->caps.has_highspeed)
1860 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1861 if (slot_data->bus_width >= 4)
1862 mmc->caps |= MMC_CAP_4_BIT_DATA;
1865 mmc->max_req_size = 32768 * 512;
1866 mmc->max_blk_size = 32768;
1867 mmc->max_blk_count = 512;
1869 /* Assume card is present initially */
1870 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1871 if (gpio_is_valid(slot->detect_pin)) {
1872 if (gpio_request(slot->detect_pin, "mmc_detect")) {
1873 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1874 slot->detect_pin = -EBUSY;
1875 } else if (gpio_get_value(slot->detect_pin) ^
1876 slot->detect_is_active_high) {
1877 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1881 if (!gpio_is_valid(slot->detect_pin))
1882 mmc->caps |= MMC_CAP_NEEDS_POLL;
1884 if (gpio_is_valid(slot->wp_pin)) {
1885 if (gpio_request(slot->wp_pin, "mmc_wp")) {
1886 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1887 slot->wp_pin = -EBUSY;
1891 host->slot[id] = slot;
1894 if (gpio_is_valid(slot->detect_pin)) {
1897 setup_timer(&slot->detect_timer, atmci_detect_change,
1898 (unsigned long)slot);
1900 ret = request_irq(gpio_to_irq(slot->detect_pin),
1901 atmci_detect_interrupt,
1902 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1903 "mmc-detect", slot);
1905 dev_dbg(&mmc->class_dev,
1906 "could not request IRQ %d for detect pin\n",
1907 gpio_to_irq(slot->detect_pin));
1908 gpio_free(slot->detect_pin);
1909 slot->detect_pin = -EBUSY;
1913 atmci_init_debugfs(slot);
1918 static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
1921 /* Debugfs stuff is cleaned up by mmc core */
1923 set_bit(ATMCI_SHUTDOWN, &slot->flags);
1926 mmc_remove_host(slot->mmc);
1928 if (gpio_is_valid(slot->detect_pin)) {
1929 int pin = slot->detect_pin;
1931 free_irq(gpio_to_irq(pin), slot);
1932 del_timer_sync(&slot->detect_timer);
1935 if (gpio_is_valid(slot->wp_pin))
1936 gpio_free(slot->wp_pin);
1938 slot->host->slot[id] = NULL;
1939 mmc_free_host(slot->mmc);
1942 static bool atmci_filter(struct dma_chan *chan, void *slave)
1944 struct mci_dma_data *sl = slave;
1946 if (sl && find_slave_dev(sl) == chan->device->dev) {
1947 chan->private = slave_data_ptr(sl);
1954 static bool atmci_configure_dma(struct atmel_mci *host)
1956 struct mci_platform_data *pdata;
1961 pdata = host->pdev->dev.platform_data;
1963 if (pdata && find_slave_dev(pdata->dma_slave)) {
1964 dma_cap_mask_t mask;
1966 /* Try to grab a DMA channel */
1968 dma_cap_set(DMA_SLAVE, mask);
1970 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
1972 if (!host->dma.chan) {
1973 dev_warn(&host->pdev->dev, "no DMA channel available\n");
1976 dev_info(&host->pdev->dev,
1977 "using %s for DMA transfers\n",
1978 dma_chan_name(host->dma.chan));
1980 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
1981 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1982 host->dma_conf.src_maxburst = 1;
1983 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
1984 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1985 host->dma_conf.dst_maxburst = 1;
1986 host->dma_conf.device_fc = false;
1991 static inline unsigned int atmci_get_version(struct atmel_mci *host)
1993 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
1997 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
1998 * HSMCI provides DMA support and a new config register but no more supports
2001 static void __init atmci_get_cap(struct atmel_mci *host)
2003 unsigned int version;
2005 version = atmci_get_version(host);
2006 dev_info(&host->pdev->dev,
2007 "version: 0x%x\n", version);
2009 host->caps.has_dma = 0;
2010 host->caps.has_pdc = 0;
2011 host->caps.has_cfg_reg = 0;
2012 host->caps.has_cstor_reg = 0;
2013 host->caps.has_highspeed = 0;
2014 host->caps.has_rwproof = 0;
2016 /* keep only major version number */
2017 switch (version & 0xf00) {
2020 host->caps.has_pdc = 1;
2021 host->caps.has_rwproof = 1;
2026 #ifdef CONFIG_AT_HDMAC
2027 host->caps.has_dma = 1;
2029 host->caps.has_dma = 0;
2030 dev_info(&host->pdev->dev,
2031 "has dma capability but dma engine is not selected, then use pio\n");
2033 host->caps.has_cfg_reg = 1;
2034 host->caps.has_cstor_reg = 1;
2035 host->caps.has_highspeed = 1;
2036 host->caps.has_rwproof = 1;
2039 dev_warn(&host->pdev->dev,
2040 "Unmanaged mci version, set minimum capabilities\n");
2045 static int __init atmci_probe(struct platform_device *pdev)
2047 struct mci_platform_data *pdata;
2048 struct atmel_mci *host;
2049 struct resource *regs;
2050 unsigned int nr_slots;
2054 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2057 pdata = pdev->dev.platform_data;
2060 irq = platform_get_irq(pdev, 0);
2064 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2069 spin_lock_init(&host->lock);
2070 INIT_LIST_HEAD(&host->queue);
2072 host->mck = clk_get(&pdev->dev, "mci_clk");
2073 if (IS_ERR(host->mck)) {
2074 ret = PTR_ERR(host->mck);
2079 host->regs = ioremap(regs->start, resource_size(regs));
2083 clk_enable(host->mck);
2084 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
2085 host->bus_hz = clk_get_rate(host->mck);
2086 clk_disable(host->mck);
2088 host->mapbase = regs->start;
2090 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
2092 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
2094 goto err_request_irq;
2096 /* Get MCI capabilities and set operations according to it */
2097 atmci_get_cap(host);
2098 if (host->caps.has_dma && atmci_configure_dma(host)) {
2099 host->prepare_data = &atmci_prepare_data_dma;
2100 host->submit_data = &atmci_submit_data_dma;
2101 host->stop_transfer = &atmci_stop_transfer_dma;
2102 } else if (host->caps.has_pdc) {
2103 dev_info(&pdev->dev, "using PDC\n");
2104 host->prepare_data = &atmci_prepare_data_pdc;
2105 host->submit_data = &atmci_submit_data_pdc;
2106 host->stop_transfer = &atmci_stop_transfer_pdc;
2108 dev_info(&pdev->dev, "using PIO\n");
2109 host->prepare_data = &atmci_prepare_data;
2110 host->submit_data = &atmci_submit_data;
2111 host->stop_transfer = &atmci_stop_transfer;
2114 platform_set_drvdata(pdev, host);
2116 /* We need at least one slot to succeed */
2119 if (pdata->slot[0].bus_width) {
2120 ret = atmci_init_slot(host, &pdata->slot[0],
2121 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
2125 if (pdata->slot[1].bus_width) {
2126 ret = atmci_init_slot(host, &pdata->slot[1],
2127 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
2133 dev_err(&pdev->dev, "init failed: no slot defined\n");
2137 dev_info(&pdev->dev,
2138 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2139 host->mapbase, irq, nr_slots);
2145 dma_release_channel(host->dma.chan);
2146 free_irq(irq, host);
2148 iounmap(host->regs);
2156 static int __exit atmci_remove(struct platform_device *pdev)
2158 struct atmel_mci *host = platform_get_drvdata(pdev);
2161 platform_set_drvdata(pdev, NULL);
2163 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2165 atmci_cleanup_slot(host->slot[i], i);
2168 clk_enable(host->mck);
2169 atmci_writel(host, ATMCI_IDR, ~0UL);
2170 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2171 atmci_readl(host, ATMCI_SR);
2172 clk_disable(host->mck);
2174 #ifdef CONFIG_MMC_ATMELMCI_DMA
2176 dma_release_channel(host->dma.chan);
2179 free_irq(platform_get_irq(pdev, 0), host);
2180 iounmap(host->regs);
2189 static int atmci_suspend(struct device *dev)
2191 struct atmel_mci *host = dev_get_drvdata(dev);
2194 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2195 struct atmel_mci_slot *slot = host->slot[i];
2200 ret = mmc_suspend_host(slot->mmc);
2203 slot = host->slot[i];
2205 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2206 mmc_resume_host(host->slot[i]->mmc);
2207 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2212 set_bit(ATMCI_SUSPENDED, &slot->flags);
2219 static int atmci_resume(struct device *dev)
2221 struct atmel_mci *host = dev_get_drvdata(dev);
2225 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2226 struct atmel_mci_slot *slot = host->slot[i];
2229 slot = host->slot[i];
2232 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2234 err = mmc_resume_host(slot->mmc);
2238 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2243 static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2244 #define ATMCI_PM_OPS (&atmci_pm)
2246 #define ATMCI_PM_OPS NULL
2249 static struct platform_driver atmci_driver = {
2250 .remove = __exit_p(atmci_remove),
2252 .name = "atmel_mci",
2257 static int __init atmci_init(void)
2259 return platform_driver_probe(&atmci_driver, atmci_probe);
2262 static void __exit atmci_exit(void)
2264 platform_driver_unregister(&atmci_driver);
2267 late_initcall(atmci_init); /* try to load after dma driver when built-in */
2268 module_exit(atmci_exit);
2270 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
2271 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2272 MODULE_LICENSE("GPL v2");