2 * Atmel MultiMedia Card Interface driver
4 * Copyright (C) 2004-2008 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/blkdev.h>
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/scatterlist.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/stat.h>
27 #include <linux/types.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/sdio.h>
32 #include <mach/atmel-mci.h>
33 #include <linux/atmel-mci.h>
34 #include <linux/atmel_pdc.h>
37 #include <asm/unaligned.h>
40 #include <mach/board.h>
42 #include "atmel-mci-regs.h"
44 #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
45 #define ATMCI_DMA_THRESHOLD 16
54 enum atmel_mci_state
{
58 STATE_WAITING_NOTBUSY
,
73 struct atmel_mci_caps
{
81 bool has_bad_data_ordering
;
82 bool need_reset_after_xfer
;
83 bool need_blksz_mul_4
;
86 struct atmel_mci_dma
{
87 struct dma_chan
*chan
;
88 struct dma_async_tx_descriptor
*data_desc
;
92 * struct atmel_mci - MMC controller state shared between all slots
93 * @lock: Spinlock protecting the queue and associated data.
94 * @regs: Pointer to MMIO registers.
95 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
96 * @pio_offset: Offset into the current scatterlist entry.
97 * @buffer: Buffer used if we don't have the r/w proof capability. We
98 * don't have the time to switch pdc buffers so we have to use only
99 * one buffer for the full transaction.
100 * @buf_size: size of the buffer.
101 * @phys_buf_addr: buffer address needed for pdc.
102 * @cur_slot: The slot which is currently using the controller.
103 * @mrq: The request currently being processed on @cur_slot,
104 * or NULL if the controller is idle.
105 * @cmd: The command currently being sent to the card, or NULL.
106 * @data: The data currently being transferred, or NULL if no data
107 * transfer is in progress.
108 * @data_size: just data->blocks * data->blksz.
109 * @dma: DMA client state.
110 * @data_chan: DMA channel being used for the current data transfer.
111 * @cmd_status: Snapshot of SR taken upon completion of the current
112 * command. Only valid when EVENT_CMD_COMPLETE is pending.
113 * @data_status: Snapshot of SR taken upon completion of the current
114 * data transfer. Only valid when EVENT_DATA_COMPLETE or
115 * EVENT_DATA_ERROR is pending.
116 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
118 * @tasklet: Tasklet running the request state machine.
119 * @pending_events: Bitmask of events flagged by the interrupt handler
120 * to be processed by the tasklet.
121 * @completed_events: Bitmask of events which the state machine has
123 * @state: Tasklet state.
124 * @queue: List of slots waiting for access to the controller.
125 * @need_clock_update: Update the clock rate before the next request.
126 * @need_reset: Reset controller before next request.
127 * @timer: Timer to balance the data timeout error flag which cannot rise.
128 * @mode_reg: Value of the MR register.
129 * @cfg_reg: Value of the CFG register.
130 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
131 * rate and timeout calculations.
132 * @mapbase: Physical address of the MMIO registers.
133 * @mck: The peripheral bus clock hooked up to the MMC controller.
134 * @pdev: Platform device associated with the MMC controller.
135 * @slot: Slots sharing this MMC controller.
136 * @caps: MCI capabilities depending on MCI version.
137 * @prepare_data: function to setup MCI before data transfer which
138 * depends on MCI capabilities.
139 * @submit_data: function to start data transfer which depends on MCI
141 * @stop_transfer: function to stop data transfer which depends on MCI
147 * @lock is a softirq-safe spinlock protecting @queue as well as
148 * @cur_slot, @mrq and @state. These must always be updated
149 * at the same time while holding @lock.
151 * @lock also protects mode_reg and need_clock_update since these are
152 * used to synchronize mode register updates with the queue
155 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
156 * and must always be written at the same time as the slot is added to
159 * @pending_events and @completed_events are accessed using atomic bit
160 * operations, so they don't need any locking.
162 * None of the fields touched by the interrupt handler need any
163 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
164 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
165 * interrupts must be disabled and @data_status updated with a
166 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
167 * CMDRDY interrupt must be disabled and @cmd_status updated with a
168 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
169 * bytes_xfered field of @data must be written. This is ensured by
176 struct scatterlist
*sg
;
177 unsigned int pio_offset
;
178 unsigned int *buffer
;
179 unsigned int buf_size
;
180 dma_addr_t buf_phys_addr
;
182 struct atmel_mci_slot
*cur_slot
;
183 struct mmc_request
*mrq
;
184 struct mmc_command
*cmd
;
185 struct mmc_data
*data
;
186 unsigned int data_size
;
188 struct atmel_mci_dma dma
;
189 struct dma_chan
*data_chan
;
190 struct dma_slave_config dma_conf
;
196 struct tasklet_struct tasklet
;
197 unsigned long pending_events
;
198 unsigned long completed_events
;
199 enum atmel_mci_state state
;
200 struct list_head queue
;
202 bool need_clock_update
;
204 struct timer_list timer
;
207 unsigned long bus_hz
;
208 unsigned long mapbase
;
210 struct platform_device
*pdev
;
212 struct atmel_mci_slot
*slot
[ATMCI_MAX_NR_SLOTS
];
214 struct atmel_mci_caps caps
;
216 u32 (*prepare_data
)(struct atmel_mci
*host
, struct mmc_data
*data
);
217 void (*submit_data
)(struct atmel_mci
*host
, struct mmc_data
*data
);
218 void (*stop_transfer
)(struct atmel_mci
*host
);
222 * struct atmel_mci_slot - MMC slot state
223 * @mmc: The mmc_host representing this slot.
224 * @host: The MMC controller this slot is using.
225 * @sdc_reg: Value of SDCR to be written before using this slot.
226 * @sdio_irq: SDIO irq mask for this slot.
227 * @mrq: mmc_request currently being processed or waiting to be
228 * processed, or NULL when the slot is idle.
229 * @queue_node: List node for placing this node in the @queue list of
231 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
232 * @flags: Random state bits associated with the slot.
233 * @detect_pin: GPIO pin used for card detection, or negative if not
235 * @wp_pin: GPIO pin used for card write protect sending, or negative
237 * @detect_is_active_high: The state of the detect pin when it is active.
238 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
240 struct atmel_mci_slot
{
241 struct mmc_host
*mmc
;
242 struct atmel_mci
*host
;
247 struct mmc_request
*mrq
;
248 struct list_head queue_node
;
252 #define ATMCI_CARD_PRESENT 0
253 #define ATMCI_CARD_NEED_INIT 1
254 #define ATMCI_SHUTDOWN 2
255 #define ATMCI_SUSPENDED 3
259 bool detect_is_active_high
;
261 struct timer_list detect_timer
;
264 #define atmci_test_and_clear_pending(host, event) \
265 test_and_clear_bit(event, &host->pending_events)
266 #define atmci_set_completed(host, event) \
267 set_bit(event, &host->completed_events)
268 #define atmci_set_pending(host, event) \
269 set_bit(event, &host->pending_events)
272 * The debugfs stuff below is mostly optimized away when
273 * CONFIG_DEBUG_FS is not set.
275 static int atmci_req_show(struct seq_file
*s
, void *v
)
277 struct atmel_mci_slot
*slot
= s
->private;
278 struct mmc_request
*mrq
;
279 struct mmc_command
*cmd
;
280 struct mmc_command
*stop
;
281 struct mmc_data
*data
;
283 /* Make sure we get a consistent snapshot */
284 spin_lock_bh(&slot
->host
->lock
);
294 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
295 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
296 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
297 cmd
->resp
[3], cmd
->error
);
299 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
300 data
->bytes_xfered
, data
->blocks
,
301 data
->blksz
, data
->flags
, data
->error
);
304 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
305 stop
->opcode
, stop
->arg
, stop
->flags
,
306 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
307 stop
->resp
[3], stop
->error
);
310 spin_unlock_bh(&slot
->host
->lock
);
315 static int atmci_req_open(struct inode
*inode
, struct file
*file
)
317 return single_open(file
, atmci_req_show
, inode
->i_private
);
320 static const struct file_operations atmci_req_fops
= {
321 .owner
= THIS_MODULE
,
322 .open
= atmci_req_open
,
325 .release
= single_release
,
328 static void atmci_show_status_reg(struct seq_file
*s
,
329 const char *regname
, u32 value
)
331 static const char *sr_bit
[] = {
362 seq_printf(s
, "%s:\t0x%08x", regname
, value
);
363 for (i
= 0; i
< ARRAY_SIZE(sr_bit
); i
++) {
364 if (value
& (1 << i
)) {
366 seq_printf(s
, " %s", sr_bit
[i
]);
368 seq_puts(s
, " UNKNOWN");
374 static int atmci_regs_show(struct seq_file
*s
, void *v
)
376 struct atmel_mci
*host
= s
->private;
379 buf
= kmalloc(ATMCI_REGS_SIZE
, GFP_KERNEL
);
384 * Grab a more or less consistent snapshot. Note that we're
385 * not disabling interrupts, so IMR and SR may not be
388 spin_lock_bh(&host
->lock
);
389 clk_enable(host
->mck
);
390 memcpy_fromio(buf
, host
->regs
, ATMCI_REGS_SIZE
);
391 clk_disable(host
->mck
);
392 spin_unlock_bh(&host
->lock
);
394 seq_printf(s
, "MR:\t0x%08x%s%s ",
396 buf
[ATMCI_MR
/ 4] & ATMCI_MR_RDPROOF
? " RDPROOF" : "",
397 buf
[ATMCI_MR
/ 4] & ATMCI_MR_WRPROOF
? " WRPROOF" : "");
398 if (host
->caps
.has_odd_clk_div
)
399 seq_printf(s
, "{CLKDIV,CLKODD}=%u\n",
400 ((buf
[ATMCI_MR
/ 4] & 0xff) << 1)
401 | ((buf
[ATMCI_MR
/ 4] >> 16) & 1));
403 seq_printf(s
, "CLKDIV=%u\n",
404 (buf
[ATMCI_MR
/ 4] & 0xff));
405 seq_printf(s
, "DTOR:\t0x%08x\n", buf
[ATMCI_DTOR
/ 4]);
406 seq_printf(s
, "SDCR:\t0x%08x\n", buf
[ATMCI_SDCR
/ 4]);
407 seq_printf(s
, "ARGR:\t0x%08x\n", buf
[ATMCI_ARGR
/ 4]);
408 seq_printf(s
, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
410 buf
[ATMCI_BLKR
/ 4] & 0xffff,
411 (buf
[ATMCI_BLKR
/ 4] >> 16) & 0xffff);
412 if (host
->caps
.has_cstor_reg
)
413 seq_printf(s
, "CSTOR:\t0x%08x\n", buf
[ATMCI_CSTOR
/ 4]);
415 /* Don't read RSPR and RDR; it will consume the data there */
417 atmci_show_status_reg(s
, "SR", buf
[ATMCI_SR
/ 4]);
418 atmci_show_status_reg(s
, "IMR", buf
[ATMCI_IMR
/ 4]);
420 if (host
->caps
.has_dma
) {
423 val
= buf
[ATMCI_DMA
/ 4];
424 seq_printf(s
, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
427 1 << (((val
>> 4) & 3) + 1) : 1,
428 val
& ATMCI_DMAEN
? " DMAEN" : "");
430 if (host
->caps
.has_cfg_reg
) {
433 val
= buf
[ATMCI_CFG
/ 4];
434 seq_printf(s
, "CFG:\t0x%08x%s%s%s%s\n",
436 val
& ATMCI_CFG_FIFOMODE_1DATA
? " FIFOMODE_ONE_DATA" : "",
437 val
& ATMCI_CFG_FERRCTRL_COR
? " FERRCTRL_CLEAR_ON_READ" : "",
438 val
& ATMCI_CFG_HSMODE
? " HSMODE" : "",
439 val
& ATMCI_CFG_LSYNC
? " LSYNC" : "");
447 static int atmci_regs_open(struct inode
*inode
, struct file
*file
)
449 return single_open(file
, atmci_regs_show
, inode
->i_private
);
452 static const struct file_operations atmci_regs_fops
= {
453 .owner
= THIS_MODULE
,
454 .open
= atmci_regs_open
,
457 .release
= single_release
,
460 static void atmci_init_debugfs(struct atmel_mci_slot
*slot
)
462 struct mmc_host
*mmc
= slot
->mmc
;
463 struct atmel_mci
*host
= slot
->host
;
467 root
= mmc
->debugfs_root
;
471 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
478 node
= debugfs_create_file("req", S_IRUSR
, root
, slot
, &atmci_req_fops
);
482 node
= debugfs_create_u32("state", S_IRUSR
, root
, (u32
*)&host
->state
);
486 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
487 (u32
*)&host
->pending_events
);
491 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
492 (u32
*)&host
->completed_events
);
499 dev_err(&mmc
->class_dev
, "failed to initialize debugfs for slot\n");
502 static inline unsigned int atmci_get_version(struct atmel_mci
*host
)
504 return atmci_readl(host
, ATMCI_VERSION
) & 0x00000fff;
507 static void atmci_timeout_timer(unsigned long data
)
509 struct atmel_mci
*host
;
511 host
= (struct atmel_mci
*)data
;
513 dev_dbg(&host
->pdev
->dev
, "software timeout\n");
515 if (host
->mrq
->cmd
->data
) {
516 host
->mrq
->cmd
->data
->error
= -ETIMEDOUT
;
519 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
522 host
->need_reset
= 1;
523 host
->state
= STATE_END_REQUEST
;
525 tasklet_schedule(&host
->tasklet
);
528 static inline unsigned int atmci_ns_to_clocks(struct atmel_mci
*host
,
532 * It is easier here to use us instead of ns for the timeout,
533 * it prevents from overflows during calculation.
535 unsigned int us
= DIV_ROUND_UP(ns
, 1000);
537 /* Maximum clock frequency is host->bus_hz/2 */
538 return us
* (DIV_ROUND_UP(host
->bus_hz
, 2000000));
541 static void atmci_set_timeout(struct atmel_mci
*host
,
542 struct atmel_mci_slot
*slot
, struct mmc_data
*data
)
544 static unsigned dtomul_to_shift
[] = {
545 0, 4, 7, 8, 10, 12, 16, 20
551 timeout
= atmci_ns_to_clocks(host
, data
->timeout_ns
)
552 + data
->timeout_clks
;
554 for (dtomul
= 0; dtomul
< 8; dtomul
++) {
555 unsigned shift
= dtomul_to_shift
[dtomul
];
556 dtocyc
= (timeout
+ (1 << shift
) - 1) >> shift
;
566 dev_vdbg(&slot
->mmc
->class_dev
, "setting timeout to %u cycles\n",
567 dtocyc
<< dtomul_to_shift
[dtomul
]);
568 atmci_writel(host
, ATMCI_DTOR
, (ATMCI_DTOMUL(dtomul
) | ATMCI_DTOCYC(dtocyc
)));
572 * Return mask with command flags to be enabled for this command.
574 static u32
atmci_prepare_command(struct mmc_host
*mmc
,
575 struct mmc_command
*cmd
)
577 struct mmc_data
*data
;
580 cmd
->error
= -EINPROGRESS
;
582 cmdr
= ATMCI_CMDR_CMDNB(cmd
->opcode
);
584 if (cmd
->flags
& MMC_RSP_PRESENT
) {
585 if (cmd
->flags
& MMC_RSP_136
)
586 cmdr
|= ATMCI_CMDR_RSPTYP_136BIT
;
588 cmdr
|= ATMCI_CMDR_RSPTYP_48BIT
;
592 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
593 * it's too difficult to determine whether this is an ACMD or
594 * not. Better make it 64.
596 cmdr
|= ATMCI_CMDR_MAXLAT_64CYC
;
598 if (mmc
->ios
.bus_mode
== MMC_BUSMODE_OPENDRAIN
)
599 cmdr
|= ATMCI_CMDR_OPDCMD
;
603 cmdr
|= ATMCI_CMDR_START_XFER
;
605 if (cmd
->opcode
== SD_IO_RW_EXTENDED
) {
606 cmdr
|= ATMCI_CMDR_SDIO_BLOCK
;
608 if (data
->flags
& MMC_DATA_STREAM
)
609 cmdr
|= ATMCI_CMDR_STREAM
;
610 else if (data
->blocks
> 1)
611 cmdr
|= ATMCI_CMDR_MULTI_BLOCK
;
613 cmdr
|= ATMCI_CMDR_BLOCK
;
616 if (data
->flags
& MMC_DATA_READ
)
617 cmdr
|= ATMCI_CMDR_TRDIR_READ
;
623 static void atmci_send_command(struct atmel_mci
*host
,
624 struct mmc_command
*cmd
, u32 cmd_flags
)
629 dev_vdbg(&host
->pdev
->dev
,
630 "start command: ARGR=0x%08x CMDR=0x%08x\n",
631 cmd
->arg
, cmd_flags
);
633 atmci_writel(host
, ATMCI_ARGR
, cmd
->arg
);
634 atmci_writel(host
, ATMCI_CMDR
, cmd_flags
);
637 static void atmci_send_stop_cmd(struct atmel_mci
*host
, struct mmc_data
*data
)
639 dev_dbg(&host
->pdev
->dev
, "send stop command\n");
640 atmci_send_command(host
, data
->stop
, host
->stop_cmdr
);
641 atmci_writel(host
, ATMCI_IER
, ATMCI_CMDRDY
);
645 * Configure given PDC buffer taking care of alignement issues.
646 * Update host->data_size and host->sg.
648 static void atmci_pdc_set_single_buf(struct atmel_mci
*host
,
649 enum atmci_xfer_dir dir
, enum atmci_pdc_buf buf_nb
)
651 u32 pointer_reg
, counter_reg
;
652 unsigned int buf_size
;
654 if (dir
== XFER_RECEIVE
) {
655 pointer_reg
= ATMEL_PDC_RPR
;
656 counter_reg
= ATMEL_PDC_RCR
;
658 pointer_reg
= ATMEL_PDC_TPR
;
659 counter_reg
= ATMEL_PDC_TCR
;
662 if (buf_nb
== PDC_SECOND_BUF
) {
663 pointer_reg
+= ATMEL_PDC_SCND_BUF_OFF
;
664 counter_reg
+= ATMEL_PDC_SCND_BUF_OFF
;
667 if (!host
->caps
.has_rwproof
) {
668 buf_size
= host
->buf_size
;
669 atmci_writel(host
, pointer_reg
, host
->buf_phys_addr
);
671 buf_size
= sg_dma_len(host
->sg
);
672 atmci_writel(host
, pointer_reg
, sg_dma_address(host
->sg
));
675 if (host
->data_size
<= buf_size
) {
676 if (host
->data_size
& 0x3) {
677 /* If size is different from modulo 4, transfer bytes */
678 atmci_writel(host
, counter_reg
, host
->data_size
);
679 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
| ATMCI_MR_PDCFBYTE
);
681 /* Else transfer 32-bits words */
682 atmci_writel(host
, counter_reg
, host
->data_size
/ 4);
686 /* We assume the size of a page is 32-bits aligned */
687 atmci_writel(host
, counter_reg
, sg_dma_len(host
->sg
) / 4);
688 host
->data_size
-= sg_dma_len(host
->sg
);
690 host
->sg
= sg_next(host
->sg
);
695 * Configure PDC buffer according to the data size ie configuring one or two
696 * buffers. Don't use this function if you want to configure only the second
697 * buffer. In this case, use atmci_pdc_set_single_buf.
699 static void atmci_pdc_set_both_buf(struct atmel_mci
*host
, int dir
)
701 atmci_pdc_set_single_buf(host
, dir
, PDC_FIRST_BUF
);
703 atmci_pdc_set_single_buf(host
, dir
, PDC_SECOND_BUF
);
707 * Unmap sg lists, called when transfer is finished.
709 static void atmci_pdc_cleanup(struct atmel_mci
*host
)
711 struct mmc_data
*data
= host
->data
;
714 dma_unmap_sg(&host
->pdev
->dev
,
715 data
->sg
, data
->sg_len
,
716 ((data
->flags
& MMC_DATA_WRITE
)
717 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
721 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
722 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
723 * interrupt needed for both transfer directions.
725 static void atmci_pdc_complete(struct atmel_mci
*host
)
727 int transfer_size
= host
->data
->blocks
* host
->data
->blksz
;
730 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
732 if ((!host
->caps
.has_rwproof
)
733 && (host
->data
->flags
& MMC_DATA_READ
)) {
734 if (host
->caps
.has_bad_data_ordering
)
735 for (i
= 0; i
< transfer_size
; i
++)
736 host
->buffer
[i
] = swab32(host
->buffer
[i
]);
737 sg_copy_from_buffer(host
->data
->sg
, host
->data
->sg_len
,
738 host
->buffer
, transfer_size
);
741 atmci_pdc_cleanup(host
);
744 * If the card was removed, data will be NULL. No point trying
745 * to send the stop command or waiting for NBUSY in this case.
748 dev_dbg(&host
->pdev
->dev
,
749 "(%s) set pending xfer complete\n", __func__
);
750 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
751 tasklet_schedule(&host
->tasklet
);
755 static void atmci_dma_cleanup(struct atmel_mci
*host
)
757 struct mmc_data
*data
= host
->data
;
760 dma_unmap_sg(host
->dma
.chan
->device
->dev
,
761 data
->sg
, data
->sg_len
,
762 ((data
->flags
& MMC_DATA_WRITE
)
763 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
767 * This function is called by the DMA driver from tasklet context.
769 static void atmci_dma_complete(void *arg
)
771 struct atmel_mci
*host
= arg
;
772 struct mmc_data
*data
= host
->data
;
774 dev_vdbg(&host
->pdev
->dev
, "DMA complete\n");
776 if (host
->caps
.has_dma
)
777 /* Disable DMA hardware handshaking on MCI */
778 atmci_writel(host
, ATMCI_DMA
, atmci_readl(host
, ATMCI_DMA
) & ~ATMCI_DMAEN
);
780 atmci_dma_cleanup(host
);
783 * If the card was removed, data will be NULL. No point trying
784 * to send the stop command or waiting for NBUSY in this case.
787 dev_dbg(&host
->pdev
->dev
,
788 "(%s) set pending xfer complete\n", __func__
);
789 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
790 tasklet_schedule(&host
->tasklet
);
793 * Regardless of what the documentation says, we have
794 * to wait for NOTBUSY even after block read
797 * When the DMA transfer is complete, the controller
798 * may still be reading the CRC from the card, i.e.
799 * the data transfer is still in progress and we
800 * haven't seen all the potential error bits yet.
802 * The interrupt handler will schedule a different
803 * tasklet to finish things up when the data transfer
804 * is completely done.
806 * We may not complete the mmc request here anyway
807 * because the mmc layer may call back and cause us to
808 * violate the "don't submit new operations from the
809 * completion callback" rule of the dma engine
812 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
817 * Returns a mask of interrupt flags to be enabled after the whole
818 * request has been prepared.
820 static u32
atmci_prepare_data(struct atmel_mci
*host
, struct mmc_data
*data
)
824 data
->error
= -EINPROGRESS
;
828 host
->data_chan
= NULL
;
830 iflags
= ATMCI_DATA_ERROR_FLAGS
;
833 * Errata: MMC data write operation with less than 12
834 * bytes is impossible.
836 * Errata: MCI Transmit Data Register (TDR) FIFO
837 * corruption when length is not multiple of 4.
839 if (data
->blocks
* data
->blksz
< 12
840 || (data
->blocks
* data
->blksz
) & 3)
841 host
->need_reset
= true;
843 host
->pio_offset
= 0;
844 if (data
->flags
& MMC_DATA_READ
)
845 iflags
|= ATMCI_RXRDY
;
847 iflags
|= ATMCI_TXRDY
;
853 * Set interrupt flags and set block length into the MCI mode register even
854 * if this value is also accessible in the MCI block register. It seems to be
855 * necessary before the High Speed MCI version. It also map sg and configure
859 atmci_prepare_data_pdc(struct atmel_mci
*host
, struct mmc_data
*data
)
863 enum dma_data_direction dir
;
866 data
->error
= -EINPROGRESS
;
870 iflags
= ATMCI_DATA_ERROR_FLAGS
;
872 /* Enable pdc mode */
873 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
| ATMCI_MR_PDCMODE
);
875 if (data
->flags
& MMC_DATA_READ
) {
876 dir
= DMA_FROM_DEVICE
;
877 iflags
|= ATMCI_ENDRX
| ATMCI_RXBUFF
;
880 iflags
|= ATMCI_ENDTX
| ATMCI_TXBUFE
| ATMCI_BLKE
;
884 tmp
= atmci_readl(host
, ATMCI_MR
);
886 tmp
|= ATMCI_BLKLEN(data
->blksz
);
887 atmci_writel(host
, ATMCI_MR
, tmp
);
890 host
->data_size
= data
->blocks
* data
->blksz
;
891 sg_len
= dma_map_sg(&host
->pdev
->dev
, data
->sg
, data
->sg_len
, dir
);
893 if ((!host
->caps
.has_rwproof
)
894 && (host
->data
->flags
& MMC_DATA_WRITE
)) {
895 sg_copy_to_buffer(host
->data
->sg
, host
->data
->sg_len
,
896 host
->buffer
, host
->data_size
);
897 if (host
->caps
.has_bad_data_ordering
)
898 for (i
= 0; i
< host
->data_size
; i
++)
899 host
->buffer
[i
] = swab32(host
->buffer
[i
]);
903 atmci_pdc_set_both_buf(host
,
904 ((dir
== DMA_FROM_DEVICE
) ? XFER_RECEIVE
: XFER_TRANSMIT
));
910 atmci_prepare_data_dma(struct atmel_mci
*host
, struct mmc_data
*data
)
912 struct dma_chan
*chan
;
913 struct dma_async_tx_descriptor
*desc
;
914 struct scatterlist
*sg
;
916 enum dma_data_direction direction
;
917 enum dma_transfer_direction slave_dirn
;
922 data
->error
= -EINPROGRESS
;
928 iflags
= ATMCI_DATA_ERROR_FLAGS
;
931 * We don't do DMA on "complex" transfers, i.e. with
932 * non-word-aligned buffers or lengths. Also, we don't bother
933 * with all the DMA setup overhead for short transfers.
935 if (data
->blocks
* data
->blksz
< ATMCI_DMA_THRESHOLD
)
936 return atmci_prepare_data(host
, data
);
938 return atmci_prepare_data(host
, data
);
940 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
941 if (sg
->offset
& 3 || sg
->length
& 3)
942 return atmci_prepare_data(host
, data
);
945 /* If we don't have a channel, we can't do DMA */
946 chan
= host
->dma
.chan
;
948 host
->data_chan
= chan
;
953 if (data
->flags
& MMC_DATA_READ
) {
954 direction
= DMA_FROM_DEVICE
;
955 host
->dma_conf
.direction
= slave_dirn
= DMA_DEV_TO_MEM
;
956 maxburst
= atmci_convert_chksize(host
->dma_conf
.src_maxburst
);
958 direction
= DMA_TO_DEVICE
;
959 host
->dma_conf
.direction
= slave_dirn
= DMA_MEM_TO_DEV
;
960 maxburst
= atmci_convert_chksize(host
->dma_conf
.dst_maxburst
);
963 atmci_writel(host
, ATMCI_DMA
, ATMCI_DMA_CHKSIZE(maxburst
) | ATMCI_DMAEN
);
965 sglen
= dma_map_sg(chan
->device
->dev
, data
->sg
,
966 data
->sg_len
, direction
);
968 dmaengine_slave_config(chan
, &host
->dma_conf
);
969 desc
= dmaengine_prep_slave_sg(chan
,
970 data
->sg
, sglen
, slave_dirn
,
971 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
975 host
->dma
.data_desc
= desc
;
976 desc
->callback
= atmci_dma_complete
;
977 desc
->callback_param
= host
;
981 dma_unmap_sg(chan
->device
->dev
, data
->sg
, data
->sg_len
, direction
);
986 atmci_submit_data(struct atmel_mci
*host
, struct mmc_data
*data
)
992 * Start PDC according to transfer direction.
995 atmci_submit_data_pdc(struct atmel_mci
*host
, struct mmc_data
*data
)
997 if (data
->flags
& MMC_DATA_READ
)
998 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTEN
);
1000 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTEN
);
1004 atmci_submit_data_dma(struct atmel_mci
*host
, struct mmc_data
*data
)
1006 struct dma_chan
*chan
= host
->data_chan
;
1007 struct dma_async_tx_descriptor
*desc
= host
->dma
.data_desc
;
1010 dmaengine_submit(desc
);
1011 dma_async_issue_pending(chan
);
1015 static void atmci_stop_transfer(struct atmel_mci
*host
)
1017 dev_dbg(&host
->pdev
->dev
,
1018 "(%s) set pending xfer complete\n", __func__
);
1019 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1020 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1024 * Stop data transfer because error(s) occured.
1026 static void atmci_stop_transfer_pdc(struct atmel_mci
*host
)
1028 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
1031 static void atmci_stop_transfer_dma(struct atmel_mci
*host
)
1033 struct dma_chan
*chan
= host
->data_chan
;
1036 dmaengine_terminate_all(chan
);
1037 atmci_dma_cleanup(host
);
1039 /* Data transfer was stopped by the interrupt handler */
1040 dev_dbg(&host
->pdev
->dev
,
1041 "(%s) set pending xfer complete\n", __func__
);
1042 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1043 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1048 * Start a request: prepare data if needed, prepare the command and activate
1051 static void atmci_start_request(struct atmel_mci
*host
,
1052 struct atmel_mci_slot
*slot
)
1054 struct mmc_request
*mrq
;
1055 struct mmc_command
*cmd
;
1056 struct mmc_data
*data
;
1061 host
->cur_slot
= slot
;
1064 host
->pending_events
= 0;
1065 host
->completed_events
= 0;
1066 host
->cmd_status
= 0;
1067 host
->data_status
= 0;
1069 dev_dbg(&host
->pdev
->dev
, "start request: cmd %u\n", mrq
->cmd
->opcode
);
1071 if (host
->need_reset
|| host
->caps
.need_reset_after_xfer
) {
1072 iflags
= atmci_readl(host
, ATMCI_IMR
);
1073 iflags
&= (ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
);
1074 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1075 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1076 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1077 if (host
->caps
.has_cfg_reg
)
1078 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1079 atmci_writel(host
, ATMCI_IER
, iflags
);
1080 host
->need_reset
= false;
1082 atmci_writel(host
, ATMCI_SDCR
, slot
->sdc_reg
);
1084 iflags
= atmci_readl(host
, ATMCI_IMR
);
1085 if (iflags
& ~(ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
))
1086 dev_dbg(&slot
->mmc
->class_dev
, "WARNING: IMR=0x%08x\n",
1089 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT
, &slot
->flags
))) {
1090 /* Send init sequence (74 clock cycles) */
1091 atmci_writel(host
, ATMCI_CMDR
, ATMCI_CMDR_SPCMD_INIT
);
1092 while (!(atmci_readl(host
, ATMCI_SR
) & ATMCI_CMDRDY
))
1098 atmci_set_timeout(host
, slot
, data
);
1100 /* Must set block count/size before sending command */
1101 atmci_writel(host
, ATMCI_BLKR
, ATMCI_BCNT(data
->blocks
)
1102 | ATMCI_BLKLEN(data
->blksz
));
1103 dev_vdbg(&slot
->mmc
->class_dev
, "BLKR=0x%08x\n",
1104 ATMCI_BCNT(data
->blocks
) | ATMCI_BLKLEN(data
->blksz
));
1106 iflags
|= host
->prepare_data(host
, data
);
1109 iflags
|= ATMCI_CMDRDY
;
1111 cmdflags
= atmci_prepare_command(slot
->mmc
, cmd
);
1112 atmci_send_command(host
, cmd
, cmdflags
);
1115 host
->submit_data(host
, data
);
1118 host
->stop_cmdr
= atmci_prepare_command(slot
->mmc
, mrq
->stop
);
1119 host
->stop_cmdr
|= ATMCI_CMDR_STOP_XFER
;
1120 if (!(data
->flags
& MMC_DATA_WRITE
))
1121 host
->stop_cmdr
|= ATMCI_CMDR_TRDIR_READ
;
1122 if (data
->flags
& MMC_DATA_STREAM
)
1123 host
->stop_cmdr
|= ATMCI_CMDR_STREAM
;
1125 host
->stop_cmdr
|= ATMCI_CMDR_MULTI_BLOCK
;
1129 * We could have enabled interrupts earlier, but I suspect
1130 * that would open up a nice can of interesting race
1131 * conditions (e.g. command and data complete, but stop not
1134 atmci_writel(host
, ATMCI_IER
, iflags
);
1136 mod_timer(&host
->timer
, jiffies
+ msecs_to_jiffies(2000));
1139 static void atmci_queue_request(struct atmel_mci
*host
,
1140 struct atmel_mci_slot
*slot
, struct mmc_request
*mrq
)
1142 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
1145 spin_lock_bh(&host
->lock
);
1147 if (host
->state
== STATE_IDLE
) {
1148 host
->state
= STATE_SENDING_CMD
;
1149 atmci_start_request(host
, slot
);
1151 dev_dbg(&host
->pdev
->dev
, "queue request\n");
1152 list_add_tail(&slot
->queue_node
, &host
->queue
);
1154 spin_unlock_bh(&host
->lock
);
1157 static void atmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1159 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1160 struct atmel_mci
*host
= slot
->host
;
1161 struct mmc_data
*data
;
1164 dev_dbg(&host
->pdev
->dev
, "MRQ: cmd %u\n", mrq
->cmd
->opcode
);
1167 * We may "know" the card is gone even though there's still an
1168 * electrical connection. If so, we really need to communicate
1169 * this to the MMC core since there won't be any more
1170 * interrupts as the card is completely removed. Otherwise,
1171 * the MMC core might believe the card is still there even
1172 * though the card was just removed very slowly.
1174 if (!test_bit(ATMCI_CARD_PRESENT
, &slot
->flags
)) {
1175 mrq
->cmd
->error
= -ENOMEDIUM
;
1176 mmc_request_done(mmc
, mrq
);
1180 /* We don't support multiple blocks of weird lengths. */
1182 if (data
&& data
->blocks
> 1 && data
->blksz
& 3) {
1183 mrq
->cmd
->error
= -EINVAL
;
1184 mmc_request_done(mmc
, mrq
);
1187 atmci_queue_request(host
, slot
, mrq
);
1190 static void atmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1192 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1193 struct atmel_mci
*host
= slot
->host
;
1196 slot
->sdc_reg
&= ~ATMCI_SDCBUS_MASK
;
1197 switch (ios
->bus_width
) {
1198 case MMC_BUS_WIDTH_1
:
1199 slot
->sdc_reg
|= ATMCI_SDCBUS_1BIT
;
1201 case MMC_BUS_WIDTH_4
:
1202 slot
->sdc_reg
|= ATMCI_SDCBUS_4BIT
;
1207 unsigned int clock_min
= ~0U;
1210 spin_lock_bh(&host
->lock
);
1211 if (!host
->mode_reg
) {
1212 clk_enable(host
->mck
);
1213 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1214 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1215 if (host
->caps
.has_cfg_reg
)
1216 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1220 * Use mirror of ios->clock to prevent race with mmc
1221 * core ios update when finding the minimum.
1223 slot
->clock
= ios
->clock
;
1224 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1225 if (host
->slot
[i
] && host
->slot
[i
]->clock
1226 && host
->slot
[i
]->clock
< clock_min
)
1227 clock_min
= host
->slot
[i
]->clock
;
1230 /* Calculate clock divider */
1231 if (host
->caps
.has_odd_clk_div
) {
1232 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, clock_min
) - 2;
1234 dev_warn(&mmc
->class_dev
,
1235 "clock %u too slow; using %lu\n",
1236 clock_min
, host
->bus_hz
/ (511 + 2));
1239 host
->mode_reg
= ATMCI_MR_CLKDIV(clkdiv
>> 1)
1240 | ATMCI_MR_CLKODD(clkdiv
& 1);
1242 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, 2 * clock_min
) - 1;
1244 dev_warn(&mmc
->class_dev
,
1245 "clock %u too slow; using %lu\n",
1246 clock_min
, host
->bus_hz
/ (2 * 256));
1249 host
->mode_reg
= ATMCI_MR_CLKDIV(clkdiv
);
1253 * WRPROOF and RDPROOF prevent overruns/underruns by
1254 * stopping the clock when the FIFO is full/empty.
1255 * This state is not expected to last for long.
1257 if (host
->caps
.has_rwproof
)
1258 host
->mode_reg
|= (ATMCI_MR_WRPROOF
| ATMCI_MR_RDPROOF
);
1260 if (host
->caps
.has_cfg_reg
) {
1261 /* setup High Speed mode in relation with card capacity */
1262 if (ios
->timing
== MMC_TIMING_SD_HS
)
1263 host
->cfg_reg
|= ATMCI_CFG_HSMODE
;
1265 host
->cfg_reg
&= ~ATMCI_CFG_HSMODE
;
1268 if (list_empty(&host
->queue
)) {
1269 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1270 if (host
->caps
.has_cfg_reg
)
1271 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1273 host
->need_clock_update
= true;
1276 spin_unlock_bh(&host
->lock
);
1278 bool any_slot_active
= false;
1280 spin_lock_bh(&host
->lock
);
1282 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1283 if (host
->slot
[i
] && host
->slot
[i
]->clock
) {
1284 any_slot_active
= true;
1288 if (!any_slot_active
) {
1289 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIDIS
);
1290 if (host
->mode_reg
) {
1291 atmci_readl(host
, ATMCI_MR
);
1292 clk_disable(host
->mck
);
1296 spin_unlock_bh(&host
->lock
);
1299 switch (ios
->power_mode
) {
1301 set_bit(ATMCI_CARD_NEED_INIT
, &slot
->flags
);
1305 * TODO: None of the currently available AVR32-based
1306 * boards allow MMC power to be turned off. Implement
1307 * power control when this can be tested properly.
1309 * We also need to hook this into the clock management
1310 * somehow so that newly inserted cards aren't
1311 * subjected to a fast clock before we have a chance
1312 * to figure out what the maximum rate is. Currently,
1313 * there's no way to avoid this, and there never will
1314 * be for boards that don't support power control.
1320 static int atmci_get_ro(struct mmc_host
*mmc
)
1322 int read_only
= -ENOSYS
;
1323 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1325 if (gpio_is_valid(slot
->wp_pin
)) {
1326 read_only
= gpio_get_value(slot
->wp_pin
);
1327 dev_dbg(&mmc
->class_dev
, "card is %s\n",
1328 read_only
? "read-only" : "read-write");
1334 static int atmci_get_cd(struct mmc_host
*mmc
)
1336 int present
= -ENOSYS
;
1337 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1339 if (gpio_is_valid(slot
->detect_pin
)) {
1340 present
= !(gpio_get_value(slot
->detect_pin
) ^
1341 slot
->detect_is_active_high
);
1342 dev_dbg(&mmc
->class_dev
, "card is %spresent\n",
1343 present
? "" : "not ");
1349 static void atmci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1351 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1352 struct atmel_mci
*host
= slot
->host
;
1355 atmci_writel(host
, ATMCI_IER
, slot
->sdio_irq
);
1357 atmci_writel(host
, ATMCI_IDR
, slot
->sdio_irq
);
1360 static const struct mmc_host_ops atmci_ops
= {
1361 .request
= atmci_request
,
1362 .set_ios
= atmci_set_ios
,
1363 .get_ro
= atmci_get_ro
,
1364 .get_cd
= atmci_get_cd
,
1365 .enable_sdio_irq
= atmci_enable_sdio_irq
,
1368 /* Called with host->lock held */
1369 static void atmci_request_end(struct atmel_mci
*host
, struct mmc_request
*mrq
)
1370 __releases(&host
->lock
)
1371 __acquires(&host
->lock
)
1373 struct atmel_mci_slot
*slot
= NULL
;
1374 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
1376 WARN_ON(host
->cmd
|| host
->data
);
1379 * Update the MMC clock rate if necessary. This may be
1380 * necessary if set_ios() is called when a different slot is
1381 * busy transferring data.
1383 if (host
->need_clock_update
) {
1384 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1385 if (host
->caps
.has_cfg_reg
)
1386 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1389 host
->cur_slot
->mrq
= NULL
;
1391 if (!list_empty(&host
->queue
)) {
1392 slot
= list_entry(host
->queue
.next
,
1393 struct atmel_mci_slot
, queue_node
);
1394 list_del(&slot
->queue_node
);
1395 dev_vdbg(&host
->pdev
->dev
, "list not empty: %s is next\n",
1396 mmc_hostname(slot
->mmc
));
1397 host
->state
= STATE_SENDING_CMD
;
1398 atmci_start_request(host
, slot
);
1400 dev_vdbg(&host
->pdev
->dev
, "list empty\n");
1401 host
->state
= STATE_IDLE
;
1404 del_timer(&host
->timer
);
1406 spin_unlock(&host
->lock
);
1407 mmc_request_done(prev_mmc
, mrq
);
1408 spin_lock(&host
->lock
);
1411 static void atmci_command_complete(struct atmel_mci
*host
,
1412 struct mmc_command
*cmd
)
1414 u32 status
= host
->cmd_status
;
1416 /* Read the response from the card (up to 16 bytes) */
1417 cmd
->resp
[0] = atmci_readl(host
, ATMCI_RSPR
);
1418 cmd
->resp
[1] = atmci_readl(host
, ATMCI_RSPR
);
1419 cmd
->resp
[2] = atmci_readl(host
, ATMCI_RSPR
);
1420 cmd
->resp
[3] = atmci_readl(host
, ATMCI_RSPR
);
1422 if (status
& ATMCI_RTOE
)
1423 cmd
->error
= -ETIMEDOUT
;
1424 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& ATMCI_RCRCE
))
1425 cmd
->error
= -EILSEQ
;
1426 else if (status
& (ATMCI_RINDE
| ATMCI_RDIRE
| ATMCI_RENDE
))
1428 else if (host
->mrq
->data
&& (host
->mrq
->data
->blksz
& 3)) {
1429 if (host
->caps
.need_blksz_mul_4
) {
1430 cmd
->error
= -EINVAL
;
1431 host
->need_reset
= 1;
1437 static void atmci_detect_change(unsigned long data
)
1439 struct atmel_mci_slot
*slot
= (struct atmel_mci_slot
*)data
;
1444 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1445 * freeing the interrupt. We must not re-enable the interrupt
1446 * if it has been freed, and if we're shutting down, it
1447 * doesn't really matter whether the card is present or not.
1450 if (test_bit(ATMCI_SHUTDOWN
, &slot
->flags
))
1453 enable_irq(gpio_to_irq(slot
->detect_pin
));
1454 present
= !(gpio_get_value(slot
->detect_pin
) ^
1455 slot
->detect_is_active_high
);
1456 present_old
= test_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1458 dev_vdbg(&slot
->mmc
->class_dev
, "detect change: %d (was %d)\n",
1459 present
, present_old
);
1461 if (present
!= present_old
) {
1462 struct atmel_mci
*host
= slot
->host
;
1463 struct mmc_request
*mrq
;
1465 dev_dbg(&slot
->mmc
->class_dev
, "card %s\n",
1466 present
? "inserted" : "removed");
1468 spin_lock(&host
->lock
);
1471 clear_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1473 set_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1475 /* Clean up queue if present */
1478 if (mrq
== host
->mrq
) {
1480 * Reset controller to terminate any ongoing
1481 * commands or data transfers.
1483 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1484 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1485 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1486 if (host
->caps
.has_cfg_reg
)
1487 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1492 switch (host
->state
) {
1495 case STATE_SENDING_CMD
:
1496 mrq
->cmd
->error
= -ENOMEDIUM
;
1498 host
->stop_transfer(host
);
1500 case STATE_DATA_XFER
:
1501 mrq
->data
->error
= -ENOMEDIUM
;
1502 host
->stop_transfer(host
);
1504 case STATE_WAITING_NOTBUSY
:
1505 mrq
->data
->error
= -ENOMEDIUM
;
1507 case STATE_SENDING_STOP
:
1508 mrq
->stop
->error
= -ENOMEDIUM
;
1510 case STATE_END_REQUEST
:
1514 atmci_request_end(host
, mrq
);
1516 list_del(&slot
->queue_node
);
1517 mrq
->cmd
->error
= -ENOMEDIUM
;
1519 mrq
->data
->error
= -ENOMEDIUM
;
1521 mrq
->stop
->error
= -ENOMEDIUM
;
1523 spin_unlock(&host
->lock
);
1524 mmc_request_done(slot
->mmc
, mrq
);
1525 spin_lock(&host
->lock
);
1528 spin_unlock(&host
->lock
);
1530 mmc_detect_change(slot
->mmc
, 0);
1534 static void atmci_tasklet_func(unsigned long priv
)
1536 struct atmel_mci
*host
= (struct atmel_mci
*)priv
;
1537 struct mmc_request
*mrq
= host
->mrq
;
1538 struct mmc_data
*data
= host
->data
;
1539 enum atmel_mci_state state
= host
->state
;
1540 enum atmel_mci_state prev_state
;
1543 spin_lock(&host
->lock
);
1545 state
= host
->state
;
1547 dev_vdbg(&host
->pdev
->dev
,
1548 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1549 state
, host
->pending_events
, host
->completed_events
,
1550 atmci_readl(host
, ATMCI_IMR
));
1554 dev_dbg(&host
->pdev
->dev
, "FSM: state=%d\n", state
);
1560 case STATE_SENDING_CMD
:
1562 * Command has been sent, we are waiting for command
1563 * ready. Then we have three next states possible:
1564 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1565 * command needing it or DATA_XFER if there is data.
1567 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready?\n");
1568 if (!atmci_test_and_clear_pending(host
,
1572 dev_dbg(&host
->pdev
->dev
, "set completed cmd ready\n");
1574 atmci_set_completed(host
, EVENT_CMD_RDY
);
1575 atmci_command_complete(host
, mrq
->cmd
);
1577 dev_dbg(&host
->pdev
->dev
,
1578 "command with data transfer");
1580 * If there is a command error don't start
1583 if (mrq
->cmd
->error
) {
1584 host
->stop_transfer(host
);
1586 atmci_writel(host
, ATMCI_IDR
,
1587 ATMCI_TXRDY
| ATMCI_RXRDY
1588 | ATMCI_DATA_ERROR_FLAGS
);
1589 state
= STATE_END_REQUEST
;
1591 state
= STATE_DATA_XFER
;
1592 } else if ((!mrq
->data
) && (mrq
->cmd
->flags
& MMC_RSP_BUSY
)) {
1593 dev_dbg(&host
->pdev
->dev
,
1594 "command response need waiting notbusy");
1595 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1596 state
= STATE_WAITING_NOTBUSY
;
1598 state
= STATE_END_REQUEST
;
1602 case STATE_DATA_XFER
:
1603 if (atmci_test_and_clear_pending(host
,
1604 EVENT_DATA_ERROR
)) {
1605 dev_dbg(&host
->pdev
->dev
, "set completed data error\n");
1606 atmci_set_completed(host
, EVENT_DATA_ERROR
);
1607 state
= STATE_END_REQUEST
;
1612 * A data transfer is in progress. The event expected
1613 * to move to the next state depends of data transfer
1614 * type (PDC or DMA). Once transfer done we can move
1615 * to the next step which is WAITING_NOTBUSY in write
1616 * case and directly SENDING_STOP in read case.
1618 dev_dbg(&host
->pdev
->dev
, "FSM: xfer complete?\n");
1619 if (!atmci_test_and_clear_pending(host
,
1620 EVENT_XFER_COMPLETE
))
1623 dev_dbg(&host
->pdev
->dev
,
1624 "(%s) set completed xfer complete\n",
1626 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
1628 if (host
->data
->flags
& MMC_DATA_WRITE
) {
1629 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1630 state
= STATE_WAITING_NOTBUSY
;
1631 } else if (host
->mrq
->stop
) {
1632 atmci_writel(host
, ATMCI_IER
, ATMCI_CMDRDY
);
1633 atmci_send_stop_cmd(host
, data
);
1634 state
= STATE_SENDING_STOP
;
1637 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1639 state
= STATE_END_REQUEST
;
1643 case STATE_WAITING_NOTBUSY
:
1645 * We can be in the state for two reasons: a command
1646 * requiring waiting not busy signal (stop command
1647 * included) or a write operation. In the latest case,
1648 * we need to send a stop command.
1650 dev_dbg(&host
->pdev
->dev
, "FSM: not busy?\n");
1651 if (!atmci_test_and_clear_pending(host
,
1655 dev_dbg(&host
->pdev
->dev
, "set completed not busy\n");
1656 atmci_set_completed(host
, EVENT_NOTBUSY
);
1660 * For some commands such as CMD53, even if
1661 * there is data transfer, there is no stop
1664 if (host
->mrq
->stop
) {
1665 atmci_writel(host
, ATMCI_IER
,
1667 atmci_send_stop_cmd(host
, data
);
1668 state
= STATE_SENDING_STOP
;
1671 data
->bytes_xfered
= data
->blocks
1674 state
= STATE_END_REQUEST
;
1677 state
= STATE_END_REQUEST
;
1680 case STATE_SENDING_STOP
:
1682 * In this state, it is important to set host->data to
1683 * NULL (which is tested in the waiting notbusy state)
1684 * in order to go to the end request state instead of
1685 * sending stop again.
1687 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready?\n");
1688 if (!atmci_test_and_clear_pending(host
,
1692 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready\n");
1694 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1696 atmci_command_complete(host
, mrq
->stop
);
1697 if (mrq
->stop
->error
) {
1698 host
->stop_transfer(host
);
1699 atmci_writel(host
, ATMCI_IDR
,
1700 ATMCI_TXRDY
| ATMCI_RXRDY
1701 | ATMCI_DATA_ERROR_FLAGS
);
1702 state
= STATE_END_REQUEST
;
1704 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1705 state
= STATE_WAITING_NOTBUSY
;
1710 case STATE_END_REQUEST
:
1711 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXRDY
| ATMCI_RXRDY
1712 | ATMCI_DATA_ERROR_FLAGS
);
1713 status
= host
->data_status
;
1714 if (unlikely(status
)) {
1715 host
->stop_transfer(host
);
1717 if (status
& ATMCI_DTOE
) {
1718 data
->error
= -ETIMEDOUT
;
1719 } else if (status
& ATMCI_DCRCE
) {
1720 data
->error
= -EILSEQ
;
1726 atmci_request_end(host
, host
->mrq
);
1730 } while (state
!= prev_state
);
1732 host
->state
= state
;
1734 spin_unlock(&host
->lock
);
1737 static void atmci_read_data_pio(struct atmel_mci
*host
)
1739 struct scatterlist
*sg
= host
->sg
;
1740 void *buf
= sg_virt(sg
);
1741 unsigned int offset
= host
->pio_offset
;
1742 struct mmc_data
*data
= host
->data
;
1745 unsigned int nbytes
= 0;
1748 value
= atmci_readl(host
, ATMCI_RDR
);
1749 if (likely(offset
+ 4 <= sg
->length
)) {
1750 put_unaligned(value
, (u32
*)(buf
+ offset
));
1755 if (offset
== sg
->length
) {
1756 flush_dcache_page(sg_page(sg
));
1757 host
->sg
= sg
= sg_next(sg
);
1765 unsigned int remaining
= sg
->length
- offset
;
1766 memcpy(buf
+ offset
, &value
, remaining
);
1767 nbytes
+= remaining
;
1769 flush_dcache_page(sg_page(sg
));
1770 host
->sg
= sg
= sg_next(sg
);
1774 offset
= 4 - remaining
;
1776 memcpy(buf
, (u8
*)&value
+ remaining
, offset
);
1780 status
= atmci_readl(host
, ATMCI_SR
);
1781 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
1782 atmci_writel(host
, ATMCI_IDR
, (ATMCI_NOTBUSY
| ATMCI_RXRDY
1783 | ATMCI_DATA_ERROR_FLAGS
));
1784 host
->data_status
= status
;
1785 data
->bytes_xfered
+= nbytes
;
1788 } while (status
& ATMCI_RXRDY
);
1790 host
->pio_offset
= offset
;
1791 data
->bytes_xfered
+= nbytes
;
1796 atmci_writel(host
, ATMCI_IDR
, ATMCI_RXRDY
);
1797 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1798 data
->bytes_xfered
+= nbytes
;
1800 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1803 static void atmci_write_data_pio(struct atmel_mci
*host
)
1805 struct scatterlist
*sg
= host
->sg
;
1806 void *buf
= sg_virt(sg
);
1807 unsigned int offset
= host
->pio_offset
;
1808 struct mmc_data
*data
= host
->data
;
1811 unsigned int nbytes
= 0;
1814 if (likely(offset
+ 4 <= sg
->length
)) {
1815 value
= get_unaligned((u32
*)(buf
+ offset
));
1816 atmci_writel(host
, ATMCI_TDR
, value
);
1820 if (offset
== sg
->length
) {
1821 host
->sg
= sg
= sg_next(sg
);
1829 unsigned int remaining
= sg
->length
- offset
;
1832 memcpy(&value
, buf
+ offset
, remaining
);
1833 nbytes
+= remaining
;
1835 host
->sg
= sg
= sg_next(sg
);
1837 atmci_writel(host
, ATMCI_TDR
, value
);
1841 offset
= 4 - remaining
;
1843 memcpy((u8
*)&value
+ remaining
, buf
, offset
);
1844 atmci_writel(host
, ATMCI_TDR
, value
);
1848 status
= atmci_readl(host
, ATMCI_SR
);
1849 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
1850 atmci_writel(host
, ATMCI_IDR
, (ATMCI_NOTBUSY
| ATMCI_TXRDY
1851 | ATMCI_DATA_ERROR_FLAGS
));
1852 host
->data_status
= status
;
1853 data
->bytes_xfered
+= nbytes
;
1856 } while (status
& ATMCI_TXRDY
);
1858 host
->pio_offset
= offset
;
1859 data
->bytes_xfered
+= nbytes
;
1864 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXRDY
);
1865 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1866 data
->bytes_xfered
+= nbytes
;
1868 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1871 static void atmci_sdio_interrupt(struct atmel_mci
*host
, u32 status
)
1875 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1876 struct atmel_mci_slot
*slot
= host
->slot
[i
];
1877 if (slot
&& (status
& slot
->sdio_irq
)) {
1878 mmc_signal_sdio_irq(slot
->mmc
);
1884 static irqreturn_t
atmci_interrupt(int irq
, void *dev_id
)
1886 struct atmel_mci
*host
= dev_id
;
1887 u32 status
, mask
, pending
;
1888 unsigned int pass_count
= 0;
1891 status
= atmci_readl(host
, ATMCI_SR
);
1892 mask
= atmci_readl(host
, ATMCI_IMR
);
1893 pending
= status
& mask
;
1897 if (pending
& ATMCI_DATA_ERROR_FLAGS
) {
1898 dev_dbg(&host
->pdev
->dev
, "IRQ: data error\n");
1899 atmci_writel(host
, ATMCI_IDR
, ATMCI_DATA_ERROR_FLAGS
1900 | ATMCI_RXRDY
| ATMCI_TXRDY
1901 | ATMCI_ENDRX
| ATMCI_ENDTX
1902 | ATMCI_RXBUFF
| ATMCI_TXBUFE
);
1904 host
->data_status
= status
;
1905 dev_dbg(&host
->pdev
->dev
, "set pending data error\n");
1907 atmci_set_pending(host
, EVENT_DATA_ERROR
);
1908 tasklet_schedule(&host
->tasklet
);
1911 if (pending
& ATMCI_TXBUFE
) {
1912 dev_dbg(&host
->pdev
->dev
, "IRQ: tx buffer empty\n");
1913 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXBUFE
);
1914 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDTX
);
1916 * We can receive this interruption before having configured
1917 * the second pdc buffer, so we need to reconfigure first and
1918 * second buffers again
1920 if (host
->data_size
) {
1921 atmci_pdc_set_both_buf(host
, XFER_TRANSMIT
);
1922 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDTX
);
1923 atmci_writel(host
, ATMCI_IER
, ATMCI_TXBUFE
);
1925 atmci_pdc_complete(host
);
1927 } else if (pending
& ATMCI_ENDTX
) {
1928 dev_dbg(&host
->pdev
->dev
, "IRQ: end of tx buffer\n");
1929 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDTX
);
1931 if (host
->data_size
) {
1932 atmci_pdc_set_single_buf(host
,
1933 XFER_TRANSMIT
, PDC_SECOND_BUF
);
1934 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDTX
);
1938 if (pending
& ATMCI_RXBUFF
) {
1939 dev_dbg(&host
->pdev
->dev
, "IRQ: rx buffer full\n");
1940 atmci_writel(host
, ATMCI_IDR
, ATMCI_RXBUFF
);
1941 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDRX
);
1943 * We can receive this interruption before having configured
1944 * the second pdc buffer, so we need to reconfigure first and
1945 * second buffers again
1947 if (host
->data_size
) {
1948 atmci_pdc_set_both_buf(host
, XFER_RECEIVE
);
1949 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDRX
);
1950 atmci_writel(host
, ATMCI_IER
, ATMCI_RXBUFF
);
1952 atmci_pdc_complete(host
);
1954 } else if (pending
& ATMCI_ENDRX
) {
1955 dev_dbg(&host
->pdev
->dev
, "IRQ: end of rx buffer\n");
1956 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDRX
);
1958 if (host
->data_size
) {
1959 atmci_pdc_set_single_buf(host
,
1960 XFER_RECEIVE
, PDC_SECOND_BUF
);
1961 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDRX
);
1966 * First mci IPs, so mainly the ones having pdc, have some
1967 * issues with the notbusy signal. You can't get it after
1968 * data transmission if you have not sent a stop command.
1969 * The appropriate workaround is to use the BLKE signal.
1971 if (pending
& ATMCI_BLKE
) {
1972 dev_dbg(&host
->pdev
->dev
, "IRQ: blke\n");
1973 atmci_writel(host
, ATMCI_IDR
, ATMCI_BLKE
);
1975 dev_dbg(&host
->pdev
->dev
, "set pending notbusy\n");
1976 atmci_set_pending(host
, EVENT_NOTBUSY
);
1977 tasklet_schedule(&host
->tasklet
);
1980 if (pending
& ATMCI_NOTBUSY
) {
1981 dev_dbg(&host
->pdev
->dev
, "IRQ: not_busy\n");
1982 atmci_writel(host
, ATMCI_IDR
, ATMCI_NOTBUSY
);
1984 dev_dbg(&host
->pdev
->dev
, "set pending notbusy\n");
1985 atmci_set_pending(host
, EVENT_NOTBUSY
);
1986 tasklet_schedule(&host
->tasklet
);
1989 if (pending
& ATMCI_RXRDY
)
1990 atmci_read_data_pio(host
);
1991 if (pending
& ATMCI_TXRDY
)
1992 atmci_write_data_pio(host
);
1994 if (pending
& ATMCI_CMDRDY
) {
1995 dev_dbg(&host
->pdev
->dev
, "IRQ: cmd ready\n");
1996 atmci_writel(host
, ATMCI_IDR
, ATMCI_CMDRDY
);
1997 host
->cmd_status
= status
;
1999 dev_dbg(&host
->pdev
->dev
, "set pending cmd rdy\n");
2000 atmci_set_pending(host
, EVENT_CMD_RDY
);
2001 tasklet_schedule(&host
->tasklet
);
2004 if (pending
& (ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
))
2005 atmci_sdio_interrupt(host
, status
);
2007 } while (pass_count
++ < 5);
2009 return pass_count
? IRQ_HANDLED
: IRQ_NONE
;
2012 static irqreturn_t
atmci_detect_interrupt(int irq
, void *dev_id
)
2014 struct atmel_mci_slot
*slot
= dev_id
;
2017 * Disable interrupts until the pin has stabilized and check
2018 * the state then. Use mod_timer() since we may be in the
2019 * middle of the timer routine when this interrupt triggers.
2021 disable_irq_nosync(irq
);
2022 mod_timer(&slot
->detect_timer
, jiffies
+ msecs_to_jiffies(20));
2027 static int __init
atmci_init_slot(struct atmel_mci
*host
,
2028 struct mci_slot_pdata
*slot_data
, unsigned int id
,
2029 u32 sdc_reg
, u32 sdio_irq
)
2031 struct mmc_host
*mmc
;
2032 struct atmel_mci_slot
*slot
;
2034 mmc
= mmc_alloc_host(sizeof(struct atmel_mci_slot
), &host
->pdev
->dev
);
2038 slot
= mmc_priv(mmc
);
2041 slot
->detect_pin
= slot_data
->detect_pin
;
2042 slot
->wp_pin
= slot_data
->wp_pin
;
2043 slot
->detect_is_active_high
= slot_data
->detect_is_active_high
;
2044 slot
->sdc_reg
= sdc_reg
;
2045 slot
->sdio_irq
= sdio_irq
;
2047 mmc
->ops
= &atmci_ops
;
2048 mmc
->f_min
= DIV_ROUND_UP(host
->bus_hz
, 512);
2049 mmc
->f_max
= host
->bus_hz
/ 2;
2050 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
2052 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
2053 if (host
->caps
.has_highspeed
)
2054 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
2056 * Without the read/write proof capability, it is strongly suggested to
2057 * use only one bit for data to prevent fifo underruns and overruns
2058 * which will corrupt data.
2060 if ((slot_data
->bus_width
>= 4) && host
->caps
.has_rwproof
)
2061 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
2063 if (atmci_get_version(host
) < 0x200) {
2064 mmc
->max_segs
= 256;
2065 mmc
->max_blk_size
= 4095;
2066 mmc
->max_blk_count
= 256;
2067 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
2068 mmc
->max_seg_size
= mmc
->max_blk_size
* mmc
->max_segs
;
2071 mmc
->max_req_size
= 32768 * 512;
2072 mmc
->max_blk_size
= 32768;
2073 mmc
->max_blk_count
= 512;
2076 /* Assume card is present initially */
2077 set_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
2078 if (gpio_is_valid(slot
->detect_pin
)) {
2079 if (gpio_request(slot
->detect_pin
, "mmc_detect")) {
2080 dev_dbg(&mmc
->class_dev
, "no detect pin available\n");
2081 slot
->detect_pin
= -EBUSY
;
2082 } else if (gpio_get_value(slot
->detect_pin
) ^
2083 slot
->detect_is_active_high
) {
2084 clear_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
2088 if (!gpio_is_valid(slot
->detect_pin
))
2089 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
2091 if (gpio_is_valid(slot
->wp_pin
)) {
2092 if (gpio_request(slot
->wp_pin
, "mmc_wp")) {
2093 dev_dbg(&mmc
->class_dev
, "no WP pin available\n");
2094 slot
->wp_pin
= -EBUSY
;
2098 host
->slot
[id
] = slot
;
2101 if (gpio_is_valid(slot
->detect_pin
)) {
2104 setup_timer(&slot
->detect_timer
, atmci_detect_change
,
2105 (unsigned long)slot
);
2107 ret
= request_irq(gpio_to_irq(slot
->detect_pin
),
2108 atmci_detect_interrupt
,
2109 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
2110 "mmc-detect", slot
);
2112 dev_dbg(&mmc
->class_dev
,
2113 "could not request IRQ %d for detect pin\n",
2114 gpio_to_irq(slot
->detect_pin
));
2115 gpio_free(slot
->detect_pin
);
2116 slot
->detect_pin
= -EBUSY
;
2120 atmci_init_debugfs(slot
);
2125 static void __exit
atmci_cleanup_slot(struct atmel_mci_slot
*slot
,
2128 /* Debugfs stuff is cleaned up by mmc core */
2130 set_bit(ATMCI_SHUTDOWN
, &slot
->flags
);
2133 mmc_remove_host(slot
->mmc
);
2135 if (gpio_is_valid(slot
->detect_pin
)) {
2136 int pin
= slot
->detect_pin
;
2138 free_irq(gpio_to_irq(pin
), slot
);
2139 del_timer_sync(&slot
->detect_timer
);
2142 if (gpio_is_valid(slot
->wp_pin
))
2143 gpio_free(slot
->wp_pin
);
2145 slot
->host
->slot
[id
] = NULL
;
2146 mmc_free_host(slot
->mmc
);
2149 static bool atmci_filter(struct dma_chan
*chan
, void *slave
)
2151 struct mci_dma_data
*sl
= slave
;
2153 if (sl
&& find_slave_dev(sl
) == chan
->device
->dev
) {
2154 chan
->private = slave_data_ptr(sl
);
2161 static bool atmci_configure_dma(struct atmel_mci
*host
)
2163 struct mci_platform_data
*pdata
;
2168 pdata
= host
->pdev
->dev
.platform_data
;
2170 if (pdata
&& find_slave_dev(pdata
->dma_slave
)) {
2171 dma_cap_mask_t mask
;
2173 /* Try to grab a DMA channel */
2175 dma_cap_set(DMA_SLAVE
, mask
);
2177 dma_request_channel(mask
, atmci_filter
, pdata
->dma_slave
);
2179 if (!host
->dma
.chan
) {
2180 dev_warn(&host
->pdev
->dev
, "no DMA channel available\n");
2183 dev_info(&host
->pdev
->dev
,
2184 "using %s for DMA transfers\n",
2185 dma_chan_name(host
->dma
.chan
));
2187 host
->dma_conf
.src_addr
= host
->mapbase
+ ATMCI_RDR
;
2188 host
->dma_conf
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2189 host
->dma_conf
.src_maxburst
= 1;
2190 host
->dma_conf
.dst_addr
= host
->mapbase
+ ATMCI_TDR
;
2191 host
->dma_conf
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2192 host
->dma_conf
.dst_maxburst
= 1;
2193 host
->dma_conf
.device_fc
= false;
2199 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2200 * HSMCI provides DMA support and a new config register but no more supports
2203 static void __init
atmci_get_cap(struct atmel_mci
*host
)
2205 unsigned int version
;
2207 version
= atmci_get_version(host
);
2208 dev_info(&host
->pdev
->dev
,
2209 "version: 0x%x\n", version
);
2211 host
->caps
.has_dma
= 0;
2212 host
->caps
.has_pdc
= 1;
2213 host
->caps
.has_cfg_reg
= 0;
2214 host
->caps
.has_cstor_reg
= 0;
2215 host
->caps
.has_highspeed
= 0;
2216 host
->caps
.has_rwproof
= 0;
2217 host
->caps
.has_odd_clk_div
= 0;
2218 host
->caps
.has_bad_data_ordering
= 1;
2219 host
->caps
.need_reset_after_xfer
= 1;
2220 host
->caps
.need_blksz_mul_4
= 1;
2222 /* keep only major version number */
2223 switch (version
& 0xf00) {
2225 host
->caps
.has_odd_clk_div
= 1;
2228 #ifdef CONFIG_AT_HDMAC
2229 host
->caps
.has_dma
= 1;
2231 dev_info(&host
->pdev
->dev
,
2232 "has dma capability but dma engine is not selected, then use pio\n");
2234 host
->caps
.has_pdc
= 0;
2235 host
->caps
.has_cfg_reg
= 1;
2236 host
->caps
.has_cstor_reg
= 1;
2237 host
->caps
.has_highspeed
= 1;
2239 host
->caps
.has_rwproof
= 1;
2240 host
->caps
.need_blksz_mul_4
= 0;
2242 host
->caps
.has_bad_data_ordering
= 0;
2243 host
->caps
.need_reset_after_xfer
= 0;
2247 host
->caps
.has_pdc
= 0;
2248 dev_warn(&host
->pdev
->dev
,
2249 "Unmanaged mci version, set minimum capabilities\n");
2254 static int __init
atmci_probe(struct platform_device
*pdev
)
2256 struct mci_platform_data
*pdata
;
2257 struct atmel_mci
*host
;
2258 struct resource
*regs
;
2259 unsigned int nr_slots
;
2263 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2266 pdata
= pdev
->dev
.platform_data
;
2269 irq
= platform_get_irq(pdev
, 0);
2273 host
= kzalloc(sizeof(struct atmel_mci
), GFP_KERNEL
);
2278 spin_lock_init(&host
->lock
);
2279 INIT_LIST_HEAD(&host
->queue
);
2281 host
->mck
= clk_get(&pdev
->dev
, "mci_clk");
2282 if (IS_ERR(host
->mck
)) {
2283 ret
= PTR_ERR(host
->mck
);
2288 host
->regs
= ioremap(regs
->start
, resource_size(regs
));
2292 clk_enable(host
->mck
);
2293 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
2294 host
->bus_hz
= clk_get_rate(host
->mck
);
2295 clk_disable(host
->mck
);
2297 host
->mapbase
= regs
->start
;
2299 tasklet_init(&host
->tasklet
, atmci_tasklet_func
, (unsigned long)host
);
2301 ret
= request_irq(irq
, atmci_interrupt
, 0, dev_name(&pdev
->dev
), host
);
2303 goto err_request_irq
;
2305 /* Get MCI capabilities and set operations according to it */
2306 atmci_get_cap(host
);
2307 if (host
->caps
.has_dma
&& atmci_configure_dma(host
)) {
2308 host
->prepare_data
= &atmci_prepare_data_dma
;
2309 host
->submit_data
= &atmci_submit_data_dma
;
2310 host
->stop_transfer
= &atmci_stop_transfer_dma
;
2311 } else if (host
->caps
.has_pdc
) {
2312 dev_info(&pdev
->dev
, "using PDC\n");
2313 host
->prepare_data
= &atmci_prepare_data_pdc
;
2314 host
->submit_data
= &atmci_submit_data_pdc
;
2315 host
->stop_transfer
= &atmci_stop_transfer_pdc
;
2317 dev_info(&pdev
->dev
, "using PIO\n");
2318 host
->prepare_data
= &atmci_prepare_data
;
2319 host
->submit_data
= &atmci_submit_data
;
2320 host
->stop_transfer
= &atmci_stop_transfer
;
2323 platform_set_drvdata(pdev
, host
);
2325 setup_timer(&host
->timer
, atmci_timeout_timer
, (unsigned long)host
);
2327 /* We need at least one slot to succeed */
2330 if (pdata
->slot
[0].bus_width
) {
2331 ret
= atmci_init_slot(host
, &pdata
->slot
[0],
2332 0, ATMCI_SDCSEL_SLOT_A
, ATMCI_SDIOIRQA
);
2335 host
->buf_size
= host
->slot
[0]->mmc
->max_req_size
;
2338 if (pdata
->slot
[1].bus_width
) {
2339 ret
= atmci_init_slot(host
, &pdata
->slot
[1],
2340 1, ATMCI_SDCSEL_SLOT_B
, ATMCI_SDIOIRQB
);
2343 if (host
->slot
[1]->mmc
->max_req_size
> host
->buf_size
)
2345 host
->slot
[1]->mmc
->max_req_size
;
2350 dev_err(&pdev
->dev
, "init failed: no slot defined\n");
2354 if (!host
->caps
.has_rwproof
) {
2355 host
->buffer
= dma_alloc_coherent(&pdev
->dev
, host
->buf_size
,
2356 &host
->buf_phys_addr
,
2358 if (!host
->buffer
) {
2360 dev_err(&pdev
->dev
, "buffer allocation failed\n");
2365 dev_info(&pdev
->dev
,
2366 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2367 host
->mapbase
, irq
, nr_slots
);
2373 dma_release_channel(host
->dma
.chan
);
2374 free_irq(irq
, host
);
2376 iounmap(host
->regs
);
2384 static int __exit
atmci_remove(struct platform_device
*pdev
)
2386 struct atmel_mci
*host
= platform_get_drvdata(pdev
);
2389 platform_set_drvdata(pdev
, NULL
);
2392 dma_free_coherent(&pdev
->dev
, host
->buf_size
,
2393 host
->buffer
, host
->buf_phys_addr
);
2395 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
2397 atmci_cleanup_slot(host
->slot
[i
], i
);
2400 clk_enable(host
->mck
);
2401 atmci_writel(host
, ATMCI_IDR
, ~0UL);
2402 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIDIS
);
2403 atmci_readl(host
, ATMCI_SR
);
2404 clk_disable(host
->mck
);
2406 #ifdef CONFIG_MMC_ATMELMCI_DMA
2408 dma_release_channel(host
->dma
.chan
);
2411 free_irq(platform_get_irq(pdev
, 0), host
);
2412 iounmap(host
->regs
);
2421 static int atmci_suspend(struct device
*dev
)
2423 struct atmel_mci
*host
= dev_get_drvdata(dev
);
2426 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
2427 struct atmel_mci_slot
*slot
= host
->slot
[i
];
2432 ret
= mmc_suspend_host(slot
->mmc
);
2435 slot
= host
->slot
[i
];
2437 && test_bit(ATMCI_SUSPENDED
, &slot
->flags
)) {
2438 mmc_resume_host(host
->slot
[i
]->mmc
);
2439 clear_bit(ATMCI_SUSPENDED
, &slot
->flags
);
2444 set_bit(ATMCI_SUSPENDED
, &slot
->flags
);
2451 static int atmci_resume(struct device
*dev
)
2453 struct atmel_mci
*host
= dev_get_drvdata(dev
);
2457 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
2458 struct atmel_mci_slot
*slot
= host
->slot
[i
];
2461 slot
= host
->slot
[i
];
2464 if (!test_bit(ATMCI_SUSPENDED
, &slot
->flags
))
2466 err
= mmc_resume_host(slot
->mmc
);
2470 clear_bit(ATMCI_SUSPENDED
, &slot
->flags
);
2475 static SIMPLE_DEV_PM_OPS(atmci_pm
, atmci_suspend
, atmci_resume
);
2476 #define ATMCI_PM_OPS (&atmci_pm)
2478 #define ATMCI_PM_OPS NULL
2481 static struct platform_driver atmci_driver
= {
2482 .remove
= __exit_p(atmci_remove
),
2484 .name
= "atmel_mci",
2489 static int __init
atmci_init(void)
2491 return platform_driver_probe(&atmci_driver
, atmci_probe
);
2494 static void __exit
atmci_exit(void)
2496 platform_driver_unregister(&atmci_driver
);
2499 late_initcall(atmci_init
); /* try to load after dma driver when built-in */
2500 module_exit(atmci_exit
);
2502 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
2503 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2504 MODULE_LICENSE("GPL v2");