flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * (C) Copyright 2002
  3. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  4. *
  5. * (C) Copyright 2000-2004
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <linux/byteorder/swab.h>
  28. #define SWAP(x) __swab32(x)
  29. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  30. /* Functions */
  31. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  32. static int write_word (flash_info_t *info, ulong dest, ulong data);
  33. static void flash_get_offsets (ulong base, flash_info_t *info);
  34. /*-----------------------------------------------------------------------
  35. */
  36. unsigned long flash_init (void)
  37. {
  38. int i;
  39. ulong size = 0;
  40. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  41. switch (i) {
  42. case 0:
  43. flash_get_size ((vu_long *) PHYS_FLASH_1, &flash_info[i]);
  44. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  45. break;
  46. case 1:
  47. flash_get_size ((vu_long *) PHYS_FLASH_2, &flash_info[i]);
  48. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  49. break;
  50. default:
  51. panic ("configured too many flash banks!\n");
  52. break;
  53. }
  54. size += flash_info[i].size;
  55. }
  56. /* Protect monitor and environment sectors */
  57. flash_protect ( FLAG_PROTECT_SET,CONFIG_SYS_FLASH_BASE,CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,&flash_info[0] );
  58. flash_protect ( FLAG_PROTECT_SET,CONFIG_ENV_ADDR,CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
  59. return size;
  60. }
  61. /*-----------------------------------------------------------------------
  62. */
  63. static void flash_get_offsets (ulong base, flash_info_t *info)
  64. {
  65. int i;
  66. if (info->flash_id == FLASH_UNKNOWN) return;
  67. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  68. for (i = 0; i < info->sector_count; i++) {
  69. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  70. info->protect[i] = 0;
  71. }
  72. }
  73. }
  74. /*-----------------------------------------------------------------------
  75. */
  76. void flash_print_info (flash_info_t *info)
  77. {
  78. int i;
  79. if (info->flash_id == FLASH_UNKNOWN) {
  80. printf ("missing or unknown FLASH type\n");
  81. return;
  82. }
  83. switch (info->flash_id & FLASH_VENDMASK) {
  84. case FLASH_MAN_AMD: printf ("AMD "); break;
  85. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  86. default: printf ("Unknown Vendor "); break;
  87. }
  88. switch (info->flash_id & FLASH_TYPEMASK) {
  89. case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
  90. break;
  91. case FLASH_S29GL064M: printf ("S29GL064M (64Mbit, top boot sector size)\n");
  92. break;
  93. default: printf ("Unknown Chip Type\n");
  94. break;
  95. }
  96. printf (" Size: %ld MB in %d Sectors\n",
  97. info->size >> 20, info->sector_count);
  98. printf (" Sector Start Addresses:");
  99. for (i=0; i<info->sector_count; ++i) {
  100. if ((i % 5) == 0)
  101. printf ("\n ");
  102. printf (" %08lX%s",
  103. info->start[i],
  104. info->protect[i] ? " (RO)" : " "
  105. );
  106. }
  107. printf ("\n");
  108. return;
  109. }
  110. /*
  111. * The following code cannot be run from FLASH!
  112. */
  113. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  114. {
  115. short i;
  116. ulong value;
  117. ulong base = (ulong)addr;
  118. /* Write auto select command: read Manufacturer ID */
  119. addr[0x0555] = 0x00AA00AA;
  120. addr[0x02AA] = 0x00550055;
  121. addr[0x0555] = 0x00900090;
  122. value = addr[0];
  123. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  124. switch (value) {
  125. case AMD_MANUFACT:
  126. debug ("Manufacturer: AMD\n");
  127. info->flash_id = FLASH_MAN_AMD;
  128. break;
  129. case FUJ_MANUFACT:
  130. debug ("Manufacturer: FUJITSU\n");
  131. info->flash_id = FLASH_MAN_FUJ;
  132. break;
  133. default:
  134. debug ("Manufacturer: *** unknown ***\n");
  135. info->flash_id = FLASH_UNKNOWN;
  136. info->sector_count = 0;
  137. info->size = 0;
  138. return (0); /* no or unknown flash */
  139. }
  140. value = addr[1]; /* device ID */
  141. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  142. switch (value) {
  143. case AMD_ID_MIRROR:
  144. debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
  145. addr[14], addr[15]);
  146. switch(addr[14]) {
  147. case AMD_ID_LV640U_2:
  148. if (addr[15] != AMD_ID_LV640U_3) {
  149. debug ("Chip: AMLV640U -> unknown\n");
  150. info->flash_id = FLASH_UNKNOWN;
  151. } else {
  152. debug ("Chip: AMLV640U\n");
  153. info->flash_id += FLASH_AMLV640U;
  154. info->sector_count = 128;
  155. info->size = 0x01000000;
  156. }
  157. break; /* => 16 MB */
  158. case AMD_ID_GL064MT_2:
  159. if (addr[15] != AMD_ID_GL064MT_3) {
  160. debug ("Chip: S29GL064M-R3 -> unknown\n");
  161. info->flash_id = FLASH_UNKNOWN;
  162. } else {
  163. debug ("Chip: S29GL064M-R3\n");
  164. info->flash_id += FLASH_S29GL064M;
  165. info->sector_count = 128;
  166. info->size = 0x01000000;
  167. }
  168. break; /* => 16 MB */
  169. default:
  170. debug ("Chip: *** unknown ***\n");
  171. info->flash_id = FLASH_UNKNOWN;
  172. break;
  173. }
  174. break;
  175. default:
  176. info->flash_id = FLASH_UNKNOWN;
  177. return (0); /* => no or unknown flash */
  178. }
  179. /* set up sector start address table */
  180. switch (value) {
  181. case AMD_ID_MIRROR:
  182. switch (info->flash_id & FLASH_TYPEMASK) {
  183. /* only known types here - no default */
  184. case FLASH_AMLV128U:
  185. case FLASH_AMLV640U:
  186. case FLASH_AMLV320U:
  187. for (i = 0; i < info->sector_count; i++) {
  188. info->start[i] = base;
  189. base += 0x20000;
  190. }
  191. break;
  192. case FLASH_AMLV320B:
  193. for (i = 0; i < info->sector_count; i++) {
  194. info->start[i] = base;
  195. /*
  196. * The first 8 sectors are 8 kB,
  197. * all the other ones are 64 kB
  198. */
  199. base += (i < 8)
  200. ? 2 * ( 8 << 10)
  201. : 2 * (64 << 10);
  202. }
  203. break;
  204. }
  205. break;
  206. default:
  207. return (0);
  208. break;
  209. }
  210. #if 0
  211. /* check for protected sectors */
  212. for (i = 0; i < info->sector_count; i++) {
  213. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  214. /* D0 = 1 if protected */
  215. addr = (volatile unsigned long *)(info->start[i]);
  216. info->protect[i] = addr[2] & 1;
  217. }
  218. #endif
  219. /*
  220. * Prevent writes to uninitialized FLASH.
  221. */
  222. if (info->flash_id != FLASH_UNKNOWN) {
  223. addr = (volatile unsigned long *)info->start[0];
  224. *addr = 0x00F000F0; /* reset bank */
  225. }
  226. return (info->size);
  227. }
  228. /*-----------------------------------------------------------------------
  229. */
  230. int flash_erase (flash_info_t *info, int s_first, int s_last)
  231. {
  232. vu_long *addr = (vu_long*)(info->start[0]);
  233. int flag, prot, sect, l_sect;
  234. ulong start, now, last;
  235. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  236. if ((s_first < 0) || (s_first > s_last)) {
  237. if (info->flash_id == FLASH_UNKNOWN) {
  238. printf ("- missing\n");
  239. } else {
  240. printf ("- no sectors to erase\n");
  241. }
  242. return 1;
  243. }
  244. if ((info->flash_id == FLASH_UNKNOWN) ||
  245. (info->flash_id > FLASH_AMD_COMP)) {
  246. printf ("Can't erase unknown flash type %08lx - aborted\n",
  247. info->flash_id);
  248. return 1;
  249. }
  250. prot = 0;
  251. for (sect=s_first; sect<=s_last; ++sect) {
  252. if (info->protect[sect]) {
  253. prot++;
  254. }
  255. }
  256. if (prot) {
  257. printf ("- Warning: %d protected sectors will not be erased!\n",
  258. prot);
  259. } else {
  260. printf ("\n");
  261. }
  262. l_sect = -1;
  263. /* Disable interrupts which might cause a timeout here */
  264. flag = disable_interrupts();
  265. addr[0x0555] = 0x00AA00AA;
  266. addr[0x02AA] = 0x00550055;
  267. addr[0x0555] = 0x00800080;
  268. addr[0x0555] = 0x00AA00AA;
  269. addr[0x02AA] = 0x00550055;
  270. /* Start erase on unprotected sectors */
  271. for (sect = s_first; sect<=s_last; sect++) {
  272. if (info->protect[sect] == 0) { /* not protected */
  273. addr = (vu_long*)(info->start[sect]);
  274. addr[0] = 0x00300030;
  275. l_sect = sect;
  276. }
  277. }
  278. /* re-enable interrupts if necessary */
  279. if (flag)
  280. enable_interrupts();
  281. /* wait at least 80us - let's wait 1 ms */
  282. udelay (1000);
  283. /*
  284. * We wait for the last triggered sector
  285. */
  286. if (l_sect < 0)
  287. goto DONE;
  288. start = get_timer (0);
  289. last = start;
  290. addr = (vu_long*)(info->start[l_sect]);
  291. while ((addr[0] & 0x00800080) != 0x00800080) {
  292. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  293. printf ("Timeout\n");
  294. return 1;
  295. }
  296. /* show that we're waiting */
  297. if ((now - last) > 100000) { /* every second */
  298. putc ('.');
  299. last = now;
  300. }
  301. }
  302. DONE:
  303. /* reset to read mode */
  304. addr = (volatile unsigned long *)info->start[0];
  305. addr[0] = 0x00F000F0; /* reset bank */
  306. printf (" done\n");
  307. return 0;
  308. }
  309. /*-----------------------------------------------------------------------
  310. * Copy memory to flash, returns:
  311. * 0 - OK
  312. * 1 - write timeout
  313. * 2 - Flash not erased
  314. */
  315. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  316. {
  317. ulong cp, wp, data;
  318. int i, l, rc;
  319. wp = (addr & ~3); /* get lower word aligned address */
  320. /*
  321. * handle unaligned start bytes
  322. */
  323. if ((l = addr - wp) != 0) {
  324. data = 0;
  325. for (i=0, cp=wp; i<l; ++i, ++cp) {
  326. data = (data << 8) | (*(uchar *)cp);
  327. }
  328. for (; i<4 && cnt>0; ++i) {
  329. data = (data << 8) | *src++;
  330. --cnt;
  331. ++cp;
  332. }
  333. for (; cnt==0 && i<4; ++i, ++cp) {
  334. data = (data << 8) | (*(uchar *)cp);
  335. }
  336. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  337. return (rc);
  338. }
  339. wp += 4;
  340. }
  341. /*
  342. * handle word aligned part
  343. */
  344. while (cnt >= 4) {
  345. data = 0;
  346. for (i=0; i<4; ++i) {
  347. data = (data << 8) | *src++;
  348. }
  349. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  350. return (rc);
  351. }
  352. wp += 4;
  353. cnt -= 4;
  354. }
  355. if (cnt == 0) {
  356. return (0);
  357. }
  358. /*
  359. * handle unaligned tail bytes
  360. */
  361. data = 0;
  362. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  363. data = (data << 8) | *src++;
  364. --cnt;
  365. }
  366. for (; i<4; ++i, ++cp) {
  367. data = (data << 8) | (*(uchar *)cp);
  368. }
  369. return (write_word(info, wp, SWAP(data)));
  370. }
  371. /*-----------------------------------------------------------------------
  372. * Write a word to Flash, returns:
  373. * 0 - OK
  374. * 1 - write timeout
  375. * 2 - Flash not erased
  376. */
  377. static int write_word (flash_info_t *info, ulong dest, ulong data)
  378. {
  379. vu_long *addr = (vu_long*)(info->start[0]);
  380. ulong start;
  381. int flag;
  382. /* Check if Flash is (sufficiently) erased */
  383. if ((*((vu_long *)dest) & data) != data) {
  384. return (2);
  385. }
  386. /* Disable interrupts which might cause a timeout here */
  387. flag = disable_interrupts();
  388. addr[0x0555] = 0x00AA00AA;
  389. addr[0x02AA] = 0x00550055;
  390. addr[0x0555] = 0x00A000A0;
  391. *((vu_long *)dest) = data;
  392. /* re-enable interrupts if necessary */
  393. if (flag)
  394. enable_interrupts();
  395. /* data polling for D7 */
  396. start = get_timer (0);
  397. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  398. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  399. return (1);
  400. }
  401. }
  402. return (0);
  403. }