blob: 1ae0bbbc6792a592003837311d5dae11225d546d [file] [log] [blame]
Damien George784e0232017-01-24 16:56:03 +11001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Damien George4e487002018-03-02 16:01:18 +11006 * Copyright (c) 2016-2018 Damien P. George
Damien George784e0232017-01-24 16:56:03 +11007 *
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 George784e0232017-01-24 16:56:03 +110032#include "drivers/memory/spiflash.h"
33
Damien Georgeb0785692025-04-02 12:47:48 +110034#if defined(CHECK_DEVID)
35#error "CHECK_DEVID no longer supported, use MICROPY_HW_SPIFLASH_DETECT_DEVICE instead"
36#endif
37
Damien George2c0240e2025-04-02 12:47:40 +110038// The default number of dummy bytes for quad-read is 2. This can be changed by enabling
39// MICROPY_HW_SPIFLASH_CHIP_PARAMS and configuring the value in mp_spiflash_chip_params_t.
40#if MICROPY_HW_SPIFLASH_CHIP_PARAMS
41#define MICROPY_HW_SPIFLASH_QREAD_NUM_DUMMY(spiflash) (spiflash->chip_params->qread_num_dummy)
42#else
43#define MICROPY_HW_SPIFLASH_QREAD_NUM_DUMMY(spiflash) (2)
44#endif
45
Damien George4e487002018-03-02 16:01:18 +110046#define QSPI_QE_MASK (0x02)
47#define USE_WR_DELAY (1)
48
49#define CMD_WRSR (0x01)
50#define CMD_WRITE (0x02)
51#define CMD_READ (0x03)
52#define CMD_RDSR (0x05)
53#define CMD_WREN (0x06)
54#define CMD_SEC_ERASE (0x20)
55#define CMD_RDCR (0x35)
56#define CMD_RD_DEVID (0x9f)
57#define CMD_CHIP_ERASE (0xc7)
58#define CMD_C4READ (0xeb)
iabdalkaderb5e80fa2024-11-29 16:56:20 +010059#define CMD_RSTEN (0x66)
60#define CMD_RESET (0x99)
Damien George4e487002018-03-02 16:01:18 +110061
Andrew Leech30501d32020-01-28 14:59:05 +110062// 32 bit addressing commands
63#define CMD_WRITE_32 (0x12)
64#define CMD_READ_32 (0x13)
65#define CMD_SEC_ERASE_32 (0x21)
66#define CMD_C4READ_32 (0xec)
67
Damien George784e0232017-01-24 16:56:03 +110068#define WAIT_SR_TIMEOUT (1000000)
69
70#define PAGE_SIZE (256) // maximum bytes we can write in one SPI transfer
Damien George86fe73b2018-06-07 14:09:10 +100071#define SECTOR_SIZE MP_SPIFLASH_ERASE_BLOCK_SIZE
Damien George784e0232017-01-24 16:56:03 +110072
Angus Grattondecf8e62024-02-27 15:32:29 +110073static void mp_spiflash_acquire_bus(mp_spiflash_t *self) {
Damien George4e487002018-03-02 16:01:18 +110074 const mp_spiflash_config_t *c = self->config;
75 if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) {
Damien Georgec61e8592025-03-19 15:47:35 +110076 c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE, 0);
Damien George4e487002018-03-02 16:01:18 +110077 }
Damien George784e0232017-01-24 16:56:03 +110078}
79
Angus Grattondecf8e62024-02-27 15:32:29 +110080static void mp_spiflash_release_bus(mp_spiflash_t *self) {
Damien George4e487002018-03-02 16:01:18 +110081 const mp_spiflash_config_t *c = self->config;
82 if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) {
Damien Georgec61e8592025-03-19 15:47:35 +110083 c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE, 0);
84 }
85}
86
87static void mp_spiflash_notify_modified(mp_spiflash_t *self, uint32_t addr, uint32_t len) {
88 const mp_spiflash_config_t *c = self->config;
89 if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) {
90 uintptr_t arg[2] = { addr, len };
91 c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_MEMORY_MODIFIED, (uintptr_t)&arg[0]);
Damien George4e487002018-03-02 16:01:18 +110092 }
Damien George784e0232017-01-24 16:56:03 +110093}
94
Angus Grattondecf8e62024-02-27 15:32:29 +110095static int mp_spiflash_write_cmd_data(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t data) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +110096 int ret = 0;
Damien George4e487002018-03-02 16:01:18 +110097 const mp_spiflash_config_t *c = self->config;
98 if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
99 // Note: len/data are unused for standard SPI
100 mp_hal_pin_write(c->bus.u_spi.cs, 0);
101 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL);
102 mp_hal_pin_write(c->bus.u_spi.cs, 1);
103 } else {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100104 ret = c->bus.u_qspi.proto->write_cmd_data(c->bus.u_qspi.data, cmd, len, data);
Damien George4e487002018-03-02 16:01:18 +1100105 }
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100106 return ret;
Damien George4e487002018-03-02 16:01:18 +1100107}
108
Angus Grattondecf8e62024-02-27 15:32:29 +1100109static int mp_spiflash_transfer_cmd_addr_data(mp_spiflash_t *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src, uint8_t *dest) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100110 int ret = 0;
Damien George4e487002018-03-02 16:01:18 +1100111 const mp_spiflash_config_t *c = self->config;
112 if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
Andrew Leech30501d32020-01-28 14:59:05 +1100113 uint8_t buf[5] = {cmd, 0};
114 uint8_t buff_len = 1 + mp_spi_set_addr_buff(&buf[1], addr);
Damien George4e487002018-03-02 16:01:18 +1100115 mp_hal_pin_write(c->bus.u_spi.cs, 0);
Andrew Leech30501d32020-01-28 14:59:05 +1100116 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, buff_len, buf, NULL);
117 if (len && (src != NULL)) {
Damien George4e487002018-03-02 16:01:18 +1100118 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, src, NULL);
Andrew Leech30501d32020-01-28 14:59:05 +1100119 } else if (len && (dest != NULL)) {
120 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, dest, dest);
Damien George4e487002018-03-02 16:01:18 +1100121 }
Andrew Leech30501d32020-01-28 14:59:05 +1100122
Damien George4e487002018-03-02 16:01:18 +1100123 mp_hal_pin_write(c->bus.u_spi.cs, 1);
124 } else {
Andrew Leech30501d32020-01-28 14:59:05 +1100125 if (dest != NULL) {
Damien George2c0240e2025-04-02 12:47:40 +1100126 uint8_t num_dummy = MICROPY_HW_SPIFLASH_QREAD_NUM_DUMMY(self);
127 ret = c->bus.u_qspi.proto->read_cmd_qaddr_qdata(c->bus.u_qspi.data, cmd, addr, num_dummy, len, dest);
Andrew Leech30501d32020-01-28 14:59:05 +1100128 } else {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100129 ret = c->bus.u_qspi.proto->write_cmd_addr_data(c->bus.u_qspi.data, cmd, addr, len, src);
Andrew Leech30501d32020-01-28 14:59:05 +1100130 }
Damien George4e487002018-03-02 16:01:18 +1100131 }
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100132 return ret;
Damien George4e487002018-03-02 16:01:18 +1100133}
134
Angus Grattondecf8e62024-02-27 15:32:29 +1100135static int mp_spiflash_read_cmd(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t *dest) {
Damien George4e487002018-03-02 16:01:18 +1100136 const mp_spiflash_config_t *c = self->config;
137 if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
Damien George4e487002018-03-02 16:01:18 +1100138 mp_hal_pin_write(c->bus.u_spi.cs, 0);
139 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL);
Damien Georgeb042fd52022-12-09 12:28:54 +1100140 c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, (void*)dest, (void*)dest);
Damien George4e487002018-03-02 16:01:18 +1100141 mp_hal_pin_write(c->bus.u_spi.cs, 1);
Damien Georgeb042fd52022-12-09 12:28:54 +1100142 return 0;
Damien George4e487002018-03-02 16:01:18 +1100143 } else {
Damien Georgeb042fd52022-12-09 12:28:54 +1100144 return c->bus.u_qspi.proto->read_cmd(c->bus.u_qspi.data, cmd, len, dest);
Damien George4e487002018-03-02 16:01:18 +1100145 }
146}
147
Angus Grattondecf8e62024-02-27 15:32:29 +1100148static int mp_spiflash_read_data(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
Damien George4e487002018-03-02 16:01:18 +1100149 const mp_spiflash_config_t *c = self->config;
Andrew Leech30501d32020-01-28 14:59:05 +1100150 uint8_t cmd;
Damien George4e487002018-03-02 16:01:18 +1100151 if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
Damien George54f16942022-06-01 18:50:43 +1000152 cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_READ_32 : CMD_READ;
Damien George4e487002018-03-02 16:01:18 +1100153 } else {
Damien George54f16942022-06-01 18:50:43 +1000154 cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_C4READ_32 : CMD_C4READ;
Damien George4e487002018-03-02 16:01:18 +1100155 }
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100156 return mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, NULL, dest);
Damien George4e487002018-03-02 16:01:18 +1100157}
158
Angus Grattondecf8e62024-02-27 15:32:29 +1100159static int mp_spiflash_write_cmd(mp_spiflash_t *self, uint8_t cmd) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100160 return mp_spiflash_write_cmd_data(self, cmd, 0, 0);
Damien George4e487002018-03-02 16:01:18 +1100161}
162
Angus Grattondecf8e62024-02-27 15:32:29 +1100163static int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) {
Andrew Leech2ed2ec12019-01-29 15:20:01 +1100164 do {
Damien Georgeb042fd52022-12-09 12:28:54 +1100165 uint32_t sr;
166 int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
167 if (ret != 0) {
168 return ret;
169 }
Damien George4e487002018-03-02 16:01:18 +1100170 if ((sr & mask) == val) {
Andrew Leech2ed2ec12019-01-29 15:20:01 +1100171 return 0; // success
Damien George784e0232017-01-24 16:56:03 +1100172 }
Andrew Leech2ed2ec12019-01-29 15:20:01 +1100173 } while (timeout--);
174
175 return -MP_ETIMEDOUT;
Damien George784e0232017-01-24 16:56:03 +1100176}
177
Angus Grattondecf8e62024-02-27 15:32:29 +1100178static int mp_spiflash_wait_wel1(mp_spiflash_t *self) {
Damien George784e0232017-01-24 16:56:03 +1100179 return mp_spiflash_wait_sr(self, 2, 2, WAIT_SR_TIMEOUT);
180}
181
Angus Grattondecf8e62024-02-27 15:32:29 +1100182static int mp_spiflash_wait_wip0(mp_spiflash_t *self) {
Damien George784e0232017-01-24 16:56:03 +1100183 return mp_spiflash_wait_sr(self, 1, 0, WAIT_SR_TIMEOUT);
184}
185
Damien George8cde5fa2019-07-03 01:03:25 +1000186static inline void mp_spiflash_deepsleep_internal(mp_spiflash_t *self, int value) {
187 mp_spiflash_write_cmd(self, value ? 0xb9 : 0xab); // sleep/wake
188}
189
Damien George4e487002018-03-02 16:01:18 +1100190void mp_spiflash_init(mp_spiflash_t *self) {
191 self->flags = 0;
192
193 if (self->config->bus_kind == MP_SPIFLASH_BUS_SPI) {
194 mp_hal_pin_write(self->config->bus.u_spi.cs, 1);
195 mp_hal_pin_output(self->config->bus.u_spi.cs);
Damien Georgea739b352018-03-09 17:32:28 +1100196 self->config->bus.u_spi.proto->ioctl(self->config->bus.u_spi.data, MP_SPI_IOCTL_INIT);
Damien George4e487002018-03-02 16:01:18 +1100197 } else {
Damien George2c0240e2025-04-02 12:47:40 +1100198 uint8_t num_dummy = MICROPY_HW_SPIFLASH_QREAD_NUM_DUMMY(self);
199 self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT, num_dummy);
Damien George4e487002018-03-02 16:01:18 +1100200 }
201
202 mp_spiflash_acquire_bus(self);
203
Damien George8cde5fa2019-07-03 01:03:25 +1000204 // Ensure SPI flash is out of sleep mode
205 mp_spiflash_deepsleep_internal(self, 0);
206
iabdalkaderb5e80fa2024-11-29 16:56:20 +0100207 // Software reset.
208 #if MICROPY_HW_SPIFLASH_SOFT_RESET
209 mp_spiflash_write_cmd(self, CMD_RSTEN);
210 mp_spiflash_write_cmd(self, CMD_RESET);
211 mp_spiflash_wait_wip0(self);
212 mp_hal_delay_ms(1);
213 #endif
214
Damien Georgeb0785692025-04-02 12:47:48 +1100215 #if MICROPY_HW_SPIFLASH_DETECT_DEVICE
216 // Attempt to detect SPI flash based on its JEDEC id.
Damien Georgeb042fd52022-12-09 12:28:54 +1100217 uint32_t devid;
218 int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid);
Damien Georgeb0785692025-04-02 12:47:48 +1100219 ret = mp_spiflash_detect(self, ret, devid);
220 if (ret != 0) {
221 // Could not read device id.
Damien Georgeb042fd52022-12-09 12:28:54 +1100222 mp_spiflash_release_bus(self);
223 return;
Damien George4e487002018-03-02 16:01:18 +1100224 }
225 #endif
226
227 if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) {
228 // Set QE bit
Damien Georgeb042fd52022-12-09 12:28:54 +1100229 uint32_t sr = 0, cr = 0;
230 int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
231 if (ret == 0) {
232 ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr);
233 }
234 uint32_t data = (sr & 0xff) | (cr & 0xff) << 8;
235 if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) {
Damien Georgecc34b082018-03-11 11:25:38 +1100236 data |= QSPI_QE_MASK << 8;
Damien George4e487002018-03-02 16:01:18 +1100237 mp_spiflash_write_cmd(self, CMD_WREN);
238 mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data);
239 mp_spiflash_wait_wip0(self);
240 }
241 }
242
243 mp_spiflash_release_bus(self);
Damien George784e0232017-01-24 16:56:03 +1100244}
245
Damien George8cde5fa2019-07-03 01:03:25 +1000246void mp_spiflash_deepsleep(mp_spiflash_t *self, int value) {
247 if (value) {
248 mp_spiflash_acquire_bus(self);
249 }
250 mp_spiflash_deepsleep_internal(self, value);
251 if (!value) {
252 mp_spiflash_release_bus(self);
253 }
254}
255
Angus Grattondecf8e62024-02-27 15:32:29 +1100256static int mp_spiflash_erase_block_internal(mp_spiflash_t *self, uint32_t addr) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100257 int ret = 0;
Damien George784e0232017-01-24 16:56:03 +1100258 // enable writes
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100259 ret = mp_spiflash_write_cmd(self, CMD_WREN);
260 if (ret != 0) {
261 return ret;
262 }
Damien George784e0232017-01-24 16:56:03 +1100263
264 // wait WEL=1
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100265 ret = mp_spiflash_wait_wel1(self);
Damien George784e0232017-01-24 16:56:03 +1100266 if (ret != 0) {
267 return ret;
268 }
269
270 // erase the sector
Damien George54f16942022-06-01 18:50:43 +1000271 uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_SEC_ERASE_32 : CMD_SEC_ERASE;
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100272 ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, 0, NULL, NULL);
273 if (ret != 0) {
274 return ret;
275 }
Damien George784e0232017-01-24 16:56:03 +1100276
277 // wait WIP=0
278 return mp_spiflash_wait_wip0(self);
279}
280
Angus Grattondecf8e62024-02-27 15:32:29 +1100281static int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100282 int ret = 0;
Damien George784e0232017-01-24 16:56:03 +1100283 // enable writes
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100284 ret = mp_spiflash_write_cmd(self, CMD_WREN);
285 if (ret != 0) {
286 return ret;
287 }
Damien George784e0232017-01-24 16:56:03 +1100288
289 // wait WEL=1
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100290 ret = mp_spiflash_wait_wel1(self);
Damien George784e0232017-01-24 16:56:03 +1100291 if (ret != 0) {
292 return ret;
293 }
294
295 // write the page
Damien George54f16942022-06-01 18:50:43 +1000296 uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_WRITE_32 : CMD_WRITE;
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100297 ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, src, NULL);
298 if (ret != 0) {
299 return ret;
300 }
Damien George784e0232017-01-24 16:56:03 +1100301
302 // wait WIP=0
303 return mp_spiflash_wait_wip0(self);
304}
305
Damien Georgecc5a9402018-06-07 15:36:27 +1000306/******************************************************************************/
Damien Georgeb78ca322018-06-07 15:39:46 +1000307// Interface functions that go direct to the SPI flash device
308
309int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) {
310 mp_spiflash_acquire_bus(self);
311 int ret = mp_spiflash_erase_block_internal(self, addr);
Damien Georgec61e8592025-03-19 15:47:35 +1100312 mp_spiflash_notify_modified(self, addr, SECTOR_SIZE);
Damien Georgeb78ca322018-06-07 15:39:46 +1000313 mp_spiflash_release_bus(self);
314 return ret;
315}
316
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100317int mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
Damien Georgeb78ca322018-06-07 15:39:46 +1000318 if (len == 0) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100319 return 0;
Damien Georgeb78ca322018-06-07 15:39:46 +1000320 }
321 mp_spiflash_acquire_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100322 int ret = mp_spiflash_read_data(self, addr, len, dest);
Damien Georgeb78ca322018-06-07 15:39:46 +1000323 mp_spiflash_release_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100324 return ret;
Damien Georgeb78ca322018-06-07 15:39:46 +1000325}
326
327int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
Damien Georgec61e8592025-03-19 15:47:35 +1100328 uint32_t orig_addr = addr;
329 uint32_t orig_len = len;
Damien Georgeb78ca322018-06-07 15:39:46 +1000330 mp_spiflash_acquire_bus(self);
331 int ret = 0;
332 uint32_t offset = addr & (PAGE_SIZE - 1);
333 while (len) {
334 size_t rest = PAGE_SIZE - offset;
335 if (rest > len) {
336 rest = len;
337 }
338 ret = mp_spiflash_write_page(self, addr, rest, src);
339 if (ret != 0) {
340 break;
341 }
342 len -= rest;
343 addr += rest;
344 src += rest;
345 offset = 0;
346 }
Damien Georgec61e8592025-03-19 15:47:35 +1100347 mp_spiflash_notify_modified(self, orig_addr, orig_len);
Damien Georgeb78ca322018-06-07 15:39:46 +1000348 mp_spiflash_release_bus(self);
349 return ret;
350}
351
352/******************************************************************************/
Damien Georgecc5a9402018-06-07 15:36:27 +1000353// Interface functions that use the cache
Damien Georgec61e8592025-03-19 15:47:35 +1100354//
355// These functions do not call mp_spiflash_notify_modified(), so shouldn't be
356// used for memory-mapped flash (for example).
Damien Georgecc5a9402018-06-07 15:36:27 +1000357
Damien Georgee43a74a2020-12-17 16:59:54 +1100358#if MICROPY_HW_SPIFLASH_ENABLE_CACHE
359
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100360int mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
Damien George4e487002018-03-02 16:01:18 +1100361 if (len == 0) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100362 return 0;
Damien George4e487002018-03-02 16:01:18 +1100363 }
Damien George784e0232017-01-24 16:56:03 +1100364 mp_spiflash_acquire_bus(self);
Damien George86fe73b2018-06-07 14:09:10 +1000365 mp_spiflash_cache_t *cache = self->config->cache;
366 if (cache->user == self && cache->block != 0xffffffff) {
Damien George4e487002018-03-02 16:01:18 +1100367 uint32_t bis = addr / SECTOR_SIZE;
Damien Georgebdc875e2018-03-13 14:13:30 +1100368 uint32_t bie = (addr + len - 1) / SECTOR_SIZE;
Damien George86fe73b2018-06-07 14:09:10 +1000369 if (bis <= cache->block && cache->block <= bie) {
Damien Georgebdc875e2018-03-13 14:13:30 +1100370 // Read straddles current buffer
371 size_t rest = 0;
Damien George86fe73b2018-06-07 14:09:10 +1000372 if (bis < cache->block) {
Damien Georgebdc875e2018-03-13 14:13:30 +1100373 // Read direct from flash for first part
Damien George86fe73b2018-06-07 14:09:10 +1000374 rest = cache->block * SECTOR_SIZE - addr;
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100375 int ret = mp_spiflash_read_data(self, addr, rest, dest);
376 if (ret != 0) {
377 mp_spiflash_release_bus(self);
378 return ret;
379 }
Damien George4e487002018-03-02 16:01:18 +1100380 len -= rest;
Damien George4e487002018-03-02 16:01:18 +1100381 dest += rest;
382 addr += rest;
383 }
Damien Georgebdc875e2018-03-13 14:13:30 +1100384 uint32_t offset = addr & (SECTOR_SIZE - 1);
Damien George4e487002018-03-02 16:01:18 +1100385 rest = SECTOR_SIZE - offset;
386 if (rest > len) {
387 rest = len;
388 }
Damien George86fe73b2018-06-07 14:09:10 +1000389 memcpy(dest, &cache->buf[offset], rest);
Damien George4e487002018-03-02 16:01:18 +1100390 len -= rest;
Damien Georgebdc875e2018-03-13 14:13:30 +1100391 if (len == 0) {
Damien George4e487002018-03-02 16:01:18 +1100392 mp_spiflash_release_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100393 return 0;
Damien George4e487002018-03-02 16:01:18 +1100394 }
Damien Georgebdc875e2018-03-13 14:13:30 +1100395 dest += rest;
396 addr += rest;
Damien George4e487002018-03-02 16:01:18 +1100397 }
Damien George4e487002018-03-02 16:01:18 +1100398 }
399 // Read rest direct from flash
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100400 int ret = mp_spiflash_read_data(self, addr, len, dest);
Damien George784e0232017-01-24 16:56:03 +1100401 mp_spiflash_release_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100402 return ret;
Damien George784e0232017-01-24 16:56:03 +1100403}
404
Angus Grattondecf8e62024-02-27 15:32:29 +1100405static int mp_spiflash_cache_flush_internal(mp_spiflash_t *self) {
Damien George4e487002018-03-02 16:01:18 +1100406 #if USE_WR_DELAY
407 if (!(self->flags & 1)) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100408 return 0;
Damien George4e487002018-03-02 16:01:18 +1100409 }
Damien George784e0232017-01-24 16:56:03 +1100410
Damien George4e487002018-03-02 16:01:18 +1100411 self->flags &= ~1;
412
Damien George86fe73b2018-06-07 14:09:10 +1000413 mp_spiflash_cache_t *cache = self->config->cache;
414
Damien George4e487002018-03-02 16:01:18 +1100415 // Erase sector
Damien Georgeb78ca322018-06-07 15:39:46 +1000416 int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE);
Damien George4e487002018-03-02 16:01:18 +1100417 if (ret != 0) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100418 return ret;
Damien George4e487002018-03-02 16:01:18 +1100419 }
420
421 // Write
422 for (int i = 0; i < 16; i += 1) {
Damien Georgeb78ca322018-06-07 15:39:46 +1000423 uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE;
424 int ret = mp_spiflash_write_page(self, addr, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
Damien George4e487002018-03-02 16:01:18 +1100425 if (ret != 0) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100426 return ret;
Damien George4e487002018-03-02 16:01:18 +1100427 }
428 }
429 #endif
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100430 return 0;
Damien George4e487002018-03-02 16:01:18 +1100431}
432
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100433int mp_spiflash_cache_flush(mp_spiflash_t *self) {
Damien George4e487002018-03-02 16:01:18 +1100434 mp_spiflash_acquire_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100435 int ret = mp_spiflash_cache_flush_internal(self);
Damien George4e487002018-03-02 16:01:18 +1100436 mp_spiflash_release_bus(self);
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100437 return ret;
Damien George4e487002018-03-02 16:01:18 +1100438}
439
Angus Grattondecf8e62024-02-27 15:32:29 +1100440static int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
Damien George4e487002018-03-02 16:01:18 +1100441 // Align to 4096 sector
Damien George784e0232017-01-24 16:56:03 +1100442 uint32_t offset = addr & 0xfff;
Damien George4e487002018-03-02 16:01:18 +1100443 uint32_t sec = addr >> 12;
444 addr = sec << 12;
Damien George784e0232017-01-24 16:56:03 +1100445
Damien George4e487002018-03-02 16:01:18 +1100446 // Restriction for now, so we don't need to erase multiple pages
Damien George86fe73b2018-06-07 14:09:10 +1000447 if (offset + len > SECTOR_SIZE) {
Damien Georgecc5a9402018-06-07 15:36:27 +1000448 printf("mp_spiflash_cached_write_part: len is too large\n");
Damien George784e0232017-01-24 16:56:03 +1100449 return -MP_EIO;
450 }
451
Damien George86fe73b2018-06-07 14:09:10 +1000452 mp_spiflash_cache_t *cache = self->config->cache;
453
Damien George4e487002018-03-02 16:01:18 +1100454 // Acquire the sector buffer
Damien George86fe73b2018-06-07 14:09:10 +1000455 if (cache->user != self) {
456 if (cache->user != NULL) {
Damien Georgecc5a9402018-06-07 15:36:27 +1000457 mp_spiflash_cache_flush(cache->user);
Damien George4e487002018-03-02 16:01:18 +1100458 }
Damien George86fe73b2018-06-07 14:09:10 +1000459 cache->user = self;
460 cache->block = 0xffffffff;
Damien George784e0232017-01-24 16:56:03 +1100461 }
462
Damien George86fe73b2018-06-07 14:09:10 +1000463 if (cache->block != sec) {
Damien George4e487002018-03-02 16:01:18 +1100464 // Read sector
465 #if USE_WR_DELAY
Damien George86fe73b2018-06-07 14:09:10 +1000466 if (cache->block != 0xffffffff) {
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100467 int ret = mp_spiflash_cache_flush_internal(self);
468 if (ret != 0) {
469 return ret;
470 }
Damien George4e487002018-03-02 16:01:18 +1100471 }
472 #endif
Andrew Leech7ee5afe2021-03-05 10:15:29 +1100473 int ret = mp_spiflash_read_data(self, addr, SECTOR_SIZE, cache->buf);
474 if (ret != 0) {
475 return ret;
476 }
Damien George4e487002018-03-02 16:01:18 +1100477 }
Damien George784e0232017-01-24 16:56:03 +1100478
Damien George4e487002018-03-02 16:01:18 +1100479 #if USE_WR_DELAY
480
Damien George86fe73b2018-06-07 14:09:10 +1000481 cache->block = sec;
Damien George4e487002018-03-02 16:01:18 +1100482 // Just copy to buffer
Damien George86fe73b2018-06-07 14:09:10 +1000483 memcpy(cache->buf + offset, src, len);
Damien George4e487002018-03-02 16:01:18 +1100484 // And mark dirty
485 self->flags |= 1;
486
487 #else
488
489 uint32_t dirty = 0;
490 for (size_t i = 0; i < len; ++i) {
Damien George86fe73b2018-06-07 14:09:10 +1000491 if (cache->buf[offset + i] != src[i]) {
492 if (cache->buf[offset + i] != 0xff) {
Damien George4e487002018-03-02 16:01:18 +1100493 // Erase sector
Damien Georgeb78ca322018-06-07 15:39:46 +1000494 int ret = mp_spiflash_erase_block_internal(self, addr);
Damien George4e487002018-03-02 16:01:18 +1100495 if (ret != 0) {
496 return ret;
497 }
498 dirty = 0xffff;
499 break;
500 } else {
501 dirty |= (1 << ((offset + i) >> 8));
502 }
Damien George784e0232017-01-24 16:56:03 +1100503 }
504 }
505
Damien George86fe73b2018-06-07 14:09:10 +1000506 cache->block = sec;
Damien George4e487002018-03-02 16:01:18 +1100507 // Copy new block into buffer
Damien George86fe73b2018-06-07 14:09:10 +1000508 memcpy(cache->buf + offset, src, len);
Damien George4e487002018-03-02 16:01:18 +1100509
510 // Write sector in pages of 256 bytes
511 for (size_t i = 0; i < 16; ++i) {
512 if (dirty & (1 << i)) {
Damien Georgeb78ca322018-06-07 15:39:46 +1000513 int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
Damien George4e487002018-03-02 16:01:18 +1100514 if (ret != 0) {
515 return ret;
516 }
517 }
518 }
519
520 #endif
521
Damien George784e0232017-01-24 16:56:03 +1100522 return 0; // success
523}
Damien George4e487002018-03-02 16:01:18 +1100524
Damien Georgecc5a9402018-06-07 15:36:27 +1000525int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
Damien George4e487002018-03-02 16:01:18 +1100526 uint32_t bis = addr / SECTOR_SIZE;
527 uint32_t bie = (addr + len - 1) / SECTOR_SIZE;
528
529 mp_spiflash_acquire_bus(self);
Damien Georgebdc875e2018-03-13 14:13:30 +1100530
Damien George86fe73b2018-06-07 14:09:10 +1000531 mp_spiflash_cache_t *cache = self->config->cache;
532 if (cache->user == self && bis <= cache->block && bie >= cache->block) {
Damien Georgebdc875e2018-03-13 14:13:30 +1100533 // Write straddles current buffer
534 uint32_t pre;
535 uint32_t offset;
Damien George86fe73b2018-06-07 14:09:10 +1000536 if (cache->block * SECTOR_SIZE >= addr) {
537 pre = cache->block * SECTOR_SIZE - addr;
Damien George4e487002018-03-02 16:01:18 +1100538 offset = 0;
539 } else {
540 pre = 0;
Damien George86fe73b2018-06-07 14:09:10 +1000541 offset = addr - cache->block * SECTOR_SIZE;
Damien George4e487002018-03-02 16:01:18 +1100542 }
Damien Georgebdc875e2018-03-13 14:13:30 +1100543
544 // Write buffered part first
545 uint32_t len_in_buf = len - pre;
546 len = 0;
547 if (len_in_buf > SECTOR_SIZE - offset) {
548 len = len_in_buf - (SECTOR_SIZE - offset);
549 len_in_buf = SECTOR_SIZE - offset;
Damien George4e487002018-03-02 16:01:18 +1100550 }
Damien George86fe73b2018-06-07 14:09:10 +1000551 memcpy(&cache->buf[offset], &src[pre], len_in_buf);
Damien George4e487002018-03-02 16:01:18 +1100552 self->flags |= 1; // Mark dirty
Damien Georgebdc875e2018-03-13 14:13:30 +1100553
554 // Write part before buffer sector
Damien George4e487002018-03-02 16:01:18 +1100555 while (pre) {
556 int rest = pre & (SECTOR_SIZE - 1);
557 if (rest == 0) {
558 rest = SECTOR_SIZE;
559 }
Damien Georgecc5a9402018-06-07 15:36:27 +1000560 int ret = mp_spiflash_cached_write_part(self, addr, rest, src);
Damien Georgebdc875e2018-03-13 14:13:30 +1100561 if (ret != 0) {
562 mp_spiflash_release_bus(self);
563 return ret;
564 }
565 src += rest;
Damien George4e487002018-03-02 16:01:18 +1100566 addr += rest;
567 pre -= rest;
568 }
Damien Georgebdc875e2018-03-13 14:13:30 +1100569 src += len_in_buf;
570 addr += len_in_buf;
571
572 // Fall through to write remaining part
Damien George4e487002018-03-02 16:01:18 +1100573 }
Damien Georgebdc875e2018-03-13 14:13:30 +1100574
575 uint32_t offset = addr & (SECTOR_SIZE - 1);
576 while (len) {
577 int rest = SECTOR_SIZE - offset;
578 if (rest > len) {
579 rest = len;
580 }
Damien Georgecc5a9402018-06-07 15:36:27 +1000581 int ret = mp_spiflash_cached_write_part(self, addr, rest, src);
Damien Georgebdc875e2018-03-13 14:13:30 +1100582 if (ret != 0) {
583 mp_spiflash_release_bus(self);
584 return ret;
585 }
586 len -= rest;
587 addr += rest;
588 src += rest;
589 offset = 0;
590 }
591
Damien George4e487002018-03-02 16:01:18 +1100592 mp_spiflash_release_bus(self);
593 return 0;
594}
Damien Georgee43a74a2020-12-17 16:59:54 +1100595
596#endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE