flash.c 12 KB

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