1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2018 by Texas Instruments, Inc. *
5 ***************************************************************************/
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>
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
25 /* MSP432E4 hardware registers */
26 #define E4_DID0_REG 0x400FE000
27 #define E4_DID1_REG 0x400FE004
29 #define FLASH_TIMEOUT 8000
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"
37 uint32_t hardware_rev;
40 uint32_t sector_length;
44 struct working_area *working_area;
45 struct armv7m_algorithm armv7m_info;
48 static int msp432_auto_probe(struct flash_bank *bank);
50 static int msp432_device_type(uint32_t family_type, uint32_t device_id,
51 uint32_t hardware_rev)
53 int device_type = MSP432_NO_TYPE;
55 if (family_type == MSP432E4) {
56 /* MSP432E4 device family */
58 if (device_id == 0x180C0002) {
59 if (hardware_rev == 0x102DC06E) {
61 device_type = MSP432E401Y;
62 } else if (hardware_rev == 0x1032E076) {
64 device_type = MSP432E411Y;
66 /* Reasonable guess that this is a new variant */
67 device_type = MSP432E4X_GUESS;
70 /* Wild guess that this is an MSP432E4 */
71 device_type = MSP432E4X_GUESS;
74 /* MSP432P4 device family */
76 /* Examine the device ID and hardware revision to get the device type */
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;
92 /* Unknown or unanticipated hardware revision */
93 device_type = MSP432P401X_GUESS;
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;
111 /* Unknown or unanticipated hardware revision */
112 device_type = MSP432P411X_GUESS;
116 /* Device is very early silicon that has been deprecated */
117 device_type = MSP432P401X_DEPR;
120 if (device_id < 0xA010) {
121 /* Wild guess that this is an MSP432P401x */
122 device_type = MSP432P401X_GUESS;
124 /* Reasonable guess that this is a new variant */
125 device_type = MSP432P411X_GUESS;
134 static const char *msp432_return_text(uint32_t return_code)
136 switch (return_code) {
140 return "FLASH_SUCCESS";
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";
152 return "UNDEFINED_RETURN_CODE";
156 static void msp432_init_params(struct msp432_algo_params *algo_params)
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);
169 static int msp432_exec_cmd(struct target *target, struct msp432_algo_params
170 *algo_params, uint32_t command)
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);
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)
186 /* Write out command to target memory */
187 retval = target_write_u32(target, ALGO_FLASH_COMMAND_ADDR, command);
192 static int msp432_wait_return_code(struct target *target)
194 uint32_t return_code = 0;
196 long long elapsed_ms;
198 int retval = ERROR_OK;
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)
206 elapsed_ms = timeval_ms() - start_ms;
207 if (elapsed_ms > 500)
209 if (elapsed_ms > FLASH_TIMEOUT)
213 if (return_code != FLASH_SUCCESS) {
214 LOG_ERROR("msp432: Flash operation failed: %s",
215 msp432_return_text(return_code));
222 static int msp432_wait_inactive(struct target *target, uint32_t buffer)
224 uint32_t status_code = BUFFER_ACTIVE;
225 uint32_t status_addr;
227 long long elapsed_ms;
232 case 1: /* Buffer 1 */
233 status_addr = ALGO_BUFFER1_STATUS_ADDR;
235 case 2: /* Buffer 2 */
236 status_addr = ALGO_BUFFER2_STATUS_ADDR;
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)
248 elapsed_ms = timeval_ms() - start_ms;
249 if (elapsed_ms > 500)
251 if (elapsed_ms > FLASH_TIMEOUT)
255 if (status_code != BUFFER_INACTIVE) {
257 "msp432: Flash operation failed: buffer not written to flash");
264 static int msp432_init(struct flash_bank *bank)
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];
271 const uint8_t *loader_code;
272 uint32_t loader_size;
273 uint32_t algo_entry_addr;
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)
281 /* Choose appropriate flash helper algorithm */
282 switch (msp432_bank->device_type) {
284 case MSP432P401X_DEPR:
285 case MSP432P401X_GUESS:
287 loader_code = msp432p401x_algo;
288 loader_size = sizeof(msp432p401x_algo);
289 algo_entry_addr = P4_ALGO_ENTRY_ADDR;
292 case MSP432P411X_GUESS:
293 loader_code = msp432p411x_algo;
294 loader_size = sizeof(msp432p411x_algo);
295 algo_entry_addr = P4_ALGO_ENTRY_ADDR;
299 case MSP432E4X_GUESS:
300 loader_code = msp432e4x_algo;
301 loader_size = sizeof(msp432e4x_algo);
302 algo_entry_addr = E4_ALGO_ENTRY_ADDR;
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. */
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) {
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. */
321 "msp432: Unrecognized MSP432E4 DID0 and DID1 values "
322 "(%08" PRIX32 ", %08" PRIX32 ")", msp432_bank->device_id,
323 msp432_bank->hardware_rev);
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;
330 retval = target_alloc_working_area(target, ALGO_WORKING_SIZE,
331 &msp432_bank->working_area);
332 if (retval != ERROR_OK)
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;
339 /* Write flash helper algorithm into target memory */
340 retval = target_write_buffer(target, ALGO_BASE_ADDR, loader_size,
342 if (retval != ERROR_OK)
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;
349 /* Initialize algorithm parameters to default values */
350 msp432_init_params(&algo_params);
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)
358 /* Initialize stack pointer for flash helper algorithm */
359 init_reg_param(®_params[0], "sp", 32, PARAM_OUT);
360 buf_set_u32(reg_params[0].value, 0, 32, ALGO_STACK_POINTER_ADDR);
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(®_params[0]);
366 if (retval != ERROR_OK) {
367 LOG_ERROR("msp432: Failed to start flash helper algorithm");
372 * At this point, the algorithm is running on the target and
373 * ready to receive commands and data to flash the target
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)
381 retval = msp432_wait_return_code(target);
386 static int msp432_quit(struct flash_bank *bank)
388 struct target *target = bank->target;
389 struct msp432_bank *msp432_bank = bank->driver_priv;
390 struct msp432_algo_params algo_params;
394 /* Initialize algorithm parameters to default values */
395 msp432_init_params(&algo_params);
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)
402 (void)msp432_wait_return_code(target);
404 /* Regardless of the return code, attempt to halt the target */
405 (void)target_halt(target);
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);
411 target_free_working_area(target, msp432_bank->working_area);
412 msp432_bank->working_area = NULL;
417 static int msp432_mass_erase(struct flash_bank *bank, bool all)
419 struct target *target = bank->target;
420 struct msp432_bank *msp432_bank = bank->driver_priv;
421 struct msp432_algo_params algo_params;
425 if (target->state != TARGET_HALTED) {
426 LOG_ERROR("Target not halted");
427 return ERROR_TARGET_NOT_HALTED;
430 retval = msp432_init(bank);
431 if (retval != ERROR_OK)
434 /* Initialize algorithm parameters to default values */
435 msp432_init_params(&algo_params);
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);
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);
450 retval = msp432_wait_return_code(target);
451 if (retval != ERROR_OK) {
452 (void)msp432_quit(bank);
456 retval = msp432_quit(bank);
457 if (retval != ERROR_OK)
463 COMMAND_HANDLER(msp432_mass_erase_command)
465 struct flash_bank *bank;
466 struct msp432_bank *msp432_bank;
472 return ERROR_COMMAND_SYNTAX_ERROR;
474 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
475 if (retval != ERROR_OK)
480 } else if (2 == CMD_ARGC) {
481 /* Check argument for how much to erase */
482 if (strcmp(CMD_ARGV[1], "main") == 0)
484 else if (strcmp(CMD_ARGV[1], "all") == 0)
487 return ERROR_COMMAND_SYNTAX_ERROR;
489 return ERROR_COMMAND_SYNTAX_ERROR;
492 msp432_bank = bank->driver_priv;
494 if (msp432_bank->family_type == MSP432E4) {
495 /* MSP432E4 does not have main vs info regions, ignore "all" */
499 retval = msp432_mass_erase(bank, all);
500 if (retval != ERROR_OK)
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");
507 LOG_INFO("msp432: Mass erase of %s is complete",
508 all ? "main + information flash" : "main flash");
514 COMMAND_HANDLER(msp432_bsl_command)
516 struct flash_bank *bank;
517 struct msp432_bank *msp432_bank;
522 return ERROR_COMMAND_SYNTAX_ERROR;
524 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
525 if (retval != ERROR_OK)
528 msp432_bank = bank->driver_priv;
530 if (msp432_bank->family_type == MSP432E4) {
531 LOG_WARNING("msp432: MSP432E4 does not have a BSL region");
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;
541 return ERROR_COMMAND_SYNTAX_ERROR;
542 } else if (1 != CMD_ARGC) {
543 /* Extra, unknown argument passed in */
544 return ERROR_COMMAND_SYNTAX_ERROR;
547 LOG_INFO("msp432: BSL flash region is currently %slocked",
548 msp432_bank->unlock_bsl ? "un" : "");
553 FLASH_BANK_COMMAND_HANDLER(msp432_flash_bank_command)
555 struct msp432_bank *msp432_bank;
558 return ERROR_COMMAND_SYNTAX_ERROR;
560 /* Create shared private struct for flash banks */
561 msp432_bank = malloc(sizeof(struct msp432_bank));
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;
576 /* Finish up initial settings here */
577 bank->driver_priv = msp432_bank;
578 bank->base = FLASH_BASE;
583 static int msp432_erase(struct flash_bank *bank, unsigned int first,
586 struct target *target = bank->target;
587 struct msp432_bank *msp432_bank = bank->driver_priv;
588 struct msp432_algo_params algo_params;
590 bool is_main = bank->base == FLASH_BASE;
591 bool is_info = bank->base == P4_FLASH_INFO_BASE;
595 if (target->state != TARGET_HALTED) {
596 LOG_ERROR("Target not halted");
597 return ERROR_TARGET_NOT_HALTED;
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);
606 retval = msp432_init(bank);
607 if (retval != ERROR_OK)
610 /* Initialize algorithm parameters to default values */
611 msp432_init_params(&algo_params);
613 /* Adjust params if this is the info bank */
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);
621 /* Erase requested sectors one by one */
622 for (unsigned int i = first; i <= last; i++) {
624 /* Skip TVL (read-only) sector of the info bank */
625 if (is_info && 1 == i)
628 /* Skip BSL sectors of info bank if locked */
629 if (is_info && (2 == i || 3 == i) &&
630 !msp432_bank->unlock_bsl)
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));
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);
644 retval = msp432_wait_return_code(target);
645 if (retval != ERROR_OK) {
646 (void)msp432_quit(bank);
651 retval = msp432_quit(bank);
652 if (retval != ERROR_OK)
658 static int msp432_write(struct flash_bank *bank, const uint8_t *buffer,
659 uint32_t offset, uint32_t count)
661 struct target *target = bank->target;
662 struct msp432_bank *msp432_bank = bank->driver_priv;
663 struct msp432_algo_params algo_params;
665 uint32_t data_ready = BUFFER_DATA_READY;
667 long long elapsed_ms;
669 bool is_info = bank->base == P4_FLASH_INFO_BASE;
673 if (target->state != TARGET_HALTED) {
674 LOG_ERROR("Target not halted");
675 return ERROR_TARGET_NOT_HALTED;
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
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 */
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)
701 /* Send a request for anything after read-only sectors */
702 if ((offset + count - 1) >= end) {
703 uint32_t skip = end - offset;
707 return msp432_write(bank, buffer, offset, count);
709 /* Request is entirely in read-only sectors */
715 retval = msp432_init(bank);
716 if (retval != ERROR_OK)
719 /* Initialize algorithm parameters to default values */
720 msp432_init_params(&algo_params);
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);
726 /* Check if this is the info bank */
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);
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);
740 /* Write requested data, one buffer at a time */
741 start_ms = timeval_ms();
744 if (count > ALGO_BUFFER_SIZE)
745 size = ALGO_BUFFER_SIZE;
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;
757 /* Signal the flash helper algorithm that data is ready to flash */
758 retval = target_write_u32(target, ALGO_BUFFER1_STATUS_ADDR,
760 if (retval != ERROR_OK) {
761 (void)msp432_quit(bank);
762 return ERROR_FLASH_OPERATION_FAILED;
765 retval = msp432_wait_inactive(target, 1);
766 if (retval != ERROR_OK) {
767 (void)msp432_quit(bank);
774 elapsed_ms = timeval_ms() - start_ms;
775 if (elapsed_ms > 500)
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);
786 retval = msp432_quit(bank);
787 if (retval != ERROR_OK)
793 static int msp432_probe(struct flash_bank *bank)
795 struct target *target = bank->target;
796 struct msp432_bank *msp432_bank = bank->driver_priv;
799 uint32_t hardware_rev;
801 uint32_t sector_length;
803 unsigned int num_sectors;
805 bool is_main = bank->base == FLASH_BASE;
806 bool is_info = bank->base == P4_FLASH_INFO_BASE;
810 /* Check if this bank has already been successfully probed */
811 if (is_main && msp432_bank->probed_main)
813 if (is_info && msp432_bank->probed_info)
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)
823 /* This is likely an MSP432E4 */
824 msp432_bank->family_type = MSP432E4;
826 retval = target_read_u32(target, E4_DID0_REG, &device_id);
827 if (retval != ERROR_OK)
830 msp432_bank->device_id = device_id;
832 retval = target_read_u32(target, E4_DID1_REG, &hardware_rev);
833 if (retval != ERROR_OK)
836 msp432_bank->hardware_rev = hardware_rev;
838 /* This is likely an MSP432P4 */
839 msp432_bank->family_type = MSP432P4;
841 retval = target_read_u32(target, P4_DEVICE_ID_REG, &device_id);
842 if (retval != ERROR_OK)
845 msp432_bank->device_id = device_id & 0xFFFF;
847 retval = target_read_u32(target, P4_HARDWARE_REV_REG, &hardware_rev);
848 if (retval != ERROR_OK)
851 msp432_bank->hardware_rev = hardware_rev & 0xFF;
854 msp432_bank->device_type = msp432_device_type(msp432_bank->family_type,
855 msp432_bank->device_id, msp432_bank->hardware_rev);
857 if (msp432_bank->family_type == MSP432P4) {
858 /* Set up MSP432P4 specific flash parameters */
860 retval = target_read_u32(target, P4_FLASH_MAIN_SIZE_REG, &size);
861 if (retval != ERROR_OK)
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)
874 /* All other MSP432P401x devices have fixed info region size */
875 size = 0x4000; /* 16 KB info region */
877 sector_length = P4_SECTOR_LENGTH;
878 num_sectors = size / sector_length;
880 /* Invalid bank somehow */
884 /* Set up MSP432E4 specific flash parameters */
886 size = E4_FLASH_SIZE;
887 sector_length = E4_SECTOR_LENGTH;
888 num_sectors = size / sector_length;
890 /* Invalid bank somehow */
896 bank->sectors = NULL;
898 if (num_sectors > 0) {
899 bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
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;
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;
917 /* We've successfully determined the stats on this flash bank */
919 msp432_bank->probed_main = true;
921 msp432_bank->probed_info = true;
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);
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);
934 /* Initialize info bank */
936 info->target = bank->target;
937 info->driver = bank->driver;
938 info->driver_priv = msp432_bank;
939 info->base = P4_FLASH_INFO_BASE;
941 flash_bank_add(info);
944 /* If we fall through to here, then all went well */
949 static int msp432_auto_probe(struct flash_bank *bank)
951 struct msp432_bank *msp432_bank = bank->driver_priv;
953 bool is_main = bank->base == FLASH_BASE;
954 bool is_info = bank->base == P4_FLASH_INFO_BASE;
956 int retval = ERROR_OK;
959 if (!msp432_bank->probed_main)
960 retval = msp432_probe(bank);
962 if (!msp432_bank->probed_info)
963 retval = msp432_probe(bank);
968 static int msp432_info(struct flash_bank *bank, struct command_invocation *cmd)
970 struct msp432_bank *msp432_bank = bank->driver_priv;
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"
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);
985 command_print_sameline(cmd, "MSP432P401x Device Rev %c\n",
986 (char)msp432_bank->hardware_rev);
989 command_print_sameline(cmd, "MSP432P411x Device Rev %c\n",
990 (char)msp432_bank->hardware_rev);
993 command_print_sameline(cmd, "MSP432E401Y Device\n");
996 command_print_sameline(cmd, "MSP432E411Y Device\n");
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);
1003 case MSP432P401X_GUESS:
1004 case MSP432P411X_GUESS:
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);
1015 static int msp432_protect_check(struct flash_bank *bank)
1017 /* Added to suppress warning, not needed for MSP432 flash */
1021 static void msp432_flash_free_driver_priv(struct flash_bank *bank)
1023 bool is_main = bank->base == FLASH_BASE;
1025 /* A single private struct is shared between main and info banks */
1026 /* Only free it on the call for main bank */
1028 free(bank->driver_priv);
1030 /* Forget about the private struct on both main and info banks */
1031 bank->driver_priv = NULL;
1034 static const struct command_registration msp432_exec_command_handlers[] = {
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']",
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']",
1049 COMMAND_REGISTRATION_DONE
1052 static const struct command_registration msp432_command_handlers[] = {
1055 .mode = COMMAND_EXEC,
1056 .help = "MSP432 flash command group",
1058 .chain = msp432_exec_command_handlers,
1060 COMMAND_REGISTRATION_DONE
1063 const struct flash_driver msp432_flash = {
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,