flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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+CFG_MONITOR_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 0x01:
  160. case AMD_MANUFACT:
  161. info->flash_id = FLASH_MAN_AMD;
  162. break;
  163. case FUJ_MANUFACT:
  164. info->flash_id = FLASH_MAN_FUJ;
  165. break;
  166. default:
  167. info->flash_id = FLASH_UNKNOWN;
  168. info->sector_count = 0;
  169. info->size = 0;
  170. break;
  171. }
  172. value = caddr[1]; /* device ID */
  173. #if 0
  174. printf("Device ID: %02x\n", value);
  175. #endif
  176. switch (value)
  177. {
  178. case AMD_ID_LV040B:
  179. info->flash_id += FLASH_AM040;
  180. info->sector_count = 8;
  181. info->size = 0x00080000;
  182. break; /* => 512Kb */
  183. default:
  184. info->flash_id = FLASH_UNKNOWN;
  185. return (0); /* => no or unknown flash */
  186. }
  187. flash_get_offsets ((ulong)addr, &flash_info[0]);
  188. /* check for protected sectors */
  189. for (i = 0; i < info->sector_count; i++)
  190. {
  191. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  192. /* D0 = 1 if protected */
  193. caddr = (volatile unsigned char *)(info->start[i]);
  194. info->protect[i] = caddr[2] & 1;
  195. }
  196. /*
  197. * Prevent writes to uninitialized FLASH.
  198. */
  199. if (info->flash_id != FLASH_UNKNOWN)
  200. {
  201. caddr = (volatile unsigned char *)info->start[0];
  202. *caddr = 0xF0; /* reset bank */
  203. }
  204. return (info->size);
  205. }
  206. /*-----------------------------------------------------------------------
  207. */
  208. int flash_erase (flash_info_t *info, int s_first, int s_last)
  209. {
  210. volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
  211. int flag, prot, sect, l_sect;
  212. ulong start, now, last;
  213. if ((s_first < 0) || (s_first > s_last)) {
  214. if (info->flash_id == FLASH_UNKNOWN) {
  215. printf ("- missing\n");
  216. } else {
  217. printf ("- no sectors to erase\n");
  218. }
  219. return 1;
  220. }
  221. if ((info->flash_id == FLASH_UNKNOWN) ||
  222. (info->flash_id > FLASH_AMD_COMP)) {
  223. printf ("Can't erase unknown flash type - aborted\n");
  224. return 1;
  225. }
  226. prot = 0;
  227. for (sect=s_first; sect<=s_last; ++sect) {
  228. if (info->protect[sect]) {
  229. prot++;
  230. }
  231. }
  232. if (prot) {
  233. printf ("- Warning: %d protected sectors will not be erased!\n",
  234. prot);
  235. } else {
  236. printf ("\n");
  237. }
  238. l_sect = -1;
  239. /* Disable interrupts which might cause a timeout here */
  240. flag = disable_interrupts();
  241. addr[0x0555] = 0xAA;
  242. addr[0x02AA] = 0x55;
  243. addr[0x0555] = 0x80;
  244. addr[0x0555] = 0xAA;
  245. addr[0x02AA] = 0x55;
  246. /* Start erase on unprotected sectors */
  247. for (sect = s_first; sect<=s_last; sect++) {
  248. if (info->protect[sect] == 0) { /* not protected */
  249. addr = (volatile unsigned char *)(info->start[sect]);
  250. addr[0] = 0x30;
  251. l_sect = sect;
  252. }
  253. }
  254. /* re-enable interrupts if necessary */
  255. if (flag)
  256. enable_interrupts();
  257. /* wait at least 80us - let's wait 1 ms */
  258. udelay (1000);
  259. /*
  260. * We wait for the last triggered sector
  261. */
  262. if (l_sect < 0)
  263. goto DONE;
  264. start = get_timer (0);
  265. last = start;
  266. addr = (volatile unsigned char *)(info->start[l_sect]);
  267. while ((addr[0] & 0xFF) != 0xFF)
  268. {
  269. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  270. printf ("Timeout\n");
  271. return 1;
  272. }
  273. /* show that we're waiting */
  274. if ((now - last) > 1000) { /* every second */
  275. putc ('.');
  276. last = now;
  277. }
  278. }
  279. DONE:
  280. /* reset to read mode */
  281. addr = (volatile unsigned char *)info->start[0];
  282. addr[0] = 0xF0; /* reset bank */
  283. printf (" done\n");
  284. return 0;
  285. }
  286. /*-----------------------------------------------------------------------
  287. * Copy memory to flash, returns:
  288. * 0 - OK
  289. * 1 - write timeout
  290. * 2 - Flash not erased
  291. */
  292. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  293. {
  294. ulong cp, wp, data;
  295. int i, l, rc;
  296. wp = (addr & ~3); /* get lower word aligned address */
  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<4 && cnt>0; ++i) {
  306. data = (data << 8) | *src++;
  307. --cnt;
  308. ++cp;
  309. }
  310. for (; cnt==0 && i<4; ++i, ++cp) {
  311. data = (data << 8) | (*(uchar *)cp);
  312. }
  313. if ((rc = write_word(info, wp, data)) != 0) {
  314. return (rc);
  315. }
  316. wp += 4;
  317. }
  318. /*
  319. * handle word aligned part
  320. */
  321. while (cnt >= 4) {
  322. data = 0;
  323. for (i=0; i<4; ++i) {
  324. data = (data << 8) | *src++;
  325. }
  326. if ((rc = write_word(info, wp, data)) != 0) {
  327. return (rc);
  328. }
  329. wp += 4;
  330. cnt -= 4;
  331. }
  332. if (cnt == 0) {
  333. return (0);
  334. }
  335. /*
  336. * handle unaligned tail bytes
  337. */
  338. data = 0;
  339. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  340. data = (data << 8) | *src++;
  341. --cnt;
  342. }
  343. for (; i<4; ++i, ++cp) {
  344. data = (data << 8) | (*(uchar *)cp);
  345. }
  346. return (write_word(info, wp, data));
  347. }
  348. /*-----------------------------------------------------------------------
  349. * Write a word to Flash, returns:
  350. * 0 - OK
  351. * 1 - write timeout
  352. * 2 - Flash not erased
  353. */
  354. static int write_word (flash_info_t *info, ulong dest, ulong data)
  355. {
  356. volatile unsigned char *addr = (volatile unsigned char*)(info->start[0]),
  357. *cdest,*cdata;
  358. ulong start;
  359. int flag, count = 4 ;
  360. cdest = (volatile unsigned char *)dest ;
  361. cdata = (volatile unsigned char *)&data ;
  362. /* Check if Flash is (sufficiently) erased */
  363. if ((*((vu_long *)dest) & data) != data) {
  364. return (2);
  365. }
  366. while(count--)
  367. {
  368. /* Disable interrupts which might cause a timeout here */
  369. flag = disable_interrupts();
  370. addr[0x0555] = 0xAA;
  371. addr[0x02AA] = 0x55;
  372. addr[0x0555] = 0xA0;
  373. *cdest = *cdata;
  374. /* re-enable interrupts if necessary */
  375. if (flag)
  376. enable_interrupts();
  377. /* data polling for D7 */
  378. start = get_timer (0);
  379. while ((*cdest ^ *cdata) & 0x80)
  380. {
  381. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  382. return (1);
  383. }
  384. }
  385. cdata++ ;
  386. cdest++ ;
  387. }
  388. return (0);
  389. }
  390. /*-----------------------------------------------------------------------
  391. */