cfi_flash.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  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. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. /* The DEBUG define must be before common to enable debugging */
  17. /* #define DEBUG */
  18. #include <common.h>
  19. #include <console.h>
  20. #include <dm.h>
  21. #include <errno.h>
  22. #include <fdt_support.h>
  23. #include <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <environment.h>
  28. #include <mtd/cfi_flash.h>
  29. #include <watchdog.h>
  30. /*
  31. * This file implements a Common Flash Interface (CFI) driver for
  32. * U-Boot.
  33. *
  34. * The width of the port and the width of the chips are determined at
  35. * initialization. These widths are used to calculate the address for
  36. * access CFI data structures.
  37. *
  38. * References
  39. * JEDEC Standard JESD68 - Common Flash Interface (CFI)
  40. * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
  41. * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
  42. * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
  43. * AMD CFI Specification, Release 2.0 December 1, 2001
  44. * AMD/Spansion Application Note: Migration from Single-byte to Three-byte
  45. * Device IDs, Publication Number 25538 Revision A, November 8, 2001
  46. *
  47. * Define CONFIG_SYS_WRITE_SWAPPED_DATA, if you have to swap the Bytes between
  48. * reading and writing ... (yes there is such a Hardware).
  49. */
  50. DECLARE_GLOBAL_DATA_PTR;
  51. static uint flash_offset_cfi[2] = { FLASH_OFFSET_CFI, FLASH_OFFSET_CFI_ALT };
  52. #ifdef CONFIG_FLASH_CFI_MTD
  53. static uint flash_verbose = 1;
  54. #else
  55. #define flash_verbose 1
  56. #endif
  57. flash_info_t flash_info[CFI_MAX_FLASH_BANKS]; /* FLASH chips info */
  58. /*
  59. * Check if chip width is defined. If not, start detecting with 8bit.
  60. */
  61. #ifndef CONFIG_SYS_FLASH_CFI_WIDTH
  62. #define CONFIG_SYS_FLASH_CFI_WIDTH FLASH_CFI_8BIT
  63. #endif
  64. #ifdef CONFIG_CFI_FLASH_USE_WEAK_ACCESSORS
  65. #define __maybe_weak __weak
  66. #else
  67. #define __maybe_weak static
  68. #endif
  69. /*
  70. * 0xffff is an undefined value for the configuration register. When
  71. * this value is returned, the configuration register shall not be
  72. * written at all (default mode).
  73. */
  74. static u16 cfi_flash_config_reg(int i)
  75. {
  76. #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
  77. return ((u16 [])CONFIG_SYS_CFI_FLASH_CONFIG_REGS)[i];
  78. #else
  79. return 0xffff;
  80. #endif
  81. }
  82. #if defined(CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
  83. int cfi_flash_num_flash_banks = CONFIG_SYS_MAX_FLASH_BANKS_DETECT;
  84. #endif
  85. #ifdef CONFIG_CFI_FLASH /* for driver model */
  86. static void cfi_flash_init_dm(void)
  87. {
  88. struct udevice *dev;
  89. cfi_flash_num_flash_banks = 0;
  90. /*
  91. * The uclass_first_device() will probe the first device and
  92. * uclass_next_device() will probe the rest if they exist. So
  93. * that cfi_flash_probe() will get called assigning the base
  94. * addresses that are available.
  95. */
  96. for (uclass_first_device(UCLASS_MTD, &dev);
  97. dev;
  98. uclass_next_device(&dev)) {
  99. }
  100. }
  101. static phys_addr_t cfi_flash_base[CFI_MAX_FLASH_BANKS];
  102. phys_addr_t cfi_flash_bank_addr(int i)
  103. {
  104. return cfi_flash_base[i];
  105. }
  106. #else
  107. __weak phys_addr_t cfi_flash_bank_addr(int i)
  108. {
  109. return ((phys_addr_t [])CONFIG_SYS_FLASH_BANKS_LIST)[i];
  110. }
  111. #endif
  112. __weak unsigned long cfi_flash_bank_size(int i)
  113. {
  114. #ifdef CONFIG_SYS_FLASH_BANKS_SIZES
  115. return ((unsigned long [])CONFIG_SYS_FLASH_BANKS_SIZES)[i];
  116. #else
  117. return 0;
  118. #endif
  119. }
  120. __maybe_weak void flash_write8(u8 value, void *addr)
  121. {
  122. __raw_writeb(value, addr);
  123. }
  124. __maybe_weak void flash_write16(u16 value, void *addr)
  125. {
  126. __raw_writew(value, addr);
  127. }
  128. __maybe_weak void flash_write32(u32 value, void *addr)
  129. {
  130. __raw_writel(value, addr);
  131. }
  132. __maybe_weak void flash_write64(u64 value, void *addr)
  133. {
  134. /* No architectures currently implement __raw_writeq() */
  135. *(volatile u64 *)addr = value;
  136. }
  137. __maybe_weak u8 flash_read8(void *addr)
  138. {
  139. return __raw_readb(addr);
  140. }
  141. __maybe_weak u16 flash_read16(void *addr)
  142. {
  143. return __raw_readw(addr);
  144. }
  145. __maybe_weak u32 flash_read32(void *addr)
  146. {
  147. return __raw_readl(addr);
  148. }
  149. __maybe_weak u64 flash_read64(void *addr)
  150. {
  151. /* No architectures currently implement __raw_readq() */
  152. return *(volatile u64 *)addr;
  153. }
  154. /*-----------------------------------------------------------------------
  155. */
  156. #if defined(CONFIG_ENV_IS_IN_FLASH) || defined(CONFIG_ENV_ADDR_REDUND) || (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
  157. flash_info_t *flash_get_info(ulong base)
  158. {
  159. int i;
  160. flash_info_t *info;
  161. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  162. info = &flash_info[i];
  163. if (info->size && info->start[0] <= base &&
  164. base <= info->start[0] + info->size - 1)
  165. return info;
  166. }
  167. return NULL;
  168. }
  169. #endif
  170. unsigned long flash_sector_size(flash_info_t *info, flash_sect_t sect)
  171. {
  172. if (sect != (info->sector_count - 1))
  173. return info->start[sect + 1] - info->start[sect];
  174. else
  175. return info->start[0] + info->size - info->start[sect];
  176. }
  177. /*-----------------------------------------------------------------------
  178. * create an address based on the offset and the port width
  179. */
  180. static inline void *
  181. flash_map (flash_info_t * info, flash_sect_t sect, uint offset)
  182. {
  183. unsigned int byte_offset = offset * info->portwidth;
  184. return (void *)(info->start[sect] + byte_offset);
  185. }
  186. static inline void flash_unmap(flash_info_t *info, flash_sect_t sect,
  187. unsigned int offset, void *addr)
  188. {
  189. }
  190. /*-----------------------------------------------------------------------
  191. * make a proper sized command based on the port and chip widths
  192. */
  193. static void flash_make_cmd(flash_info_t *info, u32 cmd, void *cmdbuf)
  194. {
  195. int i;
  196. int cword_offset;
  197. int cp_offset;
  198. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  199. u32 cmd_le = cpu_to_le32(cmd);
  200. #endif
  201. uchar val;
  202. uchar *cp = (uchar *) cmdbuf;
  203. for (i = info->portwidth; i > 0; i--){
  204. cword_offset = (info->portwidth-i)%info->chipwidth;
  205. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  206. cp_offset = info->portwidth - i;
  207. val = *((uchar*)&cmd_le + cword_offset);
  208. #else
  209. cp_offset = i - 1;
  210. val = *((uchar*)&cmd + sizeof(u32) - cword_offset - 1);
  211. #endif
  212. cp[cp_offset] = (cword_offset >= sizeof(u32)) ? 0x00 : val;
  213. }
  214. }
  215. #ifdef DEBUG
  216. /*-----------------------------------------------------------------------
  217. * Debug support
  218. */
  219. static void print_longlong (char *str, unsigned long long data)
  220. {
  221. int i;
  222. char *cp;
  223. cp = (char *) &data;
  224. for (i = 0; i < 8; i++)
  225. sprintf (&str[i * 2], "%2.2x", *cp++);
  226. }
  227. static void flash_printqry (struct cfi_qry *qry)
  228. {
  229. u8 *p = (u8 *)qry;
  230. int x, y;
  231. for (x = 0; x < sizeof(struct cfi_qry); x += 16) {
  232. debug("%02x : ", x);
  233. for (y = 0; y < 16; y++)
  234. debug("%2.2x ", p[x + y]);
  235. debug(" ");
  236. for (y = 0; y < 16; y++) {
  237. unsigned char c = p[x + y];
  238. if (c >= 0x20 && c <= 0x7e)
  239. debug("%c", c);
  240. else
  241. debug(".");
  242. }
  243. debug("\n");
  244. }
  245. }
  246. #endif
  247. /*-----------------------------------------------------------------------
  248. * read a character at a port width address
  249. */
  250. static inline uchar flash_read_uchar (flash_info_t * info, uint offset)
  251. {
  252. uchar *cp;
  253. uchar retval;
  254. cp = flash_map (info, 0, offset);
  255. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  256. retval = flash_read8(cp);
  257. #else
  258. retval = flash_read8(cp + info->portwidth - 1);
  259. #endif
  260. flash_unmap (info, 0, offset, cp);
  261. return retval;
  262. }
  263. /*-----------------------------------------------------------------------
  264. * read a word at a port width address, assume 16bit bus
  265. */
  266. static inline ushort flash_read_word (flash_info_t * info, uint offset)
  267. {
  268. ushort *addr, retval;
  269. addr = flash_map (info, 0, offset);
  270. retval = flash_read16 (addr);
  271. flash_unmap (info, 0, offset, addr);
  272. return retval;
  273. }
  274. /*-----------------------------------------------------------------------
  275. * read a long word by picking the least significant byte of each maximum
  276. * port size word. Swap for ppc format.
  277. */
  278. static ulong flash_read_long (flash_info_t * info, flash_sect_t sect,
  279. uint offset)
  280. {
  281. uchar *addr;
  282. ulong retval;
  283. #ifdef DEBUG
  284. int x;
  285. #endif
  286. addr = flash_map (info, sect, offset);
  287. #ifdef DEBUG
  288. debug ("long addr is at %p info->portwidth = %d\n", addr,
  289. info->portwidth);
  290. for (x = 0; x < 4 * info->portwidth; x++) {
  291. debug ("addr[%x] = 0x%x\n", x, flash_read8(addr + x));
  292. }
  293. #endif
  294. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  295. retval = ((flash_read8(addr) << 16) |
  296. (flash_read8(addr + info->portwidth) << 24) |
  297. (flash_read8(addr + 2 * info->portwidth)) |
  298. (flash_read8(addr + 3 * info->portwidth) << 8));
  299. #else
  300. retval = ((flash_read8(addr + 2 * info->portwidth - 1) << 24) |
  301. (flash_read8(addr + info->portwidth - 1) << 16) |
  302. (flash_read8(addr + 4 * info->portwidth - 1) << 8) |
  303. (flash_read8(addr + 3 * info->portwidth - 1)));
  304. #endif
  305. flash_unmap(info, sect, offset, addr);
  306. return retval;
  307. }
  308. /*
  309. * Write a proper sized command to the correct address
  310. */
  311. void flash_write_cmd (flash_info_t * info, flash_sect_t sect,
  312. uint offset, u32 cmd)
  313. {
  314. void *addr;
  315. cfiword_t cword;
  316. addr = flash_map (info, sect, offset);
  317. flash_make_cmd (info, cmd, &cword);
  318. switch (info->portwidth) {
  319. case FLASH_CFI_8BIT:
  320. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr, cmd,
  321. cword.w8, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  322. flash_write8(cword.w8, addr);
  323. break;
  324. case FLASH_CFI_16BIT:
  325. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr,
  326. cmd, cword.w16,
  327. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  328. flash_write16(cword.w16, addr);
  329. break;
  330. case FLASH_CFI_32BIT:
  331. debug ("fwc addr %p cmd %x %8.8x 32bit x %d bit\n", addr,
  332. cmd, cword.w32,
  333. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  334. flash_write32(cword.w32, addr);
  335. break;
  336. case FLASH_CFI_64BIT:
  337. #ifdef DEBUG
  338. {
  339. char str[20];
  340. print_longlong (str, cword.w64);
  341. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  342. addr, cmd, str,
  343. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  344. }
  345. #endif
  346. flash_write64(cword.w64, addr);
  347. break;
  348. }
  349. /* Ensure all the instructions are fully finished */
  350. sync();
  351. flash_unmap(info, sect, offset, addr);
  352. }
  353. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  354. {
  355. flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_UNLOCK_START);
  356. flash_write_cmd (info, sect, info->addr_unlock2, AMD_CMD_UNLOCK_ACK);
  357. }
  358. /*-----------------------------------------------------------------------
  359. */
  360. static int flash_isequal (flash_info_t * info, flash_sect_t sect,
  361. uint offset, uchar cmd)
  362. {
  363. void *addr;
  364. cfiword_t cword;
  365. int retval;
  366. addr = flash_map (info, sect, offset);
  367. flash_make_cmd (info, cmd, &cword);
  368. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, addr);
  369. switch (info->portwidth) {
  370. case FLASH_CFI_8BIT:
  371. debug ("is= %x %x\n", flash_read8(addr), cword.w8);
  372. retval = (flash_read8(addr) == cword.w8);
  373. break;
  374. case FLASH_CFI_16BIT:
  375. debug ("is= %4.4x %4.4x\n", flash_read16(addr), cword.w16);
  376. retval = (flash_read16(addr) == cword.w16);
  377. break;
  378. case FLASH_CFI_32BIT:
  379. debug ("is= %8.8x %8.8x\n", flash_read32(addr), cword.w32);
  380. retval = (flash_read32(addr) == cword.w32);
  381. break;
  382. case FLASH_CFI_64BIT:
  383. #ifdef DEBUG
  384. {
  385. char str1[20];
  386. char str2[20];
  387. print_longlong (str1, flash_read64(addr));
  388. print_longlong (str2, cword.w64);
  389. debug ("is= %s %s\n", str1, str2);
  390. }
  391. #endif
  392. retval = (flash_read64(addr) == cword.w64);
  393. break;
  394. default:
  395. retval = 0;
  396. break;
  397. }
  398. flash_unmap(info, sect, offset, addr);
  399. return retval;
  400. }
  401. /*-----------------------------------------------------------------------
  402. */
  403. static int flash_isset (flash_info_t * info, flash_sect_t sect,
  404. uint offset, uchar cmd)
  405. {
  406. void *addr;
  407. cfiword_t cword;
  408. int retval;
  409. addr = flash_map (info, sect, offset);
  410. flash_make_cmd (info, cmd, &cword);
  411. switch (info->portwidth) {
  412. case FLASH_CFI_8BIT:
  413. retval = ((flash_read8(addr) & cword.w8) == cword.w8);
  414. break;
  415. case FLASH_CFI_16BIT:
  416. retval = ((flash_read16(addr) & cword.w16) == cword.w16);
  417. break;
  418. case FLASH_CFI_32BIT:
  419. retval = ((flash_read32(addr) & cword.w32) == cword.w32);
  420. break;
  421. case FLASH_CFI_64BIT:
  422. retval = ((flash_read64(addr) & cword.w64) == cword.w64);
  423. break;
  424. default:
  425. retval = 0;
  426. break;
  427. }
  428. flash_unmap(info, sect, offset, addr);
  429. return retval;
  430. }
  431. /*-----------------------------------------------------------------------
  432. */
  433. static int flash_toggle (flash_info_t * info, flash_sect_t sect,
  434. uint offset, uchar cmd)
  435. {
  436. void *addr;
  437. cfiword_t cword;
  438. int retval;
  439. addr = flash_map (info, sect, offset);
  440. flash_make_cmd (info, cmd, &cword);
  441. switch (info->portwidth) {
  442. case FLASH_CFI_8BIT:
  443. retval = flash_read8(addr) != flash_read8(addr);
  444. break;
  445. case FLASH_CFI_16BIT:
  446. retval = flash_read16(addr) != flash_read16(addr);
  447. break;
  448. case FLASH_CFI_32BIT:
  449. retval = flash_read32(addr) != flash_read32(addr);
  450. break;
  451. case FLASH_CFI_64BIT:
  452. retval = ( (flash_read32( addr ) != flash_read32( addr )) ||
  453. (flash_read32(addr+4) != flash_read32(addr+4)) );
  454. break;
  455. default:
  456. retval = 0;
  457. break;
  458. }
  459. flash_unmap(info, sect, offset, addr);
  460. return retval;
  461. }
  462. /*
  463. * flash_is_busy - check to see if the flash is busy
  464. *
  465. * This routine checks the status of the chip and returns true if the
  466. * chip is busy.
  467. */
  468. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  469. {
  470. int retval;
  471. switch (info->vendor) {
  472. case CFI_CMDSET_INTEL_PROG_REGIONS:
  473. case CFI_CMDSET_INTEL_STANDARD:
  474. case CFI_CMDSET_INTEL_EXTENDED:
  475. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  476. break;
  477. case CFI_CMDSET_AMD_STANDARD:
  478. case CFI_CMDSET_AMD_EXTENDED:
  479. #ifdef CONFIG_FLASH_CFI_LEGACY
  480. case CFI_CMDSET_AMD_LEGACY:
  481. #endif
  482. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  483. break;
  484. default:
  485. retval = 0;
  486. }
  487. debug ("flash_is_busy: %d\n", retval);
  488. return retval;
  489. }
  490. /*-----------------------------------------------------------------------
  491. * wait for XSR.7 to be set. Time out with an error if it does not.
  492. * This routine does not set the flash to read-array mode.
  493. */
  494. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  495. ulong tout, char *prompt)
  496. {
  497. ulong start;
  498. #if CONFIG_SYS_HZ != 1000
  499. if ((ulong)CONFIG_SYS_HZ > 100000)
  500. tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
  501. else
  502. tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
  503. #endif
  504. /* Wait for command completion */
  505. #ifdef CONFIG_SYS_LOW_RES_TIMER
  506. reset_timer();
  507. #endif
  508. start = get_timer (0);
  509. WATCHDOG_RESET();
  510. while (flash_is_busy (info, sector)) {
  511. if (get_timer (start) > tout) {
  512. printf ("Flash %s timeout at address %lx data %lx\n",
  513. prompt, info->start[sector],
  514. flash_read_long (info, sector, 0));
  515. flash_write_cmd (info, sector, 0, info->cmd_reset);
  516. udelay(1);
  517. return ERR_TIMOUT;
  518. }
  519. udelay (1); /* also triggers watchdog */
  520. }
  521. return ERR_OK;
  522. }
  523. /*-----------------------------------------------------------------------
  524. * Wait for XSR.7 to be set, if it times out print an error, otherwise
  525. * do a full status check.
  526. *
  527. * This routine sets the flash to read-array mode.
  528. */
  529. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  530. ulong tout, char *prompt)
  531. {
  532. int retcode;
  533. retcode = flash_status_check (info, sector, tout, prompt);
  534. switch (info->vendor) {
  535. case CFI_CMDSET_INTEL_PROG_REGIONS:
  536. case CFI_CMDSET_INTEL_EXTENDED:
  537. case CFI_CMDSET_INTEL_STANDARD:
  538. if ((retcode == ERR_OK)
  539. && !flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
  540. retcode = ERR_INVAL;
  541. printf ("Flash %s error at address %lx\n", prompt,
  542. info->start[sector]);
  543. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
  544. FLASH_STATUS_PSLBS)) {
  545. puts ("Command Sequence Error.\n");
  546. } else if (flash_isset (info, sector, 0,
  547. FLASH_STATUS_ECLBS)) {
  548. puts ("Block Erase Error.\n");
  549. retcode = ERR_NOT_ERASED;
  550. } else if (flash_isset (info, sector, 0,
  551. FLASH_STATUS_PSLBS)) {
  552. puts ("Locking Error\n");
  553. }
  554. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  555. puts ("Block locked.\n");
  556. retcode = ERR_PROTECTED;
  557. }
  558. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  559. puts ("Vpp Low Error.\n");
  560. }
  561. flash_write_cmd (info, sector, 0, info->cmd_reset);
  562. udelay(1);
  563. break;
  564. default:
  565. break;
  566. }
  567. return retcode;
  568. }
  569. static int use_flash_status_poll(flash_info_t *info)
  570. {
  571. #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
  572. if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
  573. info->vendor == CFI_CMDSET_AMD_STANDARD)
  574. return 1;
  575. #endif
  576. return 0;
  577. }
  578. static int flash_status_poll(flash_info_t *info, void *src, void *dst,
  579. ulong tout, char *prompt)
  580. {
  581. #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
  582. ulong start;
  583. int ready;
  584. #if CONFIG_SYS_HZ != 1000
  585. if ((ulong)CONFIG_SYS_HZ > 100000)
  586. tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
  587. else
  588. tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
  589. #endif
  590. /* Wait for command completion */
  591. #ifdef CONFIG_SYS_LOW_RES_TIMER
  592. reset_timer();
  593. #endif
  594. start = get_timer(0);
  595. WATCHDOG_RESET();
  596. while (1) {
  597. switch (info->portwidth) {
  598. case FLASH_CFI_8BIT:
  599. ready = flash_read8(dst) == flash_read8(src);
  600. break;
  601. case FLASH_CFI_16BIT:
  602. ready = flash_read16(dst) == flash_read16(src);
  603. break;
  604. case FLASH_CFI_32BIT:
  605. ready = flash_read32(dst) == flash_read32(src);
  606. break;
  607. case FLASH_CFI_64BIT:
  608. ready = flash_read64(dst) == flash_read64(src);
  609. break;
  610. default:
  611. ready = 0;
  612. break;
  613. }
  614. if (ready)
  615. break;
  616. if (get_timer(start) > tout) {
  617. printf("Flash %s timeout at address %lx data %lx\n",
  618. prompt, (ulong)dst, (ulong)flash_read8(dst));
  619. return ERR_TIMOUT;
  620. }
  621. udelay(1); /* also triggers watchdog */
  622. }
  623. #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
  624. return ERR_OK;
  625. }
  626. /*-----------------------------------------------------------------------
  627. */
  628. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  629. {
  630. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  631. unsigned short w;
  632. unsigned int l;
  633. unsigned long long ll;
  634. #endif
  635. switch (info->portwidth) {
  636. case FLASH_CFI_8BIT:
  637. cword->w8 = c;
  638. break;
  639. case FLASH_CFI_16BIT:
  640. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  641. w = c;
  642. w <<= 8;
  643. cword->w16 = (cword->w16 >> 8) | w;
  644. #else
  645. cword->w16 = (cword->w16 << 8) | c;
  646. #endif
  647. break;
  648. case FLASH_CFI_32BIT:
  649. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  650. l = c;
  651. l <<= 24;
  652. cword->w32 = (cword->w32 >> 8) | l;
  653. #else
  654. cword->w32 = (cword->w32 << 8) | c;
  655. #endif
  656. break;
  657. case FLASH_CFI_64BIT:
  658. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  659. ll = c;
  660. ll <<= 56;
  661. cword->w64 = (cword->w64 >> 8) | ll;
  662. #else
  663. cword->w64 = (cword->w64 << 8) | c;
  664. #endif
  665. break;
  666. }
  667. }
  668. /*
  669. * Loop through the sector table starting from the previously found sector.
  670. * Searches forwards or backwards, dependent on the passed address.
  671. */
  672. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  673. {
  674. static flash_sect_t saved_sector; /* previously found sector */
  675. static flash_info_t *saved_info; /* previously used flash bank */
  676. flash_sect_t sector = saved_sector;
  677. if ((info != saved_info) || (sector >= info->sector_count))
  678. sector = 0;
  679. while ((info->start[sector] < addr)
  680. && (sector < info->sector_count - 1))
  681. sector++;
  682. while ((info->start[sector] > addr) && (sector > 0))
  683. /*
  684. * also decrements the sector in case of an overshot
  685. * in the first loop
  686. */
  687. sector--;
  688. saved_sector = sector;
  689. saved_info = info;
  690. return sector;
  691. }
  692. /*-----------------------------------------------------------------------
  693. */
  694. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  695. cfiword_t cword)
  696. {
  697. void *dstaddr = (void *)dest;
  698. int flag;
  699. flash_sect_t sect = 0;
  700. char sect_found = 0;
  701. /* Check if Flash is (sufficiently) erased */
  702. switch (info->portwidth) {
  703. case FLASH_CFI_8BIT:
  704. flag = ((flash_read8(dstaddr) & cword.w8) == cword.w8);
  705. break;
  706. case FLASH_CFI_16BIT:
  707. flag = ((flash_read16(dstaddr) & cword.w16) == cword.w16);
  708. break;
  709. case FLASH_CFI_32BIT:
  710. flag = ((flash_read32(dstaddr) & cword.w32) == cword.w32);
  711. break;
  712. case FLASH_CFI_64BIT:
  713. flag = ((flash_read64(dstaddr) & cword.w64) == cword.w64);
  714. break;
  715. default:
  716. flag = 0;
  717. break;
  718. }
  719. if (!flag)
  720. return ERR_NOT_ERASED;
  721. /* Disable interrupts which might cause a timeout here */
  722. flag = disable_interrupts ();
  723. switch (info->vendor) {
  724. case CFI_CMDSET_INTEL_PROG_REGIONS:
  725. case CFI_CMDSET_INTEL_EXTENDED:
  726. case CFI_CMDSET_INTEL_STANDARD:
  727. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  728. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  729. break;
  730. case CFI_CMDSET_AMD_EXTENDED:
  731. case CFI_CMDSET_AMD_STANDARD:
  732. sect = find_sector(info, dest);
  733. flash_unlock_seq (info, sect);
  734. flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
  735. sect_found = 1;
  736. break;
  737. #ifdef CONFIG_FLASH_CFI_LEGACY
  738. case CFI_CMDSET_AMD_LEGACY:
  739. sect = find_sector(info, dest);
  740. flash_unlock_seq (info, 0);
  741. flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
  742. sect_found = 1;
  743. break;
  744. #endif
  745. }
  746. switch (info->portwidth) {
  747. case FLASH_CFI_8BIT:
  748. flash_write8(cword.w8, dstaddr);
  749. break;
  750. case FLASH_CFI_16BIT:
  751. flash_write16(cword.w16, dstaddr);
  752. break;
  753. case FLASH_CFI_32BIT:
  754. flash_write32(cword.w32, dstaddr);
  755. break;
  756. case FLASH_CFI_64BIT:
  757. flash_write64(cword.w64, dstaddr);
  758. break;
  759. }
  760. /* re-enable interrupts if necessary */
  761. if (flag)
  762. enable_interrupts ();
  763. if (!sect_found)
  764. sect = find_sector (info, dest);
  765. if (use_flash_status_poll(info))
  766. return flash_status_poll(info, &cword, dstaddr,
  767. info->write_tout, "write");
  768. else
  769. return flash_full_status_check(info, sect,
  770. info->write_tout, "write");
  771. }
  772. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  773. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  774. int len)
  775. {
  776. flash_sect_t sector;
  777. int cnt;
  778. int retcode;
  779. void *src = cp;
  780. void *dst = (void *)dest;
  781. void *dst2 = dst;
  782. int flag = 1;
  783. uint offset = 0;
  784. unsigned int shift;
  785. uchar write_cmd;
  786. switch (info->portwidth) {
  787. case FLASH_CFI_8BIT:
  788. shift = 0;
  789. break;
  790. case FLASH_CFI_16BIT:
  791. shift = 1;
  792. break;
  793. case FLASH_CFI_32BIT:
  794. shift = 2;
  795. break;
  796. case FLASH_CFI_64BIT:
  797. shift = 3;
  798. break;
  799. default:
  800. retcode = ERR_INVAL;
  801. goto out_unmap;
  802. }
  803. cnt = len >> shift;
  804. while ((cnt-- > 0) && (flag == 1)) {
  805. switch (info->portwidth) {
  806. case FLASH_CFI_8BIT:
  807. flag = ((flash_read8(dst2) & flash_read8(src)) ==
  808. flash_read8(src));
  809. src += 1, dst2 += 1;
  810. break;
  811. case FLASH_CFI_16BIT:
  812. flag = ((flash_read16(dst2) & flash_read16(src)) ==
  813. flash_read16(src));
  814. src += 2, dst2 += 2;
  815. break;
  816. case FLASH_CFI_32BIT:
  817. flag = ((flash_read32(dst2) & flash_read32(src)) ==
  818. flash_read32(src));
  819. src += 4, dst2 += 4;
  820. break;
  821. case FLASH_CFI_64BIT:
  822. flag = ((flash_read64(dst2) & flash_read64(src)) ==
  823. flash_read64(src));
  824. src += 8, dst2 += 8;
  825. break;
  826. }
  827. }
  828. if (!flag) {
  829. retcode = ERR_NOT_ERASED;
  830. goto out_unmap;
  831. }
  832. src = cp;
  833. sector = find_sector (info, dest);
  834. switch (info->vendor) {
  835. case CFI_CMDSET_INTEL_PROG_REGIONS:
  836. case CFI_CMDSET_INTEL_STANDARD:
  837. case CFI_CMDSET_INTEL_EXTENDED:
  838. write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
  839. FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
  840. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  841. flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
  842. flash_write_cmd (info, sector, 0, write_cmd);
  843. retcode = flash_status_check (info, sector,
  844. info->buffer_write_tout,
  845. "write to buffer");
  846. if (retcode == ERR_OK) {
  847. /* reduce the number of loops by the width of
  848. * the port */
  849. cnt = len >> shift;
  850. flash_write_cmd (info, sector, 0, cnt - 1);
  851. while (cnt-- > 0) {
  852. switch (info->portwidth) {
  853. case FLASH_CFI_8BIT:
  854. flash_write8(flash_read8(src), dst);
  855. src += 1, dst += 1;
  856. break;
  857. case FLASH_CFI_16BIT:
  858. flash_write16(flash_read16(src), dst);
  859. src += 2, dst += 2;
  860. break;
  861. case FLASH_CFI_32BIT:
  862. flash_write32(flash_read32(src), dst);
  863. src += 4, dst += 4;
  864. break;
  865. case FLASH_CFI_64BIT:
  866. flash_write64(flash_read64(src), dst);
  867. src += 8, dst += 8;
  868. break;
  869. default:
  870. retcode = ERR_INVAL;
  871. goto out_unmap;
  872. }
  873. }
  874. flash_write_cmd (info, sector, 0,
  875. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  876. retcode = flash_full_status_check (
  877. info, sector, info->buffer_write_tout,
  878. "buffer write");
  879. }
  880. break;
  881. case CFI_CMDSET_AMD_STANDARD:
  882. case CFI_CMDSET_AMD_EXTENDED:
  883. flash_unlock_seq(info, sector);
  884. #ifdef CONFIG_FLASH_SPANSION_S29WS_N
  885. offset = ((unsigned long)dst - info->start[sector]) >> shift;
  886. #endif
  887. flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
  888. cnt = len >> shift;
  889. flash_write_cmd(info, sector, offset, cnt - 1);
  890. switch (info->portwidth) {
  891. case FLASH_CFI_8BIT:
  892. while (cnt-- > 0) {
  893. flash_write8(flash_read8(src), dst);
  894. src += 1, dst += 1;
  895. }
  896. break;
  897. case FLASH_CFI_16BIT:
  898. while (cnt-- > 0) {
  899. flash_write16(flash_read16(src), dst);
  900. src += 2, dst += 2;
  901. }
  902. break;
  903. case FLASH_CFI_32BIT:
  904. while (cnt-- > 0) {
  905. flash_write32(flash_read32(src), dst);
  906. src += 4, dst += 4;
  907. }
  908. break;
  909. case FLASH_CFI_64BIT:
  910. while (cnt-- > 0) {
  911. flash_write64(flash_read64(src), dst);
  912. src += 8, dst += 8;
  913. }
  914. break;
  915. default:
  916. retcode = ERR_INVAL;
  917. goto out_unmap;
  918. }
  919. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
  920. if (use_flash_status_poll(info))
  921. retcode = flash_status_poll(info, src - (1 << shift),
  922. dst - (1 << shift),
  923. info->buffer_write_tout,
  924. "buffer write");
  925. else
  926. retcode = flash_full_status_check(info, sector,
  927. info->buffer_write_tout,
  928. "buffer write");
  929. break;
  930. default:
  931. debug ("Unknown Command Set\n");
  932. retcode = ERR_INVAL;
  933. break;
  934. }
  935. out_unmap:
  936. return retcode;
  937. }
  938. #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
  939. /*-----------------------------------------------------------------------
  940. */
  941. int flash_erase (flash_info_t * info, int s_first, int s_last)
  942. {
  943. int rcode = 0;
  944. int prot;
  945. flash_sect_t sect;
  946. int st;
  947. if (info->flash_id != FLASH_MAN_CFI) {
  948. puts ("Can't erase unknown flash type - aborted\n");
  949. return 1;
  950. }
  951. if ((s_first < 0) || (s_first > s_last)) {
  952. puts ("- no sectors to erase\n");
  953. return 1;
  954. }
  955. prot = 0;
  956. for (sect = s_first; sect <= s_last; ++sect) {
  957. if (info->protect[sect]) {
  958. prot++;
  959. }
  960. }
  961. if (prot) {
  962. printf ("- Warning: %d protected sectors will not be erased!\n",
  963. prot);
  964. } else if (flash_verbose) {
  965. putc ('\n');
  966. }
  967. for (sect = s_first; sect <= s_last; sect++) {
  968. if (ctrlc()) {
  969. printf("\n");
  970. return 1;
  971. }
  972. if (info->protect[sect] == 0) { /* not protected */
  973. #ifdef CONFIG_SYS_FLASH_CHECK_BLANK_BEFORE_ERASE
  974. int k;
  975. int size;
  976. int erased;
  977. u32 *flash;
  978. /*
  979. * Check if whole sector is erased
  980. */
  981. size = flash_sector_size(info, sect);
  982. erased = 1;
  983. flash = (u32 *)info->start[sect];
  984. /* divide by 4 for longword access */
  985. size = size >> 2;
  986. for (k = 0; k < size; k++) {
  987. if (flash_read32(flash++) != 0xffffffff) {
  988. erased = 0;
  989. break;
  990. }
  991. }
  992. if (erased) {
  993. if (flash_verbose)
  994. putc(',');
  995. continue;
  996. }
  997. #endif
  998. switch (info->vendor) {
  999. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1000. case CFI_CMDSET_INTEL_STANDARD:
  1001. case CFI_CMDSET_INTEL_EXTENDED:
  1002. flash_write_cmd (info, sect, 0,
  1003. FLASH_CMD_CLEAR_STATUS);
  1004. flash_write_cmd (info, sect, 0,
  1005. FLASH_CMD_BLOCK_ERASE);
  1006. flash_write_cmd (info, sect, 0,
  1007. FLASH_CMD_ERASE_CONFIRM);
  1008. break;
  1009. case CFI_CMDSET_AMD_STANDARD:
  1010. case CFI_CMDSET_AMD_EXTENDED:
  1011. flash_unlock_seq (info, sect);
  1012. flash_write_cmd (info, sect,
  1013. info->addr_unlock1,
  1014. AMD_CMD_ERASE_START);
  1015. flash_unlock_seq (info, sect);
  1016. flash_write_cmd (info, sect, 0,
  1017. info->cmd_erase_sector);
  1018. break;
  1019. #ifdef CONFIG_FLASH_CFI_LEGACY
  1020. case CFI_CMDSET_AMD_LEGACY:
  1021. flash_unlock_seq (info, 0);
  1022. flash_write_cmd (info, 0, info->addr_unlock1,
  1023. AMD_CMD_ERASE_START);
  1024. flash_unlock_seq (info, 0);
  1025. flash_write_cmd (info, sect, 0,
  1026. AMD_CMD_ERASE_SECTOR);
  1027. break;
  1028. #endif
  1029. default:
  1030. debug ("Unkown flash vendor %d\n",
  1031. info->vendor);
  1032. break;
  1033. }
  1034. if (use_flash_status_poll(info)) {
  1035. cfiword_t cword;
  1036. void *dest;
  1037. cword.w64 = 0xffffffffffffffffULL;
  1038. dest = flash_map(info, sect, 0);
  1039. st = flash_status_poll(info, &cword, dest,
  1040. info->erase_blk_tout, "erase");
  1041. flash_unmap(info, sect, 0, dest);
  1042. } else
  1043. st = flash_full_status_check(info, sect,
  1044. info->erase_blk_tout,
  1045. "erase");
  1046. if (st)
  1047. rcode = 1;
  1048. else if (flash_verbose)
  1049. putc ('.');
  1050. }
  1051. }
  1052. if (flash_verbose)
  1053. puts (" done\n");
  1054. return rcode;
  1055. }
  1056. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  1057. static int sector_erased(flash_info_t *info, int i)
  1058. {
  1059. int k;
  1060. int size;
  1061. u32 *flash;
  1062. /*
  1063. * Check if whole sector is erased
  1064. */
  1065. size = flash_sector_size(info, i);
  1066. flash = (u32 *)info->start[i];
  1067. /* divide by 4 for longword access */
  1068. size = size >> 2;
  1069. for (k = 0; k < size; k++) {
  1070. if (flash_read32(flash++) != 0xffffffff)
  1071. return 0; /* not erased */
  1072. }
  1073. return 1; /* erased */
  1074. }
  1075. #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
  1076. void flash_print_info (flash_info_t * info)
  1077. {
  1078. int i;
  1079. if (info->flash_id != FLASH_MAN_CFI) {
  1080. puts ("missing or unknown FLASH type\n");
  1081. return;
  1082. }
  1083. printf ("%s flash (%d x %d)",
  1084. info->name,
  1085. (info->portwidth << 3), (info->chipwidth << 3));
  1086. if (info->size < 1024*1024)
  1087. printf (" Size: %ld kB in %d Sectors\n",
  1088. info->size >> 10, info->sector_count);
  1089. else
  1090. printf (" Size: %ld MB in %d Sectors\n",
  1091. info->size >> 20, info->sector_count);
  1092. printf (" ");
  1093. switch (info->vendor) {
  1094. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1095. printf ("Intel Prog Regions");
  1096. break;
  1097. case CFI_CMDSET_INTEL_STANDARD:
  1098. printf ("Intel Standard");
  1099. break;
  1100. case CFI_CMDSET_INTEL_EXTENDED:
  1101. printf ("Intel Extended");
  1102. break;
  1103. case CFI_CMDSET_AMD_STANDARD:
  1104. printf ("AMD Standard");
  1105. break;
  1106. case CFI_CMDSET_AMD_EXTENDED:
  1107. printf ("AMD Extended");
  1108. break;
  1109. #ifdef CONFIG_FLASH_CFI_LEGACY
  1110. case CFI_CMDSET_AMD_LEGACY:
  1111. printf ("AMD Legacy");
  1112. break;
  1113. #endif
  1114. default:
  1115. printf ("Unknown (%d)", info->vendor);
  1116. break;
  1117. }
  1118. printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
  1119. info->manufacturer_id);
  1120. printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
  1121. info->device_id);
  1122. if ((info->device_id & 0xff) == 0x7E) {
  1123. printf(info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
  1124. info->device_id2);
  1125. }
  1126. if ((info->vendor == CFI_CMDSET_AMD_STANDARD) && (info->legacy_unlock))
  1127. printf("\n Advanced Sector Protection (PPB) enabled");
  1128. printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
  1129. info->erase_blk_tout,
  1130. info->write_tout);
  1131. if (info->buffer_size > 1) {
  1132. printf (" Buffer write timeout: %ld ms, "
  1133. "buffer size: %d bytes\n",
  1134. info->buffer_write_tout,
  1135. info->buffer_size);
  1136. }
  1137. puts ("\n Sector Start Addresses:");
  1138. for (i = 0; i < info->sector_count; ++i) {
  1139. if (ctrlc())
  1140. break;
  1141. if ((i % 5) == 0)
  1142. putc('\n');
  1143. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  1144. /* print empty and read-only info */
  1145. printf (" %08lX %c %s ",
  1146. info->start[i],
  1147. sector_erased(info, i) ? 'E' : ' ',
  1148. info->protect[i] ? "RO" : " ");
  1149. #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
  1150. printf (" %08lX %s ",
  1151. info->start[i],
  1152. info->protect[i] ? "RO" : " ");
  1153. #endif
  1154. }
  1155. putc ('\n');
  1156. return;
  1157. }
  1158. /*-----------------------------------------------------------------------
  1159. * This is used in a few places in write_buf() to show programming
  1160. * progress. Making it a function is nasty because it needs to do side
  1161. * effect updates to digit and dots. Repeated code is nasty too, so
  1162. * we define it once here.
  1163. */
  1164. #ifdef CONFIG_FLASH_SHOW_PROGRESS
  1165. #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
  1166. if (flash_verbose) { \
  1167. dots -= dots_sub; \
  1168. if ((scale > 0) && (dots <= 0)) { \
  1169. if ((digit % 5) == 0) \
  1170. printf ("%d", digit / 5); \
  1171. else \
  1172. putc ('.'); \
  1173. digit--; \
  1174. dots += scale; \
  1175. } \
  1176. }
  1177. #else
  1178. #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
  1179. #endif
  1180. /*-----------------------------------------------------------------------
  1181. * Copy memory to flash, returns:
  1182. * 0 - OK
  1183. * 1 - write timeout
  1184. * 2 - Flash not erased
  1185. */
  1186. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  1187. {
  1188. ulong wp;
  1189. uchar *p;
  1190. int aln;
  1191. cfiword_t cword;
  1192. int i, rc;
  1193. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  1194. int buffered_size;
  1195. #endif
  1196. #ifdef CONFIG_FLASH_SHOW_PROGRESS
  1197. int digit = CONFIG_FLASH_SHOW_PROGRESS;
  1198. int scale = 0;
  1199. int dots = 0;
  1200. /*
  1201. * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
  1202. */
  1203. if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
  1204. scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
  1205. CONFIG_FLASH_SHOW_PROGRESS);
  1206. }
  1207. #endif
  1208. /* get lower aligned address */
  1209. wp = (addr & ~(info->portwidth - 1));
  1210. /* handle unaligned start */
  1211. if ((aln = addr - wp) != 0) {
  1212. cword.w32 = 0;
  1213. p = (uchar *)wp;
  1214. for (i = 0; i < aln; ++i)
  1215. flash_add_byte (info, &cword, flash_read8(p + i));
  1216. for (; (i < info->portwidth) && (cnt > 0); i++) {
  1217. flash_add_byte (info, &cword, *src++);
  1218. cnt--;
  1219. }
  1220. for (; (cnt == 0) && (i < info->portwidth); ++i)
  1221. flash_add_byte (info, &cword, flash_read8(p + i));
  1222. rc = flash_write_cfiword (info, wp, cword);
  1223. if (rc != 0)
  1224. return rc;
  1225. wp += i;
  1226. FLASH_SHOW_PROGRESS(scale, dots, digit, i);
  1227. }
  1228. /* handle the aligned part */
  1229. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  1230. buffered_size = (info->portwidth / info->chipwidth);
  1231. buffered_size *= info->buffer_size;
  1232. while (cnt >= info->portwidth) {
  1233. /* prohibit buffer write when buffer_size is 1 */
  1234. if (info->buffer_size == 1) {
  1235. cword.w32 = 0;
  1236. for (i = 0; i < info->portwidth; i++)
  1237. flash_add_byte (info, &cword, *src++);
  1238. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  1239. return rc;
  1240. wp += info->portwidth;
  1241. cnt -= info->portwidth;
  1242. continue;
  1243. }
  1244. /* write buffer until next buffered_size aligned boundary */
  1245. i = buffered_size - (wp % buffered_size);
  1246. if (i > cnt)
  1247. i = cnt;
  1248. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  1249. return rc;
  1250. i -= i & (info->portwidth - 1);
  1251. wp += i;
  1252. src += i;
  1253. cnt -= i;
  1254. FLASH_SHOW_PROGRESS(scale, dots, digit, i);
  1255. /* Only check every once in a while */
  1256. if ((cnt & 0xFFFF) < buffered_size && ctrlc())
  1257. return ERR_ABORTED;
  1258. }
  1259. #else
  1260. while (cnt >= info->portwidth) {
  1261. cword.w32 = 0;
  1262. for (i = 0; i < info->portwidth; i++) {
  1263. flash_add_byte (info, &cword, *src++);
  1264. }
  1265. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  1266. return rc;
  1267. wp += info->portwidth;
  1268. cnt -= info->portwidth;
  1269. FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
  1270. /* Only check every once in a while */
  1271. if ((cnt & 0xFFFF) < info->portwidth && ctrlc())
  1272. return ERR_ABORTED;
  1273. }
  1274. #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
  1275. if (cnt == 0) {
  1276. return (0);
  1277. }
  1278. /*
  1279. * handle unaligned tail bytes
  1280. */
  1281. cword.w32 = 0;
  1282. p = (uchar *)wp;
  1283. for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
  1284. flash_add_byte (info, &cword, *src++);
  1285. --cnt;
  1286. }
  1287. for (; i < info->portwidth; ++i)
  1288. flash_add_byte (info, &cword, flash_read8(p + i));
  1289. return flash_write_cfiword (info, wp, cword);
  1290. }
  1291. static inline int manufact_match(flash_info_t *info, u32 manu)
  1292. {
  1293. return info->manufacturer_id == ((manu & FLASH_VENDMASK) >> 16);
  1294. }
  1295. /*-----------------------------------------------------------------------
  1296. */
  1297. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1298. static int cfi_protect_bugfix(flash_info_t *info, long sector, int prot)
  1299. {
  1300. if (manufact_match(info, INTEL_MANUFACT)
  1301. && info->device_id == NUMONYX_256MBIT) {
  1302. /*
  1303. * see errata called
  1304. * "Numonyx Axcell P33/P30 Specification Update" :)
  1305. */
  1306. flash_write_cmd(info, sector, 0, FLASH_CMD_READ_ID);
  1307. if (!flash_isequal(info, sector, FLASH_OFFSET_PROTECT,
  1308. prot)) {
  1309. /*
  1310. * cmd must come before FLASH_CMD_PROTECT + 20us
  1311. * Disable interrupts which might cause a timeout here.
  1312. */
  1313. int flag = disable_interrupts();
  1314. unsigned short cmd;
  1315. if (prot)
  1316. cmd = FLASH_CMD_PROTECT_SET;
  1317. else
  1318. cmd = FLASH_CMD_PROTECT_CLEAR;
  1319. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
  1320. flash_write_cmd(info, sector, 0, cmd);
  1321. /* re-enable interrupts if necessary */
  1322. if (flag)
  1323. enable_interrupts();
  1324. }
  1325. return 1;
  1326. }
  1327. return 0;
  1328. }
  1329. int flash_real_protect (flash_info_t * info, long sector, int prot)
  1330. {
  1331. int retcode = 0;
  1332. switch (info->vendor) {
  1333. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1334. case CFI_CMDSET_INTEL_STANDARD:
  1335. case CFI_CMDSET_INTEL_EXTENDED:
  1336. if (!cfi_protect_bugfix(info, sector, prot)) {
  1337. flash_write_cmd(info, sector, 0,
  1338. FLASH_CMD_CLEAR_STATUS);
  1339. flash_write_cmd(info, sector, 0,
  1340. FLASH_CMD_PROTECT);
  1341. if (prot)
  1342. flash_write_cmd(info, sector, 0,
  1343. FLASH_CMD_PROTECT_SET);
  1344. else
  1345. flash_write_cmd(info, sector, 0,
  1346. FLASH_CMD_PROTECT_CLEAR);
  1347. }
  1348. break;
  1349. case CFI_CMDSET_AMD_EXTENDED:
  1350. case CFI_CMDSET_AMD_STANDARD:
  1351. /* U-Boot only checks the first byte */
  1352. if (manufact_match(info, ATM_MANUFACT)) {
  1353. if (prot) {
  1354. flash_unlock_seq (info, 0);
  1355. flash_write_cmd (info, 0,
  1356. info->addr_unlock1,
  1357. ATM_CMD_SOFTLOCK_START);
  1358. flash_unlock_seq (info, 0);
  1359. flash_write_cmd (info, sector, 0,
  1360. ATM_CMD_LOCK_SECT);
  1361. } else {
  1362. flash_write_cmd (info, 0,
  1363. info->addr_unlock1,
  1364. AMD_CMD_UNLOCK_START);
  1365. if (info->device_id == ATM_ID_BV6416)
  1366. flash_write_cmd (info, sector,
  1367. 0, ATM_CMD_UNLOCK_SECT);
  1368. }
  1369. }
  1370. if (info->legacy_unlock) {
  1371. int flag = disable_interrupts();
  1372. int lock_flag;
  1373. flash_unlock_seq(info, 0);
  1374. flash_write_cmd(info, 0, info->addr_unlock1,
  1375. AMD_CMD_SET_PPB_ENTRY);
  1376. lock_flag = flash_isset(info, sector, 0, 0x01);
  1377. if (prot) {
  1378. if (lock_flag) {
  1379. flash_write_cmd(info, sector, 0,
  1380. AMD_CMD_PPB_LOCK_BC1);
  1381. flash_write_cmd(info, sector, 0,
  1382. AMD_CMD_PPB_LOCK_BC2);
  1383. }
  1384. debug("sector %ld %slocked\n", sector,
  1385. lock_flag ? "" : "already ");
  1386. } else {
  1387. if (!lock_flag) {
  1388. debug("unlock %ld\n", sector);
  1389. flash_write_cmd(info, 0, 0,
  1390. AMD_CMD_PPB_UNLOCK_BC1);
  1391. flash_write_cmd(info, 0, 0,
  1392. AMD_CMD_PPB_UNLOCK_BC2);
  1393. }
  1394. debug("sector %ld %sunlocked\n", sector,
  1395. !lock_flag ? "" : "already ");
  1396. }
  1397. if (flag)
  1398. enable_interrupts();
  1399. if (flash_status_check(info, sector,
  1400. info->erase_blk_tout,
  1401. prot ? "protect" : "unprotect"))
  1402. printf("status check error\n");
  1403. flash_write_cmd(info, 0, 0,
  1404. AMD_CMD_SET_PPB_EXIT_BC1);
  1405. flash_write_cmd(info, 0, 0,
  1406. AMD_CMD_SET_PPB_EXIT_BC2);
  1407. }
  1408. break;
  1409. #ifdef CONFIG_FLASH_CFI_LEGACY
  1410. case CFI_CMDSET_AMD_LEGACY:
  1411. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1412. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  1413. if (prot)
  1414. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  1415. else
  1416. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  1417. #endif
  1418. };
  1419. /*
  1420. * Flash needs to be in status register read mode for
  1421. * flash_full_status_check() to work correctly
  1422. */
  1423. flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
  1424. if ((retcode =
  1425. flash_full_status_check (info, sector, info->erase_blk_tout,
  1426. prot ? "protect" : "unprotect")) == 0) {
  1427. info->protect[sector] = prot;
  1428. /*
  1429. * On some of Intel's flash chips (marked via legacy_unlock)
  1430. * unprotect unprotects all locking.
  1431. */
  1432. if ((prot == 0) && (info->legacy_unlock)) {
  1433. flash_sect_t i;
  1434. for (i = 0; i < info->sector_count; i++) {
  1435. if (info->protect[i])
  1436. flash_real_protect (info, i, 1);
  1437. }
  1438. }
  1439. }
  1440. return retcode;
  1441. }
  1442. /*-----------------------------------------------------------------------
  1443. * flash_read_user_serial - read the OneTimeProgramming cells
  1444. */
  1445. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  1446. int len)
  1447. {
  1448. uchar *src;
  1449. uchar *dst;
  1450. dst = buffer;
  1451. src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
  1452. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  1453. memcpy (dst, src + offset, len);
  1454. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1455. udelay(1);
  1456. flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
  1457. }
  1458. /*
  1459. * flash_read_factory_serial - read the device Id from the protection area
  1460. */
  1461. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  1462. int len)
  1463. {
  1464. uchar *src;
  1465. src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  1466. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  1467. memcpy (buffer, src + offset, len);
  1468. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1469. udelay(1);
  1470. flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
  1471. }
  1472. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  1473. /*-----------------------------------------------------------------------
  1474. * Reverse the order of the erase regions in the CFI QRY structure.
  1475. * This is needed for chips that are either a) correctly detected as
  1476. * top-boot, or b) buggy.
  1477. */
  1478. static void cfi_reverse_geometry(struct cfi_qry *qry)
  1479. {
  1480. unsigned int i, j;
  1481. u32 tmp;
  1482. for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
  1483. tmp = get_unaligned(&(qry->erase_region_info[i]));
  1484. put_unaligned(get_unaligned(&(qry->erase_region_info[j])),
  1485. &(qry->erase_region_info[i]));
  1486. put_unaligned(tmp, &(qry->erase_region_info[j]));
  1487. }
  1488. }
  1489. /*-----------------------------------------------------------------------
  1490. * read jedec ids from device and set corresponding fields in info struct
  1491. *
  1492. * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
  1493. *
  1494. */
  1495. static void cmdset_intel_read_jedec_ids(flash_info_t *info)
  1496. {
  1497. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1498. udelay(1);
  1499. flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
  1500. udelay(1000); /* some flash are slow to respond */
  1501. info->manufacturer_id = flash_read_uchar (info,
  1502. FLASH_OFFSET_MANUFACTURER_ID);
  1503. info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
  1504. flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
  1505. flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
  1506. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1507. }
  1508. static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
  1509. {
  1510. info->cmd_reset = FLASH_CMD_RESET;
  1511. cmdset_intel_read_jedec_ids(info);
  1512. flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
  1513. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1514. /* read legacy lock/unlock bit from intel flash */
  1515. if (info->ext_addr) {
  1516. info->legacy_unlock = flash_read_uchar (info,
  1517. info->ext_addr + 5) & 0x08;
  1518. }
  1519. #endif
  1520. return 0;
  1521. }
  1522. static void cmdset_amd_read_jedec_ids(flash_info_t *info)
  1523. {
  1524. ushort bankId = 0;
  1525. uchar manuId;
  1526. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1527. flash_unlock_seq(info, 0);
  1528. flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
  1529. udelay(1000); /* some flash are slow to respond */
  1530. manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
  1531. /* JEDEC JEP106Z specifies ID codes up to bank 7 */
  1532. while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
  1533. bankId += 0x100;
  1534. manuId = flash_read_uchar (info,
  1535. bankId | FLASH_OFFSET_MANUFACTURER_ID);
  1536. }
  1537. info->manufacturer_id = manuId;
  1538. switch (info->chipwidth){
  1539. case FLASH_CFI_8BIT:
  1540. info->device_id = flash_read_uchar (info,
  1541. FLASH_OFFSET_DEVICE_ID);
  1542. if (info->device_id == 0x7E) {
  1543. /* AMD 3-byte (expanded) device ids */
  1544. info->device_id2 = flash_read_uchar (info,
  1545. FLASH_OFFSET_DEVICE_ID2);
  1546. info->device_id2 <<= 8;
  1547. info->device_id2 |= flash_read_uchar (info,
  1548. FLASH_OFFSET_DEVICE_ID3);
  1549. }
  1550. break;
  1551. case FLASH_CFI_16BIT:
  1552. info->device_id = flash_read_word (info,
  1553. FLASH_OFFSET_DEVICE_ID);
  1554. if ((info->device_id & 0xff) == 0x7E) {
  1555. /* AMD 3-byte (expanded) device ids */
  1556. info->device_id2 = flash_read_uchar (info,
  1557. FLASH_OFFSET_DEVICE_ID2);
  1558. info->device_id2 <<= 8;
  1559. info->device_id2 |= flash_read_uchar (info,
  1560. FLASH_OFFSET_DEVICE_ID3);
  1561. }
  1562. break;
  1563. default:
  1564. break;
  1565. }
  1566. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1567. udelay(1);
  1568. }
  1569. static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
  1570. {
  1571. info->cmd_reset = AMD_CMD_RESET;
  1572. info->cmd_erase_sector = AMD_CMD_ERASE_SECTOR;
  1573. cmdset_amd_read_jedec_ids(info);
  1574. flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
  1575. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1576. if (info->ext_addr) {
  1577. /* read sector protect/unprotect scheme (at 0x49) */
  1578. if (flash_read_uchar(info, info->ext_addr + 9) == 0x8)
  1579. info->legacy_unlock = 1;
  1580. }
  1581. #endif
  1582. return 0;
  1583. }
  1584. #ifdef CONFIG_FLASH_CFI_LEGACY
  1585. static void flash_read_jedec_ids (flash_info_t * info)
  1586. {
  1587. info->manufacturer_id = 0;
  1588. info->device_id = 0;
  1589. info->device_id2 = 0;
  1590. switch (info->vendor) {
  1591. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1592. case CFI_CMDSET_INTEL_STANDARD:
  1593. case CFI_CMDSET_INTEL_EXTENDED:
  1594. cmdset_intel_read_jedec_ids(info);
  1595. break;
  1596. case CFI_CMDSET_AMD_STANDARD:
  1597. case CFI_CMDSET_AMD_EXTENDED:
  1598. cmdset_amd_read_jedec_ids(info);
  1599. break;
  1600. default:
  1601. break;
  1602. }
  1603. }
  1604. /*-----------------------------------------------------------------------
  1605. * Call board code to request info about non-CFI flash.
  1606. * board_flash_get_legacy needs to fill in at least:
  1607. * info->portwidth, info->chipwidth and info->interface for Jedec probing.
  1608. */
  1609. static int flash_detect_legacy(phys_addr_t base, int banknum)
  1610. {
  1611. flash_info_t *info = &flash_info[banknum];
  1612. if (board_flash_get_legacy(base, banknum, info)) {
  1613. /* board code may have filled info completely. If not, we
  1614. use JEDEC ID probing. */
  1615. if (!info->vendor) {
  1616. int modes[] = {
  1617. CFI_CMDSET_AMD_STANDARD,
  1618. CFI_CMDSET_INTEL_STANDARD
  1619. };
  1620. int i;
  1621. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  1622. info->vendor = modes[i];
  1623. info->start[0] =
  1624. (ulong)map_physmem(base,
  1625. info->portwidth,
  1626. MAP_NOCACHE);
  1627. if (info->portwidth == FLASH_CFI_8BIT
  1628. && info->interface == FLASH_CFI_X8X16) {
  1629. info->addr_unlock1 = 0x2AAA;
  1630. info->addr_unlock2 = 0x5555;
  1631. } else {
  1632. info->addr_unlock1 = 0x5555;
  1633. info->addr_unlock2 = 0x2AAA;
  1634. }
  1635. flash_read_jedec_ids(info);
  1636. debug("JEDEC PROBE: ID %x %x %x\n",
  1637. info->manufacturer_id,
  1638. info->device_id,
  1639. info->device_id2);
  1640. if (jedec_flash_match(info, info->start[0]))
  1641. break;
  1642. else
  1643. unmap_physmem((void *)info->start[0],
  1644. info->portwidth);
  1645. }
  1646. }
  1647. switch(info->vendor) {
  1648. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1649. case CFI_CMDSET_INTEL_STANDARD:
  1650. case CFI_CMDSET_INTEL_EXTENDED:
  1651. info->cmd_reset = FLASH_CMD_RESET;
  1652. break;
  1653. case CFI_CMDSET_AMD_STANDARD:
  1654. case CFI_CMDSET_AMD_EXTENDED:
  1655. case CFI_CMDSET_AMD_LEGACY:
  1656. info->cmd_reset = AMD_CMD_RESET;
  1657. break;
  1658. }
  1659. info->flash_id = FLASH_MAN_CFI;
  1660. return 1;
  1661. }
  1662. return 0; /* use CFI */
  1663. }
  1664. #else
  1665. static inline int flash_detect_legacy(phys_addr_t base, int banknum)
  1666. {
  1667. return 0; /* use CFI */
  1668. }
  1669. #endif
  1670. /*-----------------------------------------------------------------------
  1671. * detect if flash is compatible with the Common Flash Interface (CFI)
  1672. * http://www.jedec.org/download/search/jesd68.pdf
  1673. */
  1674. static void flash_read_cfi (flash_info_t *info, void *buf,
  1675. unsigned int start, size_t len)
  1676. {
  1677. u8 *p = buf;
  1678. unsigned int i;
  1679. for (i = 0; i < len; i++)
  1680. p[i] = flash_read_uchar(info, start + i);
  1681. }
  1682. static void __flash_cmd_reset(flash_info_t *info)
  1683. {
  1684. /*
  1685. * We do not yet know what kind of commandset to use, so we issue
  1686. * the reset command in both Intel and AMD variants, in the hope
  1687. * that AMD flash roms ignore the Intel command.
  1688. */
  1689. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1690. udelay(1);
  1691. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1692. }
  1693. void flash_cmd_reset(flash_info_t *info)
  1694. __attribute__((weak,alias("__flash_cmd_reset")));
  1695. static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
  1696. {
  1697. int cfi_offset;
  1698. /* Issue FLASH reset command */
  1699. flash_cmd_reset(info);
  1700. for (cfi_offset = 0; cfi_offset < ARRAY_SIZE(flash_offset_cfi);
  1701. cfi_offset++) {
  1702. flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
  1703. FLASH_CMD_CFI);
  1704. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  1705. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  1706. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  1707. flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
  1708. sizeof(struct cfi_qry));
  1709. info->interface = le16_to_cpu(qry->interface_desc);
  1710. info->cfi_offset = flash_offset_cfi[cfi_offset];
  1711. debug ("device interface is %d\n",
  1712. info->interface);
  1713. debug ("found port %d chip %d ",
  1714. info->portwidth, info->chipwidth);
  1715. debug ("port %d bits chip %d bits\n",
  1716. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1717. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1718. /* calculate command offsets as in the Linux driver */
  1719. info->addr_unlock1 = 0x555;
  1720. info->addr_unlock2 = 0x2aa;
  1721. /*
  1722. * modify the unlock address if we are
  1723. * in compatibility mode
  1724. */
  1725. if ( /* x8/x16 in x8 mode */
  1726. ((info->chipwidth == FLASH_CFI_BY8) &&
  1727. (info->interface == FLASH_CFI_X8X16)) ||
  1728. /* x16/x32 in x16 mode */
  1729. ((info->chipwidth == FLASH_CFI_BY16) &&
  1730. (info->interface == FLASH_CFI_X16X32)))
  1731. {
  1732. info->addr_unlock1 = 0xaaa;
  1733. info->addr_unlock2 = 0x555;
  1734. }
  1735. info->name = "CFI conformant";
  1736. return 1;
  1737. }
  1738. }
  1739. return 0;
  1740. }
  1741. static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
  1742. {
  1743. debug ("flash detect cfi\n");
  1744. for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
  1745. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  1746. for (info->chipwidth = FLASH_CFI_BY8;
  1747. info->chipwidth <= info->portwidth;
  1748. info->chipwidth <<= 1)
  1749. if (__flash_detect_cfi(info, qry))
  1750. return 1;
  1751. }
  1752. debug ("not found\n");
  1753. return 0;
  1754. }
  1755. /*
  1756. * Manufacturer-specific quirks. Add workarounds for geometry
  1757. * reversal, etc. here.
  1758. */
  1759. static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
  1760. {
  1761. /* check if flash geometry needs reversal */
  1762. if (qry->num_erase_regions > 1) {
  1763. /* reverse geometry if top boot part */
  1764. if (info->cfi_version < 0x3131) {
  1765. /* CFI < 1.1, try to guess from device id */
  1766. if ((info->device_id & 0x80) != 0)
  1767. cfi_reverse_geometry(qry);
  1768. } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
  1769. /* CFI >= 1.1, deduct from top/bottom flag */
  1770. /* note: ext_addr is valid since cfi_version > 0 */
  1771. cfi_reverse_geometry(qry);
  1772. }
  1773. }
  1774. }
  1775. static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
  1776. {
  1777. int reverse_geometry = 0;
  1778. /* Check the "top boot" bit in the PRI */
  1779. if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
  1780. reverse_geometry = 1;
  1781. /* AT49BV6416(T) list the erase regions in the wrong order.
  1782. * However, the device ID is identical with the non-broken
  1783. * AT49BV642D they differ in the high byte.
  1784. */
  1785. if (info->device_id == 0xd6 || info->device_id == 0xd2)
  1786. reverse_geometry = !reverse_geometry;
  1787. if (reverse_geometry)
  1788. cfi_reverse_geometry(qry);
  1789. }
  1790. static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
  1791. {
  1792. /* check if flash geometry needs reversal */
  1793. if (qry->num_erase_regions > 1) {
  1794. /* reverse geometry if top boot part */
  1795. if (info->cfi_version < 0x3131) {
  1796. /* CFI < 1.1, guess by device id */
  1797. if (info->device_id == 0x22CA || /* M29W320DT */
  1798. info->device_id == 0x2256 || /* M29W320ET */
  1799. info->device_id == 0x22D7) { /* M29W800DT */
  1800. cfi_reverse_geometry(qry);
  1801. }
  1802. } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
  1803. /* CFI >= 1.1, deduct from top/bottom flag */
  1804. /* note: ext_addr is valid since cfi_version > 0 */
  1805. cfi_reverse_geometry(qry);
  1806. }
  1807. }
  1808. }
  1809. static void flash_fixup_sst(flash_info_t *info, struct cfi_qry *qry)
  1810. {
  1811. /*
  1812. * SST, for many recent nor parallel flashes, says they are
  1813. * CFI-conformant. This is not true, since qry struct.
  1814. * reports a std. AMD command set (0x0002), while SST allows to
  1815. * erase two different sector sizes for the same memory.
  1816. * 64KB sector (SST call it block) needs 0x30 to be erased.
  1817. * 4KB sector (SST call it sector) needs 0x50 to be erased.
  1818. * Since CFI query detect the 4KB number of sectors, users expects
  1819. * a sector granularity of 4KB, and it is here set.
  1820. */
  1821. if (info->device_id == 0x5D23 || /* SST39VF3201B */
  1822. info->device_id == 0x5C23) { /* SST39VF3202B */
  1823. /* set sector granularity to 4KB */
  1824. info->cmd_erase_sector=0x50;
  1825. }
  1826. }
  1827. static void flash_fixup_num(flash_info_t *info, struct cfi_qry *qry)
  1828. {
  1829. /*
  1830. * The M29EW devices seem to report the CFI information wrong
  1831. * when it's in 8 bit mode.
  1832. * There's an app note from Numonyx on this issue.
  1833. * So adjust the buffer size for M29EW while operating in 8-bit mode
  1834. */
  1835. if (((qry->max_buf_write_size) > 0x8) &&
  1836. (info->device_id == 0x7E) &&
  1837. (info->device_id2 == 0x2201 ||
  1838. info->device_id2 == 0x2301 ||
  1839. info->device_id2 == 0x2801 ||
  1840. info->device_id2 == 0x4801)) {
  1841. debug("Adjusted buffer size on Numonyx flash"
  1842. " M29EW family in 8 bit mode\n");
  1843. qry->max_buf_write_size = 0x8;
  1844. }
  1845. }
  1846. /*
  1847. * The following code cannot be run from FLASH!
  1848. *
  1849. */
  1850. ulong flash_get_size (phys_addr_t base, int banknum)
  1851. {
  1852. flash_info_t *info = &flash_info[banknum];
  1853. int i, j;
  1854. flash_sect_t sect_cnt;
  1855. phys_addr_t sector;
  1856. unsigned long tmp;
  1857. int size_ratio;
  1858. uchar num_erase_regions;
  1859. int erase_region_size;
  1860. int erase_region_count;
  1861. struct cfi_qry qry;
  1862. unsigned long max_size;
  1863. memset(&qry, 0, sizeof(qry));
  1864. info->ext_addr = 0;
  1865. info->cfi_version = 0;
  1866. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1867. info->legacy_unlock = 0;
  1868. #endif
  1869. info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
  1870. if (flash_detect_cfi (info, &qry)) {
  1871. info->vendor = le16_to_cpu(get_unaligned(&(qry.p_id)));
  1872. info->ext_addr = le16_to_cpu(get_unaligned(&(qry.p_adr)));
  1873. num_erase_regions = qry.num_erase_regions;
  1874. if (info->ext_addr) {
  1875. info->cfi_version = (ushort) flash_read_uchar (info,
  1876. info->ext_addr + 3) << 8;
  1877. info->cfi_version |= (ushort) flash_read_uchar (info,
  1878. info->ext_addr + 4);
  1879. }
  1880. #ifdef DEBUG
  1881. flash_printqry (&qry);
  1882. #endif
  1883. switch (info->vendor) {
  1884. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1885. case CFI_CMDSET_INTEL_STANDARD:
  1886. case CFI_CMDSET_INTEL_EXTENDED:
  1887. cmdset_intel_init(info, &qry);
  1888. break;
  1889. case CFI_CMDSET_AMD_STANDARD:
  1890. case CFI_CMDSET_AMD_EXTENDED:
  1891. cmdset_amd_init(info, &qry);
  1892. break;
  1893. default:
  1894. printf("CFI: Unknown command set 0x%x\n",
  1895. info->vendor);
  1896. /*
  1897. * Unfortunately, this means we don't know how
  1898. * to get the chip back to Read mode. Might
  1899. * as well try an Intel-style reset...
  1900. */
  1901. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1902. return 0;
  1903. }
  1904. /* Do manufacturer-specific fixups */
  1905. switch (info->manufacturer_id) {
  1906. case 0x0001: /* AMD */
  1907. case 0x0037: /* AMIC */
  1908. flash_fixup_amd(info, &qry);
  1909. break;
  1910. case 0x001f:
  1911. flash_fixup_atmel(info, &qry);
  1912. break;
  1913. case 0x0020:
  1914. flash_fixup_stm(info, &qry);
  1915. break;
  1916. case 0x00bf: /* SST */
  1917. flash_fixup_sst(info, &qry);
  1918. break;
  1919. case 0x0089: /* Numonyx */
  1920. flash_fixup_num(info, &qry);
  1921. break;
  1922. }
  1923. debug ("manufacturer is %d\n", info->vendor);
  1924. debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
  1925. debug ("device id is 0x%x\n", info->device_id);
  1926. debug ("device id2 is 0x%x\n", info->device_id2);
  1927. debug ("cfi version is 0x%04x\n", info->cfi_version);
  1928. size_ratio = info->portwidth / info->chipwidth;
  1929. /* if the chip is x8/x16 reduce the ratio by half */
  1930. if ((info->interface == FLASH_CFI_X8X16)
  1931. && (info->chipwidth == FLASH_CFI_BY8)) {
  1932. size_ratio >>= 1;
  1933. }
  1934. debug ("size_ratio %d port %d bits chip %d bits\n",
  1935. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1936. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1937. info->size = 1 << qry.dev_size;
  1938. /* multiply the size by the number of chips */
  1939. info->size *= size_ratio;
  1940. max_size = cfi_flash_bank_size(banknum);
  1941. if (max_size && (info->size > max_size)) {
  1942. debug("[truncated from %ldMiB]", info->size >> 20);
  1943. info->size = max_size;
  1944. }
  1945. debug ("found %d erase regions\n", num_erase_regions);
  1946. sect_cnt = 0;
  1947. sector = base;
  1948. for (i = 0; i < num_erase_regions; i++) {
  1949. if (i > NUM_ERASE_REGIONS) {
  1950. printf ("%d erase regions found, only %d used\n",
  1951. num_erase_regions, NUM_ERASE_REGIONS);
  1952. break;
  1953. }
  1954. tmp = le32_to_cpu(get_unaligned(
  1955. &(qry.erase_region_info[i])));
  1956. debug("erase region %u: 0x%08lx\n", i, tmp);
  1957. erase_region_count = (tmp & 0xffff) + 1;
  1958. tmp >>= 16;
  1959. erase_region_size =
  1960. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  1961. debug ("erase_region_count = %d erase_region_size = %d\n",
  1962. erase_region_count, erase_region_size);
  1963. for (j = 0; j < erase_region_count; j++) {
  1964. if (sector - base >= info->size)
  1965. break;
  1966. if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
  1967. printf("ERROR: too many flash sectors\n");
  1968. break;
  1969. }
  1970. info->start[sect_cnt] =
  1971. (ulong)map_physmem(sector,
  1972. info->portwidth,
  1973. MAP_NOCACHE);
  1974. sector += (erase_region_size * size_ratio);
  1975. /*
  1976. * Only read protection status from
  1977. * supported devices (intel...)
  1978. */
  1979. switch (info->vendor) {
  1980. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1981. case CFI_CMDSET_INTEL_EXTENDED:
  1982. case CFI_CMDSET_INTEL_STANDARD:
  1983. /*
  1984. * Set flash to read-id mode. Otherwise
  1985. * reading protected status is not
  1986. * guaranteed.
  1987. */
  1988. flash_write_cmd(info, sect_cnt, 0,
  1989. FLASH_CMD_READ_ID);
  1990. info->protect[sect_cnt] =
  1991. flash_isset (info, sect_cnt,
  1992. FLASH_OFFSET_PROTECT,
  1993. FLASH_STATUS_PROTECT);
  1994. flash_write_cmd(info, sect_cnt, 0,
  1995. FLASH_CMD_RESET);
  1996. break;
  1997. case CFI_CMDSET_AMD_EXTENDED:
  1998. case CFI_CMDSET_AMD_STANDARD:
  1999. if (!info->legacy_unlock) {
  2000. /* default: not protected */
  2001. info->protect[sect_cnt] = 0;
  2002. break;
  2003. }
  2004. /* Read protection (PPB) from sector */
  2005. flash_write_cmd(info, 0, 0,
  2006. info->cmd_reset);
  2007. flash_unlock_seq(info, 0);
  2008. flash_write_cmd(info, 0,
  2009. info->addr_unlock1,
  2010. FLASH_CMD_READ_ID);
  2011. info->protect[sect_cnt] =
  2012. flash_isset(
  2013. info, sect_cnt,
  2014. FLASH_OFFSET_PROTECT,
  2015. FLASH_STATUS_PROTECT);
  2016. break;
  2017. default:
  2018. /* default: not protected */
  2019. info->protect[sect_cnt] = 0;
  2020. }
  2021. sect_cnt++;
  2022. }
  2023. }
  2024. info->sector_count = sect_cnt;
  2025. info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
  2026. tmp = 1 << qry.block_erase_timeout_typ;
  2027. info->erase_blk_tout = tmp *
  2028. (1 << qry.block_erase_timeout_max);
  2029. tmp = (1 << qry.buf_write_timeout_typ) *
  2030. (1 << qry.buf_write_timeout_max);
  2031. /* round up when converting to ms */
  2032. info->buffer_write_tout = (tmp + 999) / 1000;
  2033. tmp = (1 << qry.word_write_timeout_typ) *
  2034. (1 << qry.word_write_timeout_max);
  2035. /* round up when converting to ms */
  2036. info->write_tout = (tmp + 999) / 1000;
  2037. info->flash_id = FLASH_MAN_CFI;
  2038. if ((info->interface == FLASH_CFI_X8X16) &&
  2039. (info->chipwidth == FLASH_CFI_BY8)) {
  2040. /* XXX - Need to test on x8/x16 in parallel. */
  2041. info->portwidth >>= 1;
  2042. }
  2043. flash_write_cmd (info, 0, 0, info->cmd_reset);
  2044. }
  2045. return (info->size);
  2046. }
  2047. #ifdef CONFIG_FLASH_CFI_MTD
  2048. void flash_set_verbose(uint v)
  2049. {
  2050. flash_verbose = v;
  2051. }
  2052. #endif
  2053. static void cfi_flash_set_config_reg(u32 base, u16 val)
  2054. {
  2055. #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
  2056. /*
  2057. * Only set this config register if really defined
  2058. * to a valid value (0xffff is invalid)
  2059. */
  2060. if (val == 0xffff)
  2061. return;
  2062. /*
  2063. * Set configuration register. Data is "encrypted" in the 16 lower
  2064. * address bits.
  2065. */
  2066. flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
  2067. flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
  2068. /*
  2069. * Finally issue reset-command to bring device back to
  2070. * read-array mode
  2071. */
  2072. flash_write16(FLASH_CMD_RESET, (void *)base);
  2073. #endif
  2074. }
  2075. /*-----------------------------------------------------------------------
  2076. */
  2077. void flash_protect_default(void)
  2078. {
  2079. #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
  2080. int i;
  2081. struct apl_s {
  2082. ulong start;
  2083. ulong size;
  2084. } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
  2085. #endif
  2086. /* Monitor protection ON by default */
  2087. #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
  2088. (!defined(CONFIG_MONITOR_IS_IN_RAM))
  2089. flash_protect(FLAG_PROTECT_SET,
  2090. CONFIG_SYS_MONITOR_BASE,
  2091. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  2092. flash_get_info(CONFIG_SYS_MONITOR_BASE));
  2093. #endif
  2094. /* Environment protection ON by default */
  2095. #ifdef CONFIG_ENV_IS_IN_FLASH
  2096. flash_protect(FLAG_PROTECT_SET,
  2097. CONFIG_ENV_ADDR,
  2098. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  2099. flash_get_info(CONFIG_ENV_ADDR));
  2100. #endif
  2101. /* Redundant environment protection ON by default */
  2102. #ifdef CONFIG_ENV_ADDR_REDUND
  2103. flash_protect(FLAG_PROTECT_SET,
  2104. CONFIG_ENV_ADDR_REDUND,
  2105. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
  2106. flash_get_info(CONFIG_ENV_ADDR_REDUND));
  2107. #endif
  2108. #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
  2109. for (i = 0; i < ARRAY_SIZE(apl); i++) {
  2110. debug("autoprotecting from %08lx to %08lx\n",
  2111. apl[i].start, apl[i].start + apl[i].size - 1);
  2112. flash_protect(FLAG_PROTECT_SET,
  2113. apl[i].start,
  2114. apl[i].start + apl[i].size - 1,
  2115. flash_get_info(apl[i].start));
  2116. }
  2117. #endif
  2118. }
  2119. unsigned long flash_init (void)
  2120. {
  2121. unsigned long size = 0;
  2122. int i;
  2123. #ifdef CONFIG_SYS_FLASH_PROTECTION
  2124. /* read environment from EEPROM */
  2125. char s[64];
  2126. getenv_f("unlock", s, sizeof(s));
  2127. #endif
  2128. #ifdef CONFIG_CFI_FLASH /* for driver model */
  2129. cfi_flash_init_dm();
  2130. #endif
  2131. /* Init: no FLASHes known */
  2132. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  2133. flash_info[i].flash_id = FLASH_UNKNOWN;
  2134. /* Optionally write flash configuration register */
  2135. cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
  2136. cfi_flash_config_reg(i));
  2137. if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
  2138. flash_get_size(cfi_flash_bank_addr(i), i);
  2139. size += flash_info[i].size;
  2140. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  2141. #ifndef CONFIG_SYS_FLASH_QUIET_TEST
  2142. printf ("## Unknown flash on Bank %d "
  2143. "- Size = 0x%08lx = %ld MB\n",
  2144. i+1, flash_info[i].size,
  2145. flash_info[i].size >> 20);
  2146. #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
  2147. }
  2148. #ifdef CONFIG_SYS_FLASH_PROTECTION
  2149. else if (strcmp(s, "yes") == 0) {
  2150. /*
  2151. * Only the U-Boot image and it's environment
  2152. * is protected, all other sectors are
  2153. * unprotected (unlocked) if flash hardware
  2154. * protection is used (CONFIG_SYS_FLASH_PROTECTION)
  2155. * and the environment variable "unlock" is
  2156. * set to "yes".
  2157. */
  2158. if (flash_info[i].legacy_unlock) {
  2159. int k;
  2160. /*
  2161. * Disable legacy_unlock temporarily,
  2162. * since flash_real_protect would
  2163. * relock all other sectors again
  2164. * otherwise.
  2165. */
  2166. flash_info[i].legacy_unlock = 0;
  2167. /*
  2168. * Legacy unlocking (e.g. Intel J3) ->
  2169. * unlock only one sector. This will
  2170. * unlock all sectors.
  2171. */
  2172. flash_real_protect (&flash_info[i], 0, 0);
  2173. flash_info[i].legacy_unlock = 1;
  2174. /*
  2175. * Manually mark other sectors as
  2176. * unlocked (unprotected)
  2177. */
  2178. for (k = 1; k < flash_info[i].sector_count; k++)
  2179. flash_info[i].protect[k] = 0;
  2180. } else {
  2181. /*
  2182. * No legancy unlocking -> unlock all sectors
  2183. */
  2184. flash_protect (FLAG_PROTECT_CLEAR,
  2185. flash_info[i].start[0],
  2186. flash_info[i].start[0]
  2187. + flash_info[i].size - 1,
  2188. &flash_info[i]);
  2189. }
  2190. }
  2191. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  2192. }
  2193. flash_protect_default();
  2194. #ifdef CONFIG_FLASH_CFI_MTD
  2195. cfi_mtd_init();
  2196. #endif
  2197. return (size);
  2198. }
  2199. #ifdef CONFIG_CFI_FLASH /* for driver model */
  2200. static int cfi_flash_probe(struct udevice *dev)
  2201. {
  2202. void *blob = (void *)gd->fdt_blob;
  2203. int node = dev->of_offset;
  2204. const fdt32_t *cell;
  2205. phys_addr_t addr;
  2206. int parent, addrc, sizec;
  2207. int len, idx;
  2208. parent = fdt_parent_offset(blob, node);
  2209. of_bus_default_count_cells(blob, parent, &addrc, &sizec);
  2210. /* decode regs, there may be multiple reg tuples. */
  2211. cell = fdt_getprop(blob, node, "reg", &len);
  2212. if (!cell)
  2213. return -ENOENT;
  2214. idx = 0;
  2215. len /= sizeof(fdt32_t);
  2216. while (idx < len) {
  2217. addr = fdt_translate_address((void *)blob,
  2218. node, cell + idx);
  2219. cfi_flash_base[cfi_flash_num_flash_banks++] = addr;
  2220. idx += addrc + sizec;
  2221. }
  2222. gd->bd->bi_flashstart = cfi_flash_base[0];
  2223. return 0;
  2224. }
  2225. static const struct udevice_id cfi_flash_ids[] = {
  2226. { .compatible = "cfi-flash" },
  2227. { .compatible = "jedec-flash" },
  2228. {}
  2229. };
  2230. U_BOOT_DRIVER(cfi_flash) = {
  2231. .name = "cfi_flash",
  2232. .id = UCLASS_MTD,
  2233. .of_match = cfi_flash_ids,
  2234. .probe = cfi_flash_probe,
  2235. };
  2236. #endif /* CONFIG_CFI_FLASH */