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)
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.
33 #include "build/debug.h"
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"
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
];
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
) {
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
.link
= {NULL
, NULL
}, 0, true, NULL
},
80 // Ensure any prior DMA has completed before continuing
83 spiSequence(dev
, &segments
[0]);
85 // Block pending completion of SPI access, but the erase will be ongoing
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
])) {
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
])) {
117 bool w25m_identify(flashDevice_t
*fdevice
, uint32_t jedecID
)
121 #ifdef USE_FLASH_W25M512
122 case JEDEC_ID_WINBOND_W25M512
:
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_identify(&dieDevice
[die
], JEDEC_ID_WINBOND_W25Q256
);
133 fdevice
->geometry
.flashType
= FLASH_TYPE_NOR
;
137 #ifdef USE_FLASH_W25M02G
138 case JEDEC_ID_WINBOND_W25M02G
:
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_identify(&dieDevice
[die
], JEDEC_ID_WINBOND_W25N01GV
);
149 fdevice
->geometry
.flashType
= FLASH_TYPE_NAND
;
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;
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
;
173 void w25m_configure(flashDevice_t
*fdevice
, uint32_t configurationFlags
)
175 for (int dieNumber
= 0 ; dieNumber
< dieCount
; dieNumber
++) {
176 w25m_dieSelect(fdevice
->io
.handle
.dev
, dieNumber
);
177 dieDevice
[dieNumber
].vTable
->configure(&dieDevice
[dieNumber
], configurationFlags
);
181 void w25m_eraseSector(flashDevice_t
*fdevice
, uint32_t address
)
183 int dieNumber
= address
/ dieSize
;
185 w25m_dieSelect(fdevice
->io
.handle
.dev
, dieNumber
);
187 dieDevice
[dieNumber
].vTable
->eraseSector(&dieDevice
[dieNumber
], address
% dieSize
);
190 void w25m_eraseCompletely(flashDevice_t
*fdevice
)
192 for (int dieNumber
= 0 ; dieNumber
< dieCount
; dieNumber
++) {
193 w25m_dieSelect(fdevice
->io
.handle
.dev
, dieNumber
);
194 dieDevice
[dieNumber
].vTable
->eraseCompletely(&dieDevice
[dieNumber
]);
198 static uint32_t currentWriteAddress
;
199 static int currentWriteDie
;
201 void w25m_pageProgramBegin(flashDevice_t
*fdevice
, uint32_t address
, void (*callback
)(uint32_t length
))
203 currentWriteDie
= address
/ dieSize
;
204 w25m_dieSelect(fdevice
->io
.handle
.dev
, currentWriteDie
);
205 currentWriteAddress
= address
% dieSize
;
206 dieDevice
[currentWriteDie
].vTable
->pageProgramBegin(&dieDevice
[currentWriteDie
], address
, callback
);
209 uint32_t w25m_pageProgramContinue(flashDevice_t
*fdevice
, uint8_t const **buffers
, uint32_t *bufferSizes
, uint32_t bufferCount
)
213 return dieDevice
[currentWriteDie
].vTable
->pageProgramContinue(&dieDevice
[currentWriteDie
], buffers
, bufferSizes
, bufferCount
);
216 void w25m_pageProgramFinish(flashDevice_t
*fdevice
)
220 dieDevice
[currentWriteDie
].vTable
->pageProgramFinish(&dieDevice
[currentWriteDie
]);
223 void w25m_pageProgram(flashDevice_t
*fdevice
, uint32_t address
, const uint8_t *data
, uint32_t length
, void (*callback
)(uint32_t length
))
225 w25m_pageProgramBegin(fdevice
, address
, callback
);
227 w25m_pageProgramContinue(fdevice
, &data
, &length
, 1);
229 w25m_pageProgramFinish(fdevice
);
232 int w25m_readBytes(flashDevice_t
*fdevice
, uint32_t address
, uint8_t *buffer
, uint32_t length
)
234 int rlen
; // remaining length
235 int tlen
; // transfer length for a round
238 // Divide a read that spans multiple dies into two.
239 // The loop is executed twice at the most for decent 'length'.
241 for (rlen
= length
; rlen
; rlen
-= tlen
) {
242 int dieNumber
= address
/ dieSize
;
243 uint32_t dieAddress
= address
% dieSize
;
244 tlen
= MIN(dieAddress
+ rlen
, dieSize
) - dieAddress
;
246 w25m_dieSelect(fdevice
->io
.handle
.dev
, dieNumber
);
248 rbytes
= dieDevice
[dieNumber
].vTable
->readBytes(&dieDevice
[dieNumber
], dieAddress
, buffer
, tlen
);
260 const flashGeometry_t
* w25m_getGeometry(flashDevice_t
*fdevice
)
262 return &fdevice
->geometry
;
265 static const flashVTable_t w25m_vTable
= {
266 .configure
= w25m_configure
,
267 .isReady
= w25m_isReady
,
268 .waitForReady
= w25m_waitForReady
,
269 .eraseSector
= w25m_eraseSector
,
270 .eraseCompletely
= w25m_eraseCompletely
,
271 .pageProgramBegin
= w25m_pageProgramBegin
,
272 .pageProgramContinue
= w25m_pageProgramContinue
,
273 .pageProgramFinish
= w25m_pageProgramFinish
,
274 .pageProgram
= w25m_pageProgram
,
275 .readBytes
= w25m_readBytes
,
276 .getGeometry
= w25m_getGeometry
,