flash.c 11 KB

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