spi_flash.c 19 KB

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