+static uint8_t
+_ao_sdcard_reset(void)
+{
+ int i;
+ uint8_t ret = 0x3f;
+ uint8_t response[10];
+
+ for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
+ if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
+ break;
+ }
+ if (i == SDCARD_IDLE_RETRY) {
+ ret = 0x3f;
+ goto bail;
+ }
+
+ /* Follow the setup path to get the card out of idle state and
+ * up and running again
+ */
+ if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
+ uint32_t arg = 0;
+// uint8_t sdver2 = 0;
+
+ /* Check for SD version 2 */
+ if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
+ arg = 0x40000000;
+// sdver2 = 1;
+ }
+
+ for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
+ ret = ao_sdcard_app_send_op_cond(arg);
+ if (ret != SDCARD_STATUS_IDLE_STATE)
+ break;
+ }
+
+ if (ret != SDCARD_STATUS_READY_STATE) {
+ /* MMC */
+ for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
+ ret = ao_sdcard_send_op_cond();
+ if (ret != SDCARD_STATUS_IDLE_STATE)
+ break;
+ }
+ if (ret != SDCARD_STATUS_READY_STATE)
+ goto bail;
+ }
+
+ /* For everything but SDHC cards, set the block length */
+ if (sdtype != ao_sdtype_sd2block) {
+ ret = ao_sdcard_set_blocklen(512);
+ if (ret != SDCARD_STATUS_READY_STATE)
+ DBG ("set_blocklen failed, ignoring\n");
+ }
+ }
+bail:
+ return ret;
+}
+
+/*
+ * The card will send 0xff until it is ready to send
+ * the data block at which point it will send the START_BLOCK
+ * marker followed by the data. This function waits while
+ * the card is sending 0xff
+ */