strataflash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. #include <asm/cache.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[CFG_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 CFG_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 = CFG_FLASH_BASE;
  159. size = 0;
  160. /* Init: no FLASHes known */
  161. for (i=0; i<CFG_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 += CFG_FLASH_INCREMENT;
  165. if (flash_info[i].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. #if 0 /* test-only */
  171. /* Monitor protection ON by default */
  172. #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
  173. for(i=0; flash_info[0].start[i] < CFG_MONITOR_BASE+monitor_flash_len-1; i++)
  174. (void)flash_real_protect(&flash_info[0], i, 1);
  175. #endif
  176. #endif
  177. return (size);
  178. }
  179. /*-----------------------------------------------------------------------
  180. */
  181. int flash_erase (flash_info_t *info, int s_first, int s_last)
  182. {
  183. int rcode = 0;
  184. int prot;
  185. int sect;
  186. if( info->flash_id != FLASH_MAN_CFI) {
  187. printf ("Can't erase unknown flash type - aborted\n");
  188. return 1;
  189. }
  190. if ((s_first < 0) || (s_first > s_last)) {
  191. printf ("- no sectors to erase\n");
  192. return 1;
  193. }
  194. prot = 0;
  195. for (sect=s_first; sect<=s_last; ++sect) {
  196. if (info->protect[sect]) {
  197. prot++;
  198. }
  199. }
  200. if (prot) {
  201. printf ("- Warning: %d protected sectors will not be erased!\n",
  202. prot);
  203. } else {
  204. printf ("\n");
  205. }
  206. for (sect = s_first; sect<=s_last; sect++) {
  207. if (info->protect[sect] == 0) { /* not protected */
  208. flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
  209. flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
  210. flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
  211. if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
  212. rcode = 1;
  213. } else
  214. printf(".");
  215. }
  216. }
  217. printf (" done\n");
  218. return rcode;
  219. }
  220. /*-----------------------------------------------------------------------
  221. */
  222. void flash_print_info (flash_info_t *info)
  223. {
  224. int i;
  225. if (info->flash_id != FLASH_MAN_CFI) {
  226. printf ("missing or unknown FLASH type\n");
  227. return;
  228. }
  229. printf("CFI conformant FLASH (%d x %d)",
  230. (info->portwidth << 3 ), (info->chipwidth << 3 ));
  231. printf (" Size: %ld MB in %d Sectors\n",
  232. info->size >> 20, info->sector_count);
  233. printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
  234. info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
  235. printf (" Sector Start Addresses:");
  236. for (i=0; i<info->sector_count; ++i) {
  237. if ((i % 5) == 0)
  238. printf ("\n");
  239. printf (" %08lX%5s",
  240. info->start[i],
  241. info->protect[i] ? " (RO)" : " "
  242. );
  243. }
  244. printf ("\n");
  245. return;
  246. }
  247. /*-----------------------------------------------------------------------
  248. * Copy memory to flash, returns:
  249. * 0 - OK
  250. * 1 - write timeout
  251. * 2 - Flash not erased
  252. */
  253. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  254. {
  255. ulong wp;
  256. ulong cp;
  257. int aln;
  258. cfiword_t cword;
  259. int i, rc;
  260. /* get lower aligned address */
  261. wp = (addr & ~(info->portwidth - 1));
  262. /* handle unaligned start */
  263. if((aln = addr - wp) != 0) {
  264. cword.l = 0;
  265. cp = wp;
  266. for(i=0;i<aln; ++i, ++cp)
  267. flash_add_byte(info, &cword, (*(uchar *)cp));
  268. for(; (i< info->portwidth) && (cnt > 0) ; i++) {
  269. flash_add_byte(info, &cword, *src++);
  270. cnt--;
  271. cp++;
  272. }
  273. for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
  274. flash_add_byte(info, &cword, (*(uchar *)cp));
  275. if((rc = flash_write_cfiword(info, wp, cword)) != 0)
  276. return rc;
  277. wp = cp;
  278. }
  279. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  280. while(cnt >= info->portwidth) {
  281. i = info->buffer_size > cnt? cnt: info->buffer_size;
  282. if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
  283. return rc;
  284. wp += i;
  285. src += i;
  286. cnt -=i;
  287. }
  288. #else
  289. /* handle the aligned part */
  290. while(cnt >= info->portwidth) {
  291. cword.l = 0;
  292. for(i = 0; i < info->portwidth; i++) {
  293. flash_add_byte(info, &cword, *src++);
  294. }
  295. if((rc = flash_write_cfiword(info, wp, cword)) != 0)
  296. return rc;
  297. wp += info->portwidth;
  298. cnt -= info->portwidth;
  299. }
  300. #endif /* CFG_FLASH_USE_BUFFER_WRITE */
  301. if (cnt == 0) {
  302. return (0);
  303. }
  304. /*
  305. * handle unaligned tail bytes
  306. */
  307. cword.l = 0;
  308. for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
  309. flash_add_byte(info, &cword, *src++);
  310. --cnt;
  311. }
  312. for (; i<info->portwidth; ++i, ++cp) {
  313. flash_add_byte(info, & cword, (*(uchar *)cp));
  314. }
  315. return flash_write_cfiword(info, wp, cword);
  316. }
  317. /*-----------------------------------------------------------------------
  318. */
  319. int flash_real_protect(flash_info_t *info, long sector, int prot)
  320. {
  321. int retcode = 0;
  322. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  323. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
  324. if(prot)
  325. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
  326. else
  327. flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
  328. if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
  329. prot?"protect":"unprotect")) == 0) {
  330. info->protect[sector] = prot;
  331. /* Intel's unprotect unprotects all locking */
  332. if(prot == 0) {
  333. int i;
  334. for(i = 0 ; i<info->sector_count; i++) {
  335. if(info->protect[i])
  336. flash_real_protect(info, i, 1);
  337. }
  338. }
  339. }
  340. return retcode;
  341. }
  342. /*-----------------------------------------------------------------------
  343. * wait for XSR.7 to be set. Time out with an error if it does not.
  344. * This routine does not set the flash to read-array mode.
  345. */
  346. static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
  347. {
  348. ulong start;
  349. /* Wait for command completion */
  350. start = get_timer (0);
  351. while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
  352. if (get_timer(start) > info->erase_blk_tout) {
  353. printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
  354. flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
  355. return ERR_TIMOUT;
  356. }
  357. }
  358. return ERR_OK;
  359. }
  360. /*-----------------------------------------------------------------------
  361. * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
  362. * This routine sets the flash to read-array mode.
  363. */
  364. static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
  365. {
  366. int retcode;
  367. retcode = flash_status_check(info, sector, tout, prompt);
  368. if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
  369. retcode = ERR_INVAL;
  370. printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
  371. if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
  372. printf("Command Sequence Error.\n");
  373. } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
  374. printf("Block Erase Error.\n");
  375. retcode = ERR_NOT_ERASED;
  376. } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
  377. printf("Locking Error\n");
  378. }
  379. if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
  380. printf("Block locked.\n");
  381. retcode = ERR_PROTECTED;
  382. }
  383. if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
  384. printf("Vpp Low Error.\n");
  385. }
  386. flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
  387. return retcode;
  388. }
  389. /*-----------------------------------------------------------------------
  390. */
  391. static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
  392. {
  393. switch(info->portwidth) {
  394. case FLASH_CFI_8BIT:
  395. cword->c = c;
  396. break;
  397. case FLASH_CFI_16BIT:
  398. cword->w = (cword->w << 8) | c;
  399. break;
  400. case FLASH_CFI_32BIT:
  401. cword->l = (cword->l << 8) | c;
  402. }
  403. }
  404. /*-----------------------------------------------------------------------
  405. * make a proper sized command based on the port and chip widths
  406. */
  407. static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
  408. {
  409. int i;
  410. uchar *cp = (uchar *)cmdbuf;
  411. for(i=0; i< info->portwidth; i++)
  412. *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
  413. }
  414. /*
  415. * Write a proper sized command to the correct address
  416. */
  417. static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
  418. {
  419. volatile cfiptr_t addr;
  420. cfiword_t cword;
  421. addr.cp = flash_make_addr(info, sect, offset);
  422. flash_make_cmd(info, cmd, &cword);
  423. switch(info->portwidth) {
  424. case FLASH_CFI_8BIT:
  425. *addr.cp = cword.c;
  426. break;
  427. case FLASH_CFI_16BIT:
  428. *addr.wp = cword.w;
  429. break;
  430. case FLASH_CFI_32BIT:
  431. *addr.lp = cword.l;
  432. break;
  433. }
  434. }
  435. /*-----------------------------------------------------------------------
  436. */
  437. static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
  438. {
  439. cfiptr_t cptr;
  440. cfiword_t cword;
  441. int retval;
  442. cptr.cp = flash_make_addr(info, sect, offset);
  443. flash_make_cmd(info, cmd, &cword);
  444. switch(info->portwidth) {
  445. case FLASH_CFI_8BIT:
  446. retval = (cptr.cp[0] == cword.c);
  447. break;
  448. case FLASH_CFI_16BIT:
  449. retval = (cptr.wp[0] == cword.w);
  450. break;
  451. case FLASH_CFI_32BIT:
  452. retval = (cptr.lp[0] == cword.l);
  453. break;
  454. default:
  455. retval = 0;
  456. break;
  457. }
  458. return retval;
  459. }
  460. /*-----------------------------------------------------------------------
  461. */
  462. static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
  463. {
  464. cfiptr_t cptr;
  465. cfiword_t cword;
  466. int retval;
  467. cptr.cp = flash_make_addr(info, sect, offset);
  468. flash_make_cmd(info, cmd, &cword);
  469. switch(info->portwidth) {
  470. case FLASH_CFI_8BIT:
  471. retval = ((cptr.cp[0] & cword.c) == cword.c);
  472. break;
  473. case FLASH_CFI_16BIT:
  474. retval = ((cptr.wp[0] & cword.w) == cword.w);
  475. break;
  476. case FLASH_CFI_32BIT:
  477. retval = ((cptr.lp[0] & cword.l) == cword.l);
  478. break;
  479. default:
  480. retval = 0;
  481. break;
  482. }
  483. return retval;
  484. }
  485. /*-----------------------------------------------------------------------
  486. * detect if flash is compatible with the Common Flash Interface (CFI)
  487. * http://www.jedec.org/download/search/jesd68.pdf
  488. *
  489. */
  490. static int flash_detect_cfi(flash_info_t * info)
  491. {
  492. for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
  493. info->portwidth <<= 1) {
  494. for(info->chipwidth =FLASH_CFI_BY8;
  495. info->chipwidth <= info->portwidth;
  496. info->chipwidth <<= 1) {
  497. flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
  498. flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
  499. if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
  500. flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
  501. flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
  502. return 1;
  503. }
  504. }
  505. return 0;
  506. }
  507. /*
  508. * The following code cannot be run from FLASH!
  509. *
  510. */
  511. static ulong flash_get_size (ulong base, int banknum)
  512. {
  513. flash_info_t * info = &flash_info[banknum];
  514. int i, j;
  515. int sect_cnt;
  516. unsigned long sector;
  517. unsigned long tmp;
  518. int size_ratio = 0;
  519. uchar num_erase_regions;
  520. int erase_region_size;
  521. int erase_region_count;
  522. info->start[0] = base;
  523. invalidate_dcache_range(base, base+0x400);
  524. if(flash_detect_cfi(info)){
  525. size_ratio = info->portwidth / info->chipwidth;
  526. num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
  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. #ifdef DEBUG_FLASH
  560. printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
  561. #endif
  562. #ifdef DEBUG_FLASH
  563. printf("found %d erase regions\n", num_erase_regions);
  564. #endif
  565. #ifdef DEBUG_FLASH
  566. printf("size=%08x sectors=%08x \n", info->size, info->sector_count);
  567. #endif
  568. return(info->size);
  569. }
  570. /*-----------------------------------------------------------------------
  571. */
  572. static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
  573. {
  574. cfiptr_t ctladdr;
  575. cfiptr_t cptr;
  576. int flag;
  577. ctladdr.cp = flash_make_addr(info, 0, 0);
  578. cptr.cp = (uchar *)dest;
  579. /* Check if Flash is (sufficiently) erased */
  580. switch(info->portwidth) {
  581. case FLASH_CFI_8BIT:
  582. flag = ((cptr.cp[0] & cword.c) == cword.c);
  583. break;
  584. case FLASH_CFI_16BIT:
  585. flag = ((cptr.wp[0] & cword.w) == cword.w);
  586. break;
  587. case FLASH_CFI_32BIT:
  588. flag = ((cptr.lp[0] & cword.l) == cword.l);
  589. break;
  590. default:
  591. return 2;
  592. }
  593. if(!flag)
  594. return 2;
  595. /* Disable interrupts which might cause a timeout here */
  596. flag = disable_interrupts();
  597. flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
  598. flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
  599. switch(info->portwidth) {
  600. case FLASH_CFI_8BIT:
  601. cptr.cp[0] = cword.c;
  602. break;
  603. case FLASH_CFI_16BIT:
  604. cptr.wp[0] = cword.w;
  605. break;
  606. case FLASH_CFI_32BIT:
  607. cptr.lp[0] = cword.l;
  608. break;
  609. }
  610. /* re-enable interrupts if necessary */
  611. if(flag)
  612. enable_interrupts();
  613. return flash_full_status_check(info, 0, info->write_tout, "write");
  614. }
  615. #ifdef CFG_FLASH_USE_BUFFER_WRITE
  616. /* loop through the sectors from the highest address
  617. * when the passed address is greater or equal to the sector address
  618. * we have a match
  619. */
  620. static int find_sector(flash_info_t *info, ulong addr)
  621. {
  622. int sector;
  623. for(sector = info->sector_count - 1; sector >= 0; sector--) {
  624. if(addr >= info->start[sector])
  625. break;
  626. }
  627. return sector;
  628. }
  629. static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
  630. {
  631. int sector;
  632. int cnt;
  633. int retcode;
  634. volatile cfiptr_t src;
  635. volatile cfiptr_t dst;
  636. src.cp = cp;
  637. dst.cp = (uchar *)dest;
  638. sector = find_sector(info, dest);
  639. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  640. flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
  641. if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
  642. "write to buffer")) == ERR_OK) {
  643. switch(info->portwidth) {
  644. case FLASH_CFI_8BIT:
  645. cnt = len;
  646. break;
  647. case FLASH_CFI_16BIT:
  648. cnt = len >> 1;
  649. break;
  650. case FLASH_CFI_32BIT:
  651. cnt = len >> 2;
  652. break;
  653. default:
  654. return ERR_INVAL;
  655. break;
  656. }
  657. flash_write_cmd(info, sector, 0, (uchar)cnt-1);
  658. while(cnt-- > 0) {
  659. switch(info->portwidth) {
  660. case FLASH_CFI_8BIT:
  661. *dst.cp++ = *src.cp++;
  662. break;
  663. case FLASH_CFI_16BIT:
  664. *dst.wp++ = *src.wp++;
  665. break;
  666. case FLASH_CFI_32BIT:
  667. *dst.lp++ = *src.lp++;
  668. break;
  669. default:
  670. return ERR_INVAL;
  671. break;
  672. }
  673. }
  674. flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
  675. retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
  676. "buffer write");
  677. }
  678. flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
  679. return retcode;
  680. }
  681. #endif /* CFG_USE_FLASH_BUFFER_WRITE */