📄 mmc_main.c.bak
字号:
state = MMCSD_TRANSFER; ret = mmcsd_check_status(device,&state); if(ret) { mmcsd_free_buffer(device); FAILED("Check Status failed."); goto dma_write_end; } i = 10; while(i--) { } if(ret) { mmcsd_free_buffer(device); FAILED("Set Bus Width Failed!"); goto dma_write_end; } /******************************************************************** * Begin to write *******************************************************************/ addr = device->addr; //TRACE("6)address is 0x%lx(sector 0x%lx, %ld),blk number is %ld", // addr,addr/512,addr/512,device->cur_num); /************************************************************** * Mask Interrupt **************************************************************/ //TRACE("Disable Interrupt"); _MMCSD_Mask_interrupt(0x78,&int_mask,device->port); /* * DMA Preparation */ /************************************************************** * Config DMA **************************************************************/// TRACE("DMA Configuration"); ret = mmcsd_DmaStartTransfer(device,#ifdef MMCSD_DMA_ENDIAN_ERR device->tmp_pool[0],#else device->buff_pool[0],#endif MMCSD_MINIMUM_BLK_SIZE*(device->cur_num), MMCSD_WRITE, device->bit_width); if(ret != MMCSD_OK) { FAILED("mmcsd_DmaStartTransfer failed"); goto dma_write_end; } /************************************************************** * Set Command **************************************************************/ state =MMCSD_TRANSFER; ret = mmcsd_check_status(device,&state); if(ret) { FAILED("Check Status failed."); } // TRACE("Set Command (24/25)"); _MMCSD_StopClk(device->port); if(device->cur_num == 1) { if(device->bit_width == 0) { _MMCSD_SetCmd(MMCSD_CMD24,addr,MMCSDB_DATEN|MMCSDB_R1|MMCSDB_WRRD,device->port); } else { _MMCSD_SetCmd(MMCSD_CMD24,addr,MMCSD_BUS_4BIT|MMCSDB_DATEN|MMCSDB_R1|MMCSDB_WRRD,device->port); } } else { if(device->bit_width == 0) { _MMCSD_SetCmd(MMCSD_CMD25,addr,MMCSDB_DATEN|MMCSDB_R1|MMCSDB_WRRD,device->port); } else { _MMCSD_SetCmd(MMCSD_CMD25,addr,MMCSD_BUS_4BIT|MMCSDB_DATEN|MMCSDB_R1|MMCSDB_WRRD,device->port); } }// TRACE("Start Clk... Wait CMD Done."); ret = MMCSD_OK; _MMCSD_StartClk(device->port); //if((ret=_MMCSD_irq_WaitUntil(MMCSD_CMD_DONE,MMCSD_CMD25,100))!=0) if((ret=_MMCSD_WaitUntil(MMCSD_CMD_DONE,device->port))!=MMCSD_OK) { FAILED("WaitCMDDone ret 0x%lx",(unsigned long)ret); goto dma_write_end; }#ifdef CONFIG_ARCH_MX2ADS if(device->port == 1) g_mmcsd_status = mmcsdr_status_2; else g_mmcsd_status = mmcsdr_status;#endif#ifdef CONFIG_ARCH_MX1ADS g_mmcsd_status = mmcsdr_status;#endif if((g_mmcsd_status&0x40)==0) { FAILED("FIFO is not empty 0x%lx",(unsigned long)ret); goto dma_write_end; } /************************************************************** * Enable DMA **************************************************************/// TRACE("Enable DMA..");// _MMCSD_StartClk(); g_mmcsd_cmd[device->port] = 0xFF;#ifdef CONFIG_ARCH_MX1ADS enable_dma(device->dma_channel);#endif#ifdef CONFIG_ARCH_MX2ADS mx_dma_start(device->dma_channel);#endif timeleft = interruptible_sleep_on_timeout(&(device->select_wait),100); if(!timeleft) { FAILED("write failed, Timeout!"); #ifdef CONFIG_ARCH_MX1ADS disable_dma(device->dma_channel); #endif #ifdef CONFIG_ARCH_MX2ADS _reg_DMA_CCR(device->dma_channel) &= 0xFFFFFFFE; #endif g_mmcsd_cmd[device->port] = 0x0; ret = MMCSD_ERROR; goto dma_write_end; }#ifdef CONFIG_ARCH_MX1ADS disable_dma(device->dma_channel);#endif#ifdef CONFIG_ARCH_MX2ADS disable_dma(device->dma_channel);// _reg_DMA_CCR(device->dma_channel) &= 0xFFFFFFFE; #endif g_mmcsd_cmd[device->port] = 0x0;// TRACE("Waked up "); status = device->result;// TRACE("waked up! status = %lx",status); counter = 0xFFFF; while(counter--) {#ifdef CONFIG_ARCH_MX2ADS if(device->port == 1) g_mmcsd_status = mmcsdr_status_2; else g_mmcsd_status = mmcsdr_status;#endif#ifdef CONFIG_ARCH_MX1ADS g_mmcsd_status = mmcsdr_status;#endif TRACE("g_mmcsd_status is 0x%0lX (for TRANS-DONE)",(unsigned long)g_mmcsd_status); if((g_mmcsd_status&0x1007)==0) { mmcsd_delay(0xFFFF); continue; } if(g_mmcsd_status&0x0007) { ret = MMCSD_ERROR; FAILED("status is 0x%lx",(unsigned long)g_mmcsd_status); goto dma_write_end; } ret = MMCSD_OK; break; } if(counter == 0) { ret = MMCSD_ERROR; FAILED("TIME OUT.."); goto dma_write_end; } /******************************************************* * Write Done *******************************************************/ if(ret==MMCSD_OK) { counter = 0xFFFF; while(counter--) {#ifdef CONFIG_ARCH_MX2ADS if(device->port == 1) g_mmcsd_status = mmcsdr_status_2; else g_mmcsd_status = mmcsdr_status;#endif#ifdef CONFIG_ARCH_MX1ADS g_mmcsd_status = mmcsdr_status;#endif TRACE("g_mmcsd_status is 0x%0lX (for Write-DONE)",(unsigned long)g_mmcsd_status); if(g_mmcsd_status&0x0004) { TRACE("8)ERROR!"); ret = MMCSD_ERROR; break; } else if((g_mmcsd_status&0x1000)!=0) { ret = MMCSD_OK; break; } mmcsd_delay(0xFFFF); } if(counter==0) { ret = MMCSD_ERROR; FAILED("Time Out for Write-DONE."); goto dma_write_end; } }dma_write_end: g_mmcsd_cmd[device->port] = 0; if((ret == MMCSD_OK)&&(device->cur_num>1)) { MMCSD_Responses rp;// TRACE("CMD12 -- Last Block."); ret2=_MMCSD_NoDataCommand(MMCSD_CMD12,0,MMCSDB_R1,device->port); _MMCSD_GetResponses(&rp,MMCSDB_R1,device->port); }// INFO("Check Status.....(after cmd12)"); state =MMCSD_TRANSFER; mmcsd_check_status(device,&state); if(int_mask != 0xFFFFFFFF) {// TRACE("Enable Interrupt."); _MMCSD_Restore_Mask(int_mask,device->port); } mmcsd_free_buffer(device);// TRACE("15)return with ret = %lx,ret2 = %lx",ret,ret2); return(ret|ret2);}void mmcsd_free_buffer(MMCSD_Dev *device){ device->cur_num = 0; device->addr = 0;}/****************************************************************************** * Function Name: mmcsd_sector_erase * * Input: * device: the related device * blks: the blocks that will be erased. * * Value Returned: * 0: success. * 1: failure. * * Description: * this routine will erase indicated group. * * Modification History: *****************************************************************************/static int mmcsd_sector_erase(MMCSD_Dev *device, MMCSD_Blks *blks) { int ret; u32 state; TRACE("Erase the sector %ld ", (long int)(blks->start)); /******************************************************************** * Lock the device. *******************************************************************/#ifndef MMCSD_TEST down_interruptible(&(device->sema)); g_selected_lock[device->port].device = device;#endif /******************************************************************** * Identify the Card State ( Process only for Stand By State) *******************************************************************/ state =MMCSD_TRANSFER; ret = mmcsd_check_status(device,&state); if(ret) { FAILED(" Check Status failed."); goto sector_erase_ret; } /******************************************************************** * Erase *******************************************************************/ ret = _MMCSD_TagSectorStart(blks->start); if(ret) { TRACE("TagSectorStart error"); goto sector_erase_ret; } //ret = _MMCSD_TagSectorEnd(blks->start); ret = _MMCSD_TagSectorEnd(blks->start+device->phy_sectorsize-1); if(ret) { TRACE("TagSectorEnd error"); goto sector_erase_ret; } ret = _MMCSD_Erase(); if(ret) { TRACE("Erase error"); goto sector_erase_ret; }sector_erase_ret: /******************************************************************** * Unlock the selected device structure. *******************************************************************/ state =MMCSD_TRANSFER; mmcsd_check_status(device,&state);#ifndef MMCSD_TEST up(&(device->sema));#endif return ret;}/****************************************************************************** * Function Name: mmcsd_ReadBlock * * Input: None. * * Value Returned: * * MMCSD_STATUS * * Description: * * * Modification History: *****************************************************************************/MMCSD_STATUS mmcsd_ReadBlock(MMCSD_Dev *device,u32 start_addr,u16 nsects, u8 *buff ){ u16 i; u32 read_addr; MMCSD_STATUS ret; u8 *cur_buf; /************************************************************************ * Initialize local variables *************************************************************************/ ret = read_addr = 0; /* SEND/RECEIVE ERROR */ for ( i = 0 ; i < nsects ; i++ ) { /************************************************************ * Set the first address of read sector **************************************************************/ read_addr = ( start_addr + (MMCSD_MINIMUM_BLK_SIZE*i) ); /************************************************************ * Create cmd **************************************************************/ cur_buf = buff+(MMCSD_MINIMUM_BLK_SIZE*i);#ifdef MMCSD_READ_DMA ret=mmcsd_DMA_ReadSingleBlock(device,read_addr, cur_buf); if(ret==MMCSD_OK) // Only for DMA { // Endian processing#ifdef MMCSD_DMA_ENDIAN_ERR _mmcsd_16_big_2_little(cur_buf,MMCSD_MINIMUM_BLK_SIZE);#endif }#else ret=mmcsd_ReadSingleBlock(device,read_addr, cur_buf); #endif if (ret) { break; } } return( ret );}/****************************************************************************** * Function Name: mmcsd_ReadSingleBlock * * Input: None. * * Value Returned: * * MMCSD_STATUS * * Description: * * Modification History: *****************************************************************************/MMCSD_STATUS mmcsd_DMA_ReadSingleBlock(MMCSD_Dev *device,u32 addr,u8 * buff){ MMCSD_STATUS ret; u32 channel,status,int_mask,state; int timeleft; device->result = MMCSD_OK; channel = device->dma_channel; ret = mmcsd_DmaStartTransfer( device, buff, MMCSD_MINIMUM_BLK_SIZE, MMCSD_READ, device->bit_width); if(ret != MMCSD_OK) { FAILED("mmcsd_DmaStartTransfer failed !"); return ret; } state = MMCSD_TRANSFER; ret = mmcsd_check_status(device,&state); _MMCSD_Mask_interrupt(0x7F,&int_mask,device->port); _MMCSD_StopClk(device->port); if(device->bit_width) { _MMCSD_SetCmd(MMCSD_CMD17,addr,MMCSD_BUS_4BIT|MMCSDB_DATEN|MMCSDB_R1,device->port); // Read Single BLK } else { _MMCSD_SetCmd(MMCSD_CMD17,addr,MMCSDB_DATEN|MMCSDB_R1,device->port); // Read Single BLK } g_mmcsd_cmd[device->port] = MMCSD_CMD17; _MMCSD_StartClk(device->port); if((ret=_MMCSD_WaitUntil(MMCSD_CMD_DONE,device->port))!=0) { FAILED("WaitCMDDone ret 0x%lx",(unsigned long)ret); goto dma_read_end; } TRACE("Enable DMA..");#ifdef CONFIG_ARCH_MX1ADS enable_dma(channel);#endif#ifdef CONFIG_ARCH_MX2ADS mx_dma_start(channel);#endif timeleft = interruptible_sleep_on_timeout(&(device->select_wait),100); if(!timeleft) { TRACE("read failed, Timeout!"); ret = MMCSD_ERROR; goto dma_read_end; } status = device->result; TRACE("waked up! status = %x", status); if(status&0x800) { ret = MMCSD_OK; goto dma_read_end; } else if(status&0x3) { TRACE("g_mmcsd_status 0x%lx",(unsigned long)status); ret = MMCSD_ERROR; goto dma_read_end; } // in fact, although status&0x800 ==0, read succeed.dma_read_end:#ifdef CONFIG_ARCH_MX1ADS disable_dma(channel);#endif #ifdef CONFIG_ARCH_MX2ADS //disable dma disable_dma(device->dma_channel);// _reg_DMA_CCR(channel) &= 0xFFFFFFFE;#endif _MMCSD_Restore_Mask(int_mask,device->port); g_mmcsd_cmd[device->port] = 0; return ret;}MMCSD_STATUS mmcsd_ReadSingleBlock(MMCSD_Dev *device,u32 addr,u8 * buff){ MMCSD_STATUS ret; u32 temp32,j,int_mask,total; u16 temp16; u8 *pbuff; int i; pbuff=buff; _MMCSD_Mask_interrupt(0x7F,&int_mask,device->port); total = 0; _MMCSD_StopClk(device->port); //Ricky if(device->bit_width) { _MMCSD_SetCmd(MMCSD_CMD17,addr,MMCSD_BUS_4BIT|MMCSDB_DATEN|MMCSDB_R1,device->port); // Read Single BLK } else { _MMCSD_SetCmd(MMCSD_CMD17,addr,MMCSDB_DATEN|MMCSDB_R1,device->port); // Read Single BLK } g_mmcsd_cmd[device->port] = MMCSD_CMD17; _MMCSD_StartClk(device->port); while(1) {#ifdef CONFIG_ARCH_MX2ADS if(device->port == 1) g_mmcsd_status = (mmcsdr_status_2); else g_mmcsd_status = (mmcsdr_status);#endif#ifdef CONFIG_ARCH_MX1ADS g_mmcsd_status = (mmcsdr_status);#endif if((g_mmcsd_status&0x088B) == 0) { continue; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -