cfi_flash.c 46 KB

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