Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

flash.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2015 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Local Includes -----
  18. #include "mchck.h"
  19. // ----- Variables -----
  20. uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
  21. // ----- Functions -----
  22. /* This will have to live in SRAM. */
  23. __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
  24. int ftfl_submit_cmd()
  25. {
  26. FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
  27. .ccif = 1,
  28. //.rdcolerr = 1,
  29. .accerr = 1,
  30. .fpviol = 1
  31. }).raw;
  32. // Wait for the operation to complete
  33. struct FTFL_FSTAT_t stat;
  34. while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
  35. // Mask error bits
  36. return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
  37. //return (!!stat.mgstat0);
  38. }
  39. int flash_prepare_flashing()
  40. {
  41. /* switch to FlexRAM */
  42. if ( !FTFL.fcnfg.ramrdy )
  43. {
  44. FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
  45. FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
  46. return (ftfl_submit_cmd());
  47. }
  48. return (0);
  49. }
  50. int flash_read_1s_sector( uintptr_t addr, size_t num )
  51. {
  52. FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
  53. FTFL.fccob.read_1s_section.addr = addr;
  54. FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
  55. FTFL.fccob.read_1s_section.num_words = num;
  56. return ftfl_submit_cmd();
  57. }
  58. int flash_erase_sector( uintptr_t addr )
  59. {
  60. if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
  61. return (-1);
  62. FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
  63. FTFL.fccob.erase.addr = addr;
  64. return ftfl_submit_cmd();
  65. }
  66. int flash_program_section_longwords( uintptr_t addr, size_t num_words )
  67. {
  68. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  69. FTFL.fccob.program_section.addr = addr;
  70. FTFL.fccob.program_section.num_words = num_words;
  71. return ftfl_submit_cmd();
  72. }
  73. int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
  74. {
  75. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  76. FTFL.fccob.program_section.addr = addr;
  77. FTFL.fccob.program_section.num_words = num_phrases;
  78. return ftfl_submit_cmd();
  79. }
  80. int flash_program_sector( uintptr_t addr, size_t len )
  81. {
  82. #if defined(_mk20dx128vlf5_)
  83. return (len != FLASH_SECTOR_SIZE
  84. || (addr & (FLASH_SECTOR_SIZE - 1)) != 0
  85. || flash_erase_sector( addr )
  86. || flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 ));
  87. #elif defined(_mk20dx256vlh7_)
  88. if ( len != FLASH_SECTOR_SIZE )
  89. return 1;
  90. // Check if beginning of sector and erase if not empty
  91. // Each sector is 2 kB in length, but we can only write to half a sector at a time
  92. // We can only erase an entire sector at a time
  93. if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
  94. && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
  95. && flash_erase_sector( addr ) )
  96. return 1;
  97. // Program half-sector
  98. return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
  99. #endif
  100. }
  101. int flash_prepare_reading()
  102. {
  103. return (0);
  104. }
  105. int flash_read_sector( uintptr_t addr, size_t len )
  106. {
  107. return (0);
  108. }
  109. void *flash_get_staging_area( uintptr_t addr, size_t len )
  110. {
  111. if ( (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || len != FLASH_SECTOR_SIZE )
  112. return (NULL);
  113. return (FlexRAM);
  114. }