flash.c 10 KB

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