blob: b147ce77efd8a3eb075a951f24c12e95a118456b [file] [log] [blame]
Mike Frysinger9ce7e532008-02-19 00:58:13 -05001/*
2 * SPI flash driver
3 *
4 * Enter bugs at http://blackfin.uclinux.org/
5 *
Mike Frysinger6e87ea02008-10-11 22:47:34 -04006 * Copyright (c) 2005-2008 Analog Devices Inc.
Mike Frysinger9ce7e532008-02-19 00:58:13 -05007 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/* Configuration options:
12 * CONFIG_SPI_BAUD - value to load into SPI_BAUD (divisor of SCLK to get SPI CLK)
13 * CONFIG_SPI_FLASH_SLOW_READ - force usage of the slower read
14 * WARNING: make sure your SCLK + SPI_BAUD is slow enough
15 */
16
17#include <common.h>
18#include <malloc.h>
19#include <asm/io.h>
20#include <asm/mach-common/bits/spi.h>
21
22/* Forcibly phase out these */
23#ifdef CONFIG_SPI_FLASH_NUM_SECTORS
24# error do not set CONFIG_SPI_FLASH_NUM_SECTORS
25#endif
26#ifdef CONFIG_SPI_FLASH_SECTOR_SIZE
27# error do not set CONFIG_SPI_FLASH_SECTOR_SIZE
28#endif
29
30#if defined(CONFIG_SPI)
31
32struct flash_info {
33 char *name;
34 uint16_t id;
35 unsigned sector_size;
36 unsigned num_sectors;
37};
38
39/* SPI Speeds: 50 MHz / 33 MHz */
40static struct flash_info flash_spansion_serial_flash[] = {
41 { "S25FL016", 0x0215, 64 * 1024, 32 },
42 { "S25FL032", 0x0216, 64 * 1024, 64 },
43 { "S25FL064", 0x0217, 64 * 1024, 128 },
44 { "S25FL0128", 0x0218, 256 * 1024, 64 },
45 { NULL, 0, 0, 0 }
46};
47
48/* SPI Speeds: 50 MHz / 20 MHz */
49static struct flash_info flash_st_serial_flash[] = {
50 { "m25p05", 0x2010, 32 * 1024, 2 },
51 { "m25p10", 0x2011, 32 * 1024, 4 },
52 { "m25p20", 0x2012, 64 * 1024, 4 },
53 { "m25p40", 0x2013, 64 * 1024, 8 },
54 { "m25p16", 0x2015, 64 * 1024, 32 },
55 { "m25p32", 0x2016, 64 * 1024, 64 },
56 { "m25p64", 0x2017, 64 * 1024, 128 },
57 { "m25p128", 0x2018, 256 * 1024, 64 },
58 { NULL, 0, 0, 0 }
59};
60
61/* SPI Speeds: 66 MHz / 33 MHz */
62static struct flash_info flash_atmel_dataflash[] = {
63 { "AT45DB011x", 0x0c, 264, 512 },
64 { "AT45DB021x", 0x14, 264, 1025 },
65 { "AT45DB041x", 0x1c, 264, 2048 },
66 { "AT45DB081x", 0x24, 264, 4096 },
67 { "AT45DB161x", 0x2c, 528, 4096 },
68 { "AT45DB321x", 0x34, 528, 8192 },
69 { "AT45DB642x", 0x3c, 1056, 8192 },
70 { NULL, 0, 0, 0 }
71};
72
73/* SPI Speed: 50 MHz / 25 MHz or 40 MHz / 20 MHz */
74static struct flash_info flash_winbond_serial_flash[] = {
75 { "W25X10", 0x3011, 16 * 256, 32 },
76 { "W25X20", 0x3012, 16 * 256, 64 },
77 { "W25X40", 0x3013, 16 * 256, 128 },
78 { "W25X80", 0x3014, 16 * 256, 256 },
79 { "W25P80", 0x2014, 256 * 256, 16 },
80 { "W25P16", 0x2015, 256 * 256, 32 },
81 { NULL, 0, 0, 0 }
82};
83
84struct flash_ops {
85 uint8_t read, write, erase, status;
86};
87
88#ifdef CONFIG_SPI_FLASH_SLOW_READ
89# define OP_READ 0x03
90#else
91# define OP_READ 0x0B
92#endif
93static struct flash_ops flash_st_ops = {
94 .read = OP_READ,
95 .write = 0x02,
96 .erase = 0xD8,
97 .status = 0x05,
98};
99
100static struct flash_ops flash_atmel_ops = {
101 .read = OP_READ,
102 .write = 0x82,
103 .erase = 0x81,
104 .status = 0xD7,
105};
106
107static struct flash_ops flash_winbond_ops = {
108 .read = OP_READ,
109 .write = 0x02,
110 .erase = 0x20,
111 .status = 0x05,
112};
113
114struct manufacturer_info {
115 const char *name;
116 uint8_t id;
117 struct flash_info *flashes;
118 struct flash_ops *ops;
119};
120
121static struct {
122 struct manufacturer_info *manufacturer;
123 struct flash_info *flash;
124 struct flash_ops *ops;
125 uint8_t manufacturer_id, device_id1, device_id2;
126 unsigned int write_length;
127 unsigned long sector_size, num_sectors;
128} flash;
129
130enum {
131 JED_MANU_SPANSION = 0x01,
132 JED_MANU_ST = 0x20,
133 JED_MANU_ATMEL = 0x1F,
134 JED_MANU_WINBOND = 0xEF,
135};
136
137static struct manufacturer_info flash_manufacturers[] = {
138 {
139 .name = "Spansion",
140 .id = JED_MANU_SPANSION,
141 .flashes = flash_spansion_serial_flash,
142 .ops = &flash_st_ops,
143 },
144 {
145 .name = "ST",
146 .id = JED_MANU_ST,
147 .flashes = flash_st_serial_flash,
148 .ops = &flash_st_ops,
149 },
150 {
151 .name = "Atmel",
152 .id = JED_MANU_ATMEL,
153 .flashes = flash_atmel_dataflash,
154 .ops = &flash_atmel_ops,
155 },
156 {
157 .name = "Winbond",
158 .id = JED_MANU_WINBOND,
159 .flashes = flash_winbond_serial_flash,
160 .ops = &flash_winbond_ops,
161 },
162};
163
164#define TIMEOUT 5000 /* timeout of 5 seconds */
165
Mike Frysinger6e87ea02008-10-11 22:47:34 -0400166/* If part has multiple SPI flashes, assume SPI0 as that is
167 * the one we can boot off of ...
168 */
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500169#ifndef pSPI_CTL
170# define pSPI_CTL pSPI0_CTL
171# define pSPI_BAUD pSPI0_BAUD
172# define pSPI_FLG pSPI0_FLG
173# define pSPI_RDBR pSPI0_RDBR
174# define pSPI_STAT pSPI0_STAT
175# define pSPI_TDBR pSPI0_TDBR
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500176#endif
177
178/* Default to the SPI SSEL that we boot off of:
179 * BF54x, BF537, (everything new?): SSEL1
Mike Frysinger6e87ea02008-10-11 22:47:34 -0400180 * BF51x, BF533, BF561: SSEL2
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500181 */
182#ifndef CONFIG_SPI_FLASH_SSEL
Mike Frysinger362c9432008-04-09 02:27:06 -0400183# if defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
Mike Frysinger6e87ea02008-10-11 22:47:34 -0400184 defined(__ADSPBF538__) || defined(__ADSPBF539__) || defined(__ADSPBF561__) || \
185 defined(__ADSPBF51x__)
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500186# define CONFIG_SPI_FLASH_SSEL 2
187# else
188# define CONFIG_SPI_FLASH_SSEL 1
189# endif
190#endif
191#define SSEL_MASK (1 << CONFIG_SPI_FLASH_SSEL)
192
193static void SPI_INIT(void)
194{
195 /* [#3541] This delay appears to be necessary, but not sure
196 * exactly why as the history behind it is non-existant.
197 */
198 udelay(CONFIG_CCLK_HZ / 25000000);
199
200 /* enable SPI pins: SSEL, MOSI, MISO, SCK */
201#ifdef __ADSPBF54x__
Mike Frysinger6e87ea02008-10-11 22:47:34 -0400202 *pPORTE_FER |= (PE0 | PE1 | PE2 | PE4);
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500203#elif defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
204 *pPORTF_FER |= (PF10 | PF11 | PF12 | PF13);
205#elif defined(__ADSPBF52x__)
206 bfin_write_PORTG_MUX((bfin_read_PORTG_MUX() & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_3);
207 bfin_write_PORTG_FER(bfin_read_PORTG_FER() | PG1 | PG2 | PG3 | PG4);
Mike Frysinger6e87ea02008-10-11 22:47:34 -0400208#elif defined(__ADSPBF51x__)
209 bfin_write_PORTG_MUX((bfin_read_PORTG_MUX() & ~PORT_x_MUX_7_MASK) | PORT_x_MUX_7_FUNC_1);
210 bfin_write_PORTG_FER(bfin_read_PORTG_FER() | PG12 | PG13 | PG14 | PG15);
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500211#endif
212
213 /* initate communication upon write of TDBR */
214 *pSPI_CTL = (SPE|MSTR|CPHA|CPOL|0x01);
215 *pSPI_BAUD = CONFIG_SPI_BAUD;
216}
217
218static void SPI_DEINIT(void)
219{
220 /* put SPI settings back to reset state */
221 *pSPI_CTL = 0x0400;
222 *pSPI_BAUD = 0;
223 SSYNC();
224}
225
226static void SPI_ON(void)
227{
228 /* toggle SSEL to reset the device so it'll take a new command */
229 *pSPI_FLG = 0xFF00 | SSEL_MASK;
230 SSYNC();
231
232 *pSPI_FLG = ((0xFF & ~SSEL_MASK) << 8) | SSEL_MASK;
233 SSYNC();
234}
235
236static void SPI_OFF(void)
237{
238 /* put SPI settings back to reset state */
239 *pSPI_FLG = 0xFF00;
240 SSYNC();
241}
242
243static uint8_t spi_write_read_byte(uint8_t transmit)
244{
245 *pSPI_TDBR = transmit;
246 SSYNC();
247
248 while ((*pSPI_STAT & TXS))
249 if (ctrlc())
250 break;
251 while (!(*pSPI_STAT & SPIF))
252 if (ctrlc())
253 break;
254 while (!(*pSPI_STAT & RXS))
255 if (ctrlc())
256 break;
257
258 /* Read dummy to empty the receive register */
259 return *pSPI_RDBR;
260}
261
262static uint8_t read_status_register(void)
263{
264 uint8_t status_register;
265
266 /* send instruction to read status register */
267 SPI_ON();
268 spi_write_read_byte(flash.ops->status);
269 /* send dummy to receive the status register */
270 status_register = spi_write_read_byte(0);
271 SPI_OFF();
272
273 return status_register;
274}
275
276static int wait_for_ready_status(void)
277{
278 ulong start = get_timer(0);
279
280 while (get_timer(0) - start < TIMEOUT) {
281 switch (flash.manufacturer_id) {
282 case JED_MANU_SPANSION:
283 case JED_MANU_ST:
284 case JED_MANU_WINBOND:
285 if (!(read_status_register() & 0x01))
286 return 0;
287 break;
288
289 case JED_MANU_ATMEL:
290 if (read_status_register() & 0x80)
291 return 0;
292 break;
293 }
294
295 if (ctrlc()) {
296 puts("\nAbort\n");
297 return -1;
298 }
299 }
300
301 puts("Timeout\n");
302 return -1;
303}
304
305/* Request and read the manufacturer and device id of parts which
306 * are compatible with the JEDEC standard (JEP106) and use that to
307 * setup other operating conditions.
308 */
309static int spi_detect_part(void)
310{
311 uint16_t dev_id;
312 size_t i;
313
314 static char called_init;
315 if (called_init)
316 return 0;
317
318 SPI_ON();
319
320 /* Send the request for the part identification */
321 spi_write_read_byte(0x9F);
322
323 /* Now read in the manufacturer id bytes */
324 do {
325 flash.manufacturer_id = spi_write_read_byte(0);
326 if (flash.manufacturer_id == 0x7F)
327 puts("Warning: unhandled manufacturer continuation byte!\n");
328 } while (flash.manufacturer_id == 0x7F);
329
330 /* Now read in the first device id byte */
331 flash.device_id1 = spi_write_read_byte(0);
332
333 /* Now read in the second device id byte */
334 flash.device_id2 = spi_write_read_byte(0);
335
336 SPI_OFF();
337
338 dev_id = (flash.device_id1 << 8) | flash.device_id2;
339
340 for (i = 0; i < ARRAY_SIZE(flash_manufacturers); ++i) {
341 if (flash.manufacturer_id == flash_manufacturers[i].id)
342 break;
343 }
344 if (i == ARRAY_SIZE(flash_manufacturers))
345 goto unknown;
346
347 flash.manufacturer = &flash_manufacturers[i];
348 flash.ops = flash_manufacturers[i].ops;
349
350 switch (flash.manufacturer_id) {
351 case JED_MANU_SPANSION:
352 case JED_MANU_ST:
353 case JED_MANU_WINBOND:
354 for (i = 0; flash.manufacturer->flashes[i].name; ++i) {
355 if (dev_id == flash.manufacturer->flashes[i].id)
356 break;
357 }
358 if (!flash.manufacturer->flashes[i].name)
359 goto unknown;
360
361 flash.flash = &flash.manufacturer->flashes[i];
362 flash.sector_size = flash.flash->sector_size;
363 flash.num_sectors = flash.flash->num_sectors;
364 flash.write_length = 256;
365 break;
366
367 case JED_MANU_ATMEL: {
368 uint8_t status = read_status_register();
369
370 for (i = 0; flash.manufacturer->flashes[i].name; ++i) {
371 if ((status & 0x3c) == flash.manufacturer->flashes[i].id)
372 break;
373 }
374 if (!flash.manufacturer->flashes[i].name)
375 goto unknown;
376
377 flash.flash = &flash.manufacturer->flashes[i];
378 flash.sector_size = flash.flash->sector_size;
379 flash.num_sectors = flash.flash->num_sectors;
380
381 /* see if flash is in "power of 2" mode */
382 if (status & 0x1)
383 flash.sector_size &= ~(1 << (ffs(flash.sector_size) - 1));
384
385 flash.write_length = flash.sector_size;
386 break;
387 }
388 }
389
390 called_init = 1;
391 return 0;
392
393 unknown:
394 printf("Unknown SPI device: 0x%02X 0x%02X 0x%02X\n",
395 flash.manufacturer_id, flash.device_id1, flash.device_id2);
396 return 1;
397}
398
399/*
400 * Function: spi_init_f
401 * Description: Init SPI-Controller (ROM part)
402 * return: ---
403 */
404void spi_init_f(void)
405{
406}
407
408/*
409 * Function: spi_init_r
410 * Description: Init SPI-Controller (RAM part) -
411 * The malloc engine is ready and we can move our buffers to
412 * normal RAM
413 * return: ---
414 */
415void spi_init_r(void)
416{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200417#if defined(CONFIG_POST) && (CONFIG_POST & CONFIG_SYS_POST_SPI)
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500418 /* Our testing strategy here is pretty basic:
419 * - fill src memory with an 8-bit pattern
420 * - write the src memory to the SPI flash
421 * - read the SPI flash into the dst memory
422 * - compare src and dst memory regions
423 * - repeat a few times
424 * The variations we test for:
425 * - change the 8-bit pattern a bit
426 * - change the read/write block size so we know:
427 * - writes smaller/equal/larger than the buffer work
428 * - writes smaller/equal/larger than the sector work
429 * - change the SPI offsets so we know:
430 * - writing partial sectors works
431 */
432 uint8_t *mem_src, *mem_dst;
433 size_t i, c, l, o;
434 size_t test_count, errors;
435 uint8_t pattern;
436
437 SPI_INIT();
438
439 if (spi_detect_part())
440 goto out;
441 eeprom_info();
442
443 ulong lengths[] = {
444 flash.write_length,
445 flash.write_length * 2,
446 flash.write_length / 2,
447 flash.sector_size,
448 flash.sector_size * 2,
449 flash.sector_size / 2
450 };
451 ulong offsets[] = {
452 0,
453 flash.write_length,
454 flash.write_length * 2,
455 flash.write_length / 2,
456 flash.write_length / 4,
457 flash.sector_size,
458 flash.sector_size * 2,
459 flash.sector_size / 2,
460 flash.sector_size / 4,
461 };
462
463 /* the exact addresses are arbitrary ... they just need to not overlap */
464 mem_src = (void *)(0);
465 mem_dst = (void *)(max(flash.write_length, flash.sector_size) * 2);
466
467 test_count = 0;
468 errors = 0;
469 pattern = 0x00;
470
471 for (i = 0; i < 16; ++i) { /* 16 = 8 bits * 2 iterations */
472 for (l = 0; l < ARRAY_SIZE(lengths); ++l) {
473 for (o = 0; o < ARRAY_SIZE(offsets); ++o) {
474 ulong len = lengths[l];
475 ulong off = offsets[o];
476
477 printf("Testing pattern 0x%02X of length %5lu and offset %5lu: ", pattern, len, off);
478
479 /* setup the source memory region */
480 memset(mem_src, pattern, len);
481
482 test_count += 4;
483 for (c = 0; c < 4; ++c) { /* 4 is just a random repeat count */
484 if (ctrlc()) {
485 puts("\nAbort\n");
486 goto out;
487 }
488
489 /* make sure background fill pattern != pattern */
490 memset(mem_dst, pattern ^ 0xFF, len);
491
492 /* write out the source memory and then read it back and compare */
493 eeprom_write(0, off, mem_src, len);
494 eeprom_read(0, off, mem_dst, len);
495
496 if (memcmp(mem_src, mem_dst, len)) {
497 for (c = 0; c < len; ++c)
498 if (mem_src[c] != mem_dst[c])
499 break;
500 printf(" FAIL @ offset %u, skipping repeats ", c);
501 ++errors;
502 break;
503 }
504
505 /* XXX: should shrink write region here to test with
506 * leading/trailing canaries so we know surrounding
507 * bytes don't get screwed.
508 */
509 }
510 puts("\n");
511 }
512 }
513
514 /* invert the pattern every other run and shift out bits slowly */
515 pattern ^= 0xFF;
516 if (i % 2)
517 pattern = (pattern | 0x01) << 1;
518 }
519
520 if (errors)
521 printf("SPI FAIL: Out of %i tests, there were %i errors ;(\n", test_count, errors);
522 else
523 printf("SPI PASS: %i tests worked!\n", test_count);
524
525 out:
526 SPI_DEINIT();
527
528#endif
529}
530
531static void transmit_address(uint32_t addr)
532{
533 /* Send the highest byte of the 24 bit address at first */
534 spi_write_read_byte(addr >> 16);
535 /* Send the middle byte of the 24 bit address at second */
536 spi_write_read_byte(addr >> 8);
537 /* Send the lowest byte of the 24 bit address finally */
538 spi_write_read_byte(addr);
539}
540
541/*
542 * Read a value from flash for verify purpose
543 * Inputs: unsigned long ulStart - holds the SPI start address
544 * int pnData - pointer to store value read from flash
545 * long lCount - number of elements to read
546 */
547static int read_flash(unsigned long address, long count, uchar *buffer)
548{
549 size_t i;
550
551 /* Send the read command to SPI device */
552 SPI_ON();
553 spi_write_read_byte(flash.ops->read);
554 transmit_address(address);
555
556#ifndef CONFIG_SPI_FLASH_SLOW_READ
557 /* Send dummy byte when doing SPI fast reads */
558 spi_write_read_byte(0);
559#endif
560
561 /* After the SPI device address has been placed on the MOSI pin the data can be */
562 /* received on the MISO pin. */
563 for (i = 1; i <= count; ++i) {
564 *buffer++ = spi_write_read_byte(0);
565 if (i % flash.sector_size == 0)
566 puts(".");
567 }
568
569 SPI_OFF();
570
571 return 0;
572}
573
574static int enable_writing(void)
575{
576 ulong start;
577
578 if (flash.manufacturer_id == JED_MANU_ATMEL)
579 return 0;
580
581 /* A write enable instruction must previously have been executed */
582 SPI_ON();
583 spi_write_read_byte(0x06);
584 SPI_OFF();
585
586 /* The status register will be polled to check the write enable latch "WREN" */
587 start = get_timer(0);
588 while (get_timer(0) - start < TIMEOUT) {
589 if (read_status_register() & 0x02)
590 return 0;
591
592 if (ctrlc()) {
593 puts("\nAbort\n");
594 return -1;
595 }
596 }
597
598 puts("Timeout\n");
599 return -1;
600}
601
602static long address_to_sector(unsigned long address)
603{
604 if (address > (flash.num_sectors * flash.sector_size) - 1)
605 return -1;
606 return address / flash.sector_size;
607}
608
609static int erase_sector(int address)
610{
611 /* sector gets checked in higher function, so assume it's valid
612 * here and figure out the offset of the sector in flash
613 */
614 if (enable_writing())
615 return -1;
616
617 /*
618 * Send the erase block command to the flash followed by the 24 address
619 * to point to the start of a sector
620 */
621 SPI_ON();
622 spi_write_read_byte(flash.ops->erase);
623 transmit_address(address);
624 SPI_OFF();
625
626 return wait_for_ready_status();
627}
628
629/* Write [count] bytes out of [buffer] into the given SPI [address] */
630static long write_flash(unsigned long address, long count, uchar *buffer)
631{
632 long i, write_buffer_size;
633
634 if (enable_writing())
635 return -1;
636
637 /* Send write command followed by the 24 bit address */
638 SPI_ON();
639 spi_write_read_byte(flash.ops->write);
640 transmit_address(address);
641
642 /* Shoot out a single write buffer */
643 write_buffer_size = min(count, flash.write_length);
644 for (i = 0; i < write_buffer_size; ++i)
645 spi_write_read_byte(buffer[i]);
646
647 SPI_OFF();
648
649 /* Wait for the flash to do its thing */
650 if (wait_for_ready_status()) {
651 puts("SPI Program Time out! ");
652 return -1;
653 }
654
655 return i;
656}
657
658/* Write [count] bytes out of [buffer] into the given SPI [address] */
659static int write_sector(unsigned long address, long count, uchar *buffer)
660{
661 long write_cnt;
662
663 while (count != 0) {
664 write_cnt = write_flash(address, count, buffer);
665 if (write_cnt == -1)
666 return -1;
667
668 /* Now that we've sent some bytes out to the flash, update
669 * our counters a bit
670 */
671 count -= write_cnt;
672 address += write_cnt;
673 buffer += write_cnt;
674 }
675
676 /* return the appropriate error code */
677 return 0;
678}
679
680/*
681 * Function: spi_write
682 */
683ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
684{
685 unsigned long offset;
686 int start_sector, end_sector;
687 int start_byte, end_byte;
688 uchar *temp = NULL;
689 int num, ret = 0;
690
691 SPI_INIT();
692
693 if (spi_detect_part())
694 goto out;
695
696 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
697
698 /* Get the start block number */
699 start_sector = address_to_sector(offset);
700 if (start_sector == -1) {
701 puts("Invalid sector! ");
702 goto out;
703 }
704 end_sector = address_to_sector(offset + len - 1);
705 if (end_sector == -1) {
706 puts("Invalid sector! ");
707 goto out;
708 }
709
710 /* Since flashes operate in sector units but the eeprom command
711 * operates as a continuous stream of bytes, we need to emulate
712 * the eeprom behavior. So here we read in the sector, overlay
713 * any bytes we're actually modifying, erase the sector, and
714 * then write back out the new sector.
715 */
716 temp = malloc(flash.sector_size);
717 if (!temp) {
718 puts("Malloc for sector failed! ");
719 goto out;
720 }
721
722 for (num = start_sector; num <= end_sector; num++) {
723 unsigned long address = num * flash.sector_size;
724
725 /* XXX: should add an optimization when spanning sectors:
726 * No point in reading in a sector if we're going to be
727 * clobbering the whole thing. Need to also add a test
728 * case to make sure the optimization is correct.
729 */
730 if (read_flash(address, flash.sector_size, temp)) {
731 puts("Read sector failed! ");
732 len = 0;
733 break;
734 }
735
736 start_byte = max(address, offset);
737 end_byte = address + flash.sector_size - 1;
738 if (end_byte > (offset + len))
739 end_byte = (offset + len - 1);
740
741 memcpy(temp + start_byte - address,
742 buffer + start_byte - offset,
743 end_byte - start_byte + 1);
744
745 if (erase_sector(address)) {
746 puts("Erase sector failed! ");
747 goto out;
748 }
749
750 if (write_sector(address, flash.sector_size, temp)) {
751 puts("Write sector failed! ");
752 goto out;
753 }
754
755 puts(".");
756 }
757
758 ret = len;
759
760 out:
761 free(temp);
762
763 SPI_DEINIT();
764
765 return ret;
766}
767
768/*
769 * Function: spi_read
770 */
771ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
772{
773 unsigned long offset;
774
775 SPI_INIT();
776
777 if (spi_detect_part())
778 len = 0;
779 else {
780 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
781 read_flash(offset, len, buffer);
782 }
783
784 SPI_DEINIT();
785
786 return len;
787}
788
789/*
790 * Spit out some useful information about the SPI eeprom
791 */
792int eeprom_info(void)
793{
794 int ret = 0;
795
796 SPI_INIT();
797
798 if (spi_detect_part())
799 ret = 1;
800 else
801 printf("SPI Device: %s 0x%02X (%s) 0x%02X 0x%02X\n"
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400802 "Parameters: num sectors = %lu, sector size = %lu, write size = %i\n"
803 "Flash Size: %lu mbit (%lu mbyte)\n"
Mike Frysinger9ce7e532008-02-19 00:58:13 -0500804 "Status: 0x%02X\n",
805 flash.flash->name, flash.manufacturer_id, flash.manufacturer->name,
806 flash.device_id1, flash.device_id2, flash.num_sectors,
807 flash.sector_size, flash.write_length,
808 (flash.num_sectors * flash.sector_size) >> 17,
809 (flash.num_sectors * flash.sector_size) >> 20,
810 read_status_register());
811
812 SPI_DEINIT();
813
814 return ret;
815}
816
817#endif