spi_flash.c 19 KB

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