flash.c 12 KB

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