blob: 67692d7314efa2fc6fe4fab25e02b696630079c1 [file] [log] [blame]
Aidan Thorntondb4e87d2013-08-27 18:01:53 +00001/*
2 * Support for Atmel AT45DB series DataFlash chips.
3 * This file is part of the flashrom project.
4 *
5 * Copyright (C) 2012 Aidan Thornton
6 * Copyright (C) 2013 Stefan Tauner
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Aidan Thorntondb4e87d2013-08-27 18:01:53 +000016 */
17
18#include <string.h>
19#include "flash.h"
20#include "chipdrivers.h"
21#include "programmer.h"
Nico Huberd5185632024-01-05 18:44:41 +010022#include "spi_command.h"
Aidan Thorntondb4e87d2013-08-27 18:01:53 +000023#include "spi.h"
24
25/* Status register bits */
26#define AT45DB_READY (1<<7)
27#define AT45DB_CMP (1<<6)
28#define AT45DB_PROT (1<<1)
29#define AT45DB_POWEROF2 (1<<0)
30
31/* Opcodes */
32#define AT45DB_STATUS 0xD7 /* NB: this is a block erase command on most other chips(!). */
33#define AT45DB_DISABLE_PROTECT 0x3D, 0x2A, 0x7F, 0x9A
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +000034#define AT45DB_READ_ARRAY 0xE8
Aidan Thorntondb4e87d2013-08-27 18:01:53 +000035#define AT45DB_READ_PROTECT 0x32
36#define AT45DB_READ_LOCKDOWN 0x35
37#define AT45DB_PAGE_ERASE 0x81
38#define AT45DB_BLOCK_ERASE 0x50
39#define AT45DB_SECTOR_ERASE 0x7C
40#define AT45DB_CHIP_ERASE 0xC7
41#define AT45DB_CHIP_ERASE_ADDR 0x94809A /* Magic address. See usage. */
42#define AT45DB_BUFFER1_WRITE 0x84
43#define AT45DB_BUFFER1_PAGE_PROGRAM 0x88
44/* Buffer 2 is unused yet.
45#define AT45DB_BUFFER2_WRITE 0x87
46#define AT45DB_BUFFER2_PAGE_PROGRAM 0x89
47*/
48
49static uint8_t at45db_read_status_register(struct flashctx *flash, uint8_t *status)
50{
51 static const uint8_t cmd[] = { AT45DB_STATUS };
52
53 int ret = spi_send_command(flash, sizeof(cmd), 1, cmd, status);
54 if (ret != 0)
55 msg_cerr("Reading the status register failed!\n");
56 else
57 msg_cspew("Status register: 0x%02x.\n", *status);
58 return ret;
59}
60
61int spi_disable_blockprotect_at45db(struct flashctx *flash)
62{
63 static const uint8_t cmd[4] = { AT45DB_DISABLE_PROTECT }; /* NB: 4 bytes magic number */
64 int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
65 if (ret != 0) {
66 msg_cerr("Sending disable lockdown failed!\n");
67 return ret;
68 }
69 uint8_t status;
70 ret = at45db_read_status_register(flash, &status);
71 if (ret != 0 || ((status & AT45DB_PROT) != 0)) {
72 msg_cerr("Disabling lockdown failed!\n");
73 return 1;
74 }
75
76 return 0;
77}
78
79static unsigned int at45db_get_sector_count(struct flashctx *flash)
80{
81 unsigned int i, j;
82 unsigned int cnt = 0;
83 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
84 if (flash->chip->block_erasers[i].block_erase == &spi_erase_at45db_sector) {
85 for (j = 0; j < NUM_ERASEREGIONS; j++) {
86 cnt += flash->chip->block_erasers[i].eraseblocks[j].count;
87 }
88 }
89 }
90 msg_cspew("%s: number of sectors=%u\n", __func__, cnt);
91 return cnt;
92}
93
94/* Reads and prettyprints protection/lockdown registers.
95 * Some elegance of the printouts had to be cut down a bit to share this code. */
96static uint8_t at45db_prettyprint_protection_register(struct flashctx *flash, uint8_t opcode, const char *regname)
97{
98 const uint8_t cmd[] = { opcode, 0, 0, 0 };
Stefan Tauner23e10b82016-01-23 16:16:49 +000099 const size_t sec_count = at45db_get_sector_count(flash);
100 if (sec_count < 2)
101 return 0;
102
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000103 /* The first two sectors share the first result byte. */
104 uint8_t buf[at45db_get_sector_count(flash) - 1];
105
106 int ret = spi_send_command(flash, sizeof(cmd), sizeof(buf), cmd, buf);
107 if (ret != 0) {
108 msg_cerr("Reading the %s register failed!\n", regname);
109 return ret;
110 }
111
112 unsigned int i;
113 for (i = 0; i < sizeof(buf); i++) {
114 if (buf[i] != 0x00)
115 break;
116 if (i == sizeof(buf) - 1) {
117 msg_cdbg("No Sector is %sed.\n", regname);
118 return 0;
119 }
120 }
121
122 /* TODO: print which addresses are mapped to (un)locked sectors. */
123 msg_cdbg("Sector 0a is %s%sed.\n", ((buf[0] & 0xC0) == 0x00) ? "un" : "", regname);
124 msg_cdbg("Sector 0b is %s%sed.\n", ((buf[0] & 0x30) == 0x00) ? "un" : "", regname);
125 for (i = 1; i < sizeof(buf); i++)
126 msg_cdbg("Sector %2u is %s%sed.\n", i, (buf[i] == 0x00) ? "un" : "", regname);
127
128 return 0;
129}
130
131/* bit 7: busy flag
132 * bit 6: memory/buffer compare result
133 * bit 5-2: density (encoding see below)
134 * bit 1: protection enabled (soft or hard)
135 * bit 0: "power of 2" page size indicator (e.g. 1 means 256B; 0 means 264B)
136 *
137 * 5-2 encoding: bit 2 is always 1, bits 3-5 encode the density as "2^(bits - 1)" in Mb e.g.:
138 * AT45DB161D 1011 16Mb */
139int spi_prettyprint_status_register_at45db(struct flashctx *flash)
140{
141 uint8_t status;
142 if (at45db_read_status_register(flash, &status) != 0) {
143 return 1;
144 }
145
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000146 /* AT45DB321C does not support lockdown or a page size of a power of 2... */
147 const bool isAT45DB321C = (strcmp(flash->chip->name, "AT45DB321C") == 0);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000148 msg_cdbg("Chip status register is 0x%02x\n", status);
149 msg_cdbg("Chip status register: Bit 7 / Ready is %sset\n", (status & AT45DB_READY) ? "" : "not ");
150 msg_cdbg("Chip status register: Bit 6 / Compare match is %sset\n", (status & AT45DB_CMP) ? "" : "not ");
151 spi_prettyprint_status_register_bit(status, 5);
152 spi_prettyprint_status_register_bit(status, 4);
153 spi_prettyprint_status_register_bit(status, 3);
154 spi_prettyprint_status_register_bit(status, 2);
155 const uint8_t dens = (status >> 3) & 0x7; /* Bit 2 is always 1, we use the other bits only */
156 msg_cdbg("Chip status register: Density is %u Mb\n", 1 << (dens - 1));
157 msg_cdbg("Chip status register: Bit 1 / Protection is %sset\n", (status & AT45DB_PROT) ? "" : "not ");
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000158
159 if (isAT45DB321C)
160 spi_prettyprint_status_register_bit(status, 0);
161 else
162 msg_cdbg("Chip status register: Bit 0 / \"Power of 2\" is %sset\n",
163 (status & AT45DB_POWEROF2) ? "" : "not ");
164
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000165 if (status & AT45DB_PROT)
166 at45db_prettyprint_protection_register(flash, AT45DB_READ_PROTECT, "protect");
167
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000168 if (!isAT45DB321C)
169 at45db_prettyprint_protection_register(flash, AT45DB_READ_LOCKDOWN, "lock");
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000170
171 return 0;
172}
173
174/* Probe function for AT45DB* chips that support multiple page sizes. */
175int probe_spi_at45db(struct flashctx *flash)
176{
177 uint8_t status;
178 struct flashchip *chip = flash->chip;
179
180 if (!probe_spi_rdid(flash))
181 return 0;
182
183 /* Some AT45DB* chips support two different page sizes each (e.g. 264 and 256 B). In order to tell which
184 * page size this chip has we need to read the status register. */
185 if (at45db_read_status_register(flash, &status) != 0)
186 return 0;
187
188 /* We assume sane power-of-2 page sizes and adjust the chip attributes in case this is not the case. */
189 if ((status & AT45DB_POWEROF2) == 0) {
190 chip->total_size = (chip->total_size / 32) * 33;
191 chip->page_size = (chip->page_size / 32) * 33;
192
193 unsigned int i, j;
194 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
195 struct block_eraser *eraser = &chip->block_erasers[i];
196 for (j = 0; j < NUM_ERASEREGIONS; j++) {
197 eraser->eraseblocks[j].size = (eraser->eraseblocks[j].size / 32) * 33;
198 }
199 }
200 }
201
202 switch (chip->page_size) {
203 case 256: chip->gran = write_gran_256bytes; break;
204 case 264: chip->gran = write_gran_264bytes; break;
205 case 512: chip->gran = write_gran_512bytes; break;
206 case 528: chip->gran = write_gran_528bytes; break;
207 case 1024: chip->gran = write_gran_1024bytes; break;
208 case 1056: chip->gran = write_gran_1056bytes; break;
209 default:
210 msg_cerr("%s: unknown page size %d.\n", __func__, chip->page_size);
211 return 0;
212 }
213
214 msg_cdbg2("%s: total size %i kB, page size %i B\n", __func__, chip->total_size * 1024, chip->page_size);
215
216 return 1;
217}
218
Nico Huberc3b02dc2023-08-12 01:13:45 +0200219/* In case of non-power-of-two page sizes we need to convert the address flashprog uses to the address the
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000220 * DataFlash chips use. The latter uses a segmented address space where the page address is encoded in the
221 * more significant bits and the offset within the page is encoded in the less significant bits. The exact
222 * partition depends on the page size.
223 */
224static unsigned int at45db_convert_addr(unsigned int addr, unsigned int page_size)
225{
226 unsigned int page_bits = address_to_bits(page_size - 1);
227 unsigned int at45db_addr = ((addr / page_size) << page_bits) | (addr % page_size);
228 msg_cspew("%s: addr=0x%x, page_size=%u, page_bits=%u -> at45db_addr=0x%x\n",
229 __func__, addr, page_size, page_bits, at45db_addr);
230 return at45db_addr;
231}
232
233int spi_read_at45db(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
234{
235 const unsigned int page_size = flash->chip->page_size;
236 const unsigned int total_size = flash->chip->total_size * 1024;
237 if ((addr + len) > total_size) {
238 msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n",
239 __func__, addr, len, total_size);
240 return 1;
241 }
242
243 /* We have to split this up into chunks to fit within the programmer's read size limit, but those
244 * chunks can cross page boundaries. */
Nico Huber9a11cbf2023-01-13 01:19:07 +0100245 const unsigned int max_data_read = flash->mst.spi->max_data_read;
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000246 const unsigned int max_chunk = (max_data_read > 0) ? max_data_read : page_size;
Stefan Tauner7141b982014-05-16 17:52:04 +0000247 while (len > 0) {
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000248 unsigned int chunk = min(max_chunk, len);
Nico Huberca1c7fd2023-04-28 21:44:41 +0000249 int ret = spi_nbyte_read(flash, buf, at45db_convert_addr(addr, page_size), chunk);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000250 if (ret) {
251 msg_cerr("%s: error sending read command!\n", __func__);
252 return ret;
253 }
Richard Hughes842d6782021-01-15 09:48:12 +0000254 flashprog_progress_add(flash, chunk);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000255 addr += chunk;
Stefan Tauner7141b982014-05-16 17:52:04 +0000256 buf += chunk;
257 len -= chunk;
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000258 }
259
260 return 0;
261}
262
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000263/* Legacy continuous read, used where spi_read_at45db() is not available.
264 * The first 4 (dummy) bytes read need to be discarded. */
265int spi_read_at45db_e8(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
266{
267 const unsigned int page_size = flash->chip->page_size;
268 const unsigned int total_size = flash->chip->total_size * 1024;
269 if ((addr + len) > total_size) {
270 msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n",
271 __func__, addr, len, total_size);
272 return 1;
273 }
274
275 /* We have to split this up into chunks to fit within the programmer's read size limit, but those
276 * chunks can cross page boundaries. */
Nico Huber9a11cbf2023-01-13 01:19:07 +0100277 const unsigned int max_data_read = flash->mst.spi->max_data_read;
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000278 const unsigned int max_chunk = (max_data_read > 0) ? max_data_read : page_size;
Stefan Tauner7141b982014-05-16 17:52:04 +0000279 while (len > 0) {
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000280 const unsigned int addr_at45 = at45db_convert_addr(addr, page_size);
281 const unsigned char cmd[] = {
282 AT45DB_READ_ARRAY,
283 (addr_at45 >> 16) & 0xff,
284 (addr_at45 >> 8) & 0xff,
285 (addr_at45 >> 0) & 0xff
286 };
287 /* We need to leave place for 4 dummy bytes and handle them explicitly. */
288 unsigned int chunk = min(max_chunk, len + 4);
289 uint8_t tmp[chunk];
290 int ret = spi_send_command(flash, sizeof(cmd), chunk, cmd, tmp);
291 if (ret) {
292 msg_cerr("%s: error sending read command!\n", __func__);
293 return ret;
294 }
295 /* Copy result without dummy bytes into buf and advance address counter respectively. */
Stefan Tauner7141b982014-05-16 17:52:04 +0000296 memcpy(buf, tmp + 4, chunk - 4);
Richard Hughes842d6782021-01-15 09:48:12 +0000297 flashprog_progress_add(flash, chunk - 4);
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000298 addr += chunk - 4;
Stefan Tauner7141b982014-05-16 17:52:04 +0000299 buf += chunk - 4;
300 len -= chunk - 4;
Stefan Taunerfdc4f7e2013-08-27 18:02:12 +0000301 }
302 return 0;
303}
304
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000305/* Returns 0 when ready, 1 on errors and timeouts. */
306static int at45db_wait_ready (struct flashctx *flash, unsigned int us, unsigned int retries)
307{
308 while (true) {
309 uint8_t status;
310 int ret = at45db_read_status_register(flash, &status);
311 if ((status & AT45DB_READY) == AT45DB_READY)
312 return 0;
313 if (ret != 0 || retries-- == 0)
314 return 1;
315 programmer_delay(us);
316 }
317}
318
319static int at45db_erase(struct flashctx *flash, uint8_t opcode, unsigned int at45db_addr, unsigned int stepsize, unsigned int retries)
320{
321 const uint8_t cmd[] = {
322 opcode,
323 (at45db_addr >> 16) & 0xff,
324 (at45db_addr >> 8) & 0xff,
325 (at45db_addr >> 0) & 0xff
326 };
327
328 /* Send erase command. */
329 int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
330 if (ret != 0) {
331 msg_cerr("%s: error sending erase command!\n", __func__);
332 return ret;
333 }
334
335 /* Wait for completion. */
336 ret = at45db_wait_ready(flash, stepsize, retries);
337 if (ret != 0)
Stefan Tauner23e10b82016-01-23 16:16:49 +0000338 msg_cerr("%s: chip did not become ready again after sending the erase command!\n", __func__);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000339
340 return ret;
341}
342
343int spi_erase_at45db_page(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
344{
345 const unsigned int page_size = flash->chip->page_size;
346 const unsigned int total_size = flash->chip->total_size * 1024;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100347
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000348 if ((addr % page_size) != 0 || (blocklen % page_size) != 0) {
349 msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
350 return 1;
351 }
352
353 if ((addr + blocklen) > total_size) {
354 msg_cerr("%s: tried to erase a block beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
355 __func__, addr, blocklen, total_size);
356 return 1;
357 }
358
359 /* Needs typically about 35 ms for completion, so let's wait 100 ms in 500 us steps. */
360 return at45db_erase(flash, AT45DB_PAGE_ERASE, at45db_convert_addr(addr, page_size), 500, 200);
361}
362
363int spi_erase_at45db_block(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
364{
365 const unsigned int page_size = flash->chip->page_size;
366 const unsigned int total_size = flash->chip->total_size * 1024;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100367
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000368 if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check blocks not pages
369 msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
370 return 1;
371 }
372
373 if ((addr + blocklen) > total_size) {
374 msg_cerr("%s: tried to erase a block beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
375 __func__, addr, blocklen, total_size);
376 return 1;
377 }
378
379 /* Needs typically between 20 and 100 ms for completion, so let's wait 300 ms in 1 ms steps. */
380 return at45db_erase(flash, AT45DB_BLOCK_ERASE, at45db_convert_addr(addr, page_size), 1000, 300);
381}
382
383int spi_erase_at45db_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
384{
385 const unsigned int page_size = flash->chip->page_size;
386 const unsigned int total_size = flash->chip->total_size * 1024;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100387
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000388 if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check sectors not pages
389 msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
390 return 1;
391 }
392
393 if ((addr + blocklen) > total_size) {
394 msg_cerr("%s: tried to erase a sector beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
395 __func__, addr, blocklen, total_size);
396 return 1;
397 }
398
399 /* Needs typically about 5 s for completion, so let's wait 20 seconds in 200 ms steps. */
400 return at45db_erase(flash, AT45DB_SECTOR_ERASE, at45db_convert_addr(addr, page_size), 200000, 100);
401}
402
403int spi_erase_at45db_chip(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
404{
405 const unsigned int total_size = flash->chip->total_size * 1024;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100406
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000407 if ((addr + blocklen) > total_size) {
408 msg_cerr("%s: tried to erase beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
409 __func__, addr, blocklen, total_size);
410 return 1;
411 }
412
413 /* Needs typically from about 5 to over 60 s for completion, so let's wait 100 s in 500 ms steps.
414 * NB: the address is not a real address but a magic number. This hack allows to share code. */
415 return at45db_erase(flash, AT45DB_CHIP_ERASE, AT45DB_CHIP_ERASE_ADDR, 500000, 200);
416}
417
Stefan Tauner1dd5d3a2013-08-27 18:02:19 +0000418/* This one is really special and works only for AT45CS1282. It uses two different opcodes depending on the
419 * address and has an asymmetric layout. */
420int spi_erase_at45cs_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
421{
422 const unsigned int page_size = flash->chip->page_size;
423 const unsigned int total_size = flash->chip->total_size * 1024;
424 const struct block_eraser be = flash->chip->block_erasers[0];
425 const unsigned int sec_0a_top = be.eraseblocks[0].size;
426 const unsigned int sec_0b_top = be.eraseblocks[0].size + be.eraseblocks[1].size;
427
428 if ((addr + blocklen) > total_size) {
429 msg_cerr("%s: tried to erase a sector beyond flash boundary: addr=%u, blocklen=%u, size=%u\n",
430 __func__, addr, blocklen, total_size);
431 return 1;
432 }
433
434 bool partial_range = false;
435 uint8_t opcode = 0x7C; /* Used for all but sector 0a. */
436 if (addr < sec_0a_top) {
437 opcode = 0x50;
438 /* One single sector of 8 pages at address 0. */
439 if (addr != 0 || blocklen != (8 * page_size))
440 partial_range = true;
441 } else if (addr < sec_0b_top) {
442 /* One single sector of 248 pages adjacent to the first. */
443 if (addr != sec_0a_top || blocklen != (248 * page_size))
444 partial_range = true;
445 } else {
446 /* The rest is filled by 63 aligned sectors of 256 pages. */
447 if ((addr % (256 * page_size)) != 0 || (blocklen % (256 * page_size)) != 0)
448 partial_range = true;
449 }
450 if (partial_range) {
451 msg_cerr("%s: cannot erase partial sectors: addr=%u, blocklen=%u\n", __func__, addr, blocklen);
452 return 1;
453 }
454
455 /* Needs up to 4 s for completion, so let's wait 20 seconds in 200 ms steps. */
456 return at45db_erase(flash, opcode, at45db_convert_addr(addr, page_size), 200000, 100);
457}
458
Mark Marshallf20b7be2014-05-09 21:16:21 +0000459static int at45db_fill_buffer1(struct flashctx *flash, const uint8_t *bytes, unsigned int off, unsigned int len)
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000460{
461 const unsigned int page_size = flash->chip->page_size;
462 if ((off + len) > page_size) {
463 msg_cerr("Tried to write %u bytes at offset %u into a buffer of only %u B.\n",
464 len, off, page_size);
465 return 1;
466 }
467
468 /* Create a suitable buffer to store opcode, address and data chunks for buffer1. */
Nico Huber9a11cbf2023-01-13 01:19:07 +0100469 const unsigned int max_data_write = flash->mst.spi->max_data_write;
Nico Huber519be662018-12-23 20:03:35 +0100470 const unsigned int max_chunk = max_data_write > 4 && max_data_write - 4 <= page_size ?
471 max_data_write - 4 : page_size;
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000472 uint8_t buf[4 + max_chunk];
473
474 buf[0] = AT45DB_BUFFER1_WRITE;
475 while (off < page_size) {
476 unsigned int cur_chunk = min(max_chunk, page_size - off);
477 buf[1] = (off >> 16) & 0xff;
478 buf[2] = (off >> 8) & 0xff;
479 buf[3] = (off >> 0) & 0xff;
480 memcpy(&buf[4], bytes + off, cur_chunk);
481 int ret = spi_send_command(flash, 4 + cur_chunk, 0, buf, NULL);
482 if (ret != 0) {
483 msg_cerr("%s: error sending buffer write!\n", __func__);
484 return ret;
485 }
486 off += cur_chunk;
487 }
488 return 0;
489}
490
491static int at45db_commit_buffer1(struct flashctx *flash, unsigned int at45db_addr)
492{
493 const uint8_t cmd[] = {
494 AT45DB_BUFFER1_PAGE_PROGRAM,
495 (at45db_addr >> 16) & 0xff,
496 (at45db_addr >> 8) & 0xff,
497 (at45db_addr >> 0) & 0xff
498 };
499
500 /* Send buffer to device. */
501 int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
502 if (ret != 0) {
503 msg_cerr("%s: error sending buffer to main memory command!\n", __func__);
504 return ret;
505 }
506
507 /* Wait for completion (typically a few ms). */
508 ret = at45db_wait_ready(flash, 250, 200); // 50 ms
509 if (ret != 0) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000510 msg_cerr("%s: chip did not become ready again!\n", __func__);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000511 return ret;
512 }
513
514 return 0;
515}
516
Mark Marshallf20b7be2014-05-09 21:16:21 +0000517static int at45db_program_page(struct flashctx *flash, const uint8_t *buf, unsigned int at45db_addr)
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000518{
519 int ret = at45db_fill_buffer1(flash, buf, 0, flash->chip->page_size);
520 if (ret != 0) {
521 msg_cerr("%s: filling the buffer failed!\n", __func__);
522 return ret;
523 }
524
525 ret = at45db_commit_buffer1(flash, at45db_addr);
526 if (ret != 0) {
527 msg_cerr("%s: committing page failed!\n", __func__);
528 return ret;
529 }
530
531 return 0;
532}
533
Mark Marshallf20b7be2014-05-09 21:16:21 +0000534int spi_write_at45db(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000535{
536 const unsigned int page_size = flash->chip->page_size;
537 const unsigned int total_size = flash->chip->total_size;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100538
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000539 if ((start % page_size) != 0 || (len % page_size) != 0) {
540 msg_cerr("%s: cannot write partial pages: start=%u, len=%u\n", __func__, start, len);
541 return 1;
542 }
543
544 if ((start + len) > (total_size * 1024)) {
545 msg_cerr("%s: tried to write beyond flash boundary: start=%u, len=%u, size=%u\n",
546 __func__, start, len, total_size);
547 return 1;
548 }
549
550 unsigned int i;
551 for (i = 0; i < len; i += page_size) {
552 if (at45db_program_page(flash, buf + i, at45db_convert_addr(start + i, page_size)) != 0) {
553 msg_cerr("Writing page %u failed!\n", i);
554 return 1;
555 }
Richard Hughes842d6782021-01-15 09:48:12 +0000556 flashprog_progress_add(flash, page_size);
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000557 }
558 return 0;
559}