cfi_flash.c 63 KB

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