flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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[CFG_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 < CFG_MAX_FLASH_BANKS; i++) {
  41. switch (i) {
  42. case 0:
  43. flash_get_size ((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 ((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,CFG_FLASH_BASE,CFG_FLASH_BASE + monitor_flash_len - 1,&flash_info[0] );
  58. flash_protect ( FLAG_PROTECT_SET,CFG_ENV_ADDR,CFG_ENV_ADDR + CFG_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_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
  90. break;
  91. case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
  92. break;
  93. case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
  94. break;
  95. case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
  96. break;
  97. default: printf ("Unknown Chip Type\n");
  98. break;
  99. }
  100. printf (" Size: %ld MB in %d Sectors\n",
  101. info->size >> 20, info->sector_count);
  102. printf (" Sector Start Addresses:");
  103. for (i=0; i<info->sector_count; ++i) {
  104. if ((i % 5) == 0)
  105. printf ("\n ");
  106. printf (" %08lX%s",
  107. info->start[i],
  108. info->protect[i] ? " (RO)" : " "
  109. );
  110. }
  111. printf ("\n");
  112. return;
  113. }
  114. /*
  115. * The following code cannot be run from FLASH!
  116. */
  117. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  118. {
  119. short i;
  120. ulong value;
  121. ulong base = (ulong)addr;
  122. /* Write auto select command: read Manufacturer ID */
  123. addr[0x0555] = 0x00AA00AA;
  124. addr[0x02AA] = 0x00550055;
  125. addr[0x0555] = 0x00900090;
  126. value = addr[0];
  127. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  128. switch (value) {
  129. case AMD_MANUFACT:
  130. debug ("Manufacturer: AMD\n");
  131. info->flash_id = FLASH_MAN_AMD;
  132. break;
  133. case FUJ_MANUFACT:
  134. debug ("Manufacturer: FUJITSU\n");
  135. info->flash_id = FLASH_MAN_FUJ;
  136. break;
  137. default:
  138. debug ("Manufacturer: *** unknown ***\n");
  139. info->flash_id = FLASH_UNKNOWN;
  140. info->sector_count = 0;
  141. info->size = 0;
  142. return (0); /* no or unknown flash */
  143. }
  144. value = addr[1]; /* device ID */
  145. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  146. switch (value) {
  147. case AMD_ID_MIRROR:
  148. debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
  149. addr[14], addr[15]);
  150. switch(addr[14]) {
  151. case AMD_ID_LV128U_2:
  152. if (addr[15] != AMD_ID_LV128U_3) {
  153. debug ("Chip: AMLV128U -> unknown\n");
  154. info->flash_id = FLASH_UNKNOWN;
  155. } else {
  156. debug ("Chip: AMLV128U\n");
  157. info->flash_id += FLASH_AMLV128U;
  158. info->sector_count = 256;
  159. info->size = 0x02000000;
  160. }
  161. break; /* => 32 MB */
  162. case AMD_ID_LV640U_2:
  163. if (addr[15] != AMD_ID_LV640U_3) {
  164. debug ("Chip: AMLV640U -> unknown\n");
  165. info->flash_id = FLASH_UNKNOWN;
  166. } else {
  167. debug ("Chip: AMLV640U\n");
  168. info->flash_id += FLASH_AMLV640U;
  169. info->sector_count = 128;
  170. info->size = 0x01000000;
  171. }
  172. break; /* => 16 MB */
  173. case AMD_ID_LV320B_2:
  174. if (addr[15] != AMD_ID_LV320B_3) {
  175. debug ("Chip: AMLV320B -> unknown\n");
  176. info->flash_id = FLASH_UNKNOWN;
  177. } else {
  178. debug ("Chip: AMLV320B\n");
  179. info->flash_id += FLASH_AMLV320B;
  180. info->sector_count = 71;
  181. info->size = 0x00800000;
  182. }
  183. break; /* => 8 MB */
  184. default:
  185. debug ("Chip: *** unknown ***\n");
  186. info->flash_id = FLASH_UNKNOWN;
  187. break;
  188. }
  189. break;
  190. default:
  191. info->flash_id = FLASH_UNKNOWN;
  192. return (0); /* => no or unknown flash */
  193. }
  194. /* set up sector start address table */
  195. switch (value) {
  196. case AMD_ID_MIRROR:
  197. switch (info->flash_id & FLASH_TYPEMASK) {
  198. /* only known types here - no default */
  199. case FLASH_AMLV128U:
  200. case FLASH_AMLV640U:
  201. case FLASH_AMLV320U:
  202. for (i = 0; i < info->sector_count; i++) {
  203. info->start[i] = base;
  204. base += 0x20000;
  205. }
  206. break;
  207. case FLASH_AMLV320B:
  208. for (i = 0; i < info->sector_count; i++) {
  209. info->start[i] = base;
  210. /*
  211. * The first 8 sectors are 8 kB,
  212. * all the other ones are 64 kB
  213. */
  214. base += (i < 8)
  215. ? 2 * ( 8 << 10)
  216. : 2 * (64 << 10);
  217. }
  218. break;
  219. }
  220. break;
  221. default:
  222. return (0);
  223. break;
  224. }
  225. #if 0
  226. /* check for protected sectors */
  227. for (i = 0; i < info->sector_count; i++) {
  228. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  229. /* D0 = 1 if protected */
  230. addr = (volatile unsigned long *)(info->start[i]);
  231. info->protect[i] = addr[2] & 1;
  232. }
  233. #endif
  234. /*
  235. * Prevent writes to uninitialized FLASH.
  236. */
  237. if (info->flash_id != FLASH_UNKNOWN) {
  238. addr = (volatile unsigned long *)info->start[0];
  239. *addr = 0x00F000F0; /* reset bank */
  240. }
  241. return (info->size);
  242. }
  243. /*-----------------------------------------------------------------------
  244. */
  245. int flash_erase (flash_info_t *info, int s_first, int s_last)
  246. {
  247. vu_long *addr = (vu_long*)(info->start[0]);
  248. int flag, prot, sect, l_sect;
  249. ulong start, now, last;
  250. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  251. if ((s_first < 0) || (s_first > s_last)) {
  252. if (info->flash_id == FLASH_UNKNOWN) {
  253. printf ("- missing\n");
  254. } else {
  255. printf ("- no sectors to erase\n");
  256. }
  257. return 1;
  258. }
  259. if ((info->flash_id == FLASH_UNKNOWN) ||
  260. (info->flash_id > FLASH_AMD_COMP)) {
  261. printf ("Can't erase unknown flash type %08lx - aborted\n",
  262. info->flash_id);
  263. return 1;
  264. }
  265. prot = 0;
  266. for (sect=s_first; sect<=s_last; ++sect) {
  267. if (info->protect[sect]) {
  268. prot++;
  269. }
  270. }
  271. if (prot) {
  272. printf ("- Warning: %d protected sectors will not be erased!\n",
  273. prot);
  274. } else {
  275. printf ("\n");
  276. }
  277. l_sect = -1;
  278. /* Disable interrupts which might cause a timeout here */
  279. flag = disable_interrupts();
  280. addr[0x0555] = 0x00AA00AA;
  281. addr[0x02AA] = 0x00550055;
  282. addr[0x0555] = 0x00800080;
  283. addr[0x0555] = 0x00AA00AA;
  284. addr[0x02AA] = 0x00550055;
  285. /* Start erase on unprotected sectors */
  286. for (sect = s_first; sect<=s_last; sect++) {
  287. if (info->protect[sect] == 0) { /* not protected */
  288. addr = (vu_long*)(info->start[sect]);
  289. addr[0] = 0x00300030;
  290. l_sect = sect;
  291. }
  292. }
  293. /* re-enable interrupts if necessary */
  294. if (flag)
  295. enable_interrupts();
  296. /* wait at least 80us - let's wait 1 ms */
  297. udelay (1000);
  298. /*
  299. * We wait for the last triggered sector
  300. */
  301. if (l_sect < 0)
  302. goto DONE;
  303. start = get_timer (0);
  304. last = start;
  305. addr = (vu_long*)(info->start[l_sect]);
  306. while ((addr[0] & 0x00800080) != 0x00800080) {
  307. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  308. printf ("Timeout\n");
  309. return 1;
  310. }
  311. /* show that we're waiting */
  312. if ((now - last) > 100000) { /* every second */
  313. putc ('.');
  314. last = now;
  315. }
  316. }
  317. DONE:
  318. /* reset to read mode */
  319. addr = (volatile unsigned long *)info->start[0];
  320. addr[0] = 0x00F000F0; /* reset bank */
  321. printf (" done\n");
  322. return 0;
  323. }
  324. /*-----------------------------------------------------------------------
  325. * Copy memory to flash, returns:
  326. * 0 - OK
  327. * 1 - write timeout
  328. * 2 - Flash not erased
  329. */
  330. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  331. {
  332. ulong cp, wp, data;
  333. int i, l, rc;
  334. wp = (addr & ~3); /* get lower word aligned address */
  335. /*
  336. * handle unaligned start bytes
  337. */
  338. if ((l = addr - wp) != 0) {
  339. data = 0;
  340. for (i=0, cp=wp; i<l; ++i, ++cp) {
  341. data = (data << 8) | (*(uchar *)cp);
  342. }
  343. for (; i<4 && cnt>0; ++i) {
  344. data = (data << 8) | *src++;
  345. --cnt;
  346. ++cp;
  347. }
  348. for (; cnt==0 && i<4; ++i, ++cp) {
  349. data = (data << 8) | (*(uchar *)cp);
  350. }
  351. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  352. return (rc);
  353. }
  354. wp += 4;
  355. }
  356. /*
  357. * handle word aligned part
  358. */
  359. while (cnt >= 4) {
  360. data = 0;
  361. for (i=0; i<4; ++i) {
  362. data = (data << 8) | *src++;
  363. }
  364. if ((rc = write_word(info, wp, SWAP(data))) != 0) {
  365. return (rc);
  366. }
  367. wp += 4;
  368. cnt -= 4;
  369. }
  370. if (cnt == 0) {
  371. return (0);
  372. }
  373. /*
  374. * handle unaligned tail bytes
  375. */
  376. data = 0;
  377. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  378. data = (data << 8) | *src++;
  379. --cnt;
  380. }
  381. for (; i<4; ++i, ++cp) {
  382. data = (data << 8) | (*(uchar *)cp);
  383. }
  384. return (write_word(info, wp, SWAP(data)));
  385. }
  386. /*-----------------------------------------------------------------------
  387. * Write a word to Flash, returns:
  388. * 0 - OK
  389. * 1 - write timeout
  390. * 2 - Flash not erased
  391. */
  392. static int write_word (flash_info_t *info, ulong dest, ulong data)
  393. {
  394. vu_long *addr = (vu_long*)(info->start[0]);
  395. ulong start;
  396. int flag;
  397. /* Check if Flash is (sufficiently) erased */
  398. if ((*((vu_long *)dest) & data) != data) {
  399. return (2);
  400. }
  401. /* Disable interrupts which might cause a timeout here */
  402. flag = disable_interrupts();
  403. addr[0x0555] = 0x00AA00AA;
  404. addr[0x02AA] = 0x00550055;
  405. addr[0x0555] = 0x00A000A0;
  406. *((vu_long *)dest) = data;
  407. /* re-enable interrupts if necessary */
  408. if (flag)
  409. enable_interrupts();
  410. /* data polling for D7 */
  411. start = get_timer (0);
  412. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  413. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  414. return (1);
  415. }
  416. }
  417. return (0);
  418. }