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