mini51: support for Nuvoton NuMicro Mini51 series flash memory
[openocd.git] / src / flash / nor / mini51.c
1 /***************************************************************************
2 * Copyright (C) 2013 Cosmin Gorgovan *
3 * cosmin [at] linux-geek [dot] org *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 /*
22 Flash driver for the Nuvoton NuMicro Mini51 series microcontrollers
23
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
35
36 Datasheet & TRM
37 ---------------
38
39 The ISP flash programming procedure is described on pages 130 and 131 of the (not very verbose) TRM.
40
41 http://www.keil.com/dd/docs/datashts/nuvoton/mini51/da00-mini51_52_54c1.pdf
42
43 This driver
44 -----------
45
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.
51
52 Flash access limitations
53 ------------------------
54
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:
57
58 * If operation is write or erase, check bit BS (1) in ISPCON (0x5000_C000);
59 * If BS is 0 (APROM):
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
66
67
68 For implementing the read operation, please note that the APROM isn't memory mapped when booted from LDROM.
69 */
70
71 #ifdef HAVE_CONFIG_H
72 #include "config.h"
73 #endif
74
75 #include "imp.h"
76
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
85
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
95
96 #define MINI51 0x00205100
97 #define MINI52 0x00205200
98 #define MINI54 0x00205400
99
100 #define MINI51_APROM_BASE 0x00000000
101 #define KB 1024
102 #define PAGE_SIZE 512
103 #define TIMEOUT 1000
104
105
106 struct mini51_flash_bank {
107 bool probed;
108 };
109
110 enum mini51_boot_source {
111 APROM = 0,
112 LDROM = 1
113 };
114
115
116 /* Private methods */
117
118 static int mini51_unlock_reg(struct flash_bank *bank)
119 {
120 int status;
121 struct target *target = bank->target;
122
123 status = target_write_u32(target, REGLOCKADDR, 0x59);
124 if (status != ERROR_OK)
125 return status;
126 status = target_write_u32(target, REGLOCKADDR, 0x16);
127 if (status != ERROR_OK)
128 return status;
129 status = target_write_u32(target, REGLOCKADDR, 0x88);
130 if (status != ERROR_OK)
131 return status;
132
133 return ERROR_OK;
134 }
135
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)
139 {
140 uint32_t ispcon;
141 uint32_t isprtc1;
142 bool reboot = false;
143 int status;
144 int timeout = TIMEOUT;
145
146 /* Read current boot source */
147 struct target *target = bank->target;
148 status = target_read_u32(target, ISPCON, &ispcon);
149 if (status != ERROR_OK)
150 return status;
151
152 *prev_source = (ispcon >> 1) & 1;
153
154 if ((new_source == APROM) && (*prev_source != APROM)) {
155 ispcon &= ~ISPCON_BS_LDROM;
156 reboot = true;
157 } else if ((new_source == LDROM) && (*prev_source != LDROM)) {
158 ispcon |= ISPCON_BS_LDROM;
159 reboot = true;
160 }
161
162 if (reboot) {
163 mini51_unlock_reg(bank);
164 status = target_write_u32(target, ISPCON, ispcon);
165 if (status != ERROR_OK)
166 return status;
167
168 status = target_write_u32(target, IPRSTC1, IPRSTC_CPU_RST);
169 if (status != ERROR_OK)
170 return status;
171
172 do {
173 target_read_u32(target, IPRSTC1, &isprtc1);
174 timeout--;
175 } while ((isprtc1 & IPRSTC_CPU_RST) && timeout > 0);
176
177 if (timeout == 0) {
178 LOG_WARNING("Mini51 flash driver: timeout attempting to reboot\n");
179 return ERROR_FLASH_OPERATION_FAILED;
180 }
181 }
182
183 return ERROR_OK;
184 }
185
186 static int mini51_get_part_id(struct flash_bank *bank, uint32_t *part_id)
187 {
188 return target_read_u32(bank->target, PART_ID_REG, part_id);
189 }
190
191 static int mini51_get_flash_size(struct flash_bank *bank, uint32_t *flash_size)
192 {
193 uint32_t part_id;
194 int status;
195
196 status = mini51_get_part_id(bank, &part_id);
197 if (status != ERROR_OK)
198 return status;
199
200 switch (part_id & PART_ID_MAIN_MASK) {
201 case MINI51:
202 *flash_size = 4 * KB;
203 break;
204 case MINI52:
205 *flash_size = 8 * KB;
206 break;
207 case MINI54:
208 *flash_size = 16 * KB;
209 break;
210 default:
211 *flash_size = 0;
212 break;
213 }
214
215 return ERROR_OK;
216 }
217
218
219 /* Public (API) methods */
220
221 FLASH_BANK_COMMAND_HANDLER(mini51_flash_bank_command)
222 {
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;
227
228 return ERROR_OK;
229 }
230
231 static int mini51_protect_check(struct flash_bank *bank)
232 {
233 LOG_WARNING("Mini51 flash driver: protect_check not implemented yet\n");
234
235 return ERROR_FLASH_OPERATION_FAILED;
236 }
237
238 static int mini51_erase(struct flash_bank *bank, int first, int last)
239 {
240 int status;
241 int timeout;
242 uint32_t ispcon;
243 uint32_t isptrg;
244 enum mini51_boot_source new_source;
245 enum mini51_boot_source prev_source;
246 struct target *target = bank->target;
247
248 if (target->state != TARGET_HALTED) {
249 LOG_ERROR("Target not halted");
250 return ERROR_TARGET_NOT_HALTED;
251 }
252
253 /* TODO: add support for erasing the LDROM */
254 new_source = LDROM;
255 status = mini51_reboot_with_source(bank, new_source, &prev_source);
256 if (status != ERROR_OK)
257 return status;
258
259 /* Enable ISP */
260 status = target_read_u32(target, ISPCON, &ispcon);
261 if (status != ERROR_OK)
262 return status;
263 ispcon |= ISPCON_ISPEN;
264 status = target_write_u32(target, ISPCON, ispcon);
265
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)
270 return status;
271 status = target_write_u32(target, ISPCMD, ISPCMD_ERASE);
272 if (status != ERROR_OK)
273 return status;
274
275 /* Erase the selected page */
276 status = target_write_u32(target, ISPTRG, ISPTRG_ISPGO);
277 if (status != ERROR_OK)
278 return status;
279
280 /* Wait for for command to finish executing */
281 timeout = TIMEOUT;
282 do {
283 target_read_u32(target, ISPTRG, &isptrg);
284 timeout--;
285 } while ((isptrg & ISPTRG_ISPGO) && (timeout > 0));
286 if (timeout == 0) {
287 LOG_WARNING("Mini51 flash driver: Timeout erasing flash\n");
288 return ERROR_FLASH_OPERATION_FAILED;
289 }
290
291 /* Check for errors */
292 status = target_read_u32(target, ISPCON, &ispcon);
293 if (status != ERROR_OK)
294 return status;
295 if (ispcon & ISPCON_ISPFF) {
296 LOG_WARNING("Mini51 flash driver: Erase operation failed\n");
297 return ERROR_FLASH_OPERATION_FAILED;
298 }
299 }
300
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)
305 return status;
306 }
307
308 return ERROR_OK;
309 }
310
311 static int mini51_protect(struct flash_bank *bank, int set, int first, int last)
312 {
313 LOG_WARNING("Mini51 flash driver: protect operation not implemented yet\n");
314
315 return ERROR_FLASH_OPERATION_FAILED;
316 }
317
318 static int mini51_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
319 {
320 int status;
321 int timeout;
322 uint32_t ispcon;
323 uint32_t isptrg;
324 uint32_t ispdat;
325 enum mini51_boot_source new_source;
326 enum mini51_boot_source prev_source;
327 struct target *target = bank->target;
328
329 if (target->state != TARGET_HALTED) {
330 LOG_ERROR("Target not halted");
331 return ERROR_TARGET_NOT_HALTED;
332 }
333
334 if ((offset & 0x3) || (count & 0x3)) {
335 LOG_WARNING("Mini51 flash driver: unaligned access not supported\n");
336 return ERROR_FLASH_OPERATION_FAILED;
337 }
338
339 /* TODO: add support for writing to LDROM */
340 new_source = LDROM;
341 status = mini51_reboot_with_source(bank, new_source, &prev_source);
342 if (status != ERROR_OK)
343 return status;
344
345 /* Enable ISP */
346 status = target_read_u32(target, ISPCON, &ispcon);
347 if (status != ERROR_OK)
348 return status;
349 ispcon |= ISPCON_ISPEN;
350 status = target_write_u32(target, ISPCON, ispcon);
351
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)
356 return status;
357 status = target_write_u32(target, ISPCMD, ISPCMD_PROGRAM);
358 if (status != ERROR_OK)
359 return status;
360 memcpy(&ispdat, buffer, sizeof(ispdat));
361 buffer += sizeof(ispdat);
362 status = target_write_u32(target, ISPDAT, ispdat);
363 if (status != ERROR_OK)
364 return status;
365
366 /* Write the selected word */
367 status = target_write_u32(target, ISPTRG, ISPTRG_ISPGO);
368 if (status != ERROR_OK)
369 return status;
370
371 /* Wait for for command to finish executing */
372 timeout = TIMEOUT;
373 do {
374 target_read_u32(target, ISPTRG, &isptrg);
375 timeout--;
376 } while ((isptrg & ISPTRG_ISPGO) && (timeout > 0));
377 if (timeout == 0) {
378 LOG_WARNING("Mini51 flash driver: Timeout programming flash\n");
379 return ERROR_FLASH_OPERATION_FAILED;
380 }
381
382 /* Check for errors */
383 status = target_read_u32(target, ISPCON, &ispcon);
384 if (status != ERROR_OK)
385 return status;
386 if (ispcon & ISPCON_ISPFF) {
387 LOG_WARNING("Mini51 flash driver: Programming operation failed\n");
388 return ERROR_FLASH_OPERATION_FAILED;
389 }
390 }
391
392 if (prev_source != new_source) {
393 status = mini51_reboot_with_source(bank, prev_source, &new_source);
394 if (status != ERROR_OK)
395 return status;
396 }
397
398 return ERROR_OK;
399 }
400
401
402 static int get_mini51_info(struct flash_bank *bank, char *buf, int buf_size)
403 {
404 snprintf(buf, buf_size, "Mini51 flash driver");
405 return ERROR_OK;
406 }
407
408 static int mini51_probe(struct flash_bank *bank)
409 {
410 uint32_t flash_size;
411 int retval;
412 int num_pages;
413 uint32_t offset = 0;
414
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;
419 }
420
421 num_pages = flash_size / PAGE_SIZE;
422
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;
427
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;
433 offset += PAGE_SIZE;
434 }
435
436 struct mini51_flash_bank *mini51_info = bank->driver_priv;
437 mini51_info->probed = true;
438
439 return ERROR_OK;
440 }
441
442 static int mini51_auto_probe(struct flash_bank *bank)
443 {
444 struct mini51_flash_bank *mini51_info = bank->driver_priv;
445 if (mini51_info->probed)
446 return ERROR_OK;
447 return mini51_probe(bank);
448 }
449
450 struct flash_driver mini51_flash = {
451 .name = "mini51",
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,
462 };
463

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)