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/stat.h>
27 #include <linux/mmc/host.h>
29 #include <mach/atmel-mci.h>
30 #include <linux/atmel-mci.h>
33 #include <asm/unaligned.h>
36 #include <mach/board.h>
38 #include "atmel-mci-regs.h"
40 #define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
41 #define ATMCI_DMA_THRESHOLD 16
44 EVENT_CMD_COMPLETE = 0,
50 enum atmel_mci_state {
59 struct atmel_mci_dma {
60 #ifdef CONFIG_MMC_ATMELMCI_DMA
61 struct dma_chan *chan;
62 struct dma_async_tx_descriptor *data_desc;
67 * struct atmel_mci - MMC controller state shared between all slots
68 * @lock: Spinlock protecting the queue and associated data.
69 * @regs: Pointer to MMIO registers.
70 * @sg: Scatterlist entry currently being processed by PIO code, if any.
71 * @pio_offset: Offset into the current scatterlist entry.
72 * @cur_slot: The slot which is currently using the controller.
73 * @mrq: The request currently being processed on @cur_slot,
74 * or NULL if the controller is idle.
75 * @cmd: The command currently being sent to the card, or NULL.
76 * @data: The data currently being transferred, or NULL if no data
77 * transfer is in progress.
78 * @dma: DMA client state.
79 * @data_chan: DMA channel being used for the current data transfer.
80 * @cmd_status: Snapshot of SR taken upon completion of the current
81 * command. Only valid when EVENT_CMD_COMPLETE is pending.
82 * @data_status: Snapshot of SR taken upon completion of the current
83 * data transfer. Only valid when EVENT_DATA_COMPLETE or
84 * EVENT_DATA_ERROR is pending.
85 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
87 * @tasklet: Tasklet running the request state machine.
88 * @pending_events: Bitmask of events flagged by the interrupt handler
89 * to be processed by the tasklet.
90 * @completed_events: Bitmask of events which the state machine has
92 * @state: Tasklet state.
93 * @queue: List of slots waiting for access to the controller.
94 * @need_clock_update: Update the clock rate before the next request.
95 * @need_reset: Reset controller before next request.
96 * @mode_reg: Value of the MR register.
97 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
98 * rate and timeout calculations.
99 * @mapbase: Physical address of the MMIO registers.
100 * @mck: The peripheral bus clock hooked up to the MMC controller.
101 * @pdev: Platform device associated with the MMC controller.
102 * @slot: Slots sharing this MMC controller.
107 * @lock is a softirq-safe spinlock protecting @queue as well as
108 * @cur_slot, @mrq and @state. These must always be updated
109 * at the same time while holding @lock.
111 * @lock also protects mode_reg and need_clock_update since these are
112 * used to synchronize mode register updates with the queue
115 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
116 * and must always be written at the same time as the slot is added to
119 * @pending_events and @completed_events are accessed using atomic bit
120 * operations, so they don't need any locking.
122 * None of the fields touched by the interrupt handler need any
123 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
124 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
125 * interrupts must be disabled and @data_status updated with a
126 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
127 * CMDRDY interupt must be disabled and @cmd_status updated with a
128 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
129 * bytes_xfered field of @data must be written. This is ensured by
136 struct scatterlist *sg;
137 unsigned int pio_offset;
139 struct atmel_mci_slot *cur_slot;
140 struct mmc_request *mrq;
141 struct mmc_command *cmd;
142 struct mmc_data *data;
144 struct atmel_mci_dma dma;
145 struct dma_chan *data_chan;
151 struct tasklet_struct tasklet;
152 unsigned long pending_events;
153 unsigned long completed_events;
154 enum atmel_mci_state state;
155 struct list_head queue;
157 bool need_clock_update;
160 unsigned long bus_hz;
161 unsigned long mapbase;
163 struct platform_device *pdev;
165 struct atmel_mci_slot *slot[ATMEL_MCI_MAX_NR_SLOTS];
169 * struct atmel_mci_slot - MMC slot state
170 * @mmc: The mmc_host representing this slot.
171 * @host: The MMC controller this slot is using.
172 * @sdc_reg: Value of SDCR to be written before using this slot.
173 * @mrq: mmc_request currently being processed or waiting to be
174 * processed, or NULL when the slot is idle.
175 * @queue_node: List node for placing this node in the @queue list of
177 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
178 * @flags: Random state bits associated with the slot.
179 * @detect_pin: GPIO pin used for card detection, or negative if not
181 * @wp_pin: GPIO pin used for card write protect sending, or negative
183 * @detect_is_active_high: The state of the detect pin when it is active.
184 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
186 struct atmel_mci_slot {
187 struct mmc_host *mmc;
188 struct atmel_mci *host;
192 struct mmc_request *mrq;
193 struct list_head queue_node;
197 #define ATMCI_CARD_PRESENT 0
198 #define ATMCI_CARD_NEED_INIT 1
199 #define ATMCI_SHUTDOWN 2
203 bool detect_is_active_high;
205 struct timer_list detect_timer;
208 #define atmci_test_and_clear_pending(host, event) \
209 test_and_clear_bit(event, &host->pending_events)
210 #define atmci_set_completed(host, event) \
211 set_bit(event, &host->completed_events)
212 #define atmci_set_pending(host, event) \
213 set_bit(event, &host->pending_events)
216 * Enable or disable features/registers based on
217 * whether the processor supports them
219 static bool mci_has_rwproof(void)
221 if (cpu_is_at91sam9261() || cpu_is_at91rm9200())
228 * The debugfs stuff below is mostly optimized away when
229 * CONFIG_DEBUG_FS is not set.
231 static int atmci_req_show(struct seq_file *s, void *v)
233 struct atmel_mci_slot *slot = s->private;
234 struct mmc_request *mrq;
235 struct mmc_command *cmd;
236 struct mmc_command *stop;
237 struct mmc_data *data;
239 /* Make sure we get a consistent snapshot */
240 spin_lock_bh(&slot->host->lock);
250 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
251 cmd->opcode, cmd->arg, cmd->flags,
252 cmd->resp[0], cmd->resp[1], cmd->resp[2],
253 cmd->resp[2], cmd->error);
255 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
256 data->bytes_xfered, data->blocks,
257 data->blksz, data->flags, data->error);
260 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
261 stop->opcode, stop->arg, stop->flags,
262 stop->resp[0], stop->resp[1], stop->resp[2],
263 stop->resp[2], stop->error);
266 spin_unlock_bh(&slot->host->lock);
271 static int atmci_req_open(struct inode *inode, struct file *file)
273 return single_open(file, atmci_req_show, inode->i_private);
276 static const struct file_operations atmci_req_fops = {
277 .owner = THIS_MODULE,
278 .open = atmci_req_open,
281 .release = single_release,
284 static void atmci_show_status_reg(struct seq_file *s,
285 const char *regname, u32 value)
287 static const char *sr_bit[] = {
318 seq_printf(s, "%s:\t0x%08x", regname, value);
319 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
320 if (value & (1 << i)) {
322 seq_printf(s, " %s", sr_bit[i]);
324 seq_puts(s, " UNKNOWN");
330 static int atmci_regs_show(struct seq_file *s, void *v)
332 struct atmel_mci *host = s->private;
335 buf = kmalloc(MCI_REGS_SIZE, GFP_KERNEL);
340 * Grab a more or less consistent snapshot. Note that we're
341 * not disabling interrupts, so IMR and SR may not be
344 spin_lock_bh(&host->lock);
345 clk_enable(host->mck);
346 memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
347 clk_disable(host->mck);
348 spin_unlock_bh(&host->lock);
350 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
352 buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
353 buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
354 buf[MCI_MR / 4] & 0xff);
355 seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
356 seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
357 seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
358 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
360 buf[MCI_BLKR / 4] & 0xffff,
361 (buf[MCI_BLKR / 4] >> 16) & 0xffff);
363 /* Don't read RSPR and RDR; it will consume the data there */
365 atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
366 atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
373 static int atmci_regs_open(struct inode *inode, struct file *file)
375 return single_open(file, atmci_regs_show, inode->i_private);
378 static const struct file_operations atmci_regs_fops = {
379 .owner = THIS_MODULE,
380 .open = atmci_regs_open,
383 .release = single_release,
386 static void atmci_init_debugfs(struct atmel_mci_slot *slot)
388 struct mmc_host *mmc = slot->mmc;
389 struct atmel_mci *host = slot->host;
393 root = mmc->debugfs_root;
397 node = debugfs_create_file("regs", S_IRUSR, root, host,
404 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
408 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
412 node = debugfs_create_x32("pending_events", S_IRUSR, root,
413 (u32 *)&host->pending_events);
417 node = debugfs_create_x32("completed_events", S_IRUSR, root,
418 (u32 *)&host->completed_events);
425 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
428 static inline unsigned int ns_to_clocks(struct atmel_mci *host,
431 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
434 static void atmci_set_timeout(struct atmel_mci *host,
435 struct atmel_mci_slot *slot, struct mmc_data *data)
437 static unsigned dtomul_to_shift[] = {
438 0, 4, 7, 8, 10, 12, 16, 20
444 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
446 for (dtomul = 0; dtomul < 8; dtomul++) {
447 unsigned shift = dtomul_to_shift[dtomul];
448 dtocyc = (timeout + (1 << shift) - 1) >> shift;
458 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
459 dtocyc << dtomul_to_shift[dtomul]);
460 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
464 * Return mask with command flags to be enabled for this command.
466 static u32 atmci_prepare_command(struct mmc_host *mmc,
467 struct mmc_command *cmd)
469 struct mmc_data *data;
472 cmd->error = -EINPROGRESS;
474 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
476 if (cmd->flags & MMC_RSP_PRESENT) {
477 if (cmd->flags & MMC_RSP_136)
478 cmdr |= MCI_CMDR_RSPTYP_136BIT;
480 cmdr |= MCI_CMDR_RSPTYP_48BIT;
484 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
485 * it's too difficult to determine whether this is an ACMD or
486 * not. Better make it 64.
488 cmdr |= MCI_CMDR_MAXLAT_64CYC;
490 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
491 cmdr |= MCI_CMDR_OPDCMD;
495 cmdr |= MCI_CMDR_START_XFER;
496 if (data->flags & MMC_DATA_STREAM)
497 cmdr |= MCI_CMDR_STREAM;
498 else if (data->blocks > 1)
499 cmdr |= MCI_CMDR_MULTI_BLOCK;
501 cmdr |= MCI_CMDR_BLOCK;
503 if (data->flags & MMC_DATA_READ)
504 cmdr |= MCI_CMDR_TRDIR_READ;
510 static void atmci_start_command(struct atmel_mci *host,
511 struct mmc_command *cmd, u32 cmd_flags)
516 dev_vdbg(&host->pdev->dev,
517 "start command: ARGR=0x%08x CMDR=0x%08x\n",
518 cmd->arg, cmd_flags);
520 mci_writel(host, ARGR, cmd->arg);
521 mci_writel(host, CMDR, cmd_flags);
524 static void send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
526 atmci_start_command(host, data->stop, host->stop_cmdr);
527 mci_writel(host, IER, MCI_CMDRDY);
530 #ifdef CONFIG_MMC_ATMELMCI_DMA
531 static void atmci_dma_cleanup(struct atmel_mci *host)
533 struct mmc_data *data = host->data;
535 dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
536 ((data->flags & MMC_DATA_WRITE)
537 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
540 static void atmci_stop_dma(struct atmel_mci *host)
542 struct dma_chan *chan = host->data_chan;
545 chan->device->device_terminate_all(chan);
546 atmci_dma_cleanup(host);
548 /* Data transfer was stopped by the interrupt handler */
549 atmci_set_pending(host, EVENT_XFER_COMPLETE);
550 mci_writel(host, IER, MCI_NOTBUSY);
554 /* This function is called by the DMA driver from tasklet context. */
555 static void atmci_dma_complete(void *arg)
557 struct atmel_mci *host = arg;
558 struct mmc_data *data = host->data;
560 dev_vdbg(&host->pdev->dev, "DMA complete\n");
562 atmci_dma_cleanup(host);
565 * If the card was removed, data will be NULL. No point trying
566 * to send the stop command or waiting for NBUSY in this case.
569 atmci_set_pending(host, EVENT_XFER_COMPLETE);
570 tasklet_schedule(&host->tasklet);
573 * Regardless of what the documentation says, we have
574 * to wait for NOTBUSY even after block read
577 * When the DMA transfer is complete, the controller
578 * may still be reading the CRC from the card, i.e.
579 * the data transfer is still in progress and we
580 * haven't seen all the potential error bits yet.
582 * The interrupt handler will schedule a different
583 * tasklet to finish things up when the data transfer
584 * is completely done.
586 * We may not complete the mmc request here anyway
587 * because the mmc layer may call back and cause us to
588 * violate the "don't submit new operations from the
589 * completion callback" rule of the dma engine
592 mci_writel(host, IER, MCI_NOTBUSY);
597 atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
599 struct dma_chan *chan;
600 struct dma_async_tx_descriptor *desc;
601 struct scatterlist *sg;
603 enum dma_data_direction direction;
607 * We don't do DMA on "complex" transfers, i.e. with
608 * non-word-aligned buffers or lengths. Also, we don't bother
609 * with all the DMA setup overhead for short transfers.
611 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
616 for_each_sg(data->sg, sg, data->sg_len, i) {
617 if (sg->offset & 3 || sg->length & 3)
621 /* If we don't have a channel, we can't do DMA */
622 chan = host->dma.chan;
624 host->data_chan = chan;
629 if (data->flags & MMC_DATA_READ)
630 direction = DMA_FROM_DEVICE;
632 direction = DMA_TO_DEVICE;
634 sglen = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, direction);
635 if (sglen != data->sg_len)
637 desc = chan->device->device_prep_slave_sg(chan,
638 data->sg, data->sg_len, direction,
639 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
643 host->dma.data_desc = desc;
644 desc->callback = atmci_dma_complete;
645 desc->callback_param = host;
646 desc->tx_submit(desc);
649 chan->device->device_issue_pending(chan);
653 dma_unmap_sg(&host->pdev->dev, data->sg, sglen, direction);
657 #else /* CONFIG_MMC_ATMELMCI_DMA */
659 static int atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
664 static void atmci_stop_dma(struct atmel_mci *host)
666 /* Data transfer was stopped by the interrupt handler */
667 atmci_set_pending(host, EVENT_XFER_COMPLETE);
668 mci_writel(host, IER, MCI_NOTBUSY);
671 #endif /* CONFIG_MMC_ATMELMCI_DMA */
674 * Returns a mask of interrupt flags to be enabled after the whole
675 * request has been prepared.
677 static u32 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
681 data->error = -EINPROGRESS;
687 iflags = ATMCI_DATA_ERROR_FLAGS;
688 if (atmci_submit_data_dma(host, data)) {
689 host->data_chan = NULL;
692 * Errata: MMC data write operation with less than 12
693 * bytes is impossible.
695 * Errata: MCI Transmit Data Register (TDR) FIFO
696 * corruption when length is not multiple of 4.
698 if (data->blocks * data->blksz < 12
699 || (data->blocks * data->blksz) & 3)
700 host->need_reset = true;
703 host->pio_offset = 0;
704 if (data->flags & MMC_DATA_READ)
713 static void atmci_start_request(struct atmel_mci *host,
714 struct atmel_mci_slot *slot)
716 struct mmc_request *mrq;
717 struct mmc_command *cmd;
718 struct mmc_data *data;
723 host->cur_slot = slot;
726 host->pending_events = 0;
727 host->completed_events = 0;
728 host->data_status = 0;
730 if (host->need_reset) {
731 mci_writel(host, CR, MCI_CR_SWRST);
732 mci_writel(host, CR, MCI_CR_MCIEN);
733 mci_writel(host, MR, host->mode_reg);
734 host->need_reset = false;
736 mci_writel(host, SDCR, slot->sdc_reg);
738 iflags = mci_readl(host, IMR);
740 dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
743 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
744 /* Send init sequence (74 clock cycles) */
745 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
746 while (!(mci_readl(host, SR) & MCI_CMDRDY))
751 atmci_set_timeout(host, slot, data);
753 /* Must set block count/size before sending command */
754 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
755 | MCI_BLKLEN(data->blksz));
756 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
757 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
762 cmdflags = atmci_prepare_command(slot->mmc, cmd);
763 atmci_start_command(host, cmd, cmdflags);
766 iflags |= atmci_submit_data(host, data);
769 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
770 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
771 if (!(data->flags & MMC_DATA_WRITE))
772 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
773 if (data->flags & MMC_DATA_STREAM)
774 host->stop_cmdr |= MCI_CMDR_STREAM;
776 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
780 * We could have enabled interrupts earlier, but I suspect
781 * that would open up a nice can of interesting race
782 * conditions (e.g. command and data complete, but stop not
785 mci_writel(host, IER, iflags);
788 static void atmci_queue_request(struct atmel_mci *host,
789 struct atmel_mci_slot *slot, struct mmc_request *mrq)
791 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
794 spin_lock_bh(&host->lock);
796 if (host->state == STATE_IDLE) {
797 host->state = STATE_SENDING_CMD;
798 atmci_start_request(host, slot);
800 list_add_tail(&slot->queue_node, &host->queue);
802 spin_unlock_bh(&host->lock);
805 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
807 struct atmel_mci_slot *slot = mmc_priv(mmc);
808 struct atmel_mci *host = slot->host;
809 struct mmc_data *data;
814 * We may "know" the card is gone even though there's still an
815 * electrical connection. If so, we really need to communicate
816 * this to the MMC core since there won't be any more
817 * interrupts as the card is completely removed. Otherwise,
818 * the MMC core might believe the card is still there even
819 * though the card was just removed very slowly.
821 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
822 mrq->cmd->error = -ENOMEDIUM;
823 mmc_request_done(mmc, mrq);
827 /* We don't support multiple blocks of weird lengths. */
829 if (data && data->blocks > 1 && data->blksz & 3) {
830 mrq->cmd->error = -EINVAL;
831 mmc_request_done(mmc, mrq);
834 atmci_queue_request(host, slot, mrq);
837 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
839 struct atmel_mci_slot *slot = mmc_priv(mmc);
840 struct atmel_mci *host = slot->host;
843 slot->sdc_reg &= ~MCI_SDCBUS_MASK;
844 switch (ios->bus_width) {
845 case MMC_BUS_WIDTH_1:
846 slot->sdc_reg |= MCI_SDCBUS_1BIT;
848 case MMC_BUS_WIDTH_4:
849 slot->sdc_reg |= MCI_SDCBUS_4BIT;
854 unsigned int clock_min = ~0U;
857 spin_lock_bh(&host->lock);
858 if (!host->mode_reg) {
859 clk_enable(host->mck);
860 mci_writel(host, CR, MCI_CR_SWRST);
861 mci_writel(host, CR, MCI_CR_MCIEN);
865 * Use mirror of ios->clock to prevent race with mmc
866 * core ios update when finding the minimum.
868 slot->clock = ios->clock;
869 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
870 if (host->slot[i] && host->slot[i]->clock
871 && host->slot[i]->clock < clock_min)
872 clock_min = host->slot[i]->clock;
875 /* Calculate clock divider */
876 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
878 dev_warn(&mmc->class_dev,
879 "clock %u too slow; using %lu\n",
880 clock_min, host->bus_hz / (2 * 256));
884 host->mode_reg = MCI_MR_CLKDIV(clkdiv);
887 * WRPROOF and RDPROOF prevent overruns/underruns by
888 * stopping the clock when the FIFO is full/empty.
889 * This state is not expected to last for long.
891 if (mci_has_rwproof())
892 host->mode_reg |= (MCI_MR_WRPROOF | MCI_MR_RDPROOF);
894 if (list_empty(&host->queue))
895 mci_writel(host, MR, host->mode_reg);
897 host->need_clock_update = true;
899 spin_unlock_bh(&host->lock);
901 bool any_slot_active = false;
903 spin_lock_bh(&host->lock);
905 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
906 if (host->slot[i] && host->slot[i]->clock) {
907 any_slot_active = true;
911 if (!any_slot_active) {
912 mci_writel(host, CR, MCI_CR_MCIDIS);
913 if (host->mode_reg) {
915 clk_disable(host->mck);
919 spin_unlock_bh(&host->lock);
922 switch (ios->power_mode) {
924 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
928 * TODO: None of the currently available AVR32-based
929 * boards allow MMC power to be turned off. Implement
930 * power control when this can be tested properly.
932 * We also need to hook this into the clock management
933 * somehow so that newly inserted cards aren't
934 * subjected to a fast clock before we have a chance
935 * to figure out what the maximum rate is. Currently,
936 * there's no way to avoid this, and there never will
937 * be for boards that don't support power control.
943 static int atmci_get_ro(struct mmc_host *mmc)
945 int read_only = -ENOSYS;
946 struct atmel_mci_slot *slot = mmc_priv(mmc);
948 if (gpio_is_valid(slot->wp_pin)) {
949 read_only = gpio_get_value(slot->wp_pin);
950 dev_dbg(&mmc->class_dev, "card is %s\n",
951 read_only ? "read-only" : "read-write");
957 static int atmci_get_cd(struct mmc_host *mmc)
959 int present = -ENOSYS;
960 struct atmel_mci_slot *slot = mmc_priv(mmc);
962 if (gpio_is_valid(slot->detect_pin)) {
963 present = !(gpio_get_value(slot->detect_pin) ^
964 slot->detect_is_active_high);
965 dev_dbg(&mmc->class_dev, "card is %spresent\n",
966 present ? "" : "not ");
972 static const struct mmc_host_ops atmci_ops = {
973 .request = atmci_request,
974 .set_ios = atmci_set_ios,
975 .get_ro = atmci_get_ro,
976 .get_cd = atmci_get_cd,
979 /* Called with host->lock held */
980 static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
981 __releases(&host->lock)
982 __acquires(&host->lock)
984 struct atmel_mci_slot *slot = NULL;
985 struct mmc_host *prev_mmc = host->cur_slot->mmc;
987 WARN_ON(host->cmd || host->data);
990 * Update the MMC clock rate if necessary. This may be
991 * necessary if set_ios() is called when a different slot is
992 * busy transfering data.
994 if (host->need_clock_update)
995 mci_writel(host, MR, host->mode_reg);
997 host->cur_slot->mrq = NULL;
999 if (!list_empty(&host->queue)) {
1000 slot = list_entry(host->queue.next,
1001 struct atmel_mci_slot, queue_node);
1002 list_del(&slot->queue_node);
1003 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1004 mmc_hostname(slot->mmc));
1005 host->state = STATE_SENDING_CMD;
1006 atmci_start_request(host, slot);
1008 dev_vdbg(&host->pdev->dev, "list empty\n");
1009 host->state = STATE_IDLE;
1012 spin_unlock(&host->lock);
1013 mmc_request_done(prev_mmc, mrq);
1014 spin_lock(&host->lock);
1017 static void atmci_command_complete(struct atmel_mci *host,
1018 struct mmc_command *cmd)
1020 u32 status = host->cmd_status;
1022 /* Read the response from the card (up to 16 bytes) */
1023 cmd->resp[0] = mci_readl(host, RSPR);
1024 cmd->resp[1] = mci_readl(host, RSPR);
1025 cmd->resp[2] = mci_readl(host, RSPR);
1026 cmd->resp[3] = mci_readl(host, RSPR);
1028 if (status & MCI_RTOE)
1029 cmd->error = -ETIMEDOUT;
1030 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
1031 cmd->error = -EILSEQ;
1032 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
1038 dev_dbg(&host->pdev->dev,
1039 "command error: status=0x%08x\n", status);
1043 atmci_stop_dma(host);
1044 mci_writel(host, IDR, MCI_NOTBUSY
1045 | MCI_TXRDY | MCI_RXRDY
1046 | ATMCI_DATA_ERROR_FLAGS);
1051 static void atmci_detect_change(unsigned long data)
1053 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1058 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1059 * freeing the interrupt. We must not re-enable the interrupt
1060 * if it has been freed, and if we're shutting down, it
1061 * doesn't really matter whether the card is present or not.
1064 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
1067 enable_irq(gpio_to_irq(slot->detect_pin));
1068 present = !(gpio_get_value(slot->detect_pin) ^
1069 slot->detect_is_active_high);
1070 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
1072 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1073 present, present_old);
1075 if (present != present_old) {
1076 struct atmel_mci *host = slot->host;
1077 struct mmc_request *mrq;
1079 dev_dbg(&slot->mmc->class_dev, "card %s\n",
1080 present ? "inserted" : "removed");
1082 spin_lock(&host->lock);
1085 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1087 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1089 /* Clean up queue if present */
1092 if (mrq == host->mrq) {
1094 * Reset controller to terminate any ongoing
1095 * commands or data transfers.
1097 mci_writel(host, CR, MCI_CR_SWRST);
1098 mci_writel(host, CR, MCI_CR_MCIEN);
1099 mci_writel(host, MR, host->mode_reg);
1104 switch (host->state) {
1107 case STATE_SENDING_CMD:
1108 mrq->cmd->error = -ENOMEDIUM;
1112 case STATE_SENDING_DATA:
1113 mrq->data->error = -ENOMEDIUM;
1114 atmci_stop_dma(host);
1116 case STATE_DATA_BUSY:
1117 case STATE_DATA_ERROR:
1118 if (mrq->data->error == -EINPROGRESS)
1119 mrq->data->error = -ENOMEDIUM;
1123 case STATE_SENDING_STOP:
1124 mrq->stop->error = -ENOMEDIUM;
1128 atmci_request_end(host, mrq);
1130 list_del(&slot->queue_node);
1131 mrq->cmd->error = -ENOMEDIUM;
1133 mrq->data->error = -ENOMEDIUM;
1135 mrq->stop->error = -ENOMEDIUM;
1137 spin_unlock(&host->lock);
1138 mmc_request_done(slot->mmc, mrq);
1139 spin_lock(&host->lock);
1142 spin_unlock(&host->lock);
1144 mmc_detect_change(slot->mmc, 0);
1148 static void atmci_tasklet_func(unsigned long priv)
1150 struct atmel_mci *host = (struct atmel_mci *)priv;
1151 struct mmc_request *mrq = host->mrq;
1152 struct mmc_data *data = host->data;
1153 struct mmc_command *cmd = host->cmd;
1154 enum atmel_mci_state state = host->state;
1155 enum atmel_mci_state prev_state;
1158 spin_lock(&host->lock);
1160 state = host->state;
1162 dev_vdbg(&host->pdev->dev,
1163 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1164 state, host->pending_events, host->completed_events,
1165 mci_readl(host, IMR));
1174 case STATE_SENDING_CMD:
1175 if (!atmci_test_and_clear_pending(host,
1176 EVENT_CMD_COMPLETE))
1180 atmci_set_completed(host, EVENT_CMD_COMPLETE);
1181 atmci_command_complete(host, mrq->cmd);
1182 if (!mrq->data || cmd->error) {
1183 atmci_request_end(host, host->mrq);
1187 prev_state = state = STATE_SENDING_DATA;
1190 case STATE_SENDING_DATA:
1191 if (atmci_test_and_clear_pending(host,
1192 EVENT_DATA_ERROR)) {
1193 atmci_stop_dma(host);
1195 send_stop_cmd(host, data);
1196 state = STATE_DATA_ERROR;
1200 if (!atmci_test_and_clear_pending(host,
1201 EVENT_XFER_COMPLETE))
1204 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1205 prev_state = state = STATE_DATA_BUSY;
1208 case STATE_DATA_BUSY:
1209 if (!atmci_test_and_clear_pending(host,
1210 EVENT_DATA_COMPLETE))
1214 atmci_set_completed(host, EVENT_DATA_COMPLETE);
1215 status = host->data_status;
1216 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
1217 if (status & MCI_DTOE) {
1218 dev_dbg(&host->pdev->dev,
1219 "data timeout error\n");
1220 data->error = -ETIMEDOUT;
1221 } else if (status & MCI_DCRCE) {
1222 dev_dbg(&host->pdev->dev,
1223 "data CRC error\n");
1224 data->error = -EILSEQ;
1226 dev_dbg(&host->pdev->dev,
1227 "data FIFO error (status=%08x)\n",
1232 data->bytes_xfered = data->blocks * data->blksz;
1237 atmci_request_end(host, host->mrq);
1241 prev_state = state = STATE_SENDING_STOP;
1243 send_stop_cmd(host, data);
1246 case STATE_SENDING_STOP:
1247 if (!atmci_test_and_clear_pending(host,
1248 EVENT_CMD_COMPLETE))
1252 atmci_command_complete(host, mrq->stop);
1253 atmci_request_end(host, host->mrq);
1256 case STATE_DATA_ERROR:
1257 if (!atmci_test_and_clear_pending(host,
1258 EVENT_XFER_COMPLETE))
1261 state = STATE_DATA_BUSY;
1264 } while (state != prev_state);
1266 host->state = state;
1269 spin_unlock(&host->lock);
1272 static void atmci_read_data_pio(struct atmel_mci *host)
1274 struct scatterlist *sg = host->sg;
1275 void *buf = sg_virt(sg);
1276 unsigned int offset = host->pio_offset;
1277 struct mmc_data *data = host->data;
1280 unsigned int nbytes = 0;
1283 value = mci_readl(host, RDR);
1284 if (likely(offset + 4 <= sg->length)) {
1285 put_unaligned(value, (u32 *)(buf + offset));
1290 if (offset == sg->length) {
1291 flush_dcache_page(sg_page(sg));
1292 host->sg = sg = sg_next(sg);
1300 unsigned int remaining = sg->length - offset;
1301 memcpy(buf + offset, &value, remaining);
1302 nbytes += remaining;
1304 flush_dcache_page(sg_page(sg));
1305 host->sg = sg = sg_next(sg);
1309 offset = 4 - remaining;
1311 memcpy(buf, (u8 *)&value + remaining, offset);
1315 status = mci_readl(host, SR);
1316 if (status & ATMCI_DATA_ERROR_FLAGS) {
1317 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
1318 | ATMCI_DATA_ERROR_FLAGS));
1319 host->data_status = status;
1320 data->bytes_xfered += nbytes;
1322 atmci_set_pending(host, EVENT_DATA_ERROR);
1323 tasklet_schedule(&host->tasklet);
1326 } while (status & MCI_RXRDY);
1328 host->pio_offset = offset;
1329 data->bytes_xfered += nbytes;
1334 mci_writel(host, IDR, MCI_RXRDY);
1335 mci_writel(host, IER, MCI_NOTBUSY);
1336 data->bytes_xfered += nbytes;
1338 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1341 static void atmci_write_data_pio(struct atmel_mci *host)
1343 struct scatterlist *sg = host->sg;
1344 void *buf = sg_virt(sg);
1345 unsigned int offset = host->pio_offset;
1346 struct mmc_data *data = host->data;
1349 unsigned int nbytes = 0;
1352 if (likely(offset + 4 <= sg->length)) {
1353 value = get_unaligned((u32 *)(buf + offset));
1354 mci_writel(host, TDR, value);
1358 if (offset == sg->length) {
1359 host->sg = sg = sg_next(sg);
1367 unsigned int remaining = sg->length - offset;
1370 memcpy(&value, buf + offset, remaining);
1371 nbytes += remaining;
1373 host->sg = sg = sg_next(sg);
1375 mci_writel(host, TDR, value);
1379 offset = 4 - remaining;
1381 memcpy((u8 *)&value + remaining, buf, offset);
1382 mci_writel(host, TDR, value);
1386 status = mci_readl(host, SR);
1387 if (status & ATMCI_DATA_ERROR_FLAGS) {
1388 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
1389 | ATMCI_DATA_ERROR_FLAGS));
1390 host->data_status = status;
1391 data->bytes_xfered += nbytes;
1393 atmci_set_pending(host, EVENT_DATA_ERROR);
1394 tasklet_schedule(&host->tasklet);
1397 } while (status & MCI_TXRDY);
1399 host->pio_offset = offset;
1400 data->bytes_xfered += nbytes;
1405 mci_writel(host, IDR, MCI_TXRDY);
1406 mci_writel(host, IER, MCI_NOTBUSY);
1407 data->bytes_xfered += nbytes;
1409 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1412 static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
1414 mci_writel(host, IDR, MCI_CMDRDY);
1416 host->cmd_status = status;
1418 atmci_set_pending(host, EVENT_CMD_COMPLETE);
1419 tasklet_schedule(&host->tasklet);
1422 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1424 struct atmel_mci *host = dev_id;
1425 u32 status, mask, pending;
1426 unsigned int pass_count = 0;
1429 status = mci_readl(host, SR);
1430 mask = mci_readl(host, IMR);
1431 pending = status & mask;
1435 if (pending & ATMCI_DATA_ERROR_FLAGS) {
1436 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
1437 | MCI_RXRDY | MCI_TXRDY);
1438 pending &= mci_readl(host, IMR);
1440 host->data_status = status;
1442 atmci_set_pending(host, EVENT_DATA_ERROR);
1443 tasklet_schedule(&host->tasklet);
1445 if (pending & MCI_NOTBUSY) {
1446 mci_writel(host, IDR,
1447 ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY);
1448 if (!host->data_status)
1449 host->data_status = status;
1451 atmci_set_pending(host, EVENT_DATA_COMPLETE);
1452 tasklet_schedule(&host->tasklet);
1454 if (pending & MCI_RXRDY)
1455 atmci_read_data_pio(host);
1456 if (pending & MCI_TXRDY)
1457 atmci_write_data_pio(host);
1459 if (pending & MCI_CMDRDY)
1460 atmci_cmd_interrupt(host, status);
1461 } while (pass_count++ < 5);
1463 return pass_count ? IRQ_HANDLED : IRQ_NONE;
1466 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1468 struct atmel_mci_slot *slot = dev_id;
1471 * Disable interrupts until the pin has stabilized and check
1472 * the state then. Use mod_timer() since we may be in the
1473 * middle of the timer routine when this interrupt triggers.
1475 disable_irq_nosync(irq);
1476 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
1481 static int __init atmci_init_slot(struct atmel_mci *host,
1482 struct mci_slot_pdata *slot_data, unsigned int id,
1485 struct mmc_host *mmc;
1486 struct atmel_mci_slot *slot;
1488 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1492 slot = mmc_priv(mmc);
1495 slot->detect_pin = slot_data->detect_pin;
1496 slot->wp_pin = slot_data->wp_pin;
1497 slot->detect_is_active_high = slot_data->detect_is_active_high;
1498 slot->sdc_reg = sdc_reg;
1500 mmc->ops = &atmci_ops;
1501 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
1502 mmc->f_max = host->bus_hz / 2;
1503 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1504 if (slot_data->bus_width >= 4)
1505 mmc->caps |= MMC_CAP_4_BIT_DATA;
1507 mmc->max_hw_segs = 64;
1508 mmc->max_phys_segs = 64;
1509 mmc->max_req_size = 32768 * 512;
1510 mmc->max_blk_size = 32768;
1511 mmc->max_blk_count = 512;
1513 /* Assume card is present initially */
1514 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1515 if (gpio_is_valid(slot->detect_pin)) {
1516 if (gpio_request(slot->detect_pin, "mmc_detect")) {
1517 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1518 slot->detect_pin = -EBUSY;
1519 } else if (gpio_get_value(slot->detect_pin) ^
1520 slot->detect_is_active_high) {
1521 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1525 if (!gpio_is_valid(slot->detect_pin))
1526 mmc->caps |= MMC_CAP_NEEDS_POLL;
1528 if (gpio_is_valid(slot->wp_pin)) {
1529 if (gpio_request(slot->wp_pin, "mmc_wp")) {
1530 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1531 slot->wp_pin = -EBUSY;
1535 host->slot[id] = slot;
1538 if (gpio_is_valid(slot->detect_pin)) {
1541 setup_timer(&slot->detect_timer, atmci_detect_change,
1542 (unsigned long)slot);
1544 ret = request_irq(gpio_to_irq(slot->detect_pin),
1545 atmci_detect_interrupt,
1546 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1547 "mmc-detect", slot);
1549 dev_dbg(&mmc->class_dev,
1550 "could not request IRQ %d for detect pin\n",
1551 gpio_to_irq(slot->detect_pin));
1552 gpio_free(slot->detect_pin);
1553 slot->detect_pin = -EBUSY;
1557 atmci_init_debugfs(slot);
1562 static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
1565 /* Debugfs stuff is cleaned up by mmc core */
1567 set_bit(ATMCI_SHUTDOWN, &slot->flags);
1570 mmc_remove_host(slot->mmc);
1572 if (gpio_is_valid(slot->detect_pin)) {
1573 int pin = slot->detect_pin;
1575 free_irq(gpio_to_irq(pin), slot);
1576 del_timer_sync(&slot->detect_timer);
1579 if (gpio_is_valid(slot->wp_pin))
1580 gpio_free(slot->wp_pin);
1582 slot->host->slot[id] = NULL;
1583 mmc_free_host(slot->mmc);
1586 #ifdef CONFIG_MMC_ATMELMCI_DMA
1587 static bool filter(struct dma_chan *chan, void *slave)
1589 struct mci_dma_data *sl = slave;
1591 if (sl && find_slave_dev(sl) == chan->device->dev) {
1592 chan->private = slave_data_ptr(sl);
1599 static void atmci_configure_dma(struct atmel_mci *host)
1601 struct mci_platform_data *pdata;
1606 pdata = host->pdev->dev.platform_data;
1608 if (pdata && find_slave_dev(pdata->dma_slave)) {
1609 dma_cap_mask_t mask;
1611 setup_dma_addr(pdata->dma_slave,
1612 host->mapbase + MCI_TDR,
1613 host->mapbase + MCI_RDR);
1615 /* Try to grab a DMA channel */
1617 dma_cap_set(DMA_SLAVE, mask);
1619 dma_request_channel(mask, filter, pdata->dma_slave);
1621 if (!host->dma.chan)
1622 dev_notice(&host->pdev->dev, "DMA not available, using PIO\n");
1625 static void atmci_configure_dma(struct atmel_mci *host) {}
1628 static int __init atmci_probe(struct platform_device *pdev)
1630 struct mci_platform_data *pdata;
1631 struct atmel_mci *host;
1632 struct resource *regs;
1633 unsigned int nr_slots;
1637 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1640 pdata = pdev->dev.platform_data;
1643 irq = platform_get_irq(pdev, 0);
1647 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
1652 spin_lock_init(&host->lock);
1653 INIT_LIST_HEAD(&host->queue);
1655 host->mck = clk_get(&pdev->dev, "mci_clk");
1656 if (IS_ERR(host->mck)) {
1657 ret = PTR_ERR(host->mck);
1662 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1666 clk_enable(host->mck);
1667 mci_writel(host, CR, MCI_CR_SWRST);
1668 host->bus_hz = clk_get_rate(host->mck);
1669 clk_disable(host->mck);
1671 host->mapbase = regs->start;
1673 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
1675 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
1677 goto err_request_irq;
1679 atmci_configure_dma(host);
1681 platform_set_drvdata(pdev, host);
1683 /* We need at least one slot to succeed */
1686 if (pdata->slot[0].bus_width) {
1687 ret = atmci_init_slot(host, &pdata->slot[0],
1688 MCI_SDCSEL_SLOT_A, 0);
1692 if (pdata->slot[1].bus_width) {
1693 ret = atmci_init_slot(host, &pdata->slot[1],
1694 MCI_SDCSEL_SLOT_B, 1);
1700 dev_err(&pdev->dev, "init failed: no slot defined\n");
1704 dev_info(&pdev->dev,
1705 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
1706 host->mapbase, irq, nr_slots);
1711 #ifdef CONFIG_MMC_ATMELMCI_DMA
1713 dma_release_channel(host->dma.chan);
1715 free_irq(irq, host);
1717 iounmap(host->regs);
1725 static int __exit atmci_remove(struct platform_device *pdev)
1727 struct atmel_mci *host = platform_get_drvdata(pdev);
1730 platform_set_drvdata(pdev, NULL);
1732 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
1734 atmci_cleanup_slot(host->slot[i], i);
1737 clk_enable(host->mck);
1738 mci_writel(host, IDR, ~0UL);
1739 mci_writel(host, CR, MCI_CR_MCIDIS);
1740 mci_readl(host, SR);
1741 clk_disable(host->mck);
1743 #ifdef CONFIG_MMC_ATMELMCI_DMA
1745 dma_release_channel(host->dma.chan);
1748 free_irq(platform_get_irq(pdev, 0), host);
1749 iounmap(host->regs);
1757 static struct platform_driver atmci_driver = {
1758 .remove = __exit_p(atmci_remove),
1760 .name = "atmel_mci",
1764 static int __init atmci_init(void)
1766 return platform_driver_probe(&atmci_driver, atmci_probe);
1769 static void __exit atmci_exit(void)
1771 platform_driver_unregister(&atmci_driver);
1774 late_initcall(atmci_init); /* try to load after dma driver when built-in */
1775 module_exit(atmci_exit);
1777 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1778 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1779 MODULE_LICENSE("GPL v2");