Lines Matching refs:mmc

24 static struct mmc mmc_drv_data;
55 static void set_speed(struct mmc *mmc, uint32_t clock) in set_speed() argument
81 esdhc_out32(&mmc->esdhc_regs->sysctl, in set_speed()
100 static int esdhc_init(struct mmc *mmc, bool card_detect) in esdhc_init() argument
106 val = esdhc_in32(&mmc->esdhc_regs->sysctl) | ESDHC_SYSCTL_RSTA; in esdhc_init()
107 esdhc_out32(&mmc->esdhc_regs->sysctl, val); in esdhc_init()
112 val = esdhc_in32(&mmc->esdhc_regs->sysctl) & ESDHC_SYSCTL_RSTA; in esdhc_init()
118 val = esdhc_in32(&mmc->esdhc_regs->sysctl) & in esdhc_init()
126 set_speed(mmc, CARD_IDENTIFICATION_FREQ); in esdhc_init()
130 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_init()
139 val = esdhc_in32(&mmc->esdhc_regs->sysctl) | ESDHC_SYSCTL_SDCLKEN; in esdhc_init()
140 esdhc_out32(&mmc->esdhc_regs->sysctl, val); in esdhc_init()
143 val = esdhc_in32(&mmc->esdhc_regs->sysctl) | ESDHC_SYSCTL_INITA; in esdhc_init()
144 esdhc_out32(&mmc->esdhc_regs->sysctl, val); in esdhc_init()
148 val = esdhc_in32(&mmc->esdhc_regs->sysctl) & ESDHC_SYSCTL_INITA; in esdhc_init()
154 val = esdhc_in32(&mmc->esdhc_regs->sysctl) & ESDHC_SYSCTL_INITA; in esdhc_init()
162 val = esdhc_in32(&mmc->esdhc_regs->proctl); in esdhc_init()
166 esdhc_out32(&mmc->esdhc_regs->proctl, val); in esdhc_init()
169 val = esdhc_in32(&mmc->esdhc_regs->ctl) | ESDHC_DCR_SNOOP; in esdhc_init()
170 esdhc_out32(&mmc->esdhc_regs->ctl, val); in esdhc_init()
183 static int esdhc_send_cmd(struct mmc *mmc, uint32_t cmd, uint32_t args) in esdhc_send_cmd() argument
189 esdhc_out32(&mmc->esdhc_regs->irqstat, ESDHC_IRQSTAT_CLEAR_ALL); in esdhc_send_cmd()
195 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_send_cmd()
202 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_send_cmd()
212 } else if (cmd == CMD7 || (cmd == CMD6 && mmc->card.type == MMC_CARD)) { in esdhc_send_cmd()
225 mmc->card.type == MMC_CARD) { in esdhc_send_cmd()
234 if (!(mmc->card.type == MMC_CARD && cmd == CMD6)) { in esdhc_send_cmd()
245 if (mmc->dma_support != 0) { in esdhc_send_cmd()
251 if ((cmd == CMD17 || cmd == CMD24) && (mmc->dma_support != 0)) { in esdhc_send_cmd()
257 esdhc_out32(&mmc->esdhc_regs->cmdarg, args); in esdhc_send_cmd()
258 esdhc_out32(&mmc->esdhc_regs->xfertyp, xfertyp); in esdhc_send_cmd()
279 static int esdhc_wait_response(struct mmc *mmc, uint32_t *response) in esdhc_wait_response() argument
288 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_CC; in esdhc_wait_response()
294 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_CC; in esdhc_wait_response()
300 status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_wait_response()
322 response[0] = esdhc_in32(&mmc->esdhc_regs->cmdrsp[0]); in esdhc_wait_response()
323 response[1] = esdhc_in32(&mmc->esdhc_regs->cmdrsp[1]); in esdhc_wait_response()
324 response[2] = esdhc_in32(&mmc->esdhc_regs->cmdrsp[2]); in esdhc_wait_response()
325 response[3] = esdhc_in32(&mmc->esdhc_regs->cmdrsp[3]); in esdhc_wait_response()
337 val = esdhc_in32(&mmc->esdhc_regs->irqstat) | ESDHC_IRQSTAT_CC; in esdhc_wait_response()
338 esdhc_out32(&mmc->esdhc_regs->irqstat, val); in esdhc_wait_response()
354 static int mmc_switch_to_high_frquency(struct mmc *mmc) in mmc_switch_to_high_frquency() argument
360 mmc->card.bus_freq = MMC_SS_20MHZ; in mmc_switch_to_high_frquency()
362 if (mmc->card.version < MMC_CARD_VERSION_4_X) { in mmc_switch_to_high_frquency()
367 error = esdhc_send_cmd(mmc, CMD_SWITCH_FUNC, SET_EXT_CSD_HS_TIMING); in mmc_switch_to_high_frquency()
371 error = esdhc_wait_response(mmc, response); in mmc_switch_to_high_frquency()
379 error = esdhc_send_cmd(mmc, in mmc_switch_to_high_frquency()
380 CMD_SEND_STATUS, mmc->card.rca << 16); in mmc_switch_to_high_frquency()
385 error = esdhc_wait_response(mmc, response); in mmc_switch_to_high_frquency()
394 mmc->card.bus_freq = MMC_HS_26MHZ; in mmc_switch_to_high_frquency()
396 mmc->card.bus_freq = MMC_HS_52MHZ; in mmc_switch_to_high_frquency()
410 static int esdhc_set_data_attributes(struct mmc *mmc, uint32_t *dest_ptr, in esdhc_set_data_attributes() argument
422 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & ESDHC_PRSSTAT_DLA; in esdhc_set_data_attributes()
428 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & ESDHC_PRSSTAT_DLA; in esdhc_set_data_attributes()
434 wml = esdhc_in32(&mmc->esdhc_regs->wml); in esdhc_set_data_attributes()
438 if ((mmc->dma_support != 0) && (dest_ptr != NULL)) { in esdhc_set_data_attributes()
440 esdhc_out32(&mmc->esdhc_regs->wml, in esdhc_set_data_attributes()
442 esdhc_out32(&mmc->esdhc_regs->wml, in esdhc_set_data_attributes()
446 esdhc_out32(&mmc->esdhc_regs->dsaddr, dst); in esdhc_set_data_attributes()
451 esdhc_out32(&mmc->esdhc_regs->wml, wml | ESDHC_WML_RD_WML(wl)); in esdhc_set_data_attributes()
455 esdhc_out32(&mmc->esdhc_regs->blkattr, in esdhc_set_data_attributes()
458 mmc->block_len = blklen; in esdhc_set_data_attributes()
472 static int esdhc_read_data_nodma(struct mmc *mmc, void *dest_ptr, uint32_t len) in esdhc_read_data_nodma() argument
481 num_blocks = len / mmc->block_len; in esdhc_read_data_nodma()
487 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_read_data_nodma()
494 val = esdhc_in32(&mmc->esdhc_regs->prsstat) in esdhc_read_data_nodma()
500 for (i = 0U, status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_read_data_nodma()
501 i < mmc->block_len / 4; i++, dst++) { in esdhc_read_data_nodma()
504 (uintptr_t)&mmc->esdhc_regs->datport); in esdhc_read_data_nodma()
507 status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_read_data_nodma()
522 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_TC; in esdhc_read_data_nodma()
528 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_TC; in esdhc_read_data_nodma()
546 static int esdhc_write_data_nodma(struct mmc *mmc, void *src_ptr, uint32_t len) in esdhc_write_data_nodma() argument
555 num_blocks = len / mmc->block_len; in esdhc_write_data_nodma()
560 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_write_data_nodma()
567 val = esdhc_in32(&mmc->esdhc_regs->prsstat) & in esdhc_write_data_nodma()
573 for (i = 0U, status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_write_data_nodma()
574 i < mmc->block_len / 4; i++, src++) { in esdhc_write_data_nodma()
577 mmio_write_32((uintptr_t)&mmc->esdhc_regs->datport, in esdhc_write_data_nodma()
580 status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_write_data_nodma()
594 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_TC; in esdhc_write_data_nodma()
600 val = esdhc_in32(&mmc->esdhc_regs->irqstat) & ESDHC_IRQSTAT_TC; in esdhc_write_data_nodma()
616 static int esdhc_read_data_dma(struct mmc *mmc, uint32_t len) in esdhc_read_data_dma() argument
622 tblk = SD_BLOCK_TIMEOUT * (len / mmc->block_len); in esdhc_read_data_dma()
628 status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_read_data_dma()
643 ((esdhc_in32(&mmc->esdhc_regs->prsstat) & ESDHC_PRSSTAT_DLA) != 0) && in esdhc_read_data_dma()
661 static int esdhc_write_data_dma(struct mmc *mmc, uint32_t len) in esdhc_write_data_dma() argument
667 tblk = SD_BLOCK_TIMEOUT * (len / mmc->block_len); in esdhc_write_data_dma()
673 status = esdhc_in32(&mmc->esdhc_regs->irqstat); in esdhc_write_data_dma()
687 ((esdhc_in32(&mmc->esdhc_regs->prsstat) & ESDHC_PRSSTAT_DLA) != 0) && in esdhc_write_data_dma()
706 int esdhc_read_data(struct mmc *mmc, void *dest_ptr, uint32_t len) in esdhc_read_data() argument
710 if (mmc->dma_support && len > 64) { in esdhc_read_data()
711 ret = esdhc_read_data_dma(mmc, len); in esdhc_read_data()
713 ret = esdhc_read_data_nodma(mmc, dest_ptr, len); in esdhc_read_data()
717 esdhc_out32(&mmc->esdhc_regs->irqstat, ESDHC_IRQSTAT_CLEAR_ALL); in esdhc_read_data()
730 int esdhc_write_data(struct mmc *mmc, void *src_ptr, uint32_t len) in esdhc_write_data() argument
734 if (mmc->dma_support && len > 64) { in esdhc_write_data()
735 ret = esdhc_write_data_dma(mmc, len); in esdhc_write_data()
737 ret = esdhc_write_data_nodma(mmc, src_ptr, len); in esdhc_write_data()
741 esdhc_out32(&mmc->esdhc_regs->irqstat, ESDHC_IRQSTAT_CLEAR_ALL); in esdhc_write_data()
759 static int sd_switch_to_high_freq(struct mmc *mmc) in sd_switch_to_high_freq() argument
770 mmc->card.bus_freq = SD_SS_25MHZ; in sd_switch_to_high_freq()
772 err = esdhc_send_cmd(mmc, CMD_APP_CMD, mmc->card.rca << 16); in sd_switch_to_high_freq()
777 err = esdhc_wait_response(mmc, response); in sd_switch_to_high_freq()
782 esdhc_set_data_attributes(mmc, NULL, 1, 8); in sd_switch_to_high_freq()
784 err = esdhc_send_cmd(mmc, CMD_SEND_SCR, mmc->card.rca << 16); in sd_switch_to_high_freq()
788 err = esdhc_wait_response(mmc, response); in sd_switch_to_high_freq()
794 err = esdhc_read_data(mmc, scr, 8U); in sd_switch_to_high_freq()
802 mmc->card.version = sd_versions[version]; in sd_switch_to_high_freq()
804 mmc->card.version = SD_CARD_VERSION_2_0; in sd_switch_to_high_freq()
808 if (mmc->card.version == SD_CARD_VERSION_1_0) { in sd_switch_to_high_freq()
813 esdhc_set_data_attributes(mmc, NULL, 1U, 64U); in sd_switch_to_high_freq()
817 err = esdhc_send_cmd(mmc, CMD_SWITCH_FUNC, in sd_switch_to_high_freq()
822 err = esdhc_wait_response(mmc, response); in sd_switch_to_high_freq()
827 err = esdhc_read_data(mmc, status, 64U); in sd_switch_to_high_freq()
842 esdhc_set_data_attributes(mmc, NULL, 1, 64); in sd_switch_to_high_freq()
843 err = esdhc_send_cmd(mmc, CMD_SWITCH_FUNC, SD_SWITCH_FUNC_SWITCH_MODE); in sd_switch_to_high_freq()
847 err = esdhc_wait_response(mmc, response); in sd_switch_to_high_freq()
852 err = esdhc_read_data(mmc, status, 64U); in sd_switch_to_high_freq()
858 mmc->card.bus_freq = SD_HS_50MHZ; in sd_switch_to_high_freq()
873 static int change_state_to_transfer_state(struct mmc *mmc) in change_state_to_transfer_state() argument
882 error = esdhc_send_cmd(mmc, CMD_SELECT_CARD, mmc->card.rca << 16); in change_state_to_transfer_state()
886 error = esdhc_wait_response(mmc, response); in change_state_to_transfer_state()
894 error = esdhc_send_cmd(mmc, in change_state_to_transfer_state()
895 CMD_SEND_STATUS, mmc->card.rca << 16); in change_state_to_transfer_state()
899 error = esdhc_wait_response(mmc, response); in change_state_to_transfer_state()
926 static int get_cid_rca_csd(struct mmc *mmc) in get_cid_rca_csd() argument
935 err = esdhc_send_cmd(mmc, CMD_ALL_SEND_CID, 0); in get_cid_rca_csd()
939 err = esdhc_wait_response(mmc, response); in get_cid_rca_csd()
945 mmc->card.rca = SD_MMC_CARD_RCA; in get_cid_rca_csd()
948 err = esdhc_send_cmd(mmc, CMD_SEND_RELATIVE_ADDR, mmc->card.rca << 16); in get_cid_rca_csd()
952 err = esdhc_wait_response(mmc, response); in get_cid_rca_csd()
958 if (mmc->card.type == SD_CARD) { in get_cid_rca_csd()
959 mmc->card.rca = (response[0] >> 16) & 0xFFFF; in get_cid_rca_csd()
963 err = esdhc_send_cmd(mmc, CMD_SEND_CSD, mmc->card.rca << 16); in get_cid_rca_csd()
967 err = esdhc_wait_response(mmc, response); in get_cid_rca_csd()
973 if (mmc->card.type == MMC_CARD) { in get_cid_rca_csd()
975 mmc->card.version = mmc_version[version]; in get_cid_rca_csd()
977 mmc->card.version = MMC_CARD_VERSION_4_X; in get_cid_rca_csd()
981 mmc->card.block_len = 1 << ((response[2] >> 8) & 0xF); in get_cid_rca_csd()
983 if (mmc->card.block_len > BLOCK_LEN_512) { in get_cid_rca_csd()
984 mmc->card.block_len = BLOCK_LEN_512; in get_cid_rca_csd()
999 static int identify_mmc_card(struct mmc *mmc) in identify_mmc_card() argument
1007 ret = esdhc_send_cmd(mmc, CMD_GO_IDLE_STATE, 0U); in identify_mmc_card()
1011 ret = esdhc_wait_response(mmc, resp); in identify_mmc_card()
1022 args = mmc->voltages_caps | MMC_OCR_SECTOR_MODE; in identify_mmc_card()
1023 ret = esdhc_send_cmd(mmc, CMD_MMC_SEND_OP_COND, args); in identify_mmc_card()
1027 ret = esdhc_wait_response(mmc, resp); in identify_mmc_card()
1039 mmc->card.is_high_capacity = 1; in identify_mmc_card()
1056 static int check_for_sd_card(struct mmc *mmc) in check_for_sd_card() argument
1064 ret = esdhc_send_cmd(mmc, CMD_GO_IDLE_STATE, 0U); in check_for_sd_card()
1068 ret = esdhc_wait_response(mmc, resp); in check_for_sd_card()
1075 ret = esdhc_send_cmd(mmc, CMD_SEND_IF_COND, args); in check_for_sd_card()
1079 ret = esdhc_wait_response(mmc, resp); in check_for_sd_card()
1081 mmc->card.is_high_capacity = 0; in check_for_sd_card()
1083 mmc->card.version = SD_CARD_VERSION_2_0; in check_for_sd_card()
1093 ret = esdhc_send_cmd(mmc, CMD_APP_CMD, 0U); in check_for_sd_card()
1097 ret = esdhc_wait_response(mmc, resp); in check_for_sd_card()
1103 args = mmc->voltages_caps; in check_for_sd_card()
1104 if (mmc->card.version == SD_CARD_VERSION_2_0) { in check_for_sd_card()
1109 ret = esdhc_send_cmd(mmc, CMD_SD_SEND_OP_COND, args); in check_for_sd_card()
1113 ret = esdhc_wait_response(mmc, resp); in check_for_sd_card()
1129 mmc->card.is_high_capacity = 1; in check_for_sd_card()
1142 int esdhc_emmc_init(struct mmc *mmc, bool card_detect) in esdhc_emmc_init() argument
1147 error = esdhc_init(mmc, card_detect); in esdhc_emmc_init()
1152 mmc->card.bus_freq = CARD_IDENTIFICATION_FREQ; in esdhc_emmc_init()
1153 mmc->card.rca = 0; in esdhc_emmc_init()
1154 mmc->card.is_high_capacity = 0; in esdhc_emmc_init()
1155 mmc->card.type = ERROR_ESDHC_UNUSABLE_CARD; in esdhc_emmc_init()
1159 mmc->voltages_caps = MMC_OCR_VDD_FF8; in esdhc_emmc_init()
1163 mmc->dma_support = esdhc_in32(&mmc->esdhc_regs->hostcapblt) & in esdhc_emmc_init()
1166 mmc->dma_support = 0; in esdhc_emmc_init()
1171 ret = check_for_sd_card(mmc); in esdhc_emmc_init()
1174 mmc->card.type = SD_CARD; in esdhc_emmc_init()
1179 if (identify_mmc_card(mmc) == MMC_CARD) { in esdhc_emmc_init()
1180 mmc->card.type = MMC_CARD; in esdhc_emmc_init()
1191 error = get_cid_rca_csd(mmc); in esdhc_emmc_init()
1197 error = change_state_to_transfer_state(mmc); in esdhc_emmc_init()
1203 if (mmc->card.type == SD_CARD) { in esdhc_emmc_init()
1204 error = sd_switch_to_high_freq(mmc); in esdhc_emmc_init()
1206 error = mmc_switch_to_high_frquency(mmc); in esdhc_emmc_init()
1214 set_speed(mmc, mmc->card.bus_freq); in esdhc_emmc_init()
1229 struct mmc *mmc = NULL; in sd_mmc_init() local
1232 mmc = &mmc_drv_data; in sd_mmc_init()
1233 memset(mmc, 0, sizeof(struct mmc)); in sd_mmc_init()
1234 mmc->esdhc_regs = (struct esdhc_regs *)nxp_esdhc_addr; in sd_mmc_init()
1237 ret = esdhc_emmc_init(mmc, card_detect); in sd_mmc_init()
1251 static int esdhc_read_block(struct mmc *mmc, void *dst, uint32_t block) in esdhc_read_block() argument
1257 err = esdhc_send_cmd(mmc, CMD_SET_BLOCKLEN, mmc->card.block_len); in esdhc_read_block()
1261 err = esdhc_wait_response(mmc, NULL); in esdhc_read_block()
1266 if (mmc->card.is_high_capacity != 0) { in esdhc_read_block()
1269 offset = block * mmc->card.block_len; in esdhc_read_block()
1272 esdhc_set_data_attributes(mmc, dst, 1, mmc->card.block_len); in esdhc_read_block()
1273 err = esdhc_send_cmd(mmc, CMD_READ_SINGLE_BLOCK, offset); in esdhc_read_block()
1277 err = esdhc_wait_response(mmc, NULL); in esdhc_read_block()
1282 err = esdhc_read_data(mmc, dst, mmc->card.block_len); in esdhc_read_block()
1297 static int esdhc_write_block(struct mmc *mmc, void *src, uint32_t block) in esdhc_write_block() argument
1303 err = esdhc_send_cmd(mmc, CMD_SET_BLOCKLEN, mmc->card.block_len); in esdhc_write_block()
1307 err = esdhc_wait_response(mmc, NULL); in esdhc_write_block()
1312 if (mmc->card.is_high_capacity != 0) { in esdhc_write_block()
1315 offset = block * mmc->card.block_len; in esdhc_write_block()
1318 esdhc_set_data_attributes(mmc, src, 1, mmc->card.block_len); in esdhc_write_block()
1319 err = esdhc_send_cmd(mmc, CMD_WRITE_SINGLE_BLOCK, offset); in esdhc_write_block()
1323 err = esdhc_wait_response(mmc, NULL); in esdhc_write_block()
1328 err = esdhc_write_data(mmc, src, mmc->card.block_len); in esdhc_write_block()
1343 int esdhc_read(struct mmc *mmc, uint32_t src_offset, uintptr_t dst, size_t size) in esdhc_read() argument
1359 if ((size % mmc->card.block_len) != 0) { in esdhc_read()
1364 if ((src_offset % mmc->card.block_len) != 0) { in esdhc_read()
1370 blk = src_offset / mmc->card.block_len; in esdhc_read()
1376 num_blocks = size / mmc->card.block_len; in esdhc_read()
1379 error = esdhc_read_block(mmc, buff, blk); in esdhc_read()
1385 buff = buff + mmc->card.block_len; in esdhc_read()
1404 int esdhc_write(struct mmc *mmc, uintptr_t src, uint32_t dst_offset, in esdhc_write() argument
1421 if ((size % mmc->card.block_len) != 0) { in esdhc_write()
1426 if ((dst_offset % mmc->card.block_len) != 0) { in esdhc_write()
1432 blk = dst_offset / mmc->card.block_len; in esdhc_write()
1438 num_blocks = size / mmc->card.block_len; in esdhc_write()
1441 error = esdhc_write_block(mmc, buff, blk); in esdhc_write()
1447 buff = buff + mmc->card.block_len; in esdhc_write()
1458 struct mmc *mmc = NULL; in ls_sd_emmc_read() local
1461 mmc = &mmc_drv_data; in ls_sd_emmc_read()
1463 ret = esdhc_read(mmc, lba, buf, size); in ls_sd_emmc_read()