cfi_flash.c 34 KB

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