cfi_flash.c 32 KB

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