cfi_flash.c 41 KB

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