flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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[CONFIG_SYS_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. int i;
  76. /* Init: no FLASHes known */
  77. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
  78. flash_info[i].flash_id = FLASH_UNKNOWN;
  79. /* for now, only support the 4 MB Flash SIMM */
  80. (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
  81. &flash_info[0]);
  82. /*
  83. * protect monitor and environment sectors
  84. */
  85. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
  86. flash_protect(FLAG_PROTECT_SET,
  87. CONFIG_SYS_MONITOR_BASE,
  88. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  89. &flash_info[0]);
  90. #endif
  91. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  92. #ifndef CONFIG_ENV_SIZE
  93. #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  94. #endif
  95. flash_protect(FLAG_PROTECT_SET,
  96. CONFIG_ENV_ADDR,
  97. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  98. #endif
  99. return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024; /*size */
  100. }
  101. /*********************************************************************/
  102. /* NAME: flash_print_info() - prints flash imformation */
  103. /* */
  104. /* DESCRIPTION: */
  105. /* This function prints the flash information. */
  106. /* */
  107. /* INPUTS: */
  108. /* flash_info_t *info - flash information structure */
  109. /* */
  110. /* OUTPUTS: */
  111. /* Displays flash information to console */
  112. /* */
  113. /* RETURNS: */
  114. /* None */
  115. /* */
  116. /* RESTRICTIONS/LIMITATIONS: */
  117. /* */
  118. /* */
  119. /*********************************************************************/
  120. void flash_print_info (flash_info_t *info)
  121. {
  122. int i;
  123. if (info->flash_id == FLASH_UNKNOWN) {
  124. printf ("missing or unknown FLASH type\n");
  125. return;
  126. }
  127. switch ((info->flash_id >> 16) & 0xff) {
  128. case 0x1:
  129. printf ("AMD ");
  130. break;
  131. default:
  132. printf ("Unknown Vendor ");
  133. break;
  134. }
  135. switch (info->flash_id & FLASH_TYPEMASK) {
  136. case AMD_ID_F040B:
  137. printf ("AM29F040B (4 Mbit)\n");
  138. break;
  139. case AMD_ID_F080B:
  140. printf ("AM29F080B (8 Mbit)\n");
  141. break;
  142. case AMD_ID_F016D:
  143. printf ("AM29F016D (16 Mbit)\n");
  144. break;
  145. default:
  146. printf ("Unknown Chip Type\n");
  147. break;
  148. }
  149. printf (" Size: %ld MB in %d Sectors\n",
  150. info->size >> 20, info->sector_count);
  151. printf (" Sector Start Addresses:");
  152. for (i=0; i<info->sector_count; ++i) {
  153. if ((i % 5) == 0)
  154. printf ("\n ");
  155. printf (" %08lX%s",
  156. info->start[i],
  157. info->protect[i] ? " (RO)" : " "
  158. );
  159. }
  160. printf ("\n");
  161. return;
  162. }
  163. /*********************************************************************/
  164. /* The following code cannot be run from FLASH! */
  165. /*********************************************************************/
  166. /*********************************************************************/
  167. /* NAME: flash_get_size() - detects the flash size */
  168. /* */
  169. /* DESCRIPTION: */
  170. /* 1) Reads vendor ID and devices ID from the flash devices. */
  171. /* 2) Initializes flash info struct. */
  172. /* 3) Return the flash size */
  173. /* */
  174. /* INPUTS: */
  175. /* vu_long *addr - pointer to start of flash */
  176. /* flash_info_t *info - flash information structure */
  177. /* */
  178. /* OUTPUTS: */
  179. /* None */
  180. /* */
  181. /* RETURNS: */
  182. /* Size of the flash in bytes, or 0 if device id is unknown. */
  183. /* */
  184. /* RESTRICTIONS/LIMITATIONS: */
  185. /* Only supports the following devices: */
  186. /* AM29F080D */
  187. /* AM29F016D */
  188. /* */
  189. /*********************************************************************/
  190. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  191. {
  192. short i;
  193. vu_long vendor, devid;
  194. ulong base = (ulong)addr;
  195. /*printf("addr = %08lx\n", (unsigned long)addr); */
  196. /* Reset and Write auto select command: read Manufacturer ID */
  197. addr[0x0000] = 0xf0f0f0f0;
  198. addr[0x0555] = 0xAAAAAAAA;
  199. addr[0x02AA] = 0x55555555;
  200. addr[0x0555] = 0x90909090;
  201. udelay (1000);
  202. vendor = addr[0];
  203. /*printf("vendor = %08lx\n", vendor); */
  204. if (vendor != 0x01010101) {
  205. info->size = 0;
  206. goto out;
  207. }
  208. devid = addr[1];
  209. /*printf("devid = %08lx\n", devid); */
  210. if ((devid & 0xff) == AMD_ID_F080B) {
  211. info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
  212. /* we have 16 sectors with 64KB each x 4 */
  213. info->sector_count = 16;
  214. info->size = 4 * info->sector_count * 64*1024;
  215. } else if ((devid & 0xff) == AMD_ID_F016D){
  216. info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F016D;
  217. /* we have 32 sectors with 64KB each x 4 */
  218. info->sector_count = 32;
  219. info->size = 4 * info->sector_count * 64*1024;
  220. } else {
  221. info->size = 0;
  222. goto out;
  223. }
  224. /*printf("sector count = %08x\n", info->sector_count); */
  225. /* check for protected sectors */
  226. for (i = 0; i < info->sector_count; i++) {
  227. /* sector base address */
  228. info->start[i] = base + i * (info->size / info->sector_count);
  229. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  230. /* D0 = 1 if protected */
  231. addr = (volatile unsigned long *)(info->start[i]);
  232. info->protect[i] = addr[2] & 1;
  233. }
  234. /* reset command */
  235. addr = (vu_long *)info->start[0];
  236. out:
  237. addr[0] = 0xf0f0f0f0;
  238. /*printf("size = %08x\n", info->size); */
  239. return info->size;
  240. }
  241. /*********************************************************************/
  242. /* NAME: flash_erase() - erases flash by sector */
  243. /* */
  244. /* DESCRIPTION: */
  245. /* This function erases flash sectors starting for s_first to */
  246. /* s_last. */
  247. /* */
  248. /* INPUTS: */
  249. /* flash_info_t *info - flash information structure */
  250. /* int s_first - first sector to erase */
  251. /* int s_last - last sector to erase */
  252. /* */
  253. /* OUTPUTS: */
  254. /* None */
  255. /* */
  256. /* RETURNS: */
  257. /* Returns 0 for success, 1 for failure. */
  258. /* */
  259. /* RESTRICTIONS/LIMITATIONS: */
  260. /* */
  261. /*********************************************************************/
  262. int flash_erase (flash_info_t *info, int s_first, int s_last)
  263. {
  264. vu_long *addr = (vu_long*)(info->start[0]);
  265. int flag, prot, sect, l_sect;
  266. ulong start, now, last;
  267. if ((s_first < 0) || (s_first > s_last)) {
  268. if (info->flash_id == FLASH_UNKNOWN) {
  269. printf ("- missing\n");
  270. } else {
  271. printf ("- no sectors to erase\n");
  272. }
  273. return 1;
  274. }
  275. prot = 0;
  276. for (sect = s_first; sect <= s_last; sect++) {
  277. if (info->protect[sect]) {
  278. prot++;
  279. }
  280. }
  281. if (prot) {
  282. printf ("- Warning: %d protected sectors will not be erased!\n",
  283. prot);
  284. } else {
  285. printf ("\n");
  286. }
  287. l_sect = -1;
  288. /* Disable interrupts which might cause a timeout here */
  289. flag = disable_interrupts();
  290. addr[0x0555] = 0xAAAAAAAA;
  291. addr[0x02AA] = 0x55555555;
  292. addr[0x0555] = 0x80808080;
  293. addr[0x0555] = 0xAAAAAAAA;
  294. addr[0x02AA] = 0x55555555;
  295. udelay (100);
  296. /* Start erase on unprotected sectors */
  297. for (sect = s_first; sect <= s_last; sect++) {
  298. if (info->protect[sect] == 0) { /* not protected */
  299. addr = (vu_long*)(info->start[sect]);
  300. addr[0] = 0x30303030;
  301. l_sect = sect;
  302. }
  303. }
  304. /* re-enable interrupts if necessary */
  305. if (flag)
  306. enable_interrupts();
  307. /* wait at least 80us - let's wait 1 ms */
  308. udelay (1000);
  309. /*
  310. * We wait for the last triggered sector
  311. */
  312. if (l_sect < 0)
  313. goto DONE;
  314. start = get_timer (0);
  315. last = start;
  316. addr = (vu_long*)(info->start[l_sect]);
  317. while ((addr[0] & 0x80808080) != 0x80808080) {
  318. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  319. printf ("Timeout\n");
  320. return 1;
  321. }
  322. /* show that we're waiting */
  323. if ((now - last) > 1000) { /* every second */
  324. serial_putc ('.');
  325. last = now;
  326. }
  327. }
  328. DONE:
  329. /* reset to read mode */
  330. addr = (volatile unsigned long *)info->start[0];
  331. addr[0] = 0xF0F0F0F0; /* reset bank */
  332. printf (" done\n");
  333. return 0;
  334. }
  335. /*********************************************************************/
  336. /* NAME: write_buff() - writes a buffer to flash */
  337. /* */
  338. /* DESCRIPTION: */
  339. /* This function copies a buffer, *src, to flash. */
  340. /* */
  341. /* INPUTS: */
  342. /* flash_info_t *info - flash information structure */
  343. /* uchar *src - pointer to buffer to write to flash */
  344. /* ulong addr - address to start write at */
  345. /* ulong cnt - number of bytes to write to flash */
  346. /* */
  347. /* OUTPUTS: */
  348. /* None */
  349. /* */
  350. /* RETURNS: */
  351. /* 0 - OK */
  352. /* 1 - write timeout */
  353. /* 2 - Flash not erased */
  354. /* */
  355. /* RESTRICTIONS/LIMITATIONS: */
  356. /* */
  357. /*********************************************************************/
  358. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  359. {
  360. ulong cp, wp, data;
  361. int i, l, rc;
  362. wp = (addr & ~3); /* get lower word aligned address */
  363. /*
  364. * handle unaligned start bytes
  365. */
  366. if ((l = addr - wp) != 0) {
  367. data = 0;
  368. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  369. data = (data << 8) | (*(uchar *)cp);
  370. }
  371. for (; (i < 4) && (cnt > 0); ++i) {
  372. data = (data << 8) | *src++;
  373. --cnt;
  374. ++cp;
  375. }
  376. for (; (cnt == 0) && (i < 4); ++i, ++cp) {
  377. data = (data << 8) | (*(uchar *)cp);
  378. }
  379. if ((rc = write_word(info, wp, data)) != 0) {
  380. return (rc);
  381. }
  382. wp += 4;
  383. }
  384. /*
  385. * handle word aligned part
  386. */
  387. while (cnt >= 4) {
  388. data = 0;
  389. for (i = 0; i < 4; ++i) {
  390. data = (data << 8) | *src++;
  391. }
  392. if ((rc = write_word(info, wp, data)) != 0) {
  393. return (rc);
  394. }
  395. wp += 4;
  396. cnt -= 4;
  397. }
  398. if (cnt == 0) {
  399. return (0);
  400. }
  401. /*
  402. * handle unaligned tail bytes
  403. */
  404. data = 0;
  405. for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
  406. data = (data << 8) | *src++;
  407. --cnt;
  408. }
  409. for (; (i < 4); ++i, ++cp) {
  410. data = (data << 8) | (*(uchar *)cp);
  411. }
  412. return (write_word(info, wp, data));
  413. }
  414. /*********************************************************************/
  415. /* NAME: write_word() - writes a word to flash */
  416. /* */
  417. /* DESCRIPTION: */
  418. /* This writes a single word to flash. */
  419. /* */
  420. /* INPUTS: */
  421. /* flash_info_t *info - flash information structure */
  422. /* ulong dest - address to write */
  423. /* ulong data - data to write */
  424. /* */
  425. /* OUTPUTS: */
  426. /* None */
  427. /* */
  428. /* RETURNS: */
  429. /* 0 - OK */
  430. /* 1 - write timeout */
  431. /* 2 - Flash not erased */
  432. /* */
  433. /* RESTRICTIONS/LIMITATIONS: */
  434. /* */
  435. /*********************************************************************/
  436. static int write_word (flash_info_t *info, ulong dest, ulong data)
  437. {
  438. vu_long *addr = (vu_long*)(info->start[0]);
  439. ulong start;
  440. int flag;
  441. /* Check if Flash is (sufficiently) erased */
  442. if ((*((vu_long *)dest) & data) != data) {
  443. return (2);
  444. }
  445. /* Disable interrupts which might cause a timeout here */
  446. flag = disable_interrupts();
  447. addr[0x0555] = 0xAAAAAAAA;
  448. addr[0x02AA] = 0x55555555;
  449. addr[0x0555] = 0xA0A0A0A0;
  450. *((vu_long *)dest) = data;
  451. /* re-enable interrupts if necessary */
  452. if (flag)
  453. enable_interrupts();
  454. /* data polling for D7 */
  455. start = get_timer (0);
  456. while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
  457. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  458. return (1);
  459. }
  460. }
  461. return (0);
  462. }
  463. /*********************************************************************/
  464. /* End of flash.c */
  465. /*********************************************************************/