2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-03-01 11:44:40 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015
|
2015-11-29 10:50:53 +00:00
|
|
|
* Kamil Lulko, <kamil.lulko@gmail.com>
|
2015-03-01 11:44:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/arch/stm32.h>
|
2016-03-09 23:18:13 +00:00
|
|
|
#include "stm32_flash.h"
|
2015-03-01 11:44:40 +00:00
|
|
|
|
|
|
|
flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
|
|
|
|
|
2018-02-09 12:09:55 +00:00
|
|
|
#define STM32_FLASH ((struct stm32_flash_regs *)STM32_FLASH_CNTL_BASE)
|
2016-03-09 23:18:13 +00:00
|
|
|
|
|
|
|
void stm32_flash_latency_cfg(int latency)
|
|
|
|
{
|
|
|
|
/* 5 wait states, Prefetch enabled, D-Cache enabled, I-Cache enabled */
|
2017-07-04 16:24:43 +00:00
|
|
|
writel(FLASH_ACR_WS(latency) | FLASH_ACR_PRFTEN | FLASH_ACR_ICEN
|
2016-03-09 23:18:13 +00:00
|
|
|
| FLASH_ACR_DCEN, &STM32_FLASH->acr);
|
|
|
|
}
|
2015-03-01 11:44:40 +00:00
|
|
|
|
2016-03-09 23:18:13 +00:00
|
|
|
static void stm32_flash_lock(u8 lock)
|
2015-03-01 11:44:40 +00:00
|
|
|
{
|
|
|
|
if (lock) {
|
|
|
|
setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_LOCK);
|
|
|
|
} else {
|
|
|
|
writel(STM32_FLASH_KEY1, &STM32_FLASH->key);
|
|
|
|
writel(STM32_FLASH_KEY2, &STM32_FLASH->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long flash_init(void)
|
|
|
|
{
|
|
|
|
unsigned long total_size = 0;
|
|
|
|
u8 i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
|
2016-03-09 23:18:13 +00:00
|
|
|
flash_info[i].flash_id = FLASH_STM32;
|
2015-03-01 11:44:40 +00:00
|
|
|
flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
|
|
|
|
flash_info[i].start[0] = CONFIG_SYS_FLASH_BASE + (i << 20);
|
|
|
|
flash_info[i].size = sect_sz_kb[0];
|
|
|
|
for (j = 1; j < CONFIG_SYS_MAX_FLASH_SECT; j++) {
|
|
|
|
flash_info[i].start[j] = flash_info[i].start[j - 1]
|
|
|
|
+ (sect_sz_kb[j - 1]);
|
|
|
|
flash_info[i].size += sect_sz_kb[j];
|
|
|
|
}
|
|
|
|
total_size += flash_info[i].size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flash_print_info(flash_info_t *info)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (info->flash_id == FLASH_UNKNOWN) {
|
|
|
|
printf("missing or unknown FLASH type\n");
|
|
|
|
return;
|
2016-03-09 23:18:13 +00:00
|
|
|
} else if (info->flash_id == FLASH_STM32) {
|
|
|
|
printf("stm32 Embedded Flash\n");
|
2015-03-01 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Size: %ld MB in %d Sectors\n",
|
|
|
|
info->size >> 20, info->sector_count);
|
|
|
|
|
|
|
|
printf(" Sector Start Addresses:");
|
|
|
|
for (i = 0; i < info->sector_count; ++i) {
|
|
|
|
if ((i % 5) == 0)
|
|
|
|
printf("\n ");
|
|
|
|
printf(" %08lX%s",
|
|
|
|
info->start[i],
|
|
|
|
info->protect[i] ? " (RO)" : " ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-03-09 23:18:13 +00:00
|
|
|
stm32_flash_lock(0);
|
2015-03-01 11:44:40 +00:00
|
|
|
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
|
|
|
|
;
|
|
|
|
|
2015-10-23 18:14:07 +00:00
|
|
|
/* clear old sector number before writing a new one */
|
|
|
|
clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SNB_MASK);
|
|
|
|
|
2015-03-01 11:44:40 +00:00
|
|
|
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 {
|
2016-03-09 23:18:13 +00:00
|
|
|
stm32_flash_lock(1);
|
2015-03-01 11:44:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:18:13 +00:00
|
|
|
stm32_flash_lock(1);
|
2015-03-01 11:44:40 +00:00
|
|
|
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)
|
|
|
|
;
|
|
|
|
|
2016-03-09 23:18:13 +00:00
|
|
|
stm32_flash_lock(0);
|
2015-03-01 11:44:40 +00:00
|
|
|
|
|
|
|
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];
|
2016-04-04 23:49:02 +00:00
|
|
|
/* avoid re-ordering flash data write and busy status
|
|
|
|
* check as flash memory space attributes are generally Normal
|
|
|
|
*/
|
|
|
|
mb();
|
2015-03-01 11:44:40 +00:00
|
|
|
while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG);
|
2016-03-09 23:18:13 +00:00
|
|
|
stm32_flash_lock(1);
|
2015-03-01 11:44:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|