cfi_flash.c 42 KB

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