1 /***************************************************************************
\r
2 * Copyright (C) 2008 Øyvind Harboe *
\r
3 * oyvind.harboe@zylin.com *
\r
5 * This program is free software; you can redistribute it and/or modify *
\r
6 * it under the terms of the GNU General Public License as published by *
\r
7 * the Free Software Foundation; either version 2 of the License, or *
\r
8 * (at your option) any later version. *
\r
10 * This program is distributed in the hope that it will be useful, *
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
13 * GNU General Public License for more details. *
\r
15 * You should have received a copy of the GNU General Public License *
\r
16 * along with this program; if not, write to the *
\r
17 * Free Software Foundation, Inc., *
\r
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
19 ***************************************************************************/
\r
20 #ifdef HAVE_CONFIG_H
\r
24 #include "replacements.h"
\r
34 #include "binarybuffer.h"
\r
35 #include "../target/embeddedice.h"
\r
40 int ecosflash_register_commands(struct command_context_s *cmd_ctx);
\r
41 int ecosflash_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
\r
42 int ecosflash_erase(struct flash_bank_s *bank, int first, int last);
\r
43 int ecosflash_protect(struct flash_bank_s *bank, int set, int first, int last);
\r
44 int ecosflash_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
\r
45 int ecosflash_probe(struct flash_bank_s *bank);
\r
46 int ecosflash_erase_check(struct flash_bank_s *bank);
\r
47 int ecosflash_protect_check(struct flash_bank_s *bank);
\r
48 int ecosflash_info(struct flash_bank_s *bank, char *buf, int buf_size);
\r
50 u32 ecosflash_get_flash_status(flash_bank_t *bank);
\r
51 void ecosflash_set_flash_mode(flash_bank_t *bank,int mode);
\r
52 u32 ecosflash_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout);
\r
53 int ecosflash_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
55 flash_driver_t ecosflash_flash =
\r
57 .name = "ecosflash",
\r
58 .register_commands = ecosflash_register_commands,
\r
59 .flash_bank_command = ecosflash_flash_bank_command,
\r
60 .erase = ecosflash_erase,
\r
61 .protect = ecosflash_protect,
\r
62 .write = ecosflash_write,
\r
63 .probe = ecosflash_probe,
\r
64 .auto_probe = ecosflash_probe,
\r
65 .erase_check = ecosflash_erase_check,
\r
66 .protect_check = ecosflash_protect_check,
\r
67 .info = ecosflash_info
\r
70 typedef struct ecosflash_flash_bank_s
\r
72 struct target_s *target;
\r
73 working_area_t *write_algorithm;
\r
74 working_area_t *erase_check_algorithm;
\r
77 } ecosflash_flash_bank_t;
\r
79 static const int sectorSize=0x10000;
\r
81 #define FLASH_ERR_OK 0x00 // No error - operation complete
\r
82 #define FLASH_ERR_INVALID 0x01 // Invalid FLASH address
\r
83 #define FLASH_ERR_ERASE 0x02 // Error trying to erase
\r
84 #define FLASH_ERR_LOCK 0x03 // Error trying to lock/unlock
\r
85 #define FLASH_ERR_PROGRAM 0x04 // Error trying to program
\r
86 #define FLASH_ERR_PROTOCOL 0x05 // Generic error
\r
87 #define FLASH_ERR_PROTECT 0x06 // Device/region is write-protected
\r
88 #define FLASH_ERR_NOT_INIT 0x07 // FLASH info not yet initialized
\r
89 #define FLASH_ERR_HWR 0x08 // Hardware (configuration?) problem
\r
90 #define FLASH_ERR_ERASE_SUSPEND 0x09 // Device is in erase suspend mode
\r
91 #define FLASH_ERR_PROGRAM_SUSPEND 0x0a // Device is in in program suspend mode
\r
92 #define FLASH_ERR_DRV_VERIFY 0x0b // Driver failed to verify data
\r
93 #define FLASH_ERR_DRV_TIMEOUT 0x0c // Driver timed out waiting for device
\r
94 #define FLASH_ERR_DRV_WRONG_PART 0x0d // Driver does not support device
\r
95 #define FLASH_ERR_LOW_VOLTAGE 0x0e // Not enough juice to complete job
\r
99 flash_errmsg(int err)
\r
103 return "No error - operation complete";
\r
104 case FLASH_ERR_ERASE_SUSPEND:
\r
105 return "Device is in erase suspend state";
\r
106 case FLASH_ERR_PROGRAM_SUSPEND:
\r
107 return "Device is in program suspend state";
\r
108 case FLASH_ERR_INVALID:
\r
109 return "Invalid FLASH address";
\r
110 case FLASH_ERR_ERASE:
\r
111 return "Error trying to erase";
\r
112 case FLASH_ERR_LOCK:
\r
113 return "Error trying to lock/unlock";
\r
114 case FLASH_ERR_PROGRAM:
\r
115 return "Error trying to program";
\r
116 case FLASH_ERR_PROTOCOL:
\r
117 return "Generic error";
\r
118 case FLASH_ERR_PROTECT:
\r
119 return "Device/region is write-protected";
\r
120 case FLASH_ERR_NOT_INIT:
\r
121 return "FLASH sub-system not initialized";
\r
122 case FLASH_ERR_DRV_VERIFY:
\r
123 return "Data verify failed after operation";
\r
124 case FLASH_ERR_DRV_TIMEOUT:
\r
125 return "Driver timed out waiting for device";
\r
126 case FLASH_ERR_DRV_WRONG_PART:
\r
127 return "Driver does not support device";
\r
128 case FLASH_ERR_LOW_VOLTAGE:
\r
129 return "Device reports low voltage";
\r
131 return "Unknown error";
\r
136 /* flash bank ecosflash <base> <size> <chip_width> <bus_width> <target#> <driverPath>
\r
138 int ecosflash_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
\r
140 ecosflash_flash_bank_t *info;
\r
144 WARNING("incomplete flash_bank ecosflash configuration");
\r
145 return ERROR_FLASH_BANK_INVALID;
\r
148 info = malloc(sizeof(ecosflash_flash_bank_t));
\r
151 ERROR("no memory for flash bank info");
\r
154 bank->driver_priv = info;
\r
155 info->driverPath=strdup(args[6]);
\r
157 // eCos flash sector sizes are not exposed to OpenOCD, use 0x10000 as
\r
158 // a way to improve impeadance matach between OpenOCD and eCos flash
\r
162 bank->num_sectors=bank->size/sectorSize;
\r
163 bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
\r
164 for (i = 0; i < bank->num_sectors; i++)
\r
166 bank->sectors[i].offset = offset;
\r
167 bank->sectors[i].size = sectorSize;
\r
168 offset += bank->sectors[i].size;
\r
169 bank->sectors[i].is_erased = -1;
\r
170 bank->sectors[i].is_protected = 0;
\r
173 info->target = get_target_by_num(strtoul(args[5], NULL, 0));
\r
174 if (info->target == NULL)
\r
176 ERROR("no target '%i' configured", (int)strtoul(args[5], NULL, 0));
\r
183 int loadDriver(ecosflash_flash_bank_t *info)
\r
189 image.base_address_set = 0;
\r
190 image.start_address_set = 0;
\r
191 target_t *target=info->target;
\r
193 if (image_open(&image, info->driverPath, NULL) != ERROR_OK)
\r
195 ERROR("load_image error: %s", image.error_str);
\r
196 return ERROR_FLASH_BANK_INVALID;
\r
199 info->start_address=image.start_address;
\r
203 for (i = 0; i < image.num_sections; i++)
\r
205 void *buffer = malloc(image.sections[i].size);
\r
207 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
\r
209 ERROR("image_read_section failed with error code: %i", retval);
\r
211 image_close(&image);
\r
212 return ERROR_FLASH_BANK_INVALID;
\r
214 target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
\r
215 image_size += buf_cnt;
\r
216 DEBUG("%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
\r
221 image_close(&image);
\r
227 static int const OFFSET_ERASE=0x0;
\r
228 static int const OFFSET_ERASE_SIZE=0x8;
\r
229 static int const OFFSET_FLASH=0xc;
\r
230 static int const OFFSET_FLASH_SIZE=0x8;
\r
231 static int const OFFSET_GET_WORKAREA=0x18;
\r
232 static int const OFFSET_GET_WORKAREA_SIZE=0x4;
\r
235 int runCode(ecosflash_flash_bank_t *info,
\r
236 u32 codeStart, u32 codeStop, u32 r0, u32 r1, u32 r2,
\r
241 target_t *target=info->target;
\r
243 reg_param_t reg_params[3];
\r
244 armv4_5_algorithm_t armv4_5_info;
\r
245 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
\r
246 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
\r
247 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
\r
249 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
\r
250 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
\r
251 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
\r
253 buf_set_u32(reg_params[0].value, 0, 32, r0);
\r
254 buf_set_u32(reg_params[1].value, 0, 32, r1);
\r
255 buf_set_u32(reg_params[2].value, 0, 32, r2);
\r
258 if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params,
\r
260 codeStop, timeout,
\r
261 &armv4_5_info)) != ERROR_OK)
\r
263 ERROR("error executing eCos flash algorithm");
\r
267 *result=buf_get_u32(reg_params[0].value, 0, 32);
\r
269 destroy_reg_param(®_params[0]);
\r
270 destroy_reg_param(®_params[1]);
\r
271 destroy_reg_param(®_params[2]);
\r
276 int eCosBoard_erase(ecosflash_flash_bank_t *info, u32 address, u32 len)
\r
279 int timeout = (len / 20480 + 1) * 1000; /*asume 20 KB/s*/
\r
281 retval=loadDriver(info);
\r
282 if (retval!=ERROR_OK)
\r
286 retval=runCode(info,
\r
287 info->start_address+OFFSET_ERASE,
\r
288 info->start_address+OFFSET_ERASE+OFFSET_ERASE_SIZE,
\r
295 if (retval!=ERROR_OK)
\r
298 if (flashErr != 0x0)
\r
300 ERROR("Flash erase failed with %d (%s)\n", flashErr, flash_errmsg(flashErr));
\r
301 return ERROR_JTAG_DEVICE_ERROR;
\r
307 int eCosBoard_flash(ecosflash_flash_bank_t *info, void *data, u32 address, u32 len)
\r
309 target_t *target=info->target;
\r
310 const int chunk=8192;
\r
311 int retval=ERROR_OK;
\r
312 int timeout = (chunk / 20480 + 1) * 1000; /*asume 20 KB/s + 1 second*/
\r
314 retval=loadDriver(info);
\r
315 if (retval!=ERROR_OK)
\r
319 retval=runCode(info,
\r
320 info->start_address+OFFSET_GET_WORKAREA,
\r
321 info->start_address+OFFSET_GET_WORKAREA+OFFSET_GET_WORKAREA_SIZE,
\r
327 if (retval!=ERROR_OK)
\r
332 for (i=0; i<len; i+=chunk)
\r
341 retval=target_write_buffer(target, buffer, t, ((char *)data)+i);
\r
342 if (retval != ERROR_OK)
\r
346 retval=runCode(info,
\r
347 info->start_address+OFFSET_FLASH,
\r
348 info->start_address+OFFSET_FLASH+OFFSET_FLASH_SIZE,
\r
354 if (retval != ERROR_OK)
\r
357 if (flashErr != 0x0)
\r
359 ERROR("Flash prog failed with %d (%s)\n", flashErr, flash_errmsg(flashErr));
\r
360 return ERROR_JTAG_DEVICE_ERROR;
\r
367 int ecosflash_probe(struct flash_bank_s *bank)
\r
373 int ecosflash_register_commands(struct command_context_s *cmd_ctx)
\r
375 register_command(cmd_ctx, NULL, "ecosflash", NULL, COMMAND_ANY, NULL);
\r
381 static void command(flash_bank_t *bank, u8 cmd, u8 *cmd_buf)
\r
383 ecosflash_flash_bank_t *info = bank->driver_priv;
\r
386 if (info->target->endianness == TARGET_LITTLE_ENDIAN)
\r
388 for (i = bank->bus_width; i > 0; i--)
\r
390 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
\r
395 for (i = 1; i <= bank->bus_width; i++)
\r
397 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
\r
403 u32 ecosflash_address(struct flash_bank_s *bank, u32 address)
\r
406 switch(bank->bus_width)
\r
409 retval = address & 0xfffffffc;
\r
411 retval = address & 0xfffffffe;
\r
416 return retval + bank->base;
\r
420 int ecosflash_erase(struct flash_bank_s *bank, int first, int last)
\r
422 struct flash_bank_s *c=bank;
\r
423 ecosflash_flash_bank_t *info = bank->driver_priv;
\r
424 return eCosBoard_erase(info, c->base+first*sectorSize, sectorSize*(last-first+1));
\r
427 int ecosflash_protect(struct flash_bank_s *bank, int set, int first, int last)
\r
433 int ecosflash_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
\r
435 ecosflash_flash_bank_t *info = bank->driver_priv;
\r
436 struct flash_bank_s *c=bank;
\r
437 return eCosBoard_flash(info, buffer, c->base+offset, count);
\r
441 int ecosflash_erase_check(struct flash_bank_s *bank)
\r
446 int ecosflash_protect_check(struct flash_bank_s *bank)
\r
451 int ecosflash_info(struct flash_bank_s *bank, char *buf, int buf_size)
\r
453 ecosflash_flash_bank_t *info = bank->driver_priv;
\r
454 snprintf(buf, buf_size, "eCos flash driver: %s", info->driverPath);
\r
459 u32 ecosflash_get_flash_status(flash_bank_t *bank)
\r
464 void ecosflash_set_flash_mode(flash_bank_t *bank,int mode)
\r
469 u32 ecosflash_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout)
\r
474 int ecosflash_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r