|
| 1 | +/* mcxw.c |
| 2 | + * |
| 3 | + * Stubs for custom HAL implementation. Defines the |
| 4 | + * functions used by wolfboot for a specific target. |
| 5 | + * |
| 6 | + * Copyright (C) 2025 wolfSSL Inc. |
| 7 | + * |
| 8 | + * This file is part of wolfBoot. |
| 9 | + * |
| 10 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * wolfBoot is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdint.h> |
| 26 | +#include <target.h> |
| 27 | +#include "image.h" |
| 28 | +/* FSL includes */ |
| 29 | +#include "fsl_common.h" |
| 30 | + |
| 31 | +/* Clock + RAM voltage settings */ |
| 32 | +#include "fsl_clock.h" |
| 33 | +#include "fsl_spc.h" |
| 34 | + |
| 35 | +/* Flash driver */ |
| 36 | +#include "fsl_device_registers.h" |
| 37 | +#include "fsl_lpspi_flash.h" |
| 38 | +#include "fsl_k4_flash.h" |
| 39 | + |
| 40 | +/*!< Core clock frequency: 48000000Hz */ |
| 41 | +#define BOARD_BOOTCLOCKRUN_CORE_CLOCK 48000000U |
| 42 | +static flash_config_t pflash; |
| 43 | +static uint32_t pflash_sector_size = WOLFBOOT_SECTOR_SIZE; |
| 44 | + |
| 45 | +uint32_t SystemCoreClock; |
| 46 | + |
| 47 | + |
| 48 | +#ifdef __WOLFBOOT |
| 49 | + |
| 50 | +extern void BOARD_BootClockRUN(void); |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +/* Assert hook needed by Kinetis SDK */ |
| 55 | +void __assert_func(const char *a, int b, const char *c, const char *d) |
| 56 | +{ |
| 57 | + while(1) |
| 58 | + ; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +void hal_prepare_boot(void) |
| 63 | +{ |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +#endif |
| 68 | + |
| 69 | +void hal_init(void) |
| 70 | +{ |
| 71 | +#ifdef __WOLFBOOT |
| 72 | + /* Clock setting */ |
| 73 | + BOARD_BootClockRUN(); |
| 74 | +#endif |
| 75 | + |
| 76 | + /* Flash driver init */ |
| 77 | + flash_config_t pflash; |
| 78 | + /* Clear the FLASH configuration structure */ |
| 79 | + memset(&pflash, 0, sizeof(pflash)); |
| 80 | + /* FLASH driver init */ |
| 81 | + FLASH_Init(&pflash); |
| 82 | + FLASH_GetProperty(&pflash, kFLASH_PropertyPflash0SectorSize, |
| 83 | + &pflash_sector_size); |
| 84 | +} |
| 85 | + |
| 86 | +int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) |
| 87 | +{ |
| 88 | + int ret; |
| 89 | + int w = 0; |
| 90 | + const uint8_t empty_qword[16] = { |
| 91 | + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 92 | + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF |
| 93 | + }; |
| 94 | + |
| 95 | + while (len > 0) { |
| 96 | + if ((len < 16) || address & 0x0F) { |
| 97 | + uint8_t aligned_qword[16]; |
| 98 | + uint32_t address_align = address - (address & 0x0F); |
| 99 | + uint32_t start_off = address - address_align; |
| 100 | + int i; |
| 101 | + |
| 102 | + memcpy(aligned_qword, (void*)address_align, 16); |
| 103 | + for (i = start_off; ((i < 16) && (i < len + (int)start_off)); i++) { |
| 104 | + aligned_qword[i] = data[w++]; |
| 105 | + } |
| 106 | + if (memcmp(aligned_qword, empty_qword, 16) != 0) { |
| 107 | + ret = FLASH_Program(&pflash, FLASH, address_align, aligned_qword, 16); |
| 108 | + if (ret != kStatus_Success) |
| 109 | + return -1; |
| 110 | + } |
| 111 | + address += i; |
| 112 | + len -= i; |
| 113 | + } |
| 114 | + else { |
| 115 | + uint32_t len_align = len - (len & 0x0F); |
| 116 | + ret = FLASH_Program(&pflash, FLASH, address, (uint8_t*)data + w, len_align); |
| 117 | + if (ret != kStatus_Success) |
| 118 | + return -1; |
| 119 | + len -= len_align; |
| 120 | + address += len_align; |
| 121 | + } |
| 122 | + } |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +void RAMFUNCTION hal_flash_unlock(void) |
| 127 | +{ |
| 128 | +} |
| 129 | + |
| 130 | +void RAMFUNCTION hal_flash_lock(void) |
| 131 | +{ |
| 132 | +} |
| 133 | + |
| 134 | +int RAMFUNCTION hal_flash_erase(uint32_t address, int len) |
| 135 | +{ |
| 136 | + status_t result; |
| 137 | + if (address % pflash_sector_size) |
| 138 | + address -= address % pflash_sector_size; |
| 139 | + while (len > 0) { |
| 140 | + result = FLASH_Erase(&pflash, FLASH, address, pflash_sector_size, |
| 141 | + kFLASH_ApiEraseKey); |
| 142 | + if (kStatus_FLASH_Success != result) |
| 143 | + return -1; |
| 144 | + |
| 145 | + /* Verify sector if it's been erased. */ |
| 146 | + result = FLASH_VerifyEraseSector(&pflash, FLASH, address, |
| 147 | + pflash_sector_size); |
| 148 | + if (kStatus_FLASH_Success != result) |
| 149 | + return -1; |
| 150 | + len -= pflash_sector_size; |
| 151 | + } |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
0 commit comments