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