flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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, 2001
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * (C) Copyright 2001
  10. * Advent Networks, Inc. <http://www.adventnetworks.com>
  11. * Oliver Brown <oliverb@alumni.utexas.net>
  12. *
  13. *--------------------------------------------------------------------
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. /*********************************************************************/
  33. /* DESCRIPTION:
  34. * This file contains the flash routines for the GW8260 board.
  35. *
  36. *
  37. *
  38. * MODULE DEPENDENCY:
  39. * None
  40. *
  41. *
  42. * RESTRICTIONS/LIMITATIONS:
  43. *
  44. * Only supports the following flash devices:
  45. * AMD 29F080B
  46. * AMD 29F016D
  47. *
  48. * Copyright (c) 2001, Advent Networks, Inc.
  49. *
  50. */
  51. /*********************************************************************/
  52. #include <common.h>
  53. #include <mpc8260.h>
  54. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  55. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  56. static int write_word (flash_info_t *info, ulong dest, ulong data);
  57. /*********************************************************************/
  58. /* functions */
  59. /*********************************************************************/
  60. /*********************************************************************/
  61. /* NAME: flash_init() - initializes flash banks */
  62. /* */
  63. /* DESCRIPTION: */
  64. /* This function initializes the flash bank(s). */
  65. /* */
  66. /* RETURNS: */
  67. /* The size in bytes of the flash */
  68. /* */
  69. /* RESTRICTIONS/LIMITATIONS: */
  70. /* */
  71. /* */
  72. /*********************************************************************/
  73. unsigned long flash_init (void)
  74. {
  75. unsigned long size;
  76. int i;
  77. /* Init: no FLASHes known */
  78. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  79. flash_info[i].flash_id = FLASH_UNKNOWN;
  80. }
  81. /* for now, only support the 4 MB Flash SIMM */
  82. size = flash_get_size((vu_long *)CFG_FLASH0_BASE, &flash_info[0]);
  83. /*
  84. * protect monitor and environment sectors
  85. */
  86. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  87. flash_protect(FLAG_PROTECT_SET,
  88. CFG_MONITOR_BASE,
  89. CFG_MONITOR_BASE+monitor_flash_len-1,
  90. &flash_info[0]);
  91. #endif
  92. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  93. # ifndef CONFIG_ENV_SIZE
  94. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  95. # endif
  96. flash_protect(FLAG_PROTECT_SET,
  97. CONFIG_ENV_ADDR,
  98. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  99. &flash_info[0]);
  100. #endif
  101. return (CFG_FLASH0_SIZE * 1024 * 1024); /*size*/
  102. }
  103. /*********************************************************************/
  104. /* NAME: flash_print_info() - prints flash imformation */
  105. /* */
  106. /* DESCRIPTION: */
  107. /* This function prints the flash information. */
  108. /* */
  109. /* INPUTS: */
  110. /* flash_info_t *info - flash information structure */
  111. /* */
  112. /* OUTPUTS: */
  113. /* Displays flash information to console */
  114. /* */
  115. /* RETURNS: */
  116. /* None */
  117. /* */
  118. /* RESTRICTIONS/LIMITATIONS: */
  119. /* */
  120. /* */
  121. /*********************************************************************/
  122. void flash_print_info (flash_info_t *info)
  123. {
  124. int i;
  125. if (info->flash_id == FLASH_UNKNOWN) {
  126. printf ("missing or unknown FLASH type\n");
  127. return;
  128. }
  129. switch ((info->flash_id >> 16) & 0xff) {
  130. case 0x1:
  131. printf ("AMD ");
  132. break;
  133. default:
  134. printf ("Unknown Vendor ");
  135. break;
  136. }
  137. switch (info->flash_id & FLASH_TYPEMASK) {
  138. case AMD_ID_F040B:
  139. printf ("AM29F040B (4 Mbit)\n");
  140. break;
  141. case AMD_ID_F080B:
  142. printf ("AM29F080B (8 Mbit)\n");
  143. break;
  144. case AMD_ID_F016D:
  145. printf ("AM29F016D (16 Mbit)\n");
  146. break;
  147. default:
  148. printf ("Unknown Chip Type\n");
  149. break;
  150. }
  151. printf (" Size: %ld MB in %d Sectors\n",
  152. info->size >> 20, info->sector_count);
  153. printf (" Sector Start Addresses:");
  154. for (i=0; i<info->sector_count; ++i) {
  155. if ((i % 5) == 0)
  156. printf ("\n ");
  157. printf (" %08lX%s",
  158. info->start[i],
  159. info->protect[i] ? " (RO)" : " "
  160. );
  161. }
  162. printf ("\n");
  163. return;
  164. }
  165. /*********************************************************************/
  166. /* The following code cannot be run from FLASH! */
  167. /*********************************************************************/
  168. /*********************************************************************/
  169. /* NAME: flash_get_size() - detects the flash size */
  170. /* */
  171. /* DESCRIPTION: */
  172. /* 1) Reads vendor ID and devices ID from the flash devices. */
  173. /* 2) Initializes flash info struct. */
  174. /* 3) Return the flash size */
  175. /* */
  176. /* INPUTS: */
  177. /* vu_long *addr - pointer to start of flash */
  178. /* flash_info_t *info - flash information structure */
  179. /* */
  180. /* OUTPUTS: */
  181. /* None */
  182. /* */
  183. /* RETURNS: */
  184. /* Size of the flash in bytes, or 0 if device id is unknown. */
  185. /* */
  186. /* RESTRICTIONS/LIMITATIONS: */
  187. /* Only supports the following devices: */
  188. /* AM29F080D */
  189. /* AM29F016D */
  190. /* */
  191. /*********************************************************************/
  192. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  193. {
  194. short i;
  195. vu_long vendor, devid;
  196. ulong base = (ulong)addr;
  197. /*printf("addr = %08lx\n", (unsigned long)addr); */
  198. /* Reset and Write auto select command: read Manufacturer ID */
  199. addr[0x0000] = 0xf0f0f0f0;
  200. addr[0x0555] = 0xAAAAAAAA;
  201. addr[0x02AA] = 0x55555555;
  202. addr[0x0555] = 0x90909090;
  203. udelay (1000);
  204. vendor = addr[0];
  205. /*printf("vendor = %08lx\n", vendor); */
  206. if (vendor != 0x01010101) {
  207. info->size = 0;
  208. goto out;
  209. }
  210. devid = addr[1];
  211. /*printf("devid = %08lx\n", devid); */
  212. if ((devid & 0xff) == AMD_ID_F080B) {
  213. info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
  214. /* we have 16 sectors with 64KB each x 4 */
  215. info->sector_count = 16;
  216. info->size = 4 * info->sector_count * 64*1024;
  217. } else if ((devid & 0xff) == AMD_ID_F016D){
  218. info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F016D;
  219. /* we have 32 sectors with 64KB each x 4 */
  220. info->sector_count = 32;
  221. info->size = 4 * info->sector_count * 64*1024;
  222. } else {
  223. info->size = 0;
  224. goto out;
  225. }
  226. /*printf("sector count = %08x\n", info->sector_count); */
  227. /* check for protected sectors */
  228. for (i = 0; i < info->sector_count; i++) {
  229. /* sector base address */
  230. info->start[i] = base + i * (info->size / info->sector_count);
  231. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  232. /* D0 = 1 if protected */
  233. addr = (volatile unsigned long *)(info->start[i]);
  234. info->protect[i] = addr[2] & 1;
  235. }
  236. /* reset command */
  237. addr = (vu_long *)info->start[0];
  238. out:
  239. addr[0] = 0xf0f0f0f0;
  240. /*printf("size = %08x\n", info->size); */
  241. return info->size;
  242. }
  243. /*********************************************************************/
  244. /* NAME: flash_erase() - erases flash by sector */
  245. /* */
  246. /* DESCRIPTION: */
  247. /* This function erases flash sectors starting for s_first to */
  248. /* s_last. */
  249. /* */
  250. /* INPUTS: */
  251. /* flash_info_t *info - flash information structure */
  252. /* int s_first - first sector to erase */
  253. /* int s_last - last sector to erase */
  254. /* */
  255. /* OUTPUTS: */
  256. /* None */
  257. /* */
  258. /* RETURNS: */
  259. /* Returns 0 for success, 1 for failure. */
  260. /* */
  261. /* RESTRICTIONS/LIMITATIONS: */
  262. /* */
  263. /*********************************************************************/
  264. int flash_erase (flash_info_t *info, int s_first, int s_last)
  265. {
  266. vu_long *addr = (vu_long*)(info->start[0]);
  267. int flag, prot, sect, l_sect;
  268. ulong start, now, last;
  269. if ((s_first < 0) || (s_first > s_last)) {
  270. if (info->flash_id == FLASH_UNKNOWN) {
  271. printf ("- missing\n");
  272. } else {
  273. printf ("- no sectors to erase\n");
  274. }
  275. return 1;
  276. }
  277. prot = 0;
  278. for (sect = s_first; sect <= s_last; sect++) {
  279. if (info->protect[sect]) {
  280. prot++;
  281. }
  282. }
  283. if (prot) {
  284. printf ("- Warning: %d protected sectors will not be erased!\n",
  285. prot);
  286. } else {
  287. printf ("\n");
  288. }
  289. l_sect = -1;
  290. /* Disable interrupts which might cause a timeout here */
  291. flag = disable_interrupts();
  292. addr[0x0555] = 0xAAAAAAAA;
  293. addr[0x02AA] = 0x55555555;
  294. addr[0x0555] = 0x80808080;
  295. addr[0x0555] = 0xAAAAAAAA;
  296. addr[0x02AA] = 0x55555555;
  297. udelay (100);
  298. /* Start erase on unprotected sectors */
  299. for (sect = s_first; sect <= s_last; sect++) {
  300. if (info->protect[sect] == 0) { /* not protected */
  301. addr = (vu_long*)(info->start[sect]);
  302. addr[0] = 0x30303030;
  303. l_sect = sect;
  304. }
  305. }
  306. /* re-enable interrupts if necessary */
  307. if (flag)
  308. enable_interrupts();
  309. /* wait at least 80us - let's wait 1 ms */
  310. udelay (1000);
  311. /*
  312. * We wait for the last triggered sector
  313. */
  314. if (l_sect < 0)
  315. goto DONE;
  316. start = get_timer (0);
  317. last = start;
  318. addr = (vu_long*)(info->start[l_sect]);
  319. while ((addr[0] & 0x80808080) != 0x80808080) {
  320. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  321. printf ("Timeout\n");
  322. return 1;
  323. }
  324. /* show that we're waiting */
  325. if ((now - last) > 1000) { /* every second */
  326. serial_putc ('.');
  327. last = now;
  328. }
  329. }
  330. DONE:
  331. /* reset to read mode */
  332. addr = (volatile unsigned long *)info->start[0];
  333. addr[0] = 0xF0F0F0F0; /* reset bank */
  334. printf (" done\n");
  335. return 0;
  336. }
  337. /*********************************************************************/
  338. /* NAME: write_buff() - writes a buffer to flash */
  339. /* */
  340. /* DESCRIPTION: */
  341. /* This function copies a buffer, *src, to flash. */
  342. /* */
  343. /* INPUTS: */
  344. /* flash_info_t *info - flash information structure */
  345. /* uchar *src - pointer to buffer to write to flash */
  346. /* ulong addr - address to start write at */
  347. /* ulong cnt - number of bytes to write to flash */
  348. /* */
  349. /* OUTPUTS: */
  350. /* None */
  351. /* */
  352. /* RETURNS: */
  353. /* 0 - OK */
  354. /* 1 - write timeout */
  355. /* 2 - Flash not erased */
  356. /* */
  357. /* RESTRICTIONS/LIMITATIONS: */
  358. /* */
  359. /*********************************************************************/
  360. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  361. {
  362. ulong cp, wp, data;
  363. int i, l, rc;
  364. wp = (addr & ~3); /* get lower word aligned address */
  365. /*
  366. * handle unaligned start bytes
  367. */
  368. if ((l = addr - wp) != 0) {
  369. data = 0;
  370. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  371. data = (data << 8) | (*(uchar *)cp);
  372. }
  373. for (; (i < 4) && (cnt > 0); ++i) {
  374. data = (data << 8) | *src++;
  375. --cnt;
  376. ++cp;
  377. }
  378. for (; (cnt == 0) && (i < 4); ++i, ++cp) {
  379. data = (data << 8) | (*(uchar *)cp);
  380. }
  381. if ((rc = write_word(info, wp, data)) != 0) {
  382. return (rc);
  383. }
  384. wp += 4;
  385. }
  386. /*
  387. * handle word aligned part
  388. */
  389. while (cnt >= 4) {
  390. data = 0;
  391. for (i = 0; i < 4; ++i) {
  392. data = (data << 8) | *src++;
  393. }
  394. if ((rc = write_word(info, wp, data)) != 0) {
  395. return (rc);
  396. }
  397. wp += 4;
  398. cnt -= 4;
  399. }
  400. if (cnt == 0) {
  401. return (0);
  402. }
  403. /*
  404. * handle unaligned tail bytes
  405. */
  406. data = 0;
  407. for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
  408. data = (data << 8) | *src++;
  409. --cnt;
  410. }
  411. for (; (i < 4); ++i, ++cp) {
  412. data = (data << 8) | (*(uchar *)cp);
  413. }
  414. return (write_word(info, wp, data));
  415. }
  416. /*********************************************************************/
  417. /* NAME: write_word() - writes a word to flash */
  418. /* */
  419. /* DESCRIPTION: */
  420. /* This writes a single word to flash. */
  421. /* */
  422. /* INPUTS: */
  423. /* flash_info_t *info - flash information structure */
  424. /* ulong dest - address to write */
  425. /* ulong data - data to write */
  426. /* */
  427. /* OUTPUTS: */
  428. /* None */
  429. /* */
  430. /* RETURNS: */
  431. /* 0 - OK */
  432. /* 1 - write timeout */
  433. /* 2 - Flash not erased */
  434. /* */
  435. /* RESTRICTIONS/LIMITATIONS: */
  436. /* */
  437. /*********************************************************************/
  438. static int write_word (flash_info_t *info, ulong dest, ulong data)
  439. {
  440. vu_long *addr = (vu_long*)(info->start[0]);
  441. ulong start;
  442. int flag;
  443. /* Check if Flash is (sufficiently) erased */
  444. if ((*((vu_long *)dest) & data) != data) {
  445. return (2);
  446. }
  447. /* Disable interrupts which might cause a timeout here */
  448. flag = disable_interrupts();
  449. addr[0x0555] = 0xAAAAAAAA;
  450. addr[0x02AA] = 0x55555555;
  451. addr[0x0555] = 0xA0A0A0A0;
  452. *((vu_long *)dest) = data;
  453. /* re-enable interrupts if necessary */
  454. if (flag)
  455. enable_interrupts();
  456. /* data polling for D7 */
  457. start = get_timer (0);
  458. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  459. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  460. return (1);
  461. }
  462. }
  463. return (0);
  464. }
  465. /*********************************************************************/
  466. /* End of flash.c */
  467. /*********************************************************************/