cfi_flash.c 63 KB

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