cfi_flash.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. wp += i;
  465. src += i;
  466. cnt -= i;
  467. }
  468. #else
  469. while (cnt >= info->portwidth) {
  470. cword.l = 0;
  471. for (i = 0; i < info->portwidth; i++) {
  472. flash_add_byte (info, &cword, *src++);
  473. }
  474. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  475. return rc;
  476. wp += info->portwidth;
  477. cnt -= info->portwidth;
  478. }
  479. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  480. if (cnt == 0) {
  481. return (0);
  482. }
  483. /*
  484. * handle unaligned tail bytes
  485. */
  486. cword.l = 0;
  487. for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
  488. flash_add_byte (info, &cword, *src++);
  489. --cnt;
  490. }
  491. for (; i < info->portwidth; ++i, ++cp) {
  492. flash_add_byte (info, &cword, (*(uchar *) cp));
  493. }
  494. return flash_write_cfiword (info, wp, cword);
  495. }
  496. /*-----------------------------------------------------------------------
  497. */
  498. #ifdef CFG_FLASH_PROTECTION
  499. int flash_real_protect (flash_info_t * info, long sector, int prot)
  500. {
  501. int retcode = 0;
  502. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  503. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  504. if (prot)
  505. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  506. else
  507. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  508. if ((retcode =
  509. flash_full_status_check (info, sector, info->erase_blk_tout,
  510. prot ? "protect" : "unprotect")) == 0) {
  511. info->protect[sector] = prot;
  512. /* Intel's unprotect unprotects all locking */
  513. if (prot == 0) {
  514. flash_sect_t i;
  515. for (i = 0; i < info->sector_count; i++) {
  516. if (info->protect[i])
  517. flash_real_protect (info, i, 1);
  518. }
  519. }
  520. }
  521. return retcode;
  522. }
  523. /*-----------------------------------------------------------------------
  524. * flash_read_user_serial - read the OneTimeProgramming cells
  525. */
  526. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  527. int len)
  528. {
  529. uchar *src;
  530. uchar *dst;
  531. dst = buffer;
  532. src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
  533. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  534. memcpy (dst, src + offset, len);
  535. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  536. }
  537. /*
  538. * flash_read_factory_serial - read the device Id from the protection area
  539. */
  540. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  541. int len)
  542. {
  543. uchar *src;
  544. src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  545. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  546. memcpy (buffer, src + offset, len);
  547. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  548. }
  549. #endif /* CFG_FLASH_PROTECTION */
  550. /*
  551. * flash_is_busy - check to see if the flash is busy
  552. * This routine checks the status of the chip and returns true if the chip is busy
  553. */
  554. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  555. {
  556. int retval;
  557. switch (info->vendor) {
  558. case CFI_CMDSET_INTEL_STANDARD:
  559. case CFI_CMDSET_INTEL_EXTENDED:
  560. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  561. break;
  562. case CFI_CMDSET_AMD_STANDARD:
  563. case CFI_CMDSET_AMD_EXTENDED:
  564. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  565. break;
  566. default:
  567. retval = 0;
  568. }
  569. debug ("flash_is_busy: %d\n", retval);
  570. return retval;
  571. }
  572. /*-----------------------------------------------------------------------
  573. * wait for XSR.7 to be set. Time out with an error if it does not.
  574. * This routine does not set the flash to read-array mode.
  575. */
  576. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  577. ulong tout, char *prompt)
  578. {
  579. ulong start;
  580. /* Wait for command completion */
  581. start = get_timer (0);
  582. while (flash_is_busy (info, sector)) {
  583. if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
  584. printf ("Flash %s timeout at address %lx data %lx\n",
  585. prompt, info->start[sector],
  586. flash_read_long (info, sector, 0));
  587. flash_write_cmd (info, sector, 0, info->cmd_reset);
  588. return ERR_TIMOUT;
  589. }
  590. }
  591. return ERR_OK;
  592. }
  593. /*-----------------------------------------------------------------------
  594. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  595. * This routine sets the flash to read-array mode.
  596. */
  597. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  598. ulong tout, char *prompt)
  599. {
  600. int retcode;
  601. retcode = flash_status_check (info, sector, tout, prompt);
  602. switch (info->vendor) {
  603. case CFI_CMDSET_INTEL_EXTENDED:
  604. case CFI_CMDSET_INTEL_STANDARD:
  605. if ((retcode != ERR_OK)
  606. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  607. retcode = ERR_INVAL;
  608. printf ("Flash %s error at address %lx\n", prompt,
  609. info->start[sector]);
  610. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
  611. puts ("Command Sequence Error.\n");
  612. } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
  613. puts ("Block Erase Error.\n");
  614. retcode = ERR_NOT_ERASED;
  615. } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
  616. puts ("Locking Error\n");
  617. }
  618. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  619. puts ("Block locked.\n");
  620. retcode = ERR_PROTECTED;
  621. }
  622. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  623. puts ("Vpp Low Error.\n");
  624. }
  625. flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
  626. break;
  627. default:
  628. break;
  629. }
  630. return retcode;
  631. }
  632. /*-----------------------------------------------------------------------
  633. */
  634. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  635. {
  636. #if defined(__LITTLE_ENDIAN)
  637. unsigned short w;
  638. unsigned int l;
  639. unsigned long long ll;
  640. #endif
  641. switch (info->portwidth) {
  642. case FLASH_CFI_8BIT:
  643. cword->c = c;
  644. break;
  645. case FLASH_CFI_16BIT:
  646. #if defined(__LITTLE_ENDIAN)
  647. w = c;
  648. w <<= 8;
  649. cword->w = (cword->w >> 8) | w;
  650. #else
  651. cword->w = (cword->w << 8) | c;
  652. #endif
  653. break;
  654. case FLASH_CFI_32BIT:
  655. #if defined(__LITTLE_ENDIAN)
  656. l = c;
  657. l <<= 24;
  658. cword->l = (cword->l >> 8) | l;
  659. #else
  660. cword->l = (cword->l << 8) | c;
  661. #endif
  662. break;
  663. case FLASH_CFI_64BIT:
  664. #if defined(__LITTLE_ENDIAN)
  665. ll = c;
  666. ll <<= 56;
  667. cword->ll = (cword->ll >> 8) | ll;
  668. #else
  669. cword->ll = (cword->ll << 8) | c;
  670. #endif
  671. break;
  672. }
  673. }
  674. /*-----------------------------------------------------------------------
  675. * make a proper sized command based on the port and chip widths
  676. */
  677. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
  678. {
  679. int i;
  680. #if defined(__LITTLE_ENDIAN)
  681. ushort stmpw;
  682. uint stmpi;
  683. #endif
  684. uchar *cp = (uchar *) cmdbuf;
  685. for (i = 0; i < info->portwidth; i++)
  686. *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
  687. #if defined(__LITTLE_ENDIAN)
  688. switch (info->portwidth) {
  689. case FLASH_CFI_8BIT:
  690. break;
  691. case FLASH_CFI_16BIT:
  692. stmpw = *(ushort *) cmdbuf;
  693. *(ushort *) cmdbuf = __swab16 (stmpw);
  694. break;
  695. case FLASH_CFI_32BIT:
  696. stmpi = *(uint *) cmdbuf;
  697. *(uint *) cmdbuf = __swab32 (stmpi);
  698. break;
  699. default:
  700. puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
  701. break;
  702. }
  703. #endif
  704. }
  705. /*
  706. * Write a proper sized command to the correct address
  707. */
  708. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  709. {
  710. volatile cfiptr_t addr;
  711. cfiword_t cword;
  712. addr.cp = flash_make_addr (info, sect, offset);
  713. flash_make_cmd (info, cmd, &cword);
  714. switch (info->portwidth) {
  715. case FLASH_CFI_8BIT:
  716. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
  717. cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  718. *addr.cp = cword.c;
  719. break;
  720. case FLASH_CFI_16BIT:
  721. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
  722. cmd, cword.w,
  723. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  724. *addr.wp = cword.w;
  725. break;
  726. case FLASH_CFI_32BIT:
  727. debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
  728. cmd, cword.l,
  729. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  730. *addr.lp = cword.l;
  731. break;
  732. case FLASH_CFI_64BIT:
  733. #ifdef DEBUG
  734. {
  735. char str[20];
  736. print_longlong (str, cword.ll);
  737. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  738. addr.llp, cmd, str,
  739. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  740. }
  741. #endif
  742. *addr.llp = cword.ll;
  743. break;
  744. }
  745. }
  746. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  747. {
  748. flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
  749. flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
  750. }
  751. /*-----------------------------------------------------------------------
  752. */
  753. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  754. {
  755. cfiptr_t cptr;
  756. cfiword_t cword;
  757. int retval;
  758. cptr.cp = flash_make_addr (info, sect, offset);
  759. flash_make_cmd (info, cmd, &cword);
  760. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
  761. switch (info->portwidth) {
  762. case FLASH_CFI_8BIT:
  763. debug ("is= %x %x\n", cptr.cp[0], cword.c);
  764. retval = (cptr.cp[0] == cword.c);
  765. break;
  766. case FLASH_CFI_16BIT:
  767. debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
  768. retval = (cptr.wp[0] == cword.w);
  769. break;
  770. case FLASH_CFI_32BIT:
  771. debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
  772. retval = (cptr.lp[0] == cword.l);
  773. break;
  774. case FLASH_CFI_64BIT:
  775. #ifdef DEBUG
  776. {
  777. char str1[20];
  778. char str2[20];
  779. print_longlong (str1, cptr.llp[0]);
  780. print_longlong (str2, cword.ll);
  781. debug ("is= %s %s\n", str1, str2);
  782. }
  783. #endif
  784. retval = (cptr.llp[0] == cword.ll);
  785. break;
  786. default:
  787. retval = 0;
  788. break;
  789. }
  790. return retval;
  791. }
  792. /*-----------------------------------------------------------------------
  793. */
  794. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  795. {
  796. cfiptr_t cptr;
  797. cfiword_t cword;
  798. int retval;
  799. cptr.cp = flash_make_addr (info, sect, offset);
  800. flash_make_cmd (info, cmd, &cword);
  801. switch (info->portwidth) {
  802. case FLASH_CFI_8BIT:
  803. retval = ((cptr.cp[0] & cword.c) == cword.c);
  804. break;
  805. case FLASH_CFI_16BIT:
  806. retval = ((cptr.wp[0] & cword.w) == cword.w);
  807. break;
  808. case FLASH_CFI_32BIT:
  809. retval = ((cptr.lp[0] & cword.l) == cword.l);
  810. break;
  811. case FLASH_CFI_64BIT:
  812. retval = ((cptr.llp[0] & cword.ll) == cword.ll);
  813. break;
  814. default:
  815. retval = 0;
  816. break;
  817. }
  818. return retval;
  819. }
  820. /*-----------------------------------------------------------------------
  821. */
  822. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  823. {
  824. cfiptr_t cptr;
  825. cfiword_t cword;
  826. int retval;
  827. cptr.cp = flash_make_addr (info, sect, offset);
  828. flash_make_cmd (info, cmd, &cword);
  829. switch (info->portwidth) {
  830. case FLASH_CFI_8BIT:
  831. retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
  832. break;
  833. case FLASH_CFI_16BIT:
  834. retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
  835. break;
  836. case FLASH_CFI_32BIT:
  837. retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
  838. break;
  839. case FLASH_CFI_64BIT:
  840. retval = ((cptr.llp[0] & cword.ll) !=
  841. (cptr.llp[0] & cword.ll));
  842. break;
  843. default:
  844. retval = 0;
  845. break;
  846. }
  847. return retval;
  848. }
  849. /*-----------------------------------------------------------------------
  850. * detect if flash is compatible with the Common Flash Interface (CFI)
  851. * http://www.jedec.org/download/search/jesd68.pdf
  852. *
  853. */
  854. static int flash_detect_cfi (flash_info_t * info)
  855. {
  856. debug ("flash detect cfi\n");
  857. for (info->portwidth = FLASH_CFI_8BIT;
  858. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  859. for (info->chipwidth = FLASH_CFI_BY8;
  860. info->chipwidth <= info->portwidth;
  861. info->chipwidth <<= 1) {
  862. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  863. flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  864. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  865. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  866. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  867. info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
  868. debug ("device interface is %d\n",
  869. info->interface);
  870. debug ("found port %d chip %d ",
  871. info->portwidth, info->chipwidth);
  872. debug ("port %d bits chip %d bits\n",
  873. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  874. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  875. return 1;
  876. }
  877. }
  878. }
  879. debug ("not found\n");
  880. return 0;
  881. }
  882. /*
  883. * The following code cannot be run from FLASH!
  884. *
  885. */
  886. static ulong flash_get_size (ulong base, int banknum)
  887. {
  888. flash_info_t *info = &flash_info[banknum];
  889. int i, j;
  890. flash_sect_t sect_cnt;
  891. unsigned long sector;
  892. unsigned long tmp;
  893. int size_ratio;
  894. uchar num_erase_regions;
  895. int erase_region_size;
  896. int erase_region_count;
  897. info->start[0] = base;
  898. if (flash_detect_cfi (info)) {
  899. info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
  900. #ifdef DEBUG
  901. flash_printqry (info, 0);
  902. #endif
  903. switch (info->vendor) {
  904. case CFI_CMDSET_INTEL_STANDARD:
  905. case CFI_CMDSET_INTEL_EXTENDED:
  906. default:
  907. info->cmd_reset = FLASH_CMD_RESET;
  908. break;
  909. case CFI_CMDSET_AMD_STANDARD:
  910. case CFI_CMDSET_AMD_EXTENDED:
  911. info->cmd_reset = AMD_CMD_RESET;
  912. break;
  913. }
  914. debug ("manufacturer is %d\n", info->vendor);
  915. size_ratio = info->portwidth / info->chipwidth;
  916. /* if the chip is x8/x16 reduce the ratio by half */
  917. if ((info->interface == FLASH_CFI_X8X16)
  918. && (info->chipwidth == FLASH_CFI_BY8)) {
  919. size_ratio >>= 1;
  920. }
  921. num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  922. debug ("size_ratio %d port %d bits chip %d bits\n",
  923. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  924. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  925. debug ("found %d erase regions\n", num_erase_regions);
  926. sect_cnt = 0;
  927. sector = base;
  928. for (i = 0; i < num_erase_regions; i++) {
  929. if (i > NUM_ERASE_REGIONS) {
  930. printf ("%d erase regions found, only %d used\n",
  931. num_erase_regions, NUM_ERASE_REGIONS);
  932. break;
  933. }
  934. tmp = flash_read_long (info, 0,
  935. FLASH_OFFSET_ERASE_REGIONS +
  936. i * 4);
  937. erase_region_size =
  938. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  939. tmp >>= 16;
  940. erase_region_count = (tmp & 0xffff) + 1;
  941. debug ("erase_region_count = %d erase_region_size = %d\n",
  942. erase_region_count, erase_region_size);
  943. for (j = 0; j < erase_region_count; j++) {
  944. info->start[sect_cnt] = sector;
  945. sector += (erase_region_size * size_ratio);
  946. info->protect[sect_cnt] =
  947. flash_isset (info, sect_cnt,
  948. FLASH_OFFSET_PROTECT,
  949. FLASH_STATUS_PROTECT);
  950. sect_cnt++;
  951. }
  952. }
  953. info->sector_count = sect_cnt;
  954. /* multiply the size by the number of chips */
  955. info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
  956. info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
  957. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
  958. info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
  959. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
  960. info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
  961. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
  962. info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
  963. info->flash_id = FLASH_MAN_CFI;
  964. if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
  965. info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
  966. }
  967. }
  968. flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
  969. return (info->size);
  970. }
  971. /*-----------------------------------------------------------------------
  972. */
  973. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  974. cfiword_t cword)
  975. {
  976. cfiptr_t ctladdr;
  977. cfiptr_t cptr;
  978. int flag;
  979. ctladdr.cp = flash_make_addr (info, 0, 0);
  980. cptr.cp = (uchar *) dest;
  981. /* Check if Flash is (sufficiently) erased */
  982. switch (info->portwidth) {
  983. case FLASH_CFI_8BIT:
  984. flag = ((cptr.cp[0] & cword.c) == cword.c);
  985. break;
  986. case FLASH_CFI_16BIT:
  987. flag = ((cptr.wp[0] & cword.w) == cword.w);
  988. break;
  989. case FLASH_CFI_32BIT:
  990. flag = ((cptr.lp[0] & cword.l) == cword.l);
  991. break;
  992. case FLASH_CFI_64BIT:
  993. flag = ((cptr.lp[0] & cword.ll) == cword.ll);
  994. break;
  995. default:
  996. return 2;
  997. }
  998. if (!flag)
  999. return 2;
  1000. /* Disable interrupts which might cause a timeout here */
  1001. flag = disable_interrupts ();
  1002. switch (info->vendor) {
  1003. case CFI_CMDSET_INTEL_EXTENDED:
  1004. case CFI_CMDSET_INTEL_STANDARD:
  1005. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  1006. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  1007. break;
  1008. case CFI_CMDSET_AMD_EXTENDED:
  1009. case CFI_CMDSET_AMD_STANDARD:
  1010. flash_unlock_seq (info, 0);
  1011. flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
  1012. break;
  1013. }
  1014. switch (info->portwidth) {
  1015. case FLASH_CFI_8BIT:
  1016. cptr.cp[0] = cword.c;
  1017. break;
  1018. case FLASH_CFI_16BIT:
  1019. cptr.wp[0] = cword.w;
  1020. break;
  1021. case FLASH_CFI_32BIT:
  1022. cptr.lp[0] = cword.l;
  1023. break;
  1024. case FLASH_CFI_64BIT:
  1025. cptr.llp[0] = cword.ll;
  1026. break;
  1027. }
  1028. /* re-enable interrupts if necessary */
  1029. if (flag)
  1030. enable_interrupts ();
  1031. return flash_full_status_check (info, 0, info->write_tout, "write");
  1032. }
  1033. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  1034. /* loop through the sectors from the highest address
  1035. * when the passed address is greater or equal to the sector address
  1036. * we have a match
  1037. */
  1038. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  1039. {
  1040. flash_sect_t sector;
  1041. for (sector = info->sector_count - 1; sector >= 0; sector--) {
  1042. if (addr >= info->start[sector])
  1043. break;
  1044. }
  1045. return sector;
  1046. }
  1047. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  1048. int len)
  1049. {
  1050. flash_sect_t sector;
  1051. int cnt;
  1052. int retcode;
  1053. volatile cfiptr_t src;
  1054. volatile cfiptr_t dst;
  1055. /* buffered writes in the AMD chip set is not supported yet */
  1056. if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
  1057. (info->vendor == CFI_CMDSET_AMD_EXTENDED))
  1058. return ERR_INVAL;
  1059. src.cp = cp;
  1060. dst.cp = (uchar *) dest;
  1061. sector = find_sector (info, dest);
  1062. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1063. flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  1064. if ((retcode =
  1065. flash_status_check (info, sector, info->buffer_write_tout,
  1066. "write to buffer")) == ERR_OK) {
  1067. /* reduce the number of loops by the width of the port */
  1068. switch (info->portwidth) {
  1069. case FLASH_CFI_8BIT:
  1070. cnt = len;
  1071. break;
  1072. case FLASH_CFI_16BIT:
  1073. cnt = len >> 1;
  1074. break;
  1075. case FLASH_CFI_32BIT:
  1076. cnt = len >> 2;
  1077. break;
  1078. case FLASH_CFI_64BIT:
  1079. cnt = len >> 3;
  1080. break;
  1081. default:
  1082. return ERR_INVAL;
  1083. break;
  1084. }
  1085. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1086. while (cnt-- > 0) {
  1087. switch (info->portwidth) {
  1088. case FLASH_CFI_8BIT:
  1089. *dst.cp++ = *src.cp++;
  1090. break;
  1091. case FLASH_CFI_16BIT:
  1092. *dst.wp++ = *src.wp++;
  1093. break;
  1094. case FLASH_CFI_32BIT:
  1095. *dst.lp++ = *src.lp++;
  1096. break;
  1097. case FLASH_CFI_64BIT:
  1098. *dst.llp++ = *src.llp++;
  1099. break;
  1100. default:
  1101. return ERR_INVAL;
  1102. break;
  1103. }
  1104. }
  1105. flash_write_cmd (info, sector, 0,
  1106. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  1107. retcode =
  1108. flash_full_status_check (info, sector,
  1109. info->buffer_write_tout,
  1110. "buffer write");
  1111. }
  1112. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1113. return retcode;
  1114. }
  1115. #endif /* CFG_USE_FLASH_BUFFER_WRITE */
  1116. #endif /* CFG_FLASH_CFI */