flash.c 9.9 KB

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