You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
3.9 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 Loci Controls Inc
  4. * Copyright (C) 2016-2018 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * Neither the name of Texas Instruments Incorporated nor the names of
  19. * its contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************/
  35. #include <stdint.h>
  36. #include <stdbool.h>
  37. #include "flash.h"
  38. struct rom_api {
  39. unsigned long (*Crc32)(unsigned char *pucData,
  40. unsigned long ulByteCount);
  41. unsigned long (*GetFlashSize)(void);
  42. unsigned long (*GetChipId)(void);
  43. long (*PageErase)(unsigned long ulStartAddress,
  44. unsigned long ulEraseSize);
  45. long (*ProgramFlash)(unsigned long *pulData,
  46. unsigned long ulAddress,
  47. unsigned long ulCount);
  48. } *ROM = (struct rom_api *)0x00000048;
  49. extern uint32_t flash_sector_erase(uint32_t sector)
  50. {
  51. long ret;
  52. bool already_erased = true;
  53. int i;
  54. uint32_t address = flash_sector_to_address(sector);
  55. for (i = 0; i < FLASH_ERASE_SIZE; i += 4) {
  56. if ((*(uint32_t *)(address + i)) != 0xffffffff) {
  57. already_erased = false;
  58. break;
  59. }
  60. }
  61. if (already_erased)
  62. return 0;
  63. ret = ROM->PageErase(address, FLASH_ERASE_SIZE);
  64. if (ret == -1)
  65. return 0x101;
  66. if (ret == -2)
  67. return 0x102;
  68. if (ret != 0)
  69. return 0x103;
  70. return 0;
  71. }
  72. extern uint32_t flash_bank_erase(void)
  73. {
  74. int i;
  75. long ret;
  76. /* Some pages may be locked by bits in the CCA. Erasing the CCA
  77. first will ensure that those pages will become unlocked.
  78. So, since the CCA is the last page in flash, let's just erase
  79. sectors backwards. */
  80. int num_sectors = ROM->GetFlashSize() / FLASH_ERASE_SIZE;
  81. for (i = num_sectors - 1; i >= 0; i--)
  82. {
  83. ret = flash_sector_erase(i);
  84. if (ret != 0)
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. extern uint32_t flash_program(uint8_t *data_buffer, uint32_t address,
  90. uint32_t count)
  91. {
  92. long ret;
  93. ret = ROM->ProgramFlash((unsigned long *)data_buffer, address, count);
  94. if (ret == -1)
  95. return 0x104;
  96. if (ret == -2)
  97. return 0x105;
  98. if (ret != 0)
  99. return 0x106;
  100. return 0;
  101. }