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