Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / flash_w25m.c
blobd92fe0bfb70d2c1ab64099636e2b745fc14e9626
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.h"
40 #include "drivers/flash_impl.h"
41 #include "drivers/io.h"
42 #include "drivers/time.h"
44 #include "flash_m25p16.h"
45 #include "flash_w25m.h"
46 #include "flash_w25n01g.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
56 static const flashVTable_t w25m_vTable;
58 #define MAX_DIE_COUNT 2
60 static flashDevice_t dieDevice[MAX_DIE_COUNT];
62 static int dieCount;
63 static uint32_t dieSize;
65 static void w25m_dieSelect(const extDevice_t *dev, int die)
67 static int activeDie = -1;
69 if (activeDie == die) {
70 return;
73 uint8_t command[2] = { W25M_INSTRUCTION_SOFTWARE_DIE_SELECT, die };
75 busSegment_t segments[] = {
76 {.u.buffers = {command, NULL}, sizeof(command), true, NULL},
77 {.u.buffers = {NULL, NULL}, 0, true, NULL},
80 // Ensure any prior DMA has completed before continuing
81 spiWait(dev);
83 spiSequence(dev, &segments[0]);
85 // Block pending completion of SPI access, but the erase will be ongoing
86 spiWait(dev);
88 activeDie = die;
91 static bool w25m_isReady(flashDevice_t *fdevice)
93 for (int die = 0 ; die < dieCount ; die++) {
94 if (dieDevice[die].couldBeBusy) {
95 w25m_dieSelect(fdevice->io.handle.dev, die);
96 if (!dieDevice[die].vTable->isReady(&dieDevice[die])) {
97 return false;
102 return true;
105 static bool w25m_waitForReady(flashDevice_t *fdevice)
107 for (int die = 0 ; die < dieCount ; die++) {
108 w25m_dieSelect(fdevice->io.handle.dev, die);
109 if (!dieDevice[die].vTable->waitForReady(&dieDevice[die])) {
110 return false;
114 return true;
117 bool w25m_detect(flashDevice_t *fdevice, uint32_t chipID)
120 switch (chipID) {
121 #ifdef USE_FLASH_W25M512
122 case JEDEC_ID_WINBOND_W25M512:
123 // W25Q256 x 2
124 dieCount = 2;
126 for (int die = 0 ; die < dieCount ; die++) {
127 w25m_dieSelect(fdevice->io.handle.dev, die);
128 dieDevice[die].io.handle.dev = fdevice->io.handle.dev;
129 dieDevice[die].io.mode = fdevice->io.mode;
130 m25p16_detect(&dieDevice[die], JEDEC_ID_WINBOND_W25Q256);
133 fdevice->geometry.flashType = FLASH_TYPE_NOR;
134 break;
135 #endif
137 #ifdef USE_FLASH_W25M02G
138 case JEDEC_ID_WINBOND_W25M02G:
139 // W25N01G x 2
140 dieCount = 2;
142 for (int die = 0 ; die < dieCount ; die++) {
143 w25m_dieSelect(fdevice->io.handle.dev, die);
144 dieDevice[die].io.handle.dev = fdevice->io.handle.dev;
145 dieDevice[die].io.mode = fdevice->io.mode;
146 w25n01g_detect(&dieDevice[die], JEDEC_ID_WINBOND_W25N01GV);
149 fdevice->geometry.flashType = FLASH_TYPE_NAND;
150 break;
151 #endif
153 default:
154 // Not a valid W25M series device
155 fdevice->geometry.sectors = 0;
156 fdevice->geometry.pagesPerSector = 0;
157 fdevice->geometry.sectorSize = 0;
158 fdevice->geometry.totalSize = 0;
159 return false;
162 fdevice->geometry.sectors = dieDevice[0].geometry.sectors * dieCount;
163 fdevice->geometry.sectorSize = dieDevice[0].geometry.sectorSize;
164 fdevice->geometry.pagesPerSector = dieDevice[0].geometry.pagesPerSector;
165 fdevice->geometry.pageSize = dieDevice[0].geometry.pageSize;
166 dieSize = dieDevice[0].geometry.totalSize;
167 fdevice->geometry.totalSize = dieSize * dieCount;
168 fdevice->vTable = &w25m_vTable;
170 return true;
173 void w25m_eraseSector(flashDevice_t *fdevice, uint32_t address)
175 int dieNumber = address / dieSize;
177 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
179 dieDevice[dieNumber].vTable->eraseSector(&dieDevice[dieNumber], address % dieSize);
182 void w25m_eraseCompletely(flashDevice_t *fdevice)
184 for (int dieNumber = 0 ; dieNumber < dieCount ; dieNumber++) {
185 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
186 dieDevice[dieNumber].vTable->eraseCompletely(&dieDevice[dieNumber]);
190 static uint32_t currentWriteAddress;
191 static int currentWriteDie;
193 void w25m_pageProgramBegin(flashDevice_t *fdevice, uint32_t address, void (*callback)(uint32_t length))
195 currentWriteDie = address / dieSize;
196 w25m_dieSelect(fdevice->io.handle.dev, currentWriteDie);
197 currentWriteAddress = address % dieSize;
198 dieDevice[currentWriteDie].vTable->pageProgramBegin(&dieDevice[currentWriteDie], address, callback);
201 uint32_t w25m_pageProgramContinue(flashDevice_t *fdevice, uint8_t const **buffers, uint32_t *bufferSizes, uint32_t bufferCount)
203 UNUSED(fdevice);
205 return dieDevice[currentWriteDie].vTable->pageProgramContinue(&dieDevice[currentWriteDie], buffers, bufferSizes, bufferCount);
208 void w25m_pageProgramFinish(flashDevice_t *fdevice)
210 UNUSED(fdevice);
212 dieDevice[currentWriteDie].vTable->pageProgramFinish(&dieDevice[currentWriteDie]);
215 void w25m_pageProgram(flashDevice_t *fdevice, uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length))
217 w25m_pageProgramBegin(fdevice, address, callback);
219 w25m_pageProgramContinue(fdevice, &data, &length, 1);
221 w25m_pageProgramFinish(fdevice);
224 int w25m_readBytes(flashDevice_t *fdevice, uint32_t address, uint8_t *buffer, uint32_t length)
226 int rlen; // remaining length
227 int tlen; // transfer length for a round
228 int rbytes;
230 // Divide a read that spans multiple dies into two.
231 // The loop is executed twice at the most for decent 'length'.
233 for (rlen = length; rlen; rlen -= tlen) {
234 int dieNumber = address / dieSize;
235 uint32_t dieAddress = address % dieSize;
236 tlen = MIN(dieAddress + rlen, dieSize) - dieAddress;
238 w25m_dieSelect(fdevice->io.handle.dev, dieNumber);
240 rbytes = dieDevice[dieNumber].vTable->readBytes(&dieDevice[dieNumber], dieAddress, buffer, tlen);
242 if (!rbytes) {
243 return 0;
246 address += tlen;
247 buffer += tlen;
249 return length;
252 const flashGeometry_t* w25m_getGeometry(flashDevice_t *fdevice)
254 return &fdevice->geometry;
257 static const flashVTable_t w25m_vTable = {
258 .isReady = w25m_isReady,
259 .waitForReady = w25m_waitForReady,
260 .eraseSector = w25m_eraseSector,
261 .eraseCompletely = w25m_eraseCompletely,
262 .pageProgramBegin = w25m_pageProgramBegin,
263 .pageProgramContinue = w25m_pageProgramContinue,
264 .pageProgramFinish = w25m_pageProgramFinish,
265 .pageProgram = w25m_pageProgram,
266 .readBytes = w25m_readBytes,
267 .getGeometry = w25m_getGeometry,
269 #endif