flash.c 12 KB

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