cfi_flash.c 54 KB

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