cfi_flash.c 49 KB

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