2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ7420/JZ4740 GPIO SD/MMC controller driver
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/mmc/host.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/delay.h>
23 #include <linux/scatterlist.h>
24 #include <linux/clk.h>
25 #include <linux/mmc/jz4740_mmc.h>
27 #include <linux/gpio.h>
28 #include <asm/mach-jz4740/gpio.h>
29 #include <asm/cacheflush.h>
30 #include <linux/dma-mapping.h>
32 #define JZ_REG_MMC_STRPCL 0x00
33 #define JZ_REG_MMC_STATUS 0x04
34 #define JZ_REG_MMC_CLKRT 0x08
35 #define JZ_REG_MMC_CMDAT 0x0C
36 #define JZ_REG_MMC_RESTO 0x10
37 #define JZ_REG_MMC_RDTO 0x14
38 #define JZ_REG_MMC_BLKLEN 0x18
39 #define JZ_REG_MMC_NOB 0x1C
40 #define JZ_REG_MMC_SNOB 0x20
41 #define JZ_REG_MMC_IMASK 0x24
42 #define JZ_REG_MMC_IREG 0x28
43 #define JZ_REG_MMC_CMD 0x2C
44 #define JZ_REG_MMC_ARG 0x30
45 #define JZ_REG_MMC_RESP_FIFO 0x34
46 #define JZ_REG_MMC_RXFIFO 0x38
47 #define JZ_REG_MMC_TXFIFO 0x3C
49 #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
50 #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
51 #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
52 #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
53 #define JZ_MMC_STRPCL_RESET BIT(3)
54 #define JZ_MMC_STRPCL_START_OP BIT(2)
55 #define JZ_MMC_STRPCL_CLOCK_CONTROL BIT(1) | BIT(0)
56 #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
57 #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
60 #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
61 #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
62 #define JZ_MMC_STATUS_PRG_DONE BIT(13)
63 #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
64 #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
65 #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
66 #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
67 #define JZ_MMC_STATUS_CLK_EN BIT(8)
68 #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
69 #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
70 #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
71 #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
72 #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
73 #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
74 #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
75 #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
77 #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
78 #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
81 #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
82 #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
83 #define JZ_MMC_CMDAT_DMA_EN BIT(8)
84 #define JZ_MMC_CMDAT_INIT BIT(7)
85 #define JZ_MMC_CMDAT_BUSY BIT(6)
86 #define JZ_MMC_CMDAT_STREAM BIT(5)
87 #define JZ_MMC_CMDAT_WRITE BIT(4)
88 #define JZ_MMC_CMDAT_DATA_EN BIT(3)
89 #define JZ_MMC_CMDAT_RESPONSE_FORMAT BIT(2) | BIT(1) | BIT(0)
90 #define JZ_MMC_CMDAT_RSP_R1 1
91 #define JZ_MMC_CMDAT_RSP_R2 2
92 #define JZ_MMC_CMDAT_RSP_R3 3
94 #define JZ_MMC_IRQ_SDIO BIT(7)
95 #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
96 #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
97 #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
98 #define JZ_MMC_IRQ_PRG_DONE BIT(1)
99 #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
102 #define JZ_MMC_CLK_RATE 24000000
104 struct jz4740_mmc_host
{
105 struct mmc_host
*mmc
;
106 struct platform_device
*pdev
;
107 struct jz4740_mmc_platform_data
*pdata
;
113 struct resource
*mem
;
115 struct mmc_request
*req
;
116 struct mmc_command
*cmd
;
124 struct timer_list clock_timer
;
125 struct timer_list timeout_timer
;
129 static void jz4740_mmc_cmd_done(struct jz4740_mmc_host
*host
);
131 static void jz4740_mmc_enable_irq(struct jz4740_mmc_host
*host
, unsigned int irq
)
134 spin_lock_irqsave(&host
->lock
, flags
);
136 host
->irq_mask
&= ~irq
;
137 writew(host
->irq_mask
, host
->base
+ JZ_REG_MMC_IMASK
);
139 spin_unlock_irqrestore(&host
->lock
, flags
);
142 static void jz4740_mmc_disable_irq(struct jz4740_mmc_host
*host
, unsigned int irq
)
145 spin_lock_irqsave(&host
->lock
, flags
);
147 host
->irq_mask
|= irq
;
148 writew(host
->irq_mask
, host
->base
+ JZ_REG_MMC_IMASK
);
150 spin_unlock_irqrestore(&host
->lock
, flags
);
153 static void jz4740_mmc_clock_enable(struct jz4740_mmc_host
*host
, bool start_transfer
)
155 uint16_t val
= JZ_MMC_STRPCL_CLOCK_START
;
158 val
|= JZ_MMC_STRPCL_START_OP
;
160 writew(val
, host
->base
+ JZ_REG_MMC_STRPCL
);
163 static void jz4740_mmc_clock_disable(struct jz4740_mmc_host
*host
)
166 writew(JZ_MMC_STRPCL_CLOCK_STOP
, host
->base
+ JZ_REG_MMC_STRPCL
);
168 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
169 } while (status
& JZ_MMC_STATUS_CLK_EN
);
173 static void jz4740_mmc_reset(struct jz4740_mmc_host
*host
)
175 writew(JZ_MMC_STRPCL_RESET
, host
->base
+ JZ_REG_MMC_STRPCL
);
177 while(readw(host
->base
+ JZ_REG_MMC_STATUS
) & JZ_MMC_STATUS_IS_RESETTING
);
180 static void jz4740_mmc_request_done(struct jz4740_mmc_host
*host
)
182 struct mmc_request
*req
;
185 spin_lock_irqsave(&host
->lock
, flags
);
189 spin_unlock_irqrestore(&host
->lock
, flags
);
194 /* if (req->cmd->error != 0) {
196 jz4740_mmc_reset(host);
199 mmc_request_done(host
->mmc
, req
);
202 static void jz4740_mmc_write_data(struct jz4740_mmc_host
*host
, struct mmc_data
*data
) {
203 struct scatterlist
*sg
;
204 uint32_t *sg_pointer
;
208 for (sg
= data
->sg
; sg
; sg
= sg_next(sg
)) {
209 sg_pointer
= sg_virt(sg
);
215 status
= readw(host
->base
+ JZ_REG_MMC_IREG
);
216 } while (!(status
& JZ_MMC_IRQ_TXFIFO_WR_REQ
));
217 writew(JZ_MMC_IRQ_TXFIFO_WR_REQ
, host
->base
+ JZ_REG_MMC_IREG
);
219 writel(sg_pointer
[0], host
->base
+ JZ_REG_MMC_TXFIFO
);
220 writel(sg_pointer
[1], host
->base
+ JZ_REG_MMC_TXFIFO
);
221 writel(sg_pointer
[2], host
->base
+ JZ_REG_MMC_TXFIFO
);
222 writel(sg_pointer
[3], host
->base
+ JZ_REG_MMC_TXFIFO
);
223 writel(sg_pointer
[4], host
->base
+ JZ_REG_MMC_TXFIFO
);
224 writel(sg_pointer
[5], host
->base
+ JZ_REG_MMC_TXFIFO
);
225 writel(sg_pointer
[6], host
->base
+ JZ_REG_MMC_TXFIFO
);
226 writel(sg_pointer
[7], host
->base
+ JZ_REG_MMC_TXFIFO
);
232 status
= readw(host
->base
+ JZ_REG_MMC_IREG
);
233 } while (!(status
& JZ_MMC_IRQ_TXFIFO_WR_REQ
));
234 writew(JZ_MMC_IRQ_TXFIFO_WR_REQ
, host
->base
+ JZ_REG_MMC_IREG
);
237 writel(*sg_pointer
, host
->base
+ JZ_REG_MMC_TXFIFO
);
242 data
->bytes_xfered
+= sg
->length
;
245 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
246 if (status
& JZ_MMC_STATUS_WRITE_ERROR_MASK
)
249 writew(JZ_MMC_IRQ_TXFIFO_WR_REQ
, host
->base
+ JZ_REG_MMC_IREG
);
251 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
252 } while ((status
& JZ_MMC_STATUS_DATA_TRAN_DONE
) == 0);
253 writew(JZ_MMC_IRQ_DATA_TRAN_DONE
, host
->base
+ JZ_REG_MMC_IREG
);
257 if(status
& (JZ_MMC_STATUS_TIMEOUT_WRITE
)) {
258 host
->req
->cmd
->error
= -ETIMEDOUT
;
259 data
->error
= -ETIMEDOUT
;
261 host
->req
->cmd
->error
= -EILSEQ
;
262 data
->error
= -EILSEQ
;
266 static void jz4740_mmc_timeout(unsigned long data
)
268 struct jz4740_mmc_host
*host
= (struct jz4740_mmc_host
*)data
;
271 spin_lock_irqsave(&host
->lock
, flags
);
272 if (!host
->waiting
) {
273 spin_unlock_irqrestore(&host
->lock
, flags
);
279 spin_unlock_irqrestore(&host
->lock
, flags
);
281 host
->req
->cmd
->error
= -ETIMEDOUT
;
282 jz4740_mmc_request_done(host
);
285 static void jz4740_mmc_read_data(struct jz4740_mmc_host
*host
, struct mmc_data
*data
) {
286 struct scatterlist
*sg
;
287 uint32_t *sg_pointer
;
292 for (sg
= data
->sg
; sg
; sg
= sg_next(sg
)) {
293 sg_pointer
= sg_virt(sg
);
299 status
= readw(host
->base
+ JZ_REG_MMC_IREG
);
300 } while (!(status
& JZ_MMC_IRQ_RXFIFO_RD_REQ
));
301 writew(JZ_MMC_IRQ_RXFIFO_RD_REQ
, host
->base
+ JZ_REG_MMC_IREG
);
303 sg_pointer
[0] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
304 sg_pointer
[1] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
305 sg_pointer
[2] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
306 sg_pointer
[3] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
307 sg_pointer
[4] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
308 sg_pointer
[5] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
309 sg_pointer
[6] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
310 sg_pointer
[7] = readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
318 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
319 } while ((status
& JZ_MMC_STATUS_DATA_FIFO_EMPTY
));
321 *sg_pointer
= readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
326 d
= readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
327 memcpy(sg_pointer
, &d
, i
);
329 data
->bytes_xfered
+= sg
->length
;
331 flush_dcache_page(sg_page(sg
));
334 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
335 if (status
& JZ_MMC_STATUS_READ_ERROR_MASK
)
338 /* For whatever reason there is sometime one word more in the fifo then
340 while ((status
& JZ_MMC_STATUS_DATA_FIFO_EMPTY
) == 0) {
341 d
= readl(host
->base
+ JZ_REG_MMC_RXFIFO
);
342 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
347 if(status
& JZ_MMC_STATUS_TIMEOUT_READ
) {
348 host
->req
->cmd
->error
= -ETIMEDOUT
;
349 data
->error
= -ETIMEDOUT
;
351 host
->req
->cmd
->error
= -EILSEQ
;
352 data
->error
= -EILSEQ
;
356 static irqreturn_t
jz_mmc_irq_worker(int irq
, void *devid
)
358 struct jz4740_mmc_host
*host
= (struct jz4740_mmc_host
*)devid
;
360 if (host
->cmd
->error
)
361 jz4740_mmc_request_done(host
);
363 jz4740_mmc_cmd_done(host
);
368 static irqreturn_t
jz_mmc_irq(int irq
, void *devid
)
370 struct jz4740_mmc_host
*host
= devid
;
371 uint16_t irq_reg
, status
, tmp
;
373 irqreturn_t ret
= IRQ_HANDLED
;
375 irq_reg
= readw(host
->base
+ JZ_REG_MMC_IREG
);
378 spin_lock(&host
->lock
);
379 irq_reg
&= ~host
->irq_mask
;
380 spin_unlock(&host
->lock
);
382 if (irq_reg
& JZ_MMC_IRQ_SDIO
) {
383 writew(JZ_MMC_IRQ_SDIO
, host
->base
+ JZ_REG_MMC_IREG
);
384 mmc_signal_sdio_irq(host
->mmc
);
387 if (!host
->req
|| !host
->cmd
) {
392 spin_lock_irqsave(&host
->lock
, flags
);
393 if (!host
->waiting
) {
394 spin_unlock_irqrestore(&host
->lock
, flags
);
398 spin_unlock_irqrestore(&host
->lock
, flags
);
400 del_timer(&host
->timeout_timer
);
402 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
404 if (status
& JZ_MMC_STATUS_TIMEOUT_RES
) {
405 host
->cmd
->error
= -ETIMEDOUT
;
406 } else if (status
& JZ_MMC_STATUS_CRC_RES_ERR
) {
407 host
->cmd
->error
= -EIO
;
408 } else if(status
& (JZ_MMC_STATUS_CRC_READ_ERROR
|
409 JZ_MMC_STATUS_CRC_WRITE_ERROR
)) {
410 host
->cmd
->data
->error
= -EIO
;
411 } else if(status
& (JZ_MMC_STATUS_CRC_READ_ERROR
|
412 JZ_MMC_STATUS_CRC_WRITE_ERROR
)) {
413 host
->cmd
->data
->error
= -EIO
;
416 if (irq_reg
& JZ_MMC_IRQ_END_CMD_RES
) {
417 jz4740_mmc_disable_irq(host
, JZ_MMC_IRQ_END_CMD_RES
);
418 writew(JZ_MMC_IRQ_END_CMD_RES
, host
->base
+ JZ_REG_MMC_IREG
);
419 ret
= IRQ_WAKE_THREAD
;
425 writew(0xff, host
->base
+ JZ_REG_MMC_IREG
);
429 static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host
*host
, int rate
) {
431 int real_rate
= host
->max_clock
;
432 jz4740_mmc_clock_disable(host
);
434 while ((real_rate
>> 1) >= rate
&& div
< 7) {
438 clk_set_rate(host
->clk
, JZ_MMC_CLK_RATE
);
440 writew(div
, host
->base
+ JZ_REG_MMC_CLKRT
);
445 static void jz4740_mmc_read_response(struct jz4740_mmc_host
*host
, struct mmc_command
*cmd
)
449 if (cmd
->flags
& MMC_RSP_136
) {
450 tmp
= readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
);
451 for (i
= 0; i
< 4; ++i
) {
452 cmd
->resp
[i
] = tmp
<< 24;
453 cmd
->resp
[i
] |= readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
) << 8;
454 tmp
= readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
);
455 cmd
->resp
[i
] |= tmp
>> 8;
458 cmd
->resp
[0] = readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
) << 24;
459 cmd
->resp
[0] |= readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
) << 8;
460 cmd
->resp
[0] |= readw(host
->base
+ JZ_REG_MMC_RESP_FIFO
) & 0xff;
464 static void jz4740_mmc_send_command(struct jz4740_mmc_host
*host
, struct mmc_command
*cmd
)
466 uint32_t cmdat
= host
->cmdat
;
468 host
->cmdat
&= ~JZ_MMC_CMDAT_INIT
;
469 jz4740_mmc_clock_disable(host
);
473 if (cmd
->flags
& MMC_RSP_BUSY
)
474 cmdat
|= JZ_MMC_CMDAT_BUSY
;
476 switch (mmc_resp_type(cmd
)) {
479 cmdat
|= JZ_MMC_CMDAT_RSP_R1
;
482 cmdat
|= JZ_MMC_CMDAT_RSP_R2
;
485 cmdat
|= JZ_MMC_CMDAT_RSP_R3
;
492 cmdat
|= JZ_MMC_CMDAT_DATA_EN
;
493 if (cmd
->data
->flags
& MMC_DATA_WRITE
)
494 cmdat
|= JZ_MMC_CMDAT_WRITE
;
495 if (cmd
->data
->flags
& MMC_DATA_STREAM
)
496 cmdat
|= JZ_MMC_CMDAT_STREAM
;
498 writew(cmd
->data
->blksz
, host
->base
+ JZ_REG_MMC_BLKLEN
);
499 writew(cmd
->data
->blocks
, host
->base
+ JZ_REG_MMC_NOB
);
502 writeb(cmd
->opcode
, host
->base
+ JZ_REG_MMC_CMD
);
503 writel(cmd
->arg
, host
->base
+ JZ_REG_MMC_ARG
);
504 writel(cmdat
, host
->base
+ JZ_REG_MMC_CMDAT
);
507 jz4740_mmc_clock_enable(host
, 1);
508 mod_timer(&host
->timeout_timer
, 4*HZ
);
511 static void jz4740_mmc_cmd_done(struct jz4740_mmc_host
*host
)
514 struct mmc_command
*cmd
= host
->req
->cmd
;
515 struct mmc_request
*req
= host
->req
;
516 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
518 if (cmd
->flags
& MMC_RSP_PRESENT
)
519 jz4740_mmc_read_response(host
, cmd
);
522 if (cmd
->data
->flags
& MMC_DATA_READ
)
523 jz4740_mmc_read_data(host
, cmd
->data
);
525 jz4740_mmc_write_data(host
, cmd
->data
);
529 jz4740_mmc_send_command(host
, req
->stop
);
531 status
= readl(host
->base
+ JZ_REG_MMC_STATUS
);
532 } while ((status
& JZ_MMC_STATUS_PRG_DONE
) == 0);
533 writew(JZ_MMC_IRQ_PRG_DONE
, host
->base
+ JZ_REG_MMC_IREG
);
536 jz4740_mmc_request_done(host
);
539 static void jz4740_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
541 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
545 writew(0xffff, host
->base
+ JZ_REG_MMC_IREG
);
547 writew(JZ_MMC_IRQ_END_CMD_RES
, host
->base
+ JZ_REG_MMC_IREG
);
548 jz4740_mmc_enable_irq(host
, JZ_MMC_IRQ_END_CMD_RES
);
549 jz4740_mmc_send_command(host
, req
->cmd
);
553 static void jz4740_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
555 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
557 jz4740_mmc_set_clock_rate(host
, ios
->clock
);
559 switch(ios
->power_mode
) {
561 if (gpio_is_valid(host
->pdata
->gpio_power
))
562 gpio_set_value(host
->pdata
->gpio_power
,
563 !host
->pdata
->power_active_low
);
564 host
->cmdat
|= JZ_MMC_CMDAT_INIT
;
565 clk_enable(host
->clk
);
570 if (gpio_is_valid(host
->pdata
->gpio_power
))
571 gpio_set_value(host
->pdata
->gpio_power
,
572 host
->pdata
->power_active_low
);
573 clk_disable(host
->clk
);
577 switch(ios
->bus_width
) {
578 case MMC_BUS_WIDTH_1
:
579 host
->cmdat
&= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT
;
581 case MMC_BUS_WIDTH_4
:
582 host
->cmdat
|= JZ_MMC_CMDAT_BUS_WIDTH_4BIT
;
585 dev_err(&host
->pdev
->dev
, "Invalid bus width: %d\n", ios
->bus_width
);
589 static int jz4740_mmc_get_ro(struct mmc_host
*mmc
)
591 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
592 if (!gpio_is_valid(host
->pdata
->gpio_read_only
))
595 return gpio_get_value(host
->pdata
->gpio_read_only
) ^
596 host
->pdata
->read_only_active_low
;
599 static int jz4740_mmc_get_cd(struct mmc_host
*mmc
)
601 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
602 if (!gpio_is_valid(host
->pdata
->gpio_card_detect
))
605 return gpio_get_value(host
->pdata
->gpio_card_detect
) ^
606 host
->pdata
->card_detect_active_low
;
609 static irqreturn_t
jz4740_mmc_card_detect_irq(int irq
, void *devid
)
611 struct jz4740_mmc_host
*host
= devid
;
613 mmc_detect_change(host
->mmc
, HZ
/ 3);
618 static void jz4740_mmc_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
620 struct jz4740_mmc_host
*host
= mmc_priv(mmc
);
622 jz4740_mmc_enable_irq(host
, JZ_MMC_IRQ_SDIO
);
624 jz4740_mmc_disable_irq(host
, JZ_MMC_IRQ_SDIO
);
627 static const struct mmc_host_ops jz4740_mmc_ops
= {
628 .request
= jz4740_mmc_request
,
629 .set_ios
= jz4740_mmc_set_ios
,
630 .get_ro
= jz4740_mmc_get_ro
,
631 .get_cd
= jz4740_mmc_get_cd
,
632 .enable_sdio_irq
= jz4740_mmc_enable_sdio_irq
,
635 static const struct jz_gpio_bulk_request jz4740_mmc_pins
[] = {
636 JZ_GPIO_BULK_PIN(MSC_CMD
),
637 JZ_GPIO_BULK_PIN(MSC_CLK
),
638 JZ_GPIO_BULK_PIN(MSC_DATA0
),
639 JZ_GPIO_BULK_PIN(MSC_DATA1
),
640 JZ_GPIO_BULK_PIN(MSC_DATA2
),
641 JZ_GPIO_BULK_PIN(MSC_DATA3
),
644 static int __devinit
jz4740_mmc_request_gpios(struct platform_device
*pdev
)
647 struct jz4740_mmc_platform_data
*pdata
= pdev
->dev
.platform_data
;
652 if (gpio_is_valid(pdata
->gpio_card_detect
)) {
653 ret
= gpio_request(pdata
->gpio_card_detect
, "MMC detect change");
655 dev_err(&pdev
->dev
, "Failed to request detect change gpio\n");
658 gpio_direction_input(pdata
->gpio_card_detect
);
661 if (gpio_is_valid(pdata
->gpio_read_only
)) {
662 ret
= gpio_request(pdata
->gpio_read_only
, "MMC read only");
664 dev_err(&pdev
->dev
, "Failed to request read only gpio: %d\n", ret
);
665 goto err_free_gpio_card_detect
;
667 gpio_direction_input(pdata
->gpio_read_only
);
670 if (gpio_is_valid(pdata
->gpio_power
)) {
671 ret
= gpio_request(pdata
->gpio_power
, "MMC power");
673 dev_err(&pdev
->dev
, "Failed to request power gpio: %d\n", ret
);
674 goto err_free_gpio_read_only
;
676 gpio_direction_output(pdata
->gpio_power
, pdata
->power_active_low
);
681 err_free_gpio_read_only
:
682 if (gpio_is_valid(pdata
->gpio_read_only
))
683 gpio_free(pdata
->gpio_read_only
);
684 err_free_gpio_card_detect
:
685 if (gpio_is_valid(pdata
->gpio_card_detect
))
686 gpio_free(pdata
->gpio_card_detect
);
691 static void jz4740_mmc_free_gpios(struct platform_device
*pdev
)
693 struct jz4740_mmc_platform_data
*pdata
= pdev
->dev
.platform_data
;
698 if (gpio_is_valid(pdata
->gpio_power
))
699 gpio_free(pdata
->gpio_power
);
700 if (gpio_is_valid(pdata
->gpio_read_only
))
701 gpio_free(pdata
->gpio_read_only
);
702 if (gpio_is_valid(pdata
->gpio_card_detect
))
703 gpio_free(pdata
->gpio_card_detect
);
706 static int __devinit
jz4740_mmc_probe(struct platform_device
* pdev
)
709 struct mmc_host
*mmc
;
710 struct jz4740_mmc_host
*host
;
711 struct jz4740_mmc_platform_data
*pdata
;
713 pdata
= pdev
->dev
.platform_data
;
715 mmc
= mmc_alloc_host(sizeof(struct jz4740_mmc_host
), &pdev
->dev
);
718 dev_err(&pdev
->dev
, "Failed to alloc mmc host structure\n");
722 host
= mmc_priv(mmc
);
724 host
->irq
= platform_get_irq(pdev
, 0);
728 dev_err(&pdev
->dev
, "Failed to get platform irq: %d\n", ret
);
732 host
->clk
= clk_get(&pdev
->dev
, "mmc");
735 dev_err(&pdev
->dev
, "Failed to get mmc clock\n");
739 host
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
743 dev_err(&pdev
->dev
, "Failed to get base platform memory\n");
747 host
->mem
= request_mem_region(host
->mem
->start
, resource_size(host
->mem
),
752 dev_err(&pdev
->dev
, "Failed to request base memory region\n");
756 host
->base
= ioremap_nocache(host
->mem
->start
, resource_size(host
->mem
));
760 dev_err(&pdev
->dev
, "Failed to ioremap base memory\n");
761 goto err_release_mem_region
;
764 if (pdata
&& pdata
->data_1bit
)
765 ret
= jz_gpio_bulk_request(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
) - 3);
767 ret
= jz_gpio_bulk_request(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
));
770 dev_err(&pdev
->dev
, "Failed to request function pins: %d\n", ret
);
774 ret
= jz4740_mmc_request_gpios(pdev
);
776 goto err_gpio_bulk_free
;
778 mmc
->ops
= &jz4740_mmc_ops
;
779 mmc
->f_min
= JZ_MMC_CLK_RATE
/ 128;
780 mmc
->f_max
= JZ_MMC_CLK_RATE
;
781 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
782 mmc
->caps
= (pdata
&& pdata
->data_1bit
) ? 0 : MMC_CAP_4_BIT_DATA
;
783 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
784 mmc
->max_seg_size
= 4096;
785 mmc
->max_phys_segs
= 128;
787 mmc
->max_blk_size
= (1 << 10) - 1;
788 mmc
->max_blk_count
= (1 << 15) - 1;
789 mmc
->max_req_size
= mmc
->max_blk_size
* mmc
->max_blk_count
;
794 host
->max_clock
= JZ_MMC_CLK_RATE
;
795 spin_lock_init(&host
->lock
);
796 host
->irq_mask
= 0xffff;
798 host
->card_detect_irq
= gpio_to_irq(pdata
->gpio_card_detect
);
800 if (host
->card_detect_irq
< 0) {
801 dev_warn(&pdev
->dev
, "Failed to get irq for card detect gpio\n");
803 ret
= request_irq(host
->card_detect_irq
,
804 jz4740_mmc_card_detect_irq
, IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
, "MMC/SD detect changed", host
);
807 dev_err(&pdev
->dev
, "Failed to request card detect irq");
812 ret
= request_threaded_irq(host
->irq
, jz_mmc_irq
, jz_mmc_irq_worker
, IRQF_DISABLED
, "MMC/SD", host
);
814 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
815 goto err_free_card_detect_irq
;
818 jz4740_mmc_reset(host
);
819 jz4740_mmc_clock_disable(host
);
820 setup_timer(&host
->timeout_timer
, jz4740_mmc_timeout
, (unsigned long)host
);
822 platform_set_drvdata(pdev
, host
);
823 ret
= mmc_add_host(mmc
);
826 dev_err(&pdev
->dev
, "Failed to add mmc host: %d\n", ret
);
829 printk("JZ SD/MMC card driver registered\n");
834 free_irq(host
->irq
, host
);
835 err_free_card_detect_irq
:
836 if (host
->card_detect_irq
>= 0)
837 free_irq(host
->card_detect_irq
, host
);
839 jz4740_mmc_free_gpios(pdev
);
841 if (pdata
&& pdata
->data_1bit
)
842 jz_gpio_bulk_free(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
) - 3);
844 jz_gpio_bulk_free(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
));
847 err_release_mem_region
:
848 release_mem_region(host
->mem
->start
, resource_size(host
->mem
));
852 platform_set_drvdata(pdev
, NULL
);
858 static int jz4740_mmc_remove(struct platform_device
*pdev
)
860 struct jz4740_mmc_host
*host
= platform_get_drvdata(pdev
);
861 struct jz4740_mmc_platform_data
*pdata
= host
->pdata
;
863 del_timer_sync(&host
->timeout_timer
);
864 jz4740_mmc_disable_irq(host
, 0xff);
865 jz4740_mmc_reset(host
);
867 mmc_remove_host(host
->mmc
);
869 free_irq(host
->irq
, host
);
870 if (host
->card_detect_irq
>= 0)
871 free_irq(host
->card_detect_irq
, host
);
873 jz4740_mmc_free_gpios(pdev
);
874 if (pdata
&& pdata
->data_1bit
)
875 jz_gpio_bulk_free(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
) - 3);
877 jz_gpio_bulk_free(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
));
880 release_mem_region(host
->mem
->start
, resource_size(host
->mem
));
884 platform_set_drvdata(pdev
, NULL
);
885 mmc_free_host(host
->mmc
);
891 static int jz4740_mmc_suspend(struct device
*dev
)
893 struct jz4740_mmc_host
*host
= dev_get_drvdata(dev
);
894 struct jz4740_mmc_platform_data
*pdata
= host
->pdata
;
896 mmc_suspend_host(host
->mmc
, PMSG_SUSPEND
);
898 if (pdata
&& pdata
->data_1bit
)
899 jz_gpio_bulk_suspend(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
) - 3);
901 jz_gpio_bulk_suspend(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
));
906 static int jz4740_mmc_resume(struct device
*dev
)
908 struct jz4740_mmc_host
*host
= dev_get_drvdata(dev
);
909 struct jz4740_mmc_platform_data
*pdata
= host
->pdata
;
911 if (pdata
&& pdata
->data_1bit
)
912 jz_gpio_bulk_resume(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
) - 3);
914 jz_gpio_bulk_resume(jz4740_mmc_pins
, ARRAY_SIZE(jz4740_mmc_pins
));
916 mmc_resume_host(host
->mmc
);
921 struct dev_pm_ops jz4740_mmc_pm_ops
= {
922 .suspend
= jz4740_mmc_suspend
,
923 .resume
= jz4740_mmc_resume
,
924 .poweroff
= jz4740_mmc_suspend
,
925 .restore
= jz4740_mmc_resume
,
928 #define jz4740_mmc_PM_OPS (&jz4740_mmc_pm_ops)
930 #define jz4740_mmc_PM_OPS NULL
933 static struct platform_driver jz4740_mmc_driver
= {
934 .probe
= jz4740_mmc_probe
,
935 .remove
= jz4740_mmc_remove
,
937 .name
= "jz4740-mmc",
938 .owner
= THIS_MODULE
,
939 .pm
= jz4740_mmc_PM_OPS
,
943 static int __init
jz4740_mmc_init(void) {
944 return platform_driver_register(&jz4740_mmc_driver
);
946 module_init(jz4740_mmc_init
);
948 static void __exit
jz4740_mmc_exit(void) {
949 platform_driver_unregister(&jz4740_mmc_driver
);
951 module_exit(jz4740_mmc_exit
);
953 MODULE_DESCRIPTION("JZ4720/JZ4740 SD/MMC controller driver");
954 MODULE_LICENSE("GPL");
955 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");