flash.c 12 KB

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