13
13
#include <linux/err.h>
14
14
#include <linux/slab.h>
15
15
#include <linux/stat.h>
16
+ #include <linux/jiffies.h>
17
+ #include <linux/nmi.h>
16
18
17
19
#include <linux/mmc/host.h>
18
20
#include <linux/mmc/card.h>
@@ -58,6 +60,15 @@ static const unsigned int tacc_mant[] = {
58
60
__res & __mask; \
59
61
})
60
62
63
+ // timeout for tries
64
+ static const unsigned long retry_timeout_ms = 10 * 1000 ;
65
+
66
+ // try at least 10 times, even if timeout is reached
67
+ static const int retry_min_tries = 10 ;
68
+
69
+ // delay between tries
70
+ static const unsigned long retry_delay_ms = 10 ;
71
+
61
72
/*
62
73
* Given the decoded CSD structure, decode the raw CID to our CID structure.
63
74
*/
@@ -210,12 +221,62 @@ static int mmc_decode_scr(struct mmc_card *card)
210
221
}
211
222
212
223
/*
213
- * Fetch and process SD Status register.
224
+ * Fetch and process SD Configuration Register.
225
+ */
226
+ static int mmc_read_scr (struct mmc_card * card )
227
+ {
228
+ unsigned long timeout_at ;
229
+ int err , tries ;
230
+
231
+ timeout_at = jiffies + msecs_to_jiffies ( retry_timeout_ms );
232
+ tries = 0 ;
233
+
234
+ while ( tries < retry_min_tries || time_before ( jiffies , timeout_at ) )
235
+ {
236
+ unsigned long delay_at ;
237
+ tries ++ ;
238
+
239
+ err = mmc_app_send_scr (card , card -> raw_scr );
240
+ if ( !err )
241
+ break ; // sucess!!!
242
+
243
+ touch_nmi_watchdog (); // we are still alive!
244
+
245
+ // delay
246
+ delay_at = jiffies + msecs_to_jiffies ( retry_delay_ms );
247
+ while ( time_before ( jiffies , delay_at ) )
248
+ {
249
+ mdelay ( 1 );
250
+ touch_nmi_watchdog (); // we are still alive!
251
+ }
252
+ }
253
+
254
+ if ( err )
255
+ {
256
+ pr_err ("%s: failed to read SD Configuration register (SCR) after %d tries during %lu ms, error %d\n" , mmc_hostname (card -> host ), tries , retry_timeout_ms , err );
257
+ return err ;
258
+ }
259
+
260
+ if ( tries > 1 )
261
+ {
262
+ pr_info ("%s: could read SD Configuration register (SCR) at the %dth attempt\n" , mmc_hostname (card -> host ), tries );
263
+ }
264
+
265
+ err = mmc_decode_scr (card );
266
+ if (err )
267
+ return err ;
268
+
269
+ return err ;
270
+ }
271
+
272
+ /*
273
+ * Fetch and process SD Status Register.
214
274
*/
215
275
static int mmc_read_ssr (struct mmc_card * card )
216
276
{
277
+ unsigned long timeout_at ;
217
278
unsigned int au , es , et , eo ;
218
- int err , i ;
279
+ int err , i , tries ;
219
280
u32 * ssr ;
220
281
221
282
if (!(card -> csd .cmdclass & CCC_APP_SPEC )) {
@@ -227,15 +288,41 @@ static int mmc_read_ssr(struct mmc_card *card)
227
288
ssr = kmalloc (64 , GFP_KERNEL );
228
289
if (!ssr )
229
290
return - ENOMEM ;
230
-
231
- err = mmc_app_sd_status (card , ssr );
232
- if (err ) {
233
- pr_warning ("%s: problem reading SD Status "
234
- "register.\n" , mmc_hostname (card -> host ));
235
- err = 0 ;
291
+
292
+ timeout_at = jiffies + msecs_to_jiffies ( retry_timeout_ms );
293
+ tries = 0 ;
294
+
295
+ while ( tries < retry_min_tries || time_before ( jiffies , timeout_at ) )
296
+ {
297
+ unsigned long delay_at ;
298
+ tries ++ ;
299
+
300
+ err = mmc_app_sd_status (card , ssr );
301
+ if ( !err )
302
+ break ; // sucess!!!
303
+
304
+ touch_nmi_watchdog (); // we are still alive!
305
+
306
+ // delay
307
+ delay_at = jiffies + msecs_to_jiffies ( retry_delay_ms );
308
+ while ( time_before ( jiffies , delay_at ) )
309
+ {
310
+ mdelay ( 1 );
311
+ touch_nmi_watchdog (); // we are still alive!
312
+ }
313
+ }
314
+
315
+ if ( err )
316
+ {
317
+ pr_err ("%s: failed to read SD Status register (SSR) after %d tries during %lu ms, error %d\n" , mmc_hostname (card -> host ), tries , retry_timeout_ms , err );
236
318
goto out ;
237
319
}
238
320
321
+ if ( tries > 1 )
322
+ {
323
+ pr_info ("%s: could read SD Status register (SSR) at the %dth attempt\n" , mmc_hostname (card -> host ), tries );
324
+ }
325
+
239
326
for (i = 0 ; i < 16 ; i ++ )
240
327
ssr [i ] = be32_to_cpu (ssr [i ]);
241
328
@@ -803,15 +890,11 @@ int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
803
890
804
891
if (!reinit ) {
805
892
/*
806
- * Fetch SCR from card .
893
+ * Fetch and decode SD Configuration register .
807
894
*/
808
- err = mmc_app_send_scr (card , card -> raw_scr );
809
- if (err )
810
- return err ;
811
-
812
- err = mmc_decode_scr (card );
813
- if (err )
814
- return err ;
895
+ err = mmc_read_scr (card );
896
+ if ( err )
897
+ return err ;
815
898
816
899
/*
817
900
* Fetch and process SD Status register.
0 commit comments