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.
13 #include <linux/tifm.h>
14 #include <linux/mmc/protocol.h>
15 #include <linux/mmc/host.h>
16 #include <linux/highmem.h>
19 #define DRIVER_NAME "tifm_sd"
20 #define DRIVER_VERSION "0.7"
22 static int no_dma
= 0;
23 static int 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_DATAMASK 0x001d /* set bits: EOFB, BRS, CB, EOC */
40 #define TIFM_MMCSD_ERRMASK 0x41e0 /* set bits: CERR, CCRC, CTO, DCRC, DTO */
41 #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
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_CERR 0x4000 /* card status error */
53 #define TIFM_MMCSD_FIFO_SIZE 0x0020
55 #define TIFM_MMCSD_RSP_R0 0x0000
56 #define TIFM_MMCSD_RSP_R1 0x0100
57 #define TIFM_MMCSD_RSP_R2 0x0200
58 #define TIFM_MMCSD_RSP_R3 0x0300
59 #define TIFM_MMCSD_RSP_R4 0x0400
60 #define TIFM_MMCSD_RSP_R5 0x0500
61 #define TIFM_MMCSD_RSP_R6 0x0600
63 #define TIFM_MMCSD_RSP_BUSY 0x0800
65 #define TIFM_MMCSD_CMD_BC 0x0000
66 #define TIFM_MMCSD_CMD_BCR 0x1000
67 #define TIFM_MMCSD_CMD_AC 0x2000
68 #define TIFM_MMCSD_CMD_ADTC 0x3000
72 CMD
, /* main command ended */
73 BRS
, /* block transfer finished */
74 SCMD
, /* stop command ended */
75 CARD
, /* card left busy state */
76 FIFO
, /* FIFO operation completed (uncertain) */
81 FIFO_RDY
= 0x0001, /* hardware dependent value */
86 OPENDRAIN
= 0x0040, /* hardware dependent value */
87 CARD_EVENT
= 0x0100, /* hardware dependent value */
88 CARD_RO
= 0x0200, /* hardware dependent value */
89 FIFO_EVENT
= 0x10000 }; /* hardware dependent value */
96 unsigned int clk_freq
;
98 unsigned long timeout_jiffies
; // software timeout - 2 sec
100 struct mmc_request
*req
;
101 struct work_struct cmd_handler
;
102 struct delayed_work abort_handler
;
103 wait_queue_head_t can_eject
;
105 size_t written_blocks
;
111 static char* tifm_sd_kmap_atomic(struct mmc_data
*data
)
113 return kmap_atomic(data
->sg
->page
, KM_BIO_SRC_IRQ
) + data
->sg
->offset
;
116 static void tifm_sd_kunmap_atomic(char *buffer
, struct mmc_data
*data
)
118 kunmap_atomic(buffer
- data
->sg
->offset
, KM_BIO_SRC_IRQ
);
121 static int tifm_sd_transfer_data(struct tifm_dev
*sock
, struct tifm_sd
*host
,
122 unsigned int host_status
)
124 struct mmc_command
*cmd
= host
->req
->cmd
;
125 unsigned int t_val
= 0, cnt
= 0;
128 if (host_status
& TIFM_MMCSD_BRS
) {
129 /* in non-dma rx mode BRS fires when fifo is still not empty */
130 if (no_dma
&& (cmd
->data
->flags
& MMC_DATA_READ
)) {
131 buffer
= tifm_sd_kmap_atomic(host
->req
->data
);
132 while (host
->buffer_size
> host
->buffer_pos
) {
133 t_val
= readl(sock
->addr
+ SOCK_MMCSD_DATA
);
134 buffer
[host
->buffer_pos
++] = t_val
& 0xff;
135 buffer
[host
->buffer_pos
++] =
138 tifm_sd_kunmap_atomic(buffer
, host
->req
->data
);
142 buffer
= tifm_sd_kmap_atomic(host
->req
->data
);
143 if ((cmd
->data
->flags
& MMC_DATA_READ
) &&
144 (host_status
& TIFM_MMCSD_AF
)) {
145 for (cnt
= 0; cnt
< TIFM_MMCSD_FIFO_SIZE
; cnt
++) {
146 t_val
= readl(sock
->addr
+ SOCK_MMCSD_DATA
);
147 if (host
->buffer_size
> host
->buffer_pos
) {
148 buffer
[host
->buffer_pos
++] =
150 buffer
[host
->buffer_pos
++] =
154 } else if ((cmd
->data
->flags
& MMC_DATA_WRITE
)
155 && (host_status
& TIFM_MMCSD_AE
)) {
156 for (cnt
= 0; cnt
< TIFM_MMCSD_FIFO_SIZE
; cnt
++) {
157 if (host
->buffer_size
> host
->buffer_pos
) {
158 t_val
= buffer
[host
->buffer_pos
++]
160 t_val
|= ((buffer
[host
->buffer_pos
++])
163 sock
->addr
+ SOCK_MMCSD_DATA
);
167 tifm_sd_kunmap_atomic(buffer
, host
->req
->data
);
172 static unsigned int tifm_sd_op_flags(struct mmc_command
*cmd
)
176 switch (mmc_resp_type(cmd
)) {
178 rc
|= TIFM_MMCSD_RSP_R0
;
181 rc
|= TIFM_MMCSD_RSP_BUSY
; // deliberate fall-through
183 rc
|= TIFM_MMCSD_RSP_R1
;
186 rc
|= TIFM_MMCSD_RSP_R2
;
189 rc
|= TIFM_MMCSD_RSP_R3
;
195 switch (mmc_cmd_type(cmd
)) {
197 rc
|= TIFM_MMCSD_CMD_BC
;
200 rc
|= TIFM_MMCSD_CMD_BCR
;
203 rc
|= TIFM_MMCSD_CMD_AC
;
206 rc
|= TIFM_MMCSD_CMD_ADTC
;
214 static void tifm_sd_exec(struct tifm_sd
*host
, struct mmc_command
*cmd
)
216 struct tifm_dev
*sock
= host
->dev
;
217 unsigned int cmd_mask
= tifm_sd_op_flags(cmd
) |
218 (host
->flags
& OPENDRAIN
);
220 if (cmd
->data
&& (cmd
->data
->flags
& MMC_DATA_READ
))
221 cmd_mask
|= TIFM_MMCSD_READ
;
223 dev_dbg(&sock
->dev
, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
224 cmd
->opcode
, cmd
->arg
, cmd_mask
);
226 writel((cmd
->arg
>> 16) & 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_HIGH
);
227 writel(cmd
->arg
& 0xffff, sock
->addr
+ SOCK_MMCSD_ARG_LOW
);
228 writel(cmd
->opcode
| cmd_mask
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
231 static void tifm_sd_fetch_resp(struct mmc_command
*cmd
, struct tifm_dev
*sock
)
233 cmd
->resp
[0] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x1c) << 16)
234 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x18);
235 cmd
->resp
[1] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x14) << 16)
236 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x10);
237 cmd
->resp
[2] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x0c) << 16)
238 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x08);
239 cmd
->resp
[3] = (readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x04) << 16)
240 | readl(sock
->addr
+ SOCK_MMCSD_RESPONSE
+ 0x00);
243 static void tifm_sd_process_cmd(struct tifm_dev
*sock
, struct tifm_sd
*host
,
244 unsigned int host_status
)
246 struct mmc_command
*cmd
= host
->req
->cmd
;
249 switch (host
->state
) {
253 if (host_status
& TIFM_MMCSD_EOC
) {
254 tifm_sd_fetch_resp(cmd
, sock
);
264 if (tifm_sd_transfer_data(sock
, host
, host_status
)) {
265 if (cmd
->data
->flags
& MMC_DATA_WRITE
) {
269 if (host
->req
->stop
) {
270 tifm_sd_exec(host
, host
->req
->stop
);
283 if (host_status
& TIFM_MMCSD_EOC
) {
284 tifm_sd_fetch_resp(host
->req
->stop
, sock
);
290 dev_dbg(&sock
->dev
, "waiting for CARD, have %zd blocks\n",
291 host
->written_blocks
);
292 if (!(host
->flags
& CARD_BUSY
)
293 && (host
->written_blocks
== cmd
->data
->blocks
)) {
295 if (host
->req
->stop
) {
296 tifm_sd_exec(host
, host
->req
->stop
);
308 if (host
->flags
& FIFO_RDY
) {
309 host
->flags
&= ~FIFO_RDY
;
310 if (host
->req
->stop
) {
311 tifm_sd_exec(host
, host
->req
->stop
);
320 queue_work(sock
->wq
, &host
->cmd_handler
);
324 queue_delayed_work(sock
->wq
, &host
->abort_handler
,
325 host
->timeout_jiffies
);
328 /* Called from interrupt handler */
329 static unsigned int tifm_sd_signal_irq(struct tifm_dev
*sock
,
330 unsigned int sock_irq_status
)
332 struct tifm_sd
*host
;
333 unsigned int host_status
= 0, fifo_status
= 0;
336 spin_lock(&sock
->lock
);
337 host
= mmc_priv((struct mmc_host
*)tifm_get_drvdata(sock
));
338 cancel_delayed_work(&host
->abort_handler
);
340 if (sock_irq_status
& FIFO_EVENT
) {
341 fifo_status
= readl(sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
342 writel(fifo_status
, sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
344 host
->flags
|= fifo_status
& FIFO_RDY
;
347 if (sock_irq_status
& CARD_EVENT
) {
348 host_status
= readl(sock
->addr
+ SOCK_MMCSD_STATUS
);
349 writel(host_status
, sock
->addr
+ SOCK_MMCSD_STATUS
);
351 if (!(host
->flags
& HOST_REG
))
352 queue_work(sock
->wq
, &host
->cmd_handler
);
356 if (host_status
& TIFM_MMCSD_ERRMASK
) {
357 if (host_status
& TIFM_MMCSD_CERR
)
358 error_code
= MMC_ERR_FAILED
;
359 else if (host_status
&
360 (TIFM_MMCSD_CTO
| TIFM_MMCSD_DTO
))
361 error_code
= MMC_ERR_TIMEOUT
;
362 else if (host_status
&
363 (TIFM_MMCSD_CCRC
| TIFM_MMCSD_DCRC
))
364 error_code
= MMC_ERR_BADCRC
;
366 writel(TIFM_FIFO_INT_SETALL
,
367 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
368 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
370 if (host
->req
->stop
) {
371 if (host
->state
== SCMD
) {
372 host
->req
->stop
->error
= error_code
;
373 } else if (host
->state
== BRS
374 || host
->state
== CARD
375 || host
->state
== FIFO
) {
376 host
->req
->cmd
->error
= error_code
;
377 tifm_sd_exec(host
, host
->req
->stop
);
378 queue_delayed_work(sock
->wq
,
379 &host
->abort_handler
,
380 host
->timeout_jiffies
);
384 host
->req
->cmd
->error
= error_code
;
387 host
->req
->cmd
->error
= error_code
;
392 if (host_status
& TIFM_MMCSD_CB
)
393 host
->flags
|= CARD_BUSY
;
394 if ((host_status
& TIFM_MMCSD_EOFB
) &&
395 (host
->flags
& CARD_BUSY
)) {
396 host
->written_blocks
++;
397 host
->flags
&= ~CARD_BUSY
;
402 tifm_sd_process_cmd(sock
, host
, host_status
);
404 dev_dbg(&sock
->dev
, "host_status %x, fifo_status %x\n",
405 host_status
, fifo_status
);
406 spin_unlock(&sock
->lock
);
407 return sock_irq_status
;
410 static void tifm_sd_prepare_data(struct tifm_sd
*card
, struct mmc_command
*cmd
)
412 struct tifm_dev
*sock
= card
->dev
;
413 unsigned int dest_cnt
;
417 writel(TIFM_FIFO_INT_SETALL
,
418 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
419 writel(ilog2(cmd
->data
->blksz
) - 2,
420 sock
->addr
+ SOCK_FIFO_PAGE_SIZE
);
421 writel(TIFM_FIFO_ENABLE
, sock
->addr
+ SOCK_FIFO_CONTROL
);
422 writel(TIFM_FIFO_INTMASK
, sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
424 dest_cnt
= (cmd
->data
->blocks
) << 8;
426 writel(sg_dma_address(cmd
->data
->sg
), sock
->addr
+ SOCK_DMA_ADDRESS
);
428 writel(cmd
->data
->blocks
- 1, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
429 writel(cmd
->data
->blksz
- 1, sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
);
431 if (cmd
->data
->flags
& MMC_DATA_WRITE
) {
432 writel(TIFM_MMCSD_TXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
433 writel(dest_cnt
| TIFM_DMA_TX
| TIFM_DMA_EN
,
434 sock
->addr
+ SOCK_DMA_CONTROL
);
436 writel(TIFM_MMCSD_RXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
437 writel(dest_cnt
| TIFM_DMA_EN
, sock
->addr
+ SOCK_DMA_CONTROL
);
441 static void tifm_sd_set_data_timeout(struct tifm_sd
*host
,
442 struct mmc_data
*data
)
444 struct tifm_dev
*sock
= host
->dev
;
445 unsigned int data_timeout
= data
->timeout_clks
;
450 data_timeout
+= data
->timeout_ns
/
451 ((1000000000 / host
->clk_freq
) * host
->clk_div
);
452 data_timeout
*= 10; // call it fudge factor for now
454 if (data_timeout
< 0xffff) {
455 writel((~TIFM_MMCSD_DPE
) &
456 readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
457 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
458 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
460 writel(TIFM_MMCSD_DPE
|
461 readl(sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
),
462 sock
->addr
+ SOCK_MMCSD_SDIO_MODE_CONFIG
);
463 data_timeout
= (data_timeout
>> 10) + 1;
464 if(data_timeout
> 0xffff)
465 data_timeout
= 0; /* set to unlimited */
466 writel(data_timeout
, sock
->addr
+ SOCK_MMCSD_DATA_TO
);
470 static void tifm_sd_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
472 struct tifm_sd
*host
= mmc_priv(mmc
);
473 struct tifm_dev
*sock
= host
->dev
;
476 struct mmc_data
*r_data
= mrq
->cmd
->data
;
478 spin_lock_irqsave(&sock
->lock
, flags
);
479 if (host
->flags
& EJECT
) {
480 spin_unlock_irqrestore(&sock
->lock
, flags
);
485 printk(KERN_ERR DRIVER_NAME
": unfinished request detected\n");
486 spin_unlock_irqrestore(&sock
->lock
, flags
);
491 tifm_sd_set_data_timeout(host
, r_data
);
493 sg_count
= tifm_map_sg(sock
, r_data
->sg
, r_data
->sg_len
,
494 mrq
->cmd
->flags
& MMC_DATA_WRITE
495 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
497 printk(KERN_ERR DRIVER_NAME
498 ": scatterlist map failed\n");
499 spin_unlock_irqrestore(&sock
->lock
, flags
);
503 host
->written_blocks
= 0;
504 host
->flags
&= ~CARD_BUSY
;
505 tifm_sd_prepare_data(host
, mrq
->cmd
);
510 queue_delayed_work(sock
->wq
, &host
->abort_handler
,
511 host
->timeout_jiffies
);
512 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
513 sock
->addr
+ SOCK_CONTROL
);
514 tifm_sd_exec(host
, mrq
->cmd
);
515 spin_unlock_irqrestore(&sock
->lock
, flags
);
520 tifm_unmap_sg(sock
, r_data
->sg
, r_data
->sg_len
,
521 (r_data
->flags
& MMC_DATA_WRITE
)
522 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
524 mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
525 mmc_request_done(mmc
, mrq
);
528 static void tifm_sd_end_cmd(struct work_struct
*work
)
530 struct tifm_sd
*host
= container_of(work
, struct tifm_sd
, cmd_handler
);
531 struct tifm_dev
*sock
= host
->dev
;
532 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
533 struct mmc_request
*mrq
;
534 struct mmc_data
*r_data
= NULL
;
537 spin_lock_irqsave(&sock
->lock
, flags
);
544 printk(KERN_ERR DRIVER_NAME
": no request to complete?\n");
545 spin_unlock_irqrestore(&sock
->lock
, flags
);
549 r_data
= mrq
->cmd
->data
;
551 if (r_data
->flags
& MMC_DATA_WRITE
) {
552 r_data
->bytes_xfered
= host
->written_blocks
*
555 r_data
->bytes_xfered
= r_data
->blocks
-
556 readl(sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
) - 1;
557 r_data
->bytes_xfered
*= r_data
->blksz
;
558 r_data
->bytes_xfered
+= r_data
->blksz
-
559 readl(sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
) + 1;
561 tifm_unmap_sg(sock
, r_data
->sg
, r_data
->sg_len
,
562 (r_data
->flags
& MMC_DATA_WRITE
)
563 ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
566 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
567 sock
->addr
+ SOCK_CONTROL
);
569 spin_unlock_irqrestore(&sock
->lock
, flags
);
570 mmc_request_done(mmc
, mrq
);
573 static void tifm_sd_request_nodma(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
575 struct tifm_sd
*host
= mmc_priv(mmc
);
576 struct tifm_dev
*sock
= host
->dev
;
578 struct mmc_data
*r_data
= mrq
->cmd
->data
;
580 spin_lock_irqsave(&sock
->lock
, flags
);
581 if (host
->flags
& EJECT
) {
582 spin_unlock_irqrestore(&sock
->lock
, flags
);
587 printk(KERN_ERR DRIVER_NAME
": unfinished request detected\n");
588 spin_unlock_irqrestore(&sock
->lock
, flags
);
593 tifm_sd_set_data_timeout(host
, r_data
);
595 host
->buffer_size
= mrq
->cmd
->data
->blocks
*
596 mrq
->cmd
->data
->blksz
;
598 writel(TIFM_MMCSD_BUFINT
|
599 readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
600 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
601 writel(((TIFM_MMCSD_FIFO_SIZE
- 1) << 8) |
602 (TIFM_MMCSD_FIFO_SIZE
- 1),
603 sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
605 host
->written_blocks
= 0;
606 host
->flags
&= ~CARD_BUSY
;
607 host
->buffer_pos
= 0;
608 writel(r_data
->blocks
- 1, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
609 writel(r_data
->blksz
- 1, sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
);
614 queue_delayed_work(sock
->wq
, &host
->abort_handler
,
615 host
->timeout_jiffies
);
616 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
617 sock
->addr
+ SOCK_CONTROL
);
618 tifm_sd_exec(host
, mrq
->cmd
);
619 spin_unlock_irqrestore(&sock
->lock
, flags
);
623 mrq
->cmd
->error
= MMC_ERR_TIMEOUT
;
624 mmc_request_done(mmc
, mrq
);
627 static void tifm_sd_end_cmd_nodma(struct work_struct
*work
)
629 struct tifm_sd
*host
= container_of(work
, struct tifm_sd
, cmd_handler
);
630 struct tifm_dev
*sock
= host
->dev
;
631 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
632 struct mmc_request
*mrq
;
633 struct mmc_data
*r_data
= NULL
;
636 spin_lock_irqsave(&sock
->lock
, flags
);
643 printk(KERN_ERR DRIVER_NAME
": no request to complete?\n");
644 spin_unlock_irqrestore(&sock
->lock
, flags
);
648 r_data
= mrq
->cmd
->data
;
650 writel((~TIFM_MMCSD_BUFINT
) &
651 readl(sock
->addr
+ SOCK_MMCSD_INT_ENABLE
),
652 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
654 if (r_data
->flags
& MMC_DATA_WRITE
) {
655 r_data
->bytes_xfered
= host
->written_blocks
*
658 r_data
->bytes_xfered
= r_data
->blocks
-
659 readl(sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
) - 1;
660 r_data
->bytes_xfered
*= r_data
->blksz
;
661 r_data
->bytes_xfered
+= r_data
->blksz
-
662 readl(sock
->addr
+ SOCK_MMCSD_BLOCK_LEN
) + 1;
664 host
->buffer_pos
= 0;
665 host
->buffer_size
= 0;
668 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
669 sock
->addr
+ SOCK_CONTROL
);
671 spin_unlock_irqrestore(&sock
->lock
, flags
);
673 mmc_request_done(mmc
, mrq
);
676 static void tifm_sd_abort(struct work_struct
*work
)
678 struct tifm_sd
*host
=
679 container_of(work
, struct tifm_sd
, abort_handler
.work
);
681 printk(KERN_ERR DRIVER_NAME
682 ": card failed to respond for a long period of time");
683 tifm_eject(host
->dev
);
686 static void tifm_sd_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
688 struct tifm_sd
*host
= mmc_priv(mmc
);
689 struct tifm_dev
*sock
= host
->dev
;
690 unsigned int clk_div1
, clk_div2
;
693 spin_lock_irqsave(&sock
->lock
, flags
);
695 dev_dbg(&sock
->dev
, "Setting bus width %d, power %d\n", ios
->bus_width
,
697 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
698 writel(TIFM_MMCSD_4BBUS
| readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
699 sock
->addr
+ SOCK_MMCSD_CONFIG
);
701 writel((~TIFM_MMCSD_4BBUS
) &
702 readl(sock
->addr
+ SOCK_MMCSD_CONFIG
),
703 sock
->addr
+ SOCK_MMCSD_CONFIG
);
707 clk_div1
= 20000000 / ios
->clock
;
711 clk_div2
= 24000000 / ios
->clock
;
715 if ((20000000 / clk_div1
) > ios
->clock
)
717 if ((24000000 / clk_div2
) > ios
->clock
)
719 if ((20000000 / clk_div1
) > (24000000 / clk_div2
)) {
720 host
->clk_freq
= 20000000;
721 host
->clk_div
= clk_div1
;
722 writel((~TIFM_CTRL_FAST_CLK
) &
723 readl(sock
->addr
+ SOCK_CONTROL
),
724 sock
->addr
+ SOCK_CONTROL
);
726 host
->clk_freq
= 24000000;
727 host
->clk_div
= clk_div2
;
728 writel(TIFM_CTRL_FAST_CLK
|
729 readl(sock
->addr
+ SOCK_CONTROL
),
730 sock
->addr
+ SOCK_CONTROL
);
735 host
->clk_div
&= TIFM_MMCSD_CLKMASK
;
736 writel(host
->clk_div
| ((~TIFM_MMCSD_CLKMASK
) &
737 readl(sock
->addr
+ SOCK_MMCSD_CONFIG
)),
738 sock
->addr
+ SOCK_MMCSD_CONFIG
);
740 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
741 host
->flags
|= OPENDRAIN
;
743 host
->flags
&= ~OPENDRAIN
;
745 /* chip_select : maybe later */
747 //power is set before probe / after remove
748 //I believe, power_off when already marked for eject is sufficient to
750 if ((host
->flags
& EJECT
) && ios
->power_mode
== MMC_POWER_OFF
) {
751 host
->flags
|= EJECT_DONE
;
752 wake_up_all(&host
->can_eject
);
755 spin_unlock_irqrestore(&sock
->lock
, flags
);
758 static int tifm_sd_ro(struct mmc_host
*mmc
)
761 struct tifm_sd
*host
= mmc_priv(mmc
);
762 struct tifm_dev
*sock
= host
->dev
;
765 spin_lock_irqsave(&sock
->lock
, flags
);
767 host
->flags
|= (CARD_RO
& readl(sock
->addr
+ SOCK_PRESENT_STATE
));
768 rc
= (host
->flags
& CARD_RO
) ? 1 : 0;
770 spin_unlock_irqrestore(&sock
->lock
, flags
);
774 static struct mmc_host_ops tifm_sd_ops
= {
775 .request
= tifm_sd_request
,
776 .set_ios
= tifm_sd_ios
,
780 static void tifm_sd_register_host(struct work_struct
*work
)
782 struct tifm_sd
*host
= container_of(work
, struct tifm_sd
, cmd_handler
);
783 struct tifm_dev
*sock
= host
->dev
;
784 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
787 spin_lock_irqsave(&sock
->lock
, flags
);
788 host
->flags
|= HOST_REG
;
789 PREPARE_WORK(&host
->cmd_handler
,
790 no_dma
? tifm_sd_end_cmd_nodma
: tifm_sd_end_cmd
);
791 spin_unlock_irqrestore(&sock
->lock
, flags
);
792 dev_dbg(&sock
->dev
, "adding host\n");
796 static int tifm_sd_probe(struct tifm_dev
*sock
)
798 struct mmc_host
*mmc
;
799 struct tifm_sd
*host
;
802 if (!(TIFM_SOCK_STATE_OCCUPIED
&
803 readl(sock
->addr
+ SOCK_PRESENT_STATE
))) {
804 printk(KERN_WARNING DRIVER_NAME
": card gone, unexpectedly\n");
808 mmc
= mmc_alloc_host(sizeof(struct tifm_sd
), &sock
->dev
);
812 host
= mmc_priv(mmc
);
815 init_waitqueue_head(&host
->can_eject
);
816 INIT_WORK(&host
->cmd_handler
, tifm_sd_register_host
);
817 INIT_DELAYED_WORK(&host
->abort_handler
, tifm_sd_abort
);
819 tifm_set_drvdata(sock
, mmc
);
820 sock
->signal_irq
= tifm_sd_signal_irq
;
822 host
->clk_freq
= 20000000;
823 host
->timeout_jiffies
= msecs_to_jiffies(1000);
825 tifm_sd_ops
.request
= no_dma
? tifm_sd_request_nodma
: tifm_sd_request
;
826 mmc
->ops
= &tifm_sd_ops
;
827 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
828 mmc
->caps
= MMC_CAP_4_BIT_DATA
;
829 mmc
->f_min
= 20000000 / 60;
830 mmc
->f_max
= 24000000;
831 mmc
->max_hw_segs
= 1;
832 mmc
->max_phys_segs
= 1;
833 mmc
->max_sectors
= 127;
834 mmc
->max_seg_size
= mmc
->max_sectors
<< 11; //2k maximum hw block length
836 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
837 writel(TIFM_MMCSD_RESET
, sock
->addr
+ SOCK_MMCSD_SYSTEM_CONTROL
);
838 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
839 sock
->addr
+ SOCK_MMCSD_CONFIG
);
841 for (rc
= 0; rc
< 50; rc
++) {
842 /* Wait for reset ack */
843 if (1 & readl(sock
->addr
+ SOCK_MMCSD_SYSTEM_STATUS
)) {
851 printk(KERN_ERR DRIVER_NAME
852 ": card not ready - probe failed\n");
857 writel(0, sock
->addr
+ SOCK_MMCSD_NUM_BLOCKS
);
858 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
859 sock
->addr
+ SOCK_MMCSD_CONFIG
);
860 writel(TIFM_MMCSD_RXDE
, sock
->addr
+ SOCK_MMCSD_BUFFER_CONFIG
);
861 writel(TIFM_MMCSD_DATAMASK
| TIFM_MMCSD_ERRMASK
,
862 sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
864 writel(64, sock
->addr
+ SOCK_MMCSD_COMMAND_TO
); // command timeout 64 clocks for now
865 writel(TIFM_MMCSD_INAB
, sock
->addr
+ SOCK_MMCSD_COMMAND
);
866 writel(host
->clk_div
| TIFM_MMCSD_POWER
,
867 sock
->addr
+ SOCK_MMCSD_CONFIG
);
869 queue_delayed_work(sock
->wq
, &host
->abort_handler
,
870 host
->timeout_jiffies
);
875 static int tifm_sd_host_is_down(struct tifm_dev
*sock
)
877 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
878 struct tifm_sd
*host
= mmc_priv(mmc
);
882 spin_lock_irqsave(&sock
->lock
, flags
);
883 rc
= (host
->flags
& EJECT_DONE
);
884 spin_unlock_irqrestore(&sock
->lock
, flags
);
888 static void tifm_sd_remove(struct tifm_dev
*sock
)
890 struct mmc_host
*mmc
= tifm_get_drvdata(sock
);
891 struct tifm_sd
*host
= mmc_priv(mmc
);
894 spin_lock_irqsave(&sock
->lock
, flags
);
895 host
->flags
|= EJECT
;
897 queue_work(sock
->wq
, &host
->cmd_handler
);
898 spin_unlock_irqrestore(&sock
->lock
, flags
);
899 wait_event_timeout(host
->can_eject
, tifm_sd_host_is_down(sock
),
900 host
->timeout_jiffies
);
902 if (host
->flags
& HOST_REG
)
903 mmc_remove_host(mmc
);
905 /* The meaning of the bit majority in this constant is unknown. */
906 writel(0xfff8 & readl(sock
->addr
+ SOCK_CONTROL
),
907 sock
->addr
+ SOCK_CONTROL
);
908 writel(0, sock
->addr
+ SOCK_MMCSD_INT_ENABLE
);
909 writel(TIFM_FIFO_INT_SETALL
,
910 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
911 writel(0, sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
913 tifm_set_drvdata(sock
, NULL
);
917 static tifm_media_id tifm_sd_id_tbl
[] = {
921 static struct tifm_driver tifm_sd_driver
= {
926 .id_table
= tifm_sd_id_tbl
,
927 .probe
= tifm_sd_probe
,
928 .remove
= tifm_sd_remove
931 static int __init
tifm_sd_init(void)
933 return tifm_register_driver(&tifm_sd_driver
);
936 static void __exit
tifm_sd_exit(void)
938 tifm_unregister_driver(&tifm_sd_driver
);
941 MODULE_AUTHOR("Alex Dubov");
942 MODULE_DESCRIPTION("TI FlashMedia SD driver");
943 MODULE_LICENSE("GPL");
944 MODULE_DEVICE_TABLE(tifm
, tifm_sd_id_tbl
);
945 MODULE_VERSION(DRIVER_VERSION
);
947 module_init(tifm_sd_init
);
948 module_exit(tifm_sd_exit
);