flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * (C) Copyright 2000
  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. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  30. static int write_word (flash_info_t *info, ulong dest, ulong data);
  31. static void flash_get_offsets (ulong base, flash_info_t *info);
  32. /*-----------------------------------------------------------------------
  33. */
  34. unsigned long flash_init (void)
  35. {
  36. unsigned long size_b0, size_b1;
  37. int i;
  38. /* Init: no FLASHes known */
  39. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i)
  40. flash_info[i].flash_id = FLASH_UNKNOWN;
  41. /* Detect size */
  42. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  43. /* Setup offsets */
  44. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  45. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  46. /* Monitor protection ON by default */
  47. flash_protect(FLAG_PROTECT_SET,
  48. CFG_MONITOR_BASE,
  49. CFG_MONITOR_BASE+monitor_flash_len-1,
  50. &flash_info[0]);
  51. #endif
  52. size_b1 = 0 ;
  53. flash_info[1].flash_id = FLASH_UNKNOWN;
  54. flash_info[1].sector_count = -1;
  55. flash_info[0].size = size_b0;
  56. flash_info[1].size = size_b1;
  57. return (size_b0 + size_b1);
  58. }
  59. /*-----------------------------------------------------------------------
  60. * Fix this to support variable sector sizes
  61. */
  62. static void flash_get_offsets (ulong base, flash_info_t *info)
  63. {
  64. int i;
  65. /* set up sector start address table */
  66. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
  67. /* set sector offsets for bottom boot block type */
  68. for (i = 0; i < info->sector_count; i++)
  69. info->start[i] = base + (i * 0x00010000);
  70. }
  71. }
  72. /*-----------------------------------------------------------------------
  73. */
  74. void flash_print_info (flash_info_t *info)
  75. {
  76. int i;
  77. if (info->flash_id == FLASH_UNKNOWN)
  78. {
  79. puts ("missing or unknown FLASH type\n");
  80. return;
  81. }
  82. switch (info->flash_id & FLASH_VENDMASK)
  83. {
  84. case FLASH_MAN_AMD: printf ("AMD "); break;
  85. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  86. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  87. default: printf ("Unknown Vendor "); break;
  88. }
  89. switch (info->flash_id & FLASH_TYPEMASK)
  90. {
  91. case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
  92. break;
  93. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  94. break;
  95. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  96. break;
  97. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  98. break;
  99. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  100. break;
  101. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  102. break;
  103. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  104. break;
  105. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  106. break;
  107. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  108. break;
  109. default: printf ("Unknown Chip Type\n");
  110. break;
  111. }
  112. if (info->size >> 20) {
  113. printf (" Size: %ld MB in %d Sectors\n",
  114. info->size >> 20,
  115. info->sector_count);
  116. } else {
  117. printf (" Size: %ld KB in %d Sectors\n",
  118. info->size >> 10,
  119. info->sector_count);
  120. }
  121. puts (" Sector Start Addresses:");
  122. for (i=0; i<info->sector_count; ++i)
  123. {
  124. if ((i % 5) == 0)
  125. {
  126. puts ("\n ");
  127. }
  128. printf (" %08lX%s",
  129. info->start[i],
  130. info->protect[i] ? " (RO)" : " ");
  131. }
  132. putc ('\n');
  133. return;
  134. }
  135. /*-----------------------------------------------------------------------
  136. */
  137. /*
  138. * The following code cannot be run from FLASH!
  139. */
  140. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  141. {
  142. short i;
  143. volatile unsigned char *caddr;
  144. char value;
  145. caddr = (volatile unsigned char *)addr ;
  146. /* Write auto select command: read Manufacturer ID */
  147. #if 0
  148. printf("Base address is: %08x\n", caddr);
  149. #endif
  150. caddr[0x0555] = 0xAA;
  151. caddr[0x02AA] = 0x55;
  152. caddr[0x0555] = 0x90;
  153. value = caddr[0];
  154. #if 0
  155. printf("Manufact ID: %02x\n", value);
  156. #endif
  157. switch (value)
  158. {
  159. case 0x1: /* AMD_MANUFACT */
  160. info->flash_id = FLASH_MAN_AMD;
  161. break;
  162. case 0x4: /* FUJ_MANUFACT */
  163. info->flash_id = FLASH_MAN_FUJ;
  164. break;
  165. default:
  166. info->flash_id = FLASH_UNKNOWN;
  167. info->sector_count = 0;
  168. info->size = 0;
  169. break;
  170. }
  171. value = caddr[1]; /* device ID */
  172. #if 0
  173. printf("Device ID: %02x\n", value);
  174. #endif
  175. switch (value)
  176. {
  177. case AMD_ID_LV040B:
  178. info->flash_id += FLASH_AM040;
  179. info->sector_count = 8;
  180. info->size = 0x00080000;
  181. break; /* => 512Kb */
  182. default:
  183. info->flash_id = FLASH_UNKNOWN;
  184. return (0); /* => no or unknown flash */
  185. }
  186. flash_get_offsets ((ulong)addr, &flash_info[0]);
  187. /* check for protected sectors */
  188. for (i = 0; i < info->sector_count; i++)
  189. {
  190. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  191. /* D0 = 1 if protected */
  192. caddr = (volatile unsigned char *)(info->start[i]);
  193. info->protect[i] = caddr[2] & 1;
  194. }
  195. /*
  196. * Prevent writes to uninitialized FLASH.
  197. */
  198. if (info->flash_id != FLASH_UNKNOWN)
  199. {
  200. caddr = (volatile unsigned char *)info->start[0];
  201. *caddr = 0xF0; /* reset bank */
  202. }
  203. return (info->size);
  204. }
  205. /*-----------------------------------------------------------------------
  206. */
  207. int flash_erase (flash_info_t *info, int s_first, int s_last)
  208. {
  209. volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
  210. int flag, prot, sect, l_sect;
  211. ulong start, now, last;
  212. if ((s_first < 0) || (s_first > s_last)) {
  213. if (info->flash_id == FLASH_UNKNOWN) {
  214. printf ("- missing\n");
  215. } else {
  216. printf ("- no sectors to erase\n");
  217. }
  218. return 1;
  219. }
  220. if ((info->flash_id == FLASH_UNKNOWN) ||
  221. (info->flash_id > FLASH_AMD_COMP)) {
  222. printf ("Can't erase unknown flash type - aborted\n");
  223. return 1;
  224. }
  225. prot = 0;
  226. for (sect=s_first; sect<=s_last; ++sect) {
  227. if (info->protect[sect]) {
  228. prot++;
  229. }
  230. }
  231. if (prot) {
  232. printf ("- Warning: %d protected sectors will not be erased!\n",
  233. prot);
  234. } else {
  235. printf ("\n");
  236. }
  237. l_sect = -1;
  238. /* Disable interrupts which might cause a timeout here */
  239. flag = disable_interrupts();
  240. addr[0x0555] = 0xAA;
  241. addr[0x02AA] = 0x55;
  242. addr[0x0555] = 0x80;
  243. addr[0x0555] = 0xAA;
  244. addr[0x02AA] = 0x55;
  245. /* Start erase on unprotected sectors */
  246. for (sect = s_first; sect<=s_last; sect++) {
  247. if (info->protect[sect] == 0) { /* not protected */
  248. addr = (volatile unsigned char *)(info->start[sect]);
  249. addr[0] = 0x30;
  250. l_sect = sect;
  251. }
  252. }
  253. /* re-enable interrupts if necessary */
  254. if (flag)
  255. enable_interrupts();
  256. /* wait at least 80us - let's wait 1 ms */
  257. udelay (1000);
  258. /*
  259. * We wait for the last triggered sector
  260. */
  261. if (l_sect < 0)
  262. goto DONE;
  263. start = get_timer (0);
  264. last = start;
  265. addr = (volatile unsigned char *)(info->start[l_sect]);
  266. while ((addr[0] & 0xFF) != 0xFF)
  267. {
  268. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  269. printf ("Timeout\n");
  270. return 1;
  271. }
  272. /* show that we're waiting */
  273. if ((now - last) > 1000) { /* every second */
  274. putc ('.');
  275. last = now;
  276. }
  277. }
  278. DONE:
  279. /* reset to read mode */
  280. addr = (volatile unsigned char *)info->start[0];
  281. addr[0] = 0xF0; /* reset bank */
  282. printf (" done\n");
  283. return 0;
  284. }
  285. /*-----------------------------------------------------------------------
  286. * Copy memory to flash, returns:
  287. * 0 - OK
  288. * 1 - write timeout
  289. * 2 - Flash not erased
  290. */
  291. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  292. {
  293. ulong cp, wp, data;
  294. int i, l, rc;
  295. wp = (addr & ~3); /* get lower word aligned address */
  296. /*
  297. * handle unaligned start bytes
  298. */
  299. if ((l = addr - wp) != 0) {
  300. data = 0;
  301. for (i=0, cp=wp; i<l; ++i, ++cp) {
  302. data = (data << 8) | (*(uchar *)cp);
  303. }
  304. for (; i<4 && cnt>0; ++i) {
  305. data = (data << 8) | *src++;
  306. --cnt;
  307. ++cp;
  308. }
  309. for (; cnt==0 && i<4; ++i, ++cp) {
  310. data = (data << 8) | (*(uchar *)cp);
  311. }
  312. if ((rc = write_word(info, wp, data)) != 0) {
  313. return (rc);
  314. }
  315. wp += 4;
  316. }
  317. /*
  318. * handle word aligned part
  319. */
  320. while (cnt >= 4) {
  321. data = 0;
  322. for (i=0; i<4; ++i) {
  323. data = (data << 8) | *src++;
  324. }
  325. if ((rc = write_word(info, wp, data)) != 0) {
  326. return (rc);
  327. }
  328. wp += 4;
  329. cnt -= 4;
  330. }
  331. if (cnt == 0) {
  332. return (0);
  333. }
  334. /*
  335. * handle unaligned tail bytes
  336. */
  337. data = 0;
  338. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  339. data = (data << 8) | *src++;
  340. --cnt;
  341. }
  342. for (; i<4; ++i, ++cp) {
  343. data = (data << 8) | (*(uchar *)cp);
  344. }
  345. return (write_word(info, wp, data));
  346. }
  347. /*-----------------------------------------------------------------------
  348. * Write a word to Flash, returns:
  349. * 0 - OK
  350. * 1 - write timeout
  351. * 2 - Flash not erased
  352. */
  353. static int write_word (flash_info_t *info, ulong dest, ulong data)
  354. {
  355. volatile unsigned char *addr = (volatile unsigned char*)(info->start[0]),
  356. *cdest,*cdata;
  357. ulong start;
  358. int flag, count = 4 ;
  359. cdest = (volatile unsigned char *)dest ;
  360. cdata = (volatile unsigned char *)&data ;
  361. /* Check if Flash is (sufficiently) erased */
  362. if ((*((vu_long *)dest) & data) != data) {
  363. return (2);
  364. }
  365. while(count--)
  366. {
  367. /* Disable interrupts which might cause a timeout here */
  368. flag = disable_interrupts();
  369. addr[0x0555] = 0xAA;
  370. addr[0x02AA] = 0x55;
  371. addr[0x0555] = 0xA0;
  372. *cdest = *cdata;
  373. /* re-enable interrupts if necessary */
  374. if (flag)
  375. enable_interrupts();
  376. /* data polling for D7 */
  377. start = get_timer (0);
  378. while ((*cdest ^ *cdata) & 0x80)
  379. {
  380. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  381. return (1);
  382. }
  383. }
  384. cdata++ ;
  385. cdest++ ;
  386. }
  387. return (0);
  388. }
  389. /*-----------------------------------------------------------------------
  390. */