cfi_flash.c 38 KB

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