1
0
mirror of git://projects.qi-hardware.com/openwrt-xburst.git synced 2024-11-25 16:57:31 +02:00
openwrt-xburst/target/linux/xburst/files-2.6.31/drivers/mmc/host/jz_mmc.c

870 lines
22 KiB
C
Raw Normal View History

2009-08-20 12:38:17 +03:00
/*
2009-11-28 08:39:48 +02:00
* Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
* JZ7420/JZ4740 GPIO SD/MMC controller driver
2009-08-20 12:38:17 +03:00
*
2009-11-28 08:39:48 +02:00
* 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.
2009-08-20 12:38:17 +03:00
*
*/
2009-11-28 08:39:48 +02:00
2009-08-20 12:38:17 +03:00
#include <linux/mmc/host.h>
2009-11-28 08:39:48 +02:00
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
2009-08-20 12:38:17 +03:00
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/scatterlist.h>
2009-11-28 08:39:48 +02:00
#include <linux/clk.h>
#include <linux/mmc/jz4740_mmc.h>
#include <linux/gpio.h>
#include <asm/mach-jz4740/gpio.h>
#include <asm/cacheflush.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
struct jz4740_mmc_host {
struct mmc_host *mmc;
struct platform_device *pdev;
struct jz4740_mmc_platform_data *pdata;
struct clk *clk;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
int irq;
int card_detect_irq;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
struct resource *mem;
void __iomem *base;
struct mmc_request *req;
struct mmc_command *cmd;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
int max_clock;
uint32_t cmdat;
2009-08-20 12:38:17 +03:00
spinlock_t lock;
2009-11-28 08:39:48 +02:00
struct timer_list clock_timer;
struct timer_list timeout_timer;
unsigned waiting:1;
2009-08-20 12:38:17 +03:00
};
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_cmd_done(struct jz4740_mmc_host *host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_enable_irq(struct jz4740_mmc_host *host, unsigned int irq)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
uint16_t mask;
mask = readw(host->base + JZ_REG_MMC_IMASK);
mask &= ~irq;
writew(mask, host->base + JZ_REG_MMC_IMASK);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_disable_irq(struct jz4740_mmc_host *host, unsigned int irq)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
uint16_t mask;
mask = readw(host->base + JZ_REG_MMC_IMASK);
mask |= irq;
writew(mask, host->base + JZ_REG_MMC_IMASK);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host, bool start_transfer)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (start_transfer)
val |= JZ_MMC_STRPCL_START_OP;
writew(val, host->base + JZ_REG_MMC_STRPCL);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
uint16_t status;
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);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
udelay(10);
while(readw(host->base + JZ_REG_MMC_STATUS) & JZ_MMC_STATUS_IS_RESETTING);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct mmc_request *req;
2009-08-20 12:38:17 +03:00
unsigned long flags;
spin_lock_irqsave(&host->lock, flags);
2009-11-28 08:39:48 +02:00
req = host->req;
host->req = NULL;
2009-08-20 12:38:17 +03:00
spin_unlock_irqrestore(&host->lock, flags);
2009-11-28 08:39:48 +02:00
if (!unlikely(req))
return;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (req->cmd->error != 0)
jz4740_mmc_reset(host);
mmc_request_done(host->mmc, req);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_write_data(struct jz4740_mmc_host *host, struct mmc_data *data) {
struct scatterlist *sg;
uint32_t *sg_pointer;
int i, status;
for (sg = data->sg; sg; sg = sg_next(sg)) {
sg_pointer = page_address(sg_page(sg)) + sg->offset;
i = 0;
while (i < sg->length) {
do {
status = readl(host->base + JZ_REG_MMC_STATUS);
if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK)
goto err;
} while (status & JZ_MMC_STATUS_DATA_FIFO_FULL);
writel(*sg_pointer, host->base + JZ_REG_MMC_TXFIFO);
++sg_pointer;
i += 4;
}
data->bytes_xfered += sg->length;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
do {
status = readl(host->base + JZ_REG_MMC_STATUS);
} while ((status & JZ_MMC_STATUS_DATA_TRAN_DONE) == 0);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
return;
err:
if(status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
host->cmd->error = -ETIMEDOUT;
data->error = -ETIMEDOUT;
} else {
host->cmd->error = -EILSEQ;
data->error = -EILSEQ;
2009-08-20 12:38:17 +03:00
}
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_timeout(unsigned long data)
2009-08-20 12:38:17 +03:00
{
2009-12-01 12:13:40 +02:00
struct jz4740_mmc_host *host = (struct jz4740_mmc_host*)data;
2009-11-28 08:39:48 +02:00
unsigned long flags;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
spin_lock_irqsave(&host->lock, flags);
if (!host->waiting) {
spin_unlock_irqrestore(&host->lock, flags);
return;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
host->waiting = 0;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
spin_unlock_irqrestore(&host->lock, flags);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->cmd->error = -ETIMEDOUT;
jz4740_mmc_request_done(host);
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_read_data(struct jz4740_mmc_host *host, struct mmc_data *data) {
struct scatterlist *sg;
void *sg_pointer;
uint32_t d;
int status = 0;
size_t i = 0;
for (sg = data->sg; sg; sg = sg_next(sg)) {
sg_pointer = sg_virt(sg);
i = sg->length;
while (i >= 4) {
do {
status = readl(host->base + JZ_REG_MMC_STATUS);
if (status & JZ_MMC_STATUS_READ_ERROR_MASK)
goto err;
} while (status & JZ_MMC_STATUS_DATA_FIFO_EMPTY);
d = readl(host->base + JZ_REG_MMC_RXFIFO);
memcpy(sg_pointer, &d, 4);
sg_pointer += 4;
i -= 4;
}
if (i > 0) {
d = readl(host->base + JZ_REG_MMC_RXFIFO);
memcpy(sg_pointer, &d, i);
}
data->bytes_xfered += sg->length;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
flush_dcache_page(sg_page(sg));
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
/* For whatever reason there is sometime one word more in the fifo then
* requested */
while ((status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) == 0) {
d = readl(host->base + JZ_REG_MMC_RXFIFO);
status = readl(host->base + JZ_REG_MMC_STATUS);
}
return;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
err:
if(status & JZ_MMC_STATUS_TIMEOUT_READ) {
host->cmd->error = -ETIMEDOUT;
data->error = -ETIMEDOUT;
} else {
host->cmd->error = -EILSEQ;
data->error = -EILSEQ;
2009-08-20 12:38:17 +03:00
}
}
2009-11-28 08:39:48 +02:00
static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = (struct jz4740_mmc_host*)devid;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (host->cmd->error)
jz4740_mmc_request_done(host);
else
jz4740_mmc_cmd_done(host);
return IRQ_HANDLED;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static irqreturn_t jz_mmc_irq(int irq, void *devid)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = devid;
uint16_t irq_reg, status;
unsigned long flags;
irqreturn_t ret = IRQ_HANDLED;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (!host->req || !host->cmd) {
return IRQ_HANDLED;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
spin_lock_irqsave(&host->lock, flags);
if (!host->waiting) {
spin_unlock_irqrestore(&host->lock, flags);
return IRQ_HANDLED;
}
host->waiting = 0;
spin_unlock_irqrestore(&host->lock, flags);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
del_timer(&host->timeout_timer);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
irq_reg = readw(host->base + JZ_REG_MMC_IREG);
status = readl(host->base + JZ_REG_MMC_STATUS);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
host->cmd->error = -ETIMEDOUT;
} else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
host->cmd->error = -EIO;
} else if(status & (JZ_MMC_STATUS_CRC_READ_ERROR |
JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
host->cmd->data->error = -EIO;
} else if(status & (JZ_MMC_STATUS_CRC_READ_ERROR |
JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
host->cmd->data->error = -ETIMEDOUT;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
if (irq_reg & JZ_MMC_IRQ_END_CMD_RES) {
jz4740_mmc_disable_irq(host, JZ_MMC_IRQ_END_CMD_RES);
writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
ret = IRQ_WAKE_THREAD;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (irq_reg & JZ_MMC_IRQ_SDIO) {
writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
mmc_signal_sdio_irq(host->mmc);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
return ret;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate) {
int div = 0;
int real_rate = host->max_clock;
jz4740_mmc_clock_disable(host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
while ((real_rate >> 1) >= rate && div < 7) {
++div;
real_rate >>= 1;
}
clk_set_rate(host->clk, JZ_MMC_CLK_RATE);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
writew(div, host->base + JZ_REG_MMC_CLKRT);
return real_rate;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_read_response(struct jz4740_mmc_host *host, struct mmc_command *cmd)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
int i;
uint16_t tmp;
if (cmd->flags & MMC_RSP_136) {
tmp = readw(host->base + JZ_REG_MMC_RESP_FIFO);
for (i = 0; i < 4; ++i) {
cmd->resp[i] = tmp << 24;
cmd->resp[i] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) << 8;
tmp = readw(host->base + JZ_REG_MMC_RESP_FIFO);
cmd->resp[i] |= tmp >> 8;
}
} else {
cmd->resp[0] = readw(host->base + JZ_REG_MMC_RESP_FIFO) << 24;
cmd->resp[0] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) << 8;
cmd->resp[0] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) & 0xff;
}
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_send_command(struct jz4740_mmc_host *host, struct mmc_command *cmd)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
uint32_t cmdat = host->cmdat;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->cmdat &= ~JZ_MMC_CMDAT_INIT;
jz4740_mmc_clock_disable(host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->cmd = cmd;
2009-08-20 12:38:17 +03:00
if (cmd->flags & MMC_RSP_BUSY)
2009-11-28 08:39:48 +02:00
cmdat |= JZ_MMC_CMDAT_BUSY;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
switch (mmc_resp_type(cmd)) {
case MMC_RSP_R1B:
case MMC_RSP_R1:
cmdat |= JZ_MMC_CMDAT_RSP_R1;
2009-08-20 12:38:17 +03:00
break;
2009-11-28 08:39:48 +02:00
case MMC_RSP_R2:
cmdat |= JZ_MMC_CMDAT_RSP_R2;
2009-08-20 12:38:17 +03:00
break;
2009-11-28 08:39:48 +02:00
case MMC_RSP_R3:
cmdat |= JZ_MMC_CMDAT_RSP_R3;
2009-08-20 12:38:17 +03:00
break;
default:
break;
}
2009-11-28 08:39:48 +02:00
if (cmd->data) {
cmdat |= JZ_MMC_CMDAT_DATA_EN;
if (cmd->data->flags & MMC_DATA_WRITE)
cmdat |= JZ_MMC_CMDAT_WRITE;
if (cmd->data->flags & MMC_DATA_STREAM)
cmdat |= JZ_MMC_CMDAT_STREAM;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->waiting = 1;
jz4740_mmc_clock_enable(host, 1);
mod_timer(&host->timeout_timer, HZ);
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_cmd_done(struct jz4740_mmc_host *host)
{
uint32_t status;
struct mmc_command *cmd = host->req->cmd;
struct mmc_request *req = host->req;
status = readl(host->base + JZ_REG_MMC_STATUS);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (cmd->flags & MMC_RSP_PRESENT)
jz4740_mmc_read_response(host, cmd);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (cmd->data) {
if (cmd->data->flags & MMC_DATA_READ)
jz4740_mmc_read_data(host, cmd->data);
else
jz4740_mmc_write_data(host, cmd->data);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
if (req->stop) {
jz4740_mmc_send_command(host, req->stop);
do {
status = readl(host->base + JZ_REG_MMC_STATUS);
} while ((status & JZ_MMC_STATUS_PRG_DONE) == 0);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
jz4740_mmc_request_done(host);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = mmc_priv(mmc);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->req = req;
2009-12-01 12:13:40 +02:00
writew(0xffff, host->base + JZ_REG_MMC_IREG);
2009-11-28 08:39:48 +02:00
writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
jz4740_mmc_enable_irq(host, JZ_MMC_IRQ_END_CMD_RES);
jz4740_mmc_send_command(host, req->cmd);
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
struct jz4740_mmc_host *host = mmc_priv(mmc);
if (ios->clock)
jz4740_mmc_set_clock_rate(host, ios->clock);
switch(ios->power_mode) {
case MMC_POWER_UP:
if (gpio_is_valid(host->pdata->gpio_power))
2009-12-01 12:13:40 +02:00
gpio_set_value(host->pdata->gpio_power,
!host->pdata->power_active_low);
2009-11-28 08:39:48 +02:00
host->cmdat |= JZ_MMC_CMDAT_INIT;
clk_enable(host->clk);
2009-08-20 12:38:17 +03:00
break;
2009-11-28 08:39:48 +02:00
case MMC_POWER_ON:
2009-08-20 12:38:17 +03:00
break;
2009-11-28 08:39:48 +02:00
default:
if (gpio_is_valid(host->pdata->gpio_power))
2009-12-01 12:13:40 +02:00
gpio_set_value(host->pdata->gpio_power,
host->pdata->power_active_low);
2009-11-28 08:39:48 +02:00
clk_disable(host->clk);
2009-08-20 12:38:17 +03:00
break;
}
2009-11-28 08:39:48 +02:00
switch(ios->bus_width) {
case MMC_BUS_WIDTH_1:
host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
break;
case MMC_BUS_WIDTH_4:
host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
break;
default:
dev_err(&host->pdev->dev, "Invalid bus width: %d\n", ios->bus_width);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_get_ro(struct mmc_host *mmc)
{
struct jz4740_mmc_host *host = mmc_priv(mmc);
if (!gpio_is_valid(host->pdata->gpio_read_only))
return -ENOSYS;
return gpio_get_value(host->pdata->gpio_read_only) ^
host->pdata->read_only_active_low;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_get_cd(struct mmc_host *mmc)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = mmc_priv(mmc);
if (!gpio_is_valid(host->pdata->gpio_card_detect))
return -ENOSYS;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
return gpio_get_value(host->pdata->gpio_card_detect) ^
host->pdata->card_detect_active_low;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static irqreturn_t jz4740_mmc_card_detect_irq(int irq, void *devid)
{
struct jz4740_mmc_host *host = devid;
mmc_detect_change(host->mmc, HZ / 3);
return IRQ_HANDLED;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = mmc_priv(mmc);
if (enable)
jz4740_mmc_enable_irq(host, JZ_MMC_IRQ_SDIO);
else
jz4740_mmc_disable_irq(host, JZ_MMC_IRQ_SDIO);
}
static const struct mmc_host_ops jz4740_mmc_ops = {
.request = jz4740_mmc_request,
.set_ios = jz4740_mmc_set_ios,
.get_ro = jz4740_mmc_get_ro,
.get_cd = jz4740_mmc_get_cd,
.enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
};
static const struct jz_gpio_bulk_request jz4740_mmc_pins[] = {
JZ_GPIO_BULK_PIN(MSC_CMD),
JZ_GPIO_BULK_PIN(MSC_CLK),
JZ_GPIO_BULK_PIN(MSC_DATA0),
JZ_GPIO_BULK_PIN(MSC_DATA1),
JZ_GPIO_BULK_PIN(MSC_DATA2),
JZ_GPIO_BULK_PIN(MSC_DATA3),
};
static int __devinit jz4740_mmc_request_gpios(struct platform_device *pdev)
{
int ret;
struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
if (!pdata)
return 0;
if (gpio_is_valid(pdata->gpio_card_detect)) {
ret = gpio_request(pdata->gpio_card_detect, "MMC detect change");
if (ret) {
dev_err(&pdev->dev, "Failed to request detect change gpio\n");
goto err;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
gpio_direction_input(pdata->gpio_card_detect);
}
if (gpio_is_valid(pdata->gpio_read_only)) {
ret = gpio_request(pdata->gpio_read_only, "MMC read only");
if (ret) {
dev_err(&pdev->dev, "Failed to request read only gpio: %d\n", ret);
goto err_free_gpio_card_detect;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
gpio_direction_input(pdata->gpio_read_only);
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (gpio_is_valid(pdata->gpio_power)) {
ret = gpio_request(pdata->gpio_power, "MMC power");
if (ret) {
dev_err(&pdev->dev, "Failed to request power gpio: %d\n", ret);
goto err_free_gpio_read_only;
}
2009-12-01 12:13:40 +02:00
gpio_direction_output(pdata->gpio_power, pdata->power_active_low);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
return 0;
err_free_gpio_read_only:
if (gpio_is_valid(pdata->gpio_read_only))
gpio_free(pdata->gpio_read_only);
err_free_gpio_card_detect:
if (gpio_is_valid(pdata->gpio_card_detect))
gpio_free(pdata->gpio_card_detect);
err:
return ret;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static void jz4740_mmc_free_gpios(struct platform_device *pdev)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (!pdata)
return;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (gpio_is_valid(pdata->gpio_power))
gpio_free(pdata->gpio_power);
if (gpio_is_valid(pdata->gpio_read_only))
gpio_free(pdata->gpio_read_only);
if (gpio_is_valid(pdata->gpio_card_detect))
gpio_free(pdata->gpio_card_detect);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static int __devinit jz4740_mmc_probe(struct platform_device* pdev)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
int ret;
struct mmc_host *mmc;
struct jz4740_mmc_host *host;
struct jz4740_mmc_platform_data *pdata;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
pdata = pdev->dev.platform_data;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
if (!mmc) {
dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
return -ENOMEM;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
host = mmc_priv(mmc);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->irq = platform_get_irq(pdev, 0);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (host->irq < 0) {
ret = host->irq;
dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
goto err_free_host;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
host->clk = clk_get(&pdev->dev, "mmc");
if (!host->clk) {
ret = -ENOENT;
dev_err(&pdev->dev, "Failed to get mmc clock\n");
goto err_free_host;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (!host->mem) {
ret = -ENOENT;
dev_err(&pdev->dev, "Failed to get base platform memory\n");
goto err_clk_put;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->mem = request_mem_region(host->mem->start, resource_size(host->mem),
pdev->name);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (!host->mem) {
ret = -EBUSY;
dev_err(&pdev->dev, "Failed to request base memory region\n");
goto err_clk_put;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->base = ioremap_nocache(host->mem->start, resource_size(host->mem));
if (!host->base) {
ret = -EBUSY;
dev_err(&pdev->dev, "Failed to ioremap base memory\n");
goto err_release_mem_region;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
if (pdata && pdata->data_1bit)
ret = jz_gpio_bulk_request(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
else
ret = jz_gpio_bulk_request(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (ret) {
dev_err(&pdev->dev, "Failed to request function pins: %d\n", ret);
goto err_iounmap;
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
ret = jz4740_mmc_request_gpios(pdev);
if (ret)
goto err_gpio_bulk_free;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc->ops = &jz4740_mmc_ops;
mmc->f_min = JZ_MMC_CLK_RATE / 128;
mmc->f_max = JZ_MMC_CLK_RATE;
mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
mmc->caps = (pdata && pdata->data_1bit) ? 0 : MMC_CAP_4_BIT_DATA;
2009-12-01 12:13:40 +02:00
mmc->caps |= MMC_CAP_SDIO_IRQ;
2009-11-28 08:39:48 +02:00
mmc->max_seg_size = 4096;
2009-12-01 12:13:40 +02:00
mmc->max_phys_segs = 128;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc->max_blk_size = (1 << 10) - 1;
mmc->max_blk_count = (1 << 15) - 1;
mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->mmc = mmc;
host->pdev = pdev;
host->pdata = pdata;
host->max_clock = JZ_MMC_CLK_RATE;
2009-12-01 12:13:40 +02:00
spin_lock_init(&host->lock);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
host->card_detect_irq = gpio_to_irq(pdata->gpio_card_detect);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (host->card_detect_irq < 0) {
dev_warn(&pdev->dev, "Failed to get irq for card detect gpio\n");
} else {
ret = request_irq(host->card_detect_irq,
jz4740_mmc_card_detect_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "MMC/SD detect changed", host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (ret) {
dev_err(&pdev->dev, "Failed to request card detect irq");
goto err_free_gpios;
}
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0, "MMC/SD", host);
if (ret) {
dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
goto err_free_card_detect_irq;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
jz4740_mmc_reset(host);
jz4740_mmc_clock_disable(host);
setup_timer(&host->timeout_timer, jz4740_mmc_timeout, (unsigned long)host);
2009-11-29 05:17:52 +02:00
platform_set_drvdata(pdev, host);
2009-11-28 08:39:48 +02:00
ret = mmc_add_host(mmc);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
if (ret) {
dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
goto err_free_irq;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
printk("JZ SD/MMC card driver registered\n");
2009-08-20 12:38:17 +03:00
return 0;
2009-11-28 08:39:48 +02:00
err_free_irq:
free_irq(host->irq, host);
err_free_card_detect_irq:
if (host->card_detect_irq >= 0)
free_irq(host->card_detect_irq, host);
err_free_gpios:
jz4740_mmc_free_gpios(pdev);
err_gpio_bulk_free:
if (pdata && pdata->data_1bit)
jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
else
jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
err_iounmap:
iounmap(host->base);
err_release_mem_region:
release_mem_region(host->mem->start, resource_size(host->mem));
err_clk_put:
clk_put(host->clk);
err_free_host:
platform_set_drvdata(pdev, NULL);
mmc_free_host(mmc);
return ret;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_remove(struct platform_device *pdev)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
2009-11-29 05:17:52 +02:00
struct jz4740_mmc_platform_data *pdata = host->pdata;
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
del_timer_sync(&host->timeout_timer);
jz4740_mmc_disable_irq(host, 0xff);
jz4740_mmc_reset(host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc_remove_host(host->mmc);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
free_irq(host->irq, host);
if (host->card_detect_irq >= 0)
free_irq(host->card_detect_irq, host);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
jz4740_mmc_free_gpios(pdev);
if (pdata && pdata->data_1bit)
jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
else
jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
iounmap(host->base);
release_mem_region(host->mem->start, resource_size(host->mem));
clk_put(host->clk);
platform_set_drvdata(pdev, NULL);
mmc_free_host(host->mmc);
2009-08-20 12:38:17 +03:00
return 0;
}
#ifdef CONFIG_PM
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_suspend(struct device *dev)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = dev_get_drvdata(dev);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
/* TODO: Set gpio pins to high Z */
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc_suspend_host(host->mmc, PMSG_SUSPEND);
2009-11-29 05:17:52 +02:00
return 0;
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
static int jz4740_mmc_resume(struct device *dev)
2009-08-20 12:38:17 +03:00
{
2009-11-28 08:39:48 +02:00
struct jz4740_mmc_host *host = dev_get_drvdata(dev);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
mmc_resume_host(host->mmc);
2009-08-20 12:38:17 +03:00
2009-11-29 05:17:52 +02:00
return 0;
2009-11-28 08:39:48 +02:00
}
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
struct dev_pm_ops jz4740_mmc_pm_ops = {
.suspend = jz4740_mmc_suspend,
.resume = jz4740_mmc_resume,
.poweroff = jz4740_mmc_suspend,
.restore = jz4740_mmc_resume,
};
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
#define jz4740_mmc_PM_OPS (&jz4740_mmc_pm_ops)
2009-08-20 12:38:17 +03:00
#else
2009-11-28 08:39:48 +02:00
#define jz4740_mmc_PM_OPS NULL
2009-08-20 12:38:17 +03:00
#endif
2009-11-28 08:39:48 +02:00
static struct platform_driver jz4740_mmc_driver = {
.probe = jz4740_mmc_probe,
.remove = jz4740_mmc_remove,
2009-08-20 12:38:17 +03:00
.driver = {
2009-11-28 08:39:48 +02:00
.name = "jz4740-mmc",
.owner = THIS_MODULE,
.pm = jz4740_mmc_PM_OPS,
},
2009-08-20 12:38:17 +03:00
};
2009-11-28 08:39:48 +02:00
static int __init jz4740_mmc_init(void) {
return platform_driver_register(&jz4740_mmc_driver);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
module_init(jz4740_mmc_init);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
static void __exit jz4740_mmc_exit(void) {
platform_driver_unregister(&jz4740_mmc_driver);
2009-08-20 12:38:17 +03:00
}
2009-11-28 08:39:48 +02:00
module_exit(jz4740_mmc_exit);
2009-08-20 12:38:17 +03:00
2009-11-28 08:39:48 +02:00
MODULE_DESCRIPTION("JZ4720/JZ4740 SD/MMC controller driver");
2009-12-02 03:14:45 +02:00
MODULE_LICENSE("GPL");
2009-11-28 08:39:48 +02:00
MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");