strataflash.c 21 KB

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