cfi_flash.c 58 KB

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