flash.c 10 KB

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