cfi_flash.c 64 KB

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