flash.c 12 KB

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