1 /***************************************************************************
2 * Copyright (C) 2013 Cosmin Gorgovan *
3 * cosmin [at] linux-geek [dot] org *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
22 Flash driver for the Nuvoton NuMicro Mini51 series microcontrollers
24 Part |APROM Size |Part ID (at 0x5000_0000)
25 ----------------------------------------------
26 MINI51LAN 4 KB 0x00205100
27 MINI51ZAN 4 KB 0x00205103
28 MINI51TAN 4 KB 0x00205104
29 MINI52LAN 8 KB 0x00205200
30 MINI52ZAN 8 KB 0x00205203
31 MINI52TAN 8 KB 0x00205204
32 MINI54LAN 16 KB 0x00205400
33 MINI54ZAN 16 KB 0x00205403
34 MINI54TAN 16 KB 0x00205404
39 The ISP flash programming procedure is described on pages 130 and 131 of the (not very verbose) TRM.
41 http://www.keil.com/dd/docs/datashts/nuvoton/mini51/da00-mini51_52_54c1.pdf
46 * Only erase and write operations have been implemented;
47 * Both operations only support the APROM, not the LDROM;
48 * The TRM suggests that after the boot source has been selected, a software reset should be performed by
49 setting bit SWRST in ISPCON. However, this doesn't seem to have any effect on the MCU I'm using. At the
50 moment, the ARM core is reset using the IPRSTC1 register, which seems to do the trick.
52 Flash access limitations
53 ------------------------
55 APROM can only be modified when the MCU has booted off the LDROM. For write and erase operations, the
56 microcontroller will probably need to be rebooted. Pseudocode:
58 * If operation is write or erase, check bit BS (1) in ISPCON (0x5000_C000);
60 * unlock protected registers by writing 0x59, 0x16, 0x88 to RegLockAddr(0x5000_0100);
61 * set BS to 1 (LDROM);
62 * reboot by setting bit CPU_RST(1) in IPRSTC1 (0x50000008);
63 * poll CPU_RST until it is reset (not sure it's necessary);
64 * <Perform flash operation>
65 * reboot from APROM using the same procedure but writing 0 to BS
68 For implementing the read operation, please note that the APROM isn't memory mapped when booted from LDROM.
77 #define PART_ID_REG 0x50000000
78 #define IPRSTC1 0x50000008
79 #define REGLOCKADDR 0x50000100
80 #define ISPCON 0x5000C000
81 #define ISPADR 0x5000C004
82 #define ISPDAT 0x5000C008
83 #define ISPCMD 0x5000C00C
84 #define ISPTRG 0x5000C010
86 #define PART_ID_MAIN_MASK 0xFFFFFFF8
87 #define IPRSTC_CPU_RST 0x02
88 #define ISPCON_BS_LDROM 0x02
89 #define ISPCON_ISPEN 0x01
90 #define ISPCON_SWRST 0x80
91 #define ISPCON_ISPFF 0x40
92 #define ISPCMD_PROGRAM 0x21
93 #define ISPCMD_ERASE 0x22
94 #define ISPTRG_ISPGO 0x01
96 #define MINI51 0x00205100
97 #define MINI52 0x00205200
98 #define MINI54 0x00205400
100 #define MINI51_APROM_BASE 0x00000000
102 #define PAGE_SIZE 512
106 struct mini51_flash_bank
{
110 enum mini51_boot_source
{
116 /* Private methods */
118 static int mini51_unlock_reg(struct flash_bank
*bank
)
121 struct target
*target
= bank
->target
;
123 status
= target_write_u32(target
, REGLOCKADDR
, 0x59);
124 if (status
!= ERROR_OK
)
126 status
= target_write_u32(target
, REGLOCKADDR
, 0x16);
127 if (status
!= ERROR_OK
)
129 status
= target_write_u32(target
, REGLOCKADDR
, 0x88);
130 if (status
!= ERROR_OK
)
136 static int mini51_reboot_with_source(struct flash_bank
*bank
,
137 enum mini51_boot_source new_source
,
138 enum mini51_boot_source
*prev_source
)
144 int timeout
= TIMEOUT
;
146 /* Read current boot source */
147 struct target
*target
= bank
->target
;
148 status
= target_read_u32(target
, ISPCON
, &ispcon
);
149 if (status
!= ERROR_OK
)
152 *prev_source
= (ispcon
>> 1) & 1;
154 if ((new_source
== APROM
) && (*prev_source
!= APROM
)) {
155 ispcon
&= ~ISPCON_BS_LDROM
;
157 } else if ((new_source
== LDROM
) && (*prev_source
!= LDROM
)) {
158 ispcon
|= ISPCON_BS_LDROM
;
163 mini51_unlock_reg(bank
);
164 status
= target_write_u32(target
, ISPCON
, ispcon
);
165 if (status
!= ERROR_OK
)
168 status
= target_write_u32(target
, IPRSTC1
, IPRSTC_CPU_RST
);
169 if (status
!= ERROR_OK
)
173 target_read_u32(target
, IPRSTC1
, &isprtc1
);
175 } while ((isprtc1
& IPRSTC_CPU_RST
) && timeout
> 0);
178 LOG_WARNING("Mini51 flash driver: timeout attempting to reboot\n");
179 return ERROR_FLASH_OPERATION_FAILED
;
186 static int mini51_get_part_id(struct flash_bank
*bank
, uint32_t *part_id
)
188 return target_read_u32(bank
->target
, PART_ID_REG
, part_id
);
191 static int mini51_get_flash_size(struct flash_bank
*bank
, uint32_t *flash_size
)
196 status
= mini51_get_part_id(bank
, &part_id
);
197 if (status
!= ERROR_OK
)
200 switch (part_id
& PART_ID_MAIN_MASK
) {
202 *flash_size
= 4 * KB
;
205 *flash_size
= 8 * KB
;
208 *flash_size
= 16 * KB
;
219 /* Public (API) methods */
221 FLASH_BANK_COMMAND_HANDLER(mini51_flash_bank_command
)
223 struct mini51_flash_bank
*mini51_info
;
224 mini51_info
= malloc(sizeof(struct mini51_flash_bank
));
225 mini51_info
->probed
= false;
226 bank
->driver_priv
= mini51_info
;
231 static int mini51_protect_check(struct flash_bank
*bank
)
233 LOG_WARNING("Mini51 flash driver: protect_check not implemented yet\n");
235 return ERROR_FLASH_OPERATION_FAILED
;
238 static int mini51_erase(struct flash_bank
*bank
, int first
, int last
)
244 enum mini51_boot_source new_source
;
245 enum mini51_boot_source prev_source
;
246 struct target
*target
= bank
->target
;
248 if (target
->state
!= TARGET_HALTED
) {
249 LOG_ERROR("Target not halted");
250 return ERROR_TARGET_NOT_HALTED
;
253 /* TODO: add support for erasing the LDROM */
255 status
= mini51_reboot_with_source(bank
, new_source
, &prev_source
);
256 if (status
!= ERROR_OK
)
260 status
= target_read_u32(target
, ISPCON
, &ispcon
);
261 if (status
!= ERROR_OK
)
263 ispcon
|= ISPCON_ISPEN
;
264 status
= target_write_u32(target
, ISPCON
, ispcon
);
266 for (int page_start
= first
; page_start
<= last
; page_start
++) {
267 /* Set up erase command */
268 status
= target_write_u32(target
, ISPADR
, page_start
*PAGE_SIZE
);
269 if (status
!= ERROR_OK
)
271 status
= target_write_u32(target
, ISPCMD
, ISPCMD_ERASE
);
272 if (status
!= ERROR_OK
)
275 /* Erase the selected page */
276 status
= target_write_u32(target
, ISPTRG
, ISPTRG_ISPGO
);
277 if (status
!= ERROR_OK
)
280 /* Wait for for command to finish executing */
283 target_read_u32(target
, ISPTRG
, &isptrg
);
285 } while ((isptrg
& ISPTRG_ISPGO
) && (timeout
> 0));
287 LOG_WARNING("Mini51 flash driver: Timeout erasing flash\n");
288 return ERROR_FLASH_OPERATION_FAILED
;
291 /* Check for errors */
292 status
= target_read_u32(target
, ISPCON
, &ispcon
);
293 if (status
!= ERROR_OK
)
295 if (ispcon
& ISPCON_ISPFF
) {
296 LOG_WARNING("Mini51 flash driver: Erase operation failed\n");
297 return ERROR_FLASH_OPERATION_FAILED
;
301 /* Reboot from previous source */
302 if (prev_source
!= new_source
) {
303 status
= mini51_reboot_with_source(bank
, prev_source
, &new_source
);
304 if (status
!= ERROR_OK
)
311 static int mini51_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
313 LOG_WARNING("Mini51 flash driver: protect operation not implemented yet\n");
315 return ERROR_FLASH_OPERATION_FAILED
;
318 static int mini51_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
325 enum mini51_boot_source new_source
;
326 enum mini51_boot_source prev_source
;
327 struct target
*target
= bank
->target
;
329 if (target
->state
!= TARGET_HALTED
) {
330 LOG_ERROR("Target not halted");
331 return ERROR_TARGET_NOT_HALTED
;
334 if ((offset
& 0x3) || (count
& 0x3)) {
335 LOG_WARNING("Mini51 flash driver: unaligned access not supported\n");
336 return ERROR_FLASH_OPERATION_FAILED
;
339 /* TODO: add support for writing to LDROM */
341 status
= mini51_reboot_with_source(bank
, new_source
, &prev_source
);
342 if (status
!= ERROR_OK
)
346 status
= target_read_u32(target
, ISPCON
, &ispcon
);
347 if (status
!= ERROR_OK
)
349 ispcon
|= ISPCON_ISPEN
;
350 status
= target_write_u32(target
, ISPCON
, ispcon
);
352 for (uint32_t i
= offset
; i
< offset
+ count
; i
+= 4) {
353 /* Set up program command */
354 status
= target_write_u32(target
, ISPADR
, i
);
355 if (status
!= ERROR_OK
)
357 status
= target_write_u32(target
, ISPCMD
, ISPCMD_PROGRAM
);
358 if (status
!= ERROR_OK
)
360 memcpy(&ispdat
, buffer
, sizeof(ispdat
));
361 buffer
+= sizeof(ispdat
);
362 status
= target_write_u32(target
, ISPDAT
, ispdat
);
363 if (status
!= ERROR_OK
)
366 /* Write the selected word */
367 status
= target_write_u32(target
, ISPTRG
, ISPTRG_ISPGO
);
368 if (status
!= ERROR_OK
)
371 /* Wait for for command to finish executing */
374 target_read_u32(target
, ISPTRG
, &isptrg
);
376 } while ((isptrg
& ISPTRG_ISPGO
) && (timeout
> 0));
378 LOG_WARNING("Mini51 flash driver: Timeout programming flash\n");
379 return ERROR_FLASH_OPERATION_FAILED
;
382 /* Check for errors */
383 status
= target_read_u32(target
, ISPCON
, &ispcon
);
384 if (status
!= ERROR_OK
)
386 if (ispcon
& ISPCON_ISPFF
) {
387 LOG_WARNING("Mini51 flash driver: Programming operation failed\n");
388 return ERROR_FLASH_OPERATION_FAILED
;
392 if (prev_source
!= new_source
) {
393 status
= mini51_reboot_with_source(bank
, prev_source
, &new_source
);
394 if (status
!= ERROR_OK
)
402 static int get_mini51_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
404 snprintf(buf
, buf_size
, "Mini51 flash driver");
408 static int mini51_probe(struct flash_bank
*bank
)
415 retval
= mini51_get_flash_size(bank
, &flash_size
);
416 if (retval
!= ERROR_OK
|| flash_size
== 0) {
417 LOG_WARNING("Mini51 flash driver: Failed to detect a known part\n");
418 return ERROR_FLASH_OPERATION_FAILED
;
421 num_pages
= flash_size
/ PAGE_SIZE
;
423 bank
->base
= MINI51_APROM_BASE
;
424 bank
->num_sectors
= num_pages
;
425 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
426 bank
->size
= flash_size
;
428 for (int i
= 0; i
< num_pages
; i
++) {
429 bank
->sectors
[i
].offset
= offset
;
430 bank
->sectors
[i
].size
= PAGE_SIZE
;
431 bank
->sectors
[i
].is_erased
= -1;
432 bank
->sectors
[i
].is_protected
= 0;
436 struct mini51_flash_bank
*mini51_info
= bank
->driver_priv
;
437 mini51_info
->probed
= true;
442 static int mini51_auto_probe(struct flash_bank
*bank
)
444 struct mini51_flash_bank
*mini51_info
= bank
->driver_priv
;
445 if (mini51_info
->probed
)
447 return mini51_probe(bank
);
450 struct flash_driver mini51_flash
= {
452 .flash_bank_command
= mini51_flash_bank_command
,
453 .erase
= mini51_erase
,
454 .protect
= mini51_protect
,
455 .write
= mini51_write
,
456 .read
= default_flash_read
,
457 .probe
= mini51_probe
,
458 .auto_probe
= mini51_auto_probe
,
459 .erase_check
= default_flash_blank_check
,
460 .protect_check
= mini51_protect_check
,
461 .info
= get_mini51_info
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)