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 <mpc8xx.h>
  25. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  30. static int write_word (flash_info_t *info, ulong dest, ulong data);
  31. static void flash_get_offsets (ulong base, flash_info_t *info);
  32. /*-----------------------------------------------------------------------
  33. * Protection Flags:
  34. */
  35. #define FLAG_PROTECT_SET 0x01
  36. #define FLAG_PROTECT_CLEAR 0x02
  37. /*-----------------------------------------------------------------------
  38. */
  39. unsigned long flash_init (void)
  40. {
  41. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  42. volatile memctl8xx_t *memctl = &immap->im_memctl;
  43. unsigned long size_b0, size_b1;
  44. int i;
  45. /*printf("faking");*/
  46. return(0x1fffff);
  47. /* Init: no FLASHes known */
  48. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i)
  49. {
  50. flash_info[i].flash_id = FLASH_UNKNOWN;
  51. }
  52. /* Static FLASH Bank configuration here - FIXME XXX */
  53. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  54. if (flash_info[0].flash_id == FLASH_UNKNOWN)
  55. {
  56. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  57. size_b0, size_b0<<20);
  58. }
  59. #if 0
  60. if (FLASH_BASE1_PRELIM != 0x0) {
  61. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  62. if (size_b1 > size_b0) {
  63. printf ("## ERROR: Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  64. size_b1, size_b1<<20,size_b0, size_b0<<20);
  65. flash_info[0].flash_id = FLASH_UNKNOWN;
  66. flash_info[1].flash_id = FLASH_UNKNOWN;
  67. flash_info[0].sector_count = -1;
  68. flash_info[1].sector_count = -1;
  69. flash_info[0].size = 0;
  70. flash_info[1].size = 0;
  71. return (0);
  72. }
  73. } else {
  74. #endif
  75. size_b1 = 0;
  76. /* Remap FLASH according to real size */
  77. memctl->memc_or0 = CFG_OR0_PRELIM;
  78. memctl->memc_br0 = CFG_BR0_PRELIM;
  79. /* Re-do sizing to get full correct info */
  80. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  81. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  82. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  83. /* monitor protection ON by default */
  84. (void)flash_protect(FLAG_PROTECT_SET,
  85. CFG_MONITOR_BASE,
  86. CFG_MONITOR_BASE+monitor_flash_len-1,
  87. &flash_info[0]);
  88. #endif
  89. if (size_b1)
  90. {
  91. /* memctl->memc_or1 = CFG_OR1_PRELIM;
  92. memctl->memc_br1 = CFG_BR1_PRELIM; */
  93. /* Re-do sizing to get full correct info */
  94. size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
  95. &flash_info[1]);
  96. flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
  97. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  98. /* monitor protection ON by default */
  99. (void)flash_protect(FLAG_PROTECT_SET,
  100. CFG_MONITOR_BASE,
  101. CFG_MONITOR_BASE+monitor_flash_len-1,
  102. &flash_info[1]);
  103. #endif
  104. }
  105. else
  106. {
  107. /* memctl->memc_or1 = CFG_OR1_PRELIM;
  108. FIXME memctl->memc_br1 = CFG_BR1_PRELIM; */
  109. flash_info[1].flash_id = FLASH_UNKNOWN;
  110. flash_info[1].sector_count = -1;
  111. }
  112. flash_info[0].size = size_b0;
  113. flash_info[1].size = size_b1;
  114. return (size_b0 + size_b1);
  115. }
  116. static void flash_get_offsets (ulong base, flash_info_t *info)
  117. {
  118. int i;
  119. /* set up sector start adress table */
  120. if (info->flash_id & FLASH_BTYPE)
  121. {
  122. /* set sector offsets for bottom boot block type */
  123. for (i = 0; i < info->sector_count; i++)
  124. {
  125. info->start[i] = base + (i * 0x00040000);
  126. }
  127. }
  128. else
  129. {
  130. /* set sector offsets for top boot block type */
  131. i = info->sector_count - 1;
  132. for (; i >= 0; i--)
  133. {
  134. info->start[i] = base + i * 0x00040000;
  135. }
  136. }
  137. }
  138. /*-----------------------------------------------------------------------
  139. */
  140. void flash_print_info (flash_info_t *info)
  141. {
  142. int i;
  143. if (info->flash_id == FLASH_UNKNOWN) {
  144. printf ("missing or unknown FLASH type\n");
  145. return;
  146. }
  147. switch (info->flash_id & FLASH_VENDMASK) {
  148. case FLASH_MAN_AMD: printf ("AMD "); break;
  149. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  150. default: printf ("Unknown Vendor "); break;
  151. }
  152. switch (info->flash_id & FLASH_TYPEMASK) {
  153. #if 0
  154. case FLASH_AM040B:
  155. printf ("AM29F040B (4 Mbit, bottom boot sect)\n");
  156. break;
  157. case FLASH_AM040T:
  158. printf ("AM29F040T (4 Mbit, top boot sect)\n");
  159. break;
  160. #endif
  161. case FLASH_AM400B:
  162. printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  163. break;
  164. case FLASH_AM400T:
  165. printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  166. break;
  167. case FLASH_AM800B:
  168. printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  169. break;
  170. case FLASH_AM800T:
  171. printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  172. break;
  173. case FLASH_AM160B:
  174. printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  175. break;
  176. case FLASH_AM160T:
  177. printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  178. break;
  179. case FLASH_AM320B:
  180. printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  181. break;
  182. case FLASH_AM320T:
  183. printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  184. break;
  185. default:
  186. printf ("Unknown Chip Type\n");
  187. break;
  188. }
  189. printf (" Size: %ld MB in %d Sectors\n",
  190. info->size >> 20,
  191. info->sector_count);
  192. printf (" Sector Start Addresses:");
  193. for (i=0; i<info->sector_count; ++i)
  194. {
  195. if ((i % 5) == 0)
  196. {
  197. printf ("\n ");
  198. }
  199. printf (" %08lX%s",
  200. info->start[i],
  201. info->protect[i] ? " (RO)" : " ");
  202. }
  203. printf ("\n");
  204. return;
  205. }
  206. /*-----------------------------------------------------------------------
  207. */
  208. /*-----------------------------------------------------------------------
  209. */
  210. /*
  211. * The following code cannot be run from FLASH!
  212. */
  213. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  214. {
  215. short i;
  216. #if 0
  217. ulong base = (ulong)addr;
  218. #endif
  219. uchar value;
  220. /* Write auto select command: read Manufacturer ID */
  221. #if 0
  222. addr[0x0555] = 0x00AA00AA;
  223. addr[0x02AA] = 0x00550055;
  224. addr[0x0555] = 0x00900090;
  225. #else
  226. addr[0x0555] = 0xAAAAAAAA;
  227. addr[0x02AA] = 0x55555555;
  228. addr[0x0555] = 0x90909090;
  229. #endif
  230. value = addr[0];
  231. switch (value)
  232. {
  233. case AMD_MANUFACT:case 0x01:
  234. info->flash_id = FLASH_MAN_AMD;
  235. break;
  236. case FUJ_MANUFACT:
  237. info->flash_id = FLASH_MAN_FUJ;
  238. break;
  239. default:
  240. info->flash_id = FLASH_UNKNOWN;
  241. info->sector_count = 0;
  242. info->size = 0;
  243. break;
  244. }
  245. value = addr[1]; /* device ID */
  246. switch (value)
  247. {
  248. #if 0
  249. case AMD_ID_F040B:
  250. info->flash_id += FLASH_AM040B;
  251. info->sector_count = 8;
  252. info->size = 0x00200000;
  253. break; /* => 2 MB */
  254. #endif
  255. case AMD_ID_LV400T:
  256. info->flash_id += FLASH_AM400T;
  257. info->sector_count = 11;
  258. info->size = 0x00100000;
  259. break; /* => 1 MB */
  260. case AMD_ID_LV400B:
  261. info->flash_id += FLASH_AM400B;
  262. info->sector_count = 11;
  263. info->size = 0x00100000;
  264. break; /* => 1 MB */
  265. case AMD_ID_LV800T:
  266. info->flash_id += FLASH_AM800T;
  267. info->sector_count = 19;
  268. info->size = 0x00200000;
  269. break; /* => 2 MB */
  270. case AMD_ID_LV800B:
  271. info->flash_id += FLASH_AM800B;
  272. info->sector_count = 19;
  273. info->size = 0x00200000;
  274. break; /* => 2 MB */
  275. case AMD_ID_LV160T:
  276. info->flash_id += FLASH_AM160T;
  277. info->sector_count = 35;
  278. info->size = 0x00400000;
  279. break; /* => 4 MB */
  280. case AMD_ID_LV160B:
  281. info->flash_id += FLASH_AM160B;
  282. info->sector_count = 35;
  283. info->size = 0x00400000;
  284. break; /* => 4 MB */
  285. #if 0 /* enable when device IDs are available */
  286. case AMD_ID_LV320T:
  287. info->flash_id += FLASH_AM320T;
  288. info->sector_count = 67;
  289. info->size = 0x00800000;
  290. break; /* => 8 MB */
  291. case AMD_ID_LV320B:
  292. info->flash_id += FLASH_AM320B;
  293. info->sector_count = 67;
  294. info->size = 0x00800000;
  295. break; /* => 8 MB */
  296. #endif
  297. default:
  298. info->flash_id = FLASH_UNKNOWN;
  299. return (0); /* => no or unknown flash */
  300. }
  301. #if 0
  302. /* set up sector start adress table */
  303. if (info->flash_id & FLASH_BTYPE) {
  304. /* set sector offsets for bottom boot block type */
  305. info->start[0] = base + 0x00000000;
  306. info->start[1] = base + 0x00008000;
  307. info->start[2] = base + 0x0000C000;
  308. info->start[3] = base + 0x00010000;
  309. for (i = 4; i < info->sector_count; i++) {
  310. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  311. }
  312. } else {
  313. /* set sector offsets for top boot block type */
  314. i = info->sector_count - 1;
  315. info->start[i--] = base + info->size - 0x00008000;
  316. info->start[i--] = base + info->size - 0x0000C000;
  317. info->start[i--] = base + info->size - 0x00010000;
  318. for (; i >= 0; i--) {
  319. info->start[i] = base + i * 0x00020000;
  320. }
  321. }
  322. #else
  323. flash_get_offsets ((ulong)addr, &flash_info[0]);
  324. #endif
  325. /* check for protected sectors */
  326. for (i = 0; i < info->sector_count; i++)
  327. {
  328. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  329. /* D0 = 1 if protected */
  330. addr = (volatile unsigned long *)(info->start[i]);
  331. info->protect[i] = addr[2] & 1;
  332. }
  333. /*
  334. * Prevent writes to uninitialized FLASH.
  335. */
  336. if (info->flash_id != FLASH_UNKNOWN)
  337. {
  338. addr = (volatile unsigned long *)info->start[0];
  339. #if 0
  340. *addr = 0x00F000F0; /* reset bank */
  341. #else
  342. *addr = 0xF0F0F0F0; /* reset bank */
  343. #endif
  344. }
  345. return (info->size);
  346. }
  347. /*-----------------------------------------------------------------------
  348. */
  349. int flash_erase (flash_info_t *info, int s_first, int s_last)
  350. {
  351. vu_long *addr = (vu_long*)(info->start[0]);
  352. int flag, prot, sect, l_sect;
  353. ulong start, now, last;
  354. if ((s_first < 0) || (s_first > s_last)) {
  355. if (info->flash_id == FLASH_UNKNOWN) {
  356. printf ("- missing\n");
  357. } else {
  358. printf ("- no sectors to erase\n");
  359. }
  360. return 1;
  361. }
  362. if ((info->flash_id == FLASH_UNKNOWN) ||
  363. (info->flash_id > FLASH_AMD_COMP)) {
  364. printf ("Can't erase unknown flash type - aborted\n");
  365. return 1;
  366. }
  367. prot = 0;
  368. for (sect=s_first; sect<=s_last; ++sect) {
  369. if (info->protect[sect]) {
  370. prot++;
  371. }
  372. }
  373. if (prot) {
  374. printf ("- Warning: %d protected sectors will not be erased!\n",
  375. prot);
  376. } else {
  377. printf ("\n");
  378. }
  379. l_sect = -1;
  380. /* Disable interrupts which might cause a timeout here */
  381. flag = disable_interrupts();
  382. #if 0
  383. addr[0x0555] = 0x00AA00AA;
  384. addr[0x02AA] = 0x00550055;
  385. addr[0x0555] = 0x00800080;
  386. addr[0x0555] = 0x00AA00AA;
  387. addr[0x02AA] = 0x00550055;
  388. #else
  389. addr[0x0555] = 0xAAAAAAAA;
  390. addr[0x02AA] = 0x55555555;
  391. addr[0x0555] = 0x80808080;
  392. addr[0x0555] = 0xAAAAAAAA;
  393. addr[0x02AA] = 0x55555555;
  394. #endif
  395. /* Start erase on unprotected sectors */
  396. for (sect = s_first; sect<=s_last; sect++) {
  397. if (info->protect[sect] == 0) { /* not protected */
  398. addr = (vu_long*)(info->start[sect]);
  399. #if 0
  400. addr[0] = 0x00300030;
  401. #else
  402. addr[0] = 0x30303030;
  403. #endif
  404. l_sect = sect;
  405. }
  406. }
  407. /* re-enable interrupts if necessary */
  408. if (flag)
  409. enable_interrupts();
  410. /* wait at least 80us - let's wait 1 ms */
  411. udelay (1000);
  412. /*
  413. * We wait for the last triggered sector
  414. */
  415. if (l_sect < 0)
  416. goto DONE;
  417. start = get_timer (0);
  418. last = start;
  419. addr = (vu_long*)(info->start[l_sect]);
  420. #if 0
  421. while ((addr[0] & 0x00800080) != 0x00800080)
  422. #else
  423. while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
  424. #endif
  425. {
  426. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  427. printf ("Timeout\n");
  428. return 1;
  429. }
  430. /* show that we're waiting */
  431. if ((now - last) > 1000) { /* every second */
  432. putc ('.');
  433. last = now;
  434. }
  435. }
  436. DONE:
  437. /* reset to read mode */
  438. addr = (volatile unsigned long *)info->start[0];
  439. #if 0
  440. addr[0] = 0x00F000F0; /* reset bank */
  441. #else
  442. addr[0] = 0xF0F0F0F0; /* reset bank */
  443. #endif
  444. printf (" done\n");
  445. return 0;
  446. }
  447. /*-----------------------------------------------------------------------
  448. * Copy memory to flash, returns:
  449. * 0 - OK
  450. * 1 - write timeout
  451. * 2 - Flash not erased
  452. */
  453. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  454. {
  455. ulong cp, wp, data;
  456. int i, l, rc;
  457. wp = (addr & ~3); /* get lower word aligned address */
  458. /*
  459. * handle unaligned start bytes
  460. */
  461. if ((l = addr - wp) != 0) {
  462. data = 0;
  463. for (i=0, cp=wp; i<l; ++i, ++cp) {
  464. data = (data << 8) | (*(uchar *)cp);
  465. }
  466. for (; i<4 && cnt>0; ++i) {
  467. data = (data << 8) | *src++;
  468. --cnt;
  469. ++cp;
  470. }
  471. for (; cnt==0 && i<4; ++i, ++cp) {
  472. data = (data << 8) | (*(uchar *)cp);
  473. }
  474. if ((rc = write_word(info, wp, data)) != 0) {
  475. return (rc);
  476. }
  477. wp += 4;
  478. }
  479. /*
  480. * handle word aligned part
  481. */
  482. while (cnt >= 4) {
  483. data = 0;
  484. for (i=0; i<4; ++i) {
  485. data = (data << 8) | *src++;
  486. }
  487. if ((rc = write_word(info, wp, data)) != 0) {
  488. return (rc);
  489. }
  490. wp += 4;
  491. cnt -= 4;
  492. }
  493. if (cnt == 0) {
  494. return (0);
  495. }
  496. /*
  497. * handle unaligned tail bytes
  498. */
  499. data = 0;
  500. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  501. data = (data << 8) | *src++;
  502. --cnt;
  503. }
  504. for (; i<4; ++i, ++cp) {
  505. data = (data << 8) | (*(uchar *)cp);
  506. }
  507. return (write_word(info, wp, data));
  508. }
  509. /*-----------------------------------------------------------------------
  510. * Write a word to Flash, returns:
  511. * 0 - OK
  512. * 1 - write timeout
  513. * 2 - Flash not erased
  514. */
  515. static int write_word (flash_info_t *info, ulong dest, ulong data)
  516. {
  517. vu_long *addr = (vu_long*)(info->start[0]);
  518. ulong start;
  519. int flag;
  520. /* Check if Flash is (sufficiently) erased */
  521. if ((*((vu_long *)dest) & data) != data) {
  522. return (2);
  523. }
  524. /* Disable interrupts which might cause a timeout here */
  525. flag = disable_interrupts();
  526. #if 0
  527. addr[0x0555] = 0x00AA00AA;
  528. addr[0x02AA] = 0x00550055;
  529. addr[0x0555] = 0x00A000A0;
  530. #else
  531. addr[0x0555] = 0xAAAAAAAA;
  532. addr[0x02AA] = 0x55555555;
  533. addr[0x0555] = 0xA0A0A0A0;
  534. #endif
  535. *((vu_long *)dest) = data;
  536. /* re-enable interrupts if necessary */
  537. if (flag)
  538. enable_interrupts();
  539. /* data polling for D7 */
  540. start = get_timer (0);
  541. #if 0
  542. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
  543. #else
  544. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
  545. #endif
  546. {
  547. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  548. return (1);
  549. }
  550. }
  551. return (0);
  552. }
  553. /*-----------------------------------------------------------------------
  554. */