cfi_flash.c 42 KB

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