1 /***************************************************************************
2 * Copyright (C) 2008 Øyvind Harboe *
3 * oyvind.harboe@zylin.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
26 #include "replacements.h"
36 #include "binarybuffer.h"
37 #include "../target/embeddedice.h"
42 int ecosflash_register_commands(struct command_context_s
*cmd_ctx
);
43 int ecosflash_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct flash_bank_s
*bank
);
44 int ecosflash_erase(struct flash_bank_s
*bank
, int first
, int last
);
45 int ecosflash_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
);
46 int ecosflash_write(struct flash_bank_s
*bank
, u8
*buffer
, u32 offset
, u32 count
);
47 int ecosflash_probe(struct flash_bank_s
*bank
);
48 int ecosflash_protect_check(struct flash_bank_s
*bank
);
49 int ecosflash_info(struct flash_bank_s
*bank
, char *buf
, int buf_size
);
51 u32
ecosflash_get_flash_status(flash_bank_t
*bank
);
52 void ecosflash_set_flash_mode(flash_bank_t
*bank
,int mode
);
53 u32
ecosflash_wait_status_busy(flash_bank_t
*bank
, u32 waitbits
, int timeout
);
54 int ecosflash_handle_gpnvm_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
56 flash_driver_t ecosflash_flash
=
59 .register_commands
= ecosflash_register_commands
,
60 .flash_bank_command
= ecosflash_flash_bank_command
,
61 .erase
= ecosflash_erase
,
62 .protect
= ecosflash_protect
,
63 .write
= ecosflash_write
,
64 .probe
= ecosflash_probe
,
65 .auto_probe
= ecosflash_probe
,
66 .erase_check
= default_flash_blank_check
,
67 .protect_check
= ecosflash_protect_check
,
68 .info
= ecosflash_info
71 typedef struct ecosflash_flash_bank_s
73 struct target_s
*target
;
74 working_area_t
*write_algorithm
;
75 working_area_t
*erase_check_algorithm
;
78 } ecosflash_flash_bank_t
;
80 static const int sectorSize
=0x10000;
83 flash_errmsg(int err
);
86 #define FLASH_ERR_OK 0x00 /* No error - operation complete */
87 #define FLASH_ERR_INVALID 0x01 /* Invalid FLASH address */
88 #define FLASH_ERR_ERASE 0x02 /* Error trying to erase */
89 #define FLASH_ERR_LOCK 0x03 /* Error trying to lock/unlock */
90 #define FLASH_ERR_PROGRAM 0x04 /* Error trying to program */
91 #define FLASH_ERR_PROTOCOL 0x05 /* Generic error */
92 #define FLASH_ERR_PROTECT 0x06 /* Device/region is write-protected */
93 #define FLASH_ERR_NOT_INIT 0x07 /* FLASH info not yet initialized */
94 #define FLASH_ERR_HWR 0x08 /* Hardware (configuration?) problem */
95 #define FLASH_ERR_ERASE_SUSPEND 0x09 /* Device is in erase suspend mode */
96 #define FLASH_ERR_PROGRAM_SUSPEND 0x0a /* Device is in in program suspend mode */
97 #define FLASH_ERR_DRV_VERIFY 0x0b /* Driver failed to verify data */
98 #define FLASH_ERR_DRV_TIMEOUT 0x0c /* Driver timed out waiting for device */
99 #define FLASH_ERR_DRV_WRONG_PART 0x0d /* Driver does not support device */
100 #define FLASH_ERR_LOW_VOLTAGE 0x0e /* Not enough juice to complete job */
104 flash_errmsg(int err
)
108 return "No error - operation complete";
109 case FLASH_ERR_ERASE_SUSPEND
:
110 return "Device is in erase suspend state";
111 case FLASH_ERR_PROGRAM_SUSPEND
:
112 return "Device is in program suspend state";
113 case FLASH_ERR_INVALID
:
114 return "Invalid FLASH address";
115 case FLASH_ERR_ERASE
:
116 return "Error trying to erase";
118 return "Error trying to lock/unlock";
119 case FLASH_ERR_PROGRAM
:
120 return "Error trying to program";
121 case FLASH_ERR_PROTOCOL
:
122 return "Generic error";
123 case FLASH_ERR_PROTECT
:
124 return "Device/region is write-protected";
125 case FLASH_ERR_NOT_INIT
:
126 return "FLASH sub-system not initialized";
127 case FLASH_ERR_DRV_VERIFY
:
128 return "Data verify failed after operation";
129 case FLASH_ERR_DRV_TIMEOUT
:
130 return "Driver timed out waiting for device";
131 case FLASH_ERR_DRV_WRONG_PART
:
132 return "Driver does not support device";
133 case FLASH_ERR_LOW_VOLTAGE
:
134 return "Device reports low voltage";
136 return "Unknown error";
141 /* flash bank ecosflash <base> <size> <chip_width> <bus_width> <target#> <driverPath>
143 int ecosflash_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct flash_bank_s
*bank
)
145 ecosflash_flash_bank_t
*info
;
149 LOG_WARNING("incomplete flash_bank ecosflash configuration");
150 return ERROR_FLASH_BANK_INVALID
;
153 info
= malloc(sizeof(ecosflash_flash_bank_t
));
156 LOG_ERROR("no memory for flash bank info");
159 bank
->driver_priv
= info
;
160 info
->driverPath
=strdup(args
[6]);
162 /* eCos flash sector sizes are not exposed to OpenOCD, use 0x10000 as
163 * a way to improve impeadance matach between OpenOCD and eCos flash
168 bank
->num_sectors
=bank
->size
/sectorSize
;
169 bank
->sectors
= malloc(sizeof(flash_sector_t
) * bank
->num_sectors
);
170 for (i
= 0; i
< bank
->num_sectors
; i
++)
172 bank
->sectors
[i
].offset
= offset
;
173 bank
->sectors
[i
].size
= sectorSize
;
174 offset
+= bank
->sectors
[i
].size
;
175 bank
->sectors
[i
].is_erased
= -1;
176 bank
->sectors
[i
].is_protected
= 0;
179 info
->target
= get_target_by_num(strtoul(args
[5], NULL
, 0));
180 if (info
->target
== NULL
)
182 LOG_ERROR("no target '%i' configured", (int)strtoul(args
[5], NULL
, 0));
189 int loadDriver(ecosflash_flash_bank_t
*info
)
195 image
.base_address_set
= 0;
196 image
.start_address_set
= 0;
197 target_t
*target
=info
->target
;
200 if ((retval
=image_open(&image
, info
->driverPath
, NULL
)) != ERROR_OK
)
205 info
->start_address
=image
.start_address
;
209 for (i
= 0; i
< image
.num_sections
; i
++)
211 void *buffer
= malloc(image
.sections
[i
].size
);
213 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
219 target_write_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, buffer
);
220 image_size
+= buf_cnt
;
221 LOG_DEBUG("%u byte written at address 0x%8.8x", buf_cnt
, image
.sections
[i
].base_address
);
232 static int const OFFSET_ERASE
=0x0;
233 static int const OFFSET_ERASE_SIZE
=0x8;
234 static int const OFFSET_FLASH
=0xc;
235 static int const OFFSET_FLASH_SIZE
=0x8;
236 static int const OFFSET_GET_WORKAREA
=0x18;
237 static int const OFFSET_GET_WORKAREA_SIZE
=0x4;
240 int runCode(ecosflash_flash_bank_t
*info
,
241 u32 codeStart
, u32 codeStop
, u32 r0
, u32 r1
, u32 r2
,
246 target_t
*target
=info
->target
;
248 reg_param_t reg_params
[3];
249 armv4_5_algorithm_t armv4_5_info
;
250 armv4_5_info
.common_magic
= ARMV4_5_COMMON_MAGIC
;
251 armv4_5_info
.core_mode
= ARMV4_5_MODE_SVC
;
252 armv4_5_info
.core_state
= ARMV4_5_STATE_ARM
;
254 init_reg_param(®_params
[0], "r0", 32, PARAM_IN_OUT
);
255 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
256 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
258 buf_set_u32(reg_params
[0].value
, 0, 32, r0
);
259 buf_set_u32(reg_params
[1].value
, 0, 32, r1
);
260 buf_set_u32(reg_params
[2].value
, 0, 32, r2
);
263 if ((retval
= target
->type
->run_algorithm(target
, 0, NULL
, 3, reg_params
,
266 &armv4_5_info
)) != ERROR_OK
)
268 LOG_ERROR("error executing eCos flash algorithm");
272 *result
=buf_get_u32(reg_params
[0].value
, 0, 32);
274 destroy_reg_param(®_params
[0]);
275 destroy_reg_param(®_params
[1]);
276 destroy_reg_param(®_params
[2]);
281 int eCosBoard_erase(ecosflash_flash_bank_t
*info
, u32 address
, u32 len
)
284 int timeout
= (len
/ 20480 + 1) * 1000; /*asume 20 KB/s*/
286 retval
=loadDriver(info
);
287 if (retval
!=ERROR_OK
)
292 info
->start_address
+OFFSET_ERASE
,
293 info
->start_address
+OFFSET_ERASE
+OFFSET_ERASE_SIZE
,
300 if (retval
!=ERROR_OK
)
305 LOG_ERROR("Flash erase failed with %d (%s)\n", flashErr
, flash_errmsg(flashErr
));
312 int eCosBoard_flash(ecosflash_flash_bank_t
*info
, void *data
, u32 address
, u32 len
)
314 target_t
*target
=info
->target
;
315 const int chunk
=8192;
317 int timeout
= (chunk
/ 20480 + 1) * 1000; /*asume 20 KB/s + 1 second*/
319 retval
=loadDriver(info
);
320 if (retval
!=ERROR_OK
)
325 info
->start_address
+OFFSET_GET_WORKAREA
,
326 info
->start_address
+OFFSET_GET_WORKAREA
+OFFSET_GET_WORKAREA_SIZE
,
332 if (retval
!=ERROR_OK
)
337 for (i
=0; i
<len
; i
+=chunk
)
346 retval
=target_write_buffer(target
, buffer
, t
, ((u8
*)data
)+i
);
347 if (retval
!= ERROR_OK
)
352 info
->start_address
+OFFSET_FLASH
,
353 info
->start_address
+OFFSET_FLASH
+OFFSET_FLASH_SIZE
,
359 if (retval
!= ERROR_OK
)
364 LOG_ERROR("Flash prog failed with %d (%s)\n", flashErr
, flash_errmsg(flashErr
));
372 int ecosflash_probe(struct flash_bank_s
*bank
)
378 int ecosflash_register_commands(struct command_context_s
*cmd_ctx
)
380 register_command(cmd_ctx
, NULL
, "ecosflash", NULL
, COMMAND_ANY
, NULL
);
386 static void command(flash_bank_t *bank, u8 cmd, u8 *cmd_buf)
388 ecosflash_flash_bank_t *info = bank->driver_priv;
391 if (info->target->endianness == TARGET_LITTLE_ENDIAN)
393 for (i = bank->bus_width; i > 0; i--)
395 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
400 for (i = 1; i <= bank->bus_width; i++)
402 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
408 u32
ecosflash_address(struct flash_bank_s
*bank
, u32 address
)
411 switch(bank
->bus_width
)
414 retval
= address
& 0xfffffffc;
416 retval
= address
& 0xfffffffe;
421 return retval
+ bank
->base
;
425 int ecosflash_erase(struct flash_bank_s
*bank
, int first
, int last
)
427 struct flash_bank_s
*c
=bank
;
428 ecosflash_flash_bank_t
*info
= bank
->driver_priv
;
429 return eCosBoard_erase(info
, c
->base
+first
*sectorSize
, sectorSize
*(last
-first
+1));
432 int ecosflash_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
)
438 int ecosflash_write(struct flash_bank_s
*bank
, u8
*buffer
, u32 offset
, u32 count
)
440 ecosflash_flash_bank_t
*info
= bank
->driver_priv
;
441 struct flash_bank_s
*c
=bank
;
442 return eCosBoard_flash(info
, buffer
, c
->base
+offset
, count
);
446 int ecosflash_protect_check(struct flash_bank_s
*bank
)
451 int ecosflash_info(struct flash_bank_s
*bank
, char *buf
, int buf_size
)
453 ecosflash_flash_bank_t
*info
= bank
->driver_priv
;
454 snprintf(buf
, buf_size
, "eCos flash driver: %s", info
->driverPath
);
459 u32
ecosflash_get_flash_status(flash_bank_t
*bank
)
464 void ecosflash_set_flash_mode(flash_bank_t
*bank
,int mode
)
469 u32
ecosflash_wait_status_busy(flash_bank_t
*bank
, u32 waitbits
, int timeout
)
474 int ecosflash_handle_gpnvm_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)