cfi_flash.c 34 KB

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