cfi_flash.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. typedef union {
  134. unsigned char c;
  135. unsigned short w;
  136. unsigned long l;
  137. unsigned long long ll;
  138. } cfiword_t;
  139. typedef union {
  140. volatile unsigned char *cp;
  141. volatile unsigned short *wp;
  142. volatile unsigned long *lp;
  143. volatile unsigned long long *llp;
  144. } cfiptr_t;
  145. #define NUM_ERASE_REGIONS 4
  146. static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
  147. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  148. /*-----------------------------------------------------------------------
  149. * Functions
  150. */
  151. typedef unsigned long flash_sect_t;
  152. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
  153. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
  154. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  155. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
  156. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  157. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  158. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  159. static int flash_detect_cfi (flash_info_t * info);
  160. static ulong flash_get_size (ulong base, int banknum);
  161. static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
  162. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  163. ulong tout, char *prompt);
  164. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  165. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
  166. #endif
  167. /*-----------------------------------------------------------------------
  168. * create an address based on the offset and the port width
  169. */
  170. inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
  171. {
  172. return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
  173. }
  174. #ifdef DEBUG
  175. /*-----------------------------------------------------------------------
  176. * Debug support
  177. */
  178. void print_longlong (char *str, unsigned long long data)
  179. {
  180. int i;
  181. char *cp;
  182. cp = (unsigned char *) &data;
  183. for (i = 0; i < 8; i++)
  184. sprintf (&str[i * 2], "%2.2x", *cp++);
  185. }
  186. static void flash_printqry (flash_info_t * info, flash_sect_t sect)
  187. {
  188. cfiptr_t cptr;
  189. int x, y;
  190. for (x = 0; x < 0x40; x += 16 / info->portwidth) {
  191. cptr.cp =
  192. flash_make_addr (info, sect,
  193. x + FLASH_OFFSET_CFI_RESP);
  194. debug ("%p : ", cptr.cp);
  195. for (y = 0; y < 16; y++) {
  196. debug ("%2.2x ", cptr.cp[y]);
  197. }
  198. debug (" ");
  199. for (y = 0; y < 16; y++) {
  200. if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
  201. debug ("%c", cptr.cp[y]);
  202. } else {
  203. debug (".");
  204. }
  205. }
  206. debug ("\n");
  207. }
  208. }
  209. #endif
  210. /*-----------------------------------------------------------------------
  211. * read a character at a port width address
  212. */
  213. inline uchar flash_read_uchar (flash_info_t * info, uint offset)
  214. {
  215. uchar *cp;
  216. cp = flash_make_addr (info, 0, offset);
  217. #if defined(__LITTLE_ENDIAN)
  218. return (cp[0]);
  219. #else
  220. return (cp[info->portwidth - 1]);
  221. #endif
  222. }
  223. /*-----------------------------------------------------------------------
  224. * read a short word by swapping for ppc format.
  225. */
  226. ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
  227. {
  228. uchar *addr;
  229. ushort retval;
  230. #ifdef DEBUG
  231. int x;
  232. #endif
  233. addr = flash_make_addr (info, sect, offset);
  234. #ifdef DEBUG
  235. debug ("ushort addr is at %p info->portwidth = %d\n", addr,
  236. info->portwidth);
  237. for (x = 0; x < 2 * info->portwidth; x++) {
  238. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  239. }
  240. #endif
  241. #if defined(__LITTLE_ENDIAN)
  242. retval = ((addr[(info->portwidth)] << 8) | addr[0]);
  243. #else
  244. retval = ((addr[(2 * info->portwidth) - 1] << 8) |
  245. addr[info->portwidth - 1]);
  246. #endif
  247. debug ("retval = 0x%x\n", retval);
  248. return retval;
  249. }
  250. /*-----------------------------------------------------------------------
  251. * read a long word by picking the least significant byte of each maiximum
  252. * port size word. Swap for ppc format.
  253. */
  254. ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
  255. {
  256. uchar *addr;
  257. ulong retval;
  258. #ifdef DEBUG
  259. int x;
  260. #endif
  261. addr = flash_make_addr (info, sect, offset);
  262. #ifdef DEBUG
  263. debug ("long addr is at %p info->portwidth = %d\n", addr,
  264. info->portwidth);
  265. for (x = 0; x < 4 * info->portwidth; x++) {
  266. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  267. }
  268. #endif
  269. #if defined(__LITTLE_ENDIAN)
  270. retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
  271. (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
  272. #else
  273. retval = (addr[(2 * info->portwidth) - 1] << 24) |
  274. (addr[(info->portwidth) - 1] << 16) |
  275. (addr[(4 * info->portwidth) - 1] << 8) |
  276. addr[(3 * info->portwidth) - 1];
  277. #endif
  278. return retval;
  279. }
  280. /*-----------------------------------------------------------------------
  281. */
  282. unsigned long flash_init (void)
  283. {
  284. unsigned long size = 0;
  285. int i;
  286. /* Init: no FLASHes known */
  287. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  288. flash_info[i].flash_id = FLASH_UNKNOWN;
  289. size += flash_info[i].size = flash_get_size (bank_base[i], i);
  290. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  291. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  292. i, flash_info[i].size, flash_info[i].size << 20);
  293. }
  294. }
  295. /* Monitor protection ON by default */
  296. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  297. flash_protect (FLAG_PROTECT_SET,
  298. CFG_MONITOR_BASE,
  299. CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
  300. &flash_info[0]);
  301. #endif
  302. return (size);
  303. }
  304. /*-----------------------------------------------------------------------
  305. */
  306. int flash_erase (flash_info_t * info, int s_first, int s_last)
  307. {
  308. int rcode = 0;
  309. int prot;
  310. flash_sect_t sect;
  311. if (info->flash_id != FLASH_MAN_CFI) {
  312. puts ("Can't erase unknown flash type - aborted\n");
  313. return 1;
  314. }
  315. if ((s_first < 0) || (s_first > s_last)) {
  316. puts ("- no sectors to erase\n");
  317. return 1;
  318. }
  319. prot = 0;
  320. for (sect = s_first; sect <= s_last; ++sect) {
  321. if (info->protect[sect]) {
  322. prot++;
  323. }
  324. }
  325. if (prot) {
  326. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  327. } else {
  328. putc ('\n');
  329. }
  330. for (sect = s_first; sect <= s_last; sect++) {
  331. if (info->protect[sect] == 0) { /* not protected */
  332. switch (info->vendor) {
  333. case CFI_CMDSET_INTEL_STANDARD:
  334. case CFI_CMDSET_INTEL_EXTENDED:
  335. flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  336. flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  337. flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  338. break;
  339. case CFI_CMDSET_AMD_STANDARD:
  340. case CFI_CMDSET_AMD_EXTENDED:
  341. flash_unlock_seq (info, sect);
  342. flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
  343. AMD_CMD_ERASE_START);
  344. flash_unlock_seq (info, sect);
  345. flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
  346. break;
  347. default:
  348. debug ("Unkown flash vendor %d\n",
  349. info->vendor);
  350. break;
  351. }
  352. if (flash_full_status_check
  353. (info, sect, info->erase_blk_tout, "erase")) {
  354. rcode = 1;
  355. } else
  356. putc ('.');
  357. }
  358. }
  359. puts (" done\n");
  360. return rcode;
  361. }
  362. /*-----------------------------------------------------------------------
  363. */
  364. void flash_print_info (flash_info_t * info)
  365. {
  366. int i;
  367. if (info->flash_id != FLASH_MAN_CFI) {
  368. puts ("missing or unknown FLASH type\n");
  369. return;
  370. }
  371. printf ("CFI conformant FLASH (%d x %d)",
  372. (info->portwidth << 3), (info->chipwidth << 3));
  373. printf (" Size: %ld MB in %d Sectors\n",
  374. info->size >> 20, info->sector_count);
  375. printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
  376. info->erase_blk_tout,
  377. info->write_tout,
  378. info->buffer_write_tout,
  379. info->buffer_size);
  380. puts (" Sector Start Addresses:");
  381. for (i = 0; i < info->sector_count; ++i) {
  382. #ifdef CFG_FLASH_EMPTY_INFO
  383. int k;
  384. int size;
  385. int erased;
  386. volatile unsigned long *flash;
  387. /*
  388. * Check if whole sector is erased
  389. */
  390. if (i != (info->sector_count - 1))
  391. size = info->start[i + 1] - info->start[i];
  392. else
  393. size = info->start[0] + info->size - info->start[i];
  394. erased = 1;
  395. flash = (volatile unsigned long *) info->start[i];
  396. size = size >> 2; /* divide by 4 for longword access */
  397. for (k = 0; k < size; k++) {
  398. if (*flash++ != 0xffffffff) {
  399. erased = 0;
  400. break;
  401. }
  402. }
  403. if ((i % 5) == 0)
  404. printf ("\n");
  405. /* print empty and read-only info */
  406. printf (" %08lX%s%s",
  407. info->start[i],
  408. erased ? " E" : " ",
  409. info->protect[i] ? "RO " : " ");
  410. #else
  411. if ((i % 5) == 0)
  412. printf ("\n ");
  413. printf (" %08lX%s",
  414. info->start[i], info->protect[i] ? " (RO) " : " ");
  415. #endif
  416. }
  417. putc ('\n');
  418. return;
  419. }
  420. /*-----------------------------------------------------------------------
  421. * Copy memory to flash, returns:
  422. * 0 - OK
  423. * 1 - write timeout
  424. * 2 - Flash not erased
  425. */
  426. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  427. {
  428. ulong wp;
  429. ulong cp;
  430. int aln;
  431. cfiword_t cword;
  432. int i, rc;
  433. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  434. int buffered_size;
  435. #endif
  436. /* get lower aligned address */
  437. /* get lower aligned address */
  438. wp = (addr & ~(info->portwidth - 1));
  439. /* handle unaligned start */
  440. if ((aln = addr - wp) != 0) {
  441. cword.l = 0;
  442. cp = wp;
  443. for (i = 0; i < aln; ++i, ++cp)
  444. flash_add_byte (info, &cword, (*(uchar *) cp));
  445. for (; (i < info->portwidth) && (cnt > 0); i++) {
  446. flash_add_byte (info, &cword, *src++);
  447. cnt--;
  448. cp++;
  449. }
  450. for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  451. flash_add_byte (info, &cword, (*(uchar *) cp));
  452. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  453. return rc;
  454. wp = cp;
  455. }
  456. /* handle the aligned part */
  457. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  458. buffered_size = (info->portwidth / info->chipwidth);
  459. buffered_size *= info->buffer_size;
  460. while (cnt >= info->portwidth) {
  461. i = buffered_size > cnt ? cnt : buffered_size;
  462. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  463. return rc;
  464. i -= (i % info->portwidth);
  465. wp += i;
  466. src += i;
  467. cnt -= i;
  468. }
  469. #else
  470. while (cnt >= info->portwidth) {
  471. cword.l = 0;
  472. for (i = 0; i < info->portwidth; i++) {
  473. flash_add_byte (info, &cword, *src++);
  474. }
  475. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  476. return rc;
  477. wp += info->portwidth;
  478. cnt -= info->portwidth;
  479. }
  480. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  481. if (cnt == 0) {
  482. return (0);
  483. }
  484. /*
  485. * handle unaligned tail bytes
  486. */
  487. cword.l = 0;
  488. for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
  489. flash_add_byte (info, &cword, *src++);
  490. --cnt;
  491. }
  492. for (; i < info->portwidth; ++i, ++cp) {
  493. flash_add_byte (info, &cword, (*(uchar *) cp));
  494. }
  495. return flash_write_cfiword (info, wp, cword);
  496. }
  497. /*-----------------------------------------------------------------------
  498. */
  499. #ifdef CFG_FLASH_PROTECTION
  500. int flash_real_protect (flash_info_t * info, long sector, int prot)
  501. {
  502. int retcode = 0;
  503. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  504. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  505. if (prot)
  506. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  507. else
  508. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  509. if ((retcode =
  510. flash_full_status_check (info, sector, info->erase_blk_tout,
  511. prot ? "protect" : "unprotect")) == 0) {
  512. info->protect[sector] = prot;
  513. /* Intel's unprotect unprotects all locking */
  514. if (prot == 0) {
  515. flash_sect_t i;
  516. for (i = 0; i < info->sector_count; i++) {
  517. if (info->protect[i])
  518. flash_real_protect (info, i, 1);
  519. }
  520. }
  521. }
  522. return retcode;
  523. }
  524. /*-----------------------------------------------------------------------
  525. * flash_read_user_serial - read the OneTimeProgramming cells
  526. */
  527. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  528. int len)
  529. {
  530. uchar *src;
  531. uchar *dst;
  532. dst = buffer;
  533. src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
  534. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  535. memcpy (dst, src + offset, len);
  536. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  537. }
  538. /*
  539. * flash_read_factory_serial - read the device Id from the protection area
  540. */
  541. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  542. int len)
  543. {
  544. uchar *src;
  545. src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  546. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  547. memcpy (buffer, src + offset, len);
  548. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  549. }
  550. #endif /* CFG_FLASH_PROTECTION */
  551. /*
  552. * flash_is_busy - check to see if the flash is busy
  553. * This routine checks the status of the chip and returns true if the chip is busy
  554. */
  555. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  556. {
  557. int retval;
  558. switch (info->vendor) {
  559. case CFI_CMDSET_INTEL_STANDARD:
  560. case CFI_CMDSET_INTEL_EXTENDED:
  561. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  562. break;
  563. case CFI_CMDSET_AMD_STANDARD:
  564. case CFI_CMDSET_AMD_EXTENDED:
  565. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  566. break;
  567. default:
  568. retval = 0;
  569. }
  570. debug ("flash_is_busy: %d\n", retval);
  571. return retval;
  572. }
  573. /*-----------------------------------------------------------------------
  574. * wait for XSR.7 to be set. Time out with an error if it does not.
  575. * This routine does not set the flash to read-array mode.
  576. */
  577. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  578. ulong tout, char *prompt)
  579. {
  580. ulong start;
  581. /* Wait for command completion */
  582. start = get_timer (0);
  583. while (flash_is_busy (info, sector)) {
  584. if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
  585. printf ("Flash %s timeout at address %lx data %lx\n",
  586. prompt, info->start[sector],
  587. flash_read_long (info, sector, 0));
  588. flash_write_cmd (info, sector, 0, info->cmd_reset);
  589. return ERR_TIMOUT;
  590. }
  591. }
  592. return ERR_OK;
  593. }
  594. /*-----------------------------------------------------------------------
  595. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  596. * This routine sets the flash to read-array mode.
  597. */
  598. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  599. ulong tout, char *prompt)
  600. {
  601. int retcode;
  602. retcode = flash_status_check (info, sector, tout, prompt);
  603. switch (info->vendor) {
  604. case CFI_CMDSET_INTEL_EXTENDED:
  605. case CFI_CMDSET_INTEL_STANDARD:
  606. if ((retcode != ERR_OK)
  607. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  608. retcode = ERR_INVAL;
  609. printf ("Flash %s error at address %lx\n", prompt,
  610. info->start[sector]);
  611. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
  612. puts ("Command Sequence Error.\n");
  613. } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
  614. puts ("Block Erase Error.\n");
  615. retcode = ERR_NOT_ERASED;
  616. } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
  617. puts ("Locking Error\n");
  618. }
  619. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  620. puts ("Block locked.\n");
  621. retcode = ERR_PROTECTED;
  622. }
  623. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  624. puts ("Vpp Low Error.\n");
  625. }
  626. flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
  627. break;
  628. default:
  629. break;
  630. }
  631. return retcode;
  632. }
  633. /*-----------------------------------------------------------------------
  634. */
  635. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  636. {
  637. #if defined(__LITTLE_ENDIAN)
  638. unsigned short w;
  639. unsigned int l;
  640. unsigned long long ll;
  641. #endif
  642. switch (info->portwidth) {
  643. case FLASH_CFI_8BIT:
  644. cword->c = c;
  645. break;
  646. case FLASH_CFI_16BIT:
  647. #if defined(__LITTLE_ENDIAN)
  648. w = c;
  649. w <<= 8;
  650. cword->w = (cword->w >> 8) | w;
  651. #else
  652. cword->w = (cword->w << 8) | c;
  653. #endif
  654. break;
  655. case FLASH_CFI_32BIT:
  656. #if defined(__LITTLE_ENDIAN)
  657. l = c;
  658. l <<= 24;
  659. cword->l = (cword->l >> 8) | l;
  660. #else
  661. cword->l = (cword->l << 8) | c;
  662. #endif
  663. break;
  664. case FLASH_CFI_64BIT:
  665. #if defined(__LITTLE_ENDIAN)
  666. ll = c;
  667. ll <<= 56;
  668. cword->ll = (cword->ll >> 8) | ll;
  669. #else
  670. cword->ll = (cword->ll << 8) | c;
  671. #endif
  672. break;
  673. }
  674. }
  675. /*-----------------------------------------------------------------------
  676. * make a proper sized command based on the port and chip widths
  677. */
  678. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
  679. {
  680. int i;
  681. #if defined(__LITTLE_ENDIAN)
  682. ushort stmpw;
  683. uint stmpi;
  684. #endif
  685. uchar *cp = (uchar *) cmdbuf;
  686. for (i = 0; i < info->portwidth; i++)
  687. *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
  688. #if defined(__LITTLE_ENDIAN)
  689. switch (info->portwidth) {
  690. case FLASH_CFI_8BIT:
  691. break;
  692. case FLASH_CFI_16BIT:
  693. stmpw = *(ushort *) cmdbuf;
  694. *(ushort *) cmdbuf = __swab16 (stmpw);
  695. break;
  696. case FLASH_CFI_32BIT:
  697. stmpi = *(uint *) cmdbuf;
  698. *(uint *) cmdbuf = __swab32 (stmpi);
  699. break;
  700. default:
  701. puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
  702. break;
  703. }
  704. #endif
  705. }
  706. /*
  707. * Write a proper sized command to the correct address
  708. */
  709. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  710. {
  711. volatile cfiptr_t addr;
  712. cfiword_t cword;
  713. addr.cp = flash_make_addr (info, sect, offset);
  714. flash_make_cmd (info, cmd, &cword);
  715. switch (info->portwidth) {
  716. case FLASH_CFI_8BIT:
  717. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
  718. cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  719. *addr.cp = cword.c;
  720. break;
  721. case FLASH_CFI_16BIT:
  722. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
  723. cmd, cword.w,
  724. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  725. *addr.wp = cword.w;
  726. break;
  727. case FLASH_CFI_32BIT:
  728. debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
  729. cmd, cword.l,
  730. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  731. *addr.lp = cword.l;
  732. break;
  733. case FLASH_CFI_64BIT:
  734. #ifdef DEBUG
  735. {
  736. char str[20];
  737. print_longlong (str, cword.ll);
  738. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  739. addr.llp, cmd, str,
  740. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  741. }
  742. #endif
  743. *addr.llp = cword.ll;
  744. break;
  745. }
  746. }
  747. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  748. {
  749. flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
  750. flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
  751. }
  752. /*-----------------------------------------------------------------------
  753. */
  754. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  755. {
  756. cfiptr_t cptr;
  757. cfiword_t cword;
  758. int retval;
  759. cptr.cp = flash_make_addr (info, sect, offset);
  760. flash_make_cmd (info, cmd, &cword);
  761. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
  762. switch (info->portwidth) {
  763. case FLASH_CFI_8BIT:
  764. debug ("is= %x %x\n", cptr.cp[0], cword.c);
  765. retval = (cptr.cp[0] == cword.c);
  766. break;
  767. case FLASH_CFI_16BIT:
  768. debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
  769. retval = (cptr.wp[0] == cword.w);
  770. break;
  771. case FLASH_CFI_32BIT:
  772. debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
  773. retval = (cptr.lp[0] == cword.l);
  774. break;
  775. case FLASH_CFI_64BIT:
  776. #ifdef DEBUG
  777. {
  778. char str1[20];
  779. char str2[20];
  780. print_longlong (str1, cptr.llp[0]);
  781. print_longlong (str2, cword.ll);
  782. debug ("is= %s %s\n", str1, str2);
  783. }
  784. #endif
  785. retval = (cptr.llp[0] == cword.ll);
  786. break;
  787. default:
  788. retval = 0;
  789. break;
  790. }
  791. return retval;
  792. }
  793. /*-----------------------------------------------------------------------
  794. */
  795. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  796. {
  797. cfiptr_t cptr;
  798. cfiword_t cword;
  799. int retval;
  800. cptr.cp = flash_make_addr (info, sect, offset);
  801. flash_make_cmd (info, cmd, &cword);
  802. switch (info->portwidth) {
  803. case FLASH_CFI_8BIT:
  804. retval = ((cptr.cp[0] & cword.c) == cword.c);
  805. break;
  806. case FLASH_CFI_16BIT:
  807. retval = ((cptr.wp[0] & cword.w) == cword.w);
  808. break;
  809. case FLASH_CFI_32BIT:
  810. retval = ((cptr.lp[0] & cword.l) == cword.l);
  811. break;
  812. case FLASH_CFI_64BIT:
  813. retval = ((cptr.llp[0] & cword.ll) == cword.ll);
  814. break;
  815. default:
  816. retval = 0;
  817. break;
  818. }
  819. return retval;
  820. }
  821. /*-----------------------------------------------------------------------
  822. */
  823. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  824. {
  825. cfiptr_t cptr;
  826. cfiword_t cword;
  827. int retval;
  828. cptr.cp = flash_make_addr (info, sect, offset);
  829. flash_make_cmd (info, cmd, &cword);
  830. switch (info->portwidth) {
  831. case FLASH_CFI_8BIT:
  832. retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
  833. break;
  834. case FLASH_CFI_16BIT:
  835. retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
  836. break;
  837. case FLASH_CFI_32BIT:
  838. retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
  839. break;
  840. case FLASH_CFI_64BIT:
  841. retval = ((cptr.llp[0] & cword.ll) !=
  842. (cptr.llp[0] & cword.ll));
  843. break;
  844. default:
  845. retval = 0;
  846. break;
  847. }
  848. return retval;
  849. }
  850. /*-----------------------------------------------------------------------
  851. * detect if flash is compatible with the Common Flash Interface (CFI)
  852. * http://www.jedec.org/download/search/jesd68.pdf
  853. *
  854. */
  855. static int flash_detect_cfi (flash_info_t * info)
  856. {
  857. debug ("flash detect cfi\n");
  858. for (info->portwidth = FLASH_CFI_8BIT;
  859. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  860. for (info->chipwidth = FLASH_CFI_BY8;
  861. info->chipwidth <= info->portwidth;
  862. info->chipwidth <<= 1) {
  863. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  864. flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  865. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  866. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  867. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  868. info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
  869. debug ("device interface is %d\n",
  870. info->interface);
  871. debug ("found port %d chip %d ",
  872. info->portwidth, info->chipwidth);
  873. debug ("port %d bits chip %d bits\n",
  874. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  875. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  876. return 1;
  877. }
  878. }
  879. }
  880. debug ("not found\n");
  881. return 0;
  882. }
  883. /*
  884. * The following code cannot be run from FLASH!
  885. *
  886. */
  887. static ulong flash_get_size (ulong base, int banknum)
  888. {
  889. flash_info_t *info = &flash_info[banknum];
  890. int i, j;
  891. flash_sect_t sect_cnt;
  892. unsigned long sector;
  893. unsigned long tmp;
  894. int size_ratio;
  895. uchar num_erase_regions;
  896. int erase_region_size;
  897. int erase_region_count;
  898. info->start[0] = base;
  899. if (flash_detect_cfi (info)) {
  900. info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
  901. #ifdef DEBUG
  902. flash_printqry (info, 0);
  903. #endif
  904. switch (info->vendor) {
  905. case CFI_CMDSET_INTEL_STANDARD:
  906. case CFI_CMDSET_INTEL_EXTENDED:
  907. default:
  908. info->cmd_reset = FLASH_CMD_RESET;
  909. break;
  910. case CFI_CMDSET_AMD_STANDARD:
  911. case CFI_CMDSET_AMD_EXTENDED:
  912. info->cmd_reset = AMD_CMD_RESET;
  913. break;
  914. }
  915. debug ("manufacturer is %d\n", info->vendor);
  916. size_ratio = info->portwidth / info->chipwidth;
  917. /* if the chip is x8/x16 reduce the ratio by half */
  918. if ((info->interface == FLASH_CFI_X8X16)
  919. && (info->chipwidth == FLASH_CFI_BY8)) {
  920. size_ratio >>= 1;
  921. }
  922. num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  923. debug ("size_ratio %d port %d bits chip %d bits\n",
  924. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  925. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  926. debug ("found %d erase regions\n", num_erase_regions);
  927. sect_cnt = 0;
  928. sector = base;
  929. for (i = 0; i < num_erase_regions; i++) {
  930. if (i > NUM_ERASE_REGIONS) {
  931. printf ("%d erase regions found, only %d used\n",
  932. num_erase_regions, NUM_ERASE_REGIONS);
  933. break;
  934. }
  935. tmp = flash_read_long (info, 0,
  936. FLASH_OFFSET_ERASE_REGIONS +
  937. i * 4);
  938. erase_region_size =
  939. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  940. tmp >>= 16;
  941. erase_region_count = (tmp & 0xffff) + 1;
  942. debug ("erase_region_count = %d erase_region_size = %d\n",
  943. erase_region_count, erase_region_size);
  944. for (j = 0; j < erase_region_count; j++) {
  945. info->start[sect_cnt] = sector;
  946. sector += (erase_region_size * size_ratio);
  947. info->protect[sect_cnt] =
  948. flash_isset (info, sect_cnt,
  949. FLASH_OFFSET_PROTECT,
  950. FLASH_STATUS_PROTECT);
  951. sect_cnt++;
  952. }
  953. }
  954. info->sector_count = sect_cnt;
  955. /* multiply the size by the number of chips */
  956. info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
  957. info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
  958. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
  959. info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
  960. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
  961. info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
  962. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
  963. info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
  964. info->flash_id = FLASH_MAN_CFI;
  965. if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
  966. info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
  967. }
  968. }
  969. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  970. return (info->size);
  971. }
  972. /*-----------------------------------------------------------------------
  973. */
  974. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  975. cfiword_t cword)
  976. {
  977. cfiptr_t ctladdr;
  978. cfiptr_t cptr;
  979. int flag;
  980. ctladdr.cp = flash_make_addr (info, 0, 0);
  981. cptr.cp = (uchar *) dest;
  982. /* Check if Flash is (sufficiently) erased */
  983. switch (info->portwidth) {
  984. case FLASH_CFI_8BIT:
  985. flag = ((cptr.cp[0] & cword.c) == cword.c);
  986. break;
  987. case FLASH_CFI_16BIT:
  988. flag = ((cptr.wp[0] & cword.w) == cword.w);
  989. break;
  990. case FLASH_CFI_32BIT:
  991. flag = ((cptr.lp[0] & cword.l) == cword.l);
  992. break;
  993. case FLASH_CFI_64BIT:
  994. flag = ((cptr.lp[0] & cword.ll) == cword.ll);
  995. break;
  996. default:
  997. return 2;
  998. }
  999. if (!flag)
  1000. return 2;
  1001. /* Disable interrupts which might cause a timeout here */
  1002. flag = disable_interrupts ();
  1003. switch (info->vendor) {
  1004. case CFI_CMDSET_INTEL_EXTENDED:
  1005. case CFI_CMDSET_INTEL_STANDARD:
  1006. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  1007. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  1008. break;
  1009. case CFI_CMDSET_AMD_EXTENDED:
  1010. case CFI_CMDSET_AMD_STANDARD:
  1011. flash_unlock_seq (info, 0);
  1012. flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
  1013. break;
  1014. }
  1015. switch (info->portwidth) {
  1016. case FLASH_CFI_8BIT:
  1017. cptr.cp[0] = cword.c;
  1018. break;
  1019. case FLASH_CFI_16BIT:
  1020. cptr.wp[0] = cword.w;
  1021. break;
  1022. case FLASH_CFI_32BIT:
  1023. cptr.lp[0] = cword.l;
  1024. break;
  1025. case FLASH_CFI_64BIT:
  1026. cptr.llp[0] = cword.ll;
  1027. break;
  1028. }
  1029. /* re-enable interrupts if necessary */
  1030. if (flag)
  1031. enable_interrupts ();
  1032. return flash_full_status_check (info, 0, info->write_tout, "write");
  1033. }
  1034. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  1035. /* loop through the sectors from the highest address
  1036. * when the passed address is greater or equal to the sector address
  1037. * we have a match
  1038. */
  1039. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  1040. {
  1041. flash_sect_t sector;
  1042. for (sector = info->sector_count - 1; sector >= 0; sector--) {
  1043. if (addr >= info->start[sector])
  1044. break;
  1045. }
  1046. return sector;
  1047. }
  1048. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  1049. int len)
  1050. {
  1051. flash_sect_t sector;
  1052. int cnt;
  1053. int retcode;
  1054. volatile cfiptr_t src;
  1055. volatile cfiptr_t dst;
  1056. /* buffered writes in the AMD chip set is not supported yet */
  1057. if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
  1058. (info->vendor == CFI_CMDSET_AMD_EXTENDED))
  1059. return ERR_INVAL;
  1060. src.cp = cp;
  1061. dst.cp = (uchar *) dest;
  1062. sector = find_sector (info, dest);
  1063. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1064. flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  1065. if ((retcode =
  1066. flash_status_check (info, sector, info->buffer_write_tout,
  1067. "write to buffer")) == ERR_OK) {
  1068. /* reduce the number of loops by the width of the port */
  1069. switch (info->portwidth) {
  1070. case FLASH_CFI_8BIT:
  1071. cnt = len;
  1072. break;
  1073. case FLASH_CFI_16BIT:
  1074. cnt = len >> 1;
  1075. break;
  1076. case FLASH_CFI_32BIT:
  1077. cnt = len >> 2;
  1078. break;
  1079. case FLASH_CFI_64BIT:
  1080. cnt = len >> 3;
  1081. break;
  1082. default:
  1083. return ERR_INVAL;
  1084. break;
  1085. }
  1086. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1087. while (cnt-- > 0) {
  1088. switch (info->portwidth) {
  1089. case FLASH_CFI_8BIT:
  1090. *dst.cp++ = *src.cp++;
  1091. break;
  1092. case FLASH_CFI_16BIT:
  1093. *dst.wp++ = *src.wp++;
  1094. break;
  1095. case FLASH_CFI_32BIT:
  1096. *dst.lp++ = *src.lp++;
  1097. break;
  1098. case FLASH_CFI_64BIT:
  1099. *dst.llp++ = *src.llp++;
  1100. break;
  1101. default:
  1102. return ERR_INVAL;
  1103. break;
  1104. }
  1105. }
  1106. flash_write_cmd (info, sector, 0,
  1107. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  1108. retcode =
  1109. flash_full_status_check (info, sector,
  1110. info->buffer_write_tout,
  1111. "buffer write");
  1112. }
  1113. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1114. return retcode;
  1115. }
  1116. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  1117. #endif /* CFG_FLASH_CFI */