From f82954e98b1156f1dda20f45a2a9e5411fc59483 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 8 Jun 2020 16:26:55 -0700 Subject: [PATCH] git subrepo clone --force --branch=exo2 https://github.com/m4xw/emummc subrepo: subdir: "emummc" merged: "3791be9f" upstream: origin: "https://github.com/m4xw/emummc" branch: "exo2" commit: "3791be9f" git-subrepo: version: "0.4.1" origin: "???" commit: "???" --- emummc/.gitrepo | 6 +- emummc/Makefile | 5 +- emummc/source/emmc/mmc.h | 4 +- emummc/source/emmc/nx_sd.c | 91 +++ emummc/source/emmc/nx_sd.h | 36 ++ emummc/source/emmc/sd.h | 11 +- emummc/source/emmc/sdmmc.c | 516 ++++++++--------- emummc/source/emmc/sdmmc.h | 8 +- emummc/source/emmc/sdmmc_driver.c | 793 +++++++++++++++------------ emummc/source/emmc/sdmmc_driver.h | 195 ++++++- emummc/source/emmc/sdmmc_t210.h | 110 ++-- emummc/source/emuMMC/emummc.c | 92 ++-- emummc/source/emuMMC/emummc.h | 7 +- emummc/source/emuMMC/emummc_ctx.h | 2 +- emummc/source/libs/fatfs/ff.c | 272 ++++++++- emummc/source/libs/fatfs/ff.h | 14 +- emummc/source/libs/fatfs/ffconf.h | 11 +- emummc/source/libs/fatfs/ffunicode.c | 2 - emummc/source/power/max77620.h | 57 +- emummc/source/power/max7762x.c | 6 - emummc/source/power/max7762x.h | 9 +- emummc/source/soc/clock.c | 199 +++++-- emummc/source/soc/clock.h | 35 +- emummc/source/soc/i2c.c | 25 +- emummc/source/soc/pmc.h | 4 + emummc/source/soc/t210.h | 2 + emummc/source/utils/fatal.h | 4 + emummc/source/utils/util.c | 4 +- emummc/source/utils/util.h | 4 +- 29 files changed, 1653 insertions(+), 871 deletions(-) create mode 100644 emummc/source/emmc/nx_sd.c create mode 100644 emummc/source/emmc/nx_sd.h diff --git a/emummc/.gitrepo b/emummc/.gitrepo index df68f68fb..df0193066 100644 --- a/emummc/.gitrepo +++ b/emummc/.gitrepo @@ -5,8 +5,8 @@ ; [subrepo] remote = https://github.com/m4xw/emuMMC - branch = develop - commit = 292a8ad42c8e9f4c9a474b46a5a3190398581131 - parent = 491ba8fdcfd39a503bedd21b282991fc19aec7d4 + branch = exo2 + commit = 3791be9fd207811cce221a4311da63fda1d32d4a + parent = ff7bed5db7d441eecc9444a9068ae59e7f9b7744 method = rebase cmdver = 0.4.1 diff --git a/emummc/Makefile b/emummc/Makefile index 81e02c1cb..f902447aa 100644 --- a/emummc/Makefile +++ b/emummc/Makefile @@ -21,9 +21,10 @@ include $(DEVKITPRO)/libnx/switch_rules ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE -DEFINES := -DINNER_HEAP_SIZE=0x80000 +# Current max usage is 0x4600. (512 * 34 FatFS file objects + 1 fsync buffer). +DEFINES := -DINNER_HEAP_SIZE=0x8000 -CFLAGS := -Wall -O2 -ffunction-sections -Wno-unused-function \ +CFLAGS := -Wall -O2 -ffunction-sections -fdata-sections -Wno-unused-function \ $(ARCH) $(DEFINES) CFLAGS += $(INCLUDE) -D__SWITCH__ diff --git a/emummc/source/emmc/mmc.h b/emummc/source/emmc/mmc.h index dddb956e9..0d87e3556 100644 --- a/emummc/source/emmc/mmc.h +++ b/emummc/source/emmc/mmc.h @@ -31,7 +31,7 @@ #define MMC_ALL_SEND_CID 2 /* bcr R2 */ #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */ #define MMC_SET_DSR 4 /* bc [31:16] RCA */ -#define MMC_SLEEP_AWAKE 5 /* ac [31:16] RCA 15:flg R1b */ +#define MMC_SLEEP_AWAKE 5 /* ac [31:16] RCA 15:flg R1b */ #define MMC_SWITCH 6 /* ac [31:0] See below R1b */ #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */ #define MMC_SEND_EXT_CSD 8 /* adtc R1 */ @@ -51,7 +51,7 @@ #define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */ #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */ #define MMC_SEND_TUNING_BLOCK 19 /* adtc R1 */ -#define MMC_SEND_TUNING_BLOCK_HS200 21 /* adtc R1 */ +#define MMC_SEND_TUNING_BLOCK_HS200 21 /* adtc R1 */ /* class 3 */ #define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */ diff --git a/emummc/source/emmc/nx_sd.c b/emummc/source/emmc/nx_sd.c new file mode 100644 index 000000000..1f5fd50d2 --- /dev/null +++ b/emummc/source/emmc/nx_sd.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "nx_sd.h" +#include "sdmmc.h" +#include "sdmmc_driver.h" +#include "../soc/gpio.h" +#include "../libs/fatfs/ff.h" + +extern sdmmc_t sd_sdmmc; +extern sdmmc_storage_t sd_storage; + +static u32 sd_mode = SD_UHS_SDR104; + +u32 nx_sd_mode_get() +{ + return sd_mode; +} + +int nx_sd_init_retry(bool power_cycle) +{ + u32 bus_width = SDMMC_BUS_WIDTH_4; + u32 type = SDHCI_TIMING_UHS_SDR104; + + // Power cycle SD card. + if (power_cycle) + { + sd_mode--; + sdmmc_storage_end(&sd_storage); + } + + // Get init parameters. + switch (sd_mode) + { + case SD_INIT_FAIL: // Reset to max. + return 0; + case SD_1BIT_HS25: + bus_width = SDMMC_BUS_WIDTH_1; + type = SDHCI_TIMING_SD_HS25; + break; + case SD_4BIT_HS25: + type = SDHCI_TIMING_SD_HS25; + break; + case SD_UHS_SDR82: + type = SDHCI_TIMING_UHS_SDR82; + break; + case SD_UHS_SDR104: + type = SDHCI_TIMING_UHS_SDR104; + break; + default: + sd_mode = SD_UHS_SDR104; + } + + return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type); +} + +bool nx_sd_initialize(bool power_cycle) +{ + if (power_cycle) + sdmmc_storage_end(&sd_storage); + + int res = !nx_sd_init_retry(false); + + while (true) + { + if (!res) + return true; + else + { + if (sd_mode == SD_INIT_FAIL) + break; + + res = !nx_sd_init_retry(true); + } + } + + return false; +} \ No newline at end of file diff --git a/emummc/source/emmc/nx_sd.h b/emummc/source/emmc/nx_sd.h new file mode 100644 index 000000000..38c74edbb --- /dev/null +++ b/emummc/source/emmc/nx_sd.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NX_SD_H +#define NX_SD_H + +#include "../utils/types.h" + +enum +{ + SD_INIT_FAIL = 0, + SD_1BIT_HS25 = 1, + SD_4BIT_HS25 = 2, + SD_UHS_SDR82 = 3, + SD_UHS_SDR104 = 4 +}; + +u32 nx_sd_get_mode(); +int nx_sd_init_retry(bool power_cycle); +bool nx_sd_initialize(bool power_cycle); + +#endif \ No newline at end of file diff --git a/emummc/source/emmc/sd.h b/emummc/source/emmc/sd.h index c3bf82ba0..3ecfc073a 100644 --- a/emummc/source/emmc/sd.h +++ b/emummc/source/emmc/sd.h @@ -1,8 +1,8 @@ /* * include/linux/mmc/sd.h * - * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. - * Copyright (C) 2018 CTCaer + * Copyright (c) 2005-2007 Pierre Ossman, All Rights Reserved. + * Copyright (c) 2018 CTCaer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,9 @@ #define SD_ROCR_S18A SD_OCR_S18R /* 1.8V switching accepted by card */ #define SD_OCR_XPC (1 << 28) /* SDXC power control */ #define SD_OCR_CCS (1 << 30) /* Card Capacity Status */ +#define SD_OCR_VDD_27_34 (0x7F << 15) /* VDD voltage 2.7 ~ 3.4 */ #define SD_OCR_VDD_32_33 (1 << 20) /* VDD voltage 3.2 ~ 3.3 */ +#define SD_OCR_VDD_18 (1 << 7) /* VDD voltage 1.8 */ /* * SD_SWITCH argument format: @@ -104,6 +106,11 @@ #define SD_SET_CURRENT_LIMIT_600 2 #define SD_SET_CURRENT_LIMIT_800 3 +#define SD_MAX_CURRENT_200 (1 << SD_SET_CURRENT_LIMIT_200) +#define SD_MAX_CURRENT_400 (1 << SD_SET_CURRENT_LIMIT_400) +#define SD_MAX_CURRENT_600 (1 << SD_SET_CURRENT_LIMIT_600) +#define SD_MAX_CURRENT_800 (1 << SD_SET_CURRENT_LIMIT_800) + /* * SD_SWITCH mode */ diff --git a/emummc/source/emmc/sdmmc.c b/emummc/source/emmc/sdmmc.c index 69452dea3..db17aac0d 100644 --- a/emummc/source/emmc/sdmmc.c +++ b/emummc/source/emmc/sdmmc.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2018 naehrwert - * Copyright (C) 2018 CTCaer + * Copyright (c) 2018-2019 CTCaer * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -20,6 +20,7 @@ #include #include "sdmmc.h" #include "mmc.h" +#include "nx_sd.h" #include "sd.h" #include "../utils/types.h" #include "../utils/util.h" @@ -188,6 +189,7 @@ static int _sdmmc_storage_execute_cmd_type1_ex(sdmmc_storage_t *storage, u32 *re if (_sdmmc_storage_check_result(*resp)) if (expected_state == 0x10 || R1_CURRENT_STATE(*resp) == expected_state) return 1; + return 0; } @@ -201,6 +203,7 @@ static int _sdmmc_storage_go_idle_state(sdmmc_storage_t *storage) { sdmmc_cmd_t cmd; sdmmc_init_cmd(&cmd, MMC_GO_IDLE_STATE, 0, SDMMC_RSP_TYPE_0, 0); + return sdmmc_execute_cmd(storage->sdmmc, &cmd, 0, 0); } @@ -210,7 +213,9 @@ static int _sdmmc_storage_get_cid(sdmmc_storage_t *storage, void *buf) sdmmc_init_cmd(&cmd, MMC_ALL_SEND_CID, 0, SDMMC_RSP_TYPE_2, 0); if (!sdmmc_execute_cmd(storage->sdmmc, &cmd, 0, 0)) return 0; + sdmmc_get_rsp(storage->sdmmc, buf, 0x10, SDMMC_RSP_TYPE_2); + return 1; } @@ -225,7 +230,9 @@ static int _sdmmc_storage_get_csd(sdmmc_storage_t *storage, void *buf) sdmmc_init_cmd(&cmdbuf, MMC_SEND_CSD, storage->rca << 16, SDMMC_RSP_TYPE_2, 0); if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, 0, 0)) return 0; + sdmmc_get_rsp(storage->sdmmc, buf, 0x10, SDMMC_RSP_TYPE_2); + return 1; } @@ -247,9 +254,9 @@ static int _sdmmc_storage_check_status(sdmmc_storage_t *storage) static int _sdmmc_storage_readwrite_ex(sdmmc_storage_t *storage, u32 *blkcnt_out, u32 sector, u32 num_sectors, void *buf, u32 is_write) { + u32 tmp = 0; sdmmc_cmd_t cmdbuf; sdmmc_req_t reqbuf; - u32 tmp = 0; sdmmc_init_cmd(&cmdbuf, is_write ? MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK, sector, SDMMC_RSP_TYPE_1, 0); @@ -261,12 +268,13 @@ static int _sdmmc_storage_readwrite_ex(sdmmc_storage_t *storage, u32 *blkcnt_out reqbuf.is_auto_cmd12 = 1; if (!sdmmc_execute_cmd(storage->sdmmc, &cmdbuf, &reqbuf, blkcnt_out)) - { + { sdmmc_stop_transmission(storage->sdmmc, &tmp); _sdmmc_storage_get_status(storage, &tmp, 0); + return 0; } - + return 1; } @@ -274,36 +282,58 @@ int sdmmc_storage_end(sdmmc_storage_t *storage) { if (!_sdmmc_storage_go_idle_state(storage)) return 0; + sdmmc_end(storage->sdmmc); + return 1; } static int _sdmmc_storage_readwrite(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf, u32 is_write) { u8 *bbuf = (u8 *)buf; - + bool first_reinit = false; while (num_sectors) { u32 blkcnt = 0; - //Retry 9 times on error. - u32 retries = 10; + // Retry 5 times if failed. + u32 retries = 5; do { +reinit_try: if (_sdmmc_storage_readwrite_ex(storage, &blkcnt, sector, MIN(num_sectors, 0xFFFF), bbuf, is_write)) goto out; else retries--; - msleep(100); + msleep(50); } while (retries); + + // Disk IO failure! Reinit SD Card to a lower speed. + if (storage->sdmmc->id == SDMMC_1) + { + int res; + + if (!first_reinit) + res = nx_sd_initialize(true); + else + res = nx_sd_init_retry(true); + + retries = 3; + first_reinit = true; + + if (res) + goto reinit_try; + } + return 0; -out:; - DPRINTF("readwrite: %08X\n", blkcnt); +out: +DPRINTF("readwrite: %08X\n", blkcnt); sector += blkcnt; num_sectors -= blkcnt; bbuf += 512 * blkcnt; } + return 1; } @@ -427,10 +457,10 @@ static int _mmc_storage_get_op_cond_inner(sdmmc_storage_t *storage, u32 *pout, u switch (power) { case SDMMC_POWER_1_8: - arg = 0x40000080; //Sector access, voltage. + arg = SD_OCR_CCS | SD_OCR_VDD_18; break; case SDMMC_POWER_3_3: - arg = 0x403F8000; //Sector access, voltage. + arg = SD_OCR_CCS | SD_OCR_VDD_27_34; break; default: return 0; @@ -445,21 +475,24 @@ static int _mmc_storage_get_op_cond_inner(sdmmc_storage_t *storage, u32 *pout, u static int _mmc_storage_get_op_cond(sdmmc_storage_t *storage, u32 power) { - u32 timeout = get_tmr_ms() + 1500; + u64 timeout = get_tmr_ms() + 1500; while (1) { u32 cond = 0; if (!_mmc_storage_get_op_cond_inner(storage, &cond, power)) break; + if (cond & MMC_CARD_BUSY) { - if (cond & 0x40000000) + if (cond & SD_OCR_CCS) storage->has_sector_access = 1; + return 1; } if (get_tmr_ms() > timeout) break; + usleep(1000); } @@ -589,6 +622,7 @@ static int _mmc_storage_switch_buswidth(sdmmc_storage_t *storage, u32 bus_width) if (_sdmmc_storage_check_status(storage)) { sdmmc_set_bus_width(storage->sdmmc, bus_width); + return 1; } @@ -599,14 +633,19 @@ static int _mmc_storage_enable_HS(sdmmc_storage_t *storage, int check) { if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS))) return 0; + if (check && !_sdmmc_storage_check_status(storage)) return 0; - if (!sdmmc_setup_clock(storage->sdmmc, 2)) + + if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS52)) return 0; - DPRINTF("[MMC] switched to HS\n"); + +DPRINTF("[MMC] switched to HS\n"); storage->csd.busspeed = 52; + if (check || _sdmmc_storage_check_status(storage)) return 1; + return 0; } @@ -614,12 +653,16 @@ static int _mmc_storage_enable_HS200(sdmmc_storage_t *storage) { if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200))) return 0; - if (!sdmmc_setup_clock(storage->sdmmc, 3)) + + if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS200)) return 0; - if (!sdmmc_config_tuning(storage->sdmmc, 3, MMC_SEND_TUNING_BLOCK_HS200)) + + if (!sdmmc_tuning_execute(storage->sdmmc, SDHCI_TIMING_MMC_HS200, MMC_SEND_TUNING_BLOCK_HS200)) return 0; - DPRINTF("[MMC] switched to HS200\n"); + +DPRINTF("[MMC] switched to HS200\n"); storage->csd.busspeed = 200; + return _sdmmc_storage_check_status(storage); } @@ -627,41 +670,46 @@ static int _mmc_storage_enable_HS400(sdmmc_storage_t *storage) { if (!_mmc_storage_enable_HS200(storage)) return 0; - sdmmc_get_venclkctl(storage->sdmmc); + + sdmmc_set_tap_value(storage->sdmmc); + if (!_mmc_storage_enable_HS(storage, 0)) return 0; + if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_BUS_WIDTH, EXT_CSD_DDR_BUS_WIDTH_8))) return 0; + if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400))) return 0; - if (!sdmmc_setup_clock(storage->sdmmc, 4)) + + if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_HS400)) return 0; - DPRINTF("[MMC] switched to HS400\n"); + +DPRINTF("[MMC] switched to HS400\n"); storage->csd.busspeed = 400; + return _sdmmc_storage_check_status(storage); } static int _mmc_storage_enable_highspeed(sdmmc_storage_t *storage, u32 card_type, u32 type) { - //TODO: this should be a config item. - // --v - if (!1 || sdmmc_get_voltage(storage->sdmmc) != SDMMC_POWER_1_8) + if (sdmmc_get_io_power(storage->sdmmc) != SDMMC_POWER_1_8) goto out; if (sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_8 && - card_type & EXT_CSD_CARD_TYPE_HS400_1_8V && - type == 4) + card_type & EXT_CSD_CARD_TYPE_HS400_1_8V && type == SDHCI_TIMING_MMC_HS400) return _mmc_storage_enable_HS400(storage); if (sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_8 || (sdmmc_get_bus_width(storage->sdmmc) == SDMMC_BUS_WIDTH_4 && card_type & EXT_CSD_CARD_TYPE_HS200_1_8V - && (type == 4 || type == 3))) + && (type == SDHCI_TIMING_MMC_HS400 || type == SDHCI_TIMING_MMC_HS200))) return _mmc_storage_enable_HS200(storage); -out:; +out: if (card_type & EXT_CSD_CARD_TYPE_HS_52) return _mmc_storage_enable_HS(storage, 1); + return 1; } @@ -669,53 +717,54 @@ static int _mmc_storage_enable_bkops(sdmmc_storage_t *storage) { if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_SET_BITS, EXT_CSD_BKOPS_EN, EXT_CSD_BKOPS_LEVEL_2))) return 0; + return _sdmmc_storage_check_status(storage); } -int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 bus_width, u32 type) +int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type) { memset(storage, 0, sizeof(sdmmc_storage_t)); storage->sdmmc = sdmmc; storage->rca = 2; //TODO: this could be a config item. - if (!sdmmc_init(sdmmc, id, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_1, 0, 0)) + if (!sdmmc_init(sdmmc, SDMMC_4, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_1, SDHCI_TIMING_MMC_ID, SDMMC_AUTO_CAL_DISABLE)) return 0; - DPRINTF("[MMC] after init\n"); +DPRINTF("[MMC] after init\n"); usleep(1000 + (74000 + sdmmc->divisor - 1) / sdmmc->divisor); if (!_sdmmc_storage_go_idle_state(storage)) return 0; - DPRINTF("[MMC] went to idle state\n"); +DPRINTF("[MMC] went to idle state\n"); if (!_mmc_storage_get_op_cond(storage, SDMMC_POWER_1_8)) return 0; - DPRINTF("[MMC] got op cond\n"); +DPRINTF("[MMC] got op cond\n"); if (!_sdmmc_storage_get_cid(storage, storage->raw_cid)) return 0; - DPRINTF("[MMC] got cid\n"); +DPRINTF("[MMC] got cid\n"); if (!_mmc_storage_set_relative_addr(storage)) return 0; - DPRINTF("[MMC] set relative addr\n"); +DPRINTF("[MMC] set relative addr\n"); if (!_sdmmc_storage_get_csd(storage, storage->raw_csd)) return 0; - DPRINTF("[MMC] got csd\n"); +DPRINTF("[MMC] got csd\n"); _mmc_storage_parse_csd(storage); - if (!sdmmc_setup_clock(storage->sdmmc, 1)) + if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_MMC_LS26)) return 0; - DPRINTF("[MMC] after setup clock\n"); +DPRINTF("[MMC] after setup clock\n"); if (!_sdmmc_storage_select_card(storage)) return 0; - DPRINTF("[MMC] card selected\n"); +DPRINTF("[MMC] card selected\n"); if (!_sdmmc_storage_set_blocklen(storage, 512)) return 0; - DPRINTF("[MMC] set blocklen to 512\n"); +DPRINTF("[MMC] set blocklen to 512\n"); u32 *csd = (u32 *)storage->raw_csd; //Check system specification version, only version 4.0 and later support below features. @@ -727,36 +776,29 @@ int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 if (!_mmc_storage_switch_buswidth(storage, bus_width)) return 0; - DPRINTF("[MMC] switched buswidth\n"); - - u8 *ext_csd = (u8 *)malloc(512); - if (!_mmc_storage_get_ext_csd(storage, ext_csd)) - { - free(ext_csd); +DPRINTF("[MMC] switched buswidth\n"); + u8 buf[512]; + memset(buf, 0, sizeof(buf)); + if (!_mmc_storage_get_ext_csd(storage, buf)) return 0; - } - free(ext_csd); - DPRINTF("[MMC] got ext_csd\n"); +DPRINTF("[MMC] got ext_csd\n"); + _mmc_storage_parse_cid(storage); //This needs to be after csd and ext_csd /* When auto BKOPS is enabled the mmc device should be powered all the time until we disable this and check status. Disable it for now until BKOPS disable added to power down sequence at sdmmc_storage_end(). Additionally this works only when we put the device in idle mode which we don't after enabling it. */ - if (storage->ext_csd.bkops & 0x1 && !(storage->ext_csd.bkops_en & EXT_CSD_BKOPS_LEVEL_2) && 0) + if (0 && storage->ext_csd.bkops & 0x1 && !(storage->ext_csd.bkops_en & EXT_CSD_BKOPS_LEVEL_2)) { _mmc_storage_enable_bkops(storage); - DPRINTF("[MMC] BKOPS enabled\n"); - } - else - { - DPRINTF("[MMC] BKOPS disabled\n"); +DPRINTF("[MMC] BKOPS enabled\n"); } if (!_mmc_storage_enable_highspeed(storage, storage->ext_csd.card_type, type)) return 0; - DPRINTF("[MMC] succesfully switched to highspeed mode\n"); +DPRINTF("[MMC] succesfully switched to HS mode\n"); - sdmmc_sd_clock_ctrl(storage->sdmmc, 1); + sdmmc_card_clock_ctrl(storage->sdmmc, SDMMC_AUTO_CAL_ENABLE); return 1; } @@ -765,8 +807,10 @@ int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition) { if (!_mmc_storage_switch(storage, SDMMC_SWITCH(MMC_SWITCH_MODE_WRITE_BYTE, EXT_CSD_PART_CONFIG, partition))) return 0; + if (!_sdmmc_storage_check_status(storage)) return 0; + storage->partition = partition; return 1; } @@ -780,6 +824,7 @@ static int _sd_storage_execute_app_cmd(sdmmc_storage_t *storage, u32 expected_st u32 tmp; if (!_sdmmc_storage_execute_cmd_type1_ex(storage, &tmp, MMC_APP_CMD, storage->rca << 16, 0, expected_state, mask)) return 0; + return sdmmc_execute_cmd(storage->sdmmc, cmd, req, blkcnt_out); } @@ -787,6 +832,7 @@ static int _sd_storage_execute_app_cmd_type1(sdmmc_storage_t *storage, u32 *resp { if (!_sdmmc_storage_execute_cmd_type1(storage, MMC_APP_CMD, storage->rca << 16, 0, R1_STATE_TRAN)) return 0; + return _sdmmc_storage_execute_cmd_type1_ex(storage, resp, cmd, arg, check_busy, expected_state, 0); } @@ -816,32 +862,27 @@ static int _sd_storage_get_op_cond_once(sdmmc_storage_t *storage, u32 *cond, int // This is needed for most cards. Do not set bit7 even if 1.8V is supported. arg |= SD_OCR_VDD_32_33; sdmmc_init_cmd(&cmdbuf, SD_APP_OP_COND, arg, SDMMC_RSP_TYPE_3, 0); - - DPRINTF("[SD] before _sd_storage_execute_app_cmd\n"); if (!_sd_storage_execute_app_cmd(storage, 0x10, is_version_1 ? 0x400000 : 0, &cmdbuf, 0, 0)) return 0; - DPRINTF("[SD] before sdmmc_get_rsp\n"); + return sdmmc_get_rsp(storage->sdmmc, cond, 4, SDMMC_RSP_TYPE_3); } static int _sd_storage_get_op_cond(sdmmc_storage_t *storage, int is_version_1, int supports_low_voltage) { - u32 timeout = get_tmr_ms() + 1500; + u64 timeout = get_tmr_ms() + 1500; while (1) { u32 cond = 0; if (!_sd_storage_get_op_cond_once(storage, &cond, is_version_1, supports_low_voltage)) - { - DPRINTF("[SD] _sd_storage_get_op_cond_once failed\r\n"); break; - } - if (cond & MMC_CARD_BUSY) { if (cond & SD_OCR_CCS) storage->has_sector_access = 1; + // Check if card supports 1.8V signaling. if (cond & SD_ROCR_S18A && supports_low_voltage) { //The low voltage regulator configuration is valid for SDMMC1 only. @@ -852,7 +893,7 @@ static int _sd_storage_get_op_cond(sdmmc_storage_t *storage, int is_version_1, i return 0; storage->is_low_voltage = 1; - DPRINTF("-> switched to low voltage\n"); +DPRINTF("-> switched to low voltage\n"); } } @@ -863,8 +904,6 @@ static int _sd_storage_get_op_cond(sdmmc_storage_t *storage, int is_version_1, i msleep(10); // Needs to be at least 10ms for some SD Cards } - DPRINTF("[SD] _sd_storage_get_op_cond Timeout\r\n"); - return 0; } @@ -873,7 +912,7 @@ static int _sd_storage_get_rca(sdmmc_storage_t *storage) sdmmc_cmd_t cmdbuf; sdmmc_init_cmd(&cmdbuf, SD_SEND_RELATIVE_ADDR, 0, SDMMC_RSP_TYPE_4, 0); - u32 timeout = get_tmr_ms() + 1500; + u64 timeout = get_tmr_ms() + 1500; while (1) { @@ -991,34 +1030,37 @@ int _sd_storage_switch(sdmmc_storage_t *storage, void *buf, int mode, int group, return _sdmmc_storage_check_result(tmp); } -void _sd_storage_set_current_limit(sdmmc_storage_t *storage, u8 *buf) +void _sd_storage_set_current_limit(sdmmc_storage_t *storage, u16 current_limit, u8 *buf) { - u32 pwr = SD_SET_CURRENT_LIMIT_800; + u32 pwr = SD_SET_CURRENT_LIMIT_200; + + if (current_limit & SD_MAX_CURRENT_800) + pwr = SD_SET_CURRENT_LIMIT_800; + else if (current_limit & SD_MAX_CURRENT_600) + pwr = SD_SET_CURRENT_LIMIT_600; + else if (current_limit & SD_MAX_CURRENT_400) + pwr = SD_SET_CURRENT_LIMIT_400; + _sd_storage_switch(storage, buf, SD_SWITCH_SET, 3, pwr); - while (pwr > 0) + if (((buf[15] >> 4) & 0x0F) == pwr) { - pwr--; - _sd_storage_switch(storage, buf, SD_SWITCH_SET, 3, pwr); - if (((buf[15] >> 4) & 0x0F) == pwr) + switch (pwr) + { + case SD_SET_CURRENT_LIMIT_800: +DPRINTF("[SD] power limit raised to 800mA\n"); break; - } - - switch (pwr) - { - case SD_SET_CURRENT_LIMIT_800: - DPRINTF("[SD] Power limit raised to 800mA\n"); - break; - case SD_SET_CURRENT_LIMIT_600: - DPRINTF("[SD] Power limit raised to 600mA\n"); - break; - case SD_SET_CURRENT_LIMIT_400: - DPRINTF("[SD] Power limit raised to 800mA\n"); - break; - default: - case SD_SET_CURRENT_LIMIT_200: - DPRINTF("[SD] Power limit defaulted to 200mA\n"); - break; + case SD_SET_CURRENT_LIMIT_600: +DPRINTF("[SD] power limit raised to 600mA\n"); + break; + case SD_SET_CURRENT_LIMIT_400: +DPRINTF("[SD] power limit raised to 400mA\n"); + break; + default: + case SD_SET_CURRENT_LIMIT_200: +DPRINTF("[SD] power limit defaulted to 200mA\n"); + break; + } } } @@ -1026,62 +1068,91 @@ int _sd_storage_enable_highspeed(sdmmc_storage_t *storage, u32 hs_type, u8 *buf) { if (!_sd_storage_switch(storage, buf, SD_SWITCH_CHECK, 0, hs_type)) return 0; +DPRINTF("[SD] supports switch to (U)HS mode\n"); u32 type_out = buf[16] & 0xF; if (type_out != hs_type) return 0; +DPRINTF("[SD] supports selected (U)HS mode\n"); - if ((((u16)buf[0] << 8) | buf[1]) < 0x320) + u16 total_pwr_consumption = ((u16)buf[0] << 8) | buf[1]; +DPRINTF("[SD] total max current: %d\n", total_pwr_consumption); + + if (total_pwr_consumption <= 800) { if (!_sd_storage_switch(storage, buf, SD_SWITCH_SET, 0, hs_type)) return 0; if (type_out != (buf[16] & 0xF)) return 0; - } - return 1; + return 1; + } +DPRINTF("[SD] card max current over limit\n"); + + return 0; } -int _sd_storage_enable_highspeed_low_volt(sdmmc_storage_t *storage, u32 type, u8 *buf) +int _sd_storage_enable_uhs_low_volt(sdmmc_storage_t *storage, u32 type, u8 *buf) { - // Try to raise the current limit to let the card perform better. - _sd_storage_set_current_limit(storage, buf); - if (sdmmc_get_bus_width(storage->sdmmc) != SDMMC_BUS_WIDTH_4) return 0; if (!_sd_storage_switch_get(storage, buf)) return 0; + //gfx_hexdump(0, (u8 *)buf, 64); + + u8 access_mode = buf[13]; + u16 current_limit = buf[7] | buf[6] << 8; + + // Try to raise the current limit to let the card perform better. + _sd_storage_set_current_limit(storage, current_limit, buf); u32 hs_type = 0; switch (type) { - case 11: + case SDHCI_TIMING_UHS_SDR104: + case SDHCI_TIMING_UHS_SDR82: // Fall through if not supported. - if (buf[13] & SD_MODE_UHS_SDR104) + if (access_mode & SD_MODE_UHS_SDR104) { - type = 11; hs_type = UHS_SDR104_BUS_SPEED; - DPRINTF("[SD] Bus speed set to SDR104\n"); - storage->csd.busspeed = 104; +DPRINTF("[SD] bus speed set to SDR104\n"); + switch (type) + { + case SDHCI_TIMING_UHS_SDR104: + storage->csd.busspeed = 104; + break; + case SDHCI_TIMING_UHS_SDR82: + storage->csd.busspeed = 82; + break; + } break; } - case 10: - if (buf[13] & SD_MODE_UHS_SDR50) + case SDHCI_TIMING_UHS_SDR50: + if (access_mode & SD_MODE_UHS_SDR50) { - type = 10; + type = SDHCI_TIMING_UHS_SDR50; hs_type = UHS_SDR50_BUS_SPEED; - DPRINTF("[SD] Bus speed set to SDR50\n"); +DPRINTF("[SD] bus speed set to SDR50\n"); storage->csd.busspeed = 50; break; } - case 8: - if (!(buf[13] & SD_MODE_UHS_SDR12)) + case SDHCI_TIMING_UHS_SDR25: + if (access_mode & SD_MODE_UHS_SDR25) + { + type = SDHCI_TIMING_UHS_SDR25; + hs_type = UHS_SDR50_BUS_SPEED; +DPRINTF("[SD] bus speed set to SDR25\n"); + storage->csd.busspeed = 25; + break; + } + case SDHCI_TIMING_UHS_SDR12: + if (!(access_mode & SD_MODE_UHS_SDR12)) return 0; - type = 8; + type = SDHCI_TIMING_UHS_SDR12; hs_type = UHS_SDR12_BUS_SPEED; - DPRINTF("[SD] Bus speed set to SDR12\n"); +DPRINTF("[SD] bus speed set to SDR12\n"); storage->csd.busspeed = 12; break; default: @@ -1091,106 +1162,38 @@ int _sd_storage_enable_highspeed_low_volt(sdmmc_storage_t *storage, u32 type, u8 if (!_sd_storage_enable_highspeed(storage, hs_type, buf)) return 0; +DPRINTF("[SD] card accepted UHS\n"); if (!sdmmc_setup_clock(storage->sdmmc, type)) return 0; - if (!sdmmc_config_tuning(storage->sdmmc, type, MMC_SEND_TUNING_BLOCK)) +DPRINTF("[SD] setup clock\n"); + if (!sdmmc_tuning_execute(storage->sdmmc, type, MMC_SEND_TUNING_BLOCK)) return 0; +DPRINTF("[SD] config tuning\n"); return _sdmmc_storage_check_status(storage); } -int _sd_storage_enable_highspeed_high_volt(sdmmc_storage_t *storage, u8 *buf) +int _sd_storage_enable_hs_high_volt(sdmmc_storage_t *storage, u8 *buf) { if (!_sd_storage_switch_get(storage, buf)) return 0; - if (!(buf[13] & SD_MODE_HIGH_SPEED)) + //gfx_hexdump(0, (u8 *)buf, 64); + + u8 access_mode = buf[13]; + u16 current_limit = buf[7] | buf[6] << 8; + + // Try to raise the current limit to let the card perform better. + _sd_storage_set_current_limit(storage, current_limit, buf); + + if (!(access_mode & SD_MODE_HIGH_SPEED)) return 1; - if (!_sd_storage_enable_highspeed(storage, 1, buf)) + if (!_sd_storage_enable_highspeed(storage, HIGH_SPEED_BUS_SPEED, buf)) return 0; + if (!_sdmmc_storage_check_status(storage)) return 0; - return sdmmc_setup_clock(storage->sdmmc, 7); -} -static void _sd_storage_parse_ssr(sdmmc_storage_t *storage) -{ - // unstuff_bits supports only 4 u32 so break into 2 x 16byte groups - u32 raw_ssr1[4]; - u32 raw_ssr2[4]; - - raw_ssr1[3] = *(u32 *)&storage->raw_ssr[12]; - raw_ssr1[2] = *(u32 *)&storage->raw_ssr[8]; - raw_ssr1[1] = *(u32 *)&storage->raw_ssr[4]; - raw_ssr1[0] = *(u32 *)&storage->raw_ssr[0]; - - raw_ssr2[3] = *(u32 *)&storage->raw_ssr[28]; - raw_ssr2[2] = *(u32 *)&storage->raw_ssr[24]; - raw_ssr2[1] = *(u32 *)&storage->raw_ssr[20]; - raw_ssr2[0] = *(u32 *)&storage->raw_ssr[16]; - - storage->ssr.bus_width = (unstuff_bits(raw_ssr1, 510 - 384, 2) & SD_BUS_WIDTH_4) ? 4 : 1; - switch(unstuff_bits(raw_ssr1, 440 - 384, 8)) - { - case 0: - storage->ssr.speed_class = 0; - break; - case 1: - storage->ssr.speed_class = 2; - break; - case 2: - storage->ssr.speed_class = 4; - break; - case 3: - storage->ssr.speed_class = 6; - break; - case 4: - storage->ssr.speed_class = 10; - break; - default: - storage->ssr.speed_class = unstuff_bits(raw_ssr1, 440 - 384, 8); - break; - } - storage->ssr.uhs_grade = unstuff_bits(raw_ssr1, 396 - 384, 4); - storage->ssr.video_class = unstuff_bits(raw_ssr1, 384 - 384, 8); - - storage->ssr.app_class = unstuff_bits(raw_ssr2, 336 - 256, 4); -} - -static int _sd_storage_get_ssr(sdmmc_storage_t *storage, u8 *buf) -{ - sdmmc_cmd_t cmdbuf; - sdmmc_init_cmd(&cmdbuf, SD_APP_SD_STATUS, 0, SDMMC_RSP_TYPE_1, 0); - - sdmmc_req_t reqbuf; - reqbuf.buf = buf; - reqbuf.blksize = 64; - reqbuf.num_sectors = 1; - reqbuf.is_write = 0; - reqbuf.is_multi_block = 0; - reqbuf.is_auto_cmd12 = 0; - - if (!(storage->csd.cmdclass & CCC_APP_SPEC)) - { - DPRINTF("[SD] ssr: Card lacks mandatory SD Status function\n"); - return 0; - } - - if (!_sd_storage_execute_app_cmd(storage, R1_STATE_TRAN, 0, &cmdbuf, &reqbuf, 0)) - return 0; - - u32 tmp = 0; - sdmmc_get_rsp(storage->sdmmc, &tmp, 4, SDMMC_RSP_TYPE_1); - //Prepare buffer for unstuff_bits - for (int i = 0; i < 64; i+=4) - { - storage->raw_ssr[i + 3] = buf[i]; - storage->raw_ssr[i + 2] = buf[i + 1]; - storage->raw_ssr[i + 1] = buf[i + 2]; - storage->raw_ssr[i] = buf[i + 3]; - } - _sd_storage_parse_ssr(storage); - - return _sdmmc_storage_check_result(tmp); + return sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_SD_HS25); } static void _sd_storage_parse_cid(sdmmc_storage_t *storage) @@ -1232,45 +1235,65 @@ static void _sd_storage_parse_csd(sdmmc_storage_t *storage) } } -int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 bus_width, u32 type) +static bool _sdmmc_storage_supports_low_voltage(u32 bus_width, u32 type) { + switch (type) + { + case SDHCI_TIMING_UHS_SDR12: + case SDHCI_TIMING_UHS_SDR25: + case SDHCI_TIMING_UHS_SDR50: + case SDHCI_TIMING_UHS_SDR104: + case SDHCI_TIMING_UHS_SDR82: + case SDHCI_TIMING_UHS_DDR50: + if (bus_width == SDMMC_BUS_WIDTH_4) + return true; + default: + return false; + } +} + +int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type) +{ + u8 buf[512]; int is_version_1 = 0; + memset(buf, 0, sizeof(buf)); memset(storage, 0, sizeof(sdmmc_storage_t)); storage->sdmmc = sdmmc; - DPRINTF("[SD] before init\n"); - if (!sdmmc_init(sdmmc, id, SDMMC_POWER_3_3, SDMMC_BUS_WIDTH_1, 5, 0)) + if (!sdmmc_init(sdmmc, SDMMC_1, SDMMC_POWER_3_3, SDMMC_BUS_WIDTH_1, SDHCI_TIMING_SD_ID, SDMMC_AUTO_CAL_DISABLE)) return 0; - DPRINTF("[SD] after init\n"); +DPRINTF("[SD] after init\n"); usleep(1000 + (74000 + sdmmc->divisor - 1) / sdmmc->divisor); if (!_sdmmc_storage_go_idle_state(storage)) return 0; - DPRINTF("[SD] went to idle state\n"); +DPRINTF("[SD] went to idle state\n"); is_version_1 = _sd_storage_send_if_cond(storage); if (is_version_1 == 2) return 0; - DPRINTF("[SD] after send if cond\n"); +DPRINTF("[SD] after send if cond\n"); - if (!_sd_storage_get_op_cond(storage, is_version_1, bus_width == SDMMC_BUS_WIDTH_4 && type == 11)) + bool supports_low_voltage = _sdmmc_storage_supports_low_voltage(bus_width, type); + + if (!_sd_storage_get_op_cond(storage, is_version_1, supports_low_voltage)) return 0; - DPRINTF("[SD] got op cond\n"); +DPRINTF("[SD] got op cond\n"); if (!_sdmmc_storage_get_cid(storage, storage->raw_cid)) return 0; - DPRINTF("[SD] got cid\n"); +DPRINTF("[SD] got cid\n"); _sd_storage_parse_cid(storage); if (!_sd_storage_get_rca(storage)) return 0; - DPRINTF("[SD] got rca (= %04X)\n", storage->rca); +DPRINTF("[SD] got rca (= %04X)\n", storage->rca); if (!_sdmmc_storage_get_csd(storage, storage->raw_csd)) return 0; - DPRINTF("[SD] got csd\n"); +DPRINTF("[SD] got csd\n"); //Parse CSD. _sd_storage_parse_csd(storage); @@ -1283,84 +1306,75 @@ int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 storage->sec_cnt = storage->csd.c_size << 10; break; default: - DPRINTF("[SD] Unknown CSD structure %d\n", storage->csd.structure); +DPRINTF("[SD] unknown CSD structure %d\n", storage->csd.structure); break; } if (!storage->is_low_voltage) { - if (!sdmmc_setup_clock(storage->sdmmc, 6)) + if (!sdmmc_setup_clock(storage->sdmmc, SDHCI_TIMING_SD_DS12)) return 0; - DPRINTF("[SD] after setup clock\n"); +DPRINTF("[SD] after setup clock\n"); } if (!_sdmmc_storage_select_card(storage)) return 0; - DPRINTF("[SD] card selected\n"); +DPRINTF("[SD] card selected\n"); if (!_sdmmc_storage_set_blocklen(storage, 512)) return 0; - DPRINTF("[SD] set blocklen to 512\n"); +DPRINTF("[SD] set blocklen to 512\n"); u32 tmp = 0; if (!_sd_storage_execute_app_cmd_type1(storage, &tmp, SD_APP_SET_CLR_CARD_DETECT, 0, 0, R1_STATE_TRAN)) return 0; - DPRINTF("[SD] cleared card detect\n"); +DPRINTF("[SD] cleared card detect\n"); - u8 *buf = (u8 *)malloc(512); if (!_sd_storage_get_scr(storage, buf)) - { - free(buf); return 0; - } - - DPRINTF("[SD] got scr\n"); + + //gfx_hexdump(0, storage->raw_scr, 8); +DPRINTF("[SD] got scr\n"); // Check if card supports a wider bus and if it's not SD Version 1.X if (bus_width == SDMMC_BUS_WIDTH_4 && (storage->scr.bus_widths & 4) && (storage->scr.sda_vsn & 0xF)) { if (!_sd_storage_execute_app_cmd_type1(storage, &tmp, SD_APP_SET_BUS_WIDTH, SD_BUS_WIDTH_4, 0, R1_STATE_TRAN)) - { - free(buf); return 0; - } + sdmmc_set_bus_width(storage->sdmmc, SDMMC_BUS_WIDTH_4); - DPRINTF("[SD] switched to wide bus width\n"); +DPRINTF("[SD] switched to wide bus width\n"); } else { - DPRINTF("[SD] SD does not support wide bus width\n"); +DPRINTF("[SD] SD does not support wide bus width\n"); } if (storage->is_low_voltage) { - if (!_sd_storage_enable_highspeed_low_volt(storage, type, buf)) - { - free(buf); + if (!_sd_storage_enable_uhs_low_volt(storage, type, buf)) return 0; - } - DPRINTF("[SD] enabled highspeed (low voltage)\n"); +DPRINTF("[SD] enabled UHS\n"); + + sdmmc_card_clock_ctrl(sdmmc, SDMMC_AUTO_CAL_ENABLE); } - else if (type != 6 && (storage->scr.sda_vsn & 0xF) != 0) + else if (type != SDHCI_TIMING_SD_DS12 && (storage->scr.sda_vsn & 0xF) != 0) { - if (!_sd_storage_enable_highspeed_high_volt(storage, buf)) - { - free(buf); + if (!_sd_storage_enable_hs_high_volt(storage, buf)) return 0; + +DPRINTF("[SD] enabled HS\n"); + switch (bus_width) + { + case SDMMC_BUS_WIDTH_4: + storage->csd.busspeed = 25; + break; + case SDMMC_BUS_WIDTH_1: + storage->csd.busspeed = 6; + break; } - DPRINTF("[SD] enabled highspeed (high voltage)\n"); - storage->csd.busspeed = 25; } - sdmmc_sd_clock_ctrl(sdmmc, 1); - - // Parse additional card info from sd status. - if (_sd_storage_get_ssr(storage, buf)) - { - DPRINTF("[SD] got sd status\n"); - } - - free(buf); return 1; } @@ -1400,17 +1414,17 @@ int sdmmc_storage_init_gc(sdmmc_storage_t *storage, sdmmc_t *sdmmc) memset(storage, 0, sizeof(sdmmc_storage_t)); storage->sdmmc = sdmmc; - if (!sdmmc_init(sdmmc, SDMMC_2, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_8, 14, 0)) + if (!sdmmc_init(sdmmc, SDMMC_2, SDMMC_POWER_1_8, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_DDR52, SDMMC_AUTO_CAL_DISABLE)) return 0; - DPRINTF("[gc] after init\n"); +DPRINTF("[gc] after init\n"); usleep(1000 + (10000 + sdmmc->divisor - 1) / sdmmc->divisor); - if (!sdmmc_config_tuning(storage->sdmmc, 14, MMC_SEND_TUNING_BLOCK_HS200)) + if (!sdmmc_tuning_execute(storage->sdmmc, SDHCI_TIMING_MMC_DDR52, MMC_SEND_TUNING_BLOCK_HS200)) return 0; - DPRINTF("[gc] after tuning\n"); +DPRINTF("[gc] after tuning\n"); - sdmmc_sd_clock_ctrl(sdmmc, 1); + sdmmc_card_clock_ctrl(sdmmc, SDMMC_AUTO_CAL_ENABLE); return 1; } diff --git a/emummc/source/emmc/sdmmc.h b/emummc/source/emmc/sdmmc.h index fc9e54831..96c83be12 100644 --- a/emummc/source/emmc/sdmmc.h +++ b/emummc/source/emmc/sdmmc.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2018 naehrwert - * Copyright (C) 2018 CTCaer + * Copyright (c) 2018 CTCaer * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -95,12 +95,10 @@ typedef struct _sdmmc_storage_t u8 raw_cid[0x10]; u8 raw_csd[0x10]; u8 raw_scr[8]; - u8 raw_ssr[0x40]; mmc_cid_t cid; mmc_csd_t csd; mmc_ext_csd_t ext_csd; sd_scr_t scr; - sd_ssr_t ssr; } sdmmc_storage_t; extern sdmmc_accessor_t *_current_accessor; @@ -109,9 +107,9 @@ extern bool sdmmc_memcpy_buf; int sdmmc_storage_end(sdmmc_storage_t *storage); int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); -int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 bus_width, u32 type); +int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type); int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition); -int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 id, u32 bus_width, u32 type); +int sdmmc_storage_init_sd(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type); int sdmmc_storage_init_gc(sdmmc_storage_t *storage, sdmmc_t *sdmmc); intptr_t sdmmc_calculate_dma_addr(sdmmc_accessor_t *_this, void *buf, unsigned int num_sectors); int sdmmc_calculate_dma_index(sdmmc_accessor_t *_this, void *buf, unsigned int num_sectors); diff --git a/emummc/source/emmc/sdmmc_driver.c b/emummc/source/emmc/sdmmc_driver.c index 7e20c3786..fc63f6e5c 100644 --- a/emummc/source/emmc/sdmmc_driver.c +++ b/emummc/source/emmc/sdmmc_driver.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2018 naehrwert - * Copyright (C) 2018 CTCaer + * Copyright (c) 2018-2019 CTCaer * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -16,18 +16,18 @@ */ #include -#include "../utils/types.h" -#include "../nx/cache.h" -#include "sdmmc.h" -#include "../utils/util.h" -#include "../soc/clock.h" #include "mmc.h" +#include "sdmmc.h" +#include "../nx/cache.h" #include "../power/max7762x.h" -#include "../soc/t210.h" -#include "../soc/pmc.h" -#include "../soc/pinmux.h" +#include "../soc/clock.h" #include "../soc/gpio.h" +#include "../soc/pinmux.h" +#include "../soc/pmc.h" +#include "../soc/t210.h" #include "../utils/fatal.h" +#include "../utils/types.h" +#include "../utils/util.h" #define DPRINTF(...) @@ -39,44 +39,37 @@ static const u64 _sdmmc_bases[4] = { 0x700B0600, }; -int sdmmc_get_voltage(sdmmc_t *sdmmc) +int sdmmc_get_io_power(sdmmc_t *sdmmc) { u32 p = sdmmc->regs->pwrcon; - if (!(p & TEGRA_MMC_PWRCTL_SD_BUS_POWER)) + if (!(p & SDHCI_POWER_ON)) return SDMMC_POWER_OFF; - if (p & TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8) + if (p & SDHCI_POWER_180) return SDMMC_POWER_1_8; - if (p & TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3) + if (p & SDHCI_POWER_330) return SDMMC_POWER_3_3; return -1; } -static int _sdmmc_set_voltage(sdmmc_t *sdmmc, u32 power) +static int _sdmmc_set_io_power(sdmmc_t *sdmmc, u32 power) { - u8 pwr = 0; - switch (power) { case SDMMC_POWER_OFF: - sdmmc->regs->pwrcon &= ~TEGRA_MMC_PWRCTL_SD_BUS_POWER; + sdmmc->regs->pwrcon &= ~SDHCI_POWER_ON; break; case SDMMC_POWER_1_8: - sdmmc->regs->pwrcon = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8; - pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8; + sdmmc->regs->pwrcon = SDHCI_POWER_180; break; case SDMMC_POWER_3_3: - sdmmc->regs->pwrcon = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3; - pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3; + sdmmc->regs->pwrcon = SDHCI_POWER_330; break; default: return 0; } if (power != SDMMC_POWER_OFF) - { - pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER; - sdmmc->regs->pwrcon = pwr; - } + sdmmc->regs->pwrcon |= SDHCI_POWER_ON; return 1; } @@ -84,48 +77,52 @@ static int _sdmmc_set_voltage(sdmmc_t *sdmmc, u32 power) u32 sdmmc_get_bus_width(sdmmc_t *sdmmc) { u32 h = sdmmc->regs->hostctl; - if (h & TEGRA_MMC_HOSTCTL_8BIT) + if (h & SDHCI_CTRL_8BITBUS) return SDMMC_BUS_WIDTH_8; - if (h & TEGRA_MMC_HOSTCTL_4BIT) + if (h & SDHCI_CTRL_4BITBUS) return SDMMC_BUS_WIDTH_4; return SDMMC_BUS_WIDTH_1; } void sdmmc_set_bus_width(sdmmc_t *sdmmc, u32 bus_width) { + u32 host_control = sdmmc->regs->hostctl & ~(SDHCI_CTRL_4BITBUS | SDHCI_CTRL_8BITBUS); + if (bus_width == SDMMC_BUS_WIDTH_1) - sdmmc->regs->hostctl &= ~(TEGRA_MMC_HOSTCTL_4BIT | TEGRA_MMC_HOSTCTL_8BIT); + sdmmc->regs->hostctl = host_control; else if (bus_width == SDMMC_BUS_WIDTH_4) - { - sdmmc->regs->hostctl |= TEGRA_MMC_HOSTCTL_4BIT; - sdmmc->regs->hostctl &= ~TEGRA_MMC_HOSTCTL_8BIT; - } + sdmmc->regs->hostctl = host_control | SDHCI_CTRL_4BITBUS; else if (bus_width == SDMMC_BUS_WIDTH_8) - sdmmc->regs->hostctl |= TEGRA_MMC_HOSTCTL_8BIT; + sdmmc->regs->hostctl = host_control | SDHCI_CTRL_8BITBUS; } -void sdmmc_get_venclkctl(sdmmc_t *sdmmc) +void sdmmc_set_tap_value(sdmmc_t *sdmmc) { sdmmc->venclkctl_tap = sdmmc->regs->venclkctl >> 16; sdmmc->venclkctl_set = 1; } -static int _sdmmc_config_ven_ceata_clk(sdmmc_t *sdmmc, u32 id) +static int _sdmmc_config_tap_val(sdmmc_t *sdmmc, u32 type) { + const u32 dqs_trim_val = 0x28; + const u32 tap_values[] = { 4, 0, 3, 0 }; + u32 tap_val = 0; - if (id == 4) - sdmmc->regs->venceatactl = (sdmmc->regs->venceatactl & 0xFFFFC0FF) | 0x2800; - sdmmc->regs->ventunctl0 &= 0xFFFDFFFF; - if (id == 4) + if (type == SDHCI_TIMING_MMC_HS400) + sdmmc->regs->vencapover = (sdmmc->regs->vencapover & 0xFFFFC0FF) | (dqs_trim_val << 8); + + sdmmc->regs->ventunctl0 &= ~TEGRA_MMC_VNDR_TUN_CTRL0_TAP_VAL_UPDATED_BY_HW; + + if (type == SDHCI_TIMING_MMC_HS400) { if (!sdmmc->venclkctl_set) return 0; + tap_val = sdmmc->venclkctl_tap; } else { - static const u32 tap_values[] = { 4, 0, 3, 0 }; tap_val = tap_values[sdmmc->id]; } sdmmc->regs->venclkctl = (sdmmc->regs->venclkctl & 0xFF00FFFF) | (tap_val << 16); @@ -143,163 +140,250 @@ static void _sdmmc_pad_config_fallback(sdmmc_t *sdmmc, u32 power) _sdmmc_get_clkcon(sdmmc); switch (sdmmc->id) { - case SDMMC_1: + case SDMMC_1: // 33 Ohm 2X Driver. if (power == SDMMC_POWER_OFF) break; + u32 sdmmc1_pad_cfg = APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) & 0xF8080FFF; if (power == SDMMC_POWER_1_8) - APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = 0x304; // Up: 3, Dn: 4. + APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = sdmmc1_pad_cfg | (0xB0F << 12); // Up: 11, Dn: 15. For 33 ohm. else if (power == SDMMC_POWER_3_3) - APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = 0x808; // Up: 8, Dn: 8. + APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = sdmmc1_pad_cfg | (0xC0C << 12); // Up: 12, Dn: 12. For 33 ohm. break; - case SDMMC_4: - APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) = (APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) & 0x3FFC) | 0x1040; + case SDMMC_2: + case SDMMC_4: // 50 Ohm 2X Driver. PU:16, PD:16. + APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) = (APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) & 0xFFFFC003) | 0x1040; break; } - //TODO: load standard values for other controllers, can depend on power. } -static int _sdmmc_wait_type4(sdmmc_t *sdmmc) +static void _sdmmc_autocal_execute(sdmmc_t *sdmmc, u32 power) { - int res = 1, should_disable_sd_clock = 0; - - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) + bool should_enable_sd_clock = false; + if (sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN) { - should_disable_sd_clock = 1; - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + should_enable_sd_clock = true; + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; } - sdmmc->regs->vendllcal |= 0x80000000; - _sdmmc_get_clkcon(sdmmc); + // Enable E_INPUT power. + if (!(sdmmc->regs->sdmemcmppadctl & TEGRA_MMC_SDMEMCOMPPADCTRL_PAD_E_INPUT_PWRD)) + { + sdmmc->regs->sdmemcmppadctl |= TEGRA_MMC_SDMEMCOMPPADCTRL_PAD_E_INPUT_PWRD; + _sdmmc_get_clkcon(sdmmc); + usleep(1); + } - u32 timeout = get_tmr_ms() + 5; - while (sdmmc->regs->vendllcal & 0x80000000) + // Enable auto calibration and start auto configuration. + sdmmc->regs->autocalcfg |= TEGRA_MMC_AUTOCALCFG_AUTO_CAL_ENABLE | TEGRA_MMC_AUTOCALCFG_AUTO_CAL_START; + _sdmmc_get_clkcon(sdmmc); + usleep(2); + + u64 timeout = get_tmr_ms() + 10; + while (sdmmc->regs->autocalsts & TEGRA_MMC_AUTOCALSTS_AUTO_CAL_ACTIVE) { if (get_tmr_ms() > timeout) { - res = 0; + timeout = 0; // Set timeout to 0 if we timed out. + break; + } + } +/* + // Check if PU results are inside limits. + // SDMMC1: CZ pads - 7-bit PU. SDMMC2/4: LV_CZ pads - 5-bit PU. + u8 autocal_pu_status = sdmmc->regs->autocalsts & 0x7F; + switch (sdmmc->id) + { + case SDMMC_1: + if (!autocal_pu_status || autocal_pu_status == 0x7F) + timeout = 0; + break; + case SDMMC_2: + case SDMMC_4: + autocal_pu_status &= 0x1F; + if (!autocal_pu_status || autocal_pu_status == 0x1F) + timeout = 0; + break; + } +*/ + // In case auto calibration fails, we load suggested standard values. + if (!timeout) + { + _sdmmc_pad_config_fallback(sdmmc, power); + sdmmc->regs->autocalcfg &= ~TEGRA_MMC_AUTOCALCFG_AUTO_CAL_ENABLE; + } + + // Disable E_INPUT to conserve power. + sdmmc->regs->sdmemcmppadctl &= ~TEGRA_MMC_SDMEMCOMPPADCTRL_PAD_E_INPUT_PWRD; + + if(should_enable_sd_clock) + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; +} + +static int _sdmmc_dll_cal_execute(sdmmc_t *sdmmc) +{ + int result = 1, should_disable_sd_clock = 0; + + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) + { + should_disable_sd_clock = 1; + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; + } + + sdmmc->regs->vendllcalcfg |= TEGRA_MMC_DLLCAL_CFG_EN_CALIBRATE; + _sdmmc_get_clkcon(sdmmc); + + u64 timeout = get_tmr_ms() + 5; + while (sdmmc->regs->vendllcalcfg & TEGRA_MMC_DLLCAL_CFG_EN_CALIBRATE) + { + if (get_tmr_ms() > timeout) + { + result = 0; goto out; } } timeout = get_tmr_ms() + 10; - while (sdmmc->regs->dllcfgstatus & 0x80000000) + while (sdmmc->regs->vendllcalcfgsts & TEGRA_MMC_DLLCAL_CFG_STATUS_DLL_ACTIVE) { if (get_tmr_ms() > timeout) { - res = 0; + result = 0; goto out; } } out:; if (should_disable_sd_clock) - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; - return res; + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; + return result; +} + +static void _sdmmc_reset(sdmmc_t *sdmmc) +{ + sdmmc->regs->swrst |= SDHCI_RESET_CMD | SDHCI_RESET_DATA; + _sdmmc_get_clkcon(sdmmc); + u64 timeout = get_tmr_ms() + 2000; + while ((sdmmc->regs->swrst & (SDHCI_RESET_CMD | SDHCI_RESET_DATA)) && get_tmr_ms() < timeout) + ; } int sdmmc_setup_clock(sdmmc_t *sdmmc, u32 type) { - //Disable the SD clock if it was enabled, and reenable it later. + // Disable the SD clock if it was enabled, and reenable it later. bool should_enable_sd_clock = false; - if (sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE) + if (sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN) { should_enable_sd_clock = true; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; } - _sdmmc_config_ven_ceata_clk(sdmmc, type); + _sdmmc_config_tap_val(sdmmc, type); + + _sdmmc_reset(sdmmc); switch (type) { - case 0: - case 1: - case 5: - case 6: - sdmmc->regs->hostctl &= 0xFB; //Should this be 0xFFFB (~4) ? - sdmmc->regs->hostctl2 &= SDHCI_CTRL_VDD_330; + case SDHCI_TIMING_MMC_ID: + case SDHCI_TIMING_MMC_LS26: + case SDHCI_TIMING_SD_ID: + case SDHCI_TIMING_SD_DS12: + sdmmc->regs->hostctl &= ~SDHCI_CTRL_HISPD; + sdmmc->regs->hostctl2 &= ~SDHCI_CTRL_VDD_180; break; - case 2: - case 7: - sdmmc->regs->hostctl |= 4; - sdmmc->regs->hostctl2 &= SDHCI_CTRL_VDD_330; + case SDHCI_TIMING_MMC_HS52: + case SDHCI_TIMING_SD_HS25: + sdmmc->regs->hostctl |= SDHCI_CTRL_HISPD; + sdmmc->regs->hostctl2 &= ~SDHCI_CTRL_VDD_180; break; - case 3: - case 11: - case 13: - case 14: + case SDHCI_TIMING_MMC_HS200: + case SDHCI_TIMING_UHS_SDR50: // T210 Errata for SDR50, the host must be set to SDR104. + case SDHCI_TIMING_UHS_SDR104: + case SDHCI_TIMING_UHS_SDR82: + case SDHCI_TIMING_UHS_DDR50: + case SDHCI_TIMING_MMC_DDR52: sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | UHS_SDR104_BUS_SPEED; sdmmc->regs->hostctl2 |= SDHCI_CTRL_VDD_180; break; - case 4: - //Non standard + case SDHCI_TIMING_MMC_HS400: + // Non standard. sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | HS400_BUS_SPEED; sdmmc->regs->hostctl2 |= SDHCI_CTRL_VDD_180; break; - case 8: - sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | UHS_SDR12_BUS_SPEED; + case SDHCI_TIMING_UHS_SDR25: + sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | UHS_SDR25_BUS_SPEED; sdmmc->regs->hostctl2 |= SDHCI_CTRL_VDD_180; break; - case 10: - //T210 Errata for SDR50, the host must be set to SDR104. - sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | UHS_SDR104_BUS_SPEED; + case SDHCI_TIMING_UHS_SDR12: + sdmmc->regs->hostctl2 = (sdmmc->regs->hostctl2 & SDHCI_CTRL_UHS_MASK) | UHS_SDR12_BUS_SPEED; sdmmc->regs->hostctl2 |= SDHCI_CTRL_VDD_180; break; } _sdmmc_get_clkcon(sdmmc); - u32 tmp; + u32 clock; u16 divisor; - clock_sdmmc_get_params(&tmp, &divisor, type); - clock_sdmmc_config_clock_source(&tmp, sdmmc->id, tmp); - sdmmc->divisor = (tmp + divisor - 1) / divisor; + clock_sdmmc_get_card_clock_div(&clock, &divisor, type); + clock_sdmmc_config_clock_source(&clock, sdmmc->id, clock); + sdmmc->divisor = (clock + divisor - 1) / divisor; //if divisor != 1 && divisor << 31 -> error u16 div = divisor >> 1; divisor = 0; if (div > 0xFF) - divisor = div >> 8; - sdmmc->regs->clkcon = (sdmmc->regs->clkcon & 0x3F) | (div << 8) | (divisor << 6); + divisor = div >> SDHCI_DIVIDER_SHIFT; - //Enable the SD clock again. + sdmmc->regs->clkcon = (sdmmc->regs->clkcon & ~(SDHCI_DIV_MASK | SDHCI_DIV_HI_MASK)) + | (div << SDHCI_DIVIDER_SHIFT) | (divisor << SDHCI_DIVIDER_HI_SHIFT); + + // Enable the SD clock again. if (should_enable_sd_clock) - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; - if (type == 4) - return _sdmmc_wait_type4(sdmmc); + if (type == SDHCI_TIMING_MMC_HS400) + return _sdmmc_dll_cal_execute(sdmmc); return 1; } -static void _sdmmc_sd_clock_enable(sdmmc_t *sdmmc) +static void _sdmmc_card_clock_enable(sdmmc_t *sdmmc) { - if (!sdmmc->no_sd) + // Recalibrate conditionally. + if ((sdmmc->id == SDMMC_1) && !sdmmc->auto_cal_enabled) + _sdmmc_autocal_execute(sdmmc, sdmmc_get_io_power(sdmmc)); + + if (!sdmmc->auto_cal_enabled) { - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; } - sdmmc->sd_clock_enabled = 1; + sdmmc->card_clock_enabled = 1; } static void _sdmmc_sd_clock_disable(sdmmc_t *sdmmc) { - sdmmc->sd_clock_enabled = 0; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->card_clock_enabled = 0; + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; } -void sdmmc_sd_clock_ctrl(sdmmc_t *sdmmc, int no_sd) +void sdmmc_card_clock_ctrl(sdmmc_t *sdmmc, int auto_cal_enable) { - sdmmc->no_sd = no_sd; - if (no_sd) + // Recalibrate periodically for SDMMC1. + if ((sdmmc->id == SDMMC_1) && !auto_cal_enable && sdmmc->card_clock_enabled) + _sdmmc_autocal_execute(sdmmc, sdmmc_get_io_power(sdmmc)); + + sdmmc->auto_cal_enabled = auto_cal_enable; + if (auto_cal_enable) { - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) return; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; return; } - if (sdmmc->sd_clock_enabled) - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + + if (sdmmc->card_clock_enabled) + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; } static int _sdmmc_cache_rsp(sdmmc_t *sdmmc, u32 *rsp, u32 size, u32 type) @@ -381,22 +465,12 @@ int sdmmc_get_rsp(sdmmc_t *sdmmc, u32 *rsp, u32 size, u32 type) return 1; } -static void _sdmmc_reset(sdmmc_t *sdmmc) -{ - sdmmc->regs->swrst |= - TEGRA_MMC_SWRST_SW_RESET_FOR_CMD_LINE | TEGRA_MMC_SWRST_SW_RESET_FOR_DAT_LINE; - _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_ms() + 2000; - while (sdmmc->regs->swrst << 29 >> 30 && get_tmr_ms() < timeout) - ; -} - -static int _sdmmc_wait_prnsts_type0(sdmmc_t *sdmmc, u32 wait_dat) +static int _sdmmc_wait_cmd_data_inhibit(sdmmc_t *sdmmc, bool wait_dat) { _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_ms() + 2000; - while(sdmmc->regs->prnsts & 1) //CMD inhibit. + u64 timeout = get_tmr_ms() + 2000; + while(sdmmc->regs->prnsts & SDHCI_CMD_INHIBIT) if (get_tmr_ms() > timeout) { _sdmmc_reset(sdmmc); @@ -406,7 +480,7 @@ static int _sdmmc_wait_prnsts_type0(sdmmc_t *sdmmc, u32 wait_dat) if (wait_dat) { timeout = get_tmr_ms() + 2000; - while (sdmmc->regs->prnsts & 2) //DAT inhibit. + while (sdmmc->regs->prnsts & SDHCI_DATA_INHIBIT) if (get_tmr_ms() > timeout) { _sdmmc_reset(sdmmc); @@ -417,12 +491,12 @@ static int _sdmmc_wait_prnsts_type0(sdmmc_t *sdmmc, u32 wait_dat) return 1; } -static int _sdmmc_wait_prnsts_type1(sdmmc_t *sdmmc) +static int _sdmmc_wait_card_busy(sdmmc_t *sdmmc) { _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_ms() + 2000; - while (!(sdmmc->regs->prnsts & 0x100000)) //DAT0 line level. + u64 timeout = get_tmr_ms() + 2000; + while (!(sdmmc->regs->prnsts & SDHCI_DATA_0_LVL_MASK)) if (get_tmr_ms() > timeout) { _sdmmc_reset(sdmmc); @@ -440,21 +514,21 @@ static int _sdmmc_setup_read_small_block(sdmmc_t *sdmmc) return 0; break; case SDMMC_BUS_WIDTH_4: - sdmmc->regs->blksize = 0x40; + sdmmc->regs->blksize = 64; break; case SDMMC_BUS_WIDTH_8: - sdmmc->regs->blksize = 0x80; + sdmmc->regs->blksize = 128; break; } sdmmc->regs->blkcnt = 1; - sdmmc->regs->trnmod = TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ; + sdmmc->regs->trnmod = SDHCI_TRNS_READ; return 1; } -static int _sdmmc_parse_cmdbuf(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, bool is_data_present) +static int _sdmmc_send_cmd(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, bool is_data_present) { u16 cmdflags = 0; - + switch (cmd->rsp_type) { case SDMMC_RSP_TYPE_0: @@ -463,20 +537,15 @@ static int _sdmmc_parse_cmdbuf(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, bool is_data_pr case SDMMC_RSP_TYPE_4: case SDMMC_RSP_TYPE_5: if (cmd->check_busy) - cmdflags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY | - TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK | - TEGRA_MMC_TRNMOD_CMD_CRC_CHECK; + cmdflags = SDHCI_CMD_RESP_LEN48_BUSY | SDHCI_CMD_INDEX | SDHCI_CMD_CRC; else - cmdflags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48 | - TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK | - TEGRA_MMC_TRNMOD_CMD_CRC_CHECK; + cmdflags = SDHCI_CMD_RESP_LEN48 | SDHCI_CMD_INDEX | SDHCI_CMD_CRC; break; case SDMMC_RSP_TYPE_2: - cmdflags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136 | - TEGRA_MMC_TRNMOD_CMD_CRC_CHECK; + cmdflags = SDHCI_CMD_RESP_LEN136 | SDHCI_CMD_CRC; break; case SDMMC_RSP_TYPE_3: - cmdflags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48; + cmdflags = SDHCI_CMD_RESP_LEN48; break; default: return 0; @@ -484,121 +553,134 @@ static int _sdmmc_parse_cmdbuf(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, bool is_data_pr } if (is_data_present) - cmdflags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER; + cmdflags |= SDHCI_CMD_DATA; sdmmc->regs->argument = cmd->arg; sdmmc->regs->cmdreg = (cmd->cmd << 8) | cmdflags; return 1; } -static void _sdmmc_parse_cmd_48(sdmmc_t *sdmmc, u32 cmd) +static void _sdmmc_send_tuning_cmd(sdmmc_t *sdmmc, u32 cmd) { sdmmc_cmd_t cmdbuf; cmdbuf.cmd = cmd; cmdbuf.arg = 0; cmdbuf.rsp_type = SDMMC_RSP_TYPE_1; cmdbuf.check_busy = 0; - _sdmmc_parse_cmdbuf(sdmmc, &cmdbuf, true); + _sdmmc_send_cmd(sdmmc, &cmdbuf, true); } -static int _sdmmc_config_tuning_once(sdmmc_t *sdmmc, u32 cmd) +static int _sdmmc_tuning_execute_once(sdmmc_t *sdmmc, u32 cmd) { - if (sdmmc->no_sd) + if (sdmmc->auto_cal_enabled) return 0; - if (!_sdmmc_wait_prnsts_type0(sdmmc, 1)) + if (!_sdmmc_wait_cmd_data_inhibit(sdmmc, true)) return 0; _sdmmc_setup_read_small_block(sdmmc); - sdmmc->regs->norintstsen |= TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY; + + sdmmc->regs->norintstsen |= SDHCI_INT_DATA_AVAIL; sdmmc->regs->norintsts = sdmmc->regs->norintsts; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; - _sdmmc_parse_cmd_48(sdmmc, cmd); + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; + + _sdmmc_send_tuning_cmd(sdmmc, cmd); _sdmmc_get_clkcon(sdmmc); usleep(1); + _sdmmc_reset(sdmmc); - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_us() + 5000; + u64 timeout = get_tmr_us() + 5000; while (get_tmr_us() < timeout) { - if (sdmmc->regs->norintsts & 0x20) + if (sdmmc->regs->norintsts & SDHCI_INT_DATA_AVAIL) { - sdmmc->regs->norintsts = 0x20; - sdmmc->regs->norintstsen &= 0xFFDF; + sdmmc->regs->norintsts = SDHCI_INT_DATA_AVAIL; + sdmmc->regs->norintstsen &= ~SDHCI_INT_DATA_AVAIL; _sdmmc_get_clkcon(sdmmc); usleep((1000 * 8 + sdmmc->divisor - 1) / sdmmc->divisor); return 1; } } + _sdmmc_reset(sdmmc); - sdmmc->regs->norintstsen &= 0xFFDF; + + sdmmc->regs->norintstsen &= ~SDHCI_INT_DATA_AVAIL; _sdmmc_get_clkcon(sdmmc); usleep((1000 * 8 + sdmmc->divisor - 1) / sdmmc->divisor); + return 0; } -int sdmmc_config_tuning(sdmmc_t *sdmmc, u32 type, u32 cmd) +int sdmmc_tuning_execute(sdmmc_t *sdmmc, u32 type, u32 cmd) { u32 max = 0, flag = 0; - sdmmc->regs->field_1C4 = 0; switch (type) { - case 3: - case 4: - case 11: - max = 0x80; - flag = 0x4000; + case SDHCI_TIMING_MMC_HS200: + case SDHCI_TIMING_MMC_HS400: + case SDHCI_TIMING_UHS_SDR104: + case SDHCI_TIMING_UHS_SDR82: + max = 128; + flag = (2 << 13); // 128 iterations. break; - case 10: - case 13: - case 14: - max = 0x100; - flag = 0x8000; + case SDHCI_TIMING_UHS_SDR50: + case SDHCI_TIMING_UHS_DDR50: + case SDHCI_TIMING_MMC_DDR52: + max = 256; + flag = (4 << 13); // 256 iterations. break; + case SDHCI_TIMING_UHS_SDR12: + case SDHCI_TIMING_UHS_SDR25: + return 1; default: return 0; } - sdmmc->regs->ventunctl0 = (sdmmc->regs->ventunctl0 & 0xFFFF1FFF) | flag; - sdmmc->regs->ventunctl0 = (sdmmc->regs->ventunctl0 & 0xFFFFE03F) | 0x40; - sdmmc->regs->ventunctl0 |= 0x20000; - sdmmc->regs->hostctl2 |= SDHCI_CTRL_EXEC_TUNING; + sdmmc->regs->ventunctl1 = 0; // step_size 1. + + sdmmc->regs->ventunctl0 = (sdmmc->regs->ventunctl0 & 0xFFFF1FFF) | flag; // Tries. + sdmmc->regs->ventunctl0 = (sdmmc->regs->ventunctl0 & 0xFFFFE03F) | (1 << 6); // 1x Multiplier. + sdmmc->regs->ventunctl0 |= TEGRA_MMC_VNDR_TUN_CTRL0_TAP_VAL_UPDATED_BY_HW; + sdmmc->regs->hostctl2 |= SDHCI_CTRL_EXEC_TUNING; for (u32 i = 0; i < max; i++) { - _sdmmc_config_tuning_once(sdmmc, cmd); + _sdmmc_tuning_execute_once(sdmmc, cmd); if (!(sdmmc->regs->hostctl2 & SDHCI_CTRL_EXEC_TUNING)) break; } if (sdmmc->regs->hostctl2 & SDHCI_CTRL_TUNED_CLK) return 1; + return 0; } static int _sdmmc_enable_internal_clock(sdmmc_t *sdmmc) { //Enable internal clock and wait till it is stable. - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE; + sdmmc->regs->clkcon |= SDHCI_CLOCK_INT_EN; _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_ms() + 2000; - while (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) + u64 timeout = get_tmr_ms() + 2000; + while (!(sdmmc->regs->clkcon & SDHCI_CLOCK_INT_STABLE)) { if (get_tmr_ms() > timeout) return 0; } sdmmc->regs->hostctl2 &= ~SDHCI_CTRL_PRESET_VAL_EN; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_CLKGEN_SELECT; + sdmmc->regs->clkcon &= ~SDHCI_PROG_CLOCK_MODE; sdmmc->regs->hostctl2 |= SDHCI_HOST_VERSION_4_EN; - if (!(sdmmc->regs->capareg & 0x10000000)) + if (!(sdmmc->regs->capareg & SDHCI_CAN_64BIT)) return 0; sdmmc->regs->hostctl2 |= SDHCI_ADDRESSING_64BIT_EN; - sdmmc->regs->hostctl &= 0xE7; + sdmmc->regs->hostctl &= ~SDHCI_CTRL_DMA_MASK; sdmmc->regs->timeoutcon = (sdmmc->regs->timeoutcon & 0xF0) | 0xE; return 1; @@ -635,60 +717,22 @@ static int _sdmmc_autocal_config_offset(sdmmc_t *sdmmc, u32 power) break; } - sdmmc->regs->autocalcfg = (((sdmmc->regs->autocalcfg & 0xFFFF80FF) | (off_pd << 8)) >> 7 << 7) | off_pu; + sdmmc->regs->autocalcfg = (sdmmc->regs->autocalcfg & 0xFFFF8080) | (off_pd << 8) | off_pu; return 1; } -static void _sdmmc_autocal_execute(sdmmc_t *sdmmc, u32 power) -{ - bool should_enable_sd_clock = false; - if (sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE) - { - should_enable_sd_clock = true; - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; - } - - if (!(sdmmc->regs->sdmemcmppadctl & 0x80000000)) - { - sdmmc->regs->sdmemcmppadctl |= 0x80000000; - _sdmmc_get_clkcon(sdmmc); - usleep(1); - } - - sdmmc->regs->autocalcfg |= 0xA0000000; - _sdmmc_get_clkcon(sdmmc); - usleep(1); - - u32 timeout = get_tmr_ms() + 10; - while (sdmmc->regs->autocalcfg & 0x80000000) - { - if (get_tmr_ms() > timeout) - { - //In case autocalibration fails, we load suggested standard values. - _sdmmc_pad_config_fallback(sdmmc, power); - sdmmc->regs->autocalcfg &= 0xDFFFFFFF; - break; - } - } - - sdmmc->regs->sdmemcmppadctl &= 0x7FFFFFFF; - - if(should_enable_sd_clock) - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; -} - static void _sdmmc_enable_interrupts(sdmmc_t *sdmmc) { - sdmmc->regs->norintstsen |= 0xB; - sdmmc->regs->errintstsen |= 0x17F; + sdmmc->regs->norintstsen |= SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE; + sdmmc->regs->errintstsen |= SDHCI_ERR_INT_ALL_EXCEPT_ADMA_BUSPWR; sdmmc->regs->norintsts = sdmmc->regs->norintsts; sdmmc->regs->errintsts = sdmmc->regs->errintsts; } static void _sdmmc_mask_interrupts(sdmmc_t *sdmmc) { - sdmmc->regs->errintstsen &= 0xFE80; - sdmmc->regs->norintstsen &= 0xFFF4; + sdmmc->regs->errintstsen &= ~SDHCI_ERR_INT_ALL_EXCEPT_ADMA_BUSPWR; + sdmmc->regs->norintstsen &= ~(SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE); } static int _sdmmc_check_mask_interrupt(sdmmc_t *sdmmc, u16 *pout, u16 mask) @@ -696,13 +740,13 @@ static int _sdmmc_check_mask_interrupt(sdmmc_t *sdmmc, u16 *pout, u16 mask) u16 norintsts = sdmmc->regs->norintsts; u16 errintsts = sdmmc->regs->errintsts; - DPRINTF("norintsts %08X; errintsts %08X\n", norintsts, errintsts); +DPRINTF("norintsts %08X; errintsts %08X\n", norintsts, errintsts); if (pout) *pout = norintsts; - //Check for error interrupt. - if (norintsts & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) + // Check for error interrupt. + if (norintsts & SDHCI_INT_ERROR) { sdmmc->regs->errintsts = errintsts; return SDMMC_MASKINT_ERROR; @@ -712,21 +756,21 @@ static int _sdmmc_check_mask_interrupt(sdmmc_t *sdmmc, u16 *pout, u16 mask) sdmmc->regs->norintsts = norintsts & mask; return SDMMC_MASKINT_MASKED; } - + return SDMMC_MASKINT_NOERROR; } -static int _sdmmc_wait_request(sdmmc_t *sdmmc) +static int _sdmmc_wait_response(sdmmc_t *sdmmc) { _sdmmc_get_clkcon(sdmmc); - u32 timeout = get_tmr_ms() + 2000; - while (1) + u64 timeout = get_tmr_ms() + 2000; + while (true) { - int res = _sdmmc_check_mask_interrupt(sdmmc, 0, TEGRA_MMC_NORINTSTS_CMD_COMPLETE); - if (res == SDMMC_MASKINT_MASKED) + int result = _sdmmc_check_mask_interrupt(sdmmc, NULL, SDHCI_INT_RESPONSE); + if (result == SDMMC_MASKINT_MASKED) break; - if (res != SDMMC_MASKINT_NOERROR || get_tmr_ms() > timeout) + if (result != SDMMC_MASKINT_NOERROR || get_tmr_ms() > timeout) { _sdmmc_reset(sdmmc); return 0; @@ -740,45 +784,54 @@ static int _sdmmc_stop_transmission_inner(sdmmc_t *sdmmc, u32 *rsp) { sdmmc_cmd_t cmd; - if (!_sdmmc_wait_prnsts_type0(sdmmc, 0)) + if (!_sdmmc_wait_cmd_data_inhibit(sdmmc, false)) return 0; _sdmmc_enable_interrupts(sdmmc); + cmd.cmd = MMC_STOP_TRANSMISSION; cmd.arg = 0; cmd.rsp_type = SDMMC_RSP_TYPE_1; cmd.check_busy = 1; - _sdmmc_parse_cmdbuf(sdmmc, &cmd, false); - int res = _sdmmc_wait_request(sdmmc); + + _sdmmc_send_cmd(sdmmc, &cmd, false); + + int result = _sdmmc_wait_response(sdmmc); _sdmmc_mask_interrupts(sdmmc); - if (!res) + if (!result) return 0; - + _sdmmc_cache_rsp(sdmmc, rsp, 4, SDMMC_RSP_TYPE_1); - return _sdmmc_wait_prnsts_type1(sdmmc); + + return _sdmmc_wait_card_busy(sdmmc); } int sdmmc_stop_transmission(sdmmc_t *sdmmc, u32 *rsp) { - if (!sdmmc->sd_clock_enabled) + if (!sdmmc->card_clock_enabled) return 0; + // Recalibrate periodically for SDMMC1. + if ((sdmmc->id == SDMMC_1) && sdmmc->auto_cal_enabled) + _sdmmc_autocal_execute(sdmmc, sdmmc_get_io_power(sdmmc)); + bool should_disable_sd_clock = false; - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) { should_disable_sd_clock = true; - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; _sdmmc_get_clkcon(sdmmc); usleep((8000 + sdmmc->divisor - 1) / sdmmc->divisor); } - int res = _sdmmc_stop_transmission_inner(sdmmc, rsp); + int result = _sdmmc_stop_transmission_inner(sdmmc, rsp); usleep((8000 + sdmmc->divisor - 1) / sdmmc->divisor); - if (should_disable_sd_clock) - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; - return res; + if (should_disable_sd_clock) + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; + + return result; } static int _sdmmc_config_dma(sdmmc_t *sdmmc, u32 *blkcnt_out, sdmmc_req_t *req) @@ -799,7 +852,7 @@ static int _sdmmc_config_dma(sdmmc_t *sdmmc, u32 *blkcnt_out, sdmmc_req_t *req) sdmmc->last_dma_idx = dma_idx; } - //Check alignment. + // Check alignment. if (admaaddr & 7) return 0; @@ -814,15 +867,13 @@ static int _sdmmc_config_dma(sdmmc_t *sdmmc, u32 *blkcnt_out, sdmmc_req_t *req) if (blkcnt_out) *blkcnt_out = blkcnt; - u32 trnmode = TEGRA_MMC_TRNMOD_DMA_ENABLE; + u32 trnmode = SDHCI_TRNS_DMA; if (req->is_multi_block) - trnmode = TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT | - TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE | - TEGRA_MMC_TRNMOD_DMA_ENABLE; + trnmode = SDHCI_TRNS_MULTI | SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_DMA; if (!req->is_write) - trnmode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ; + trnmode |= SDHCI_TRNS_READ; if (req->is_auto_cmd12) - trnmode = (trnmode & 0xFFF3) | TEGRA_MMC_TRNMOD_AUTO_CMD12; + trnmode = (trnmode & ~(SDHCI_TRNS_AUTO_CMD12 | SDHCI_TRNS_AUTO_CMD23)) | SDHCI_TRNS_AUTO_CMD12; sdmmc->regs->trnmod = trnmode; @@ -835,28 +886,30 @@ static int _sdmmc_update_dma(sdmmc_t *sdmmc) do { blkcnt = sdmmc->regs->blkcnt; - u32 timeout = get_tmr_ms() + 1500; + u64 timeout = get_tmr_ms() + 1500; do { - int res = 0; - while (1) + int result = 0; + while (true) { u16 intr = 0; - res = _sdmmc_check_mask_interrupt(sdmmc, &intr, - TEGRA_MMC_NORINTSTS_XFER_COMPLETE | TEGRA_MMC_NORINTSTS_DMA_INTERRUPT); - if (res < 0) + result = _sdmmc_check_mask_interrupt(sdmmc, &intr, + SDHCI_INT_DATA_END | SDHCI_INT_DMA_END); + if (result < 0) break; - if (intr & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) - return 1; //Transfer complete. - if (intr & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) + + if (intr & SDHCI_INT_DATA_END) + return 1; // Transfer complete. + + if (intr & SDHCI_INT_DMA_END) { - //Update DMA. + // Update DMA. sdmmc->regs->admaaddr = sdmmc->dma_addr_next & 0xFFFFFFFFF; sdmmc->regs->admaaddr_hi = (sdmmc->dma_addr_next >> 32) & 0xFFFFFFFFF; sdmmc->dma_addr_next += 0x80000; } } - if (res != SDMMC_MASKINT_NOERROR) + if (result != SDMMC_MASKINT_NOERROR) { _sdmmc_reset(sdmmc); return 0; @@ -871,14 +924,16 @@ static int _sdmmc_update_dma(sdmmc_t *sdmmc) static int _sdmmc_execute_cmd_inner(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_t *req, u32 *blkcnt_out) { int has_req_or_check_busy = req || cmd->check_busy; - if (!_sdmmc_wait_prnsts_type0(sdmmc, has_req_or_check_busy)) + if (!_sdmmc_wait_cmd_data_inhibit(sdmmc, has_req_or_check_busy)) return 0; u32 blkcnt = 0; bool is_data_present = false; if (req) { - _sdmmc_config_dma(sdmmc, &blkcnt, req); + if (!_sdmmc_config_dma(sdmmc, &blkcnt, req)) + return 0; + if(!sdmmc_memcpy_buf) { // Flush from/to phys @@ -896,41 +951,40 @@ static int _sdmmc_execute_cmd_inner(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_ } } - _sdmmc_enable_interrupts(sdmmc); is_data_present = true; } else - { - _sdmmc_enable_interrupts(sdmmc); is_data_present = false; - } - _sdmmc_parse_cmdbuf(sdmmc, cmd, is_data_present); + _sdmmc_enable_interrupts(sdmmc); - int res = _sdmmc_wait_request(sdmmc); - DPRINTF("rsp(%d): %08X, %08X, %08X, %08X\n", res, + if (!_sdmmc_send_cmd(sdmmc, cmd, is_data_present)) + return 0; + + int result = _sdmmc_wait_response(sdmmc); + DPRINTF("rsp(%d): %08X, %08X, %08X, %08X\n", result, sdmmc->regs->rspreg0, sdmmc->regs->rspreg1, sdmmc->regs->rspreg2, sdmmc->regs->rspreg3); - if (res) + if (result) { if (cmd->rsp_type) { sdmmc->expected_rsp_type = cmd->rsp_type; - _sdmmc_cache_rsp(sdmmc, sdmmc->rsp, 0x10, cmd->rsp_type); - + result = _sdmmc_cache_rsp(sdmmc, sdmmc->rsp, 0x10, cmd->rsp_type); + /*if(sdmmc->rsp[0] & 0xFDF9A080) { res = 0; sdmmc->rsp[0] = 0; // Reset error }*/ } - - if (res && req) - _sdmmc_update_dma(sdmmc); + + if (req && result) + result = _sdmmc_update_dma(sdmmc); } _sdmmc_mask_interrupts(sdmmc); - if (res) + if (result) { if (req) { @@ -959,34 +1013,46 @@ static int _sdmmc_execute_cmd_inner(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_ } if (cmd->check_busy || req) - return _sdmmc_wait_prnsts_type1(sdmmc); + return _sdmmc_wait_card_busy(sdmmc); } - return res; + return result; +} + +int sdmmc_get_sd_power_enabled() +{ + return gpio_read(GPIO_PORT_E, GPIO_PIN_4); +} + +bool sdmmc_get_sd_inserted() +{ + return (!gpio_read(GPIO_PORT_Z, GPIO_PIN_1)); } static int _sdmmc_config_sdmmc1() { - //Configure SD card detect. - PINMUX_AUX(PINMUX_AUX_GPIO_PZ1) = PINMUX_INPUT_ENABLE | PINMUX_PULL_UP | 1; //GPIO control, pull up. + // Configure SD card detect. + PINMUX_AUX(PINMUX_AUX_GPIO_PZ1) = PINMUX_INPUT_ENABLE | PINMUX_PULL_UP | 2; // GPIO control, pull up. APB_MISC(APB_MISC_GP_VGPIO_GPIO_MUX_SEL) = 0; gpio_config(GPIO_PORT_Z, GPIO_PIN_1, GPIO_MODE_GPIO); gpio_output_enable(GPIO_PORT_Z, GPIO_PIN_1, GPIO_OUTPUT_DISABLE); usleep(100); - if(!!gpio_read(GPIO_PORT_Z, GPIO_PIN_1)) + + // Check if SD card is inserted. + if(!sdmmc_get_sd_inserted()) return 0; /* * Pinmux config: * DRV_TYPE = DRIVE_2X - * E_SCHMT = ENABLE (for 1.8V), DISABLE (for 3.3V) - * E_INPUT = ENABLE + * E_SCHMT = ENABLE (for 1.8V), DISABLE (for 3.3V) + * E_INPUT = ENABLE * TRISTATE = PASSTHROUGH * APB_MISC_GP_SDMMCx_CLK_LPBK_CONTROL = SDMMCx_CLK_PAD_E_LPBK for CLK */ - //Configure SDMMC1 pinmux. - APB_MISC(APB_MISC_GP_SDMMC1_CLK_LPBK_CONTROL) = 1; + // Configure SDMMC1 pinmux. + APB_MISC(APB_MISC_GP_SDMMC1_CLK_LPBK_CONTROL) = 1; // Enable deep loopback for SDMMC1 CLK pad. PINMUX_AUX(PINMUX_AUX_SDMMC1_CLK) = PINMUX_DRIVE_2X | PINMUX_INPUT_ENABLE | PINMUX_PARKED; PINMUX_AUX(PINMUX_AUX_SDMMC1_CMD) = PINMUX_DRIVE_2X | PINMUX_INPUT_ENABLE | PINMUX_PARKED | PINMUX_PULL_UP; PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT3) = PINMUX_DRIVE_2X | PINMUX_INPUT_ENABLE | PINMUX_PARKED | PINMUX_PULL_UP; @@ -994,51 +1060,81 @@ static int _sdmmc_config_sdmmc1() PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT1) = PINMUX_DRIVE_2X | PINMUX_INPUT_ENABLE | PINMUX_PARKED | PINMUX_PULL_UP; PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT0) = PINMUX_DRIVE_2X | PINMUX_INPUT_ENABLE | PINMUX_PARKED | PINMUX_PULL_UP; - //Make sure SDMMC1 controller is reset. - smcReadWriteRegister(PMC_BASE + APBDEV_PMC_NO_IOPOWER, (1 << 12), (1 << 12)); + // Make sure the SDMMC1 controller is powered. + smcReadWriteRegister(PMC_BASE + APBDEV_PMC_NO_IOPOWER, PMC_NO_IOPOWER_SDMMC1_IO_EN, PMC_NO_IOPOWER_SDMMC1_IO_EN); usleep(1000); + smcReadWriteRegister(PMC_BASE + APBDEV_PMC_NO_IOPOWER, ~PMC_NO_IOPOWER_SDMMC1_IO_EN, PMC_NO_IOPOWER_SDMMC1_IO_EN); - //Make sure the SDMMC1 controller is powered. - smcReadWriteRegister(PMC_BASE + APBDEV_PMC_NO_IOPOWER, ~(1 << 12), (1 << 12)); - smcReadWriteRegister(PMC_BASE + APBDEV_PMC_PWR_DET_VAL, (1 << 12), (1 << 12)); + // Inform IO pads that voltage is gonna be 3.3V. + smcReadWriteRegister(PMC_BASE + APBDEV_PMC_PWR_DET_VAL, PMC_PWR_DET_SDMMC1_IO_EN, PMC_PWR_DET_SDMMC1_IO_EN); - //Set enable SD card power. - PINMUX_AUX(PINMUX_AUX_DMIC3_CLK) = PINMUX_INPUT_ENABLE | PINMUX_PULL_DOWN | 1; //GPIO control, pull down. + // Set enable SD card power. + //PINMUX_AUX(PINMUX_AUX_DMIC3_CLK) = PINMUX_PULL_DOWN | 2; // Pull down. + PINMUX_AUX(PINMUX_AUX_DMIC3_CLK) = PINMUX_INPUT_ENABLE | PINMUX_PULL_DOWN | 1; // GPIO control, pull down. gpio_config(GPIO_PORT_E, GPIO_PIN_4, GPIO_MODE_GPIO); gpio_write(GPIO_PORT_E, GPIO_PIN_4, GPIO_HIGH); gpio_output_enable(GPIO_PORT_E, GPIO_PIN_4, GPIO_OUTPUT_ENABLE); - usleep(1000); - //Enable SD card power. + // Enable SD card power. max77620_regulator_set_voltage(REGULATOR_LDO2, 3300000); max77620_regulator_enable(REGULATOR_LDO2, 1); - usleep(1000); - //For good measure. - APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = 0x10000000; - + // Set pad slew codes to get good quality clock. + APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) = (APB_MISC(APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL) & 0xFFFFFFF) | 0x50000000; usleep(1000); return 1; } -int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type, int no_sd) +static void _sdmmc_config_emmc(u32 id) { + switch (id) + { + case SDMMC_2: + // Unset park for pads. + APB_MISC(APB_MISC_GP_EMMC2_PAD_CFGPADCTRL) &= 0xF8003FFF; + break; + case SDMMC_4: + // Unset park for pads. + APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) &= 0xF8003FFF; + // Set default pad cfg. + APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) = (APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) & 0xFFFFC003) | 0x1040; + + // Enabled schmitt trigger. + APB_MISC(APB_MISC_GP_EMMC4_PAD_CFGPADCTRL) |= 1; // Enable Schmitt trigger. + break; + } + +} + +int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type, int auto_cal_enable) +{ + const u32 trim_values[] = { 2, 8, 3, 8 }; + if (id > SDMMC_4) return 0; - if (id == SDMMC_1) - if (!_sdmmc_config_sdmmc1()) - return 0; - memset(sdmmc, 0, sizeof(sdmmc_t)); sdmmc->regs = (t210_sdmmc_t *)QueryIoMapping(_sdmmc_bases[id], 0x200); sdmmc->id = id; sdmmc->clock_stopped = 1; + // Do specific SDMMC HW configuration. + switch (id) + { + case SDMMC_1: + if (!_sdmmc_config_sdmmc1()) + return 0; + break; + case SDMMC_2: + case SDMMC_4: + _sdmmc_config_emmc(id); + break; + } + if (clock_sdmmc_is_not_reset_and_enabled(id)) { _sdmmc_sd_clock_disable(sdmmc); @@ -1047,31 +1143,37 @@ int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type, int n u32 clock; u16 divisor; - clock_sdmmc_get_params(&clock, &divisor, type); + clock_sdmmc_get_card_clock_div(&clock, &divisor, type); clock_sdmmc_enable(id, clock); sdmmc->clock_stopped = 0; //TODO: make this skip-able. - sdmmc->regs->iospare |= 0x80000; - sdmmc->regs->veniotrimctl &= 0xFFFFFFFB; - static const u32 trim_values[] = { 2, 8, 3, 8 }; - sdmmc->regs->venclkctl = (sdmmc->regs->venclkctl & 0xE0FFFFFF) | (trim_values[sdmmc->id] << 24); - sdmmc->regs->sdmemcmppadctl = (sdmmc->regs->sdmemcmppadctl & 0xFFFFFFF0) | 7; + sdmmc->regs->iospare |= 0x80000; // Enable muxing. + sdmmc->regs->veniotrimctl &= 0xFFFFFFFB; // Set Band Gap VREG to supply DLL. + sdmmc->regs->venclkctl = (sdmmc->regs->venclkctl & 0xE0FFFFFB) | (trim_values[sdmmc->id] << 24); + sdmmc->regs->sdmemcmppadctl = + (sdmmc->regs->sdmemcmppadctl & TEGRA_MMC_SDMEMCOMPPADCTRL_COMP_VREF_SEL_MASK) | 7; + if (!_sdmmc_autocal_config_offset(sdmmc, power)) return 0; + _sdmmc_autocal_execute(sdmmc, power); + if (_sdmmc_enable_internal_clock(sdmmc)) { sdmmc_set_bus_width(sdmmc, bus_width); - _sdmmc_set_voltage(sdmmc, power); + _sdmmc_set_io_power(sdmmc, power); + if (sdmmc_setup_clock(sdmmc, type)) { - sdmmc_sd_clock_ctrl(sdmmc, no_sd); - _sdmmc_sd_clock_enable(sdmmc); + sdmmc_card_clock_ctrl(sdmmc, auto_cal_enable); + _sdmmc_card_clock_enable(sdmmc); _sdmmc_get_clkcon(sdmmc); + return 1; } + return 0; } return 0; @@ -1082,16 +1184,19 @@ void sdmmc_end(sdmmc_t *sdmmc) if (!sdmmc->clock_stopped) { _sdmmc_sd_clock_disable(sdmmc); - // Disable SDMMC power. - _sdmmc_set_voltage(sdmmc, SDMMC_POWER_OFF); + // Disable SDMMC power. + _sdmmc_set_io_power(sdmmc, SDMMC_POWER_OFF); // Disable SD card power. if (sdmmc->id == SDMMC_1) { gpio_output_enable(GPIO_PORT_E, GPIO_PIN_4, GPIO_OUTPUT_DISABLE); - msleep(1); // To power cycle min 1ms without power is needed. max77620_regulator_enable(REGULATOR_LDO2, 0); - msleep(100); // Some extra. + + // Inform IO pads that next voltage might be 3.3V. + smcReadWriteRegister(PMC_BASE + APBDEV_PMC_PWR_DET_VAL, PMC_PWR_DET_SDMMC1_IO_EN, PMC_PWR_DET_SDMMC1_IO_EN); + + msleep(100); // To power cycle min 1ms without power is needed. } _sdmmc_get_clkcon(sdmmc); @@ -1110,28 +1215,29 @@ void sdmmc_init_cmd(sdmmc_cmd_t *cmdbuf, u16 cmd, u32 arg, u32 rsp_type, u32 che int sdmmc_execute_cmd(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_t *req, u32 *blkcnt_out) { - if (!sdmmc->sd_clock_enabled) + if (!sdmmc->card_clock_enabled) return 0; - //Recalibrate periodically for SDMMC1. - if (sdmmc->id == SDMMC_1 && sdmmc->no_sd) - _sdmmc_autocal_execute(sdmmc, sdmmc_get_voltage(sdmmc)); + // Recalibrate periodically for SDMMC1. + if (sdmmc->id == SDMMC_1 && sdmmc->auto_cal_enabled) + _sdmmc_autocal_execute(sdmmc, sdmmc_get_io_power(sdmmc)); int should_disable_sd_clock = 0; - if (!(sdmmc->regs->clkcon & TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE)) + if (!(sdmmc->regs->clkcon & SDHCI_CLOCK_CARD_EN)) { should_disable_sd_clock = 1; - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; _sdmmc_get_clkcon(sdmmc); usleep((8000 + sdmmc->divisor - 1) / sdmmc->divisor); } - int res = _sdmmc_execute_cmd_inner(sdmmc, cmd, req, blkcnt_out); + int result = _sdmmc_execute_cmd_inner(sdmmc, cmd, req, blkcnt_out); usleep((8000 + sdmmc->divisor - 1) / sdmmc->divisor); - if (should_disable_sd_clock) - sdmmc->regs->clkcon &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; - return res; + if (should_disable_sd_clock) + sdmmc->regs->clkcon &= ~SDHCI_CLOCK_CARD_EN; + + return result; } int sdmmc_enable_low_voltage(sdmmc_t *sdmmc) @@ -1139,25 +1245,38 @@ int sdmmc_enable_low_voltage(sdmmc_t *sdmmc) if(sdmmc->id != SDMMC_1) return 0; - if (!sdmmc_setup_clock(sdmmc, 8)) + if (!sdmmc_setup_clock(sdmmc, SDHCI_TIMING_UHS_SDR12)) return 0; _sdmmc_get_clkcon(sdmmc); + // Switch to 1.8V and wait for regulator to stabilize. Assume max possible wait needed. max77620_regulator_set_voltage(REGULATOR_LDO2, 1800000); - smcReadWriteRegister(PMC_BASE + APBDEV_PMC_PWR_DET_VAL, ~(1 << 12), (1 << 12)); + usleep(300); + + // Inform IO pads that we switched to 1.8V. + smcReadWriteRegister(PMC_BASE + APBDEV_PMC_PWR_DET_VAL, ~PMC_PWR_DET_SDMMC1_IO_EN, PMC_PWR_DET_SDMMC1_IO_EN); + + // Enable schmitt trigger for better duty cycle and low jitter clock. + PINMUX_AUX(PINMUX_AUX_SDMMC1_CLK) |= PINMUX_SCHMT; + PINMUX_AUX(PINMUX_AUX_SDMMC1_CMD) |= PINMUX_SCHMT; + PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT3) |= PINMUX_SCHMT; + PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT2) |= PINMUX_SCHMT; + PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT1) |= PINMUX_SCHMT; + PINMUX_AUX(PINMUX_AUX_SDMMC1_DAT0) |= PINMUX_SCHMT; _sdmmc_autocal_config_offset(sdmmc, SDMMC_POWER_1_8); _sdmmc_autocal_execute(sdmmc, SDMMC_POWER_1_8); - _sdmmc_set_voltage(sdmmc, SDMMC_POWER_1_8); + _sdmmc_set_io_power(sdmmc, SDMMC_POWER_1_8); _sdmmc_get_clkcon(sdmmc); - msleep(5); - + msleep(5); // Wait minimum 5ms before turning on the card clock. + + // Turn on SDCLK. if (sdmmc->regs->hostctl2 & SDHCI_CTRL_VDD_180) { - sdmmc->regs->clkcon |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; + sdmmc->regs->clkcon |= SDHCI_CLOCK_CARD_EN; _sdmmc_get_clkcon(sdmmc); - msleep(1); + usleep(1000); if ((sdmmc->regs->prnsts & 0xF00000) == 0xF00000) return 1; } diff --git a/emummc/source/emmc/sdmmc_driver.h b/emummc/source/emmc/sdmmc_driver.h index d5c36d7c5..a19602528 100644 --- a/emummc/source/emmc/sdmmc_driver.h +++ b/emummc/source/emmc/sdmmc_driver.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -49,24 +50,158 @@ #define SDMMC_MASKINT_NOERROR -1 #define SDMMC_MASKINT_ERROR -2 -/*! SDMMC host control 2 */ -#define SDHCI_CTRL_UHS_MASK 0xFFF8 -#define SDHCI_CTRL_VDD_330 0xFFF7 -#define SDHCI_CTRL_VDD_180 8 -#define SDHCI_CTRL_EXEC_TUNING 0x40 -#define SDHCI_CTRL_TUNED_CLK 0x80 -#define SDHCI_HOST_VERSION_4_EN 0x1000 -#define SDHCI_ADDRESSING_64BIT_EN 0x2000 -#define SDHCI_CTRL_PRESET_VAL_EN 0x8000 +/*! SDMMC present state. */ +#define SDHCI_CMD_INHIBIT 0x1 +#define SDHCI_DATA_INHIBIT 0x2 +#define SDHCI_DOING_WRITE 0x100 +#define SDHCI_DOING_READ 0x200 +#define SDHCI_SPACE_AVAILABLE 0x400 +#define SDHCI_DATA_AVAILABLE 0x800 +#define SDHCI_CARD_PRESENT 0x10000 +#define SDHCI_CD_STABLE 0x20000 +#define SDHCI_CD_LVL 0x40000 +#define SDHCI_WRITE_PROTECT 0x80000 +#define SDHCI_DATA_LVL_MASK 0xF00000 +#define SDHCI_DATA_0_LVL_MASK 0x100000 +#define SDHCI_CMD_LVL 0x1000000 + +/*! SDMMC transfer mode. */ +#define SDHCI_TRNS_DMA 0x01 +#define SDHCI_TRNS_BLK_CNT_EN 0x02 +#define SDHCI_TRNS_AUTO_CMD12 0x04 +#define SDHCI_TRNS_AUTO_CMD23 0x08 +#define SDHCI_TRNS_AUTO_SEL 0x0C +#define SDHCI_TRNS_WRITE 0x00 +#define SDHCI_TRNS_READ 0x10 +#define SDHCI_TRNS_MULTI 0x20 + +/*! SDMMC command. */ +#define SDHCI_CMD_RESP_MASK 0x3 +#define SDHCI_CMD_RESP_NO_RESP 0x0 +#define SDHCI_CMD_RESP_LEN136 0x1 +#define SDHCI_CMD_RESP_LEN48 0x2 +#define SDHCI_CMD_RESP_LEN48_BUSY 0x3 +#define SDHCI_CMD_CRC 0x08 +#define SDHCI_CMD_INDEX 0x10 +#define SDHCI_CMD_DATA 0x20 +#define SDHCI_CMD_ABORTCMD 0xC0 + +/*! SDMMC host control. */ +#define SDHCI_CTRL_LED 0x01 +#define SDHCI_CTRL_4BITBUS 0x02 +#define SDHCI_CTRL_HISPD 0x04 +#define SDHCI_CTRL_DMA_MASK 0x18 +#define SDHCI_CTRL_SDMA 0x00 +#define SDHCI_CTRL_ADMA1 0x08 +#define SDHCI_CTRL_ADMA32 0x10 +#define SDHCI_CTRL_ADMA64 0x18 +#define SDHCI_CTRL_8BITBUS 0x20 +#define SDHCI_CTRL_CDTEST_INS 0x40 +#define SDHCI_CTRL_CDTEST_EN 0x80 + +/*! SDMMC host control 2. */ +#define SDHCI_CTRL_UHS_MASK 0xFFF8 +#define SDHCI_CTRL_VDD_180 8 +#define SDHCI_CTRL_DRV_TYPE_B 0x00 +#define SDHCI_CTRL_DRV_TYPE_A 0x10 +#define SDHCI_CTRL_DRV_TYPE_C 0x20 +#define SDHCI_CTRL_DRV_TYPE_D 0x30 +#define SDHCI_CTRL_EXEC_TUNING 0x40 +#define SDHCI_CTRL_TUNED_CLK 0x80 +#define SDHCI_HOST_VERSION_4_EN 0x1000 +#define SDHCI_ADDRESSING_64BIT_EN 0x2000 +#define SDHCI_CTRL_PRESET_VAL_EN 0x8000 + +/*! SDMMC power control. */ +#define SDHCI_POWER_ON 0x01 +#define SDHCI_POWER_180 0x0A +#define SDHCI_POWER_300 0x0C +#define SDHCI_POWER_330 0x0E +#define SDHCI_POWER_MASK 0xF1 + +// /*! SDMMC max current. */ +// #define SDHCI_MAX_CURRENT_330_MASK 0xFF +// #define SDHCI_MAX_CURRENT_180_MASK 0xFF0000 +// #define SDHCI_MAX_CURRENT_MULTIPLIER 4 + +/*! SDMMC clock control. */ +#define SDHCI_DIVIDER_SHIFT 8 +#define SDHCI_DIVIDER_HI_SHIFT 6 +#define SDHCI_DIV_MASK 0xFF00 +#define SDHCI_DIV_HI_MASK 0xC0 +#define SDHCI_PROG_CLOCK_MODE 0x20 +#define SDHCI_CLOCK_CARD_EN 0x4 +#define SDHCI_CLOCK_INT_STABLE 0x2 +#define SDHCI_CLOCK_INT_EN 0x1 + +/*! SDMMC software reset. */ +#define SDHCI_RESET_ALL 0x01 +#define SDHCI_RESET_CMD 0x02 +#define SDHCI_RESET_DATA 0x04 + +/*! SDMMC interrupt status and control. */ +#define SDHCI_INT_RESPONSE 0x1 +#define SDHCI_INT_DATA_END 0x2 +#define SDHCI_INT_BLK_GAP 0x4 +#define SDHCI_INT_DMA_END 0x8 +#define SDHCI_INT_SPACE_AVAIL 0x10 +#define SDHCI_INT_DATA_AVAIL 0x20 +#define SDHCI_INT_CARD_INSERT 0x40 +#define SDHCI_INT_CARD_REMOVE 0x80 +#define SDHCI_INT_CARD_INT 0x100 +#define SDHCI_INT_RETUNE 0x1000 +#define SDHCI_INT_CQE 0x4000 +#define SDHCI_INT_ERROR 0x8000 + +/*! SDMMC error interrupt status and control. */ +#define SDHCI_ERR_INT_TIMEOUT 0x1 +#define SDHCI_ERR_INT_CRC 0x2 +#define SDHCI_ERR_INT_END_BIT 0x4 +#define SDHCI_ERR_INT_INDEX 0x8 +#define SDHCI_ERR_INT_DATA_TIMEOUT 0x10 +#define SDHCI_ERR_INT_DATA_CRC 0x20 +#define SDHCI_ERR_INT_DATA_END_BIT 0x40 +#define SDHCI_ERR_INT_BUS_POWER 0x80 +#define SDHCI_ERR_INT_AUTO_CMD_ERR 0x100 +#define SDHCI_ERR_INT_ADMA_ERROR 0x200 + +#define SDHCI_ERR_INT_ALL_EXCEPT_ADMA_BUSPWR \ + (SDHCI_ERR_INT_AUTO_CMD_ERR | SDHCI_ERR_INT_DATA_END_BIT | \ + SDHCI_ERR_INT_DATA_CRC | SDHCI_ERR_INT_DATA_TIMEOUT | \ + SDHCI_ERR_INT_INDEX | SDHCI_ERR_INT_END_BIT | \ + SDHCI_ERR_INT_CRC | SDHCI_ERR_INT_TIMEOUT) /*! SD bus speeds. */ -#define UHS_SDR12_BUS_SPEED 0 -#define HIGH_SPEED_BUS_SPEED 1 -#define UHS_SDR25_BUS_SPEED 1 -#define UHS_SDR50_BUS_SPEED 2 -#define UHS_SDR104_BUS_SPEED 3 -#define UHS_DDR50_BUS_SPEED 4 -#define HS400_BUS_SPEED 5 +#define UHS_SDR12_BUS_SPEED 0 +#define HIGH_SPEED_BUS_SPEED 1 +#define UHS_SDR25_BUS_SPEED 1 +#define UHS_SDR50_BUS_SPEED 2 +#define UHS_SDR104_BUS_SPEED 3 +#define UHS_DDR50_BUS_SPEED 4 +#define HS400_BUS_SPEED 5 + +/*! SDMMC timmings. */ +#define SDHCI_TIMING_MMC_ID 0 +#define SDHCI_TIMING_MMC_LS26 1 +#define SDHCI_TIMING_MMC_HS52 2 +#define SDHCI_TIMING_MMC_HS200 3 +#define SDHCI_TIMING_MMC_HS400 4 +#define SDHCI_TIMING_SD_ID 5 +#define SDHCI_TIMING_SD_DS12 6 +#define SDHCI_TIMING_SD_HS25 7 +#define SDHCI_TIMING_UHS_SDR12 8 +#define SDHCI_TIMING_UHS_SDR25 9 +#define SDHCI_TIMING_UHS_SDR50 10 +#define SDHCI_TIMING_UHS_SDR104 11 +#define SDHCI_TIMING_UHS_SDR82 12 // SDR104 with a 163.2MHz -> 81.6MHz clock. +#define SDHCI_TIMING_UHS_DDR50 13 +#define SDHCI_TIMING_MMC_DDR52 14 + +#define SDHCI_CAN_64BIT 0x10000000 + +/*! SDMMC Low power features. */ +#define SDMMC_AUTO_CAL_DISABLE 0 +#define SDMMC_AUTO_CAL_ENABLE 1 /*! Helper for SWITCH command argument. */ #define SDMMC_SWITCH(mode, index, value) (((mode) << 24) | ((index) << 16) | ((value) << 8)) @@ -78,8 +213,8 @@ typedef struct _sdmmc_t u32 id; u32 divisor; u32 clock_stopped; - int no_sd; - int sd_clock_enabled; + int auto_cal_enabled; + int card_clock_enabled; int venclkctl_set; u32 venclkctl_tap; u32 expected_rsp_type; @@ -109,19 +244,21 @@ typedef struct _sdmmc_req_t int is_auto_cmd12; } sdmmc_req_t; -int sdmmc_get_voltage(sdmmc_t *sdmmc); -u32 sdmmc_get_bus_width(sdmmc_t *sdmmc); +int sdmmc_get_io_power(sdmmc_t *sdmmc); +u32 sdmmc_get_bus_width(sdmmc_t *sdmmc); void sdmmc_set_bus_width(sdmmc_t *sdmmc, u32 bus_width); -void sdmmc_get_venclkctl(sdmmc_t *sdmmc); -int sdmmc_setup_clock(sdmmc_t *sdmmc, u32 type); -void sdmmc_sd_clock_ctrl(sdmmc_t *sdmmc, int no_sd); -int sdmmc_get_rsp(sdmmc_t *sdmmc, u32 *rsp, u32 size, u32 type); -int sdmmc_config_tuning(sdmmc_t *sdmmc, u32 type, u32 cmd); -int sdmmc_stop_transmission(sdmmc_t *sdmmc, u32 *rsp); -int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type, int no_sd); +void sdmmc_set_tap_value(sdmmc_t *sdmmc); +int sdmmc_setup_clock(sdmmc_t *sdmmc, u32 type); +void sdmmc_card_clock_ctrl(sdmmc_t *sdmmc, int auto_cal_enable); +int sdmmc_get_rsp(sdmmc_t *sdmmc, u32 *rsp, u32 size, u32 type); +int sdmmc_tuning_execute(sdmmc_t *sdmmc, u32 type, u32 cmd); +int sdmmc_stop_transmission(sdmmc_t *sdmmc, u32 *rsp); +int sdmmc_get_sd_power_enabled(); +bool sdmmc_get_sd_inserted(); +int sdmmc_init(sdmmc_t *sdmmc, u32 id, u32 power, u32 bus_width, u32 type, int auto_cal_enable); void sdmmc_end(sdmmc_t *sdmmc); void sdmmc_init_cmd(sdmmc_cmd_t *cmdbuf, u16 cmd, u32 arg, u32 rsp_type, u32 check_busy); -int sdmmc_execute_cmd(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_t *req, u32 *blkcnt_out); -int sdmmc_enable_low_voltage(sdmmc_t *sdmmc); +int sdmmc_execute_cmd(sdmmc_t *sdmmc, sdmmc_cmd_t *cmd, sdmmc_req_t *req, u32 *blkcnt_out); +int sdmmc_enable_low_voltage(sdmmc_t *sdmmc); #endif diff --git a/emummc/source/emmc/sdmmc_t210.h b/emummc/source/emmc/sdmmc_t210.h index e11c3ff9c..eb17714ce 100644 --- a/emummc/source/emmc/sdmmc_t210.h +++ b/emummc/source/emmc/sdmmc_t210.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, @@ -19,49 +20,14 @@ #include "../utils/types.h" -#define TEGRA_MMC_PWRCTL_SD_BUS_POWER 0x1 -#define TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8 0xA -#define TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0 0xC -#define TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3 0xE -#define TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_MASK 0xF1 - -#define TEGRA_MMC_HOSTCTL_1BIT 0x00 -#define TEGRA_MMC_HOSTCTL_4BIT 0x02 -#define TEGRA_MMC_HOSTCTL_8BIT 0x20 - -#define TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE 0x1 -#define TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE 0x2 -#define TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE 0x4 -#define TEGRA_MMC_CLKCON_CLKGEN_SELECT 0x20 - -#define TEGRA_MMC_SWRST_SW_RESET_FOR_ALL 0x1 -#define TEGRA_MMC_SWRST_SW_RESET_FOR_CMD_LINE 0x2 -#define TEGRA_MMC_SWRST_SW_RESET_FOR_DAT_LINE 0x4 - -#define TEGRA_MMC_TRNMOD_DMA_ENABLE 0x1 -#define TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE 0x2 -#define TEGRA_MMC_TRNMOD_AUTO_CMD12 0x4 -#define TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_WRITE 0x0 -#define TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ 0x10 -#define TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT 0x20 - -#define TEGRA_MMC_TRNMOD_CMD_CRC_CHECK 0x8 -#define TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK 0x10 -#define TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER 0x20 - -#define TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_MASK 0x3 -#define TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE 0x0 -#define TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136 0x1 -#define TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48 0x2 -#define TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY 0x3 - -#define TEGRA_MMC_NORINTSTS_CMD_COMPLETE 0x1 -#define TEGRA_MMC_NORINTSTS_XFER_COMPLETE 0x2 -#define TEGRA_MMC_NORINTSTS_DMA_INTERRUPT 0x8 -#define TEGRA_MMC_NORINTSTS_ERR_INTERRUPT 0x8000 -#define TEGRA_MMC_NORINTSTS_CMD_TIMEOUT 0x10000 - -#define TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY 0x20 +#define TEGRA_MMC_VNDR_TUN_CTRL0_TAP_VAL_UPDATED_BY_HW 0x20000 +#define TEGRA_MMC_DLLCAL_CFG_EN_CALIBRATE 0x80000000 +#define TEGRA_MMC_DLLCAL_CFG_STATUS_DLL_ACTIVE 0x80000000 +#define TEGRA_MMC_SDMEMCOMPPADCTRL_PAD_E_INPUT_PWRD 0x80000000 +#define TEGRA_MMC_SDMEMCOMPPADCTRL_COMP_VREF_SEL_MASK 0xFFFFFFF0 +#define TEGRA_MMC_AUTOCALCFG_AUTO_CAL_ENABLE 0x20000000 +#define TEGRA_MMC_AUTOCALCFG_AUTO_CAL_START 0x80000000 +#define TEGRA_MMC_AUTOCALSTS_AUTO_CAL_ACTIVE 0x80000000 typedef struct _t210_sdmmc_t { @@ -77,56 +43,66 @@ typedef struct _t210_sdmmc_t vu32 rspreg3; vu32 bdata; vu32 prnsts; - vu8 hostctl; - vu8 pwrcon; - vu8 blkgap; - vu8 wakcon; + vu8 hostctl; + vu8 pwrcon; + vu8 blkgap; + vu8 wakcon; vu16 clkcon; - vu8 timeoutcon; - vu8 swrst; + vu8 timeoutcon; + vu8 swrst; vu16 norintsts; vu16 errintsts; - vu16 norintstsen; - vu16 errintstsen; - vu16 norintsigen; - vu16 errintsigen; + vu16 norintstsen; // Enable irq status. + vu16 errintstsen; // Enable irq status. + vu16 norintsigen; // Enable irq signal to LIC/GIC. + vu16 errintsigen; // Enable irq signal to LIC/GIC. vu16 acmd12errsts; vu16 hostctl2; vu32 capareg; vu32 capareg_1; vu32 maxcurr; - vu8 res3[4]; + vu8 rsvd0[4]; // 4C-4F reserved for more max current. vu16 setacmd12err; vu16 setinterr; - vu8 admaerr; - vu8 res4[3]; + vu8 admaerr; + vu8 rsvd1[3]; // 55-57 reserved. vu32 admaaddr; vu32 admaaddr_hi; - vu8 res5[156]; - vu16 slotintstatus; + vu8 rsvd2[156]; // 60-FB reserved. + vu16 slotintsts; vu16 hcver; vu32 venclkctl; - vu32 venspictl; - vu32 venspiintsts; - vu32 venceatactl; + vu32 vensysswctl; + vu32 venerrintsts; + vu32 vencapover; vu32 venbootctl; vu32 venbootacktout; vu32 venbootdattout; vu32 vendebouncecnt; vu32 venmiscctl; - vu32 res6[34]; + vu32 maxcurrover; + vu32 maxcurrover_hi; + vu32 unk0[32]; // 0x12C vu32 veniotrimctl; - vu32 vendllcal; - vu8 res7[8]; - vu32 dllcfgstatus; + vu32 vendllcalcfg; + vu32 vendllctl0; + vu32 vendllctl1; + vu32 vendllcalcfgsts; vu32 ventunctl0; - vu32 field_1C4; - vu8 field_1C8[24]; + vu32 ventunctl1; + vu32 ventunsts0; + vu32 ventunsts1; + vu32 venclkgatehystcnt; + vu32 venpresetval0; + vu32 venpresetval1; + vu32 venpresetval2; vu32 sdmemcmppadctl; vu32 autocalcfg; vu32 autocalintval; vu32 autocalsts; vu32 iospare; + vu32 mcciffifoctl; + vu32 timeoutwcoal; } t210_sdmmc_t; #endif diff --git a/emummc/source/emuMMC/emummc.c b/emummc/source/emuMMC/emummc.c index ac4050a4e..237ebf116 100644 --- a/emummc/source/emuMMC/emummc.c +++ b/emummc/source/emuMMC/emummc.c @@ -18,14 +18,12 @@ #include -#include "../soc/gpio.h" -#include "../utils/fatal.h" -#include "../libs/fatfs/diskio.h" #include "emummc.h" #include "emummc_ctx.h" +#include "../utils/fatal.h" +#include "../libs/fatfs/diskio.h" static bool sdmmc_first_init = false; -static bool storageMMCinitialized = false; static bool storageSDinitialized = false; // hekate sdmmmc vars @@ -35,6 +33,7 @@ sdmmc_t sd_sdmmc; sdmmc_storage_t sd_storage; // init vars +bool init_done = false; bool custom_driver = true; // FS funcs @@ -51,8 +50,8 @@ volatile int *active_partition; volatile Handle *sdmmc_das_handle; // FatFS +file_based_ctxt f_emu; static bool fat_mounted = false; -static file_based_ctxt f_emu; static void _sdmmc_ensure_device_attached(void) { @@ -68,8 +67,6 @@ static void _sdmmc_ensure_device_attached(void) static void _sdmmc_ensure_initialized(void) { - static bool init_done = false; - // First Initial init if (!sdmmc_first_init) { @@ -78,10 +75,11 @@ static void _sdmmc_ensure_initialized(void) } else { - // The boot sysmodule will eventually kill power to SD. Detect this, and reinitialize when it happens. + // The boot sysmodule will eventually kill power to SD. + // Detect this, and reinitialize when it happens. if (!init_done) { - if (gpio_read(GPIO_PORT_E, GPIO_PIN_4) == 0) + if (sdmmc_get_sd_power_enabled() == 0) { sdmmc_finalize(); sdmmc_initialize(); @@ -118,8 +116,6 @@ static void _file_based_emmc_finalize(void) void sdmmc_finalize(void) { - _file_based_emmc_finalize(); - if (!sdmmc_storage_end(&sd_storage)) { fatal_abort(Fatal_InitSD); @@ -132,7 +128,6 @@ static void _file_based_emmc_initialize(void) { char path[sizeof(emuMMC_ctx.storagePath) + 0x20]; memset(&path, 0, sizeof(path)); - memset(&f_emu, 0, sizeof(file_based_ctxt)); memcpy(path, (void *)emuMMC_ctx.storagePath, sizeof(emuMMC_ctx.storagePath)); strcat(path, "/eMMC/"); @@ -141,18 +136,24 @@ static void _file_based_emmc_initialize(void) // Open BOOT0 physical partition. memcpy(path + path_len, "BOOT0", 6); if (f_open(&f_emu.fp_boot0, path, FA_READ | FA_WRITE) != FR_OK) - fatal_abort(Fatal_InitSD); + fatal_abort(Fatal_FatfsFileOpen); + if (!f_expand_cltbl(&f_emu.fp_boot0, 0x400, f_emu.clmt_boot0, f_size(&f_emu.fp_boot0))) + fatal_abort(Fatal_FatfsMemExhaustion); // Open BOOT1 physical partition. memcpy(path + path_len, "BOOT1", 6); if (f_open(&f_emu.fp_boot1, path, FA_READ | FA_WRITE) != FR_OK) - fatal_abort(Fatal_InitSD); + fatal_abort(Fatal_FatfsFileOpen); + if (!f_expand_cltbl(&f_emu.fp_boot1, 0x400, f_emu.clmt_boot1, f_size(&f_emu.fp_boot1))) + fatal_abort(Fatal_FatfsMemExhaustion); // Open handles for GPP physical partition files. _file_based_update_filename(path, path_len, 00); if (f_open(&f_emu.fp_gpp[0], path, FA_READ | FA_WRITE) != FR_OK) - fatal_abort(Fatal_InitSD); + fatal_abort(Fatal_FatfsFileOpen); + if (!f_expand_cltbl(&f_emu.fp_gpp[0], 0x400, &f_emu.clmt_gpp[0], f_size(&f_emu.fp_gpp[0]))) + fatal_abort(Fatal_FatfsMemExhaustion); f_emu.part_size = f_size(&f_emu.fp_gpp[0]) >> 9; @@ -171,38 +172,27 @@ static void _file_based_emmc_initialize(void) return; } + + if (!f_expand_cltbl(&f_emu.fp_gpp[f_emu.parts], 0x400, &f_emu.clmt_gpp[f_emu.parts * 0x400], f_size(&f_emu.fp_gpp[f_emu.parts]))) + fatal_abort(Fatal_FatfsMemExhaustion); } } bool sdmmc_initialize(void) { - if (!storageMMCinitialized) - { - if (sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_4, SDMMC_BUS_WIDTH_8, 4)) - { - if (sdmmc_storage_set_mmc_partition(&storage, FS_EMMC_PARTITION_GPP)) - storageMMCinitialized = true; - } - else - { - fatal_abort(Fatal_InitMMC); - } - } - if (!storageSDinitialized) { - int retries = 5; + int retries = 3; while (retries) { - if (sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, SDMMC_1, SDMMC_BUS_WIDTH_4, 11)) + if (nx_sd_initialize(false)) { storageSDinitialized = true; // File based emummc. if ((emuMMC_ctx.EMMC_Type == emuMMC_SD_File) && !fat_mounted) { - f_emu.sd_fs = (FATFS *)malloc(sizeof(FATFS)); - if (f_mount(f_emu.sd_fs, "", 1) != FR_OK) + if (f_mount(&f_emu.sd_fs, "", 1) != FR_OK) fatal_abort(Fatal_InitSD); else fat_mounted = true; @@ -214,7 +204,6 @@ bool sdmmc_initialize(void) } retries--; - msleep(100); } if (!storageSDinitialized) @@ -223,7 +212,7 @@ bool sdmmc_initialize(void) } } - return storageMMCinitialized && storageSDinitialized; + return storageSDinitialized; } sdmmc_accessor_t *sdmmc_accessor_get(int mmc_id) @@ -295,38 +284,36 @@ static uint64_t emummc_read_write_inner(void *buf, unsigned int sector, unsigned } // File based emummc. - FIL *fp_tmp = NULL; + FIL *fp = NULL; switch (*active_partition) { case FS_EMMC_PARTITION_GPP: if (f_emu.parts) { - fp_tmp = &f_emu.fp_gpp[sector / f_emu.part_size]; + fp = &f_emu.fp_gpp[sector / f_emu.part_size]; sector = sector % f_emu.part_size; } else { - fp_tmp = &f_emu.fp_gpp[0]; + fp = &f_emu.fp_gpp[0]; } break; case FS_EMMC_PARTITION_BOOT1: - fp_tmp = &f_emu.fp_boot1; + fp = &f_emu.fp_boot1; break; case FS_EMMC_PARTITION_BOOT0: - fp_tmp = &f_emu.fp_boot0; + fp = &f_emu.fp_boot0; break; } - if (f_lseek(fp_tmp, sector << 9) != FR_OK) - { - ; //TODO. Out of range. close stuff and fatal? - } + if (f_lseek(fp, sector << 9) != FR_OK) + return 0; // Out of bounds. uint64_t res = 0; if (!is_write) - res = !(f_read(fp_tmp, buf, num_sectors << 9, NULL)); + res = !f_read_fast(fp, buf, num_sectors << 9); else - res = !(f_write(fp_tmp, buf, num_sectors << 9, NULL)); + res = !f_write_fast(fp, buf, num_sectors << 9); return res; } @@ -402,12 +389,15 @@ uint64_t sdmmc_wrapper_read(void *buf, uint64_t bufSize, int mmc_id, unsigned in if (first_sd_read) { first_sd_read = false; - // Because some SD cards have issues with emuMMC's driver - // we currently swap to FS's driver after first SD read - // TODO: Fix remaining driver issues - custom_driver = false; - // FS will handle sd mutex w/o custom driver from here on - unlock_mutex(sd_mutex); + if (emuMMC_ctx.EMMC_Type == emuMMC_SD) + { + // Because some SD cards have issues with emuMMC's driver + // we currently swap to FS's driver after first SD read + // for raw based emuMMC + custom_driver = false; + // FS will handle sd mutex w/o custom driver from here on + unlock_mutex(sd_mutex); + } } // Call hekates driver. diff --git a/emummc/source/emuMMC/emummc.h b/emummc/source/emuMMC/emummc.h index 16b1151e4..0acee8e4c 100644 --- a/emummc/source/emuMMC/emummc.h +++ b/emummc/source/emuMMC/emummc.h @@ -28,6 +28,7 @@ extern "C" { #include #include +#include "../emmc/nx_sd.h" #include "../emmc/sdmmc.h" #include "../soc/i2c.h" #include "../soc/gpio.h" @@ -55,15 +56,17 @@ uint64_t sdmmc_wrapper_controller_close(int mmc_id); uint64_t sdmmc_wrapper_read(void *buf, uint64_t bufSize, int mmc_id, unsigned int sector, unsigned int num_sectors); uint64_t sdmmc_wrapper_write(int mmc_id, unsigned int sector, unsigned int num_sectors, void *buf, uint64_t bufSize); -// TODO: check if FatFS internal buffers are good (perf wise) to have a x16 alignment. typedef struct _file_based_ctxt { + FATFS sd_fs; uint64_t parts; uint64_t part_size; - FATFS *sd_fs; FIL fp_boot0; + DWORD clmt_boot0[0x400]; FIL fp_boot1; + DWORD clmt_boot1[0x400]; FIL fp_gpp[32]; + DWORD clmt_gpp[0x8000]; } file_based_ctxt; #ifdef __cplusplus diff --git a/emummc/source/emuMMC/emummc_ctx.h b/emummc/source/emuMMC/emummc_ctx.h index 0bf404a3c..2663f528d 100644 --- a/emummc/source/emuMMC/emummc_ctx.h +++ b/emummc/source/emuMMC/emummc_ctx.h @@ -44,7 +44,7 @@ typedef struct _emuMMC_ctx_t enum FS_VER fs_ver; enum emuMMC_Type EMMC_Type; enum emuMMC_Type SD_Type; - + /* Partition based */ u64 EMMC_StoragePartitionOffset; u64 SD_StoragePartitionOffset; diff --git a/emummc/source/libs/fatfs/ff.c b/emummc/source/libs/fatfs/ff.c index 32304742f..50105173e 100644 --- a/emummc/source/libs/fatfs/ff.c +++ b/emummc/source/libs/fatfs/ff.c @@ -1,10 +1,25 @@ +/* + * Copyright (c) 2018 naehrwert + * Copyright (c) 2018-2019 CTCaer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem Module R0.13c (p3) / +/ FatFs - Generic FAT Filesystem Module R0.13c (p4) / /-----------------------------------------------------------------------------/ / / Copyright (C) 2018, ChaN, all right reserved. -/ Copyright (c) 2018 naehrwert -/ Copyright (C) 2018-2019 CTCaer / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -515,7 +530,7 @@ static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */ #define FREE_NAMBUF() ff_memfree(lfn) #endif #define LEAVE_MKFS(res) { if (!work) ff_memfree(buf); return res; } -#define MAX_MALLOC 0x8000 /* Must be >=FF_MAX_SS */ +#define MAX_MALLOC 0x4000 /* Must be >=FF_MAX_SS */ #else #error Wrong setting of FF_USE_LFN @@ -3879,6 +3894,109 @@ FRESULT f_read ( +#ifdef FF_FASTFS +/*-----------------------------------------------------------------------*/ +/* Fast Read Aligned Sized File Without a Cache */ +/*-----------------------------------------------------------------------*/ +#if FF_USE_FASTSEEK +FRESULT f_read_fast ( + FIL* fp, /* Pointer to the file object */ + const void* buff, /* Pointer to the data to be written */ + UINT btr /* Number of bytes to read */ +) +{ + FRESULT res; + FATFS *fs; + UINT csize_bytes; + DWORD clst; + DWORD wbytes; + UINT count; + FSIZE_t work_sector = 0; + FSIZE_t sector_base = 0; + BYTE *wbuff = (BYTE*)buff; + + res = validate(&fp->obj, &fs); /* Check validity of the file object */ + if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) { + EFSPRINTF("FOV"); + LEAVE_FF(fs, res); /* Check validity */ + } + + if (!(fp->flag & FA_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ + FSIZE_t remain = fp->obj.objsize - fp->fptr; + if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ + + csize_bytes = fs->csize * SS(fs); + DWORD csect = (UINT)((fp->fptr / SS(fs)) & (fs->csize - 1)); /* Sector offset in the cluster */ + + /* If inside a cluster, read the sectors and align to cluster. */ + if (csect) { + wbytes = MIN(btr, (fs->csize - csect) * SS(fs)); + f_read(fp, wbuff, wbytes, (void *)0); + wbuff += wbytes; + btr -= wbytes; + if (!btr) + goto out; + } + + if (!fp->fptr) { /* On the top of the file? */ + clst = fp->obj.sclust; /* Follow from the origin */ + } else { + if (fp->cltbl) clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ + else { EFSPRINTF("CLTBL"); ABORT(fs, FR_CLTBL_NO_INIT); } + } + + if (clst < 2) { EFSPRINTF("CCHK"); ABORT(fs, FR_INT_ERR); } + else if (clst == 0xFFFFFFFF) { EFSPRINTF("DSKC"); ABORT(fs, FR_DISK_ERR); } + + fp->clust = clst; /* Set working cluster */ + + wbytes = MIN(btr, csize_bytes); + sector_base = clst2sect(fs, fp->clust); + count = wbytes / SS(fs); + fp->fptr += wbytes; + btr -= wbytes; + + if (!btr) { /* Final cluster/sectors read. */ + if (disk_read(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + goto out; + } + + while (btr) { + clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ + + if (clst < 2) { EFSPRINTF("CCHK2"); ABORT(fs, FR_INT_ERR); } + else if (clst == 0xFFFFFFFF) { EFSPRINTF("DSKC"); ABORT(fs, FR_DISK_ERR); } + + fp->clust = clst; + + work_sector = clst2sect(fs, fp->clust); + wbytes = MIN(btr, csize_bytes); + if ((work_sector - sector_base) == count) count += wbytes / SS(fs); + else { + if (disk_read(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + wbuff += count * SS(fs); + + sector_base = work_sector; + count = wbytes / SS(fs); + } + + fp->fptr += wbytes; + btr -= wbytes; + + if (!btr) { /* Final cluster/sectors read. */ + if (disk_read(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + } + } + +out: + LEAVE_FF(fs, FR_OK); +} +#endif +#endif + + + + #if !FF_FS_READONLY /*-----------------------------------------------------------------------*/ /* Write File */ @@ -4018,6 +4136,117 @@ FRESULT f_write ( +#ifdef FF_FASTFS +/*-----------------------------------------------------------------------*/ +/* Fast Write Aligned Sized File Without a Cache */ +/*-----------------------------------------------------------------------*/ +#if FF_USE_FASTSEEK +FRESULT f_write_fast ( + FIL* fp, /* Pointer to the file object */ + const void* buff, /* Pointer to the data to be written */ + UINT btw /* Number of bytes to write */ +) +{ + FRESULT res; + FATFS *fs; + UINT csize_bytes; + DWORD clst; + DWORD wbytes; + UINT count; + FSIZE_t work_sector = 0; + FSIZE_t sector_base = 0; + BYTE *wbuff = (BYTE*)buff; + + res = validate(&fp->obj, &fs); /* Check validity of the file object */ + if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) { + EFSPRINTF("FOV"); + LEAVE_FF(fs, res); /* Check validity */ + } + + if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */ + /* Check fptr wrap-around (file size cannot reach 4 GiB at FAT volume) */ + if ((!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) { + btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr); + } + + csize_bytes = fs->csize * SS(fs); + DWORD csect = (UINT)((fp->fptr / SS(fs)) & (fs->csize - 1)); /* Sector offset in the cluster */ + + /* If inside a cluster, write the sectors and align to cluster. */ + if (csect) { + wbytes = MIN(btw, (fs->csize - csect) * SS(fs)); + f_write(fp, wbuff, wbytes, (void *)0); + /* Ensure flushing of it. FatFS is not notified for next write if raw. */ + f_sync(fp); + wbuff += wbytes; + btw -= wbytes; + if (!btw) + goto out; + } + + if (!fp->fptr) { /* On the top of the file? */ + clst = fp->obj.sclust; /* Follow from the origin */ + } else { + if (fp->cltbl) clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ + else { EFSPRINTF("CLTBL"); ABORT(fs, FR_CLTBL_NO_INIT); } + } + + if (clst < 2) { EFSPRINTF("CCHK"); ABORT(fs, FR_INT_ERR); } + else if (clst == 0xFFFFFFFF) { EFSPRINTF("DSKC"); ABORT(fs, FR_DISK_ERR); } + + fp->clust = clst; /* Set working cluster */ + + wbytes = MIN(btw, csize_bytes); + sector_base = clst2sect(fs, fp->clust); + count = wbytes / SS(fs); + fp->fptr += wbytes; + btw -= wbytes; + + if (!btw) { /* Final cluster/sectors write. */ + if (disk_write(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + fp->flag &= (BYTE)~FA_DIRTY; + goto out; + } + + while (btw) { + clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */ + + if (clst < 2) { EFSPRINTF("CCHK2"); ABORT(fs, FR_INT_ERR); } + else if (clst == 0xFFFFFFFF) { EFSPRINTF("DSKC"); ABORT(fs, FR_DISK_ERR); } + + fp->clust = clst; + + work_sector = clst2sect(fs, fp->clust); + wbytes = MIN(btw, csize_bytes); + if ((work_sector - sector_base) == count) count += wbytes / SS(fs); + else { + if (disk_write(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + wbuff += count * SS(fs); + + sector_base = work_sector; + count = wbytes / SS(fs); + } + + fp->fptr += wbytes; + btw -= wbytes; + + if (!btw) { /* Final cluster/sectors write. */ + if (disk_write(fs->pdrv, wbuff, sector_base, count) != RES_OK) ABORT(fs, FR_DISK_ERR); + fp->flag &= (BYTE)~FA_DIRTY; + } + } + +out: + fp->flag |= FA_MODIFIED; /* Set file change flag */ + + LEAVE_FF(fs, FR_OK); +} +#endif +#endif + + + + /*-----------------------------------------------------------------------*/ /* Synchronize the File */ /*-----------------------------------------------------------------------*/ @@ -4231,9 +4460,9 @@ FRESULT f_getcwd ( TCHAR *tp = buff; #if FF_VOLUMES >= 2 UINT vl; -#endif #if FF_STR_VOLUME_ID const char *vp; +#endif #endif FILINFO fno; DEF_NAMBUF @@ -4474,6 +4703,37 @@ FRESULT f_lseek ( +#ifdef FF_FASTFS +#if FF_USE_FASTSEEK +/*-----------------------------------------------------------------------*/ +/* Seek File Read/Write Pointer */ +/*-----------------------------------------------------------------------*/ + +DWORD *f_expand_cltbl ( + FIL* fp, /* Pointer to the file object */ + UINT tblsz, /* Size of table */ + DWORD *tbl, /* Table pointer */ + FSIZE_t ofs /* File pointer from top of file */ +) +{ + if (fp->flag & FA_WRITE) f_lseek(fp, ofs); /* Expand file if write is enabled */ + fp->cltbl = (DWORD *)tbl; + fp->cltbl[0] = tblsz; + if (f_lseek(fp, CREATE_LINKMAP)) { /* Create cluster link table */ + fp->cltbl = (void *)0; + EFSPRINTF("CLTBLSZ"); + return (void *)0; + } + f_lseek(fp, 0); + + return fp->cltbl; +} +#endif +#endif + + + + #if FF_FS_MINIMIZE <= 1 /*-----------------------------------------------------------------------*/ /* Create a Directory Object */ @@ -4714,7 +4974,7 @@ FRESULT f_getfree ( /* Get logical drive */ res = find_volume(&path, &fs, 0); if (res == FR_OK) { - *fatfs = fs; /* Return ptr to the fs object */ + if (fatfs) *fatfs = fs; /* Return ptr to the fs object */ /* If free_clst is valid, return it without full FAT scan */ if (fs->free_clst <= fs->n_fatent - 2) { *nclst = fs->free_clst; diff --git a/emummc/source/libs/fatfs/ff.h b/emummc/source/libs/fatfs/ff.h index 27bf89dbd..68dc7ab65 100644 --- a/emummc/source/libs/fatfs/ff.h +++ b/emummc/source/libs/fatfs/ff.h @@ -246,7 +246,12 @@ typedef enum { FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ +#ifdef FF_FASTFS + FR_INVALID_PARAMETER, /* (19) Given parameter is invalid */ + FR_CLTBL_NO_INIT /* (20) The cluster table for fast seek/read/write was not created */ +#else FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ +#endif } FRESULT; @@ -258,6 +263,10 @@ FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a f FRESULT f_close (FIL* fp); /* Close an open file object */ FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ +#ifdef FF_FASTFS +FRESULT f_read_fast (FIL* fp, const void* buff, UINT btr); /* Fast read data from the file */ +FRESULT f_write_fast (FIL* fp, const void* buff, UINT btw); /* Fast write data to the file */ +#endif FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ FRESULT f_truncate (FIL* fp); /* Truncate the file */ FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ @@ -279,7 +288,10 @@ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get numbe FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ -FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */ +#ifdef FF_FASTFS +DWORD *f_expand_cltbl (FIL* fp, UINT tblsz, DWORD *tbl, FSIZE_t ofs); /* Expand file and populate cluster table */ +#endif +FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */ FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */ FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */ diff --git a/emummc/source/libs/fatfs/ffconf.h b/emummc/source/libs/fatfs/ffconf.h index a8be71b0a..3d01d8b0c 100644 --- a/emummc/source/libs/fatfs/ffconf.h +++ b/emummc/source/libs/fatfs/ffconf.h @@ -15,7 +15,7 @@ / and optional writing functions as well. */ -#define FF_FS_MINIMIZE 0 +#define FF_FS_MINIMIZE 2 /* This option defines minimization level to remove some basic API functions. / / 0: Basic functions are fully enabled. @@ -41,8 +41,13 @@ #define FF_USE_MKFS 0 /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ +#define FF_FASTFS 1 +#ifdef FF_FASTFS +#define FF_USE_FASTSEEK 1 +#else #define FF_USE_FASTSEEK 0 +#endif /* This option switches fast seek function. (0:Disable or 1:Enable) */ @@ -50,7 +55,7 @@ /* This option switches f_expand function. (0:Disable or 1:Enable) */ -#define FF_USE_CHMOD 1 +#define FF_USE_CHMOD 0 /* This option switches attribute manipulation functions, f_chmod() and f_utime(). / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ @@ -239,7 +244,7 @@ #define FF_FS_NORTC 1 #define FF_NORTC_MON 1 #define FF_NORTC_MDAY 1 -#define FF_NORTC_YEAR 2019 +#define FF_NORTC_YEAR 2020 /* The option FF_FS_NORTC switches timestamp function. If the system does not have / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable / the timestamp function. Every object modified by FatFs will have a fixed timestamp diff --git a/emummc/source/libs/fatfs/ffunicode.c b/emummc/source/libs/fatfs/ffunicode.c index bc23f806c..9f039637b 100644 --- a/emummc/source/libs/fatfs/ffunicode.c +++ b/emummc/source/libs/fatfs/ffunicode.c @@ -34,7 +34,6 @@ #define MERGE2(a, b) a ## b #define CVTBL(tbl, cp) MERGE2(tbl, cp) - /*------------------------------------------------------------------------*/ /* Code Conversion Tables */ /*------------------------------------------------------------------------*/ @@ -623,5 +622,4 @@ DWORD ff_wtoupper ( /* Returns up-converted code point */ return uni; } - #endif /* #if FF_USE_LFN */ diff --git a/emummc/source/power/max77620.h b/emummc/source/power/max77620.h index fcce30980..26ea85595 100644 --- a/emummc/source/power/max77620.h +++ b/emummc/source/power/max77620.h @@ -1,7 +1,7 @@ /* * Defining registers address and its bit definitions of MAX77620 and MAX20024 * - * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2016 NVIDIA CORPORATION. All rights reserved. * Copyright (c) 2019 CTCaer * * This program is free software; you can redistribute it and/or modify it @@ -19,9 +19,19 @@ #define MAX77620_CNFGGLBL1_LBDAC_EN (1 << 7) #define MAX77620_CNFGGLBL1_MPPLD (1 << 6) #define MAX77620_CNFGGLBL1_LBHYST ((1 << 5) | (1 << 4)) -#define MAX77620_CNFGGLBL1_LBHYST_N (1 << 4) -#define MAX77620_CNFGGLBL1_LBDAC 0x0E -#define MAX77620_CNFGGLBL1_LBDAC_N (1 << 1) +#define MAX77620_CNFGGLBL1_LBHYST_100 (0 << 4) +#define MAX77620_CNFGGLBL1_LBHYST_200 (1 << 4) +#define MAX77620_CNFGGLBL1_LBHYST_300 (2 << 4) +#define MAX77620_CNFGGLBL1_LBHYST_400 (3 << 4) +#define MAX77620_CNFGGLBL1_LBDAC_MASK 0x0E +#define MAX77620_CNFGGLBL1_LBDAC_2700 (0 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_2800 (1 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_2900 (2 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_3000 (3 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_3100 (4 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_3200 (5 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_3300 (6 << 1) +#define MAX77620_CNFGGLBL1_LBDAC_3400 (7 << 1) #define MAX77620_CNFGGLBL1_LBRSTEN (1 << 0) #define MAX77620_REG_CNFGGLBL2 0x01 @@ -130,7 +140,7 @@ #define MAX77620_POWER_MODE_DISABLE 0 #define MAX20024_LDO_CFG2_MPOK_MASK (1 << 2) #define MAX77620_LDO_CFG2_ADE_MASK (1 << 1) -#define MAX77620_LDO_CFG2_ADE_DISABLE 0 +#define MAX77620_LDO_CFG2_ADE_DISABLE (0 << 1) #define MAX77620_LDO_CFG2_ADE_ENABLE (1 << 1) #define MAX77620_LDO_CFG2_SS_MASK (1 << 0) #define MAX77620_LDO_CFG2_SS_FAST (1 << 0) @@ -153,6 +163,24 @@ #define MAX77620_REG_PUE_GPIO 0x3E #define MAX77620_REG_PDE_GPIO 0x3F #define MAX77620_REG_AME_GPIO 0x40 +#define MAX77620_CNFG_GPIO_DRV_MASK (1 << 0) +#define MAX77620_CNFG_GPIO_DRV_PUSHPULL (1 << 0) +#define MAX77620_CNFG_GPIO_DRV_OPENDRAIN (0 << 0) +#define MAX77620_CNFG_GPIO_DIR_MASK (1 << 1) +#define MAX77620_CNFG_GPIO_DIR_INPUT (1 << 1) +#define MAX77620_CNFG_GPIO_DIR_OUTPUT (0 << 1) +#define MAX77620_CNFG_GPIO_INPUT_VAL_MASK (1 << 2) +#define MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK (1 << 3) +#define MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH (1 << 3) +#define MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW (0 << 3) +#define MAX77620_CNFG_GPIO_INT_MASK (0x3 << 4) +#define MAX77620_CNFG_GPIO_INT_FALLING (1 << 4) +#define MAX77620_CNFG_GPIO_INT_RISING (1 << 5) +#define MAX77620_CNFG_GPIO_DBNC_MASK (0x3 << 6) +#define MAX77620_CNFG_GPIO_DBNC_None (0x0 << 6) +#define MAX77620_CNFG_GPIO_DBNC_8ms (0x1 << 6) +#define MAX77620_CNFG_GPIO_DBNC_16ms (0x2 << 6) +#define MAX77620_CNFG_GPIO_DBNC_32ms (0x3 << 6) #define MAX77620_REG_ONOFFCNFG1 0x41 #define MAX77620_ONOFFCNFG1_SFT_RST (1 << 7) @@ -259,25 +287,6 @@ #define MAX77620_SD_CFG1_FSRADE_SD_DISABLE 0 #define MAX77620_SD_CFG1_FSRADE_SD_ENABLE (1 << 0) -#define MAX77620_CNFG_GPIO_DRV_MASK (1 << 0) -#define MAX77620_CNFG_GPIO_DRV_PUSHPULL (1 << 0) -#define MAX77620_CNFG_GPIO_DRV_OPENDRAIN 0 -#define MAX77620_CNFG_GPIO_DIR_MASK (1 << 1) -#define MAX77620_CNFG_GPIO_DIR_INPUT (1 << 1) -#define MAX77620_CNFG_GPIO_DIR_OUTPUT 0 -#define MAX77620_CNFG_GPIO_INPUT_VAL_MASK (1 << 2) -#define MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK (1 << 3) -#define MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH (1 << 3) -#define MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW 0 -#define MAX77620_CNFG_GPIO_INT_MASK (0x3 << 4) -#define MAX77620_CNFG_GPIO_INT_FALLING (1 << 4) -#define MAX77620_CNFG_GPIO_INT_RISING (1 << 5) -#define MAX77620_CNFG_GPIO_DBNC_MASK (0x3 << 6) -#define MAX77620_CNFG_GPIO_DBNC_None (0x0 << 6) -#define MAX77620_CNFG_GPIO_DBNC_8ms (0x1 << 6) -#define MAX77620_CNFG_GPIO_DBNC_16ms (0x2 << 6) -#define MAX77620_CNFG_GPIO_DBNC_32ms (0x3 << 6) - #define MAX77620_IRQ_LVL2_GPIO_EDGE0 (1 << 0) #define MAX77620_IRQ_LVL2_GPIO_EDGE1 (1 << 1) #define MAX77620_IRQ_LVL2_GPIO_EDGE2 (1 << 2) diff --git a/emummc/source/power/max7762x.c b/emummc/source/power/max7762x.c index 2c8cff402..1e090bf52 100644 --- a/emummc/source/power/max7762x.c +++ b/emummc/source/power/max7762x.c @@ -157,9 +157,3 @@ void max77620_config_default() } i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_SD_CFG2, 4); } - -void max77620_low_battery_monitor_config() -{ - i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_CNFGGLBL1, - MAX77620_CNFGGLBL1_LBDAC_EN | MAX77620_CNFGGLBL1_LBHYST_N | MAX77620_CNFGGLBL1_LBDAC_N); -} diff --git a/emummc/source/power/max7762x.h b/emummc/source/power/max7762x.h index eefa11295..c8b5c5309 100644 --- a/emummc/source/power/max7762x.h +++ b/emummc/source/power/max7762x.h @@ -24,16 +24,16 @@ * Switch Power domains (max77620): * Name | Usage | uV step | uV min | uV default | uV max | Init *-------+---------------+---------+--------+------------+---------+------------------ -* sd0 | core | 12500 | 600000 | 625000 | 1400000 | 1.125V (pkg1.1) +* sd0 | SoC | 12500 | 600000 | 625000 | 1400000 | 1.125V (pkg1.1) * sd1 | SDRAM | 12500 | 600000 | 1125000 | 1125000 | 1.1V (pkg1.1) * sd2 | ldo{0-1, 7-8} | 12500 | 600000 | 1325000 | 1350000 | 1.325V (pcv) * sd3 | 1.8V general | 12500 | 600000 | 1800000 | 1800000 | * ldo0 | Display Panel | 25000 | 800000 | 1200000 | 1200000 | 1.2V (pkg1.1) * ldo1 | XUSB, PCIE | 25000 | 800000 | 1050000 | 1050000 | 1.05V (pcv) * ldo2 | SDMMC1 | 50000 | 800000 | 1800000 | 3300000 | -* ldo3 | GC ASIC | 50000 | 800000 | 3100000 | 3100000 | 3.1V (pcv) +* ldo3 | GC ASIC | 50000 | 800000 | 3100000 | 3100000 | 3.1V (pcv) * ldo4 | RTC | 12500 | 800000 | 850000 | 850000 | -* ldo5 | GC ASIC | 50000 | 800000 | 1800000 | 1800000 | 1.8V (pcv) +* ldo5 | GC ASIC | 50000 | 800000 | 1800000 | 1800000 | 1.8V (pcv) * ldo6 | Touch, ALS | 50000 | 800000 | 2900000 | 2900000 | 2.9V * ldo7 | XUSB | 50000 | 800000 | 1050000 | 1050000 | * ldo8 | XUSB, DC | 50000 | 800000 | 1050000 | 1050000 | @@ -71,6 +71,8 @@ /* MAX77621_VOUT */ #define MAX77621_VOUT_ENABLE (1 << 7) #define MAX77621_VOUT_MASK 0x7F +#define MAX77621_VOUT_0_95V 0x37 +#define MAX77621_VOUT_1_09V 0x4F /* MAX77621_VOUT_DVC_DVS */ #define MAX77621_DVS_VOUT_MASK 0x7F @@ -111,6 +113,5 @@ int max77620_regulator_set_voltage(u32 id, u32 mv); int max77620_regulator_enable(u32 id, int enable); int max77620_regulator_set_volt_and_flags(u32 id, u32 mv, u8 flags); void max77620_config_default(); -void max77620_low_battery_monitor_config(); #endif diff --git a/emummc/source/soc/clock.c b/emummc/source/soc/clock.c index ea1920865..d0d97cc4a 100644 --- a/emummc/source/soc/clock.c +++ b/emummc/source/soc/clock.c @@ -23,6 +23,10 @@ static const sclock_t _clock_i2c5 = { CLK_RST_CONTROLLER_RST_DEVICES_H, CLK_RST_CONTROLLER_CLK_OUT_ENB_H, CLK_RST_CONTROLLER_CLK_SOURCE_I2C5, 0xF, 0, 4 //81.6MHz -> 400KHz }; +static sclock_t _clock_sdmmc_legacy_tm = { + CLK_RST_CONTROLLER_RST_DEVICES_Y, CLK_RST_CONTROLLER_CLK_OUT_ENB_Y, CLK_RST_CONTROLLER_CLK_SOURCE_SDMMC_LEGACY_TM, 1, 4, 66 +}; + void clock_enable(const sclock_t *clk) { // Put clock into reset. @@ -34,6 +38,8 @@ void clock_enable(const sclock_t *clk) CLOCK(clk->source) = clk->clk_div | (clk->clk_src << 29); // Enable. CLOCK(clk->enable) = (CLOCK(clk->enable) & ~(1 << clk->index)) | (1 << clk->index); + usleep(2); + // Take clock off reset. CLOCK(clk->reset) &= ~(1 << clk->index); } @@ -56,6 +62,33 @@ void clock_disable_i2c5() clock_disable(&_clock_i2c5); } +static void _clock_enable_pllc4() +{ + if ((CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) & (PLLCX_BASE_ENABLE | PLLCX_BASE_LOCK | 0xFFFFFF)) + == (PLLCX_BASE_ENABLE | PLLCX_BASE_LOCK | (104 << 8) | 4)) + return; + + // Enable Phase and Frequency lock detection. + //CLOCK(CLK_RST_CONTROLLER_PLLC4_MISC) = PLLC4_MISC_EN_LCKDET; + + // Disable PLL and IDDQ in case they are on. + CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) &= ~PLLCX_BASE_ENABLE; + CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) &= ~PLLC4_BASE_IDDQ; + (void)CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE); + usleep(10); + + // Set PLLC4 dividers. + CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) = (104 << 8) | 4; // DIVM: 4, DIVP: 1. + + // Enable PLLC4 and wait for Phase and Frequency lock. + CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) |= PLLCX_BASE_ENABLE; + (void)CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE); + while (!(CLOCK(CLK_RST_CONTROLLER_PLLC4_BASE) & PLLCX_BASE_LOCK)) + ; + + msleep(1); // Wait a bit for PLL to stabilize. +} + #define L_SWR_SDMMC1_RST (1 << 14) #define L_SWR_SDMMC2_RST (1 << 9) #define L_SWR_SDMMC4_RST (1 << 15) @@ -194,57 +227,103 @@ static void _clock_sdmmc_clear_enable(u32 id) } } -static u32 _clock_sdmmc_table[8] = { 0 }; +static void _clock_sdmmc_config_legacy_tm() +{ + sclock_t *clk = &_clock_sdmmc_legacy_tm; + if (!(CLOCK(clk->enable) & (1 << clk->index))) + clock_enable(clk); +} -#define PLLP_OUT0 0x0 +typedef struct _clock_sdmmc_t +{ + u32 clock; + u32 real_clock; +} clock_sdmmc_t; -static int _clock_sdmmc_config_clock_source_inner(u32 *pout, u32 id, u32 val) +static clock_sdmmc_t _clock_sdmmc_table[4] = { 0 }; + +#define SDMMC_CLOCK_SRC_PLLP_OUT0 0x0 +#define SDMMC_CLOCK_SRC_PLLC4_OUT2 0x3 +#define SDMMC4_CLOCK_SRC_PLLC4_OUT2_LJ 0x1 + +static int _clock_sdmmc_config_clock_host(u32 *pclock, u32 id, u32 val) { u32 divisor = 0; - u32 source = PLLP_OUT0; + u32 source = SDMMC_CLOCK_SRC_PLLP_OUT0; + if (id > SDMMC_4) + return 0; + + // Get IO clock divisor. switch (val) { case 25000: - *pout = 24728; - divisor = 31; + *pclock = 24728; + divisor = 31; // 16.5 div. break; case 26000: - *pout = 25500; - divisor = 30; + *pclock = 25500; + divisor = 30; // 16 div. break; case 40800: - *pout = 40800; - divisor = 18; + *pclock = 40800; + divisor = 18; // 10 div. break; case 50000: - *pout = 48000; - divisor = 15; + *pclock = 48000; + divisor = 15; // 8.5 div. break; case 52000: - *pout = 51000; - divisor = 14; + *pclock = 51000; + divisor = 14; // 8 div. break; case 100000: - *pout = 90667; - divisor = 7; + source = SDMMC_CLOCK_SRC_PLLC4_OUT2; + *pclock = 99840; + divisor = 2; // 2 div. + break; + case 164000: + *pclock = 163200; + divisor = 3; // 2.5 div. break; case 200000: - *pout = 163200; - divisor = 3; - break; - case 208000: - *pout = 204000; - divisor = 2; + switch (id) + { + case SDMMC_1: + source = SDMMC_CLOCK_SRC_PLLC4_OUT2; + break; + case SDMMC_2: + source = SDMMC4_CLOCK_SRC_PLLC4_OUT2_LJ; + break; + case SDMMC_3: + source = SDMMC_CLOCK_SRC_PLLC4_OUT2; + break; + case SDMMC_4: + source = SDMMC4_CLOCK_SRC_PLLC4_OUT2_LJ; + break; + } + *pclock = 199680; + divisor = 0; // 1 div. break; default: - *pout = 24728; - divisor = 31; + *pclock = 24728; + divisor = 31; // 16.5 div. } - _clock_sdmmc_table[2 * id] = val; - _clock_sdmmc_table[2 * id + 1] = *pout; + _clock_sdmmc_table[id].clock = val; + _clock_sdmmc_table[id].real_clock = *pclock; + // PLLC4 and LEGACY_TM clocks are already initialized, + // because we init at the first eMMC read. + // // Enable PLLC4 if in use by any SDMMC. + // if (source) + // _clock_enable_pllc4(); + + // // Set SDMMC legacy timeout clock. + // _clock_sdmmc_config_legacy_tm(); + + + // Set SDMMC clock. switch (id) { case SDMMC_1: @@ -264,69 +343,75 @@ static int _clock_sdmmc_config_clock_source_inner(u32 *pout, u32 id, u32 val) return 1; } -void clock_sdmmc_config_clock_source(u32 *pout, u32 id, u32 val) +void clock_sdmmc_config_clock_source(u32 *pclock, u32 id, u32 val) { - if (_clock_sdmmc_table[2 * id] == val) + if (_clock_sdmmc_table[id].clock == val) { - *pout = _clock_sdmmc_table[2 * id + 1]; + *pclock = _clock_sdmmc_table[id].real_clock; } else { int is_enabled = _clock_sdmmc_is_enabled(id); if (is_enabled) _clock_sdmmc_clear_enable(id); - _clock_sdmmc_config_clock_source_inner(pout, id, val); + _clock_sdmmc_config_clock_host(pclock, id, val); if (is_enabled) _clock_sdmmc_set_enable(id); _clock_sdmmc_is_reset(id); } } -void clock_sdmmc_get_params(u32 *pout, u16 *pdivisor, u32 type) +void clock_sdmmc_get_card_clock_div(u32 *pclock, u16 *pdivisor, u32 type) { + // Get Card clock divisor. switch (type) { - case 0: - *pout = 26000; + case SDHCI_TIMING_MMC_ID: // Actual IO Freq: 380.59 KHz. + *pclock = 26000; *pdivisor = 66; break; - case 1: - *pout = 26000; + case SDHCI_TIMING_MMC_LS26: + *pclock = 26000; *pdivisor = 1; break; - case 2: - *pout = 52000; + case SDHCI_TIMING_MMC_HS52: + *pclock = 52000; *pdivisor = 1; break; - case 3: - case 4: - case 11: - *pout = 200000; + case SDHCI_TIMING_MMC_HS200: + case SDHCI_TIMING_MMC_HS400: + case SDHCI_TIMING_UHS_SDR104: + *pclock = 200000; *pdivisor = 1; break; - case 5: - *pout = 25000; + case SDHCI_TIMING_SD_ID: // Actual IO Freq: 380.43 KHz. + *pclock = 25000; *pdivisor = 64; break; - case 6: - case 8: - *pout = 25000; + case SDHCI_TIMING_SD_DS12: + case SDHCI_TIMING_UHS_SDR12: + *pclock = 25000; *pdivisor = 1; break; - case 7: - *pout = 50000; + case SDHCI_TIMING_SD_HS25: + case SDHCI_TIMING_UHS_SDR25: + *pclock = 50000; *pdivisor = 1; break; - case 10: - *pout = 100000; + case SDHCI_TIMING_UHS_SDR50: + *pclock = 100000; *pdivisor = 1; break; - case 13: - *pout = 40800; + case SDHCI_TIMING_UHS_SDR82: + *pclock = 164000; *pdivisor = 1; break; - case 14: - *pout = 200000; + case SDHCI_TIMING_UHS_DDR50: + *pclock = 40800; + *pdivisor = 1; + break; + case SDHCI_TIMING_MMC_DDR52: // Actual IO Freq: 49.92 MHz. + *pclock = 200000; *pdivisor = 2; break; } @@ -339,15 +424,15 @@ int clock_sdmmc_is_not_reset_and_enabled(u32 id) void clock_sdmmc_enable(u32 id, u32 val) { - u32 div = 0; + u32 clock = 0; if (_clock_sdmmc_is_enabled(id)) _clock_sdmmc_clear_enable(id); _clock_sdmmc_set_reset(id); - _clock_sdmmc_config_clock_source_inner(&div, id, val); + _clock_sdmmc_config_clock_host(&clock, id, val); _clock_sdmmc_set_enable(id); _clock_sdmmc_is_reset(id); - usleep((100000 + div - 1) / div); + usleep((100000 + clock - 1) / clock); _clock_sdmmc_clear_reset(id); _clock_sdmmc_is_reset(id); } diff --git a/emummc/source/soc/clock.h b/emummc/source/soc/clock.h index bad138bbe..09f18a0f1 100644 --- a/emummc/source/soc/clock.h +++ b/emummc/source/soc/clock.h @@ -35,12 +35,16 @@ #define CLK_RST_CONTROLLER_MISC_CLK_ENB 0x48 #define CLK_RST_CONTROLLER_OSC_CTRL 0x50 #define CLK_RST_CONTROLLER_PLLC_BASE 0x80 +#define CLK_RST_CONTROLLER_PLLC_OUT 0x84 #define CLK_RST_CONTROLLER_PLLC_MISC 0x88 +#define CLK_RST_CONTROLLER_PLLC_MISC_1 0x8C #define CLK_RST_CONTROLLER_PLLM_BASE 0x90 #define CLK_RST_CONTROLLER_PLLM_MISC1 0x98 #define CLK_RST_CONTROLLER_PLLM_MISC2 0x9C #define CLK_RST_CONTROLLER_PLLP_BASE 0xA0 #define CLK_RST_CONTROLLER_PLLD_BASE 0xD0 +#define CLK_RST_CONTROLLER_PLLD_MISC1 0xD8 +#define CLK_RST_CONTROLLER_PLLD_MISC 0xDC #define CLK_RST_CONTROLLER_PLLX_BASE 0xE0 #define CLK_RST_CONTROLLER_PLLX_MISC 0xE4 #define CLK_RST_CONTROLLER_PLLE_BASE 0xE8 @@ -50,6 +54,7 @@ #define CLK_RST_CONTROLLER_CLK_SOURCE_PWM 0x110 #define CLK_RST_CONTROLLER_CLK_SOURCE_I2C1 0x124 #define CLK_RST_CONTROLLER_CLK_SOURCE_I2C5 0x128 +#define CLK_RST_CONTROLLER_CLK_SOURCE_DISP1 0x138 #define CLK_RST_CONTROLLER_CLK_SOURCE_VI 0x148 #define CLK_RST_CONTROLLER_CLK_SOURCE_SDMMC1 0x150 #define CLK_RST_CONTROLLER_CLK_SOURCE_SDMMC2 0x154 @@ -57,11 +62,13 @@ #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTA 0x178 #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTB 0x17C #define CLK_RST_CONTROLLER_CLK_SOURCE_HOST1X 0x180 +#define CLK_RST_CONTROLLER_CLK_SOURCE_I2C2 0x198 +#define CLK_RST_CONTROLLER_CLK_SOURCE_EMC 0x19C #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTC 0x1A0 #define CLK_RST_CONTROLLER_CLK_SOURCE_I2C3 0x1B8 #define CLK_RST_CONTROLLER_CLK_SOURCE_SDMMC3 0x1BC +#define CLK_RST_CONTROLLER_CLK_SOURCE_UARTD 0x1C0 #define CLK_RST_CONTROLLER_CLK_SOURCE_CSITE 0x1D4 -#define CLK_RST_CONTROLLER_CLK_SOURCE_EMC 0x19C #define CLK_RST_CONTROLLER_CLK_SOURCE_TSEC 0x1F4 #define CLK_RST_CONTROLLER_CLK_OUT_ENB_X 0x280 #define CLK_RST_CONTROLLER_CLK_ENB_X_SET 0x284 @@ -95,9 +102,13 @@ #define CLK_RST_CONTROLLER_LVL2_CLK_GATE_OVRC 0x3A0 #define CLK_RST_CONTROLLER_LVL2_CLK_GATE_OVRD 0x3A4 #define CLK_RST_CONTROLLER_CLK_SOURCE_MSELECT 0x3B4 +#define CLK_RST_CONTROLLER_CLK_SOURCE_I2C4 0x3C4 +#define CLK_RST_CONTROLLER_CLK_SOURCE_SYS 0x400 #define CLK_RST_CONTROLLER_CLK_SOURCE_SOR1 0x410 #define CLK_RST_CONTROLLER_CLK_SOURCE_SE 0x42C +#define CLK_RST_CONTROLLER_RST_DEV_V_CLR 0x434 #define CLK_RST_CONTROLLER_CLK_ENB_V_SET 0x440 +#define CLK_RST_CONTROLLER_CLK_ENB_V_CLR 0x444 #define CLK_RST_CONTROLLER_CLK_ENB_W_SET 0x448 #define CLK_RST_CONTROLLER_CLK_ENB_W_CLR 0x44C #define CLK_RST_CONTROLLER_RST_CPUG_CMPLX_SET 0x450 @@ -108,16 +119,32 @@ #define CLK_RST_CONTROLLER_PLLX_MISC_3 0x518 #define CLK_RST_CONTROLLER_LVL2_CLK_GATE_OVRE 0x554 #define CLK_RST_CONTROLLER_SPARE_REG0 0x55C +#define CLK_RST_CONTROLLER_PLLC4_BASE 0x5A4 +#define CLK_RST_CONTROLLER_PLLC4_MISC 0x5A8 +#define CLK_RST_CONTROLLER_PLLC_MISC_2 0x5D0 +#define CLK_RST_CONTROLLER_PLLC4_OUT 0x5E4 #define CLK_RST_CONTROLLER_PLLMB_BASE 0x5E8 #define CLK_RST_CONTROLLER_CLK_SOURCE_DSIA_LP 0x620 +#define CLK_RST_CONTROLLER_CLK_SOURCE_I2C6 0x65C #define CLK_RST_CONTROLLER_CLK_SOURCE_EMC_DLL 0x664 -#define CLK_RST_CONTROLLER_CLK_SOURCE_UART_FST_MIP_CAL 0x66C +#define CLK_RST_CONTROLLER_CLK_SOURCE_UART_FST_MIPI_CAL 0x66C #define CLK_RST_CONTROLLER_CLK_SOURCE_SDMMC_LEGACY_TM 0x694 #define CLK_RST_CONTROLLER_CLK_SOURCE_NVENC 0x6A0 #define CLK_RST_CONTROLLER_SE_SUPER_CLK_DIVIDER 0x704 +#define CLK_RST_CONTROLLER_CLK_SOURCE_UARTAPE 0x710 #define CLK_NO_SOURCE 0x0 +/*! PLL control and status bits */ +#define PLLCX_BASE_ENABLE (1 << 30) +#define PLLCX_BASE_REF_DIS (1 << 29) +#define PLLCX_BASE_LOCK (1 << 27) + +#define PLLC4_MISC_EN_LCKDET (1 << 30) +#define PLLC4_BASE_IDDQ (1 << 18) +#define PLLC4_OUT3_CLKEN (1 << 1) +#define PLLC4_OUT3_RSTN_CLR (1 << 0) + /*! Generic clock descriptor. */ typedef struct _sclock_t { @@ -136,8 +163,8 @@ void clock_disable(const sclock_t *clk); /*! Clock control for specific hardware portions. */ void clock_enable_i2c5(); void clock_disable_i2c5(); -void clock_sdmmc_config_clock_source(u32 *pout, u32 id, u32 val); -void clock_sdmmc_get_params(u32 *pout, u16 *pdivisor, u32 type); +void clock_sdmmc_config_clock_source(u32 *pclock, u32 id, u32 val); +void clock_sdmmc_get_card_clock_div(u32 *pclock, u16 *pdivisor, u32 type); int clock_sdmmc_is_not_reset_and_enabled(u32 id); void clock_sdmmc_enable(u32 id, u32 val); void clock_sdmmc_disable(u32 id); diff --git a/emummc/source/soc/i2c.c b/emummc/source/soc/i2c.c index 3343f1d35..423210f6f 100644 --- a/emummc/source/soc/i2c.c +++ b/emummc/source/soc/i2c.c @@ -49,12 +49,17 @@ static int _i2c_send_pkt(u32 idx, u32 x, u8 *buf, u32 size) vu32 *base = (vu32 *)QueryIoMapping(i2c_addrs[I2C_5], 0x1000); base[I2C_CMD_ADDR0] = x << 1; //Set x (send mode). base[I2C_CMD_DATA1] = tmp; //Set value. - base[I2C_CNFG] = (2 * size - 2) | 0x2800; //Set size and send mode. - _i2c_wait(base); //Kick transaction. + base[I2C_CNFG] = ((size - 1) << 1) | 0x2800; //Set size and send mode. + _i2c_wait(base); //Kick transaction. base[I2C_CNFG] = (base[I2C_CNFG] & 0xFFFFFDFF) | 0x200; + + u32 timeout = get_tmr_ms() + 1500; while (base[I2C_STATUS] & 0x100) - ; + { + if (get_tmr_ms() > timeout) + return 0; + } if (base[I2C_STATUS] << 28) return 0; @@ -68,14 +73,18 @@ static int _i2c_recv_pkt(u32 idx, u8 *buf, u32 size, u32 x) return 0; vu32 *base = (vu32 *)QueryIoMapping(i2c_addrs[I2C_5], 0x1000); - - base[I2C_CMD_ADDR0] = (x << 1) | 1; // Set x (recv mode). - base[I2C_CNFG] = (size - 1) << 1 | 0x2840; // Set size and recv mode. - _i2c_wait(base); // Kick transaction. + base[I2C_CMD_ADDR0] = (x << 1) | 1; // Set x (recv mode). + base[I2C_CNFG] = ((size - 1) << 1) | 0x2840; // Set size and recv mode. + _i2c_wait(base); // Kick transaction. base[I2C_CNFG] = (base[I2C_CNFG] & 0xFFFFFDFF) | 0x200; + + u32 timeout = get_tmr_ms() + 1500; while (base[I2C_STATUS] & 0x100) - ; + { + if (get_tmr_ms() > timeout) + return 0; + } if (base[I2C_STATUS] << 28) return 0; diff --git a/emummc/source/soc/pmc.h b/emummc/source/soc/pmc.h index ec8eb99a8..8cd916160 100644 --- a/emummc/source/soc/pmc.h +++ b/emummc/source/soc/pmc.h @@ -25,6 +25,7 @@ #define APBDEV_PMC_PWRGATE_TOGGLE 0x30 #define APBDEV_PMC_PWRGATE_STATUS 0x38 #define APBDEV_PMC_NO_IOPOWER 0x44 +#define PMC_NO_IOPOWER_SDMMC1_IO_EN (1 << 12) #define APBDEV_PMC_SCRATCH0 0x50 #define APBDEV_PMC_SCRATCH1 0x54 #define APBDEV_PMC_SCRATCH20 0xA0 @@ -37,6 +38,7 @@ #define APBDEV_PMC_SCRATCH33 0x120 #define APBDEV_PMC_SCRATCH40 0x13C #define APBDEV_PMC_OSC_EDPD_OVER 0x1A4 +#define PMC_OSC_EDPD_OVER_OSC_CTRL_OVER 0x400000 #define APBDEV_PMC_RST_STATUS 0x1B4 #define APBDEV_PMC_IO_DPD_REQ 0x1B8 #define APBDEV_PMC_IO_DPD2_REQ 0x1C0 @@ -51,9 +53,11 @@ #define APBDEV_PMC_REG_SHORT 0x2CC #define APBDEV_PMC_SEC_DISABLE3 0x2D8 #define APBDEV_PMC_SECURE_SCRATCH21 0x334 +#define PMC_FUSE_PRIVATEKEYDISABLE_TZ_STICKY_BIT 0x10 #define APBDEV_PMC_SECURE_SCRATCH32 0x360 #define APBDEV_PMC_SECURE_SCRATCH49 0x3A4 #define APBDEV_PMC_CNTRL2 0x440 +#define PMC_CNTRL2_HOLD_CKE_LOW_EN 0x1000 #define APBDEV_PMC_IO_DPD3_REQ 0x45C #define APBDEV_PMC_IO_DPD4_REQ 0x464 #define APBDEV_PMC_UTMIP_PAD_CFG1 0x4C4 diff --git a/emummc/source/soc/t210.h b/emummc/source/soc/t210.h index 0ce1d5d39..8e6f3cfa9 100644 --- a/emummc/source/soc/t210.h +++ b/emummc/source/soc/t210.h @@ -88,7 +88,9 @@ intptr_t QueryIoMapping(u64 addr, u64 size); #define APB_MISC_PP_PINMUX_GLOBAL 0x40 #define APB_MISC_GP_LCD_BL_PWM_CFGPADCTRL 0xA34 #define APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL 0xA98 +#define APB_MISC_GP_EMMC2_PAD_CFGPADCTRL 0xA9C #define APB_MISC_GP_EMMC4_PAD_CFGPADCTRL 0xAB4 +#define APB_MISC_GP_EMMC4_PAD_PUPD_CFGPADCTRL 0xABC #define APB_MISC_GP_WIFI_EN_CFGPADCTRL 0xB64 #define APB_MISC_GP_WIFI_RST_CFGPADCTRL 0xB68 diff --git a/emummc/source/utils/fatal.h b/emummc/source/utils/fatal.h index f2d42fd6f..7843b54d4 100644 --- a/emummc/source/utils/fatal.h +++ b/emummc/source/utils/fatal.h @@ -29,8 +29,12 @@ enum FatalReason Fatal_UnknownVersion, Fatal_BadResult, Fatal_GetConfig, + Fatal_OpenAccessor, Fatal_CloseAccessor, Fatal_IoMapping, + Fatal_FatfsMount, + Fatal_FatfsFileOpen, + Fatal_FatfsMemExhaustion, Fatal_Max }; diff --git a/emummc/source/utils/util.c b/emummc/source/utils/util.c index 274be7e4f..01c96ae34 100644 --- a/emummc/source/utils/util.c +++ b/emummc/source/utils/util.c @@ -96,7 +96,7 @@ u64 get_tmr_us() void msleep(u64 milliseconds) { u64 now = get_tmr_ms(); - while (get_tmr_ms() - now < milliseconds) + while (((u64)get_tmr_ms() - now) < milliseconds) ; //svcSleepThread(1000000 * milliseconds); } @@ -105,7 +105,7 @@ void msleep(u64 milliseconds) void usleep(u64 microseconds) { u64 now = get_tmr_us(); - while (get_tmr_us() - now < microseconds) + while (((u64)get_tmr_us() - now) < microseconds) ; //svcSleepThread(1000 * microseconds); } diff --git a/emummc/source/utils/util.h b/emummc/source/utils/util.h index 843e969aa..bf13c8958 100644 --- a/emummc/source/utils/util.h +++ b/emummc/source/utils/util.h @@ -22,8 +22,8 @@ #include "../emuMMC/emummc_ctx.h" intptr_t QueryIoMapping(u64 addr, u64 size); -#define byte_swap_32(num) ((num >> 24) & 0xff) | ((num << 8) & 0xff0000) | \ - ((num >> 8 )& 0xff00) | ((num << 24) & 0xff000000) +#define byte_swap_32(num) (((num >> 24) & 0xff) | ((num << 8) & 0xff0000) | \ + ((num >> 8 )& 0xff00) | ((num << 24) & 0xff000000)) typedef struct _cfg_op_t {