/* write one byte to ec rom */ int ec_write_byte(unsigned int addr, unsigned char byte) { int ret = 0; /* enable spicmd writing. */ ec_start_spi(); /* enable write spi flash */ wrec(REG_XBISPICMD, SPICMD_WRITE_ENABLE); if(rom_instruction_cycle(SPICMD_WRITE_ENABLE) == EC_STATE_BUSY){ printf("EC_WRITE_BYTE : SPICMD_WRITE_ENABLE failed.\n"); ret = -1; goto out; } /* write the address */ wrec(REG_XBISPIA2, (addr & 0xff0000) >> 16); wrec(REG_XBISPIA1, (addr & 0x00ff00) >> 8); wrec(REG_XBISPIA0, (addr & 0x0000ff) >> 0); wrec(REG_XBISPIDAT, byte); /* start action */ wrec(REG_XBISPICMD, SPICMD_BYTE_PROGRAM); if(rom_instruction_cycle(SPICMD_BYTE_PROGRAM) == EC_STATE_BUSY){ printf("EC_WRITE_BYTE : SPICMD_BYTE_PROGRAM failed.\n"); ret = -1; goto out; } out : /* disable spicmd writing. */ ec_stop_spi(); return ret; }
/* Starts to execute the protect SPI ROM function. */ int EC_ROM_start_protect(void) { unsigned char status; int j; /* we should start spi access firstly */ ec_start_spi(); /* enable write spi flash */ wrec(REG_XBISPICMD, SPICMD_WRITE_ENABLE); if(rom_instruction_cycle(SPICMD_WRITE_ENABLE) == EC_STATE_BUSY){ printf("EC_PROGRAM_ROM : SPICMD_WRITE_ENABLE failed.\n"); goto out1; } /* protect the status register of rom */ wrec(REG_XBISPICMD, SPICMD_READ_STATUS); if(rom_instruction_cycle(SPICMD_READ_STATUS) == EC_STATE_BUSY){ printf("EC_PROGRAM_ROM : SPICMD_READ_STATUS failed.\n"); goto out1; } status = rdec(REG_XBISPIDAT); wrec(REG_XBISPIDAT, status | 0x1C); if(ec_instruction_cycle() < 0){ printf("EC_PROGRAM_ROM : write status value failed.\n"); goto out1; } wrec(REG_XBISPICMD, SPICMD_WRITE_STATUS); if(rom_instruction_cycle(SPICMD_WRITE_STATUS) == EC_STATE_BUSY){ printf("EC_PROGRAM_ROM : SPICMD_WRITE_STATUS failed.\n"); goto out1; } /* disable the write action to spi rom */ wrec(REG_XBISPICMD, SPICMD_WRITE_DISABLE); if(rom_instruction_cycle(SPICMD_WRITE_DISABLE) == EC_STATE_BUSY){ printf("EC_PROGRAM_ROM : SPICMD_WRITE_DISABLE failed.\n"); goto out1; } out1: /* we should stop spi access firstly */ ec_stop_spi(); out: /* for security */ //for(j = 0; j < 2000; j++) // udelay(1000); /* exit from the reset mode */ //ec_exit_reset_mode(); return 0; }
/* EC_ROM_unprotect function code */ static int EC_ROM_unprotect(void) { unsigned char status; /* enable write spi flash */ ec_write(REG_XBISPICMD, SPICMD_WRITE_ENABLE); if (rom_instruction_cycle(SPICMD_WRITE_ENABLE) == EC_STATE_BUSY) { printk(KERN_ERR "EC_UNIT_ERASE : SPICMD_WRITE_ENABLE failed.\n"); return 1; } /* unprotect the status register of rom */ ec_write(REG_XBISPICMD, SPICMD_READ_STATUS); if (rom_instruction_cycle(SPICMD_READ_STATUS) == EC_STATE_BUSY) { printk(KERN_ERR "EC_UNIT_ERASE : SPICMD_READ_STATUS failed.\n"); return 1; } status = ec_read(REG_XBISPIDAT); ec_write(REG_XBISPIDAT, status & 0x02); if (ec_instruction_cycle() < 0) { printk(KERN_ERR "EC_UNIT_ERASE : write status value failed.\n"); return 1; } ec_write(REG_XBISPICMD, SPICMD_WRITE_STATUS); if (rom_instruction_cycle(SPICMD_WRITE_STATUS) == EC_STATE_BUSY) { printk(KERN_ERR "EC_UNIT_ERASE : SPICMD_WRITE_STATUS failed.\n"); return 1; } /* enable write spi flash */ ec_write(REG_XBISPICMD, SPICMD_WRITE_ENABLE); if (rom_instruction_cycle(SPICMD_WRITE_ENABLE) == EC_STATE_BUSY) { printk(KERN_ERR "EC_UNIT_ERASE : SPICMD_WRITE_ENABLE failed.\n"); return 1; } return 0; }
/* erase one block or chip or sector as needed */ static int ec_unit_erase(unsigned char erase_cmd, unsigned int addr) { unsigned char status; int ret = 0, i = 0; int unprotect_count = 3; int check_flag = 0; /* enable spicmd writing. */ ec_start_spi(); #ifdef EC_ROM_PROTECTION /* added for re-check SPICMD_READ_STATUS */ while (unprotect_count-- > 0) { if (EC_ROM_unprotect()) { ret = -EINVAL; goto out; } /* first time:500ms --> 5.5sec -->10.5sec */ for (i = 0; i < ((2 - unprotect_count) * 100 + 10); i++) udelay(50000); ec_write(REG_XBISPICMD, SPICMD_READ_STATUS); if (rom_instruction_cycle(SPICMD_READ_STATUS) == EC_STATE_BUSY) { printk(KERN_ERR "EC_PROGRAM_ROM : SPICMD_READ_STATUS failed.\n"); } else { status = ec_read(REG_XBISPIDAT); printk(KERN_INFO "Read unprotect status : 0x%x\n", status); if ((status & 0x1C) == 0x00) { printk(KERN_INFO "Read unprotect status OK1 : 0x%x\n", status & 0x1C); check_flag = 1; break; } } } if (!check_flag) { printk(KERN_INFO "SPI ROM unprotect fail.\n"); return 1; } #endif /* block address fill */ if (erase_cmd == SPICMD_BLK_ERASE) { ec_write(REG_XBISPIA2, (addr & 0x00ff0000) >> 16); ec_write(REG_XBISPIA1, (addr & 0x0000ff00) >> 8); ec_write(REG_XBISPIA0, (addr & 0x000000ff) >> 0); }
/* read one byte from xbi interface */ static int ec_read_byte(unsigned int addr, unsigned char *byte) { int ret = 0; /* enable spicmd writing. */ ec_start_spi(); /* enable write spi flash */ ec_write(REG_XBISPICMD, SPICMD_WRITE_ENABLE); if (rom_instruction_cycle(SPICMD_WRITE_ENABLE) == EC_STATE_BUSY) { printk(KERN_ERR "EC_READ_BYTE : SPICMD_WRITE_ENABLE failed.\n"); ret = -EINVAL; goto out; } /* write the address */ ec_write(REG_XBISPIA2, (addr & 0xff0000) >> 16); ec_write(REG_XBISPIA1, (addr & 0x00ff00) >> 8); ec_write(REG_XBISPIA0, (addr & 0x0000ff) >> 0); /* start action */ ec_write(REG_XBISPICMD, SPICMD_HIGH_SPEED_READ); if (rom_instruction_cycle(SPICMD_HIGH_SPEED_READ) == EC_STATE_BUSY) { printk(KERN_ERR "EC_READ_BYTE : SPICMD_HIGH_SPEED_READ failed.\n"); ret = -EINVAL; goto out; } *byte = ec_read(REG_XBISPIDAT); out: /* disable spicmd writing. */ ec_stop_spi(); return ret; }
/* Starts to execute the unprotect SPI ROM function. */ int EC_ROM_start_unprotect(void) { unsigned char status; int ret = 0, i = 0; int unprotect_count = 3; int check_flag =0; /* added for re-check SPICMD_READ_STATUS */ while(unprotect_count-- > 0){ if(EC_ROM_unprotect()){ ret = -1; return ret; } for(i = 0; i < ((2 - unprotect_count) * 100 + 10); i++) //first time:500ms --> 5.5sec -->10.5sec udelay(50000); wrec(REG_XBISPICMD, SPICMD_READ_STATUS); if(rom_instruction_cycle(SPICMD_READ_STATUS) == EC_STATE_BUSY){ printf("EC_PROGRAM_ROM : SPICMD_READ_STATUS failed.\n"); } else { status = rdec(REG_XBISPIDAT); //printf("Read unprotect status : 0x%x\n", status); if((status & 0x1C) == 0x00){ //printf("Read unprotect status OK1 : 0x%x\n", status & 0x1C); check_flag = 1; break; } } } if(!check_flag){ printf("SPI ROM unprotect fail.\n"); return 1; } //printf("SPI ROM unprotect success.\n"); return ret; }