flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #include <common.h>
  24. #include <mpc8xx.h>
  25. #ifndef CFG_ENV_ADDR
  26. #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_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 CFG_ENV_IS_IN_FLASH
  66. /* ENV protection ON by default */
  67. flash_protect(FLAG_PROTECT_SET,
  68. CFG_ENV_ADDR,
  69. CFG_ENV_ADDR+CFG_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 FUJ_MANUFACT:
  144. info->flash_id = FLASH_MAN_FUJ;
  145. break;
  146. default:
  147. info->flash_id = FLASH_UNKNOWN;
  148. info->sector_count = 0;
  149. info->size = 0;
  150. return (0); /* no or unknown flash */
  151. }
  152. value = s_addr[1];
  153. value = value|(value<<16);
  154. switch (value) {
  155. case FUJI_ID_29F800BA:
  156. info->flash_id += FLASH_AM400T;
  157. info->sector_count = 19;
  158. info->size = 0x00100000;
  159. break; /* => 1 MB */
  160. default:
  161. info->flash_id = FLASH_UNKNOWN;
  162. return (0); /* => no or unknown flash */
  163. }
  164. /* set up sector start address table */
  165. /* set sector offsets for bottom boot block type */
  166. info->start[0] = base + 0x00000000;
  167. info->start[1] = base + 0x00004000;
  168. info->start[2] = base + 0x00006000;
  169. info->start[3] = base + 0x00008000;
  170. for (i = 4; i < info->sector_count; i++) {
  171. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  172. }
  173. /* check for protected sectors */
  174. for (i = 0; i < info->sector_count; i++) {
  175. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  176. /* D0 = 1 if protected */
  177. s_addr = (volatile unsigned short *)(info->start[i]);
  178. info->protect[i] = s_addr[2] & 1;
  179. }
  180. /*
  181. * Prevent writes to uninitialized FLASH.
  182. */
  183. if (info->flash_id != FLASH_UNKNOWN) {
  184. s_addr = (volatile unsigned short *)info->start[0];
  185. *s_addr = 0x00F0; /* reset bank */
  186. }
  187. return (info->size);
  188. }
  189. /*-----------------------------------------------------------------------
  190. */
  191. int flash_erase (flash_info_t *info, int s_first, int s_last)
  192. {
  193. vu_long *addr = (vu_long*)(info->start[0]);
  194. int flag, prot, sect;
  195. ulong start, now, last;
  196. #ifdef CONFIG_FLASH_16BIT
  197. vu_short *s_addr = (vu_short*)addr;
  198. #endif
  199. if ((s_first < 0) || (s_first > s_last)) {
  200. if (info->flash_id == FLASH_UNKNOWN) {
  201. printf ("- missing\n");
  202. } else {
  203. printf ("- no sectors to erase\n");
  204. }
  205. return 1;
  206. }
  207. /*#ifndef CONFIG_FLASH_16BIT
  208. ulong type;
  209. type = (info->flash_id & FLASH_VENDMASK);
  210. if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
  211. printf ("Can't erase unknown flash type %08lx - aborted\n",
  212. info->flash_id);
  213. return;
  214. }
  215. #endif*/
  216. prot = 0;
  217. for (sect=s_first; sect<=s_last; ++sect) {
  218. if (info->protect[sect]) {
  219. prot++;
  220. }
  221. }
  222. if (prot) {
  223. printf ("- Warning: %d protected sectors will not be erased!\n",
  224. prot);
  225. } else {
  226. printf ("\n");
  227. }
  228. start = get_timer (0);
  229. last = start;
  230. /* Start erase on unprotected sectors */
  231. for (sect = s_first; sect<=s_last; sect++) {
  232. if (info->protect[sect] == 0) { /* not protected */
  233. #ifdef CONFIG_FLASH_16BIT
  234. vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
  235. #else
  236. vu_long *sect_addr = (vu_long*)(info->start[sect]);
  237. #endif
  238. /* Disable interrupts which might cause a timeout here */
  239. flag = disable_interrupts();
  240. #ifdef CONFIG_FLASH_16BIT
  241. /*printf("\ns_sect_addr=%x",s_sect_addr);*/
  242. s_addr[0x5555] = 0x00AA;
  243. s_addr[0x2AAA] = 0x0055;
  244. s_addr[0x5555] = 0x0080;
  245. s_addr[0x5555] = 0x00AA;
  246. s_addr[0x2AAA] = 0x0055;
  247. s_sect_addr[0] = 0x0030;
  248. #else
  249. addr[0x5555] = 0x00AA00AA;
  250. addr[0x2AAA] = 0x00550055;
  251. addr[0x5555] = 0x00800080;
  252. addr[0x5555] = 0x00AA00AA;
  253. addr[0x2AAA] = 0x00550055;
  254. sect_addr[0] = 0x00300030;
  255. #endif
  256. /* re-enable interrupts if necessary */
  257. if (flag)
  258. enable_interrupts();
  259. /* wait at least 80us - let's wait 1 ms */
  260. udelay (1000);
  261. #ifdef CONFIG_FLASH_16BIT
  262. while ((s_sect_addr[0] & 0x0080) != 0x0080) {
  263. #else
  264. while ((sect_addr[0] & 0x00800080) != 0x00800080) {
  265. #endif
  266. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  267. printf ("Timeout\n");
  268. return 1;
  269. }
  270. /* show that we're waiting */
  271. if ((now - last) > 1000) { /* every second */
  272. putc ('.');
  273. last = now;
  274. }
  275. }
  276. }
  277. }
  278. /* reset to read mode */
  279. addr = (volatile unsigned long *)info->start[0];
  280. #ifdef CONFIG_FLASH_16BIT
  281. s_addr[0] = 0x00F0; /* reset bank */
  282. #else
  283. addr[0] = 0x00F000F0; /* reset bank */
  284. #endif
  285. printf (" done\n");
  286. return 0;
  287. }
  288. /*-----------------------------------------------------------------------
  289. * Copy memory to flash, returns:
  290. * 0 - OK
  291. * 1 - write timeout
  292. * 2 - Flash not erased
  293. */
  294. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  295. {
  296. ulong cp, wp, data;
  297. int i, l, rc;
  298. wp = (addr & ~3); /* get lower word aligned address */
  299. /*
  300. * handle unaligned start bytes
  301. */
  302. if ((l = addr - wp) != 0) {
  303. data = 0;
  304. for (i=0, cp=wp; i<l; ++i, ++cp) {
  305. data = (data << 8) | (*(uchar *)cp);
  306. }
  307. for (; i<4 && cnt>0; ++i) {
  308. data = (data << 8) | *src++;
  309. --cnt;
  310. ++cp;
  311. }
  312. for (; cnt==0 && i<4; ++i, ++cp) {
  313. data = (data << 8) | (*(uchar *)cp);
  314. }
  315. if ((rc = write_word(info, wp, data)) != 0) {
  316. return (rc);
  317. }
  318. wp += 4;
  319. }
  320. /*
  321. * handle word aligned part
  322. */
  323. while (cnt >= 4) {
  324. data = 0;
  325. for (i=0; i<4; ++i) {
  326. data = (data << 8) | *src++;
  327. }
  328. if ((rc = write_word(info, wp, data)) != 0) {
  329. return (rc);
  330. }
  331. wp += 4;
  332. cnt -= 4;
  333. }
  334. if (cnt == 0) {
  335. return (0);
  336. }
  337. /*
  338. * handle unaligned tail bytes
  339. */
  340. data = 0;
  341. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  342. data = (data << 8) | *src++;
  343. --cnt;
  344. }
  345. for (; i<4; ++i, ++cp) {
  346. data = (data << 8) | (*(uchar *)cp);
  347. }
  348. return (write_word(info, wp, data));
  349. }
  350. /*-----------------------------------------------------------------------
  351. * Write a word to Flash, returns:
  352. * 0 - OK
  353. * 1 - write timeout
  354. * 2 - Flash not erased
  355. */
  356. static int write_word (flash_info_t *info, ulong dest, ulong data)
  357. {
  358. vu_long *addr = (vu_long*)(info->start[0]);
  359. #ifdef CONFIG_FLASH_16BIT
  360. vu_short high_data;
  361. vu_short low_data;
  362. vu_short *s_addr = (vu_short*)addr;
  363. #endif
  364. ulong start;
  365. int flag;
  366. /* Check if Flash is (sufficiently) erased */
  367. if ((*((vu_long *)dest) & data) != data) {
  368. return (2);
  369. }
  370. #ifdef CONFIG_FLASH_16BIT
  371. /* Write the 16 higher-bits */
  372. /* Disable interrupts which might cause a timeout here */
  373. flag = disable_interrupts();
  374. high_data = ((data>>16) & 0x0000ffff);
  375. s_addr[0x5555] = 0x00AA;
  376. s_addr[0x2AAA] = 0x0055;
  377. s_addr[0x5555] = 0x00A0;
  378. *((vu_short *)dest) = high_data;
  379. /* re-enable interrupts if necessary */
  380. if (flag)
  381. enable_interrupts();
  382. /* data polling for D7 */
  383. start = get_timer (0);
  384. while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
  385. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  386. return (1);
  387. }
  388. }
  389. /* Write the 16 lower-bits */
  390. #endif
  391. /* Disable interrupts which might cause a timeout here */
  392. flag = disable_interrupts();
  393. #ifdef CONFIG_FLASH_16BIT
  394. dest += 0x2;
  395. low_data = (data & 0x0000ffff);
  396. s_addr[0x5555] = 0x00AA;
  397. s_addr[0x2AAA] = 0x0055;
  398. s_addr[0x5555] = 0x00A0;
  399. *((vu_short *)dest) = low_data;
  400. #else
  401. addr[0x5555] = 0x00AA00AA;
  402. addr[0x2AAA] = 0x00550055;
  403. addr[0x5555] = 0x00A000A0;
  404. *((vu_long *)dest) = data;
  405. #endif
  406. /* re-enable interrupts if necessary */
  407. if (flag)
  408. enable_interrupts();
  409. /* data polling for D7 */
  410. start = get_timer (0);
  411. #ifdef CONFIG_FLASH_16BIT
  412. while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
  413. #else
  414. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  415. #endif
  416. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  417. return (1);
  418. }
  419. }
  420. return (0);
  421. }