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