···401401static inline void dw_mci_set_cto(struct dw_mci *host)402402{403403 unsigned int cto_clks;404404+ unsigned int cto_div;404405 unsigned int cto_ms;406406+ unsigned long irqflags;405407406408 cto_clks = mci_readl(host, TMOUT) & 0xff;407407- cto_ms = DIV_ROUND_UP(cto_clks, host->bus_hz / 1000);409409+ cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;410410+ if (cto_div == 0)411411+ cto_div = 1;412412+ cto_ms = DIV_ROUND_UP(MSEC_PER_SEC * cto_clks * cto_div, host->bus_hz);408413409414 /* add a bit spare time */410415 cto_ms += 10;411416412412- mod_timer(&host->cto_timer,413413- jiffies + msecs_to_jiffies(cto_ms) + 1);417417+ /*418418+ * The durations we're working with are fairly short so we have to be419419+ * extra careful about synchronization here. Specifically in hardware a420420+ * command timeout is _at most_ 5.1 ms, so that means we expect an421421+ * interrupt (either command done or timeout) to come rather quickly422422+ * after the mci_writel. ...but just in case we have a long interrupt423423+ * latency let's add a bit of paranoia.424424+ *425425+ * In general we'll assume that at least an interrupt will be asserted426426+ * in hardware by the time the cto_timer runs. ...and if it hasn't427427+ * been asserted in hardware by that time then we'll assume it'll never428428+ * come.429429+ */430430+ spin_lock_irqsave(&host->irq_lock, irqflags);431431+ if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))432432+ mod_timer(&host->cto_timer,433433+ jiffies + msecs_to_jiffies(cto_ms) + 1);434434+ spin_unlock_irqrestore(&host->irq_lock, irqflags);414435}415436416437static void dw_mci_start_command(struct dw_mci *host,···446425 wmb(); /* drain writebuffer */447426 dw_mci_wait_while_busy(host, cmd_flags);448427428428+ mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);429429+449430 /* response expected command only */450431 if (cmd_flags & SDMMC_CMD_RESP_EXP)451432 dw_mci_set_cto(host);452452-453453- mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);454433}455434456435static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)···19361915static void dw_mci_set_drto(struct dw_mci *host)19371916{19381917 unsigned int drto_clks;19181918+ unsigned int drto_div;19391919 unsigned int drto_ms;1940192019411921 drto_clks = mci_readl(host, TMOUT) >> 8;19421942- drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);19221922+ drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;19231923+ if (drto_div == 0)19241924+ drto_div = 1;19251925+ drto_ms = DIV_ROUND_UP(MSEC_PER_SEC * drto_clks * drto_div,19261926+ host->bus_hz);1943192719441928 /* add a bit spare time */19451929 drto_ms += 10;1946193019471931 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));19321932+}19331933+19341934+static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)19351935+{19361936+ if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))19371937+ return false;19381938+19391939+ /*19401940+ * Really be certain that the timer has stopped. This is a bit of19411941+ * paranoia and could only really happen if we had really bad19421942+ * interrupt latency and the interrupt routine and timeout were19431943+ * running concurrently so that the del_timer() in the interrupt19441944+ * handler couldn't run.19451945+ */19461946+ WARN_ON(del_timer_sync(&host->cto_timer));19471947+ clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);19481948+19491949+ return true;19481950}1949195119501952static void dw_mci_tasklet_func(unsigned long priv)···1996195219971953 case STATE_SENDING_CMD11:19981954 case STATE_SENDING_CMD:19991999- if (!test_and_clear_bit(EVENT_CMD_COMPLETE,20002000- &host->pending_events))19551955+ if (!dw_mci_clear_pending_cmd_complete(host))20011956 break;2002195720031958 cmd = host->cmd;···21652122 /* fall through */2166212321672124 case STATE_SENDING_STOP:21682168- if (!test_and_clear_bit(EVENT_CMD_COMPLETE,21692169- &host->pending_events))21252125+ if (!dw_mci_clear_pending_cmd_complete(host))21702126 break;2171212721722128 /* CMD error in data command */···2612257026132571static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)26142572{25732573+ del_timer(&host->cto_timer);25742574+26152575 if (!host->cmd_status)26162576 host->cmd_status = status;26172577···26382594 struct dw_mci *host = dev_id;26392595 u32 pending;26402596 struct dw_mci_slot *slot = host->slot;25972597+ unsigned long irqflags;2641259826422599 pending = mci_readl(host, MINTSTS); /* read-only mask reg */26432600···26462601 /* Check volt switch first, since it can look like an error */26472602 if ((host->state == STATE_SENDING_CMD11) &&26482603 (pending & SDMMC_INT_VOLT_SWITCH)) {26492649- unsigned long irqflags;26502650-26512604 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);26522605 pending &= ~SDMMC_INT_VOLT_SWITCH;26532606···26612618 }2662261926632620 if (pending & DW_MCI_CMD_ERROR_FLAGS) {26212621+ spin_lock_irqsave(&host->irq_lock, irqflags);26222622+26642623 del_timer(&host->cto_timer);26652624 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);26662625 host->cmd_status = pending;26672626 smp_wmb(); /* drain writebuffer */26682627 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);26282628+26292629+ spin_unlock_irqrestore(&host->irq_lock, irqflags);26692630 }2670263126712632 if (pending & DW_MCI_DATA_ERROR_FLAGS) {···27092662 }2710266327112664 if (pending & SDMMC_INT_CMD_DONE) {27122712- del_timer(&host->cto_timer);26652665+ spin_lock_irqsave(&host->irq_lock, irqflags);26662666+27132667 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);27142668 dw_mci_cmd_interrupt(host, pending);26692669+26702670+ spin_unlock_irqrestore(&host->irq_lock, irqflags);27152671 }2716267227172673 if (pending & SDMMC_INT_CD) {···29882938static void dw_mci_cto_timer(unsigned long arg)29892939{29902940 struct dw_mci *host = (struct dw_mci *)arg;29412941+ unsigned long irqflags;29422942+ u32 pending;2991294329442944+ spin_lock_irqsave(&host->irq_lock, irqflags);29452945+29462946+ /*29472947+ * If somehow we have very bad interrupt latency it's remotely possible29482948+ * that the timer could fire while the interrupt is still pending or29492949+ * while the interrupt is midway through running. Let's be paranoid29502950+ * and detect those two cases. Note that this is paranoia is somewhat29512951+ * justified because in this function we don't actually cancel the29522952+ * pending command in the controller--we just assume it will never come.29532953+ */29542954+ pending = mci_readl(host, MINTSTS); /* read-only mask reg */29552955+ if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {29562956+ /* The interrupt should fire; no need to act but we can warn */29572957+ dev_warn(host->dev, "Unexpected interrupt latency\n");29582958+ goto exit;29592959+ }29602960+ if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {29612961+ /* Presumably interrupt handler couldn't delete the timer */29622962+ dev_warn(host->dev, "CTO timeout when already completed\n");29632963+ goto exit;29642964+ }29652965+29662966+ /*29672967+ * Continued paranoia to make sure we're in the state we expect.29682968+ * This paranoia isn't really justified but it seems good to be safe.29692969+ */29922970 switch (host->state) {29932971 case STATE_SENDING_CMD11:29942972 case STATE_SENDING_CMD:···30352957 host->state);30362958 break;30372959 }29602960+29612961+exit:29622962+ spin_unlock_irqrestore(&host->irq_lock, irqflags);30382963}3039296430402965static void dw_mci_dto_timer(unsigned long arg)