cfi_flash.c 42 KB

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