cfi_flash.c 38 KB

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