cfi_flash.c 37 KB

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