flash: avoid checking for non NULL pointer to free it
[fw/openocd] / src / flash / nor / ambiqmicro.c
1 /******************************************************************************
2  *
3  * @file ambiqmicro.c
4  *
5  * @brief Ambiq Micro flash driver.
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  * Copyright (c) 2015, David Racine <dracine at ambiqmicro.com>
11  *
12  * Copyright (c) 2016, Rick Foos <rfoos at solengtech.com>
13  *
14  * Copyright (c) 2015-2016, Ambiq Micro, Inc.
15  *
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are met:
20  *
21  * 1. Redistributions of source code must retain the above copyright notice,
22  * this list of conditions and the following disclaimer.
23  *
24  * 2. Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in the
26  * documentation and/or other materials provided with the distribution.
27  *
28  * 3. Neither the name of the copyright holder nor the names of its
29  * contributors may be used to endorse or promote products derived from this
30  * software without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGE.
43  *
44  *****************************************************************************/
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "jtag/interface.h"
50 #include "imp.h"
51 #include "target/algorithm.h"
52 #include "target/armv7m.h"
53 #include "target/cortex_m.h"
54
55 /** Check error, log error. */
56 #define CHECK_STATUS(rc, msg) { \
57                 if (rc != ERROR_OK) { \
58                         LOG_ERROR("status(%d):%s\n", rc, msg); } }
59
60 /*
61  * Address and Key defines.
62  */
63 #define PROGRAM_KEY      (0x12344321)
64 #define OTP_PROGRAM_KEY  (0x87655678)
65
66 #define FLASH_PROGRAM_MAIN_FROM_SRAM                0x0800005d
67 #define FLASH_PROGRAM_OTP_FROM_SRAM                 0x08000061
68 #define FLASH_ERASE_LIST_MAIN_PAGES_FROM_SRAM       0x08000065
69 #define FLASH_MASS_ERASE_MAIN_PAGES_FROM_SRAM       0x08000069
70
71
72 static const uint32_t apollo_flash_size[] = {
73         1 << 15,
74         1 << 16,
75         1 << 17,
76         1 << 18,
77         1 << 19,
78         1 << 20,
79         1 << 21
80 };
81
82 static const uint32_t apollo_sram_size[] = {
83         1 << 15,
84         1 << 16,
85         1 << 17,
86         1 << 18,
87         1 << 19,
88         1 << 20,
89         1 << 21
90 };
91
92 struct ambiqmicro_flash_bank {
93         /* chip id register */
94
95         bool probed;
96
97         const char *target_name;
98         uint8_t target_class;
99
100         uint32_t sramsiz;
101         uint32_t flshsiz;
102
103         /* flash geometry */
104         uint32_t num_pages;
105         uint32_t pagesize;
106         uint32_t pages_in_lockregion;
107
108         /* nv memory bits */
109         uint16_t num_lockbits;
110
111         /* main clock status */
112         uint32_t rcc;
113         uint32_t rcc2;
114         uint8_t mck_valid;
115         uint8_t xtal_mask;
116         uint32_t iosc_freq;
117         uint32_t mck_freq;
118         const char *iosc_desc;
119         const char *mck_desc;
120 };
121
122 static struct {
123         uint8_t class;
124         uint8_t partno;
125         const char *partname;
126 } ambiqmicroParts[6] = {
127         {0xFF, 0x00, "Unknown"},
128         {0x01, 0x00, "Apollo"},
129         {0x02, 0x00, "Apollo2"},
130         {0x03, 0x00, "Unknown"},
131         {0x04, 0x00, "Unknown"},
132         {0x05, 0x00, "Apollo"},
133 };
134
135 static char *ambiqmicroClassname[6] = {
136         "Unknown", "Apollo", "Apollo2", "Unknown", "Unknown", "Apollo"
137 };
138
139 /***************************************************************************
140 *       openocd command interface                                              *
141 ***************************************************************************/
142
143 /* flash_bank ambiqmicro <base> <size> 0 0 <target#>
144  */
145 FLASH_BANK_COMMAND_HANDLER(ambiqmicro_flash_bank_command)
146 {
147         struct ambiqmicro_flash_bank *ambiqmicro_info;
148
149         if (CMD_ARGC < 6)
150                 return ERROR_COMMAND_SYNTAX_ERROR;
151
152         ambiqmicro_info = calloc(sizeof(struct ambiqmicro_flash_bank), 1);
153
154         bank->driver_priv = ambiqmicro_info;
155
156         ambiqmicro_info->target_name = "Unknown target";
157
158         /* part wasn't probed yet */
159         ambiqmicro_info->probed = false;
160
161         return ERROR_OK;
162 }
163
164 static int get_ambiqmicro_info(struct flash_bank *bank, char *buf, int buf_size)
165 {
166         struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
167         int printed;
168         char *classname;
169
170         if (!ambiqmicro_info->probed) {
171                 LOG_ERROR("Target not probed");
172                 return ERROR_FLASH_BANK_NOT_PROBED;
173         }
174
175         /* Check class name in range. */
176         if (ambiqmicro_info->target_class < sizeof(ambiqmicroClassname))
177                 classname = ambiqmicroClassname[ambiqmicro_info->target_class];
178         else
179                 classname = ambiqmicroClassname[0];
180
181         printed = snprintf(buf,
182                 buf_size,
183                 "\nAmbiq Micro information: Chip is "
184                 "class %d (%s) %s\n",
185                 ambiqmicro_info->target_class,
186                 classname,
187                 ambiqmicro_info->target_name);
188
189         if ((printed < 0))
190                 return ERROR_BUF_TOO_SMALL;
191         return ERROR_OK;
192 }
193
194 /***************************************************************************
195 *       chip identification and status                                         *
196 ***************************************************************************/
197
198 /* Fill in driver info structure */
199 static int ambiqmicro_read_part_info(struct flash_bank *bank)
200 {
201         struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
202         struct target *target = bank->target;
203         uint32_t PartNum = 0;
204         int retval;
205
206         /*
207          * Read Part Number.
208          */
209         retval = target_read_u32(target, 0x40020000, &PartNum);
210         if (retval != ERROR_OK) {
211                 LOG_ERROR("status(0x%x):Could not read PartNum.\n", retval);
212                 /* Set PartNum to default device */
213                 PartNum = 0;
214         }
215         LOG_DEBUG("Part number: 0x%x", PartNum);
216
217         /*
218          * Determine device class.
219          */
220         ambiqmicro_info->target_class = (PartNum & 0xFF000000) >> 24;
221
222         switch (ambiqmicro_info->target_class) {
223                 case 1:         /* 1 - Apollo */
224                 case 5:         /* 5 - Apollo Bootloader */
225                         bank->base = bank->bank_number * 0x40000;
226                         ambiqmicro_info->pagesize = 2048;
227                         ambiqmicro_info->flshsiz =
228                         apollo_flash_size[(PartNum & 0x00F00000) >> 20];
229                         ambiqmicro_info->sramsiz =
230                         apollo_sram_size[(PartNum & 0x000F0000) >> 16];
231                         ambiqmicro_info->num_pages = ambiqmicro_info->flshsiz /
232                         ambiqmicro_info->pagesize;
233                         if (ambiqmicro_info->num_pages > 128) {
234                                 ambiqmicro_info->num_pages = 128;
235                                 ambiqmicro_info->flshsiz = 1024 * 256;
236                         }
237                         break;
238
239                 default:
240                         LOG_INFO("Unknown Class. Using Apollo-64 as default.");
241
242                         bank->base = bank->bank_number * 0x40000;
243                         ambiqmicro_info->pagesize = 2048;
244                         ambiqmicro_info->flshsiz = apollo_flash_size[1];
245                         ambiqmicro_info->sramsiz = apollo_sram_size[0];
246                         ambiqmicro_info->num_pages = ambiqmicro_info->flshsiz /
247                         ambiqmicro_info->pagesize;
248                         if (ambiqmicro_info->num_pages > 128) {
249                                 ambiqmicro_info->num_pages = 128;
250                                 ambiqmicro_info->flshsiz = 1024 * 256;
251                         }
252                         break;
253
254         }
255
256         if (ambiqmicro_info->target_class < ARRAY_SIZE(ambiqmicroParts))
257                 ambiqmicro_info->target_name =
258                         ambiqmicroParts[ambiqmicro_info->target_class].partname;
259         else
260                 ambiqmicro_info->target_name =
261                         ambiqmicroParts[0].partname;
262
263         LOG_DEBUG("num_pages: %d, pagesize: %d, flash: %d, sram: %d",
264                 ambiqmicro_info->num_pages,
265                 ambiqmicro_info->pagesize,
266                 ambiqmicro_info->flshsiz,
267                 ambiqmicro_info->sramsiz);
268
269         return ERROR_OK;
270 }
271
272 /***************************************************************************
273 *       flash operations                                                       *
274 ***************************************************************************/
275
276 static int ambiqmicro_protect_check(struct flash_bank *bank)
277 {
278         struct ambiqmicro_flash_bank *ambiqmicro = bank->driver_priv;
279         int status = ERROR_OK;
280         uint32_t i;
281
282
283         if (!ambiqmicro->probed) {
284                 LOG_ERROR("Target not probed");
285                 return ERROR_FLASH_BANK_NOT_PROBED;
286         }
287
288         for (i = 0; i < (unsigned) bank->num_sectors; i++)
289                 bank->sectors[i].is_protected = -1;
290
291         return status;
292 }
293 /** Read flash status from bootloader. */
294 static int check_flash_status(struct target *target, uint32_t address)
295 {
296         uint32_t retflash;
297         int rc;
298         rc = target_read_u32(target, address, &retflash);
299         /* target connection failed. */
300         if (rc != ERROR_OK) {
301                 LOG_DEBUG("%s:%d:%s(): status(0x%x)\n",
302                         __FILE__, __LINE__, __func__, rc);
303                 return rc;
304         }
305         /* target flash failed, unknown cause. */
306         if (retflash != 0) {
307                 LOG_ERROR("Flash not happy: status(0x%x)", retflash);
308                 return ERROR_FLASH_OPERATION_FAILED;
309         }
310         return ERROR_OK;
311 }
312
313 static int ambiqmicro_exec_command(struct target *target,
314         uint32_t command,
315         uint32_t flash_return_address)
316 {
317         int retval, retflash;
318
319         retval = target_resume(
320                 target,
321                 false,
322                 command,
323                 true,
324                 true);
325
326         CHECK_STATUS(retval, "error executing ambiqmicro command");
327
328         /*
329          * Wait for halt.
330          */
331         for (;; ) {
332                 target_poll(target);
333                 if (target->state == TARGET_HALTED)
334                         break;
335                 else if (target->state == TARGET_RUNNING ||
336                         target->state == TARGET_DEBUG_RUNNING) {
337                         /*
338                          * Keep polling until target halts.
339                          */
340                         target_poll(target);
341                         alive_sleep(100);
342                         LOG_DEBUG("state = %d", target->state);
343                 } else {
344                         LOG_ERROR("Target not halted or running %d", target->state);
345                         break;
346                 }
347         }
348
349         /*
350          * Read return value, flash error takes precedence.
351          */
352         retflash = check_flash_status(target, flash_return_address);
353         if (retflash != ERROR_OK)
354                 retval = retflash;
355
356         /* Return code from target_resume OR flash. */
357         return retval;
358 }
359
360 static int ambiqmicro_mass_erase(struct flash_bank *bank)
361 {
362         struct target *target = NULL;
363         struct ambiqmicro_flash_bank *ambiqmicro_info = NULL;
364         int retval = ERROR_OK;
365
366         ambiqmicro_info = bank->driver_priv;
367         target = bank->target;
368
369         if (target->state != TARGET_HALTED) {
370                 LOG_ERROR("Target not halted");
371                 return ERROR_TARGET_NOT_HALTED;
372         }
373
374         if (!ambiqmicro_info->probed) {
375                 LOG_ERROR("Target not probed");
376                 return ERROR_FLASH_BANK_NOT_PROBED;
377         }
378
379         /*
380          * Clear Bootloader bit.
381          */
382         retval = target_write_u32(target, 0x400201a0, 0x0);
383         CHECK_STATUS(retval, "error clearing bootloader bit.");
384
385         /*
386          * Set up the SRAM.
387          */
388
389         /*
390          * Bank.
391          */
392         retval = target_write_u32(target, 0x10000000, bank->bank_number);
393         CHECK_STATUS(retval, "error writing target SRAM parameters.");
394
395         /*
396          * Write Key.
397          */
398         retval = target_write_u32(target, 0x10000004, PROGRAM_KEY);
399         CHECK_STATUS(retval, "error writing target SRAM parameters.");
400
401         /*
402          * Breakpoint.
403          */
404         retval = target_write_u32(target, 0x10000008, 0xfffffffe);
405         CHECK_STATUS(retval, "error writing target SRAM parameters.");
406
407         /*
408          * Erase the main array.
409          */
410         LOG_INFO("Mass erase on bank %d.", bank->bank_number);
411
412         /*
413          * passed pc, addr = ROM function, handle breakpoints, not debugging.
414          */
415         retval = ambiqmicro_exec_command(target, FLASH_MASS_ERASE_MAIN_PAGES_FROM_SRAM, 0x10000008);
416         CHECK_STATUS(retval, "error executing ambiqmicro flash mass erase.");
417         if (retval != ERROR_OK)
418                 return retval;
419
420         /*
421          * Set Bootloader bit, regardless of command execution.
422          */
423         retval = target_write_u32(target, 0x400201a0, 0x1);
424         CHECK_STATUS(retval, "error setting bootloader bit.");
425
426         return retval;
427 }
428
429
430 static int ambiqmicro_erase(struct flash_bank *bank, unsigned int first,
431                 unsigned int last)
432 {
433         struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
434         struct target *target = bank->target;
435         uint32_t retval = ERROR_OK;
436
437         if (bank->target->state != TARGET_HALTED) {
438                 LOG_ERROR("Target not halted");
439                 return ERROR_TARGET_NOT_HALTED;
440         }
441
442         if (!ambiqmicro_info->probed) {
443                 LOG_ERROR("Target not probed");
444                 return ERROR_FLASH_BANK_NOT_PROBED;
445         }
446
447         /*
448          * Check pages.
449          * Fix num_pages for the device.
450          */
451         if ((last < first) || (last >= ambiqmicro_info->num_pages))
452                 return ERROR_FLASH_SECTOR_INVALID;
453
454         /*
455          * Just Mass Erase if all pages are given.
456          * TODO: Fix num_pages for the device
457          */
458         if ((first == 0) && (last == (ambiqmicro_info->num_pages - 1)))
459                 return ambiqmicro_mass_erase(bank);
460
461         /*
462          * Clear Bootloader bit.
463          */
464         retval = target_write_u32(target, 0x400201a0, 0x0);
465         CHECK_STATUS(retval, "error clearing bootloader bit.");
466
467         /*
468          * Set up the SRAM.
469          */
470
471         /*
472          * Bank.
473          */
474         retval = target_write_u32(target, 0x10000000, bank->bank_number);
475         CHECK_STATUS(retval, "error writing target SRAM parameters.");
476
477         /*
478          * Number of pages to erase.
479          */
480         retval = target_write_u32(target, 0x10000004, 1 + (last-first));
481         CHECK_STATUS(retval, "error writing target SRAM parameters.");
482
483         /*
484          * Write Key.
485          */
486         retval = target_write_u32(target, 0x10000008, PROGRAM_KEY);
487         CHECK_STATUS(retval, "error writing target SRAM parameters.");
488
489         /*
490          * Breakpoint.
491          */
492         retval = target_write_u32(target, 0x1000000c, 0xfffffffe);
493         CHECK_STATUS(retval, "error writing target SRAM parameters.");
494
495         /*
496          * Pointer to flash address.
497          */
498         retval = target_write_u32(target, 0x10000010, first);
499         CHECK_STATUS(retval, "error writing target SRAM parameters.");
500         if (retval != ERROR_OK)
501                 return retval;
502
503         /*
504          * Erase the pages.
505          */
506         LOG_INFO("Erasing pages %u to %u on bank %u", first, last, bank->bank_number);
507
508         /*
509          * passed pc, addr = ROM function, handle breakpoints, not debugging.
510          */
511         retval = ambiqmicro_exec_command(target, FLASH_ERASE_LIST_MAIN_PAGES_FROM_SRAM, 0x1000000C);
512         CHECK_STATUS(retval, "error executing flash page erase");
513         if (retval != ERROR_OK)
514                 return retval;
515
516         LOG_INFO("%u pages erased!", 1+(last-first));
517
518         if (first == 0) {
519                 /*
520                  * Set Bootloader bit.
521                  */
522                 retval = target_write_u32(target, 0x400201a0, 0x1);
523                 CHECK_STATUS(retval, "error setting bootloader bit.");
524                 if (retval != ERROR_OK)
525                         return retval;
526         }
527
528         return retval;
529 }
530
531 static int ambiqmicro_protect(struct flash_bank *bank, int set,
532                 unsigned int first, unsigned int last)
533 {
534         /* struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
535          * struct target *target = bank->target; */
536
537         /*
538          * TODO
539          */
540         LOG_INFO("Not yet implemented");
541
542         if (bank->target->state != TARGET_HALTED) {
543                 LOG_ERROR("Target not halted");
544                 return ERROR_TARGET_NOT_HALTED;
545         }
546
547         return ERROR_OK;
548 }
549
550 static int ambiqmicro_write_block(struct flash_bank *bank,
551         const uint8_t *buffer, uint32_t offset, uint32_t count)
552 {
553         /* struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv; */
554         struct target *target = bank->target;
555         uint32_t address = bank->base + offset;
556         uint32_t buffer_pointer = 0x10000010;
557         uint32_t maxbuffer;
558         uint32_t thisrun_count;
559         int retval = ERROR_OK;
560
561         if (((count%4) != 0) || ((offset%4) != 0)) {
562                 LOG_ERROR("write block must be multiple of 4 bytes in offset & length");
563                 return ERROR_FAIL;
564         }
565
566         /*
567          * Max buffer size for this device.
568          * Hard code 6kB for the buffer.
569          */
570         maxbuffer = 0x1800;
571
572         LOG_INFO("Flashing main array");
573
574         while (count > 0) {
575                 if (count > maxbuffer)
576                         thisrun_count = maxbuffer;
577                 else
578                         thisrun_count = count;
579
580                 /*
581                  * Set up the SRAM.
582                  */
583
584                 /*
585                  * Pointer to flash.
586                  */
587                 retval = target_write_u32(target, 0x10000000, address);
588                 CHECK_STATUS(retval, "error writing target SRAM parameters.");
589
590                 /*
591                  * Number of 32-bit words to program.
592                  */
593                 retval = target_write_u32(target, 0x10000004, thisrun_count/4);
594                 CHECK_STATUS(retval, "error writing target SRAM parameters.");
595
596                 /*
597                  * Write Key.
598                  */
599                 retval = target_write_u32(target, 0x10000008, PROGRAM_KEY);
600                 CHECK_STATUS(retval, "error writing target SRAM parameters.");
601
602                 /*
603                  * Breakpoint.
604                  */
605                 retval = target_write_u32(target, 0x1000000c, 0xfffffffe);
606                 CHECK_STATUS(retval, "error writing target SRAM parameters.");
607
608                 /*
609                  * Write Buffer.
610                  */
611                 retval = target_write_buffer(target, buffer_pointer, thisrun_count, buffer);
612
613                 if (retval != ERROR_OK) {
614                         CHECK_STATUS(retval, "error writing target SRAM parameters.");
615                         break;
616                 }
617
618                 LOG_DEBUG("address = 0x%08x", address);
619
620                 retval = ambiqmicro_exec_command(target, FLASH_PROGRAM_MAIN_FROM_SRAM, 0x1000000c);
621                 CHECK_STATUS(retval, "error executing ambiqmicro flash write algorithm");
622                 if (retval != ERROR_OK)
623                         break;
624                 buffer += thisrun_count;
625                 address += thisrun_count;
626                 count -= thisrun_count;
627         }
628
629
630         LOG_INFO("Main array flashed");
631
632         /*
633          * Clear Bootloader bit.
634          */
635         retval = target_write_u32(target, 0x400201a0, 0x0);
636         CHECK_STATUS(retval, "error clearing bootloader bit");
637
638         return retval;
639 }
640
641 static int ambiqmicro_write(struct flash_bank *bank, const uint8_t *buffer,
642         uint32_t offset, uint32_t count)
643 {
644         uint32_t retval;
645
646         /* try using a block write */
647         retval = ambiqmicro_write_block(bank, buffer, offset, count);
648         if (retval != ERROR_OK)
649                 LOG_ERROR("write failed");
650
651         return retval;
652 }
653
654 static int ambiqmicro_probe(struct flash_bank *bank)
655 {
656         struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
657         uint32_t retval;
658
659         /* If this is a ambiqmicro chip, it has flash; probe() is just
660          * to figure out how much is present.  Only do it once.
661          */
662         if (ambiqmicro_info->probed) {
663                 LOG_INFO("Target already probed");
664                 return ERROR_OK;
665         }
666
667         /* ambiqmicro_read_part_info() already handled error checking and
668          * reporting.  Note that it doesn't write, so we don't care about
669          * whether the target is halted or not.
670          */
671         retval = ambiqmicro_read_part_info(bank);
672         if (retval != ERROR_OK)
673                 return retval;
674
675         free(bank->sectors);
676
677         /* provide this for the benefit of the NOR flash framework */
678         bank->size = ambiqmicro_info->pagesize * ambiqmicro_info->num_pages;
679         bank->num_sectors = ambiqmicro_info->num_pages;
680         bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
681         for (unsigned int i = 0; i < bank->num_sectors; i++) {
682                 bank->sectors[i].offset = i * ambiqmicro_info->pagesize;
683                 bank->sectors[i].size = ambiqmicro_info->pagesize;
684                 bank->sectors[i].is_erased = -1;
685                 bank->sectors[i].is_protected = -1;
686         }
687
688         /*
689          * Part has been probed.
690          */
691         ambiqmicro_info->probed = true;
692
693         return retval;
694 }
695
696 static int ambiqmicro_otp_program(struct flash_bank *bank,
697         uint32_t offset, uint32_t count)
698 {
699         struct target *target = NULL;
700         struct ambiqmicro_flash_bank *ambiqmicro_info = NULL;
701         uint32_t retval = ERROR_OK;
702
703         ambiqmicro_info = bank->driver_priv;
704         target = bank->target;
705
706         if (target->state != TARGET_HALTED) {
707                 LOG_ERROR("Target not halted");
708                 return ERROR_TARGET_NOT_HALTED;
709         }
710
711         if (!ambiqmicro_info->probed) {
712                 LOG_ERROR("Target not probed");
713                 return ERROR_FLASH_BANK_NOT_PROBED;
714         }
715
716         if (count > 256) {
717                 LOG_ERROR("Count must be < 256");
718                 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
719         }
720
721         /*
722          * Clear Bootloader bit.
723          */
724         retval = target_write_u32(target, 0x400201a0, 0x0);
725         CHECK_STATUS(retval, "error clearing bootloader bit.");
726
727         /*
728          * Set up the SRAM.
729          */
730
731         /*
732          * Bank.
733          */
734         retval = target_write_u32(target, 0x10000000, offset);
735         CHECK_STATUS(retval, "error setting target SRAM parameters.");
736
737         /*
738          * Num of words to program.
739          */
740         retval = target_write_u32(target, 0x10000004, count);
741         CHECK_STATUS(retval, "error setting target SRAM parameters.");
742
743         /*
744          * Write Key.
745          */
746         retval = target_write_u32(target, 0x10000008, OTP_PROGRAM_KEY);
747         CHECK_STATUS(retval, "error setting target SRAM parameters.");
748
749         /*
750          * Breakpoint.
751          */
752         retval = target_write_u32(target, 0x1000000c, 0xfffffffe);
753         CHECK_STATUS(retval, "error setting target SRAM parameters.");
754         if (retval != ERROR_OK)
755                 return retval;
756
757         /*
758          * Program OTP.
759          */
760         LOG_INFO("Programming OTP offset 0x%08x", offset);
761
762         /*
763          * passed pc, addr = ROM function, handle breakpoints, not debugging.
764          */
765         retval = ambiqmicro_exec_command(target, FLASH_PROGRAM_OTP_FROM_SRAM, 0x1000000C);
766         CHECK_STATUS(retval, "error executing ambiqmicro otp program algorithm");
767
768         LOG_INFO("Programming OTP finished.");
769
770         return retval;
771 }
772
773
774
775 COMMAND_HANDLER(ambiqmicro_handle_mass_erase_command)
776 {
777         if (CMD_ARGC < 1)
778                 return ERROR_COMMAND_SYNTAX_ERROR;
779
780         struct flash_bank *bank;
781         uint32_t retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
782         if (ERROR_OK != retval)
783                 return retval;
784
785         if (ambiqmicro_mass_erase(bank) == ERROR_OK) {
786                 /* set all sectors as erased */
787                 for (unsigned int i = 0; i < bank->num_sectors; i++)
788                         bank->sectors[i].is_erased = 1;
789
790                 command_print(CMD, "ambiqmicro mass erase complete");
791         } else
792                 command_print(CMD, "ambiqmicro mass erase failed");
793
794         return ERROR_OK;
795 }
796
797 COMMAND_HANDLER(ambiqmicro_handle_page_erase_command)
798 {
799         struct flash_bank *bank;
800         uint32_t first, last;
801         uint32_t retval;
802
803         if (CMD_ARGC < 3)
804                 return ERROR_COMMAND_SYNTAX_ERROR;
805
806         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
807         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
808
809         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
810         if (ERROR_OK != retval)
811                 return retval;
812
813         if (ambiqmicro_erase(bank, first, last) == ERROR_OK)
814                 command_print(CMD, "ambiqmicro page erase complete");
815         else
816                 command_print(CMD, "ambiqmicro page erase failed");
817
818         return ERROR_OK;
819 }
820
821
822 /**
823  * Program the otp block.
824  */
825 COMMAND_HANDLER(ambiqmicro_handle_program_otp_command)
826 {
827         struct flash_bank *bank;
828         uint32_t offset, count;
829         uint32_t retval;
830
831         if (CMD_ARGC < 3)
832                 return ERROR_COMMAND_SYNTAX_ERROR;
833
834         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], offset);
835         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
836
837         command_print(CMD, "offset=0x%08x count=%d", offset, count);
838
839         CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
840
841         retval = ambiqmicro_otp_program(bank, offset, count);
842
843         if (retval != ERROR_OK)
844                 LOG_ERROR("error check log");
845
846         return ERROR_OK;
847 }
848
849
850
851 static const struct command_registration ambiqmicro_exec_command_handlers[] = {
852         {
853                 .name = "mass_erase",
854                 .usage = "<bank>",
855                 .handler = ambiqmicro_handle_mass_erase_command,
856                 .mode = COMMAND_EXEC,
857                 .help = "Erase entire device",
858         },
859         {
860                 .name = "page_erase",
861                 .usage = "<bank> <first> <last>",
862                 .handler = ambiqmicro_handle_page_erase_command,
863                 .mode = COMMAND_EXEC,
864                 .help = "Erase device pages",
865         },
866         {
867                 .name = "program_otp",
868                 .handler = ambiqmicro_handle_program_otp_command,
869                 .mode = COMMAND_EXEC,
870                 .usage = "<bank> <offset> <count>",
871                 .help =
872                         "Program OTP (assumes you have already written array starting at 0x10000010)",
873         },
874         COMMAND_REGISTRATION_DONE
875 };
876 static const struct command_registration ambiqmicro_command_handlers[] = {
877         {
878                 .name = "ambiqmicro",
879                 .mode = COMMAND_EXEC,
880                 .help = "ambiqmicro flash command group",
881                 .usage = "Support for Ambiq Micro parts.",
882                 .chain = ambiqmicro_exec_command_handlers,
883         },
884         COMMAND_REGISTRATION_DONE
885 };
886
887 const struct flash_driver ambiqmicro_flash = {
888         .name = "ambiqmicro",
889         .commands = ambiqmicro_command_handlers,
890         .flash_bank_command = ambiqmicro_flash_bank_command,
891         .erase = ambiqmicro_erase,
892         .protect = ambiqmicro_protect,
893         .write = ambiqmicro_write,
894         .read = default_flash_read,
895         .probe = ambiqmicro_probe,
896         .auto_probe = ambiqmicro_probe,
897         .erase_check = default_flash_blank_check,
898         .protect_check = ambiqmicro_protect_check,
899         .info = get_ambiqmicro_info,
900         .free_driver_priv = default_flash_free_driver_priv,
901 };