cfi_flash.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * (C) Copyright 2002-2004
  3. * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
  4. *
  5. * Copyright (C) 2003 Arabella Software Ltd.
  6. * Yuli Barcohen <yuli@arabellasw.com>
  7. * Modified to work with AMD flashes
  8. *
  9. * Copyright (C) 2004
  10. * Ed Okerson
  11. * Modified to work with little-endian systems.
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. *
  31. * History
  32. * 01/20/2004 - combined variants of original driver.
  33. * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
  34. * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
  35. * 01/27/2004 - Little endian support Ed Okerson
  36. *
  37. * Tested Architectures
  38. * Port Width Chip Width # of banks Flash Chip Board
  39. * 32 16 1 28F128J3 seranoa/eagle
  40. * 64 16 1 28F128J3 seranoa/falcon
  41. *
  42. */
  43. /* The DEBUG define must be before common to enable debugging */
  44. /* #define DEBUG */
  45. #include <common.h>
  46. #include <asm/processor.h>
  47. #include <asm/byteorder.h>
  48. #include <linux/byteorder/swab.h>
  49. #ifdef CFG_FLASH_CFI_DRIVER
  50. /*
  51. * This file implements a Common Flash Interface (CFI) driver for U-Boot.
  52. * The width of the port and the width of the chips are determined at initialization.
  53. * These widths are used to calculate the address for access CFI data structures.
  54. * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
  55. *
  56. * References
  57. * JEDEC Standard JESD68 - Common Flash Interface (CFI)
  58. * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
  59. * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
  60. * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
  61. *
  62. * TODO
  63. *
  64. * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
  65. * Table (ALT) to determine if protection is available
  66. *
  67. * Add support for other command sets Use the PRI and ALT to determine command set
  68. * Verify erase and program timeouts.
  69. */
  70. #ifndef CFG_FLASH_BANKS_LIST
  71. #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
  72. #endif
  73. #define FLASH_CMD_CFI 0x98
  74. #define FLASH_CMD_READ_ID 0x90
  75. #define FLASH_CMD_RESET 0xff
  76. #define FLASH_CMD_BLOCK_ERASE 0x20
  77. #define FLASH_CMD_ERASE_CONFIRM 0xD0
  78. #define FLASH_CMD_WRITE 0x40
  79. #define FLASH_CMD_PROTECT 0x60
  80. #define FLASH_CMD_PROTECT_SET 0x01
  81. #define FLASH_CMD_PROTECT_CLEAR 0xD0
  82. #define FLASH_CMD_CLEAR_STATUS 0x50
  83. #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
  84. #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
  85. #define FLASH_STATUS_DONE 0x80
  86. #define FLASH_STATUS_ESS 0x40
  87. #define FLASH_STATUS_ECLBS 0x20
  88. #define FLASH_STATUS_PSLBS 0x10
  89. #define FLASH_STATUS_VPENS 0x08
  90. #define FLASH_STATUS_PSS 0x04
  91. #define FLASH_STATUS_DPS 0x02
  92. #define FLASH_STATUS_R 0x01
  93. #define FLASH_STATUS_PROTECT 0x01
  94. #define AMD_CMD_RESET 0xF0
  95. #define AMD_CMD_WRITE 0xA0
  96. #define AMD_CMD_ERASE_START 0x80
  97. #define AMD_CMD_ERASE_SECTOR 0x30
  98. #define AMD_CMD_UNLOCK_START 0xAA
  99. #define AMD_CMD_UNLOCK_ACK 0x55
  100. #define AMD_STATUS_TOGGLE 0x40
  101. #define AMD_STATUS_ERROR 0x20
  102. #define AMD_ADDR_ERASE_START 0x555
  103. #define AMD_ADDR_START 0x555
  104. #define AMD_ADDR_ACK 0x2AA
  105. #define FLASH_OFFSET_CFI 0x55
  106. #define FLASH_OFFSET_CFI_RESP 0x10
  107. #define FLASH_OFFSET_PRIMARY_VENDOR 0x13
  108. #define FLASH_OFFSET_WTOUT 0x1F
  109. #define FLASH_OFFSET_WBTOUT 0x20
  110. #define FLASH_OFFSET_ETOUT 0x21
  111. #define FLASH_OFFSET_CETOUT 0x22
  112. #define FLASH_OFFSET_WMAX_TOUT 0x23
  113. #define FLASH_OFFSET_WBMAX_TOUT 0x24
  114. #define FLASH_OFFSET_EMAX_TOUT 0x25
  115. #define FLASH_OFFSET_CEMAX_TOUT 0x26
  116. #define FLASH_OFFSET_SIZE 0x27
  117. #define FLASH_OFFSET_INTERFACE 0x28
  118. #define FLASH_OFFSET_BUFFER_SIZE 0x2A
  119. #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
  120. #define FLASH_OFFSET_ERASE_REGIONS 0x2D
  121. #define FLASH_OFFSET_PROTECT 0x02
  122. #define FLASH_OFFSET_USER_PROTECTION 0x85
  123. #define FLASH_OFFSET_INTEL_PROTECTION 0x81
  124. #define FLASH_MAN_CFI 0x01000000
  125. #define CFI_CMDSET_NONE 0
  126. #define CFI_CMDSET_INTEL_EXTENDED 1
  127. #define CFI_CMDSET_AMD_STANDARD 2
  128. #define CFI_CMDSET_INTEL_STANDARD 3
  129. #define CFI_CMDSET_AMD_EXTENDED 4
  130. #define CFI_CMDSET_MITSU_STANDARD 256
  131. #define CFI_CMDSET_MITSU_EXTENDED 257
  132. #define CFI_CMDSET_SST 258
  133. #ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
  134. # undef FLASH_CMD_RESET
  135. # define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
  136. #endif
  137. typedef union {
  138. unsigned char c;
  139. unsigned short w;
  140. unsigned long l;
  141. unsigned long long ll;
  142. } cfiword_t;
  143. typedef union {
  144. volatile unsigned char *cp;
  145. volatile unsigned short *wp;
  146. volatile unsigned long *lp;
  147. volatile unsigned long long *llp;
  148. } cfiptr_t;
  149. #define NUM_ERASE_REGIONS 4
  150. static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
  151. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  152. /*-----------------------------------------------------------------------
  153. * Functions
  154. */
  155. typedef unsigned long flash_sect_t;
  156. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
  157. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
  158. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  159. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
  160. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  161. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  162. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  163. static int flash_detect_cfi (flash_info_t * info);
  164. static ulong flash_get_size (ulong base, int banknum);
  165. static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
  166. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  167. ulong tout, char *prompt);
  168. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  169. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
  170. #endif
  171. /*-----------------------------------------------------------------------
  172. * create an address based on the offset and the port width
  173. */
  174. inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
  175. {
  176. return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
  177. }
  178. #ifdef DEBUG
  179. /*-----------------------------------------------------------------------
  180. * Debug support
  181. */
  182. void print_longlong (char *str, unsigned long long data)
  183. {
  184. int i;
  185. char *cp;
  186. cp = (unsigned char *) &data;
  187. for (i = 0; i < 8; i++)
  188. sprintf (&str[i * 2], "%2.2x", *cp++);
  189. }
  190. static void flash_printqry (flash_info_t * info, flash_sect_t sect)
  191. {
  192. cfiptr_t cptr;
  193. int x, y;
  194. for (x = 0; x < 0x40; x += 16 / info->portwidth) {
  195. cptr.cp =
  196. flash_make_addr (info, sect,
  197. x + FLASH_OFFSET_CFI_RESP);
  198. debug ("%p : ", cptr.cp);
  199. for (y = 0; y < 16; y++) {
  200. debug ("%2.2x ", cptr.cp[y]);
  201. }
  202. debug (" ");
  203. for (y = 0; y < 16; y++) {
  204. if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
  205. debug ("%c", cptr.cp[y]);
  206. } else {
  207. debug (".");
  208. }
  209. }
  210. debug ("\n");
  211. }
  212. }
  213. #endif
  214. /*-----------------------------------------------------------------------
  215. * read a character at a port width address
  216. */
  217. inline uchar flash_read_uchar (flash_info_t * info, uint offset)
  218. {
  219. uchar *cp;
  220. cp = flash_make_addr (info, 0, offset);
  221. #if defined(__LITTLE_ENDIAN)
  222. return (cp[0]);
  223. #else
  224. return (cp[info->portwidth - 1]);
  225. #endif
  226. }
  227. /*-----------------------------------------------------------------------
  228. * read a short word by swapping for ppc format.
  229. */
  230. ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
  231. {
  232. uchar *addr;
  233. ushort retval;
  234. #ifdef DEBUG
  235. int x;
  236. #endif
  237. addr = flash_make_addr (info, sect, offset);
  238. #ifdef DEBUG
  239. debug ("ushort addr is at %p info->portwidth = %d\n", addr,
  240. info->portwidth);
  241. for (x = 0; x < 2 * info->portwidth; x++) {
  242. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  243. }
  244. #endif
  245. #if defined(__LITTLE_ENDIAN)
  246. retval = ((addr[(info->portwidth)] << 8) | addr[0]);
  247. #else
  248. retval = ((addr[(2 * info->portwidth) - 1] << 8) |
  249. addr[info->portwidth - 1]);
  250. #endif
  251. debug ("retval = 0x%x\n", retval);
  252. return retval;
  253. }
  254. /*-----------------------------------------------------------------------
  255. * read a long word by picking the least significant byte of each maiximum
  256. * port size word. Swap for ppc format.
  257. */
  258. ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
  259. {
  260. uchar *addr;
  261. ulong retval;
  262. #ifdef DEBUG
  263. int x;
  264. #endif
  265. addr = flash_make_addr (info, sect, offset);
  266. #ifdef DEBUG
  267. debug ("long addr is at %p info->portwidth = %d\n", addr,
  268. info->portwidth);
  269. for (x = 0; x < 4 * info->portwidth; x++) {
  270. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  271. }
  272. #endif
  273. #if defined(__LITTLE_ENDIAN)
  274. retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
  275. (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
  276. #else
  277. retval = (addr[(2 * info->portwidth) - 1] << 24) |
  278. (addr[(info->portwidth) - 1] << 16) |
  279. (addr[(4 * info->portwidth) - 1] << 8) |
  280. addr[(3 * info->portwidth) - 1];
  281. #endif
  282. return retval;
  283. }
  284. /*-----------------------------------------------------------------------
  285. */
  286. unsigned long flash_init (void)
  287. {
  288. unsigned long size = 0;
  289. int i;
  290. /* Init: no FLASHes known */
  291. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  292. flash_info[i].flash_id = FLASH_UNKNOWN;
  293. size += flash_info[i].size = flash_get_size (bank_base[i], i);
  294. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  295. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  296. i, flash_info[i].size, flash_info[i].size << 20);
  297. }
  298. }
  299. /* Monitor protection ON by default */
  300. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  301. flash_protect (FLAG_PROTECT_SET,
  302. CFG_MONITOR_BASE,
  303. CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
  304. &flash_info[0]);
  305. #endif
  306. /* Environment protection ON by default */
  307. #ifdef CFG_ENV_IS_IN_FLASH
  308. flash_protect (FLAG_PROTECT_SET,
  309. CFG_ENV_ADDR,
  310. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  311. &flash_info[0]);
  312. #endif
  313. /* Redundant environment protection ON by default */
  314. #ifdef CFG_ENV_ADDR_REDUND
  315. flash_protect (FLAG_PROTECT_SET,
  316. CFG_ENV_ADDR_REDUND,
  317. CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
  318. &flash_info[0]);
  319. #endif
  320. return (size);
  321. }
  322. /*-----------------------------------------------------------------------
  323. */
  324. int flash_erase (flash_info_t * info, int s_first, int s_last)
  325. {
  326. int rcode = 0;
  327. int prot;
  328. flash_sect_t sect;
  329. if (info->flash_id != FLASH_MAN_CFI) {
  330. puts ("Can't erase unknown flash type - aborted\n");
  331. return 1;
  332. }
  333. if ((s_first < 0) || (s_first > s_last)) {
  334. puts ("- no sectors to erase\n");
  335. return 1;
  336. }
  337. prot = 0;
  338. for (sect = s_first; sect <= s_last; ++sect) {
  339. if (info->protect[sect]) {
  340. prot++;
  341. }
  342. }
  343. if (prot) {
  344. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  345. } else {
  346. putc ('\n');
  347. }
  348. for (sect = s_first; sect <= s_last; sect++) {
  349. if (info->protect[sect] == 0) { /* not protected */
  350. switch (info->vendor) {
  351. case CFI_CMDSET_INTEL_STANDARD:
  352. case CFI_CMDSET_INTEL_EXTENDED:
  353. flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  354. flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  355. flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  356. break;
  357. case CFI_CMDSET_AMD_STANDARD:
  358. case CFI_CMDSET_AMD_EXTENDED:
  359. flash_unlock_seq (info, sect);
  360. flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
  361. AMD_CMD_ERASE_START);
  362. flash_unlock_seq (info, sect);
  363. flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
  364. break;
  365. default:
  366. debug ("Unkown flash vendor %d\n",
  367. info->vendor);
  368. break;
  369. }
  370. if (flash_full_status_check
  371. (info, sect, info->erase_blk_tout, "erase")) {
  372. rcode = 1;
  373. } else
  374. putc ('.');
  375. }
  376. }
  377. puts (" done\n");
  378. return rcode;
  379. }
  380. /*-----------------------------------------------------------------------
  381. */
  382. void flash_print_info (flash_info_t * info)
  383. {
  384. int i;
  385. if (info->flash_id != FLASH_MAN_CFI) {
  386. puts ("missing or unknown FLASH type\n");
  387. return;
  388. }
  389. printf ("CFI conformant FLASH (%d x %d)",
  390. (info->portwidth << 3), (info->chipwidth << 3));
  391. printf (" Size: %ld MB in %d Sectors\n",
  392. info->size >> 20, info->sector_count);
  393. printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
  394. info->erase_blk_tout,
  395. info->write_tout,
  396. info->buffer_write_tout,
  397. info->buffer_size);
  398. puts (" Sector Start Addresses:");
  399. for (i = 0; i < info->sector_count; ++i) {
  400. #ifdef CFG_FLASH_EMPTY_INFO
  401. int k;
  402. int size;
  403. int erased;
  404. volatile unsigned long *flash;
  405. /*
  406. * Check if whole sector is erased
  407. */
  408. if (i != (info->sector_count - 1))
  409. size = info->start[i + 1] - info->start[i];
  410. else
  411. size = info->start[0] + info->size - info->start[i];
  412. erased = 1;
  413. flash = (volatile unsigned long *) info->start[i];
  414. size = size >> 2; /* divide by 4 for longword access */
  415. for (k = 0; k < size; k++) {
  416. if (*flash++ != 0xffffffff) {
  417. erased = 0;
  418. break;
  419. }
  420. }
  421. if ((i % 5) == 0)
  422. printf ("\n");
  423. /* print empty and read-only info */
  424. printf (" %08lX%s%s",
  425. info->start[i],
  426. erased ? " E" : " ",
  427. info->protect[i] ? "RO " : " ");
  428. #else
  429. if ((i % 5) == 0)
  430. printf ("\n ");
  431. printf (" %08lX%s",
  432. info->start[i], info->protect[i] ? " (RO) " : " ");
  433. #endif
  434. }
  435. putc ('\n');
  436. return;
  437. }
  438. /*-----------------------------------------------------------------------
  439. * Copy memory to flash, returns:
  440. * 0 - OK
  441. * 1 - write timeout
  442. * 2 - Flash not erased
  443. */
  444. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  445. {
  446. ulong wp;
  447. ulong cp;
  448. int aln;
  449. cfiword_t cword;
  450. int i, rc;
  451. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  452. int buffered_size;
  453. #endif
  454. /* get lower aligned address */
  455. /* get lower aligned address */
  456. wp = (addr & ~(info->portwidth - 1));
  457. /* handle unaligned start */
  458. if ((aln = addr - wp) != 0) {
  459. cword.l = 0;
  460. cp = wp;
  461. for (i = 0; i < aln; ++i, ++cp)
  462. flash_add_byte (info, &cword, (*(uchar *) cp));
  463. for (; (i < info->portwidth) && (cnt > 0); i++) {
  464. flash_add_byte (info, &cword, *src++);
  465. cnt--;
  466. cp++;
  467. }
  468. for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  469. flash_add_byte (info, &cword, (*(uchar *) cp));
  470. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  471. return rc;
  472. wp = cp;
  473. }
  474. /* handle the aligned part */
  475. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  476. buffered_size = (info->portwidth / info->chipwidth);
  477. buffered_size *= info->buffer_size;
  478. while (cnt >= info->portwidth) {
  479. i = buffered_size > cnt ? cnt : buffered_size;
  480. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  481. return rc;
  482. i -= (i % info->portwidth);
  483. wp += i;
  484. src += i;
  485. cnt -= i;
  486. }
  487. #else
  488. while (cnt >= info->portwidth) {
  489. cword.l = 0;
  490. for (i = 0; i < info->portwidth; i++) {
  491. flash_add_byte (info, &cword, *src++);
  492. }
  493. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  494. return rc;
  495. wp += info->portwidth;
  496. cnt -= info->portwidth;
  497. }
  498. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  499. if (cnt == 0) {
  500. return (0);
  501. }
  502. /*
  503. * handle unaligned tail bytes
  504. */
  505. cword.l = 0;
  506. for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
  507. flash_add_byte (info, &cword, *src++);
  508. --cnt;
  509. }
  510. for (; i < info->portwidth; ++i, ++cp) {
  511. flash_add_byte (info, &cword, (*(uchar *) cp));
  512. }
  513. return flash_write_cfiword (info, wp, cword);
  514. }
  515. /*-----------------------------------------------------------------------
  516. */
  517. #ifdef CFG_FLASH_PROTECTION
  518. int flash_real_protect (flash_info_t * info, long sector, int prot)
  519. {
  520. int retcode = 0;
  521. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  522. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  523. if (prot)
  524. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  525. else
  526. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  527. if ((retcode =
  528. flash_full_status_check (info, sector, info->erase_blk_tout,
  529. prot ? "protect" : "unprotect")) == 0) {
  530. info->protect[sector] = prot;
  531. /* Intel's unprotect unprotects all locking */
  532. if (prot == 0) {
  533. flash_sect_t i;
  534. for (i = 0; i < info->sector_count; i++) {
  535. if (info->protect[i])
  536. flash_real_protect (info, i, 1);
  537. }
  538. }
  539. }
  540. return retcode;
  541. }
  542. /*-----------------------------------------------------------------------
  543. * flash_read_user_serial - read the OneTimeProgramming cells
  544. */
  545. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  546. int len)
  547. {
  548. uchar *src;
  549. uchar *dst;
  550. dst = buffer;
  551. src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
  552. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  553. memcpy (dst, src + offset, len);
  554. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  555. }
  556. /*
  557. * flash_read_factory_serial - read the device Id from the protection area
  558. */
  559. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  560. int len)
  561. {
  562. uchar *src;
  563. src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  564. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  565. memcpy (buffer, src + offset, len);
  566. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  567. }
  568. #endif /* CFG_FLASH_PROTECTION */
  569. /*
  570. * flash_is_busy - check to see if the flash is busy
  571. * This routine checks the status of the chip and returns true if the chip is busy
  572. */
  573. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  574. {
  575. int retval;
  576. switch (info->vendor) {
  577. case CFI_CMDSET_INTEL_STANDARD:
  578. case CFI_CMDSET_INTEL_EXTENDED:
  579. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  580. break;
  581. case CFI_CMDSET_AMD_STANDARD:
  582. case CFI_CMDSET_AMD_EXTENDED:
  583. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  584. break;
  585. default:
  586. retval = 0;
  587. }
  588. debug ("flash_is_busy: %d\n", retval);
  589. return retval;
  590. }
  591. /*-----------------------------------------------------------------------
  592. * wait for XSR.7 to be set. Time out with an error if it does not.
  593. * This routine does not set the flash to read-array mode.
  594. */
  595. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  596. ulong tout, char *prompt)
  597. {
  598. ulong start;
  599. /* Wait for command completion */
  600. start = get_timer (0);
  601. while (flash_is_busy (info, sector)) {
  602. if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
  603. printf ("Flash %s timeout at address %lx data %lx\n",
  604. prompt, info->start[sector],
  605. flash_read_long (info, sector, 0));
  606. flash_write_cmd (info, sector, 0, info->cmd_reset);
  607. return ERR_TIMOUT;
  608. }
  609. }
  610. return ERR_OK;
  611. }
  612. /*-----------------------------------------------------------------------
  613. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  614. * This routine sets the flash to read-array mode.
  615. */
  616. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  617. ulong tout, char *prompt)
  618. {
  619. int retcode;
  620. retcode = flash_status_check (info, sector, tout, prompt);
  621. switch (info->vendor) {
  622. case CFI_CMDSET_INTEL_EXTENDED:
  623. case CFI_CMDSET_INTEL_STANDARD:
  624. if ((retcode != ERR_OK)
  625. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  626. retcode = ERR_INVAL;
  627. printf ("Flash %s error at address %lx\n", prompt,
  628. info->start[sector]);
  629. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
  630. puts ("Command Sequence Error.\n");
  631. } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
  632. puts ("Block Erase Error.\n");
  633. retcode = ERR_NOT_ERASED;
  634. } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
  635. puts ("Locking Error\n");
  636. }
  637. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  638. puts ("Block locked.\n");
  639. retcode = ERR_PROTECTED;
  640. }
  641. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  642. puts ("Vpp Low Error.\n");
  643. }
  644. flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
  645. break;
  646. default:
  647. break;
  648. }
  649. return retcode;
  650. }
  651. /*-----------------------------------------------------------------------
  652. */
  653. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  654. {
  655. #if defined(__LITTLE_ENDIAN)
  656. unsigned short w;
  657. unsigned int l;
  658. unsigned long long ll;
  659. #endif
  660. switch (info->portwidth) {
  661. case FLASH_CFI_8BIT:
  662. cword->c = c;
  663. break;
  664. case FLASH_CFI_16BIT:
  665. #if defined(__LITTLE_ENDIAN)
  666. w = c;
  667. w <<= 8;
  668. cword->w = (cword->w >> 8) | w;
  669. #else
  670. cword->w = (cword->w << 8) | c;
  671. #endif
  672. break;
  673. case FLASH_CFI_32BIT:
  674. #if defined(__LITTLE_ENDIAN)
  675. l = c;
  676. l <<= 24;
  677. cword->l = (cword->l >> 8) | l;
  678. #else
  679. cword->l = (cword->l << 8) | c;
  680. #endif
  681. break;
  682. case FLASH_CFI_64BIT:
  683. #if defined(__LITTLE_ENDIAN)
  684. ll = c;
  685. ll <<= 56;
  686. cword->ll = (cword->ll >> 8) | ll;
  687. #else
  688. cword->ll = (cword->ll << 8) | c;
  689. #endif
  690. break;
  691. }
  692. }
  693. /*-----------------------------------------------------------------------
  694. * make a proper sized command based on the port and chip widths
  695. */
  696. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
  697. {
  698. int i;
  699. #if defined(__LITTLE_ENDIAN)
  700. ushort stmpw;
  701. uint stmpi;
  702. #endif
  703. uchar *cp = (uchar *) cmdbuf;
  704. for (i = 0; i < info->portwidth; i++)
  705. *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
  706. #if defined(__LITTLE_ENDIAN)
  707. switch (info->portwidth) {
  708. case FLASH_CFI_8BIT:
  709. break;
  710. case FLASH_CFI_16BIT:
  711. stmpw = *(ushort *) cmdbuf;
  712. *(ushort *) cmdbuf = __swab16 (stmpw);
  713. break;
  714. case FLASH_CFI_32BIT:
  715. stmpi = *(uint *) cmdbuf;
  716. *(uint *) cmdbuf = __swab32 (stmpi);
  717. break;
  718. default:
  719. puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
  720. break;
  721. }
  722. #endif
  723. }
  724. /*
  725. * Write a proper sized command to the correct address
  726. */
  727. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  728. {
  729. volatile cfiptr_t addr;
  730. cfiword_t cword;
  731. addr.cp = flash_make_addr (info, sect, offset);
  732. flash_make_cmd (info, cmd, &cword);
  733. switch (info->portwidth) {
  734. case FLASH_CFI_8BIT:
  735. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
  736. cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  737. *addr.cp = cword.c;
  738. break;
  739. case FLASH_CFI_16BIT:
  740. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
  741. cmd, cword.w,
  742. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  743. *addr.wp = cword.w;
  744. break;
  745. case FLASH_CFI_32BIT:
  746. debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
  747. cmd, cword.l,
  748. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  749. *addr.lp = cword.l;
  750. break;
  751. case FLASH_CFI_64BIT:
  752. #ifdef DEBUG
  753. {
  754. char str[20];
  755. print_longlong (str, cword.ll);
  756. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  757. addr.llp, cmd, str,
  758. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  759. }
  760. #endif
  761. *addr.llp = cword.ll;
  762. break;
  763. }
  764. }
  765. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  766. {
  767. flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
  768. flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
  769. }
  770. /*-----------------------------------------------------------------------
  771. */
  772. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  773. {
  774. cfiptr_t cptr;
  775. cfiword_t cword;
  776. int retval;
  777. cptr.cp = flash_make_addr (info, sect, offset);
  778. flash_make_cmd (info, cmd, &cword);
  779. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
  780. switch (info->portwidth) {
  781. case FLASH_CFI_8BIT:
  782. debug ("is= %x %x\n", cptr.cp[0], cword.c);
  783. retval = (cptr.cp[0] == cword.c);
  784. break;
  785. case FLASH_CFI_16BIT:
  786. debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
  787. retval = (cptr.wp[0] == cword.w);
  788. break;
  789. case FLASH_CFI_32BIT:
  790. debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
  791. retval = (cptr.lp[0] == cword.l);
  792. break;
  793. case FLASH_CFI_64BIT:
  794. #ifdef DEBUG
  795. {
  796. char str1[20];
  797. char str2[20];
  798. print_longlong (str1, cptr.llp[0]);
  799. print_longlong (str2, cword.ll);
  800. debug ("is= %s %s\n", str1, str2);
  801. }
  802. #endif
  803. retval = (cptr.llp[0] == cword.ll);
  804. break;
  805. default:
  806. retval = 0;
  807. break;
  808. }
  809. return retval;
  810. }
  811. /*-----------------------------------------------------------------------
  812. */
  813. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  814. {
  815. cfiptr_t cptr;
  816. cfiword_t cword;
  817. int retval;
  818. cptr.cp = flash_make_addr (info, sect, offset);
  819. flash_make_cmd (info, cmd, &cword);
  820. switch (info->portwidth) {
  821. case FLASH_CFI_8BIT:
  822. retval = ((cptr.cp[0] & cword.c) == cword.c);
  823. break;
  824. case FLASH_CFI_16BIT:
  825. retval = ((cptr.wp[0] & cword.w) == cword.w);
  826. break;
  827. case FLASH_CFI_32BIT:
  828. retval = ((cptr.lp[0] & cword.l) == cword.l);
  829. break;
  830. case FLASH_CFI_64BIT:
  831. retval = ((cptr.llp[0] & cword.ll) == cword.ll);
  832. break;
  833. default:
  834. retval = 0;
  835. break;
  836. }
  837. return retval;
  838. }
  839. /*-----------------------------------------------------------------------
  840. */
  841. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  842. {
  843. cfiptr_t cptr;
  844. cfiword_t cword;
  845. int retval;
  846. cptr.cp = flash_make_addr (info, sect, offset);
  847. flash_make_cmd (info, cmd, &cword);
  848. switch (info->portwidth) {
  849. case FLASH_CFI_8BIT:
  850. retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
  851. break;
  852. case FLASH_CFI_16BIT:
  853. retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
  854. break;
  855. case FLASH_CFI_32BIT:
  856. retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
  857. break;
  858. case FLASH_CFI_64BIT:
  859. retval = ((cptr.llp[0] & cword.ll) !=
  860. (cptr.llp[0] & cword.ll));
  861. break;
  862. default:
  863. retval = 0;
  864. break;
  865. }
  866. return retval;
  867. }
  868. /*-----------------------------------------------------------------------
  869. * detect if flash is compatible with the Common Flash Interface (CFI)
  870. * http://www.jedec.org/download/search/jesd68.pdf
  871. *
  872. */
  873. static int flash_detect_cfi (flash_info_t * info)
  874. {
  875. debug ("flash detect cfi\n");
  876. for (info->portwidth = FLASH_CFI_8BIT;
  877. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  878. for (info->chipwidth = FLASH_CFI_BY8;
  879. info->chipwidth <= info->portwidth;
  880. info->chipwidth <<= 1) {
  881. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  882. flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  883. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  884. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  885. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  886. info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
  887. debug ("device interface is %d\n",
  888. info->interface);
  889. debug ("found port %d chip %d ",
  890. info->portwidth, info->chipwidth);
  891. debug ("port %d bits chip %d bits\n",
  892. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  893. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  894. return 1;
  895. }
  896. }
  897. }
  898. debug ("not found\n");
  899. return 0;
  900. }
  901. /*
  902. * The following code cannot be run from FLASH!
  903. *
  904. */
  905. static ulong flash_get_size (ulong base, int banknum)
  906. {
  907. flash_info_t *info = &flash_info[banknum];
  908. int i, j;
  909. flash_sect_t sect_cnt;
  910. unsigned long sector;
  911. unsigned long tmp;
  912. int size_ratio;
  913. uchar num_erase_regions;
  914. int erase_region_size;
  915. int erase_region_count;
  916. info->start[0] = base;
  917. if (flash_detect_cfi (info)) {
  918. info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
  919. #ifdef DEBUG
  920. flash_printqry (info, 0);
  921. #endif
  922. switch (info->vendor) {
  923. case CFI_CMDSET_INTEL_STANDARD:
  924. case CFI_CMDSET_INTEL_EXTENDED:
  925. default:
  926. info->cmd_reset = FLASH_CMD_RESET;
  927. break;
  928. case CFI_CMDSET_AMD_STANDARD:
  929. case CFI_CMDSET_AMD_EXTENDED:
  930. info->cmd_reset = AMD_CMD_RESET;
  931. break;
  932. }
  933. debug ("manufacturer is %d\n", info->vendor);
  934. size_ratio = info->portwidth / info->chipwidth;
  935. /* if the chip is x8/x16 reduce the ratio by half */
  936. if ((info->interface == FLASH_CFI_X8X16)
  937. && (info->chipwidth == FLASH_CFI_BY8)) {
  938. size_ratio >>= 1;
  939. }
  940. num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  941. debug ("size_ratio %d port %d bits chip %d bits\n",
  942. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  943. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  944. debug ("found %d erase regions\n", num_erase_regions);
  945. sect_cnt = 0;
  946. sector = base;
  947. for (i = 0; i < num_erase_regions; i++) {
  948. if (i > NUM_ERASE_REGIONS) {
  949. printf ("%d erase regions found, only %d used\n",
  950. num_erase_regions, NUM_ERASE_REGIONS);
  951. break;
  952. }
  953. tmp = flash_read_long (info, 0,
  954. FLASH_OFFSET_ERASE_REGIONS +
  955. i * 4);
  956. erase_region_size =
  957. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  958. tmp >>= 16;
  959. erase_region_count = (tmp & 0xffff) + 1;
  960. debug ("erase_region_count = %d erase_region_size = %d\n",
  961. erase_region_count, erase_region_size);
  962. for (j = 0; j < erase_region_count; j++) {
  963. info->start[sect_cnt] = sector;
  964. sector += (erase_region_size * size_ratio);
  965. info->protect[sect_cnt] =
  966. flash_isset (info, sect_cnt,
  967. FLASH_OFFSET_PROTECT,
  968. FLASH_STATUS_PROTECT);
  969. sect_cnt++;
  970. }
  971. }
  972. info->sector_count = sect_cnt;
  973. /* multiply the size by the number of chips */
  974. info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
  975. info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
  976. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
  977. info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
  978. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
  979. info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
  980. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
  981. info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
  982. info->flash_id = FLASH_MAN_CFI;
  983. if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
  984. info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
  985. }
  986. }
  987. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  988. return (info->size);
  989. }
  990. /*-----------------------------------------------------------------------
  991. */
  992. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  993. cfiword_t cword)
  994. {
  995. cfiptr_t ctladdr;
  996. cfiptr_t cptr;
  997. int flag;
  998. ctladdr.cp = flash_make_addr (info, 0, 0);
  999. cptr.cp = (uchar *) dest;
  1000. /* Check if Flash is (sufficiently) erased */
  1001. switch (info->portwidth) {
  1002. case FLASH_CFI_8BIT:
  1003. flag = ((cptr.cp[0] & cword.c) == cword.c);
  1004. break;
  1005. case FLASH_CFI_16BIT:
  1006. flag = ((cptr.wp[0] & cword.w) == cword.w);
  1007. break;
  1008. case FLASH_CFI_32BIT:
  1009. flag = ((cptr.lp[0] & cword.l) == cword.l);
  1010. break;
  1011. case FLASH_CFI_64BIT:
  1012. flag = ((cptr.llp[0] & cword.ll) == cword.ll);
  1013. break;
  1014. default:
  1015. return 2;
  1016. }
  1017. if (!flag)
  1018. return 2;
  1019. /* Disable interrupts which might cause a timeout here */
  1020. flag = disable_interrupts ();
  1021. switch (info->vendor) {
  1022. case CFI_CMDSET_INTEL_EXTENDED:
  1023. case CFI_CMDSET_INTEL_STANDARD:
  1024. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  1025. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  1026. break;
  1027. case CFI_CMDSET_AMD_EXTENDED:
  1028. case CFI_CMDSET_AMD_STANDARD:
  1029. flash_unlock_seq (info, 0);
  1030. flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
  1031. break;
  1032. }
  1033. switch (info->portwidth) {
  1034. case FLASH_CFI_8BIT:
  1035. cptr.cp[0] = cword.c;
  1036. break;
  1037. case FLASH_CFI_16BIT:
  1038. cptr.wp[0] = cword.w;
  1039. break;
  1040. case FLASH_CFI_32BIT:
  1041. cptr.lp[0] = cword.l;
  1042. break;
  1043. case FLASH_CFI_64BIT:
  1044. cptr.llp[0] = cword.ll;
  1045. break;
  1046. }
  1047. /* re-enable interrupts if necessary */
  1048. if (flag)
  1049. enable_interrupts ();
  1050. return flash_full_status_check (info, 0, info->write_tout, "write");
  1051. }
  1052. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  1053. /* loop through the sectors from the highest address
  1054. * when the passed address is greater or equal to the sector address
  1055. * we have a match
  1056. */
  1057. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  1058. {
  1059. flash_sect_t sector;
  1060. for (sector = info->sector_count - 1; sector >= 0; sector--) {
  1061. if (addr >= info->start[sector])
  1062. break;
  1063. }
  1064. return sector;
  1065. }
  1066. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  1067. int len)
  1068. {
  1069. flash_sect_t sector;
  1070. int cnt;
  1071. int retcode;
  1072. volatile cfiptr_t src;
  1073. volatile cfiptr_t dst;
  1074. /* buffered writes in the AMD chip set is not supported yet */
  1075. if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
  1076. (info->vendor == CFI_CMDSET_AMD_EXTENDED))
  1077. return ERR_INVAL;
  1078. src.cp = cp;
  1079. dst.cp = (uchar *) dest;
  1080. sector = find_sector (info, dest);
  1081. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1082. flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  1083. if ((retcode =
  1084. flash_status_check (info, sector, info->buffer_write_tout,
  1085. "write to buffer")) == ERR_OK) {
  1086. /* reduce the number of loops by the width of the port */
  1087. switch (info->portwidth) {
  1088. case FLASH_CFI_8BIT:
  1089. cnt = len;
  1090. break;
  1091. case FLASH_CFI_16BIT:
  1092. cnt = len >> 1;
  1093. break;
  1094. case FLASH_CFI_32BIT:
  1095. cnt = len >> 2;
  1096. break;
  1097. case FLASH_CFI_64BIT:
  1098. cnt = len >> 3;
  1099. break;
  1100. default:
  1101. return ERR_INVAL;
  1102. break;
  1103. }
  1104. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1105. while (cnt-- > 0) {
  1106. switch (info->portwidth) {
  1107. case FLASH_CFI_8BIT:
  1108. *dst.cp++ = *src.cp++;
  1109. break;
  1110. case FLASH_CFI_16BIT:
  1111. *dst.wp++ = *src.wp++;
  1112. break;
  1113. case FLASH_CFI_32BIT:
  1114. *dst.lp++ = *src.lp++;
  1115. break;
  1116. case FLASH_CFI_64BIT:
  1117. *dst.llp++ = *src.llp++;
  1118. break;
  1119. default:
  1120. return ERR_INVAL;
  1121. break;
  1122. }
  1123. }
  1124. flash_write_cmd (info, sector, 0,
  1125. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  1126. retcode =
  1127. flash_full_status_check (info, sector,
  1128. info->buffer_write_tout,
  1129. "buffer write");
  1130. }
  1131. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1132. return retcode;
  1133. }
  1134. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  1135. #endif /* CFG_FLASH_CFI */