cfi_flash.c 64 KB

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