cfi_flash.c 54 KB

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