1 // SPDX-License-Identifier: GPL-2.0-only
3 * tifm_sd.c - TI FlashMedia driver
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>
18 #define DRIVER_NAME "tifm_sd"
19 #define DRIVER_VERSION "0.8"
21 static bool no_dma = 0;
22 static bool fixed_timeout = 0;
23 module_param(no_dma, bool, 0644);
24 module_param(fixed_timeout, bool, 0644);
26 /* Constants here are mostly from OMAP5912 datasheet */
27 #define TIFM_MMCSD_RESET 0x0002
28 #define TIFM_MMCSD_CLKMASK 0x03ff
29 #define TIFM_MMCSD_POWER 0x0800
30 #define TIFM_MMCSD_4BBUS 0x8000
31 #define TIFM_MMCSD_RXDE 0x8000 /* rx dma enable */
32 #define TIFM_MMCSD_TXDE 0x0080 /* tx dma enable */
33 #define TIFM_MMCSD_BUFINT 0x0c00 /* set bits: AE, AF */
34 #define TIFM_MMCSD_DPE 0x0020 /* data timeout counted in kilocycles */
35 #define TIFM_MMCSD_INAB 0x0080 /* abort / initialize command */
36 #define TIFM_MMCSD_READ 0x8000
38 #define TIFM_MMCSD_ERRMASK 0x01e0 /* set bits: CCRC, CTO, DCRC, DTO */
39 #define TIFM_MMCSD_EOC 0x0001 /* end of command phase */
40 #define TIFM_MMCSD_CD 0x0002 /* card detect */
41 #define TIFM_MMCSD_CB 0x0004 /* card enter busy state */
42 #define TIFM_MMCSD_BRS 0x0008 /* block received/sent */
43 #define TIFM_MMCSD_EOFB 0x0010 /* card exit busy state */
44 #define TIFM_MMCSD_DTO 0x0020 /* data time-out */
45 #define TIFM_MMCSD_DCRC 0x0040 /* data crc error */
46 #define TIFM_MMCSD_CTO 0x0080 /* command time-out */
47 #define TIFM_MMCSD_CCRC 0x0100 /* command crc error */
48 #define TIFM_MMCSD_AF 0x0400 /* fifo almost full */
49 #define TIFM_MMCSD_AE 0x0800 /* fifo almost empty */
50 #define TIFM_MMCSD_OCRB 0x1000 /* OCR busy */
51 #define TIFM_MMCSD_CIRQ 0x2000 /* card irq (cmd40/sdio) */
52 #define TIFM_MMCSD_CERR 0x4000 /* card status error */
54 #define TIFM_MMCSD_ODTO 0x0040 /* open drain / extended timeout */
55 #define TIFM_MMCSD_CARD_RO 0x0200 /* card is read-only */
57 #define TIFM_MMCSD_FIFO_SIZE 0x0020
59 #define TIFM_MMCSD_RSP_R0 0x0000
60 #define TIFM_MMCSD_RSP_R1 0x0100
61 #define TIFM_MMCSD_RSP_R2 0x0200
62 #define TIFM_MMCSD_RSP_R3 0x0300
63 #define TIFM_MMCSD_RSP_R4 0x0400
64 #define TIFM_MMCSD_RSP_R5 0x0500
65 #define TIFM_MMCSD_RSP_R6 0x0600
67 #define TIFM_MMCSD_RSP_BUSY 0x0800
69 #define TIFM_MMCSD_CMD_BC 0x0000
70 #define TIFM_MMCSD_CMD_BCR 0x1000
71 #define TIFM_MMCSD_CMD_AC 0x2000
72 #define TIFM_MMCSD_CMD_ADTC 0x3000
74 #define TIFM_MMCSD_MAX_BLOCK_SIZE 0x0800UL
89 unsigned short eject:1,
92 unsigned short cmd_flags;
94 unsigned int clk_freq;
96 unsigned long timeout_jiffies;
98 struct tasklet_struct finish_tasklet;
99 struct timer_list timer;
100 struct mmc_request *req;
104 unsigned int block_pos;
105 struct scatterlist bounce_buf;
106 unsigned char bounce_buf_data[TIFM_MMCSD_MAX_BLOCK_SIZE];
109 /* for some reason, host won't respond correctly to readw/writew */
110 static void tifm_sd_read_fifo(struct tifm_sd *host, struct page *pg,
111 unsigned int off, unsigned int cnt)
113 struct tifm_dev *sock = host->dev;
115 unsigned int pos = 0, val;
117 buf = kmap_atomic(pg) + off;
118 if (host->cmd_flags & DATA_CARRY) {
119 buf[pos++] = host->bounce_buf_data[0];
120 host->cmd_flags &= ~DATA_CARRY;
124 val = readl(sock->addr + SOCK_MMCSD_DATA);
125 buf[pos++] = val & 0xff;
127 host->bounce_buf_data[0] = (val >> 8) & 0xff;
128 host->cmd_flags |= DATA_CARRY;
131 buf[pos++] = (val >> 8) & 0xff;
133 kunmap_atomic(buf - off);
136 static void tifm_sd_write_fifo(struct tifm_sd *host, struct page *pg,
137 unsigned int off, unsigned int cnt)
139 struct tifm_dev *sock = host->dev;
141 unsigned int pos = 0, val;
143 buf = kmap_atomic(pg) + off;
144 if (host->cmd_flags & DATA_CARRY) {
145 val = host->bounce_buf_data[0] | ((buf[pos++] << 8) & 0xff00);
146 writel(val, sock->addr + SOCK_MMCSD_DATA);
147 host->cmd_flags &= ~DATA_CARRY;
153 host->bounce_buf_data[0] = val & 0xff;
154 host->cmd_flags |= DATA_CARRY;
157 val |= (buf[pos++] << 8) & 0xff00;
158 writel(val, sock->addr + SOCK_MMCSD_DATA);
160 kunmap_atomic(buf - off);
163 static void tifm_sd_transfer_data(struct tifm_sd *host)
165 struct mmc_data *r_data = host->req->cmd->data;
166 struct scatterlist *sg = r_data->sg;
167 unsigned int off, cnt, t_size = TIFM_MMCSD_FIFO_SIZE * 2;
168 unsigned int p_off, p_cnt;
171 if (host->sg_pos == host->sg_len)
174 cnt = sg[host->sg_pos].length - host->block_pos;
178 if (host->sg_pos == host->sg_len) {
179 if ((r_data->flags & MMC_DATA_WRITE)
180 && (host->cmd_flags & DATA_CARRY))
181 writel(host->bounce_buf_data[0],
187 cnt = sg[host->sg_pos].length;
189 off = sg[host->sg_pos].offset + host->block_pos;
191 pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT);
192 p_off = offset_in_page(off);
193 p_cnt = PAGE_SIZE - p_off;
194 p_cnt = min(p_cnt, cnt);
195 p_cnt = min(p_cnt, t_size);
197 if (r_data->flags & MMC_DATA_READ)
198 tifm_sd_read_fifo(host, pg, p_off, p_cnt);
199 else if (r_data->flags & MMC_DATA_WRITE)
200 tifm_sd_write_fifo(host, pg, p_off, p_cnt);
203 host->block_pos += p_cnt;
207 static void tifm_sd_copy_page(struct page *dst, unsigned int dst_off,
208 struct page *src, unsigned int src_off,
211 unsigned char *src_buf = kmap_atomic(src) + src_off;
212 unsigned char *dst_buf = kmap_atomic(dst) + dst_off;
214 memcpy(dst_buf, src_buf, count);
216 kunmap_atomic(dst_buf - dst_off);
217 kunmap_atomic(src_buf - src_off);
220 static void tifm_sd_bounce_block(struct tifm_sd *host, struct mmc_data *r_data)
222 struct scatterlist *sg = r_data->sg;
223 unsigned int t_size = r_data->blksz;
224 unsigned int off, cnt;
225 unsigned int p_off, p_cnt;
228 dev_dbg(&host->dev->dev, "bouncing block\n");
230 cnt = sg[host->sg_pos].length - host->block_pos;
234 if (host->sg_pos == host->sg_len)
236 cnt = sg[host->sg_pos].length;
238 off = sg[host->sg_pos].offset + host->block_pos;
240 pg = nth_page(sg_page(&sg[host->sg_pos]), off >> PAGE_SHIFT);
241 p_off = offset_in_page(off);
242 p_cnt = PAGE_SIZE - p_off;
243 p_cnt = min(p_cnt, cnt);
244 p_cnt = min(p_cnt, t_size);
246 if (r_data->flags & MMC_DATA_WRITE)
247 tifm_sd_copy_page(sg_page(&host->bounce_buf),
248 r_data->blksz - t_size,
250 else if (r_data->flags & MMC_DATA_READ)
251 tifm_sd_copy_page(pg, p_off, sg_page(&host->bounce_buf),
252 r_data->blksz - t_size, p_cnt);
255 host->block_pos += p_cnt;
259 static int tifm_sd_set_dma_data(struct tifm_sd *host, struct mmc_data *r_data)
261 struct tifm_dev *sock = host->dev;
262 unsigned int t_size = TIFM_DMA_TSIZE * r_data->blksz;
263 unsigned int dma_len, dma_blk_cnt, dma_off;
264 struct scatterlist *sg = NULL;
267 if (host->sg_pos == host->sg_len)
270 if (host->cmd_flags & DATA_CARRY) {
271 host->cmd_flags &= ~DATA_CARRY;
272 local_irq_save(flags);
273 tifm_sd_bounce_block(host, r_data);
274 local_irq_restore(flags);
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 local_irq_save(flags);
303 tifm_sd_bounce_block(host, r_data);
304 local_irq_restore(flags);
306 host->cmd_flags |= DATA_CARRY;
308 sg = &host->bounce_buf;
314 dev_dbg(&sock->dev, "setting dma for %d blocks\n", dma_blk_cnt);
315 writel(sg_dma_address(sg) + dma_off, sock->addr + SOCK_DMA_ADDRESS);
316 if (r_data->flags & MMC_DATA_WRITE)
317 writel((dma_blk_cnt << 8) | TIFM_DMA_TX | TIFM_DMA_EN,
318 sock->addr + SOCK_DMA_CONTROL);
320 writel((dma_blk_cnt << 8) | TIFM_DMA_EN,
321 sock->addr + SOCK_DMA_CONTROL);
326 static unsigned int tifm_sd_op_flags(struct mmc_command *cmd)
330 switch (mmc_resp_type(cmd)) {
332 rc |= TIFM_MMCSD_RSP_R0;
335 rc |= TIFM_MMCSD_RSP_BUSY;
338 rc |= TIFM_MMCSD_RSP_R1;
341 rc |= TIFM_MMCSD_RSP_R2;
344 rc |= TIFM_MMCSD_RSP_R3;
350 switch (mmc_cmd_type(cmd)) {
352 rc |= TIFM_MMCSD_CMD_BC;
355 rc |= TIFM_MMCSD_CMD_BCR;
358 rc |= TIFM_MMCSD_CMD_AC;
361 rc |= TIFM_MMCSD_CMD_ADTC;
369 static void tifm_sd_exec(struct tifm_sd *host, struct mmc_command *cmd)
371 struct tifm_dev *sock = host->dev;
372 unsigned int cmd_mask = tifm_sd_op_flags(cmd);
374 if (host->open_drain)
375 cmd_mask |= TIFM_MMCSD_ODTO;
377 if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
378 cmd_mask |= TIFM_MMCSD_READ;
380 dev_dbg(&sock->dev, "executing opcode 0x%x, arg: 0x%x, mask: 0x%x\n",
381 cmd->opcode, cmd->arg, cmd_mask);
383 writel((cmd->arg >> 16) & 0xffff, sock->addr + SOCK_MMCSD_ARG_HIGH);
384 writel(cmd->arg & 0xffff, sock->addr + SOCK_MMCSD_ARG_LOW);
385 writel(cmd->opcode | cmd_mask, sock->addr + SOCK_MMCSD_COMMAND);
388 static void tifm_sd_fetch_resp(struct mmc_command *cmd, struct tifm_dev *sock)
390 cmd->resp[0] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x1c) << 16)
391 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x18);
392 cmd->resp[1] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x14) << 16)
393 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x10);
394 cmd->resp[2] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x0c) << 16)
395 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x08);
396 cmd->resp[3] = (readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x04) << 16)
397 | readl(sock->addr + SOCK_MMCSD_RESPONSE + 0x00);
400 static void tifm_sd_check_status(struct tifm_sd *host)
402 struct tifm_dev *sock = host->dev;
403 struct mmc_command *cmd = host->req->cmd;
408 if (!(host->cmd_flags & CMD_READY))
412 if (cmd->data->error) {
413 if ((host->cmd_flags & SCMD_ACTIVE)
414 && !(host->cmd_flags & SCMD_READY))
420 if (!(host->cmd_flags & BRS_READY))
423 if (!(host->no_dma || (host->cmd_flags & FIFO_READY)))
426 if (cmd->data->flags & MMC_DATA_WRITE) {
427 if (host->req->stop) {
428 if (!(host->cmd_flags & SCMD_ACTIVE)) {
429 host->cmd_flags |= SCMD_ACTIVE;
430 writel(TIFM_MMCSD_EOFB
432 + SOCK_MMCSD_INT_ENABLE),
434 + SOCK_MMCSD_INT_ENABLE);
435 tifm_sd_exec(host, host->req->stop);
438 if (!(host->cmd_flags & SCMD_READY)
439 || (host->cmd_flags & CARD_BUSY))
441 writel((~TIFM_MMCSD_EOFB)
443 + SOCK_MMCSD_INT_ENABLE),
445 + SOCK_MMCSD_INT_ENABLE);
448 if (host->cmd_flags & CARD_BUSY)
450 writel((~TIFM_MMCSD_EOFB)
452 + SOCK_MMCSD_INT_ENABLE),
453 sock->addr + SOCK_MMCSD_INT_ENABLE);
456 if (host->req->stop) {
457 if (!(host->cmd_flags & SCMD_ACTIVE)) {
458 host->cmd_flags |= SCMD_ACTIVE;
459 tifm_sd_exec(host, host->req->stop);
462 if (!(host->cmd_flags & SCMD_READY))
469 tasklet_schedule(&host->finish_tasklet);
472 /* Called from interrupt handler */
473 static void tifm_sd_data_event(struct tifm_dev *sock)
475 struct tifm_sd *host;
476 unsigned int fifo_status = 0;
477 struct mmc_data *r_data = NULL;
479 spin_lock(&sock->lock);
480 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
481 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
482 dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n",
483 fifo_status, host->cmd_flags);
486 r_data = host->req->cmd->data;
488 if (r_data && (fifo_status & TIFM_FIFO_READY)) {
489 if (tifm_sd_set_dma_data(host, r_data)) {
490 host->cmd_flags |= FIFO_READY;
491 tifm_sd_check_status(host);
496 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
497 spin_unlock(&sock->lock);
500 /* Called from interrupt handler */
501 static void tifm_sd_card_event(struct tifm_dev *sock)
503 struct tifm_sd *host;
504 unsigned int host_status = 0;
506 struct mmc_command *cmd = NULL;
509 spin_lock(&sock->lock);
510 host = mmc_priv((struct mmc_host*)tifm_get_drvdata(sock));
511 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
512 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
513 host_status, host->cmd_flags);
516 cmd = host->req->cmd;
518 if (host_status & TIFM_MMCSD_ERRMASK) {
519 writel(host_status & TIFM_MMCSD_ERRMASK,
520 sock->addr + SOCK_MMCSD_STATUS);
521 if (host_status & TIFM_MMCSD_CTO)
522 cmd_error = -ETIMEDOUT;
523 else if (host_status & TIFM_MMCSD_CCRC)
527 if (host_status & TIFM_MMCSD_DTO)
528 cmd->data->error = -ETIMEDOUT;
529 else if (host_status & TIFM_MMCSD_DCRC)
530 cmd->data->error = -EILSEQ;
533 writel(TIFM_FIFO_INT_SETALL,
534 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
535 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
537 if (host->req->stop) {
538 if (host->cmd_flags & SCMD_ACTIVE) {
539 host->req->stop->error = cmd_error;
540 host->cmd_flags |= SCMD_READY;
542 cmd->error = cmd_error;
543 host->cmd_flags |= SCMD_ACTIVE;
544 tifm_sd_exec(host, host->req->stop);
548 cmd->error = cmd_error;
550 if (host_status & (TIFM_MMCSD_EOC | TIFM_MMCSD_CERR)) {
551 if (!(host->cmd_flags & CMD_READY)) {
552 host->cmd_flags |= CMD_READY;
553 tifm_sd_fetch_resp(cmd, sock);
554 } else if (host->cmd_flags & SCMD_ACTIVE) {
555 host->cmd_flags |= SCMD_READY;
556 tifm_sd_fetch_resp(host->req->stop,
560 if (host_status & TIFM_MMCSD_BRS)
561 host->cmd_flags |= BRS_READY;
564 if (host->no_dma && cmd->data) {
565 if (host_status & TIFM_MMCSD_AE)
566 writel(host_status & TIFM_MMCSD_AE,
567 sock->addr + SOCK_MMCSD_STATUS);
569 if (host_status & (TIFM_MMCSD_AE | TIFM_MMCSD_AF
571 local_irq_save(flags);
572 tifm_sd_transfer_data(host);
573 local_irq_restore(flags);
574 host_status &= ~TIFM_MMCSD_AE;
578 if (host_status & TIFM_MMCSD_EOFB)
579 host->cmd_flags &= ~CARD_BUSY;
580 else if (host_status & TIFM_MMCSD_CB)
581 host->cmd_flags |= CARD_BUSY;
583 tifm_sd_check_status(host);
586 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
587 spin_unlock(&sock->lock);
590 static void tifm_sd_set_data_timeout(struct tifm_sd *host,
591 struct mmc_data *data)
593 struct tifm_dev *sock = host->dev;
594 unsigned int data_timeout = data->timeout_clks;
599 data_timeout += data->timeout_ns /
600 ((1000000000UL / host->clk_freq) * host->clk_div);
602 if (data_timeout < 0xffff) {
603 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
604 writel((~TIFM_MMCSD_DPE)
605 & readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
606 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
608 data_timeout = (data_timeout >> 10) + 1;
609 if (data_timeout > 0xffff)
610 data_timeout = 0; /* set to unlimited */
611 writel(data_timeout, sock->addr + SOCK_MMCSD_DATA_TO);
612 writel(TIFM_MMCSD_DPE
613 | readl(sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG),
614 sock->addr + SOCK_MMCSD_SDIO_MODE_CONFIG);
618 static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
620 struct tifm_sd *host = mmc_priv(mmc);
621 struct tifm_dev *sock = host->dev;
623 struct mmc_data *r_data = mrq->cmd->data;
625 spin_lock_irqsave(&sock->lock, flags);
627 mrq->cmd->error = -ENOMEDIUM;
632 pr_err("%s : unfinished request detected\n",
633 dev_name(&sock->dev));
634 mrq->cmd->error = -ETIMEDOUT;
642 if (mrq->data && !is_power_of_2(mrq->data->blksz))
645 host->no_dma = no_dma ? 1 : 0;
648 tifm_sd_set_data_timeout(host, r_data);
650 if ((r_data->flags & MMC_DATA_WRITE) && !mrq->stop)
651 writel(TIFM_MMCSD_EOFB
652 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
653 sock->addr + SOCK_MMCSD_INT_ENABLE);
656 writel(TIFM_MMCSD_BUFINT
657 | readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
658 sock->addr + SOCK_MMCSD_INT_ENABLE);
659 writel(((TIFM_MMCSD_FIFO_SIZE - 1) << 8)
660 | (TIFM_MMCSD_FIFO_SIZE - 1),
661 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
663 host->sg_len = r_data->sg_len;
665 sg_init_one(&host->bounce_buf, host->bounce_buf_data,
668 if(1 != tifm_map_sg(sock, &host->bounce_buf, 1,
669 r_data->flags & MMC_DATA_WRITE
671 : PCI_DMA_FROMDEVICE)) {
672 pr_err("%s : scatterlist map failed\n",
673 dev_name(&sock->dev));
674 mrq->cmd->error = -ENOMEM;
677 host->sg_len = tifm_map_sg(sock, r_data->sg,
682 : PCI_DMA_FROMDEVICE);
683 if (host->sg_len < 1) {
684 pr_err("%s : scatterlist map failed\n",
685 dev_name(&sock->dev));
686 tifm_unmap_sg(sock, &host->bounce_buf, 1,
687 r_data->flags & MMC_DATA_WRITE
689 : PCI_DMA_FROMDEVICE);
690 mrq->cmd->error = -ENOMEM;
694 writel(TIFM_FIFO_INT_SETALL,
695 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
696 writel(ilog2(r_data->blksz) - 2,
697 sock->addr + SOCK_FIFO_PAGE_SIZE);
698 writel(TIFM_FIFO_ENABLE,
699 sock->addr + SOCK_FIFO_CONTROL);
700 writel(TIFM_FIFO_INTMASK,
701 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
703 if (r_data->flags & MMC_DATA_WRITE)
704 writel(TIFM_MMCSD_TXDE,
705 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
707 writel(TIFM_MMCSD_RXDE,
708 sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
710 tifm_sd_set_dma_data(host, r_data);
713 writel(r_data->blocks - 1,
714 sock->addr + SOCK_MMCSD_NUM_BLOCKS);
715 writel(r_data->blksz - 1,
716 sock->addr + SOCK_MMCSD_BLOCK_LEN);
720 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
721 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
722 sock->addr + SOCK_CONTROL);
723 tifm_sd_exec(host, mrq->cmd);
724 spin_unlock_irqrestore(&sock->lock, flags);
728 spin_unlock_irqrestore(&sock->lock, flags);
729 mmc_request_done(mmc, mrq);
732 static void tifm_sd_end_cmd(unsigned long data)
734 struct tifm_sd *host = (struct tifm_sd*)data;
735 struct tifm_dev *sock = host->dev;
736 struct mmc_host *mmc = tifm_get_drvdata(sock);
737 struct mmc_request *mrq;
738 struct mmc_data *r_data = NULL;
741 spin_lock_irqsave(&sock->lock, flags);
743 del_timer(&host->timer);
748 pr_err(" %s : no request to complete?\n",
749 dev_name(&sock->dev));
750 spin_unlock_irqrestore(&sock->lock, flags);
754 r_data = mrq->cmd->data;
757 writel((~TIFM_MMCSD_BUFINT)
758 & readl(sock->addr + SOCK_MMCSD_INT_ENABLE),
759 sock->addr + SOCK_MMCSD_INT_ENABLE);
761 tifm_unmap_sg(sock, &host->bounce_buf, 1,
762 (r_data->flags & MMC_DATA_WRITE)
763 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
764 tifm_unmap_sg(sock, r_data->sg, r_data->sg_len,
765 (r_data->flags & MMC_DATA_WRITE)
766 ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
769 r_data->bytes_xfered = r_data->blocks
770 - readl(sock->addr + SOCK_MMCSD_NUM_BLOCKS) - 1;
771 r_data->bytes_xfered *= r_data->blksz;
772 r_data->bytes_xfered += r_data->blksz
773 - readl(sock->addr + SOCK_MMCSD_BLOCK_LEN) + 1;
776 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
777 sock->addr + SOCK_CONTROL);
779 spin_unlock_irqrestore(&sock->lock, flags);
780 mmc_request_done(mmc, mrq);
783 static void tifm_sd_abort(struct timer_list *t)
785 struct tifm_sd *host = from_timer(host, t, timer);
787 pr_err("%s : card failed to respond for a long period of time "
789 dev_name(&host->dev->dev), host->req->cmd->opcode, host->cmd_flags);
791 tifm_eject(host->dev);
794 static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
796 struct tifm_sd *host = mmc_priv(mmc);
797 struct tifm_dev *sock = host->dev;
798 unsigned int clk_div1, clk_div2;
801 spin_lock_irqsave(&sock->lock, flags);
803 dev_dbg(&sock->dev, "ios: clock = %u, vdd = %x, bus_mode = %x, "
804 "chip_select = %x, power_mode = %x, bus_width = %x\n",
805 ios->clock, ios->vdd, ios->bus_mode, ios->chip_select,
806 ios->power_mode, ios->bus_width);
808 if (ios->bus_width == MMC_BUS_WIDTH_4) {
809 writel(TIFM_MMCSD_4BBUS | readl(sock->addr + SOCK_MMCSD_CONFIG),
810 sock->addr + SOCK_MMCSD_CONFIG);
812 writel((~TIFM_MMCSD_4BBUS)
813 & readl(sock->addr + SOCK_MMCSD_CONFIG),
814 sock->addr + SOCK_MMCSD_CONFIG);
818 clk_div1 = 20000000 / ios->clock;
822 clk_div2 = 24000000 / ios->clock;
826 if ((20000000 / clk_div1) > ios->clock)
828 if ((24000000 / clk_div2) > ios->clock)
830 if ((20000000 / clk_div1) > (24000000 / clk_div2)) {
831 host->clk_freq = 20000000;
832 host->clk_div = clk_div1;
833 writel((~TIFM_CTRL_FAST_CLK)
834 & readl(sock->addr + SOCK_CONTROL),
835 sock->addr + SOCK_CONTROL);
837 host->clk_freq = 24000000;
838 host->clk_div = clk_div2;
839 writel(TIFM_CTRL_FAST_CLK
840 | readl(sock->addr + SOCK_CONTROL),
841 sock->addr + SOCK_CONTROL);
846 host->clk_div &= TIFM_MMCSD_CLKMASK;
848 | ((~TIFM_MMCSD_CLKMASK)
849 & readl(sock->addr + SOCK_MMCSD_CONFIG)),
850 sock->addr + SOCK_MMCSD_CONFIG);
852 host->open_drain = (ios->bus_mode == MMC_BUSMODE_OPENDRAIN);
854 /* chip_select : maybe later */
856 //power is set before probe / after remove
858 spin_unlock_irqrestore(&sock->lock, flags);
861 static int tifm_sd_ro(struct mmc_host *mmc)
864 struct tifm_sd *host = mmc_priv(mmc);
865 struct tifm_dev *sock = host->dev;
868 spin_lock_irqsave(&sock->lock, flags);
869 if (TIFM_MMCSD_CARD_RO & readl(sock->addr + SOCK_PRESENT_STATE))
871 spin_unlock_irqrestore(&sock->lock, flags);
875 static const struct mmc_host_ops tifm_sd_ops = {
876 .request = tifm_sd_request,
877 .set_ios = tifm_sd_ios,
881 static int tifm_sd_initialize_host(struct tifm_sd *host)
884 unsigned int host_status = 0;
885 struct tifm_dev *sock = host->dev;
887 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
889 host->clk_freq = 20000000;
890 writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
891 writel(host->clk_div | TIFM_MMCSD_POWER,
892 sock->addr + SOCK_MMCSD_CONFIG);
894 /* wait up to 0.51 sec for reset */
895 for (rc = 32; rc <= 256; rc <<= 1) {
896 if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
904 pr_err("%s : controller failed to reset\n",
905 dev_name(&sock->dev));
909 writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
910 writel(host->clk_div | TIFM_MMCSD_POWER,
911 sock->addr + SOCK_MMCSD_CONFIG);
912 writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
914 // command timeout fixed to 64 clocks for now
915 writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
916 writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
918 for (rc = 16; rc <= 64; rc <<= 1) {
919 host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
920 writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
921 if (!(host_status & TIFM_MMCSD_ERRMASK)
922 && (host_status & TIFM_MMCSD_EOC)) {
930 pr_err("%s : card not ready - probe failed on initialization\n",
931 dev_name(&sock->dev));
935 writel(TIFM_MMCSD_CERR | TIFM_MMCSD_BRS | TIFM_MMCSD_EOC
936 | TIFM_MMCSD_ERRMASK,
937 sock->addr + SOCK_MMCSD_INT_ENABLE);
942 static int tifm_sd_probe(struct tifm_dev *sock)
944 struct mmc_host *mmc;
945 struct tifm_sd *host;
948 if (!(TIFM_SOCK_STATE_OCCUPIED
949 & readl(sock->addr + SOCK_PRESENT_STATE))) {
950 pr_warn("%s : card gone, unexpectedly\n",
951 dev_name(&sock->dev));
955 mmc = mmc_alloc_host(sizeof(struct tifm_sd), &sock->dev);
959 host = mmc_priv(mmc);
960 tifm_set_drvdata(sock, mmc);
962 host->timeout_jiffies = msecs_to_jiffies(1000);
964 tasklet_init(&host->finish_tasklet, tifm_sd_end_cmd,
965 (unsigned long)host);
966 timer_setup(&host->timer, tifm_sd_abort, 0);
968 mmc->ops = &tifm_sd_ops;
969 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
970 mmc->caps = MMC_CAP_4_BIT_DATA;
971 mmc->f_min = 20000000 / 60;
972 mmc->f_max = 24000000;
974 mmc->max_blk_count = 2048;
975 mmc->max_segs = mmc->max_blk_count;
976 mmc->max_blk_size = min(TIFM_MMCSD_MAX_BLOCK_SIZE, PAGE_SIZE);
977 mmc->max_seg_size = mmc->max_blk_count * mmc->max_blk_size;
978 mmc->max_req_size = mmc->max_seg_size;
980 sock->card_event = tifm_sd_card_event;
981 sock->data_event = tifm_sd_data_event;
982 rc = tifm_sd_initialize_host(host);
985 rc = mmc_add_host(mmc);
993 static void tifm_sd_remove(struct tifm_dev *sock)
995 struct mmc_host *mmc = tifm_get_drvdata(sock);
996 struct tifm_sd *host = mmc_priv(mmc);
999 spin_lock_irqsave(&sock->lock, flags);
1001 writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
1002 spin_unlock_irqrestore(&sock->lock, flags);
1004 tasklet_kill(&host->finish_tasklet);
1006 spin_lock_irqsave(&sock->lock, flags);
1008 writel(TIFM_FIFO_INT_SETALL,
1009 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
1010 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
1011 host->req->cmd->error = -ENOMEDIUM;
1012 if (host->req->stop)
1013 host->req->stop->error = -ENOMEDIUM;
1014 tasklet_schedule(&host->finish_tasklet);
1016 spin_unlock_irqrestore(&sock->lock, flags);
1017 mmc_remove_host(mmc);
1018 dev_dbg(&sock->dev, "after remove\n");
1025 static int tifm_sd_suspend(struct tifm_dev *sock, pm_message_t state)
1030 static int tifm_sd_resume(struct tifm_dev *sock)
1032 struct mmc_host *mmc = tifm_get_drvdata(sock);
1033 struct tifm_sd *host = mmc_priv(mmc);
1036 rc = tifm_sd_initialize_host(host);
1037 dev_dbg(&sock->dev, "resume initialize %d\n", rc);
1047 #define tifm_sd_suspend NULL
1048 #define tifm_sd_resume NULL
1050 #endif /* CONFIG_PM */
1052 static struct tifm_device_id tifm_sd_id_tbl[] = {
1053 { TIFM_TYPE_SD }, { }
1056 static struct tifm_driver tifm_sd_driver = {
1058 .name = DRIVER_NAME,
1059 .owner = THIS_MODULE
1061 .id_table = tifm_sd_id_tbl,
1062 .probe = tifm_sd_probe,
1063 .remove = tifm_sd_remove,
1064 .suspend = tifm_sd_suspend,
1065 .resume = tifm_sd_resume
1068 static int __init tifm_sd_init(void)
1070 return tifm_register_driver(&tifm_sd_driver);
1073 static void __exit tifm_sd_exit(void)
1075 tifm_unregister_driver(&tifm_sd_driver);
1078 MODULE_AUTHOR("Alex Dubov");
1079 MODULE_DESCRIPTION("TI FlashMedia SD driver");
1080 MODULE_LICENSE("GPL");
1081 MODULE_DEVICE_TABLE(tifm, tifm_sd_id_tbl);
1082 MODULE_VERSION(DRIVER_VERSION);
1084 module_init(tifm_sd_init);
1085 module_exit(tifm_sd_exit);