flash.c 12 KB

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