19 #include <linux/module.h>
22 #define DRIVER_NAME "tifm_sd"
23 #define DRIVER_VERSION "0.8"
25 static bool no_dma = 0;
26 static bool fixed_timeout = 0;
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
36 #define TIFM_MMCSD_TXDE 0x0080
37 #define TIFM_MMCSD_BUFINT 0x0c00
38 #define TIFM_MMCSD_DPE 0x0020
39 #define TIFM_MMCSD_INAB 0x0080
40 #define TIFM_MMCSD_READ 0x8000
42 #define TIFM_MMCSD_ERRMASK 0x01e0
43 #define TIFM_MMCSD_EOC 0x0001
44 #define TIFM_MMCSD_CD 0x0002
45 #define TIFM_MMCSD_CB 0x0004
46 #define TIFM_MMCSD_BRS 0x0008
47 #define TIFM_MMCSD_EOFB 0x0010
48 #define TIFM_MMCSD_DTO 0x0020
49 #define TIFM_MMCSD_DCRC 0x0040
50 #define TIFM_MMCSD_CTO 0x0080
51 #define TIFM_MMCSD_CCRC 0x0100
52 #define TIFM_MMCSD_AF 0x0400
53 #define TIFM_MMCSD_AE 0x0800
54 #define TIFM_MMCSD_OCRB 0x1000
55 #define TIFM_MMCSD_CIRQ 0x2000
56 #define TIFM_MMCSD_CERR 0x4000
58 #define TIFM_MMCSD_ODTO 0x0040
59 #define TIFM_MMCSD_CARD_RO 0x0200
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
115 unsigned int off,
unsigned int cnt)
119 unsigned int pos = 0,
val;
129 buf[pos++] =
val & 0xff;
135 buf[pos++] = (
val >> 8) & 0xff;
140 static void tifm_sd_write_fifo(
struct tifm_sd *host,
struct page *pg,
141 unsigned int off,
unsigned int cnt)
145 unsigned int pos = 0,
val;
161 val |= (buf[pos++] << 8) & 0xff00;
167 static void tifm_sd_transfer_data(
struct tifm_sd *host)
172 unsigned int p_off, p_cnt;
198 p_cnt =
min(p_cnt, cnt);
199 p_cnt =
min(p_cnt, t_size);
202 tifm_sd_read_fifo(host, pg, p_off, p_cnt);
204 tifm_sd_write_fifo(host, pg, p_off, 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) + src_off;
216 unsigned char *dst_buf =
kmap_atomic(dst) + dst_off;
218 memcpy(dst_buf, src_buf, count);
224 static void tifm_sd_bounce_block(
struct tifm_sd *host,
struct mmc_data *r_data)
227 unsigned int t_size = r_data->
blksz;
228 unsigned int off,
cnt;
229 unsigned int p_off, p_cnt;
247 p_cnt =
min(p_cnt, cnt);
248 p_cnt =
min(p_cnt, t_size);
252 r_data->
blksz - t_size,
255 tifm_sd_copy_page(pg, p_off, sg_page(&host->
bounce_buf),
256 r_data->
blksz - t_size, p_cnt);
263 static int tifm_sd_set_dma_data(
struct tifm_sd *host,
struct mmc_data *r_data)
267 unsigned int dma_len, dma_blk_cnt,
dma_off;
277 tifm_sd_bounce_block(host, r_data);
292 if (dma_len < t_size) {
293 dma_blk_cnt = dma_len / r_data->
blksz;
307 tifm_sd_bounce_block(host, r_data);
318 dev_dbg(&sock->
dev,
"setting dma for %d blocks\n", dma_blk_cnt);
375 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
383 dev_dbg(&sock->
dev,
"executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
403 static void tifm_sd_check_status(
struct tifm_sd *host)
415 if (cmd->
data->error) {
430 if (host->
req->stop) {
438 tifm_sd_exec(host, host->
req->stop);
459 if (host->
req->stop) {
462 tifm_sd_exec(host, host->
req->stop);
476 static void tifm_sd_data_event(
struct tifm_dev *sock)
479 unsigned int fifo_status = 0;
482 spin_lock(&sock->
lock);
483 host = mmc_priv((
struct mmc_host*)tifm_get_drvdata(sock));
485 dev_dbg(&sock->
dev,
"data event: fifo_status %x, flags %x\n",
489 r_data = host->
req->cmd->data;
492 if (tifm_sd_set_dma_data(host, r_data)) {
494 tifm_sd_check_status(host);
500 spin_unlock(&sock->
lock);
504 static void tifm_sd_card_event(
struct tifm_dev *sock)
512 spin_lock(&sock->
lock);
513 host = mmc_priv((
struct mmc_host*)tifm_get_drvdata(sock));
515 dev_dbg(&sock->
dev,
"host event: host_status %x, flags %x\n",
519 cmd = host->
req->cmd;
522 writel(host_status & TIFM_MMCSD_ERRMASK,
540 if (host->
req->stop) {
542 host->
req->stop->error = cmd_error;
545 cmd->
error = cmd_error;
547 tifm_sd_exec(host, host->
req->stop);
551 cmd->
error = cmd_error;
556 tifm_sd_fetch_resp(cmd, sock);
559 tifm_sd_fetch_resp(host->
req->stop,
569 writel(host_status & TIFM_MMCSD_AE,
575 tifm_sd_transfer_data(host);
577 host_status &= ~TIFM_MMCSD_AE;
586 tifm_sd_check_status(host);
590 spin_unlock(&sock->
lock);
593 static void tifm_sd_set_data_timeout(
struct tifm_sd *host,
605 if (data_timeout < 0xffff) {
611 data_timeout = (data_timeout >> 10) + 1;
612 if (data_timeout > 0xffff)
623 struct tifm_sd *host = mmc_priv(mmc);
635 pr_err(
"%s : unfinished request detected\n",
636 dev_name(&sock->
dev));
648 host->
no_dma = no_dma ? 1 : 0;
651 tifm_sd_set_data_timeout(host, r_data);
675 pr_err(
"%s : scatterlist map failed\n",
676 dev_name(&sock->
dev));
685 : PCI_DMA_FROMDEVICE);
687 pr_err(
"%s : scatterlist map failed\n",
688 dev_name(&sock->
dev));
692 : PCI_DMA_FROMDEVICE);
713 tifm_sd_set_dma_data(host, r_data);
726 tifm_sd_exec(host, mrq->
cmd);
727 spin_unlock_irqrestore(&sock->
lock, flags);
731 spin_unlock_irqrestore(&sock->
lock, flags);
735 static void tifm_sd_end_cmd(
unsigned long data)
739 struct mmc_host *mmc = tifm_get_drvdata(sock);
751 pr_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;
782 spin_unlock_irqrestore(&sock->
lock, flags);
786 static void tifm_sd_abort(
unsigned long data)
790 pr_err(
"%s : card failed to respond for a long period of time "
799 struct tifm_sd *host = mmc_priv(mmc);
801 unsigned int clk_div1, clk_div2;
806 dev_dbg(&sock->
dev,
"ios: clock = %u, vdd = %x, bus_mode = %x, "
807 "chip_select = %x, power_mode = %x, bus_width = %x\n",
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)) {
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);
874 spin_unlock_irqrestore(&sock->
lock, flags);
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;
899 for (rc = 32; rc <= 256; rc <<= 1) {
908 pr_err(
"%s : controller failed to reset\n",
909 dev_name(&sock->
dev));
922 for (rc = 16; rc <= 64; rc <<= 1) {
925 if (!(host_status & TIFM_MMCSD_ERRMASK)
934 pr_err(
"%s : card not ready - probe failed on initialization\n",
935 dev_name(&sock->
dev));
940 | TIFM_MMCSD_ERRMASK,
947 static int tifm_sd_probe(
struct tifm_dev *sock)
956 dev_name(&sock->
dev));
964 host = mmc_priv(mmc);
965 tifm_set_drvdata(sock, mmc);
970 (
unsigned long)host);
973 mmc->
ops = &tifm_sd_ops;
976 mmc->
f_min = 20000000 / 60;
977 mmc->
f_max = 24000000;
987 rc = tifm_sd_initialize_host(host);
998 static void tifm_sd_remove(
struct tifm_dev *sock)
1000 struct mmc_host *mmc = tifm_get_drvdata(sock);
1001 struct tifm_sd *host = mmc_priv(mmc);
1002 unsigned long flags;
1008 spin_unlock_irqrestore(&sock->
lock, flags);
1018 if (host->
req->stop)
1022 spin_unlock_irqrestore(&sock->
lock, flags);
1038 struct mmc_host *mmc = tifm_get_drvdata(sock);
1039 struct tifm_sd *host = mmc_priv(mmc);
1042 rc = tifm_sd_initialize_host(host);
1043 dev_dbg(&sock->
dev,
"resume initialize %d\n", rc);
1055 #define tifm_sd_suspend NULL
1056 #define tifm_sd_resume NULL
1069 .id_table = tifm_sd_id_tbl,
1070 .probe = tifm_sd_probe,
1071 .remove = tifm_sd_remove,
1076 static int __init tifm_sd_init(
void)
1081 static void __exit tifm_sd_exit(
void)