mini51: support for Nuvoton NuMicro Mini51 series flash memory
[openocd.git] / src / flash / nor / mini51.c
blobeb74a314ae2fd92e3bb54ffd7cb00a87bac5119d
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 ***************************************************************************/
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
36 Datasheet & TRM
37 ---------------
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
43 This driver
44 -----------
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);
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
68 For implementing the read operation, please note that the APROM isn't memory mapped when booted from LDROM.
71 #ifdef HAVE_CONFIG_H
72 #include "config.h"
73 #endif
75 #include "imp.h"
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
101 #define KB 1024
102 #define PAGE_SIZE 512
103 #define TIMEOUT 1000
106 struct mini51_flash_bank {
107 bool probed;
110 enum mini51_boot_source {
111 APROM = 0,
112 LDROM = 1
116 /* Private methods */
118 static int mini51_unlock_reg(struct flash_bank *bank)
120 int status;
121 struct target *target = bank->target;
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;
133 return 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)
140 uint32_t ispcon;
141 uint32_t isprtc1;
142 bool reboot = false;
143 int status;
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)
150 return status;
152 *prev_source = (ispcon >> 1) & 1;
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;
162 if (reboot) {
163 mini51_unlock_reg(bank);
164 status = target_write_u32(target, ISPCON, ispcon);
165 if (status != ERROR_OK)
166 return status;
168 status = target_write_u32(target, IPRSTC1, IPRSTC_CPU_RST);
169 if (status != ERROR_OK)
170 return status;
172 do {
173 target_read_u32(target, IPRSTC1, &isprtc1);
174 timeout--;
175 } while ((isprtc1 & IPRSTC_CPU_RST) && timeout > 0);
177 if (timeout == 0) {
178 LOG_WARNING("Mini51 flash driver: timeout attempting to reboot\n");
179 return ERROR_FLASH_OPERATION_FAILED;
183 return ERROR_OK;
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)
193 uint32_t part_id;
194 int status;
196 status = mini51_get_part_id(bank, &part_id);
197 if (status != ERROR_OK)
198 return status;
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;
215 return ERROR_OK;
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;
228 return ERROR_OK;
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)
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;
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 */
254 new_source = LDROM;
255 status = mini51_reboot_with_source(bank, new_source, &prev_source);
256 if (status != ERROR_OK)
257 return status;
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);
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;
275 /* Erase the selected page */
276 status = target_write_u32(target, ISPTRG, ISPTRG_ISPGO);
277 if (status != ERROR_OK)
278 return status;
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;
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;
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;
308 return 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)
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;
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 */
340 new_source = LDROM;
341 status = mini51_reboot_with_source(bank, new_source, &prev_source);
342 if (status != ERROR_OK)
343 return status;
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);
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;
366 /* Write the selected word */
367 status = target_write_u32(target, ISPTRG, ISPTRG_ISPGO);
368 if (status != ERROR_OK)
369 return status;
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;
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;
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;
398 return 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");
405 return ERROR_OK;
408 static int mini51_probe(struct flash_bank *bank)
410 uint32_t flash_size;
411 int retval;
412 int num_pages;
413 uint32_t offset = 0;
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;
433 offset += PAGE_SIZE;
436 struct mini51_flash_bank *mini51_info = bank->driver_priv;
437 mini51_info->probed = true;
439 return ERROR_OK;
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)
446 return ERROR_OK;
447 return mini51_probe(bank);
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,