cfi_flash.c 63 KB

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