flash.c 13 KB

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