plat_nv_storage.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <platform_def.h>
  15. #include <common/debug.h>
  16. #ifndef NXP_COINED_BB
  17. #include <flash_info.h>
  18. #include <fspi.h>
  19. #include <fspi_api.h>
  20. #endif
  21. #include <lib/mmio.h>
  22. #ifdef NXP_COINED_BB
  23. #include <snvs.h>
  24. #else
  25. #include <xspi_error_codes.h>
  26. #endif
  27. #include <plat_nv_storage.h>
  28. /*This structure will be a static structure and
  29. * will be populated as first step of BL2 booting-up.
  30. * fspi_strorage.c . To be located in the fspi driver folder.
  31. */
  32. static nv_app_data_t nv_app_data;
  33. int read_nv_app_data(void)
  34. {
  35. int ret = 0;
  36. #ifdef NXP_COINED_BB
  37. uint8_t *nv_app_data_array = (uint8_t *) &nv_app_data;
  38. uint8_t offset = 0U;
  39. ret = snvs_read_app_data();
  40. do {
  41. nv_app_data_array[offset] = snvs_read_app_data_bit(offset);
  42. offset++;
  43. } while (offset < APP_DATA_MAX_OFFSET);
  44. snvs_clear_app_data();
  45. #else
  46. uintptr_t nv_base_addr = NV_STORAGE_BASE_ADDR;
  47. ret = fspi_init(NXP_FLEXSPI_ADDR, NXP_FLEXSPI_FLASH_ADDR);
  48. if (ret != XSPI_SUCCESS) {
  49. ERROR("Failed to initialized driver flexspi-nor.\n");
  50. ERROR("exiting warm-reset request.\n");
  51. return -ENODEV;
  52. }
  53. xspi_read(nv_base_addr,
  54. (uint32_t *)&nv_app_data, sizeof(nv_app_data_t));
  55. xspi_sector_erase((uint32_t) nv_base_addr,
  56. F_SECTOR_ERASE_SZ);
  57. #endif
  58. return ret;
  59. }
  60. int wr_nv_app_data(int data_offset,
  61. uint8_t *data,
  62. int data_size)
  63. {
  64. int ret = 0;
  65. #ifdef NXP_COINED_BB
  66. #if !TRUSTED_BOARD_BOOT
  67. snvs_disable_zeroize_lp_gpr();
  68. #endif
  69. /* In case LP SecMon General purpose register,
  70. * only 1 bit flags can be saved.
  71. */
  72. if ((data_size > 1) || (*data != DEFAULT_SET_VALUE)) {
  73. ERROR("Only binary value is allowed to be written.\n");
  74. ERROR("Use flash instead of SNVS GPR as NV location.\n");
  75. return -ENODEV;
  76. }
  77. snvs_write_app_data_bit(data_offset);
  78. #else
  79. uint8_t read_val[sizeof(nv_app_data_t)];
  80. uint8_t ready_to_write_val[sizeof(nv_app_data_t)];
  81. uintptr_t nv_base_addr = NV_STORAGE_BASE_ADDR;
  82. assert((nv_base_addr + data_offset + data_size) <= (nv_base_addr + F_SECTOR_ERASE_SZ));
  83. ret = fspi_init(NXP_FLEXSPI_ADDR, NXP_FLEXSPI_FLASH_ADDR);
  84. if (ret != XSPI_SUCCESS) {
  85. ERROR("Failed to initialized driver flexspi-nor.\n");
  86. ERROR("exiting warm-reset request.\n");
  87. return -ENODEV;
  88. }
  89. ret = xspi_read(nv_base_addr + data_offset, (uint32_t *)read_val, data_size);
  90. memset(ready_to_write_val, READY_TO_WRITE_VALUE, ARRAY_SIZE(ready_to_write_val));
  91. if (memcmp(read_val, ready_to_write_val, data_size) == 0) {
  92. xspi_write(nv_base_addr + data_offset, data, data_size);
  93. }
  94. #endif
  95. return ret;
  96. }
  97. const nv_app_data_t *get_nv_data(void)
  98. {
  99. return (const nv_app_data_t *) &nv_app_data;
  100. }