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 0x401d /* set bits: CERR, EOFB, BRS, CB, EOC */
40 #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: 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 */
85 OPENDRAIN = 0x0040, /* hardware dependent value */
86 CARD_EVENT = 0x0100, /* hardware dependent value */
87 CARD_RO = 0x0200, /* hardware dependent value */
88 FIFO_EVENT = 0x10000 }; /* hardware dependent value */
95 unsigned int clk_freq;
97 unsigned long timeout_jiffies;
99 struct tasklet_struct finish_tasklet;
100 struct timer_list timer;
101 struct mmc_request *req;
102 wait_queue_head_t notify;
104 size_t written_blocks;
110 static char* tifm_sd_data_buffer(struct mmc_data *data)
112 return page_address(data->sg->page) + data->sg->offset;
115 static int tifm_sd_transfer_data(struct tifm_dev *sock, struct tifm_sd *host,
116 unsigned int host_status)
118 struct mmc_command *cmd = host->req->cmd;
119 unsigned int t_val = 0, cnt = 0;
122 if (host_status & TIFM_MMCSD_BRS) {
123 /* in non-dma rx mode BRS fires when fifo is still not empty */
124 if (no_dma && (cmd->data->flags & MMC_DATA_READ)) {
125 buffer = tifm_sd_data_buffer(host->req->data);
126 while (host->buffer_size > host->buffer_pos) {
127 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
128 buffer[host->buffer_pos++] = t_val & 0xff;
129 buffer[host->buffer_pos++] =
135 buffer = tifm_sd_data_buffer(host->req->data);
136 if ((cmd->data->flags & MMC_DATA_READ) &&
137 (host_status & TIFM_MMCSD_AF)) {
138 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
139 t_val = readl(sock->addr + SOCK_MMCSD_DATA);
140 if (host->buffer_size > host->buffer_pos) {
141 buffer[host->buffer_pos++] =
143 buffer[host->buffer_pos++] =
147 } else if ((cmd->data->flags & MMC_DATA_WRITE)
148 && (host_status & TIFM_MMCSD_AE)) {
149 for (cnt = 0; cnt < TIFM_MMCSD_FIFO_SIZE; cnt++) {
150 if (host->buffer_size > host->buffer_pos) {
151 t_val = buffer[host->buffer_pos++]
153 t_val |= ((buffer[host->buffer_pos++])
156 sock->addr + SOCK_MMCSD_DATA);
164 static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
168 switch (mmc_resp_type(cmd)) {
170 rc |= TIFM_MMCSD_RSP_R0;
173 rc |= TIFM_MMCSD_RSP_BUSY; // deliberate fall-through
175 rc |= TIFM_MMCSD_RSP_R1;
178 rc |= TIFM_MMCSD_RSP_R2;
181 rc |= TIFM_MMCSD_RSP_R3;
187 switch (mmc_cmd_type(cmd)) {
189 rc |= TIFM_MMCSD_CMD_BC;
192 rc |= TIFM_MMCSD_CMD_BCR;
195 rc |= TIFM_MMCSD_CMD_AC;
198 rc |= TIFM_MMCSD_CMD_ADTC;
206 static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
208 struct tifm_dev *sock = host->dev;
209 unsigned int cmd_mask = tifm_sd_op_flags(cmd) |
210 (host->flags & OPENDRAIN);
212 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
213 cmd_mask |= TIFM_MMCSD_READ;
215 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
216 cmd->opcode, cmd->arg, cmd_mask);
218 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
219 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
220 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
223 static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
225 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
226 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
227 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
228 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
229 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
230 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
231 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
232 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
235 static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
236 unsigned int host_status)
238 struct mmc_command *cmd = host->req->cmd;
241 switch (host->state) {
245 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
246 tifm_sd_fetch_resp(cmd, sock);
256 if (tifm_sd_transfer_data(sock, host, host_status)) {
257 if (cmd->data->flags & MMC_DATA_WRITE) {
261 if (host->req->stop) {
262 tifm_sd_exec(host, host->req->stop);
275 if (host_status & TIFM_MMCSD_EOC) {
276 tifm_sd_fetch_resp(host->req->stop, sock);
282 dev_dbg(&sock->dev, "waiting for CARD, have %zd blocks\n",
283 host->written_blocks);
284 if (!(host->flags & CARD_BUSY)
285 && (host->written_blocks == cmd->data->blocks)) {
287 if (host->req->stop) {
288 tifm_sd_exec(host, host->req->stop);
300 if (host->flags & FIFO_RDY) {
301 host->flags &= ~FIFO_RDY;
302 if (host->req->stop) {
303 tifm_sd_exec(host, host->req->stop);
312 tasklet_schedule(&host->finish_tasklet);
318 /* Called from interrupt handler */
319 static void tifm_sd_signal_irq(struct tifm_dev *sock,
320 unsigned int sock_irq_status)
322 struct tifm_sd *host;
323 unsigned int host_status = 0, fifo_status = 0;
326 spin_lock(&sock->lock);
327 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
329 if (sock_irq_status & FIFO_EVENT) {
330 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
331 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
333 host->flags |= fifo_status & FIFO_RDY;
336 if (sock_irq_status & CARD_EVENT) {
337 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
338 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
343 if (host_status & TIFM_MMCSD_ERRMASK) {
344 if (host_status & (TIFM_MMCSD_CTO | TIFM_MMCSD_DTO))
345 error_code = MMC_ERR_TIMEOUT;
347 & (TIFM_MMCSD_CCRC | TIFM_MMCSD_DCRC))
348 error_code = MMC_ERR_BADCRC;
350 writel(TIFM_FIFO_INT_SETALL,
351 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
352 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
354 if (host->req->stop) {
355 if (host->state == SCMD) {
356 host->req->stop->error = error_code;
357 } else if (host->state == BRS
358 || host->state == CARD
359 || host->state == FIFO) {
360 host->req->cmd->error = error_code;
361 tifm_sd_exec(host, host->req->stop);
365 host->req->cmd->error = error_code;
368 host->req->cmd->error = error_code;
373 if (host_status & TIFM_MMCSD_CB)
374 host->flags |= CARD_BUSY;
375 if ((host_status & TIFM_MMCSD_EOFB)
376 && (host->flags & CARD_BUSY)) {
377 host->written_blocks++;
378 host->flags &= ~CARD_BUSY;
383 tifm_sd_process_cmd(sock, host, host_status);
385 dev_dbg(&sock->dev, "host_status %x, fifo_status %x\n",
386 host_status, fifo_status);
387 spin_unlock(&sock->lock);
390 static void tifm_sd_prepare_data(struct tifm_sd *host, struct mmc_command *cmd)
392 struct tifm_dev *sock = host->dev;
393 unsigned int dest_cnt;
396 dev_dbg(&sock->dev, "setting dma for %d blocks\n",
398 writel(TIFM_FIFO_INT_SETALL,
399 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
400 writel(ilog2(cmd->data->blksz) - 2,
401 sock->addr + SOCK_FIFO_PAGE_SIZE);
402 writel(TIFM_FIFO_ENABLE, sock->addr + SOCK_FIFO_CONTROL);
403 writel(TIFM_FIFO_INTMASK, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
405 dest_cnt = (cmd->data->blocks) << 8;
407 writel(sg_dma_address(cmd->data->sg), sock->addr + SOCK_DMA_ADDRESS);
409 writel(cmd->data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
410 writel(cmd->data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
412 if (cmd->data->flags & MMC_DATA_WRITE) {
413 writel(TIFM_MMCSD_TXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
414 writel(dest_cnt | TIFM_DMA_TX | TIFM_DMA_EN,
415 sock->addr + SOCK_DMA_CONTROL);
417 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
418 writel(dest_cnt | TIFM_DMA_EN, sock->addr + SOCK_DMA_CONTROL);
422 static void tifm_sd_set_data_timeout(struct tifm_sd *host,
423 struct mmc_data *data)
425 struct tifm_dev *sock = host->dev;
426 unsigned int data_timeout = data->timeout_clks;
431 data_timeout += data->timeout_ns /
432 ((1000000000UL / host->clk_freq) * host->clk_div);
434 if (data_timeout < 0xffff) {
435 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
436 writel((~TIFM_MMCSD_DPE)
437 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
438 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
440 data_timeout = (data_timeout >> 10) + 1;
441 if (data_timeout > 0xffff)
442 data_timeout = 0; /* set to unlimited */
443 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
444 writel(TIFM_MMCSD_DPE
445 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
446 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
450 static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
452 struct tifm_sd *host = mmc_priv(mmc);
453 struct tifm_dev *sock = host->dev;
456 struct mmc_data *r_data = mrq->cmd->data;
458 spin_lock_irqsave(&sock->lock, flags);
459 if (host->flags & EJECT) {
460 spin_unlock_irqrestore(&sock->lock, flags);
465 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
466 spin_unlock_irqrestore(&sock->lock, flags);
471 tifm_sd_set_data_timeout(host, r_data);
473 sg_count = tifm_map_sg(sock, r_data->sg, r_data->sg_len,
474 mrq->cmd->flags & MMC_DATA_WRITE
475 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
477 printk(KERN_ERR DRIVER_NAME
478 ": scatterlist map failed\n");
479 spin_unlock_irqrestore(&sock->lock, flags);
483 host->written_blocks = 0;
484 host->flags &= ~CARD_BUSY;
485 tifm_sd_prepare_data(host, mrq->cmd);
489 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
491 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
492 sock->addr + SOCK_CONTROL);
493 tifm_sd_exec(host, mrq->cmd);
494 spin_unlock_irqrestore(&sock->lock, flags);
499 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
500 (r_data->flags & MMC_DATA_WRITE)
501 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
503 mrq->cmd->error = MMC_ERR_TIMEOUT;
504 mmc_request_done(mmc, mrq);
507 static void tifm_sd_end_cmd(unsigned long data)
509 struct tifm_sd *host = (struct tifm_sd*)data;
510 struct tifm_dev *sock = host->dev;
511 struct mmc_host *mmc = tifm_get_drvdata(sock);
512 struct mmc_request *mrq;
513 struct mmc_data *r_data = NULL;
516 spin_lock_irqsave(&sock->lock, flags);
518 del_timer(&host->timer);
524 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
525 spin_unlock_irqrestore(&sock->lock, flags);
529 r_data = mrq->cmd->data;
531 if (r_data->flags & MMC_DATA_WRITE) {
532 r_data->bytes_xfered = host->written_blocks
535 r_data->bytes_xfered = r_data->blocks -
536 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
537 r_data->bytes_xfered *= r_data->blksz;
538 r_data->bytes_xfered += r_data->blksz -
539 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
541 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
542 (r_data->flags & MMC_DATA_WRITE)
543 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
546 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
547 sock->addr + SOCK_CONTROL);
549 spin_unlock_irqrestore(&sock->lock, flags);
550 mmc_request_done(mmc, mrq);
553 static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
555 struct tifm_sd *host = mmc_priv(mmc);
556 struct tifm_dev *sock = host->dev;
558 struct mmc_data *r_data = mrq->cmd->data;
560 spin_lock_irqsave(&sock->lock, flags);
561 if (host->flags & EJECT) {
562 spin_unlock_irqrestore(&sock->lock, flags);
567 printk(KERN_ERR DRIVER_NAME ": unfinished request detected\n");
568 spin_unlock_irqrestore(&sock->lock, flags);
573 tifm_sd_set_data_timeout(host, r_data);
575 host->buffer_size = mrq->cmd->data->blocks
576 * mrq->cmd->data->blksz;
578 writel(TIFM_MMCSD_BUFINT
579 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
580 sock->addr + SOCK_MMCSD_INT_ENABLE);
581 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
582 | (TIFM_MMCSD_FIFO_SIZE - 1),
583 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
585 host->written_blocks = 0;
586 host->flags &= ~CARD_BUSY;
587 host->buffer_pos = 0;
588 writel(r_data->blocks - 1, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
589 writel(r_data->blksz - 1, sock->addr + SOCK_MMCSD_BLOCK_LEN);
593 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
595 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
596 sock->addr + SOCK_CONTROL);
597 tifm_sd_exec(host, mrq->cmd);
598 spin_unlock_irqrestore(&sock->lock, flags);
602 mrq->cmd->error = MMC_ERR_TIMEOUT;
603 mmc_request_done(mmc, mrq);
606 static void tifm_sd_end_cmd_nodma(unsigned long data)
608 struct tifm_sd *host = (struct tifm_sd*)data;
609 struct tifm_dev *sock = host->dev;
610 struct mmc_host *mmc = tifm_get_drvdata(sock);
611 struct mmc_request *mrq;
612 struct mmc_data *r_data = NULL;
615 spin_lock_irqsave(&sock->lock, flags);
617 del_timer(&host->timer);
623 printk(KERN_ERR DRIVER_NAME ": no request to complete?\n");
624 spin_unlock_irqrestore(&sock->lock, flags);
628 r_data = mrq->cmd->data;
630 writel((~TIFM_MMCSD_BUFINT) &
631 readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
632 sock->addr + SOCK_MMCSD_INT_ENABLE);
634 if (r_data->flags & MMC_DATA_WRITE) {
635 r_data->bytes_xfered = host->written_blocks
638 r_data->bytes_xfered = r_data->blocks -
639 readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
640 r_data->bytes_xfered *= r_data->blksz;
641 r_data->bytes_xfered += r_data->blksz -
642 readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
644 host->buffer_pos = 0;
645 host->buffer_size = 0;
648 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
649 sock->addr + SOCK_CONTROL);
651 spin_unlock_irqrestore(&sock->lock, flags);
653 mmc_request_done(mmc, mrq);
656 static void tifm_sd_terminate(struct tifm_sd *host)
658 struct tifm_dev *sock = host->dev;
661 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
663 spin_lock_irqsave(&sock->lock, flags);
664 host->flags |= EJECT;
666 writel(TIFM_FIFO_INT_SETALL,
667 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
668 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
669 tasklet_schedule(&host->finish_tasklet);
671 spin_unlock_irqrestore(&sock->lock, flags);
674 static void tifm_sd_abort(unsigned long data)
676 struct tifm_sd *host = (struct tifm_sd*)data;
678 printk(KERN_ERR DRIVER_NAME
679 ": card failed to respond for a long period of time");
681 tifm_sd_terminate(host);
682 tifm_eject(host->dev);
685 static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
687 struct tifm_sd *host = mmc_priv(mmc);
688 struct tifm_dev *sock = host->dev;
689 unsigned int clk_div1, clk_div2;
692 spin_lock_irqsave(&sock->lock, flags);
694 dev_dbg(&sock->dev, "Setting bus width %d, power %d\n", ios->bus_width,
696 if (ios->bus_width == MMC_BUS_WIDTH_4) {
697 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
698 sock->addr + SOCK_MMCSD_CONFIG);
700 writel((~TIFM_MMCSD_4BBUS)
701 & readl(sock->addr + SOCK_MMCSD_CONFIG),
702 sock->addr + SOCK_MMCSD_CONFIG);
706 clk_div1 = 20000000 / ios->clock;
710 clk_div2 = 24000000 / ios->clock;
714 if ((20000000 / clk_div1) > ios->clock)
716 if ((24000000 / clk_div2) > ios->clock)
718 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
719 host->clk_freq = 20000000;
720 host->clk_div = clk_div1;
721 writel((~TIFM_CTRL_FAST_CLK)
722 & readl(sock->addr + SOCK_CONTROL),
723 sock->addr + SOCK_CONTROL);
725 host->clk_freq = 24000000;
726 host->clk_div = clk_div2;
727 writel(TIFM_CTRL_FAST_CLK
728 | readl(sock->addr + SOCK_CONTROL),
729 sock->addr + SOCK_CONTROL);
734 host->clk_div &= TIFM_MMCSD_CLKMASK;
736 | ((~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->notify);
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 int tifm_sd_initialize_host(struct tifm_sd *host)
783 unsigned int host_status = 0;
784 struct tifm_dev *sock = host->dev;
786 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
789 host->clk_freq = 20000000;
790 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
791 writel(host->clk_div | TIFM_MMCSD_POWER,
792 sock->addr + SOCK_MMCSD_CONFIG);
794 /* wait up to 0.51 sec for reset */
795 for (rc = 2; rc <= 256; rc <<= 1) {
796 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
804 printk(KERN_ERR DRIVER_NAME
805 ": controller failed to reset\n");
809 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
810 writel(host->clk_div | TIFM_MMCSD_POWER,
811 sock->addr + SOCK_MMCSD_CONFIG);
812 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
814 // command timeout fixed to 64 clocks for now
815 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
816 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
818 /* INAB should take much less than reset */
819 for (rc = 1; rc <= 16; rc <<= 1) {
820 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
821 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
822 if (!(host_status & TIFM_MMCSD_ERRMASK)
823 && (host_status & TIFM_MMCSD_EOC)) {
831 printk(KERN_ERR DRIVER_NAME
832 ": card not ready - probe failed on initialization\n");
836 writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
837 sock->addr + SOCK_MMCSD_INT_ENABLE);
843 static int tifm_sd_probe(struct tifm_dev *sock)
845 struct mmc_host *mmc;
846 struct tifm_sd *host;
849 if (!(TIFM_SOCK_STATE_OCCUPIED
850 & readl(sock->addr + SOCK_PRESENT_STATE))) {
851 printk(KERN_WARNING DRIVER_NAME ": card gone, unexpectedly\n");
855 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
859 host = mmc_priv(mmc);
860 tifm_set_drvdata(sock, mmc);
862 host->timeout_jiffies = msecs_to_jiffies(1000);
864 init_waitqueue_head(&host->notify);
865 tasklet_init(&host->finish_tasklet,
866 no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
867 (unsigned long)host);
868 setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
870 tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
871 mmc->ops = &tifm_sd_ops;
872 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
873 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
874 mmc->f_min = 20000000 / 60;
875 mmc->f_max = 24000000;
876 mmc->max_hw_segs = 1;
877 mmc->max_phys_segs = 1;
878 // limited by DMA counter - it's safer to stick with
879 // block counter has 11 bits though
880 mmc->max_blk_count = 256;
881 // 2k maximum hw block length
882 mmc->max_blk_size = 2048;
883 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
884 mmc->max_seg_size = mmc->max_req_size;
885 sock->signal_irq = tifm_sd_signal_irq;
886 rc = tifm_sd_initialize_host(host);
889 rc = mmc_add_host(mmc);
899 static void tifm_sd_remove(struct tifm_dev *sock)
901 struct mmc_host *mmc = tifm_get_drvdata(sock);
902 struct tifm_sd *host = mmc_priv(mmc);
904 del_timer_sync(&host->timer);
905 tifm_sd_terminate(host);
906 wait_event_timeout(host->notify, host->flags & EJECT_DONE,
907 host->timeout_jiffies);
908 tasklet_kill(&host->finish_tasklet);
909 mmc_remove_host(mmc);
911 /* The meaning of the bit majority in this constant is unknown. */
912 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
913 sock->addr + SOCK_CONTROL);
915 tifm_set_drvdata(sock, NULL);
921 static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
923 struct mmc_host *mmc = tifm_get_drvdata(sock);
926 rc = mmc_suspend_host(mmc, state);
927 /* The meaning of the bit majority in this constant is unknown. */
928 writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
929 sock->addr + SOCK_CONTROL);
933 static int tifm_sd_resume(struct tifm_dev *sock)
935 struct mmc_host *mmc = tifm_get_drvdata(sock);
936 struct tifm_sd *host = mmc_priv(mmc);
938 if (sock->media_id != FM_SD
939 || tifm_sd_initialize_host(host)) {
943 return mmc_resume_host(mmc);
949 #define tifm_sd_suspend NULL
950 #define tifm_sd_resume NULL
952 #endif /* CONFIG_PM */
954 static tifm_media_id tifm_sd_id_tbl[] = {
958 static struct tifm_driver tifm_sd_driver = {
963 .id_table = tifm_sd_id_tbl,
964 .probe = tifm_sd_probe,
965 .remove = tifm_sd_remove,
966 .suspend = tifm_sd_suspend,
967 .resume = tifm_sd_resume
970 static int __init tifm_sd_init(void)
972 return tifm_register_driver(&tifm_sd_driver);
975 static void __exit tifm_sd_exit(void)
977 tifm_unregister_driver(&tifm_sd_driver);
980 MODULE_AUTHOR("Alex Dubov");
981 MODULE_DESCRIPTION("TI FlashMedia SD driver");
982 MODULE_LICENSE("GPL");
983 MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
984 MODULE_VERSION(DRIVER_VERSION);
986 module_init(tifm_sd_init);
987 module_exit(tifm_sd_exit);