flash.c 9.5 KB

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