flash.c 9.2 KB

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