Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 6 | * Copyright (c) 2016-2018 Damien P. George |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 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 <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "py/mperrno.h" |
| 31 | #include "py/mphal.h" |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 32 | #include "drivers/memory/spiflash.h" |
| 33 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 34 | #define QSPI_QE_MASK (0x02) |
| 35 | #define USE_WR_DELAY (1) |
| 36 | |
| 37 | #define CMD_WRSR (0x01) |
| 38 | #define CMD_WRITE (0x02) |
| 39 | #define CMD_READ (0x03) |
| 40 | #define CMD_RDSR (0x05) |
| 41 | #define CMD_WREN (0x06) |
| 42 | #define CMD_SEC_ERASE (0x20) |
| 43 | #define CMD_RDCR (0x35) |
| 44 | #define CMD_RD_DEVID (0x9f) |
| 45 | #define CMD_CHIP_ERASE (0xc7) |
| 46 | #define CMD_C4READ (0xeb) |
| 47 | |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 48 | #define WAIT_SR_TIMEOUT (1000000) |
| 49 | |
| 50 | #define PAGE_SIZE (256) // maximum bytes we can write in one SPI transfer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 51 | #define SECTOR_SIZE MP_SPIFLASH_ERASE_BLOCK_SIZE |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 52 | |
| 53 | STATIC void mp_spiflash_acquire_bus(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 54 | const mp_spiflash_config_t *c = self->config; |
| 55 | if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 56 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE); |
| 57 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | STATIC void mp_spiflash_release_bus(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 61 | const mp_spiflash_config_t *c = self->config; |
| 62 | if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 63 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE); |
| 64 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 65 | } |
| 66 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 67 | STATIC void mp_spiflash_write_cmd_data(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t data) { |
| 68 | const mp_spiflash_config_t *c = self->config; |
| 69 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 70 | // Note: len/data are unused for standard SPI |
| 71 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 72 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL); |
| 73 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 74 | } else { |
| 75 | c->bus.u_qspi.proto->write_cmd_data(c->bus.u_qspi.data, cmd, len, data); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | STATIC void mp_spiflash_write_cmd_addr_data(mp_spiflash_t *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src) { |
| 80 | const mp_spiflash_config_t *c = self->config; |
| 81 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 82 | uint8_t buf[4] = {cmd, addr >> 16, addr >> 8, addr}; |
| 83 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 84 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 4, buf, NULL); |
| 85 | if (len) { |
| 86 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, src, NULL); |
| 87 | } |
| 88 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 89 | } else { |
| 90 | c->bus.u_qspi.proto->write_cmd_addr_data(c->bus.u_qspi.data, cmd, addr, len, src); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | STATIC uint32_t mp_spiflash_read_cmd(mp_spiflash_t *self, uint8_t cmd, size_t len) { |
| 95 | const mp_spiflash_config_t *c = self->config; |
| 96 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 97 | uint32_t buf; |
| 98 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 99 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL); |
| 100 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, (void*)&buf, (void*)&buf); |
| 101 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 102 | return buf; |
| 103 | } else { |
| 104 | return c->bus.u_qspi.proto->read_cmd(c->bus.u_qspi.data, cmd, len); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | STATIC void mp_spiflash_read_data(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
| 109 | const mp_spiflash_config_t *c = self->config; |
| 110 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 111 | uint8_t buf[4] = {CMD_READ, addr >> 16, addr >> 8, addr}; |
| 112 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 113 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 4, buf, NULL); |
| 114 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, dest, dest); |
| 115 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 116 | } else { |
| 117 | c->bus.u_qspi.proto->read_cmd_qaddr_qdata(c->bus.u_qspi.data, CMD_C4READ, addr, len, dest); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | STATIC void mp_spiflash_write_cmd(mp_spiflash_t *self, uint8_t cmd) { |
| 122 | mp_spiflash_write_cmd_data(self, cmd, 0, 0); |
| 123 | } |
| 124 | |
| 125 | STATIC void mp_spiflash_write_cmd_addr(mp_spiflash_t *self, uint8_t cmd, uint32_t addr) { |
| 126 | mp_spiflash_write_cmd_addr_data(self, cmd, addr, 0, NULL); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | STATIC int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 130 | uint8_t sr; |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 131 | do { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 132 | sr = mp_spiflash_read_cmd(self, CMD_RDSR, 1); |
| 133 | if ((sr & mask) == val) { |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 134 | return 0; // success |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 135 | } |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 136 | } while (timeout--); |
| 137 | |
| 138 | return -MP_ETIMEDOUT; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | STATIC int mp_spiflash_wait_wel1(mp_spiflash_t *self) { |
| 142 | return mp_spiflash_wait_sr(self, 2, 2, WAIT_SR_TIMEOUT); |
| 143 | } |
| 144 | |
| 145 | STATIC int mp_spiflash_wait_wip0(mp_spiflash_t *self) { |
| 146 | return mp_spiflash_wait_sr(self, 1, 0, WAIT_SR_TIMEOUT); |
| 147 | } |
| 148 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame^] | 149 | static inline void mp_spiflash_deepsleep_internal(mp_spiflash_t *self, int value) { |
| 150 | mp_spiflash_write_cmd(self, value ? 0xb9 : 0xab); // sleep/wake |
| 151 | } |
| 152 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 153 | void mp_spiflash_init(mp_spiflash_t *self) { |
| 154 | self->flags = 0; |
| 155 | |
| 156 | if (self->config->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 157 | mp_hal_pin_write(self->config->bus.u_spi.cs, 1); |
| 158 | mp_hal_pin_output(self->config->bus.u_spi.cs); |
Damien George | a739b35 | 2018-03-09 17:32:28 +1100 | [diff] [blame] | 159 | self->config->bus.u_spi.proto->ioctl(self->config->bus.u_spi.data, MP_SPI_IOCTL_INIT); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 160 | } else { |
| 161 | self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT); |
| 162 | } |
| 163 | |
| 164 | mp_spiflash_acquire_bus(self); |
| 165 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame^] | 166 | // Ensure SPI flash is out of sleep mode |
| 167 | mp_spiflash_deepsleep_internal(self, 0); |
| 168 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 169 | #if defined(CHECK_DEVID) |
| 170 | // Validate device id |
| 171 | uint32_t devid = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3); |
| 172 | if (devid != CHECK_DEVID) { |
| 173 | return 0; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 178 | // Set QE bit |
| 179 | uint32_t data = (mp_spiflash_read_cmd(self, CMD_RDSR, 1) & 0xff) |
| 180 | | (mp_spiflash_read_cmd(self, CMD_RDCR, 1) & 0xff) << 8; |
Damien George | cc34b08 | 2018-03-11 11:25:38 +1100 | [diff] [blame] | 181 | if (!(data & (QSPI_QE_MASK << 8))) { |
| 182 | data |= QSPI_QE_MASK << 8; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 183 | mp_spiflash_write_cmd(self, CMD_WREN); |
| 184 | mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data); |
| 185 | mp_spiflash_wait_wip0(self); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | mp_spiflash_release_bus(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 190 | } |
| 191 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame^] | 192 | void mp_spiflash_deepsleep(mp_spiflash_t *self, int value) { |
| 193 | if (value) { |
| 194 | mp_spiflash_acquire_bus(self); |
| 195 | } |
| 196 | mp_spiflash_deepsleep_internal(self, value); |
| 197 | if (!value) { |
| 198 | mp_spiflash_release_bus(self); |
| 199 | } |
| 200 | } |
| 201 | |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 202 | STATIC int mp_spiflash_erase_block_internal(mp_spiflash_t *self, uint32_t addr) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 203 | // enable writes |
| 204 | mp_spiflash_write_cmd(self, CMD_WREN); |
| 205 | |
| 206 | // wait WEL=1 |
| 207 | int ret = mp_spiflash_wait_wel1(self); |
| 208 | if (ret != 0) { |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | // erase the sector |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 213 | mp_spiflash_write_cmd_addr(self, CMD_SEC_ERASE, addr); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 214 | |
| 215 | // wait WIP=0 |
| 216 | return mp_spiflash_wait_wip0(self); |
| 217 | } |
| 218 | |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 219 | STATIC int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 220 | // enable writes |
| 221 | mp_spiflash_write_cmd(self, CMD_WREN); |
| 222 | |
| 223 | // wait WEL=1 |
| 224 | int ret = mp_spiflash_wait_wel1(self); |
| 225 | if (ret != 0) { |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | // write the page |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 230 | mp_spiflash_write_cmd_addr_data(self, CMD_WRITE, addr, len, src); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 231 | |
| 232 | // wait WIP=0 |
| 233 | return mp_spiflash_wait_wip0(self); |
| 234 | } |
| 235 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 236 | /******************************************************************************/ |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 237 | // Interface functions that go direct to the SPI flash device |
| 238 | |
| 239 | int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) { |
| 240 | mp_spiflash_acquire_bus(self); |
| 241 | int ret = mp_spiflash_erase_block_internal(self, addr); |
| 242 | mp_spiflash_release_bus(self); |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | void mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
| 247 | if (len == 0) { |
| 248 | return; |
| 249 | } |
| 250 | mp_spiflash_acquire_bus(self); |
| 251 | mp_spiflash_read_data(self, addr, len, dest); |
| 252 | mp_spiflash_release_bus(self); |
| 253 | } |
| 254 | |
| 255 | int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
| 256 | mp_spiflash_acquire_bus(self); |
| 257 | int ret = 0; |
| 258 | uint32_t offset = addr & (PAGE_SIZE - 1); |
| 259 | while (len) { |
| 260 | size_t rest = PAGE_SIZE - offset; |
| 261 | if (rest > len) { |
| 262 | rest = len; |
| 263 | } |
| 264 | ret = mp_spiflash_write_page(self, addr, rest, src); |
| 265 | if (ret != 0) { |
| 266 | break; |
| 267 | } |
| 268 | len -= rest; |
| 269 | addr += rest; |
| 270 | src += rest; |
| 271 | offset = 0; |
| 272 | } |
| 273 | mp_spiflash_release_bus(self); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | /******************************************************************************/ |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 278 | // Interface functions that use the cache |
| 279 | |
| 280 | void mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 281 | if (len == 0) { |
| 282 | return; |
| 283 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 284 | mp_spiflash_acquire_bus(self); |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 285 | mp_spiflash_cache_t *cache = self->config->cache; |
| 286 | if (cache->user == self && cache->block != 0xffffffff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 287 | uint32_t bis = addr / SECTOR_SIZE; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 288 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 289 | if (bis <= cache->block && cache->block <= bie) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 290 | // Read straddles current buffer |
| 291 | size_t rest = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 292 | if (bis < cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 293 | // Read direct from flash for first part |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 294 | rest = cache->block * SECTOR_SIZE - addr; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 295 | mp_spiflash_read_data(self, addr, rest, dest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 296 | len -= rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 297 | dest += rest; |
| 298 | addr += rest; |
| 299 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 300 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 301 | rest = SECTOR_SIZE - offset; |
| 302 | if (rest > len) { |
| 303 | rest = len; |
| 304 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 305 | memcpy(dest, &cache->buf[offset], rest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 306 | len -= rest; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 307 | if (len == 0) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 308 | mp_spiflash_release_bus(self); |
| 309 | return; |
| 310 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 311 | dest += rest; |
| 312 | addr += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 313 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 314 | } |
| 315 | // Read rest direct from flash |
| 316 | mp_spiflash_read_data(self, addr, len, dest); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 317 | mp_spiflash_release_bus(self); |
| 318 | } |
| 319 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 320 | STATIC void mp_spiflash_cache_flush_internal(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 321 | #if USE_WR_DELAY |
| 322 | if (!(self->flags & 1)) { |
| 323 | return; |
| 324 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 325 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 326 | self->flags &= ~1; |
| 327 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 328 | mp_spiflash_cache_t *cache = self->config->cache; |
| 329 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 330 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 331 | int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 332 | if (ret != 0) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | // Write |
| 337 | for (int i = 0; i < 16; i += 1) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 338 | uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE; |
| 339 | int ret = mp_spiflash_write_page(self, addr, PAGE_SIZE, cache->buf + i * PAGE_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 340 | if (ret != 0) { |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | #endif |
| 345 | } |
| 346 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 347 | void mp_spiflash_cache_flush(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 348 | mp_spiflash_acquire_bus(self); |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 349 | mp_spiflash_cache_flush_internal(self); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 350 | mp_spiflash_release_bus(self); |
| 351 | } |
| 352 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 353 | STATIC int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 354 | // Align to 4096 sector |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 355 | uint32_t offset = addr & 0xfff; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 356 | uint32_t sec = addr >> 12; |
| 357 | addr = sec << 12; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 358 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 359 | // Restriction for now, so we don't need to erase multiple pages |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 360 | if (offset + len > SECTOR_SIZE) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 361 | printf("mp_spiflash_cached_write_part: len is too large\n"); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 362 | return -MP_EIO; |
| 363 | } |
| 364 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 365 | mp_spiflash_cache_t *cache = self->config->cache; |
| 366 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 367 | // Acquire the sector buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 368 | if (cache->user != self) { |
| 369 | if (cache->user != NULL) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 370 | mp_spiflash_cache_flush(cache->user); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 371 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 372 | cache->user = self; |
| 373 | cache->block = 0xffffffff; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 374 | } |
| 375 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 376 | if (cache->block != sec) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 377 | // Read sector |
| 378 | #if USE_WR_DELAY |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 379 | if (cache->block != 0xffffffff) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 380 | mp_spiflash_cache_flush_internal(self); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 381 | } |
| 382 | #endif |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 383 | mp_spiflash_read_data(self, addr, SECTOR_SIZE, cache->buf); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 384 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 385 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 386 | #if USE_WR_DELAY |
| 387 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 388 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 389 | // Just copy to buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 390 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 391 | // And mark dirty |
| 392 | self->flags |= 1; |
| 393 | |
| 394 | #else |
| 395 | |
| 396 | uint32_t dirty = 0; |
| 397 | for (size_t i = 0; i < len; ++i) { |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 398 | if (cache->buf[offset + i] != src[i]) { |
| 399 | if (cache->buf[offset + i] != 0xff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 400 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 401 | int ret = mp_spiflash_erase_block_internal(self, addr); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 402 | if (ret != 0) { |
| 403 | return ret; |
| 404 | } |
| 405 | dirty = 0xffff; |
| 406 | break; |
| 407 | } else { |
| 408 | dirty |= (1 << ((offset + i) >> 8)); |
| 409 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 413 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 414 | // Copy new block into buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 415 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 416 | |
| 417 | // Write sector in pages of 256 bytes |
| 418 | for (size_t i = 0; i < 16; ++i) { |
| 419 | if (dirty & (1 << i)) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 420 | int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, PAGE_SIZE, cache->buf + i * PAGE_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 421 | if (ret != 0) { |
| 422 | return ret; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | #endif |
| 428 | |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 429 | return 0; // success |
| 430 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 431 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 432 | int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 433 | uint32_t bis = addr / SECTOR_SIZE; |
| 434 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
| 435 | |
| 436 | mp_spiflash_acquire_bus(self); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 437 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 438 | mp_spiflash_cache_t *cache = self->config->cache; |
| 439 | if (cache->user == self && bis <= cache->block && bie >= cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 440 | // Write straddles current buffer |
| 441 | uint32_t pre; |
| 442 | uint32_t offset; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 443 | if (cache->block * SECTOR_SIZE >= addr) { |
| 444 | pre = cache->block * SECTOR_SIZE - addr; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 445 | offset = 0; |
| 446 | } else { |
| 447 | pre = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 448 | offset = addr - cache->block * SECTOR_SIZE; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 449 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 450 | |
| 451 | // Write buffered part first |
| 452 | uint32_t len_in_buf = len - pre; |
| 453 | len = 0; |
| 454 | if (len_in_buf > SECTOR_SIZE - offset) { |
| 455 | len = len_in_buf - (SECTOR_SIZE - offset); |
| 456 | len_in_buf = SECTOR_SIZE - offset; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 457 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 458 | memcpy(&cache->buf[offset], &src[pre], len_in_buf); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 459 | self->flags |= 1; // Mark dirty |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 460 | |
| 461 | // Write part before buffer sector |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 462 | while (pre) { |
| 463 | int rest = pre & (SECTOR_SIZE - 1); |
| 464 | if (rest == 0) { |
| 465 | rest = SECTOR_SIZE; |
| 466 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 467 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 468 | if (ret != 0) { |
| 469 | mp_spiflash_release_bus(self); |
| 470 | return ret; |
| 471 | } |
| 472 | src += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 473 | addr += rest; |
| 474 | pre -= rest; |
| 475 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 476 | src += len_in_buf; |
| 477 | addr += len_in_buf; |
| 478 | |
| 479 | // Fall through to write remaining part |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 480 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 481 | |
| 482 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
| 483 | while (len) { |
| 484 | int rest = SECTOR_SIZE - offset; |
| 485 | if (rest > len) { |
| 486 | rest = len; |
| 487 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 488 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 489 | if (ret != 0) { |
| 490 | mp_spiflash_release_bus(self); |
| 491 | return ret; |
| 492 | } |
| 493 | len -= rest; |
| 494 | addr += rest; |
| 495 | src += rest; |
| 496 | offset = 0; |
| 497 | } |
| 498 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 499 | mp_spiflash_release_bus(self); |
| 500 | return 0; |
| 501 | } |