14 #include <linux/module.h>
30 #include <linux/slab.h>
35 #define OMAP_MMC_REG_CMD 0x00
36 #define OMAP_MMC_REG_ARGL 0x01
37 #define OMAP_MMC_REG_ARGH 0x02
38 #define OMAP_MMC_REG_CON 0x03
39 #define OMAP_MMC_REG_STAT 0x04
40 #define OMAP_MMC_REG_IE 0x05
41 #define OMAP_MMC_REG_CTO 0x06
42 #define OMAP_MMC_REG_DTO 0x07
43 #define OMAP_MMC_REG_DATA 0x08
44 #define OMAP_MMC_REG_BLEN 0x09
45 #define OMAP_MMC_REG_NBLK 0x0a
46 #define OMAP_MMC_REG_BUF 0x0b
47 #define OMAP_MMC_REG_SDIO 0x0d
48 #define OMAP_MMC_REG_REV 0x0f
49 #define OMAP_MMC_REG_RSP0 0x10
50 #define OMAP_MMC_REG_RSP1 0x11
51 #define OMAP_MMC_REG_RSP2 0x12
52 #define OMAP_MMC_REG_RSP3 0x13
53 #define OMAP_MMC_REG_RSP4 0x14
54 #define OMAP_MMC_REG_RSP5 0x15
55 #define OMAP_MMC_REG_RSP6 0x16
56 #define OMAP_MMC_REG_RSP7 0x17
57 #define OMAP_MMC_REG_IOSR 0x18
58 #define OMAP_MMC_REG_SYSC 0x19
59 #define OMAP_MMC_REG_SYSS 0x1a
61 #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
62 #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
63 #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
64 #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
65 #define OMAP_MMC_STAT_A_FULL (1 << 10)
66 #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
67 #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
68 #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
69 #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
70 #define OMAP_MMC_STAT_END_BUSY (1 << 4)
71 #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
72 #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
73 #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
75 #define OMAP_MMC_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
76 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
77 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
82 #define OMAP_MMC_CMDTYPE_BC 0
83 #define OMAP_MMC_CMDTYPE_BCR 1
84 #define OMAP_MMC_CMDTYPE_AC 2
85 #define OMAP_MMC_CMDTYPE_ADTC 3
88 #define DRIVER_NAME "mmci-omap"
92 #define OMAP_MMC_COVER_POLL_DELAY 500
172 unsigned long tick_ns;
192 spin_unlock_irqrestore(&host->
clk_lock, flags);
204 spin_unlock_irqrestore(&host->
slot_lock, flags);
209 spin_unlock_irqrestore(&host->
slot_lock, flags);
223 mmc_omap_fclk_enable(host, 1);
232 mmc_omap_fclk_enable(host, 0);
235 static void mmc_omap_start_request(
struct mmc_omap_host *host,
246 mmc_omap_select_slot(next_slot, 1);
250 mmc_omap_start_request(host, rq);
253 static void mmc_omap_release_slot(
struct mmc_omap_slot *slot,
int clk_enabled)
266 mmc_omap_fclk_offdelay(slot);
267 mmc_omap_fclk_enable(host, 0);
272 for (i = 0; i < host->
nr_slots; i++) {
279 new_slot = host->
slots[
i];
284 host->
mmc = new_slot->
mmc;
285 spin_unlock_irqrestore(&host->
slot_lock, flags);
292 spin_unlock_irqrestore(&host->
slot_lock, flags);
298 if (slot->
pdata->get_cover_state)
311 return sprintf(buf,
"%s\n", mmc_omap_cover_is_open(slot) ?
"open" :
371 cmdreg = cmd->
opcode | (resptype << 8) | (cmdtype << 12);
413 dmaengine_terminate_all(c);
428 unsigned long tick_ns;
433 mmc_omap_start_command(host, data->
stop);
440 mmc_omap_release_dma(host, data, data->
error);
465 mmc_omap_send_abort(
struct mmc_omap_host *host,
int maxloops)
468 unsigned int restarts, passes,
timeout;
474 while (restarts < maxloops) {
479 while (passes < timeout) {
497 mmc_omap_release_dma(host, data, 1);
502 mmc_omap_send_abort(host, 10000);
512 mmc_omap_xfer_done(host, data);
521 spin_unlock_irqrestore(&host->
dma_lock, flags);
523 mmc_omap_xfer_done(host, data);
538 spin_unlock_irqrestore(&host->
dma_lock, flags);
540 mmc_omap_xfer_done(host, data);
577 mmc_omap_abort_xfer(host, host->
data);
589 static void mmc_omap_abort_command(
struct work_struct *work)
598 if (host->
cmd->error == 0)
607 mmc_omap_send_abort(host, 10000);
614 mmc_omap_cmd_done(host, host->
cmd);
621 mmc_omap_cmd_timer(
unsigned long data)
633 spin_unlock_irqrestore(&host->
slot_lock, flags);
644 host->
buffer = sg_virt(sg);
650 mmc_omap_clk_timer(
unsigned long data)
654 mmc_omap_fclk_enable(host, 0);
666 mmc_omap_sg_to_buf(host);
677 host->
data->bytes_xfered +=
n;
690 #ifdef CONFIG_MMC_DEBUG
693 static const char *mmc_omap_status_bits[] = {
694 "EOC",
"CD",
"CB",
"BRS",
"EOFB",
"DTO",
"DCRC",
"CTO",
695 "CCRC",
"CRW",
"AF",
"AE",
"OCRB",
"CIRQ",
"CERR"
700 buf +=
sprintf(buf,
"MMC IRQ 0x%x:", status);
702 for (i = 0; i <
ARRAY_SIZE(mmc_omap_status_bits); i++)
703 if (status & (1 << i))
704 buf +=
sprintf(buf,
" %s", mmc_omap_status_bits[i]);
720 int transfer_error, cmd_error;
725 "Spurious IRQ 0x%04x\n", status);
743 cmd = host->
cmd->opcode;
748 mmc_omap_report_irq(host, status);
753 mmc_omap_xfer_data(host, 0);
755 mmc_omap_xfer_data(host, 1);
758 if (status & OMAP_MMC_STAT_END_OF_DATA)
774 "data CRC error, bytes left %d\n",
788 !mmc_omap_cover_is_open(slot))
790 "command timeout (CMD%d)\n",
801 "command CRC error (CMD%d, arg 0x%08x)\n",
802 cmd, host->
cmd->arg);
808 "command CRC error without cmd?\n");
813 "ignoring card status error (CMD%d)\n",
828 if (cmd_error && host->
data) {
837 if (end_command && host->
cmd)
838 mmc_omap_cmd_done(host, host->
cmd);
841 mmc_omap_xfer_done(host, host->
data);
842 else if (end_transfer)
843 mmc_omap_end_of_data(host, host->
data);
861 cover_open = mmc_omap_cover_is_open(slot);
870 static void mmc_omap_cover_timer(
unsigned long arg)
876 static void mmc_omap_cover_handler(
unsigned long param)
879 int cover_open = mmc_omap_cover_is_open(slot);
896 static void mmc_omap_dma_callback(
void *
priv)
904 mmc_omap_dma_done(host, data);
920 unsigned int timeout, cycle_ns;
924 timeout = req->
data->timeout_ns / cycle_ns;
925 timeout += req->
data->timeout_clks;
929 if (timeout > 0xffff) {
951 set_cmd_timeout(host, req);
955 block_size = data->
blksz;
959 set_data_timeout(host, req);
969 for (i = 0; i <
sg_len; i++) {
970 if ((data->
sg[i].length % block_size) != 0) {
992 if (burst > data->
blksz)
1000 buf = 0x0f80 | (burst - 1) << 0;
1005 buf = 0x800f | (burst - 1) << 8;
1020 cfg.src_maxburst = burst;
1021 cfg.dst_maxburst = burst;
1023 if (dmaengine_slave_config(c, &
cfg))
1034 tx = dmaengine_prep_slave_sg(c, data->
sg, host->
sg_len,
1042 tx->
callback = mmc_omap_dma_callback;
1044 dmaengine_submit(tx);
1056 mmc_omap_sg_to_buf(host);
1060 static void mmc_omap_start_request(
struct mmc_omap_host *host,
1068 mmc_omap_prepare_data(host, req);
1069 mmc_omap_start_command(host, req->
cmd);
1074 dma_async_issue_pending(c);
1082 unsigned long flags;
1088 spin_unlock_irqrestore(&host->
slot_lock, flags);
1092 spin_unlock_irqrestore(&host->
slot_lock, flags);
1093 mmc_omap_select_slot(slot, 1);
1094 mmc_omap_start_request(host, req);
1097 static void mmc_omap_set_power(
struct mmc_omap_slot *slot,
int power_on,
1121 static int mmc_omap_calc_divisor(
struct mmc_host *mmc,
struct mmc_ios *ios)
1128 if (ios->
clock == 0)
1131 dsor = func_clk_rate / ios->
clock;
1135 if (func_clk_rate / dsor > ios->
clock)
1149 static void mmc_omap_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
1156 mmc_omap_select_slot(slot, 0);
1158 dsor = mmc_omap_calc_divisor(mmc, ios);
1160 if (ios->
vdd != slot->
vdd)
1166 mmc_omap_set_power(slot, 0, ios->
vdd);
1170 mmc_omap_set_power(slot, 1, ios->
vdd);
1173 mmc_omap_fclk_enable(host, 1);
1190 for (i = 0; i < 2; i++)
1209 mmc_omap_release_slot(slot, clk_enabled);
1213 .request = mmc_omap_request,
1214 .set_ios = mmc_omap_set_ios,
1227 slot = mmc_priv(mmc);
1236 if (host->
pdata->slots[
id].wires >= 4)
1239 mmc->
ops = &mmc_omap_ops;
1240 mmc->
f_min = 400000;
1243 mmc->
f_max = 48000000;
1245 mmc->
f_max = 24000000;
1246 if (host->
pdata->max_freq)
1262 goto err_remove_host;
1266 &dev_attr_slot_name);
1268 goto err_remove_host;
1271 if (slot->
pdata->get_cover_state !=
NULL) {
1273 &dev_attr_cover_switch);
1275 goto err_remove_slot_name;
1278 (
unsigned long)slot);
1280 (
unsigned long)slot);
1286 err_remove_slot_name:
1295 static void mmc_omap_remove_slot(
struct mmc_omap_slot *slot)
1301 if (slot->
pdata->get_cover_state !=
NULL)
1322 if (pdata ==
NULL) {
1323 dev_err(&pdev->
dev,
"platform data missing\n");
1333 if (res ==
NULL || irq < 0)
1344 goto err_free_mem_region;
1352 (
unsigned long) host);
1363 platform_set_drvdata(pdev, host);
1365 host->
id = pdev->
id;
1376 if (IS_ERR(host->
iclk)) {
1377 ret = PTR_ERR(host->
iclk);
1378 goto err_free_mmc_host;
1383 if (IS_ERR(host->
fclk)) {
1384 ret = PTR_ERR(host->
fclk);
1401 dev_err(host->
dev,
"unable to obtain TX DMA engine channel %u\n",
1407 dev_warn(host->
dev,
"unable to obtain TX DMA engine channel %u\n",
1417 dev_err(host->
dev,
"unable to obtain RX DMA engine channel %u\n",
1423 dev_warn(host->
dev,
"unable to obtain RX DMA engine channel %u\n",
1432 ret = pdata->
init(&pdev->
dev);
1442 goto err_plat_cleanup;
1444 for (i = 0; i < pdata->
nr_slots; i++) {
1445 ret = mmc_omap_new_slot(host, i);
1448 mmc_omap_remove_slot(host->
slots[i]);
1450 goto err_destroy_wq;
1476 err_free_mem_region:
1486 platform_set_drvdata(pdev,
NULL);
1490 for (i = 0; i < host->
nr_slots; i++)
1491 mmc_omap_remove_slot(host->
slots[i]);
1493 if (host->
pdata->cleanup)
1496 mmc_omap_fclk_enable(host, 0);
1526 for (i = 0; i < host->
nr_slots; i++) {
1551 for (i = 0; i < host->
nr_slots; i++) {
1563 #define mmc_omap_suspend NULL
1564 #define mmc_omap_resume NULL
1568 .probe = mmc_omap_probe,