/* Copy memory buffer to a sector in flash  */
static int kinetis_flash_program_sector(u32 sect, u32 sect_offset, const u_char *src, u32 size)
/* Copy memory buffer to a sector in flash  */
static int kinetis_flash_program_sector(u32 sect, u32 sect_offset, const u_char *src, u32 size)
{
	int rv;
	if (sect_offset > 0) {
		memcpy(FLASH_PROG_ACCEL_PTR,
		       KINETIS_NVM_PTR + sect * FLASH_SECTOR_SIZE,
		       sect_offset);
	}
	if (sect_offset + size < FLASH_SECTOR_SIZE) {
		memcpy(FLASH_PROG_ACCEL_PTR + sect_offset + size,
		       KINETIS_NVM_PTR + sect * FLASH_SECTOR_SIZE +
		       sect_offset + size,
		       FLASH_SECTOR_SIZE - (sect_offset + size));
	}
	memcpy(FLASH_PROG_ACCEL_PTR + sect_offset, src, size);
	rv = kinetis_ftfl_command(FTFL_CMD_PROGRAM_SECTION,
				  sect * FLASH_SECTOR_SIZE,
				  FLASH_SECTOR_SIZE << (16 - KINETIS_ENVM_SIZE_UNIT_BITS), 0);
	return rv;
}