This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

sd read debugging

By: a guest on Jun 29th, 2010  |  syntax: C  |  size: 8.14 KB  |  views: 99  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Index: debug-as3525.c
  2. ===================================================================
  3. --- debug-as3525.c      (revision 27164)
  4. +++ debug-as3525.c      (working copy)
  5. @@ -268,6 +268,30 @@
  6.      {
  7.          while(1)
  8.          {
  9. +        int i;
  10. +        extern int sd_cmds_submitted[NUM_VOLUMES];
  11. +        extern int sd_cmds_errors[NUM_VOLUMES];
  12. +        extern int sd_transfers[NUM_VOLUMES];
  13. +        extern int sd_transfer_errors[NUM_VOLUMES];
  14. +        lcd_clear_display();
  15. +        line = 0;
  16. +        lcd_puts(0, line++, "[SD error stats]");
  17. +        for (i=0; i<NUM_VOLUMES; i++) {
  18. +            lcd_putsf(0, line++, "Drive %d errors:", i);
  19. +            lcd_putsf(0, line++, "cmd %d of %d",
  20. +                       sd_cmds_errors[i], sd_cmds_submitted[i]);
  21. +            lcd_putsf(0, line++, "r/w %d of %d",
  22. +                      sd_transfer_errors[i], sd_transfers[i]);
  23. +        }
  24. +        lcd_update();
  25. +        int btn = button_get_w_tmo(HZ/10);
  26. +        if(btn == (DEBUG_CANCEL|BUTTON_REL))
  27. +            goto end;
  28. +        else if(btn == (BUTTON_DOWN|BUTTON_REL))
  29. +            break;
  30. +        }
  31. +        while(1)
  32. +        {
  33.  #ifdef SANSA_C200V2
  34.          lcd_clear_display();
  35.          line = 0;
  36. Index: sd-as3525.c
  37. ===================================================================
  38. --- sd-as3525.c (revision 27165)
  39. +++ sd-as3525.c (working copy)
  40. @@ -126,6 +126,11 @@
  41.  static struct event_queue sd_queue;
  42.  bool sd_enabled = false;
  43.  
  44. +int sd_cmds_submitted[NUM_VOLUMES];
  45. +int sd_cmds_errors[NUM_VOLUMES];
  46. +int sd_transfers[NUM_VOLUMES];
  47. +int sd_transfer_errors[NUM_VOLUMES];
  48. +
  49.  #if defined(HAVE_MULTIDRIVE)
  50.  static bool hs_card = false;
  51.  #define EXT_SD_BITS (1<<2)
  52. @@ -174,7 +179,7 @@
  53.  {
  54.      static struct timeout sd1_oneshot;
  55.      if (GPIOA_MIS & EXT_SD_BITS)
  56. -        timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
  57. +        timeout_register(&sd1_oneshot, sd1_oneshot_callback, (9*HZ/10), 0);
  58.      /* acknowledge interrupt */
  59.      GPIOA_IC = EXT_SD_BITS;
  60.  }
  61. @@ -207,6 +212,8 @@
  62.  {
  63.      int status;
  64.  
  65. +    sd_cmds_submitted[drive]++;
  66. +
  67.      unsigned cmd_retries = 6;
  68.      while(cmd_retries--)
  69.      {
  70. @@ -236,8 +243,10 @@
  71.          {
  72.              response[0] = MCI_RESP0(drive); /* Always prepare short response */
  73.  
  74. -            if(status & MCI_RESPONSE_ERROR) /* timeout or crc failure */
  75. +            if(status & MCI_RESPONSE_ERROR) {/* timeout or crc failure */
  76. +                sd_cmds_errors[drive]++;
  77.                  continue;
  78. +            }
  79.  
  80.              if(status & MCI_CMD_RESP_END)   /* Response passed CRC check */
  81.              {
  82. @@ -257,6 +266,32 @@
  83.      return false;
  84.  }
  85.  
  86. +static void sd_set_widebus_mode(IF_MD2(int drive,) int on) {
  87. +#ifndef HAVE_MULTIDRIVE
  88. +    const int drive = 0;
  89. +#endif
  90. +    long response = 0;
  91. +    int ret;
  92. +    if((ret = sd_wait_for_tran_state(drive)) < 0) {
  93. +        int ret2 = send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
  94. +                            MCI_RESP, &response);
  95. +        panicf("sd_set_widebus(%d): tran: %d %d %lx %lx", on, ret, ret2, response, MCI_STATUS(drive));
  96. +    }
  97. +    /* CMD55 */              /*  Response is requested due to timing issue  */
  98. +    if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
  99. +        panicf("sd_set_widebus(%d): app_cmd", on);
  100. +    /* ACMD6  */
  101. +    if(!send_cmd(drive, SD_SET_BUS_WIDTH, on ? 2 : 0, MCI_RESP, &response))
  102. +        panicf("sd_set_widebus(%d): set_bus_width", on);
  103. +    /* Now that card is widebus make controller aware */
  104. +    if (on) {
  105. +        MCI_CLOCK(drive) |= MCI_CLOCK_WIDEBUS;
  106. +    } else {
  107. +        MCI_CLOCK(drive) &= ~MCI_CLOCK_WIDEBUS;
  108. +    }
  109. +}
  110. +
  111. +
  112.  #define MCI_FULLSPEED     (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS)     /* MCLK   */
  113.  #define MCI_HALFSPEED     (MCI_CLOCK_ENABLE)                        /* MCLK/2 */
  114.  #define MCI_QUARTERSPEED  (MCI_CLOCK_ENABLE | 1)                    /* MCLK/4 */
  115. @@ -362,24 +397,17 @@
  116.      if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_RESP, &response))
  117.          return -10;
  118.  
  119. -    /*  Switch to to 4 bit widebus mode  */
  120. -    if(sd_wait_for_tran_state(drive) < 0)
  121. -        return -11;
  122. +    /* Disable card detect pullup, we don't need it */
  123.      /* CMD55 */             /*  Response is requested due to timing issue  */
  124.      if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
  125.          return -14;
  126.      /* ACMD42  */
  127.      if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP, &response))
  128.          return -15;
  129. -    /* CMD55 */              /*  Response is requested due to timing issue  */
  130. -    if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
  131. -        return -12;
  132. -    /* ACMD6  */
  133. -    if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP, &response))
  134. -        return -13;
  135. -    /* Now that card is widebus make controller aware */
  136. -    MCI_CLOCK(drive) |= MCI_CLOCK_WIDEBUS;
  137.  
  138. +    /*  Switch to to 4 bit widebus mode  */
  139. +    sd_set_widebus_mode(IF_MD2(drive,) 1);
  140. +
  141.      /*
  142.       * enable bank switching
  143.       * without issuing this command, we only have access to 1/4 of the blocks
  144. @@ -596,15 +624,17 @@
  145.  
  146.      while (1)
  147.      {
  148. -        if(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP,
  149. -                    &response))
  150. -            return -1;
  151. +        int ret = send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
  152. +                           MCI_RESP, &response);
  153.  
  154. -        if (((response >> 9) & 0xf) == SD_TRAN)
  155. +        if (ret && ((response >> 9) & 0xf) == SD_TRAN)
  156.              return 0;
  157.  
  158. -        if(TIME_AFTER(current_tick, timeout))
  159. +        if(TIME_AFTER(current_tick, timeout)) {
  160. +            if (!ret)
  161. +                return -1;
  162.              return -2;
  163. +        }
  164.  
  165.          if (TIME_AFTER(current_tick, next_yield))
  166.          {
  167. @@ -690,6 +720,7 @@
  168.      bool aligned = !((uintptr_t)buf & (CACHEALIGN_SIZE - 1));
  169.  
  170.      mutex_lock(&sd_mtx);
  171. +    sd_transfers[drive]++;
  172.      sd_enable(true);
  173.      led(true);
  174.  
  175. @@ -724,7 +755,7 @@
  176.          if(write)
  177.              clean_dcache_range(buf, count * SECTOR_SIZE);
  178.          else
  179. -            dump_dcache_range(buf, count * SECTOR_SIZE);
  180. +            invalidate_dcache();
  181.      }
  182.  
  183.      while(count)
  184. @@ -853,9 +884,12 @@
  185.              count -= transfer;
  186.              loops = 0;  /* reset errors counter */
  187.          }
  188. -        else if(loops++ > PL180_MAX_TRANSFER_ERRORS)
  189. +        else {
  190. +            sd_transfer_errors[drive]++;
  191. +            if(loops++ > PL180_MAX_TRANSFER_ERRORS)
  192.                  panicf("SD Xfer %s err:0x%x Disk%d", (write? "write": "read"),
  193.                                                    transfer_error[drive], drive);
  194. +        }
  195.      }
  196.  
  197.  sd_transfer_error:
  198. @@ -874,10 +908,35 @@
  199.      return ret;
  200.  }
  201.  
  202. +#define VERIFY_READ 1
  203. +
  204.  int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
  205.                       void* buf)
  206.  {
  207. -    return sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
  208. +    int ret;
  209. +
  210. +    ret = sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
  211. +
  212. +#ifdef VERIFY_READ
  213. +    if (ret) /* read failed, no point in verifying? */
  214. +        return ret;
  215. +
  216. +    while (count) {
  217. +        int transfer = count;
  218. +        if(transfer > UNALIGNED_NUM_SECTORS)
  219. +            transfer = UNALIGNED_NUM_SECTORS;
  220. +
  221. +        sd_transfer_sectors(IF_MD2(drive,) start, transfer, aligned_buffer, false);
  222. +        if (memcmp(buf, aligned_buffer, transfer * 512) != 0)
  223. +            panicf("sd: read verify failed: sec=%ld n=%d!", start, transfer);
  224. +
  225. +        buf   += transfer * 512;
  226. +        count -= transfer;
  227. +        start += transfer;
  228. +    }
  229. +
  230. +#endif
  231. +    return ret;
  232.  }
  233.  
  234.  int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
  235. @@ -908,7 +967,7 @@
  236.          if (memcmp(buf, aligned_buffer, transfer * 512) != 0) {
  237.              /* try the write again in the hope to repair the damage */
  238.              sd_transfer_sectors(IF_MD2(drive,) saved_start, saved_count, saved_buf, true);
  239. -            panicf("sd: verify failed: sec=%ld n=%d!", start, transfer);
  240. +            panicf("sd: write verify failed: sec=%ld n=%d!", start, transfer);
  241.          }
  242.  
  243.          buf   += transfer * 512;
clone this paste RAW Paste Data