strataflash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * (C) Copyright 2002
  3. * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #undef DEBUG_FLASH
  26. /*
  27. * This file implements a Common Flash Interface (CFI) driver for U-Boot.
  28. * The width of the port and the width of the chips are determined at initialization.
  29. * These widths are used to calculate the address for access CFI data structures.
  30. * It has been tested on an Intel Strataflash implementation.
  31. *
  32. * References
  33. * JEDEC Standard JESD68 - Common Flash Interface (CFI)
  34. * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
  35. * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
  36. * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
  37. *
  38. * TODO
  39. * Use Primary Extended Query table (PRI) and Alternate Algorithm Query Table (ALT) to determine if protection is available
  40. * Add support for other command sets Use the PRI and ALT to determine command set
  41. * Verify erase and program timeouts.
  42. */
  43. #define FLASH_CMD_CFI 0x98
  44. #define FLASH_CMD_READ_ID 0x90
  45. #define FLASH_CMD_RESET 0xff
  46. #define FLASH_CMD_BLOCK_ERASE 0x20
  47. #define FLASH_CMD_ERASE_CONFIRM 0xD0
  48. #define FLASH_CMD_WRITE 0x40
  49. #define FLASH_CMD_PROTECT 0x60
  50. #define FLASH_CMD_PROTECT_SET 0x01
  51. #define FLASH_CMD_PROTECT_CLEAR 0xD0
  52. #define FLASH_CMD_CLEAR_STATUS 0x50
  53. #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
  54. #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
  55. #define FLASH_STATUS_DONE 0x80
  56. #define FLASH_STATUS_ESS 0x40
  57. #define FLASH_STATUS_ECLBS 0x20
  58. #define FLASH_STATUS_PSLBS 0x10
  59. #define FLASH_STATUS_VPENS 0x08
  60. #define FLASH_STATUS_PSS 0x04
  61. #define FLASH_STATUS_DPS 0x02
  62. #define FLASH_STATUS_R 0x01
  63. #define FLASH_STATUS_PROTECT 0x01
  64. #define FLASH_OFFSET_CFI 0x55
  65. #define FLASH_OFFSET_CFI_RESP 0x10
  66. #define FLASH_OFFSET_WTOUT 0x1F
  67. #define FLASH_OFFSET_WBTOUT 0x20
  68. #define FLASH_OFFSET_ETOUT 0x21
  69. #define FLASH_OFFSET_CETOUT 0x22
  70. #define FLASH_OFFSET_WMAX_TOUT 0x23
  71. #define FLASH_OFFSET_WBMAX_TOUT 0x24
  72. #define FLASH_OFFSET_EMAX_TOUT 0x25
  73. #define FLASH_OFFSET_CEMAX_TOUT 0x26
  74. #define FLASH_OFFSET_SIZE 0x27
  75. #define FLASH_OFFSET_INTERFACE 0x28
  76. #define FLASH_OFFSET_BUFFER_SIZE 0x2A
  77. #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
  78. #define FLASH_OFFSET_ERASE_REGIONS 0x2D
  79. #define FLASH_OFFSET_PROTECT 0x02
  80. #define FLASH_OFFSET_USER_PROTECTION 0x85
  81. #define FLASH_OFFSET_INTEL_PROTECTION 0x81
  82. #define FLASH_MAN_CFI 0x01000000
  83. typedef union {
  84. unsigned char c;
  85. unsigned short w;
  86. unsigned long l;
  87. } cfiword_t;
  88. typedef union {
  89. unsigned char * cp;
  90. unsigned short *wp;
  91. unsigned long *lp;
  92. } cfiptr_t;
  93. #define NUM_ERASE_REGIONS 4
  94. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  95. /*-----------------------------------------------------------------------
  96. * Functions
  97. */
  98. static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
  99. static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
  100. static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
  101. static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
  102. static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
  103. static int flash_detect_cfi(flash_info_t * info);
  104. static ulong flash_get_size (ulong base, int banknum);
  105. static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
  106. static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
  107. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  108. static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
  109. #endif
  110. /*-----------------------------------------------------------------------
  111. * create an address based on the offset and the port width
  112. */
  113. inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
  114. {
  115. return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
  116. }
  117. /*-----------------------------------------------------------------------
  118. * read a character at a port width address
  119. */
  120. inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
  121. {
  122. uchar *cp;
  123. cp = flash_make_addr(info, 0, offset);
  124. return (cp[info->portwidth - 1]);
  125. }
  126. /*-----------------------------------------------------------------------
  127. * read a short word by swapping for ppc format.
  128. */
  129. ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
  130. {
  131. uchar * addr;
  132. addr = flash_make_addr(info, sect, offset);
  133. return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
  134. }
  135. /*-----------------------------------------------------------------------
  136. * read a long word by picking the least significant byte of each maiximum
  137. * port size word. Swap for ppc format.
  138. */
  139. ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
  140. {
  141. uchar * addr;
  142. addr = flash_make_addr(info, sect, offset);
  143. return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
  144. (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
  145. }
  146. /*-----------------------------------------------------------------------
  147. */
  148. unsigned long flash_init (void)
  149. {
  150. unsigned long size;
  151. int i;
  152. unsigned long address;
  153. /* The flash is positioned back to back, with the demultiplexing of the chip
  154. * based on the A24 address line.
  155. *
  156. */
  157. address = CFG_FLASH_BASE;
  158. size = 0;
  159. /* Init: no FLASHes known */
  160. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  161. flash_info[i].flash_id = FLASH_UNKNOWN;
  162. size += flash_info[i].size = flash_get_size(address, i);
  163. address += CFG_FLASH_INCREMENT;
  164. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  165. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
  166. flash_info[0].size, flash_info[i].size<<20);
  167. }
  168. }
  169. #if 0 /* test-only */
  170. /* Monitor protection ON by default */
  171. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  172. for(i=0; flash_info[0].start[i] < CFG_MONITOR_BASE+monitor_flash_len-1; i++)
  173. (void)flash_real_protect(&flash_info[0], i, 1);
  174. #endif
  175. #endif
  176. return (size);
  177. }
  178. /*-----------------------------------------------------------------------
  179. */
  180. int flash_erase (flash_info_t *info, int s_first, int s_last)
  181. {
  182. int rcode = 0;
  183. int prot;
  184. int sect;
  185. if( info->flash_id != FLASH_MAN_CFI) {
  186. printf ("Can't erase unknown flash type - aborted\n");
  187. return 1;
  188. }
  189. if ((s_first < 0) || (s_first > s_last)) {
  190. printf ("- no sectors to erase\n");
  191. return 1;
  192. }
  193. prot = 0;
  194. for (sect=s_first; sect<=s_last; ++sect) {
  195. if (info->protect[sect]) {
  196. prot++;
  197. }
  198. }
  199. if (prot) {
  200. printf ("- Warning: %d protected sectors will not be erased!\n",
  201. prot);
  202. } else {
  203. printf ("\n");
  204. }
  205. for (sect = s_first; sect<=s_last; sect++) {
  206. if (info->protect[sect] == 0) { /* not protected */
  207. flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  208. flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  209. flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  210. if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
  211. rcode = 1;
  212. } else
  213. printf(".");
  214. }
  215. }
  216. printf (" done\n");
  217. return rcode;
  218. }
  219. /*-----------------------------------------------------------------------
  220. */
  221. void flash_print_info (flash_info_t *info)
  222. {
  223. int i;
  224. if (info->flash_id != FLASH_MAN_CFI) {
  225. printf ("missing or unknown FLASH type\n");
  226. return;
  227. }
  228. printf("CFI conformant FLASH (%d x %d)",
  229. (info->portwidth << 3 ), (info->chipwidth << 3 ));
  230. printf (" Size: %ld MB in %d Sectors\n",
  231. info->size >> 20, info->sector_count);
  232. printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
  233. info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
  234. printf (" Sector Start Addresses:");
  235. for (i=0; i<info->sector_count; ++i) {
  236. if ((i % 5) == 0)
  237. printf ("\n");
  238. printf (" %08lX%5s",
  239. info->start[i],
  240. info->protect[i] ? " (RO)" : " "
  241. );
  242. }
  243. printf ("\n");
  244. return;
  245. }
  246. /*-----------------------------------------------------------------------
  247. * Copy memory to flash, returns:
  248. * 0 - OK
  249. * 1 - write timeout
  250. * 2 - Flash not erased
  251. */
  252. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  253. {
  254. ulong wp;
  255. ulong cp;
  256. int aln;
  257. cfiword_t cword;
  258. int i, rc;
  259. /* get lower aligned address */
  260. wp = (addr & ~(info->portwidth - 1));
  261. /* handle unaligned start */
  262. if((aln = addr - wp) != 0) {
  263. cword.l = 0;
  264. cp = wp;
  265. for(i=0;i<aln; ++i, ++cp)
  266. flash_add_byte(info, &cword, (*(uchar *)cp));
  267. for(; (i< info->portwidth) && (cnt > 0) ; i++) {
  268. flash_add_byte(info, &cword, *src++);
  269. cnt--;
  270. cp++;
  271. }
  272. for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  273. flash_add_byte(info, &cword, (*(uchar *)cp));
  274. if((rc = flash_write_cfiword(info, wp, cword)) != 0)
  275. return rc;
  276. wp = cp;
  277. }
  278. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  279. while(cnt >= info->portwidth) {
  280. i = info->buffer_size > cnt? cnt: info->buffer_size;
  281. if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
  282. return rc;
  283. wp += i;
  284. src += i;
  285. cnt -=i;
  286. }
  287. #else
  288. /* handle the aligned part */
  289. while(cnt >= info->portwidth) {
  290. cword.l = 0;
  291. for(i = 0; i < info->portwidth; i++) {
  292. flash_add_byte(info, &cword, *src++);
  293. }
  294. if((rc = flash_write_cfiword(info, wp, cword)) != 0)
  295. return rc;
  296. wp += info->portwidth;
  297. cnt -= info->portwidth;
  298. }
  299. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  300. if (cnt == 0) {
  301. return (0);
  302. }
  303. /*
  304. * handle unaligned tail bytes
  305. */
  306. cword.l = 0;
  307. for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
  308. flash_add_byte(info, &cword, *src++);
  309. --cnt;
  310. }
  311. for (; i<info->portwidth; ++i, ++cp) {
  312. flash_add_byte(info, & cword, (*(uchar *)cp));
  313. }
  314. return flash_write_cfiword(info, wp, cword);
  315. }
  316. /*-----------------------------------------------------------------------
  317. */
  318. int flash_real_protect(flash_info_t *info, long sector, int prot)
  319. {
  320. int retcode = 0;
  321. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  322. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
  323. if(prot)
  324. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
  325. else
  326. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  327. if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
  328. prot?"protect":"unprotect")) == 0) {
  329. info->protect[sector] = prot;
  330. /* Intel's unprotect unprotects all locking */
  331. if(prot == 0) {
  332. int i;
  333. for(i = 0 ; i<info->sector_count; i++) {
  334. if(info->protect[i])
  335. flash_real_protect(info, i, 1);
  336. }
  337. }
  338. }
  339. return retcode;
  340. }
  341. /*-----------------------------------------------------------------------
  342. * wait for XSR.7 to be set. Time out with an error if it does not.
  343. * This routine does not set the flash to read-array mode.
  344. */
  345. static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
  346. {
  347. ulong start;
  348. /* Wait for command completion */
  349. start = get_timer (0);
  350. while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
  351. if (get_timer(start) > info->erase_blk_tout) {
  352. printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
  353. flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
  354. return ERR_TIMOUT;
  355. }
  356. }
  357. return ERR_OK;
  358. }
  359. /*-----------------------------------------------------------------------
  360. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  361. * This routine sets the flash to read-array mode.
  362. */
  363. static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
  364. {
  365. int retcode;
  366. retcode = flash_status_check(info, sector, tout, prompt);
  367. if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
  368. retcode = ERR_INVAL;
  369. printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
  370. if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
  371. printf("Command Sequence Error.\n");
  372. } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
  373. printf("Block Erase Error.\n");
  374. retcode = ERR_NOT_ERASED;
  375. } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
  376. printf("Locking Error\n");
  377. }
  378. if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
  379. printf("Block locked.\n");
  380. retcode = ERR_PROTECTED;
  381. }
  382. if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
  383. printf("Vpp Low Error.\n");
  384. }
  385. flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
  386. return retcode;
  387. }
  388. /*-----------------------------------------------------------------------
  389. */
  390. static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
  391. {
  392. switch(info->portwidth) {
  393. case FLASH_CFI_8BIT:
  394. cword->c = c;
  395. break;
  396. case FLASH_CFI_16BIT:
  397. cword->w = (cword->w << 8) | c;
  398. break;
  399. case FLASH_CFI_32BIT:
  400. cword->l = (cword->l << 8) | c;
  401. }
  402. }
  403. /*-----------------------------------------------------------------------
  404. * make a proper sized command based on the port and chip widths
  405. */
  406. static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
  407. {
  408. int i;
  409. uchar *cp = (uchar *)cmdbuf;
  410. for(i=0; i< info->portwidth; i++)
  411. *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
  412. }
  413. /*
  414. * Write a proper sized command to the correct address
  415. */
  416. static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
  417. {
  418. volatile cfiptr_t addr;
  419. cfiword_t cword;
  420. addr.cp = flash_make_addr(info, sect, offset);
  421. flash_make_cmd(info, cmd, &cword);
  422. switch(info->portwidth) {
  423. case FLASH_CFI_8BIT:
  424. *addr.cp = cword.c;
  425. break;
  426. case FLASH_CFI_16BIT:
  427. *addr.wp = cword.w;
  428. break;
  429. case FLASH_CFI_32BIT:
  430. *addr.lp = cword.l;
  431. break;
  432. }
  433. }
  434. /*-----------------------------------------------------------------------
  435. */
  436. static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
  437. {
  438. cfiptr_t cptr;
  439. cfiword_t cword;
  440. int retval;
  441. cptr.cp = flash_make_addr(info, sect, offset);
  442. flash_make_cmd(info, cmd, &cword);
  443. switch(info->portwidth) {
  444. case FLASH_CFI_8BIT:
  445. retval = (cptr.cp[0] == cword.c);
  446. break;
  447. case FLASH_CFI_16BIT:
  448. retval = (cptr.wp[0] == cword.w);
  449. break;
  450. case FLASH_CFI_32BIT:
  451. retval = (cptr.lp[0] == cword.l);
  452. break;
  453. default:
  454. retval = 0;
  455. break;
  456. }
  457. return retval;
  458. }
  459. /*-----------------------------------------------------------------------
  460. */
  461. static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
  462. {
  463. cfiptr_t cptr;
  464. cfiword_t cword;
  465. int retval;
  466. cptr.cp = flash_make_addr(info, sect, offset);
  467. flash_make_cmd(info, cmd, &cword);
  468. switch(info->portwidth) {
  469. case FLASH_CFI_8BIT:
  470. retval = ((cptr.cp[0] & cword.c) == cword.c);
  471. break;
  472. case FLASH_CFI_16BIT:
  473. retval = ((cptr.wp[0] & cword.w) == cword.w);
  474. break;
  475. case FLASH_CFI_32BIT:
  476. retval = ((cptr.lp[0] & cword.l) == cword.l);
  477. break;
  478. default:
  479. retval = 0;
  480. break;
  481. }
  482. return retval;
  483. }
  484. /*-----------------------------------------------------------------------
  485. * detect if flash is compatible with the Common Flash Interface (CFI)
  486. * http://www.jedec.org/download/search/jesd68.pdf
  487. *
  488. */
  489. static int flash_detect_cfi(flash_info_t * info)
  490. {
  491. for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
  492. info->portwidth <<= 1) {
  493. for(info->chipwidth =FLASH_CFI_BY8;
  494. info->chipwidth <= info->portwidth;
  495. info->chipwidth <<= 1) {
  496. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  497. flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  498. if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
  499. flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
  500. flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
  501. return 1;
  502. }
  503. }
  504. return 0;
  505. }
  506. /*
  507. * The following code cannot be run from FLASH!
  508. *
  509. */
  510. static ulong flash_get_size (ulong base, int banknum)
  511. {
  512. flash_info_t * info = &flash_info[banknum];
  513. int i, j;
  514. int sect_cnt;
  515. unsigned long sector;
  516. unsigned long tmp;
  517. int size_ratio;
  518. uchar num_erase_regions;
  519. int erase_region_size;
  520. int erase_region_count;
  521. info->start[0] = base;
  522. if(flash_detect_cfi(info)){
  523. #ifdef DEBUG_FLASH
  524. printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
  525. #endif
  526. size_ratio = info->portwidth / info->chipwidth;
  527. num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  528. #ifdef DEBUG_FLASH
  529. printf("found %d erase regions\n", num_erase_regions);
  530. #endif
  531. sect_cnt = 0;
  532. sector = base;
  533. for(i = 0 ; i < num_erase_regions; i++) {
  534. if(i > NUM_ERASE_REGIONS) {
  535. printf("%d erase regions found, only %d used\n",
  536. num_erase_regions, NUM_ERASE_REGIONS);
  537. break;
  538. }
  539. tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
  540. erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
  541. tmp >>= 16;
  542. erase_region_count = (tmp & 0xffff) +1;
  543. for(j = 0; j< erase_region_count; j++) {
  544. info->start[sect_cnt] = sector;
  545. sector += (erase_region_size * size_ratio);
  546. info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
  547. sect_cnt++;
  548. }
  549. }
  550. info->sector_count = sect_cnt;
  551. /* multiply the size by the number of chips */
  552. info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
  553. info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
  554. tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
  555. info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
  556. tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
  557. info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
  558. tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
  559. info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
  560. info->flash_id = FLASH_MAN_CFI;
  561. }
  562. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  563. return(info->size);
  564. }
  565. /*-----------------------------------------------------------------------
  566. */
  567. static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
  568. {
  569. cfiptr_t ctladdr;
  570. cfiptr_t cptr;
  571. int flag;
  572. ctladdr.cp = flash_make_addr(info, 0, 0);
  573. cptr.cp = (uchar *)dest;
  574. /* Check if Flash is (sufficiently) erased */
  575. switch(info->portwidth) {
  576. case FLASH_CFI_8BIT:
  577. flag = ((cptr.cp[0] & cword.c) == cword.c);
  578. break;
  579. case FLASH_CFI_16BIT:
  580. flag = ((cptr.wp[0] & cword.w) == cword.w);
  581. break;
  582. case FLASH_CFI_32BIT:
  583. flag = ((cptr.lp[0] & cword.l) == cword.l);
  584. break;
  585. default:
  586. return 2;
  587. }
  588. if(!flag)
  589. return 2;
  590. /* Disable interrupts which might cause a timeout here */
  591. flag = disable_interrupts();
  592. flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  593. flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
  594. switch(info->portwidth) {
  595. case FLASH_CFI_8BIT:
  596. cptr.cp[0] = cword.c;
  597. break;
  598. case FLASH_CFI_16BIT:
  599. cptr.wp[0] = cword.w;
  600. break;
  601. case FLASH_CFI_32BIT:
  602. cptr.lp[0] = cword.l;
  603. break;
  604. }
  605. /* re-enable interrupts if necessary */
  606. if(flag)
  607. enable_interrupts();
  608. return flash_full_status_check(info, 0, info->write_tout, "write");
  609. }
  610. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  611. /* loop through the sectors from the highest address
  612. * when the passed address is greater or equal to the sector address
  613. * we have a match
  614. */
  615. static int find_sector(flash_info_t *info, ulong addr)
  616. {
  617. int sector;
  618. for(sector = info->sector_count - 1; sector >= 0; sector--) {
  619. if(addr >= info->start[sector])
  620. break;
  621. }
  622. return sector;
  623. }
  624. static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
  625. {
  626. int sector;
  627. int cnt;
  628. int retcode;
  629. volatile cfiptr_t src;
  630. volatile cfiptr_t dst;
  631. src.cp = cp;
  632. dst.cp = (uchar *)dest;
  633. sector = find_sector(info, dest);
  634. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  635. flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  636. if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
  637. "write to buffer")) == ERR_OK) {
  638. switch(info->portwidth) {
  639. case FLASH_CFI_8BIT:
  640. cnt = len;
  641. break;
  642. case FLASH_CFI_16BIT:
  643. cnt = len >> 1;
  644. break;
  645. case FLASH_CFI_32BIT:
  646. cnt = len >> 2;
  647. break;
  648. default:
  649. return ERR_INVAL;
  650. break;
  651. }
  652. flash_write_cmd(info, sector, 0, (uchar)cnt-1);
  653. while(cnt-- > 0) {
  654. switch(info->portwidth) {
  655. case FLASH_CFI_8BIT:
  656. *dst.cp++ = *src.cp++;
  657. break;
  658. case FLASH_CFI_16BIT:
  659. *dst.wp++ = *src.wp++;
  660. break;
  661. case FLASH_CFI_32BIT:
  662. *dst.lp++ = *src.lp++;
  663. break;
  664. default:
  665. return ERR_INVAL;
  666. break;
  667. }
  668. }
  669. flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
  670. retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
  671. "buffer write");
  672. }
  673. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  674. return retcode;
  675. }
  676. #endif /* CFG_USE_FLASH_BUFFER_WRITE */