From 9a8edbfa8bd83d58a1904dfd35a00f9793d99314 Mon Sep 17 00:00:00 2001 From: Andreas Fritiofson Date: Tue, 17 Jul 2012 00:43:46 +0200 Subject: [PATCH] flash: reduce code duplication in stm32 flash probe Remove a lot of the repetitive code in stm32f1x flash probe by converting the large if-selector to a switch, moving the common checks outside it and concentrating the failure handling to a single point. Do the same with stm32f2x and stm32lx for consistency. Change-Id: Ic0ecfb1533c49f5d2108cda5fd20c8372d7c71ef Signed-off-by: Andreas Fritiofson Reviewed-on: http://openocd.zylin.com/746 Tested-by: jenkins Reviewed-by: Spencer Oliver Reviewed-by: Freddie Chopin --- src/flash/nor/stm32f1x.c | 199 +++++++++++++-------------------------- src/flash/nor/stm32f2x.c | 47 ++++----- src/flash/nor/stm32lx.c | 49 +++++----- 3 files changed, 107 insertions(+), 188 deletions(-) diff --git a/src/flash/nor/stm32f1x.c b/src/flash/nor/stm32f1x.c index 2a6604dc2..d06016a4d 100644 --- a/src/flash/nor/stm32f1x.c +++ b/src/flash/nor/stm32f1x.c @@ -880,6 +880,7 @@ static int stm32x_probe(struct flash_bank *bank) struct stm32x_flash_bank *stm32x_info = bank->driver_priv; int i; uint16_t flash_size_in_kb; + uint16_t max_flash_size_in_kb; uint32_t device_id; int page_size; uint32_t base_address = 0x08000000; @@ -894,116 +895,76 @@ static int stm32x_probe(struct flash_bank *bank) LOG_INFO("device id = 0x%08" PRIx32 "", device_id); - /* get flash size from target. */ - retval = stm32x_get_flash_size(bank, &flash_size_in_kb); - if (retval != ERROR_OK) { - LOG_WARNING("failed reading flash size, default to max target family"); - /* failed reading flash size, default to max target family */ - flash_size_in_kb = 0xffff; + /* set page size, protection granularity and max flash size depending on family */ + switch (device_id & 0xfff) { + case 0x410: /* medium density */ + page_size = 1024; + stm32x_info->ppage_size = 4; + max_flash_size_in_kb = 128; + break; + case 0x412: /* low density */ + page_size = 1024; + stm32x_info->ppage_size = 4; + max_flash_size_in_kb = 32; + break; + case 0x414: /* high density */ + page_size = 2048; + stm32x_info->ppage_size = 2; + max_flash_size_in_kb = 512; + break; + case 0x418: /* connectivity line density */ + page_size = 2048; + stm32x_info->ppage_size = 2; + max_flash_size_in_kb = 256; + break; + case 0x420: /* value line density */ + page_size = 1024; + stm32x_info->ppage_size = 4; + max_flash_size_in_kb = 128; + break; + case 0x422: /* stm32f30x */ + page_size = 2048; + stm32x_info->ppage_size = 2; + max_flash_size_in_kb = 256; + break; + case 0x428: /* value line High density */ + page_size = 2048; + stm32x_info->ppage_size = 4; + max_flash_size_in_kb = 128; + break; + case 0x430: /* xl line density (dual flash banks) */ + page_size = 2048; + stm32x_info->ppage_size = 2; + max_flash_size_in_kb = 1024; + stm32x_info->has_dual_banks = true; + break; + case 0x432: /* stm32f37x */ + page_size = 2048; + stm32x_info->ppage_size = 2; + max_flash_size_in_kb = 256; + break; + case 0x440: /* stm32f0x */ + page_size = 1024; + stm32x_info->ppage_size = 4; + max_flash_size_in_kb = 64; + break; + default: + LOG_WARNING("Cannot identify target as a STM32 family."); + return ERROR_FAIL; } - /* some variants read 0 for flash size register - * use a max flash size as a default */ - if (flash_size_in_kb == 0) - flash_size_in_kb = 0xffff; + /* get flash size from target. */ + retval = stm32x_get_flash_size(bank, &flash_size_in_kb); - if ((device_id & 0xfff) == 0x410) { - /* medium density - we have 1k pages - * 4 pages for a protection area */ - page_size = 1024; - stm32x_info->ppage_size = 4; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors incorrect on revA */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash"); - flash_size_in_kb = 128; - } - } else if ((device_id & 0xfff) == 0x412) { - /* low density - we have 1k pages - * 4 pages for a protection area */ - page_size = 1024; - stm32x_info->ppage_size = 4; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors incorrect on revA */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 32k flash"); - flash_size_in_kb = 32; - } - } else if ((device_id & 0xfff) == 0x414) { - /* high density - we have 2k pages - * 2 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 2; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors incorrect on revZ */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 512k flash"); - flash_size_in_kb = 512; - } - } else if ((device_id & 0xfff) == 0x418) { - /* connectivity line density - we have 2k pages - * 2 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 2; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors incorrect on revZ */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash"); - flash_size_in_kb = 256; - } - } else if ((device_id & 0xfff) == 0x420) { - /* value line density - we have 1k pages - * 4 pages for a protection area */ - page_size = 1024; - stm32x_info->ppage_size = 4; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash"); - flash_size_in_kb = 128; - } - } else if ((device_id & 0xfff) == 0x422) { - /* stm32f30x - we have 2k pages - * 2 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 2; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash"); - flash_size_in_kb = 256; - } - } else if ((device_id & 0xfff) == 0x428) { - /* value line High density - we have 2k pages - * 4 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 4; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash"); - flash_size_in_kb = 128; - } - } else if ((device_id & 0xfff) == 0x430) { - /* xl line density - we have 2k pages - * 2 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 2; - stm32x_info->has_dual_banks = true; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash"); - flash_size_in_kb = 1024; - } + /* failed reading flash size or flash size invalid (early silicon), + * default to max target family */ + if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) { + LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash", + max_flash_size_in_kb); + flash_size_in_kb = max_flash_size_in_kb; + } + if (stm32x_info->has_dual_banks) { /* split reported size into matching bank */ if (bank->base != 0x08080000) { /* bank 0 will be fixed 512k */ @@ -1014,32 +975,6 @@ static int stm32x_probe(struct flash_bank *bank) stm32x_info->register_base = FLASH_REG_BASE_B1; base_address = 0x08080000; } - } else if ((device_id & 0xfff) == 0x432) { - /* stm32f37x - we have 2k pages - * 2 pages for a protection area */ - page_size = 2048; - stm32x_info->ppage_size = 2; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 256k flash"); - flash_size_in_kb = 256; - } - } else if ((device_id & 0xfff) == 0x440) { - /* stm32f0x - we have 1k pages - * 4 pages for a protection area */ - page_size = 1024; - stm32x_info->ppage_size = 4; - - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors incorrect on revZ */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 64k flash"); - flash_size_in_kb = 64; - } - } else { - LOG_WARNING("Cannot identify target as a STM32 family."); - return ERROR_FAIL; } LOG_INFO("flash size = %dkbytes", flash_size_in_kb); diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c index c7abd6a83..2407977f1 100644 --- a/src/flash/nor/stm32f2x.c +++ b/src/flash/nor/stm32f2x.c @@ -586,6 +586,7 @@ static int stm32x_probe(struct flash_bank *bank) struct stm32x_flash_bank *stm32x_info = bank->driver_priv; int i; uint16_t flash_size_in_kb; + uint16_t max_flash_size_in_kb; uint32_t device_id; uint32_t base_address = 0x08000000; @@ -597,38 +598,28 @@ static int stm32x_probe(struct flash_bank *bank) return retval; LOG_INFO("device id = 0x%08" PRIx32 "", device_id); - /* get flash size from target. */ - retval = target_read_u16(target, 0x1FFF7A22, &flash_size_in_kb); - if (retval != ERROR_OK) { - LOG_WARNING("failed reading flash size, default to max target family"); - /* failed reading flash size, default to max target family */ - flash_size_in_kb = 0xffff; - } - - /* some variants read 0 for flash size register - * use a max flash size as a default */ - if (flash_size_in_kb == 0) - flash_size_in_kb = 0xffff; - - if ((device_id & 0xfff) == 0x411) { - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash"); - flash_size_in_kb = 1024; - } - } else if ((device_id & 0xfff) == 0x413) { - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 1024k flash"); - flash_size_in_kb = 1024; - } - } else { + /* set max flash size depending on family */ + switch (device_id & 0xfff) { + case 0x411: + case 0x413: + max_flash_size_in_kb = 1024; + break; + default: LOG_WARNING("Cannot identify target as a STM32 family."); return ERROR_FAIL; } + /* get flash size from target. */ + retval = target_read_u16(target, 0x1FFF7A22, &flash_size_in_kb); + + /* failed reading flash size or flash size invalid (early silicon), + * default to max target family */ + if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) { + LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash", + max_flash_size_in_kb); + flash_size_in_kb = max_flash_size_in_kb; + } + LOG_INFO("flash size = %dkbytes", flash_size_in_kb); /* did we assign flash size? */ diff --git a/src/flash/nor/stm32lx.c b/src/flash/nor/stm32lx.c index fedab3006..ab61538ff 100644 --- a/src/flash/nor/stm32lx.c +++ b/src/flash/nor/stm32lx.c @@ -463,6 +463,7 @@ static int stm32lx_probe(struct flash_bank *bank) struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv; int i; uint16_t flash_size_in_kb; + uint16_t max_flash_size_in_kb; uint32_t device_id; stm32lx_info->probed = 0; @@ -474,38 +475,30 @@ static int stm32lx_probe(struct flash_bank *bank) LOG_DEBUG("device id = 0x%08" PRIx32 "", device_id); - /* get flash size from target. */ - retval = target_read_u16(target, F_SIZE, &flash_size_in_kb); - if (retval != ERROR_OK) { - LOG_WARNING("failed reading flash size, default to max target family"); - /* failed reading flash size, default to max target family */ - flash_size_in_kb = 0xffff; - } - - /* some variants read 0 for flash size register - * use a max flash size as a default */ - if (flash_size_in_kb == 0) - flash_size_in_kb = 0xffff; - - if ((device_id & 0xfff) == 0x416) { - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 128k flash"); - flash_size_in_kb = 128; - } - } else if ((device_id & 0xfff) == 0x436) { - /* check for early silicon */ - if (flash_size_in_kb == 0xffff) { - /* number of sectors may be incorrect on early silicon */ - LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming 384k flash"); - flash_size_in_kb = 384; - } - } else { + /* set max flash size depending on family */ + switch (device_id & 0xfff) { + case 0x416: + max_flash_size_in_kb = 128; + break; + case 0x436: + max_flash_size_in_kb = 384; + break; + default: LOG_WARNING("Cannot identify target as a STM32L family."); return ERROR_FAIL; } + /* get flash size from target. */ + retval = target_read_u16(target, F_SIZE, &flash_size_in_kb); + + /* failed reading flash size or flash size invalid (early silicon), + * default to max target family */ + if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) { + LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash", + max_flash_size_in_kb); + flash_size_in_kb = max_flash_size_in_kb; + } + /* STM32L - we have 32 sectors, 16 pages per sector -> 512 pages * 16 pages for a protection area */