flash.c 13 KB

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