931c4ff66787ed87fb405da5d6f09f00bbaa8676
[fw/openocd] / src / flash / nor / msp432.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2018 by Texas Instruments, Inc.                         *
5  ***************************************************************************/
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include "imp.h"
12 #include "msp432.h"
13 #include <helper/binarybuffer.h>
14 #include <helper/time_support.h>
15 #include <target/algorithm.h>
16 #include <target/armv7m.h>
17 #include <target/image.h>
18
19 /* MSP432P4 hardware registers */
20 #define P4_FLASH_MAIN_SIZE_REG 0xE0043020
21 #define P4_FLASH_INFO_SIZE_REG 0xE0043024
22 #define P4_DEVICE_ID_REG       0x0020100C
23 #define P4_HARDWARE_REV_REG    0x00201010
24
25 /* MSP432E4 hardware registers */
26 #define E4_DID0_REG 0x400FE000
27 #define E4_DID1_REG 0x400FE004
28
29 #define FLASH_TIMEOUT 8000
30
31 #define SUPPORT_MESSAGE \
32         "Your pre-production MSP432P401x silicon is not fully supported\n" \
33         "You can find more information at www.ti.com/product/MSP432P401R"
34
35 struct msp432_bank {
36         uint32_t device_id;
37         uint32_t hardware_rev;
38         int family_type;
39         int device_type;
40         uint32_t sector_length;
41         bool probed_main;
42         bool probed_info;
43         bool unlock_bsl;
44         struct working_area *working_area;
45         struct armv7m_algorithm armv7m_info;
46 };
47
48 static int msp432_auto_probe(struct flash_bank *bank);
49
50 static int msp432_device_type(uint32_t family_type, uint32_t device_id,
51         uint32_t hardware_rev)
52 {
53         int device_type = MSP432_NO_TYPE;
54
55         if (family_type == MSP432E4) {
56                 /* MSP432E4 device family */
57
58                 if (device_id == 0x180C0002) {
59                         if (hardware_rev == 0x102DC06E) {
60                                 /* The 01Y variant */
61                                 device_type = MSP432E401Y;
62                         } else if (hardware_rev == 0x1032E076) {
63                                 /* The 11Y variant */
64                                 device_type = MSP432E411Y;
65                         } else {
66                                 /* Reasonable guess that this is a new variant */
67                                 device_type = MSP432E4X_GUESS;
68                         }
69                 } else {
70                         /* Wild guess that this is an MSP432E4 */
71                         device_type = MSP432E4X_GUESS;
72                 }
73         } else {
74                 /* MSP432P4 device family */
75
76                 /* Examine the device ID and hardware revision to get the device type */
77                 switch (device_id) {
78                         case 0xA000:
79                         case 0xA001:
80                         case 0xA002:
81                         case 0xA003:
82                         case 0xA004:
83                         case 0xA005:
84                                 /* Device is definitely MSP432P401x, check hardware revision */
85                                 if (hardware_rev == 0x41 || hardware_rev == 0x42) {
86                                         /* Rev A or B of the silicon has been deprecated */
87                                         device_type = MSP432P401X_DEPR;
88                                 } else if (hardware_rev >= 0x43 && hardware_rev <= 0x49) {
89                                         /* Current and future revisions of the MSP432P401x device */
90                                         device_type = MSP432P401X;
91                                 } else {
92                                         /* Unknown or unanticipated hardware revision */
93                                         device_type = MSP432P401X_GUESS;
94                                 }
95                                 break;
96                         case 0xA010:
97                         case 0xA012:
98                         case 0xA016:
99                         case 0xA019:
100                         case 0xA01F:
101                         case 0xA020:
102                         case 0xA022:
103                         case 0xA026:
104                         case 0xA029:
105                         case 0xA02F:
106                                 /* Device is definitely MSP432P411x, check hardware revision */
107                                 if (hardware_rev >= 0x41 && hardware_rev <= 0x49) {
108                                         /* Current and future revisions of the MSP432P411x device */
109                                         device_type = MSP432P411X;
110                                 } else {
111                                         /* Unknown or unanticipated hardware revision */
112                                         device_type = MSP432P411X_GUESS;
113                                 }
114                                 break;
115                         case 0xFFFF:
116                                 /* Device is very early silicon that has been deprecated */
117                                 device_type = MSP432P401X_DEPR;
118                                 break;
119                         default:
120                                 if (device_id < 0xA010) {
121                                         /* Wild guess that this is an MSP432P401x */
122                                         device_type = MSP432P401X_GUESS;
123                                 } else {
124                                         /* Reasonable guess that this is a new variant */
125                                         device_type = MSP432P411X_GUESS;
126                                 }
127                                 break;
128                 }
129         }
130
131         return device_type;
132 }
133
134 static const char *msp432_return_text(uint32_t return_code)
135 {
136         switch (return_code) {
137                 case FLASH_BUSY:
138                         return "FLASH_BUSY";
139                 case FLASH_SUCCESS:
140                         return "FLASH_SUCCESS";
141                 case FLASH_ERROR:
142                         return "FLASH_ERROR";
143                 case FLASH_TIMEOUT_ERROR:
144                         return "FLASH_TIMEOUT_ERROR";
145                 case FLASH_VERIFY_ERROR:
146                         return "FLASH_VERIFY_WRONG";
147                 case FLASH_WRONG_COMMAND:
148                         return "FLASH_WRONG_COMMAND";
149                 case FLASH_POWER_ERROR:
150                         return "FLASH_POWER_ERROR";
151                 default:
152                         return "UNDEFINED_RETURN_CODE";
153         }
154 }
155
156 static void msp432_init_params(struct msp432_algo_params *algo_params)
157 {
158         buf_set_u32(algo_params->flash_command, 0, 32, FLASH_NO_COMMAND);
159         buf_set_u32(algo_params->return_code, 0, 32, 0);
160         buf_set_u32(algo_params->_reserved0, 0, 32, 0);
161         buf_set_u32(algo_params->address, 0, 32, 0);
162         buf_set_u32(algo_params->length, 0, 32, 0);
163         buf_set_u32(algo_params->buffer1_status, 0, 32, BUFFER_INACTIVE);
164         buf_set_u32(algo_params->buffer2_status, 0, 32, BUFFER_INACTIVE);
165         buf_set_u32(algo_params->erase_param, 0, 32, FLASH_ERASE_MAIN);
166         buf_set_u32(algo_params->unlock_bsl, 0, 32, FLASH_LOCK_BSL);
167 }
168
169 static int msp432_exec_cmd(struct target *target, struct msp432_algo_params
170                         *algo_params, uint32_t command)
171 {
172         int retval;
173
174         /* Make sure the given params do not include the command */
175         buf_set_u32(algo_params->flash_command, 0, 32, FLASH_NO_COMMAND);
176         buf_set_u32(algo_params->return_code, 0, 32, 0);
177         buf_set_u32(algo_params->buffer1_status, 0, 32, BUFFER_INACTIVE);
178         buf_set_u32(algo_params->buffer2_status, 0, 32, BUFFER_INACTIVE);
179
180         /* Write out parameters to target memory */
181         retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR,
182                                 sizeof(struct msp432_algo_params), (uint8_t *)algo_params);
183         if (retval != ERROR_OK)
184                 return retval;
185
186         /* Write out command to target memory */
187         retval = target_write_u32(target, ALGO_FLASH_COMMAND_ADDR, command);
188
189         return retval;
190 }
191
192 static int msp432_wait_return_code(struct target *target)
193 {
194         uint32_t return_code = 0;
195         long long start_ms;
196         long long elapsed_ms;
197
198         int retval = ERROR_OK;
199
200         start_ms = timeval_ms();
201         while ((return_code == 0) || (return_code == FLASH_BUSY)) {
202                 retval = target_read_u32(target, ALGO_RETURN_CODE_ADDR, &return_code);
203                 if (retval != ERROR_OK)
204                         return retval;
205
206                 elapsed_ms = timeval_ms() - start_ms;
207                 if (elapsed_ms > 500)
208                         keep_alive();
209                 if (elapsed_ms > FLASH_TIMEOUT)
210                         break;
211         };
212
213         if (return_code != FLASH_SUCCESS) {
214                 LOG_ERROR("msp432: Flash operation failed: %s",
215                         msp432_return_text(return_code));
216                 return ERROR_FAIL;
217         }
218
219         return ERROR_OK;
220 }
221
222 static int msp432_wait_inactive(struct target *target, uint32_t buffer)
223 {
224         uint32_t status_code = BUFFER_ACTIVE;
225         uint32_t status_addr;
226         long long start_ms;
227         long long elapsed_ms;
228
229         int retval;
230
231         switch (buffer) {
232                 case 1: /* Buffer 1 */
233                         status_addr = ALGO_BUFFER1_STATUS_ADDR;
234                         break;
235                 case 2: /* Buffer 2 */
236                         status_addr = ALGO_BUFFER2_STATUS_ADDR;
237                         break;
238                 default:
239                         return ERROR_FAIL;
240         }
241
242         start_ms = timeval_ms();
243         while (status_code != BUFFER_INACTIVE) {
244                 retval = target_read_u32(target, status_addr, &status_code);
245                 if (retval != ERROR_OK)
246                         return retval;
247
248                 elapsed_ms = timeval_ms() - start_ms;
249                 if (elapsed_ms > 500)
250                         keep_alive();
251                 if (elapsed_ms > FLASH_TIMEOUT)
252                         break;
253         };
254
255         if (status_code != BUFFER_INACTIVE) {
256                 LOG_ERROR(
257                         "msp432: Flash operation failed: buffer not written to flash");
258                 return ERROR_FAIL;
259         }
260
261         return ERROR_OK;
262 }
263
264 static int msp432_init(struct flash_bank *bank)
265 {
266         struct target *target = bank->target;
267         struct msp432_bank *msp432_bank = bank->driver_priv;
268         struct msp432_algo_params algo_params;
269         struct reg_param reg_params[1];
270
271         const uint8_t *loader_code;
272         uint32_t loader_size;
273         uint32_t algo_entry_addr;
274         int retval;
275
276         /* Make sure we've probed the flash to get the device and size */
277         retval = msp432_auto_probe(bank);
278         if (retval != ERROR_OK)
279                 return retval;
280
281         /* Choose appropriate flash helper algorithm */
282         switch (msp432_bank->device_type) {
283                 case MSP432P401X:
284                 case MSP432P401X_DEPR:
285                 case MSP432P401X_GUESS:
286                 default:
287                         loader_code = msp432p401x_algo;
288                         loader_size = sizeof(msp432p401x_algo);
289                         algo_entry_addr = P4_ALGO_ENTRY_ADDR;
290                         break;
291                 case MSP432P411X:
292                 case MSP432P411X_GUESS:
293                         loader_code = msp432p411x_algo;
294                         loader_size = sizeof(msp432p411x_algo);
295                         algo_entry_addr = P4_ALGO_ENTRY_ADDR;
296                         break;
297                 case MSP432E401Y:
298                 case MSP432E411Y:
299                 case MSP432E4X_GUESS:
300                         loader_code = msp432e4x_algo;
301                         loader_size = sizeof(msp432e4x_algo);
302                         algo_entry_addr = E4_ALGO_ENTRY_ADDR;
303                         break;
304         }
305
306         /* Issue warnings if this is a device we may not be able to flash */
307         if (msp432_bank->device_type == MSP432P401X_GUESS ||
308                 msp432_bank->device_type == MSP432P411X_GUESS) {
309                 /* Explicit device type check failed. Report this. */
310                 LOG_WARNING(
311                         "msp432: Unrecognized MSP432P4 Device ID and Hardware "
312                         "Rev (%04" PRIX32 ", %02" PRIX32 ")", msp432_bank->device_id,
313                         msp432_bank->hardware_rev);
314         } else if (msp432_bank->device_type == MSP432P401X_DEPR) {
315                 LOG_WARNING(
316                         "msp432: MSP432P401x pre-production device (deprecated "
317                         "silicon)\n" SUPPORT_MESSAGE);
318         } else if (msp432_bank->device_type == MSP432E4X_GUESS) {
319                 /* Explicit device type check failed. Report this. */
320                 LOG_WARNING(
321                         "msp432: Unrecognized MSP432E4 DID0 and DID1 values "
322                         "(%08" PRIX32 ", %08" PRIX32 ")", msp432_bank->device_id,
323                         msp432_bank->hardware_rev);
324         }
325
326         /* Check for working area to use for flash helper algorithm */
327         target_free_working_area(target, msp432_bank->working_area);
328         msp432_bank->working_area = NULL;
329
330         retval = target_alloc_working_area(target, ALGO_WORKING_SIZE,
331                                 &msp432_bank->working_area);
332         if (retval != ERROR_OK)
333                 return retval;
334
335         /* Confirm the defined working address is the area we need to use */
336         if (msp432_bank->working_area->address != ALGO_BASE_ADDR)
337                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
338
339         /* Write flash helper algorithm into target memory */
340         retval = target_write_buffer(target, ALGO_BASE_ADDR, loader_size,
341                                 loader_code);
342         if (retval != ERROR_OK)
343                 return retval;
344
345         /* Initialize the ARMv7 specific info to run the algorithm */
346         msp432_bank->armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
347         msp432_bank->armv7m_info.core_mode = ARM_MODE_THREAD;
348
349         /* Initialize algorithm parameters to default values */
350         msp432_init_params(&algo_params);
351
352         /* Write out parameters to target memory */
353         retval = target_write_buffer(target, ALGO_PARAMS_BASE_ADDR,
354                                 sizeof(algo_params), (uint8_t *)&algo_params);
355         if (retval != ERROR_OK)
356                 return retval;
357
358         /* Initialize stack pointer for flash helper algorithm */
359         init_reg_param(&reg_params[0], "sp", 32, PARAM_OUT);
360         buf_set_u32(reg_params[0].value, 0, 32, ALGO_STACK_POINTER_ADDR);
361
362         /* Begin executing the flash helper algorithm */
363         retval = target_start_algorithm(target, 0, 0, 1, reg_params,
364                                 algo_entry_addr, 0, &msp432_bank->armv7m_info);
365         destroy_reg_param(&reg_params[0]);
366         if (retval != ERROR_OK) {
367                 LOG_ERROR("msp432: Failed to start flash helper algorithm");
368                 return retval;
369         }
370
371         /*
372          * At this point, the algorithm is running on the target and
373          * ready to receive commands and data to flash the target
374          */
375
376         /* Issue the init command to the flash helper algorithm */
377         retval = msp432_exec_cmd(target, &algo_params, FLASH_INIT);
378         if (retval != ERROR_OK)
379                 return retval;
380
381         retval = msp432_wait_return_code(target);
382
383         return retval;
384 }
385
386 static int msp432_quit(struct flash_bank *bank)
387 {
388         struct target *target = bank->target;
389         struct msp432_bank *msp432_bank = bank->driver_priv;
390         struct msp432_algo_params algo_params;
391
392         int retval;
393
394         /* Initialize algorithm parameters to default values */
395         msp432_init_params(&algo_params);
396
397         /* Issue the exit command to the flash helper algorithm */
398         retval = msp432_exec_cmd(target, &algo_params, FLASH_EXIT);
399         if (retval != ERROR_OK)
400                 return retval;
401
402         (void)msp432_wait_return_code(target);
403
404         /* Regardless of the return code, attempt to halt the target */
405         (void)target_halt(target);
406
407         /* Now confirm target halted and clean up from flash helper algorithm */
408         retval = target_wait_algorithm(target, 0, NULL, 0, NULL, 0, FLASH_TIMEOUT,
409                                 &msp432_bank->armv7m_info);
410
411         target_free_working_area(target, msp432_bank->working_area);
412         msp432_bank->working_area = NULL;
413
414         return retval;
415 }
416
417 static int msp432_mass_erase(struct flash_bank *bank, bool all)
418 {
419         struct target *target = bank->target;
420         struct msp432_bank *msp432_bank = bank->driver_priv;
421         struct msp432_algo_params algo_params;
422
423         int retval;
424
425         if (target->state != TARGET_HALTED) {
426                 LOG_ERROR("Target not halted");
427                 return ERROR_TARGET_NOT_HALTED;
428         }
429
430         retval = msp432_init(bank);
431         if (retval != ERROR_OK)
432                 return retval;
433
434         /* Initialize algorithm parameters to default values */
435         msp432_init_params(&algo_params);
436         if (all) {
437                 buf_set_u32(algo_params.erase_param, 0, 32,
438                         FLASH_ERASE_MAIN | FLASH_ERASE_INFO);
439                 if (msp432_bank->unlock_bsl)
440                         buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
441         }
442
443         /* Issue the mass erase command to the flash helper algorithm */
444         retval = msp432_exec_cmd(target, &algo_params, FLASH_MASS_ERASE);
445         if (retval != ERROR_OK) {
446                 (void)msp432_quit(bank);
447                 return retval;
448         }
449
450         retval = msp432_wait_return_code(target);
451         if (retval != ERROR_OK) {
452                 (void)msp432_quit(bank);
453                 return retval;
454         }
455
456         retval = msp432_quit(bank);
457         if (retval != ERROR_OK)
458                 return retval;
459
460         return retval;
461 }
462
463 COMMAND_HANDLER(msp432_mass_erase_command)
464 {
465         struct flash_bank *bank;
466         struct msp432_bank *msp432_bank;
467         bool all;
468
469         int retval;
470
471         if (1 > CMD_ARGC)
472                 return ERROR_COMMAND_SYNTAX_ERROR;
473
474         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
475         if (retval != ERROR_OK)
476                 return retval;
477
478         if (1 == CMD_ARGC) {
479                 all = false;
480         } else if (2 == CMD_ARGC) {
481                 /* Check argument for how much to erase */
482                 if (strcmp(CMD_ARGV[1], "main") == 0)
483                         all = false;
484                 else if (strcmp(CMD_ARGV[1], "all") == 0)
485                         all = true;
486                 else
487                         return ERROR_COMMAND_SYNTAX_ERROR;
488         } else {
489                 return ERROR_COMMAND_SYNTAX_ERROR;
490         }
491
492         msp432_bank = bank->driver_priv;
493
494         if (msp432_bank->family_type == MSP432E4) {
495                 /* MSP432E4 does not have main vs info regions, ignore "all" */
496                 all = false;
497         }
498
499         retval = msp432_mass_erase(bank, all);
500         if (retval != ERROR_OK)
501                 return retval;
502
503         if (msp432_bank->family_type == MSP432E4) {
504                 /* MSP432E4 does not have main vs info regions */
505                 LOG_INFO("msp432: Mass erase of flash is complete");
506         } else {
507                 LOG_INFO("msp432: Mass erase of %s is complete",
508                         all ? "main + information flash" : "main flash");
509         }
510
511         return ERROR_OK;
512 }
513
514 COMMAND_HANDLER(msp432_bsl_command)
515 {
516         struct flash_bank *bank;
517         struct msp432_bank *msp432_bank;
518
519         int retval;
520
521         if (1 > CMD_ARGC)
522                 return ERROR_COMMAND_SYNTAX_ERROR;
523
524         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
525         if (retval != ERROR_OK)
526                 return retval;
527
528         msp432_bank = bank->driver_priv;
529
530         if (msp432_bank->family_type == MSP432E4) {
531                 LOG_WARNING("msp432: MSP432E4 does not have a BSL region");
532                 return ERROR_OK;
533         }
534
535         if (2 == CMD_ARGC) {
536                 if (strcmp(CMD_ARGV[1], "lock") == 0)
537                         msp432_bank->unlock_bsl = false;
538                 else if (strcmp(CMD_ARGV[1], "unlock") == 0)
539                         msp432_bank->unlock_bsl = true;
540                 else
541                         return ERROR_COMMAND_SYNTAX_ERROR;
542         } else if (1 != CMD_ARGC) {
543                 /* Extra, unknown argument passed in */
544                 return ERROR_COMMAND_SYNTAX_ERROR;
545         }
546
547         LOG_INFO("msp432: BSL flash region is currently %slocked",
548                 msp432_bank->unlock_bsl ? "un" : "");
549
550         return ERROR_OK;
551 }
552
553 FLASH_BANK_COMMAND_HANDLER(msp432_flash_bank_command)
554 {
555         struct msp432_bank *msp432_bank;
556
557         if (CMD_ARGC < 6)
558                 return ERROR_COMMAND_SYNTAX_ERROR;
559
560         /* Create shared private struct for flash banks */
561         msp432_bank = malloc(sizeof(struct msp432_bank));
562         if (!msp432_bank)
563                 return ERROR_FAIL;
564
565         /* Initialize private flash information */
566         msp432_bank->device_id = 0;
567         msp432_bank->hardware_rev = 0;
568         msp432_bank->family_type = MSP432_NO_FAMILY;
569         msp432_bank->device_type = MSP432_NO_TYPE;
570         msp432_bank->sector_length = 0x1000;
571         msp432_bank->probed_main = false;
572         msp432_bank->probed_info = false;
573         msp432_bank->unlock_bsl = false;
574         msp432_bank->working_area = NULL;
575
576         /* Finish up initial settings here */
577         bank->driver_priv = msp432_bank;
578         bank->base = FLASH_BASE;
579
580         return ERROR_OK;
581 }
582
583 static int msp432_erase(struct flash_bank *bank, unsigned int first,
584                 unsigned int last)
585 {
586         struct target *target = bank->target;
587         struct msp432_bank *msp432_bank = bank->driver_priv;
588         struct msp432_algo_params algo_params;
589
590         bool is_main = bank->base == FLASH_BASE;
591         bool is_info = bank->base == P4_FLASH_INFO_BASE;
592
593         int retval;
594
595         if (target->state != TARGET_HALTED) {
596                 LOG_ERROR("Target not halted");
597                 return ERROR_TARGET_NOT_HALTED;
598         }
599
600         /* Do a mass erase if user requested all sectors of main flash */
601         if (is_main && (first == 0) && (last == (bank->num_sectors - 1))) {
602                 /* Request mass erase of main flash */
603                 return msp432_mass_erase(bank, false);
604         }
605
606         retval = msp432_init(bank);
607         if (retval != ERROR_OK)
608                 return retval;
609
610         /* Initialize algorithm parameters to default values */
611         msp432_init_params(&algo_params);
612
613         /* Adjust params if this is the info bank */
614         if (is_info) {
615                 buf_set_u32(algo_params.erase_param, 0, 32, FLASH_ERASE_INFO);
616                 /* And flag if BSL is unlocked */
617                 if (msp432_bank->unlock_bsl)
618                         buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
619         }
620
621         /* Erase requested sectors one by one */
622         for (unsigned int i = first; i <= last; i++) {
623
624                 /* Skip TVL (read-only) sector of the info bank */
625                 if (is_info && 1 == i)
626                         continue;
627
628                 /* Skip BSL sectors of info bank if locked */
629                 if (is_info && (2 == i || 3 == i) &&
630                         !msp432_bank->unlock_bsl)
631                         continue;
632
633                 /* Convert sector number to starting address of sector */
634                 buf_set_u32(algo_params.address, 0, 32, bank->base +
635                         (i * msp432_bank->sector_length));
636
637                 /* Issue the sector erase command to the flash helper algorithm */
638                 retval = msp432_exec_cmd(target, &algo_params, FLASH_SECTOR_ERASE);
639                 if (retval != ERROR_OK) {
640                         (void)msp432_quit(bank);
641                         return retval;
642                 }
643
644                 retval = msp432_wait_return_code(target);
645                 if (retval != ERROR_OK) {
646                         (void)msp432_quit(bank);
647                         return retval;
648                 }
649         }
650
651         retval = msp432_quit(bank);
652         if (retval != ERROR_OK)
653                 return retval;
654
655         return retval;
656 }
657
658 static int msp432_write(struct flash_bank *bank, const uint8_t *buffer,
659         uint32_t offset, uint32_t count)
660 {
661         struct target *target = bank->target;
662         struct msp432_bank *msp432_bank = bank->driver_priv;
663         struct msp432_algo_params algo_params;
664         uint32_t size;
665         uint32_t data_ready = BUFFER_DATA_READY;
666         long long start_ms;
667         long long elapsed_ms;
668
669         bool is_info = bank->base == P4_FLASH_INFO_BASE;
670
671         int retval;
672
673         if (target->state != TARGET_HALTED) {
674                 LOG_ERROR("Target not halted");
675                 return ERROR_TARGET_NOT_HALTED;
676         }
677
678         /*
679          * Block attempts to write to read-only sectors of flash
680          * The TVL region in sector 1 of the info flash is always read-only
681          * The BSL region in sectors 2 and 3 of the info flash may be unlocked
682          * The helper algorithm will hang on attempts to write to TVL
683          */
684         if (is_info) {
685                 /* Set read-only start to TVL sector */
686                 uint32_t start = 0x1000;
687                 /* Set read-only end after BSL region if locked */
688                 uint32_t end = (msp432_bank->unlock_bsl) ? 0x2000 : 0x4000;
689                 /* Check if request includes anything in read-only sectors */
690                 if ((offset + count - 1) < start || offset >= end) {
691                         /* The request includes no bytes in read-only sectors */
692                         /* Fall out and process the request normally */
693                 } else {
694                         /* Send a request for anything before read-only sectors */
695                         if (offset < start) {
696                                 uint32_t start_count = MIN(start - offset, count);
697                                 retval = msp432_write(bank, buffer, offset, start_count);
698                                 if (retval != ERROR_OK)
699                                         return retval;
700                         }
701                         /* Send a request for anything after read-only sectors */
702                         if ((offset + count - 1) >= end) {
703                                 uint32_t skip = end - offset;
704                                 count -= skip;
705                                 offset += skip;
706                                 buffer += skip;
707                                 return msp432_write(bank, buffer, offset, count);
708                         } else {
709                                 /* Request is entirely in read-only sectors */
710                                 return ERROR_OK;
711                         }
712                 }
713         }
714
715         retval = msp432_init(bank);
716         if (retval != ERROR_OK)
717                 return retval;
718
719         /* Initialize algorithm parameters to default values */
720         msp432_init_params(&algo_params);
721
722         /* Set up parameters for requested flash write operation */
723         buf_set_u32(algo_params.address, 0, 32, bank->base + offset);
724         buf_set_u32(algo_params.length, 0, 32, count);
725
726         /* Check if this is the info bank */
727         if (is_info) {
728                 /* And flag if BSL is unlocked */
729                 if (msp432_bank->unlock_bsl)
730                         buf_set_u32(algo_params.unlock_bsl, 0, 32, FLASH_UNLOCK_BSL);
731         }
732
733         /* Set up flash helper algorithm to continuous flash mode */
734         retval = msp432_exec_cmd(target, &algo_params, FLASH_CONTINUOUS);
735         if (retval != ERROR_OK) {
736                 (void)msp432_quit(bank);
737                 return retval;
738         }
739
740         /* Write requested data, one buffer at a time */
741         start_ms = timeval_ms();
742         while (count > 0) {
743
744                 if (count > ALGO_BUFFER_SIZE)
745                         size = ALGO_BUFFER_SIZE;
746                 else
747                         size = count;
748
749                 /* Put next block of data to flash into buffer */
750                 retval = target_write_buffer(target, ALGO_BUFFER1_ADDR, size, buffer);
751                 if (retval != ERROR_OK) {
752                         LOG_ERROR("Unable to write data to target memory");
753                         (void)msp432_quit(bank);
754                         return ERROR_FLASH_OPERATION_FAILED;
755                 }
756
757                 /* Signal the flash helper algorithm that data is ready to flash */
758                 retval = target_write_u32(target, ALGO_BUFFER1_STATUS_ADDR,
759                                         data_ready);
760                 if (retval != ERROR_OK) {
761                         (void)msp432_quit(bank);
762                         return ERROR_FLASH_OPERATION_FAILED;
763                 }
764
765                 retval = msp432_wait_inactive(target, 1);
766                 if (retval != ERROR_OK) {
767                         (void)msp432_quit(bank);
768                         return retval;
769                 }
770
771                 count -= size;
772                 buffer += size;
773
774                 elapsed_ms = timeval_ms() - start_ms;
775                 if (elapsed_ms > 500)
776                         keep_alive();
777         }
778
779         /* Confirm that the flash helper algorithm is finished */
780         retval = msp432_wait_return_code(target);
781         if (retval != ERROR_OK) {
782                 (void)msp432_quit(bank);
783                 return retval;
784         }
785
786         retval = msp432_quit(bank);
787         if (retval != ERROR_OK)
788                 return retval;
789
790         return retval;
791 }
792
793 static int msp432_probe(struct flash_bank *bank)
794 {
795         struct target *target = bank->target;
796         struct msp432_bank *msp432_bank = bank->driver_priv;
797
798         uint32_t device_id;
799         uint32_t hardware_rev;
800
801         uint32_t sector_length;
802         uint32_t size;
803         unsigned int num_sectors;
804
805         bool is_main = bank->base == FLASH_BASE;
806         bool is_info = bank->base == P4_FLASH_INFO_BASE;
807
808         int retval;
809
810         /* Check if this bank has already been successfully probed */
811         if (is_main && msp432_bank->probed_main)
812                 return ERROR_OK;
813         if (is_info && msp432_bank->probed_info)
814                 return ERROR_OK;
815
816         /* Read the flash size register to determine this is a P4 or not */
817         /* MSP432P4s will return the size of flash.  MSP432E4s will return zero */
818         retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size);
819         if (retval != ERROR_OK)
820                 return retval;
821
822         if (size == 0) {
823                 /* This is likely an MSP432E4 */
824                 msp432_bank->family_type = MSP432E4;
825
826                 retval = target_read_u32(target, E4_DID0_REG, &device_id);
827                 if (retval != ERROR_OK)
828                         return retval;
829
830                 msp432_bank->device_id = device_id;
831
832                 retval = target_read_u32(target, E4_DID1_REG, &hardware_rev);
833                 if (retval != ERROR_OK)
834                         return retval;
835
836                 msp432_bank->hardware_rev = hardware_rev;
837         } else {
838                 /* This is likely an MSP432P4 */
839                 msp432_bank->family_type = MSP432P4;
840
841                 retval = target_read_u32(target, P4_DEVICE_ID_REG, &device_id);
842                 if (retval != ERROR_OK)
843                         return retval;
844
845                 msp432_bank->device_id = device_id & 0xFFFF;
846
847                 retval = target_read_u32(target, P4_HARDWARE_REV_REG, &hardware_rev);
848                 if (retval != ERROR_OK)
849                         return retval;
850
851                 msp432_bank->hardware_rev = hardware_rev & 0xFF;
852         }
853
854         msp432_bank->device_type = msp432_device_type(msp432_bank->family_type,
855                 msp432_bank->device_id, msp432_bank->hardware_rev);
856
857         if (msp432_bank->family_type == MSP432P4) {
858                 /* Set up MSP432P4 specific flash parameters */
859                 if (is_main) {
860                         retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size);
861                         if (retval != ERROR_OK)
862                                 return retval;
863
864                         sector_length = P4_SECTOR_LENGTH;
865                         num_sectors = size / sector_length;
866                 } else if (is_info) {
867                         if (msp432_bank->device_type == MSP432P411X ||
868                                 msp432_bank->device_type == MSP432P411X_GUESS) {
869                                 /* MSP432P411x has an info size register, use that for size */
870                                 retval = target_read_u32(target, P4_FLASH_INFO_SIZE_REG, &size);
871                                 if (retval != ERROR_OK)
872                                         return retval;
873                         } else {
874                                 /* All other MSP432P401x devices have fixed info region size */
875                                 size = 0x4000; /* 16 KB info region */
876                         }
877                         sector_length = P4_SECTOR_LENGTH;
878                         num_sectors = size / sector_length;
879                 } else {
880                         /* Invalid bank somehow */
881                         return ERROR_FAIL;
882                 }
883         } else {
884                 /* Set up MSP432E4 specific flash parameters */
885                 if (is_main) {
886                         size = E4_FLASH_SIZE;
887                         sector_length = E4_SECTOR_LENGTH;
888                         num_sectors = size / sector_length;
889                 } else {
890                         /* Invalid bank somehow */
891                         return ERROR_FAIL;
892                 }
893         }
894
895         free(bank->sectors);
896         bank->sectors = NULL;
897
898         if (num_sectors > 0) {
899                 bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
900                 if (!bank->sectors)
901                         return ERROR_FAIL;
902         }
903
904         bank->size = size;
905         bank->write_start_alignment = 0;
906         bank->write_end_alignment = 0;
907         bank->num_sectors = num_sectors;
908         msp432_bank->sector_length = sector_length;
909
910         for (unsigned int i = 0; i < num_sectors; i++) {
911                 bank->sectors[i].offset = i * sector_length;
912                 bank->sectors[i].size = sector_length;
913                 bank->sectors[i].is_erased = -1;
914                 bank->sectors[i].is_protected = 0;
915         }
916
917         /* We've successfully determined the stats on this flash bank */
918         if (is_main)
919                 msp432_bank->probed_main = true;
920         if (is_info)
921                 msp432_bank->probed_info = true;
922
923         if (is_main && MSP432P4 == msp432_bank->family_type) {
924                 /* Create the info flash bank needed by MSP432P4 variants */
925                 struct flash_bank *info = calloc(sizeof(struct flash_bank), 1);
926                 if (!info)
927                         return ERROR_FAIL;
928
929                 /* Create a name for the info bank, append "_1" to main name */
930                 char *name = malloc(strlen(bank->name) + 3);
931                 strcpy(name, bank->name);
932                 strcat(name, "_1");
933
934                 /* Initialize info bank */
935                 info->name = name;
936                 info->target = bank->target;
937                 info->driver = bank->driver;
938                 info->driver_priv = msp432_bank;
939                 info->base = P4_FLASH_INFO_BASE;
940
941                 flash_bank_add(info);
942         }
943
944         /* If we fall through to here, then all went well */
945
946         return ERROR_OK;
947 }
948
949 static int msp432_auto_probe(struct flash_bank *bank)
950 {
951         struct msp432_bank *msp432_bank = bank->driver_priv;
952
953         bool is_main = bank->base == FLASH_BASE;
954         bool is_info = bank->base == P4_FLASH_INFO_BASE;
955
956         int retval = ERROR_OK;
957
958         if (is_main)
959                 if (!msp432_bank->probed_main)
960                         retval = msp432_probe(bank);
961         if (is_info)
962                 if (!msp432_bank->probed_info)
963                         retval = msp432_probe(bank);
964
965         return retval;
966 }
967
968 static int msp432_info(struct flash_bank *bank, struct command_invocation *cmd)
969 {
970         struct msp432_bank *msp432_bank = bank->driver_priv;
971
972         switch (msp432_bank->device_type) {
973                 case MSP432P401X_DEPR:
974                         if (msp432_bank->device_id == 0xFFFF) {
975                                 /* Very early pre-production silicon currently deprecated */
976                                 command_print_sameline(cmd, "MSP432P401x pre-production device (deprecated silicon)\n"
977                                         SUPPORT_MESSAGE);
978                         } else {
979                                 /* Revision A or B silicon, also deprecated */
980                                 command_print_sameline(cmd, "MSP432P401x Device Rev %c (deprecated silicon)\n"
981                                         SUPPORT_MESSAGE, (char)msp432_bank->hardware_rev);
982                         }
983                         break;
984                 case MSP432P401X:
985                         command_print_sameline(cmd, "MSP432P401x Device Rev %c\n",
986                                 (char)msp432_bank->hardware_rev);
987                         break;
988                 case MSP432P411X:
989                         command_print_sameline(cmd, "MSP432P411x Device Rev %c\n",
990                                 (char)msp432_bank->hardware_rev);
991                         break;
992                 case MSP432E401Y:
993                         command_print_sameline(cmd, "MSP432E401Y Device\n");
994                         break;
995                 case MSP432E411Y:
996                         command_print_sameline(cmd, "MSP432E411Y Device\n");
997                         break;
998                 case MSP432E4X_GUESS:
999                         command_print_sameline(cmd,
1000                                 "Unrecognized MSP432E4 DID0 and DID1 IDs (%08" PRIX32 ", %08" PRIX32 ")",
1001                                 msp432_bank->device_id, msp432_bank->hardware_rev);
1002                         break;
1003                 case MSP432P401X_GUESS:
1004                 case MSP432P411X_GUESS:
1005                 default:
1006                         command_print_sameline(cmd,
1007                                 "Unrecognized MSP432P4 Device ID and Hardware Rev (%04" PRIX32 ", %02" PRIX32 ")",
1008                                 msp432_bank->device_id, msp432_bank->hardware_rev);
1009                         break;
1010         }
1011
1012         return ERROR_OK;
1013 }
1014
1015 static int msp432_protect_check(struct flash_bank *bank)
1016 {
1017         /* Added to suppress warning, not needed for MSP432 flash */
1018         return ERROR_OK;
1019 }
1020
1021 static void msp432_flash_free_driver_priv(struct flash_bank *bank)
1022 {
1023         bool is_main = bank->base == FLASH_BASE;
1024
1025         /* A single private struct is shared between main and info banks */
1026         /* Only free it on the call for main bank */
1027         if (is_main)
1028                 free(bank->driver_priv);
1029
1030         /* Forget about the private struct on both main and info banks */
1031         bank->driver_priv = NULL;
1032 }
1033
1034 static const struct command_registration msp432_exec_command_handlers[] = {
1035         {
1036                 .name = "mass_erase",
1037                 .handler = msp432_mass_erase_command,
1038                 .mode = COMMAND_EXEC,
1039                 .help = "Erase entire flash memory on device.",
1040                 .usage = "bank_id ['main' | 'all']",
1041         },
1042         {
1043                 .name = "bsl",
1044                 .handler = msp432_bsl_command,
1045                 .mode = COMMAND_EXEC,
1046                 .help = "Allow BSL to be erased or written by flash commands.",
1047                 .usage = "bank_id ['unlock' | 'lock']",
1048         },
1049         COMMAND_REGISTRATION_DONE
1050 };
1051
1052 static const struct command_registration msp432_command_handlers[] = {
1053         {
1054                 .name = "msp432",
1055                 .mode = COMMAND_EXEC,
1056                 .help = "MSP432 flash command group",
1057                 .usage = "",
1058                 .chain = msp432_exec_command_handlers,
1059         },
1060         COMMAND_REGISTRATION_DONE
1061 };
1062
1063 const struct flash_driver msp432_flash = {
1064         .name = "msp432",
1065         .commands = msp432_command_handlers,
1066         .flash_bank_command = msp432_flash_bank_command,
1067         .erase = msp432_erase,
1068         .write = msp432_write,
1069         .read = default_flash_read,
1070         .probe = msp432_probe,
1071         .auto_probe = msp432_auto_probe,
1072         .erase_check = default_flash_blank_check,
1073         .protect_check = msp432_protect_check,
1074         .info = msp432_info,
1075         .free_driver_priv = msp432_flash_free_driver_priv,
1076 };