flash.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * (C) Copyright 2000
  3. * Marius Groeger <mgroeger@sysgo.de>
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * Flash Routines for AM290[48]0B devices
  10. *
  11. *--------------------------------------------------------------------
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <mpc8xx.h>
  16. #include "vpd.h"
  17. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  18. /*-----------------------------------------------------------------------
  19. * Functions
  20. */
  21. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  22. static int write_word (flash_info_t *info, ulong dest, ulong data);
  23. /*-----------------------------------------------------------------------
  24. */
  25. unsigned long flash_init (void)
  26. {
  27. unsigned long size, totsize;
  28. int i;
  29. ulong addr;
  30. /* Init: no FLASHes known */
  31. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  32. flash_info[i].flash_id = FLASH_UNKNOWN;
  33. }
  34. totsize = 0;
  35. addr = 0xfc000000;
  36. for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  37. size = flash_get_size((vu_long *)addr, &flash_info[i]);
  38. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  39. break;
  40. totsize += size;
  41. addr += size;
  42. }
  43. addr = 0xfe000000;
  44. for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  45. size = flash_get_size((vu_long *)addr, &flash_info[i]);
  46. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  47. break;
  48. totsize += size;
  49. addr += size;
  50. }
  51. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  52. /* monitor protection ON by default */
  53. flash_protect(FLAG_PROTECT_SET,
  54. CONFIG_SYS_MONITOR_BASE,
  55. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  56. &flash_info[0]);
  57. #endif
  58. return (totsize);
  59. }
  60. /*-----------------------------------------------------------------------
  61. */
  62. void flash_print_info (flash_info_t *info)
  63. {
  64. int i;
  65. if (info->flash_id == FLASH_UNKNOWN) {
  66. printf ("missing or unknown FLASH type\n");
  67. return;
  68. }
  69. switch (info->flash_id >> 16) {
  70. case 0x1:
  71. printf ("AMD ");
  72. break;
  73. default:
  74. printf ("Unknown Vendor ");
  75. break;
  76. }
  77. switch (info->flash_id & FLASH_TYPEMASK) {
  78. case AMD_ID_F040B:
  79. printf ("AM29F040B (4 Mbit)\n");
  80. break;
  81. case AMD_ID_F080B:
  82. printf ("AM29F080B (8 Mbit)\n");
  83. break;
  84. case AMD_ID_F016D:
  85. printf ("AM29F016D (16 Mbit)\n");
  86. break;
  87. default:
  88. printf ("Unknown Chip Type\n");
  89. break;
  90. }
  91. printf (" Size: %ld MB in %d Sectors\n",
  92. info->size >> 20, info->sector_count);
  93. printf (" Sector Start Addresses:");
  94. for (i=0; i<info->sector_count; ++i) {
  95. if ((i % 5) == 0)
  96. printf ("\n ");
  97. printf (" %08lX%s",
  98. info->start[i],
  99. info->protect[i] ? " (RO)" : " "
  100. );
  101. }
  102. printf ("\n");
  103. return;
  104. }
  105. /*
  106. * The following code cannot be run from FLASH!
  107. */
  108. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  109. {
  110. short i;
  111. ulong vendor, devid;
  112. ulong base = (ulong)addr;
  113. /* Write auto select command: read Manufacturer ID */
  114. addr[0x0555] = 0xAAAAAAAA;
  115. addr[0x02AA] = 0x55555555;
  116. addr[0x0555] = 0x90909090;
  117. vendor = addr[0];
  118. devid = addr[1] & 0xff;
  119. /* only support AMD */
  120. if (vendor != 0x01010101) {
  121. return 0;
  122. }
  123. vendor &= 0xf;
  124. devid &= 0xff;
  125. if (devid == AMD_ID_F040B) {
  126. info->flash_id = vendor << 16 | devid;
  127. info->sector_count = 8;
  128. info->size = info->sector_count * 0x10000;
  129. }
  130. else if (devid == AMD_ID_F080B) {
  131. info->flash_id = vendor << 16 | devid;
  132. info->sector_count = 16;
  133. info->size = 4 * info->sector_count * 0x10000;
  134. }
  135. else if (devid == AMD_ID_F016D) {
  136. info->flash_id = vendor << 16 | devid;
  137. info->sector_count = 32;
  138. info->size = 4 * info->sector_count * 0x10000;
  139. }
  140. else {
  141. printf ("## Unknown Flash Type: %08lx\n", devid);
  142. return 0;
  143. }
  144. /* check for protected sectors */
  145. for (i = 0; i < info->sector_count; i++) {
  146. /* sector base address */
  147. info->start[i] = base + i * (info->size / info->sector_count);
  148. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  149. /* D0 = 1 if protected */
  150. addr = (volatile unsigned long *)(info->start[i]);
  151. info->protect[i] = addr[2] & 1;
  152. }
  153. /*
  154. * Prevent writes to uninitialized FLASH.
  155. */
  156. if (info->flash_id != FLASH_UNKNOWN) {
  157. addr = (vu_long *)info->start[0];
  158. addr[0] = 0xF0; /* reset bank */
  159. }
  160. return (info->size);
  161. }
  162. /*-----------------------------------------------------------------------
  163. */
  164. int flash_erase (flash_info_t *info, int s_first, int s_last)
  165. {
  166. vu_long *addr = (vu_long*)(info->start[0]);
  167. int flag, prot, sect, l_sect;
  168. ulong start, now, last;
  169. if ((s_first < 0) || (s_first > s_last)) {
  170. if (info->flash_id == FLASH_UNKNOWN) {
  171. printf ("- missing\n");
  172. } else {
  173. printf ("- no sectors to erase\n");
  174. }
  175. return 1;
  176. }
  177. prot = 0;
  178. for (sect = s_first; sect <= s_last; sect++) {
  179. if (info->protect[sect]) {
  180. prot++;
  181. }
  182. }
  183. if (prot) {
  184. printf ("- Warning: %d protected sectors will not be erased!\n",
  185. prot);
  186. } else {
  187. printf ("\n");
  188. }
  189. l_sect = -1;
  190. /* Disable interrupts which might cause a timeout here */
  191. flag = disable_interrupts();
  192. addr[0x0555] = 0XAAAAAAAA;
  193. addr[0x02AA] = 0x55555555;
  194. addr[0x0555] = 0x80808080;
  195. addr[0x0555] = 0XAAAAAAAA;
  196. addr[0x02AA] = 0x55555555;
  197. /* Start erase on unprotected sectors */
  198. for (sect = s_first; sect<=s_last; sect++) {
  199. if (info->protect[sect] == 0) { /* not protected */
  200. addr = (vu_long*)(info->start[sect]);
  201. addr[0] = 0x30303030;
  202. l_sect = sect;
  203. }
  204. }
  205. /* re-enable interrupts if necessary */
  206. if (flag)
  207. enable_interrupts();
  208. /* wait at least 80us - let's wait 1 ms */
  209. udelay (1000);
  210. /*
  211. * We wait for the last triggered sector
  212. */
  213. if (l_sect < 0)
  214. goto DONE;
  215. start = get_timer (0);
  216. last = start;
  217. addr = (vu_long*)(info->start[l_sect]);
  218. while ((addr[0] & 0x80808080) != 0x80808080) {
  219. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  220. printf ("Timeout\n");
  221. return 1;
  222. }
  223. /* show that we're waiting */
  224. if ((now - last) > 1000) { /* every second */
  225. serial_putc ('.');
  226. last = now;
  227. }
  228. }
  229. DONE:
  230. /* reset to read mode */
  231. addr = (volatile unsigned long *)info->start[0];
  232. addr[0] = 0xF0F0F0F0; /* reset bank */
  233. printf (" done\n");
  234. return 0;
  235. }
  236. /*-----------------------------------------------------------------------
  237. * Copy memory to flash, returns:
  238. * 0 - OK
  239. * 1 - write timeout
  240. * 2 - Flash not erased
  241. */
  242. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  243. {
  244. ulong cp, wp, data;
  245. int i, l, rc;
  246. wp = (addr & ~3); /* get lower word aligned address */
  247. /*
  248. * handle unaligned start bytes
  249. */
  250. if ((l = addr - wp) != 0) {
  251. data = 0;
  252. for (i=0, cp=wp; i<l; ++i, ++cp) {
  253. data = (data << 8) | (*(uchar *)cp);
  254. }
  255. for (; i<4 && cnt>0; ++i) {
  256. data = (data << 8) | *src++;
  257. --cnt;
  258. ++cp;
  259. }
  260. for (; cnt==0 && i<4; ++i, ++cp) {
  261. data = (data << 8) | (*(uchar *)cp);
  262. }
  263. if ((rc = write_word(info, wp, data)) != 0) {
  264. return (rc);
  265. }
  266. wp += 4;
  267. }
  268. /*
  269. * handle word aligned part
  270. */
  271. while (cnt >= 4) {
  272. data = 0;
  273. for (i=0; i<4; ++i) {
  274. data = (data << 8) | *src++;
  275. }
  276. if ((rc = write_word(info, wp, data)) != 0) {
  277. return (rc);
  278. }
  279. wp += 4;
  280. cnt -= 4;
  281. }
  282. if (cnt == 0) {
  283. return (0);
  284. }
  285. /*
  286. * handle unaligned tail bytes
  287. */
  288. data = 0;
  289. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  290. data = (data << 8) | *src++;
  291. --cnt;
  292. }
  293. for (; i<4; ++i, ++cp) {
  294. data = (data << 8) | (*(uchar *)cp);
  295. }
  296. return (write_word(info, wp, data));
  297. }
  298. /*-----------------------------------------------------------------------
  299. * Write a word to Flash, returns:
  300. * 0 - OK
  301. * 1 - write timeout
  302. * 2 - Flash not erased
  303. */
  304. static int write_word (flash_info_t *info, ulong dest, ulong data)
  305. {
  306. vu_long *addr = (vu_long*)(info->start[0]);
  307. ulong start;
  308. int flag;
  309. /* Check if Flash is (sufficiently) erased */
  310. if ((*((vu_long *)dest) & data) != data) {
  311. return (2);
  312. }
  313. /* Disable interrupts which might cause a timeout here */
  314. flag = disable_interrupts();
  315. addr[0x0555] = 0xAAAAAAAA;
  316. addr[0x02AA] = 0x55555555;
  317. addr[0x0555] = 0xA0A0A0A0;
  318. *((vu_long *)dest) = data;
  319. /* re-enable interrupts if necessary */
  320. if (flag)
  321. enable_interrupts();
  322. /* data polling for D7 */
  323. start = get_timer (0);
  324. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  325. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  326. return (1);
  327. }
  328. }
  329. return (0);
  330. }
  331. /*-----------------------------------------------------------------------
  332. */