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>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
25 #include <linux/platform_device.h>
26 #include <linux/scatterlist.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/types.h>
31 #include <linux/platform_data/atmel.h>
33 #include <linux/mmc/host.h>
34 #include <linux/mmc/sdio.h>
36 #include <mach/atmel-mci.h>
37 #include <linux/atmel-mci.h>
38 #include <linux/atmel_pdc.h>
41 #include <asm/unaligned.h>
45 #include "atmel-mci-regs.h"
47 #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
48 #define ATMCI_DMA_THRESHOLD 16
57 enum atmel_mci_state {
61 STATE_WAITING_NOTBUSY,
76 struct atmel_mci_caps {
77 bool has_dma_conf_reg;
84 bool has_bad_data_ordering;
85 bool need_reset_after_xfer;
86 bool need_blksz_mul_4;
87 bool need_notbusy_for_read_ops;
90 struct atmel_mci_dma {
91 struct dma_chan *chan;
92 struct dma_async_tx_descriptor *data_desc;
96 * struct atmel_mci - MMC controller state shared between all slots
97 * @lock: Spinlock protecting the queue and associated data.
98 * @regs: Pointer to MMIO registers.
99 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
100 * @pio_offset: Offset into the current scatterlist entry.
101 * @buffer: Buffer used if we don't have the r/w proof capability. We
102 * don't have the time to switch pdc buffers so we have to use only
103 * one buffer for the full transaction.
104 * @buf_size: size of the buffer.
105 * @phys_buf_addr: buffer address needed for pdc.
106 * @cur_slot: The slot which is currently using the controller.
107 * @mrq: The request currently being processed on @cur_slot,
108 * or NULL if the controller is idle.
109 * @cmd: The command currently being sent to the card, or NULL.
110 * @data: The data currently being transferred, or NULL if no data
111 * transfer is in progress.
112 * @data_size: just data->blocks * data->blksz.
113 * @dma: DMA client state.
114 * @data_chan: DMA channel being used for the current data transfer.
115 * @cmd_status: Snapshot of SR taken upon completion of the current
116 * command. Only valid when EVENT_CMD_COMPLETE is pending.
117 * @data_status: Snapshot of SR taken upon completion of the current
118 * data transfer. Only valid when EVENT_DATA_COMPLETE or
119 * EVENT_DATA_ERROR is pending.
120 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
122 * @tasklet: Tasklet running the request state machine.
123 * @pending_events: Bitmask of events flagged by the interrupt handler
124 * to be processed by the tasklet.
125 * @completed_events: Bitmask of events which the state machine has
127 * @state: Tasklet state.
128 * @queue: List of slots waiting for access to the controller.
129 * @need_clock_update: Update the clock rate before the next request.
130 * @need_reset: Reset controller before next request.
131 * @timer: Timer to balance the data timeout error flag which cannot rise.
132 * @mode_reg: Value of the MR register.
133 * @cfg_reg: Value of the CFG register.
134 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
135 * rate and timeout calculations.
136 * @mapbase: Physical address of the MMIO registers.
137 * @mck: The peripheral bus clock hooked up to the MMC controller.
138 * @pdev: Platform device associated with the MMC controller.
139 * @slot: Slots sharing this MMC controller.
140 * @caps: MCI capabilities depending on MCI version.
141 * @prepare_data: function to setup MCI before data transfer which
142 * depends on MCI capabilities.
143 * @submit_data: function to start data transfer which depends on MCI
145 * @stop_transfer: function to stop data transfer which depends on MCI
151 * @lock is a softirq-safe spinlock protecting @queue as well as
152 * @cur_slot, @mrq and @state. These must always be updated
153 * at the same time while holding @lock.
155 * @lock also protects mode_reg and need_clock_update since these are
156 * used to synchronize mode register updates with the queue
159 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
160 * and must always be written at the same time as the slot is added to
163 * @pending_events and @completed_events are accessed using atomic bit
164 * operations, so they don't need any locking.
166 * None of the fields touched by the interrupt handler need any
167 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
168 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
169 * interrupts must be disabled and @data_status updated with a
170 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
171 * CMDRDY interrupt must be disabled and @cmd_status updated with a
172 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
173 * bytes_xfered field of @data must be written. This is ensured by
180 struct scatterlist *sg;
182 unsigned int pio_offset;
183 unsigned int *buffer;
184 unsigned int buf_size;
185 dma_addr_t buf_phys_addr;
187 struct atmel_mci_slot *cur_slot;
188 struct mmc_request *mrq;
189 struct mmc_command *cmd;
190 struct mmc_data *data;
191 unsigned int data_size;
193 struct atmel_mci_dma dma;
194 struct dma_chan *data_chan;
195 struct dma_slave_config dma_conf;
201 struct tasklet_struct tasklet;
202 unsigned long pending_events;
203 unsigned long completed_events;
204 enum atmel_mci_state state;
205 struct list_head queue;
207 bool need_clock_update;
209 struct timer_list timer;
212 unsigned long bus_hz;
213 unsigned long mapbase;
215 struct platform_device *pdev;
217 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
219 struct atmel_mci_caps caps;
221 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
222 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
223 void (*stop_transfer)(struct atmel_mci *host);
227 * struct atmel_mci_slot - MMC slot state
228 * @mmc: The mmc_host representing this slot.
229 * @host: The MMC controller this slot is using.
230 * @sdc_reg: Value of SDCR to be written before using this slot.
231 * @sdio_irq: SDIO irq mask for this slot.
232 * @mrq: mmc_request currently being processed or waiting to be
233 * processed, or NULL when the slot is idle.
234 * @queue_node: List node for placing this node in the @queue list of
236 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
237 * @flags: Random state bits associated with the slot.
238 * @detect_pin: GPIO pin used for card detection, or negative if not
240 * @wp_pin: GPIO pin used for card write protect sending, or negative
242 * @detect_is_active_high: The state of the detect pin when it is active.
243 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
245 struct atmel_mci_slot {
246 struct mmc_host *mmc;
247 struct atmel_mci *host;
252 struct mmc_request *mrq;
253 struct list_head queue_node;
257 #define ATMCI_CARD_PRESENT 0
258 #define ATMCI_CARD_NEED_INIT 1
259 #define ATMCI_SHUTDOWN 2
260 #define ATMCI_SUSPENDED 3
264 bool detect_is_active_high;
266 struct timer_list detect_timer;
269 #define atmci_test_and_clear_pending(host, event) \
270 test_and_clear_bit(event, &host->pending_events)
271 #define atmci_set_completed(host, event) \
272 set_bit(event, &host->completed_events)
273 #define atmci_set_pending(host, event) \
274 set_bit(event, &host->pending_events)
277 * The debugfs stuff below is mostly optimized away when
278 * CONFIG_DEBUG_FS is not set.
280 static int atmci_req_show(struct seq_file *s, void *v)
282 struct atmel_mci_slot *slot = s->private;
283 struct mmc_request *mrq;
284 struct mmc_command *cmd;
285 struct mmc_command *stop;
286 struct mmc_data *data;
288 /* Make sure we get a consistent snapshot */
289 spin_lock_bh(&slot->host->lock);
299 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
300 cmd->opcode, cmd->arg, cmd->flags,
301 cmd->resp[0], cmd->resp[1], cmd->resp[2],
302 cmd->resp[3], cmd->error);
304 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
305 data->bytes_xfered, data->blocks,
306 data->blksz, data->flags, data->error);
309 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
310 stop->opcode, stop->arg, stop->flags,
311 stop->resp[0], stop->resp[1], stop->resp[2],
312 stop->resp[3], stop->error);
315 spin_unlock_bh(&slot->host->lock);
320 static int atmci_req_open(struct inode *inode, struct file *file)
322 return single_open(file, atmci_req_show, inode->i_private);
325 static const struct file_operations atmci_req_fops = {
326 .owner = THIS_MODULE,
327 .open = atmci_req_open,
330 .release = single_release,
333 static void atmci_show_status_reg(struct seq_file *s,
334 const char *regname, u32 value)
336 static const char *sr_bit[] = {
367 seq_printf(s, "%s:\t0x%08x", regname, value);
368 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
369 if (value & (1 << i)) {
371 seq_printf(s, " %s", sr_bit[i]);
373 seq_puts(s, " UNKNOWN");
379 static int atmci_regs_show(struct seq_file *s, void *v)
381 struct atmel_mci *host = s->private;
384 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
389 * Grab a more or less consistent snapshot. Note that we're
390 * not disabling interrupts, so IMR and SR may not be
393 spin_lock_bh(&host->lock);
394 clk_enable(host->mck);
395 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
396 clk_disable(host->mck);
397 spin_unlock_bh(&host->lock);
399 seq_printf(s, "MR:\t0x%08x%s%s ",
401 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
402 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
403 if (host->caps.has_odd_clk_div)
404 seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
405 ((buf[ATMCI_MR / 4] & 0xff) << 1)
406 | ((buf[ATMCI_MR / 4] >> 16) & 1));
408 seq_printf(s, "CLKDIV=%u\n",
409 (buf[ATMCI_MR / 4] & 0xff));
410 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
411 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
412 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
413 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
415 buf[ATMCI_BLKR / 4] & 0xffff,
416 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
417 if (host->caps.has_cstor_reg)
418 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
420 /* Don't read RSPR and RDR; it will consume the data there */
422 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
423 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
425 if (host->caps.has_dma_conf_reg) {
428 val = buf[ATMCI_DMA / 4];
429 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
432 1 << (((val >> 4) & 3) + 1) : 1,
433 val & ATMCI_DMAEN ? " DMAEN" : "");
435 if (host->caps.has_cfg_reg) {
438 val = buf[ATMCI_CFG / 4];
439 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
441 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
442 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
443 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
444 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
452 static int atmci_regs_open(struct inode *inode, struct file *file)
454 return single_open(file, atmci_regs_show, inode->i_private);
457 static const struct file_operations atmci_regs_fops = {
458 .owner = THIS_MODULE,
459 .open = atmci_regs_open,
462 .release = single_release,
465 static void atmci_init_debugfs(struct atmel_mci_slot *slot)
467 struct mmc_host *mmc = slot->mmc;
468 struct atmel_mci *host = slot->host;
472 root = mmc->debugfs_root;
476 node = debugfs_create_file("regs", S_IRUSR, root, host,
483 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
487 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
491 node = debugfs_create_x32("pending_events", S_IRUSR, root,
492 (u32 *)&host->pending_events);
496 node = debugfs_create_x32("completed_events", S_IRUSR, root,
497 (u32 *)&host->completed_events);
504 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
507 #if defined(CONFIG_OF)
508 static const struct of_device_id atmci_dt_ids[] = {
509 { .compatible = "atmel,hsmci" },
513 MODULE_DEVICE_TABLE(of, atmci_dt_ids);
515 static struct mci_platform_data*
516 atmci_of_init(struct platform_device *pdev)
518 struct device_node *np = pdev->dev.of_node;
519 struct device_node *cnp;
520 struct mci_platform_data *pdata;
524 dev_err(&pdev->dev, "device node not found\n");
525 return ERR_PTR(-EINVAL);
528 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
530 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
531 return ERR_PTR(-ENOMEM);
534 for_each_child_of_node(np, cnp) {
535 if (of_property_read_u32(cnp, "reg", &slot_id)) {
536 dev_warn(&pdev->dev, "reg property is missing for %s\n",
541 if (slot_id >= ATMCI_MAX_NR_SLOTS) {
542 dev_warn(&pdev->dev, "can't have more than %d slots\n",
547 if (of_property_read_u32(cnp, "bus-width",
548 &pdata->slot[slot_id].bus_width))
549 pdata->slot[slot_id].bus_width = 1;
551 pdata->slot[slot_id].detect_pin =
552 of_get_named_gpio(cnp, "cd-gpios", 0);
554 pdata->slot[slot_id].detect_is_active_high =
555 of_property_read_bool(cnp, "cd-inverted");
557 pdata->slot[slot_id].wp_pin =
558 of_get_named_gpio(cnp, "wp-gpios", 0);
563 #else /* CONFIG_OF */
564 static inline struct mci_platform_data*
565 atmci_of_init(struct platform_device *dev)
567 return ERR_PTR(-EINVAL);
571 static inline unsigned int atmci_get_version(struct atmel_mci *host)
573 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
576 static void atmci_timeout_timer(unsigned long data)
578 struct atmel_mci *host;
580 host = (struct atmel_mci *)data;
582 dev_dbg(&host->pdev->dev, "software timeout\n");
584 if (host->mrq->cmd->data) {
585 host->mrq->cmd->data->error = -ETIMEDOUT;
588 host->mrq->cmd->error = -ETIMEDOUT;
591 host->need_reset = 1;
592 host->state = STATE_END_REQUEST;
594 tasklet_schedule(&host->tasklet);
597 static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
601 * It is easier here to use us instead of ns for the timeout,
602 * it prevents from overflows during calculation.
604 unsigned int us = DIV_ROUND_UP(ns, 1000);
606 /* Maximum clock frequency is host->bus_hz/2 */
607 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
610 static void atmci_set_timeout(struct atmel_mci *host,
611 struct atmel_mci_slot *slot, struct mmc_data *data)
613 static unsigned dtomul_to_shift[] = {
614 0, 4, 7, 8, 10, 12, 16, 20
620 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
621 + data->timeout_clks;
623 for (dtomul = 0; dtomul < 8; dtomul++) {
624 unsigned shift = dtomul_to_shift[dtomul];
625 dtocyc = (timeout + (1 << shift) - 1) >> shift;
635 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
636 dtocyc << dtomul_to_shift[dtomul]);
637 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
641 * Return mask with command flags to be enabled for this command.
643 static u32 atmci_prepare_command(struct mmc_host *mmc,
644 struct mmc_command *cmd)
646 struct mmc_data *data;
649 cmd->error = -EINPROGRESS;
651 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
653 if (cmd->flags & MMC_RSP_PRESENT) {
654 if (cmd->flags & MMC_RSP_136)
655 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
657 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
661 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
662 * it's too difficult to determine whether this is an ACMD or
663 * not. Better make it 64.
665 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
667 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
668 cmdr |= ATMCI_CMDR_OPDCMD;
672 cmdr |= ATMCI_CMDR_START_XFER;
674 if (cmd->opcode == SD_IO_RW_EXTENDED) {
675 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
677 if (data->flags & MMC_DATA_STREAM)
678 cmdr |= ATMCI_CMDR_STREAM;
679 else if (data->blocks > 1)
680 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
682 cmdr |= ATMCI_CMDR_BLOCK;
685 if (data->flags & MMC_DATA_READ)
686 cmdr |= ATMCI_CMDR_TRDIR_READ;
692 static void atmci_send_command(struct atmel_mci *host,
693 struct mmc_command *cmd, u32 cmd_flags)
698 dev_vdbg(&host->pdev->dev,
699 "start command: ARGR=0x%08x CMDR=0x%08x\n",
700 cmd->arg, cmd_flags);
702 atmci_writel(host, ATMCI_ARGR, cmd->arg);
703 atmci_writel(host, ATMCI_CMDR, cmd_flags);
706 static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
708 dev_dbg(&host->pdev->dev, "send stop command\n");
709 atmci_send_command(host, data->stop, host->stop_cmdr);
710 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
714 * Configure given PDC buffer taking care of alignement issues.
715 * Update host->data_size and host->sg.
717 static void atmci_pdc_set_single_buf(struct atmel_mci *host,
718 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
720 u32 pointer_reg, counter_reg;
721 unsigned int buf_size;
723 if (dir == XFER_RECEIVE) {
724 pointer_reg = ATMEL_PDC_RPR;
725 counter_reg = ATMEL_PDC_RCR;
727 pointer_reg = ATMEL_PDC_TPR;
728 counter_reg = ATMEL_PDC_TCR;
731 if (buf_nb == PDC_SECOND_BUF) {
732 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
733 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
736 if (!host->caps.has_rwproof) {
737 buf_size = host->buf_size;
738 atmci_writel(host, pointer_reg, host->buf_phys_addr);
740 buf_size = sg_dma_len(host->sg);
741 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
744 if (host->data_size <= buf_size) {
745 if (host->data_size & 0x3) {
746 /* If size is different from modulo 4, transfer bytes */
747 atmci_writel(host, counter_reg, host->data_size);
748 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
750 /* Else transfer 32-bits words */
751 atmci_writel(host, counter_reg, host->data_size / 4);
755 /* We assume the size of a page is 32-bits aligned */
756 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
757 host->data_size -= sg_dma_len(host->sg);
759 host->sg = sg_next(host->sg);
764 * Configure PDC buffer according to the data size ie configuring one or two
765 * buffers. Don't use this function if you want to configure only the second
766 * buffer. In this case, use atmci_pdc_set_single_buf.
768 static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
770 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
772 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
776 * Unmap sg lists, called when transfer is finished.
778 static void atmci_pdc_cleanup(struct atmel_mci *host)
780 struct mmc_data *data = host->data;
783 dma_unmap_sg(&host->pdev->dev,
784 data->sg, data->sg_len,
785 ((data->flags & MMC_DATA_WRITE)
786 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
790 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
791 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
792 * interrupt needed for both transfer directions.
794 static void atmci_pdc_complete(struct atmel_mci *host)
796 int transfer_size = host->data->blocks * host->data->blksz;
799 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
801 if ((!host->caps.has_rwproof)
802 && (host->data->flags & MMC_DATA_READ)) {
803 if (host->caps.has_bad_data_ordering)
804 for (i = 0; i < transfer_size; i++)
805 host->buffer[i] = swab32(host->buffer[i]);
806 sg_copy_from_buffer(host->data->sg, host->data->sg_len,
807 host->buffer, transfer_size);
810 atmci_pdc_cleanup(host);
813 * If the card was removed, data will be NULL. No point trying
814 * to send the stop command or waiting for NBUSY in this case.
817 dev_dbg(&host->pdev->dev,
818 "(%s) set pending xfer complete\n", __func__);
819 atmci_set_pending(host, EVENT_XFER_COMPLETE);
820 tasklet_schedule(&host->tasklet);
824 static void atmci_dma_cleanup(struct atmel_mci *host)
826 struct mmc_data *data = host->data;
829 dma_unmap_sg(host->dma.chan->device->dev,
830 data->sg, data->sg_len,
831 ((data->flags & MMC_DATA_WRITE)
832 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
836 * This function is called by the DMA driver from tasklet context.
838 static void atmci_dma_complete(void *arg)
840 struct atmel_mci *host = arg;
841 struct mmc_data *data = host->data;
843 dev_vdbg(&host->pdev->dev, "DMA complete\n");
845 if (host->caps.has_dma_conf_reg)
846 /* Disable DMA hardware handshaking on MCI */
847 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
849 atmci_dma_cleanup(host);
852 * If the card was removed, data will be NULL. No point trying
853 * to send the stop command or waiting for NBUSY in this case.
856 dev_dbg(&host->pdev->dev,
857 "(%s) set pending xfer complete\n", __func__);
858 atmci_set_pending(host, EVENT_XFER_COMPLETE);
859 tasklet_schedule(&host->tasklet);
862 * Regardless of what the documentation says, we have
863 * to wait for NOTBUSY even after block read
866 * When the DMA transfer is complete, the controller
867 * may still be reading the CRC from the card, i.e.
868 * the data transfer is still in progress and we
869 * haven't seen all the potential error bits yet.
871 * The interrupt handler will schedule a different
872 * tasklet to finish things up when the data transfer
873 * is completely done.
875 * We may not complete the mmc request here anyway
876 * because the mmc layer may call back and cause us to
877 * violate the "don't submit new operations from the
878 * completion callback" rule of the dma engine
881 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
886 * Returns a mask of interrupt flags to be enabled after the whole
887 * request has been prepared.
889 static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
893 data->error = -EINPROGRESS;
896 host->sg_len = data->sg_len;
898 host->data_chan = NULL;
900 iflags = ATMCI_DATA_ERROR_FLAGS;
903 * Errata: MMC data write operation with less than 12
904 * bytes is impossible.
906 * Errata: MCI Transmit Data Register (TDR) FIFO
907 * corruption when length is not multiple of 4.
909 if (data->blocks * data->blksz < 12
910 || (data->blocks * data->blksz) & 3)
911 host->need_reset = true;
913 host->pio_offset = 0;
914 if (data->flags & MMC_DATA_READ)
915 iflags |= ATMCI_RXRDY;
917 iflags |= ATMCI_TXRDY;
923 * Set interrupt flags and set block length into the MCI mode register even
924 * if this value is also accessible in the MCI block register. It seems to be
925 * necessary before the High Speed MCI version. It also map sg and configure
929 atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
933 enum dma_data_direction dir;
936 data->error = -EINPROGRESS;
940 iflags = ATMCI_DATA_ERROR_FLAGS;
942 /* Enable pdc mode */
943 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
945 if (data->flags & MMC_DATA_READ) {
946 dir = DMA_FROM_DEVICE;
947 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
950 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
954 tmp = atmci_readl(host, ATMCI_MR);
956 tmp |= ATMCI_BLKLEN(data->blksz);
957 atmci_writel(host, ATMCI_MR, tmp);
960 host->data_size = data->blocks * data->blksz;
961 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
963 if ((!host->caps.has_rwproof)
964 && (host->data->flags & MMC_DATA_WRITE)) {
965 sg_copy_to_buffer(host->data->sg, host->data->sg_len,
966 host->buffer, host->data_size);
967 if (host->caps.has_bad_data_ordering)
968 for (i = 0; i < host->data_size; i++)
969 host->buffer[i] = swab32(host->buffer[i]);
973 atmci_pdc_set_both_buf(host,
974 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
980 atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
982 struct dma_chan *chan;
983 struct dma_async_tx_descriptor *desc;
984 struct scatterlist *sg;
986 enum dma_data_direction direction;
987 enum dma_transfer_direction slave_dirn;
992 data->error = -EINPROGRESS;
998 iflags = ATMCI_DATA_ERROR_FLAGS;
1001 * We don't do DMA on "complex" transfers, i.e. with
1002 * non-word-aligned buffers or lengths. Also, we don't bother
1003 * with all the DMA setup overhead for short transfers.
1005 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
1006 return atmci_prepare_data(host, data);
1007 if (data->blksz & 3)
1008 return atmci_prepare_data(host, data);
1010 for_each_sg(data->sg, sg, data->sg_len, i) {
1011 if (sg->offset & 3 || sg->length & 3)
1012 return atmci_prepare_data(host, data);
1015 /* If we don't have a channel, we can't do DMA */
1016 chan = host->dma.chan;
1018 host->data_chan = chan;
1023 if (data->flags & MMC_DATA_READ) {
1024 direction = DMA_FROM_DEVICE;
1025 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
1026 maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
1028 direction = DMA_TO_DEVICE;
1029 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
1030 maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
1033 if (host->caps.has_dma_conf_reg)
1034 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
1037 sglen = dma_map_sg(chan->device->dev, data->sg,
1038 data->sg_len, direction);
1040 dmaengine_slave_config(chan, &host->dma_conf);
1041 desc = dmaengine_prep_slave_sg(chan,
1042 data->sg, sglen, slave_dirn,
1043 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1047 host->dma.data_desc = desc;
1048 desc->callback = atmci_dma_complete;
1049 desc->callback_param = host;
1053 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
1058 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
1064 * Start PDC according to transfer direction.
1067 atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
1069 if (data->flags & MMC_DATA_READ)
1070 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
1072 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1076 atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
1078 struct dma_chan *chan = host->data_chan;
1079 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
1082 dmaengine_submit(desc);
1083 dma_async_issue_pending(chan);
1087 static void atmci_stop_transfer(struct atmel_mci *host)
1089 dev_dbg(&host->pdev->dev,
1090 "(%s) set pending xfer complete\n", __func__);
1091 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1092 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1096 * Stop data transfer because error(s) occurred.
1098 static void atmci_stop_transfer_pdc(struct atmel_mci *host)
1100 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
1103 static void atmci_stop_transfer_dma(struct atmel_mci *host)
1105 struct dma_chan *chan = host->data_chan;
1108 dmaengine_terminate_all(chan);
1109 atmci_dma_cleanup(host);
1111 /* Data transfer was stopped by the interrupt handler */
1112 dev_dbg(&host->pdev->dev,
1113 "(%s) set pending xfer complete\n", __func__);
1114 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1115 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1120 * Start a request: prepare data if needed, prepare the command and activate
1123 static void atmci_start_request(struct atmel_mci *host,
1124 struct atmel_mci_slot *slot)
1126 struct mmc_request *mrq;
1127 struct mmc_command *cmd;
1128 struct mmc_data *data;
1133 host->cur_slot = slot;
1136 host->pending_events = 0;
1137 host->completed_events = 0;
1138 host->cmd_status = 0;
1139 host->data_status = 0;
1141 dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
1143 if (host->need_reset || host->caps.need_reset_after_xfer) {
1144 iflags = atmci_readl(host, ATMCI_IMR);
1145 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
1146 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1147 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1148 atmci_writel(host, ATMCI_MR, host->mode_reg);
1149 if (host->caps.has_cfg_reg)
1150 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1151 atmci_writel(host, ATMCI_IER, iflags);
1152 host->need_reset = false;
1154 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
1156 iflags = atmci_readl(host, ATMCI_IMR);
1157 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
1158 dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
1161 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1162 /* Send init sequence (74 clock cycles) */
1163 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1164 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
1170 atmci_set_timeout(host, slot, data);
1172 /* Must set block count/size before sending command */
1173 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
1174 | ATMCI_BLKLEN(data->blksz));
1175 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
1176 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
1178 iflags |= host->prepare_data(host, data);
1181 iflags |= ATMCI_CMDRDY;
1183 cmdflags = atmci_prepare_command(slot->mmc, cmd);
1184 atmci_send_command(host, cmd, cmdflags);
1187 host->submit_data(host, data);
1190 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
1191 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
1192 if (!(data->flags & MMC_DATA_WRITE))
1193 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
1194 if (data->flags & MMC_DATA_STREAM)
1195 host->stop_cmdr |= ATMCI_CMDR_STREAM;
1197 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
1201 * We could have enabled interrupts earlier, but I suspect
1202 * that would open up a nice can of interesting race
1203 * conditions (e.g. command and data complete, but stop not
1206 atmci_writel(host, ATMCI_IER, iflags);
1208 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
1211 static void atmci_queue_request(struct atmel_mci *host,
1212 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1214 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1217 spin_lock_bh(&host->lock);
1219 if (host->state == STATE_IDLE) {
1220 host->state = STATE_SENDING_CMD;
1221 atmci_start_request(host, slot);
1223 dev_dbg(&host->pdev->dev, "queue request\n");
1224 list_add_tail(&slot->queue_node, &host->queue);
1226 spin_unlock_bh(&host->lock);
1229 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1231 struct atmel_mci_slot *slot = mmc_priv(mmc);
1232 struct atmel_mci *host = slot->host;
1233 struct mmc_data *data;
1236 dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
1239 * We may "know" the card is gone even though there's still an
1240 * electrical connection. If so, we really need to communicate
1241 * this to the MMC core since there won't be any more
1242 * interrupts as the card is completely removed. Otherwise,
1243 * the MMC core might believe the card is still there even
1244 * though the card was just removed very slowly.
1246 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1247 mrq->cmd->error = -ENOMEDIUM;
1248 mmc_request_done(mmc, mrq);
1252 /* We don't support multiple blocks of weird lengths. */
1254 if (data && data->blocks > 1 && data->blksz & 3) {
1255 mrq->cmd->error = -EINVAL;
1256 mmc_request_done(mmc, mrq);
1259 atmci_queue_request(host, slot, mrq);
1262 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1264 struct atmel_mci_slot *slot = mmc_priv(mmc);
1265 struct atmel_mci *host = slot->host;
1268 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
1269 switch (ios->bus_width) {
1270 case MMC_BUS_WIDTH_1:
1271 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
1273 case MMC_BUS_WIDTH_4:
1274 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
1279 unsigned int clock_min = ~0U;
1282 spin_lock_bh(&host->lock);
1283 if (!host->mode_reg) {
1284 clk_enable(host->mck);
1285 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1286 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1287 if (host->caps.has_cfg_reg)
1288 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1292 * Use mirror of ios->clock to prevent race with mmc
1293 * core ios update when finding the minimum.
1295 slot->clock = ios->clock;
1296 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1297 if (host->slot[i] && host->slot[i]->clock
1298 && host->slot[i]->clock < clock_min)
1299 clock_min = host->slot[i]->clock;
1302 /* Calculate clock divider */
1303 if (host->caps.has_odd_clk_div) {
1304 clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
1306 dev_warn(&mmc->class_dev,
1307 "clock %u too slow; using %lu\n",
1308 clock_min, host->bus_hz / (511 + 2));
1311 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
1312 | ATMCI_MR_CLKODD(clkdiv & 1);
1314 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1316 dev_warn(&mmc->class_dev,
1317 "clock %u too slow; using %lu\n",
1318 clock_min, host->bus_hz / (2 * 256));
1321 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
1325 * WRPROOF and RDPROOF prevent overruns/underruns by
1326 * stopping the clock when the FIFO is full/empty.
1327 * This state is not expected to last for long.
1329 if (host->caps.has_rwproof)
1330 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
1332 if (host->caps.has_cfg_reg) {
1333 /* setup High Speed mode in relation with card capacity */
1334 if (ios->timing == MMC_TIMING_SD_HS)
1335 host->cfg_reg |= ATMCI_CFG_HSMODE;
1337 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
1340 if (list_empty(&host->queue)) {
1341 atmci_writel(host, ATMCI_MR, host->mode_reg);
1342 if (host->caps.has_cfg_reg)
1343 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1345 host->need_clock_update = true;
1348 spin_unlock_bh(&host->lock);
1350 bool any_slot_active = false;
1352 spin_lock_bh(&host->lock);
1354 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1355 if (host->slot[i] && host->slot[i]->clock) {
1356 any_slot_active = true;
1360 if (!any_slot_active) {
1361 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
1362 if (host->mode_reg) {
1363 atmci_readl(host, ATMCI_MR);
1364 clk_disable(host->mck);
1368 spin_unlock_bh(&host->lock);
1371 switch (ios->power_mode) {
1373 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1377 * TODO: None of the currently available AVR32-based
1378 * boards allow MMC power to be turned off. Implement
1379 * power control when this can be tested properly.
1381 * We also need to hook this into the clock management
1382 * somehow so that newly inserted cards aren't
1383 * subjected to a fast clock before we have a chance
1384 * to figure out what the maximum rate is. Currently,
1385 * there's no way to avoid this, and there never will
1386 * be for boards that don't support power control.
1392 static int atmci_get_ro(struct mmc_host *mmc)
1394 int read_only = -ENOSYS;
1395 struct atmel_mci_slot *slot = mmc_priv(mmc);
1397 if (gpio_is_valid(slot->wp_pin)) {
1398 read_only = gpio_get_value(slot->wp_pin);
1399 dev_dbg(&mmc->class_dev, "card is %s\n",
1400 read_only ? "read-only" : "read-write");
1406 static int atmci_get_cd(struct mmc_host *mmc)
1408 int present = -ENOSYS;
1409 struct atmel_mci_slot *slot = mmc_priv(mmc);
1411 if (gpio_is_valid(slot->detect_pin)) {
1412 present = !(gpio_get_value(slot->detect_pin) ^
1413 slot->detect_is_active_high);
1414 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1415 present ? "" : "not ");
1421 static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1423 struct atmel_mci_slot *slot = mmc_priv(mmc);
1424 struct atmel_mci *host = slot->host;
1427 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
1429 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
1432 static const struct mmc_host_ops atmci_ops = {
1433 .request = atmci_request,
1434 .set_ios = atmci_set_ios,
1435 .get_ro = atmci_get_ro,
1436 .get_cd = atmci_get_cd,
1437 .enable_sdio_irq = atmci_enable_sdio_irq,
1440 /* Called with host->lock held */
1441 static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1442 __releases(&host->lock)
1443 __acquires(&host->lock)
1445 struct atmel_mci_slot *slot = NULL;
1446 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1448 WARN_ON(host->cmd || host->data);
1451 * Update the MMC clock rate if necessary. This may be
1452 * necessary if set_ios() is called when a different slot is
1453 * busy transferring data.
1455 if (host->need_clock_update) {
1456 atmci_writel(host, ATMCI_MR, host->mode_reg);
1457 if (host->caps.has_cfg_reg)
1458 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1461 host->cur_slot->mrq = NULL;
1463 if (!list_empty(&host->queue)) {
1464 slot = list_entry(host->queue.next,
1465 struct atmel_mci_slot, queue_node);
1466 list_del(&slot->queue_node);
1467 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1468 mmc_hostname(slot->mmc));
1469 host->state = STATE_SENDING_CMD;
1470 atmci_start_request(host, slot);
1472 dev_vdbg(&host->pdev->dev, "list empty\n");
1473 host->state = STATE_IDLE;
1476 del_timer(&host->timer);
1478 spin_unlock(&host->lock);
1479 mmc_request_done(prev_mmc, mrq);
1480 spin_lock(&host->lock);
1483 static void atmci_command_complete(struct atmel_mci *host,
1484 struct mmc_command *cmd)
1486 u32 status = host->cmd_status;
1488 /* Read the response from the card (up to 16 bytes) */
1489 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1490 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1491 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1492 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
1494 if (status & ATMCI_RTOE)
1495 cmd->error = -ETIMEDOUT;
1496 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
1497 cmd->error = -EILSEQ;
1498 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
1500 else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
1501 if (host->caps.need_blksz_mul_4) {
1502 cmd->error = -EINVAL;
1503 host->need_reset = 1;
1509 static void atmci_detect_change(unsigned long data)
1511 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1516 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1517 * freeing the interrupt. We must not re-enable the interrupt
1518 * if it has been freed, and if we're shutting down, it
1519 * doesn't really matter whether the card is present or not.
1522 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
1525 enable_irq(gpio_to_irq(slot->detect_pin));
1526 present = !(gpio_get_value(slot->detect_pin) ^
1527 slot->detect_is_active_high);
1528 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
1530 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1531 present, present_old);
1533 if (present != present_old) {
1534 struct atmel_mci *host = slot->host;
1535 struct mmc_request *mrq;
1537 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1538 present ? "inserted" : "removed");
1540 spin_lock(&host->lock);
1543 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1545 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1547 /* Clean up queue if present */
1550 if (mrq == host->mrq) {
1552 * Reset controller to terminate any ongoing
1553 * commands or data transfers.
1555 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1556 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1557 atmci_writel(host, ATMCI_MR, host->mode_reg);
1558 if (host->caps.has_cfg_reg)
1559 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1564 switch (host->state) {
1567 case STATE_SENDING_CMD:
1568 mrq->cmd->error = -ENOMEDIUM;
1570 host->stop_transfer(host);
1572 case STATE_DATA_XFER:
1573 mrq->data->error = -ENOMEDIUM;
1574 host->stop_transfer(host);
1576 case STATE_WAITING_NOTBUSY:
1577 mrq->data->error = -ENOMEDIUM;
1579 case STATE_SENDING_STOP:
1580 mrq->stop->error = -ENOMEDIUM;
1582 case STATE_END_REQUEST:
1586 atmci_request_end(host, mrq);
1588 list_del(&slot->queue_node);
1589 mrq->cmd->error = -ENOMEDIUM;
1591 mrq->data->error = -ENOMEDIUM;
1593 mrq->stop->error = -ENOMEDIUM;
1595 spin_unlock(&host->lock);
1596 mmc_request_done(slot->mmc, mrq);
1597 spin_lock(&host->lock);
1600 spin_unlock(&host->lock);
1602 mmc_detect_change(slot->mmc, 0);
1606 static void atmci_tasklet_func(unsigned long priv)
1608 struct atmel_mci *host = (struct atmel_mci *)priv;
1609 struct mmc_request *mrq = host->mrq;
1610 struct mmc_data *data = host->data;
1611 enum atmel_mci_state state = host->state;
1612 enum atmel_mci_state prev_state;
1615 spin_lock(&host->lock);
1617 state = host->state;
1619 dev_vdbg(&host->pdev->dev,
1620 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1621 state, host->pending_events, host->completed_events,
1622 atmci_readl(host, ATMCI_IMR));
1626 dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
1632 case STATE_SENDING_CMD:
1634 * Command has been sent, we are waiting for command
1635 * ready. Then we have three next states possible:
1636 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1637 * command needing it or DATA_XFER if there is data.
1639 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1640 if (!atmci_test_and_clear_pending(host,
1644 dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
1646 atmci_set_completed(host, EVENT_CMD_RDY);
1647 atmci_command_complete(host, mrq->cmd);
1649 dev_dbg(&host->pdev->dev,
1650 "command with data transfer");
1652 * If there is a command error don't start
1655 if (mrq->cmd->error) {
1656 host->stop_transfer(host);
1658 atmci_writel(host, ATMCI_IDR,
1659 ATMCI_TXRDY | ATMCI_RXRDY
1660 | ATMCI_DATA_ERROR_FLAGS);
1661 state = STATE_END_REQUEST;
1663 state = STATE_DATA_XFER;
1664 } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
1665 dev_dbg(&host->pdev->dev,
1666 "command response need waiting notbusy");
1667 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1668 state = STATE_WAITING_NOTBUSY;
1670 state = STATE_END_REQUEST;
1674 case STATE_DATA_XFER:
1675 if (atmci_test_and_clear_pending(host,
1676 EVENT_DATA_ERROR)) {
1677 dev_dbg(&host->pdev->dev, "set completed data error\n");
1678 atmci_set_completed(host, EVENT_DATA_ERROR);
1679 state = STATE_END_REQUEST;
1684 * A data transfer is in progress. The event expected
1685 * to move to the next state depends of data transfer
1686 * type (PDC or DMA). Once transfer done we can move
1687 * to the next step which is WAITING_NOTBUSY in write
1688 * case and directly SENDING_STOP in read case.
1690 dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
1691 if (!atmci_test_and_clear_pending(host,
1692 EVENT_XFER_COMPLETE))
1695 dev_dbg(&host->pdev->dev,
1696 "(%s) set completed xfer complete\n",
1698 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1700 if (host->caps.need_notbusy_for_read_ops ||
1701 (host->data->flags & MMC_DATA_WRITE)) {
1702 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1703 state = STATE_WAITING_NOTBUSY;
1704 } else if (host->mrq->stop) {
1705 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
1706 atmci_send_stop_cmd(host, data);
1707 state = STATE_SENDING_STOP;
1710 data->bytes_xfered = data->blocks * data->blksz;
1712 state = STATE_END_REQUEST;
1716 case STATE_WAITING_NOTBUSY:
1718 * We can be in the state for two reasons: a command
1719 * requiring waiting not busy signal (stop command
1720 * included) or a write operation. In the latest case,
1721 * we need to send a stop command.
1723 dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
1724 if (!atmci_test_and_clear_pending(host,
1728 dev_dbg(&host->pdev->dev, "set completed not busy\n");
1729 atmci_set_completed(host, EVENT_NOTBUSY);
1733 * For some commands such as CMD53, even if
1734 * there is data transfer, there is no stop
1737 if (host->mrq->stop) {
1738 atmci_writel(host, ATMCI_IER,
1740 atmci_send_stop_cmd(host, data);
1741 state = STATE_SENDING_STOP;
1744 data->bytes_xfered = data->blocks
1747 state = STATE_END_REQUEST;
1750 state = STATE_END_REQUEST;
1753 case STATE_SENDING_STOP:
1755 * In this state, it is important to set host->data to
1756 * NULL (which is tested in the waiting notbusy state)
1757 * in order to go to the end request state instead of
1758 * sending stop again.
1760 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1761 if (!atmci_test_and_clear_pending(host,
1765 dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
1767 data->bytes_xfered = data->blocks * data->blksz;
1769 atmci_command_complete(host, mrq->stop);
1770 if (mrq->stop->error) {
1771 host->stop_transfer(host);
1772 atmci_writel(host, ATMCI_IDR,
1773 ATMCI_TXRDY | ATMCI_RXRDY
1774 | ATMCI_DATA_ERROR_FLAGS);
1775 state = STATE_END_REQUEST;
1777 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1778 state = STATE_WAITING_NOTBUSY;
1783 case STATE_END_REQUEST:
1784 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
1785 | ATMCI_DATA_ERROR_FLAGS);
1786 status = host->data_status;
1787 if (unlikely(status)) {
1788 host->stop_transfer(host);
1790 if (status & ATMCI_DTOE) {
1791 data->error = -ETIMEDOUT;
1792 } else if (status & ATMCI_DCRCE) {
1793 data->error = -EILSEQ;
1799 atmci_request_end(host, host->mrq);
1803 } while (state != prev_state);
1805 host->state = state;
1807 spin_unlock(&host->lock);
1810 static void atmci_read_data_pio(struct atmel_mci *host)
1812 struct scatterlist *sg = host->sg;
1813 void *buf = sg_virt(sg);
1814 unsigned int offset = host->pio_offset;
1815 struct mmc_data *data = host->data;
1818 unsigned int nbytes = 0;
1821 value = atmci_readl(host, ATMCI_RDR);
1822 if (likely(offset + 4 <= sg->length)) {
1823 put_unaligned(value, (u32 *)(buf + offset));
1828 if (offset == sg->length) {
1829 flush_dcache_page(sg_page(sg));
1830 host->sg = sg = sg_next(sg);
1832 if (!sg || !host->sg_len)
1839 unsigned int remaining = sg->length - offset;
1840 memcpy(buf + offset, &value, remaining);
1841 nbytes += remaining;
1843 flush_dcache_page(sg_page(sg));
1844 host->sg = sg = sg_next(sg);
1846 if (!sg || !host->sg_len)
1849 offset = 4 - remaining;
1851 memcpy(buf, (u8 *)&value + remaining, offset);
1855 status = atmci_readl(host, ATMCI_SR);
1856 if (status & ATMCI_DATA_ERROR_FLAGS) {
1857 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
1858 | ATMCI_DATA_ERROR_FLAGS));
1859 host->data_status = status;
1860 data->bytes_xfered += nbytes;
1863 } while (status & ATMCI_RXRDY);
1865 host->pio_offset = offset;
1866 data->bytes_xfered += nbytes;
1871 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1872 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1873 data->bytes_xfered += nbytes;
1875 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1878 static void atmci_write_data_pio(struct atmel_mci *host)
1880 struct scatterlist *sg = host->sg;
1881 void *buf = sg_virt(sg);
1882 unsigned int offset = host->pio_offset;
1883 struct mmc_data *data = host->data;
1886 unsigned int nbytes = 0;
1889 if (likely(offset + 4 <= sg->length)) {
1890 value = get_unaligned((u32 *)(buf + offset));
1891 atmci_writel(host, ATMCI_TDR, value);
1895 if (offset == sg->length) {
1896 host->sg = sg = sg_next(sg);
1898 if (!sg || !host->sg_len)
1905 unsigned int remaining = sg->length - offset;
1908 memcpy(&value, buf + offset, remaining);
1909 nbytes += remaining;
1911 host->sg = sg = sg_next(sg);
1913 if (!sg || !host->sg_len) {
1914 atmci_writel(host, ATMCI_TDR, value);
1918 offset = 4 - remaining;
1920 memcpy((u8 *)&value + remaining, buf, offset);
1921 atmci_writel(host, ATMCI_TDR, value);
1925 status = atmci_readl(host, ATMCI_SR);
1926 if (status & ATMCI_DATA_ERROR_FLAGS) {
1927 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
1928 | ATMCI_DATA_ERROR_FLAGS));
1929 host->data_status = status;
1930 data->bytes_xfered += nbytes;
1933 } while (status & ATMCI_TXRDY);
1935 host->pio_offset = offset;
1936 data->bytes_xfered += nbytes;
1941 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1942 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1943 data->bytes_xfered += nbytes;
1945 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1948 static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1952 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1953 struct atmel_mci_slot *slot = host->slot[i];
1954 if (slot && (status & slot->sdio_irq)) {
1955 mmc_signal_sdio_irq(slot->mmc);
1961 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1963 struct atmel_mci *host = dev_id;
1964 u32 status, mask, pending;
1965 unsigned int pass_count = 0;
1968 status = atmci_readl(host, ATMCI_SR);
1969 mask = atmci_readl(host, ATMCI_IMR);
1970 pending = status & mask;
1974 if (pending & ATMCI_DATA_ERROR_FLAGS) {
1975 dev_dbg(&host->pdev->dev, "IRQ: data error\n");
1976 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
1977 | ATMCI_RXRDY | ATMCI_TXRDY
1978 | ATMCI_ENDRX | ATMCI_ENDTX
1979 | ATMCI_RXBUFF | ATMCI_TXBUFE);
1981 host->data_status = status;
1982 dev_dbg(&host->pdev->dev, "set pending data error\n");
1984 atmci_set_pending(host, EVENT_DATA_ERROR);
1985 tasklet_schedule(&host->tasklet);
1988 if (pending & ATMCI_TXBUFE) {
1989 dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
1990 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
1991 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1993 * We can receive this interruption before having configured
1994 * the second pdc buffer, so we need to reconfigure first and
1995 * second buffers again
1997 if (host->data_size) {
1998 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
1999 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2000 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
2002 atmci_pdc_complete(host);
2004 } else if (pending & ATMCI_ENDTX) {
2005 dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
2006 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
2008 if (host->data_size) {
2009 atmci_pdc_set_single_buf(host,
2010 XFER_TRANSMIT, PDC_SECOND_BUF);
2011 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2015 if (pending & ATMCI_RXBUFF) {
2016 dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
2017 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
2018 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2020 * We can receive this interruption before having configured
2021 * the second pdc buffer, so we need to reconfigure first and
2022 * second buffers again
2024 if (host->data_size) {
2025 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
2026 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2027 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
2029 atmci_pdc_complete(host);
2031 } else if (pending & ATMCI_ENDRX) {
2032 dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
2033 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2035 if (host->data_size) {
2036 atmci_pdc_set_single_buf(host,
2037 XFER_RECEIVE, PDC_SECOND_BUF);
2038 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2043 * First mci IPs, so mainly the ones having pdc, have some
2044 * issues with the notbusy signal. You can't get it after
2045 * data transmission if you have not sent a stop command.
2046 * The appropriate workaround is to use the BLKE signal.
2048 if (pending & ATMCI_BLKE) {
2049 dev_dbg(&host->pdev->dev, "IRQ: blke\n");
2050 atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
2052 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
2053 atmci_set_pending(host, EVENT_NOTBUSY);
2054 tasklet_schedule(&host->tasklet);
2057 if (pending & ATMCI_NOTBUSY) {
2058 dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
2059 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
2061 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
2062 atmci_set_pending(host, EVENT_NOTBUSY);
2063 tasklet_schedule(&host->tasklet);
2066 if (pending & ATMCI_RXRDY)
2067 atmci_read_data_pio(host);
2068 if (pending & ATMCI_TXRDY)
2069 atmci_write_data_pio(host);
2071 if (pending & ATMCI_CMDRDY) {
2072 dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
2073 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
2074 host->cmd_status = status;
2076 dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
2077 atmci_set_pending(host, EVENT_CMD_RDY);
2078 tasklet_schedule(&host->tasklet);
2081 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
2082 atmci_sdio_interrupt(host, status);
2084 } while (pass_count++ < 5);
2086 return pass_count ? IRQ_HANDLED : IRQ_NONE;
2089 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
2091 struct atmel_mci_slot *slot = dev_id;
2094 * Disable interrupts until the pin has stabilized and check
2095 * the state then. Use mod_timer() since we may be in the
2096 * middle of the timer routine when this interrupt triggers.
2098 disable_irq_nosync(irq);
2099 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
2104 static int __init atmci_init_slot(struct atmel_mci *host,
2105 struct mci_slot_pdata *slot_data, unsigned int id,
2106 u32 sdc_reg, u32 sdio_irq)
2108 struct mmc_host *mmc;
2109 struct atmel_mci_slot *slot;
2111 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
2115 slot = mmc_priv(mmc);
2118 slot->detect_pin = slot_data->detect_pin;
2119 slot->wp_pin = slot_data->wp_pin;
2120 slot->detect_is_active_high = slot_data->detect_is_active_high;
2121 slot->sdc_reg = sdc_reg;
2122 slot->sdio_irq = sdio_irq;
2124 dev_dbg(&mmc->class_dev,
2125 "slot[%u]: bus_width=%u, detect_pin=%d, "
2126 "detect_is_active_high=%s, wp_pin=%d\n",
2127 id, slot_data->bus_width, slot_data->detect_pin,
2128 slot_data->detect_is_active_high ? "true" : "false",
2131 mmc->ops = &atmci_ops;
2132 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
2133 mmc->f_max = host->bus_hz / 2;
2134 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2136 mmc->caps |= MMC_CAP_SDIO_IRQ;
2137 if (host->caps.has_highspeed)
2138 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2140 * Without the read/write proof capability, it is strongly suggested to
2141 * use only one bit for data to prevent fifo underruns and overruns
2142 * which will corrupt data.
2144 if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
2145 mmc->caps |= MMC_CAP_4_BIT_DATA;
2147 if (atmci_get_version(host) < 0x200) {
2148 mmc->max_segs = 256;
2149 mmc->max_blk_size = 4095;
2150 mmc->max_blk_count = 256;
2151 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2152 mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
2155 mmc->max_req_size = 32768 * 512;
2156 mmc->max_blk_size = 32768;
2157 mmc->max_blk_count = 512;
2160 /* Assume card is present initially */
2161 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
2162 if (gpio_is_valid(slot->detect_pin)) {
2163 if (gpio_request(slot->detect_pin, "mmc_detect")) {
2164 dev_dbg(&mmc->class_dev, "no detect pin available\n");
2165 slot->detect_pin = -EBUSY;
2166 } else if (gpio_get_value(slot->detect_pin) ^
2167 slot->detect_is_active_high) {
2168 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
2172 if (!gpio_is_valid(slot->detect_pin))
2173 mmc->caps |= MMC_CAP_NEEDS_POLL;
2175 if (gpio_is_valid(slot->wp_pin)) {
2176 if (gpio_request(slot->wp_pin, "mmc_wp")) {
2177 dev_dbg(&mmc->class_dev, "no WP pin available\n");
2178 slot->wp_pin = -EBUSY;
2182 host->slot[id] = slot;
2185 if (gpio_is_valid(slot->detect_pin)) {
2188 setup_timer(&slot->detect_timer, atmci_detect_change,
2189 (unsigned long)slot);
2191 ret = request_irq(gpio_to_irq(slot->detect_pin),
2192 atmci_detect_interrupt,
2193 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2194 "mmc-detect", slot);
2196 dev_dbg(&mmc->class_dev,
2197 "could not request IRQ %d for detect pin\n",
2198 gpio_to_irq(slot->detect_pin));
2199 gpio_free(slot->detect_pin);
2200 slot->detect_pin = -EBUSY;
2204 atmci_init_debugfs(slot);
2209 static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
2212 /* Debugfs stuff is cleaned up by mmc core */
2214 set_bit(ATMCI_SHUTDOWN, &slot->flags);
2217 mmc_remove_host(slot->mmc);
2219 if (gpio_is_valid(slot->detect_pin)) {
2220 int pin = slot->detect_pin;
2222 free_irq(gpio_to_irq(pin), slot);
2223 del_timer_sync(&slot->detect_timer);
2226 if (gpio_is_valid(slot->wp_pin))
2227 gpio_free(slot->wp_pin);
2229 slot->host->slot[id] = NULL;
2230 mmc_free_host(slot->mmc);
2233 static bool atmci_filter(struct dma_chan *chan, void *pdata)
2235 struct mci_platform_data *sl_pdata = pdata;
2236 struct mci_dma_data *sl;
2241 sl = sl_pdata->dma_slave;
2242 if (sl && find_slave_dev(sl) == chan->device->dev) {
2243 chan->private = slave_data_ptr(sl);
2250 static bool atmci_configure_dma(struct atmel_mci *host)
2252 struct mci_platform_data *pdata;
2253 dma_cap_mask_t mask;
2258 pdata = host->pdev->dev.platform_data;
2261 dma_cap_set(DMA_SLAVE, mask);
2263 host->dma.chan = dma_request_slave_channel_compat(mask, atmci_filter, pdata,
2264 &host->pdev->dev, "rxtx");
2265 if (!host->dma.chan) {
2266 dev_warn(&host->pdev->dev, "no DMA channel available\n");
2269 dev_info(&host->pdev->dev,
2270 "using %s for DMA transfers\n",
2271 dma_chan_name(host->dma.chan));
2273 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
2274 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2275 host->dma_conf.src_maxburst = 1;
2276 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
2277 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2278 host->dma_conf.dst_maxburst = 1;
2279 host->dma_conf.device_fc = false;
2285 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2286 * HSMCI provides DMA support and a new config register but no more supports
2289 static void __init atmci_get_cap(struct atmel_mci *host)
2291 unsigned int version;
2293 version = atmci_get_version(host);
2294 dev_info(&host->pdev->dev,
2295 "version: 0x%x\n", version);
2297 host->caps.has_dma_conf_reg = 0;
2298 host->caps.has_pdc = ATMCI_PDC_CONNECTED;
2299 host->caps.has_cfg_reg = 0;
2300 host->caps.has_cstor_reg = 0;
2301 host->caps.has_highspeed = 0;
2302 host->caps.has_rwproof = 0;
2303 host->caps.has_odd_clk_div = 0;
2304 host->caps.has_bad_data_ordering = 1;
2305 host->caps.need_reset_after_xfer = 1;
2306 host->caps.need_blksz_mul_4 = 1;
2307 host->caps.need_notbusy_for_read_ops = 0;
2309 /* keep only major version number */
2310 switch (version & 0xf00) {
2312 host->caps.has_odd_clk_div = 1;
2315 host->caps.has_dma_conf_reg = 1;
2316 host->caps.has_pdc = 0;
2317 host->caps.has_cfg_reg = 1;
2318 host->caps.has_cstor_reg = 1;
2319 host->caps.has_highspeed = 1;
2321 host->caps.has_rwproof = 1;
2322 host->caps.need_blksz_mul_4 = 0;
2323 host->caps.need_notbusy_for_read_ops = 1;
2325 host->caps.has_bad_data_ordering = 0;
2326 host->caps.need_reset_after_xfer = 0;
2330 host->caps.has_pdc = 0;
2331 dev_warn(&host->pdev->dev,
2332 "Unmanaged mci version, set minimum capabilities\n");
2337 static int __init atmci_probe(struct platform_device *pdev)
2339 struct mci_platform_data *pdata;
2340 struct atmel_mci *host;
2341 struct resource *regs;
2342 unsigned int nr_slots;
2346 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2349 pdata = pdev->dev.platform_data;
2351 pdata = atmci_of_init(pdev);
2352 if (IS_ERR(pdata)) {
2353 dev_err(&pdev->dev, "platform data not available\n");
2354 return PTR_ERR(pdata);
2358 irq = platform_get_irq(pdev, 0);
2362 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2367 spin_lock_init(&host->lock);
2368 INIT_LIST_HEAD(&host->queue);
2370 host->mck = clk_get(&pdev->dev, "mci_clk");
2371 if (IS_ERR(host->mck)) {
2372 ret = PTR_ERR(host->mck);
2377 host->regs = ioremap(regs->start, resource_size(regs));
2381 clk_enable(host->mck);
2382 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
2383 host->bus_hz = clk_get_rate(host->mck);
2384 clk_disable(host->mck);
2386 host->mapbase = regs->start;
2388 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
2390 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
2392 goto err_request_irq;
2394 /* Get MCI capabilities and set operations according to it */
2395 atmci_get_cap(host);
2396 if (atmci_configure_dma(host)) {
2397 host->prepare_data = &atmci_prepare_data_dma;
2398 host->submit_data = &atmci_submit_data_dma;
2399 host->stop_transfer = &atmci_stop_transfer_dma;
2400 } else if (host->caps.has_pdc) {
2401 dev_info(&pdev->dev, "using PDC\n");
2402 host->prepare_data = &atmci_prepare_data_pdc;
2403 host->submit_data = &atmci_submit_data_pdc;
2404 host->stop_transfer = &atmci_stop_transfer_pdc;
2406 dev_info(&pdev->dev, "using PIO\n");
2407 host->prepare_data = &atmci_prepare_data;
2408 host->submit_data = &atmci_submit_data;
2409 host->stop_transfer = &atmci_stop_transfer;
2412 platform_set_drvdata(pdev, host);
2414 setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
2416 /* We need at least one slot to succeed */
2419 if (pdata->slot[0].bus_width) {
2420 ret = atmci_init_slot(host, &pdata->slot[0],
2421 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
2424 host->buf_size = host->slot[0]->mmc->max_req_size;
2427 if (pdata->slot[1].bus_width) {
2428 ret = atmci_init_slot(host, &pdata->slot[1],
2429 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
2432 if (host->slot[1]->mmc->max_req_size > host->buf_size)
2434 host->slot[1]->mmc->max_req_size;
2439 dev_err(&pdev->dev, "init failed: no slot defined\n");
2443 if (!host->caps.has_rwproof) {
2444 host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
2445 &host->buf_phys_addr,
2447 if (!host->buffer) {
2449 dev_err(&pdev->dev, "buffer allocation failed\n");
2454 dev_info(&pdev->dev,
2455 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2456 host->mapbase, irq, nr_slots);
2462 dma_release_channel(host->dma.chan);
2463 free_irq(irq, host);
2465 iounmap(host->regs);
2473 static int __exit atmci_remove(struct platform_device *pdev)
2475 struct atmel_mci *host = platform_get_drvdata(pdev);
2478 platform_set_drvdata(pdev, NULL);
2481 dma_free_coherent(&pdev->dev, host->buf_size,
2482 host->buffer, host->buf_phys_addr);
2484 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2486 atmci_cleanup_slot(host->slot[i], i);
2489 clk_enable(host->mck);
2490 atmci_writel(host, ATMCI_IDR, ~0UL);
2491 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2492 atmci_readl(host, ATMCI_SR);
2493 clk_disable(host->mck);
2496 dma_release_channel(host->dma.chan);
2498 free_irq(platform_get_irq(pdev, 0), host);
2499 iounmap(host->regs);
2508 static int atmci_suspend(struct device *dev)
2510 struct atmel_mci *host = dev_get_drvdata(dev);
2513 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2514 struct atmel_mci_slot *slot = host->slot[i];
2519 ret = mmc_suspend_host(slot->mmc);
2522 slot = host->slot[i];
2524 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2525 mmc_resume_host(host->slot[i]->mmc);
2526 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2531 set_bit(ATMCI_SUSPENDED, &slot->flags);
2538 static int atmci_resume(struct device *dev)
2540 struct atmel_mci *host = dev_get_drvdata(dev);
2544 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2545 struct atmel_mci_slot *slot = host->slot[i];
2548 slot = host->slot[i];
2551 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2553 err = mmc_resume_host(slot->mmc);
2557 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2562 static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2563 #define ATMCI_PM_OPS (&atmci_pm)
2565 #define ATMCI_PM_OPS NULL
2568 static struct platform_driver atmci_driver = {
2569 .remove = __exit_p(atmci_remove),
2571 .name = "atmel_mci",
2573 .of_match_table = of_match_ptr(atmci_dt_ids),
2577 static int __init atmci_init(void)
2579 return platform_driver_probe(&atmci_driver, atmci_probe);
2582 static void __exit atmci_exit(void)
2584 platform_driver_unregister(&atmci_driver);
2587 late_initcall(atmci_init); /* try to load after dma driver when built-in */
2588 module_exit(atmci_exit);
2590 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
2591 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2592 MODULE_LICENSE("GPL v2");