1 From 11bc6d97096ab89da31f628c89b19ff37dfdd526 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Thu, 15 Jul 2010 20:06:04 +0000
4 Subject: [PATCH] MMC: Add support for the controller on JZ4740 SoCs.
6 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
7 Acked-by: Matt Fleming <matt@console-pimps.org>
8 Cc: Andrew Morton <akpm@linux-foundation.org>
9 Cc: Matt Fleming <matt@console-pimps.org>
10 Cc: linux-mmc@vger.kernel.org
11 Cc: linux-mips@linux-mips.org
12 Cc: linux-kernel@vger.kernel.org
13 Patchwork: https://patchwork.linux-mips.org/patch/1463/
14 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
16 arch/mips/include/asm/mach-jz4740/jz4740_mmc.h | 15 +
17 drivers/mmc/host/Kconfig | 9 +
18 drivers/mmc/host/Makefile | 1 +
19 drivers/mmc/host/jz4740_mmc.c | 1029 ++++++++++++++++++++++++
20 4 files changed, 1054 insertions(+), 0 deletions(-)
21 create mode 100644 arch/mips/include/asm/mach-jz4740/jz4740_mmc.h
22 create mode 100644 drivers/mmc/host/jz4740_mmc.c
25 +++ b/arch/mips/include/asm/mach-jz4740/jz4740_mmc.h
27 +#ifndef __LINUX_MMC_JZ4740_MMC
28 +#define __LINUX_MMC_JZ4740_MMC
30 +struct jz4740_mmc_platform_data {
32 + int gpio_card_detect;
34 + unsigned card_detect_active_low:1;
35 + unsigned read_only_active_low:1;
36 + unsigned power_active_low:1;
38 + unsigned data_1bit:1;
42 --- a/drivers/mmc/host/Kconfig
43 +++ b/drivers/mmc/host/Kconfig
44 @@ -457,3 +457,12 @@ config MMC_SH_MMCIF
45 This selects the MMC Host Interface controler (MMCIF).
47 This driver supports MMCIF in sh7724/sh7757/sh7372.
50 + tristate "JZ4740 SD/Multimedia Card Interface support"
51 + depends on MACH_JZ4740
53 + This selects support for the SD/MMC controller on Ingenic JZ4740
55 + If you have a board based on such a SoC and with a SD/MMC slot,
57 --- a/drivers/mmc/host/Makefile
58 +++ b/drivers/mmc/host/Makefile
59 @@ -37,6 +37,7 @@ obj-$(CONFIG_MMC_VIA_SDMMC) += via-sdmmc
60 obj-$(CONFIG_GPIOMMC) += gpiommc.o
61 obj-$(CONFIG_SDH_BFIN) += bfin_sdh.o
62 obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o
63 +obj-$(CONFIG_MMC_JZ4740) += jz4740_mmc.o
65 obj-$(CONFIG_MMC_SDHCI_OF) += sdhci-of.o
66 sdhci-of-y := sdhci-of-core.o
68 +++ b/drivers/mmc/host/jz4740_mmc.c
71 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
72 + * JZ4740 SD/MMC controller driver
74 + * This program is free software; you can redistribute it and/or modify it
75 + * under the terms of the GNU General Public License as published by the
76 + * Free Software Foundation; either version 2 of the License, or (at your
77 + * option) any later version.
79 + * You should have received a copy of the GNU General Public License along
80 + * with this program; if not, write to the Free Software Foundation, Inc.,
81 + * 675 Mass Ave, Cambridge, MA 02139, USA.
85 +#include <linux/mmc/host.h>
86 +#include <linux/io.h>
87 +#include <linux/irq.h>
88 +#include <linux/interrupt.h>
89 +#include <linux/module.h>
90 +#include <linux/platform_device.h>
91 +#include <linux/delay.h>
92 +#include <linux/scatterlist.h>
93 +#include <linux/clk.h>
95 +#include <linux/bitops.h>
96 +#include <linux/gpio.h>
97 +#include <asm/mach-jz4740/gpio.h>
98 +#include <asm/cacheflush.h>
99 +#include <linux/dma-mapping.h>
101 +#include <asm/mach-jz4740/jz4740_mmc.h>
103 +#define JZ_REG_MMC_STRPCL 0x00
104 +#define JZ_REG_MMC_STATUS 0x04
105 +#define JZ_REG_MMC_CLKRT 0x08
106 +#define JZ_REG_MMC_CMDAT 0x0C
107 +#define JZ_REG_MMC_RESTO 0x10
108 +#define JZ_REG_MMC_RDTO 0x14
109 +#define JZ_REG_MMC_BLKLEN 0x18
110 +#define JZ_REG_MMC_NOB 0x1C
111 +#define JZ_REG_MMC_SNOB 0x20
112 +#define JZ_REG_MMC_IMASK 0x24
113 +#define JZ_REG_MMC_IREG 0x28
114 +#define JZ_REG_MMC_CMD 0x2C
115 +#define JZ_REG_MMC_ARG 0x30
116 +#define JZ_REG_MMC_RESP_FIFO 0x34
117 +#define JZ_REG_MMC_RXFIFO 0x38
118 +#define JZ_REG_MMC_TXFIFO 0x3C
120 +#define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
121 +#define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
122 +#define JZ_MMC_STRPCL_START_READWAIT BIT(5)
123 +#define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
124 +#define JZ_MMC_STRPCL_RESET BIT(3)
125 +#define JZ_MMC_STRPCL_START_OP BIT(2)
126 +#define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
127 +#define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
128 +#define JZ_MMC_STRPCL_CLOCK_START BIT(1)
131 +#define JZ_MMC_STATUS_IS_RESETTING BIT(15)
132 +#define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
133 +#define JZ_MMC_STATUS_PRG_DONE BIT(13)
134 +#define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
135 +#define JZ_MMC_STATUS_END_CMD_RES BIT(11)
136 +#define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
137 +#define JZ_MMC_STATUS_IS_READWAIT BIT(9)
138 +#define JZ_MMC_STATUS_CLK_EN BIT(8)
139 +#define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
140 +#define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
141 +#define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
142 +#define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
143 +#define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
144 +#define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
145 +#define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
146 +#define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
148 +#define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
149 +#define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
152 +#define JZ_MMC_CMDAT_IO_ABORT BIT(11)
153 +#define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
154 +#define JZ_MMC_CMDAT_DMA_EN BIT(8)
155 +#define JZ_MMC_CMDAT_INIT BIT(7)
156 +#define JZ_MMC_CMDAT_BUSY BIT(6)
157 +#define JZ_MMC_CMDAT_STREAM BIT(5)
158 +#define JZ_MMC_CMDAT_WRITE BIT(4)
159 +#define JZ_MMC_CMDAT_DATA_EN BIT(3)
160 +#define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
161 +#define JZ_MMC_CMDAT_RSP_R1 1
162 +#define JZ_MMC_CMDAT_RSP_R2 2
163 +#define JZ_MMC_CMDAT_RSP_R3 3
165 +#define JZ_MMC_IRQ_SDIO BIT(7)
166 +#define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
167 +#define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
168 +#define JZ_MMC_IRQ_END_CMD_RES BIT(2)
169 +#define JZ_MMC_IRQ_PRG_DONE BIT(1)
170 +#define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
173 +#define JZ_MMC_CLK_RATE 24000000
175 +enum jz4740_mmc_state {
176 + JZ4740_MMC_STATE_READ_RESPONSE,
177 + JZ4740_MMC_STATE_TRANSFER_DATA,
178 + JZ4740_MMC_STATE_SEND_STOP,
179 + JZ4740_MMC_STATE_DONE,
182 +struct jz4740_mmc_host {
183 + struct mmc_host *mmc;
184 + struct platform_device *pdev;
185 + struct jz4740_mmc_platform_data *pdata;
189 + int card_detect_irq;
191 + struct resource *mem;
192 + void __iomem *base;
193 + struct mmc_request *req;
194 + struct mmc_command *cmd;
196 + unsigned long waiting;
204 + struct timer_list timeout_timer;
205 + struct sg_mapping_iter miter;
206 + enum jz4740_mmc_state state;
209 +static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
210 + unsigned int irq, bool enabled)
212 + unsigned long flags;
214 + spin_lock_irqsave(&host->lock, flags);
216 + host->irq_mask &= ~irq;
218 + host->irq_mask |= irq;
219 + spin_unlock_irqrestore(&host->lock, flags);
221 + writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
224 +static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
225 + bool start_transfer)
227 + uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
229 + if (start_transfer)
230 + val |= JZ_MMC_STRPCL_START_OP;
232 + writew(val, host->base + JZ_REG_MMC_STRPCL);
235 +static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
238 + unsigned int timeout = 1000;
240 + writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
242 + status = readl(host->base + JZ_REG_MMC_STATUS);
243 + } while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
246 +static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
249 + unsigned int timeout = 1000;
251 + writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
254 + status = readl(host->base + JZ_REG_MMC_STATUS);
255 + } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
258 +static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
260 + struct mmc_request *req;
265 + mmc_request_done(host->mmc, req);
268 +static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
271 + unsigned int timeout = 0x800;
275 + status = readw(host->base + JZ_REG_MMC_IREG);
276 + } while (!(status & irq) && --timeout);
278 + if (timeout == 0) {
279 + set_bit(0, &host->waiting);
280 + mod_timer(&host->timeout_timer, jiffies + 5*HZ);
281 + jz4740_mmc_set_irq_enabled(host, irq, true);
288 +static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
289 + struct mmc_data *data)
293 + status = readl(host->base + JZ_REG_MMC_STATUS);
294 + if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
295 + if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
296 + host->req->cmd->error = -ETIMEDOUT;
297 + data->error = -ETIMEDOUT;
299 + host->req->cmd->error = -EIO;
300 + data->error = -EIO;
305 +static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
306 + struct mmc_data *data)
308 + struct sg_mapping_iter *miter = &host->miter;
309 + void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
314 + while (sg_miter_next(miter)) {
316 + i = miter->length / 4;
320 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
321 + if (unlikely(timeout))
324 + writel(buf[0], fifo_addr);
325 + writel(buf[1], fifo_addr);
326 + writel(buf[2], fifo_addr);
327 + writel(buf[3], fifo_addr);
328 + writel(buf[4], fifo_addr);
329 + writel(buf[5], fifo_addr);
330 + writel(buf[6], fifo_addr);
331 + writel(buf[7], fifo_addr);
336 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
337 + if (unlikely(timeout))
341 + writel(*buf, fifo_addr);
346 + data->bytes_xfered += miter->length;
348 + sg_miter_stop(miter);
353 + miter->consumed = (void *)buf - miter->addr;
354 + data->bytes_xfered += miter->consumed;
355 + sg_miter_stop(miter);
360 +static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host,
361 + struct mmc_data *data)
363 + struct sg_mapping_iter *miter = &host->miter;
364 + void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO;
369 + unsigned int timeout;
371 + while (sg_miter_next(miter)) {
377 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
378 + if (unlikely(timeout))
381 + buf[0] = readl(fifo_addr);
382 + buf[1] = readl(fifo_addr);
383 + buf[2] = readl(fifo_addr);
384 + buf[3] = readl(fifo_addr);
385 + buf[4] = readl(fifo_addr);
386 + buf[5] = readl(fifo_addr);
387 + buf[6] = readl(fifo_addr);
388 + buf[7] = readl(fifo_addr);
395 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
396 + if (unlikely(timeout))
400 + *buf++ = readl(fifo_addr);
403 + if (unlikely(i > 0)) {
404 + d = readl(fifo_addr);
405 + memcpy(buf, &d, i);
408 + data->bytes_xfered += miter->length;
410 + /* This can go away once MIPS implements
411 + * flush_kernel_dcache_page */
412 + flush_dcache_page(miter->page);
414 + sg_miter_stop(miter);
416 + /* For whatever reason there is sometime one word more in the fifo then
419 + status = readl(host->base + JZ_REG_MMC_STATUS);
420 + while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) {
421 + d = readl(fifo_addr);
422 + status = readl(host->base + JZ_REG_MMC_STATUS);
428 + miter->consumed = (void *)buf - miter->addr;
429 + data->bytes_xfered += miter->consumed;
430 + sg_miter_stop(miter);
435 +static void jz4740_mmc_timeout(unsigned long data)
437 + struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)data;
439 + if (!test_and_clear_bit(0, &host->waiting))
442 + jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false);
444 + host->req->cmd->error = -ETIMEDOUT;
445 + jz4740_mmc_request_done(host);
448 +static void jz4740_mmc_read_response(struct jz4740_mmc_host *host,
449 + struct mmc_command *cmd)
453 + void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO;
455 + if (cmd->flags & MMC_RSP_136) {
456 + tmp = readw(fifo_addr);
457 + for (i = 0; i < 4; ++i) {
458 + cmd->resp[i] = tmp << 24;
459 + tmp = readw(fifo_addr);
460 + cmd->resp[i] |= tmp << 8;
461 + tmp = readw(fifo_addr);
462 + cmd->resp[i] |= tmp >> 8;
465 + cmd->resp[0] = readw(fifo_addr) << 24;
466 + cmd->resp[0] |= readw(fifo_addr) << 8;
467 + cmd->resp[0] |= readw(fifo_addr) & 0xff;
471 +static void jz4740_mmc_send_command(struct jz4740_mmc_host *host,
472 + struct mmc_command *cmd)
474 + uint32_t cmdat = host->cmdat;
476 + host->cmdat &= ~JZ_MMC_CMDAT_INIT;
477 + jz4740_mmc_clock_disable(host);
481 + if (cmd->flags & MMC_RSP_BUSY)
482 + cmdat |= JZ_MMC_CMDAT_BUSY;
484 + switch (mmc_resp_type(cmd)) {
487 + cmdat |= JZ_MMC_CMDAT_RSP_R1;
490 + cmdat |= JZ_MMC_CMDAT_RSP_R2;
493 + cmdat |= JZ_MMC_CMDAT_RSP_R3;
500 + cmdat |= JZ_MMC_CMDAT_DATA_EN;
501 + if (cmd->data->flags & MMC_DATA_WRITE)
502 + cmdat |= JZ_MMC_CMDAT_WRITE;
503 + if (cmd->data->flags & MMC_DATA_STREAM)
504 + cmdat |= JZ_MMC_CMDAT_STREAM;
506 + writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
507 + writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
510 + writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
511 + writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
512 + writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
514 + jz4740_mmc_clock_enable(host, 1);
517 +static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host)
519 + struct mmc_command *cmd = host->req->cmd;
520 + struct mmc_data *data = cmd->data;
523 + if (data->flags & MMC_DATA_READ)
524 + direction = SG_MITER_TO_SG;
526 + direction = SG_MITER_FROM_SG;
528 + sg_miter_start(&host->miter, data->sg, data->sg_len, direction);
532 +static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
534 + struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
535 + struct mmc_command *cmd = host->req->cmd;
536 + struct mmc_request *req = host->req;
537 + bool timeout = false;
540 + host->state = JZ4740_MMC_STATE_DONE;
542 + switch (host->state) {
543 + case JZ4740_MMC_STATE_READ_RESPONSE:
544 + if (cmd->flags & MMC_RSP_PRESENT)
545 + jz4740_mmc_read_response(host, cmd);
550 + jz_mmc_prepare_data_transfer(host);
552 + case JZ4740_MMC_STATE_TRANSFER_DATA:
553 + if (cmd->data->flags & MMC_DATA_READ)
554 + timeout = jz4740_mmc_read_data(host, cmd->data);
556 + timeout = jz4740_mmc_write_data(host, cmd->data);
558 + if (unlikely(timeout)) {
559 + host->state = JZ4740_MMC_STATE_TRANSFER_DATA;
563 + jz4740_mmc_transfer_check_state(host, cmd->data);
565 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
566 + if (unlikely(timeout)) {
567 + host->state = JZ4740_MMC_STATE_SEND_STOP;
570 + writew(JZ_MMC_IRQ_DATA_TRAN_DONE, host->base + JZ_REG_MMC_IREG);
572 + case JZ4740_MMC_STATE_SEND_STOP:
576 + jz4740_mmc_send_command(host, req->stop);
578 + timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_PRG_DONE);
580 + host->state = JZ4740_MMC_STATE_DONE;
583 + case JZ4740_MMC_STATE_DONE:
588 + jz4740_mmc_request_done(host);
590 + return IRQ_HANDLED;
593 +static irqreturn_t jz_mmc_irq(int irq, void *devid)
595 + struct jz4740_mmc_host *host = devid;
596 + struct mmc_command *cmd = host->cmd;
597 + uint16_t irq_reg, status, tmp;
599 + irq_reg = readw(host->base + JZ_REG_MMC_IREG);
602 + irq_reg &= ~host->irq_mask;
604 + tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
605 + JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
607 + if (tmp != irq_reg)
608 + writew(tmp & ~irq_reg, host->base + JZ_REG_MMC_IREG);
610 + if (irq_reg & JZ_MMC_IRQ_SDIO) {
611 + writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
612 + mmc_signal_sdio_irq(host->mmc);
613 + irq_reg &= ~JZ_MMC_IRQ_SDIO;
616 + if (host->req && cmd && irq_reg) {
617 + if (test_and_clear_bit(0, &host->waiting)) {
618 + del_timer(&host->timeout_timer);
620 + status = readl(host->base + JZ_REG_MMC_STATUS);
622 + if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
623 + cmd->error = -ETIMEDOUT;
624 + } else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
626 + } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
627 + JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
629 + cmd->data->error = -EIO;
631 + } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
632 + JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
634 + cmd->data->error = -EIO;
638 + jz4740_mmc_set_irq_enabled(host, irq_reg, false);
639 + writew(irq_reg, host->base + JZ_REG_MMC_IREG);
641 + return IRQ_WAKE_THREAD;
645 + return IRQ_HANDLED;
648 +static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate)
653 + jz4740_mmc_clock_disable(host);
654 + clk_set_rate(host->clk, JZ_MMC_CLK_RATE);
656 + real_rate = clk_get_rate(host->clk);
658 + while (real_rate > rate && div < 7) {
663 + writew(div, host->base + JZ_REG_MMC_CLKRT);
667 +static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
669 + struct jz4740_mmc_host *host = mmc_priv(mmc);
673 + writew(0xffff, host->base + JZ_REG_MMC_IREG);
675 + writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
676 + jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true);
678 + host->state = JZ4740_MMC_STATE_READ_RESPONSE;
679 + set_bit(0, &host->waiting);
680 + mod_timer(&host->timeout_timer, jiffies + 5*HZ);
681 + jz4740_mmc_send_command(host, req->cmd);
684 +static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
686 + struct jz4740_mmc_host *host = mmc_priv(mmc);
688 + jz4740_mmc_set_clock_rate(host, ios->clock);
690 + switch (ios->power_mode) {
692 + jz4740_mmc_reset(host);
693 + if (gpio_is_valid(host->pdata->gpio_power))
694 + gpio_set_value(host->pdata->gpio_power,
695 + !host->pdata->power_active_low);
696 + host->cmdat |= JZ_MMC_CMDAT_INIT;
697 + clk_enable(host->clk);
702 + if (gpio_is_valid(host->pdata->gpio_power))
703 + gpio_set_value(host->pdata->gpio_power,
704 + host->pdata->power_active_low);
705 + clk_disable(host->clk);
709 + switch (ios->bus_width) {
710 + case MMC_BUS_WIDTH_1:
711 + host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
713 + case MMC_BUS_WIDTH_4:
714 + host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
721 +static int jz4740_mmc_get_ro(struct mmc_host *mmc)
723 + struct jz4740_mmc_host *host = mmc_priv(mmc);
724 + if (!gpio_is_valid(host->pdata->gpio_read_only))
727 + return gpio_get_value(host->pdata->gpio_read_only) ^
728 + host->pdata->read_only_active_low;
731 +static int jz4740_mmc_get_cd(struct mmc_host *mmc)
733 + struct jz4740_mmc_host *host = mmc_priv(mmc);
734 + if (!gpio_is_valid(host->pdata->gpio_card_detect))
737 + return gpio_get_value(host->pdata->gpio_card_detect) ^
738 + host->pdata->card_detect_active_low;
741 +static irqreturn_t jz4740_mmc_card_detect_irq(int irq, void *devid)
743 + struct jz4740_mmc_host *host = devid;
745 + mmc_detect_change(host->mmc, HZ / 2);
747 + return IRQ_HANDLED;
750 +static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
752 + struct jz4740_mmc_host *host = mmc_priv(mmc);
753 + jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
756 +static const struct mmc_host_ops jz4740_mmc_ops = {
757 + .request = jz4740_mmc_request,
758 + .set_ios = jz4740_mmc_set_ios,
759 + .get_ro = jz4740_mmc_get_ro,
760 + .get_cd = jz4740_mmc_get_cd,
761 + .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
764 +static const struct jz_gpio_bulk_request jz4740_mmc_pins[] = {
765 + JZ_GPIO_BULK_PIN(MSC_CMD),
766 + JZ_GPIO_BULK_PIN(MSC_CLK),
767 + JZ_GPIO_BULK_PIN(MSC_DATA0),
768 + JZ_GPIO_BULK_PIN(MSC_DATA1),
769 + JZ_GPIO_BULK_PIN(MSC_DATA2),
770 + JZ_GPIO_BULK_PIN(MSC_DATA3),
773 +static int __devinit jz4740_mmc_request_gpio(struct device *dev, int gpio,
774 + const char *name, bool output, int value)
778 + if (!gpio_is_valid(gpio))
781 + ret = gpio_request(gpio, name);
783 + dev_err(dev, "Failed to request %s gpio: %d\n", name, ret);
788 + gpio_direction_output(gpio, value);
790 + gpio_direction_input(gpio);
795 +static int __devinit jz4740_mmc_request_gpios(struct platform_device *pdev)
798 + struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
803 + ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_card_detect,
804 + "MMC detect change", false, 0);
808 + ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_read_only,
809 + "MMC read only", false, 0);
811 + goto err_free_gpio_card_detect;
813 + ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_power,
814 + "MMC read only", true, pdata->power_active_low);
816 + goto err_free_gpio_read_only;
820 +err_free_gpio_read_only:
821 + if (gpio_is_valid(pdata->gpio_read_only))
822 + gpio_free(pdata->gpio_read_only);
823 +err_free_gpio_card_detect:
824 + if (gpio_is_valid(pdata->gpio_card_detect))
825 + gpio_free(pdata->gpio_card_detect);
830 +static int __devinit jz4740_mmc_request_cd_irq(struct platform_device *pdev,
831 + struct jz4740_mmc_host *host)
833 + struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
835 + if (!gpio_is_valid(pdata->gpio_card_detect))
838 + host->card_detect_irq = gpio_to_irq(pdata->gpio_card_detect);
839 + if (host->card_detect_irq < 0) {
840 + dev_warn(&pdev->dev, "Failed to get card detect irq\n");
844 + return request_irq(host->card_detect_irq, jz4740_mmc_card_detect_irq,
845 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
846 + "MMC card detect", host);
849 +static void jz4740_mmc_free_gpios(struct platform_device *pdev)
851 + struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
856 + if (gpio_is_valid(pdata->gpio_power))
857 + gpio_free(pdata->gpio_power);
858 + if (gpio_is_valid(pdata->gpio_read_only))
859 + gpio_free(pdata->gpio_read_only);
860 + if (gpio_is_valid(pdata->gpio_card_detect))
861 + gpio_free(pdata->gpio_card_detect);
864 +static inline size_t jz4740_mmc_num_pins(struct jz4740_mmc_host *host)
866 + size_t num_pins = ARRAY_SIZE(jz4740_mmc_pins);
867 + if (host->pdata && host->pdata->data_1bit)
873 +static int __devinit jz4740_mmc_probe(struct platform_device* pdev)
876 + struct mmc_host *mmc;
877 + struct jz4740_mmc_host *host;
878 + struct jz4740_mmc_platform_data *pdata;
880 + pdata = pdev->dev.platform_data;
882 + mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
884 + dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
888 + host = mmc_priv(mmc);
889 + host->pdata = pdata;
891 + host->irq = platform_get_irq(pdev, 0);
892 + if (host->irq < 0) {
894 + dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
895 + goto err_free_host;
898 + host->clk = clk_get(&pdev->dev, "mmc");
901 + dev_err(&pdev->dev, "Failed to get mmc clock\n");
902 + goto err_free_host;
905 + host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
908 + dev_err(&pdev->dev, "Failed to get base platform memory\n");
912 + host->mem = request_mem_region(host->mem->start,
913 + resource_size(host->mem), pdev->name);
916 + dev_err(&pdev->dev, "Failed to request base memory region\n");
920 + host->base = ioremap_nocache(host->mem->start, resource_size(host->mem));
923 + dev_err(&pdev->dev, "Failed to ioremap base memory\n");
924 + goto err_release_mem_region;
927 + ret = jz_gpio_bulk_request(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
929 + dev_err(&pdev->dev, "Failed to request mmc pins: %d\n", ret);
933 + ret = jz4740_mmc_request_gpios(pdev);
935 + goto err_gpio_bulk_free;
937 + mmc->ops = &jz4740_mmc_ops;
938 + mmc->f_min = JZ_MMC_CLK_RATE / 128;
939 + mmc->f_max = JZ_MMC_CLK_RATE;
940 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
941 + mmc->caps = (pdata && pdata->data_1bit) ? 0 : MMC_CAP_4_BIT_DATA;
942 + mmc->caps |= MMC_CAP_SDIO_IRQ;
944 + mmc->max_blk_size = (1 << 10) - 1;
945 + mmc->max_blk_count = (1 << 15) - 1;
946 + mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
948 + mmc->max_phys_segs = 128;
949 + mmc->max_hw_segs = 128;
950 + mmc->max_seg_size = mmc->max_req_size;
954 + spin_lock_init(&host->lock);
955 + host->irq_mask = 0xffff;
957 + ret = jz4740_mmc_request_cd_irq(pdev, host);
959 + dev_err(&pdev->dev, "Failed to request card detect irq\n");
960 + goto err_free_gpios;
963 + ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0,
964 + dev_name(&pdev->dev), host);
966 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
967 + goto err_free_card_detect_irq;
970 + jz4740_mmc_reset(host);
971 + jz4740_mmc_clock_disable(host);
972 + setup_timer(&host->timeout_timer, jz4740_mmc_timeout,
973 + (unsigned long)host);
974 + /* It is not important when it times out, it just needs to timeout. */
975 + set_timer_slack(&host->timeout_timer, HZ);
977 + platform_set_drvdata(pdev, host);
978 + ret = mmc_add_host(mmc);
981 + dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
984 + dev_info(&pdev->dev, "JZ SD/MMC card driver registered\n");
989 + free_irq(host->irq, host);
990 +err_free_card_detect_irq:
991 + if (host->card_detect_irq >= 0)
992 + free_irq(host->card_detect_irq, host);
994 + jz4740_mmc_free_gpios(pdev);
996 + jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
998 + iounmap(host->base);
999 +err_release_mem_region:
1000 + release_mem_region(host->mem->start, resource_size(host->mem));
1002 + clk_put(host->clk);
1004 + platform_set_drvdata(pdev, NULL);
1005 + mmc_free_host(mmc);
1010 +static int __devexit jz4740_mmc_remove(struct platform_device *pdev)
1012 + struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
1014 + del_timer_sync(&host->timeout_timer);
1015 + jz4740_mmc_set_irq_enabled(host, 0xff, false);
1016 + jz4740_mmc_reset(host);
1018 + mmc_remove_host(host->mmc);
1020 + free_irq(host->irq, host);
1021 + if (host->card_detect_irq >= 0)
1022 + free_irq(host->card_detect_irq, host);
1024 + jz4740_mmc_free_gpios(pdev);
1025 + jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
1027 + iounmap(host->base);
1028 + release_mem_region(host->mem->start, resource_size(host->mem));
1030 + clk_put(host->clk);
1032 + platform_set_drvdata(pdev, NULL);
1033 + mmc_free_host(host->mmc);
1040 +static int jz4740_mmc_suspend(struct device *dev)
1042 + struct jz4740_mmc_host *host = dev_get_drvdata(dev);
1044 + mmc_suspend_host(host->mmc);
1046 + jz_gpio_bulk_suspend(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
1051 +static int jz4740_mmc_resume(struct device *dev)
1053 + struct jz4740_mmc_host *host = dev_get_drvdata(dev);
1055 + jz_gpio_bulk_resume(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
1057 + mmc_resume_host(host->mmc);
1062 +const struct dev_pm_ops jz4740_mmc_pm_ops = {
1063 + .suspend = jz4740_mmc_suspend,
1064 + .resume = jz4740_mmc_resume,
1065 + .poweroff = jz4740_mmc_suspend,
1066 + .restore = jz4740_mmc_resume,
1069 +#define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1071 +#define JZ4740_MMC_PM_OPS NULL
1074 +static struct platform_driver jz4740_mmc_driver = {
1075 + .probe = jz4740_mmc_probe,
1076 + .remove = __devexit_p(jz4740_mmc_remove),
1078 + .name = "jz4740-mmc",
1079 + .owner = THIS_MODULE,
1080 + .pm = JZ4740_MMC_PM_OPS,
1084 +static int __init jz4740_mmc_init(void)
1086 + return platform_driver_register(&jz4740_mmc_driver);
1088 +module_init(jz4740_mmc_init);
1090 +static void __exit jz4740_mmc_exit(void)
1092 + platform_driver_unregister(&jz4740_mmc_driver);
1094 +module_exit(jz4740_mmc_exit);
1096 +MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1097 +MODULE_LICENSE("GPL");
1098 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");