spi_flash.c 24 KB

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