cfi_flash.c 58 KB

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