4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (c) 2005-2008 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
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
20 #include <asm/mach-common/bits/spi.h>
22 /* Forcibly phase out these */
23 #ifdef CONFIG_SPI_FLASH_NUM_SECTORS
24 # error do not set CONFIG_SPI_FLASH_NUM_SECTORS
26 #ifdef CONFIG_SPI_FLASH_SECTOR_SIZE
27 # error do not set CONFIG_SPI_FLASH_SECTOR_SIZE
30 #if defined(CONFIG_SPI)
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 },
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 },
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 },
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 },
86 uint8_t read, write, erase, status;
89 #ifdef CONFIG_SPI_FLASH_SLOW_READ
94 static struct flash_ops flash_st_ops = {
101 static struct flash_ops flash_atmel_ops = {
108 static struct flash_ops flash_winbond_ops = {
115 struct manufacturer_info {
118 struct flash_info *flashes;
119 struct flash_ops *ops;
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;
132 JED_MANU_SPANSION = 0x01,
134 JED_MANU_ATMEL = 0x1F,
135 JED_MANU_WINBOND = 0xEF,
138 static struct manufacturer_info flash_manufacturers[] = {
141 .id = JED_MANU_SPANSION,
142 .flashes = flash_spansion_serial_flash,
143 .ops = &flash_st_ops,
148 .flashes = flash_st_serial_flash,
149 .ops = &flash_st_ops,
153 .id = JED_MANU_ATMEL,
154 .flashes = flash_atmel_dataflash,
155 .ops = &flash_atmel_ops,
159 .id = JED_MANU_WINBOND,
160 .flashes = flash_winbond_serial_flash,
161 .ops = &flash_winbond_ops,
165 #define TIMEOUT 5000 /* timeout of 5 seconds */
167 /* If part has multiple SPI flashes, assume SPI0 as that is
168 * the one we can boot off of ...
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
179 /* Default to the SPI SSEL that we boot off of:
180 * BF54x, BF537, (everything new?): SSEL1
181 * BF51x, BF533, BF561: SSEL2
183 #ifndef CONFIG_SPI_FLASH_SSEL
184 # define CONFIG_SPI_FLASH_SSEL BFIN_BOOT_SPI_SSEL
186 #define SSEL_MASK (1 << CONFIG_SPI_FLASH_SSEL)
188 static void SPI_INIT(void)
190 /* [#3541] This delay appears to be necessary, but not sure
191 * exactly why as the history behind it is non-existant.
193 udelay(CONFIG_CCLK_HZ / 25000000);
195 /* enable SPI pins: SSEL, MOSI, MISO, SCK */
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);
208 /* initate communication upon write of TDBR */
209 *pSPI_CTL = (SPE|MSTR|CPHA|CPOL|0x01);
210 *pSPI_BAUD = CONFIG_SPI_BAUD;
213 static void SPI_DEINIT(void)
215 /* put SPI settings back to reset state */
221 static void SPI_ON(void)
223 /* toggle SSEL to reset the device so it'll take a new command */
224 *pSPI_FLG = 0xFF00 | SSEL_MASK;
227 *pSPI_FLG = ((0xFF & ~SSEL_MASK) << 8) | SSEL_MASK;
231 static void SPI_OFF(void)
233 /* put SPI settings back to reset state */
238 static uint8_t spi_write_read_byte(uint8_t transmit)
240 *pSPI_TDBR = transmit;
243 while ((*pSPI_STAT & TXS))
246 while (!(*pSPI_STAT & SPIF))
249 while (!(*pSPI_STAT & RXS))
253 /* Read dummy to empty the receive register */
257 static uint8_t read_status_register(void)
259 uint8_t status_register;
261 /* send instruction to read status register */
263 spi_write_read_byte(flash.ops->status);
264 /* send dummy to receive the status register */
265 status_register = spi_write_read_byte(0);
268 return status_register;
271 static int wait_for_ready_status(void)
273 ulong start = get_timer(0);
275 while (get_timer(0) - start < TIMEOUT) {
276 switch (flash.manufacturer_id) {
277 case JED_MANU_SPANSION:
279 case JED_MANU_WINBOND:
280 if (!(read_status_register() & 0x01))
285 if (read_status_register() & 0x80)
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.
304 static int spi_detect_part(void)
309 static char called_init;
313 #ifdef CONFIG_SPI_FLASH_M25P80
314 flash.manufacturer_id = JED_MANU_ST;
315 flash.device_id1 = 0x20;
316 flash.device_id2 = 0xFF;
320 /* Send the request for the part identification */
321 spi_write_read_byte(0x9F);
323 /* Now read in the manufacturer id bytes */
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);
330 /* Now read in the first device id byte */
331 flash.device_id1 = spi_write_read_byte(0);
333 /* Now read in the second device id byte */
334 flash.device_id2 = spi_write_read_byte(0);
339 dev_id = (flash.device_id1 << 8) | flash.device_id2;
341 for (i = 0; i < ARRAY_SIZE(flash_manufacturers); ++i) {
342 if (flash.manufacturer_id == flash_manufacturers[i].id)
345 if (i == ARRAY_SIZE(flash_manufacturers))
348 flash.manufacturer = &flash_manufacturers[i];
349 flash.ops = flash_manufacturers[i].ops;
351 switch (flash.manufacturer_id) {
352 case JED_MANU_SPANSION:
354 case JED_MANU_WINBOND:
355 for (i = 0; flash.manufacturer->flashes[i].name; ++i) {
356 if (dev_id == flash.manufacturer->flashes[i].id)
359 if (!flash.manufacturer->flashes[i].name)
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;
368 case JED_MANU_ATMEL: {
369 uint8_t status = read_status_register();
371 for (i = 0; flash.manufacturer->flashes[i].name; ++i) {
372 if ((status & 0x3c) == flash.manufacturer->flashes[i].id)
375 if (!flash.manufacturer->flashes[i].name)
378 flash.flash = &flash.manufacturer->flashes[i];
379 flash.sector_size = flash.flash->sector_size;
380 flash.num_sectors = flash.flash->num_sectors;
382 /* see if flash is in "power of 2" mode */
384 flash.sector_size &= ~(1 << (ffs(flash.sector_size) - 1));
386 flash.write_length = flash.sector_size;
395 printf("Unknown SPI device: 0x%02X 0x%02X 0x%02X\n",
396 flash.manufacturer_id, flash.device_id1, flash.device_id2);
401 * Function: spi_init_f
402 * Description: Init SPI-Controller (ROM part)
405 void spi_init_f(void)
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
416 void spi_init_r(void)
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
433 uint8_t *mem_src, *mem_dst;
435 size_t test_count, errors;
440 if (spi_detect_part())
446 flash.write_length * 2,
447 flash.write_length / 2,
449 flash.sector_size * 2,
450 flash.sector_size / 2
455 flash.write_length * 2,
456 flash.write_length / 2,
457 flash.write_length / 4,
459 flash.sector_size * 2,
460 flash.sector_size / 2,
461 flash.sector_size / 4,
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);
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];
478 printf("Testing pattern 0x%02X of length %5lu and offset %5lu: ", pattern, len, off);
480 /* setup the source memory region */
481 memset(mem_src, pattern, len);
484 for (c = 0; c < 4; ++c) { /* 4 is just a random repeat count */
490 /* make sure background fill pattern != pattern */
491 memset(mem_dst, pattern ^ 0xFF, len);
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);
497 if (memcmp(mem_src, mem_dst, len)) {
498 for (c = 0; c < len; ++c)
499 if (mem_src[c] != mem_dst[c])
501 printf(" FAIL @ offset %u, skipping repeats ", c);
506 /* XXX: should shrink write region here to test with
507 * leading/trailing canaries so we know surrounding
508 * bytes don't get screwed.
515 /* invert the pattern every other run and shift out bits slowly */
518 pattern = (pattern | 0x01) << 1;
522 printf("SPI FAIL: Out of %i tests, there were %i errors ;(\n", test_count, errors);
524 printf("SPI PASS: %i tests worked!\n", test_count);
532 static void transmit_address(uint32_t addr)
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);
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
548 static int read_flash(unsigned long address, long count, uchar *buffer)
552 /* Send the read command to SPI device */
554 spi_write_read_byte(flash.ops->read);
555 transmit_address(address);
557 #ifndef CONFIG_SPI_FLASH_SLOW_READ
558 /* Send dummy byte when doing SPI fast reads */
559 spi_write_read_byte(0);
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)
575 static int enable_writing(void)
579 if (flash.manufacturer_id == JED_MANU_ATMEL)
582 /* A write enable instruction must previously have been executed */
584 spi_write_read_byte(0x06);
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)
603 static long address_to_sector(unsigned long address)
605 if (address > (flash.num_sectors * flash.sector_size) - 1)
607 return address / flash.sector_size;
610 static int erase_sector(int address)
612 /* sector gets checked in higher function, so assume it's valid
613 * here and figure out the offset of the sector in flash
615 if (enable_writing())
619 * Send the erase block command to the flash followed by the 24 address
620 * to point to the start of a sector
623 spi_write_read_byte(flash.ops->erase);
624 transmit_address(address);
627 return wait_for_ready_status();
630 /* Write [count] bytes out of [buffer] into the given SPI [address] */
631 static long write_flash(unsigned long address, long count, uchar *buffer)
633 long i, write_buffer_size;
635 if (enable_writing())
638 /* Send write command followed by the 24 bit address */
640 spi_write_read_byte(flash.ops->write);
641 transmit_address(address);
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]);
650 /* Wait for the flash to do its thing */
651 if (wait_for_ready_status()) {
652 puts("SPI Program Time out! ");
659 /* Write [count] bytes out of [buffer] into the given SPI [address] */
660 static int write_sector(unsigned long address, long count, uchar *buffer)
665 write_cnt = write_flash(address, count, buffer);
669 /* Now that we've sent some bytes out to the flash, update
673 address += write_cnt;
677 /* return the appropriate error code */
682 * Function: spi_write
684 ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
686 unsigned long offset;
687 int start_sector, end_sector;
688 int start_byte, end_byte;
694 if (spi_detect_part())
697 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
699 /* Get the start block number */
700 start_sector = address_to_sector(offset);
701 if (start_sector == -1) {
702 puts("Invalid sector! ");
705 end_sector = address_to_sector(offset + len - 1);
706 if (end_sector == -1) {
707 puts("Invalid sector! ");
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.
717 temp = malloc(flash.sector_size);
719 puts("Malloc for sector failed! ");
723 for (num = start_sector; num <= end_sector; num++) {
724 unsigned long address = num * flash.sector_size;
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.
731 if (read_flash(address, flash.sector_size, temp)) {
732 puts("Read sector failed! ");
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);
742 memcpy(temp + start_byte - address,
743 buffer + start_byte - offset,
744 end_byte - start_byte + 1);
746 if (erase_sector(address)) {
747 puts("Erase sector failed! ");
751 if (write_sector(address, flash.sector_size, temp)) {
752 puts("Write sector failed! ");
772 ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
774 unsigned long offset;
778 if (spi_detect_part())
781 offset = addr[0] << 16 | addr[1] << 8 | addr[2];
782 read_flash(offset, len, buffer);
791 * Spit out some useful information about the SPI eeprom
793 int eeprom_info(void)
799 if (spi_detect_part())
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"
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());