struct sd_card * mmchs_card_initialize(struct sd_slot *slot) { // mmchs_init(1); struct sd_card *card; card = &slot->card; memset(card, 0, sizeof(struct sd_card)); card->slot = slot; if (card_goto_idle_state()) { mmc_log_warn(&log, "Failed to go idle state\n"); return NULL; } if (card_identification()) { mmc_log_warn(&log, "Failed to do card_identification\n"); return NULL; } if (card_query_voltage_and_type(&slot->card.regs)) { mmc_log_warn(&log, "Failed to do card_query_voltage_and_type\n"); return NULL; } if (card_identify(&slot->card.regs)) { mmc_log_warn(&log, "Failed to identify card\n"); return NULL; } /* We have now initialized the hardware identified the card */ if (card_csd(&slot->card.regs)) { mmc_log_warn(&log, "failed to read csd (card specific data)\n"); return NULL; } if (select_card(&slot->card.regs)) { mmc_log_warn(&log, "Failed to select card\n"); return NULL; } if (SD_CSD_READ_BL_LEN(slot->card.regs.csd) != 0x09) { /* for CSD version 2.0 the value is fixed to 0x09 and means a * block size of 512 */ mmc_log_warn(&log, "Block size expect to be 512\n"); return NULL; } slot->card.blk_size = 512; /* HARDCODED value */ slot->card.blk_count = SD_CSD_V2_CAPACITY(slot->card.regs.csd); slot->card.state = SD_MODE_DATA_TRANSFER_MODE; memset(slot->card.part, 0, sizeof(slot->card.part)); memset(slot->card.subpart, 0, sizeof(slot->card.subpart)); slot->card.part[0].dv_base = 0; slot->card.part[0].dv_size = (unsigned long long) SD_CSD_V2_CAPACITY(slot->card.regs.csd) * 512; return &slot->card; }
int main(void) { struct sd_card card; int i; unsigned char buf[1024]; memset(buf, 0, 1024); if (mmchs_init(1)) { printf("Failed to initialize the host controller\n"); return 1; } if (card_goto_idle_state()) { printf("Failed to go into idle state\n"); return 1; } if (card_identification()) { printf("Failed to go card_identification\n"); return 1; } if (card_query_voltage_and_type(&card)) { printf("Failed to go card_query_voltage_and_type\n"); return 1; } if (card_identify(&card)) { printf("Failed to identify card\n"); return 1; } /* We have now initialized the hardware identified the card */ if (card_csd(&card)) { printf("failed to read csd (card specific data)\n"); return 1; } if (select_card(&card)) { printf("Failed to select card\n"); return 1; } if (read_single_block(&card, 0, buf)) { printf("Failed to read a single block\n"); return 1; } /* check signature */ if (!(buf[0x01fe] == 0x55 && buf[0x01ff] == 0xaa)) { printf("Failed to find MBR signature\n"); return 1; } for (i = 0; i < 512; i++) { buf[i] = i % 256; } if (read_single_block(&card, 0, buf)) { printf("Failed to read a single block\n"); return 1; } /* check signature */ if (!(buf[0x01fe] == 0x55 && buf[0x01ff] == 0xaa)) { printf("Failed to find MBR signature\n"); return 1; } /* DESCUCTIVE... */ for (i = 0; i < 512; i++) { buf[i] = i % 256; } if (write_single_block(&card, 0xfffff, buf)) { printf("Failed to write a single block\n"); return 1; } for (i = 0; i < 512; i++) { buf[i] = 0; } if (read_single_block(&card, 0xfffff, buf)) { printf("Failed to write a single block (check)\n"); return 1; } for (i = 0; i < 512; i++) { if (!buf[i] == i % 256) { printf("Failed to write a single block and read it again \n"); return 1; } } printf("Finished\n"); return 0; }