blob: 6602f3cac374ecc2a3dbae15d843f18aaa414973 [file] [log] [blame]
Norman James6a58a272015-10-07 14:34:16 -05001#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5#include "libflash.h"
6#include "libflash-priv.h"
7
8static const struct flash_info flash_info[] = {
9 { 0xc22019, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL25635F"},
10 { 0xc2201a, 0x04000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL51235F"},
11 { 0xef4018, 0x01000000, FL_ERASE_ALL, "Winbond W25Q128BV" },
12 { 0x20ba20, 0x04000000, FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B |
13 FL_ERASE_BULK | FL_MICRON_BUGS,
14 "Micron N25Qx512Ax" },
15 { 0x55aa55, 0x00100000, FL_ERASE_ALL | FL_CAN_4B, "TEST_FLASH" },
Norman Jamesca3f6032015-11-11 07:39:23 -060016 { 0x20ba19, 0x02000000, FL_ERASE_4K | FL_ERASE_64K | FL_CAN_4B |
17 FL_ERASE_BULK | FL_MICRON_BUGS,
18 "Micron N25Qx256Ax" },
19
Norman James6a58a272015-10-07 14:34:16 -050020};
21
22struct flash_chip {
23 struct spi_flash_ctrl *ctrl; /* Controller */
24 struct flash_info info; /* Flash info */
25 uint32_t tsize; /* Corrected flash size */
26 uint32_t min_erase_mask; /* Minimum erase size */
27 bool mode_4b; /* Flash currently in 4b mode */
28 struct flash_req *cur_req; /* Current request */
29 void *smart_buf; /* Buffer for smart writes */
30};
31
32bool libflash_debug;
33
34int fl_read_stat(struct spi_flash_ctrl *ct, uint8_t *stat)
35{
36 return ct->cmd_rd(ct, CMD_RDSR, false, 0, stat, 1);
37}
38
39static void fl_micron_status(struct spi_flash_ctrl *ct)
40{
41 uint8_t flst;
42
43 /*
44 * After a success status on a write or erase, we
45 * need to do that command or some chip variants will
46 * lock
47 */
48 ct->cmd_rd(ct, CMD_MIC_RDFLST, false, 0, &flst, 1);
49}
50
51/* Synchronous write completion, probably need a yield hook */
52int fl_sync_wait_idle(struct spi_flash_ctrl *ct)
53{
54 uint8_t stat;
55 int rc;
56
57 /* XXX Add timeout */
58 for (;;) {
59 rc = fl_read_stat(ct, &stat);
60 if (rc) return rc;
61 if (!(stat & STAT_WIP)) {
62 if (ct->finfo->flags & FL_MICRON_BUGS)
63 fl_micron_status(ct);
64 return 0;
65 }
66 }
67 /* return FLASH_ERR_WIP_TIMEOUT; */
68}
69
70/* Exported for internal use */
71int fl_wren(struct spi_flash_ctrl *ct)
72{
73 int i, rc;
74 uint8_t stat;
75
76 /* Some flashes need it to be hammered */
77 for (i = 0; i < 1000; i++) {
78 rc = ct->cmd_wr(ct, CMD_WREN, false, 0, NULL, 0);
79 if (rc) return rc;
80 rc = fl_read_stat(ct, &stat);
81 if (rc) return rc;
82 if (stat & STAT_WIP) {
83 FL_ERR("LIBFLASH: WREN has WIP status set !\n");
84 rc = fl_sync_wait_idle(ct);
85 if (rc)
86 return rc;
87 continue;
88 }
89 if (stat & STAT_WEN)
90 return 0;
91 }
92 return FLASH_ERR_WREN_TIMEOUT;
93}
94
95int flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len)
96{
97 struct spi_flash_ctrl *ct = c->ctrl;
98
99 /* XXX Add sanity/bound checking */
100
101 /*
102 * If the controller supports read and either we are in 3b mode
103 * or we are in 4b *and* the controller supports it, then do a
104 * high level read.
105 */
106 if ((!c->mode_4b || ct->set_4b) && ct->read)
107 return ct->read(ct, pos, buf, len);
108
109 /* Otherwise, go manual if supported */
110 if (!ct->cmd_rd)
111 return FLASH_ERR_CTRL_CMD_UNSUPPORTED;
112 return ct->cmd_rd(ct, CMD_READ, true, pos, buf, len);
113}
114
115static void fl_get_best_erase(struct flash_chip *c, uint32_t dst, uint32_t size,
116 uint32_t *chunk, uint8_t *cmd)
117{
118 /* Smaller than 32k, use 4k */
119 if ((dst & 0x7fff) || (size < 0x8000)) {
120 *chunk = 0x1000;
121 *cmd = CMD_SE;
122 return;
123 }
124 /* Smaller than 64k and 32k is supported, use it */
125 if ((c->info.flags & FL_ERASE_32K) &&
126 ((dst & 0xffff) || (size < 0x10000))) {
127 *chunk = 0x8000;
128 *cmd = CMD_BE32K;
129 return;
130 }
131 /* If 64K is not supported, use whatever smaller size is */
132 if (!(c->info.flags & FL_ERASE_64K)) {
133 if (c->info.flags & FL_ERASE_32K) {
134 *chunk = 0x8000;
135 *cmd = CMD_BE32K;
136 } else {
137 *chunk = 0x1000;
138 *cmd = CMD_SE;
139 }
140 return;
141 }
142 /* Allright, let's go for 64K */
143 *chunk = 0x10000;
144 *cmd = CMD_BE;
145}
146
147int flash_erase(struct flash_chip *c, uint32_t dst, uint32_t size)
148{
149 struct spi_flash_ctrl *ct = c->ctrl;
150 uint32_t chunk;
151 uint8_t cmd;
152 int rc;
153
154 /* Some sanity checking */
155 if (((dst + size) <= dst) || !size || (dst + size) > c->tsize)
156 return FLASH_ERR_PARM_ERROR;
157
158 /* Check boundaries fit erase blocks */
159 if ((dst | size) & c->min_erase_mask)
160 return FLASH_ERR_ERASE_BOUNDARY;
161
162 FL_DBG("LIBFLASH: Erasing 0x%08x..0%08x...\n", dst, dst + size);
163
164 /* Use controller erase if supported */
165 if (ct->erase)
166 return ct->erase(ct, dst, size);
167
168 /* Allright, loop as long as there's something to erase */
169 while(size) {
170 /* How big can we make it based on alignent & size */
171 fl_get_best_erase(c, dst, size, &chunk, &cmd);
172
173 /* Poke write enable */
174 rc = fl_wren(ct);
175 if (rc)
176 return rc;
177
178 /* Send erase command */
179 rc = ct->cmd_wr(ct, cmd, true, dst, NULL, 0);
180 if (rc)
181 return rc;
182
183 /* Wait for write complete */
184 rc = fl_sync_wait_idle(ct);
185 if (rc)
186 return rc;
187
188 size -= chunk;
189 dst += chunk;
190 }
191 return 0;
192}
193
194int flash_erase_chip(struct flash_chip *c)
195{
196 struct spi_flash_ctrl *ct = c->ctrl;
197 int rc;
198
199 /* XXX TODO: Fallback to using normal erases */
200 if (!(c->info.flags & (FL_ERASE_CHIP|FL_ERASE_BULK)))
201 return FLASH_ERR_CHIP_ER_NOT_SUPPORTED;
202
203 FL_DBG("LIBFLASH: Erasing chip...\n");
204
205 /* Use controller erase if supported */
206 if (ct->erase)
207 return ct->erase(ct, 0, 0xffffffff);
208
209 rc = fl_wren(ct);
210 if (rc) return rc;
211
212 if (c->info.flags & FL_ERASE_CHIP)
213 rc = ct->cmd_wr(ct, CMD_CE, false, 0, NULL, 0);
214 else
215 rc = ct->cmd_wr(ct, CMD_MIC_BULK_ERASE, false, 0, NULL, 0);
216 if (rc)
217 return rc;
218
219 /* Wait for write complete */
220 return fl_sync_wait_idle(ct);
221}
222
223static int fl_wpage(struct flash_chip *c, uint32_t dst, const void *src,
224 uint32_t size)
225{
226 struct spi_flash_ctrl *ct = c->ctrl;
227 int rc;
228
229 if (size < 1 || size > 0x100)
230 return FLASH_ERR_BAD_PAGE_SIZE;
231
232 rc = fl_wren(ct);
233 if (rc) return rc;
234
235 rc = ct->cmd_wr(ct, CMD_PP, true, dst, src, size);
236 if (rc)
237 return rc;
238
239 /* Wait for write complete */
240 return fl_sync_wait_idle(ct);
241}
242
243int flash_write(struct flash_chip *c, uint32_t dst, const void *src,
244 uint32_t size, bool verify)
245{
246 struct spi_flash_ctrl *ct = c->ctrl;
247 uint32_t todo = size;
248 uint32_t d = dst;
249 const void *s = src;
250 uint8_t vbuf[0x100];
251 int rc;
252
253 /* Some sanity checking */
254 if (((dst + size) <= dst) || !size || (dst + size) > c->tsize)
255 return FLASH_ERR_PARM_ERROR;
256
257 FL_DBG("LIBFLASH: Writing to 0x%08x..0%08x...\n", dst, dst + size);
258
259 /*
260 * If the controller supports write and either we are in 3b mode
261 * or we are in 4b *and* the controller supports it, then do a
262 * high level write.
263 */
264 if ((!c->mode_4b || ct->set_4b) && ct->write) {
265 rc = ct->write(ct, dst, src, size);
266 if (rc)
267 return rc;
268 goto writing_done;
269 }
270
271 /* Otherwise, go manual if supported */
272 if (!ct->cmd_wr)
273 return FLASH_ERR_CTRL_CMD_UNSUPPORTED;
274
275 /* Iterate for each page to write */
276 while(todo) {
277 uint32_t chunk;
278
279 /* Handle misaligned start */
280 chunk = 0x100 - (d & 0xff);
281 if (chunk > 0x100)
282 chunk = 0x100;
283 if (chunk > todo)
284 chunk = todo;
285
286 rc = fl_wpage(c, d, s, chunk);
287 if (rc) return rc;
288 d += chunk;
289 s += chunk;
290 todo -= chunk;
291 }
292
293 writing_done:
294 if (!verify)
295 return 0;
296
297 /* Verify */
298 FL_DBG("LIBFLASH: Verifying...\n");
299
300 while(size) {
301 uint32_t chunk;
302
303 chunk = sizeof(vbuf);
304 if (chunk > size)
305 chunk = size;
306 rc = flash_read(c, dst, vbuf, chunk);
307 if (rc) return rc;
308 if (memcmp(vbuf, src, chunk)) {
309 FL_ERR("LIBFLASH: Miscompare at 0x%08x\n", dst);
310 return FLASH_ERR_VERIFY_FAILURE;
311 }
312 dst += chunk;
313 src += chunk;
314 size -= chunk;
315 }
316 return 0;
317}
318
319enum sm_comp_res {
320 sm_no_change,
321 sm_need_write,
322 sm_need_erase,
323};
324
325static enum sm_comp_res flash_smart_comp(struct flash_chip *c,
326 const void *src,
327 uint32_t offset, uint32_t size)
328{
329 uint8_t *b = c->smart_buf + offset;
330 const uint8_t *s = src;
331 bool is_same = true;
332 uint32_t i;
333
334 /* SRC DEST NEED_ERASE
335 * 0 1 0
336 * 1 1 0
337 * 0 0 0
338 * 1 0 1
339 */
340 for (i = 0; i < size; i++) {
341 /* Any bit need to be set, need erase */
342 if (s[i] & ~b[i])
343 return sm_need_erase;
344 if (is_same && (b[i] != s[i]))
345 is_same = false;
346 }
347 return is_same ? sm_no_change : sm_need_write;
348}
349
350int flash_smart_write(struct flash_chip *c, uint32_t dst, const void *src,
351 uint32_t size)
352{
353 uint32_t er_size = c->min_erase_mask + 1;
354 uint32_t end = dst + size;
355 int rc;
356
357 /* Some sanity checking */
358 if (end <= dst || !size || end > c->tsize) {
359 FL_DBG("LIBFLASH: Smart write param error\n");
360 return FLASH_ERR_PARM_ERROR;
361 }
362
363 FL_DBG("LIBFLASH: Smart writing to 0x%08x..0%08x...\n",
364 dst, dst + size);
365
366 /* As long as we have something to write ... */
367 while(dst < end) {
368 uint32_t page, off, chunk;
369 enum sm_comp_res sr;
370
371 /* Figure out which erase page we are in and read it */
372 page = dst & ~c->min_erase_mask;
373 off = dst & c->min_erase_mask;
374 FL_DBG("LIBFLASH: reading page 0x%08x..0x%08x...",
375 page, page + er_size);
376 rc = flash_read(c, page, c->smart_buf, er_size);
377 if (rc) {
378 FL_DBG(" error %d!\n", rc);
379 return rc;
380 }
381
382 /* Locate the chunk of data we are working on */
383 chunk = er_size - off;
384 if (size < chunk)
385 chunk = size;
386
387 /* Compare against what we are writing and ff */
388 sr = flash_smart_comp(c, src, off, chunk);
389 switch(sr) {
390 case sm_no_change:
391 /* Identical, skip it */
392 FL_DBG(" same !\n");
393 break;
394 case sm_need_write:
395 /* Just needs writing over */
396 FL_DBG(" need write !\n");
397 rc = flash_write(c, dst, src, chunk, true);
398 if (rc) {
399 FL_DBG("LIBFLASH: Write error %d !\n", rc);
400 return rc;
401 }
402 break;
403 case sm_need_erase:
404 FL_DBG(" need erase !\n");
405 rc = flash_erase(c, page, er_size);
406 if (rc) {
407 FL_DBG("LIBFLASH: erase error %d !\n", rc);
408 return rc;
409 }
410 /* Then update the portion of the buffer and write the block */
411 memcpy(c->smart_buf + off, src, chunk);
412 rc = flash_write(c, page, c->smart_buf, er_size, true);
413 if (rc) {
414 FL_DBG("LIBFLASH: write error %d !\n", rc);
415 return rc;
416 }
417 break;
418 }
419 dst += chunk;
420 src += chunk;
421 size -= chunk;
422 }
423 return 0;
424}
425
426static int fl_chip_id(struct spi_flash_ctrl *ct, uint8_t *id_buf,
427 uint32_t *id_size)
428{
429 int rc;
430 uint8_t stat;
431
432 /* Check initial status */
433 rc = fl_read_stat(ct, &stat);
434 if (rc)
435 return rc;
436
437 /* If stuck writing, wait for idle */
438 if (stat & STAT_WIP) {
439 FL_ERR("LIBFLASH: Flash in writing state ! Waiting...\n");
440 rc = fl_sync_wait_idle(ct);
441 if (rc)
442 return rc;
443 } else
444 FL_DBG("LIBFLASH: Init status: %02x\n", stat);
445
446 /* Fallback to get ID manually */
447 rc = ct->cmd_rd(ct, CMD_RDID, false, 0, id_buf, 3);
448 if (rc)
449 return rc;
450 *id_size = 3;
451
452 return 0;
453}
454
455static int flash_identify(struct flash_chip *c)
456{
457 struct spi_flash_ctrl *ct = c->ctrl;
458 const struct flash_info *info;
459 uint32_t iid, id_size;
460#define MAX_ID_SIZE 16
461 uint8_t id[MAX_ID_SIZE];
462 int rc, i;
463
464 if (ct->chip_id) {
465 /* High level controller interface */
466 id_size = MAX_ID_SIZE;
467 rc = ct->chip_id(ct, id, &id_size);
468 } else
469 rc = fl_chip_id(ct, id, &id_size);
470 if (rc)
471 return rc;
472 if (id_size < 3)
473 return FLASH_ERR_CHIP_UNKNOWN;
474
475 /* Convert to a dword for lookup */
476 iid = id[0];
477 iid = (iid << 8) | id[1];
478 iid = (iid << 8) | id[2];
479
480 FL_DBG("LIBFLASH: Flash ID: %02x.%02x.%02x (%06x)\n",
481 id[0], id[1], id[2], iid);
482
483 /* Lookup in flash_info */
484 for (i = 0; i < ARRAY_SIZE(flash_info); i++) {
485 info = &flash_info[i];
486 if (info->id == iid)
487 break;
488 }
489 if (info->id != iid)
490 return FLASH_ERR_CHIP_UNKNOWN;
491
492 c->info = *info;
493 c->tsize = info->size;
494 ct->finfo = &c->info;
495
496 /*
497 * Let controller know about our settings and possibly
498 * override them
499 */
500 if (ct->setup) {
501 rc = ct->setup(ct, &c->tsize);
502 if (rc)
503 return rc;
504 }
505
506 /* Calculate min erase granularity */
507 if (c->info.flags & FL_ERASE_4K)
508 c->min_erase_mask = 0xfff;
509 else if (c->info.flags & FL_ERASE_32K)
510 c->min_erase_mask = 0x7fff;
511 else if (c->info.flags & FL_ERASE_64K)
512 c->min_erase_mask = 0xffff;
513 else {
514 /* No erase size ? oops ... */
515 FL_ERR("LIBFLASH: No erase sizes !\n");
516 return FLASH_ERR_CTRL_CONFIG_MISMATCH;
517 }
518
519 FL_DBG("LIBFLASH: Found chip %s size %dM erase granule: %dK\n",
520 c->info.name, c->tsize >> 20, (c->min_erase_mask + 1) >> 10);
521
522 return 0;
523}
524
525static int flash_set_4b(struct flash_chip *c, bool enable)
526{
527 struct spi_flash_ctrl *ct = c->ctrl;
528 int rc;
529
530 /* Some flash chips want this */
531 rc = fl_wren(ct);
532 if (rc) {
533 FL_ERR("LIBFLASH: Error %d enabling write for set_4b\n", rc);
534 /* Ignore the error & move on (could be wrprotect chip) */
535 }
536
537 /* Ignore error in case chip is write protected */
538
539 return ct->cmd_wr(ct, enable ? CMD_EN4B : CMD_EX4B, false, 0, NULL, 0);
540}
541
542int flash_force_4b_mode(struct flash_chip *c, bool enable_4b)
543{
544 struct spi_flash_ctrl *ct = c->ctrl;
545 int rc;
546
547 /*
548 * We only allow force 4b if both controller and flash do 4b
549 * as this is mainly used if a 3rd party tries to directly
550 * access a direct mapped read region
551 */
552 if (enable_4b && !((c->info.flags & FL_CAN_4B) && ct->set_4b))
553 return FLASH_ERR_4B_NOT_SUPPORTED;
554
555 /* Only send to flash directly on controllers that implement
556 * the low level callbacks
557 */
558 if (ct->cmd_wr) {
559 rc = flash_set_4b(c, enable_4b);
560 if (rc)
561 return rc;
562 }
563
564 /* Then inform the controller */
565 if (ct->set_4b)
566 rc = ct->set_4b(ct, enable_4b);
567 return rc;
568}
569
570static int flash_configure(struct flash_chip *c)
571{
572 struct spi_flash_ctrl *ct = c->ctrl;
573 int rc;
574
575 /* Crop flash size if necessary */
576 if (c->tsize > 0x01000000 && !(c->info.flags & FL_CAN_4B)) {
577 FL_ERR("LIBFLASH: Flash chip cropped to 16M, no 4b mode\n");
578 c->tsize = 0x01000000;
579 }
580
581 /* If flash chip > 16M, enable 4b mode */
582 if (c->tsize > 0x01000000) {
583 FL_DBG("LIBFLASH: Flash >16MB, enabling 4B mode...\n");
584
585 /* Set flash to 4b mode if we can */
586 if (ct->cmd_wr) {
587 rc = flash_set_4b(c, true);
588 if (rc) {
589 FL_ERR("LIBFLASH: Failed to set flash 4b mode\n");
590 return rc;
591 }
592 }
593
594
595 /* Set controller to 4b mode if supported */
596 if (ct->set_4b) {
597 FL_DBG("LIBFLASH: Enabling controller 4B mode...\n");
598 rc = ct->set_4b(ct, true);
599 if (rc) {
600 FL_ERR("LIBFLASH: Failed"
601 " to set controller 4b mode\n");
602 return rc;
603 }
604 }
605 } else {
606 FL_DBG("LIBFLASH: Flash <=16MB, disabling 4B mode...\n");
607
608 /*
609 * If flash chip supports 4b mode, make sure we disable
610 * it in case it was left over by the previous user
611 */
612 if (c->info.flags & FL_CAN_4B) {
613 rc = flash_set_4b(c, false);
614 if (rc) {
615 FL_ERR("LIBFLASH: Failed to"
616 " clear flash 4b mode\n");
617 return rc;
618 }
619 }
620
621 /* Set controller to 3b mode if mode switch is supported */
622 if (ct->set_4b) {
623 FL_DBG("LIBFLASH: Disabling controller 4B mode...\n");
624 rc = ct->set_4b(ct, false);
625 if (rc) {
626 FL_ERR("LIBFLASH: Failed to"
627 " clear controller 4b mode\n");
628 return rc;
629 }
630 }
631 }
632 return 0;
633}
634
635int flash_get_info(struct flash_chip *chip, const char **name,
636 uint32_t *total_size, uint32_t *erase_granule)
637{
638 if (name)
639 *name = chip->info.name;
640 if (total_size)
641 *total_size = chip->tsize;
642 if (erase_granule)
643 *erase_granule = chip->min_erase_mask + 1;
644 return 0;
645}
646
647int flash_init(struct spi_flash_ctrl *ctrl, struct flash_chip **flash)
648{
649 struct flash_chip *c;
650 int rc;
651
652 *flash = NULL;
653 c = malloc(sizeof(struct flash_chip));
654 if (!c)
655 return FLASH_ERR_MALLOC_FAILED;
656 memset(c, 0, sizeof(*c));
657 c->ctrl = ctrl;
658
659 rc = flash_identify(c);
660 if (rc) {
661 FL_ERR("LIBFLASH: Flash identification failed\n");
662 goto bail;
663 }
664 c->smart_buf = malloc(c->min_erase_mask + 1);
665 if (!c->smart_buf) {
666 FL_ERR("LIBFLASH: Failed to allocate smart buffer !\n");
667 rc = FLASH_ERR_MALLOC_FAILED;
668 goto bail;
669 }
670 rc = flash_configure(c);
671 if (rc)
672 FL_ERR("LIBFLASH: Flash configuration failed\n");
673 bail:
674 if (rc) {
675 free(c);
676 return rc;
677 }
678 *flash = c;
679 return 0;
680}
681
682void flash_exit(struct flash_chip *chip)
683{
684 /* XXX Make sure we are idle etc... */
685 free(chip);
686}
687