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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(void)
  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(void)
  40. {
  41. /* switch to FlexRAM */
  42. if (!FTFL.fcnfg.ramrdy) {
  43. FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
  44. FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
  45. return (ftfl_submit_cmd());
  46. }
  47. return (0);
  48. }
  49. int flash_erase_sector(uintptr_t addr)
  50. {
  51. if (addr < (uintptr_t)&_app_rom &&
  52. flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
  53. return (-1);
  54. FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
  55. FTFL.fccob.erase.addr = addr;
  56. return (ftfl_submit_cmd());
  57. }
  58. int flash_program_section_longwords(uintptr_t addr, size_t num_words)
  59. {
  60. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  61. FTFL.fccob.program_section.addr = addr;
  62. FTFL.fccob.program_section.num_words = num_words;
  63. return ftfl_submit_cmd();
  64. }
  65. int flash_program_section_phrases(uintptr_t addr, size_t num_phrases)
  66. {
  67. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  68. FTFL.fccob.program_section.addr = addr;
  69. FTFL.fccob.program_section.num_words = num_phrases;
  70. return ftfl_submit_cmd();
  71. }
  72. int flash_program_longword(uintptr_t addr, uint8_t *data)
  73. {
  74. FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
  75. FTFL.fccob.program_longword.addr = addr;
  76. FTFL.fccob.program_longword.data_be[0] = data[0];
  77. FTFL.fccob.program_longword.data_be[1] = data[1];
  78. FTFL.fccob.program_longword.data_be[2] = data[2];
  79. FTFL.fccob.program_longword.data_be[3] = data[3];
  80. return ftfl_submit_cmd();
  81. }
  82. int flash_program_sector(uintptr_t addr, size_t len)
  83. {
  84. #if defined(_mk20dx128vlf5_)
  85. return (len != FLASH_SECTOR_SIZE ||
  86. (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  87. flash_erase_sector(addr) ||
  88. flash_program_section_longwords(addr, FLASH_SECTOR_SIZE / 4));
  89. #elif defined(_mk20dx256vlh7_)
  90. /*
  91. return (len != FLASH_SECTOR_SIZE ||
  92. (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  93. flash_erase_sector(addr) ||
  94. flash_program_section_phrases(addr, FLASH_SECTOR_SIZE / 8));
  95. */
  96. return (len != FLASH_SECTOR_SIZE ||
  97. (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  98. flash_erase_sector(addr) ||
  99. flash_program_section_phrases(addr, FLASH_SECTOR_SIZE / 8));
  100. #endif
  101. }
  102. int flash_prepare_reading(void)
  103. {
  104. return (0);
  105. }
  106. int flash_read_sector(uintptr_t addr, size_t len)
  107. {
  108. return (0);
  109. }
  110. void *flash_get_staging_area(uintptr_t addr, size_t len)
  111. {
  112. if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  113. len != FLASH_SECTOR_SIZE)
  114. return (NULL);
  115. return (FlexRAM);
  116. }