/drivers/mmc/host/jz4740_mmc.c
C | 1029 lines | 815 code | 195 blank | 19 comment | 104 complexity | 78642457c8e0bda2065ba49567f5f304 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
- * JZ4740 SD/MMC controller driver
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
- #include <linux/mmc/host.h>
- #include <linux/err.h>
- #include <linux/io.h>
- #include <linux/irq.h>
- #include <linux/interrupt.h>
- #include <linux/module.h>
- #include <linux/platform_device.h>
- #include <linux/delay.h>
- #include <linux/scatterlist.h>
- #include <linux/clk.h>
- #include <linux/bitops.h>
- #include <linux/gpio.h>
- #include <asm/mach-jz4740/gpio.h>
- #include <asm/cacheflush.h>
- #include <linux/dma-mapping.h>
- #include <asm/mach-jz4740/jz4740_mmc.h>
- #define JZ_REG_MMC_STRPCL 0x00
- #define JZ_REG_MMC_STATUS 0x04
- #define JZ_REG_MMC_CLKRT 0x08
- #define JZ_REG_MMC_CMDAT 0x0C
- #define JZ_REG_MMC_RESTO 0x10
- #define JZ_REG_MMC_RDTO 0x14
- #define JZ_REG_MMC_BLKLEN 0x18
- #define JZ_REG_MMC_NOB 0x1C
- #define JZ_REG_MMC_SNOB 0x20
- #define JZ_REG_MMC_IMASK 0x24
- #define JZ_REG_MMC_IREG 0x28
- #define JZ_REG_MMC_CMD 0x2C
- #define JZ_REG_MMC_ARG 0x30
- #define JZ_REG_MMC_RESP_FIFO 0x34
- #define JZ_REG_MMC_RXFIFO 0x38
- #define JZ_REG_MMC_TXFIFO 0x3C
- #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
- #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
- #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
- #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
- #define JZ_MMC_STRPCL_RESET BIT(3)
- #define JZ_MMC_STRPCL_START_OP BIT(2)
- #define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
- #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
- #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
- #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
- #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
- #define JZ_MMC_STATUS_PRG_DONE BIT(13)
- #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
- #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
- #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
- #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
- #define JZ_MMC_STATUS_CLK_EN BIT(8)
- #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
- #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
- #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
- #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
- #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
- #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
- #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
- #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
- #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
- #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
- #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
- #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
- #define JZ_MMC_CMDAT_DMA_EN BIT(8)
- #define JZ_MMC_CMDAT_INIT BIT(7)
- #define JZ_MMC_CMDAT_BUSY BIT(6)
- #define JZ_MMC_CMDAT_STREAM BIT(5)
- #define JZ_MMC_CMDAT_WRITE BIT(4)
- #define JZ_MMC_CMDAT_DATA_EN BIT(3)
- #define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
- #define JZ_MMC_CMDAT_RSP_R1 1
- #define JZ_MMC_CMDAT_RSP_R2 2
- #define JZ_MMC_CMDAT_RSP_R3 3
- #define JZ_MMC_IRQ_SDIO BIT(7)
- #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
- #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
- #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
- #define JZ_MMC_IRQ_PRG_DONE BIT(1)
- #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
- #define JZ_MMC_CLK_RATE 24000000
- enum jz4740_mmc_state {
- JZ4740_MMC_STATE_READ_RESPONSE,
- JZ4740_MMC_STATE_TRANSFER_DATA,
- JZ4740_MMC_STATE_SEND_STOP,
- JZ4740_MMC_STATE_DONE,
- };
- struct jz4740_mmc_host {
- struct mmc_host *mmc;
- struct platform_device *pdev;
- struct jz4740_mmc_platform_data *pdata;
- struct clk *clk;
- int irq;
- int card_detect_irq;
- struct resource *mem;
- void __iomem *base;
- struct mmc_request *req;
- struct mmc_command *cmd;
- unsigned long waiting;
- uint32_t cmdat;
- uint16_t irq_mask;
- spinlock_t lock;
- struct timer_list timeout_timer;
- struct sg_mapping_iter miter;
- enum jz4740_mmc_state state;
- };
- static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
- unsigned int irq, bool enabled)
- {
- unsigned long flags;
- spin_lock_irqsave(&host->lock, flags);
- if (enabled)
- host->irq_mask &= ~irq;
- else
- host->irq_mask |= irq;
- spin_unlock_irqrestore(&host->lock, flags);
- writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
- }
- static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
- bool start_transfer)
- {
- uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
- if (start_transfer)
- val |= JZ_MMC_STRPCL_START_OP;
- writew(val, host->base + JZ_REG_MMC_STRPCL);
- }
- static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
- {
- uint32_t status;
- unsigned int timeout = 1000;
- writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
- do {
- status = readl(host->base + JZ_REG_MMC_STATUS);
- } while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
- }
- static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
- {
- uint32_t status;
- unsigned int timeout = 1000;
- writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
- udelay(10);
- do {
- status = readl(host->base + JZ_REG_MMC_STATUS);
- } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
- }
- static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
- {
- struct mmc_request *req;
- req = host->req;
- host->req = NULL;
- mmc_request_done(host->mmc, req);
- }
- static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
- unsigned int irq)
- {
- unsigned int timeout = 0x800;
- uint16_t status;
- do {
- status = readw(host->base + JZ_REG_MMC_IREG);
- } while (!(status & irq) && --timeout);
- if (timeout == 0) {
- set_bit(0, &host->waiting);
- mod_timer(&host->timeout_timer, jiffies + 5*HZ);
- jz4740_mmc_set_irq_enabled(host, irq, true);
- return true;
- }
- return false;
- }
- static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
- struct mmc_data *data)
- {
- int status;
- status = readl(host->base + JZ_REG_MMC_STATUS);
- if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
- if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
- host->req->cmd->error = -ETIMEDOUT;
- data->error = -ETIMEDOUT;
- } else {
- host->req->cmd->error = -EIO;
- data->error = -EIO;
- }
- }
- }
- static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
- struct mmc_data *data)
- {
- struct sg_mapping_iter *miter = &host->miter;
- void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
- uint32_t *buf;
- bool timeout;
- size_t i, j;
- while (sg_miter_next(miter)) {
- buf = miter->addr;
-