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>
21 #include <linux/ioport.h>
22 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/of_gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/scatterlist.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/stat.h>
31 #include <linux/types.h>
32 #include <linux/platform_data/mmc-atmel-mci.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/sdio.h>
37 #include <linux/atmel-mci.h>
38 #include <linux/atmel_pdc.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/pinctrl/consumer.h>
43 #include <asm/cacheflush.h>
45 #include <asm/unaligned.h>
47 #include "atmel-mci-regs.h"
49 #define AUTOSUSPEND_DELAY 50
51 #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
52 #define ATMCI_DMA_THRESHOLD 16
61 enum atmel_mci_state
{
65 STATE_WAITING_NOTBUSY
,
80 struct atmel_mci_caps
{
81 bool has_dma_conf_reg
;
88 bool has_bad_data_ordering
;
89 bool need_reset_after_xfer
;
90 bool need_blksz_mul_4
;
91 bool need_notbusy_for_read_ops
;
94 struct atmel_mci_dma
{
95 struct dma_chan
*chan
;
96 struct dma_async_tx_descriptor
*data_desc
;
100 * struct atmel_mci - MMC controller state shared between all slots
101 * @lock: Spinlock protecting the queue and associated data.
102 * @regs: Pointer to MMIO registers.
103 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
104 * @pio_offset: Offset into the current scatterlist entry.
105 * @buffer: Buffer used if we don't have the r/w proof capability. We
106 * don't have the time to switch pdc buffers so we have to use only
107 * one buffer for the full transaction.
108 * @buf_size: size of the buffer.
109 * @phys_buf_addr: buffer address needed for pdc.
110 * @cur_slot: The slot which is currently using the controller.
111 * @mrq: The request currently being processed on @cur_slot,
112 * or NULL if the controller is idle.
113 * @cmd: The command currently being sent to the card, or NULL.
114 * @data: The data currently being transferred, or NULL if no data
115 * transfer is in progress.
116 * @data_size: just data->blocks * data->blksz.
117 * @dma: DMA client state.
118 * @data_chan: DMA channel being used for the current data transfer.
119 * @cmd_status: Snapshot of SR taken upon completion of the current
120 * command. Only valid when EVENT_CMD_COMPLETE is pending.
121 * @data_status: Snapshot of SR taken upon completion of the current
122 * data transfer. Only valid when EVENT_DATA_COMPLETE or
123 * EVENT_DATA_ERROR is pending.
124 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
126 * @tasklet: Tasklet running the request state machine.
127 * @pending_events: Bitmask of events flagged by the interrupt handler
128 * to be processed by the tasklet.
129 * @completed_events: Bitmask of events which the state machine has
131 * @state: Tasklet state.
132 * @queue: List of slots waiting for access to the controller.
133 * @need_clock_update: Update the clock rate before the next request.
134 * @need_reset: Reset controller before next request.
135 * @timer: Timer to balance the data timeout error flag which cannot rise.
136 * @mode_reg: Value of the MR register.
137 * @cfg_reg: Value of the CFG register.
138 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
139 * rate and timeout calculations.
140 * @mapbase: Physical address of the MMIO registers.
141 * @mck: The peripheral bus clock hooked up to the MMC controller.
142 * @pdev: Platform device associated with the MMC controller.
143 * @slot: Slots sharing this MMC controller.
144 * @caps: MCI capabilities depending on MCI version.
145 * @prepare_data: function to setup MCI before data transfer which
146 * depends on MCI capabilities.
147 * @submit_data: function to start data transfer which depends on MCI
149 * @stop_transfer: function to stop data transfer which depends on MCI
155 * @lock is a softirq-safe spinlock protecting @queue as well as
156 * @cur_slot, @mrq and @state. These must always be updated
157 * at the same time while holding @lock.
159 * @lock also protects mode_reg and need_clock_update since these are
160 * used to synchronize mode register updates with the queue
163 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
164 * and must always be written at the same time as the slot is added to
167 * @pending_events and @completed_events are accessed using atomic bit
168 * operations, so they don't need any locking.
170 * None of the fields touched by the interrupt handler need any
171 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
172 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
173 * interrupts must be disabled and @data_status updated with a
174 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
175 * CMDRDY interrupt must be disabled and @cmd_status updated with a
176 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
177 * bytes_xfered field of @data must be written. This is ensured by
184 struct scatterlist
*sg
;
186 unsigned int pio_offset
;
187 unsigned int *buffer
;
188 unsigned int buf_size
;
189 dma_addr_t buf_phys_addr
;
191 struct atmel_mci_slot
*cur_slot
;
192 struct mmc_request
*mrq
;
193 struct mmc_command
*cmd
;
194 struct mmc_data
*data
;
195 unsigned int data_size
;
197 struct atmel_mci_dma dma
;
198 struct dma_chan
*data_chan
;
199 struct dma_slave_config dma_conf
;
205 struct tasklet_struct tasklet
;
206 unsigned long pending_events
;
207 unsigned long completed_events
;
208 enum atmel_mci_state state
;
209 struct list_head queue
;
211 bool need_clock_update
;
213 struct timer_list timer
;
216 unsigned long bus_hz
;
217 unsigned long mapbase
;
219 struct platform_device
*pdev
;
221 struct atmel_mci_slot
*slot
[ATMCI_MAX_NR_SLOTS
];
223 struct atmel_mci_caps caps
;
225 u32 (*prepare_data
)(struct atmel_mci
*host
, struct mmc_data
*data
);
226 void (*submit_data
)(struct atmel_mci
*host
, struct mmc_data
*data
);
227 void (*stop_transfer
)(struct atmel_mci
*host
);
231 * struct atmel_mci_slot - MMC slot state
232 * @mmc: The mmc_host representing this slot.
233 * @host: The MMC controller this slot is using.
234 * @sdc_reg: Value of SDCR to be written before using this slot.
235 * @sdio_irq: SDIO irq mask for this slot.
236 * @mrq: mmc_request currently being processed or waiting to be
237 * processed, or NULL when the slot is idle.
238 * @queue_node: List node for placing this node in the @queue list of
240 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
241 * @flags: Random state bits associated with the slot.
242 * @detect_pin: GPIO pin used for card detection, or negative if not
244 * @wp_pin: GPIO pin used for card write protect sending, or negative
246 * @detect_is_active_high: The state of the detect pin when it is active.
247 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
249 struct atmel_mci_slot
{
250 struct mmc_host
*mmc
;
251 struct atmel_mci
*host
;
256 struct mmc_request
*mrq
;
257 struct list_head queue_node
;
261 #define ATMCI_CARD_PRESENT 0
262 #define ATMCI_CARD_NEED_INIT 1
263 #define ATMCI_SHUTDOWN 2
267 bool detect_is_active_high
;
269 struct timer_list detect_timer
;
272 #define atmci_test_and_clear_pending(host, event) \
273 test_and_clear_bit(event, &host->pending_events)
274 #define atmci_set_completed(host, event) \
275 set_bit(event, &host->completed_events)
276 #define atmci_set_pending(host, event) \
277 set_bit(event, &host->pending_events)
280 * The debugfs stuff below is mostly optimized away when
281 * CONFIG_DEBUG_FS is not set.
283 static int atmci_req_show(struct seq_file
*s
, void *v
)
285 struct atmel_mci_slot
*slot
= s
->private;
286 struct mmc_request
*mrq
;
287 struct mmc_command
*cmd
;
288 struct mmc_command
*stop
;
289 struct mmc_data
*data
;
291 /* Make sure we get a consistent snapshot */
292 spin_lock_bh(&slot
->host
->lock
);
302 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
303 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
304 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
305 cmd
->resp
[3], cmd
->error
);
307 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
308 data
->bytes_xfered
, data
->blocks
,
309 data
->blksz
, data
->flags
, data
->error
);
312 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
313 stop
->opcode
, stop
->arg
, stop
->flags
,
314 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
315 stop
->resp
[3], stop
->error
);
318 spin_unlock_bh(&slot
->host
->lock
);
323 static int atmci_req_open(struct inode
*inode
, struct file
*file
)
325 return single_open(file
, atmci_req_show
, inode
->i_private
);
328 static const struct file_operations atmci_req_fops
= {
329 .owner
= THIS_MODULE
,
330 .open
= atmci_req_open
,
333 .release
= single_release
,
336 static void atmci_show_status_reg(struct seq_file
*s
,
337 const char *regname
, u32 value
)
339 static const char *sr_bit
[] = {
370 seq_printf(s
, "%s:\t0x%08x", regname
, value
);
371 for (i
= 0; i
< ARRAY_SIZE(sr_bit
); i
++) {
372 if (value
& (1 << i
)) {
374 seq_printf(s
, " %s", sr_bit
[i
]);
376 seq_puts(s
, " UNKNOWN");
382 static int atmci_regs_show(struct seq_file
*s
, void *v
)
384 struct atmel_mci
*host
= s
->private;
389 buf
= kmalloc(ATMCI_REGS_SIZE
, GFP_KERNEL
);
393 pm_runtime_get_sync(&host
->pdev
->dev
);
396 * Grab a more or less consistent snapshot. Note that we're
397 * not disabling interrupts, so IMR and SR may not be
400 spin_lock_bh(&host
->lock
);
401 memcpy_fromio(buf
, host
->regs
, ATMCI_REGS_SIZE
);
402 spin_unlock_bh(&host
->lock
);
404 pm_runtime_mark_last_busy(&host
->pdev
->dev
);
405 pm_runtime_put_autosuspend(&host
->pdev
->dev
);
407 seq_printf(s
, "MR:\t0x%08x%s%s ",
409 buf
[ATMCI_MR
/ 4] & ATMCI_MR_RDPROOF
? " RDPROOF" : "",
410 buf
[ATMCI_MR
/ 4] & ATMCI_MR_WRPROOF
? " WRPROOF" : "");
411 if (host
->caps
.has_odd_clk_div
)
412 seq_printf(s
, "{CLKDIV,CLKODD}=%u\n",
413 ((buf
[ATMCI_MR
/ 4] & 0xff) << 1)
414 | ((buf
[ATMCI_MR
/ 4] >> 16) & 1));
416 seq_printf(s
, "CLKDIV=%u\n",
417 (buf
[ATMCI_MR
/ 4] & 0xff));
418 seq_printf(s
, "DTOR:\t0x%08x\n", buf
[ATMCI_DTOR
/ 4]);
419 seq_printf(s
, "SDCR:\t0x%08x\n", buf
[ATMCI_SDCR
/ 4]);
420 seq_printf(s
, "ARGR:\t0x%08x\n", buf
[ATMCI_ARGR
/ 4]);
421 seq_printf(s
, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
423 buf
[ATMCI_BLKR
/ 4] & 0xffff,
424 (buf
[ATMCI_BLKR
/ 4] >> 16) & 0xffff);
425 if (host
->caps
.has_cstor_reg
)
426 seq_printf(s
, "CSTOR:\t0x%08x\n", buf
[ATMCI_CSTOR
/ 4]);
428 /* Don't read RSPR and RDR; it will consume the data there */
430 atmci_show_status_reg(s
, "SR", buf
[ATMCI_SR
/ 4]);
431 atmci_show_status_reg(s
, "IMR", buf
[ATMCI_IMR
/ 4]);
433 if (host
->caps
.has_dma_conf_reg
) {
436 val
= buf
[ATMCI_DMA
/ 4];
437 seq_printf(s
, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
440 1 << (((val
>> 4) & 3) + 1) : 1,
441 val
& ATMCI_DMAEN
? " DMAEN" : "");
443 if (host
->caps
.has_cfg_reg
) {
446 val
= buf
[ATMCI_CFG
/ 4];
447 seq_printf(s
, "CFG:\t0x%08x%s%s%s%s\n",
449 val
& ATMCI_CFG_FIFOMODE_1DATA
? " FIFOMODE_ONE_DATA" : "",
450 val
& ATMCI_CFG_FERRCTRL_COR
? " FERRCTRL_CLEAR_ON_READ" : "",
451 val
& ATMCI_CFG_HSMODE
? " HSMODE" : "",
452 val
& ATMCI_CFG_LSYNC
? " LSYNC" : "");
460 static int atmci_regs_open(struct inode
*inode
, struct file
*file
)
462 return single_open(file
, atmci_regs_show
, inode
->i_private
);
465 static const struct file_operations atmci_regs_fops
= {
466 .owner
= THIS_MODULE
,
467 .open
= atmci_regs_open
,
470 .release
= single_release
,
473 static void atmci_init_debugfs(struct atmel_mci_slot
*slot
)
475 struct mmc_host
*mmc
= slot
->mmc
;
476 struct atmel_mci
*host
= slot
->host
;
480 root
= mmc
->debugfs_root
;
484 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
491 node
= debugfs_create_file("req", S_IRUSR
, root
, slot
, &atmci_req_fops
);
495 node
= debugfs_create_u32("state", S_IRUSR
, root
, (u32
*)&host
->state
);
499 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
500 (u32
*)&host
->pending_events
);
504 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
505 (u32
*)&host
->completed_events
);
512 dev_err(&mmc
->class_dev
, "failed to initialize debugfs for slot\n");
515 #if defined(CONFIG_OF)
516 static const struct of_device_id atmci_dt_ids
[] = {
517 { .compatible
= "atmel,hsmci" },
521 MODULE_DEVICE_TABLE(of
, atmci_dt_ids
);
523 static struct mci_platform_data
*
524 atmci_of_init(struct platform_device
*pdev
)
526 struct device_node
*np
= pdev
->dev
.of_node
;
527 struct device_node
*cnp
;
528 struct mci_platform_data
*pdata
;
532 dev_err(&pdev
->dev
, "device node not found\n");
533 return ERR_PTR(-EINVAL
);
536 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
538 dev_err(&pdev
->dev
, "could not allocate memory for pdata\n");
539 return ERR_PTR(-ENOMEM
);
542 for_each_child_of_node(np
, cnp
) {
543 if (of_property_read_u32(cnp
, "reg", &slot_id
)) {
544 dev_warn(&pdev
->dev
, "reg property is missing for %s\n",
549 if (slot_id
>= ATMCI_MAX_NR_SLOTS
) {
550 dev_warn(&pdev
->dev
, "can't have more than %d slots\n",
555 if (of_property_read_u32(cnp
, "bus-width",
556 &pdata
->slot
[slot_id
].bus_width
))
557 pdata
->slot
[slot_id
].bus_width
= 1;
559 pdata
->slot
[slot_id
].detect_pin
=
560 of_get_named_gpio(cnp
, "cd-gpios", 0);
562 pdata
->slot
[slot_id
].detect_is_active_high
=
563 of_property_read_bool(cnp
, "cd-inverted");
565 pdata
->slot
[slot_id
].non_removable
=
566 of_property_read_bool(cnp
, "non-removable");
568 pdata
->slot
[slot_id
].wp_pin
=
569 of_get_named_gpio(cnp
, "wp-gpios", 0);
574 #else /* CONFIG_OF */
575 static inline struct mci_platform_data
*
576 atmci_of_init(struct platform_device
*dev
)
578 return ERR_PTR(-EINVAL
);
582 static inline unsigned int atmci_get_version(struct atmel_mci
*host
)
584 return atmci_readl(host
, ATMCI_VERSION
) & 0x00000fff;
587 static void atmci_timeout_timer(unsigned long data
)
589 struct atmel_mci
*host
;
591 host
= (struct atmel_mci
*)data
;
593 dev_dbg(&host
->pdev
->dev
, "software timeout\n");
595 if (host
->mrq
->cmd
->data
) {
596 host
->mrq
->cmd
->data
->error
= -ETIMEDOUT
;
599 * With some SDIO modules, sometimes DMA transfer hangs. If
600 * stop_transfer() is not called then the DMA request is not
601 * removed, following ones are queued and never computed.
603 if (host
->state
== STATE_DATA_XFER
)
604 host
->stop_transfer(host
);
606 host
->mrq
->cmd
->error
= -ETIMEDOUT
;
609 host
->need_reset
= 1;
610 host
->state
= STATE_END_REQUEST
;
612 tasklet_schedule(&host
->tasklet
);
615 static inline unsigned int atmci_ns_to_clocks(struct atmel_mci
*host
,
619 * It is easier here to use us instead of ns for the timeout,
620 * it prevents from overflows during calculation.
622 unsigned int us
= DIV_ROUND_UP(ns
, 1000);
624 /* Maximum clock frequency is host->bus_hz/2 */
625 return us
* (DIV_ROUND_UP(host
->bus_hz
, 2000000));
628 static void atmci_set_timeout(struct atmel_mci
*host
,
629 struct atmel_mci_slot
*slot
, struct mmc_data
*data
)
631 static unsigned dtomul_to_shift
[] = {
632 0, 4, 7, 8, 10, 12, 16, 20
638 timeout
= atmci_ns_to_clocks(host
, data
->timeout_ns
)
639 + data
->timeout_clks
;
641 for (dtomul
= 0; dtomul
< 8; dtomul
++) {
642 unsigned shift
= dtomul_to_shift
[dtomul
];
643 dtocyc
= (timeout
+ (1 << shift
) - 1) >> shift
;
653 dev_vdbg(&slot
->mmc
->class_dev
, "setting timeout to %u cycles\n",
654 dtocyc
<< dtomul_to_shift
[dtomul
]);
655 atmci_writel(host
, ATMCI_DTOR
, (ATMCI_DTOMUL(dtomul
) | ATMCI_DTOCYC(dtocyc
)));
659 * Return mask with command flags to be enabled for this command.
661 static u32
atmci_prepare_command(struct mmc_host
*mmc
,
662 struct mmc_command
*cmd
)
664 struct mmc_data
*data
;
667 cmd
->error
= -EINPROGRESS
;
669 cmdr
= ATMCI_CMDR_CMDNB(cmd
->opcode
);
671 if (cmd
->flags
& MMC_RSP_PRESENT
) {
672 if (cmd
->flags
& MMC_RSP_136
)
673 cmdr
|= ATMCI_CMDR_RSPTYP_136BIT
;
675 cmdr
|= ATMCI_CMDR_RSPTYP_48BIT
;
679 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
680 * it's too difficult to determine whether this is an ACMD or
681 * not. Better make it 64.
683 cmdr
|= ATMCI_CMDR_MAXLAT_64CYC
;
685 if (mmc
->ios
.bus_mode
== MMC_BUSMODE_OPENDRAIN
)
686 cmdr
|= ATMCI_CMDR_OPDCMD
;
690 cmdr
|= ATMCI_CMDR_START_XFER
;
692 if (cmd
->opcode
== SD_IO_RW_EXTENDED
) {
693 cmdr
|= ATMCI_CMDR_SDIO_BLOCK
;
695 if (data
->flags
& MMC_DATA_STREAM
)
696 cmdr
|= ATMCI_CMDR_STREAM
;
697 else if (data
->blocks
> 1)
698 cmdr
|= ATMCI_CMDR_MULTI_BLOCK
;
700 cmdr
|= ATMCI_CMDR_BLOCK
;
703 if (data
->flags
& MMC_DATA_READ
)
704 cmdr
|= ATMCI_CMDR_TRDIR_READ
;
710 static void atmci_send_command(struct atmel_mci
*host
,
711 struct mmc_command
*cmd
, u32 cmd_flags
)
716 dev_vdbg(&host
->pdev
->dev
,
717 "start command: ARGR=0x%08x CMDR=0x%08x\n",
718 cmd
->arg
, cmd_flags
);
720 atmci_writel(host
, ATMCI_ARGR
, cmd
->arg
);
721 atmci_writel(host
, ATMCI_CMDR
, cmd_flags
);
724 static void atmci_send_stop_cmd(struct atmel_mci
*host
, struct mmc_data
*data
)
726 dev_dbg(&host
->pdev
->dev
, "send stop command\n");
727 atmci_send_command(host
, data
->stop
, host
->stop_cmdr
);
728 atmci_writel(host
, ATMCI_IER
, ATMCI_CMDRDY
);
732 * Configure given PDC buffer taking care of alignement issues.
733 * Update host->data_size and host->sg.
735 static void atmci_pdc_set_single_buf(struct atmel_mci
*host
,
736 enum atmci_xfer_dir dir
, enum atmci_pdc_buf buf_nb
)
738 u32 pointer_reg
, counter_reg
;
739 unsigned int buf_size
;
741 if (dir
== XFER_RECEIVE
) {
742 pointer_reg
= ATMEL_PDC_RPR
;
743 counter_reg
= ATMEL_PDC_RCR
;
745 pointer_reg
= ATMEL_PDC_TPR
;
746 counter_reg
= ATMEL_PDC_TCR
;
749 if (buf_nb
== PDC_SECOND_BUF
) {
750 pointer_reg
+= ATMEL_PDC_SCND_BUF_OFF
;
751 counter_reg
+= ATMEL_PDC_SCND_BUF_OFF
;
754 if (!host
->caps
.has_rwproof
) {
755 buf_size
= host
->buf_size
;
756 atmci_writel(host
, pointer_reg
, host
->buf_phys_addr
);
758 buf_size
= sg_dma_len(host
->sg
);
759 atmci_writel(host
, pointer_reg
, sg_dma_address(host
->sg
));
762 if (host
->data_size
<= buf_size
) {
763 if (host
->data_size
& 0x3) {
764 /* If size is different from modulo 4, transfer bytes */
765 atmci_writel(host
, counter_reg
, host
->data_size
);
766 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
| ATMCI_MR_PDCFBYTE
);
768 /* Else transfer 32-bits words */
769 atmci_writel(host
, counter_reg
, host
->data_size
/ 4);
773 /* We assume the size of a page is 32-bits aligned */
774 atmci_writel(host
, counter_reg
, sg_dma_len(host
->sg
) / 4);
775 host
->data_size
-= sg_dma_len(host
->sg
);
777 host
->sg
= sg_next(host
->sg
);
782 * Configure PDC buffer according to the data size ie configuring one or two
783 * buffers. Don't use this function if you want to configure only the second
784 * buffer. In this case, use atmci_pdc_set_single_buf.
786 static void atmci_pdc_set_both_buf(struct atmel_mci
*host
, int dir
)
788 atmci_pdc_set_single_buf(host
, dir
, PDC_FIRST_BUF
);
790 atmci_pdc_set_single_buf(host
, dir
, PDC_SECOND_BUF
);
794 * Unmap sg lists, called when transfer is finished.
796 static void atmci_pdc_cleanup(struct atmel_mci
*host
)
798 struct mmc_data
*data
= host
->data
;
801 dma_unmap_sg(&host
->pdev
->dev
,
802 data
->sg
, data
->sg_len
,
803 ((data
->flags
& MMC_DATA_WRITE
)
804 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
808 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
809 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
810 * interrupt needed for both transfer directions.
812 static void atmci_pdc_complete(struct atmel_mci
*host
)
814 int transfer_size
= host
->data
->blocks
* host
->data
->blksz
;
817 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
819 if ((!host
->caps
.has_rwproof
)
820 && (host
->data
->flags
& MMC_DATA_READ
)) {
821 if (host
->caps
.has_bad_data_ordering
)
822 for (i
= 0; i
< transfer_size
; i
++)
823 host
->buffer
[i
] = swab32(host
->buffer
[i
]);
824 sg_copy_from_buffer(host
->data
->sg
, host
->data
->sg_len
,
825 host
->buffer
, transfer_size
);
828 atmci_pdc_cleanup(host
);
830 dev_dbg(&host
->pdev
->dev
, "(%s) set pending xfer complete\n", __func__
);
831 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
832 tasklet_schedule(&host
->tasklet
);
835 static void atmci_dma_cleanup(struct atmel_mci
*host
)
837 struct mmc_data
*data
= host
->data
;
840 dma_unmap_sg(host
->dma
.chan
->device
->dev
,
841 data
->sg
, data
->sg_len
,
842 ((data
->flags
& MMC_DATA_WRITE
)
843 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
));
847 * This function is called by the DMA driver from tasklet context.
849 static void atmci_dma_complete(void *arg
)
851 struct atmel_mci
*host
= arg
;
852 struct mmc_data
*data
= host
->data
;
854 dev_vdbg(&host
->pdev
->dev
, "DMA complete\n");
856 if (host
->caps
.has_dma_conf_reg
)
857 /* Disable DMA hardware handshaking on MCI */
858 atmci_writel(host
, ATMCI_DMA
, atmci_readl(host
, ATMCI_DMA
) & ~ATMCI_DMAEN
);
860 atmci_dma_cleanup(host
);
863 * If the card was removed, data will be NULL. No point trying
864 * to send the stop command or waiting for NBUSY in this case.
867 dev_dbg(&host
->pdev
->dev
,
868 "(%s) set pending xfer complete\n", __func__
);
869 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
870 tasklet_schedule(&host
->tasklet
);
873 * Regardless of what the documentation says, we have
874 * to wait for NOTBUSY even after block read
877 * When the DMA transfer is complete, the controller
878 * may still be reading the CRC from the card, i.e.
879 * the data transfer is still in progress and we
880 * haven't seen all the potential error bits yet.
882 * The interrupt handler will schedule a different
883 * tasklet to finish things up when the data transfer
884 * is completely done.
886 * We may not complete the mmc request here anyway
887 * because the mmc layer may call back and cause us to
888 * violate the "don't submit new operations from the
889 * completion callback" rule of the dma engine
892 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
897 * Returns a mask of interrupt flags to be enabled after the whole
898 * request has been prepared.
900 static u32
atmci_prepare_data(struct atmel_mci
*host
, struct mmc_data
*data
)
904 data
->error
= -EINPROGRESS
;
907 host
->sg_len
= data
->sg_len
;
909 host
->data_chan
= NULL
;
911 iflags
= ATMCI_DATA_ERROR_FLAGS
;
914 * Errata: MMC data write operation with less than 12
915 * bytes is impossible.
917 * Errata: MCI Transmit Data Register (TDR) FIFO
918 * corruption when length is not multiple of 4.
920 if (data
->blocks
* data
->blksz
< 12
921 || (data
->blocks
* data
->blksz
) & 3)
922 host
->need_reset
= true;
924 host
->pio_offset
= 0;
925 if (data
->flags
& MMC_DATA_READ
)
926 iflags
|= ATMCI_RXRDY
;
928 iflags
|= ATMCI_TXRDY
;
934 * Set interrupt flags and set block length into the MCI mode register even
935 * if this value is also accessible in the MCI block register. It seems to be
936 * necessary before the High Speed MCI version. It also map sg and configure
940 atmci_prepare_data_pdc(struct atmel_mci
*host
, struct mmc_data
*data
)
944 enum dma_data_direction dir
;
947 data
->error
= -EINPROGRESS
;
951 iflags
= ATMCI_DATA_ERROR_FLAGS
;
953 /* Enable pdc mode */
954 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
| ATMCI_MR_PDCMODE
);
956 if (data
->flags
& MMC_DATA_READ
) {
957 dir
= DMA_FROM_DEVICE
;
958 iflags
|= ATMCI_ENDRX
| ATMCI_RXBUFF
;
961 iflags
|= ATMCI_ENDTX
| ATMCI_TXBUFE
| ATMCI_BLKE
;
965 tmp
= atmci_readl(host
, ATMCI_MR
);
967 tmp
|= ATMCI_BLKLEN(data
->blksz
);
968 atmci_writel(host
, ATMCI_MR
, tmp
);
971 host
->data_size
= data
->blocks
* data
->blksz
;
972 sg_len
= dma_map_sg(&host
->pdev
->dev
, data
->sg
, data
->sg_len
, dir
);
974 if ((!host
->caps
.has_rwproof
)
975 && (host
->data
->flags
& MMC_DATA_WRITE
)) {
976 sg_copy_to_buffer(host
->data
->sg
, host
->data
->sg_len
,
977 host
->buffer
, host
->data_size
);
978 if (host
->caps
.has_bad_data_ordering
)
979 for (i
= 0; i
< host
->data_size
; i
++)
980 host
->buffer
[i
] = swab32(host
->buffer
[i
]);
984 atmci_pdc_set_both_buf(host
,
985 ((dir
== DMA_FROM_DEVICE
) ? XFER_RECEIVE
: XFER_TRANSMIT
));
991 atmci_prepare_data_dma(struct atmel_mci
*host
, struct mmc_data
*data
)
993 struct dma_chan
*chan
;
994 struct dma_async_tx_descriptor
*desc
;
995 struct scatterlist
*sg
;
997 enum dma_data_direction direction
;
998 enum dma_transfer_direction slave_dirn
;
1003 data
->error
= -EINPROGRESS
;
1005 WARN_ON(host
->data
);
1009 iflags
= ATMCI_DATA_ERROR_FLAGS
;
1012 * We don't do DMA on "complex" transfers, i.e. with
1013 * non-word-aligned buffers or lengths. Also, we don't bother
1014 * with all the DMA setup overhead for short transfers.
1016 if (data
->blocks
* data
->blksz
< ATMCI_DMA_THRESHOLD
)
1017 return atmci_prepare_data(host
, data
);
1018 if (data
->blksz
& 3)
1019 return atmci_prepare_data(host
, data
);
1021 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
) {
1022 if (sg
->offset
& 3 || sg
->length
& 3)
1023 return atmci_prepare_data(host
, data
);
1026 /* If we don't have a channel, we can't do DMA */
1027 chan
= host
->dma
.chan
;
1029 host
->data_chan
= chan
;
1034 if (data
->flags
& MMC_DATA_READ
) {
1035 direction
= DMA_FROM_DEVICE
;
1036 host
->dma_conf
.direction
= slave_dirn
= DMA_DEV_TO_MEM
;
1037 maxburst
= atmci_convert_chksize(host
->dma_conf
.src_maxburst
);
1039 direction
= DMA_TO_DEVICE
;
1040 host
->dma_conf
.direction
= slave_dirn
= DMA_MEM_TO_DEV
;
1041 maxburst
= atmci_convert_chksize(host
->dma_conf
.dst_maxburst
);
1044 if (host
->caps
.has_dma_conf_reg
)
1045 atmci_writel(host
, ATMCI_DMA
, ATMCI_DMA_CHKSIZE(maxburst
) |
1048 sglen
= dma_map_sg(chan
->device
->dev
, data
->sg
,
1049 data
->sg_len
, direction
);
1051 dmaengine_slave_config(chan
, &host
->dma_conf
);
1052 desc
= dmaengine_prep_slave_sg(chan
,
1053 data
->sg
, sglen
, slave_dirn
,
1054 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1058 host
->dma
.data_desc
= desc
;
1059 desc
->callback
= atmci_dma_complete
;
1060 desc
->callback_param
= host
;
1064 dma_unmap_sg(chan
->device
->dev
, data
->sg
, data
->sg_len
, direction
);
1069 atmci_submit_data(struct atmel_mci
*host
, struct mmc_data
*data
)
1075 * Start PDC according to transfer direction.
1078 atmci_submit_data_pdc(struct atmel_mci
*host
, struct mmc_data
*data
)
1080 if (data
->flags
& MMC_DATA_READ
)
1081 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTEN
);
1083 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTEN
);
1087 atmci_submit_data_dma(struct atmel_mci
*host
, struct mmc_data
*data
)
1089 struct dma_chan
*chan
= host
->data_chan
;
1090 struct dma_async_tx_descriptor
*desc
= host
->dma
.data_desc
;
1093 dmaengine_submit(desc
);
1094 dma_async_issue_pending(chan
);
1098 static void atmci_stop_transfer(struct atmel_mci
*host
)
1100 dev_dbg(&host
->pdev
->dev
,
1101 "(%s) set pending xfer complete\n", __func__
);
1102 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1103 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1107 * Stop data transfer because error(s) occurred.
1109 static void atmci_stop_transfer_pdc(struct atmel_mci
*host
)
1111 atmci_writel(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
1114 static void atmci_stop_transfer_dma(struct atmel_mci
*host
)
1116 struct dma_chan
*chan
= host
->data_chan
;
1119 dmaengine_terminate_all(chan
);
1120 atmci_dma_cleanup(host
);
1122 /* Data transfer was stopped by the interrupt handler */
1123 dev_dbg(&host
->pdev
->dev
,
1124 "(%s) set pending xfer complete\n", __func__
);
1125 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1126 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1131 * Start a request: prepare data if needed, prepare the command and activate
1134 static void atmci_start_request(struct atmel_mci
*host
,
1135 struct atmel_mci_slot
*slot
)
1137 struct mmc_request
*mrq
;
1138 struct mmc_command
*cmd
;
1139 struct mmc_data
*data
;
1144 host
->cur_slot
= slot
;
1147 host
->pending_events
= 0;
1148 host
->completed_events
= 0;
1149 host
->cmd_status
= 0;
1150 host
->data_status
= 0;
1152 dev_dbg(&host
->pdev
->dev
, "start request: cmd %u\n", mrq
->cmd
->opcode
);
1154 if (host
->need_reset
|| host
->caps
.need_reset_after_xfer
) {
1155 iflags
= atmci_readl(host
, ATMCI_IMR
);
1156 iflags
&= (ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
);
1157 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1158 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1159 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1160 if (host
->caps
.has_cfg_reg
)
1161 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1162 atmci_writel(host
, ATMCI_IER
, iflags
);
1163 host
->need_reset
= false;
1165 atmci_writel(host
, ATMCI_SDCR
, slot
->sdc_reg
);
1167 iflags
= atmci_readl(host
, ATMCI_IMR
);
1168 if (iflags
& ~(ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
))
1169 dev_dbg(&slot
->mmc
->class_dev
, "WARNING: IMR=0x%08x\n",
1172 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT
, &slot
->flags
))) {
1173 /* Send init sequence (74 clock cycles) */
1174 atmci_writel(host
, ATMCI_CMDR
, ATMCI_CMDR_SPCMD_INIT
);
1175 while (!(atmci_readl(host
, ATMCI_SR
) & ATMCI_CMDRDY
))
1181 atmci_set_timeout(host
, slot
, data
);
1183 /* Must set block count/size before sending command */
1184 atmci_writel(host
, ATMCI_BLKR
, ATMCI_BCNT(data
->blocks
)
1185 | ATMCI_BLKLEN(data
->blksz
));
1186 dev_vdbg(&slot
->mmc
->class_dev
, "BLKR=0x%08x\n",
1187 ATMCI_BCNT(data
->blocks
) | ATMCI_BLKLEN(data
->blksz
));
1189 iflags
|= host
->prepare_data(host
, data
);
1192 iflags
|= ATMCI_CMDRDY
;
1194 cmdflags
= atmci_prepare_command(slot
->mmc
, cmd
);
1197 * DMA transfer should be started before sending the command to avoid
1198 * unexpected errors especially for read operations in SDIO mode.
1199 * Unfortunately, in PDC mode, command has to be sent before starting
1202 if (host
->submit_data
!= &atmci_submit_data_dma
)
1203 atmci_send_command(host
, cmd
, cmdflags
);
1206 host
->submit_data(host
, data
);
1208 if (host
->submit_data
== &atmci_submit_data_dma
)
1209 atmci_send_command(host
, cmd
, cmdflags
);
1212 host
->stop_cmdr
= atmci_prepare_command(slot
->mmc
, mrq
->stop
);
1213 host
->stop_cmdr
|= ATMCI_CMDR_STOP_XFER
;
1214 if (!(data
->flags
& MMC_DATA_WRITE
))
1215 host
->stop_cmdr
|= ATMCI_CMDR_TRDIR_READ
;
1216 if (data
->flags
& MMC_DATA_STREAM
)
1217 host
->stop_cmdr
|= ATMCI_CMDR_STREAM
;
1219 host
->stop_cmdr
|= ATMCI_CMDR_MULTI_BLOCK
;
1223 * We could have enabled interrupts earlier, but I suspect
1224 * that would open up a nice can of interesting race
1225 * conditions (e.g. command and data complete, but stop not
1228 atmci_writel(host
, ATMCI_IER
, iflags
);
1230 mod_timer(&host
->timer
, jiffies
+ msecs_to_jiffies(2000));
1233 static void atmci_queue_request(struct atmel_mci
*host
,
1234 struct atmel_mci_slot
*slot
, struct mmc_request
*mrq
)
1236 dev_vdbg(&slot
->mmc
->class_dev
, "queue request: state=%d\n",
1239 spin_lock_bh(&host
->lock
);
1241 if (host
->state
== STATE_IDLE
) {
1242 host
->state
= STATE_SENDING_CMD
;
1243 atmci_start_request(host
, slot
);
1245 dev_dbg(&host
->pdev
->dev
, "queue request\n");
1246 list_add_tail(&slot
->queue_node
, &host
->queue
);
1248 spin_unlock_bh(&host
->lock
);
1251 static void atmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1253 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1254 struct atmel_mci
*host
= slot
->host
;
1255 struct mmc_data
*data
;
1258 dev_dbg(&host
->pdev
->dev
, "MRQ: cmd %u\n", mrq
->cmd
->opcode
);
1260 pm_runtime_get_sync(&host
->pdev
->dev
);
1263 * We may "know" the card is gone even though there's still an
1264 * electrical connection. If so, we really need to communicate
1265 * this to the MMC core since there won't be any more
1266 * interrupts as the card is completely removed. Otherwise,
1267 * the MMC core might believe the card is still there even
1268 * though the card was just removed very slowly.
1270 if (!test_bit(ATMCI_CARD_PRESENT
, &slot
->flags
)) {
1271 mrq
->cmd
->error
= -ENOMEDIUM
;
1272 mmc_request_done(mmc
, mrq
);
1276 /* We don't support multiple blocks of weird lengths. */
1278 if (data
&& data
->blocks
> 1 && data
->blksz
& 3) {
1279 mrq
->cmd
->error
= -EINVAL
;
1280 mmc_request_done(mmc
, mrq
);
1283 atmci_queue_request(host
, slot
, mrq
);
1286 static void atmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1288 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1289 struct atmel_mci
*host
= slot
->host
;
1292 pm_runtime_get_sync(&host
->pdev
->dev
);
1294 slot
->sdc_reg
&= ~ATMCI_SDCBUS_MASK
;
1295 switch (ios
->bus_width
) {
1296 case MMC_BUS_WIDTH_1
:
1297 slot
->sdc_reg
|= ATMCI_SDCBUS_1BIT
;
1299 case MMC_BUS_WIDTH_4
:
1300 slot
->sdc_reg
|= ATMCI_SDCBUS_4BIT
;
1305 unsigned int clock_min
= ~0U;
1308 spin_lock_bh(&host
->lock
);
1309 if (!host
->mode_reg
) {
1310 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1311 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1312 if (host
->caps
.has_cfg_reg
)
1313 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1317 * Use mirror of ios->clock to prevent race with mmc
1318 * core ios update when finding the minimum.
1320 slot
->clock
= ios
->clock
;
1321 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1322 if (host
->slot
[i
] && host
->slot
[i
]->clock
1323 && host
->slot
[i
]->clock
< clock_min
)
1324 clock_min
= host
->slot
[i
]->clock
;
1327 /* Calculate clock divider */
1328 if (host
->caps
.has_odd_clk_div
) {
1329 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, clock_min
) - 2;
1331 dev_warn(&mmc
->class_dev
,
1332 "clock %u too fast; using %lu\n",
1333 clock_min
, host
->bus_hz
/ 2);
1335 } else if (clkdiv
> 511) {
1336 dev_warn(&mmc
->class_dev
,
1337 "clock %u too slow; using %lu\n",
1338 clock_min
, host
->bus_hz
/ (511 + 2));
1341 host
->mode_reg
= ATMCI_MR_CLKDIV(clkdiv
>> 1)
1342 | ATMCI_MR_CLKODD(clkdiv
& 1);
1344 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, 2 * clock_min
) - 1;
1346 dev_warn(&mmc
->class_dev
,
1347 "clock %u too slow; using %lu\n",
1348 clock_min
, host
->bus_hz
/ (2 * 256));
1351 host
->mode_reg
= ATMCI_MR_CLKDIV(clkdiv
);
1355 * WRPROOF and RDPROOF prevent overruns/underruns by
1356 * stopping the clock when the FIFO is full/empty.
1357 * This state is not expected to last for long.
1359 if (host
->caps
.has_rwproof
)
1360 host
->mode_reg
|= (ATMCI_MR_WRPROOF
| ATMCI_MR_RDPROOF
);
1362 if (host
->caps
.has_cfg_reg
) {
1363 /* setup High Speed mode in relation with card capacity */
1364 if (ios
->timing
== MMC_TIMING_SD_HS
)
1365 host
->cfg_reg
|= ATMCI_CFG_HSMODE
;
1367 host
->cfg_reg
&= ~ATMCI_CFG_HSMODE
;
1370 if (list_empty(&host
->queue
)) {
1371 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1372 if (host
->caps
.has_cfg_reg
)
1373 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1375 host
->need_clock_update
= true;
1378 spin_unlock_bh(&host
->lock
);
1380 bool any_slot_active
= false;
1382 spin_lock_bh(&host
->lock
);
1384 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1385 if (host
->slot
[i
] && host
->slot
[i
]->clock
) {
1386 any_slot_active
= true;
1390 if (!any_slot_active
) {
1391 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIDIS
);
1392 if (host
->mode_reg
) {
1393 atmci_readl(host
, ATMCI_MR
);
1397 spin_unlock_bh(&host
->lock
);
1400 switch (ios
->power_mode
) {
1402 if (!IS_ERR(mmc
->supply
.vmmc
))
1403 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, 0);
1406 set_bit(ATMCI_CARD_NEED_INIT
, &slot
->flags
);
1407 if (!IS_ERR(mmc
->supply
.vmmc
))
1408 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, ios
->vdd
);
1412 * TODO: None of the currently available AVR32-based
1413 * boards allow MMC power to be turned off. Implement
1414 * power control when this can be tested properly.
1416 * We also need to hook this into the clock management
1417 * somehow so that newly inserted cards aren't
1418 * subjected to a fast clock before we have a chance
1419 * to figure out what the maximum rate is. Currently,
1420 * there's no way to avoid this, and there never will
1421 * be for boards that don't support power control.
1426 pm_runtime_mark_last_busy(&host
->pdev
->dev
);
1427 pm_runtime_put_autosuspend(&host
->pdev
->dev
);
1430 static int atmci_get_ro(struct mmc_host
*mmc
)
1432 int read_only
= -ENOSYS
;
1433 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1435 if (gpio_is_valid(slot
->wp_pin
)) {
1436 read_only
= gpio_get_value(slot
->wp_pin
);
1437 dev_dbg(&mmc
->class_dev
, "card is %s\n",
1438 read_only
? "read-only" : "read-write");
1444 static int atmci_get_cd(struct mmc_host
*mmc
)
1446 int present
= -ENOSYS
;
1447 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1449 if (gpio_is_valid(slot
->detect_pin
)) {
1450 present
= !(gpio_get_value(slot
->detect_pin
) ^
1451 slot
->detect_is_active_high
);
1452 dev_dbg(&mmc
->class_dev
, "card is %spresent\n",
1453 present
? "" : "not ");
1459 static void atmci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
1461 struct atmel_mci_slot
*slot
= mmc_priv(mmc
);
1462 struct atmel_mci
*host
= slot
->host
;
1465 atmci_writel(host
, ATMCI_IER
, slot
->sdio_irq
);
1467 atmci_writel(host
, ATMCI_IDR
, slot
->sdio_irq
);
1470 static const struct mmc_host_ops atmci_ops
= {
1471 .request
= atmci_request
,
1472 .set_ios
= atmci_set_ios
,
1473 .get_ro
= atmci_get_ro
,
1474 .get_cd
= atmci_get_cd
,
1475 .enable_sdio_irq
= atmci_enable_sdio_irq
,
1478 /* Called with host->lock held */
1479 static void atmci_request_end(struct atmel_mci
*host
, struct mmc_request
*mrq
)
1480 __releases(&host
->lock
)
1481 __acquires(&host
->lock
)
1483 struct atmel_mci_slot
*slot
= NULL
;
1484 struct mmc_host
*prev_mmc
= host
->cur_slot
->mmc
;
1486 WARN_ON(host
->cmd
|| host
->data
);
1489 * Update the MMC clock rate if necessary. This may be
1490 * necessary if set_ios() is called when a different slot is
1491 * busy transferring data.
1493 if (host
->need_clock_update
) {
1494 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1495 if (host
->caps
.has_cfg_reg
)
1496 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1499 host
->cur_slot
->mrq
= NULL
;
1501 if (!list_empty(&host
->queue
)) {
1502 slot
= list_entry(host
->queue
.next
,
1503 struct atmel_mci_slot
, queue_node
);
1504 list_del(&slot
->queue_node
);
1505 dev_vdbg(&host
->pdev
->dev
, "list not empty: %s is next\n",
1506 mmc_hostname(slot
->mmc
));
1507 host
->state
= STATE_SENDING_CMD
;
1508 atmci_start_request(host
, slot
);
1510 dev_vdbg(&host
->pdev
->dev
, "list empty\n");
1511 host
->state
= STATE_IDLE
;
1514 del_timer(&host
->timer
);
1516 spin_unlock(&host
->lock
);
1517 mmc_request_done(prev_mmc
, mrq
);
1518 spin_lock(&host
->lock
);
1520 pm_runtime_mark_last_busy(&host
->pdev
->dev
);
1521 pm_runtime_put_autosuspend(&host
->pdev
->dev
);
1524 static void atmci_command_complete(struct atmel_mci
*host
,
1525 struct mmc_command
*cmd
)
1527 u32 status
= host
->cmd_status
;
1529 /* Read the response from the card (up to 16 bytes) */
1530 cmd
->resp
[0] = atmci_readl(host
, ATMCI_RSPR
);
1531 cmd
->resp
[1] = atmci_readl(host
, ATMCI_RSPR
);
1532 cmd
->resp
[2] = atmci_readl(host
, ATMCI_RSPR
);
1533 cmd
->resp
[3] = atmci_readl(host
, ATMCI_RSPR
);
1535 if (status
& ATMCI_RTOE
)
1536 cmd
->error
= -ETIMEDOUT
;
1537 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& ATMCI_RCRCE
))
1538 cmd
->error
= -EILSEQ
;
1539 else if (status
& (ATMCI_RINDE
| ATMCI_RDIRE
| ATMCI_RENDE
))
1541 else if (host
->mrq
->data
&& (host
->mrq
->data
->blksz
& 3)) {
1542 if (host
->caps
.need_blksz_mul_4
) {
1543 cmd
->error
= -EINVAL
;
1544 host
->need_reset
= 1;
1550 static void atmci_detect_change(unsigned long data
)
1552 struct atmel_mci_slot
*slot
= (struct atmel_mci_slot
*)data
;
1557 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1558 * freeing the interrupt. We must not re-enable the interrupt
1559 * if it has been freed, and if we're shutting down, it
1560 * doesn't really matter whether the card is present or not.
1563 if (test_bit(ATMCI_SHUTDOWN
, &slot
->flags
))
1566 enable_irq(gpio_to_irq(slot
->detect_pin
));
1567 present
= !(gpio_get_value(slot
->detect_pin
) ^
1568 slot
->detect_is_active_high
);
1569 present_old
= test_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1571 dev_vdbg(&slot
->mmc
->class_dev
, "detect change: %d (was %d)\n",
1572 present
, present_old
);
1574 if (present
!= present_old
) {
1575 struct atmel_mci
*host
= slot
->host
;
1576 struct mmc_request
*mrq
;
1578 dev_dbg(&slot
->mmc
->class_dev
, "card %s\n",
1579 present
? "inserted" : "removed");
1581 spin_lock(&host
->lock
);
1584 clear_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1586 set_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
1588 /* Clean up queue if present */
1591 if (mrq
== host
->mrq
) {
1593 * Reset controller to terminate any ongoing
1594 * commands or data transfers.
1596 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
1597 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIEN
);
1598 atmci_writel(host
, ATMCI_MR
, host
->mode_reg
);
1599 if (host
->caps
.has_cfg_reg
)
1600 atmci_writel(host
, ATMCI_CFG
, host
->cfg_reg
);
1605 switch (host
->state
) {
1608 case STATE_SENDING_CMD
:
1609 mrq
->cmd
->error
= -ENOMEDIUM
;
1611 host
->stop_transfer(host
);
1613 case STATE_DATA_XFER
:
1614 mrq
->data
->error
= -ENOMEDIUM
;
1615 host
->stop_transfer(host
);
1617 case STATE_WAITING_NOTBUSY
:
1618 mrq
->data
->error
= -ENOMEDIUM
;
1620 case STATE_SENDING_STOP
:
1621 mrq
->stop
->error
= -ENOMEDIUM
;
1623 case STATE_END_REQUEST
:
1627 atmci_request_end(host
, mrq
);
1629 list_del(&slot
->queue_node
);
1630 mrq
->cmd
->error
= -ENOMEDIUM
;
1632 mrq
->data
->error
= -ENOMEDIUM
;
1634 mrq
->stop
->error
= -ENOMEDIUM
;
1636 spin_unlock(&host
->lock
);
1637 mmc_request_done(slot
->mmc
, mrq
);
1638 spin_lock(&host
->lock
);
1641 spin_unlock(&host
->lock
);
1643 mmc_detect_change(slot
->mmc
, 0);
1647 static void atmci_tasklet_func(unsigned long priv
)
1649 struct atmel_mci
*host
= (struct atmel_mci
*)priv
;
1650 struct mmc_request
*mrq
= host
->mrq
;
1651 struct mmc_data
*data
= host
->data
;
1652 enum atmel_mci_state state
= host
->state
;
1653 enum atmel_mci_state prev_state
;
1656 spin_lock(&host
->lock
);
1658 state
= host
->state
;
1660 dev_vdbg(&host
->pdev
->dev
,
1661 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1662 state
, host
->pending_events
, host
->completed_events
,
1663 atmci_readl(host
, ATMCI_IMR
));
1667 dev_dbg(&host
->pdev
->dev
, "FSM: state=%d\n", state
);
1673 case STATE_SENDING_CMD
:
1675 * Command has been sent, we are waiting for command
1676 * ready. Then we have three next states possible:
1677 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1678 * command needing it or DATA_XFER if there is data.
1680 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready?\n");
1681 if (!atmci_test_and_clear_pending(host
,
1685 dev_dbg(&host
->pdev
->dev
, "set completed cmd ready\n");
1687 atmci_set_completed(host
, EVENT_CMD_RDY
);
1688 atmci_command_complete(host
, mrq
->cmd
);
1690 dev_dbg(&host
->pdev
->dev
,
1691 "command with data transfer");
1693 * If there is a command error don't start
1696 if (mrq
->cmd
->error
) {
1697 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 state
= STATE_DATA_XFER
;
1705 } else if ((!mrq
->data
) && (mrq
->cmd
->flags
& MMC_RSP_BUSY
)) {
1706 dev_dbg(&host
->pdev
->dev
,
1707 "command response need waiting notbusy");
1708 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1709 state
= STATE_WAITING_NOTBUSY
;
1711 state
= STATE_END_REQUEST
;
1715 case STATE_DATA_XFER
:
1716 if (atmci_test_and_clear_pending(host
,
1717 EVENT_DATA_ERROR
)) {
1718 dev_dbg(&host
->pdev
->dev
, "set completed data error\n");
1719 atmci_set_completed(host
, EVENT_DATA_ERROR
);
1720 state
= STATE_END_REQUEST
;
1725 * A data transfer is in progress. The event expected
1726 * to move to the next state depends of data transfer
1727 * type (PDC or DMA). Once transfer done we can move
1728 * to the next step which is WAITING_NOTBUSY in write
1729 * case and directly SENDING_STOP in read case.
1731 dev_dbg(&host
->pdev
->dev
, "FSM: xfer complete?\n");
1732 if (!atmci_test_and_clear_pending(host
,
1733 EVENT_XFER_COMPLETE
))
1736 dev_dbg(&host
->pdev
->dev
,
1737 "(%s) set completed xfer complete\n",
1739 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
1741 if (host
->caps
.need_notbusy_for_read_ops
||
1742 (host
->data
->flags
& MMC_DATA_WRITE
)) {
1743 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1744 state
= STATE_WAITING_NOTBUSY
;
1745 } else if (host
->mrq
->stop
) {
1746 atmci_writel(host
, ATMCI_IER
, ATMCI_CMDRDY
);
1747 atmci_send_stop_cmd(host
, data
);
1748 state
= STATE_SENDING_STOP
;
1751 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1753 state
= STATE_END_REQUEST
;
1757 case STATE_WAITING_NOTBUSY
:
1759 * We can be in the state for two reasons: a command
1760 * requiring waiting not busy signal (stop command
1761 * included) or a write operation. In the latest case,
1762 * we need to send a stop command.
1764 dev_dbg(&host
->pdev
->dev
, "FSM: not busy?\n");
1765 if (!atmci_test_and_clear_pending(host
,
1769 dev_dbg(&host
->pdev
->dev
, "set completed not busy\n");
1770 atmci_set_completed(host
, EVENT_NOTBUSY
);
1774 * For some commands such as CMD53, even if
1775 * there is data transfer, there is no stop
1778 if (host
->mrq
->stop
) {
1779 atmci_writel(host
, ATMCI_IER
,
1781 atmci_send_stop_cmd(host
, data
);
1782 state
= STATE_SENDING_STOP
;
1785 data
->bytes_xfered
= data
->blocks
1788 state
= STATE_END_REQUEST
;
1791 state
= STATE_END_REQUEST
;
1794 case STATE_SENDING_STOP
:
1796 * In this state, it is important to set host->data to
1797 * NULL (which is tested in the waiting notbusy state)
1798 * in order to go to the end request state instead of
1799 * sending stop again.
1801 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready?\n");
1802 if (!atmci_test_and_clear_pending(host
,
1806 dev_dbg(&host
->pdev
->dev
, "FSM: cmd ready\n");
1808 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1810 atmci_command_complete(host
, mrq
->stop
);
1811 if (mrq
->stop
->error
) {
1812 host
->stop_transfer(host
);
1813 atmci_writel(host
, ATMCI_IDR
,
1814 ATMCI_TXRDY
| ATMCI_RXRDY
1815 | ATMCI_DATA_ERROR_FLAGS
);
1816 state
= STATE_END_REQUEST
;
1818 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1819 state
= STATE_WAITING_NOTBUSY
;
1824 case STATE_END_REQUEST
:
1825 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXRDY
| ATMCI_RXRDY
1826 | ATMCI_DATA_ERROR_FLAGS
);
1827 status
= host
->data_status
;
1828 if (unlikely(status
)) {
1829 host
->stop_transfer(host
);
1832 if (status
& ATMCI_DTOE
) {
1833 data
->error
= -ETIMEDOUT
;
1834 } else if (status
& ATMCI_DCRCE
) {
1835 data
->error
= -EILSEQ
;
1842 atmci_request_end(host
, host
->mrq
);
1846 } while (state
!= prev_state
);
1848 host
->state
= state
;
1850 spin_unlock(&host
->lock
);
1853 static void atmci_read_data_pio(struct atmel_mci
*host
)
1855 struct scatterlist
*sg
= host
->sg
;
1856 void *buf
= sg_virt(sg
);
1857 unsigned int offset
= host
->pio_offset
;
1858 struct mmc_data
*data
= host
->data
;
1861 unsigned int nbytes
= 0;
1864 value
= atmci_readl(host
, ATMCI_RDR
);
1865 if (likely(offset
+ 4 <= sg
->length
)) {
1866 put_unaligned(value
, (u32
*)(buf
+ offset
));
1871 if (offset
== sg
->length
) {
1872 flush_dcache_page(sg_page(sg
));
1873 host
->sg
= sg
= sg_next(sg
);
1875 if (!sg
|| !host
->sg_len
)
1882 unsigned int remaining
= sg
->length
- offset
;
1883 memcpy(buf
+ offset
, &value
, remaining
);
1884 nbytes
+= remaining
;
1886 flush_dcache_page(sg_page(sg
));
1887 host
->sg
= sg
= sg_next(sg
);
1889 if (!sg
|| !host
->sg_len
)
1892 offset
= 4 - remaining
;
1894 memcpy(buf
, (u8
*)&value
+ remaining
, offset
);
1898 status
= atmci_readl(host
, ATMCI_SR
);
1899 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
1900 atmci_writel(host
, ATMCI_IDR
, (ATMCI_NOTBUSY
| ATMCI_RXRDY
1901 | ATMCI_DATA_ERROR_FLAGS
));
1902 host
->data_status
= status
;
1903 data
->bytes_xfered
+= nbytes
;
1906 } while (status
& ATMCI_RXRDY
);
1908 host
->pio_offset
= offset
;
1909 data
->bytes_xfered
+= nbytes
;
1914 atmci_writel(host
, ATMCI_IDR
, ATMCI_RXRDY
);
1915 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1916 data
->bytes_xfered
+= nbytes
;
1918 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1921 static void atmci_write_data_pio(struct atmel_mci
*host
)
1923 struct scatterlist
*sg
= host
->sg
;
1924 void *buf
= sg_virt(sg
);
1925 unsigned int offset
= host
->pio_offset
;
1926 struct mmc_data
*data
= host
->data
;
1929 unsigned int nbytes
= 0;
1932 if (likely(offset
+ 4 <= sg
->length
)) {
1933 value
= get_unaligned((u32
*)(buf
+ offset
));
1934 atmci_writel(host
, ATMCI_TDR
, value
);
1938 if (offset
== sg
->length
) {
1939 host
->sg
= sg
= sg_next(sg
);
1941 if (!sg
|| !host
->sg_len
)
1948 unsigned int remaining
= sg
->length
- offset
;
1951 memcpy(&value
, buf
+ offset
, remaining
);
1952 nbytes
+= remaining
;
1954 host
->sg
= sg
= sg_next(sg
);
1956 if (!sg
|| !host
->sg_len
) {
1957 atmci_writel(host
, ATMCI_TDR
, value
);
1961 offset
= 4 - remaining
;
1963 memcpy((u8
*)&value
+ remaining
, buf
, offset
);
1964 atmci_writel(host
, ATMCI_TDR
, value
);
1968 status
= atmci_readl(host
, ATMCI_SR
);
1969 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
1970 atmci_writel(host
, ATMCI_IDR
, (ATMCI_NOTBUSY
| ATMCI_TXRDY
1971 | ATMCI_DATA_ERROR_FLAGS
));
1972 host
->data_status
= status
;
1973 data
->bytes_xfered
+= nbytes
;
1976 } while (status
& ATMCI_TXRDY
);
1978 host
->pio_offset
= offset
;
1979 data
->bytes_xfered
+= nbytes
;
1984 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXRDY
);
1985 atmci_writel(host
, ATMCI_IER
, ATMCI_NOTBUSY
);
1986 data
->bytes_xfered
+= nbytes
;
1988 atmci_set_pending(host
, EVENT_XFER_COMPLETE
);
1991 static void atmci_sdio_interrupt(struct atmel_mci
*host
, u32 status
)
1995 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
1996 struct atmel_mci_slot
*slot
= host
->slot
[i
];
1997 if (slot
&& (status
& slot
->sdio_irq
)) {
1998 mmc_signal_sdio_irq(slot
->mmc
);
2004 static irqreturn_t
atmci_interrupt(int irq
, void *dev_id
)
2006 struct atmel_mci
*host
= dev_id
;
2007 u32 status
, mask
, pending
;
2008 unsigned int pass_count
= 0;
2011 status
= atmci_readl(host
, ATMCI_SR
);
2012 mask
= atmci_readl(host
, ATMCI_IMR
);
2013 pending
= status
& mask
;
2017 if (pending
& ATMCI_DATA_ERROR_FLAGS
) {
2018 dev_dbg(&host
->pdev
->dev
, "IRQ: data error\n");
2019 atmci_writel(host
, ATMCI_IDR
, ATMCI_DATA_ERROR_FLAGS
2020 | ATMCI_RXRDY
| ATMCI_TXRDY
2021 | ATMCI_ENDRX
| ATMCI_ENDTX
2022 | ATMCI_RXBUFF
| ATMCI_TXBUFE
);
2024 host
->data_status
= status
;
2025 dev_dbg(&host
->pdev
->dev
, "set pending data error\n");
2027 atmci_set_pending(host
, EVENT_DATA_ERROR
);
2028 tasklet_schedule(&host
->tasklet
);
2031 if (pending
& ATMCI_TXBUFE
) {
2032 dev_dbg(&host
->pdev
->dev
, "IRQ: tx buffer empty\n");
2033 atmci_writel(host
, ATMCI_IDR
, ATMCI_TXBUFE
);
2034 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDTX
);
2036 * We can receive this interruption before having configured
2037 * the second pdc buffer, so we need to reconfigure first and
2038 * second buffers again
2040 if (host
->data_size
) {
2041 atmci_pdc_set_both_buf(host
, XFER_TRANSMIT
);
2042 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDTX
);
2043 atmci_writel(host
, ATMCI_IER
, ATMCI_TXBUFE
);
2045 atmci_pdc_complete(host
);
2047 } else if (pending
& ATMCI_ENDTX
) {
2048 dev_dbg(&host
->pdev
->dev
, "IRQ: end of tx buffer\n");
2049 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDTX
);
2051 if (host
->data_size
) {
2052 atmci_pdc_set_single_buf(host
,
2053 XFER_TRANSMIT
, PDC_SECOND_BUF
);
2054 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDTX
);
2058 if (pending
& ATMCI_RXBUFF
) {
2059 dev_dbg(&host
->pdev
->dev
, "IRQ: rx buffer full\n");
2060 atmci_writel(host
, ATMCI_IDR
, ATMCI_RXBUFF
);
2061 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDRX
);
2063 * We can receive this interruption before having configured
2064 * the second pdc buffer, so we need to reconfigure first and
2065 * second buffers again
2067 if (host
->data_size
) {
2068 atmci_pdc_set_both_buf(host
, XFER_RECEIVE
);
2069 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDRX
);
2070 atmci_writel(host
, ATMCI_IER
, ATMCI_RXBUFF
);
2072 atmci_pdc_complete(host
);
2074 } else if (pending
& ATMCI_ENDRX
) {
2075 dev_dbg(&host
->pdev
->dev
, "IRQ: end of rx buffer\n");
2076 atmci_writel(host
, ATMCI_IDR
, ATMCI_ENDRX
);
2078 if (host
->data_size
) {
2079 atmci_pdc_set_single_buf(host
,
2080 XFER_RECEIVE
, PDC_SECOND_BUF
);
2081 atmci_writel(host
, ATMCI_IER
, ATMCI_ENDRX
);
2086 * First mci IPs, so mainly the ones having pdc, have some
2087 * issues with the notbusy signal. You can't get it after
2088 * data transmission if you have not sent a stop command.
2089 * The appropriate workaround is to use the BLKE signal.
2091 if (pending
& ATMCI_BLKE
) {
2092 dev_dbg(&host
->pdev
->dev
, "IRQ: blke\n");
2093 atmci_writel(host
, ATMCI_IDR
, ATMCI_BLKE
);
2095 dev_dbg(&host
->pdev
->dev
, "set pending notbusy\n");
2096 atmci_set_pending(host
, EVENT_NOTBUSY
);
2097 tasklet_schedule(&host
->tasklet
);
2100 if (pending
& ATMCI_NOTBUSY
) {
2101 dev_dbg(&host
->pdev
->dev
, "IRQ: not_busy\n");
2102 atmci_writel(host
, ATMCI_IDR
, ATMCI_NOTBUSY
);
2104 dev_dbg(&host
->pdev
->dev
, "set pending notbusy\n");
2105 atmci_set_pending(host
, EVENT_NOTBUSY
);
2106 tasklet_schedule(&host
->tasklet
);
2109 if (pending
& ATMCI_RXRDY
)
2110 atmci_read_data_pio(host
);
2111 if (pending
& ATMCI_TXRDY
)
2112 atmci_write_data_pio(host
);
2114 if (pending
& ATMCI_CMDRDY
) {
2115 dev_dbg(&host
->pdev
->dev
, "IRQ: cmd ready\n");
2116 atmci_writel(host
, ATMCI_IDR
, ATMCI_CMDRDY
);
2117 host
->cmd_status
= status
;
2119 dev_dbg(&host
->pdev
->dev
, "set pending cmd rdy\n");
2120 atmci_set_pending(host
, EVENT_CMD_RDY
);
2121 tasklet_schedule(&host
->tasklet
);
2124 if (pending
& (ATMCI_SDIOIRQA
| ATMCI_SDIOIRQB
))
2125 atmci_sdio_interrupt(host
, status
);
2127 } while (pass_count
++ < 5);
2129 return pass_count
? IRQ_HANDLED
: IRQ_NONE
;
2132 static irqreturn_t
atmci_detect_interrupt(int irq
, void *dev_id
)
2134 struct atmel_mci_slot
*slot
= dev_id
;
2137 * Disable interrupts until the pin has stabilized and check
2138 * the state then. Use mod_timer() since we may be in the
2139 * middle of the timer routine when this interrupt triggers.
2141 disable_irq_nosync(irq
);
2142 mod_timer(&slot
->detect_timer
, jiffies
+ msecs_to_jiffies(20));
2147 static int atmci_init_slot(struct atmel_mci
*host
,
2148 struct mci_slot_pdata
*slot_data
, unsigned int id
,
2149 u32 sdc_reg
, u32 sdio_irq
)
2151 struct mmc_host
*mmc
;
2152 struct atmel_mci_slot
*slot
;
2154 mmc
= mmc_alloc_host(sizeof(struct atmel_mci_slot
), &host
->pdev
->dev
);
2158 slot
= mmc_priv(mmc
);
2161 slot
->detect_pin
= slot_data
->detect_pin
;
2162 slot
->wp_pin
= slot_data
->wp_pin
;
2163 slot
->detect_is_active_high
= slot_data
->detect_is_active_high
;
2164 slot
->sdc_reg
= sdc_reg
;
2165 slot
->sdio_irq
= sdio_irq
;
2167 dev_dbg(&mmc
->class_dev
,
2168 "slot[%u]: bus_width=%u, detect_pin=%d, "
2169 "detect_is_active_high=%s, wp_pin=%d\n",
2170 id
, slot_data
->bus_width
, slot_data
->detect_pin
,
2171 slot_data
->detect_is_active_high
? "true" : "false",
2174 mmc
->ops
= &atmci_ops
;
2175 mmc
->f_min
= DIV_ROUND_UP(host
->bus_hz
, 512);
2176 mmc
->f_max
= host
->bus_hz
/ 2;
2177 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
2179 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
2180 if (host
->caps
.has_highspeed
)
2181 mmc
->caps
|= MMC_CAP_SD_HIGHSPEED
;
2183 * Without the read/write proof capability, it is strongly suggested to
2184 * use only one bit for data to prevent fifo underruns and overruns
2185 * which will corrupt data.
2187 if ((slot_data
->bus_width
>= 4) && host
->caps
.has_rwproof
)
2188 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
2190 if (atmci_get_version(host
) < 0x200) {
2191 mmc
->max_segs
= 256;
2192 mmc
->max_blk_size
= 4095;
2193 mmc
->max_blk_count
= 256;
2194 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
2195 mmc
->max_seg_size
= mmc
->max_blk_size
* mmc
->max_segs
;
2198 mmc
->max_req_size
= 32768 * 512;
2199 mmc
->max_blk_size
= 32768;
2200 mmc
->max_blk_count
= 512;
2203 /* Assume card is present initially */
2204 set_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
2205 if (gpio_is_valid(slot
->detect_pin
)) {
2206 if (devm_gpio_request(&host
->pdev
->dev
, slot
->detect_pin
,
2208 dev_dbg(&mmc
->class_dev
, "no detect pin available\n");
2209 slot
->detect_pin
= -EBUSY
;
2210 } else if (gpio_get_value(slot
->detect_pin
) ^
2211 slot
->detect_is_active_high
) {
2212 clear_bit(ATMCI_CARD_PRESENT
, &slot
->flags
);
2216 if (!gpio_is_valid(slot
->detect_pin
)) {
2217 if (slot_data
->non_removable
)
2218 mmc
->caps
|= MMC_CAP_NONREMOVABLE
;
2220 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
2223 if (gpio_is_valid(slot
->wp_pin
)) {
2224 if (devm_gpio_request(&host
->pdev
->dev
, slot
->wp_pin
,
2226 dev_dbg(&mmc
->class_dev
, "no WP pin available\n");
2227 slot
->wp_pin
= -EBUSY
;
2231 host
->slot
[id
] = slot
;
2232 mmc_regulator_get_supply(mmc
);
2235 if (gpio_is_valid(slot
->detect_pin
)) {
2238 setup_timer(&slot
->detect_timer
, atmci_detect_change
,
2239 (unsigned long)slot
);
2241 ret
= request_irq(gpio_to_irq(slot
->detect_pin
),
2242 atmci_detect_interrupt
,
2243 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
2244 "mmc-detect", slot
);
2246 dev_dbg(&mmc
->class_dev
,
2247 "could not request IRQ %d for detect pin\n",
2248 gpio_to_irq(slot
->detect_pin
));
2249 slot
->detect_pin
= -EBUSY
;
2253 atmci_init_debugfs(slot
);
2258 static void atmci_cleanup_slot(struct atmel_mci_slot
*slot
,
2261 /* Debugfs stuff is cleaned up by mmc core */
2263 set_bit(ATMCI_SHUTDOWN
, &slot
->flags
);
2266 mmc_remove_host(slot
->mmc
);
2268 if (gpio_is_valid(slot
->detect_pin
)) {
2269 int pin
= slot
->detect_pin
;
2271 free_irq(gpio_to_irq(pin
), slot
);
2272 del_timer_sync(&slot
->detect_timer
);
2275 slot
->host
->slot
[id
] = NULL
;
2276 mmc_free_host(slot
->mmc
);
2279 static int atmci_configure_dma(struct atmel_mci
*host
)
2281 host
->dma
.chan
= dma_request_slave_channel_reason(&host
->pdev
->dev
,
2283 if (IS_ERR(host
->dma
.chan
))
2284 return PTR_ERR(host
->dma
.chan
);
2286 dev_info(&host
->pdev
->dev
, "using %s for DMA transfers\n",
2287 dma_chan_name(host
->dma
.chan
));
2289 host
->dma_conf
.src_addr
= host
->mapbase
+ ATMCI_RDR
;
2290 host
->dma_conf
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2291 host
->dma_conf
.src_maxburst
= 1;
2292 host
->dma_conf
.dst_addr
= host
->mapbase
+ ATMCI_TDR
;
2293 host
->dma_conf
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2294 host
->dma_conf
.dst_maxburst
= 1;
2295 host
->dma_conf
.device_fc
= false;
2301 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2302 * HSMCI provides DMA support and a new config register but no more supports
2305 static void atmci_get_cap(struct atmel_mci
*host
)
2307 unsigned int version
;
2309 version
= atmci_get_version(host
);
2310 dev_info(&host
->pdev
->dev
,
2311 "version: 0x%x\n", version
);
2313 host
->caps
.has_dma_conf_reg
= 0;
2314 host
->caps
.has_pdc
= ATMCI_PDC_CONNECTED
;
2315 host
->caps
.has_cfg_reg
= 0;
2316 host
->caps
.has_cstor_reg
= 0;
2317 host
->caps
.has_highspeed
= 0;
2318 host
->caps
.has_rwproof
= 0;
2319 host
->caps
.has_odd_clk_div
= 0;
2320 host
->caps
.has_bad_data_ordering
= 1;
2321 host
->caps
.need_reset_after_xfer
= 1;
2322 host
->caps
.need_blksz_mul_4
= 1;
2323 host
->caps
.need_notbusy_for_read_ops
= 0;
2325 /* keep only major version number */
2326 switch (version
& 0xf00) {
2329 host
->caps
.has_odd_clk_div
= 1;
2332 host
->caps
.has_dma_conf_reg
= 1;
2333 host
->caps
.has_pdc
= 0;
2334 host
->caps
.has_cfg_reg
= 1;
2335 host
->caps
.has_cstor_reg
= 1;
2336 host
->caps
.has_highspeed
= 1;
2338 host
->caps
.has_rwproof
= 1;
2339 host
->caps
.need_blksz_mul_4
= 0;
2340 host
->caps
.need_notbusy_for_read_ops
= 1;
2342 host
->caps
.has_bad_data_ordering
= 0;
2343 host
->caps
.need_reset_after_xfer
= 0;
2347 host
->caps
.has_pdc
= 0;
2348 dev_warn(&host
->pdev
->dev
,
2349 "Unmanaged mci version, set minimum capabilities\n");
2354 static int atmci_probe(struct platform_device
*pdev
)
2356 struct mci_platform_data
*pdata
;
2357 struct atmel_mci
*host
;
2358 struct resource
*regs
;
2359 unsigned int nr_slots
;
2363 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2366 pdata
= pdev
->dev
.platform_data
;
2368 pdata
= atmci_of_init(pdev
);
2369 if (IS_ERR(pdata
)) {
2370 dev_err(&pdev
->dev
, "platform data not available\n");
2371 return PTR_ERR(pdata
);
2375 irq
= platform_get_irq(pdev
, 0);
2379 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
2384 spin_lock_init(&host
->lock
);
2385 INIT_LIST_HEAD(&host
->queue
);
2387 host
->mck
= devm_clk_get(&pdev
->dev
, "mci_clk");
2388 if (IS_ERR(host
->mck
))
2389 return PTR_ERR(host
->mck
);
2391 host
->regs
= devm_ioremap(&pdev
->dev
, regs
->start
, resource_size(regs
));
2395 ret
= clk_prepare_enable(host
->mck
);
2399 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_SWRST
);
2400 host
->bus_hz
= clk_get_rate(host
->mck
);
2402 host
->mapbase
= regs
->start
;
2404 tasklet_init(&host
->tasklet
, atmci_tasklet_func
, (unsigned long)host
);
2406 ret
= request_irq(irq
, atmci_interrupt
, 0, dev_name(&pdev
->dev
), host
);
2408 clk_disable_unprepare(host
->mck
);
2412 /* Get MCI capabilities and set operations according to it */
2413 atmci_get_cap(host
);
2414 ret
= atmci_configure_dma(host
);
2415 if (ret
== -EPROBE_DEFER
)
2416 goto err_dma_probe_defer
;
2418 host
->prepare_data
= &atmci_prepare_data_dma
;
2419 host
->submit_data
= &atmci_submit_data_dma
;
2420 host
->stop_transfer
= &atmci_stop_transfer_dma
;
2421 } else if (host
->caps
.has_pdc
) {
2422 dev_info(&pdev
->dev
, "using PDC\n");
2423 host
->prepare_data
= &atmci_prepare_data_pdc
;
2424 host
->submit_data
= &atmci_submit_data_pdc
;
2425 host
->stop_transfer
= &atmci_stop_transfer_pdc
;
2427 dev_info(&pdev
->dev
, "using PIO\n");
2428 host
->prepare_data
= &atmci_prepare_data
;
2429 host
->submit_data
= &atmci_submit_data
;
2430 host
->stop_transfer
= &atmci_stop_transfer
;
2433 platform_set_drvdata(pdev
, host
);
2435 setup_timer(&host
->timer
, atmci_timeout_timer
, (unsigned long)host
);
2437 pm_runtime_get_noresume(&pdev
->dev
);
2438 pm_runtime_set_active(&pdev
->dev
);
2439 pm_runtime_set_autosuspend_delay(&pdev
->dev
, AUTOSUSPEND_DELAY
);
2440 pm_runtime_use_autosuspend(&pdev
->dev
);
2441 pm_runtime_enable(&pdev
->dev
);
2443 /* We need at least one slot to succeed */
2446 if (pdata
->slot
[0].bus_width
) {
2447 ret
= atmci_init_slot(host
, &pdata
->slot
[0],
2448 0, ATMCI_SDCSEL_SLOT_A
, ATMCI_SDIOIRQA
);
2451 host
->buf_size
= host
->slot
[0]->mmc
->max_req_size
;
2454 if (pdata
->slot
[1].bus_width
) {
2455 ret
= atmci_init_slot(host
, &pdata
->slot
[1],
2456 1, ATMCI_SDCSEL_SLOT_B
, ATMCI_SDIOIRQB
);
2459 if (host
->slot
[1]->mmc
->max_req_size
> host
->buf_size
)
2461 host
->slot
[1]->mmc
->max_req_size
;
2466 dev_err(&pdev
->dev
, "init failed: no slot defined\n");
2470 if (!host
->caps
.has_rwproof
) {
2471 host
->buffer
= dma_alloc_coherent(&pdev
->dev
, host
->buf_size
,
2472 &host
->buf_phys_addr
,
2474 if (!host
->buffer
) {
2476 dev_err(&pdev
->dev
, "buffer allocation failed\n");
2481 dev_info(&pdev
->dev
,
2482 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2483 host
->mapbase
, irq
, nr_slots
);
2485 pm_runtime_mark_last_busy(&host
->pdev
->dev
);
2486 pm_runtime_put_autosuspend(&pdev
->dev
);
2491 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
2493 atmci_cleanup_slot(host
->slot
[i
], i
);
2496 clk_disable_unprepare(host
->mck
);
2498 pm_runtime_disable(&pdev
->dev
);
2499 pm_runtime_put_noidle(&pdev
->dev
);
2501 del_timer_sync(&host
->timer
);
2502 if (!IS_ERR(host
->dma
.chan
))
2503 dma_release_channel(host
->dma
.chan
);
2504 err_dma_probe_defer
:
2505 free_irq(irq
, host
);
2509 static int atmci_remove(struct platform_device
*pdev
)
2511 struct atmel_mci
*host
= platform_get_drvdata(pdev
);
2514 pm_runtime_get_sync(&pdev
->dev
);
2517 dma_free_coherent(&pdev
->dev
, host
->buf_size
,
2518 host
->buffer
, host
->buf_phys_addr
);
2520 for (i
= 0; i
< ATMCI_MAX_NR_SLOTS
; i
++) {
2522 atmci_cleanup_slot(host
->slot
[i
], i
);
2525 atmci_writel(host
, ATMCI_IDR
, ~0UL);
2526 atmci_writel(host
, ATMCI_CR
, ATMCI_CR_MCIDIS
);
2527 atmci_readl(host
, ATMCI_SR
);
2529 del_timer_sync(&host
->timer
);
2530 if (!IS_ERR(host
->dma
.chan
))
2531 dma_release_channel(host
->dma
.chan
);
2533 free_irq(platform_get_irq(pdev
, 0), host
);
2535 clk_disable_unprepare(host
->mck
);
2537 pm_runtime_disable(&pdev
->dev
);
2538 pm_runtime_put_noidle(&pdev
->dev
);
2544 static int atmci_runtime_suspend(struct device
*dev
)
2546 struct atmel_mci
*host
= dev_get_drvdata(dev
);
2548 clk_disable_unprepare(host
->mck
);
2550 pinctrl_pm_select_sleep_state(dev
);
2555 static int atmci_runtime_resume(struct device
*dev
)
2557 struct atmel_mci
*host
= dev_get_drvdata(dev
);
2559 pinctrl_pm_select_default_state(dev
);
2561 return clk_prepare_enable(host
->mck
);
2565 static const struct dev_pm_ops atmci_dev_pm_ops
= {
2566 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
2567 pm_runtime_force_resume
)
2568 SET_RUNTIME_PM_OPS(atmci_runtime_suspend
, atmci_runtime_resume
, NULL
)
2571 static struct platform_driver atmci_driver
= {
2572 .probe
= atmci_probe
,
2573 .remove
= atmci_remove
,
2575 .name
= "atmel_mci",
2576 .of_match_table
= of_match_ptr(atmci_dt_ids
),
2577 .pm
= &atmci_dev_pm_ops
,
2580 module_platform_driver(atmci_driver
);
2582 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
2583 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2584 MODULE_LICENSE("GPL v2");