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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  59. FTFL.fccob.program_section.addr = addr;
  60. FTFL.fccob.program_section.num_words = num_words;
  61. return (ftfl_submit_cmd());
  62. }
  63. int flash_program_sector(uintptr_t addr, size_t len)
  64. {
  65. return (len != FLASH_SECTOR_SIZE ||
  66. (addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  67. flash_erase_sector(addr) ||
  68. flash_program_section(addr, FLASH_SECTOR_SIZE/4));
  69. }
  70. void *flash_get_staging_area(uintptr_t addr, size_t len)
  71. {
  72. if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 ||
  73. len != FLASH_SECTOR_SIZE)
  74. return (NULL);
  75. return (FlexRAM);
  76. }