flash.c 13 KB

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