cfi_flash.c 42 KB

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