@@ -401,16 +401,37 @@ static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
401401static inline void dw_mci_set_cto (struct dw_mci * host )
402402{
403403 unsigned int cto_clks ;
404+ unsigned int cto_div ;
404405 unsigned int cto_ms ;
406+ unsigned long irqflags ;
405407
406408 cto_clks = mci_readl (host , TMOUT ) & 0xff ;
407- cto_ms = DIV_ROUND_UP (cto_clks , host -> bus_hz / 1000 );
409+ cto_div = (mci_readl (host , CLKDIV ) & 0xff ) * 2 ;
410+ if (cto_div == 0 )
411+ cto_div = 1 ;
412+ cto_ms = DIV_ROUND_UP (MSEC_PER_SEC * cto_clks * cto_div , host -> bus_hz );
408413
409414 /* add a bit spare time */
410415 cto_ms += 10 ;
411416
412- mod_timer (& host -> cto_timer ,
413- jiffies + msecs_to_jiffies (cto_ms ) + 1 );
417+ /*
418+ * The durations we're working with are fairly short so we have to be
419+ * extra careful about synchronization here. Specifically in hardware a
420+ * command timeout is _at most_ 5.1 ms, so that means we expect an
421+ * interrupt (either command done or timeout) to come rather quickly
422+ * after the mci_writel. ...but just in case we have a long interrupt
423+ * latency let's add a bit of paranoia.
424+ *
425+ * In general we'll assume that at least an interrupt will be asserted
426+ * in hardware by the time the cto_timer runs. ...and if it hasn't
427+ * been asserted in hardware by that time then we'll assume it'll never
428+ * come.
429+ */
430+ spin_lock_irqsave (& host -> irq_lock , irqflags );
431+ if (!test_bit (EVENT_CMD_COMPLETE , & host -> pending_events ))
432+ mod_timer (& host -> cto_timer ,
433+ jiffies + msecs_to_jiffies (cto_ms ) + 1 );
434+ spin_unlock_irqrestore (& host -> irq_lock , irqflags );
414435}
415436
416437static void dw_mci_start_command (struct dw_mci * host ,
@@ -425,11 +446,11 @@ static void dw_mci_start_command(struct dw_mci *host,
425446 wmb (); /* drain writebuffer */
426447 dw_mci_wait_while_busy (host , cmd_flags );
427448
449+ mci_writel (host , CMD , cmd_flags | SDMMC_CMD_START );
450+
428451 /* response expected command only */
429452 if (cmd_flags & SDMMC_CMD_RESP_EXP )
430453 dw_mci_set_cto (host );
431-
432- mci_writel (host , CMD , cmd_flags | SDMMC_CMD_START );
433454}
434455
435456static inline void send_stop_abort (struct dw_mci * host , struct mmc_data * data )
@@ -1915,17 +1936,40 @@ static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
19151936static void dw_mci_set_drto (struct dw_mci * host )
19161937{
19171938 unsigned int drto_clks ;
1939+ unsigned int drto_div ;
19181940 unsigned int drto_ms ;
19191941
19201942 drto_clks = mci_readl (host , TMOUT ) >> 8 ;
1921- drto_ms = DIV_ROUND_UP (drto_clks , host -> bus_hz / 1000 );
1943+ drto_div = (mci_readl (host , CLKDIV ) & 0xff ) * 2 ;
1944+ if (drto_div == 0 )
1945+ drto_div = 1 ;
1946+ drto_ms = DIV_ROUND_UP (MSEC_PER_SEC * drto_clks * drto_div ,
1947+ host -> bus_hz );
19221948
19231949 /* add a bit spare time */
19241950 drto_ms += 10 ;
19251951
19261952 mod_timer (& host -> dto_timer , jiffies + msecs_to_jiffies (drto_ms ));
19271953}
19281954
1955+ static bool dw_mci_clear_pending_cmd_complete (struct dw_mci * host )
1956+ {
1957+ if (!test_bit (EVENT_CMD_COMPLETE , & host -> pending_events ))
1958+ return false;
1959+
1960+ /*
1961+ * Really be certain that the timer has stopped. This is a bit of
1962+ * paranoia and could only really happen if we had really bad
1963+ * interrupt latency and the interrupt routine and timeout were
1964+ * running concurrently so that the del_timer() in the interrupt
1965+ * handler couldn't run.
1966+ */
1967+ WARN_ON (del_timer_sync (& host -> cto_timer ));
1968+ clear_bit (EVENT_CMD_COMPLETE , & host -> pending_events );
1969+
1970+ return true;
1971+ }
1972+
19291973static void dw_mci_tasklet_func (unsigned long priv )
19301974{
19311975 struct dw_mci * host = (struct dw_mci * )priv ;
@@ -1952,8 +1996,7 @@ static void dw_mci_tasklet_func(unsigned long priv)
19521996
19531997 case STATE_SENDING_CMD11 :
19541998 case STATE_SENDING_CMD :
1955- if (!test_and_clear_bit (EVENT_CMD_COMPLETE ,
1956- & host -> pending_events ))
1999+ if (!dw_mci_clear_pending_cmd_complete (host ))
19572000 break ;
19582001
19592002 cmd = host -> cmd ;
@@ -2122,8 +2165,7 @@ static void dw_mci_tasklet_func(unsigned long priv)
21222165 /* fall through */
21232166
21242167 case STATE_SENDING_STOP :
2125- if (!test_and_clear_bit (EVENT_CMD_COMPLETE ,
2126- & host -> pending_events ))
2168+ if (!dw_mci_clear_pending_cmd_complete (host ))
21272169 break ;
21282170
21292171 /* CMD error in data command */
@@ -2570,6 +2612,8 @@ static void dw_mci_write_data_pio(struct dw_mci *host)
25702612
25712613static void dw_mci_cmd_interrupt (struct dw_mci * host , u32 status )
25722614{
2615+ del_timer (& host -> cto_timer );
2616+
25732617 if (!host -> cmd_status )
25742618 host -> cmd_status = status ;
25752619
@@ -2594,15 +2638,14 @@ static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
25942638 struct dw_mci * host = dev_id ;
25952639 u32 pending ;
25962640 struct dw_mci_slot * slot = host -> slot ;
2641+ unsigned long irqflags ;
25972642
25982643 pending = mci_readl (host , MINTSTS ); /* read-only mask reg */
25992644
26002645 if (pending ) {
26012646 /* Check volt switch first, since it can look like an error */
26022647 if ((host -> state == STATE_SENDING_CMD11 ) &&
26032648 (pending & SDMMC_INT_VOLT_SWITCH )) {
2604- unsigned long irqflags ;
2605-
26062649 mci_writel (host , RINTSTS , SDMMC_INT_VOLT_SWITCH );
26072650 pending &= ~SDMMC_INT_VOLT_SWITCH ;
26082651
@@ -2618,11 +2661,15 @@ static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
26182661 }
26192662
26202663 if (pending & DW_MCI_CMD_ERROR_FLAGS ) {
2664+ spin_lock_irqsave (& host -> irq_lock , irqflags );
2665+
26212666 del_timer (& host -> cto_timer );
26222667 mci_writel (host , RINTSTS , DW_MCI_CMD_ERROR_FLAGS );
26232668 host -> cmd_status = pending ;
26242669 smp_wmb (); /* drain writebuffer */
26252670 set_bit (EVENT_CMD_COMPLETE , & host -> pending_events );
2671+
2672+ spin_unlock_irqrestore (& host -> irq_lock , irqflags );
26262673 }
26272674
26282675 if (pending & DW_MCI_DATA_ERROR_FLAGS ) {
@@ -2662,9 +2709,12 @@ static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
26622709 }
26632710
26642711 if (pending & SDMMC_INT_CMD_DONE ) {
2665- del_timer (& host -> cto_timer );
2712+ spin_lock_irqsave (& host -> irq_lock , irqflags );
2713+
26662714 mci_writel (host , RINTSTS , SDMMC_INT_CMD_DONE );
26672715 dw_mci_cmd_interrupt (host , pending );
2716+
2717+ spin_unlock_irqrestore (& host -> irq_lock , irqflags );
26682718 }
26692719
26702720 if (pending & SDMMC_INT_CD ) {
@@ -2938,7 +2988,35 @@ static void dw_mci_cmd11_timer(unsigned long arg)
29382988static void dw_mci_cto_timer (unsigned long arg )
29392989{
29402990 struct dw_mci * host = (struct dw_mci * )arg ;
2991+ unsigned long irqflags ;
2992+ u32 pending ;
29412993
2994+ spin_lock_irqsave (& host -> irq_lock , irqflags );
2995+
2996+ /*
2997+ * If somehow we have very bad interrupt latency it's remotely possible
2998+ * that the timer could fire while the interrupt is still pending or
2999+ * while the interrupt is midway through running. Let's be paranoid
3000+ * and detect those two cases. Note that this is paranoia is somewhat
3001+ * justified because in this function we don't actually cancel the
3002+ * pending command in the controller--we just assume it will never come.
3003+ */
3004+ pending = mci_readl (host , MINTSTS ); /* read-only mask reg */
3005+ if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE )) {
3006+ /* The interrupt should fire; no need to act but we can warn */
3007+ dev_warn (host -> dev , "Unexpected interrupt latency\n" );
3008+ goto exit ;
3009+ }
3010+ if (test_bit (EVENT_CMD_COMPLETE , & host -> pending_events )) {
3011+ /* Presumably interrupt handler couldn't delete the timer */
3012+ dev_warn (host -> dev , "CTO timeout when already completed\n" );
3013+ goto exit ;
3014+ }
3015+
3016+ /*
3017+ * Continued paranoia to make sure we're in the state we expect.
3018+ * This paranoia isn't really justified but it seems good to be safe.
3019+ */
29423020 switch (host -> state ) {
29433021 case STATE_SENDING_CMD11 :
29443022 case STATE_SENDING_CMD :
@@ -2957,6 +3035,9 @@ static void dw_mci_cto_timer(unsigned long arg)
29573035 host -> state );
29583036 break ;
29593037 }
3038+
3039+ exit :
3040+ spin_unlock_irqrestore (& host -> irq_lock , irqflags );
29603041}
29613042
29623043static void dw_mci_dto_timer (unsigned long arg )
0 commit comments