cfi_flash.c 58 KB

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