spi_flash.c 19 KB

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