cfi_flash.c 42 KB

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