cfi_flash.c 34 KB

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