2 * tifm_sd.c - TI FlashMedia driver
4 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Special thanks to Brad Campbell for extensive testing of this driver.
15 #include <linux/tifm.h>
16 #include <linux/mmc/host.h>
17 #include <linux/highmem.h>
18 #include <linux/scatterlist.h>
19 #include <linux/module.h>
22 #define DRIVER_NAME "tifm_sd"
23 #define DRIVER_VERSION "0.8"
25 static int no_dma
= 0;
26 static int fixed_timeout
= 0;
27 module_param(no_dma
, bool, 0644);
28 module_param(fixed_timeout
, bool, 0644);
30 /* Constants here are mostly from OMAP5912 datasheet */
31 #define TIFM_MMCSD_RESET 0x0002
32 #define TIFM_MMCSD_CLKMASK 0x03ff
33 #define TIFM_MMCSD_POWER 0x0800
34 #define TIFM_MMCSD_4BBUS 0x8000
35 #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
36 #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
37 #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
38 #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
39 #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
40 #define TIFM_MMCSD_READ 0x8000
42 #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
43 #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
44 #define TIFM_MMCSD_CD 0x0002 /* card detect */
45 #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
46 #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
47 #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
48 #define TIFM_MMCSD_DTO 0x0020 /* data time-out */
49 #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
50 #define TIFM_MMCSD_CTO 0x0080 /* command time-out */
51 #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
52 #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
53 #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
54 #define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
55 #define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
56 #define TIFM_MMCSD_CERR 0x4000 /* card status error */
58 #define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
59 #define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
61 #define TIFM_MMCSD_FIFO_SIZE 0x0020
63 #define TIFM_MMCSD_RSP_R0 0x0000
64 #define TIFM_MMCSD_RSP_R1 0x0100
65 #define TIFM_MMCSD_RSP_R2 0x0200
66 #define TIFM_MMCSD_RSP_R3 0x0300
67 #define TIFM_MMCSD_RSP_R4 0x0400
68 #define TIFM_MMCSD_RSP_R5 0x0500
69 #define TIFM_MMCSD_RSP_R6 0x0600
71 #define TIFM_MMCSD_RSP_BUSY 0x0800
73 #define TIFM_MMCSD_CMD_BC 0x0000
74 #define TIFM_MMCSD_CMD_BCR 0x1000
75 #define TIFM_MMCSD_CMD_AC 0x2000
76 #define TIFM_MMCSD_CMD_ADTC 0x3000
78 #define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
93 unsigned short eject
:1,
96 unsigned short cmd_flags
;
98 unsigned int clk_freq
;
100 unsigned long timeout_jiffies
;
102 struct tasklet_struct finish_tasklet
;
103 struct timer_list timer
;
104 struct mmc_request
*req
;
108 unsigned int block_pos
;
109 struct scatterlist bounce_buf
;
110 unsigned char bounce_buf_data
[TIFM_MMCSD_MAX_BLOCK_SIZE
];
113 /* for some reason, host won't respond correctly to readw/writew */
114 static void tifm_sd_read_fifo(struct tifm_sd
*host
, struct page
*pg
,
115 unsigned int off
, unsigned int cnt
)
117 struct tifm_dev
*sock
= host
->dev
;
119 unsigned int pos
= 0, val
;
121 buf
= kmap_atomic(pg
, KM_BIO_DST_IRQ
) + off
;
122 if (host
->cmd_flags
& DATA_CARRY
) {
123 buf
[pos
++] = host
->bounce_buf_data
[0];
124 host
->cmd_flags
&= ~DATA_CARRY
;
128 val
= readl(sock
->addr
+ SOCK_MMCSD_DATA
);
129 buf
[pos
++] = val
& 0xff;
131 host
->bounce_buf_data
[0] = (val
>> 8) & 0xff;
132 host
->cmd_flags
|= DATA_CARRY
;
135 buf
[pos
++] = (val
>> 8) & 0xff;
137 kunmap_atomic(buf
- off
, KM_BIO_DST_IRQ
);
140 static void tifm_sd_write_fifo(struct tifm_sd
*host
, struct page
*pg
,
141 unsigned int off
, unsigned int cnt
)
143 struct tifm_dev
*sock
= host
->dev
;
145 unsigned int pos
= 0, val
;
147 buf
= kmap_atomic(pg
, KM_BIO_SRC_IRQ
) + off
;
148 if (host
->cmd_flags
& DATA_CARRY
) {
149 val
= host
->bounce_buf_data
[0] | ((buf
[pos
++] << 8) & 0xff00);
150 writel(val
, sock
->addr
+ SOCK_MMCSD_DATA
);
151 host
->cmd_flags
&= ~DATA_CARRY
;
157 host
->bounce_buf_data
[0] = val
& 0xff;
158 host
->cmd_flags
|= DATA_CARRY
;
161 val
|= (buf
[pos
++] << 8) & 0xff00;
162 writel(val
, sock
->addr
+ SOCK_MMCSD_DATA
);
164 kunmap_atomic(buf
- off
, KM_BIO_SRC_IRQ
);
167 static void tifm_sd_transfer_data(struct tifm_sd
*host
)
169 struct mmc_data
*r_data
= host
->req
->cmd
->data
;
170 struct scatterlist
*sg
= r_data
->sg
;
171 unsigned int off
, cnt
, t_size
= TIFM_MMCSD_FIFO_SIZE
* 2;
172 unsigned int p_off
, p_cnt
;
175 if (host
->sg_pos
== host
->sg_len
)
178 cnt
= sg
[host
->sg_pos
].length
- host
->block_pos
;
182 if (host
->sg_pos
== host
->sg_len
) {
183 if ((r_data
->flags
& MMC_DATA_WRITE
)
184 && (host
->cmd_flags
& DATA_CARRY
))
185 writel(host
->bounce_buf_data
[0],
191 cnt
= sg
[host
->sg_pos
].length
;
193 off
= sg
[host
->sg_pos
].offset
+ host
->block_pos
;
195 pg
= nth_page(sg_page(&sg
[host
->sg_pos
]), off
>> PAGE_SHIFT
);
196 p_off
= offset_in_page(off
);
197 p_cnt
= PAGE_SIZE
- p_off
;
198 p_cnt
= min(p_cnt
, cnt
);
199 p_cnt
= min(p_cnt
, t_size
);
201 if (r_data
->flags
& MMC_DATA_READ
)
202 tifm_sd_read_fifo(host
, pg
, p_off
, p_cnt
);
203 else if (r_data
->flags
& MMC_DATA_WRITE
)
204 tifm_sd_write_fifo(host
, pg
, p_off
, p_cnt
);
207 host
->block_pos
+= p_cnt
;
211 static void tifm_sd_copy_page(struct page
*dst
, unsigned int dst_off
,
212 struct page
*src
, unsigned int src_off
,
215 unsigned char *src_buf
= kmap_atomic(src
, KM_BIO_SRC_IRQ
) + src_off
;
216 unsigned char *dst_buf
= kmap_atomic(dst
, KM_BIO_DST_IRQ
) + dst_off
;
218 memcpy(dst_buf
, src_buf
, count
);
220 kunmap_atomic(dst_buf
- dst_off
, KM_BIO_DST_IRQ
);
221 kunmap_atomic(src_buf
- src_off
, KM_BIO_SRC_IRQ
);
224 static void tifm_sd_bounce_block(struct tifm_sd
*host
, struct mmc_data
*r_data
)
226 struct scatterlist
*sg
= r_data
->sg
;
227 unsigned int t_size
= r_data
->blksz
;
228 unsigned int off
, cnt
;
229 unsigned int p_off
, p_cnt
;
232 dev_dbg(&host
->dev
->dev
, "bouncing block\n");
234 cnt
= sg
[host
->sg_pos
].length
- host
->block_pos
;
238 if (host
->sg_pos
== host
->sg_len
)
240 cnt
= sg
[host
->sg_pos
].length
;
242 off
= sg
[host
->sg_pos
].offset
+ host
->block_pos
;
244 pg
= nth_page(sg_page(&sg
[host
->sg_pos
]), off
>> PAGE_SHIFT
);
245 p_off
= offset_in_page(off
);
246 p_cnt
= PAGE_SIZE
- p_off
;
247 p_cnt
= min(p_cnt
, cnt
);
248 p_cnt
= min(p_cnt
, t_size
);
250 if (r_data
->flags
& MMC_DATA_WRITE
)
251 tifm_sd_copy_page(sg_page(&host
->bounce_buf
),
252 r_data
->blksz
- t_size
,
254 else if (r_data
->flags
& MMC_DATA_READ
)
255 tifm_sd_copy_page(pg
, p_off
, sg_page(&host
->bounce_buf
),
256 r_data
->blksz
- t_size
, p_cnt
);
259 host
->block_pos
+= p_cnt
;
263 static int tifm_sd_set_dma_data(struct tifm_sd
*host
, struct mmc_data
*r_data
)
265 struct tifm_dev
*sock
= host
->dev
;
266 unsigned int t_size
= TIFM_DMA_TSIZE
* r_data
->blksz
;
267 unsigned int dma_len
, dma_blk_cnt
, dma_off
;
268 struct scatterlist
*sg
= NULL
;
271 if (host
->sg_pos
== host
->sg_len
)
274 if (host
->cmd_flags
& DATA_CARRY
) {
275 host
->cmd_flags
&= ~DATA_CARRY
;
276 local_irq_save(flags
);
277 tifm_sd_bounce_block(host
, r_data
);
278 local_irq_restore(flags
);
279 if (host
->sg_pos
== host
->sg_len
)
283 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]) - host
->block_pos
;
287 if (host
->sg_pos
== host
->sg_len
)
289 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]);
292 if (dma_len
< t_size
) {
293 dma_blk_cnt
= dma_len
/ r_data
->blksz
;
294 dma_off
= host
->block_pos
;
295 host
->block_pos
+= dma_blk_cnt
* r_data
->blksz
;
297 dma_blk_cnt
= TIFM_DMA_TSIZE
;
298 dma_off
= host
->block_pos
;
299 host
->block_pos
+= t_size
;
303 sg
= &r_data
->sg
[host
->sg_pos
];
305 if (r_data
->flags
& MMC_DATA_WRITE
) {
306 local_irq_save(flags
);
307 tifm_sd_bounce_block(host
, r_data
);
308 local_irq_restore(flags
);
310 host
->cmd_flags
|= DATA_CARRY
;
312 sg
= &host
->bounce_buf
;
318 dev_dbg(&sock
->dev
, "setting dma for %d blocks\n", dma_blk_cnt
);
319 writel(sg_dma_address(sg
) + dma_off
, sock
->addr
+ SOCK_DMA_ADDRESS
);
320 if (r_data
->flags
& MMC_DATA_WRITE
)
321 writel((dma_blk_cnt
<< 8) | TIFM_DMA_TX
| TIFM_DMA_EN
,
322 sock
->addr
+ SOCK_DMA_CONTROL
);
324 writel((dma_blk_cnt
<< 8) | TIFM_DMA_EN
,
325 sock
->addr
+ SOCK_DMA_CONTROL
);
330 static unsigned int tifm_sd_op_flags(struct mmc_command
*cmd
)
334 switch (mmc_resp_type(cmd
)) {
336 rc
|= TIFM_MMCSD_RSP_R0
;
339 rc
|= TIFM_MMCSD_RSP_BUSY
; // deliberate fall-through
341 rc
|= TIFM_MMCSD_RSP_R1
;
344 rc
|= TIFM_MMCSD_RSP_R2
;
347 rc
|= TIFM_MMCSD_RSP_R3
;
353 switch (mmc_cmd_type(cmd
)) {
355 rc
|= TIFM_MMCSD_CMD_BC
;
358 rc
|= TIFM_MMCSD_CMD_BCR
;
361 rc
|= TIFM_MMCSD_CMD_AC
;
364 rc
|= TIFM_MMCSD_CMD_ADTC
;
372 static void tifm_sd_exec(struct tifm_sd
*host
, struct mmc_command
*cmd
)
374 struct tifm_dev
*sock
= host
->dev
;
375 unsigned int cmd_mask
= tifm_sd_op_flags(cmd
);
377 if (host
->open_drain
)
378 cmd_mask
|= TIFM_MMCSD_ODTO
;
380 if (cmd
->data
&& (cmd
->data
->flags
& MMC_DATA_READ
))
381 cmd_mask
|= TIFM_MMCSD_READ
;
383 dev_dbg(&sock
->dev
, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
384 cmd
->opcode
, cmd
->arg
, cmd_mask
);
386 writel((cmd
->arg
>> 16) & 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_HIGH
);
387 writel(cmd
->arg
& 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_LOW
);
388 writel(cmd
->opcode
| cmd_mask
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
391 static void tifm_sd_fetch_resp(struct mmc_command
*cmd
, struct tifm_dev
*sock
)
393 cmd
->resp
[0] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x1c) << 16)
394 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x18);
395 cmd
->resp
[1] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x14) << 16)
396 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x10);
397 cmd
->resp
[2] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x0c) << 16)
398 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x08);
399 cmd
->resp
[3] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x04) << 16)
400 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x00);
403 static void tifm_sd_check_status(struct tifm_sd
*host
)
405 struct tifm_dev
*sock
= host
->dev
;
406 struct mmc_command
*cmd
= host
->req
->cmd
;
411 if (!(host
->cmd_flags
& CMD_READY
))
415 if (cmd
->data
->error
) {
416 if ((host
->cmd_flags
& SCMD_ACTIVE
)
417 && !(host
->cmd_flags
& SCMD_READY
))
423 if (!(host
->cmd_flags
& BRS_READY
))
426 if (!(host
->no_dma
|| (host
->cmd_flags
& FIFO_READY
)))
429 if (cmd
->data
->flags
& MMC_DATA_WRITE
) {
430 if (host
->req
->stop
) {
431 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
432 host
->cmd_flags
|= SCMD_ACTIVE
;
433 writel(TIFM_MMCSD_EOFB
435 + SOCK_MMCSD_INT_ENABLE
),
437 + SOCK_MMCSD_INT_ENABLE
);
438 tifm_sd_exec(host
, host
->req
->stop
);
441 if (!(host
->cmd_flags
& SCMD_READY
)
442 || (host
->cmd_flags
& CARD_BUSY
))
444 writel((~TIFM_MMCSD_EOFB
)
446 + SOCK_MMCSD_INT_ENABLE
),
448 + SOCK_MMCSD_INT_ENABLE
);
451 if (host
->cmd_flags
& CARD_BUSY
)
453 writel((~TIFM_MMCSD_EOFB
)
455 + SOCK_MMCSD_INT_ENABLE
),
456 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
459 if (host
->req
->stop
) {
460 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
461 host
->cmd_flags
|= SCMD_ACTIVE
;
462 tifm_sd_exec(host
, host
->req
->stop
);
465 if (!(host
->cmd_flags
& SCMD_READY
))
472 tasklet_schedule(&host
->finish_tasklet
);
475 /* Called from interrupt handler */
476 static void tifm_sd_data_event(struct tifm_dev
*sock
)
478 struct tifm_sd
*host
;
479 unsigned int fifo_status
= 0;
480 struct mmc_data
*r_data
= NULL
;
482 spin_lock(&sock
->lock
);
483 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
484 fifo_status
= readl(sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
485 dev_dbg(&sock
->dev
, "data event: fifo_status %x, flags %x\n",
486 fifo_status
, host
->cmd_flags
);
489 r_data
= host
->req
->cmd
->data
;
491 if (r_data
&& (fifo_status
& TIFM_FIFO_READY
)) {
492 if (tifm_sd_set_dma_data(host
, r_data
)) {
493 host
->cmd_flags
|= FIFO_READY
;
494 tifm_sd_check_status(host
);
499 writel(fifo_status
, sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
500 spin_unlock(&sock
->lock
);
503 /* Called from interrupt handler */
504 static void tifm_sd_card_event(struct tifm_dev
*sock
)
506 struct tifm_sd
*host
;
507 unsigned int host_status
= 0;
509 struct mmc_command
*cmd
= NULL
;
512 spin_lock(&sock
->lock
);
513 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
514 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
515 dev_dbg(&sock
->dev
, "host event: host_status %x, flags %x\n",
516 host_status
, host
->cmd_flags
);
519 cmd
= host
->req
->cmd
;
521 if (host_status
& TIFM_MMCSD_ERRMASK
) {
522 writel(host_status
& TIFM_MMCSD_ERRMASK
,
523 sock
->addr
+ SOCK_MMCSD_STATUS
);
524 if (host_status
& TIFM_MMCSD_CTO
)
525 cmd_error
= -ETIMEDOUT
;
526 else if (host_status
& TIFM_MMCSD_CCRC
)
530 if (host_status
& TIFM_MMCSD_DTO
)
531 cmd
->data
->error
= -ETIMEDOUT
;
532 else if (host_status
& TIFM_MMCSD_DCRC
)
533 cmd
->data
->error
= -EILSEQ
;
536 writel(TIFM_FIFO_INT_SETALL
,
537 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
538 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
540 if (host
->req
->stop
) {
541 if (host
->cmd_flags
& SCMD_ACTIVE
) {
542 host
->req
->stop
->error
= cmd_error
;
543 host
->cmd_flags
|= SCMD_READY
;
545 cmd
->error
= cmd_error
;
546 host
->cmd_flags
|= SCMD_ACTIVE
;
547 tifm_sd_exec(host
, host
->req
->stop
);
551 cmd
->error
= cmd_error
;
553 if (host_status
& (TIFM_MMCSD_EOC
| TIFM_MMCSD_CERR
)) {
554 if (!(host
->cmd_flags
& CMD_READY
)) {
555 host
->cmd_flags
|= CMD_READY
;
556 tifm_sd_fetch_resp(cmd
, sock
);
557 } else if (host
->cmd_flags
& SCMD_ACTIVE
) {
558 host
->cmd_flags
|= SCMD_READY
;
559 tifm_sd_fetch_resp(host
->req
->stop
,
563 if (host_status
& TIFM_MMCSD_BRS
)
564 host
->cmd_flags
|= BRS_READY
;
567 if (host
->no_dma
&& cmd
->data
) {
568 if (host_status
& TIFM_MMCSD_AE
)
569 writel(host_status
& TIFM_MMCSD_AE
,
570 sock
->addr
+ SOCK_MMCSD_STATUS
);
572 if (host_status
& (TIFM_MMCSD_AE
| TIFM_MMCSD_AF
574 local_irq_save(flags
);
575 tifm_sd_transfer_data(host
);
576 local_irq_restore(flags
);
577 host_status
&= ~TIFM_MMCSD_AE
;
581 if (host_status
& TIFM_MMCSD_EOFB
)
582 host
->cmd_flags
&= ~CARD_BUSY
;
583 else if (host_status
& TIFM_MMCSD_CB
)
584 host
->cmd_flags
|= CARD_BUSY
;
586 tifm_sd_check_status(host
);
589 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
590 spin_unlock(&sock
->lock
);
593 static void tifm_sd_set_data_timeout(struct tifm_sd
*host
,
594 struct mmc_data
*data
)
596 struct tifm_dev
*sock
= host
->dev
;
597 unsigned int data_timeout
= data
->timeout_clks
;
602 data_timeout
+= data
->timeout_ns
/
603 ((1000000000UL / host
->clk_freq
) * host
->clk_div
);
605 if (data_timeout
< 0xffff) {
606 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
607 writel((~TIFM_MMCSD_DPE
)
608 & readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
609 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
611 data_timeout
= (data_timeout
>> 10) + 1;
612 if (data_timeout
> 0xffff)
613 data_timeout
= 0; /* set to unlimited */
614 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
615 writel(TIFM_MMCSD_DPE
616 | readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
617 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
621 static void tifm_sd_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
623 struct tifm_sd
*host
= mmc_priv(mmc
);
624 struct tifm_dev
*sock
= host
->dev
;
626 struct mmc_data
*r_data
= mrq
->cmd
->data
;
628 spin_lock_irqsave(&sock
->lock
, flags
);
630 mrq
->cmd
->error
= -ENOMEDIUM
;
635 printk(KERN_ERR
"%s : unfinished request detected\n",
636 dev_name(&sock
->dev
));
637 mrq
->cmd
->error
= -ETIMEDOUT
;
645 if (mrq
->data
&& !is_power_of_2(mrq
->data
->blksz
))
648 host
->no_dma
= no_dma
? 1 : 0;
651 tifm_sd_set_data_timeout(host
, r_data
);
653 if ((r_data
->flags
& MMC_DATA_WRITE
) && !mrq
->stop
)
654 writel(TIFM_MMCSD_EOFB
655 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
656 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
659 writel(TIFM_MMCSD_BUFINT
660 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
661 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
662 writel(((TIFM_MMCSD_FIFO_SIZE
- 1) << 8)
663 | (TIFM_MMCSD_FIFO_SIZE
- 1),
664 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
666 host
->sg_len
= r_data
->sg_len
;
668 sg_init_one(&host
->bounce_buf
, host
->bounce_buf_data
,
671 if(1 != tifm_map_sg(sock
, &host
->bounce_buf
, 1,
672 r_data
->flags
& MMC_DATA_WRITE
674 : PCI_DMA_FROMDEVICE
)) {
675 printk(KERN_ERR
"%s : scatterlist map failed\n",
676 dev_name(&sock
->dev
));
677 mrq
->cmd
->error
= -ENOMEM
;
680 host
->sg_len
= tifm_map_sg(sock
, r_data
->sg
,
685 : PCI_DMA_FROMDEVICE
);
686 if (host
->sg_len
< 1) {
687 printk(KERN_ERR
"%s : scatterlist map failed\n",
688 dev_name(&sock
->dev
));
689 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
690 r_data
->flags
& MMC_DATA_WRITE
692 : PCI_DMA_FROMDEVICE
);
693 mrq
->cmd
->error
= -ENOMEM
;
697 writel(TIFM_FIFO_INT_SETALL
,
698 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
699 writel(ilog2(r_data
->blksz
) - 2,
700 sock
->addr
+ SOCK_FIFO_PAGE_SIZE
);
701 writel(TIFM_FIFO_ENABLE
,
702 sock
->addr
+ SOCK_FIFO_CONTROL
);
703 writel(TIFM_FIFO_INTMASK
,
704 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
706 if (r_data
->flags
& MMC_DATA_WRITE
)
707 writel(TIFM_MMCSD_TXDE
,
708 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
710 writel(TIFM_MMCSD_RXDE
,
711 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
713 tifm_sd_set_dma_data(host
, r_data
);
716 writel(r_data
->blocks
- 1,
717 sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
718 writel(r_data
->blksz
- 1,
719 sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
);
723 mod_timer(&host
->timer
, jiffies
+ host
->timeout_jiffies
);
724 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
725 sock
->addr
+ SOCK_CONTROL
);
726 tifm_sd_exec(host
, mrq
->cmd
);
727 spin_unlock_irqrestore(&sock
->lock
, flags
);
731 spin_unlock_irqrestore(&sock
->lock
, flags
);
732 mmc_request_done(mmc
, mrq
);
735 static void tifm_sd_end_cmd(unsigned long data
)
737 struct tifm_sd
*host
= (struct tifm_sd
*)data
;
738 struct tifm_dev
*sock
= host
->dev
;
739 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
740 struct mmc_request
*mrq
;
741 struct mmc_data
*r_data
= NULL
;
744 spin_lock_irqsave(&sock
->lock
, flags
);
746 del_timer(&host
->timer
);
751 printk(KERN_ERR
" %s : no request to complete?\n",
752 dev_name(&sock
->dev
));
753 spin_unlock_irqrestore(&sock
->lock
, flags
);
757 r_data
= mrq
->cmd
->data
;
760 writel((~TIFM_MMCSD_BUFINT
)
761 & readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
762 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
764 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
765 (r_data
->flags
& MMC_DATA_WRITE
)
766 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
767 tifm_unmap_sg(sock
, r_data
->sg
, r_data
->sg_len
,
768 (r_data
->flags
& MMC_DATA_WRITE
)
769 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
772 r_data
->bytes_xfered
= r_data
->blocks
773 - readl(sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
) - 1;
774 r_data
->bytes_xfered
*= r_data
->blksz
;
775 r_data
->bytes_xfered
+= r_data
->blksz
776 - readl(sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
) + 1;
779 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
780 sock
->addr
+ SOCK_CONTROL
);
782 spin_unlock_irqrestore(&sock
->lock
, flags
);
783 mmc_request_done(mmc
, mrq
);
786 static void tifm_sd_abort(unsigned long data
)
788 struct tifm_sd
*host
= (struct tifm_sd
*)data
;
791 "%s : card failed to respond for a long period of time "
793 dev_name(&host
->dev
->dev
), host
->req
->cmd
->opcode
, host
->cmd_flags
);
795 tifm_eject(host
->dev
);
798 static void tifm_sd_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
800 struct tifm_sd
*host
= mmc_priv(mmc
);
801 struct tifm_dev
*sock
= host
->dev
;
802 unsigned int clk_div1
, clk_div2
;
805 spin_lock_irqsave(&sock
->lock
, flags
);
807 dev_dbg(&sock
->dev
, "ios: clock = %u, vdd = %x, bus_mode = %x, "
808 "chip_select = %x, power_mode = %x, bus_width = %x\n",
809 ios
->clock
, ios
->vdd
, ios
->bus_mode
, ios
->chip_select
,
810 ios
->power_mode
, ios
->bus_width
);
812 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
813 writel(TIFM_MMCSD_4BBUS
| readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
814 sock
->addr
+ SOCK_MMCSD_CONFIG
);
816 writel((~TIFM_MMCSD_4BBUS
)
817 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
818 sock
->addr
+ SOCK_MMCSD_CONFIG
);
822 clk_div1
= 20000000 / ios
->clock
;
826 clk_div2
= 24000000 / ios
->clock
;
830 if ((20000000 / clk_div1
) > ios
->clock
)
832 if ((24000000 / clk_div2
) > ios
->clock
)
834 if ((20000000 / clk_div1
) > (24000000 / clk_div2
)) {
835 host
->clk_freq
= 20000000;
836 host
->clk_div
= clk_div1
;
837 writel((~TIFM_CTRL_FAST_CLK
)
838 & readl(sock
->addr
+ SOCK_CONTROL
),
839 sock
->addr
+ SOCK_CONTROL
);
841 host
->clk_freq
= 24000000;
842 host
->clk_div
= clk_div2
;
843 writel(TIFM_CTRL_FAST_CLK
844 | readl(sock
->addr
+ SOCK_CONTROL
),
845 sock
->addr
+ SOCK_CONTROL
);
850 host
->clk_div
&= TIFM_MMCSD_CLKMASK
;
852 | ((~TIFM_MMCSD_CLKMASK
)
853 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
)),
854 sock
->addr
+ SOCK_MMCSD_CONFIG
);
856 host
->open_drain
= (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
);
858 /* chip_select : maybe later */
860 //power is set before probe / after remove
862 spin_unlock_irqrestore(&sock
->lock
, flags
);
865 static int tifm_sd_ro(struct mmc_host
*mmc
)
868 struct tifm_sd
*host
= mmc_priv(mmc
);
869 struct tifm_dev
*sock
= host
->dev
;
872 spin_lock_irqsave(&sock
->lock
, flags
);
873 if (TIFM_MMCSD_CARD_RO
& readl(sock
->addr
+ SOCK_PRESENT_STATE
))
875 spin_unlock_irqrestore(&sock
->lock
, flags
);
879 static const struct mmc_host_ops tifm_sd_ops
= {
880 .request
= tifm_sd_request
,
881 .set_ios
= tifm_sd_ios
,
885 static int tifm_sd_initialize_host(struct tifm_sd
*host
)
888 unsigned int host_status
= 0;
889 struct tifm_dev
*sock
= host
->dev
;
891 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
894 host
->clk_freq
= 20000000;
895 writel(TIFM_MMCSD_RESET
, sock
->addr
+ SOCK_MMCSD_SYSTEM_CONTROL
);
896 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
897 sock
->addr
+ SOCK_MMCSD_CONFIG
);
899 /* wait up to 0.51 sec for reset */
900 for (rc
= 32; rc
<= 256; rc
<<= 1) {
901 if (1 & readl(sock
->addr
+ SOCK_MMCSD_SYSTEM_STATUS
)) {
909 printk(KERN_ERR
"%s : controller failed to reset\n",
910 dev_name(&sock
->dev
));
914 writel(0, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
915 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
916 sock
->addr
+ SOCK_MMCSD_CONFIG
);
917 writel(TIFM_MMCSD_RXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
919 // command timeout fixed to 64 clocks for now
920 writel(64, sock
->addr
+ SOCK_MMCSD_COMMAND_TO
);
921 writel(TIFM_MMCSD_INAB
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
923 for (rc
= 16; rc
<= 64; rc
<<= 1) {
924 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
925 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
926 if (!(host_status
& TIFM_MMCSD_ERRMASK
)
927 && (host_status
& TIFM_MMCSD_EOC
)) {
936 "%s : card not ready - probe failed on initialization\n",
937 dev_name(&sock
->dev
));
941 writel(TIFM_MMCSD_CERR
| TIFM_MMCSD_BRS
| TIFM_MMCSD_EOC
942 | TIFM_MMCSD_ERRMASK
,
943 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
949 static int tifm_sd_probe(struct tifm_dev
*sock
)
951 struct mmc_host
*mmc
;
952 struct tifm_sd
*host
;
955 if (!(TIFM_SOCK_STATE_OCCUPIED
956 & readl(sock
->addr
+ SOCK_PRESENT_STATE
))) {
957 printk(KERN_WARNING
"%s : card gone, unexpectedly\n",
958 dev_name(&sock
->dev
));
962 mmc
= mmc_alloc_host(sizeof(struct tifm_sd
), &sock
->dev
);
966 host
= mmc_priv(mmc
);
967 tifm_set_drvdata(sock
, mmc
);
969 host
->timeout_jiffies
= msecs_to_jiffies(1000);
971 tasklet_init(&host
->finish_tasklet
, tifm_sd_end_cmd
,
972 (unsigned long)host
);
973 setup_timer(&host
->timer
, tifm_sd_abort
, (unsigned long)host
);
975 mmc
->ops
= &tifm_sd_ops
;
976 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
977 mmc
->caps
= MMC_CAP_4_BIT_DATA
;
978 mmc
->f_min
= 20000000 / 60;
979 mmc
->f_max
= 24000000;
981 mmc
->max_blk_count
= 2048;
982 mmc
->max_segs
= mmc
->max_blk_count
;
983 mmc
->max_blk_size
= min(TIFM_MMCSD_MAX_BLOCK_SIZE
, PAGE_SIZE
);
984 mmc
->max_seg_size
= mmc
->max_blk_count
* mmc
->max_blk_size
;
985 mmc
->max_req_size
= mmc
->max_seg_size
;
987 sock
->card_event
= tifm_sd_card_event
;
988 sock
->data_event
= tifm_sd_data_event
;
989 rc
= tifm_sd_initialize_host(host
);
992 rc
= mmc_add_host(mmc
);
1000 static void tifm_sd_remove(struct tifm_dev
*sock
)
1002 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
1003 struct tifm_sd
*host
= mmc_priv(mmc
);
1004 unsigned long flags
;
1006 spin_lock_irqsave(&sock
->lock
, flags
);
1008 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
1010 spin_unlock_irqrestore(&sock
->lock
, flags
);
1012 tasklet_kill(&host
->finish_tasklet
);
1014 spin_lock_irqsave(&sock
->lock
, flags
);
1016 writel(TIFM_FIFO_INT_SETALL
,
1017 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
1018 writel(0, sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
1019 host
->req
->cmd
->error
= -ENOMEDIUM
;
1020 if (host
->req
->stop
)
1021 host
->req
->stop
->error
= -ENOMEDIUM
;
1022 tasklet_schedule(&host
->finish_tasklet
);
1024 spin_unlock_irqrestore(&sock
->lock
, flags
);
1025 mmc_remove_host(mmc
);
1026 dev_dbg(&sock
->dev
, "after remove\n");
1033 static int tifm_sd_suspend(struct tifm_dev
*sock
, pm_message_t state
)
1035 return mmc_suspend_host(tifm_get_drvdata(sock
));
1038 static int tifm_sd_resume(struct tifm_dev
*sock
)
1040 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
1041 struct tifm_sd
*host
= mmc_priv(mmc
);
1044 rc
= tifm_sd_initialize_host(host
);
1045 dev_dbg(&sock
->dev
, "resume initialize %d\n", rc
);
1050 rc
= mmc_resume_host(mmc
);
1057 #define tifm_sd_suspend NULL
1058 #define tifm_sd_resume NULL
1060 #endif /* CONFIG_PM */
1062 static struct tifm_device_id tifm_sd_id_tbl
[] = {
1063 { TIFM_TYPE_SD
}, { }
1066 static struct tifm_driver tifm_sd_driver
= {
1068 .name
= DRIVER_NAME
,
1069 .owner
= THIS_MODULE
1071 .id_table
= tifm_sd_id_tbl
,
1072 .probe
= tifm_sd_probe
,
1073 .remove
= tifm_sd_remove
,
1074 .suspend
= tifm_sd_suspend
,
1075 .resume
= tifm_sd_resume
1078 static int __init
tifm_sd_init(void)
1080 return tifm_register_driver(&tifm_sd_driver
);
1083 static void __exit
tifm_sd_exit(void)
1085 tifm_unregister_driver(&tifm_sd_driver
);
1088 MODULE_AUTHOR("Alex Dubov");
1089 MODULE_DESCRIPTION("TI FlashMedia SD driver");
1090 MODULE_LICENSE("GPL");
1091 MODULE_DEVICE_TABLE(tifm
, tifm_sd_id_tbl
);
1092 MODULE_VERSION(DRIVER_VERSION
);
1094 module_init(tifm_sd_init
);
1095 module_exit(tifm_sd_exit
);