flash.c 13 KB

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