cfi_flash.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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 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) || defined(CFG_WRITE_SWAPPED_DATA)
  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) || defined(CFG_WRITE_SWAPPED_DATA)
  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) || defined(CFG_WRITE_SWAPPED_DATA)
  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. #ifdef CONFIG_FLASH_CFI_LEGACY
  294. /*-----------------------------------------------------------------------
  295. * Call board code to request info about non-CFI flash.
  296. * board_flash_get_legacy needs to fill in at least:
  297. * info->portwidth, info->chipwidth and info->interface for Jedec probing.
  298. */
  299. int flash_detect_legacy(ulong base, int banknum)
  300. {
  301. flash_info_t *info = &flash_info[banknum];
  302. if (board_flash_get_legacy(base, banknum, info)) {
  303. /* board code may have filled info completely. If not, we
  304. use JEDEC ID probing. */
  305. if (!info->vendor) {
  306. int modes[] = { CFI_CMDSET_AMD_STANDARD, CFI_CMDSET_INTEL_STANDARD };
  307. int i;
  308. for(i=0; i<sizeof(modes)/sizeof(modes[0]); i++) {
  309. info->vendor = modes[i];
  310. info->start[0] = base;
  311. if (info->portwidth == FLASH_CFI_8BIT && info->interface == FLASH_CFI_X8X16) {
  312. info->addr_unlock1 = 0x2AAA;
  313. info->addr_unlock2 = 0x5555;
  314. } else {
  315. info->addr_unlock1 = 0x5555;
  316. info->addr_unlock2 = 0x2AAA;
  317. }
  318. flash_read_jedec_ids(info);
  319. debug("JEDEC PROBE: ID %x %x %x\n", info->manufacturer_id, info->device_id, info->device_id2);
  320. if (jedec_flash_match(info, base))
  321. break;
  322. }
  323. }
  324. switch(info->vendor) {
  325. case CFI_CMDSET_INTEL_STANDARD:
  326. case CFI_CMDSET_INTEL_EXTENDED:
  327. info->cmd_reset = FLASH_CMD_RESET;
  328. break;
  329. case CFI_CMDSET_AMD_STANDARD:
  330. case CFI_CMDSET_AMD_EXTENDED:
  331. case CFI_CMDSET_AMD_LEGACY:
  332. info->cmd_reset = AMD_CMD_RESET;
  333. break;
  334. }
  335. info->flash_id = FLASH_MAN_CFI;
  336. return 1;
  337. }
  338. return 0; /* use CFI */
  339. }
  340. #else
  341. int inline flash_detect_legacy(ulong base, int banknum)
  342. {
  343. return 0; /* use CFI */
  344. }
  345. #endif
  346. /*-----------------------------------------------------------------------
  347. */
  348. unsigned long flash_init (void)
  349. {
  350. unsigned long size = 0;
  351. int i;
  352. #ifdef CFG_FLASH_PROTECTION
  353. char *s = getenv("unlock");
  354. #endif
  355. /* Init: no FLASHes known */
  356. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  357. flash_info[i].flash_id = FLASH_UNKNOWN;
  358. if (!flash_detect_legacy (bank_base[i], i))
  359. flash_get_size (bank_base[i], i);
  360. size += flash_info[i].size;
  361. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  362. #ifndef CFG_FLASH_QUIET_TEST
  363. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  364. i+1, flash_info[i].size, flash_info[i].size << 20);
  365. #endif /* CFG_FLASH_QUIET_TEST */
  366. }
  367. #ifdef CFG_FLASH_PROTECTION
  368. else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
  369. /*
  370. * Only the U-Boot image and it's environment is protected,
  371. * all other sectors are unprotected (unlocked) if flash
  372. * hardware protection is used (CFG_FLASH_PROTECTION) and
  373. * the environment variable "unlock" is set to "yes".
  374. */
  375. if (flash_info[i].legacy_unlock) {
  376. int k;
  377. /*
  378. * Disable legacy_unlock temporarily, since
  379. * flash_real_protect would relock all other sectors
  380. * again otherwise.
  381. */
  382. flash_info[i].legacy_unlock = 0;
  383. /*
  384. * Legacy unlocking (e.g. Intel J3) -> unlock only one
  385. * sector. This will unlock all sectors.
  386. */
  387. flash_real_protect (&flash_info[i], 0, 0);
  388. flash_info[i].legacy_unlock = 1;
  389. /*
  390. * Manually mark other sectors as unlocked (unprotected)
  391. */
  392. for (k = 1; k < flash_info[i].sector_count; k++)
  393. flash_info[i].protect[k] = 0;
  394. } else {
  395. /*
  396. * No legancy unlocking -> unlock all sectors
  397. */
  398. flash_protect (FLAG_PROTECT_CLEAR,
  399. flash_info[i].start[0],
  400. flash_info[i].start[0] + flash_info[i].size - 1,
  401. &flash_info[i]);
  402. }
  403. }
  404. #endif /* CFG_FLASH_PROTECTION */
  405. }
  406. /* Monitor protection ON by default */
  407. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  408. flash_protect (FLAG_PROTECT_SET,
  409. CFG_MONITOR_BASE,
  410. CFG_MONITOR_BASE + monitor_flash_len - 1,
  411. flash_get_info(CFG_MONITOR_BASE));
  412. #endif
  413. /* Environment protection ON by default */
  414. #ifdef CFG_ENV_IS_IN_FLASH
  415. flash_protect (FLAG_PROTECT_SET,
  416. CFG_ENV_ADDR,
  417. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  418. flash_get_info(CFG_ENV_ADDR));
  419. #endif
  420. /* Redundant environment protection ON by default */
  421. #ifdef CFG_ENV_ADDR_REDUND
  422. flash_protect (FLAG_PROTECT_SET,
  423. CFG_ENV_ADDR_REDUND,
  424. CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
  425. flash_get_info(CFG_ENV_ADDR_REDUND));
  426. #endif
  427. return (size);
  428. }
  429. /*-----------------------------------------------------------------------
  430. */
  431. #if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  432. static flash_info_t *flash_get_info(ulong base)
  433. {
  434. int i;
  435. flash_info_t * info = 0;
  436. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  437. info = & flash_info[i];
  438. if (info->size && info->start[0] <= base &&
  439. base <= info->start[0] + info->size - 1)
  440. break;
  441. }
  442. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  443. }
  444. #endif
  445. /*-----------------------------------------------------------------------
  446. */
  447. int flash_erase (flash_info_t * info, int s_first, int s_last)
  448. {
  449. int rcode = 0;
  450. int prot;
  451. flash_sect_t sect;
  452. if (info->flash_id != FLASH_MAN_CFI) {
  453. puts ("Can't erase unknown flash type - aborted\n");
  454. return 1;
  455. }
  456. if ((s_first < 0) || (s_first > s_last)) {
  457. puts ("- no sectors to erase\n");
  458. return 1;
  459. }
  460. prot = 0;
  461. for (sect = s_first; sect <= s_last; ++sect) {
  462. if (info->protect[sect]) {
  463. prot++;
  464. }
  465. }
  466. if (prot) {
  467. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  468. } else {
  469. putc ('\n');
  470. }
  471. for (sect = s_first; sect <= s_last; sect++) {
  472. if (info->protect[sect] == 0) { /* not protected */
  473. switch (info->vendor) {
  474. case CFI_CMDSET_INTEL_STANDARD:
  475. case CFI_CMDSET_INTEL_EXTENDED:
  476. flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  477. flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  478. flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  479. break;
  480. case CFI_CMDSET_AMD_STANDARD:
  481. case CFI_CMDSET_AMD_EXTENDED:
  482. flash_unlock_seq (info, sect);
  483. flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_ERASE_START);
  484. flash_unlock_seq (info, sect);
  485. flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
  486. break;
  487. #ifdef CONFIG_FLASH_CFI_LEGACY
  488. case CFI_CMDSET_AMD_LEGACY:
  489. flash_unlock_seq (info, 0);
  490. flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_ERASE_START);
  491. flash_unlock_seq (info, 0);
  492. flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
  493. break;
  494. #endif
  495. default:
  496. debug ("Unkown flash vendor %d\n",
  497. info->vendor);
  498. break;
  499. }
  500. if (flash_full_status_check
  501. (info, sect, info->erase_blk_tout, "erase")) {
  502. rcode = 1;
  503. } else
  504. putc ('.');
  505. }
  506. }
  507. puts (" done\n");
  508. return rcode;
  509. }
  510. /*-----------------------------------------------------------------------
  511. */
  512. void flash_print_info (flash_info_t * info)
  513. {
  514. int i;
  515. if (info->flash_id != FLASH_MAN_CFI) {
  516. puts ("missing or unknown FLASH type\n");
  517. return;
  518. }
  519. printf ("%s FLASH (%d x %d)",
  520. info->name,
  521. (info->portwidth << 3), (info->chipwidth << 3));
  522. if (info->size < 1024*1024)
  523. printf (" Size: %ld kB in %d Sectors\n",
  524. info->size >> 10, info->sector_count);
  525. else
  526. printf (" Size: %ld MB in %d Sectors\n",
  527. info->size >> 20, info->sector_count);
  528. printf (" ");
  529. switch (info->vendor) {
  530. case CFI_CMDSET_INTEL_STANDARD:
  531. printf ("Intel Standard");
  532. break;
  533. case CFI_CMDSET_INTEL_EXTENDED:
  534. printf ("Intel Extended");
  535. break;
  536. case CFI_CMDSET_AMD_STANDARD:
  537. printf ("AMD Standard");
  538. break;
  539. case CFI_CMDSET_AMD_EXTENDED:
  540. printf ("AMD Extended");
  541. break;
  542. #ifdef CONFIG_FLASH_CFI_LEGACY
  543. case CFI_CMDSET_AMD_LEGACY:
  544. printf ("AMD Legacy");
  545. break;
  546. #endif
  547. default:
  548. printf ("Unknown (%d)", info->vendor);
  549. break;
  550. }
  551. printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x%02X",
  552. info->manufacturer_id, info->device_id);
  553. if (info->device_id == 0x7E) {
  554. printf("%04X", info->device_id2);
  555. }
  556. printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
  557. info->erase_blk_tout,
  558. info->write_tout);
  559. if (info->buffer_size > 1) {
  560. printf (" Buffer write timeout: %ld ms, buffer size: %d bytes\n",
  561. info->buffer_write_tout,
  562. info->buffer_size);
  563. }
  564. puts ("\n Sector Start Addresses:");
  565. for (i = 0; i < info->sector_count; ++i) {
  566. if ((i % 5) == 0)
  567. printf ("\n");
  568. #ifdef CFG_FLASH_EMPTY_INFO
  569. int k;
  570. int size;
  571. int erased;
  572. volatile unsigned long *flash;
  573. /*
  574. * Check if whole sector is erased
  575. */
  576. if (i != (info->sector_count - 1))
  577. size = info->start[i + 1] - info->start[i];
  578. else
  579. size = info->start[0] + info->size - info->start[i];
  580. erased = 1;
  581. flash = (volatile unsigned long *) info->start[i];
  582. size = size >> 2; /* divide by 4 for longword access */
  583. for (k = 0; k < size; k++) {
  584. if (*flash++ != 0xffffffff) {
  585. erased = 0;
  586. break;
  587. }
  588. }
  589. /* print empty and read-only info */
  590. printf (" %08lX %c %s ",
  591. info->start[i],
  592. erased ? 'E' : ' ',
  593. info->protect[i] ? "RO" : " ");
  594. #else /* ! CFG_FLASH_EMPTY_INFO */
  595. printf (" %08lX %s ",
  596. info->start[i],
  597. info->protect[i] ? "RO" : " ");
  598. #endif
  599. }
  600. putc ('\n');
  601. return;
  602. }
  603. /*-----------------------------------------------------------------------
  604. * Copy memory to flash, returns:
  605. * 0 - OK
  606. * 1 - write timeout
  607. * 2 - Flash not erased
  608. */
  609. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  610. {
  611. ulong wp;
  612. ulong cp;
  613. int aln;
  614. cfiword_t cword;
  615. int i, rc;
  616. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  617. int buffered_size;
  618. #endif
  619. /* get lower aligned address */
  620. /* get lower aligned address */
  621. wp = (addr & ~(info->portwidth - 1));
  622. /* handle unaligned start */
  623. if ((aln = addr - wp) != 0) {
  624. cword.l = 0;
  625. cp = wp;
  626. for (i = 0; i < aln; ++i, ++cp)
  627. flash_add_byte (info, &cword, (*(uchar *) cp));
  628. for (; (i < info->portwidth) && (cnt > 0); i++) {
  629. flash_add_byte (info, &cword, *src++);
  630. cnt--;
  631. cp++;
  632. }
  633. for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  634. flash_add_byte (info, &cword, (*(uchar *) cp));
  635. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  636. return rc;
  637. wp = cp;
  638. }
  639. /* handle the aligned part */
  640. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  641. buffered_size = (info->portwidth / info->chipwidth);
  642. buffered_size *= info->buffer_size;
  643. while (cnt >= info->portwidth) {
  644. /* prohibit buffer write when buffer_size is 1 */
  645. if (info->buffer_size == 1) {
  646. cword.l = 0;
  647. for (i = 0; i < info->portwidth; i++)
  648. flash_add_byte (info, &cword, *src++);
  649. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  650. return rc;
  651. wp += info->portwidth;
  652. cnt -= info->portwidth;
  653. continue;
  654. }
  655. /* write buffer until next buffered_size aligned boundary */
  656. i = buffered_size - (wp % buffered_size);
  657. if (i > cnt)
  658. i = cnt;
  659. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  660. return rc;
  661. i -= i & (info->portwidth - 1);
  662. wp += i;
  663. src += i;
  664. cnt -= i;
  665. }
  666. #else
  667. while (cnt >= info->portwidth) {
  668. cword.l = 0;
  669. for (i = 0; i < info->portwidth; i++) {
  670. flash_add_byte (info, &cword, *src++);
  671. }
  672. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  673. return rc;
  674. wp += info->portwidth;
  675. cnt -= info->portwidth;
  676. }
  677. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  678. if (cnt == 0) {
  679. return (0);
  680. }
  681. /*
  682. * handle unaligned tail bytes
  683. */
  684. cword.l = 0;
  685. for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
  686. flash_add_byte (info, &cword, *src++);
  687. --cnt;
  688. }
  689. for (; i < info->portwidth; ++i, ++cp) {
  690. flash_add_byte (info, &cword, (*(uchar *) cp));
  691. }
  692. return flash_write_cfiword (info, wp, cword);
  693. }
  694. /*-----------------------------------------------------------------------
  695. */
  696. #ifdef CFG_FLASH_PROTECTION
  697. int flash_real_protect (flash_info_t * info, long sector, int prot)
  698. {
  699. int retcode = 0;
  700. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  701. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  702. if (prot)
  703. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  704. else
  705. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  706. if ((retcode =
  707. flash_full_status_check (info, sector, info->erase_blk_tout,
  708. prot ? "protect" : "unprotect")) == 0) {
  709. info->protect[sector] = prot;
  710. /*
  711. * On some of Intel's flash chips (marked via legacy_unlock)
  712. * unprotect unprotects all locking.
  713. */
  714. if ((prot == 0) && (info->legacy_unlock)) {
  715. flash_sect_t i;
  716. for (i = 0; i < info->sector_count; i++) {
  717. if (info->protect[i])
  718. flash_real_protect (info, i, 1);
  719. }
  720. }
  721. }
  722. return retcode;
  723. }
  724. /*-----------------------------------------------------------------------
  725. * flash_read_user_serial - read the OneTimeProgramming cells
  726. */
  727. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  728. int len)
  729. {
  730. uchar *src;
  731. uchar *dst;
  732. dst = buffer;
  733. src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
  734. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  735. memcpy (dst, src + offset, len);
  736. flash_write_cmd (info, 0, 0, info->cmd_reset);
  737. }
  738. /*
  739. * flash_read_factory_serial - read the device Id from the protection area
  740. */
  741. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  742. int len)
  743. {
  744. uchar *src;
  745. src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  746. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  747. memcpy (buffer, src + offset, len);
  748. flash_write_cmd (info, 0, 0, info->cmd_reset);
  749. }
  750. #endif /* CFG_FLASH_PROTECTION */
  751. /*
  752. * flash_is_busy - check to see if the flash is busy
  753. * This routine checks the status of the chip and returns true if the chip is busy
  754. */
  755. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  756. {
  757. int retval;
  758. switch (info->vendor) {
  759. case CFI_CMDSET_INTEL_STANDARD:
  760. case CFI_CMDSET_INTEL_EXTENDED:
  761. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  762. break;
  763. case CFI_CMDSET_AMD_STANDARD:
  764. case CFI_CMDSET_AMD_EXTENDED:
  765. #ifdef CONFIG_FLASH_CFI_LEGACY
  766. case CFI_CMDSET_AMD_LEGACY:
  767. #endif
  768. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  769. break;
  770. default:
  771. retval = 0;
  772. }
  773. debug ("flash_is_busy: %d\n", retval);
  774. return retval;
  775. }
  776. /*-----------------------------------------------------------------------
  777. * wait for XSR.7 to be set. Time out with an error if it does not.
  778. * This routine does not set the flash to read-array mode.
  779. */
  780. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  781. ulong tout, char *prompt)
  782. {
  783. ulong start;
  784. #if CFG_HZ != 1000
  785. tout *= CFG_HZ/1000;
  786. #endif
  787. /* Wait for command completion */
  788. start = get_timer (0);
  789. while (flash_is_busy (info, sector)) {
  790. if (get_timer (start) > tout) {
  791. printf ("Flash %s timeout at address %lx data %lx\n",
  792. prompt, info->start[sector],
  793. flash_read_long (info, sector, 0));
  794. flash_write_cmd (info, sector, 0, info->cmd_reset);
  795. return ERR_TIMOUT;
  796. }
  797. udelay (1); /* also triggers watchdog */
  798. }
  799. return ERR_OK;
  800. }
  801. /*-----------------------------------------------------------------------
  802. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  803. * This routine sets the flash to read-array mode.
  804. */
  805. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  806. ulong tout, char *prompt)
  807. {
  808. int retcode;
  809. retcode = flash_status_check (info, sector, tout, prompt);
  810. switch (info->vendor) {
  811. case CFI_CMDSET_INTEL_EXTENDED:
  812. case CFI_CMDSET_INTEL_STANDARD:
  813. if ((retcode == ERR_OK)
  814. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  815. retcode = ERR_INVAL;
  816. printf ("Flash %s error at address %lx\n", prompt,
  817. info->start[sector]);
  818. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
  819. puts ("Command Sequence Error.\n");
  820. } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
  821. puts ("Block Erase Error.\n");
  822. retcode = ERR_NOT_ERASED;
  823. } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
  824. puts ("Locking Error\n");
  825. }
  826. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  827. puts ("Block locked.\n");
  828. retcode = ERR_PROTECTED;
  829. }
  830. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  831. puts ("Vpp Low Error.\n");
  832. }
  833. flash_write_cmd (info, sector, 0, info->cmd_reset);
  834. break;
  835. default:
  836. break;
  837. }
  838. return retcode;
  839. }
  840. /*-----------------------------------------------------------------------
  841. */
  842. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  843. {
  844. #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
  845. unsigned short w;
  846. unsigned int l;
  847. unsigned long long ll;
  848. #endif
  849. switch (info->portwidth) {
  850. case FLASH_CFI_8BIT:
  851. cword->c = c;
  852. break;
  853. case FLASH_CFI_16BIT:
  854. #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
  855. w = c;
  856. w <<= 8;
  857. cword->w = (cword->w >> 8) | w;
  858. #else
  859. cword->w = (cword->w << 8) | c;
  860. #endif
  861. break;
  862. case FLASH_CFI_32BIT:
  863. #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
  864. l = c;
  865. l <<= 24;
  866. cword->l = (cword->l >> 8) | l;
  867. #else
  868. cword->l = (cword->l << 8) | c;
  869. #endif
  870. break;
  871. case FLASH_CFI_64BIT:
  872. #if defined(__LITTLE_ENDIAN) && !defined(CFG_WRITE_SWAPPED_DATA)
  873. ll = c;
  874. ll <<= 56;
  875. cword->ll = (cword->ll >> 8) | ll;
  876. #else
  877. cword->ll = (cword->ll << 8) | c;
  878. #endif
  879. break;
  880. }
  881. }
  882. /*-----------------------------------------------------------------------
  883. * make a proper sized command based on the port and chip widths
  884. */
  885. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
  886. {
  887. int i;
  888. uchar *cp = (uchar *) cmdbuf;
  889. #if defined(__LITTLE_ENDIAN) || defined(CFG_WRITE_SWAPPED_DATA)
  890. for (i = info->portwidth; i > 0; i--)
  891. #else
  892. for (i = 1; i <= info->portwidth; i++)
  893. #endif
  894. *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
  895. }
  896. /*
  897. * Write a proper sized command to the correct address
  898. */
  899. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  900. {
  901. volatile cfiptr_t addr;
  902. cfiword_t cword;
  903. addr.cp = flash_make_addr (info, sect, offset);
  904. flash_make_cmd (info, cmd, &cword);
  905. switch (info->portwidth) {
  906. case FLASH_CFI_8BIT:
  907. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
  908. cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  909. *addr.cp = cword.c;
  910. break;
  911. case FLASH_CFI_16BIT:
  912. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
  913. cmd, cword.w,
  914. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  915. *addr.wp = cword.w;
  916. break;
  917. case FLASH_CFI_32BIT:
  918. debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
  919. cmd, cword.l,
  920. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  921. *addr.lp = cword.l;
  922. break;
  923. case FLASH_CFI_64BIT:
  924. #ifdef DEBUG
  925. {
  926. char str[20];
  927. print_longlong (str, cword.ll);
  928. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  929. addr.llp, cmd, str,
  930. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  931. }
  932. #endif
  933. *addr.llp = cword.ll;
  934. break;
  935. }
  936. /* Ensure all the instructions are fully finished */
  937. sync();
  938. }
  939. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  940. {
  941. flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
  942. flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
  943. }
  944. /*-----------------------------------------------------------------------
  945. */
  946. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  947. {
  948. cfiptr_t cptr;
  949. cfiword_t cword;
  950. int retval;
  951. cptr.cp = flash_make_addr (info, sect, offset);
  952. flash_make_cmd (info, cmd, &cword);
  953. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
  954. switch (info->portwidth) {
  955. case FLASH_CFI_8BIT:
  956. debug ("is= %x %x\n", cptr.cp[0], cword.c);
  957. retval = (cptr.cp[0] == cword.c);
  958. break;
  959. case FLASH_CFI_16BIT:
  960. debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
  961. retval = (cptr.wp[0] == cword.w);
  962. break;
  963. case FLASH_CFI_32BIT:
  964. debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
  965. retval = (cptr.lp[0] == cword.l);
  966. break;
  967. case FLASH_CFI_64BIT:
  968. #ifdef DEBUG
  969. {
  970. char str1[20];
  971. char str2[20];
  972. print_longlong (str1, cptr.llp[0]);
  973. print_longlong (str2, cword.ll);
  974. debug ("is= %s %s\n", str1, str2);
  975. }
  976. #endif
  977. retval = (cptr.llp[0] == cword.ll);
  978. break;
  979. default:
  980. retval = 0;
  981. break;
  982. }
  983. return retval;
  984. }
  985. /*-----------------------------------------------------------------------
  986. */
  987. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  988. {
  989. cfiptr_t cptr;
  990. cfiword_t cword;
  991. int retval;
  992. cptr.cp = flash_make_addr (info, sect, offset);
  993. flash_make_cmd (info, cmd, &cword);
  994. switch (info->portwidth) {
  995. case FLASH_CFI_8BIT:
  996. retval = ((cptr.cp[0] & cword.c) == cword.c);
  997. break;
  998. case FLASH_CFI_16BIT:
  999. retval = ((cptr.wp[0] & cword.w) == cword.w);
  1000. break;
  1001. case FLASH_CFI_32BIT:
  1002. retval = ((cptr.lp[0] & cword.l) == cword.l);
  1003. break;
  1004. case FLASH_CFI_64BIT:
  1005. retval = ((cptr.llp[0] & cword.ll) == cword.ll);
  1006. break;
  1007. default:
  1008. retval = 0;
  1009. break;
  1010. }
  1011. return retval;
  1012. }
  1013. /*-----------------------------------------------------------------------
  1014. */
  1015. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  1016. {
  1017. cfiptr_t cptr;
  1018. cfiword_t cword;
  1019. int retval;
  1020. cptr.cp = flash_make_addr (info, sect, offset);
  1021. flash_make_cmd (info, cmd, &cword);
  1022. switch (info->portwidth) {
  1023. case FLASH_CFI_8BIT:
  1024. retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
  1025. break;
  1026. case FLASH_CFI_16BIT:
  1027. retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
  1028. break;
  1029. case FLASH_CFI_32BIT:
  1030. retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
  1031. break;
  1032. case FLASH_CFI_64BIT:
  1033. retval = ((cptr.llp[0] & cword.ll) !=
  1034. (cptr.llp[0] & cword.ll));
  1035. break;
  1036. default:
  1037. retval = 0;
  1038. break;
  1039. }
  1040. return retval;
  1041. }
  1042. /*-----------------------------------------------------------------------
  1043. * read jedec ids from device and set corresponding fields in info struct
  1044. *
  1045. * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
  1046. *
  1047. */
  1048. static void flash_read_jedec_ids (flash_info_t * info)
  1049. {
  1050. info->manufacturer_id = 0;
  1051. info->device_id = 0;
  1052. info->device_id2 = 0;
  1053. switch (info->vendor) {
  1054. case CFI_CMDSET_INTEL_STANDARD:
  1055. case CFI_CMDSET_INTEL_EXTENDED:
  1056. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1057. flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
  1058. udelay(1000); /* some flash are slow to respond */
  1059. info->manufacturer_id = flash_read_uchar (info,
  1060. FLASH_OFFSET_MANUFACTURER_ID);
  1061. info->device_id = flash_read_uchar (info,
  1062. FLASH_OFFSET_DEVICE_ID);
  1063. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1064. break;
  1065. case CFI_CMDSET_AMD_STANDARD:
  1066. case CFI_CMDSET_AMD_EXTENDED:
  1067. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1068. flash_unlock_seq(info, 0);
  1069. flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
  1070. udelay(1000); /* some flash are slow to respond */
  1071. info->manufacturer_id = flash_read_uchar (info,
  1072. FLASH_OFFSET_MANUFACTURER_ID);
  1073. info->device_id = flash_read_uchar (info,
  1074. FLASH_OFFSET_DEVICE_ID);
  1075. if (info->device_id == 0x7E) {
  1076. /* AMD 3-byte (expanded) device ids */
  1077. info->device_id2 = flash_read_uchar (info,
  1078. FLASH_OFFSET_DEVICE_ID2);
  1079. info->device_id2 <<= 8;
  1080. info->device_id2 |= flash_read_uchar (info,
  1081. FLASH_OFFSET_DEVICE_ID3);
  1082. }
  1083. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1084. break;
  1085. default:
  1086. break;
  1087. }
  1088. }
  1089. /*-----------------------------------------------------------------------
  1090. * detect if flash is compatible with the Common Flash Interface (CFI)
  1091. * http://www.jedec.org/download/search/jesd68.pdf
  1092. *
  1093. */
  1094. static int flash_detect_cfi (flash_info_t * info)
  1095. {
  1096. int cfi_offset;
  1097. debug ("flash detect cfi\n");
  1098. for (info->portwidth = CFG_FLASH_CFI_WIDTH;
  1099. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  1100. for (info->chipwidth = FLASH_CFI_BY8;
  1101. info->chipwidth <= info->portwidth;
  1102. info->chipwidth <<= 1) {
  1103. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1104. for (cfi_offset=0; cfi_offset < sizeof(flash_offset_cfi)/sizeof(uint); cfi_offset++) {
  1105. flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset], FLASH_CMD_CFI);
  1106. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  1107. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  1108. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  1109. info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
  1110. info->cfi_offset=flash_offset_cfi[cfi_offset];
  1111. debug ("device interface is %d\n",
  1112. info->interface);
  1113. debug ("found port %d chip %d ",
  1114. info->portwidth, info->chipwidth);
  1115. debug ("port %d bits chip %d bits\n",
  1116. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1117. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1118. /* this probably only works if info->interface == FLASH_CFI_X8X16 */
  1119. info->addr_unlock1 = (info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555;
  1120. info->addr_unlock2 = (info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA;
  1121. info->name = "CFI conformant";
  1122. return 1;
  1123. }
  1124. }
  1125. }
  1126. }
  1127. debug ("not found\n");
  1128. return 0;
  1129. }
  1130. /*
  1131. * The following code cannot be run from FLASH!
  1132. *
  1133. */
  1134. ulong flash_get_size (ulong base, int banknum)
  1135. {
  1136. flash_info_t *info = &flash_info[banknum];
  1137. int i, j;
  1138. flash_sect_t sect_cnt;
  1139. unsigned long sector;
  1140. unsigned long tmp;
  1141. int size_ratio;
  1142. uchar num_erase_regions;
  1143. int erase_region_size;
  1144. int erase_region_count;
  1145. int geometry_reversed = 0;
  1146. info->ext_addr = 0;
  1147. info->cfi_version = 0;
  1148. #ifdef CFG_FLASH_PROTECTION
  1149. info->legacy_unlock = 0;
  1150. #endif
  1151. info->start[0] = base;
  1152. if (flash_detect_cfi (info)) {
  1153. info->vendor = flash_read_ushort (info, 0,
  1154. FLASH_OFFSET_PRIMARY_VENDOR);
  1155. flash_read_jedec_ids (info);
  1156. flash_write_cmd (info, 0, info->cfi_offset, FLASH_CMD_CFI);
  1157. num_erase_regions = flash_read_uchar (info,
  1158. FLASH_OFFSET_NUM_ERASE_REGIONS);
  1159. info->ext_addr = flash_read_ushort (info, 0,
  1160. FLASH_OFFSET_EXT_QUERY_T_P_ADDR);
  1161. if (info->ext_addr) {
  1162. info->cfi_version = (ushort) flash_read_uchar (info,
  1163. info->ext_addr + 3) << 8;
  1164. info->cfi_version |= (ushort) flash_read_uchar (info,
  1165. info->ext_addr + 4);
  1166. }
  1167. #ifdef DEBUG
  1168. flash_printqry (info, 0);
  1169. #endif
  1170. switch (info->vendor) {
  1171. case CFI_CMDSET_INTEL_STANDARD:
  1172. case CFI_CMDSET_INTEL_EXTENDED:
  1173. default:
  1174. info->cmd_reset = FLASH_CMD_RESET;
  1175. #ifdef CFG_FLASH_PROTECTION
  1176. /* read legacy lock/unlock bit from intel flash */
  1177. if (info->ext_addr) {
  1178. info->legacy_unlock = flash_read_uchar (info,
  1179. info->ext_addr + 5) & 0x08;
  1180. }
  1181. #endif
  1182. break;
  1183. case CFI_CMDSET_AMD_STANDARD:
  1184. case CFI_CMDSET_AMD_EXTENDED:
  1185. info->cmd_reset = AMD_CMD_RESET;
  1186. /* check if flash geometry needs reversal */
  1187. if (num_erase_regions <= 1)
  1188. break;
  1189. /* reverse geometry if top boot part */
  1190. if (info->cfi_version < 0x3131) {
  1191. /* CFI < 1.1, try to guess from device id */
  1192. if ((info->device_id & 0x80) != 0) {
  1193. geometry_reversed = 1;
  1194. }
  1195. break;
  1196. }
  1197. /* CFI >= 1.1, deduct from top/bottom flag */
  1198. /* note: ext_addr is valid since cfi_version > 0 */
  1199. if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
  1200. geometry_reversed = 1;
  1201. }
  1202. break;
  1203. }
  1204. debug ("manufacturer is %d\n", info->vendor);
  1205. debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
  1206. debug ("device id is 0x%x\n", info->device_id);
  1207. debug ("device id2 is 0x%x\n", info->device_id2);
  1208. debug ("cfi version is 0x%04x\n", info->cfi_version);
  1209. size_ratio = info->portwidth / info->chipwidth;
  1210. /* if the chip is x8/x16 reduce the ratio by half */
  1211. if ((info->interface == FLASH_CFI_X8X16)
  1212. && (info->chipwidth == FLASH_CFI_BY8)) {
  1213. size_ratio >>= 1;
  1214. }
  1215. debug ("size_ratio %d port %d bits chip %d bits\n",
  1216. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1217. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1218. debug ("found %d erase regions\n", num_erase_regions);
  1219. sect_cnt = 0;
  1220. sector = base;
  1221. for (i = 0; i < num_erase_regions; i++) {
  1222. if (i > NUM_ERASE_REGIONS) {
  1223. printf ("%d erase regions found, only %d used\n",
  1224. num_erase_regions, NUM_ERASE_REGIONS);
  1225. break;
  1226. }
  1227. if (geometry_reversed)
  1228. tmp = flash_read_long (info, 0,
  1229. FLASH_OFFSET_ERASE_REGIONS +
  1230. (num_erase_regions - 1 - i) * 4);
  1231. else
  1232. tmp = flash_read_long (info, 0,
  1233. FLASH_OFFSET_ERASE_REGIONS +
  1234. i * 4);
  1235. erase_region_size =
  1236. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  1237. tmp >>= 16;
  1238. erase_region_count = (tmp & 0xffff) + 1;
  1239. debug ("erase_region_count = %d erase_region_size = %d\n",
  1240. erase_region_count, erase_region_size);
  1241. for (j = 0; j < erase_region_count; j++) {
  1242. if (sect_cnt >= CFG_MAX_FLASH_SECT) {
  1243. printf("ERROR: too many flash sectors\n");
  1244. break;
  1245. }
  1246. info->start[sect_cnt] = sector;
  1247. sector += (erase_region_size * size_ratio);
  1248. /*
  1249. * Only read protection status from supported devices (intel...)
  1250. */
  1251. switch (info->vendor) {
  1252. case CFI_CMDSET_INTEL_EXTENDED:
  1253. case CFI_CMDSET_INTEL_STANDARD:
  1254. info->protect[sect_cnt] =
  1255. flash_isset (info, sect_cnt,
  1256. FLASH_OFFSET_PROTECT,
  1257. FLASH_STATUS_PROTECT);
  1258. break;
  1259. default:
  1260. info->protect[sect_cnt] = 0; /* default: not protected */
  1261. }
  1262. sect_cnt++;
  1263. }
  1264. }
  1265. info->sector_count = sect_cnt;
  1266. /* multiply the size by the number of chips */
  1267. info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
  1268. info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
  1269. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
  1270. info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
  1271. tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT)) *
  1272. (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT));
  1273. info->buffer_write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
  1274. tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
  1275. (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
  1276. info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
  1277. info->flash_id = FLASH_MAN_CFI;
  1278. if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
  1279. info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
  1280. }
  1281. }
  1282. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1283. return (info->size);
  1284. }
  1285. /* loop through the sectors from the highest address
  1286. * when the passed address is greater or equal to the sector address
  1287. * we have a match
  1288. */
  1289. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  1290. {
  1291. flash_sect_t sector;
  1292. for (sector = info->sector_count - 1; sector >= 0; sector--) {
  1293. if (addr >= info->start[sector])
  1294. break;
  1295. }
  1296. return sector;
  1297. }
  1298. /*-----------------------------------------------------------------------
  1299. */
  1300. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  1301. cfiword_t cword)
  1302. {
  1303. cfiptr_t ctladdr;
  1304. cfiptr_t cptr;
  1305. int flag;
  1306. ctladdr.cp = flash_make_addr (info, 0, 0);
  1307. cptr.cp = (uchar *) dest;
  1308. /* Check if Flash is (sufficiently) erased */
  1309. switch (info->portwidth) {
  1310. case FLASH_CFI_8BIT:
  1311. flag = ((cptr.cp[0] & cword.c) == cword.c);
  1312. break;
  1313. case FLASH_CFI_16BIT:
  1314. flag = ((cptr.wp[0] & cword.w) == cword.w);
  1315. break;
  1316. case FLASH_CFI_32BIT:
  1317. flag = ((cptr.lp[0] & cword.l) == cword.l);
  1318. break;
  1319. case FLASH_CFI_64BIT:
  1320. flag = ((cptr.llp[0] & cword.ll) == cword.ll);
  1321. break;
  1322. default:
  1323. return 2;
  1324. }
  1325. if (!flag)
  1326. return 2;
  1327. /* Disable interrupts which might cause a timeout here */
  1328. flag = disable_interrupts ();
  1329. switch (info->vendor) {
  1330. case CFI_CMDSET_INTEL_EXTENDED:
  1331. case CFI_CMDSET_INTEL_STANDARD:
  1332. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  1333. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  1334. break;
  1335. case CFI_CMDSET_AMD_EXTENDED:
  1336. case CFI_CMDSET_AMD_STANDARD:
  1337. #ifdef CONFIG_FLASH_CFI_LEGACY
  1338. case CFI_CMDSET_AMD_LEGACY:
  1339. #endif
  1340. flash_unlock_seq (info, 0);
  1341. flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
  1342. break;
  1343. }
  1344. switch (info->portwidth) {
  1345. case FLASH_CFI_8BIT:
  1346. cptr.cp[0] = cword.c;
  1347. break;
  1348. case FLASH_CFI_16BIT:
  1349. cptr.wp[0] = cword.w;
  1350. break;
  1351. case FLASH_CFI_32BIT:
  1352. cptr.lp[0] = cword.l;
  1353. break;
  1354. case FLASH_CFI_64BIT:
  1355. cptr.llp[0] = cword.ll;
  1356. break;
  1357. }
  1358. /* re-enable interrupts if necessary */
  1359. if (flag)
  1360. enable_interrupts ();
  1361. return flash_full_status_check (info, find_sector (info, dest),
  1362. info->write_tout, "write");
  1363. }
  1364. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  1365. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  1366. int len)
  1367. {
  1368. flash_sect_t sector;
  1369. int cnt;
  1370. int retcode;
  1371. volatile cfiptr_t src;
  1372. volatile cfiptr_t dst;
  1373. switch (info->vendor) {
  1374. case CFI_CMDSET_INTEL_STANDARD:
  1375. case CFI_CMDSET_INTEL_EXTENDED:
  1376. src.cp = cp;
  1377. dst.cp = (uchar *) dest;
  1378. sector = find_sector (info, dest);
  1379. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1380. flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  1381. if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
  1382. "write to buffer")) == ERR_OK) {
  1383. /* reduce the number of loops by the width of the port */
  1384. switch (info->portwidth) {
  1385. case FLASH_CFI_8BIT:
  1386. cnt = len;
  1387. break;
  1388. case FLASH_CFI_16BIT:
  1389. cnt = len >> 1;
  1390. break;
  1391. case FLASH_CFI_32BIT:
  1392. cnt = len >> 2;
  1393. break;
  1394. case FLASH_CFI_64BIT:
  1395. cnt = len >> 3;
  1396. break;
  1397. default:
  1398. return ERR_INVAL;
  1399. break;
  1400. }
  1401. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1402. while (cnt-- > 0) {
  1403. switch (info->portwidth) {
  1404. case FLASH_CFI_8BIT:
  1405. *dst.cp++ = *src.cp++;
  1406. break;
  1407. case FLASH_CFI_16BIT:
  1408. *dst.wp++ = *src.wp++;
  1409. break;
  1410. case FLASH_CFI_32BIT:
  1411. *dst.lp++ = *src.lp++;
  1412. break;
  1413. case FLASH_CFI_64BIT:
  1414. *dst.llp++ = *src.llp++;
  1415. break;
  1416. default:
  1417. return ERR_INVAL;
  1418. break;
  1419. }
  1420. }
  1421. flash_write_cmd (info, sector, 0,
  1422. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  1423. retcode = flash_full_status_check (info, sector,
  1424. info->buffer_write_tout,
  1425. "buffer write");
  1426. }
  1427. return retcode;
  1428. case CFI_CMDSET_AMD_STANDARD:
  1429. case CFI_CMDSET_AMD_EXTENDED:
  1430. src.cp = cp;
  1431. dst.cp = (uchar *) dest;
  1432. sector = find_sector (info, dest);
  1433. flash_unlock_seq(info,0);
  1434. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
  1435. switch (info->portwidth) {
  1436. case FLASH_CFI_8BIT:
  1437. cnt = len;
  1438. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1439. while (cnt-- > 0) *dst.cp++ = *src.cp++;
  1440. break;
  1441. case FLASH_CFI_16BIT:
  1442. cnt = len >> 1;
  1443. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1444. while (cnt-- > 0) *dst.wp++ = *src.wp++;
  1445. break;
  1446. case FLASH_CFI_32BIT:
  1447. cnt = len >> 2;
  1448. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1449. while (cnt-- > 0) *dst.lp++ = *src.lp++;
  1450. break;
  1451. case FLASH_CFI_64BIT:
  1452. cnt = len >> 3;
  1453. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1454. while (cnt-- > 0) *dst.llp++ = *src.llp++;
  1455. break;
  1456. default:
  1457. return ERR_INVAL;
  1458. }
  1459. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
  1460. retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
  1461. "buffer write");
  1462. return retcode;
  1463. default:
  1464. debug ("Unknown Command Set\n");
  1465. return ERR_INVAL;
  1466. }
  1467. }
  1468. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  1469. #endif /* CFG_FLASH_CFI */