cfi_flash.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. * Modified to work with AMD flashes
  8. *
  9. * Copyright (C) 2004
  10. * Ed Okerson
  11. * Modified to work with little-endian systems.
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. *
  31. * History
  32. * 01/20/2004 - combined variants of original driver.
  33. * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
  34. * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
  35. * 01/27/2004 - Little endian support Ed Okerson
  36. *
  37. * Tested Architectures
  38. * Port Width Chip Width # of banks Flash Chip Board
  39. * 32 16 1 28F128J3 seranoa/eagle
  40. * 64 16 1 28F128J3 seranoa/falcon
  41. *
  42. */
  43. /* The DEBUG define must be before common to enable debugging */
  44. /* #define DEBUG */
  45. #include <common.h>
  46. #include <asm/processor.h>
  47. #include <asm/byteorder.h>
  48. #include <environment.h>
  49. #ifdef CFG_FLASH_CFI_DRIVER
  50. /*
  51. * This file implements a Common Flash Interface (CFI) driver for U-Boot.
  52. * The width of the port and the width of the chips are determined at initialization.
  53. * These widths are used to calculate the address for access CFI data structures.
  54. * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
  55. *
  56. * References
  57. * JEDEC Standard JESD68 - Common Flash Interface (CFI)
  58. * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
  59. * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
  60. * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
  61. *
  62. * TODO
  63. *
  64. * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
  65. * Table (ALT) to determine if protection is available
  66. *
  67. * Add support for other command sets Use the PRI and ALT to determine command set
  68. * Verify erase and program timeouts.
  69. */
  70. #ifndef CFG_FLASH_BANKS_LIST
  71. #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
  72. #endif
  73. #define FLASH_CMD_CFI 0x98
  74. #define FLASH_CMD_READ_ID 0x90
  75. #define FLASH_CMD_RESET 0xff
  76. #define FLASH_CMD_BLOCK_ERASE 0x20
  77. #define FLASH_CMD_ERASE_CONFIRM 0xD0
  78. #define FLASH_CMD_WRITE 0x40
  79. #define FLASH_CMD_PROTECT 0x60
  80. #define FLASH_CMD_PROTECT_SET 0x01
  81. #define FLASH_CMD_PROTECT_CLEAR 0xD0
  82. #define FLASH_CMD_CLEAR_STATUS 0x50
  83. #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
  84. #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
  85. #define FLASH_STATUS_DONE 0x80
  86. #define FLASH_STATUS_ESS 0x40
  87. #define FLASH_STATUS_ECLBS 0x20
  88. #define FLASH_STATUS_PSLBS 0x10
  89. #define FLASH_STATUS_VPENS 0x08
  90. #define FLASH_STATUS_PSS 0x04
  91. #define FLASH_STATUS_DPS 0x02
  92. #define FLASH_STATUS_R 0x01
  93. #define FLASH_STATUS_PROTECT 0x01
  94. #define AMD_CMD_RESET 0xF0
  95. #define AMD_CMD_WRITE 0xA0
  96. #define AMD_CMD_ERASE_START 0x80
  97. #define AMD_CMD_ERASE_SECTOR 0x30
  98. #define AMD_CMD_UNLOCK_START 0xAA
  99. #define AMD_CMD_UNLOCK_ACK 0x55
  100. #define AMD_CMD_WRITE_TO_BUFFER 0x25
  101. #define AMD_CMD_WRITE_BUFFER_CONFIRM 0x29
  102. #define AMD_STATUS_TOGGLE 0x40
  103. #define AMD_STATUS_ERROR 0x20
  104. #define AMD_ADDR_ERASE_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
  105. #define AMD_ADDR_START ((info->portwidth == FLASH_CFI_8BIT) ? 0xAAA : 0x555)
  106. #define AMD_ADDR_ACK ((info->portwidth == FLASH_CFI_8BIT) ? 0x555 : 0x2AA)
  107. #define FLASH_OFFSET_CFI 0x55
  108. #define FLASH_OFFSET_CFI_RESP 0x10
  109. #define FLASH_OFFSET_PRIMARY_VENDOR 0x13
  110. #define FLASH_OFFSET_WTOUT 0x1F
  111. #define FLASH_OFFSET_WBTOUT 0x20
  112. #define FLASH_OFFSET_ETOUT 0x21
  113. #define FLASH_OFFSET_CETOUT 0x22
  114. #define FLASH_OFFSET_WMAX_TOUT 0x23
  115. #define FLASH_OFFSET_WBMAX_TOUT 0x24
  116. #define FLASH_OFFSET_EMAX_TOUT 0x25
  117. #define FLASH_OFFSET_CEMAX_TOUT 0x26
  118. #define FLASH_OFFSET_SIZE 0x27
  119. #define FLASH_OFFSET_INTERFACE 0x28
  120. #define FLASH_OFFSET_BUFFER_SIZE 0x2A
  121. #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
  122. #define FLASH_OFFSET_ERASE_REGIONS 0x2D
  123. #define FLASH_OFFSET_PROTECT 0x02
  124. #define FLASH_OFFSET_USER_PROTECTION 0x85
  125. #define FLASH_OFFSET_INTEL_PROTECTION 0x81
  126. #define FLASH_MAN_CFI 0x01000000
  127. #define CFI_CMDSET_NONE 0
  128. #define CFI_CMDSET_INTEL_EXTENDED 1
  129. #define CFI_CMDSET_AMD_STANDARD 2
  130. #define CFI_CMDSET_INTEL_STANDARD 3
  131. #define CFI_CMDSET_AMD_EXTENDED 4
  132. #define CFI_CMDSET_MITSU_STANDARD 256
  133. #define CFI_CMDSET_MITSU_EXTENDED 257
  134. #define CFI_CMDSET_SST 258
  135. #ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
  136. # undef FLASH_CMD_RESET
  137. # define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
  138. #endif
  139. typedef union {
  140. unsigned char c;
  141. unsigned short w;
  142. unsigned long l;
  143. unsigned long long ll;
  144. } cfiword_t;
  145. typedef union {
  146. volatile unsigned char *cp;
  147. volatile unsigned short *wp;
  148. volatile unsigned long *lp;
  149. volatile unsigned long long *llp;
  150. } cfiptr_t;
  151. #define NUM_ERASE_REGIONS 4
  152. /* use CFG_MAX_FLASH_BANKS_DETECT if defined */
  153. #ifdef CFG_MAX_FLASH_BANKS_DETECT
  154. static ulong bank_base[CFG_MAX_FLASH_BANKS_DETECT] = CFG_FLASH_BANKS_LIST;
  155. flash_info_t flash_info[CFG_MAX_FLASH_BANKS_DETECT]; /* FLASH chips info */
  156. #else
  157. static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
  158. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* FLASH chips info */
  159. #endif
  160. /*
  161. * Check if chip width is defined. If not, start detecting with 8bit.
  162. */
  163. #ifndef CFG_FLASH_CFI_WIDTH
  164. #define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
  165. #endif
  166. /*-----------------------------------------------------------------------
  167. * Functions
  168. */
  169. typedef unsigned long flash_sect_t;
  170. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
  171. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
  172. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  173. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
  174. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  175. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  176. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
  177. static int flash_detect_cfi (flash_info_t * info);
  178. static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
  179. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  180. ulong tout, char *prompt);
  181. #if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  182. static flash_info_t *flash_get_info(ulong base);
  183. #endif
  184. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  185. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
  186. #endif
  187. /*-----------------------------------------------------------------------
  188. * create an address based on the offset and the port width
  189. */
  190. inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
  191. {
  192. return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
  193. }
  194. #ifdef DEBUG
  195. /*-----------------------------------------------------------------------
  196. * Debug support
  197. */
  198. void print_longlong (char *str, unsigned long long data)
  199. {
  200. int i;
  201. char *cp;
  202. cp = (unsigned char *) &data;
  203. for (i = 0; i < 8; i++)
  204. sprintf (&str[i * 2], "%2.2x", *cp++);
  205. }
  206. static void flash_printqry (flash_info_t * info, flash_sect_t sect)
  207. {
  208. cfiptr_t cptr;
  209. int x, y;
  210. for (x = 0; x < 0x40; x += 16U / info->portwidth) {
  211. cptr.cp =
  212. flash_make_addr (info, sect,
  213. x + FLASH_OFFSET_CFI_RESP);
  214. debug ("%p : ", cptr.cp);
  215. for (y = 0; y < 16; y++) {
  216. debug ("%2.2x ", cptr.cp[y]);
  217. }
  218. debug (" ");
  219. for (y = 0; y < 16; y++) {
  220. if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
  221. debug ("%c", cptr.cp[y]);
  222. } else {
  223. debug (".");
  224. }
  225. }
  226. debug ("\n");
  227. }
  228. }
  229. #endif
  230. /*-----------------------------------------------------------------------
  231. * read a character at a port width address
  232. */
  233. inline uchar flash_read_uchar (flash_info_t * info, uint offset)
  234. {
  235. uchar *cp;
  236. cp = flash_make_addr (info, 0, offset);
  237. #if defined(__LITTLE_ENDIAN)
  238. return (cp[0]);
  239. #else
  240. return (cp[info->portwidth - 1]);
  241. #endif
  242. }
  243. /*-----------------------------------------------------------------------
  244. * read a short word by swapping for ppc format.
  245. */
  246. ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
  247. {
  248. uchar *addr;
  249. ushort retval;
  250. #ifdef DEBUG
  251. int x;
  252. #endif
  253. addr = flash_make_addr (info, sect, offset);
  254. #ifdef DEBUG
  255. debug ("ushort addr is at %p info->portwidth = %d\n", addr,
  256. info->portwidth);
  257. for (x = 0; x < 2 * info->portwidth; x++) {
  258. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  259. }
  260. #endif
  261. #if defined(__LITTLE_ENDIAN)
  262. retval = ((addr[(info->portwidth)] << 8) | addr[0]);
  263. #else
  264. retval = ((addr[(2 * info->portwidth) - 1] << 8) |
  265. addr[info->portwidth - 1]);
  266. #endif
  267. debug ("retval = 0x%x\n", retval);
  268. return retval;
  269. }
  270. /*-----------------------------------------------------------------------
  271. * read a long word by picking the least significant byte of each maiximum
  272. * port size word. Swap for ppc format.
  273. */
  274. ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
  275. {
  276. uchar *addr;
  277. ulong retval;
  278. #ifdef DEBUG
  279. int x;
  280. #endif
  281. addr = flash_make_addr (info, sect, offset);
  282. #ifdef DEBUG
  283. debug ("long addr is at %p info->portwidth = %d\n", addr,
  284. info->portwidth);
  285. for (x = 0; x < 4 * info->portwidth; x++) {
  286. debug ("addr[%x] = 0x%x\n", x, addr[x]);
  287. }
  288. #endif
  289. #if defined(__LITTLE_ENDIAN)
  290. retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
  291. (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
  292. #else
  293. retval = (addr[(2 * info->portwidth) - 1] << 24) |
  294. (addr[(info->portwidth) - 1] << 16) |
  295. (addr[(4 * info->portwidth) - 1] << 8) |
  296. addr[(3 * info->portwidth) - 1];
  297. #endif
  298. return retval;
  299. }
  300. /*-----------------------------------------------------------------------
  301. */
  302. unsigned long flash_init (void)
  303. {
  304. unsigned long size = 0;
  305. int i;
  306. /* Init: no FLASHes known */
  307. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  308. flash_info[i].flash_id = FLASH_UNKNOWN;
  309. size += flash_info[i].size = flash_get_size (bank_base[i], i);
  310. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  311. #ifndef CFG_FLASH_QUIET_TEST
  312. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  313. i, flash_info[i].size, flash_info[i].size << 20);
  314. #endif /* CFG_FLASH_QUIET_TEST */
  315. }
  316. #ifdef CFG_FLASH_PROTECTION
  317. else {
  318. char *s = getenv("unlock");
  319. if (((s = getenv("unlock")) != NULL) && (strcmp(s, "yes") == 0)) {
  320. /*
  321. * Only the U-Boot image and it's environment is protected,
  322. * all other sectors are unprotected (unlocked) if flash
  323. * hardware protection is used (CFG_FLASH_PROTECTION) and
  324. * the environment variable "unlock" is set to "yes".
  325. */
  326. flash_protect (FLAG_PROTECT_CLEAR,
  327. flash_info[i].start[0],
  328. flash_info[i].start[0] + flash_info[i].size - 1,
  329. &flash_info[i]);
  330. }
  331. }
  332. #endif /* CFG_FLASH_PROTECTION */
  333. }
  334. /* Monitor protection ON by default */
  335. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  336. flash_protect (FLAG_PROTECT_SET,
  337. CFG_MONITOR_BASE,
  338. CFG_MONITOR_BASE + monitor_flash_len - 1,
  339. flash_get_info(CFG_MONITOR_BASE));
  340. #endif
  341. /* Environment protection ON by default */
  342. #ifdef CFG_ENV_IS_IN_FLASH
  343. flash_protect (FLAG_PROTECT_SET,
  344. CFG_ENV_ADDR,
  345. CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
  346. flash_get_info(CFG_ENV_ADDR));
  347. #endif
  348. /* Redundant environment protection ON by default */
  349. #ifdef CFG_ENV_ADDR_REDUND
  350. flash_protect (FLAG_PROTECT_SET,
  351. CFG_ENV_ADDR_REDUND,
  352. CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
  353. flash_get_info(CFG_ENV_ADDR_REDUND));
  354. #endif
  355. return (size);
  356. }
  357. /*-----------------------------------------------------------------------
  358. */
  359. #if defined(CFG_ENV_IS_IN_FLASH) || defined(CFG_ENV_ADDR_REDUND) || (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  360. static flash_info_t *flash_get_info(ulong base)
  361. {
  362. int i;
  363. flash_info_t * info = 0;
  364. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  365. info = & flash_info[i];
  366. if (info->size && info->start[0] <= base &&
  367. base <= info->start[0] + info->size - 1)
  368. break;
  369. }
  370. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  371. }
  372. #endif
  373. /*-----------------------------------------------------------------------
  374. */
  375. int flash_erase (flash_info_t * info, int s_first, int s_last)
  376. {
  377. int rcode = 0;
  378. int prot;
  379. flash_sect_t sect;
  380. if (info->flash_id != FLASH_MAN_CFI) {
  381. puts ("Can't erase unknown flash type - aborted\n");
  382. return 1;
  383. }
  384. if ((s_first < 0) || (s_first > s_last)) {
  385. puts ("- no sectors to erase\n");
  386. return 1;
  387. }
  388. prot = 0;
  389. for (sect = s_first; sect <= s_last; ++sect) {
  390. if (info->protect[sect]) {
  391. prot++;
  392. }
  393. }
  394. if (prot) {
  395. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  396. } else {
  397. putc ('\n');
  398. }
  399. for (sect = s_first; sect <= s_last; sect++) {
  400. if (info->protect[sect] == 0) { /* not protected */
  401. switch (info->vendor) {
  402. case CFI_CMDSET_INTEL_STANDARD:
  403. case CFI_CMDSET_INTEL_EXTENDED:
  404. flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  405. flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  406. flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  407. break;
  408. case CFI_CMDSET_AMD_STANDARD:
  409. case CFI_CMDSET_AMD_EXTENDED:
  410. flash_unlock_seq (info, sect);
  411. flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
  412. AMD_CMD_ERASE_START);
  413. flash_unlock_seq (info, sect);
  414. flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
  415. break;
  416. default:
  417. debug ("Unkown flash vendor %d\n",
  418. info->vendor);
  419. break;
  420. }
  421. if (flash_full_status_check
  422. (info, sect, info->erase_blk_tout, "erase")) {
  423. rcode = 1;
  424. } else
  425. putc ('.');
  426. }
  427. }
  428. puts (" done\n");
  429. return rcode;
  430. }
  431. /*-----------------------------------------------------------------------
  432. */
  433. void flash_print_info (flash_info_t * info)
  434. {
  435. int i;
  436. if (info->flash_id != FLASH_MAN_CFI) {
  437. puts ("missing or unknown FLASH type\n");
  438. return;
  439. }
  440. printf ("CFI conformant FLASH (%d x %d)",
  441. (info->portwidth << 3), (info->chipwidth << 3));
  442. printf (" Size: %ld MB in %d Sectors\n",
  443. info->size >> 20, info->sector_count);
  444. printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
  445. info->erase_blk_tout,
  446. info->write_tout,
  447. info->buffer_write_tout,
  448. info->buffer_size);
  449. puts (" Sector Start Addresses:");
  450. for (i = 0; i < info->sector_count; ++i) {
  451. #ifdef CFG_FLASH_EMPTY_INFO
  452. int k;
  453. int size;
  454. int erased;
  455. volatile unsigned long *flash;
  456. /*
  457. * Check if whole sector is erased
  458. */
  459. if (i != (info->sector_count - 1))
  460. size = info->start[i + 1] - info->start[i];
  461. else
  462. size = info->start[0] + info->size - info->start[i];
  463. erased = 1;
  464. flash = (volatile unsigned long *) info->start[i];
  465. size = size >> 2; /* divide by 4 for longword access */
  466. for (k = 0; k < size; k++) {
  467. if (*flash++ != 0xffffffff) {
  468. erased = 0;
  469. break;
  470. }
  471. }
  472. if ((i % 5) == 0)
  473. printf ("\n");
  474. /* print empty and read-only info */
  475. printf (" %08lX%s%s",
  476. info->start[i],
  477. erased ? " E" : " ",
  478. info->protect[i] ? "RO " : " ");
  479. #else /* ! CFG_FLASH_EMPTY_INFO */
  480. if ((i % 5) == 0)
  481. printf ("\n ");
  482. printf (" %08lX%s",
  483. info->start[i], info->protect[i] ? " (RO)" : " ");
  484. #endif
  485. }
  486. putc ('\n');
  487. return;
  488. }
  489. /*-----------------------------------------------------------------------
  490. * Copy memory to flash, returns:
  491. * 0 - OK
  492. * 1 - write timeout
  493. * 2 - Flash not erased
  494. */
  495. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  496. {
  497. ulong wp;
  498. ulong cp;
  499. int aln;
  500. cfiword_t cword;
  501. int i, rc;
  502. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  503. int buffered_size;
  504. #endif
  505. /* get lower aligned address */
  506. /* get lower aligned address */
  507. wp = (addr & ~(info->portwidth - 1));
  508. /* handle unaligned start */
  509. if ((aln = addr - wp) != 0) {
  510. cword.l = 0;
  511. cp = wp;
  512. for (i = 0; i < aln; ++i, ++cp)
  513. flash_add_byte (info, &cword, (*(uchar *) cp));
  514. for (; (i < info->portwidth) && (cnt > 0); i++) {
  515. flash_add_byte (info, &cword, *src++);
  516. cnt--;
  517. cp++;
  518. }
  519. for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  520. flash_add_byte (info, &cword, (*(uchar *) cp));
  521. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  522. return rc;
  523. wp = cp;
  524. }
  525. /* handle the aligned part */
  526. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  527. buffered_size = (info->portwidth / info->chipwidth);
  528. buffered_size *= info->buffer_size;
  529. while (cnt >= info->portwidth) {
  530. /* prohibit buffer write when buffer_size is 1 */
  531. if (info->buffer_size == 1) {
  532. cword.l = 0;
  533. for (i = 0; i < info->portwidth; i++)
  534. flash_add_byte (info, &cword, *src++);
  535. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  536. return rc;
  537. wp += info->portwidth;
  538. cnt -= info->portwidth;
  539. continue;
  540. }
  541. /* write buffer until next buffered_size aligned boundary */
  542. i = buffered_size - (wp % buffered_size);
  543. if (i > cnt)
  544. i = cnt;
  545. if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
  546. return rc;
  547. i -= i & (info->portwidth - 1);
  548. wp += i;
  549. src += i;
  550. cnt -= i;
  551. }
  552. #else
  553. while (cnt >= info->portwidth) {
  554. cword.l = 0;
  555. for (i = 0; i < info->portwidth; i++) {
  556. flash_add_byte (info, &cword, *src++);
  557. }
  558. if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
  559. return rc;
  560. wp += info->portwidth;
  561. cnt -= info->portwidth;
  562. }
  563. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  564. if (cnt == 0) {
  565. return (0);
  566. }
  567. /*
  568. * handle unaligned tail bytes
  569. */
  570. cword.l = 0;
  571. for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
  572. flash_add_byte (info, &cword, *src++);
  573. --cnt;
  574. }
  575. for (; i < info->portwidth; ++i, ++cp) {
  576. flash_add_byte (info, &cword, (*(uchar *) cp));
  577. }
  578. return flash_write_cfiword (info, wp, cword);
  579. }
  580. /*-----------------------------------------------------------------------
  581. */
  582. #ifdef CFG_FLASH_PROTECTION
  583. int flash_real_protect (flash_info_t * info, long sector, int prot)
  584. {
  585. int retcode = 0;
  586. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  587. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
  588. if (prot)
  589. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
  590. else
  591. flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  592. if ((retcode =
  593. flash_full_status_check (info, sector, info->erase_blk_tout,
  594. prot ? "protect" : "unprotect")) == 0) {
  595. info->protect[sector] = prot;
  596. /* Intel's unprotect unprotects all locking */
  597. if (prot == 0) {
  598. flash_sect_t i;
  599. for (i = 0; i < info->sector_count; i++) {
  600. if (info->protect[i])
  601. flash_real_protect (info, i, 1);
  602. }
  603. }
  604. }
  605. return retcode;
  606. }
  607. /*-----------------------------------------------------------------------
  608. * flash_read_user_serial - read the OneTimeProgramming cells
  609. */
  610. void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
  611. int len)
  612. {
  613. uchar *src;
  614. uchar *dst;
  615. dst = buffer;
  616. src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
  617. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  618. memcpy (dst, src + offset, len);
  619. flash_write_cmd (info, 0, 0, info->cmd_reset);
  620. }
  621. /*
  622. * flash_read_factory_serial - read the device Id from the protection area
  623. */
  624. void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
  625. int len)
  626. {
  627. uchar *src;
  628. src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
  629. flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
  630. memcpy (buffer, src + offset, len);
  631. flash_write_cmd (info, 0, 0, info->cmd_reset);
  632. }
  633. #endif /* CFG_FLASH_PROTECTION */
  634. /*
  635. * flash_is_busy - check to see if the flash is busy
  636. * This routine checks the status of the chip and returns true if the chip is busy
  637. */
  638. static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
  639. {
  640. int retval;
  641. switch (info->vendor) {
  642. case CFI_CMDSET_INTEL_STANDARD:
  643. case CFI_CMDSET_INTEL_EXTENDED:
  644. retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
  645. break;
  646. case CFI_CMDSET_AMD_STANDARD:
  647. case CFI_CMDSET_AMD_EXTENDED:
  648. retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
  649. break;
  650. default:
  651. retval = 0;
  652. }
  653. debug ("flash_is_busy: %d\n", retval);
  654. return retval;
  655. }
  656. /*-----------------------------------------------------------------------
  657. * wait for XSR.7 to be set. Time out with an error if it does not.
  658. * This routine does not set the flash to read-array mode.
  659. */
  660. static int flash_status_check (flash_info_t * info, flash_sect_t sector,
  661. ulong tout, char *prompt)
  662. {
  663. ulong start;
  664. /* Wait for command completion */
  665. start = get_timer (0);
  666. while (flash_is_busy (info, sector)) {
  667. if (get_timer (start) > tout) {
  668. printf ("Flash %s timeout at address %lx data %lx\n",
  669. prompt, info->start[sector],
  670. flash_read_long (info, sector, 0));
  671. flash_write_cmd (info, sector, 0, info->cmd_reset);
  672. return ERR_TIMOUT;
  673. }
  674. }
  675. return ERR_OK;
  676. }
  677. /*-----------------------------------------------------------------------
  678. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  679. * This routine sets the flash to read-array mode.
  680. */
  681. static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
  682. ulong tout, char *prompt)
  683. {
  684. int retcode;
  685. retcode = flash_status_check (info, sector, tout, prompt);
  686. switch (info->vendor) {
  687. case CFI_CMDSET_INTEL_EXTENDED:
  688. case CFI_CMDSET_INTEL_STANDARD:
  689. if ((retcode == ERR_OK)
  690. && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
  691. retcode = ERR_INVAL;
  692. printf ("Flash %s error at address %lx\n", prompt,
  693. info->start[sector]);
  694. if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
  695. puts ("Command Sequence Error.\n");
  696. } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
  697. puts ("Block Erase Error.\n");
  698. retcode = ERR_NOT_ERASED;
  699. } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
  700. puts ("Locking Error\n");
  701. }
  702. if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
  703. puts ("Block locked.\n");
  704. retcode = ERR_PROTECTED;
  705. }
  706. if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
  707. puts ("Vpp Low Error.\n");
  708. }
  709. flash_write_cmd (info, sector, 0, info->cmd_reset);
  710. break;
  711. default:
  712. break;
  713. }
  714. return retcode;
  715. }
  716. /*-----------------------------------------------------------------------
  717. */
  718. static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
  719. {
  720. #if defined(__LITTLE_ENDIAN)
  721. unsigned short w;
  722. unsigned int l;
  723. unsigned long long ll;
  724. #endif
  725. switch (info->portwidth) {
  726. case FLASH_CFI_8BIT:
  727. cword->c = c;
  728. break;
  729. case FLASH_CFI_16BIT:
  730. #if defined(__LITTLE_ENDIAN)
  731. w = c;
  732. w <<= 8;
  733. cword->w = (cword->w >> 8) | w;
  734. #else
  735. cword->w = (cword->w << 8) | c;
  736. #endif
  737. break;
  738. case FLASH_CFI_32BIT:
  739. #if defined(__LITTLE_ENDIAN)
  740. l = c;
  741. l <<= 24;
  742. cword->l = (cword->l >> 8) | l;
  743. #else
  744. cword->l = (cword->l << 8) | c;
  745. #endif
  746. break;
  747. case FLASH_CFI_64BIT:
  748. #if defined(__LITTLE_ENDIAN)
  749. ll = c;
  750. ll <<= 56;
  751. cword->ll = (cword->ll >> 8) | ll;
  752. #else
  753. cword->ll = (cword->ll << 8) | c;
  754. #endif
  755. break;
  756. }
  757. }
  758. /*-----------------------------------------------------------------------
  759. * make a proper sized command based on the port and chip widths
  760. */
  761. static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
  762. {
  763. int i;
  764. uchar *cp = (uchar *) cmdbuf;
  765. #if defined(__LITTLE_ENDIAN)
  766. for (i = info->portwidth; i > 0; i--)
  767. #else
  768. for (i = 1; i <= info->portwidth; i++)
  769. #endif
  770. *cp++ = (i & (info->chipwidth - 1)) ? '\0' : cmd;
  771. }
  772. /*
  773. * Write a proper sized command to the correct address
  774. */
  775. static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  776. {
  777. volatile cfiptr_t addr;
  778. cfiword_t cword;
  779. addr.cp = flash_make_addr (info, sect, offset);
  780. flash_make_cmd (info, cmd, &cword);
  781. switch (info->portwidth) {
  782. case FLASH_CFI_8BIT:
  783. debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
  784. cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  785. *addr.cp = cword.c;
  786. break;
  787. case FLASH_CFI_16BIT:
  788. debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
  789. cmd, cword.w,
  790. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  791. *addr.wp = cword.w;
  792. break;
  793. case FLASH_CFI_32BIT:
  794. debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
  795. cmd, cword.l,
  796. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  797. *addr.lp = cword.l;
  798. break;
  799. case FLASH_CFI_64BIT:
  800. #ifdef DEBUG
  801. {
  802. char str[20];
  803. print_longlong (str, cword.ll);
  804. debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
  805. addr.llp, cmd, str,
  806. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  807. }
  808. #endif
  809. *addr.llp = cword.ll;
  810. break;
  811. }
  812. }
  813. static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
  814. {
  815. flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
  816. flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
  817. }
  818. /*-----------------------------------------------------------------------
  819. */
  820. static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  821. {
  822. cfiptr_t cptr;
  823. cfiword_t cword;
  824. int retval;
  825. cptr.cp = flash_make_addr (info, sect, offset);
  826. flash_make_cmd (info, cmd, &cword);
  827. debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
  828. switch (info->portwidth) {
  829. case FLASH_CFI_8BIT:
  830. debug ("is= %x %x\n", cptr.cp[0], cword.c);
  831. retval = (cptr.cp[0] == cword.c);
  832. break;
  833. case FLASH_CFI_16BIT:
  834. debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
  835. retval = (cptr.wp[0] == cword.w);
  836. break;
  837. case FLASH_CFI_32BIT:
  838. debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
  839. retval = (cptr.lp[0] == cword.l);
  840. break;
  841. case FLASH_CFI_64BIT:
  842. #ifdef DEBUG
  843. {
  844. char str1[20];
  845. char str2[20];
  846. print_longlong (str1, cptr.llp[0]);
  847. print_longlong (str2, cword.ll);
  848. debug ("is= %s %s\n", str1, str2);
  849. }
  850. #endif
  851. retval = (cptr.llp[0] == cword.ll);
  852. break;
  853. default:
  854. retval = 0;
  855. break;
  856. }
  857. return retval;
  858. }
  859. /*-----------------------------------------------------------------------
  860. */
  861. static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  862. {
  863. cfiptr_t cptr;
  864. cfiword_t cword;
  865. int retval;
  866. cptr.cp = flash_make_addr (info, sect, offset);
  867. flash_make_cmd (info, cmd, &cword);
  868. switch (info->portwidth) {
  869. case FLASH_CFI_8BIT:
  870. retval = ((cptr.cp[0] & cword.c) == cword.c);
  871. break;
  872. case FLASH_CFI_16BIT:
  873. retval = ((cptr.wp[0] & cword.w) == cword.w);
  874. break;
  875. case FLASH_CFI_32BIT:
  876. retval = ((cptr.lp[0] & cword.l) == cword.l);
  877. break;
  878. case FLASH_CFI_64BIT:
  879. retval = ((cptr.llp[0] & cword.ll) == cword.ll);
  880. break;
  881. default:
  882. retval = 0;
  883. break;
  884. }
  885. return retval;
  886. }
  887. /*-----------------------------------------------------------------------
  888. */
  889. static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
  890. {
  891. cfiptr_t cptr;
  892. cfiword_t cword;
  893. int retval;
  894. cptr.cp = flash_make_addr (info, sect, offset);
  895. flash_make_cmd (info, cmd, &cword);
  896. switch (info->portwidth) {
  897. case FLASH_CFI_8BIT:
  898. retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
  899. break;
  900. case FLASH_CFI_16BIT:
  901. retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
  902. break;
  903. case FLASH_CFI_32BIT:
  904. retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
  905. break;
  906. case FLASH_CFI_64BIT:
  907. retval = ((cptr.llp[0] & cword.ll) !=
  908. (cptr.llp[0] & cword.ll));
  909. break;
  910. default:
  911. retval = 0;
  912. break;
  913. }
  914. return retval;
  915. }
  916. /*-----------------------------------------------------------------------
  917. * detect if flash is compatible with the Common Flash Interface (CFI)
  918. * http://www.jedec.org/download/search/jesd68.pdf
  919. *
  920. */
  921. static int flash_detect_cfi (flash_info_t * info)
  922. {
  923. debug ("flash detect cfi\n");
  924. for (info->portwidth = CFG_FLASH_CFI_WIDTH;
  925. info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
  926. for (info->chipwidth = FLASH_CFI_BY8;
  927. info->chipwidth <= info->portwidth;
  928. info->chipwidth <<= 1) {
  929. flash_write_cmd (info, 0, 0, info->cmd_reset);
  930. flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  931. if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
  932. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
  933. && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
  934. info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
  935. debug ("device interface is %d\n",
  936. info->interface);
  937. debug ("found port %d chip %d ",
  938. info->portwidth, info->chipwidth);
  939. debug ("port %d bits chip %d bits\n",
  940. info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  941. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  942. return 1;
  943. }
  944. }
  945. }
  946. debug ("not found\n");
  947. return 0;
  948. }
  949. /*
  950. * The following code cannot be run from FLASH!
  951. *
  952. */
  953. ulong flash_get_size (ulong base, int banknum)
  954. {
  955. flash_info_t *info = &flash_info[banknum];
  956. int i, j;
  957. flash_sect_t sect_cnt;
  958. unsigned long sector;
  959. unsigned long tmp;
  960. int size_ratio;
  961. uchar num_erase_regions;
  962. int erase_region_size;
  963. int erase_region_count;
  964. info->start[0] = base;
  965. if (flash_detect_cfi (info)) {
  966. info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
  967. #ifdef DEBUG
  968. flash_printqry (info, 0);
  969. #endif
  970. switch (info->vendor) {
  971. case CFI_CMDSET_INTEL_STANDARD:
  972. case CFI_CMDSET_INTEL_EXTENDED:
  973. default:
  974. info->cmd_reset = FLASH_CMD_RESET;
  975. break;
  976. case CFI_CMDSET_AMD_STANDARD:
  977. case CFI_CMDSET_AMD_EXTENDED:
  978. info->cmd_reset = AMD_CMD_RESET;
  979. break;
  980. }
  981. debug ("manufacturer is %d\n", info->vendor);
  982. size_ratio = info->portwidth / info->chipwidth;
  983. /* if the chip is x8/x16 reduce the ratio by half */
  984. if ((info->interface == FLASH_CFI_X8X16)
  985. && (info->chipwidth == FLASH_CFI_BY8)) {
  986. size_ratio >>= 1;
  987. }
  988. num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  989. debug ("size_ratio %d port %d bits chip %d bits\n",
  990. size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
  991. info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
  992. debug ("found %d erase regions\n", num_erase_regions);
  993. sect_cnt = 0;
  994. sector = base;
  995. for (i = 0; i < num_erase_regions; i++) {
  996. if (i > NUM_ERASE_REGIONS) {
  997. printf ("%d erase regions found, only %d used\n",
  998. num_erase_regions, NUM_ERASE_REGIONS);
  999. break;
  1000. }
  1001. tmp = flash_read_long (info, 0,
  1002. FLASH_OFFSET_ERASE_REGIONS +
  1003. i * 4);
  1004. erase_region_size =
  1005. (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
  1006. tmp >>= 16;
  1007. erase_region_count = (tmp & 0xffff) + 1;
  1008. debug ("erase_region_count = %d erase_region_size = %d\n",
  1009. erase_region_count, erase_region_size);
  1010. for (j = 0; j < erase_region_count; j++) {
  1011. info->start[sect_cnt] = sector;
  1012. sector += (erase_region_size * size_ratio);
  1013. /*
  1014. * Only read protection status from supported devices (intel...)
  1015. */
  1016. switch (info->vendor) {
  1017. case CFI_CMDSET_INTEL_EXTENDED:
  1018. case CFI_CMDSET_INTEL_STANDARD:
  1019. info->protect[sect_cnt] =
  1020. flash_isset (info, sect_cnt,
  1021. FLASH_OFFSET_PROTECT,
  1022. FLASH_STATUS_PROTECT);
  1023. break;
  1024. default:
  1025. info->protect[sect_cnt] = 0; /* default: not protected */
  1026. }
  1027. sect_cnt++;
  1028. }
  1029. }
  1030. info->sector_count = sect_cnt;
  1031. /* multiply the size by the number of chips */
  1032. info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
  1033. info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
  1034. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
  1035. info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
  1036. tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
  1037. info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
  1038. tmp = (1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT)) *
  1039. (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT));
  1040. info->write_tout = tmp / 1000 + (tmp % 1000 ? 1 : 0); /* round up when converting to ms */
  1041. info->flash_id = FLASH_MAN_CFI;
  1042. if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
  1043. info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
  1044. }
  1045. }
  1046. flash_write_cmd (info, 0, 0, info->cmd_reset);
  1047. return (info->size);
  1048. }
  1049. /* loop through the sectors from the highest address
  1050. * when the passed address is greater or equal to the sector address
  1051. * we have a match
  1052. */
  1053. static flash_sect_t find_sector (flash_info_t * info, ulong addr)
  1054. {
  1055. flash_sect_t sector;
  1056. for (sector = info->sector_count - 1; sector >= 0; sector--) {
  1057. if (addr >= info->start[sector])
  1058. break;
  1059. }
  1060. return sector;
  1061. }
  1062. /*-----------------------------------------------------------------------
  1063. */
  1064. static int flash_write_cfiword (flash_info_t * info, ulong dest,
  1065. cfiword_t cword)
  1066. {
  1067. cfiptr_t ctladdr;
  1068. cfiptr_t cptr;
  1069. int flag;
  1070. ctladdr.cp = flash_make_addr (info, 0, 0);
  1071. cptr.cp = (uchar *) dest;
  1072. /* Check if Flash is (sufficiently) erased */
  1073. switch (info->portwidth) {
  1074. case FLASH_CFI_8BIT:
  1075. flag = ((cptr.cp[0] & cword.c) == cword.c);
  1076. break;
  1077. case FLASH_CFI_16BIT:
  1078. flag = ((cptr.wp[0] & cword.w) == cword.w);
  1079. break;
  1080. case FLASH_CFI_32BIT:
  1081. flag = ((cptr.lp[0] & cword.l) == cword.l);
  1082. break;
  1083. case FLASH_CFI_64BIT:
  1084. flag = ((cptr.llp[0] & cword.ll) == cword.ll);
  1085. break;
  1086. default:
  1087. return 2;
  1088. }
  1089. if (!flag)
  1090. return 2;
  1091. /* Disable interrupts which might cause a timeout here */
  1092. flag = disable_interrupts ();
  1093. switch (info->vendor) {
  1094. case CFI_CMDSET_INTEL_EXTENDED:
  1095. case CFI_CMDSET_INTEL_STANDARD:
  1096. flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  1097. flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
  1098. break;
  1099. case CFI_CMDSET_AMD_EXTENDED:
  1100. case CFI_CMDSET_AMD_STANDARD:
  1101. flash_unlock_seq (info, 0);
  1102. flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
  1103. break;
  1104. }
  1105. switch (info->portwidth) {
  1106. case FLASH_CFI_8BIT:
  1107. cptr.cp[0] = cword.c;
  1108. break;
  1109. case FLASH_CFI_16BIT:
  1110. cptr.wp[0] = cword.w;
  1111. break;
  1112. case FLASH_CFI_32BIT:
  1113. cptr.lp[0] = cword.l;
  1114. break;
  1115. case FLASH_CFI_64BIT:
  1116. cptr.llp[0] = cword.ll;
  1117. break;
  1118. }
  1119. /* re-enable interrupts if necessary */
  1120. if (flag)
  1121. enable_interrupts ();
  1122. return flash_full_status_check (info, find_sector (info, dest),
  1123. info->write_tout, "write");
  1124. }
  1125. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  1126. static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
  1127. int len)
  1128. {
  1129. flash_sect_t sector;
  1130. int cnt;
  1131. int retcode;
  1132. volatile cfiptr_t src;
  1133. volatile cfiptr_t dst;
  1134. switch (info->vendor) {
  1135. case CFI_CMDSET_INTEL_STANDARD:
  1136. case CFI_CMDSET_INTEL_EXTENDED:
  1137. src.cp = cp;
  1138. dst.cp = (uchar *) dest;
  1139. sector = find_sector (info, dest);
  1140. flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  1141. flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  1142. if ((retcode = flash_status_check (info, sector, info->buffer_write_tout,
  1143. "write to buffer")) == ERR_OK) {
  1144. /* reduce the number of loops by the width of the port */
  1145. switch (info->portwidth) {
  1146. case FLASH_CFI_8BIT:
  1147. cnt = len;
  1148. break;
  1149. case FLASH_CFI_16BIT:
  1150. cnt = len >> 1;
  1151. break;
  1152. case FLASH_CFI_32BIT:
  1153. cnt = len >> 2;
  1154. break;
  1155. case FLASH_CFI_64BIT:
  1156. cnt = len >> 3;
  1157. break;
  1158. default:
  1159. return ERR_INVAL;
  1160. break;
  1161. }
  1162. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1163. while (cnt-- > 0) {
  1164. switch (info->portwidth) {
  1165. case FLASH_CFI_8BIT:
  1166. *dst.cp++ = *src.cp++;
  1167. break;
  1168. case FLASH_CFI_16BIT:
  1169. *dst.wp++ = *src.wp++;
  1170. break;
  1171. case FLASH_CFI_32BIT:
  1172. *dst.lp++ = *src.lp++;
  1173. break;
  1174. case FLASH_CFI_64BIT:
  1175. *dst.llp++ = *src.llp++;
  1176. break;
  1177. default:
  1178. return ERR_INVAL;
  1179. break;
  1180. }
  1181. }
  1182. flash_write_cmd (info, sector, 0,
  1183. FLASH_CMD_WRITE_BUFFER_CONFIRM);
  1184. retcode = flash_full_status_check (info, sector,
  1185. info->buffer_write_tout,
  1186. "buffer write");
  1187. }
  1188. return retcode;
  1189. case CFI_CMDSET_AMD_STANDARD:
  1190. case CFI_CMDSET_AMD_EXTENDED:
  1191. src.cp = cp;
  1192. dst.cp = (uchar *) dest;
  1193. sector = find_sector (info, dest);
  1194. flash_unlock_seq(info,0);
  1195. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_TO_BUFFER);
  1196. switch (info->portwidth) {
  1197. case FLASH_CFI_8BIT:
  1198. cnt = len;
  1199. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1200. while (cnt-- > 0) *dst.cp++ = *src.cp++;
  1201. break;
  1202. case FLASH_CFI_16BIT:
  1203. cnt = len >> 1;
  1204. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1205. while (cnt-- > 0) *dst.wp++ = *src.wp++;
  1206. break;
  1207. case FLASH_CFI_32BIT:
  1208. cnt = len >> 2;
  1209. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1210. while (cnt-- > 0) *dst.lp++ = *src.lp++;
  1211. break;
  1212. case FLASH_CFI_64BIT:
  1213. cnt = len >> 3;
  1214. flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
  1215. while (cnt-- > 0) *dst.llp++ = *src.llp++;
  1216. break;
  1217. default:
  1218. return ERR_INVAL;
  1219. }
  1220. flash_write_cmd (info, sector, 0, AMD_CMD_WRITE_BUFFER_CONFIRM);
  1221. retcode = flash_full_status_check (info, sector, info->buffer_write_tout,
  1222. "buffer write");
  1223. return retcode;
  1224. default:
  1225. debug ("Unknown Command Set\n");
  1226. return ERR_INVAL;
  1227. }
  1228. }
  1229. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  1230. #endif /* CFG_FLASH_CFI */