1 // SPDX-License-Identifier: GPL-2.0-only
3 * tifm_sd.c - TI FlashMedia driver
5 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
7 * Special thanks to Brad Campbell for extensive testing of this driver.
11 #include <linux/tifm.h>
12 #include <linux/mmc/host.h>
13 #include <linux/highmem.h>
14 #include <linux/scatterlist.h>
15 #include <linux/module.h>
16 #include <linux/workqueue.h>
19 #define DRIVER_NAME "tifm_sd"
20 #define DRIVER_VERSION "0.8"
22 static bool no_dma
= 0;
23 static bool fixed_timeout
= 0;
24 module_param(no_dma
, bool, 0644);
25 module_param(fixed_timeout
, bool, 0644);
27 /* Constants here are mostly from OMAP5912 datasheet */
28 #define TIFM_MMCSD_RESET 0x0002
29 #define TIFM_MMCSD_CLKMASK 0x03ff
30 #define TIFM_MMCSD_POWER 0x0800
31 #define TIFM_MMCSD_4BBUS 0x8000
32 #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
33 #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
34 #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
35 #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
36 #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
37 #define TIFM_MMCSD_READ 0x8000
39 #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
40 #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
41 #define TIFM_MMCSD_CD 0x0002 /* card detect */
42 #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
43 #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
44 #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
45 #define TIFM_MMCSD_DTO 0x0020 /* data time-out */
46 #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
47 #define TIFM_MMCSD_CTO 0x0080 /* command time-out */
48 #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
49 #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
50 #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
51 #define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
52 #define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
53 #define TIFM_MMCSD_CERR 0x4000 /* card status error */
55 #define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
56 #define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
58 #define TIFM_MMCSD_FIFO_SIZE 0x0020
60 #define TIFM_MMCSD_RSP_R0 0x0000
61 #define TIFM_MMCSD_RSP_R1 0x0100
62 #define TIFM_MMCSD_RSP_R2 0x0200
63 #define TIFM_MMCSD_RSP_R3 0x0300
64 #define TIFM_MMCSD_RSP_R4 0x0400
65 #define TIFM_MMCSD_RSP_R5 0x0500
66 #define TIFM_MMCSD_RSP_R6 0x0600
68 #define TIFM_MMCSD_RSP_BUSY 0x0800
70 #define TIFM_MMCSD_CMD_BC 0x0000
71 #define TIFM_MMCSD_CMD_BCR 0x1000
72 #define TIFM_MMCSD_CMD_AC 0x2000
73 #define TIFM_MMCSD_CMD_ADTC 0x3000
75 #define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
77 #define TIFM_MMCSD_REQ_TIMEOUT_MS 1000
92 unsigned short eject
:1,
95 unsigned short cmd_flags
;
97 unsigned int clk_freq
;
99 unsigned long timeout_jiffies
;
101 struct work_struct finish_bh_work
;
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_local_page(pg
) + 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_local(buf
- off
);
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_local_page(pg
) + 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_local(buf
- off
);
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
)
183 && (host
->cmd_flags
& DATA_CARRY
))
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_local_page(src
) + src_off
;
215 unsigned char *dst_buf
= kmap_local_page(dst
) + dst_off
;
217 memcpy(dst_buf
, src_buf
, count
);
219 kunmap_local(dst_buf
- dst_off
);
220 kunmap_local(src_buf
- src_off
);
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
;
269 if (host
->sg_pos
== host
->sg_len
)
272 if (host
->cmd_flags
& DATA_CARRY
) {
273 host
->cmd_flags
&= ~DATA_CARRY
;
274 tifm_sd_bounce_block(host
, r_data
);
275 if (host
->sg_pos
== host
->sg_len
)
279 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]) - host
->block_pos
;
283 if (host
->sg_pos
== host
->sg_len
)
285 dma_len
= sg_dma_len(&r_data
->sg
[host
->sg_pos
]);
288 if (dma_len
< t_size
) {
289 dma_blk_cnt
= dma_len
/ r_data
->blksz
;
290 dma_off
= host
->block_pos
;
291 host
->block_pos
+= dma_blk_cnt
* r_data
->blksz
;
293 dma_blk_cnt
= TIFM_DMA_TSIZE
;
294 dma_off
= host
->block_pos
;
295 host
->block_pos
+= t_size
;
299 sg
= &r_data
->sg
[host
->sg_pos
];
301 if (r_data
->flags
& MMC_DATA_WRITE
)
302 tifm_sd_bounce_block(host
, r_data
);
304 host
->cmd_flags
|= DATA_CARRY
;
306 sg
= &host
->bounce_buf
;
312 dev_dbg(&sock
->dev
, "setting dma for %d blocks\n", dma_blk_cnt
);
313 writel(sg_dma_address(sg
) + dma_off
, sock
->addr
+ SOCK_DMA_ADDRESS
);
314 if (r_data
->flags
& MMC_DATA_WRITE
)
315 writel((dma_blk_cnt
<< 8) | TIFM_DMA_TX
| TIFM_DMA_EN
,
316 sock
->addr
+ SOCK_DMA_CONTROL
);
318 writel((dma_blk_cnt
<< 8) | TIFM_DMA_EN
,
319 sock
->addr
+ SOCK_DMA_CONTROL
);
324 static unsigned int tifm_sd_op_flags(struct mmc_command
*cmd
)
328 switch (mmc_resp_type(cmd
)) {
330 rc
|= TIFM_MMCSD_RSP_R0
;
333 rc
|= TIFM_MMCSD_RSP_BUSY
;
336 rc
|= TIFM_MMCSD_RSP_R1
;
339 rc
|= TIFM_MMCSD_RSP_R2
;
342 rc
|= TIFM_MMCSD_RSP_R3
;
348 switch (mmc_cmd_type(cmd
)) {
350 rc
|= TIFM_MMCSD_CMD_BC
;
353 rc
|= TIFM_MMCSD_CMD_BCR
;
356 rc
|= TIFM_MMCSD_CMD_AC
;
359 rc
|= TIFM_MMCSD_CMD_ADTC
;
367 static void tifm_sd_exec(struct tifm_sd
*host
, struct mmc_command
*cmd
)
369 struct tifm_dev
*sock
= host
->dev
;
370 unsigned int cmd_mask
= tifm_sd_op_flags(cmd
);
372 if (host
->open_drain
)
373 cmd_mask
|= TIFM_MMCSD_ODTO
;
375 if (cmd
->data
&& (cmd
->data
->flags
& MMC_DATA_READ
))
376 cmd_mask
|= TIFM_MMCSD_READ
;
378 dev_dbg(&sock
->dev
, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
379 cmd
->opcode
, cmd
->arg
, cmd_mask
);
381 writel((cmd
->arg
>> 16) & 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_HIGH
);
382 writel(cmd
->arg
& 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_LOW
);
383 writel(cmd
->opcode
| cmd_mask
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
386 static void tifm_sd_fetch_resp(struct mmc_command
*cmd
, struct tifm_dev
*sock
)
388 cmd
->resp
[0] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x1c) << 16)
389 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x18);
390 cmd
->resp
[1] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x14) << 16)
391 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x10);
392 cmd
->resp
[2] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x0c) << 16)
393 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x08);
394 cmd
->resp
[3] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x04) << 16)
395 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x00);
398 static void tifm_sd_check_status(struct tifm_sd
*host
)
400 struct tifm_dev
*sock
= host
->dev
;
401 struct mmc_command
*cmd
= host
->req
->cmd
;
406 if (!(host
->cmd_flags
& CMD_READY
))
410 if (cmd
->data
->error
) {
411 if ((host
->cmd_flags
& SCMD_ACTIVE
)
412 && !(host
->cmd_flags
& SCMD_READY
))
418 if (!(host
->cmd_flags
& BRS_READY
))
421 if (!(host
->no_dma
|| (host
->cmd_flags
& FIFO_READY
)))
424 if (cmd
->data
->flags
& MMC_DATA_WRITE
) {
425 if (host
->req
->stop
) {
426 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
427 host
->cmd_flags
|= SCMD_ACTIVE
;
428 writel(TIFM_MMCSD_EOFB
430 + SOCK_MMCSD_INT_ENABLE
),
432 + SOCK_MMCSD_INT_ENABLE
);
433 tifm_sd_exec(host
, host
->req
->stop
);
436 if (!(host
->cmd_flags
& SCMD_READY
)
437 || (host
->cmd_flags
& CARD_BUSY
))
439 writel((~TIFM_MMCSD_EOFB
)
441 + SOCK_MMCSD_INT_ENABLE
),
443 + SOCK_MMCSD_INT_ENABLE
);
446 if (host
->cmd_flags
& CARD_BUSY
)
448 writel((~TIFM_MMCSD_EOFB
)
450 + SOCK_MMCSD_INT_ENABLE
),
451 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
454 if (host
->req
->stop
) {
455 if (!(host
->cmd_flags
& SCMD_ACTIVE
)) {
456 host
->cmd_flags
|= SCMD_ACTIVE
;
457 tifm_sd_exec(host
, host
->req
->stop
);
460 if (!(host
->cmd_flags
& SCMD_READY
))
467 queue_work(system_bh_wq
, &host
->finish_bh_work
);
470 /* Called from interrupt handler */
471 static void tifm_sd_data_event(struct tifm_dev
*sock
)
473 struct tifm_sd
*host
;
474 unsigned int fifo_status
= 0;
475 struct mmc_data
*r_data
= NULL
;
477 spin_lock(&sock
->lock
);
478 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
479 fifo_status
= readl(sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
480 dev_dbg(&sock
->dev
, "data event: fifo_status %x, flags %x\n",
481 fifo_status
, host
->cmd_flags
);
484 r_data
= host
->req
->cmd
->data
;
486 if (r_data
&& (fifo_status
& TIFM_FIFO_READY
)) {
487 if (tifm_sd_set_dma_data(host
, r_data
)) {
488 host
->cmd_flags
|= FIFO_READY
;
489 tifm_sd_check_status(host
);
494 writel(fifo_status
, sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
495 spin_unlock(&sock
->lock
);
498 /* Called from interrupt handler */
499 static void tifm_sd_card_event(struct tifm_dev
*sock
)
501 struct tifm_sd
*host
;
502 unsigned int host_status
= 0;
504 struct mmc_command
*cmd
= NULL
;
506 spin_lock(&sock
->lock
);
507 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
508 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
509 dev_dbg(&sock
->dev
, "host event: host_status %x, flags %x\n",
510 host_status
, host
->cmd_flags
);
513 cmd
= host
->req
->cmd
;
515 if (host_status
& TIFM_MMCSD_ERRMASK
) {
516 writel(host_status
& TIFM_MMCSD_ERRMASK
,
517 sock
->addr
+ SOCK_MMCSD_STATUS
);
518 if (host_status
& TIFM_MMCSD_CTO
)
519 cmd_error
= -ETIMEDOUT
;
520 else if (host_status
& TIFM_MMCSD_CCRC
)
524 if (host_status
& TIFM_MMCSD_DTO
)
525 cmd
->data
->error
= -ETIMEDOUT
;
526 else if (host_status
& TIFM_MMCSD_DCRC
)
527 cmd
->data
->error
= -EILSEQ
;
530 writel(TIFM_FIFO_INT_SETALL
,
531 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
532 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
534 if (host
->req
->stop
) {
535 if (host
->cmd_flags
& SCMD_ACTIVE
) {
536 host
->req
->stop
->error
= cmd_error
;
537 host
->cmd_flags
|= SCMD_READY
;
539 cmd
->error
= cmd_error
;
540 host
->cmd_flags
|= SCMD_ACTIVE
;
541 tifm_sd_exec(host
, host
->req
->stop
);
545 cmd
->error
= cmd_error
;
547 if (host_status
& (TIFM_MMCSD_EOC
| TIFM_MMCSD_CERR
)) {
548 if (!(host
->cmd_flags
& CMD_READY
)) {
549 host
->cmd_flags
|= CMD_READY
;
550 tifm_sd_fetch_resp(cmd
, sock
);
551 } else if (host
->cmd_flags
& SCMD_ACTIVE
) {
552 host
->cmd_flags
|= SCMD_READY
;
553 tifm_sd_fetch_resp(host
->req
->stop
,
557 if (host_status
& TIFM_MMCSD_BRS
)
558 host
->cmd_flags
|= BRS_READY
;
561 if (host
->no_dma
&& cmd
->data
) {
562 if (host_status
& TIFM_MMCSD_AE
)
563 writel(host_status
& TIFM_MMCSD_AE
,
564 sock
->addr
+ SOCK_MMCSD_STATUS
);
566 if (host_status
& (TIFM_MMCSD_AE
| TIFM_MMCSD_AF
568 tifm_sd_transfer_data(host
);
569 host_status
&= ~TIFM_MMCSD_AE
;
573 if (host_status
& TIFM_MMCSD_EOFB
)
574 host
->cmd_flags
&= ~CARD_BUSY
;
575 else if (host_status
& TIFM_MMCSD_CB
)
576 host
->cmd_flags
|= CARD_BUSY
;
578 tifm_sd_check_status(host
);
581 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
582 spin_unlock(&sock
->lock
);
585 static void tifm_sd_set_data_timeout(struct tifm_sd
*host
,
586 struct mmc_data
*data
)
588 struct tifm_dev
*sock
= host
->dev
;
589 unsigned int data_timeout
= data
->timeout_clks
;
594 data_timeout
+= data
->timeout_ns
/
595 ((1000000000UL / host
->clk_freq
) * host
->clk_div
);
597 if (data_timeout
< 0xffff) {
598 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
599 writel((~TIFM_MMCSD_DPE
)
600 & readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
601 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
603 data_timeout
= (data_timeout
>> 10) + 1;
604 if (data_timeout
> 0xffff)
605 data_timeout
= 0; /* set to unlimited */
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
);
613 static void tifm_sd_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
615 struct tifm_sd
*host
= mmc_priv(mmc
);
616 struct tifm_dev
*sock
= host
->dev
;
618 struct mmc_data
*r_data
= mrq
->cmd
->data
;
620 spin_lock_irqsave(&sock
->lock
, flags
);
622 mrq
->cmd
->error
= -ENOMEDIUM
;
627 pr_err("%s : unfinished request detected\n",
628 dev_name(&sock
->dev
));
629 mrq
->cmd
->error
= -ETIMEDOUT
;
637 if (mrq
->data
&& !is_power_of_2(mrq
->data
->blksz
))
640 host
->no_dma
= no_dma
? 1 : 0;
643 tifm_sd_set_data_timeout(host
, r_data
);
645 if ((r_data
->flags
& MMC_DATA_WRITE
) && !mrq
->stop
)
646 writel(TIFM_MMCSD_EOFB
647 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
648 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
651 writel(TIFM_MMCSD_BUFINT
652 | readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
653 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
654 writel(((TIFM_MMCSD_FIFO_SIZE
- 1) << 8)
655 | (TIFM_MMCSD_FIFO_SIZE
- 1),
656 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
658 host
->sg_len
= r_data
->sg_len
;
660 sg_init_one(&host
->bounce_buf
, host
->bounce_buf_data
,
663 if(1 != tifm_map_sg(sock
, &host
->bounce_buf
, 1,
664 r_data
->flags
& MMC_DATA_WRITE
666 : DMA_FROM_DEVICE
)) {
667 pr_err("%s : scatterlist map failed\n",
668 dev_name(&sock
->dev
));
669 mrq
->cmd
->error
= -ENOMEM
;
672 host
->sg_len
= tifm_map_sg(sock
, r_data
->sg
,
678 if (host
->sg_len
< 1) {
679 pr_err("%s : scatterlist map failed\n",
680 dev_name(&sock
->dev
));
681 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
682 r_data
->flags
& MMC_DATA_WRITE
685 mrq
->cmd
->error
= -ENOMEM
;
689 writel(TIFM_FIFO_INT_SETALL
,
690 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
691 writel(ilog2(r_data
->blksz
) - 2,
692 sock
->addr
+ SOCK_FIFO_PAGE_SIZE
);
693 writel(TIFM_FIFO_ENABLE
,
694 sock
->addr
+ SOCK_FIFO_CONTROL
);
695 writel(TIFM_FIFO_INTMASK
,
696 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
698 if (r_data
->flags
& MMC_DATA_WRITE
)
699 writel(TIFM_MMCSD_TXDE
,
700 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
702 writel(TIFM_MMCSD_RXDE
,
703 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
705 tifm_sd_set_dma_data(host
, r_data
);
708 writel(r_data
->blocks
- 1,
709 sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
710 writel(r_data
->blksz
- 1,
711 sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
);
715 mod_timer(&host
->timer
, jiffies
+ host
->timeout_jiffies
);
716 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
717 sock
->addr
+ SOCK_CONTROL
);
718 tifm_sd_exec(host
, mrq
->cmd
);
719 spin_unlock_irqrestore(&sock
->lock
, flags
);
723 spin_unlock_irqrestore(&sock
->lock
, flags
);
724 mmc_request_done(mmc
, mrq
);
727 static void tifm_sd_end_cmd(struct work_struct
*t
)
729 struct tifm_sd
*host
= from_work(host
, t
, finish_bh_work
);
730 struct tifm_dev
*sock
= host
->dev
;
731 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
732 struct mmc_request
*mrq
;
733 struct mmc_data
*r_data
= NULL
;
736 spin_lock_irqsave(&sock
->lock
, flags
);
738 del_timer(&host
->timer
);
743 pr_err(" %s : no request to complete?\n",
744 dev_name(&sock
->dev
));
745 spin_unlock_irqrestore(&sock
->lock
, flags
);
749 r_data
= mrq
->cmd
->data
;
752 writel((~TIFM_MMCSD_BUFINT
)
753 & readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
754 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
756 tifm_unmap_sg(sock
, &host
->bounce_buf
, 1,
757 (r_data
->flags
& MMC_DATA_WRITE
)
758 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
759 tifm_unmap_sg(sock
, r_data
->sg
, r_data
->sg_len
,
760 (r_data
->flags
& MMC_DATA_WRITE
)
761 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
764 r_data
->bytes_xfered
= r_data
->blocks
765 - readl(sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
) - 1;
766 r_data
->bytes_xfered
*= r_data
->blksz
;
767 r_data
->bytes_xfered
+= r_data
->blksz
768 - readl(sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
) + 1;
771 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
772 sock
->addr
+ SOCK_CONTROL
);
774 spin_unlock_irqrestore(&sock
->lock
, flags
);
775 mmc_request_done(mmc
, mrq
);
778 static void tifm_sd_abort(struct timer_list
*t
)
780 struct tifm_sd
*host
= from_timer(host
, t
, timer
);
782 pr_err("%s : card failed to respond for a long period of time "
784 dev_name(&host
->dev
->dev
), host
->req
->cmd
->opcode
, host
->cmd_flags
);
786 tifm_eject(host
->dev
);
789 static void tifm_sd_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
791 struct tifm_sd
*host
= mmc_priv(mmc
);
792 struct tifm_dev
*sock
= host
->dev
;
793 unsigned int clk_div1
, clk_div2
;
796 spin_lock_irqsave(&sock
->lock
, flags
);
798 dev_dbg(&sock
->dev
, "ios: clock = %u, vdd = %x, bus_mode = %x, "
799 "chip_select = %x, power_mode = %x, bus_width = %x\n",
800 ios
->clock
, ios
->vdd
, ios
->bus_mode
, ios
->chip_select
,
801 ios
->power_mode
, ios
->bus_width
);
803 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
804 writel(TIFM_MMCSD_4BBUS
| readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
805 sock
->addr
+ SOCK_MMCSD_CONFIG
);
807 writel((~TIFM_MMCSD_4BBUS
)
808 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
809 sock
->addr
+ SOCK_MMCSD_CONFIG
);
813 clk_div1
= 20000000 / ios
->clock
;
817 clk_div2
= 24000000 / ios
->clock
;
821 if ((20000000 / clk_div1
) > ios
->clock
)
823 if ((24000000 / clk_div2
) > ios
->clock
)
825 if ((20000000 / clk_div1
) > (24000000 / clk_div2
)) {
826 host
->clk_freq
= 20000000;
827 host
->clk_div
= clk_div1
;
828 writel((~TIFM_CTRL_FAST_CLK
)
829 & readl(sock
->addr
+ SOCK_CONTROL
),
830 sock
->addr
+ SOCK_CONTROL
);
832 host
->clk_freq
= 24000000;
833 host
->clk_div
= clk_div2
;
834 writel(TIFM_CTRL_FAST_CLK
835 | readl(sock
->addr
+ SOCK_CONTROL
),
836 sock
->addr
+ SOCK_CONTROL
);
841 host
->clk_div
&= TIFM_MMCSD_CLKMASK
;
843 | ((~TIFM_MMCSD_CLKMASK
)
844 & readl(sock
->addr
+ SOCK_MMCSD_CONFIG
)),
845 sock
->addr
+ SOCK_MMCSD_CONFIG
);
847 host
->open_drain
= (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
);
849 /* chip_select : maybe later */
851 //power is set before probe / after remove
853 spin_unlock_irqrestore(&sock
->lock
, flags
);
856 static int tifm_sd_ro(struct mmc_host
*mmc
)
859 struct tifm_sd
*host
= mmc_priv(mmc
);
860 struct tifm_dev
*sock
= host
->dev
;
863 spin_lock_irqsave(&sock
->lock
, flags
);
864 if (TIFM_MMCSD_CARD_RO
& readl(sock
->addr
+ SOCK_PRESENT_STATE
))
866 spin_unlock_irqrestore(&sock
->lock
, flags
);
870 static const struct mmc_host_ops tifm_sd_ops
= {
871 .request
= tifm_sd_request
,
872 .set_ios
= tifm_sd_ios
,
876 static int tifm_sd_initialize_host(struct tifm_sd
*host
)
879 unsigned int host_status
= 0;
880 struct tifm_dev
*sock
= host
->dev
;
882 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
884 host
->clk_freq
= 20000000;
885 writel(TIFM_MMCSD_RESET
, sock
->addr
+ SOCK_MMCSD_SYSTEM_CONTROL
);
886 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
887 sock
->addr
+ SOCK_MMCSD_CONFIG
);
889 /* wait up to 0.51 sec for reset */
890 for (rc
= 32; rc
<= 256; rc
<<= 1) {
891 if (1 & readl(sock
->addr
+ SOCK_MMCSD_SYSTEM_STATUS
)) {
899 pr_err("%s : controller failed to reset\n",
900 dev_name(&sock
->dev
));
904 writel(0, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
905 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
906 sock
->addr
+ SOCK_MMCSD_CONFIG
);
907 writel(TIFM_MMCSD_RXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
909 // command timeout fixed to 64 clocks for now
910 writel(64, sock
->addr
+ SOCK_MMCSD_COMMAND_TO
);
911 writel(TIFM_MMCSD_INAB
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
913 for (rc
= 16; rc
<= 64; rc
<<= 1) {
914 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
915 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
916 if (!(host_status
& TIFM_MMCSD_ERRMASK
)
917 && (host_status
& TIFM_MMCSD_EOC
)) {
925 pr_err("%s : card not ready - probe failed on initialization\n",
926 dev_name(&sock
->dev
));
930 writel(TIFM_MMCSD_CERR
| TIFM_MMCSD_BRS
| TIFM_MMCSD_EOC
931 | TIFM_MMCSD_ERRMASK
,
932 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
937 static int tifm_sd_probe(struct tifm_dev
*sock
)
939 struct mmc_host
*mmc
;
940 struct tifm_sd
*host
;
943 if (!(TIFM_SOCK_STATE_OCCUPIED
944 & readl(sock
->addr
+ SOCK_PRESENT_STATE
))) {
945 pr_warn("%s : card gone, unexpectedly\n",
946 dev_name(&sock
->dev
));
950 mmc
= mmc_alloc_host(sizeof(struct tifm_sd
), &sock
->dev
);
954 host
= mmc_priv(mmc
);
955 tifm_set_drvdata(sock
, mmc
);
957 host
->timeout_jiffies
= msecs_to_jiffies(TIFM_MMCSD_REQ_TIMEOUT_MS
);
959 * We use a fixed request timeout of 1s, hence inform the core about it.
960 * A future improvement should instead respect the cmd->busy_timeout.
962 mmc
->max_busy_timeout
= TIFM_MMCSD_REQ_TIMEOUT_MS
;
964 INIT_WORK(&host
->finish_bh_work
, tifm_sd_end_cmd
);
965 timer_setup(&host
->timer
, tifm_sd_abort
, 0);
967 mmc
->ops
= &tifm_sd_ops
;
968 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
969 mmc
->caps
= MMC_CAP_4_BIT_DATA
;
970 mmc
->f_min
= 20000000 / 60;
971 mmc
->f_max
= 24000000;
973 mmc
->max_blk_count
= 2048;
974 mmc
->max_segs
= mmc
->max_blk_count
;
975 mmc
->max_blk_size
= min(TIFM_MMCSD_MAX_BLOCK_SIZE
, PAGE_SIZE
);
976 mmc
->max_seg_size
= mmc
->max_blk_count
* mmc
->max_blk_size
;
977 mmc
->max_req_size
= mmc
->max_seg_size
;
979 sock
->card_event
= tifm_sd_card_event
;
980 sock
->data_event
= tifm_sd_data_event
;
981 rc
= tifm_sd_initialize_host(host
);
984 rc
= mmc_add_host(mmc
);
992 static void tifm_sd_remove(struct tifm_dev
*sock
)
994 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
995 struct tifm_sd
*host
= mmc_priv(mmc
);
998 spin_lock_irqsave(&sock
->lock
, flags
);
1000 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
1001 spin_unlock_irqrestore(&sock
->lock
, flags
);
1003 cancel_work_sync(&host
->finish_bh_work
);
1005 spin_lock_irqsave(&sock
->lock
, flags
);
1007 writel(TIFM_FIFO_INT_SETALL
,
1008 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
1009 writel(0, sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
1010 host
->req
->cmd
->error
= -ENOMEDIUM
;
1011 if (host
->req
->stop
)
1012 host
->req
->stop
->error
= -ENOMEDIUM
;
1013 queue_work(system_bh_wq
, &host
->finish_bh_work
);
1015 spin_unlock_irqrestore(&sock
->lock
, flags
);
1016 mmc_remove_host(mmc
);
1017 dev_dbg(&sock
->dev
, "after remove\n");
1024 static int tifm_sd_suspend(struct tifm_dev
*sock
, pm_message_t state
)
1029 static int tifm_sd_resume(struct tifm_dev
*sock
)
1031 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
1032 struct tifm_sd
*host
= mmc_priv(mmc
);
1035 rc
= tifm_sd_initialize_host(host
);
1036 dev_dbg(&sock
->dev
, "resume initialize %d\n", rc
);
1046 #define tifm_sd_suspend NULL
1047 #define tifm_sd_resume NULL
1049 #endif /* CONFIG_PM */
1051 static struct tifm_device_id tifm_sd_id_tbl
[] = {
1052 { TIFM_TYPE_SD
}, { }
1055 static struct tifm_driver tifm_sd_driver
= {
1057 .name
= DRIVER_NAME
,
1058 .owner
= THIS_MODULE
1060 .id_table
= tifm_sd_id_tbl
,
1061 .probe
= tifm_sd_probe
,
1062 .remove
= tifm_sd_remove
,
1063 .suspend
= tifm_sd_suspend
,
1064 .resume
= tifm_sd_resume
1067 static int __init
tifm_sd_init(void)
1069 return tifm_register_driver(&tifm_sd_driver
);
1072 static void __exit
tifm_sd_exit(void)
1074 tifm_unregister_driver(&tifm_sd_driver
);
1077 MODULE_AUTHOR("Alex Dubov");
1078 MODULE_DESCRIPTION("TI FlashMedia SD driver");
1079 MODULE_LICENSE("GPL");
1080 MODULE_DEVICE_TABLE(tifm
, tifm_sd_id_tbl
);
1081 MODULE_VERSION(DRIVER_VERSION
);
1083 module_init(tifm_sd_init
);
1084 module_exit(tifm_sd_exit
);