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