1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2016 - 2020, Broadcom 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <inttypes.h> 8*91f16700Schasinglulu #include <stddef.h> 9*91f16700Schasinglulu #include <stdint.h> 10*91f16700Schasinglulu #include <stdlib.h> 11*91f16700Schasinglulu #include <string.h> 12*91f16700Schasinglulu 13*91f16700Schasinglulu #include <arch_helpers.h> 14*91f16700Schasinglulu #include <lib/mmio.h> 15*91f16700Schasinglulu 16*91f16700Schasinglulu #include "bcm_emmc.h" 17*91f16700Schasinglulu #include "emmc_chal_types.h" 18*91f16700Schasinglulu #include "emmc_csl_sdprot.h" 19*91f16700Schasinglulu #include "emmc_chal_sd.h" 20*91f16700Schasinglulu #include "emmc_csl_sdcmd.h" 21*91f16700Schasinglulu #include "emmc_csl_sd.h" 22*91f16700Schasinglulu #include "emmc_pboot_hal_memory_drv.h" 23*91f16700Schasinglulu 24*91f16700Schasinglulu #define SD_CARD_BUSY 0x80000000 25*91f16700Schasinglulu #define SD_CARD_RETRY_LIMIT 1000 26*91f16700Schasinglulu #define SD_CARD_HIGH_SPEED_PS 13 27*91f16700Schasinglulu #define SD_CHK_HIGH_SPEED_MODE 0x00FFFFF1 28*91f16700Schasinglulu #define SD_SET_HIGH_SPEED_MODE 0x80FFFFF1 29*91f16700Schasinglulu #define SD_MMC_ENABLE_HIGH_SPEED 0x03b90100 //0x03b90103 30*91f16700Schasinglulu #define SD_MMC_8BIT_MODE 0x03b70200 31*91f16700Schasinglulu #define SD_MMC_4BIT_MODE 0x03b70100 32*91f16700Schasinglulu #define SD_MMC_1BIT_MODE 0x03b70000 33*91f16700Schasinglulu 34*91f16700Schasinglulu #define SD_MMC_BOOT_8BIT_MODE 0x03b10200 35*91f16700Schasinglulu #define SD_MMC_BOOT_4BIT_MODE 0x03b10100 36*91f16700Schasinglulu #define SD_MMC_BOOT_1BIT_MODE 0x03b10000 37*91f16700Schasinglulu #define SDIO_HW_EMMC_EXT_CSD_BOOT_CNF 0X03B30000 38*91f16700Schasinglulu 39*91f16700Schasinglulu #ifdef USE_EMMC_FIP_TOC_CACHE 40*91f16700Schasinglulu /* 41*91f16700Schasinglulu * Cache size mirrors the size of the global eMMC temp buffer 42*91f16700Schasinglulu * which is used for non-image body reads such as headers, ToC etc. 43*91f16700Schasinglulu */ 44*91f16700Schasinglulu #define CACHE_SIZE ((EMMC_BLOCK_SIZE) * 2) 45*91f16700Schasinglulu #define PARTITION_BLOCK_ADDR ((PLAT_FIP_ATTEMPT_OFFSET)/(EMMC_BLOCK_SIZE)) 46*91f16700Schasinglulu 47*91f16700Schasinglulu static uint32_t cached_partition_block; 48*91f16700Schasinglulu static uint8_t cached_block[CACHE_SIZE]; 49*91f16700Schasinglulu #endif 50*91f16700Schasinglulu 51*91f16700Schasinglulu static int set_card_data_width(struct sd_handle *handle, int width); 52*91f16700Schasinglulu static int abort_err(struct sd_handle *handle); 53*91f16700Schasinglulu static int err_recovery(struct sd_handle *handle, uint32_t errors); 54*91f16700Schasinglulu static int xfer_data(struct sd_handle *handle, uint32_t mode, uint32_t addr, 55*91f16700Schasinglulu uint32_t length, uint8_t *base); 56*91f16700Schasinglulu 57*91f16700Schasinglulu int set_boot_config(struct sd_handle *handle, uint32_t config) 58*91f16700Schasinglulu { 59*91f16700Schasinglulu return mmc_cmd6(handle, SDIO_HW_EMMC_EXT_CSD_BOOT_CNF | config); 60*91f16700Schasinglulu } 61*91f16700Schasinglulu 62*91f16700Schasinglulu void process_csd_mmc_speed(struct sd_handle *handle, uint32_t csd_mmc_speed) 63*91f16700Schasinglulu { 64*91f16700Schasinglulu uint32_t div_ctrl_setting; 65*91f16700Schasinglulu 66*91f16700Schasinglulu /* CSD field TRAN_SPEED: 67*91f16700Schasinglulu * Bits [2:0] 0 = 100 KHz 68*91f16700Schasinglulu * 1 = 1 MHz 69*91f16700Schasinglulu * 2 = 10 MHz 70*91f16700Schasinglulu * 3 = 100 MHz 71*91f16700Schasinglulu * 4...7 Reserved. 72*91f16700Schasinglulu * Bits [6:3] 0 = Reserved 73*91f16700Schasinglulu * 1 = 1.0 74*91f16700Schasinglulu * 2 = 1.2 75*91f16700Schasinglulu * 3 = 1.3 76*91f16700Schasinglulu * 4 = 1.5 77*91f16700Schasinglulu * 5 = 2.0 78*91f16700Schasinglulu * 6 = 2.6 79*91f16700Schasinglulu * 7 = 3.0 80*91f16700Schasinglulu * 8 = 3.5 81*91f16700Schasinglulu * 9 = 4.0 82*91f16700Schasinglulu * A = 4.5 83*91f16700Schasinglulu * B = 5.2 84*91f16700Schasinglulu * C = 5.5 85*91f16700Schasinglulu * D = 6.0 86*91f16700Schasinglulu * E = 7.0 87*91f16700Schasinglulu * F = 8.0 88*91f16700Schasinglulu * For cards supporting version 4.0, 4.1, and 4.2 of the standard, 89*91f16700Schasinglulu * the value shall be 20 MHz (0x2A). 90*91f16700Schasinglulu * For cards supporting version 4.3 , the value shall be 26 MHz (0x32) 91*91f16700Schasinglulu */ 92*91f16700Schasinglulu 93*91f16700Schasinglulu switch (csd_mmc_speed & 0x7F) { 94*91f16700Schasinglulu case 0x2A: 95*91f16700Schasinglulu EMMC_TRACE("Speeding up eMMC clock to 20MHz\n"); 96*91f16700Schasinglulu div_ctrl_setting = 97*91f16700Schasinglulu chal_sd_freq_2_div_ctrl_setting(20 * 1000 * 1000); 98*91f16700Schasinglulu break; 99*91f16700Schasinglulu case 0x32: 100*91f16700Schasinglulu EMMC_TRACE("Speeding up eMMC clock to 26MHz\n"); 101*91f16700Schasinglulu div_ctrl_setting = 102*91f16700Schasinglulu chal_sd_freq_2_div_ctrl_setting(26 * 1000 * 1000); 103*91f16700Schasinglulu break; 104*91f16700Schasinglulu default: 105*91f16700Schasinglulu /* Unknown */ 106*91f16700Schasinglulu return; 107*91f16700Schasinglulu } 108*91f16700Schasinglulu 109*91f16700Schasinglulu chal_sd_set_clock((CHAL_HANDLE *) handle->device, div_ctrl_setting, 0); 110*91f16700Schasinglulu 111*91f16700Schasinglulu chal_sd_set_clock((CHAL_HANDLE *) handle->device, div_ctrl_setting, 1); 112*91f16700Schasinglulu 113*91f16700Schasinglulu SD_US_DELAY(1000); 114*91f16700Schasinglulu } 115*91f16700Schasinglulu 116*91f16700Schasinglulu 117*91f16700Schasinglulu /* 118*91f16700Schasinglulu * The function changes SD/SDIO/MMC card data width if 119*91f16700Schasinglulu * the card support configurable data width. The host controller 120*91f16700Schasinglulu * and the card has to be in the same bus data width. 121*91f16700Schasinglulu */ 122*91f16700Schasinglulu int set_card_data_width(struct sd_handle *handle, int width) 123*91f16700Schasinglulu { 124*91f16700Schasinglulu uint32_t data_width = 0; 125*91f16700Schasinglulu int is_valid_arg = 1; 126*91f16700Schasinglulu int rc = SD_FAIL; 127*91f16700Schasinglulu char *bitwidth_str = " "; 128*91f16700Schasinglulu char *result_str = "failed"; 129*91f16700Schasinglulu 130*91f16700Schasinglulu switch (width) { 131*91f16700Schasinglulu #ifdef DRIVER_EMMC_ENABLE_DATA_WIDTH_8BIT 132*91f16700Schasinglulu case SD_BUS_DATA_WIDTH_8BIT: 133*91f16700Schasinglulu data_width = SD_MMC_8BIT_MODE; 134*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 135*91f16700Schasinglulu bitwidth_str = "8_BIT"; 136*91f16700Schasinglulu #endif 137*91f16700Schasinglulu break; 138*91f16700Schasinglulu #endif 139*91f16700Schasinglulu case SD_BUS_DATA_WIDTH_4BIT: 140*91f16700Schasinglulu data_width = SD_MMC_4BIT_MODE; 141*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 142*91f16700Schasinglulu bitwidth_str = "4_BIT"; 143*91f16700Schasinglulu #endif 144*91f16700Schasinglulu break; 145*91f16700Schasinglulu 146*91f16700Schasinglulu case SD_BUS_DATA_WIDTH_1BIT: 147*91f16700Schasinglulu data_width = SD_MMC_1BIT_MODE; 148*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 149*91f16700Schasinglulu bitwidth_str = "1_BIT"; 150*91f16700Schasinglulu #endif 151*91f16700Schasinglulu break; 152*91f16700Schasinglulu 153*91f16700Schasinglulu default: 154*91f16700Schasinglulu is_valid_arg = 0; 155*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 156*91f16700Schasinglulu bitwidth_str = "unknown"; 157*91f16700Schasinglulu #endif 158*91f16700Schasinglulu break; 159*91f16700Schasinglulu } 160*91f16700Schasinglulu 161*91f16700Schasinglulu if (is_valid_arg) { 162*91f16700Schasinglulu rc = mmc_cmd6(handle, data_width); 163*91f16700Schasinglulu if (rc == SD_OK) { 164*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 165*91f16700Schasinglulu result_str = "succeeded"; 166*91f16700Schasinglulu #endif 167*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 168*91f16700Schasinglulu width); 169*91f16700Schasinglulu } else { 170*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 171*91f16700Schasinglulu result_str = "failed"; 172*91f16700Schasinglulu #endif 173*91f16700Schasinglulu } 174*91f16700Schasinglulu } else { 175*91f16700Schasinglulu rc = SD_FAIL; 176*91f16700Schasinglulu #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 177*91f16700Schasinglulu result_str = "ignored"; 178*91f16700Schasinglulu #endif 179*91f16700Schasinglulu } 180*91f16700Schasinglulu 181*91f16700Schasinglulu VERBOSE("SDIO Data Width(%s) %s.\n", bitwidth_str, result_str); 182*91f16700Schasinglulu 183*91f16700Schasinglulu return rc; 184*91f16700Schasinglulu } 185*91f16700Schasinglulu 186*91f16700Schasinglulu 187*91f16700Schasinglulu /* 188*91f16700Schasinglulu * Error handling routine. Does abort data 189*91f16700Schasinglulu * transmission if error is found. 190*91f16700Schasinglulu */ 191*91f16700Schasinglulu static int abort_err(struct sd_handle *handle) 192*91f16700Schasinglulu { 193*91f16700Schasinglulu uint32_t present, options, event, rel = 0; 194*91f16700Schasinglulu struct sd_resp cmdRsp; 195*91f16700Schasinglulu 196*91f16700Schasinglulu handle->device->ctrl.argReg = 0; 197*91f16700Schasinglulu handle->device->ctrl.cmdIndex = SD_CMD_STOP_TRANSMISSION; 198*91f16700Schasinglulu 199*91f16700Schasinglulu options = (SD_CMD_STOP_TRANSMISSION << 24) | 200*91f16700Schasinglulu (SD_CMDR_RSP_TYPE_R1b_5b << SD_CMDR_RSP_TYPE_S) | 201*91f16700Schasinglulu SD4_EMMC_TOP_CMD_CRC_EN_MASK | 202*91f16700Schasinglulu SD4_EMMC_TOP_CMD_CCHK_EN_MASK; 203*91f16700Schasinglulu 204*91f16700Schasinglulu chal_sd_send_cmd((CHAL_HANDLE *) handle->device, 205*91f16700Schasinglulu handle->device->ctrl.cmdIndex, 206*91f16700Schasinglulu handle->device->ctrl.argReg, options); 207*91f16700Schasinglulu 208*91f16700Schasinglulu event = wait_for_event(handle, 209*91f16700Schasinglulu SD4_EMMC_TOP_INTR_CMDDONE_MASK | 210*91f16700Schasinglulu SD_ERR_INTERRUPTS, 211*91f16700Schasinglulu handle->device->cfg.wfe_retry); 212*91f16700Schasinglulu 213*91f16700Schasinglulu if (event & SD_CMD_ERROR_INT) { 214*91f16700Schasinglulu rel = SD_ERROR_NON_RECOVERABLE; 215*91f16700Schasinglulu } else { 216*91f16700Schasinglulu if (event & SD_DAT_TIMEOUT) { 217*91f16700Schasinglulu return SD_ERROR_NON_RECOVERABLE; 218*91f16700Schasinglulu } 219*91f16700Schasinglulu 220*91f16700Schasinglulu chal_sd_get_response((CHAL_HANDLE *) handle->device, 221*91f16700Schasinglulu (uint32_t *)&cmdRsp); 222*91f16700Schasinglulu 223*91f16700Schasinglulu process_cmd_response(handle, handle->device->ctrl.cmdIndex, 224*91f16700Schasinglulu cmdRsp.data.r2.rsp1, cmdRsp.data.r2.rsp2, 225*91f16700Schasinglulu cmdRsp.data.r2.rsp3, cmdRsp.data.r2.rsp4, 226*91f16700Schasinglulu &cmdRsp); 227*91f16700Schasinglulu 228*91f16700Schasinglulu SD_US_DELAY(2000); 229*91f16700Schasinglulu 230*91f16700Schasinglulu present = 231*91f16700Schasinglulu chal_sd_get_present_status((CHAL_HANDLE *) handle->device); 232*91f16700Schasinglulu 233*91f16700Schasinglulu if ((present & 0x00F00000) == 0x00F00000) 234*91f16700Schasinglulu rel = SD_ERROR_RECOVERABLE; 235*91f16700Schasinglulu else 236*91f16700Schasinglulu rel = SD_ERROR_NON_RECOVERABLE; 237*91f16700Schasinglulu } 238*91f16700Schasinglulu 239*91f16700Schasinglulu return rel; 240*91f16700Schasinglulu } 241*91f16700Schasinglulu 242*91f16700Schasinglulu 243*91f16700Schasinglulu /* 244*91f16700Schasinglulu * The function handles real data transmission on both DMA and 245*91f16700Schasinglulu * none DMA mode, In None DMA mode the data transfer starts 246*91f16700Schasinglulu * when the command is sent to the card, data has to be written 247*91f16700Schasinglulu * into the host controllers buffer at this time one block 248*91f16700Schasinglulu * at a time. 249*91f16700Schasinglulu * In DMA mode, the real data transfer is done by the DMA engine 250*91f16700Schasinglulu * and this functions just waits for the data transfer to complete. 251*91f16700Schasinglulu * 252*91f16700Schasinglulu */ 253*91f16700Schasinglulu int process_data_xfer(struct sd_handle *handle, uint8_t *buffer, uint32_t addr, 254*91f16700Schasinglulu uint32_t length, int dir) 255*91f16700Schasinglulu { 256*91f16700Schasinglulu if (dir == SD_XFER_HOST_TO_CARD) { 257*91f16700Schasinglulu #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE 258*91f16700Schasinglulu if (handle->device->cfg.dma == SD_DMA_OFF) { 259*91f16700Schasinglulu /* 260*91f16700Schasinglulu * In NON DMA mode, the real data xfer starts from here 261*91f16700Schasinglulu */ 262*91f16700Schasinglulu if (write_buffer(handle, length, buffer)) 263*91f16700Schasinglulu return SD_WRITE_ERROR; 264*91f16700Schasinglulu } else { 265*91f16700Schasinglulu wait_for_event(handle, 266*91f16700Schasinglulu SD4_EMMC_TOP_INTR_TXDONE_MASK | 267*91f16700Schasinglulu SD_ERR_INTERRUPTS, 268*91f16700Schasinglulu handle->device->cfg.wfe_retry); 269*91f16700Schasinglulu 270*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus == SD_OK) 271*91f16700Schasinglulu return SD_OK; 272*91f16700Schasinglulu 273*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 274*91f16700Schasinglulu return SD_WRITE_ERROR; 275*91f16700Schasinglulu } 276*91f16700Schasinglulu #else 277*91f16700Schasinglulu return SD_WRITE_ERROR; 278*91f16700Schasinglulu #endif 279*91f16700Schasinglulu } else { /* SD_XFER_CARD_TO_HOST */ 280*91f16700Schasinglulu 281*91f16700Schasinglulu if (handle->device->cfg.dma == SD_DMA_OFF) { 282*91f16700Schasinglulu /* In NON DMA mode, the real data 283*91f16700Schasinglulu * transfer starts from here 284*91f16700Schasinglulu */ 285*91f16700Schasinglulu if (read_buffer(handle, length, buffer)) 286*91f16700Schasinglulu return SD_READ_ERROR; 287*91f16700Schasinglulu 288*91f16700Schasinglulu } else { /* for DMA mode */ 289*91f16700Schasinglulu 290*91f16700Schasinglulu /* 291*91f16700Schasinglulu * once the data transmission is done 292*91f16700Schasinglulu * copy data to the host buffer. 293*91f16700Schasinglulu */ 294*91f16700Schasinglulu wait_for_event(handle, 295*91f16700Schasinglulu SD4_EMMC_TOP_INTR_TXDONE_MASK | 296*91f16700Schasinglulu SD_ERR_INTERRUPTS, 297*91f16700Schasinglulu handle->device->cfg.wfe_retry); 298*91f16700Schasinglulu 299*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus == SD_OK) 300*91f16700Schasinglulu return SD_OK; 301*91f16700Schasinglulu 302*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 303*91f16700Schasinglulu return SD_READ_ERROR; 304*91f16700Schasinglulu } 305*91f16700Schasinglulu } 306*91f16700Schasinglulu return SD_OK; 307*91f16700Schasinglulu } 308*91f16700Schasinglulu 309*91f16700Schasinglulu 310*91f16700Schasinglulu /* 311*91f16700Schasinglulu * The function sets block size for the next SD/SDIO/MMC 312*91f16700Schasinglulu * card read/write command. 313*91f16700Schasinglulu */ 314*91f16700Schasinglulu int select_blk_sz(struct sd_handle *handle, uint16_t size) 315*91f16700Schasinglulu { 316*91f16700Schasinglulu return sd_cmd16(handle, size); 317*91f16700Schasinglulu } 318*91f16700Schasinglulu 319*91f16700Schasinglulu 320*91f16700Schasinglulu /* 321*91f16700Schasinglulu * The function initializes the SD/SDIO/MMC/CEATA and detects 322*91f16700Schasinglulu * the card according to the flag of detection. 323*91f16700Schasinglulu * Once this function is called, the card is put into ready state 324*91f16700Schasinglulu * so application can do data transfer to and from the card. 325*91f16700Schasinglulu */ 326*91f16700Schasinglulu int init_card(struct sd_handle *handle, int detection) 327*91f16700Schasinglulu { 328*91f16700Schasinglulu /* 329*91f16700Schasinglulu * After Reset, eMMC comes up in 1 Bit Data Width by default. 330*91f16700Schasinglulu * Set host side to match. 331*91f16700Schasinglulu */ 332*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 333*91f16700Schasinglulu SD_BUS_DATA_WIDTH_1BIT); 334*91f16700Schasinglulu 335*91f16700Schasinglulu #ifdef USE_EMMC_FIP_TOC_CACHE 336*91f16700Schasinglulu cached_partition_block = 0; 337*91f16700Schasinglulu #endif 338*91f16700Schasinglulu handle->device->ctrl.present = 0; /* init card present to be no card */ 339*91f16700Schasinglulu 340*91f16700Schasinglulu init_mmc_card(handle); 341*91f16700Schasinglulu 342*91f16700Schasinglulu handle->device->ctrl.present = 1; /* card is detected */ 343*91f16700Schasinglulu 344*91f16700Schasinglulu /* switch the data width back */ 345*91f16700Schasinglulu if (handle->card->type != SD_CARD_MMC) 346*91f16700Schasinglulu return SD_FAIL; 347*91f16700Schasinglulu 348*91f16700Schasinglulu /* 349*91f16700Schasinglulu * Dynamically set Data Width to highest supported value. 350*91f16700Schasinglulu * Try different data width settings (highest to lowest). 351*91f16700Schasinglulu * Verify each setting by reading EXT_CSD and comparing 352*91f16700Schasinglulu * against the EXT_CSD contents previously read in call to 353*91f16700Schasinglulu * init_mmc_card() earlier. Stop at first verified data width 354*91f16700Schasinglulu * setting. 355*91f16700Schasinglulu */ 356*91f16700Schasinglulu { 357*91f16700Schasinglulu #define EXT_CSD_PROPERTIES_SECTION_START_INDEX 192 358*91f16700Schasinglulu #define EXT_CSD_PROPERTIES_SECTION_END_INDEX 511 359*91f16700Schasinglulu uint8_t buffer[EXT_CSD_SIZE]; 360*91f16700Schasinglulu #ifdef DRIVER_EMMC_ENABLE_DATA_WIDTH_8BIT 361*91f16700Schasinglulu /* Try 8 Bit Data Width */ 362*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 363*91f16700Schasinglulu SD_BUS_DATA_WIDTH_8BIT); 364*91f16700Schasinglulu if ((!set_card_data_width(handle, SD_BUS_DATA_WIDTH_8BIT)) && 365*91f16700Schasinglulu (!mmc_cmd8(handle, buffer)) && 366*91f16700Schasinglulu (!memcmp(&buffer[EXT_CSD_PROPERTIES_SECTION_START_INDEX], 367*91f16700Schasinglulu &(emmc_global_buf_ptr->u.Ext_CSD_storage[EXT_CSD_PROPERTIES_SECTION_START_INDEX]), 368*91f16700Schasinglulu EXT_CSD_PROPERTIES_SECTION_END_INDEX - EXT_CSD_PROPERTIES_SECTION_START_INDEX + 1))) 369*91f16700Schasinglulu 370*91f16700Schasinglulu return SD_OK; 371*91f16700Schasinglulu #endif 372*91f16700Schasinglulu /* Fall back to 4 Bit Data Width */ 373*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 374*91f16700Schasinglulu SD_BUS_DATA_WIDTH_4BIT); 375*91f16700Schasinglulu if ((!set_card_data_width(handle, SD_BUS_DATA_WIDTH_4BIT)) && 376*91f16700Schasinglulu (!mmc_cmd8(handle, buffer)) && 377*91f16700Schasinglulu (!memcmp(&buffer[EXT_CSD_PROPERTIES_SECTION_START_INDEX], 378*91f16700Schasinglulu &(emmc_global_buf_ptr->u.Ext_CSD_storage[EXT_CSD_PROPERTIES_SECTION_START_INDEX]), 379*91f16700Schasinglulu EXT_CSD_PROPERTIES_SECTION_END_INDEX - EXT_CSD_PROPERTIES_SECTION_START_INDEX + 1))) 380*91f16700Schasinglulu 381*91f16700Schasinglulu return SD_OK; 382*91f16700Schasinglulu 383*91f16700Schasinglulu /* Fall back to 1 Bit Data Width */ 384*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 385*91f16700Schasinglulu SD_BUS_DATA_WIDTH_1BIT); 386*91f16700Schasinglulu /* Just use 1 Bit Data Width then. */ 387*91f16700Schasinglulu if (!set_card_data_width(handle, SD_BUS_DATA_WIDTH_1BIT)) 388*91f16700Schasinglulu return SD_OK; 389*91f16700Schasinglulu 390*91f16700Schasinglulu } 391*91f16700Schasinglulu return SD_CARD_INIT_ERROR; 392*91f16700Schasinglulu } 393*91f16700Schasinglulu 394*91f16700Schasinglulu 395*91f16700Schasinglulu /* 396*91f16700Schasinglulu * The function handles MMC/CEATA card initialization. 397*91f16700Schasinglulu */ 398*91f16700Schasinglulu int init_mmc_card(struct sd_handle *handle) 399*91f16700Schasinglulu { 400*91f16700Schasinglulu uint32_t ocr = 0, newOcr, rc, limit = 0; 401*91f16700Schasinglulu uint32_t cmd1_option = 0x40300000; 402*91f16700Schasinglulu uint32_t sec_count; 403*91f16700Schasinglulu 404*91f16700Schasinglulu handle->card->type = SD_CARD_MMC; 405*91f16700Schasinglulu 406*91f16700Schasinglulu do { 407*91f16700Schasinglulu SD_US_DELAY(1000); 408*91f16700Schasinglulu newOcr = 0; 409*91f16700Schasinglulu ocr = 0; 410*91f16700Schasinglulu rc = sd_cmd1(handle, cmd1_option, &newOcr); 411*91f16700Schasinglulu limit++; 412*91f16700Schasinglulu 413*91f16700Schasinglulu if (rc == SD_OK) 414*91f16700Schasinglulu ocr = newOcr; 415*91f16700Schasinglulu 416*91f16700Schasinglulu } while (((ocr & SD_CARD_BUSY) == 0) && (limit < SD_CARD_RETRY_LIMIT)); 417*91f16700Schasinglulu 418*91f16700Schasinglulu if (limit >= SD_CARD_RETRY_LIMIT) { 419*91f16700Schasinglulu handle->card->type = SD_CARD_UNKNOWN; 420*91f16700Schasinglulu EMMC_TRACE("CMD1 Timeout: Device is not ready\n"); 421*91f16700Schasinglulu return SD_CARD_UNKNOWN; 422*91f16700Schasinglulu } 423*91f16700Schasinglulu 424*91f16700Schasinglulu /* Save the ocr register */ 425*91f16700Schasinglulu handle->device->ctrl.ocr = ocr; 426*91f16700Schasinglulu 427*91f16700Schasinglulu /* Ready State */ 428*91f16700Schasinglulu rc = sd_cmd2(handle); 429*91f16700Schasinglulu if (rc != SD_OK) { 430*91f16700Schasinglulu handle->card->type = SD_CARD_UNKNOWN; 431*91f16700Schasinglulu return SD_CARD_UNKNOWN; 432*91f16700Schasinglulu } 433*91f16700Schasinglulu 434*91f16700Schasinglulu rc = sd_cmd3(handle); 435*91f16700Schasinglulu if (rc != SD_OK) { 436*91f16700Schasinglulu handle->card->type = SD_CARD_UNKNOWN; 437*91f16700Schasinglulu return SD_CARD_UNKNOWN; 438*91f16700Schasinglulu } 439*91f16700Schasinglulu /* read CSD */ 440*91f16700Schasinglulu rc = sd_cmd9(handle, &emmc_global_vars_ptr->cardData); 441*91f16700Schasinglulu if (rc != SD_OK) { 442*91f16700Schasinglulu handle->card->type = SD_CARD_UNKNOWN; 443*91f16700Schasinglulu return SD_CARD_UNKNOWN; 444*91f16700Schasinglulu } 445*91f16700Schasinglulu 446*91f16700Schasinglulu /* Increase clock frequency according to what the card advertises */ 447*91f16700Schasinglulu EMMC_TRACE("From CSD... cardData.csd.mmc.speed = 0x%X\n", 448*91f16700Schasinglulu emmc_global_vars_ptr->cardData.csd.mmc.speed); 449*91f16700Schasinglulu process_csd_mmc_speed(handle, 450*91f16700Schasinglulu emmc_global_vars_ptr->cardData.csd.mmc.speed); 451*91f16700Schasinglulu 452*91f16700Schasinglulu /* goto transfer mode */ 453*91f16700Schasinglulu rc = sd_cmd7(handle, handle->device->ctrl.rca); 454*91f16700Schasinglulu if (rc != SD_OK) { 455*91f16700Schasinglulu handle->card->type = SD_CARD_UNKNOWN; 456*91f16700Schasinglulu return SD_CARD_UNKNOWN; 457*91f16700Schasinglulu } 458*91f16700Schasinglulu 459*91f16700Schasinglulu rc = mmc_cmd8(handle, emmc_global_buf_ptr->u.Ext_CSD_storage); 460*91f16700Schasinglulu if (rc == SD_OK) { 461*91f16700Schasinglulu /* calcul real capacity */ 462*91f16700Schasinglulu sec_count = emmc_global_buf_ptr->u.Ext_CSD_storage[212] | 463*91f16700Schasinglulu emmc_global_buf_ptr->u.Ext_CSD_storage[213] << 8 | 464*91f16700Schasinglulu emmc_global_buf_ptr->u.Ext_CSD_storage[214] << 16 | 465*91f16700Schasinglulu emmc_global_buf_ptr->u.Ext_CSD_storage[215] << 24; 466*91f16700Schasinglulu 467*91f16700Schasinglulu EMMC_TRACE("Device density = %ldMBytes\n", 468*91f16700Schasinglulu handle->card->size / (1024 * 1024)); 469*91f16700Schasinglulu 470*91f16700Schasinglulu if (sec_count > 0) { 471*91f16700Schasinglulu handle->card->size = (uint64_t)sec_count * 512; 472*91f16700Schasinglulu 473*91f16700Schasinglulu EMMC_TRACE("Updated Device density = %ldMBytes\n", 474*91f16700Schasinglulu handle->card->size / (1024 * 1024)); 475*91f16700Schasinglulu } 476*91f16700Schasinglulu 477*91f16700Schasinglulu if (sec_count > (2u * 1024 * 1024 * 1024) / 512) { 478*91f16700Schasinglulu handle->device->ctrl.ocr |= SD_CARD_HIGH_CAPACITY; 479*91f16700Schasinglulu handle->device->cfg.blockSize = 512; 480*91f16700Schasinglulu } 481*91f16700Schasinglulu 482*91f16700Schasinglulu if (handle->device->ctrl.ocr & SD_CARD_HIGH_CAPACITY) { 483*91f16700Schasinglulu EMMC_TRACE("Sector addressing\n"); 484*91f16700Schasinglulu } else { 485*91f16700Schasinglulu EMMC_TRACE("Byte addressing\n"); 486*91f16700Schasinglulu } 487*91f16700Schasinglulu 488*91f16700Schasinglulu EMMC_TRACE("Ext_CSD_storage[162]: 0x%02X Ext_CSD_storage[179]: 0x%02X\n", 489*91f16700Schasinglulu emmc_global_buf_ptr->u.Ext_CSD_storage[162], 490*91f16700Schasinglulu emmc_global_buf_ptr->u.Ext_CSD_storage[179]); 491*91f16700Schasinglulu } 492*91f16700Schasinglulu 493*91f16700Schasinglulu return handle->card->type; 494*91f16700Schasinglulu } 495*91f16700Schasinglulu 496*91f16700Schasinglulu 497*91f16700Schasinglulu /* 498*91f16700Schasinglulu * The function send reset command to the card. 499*91f16700Schasinglulu * The card will be in ready status after the reset. 500*91f16700Schasinglulu */ 501*91f16700Schasinglulu int reset_card(struct sd_handle *handle) 502*91f16700Schasinglulu { 503*91f16700Schasinglulu int res = SD_OK; 504*91f16700Schasinglulu 505*91f16700Schasinglulu /* on reset, card's RCA should return to 0 */ 506*91f16700Schasinglulu handle->device->ctrl.rca = 0; 507*91f16700Schasinglulu 508*91f16700Schasinglulu res = sd_cmd0(handle); 509*91f16700Schasinglulu 510*91f16700Schasinglulu if (res != SD_OK) 511*91f16700Schasinglulu return SD_RESET_ERROR; 512*91f16700Schasinglulu 513*91f16700Schasinglulu return res; 514*91f16700Schasinglulu } 515*91f16700Schasinglulu 516*91f16700Schasinglulu 517*91f16700Schasinglulu /* 518*91f16700Schasinglulu * The function sends command to the card and starts 519*91f16700Schasinglulu * data transmission. 520*91f16700Schasinglulu */ 521*91f16700Schasinglulu static int xfer_data(struct sd_handle *handle, 522*91f16700Schasinglulu uint32_t mode, 523*91f16700Schasinglulu uint32_t addr, uint32_t length, uint8_t *base) 524*91f16700Schasinglulu { 525*91f16700Schasinglulu int rc = SD_OK; 526*91f16700Schasinglulu 527*91f16700Schasinglulu VERBOSE("XFER: dest: 0x%" PRIx64 ", addr: 0x%x, size: 0x%x bytes\n", 528*91f16700Schasinglulu (uint64_t)base, addr, length); 529*91f16700Schasinglulu 530*91f16700Schasinglulu if ((length / handle->device->cfg.blockSize) > 1) { 531*91f16700Schasinglulu if (mode == SD_OP_READ) { 532*91f16700Schasinglulu inv_dcache_range((uintptr_t)base, (uint64_t)length); 533*91f16700Schasinglulu rc = sd_cmd18(handle, addr, length, base); 534*91f16700Schasinglulu } else { 535*91f16700Schasinglulu #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE 536*91f16700Schasinglulu flush_dcache_range((uintptr_t)base, (uint64_t)length); 537*91f16700Schasinglulu rc = sd_cmd25(handle, addr, length, base); 538*91f16700Schasinglulu #else 539*91f16700Schasinglulu rc = SD_DATA_XFER_ERROR; 540*91f16700Schasinglulu #endif 541*91f16700Schasinglulu } 542*91f16700Schasinglulu } else { 543*91f16700Schasinglulu if (mode == SD_OP_READ) { 544*91f16700Schasinglulu inv_dcache_range((uintptr_t)base, (uint64_t)length); 545*91f16700Schasinglulu rc = sd_cmd17(handle, addr, 546*91f16700Schasinglulu handle->device->cfg.blockSize, base); 547*91f16700Schasinglulu } else { 548*91f16700Schasinglulu #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE 549*91f16700Schasinglulu flush_dcache_range((uintptr_t)base, (uint64_t)length); 550*91f16700Schasinglulu rc = sd_cmd24(handle, addr, 551*91f16700Schasinglulu handle->device->cfg.blockSize, base); 552*91f16700Schasinglulu #else 553*91f16700Schasinglulu rc = SD_DATA_XFER_ERROR; 554*91f16700Schasinglulu #endif 555*91f16700Schasinglulu } 556*91f16700Schasinglulu } 557*91f16700Schasinglulu 558*91f16700Schasinglulu if (rc != SD_OK) 559*91f16700Schasinglulu return SD_DATA_XFER_ERROR; 560*91f16700Schasinglulu 561*91f16700Schasinglulu return SD_OK; 562*91f16700Schasinglulu } 563*91f16700Schasinglulu 564*91f16700Schasinglulu #ifdef INCLUDE_EMMC_DRIVER_ERASE_CODE 565*91f16700Schasinglulu int erase_card(struct sd_handle *handle, uint32_t addr, uint32_t blocks) 566*91f16700Schasinglulu { 567*91f16700Schasinglulu uint32_t end_addr; 568*91f16700Schasinglulu 569*91f16700Schasinglulu INFO("ERASE: addr: 0x%x, num of sectors: 0x%x\n", addr, blocks); 570*91f16700Schasinglulu 571*91f16700Schasinglulu if (sd_cmd35(handle, addr) != SD_OK) 572*91f16700Schasinglulu return SD_FAIL; 573*91f16700Schasinglulu 574*91f16700Schasinglulu end_addr = addr + blocks - 1; 575*91f16700Schasinglulu if (sd_cmd36(handle, end_addr) != SD_OK) 576*91f16700Schasinglulu return SD_FAIL; 577*91f16700Schasinglulu 578*91f16700Schasinglulu if (sd_cmd38(handle) != SD_OK) 579*91f16700Schasinglulu return SD_FAIL; 580*91f16700Schasinglulu 581*91f16700Schasinglulu return SD_OK; 582*91f16700Schasinglulu } 583*91f16700Schasinglulu #endif 584*91f16700Schasinglulu 585*91f16700Schasinglulu /* 586*91f16700Schasinglulu * The function reads block data from a card. 587*91f16700Schasinglulu */ 588*91f16700Schasinglulu #ifdef USE_EMMC_FIP_TOC_CACHE 589*91f16700Schasinglulu int read_block(struct sd_handle *handle, 590*91f16700Schasinglulu uint8_t *dst, uint32_t addr, uint32_t len) 591*91f16700Schasinglulu { 592*91f16700Schasinglulu int rel = SD_OK; 593*91f16700Schasinglulu 594*91f16700Schasinglulu /* 595*91f16700Schasinglulu * Avoid doing repeated reads of the partition block 596*91f16700Schasinglulu * by caching. 597*91f16700Schasinglulu */ 598*91f16700Schasinglulu if (cached_partition_block && 599*91f16700Schasinglulu addr == PARTITION_BLOCK_ADDR && 600*91f16700Schasinglulu len == CACHE_SIZE) { 601*91f16700Schasinglulu memcpy(dst, cached_block, len); 602*91f16700Schasinglulu } else { 603*91f16700Schasinglulu rel = xfer_data(handle, SD_OP_READ, addr, len, dst); 604*91f16700Schasinglulu 605*91f16700Schasinglulu if (len == CACHE_SIZE && addr == PARTITION_BLOCK_ADDR) { 606*91f16700Schasinglulu cached_partition_block = 1; 607*91f16700Schasinglulu memcpy(cached_block, dst, len); 608*91f16700Schasinglulu } 609*91f16700Schasinglulu } 610*91f16700Schasinglulu 611*91f16700Schasinglulu return rel; 612*91f16700Schasinglulu } 613*91f16700Schasinglulu #else 614*91f16700Schasinglulu int read_block(struct sd_handle *handle, 615*91f16700Schasinglulu uint8_t *dst, uint32_t addr, uint32_t len) 616*91f16700Schasinglulu { 617*91f16700Schasinglulu return xfer_data(handle, SD_OP_READ, addr, len, dst); 618*91f16700Schasinglulu } 619*91f16700Schasinglulu #endif 620*91f16700Schasinglulu 621*91f16700Schasinglulu #ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE 622*91f16700Schasinglulu 623*91f16700Schasinglulu /* 624*91f16700Schasinglulu * The function writes block data to a card. 625*91f16700Schasinglulu */ 626*91f16700Schasinglulu int write_block(struct sd_handle *handle, 627*91f16700Schasinglulu uint8_t *src, uint32_t addr, uint32_t len) 628*91f16700Schasinglulu { 629*91f16700Schasinglulu int rel = SD_OK; 630*91f16700Schasinglulu 631*91f16700Schasinglulu /* 632*91f16700Schasinglulu * Current HC has problem to get response of cmd16 after cmd12, 633*91f16700Schasinglulu * the delay is necessary to sure the next cmd16 will not be timed out. 634*91f16700Schasinglulu * The delay has to be at least 4 ms. 635*91f16700Schasinglulu * The code removed cmd16 and use cmd13 to get card status before 636*91f16700Schasinglulu * sending cmd18 or cmd25 to make sure the card is ready and thus 637*91f16700Schasinglulu * no need to have delay here. 638*91f16700Schasinglulu */ 639*91f16700Schasinglulu 640*91f16700Schasinglulu rel = xfer_data(handle, SD_OP_WRITE, addr, len, src); 641*91f16700Schasinglulu 642*91f16700Schasinglulu EMMC_TRACE("wr_blk addr:0x%08X src:0x%08X len:0x%08X result:%d\n", 643*91f16700Schasinglulu addr, src, len, rel); 644*91f16700Schasinglulu 645*91f16700Schasinglulu return rel; 646*91f16700Schasinglulu } 647*91f16700Schasinglulu 648*91f16700Schasinglulu 649*91f16700Schasinglulu /* 650*91f16700Schasinglulu * The function is called to write one block data directly to 651*91f16700Schasinglulu * a card's data buffer. 652*91f16700Schasinglulu * it is used in Non-DMA mode for card data transmission. 653*91f16700Schasinglulu */ 654*91f16700Schasinglulu int write_buffer(struct sd_handle *handle, uint32_t length, uint8_t *data) 655*91f16700Schasinglulu { 656*91f16700Schasinglulu uint32_t rem, blockSize, event; 657*91f16700Schasinglulu uint8_t *pData = data; 658*91f16700Schasinglulu 659*91f16700Schasinglulu blockSize = handle->device->cfg.blockSize; 660*91f16700Schasinglulu rem = length; 661*91f16700Schasinglulu 662*91f16700Schasinglulu if (rem == 0) 663*91f16700Schasinglulu return SD_OK; 664*91f16700Schasinglulu 665*91f16700Schasinglulu while (rem > 0) { 666*91f16700Schasinglulu 667*91f16700Schasinglulu event = wait_for_event(handle, 668*91f16700Schasinglulu SD4_EMMC_TOP_INTR_BWRDY_MASK | 669*91f16700Schasinglulu SD_ERR_INTERRUPTS, 670*91f16700Schasinglulu handle->device->cfg.wfe_retry); 671*91f16700Schasinglulu 672*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus) { 673*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 674*91f16700Schasinglulu return SD_WRITE_ERROR; 675*91f16700Schasinglulu } 676*91f16700Schasinglulu 677*91f16700Schasinglulu if (rem >= blockSize) 678*91f16700Schasinglulu chal_sd_write_buffer((CHAL_HANDLE *) handle->device, 679*91f16700Schasinglulu blockSize, pData); 680*91f16700Schasinglulu else 681*91f16700Schasinglulu chal_sd_write_buffer((CHAL_HANDLE *) handle->device, 682*91f16700Schasinglulu rem, pData); 683*91f16700Schasinglulu 684*91f16700Schasinglulu if (rem > blockSize) { 685*91f16700Schasinglulu rem -= blockSize; 686*91f16700Schasinglulu pData += blockSize; 687*91f16700Schasinglulu } else { 688*91f16700Schasinglulu pData += rem; 689*91f16700Schasinglulu rem = 0; 690*91f16700Schasinglulu } 691*91f16700Schasinglulu } 692*91f16700Schasinglulu 693*91f16700Schasinglulu if ((event & SD4_EMMC_TOP_INTR_TXDONE_MASK) != 694*91f16700Schasinglulu SD4_EMMC_TOP_INTR_TXDONE_MASK) { 695*91f16700Schasinglulu event = wait_for_event(handle, 696*91f16700Schasinglulu SD4_EMMC_TOP_INTR_TXDONE_MASK | 697*91f16700Schasinglulu SD_ERR_INTERRUPTS, 698*91f16700Schasinglulu handle->device->cfg.wfe_retry); 699*91f16700Schasinglulu 700*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus != SD_OK) { 701*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 702*91f16700Schasinglulu return SD_WRITE_ERROR; 703*91f16700Schasinglulu } 704*91f16700Schasinglulu } else { 705*91f16700Schasinglulu handle->device->ctrl.eventList &= ~SD4_EMMC_TOP_INTR_TXDONE_MASK; 706*91f16700Schasinglulu } 707*91f16700Schasinglulu 708*91f16700Schasinglulu return SD_OK; 709*91f16700Schasinglulu } 710*91f16700Schasinglulu #endif /* INCLUDE_EMMC_DRIVER_WRITE_CODE */ 711*91f16700Schasinglulu 712*91f16700Schasinglulu 713*91f16700Schasinglulu /* 714*91f16700Schasinglulu * The function is called to read maximal one block data 715*91f16700Schasinglulu * directly from a card 716*91f16700Schasinglulu * It is used in Non-DMA mode for card data transmission. 717*91f16700Schasinglulu */ 718*91f16700Schasinglulu int read_buffer(struct sd_handle *handle, uint32_t length, uint8_t *data) 719*91f16700Schasinglulu { 720*91f16700Schasinglulu uint32_t rem, blockSize, event = 0; 721*91f16700Schasinglulu uint8_t *pData = data; 722*91f16700Schasinglulu 723*91f16700Schasinglulu blockSize = handle->device->cfg.blockSize; 724*91f16700Schasinglulu rem = length; 725*91f16700Schasinglulu 726*91f16700Schasinglulu if (rem == 0) 727*91f16700Schasinglulu return SD_OK; 728*91f16700Schasinglulu 729*91f16700Schasinglulu while (rem > 0) { 730*91f16700Schasinglulu event = wait_for_event(handle, 731*91f16700Schasinglulu SD4_EMMC_TOP_INTR_BRRDY_MASK | 732*91f16700Schasinglulu SD_ERR_INTERRUPTS, 733*91f16700Schasinglulu handle->device->cfg.wfe_retry); 734*91f16700Schasinglulu 735*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus) { 736*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 737*91f16700Schasinglulu return SD_READ_ERROR; 738*91f16700Schasinglulu } 739*91f16700Schasinglulu 740*91f16700Schasinglulu if (rem >= blockSize) 741*91f16700Schasinglulu chal_sd_read_buffer((CHAL_HANDLE *) handle->device, 742*91f16700Schasinglulu blockSize, pData); 743*91f16700Schasinglulu else 744*91f16700Schasinglulu chal_sd_read_buffer((CHAL_HANDLE *) handle->device, rem, 745*91f16700Schasinglulu pData); 746*91f16700Schasinglulu 747*91f16700Schasinglulu if (rem > blockSize) { 748*91f16700Schasinglulu rem -= blockSize; 749*91f16700Schasinglulu pData += blockSize; 750*91f16700Schasinglulu } else { 751*91f16700Schasinglulu pData += rem; 752*91f16700Schasinglulu rem = 0; 753*91f16700Schasinglulu } 754*91f16700Schasinglulu } 755*91f16700Schasinglulu 756*91f16700Schasinglulu /* In case, there are extra data in the SD FIFO, just dump them. */ 757*91f16700Schasinglulu chal_sd_dump_fifo((CHAL_HANDLE *) handle->device); 758*91f16700Schasinglulu 759*91f16700Schasinglulu if ((event & SD4_EMMC_TOP_INTR_TXDONE_MASK) != 760*91f16700Schasinglulu SD4_EMMC_TOP_INTR_TXDONE_MASK) { 761*91f16700Schasinglulu event = wait_for_event(handle, SD4_EMMC_TOP_INTR_TXDONE_MASK, 762*91f16700Schasinglulu handle->device->cfg.wfe_retry); 763*91f16700Schasinglulu 764*91f16700Schasinglulu if (handle->device->ctrl.cmdStatus) { 765*91f16700Schasinglulu check_error(handle, handle->device->ctrl.cmdStatus); 766*91f16700Schasinglulu return SD_READ_ERROR; 767*91f16700Schasinglulu } 768*91f16700Schasinglulu } else { 769*91f16700Schasinglulu handle->device->ctrl.eventList &= ~SD4_EMMC_TOP_INTR_TXDONE_MASK; 770*91f16700Schasinglulu } 771*91f16700Schasinglulu 772*91f16700Schasinglulu return SD_OK; 773*91f16700Schasinglulu } 774*91f16700Schasinglulu 775*91f16700Schasinglulu 776*91f16700Schasinglulu /* 777*91f16700Schasinglulu * Error handling routine. 778*91f16700Schasinglulu * The function just reset the DAT 779*91f16700Schasinglulu * and CMD line if an error occures during data transmission. 780*91f16700Schasinglulu */ 781*91f16700Schasinglulu int check_error(struct sd_handle *handle, uint32_t ints) 782*91f16700Schasinglulu { 783*91f16700Schasinglulu uint32_t rel; 784*91f16700Schasinglulu 785*91f16700Schasinglulu chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device, 786*91f16700Schasinglulu SD_ERR_INTERRUPTS, 0); 787*91f16700Schasinglulu 788*91f16700Schasinglulu if (ints & SD4_EMMC_TOP_INTR_CMDERROR_MASK) { 789*91f16700Schasinglulu 790*91f16700Schasinglulu chal_sd_reset_line((CHAL_HANDLE *) handle->device, 791*91f16700Schasinglulu SD4_EMMC_TOP_CTRL1_CMDRST_MASK); 792*91f16700Schasinglulu rel = abort_err(handle); 793*91f16700Schasinglulu 794*91f16700Schasinglulu chal_sd_reset_line((CHAL_HANDLE *) handle->device, 795*91f16700Schasinglulu SD4_EMMC_TOP_CTRL1_DATRST_MASK); 796*91f16700Schasinglulu chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device, 797*91f16700Schasinglulu SD_ERR_INTERRUPTS, 1); 798*91f16700Schasinglulu 799*91f16700Schasinglulu return (rel == SD_ERROR_NON_RECOVERABLE) ? 800*91f16700Schasinglulu SD_ERROR_NON_RECOVERABLE : SD_ERROR_RECOVERABLE; 801*91f16700Schasinglulu } else { 802*91f16700Schasinglulu rel = err_recovery(handle, ints); 803*91f16700Schasinglulu } 804*91f16700Schasinglulu 805*91f16700Schasinglulu chal_sd_set_irq_signal((CHAL_HANDLE *) handle->device, 806*91f16700Schasinglulu SD_ERR_INTERRUPTS, 1); 807*91f16700Schasinglulu 808*91f16700Schasinglulu return rel; 809*91f16700Schasinglulu } 810*91f16700Schasinglulu 811*91f16700Schasinglulu 812*91f16700Schasinglulu /* 813*91f16700Schasinglulu * Error recovery routine. 814*91f16700Schasinglulu * Try to recover from the error. 815*91f16700Schasinglulu */ 816*91f16700Schasinglulu static int err_recovery(struct sd_handle *handle, uint32_t errors) 817*91f16700Schasinglulu { 818*91f16700Schasinglulu uint32_t rel = 0; 819*91f16700Schasinglulu 820*91f16700Schasinglulu /* 821*91f16700Schasinglulu * In case of timeout error, the cmd line and data line maybe 822*91f16700Schasinglulu * still active or stuck at atcitve so it is needed to reset 823*91f16700Schasinglulu * either data line or cmd line to make sure a new cmd can be sent. 824*91f16700Schasinglulu */ 825*91f16700Schasinglulu 826*91f16700Schasinglulu if (errors & SD_CMD_ERROR_INT) 827*91f16700Schasinglulu chal_sd_reset_line((CHAL_HANDLE *) handle->device, 828*91f16700Schasinglulu SD4_EMMC_TOP_CTRL1_CMDRST_MASK); 829*91f16700Schasinglulu 830*91f16700Schasinglulu if (errors & SD_DAT_ERROR_INT) 831*91f16700Schasinglulu chal_sd_reset_line((CHAL_HANDLE *) handle->device, 832*91f16700Schasinglulu SD4_EMMC_TOP_CTRL1_DATRST_MASK); 833*91f16700Schasinglulu 834*91f16700Schasinglulu /* Abort transaction by sending out stop command */ 835*91f16700Schasinglulu if ((handle->device->ctrl.cmdIndex == 18) || 836*91f16700Schasinglulu (handle->device->ctrl.cmdIndex == 25)) 837*91f16700Schasinglulu rel = abort_err(handle); 838*91f16700Schasinglulu 839*91f16700Schasinglulu return rel; 840*91f16700Schasinglulu } 841*91f16700Schasinglulu 842*91f16700Schasinglulu 843*91f16700Schasinglulu /* 844*91f16700Schasinglulu * The function is called to read one block data directly from a card. 845*91f16700Schasinglulu * It is used in Non-DMA mode for card data transmission. 846*91f16700Schasinglulu */ 847*91f16700Schasinglulu int process_cmd_response(struct sd_handle *handle, 848*91f16700Schasinglulu uint32_t cmdIndex, 849*91f16700Schasinglulu uint32_t rsp0, 850*91f16700Schasinglulu uint32_t rsp1, 851*91f16700Schasinglulu uint32_t rsp2, uint32_t rsp3, struct sd_resp *resp) 852*91f16700Schasinglulu { 853*91f16700Schasinglulu int result = SD_OK; 854*91f16700Schasinglulu 855*91f16700Schasinglulu /* R6 */ 856*91f16700Schasinglulu uint32_t rca = (rsp0 >> 16) & 0xffff; 857*91f16700Schasinglulu uint32_t cardStatus = rsp0; 858*91f16700Schasinglulu 859*91f16700Schasinglulu /* R4 */ 860*91f16700Schasinglulu uint32_t cBit = (rsp0 >> 31) & 0x1; 861*91f16700Schasinglulu uint32_t funcs = (rsp0 >> 28) & 0x7; 862*91f16700Schasinglulu uint32_t memPresent = (rsp0 >> 27) & 0x1; 863*91f16700Schasinglulu 864*91f16700Schasinglulu resp->r1 = 0x3f; 865*91f16700Schasinglulu resp->cardStatus = cardStatus; 866*91f16700Schasinglulu 867*91f16700Schasinglulu if (cmdIndex == SD_CMD_IO_SEND_OP_COND) { 868*91f16700Schasinglulu resp->data.r4.cardReady = cBit; 869*91f16700Schasinglulu resp->data.r4.funcs = funcs; 870*91f16700Schasinglulu resp->data.r4.memPresent = memPresent; 871*91f16700Schasinglulu resp->data.r4.ocr = cardStatus; 872*91f16700Schasinglulu } 873*91f16700Schasinglulu 874*91f16700Schasinglulu if (cmdIndex == SD_CMD_MMC_SET_RCA) { 875*91f16700Schasinglulu resp->data.r6.rca = rca; 876*91f16700Schasinglulu resp->data.r6.cardStatus = cardStatus & 0xFFFF; 877*91f16700Schasinglulu } 878*91f16700Schasinglulu 879*91f16700Schasinglulu if (cmdIndex == SD_CMD_SELECT_DESELECT_CARD) { 880*91f16700Schasinglulu resp->data.r7.rca = rca; 881*91f16700Schasinglulu } 882*91f16700Schasinglulu 883*91f16700Schasinglulu if (cmdIndex == SD_CMD_IO_RW_DIRECT) { 884*91f16700Schasinglulu if (((rsp0 >> 16) & 0xffff) != 0) 885*91f16700Schasinglulu result = SD_CMD_ERR_INVALID_RESPONSE; 886*91f16700Schasinglulu 887*91f16700Schasinglulu resp->data.r5.data = rsp0 & 0xff; 888*91f16700Schasinglulu } 889*91f16700Schasinglulu 890*91f16700Schasinglulu if (cmdIndex == SD_CMD_IO_RW_EXTENDED) { 891*91f16700Schasinglulu if (((rsp0 >> 16) & 0xffff) != 0) 892*91f16700Schasinglulu result = SD_CMD_ERR_INVALID_RESPONSE; 893*91f16700Schasinglulu 894*91f16700Schasinglulu resp->data.r5.data = rsp0 & 0xff; 895*91f16700Schasinglulu } 896*91f16700Schasinglulu 897*91f16700Schasinglulu if (cmdIndex == SD_ACMD_SD_SEND_OP_COND || 898*91f16700Schasinglulu cmdIndex == SD_CMD_SEND_OPCOND) 899*91f16700Schasinglulu resp->data.r3.ocr = cardStatus; 900*91f16700Schasinglulu 901*91f16700Schasinglulu if (cmdIndex == SD_CMD_SEND_CSD || 902*91f16700Schasinglulu cmdIndex == SD_CMD_SEND_CID || 903*91f16700Schasinglulu cmdIndex == SD_CMD_ALL_SEND_CID) { 904*91f16700Schasinglulu resp->data.r2.rsp4 = rsp3; 905*91f16700Schasinglulu resp->data.r2.rsp3 = rsp2; 906*91f16700Schasinglulu resp->data.r2.rsp2 = rsp1; 907*91f16700Schasinglulu resp->data.r2.rsp1 = rsp0; 908*91f16700Schasinglulu } 909*91f16700Schasinglulu 910*91f16700Schasinglulu if ((cmdIndex == SD_CMD_READ_EXT_CSD) && 911*91f16700Schasinglulu (handle->card->type == SD_CARD_SD)) { 912*91f16700Schasinglulu if ((resp->cardStatus & 0xAA) != 0xAA) { 913*91f16700Schasinglulu result = SD_CMD_ERR_INVALID_RESPONSE; 914*91f16700Schasinglulu } 915*91f16700Schasinglulu } 916*91f16700Schasinglulu 917*91f16700Schasinglulu return result; 918*91f16700Schasinglulu } 919*91f16700Schasinglulu 920*91f16700Schasinglulu 921*91f16700Schasinglulu /* 922*91f16700Schasinglulu * The function sets DMA buffer and data length, process 923*91f16700Schasinglulu * block size and the number of blocks to be transferred. 924*91f16700Schasinglulu * It returns the DMA buffer address. 925*91f16700Schasinglulu * It copies dma data from user buffer to the DMA buffer 926*91f16700Schasinglulu * if the operation is to write data to the SD card. 927*91f16700Schasinglulu */ 928*91f16700Schasinglulu void data_xfer_setup(struct sd_handle *handle, uint8_t *data, uint32_t length, 929*91f16700Schasinglulu int dir) 930*91f16700Schasinglulu { 931*91f16700Schasinglulu chal_sd_setup_xfer((CHAL_HANDLE *)handle->device, data, length, dir); 932*91f16700Schasinglulu } 933*91f16700Schasinglulu 934*91f16700Schasinglulu 935*91f16700Schasinglulu /* 936*91f16700Schasinglulu * The function does soft reset the host SD controller. After 937*91f16700Schasinglulu * the function call all host controller's register are reset 938*91f16700Schasinglulu * to default vallue; 939*91f16700Schasinglulu * 940*91f16700Schasinglulu * Note This function only resets the host controller it does not 941*91f16700Schasinglulu * reset the controller's handler. 942*91f16700Schasinglulu */ 943*91f16700Schasinglulu int reset_host_ctrl(struct sd_handle *handle) 944*91f16700Schasinglulu { 945*91f16700Schasinglulu chal_sd_stop(); 946*91f16700Schasinglulu 947*91f16700Schasinglulu return SD_OK; 948*91f16700Schasinglulu } 949*91f16700Schasinglulu 950*91f16700Schasinglulu static void pstate_log(struct sd_handle *handle) 951*91f16700Schasinglulu { 952*91f16700Schasinglulu ERROR("PSTATE: 0x%x\n", mmio_read_32 953*91f16700Schasinglulu (handle->device->ctrl.sdRegBaseAddr + 954*91f16700Schasinglulu SD4_EMMC_TOP_PSTATE_SD4_OFFSET)); 955*91f16700Schasinglulu ERROR("ERRSTAT: 0x%x\n", mmio_read_32 956*91f16700Schasinglulu (handle->device->ctrl.sdRegBaseAddr + 957*91f16700Schasinglulu SD4_EMMC_TOP_ERRSTAT_OFFSET)); 958*91f16700Schasinglulu } 959*91f16700Schasinglulu 960*91f16700Schasinglulu /* 961*91f16700Schasinglulu * The function waits for one or a group of interrupts specified 962*91f16700Schasinglulu * by mask. The function returns if any one the interrupt status 963*91f16700Schasinglulu * is set. If interrupt mode is not enabled then it will poll 964*91f16700Schasinglulu * the interrupt status register until a interrupt status is set 965*91f16700Schasinglulu * an error interrupt happens. If interrupt mode is enabled then 966*91f16700Schasinglulu * this function should be called after the interrupt 967*91f16700Schasinglulu * is received by ISR routine. 968*91f16700Schasinglulu */ 969*91f16700Schasinglulu uint32_t wait_for_event(struct sd_handle *handle, 970*91f16700Schasinglulu uint32_t mask, uint32_t retry) 971*91f16700Schasinglulu { 972*91f16700Schasinglulu uint32_t regval, cmd12, time = 0; 973*91f16700Schasinglulu 974*91f16700Schasinglulu handle->device->ctrl.cmdStatus = 0; /* no error */ 975*91f16700Schasinglulu EMMC_TRACE("%s %d mask:0x%x timeout:%d irq_status:0x%x\n", 976*91f16700Schasinglulu __func__, __LINE__, mask, retry, 977*91f16700Schasinglulu chal_sd_get_irq_status((CHAL_HANDLE *)handle->device)); 978*91f16700Schasinglulu 979*91f16700Schasinglulu /* Polling mode */ 980*91f16700Schasinglulu do { 981*91f16700Schasinglulu regval = chal_sd_get_irq_status((CHAL_HANDLE *)handle->device); 982*91f16700Schasinglulu 983*91f16700Schasinglulu if (regval & SD4_EMMC_TOP_INTR_DMAIRQ_MASK) { 984*91f16700Schasinglulu chal_sd_set_dma_addr((CHAL_HANDLE *)handle->device, 985*91f16700Schasinglulu (uintptr_t) 986*91f16700Schasinglulu chal_sd_get_dma_addr((CHAL_HANDLE *) 987*91f16700Schasinglulu handle->device)); 988*91f16700Schasinglulu chal_sd_clear_irq((CHAL_HANDLE *)handle->device, 989*91f16700Schasinglulu SD4_EMMC_TOP_INTR_DMAIRQ_MASK); 990*91f16700Schasinglulu } 991*91f16700Schasinglulu 992*91f16700Schasinglulu if (time++ > retry) { 993*91f16700Schasinglulu ERROR("EMMC: No response (cmd%d) after %dus.\n", 994*91f16700Schasinglulu handle->device->ctrl.cmdIndex, 995*91f16700Schasinglulu time * EMMC_WFE_RETRY_DELAY_US); 996*91f16700Schasinglulu handle->device->ctrl.cmdStatus = SD_CMD_MISSING; 997*91f16700Schasinglulu pstate_log(handle); 998*91f16700Schasinglulu ERROR("EMMC: INT[0x%x]\n", regval); 999*91f16700Schasinglulu break; 1000*91f16700Schasinglulu } 1001*91f16700Schasinglulu 1002*91f16700Schasinglulu if (regval & SD4_EMMC_TOP_INTR_CTOERR_MASK) { 1003*91f16700Schasinglulu ERROR("EMMC: Cmd%d timeout INT[0x%x]\n", 1004*91f16700Schasinglulu handle->device->ctrl.cmdIndex, regval); 1005*91f16700Schasinglulu handle->device->ctrl.cmdStatus = 1006*91f16700Schasinglulu SD4_EMMC_TOP_INTR_CTOERR_MASK; 1007*91f16700Schasinglulu pstate_log(handle); 1008*91f16700Schasinglulu break; 1009*91f16700Schasinglulu } 1010*91f16700Schasinglulu if (regval & SD_CMD_ERROR_FLAGS) { 1011*91f16700Schasinglulu ERROR("EMMC: Cmd%d error INT[0x%x]\n", 1012*91f16700Schasinglulu handle->device->ctrl.cmdIndex, regval); 1013*91f16700Schasinglulu handle->device->ctrl.cmdStatus = SD_CMD_ERROR_FLAGS; 1014*91f16700Schasinglulu pstate_log(handle); 1015*91f16700Schasinglulu break; 1016*91f16700Schasinglulu } 1017*91f16700Schasinglulu 1018*91f16700Schasinglulu cmd12 = chal_sd_get_atuo12_error((CHAL_HANDLE *)handle->device); 1019*91f16700Schasinglulu if (cmd12) { 1020*91f16700Schasinglulu ERROR("EMMC: Cmd%d auto cmd12 err:0x%x\n", 1021*91f16700Schasinglulu handle->device->ctrl.cmdIndex, cmd12); 1022*91f16700Schasinglulu handle->device->ctrl.cmdStatus = cmd12; 1023*91f16700Schasinglulu pstate_log(handle); 1024*91f16700Schasinglulu break; 1025*91f16700Schasinglulu } 1026*91f16700Schasinglulu 1027*91f16700Schasinglulu if (SD_DATA_ERROR_FLAGS & regval) { 1028*91f16700Schasinglulu ERROR("EMMC: Data for cmd%d error, INT[0x%x]\n", 1029*91f16700Schasinglulu handle->device->ctrl.cmdIndex, regval); 1030*91f16700Schasinglulu handle->device->ctrl.cmdStatus = 1031*91f16700Schasinglulu (SD_DATA_ERROR_FLAGS & regval); 1032*91f16700Schasinglulu pstate_log(handle); 1033*91f16700Schasinglulu break; 1034*91f16700Schasinglulu } 1035*91f16700Schasinglulu 1036*91f16700Schasinglulu if ((regval & mask) == 0) 1037*91f16700Schasinglulu udelay(EMMC_WFE_RETRY_DELAY_US); 1038*91f16700Schasinglulu 1039*91f16700Schasinglulu } while ((regval & mask) == 0); 1040*91f16700Schasinglulu 1041*91f16700Schasinglulu /* clear the interrupt since it is processed */ 1042*91f16700Schasinglulu chal_sd_clear_irq((CHAL_HANDLE *)handle->device, (regval & mask)); 1043*91f16700Schasinglulu 1044*91f16700Schasinglulu return (regval & mask); 1045*91f16700Schasinglulu } 1046*91f16700Schasinglulu 1047*91f16700Schasinglulu int32_t set_config(struct sd_handle *handle, uint32_t speed, uint32_t retry, 1048*91f16700Schasinglulu uint32_t dma, uint32_t dmaBound, uint32_t blkSize, 1049*91f16700Schasinglulu uint32_t wfe_retry) 1050*91f16700Schasinglulu { 1051*91f16700Schasinglulu int32_t rel = 0; 1052*91f16700Schasinglulu 1053*91f16700Schasinglulu if (handle == NULL) 1054*91f16700Schasinglulu return SD_FAIL; 1055*91f16700Schasinglulu 1056*91f16700Schasinglulu handle->device->cfg.wfe_retry = wfe_retry; 1057*91f16700Schasinglulu 1058*91f16700Schasinglulu rel = chal_sd_config((CHAL_HANDLE *)handle->device, speed, retry, 1059*91f16700Schasinglulu dmaBound, blkSize, dma); 1060*91f16700Schasinglulu return rel; 1061*91f16700Schasinglulu 1062*91f16700Schasinglulu } 1063*91f16700Schasinglulu 1064*91f16700Schasinglulu int mmc_cmd1(struct sd_handle *handle) 1065*91f16700Schasinglulu { 1066*91f16700Schasinglulu uint32_t newOcr, res; 1067*91f16700Schasinglulu uint32_t cmd1_option = MMC_OCR_OP_VOLT | MMC_OCR_SECTOR_ACCESS_MODE; 1068*91f16700Schasinglulu 1069*91f16700Schasinglulu /* 1070*91f16700Schasinglulu * After Reset, eMMC comes up in 1 Bit Data Width by default. 1071*91f16700Schasinglulu * Set host side to match. 1072*91f16700Schasinglulu */ 1073*91f16700Schasinglulu chal_sd_config_bus_width((CHAL_HANDLE *) handle->device, 1074*91f16700Schasinglulu SD_BUS_DATA_WIDTH_1BIT); 1075*91f16700Schasinglulu 1076*91f16700Schasinglulu #ifdef USE_EMMC_FIP_TOC_CACHE 1077*91f16700Schasinglulu cached_partition_block = 0; 1078*91f16700Schasinglulu #endif 1079*91f16700Schasinglulu handle->device->ctrl.present = 0; /* init card present to be no card */ 1080*91f16700Schasinglulu 1081*91f16700Schasinglulu handle->card->type = SD_CARD_MMC; 1082*91f16700Schasinglulu 1083*91f16700Schasinglulu res = sd_cmd1(handle, cmd1_option, &newOcr); 1084*91f16700Schasinglulu 1085*91f16700Schasinglulu if (res != SD_OK) { 1086*91f16700Schasinglulu EMMC_TRACE("CMD1 Timeout: Device is not ready\n"); 1087*91f16700Schasinglulu res = SD_CARD_UNKNOWN; 1088*91f16700Schasinglulu } 1089*91f16700Schasinglulu return res; 1090*91f16700Schasinglulu } 1091