flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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 <board/cogent/flash.h>
  25. #include <linux/compiler.h>
  26. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  27. #if defined(CONFIG_ENV_IS_IN_FLASH)
  28. # ifndef CONFIG_ENV_ADDR
  29. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  30. # endif
  31. # ifndef CONFIG_ENV_SIZE
  32. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  33. # endif
  34. # ifndef CONFIG_ENV_SECT_SIZE
  35. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  36. # endif
  37. #endif
  38. /*-----------------------------------------------------------------------
  39. * Functions
  40. */
  41. static int write_word (flash_info_t *info, ulong dest, ulong data);
  42. /*-----------------------------------------------------------------------
  43. */
  44. #if defined(CONFIG_CMA302)
  45. /*
  46. * probe for the existence of flash at address "addr"
  47. * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
  48. */
  49. static int
  50. c302f_probe_word(c302f_addr_t addr)
  51. {
  52. /* reset the flash */
  53. *addr = C302F_BNK_CMD_RST;
  54. /* check the manufacturer id */
  55. *addr = C302F_BNK_CMD_RD_ID;
  56. if (*C302F_BNK_ADDR_MAN(addr) != C302F_BNK_RD_ID_MAN)
  57. return 1;
  58. /* check the device id */
  59. *addr = C302F_BNK_CMD_RD_ID;
  60. if (*C302F_BNK_ADDR_DEV(addr) != C302F_BNK_RD_ID_DEV)
  61. return 2;
  62. #ifdef FLASH_DEBUG
  63. {
  64. int i;
  65. printf("\nMaster Lock Config = 0x%08lx\n",
  66. *C302F_BNK_ADDR_CFGM(addr));
  67. for (i = 0; i < C302F_BNK_NBLOCKS; i++)
  68. printf("Block %2d Lock Config = 0x%08lx\n",
  69. i, *C302F_BNK_ADDR_CFG(i, addr));
  70. }
  71. #endif
  72. /* reset the flash again */
  73. *addr = C302F_BNK_CMD_RST;
  74. return 0;
  75. }
  76. /*
  77. * probe for Cogent CMA302 flash module at address "base" and store
  78. * info for any found into flash_info entry "fip". Must find at least
  79. * one bank.
  80. */
  81. static void
  82. c302f_probe(flash_info_t *fip, c302f_addr_t base)
  83. {
  84. c302f_addr_t addr, eaddr;
  85. int nbanks;
  86. fip->size = 0L;
  87. fip->sector_count = 0;
  88. addr = base;
  89. eaddr = C302F_BNK_ADDR_BASE(addr, C302F_MAX_BANKS);
  90. nbanks = 0;
  91. while (addr < eaddr) {
  92. c302f_addr_t addrw, eaddrw, addrb;
  93. int i, osc, nsc;
  94. addrw = addr;
  95. eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
  96. while (addrw < eaddrw)
  97. if (c302f_probe_word(addrw++) != 0)
  98. goto out;
  99. /* bank exists - append info for this bank to *fip */
  100. fip->flash_id = FLASH_MAN_INTEL|FLASH_28F008S5;
  101. fip->size += C302F_BNK_SIZE;
  102. osc = fip->sector_count;
  103. fip->sector_count += C302F_BNK_NBLOCKS;
  104. if ((nsc = fip->sector_count) >= CONFIG_SYS_MAX_FLASH_SECT)
  105. panic("Too many sectors in flash at address 0x%08lx\n",
  106. (unsigned long)base);
  107. addrb = addr;
  108. for (i = osc; i < nsc; i++) {
  109. fip->start[i] = (ulong)addrb;
  110. fip->protect[i] = 0;
  111. addrb = C302F_BNK_ADDR_NEXT_BLK(addrb);
  112. }
  113. addr = C302F_BNK_ADDR_NEXT_BNK(addr);
  114. nbanks++;
  115. }
  116. out:
  117. if (nbanks == 0)
  118. panic("ERROR: no flash found at address 0x%08lx\n",
  119. (unsigned long)base);
  120. }
  121. static void
  122. c302f_reset(flash_info_t *info, int sect)
  123. {
  124. c302f_addr_t addrw, eaddrw;
  125. addrw = (c302f_addr_t)info->start[sect];
  126. eaddrw = C302F_BNK_ADDR_NEXT_WORD(addrw);
  127. while (addrw < eaddrw) {
  128. #ifdef FLASH_DEBUG
  129. printf(" writing reset cmd to addr 0x%08lx\n",
  130. (unsigned long)addrw);
  131. #endif
  132. *addrw = C302F_BNK_CMD_RST;
  133. addrw++;
  134. }
  135. }
  136. static void
  137. c302f_erase_init(flash_info_t *info, int sect)
  138. {
  139. c302f_addr_t addrw, saddrw, eaddrw;
  140. int flag;
  141. #ifdef FLASH_DEBUG
  142. printf("0x%08lx C302F_BNK_CMD_PROG\n", C302F_BNK_CMD_PROG);
  143. printf("0x%08lx C302F_BNK_CMD_ERASE1\n", C302F_BNK_CMD_ERASE1);
  144. printf("0x%08lx C302F_BNK_CMD_ERASE2\n", C302F_BNK_CMD_ERASE2);
  145. printf("0x%08lx C302F_BNK_CMD_CLR_STAT\n", C302F_BNK_CMD_CLR_STAT);
  146. printf("0x%08lx C302F_BNK_CMD_RST\n", C302F_BNK_CMD_RST);
  147. printf("0x%08lx C302F_BNK_STAT_RDY\n", C302F_BNK_STAT_RDY);
  148. printf("0x%08lx C302F_BNK_STAT_ERR\n", C302F_BNK_STAT_ERR);
  149. #endif
  150. saddrw = (c302f_addr_t)info->start[sect];
  151. eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
  152. #ifdef FLASH_DEBUG
  153. printf("erasing sector %d, start addr = 0x%08lx "
  154. "(bank next word addr = 0x%08lx)\n", sect,
  155. (unsigned long)saddrw, (unsigned long)eaddrw);
  156. #endif
  157. /* Disable intrs which might cause a timeout here */
  158. flag = disable_interrupts();
  159. for (addrw = saddrw; addrw < eaddrw; addrw++) {
  160. #ifdef FLASH_DEBUG
  161. printf(" writing erase cmd to addr 0x%08lx\n",
  162. (unsigned long)addrw);
  163. #endif
  164. *addrw = C302F_BNK_CMD_ERASE1;
  165. *addrw = C302F_BNK_CMD_ERASE2;
  166. }
  167. /* re-enable interrupts if necessary */
  168. if (flag)
  169. enable_interrupts();
  170. }
  171. static int
  172. c302f_erase_poll(flash_info_t *info, int sect)
  173. {
  174. c302f_addr_t addrw, saddrw, eaddrw;
  175. int sectdone, haderr;
  176. saddrw = (c302f_addr_t)info->start[sect];
  177. eaddrw = C302F_BNK_ADDR_NEXT_WORD(saddrw);
  178. sectdone = 1;
  179. haderr = 0;
  180. for (addrw = saddrw; addrw < eaddrw; addrw++) {
  181. c302f_word_t stat = *addrw;
  182. #ifdef FLASH_DEBUG
  183. printf(" checking status at addr "
  184. "0x%08lx [0x%08lx]\n",
  185. (unsigned long)addrw, stat);
  186. #endif
  187. if ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY)
  188. sectdone = 0;
  189. else if ((stat & C302F_BNK_STAT_ERR) != 0) {
  190. printf(" failed on sector %d "
  191. "(stat = 0x%08lx) at "
  192. "address 0x%08lx\n",
  193. sect, stat,
  194. (unsigned long)addrw);
  195. *addrw = C302F_BNK_CMD_CLR_STAT;
  196. haderr = 1;
  197. }
  198. }
  199. if (haderr)
  200. return (-1);
  201. else
  202. return (sectdone);
  203. }
  204. static int
  205. c302f_write_word(c302f_addr_t addr, c302f_word_t value)
  206. {
  207. c302f_word_t stat;
  208. ulong start;
  209. int flag, retval;
  210. /* Disable interrupts which might cause a timeout here */
  211. flag = disable_interrupts();
  212. *addr = C302F_BNK_CMD_PROG;
  213. *addr = value;
  214. /* re-enable interrupts if necessary */
  215. if (flag)
  216. enable_interrupts();
  217. retval = 0;
  218. /* data polling for D7 */
  219. start = get_timer (0);
  220. do {
  221. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  222. retval = 1;
  223. goto done;
  224. }
  225. stat = *addr;
  226. } while ((stat & C302F_BNK_STAT_RDY) != C302F_BNK_STAT_RDY);
  227. if ((stat & C302F_BNK_STAT_ERR) != 0) {
  228. printf("flash program failed (stat = 0x%08lx) "
  229. "at address 0x%08lx\n", (ulong)stat, (ulong)addr);
  230. *addr = C302F_BNK_CMD_CLR_STAT;
  231. retval = 3;
  232. }
  233. done:
  234. /* reset to read mode */
  235. *addr = C302F_BNK_CMD_RST;
  236. return (retval);
  237. }
  238. #endif /* CONFIG_CMA302 */
  239. unsigned long
  240. flash_init(void)
  241. {
  242. unsigned long total;
  243. int i;
  244. __maybe_unused flash_info_t *fip;
  245. /* Init: no FLASHes known */
  246. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  247. flash_info[i].flash_id = FLASH_UNKNOWN;
  248. }
  249. fip = &flash_info[0];
  250. total = 0L;
  251. #if defined(CONFIG_CMA302)
  252. c302f_probe(fip, (c302f_addr_t)CONFIG_SYS_FLASH_BASE);
  253. total += fip->size;
  254. fip++;
  255. #endif
  256. #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
  257. /* not yet ...
  258. cmbf_probe(fip, (cmbf_addr_t)CMA_MB_FLASH_BASE);
  259. total += fip->size;
  260. fip++;
  261. */
  262. #endif
  263. /*
  264. * protect monitor and environment sectors
  265. */
  266. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  267. flash_protect(FLAG_PROTECT_SET,
  268. CONFIG_SYS_MONITOR_BASE,
  269. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  270. &flash_info[0]);
  271. #endif
  272. #ifdef CONFIG_ENV_IS_IN_FLASH
  273. /* ENV protection ON by default */
  274. flash_protect(FLAG_PROTECT_SET,
  275. CONFIG_ENV_ADDR,
  276. CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
  277. &flash_info[0]);
  278. #endif
  279. return total;
  280. }
  281. /*-----------------------------------------------------------------------
  282. */
  283. void
  284. flash_print_info(flash_info_t *info)
  285. {
  286. int i;
  287. if (info->flash_id == FLASH_UNKNOWN) {
  288. printf ("missing or unknown FLASH type\n");
  289. return;
  290. }
  291. switch (info->flash_id & FLASH_VENDMASK) {
  292. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  293. default: printf ("Unknown Vendor "); break;
  294. }
  295. switch (info->flash_id & FLASH_TYPEMASK) {
  296. case FLASH_28F008S5: printf ("28F008S5\n");
  297. break;
  298. default: printf ("Unknown Chip Type\n");
  299. break;
  300. }
  301. printf (" Size: %ld MB in %d Sectors\n",
  302. info->size >> 20, info->sector_count);
  303. printf (" Sector Start Addresses:");
  304. for (i=0; i<info->sector_count; ++i) {
  305. if ((i % 4) == 0)
  306. printf ("\n ");
  307. printf (" %2d - %08lX%s", i,
  308. info->start[i],
  309. info->protect[i] ? " (RO)" : " "
  310. );
  311. }
  312. printf ("\n");
  313. return;
  314. }
  315. /*-----------------------------------------------------------------------
  316. */
  317. /*-----------------------------------------------------------------------
  318. */
  319. /*
  320. * The following code cannot be run from FLASH!
  321. */
  322. int
  323. flash_erase(flash_info_t *info, int s_first, int s_last)
  324. {
  325. int prot, sect, haderr;
  326. ulong start, now, last;
  327. void (*erase_init)(flash_info_t *, int);
  328. int (*erase_poll)(flash_info_t *, int);
  329. void (*reset)(flash_info_t *, int);
  330. int rcode = 0;
  331. #ifdef FLASH_DEBUG
  332. printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
  333. " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
  334. (info - flash_info) + 1);
  335. flash_print_info(info);
  336. #endif
  337. if ((s_first < 0) || (s_first > s_last)) {
  338. if (info->flash_id == FLASH_UNKNOWN) {
  339. printf ("- missing\n");
  340. } else {
  341. printf ("- no sectors to erase\n");
  342. }
  343. return 1;
  344. }
  345. switch (info->flash_id) {
  346. #if defined(CONFIG_CMA302)
  347. case FLASH_MAN_INTEL|FLASH_28F008S5:
  348. erase_init = c302f_erase_init;
  349. erase_poll = c302f_erase_poll;
  350. reset = c302f_reset;
  351. break;
  352. #endif
  353. #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
  354. case FLASH_MAN_INTEL|FLASH_28F800_B:
  355. case FLASH_MAN_AMD|FLASH_AM29F800B:
  356. /* not yet ...
  357. erase_init = cmbf_erase_init;
  358. erase_poll = cmbf_erase_poll;
  359. reset = cmbf_reset;
  360. break;
  361. */
  362. #endif
  363. default:
  364. printf ("Flash type %08lx not supported - aborted\n",
  365. info->flash_id);
  366. return 1;
  367. }
  368. prot = 0;
  369. for (sect=s_first; sect<=s_last; ++sect) {
  370. if (info->protect[sect]) {
  371. prot++;
  372. }
  373. }
  374. if (prot) {
  375. printf("- Warning: %d protected sector%s will not be erased!\n",
  376. prot, (prot > 1 ? "s" : ""));
  377. }
  378. start = get_timer (0);
  379. last = 0;
  380. haderr = 0;
  381. for (sect = s_first; sect <= s_last; sect++) {
  382. if (info->protect[sect] == 0) { /* not protected */
  383. ulong estart;
  384. int sectdone;
  385. (*erase_init)(info, sect);
  386. /* wait at least 80us - let's wait 1 ms */
  387. udelay (1000);
  388. estart = get_timer(start);
  389. do {
  390. now = get_timer(start);
  391. if (now - estart > CONFIG_SYS_FLASH_ERASE_TOUT) {
  392. printf ("Timeout (sect %d)\n", sect);
  393. haderr = 1;
  394. break;
  395. }
  396. #ifndef FLASH_DEBUG
  397. /* show that we're waiting */
  398. if ((now - last) > 1000) { /* every second */
  399. putc ('.');
  400. last = now;
  401. }
  402. #endif
  403. sectdone = (*erase_poll)(info, sect);
  404. if (sectdone < 0) {
  405. haderr = 1;
  406. break;
  407. }
  408. } while (!sectdone);
  409. if (haderr)
  410. break;
  411. }
  412. }
  413. if (haderr > 0) {
  414. printf (" failed\n");
  415. rcode = 1;
  416. }
  417. else
  418. printf (" done\n");
  419. /* reset to read mode */
  420. for (sect = s_first; sect <= s_last; sect++) {
  421. if (info->protect[sect] == 0) { /* not protected */
  422. (*reset)(info, sect);
  423. }
  424. }
  425. return rcode;
  426. }
  427. /*-----------------------------------------------------------------------
  428. * Copy memory to flash, returns:
  429. * 0 - OK
  430. * 1 - write timeout
  431. * 2 - Flash not erased
  432. * 3 - write error
  433. */
  434. int
  435. write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  436. {
  437. ulong cp, wp, data;
  438. int i, l, rc;
  439. ulong start, now, last;
  440. wp = (addr & ~3); /* get lower word aligned address */
  441. /*
  442. * handle unaligned start bytes
  443. */
  444. if ((l = addr - wp) != 0) {
  445. data = 0;
  446. for (i=0, cp=wp; i<l; ++i, ++cp) {
  447. data = (data << 8) | (*(uchar *)cp);
  448. }
  449. for (; i<4 && cnt>0; ++i) {
  450. data = (data << 8) | *src++;
  451. --cnt;
  452. ++cp;
  453. }
  454. for (; cnt==0 && i<4; ++i, ++cp) {
  455. data = (data << 8) | (*(uchar *)cp);
  456. }
  457. if ((rc = write_word(info, wp, data)) != 0) {
  458. return (rc);
  459. }
  460. wp += 4;
  461. }
  462. /*
  463. * handle word aligned part
  464. */
  465. start = get_timer (0);
  466. last = 0;
  467. while (cnt >= 4) {
  468. data = 0;
  469. for (i=0; i<4; ++i) {
  470. data = (data << 8) | *src++;
  471. }
  472. if ((rc = write_word(info, wp, data)) != 0) {
  473. return (rc);
  474. }
  475. wp += 4;
  476. cnt -= 4;
  477. /* show that we're waiting */
  478. now = get_timer(start);
  479. if ((now - last) > 1000) { /* every second */
  480. putc ('.');
  481. last = now;
  482. }
  483. }
  484. if (cnt == 0) {
  485. return (0);
  486. }
  487. /*
  488. * handle unaligned tail bytes
  489. */
  490. data = 0;
  491. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  492. data = (data << 8) | *src++;
  493. --cnt;
  494. }
  495. for (; i<4; ++i, ++cp) {
  496. data = (data << 8) | (*(uchar *)cp);
  497. }
  498. return (write_word(info, wp, data));
  499. }
  500. /*-----------------------------------------------------------------------
  501. * Write a word to Flash, returns:
  502. * 0 - OK
  503. * 1 - write timeout
  504. * 2 - Flash not erased
  505. * 3 - write error
  506. */
  507. static int
  508. write_word(flash_info_t *info, ulong dest, ulong data)
  509. {
  510. int retval;
  511. /* Check if Flash is (sufficiently) erased */
  512. if ((*(ulong *)dest & data) != data) {
  513. return (2);
  514. }
  515. switch (info->flash_id) {
  516. #if defined(CONFIG_CMA302)
  517. case FLASH_MAN_INTEL|FLASH_28F008S5:
  518. retval = c302f_write_word((c302f_addr_t)dest, (c302f_word_t)data);
  519. break;
  520. #endif
  521. #if (CMA_MB_CAPS & CMA_MB_CAP_FLASH)
  522. case FLASH_MAN_INTEL|FLASH_28F800_B:
  523. case FLASH_MAN_AMD|FLASH_AM29F800B:
  524. /* not yet ...
  525. retval = cmbf_write_word((cmbf_addr_t)dest, (cmbf_word_t)data);
  526. */
  527. retval = 3;
  528. break;
  529. #endif
  530. default:
  531. printf ("Flash type %08lx not supported - aborted\n",
  532. info->flash_id);
  533. retval = 3;
  534. break;
  535. }
  536. return (retval);
  537. }
  538. /*-----------------------------------------------------------------------
  539. */