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>
21 #define DRIVER_NAME "tifm_sd"
22 #define DRIVER_VERSION "0.8"
24 static int no_dma
= 0;
25 static int fixed_timeout
= 0;
26 module_param(no_dma
, bool, 0644);
27 module_param(fixed_timeout
, bool, 0644);
29 /* Constants here are mostly from OMAP5912 datasheet */
30 #define TIFM_MMCSD_RESET 0x0002
31 #define TIFM_MMCSD_CLKMASK 0x03ff
32 #define TIFM_MMCSD_POWER 0x0800
33 #define TIFM_MMCSD_4BBUS 0x8000
34 #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
35 #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
36 #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
37 #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
38 #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
39 #define TIFM_MMCSD_READ 0x8000
41 #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
42 #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
43 #define TIFM_MMCSD_CD 0x0002 /* card detect */
44 #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
45 #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
46 #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
47 #define TIFM_MMCSD_DTO 0x0020 /* data time-out */
48 #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
49 #define TIFM_MMCSD_CTO 0x0080 /* command time-out */
50 #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
51 #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
52 #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
53 #define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
54 #define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
55 #define TIFM_MMCSD_CERR 0x4000 /* card status error */
57 #define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
58 #define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
60 #define TIFM_MMCSD_FIFO_SIZE 0x0020
62 #define TIFM_MMCSD_RSP_R0 0x0000
63 #define TIFM_MMCSD_RSP_R1 0x0100
64 #define TIFM_MMCSD_RSP_R2 0x0200
65 #define TIFM_MMCSD_RSP_R3 0x0300
66 #define TIFM_MMCSD_RSP_R4 0x0400
67 #define TIFM_MMCSD_RSP_R5 0x0500
68 #define TIFM_MMCSD_RSP_R6 0x0600
70 #define TIFM_MMCSD_RSP_BUSY 0x0800
72 #define TIFM_MMCSD_CMD_BC 0x0000
73 #define TIFM_MMCSD_CMD_BCR 0x1000
74 #define TIFM_MMCSD_CMD_AC 0x2000
75 #define TIFM_MMCSD_CMD_ADTC 0x3000
77 #define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
92 unsigned short eject
:1,
95 unsigned short cmd_flags
;
97 unsigned int clk_freq
;
99 unsigned long timeout_jiffies
;
101 struct tasklet_struct finish_tasklet
;
102 struct timer_list timer
;
103 struct mmc_request
*req
;
107 unsigned int block_pos
;
108 struct scatterlist bounce_buf
;
109 unsigned char bounce_buf_data
[TIFM_MMCSD_MAX_BLOCK_SIZE
];
112 /* for some reason, host won't respond correctly to readw/writew */
113 static void tifm_sd_read_fifo(struct tifm_sd
*host
, struct page
*pg
,
114 unsigned int off
, unsigned int cnt
)
116 struct tifm_dev
*sock
= host
->dev
;
118 unsigned int pos
= 0, val
;
120 buf
= kmap_atomic(pg
, KM_BIO_DST_IRQ
) + off
;
121 if (host
->cmd_flags
& DATA_CARRY
) {
122 buf
[pos
++] = host
->bounce_buf_data
[0];
123 host
->cmd_flags
&= ~DATA_CARRY
;
127 val
= readl(sock
->addr
+ SOCK_MMCSD_DATA
);
128 buf
[pos
++] = val
& 0xff;
130 host
->bounce_buf_data
[0] = (val
>> 8) & 0xff;
131 host
->cmd_flags
|= DATA_CARRY
;
134 buf
[pos
++] = (val
>> 8) & 0xff;
136 kunmap_atomic(buf
- off
, KM_BIO_DST_IRQ
);
139 static void tifm_sd_write_fifo(struct tifm_sd
*host
, struct page
*pg
,
140 unsigned int off
, unsigned int cnt
)
142 struct tifm_dev
*sock
= host
->dev
;
144 unsigned int pos
= 0, val
;
146 buf
= kmap_atomic(pg
, KM_BIO_SRC_IRQ
) + off
;
147 if (host
->cmd_flags
& DATA_CARRY
) {
148 val
= host
->bounce_buf_data
[0] | ((buf
[pos
++] << 8) & 0xff00);
149 writel(val
, sock
->addr
+ SOCK_MMCSD_DATA
);
150 host
->cmd_flags
&= ~DATA_CARRY
;
156 host
->bounce_buf_data
[0] = val
& 0xff;
157 host
->cmd_flags
|= DATA_CARRY
;
160 val
|= (buf
[pos
++] << 8) & 0xff00;
161 writel(val
, sock
->addr
+ SOCK_MMCSD_DATA
);
163 kunmap_atomic(buf
- off
, KM_BIO_SRC_IRQ
);
166 static void tifm_sd_transfer_data(struct tifm_sd
*host
)
168 struct mmc_data
*r_data
= host
->req
->cmd
->data
;
169 struct scatterlist
*sg
= r_data
->sg
;
170 unsigned int off
, cnt
, t_size
= TIFM_MMCSD_FIFO_SIZE
* 2;
171 unsigned int p_off
, p_cnt
;
174 if (host
->sg_pos
== host
->sg_len
)
177 cnt
= sg
[host
->sg_pos
].length
- host
->block_pos
;
181 if (host
->sg_pos
== host
->sg_len
) {
182 if ((r_data
->flags
& MMC_DATA_WRITE
)
184 writel(host
->bounce_buf_data
[0],
190 cnt
= sg
[host
->sg_pos
].length
;
192 off
= sg
[host
->sg_pos
].offset
+ host
->block_pos
;
194 pg
= nth_page(sg_page(&sg
[host
->sg_pos
]), off
>> PAGE_SHIFT
);
195 p_off
= offset_in_page(off
);
196 p_cnt
= PAGE_SIZE
- p_off
;
197 p_cnt
= min(p_cnt
, cnt
);
198 p_cnt
= min(p_cnt
, t_size
);
200 if (r_data
->flags
& MMC_DATA_READ
)
201 tifm_sd_read_fifo(host
, pg
, p_off
, p_cnt
);
202 else if (r_data
->flags
& MMC_DATA_WRITE
)
203 tifm_sd_write_fifo(host
, pg
, p_off
, p_cnt
);
206 host
->block_pos
+= p_cnt
;
210 static void tifm_sd_copy_page(struct page
*dst
, unsigned int dst_off
,
211 struct page
*src
, unsigned int src_off
,
214 unsigned char *src_buf
= kmap_atomic(src
, KM_BIO_SRC_IRQ
) + src_off
;
215 unsigned char *dst_buf
= kmap_atomic(dst
, KM_BIO_DST_IRQ
) + dst_off
;
217 memcpy(dst_buf
, src_buf
, count
);
219 kunmap_atomic(dst_buf
- dst_off
, KM_BIO_DST_IRQ
);
220 kunmap_atomic(src_buf
- src_off
, KM_BIO_SRC_IRQ
);
223 static void tifm_sd_bounce_block(struct tifm_sd
*host
, struct mmc_data
*r_data
)
225 struct scatterlist
*sg
= r_data
->sg
;
226 unsigned int t_size
= r_data
->blksz
;
227 unsigned int off
, cnt
;
228 unsigned int p_off
, p_cnt
;
231 dev_dbg(&host
->dev
->dev
, "bouncing block\n");
233 cnt
= sg
[host
->sg_pos
].length
- host
->block_pos
;
237 if (host
->sg_pos
== host
->sg_len
)
239 cnt
= sg
[host
->sg_pos
].length
;
241 off
= sg
[host
->sg_pos
].offset
+ host
->block_pos
;
243 pg
= nth_page(sg_page(&sg
[host
->sg_pos
]), off
>> PAGE_SHIFT
);
244 p_off
= offset_in_page(off
);
245 p_cnt
= PAGE_SIZE
- p_off
;
246 p_cnt
= min(p_cnt
, cnt
);
247 p_cnt
= min(p_cnt
, t_size
);
249 if (r_data
->flags
& MMC_DATA_WRITE
)
250 tifm_sd_copy_page(sg_page(&host
->bounce_buf
),
251 r_data
->blksz
- t_size
,
253 else if (r_data
->flags
& MMC_DATA_READ
)
254 tifm_sd_copy_page(pg
, p_off
, sg_page(&host
->bounce_buf
),
255 r_data
->blksz
- t_size
, p_cnt
);
258 host
->block_pos
+= p_cnt
;
262 static int tifm_sd_set_dma_data(struct tifm_sd
*host
, struct mmc_data
*r_data
)
264 struct tifm_dev
*sock
= host
->dev
;
265 unsigned int t_size
= TIFM_DMA_TSIZE
* r_data
->blksz
;
266 unsigned int dma_len
, dma_blk_cnt
, dma_off
;
267 struct scatterlist
*sg
= NULL
;
270 if (host
->sg_pos
== host
->sg_len
)
273 if (host
->cmd_flags
& DATA_CARRY
) {
274 host
->cmd_flags
&= ~DATA_CARRY
;
275 local_irq_save(flags
);
276 tifm_sd_bounce_block(host
, r_data
);
277 local_irq_restore(flags
);
278 if (host
->sg_pos
== host
->sg_len
)
282 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]) - host
->block_pos
;
286 if (host
->sg_pos
== host
->sg_len
)
288 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]);
291 if (dma_len
< t_size
) {
292 dma_blk_cnt
= dma_len
/ r_data
->blksz
;
293 dma_off
= host
->block_pos
;
294 host
->block_pos
+= dma_blk_cnt
* r_data
->blksz
;
296 dma_blk_cnt
= TIFM_DMA_TSIZE
;
297 dma_off
= host
->block_pos
;
298 host
->block_pos
+= t_size
;
302 sg
= &r_data
->sg
[host
->sg_pos
];
304 if (r_data
->flags
& MMC_DATA_WRITE
) {
305 local_irq_save(flags
);
306 tifm_sd_bounce_block(host
, r_data
);
307 local_irq_restore(flags
);
309 host
->cmd_flags
|= DATA_CARRY
;
311 sg
= &host
->bounce_buf
;
317 dev_dbg(&sock
->dev
, "setting dma for %d blocks\n", dma_blk_cnt
);
318 writel(sg_dma_address(sg
) + dma_off
, sock
->addr
+ SOCK_DMA_ADDRESS
);
319 if (r_data
->flags
& MMC_DATA_WRITE
)
320 writel((dma_blk_cnt
<< 8) | TIFM_DMA_TX
| TIFM_DMA_EN
,
321 sock
->addr
+ SOCK_DMA_CONTROL
);
323 writel((dma_blk_cnt
<< 8) | TIFM_DMA_EN
,
324 sock
->addr
+ SOCK_DMA_CONTROL
);
329 static unsigned int tifm_sd_op_flags(struct mmc_command
*cmd
)
333 switch (mmc_resp_type(cmd
)) {
335 rc
|= TIFM_MMCSD_RSP_R0
;
338 rc
|= TIFM_MMCSD_RSP_BUSY
; // deliberate fall-through
340 rc
|= TIFM_MMCSD_RSP_R1
;
343 rc
|= TIFM_MMCSD_RSP_R2
;
346 rc
|= TIFM_MMCSD_RSP_R3
;
352 switch (mmc_cmd_type(cmd
)) {
354 rc
|= TIFM_MMCSD_CMD_BC
;
357 rc
|= TIFM_MMCSD_CMD_BCR
;
360 rc
|= TIFM_MMCSD_CMD_AC
;
363 rc
|= TIFM_MMCSD_CMD_ADTC
;
371 static void tifm_sd_exec(struct tifm_sd
*host
, struct mmc_command
*cmd
)
373 struct tifm_dev
*sock
= host
->dev
;
374 unsigned int cmd_mask
= tifm_sd_op_flags(cmd
);
376 if (host
->open_drain
)
377 cmd_mask
|= TIFM_MMCSD_ODTO
;
379 if (cmd
->data
&& (cmd
->data
->flags
& MMC_DATA_READ
))
380 cmd_mask
|= TIFM_MMCSD_READ
;
382 dev_dbg(&sock
->dev
, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
383 cmd
->opcode
, cmd
->arg
, cmd_mask
);
385 writel((cmd
->arg
>> 16) & 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_HIGH
);
386 writel(cmd
->arg
& 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_LOW
);
387 writel(cmd
->opcode
| cmd_mask
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
390 static void tifm_sd_fetch_resp(struct mmc_command
*cmd
, struct tifm_dev
*sock
)
392 cmd
->resp
[0] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x1c) << 16)
393 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x18);
394 cmd
->resp
[1] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x14) << 16)
395 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x10);
396 cmd
->resp
[2] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x0c) << 16)
397 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x08);
398 cmd
->resp
[3] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x04) << 16)
399 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x00);
402 static void tifm_sd_check_status(struct tifm_sd
*host
)
404 struct tifm_dev
*sock
= host
->dev
;
405 struct mmc_command
*cmd
= host
->req
->cmd
;
410 if (!(host
->cmd_flags
& CMD_READY
))
414 if (cmd
->data
->error
) {
415 if ((host
->cmd_flags
& SCMD_ACTIVE
)
416 && !(host
->cmd_flags
& SCMD_READY
))
422 if (!(host
->cmd_flags
& BRS_READY
))
425 if (!(host
->no_dma
|| (host
->cmd_flags
& FIFO_READY
)))
428 if (cmd
->data
->flags
& MMC_DATA_WRITE
) {
429 if (host
->req
->stop
) {
430 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
431 host
->cmd_flags
|= SCMD_ACTIVE
;
432 writel(TIFM_MMCSD_EOFB
434 + SOCK_MMCSD_INT_ENABLE
),
436 + SOCK_MMCSD_INT_ENABLE
);
437 tifm_sd_exec(host
, host
->req
->stop
);
440 if (!(host
->cmd_flags
& SCMD_READY
)
441 || (host
->cmd_flags
& CARD_BUSY
))
443 writel((~TIFM_MMCSD_EOFB
)
445 + SOCK_MMCSD_INT_ENABLE
),
447 + SOCK_MMCSD_INT_ENABLE
);
450 if (host
->cmd_flags
& CARD_BUSY
)
452 writel((~TIFM_MMCSD_EOFB
)
454 + SOCK_MMCSD_INT_ENABLE
),
455 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
458 if (host
->req
->stop
) {
459 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
460 host
->cmd_flags
|= SCMD_ACTIVE
;
461 tifm_sd_exec(host
, host
->req
->stop
);
464 if (!(host
->cmd_flags
& SCMD_READY
))
471 tasklet_schedule(&host
->finish_tasklet
);
474 /* Called from interrupt handler */
475 static void tifm_sd_data_event(struct tifm_dev
*sock
)
477 struct tifm_sd
*host
;
478 unsigned int fifo_status
= 0;
479 struct mmc_data
*r_data
= NULL
;
481 spin_lock(&sock
->lock
);
482 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
483 fifo_status
= readl(sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
484 dev_dbg(&sock
->dev
, "data event: fifo_status %x, flags %x\n",
485 fifo_status
, host
->cmd_flags
);
488 r_data
= host
->req
->cmd
->data
;
490 if (r_data
&& (fifo_status
& TIFM_FIFO_READY
)) {
491 if (tifm_sd_set_dma_data(host
, r_data
)) {
492 host
->cmd_flags
|= FIFO_READY
;
493 tifm_sd_check_status(host
);
498 writel(fifo_status
, sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
499 spin_unlock(&sock
->lock
);
502 /* Called from interrupt handler */
503 static void tifm_sd_card_event(struct tifm_dev
*sock
)
505 struct tifm_sd
*host
;
506 unsigned int host_status
= 0;
508 struct mmc_command
*cmd
= NULL
;
511 spin_lock(&sock
->lock
);
512 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
513 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
514 dev_dbg(&sock
->dev
, "host event: host_status %x, flags %x\n",
515 host_status
, host
->cmd_flags
);
518 cmd
= host
->req
->cmd
;
520 if (host_status
& TIFM_MMCSD_ERRMASK
) {
521 writel(host_status
& TIFM_MMCSD_ERRMASK
,
522 sock
->addr
+ SOCK_MMCSD_STATUS
);
523 if (host_status
& TIFM_MMCSD_CTO
)
524 cmd_error
= -ETIMEDOUT
;
525 else if (host_status
& TIFM_MMCSD_CCRC
)
529 if (host_status
& TIFM_MMCSD_DTO
)
530 cmd
->data
->error
= -ETIMEDOUT
;
531 else if (host_status
& TIFM_MMCSD_DCRC
)
532 cmd
->data
->error
= -EILSEQ
;
535 writel(TIFM_FIFO_INT_SETALL
,
536 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
537 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
539 if (host
->req
->stop
) {
540 if (host
->cmd_flags
& SCMD_ACTIVE
) {
541 host
->req
->stop
->error
= cmd_error
;
542 host
->cmd_flags
|= SCMD_READY
;
544 cmd
->error
= cmd_error
;
545 host
->cmd_flags
|= SCMD_ACTIVE
;
546 tifm_sd_exec(host
, host
->req
->stop
);
550 cmd
->error
= cmd_error
;
552 if (host_status
& (TIFM_MMCSD_EOC
| TIFM_MMCSD_CERR
)) {
553 if (!(host
->cmd_flags
& CMD_READY
)) {
554 host
->cmd_flags
|= CMD_READY
;
555 tifm_sd_fetch_resp(cmd
, sock
);
556 } else if (host
->cmd_flags
& SCMD_ACTIVE
) {
557 host
->cmd_flags
|= SCMD_READY
;
558 tifm_sd_fetch_resp(host
->req
->stop
,
562 if (host_status
& TIFM_MMCSD_BRS
)
563 host
->cmd_flags
|= BRS_READY
;
566 if (host
->no_dma
&& cmd
->data
) {
567 if (host_status
& TIFM_MMCSD_AE
)
568 writel(host_status
& TIFM_MMCSD_AE
,
569 sock
->addr
+ SOCK_MMCSD_STATUS
);
571 if (host_status
& (TIFM_MMCSD_AE
| TIFM_MMCSD_AF
573 local_irq_save(flags
);
574 tifm_sd_transfer_data(host
);
575 local_irq_restore(flags
);
576 host_status
&= ~TIFM_MMCSD_AE
;
580 if (host_status
& TIFM_MMCSD_EOFB
)
581 host
->cmd_flags
&= ~CARD_BUSY
;
582 else if (host_status
& TIFM_MMCSD_CB
)
583 host
->cmd_flags
|= CARD_BUSY
;
585 tifm_sd_check_status(host
);
588 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
589 spin_unlock(&sock
->lock
);
592 static void tifm_sd_set_data_timeout(struct tifm_sd
*host
,
593 struct mmc_data
*data
)
595 struct tifm_dev
*sock
= host
->dev
;
596 unsigned int data_timeout
= data
->timeout_clks
;
601 data_timeout
+= data
->timeout_ns
/
602 ((1000000000UL / host
->clk_freq
) * host
->clk_div
);
604 if (data_timeout
< 0xffff) {
605 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
606 writel((~TIFM_MMCSD_DPE
)
607 & readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
608 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
610 data_timeout
= (data_timeout
>> 10) + 1;
611 if (data_timeout
> 0xffff)
612 data_timeout
= 0; /* set to unlimited */
613 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
614 writel(TIFM_MMCSD_DPE
615 | readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
616 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
620 static void tifm_sd_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
622 struct tifm_sd
*host
= mmc_priv(mmc
);
623 struct tifm_dev
*sock
= host
->dev
;
625 struct mmc_data
*r_data
= mrq
->cmd
->data
;
627 spin_lock_irqsave(&sock
->lock
, flags
);
629 mrq
->cmd
->error
= -ENOMEDIUM
;
634 printk(KERN_ERR
"%s : unfinished request detected\n",
636 mrq
->cmd
->error
= -ETIMEDOUT
;
644 if (mrq
->data
&& !is_power_of_2(mrq
->data
->blksz
))
647 host
->no_dma
= no_dma
? 1 : 0;
650 tifm_sd_set_data_timeout(host
, r_data
);
652 if ((r_data
->flags
& MMC_DATA_WRITE
) && !mrq
->stop
)
653 writel(TIFM_MMCSD_EOFB
654 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
655 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
658 writel(TIFM_MMCSD_BUFINT
659 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
660 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
661 writel(((TIFM_MMCSD_FIFO_SIZE
- 1) << 8)
662 | (TIFM_MMCSD_FIFO_SIZE
- 1),
663 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
665 host
->sg_len
= r_data
->sg_len
;
667 sg_init_one(&host
->bounce_buf
, host
->bounce_buf_data
,
670 if(1 != tifm_map_sg(sock
, &host
->bounce_buf
, 1,
671 r_data
->flags
& MMC_DATA_WRITE
673 : PCI_DMA_FROMDEVICE
)) {
674 printk(KERN_ERR
"%s : scatterlist map failed\n",
676 mrq
->cmd
->error
= -ENOMEM
;
679 host
->sg_len
= tifm_map_sg(sock
, r_data
->sg
,
684 : PCI_DMA_FROMDEVICE
);
685 if (host
->sg_len
< 1) {
686 printk(KERN_ERR
"%s : scatterlist map failed\n",
688 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
689 r_data
->flags
& MMC_DATA_WRITE
691 : PCI_DMA_FROMDEVICE
);
692 mrq
->cmd
->error
= -ENOMEM
;
696 writel(TIFM_FIFO_INT_SETALL
,
697 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
698 writel(ilog2(r_data
->blksz
) - 2,
699 sock
->addr
+ SOCK_FIFO_PAGE_SIZE
);
700 writel(TIFM_FIFO_ENABLE
,
701 sock
->addr
+ SOCK_FIFO_CONTROL
);
702 writel(TIFM_FIFO_INTMASK
,
703 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
705 if (r_data
->flags
& MMC_DATA_WRITE
)
706 writel(TIFM_MMCSD_TXDE
,
707 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
709 writel(TIFM_MMCSD_RXDE
,
710 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
712 tifm_sd_set_dma_data(host
, r_data
);
715 writel(r_data
->blocks
- 1,
716 sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
717 writel(r_data
->blksz
- 1,
718 sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
);
722 mod_timer(&host
->timer
, jiffies
+ host
->timeout_jiffies
);
723 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
724 sock
->addr
+ SOCK_CONTROL
);
725 tifm_sd_exec(host
, mrq
->cmd
);
726 spin_unlock_irqrestore(&sock
->lock
, flags
);
730 spin_unlock_irqrestore(&sock
->lock
, flags
);
731 mmc_request_done(mmc
, mrq
);
734 static void tifm_sd_end_cmd(unsigned long data
)
736 struct tifm_sd
*host
= (struct tifm_sd
*)data
;
737 struct tifm_dev
*sock
= host
->dev
;
738 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
739 struct mmc_request
*mrq
;
740 struct mmc_data
*r_data
= NULL
;
743 spin_lock_irqsave(&sock
->lock
, flags
);
745 del_timer(&host
->timer
);
750 printk(KERN_ERR
" %s : no request to complete?\n",
752 spin_unlock_irqrestore(&sock
->lock
, flags
);
756 r_data
= mrq
->cmd
->data
;
759 writel((~TIFM_MMCSD_BUFINT
)
760 & readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
761 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
763 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
764 (r_data
->flags
& MMC_DATA_WRITE
)
765 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
766 tifm_unmap_sg(sock
, r_data
->sg
, r_data
->sg_len
,
767 (r_data
->flags
& MMC_DATA_WRITE
)
768 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
771 r_data
->bytes_xfered
= r_data
->blocks
772 - readl(sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
) - 1;
773 r_data
->bytes_xfered
*= r_data
->blksz
;
774 r_data
->bytes_xfered
+= r_data
->blksz
775 - readl(sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
) + 1;
778 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
779 sock
->addr
+ SOCK_CONTROL
);
781 spin_unlock_irqrestore(&sock
->lock
, flags
);
782 mmc_request_done(mmc
, mrq
);
785 static void tifm_sd_abort(unsigned long data
)
787 struct tifm_sd
*host
= (struct tifm_sd
*)data
;
790 "%s : card failed to respond for a long period of time "
792 host
->dev
->dev
.bus_id
, host
->req
->cmd
->opcode
, host
->cmd_flags
);
794 tifm_eject(host
->dev
);
797 static void tifm_sd_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
799 struct tifm_sd
*host
= mmc_priv(mmc
);
800 struct tifm_dev
*sock
= host
->dev
;
801 unsigned int clk_div1
, clk_div2
;
804 spin_lock_irqsave(&sock
->lock
, flags
);
806 dev_dbg(&sock
->dev
, "ios: clock = %u, vdd = %x, bus_mode = %x, "
807 "chip_select = %x, power_mode = %x, bus_width = %x\n",
808 ios
->clock
, ios
->vdd
, ios
->bus_mode
, ios
->chip_select
,
809 ios
->power_mode
, ios
->bus_width
);
811 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
812 writel(TIFM_MMCSD_4BBUS
| readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
813 sock
->addr
+ SOCK_MMCSD_CONFIG
);
815 writel((~TIFM_MMCSD_4BBUS
)
816 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
817 sock
->addr
+ SOCK_MMCSD_CONFIG
);
821 clk_div1
= 20000000 / ios
->clock
;
825 clk_div2
= 24000000 / ios
->clock
;
829 if ((20000000 / clk_div1
) > ios
->clock
)
831 if ((24000000 / clk_div2
) > ios
->clock
)
833 if ((20000000 / clk_div1
) > (24000000 / clk_div2
)) {
834 host
->clk_freq
= 20000000;
835 host
->clk_div
= clk_div1
;
836 writel((~TIFM_CTRL_FAST_CLK
)
837 & readl(sock
->addr
+ SOCK_CONTROL
),
838 sock
->addr
+ SOCK_CONTROL
);
840 host
->clk_freq
= 24000000;
841 host
->clk_div
= clk_div2
;
842 writel(TIFM_CTRL_FAST_CLK
843 | readl(sock
->addr
+ SOCK_CONTROL
),
844 sock
->addr
+ SOCK_CONTROL
);
849 host
->clk_div
&= TIFM_MMCSD_CLKMASK
;
851 | ((~TIFM_MMCSD_CLKMASK
)
852 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
)),
853 sock
->addr
+ SOCK_MMCSD_CONFIG
);
855 host
->open_drain
= (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
);
857 /* chip_select : maybe later */
859 //power is set before probe / after remove
861 spin_unlock_irqrestore(&sock
->lock
, flags
);
864 static int tifm_sd_ro(struct mmc_host
*mmc
)
867 struct tifm_sd
*host
= mmc_priv(mmc
);
868 struct tifm_dev
*sock
= host
->dev
;
871 spin_lock_irqsave(&sock
->lock
, flags
);
872 if (TIFM_MMCSD_CARD_RO
& readl(sock
->addr
+ SOCK_PRESENT_STATE
))
874 spin_unlock_irqrestore(&sock
->lock
, flags
);
878 static const struct mmc_host_ops tifm_sd_ops
= {
879 .request
= tifm_sd_request
,
880 .set_ios
= tifm_sd_ios
,
884 static int tifm_sd_initialize_host(struct tifm_sd
*host
)
887 unsigned int host_status
= 0;
888 struct tifm_dev
*sock
= host
->dev
;
890 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
893 host
->clk_freq
= 20000000;
894 writel(TIFM_MMCSD_RESET
, sock
->addr
+ SOCK_MMCSD_SYSTEM_CONTROL
);
895 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
896 sock
->addr
+ SOCK_MMCSD_CONFIG
);
898 /* wait up to 0.51 sec for reset */
899 for (rc
= 32; rc
<= 256; rc
<<= 1) {
900 if (1 & readl(sock
->addr
+ SOCK_MMCSD_SYSTEM_STATUS
)) {
908 printk(KERN_ERR
"%s : controller failed to reset\n",
913 writel(0, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
914 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
915 sock
->addr
+ SOCK_MMCSD_CONFIG
);
916 writel(TIFM_MMCSD_RXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
918 // command timeout fixed to 64 clocks for now
919 writel(64, sock
->addr
+ SOCK_MMCSD_COMMAND_TO
);
920 writel(TIFM_MMCSD_INAB
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
922 for (rc
= 16; rc
<= 64; rc
<<= 1) {
923 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
924 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
925 if (!(host_status
& TIFM_MMCSD_ERRMASK
)
926 && (host_status
& TIFM_MMCSD_EOC
)) {
935 "%s : card not ready - probe failed on initialization\n",
940 writel(TIFM_MMCSD_CERR
| TIFM_MMCSD_BRS
| TIFM_MMCSD_EOC
941 | TIFM_MMCSD_ERRMASK
,
942 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
948 static int tifm_sd_probe(struct tifm_dev
*sock
)
950 struct mmc_host
*mmc
;
951 struct tifm_sd
*host
;
954 if (!(TIFM_SOCK_STATE_OCCUPIED
955 & readl(sock
->addr
+ SOCK_PRESENT_STATE
))) {
956 printk(KERN_WARNING
"%s : card gone, unexpectedly\n",
961 mmc
= mmc_alloc_host(sizeof(struct tifm_sd
), &sock
->dev
);
965 host
= mmc_priv(mmc
);
966 tifm_set_drvdata(sock
, mmc
);
968 host
->timeout_jiffies
= msecs_to_jiffies(1000);
970 tasklet_init(&host
->finish_tasklet
, tifm_sd_end_cmd
,
971 (unsigned long)host
);
972 setup_timer(&host
->timer
, tifm_sd_abort
, (unsigned long)host
);
974 mmc
->ops
= &tifm_sd_ops
;
975 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
976 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_MULTIWRITE
;
977 mmc
->f_min
= 20000000 / 60;
978 mmc
->f_max
= 24000000;
980 mmc
->max_blk_count
= 2048;
981 mmc
->max_hw_segs
= mmc
->max_blk_count
;
982 mmc
->max_blk_size
= min(TIFM_MMCSD_MAX_BLOCK_SIZE
, PAGE_SIZE
);
983 mmc
->max_seg_size
= mmc
->max_blk_count
* mmc
->max_blk_size
;
984 mmc
->max_req_size
= mmc
->max_seg_size
;
985 mmc
->max_phys_segs
= mmc
->max_hw_segs
;
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
), state
);
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
);