flash.c 10 KB

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