1 From 63f8a44fa0a452e0f93ca9b88ccdc5ade02f80f3 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:48:14 +0200
4 Subject: [PATCH] Add jz4740 mmc driver
7 drivers/mmc/host/Kconfig | 15 +
8 drivers/mmc/host/Makefile | 1 +
9 drivers/mmc/host/jz_mmc.c | 1005 ++++++++++++++++++++++++++++++++++++++++
10 include/linux/mmc/jz4740_mmc.h | 15 +
11 4 files changed, 1036 insertions(+), 0 deletions(-)
12 create mode 100644 drivers/mmc/host/jz_mmc.c
13 create mode 100644 include/linux/mmc/jz4740_mmc.h
15 diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
16 index 2e13b94..94e9240 100644
17 --- a/drivers/mmc/host/Kconfig
18 +++ b/drivers/mmc/host/Kconfig
19 @@ -81,6 +81,21 @@ config MMC_RICOH_MMC
24 + tristate "JZ SD/Multimedia Card Interface support"
25 + depends on SOC_JZ4720 || SOC_JZ4740
27 + This selects the Ingenic JZ4720/JZ4740 SD/Multimedia card Interface.
28 + If you have abIngenic platform with a Multimedia Card slot,
33 + To compile this driver as a module, choose M here:
34 + the module will be called ricoh_mmc.
39 tristate "SDHCI support on OpenFirmware platforms"
40 depends on MMC_SDHCI && PPC_OF
41 diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
42 index f480397..7e83c54 100644
43 --- a/drivers/mmc/host/Makefile
44 +++ b/drivers/mmc/host/Makefile
45 @@ -6,6 +6,7 @@ ifeq ($(CONFIG_MMC_DEBUG),y)
46 EXTRA_CFLAGS += -DDEBUG
49 +obj-$(CONFIG_MMC_JZ) += jz_mmc.o
50 obj-$(CONFIG_MMC_ARMMMCI) += mmci.o
51 obj-$(CONFIG_MMC_PXA) += pxamci.o
52 obj-$(CONFIG_MMC_IMX) += imxmmc.o
53 diff --git a/drivers/mmc/host/jz_mmc.c b/drivers/mmc/host/jz_mmc.c
55 index 0000000..ac7668a
57 +++ b/drivers/mmc/host/jz_mmc.c
60 + * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
61 + * JZ7420/JZ4740 GPIO SD/MMC controller driver
63 + * This program is free software; you can redistribute it and/or modify it
64 + * under the terms of the GNU General Public License as published by the
65 + * Free Software Foundation; either version 2 of the License, or (at your
66 + * option) any later version.
68 + * You should have received a copy of the GNU General Public License along
69 + * with this program; if not, write to the Free Software Foundation, Inc.,
70 + * 675 Mass Ave, Cambridge, MA 02139, USA.
74 +#include <linux/mmc/host.h>
75 +#include <linux/io.h>
76 +#include <linux/irq.h>
77 +#include <linux/interrupt.h>
78 +#include <linux/module.h>
79 +#include <linux/platform_device.h>
80 +#include <linux/delay.h>
81 +#include <linux/scatterlist.h>
82 +#include <linux/clk.h>
83 +#include <linux/mmc/jz4740_mmc.h>
85 +#include <linux/gpio.h>
86 +#include <asm/mach-jz4740/gpio.h>
87 +#include <asm/cacheflush.h>
88 +#include <linux/dma-mapping.h>
90 +#define JZ_REG_MMC_STRPCL 0x00
91 +#define JZ_REG_MMC_STATUS 0x04
92 +#define JZ_REG_MMC_CLKRT 0x08
93 +#define JZ_REG_MMC_CMDAT 0x0C
94 +#define JZ_REG_MMC_RESTO 0x10
95 +#define JZ_REG_MMC_RDTO 0x14
96 +#define JZ_REG_MMC_BLKLEN 0x18
97 +#define JZ_REG_MMC_NOB 0x1C
98 +#define JZ_REG_MMC_SNOB 0x20
99 +#define JZ_REG_MMC_IMASK 0x24
100 +#define JZ_REG_MMC_IREG 0x28
101 +#define JZ_REG_MMC_CMD 0x2C
102 +#define JZ_REG_MMC_ARG 0x30
103 +#define JZ_REG_MMC_RESP_FIFO 0x34
104 +#define JZ_REG_MMC_RXFIFO 0x38
105 +#define JZ_REG_MMC_TXFIFO 0x3C
107 +#define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
108 +#define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
109 +#define JZ_MMC_STRPCL_START_READWAIT BIT(5)
110 +#define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
111 +#define JZ_MMC_STRPCL_RESET BIT(3)
112 +#define JZ_MMC_STRPCL_START_OP BIT(2)
113 +#define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
114 +#define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
115 +#define JZ_MMC_STRPCL_CLOCK_START BIT(1)
118 +#define JZ_MMC_STATUS_IS_RESETTING BIT(15)
119 +#define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
120 +#define JZ_MMC_STATUS_PRG_DONE BIT(13)
121 +#define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
122 +#define JZ_MMC_STATUS_END_CMD_RES BIT(11)
123 +#define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
124 +#define JZ_MMC_STATUS_IS_READWAIT BIT(9)
125 +#define JZ_MMC_STATUS_CLK_EN BIT(8)
126 +#define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
127 +#define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
128 +#define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
129 +#define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
130 +#define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
131 +#define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
132 +#define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
133 +#define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
135 +#define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
136 +#define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
139 +#define JZ_MMC_CMDAT_IO_ABORT BIT(11)
140 +#define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
141 +#define JZ_MMC_CMDAT_DMA_EN BIT(8)
142 +#define JZ_MMC_CMDAT_INIT BIT(7)
143 +#define JZ_MMC_CMDAT_BUSY BIT(6)
144 +#define JZ_MMC_CMDAT_STREAM BIT(5)
145 +#define JZ_MMC_CMDAT_WRITE BIT(4)
146 +#define JZ_MMC_CMDAT_DATA_EN BIT(3)
147 +#define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
148 +#define JZ_MMC_CMDAT_RSP_R1 1
149 +#define JZ_MMC_CMDAT_RSP_R2 2
150 +#define JZ_MMC_CMDAT_RSP_R3 3
152 +#define JZ_MMC_IRQ_SDIO BIT(7)
153 +#define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
154 +#define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
155 +#define JZ_MMC_IRQ_END_CMD_RES BIT(2)
156 +#define JZ_MMC_IRQ_PRG_DONE BIT(1)
157 +#define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
160 +#define JZ_MMC_CLK_RATE 24000000
162 +#define JZ4740_MMC_MAX_TIMEOUT 10000000
164 +struct jz4740_mmc_host {
165 + struct mmc_host *mmc;
166 + struct platform_device *pdev;
167 + struct jz4740_mmc_platform_data *pdata;
171 + int card_detect_irq;
173 + struct resource *mem;
174 + void __iomem *base;
175 + struct mmc_request *req;
176 + struct mmc_command *cmd;
184 + struct timer_list clock_timer;
185 + struct timer_list timeout_timer;
186 + unsigned waiting:1;
189 +static void jz4740_mmc_cmd_done(struct jz4740_mmc_host *host);
191 +static void jz4740_mmc_enable_irq(struct jz4740_mmc_host *host, unsigned int irq)
193 + unsigned long flags;
194 + spin_lock_irqsave(&host->lock, flags);
196 + host->irq_mask &= ~irq;
197 + writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
199 + spin_unlock_irqrestore(&host->lock, flags);
202 +static void jz4740_mmc_disable_irq(struct jz4740_mmc_host *host, unsigned int irq)
204 + unsigned long flags;
205 + spin_lock_irqsave(&host->lock, flags);
207 + host->irq_mask |= irq;
208 + writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
210 + spin_unlock_irqrestore(&host->lock, flags);
213 +static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host, bool start_transfer)
215 + uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
217 + if (start_transfer)
218 + val |= JZ_MMC_STRPCL_START_OP;
220 + writew(val, host->base + JZ_REG_MMC_STRPCL);
223 +static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
227 + writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
229 + status = readl(host->base + JZ_REG_MMC_STATUS);
230 + } while (status & JZ_MMC_STATUS_CLK_EN);
234 +static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
238 + writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
241 + status = readl(host->base + JZ_REG_MMC_STATUS);
242 + } while (status & JZ_MMC_STATUS_IS_RESETTING);
245 +static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
247 + struct mmc_request *req;
248 + unsigned long flags;
250 + spin_lock_irqsave(&host->lock, flags);
254 + spin_unlock_irqrestore(&host->lock, flags);
256 + if (!unlikely(req))
259 + mmc_request_done(host->mmc, req);
262 +static inline unsigned int jz4740_mmc_wait_irq(struct jz4740_mmc_host *host,
265 + unsigned int timeout = JZ4740_MMC_MAX_TIMEOUT;
269 + status = readw(host->base + JZ_REG_MMC_IREG);
270 + } while (!(status & irq) && --timeout);
275 +static void jz4740_mmc_write_data(struct jz4740_mmc_host *host, struct mmc_data *data)
277 + struct scatterlist *sg;
278 + uint32_t *sg_pointer;
280 + unsigned int timeout;
283 + for (sg = data->sg; sg; sg = sg_next(sg)) {
284 + sg_pointer = sg_virt(sg);
285 + i = sg->length / 4;
289 + timeout = jz4740_mmc_wait_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
290 + if (unlikely(timeout == 0))
293 + writel(sg_pointer[0], host->base + JZ_REG_MMC_TXFIFO);
294 + writel(sg_pointer[1], host->base + JZ_REG_MMC_TXFIFO);
295 + writel(sg_pointer[2], host->base + JZ_REG_MMC_TXFIFO);
296 + writel(sg_pointer[3], host->base + JZ_REG_MMC_TXFIFO);
297 + writel(sg_pointer[4], host->base + JZ_REG_MMC_TXFIFO);
298 + writel(sg_pointer[5], host->base + JZ_REG_MMC_TXFIFO);
299 + writel(sg_pointer[6], host->base + JZ_REG_MMC_TXFIFO);
300 + writel(sg_pointer[7], host->base + JZ_REG_MMC_TXFIFO);
305 + timeout = jz4740_mmc_wait_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
306 + if (unlikely(timeout == 0))
310 + writel(*sg_pointer, host->base + JZ_REG_MMC_TXFIFO);
315 + data->bytes_xfered += sg->length;
318 + status = readl(host->base + JZ_REG_MMC_STATUS);
319 + if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK)
322 + timeout = JZ4740_MMC_MAX_TIMEOUT;
324 + status = readl(host->base + JZ_REG_MMC_STATUS);
325 + } while ((status & JZ_MMC_STATUS_DATA_TRAN_DONE) == 0 && --timeout);
327 + if (unlikely(timeout == 0))
329 + writew(JZ_MMC_IRQ_DATA_TRAN_DONE, host->base + JZ_REG_MMC_IREG);
333 + host->req->cmd->error = -ETIMEDOUT;
334 + data->error = -ETIMEDOUT;
337 + if(status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
338 + host->req->cmd->error = -ETIMEDOUT;
339 + data->error = -ETIMEDOUT;
341 + host->req->cmd->error = -EILSEQ;
342 + data->error = -EILSEQ;
346 +static void jz4740_mmc_timeout(unsigned long data)
348 + struct jz4740_mmc_host *host = (struct jz4740_mmc_host*)data;
349 + unsigned long flags;
351 + spin_lock_irqsave(&host->lock, flags);
352 + if (!host->waiting) {
353 + spin_unlock_irqrestore(&host->lock, flags);
359 + spin_unlock_irqrestore(&host->lock, flags);
361 + host->req->cmd->error = -ETIMEDOUT;
362 + jz4740_mmc_request_done(host);
365 +static void jz4740_mmc_read_data(struct jz4740_mmc_host *host,
366 + struct mmc_data *data)
368 + struct scatterlist *sg;
369 + uint32_t *sg_pointer;
371 + uint16_t status = 0;
373 + unsigned int timeout;
375 + for (sg = data->sg; sg; sg = sg_next(sg)) {
376 + sg_pointer = sg_virt(sg);
381 + timeout = jz4740_mmc_wait_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
382 + if (unlikely(timeout == 0))
385 + sg_pointer[0] = readl(host->base + JZ_REG_MMC_RXFIFO);
386 + sg_pointer[1] = readl(host->base + JZ_REG_MMC_RXFIFO);
387 + sg_pointer[2] = readl(host->base + JZ_REG_MMC_RXFIFO);
388 + sg_pointer[3] = readl(host->base + JZ_REG_MMC_RXFIFO);
389 + sg_pointer[4] = readl(host->base + JZ_REG_MMC_RXFIFO);
390 + sg_pointer[5] = readl(host->base + JZ_REG_MMC_RXFIFO);
391 + sg_pointer[6] = readl(host->base + JZ_REG_MMC_RXFIFO);
392 + sg_pointer[7] = readl(host->base + JZ_REG_MMC_RXFIFO);
399 + timeout = jz4740_mmc_wait_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
400 + if (unlikely(timeout == 0))
403 + *sg_pointer = readl(host->base + JZ_REG_MMC_RXFIFO);
408 + d = readl(host->base + JZ_REG_MMC_RXFIFO);
409 + memcpy(sg_pointer, &d, i);
411 + data->bytes_xfered += sg->length;
413 + flush_dcache_page(sg_page(sg));
416 + status = readl(host->base + JZ_REG_MMC_STATUS);
417 + if (status & JZ_MMC_STATUS_READ_ERROR_MASK)
420 + /* For whatever reason there is sometime one word more in the fifo then
422 + while ((status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) == 0 && --timeout) {
423 + d = readl(host->base + JZ_REG_MMC_RXFIFO);
424 + status = readl(host->base + JZ_REG_MMC_STATUS);
428 + host->req->cmd->error = -ETIMEDOUT;
429 + data->error = -ETIMEDOUT;
432 + if (status & JZ_MMC_STATUS_TIMEOUT_READ) {
433 + host->req->cmd->error = -ETIMEDOUT;
434 + data->error = -ETIMEDOUT;
436 + host->req->cmd->error = -EILSEQ;
437 + data->error = -EILSEQ;
441 +static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
443 + struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
445 + if (host->cmd->error)
446 + jz4740_mmc_request_done(host);
448 + jz4740_mmc_cmd_done(host);
450 + return IRQ_HANDLED;
453 +static irqreturn_t jz_mmc_irq(int irq, void *devid)
455 + struct jz4740_mmc_host *host = devid;
456 + uint16_t irq_reg, status, tmp;
457 + unsigned long flags;
458 + irqreturn_t ret = IRQ_HANDLED;
460 + irq_reg = readw(host->base + JZ_REG_MMC_IREG);
463 + spin_lock_irqsave(&host->lock, flags);
464 + irq_reg &= ~host->irq_mask;
465 + spin_unlock_irqrestore(&host->lock, flags);
467 + tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
468 + JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
470 + if (tmp != irq_reg) {
471 + dev_warn(&host->pdev->dev, "Sparse irq: %x\n", tmp & ~irq_reg);
472 + writew(tmp & ~irq_reg, host->base + JZ_REG_MMC_IREG);
476 + if (irq_reg & JZ_MMC_IRQ_SDIO) {
477 + writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
478 + mmc_signal_sdio_irq(host->mmc);
481 + if (!host->req || !host->cmd) {
486 + spin_lock_irqsave(&host->lock, flags);
487 + if (!host->waiting) {
488 + spin_unlock_irqrestore(&host->lock, flags);
493 + spin_unlock_irqrestore(&host->lock, flags);
495 + del_timer(&host->timeout_timer);
497 + status = readl(host->base + JZ_REG_MMC_STATUS);
499 + if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
500 + host->cmd->error = -ETIMEDOUT;
501 + } else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
502 + host->cmd->error = -EIO;
503 + } else if(status & (JZ_MMC_STATUS_CRC_READ_ERROR |
504 + JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
505 + host->cmd->data->error = -EIO;
506 + } else if(status & (JZ_MMC_STATUS_CRC_READ_ERROR |
507 + JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
508 + host->cmd->data->error = -EIO;
511 + if (irq_reg & JZ_MMC_IRQ_END_CMD_RES) {
512 + jz4740_mmc_disable_irq(host, JZ_MMC_IRQ_END_CMD_RES);
513 + writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
514 + ret = IRQ_WAKE_THREAD;
520 + writew(0xff, host->base + JZ_REG_MMC_IREG);
521 + return IRQ_HANDLED;
524 +static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate) {
528 + jz4740_mmc_clock_disable(host);
529 + clk_set_rate(host->clk, JZ_MMC_CLK_RATE);
531 + real_rate = clk_get_rate(host->clk);
533 + while (real_rate > rate && div < 7) {
538 + writew(div, host->base + JZ_REG_MMC_CLKRT);
543 +static void jz4740_mmc_read_response(struct jz4740_mmc_host *host, struct mmc_command *cmd)
547 + if (cmd->flags & MMC_RSP_136) {
548 + tmp = readw(host->base + JZ_REG_MMC_RESP_FIFO);
549 + for (i = 0; i < 4; ++i) {
550 + cmd->resp[i] = tmp << 24;
551 + cmd->resp[i] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) << 8;
552 + tmp = readw(host->base + JZ_REG_MMC_RESP_FIFO);
553 + cmd->resp[i] |= tmp >> 8;
556 + cmd->resp[0] = readw(host->base + JZ_REG_MMC_RESP_FIFO) << 24;
557 + cmd->resp[0] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) << 8;
558 + cmd->resp[0] |= readw(host->base + JZ_REG_MMC_RESP_FIFO) & 0xff;
562 +static void jz4740_mmc_send_command(struct jz4740_mmc_host *host, struct mmc_command *cmd)
564 + uint32_t cmdat = host->cmdat;
566 + host->cmdat &= ~JZ_MMC_CMDAT_INIT;
567 + jz4740_mmc_clock_disable(host);
571 + if (cmd->flags & MMC_RSP_BUSY)
572 + cmdat |= JZ_MMC_CMDAT_BUSY;
574 + switch (mmc_resp_type(cmd)) {
577 + cmdat |= JZ_MMC_CMDAT_RSP_R1;
580 + cmdat |= JZ_MMC_CMDAT_RSP_R2;
583 + cmdat |= JZ_MMC_CMDAT_RSP_R3;
590 + cmdat |= JZ_MMC_CMDAT_DATA_EN;
591 + if (cmd->data->flags & MMC_DATA_WRITE)
592 + cmdat |= JZ_MMC_CMDAT_WRITE;
593 + if (cmd->data->flags & MMC_DATA_STREAM)
594 + cmdat |= JZ_MMC_CMDAT_STREAM;
596 + writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
597 + writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
600 + writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
601 + writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
602 + writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
605 + jz4740_mmc_clock_enable(host, 1);
606 + mod_timer(&host->timeout_timer, jiffies + 5*HZ);
609 +static void jz4740_mmc_cmd_done(struct jz4740_mmc_host *host)
612 + struct mmc_command *cmd = host->req->cmd;
613 + struct mmc_request *req = host->req;
614 + unsigned int timeout = JZ4740_MMC_MAX_TIMEOUT;
616 + if (cmd->flags & MMC_RSP_PRESENT)
617 + jz4740_mmc_read_response(host, cmd);
620 + if (cmd->data->flags & MMC_DATA_READ)
621 + jz4740_mmc_read_data(host, cmd->data);
623 + jz4740_mmc_write_data(host, cmd->data);
627 + jz4740_mmc_send_command(host, req->stop);
629 + status = readw(host->base + JZ_REG_MMC_IREG);
630 + } while ((status & JZ_MMC_IRQ_PRG_DONE) == 0 && --timeout);
631 + writew(JZ_MMC_IRQ_PRG_DONE, host->base + JZ_REG_MMC_IREG);
634 + if (unlikely(timeout == 0))
635 + req->stop->error = -ETIMEDOUT;
637 + jz4740_mmc_request_done(host);
640 +static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
642 + struct jz4740_mmc_host *host = mmc_priv(mmc);
646 + writew(0xffff, host->base + JZ_REG_MMC_IREG);
648 + writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
649 + jz4740_mmc_enable_irq(host, JZ_MMC_IRQ_END_CMD_RES);
650 + jz4740_mmc_send_command(host, req->cmd);
654 +static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
656 + struct jz4740_mmc_host *host = mmc_priv(mmc);
658 + jz4740_mmc_set_clock_rate(host, ios->clock);
660 + switch (ios->power_mode) {
662 + jz4740_mmc_reset(host);
663 + if (gpio_is_valid(host->pdata->gpio_power))
664 + gpio_set_value(host->pdata->gpio_power,
665 + !host->pdata->power_active_low);
666 + host->cmdat |= JZ_MMC_CMDAT_INIT;
667 + clk_enable(host->clk);
672 + if (gpio_is_valid(host->pdata->gpio_power))
673 + gpio_set_value(host->pdata->gpio_power,
674 + host->pdata->power_active_low);
675 + clk_disable(host->clk);
679 + switch (ios->bus_width) {
680 + case MMC_BUS_WIDTH_1:
681 + host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
683 + case MMC_BUS_WIDTH_4:
684 + host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
687 + dev_err(&host->pdev->dev, "Invalid bus width: %d\n", ios->bus_width);
691 +static int jz4740_mmc_get_ro(struct mmc_host *mmc)
693 + struct jz4740_mmc_host *host = mmc_priv(mmc);
694 + if (!gpio_is_valid(host->pdata->gpio_read_only))
697 + return gpio_get_value(host->pdata->gpio_read_only) ^
698 + host->pdata->read_only_active_low;
701 +static int jz4740_mmc_get_cd(struct mmc_host *mmc)
703 + struct jz4740_mmc_host *host = mmc_priv(mmc);
704 + if (!gpio_is_valid(host->pdata->gpio_card_detect))
707 + return gpio_get_value(host->pdata->gpio_card_detect) ^
708 + host->pdata->card_detect_active_low;
711 +static irqreturn_t jz4740_mmc_card_detect_irq(int irq, void *devid)
713 + struct jz4740_mmc_host *host = devid;
715 + mmc_detect_change(host->mmc, HZ / 3);
717 + return IRQ_HANDLED;
720 +static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
722 + struct jz4740_mmc_host *host = mmc_priv(mmc);
724 + jz4740_mmc_enable_irq(host, JZ_MMC_IRQ_SDIO);
726 + jz4740_mmc_disable_irq(host, JZ_MMC_IRQ_SDIO);
729 +static const struct mmc_host_ops jz4740_mmc_ops = {
730 + .request = jz4740_mmc_request,
731 + .set_ios = jz4740_mmc_set_ios,
732 + .get_ro = jz4740_mmc_get_ro,
733 + .get_cd = jz4740_mmc_get_cd,
734 + .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
737 +static const struct jz_gpio_bulk_request jz4740_mmc_pins[] = {
738 + JZ_GPIO_BULK_PIN(MSC_CMD),
739 + JZ_GPIO_BULK_PIN(MSC_CLK),
740 + JZ_GPIO_BULK_PIN(MSC_DATA0),
741 + JZ_GPIO_BULK_PIN(MSC_DATA1),
742 + JZ_GPIO_BULK_PIN(MSC_DATA2),
743 + JZ_GPIO_BULK_PIN(MSC_DATA3),
746 +static int __devinit jz4740_mmc_request_gpios(struct platform_device *pdev)
749 + struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
754 + if (gpio_is_valid(pdata->gpio_card_detect)) {
755 + ret = gpio_request(pdata->gpio_card_detect, "MMC detect change");
757 + dev_err(&pdev->dev, "Failed to request detect change gpio\n");
760 + gpio_direction_input(pdata->gpio_card_detect);
763 + if (gpio_is_valid(pdata->gpio_read_only)) {
764 + ret = gpio_request(pdata->gpio_read_only, "MMC read only");
766 + dev_err(&pdev->dev, "Failed to request read only gpio: %d\n", ret);
767 + goto err_free_gpio_card_detect;
769 + gpio_direction_input(pdata->gpio_read_only);
772 + if (gpio_is_valid(pdata->gpio_power)) {
773 + ret = gpio_request(pdata->gpio_power, "MMC power");
775 + dev_err(&pdev->dev, "Failed to request power gpio: %d\n", ret);
776 + goto err_free_gpio_read_only;
778 + gpio_direction_output(pdata->gpio_power, pdata->power_active_low);
783 +err_free_gpio_read_only:
784 + if (gpio_is_valid(pdata->gpio_read_only))
785 + gpio_free(pdata->gpio_read_only);
786 +err_free_gpio_card_detect:
787 + if (gpio_is_valid(pdata->gpio_card_detect))
788 + gpio_free(pdata->gpio_card_detect);
793 +static void jz4740_mmc_free_gpios(struct platform_device *pdev)
795 + struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
800 + if (gpio_is_valid(pdata->gpio_power))
801 + gpio_free(pdata->gpio_power);
802 + if (gpio_is_valid(pdata->gpio_read_only))
803 + gpio_free(pdata->gpio_read_only);
804 + if (gpio_is_valid(pdata->gpio_card_detect))
805 + gpio_free(pdata->gpio_card_detect);
808 +static int __devinit jz4740_mmc_probe(struct platform_device* pdev)
811 + struct mmc_host *mmc;
812 + struct jz4740_mmc_host *host;
813 + struct jz4740_mmc_platform_data *pdata;
815 + pdata = pdev->dev.platform_data;
817 + mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
820 + dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
824 + host = mmc_priv(mmc);
826 + host->irq = platform_get_irq(pdev, 0);
828 + if (host->irq < 0) {
830 + dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
831 + goto err_free_host;
834 + host->clk = clk_get(&pdev->dev, "mmc");
837 + dev_err(&pdev->dev, "Failed to get mmc clock\n");
838 + goto err_free_host;
841 + host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
845 + dev_err(&pdev->dev, "Failed to get base platform memory\n");
849 + host->mem = request_mem_region(host->mem->start, resource_size(host->mem),
854 + dev_err(&pdev->dev, "Failed to request base memory region\n");
858 + host->base = ioremap_nocache(host->mem->start, resource_size(host->mem));
862 + dev_err(&pdev->dev, "Failed to ioremap base memory\n");
863 + goto err_release_mem_region;
866 + if (pdata && pdata->data_1bit)
867 + ret = jz_gpio_bulk_request(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
869 + ret = jz_gpio_bulk_request(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
872 + dev_err(&pdev->dev, "Failed to request function pins: %d\n", ret);
876 + ret = jz4740_mmc_request_gpios(pdev);
878 + goto err_gpio_bulk_free;
880 + mmc->ops = &jz4740_mmc_ops;
881 + mmc->f_min = JZ_MMC_CLK_RATE / 128;
882 + mmc->f_max = JZ_MMC_CLK_RATE;
883 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
884 + mmc->caps = (pdata && pdata->data_1bit) ? 0 : MMC_CAP_4_BIT_DATA;
885 + mmc->caps |= MMC_CAP_SDIO_IRQ;
886 + mmc->max_seg_size = 4096;
887 + mmc->max_phys_segs = 128;
889 + mmc->max_blk_size = (1 << 10) - 1;
890 + mmc->max_blk_count = (1 << 15) - 1;
891 + mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
895 + host->pdata = pdata;
896 + host->max_clock = JZ_MMC_CLK_RATE;
897 + spin_lock_init(&host->lock);
898 + host->irq_mask = 0xffff;
900 + host->card_detect_irq = gpio_to_irq(pdata->gpio_card_detect);
902 + if (host->card_detect_irq < 0) {
903 + dev_warn(&pdev->dev, "Failed to get irq for card detect gpio\n");
905 + ret = request_irq(host->card_detect_irq,
906 + jz4740_mmc_card_detect_irq,
907 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
908 + "MMC card detect", host);
911 + dev_err(&pdev->dev, "Failed to request card detect irq");
912 + goto err_free_gpios;
916 + ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker,
917 + IRQF_DISABLED, dev_name(&pdev->dev), host);
919 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
920 + goto err_free_card_detect_irq;
923 + jz4740_mmc_reset(host);
924 + jz4740_mmc_clock_disable(host);
925 + setup_timer(&host->timeout_timer, jz4740_mmc_timeout,
926 + (unsigned long)host);
928 + platform_set_drvdata(pdev, host);
929 + ret = mmc_add_host(mmc);
932 + dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
935 + dev_info(&pdev->dev, "JZ SD/MMC card driver registered\n");
940 + free_irq(host->irq, host);
941 +err_free_card_detect_irq:
942 + if (host->card_detect_irq >= 0)
943 + free_irq(host->card_detect_irq, host);
945 + jz4740_mmc_free_gpios(pdev);
947 + if (pdata && pdata->data_1bit)
948 + jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
950 + jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
952 + iounmap(host->base);
953 +err_release_mem_region:
954 + release_mem_region(host->mem->start, resource_size(host->mem));
956 + clk_put(host->clk);
958 + platform_set_drvdata(pdev, NULL);
959 + mmc_free_host(mmc);
964 +static int __devexit jz4740_mmc_remove(struct platform_device *pdev)
966 + struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
967 + struct jz4740_mmc_platform_data *pdata = host->pdata;
969 + del_timer_sync(&host->timeout_timer);
970 + jz4740_mmc_disable_irq(host, 0xff);
971 + jz4740_mmc_reset(host);
973 + mmc_remove_host(host->mmc);
975 + free_irq(host->irq, host);
976 + if (host->card_detect_irq >= 0)
977 + free_irq(host->card_detect_irq, host);
979 + jz4740_mmc_free_gpios(pdev);
980 + if (pdata && pdata->data_1bit)
981 + jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
983 + jz_gpio_bulk_free(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
985 + iounmap(host->base);
986 + release_mem_region(host->mem->start, resource_size(host->mem));
988 + clk_put(host->clk);
990 + platform_set_drvdata(pdev, NULL);
991 + mmc_free_host(host->mmc);
997 +static int jz4740_mmc_suspend(struct device *dev)
999 + struct jz4740_mmc_host *host = dev_get_drvdata(dev);
1000 + struct jz4740_mmc_platform_data *pdata = host->pdata;
1002 + mmc_suspend_host(host->mmc, PMSG_SUSPEND);
1004 + if (pdata && pdata->data_1bit)
1005 + jz_gpio_bulk_suspend(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
1007 + jz_gpio_bulk_suspend(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
1012 +static int jz4740_mmc_resume(struct device *dev)
1014 + struct jz4740_mmc_host *host = dev_get_drvdata(dev);
1015 + struct jz4740_mmc_platform_data *pdata = host->pdata;
1017 + if (pdata && pdata->data_1bit)
1018 + jz_gpio_bulk_resume(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins) - 3);
1020 + jz_gpio_bulk_resume(jz4740_mmc_pins, ARRAY_SIZE(jz4740_mmc_pins));
1022 + mmc_resume_host(host->mmc);
1027 +const struct dev_pm_ops jz4740_mmc_pm_ops = {
1028 + .suspend = jz4740_mmc_suspend,
1029 + .resume = jz4740_mmc_resume,
1030 + .poweroff = jz4740_mmc_suspend,
1031 + .restore = jz4740_mmc_resume,
1034 +#define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1036 +#define JZ4740_MMC_PM_OPS NULL
1039 +static struct platform_driver jz4740_mmc_driver = {
1040 + .probe = jz4740_mmc_probe,
1041 + .remove = __devexit_p(jz4740_mmc_remove),
1043 + .name = "jz4740-mmc",
1044 + .owner = THIS_MODULE,
1045 + .pm = JZ4740_MMC_PM_OPS,
1049 +static int __init jz4740_mmc_init(void)
1051 + return platform_driver_register(&jz4740_mmc_driver);
1053 +module_init(jz4740_mmc_init);
1055 +static void __exit jz4740_mmc_exit(void)
1057 + platform_driver_unregister(&jz4740_mmc_driver);
1059 +module_exit(jz4740_mmc_exit);
1061 +MODULE_DESCRIPTION("JZ4720/JZ4740 SD/MMC controller driver");
1062 +MODULE_LICENSE("GPL");
1063 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1064 diff --git a/include/linux/mmc/jz4740_mmc.h b/include/linux/mmc/jz4740_mmc.h
1065 new file mode 100644
1066 index 0000000..8543f43
1068 +++ b/include/linux/mmc/jz4740_mmc.h
1070 +#ifndef __LINUX_MMC_JZ4740_MMC
1071 +#define __LINUX_MMC_JZ4740_MMC
1073 +struct jz4740_mmc_platform_data {
1075 + int gpio_card_detect;
1076 + int gpio_read_only;
1077 + unsigned card_detect_active_low:1;
1078 + unsigned read_only_active_low:1;
1079 + unsigned power_active_low:1;
1081 + unsigned data_1bit:1;