2016-09-02 20:00:30 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-09-02 20:00:30 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2016-09-02 20:00:30 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-11-09 03:27:18 -05:00
|
|
|
#include "supervisor/internal_flash.h"
|
|
|
|
|
2016-09-02 20:00:30 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-06-20 13:56:05 -04:00
|
|
|
#include "extmod/vfs.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
2016-10-25 17:27:59 -04:00
|
|
|
#include "py/mphal.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2017-06-20 13:56:05 -04:00
|
|
|
#include "lib/oofatfs/ff.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
#include "hpl/pm/hpl_pm_base.h"
|
|
|
|
#endif
|
2020-09-08 11:43:24 -04:00
|
|
|
#ifdef SAME51
|
|
|
|
#include "hri/hri_mclk_e51.h"
|
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAME54
|
|
|
|
#include "hri/hri_mclk_e54.h"
|
|
|
|
#endif
|
|
|
|
#ifdef SAMD51
|
|
|
|
#include "hri/hri_mclk_d51.h"
|
|
|
|
#endif
|
2017-10-10 14:36:00 -04:00
|
|
|
#include "hal/include/hal_flash.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2020-03-17 15:10:35 -04:00
|
|
|
#include "supervisor/flash.h"
|
2017-10-10 14:36:00 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
static struct flash_descriptor supervisor_flash_desc;
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void supervisor_flash_init(void) {
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-10-10 14:36:00 -04:00
|
|
|
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
|
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
|
|
|
_pm_enable_bus_clock(PM_BUS_APBB, NVMCTRL);
|
|
|
|
#endif
|
2018-10-19 21:46:22 -04:00
|
|
|
flash_init(&supervisor_flash_desc, NVMCTRL);
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
uint32_t supervisor_flash_get_block_size(void) {
|
2017-05-12 17:47:39 -04:00
|
|
|
return FILESYSTEM_BLOCK_SIZE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
uint32_t supervisor_flash_get_block_count(void) {
|
2018-11-14 20:59:11 -05:00
|
|
|
return INTERNAL_FLASH_PART1_NUM_BLOCKS;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:10:35 -04:00
|
|
|
void port_internal_flash_flush(void) {
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 21:28:27 -04:00
|
|
|
void supervisor_flash_release_cache(void) {
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
static int32_t convert_block_to_flash_addr(uint32_t block) {
|
2018-11-14 20:59:11 -05:00
|
|
|
if (0 <= block && block < INTERNAL_FLASH_PART1_NUM_BLOCKS) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// a block in partition 1
|
2019-10-28 18:15:02 -04:00
|
|
|
return CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
// bad block
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:47:55 -05:00
|
|
|
STATIC bool supervisor_flash_read_block(uint8_t *dest, uint32_t block) {
|
2018-11-14 20:59:11 -05:00
|
|
|
// non-MBR block, get data from flash memory
|
|
|
|
int32_t src = convert_block_to_flash_addr(block);
|
|
|
|
if (src == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
2018-11-14 20:59:11 -05:00
|
|
|
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
|
|
|
|
return error_code == ERR_NONE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2021-11-10 13:47:55 -05:00
|
|
|
STATIC bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
|
2018-11-14 20:59:11 -05:00
|
|
|
// non-MBR block, copy to cache
|
|
|
|
int32_t dest = convert_block_to_flash_addr(block);
|
|
|
|
if (dest == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int32_t error_code;
|
|
|
|
error_code = flash_erase(&supervisor_flash_desc,
|
2021-04-30 11:47:37 -04:00
|
|
|
dest,
|
|
|
|
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
|
2018-11-14 20:59:11 -05:00
|
|
|
if (error_code != ERR_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2018-11-14 20:59:11 -05:00
|
|
|
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
|
|
|
|
if (error_code != ERR_NONE) {
|
|
|
|
return false;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
2018-11-14 20:59:11 -05:00
|
|
|
return true;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2016-09-02 20:00:30 -04:00
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
2018-10-19 21:46:22 -04:00
|
|
|
if (!supervisor_flash_read_block(dest + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
|
2016-09-02 20:00:30 -04:00
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2016-09-02 20:00:30 -04:00
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
2018-10-19 21:46:22 -04:00
|
|
|
if (!supervisor_flash_write_block(src + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
|
2016-09-02 20:00:30 -04:00
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|