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.
 
 
 
 
 
 

108 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_address)
  50. {
  51. long ret;
  52. bool already_erased = true;
  53. int i;
  54. for (i = 0; i < FLASH_ERASE_SIZE; i += 4) {
  55. if ((*(uint32_t *)(sector_address + i)) != 0xffffffff) {
  56. already_erased = false;
  57. break;
  58. }
  59. }
  60. if (already_erased)
  61. return 0;
  62. ret = ROM->PageErase(sector_address, FLASH_ERASE_SIZE);
  63. if (ret == -1)
  64. return 0x101;
  65. if (ret == -2)
  66. return 0x102;
  67. if (ret != 0)
  68. return 0x103;
  69. return 0;
  70. }
  71. extern uint32_t flash_bank_erase(void)
  72. {
  73. int i;
  74. long ret;
  75. /* Some pages may be locked by bits in the CCA. Erasing the CCA
  76. first will ensure that those pages will become unlocked.
  77. So, since the CCA is the last page in flash, let's just erase
  78. sectors backwards. */
  79. int num_sectors = ROM->GetFlashSize() / FLASH_ERASE_SIZE;
  80. for (i = num_sectors - 1; i >= 0; i--)
  81. {
  82. ret = flash_sector_erase(flash_sector_to_address(i));
  83. if (ret != 0)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. extern uint32_t flash_program(uint8_t *data_buffer, uint32_t address,
  89. uint32_t count)
  90. {
  91. long ret;
  92. ret = ROM->ProgramFlash((unsigned long *)data_buffer, address, count);
  93. if (ret == -1)
  94. return 0x104;
  95. if (ret == -2)
  96. return 0x105;
  97. if (ret != 0)
  98. return 0x106;
  99. return 0;
  100. }