FIX: Invalid references when neither DSHOT nor PWM_OUTPUT is defined. (#14135)
[betaflight.git] / src / main / drivers / flash / flash_w25m.c
blobd0024e050f24e1ee28e37fa7f45f02ad8b199b70
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * Winbond W25M series stacked die flash driver.
23 * Handles homogeneous stack of identical dies by calling die drivers.
25 * Author: jflyper
28 #include <stdbool.h>
29 #include <stdint.h>
31 #include "platform.h"
33 #include "build/debug.h"
35 #ifdef USE_FLASH_W25M
37 #include "common/maths.h"
38 #include "drivers/bus_spi.h"
39 #include "drivers/flash/flash.h"
40 #include "drivers/flash/flash_impl.h"
41 #include "drivers/io.h"
42 #include "drivers/time.h"
44 #include "drivers/flash/flash_m25p16.h"
45 #include "drivers/flash/flash_w25m.h"
46 #include "drivers/flash/flash_w25n.h"
48 #include "pg/flash.h"
50 #define W25M_INSTRUCTION_SOFTWARE_DIE_SELECT 0xC2
52 #define JEDEC_ID_WINBOND_W25M512 0xEF7119 // W25Q256 x 2
53 #define JEDEC_ID_WINBOND_W25M02G 0xEFAB21 // W25N01G x 2
54 #define JEDEC_ID_WINBOND_W25Q256 0xEF4019
55 #define JEDEC_ID_WINBOND_W25N01GV 0xEFAA21
57 static const flashVTable_t w25m_vTable;
59 #define MAX_DIE_COUNT 2
61 static flashDevice_t dieDevice[MAX_DIE_COUNT];
63 static int dieCount;
64 static uint32_t dieSize;
66 static void w25m_dieSelect(const extDevice_t *dev, int die)
68 static int activeDie = -1;
70 if (activeDie == die) {
71 return;
74 uint8_t command[2] = { W25M_INSTRUCTION_SOFTWARE_DIE_SELECT, die };
76 busSegment_t segments[] = {
77 {.u.buffers = {command, NULL}, sizeof(command), true, NULL},
78 {.u.link = {NULL, NULL}, 0, true, NULL},
81 // Ensure any prior DMA has completed before continuing
82 spiWait(dev);
84 spiSequence(dev, &segments[0]);
86 // Block pending completion of SPI access, but the erase will be ongoing
87 spiWait(dev);
89 activeDie = die;
92 static bool w25m_isReady(flashDevice_t *fdevice)
94 for (int die = 0 ; die < dieCount ; die++) {
95 if (dieDevice[die].couldBeBusy) {
96 w25m_dieSelect(fdevice->io.handle.dev, die);
97 if (!dieDevice[die].vTable->isReady(&dieDevice[die])) {
98 return false;
103 return true;
106 static bool w25m_waitForReady(flashDevice_t *fdevice)
108 for (int die = 0 ; die < dieCount ; die++) {
109 w25m_dieSelect(fdevice->io.handle.dev, die);
110 if (!dieDevice[die].vTable->waitForReady(&dieDevice[die])) {
111 return false;
115 return true;
118 bool w25m_identify(flashDevice_t *fdevice, uint32_t jedecID)
121 switch (jedecID) {
122 #ifdef USE_FLASH_W25M512
123 case JEDEC_ID_WINBOND_W25M512:
124 // W25Q256 x 2
125 dieCount = 2;
127 for (int die = 0 ; die < dieCount ; die++) {
128 w25m_dieSelect(fdevice->io.handle.dev, die);
129 dieDevice[die].io.handle.dev = fdevice->io.handle.dev;
130 dieDevice[die].io.mode = fdevice->io.mode;
131 m25p16_identify(&dieDevice[die], JEDEC_ID_WINBOND_W25Q256);
134 fdevice->geometry.flashType = FLASH_TYPE_NOR;
135 break;
136 #endif
138 #ifdef USE_FLASH_W25M02G
139 case JEDEC_ID_WINBOND_W25M02G:
140 // W25N01G x 2
141 dieCount = 2;
143 for (int die = 0 ; die < dieCount ; die++) {
144 w25m_dieSelect(fdevice->io.handle.dev, die);
145 dieDevice[die].io.handle.dev = fdevice->io.handle.dev;
146 dieDevice[die].io.mode = fdevice->io.mode;
147 w25n_identify(&dieDevice[die], JEDEC_ID_WINBOND_W25N01GV);
150 fdevice->geometry.flashType = FLASH_TYPE_NAND;
151 break;
152 #endif
154 default:
155 // Not a valid W25M series device
156 fdevice->geometry.sectors = 0;
157 fdevice->geometry.pagesPerSector = 0;
158 fdevice->geometry.sectorSize = 0;
159 fdevice->geometry.totalSize = 0;
160 return false;
163 fdevice->geometry.sectors = dieDevice[0].geometry.sectors * dieCount;
164 fdevice->geometry.sectorSize = dieDevice[0].geometry.sectorSize;
165 fdevice->geometry.pagesPerSector = dieDevice[0].geometry.pagesPerSector;
166 fdevice->geometry.pageSize = dieDevice[0].geometry.pageSize;
167 dieSize = dieDevice[0].geometry.totalSize;
168 fdevice->geometry.totalSize = dieSize * dieCount;
169 fdevice->vTable = &w25m_vTable;
171 return true;
174 void w25m_configure(flashDevice_t *fdevice, uint32_t configurationFlags)
176 for (int dieNumber = 0 ; dieNumber < dieCount ; dieNumber++) {
177 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
178 dieDevice[dieNumber].vTable->configure(&dieDevice[dieNumber], configurationFlags);
182 void w25m_eraseSector(flashDevice_t *fdevice, uint32_t address)
184 int dieNumber = address / dieSize;
186 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
188 dieDevice[dieNumber].vTable->eraseSector(&dieDevice[dieNumber], address % dieSize);
191 void w25m_eraseCompletely(flashDevice_t *fdevice)
193 for (int dieNumber = 0 ; dieNumber < dieCount ; dieNumber++) {
194 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
195 dieDevice[dieNumber].vTable->eraseCompletely(&dieDevice[dieNumber]);
199 static uint32_t currentWriteAddress;
200 static int currentWriteDie;
202 void w25m_pageProgramBegin(flashDevice_t *fdevice, uint32_t address, void (*callback)(uint32_t length))
204 currentWriteDie = address / dieSize;
205 w25m_dieSelect(fdevice->io.handle.dev, currentWriteDie);
206 currentWriteAddress = address % dieSize;
207 dieDevice[currentWriteDie].vTable->pageProgramBegin(&dieDevice[currentWriteDie], address, callback);
210 uint32_t w25m_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buffers, const uint32_t *bufferSizes, uint32_t bufferCount)
212 UNUSED(fdevice);
214 return dieDevice[currentWriteDie].vTable->pageProgramContinue(&dieDevice[currentWriteDie], buffers, bufferSizes, bufferCount);
217 void w25m_pageProgramFinish(flashDevice_t *fdevice)
219 UNUSED(fdevice);
221 dieDevice[currentWriteDie].vTable->pageProgramFinish(&dieDevice[currentWriteDie]);
224 void w25m_pageProgram(flashDevice_t *fdevice, uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length))
226 w25m_pageProgramBegin(fdevice, address, callback);
228 w25m_pageProgramContinue(fdevice, &data, &length, 1);
230 w25m_pageProgramFinish(fdevice);
233 int w25m_readBytes(flashDevice_t *fdevice, uint32_t address, uint8_t *buffer, uint32_t length)
235 int rlen; // remaining length
236 int tlen; // transfer length for a round
237 int rbytes;
239 // Divide a read that spans multiple dies into two.
240 // The loop is executed twice at the most for decent 'length'.
242 for (rlen = length; rlen; rlen -= tlen) {
243 int dieNumber = address / dieSize;
244 uint32_t dieAddress = address % dieSize;
245 tlen = MIN(dieAddress + rlen, dieSize) - dieAddress;
247 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
249 rbytes = dieDevice[dieNumber].vTable->readBytes(&dieDevice[dieNumber], dieAddress, buffer, tlen);
251 if (!rbytes) {
252 return 0;
255 address += tlen;
256 buffer += tlen;
258 return length;
261 const flashGeometry_t* w25m_getGeometry(flashDevice_t *fdevice)
263 return &fdevice->geometry;
266 static const flashVTable_t w25m_vTable = {
267 .configure = w25m_configure,
268 .isReady = w25m_isReady,
269 .waitForReady = w25m_waitForReady,
270 .eraseSector = w25m_eraseSector,
271 .eraseCompletely = w25m_eraseCompletely,
272 .pageProgramBegin = w25m_pageProgramBegin,
273 .pageProgramContinue = w25m_pageProgramContinue,
274 .pageProgramFinish = w25m_pageProgramFinish,
275 .pageProgram = w25m_pageProgram,
276 .readBytes = w25m_readBytes,
277 .getGeometry = w25m_getGeometry,
279 #endif