| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2025 Damien P. George |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
| 10 |
* in the Software without restriction, including without limitation the rights |
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
* THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdbool.h> |
| 28 |
#include "boardctrl.h" |
| 29 |
#include "qspi.h" |
| 30 |
|
| 31 |
#if BUILDING_MBOOT |
| 32 |
#include "mboot/mboot.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
// Use PYBD_SF2 as base configuration. |
| 36 |
#include "boards/PYBD_SF2/board_init.c" |
| 37 |
|
| 38 |
// Adesto AT25SF161 16-MBit. |
| 39 |
static const mp_spiflash_chip_params_t chip_params_at25sf161 = { |
| 40 |
.jedec_id = 0x01861f, |
| 41 |
.memory_size_bytes_log2 = 21, |
| 42 |
.qspi_prescaler = 3, // maximum frequency 104MHz |
| 43 |
.qread_num_dummy = 2, |
| 44 |
}; |
| 45 |
|
| 46 |
// Infineon S25FL064 64-MBit. |
| 47 |
static const mp_spiflash_chip_params_t chip_params_s25fl064 = { |
| 48 |
.jedec_id = 0x176001, |
| 49 |
.memory_size_bytes_log2 = 23, |
| 50 |
.qspi_prescaler = 2, // maximum frequency 108MHz |
| 51 |
.qread_num_dummy = 4, |
| 52 |
}; |
| 53 |
|
| 54 |
// Selection of possible SPI flash chips. |
| 55 |
static const mp_spiflash_chip_params_t *const chip_params_table[] = { |
| 56 |
&chip_params_at25sf161, |
| 57 |
&chip_params_s25fl064, |
| 58 |
}; |
| 59 |
|
| 60 |
void board_early_init_sf6(void) { |
| 61 |
// Initialise default SPI flash parameters. |
| 62 |
MICROPY_BOARD_SPIFLASH_CHIP_PARAMS0 = &chip_params_at25sf161; |
| 63 |
MICROPY_BOARD_SPIFLASH_CHIP_PARAMS1 = &chip_params_at25sf161; |
| 64 |
|
| 65 |
// Continue with standard board early init. |
| 66 |
board_early_init(); |
| 67 |
} |
| 68 |
|
| 69 |
int mp_spiflash_detect(mp_spiflash_t *spiflash, int ret, uint32_t devid) { |
| 70 |
if (ret != 0) { |
| 71 |
// Could not identify flash. Succeed anyway using default chip parameters. |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
// Try to detect the SPI flash based on the JEDEC id. |
| 76 |
for (size_t i = 0; i < MP_ARRAY_SIZE(chip_params_table); ++i) { |
| 77 |
if (devid == chip_params_table[i]->jedec_id) { |
| 78 |
spiflash->chip_params = chip_params_table[i]; |
| 79 |
if (spiflash->config->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 80 |
// Reinitialise the QSPI but to set new size, prescaler and dummy bytes. |
| 81 |
uint8_t num_dummy = spiflash->chip_params->qread_num_dummy; |
| 82 |
spiflash->config->bus.u_qspi.proto->ioctl(spiflash->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT, num_dummy); |
| 83 |
} |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |