cfi_flash.c 64 KB

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