2014-08-15 17:53:43 +00:00
/* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2015-04-05 06:21:11 +00:00
* Modifications by Jacob Alexander 2014 - 2015 < haata @ kiibohd . com >
2014-08-15 17:53:43 +00:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
// ----- Local Includes -----
# include "mchck.h"
2015-05-30 07:02:22 +00:00
# include "debug.h"
2014-08-15 17:53:43 +00:00
// ----- Variables -----
uint32_t flash_ALLOW_BRICKABLE_ADDRESSES ;
// ----- Functions -----
/* This will have to live in SRAM. */
__attribute__ ( ( section ( " .ramtext.ftfl_submit_cmd " ) , long_call ) )
2015-05-26 00:23:18 +00:00
int ftfl_submit_cmd ( )
2014-08-15 17:53:43 +00:00
{
2015-03-09 01:40:01 +00:00
FTFL . fstat . raw = ( ( struct FTFL_FSTAT_t ) {
. ccif = 1 ,
2015-04-05 06:21:11 +00:00
//.rdcolerr = 1,
. accerr = 1 ,
. fpviol = 1
} ) . raw ;
// Wait for the operation to complete
2015-03-09 01:40:01 +00:00
struct FTFL_FSTAT_t stat ;
2015-04-05 06:21:11 +00:00
while ( ! ( stat = FTFL . fstat ) . ccif ) ; // XXX maybe WFI?
// Mask error bits
return stat . raw & ( FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0 ) ;
//return (!!stat.mgstat0);
2014-08-15 17:53:43 +00:00
}
2015-05-26 00:23:18 +00:00
int flash_prepare_flashing ( )
2014-08-15 17:53:43 +00:00
{
2015-03-09 01:40:01 +00:00
/* switch to FlexRAM */
2015-05-26 00:23:18 +00:00
if ( ! FTFL . fcnfg . ramrdy )
{
2015-03-09 01:40:01 +00:00
FTFL . fccob . set_flexram . fcmd = FTFL_FCMD_SET_FLEXRAM ;
FTFL . fccob . set_flexram . flexram_function = FTFL_FLEXRAM_RAM ;
return ( ftfl_submit_cmd ( ) ) ;
}
return ( 0 ) ;
2014-08-15 17:53:43 +00:00
}
2015-05-26 00:23:18 +00:00
int flash_read_1s_sector ( uintptr_t addr , size_t num )
2014-08-15 17:53:43 +00:00
{
2015-05-26 00:23:18 +00:00
FTFL . fccob . read_1s_section . fcmd = FTFL_FCMD_READ_1s_SECTION ;
FTFL . fccob . read_1s_section . addr = addr ;
FTFL . fccob . read_1s_section . margin = FTFL_MARGIN_NORMAL ;
FTFL . fccob . read_1s_section . num_words = num ;
return ftfl_submit_cmd ( ) ;
}
int flash_erase_sector ( uintptr_t addr )
{
2015-05-30 07:02:22 +00:00
# ifdef FLASH_DEBUG
// Debug
print ( " Erasing Sector: address( " ) ;
printHex ( addr ) ;
printNL ( " ) " ) ;
# endif
2015-05-26 00:23:18 +00:00
if ( addr < ( uintptr_t ) & _app_rom & & flash_ALLOW_BRICKABLE_ADDRESSES ! = 0x00023420 )
2015-03-09 01:40:01 +00:00
return ( - 1 ) ;
FTFL . fccob . erase . fcmd = FTFL_FCMD_ERASE_SECTOR ;
FTFL . fccob . erase . addr = addr ;
2015-05-26 00:23:18 +00:00
return ftfl_submit_cmd ( ) ;
2014-08-15 17:53:43 +00:00
}
2015-05-26 00:23:18 +00:00
int flash_program_section_longwords ( uintptr_t addr , size_t num_words )
2014-08-15 17:53:43 +00:00
{
2015-05-30 07:02:22 +00:00
# ifdef FLASH_DEBUG
// Debug
print ( " Programming Sector: address( " ) ;
printHex ( addr ) ;
print ( " ) longwords( " ) ;
printHex ( num_words ) ;
printNL ( " ) " ) ;
# endif
2015-03-09 01:40:01 +00:00
FTFL . fccob . program_section . fcmd = FTFL_FCMD_PROGRAM_SECTION ;
FTFL . fccob . program_section . addr = addr ;
FTFL . fccob . program_section . num_words = num_words ;
2015-04-05 06:21:11 +00:00
return ftfl_submit_cmd ( ) ;
}
2015-05-26 00:23:18 +00:00
int flash_program_section_phrases ( uintptr_t addr , size_t num_phrases )
2015-04-05 06:21:11 +00:00
{
2015-05-30 07:02:22 +00:00
# ifdef FLASH_DEBUG
// Debug
print ( " Programming Sector: address( " ) ;
printHex ( addr ) ;
print ( " ) phrases( " ) ;
printHex ( num_phrases ) ;
printNL ( " ) " ) ;
# endif
2015-04-05 06:21:11 +00:00
FTFL . fccob . program_section . fcmd = FTFL_FCMD_PROGRAM_SECTION ;
FTFL . fccob . program_section . addr = addr ;
FTFL . fccob . program_section . num_words = num_phrases ;
return ftfl_submit_cmd ( ) ;
}
2015-05-26 00:23:18 +00:00
int flash_program_sector ( uintptr_t addr , size_t len )
2014-08-15 17:53:43 +00:00
{
2015-05-30 07:02:22 +00:00
if ( len ! = USB_DFU_TRANSFER_SIZE )
2015-05-26 00:23:18 +00:00
return 1 ;
2015-05-26 00:50:32 +00:00
# if defined(_mk20dx128vlf5_)
// Check if this is the beginning of a sector
// Only erase if necessary
if ( ( addr & ( FLASH_SECTOR_SIZE - 1 ) ) = = 0
& & flash_read_1s_sector ( addr , FLASH_SECTOR_SIZE / 4 )
& & flash_erase_sector ( addr ) )
return 1 ;
// Program sector
return flash_program_section_longwords ( addr , FLASH_SECTOR_SIZE / 4 ) ;
# elif defined(_mk20dx256vlh7_)
2015-05-26 00:23:18 +00:00
// Check if beginning of sector and erase if not empty
// Each sector is 2 kB in length, but we can only write to half a sector at a time
// We can only erase an entire sector at a time
if ( ( addr & ( FLASH_SECTOR_SIZE - 1 ) ) = = 0
& & flash_read_1s_sector ( addr , FLASH_SECTOR_SIZE / 8 )
& & flash_erase_sector ( addr ) )
return 1 ;
// Program half-sector
return flash_program_section_phrases ( addr , FLASH_SECTOR_SIZE / 16 ) ;
2015-04-05 06:21:11 +00:00
# endif
}
2015-05-26 00:23:18 +00:00
int flash_prepare_reading ( )
2015-04-05 06:21:11 +00:00
{
return ( 0 ) ;
}
2015-05-26 00:23:18 +00:00
int flash_read_sector ( uintptr_t addr , size_t len )
2015-04-05 06:21:11 +00:00
{
return ( 0 ) ;
2014-08-15 17:53:43 +00:00
}
2015-05-26 00:23:18 +00:00
void * flash_get_staging_area ( uintptr_t addr , size_t len )
2014-08-15 17:53:43 +00:00
{
2015-05-30 07:02:22 +00:00
if ( ( addr & ( USB_DFU_TRANSFER_SIZE - 1 ) ) ! = 0 | | len ! = USB_DFU_TRANSFER_SIZE )
2015-03-09 01:40:01 +00:00
return ( NULL ) ;
return ( FlexRAM ) ;
2014-08-15 17:53:43 +00:00
}