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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014 <[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. struct FTFL_FSTAT_t stat;
  33. while (!(stat = FTFL.fstat).ccif)
  34. /* NOTHING */; /* XXX maybe WFI? */
  35. return (!!stat.mgstat0);
  36. }
  37. int flash_prepare_flashing(void)
  38. {
  39. /* switch to FlexRAM */
  40. if (!FTFL.fcnfg.ramrdy) {
  41. FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
  42. FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
  43. return (ftfl_submit_cmd());
  44. }
  45. return (0);
  46. }
  47. int flash_erase_sector(uintptr_t addr)
  48. {
  49. if (addr < (uintptr_t)&_app_rom &&
  50. flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420)
  51. return (-1);
  52. FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
  53. FTFL.fccob.erase.addr = addr;
  54. return (ftfl_submit_cmd());
  55. }
  56. int flash_program_section(uintptr_t addr, size_t num_words)
  57. {
  58. GPIOA_PSOR |= (1<<19);
  59. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  60. FTFL.fccob.program_section.addr = addr;
  61. FTFL.fccob.program_section.num_words = num_words;
  62. return (ftfl_submit_cmd());
  63. }
  64. int flash_program_sector(uintptr_t addr, size_t len)
  65. {
  66. return (len != FLASH_SECTOR_SIZE ||
  67. (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  68. flash_erase_sector(addr) ||
  69. flash_program_section(addr, FLASH_SECTOR_SIZE/4));
  70. }
  71. void *flash_get_staging_area(uintptr_t addr, size_t len)
  72. {
  73. if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  74. len != FLASH_SECTOR_SIZE)
  75. return (NULL);
  76. return (FlexRAM);
  77. }