flash.c 11 KB

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