1 /***************************************************************************
2 * Copyright (C) 2008 by Kevin McGuire *
3 * Copyright (C) 2008 by Marcel Wijlaars *
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 ***************************************************************************/
25 #include "replacements.h"
31 #include "algorithm.h"
32 #include "binarybuffer.h"
38 int aduc702x_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct flash_bank_s
*bank
);
39 int aduc702x_register_commands(struct command_context_s
*cmd_ctx
);
40 int aduc702x_erase(struct flash_bank_s
*bank
, int first
, int last
);
41 int aduc702x_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
);
42 int aduc702x_write(struct flash_bank_s
*bank
, u8
*buffer
, u32 offset
, u32 count
);
43 int aduc702x_probe(struct flash_bank_s
*bank
);
44 int aduc702x_info(struct flash_bank_s
*bank
, char *buf
, int buf_size
);
45 int aduc702x_protect_check(struct flash_bank_s
*bank
);
47 #define ADUC702x_FLASH 0xfffff800
48 #define ADUC702x_FLASH_FEESTA (0*4)
49 #define ADUC702x_FLASH_FEEMOD (1*4)
50 #define ADUC702x_FLASH_FEECON (2*4)
51 #define ADUC702x_FLASH_FEEDAT (3*4)
52 #define ADUC702x_FLASH_FEEADR (4*4)
53 #define ADUC702x_FLASH_FEESIGN (5*4)
54 #define ADUC702x_FLASH_FEEPRO (6*4)
55 #define ADUC702x_FLASH_FEEHIDE (7*4)
66 } ADUC702x_FLASH_MMIO
;
73 flash_driver_t aduc702x_flash
=
76 .register_commands
= aduc702x_register_commands
,
77 .flash_bank_command
= aduc702x_flash_bank_command
,
78 .erase
= aduc702x_erase
,
79 .protect
= aduc702x_protect
,
80 .write
= aduc702x_write
,
81 .probe
= aduc702x_probe
,
82 .auto_probe
= aduc702x_probe
,
83 .erase_check
= default_flash_blank_check
,
84 .protect_check
= aduc702x_protect_check
,
88 int aduc702x_register_commands(struct command_context_s
*cmd_ctx
)
93 /* flash bank aduc702x <base> <size> 0 0 <target#> */
94 int aduc702x_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct flash_bank_s
*bank
)
100 LOG_WARNING("incomplete flash_bank aduc702x configuration");
101 return ERROR_FLASH_BANK_INVALID
;
104 nbank
= malloc(sizeof(aduc702x_bank
));
105 /* just warn that we are used to normally using 0x80000 */
106 if (bank
->base
!= 0x80000)
108 LOG_WARNING("Default base address is 0x80000 on the ADuC7026!");
112 bank
->driver_priv
= nbank
;
116 int aduc702x_protect_check(struct flash_bank_s
*bank
)
118 printf("aduc702x_protect_check not implemented yet.\n");
122 int aduc702x_erase(struct flash_bank_s
*bank
, int first
, int last
)
127 target_t
*target
= bank
->target
;
130 if ((first
| last
) == 0)
132 LOG_DEBUG("performing mass erase.\n");
133 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEDAT
, 0x3cff);
134 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEADR
, 0xffc3);
135 target_read_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEMOD
, &v
);
136 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEMOD
, v
| 0x8);
137 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEECON
, 0x06);
138 for (v
= 0x4; v
& 0x4;
139 target_read_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEESTA
, &v
));
143 LOG_ERROR("mass erase failed.\n");
144 return ERROR_FLASH_SECTOR_NOT_ERASED
;
146 LOG_DEBUG("mass erase successful.\n");
150 count
= last
- first
;
151 for (x
= 0; x
< count
; ++x
)
153 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEADR
, bank
->base
+ first
* 512 + x
* 512);
154 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEECON
, 0x05);
155 for (v
= 0x4; v
& 0x4; target_read_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEESTA
, &v
));
158 LOG_ERROR("erase failed for page address %x\n", bank
->base
+ first
* 512 + x
* 512);
159 return ERROR_FLASH_SECTOR_NOT_ERASED
;
161 LOG_DEBUG("erased page address %x\n", bank
->base
+ first
* 512 + x
* 512);
166 int aduc702x_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
)
168 printf("aduc702x_protect not implemented yet.\n");
169 return ERROR_FLASH_OPERATION_FAILED
;
172 int aduc702x_write(struct flash_bank_s
*bank
, u8
*buffer
, u32 offset
, u32 count
)
176 target_t
*target
= bank
->target
;
178 for (x
= 0; x
< count
; x
+= 2)
180 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEADR
, offset
+ x
);
181 /* if we able to encounter a single byte instead of a word */
182 if ((x
+ 1) == count
) {
183 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEDAT
, buffer
[x
]);
186 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEEDAT
, buffer
[x
] | (buffer
[x
+1] << 8));
188 target_write_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEECON
, 0x02);
189 for (v
= 0x4; v
& 0x4; target_read_u32(target
, ADUC702x_FLASH
+ ADUC702x_FLASH_FEESTA
, &v
));
192 LOG_ERROR("single write failed for address %x.\n", offset
+ x
);
193 return ERROR_FLASH_OPERATION_FAILED
;
195 LOG_DEBUG("single write for address %x.\n", offset
+ x
);
200 int aduc702x_probe(struct flash_bank_s
*bank
)
205 int aduc702x_info(struct flash_bank_s
*bank
, char *buf
, int buf_size
)
207 snprintf(buf
, buf_size
, "aduc702x flash driver info" );