fsl_flashiap.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_flashiap.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.flashiap"
  38. #endif
  39. #define HZ_TO_KHZ_DIV 1000
  40. /*******************************************************************************
  41. * Code
  42. ******************************************************************************/
  43. static status_t translate_iap_status(uint32_t status)
  44. {
  45. /* Translate IAP return code to sdk status code */
  46. if (status == kStatus_Success)
  47. {
  48. return status;
  49. }
  50. else
  51. {
  52. return MAKE_STATUS(kStatusGroup_FLASHIAP, status);
  53. }
  54. }
  55. status_t FLASHIAP_PrepareSectorForWrite(uint32_t startSector, uint32_t endSector)
  56. {
  57. uint32_t command[5], result[4];
  58. command[0] = kIapCmd_FLASHIAP_PrepareSectorforWrite;
  59. command[1] = startSector;
  60. command[2] = endSector;
  61. iap_entry(command, result);
  62. return translate_iap_status(result[0]);
  63. }
  64. status_t FLASHIAP_CopyRamToFlash(uint32_t dstAddr, uint32_t *srcAddr, uint32_t numOfBytes, uint32_t systemCoreClock)
  65. {
  66. uint32_t command[5], result[4];
  67. command[0] = kIapCmd_FLASHIAP_CopyRamToFlash;
  68. command[1] = dstAddr;
  69. command[2] = (uint32_t)srcAddr;
  70. command[3] = numOfBytes;
  71. command[4] = systemCoreClock / HZ_TO_KHZ_DIV;
  72. iap_entry(command, result);
  73. return translate_iap_status(result[0]);
  74. }
  75. status_t FLASHIAP_EraseSector(uint32_t startSector, uint32_t endSector, uint32_t systemCoreClock)
  76. {
  77. uint32_t command[5], result[4];
  78. command[0] = kIapCmd_FLASHIAP_EraseSector;
  79. command[1] = startSector;
  80. command[2] = endSector;
  81. command[3] = systemCoreClock / HZ_TO_KHZ_DIV;
  82. iap_entry(command, result);
  83. return translate_iap_status(result[0]);
  84. }
  85. status_t FLASHIAP_ErasePage(uint32_t startPage, uint32_t endPage, uint32_t systemCoreClock)
  86. {
  87. uint32_t command[5], result[4];
  88. command[0] = kIapCmd_FLASHIAP_ErasePage;
  89. command[1] = startPage;
  90. command[2] = endPage;
  91. command[3] = systemCoreClock / HZ_TO_KHZ_DIV;
  92. iap_entry(command, result);
  93. return translate_iap_status(result[0]);
  94. }
  95. status_t FLASHIAP_BlankCheckSector(uint32_t startSector, uint32_t endSector)
  96. {
  97. uint32_t command[5], result[4];
  98. command[0] = kIapCmd_FLASHIAP_BlankCheckSector;
  99. command[1] = startSector;
  100. command[2] = endSector;
  101. iap_entry(command, result);
  102. return translate_iap_status(result[0]);
  103. }
  104. status_t FLASHIAP_Compare(uint32_t dstAddr, uint32_t *srcAddr, uint32_t numOfBytes)
  105. {
  106. uint32_t command[5], result[4];
  107. command[0] = kIapCmd_FLASHIAP_Compare;
  108. command[1] = dstAddr;
  109. command[2] = (uint32_t)srcAddr;
  110. command[3] = numOfBytes;
  111. iap_entry(command, result);
  112. return translate_iap_status(result[0]);
  113. }