flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /* #define DEBUG */
  11. #include <common.h>
  12. #include <mpc8xx.h>
  13. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  14. #if defined(CONFIG_ENV_IS_IN_FLASH)
  15. # ifndef CONFIG_ENV_ADDR
  16. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  17. # endif
  18. # ifndef CONFIG_ENV_SIZE
  19. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  20. # endif
  21. # ifndef CONFIG_ENV_SECT_SIZE
  22. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  23. # endif
  24. #endif
  25. /*-----------------------------------------------------------------------
  26. * Protection Flags:
  27. */
  28. #define FLAG_PROTECT_SET 0x01
  29. #define FLAG_PROTECT_CLEAR 0x02
  30. /* Board support for 1 or 2 flash devices */
  31. #undef FLASH_PORT_WIDTH32
  32. #define FLASH_PORT_WIDTH16
  33. #ifdef FLASH_PORT_WIDTH16
  34. #define FLASH_PORT_WIDTH ushort
  35. #define FLASH_PORT_WIDTHV vu_short
  36. #else
  37. #define FLASH_PORT_WIDTH ulong
  38. #define FLASH_PORT_WIDTHV vu_long
  39. #endif
  40. #define FPW FLASH_PORT_WIDTH
  41. #define FPWV FLASH_PORT_WIDTHV
  42. /*-----------------------------------------------------------------------
  43. * Functions
  44. */
  45. static ulong flash_get_size (FPW * addr, flash_info_t * info);
  46. static int write_data (flash_info_t * info, ulong dest, FPW data);
  47. static void flash_get_offsets (ulong base, flash_info_t * info);
  48. /*-----------------------------------------------------------------------
  49. */
  50. unsigned long flash_init (void)
  51. {
  52. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  53. volatile memctl8xx_t *memctl = &immap->im_memctl;
  54. unsigned long size_b0;
  55. int i;
  56. /* Init: no FLASHes known */
  57. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  58. flash_info[i].flash_id = FLASH_UNKNOWN;
  59. }
  60. /* Static FLASH Bank configuration here - FIXME XXX */
  61. size_b0 = flash_get_size ((FPW *) FLASH_BASE0_PRELIM, &flash_info[0]);
  62. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  63. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  64. size_b0, size_b0 << 20);
  65. }
  66. /* Remap FLASH according to real size */
  67. memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  68. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
  69. /* Re-do sizing to get full correct info */
  70. size_b0 = flash_get_size ((FPW *) CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  71. flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  72. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  73. /* monitor protection ON by default */
  74. (void) flash_protect (FLAG_PROTECT_SET,
  75. CONFIG_SYS_FLASH_BASE,
  76. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  77. &flash_info[0]);
  78. #endif
  79. #ifdef CONFIG_ENV_IS_IN_FLASH
  80. /* ENV protection ON by default */
  81. flash_protect (FLAG_PROTECT_SET,
  82. CONFIG_ENV_ADDR,
  83. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  84. &flash_info[0]);
  85. #endif
  86. flash_info[0].size = size_b0;
  87. return (size_b0);
  88. }
  89. /*-----------------------------------------------------------------------
  90. */
  91. static void flash_get_offsets (ulong base, flash_info_t * info)
  92. {
  93. int i;
  94. if (info->flash_id == FLASH_UNKNOWN) {
  95. return;
  96. }
  97. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  98. for (i = 0; i < info->sector_count; i++) {
  99. info->start[i] = base + (i * 0x00020000);
  100. }
  101. }
  102. }
  103. /*-----------------------------------------------------------------------
  104. */
  105. void flash_print_info (flash_info_t * info)
  106. {
  107. int i;
  108. if (info->flash_id == FLASH_UNKNOWN) {
  109. printf ("missing or unknown FLASH type\n");
  110. return;
  111. }
  112. switch (info->flash_id & FLASH_VENDMASK) {
  113. case FLASH_MAN_INTEL:
  114. printf ("INTEL ");
  115. break;
  116. default:
  117. printf ("Unknown Vendor ");
  118. break;
  119. }
  120. switch (info->flash_id & FLASH_TYPEMASK) {
  121. case FLASH_28F320J3A:
  122. printf ("28F320J3A\n");
  123. break;
  124. case FLASH_28F640J3A:
  125. printf ("28F640J3A\n");
  126. break;
  127. case FLASH_28F128J3A:
  128. printf ("28F128J3A\n");
  129. break;
  130. default:
  131. printf ("Unknown Chip Type\n");
  132. break;
  133. }
  134. printf (" Size: %ld MB in %d Sectors\n",
  135. info->size >> 20, info->sector_count);
  136. printf (" Sector Start Addresses:");
  137. for (i = 0; i < info->sector_count; ++i) {
  138. if ((i % 5) == 0)
  139. printf ("\n ");
  140. printf (" %08lX%s",
  141. info->start[i],
  142. info->protect[i] ? " (RO)" : " ");
  143. }
  144. printf ("\n");
  145. return;
  146. }
  147. /*-----------------------------------------------------------------------
  148. */
  149. /*-----------------------------------------------------------------------
  150. */
  151. /*
  152. * The following code cannot be run from FLASH!
  153. */
  154. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  155. {
  156. FPW value;
  157. /* Make sure Block Lock Bits get cleared */
  158. addr[0] = (FPW) 0x00FF00FF;
  159. addr[0] = (FPW) 0x00600060;
  160. addr[0] = (FPW) 0x00D000D0;
  161. addr[0] = (FPW) 0x00FF00FF;
  162. /* Write auto select command: read Manufacturer ID */
  163. addr[0x5555] = (FPW) 0x00AA00AA;
  164. addr[0x2AAA] = (FPW) 0x00550055;
  165. addr[0x5555] = (FPW) 0x00900090;
  166. value = addr[0];
  167. debug("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, value);
  168. switch (value) {
  169. case (FPW) INTEL_MANUFACT:
  170. info->flash_id = FLASH_MAN_INTEL;
  171. break;
  172. default:
  173. info->flash_id = FLASH_UNKNOWN;
  174. info->sector_count = 0;
  175. info->size = 0;
  176. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  177. return (0); /* no or unknown flash */
  178. }
  179. value = addr[1]; /* device ID */
  180. debug("Device ID @ 0x%08lx: 0x%08x\n", (ulong)(&addr[1]), value);
  181. switch (value) {
  182. case (FPW) INTEL_ID_28F320J3A:
  183. info->flash_id += FLASH_28F320J3A;
  184. info->sector_count = 32;
  185. info->size = 0x00400000;
  186. break; /* => 4 MB */
  187. case (FPW) INTEL_ID_28F640J3A:
  188. info->flash_id += FLASH_28F640J3A;
  189. info->sector_count = 64;
  190. info->size = 0x00800000;
  191. break; /* => 8 MB */
  192. case (FPW) INTEL_ID_28F128J3A:
  193. info->flash_id += FLASH_28F128J3A;
  194. info->sector_count = 128;
  195. info->size = 0x01000000;
  196. break; /* => 16 MB */
  197. default:
  198. info->flash_id = FLASH_UNKNOWN;
  199. break;
  200. }
  201. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  202. printf ("** ERROR: sector count %d > max (%d) **\n",
  203. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  204. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  205. }
  206. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  207. return (info->size);
  208. }
  209. /*-----------------------------------------------------------------------
  210. */
  211. int flash_erase (flash_info_t * info, int s_first, int s_last)
  212. {
  213. int flag, prot, sect;
  214. ulong type, start, now, last;
  215. int rcode = 0;
  216. if ((s_first < 0) || (s_first > s_last)) {
  217. if (info->flash_id == FLASH_UNKNOWN) {
  218. printf ("- missing\n");
  219. } else {
  220. printf ("- no sectors to erase\n");
  221. }
  222. return 1;
  223. }
  224. type = (info->flash_id & FLASH_VENDMASK);
  225. if ((type != FLASH_MAN_INTEL)) {
  226. printf ("Can't erase unknown flash type %08lx - aborted\n",
  227. info->flash_id);
  228. return 1;
  229. }
  230. prot = 0;
  231. for (sect = s_first; sect <= s_last; ++sect) {
  232. if (info->protect[sect]) {
  233. prot++;
  234. }
  235. }
  236. if (prot) {
  237. printf ("- Warning: %d protected sectors will not be erased!\n",
  238. prot);
  239. } else {
  240. printf ("\n");
  241. }
  242. start = get_timer (0);
  243. last = start;
  244. /* Start erase on unprotected sectors */
  245. for (sect = s_first; sect <= s_last; sect++) {
  246. if (info->protect[sect] == 0) { /* not protected */
  247. FPWV *addr = (FPWV *) (info->start[sect]);
  248. FPW status;
  249. /* Disable interrupts which might cause a timeout here */
  250. flag = disable_interrupts ();
  251. *addr = (FPW) 0x00500050; /* clear status register */
  252. *addr = (FPW) 0x00200020; /* erase setup */
  253. *addr = (FPW) 0x00D000D0; /* erase confirm */
  254. /* re-enable interrupts if necessary */
  255. if (flag)
  256. enable_interrupts ();
  257. /* wait at least 80us - let's wait 1 ms */
  258. udelay (1000);
  259. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  260. if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  261. printf ("Timeout\n");
  262. *addr = (FPW) 0x00B000B0; /* suspend erase */
  263. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  264. rcode = 1;
  265. break;
  266. }
  267. /* show that we're waiting */
  268. if ((now - last) > 1000) { /* every second */
  269. putc ('.');
  270. last = now;
  271. }
  272. }
  273. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  274. }
  275. }
  276. printf (" done\n");
  277. return rcode;
  278. }
  279. /*-----------------------------------------------------------------------
  280. * Copy memory to flash, returns:
  281. * 0 - OK
  282. * 1 - write timeout
  283. * 2 - Flash not erased
  284. * 4 - Flash not identified
  285. */
  286. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  287. {
  288. ulong cp, wp;
  289. FPW data;
  290. int i, l, rc, port_width;
  291. if (info->flash_id == FLASH_UNKNOWN) {
  292. return 4;
  293. }
  294. /* get lower word aligned address */
  295. #ifdef FLASH_PORT_WIDTH16
  296. wp = (addr & ~1);
  297. port_width = 2;
  298. #else
  299. wp = (addr & ~3);
  300. port_width = 4;
  301. #endif
  302. /*
  303. * handle unaligned start bytes
  304. */
  305. if ((l = addr - wp) != 0) {
  306. data = 0;
  307. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  308. data = (data << 8) | (*(uchar *) cp);
  309. }
  310. for (; i < port_width && cnt > 0; ++i) {
  311. data = (data << 8) | *src++;
  312. --cnt;
  313. ++cp;
  314. }
  315. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  316. data = (data << 8) | (*(uchar *) cp);
  317. }
  318. if ((rc = write_data (info, wp, data)) != 0) {
  319. return (rc);
  320. }
  321. wp += port_width;
  322. }
  323. /*
  324. * handle word aligned part
  325. */
  326. while (cnt >= port_width) {
  327. data = 0;
  328. for (i = 0; i < port_width; ++i) {
  329. data = (data << 8) | *src++;
  330. }
  331. if ((rc = write_data (info, wp, data)) != 0) {
  332. return (rc);
  333. }
  334. wp += port_width;
  335. cnt -= port_width;
  336. }
  337. if (cnt == 0) {
  338. return (0);
  339. }
  340. /*
  341. * handle unaligned tail bytes
  342. */
  343. data = 0;
  344. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  345. data = (data << 8) | *src++;
  346. --cnt;
  347. }
  348. for (; i < port_width; ++i, ++cp) {
  349. data = (data << 8) | (*(uchar *) cp);
  350. }
  351. return (write_data (info, wp, data));
  352. }
  353. /*-----------------------------------------------------------------------
  354. * Write a word or halfword to Flash, returns:
  355. * 0 - OK
  356. * 1 - write timeout
  357. * 2 - Flash not erased
  358. */
  359. static int write_data (flash_info_t * info, ulong dest, FPW data)
  360. {
  361. FPWV *addr = (FPWV *) dest;
  362. ulong status;
  363. ulong start;
  364. int flag;
  365. /* Check if Flash is (sufficiently) erased */
  366. if ((*addr & data) != data) {
  367. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  368. return (2);
  369. }
  370. /* Disable interrupts which might cause a timeout here */
  371. flag = disable_interrupts ();
  372. *addr = (FPW) 0x00400040; /* write setup */
  373. *addr = data;
  374. /* re-enable interrupts if necessary */
  375. if (flag)
  376. enable_interrupts ();
  377. start = get_timer (0);
  378. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  379. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  380. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  381. return (1);
  382. }
  383. }
  384. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  385. return (0);
  386. }