|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Piotr Dymacz |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <zephyr/drivers/flash.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#include <driverlib/flash.h> |
| 12 | +#include <driverlib/vims.h> |
| 13 | + |
| 14 | +#define DT_DRV_COMPAT ti_cc13xx_cc26xx_flash_controller |
| 15 | +#define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash) |
| 16 | + |
| 17 | +#define FLASH_ADDR DT_REG_ADDR(SOC_NV_FLASH_NODE) |
| 18 | +#define FLASH_SIZE DT_REG_SIZE(SOC_NV_FLASH_NODE) |
| 19 | +#define FLASH_ERASE_SIZE DT_PROP(SOC_NV_FLASH_NODE, erase_block_size) |
| 20 | +#define FLASH_WRITE_SIZE DT_PROP(SOC_NV_FLASH_NODE, write_block_size) |
| 21 | + |
| 22 | + |
| 23 | +struct flash_priv { |
| 24 | + struct k_sem mutex; |
| 25 | +}; |
| 26 | + |
| 27 | +static const struct flash_parameters flash_cc13xx_cc26xx_parameters = { |
| 28 | + .write_block_size = FLASH_WRITE_SIZE, |
| 29 | + .erase_value = 0xff, |
| 30 | +}; |
| 31 | + |
| 32 | + |
| 33 | +static int flash_cc13xx_cc26xx_init(const struct device *dev) |
| 34 | +{ |
| 35 | + struct flash_priv *priv = dev->data; |
| 36 | + |
| 37 | + k_sem_init(&priv->mutex, 1, 1); |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static void flash_cc13xx_cc26xx_cache_restore(uint32_t vims_mode) |
| 43 | +{ |
| 44 | + while (VIMSModeGet(VIMS_BASE) == VIMS_MODE_CHANGING) |
| 45 | + ; |
| 46 | + |
| 47 | + /* Restore VIMS mode and line buffers */ |
| 48 | + if (vims_mode != VIMS_MODE_DISABLED) { |
| 49 | + VIMSModeSafeSet(VIMS_BASE, vims_mode, true); |
| 50 | + } |
| 51 | + |
| 52 | + VIMSLineBufEnable(VIMS_BASE); |
| 53 | +} |
| 54 | + |
| 55 | +static uint32_t flash_cc13xx_cc26xx_cache_disable(void) |
| 56 | +{ |
| 57 | + uint32_t vims_mode; |
| 58 | + |
| 59 | + /* VIMS and both line buffers should be off during flash update */ |
| 60 | + VIMSLineBufDisable(VIMS_BASE); |
| 61 | + |
| 62 | + while (VIMSModeGet(VIMS_BASE) == VIMS_MODE_CHANGING) |
| 63 | + ; |
| 64 | + |
| 65 | + /* Save current VIMS mode for restoring it later */ |
| 66 | + vims_mode = VIMSModeGet(VIMS_BASE); |
| 67 | + if (vims_mode != VIMS_MODE_DISABLED) { |
| 68 | + VIMSModeSafeSet(VIMS_BASE, VIMS_MODE_DISABLED, true); |
| 69 | + } |
| 70 | + |
| 71 | + return vims_mode; |
| 72 | +} |
| 73 | + |
| 74 | +static bool flash_cc13xx_cc26xx_range_protected(off_t offs, size_t size) |
| 75 | +{ |
| 76 | + off_t sector, end; |
| 77 | + |
| 78 | + sector = (offs / FLASH_ERASE_SIZE) * FLASH_ERASE_SIZE; |
| 79 | + end = offs + size; |
| 80 | + |
| 81 | + /* |
| 82 | + * From TI's HAL 'driverlib/flash.h': |
| 83 | + * |
| 84 | + * After write protecting a sector this sector can only be set back |
| 85 | + * to unprotected by a device reset. |
| 86 | + * |
| 87 | + * Return early if any of sectors from requested range is protected. |
| 88 | + */ |
| 89 | + do { |
| 90 | + if (FlashProtectionGet(sector) == FLASH_WRITE_PROTECT) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + sector += FLASH_ERASE_SIZE; |
| 95 | + } while (sector < end); |
| 96 | + |
| 97 | + return false; |
| 98 | +} |
| 99 | + |
| 100 | +static int flash_cc13xx_cc26xx_erase(const struct device *dev, off_t offs, |
| 101 | + size_t size) |
| 102 | +{ |
| 103 | + struct flash_priv *priv = dev->data; |
| 104 | + uint32_t vims_mode; |
| 105 | + unsigned int key; |
| 106 | + int i, rc = 0; |
| 107 | + size_t cnt; |
| 108 | + |
| 109 | + if (!size) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + |
| 113 | + /* Offset and length should be multiple of erase size */ |
| 114 | + if (((offs % FLASH_ERASE_SIZE) != 0) || |
| 115 | + ((size % FLASH_ERASE_SIZE) != 0)) { |
| 116 | + return -EINVAL; |
| 117 | + } |
| 118 | + |
| 119 | + if (flash_cc13xx_cc26xx_range_protected(offs, size)) { |
| 120 | + return -EINVAL; |
| 121 | + } |
| 122 | + |
| 123 | + if (k_sem_take(&priv->mutex, K_FOREVER)) { |
| 124 | + return -EACCES; |
| 125 | + } |
| 126 | + |
| 127 | + vims_mode = flash_cc13xx_cc26xx_cache_disable(); |
| 128 | + |
| 129 | + /* |
| 130 | + * Disable all interrupts to prevent flash read, from TI's TRF: |
| 131 | + * |
| 132 | + * During a FLASH memory write or erase operation, the FLASH memory |
| 133 | + * must not be read. |
| 134 | + */ |
| 135 | + key = irq_lock(); |
| 136 | + |
| 137 | + /* Erase sector/page one by one, break out in case of an error */ |
| 138 | + cnt = size / FLASH_ERASE_SIZE; |
| 139 | + for (i = 0; i < cnt; i++, offs += FLASH_ERASE_SIZE) { |
| 140 | + while (FlashCheckFsmForReady() != FAPI_STATUS_FSM_READY) |
| 141 | + ; |
| 142 | + |
| 143 | + rc = FlashSectorErase(offs); |
| 144 | + if (rc != FAPI_STATUS_SUCCESS) { |
| 145 | + rc = -EIO; |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + irq_unlock(key); |
| 151 | + |
| 152 | + flash_cc13xx_cc26xx_cache_restore(vims_mode); |
| 153 | + |
| 154 | + k_sem_give(&priv->mutex); |
| 155 | + |
| 156 | + return rc; |
| 157 | +} |
| 158 | + |
| 159 | +static int flash_cc13xx_cc26xx_write(const struct device *dev, off_t offs, |
| 160 | + const void *data, size_t size) |
| 161 | +{ |
| 162 | + struct flash_priv *priv = dev->data; |
| 163 | + uint32_t vims_mode; |
| 164 | + unsigned int key; |
| 165 | + int rc = 0; |
| 166 | + |
| 167 | + if (!size) { |
| 168 | + return 0; |
| 169 | + } |
| 170 | + |
| 171 | + if ((offs < 0) || (size < 1)) { |
| 172 | + return -EINVAL; |
| 173 | + } |
| 174 | + |
| 175 | + if ((offs + size) > FLASH_SIZE) { |
| 176 | + return -EINVAL; |
| 177 | + } |
| 178 | + |
| 179 | + /* |
| 180 | + * From TI's HAL 'driverlib/flash.h': |
| 181 | + * |
| 182 | + * The pui8DataBuffer pointer can not point to flash. |
| 183 | + */ |
| 184 | + if ((data >= (void *)FLASH_ADDR) && |
| 185 | + (data <= (void *)(FLASH_ADDR + FLASH_SIZE))) { |
| 186 | + return -EINVAL; |
| 187 | + } |
| 188 | + |
| 189 | + if (flash_cc13xx_cc26xx_range_protected(offs, size)) { |
| 190 | + return -EINVAL; |
| 191 | + } |
| 192 | + |
| 193 | + if (k_sem_take(&priv->mutex, K_FOREVER)) { |
| 194 | + return -EACCES; |
| 195 | + } |
| 196 | + |
| 197 | + vims_mode = flash_cc13xx_cc26xx_cache_disable(); |
| 198 | + |
| 199 | + key = irq_lock(); |
| 200 | + |
| 201 | + while (FlashCheckFsmForReady() != FAPI_STATUS_FSM_READY) |
| 202 | + ; |
| 203 | + rc = FlashProgram((uint8_t *)data, offs, size); |
| 204 | + if (rc != FAPI_STATUS_SUCCESS) { |
| 205 | + rc = -EIO; |
| 206 | + } |
| 207 | + |
| 208 | + irq_unlock(key); |
| 209 | + |
| 210 | + flash_cc13xx_cc26xx_cache_restore(vims_mode); |
| 211 | + |
| 212 | + k_sem_give(&priv->mutex); |
| 213 | + |
| 214 | + return rc; |
| 215 | +} |
| 216 | + |
| 217 | +static int flash_cc13xx_cc26xx_read(const struct device *dev, off_t offs, |
| 218 | + void *data, size_t size) |
| 219 | +{ |
| 220 | + ARG_UNUSED(dev); |
| 221 | + |
| 222 | + if (!size) { |
| 223 | + return 0; |
| 224 | + } |
| 225 | + |
| 226 | + if ((offs < 0) || (size < 1)) { |
| 227 | + return -EINVAL; |
| 228 | + } |
| 229 | + |
| 230 | + if ((offs + size) > FLASH_SIZE) { |
| 231 | + return -EINVAL; |
| 232 | + } |
| 233 | + |
| 234 | + memcpy(data, (void *)offs, size); |
| 235 | + |
| 236 | + return 0; |
| 237 | +} |
| 238 | + |
| 239 | +static const struct flash_parameters * |
| 240 | +flash_cc13xx_cc26xx_get_parameters(const struct device *dev) |
| 241 | +{ |
| 242 | + ARG_UNUSED(dev); |
| 243 | + |
| 244 | + return &flash_cc13xx_cc26xx_parameters; |
| 245 | +} |
| 246 | + |
| 247 | +#if defined(CONFIG_FLASH_PAGE_LAYOUT) |
| 248 | +static const struct flash_pages_layout dev_layout = { |
| 249 | + .pages_count = FLASH_SIZE / FLASH_ERASE_SIZE, |
| 250 | + .pages_size = FLASH_ERASE_SIZE, |
| 251 | +}; |
| 252 | + |
| 253 | +static void flash_cc13xx_cc26xx_layout(const struct device *dev, |
| 254 | + const struct flash_pages_layout **layout, |
| 255 | + size_t *layout_size) |
| 256 | +{ |
| 257 | + *layout = &dev_layout; |
| 258 | + *layout_size = 1; |
| 259 | +} |
| 260 | +#endif /* CONFIG_FLASH_PAGE_LAYOUT */ |
| 261 | + |
| 262 | +static const struct flash_driver_api flash_cc13xx_cc26xx_api = { |
| 263 | + .erase = flash_cc13xx_cc26xx_erase, |
| 264 | + .write = flash_cc13xx_cc26xx_write, |
| 265 | + .read = flash_cc13xx_cc26xx_read, |
| 266 | + .get_parameters = flash_cc13xx_cc26xx_get_parameters, |
| 267 | +#if defined(CONFIG_FLASH_PAGE_LAYOUT) |
| 268 | + .page_layout = flash_cc13xx_cc26xx_layout, |
| 269 | +#endif |
| 270 | +}; |
| 271 | + |
| 272 | +static struct flash_priv flash_data; |
| 273 | + |
| 274 | +DEVICE_DT_INST_DEFINE(0, flash_cc13xx_cc26xx_init, NULL, &flash_data, NULL, |
| 275 | + POST_KERNEL, CONFIG_FLASH_INIT_PRIORITY, |
| 276 | + &flash_cc13xx_cc26xx_api); |
0 commit comments