2 * bfin_sdh.c - Analog Devices Blackfin SDH Controller
4 * Copyright (C) 2007-2009 Analog Device Inc.
6 * Licensed under the GPL-2 or later.
9 #define DRIVER_NAME "bfin-sdh"
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/ioport.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/mmc/host.h>
19 #include <linux/proc_fs.h>
20 #include <linux/gfp.h>
22 #include <asm/cacheflush.h>
24 #include <asm/portmux.h>
25 #include <asm/bfin_sdh.h>
27 #if defined(CONFIG_BF51x)
28 #define bfin_read_SDH_PWR_CTL bfin_read_RSI_PWR_CTL
29 #define bfin_write_SDH_PWR_CTL bfin_write_RSI_PWR_CTL
30 #define bfin_read_SDH_CLK_CTL bfin_read_RSI_CLK_CTL
31 #define bfin_write_SDH_CLK_CTL bfin_write_RSI_CLK_CTL
32 #define bfin_write_SDH_ARGUMENT bfin_write_RSI_ARGUMENT
33 #define bfin_write_SDH_COMMAND bfin_write_RSI_COMMAND
34 #define bfin_write_SDH_DATA_TIMER bfin_write_RSI_DATA_TIMER
35 #define bfin_read_SDH_RESPONSE0 bfin_read_RSI_RESPONSE0
36 #define bfin_read_SDH_RESPONSE1 bfin_read_RSI_RESPONSE1
37 #define bfin_read_SDH_RESPONSE2 bfin_read_RSI_RESPONSE2
38 #define bfin_read_SDH_RESPONSE3 bfin_read_RSI_RESPONSE3
39 #define bfin_write_SDH_DATA_LGTH bfin_write_RSI_DATA_LGTH
40 #define bfin_read_SDH_DATA_CTL bfin_read_RSI_DATA_CTL
41 #define bfin_write_SDH_DATA_CTL bfin_write_RSI_DATA_CTL
42 #define bfin_read_SDH_DATA_CNT bfin_read_RSI_DATA_CNT
43 #define bfin_write_SDH_STATUS_CLR bfin_write_RSI_STATUS_CLR
44 #define bfin_read_SDH_E_STATUS bfin_read_RSI_E_STATUS
45 #define bfin_write_SDH_E_STATUS bfin_write_RSI_E_STATUS
46 #define bfin_read_SDH_STATUS bfin_read_RSI_STATUS
47 #define bfin_write_SDH_MASK0 bfin_write_RSI_MASK0
48 #define bfin_read_SDH_CFG bfin_read_RSI_CFG
49 #define bfin_write_SDH_CFG bfin_write_RSI_CFG
61 struct dma_desc_array
*sg_cpu
;
66 unsigned int power_mode
;
69 struct mmc_request
*mrq
;
70 struct mmc_command
*cmd
;
71 struct mmc_data
*data
;
74 static struct bfin_sd_host
*get_sdh_data(struct platform_device
*pdev
)
76 return pdev
->dev
.platform_data
;
79 static void sdh_stop_clock(struct sdh_host
*host
)
81 bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() & ~CLK_E
);
85 static void sdh_enable_stat_irq(struct sdh_host
*host
, unsigned int mask
)
89 spin_lock_irqsave(&host
->lock
, flags
);
91 bfin_write_SDH_MASK0(mask
);
93 spin_unlock_irqrestore(&host
->lock
, flags
);
96 static void sdh_disable_stat_irq(struct sdh_host
*host
, unsigned int mask
)
100 spin_lock_irqsave(&host
->lock
, flags
);
101 host
->imask
&= ~mask
;
102 bfin_write_SDH_MASK0(host
->imask
);
104 spin_unlock_irqrestore(&host
->lock
, flags
);
107 static int sdh_setup_data(struct sdh_host
*host
, struct mmc_data
*data
)
110 unsigned int data_ctl
;
111 unsigned int dma_cfg
;
112 unsigned int cycle_ns
, timeout
;
114 dev_dbg(mmc_dev(host
->mmc
), "%s enter flags: 0x%x\n", __func__
, data
->flags
);
119 length
= data
->blksz
* data
->blocks
;
120 bfin_write_SDH_DATA_LGTH(length
);
122 if (data
->flags
& MMC_DATA_STREAM
)
123 data_ctl
|= DTX_MODE
;
125 if (data
->flags
& MMC_DATA_READ
)
127 /* Only supports power-of-2 block size */
128 if (data
->blksz
& (data
->blksz
- 1))
130 data_ctl
|= ((ffs(data
->blksz
) - 1) << 4);
132 bfin_write_SDH_DATA_CTL(data_ctl
);
133 /* the time of a host clock period in ns */
134 cycle_ns
= 1000000000 / (get_sclk() / (2 * (host
->clk_div
+ 1)));
135 timeout
= data
->timeout_ns
/ cycle_ns
;
136 timeout
+= data
->timeout_clks
;
137 bfin_write_SDH_DATA_TIMER(timeout
);
140 if (data
->flags
& MMC_DATA_READ
) {
141 host
->dma_dir
= DMA_FROM_DEVICE
;
144 host
->dma_dir
= DMA_TO_DEVICE
;
146 sdh_enable_stat_irq(host
, (DAT_CRC_FAIL
| DAT_TIME_OUT
| DAT_END
));
147 host
->dma_len
= dma_map_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
, host
->dma_dir
);
148 #if defined(CONFIG_BF54x)
149 dma_cfg
|= DMAFLOW_ARRAY
| NDSIZE_5
| RESTART
| WDSIZE_32
| DMAEN
;
151 struct scatterlist
*sg
;
153 for_each_sg(data
->sg
, sg
, host
->dma_len
, i
) {
154 host
->sg_cpu
[i
].start_addr
= sg_dma_address(sg
);
155 host
->sg_cpu
[i
].cfg
= dma_cfg
;
156 host
->sg_cpu
[i
].x_count
= sg_dma_len(sg
) / 4;
157 host
->sg_cpu
[i
].x_modify
= 4;
158 dev_dbg(mmc_dev(host
->mmc
), "%d: start_addr:0x%lx, "
159 "cfg:0x%x, x_count:0x%x, x_modify:0x%x\n",
160 i
, host
->sg_cpu
[i
].start_addr
,
161 host
->sg_cpu
[i
].cfg
, host
->sg_cpu
[i
].x_count
,
162 host
->sg_cpu
[i
].x_modify
);
165 flush_dcache_range((unsigned int)host
->sg_cpu
,
166 (unsigned int)host
->sg_cpu
+
167 host
->dma_len
* sizeof(struct dma_desc_array
));
168 /* Set the last descriptor to stop mode */
169 host
->sg_cpu
[host
->dma_len
- 1].cfg
&= ~(DMAFLOW
| NDSIZE
);
170 host
->sg_cpu
[host
->dma_len
- 1].cfg
|= DI_EN
;
172 set_dma_curr_desc_addr(host
->dma_ch
, (unsigned long *)host
->sg_dma
);
173 set_dma_x_count(host
->dma_ch
, 0);
174 set_dma_x_modify(host
->dma_ch
, 0);
175 set_dma_config(host
->dma_ch
, dma_cfg
);
176 #elif defined(CONFIG_BF51x)
177 /* RSI DMA doesn't work in array mode */
178 dma_cfg
|= WDSIZE_32
| DMAEN
;
179 set_dma_start_addr(host
->dma_ch
, sg_dma_address(&data
->sg
[0]));
180 set_dma_x_count(host
->dma_ch
, length
/ 4);
181 set_dma_x_modify(host
->dma_ch
, 4);
182 set_dma_config(host
->dma_ch
, dma_cfg
);
184 bfin_write_SDH_DATA_CTL(bfin_read_SDH_DATA_CTL() | DTX_DMA_E
| DTX_E
);
188 dev_dbg(mmc_dev(host
->mmc
), "%s exit\n", __func__
);
192 static void sdh_start_cmd(struct sdh_host
*host
, struct mmc_command
*cmd
)
194 unsigned int sdh_cmd
;
195 unsigned int stat_mask
;
197 dev_dbg(mmc_dev(host
->mmc
), "%s enter cmd: 0x%p\n", __func__
, cmd
);
198 WARN_ON(host
->cmd
!= NULL
);
204 sdh_cmd
|= cmd
->opcode
;
206 if (cmd
->flags
& MMC_RSP_PRESENT
) {
208 stat_mask
|= CMD_RESP_END
;
210 stat_mask
|= CMD_SENT
;
213 if (cmd
->flags
& MMC_RSP_136
)
214 sdh_cmd
|= CMD_L_RSP
;
216 stat_mask
|= CMD_CRC_FAIL
| CMD_TIME_OUT
;
218 sdh_enable_stat_irq(host
, stat_mask
);
220 bfin_write_SDH_ARGUMENT(cmd
->arg
);
221 bfin_write_SDH_COMMAND(sdh_cmd
| CMD_E
);
222 bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() | CLK_E
);
226 static void sdh_finish_request(struct sdh_host
*host
, struct mmc_request
*mrq
)
228 dev_dbg(mmc_dev(host
->mmc
), "%s enter\n", __func__
);
232 mmc_request_done(host
->mmc
, mrq
);
235 static int sdh_cmd_done(struct sdh_host
*host
, unsigned int stat
)
237 struct mmc_command
*cmd
= host
->cmd
;
240 dev_dbg(mmc_dev(host
->mmc
), "%s enter cmd: %p\n", __func__
, cmd
);
246 if (cmd
->flags
& MMC_RSP_PRESENT
) {
247 cmd
->resp
[0] = bfin_read_SDH_RESPONSE0();
248 if (cmd
->flags
& MMC_RSP_136
) {
249 cmd
->resp
[1] = bfin_read_SDH_RESPONSE1();
250 cmd
->resp
[2] = bfin_read_SDH_RESPONSE2();
251 cmd
->resp
[3] = bfin_read_SDH_RESPONSE3();
254 if (stat
& CMD_TIME_OUT
)
255 cmd
->error
= -ETIMEDOUT
;
256 else if (stat
& CMD_CRC_FAIL
&& cmd
->flags
& MMC_RSP_CRC
)
257 cmd
->error
= -EILSEQ
;
259 sdh_disable_stat_irq(host
, (CMD_SENT
| CMD_RESP_END
| CMD_TIME_OUT
| CMD_CRC_FAIL
));
261 if (host
->data
&& !cmd
->error
) {
262 if (host
->data
->flags
& MMC_DATA_WRITE
) {
263 ret
= sdh_setup_data(host
, host
->data
);
268 sdh_enable_stat_irq(host
, DAT_END
| RX_OVERRUN
| TX_UNDERRUN
| DAT_TIME_OUT
);
270 sdh_finish_request(host
, host
->mrq
);
275 static int sdh_data_done(struct sdh_host
*host
, unsigned int stat
)
277 struct mmc_data
*data
= host
->data
;
279 dev_dbg(mmc_dev(host
->mmc
), "%s enter stat: 0x%x\n", __func__
, stat
);
283 disable_dma(host
->dma_ch
);
284 dma_unmap_sg(mmc_dev(host
->mmc
), data
->sg
, data
->sg_len
,
287 if (stat
& DAT_TIME_OUT
)
288 data
->error
= -ETIMEDOUT
;
289 else if (stat
& DAT_CRC_FAIL
)
290 data
->error
= -EILSEQ
;
291 else if (stat
& (RX_OVERRUN
| TX_UNDERRUN
))
295 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
297 data
->bytes_xfered
= 0;
299 sdh_disable_stat_irq(host
, DAT_END
| DAT_TIME_OUT
| DAT_CRC_FAIL
| RX_OVERRUN
| TX_UNDERRUN
);
300 bfin_write_SDH_STATUS_CLR(DAT_END_STAT
| DAT_TIMEOUT_STAT
| \
301 DAT_CRC_FAIL_STAT
| DAT_BLK_END_STAT
| RX_OVERRUN
| TX_UNDERRUN
);
302 bfin_write_SDH_DATA_CTL(0);
306 if (host
->mrq
->stop
) {
307 sdh_stop_clock(host
);
308 sdh_start_cmd(host
, host
->mrq
->stop
);
310 sdh_finish_request(host
, host
->mrq
);
316 static void sdh_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
318 struct sdh_host
*host
= mmc_priv(mmc
);
321 dev_dbg(mmc_dev(host
->mmc
), "%s enter, mrp:%p, cmd:%p\n", __func__
, mrq
, mrq
->cmd
);
322 WARN_ON(host
->mrq
!= NULL
);
325 host
->data
= mrq
->data
;
327 if (mrq
->data
&& mrq
->data
->flags
& MMC_DATA_READ
) {
328 ret
= sdh_setup_data(host
, mrq
->data
);
333 sdh_start_cmd(host
, mrq
->cmd
);
336 static void sdh_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
338 struct sdh_host
*host
;
343 host
= mmc_priv(mmc
);
345 spin_lock_irqsave(&host
->lock
, flags
);
347 unsigned long sys_clk
, ios_clk
;
348 unsigned char clk_div
;
349 ios_clk
= 2 * ios
->clock
;
350 sys_clk
= get_sclk();
351 clk_div
= sys_clk
/ ios_clk
;
352 if (sys_clk
% ios_clk
== 0)
354 clk_div
= min_t(unsigned char, clk_div
, 0xFF);
357 host
->clk_div
= clk_div
;
359 sdh_stop_clock(host
);
361 if (ios
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
362 #ifdef CONFIG_SDH_BFIN_MISSING_CMD_PULLUP_WORKAROUND
365 pwr_ctl
|= SD_CMD_OD
| ROD_CTL
;
368 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
369 cfg
= bfin_read_SDH_CFG();
372 /* Enable 4 bit SDIO */
374 bfin_write_SDH_CFG(cfg
);
377 cfg
= bfin_read_SDH_CFG();
379 bfin_write_SDH_CFG(cfg
);
382 bfin_write_SDH_CLK_CTL(clk_ctl
);
384 host
->power_mode
= ios
->power_mode
;
385 if (ios
->power_mode
== MMC_POWER_ON
)
388 bfin_write_SDH_PWR_CTL(pwr_ctl
);
391 spin_unlock_irqrestore(&host
->lock
, flags
);
393 dev_dbg(mmc_dev(host
->mmc
), "SDH: clk_div = 0x%x actual clock:%ld expected clock:%d\n",
395 host
->clk_div
? get_sclk() / (2 * (host
->clk_div
+ 1)) : 0,
399 static const struct mmc_host_ops sdh_ops
= {
400 .request
= sdh_request
,
401 .set_ios
= sdh_set_ios
,
404 static irqreturn_t
sdh_dma_irq(int irq
, void *devid
)
406 struct sdh_host
*host
= devid
;
408 dev_dbg(mmc_dev(host
->mmc
), "%s enter, irq_stat: 0x%04x\n", __func__
,
409 get_dma_curr_irqstat(host
->dma_ch
));
410 clear_dma_irqstat(host
->dma_ch
);
416 static irqreturn_t
sdh_stat_irq(int irq
, void *devid
)
418 struct sdh_host
*host
= devid
;
422 dev_dbg(mmc_dev(host
->mmc
), "%s enter\n", __func__
);
423 status
= bfin_read_SDH_E_STATUS();
424 if (status
& SD_CARD_DET
) {
425 mmc_detect_change(host
->mmc
, 0);
426 bfin_write_SDH_E_STATUS(SD_CARD_DET
);
428 status
= bfin_read_SDH_STATUS();
429 if (status
& (CMD_SENT
| CMD_RESP_END
| CMD_TIME_OUT
| CMD_CRC_FAIL
)) {
430 handled
|= sdh_cmd_done(host
, status
);
431 bfin_write_SDH_STATUS_CLR(CMD_SENT_STAT
| CMD_RESP_END_STAT
| \
432 CMD_TIMEOUT_STAT
| CMD_CRC_FAIL_STAT
);
436 status
= bfin_read_SDH_STATUS();
437 if (status
& (DAT_END
| DAT_TIME_OUT
| DAT_CRC_FAIL
| RX_OVERRUN
| TX_UNDERRUN
))
438 handled
|= sdh_data_done(host
, status
);
440 dev_dbg(mmc_dev(host
->mmc
), "%s exit\n\n", __func__
);
442 return IRQ_RETVAL(handled
);
445 static int __devinit
sdh_probe(struct platform_device
*pdev
)
447 struct mmc_host
*mmc
;
448 struct sdh_host
*host
;
449 struct bfin_sd_host
*drv_data
= get_sdh_data(pdev
);
453 dev_err(&pdev
->dev
, "missing platform driver data\n");
458 mmc
= mmc_alloc_host(sizeof(struct sdh_host
), &pdev
->dev
);
466 mmc
->max_seg_size
= 1 << 16;
467 mmc
->max_blk_size
= 1 << 11;
468 mmc
->max_blk_count
= 1 << 11;
469 mmc
->max_req_size
= PAGE_SIZE
;
470 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
471 mmc
->f_max
= get_sclk();
472 mmc
->f_min
= mmc
->f_max
>> 9;
473 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_NEEDS_POLL
;
474 host
= mmc_priv(mmc
);
477 spin_lock_init(&host
->lock
);
478 host
->irq
= drv_data
->irq_int0
;
479 host
->dma_ch
= drv_data
->dma_chan
;
481 ret
= request_dma(host
->dma_ch
, DRIVER_NAME
"DMA");
483 dev_err(&pdev
->dev
, "unable to request DMA channel\n");
487 ret
= set_dma_callback(host
->dma_ch
, sdh_dma_irq
, host
);
489 dev_err(&pdev
->dev
, "unable to request DMA irq\n");
493 host
->sg_cpu
= dma_alloc_coherent(&pdev
->dev
, PAGE_SIZE
, &host
->sg_dma
, GFP_KERNEL
);
494 if (host
->sg_cpu
== NULL
) {
499 platform_set_drvdata(pdev
, mmc
);
502 ret
= request_irq(host
->irq
, sdh_stat_irq
, 0, "SDH Status IRQ", host
);
504 dev_err(&pdev
->dev
, "unable to request status irq\n");
508 ret
= peripheral_request_list(drv_data
->pin_req
, DRIVER_NAME
);
510 dev_err(&pdev
->dev
, "unable to request peripheral pins\n");
513 #if defined(CONFIG_BF54x)
514 /* Secure Digital Host shares DMA with Nand controller */
515 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
518 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN
);
521 /* Disable card inserting detection pin. set MMC_CAP_NEES_POLL, and
522 * mmc stack will do the detection.
524 bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT
| PUP_SDDAT3
));
530 free_irq(host
->irq
, host
);
532 mmc_remove_host(mmc
);
533 dma_free_coherent(&pdev
->dev
, PAGE_SIZE
, host
->sg_cpu
, host
->sg_dma
);
535 free_dma(host
->dma_ch
);
542 static int __devexit
sdh_remove(struct platform_device
*pdev
)
544 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
546 platform_set_drvdata(pdev
, NULL
);
549 struct sdh_host
*host
= mmc_priv(mmc
);
551 mmc_remove_host(mmc
);
553 sdh_stop_clock(host
);
554 free_irq(host
->irq
, host
);
555 free_dma(host
->dma_ch
);
556 dma_free_coherent(&pdev
->dev
, PAGE_SIZE
, host
->sg_cpu
, host
->sg_dma
);
565 static int sdh_suspend(struct platform_device
*dev
, pm_message_t state
)
567 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
568 struct bfin_sd_host
*drv_data
= get_sdh_data(dev
);
572 ret
= mmc_suspend_host(mmc
);
574 bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() & ~PWR_ON
);
575 peripheral_free_list(drv_data
->pin_req
);
580 static int sdh_resume(struct platform_device
*dev
)
582 struct mmc_host
*mmc
= platform_get_drvdata(dev
);
583 struct bfin_sd_host
*drv_data
= get_sdh_data(dev
);
586 ret
= peripheral_request_list(drv_data
->pin_req
, DRIVER_NAME
);
588 dev_err(&dev
->dev
, "unable to request peripheral pins\n");
592 bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() | PWR_ON
);
593 #if defined(CONFIG_BF54x)
594 /* Secure Digital Host shares DMA with Nand controller */
595 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
597 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN
);
600 bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT
| PUP_SDDAT3
));
604 ret
= mmc_resume_host(mmc
);
609 # define sdh_suspend NULL
610 # define sdh_resume NULL
613 static struct platform_driver sdh_driver
= {
615 .remove
= __devexit_p(sdh_remove
),
616 .suspend
= sdh_suspend
,
617 .resume
= sdh_resume
,
623 module_platform_driver(sdh_driver
);
625 MODULE_DESCRIPTION("Blackfin Secure Digital Host Driver");
626 MODULE_AUTHOR("Cliff Cai, Roy Huang");
627 MODULE_LICENSE("GPL");