cfi_flash.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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. return ERR_TIMOUT;
  519. }
  520. udelay (1); /* also triggers watchdog */
  521. }
  522. return ERR_OK;
  523. }
  524. /*-----------------------------------------------------------------------
  525. * Wait for XSR.7 to be set, if it times out print an error, otherwise
  526. * do a full status check.
  527. *
  528. * This routine sets the flash to read-array mode.
  529. */
  530. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  531. ulong tout, char *prompt)
  532. {
  533. int retcode;
  534. retcode = flash_status_check (info, sector, tout, prompt);
  535. switch (info->vendor) {
  536. case CFI_CMDSET_INTEL_PROG_REGIONS:
  537. case CFI_CMDSET_INTEL_EXTENDED:
  538. case CFI_CMDSET_INTEL_STANDARD:
  539. if ((retcode != ERR_OK)
  540. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  541. retcode = ERR_INVAL;
  542. printf ("Flash %s error at address %lx\n", prompt,
  543. info->start[sector]);
  544. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS |
  545. FLASH_STATUS_PSLBS)) {
  546. puts ("Command Sequence Error.\n");
  547. } else if (flash_isset (info, sector, 0,
  548. FLASH_STATUS_ECLBS)) {
  549. puts ("Block Erase Error.\n");
  550. retcode = ERR_NOT_ERASED;
  551. } else if (flash_isset (info, sector, 0,
  552. FLASH_STATUS_PSLBS)) {
  553. puts ("Locking Error\n");
  554. }
  555. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  556. puts ("Block locked.\n");
  557. retcode = ERR_PROTECTED;
  558. }
  559. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  560. puts ("Vpp Low Error.\n");
  561. }
  562. flash_write_cmd (info, sector, 0, info->cmd_reset);
  563. break;
  564. default:
  565. break;
  566. }
  567. return retcode;
  568. }
  569. static int use_flash_status_poll(flash_info_t *info)
  570. {
  571. #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
  572. if (info->vendor == CFI_CMDSET_AMD_EXTENDED ||
  573. info->vendor == CFI_CMDSET_AMD_STANDARD)
  574. return 1;
  575. #endif
  576. return 0;
  577. }
  578. static int flash_status_poll(flash_info_t *info, void *src, void *dst,
  579. ulong tout, char *prompt)
  580. {
  581. #ifdef CONFIG_SYS_CFI_FLASH_STATUS_POLL
  582. ulong start;
  583. int ready;
  584. #if CONFIG_SYS_HZ != 1000
  585. if ((ulong)CONFIG_SYS_HZ > 100000)
  586. tout *= (ulong)CONFIG_SYS_HZ / 1000; /* for a big HZ, avoid overflow */
  587. else
  588. tout = DIV_ROUND_UP(tout * (ulong)CONFIG_SYS_HZ, 1000);
  589. #endif
  590. /* Wait for command completion */
  591. reset_timer();
  592. start = get_timer(0);
  593. while (1) {
  594. switch (info->portwidth) {
  595. case FLASH_CFI_8BIT:
  596. ready = flash_read8(dst) == flash_read8(src);
  597. break;
  598. case FLASH_CFI_16BIT:
  599. ready = flash_read16(dst) == flash_read16(src);
  600. break;
  601. case FLASH_CFI_32BIT:
  602. ready = flash_read32(dst) == flash_read32(src);
  603. break;
  604. case FLASH_CFI_64BIT:
  605. ready = flash_read64(dst) == flash_read64(src);
  606. break;
  607. default:
  608. ready = 0;
  609. break;
  610. }
  611. if (ready)
  612. break;
  613. if (get_timer(start) > tout) {
  614. printf("Flash %s timeout at address %lx data %lx\n",
  615. prompt, (ulong)dst, (ulong)flash_read8(dst));
  616. return ERR_TIMOUT;
  617. }
  618. udelay(1); /* also triggers watchdog */
  619. }
  620. #endif /* CONFIG_SYS_CFI_FLASH_STATUS_POLL */
  621. return ERR_OK;
  622. }
  623. /*-----------------------------------------------------------------------
  624. */
  625. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  626. {
  627. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  628. unsigned short w;
  629. unsigned int l;
  630. unsigned long long ll;
  631. #endif
  632. switch (info->portwidth) {
  633. case FLASH_CFI_8BIT:
  634. cword->c = c;
  635. break;
  636. case FLASH_CFI_16BIT:
  637. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  638. w = c;
  639. w <<= 8;
  640. cword->w = (cword->w >> 8) | w;
  641. #else
  642. cword->w = (cword->w << 8) | c;
  643. #endif
  644. break;
  645. case FLASH_CFI_32BIT:
  646. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  647. l = c;
  648. l <<= 24;
  649. cword->l = (cword->l >> 8) | l;
  650. #else
  651. cword->l = (cword->l << 8) | c;
  652. #endif
  653. break;
  654. case FLASH_CFI_64BIT:
  655. #if defined(__LITTLE_ENDIAN) && !defined(CONFIG_SYS_WRITE_SWAPPED_DATA)
  656. ll = c;
  657. ll <<= 56;
  658. cword->ll = (cword->ll >> 8) | ll;
  659. #else
  660. cword->ll = (cword->ll << 8) | c;
  661. #endif
  662. break;
  663. }
  664. }
  665. /*
  666. * Loop through the sector table starting from the previously found sector.
  667. * Searches forwards or backwards, dependent on the passed address.
  668. */
  669. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  670. {
  671. static flash_sect_t saved_sector = 0; /* previously found sector */
  672. flash_sect_t sector = saved_sector;
  673. while ((info->start[sector] < addr)
  674. && (sector < info->sector_count - 1))
  675. sector++;
  676. while ((info->start[sector] > addr) && (sector > 0))
  677. /*
  678. * also decrements the sector in case of an overshot
  679. * in the first loop
  680. */
  681. sector--;
  682. saved_sector = sector;
  683. return sector;
  684. }
  685. /*-----------------------------------------------------------------------
  686. */
  687. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  688. cfiword_t cword)
  689. {
  690. void *dstaddr = (void *)dest;
  691. int flag;
  692. flash_sect_t sect = 0;
  693. char sect_found = 0;
  694. /* Check if Flash is (sufficiently) erased */
  695. switch (info->portwidth) {
  696. case FLASH_CFI_8BIT:
  697. flag = ((flash_read8(dstaddr) & cword.c) == cword.c);
  698. break;
  699. case FLASH_CFI_16BIT:
  700. flag = ((flash_read16(dstaddr) & cword.w) == cword.w);
  701. break;
  702. case FLASH_CFI_32BIT:
  703. flag = ((flash_read32(dstaddr) & cword.l) == cword.l);
  704. break;
  705. case FLASH_CFI_64BIT:
  706. flag = ((flash_read64(dstaddr) & cword.ll) == cword.ll);
  707. break;
  708. default:
  709. flag = 0;
  710. break;
  711. }
  712. if (!flag)
  713. return ERR_NOT_ERASED;
  714. /* Disable interrupts which might cause a timeout here */
  715. flag = disable_interrupts ();
  716. switch (info->vendor) {
  717. case CFI_CMDSET_INTEL_PROG_REGIONS:
  718. case CFI_CMDSET_INTEL_EXTENDED:
  719. case CFI_CMDSET_INTEL_STANDARD:
  720. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  721. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  722. break;
  723. case CFI_CMDSET_AMD_EXTENDED:
  724. case CFI_CMDSET_AMD_STANDARD:
  725. sect = find_sector(info, dest);
  726. flash_unlock_seq (info, sect);
  727. flash_write_cmd (info, sect, info->addr_unlock1, AMD_CMD_WRITE);
  728. sect_found = 1;
  729. break;
  730. #ifdef CONFIG_FLASH_CFI_LEGACY
  731. case CFI_CMDSET_AMD_LEGACY:
  732. sect = find_sector(info, dest);
  733. flash_unlock_seq (info, 0);
  734. flash_write_cmd (info, 0, info->addr_unlock1, AMD_CMD_WRITE);
  735. sect_found = 1;
  736. break;
  737. #endif
  738. }
  739. switch (info->portwidth) {
  740. case FLASH_CFI_8BIT:
  741. flash_write8(cword.c, dstaddr);
  742. break;
  743. case FLASH_CFI_16BIT:
  744. flash_write16(cword.w, dstaddr);
  745. break;
  746. case FLASH_CFI_32BIT:
  747. flash_write32(cword.l, dstaddr);
  748. break;
  749. case FLASH_CFI_64BIT:
  750. flash_write64(cword.ll, dstaddr);
  751. break;
  752. }
  753. /* re-enable interrupts if necessary */
  754. if (flag)
  755. enable_interrupts ();
  756. if (!sect_found)
  757. sect = find_sector (info, dest);
  758. if (use_flash_status_poll(info))
  759. return flash_status_poll(info, &cword, dstaddr,
  760. info->write_tout, "write");
  761. else
  762. return flash_full_status_check(info, sect,
  763. info->write_tout, "write");
  764. }
  765. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  766. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  767. int len)
  768. {
  769. flash_sect_t sector;
  770. int cnt;
  771. int retcode;
  772. void *src = cp;
  773. void *dst = (void *)dest;
  774. void *dst2 = dst;
  775. int flag = 0;
  776. uint offset = 0;
  777. unsigned int shift;
  778. uchar write_cmd;
  779. switch (info->portwidth) {
  780. case FLASH_CFI_8BIT:
  781. shift = 0;
  782. break;
  783. case FLASH_CFI_16BIT:
  784. shift = 1;
  785. break;
  786. case FLASH_CFI_32BIT:
  787. shift = 2;
  788. break;
  789. case FLASH_CFI_64BIT:
  790. shift = 3;
  791. break;
  792. default:
  793. retcode = ERR_INVAL;
  794. goto out_unmap;
  795. }
  796. cnt = len >> shift;
  797. while ((cnt-- > 0) && (flag == 0)) {
  798. switch (info->portwidth) {
  799. case FLASH_CFI_8BIT:
  800. flag = ((flash_read8(dst2) & flash_read8(src)) ==
  801. flash_read8(src));
  802. src += 1, dst2 += 1;
  803. break;
  804. case FLASH_CFI_16BIT:
  805. flag = ((flash_read16(dst2) & flash_read16(src)) ==
  806. flash_read16(src));
  807. src += 2, dst2 += 2;
  808. break;
  809. case FLASH_CFI_32BIT:
  810. flag = ((flash_read32(dst2) & flash_read32(src)) ==
  811. flash_read32(src));
  812. src += 4, dst2 += 4;
  813. break;
  814. case FLASH_CFI_64BIT:
  815. flag = ((flash_read64(dst2) & flash_read64(src)) ==
  816. flash_read64(src));
  817. src += 8, dst2 += 8;
  818. break;
  819. }
  820. }
  821. if (!flag) {
  822. retcode = ERR_NOT_ERASED;
  823. goto out_unmap;
  824. }
  825. src = cp;
  826. sector = find_sector (info, dest);
  827. switch (info->vendor) {
  828. case CFI_CMDSET_INTEL_PROG_REGIONS:
  829. case CFI_CMDSET_INTEL_STANDARD:
  830. case CFI_CMDSET_INTEL_EXTENDED:
  831. write_cmd = (info->vendor == CFI_CMDSET_INTEL_PROG_REGIONS) ?
  832. FLASH_CMD_WRITE_BUFFER_PROG : FLASH_CMD_WRITE_TO_BUFFER;
  833. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  834. flash_write_cmd (info, sector, 0, FLASH_CMD_READ_STATUS);
  835. flash_write_cmd (info, sector, 0, write_cmd);
  836. retcode = flash_status_check (info, sector,
  837. info->buffer_write_tout,
  838. "write to buffer");
  839. if (retcode == ERR_OK) {
  840. /* reduce the number of loops by the width of
  841. * the port */
  842. cnt = len >> shift;
  843. flash_write_cmd (info, sector, 0, cnt - 1);
  844. while (cnt-- > 0) {
  845. switch (info->portwidth) {
  846. case FLASH_CFI_8BIT:
  847. flash_write8(flash_read8(src), dst);
  848. src += 1, dst += 1;
  849. break;
  850. case FLASH_CFI_16BIT:
  851. flash_write16(flash_read16(src), dst);
  852. src += 2, dst += 2;
  853. break;
  854. case FLASH_CFI_32BIT:
  855. flash_write32(flash_read32(src), dst);
  856. src += 4, dst += 4;
  857. break;
  858. case FLASH_CFI_64BIT:
  859. flash_write64(flash_read64(src), dst);
  860. src += 8, dst += 8;
  861. break;
  862. default:
  863. retcode = ERR_INVAL;
  864. goto out_unmap;
  865. }
  866. }
  867. flash_write_cmd (info, sector, 0,
  868. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  869. retcode = flash_full_status_check (
  870. info, sector, info->buffer_write_tout,
  871. "buffer write");
  872. }
  873. break;
  874. case CFI_CMDSET_AMD_STANDARD:
  875. case CFI_CMDSET_AMD_EXTENDED:
  876. flash_unlock_seq(info,0);
  877. #ifdef CONFIG_FLASH_SPANSION_S29WS_N
  878. offset = ((unsigned long)dst - info->start[sector]) >> shift;
  879. #endif
  880. flash_write_cmd(info, sector, offset, AMD_CMD_WRITE_TO_BUFFER);
  881. cnt = len >> shift;
  882. flash_write_cmd(info, sector, offset, cnt - 1);
  883. switch (info->portwidth) {
  884. case FLASH_CFI_8BIT:
  885. while (cnt-- > 0) {
  886. flash_write8(flash_read8(src), dst);
  887. src += 1, dst += 1;
  888. }
  889. break;
  890. case FLASH_CFI_16BIT:
  891. while (cnt-- > 0) {
  892. flash_write16(flash_read16(src), dst);
  893. src += 2, dst += 2;
  894. }
  895. break;
  896. case FLASH_CFI_32BIT:
  897. while (cnt-- > 0) {
  898. flash_write32(flash_read32(src), dst);
  899. src += 4, dst += 4;
  900. }
  901. break;
  902. case FLASH_CFI_64BIT:
  903. while (cnt-- > 0) {
  904. flash_write64(flash_read64(src), dst);
  905. src += 8, dst += 8;
  906. }
  907. break;
  908. default:
  909. retcode = ERR_INVAL;
  910. goto out_unmap;
  911. }
  912. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
  913. if (use_flash_status_poll(info))
  914. retcode = flash_status_poll(info, src - (1 << shift),
  915. dst - (1 << shift),
  916. info->buffer_write_tout,
  917. "buffer write");
  918. else
  919. retcode = flash_full_status_check(info, sector,
  920. info->buffer_write_tout,
  921. "buffer write");
  922. break;
  923. default:
  924. debug ("Unknown Command Set\n");
  925. retcode = ERR_INVAL;
  926. break;
  927. }
  928. out_unmap:
  929. return retcode;
  930. }
  931. #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
  932. /*-----------------------------------------------------------------------
  933. */
  934. int flash_erase (flash_info_t * info, int s_first, int s_last)
  935. {
  936. int rcode = 0;
  937. int prot;
  938. flash_sect_t sect;
  939. int st;
  940. if (info->flash_id != FLASH_MAN_CFI) {
  941. puts ("Can't erase unknown flash type - aborted\n");
  942. return 1;
  943. }
  944. if ((s_first < 0) || (s_first > s_last)) {
  945. puts ("- no sectors to erase\n");
  946. return 1;
  947. }
  948. prot = 0;
  949. for (sect = s_first; sect <= s_last; ++sect) {
  950. if (info->protect[sect]) {
  951. prot++;
  952. }
  953. }
  954. if (prot) {
  955. printf ("- Warning: %d protected sectors will not be erased!\n",
  956. prot);
  957. } else if (flash_verbose) {
  958. putc ('\n');
  959. }
  960. for (sect = s_first; sect <= s_last; sect++) {
  961. if (info->protect[sect] == 0) { /* not protected */
  962. switch (info->vendor) {
  963. case CFI_CMDSET_INTEL_PROG_REGIONS:
  964. case CFI_CMDSET_INTEL_STANDARD:
  965. case CFI_CMDSET_INTEL_EXTENDED:
  966. flash_write_cmd (info, sect, 0,
  967. FLASH_CMD_CLEAR_STATUS);
  968. flash_write_cmd (info, sect, 0,
  969. FLASH_CMD_BLOCK_ERASE);
  970. flash_write_cmd (info, sect, 0,
  971. FLASH_CMD_ERASE_CONFIRM);
  972. break;
  973. case CFI_CMDSET_AMD_STANDARD:
  974. case CFI_CMDSET_AMD_EXTENDED:
  975. flash_unlock_seq (info, sect);
  976. flash_write_cmd (info, sect,
  977. info->addr_unlock1,
  978. AMD_CMD_ERASE_START);
  979. flash_unlock_seq (info, sect);
  980. flash_write_cmd (info, sect, 0,
  981. AMD_CMD_ERASE_SECTOR);
  982. break;
  983. #ifdef CONFIG_FLASH_CFI_LEGACY
  984. case CFI_CMDSET_AMD_LEGACY:
  985. flash_unlock_seq (info, 0);
  986. flash_write_cmd (info, 0, info->addr_unlock1,
  987. AMD_CMD_ERASE_START);
  988. flash_unlock_seq (info, 0);
  989. flash_write_cmd (info, sect, 0,
  990. AMD_CMD_ERASE_SECTOR);
  991. break;
  992. #endif
  993. default:
  994. debug ("Unkown flash vendor %d\n",
  995. info->vendor);
  996. break;
  997. }
  998. if (use_flash_status_poll(info)) {
  999. cfiword_t cword = (cfiword_t)0xffffffffffffffffULL;
  1000. void *dest;
  1001. dest = flash_map(info, sect, 0);
  1002. st = flash_status_poll(info, &cword, dest,
  1003. info->erase_blk_tout, "erase");
  1004. flash_unmap(info, sect, 0, dest);
  1005. } else
  1006. st = flash_full_status_check(info, sect,
  1007. info->erase_blk_tout,
  1008. "erase");
  1009. if (st)
  1010. rcode = 1;
  1011. else if (flash_verbose)
  1012. putc ('.');
  1013. }
  1014. }
  1015. if (flash_verbose)
  1016. puts (" done\n");
  1017. return rcode;
  1018. }
  1019. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  1020. static int sector_erased(flash_info_t *info, int i)
  1021. {
  1022. int k;
  1023. int size;
  1024. u32 *flash;
  1025. /*
  1026. * Check if whole sector is erased
  1027. */
  1028. size = flash_sector_size(info, i);
  1029. flash = (u32 *)info->start[i];
  1030. /* divide by 4 for longword access */
  1031. size = size >> 2;
  1032. for (k = 0; k < size; k++) {
  1033. if (flash_read32(flash++) != 0xffffffff)
  1034. return 0; /* not erased */
  1035. }
  1036. return 1; /* erased */
  1037. }
  1038. #endif /* CONFIG_SYS_FLASH_EMPTY_INFO */
  1039. void flash_print_info (flash_info_t * info)
  1040. {
  1041. int i;
  1042. if (info->flash_id != FLASH_MAN_CFI) {
  1043. puts ("missing or unknown FLASH type\n");
  1044. return;
  1045. }
  1046. printf ("%s flash (%d x %d)",
  1047. info->name,
  1048. (info->portwidth << 3), (info->chipwidth << 3));
  1049. if (info->size < 1024*1024)
  1050. printf (" Size: %ld kB in %d Sectors\n",
  1051. info->size >> 10, info->sector_count);
  1052. else
  1053. printf (" Size: %ld MB in %d Sectors\n",
  1054. info->size >> 20, info->sector_count);
  1055. printf (" ");
  1056. switch (info->vendor) {
  1057. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1058. printf ("Intel Prog Regions");
  1059. break;
  1060. case CFI_CMDSET_INTEL_STANDARD:
  1061. printf ("Intel Standard");
  1062. break;
  1063. case CFI_CMDSET_INTEL_EXTENDED:
  1064. printf ("Intel Extended");
  1065. break;
  1066. case CFI_CMDSET_AMD_STANDARD:
  1067. printf ("AMD Standard");
  1068. break;
  1069. case CFI_CMDSET_AMD_EXTENDED:
  1070. printf ("AMD Extended");
  1071. break;
  1072. #ifdef CONFIG_FLASH_CFI_LEGACY
  1073. case CFI_CMDSET_AMD_LEGACY:
  1074. printf ("AMD Legacy");
  1075. break;
  1076. #endif
  1077. default:
  1078. printf ("Unknown (%d)", info->vendor);
  1079. break;
  1080. }
  1081. printf (" command set, Manufacturer ID: 0x%02X, Device ID: 0x",
  1082. info->manufacturer_id);
  1083. printf (info->chipwidth == FLASH_CFI_16BIT ? "%04X" : "%02X",
  1084. info->device_id);
  1085. if (info->device_id == 0x7E) {
  1086. printf("%04X", info->device_id2);
  1087. }
  1088. printf ("\n Erase timeout: %ld ms, write timeout: %ld ms\n",
  1089. info->erase_blk_tout,
  1090. info->write_tout);
  1091. if (info->buffer_size > 1) {
  1092. printf (" Buffer write timeout: %ld ms, "
  1093. "buffer size: %d bytes\n",
  1094. info->buffer_write_tout,
  1095. info->buffer_size);
  1096. }
  1097. puts ("\n Sector Start Addresses:");
  1098. for (i = 0; i < info->sector_count; ++i) {
  1099. if (ctrlc())
  1100. break;
  1101. if ((i % 5) == 0)
  1102. putc('\n');
  1103. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  1104. /* print empty and read-only info */
  1105. printf (" %08lX %c %s ",
  1106. info->start[i],
  1107. sector_erased(info, i) ? 'E' : ' ',
  1108. info->protect[i] ? "RO" : " ");
  1109. #else /* ! CONFIG_SYS_FLASH_EMPTY_INFO */
  1110. printf (" %08lX %s ",
  1111. info->start[i],
  1112. info->protect[i] ? "RO" : " ");
  1113. #endif
  1114. }
  1115. putc ('\n');
  1116. return;
  1117. }
  1118. /*-----------------------------------------------------------------------
  1119. * This is used in a few places in write_buf() to show programming
  1120. * progress. Making it a function is nasty because it needs to do side
  1121. * effect updates to digit and dots. Repeated code is nasty too, so
  1122. * we define it once here.
  1123. */
  1124. #ifdef CONFIG_FLASH_SHOW_PROGRESS
  1125. #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub) \
  1126. if (flash_verbose) { \
  1127. dots -= dots_sub; \
  1128. if ((scale > 0) && (dots <= 0)) { \
  1129. if ((digit % 5) == 0) \
  1130. printf ("%d", digit / 5); \
  1131. else \
  1132. putc ('.'); \
  1133. digit--; \
  1134. dots += scale; \
  1135. } \
  1136. }
  1137. #else
  1138. #define FLASH_SHOW_PROGRESS(scale, dots, digit, dots_sub)
  1139. #endif
  1140. /*-----------------------------------------------------------------------
  1141. * Copy memory to flash, returns:
  1142. * 0 - OK
  1143. * 1 - write timeout
  1144. * 2 - Flash not erased
  1145. */
  1146. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  1147. {
  1148. ulong wp;
  1149. uchar *p;
  1150. int aln;
  1151. cfiword_t cword;
  1152. int i, rc;
  1153. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  1154. int buffered_size;
  1155. #endif
  1156. #ifdef CONFIG_FLASH_SHOW_PROGRESS
  1157. int digit = CONFIG_FLASH_SHOW_PROGRESS;
  1158. int scale = 0;
  1159. int dots = 0;
  1160. /*
  1161. * Suppress if there are fewer than CONFIG_FLASH_SHOW_PROGRESS writes.
  1162. */
  1163. if (cnt >= CONFIG_FLASH_SHOW_PROGRESS) {
  1164. scale = (int)((cnt + CONFIG_FLASH_SHOW_PROGRESS - 1) /
  1165. CONFIG_FLASH_SHOW_PROGRESS);
  1166. }
  1167. #endif
  1168. /* get lower aligned address */
  1169. wp = (addr & ~(info->portwidth - 1));
  1170. /* handle unaligned start */
  1171. if ((aln = addr - wp) != 0) {
  1172. cword.l = 0;
  1173. p = (uchar *)wp;
  1174. for (i = 0; i < aln; ++i)
  1175. flash_add_byte (info, &cword, flash_read8(p + i));
  1176. for (; (i < info->portwidth) && (cnt > 0); i++) {
  1177. flash_add_byte (info, &cword, *src++);
  1178. cnt--;
  1179. }
  1180. for (; (cnt == 0) && (i < info->portwidth); ++i)
  1181. flash_add_byte (info, &cword, flash_read8(p + i));
  1182. rc = flash_write_cfiword (info, wp, cword);
  1183. if (rc != 0)
  1184. return rc;
  1185. wp += i;
  1186. FLASH_SHOW_PROGRESS(scale, dots, digit, i);
  1187. }
  1188. /* handle the aligned part */
  1189. #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
  1190. buffered_size = (info->portwidth / info->chipwidth);
  1191. buffered_size *= info->buffer_size;
  1192. while (cnt >= info->portwidth) {
  1193. /* prohibit buffer write when buffer_size is 1 */
  1194. if (info->buffer_size == 1) {
  1195. cword.l = 0;
  1196. for (i = 0; i < info->portwidth; i++)
  1197. flash_add_byte (info, &cword, *src++);
  1198. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  1199. return rc;
  1200. wp += info->portwidth;
  1201. cnt -= info->portwidth;
  1202. continue;
  1203. }
  1204. /* write buffer until next buffered_size aligned boundary */
  1205. i = buffered_size - (wp % buffered_size);
  1206. if (i > cnt)
  1207. i = cnt;
  1208. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  1209. return rc;
  1210. i -= i & (info->portwidth - 1);
  1211. wp += i;
  1212. src += i;
  1213. cnt -= i;
  1214. FLASH_SHOW_PROGRESS(scale, dots, digit, i);
  1215. }
  1216. #else
  1217. while (cnt >= info->portwidth) {
  1218. cword.l = 0;
  1219. for (i = 0; i < info->portwidth; i++) {
  1220. flash_add_byte (info, &cword, *src++);
  1221. }
  1222. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  1223. return rc;
  1224. wp += info->portwidth;
  1225. cnt -= info->portwidth;
  1226. FLASH_SHOW_PROGRESS(scale, dots, digit, info->portwidth);
  1227. }
  1228. #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
  1229. if (cnt == 0) {
  1230. return (0);
  1231. }
  1232. /*
  1233. * handle unaligned tail bytes
  1234. */
  1235. cword.l = 0;
  1236. p = (uchar *)wp;
  1237. for (i = 0; (i < info->portwidth) && (cnt > 0); ++i) {
  1238. flash_add_byte (info, &cword, *src++);
  1239. --cnt;
  1240. }
  1241. for (; i < info->portwidth; ++i)
  1242. flash_add_byte (info, &cword, flash_read8(p + i));
  1243. return flash_write_cfiword (info, wp, cword);
  1244. }
  1245. /*-----------------------------------------------------------------------
  1246. */
  1247. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1248. int flash_real_protect (flash_info_t * info, long sector, int prot)
  1249. {
  1250. int retcode = 0;
  1251. switch (info->vendor) {
  1252. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1253. case CFI_CMDSET_INTEL_STANDARD:
  1254. case CFI_CMDSET_INTEL_EXTENDED:
  1255. /*
  1256. * see errata called
  1257. * "Numonyx Axcell P33/P30 Specification Update" :)
  1258. */
  1259. flash_write_cmd (info, sector, 0, FLASH_CMD_READ_ID);
  1260. if (!flash_isequal (info, sector, FLASH_OFFSET_PROTECT,
  1261. prot)) {
  1262. /*
  1263. * cmd must come before FLASH_CMD_PROTECT + 20us
  1264. * Disable interrupts which might cause a timeout here.
  1265. */
  1266. int flag = disable_interrupts ();
  1267. unsigned short cmd;
  1268. if (prot)
  1269. cmd = FLASH_CMD_PROTECT_SET;
  1270. else
  1271. cmd = FLASH_CMD_PROTECT_CLEAR;
  1272. flash_write_cmd (info, sector, 0,
  1273. FLASH_CMD_PROTECT);
  1274. flash_write_cmd (info, sector, 0, cmd);
  1275. /* re-enable interrupts if necessary */
  1276. if (flag)
  1277. enable_interrupts ();
  1278. }
  1279. break;
  1280. case CFI_CMDSET_AMD_EXTENDED:
  1281. case CFI_CMDSET_AMD_STANDARD:
  1282. /* U-Boot only checks the first byte */
  1283. if (info->manufacturer_id == (uchar)ATM_MANUFACT) {
  1284. if (prot) {
  1285. flash_unlock_seq (info, 0);
  1286. flash_write_cmd (info, 0,
  1287. info->addr_unlock1,
  1288. ATM_CMD_SOFTLOCK_START);
  1289. flash_unlock_seq (info, 0);
  1290. flash_write_cmd (info, sector, 0,
  1291. ATM_CMD_LOCK_SECT);
  1292. } else {
  1293. flash_write_cmd (info, 0,
  1294. info->addr_unlock1,
  1295. AMD_CMD_UNLOCK_START);
  1296. if (info->device_id == ATM_ID_BV6416)
  1297. flash_write_cmd (info, sector,
  1298. 0, ATM_CMD_UNLOCK_SECT);
  1299. }
  1300. }
  1301. break;
  1302. #ifdef CONFIG_FLASH_CFI_LEGACY
  1303. case CFI_CMDSET_AMD_LEGACY:
  1304. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1305. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  1306. if (prot)
  1307. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  1308. else
  1309. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  1310. #endif
  1311. };
  1312. /*
  1313. * Flash needs to be in status register read mode for
  1314. * flash_full_status_check() to work correctly
  1315. */
  1316. flash_write_cmd(info, sector, 0, FLASH_CMD_READ_STATUS);
  1317. if ((retcode =
  1318. flash_full_status_check (info, sector, info->erase_blk_tout,
  1319. prot ? "protect" : "unprotect")) == 0) {
  1320. info->protect[sector] = prot;
  1321. /*
  1322. * On some of Intel's flash chips (marked via legacy_unlock)
  1323. * unprotect unprotects all locking.
  1324. */
  1325. if ((prot == 0) && (info->legacy_unlock)) {
  1326. flash_sect_t i;
  1327. for (i = 0; i < info->sector_count; i++) {
  1328. if (info->protect[i])
  1329. flash_real_protect (info, i, 1);
  1330. }
  1331. }
  1332. }
  1333. return retcode;
  1334. }
  1335. /*-----------------------------------------------------------------------
  1336. * flash_read_user_serial - read the OneTimeProgramming cells
  1337. */
  1338. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  1339. int len)
  1340. {
  1341. uchar *src;
  1342. uchar *dst;
  1343. dst = buffer;
  1344. src = flash_map (info, 0, FLASH_OFFSET_USER_PROTECTION);
  1345. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  1346. memcpy (dst, src + offset, len);
  1347. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1348. flash_unmap(info, 0, FLASH_OFFSET_USER_PROTECTION, src);
  1349. }
  1350. /*
  1351. * flash_read_factory_serial - read the device Id from the protection area
  1352. */
  1353. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  1354. int len)
  1355. {
  1356. uchar *src;
  1357. src = flash_map (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  1358. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  1359. memcpy (buffer, src + offset, len);
  1360. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1361. flash_unmap(info, 0, FLASH_OFFSET_INTEL_PROTECTION, src);
  1362. }
  1363. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  1364. /*-----------------------------------------------------------------------
  1365. * Reverse the order of the erase regions in the CFI QRY structure.
  1366. * This is needed for chips that are either a) correctly detected as
  1367. * top-boot, or b) buggy.
  1368. */
  1369. static void cfi_reverse_geometry(struct cfi_qry *qry)
  1370. {
  1371. unsigned int i, j;
  1372. u32 tmp;
  1373. for (i = 0, j = qry->num_erase_regions - 1; i < j; i++, j--) {
  1374. tmp = qry->erase_region_info[i];
  1375. qry->erase_region_info[i] = qry->erase_region_info[j];
  1376. qry->erase_region_info[j] = tmp;
  1377. }
  1378. }
  1379. /*-----------------------------------------------------------------------
  1380. * read jedec ids from device and set corresponding fields in info struct
  1381. *
  1382. * Note: assume cfi->vendor, cfi->portwidth and cfi->chipwidth are correct
  1383. *
  1384. */
  1385. static void cmdset_intel_read_jedec_ids(flash_info_t *info)
  1386. {
  1387. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1388. flash_write_cmd(info, 0, 0, FLASH_CMD_READ_ID);
  1389. udelay(1000); /* some flash are slow to respond */
  1390. info->manufacturer_id = flash_read_uchar (info,
  1391. FLASH_OFFSET_MANUFACTURER_ID);
  1392. info->device_id = (info->chipwidth == FLASH_CFI_16BIT) ?
  1393. flash_read_word (info, FLASH_OFFSET_DEVICE_ID) :
  1394. flash_read_uchar (info, FLASH_OFFSET_DEVICE_ID);
  1395. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1396. }
  1397. static int cmdset_intel_init(flash_info_t *info, struct cfi_qry *qry)
  1398. {
  1399. info->cmd_reset = FLASH_CMD_RESET;
  1400. cmdset_intel_read_jedec_ids(info);
  1401. flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
  1402. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1403. /* read legacy lock/unlock bit from intel flash */
  1404. if (info->ext_addr) {
  1405. info->legacy_unlock = flash_read_uchar (info,
  1406. info->ext_addr + 5) & 0x08;
  1407. }
  1408. #endif
  1409. return 0;
  1410. }
  1411. static void cmdset_amd_read_jedec_ids(flash_info_t *info)
  1412. {
  1413. ushort bankId = 0;
  1414. uchar manuId;
  1415. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1416. flash_unlock_seq(info, 0);
  1417. flash_write_cmd(info, 0, info->addr_unlock1, FLASH_CMD_READ_ID);
  1418. udelay(1000); /* some flash are slow to respond */
  1419. manuId = flash_read_uchar (info, FLASH_OFFSET_MANUFACTURER_ID);
  1420. /* JEDEC JEP106Z specifies ID codes up to bank 7 */
  1421. while (manuId == FLASH_CONTINUATION_CODE && bankId < 0x800) {
  1422. bankId += 0x100;
  1423. manuId = flash_read_uchar (info,
  1424. bankId | FLASH_OFFSET_MANUFACTURER_ID);
  1425. }
  1426. info->manufacturer_id = manuId;
  1427. switch (info->chipwidth){
  1428. case FLASH_CFI_8BIT:
  1429. info->device_id = flash_read_uchar (info,
  1430. FLASH_OFFSET_DEVICE_ID);
  1431. if (info->device_id == 0x7E) {
  1432. /* AMD 3-byte (expanded) device ids */
  1433. info->device_id2 = flash_read_uchar (info,
  1434. FLASH_OFFSET_DEVICE_ID2);
  1435. info->device_id2 <<= 8;
  1436. info->device_id2 |= flash_read_uchar (info,
  1437. FLASH_OFFSET_DEVICE_ID3);
  1438. }
  1439. break;
  1440. case FLASH_CFI_16BIT:
  1441. info->device_id = flash_read_word (info,
  1442. FLASH_OFFSET_DEVICE_ID);
  1443. break;
  1444. default:
  1445. break;
  1446. }
  1447. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1448. }
  1449. static int cmdset_amd_init(flash_info_t *info, struct cfi_qry *qry)
  1450. {
  1451. info->cmd_reset = AMD_CMD_RESET;
  1452. cmdset_amd_read_jedec_ids(info);
  1453. flash_write_cmd(info, 0, info->cfi_offset, FLASH_CMD_CFI);
  1454. return 0;
  1455. }
  1456. #ifdef CONFIG_FLASH_CFI_LEGACY
  1457. static void flash_read_jedec_ids (flash_info_t * info)
  1458. {
  1459. info->manufacturer_id = 0;
  1460. info->device_id = 0;
  1461. info->device_id2 = 0;
  1462. switch (info->vendor) {
  1463. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1464. case CFI_CMDSET_INTEL_STANDARD:
  1465. case CFI_CMDSET_INTEL_EXTENDED:
  1466. cmdset_intel_read_jedec_ids(info);
  1467. break;
  1468. case CFI_CMDSET_AMD_STANDARD:
  1469. case CFI_CMDSET_AMD_EXTENDED:
  1470. cmdset_amd_read_jedec_ids(info);
  1471. break;
  1472. default:
  1473. break;
  1474. }
  1475. }
  1476. /*-----------------------------------------------------------------------
  1477. * Call board code to request info about non-CFI flash.
  1478. * board_flash_get_legacy needs to fill in at least:
  1479. * info->portwidth, info->chipwidth and info->interface for Jedec probing.
  1480. */
  1481. static int flash_detect_legacy(phys_addr_t base, int banknum)
  1482. {
  1483. flash_info_t *info = &flash_info[banknum];
  1484. if (board_flash_get_legacy(base, banknum, info)) {
  1485. /* board code may have filled info completely. If not, we
  1486. use JEDEC ID probing. */
  1487. if (!info->vendor) {
  1488. int modes[] = {
  1489. CFI_CMDSET_AMD_STANDARD,
  1490. CFI_CMDSET_INTEL_STANDARD
  1491. };
  1492. int i;
  1493. for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
  1494. info->vendor = modes[i];
  1495. info->start[0] =
  1496. (ulong)map_physmem(base,
  1497. info->portwidth,
  1498. MAP_NOCACHE);
  1499. if (info->portwidth == FLASH_CFI_8BIT
  1500. && info->interface == FLASH_CFI_X8X16) {
  1501. info->addr_unlock1 = 0x2AAA;
  1502. info->addr_unlock2 = 0x5555;
  1503. } else {
  1504. info->addr_unlock1 = 0x5555;
  1505. info->addr_unlock2 = 0x2AAA;
  1506. }
  1507. flash_read_jedec_ids(info);
  1508. debug("JEDEC PROBE: ID %x %x %x\n",
  1509. info->manufacturer_id,
  1510. info->device_id,
  1511. info->device_id2);
  1512. if (jedec_flash_match(info, info->start[0]))
  1513. break;
  1514. else
  1515. unmap_physmem((void *)info->start[0],
  1516. MAP_NOCACHE);
  1517. }
  1518. }
  1519. switch(info->vendor) {
  1520. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1521. case CFI_CMDSET_INTEL_STANDARD:
  1522. case CFI_CMDSET_INTEL_EXTENDED:
  1523. info->cmd_reset = FLASH_CMD_RESET;
  1524. break;
  1525. case CFI_CMDSET_AMD_STANDARD:
  1526. case CFI_CMDSET_AMD_EXTENDED:
  1527. case CFI_CMDSET_AMD_LEGACY:
  1528. info->cmd_reset = AMD_CMD_RESET;
  1529. break;
  1530. }
  1531. info->flash_id = FLASH_MAN_CFI;
  1532. return 1;
  1533. }
  1534. return 0; /* use CFI */
  1535. }
  1536. #else
  1537. static inline int flash_detect_legacy(phys_addr_t base, int banknum)
  1538. {
  1539. return 0; /* use CFI */
  1540. }
  1541. #endif
  1542. /*-----------------------------------------------------------------------
  1543. * detect if flash is compatible with the Common Flash Interface (CFI)
  1544. * http://www.jedec.org/download/search/jesd68.pdf
  1545. */
  1546. static void flash_read_cfi (flash_info_t *info, void *buf,
  1547. unsigned int start, size_t len)
  1548. {
  1549. u8 *p = buf;
  1550. unsigned int i;
  1551. for (i = 0; i < len; i++)
  1552. p[i] = flash_read_uchar(info, start + i);
  1553. }
  1554. void __flash_cmd_reset(flash_info_t *info)
  1555. {
  1556. /*
  1557. * We do not yet know what kind of commandset to use, so we issue
  1558. * the reset command in both Intel and AMD variants, in the hope
  1559. * that AMD flash roms ignore the Intel command.
  1560. */
  1561. flash_write_cmd(info, 0, 0, AMD_CMD_RESET);
  1562. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1563. }
  1564. void flash_cmd_reset(flash_info_t *info)
  1565. __attribute__((weak,alias("__flash_cmd_reset")));
  1566. static int __flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
  1567. {
  1568. int cfi_offset;
  1569. /* Issue FLASH reset command */
  1570. flash_cmd_reset(info);
  1571. for (cfi_offset=0;
  1572. cfi_offset < sizeof(flash_offset_cfi) / sizeof(uint);
  1573. cfi_offset++) {
  1574. flash_write_cmd (info, 0, flash_offset_cfi[cfi_offset],
  1575. FLASH_CMD_CFI);
  1576. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  1577. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  1578. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  1579. flash_read_cfi(info, qry, FLASH_OFFSET_CFI_RESP,
  1580. sizeof(struct cfi_qry));
  1581. info->interface = le16_to_cpu(qry->interface_desc);
  1582. info->cfi_offset = flash_offset_cfi[cfi_offset];
  1583. debug ("device interface is %d\n",
  1584. info->interface);
  1585. debug ("found port %d chip %d ",
  1586. info->portwidth, info->chipwidth);
  1587. debug ("port %d bits chip %d bits\n",
  1588. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1589. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1590. /* calculate command offsets as in the Linux driver */
  1591. info->addr_unlock1 = 0x555;
  1592. info->addr_unlock2 = 0x2aa;
  1593. /*
  1594. * modify the unlock address if we are
  1595. * in compatibility mode
  1596. */
  1597. if ( /* x8/x16 in x8 mode */
  1598. ((info->chipwidth == FLASH_CFI_BY8) &&
  1599. (info->interface == FLASH_CFI_X8X16)) ||
  1600. /* x16/x32 in x16 mode */
  1601. ((info->chipwidth == FLASH_CFI_BY16) &&
  1602. (info->interface == FLASH_CFI_X16X32)))
  1603. {
  1604. info->addr_unlock1 = 0xaaa;
  1605. info->addr_unlock2 = 0x555;
  1606. }
  1607. info->name = "CFI conformant";
  1608. return 1;
  1609. }
  1610. }
  1611. return 0;
  1612. }
  1613. static int flash_detect_cfi (flash_info_t * info, struct cfi_qry *qry)
  1614. {
  1615. debug ("flash detect cfi\n");
  1616. for (info->portwidth = CONFIG_SYS_FLASH_CFI_WIDTH;
  1617. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  1618. for (info->chipwidth = FLASH_CFI_BY8;
  1619. info->chipwidth <= info->portwidth;
  1620. info->chipwidth <<= 1)
  1621. if (__flash_detect_cfi(info, qry))
  1622. return 1;
  1623. }
  1624. debug ("not found\n");
  1625. return 0;
  1626. }
  1627. /*
  1628. * Manufacturer-specific quirks. Add workarounds for geometry
  1629. * reversal, etc. here.
  1630. */
  1631. static void flash_fixup_amd(flash_info_t *info, struct cfi_qry *qry)
  1632. {
  1633. /* check if flash geometry needs reversal */
  1634. if (qry->num_erase_regions > 1) {
  1635. /* reverse geometry if top boot part */
  1636. if (info->cfi_version < 0x3131) {
  1637. /* CFI < 1.1, try to guess from device id */
  1638. if ((info->device_id & 0x80) != 0)
  1639. cfi_reverse_geometry(qry);
  1640. } else if (flash_read_uchar(info, info->ext_addr + 0xf) == 3) {
  1641. /* CFI >= 1.1, deduct from top/bottom flag */
  1642. /* note: ext_addr is valid since cfi_version > 0 */
  1643. cfi_reverse_geometry(qry);
  1644. }
  1645. }
  1646. }
  1647. static void flash_fixup_atmel(flash_info_t *info, struct cfi_qry *qry)
  1648. {
  1649. int reverse_geometry = 0;
  1650. /* Check the "top boot" bit in the PRI */
  1651. if (info->ext_addr && !(flash_read_uchar(info, info->ext_addr + 6) & 1))
  1652. reverse_geometry = 1;
  1653. /* AT49BV6416(T) list the erase regions in the wrong order.
  1654. * However, the device ID is identical with the non-broken
  1655. * AT49BV642D they differ in the high byte.
  1656. */
  1657. if (info->device_id == 0xd6 || info->device_id == 0xd2)
  1658. reverse_geometry = !reverse_geometry;
  1659. if (reverse_geometry)
  1660. cfi_reverse_geometry(qry);
  1661. }
  1662. static void flash_fixup_stm(flash_info_t *info, struct cfi_qry *qry)
  1663. {
  1664. /* check if flash geometry needs reversal */
  1665. if (qry->num_erase_regions > 1) {
  1666. /* reverse geometry if top boot part */
  1667. if (info->cfi_version < 0x3131) {
  1668. /* CFI < 1.1, guess by device id (M29W320{DT,ET} only) */
  1669. if (info->device_id == 0x22CA ||
  1670. info->device_id == 0x2256) {
  1671. cfi_reverse_geometry(qry);
  1672. }
  1673. }
  1674. }
  1675. }
  1676. /*
  1677. * The following code cannot be run from FLASH!
  1678. *
  1679. */
  1680. ulong flash_get_size (phys_addr_t base, int banknum)
  1681. {
  1682. flash_info_t *info = &flash_info[banknum];
  1683. int i, j;
  1684. flash_sect_t sect_cnt;
  1685. phys_addr_t sector;
  1686. unsigned long tmp;
  1687. int size_ratio;
  1688. uchar num_erase_regions;
  1689. int erase_region_size;
  1690. int erase_region_count;
  1691. struct cfi_qry qry;
  1692. unsigned long max_size;
  1693. memset(&qry, 0, sizeof(qry));
  1694. info->ext_addr = 0;
  1695. info->cfi_version = 0;
  1696. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1697. info->legacy_unlock = 0;
  1698. #endif
  1699. info->start[0] = (ulong)map_physmem(base, info->portwidth, MAP_NOCACHE);
  1700. if (flash_detect_cfi (info, &qry)) {
  1701. info->vendor = le16_to_cpu(qry.p_id);
  1702. info->ext_addr = le16_to_cpu(qry.p_adr);
  1703. num_erase_regions = qry.num_erase_regions;
  1704. if (info->ext_addr) {
  1705. info->cfi_version = (ushort) flash_read_uchar (info,
  1706. info->ext_addr + 3) << 8;
  1707. info->cfi_version |= (ushort) flash_read_uchar (info,
  1708. info->ext_addr + 4);
  1709. }
  1710. #ifdef DEBUG
  1711. flash_printqry (&qry);
  1712. #endif
  1713. switch (info->vendor) {
  1714. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1715. case CFI_CMDSET_INTEL_STANDARD:
  1716. case CFI_CMDSET_INTEL_EXTENDED:
  1717. cmdset_intel_init(info, &qry);
  1718. break;
  1719. case CFI_CMDSET_AMD_STANDARD:
  1720. case CFI_CMDSET_AMD_EXTENDED:
  1721. cmdset_amd_init(info, &qry);
  1722. break;
  1723. default:
  1724. printf("CFI: Unknown command set 0x%x\n",
  1725. info->vendor);
  1726. /*
  1727. * Unfortunately, this means we don't know how
  1728. * to get the chip back to Read mode. Might
  1729. * as well try an Intel-style reset...
  1730. */
  1731. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  1732. return 0;
  1733. }
  1734. /* Do manufacturer-specific fixups */
  1735. switch (info->manufacturer_id) {
  1736. case 0x0001:
  1737. flash_fixup_amd(info, &qry);
  1738. break;
  1739. case 0x001f:
  1740. flash_fixup_atmel(info, &qry);
  1741. break;
  1742. case 0x0020:
  1743. flash_fixup_stm(info, &qry);
  1744. break;
  1745. }
  1746. debug ("manufacturer is %d\n", info->vendor);
  1747. debug ("manufacturer id is 0x%x\n", info->manufacturer_id);
  1748. debug ("device id is 0x%x\n", info->device_id);
  1749. debug ("device id2 is 0x%x\n", info->device_id2);
  1750. debug ("cfi version is 0x%04x\n", info->cfi_version);
  1751. size_ratio = info->portwidth / info->chipwidth;
  1752. /* if the chip is x8/x16 reduce the ratio by half */
  1753. if ((info->interface == FLASH_CFI_X8X16)
  1754. && (info->chipwidth == FLASH_CFI_BY8)) {
  1755. size_ratio >>= 1;
  1756. }
  1757. debug ("size_ratio %d port %d bits chip %d bits\n",
  1758. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  1759. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  1760. info->size = 1 << qry.dev_size;
  1761. /* multiply the size by the number of chips */
  1762. info->size *= size_ratio;
  1763. max_size = cfi_flash_bank_size(banknum);
  1764. if (max_size && (info->size > max_size)) {
  1765. debug("[truncated from %ldMiB]", info->size >> 20);
  1766. info->size = max_size;
  1767. }
  1768. debug ("found %d erase regions\n", num_erase_regions);
  1769. sect_cnt = 0;
  1770. sector = base;
  1771. for (i = 0; i < num_erase_regions; i++) {
  1772. if (i > NUM_ERASE_REGIONS) {
  1773. printf ("%d erase regions found, only %d used\n",
  1774. num_erase_regions, NUM_ERASE_REGIONS);
  1775. break;
  1776. }
  1777. tmp = le32_to_cpu(qry.erase_region_info[i]);
  1778. debug("erase region %u: 0x%08lx\n", i, tmp);
  1779. erase_region_count = (tmp & 0xffff) + 1;
  1780. tmp >>= 16;
  1781. erase_region_size =
  1782. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  1783. debug ("erase_region_count = %d erase_region_size = %d\n",
  1784. erase_region_count, erase_region_size);
  1785. for (j = 0; j < erase_region_count; j++) {
  1786. if (sector - base >= info->size)
  1787. break;
  1788. if (sect_cnt >= CONFIG_SYS_MAX_FLASH_SECT) {
  1789. printf("ERROR: too many flash sectors\n");
  1790. break;
  1791. }
  1792. info->start[sect_cnt] =
  1793. (ulong)map_physmem(sector,
  1794. info->portwidth,
  1795. MAP_NOCACHE);
  1796. sector += (erase_region_size * size_ratio);
  1797. /*
  1798. * Only read protection status from
  1799. * supported devices (intel...)
  1800. */
  1801. switch (info->vendor) {
  1802. case CFI_CMDSET_INTEL_PROG_REGIONS:
  1803. case CFI_CMDSET_INTEL_EXTENDED:
  1804. case CFI_CMDSET_INTEL_STANDARD:
  1805. /*
  1806. * Set flash to read-id mode. Otherwise
  1807. * reading protected status is not
  1808. * guaranteed.
  1809. */
  1810. flash_write_cmd(info, sect_cnt, 0,
  1811. FLASH_CMD_READ_ID);
  1812. info->protect[sect_cnt] =
  1813. flash_isset (info, sect_cnt,
  1814. FLASH_OFFSET_PROTECT,
  1815. FLASH_STATUS_PROTECT);
  1816. break;
  1817. default:
  1818. /* default: not protected */
  1819. info->protect[sect_cnt] = 0;
  1820. }
  1821. sect_cnt++;
  1822. }
  1823. }
  1824. info->sector_count = sect_cnt;
  1825. info->buffer_size = 1 << le16_to_cpu(qry.max_buf_write_size);
  1826. tmp = 1 << qry.block_erase_timeout_typ;
  1827. info->erase_blk_tout = tmp *
  1828. (1 << qry.block_erase_timeout_max);
  1829. tmp = (1 << qry.buf_write_timeout_typ) *
  1830. (1 << qry.buf_write_timeout_max);
  1831. /* round up when converting to ms */
  1832. info->buffer_write_tout = (tmp + 999) / 1000;
  1833. tmp = (1 << qry.word_write_timeout_typ) *
  1834. (1 << qry.word_write_timeout_max);
  1835. /* round up when converting to ms */
  1836. info->write_tout = (tmp + 999) / 1000;
  1837. info->flash_id = FLASH_MAN_CFI;
  1838. if ((info->interface == FLASH_CFI_X8X16) &&
  1839. (info->chipwidth == FLASH_CFI_BY8)) {
  1840. /* XXX - Need to test on x8/x16 in parallel. */
  1841. info->portwidth >>= 1;
  1842. }
  1843. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1844. }
  1845. return (info->size);
  1846. }
  1847. #ifdef CONFIG_FLASH_CFI_MTD
  1848. void flash_set_verbose(uint v)
  1849. {
  1850. flash_verbose = v;
  1851. }
  1852. #endif
  1853. static void cfi_flash_set_config_reg(u32 base, u16 val)
  1854. {
  1855. #ifdef CONFIG_SYS_CFI_FLASH_CONFIG_REGS
  1856. /*
  1857. * Only set this config register if really defined
  1858. * to a valid value (0xffff is invalid)
  1859. */
  1860. if (val == 0xffff)
  1861. return;
  1862. /*
  1863. * Set configuration register. Data is "encrypted" in the 16 lower
  1864. * address bits.
  1865. */
  1866. flash_write16(FLASH_CMD_SETUP, (void *)(base + (val << 1)));
  1867. flash_write16(FLASH_CMD_SET_CR_CONFIRM, (void *)(base + (val << 1)));
  1868. /*
  1869. * Finally issue reset-command to bring device back to
  1870. * read-array mode
  1871. */
  1872. flash_write16(FLASH_CMD_RESET, (void *)base);
  1873. #endif
  1874. }
  1875. /*-----------------------------------------------------------------------
  1876. */
  1877. unsigned long flash_init (void)
  1878. {
  1879. unsigned long size = 0;
  1880. int i;
  1881. #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
  1882. struct apl_s {
  1883. ulong start;
  1884. ulong size;
  1885. } apl[] = CONFIG_SYS_FLASH_AUTOPROTECT_LIST;
  1886. #endif
  1887. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1888. /* read environment from EEPROM */
  1889. char s[64];
  1890. getenv_f("unlock", s, sizeof(s));
  1891. #endif
  1892. /* Init: no FLASHes known */
  1893. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  1894. flash_info[i].flash_id = FLASH_UNKNOWN;
  1895. /* Optionally write flash configuration register */
  1896. cfi_flash_set_config_reg(cfi_flash_bank_addr(i),
  1897. cfi_flash_config_reg(i));
  1898. if (!flash_detect_legacy(cfi_flash_bank_addr(i), i))
  1899. flash_get_size(cfi_flash_bank_addr(i), i);
  1900. size += flash_info[i].size;
  1901. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  1902. #ifndef CONFIG_SYS_FLASH_QUIET_TEST
  1903. printf ("## Unknown flash on Bank %d "
  1904. "- Size = 0x%08lx = %ld MB\n",
  1905. i+1, flash_info[i].size,
  1906. flash_info[i].size >> 20);
  1907. #endif /* CONFIG_SYS_FLASH_QUIET_TEST */
  1908. }
  1909. #ifdef CONFIG_SYS_FLASH_PROTECTION
  1910. else if ((s != NULL) && (strcmp(s, "yes") == 0)) {
  1911. /*
  1912. * Only the U-Boot image and it's environment
  1913. * is protected, all other sectors are
  1914. * unprotected (unlocked) if flash hardware
  1915. * protection is used (CONFIG_SYS_FLASH_PROTECTION)
  1916. * and the environment variable "unlock" is
  1917. * set to "yes".
  1918. */
  1919. if (flash_info[i].legacy_unlock) {
  1920. int k;
  1921. /*
  1922. * Disable legacy_unlock temporarily,
  1923. * since flash_real_protect would
  1924. * relock all other sectors again
  1925. * otherwise.
  1926. */
  1927. flash_info[i].legacy_unlock = 0;
  1928. /*
  1929. * Legacy unlocking (e.g. Intel J3) ->
  1930. * unlock only one sector. This will
  1931. * unlock all sectors.
  1932. */
  1933. flash_real_protect (&flash_info[i], 0, 0);
  1934. flash_info[i].legacy_unlock = 1;
  1935. /*
  1936. * Manually mark other sectors as
  1937. * unlocked (unprotected)
  1938. */
  1939. for (k = 1; k < flash_info[i].sector_count; k++)
  1940. flash_info[i].protect[k] = 0;
  1941. } else {
  1942. /*
  1943. * No legancy unlocking -> unlock all sectors
  1944. */
  1945. flash_protect (FLAG_PROTECT_CLEAR,
  1946. flash_info[i].start[0],
  1947. flash_info[i].start[0]
  1948. + flash_info[i].size - 1,
  1949. &flash_info[i]);
  1950. }
  1951. }
  1952. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  1953. }
  1954. /* Monitor protection ON by default */
  1955. #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) && \
  1956. (!defined(CONFIG_MONITOR_IS_IN_RAM))
  1957. flash_protect (FLAG_PROTECT_SET,
  1958. CONFIG_SYS_MONITOR_BASE,
  1959. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  1960. flash_get_info(CONFIG_SYS_MONITOR_BASE));
  1961. #endif
  1962. /* Environment protection ON by default */
  1963. #ifdef CONFIG_ENV_IS_IN_FLASH
  1964. flash_protect (FLAG_PROTECT_SET,
  1965. CONFIG_ENV_ADDR,
  1966. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  1967. flash_get_info(CONFIG_ENV_ADDR));
  1968. #endif
  1969. /* Redundant environment protection ON by default */
  1970. #ifdef CONFIG_ENV_ADDR_REDUND
  1971. flash_protect (FLAG_PROTECT_SET,
  1972. CONFIG_ENV_ADDR_REDUND,
  1973. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
  1974. flash_get_info(CONFIG_ENV_ADDR_REDUND));
  1975. #endif
  1976. #if defined(CONFIG_SYS_FLASH_AUTOPROTECT_LIST)
  1977. for (i = 0; i < (sizeof(apl) / sizeof(struct apl_s)); i++) {
  1978. debug("autoprotecting from %08x to %08x\n",
  1979. apl[i].start, apl[i].start + apl[i].size - 1);
  1980. flash_protect (FLAG_PROTECT_SET,
  1981. apl[i].start,
  1982. apl[i].start + apl[i].size - 1,
  1983. flash_get_info(apl[i].start));
  1984. }
  1985. #endif
  1986. #ifdef CONFIG_FLASH_CFI_MTD
  1987. cfi_mtd_init();
  1988. #endif
  1989. return (size);
  1990. }