cfi_flash.c 52 KB

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