flash.c 9.4 KB

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