int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) { ulong i; while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; stm32_flash_lock(0); setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG); /* To make things simple use byte writes only */ for (i = 0; i < cnt; i++) { *(uchar *)(addr + i) = src[i]; /* avoid re-ordering flash data write and busy status * check as flash memory space attributes are generally Normal */ mb(); while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; } clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG); stm32_flash_lock(1); return 0; }
int flash_erase(flash_info_t *info, int first, int last) { u8 bank = 0xFF; int i; for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { if (info == &flash_info[i]) { bank = i; break; } } if (bank == 0xFF) return -1; stm32_flash_lock(0); for (i = first; i <= last; i++) { while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; /* clear old sector number before writing a new one */ clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SNB_MASK); if (bank == 0) { setbits_le32(&STM32_FLASH->cr, (i << STM32_FLASH_CR_SNB_OFFSET)); } else if (bank == 1) { setbits_le32(&STM32_FLASH->cr, ((0x10 | i) << STM32_FLASH_CR_SNB_OFFSET)); } else { stm32_flash_lock(1); return -1; } setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SER); setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_STRT); while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SER); } stm32_flash_lock(1); return 0; }
int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) { ulong i; while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; stm32_flash_lock(0); setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG); /* To make things simple use byte writes only */ for (i = 0; i < cnt; i++) { *(uchar *)(addr + i) = src[i]; while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY) ; } clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG); stm32_flash_lock(1); return 0; }