flash.c 12 KB

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