Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/flash/nand/davinci.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ static int davinci_write_page_ecc4infix(struct nand_device *nand, uint32_t page,
davinci_write_block_data(nand, oob, 16);
oob += 16;
oob_size -= 16;
(void) oob_size;

} while (data_size);

Expand Down
3 changes: 0 additions & 3 deletions src/flash/nor/dsp5680xx_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@

static int dsp5680xx_build_sector_list(struct flash_bank *bank)
{
uint32_t offset = HFM_FLASH_BASE_ADDR;

bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);

for (unsigned int i = 0; i < bank->num_sectors; ++i) {
bank->sectors[i].offset = i * HFM_SECTOR_SIZE;
bank->sectors[i].size = HFM_SECTOR_SIZE;
offset += bank->sectors[i].size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = -1;
}
Expand Down
133 changes: 68 additions & 65 deletions src/flash/nor/wcharm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Expand Down Expand Up @@ -74,12 +74,12 @@
#define FLASH_OBL_LAUNCH (1 << 13) /* except ch32f1x series */


#define FLASH_PAGE_PROGRAM 0x00010000
#define FLASH_PAGE_ERASE 0x00020000
#define FLASH_STD_PAGE_ERASE 0x00000002
#define FLASH_STD_PAGE_PRG 0x00000001
#define FLASH_BUF_LOAD 0x00040000
#define FLASH_BUF_RTS 0x00080000
#define FLASH_PAGE_PROGRAM 0x00010000
#define FLASH_PAGE_ERASE 0x00020000
#define FLASH_STD_PAGE_ERASE 0x00000002
#define FLASH_STD_PAGE_PRG 0x00000001
#define FLASH_BUF_LOAD 0x00040000
#define FLASH_BUF_RTS 0x00080000



Expand Down Expand Up @@ -141,8 +141,8 @@ static int ch32x_get_device_id(struct flash_bank *bank, uint32_t *device_id);
static int ch32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
uint32_t address, uint32_t count);
extern int wlink_armcheckprotect(void);
extern void wlink_sendchip(config);
extern void wlink_sendchip(uint8_t config);



/* flash bank ch32x <base> <size> 0 0 <target#>
Expand Down Expand Up @@ -222,8 +222,8 @@ static int ch32x_wait_status_busy(struct flash_bank *bank, int timeout)

static int ch32x_check_operation_supported(struct flash_bank *bank)
{


struct ch32x_flash_bank *ch32x_info = bank->driver_priv;

/* if we have a dual flash bank device then
Expand All @@ -238,8 +238,8 @@ static int ch32x_check_operation_supported(struct flash_bank *bank)

static int ch32x_read_options(struct flash_bank *bank)
{


struct ch32x_flash_bank *ch32x_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t option_bytes;
Expand Down Expand Up @@ -278,8 +278,8 @@ static int ch32x_read_options(struct flash_bank *bank)

static int ch32x_erase_options(struct flash_bank *bank)
{


struct ch32x_flash_bank *ch32x_info = bank->driver_priv;
struct target *target = bank->target;

Expand Down Expand Up @@ -324,8 +324,8 @@ static int ch32x_erase_options(struct flash_bank *bank)

static int ch32x_write_options(struct flash_bank *bank)
{


struct ch32x_flash_bank *ch32x_info = NULL;
struct target *target = bank->target;

Expand Down Expand Up @@ -379,7 +379,7 @@ static int ch32x_write_options(struct flash_bank *bank)

static int ch32x_protect_check(struct flash_bank *bank)
{


struct target *target = bank->target;
uint32_t protection;
Expand All @@ -395,29 +395,30 @@ static int ch32x_protect_check(struct flash_bank *bank)
if (retval != ERROR_OK)
return retval;

for (int i = 0; i < bank->num_prot_blocks; i++)
for (unsigned int i = 0; i < bank->num_prot_blocks; i++)
bank->prot_blocks[i].is_protected = (protection & (1 << i)) ? 0 : 1;

return ERROR_OK;
}

static int ch32x_erase(struct flash_bank *bank, int first, int last)
{
if(armchip)
{
if(noloadflag)
return ERROR_OK;
int ret=wlink_armerase();
static int ch32x_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
{
// TODO: This function doesn't erase anything on !arm. That's probably bad.
if (armchip) {
if (noloadflag) {
return ERROR_OK;
}

int ret = wlink_armerase();
return ret;

}

}
return ERROR_OK;
}

static int ch32x_protect(struct flash_bank *bank, int set, int first, int last)
static int ch32x_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
{

struct target *target = bank->target;
struct ch32x_flash_bank *ch32x_info = bank->driver_priv;

Expand All @@ -436,7 +437,7 @@ static int ch32x_protect(struct flash_bank *bank, int set, int first, int last)
return retval;
}

for (int i = first; i <= last; i++) {
for (unsigned int i = first; i <= last; i++) {
if (set)
ch32x_info->option_bytes.protection &= ~(1 << i);
else
Expand All @@ -455,29 +456,31 @@ static int ch32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
static int ch32x_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
if(armchip)
{
if(noloadflag)
return ERROR_OK;
int ret=wlink_armwrite(buffer,bank->base + offset,count);
return ret;
}
// TODO: This doesn't write anything for !arm. Is that intended?
if (armchip) {
if (noloadflag) {
return ERROR_OK;
}
int ret=wlink_armwrite(buffer,bank->base + offset,count);
return ret;
}

return ERROR_OK;

}

static int ch32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
{
/* This check the device CPUID core register to detect
* the M0 from the M3 devices. */

struct target *target = bank->target;
uint32_t cpuid, device_id_register = 0;
uint32_t testid=0;
uint32_t tmp,tmp1,tmp2=0;
uint8_t user_cfg,config;
target_read_u8(target, 0x1ffff802, &user_cfg);
config=user_cfg>>6;
target_read_u32(target, 0x1ffff884, &testid);
target_read_u32(target, 0x1ffff884, &testid);
if(((testid>>16)==0x2000)||((testid>>16)==0x1000)||((testid>>16)==0x3000)){
target_read_u32(target, 0x1ffff7e8, &tmp);
target_read_u32(target, 0x1ffff8a0, &tmp1);
Expand All @@ -489,15 +492,15 @@ static int ch32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
wlink_sendchip(config);
return ERROR_OK;
}

}
target_read_u32(target, 0xe000edfc, &testid);

target_read_u32(target, 0xe000edf0, &testid);

target_read_u32(target, 0x1ffff704, &testid);
if(((testid>>20)==0x203)||((testid>>20)==0x205)||((testid>>20)==0x207)||((testid>>20)==0x208)){

if(((testid>>20)==0x203)||((testid>>20)==0x205)||((testid>>20)==0x207)||((testid>>20)==0x208)){
armchip=2;
*device_id=0x20000410;
wlink_sendchip(config);
Expand All @@ -508,48 +511,50 @@ static int ch32x_get_device_id(struct flash_bank *bank, uint32_t *device_id)
}

static int ch32x_get_flash_size(struct flash_bank *bank, uint16_t *flash_size_in_kb)
{
{
struct target *target = bank->target;
uint32_t cpuid, flash_size_reg;
uint32_t temp;
int retval = target_read_u32(target, 0x1ffff7e0, flash_size_in_kb);
// FIXME: this is almost certainly an actual bug and I'm applying pain killer
// to it.
// target_read_u32 reads 32 bits and stores it in the pointee of the final arg.
// The final argument here is a uint16_t as it is in the caller. I'm going to
// change this to read 16 bits, but this could cause synchronization issues.
// Orig: int retval = target_read_u32(target, 0x1ffff7e0, flash_size_in_kb);
int retval = target_read_u16(target, 0x1ffff7e0, flash_size_in_kb);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without debug docs from WCH, we don't know if this is wrong or right. It might only allow a 32bit read, even if they are only interested in the lower 16bits. Is the code gross? yes! is this code actually wrong? we have no idea!

if (retval != ERROR_OK)
return retval;

return retval;
}

static int ch32x_probe(struct flash_bank *bank)
{
{
struct ch32x_flash_bank *ch32x_info = bank->driver_priv;
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;
uint32_t rid=0;
ch32x_info->probed = 0;
ch32x_info->register_base = FLASH_REG_BASE_B0;
ch32x_info->user_data_offset = 10;
ch32x_info->option_offset = 0;

/* default factory read protection level 0 */
ch32x_info->default_rdp = 0xA5;
ch32x_info->default_rdp = 0xA5;

int retval = ch32x_get_device_id(bank, &device_id);
if (retval != ERROR_OK)
return retval;

LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
rid=device_id & 0xfff ;
/* set page size, protection granularity and max flash size depending on family */
switch (device_id & 0xfff) {
case 0x410: /* medium density */
page_size = 1024;
ch32x_info->ppage_size = 4;
max_flash_size_in_kb = 512;
break;

default:
LOG_WARNING("Cannot identify target as a ch32 family.");
return ERROR_FAIL;
Expand Down Expand Up @@ -632,7 +637,7 @@ static int ch32x_probe(struct flash_bank *bank)

static int ch32x_auto_probe(struct flash_bank *bank)
{

struct ch32x_flash_bank *ch32x_info = bank->driver_priv;
if (ch32x_info->probed)
return ERROR_OK;
Expand All @@ -646,7 +651,7 @@ COMMAND_HANDLER(ch32x_handle_part_id_command)
}
#endif

static int get_ch32x_info(struct flash_bank *bank, char *buf, int buf_size)
static int get_ch32x_info(struct flash_bank *bank, struct command_invocation *buf_size)
{


Expand Down Expand Up @@ -945,7 +950,7 @@ COMMAND_HANDLER(ch32x_handle_options_load_command)

static int ch32x_mass_erase(struct flash_bank *bank)
{

struct target *target = bank->target;

if (target->state != TARGET_HALTED) {
Expand Down Expand Up @@ -983,8 +988,6 @@ static int ch32x_mass_erase(struct flash_bank *bank)

COMMAND_HANDLER(ch32x_handle_mass_erase_command)
{
int i;

if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;

Expand All @@ -996,7 +999,7 @@ COMMAND_HANDLER(ch32x_handle_mass_erase_command)
retval = ch32x_mass_erase(bank);
if (retval == ERROR_OK) {
/* set all sectors as erased */
for (i = 0; i < bank->num_sectors; i++)
for (unsigned i = 0; i < bank->num_sectors; i++)
bank->sectors[i].is_erased = 1;

command_print(CMD, "ch32x mass erase complete");
Expand Down
Loading