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
52 struct dma_desc_array {
53 unsigned long start_addr;
55 unsigned short x_count;
68 struct dma_desc_array *sg_cpu;
73 unsigned int power_mode;
76 struct mmc_request *mrq;
77 struct mmc_command *cmd;
78 struct mmc_data *data;
81 static struct bfin_sd_host *get_sdh_data(struct platform_device *pdev)
83 return pdev->dev.platform_data;
86 static void sdh_stop_clock(struct sdh_host *host)
88 bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() & ~CLK_E);
92 static void sdh_enable_stat_irq(struct sdh_host *host, unsigned int mask)
96 spin_lock_irqsave(&host->lock, flags);
98 bfin_write_SDH_MASK0(mask);
100 spin_unlock_irqrestore(&host->lock, flags);
103 static void sdh_disable_stat_irq(struct sdh_host *host, unsigned int mask)
107 spin_lock_irqsave(&host->lock, flags);
108 host->imask &= ~mask;
109 bfin_write_SDH_MASK0(host->imask);
111 spin_unlock_irqrestore(&host->lock, flags);
114 static int sdh_setup_data(struct sdh_host *host, struct mmc_data *data)
117 unsigned int data_ctl;
118 unsigned int dma_cfg;
119 unsigned int cycle_ns, timeout;
121 dev_dbg(mmc_dev(host->mmc), "%s enter flags: 0x%x\n", __func__, data->flags);
126 length = data->blksz * data->blocks;
127 bfin_write_SDH_DATA_LGTH(length);
129 if (data->flags & MMC_DATA_STREAM)
130 data_ctl |= DTX_MODE;
132 if (data->flags & MMC_DATA_READ)
134 /* Only supports power-of-2 block size */
135 if (data->blksz & (data->blksz - 1))
137 data_ctl |= ((ffs(data->blksz) - 1) << 4);
139 bfin_write_SDH_DATA_CTL(data_ctl);
140 /* the time of a host clock period in ns */
141 cycle_ns = 1000000000 / (get_sclk() / (2 * (host->clk_div + 1)));
142 timeout = data->timeout_ns / cycle_ns;
143 timeout += data->timeout_clks;
144 bfin_write_SDH_DATA_TIMER(timeout);
147 if (data->flags & MMC_DATA_READ) {
148 host->dma_dir = DMA_FROM_DEVICE;
151 host->dma_dir = DMA_TO_DEVICE;
153 sdh_enable_stat_irq(host, (DAT_CRC_FAIL | DAT_TIME_OUT | DAT_END));
154 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma_dir);
155 #if defined(CONFIG_BF54x)
156 dma_cfg |= DMAFLOW_ARRAY | NDSIZE_5 | RESTART | WDSIZE_32 | DMAEN;
158 struct scatterlist *sg;
160 for_each_sg(data->sg, sg, host->dma_len, i) {
161 host->sg_cpu[i].start_addr = sg_dma_address(sg);
162 host->sg_cpu[i].cfg = dma_cfg;
163 host->sg_cpu[i].x_count = sg_dma_len(sg) / 4;
164 host->sg_cpu[i].x_modify = 4;
165 dev_dbg(mmc_dev(host->mmc), "%d: start_addr:0x%lx, "
166 "cfg:0x%x, x_count:0x%x, x_modify:0x%x\n",
167 i, host->sg_cpu[i].start_addr,
168 host->sg_cpu[i].cfg, host->sg_cpu[i].x_count,
169 host->sg_cpu[i].x_modify);
172 flush_dcache_range((unsigned int)host->sg_cpu,
173 (unsigned int)host->sg_cpu +
174 host->dma_len * sizeof(struct dma_desc_array));
175 /* Set the last descriptor to stop mode */
176 host->sg_cpu[host->dma_len - 1].cfg &= ~(DMAFLOW | NDSIZE);
177 host->sg_cpu[host->dma_len - 1].cfg |= DI_EN;
179 set_dma_curr_desc_addr(host->dma_ch, (unsigned long *)host->sg_dma);
180 set_dma_x_count(host->dma_ch, 0);
181 set_dma_x_modify(host->dma_ch, 0);
182 set_dma_config(host->dma_ch, dma_cfg);
183 #elif defined(CONFIG_BF51x)
184 /* RSI DMA doesn't work in array mode */
185 dma_cfg |= WDSIZE_32 | DMAEN;
186 set_dma_start_addr(host->dma_ch, sg_dma_address(&data->sg[0]));
187 set_dma_x_count(host->dma_ch, length / 4);
188 set_dma_x_modify(host->dma_ch, 4);
189 set_dma_config(host->dma_ch, dma_cfg);
191 bfin_write_SDH_DATA_CTL(bfin_read_SDH_DATA_CTL() | DTX_DMA_E | DTX_E);
195 dev_dbg(mmc_dev(host->mmc), "%s exit\n", __func__);
199 static void sdh_start_cmd(struct sdh_host *host, struct mmc_command *cmd)
201 unsigned int sdh_cmd;
202 unsigned int stat_mask;
204 dev_dbg(mmc_dev(host->mmc), "%s enter cmd: 0x%p\n", __func__, cmd);
205 WARN_ON(host->cmd != NULL);
211 sdh_cmd |= cmd->opcode;
213 if (cmd->flags & MMC_RSP_PRESENT) {
215 stat_mask |= CMD_RESP_END;
217 stat_mask |= CMD_SENT;
220 if (cmd->flags & MMC_RSP_136)
221 sdh_cmd |= CMD_L_RSP;
223 stat_mask |= CMD_CRC_FAIL | CMD_TIME_OUT;
225 sdh_enable_stat_irq(host, stat_mask);
227 bfin_write_SDH_ARGUMENT(cmd->arg);
228 bfin_write_SDH_COMMAND(sdh_cmd | CMD_E);
229 bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() | CLK_E);
233 static void sdh_finish_request(struct sdh_host *host, struct mmc_request *mrq)
235 dev_dbg(mmc_dev(host->mmc), "%s enter\n", __func__);
239 mmc_request_done(host->mmc, mrq);
242 static int sdh_cmd_done(struct sdh_host *host, unsigned int stat)
244 struct mmc_command *cmd = host->cmd;
247 dev_dbg(mmc_dev(host->mmc), "%s enter cmd: %p\n", __func__, cmd);
253 if (cmd->flags & MMC_RSP_PRESENT) {
254 cmd->resp[0] = bfin_read_SDH_RESPONSE0();
255 if (cmd->flags & MMC_RSP_136) {
256 cmd->resp[1] = bfin_read_SDH_RESPONSE1();
257 cmd->resp[2] = bfin_read_SDH_RESPONSE2();
258 cmd->resp[3] = bfin_read_SDH_RESPONSE3();
261 if (stat & CMD_TIME_OUT)
262 cmd->error = -ETIMEDOUT;
263 else if (stat & CMD_CRC_FAIL && cmd->flags & MMC_RSP_CRC)
264 cmd->error = -EILSEQ;
266 sdh_disable_stat_irq(host, (CMD_SENT | CMD_RESP_END | CMD_TIME_OUT | CMD_CRC_FAIL));
268 if (host->data && !cmd->error) {
269 if (host->data->flags & MMC_DATA_WRITE) {
270 ret = sdh_setup_data(host, host->data);
275 sdh_enable_stat_irq(host, DAT_END | RX_OVERRUN | TX_UNDERRUN | DAT_TIME_OUT);
277 sdh_finish_request(host, host->mrq);
282 static int sdh_data_done(struct sdh_host *host, unsigned int stat)
284 struct mmc_data *data = host->data;
286 dev_dbg(mmc_dev(host->mmc), "%s enter stat: 0x%x\n", __func__, stat);
290 disable_dma(host->dma_ch);
291 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
294 if (stat & DAT_TIME_OUT)
295 data->error = -ETIMEDOUT;
296 else if (stat & DAT_CRC_FAIL)
297 data->error = -EILSEQ;
298 else if (stat & (RX_OVERRUN | TX_UNDERRUN))
302 data->bytes_xfered = data->blocks * data->blksz;
304 data->bytes_xfered = 0;
306 sdh_disable_stat_irq(host, DAT_END | DAT_TIME_OUT | DAT_CRC_FAIL | RX_OVERRUN | TX_UNDERRUN);
307 bfin_write_SDH_STATUS_CLR(DAT_END_STAT | DAT_TIMEOUT_STAT | \
308 DAT_CRC_FAIL_STAT | DAT_BLK_END_STAT | RX_OVERRUN | TX_UNDERRUN);
309 bfin_write_SDH_DATA_CTL(0);
313 if (host->mrq->stop) {
314 sdh_stop_clock(host);
315 sdh_start_cmd(host, host->mrq->stop);
317 sdh_finish_request(host, host->mrq);
323 static void sdh_request(struct mmc_host *mmc, struct mmc_request *mrq)
325 struct sdh_host *host = mmc_priv(mmc);
328 dev_dbg(mmc_dev(host->mmc), "%s enter, mrp:%p, cmd:%p\n", __func__, mrq, mrq->cmd);
329 WARN_ON(host->mrq != NULL);
332 host->data = mrq->data;
334 if (mrq->data && mrq->data->flags & MMC_DATA_READ) {
335 ret = sdh_setup_data(host, mrq->data);
340 sdh_start_cmd(host, mrq->cmd);
343 static void sdh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
345 struct sdh_host *host;
350 host = mmc_priv(mmc);
352 spin_lock_irqsave(&host->lock, flags);
354 unsigned long sys_clk, ios_clk;
355 unsigned char clk_div;
356 ios_clk = 2 * ios->clock;
357 sys_clk = get_sclk();
358 clk_div = sys_clk / ios_clk;
359 if (sys_clk % ios_clk == 0)
361 clk_div = min_t(unsigned char, clk_div, 0xFF);
364 host->clk_div = clk_div;
366 sdh_stop_clock(host);
368 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
369 #ifdef CONFIG_SDH_BFIN_MISSING_CMD_PULLUP_WORKAROUND
372 pwr_ctl |= SD_CMD_OD | ROD_CTL;
375 if (ios->bus_width == MMC_BUS_WIDTH_4) {
376 cfg = bfin_read_SDH_CFG();
379 /* Enable 4 bit SDIO */
381 bfin_write_SDH_CFG(cfg);
384 cfg = bfin_read_SDH_CFG();
386 bfin_write_SDH_CFG(cfg);
389 bfin_write_SDH_CLK_CTL(clk_ctl);
391 host->power_mode = ios->power_mode;
392 if (ios->power_mode == MMC_POWER_ON)
395 bfin_write_SDH_PWR_CTL(pwr_ctl);
398 spin_unlock_irqrestore(&host->lock, flags);
400 dev_dbg(mmc_dev(host->mmc), "SDH: clk_div = 0x%x actual clock:%ld expected clock:%d\n",
402 host->clk_div ? get_sclk() / (2 * (host->clk_div + 1)) : 0,
406 static const struct mmc_host_ops sdh_ops = {
407 .request = sdh_request,
408 .set_ios = sdh_set_ios,
411 static irqreturn_t sdh_dma_irq(int irq, void *devid)
413 struct sdh_host *host = devid;
415 dev_dbg(mmc_dev(host->mmc), "%s enter, irq_stat: 0x%04x\n", __func__,
416 get_dma_curr_irqstat(host->dma_ch));
417 clear_dma_irqstat(host->dma_ch);
423 static irqreturn_t sdh_stat_irq(int irq, void *devid)
425 struct sdh_host *host = devid;
429 dev_dbg(mmc_dev(host->mmc), "%s enter\n", __func__);
430 status = bfin_read_SDH_E_STATUS();
431 if (status & SD_CARD_DET) {
432 mmc_detect_change(host->mmc, 0);
433 bfin_write_SDH_E_STATUS(SD_CARD_DET);
435 status = bfin_read_SDH_STATUS();
436 if (status & (CMD_SENT | CMD_RESP_END | CMD_TIME_OUT | CMD_CRC_FAIL)) {
437 handled |= sdh_cmd_done(host, status);
438 bfin_write_SDH_STATUS_CLR(CMD_SENT_STAT | CMD_RESP_END_STAT | \
439 CMD_TIMEOUT_STAT | CMD_CRC_FAIL_STAT);
443 status = bfin_read_SDH_STATUS();
444 if (status & (DAT_END | DAT_TIME_OUT | DAT_CRC_FAIL | RX_OVERRUN | TX_UNDERRUN))
445 handled |= sdh_data_done(host, status);
447 dev_dbg(mmc_dev(host->mmc), "%s exit\n\n", __func__);
449 return IRQ_RETVAL(handled);
452 static int __devinit sdh_probe(struct platform_device *pdev)
454 struct mmc_host *mmc;
455 struct sdh_host *host;
456 struct bfin_sd_host *drv_data = get_sdh_data(pdev);
460 dev_err(&pdev->dev, "missing platform driver data\n");
465 mmc = mmc_alloc_host(sizeof(struct sdh_host), &pdev->dev);
473 mmc->max_seg_size = 1 << 16;
474 mmc->max_blk_size = 1 << 11;
475 mmc->max_blk_count = 1 << 11;
476 mmc->max_req_size = PAGE_SIZE;
477 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
478 mmc->f_max = get_sclk();
479 mmc->f_min = mmc->f_max >> 9;
480 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_NEEDS_POLL;
481 host = mmc_priv(mmc);
484 spin_lock_init(&host->lock);
485 host->irq = drv_data->irq_int0;
486 host->dma_ch = drv_data->dma_chan;
488 ret = request_dma(host->dma_ch, DRIVER_NAME "DMA");
490 dev_err(&pdev->dev, "unable to request DMA channel\n");
494 ret = set_dma_callback(host->dma_ch, sdh_dma_irq, host);
496 dev_err(&pdev->dev, "unable to request DMA irq\n");
500 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
501 if (host->sg_cpu == NULL) {
506 platform_set_drvdata(pdev, mmc);
509 ret = request_irq(host->irq, sdh_stat_irq, 0, "SDH Status IRQ", host);
511 dev_err(&pdev->dev, "unable to request status irq\n");
515 ret = peripheral_request_list(drv_data->pin_req, DRIVER_NAME);
517 dev_err(&pdev->dev, "unable to request peripheral pins\n");
520 #if defined(CONFIG_BF54x)
521 /* Secure Digital Host shares DMA with Nand controller */
522 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
525 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN);
528 /* Disable card inserting detection pin. set MMC_CAP_NEES_POLL, and
529 * mmc stack will do the detection.
531 bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT | PUP_SDDAT3));
537 free_irq(host->irq, host);
539 mmc_remove_host(mmc);
540 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
542 free_dma(host->dma_ch);
549 static int __devexit sdh_remove(struct platform_device *pdev)
551 struct mmc_host *mmc = platform_get_drvdata(pdev);
553 platform_set_drvdata(pdev, NULL);
556 struct sdh_host *host = mmc_priv(mmc);
558 mmc_remove_host(mmc);
560 sdh_stop_clock(host);
561 free_irq(host->irq, host);
562 free_dma(host->dma_ch);
563 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
572 static int sdh_suspend(struct platform_device *dev, pm_message_t state)
574 struct mmc_host *mmc = platform_get_drvdata(dev);
575 struct bfin_sd_host *drv_data = get_sdh_data(dev);
579 ret = mmc_suspend_host(mmc);
581 bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() & ~PWR_ON);
582 peripheral_free_list(drv_data->pin_req);
587 static int sdh_resume(struct platform_device *dev)
589 struct mmc_host *mmc = platform_get_drvdata(dev);
590 struct bfin_sd_host *drv_data = get_sdh_data(dev);
593 ret = peripheral_request_list(drv_data->pin_req, DRIVER_NAME);
595 dev_err(&dev->dev, "unable to request peripheral pins\n");
599 bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() | PWR_ON);
600 #if defined(CONFIG_BF54x)
601 /* Secure Digital Host shares DMA with Nand controller */
602 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
604 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN);
607 bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT | PUP_SDDAT3));
611 ret = mmc_resume_host(mmc);
616 # define sdh_suspend NULL
617 # define sdh_resume NULL
620 static struct platform_driver sdh_driver = {
622 .remove = __devexit_p(sdh_remove),
623 .suspend = sdh_suspend,
624 .resume = sdh_resume,
630 static int __init sdh_init(void)
632 return platform_driver_register(&sdh_driver);
634 module_init(sdh_init);
636 static void __exit sdh_exit(void)
638 platform_driver_unregister(&sdh_driver);
640 module_exit(sdh_exit);
642 MODULE_DESCRIPTION("Blackfin Secure Digital Host Driver");
643 MODULE_AUTHOR("Cliff Cai, Roy Huang");
644 MODULE_LICENSE("GPL");