13 #include <linux/device.h>
21 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/types.h>
35 #include <mach/atmel-mci.h>
40 #include <asm/unaligned.h>
43 #include <mach/board.h>
47 #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
48 #define ATMCI_DMA_THRESHOLD 16
256 #define ATMCI_CARD_PRESENT 0
257 #define ATMCI_CARD_NEED_INIT 1
258 #define ATMCI_SHUTDOWN 2
259 #define ATMCI_SUSPENDED 3
268 #define atmci_test_and_clear_pending(host, event) \
269 test_and_clear_bit(event, &host->pending_events)
270 #define atmci_set_completed(host, event) \
271 set_bit(event, &host->completed_events)
272 #define atmci_set_pending(host, event) \
273 set_bit(event, &host->pending_events)
279 static int atmci_req_show(
struct seq_file *
s,
void *
v)
288 spin_lock_bh(&slot->
host->lock);
298 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
303 seq_printf(s,
"DATA %u / %u * %u flg %x err %d\n",
308 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
314 spin_unlock_bh(&slot->
host->lock);
326 .open = atmci_req_open,
332 static void atmci_show_status_reg(
struct seq_file *
s,
335 static const char *sr_bit[] = {
368 if (value & (1 << i)) {
378 static int atmci_regs_show(
struct seq_file *s,
void *
v)
392 spin_lock_bh(&host->
lock);
396 spin_unlock_bh(&host->
lock);
402 if (host->
caps.has_odd_clk_div)
412 seq_printf(s,
"BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
416 if (host->
caps.has_cstor_reg)
421 atmci_show_status_reg(s,
"SR", buf[
ATMCI_SR / 4]);
422 atmci_show_status_reg(s,
"IMR", buf[
ATMCI_IMR / 4]);
424 if (host->
caps.has_dma_conf_reg) {
428 seq_printf(s,
"DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
431 1 << (((val >> 4) & 3) + 1) : 1,
434 if (host->
caps.has_cfg_reg) {
458 .open = atmci_regs_open,
506 #if defined(CONFIG_OF)
523 dev_err(&pdev->
dev,
"device node not found\n");
529 dev_err(&pdev->
dev,
"could not allocate memory for pdata\n");
534 if (of_property_read_u32(cnp,
"reg", &slot_id)) {
535 dev_warn(&pdev->
dev,
"reg property is missing for %s\n",
541 dev_warn(&pdev->
dev,
"can't have more than %d slots\n",
546 if (of_property_read_u32(cnp,
"bus-width",
547 &pdata->
slot[slot_id].bus_width))
548 pdata->
slot[slot_id].bus_width = 1;
550 pdata->
slot[slot_id].detect_pin =
551 of_get_named_gpio(cnp,
"cd-gpios", 0);
553 pdata->
slot[slot_id].detect_is_active_high =
554 of_property_read_bool(cnp,
"cd-inverted");
556 pdata->
slot[slot_id].wp_pin =
557 of_get_named_gpio(cnp,
"wp-gpios", 0);
570 static inline unsigned int atmci_get_version(
struct atmel_mci *host)
575 static void atmci_timeout_timer(
unsigned long data)
583 if (host->
mrq->cmd->data) {
593 tasklet_schedule(&host->
tasklet);
596 static inline unsigned int atmci_ns_to_clocks(
struct atmel_mci *host,
609 static void atmci_set_timeout(
struct atmel_mci *host,
612 static unsigned dtomul_to_shift[] = {
613 0, 4, 7, 8, 10, 12, 16, 20
619 timeout = atmci_ns_to_clocks(host, data->
timeout_ns)
622 for (dtomul = 0; dtomul < 8; dtomul++) {
623 unsigned shift = dtomul_to_shift[dtomul];
624 dtocyc = (timeout + (1 << shift) - 1) >> shift;
634 dev_vdbg(&slot->
mmc->class_dev,
"setting timeout to %u cycles\n",
635 dtocyc << dtomul_to_shift[dtomul]);
642 static u32 atmci_prepare_command(
struct mmc_host *mmc,
678 else if (data->
blocks > 1)
691 static void atmci_send_command(
struct atmel_mci *host,
698 "start command: ARGR=0x%08x CMDR=0x%08x\n",
699 cmd->
arg, cmd_flags);
716 static void atmci_pdc_set_single_buf(
struct atmel_mci *host,
719 u32 pointer_reg, counter_reg;
735 if (!host->
caps.has_rwproof) {
767 static void atmci_pdc_set_both_buf(
struct atmel_mci *host,
int dir)
777 static void atmci_pdc_cleanup(
struct atmel_mci *host)
793 static void atmci_pdc_complete(
struct atmel_mci *host)
800 if ((!host->
caps.has_rwproof)
802 if (host->
caps.has_bad_data_ordering)
806 host->
buffer, transfer_size);
809 atmci_pdc_cleanup(host);
817 "(%s) set pending xfer complete\n", __func__);
819 tasklet_schedule(&host->
tasklet);
823 static void atmci_dma_cleanup(
struct atmel_mci *host)
837 static void atmci_dma_complete(
void *
arg)
844 if (host->
caps.has_dma_conf_reg)
848 atmci_dma_cleanup(host);
856 "(%s) set pending xfer complete\n", __func__);
858 tasklet_schedule(&host->
tasklet);
961 if ((!host->
caps.has_rwproof)
965 if (host->
caps.has_bad_data_ordering)
971 atmci_pdc_set_both_buf(host,
1004 return atmci_prepare_data(host, data);
1005 if (data->
blksz & 3)
1006 return atmci_prepare_data(host, data);
1010 return atmci_prepare_data(host, data);
1014 chan = host->
dma.chan;
1024 maxburst = atmci_convert_chksize(host->
dma_conf.src_maxburst);
1028 maxburst = atmci_convert_chksize(host->
dma_conf.dst_maxburst);
1031 if (host->
caps.has_dma_conf_reg)
1036 data->
sg_len, direction);
1038 dmaengine_slave_config(chan, &host->
dma_conf);
1039 desc = dmaengine_prep_slave_sg(chan,
1040 data->
sg, sglen, slave_dirn,
1045 host->
dma.data_desc = desc;
1046 desc->
callback = atmci_dma_complete;
1080 dmaengine_submit(desc);
1081 dma_async_issue_pending(chan);
1085 static void atmci_stop_transfer(
struct atmel_mci *host)
1088 "(%s) set pending xfer complete\n", __func__);
1096 static void atmci_stop_transfer_pdc(
struct atmel_mci *host)
1101 static void atmci_stop_transfer_dma(
struct atmel_mci *host)
1106 dmaengine_terminate_all(chan);
1107 atmci_dma_cleanup(host);
1111 "(%s) set pending xfer complete\n", __func__);
1121 static void atmci_start_request(
struct atmel_mci *host,
1139 dev_dbg(&host->
pdev->dev,
"start request: cmd %u\n", mrq->
cmd->opcode);
1147 if (host->
caps.has_cfg_reg)
1156 dev_dbg(&slot->
mmc->class_dev,
"WARNING: IMR=0x%08x\n",
1168 atmci_set_timeout(host, slot, data);
1181 cmdflags = atmci_prepare_command(slot->
mmc, cmd);
1182 atmci_send_command(host, cmd, cmdflags);
1209 static void atmci_queue_request(
struct atmel_mci *host,
1212 dev_vdbg(&slot->
mmc->class_dev,
"queue request: state=%d\n",
1215 spin_lock_bh(&host->
lock);
1219 atmci_start_request(host, slot);
1224 spin_unlock_bh(&host->
lock);
1252 if (data && data->
blocks > 1 && data->
blksz & 3) {
1257 atmci_queue_request(host, slot, mrq);
1277 unsigned int clock_min = ~0
U;
1280 spin_lock_bh(&host->
lock);
1285 if (host->
caps.has_cfg_reg)
1295 if (host->
slot[i] && host->
slot[i]->clock
1296 && host->
slot[i]->clock < clock_min)
1297 clock_min = host->
slot[
i]->clock;
1301 if (host->
caps.has_odd_clk_div) {
1305 "clock %u too slow; using %lu\n",
1306 clock_min, host->
bus_hz / (511 + 2));
1315 "clock %u too slow; using %lu\n",
1316 clock_min, host->
bus_hz / (2 * 256));
1327 if (host->
caps.has_rwproof)
1330 if (host->
caps.has_cfg_reg) {
1338 if (list_empty(&host->
queue)) {
1340 if (host->
caps.has_cfg_reg)
1346 spin_unlock_bh(&host->
lock);
1348 bool any_slot_active =
false;
1350 spin_lock_bh(&host->
lock);
1353 if (host->
slot[i] && host->
slot[i]->clock) {
1354 any_slot_active =
true;
1358 if (!any_slot_active) {
1366 spin_unlock_bh(&host->
lock);
1390 static int atmci_get_ro(
struct mmc_host *mmc)
1395 if (gpio_is_valid(slot->
wp_pin)) {
1398 read_only ?
"read-only" :
"read-write");
1404 static int atmci_get_cd(
struct mmc_host *mmc)
1413 present ?
"" :
"not ");
1419 static void atmci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
1431 .request = atmci_request,
1432 .set_ios = atmci_set_ios,
1433 .get_ro = atmci_get_ro,
1434 .get_cd = atmci_get_cd,
1435 .enable_sdio_irq = atmci_enable_sdio_irq,
1455 if (host->
caps.has_cfg_reg)
1461 if (!list_empty(&host->
queue)) {
1465 dev_vdbg(&host->
pdev->dev,
"list not empty: %s is next\n",
1468 atmci_start_request(host, slot);
1476 spin_unlock(&host->
lock);
1478 spin_lock(&host->
lock);
1481 static void atmci_command_complete(
struct atmel_mci *host,
1498 else if (host->
mrq->data && (host->
mrq->data->blksz & 3)) {
1499 if (host->
caps.need_blksz_mul_4) {
1507 static void atmci_detect_change(
unsigned long data)
1528 dev_vdbg(&slot->
mmc->class_dev,
"detect change: %d (was %d)\n",
1529 present, present_old);
1531 if (present != present_old) {
1536 present ?
"inserted" :
"removed");
1538 spin_lock(&host->
lock);
1548 if (mrq == host->
mrq) {
1556 if (host->
caps.has_cfg_reg)
1562 switch (host->
state) {
1584 atmci_request_end(host, mrq);
1593 spin_unlock(&host->
lock);
1595 spin_lock(&host->
lock);
1598 spin_unlock(&host->
lock);
1604 static void atmci_tasklet_func(
unsigned long priv)
1613 spin_lock(&host->
lock);
1615 state = host->
state;
1618 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1624 dev_dbg(&host->
pdev->dev,
"FSM: state=%d\n", state);
1642 dev_dbg(&host->
pdev->dev,
"set completed cmd ready\n");
1645 atmci_command_complete(host, mrq->
cmd);
1648 "command with data transfer");
1653 if (mrq->
cmd->error) {
1664 "command response need waiting notbusy");
1675 dev_dbg(&host->
pdev->dev,
"set completed data error\n");
1688 dev_dbg(&host->
pdev->dev,
"FSM: xfer complete?\n");
1694 "(%s) set completed xfer complete\n",
1698 if (host->
caps.need_notbusy_for_read_ops ||
1702 }
else if (host->
mrq->stop) {
1704 atmci_send_stop_cmd(host, data);
1726 dev_dbg(&host->
pdev->dev,
"set completed not busy\n");
1735 if (host->
mrq->stop) {
1738 atmci_send_stop_cmd(host, data);
1767 atmci_command_complete(host, mrq->
stop);
1768 if (mrq->
stop->error) {
1797 atmci_request_end(host, host->
mrq);
1801 }
while (state != prev_state);
1805 spin_unlock(&host->
lock);
1808 static void atmci_read_data_pio(
struct atmel_mci *host)
1811 void *buf = sg_virt(sg);
1826 if (offset == sg->
length) {
1837 memcpy(buf + offset, &value, remaining);
1838 nbytes += remaining;
1845 offset = 4 - remaining;
1847 memcpy(buf, (
u8 *)&value + remaining, offset);
1854 | ATMCI_DATA_ERROR_FLAGS));
1874 static void atmci_write_data_pio(
struct atmel_mci *host)
1877 void *buf = sg_virt(sg);
1882 unsigned int nbytes = 0;
1891 if (offset == sg->
length) {
1903 memcpy(&value, buf + offset, remaining);
1904 nbytes += remaining;
1912 offset = 4 - remaining;
1914 memcpy((
u8 *)&value + remaining, buf, offset);
1920 if (status & ATMCI_DATA_ERROR_FLAGS) {
1922 | ATMCI_DATA_ERROR_FLAGS));
1942 static void atmci_sdio_interrupt(
struct atmel_mci *host,
u32 status)
1948 if (slot && (status & slot->
sdio_irq)) {
1949 mmc_signal_sdio_irq(slot->
mmc);
1959 unsigned int pass_count = 0;
1964 pending = status &
mask;
1968 if (pending & ATMCI_DATA_ERROR_FLAGS) {
1976 dev_dbg(&host->
pdev->dev,
"set pending data error\n");
1979 tasklet_schedule(&host->
tasklet);
1983 dev_dbg(&host->
pdev->dev,
"IRQ: tx buffer empty\n");
1996 atmci_pdc_complete(host);
1999 dev_dbg(&host->
pdev->dev,
"IRQ: end of tx buffer\n");
2003 atmci_pdc_set_single_buf(host,
2010 dev_dbg(&host->
pdev->dev,
"IRQ: rx buffer full\n");
2023 atmci_pdc_complete(host);
2026 dev_dbg(&host->
pdev->dev,
"IRQ: end of rx buffer\n");
2030 atmci_pdc_set_single_buf(host,
2046 dev_dbg(&host->
pdev->dev,
"set pending notbusy\n");
2048 tasklet_schedule(&host->
tasklet);
2055 dev_dbg(&host->
pdev->dev,
"set pending notbusy\n");
2057 tasklet_schedule(&host->
tasklet);
2061 atmci_read_data_pio(host);
2063 atmci_write_data_pio(host);
2070 dev_dbg(&host->
pdev->dev,
"set pending cmd rdy\n");
2072 tasklet_schedule(&host->
tasklet);
2076 atmci_sdio_interrupt(host, status);
2078 }
while (pass_count++ < 5);
2083 static irqreturn_t atmci_detect_interrupt(
int irq,
void *dev_id)
2109 slot = mmc_priv(mmc);
2119 "slot[%u]: bus_width=%u, detect_pin=%d, "
2120 "detect_is_active_high=%s, wp_pin=%d\n",
2125 mmc->
ops = &atmci_ops;
2131 if (host->
caps.has_highspeed)
2141 if (atmci_get_version(host) < 0x200) {
2169 if (gpio_is_valid(slot->
wp_pin)) {
2183 (
unsigned long)slot);
2186 atmci_detect_interrupt,
2188 "mmc-detect", slot);
2191 "could not request IRQ %d for detect pin\n",
2198 atmci_init_debugfs(slot);
2220 if (gpio_is_valid(slot->
wp_pin))
2227 static bool atmci_filter(
struct dma_chan *chan,
void *
slave)
2239 static bool atmci_configure_dma(
struct atmel_mci *host)
2246 pdata = host->
pdev->dev.platform_data;
2260 if (!host->
dma.chan) {
2261 dev_warn(&host->
pdev->dev,
"no DMA channel available\n");
2265 "using %s for DMA transfers\n",
2266 dma_chan_name(host->
dma.chan));
2288 version = atmci_get_version(host);
2290 "version: 0x%x\n", version);
2292 host->
caps.has_dma_conf_reg = 0;
2294 host->
caps.has_cfg_reg = 0;
2295 host->
caps.has_cstor_reg = 0;
2296 host->
caps.has_highspeed = 0;
2297 host->
caps.has_rwproof = 0;
2298 host->
caps.has_odd_clk_div = 0;
2299 host->
caps.has_bad_data_ordering = 1;
2300 host->
caps.need_reset_after_xfer = 1;
2301 host->
caps.need_blksz_mul_4 = 1;
2302 host->
caps.need_notbusy_for_read_ops = 0;
2305 switch (version & 0xf00) {
2307 host->
caps.has_odd_clk_div = 1;
2310 host->
caps.has_dma_conf_reg = 1;
2311 host->
caps.has_pdc = 0;
2312 host->
caps.has_cfg_reg = 1;
2313 host->
caps.has_cstor_reg = 1;
2314 host->
caps.has_highspeed = 1;
2316 host->
caps.has_rwproof = 1;
2317 host->
caps.need_blksz_mul_4 = 0;
2318 host->
caps.need_notbusy_for_read_ops = 1;
2320 host->
caps.has_bad_data_ordering = 0;
2321 host->
caps.need_reset_after_xfer = 0;
2325 host->
caps.has_pdc = 0;
2327 "Unmanaged mci version, set minimum capabilities\n");
2337 unsigned int nr_slots;
2344 pdata = pdev->
dev.platform_data;
2346 pdata = atmci_of_init(pdev);
2347 if (IS_ERR(pdata)) {
2348 dev_err(&pdev->
dev,
"platform data not available\n");
2349 return PTR_ERR(pdata);
2363 INIT_LIST_HEAD(&host->
queue);
2366 if (IS_ERR(host->
mck)) {
2367 ret = PTR_ERR(host->
mck);
2385 ret =
request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->
dev), host);
2387 goto err_request_irq;
2390 atmci_get_cap(host);
2391 if (atmci_configure_dma(host)) {
2395 }
else if (host->
caps.has_pdc) {
2407 platform_set_drvdata(pdev, host);
2414 if (pdata->
slot[0].bus_width) {
2415 ret = atmci_init_slot(host, &pdata->
slot[0],
2422 if (pdata->
slot[1].bus_width) {
2423 ret = atmci_init_slot(host, &pdata->
slot[1],
2429 host->
slot[1]->mmc->max_req_size;
2434 dev_err(&pdev->
dev,
"init failed: no slot defined\n");
2438 if (!host->
caps.has_rwproof) {
2444 dev_err(&pdev->
dev,
"buffer allocation failed\n");
2450 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2451 host->
mapbase, irq, nr_slots);
2470 struct atmel_mci *host = platform_get_drvdata(pdev);
2473 platform_set_drvdata(pdev,
NULL);
2481 atmci_cleanup_slot(host->
slot[i], i);
2490 #ifdef CONFIG_MMC_ATMELMCI_DMA
2505 static int atmci_suspend(
struct device *
dev)
2519 slot = host->
slot[
i];
2535 static int atmci_resume(
struct device *dev)
2545 slot = host->
slot[
i];
2560 #define ATMCI_PM_OPS (&atmci_pm)
2562 #define ATMCI_PM_OPS NULL
2568 .name =
"atmel_mci",
2574 static int __init atmci_init(
void)
2579 static void __exit atmci_exit(
void)