17 #include <linux/device.h>
23 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
33 #include <linux/bitops.h>
41 #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \
42 SDMMC_INT_HTO | SDMMC_INT_SBE | \
44 #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
46 #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
47 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
48 #define DW_MCI_SEND_STATUS 1
49 #define DW_MCI_RECV_STATUS 2
50 #define DW_MCI_DMA_THRESHOLD 16
52 #ifdef CONFIG_MMC_DW_IDMAC
55 #define IDMAC_DES0_DIC BIT(1)
56 #define IDMAC_DES0_LD BIT(2)
57 #define IDMAC_DES0_FD BIT(3)
58 #define IDMAC_DES0_CH BIT(4)
59 #define IDMAC_DES0_ER BIT(5)
60 #define IDMAC_DES0_CES BIT(30)
61 #define IDMAC_DES0_OWN BIT(31)
64 #define IDMAC_SET_BUFFER1_SIZE(d, s) \
65 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
98 #define DW_MMC_CARD_PRESENT 0
99 #define DW_MMC_CARD_NEED_INIT 1
104 #if defined(CONFIG_DEBUG_FS)
105 static int dw_mci_req_show(
struct seq_file *
s,
void *
v)
114 spin_lock_bh(&slot->
host->lock);
124 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
129 seq_printf(s,
"DATA %u / %u * %u flg %x err %d\n",
134 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
140 spin_unlock_bh(&slot->
host->lock);
152 .open = dw_mci_req_open,
158 static int dw_mci_regs_show(
struct seq_file *
s,
void *
v)
177 .open = dw_mci_regs_open,
225 static void dw_mci_set_timeout(
struct dw_mci *host)
271 static void dw_mci_start_command(
struct dw_mci *host,
276 "start command: ARGR=0x%08x CMDR=0x%08x\n",
277 cmd->
arg, cmd_flags);
285 static void send_stop_cmd(
struct dw_mci *host,
struct mmc_data *data)
291 static void dw_mci_stop_dma(
struct dw_mci *host)
302 static int dw_mci_get_dma_dir(
struct mmc_data *data)
310 #ifdef CONFIG_MMC_DW_IDMAC
311 static void dw_mci_dma_cleanup(
struct dw_mci *host)
320 dw_mci_get_dma_dir(data));
323 static void dw_mci_idmac_stop_dma(
struct dw_mci *host)
339 static void dw_mci_idmac_complete_dma(
struct dw_mci *host)
353 tasklet_schedule(&host->
tasklet);
357 static void dw_mci_translate_sglist(
struct dw_mci *host,
struct mmc_data *data,
363 for (i = 0; i < sg_len; i++, desc++) {
368 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
371 IDMAC_SET_BUFFER1_SIZE(desc, length);
374 desc->des2 = mem_addr;
379 desc->des0 |= IDMAC_DES0_FD;
382 desc = host->
sg_cpu + (i - 1) *
sizeof(
struct idmac_desc);
383 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
384 desc->des0 |= IDMAC_DES0_LD;
389 static void dw_mci_idmac_start_dma(
struct dw_mci *host,
unsigned int sg_len)
393 dw_mci_translate_sglist(host, host->
data, sg_len);
411 static int dw_mci_idmac_init(
struct dw_mci *host)
413 struct idmac_desc *
p;
417 host->ring_size =
PAGE_SIZE /
sizeof(
struct idmac_desc);
421 p->des3 = host->
sg_dma + (
sizeof(
struct idmac_desc) * (i + 1));
425 p->des0 = IDMAC_DES0_ER;
439 .
init = dw_mci_idmac_init,
440 .start = dw_mci_idmac_start_dma,
441 .stop = dw_mci_idmac_stop_dma,
442 .complete = dw_mci_idmac_complete_dma,
443 .cleanup = dw_mci_dma_cleanup,
447 static int dw_mci_pre_dma_transfer(
struct dw_mci *host,
452 unsigned int i, sg_len;
476 dw_mci_get_dma_dir(data));
486 static void dw_mci_pre_req(
struct mmc_host *mmc,
493 if (!slot->
host->use_dma || !data)
501 if (dw_mci_pre_dma_transfer(slot->
host, mrq->
data, 1) < 0)
505 static void dw_mci_post_req(
struct mmc_host *mmc,
512 if (!slot->
host->use_dma || !data)
519 dw_mci_get_dma_dir(data));
523 static int dw_mci_submit_data_dma(
struct dw_mci *host,
struct mmc_data *data)
534 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
543 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
544 (
unsigned long)host->
sg_cpu, (
unsigned long)host->
sg_dma,
557 host->
dma_ops->start(host, sg_len);
562 static void dw_mci_submit_data(
struct dw_mci *host,
struct mmc_data *data)
577 if (dw_mci_submit_data_dma(host, data)) {
616 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
617 cmd, arg, cmd_status);
620 static void dw_mci_setup_bus(
struct dw_mci_slot *slot)
638 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
674 static void __dw_mci_start_request(
struct dw_mci *host,
683 if (host->
pdata->select_slot)
684 host->
pdata->select_slot(slot->
id);
687 dw_mci_setup_bus(slot);
698 dw_mci_set_timeout(host);
703 cmdflags = dw_mci_prepare_command(slot->
mmc, cmd);
710 dw_mci_submit_data(host, data);
714 dw_mci_start_command(host, cmd, cmdflags);
720 static void dw_mci_start_request(
struct dw_mci *host,
727 __dw_mci_start_request(host, slot, cmd);
734 dev_vdbg(&slot->
mmc->class_dev,
"queue request: state=%d\n",
741 dw_mci_start_request(host, slot);
759 spin_lock_bh(&host->
lock);
762 spin_unlock_bh(&host->
lock);
768 dw_mci_queue_request(host, slot, mrq);
770 spin_unlock_bh(&host->
lock);
798 regs |= (0x1 << slot->
id) << 16;
800 regs &= ~(0x1 << slot->
id) << 16;
812 if (drv_data && drv_data->
set_ios)
824 static int dw_mci_get_ro(
struct mmc_host *mmc)
840 read_only ?
"read-only" :
"read-write");
845 static int dw_mci_get_cd(
struct mmc_host *mmc)
877 static void dw_mci_disable_low_power(
struct dw_mci_slot *slot)
885 if (clk_en_a & clken_low_pwr) {
886 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
892 static void dw_mci_enable_sdio_irq(
struct mmc_host *mmc,
int enb)
907 dw_mci_disable_low_power(slot);
918 .request = dw_mci_request,
919 .pre_req = dw_mci_pre_req,
920 .post_req = dw_mci_post_req,
921 .set_ios = dw_mci_set_ios,
922 .get_ro = dw_mci_get_ro,
923 .get_cd = dw_mci_get_cd,
924 .enable_sdio_irq = dw_mci_enable_sdio_irq,
938 if (!list_empty(&host->
queue)) {
942 dev_vdbg(host->
dev,
"list not empty: %s is next\n",
945 dw_mci_start_request(host, slot);
951 spin_unlock(&host->
lock);
953 spin_lock(&host->
lock);
956 static void dw_mci_command_complete(
struct dw_mci *host,
struct mmc_command *cmd)
992 dw_mci_stop_dma(host);
998 static void dw_mci_tasklet_func(
unsigned long priv)
1007 spin_lock(&host->
lock);
1009 state = host->
state;
1027 dw_mci_command_complete(host, cmd);
1028 if (cmd == host->
mrq->sbc && !cmd->
error) {
1030 __dw_mci_start_request(host, host->
cur_slot,
1035 if (!host->
mrq->data || cmd->
error) {
1036 dw_mci_request_end(host, host->
mrq);
1046 dw_mci_stop_dma(host);
1048 send_stop_cmd(host, data);
1109 dw_mci_request_end(host, host->
mrq);
1113 if (host->
mrq->sbc && !data->
error) {
1114 data->
stop->error = 0;
1115 dw_mci_request_end(host, host->
mrq);
1121 send_stop_cmd(host, data);
1130 dw_mci_command_complete(host, host->
mrq->stop);
1131 dw_mci_request_end(host, host->
mrq);
1142 }
while (state != prev_state);
1146 spin_unlock(&host->
lock);
1151 static void dw_mci_set_part_bytes(
struct dw_mci *host,
void *
buf,
int cnt)
1158 static int dw_mci_push_part_bytes(
struct dw_mci *host,
void *buf,
int cnt)
1167 static int dw_mci_pull_part_bytes(
struct dw_mci *host,
void *buf,
int cnt)
1180 static void dw_mci_pull_final_bytes(
struct dw_mci *host,
void *buf,
int cnt)
1187 static void dw_mci_push_data16(
struct dw_mci *host,
void *buf,
int cnt)
1191 int len = dw_mci_push_part_bytes(host, buf, cnt);
1200 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1201 if (
unlikely((
unsigned long)buf & 0x1)) {
1203 u16 aligned_buf[64];
1204 int len =
min(cnt & -2, (
int)
sizeof(aligned_buf));
1205 int items = len >> 1;
1208 memcpy(aligned_buf, buf, len);
1212 for (i = 0; i < items; ++
i)
1220 for (; cnt >= 2; cnt -= 2)
1226 dw_mci_set_part_bytes(host, buf, cnt);
1233 static void dw_mci_pull_data16(
struct dw_mci *host,
void *buf,
int cnt)
1235 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1236 if (
unlikely((
unsigned long)buf & 0x1)) {
1239 u16 aligned_buf[64];
1240 int len =
min(cnt & -2, (
int)
sizeof(aligned_buf));
1241 int items = len >> 1;
1243 for (i = 0; i < items; ++
i)
1247 memcpy(buf, aligned_buf, len);
1255 for (; cnt >= 2; cnt -= 2)
1261 dw_mci_pull_final_bytes(host, buf, cnt);
1265 static void dw_mci_push_data32(
struct dw_mci *host,
void *buf,
int cnt)
1269 int len = dw_mci_push_part_bytes(host, buf, cnt);
1278 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1279 if (
unlikely((
unsigned long)buf & 0x3)) {
1281 u32 aligned_buf[32];
1282 int len =
min(cnt & -4, (
int)
sizeof(aligned_buf));
1283 int items = len >> 2;
1286 memcpy(aligned_buf, buf, len);
1290 for (i = 0; i < items; ++
i)
1298 for (; cnt >= 4; cnt -= 4)
1304 dw_mci_set_part_bytes(host, buf, cnt);
1311 static void dw_mci_pull_data32(
struct dw_mci *host,
void *buf,
int cnt)
1313 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1314 if (
unlikely((
unsigned long)buf & 0x3)) {
1317 u32 aligned_buf[32];
1318 int len =
min(cnt & -4, (
int)
sizeof(aligned_buf));
1319 int items = len >> 2;
1321 for (i = 0; i < items; ++
i)
1325 memcpy(buf, aligned_buf, len);
1333 for (; cnt >= 4; cnt -= 4)
1339 dw_mci_pull_final_bytes(host, buf, cnt);
1343 static void dw_mci_push_data64(
struct dw_mci *host,
void *buf,
int cnt)
1347 int len = dw_mci_push_part_bytes(host, buf, cnt);
1356 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1357 if (
unlikely((
unsigned long)buf & 0x7)) {
1359 u64 aligned_buf[16];
1360 int len =
min(cnt & -8, (
int)
sizeof(aligned_buf));
1361 int items = len >> 3;
1364 memcpy(aligned_buf, buf, len);
1368 for (i = 0; i < items; ++
i)
1376 for (; cnt >= 8; cnt -= 8)
1382 dw_mci_set_part_bytes(host, buf, cnt);
1389 static void dw_mci_pull_data64(
struct dw_mci *host,
void *buf,
int cnt)
1391 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1392 if (
unlikely((
unsigned long)buf & 0x7)) {
1395 u64 aligned_buf[16];
1396 int len =
min(cnt & -8, (
int)
sizeof(aligned_buf));
1397 int items = len >> 3;
1399 for (i = 0; i < items; ++
i)
1403 memcpy(buf, aligned_buf, len);
1411 for (; cnt >= 8; cnt -= 8)
1417 dw_mci_pull_final_bytes(host, buf, cnt);
1421 static void dw_mci_pull_data(
struct dw_mci *host,
void *buf,
int cnt)
1426 len = dw_mci_pull_part_bytes(host, buf, cnt);
1436 static void dw_mci_read_data_pio(
struct dw_mci *host)
1444 unsigned int nbytes = 0, len;
1445 unsigned int remain, fcnt;
1451 host->
sg = sg_miter->
__sg;
1452 buf = sg_miter->
addr;
1453 remain = sg_miter->
length;
1459 len =
min(remain, fcnt);
1462 dw_mci_pull_data(host, (
void *)(buf + offset), len);
1490 static void dw_mci_write_data_pio(
struct dw_mci *host)
1498 unsigned int nbytes = 0, len;
1500 unsigned int remain, fcnt;
1506 host->
sg = sg_miter->
__sg;
1507 buf = sg_miter->
addr;
1508 remain = sg_miter->
length;
1512 fcnt = ((fifo_depth -
1515 len =
min(remain, fcnt);
1518 host->
push_data(host, (
void *)(buf + offset), len);
1546 static void dw_mci_cmd_interrupt(
struct dw_mci *host,
u32 status)
1554 tasklet_schedule(&host->
tasklet);
1561 unsigned int pass_count = 0;
1581 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
1587 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
1589 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
1593 tasklet_schedule(&host->
tasklet);
1597 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
1603 dw_mci_read_data_pio(host);
1606 tasklet_schedule(&host->
tasklet);
1609 if (pending & SDMMC_INT_RXDR) {
1612 dw_mci_read_data_pio(host);
1615 if (pending & SDMMC_INT_TXDR) {
1618 dw_mci_write_data_pio(host);
1622 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
1623 dw_mci_cmd_interrupt(host, pending);
1636 mmc_signal_sdio_irq(slot->
mmc);
1640 }
while (pass_count++ < 5);
1642 #ifdef CONFIG_MMC_DW_IDMAC
1648 host->
dma_ops->complete(host);
1667 present = dw_mci_get_cd(mmc);
1670 present ?
"inserted" :
"removed");
1673 if (present != 0 && host->
pdata->setpower)
1676 spin_lock_bh(&host->
lock);
1688 if (mrq == host->
mrq) {
1692 switch (host->
state) {
1702 dw_mci_stop_dma(host);
1716 dw_mci_request_end(host, mrq);
1725 spin_unlock(&host->
lock);
1727 spin_lock(&host->
lock);
1747 #ifdef CONFIG_MMC_DW_IDMAC
1756 spin_unlock_bh(&host->
lock);
1759 if (present == 0 && host->
pdata->setpower)
1760 host->
pdata->setpower(slot->
id, 0);
1762 present = dw_mci_get_cd(mmc);
1783 if (!addr || (len <
sizeof(
int)))
1792 static u32 dw_mci_of_get_bus_wd(
struct device *dev,
u8 slot)
1794 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
1800 if (of_property_read_u32(np,
"bus-width", &bus_wd))
1801 dev_err(dev,
"bus-width property not found, assuming width"
1806 static u32 dw_mci_of_get_bus_wd(
struct device *dev,
u8 slot)
1816 static int dw_mci_init_slot(
struct dw_mci *host,
unsigned int id)
1828 slot = mmc_priv(mmc);
1834 mmc->
ops = &dw_mci_ops;
1838 if (host->
pdata->get_ocr)
1847 if (host->
pdata->setpower)
1848 host->
pdata->setpower(
id, 0);
1850 if (host->
pdata->caps)
1853 if (host->
dev->of_node) {
1860 if (drv_data && drv_data->
caps)
1861 mmc->
caps |= drv_data->
caps[ctrl_id];
1863 if (host->
pdata->caps2)
1866 if (host->
pdata->get_bus_wd)
1867 bus_width = host->
pdata->get_bus_wd(slot->
id);
1868 else if (host->
dev->of_node)
1869 bus_width = dw_mci_of_get_bus_wd(host->
dev, slot->
id);
1875 slot_np = dw_mci_of_find_slot_node(host->
dev, slot->
id);
1876 ret = drv_data->
setup_bus(host, slot_np, bus_width);
1881 switch (bus_width) {
1891 if (host->
pdata->blk_settings) {
1899 #ifdef CONFIG_MMC_DW_IDMAC
1915 if (IS_ERR(host->
vmmc)) {
1921 if (dw_mci_get_cd(mmc))
1928 #if defined(CONFIG_DEBUG_FS)
1929 dw_mci_init_debugfs(slot);
1948 static void dw_mci_cleanup_slot(
struct dw_mci_slot *slot,
unsigned int id)
1951 if (slot->
host->pdata->exit)
1952 slot->
host->pdata->exit(
id);
1960 static void dw_mci_init_dma(
struct dw_mci *host)
1966 dev_err(host->
dev,
"%s: could not alloc DMA memory\n",
1972 #ifdef CONFIG_MMC_DW_IDMAC
1973 host->
dma_ops = &dw_mci_idmac_ops;
1974 dev_info(host->
dev,
"Using internal DMA controller.\n");
1982 if (host->
dma_ops->init(host)) {
1983 dev_err(host->
dev,
"%s: Unable to initialize "
1984 "DMA Controller.\n", __func__);
1988 dev_err(host->
dev,
"DMA initialization not found.\n");
2001 static bool mci_wait_reset(
struct device *dev,
struct dw_mci *host)
2017 dev_err(dev,
"Timeout resetting block (ctrl %#x)\n", ctrl);
2023 static struct dw_mci_of_quirks {
2028 .quirk =
"supports-highspeed",
2031 .quirk =
"broken-cd",
2046 dev_err(dev,
"could not allocate memory for pdata\n");
2051 if (of_property_read_u32(dev->
of_node,
"num-slots",
2053 dev_info(dev,
"num-slots property not found, "
2054 "assuming 1 slot is available\n");
2059 for (idx = 0; idx <
ARRAY_SIZE(of_quirks); idx++)
2061 pdata->
quirks |= of_quirks[idx].id;
2063 if (of_property_read_u32(np,
"fifo-depth", &pdata->
fifo_depth))
2064 dev_info(dev,
"fifo-depth property not found, using "
2065 "value of FIFOTH register as default\n");
2067 of_property_read_u32(np,
"card-detect-delay", &pdata->
detect_delay_ms);
2069 if (drv_data && drv_data->
parse_dt) {
2072 return ERR_PTR(ret);
2093 host->
pdata = dw_mci_parse_dt(host);
2094 if (IS_ERR(host->
pdata)) {
2095 dev_err(host->
dev,
"platform data not available\n");
2100 if (!host->
pdata->select_slot && host->
pdata->num_slots > 1) {
2102 "Platform data must supply select_slot function\n");
2108 dev_dbg(host->
dev,
"biu clock not available\n");
2110 ret = clk_prepare_enable(host->
biu_clk);
2112 dev_err(host->
dev,
"failed to enable biu clock\n");
2120 dev_dbg(host->
dev,
"ciu clock not available\n");
2122 ret = clk_prepare_enable(host->
ciu_clk);
2124 dev_err(host->
dev,
"failed to enable ciu clock\n");
2139 "implementation specific clock setup failed\n");
2146 "Platform data must supply bus speed\n");
2154 INIT_LIST_HEAD(&host->
queue);
2166 }
else if (i == 2) {
2174 "HCON reports a reserved host data width!\n"
2175 "Defaulting to 32-bit access.\n");
2183 if (!mci_wait_reset(host->
dev, host))
2187 dw_mci_init_dma(host);
2200 if (!host->
pdata->fifo_depth) {
2208 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
2210 fifo_size = host->
pdata->fifo_depth;
2213 host->
fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
2214 ((fifo_size/2) << 0));
2224 if (!host->card_workqueue)
2226 INIT_WORK(&host->card_work, dw_mci_work_routine_card);
2227 ret =
request_irq(host->irq, dw_mci_interrupt, host->irq_flags,
"dw-mci", host);
2231 if (host->pdata->num_slots)
2232 host->num_slots = host->pdata->num_slots;
2234 host->num_slots = ((
mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2242 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2246 dev_info(host->dev,
"DW MMC controller at irq %d, "
2247 "%d bit host data width, "
2249 host->irq, width, fifo_size);
2252 for (i = 0; i < host->num_slots; i++) {
2253 ret = dw_mci_init_slot(host, i);
2255 dev_dbg(host->dev,
"slot %d init failed\n", i);
2261 dev_info(host->dev,
"%d slots initialized\n", init_slots);
2263 dev_dbg(host->dev,
"attempted to initialize %d slots, "
2264 "but failed on all\n", host->num_slots);
2273 dev_info(host->dev,
"Version ID is %04x\n", host->verid);
2281 dev_info(host->dev,
"Internal DMAC interrupt fix enabled.\n");
2292 if (host->use_dma && host->dma_ops->exit)
2293 host->dma_ops->exit(host);
2295 host->sg_cpu, host->sg_dma);
2303 if (!IS_ERR(host->ciu_clk)) {
2304 clk_disable_unprepare(host->ciu_clk);
2308 if (!IS_ERR(host->biu_clk)) {
2309 clk_disable_unprepare(host->biu_clk);
2326 dw_mci_cleanup_slot(host->
slot[i], i);
2346 clk_disable_unprepare(host->
ciu_clk);
2348 clk_disable_unprepare(host->
biu_clk);
2356 #ifdef CONFIG_PM_SLEEP
2360 int dw_mci_suspend(
struct dw_mci *host)
2371 slot = host->
slot[
i];
2386 int dw_mci_resume(
struct dw_mci *host)
2393 if (!mci_wait_reset(host->
dev, host)) {
2406 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2423 static int __init dw_mci_init(
void)
2425 printk(
KERN_INFO "Synopsys Designware Multimedia Card Interface Driver");
2429 static void __exit dw_mci_exit(
void)