cfi_flash.c 33 KB

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