cfi_flash.c 38 KB

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