flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001-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. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  29. /* Board support for 1 or 2 flash devices */
  30. #define FLASH_PORT_WIDTH32
  31. #undef FLASH_PORT_WIDTH16
  32. #ifdef FLASH_PORT_WIDTH16
  33. #define FLASH_PORT_WIDTH ushort
  34. #define FLASH_PORT_WIDTHV vu_short
  35. #define SWAP(x) (x)
  36. #else
  37. #define FLASH_PORT_WIDTH ulong
  38. #define FLASH_PORT_WIDTHV vu_long
  39. #define SWAP(x) (x)
  40. #endif
  41. /* Intel-compatible flash ID */
  42. #define INTEL_COMPAT 0x00890089
  43. #define INTEL_ALT 0x00B000B0
  44. /* Intel-compatible flash commands */
  45. #define INTEL_PROGRAM 0x00100010
  46. #define INTEL_ERASE 0x00200020
  47. #define INTEL_CLEAR 0x00500050
  48. #define INTEL_LOCKBIT 0x00600060
  49. #define INTEL_PROTECT 0x00010001
  50. #define INTEL_STATUS 0x00700070
  51. #define INTEL_READID 0x00900090
  52. #define INTEL_CONFIRM 0x00D000D0
  53. #define INTEL_RESET 0xFFFFFFFF
  54. /* Intel-compatible flash status bits */
  55. #define INTEL_FINISHED 0x00800080
  56. #define INTEL_OK 0x00800080
  57. #define FPW FLASH_PORT_WIDTH
  58. #define FPWV FLASH_PORT_WIDTHV
  59. #define mb() __asm__ __volatile__ ("" : : : "memory")
  60. /*-----------------------------------------------------------------------
  61. * Functions
  62. */
  63. static ulong flash_get_size (FPW *addr, flash_info_t *info);
  64. static int write_data (flash_info_t *info, ulong dest, FPW data);
  65. static void flash_get_offsets (ulong base, flash_info_t *info);
  66. void inline spin_wheel (void);
  67. /*-----------------------------------------------------------------------
  68. */
  69. unsigned long flash_init (void)
  70. {
  71. int i;
  72. ulong size = 0;
  73. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  74. switch (i) {
  75. case 0:
  76. flash_get_size ((FPW *) CFG_FLASH_BASE, &flash_info[i]);
  77. flash_get_offsets (CFG_FLASH_BASE, &flash_info[i]);
  78. break;
  79. default:
  80. panic ("configured to many flash banks!\n");
  81. break;
  82. }
  83. size += flash_info[i].size;
  84. }
  85. /* Protect monitor and environment sectors
  86. */
  87. flash_protect ( FLAG_PROTECT_SET,
  88. CFG_MONITOR_BASE,
  89. CFG_MONITOR_BASE + monitor_flash_len - 1,
  90. &flash_info[0] );
  91. flash_protect ( FLAG_PROTECT_SET,
  92. CFG_ENV_ADDR,
  93. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
  94. return size;
  95. }
  96. /*-----------------------------------------------------------------------
  97. */
  98. static void flash_get_offsets (ulong base, flash_info_t *info)
  99. {
  100. int i;
  101. if (info->flash_id == FLASH_UNKNOWN) {
  102. return;
  103. }
  104. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  105. for (i = 0; i < info->sector_count; i++) {
  106. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  107. info->protect[i] = 0;
  108. }
  109. }
  110. }
  111. /*-----------------------------------------------------------------------
  112. */
  113. void flash_print_info (flash_info_t *info)
  114. {
  115. int i;
  116. if (info->flash_id == FLASH_UNKNOWN) {
  117. printf ("missing or unknown FLASH type\n");
  118. return;
  119. }
  120. switch (info->flash_id & FLASH_VENDMASK) {
  121. case FLASH_MAN_INTEL:
  122. printf ("INTEL ");
  123. break;
  124. default:
  125. printf ("Unknown Vendor ");
  126. break;
  127. }
  128. switch (info->flash_id & FLASH_TYPEMASK) {
  129. case FLASH_28F128J3A:
  130. printf ("28F128J3A\n");
  131. break;
  132. case FLASH_28F640J3A:
  133. printf ("28F640J3A\n");
  134. break;
  135. case FLASH_28F320J3A:
  136. printf ("28F320J3A\n");
  137. break;
  138. default:
  139. printf ("Unknown Chip Type\n");
  140. break;
  141. }
  142. printf (" Size: %ld MB in %d Sectors\n",
  143. info->size >> 20, info->sector_count);
  144. printf (" Sector Start Addresses:");
  145. for (i = 0; i < info->sector_count; ++i) {
  146. if ((i % 5) == 0)
  147. printf ("\n ");
  148. printf (" %08lX%s",
  149. info->start[i],
  150. info->protect[i] ? " (RO)" : " ");
  151. }
  152. printf ("\n");
  153. return;
  154. }
  155. /*
  156. * The following code cannot be run from FLASH!
  157. */
  158. static ulong flash_get_size (FPW *addr, flash_info_t *info)
  159. {
  160. volatile FPW value;
  161. /* Write auto select command: read Manufacturer ID */
  162. addr[0x5555] = (FPW) 0x00AA00AA;
  163. addr[0x2AAA] = (FPW) 0x00550055;
  164. addr[0x5555] = (FPW) 0x00900090;
  165. mb ();
  166. value = addr[0];
  167. switch (value) {
  168. case (FPW) INTEL_MANUFACT:
  169. info->flash_id = FLASH_MAN_INTEL;
  170. break;
  171. default:
  172. info->flash_id = FLASH_UNKNOWN;
  173. info->sector_count = 0;
  174. info->size = 0;
  175. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  176. return (0); /* no or unknown flash */
  177. }
  178. mb ();
  179. value = addr[1]; /* device ID */
  180. switch (value) {
  181. case (FPW) INTEL_ID_28F128J3A:
  182. info->flash_id += FLASH_28F128J3A;
  183. info->sector_count = 128;
  184. info->size = 0x02000000;
  185. break; /* => 32 MB */
  186. case (FPW) INTEL_ID_28F640J3A:
  187. info->flash_id += FLASH_28F640J3A;
  188. info->sector_count = 64;
  189. info->size = 0x01000000;
  190. break; /* => 16 MB */
  191. case (FPW) INTEL_ID_28F320J3A:
  192. info->flash_id += FLASH_28F320J3A;
  193. info->sector_count = 32;
  194. info->size = 0x00800000;
  195. break; /* => 8 MB */
  196. default:
  197. info->flash_id = FLASH_UNKNOWN;
  198. break;
  199. }
  200. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  201. printf ("** ERROR: sector count %d > max (%d) **\n",
  202. info->sector_count, CFG_MAX_FLASH_SECT);
  203. info->sector_count = CFG_MAX_FLASH_SECT;
  204. }
  205. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  206. return (info->size);
  207. }
  208. /*-----------------------------------------------------------------------
  209. */
  210. int flash_erase (flash_info_t *info, int s_first, int s_last)
  211. {
  212. int flag, prot, sect;
  213. ulong type, start, last;
  214. int rcode = 0;
  215. if ((s_first < 0) || (s_first > s_last)) {
  216. if (info->flash_id == FLASH_UNKNOWN) {
  217. printf ("- missing\n");
  218. } else {
  219. printf ("- no sectors to erase\n");
  220. }
  221. return 1;
  222. }
  223. type = (info->flash_id & FLASH_VENDMASK);
  224. if ((type != FLASH_MAN_INTEL)) {
  225. printf ("Can't erase unknown flash type %08lx - aborted\n",
  226. info->flash_id);
  227. return 1;
  228. }
  229. prot = 0;
  230. for (sect = s_first; sect <= s_last; ++sect) {
  231. if (info->protect[sect]) {
  232. prot++;
  233. }
  234. }
  235. if (prot) {
  236. printf ("- Warning: %d protected sectors will not be erased!\n",
  237. prot);
  238. } else {
  239. printf ("\n");
  240. }
  241. start = get_timer (0);
  242. last = start;
  243. /* Disable interrupts which might cause a timeout here */
  244. flag = disable_interrupts ();
  245. /* Start erase on unprotected sectors */
  246. for (sect = s_first; sect <= s_last; sect++) {
  247. if (info->protect[sect] == 0) { /* not protected */
  248. FPWV *addr = (FPWV *) (info->start[sect]);
  249. FPW status;
  250. printf ("Erasing sector %2d ... ", sect);
  251. /* arm simple, non interrupt dependent timer */
  252. start = get_timer(0);
  253. *addr = (FPW) 0x00500050; /* clear status register */
  254. *addr = (FPW) 0x00200020; /* erase setup */
  255. *addr = (FPW) 0x00D000D0; /* erase confirm */
  256. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  257. if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
  258. printf ("Timeout\n");
  259. *addr = (FPW) 0x00B000B0; /* suspend erase */
  260. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  261. rcode = 1;
  262. break;
  263. }
  264. }
  265. *addr = 0x00500050; /* clear status register cmd. */
  266. *addr = 0x00FF00FF; /* resest to read mode */
  267. printf (" done\n");
  268. }
  269. }
  270. return rcode;
  271. }
  272. /*-----------------------------------------------------------------------
  273. * Copy memory to flash, returns:
  274. * 0 - OK
  275. * 1 - write timeout
  276. * 2 - Flash not erased
  277. * 4 - Flash not identified
  278. */
  279. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  280. {
  281. ulong cp, wp;
  282. FPW data;
  283. int count, i, l, rc, port_width;
  284. if (info->flash_id == FLASH_UNKNOWN) {
  285. return 4;
  286. }
  287. /* get lower word aligned address */
  288. #ifdef FLASH_PORT_WIDTH16
  289. wp = (addr & ~1);
  290. port_width = 2;
  291. #else
  292. wp = (addr & ~3);
  293. port_width = 4;
  294. #endif
  295. /*
  296. * handle unaligned start bytes
  297. */
  298. if ((l = addr - wp) != 0) {
  299. data = 0;
  300. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  301. data = (data << 8) | (*(uchar *) cp);
  302. }
  303. for (; i < port_width && cnt > 0; ++i) {
  304. data = (data << 8) | *src++;
  305. --cnt;
  306. ++cp;
  307. }
  308. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  309. data = (data << 8) | (*(uchar *) cp);
  310. }
  311. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  312. return (rc);
  313. }
  314. wp += port_width;
  315. }
  316. /*
  317. * handle word aligned part
  318. */
  319. count = 0;
  320. while (cnt >= port_width) {
  321. data = 0;
  322. for (i = 0; i < port_width; ++i) {
  323. data = (data << 8) | *src++;
  324. }
  325. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  326. return (rc);
  327. }
  328. wp += port_width;
  329. cnt -= port_width;
  330. if (count++ > 0x800) {
  331. spin_wheel ();
  332. count = 0;
  333. }
  334. }
  335. if (cnt == 0) {
  336. return (0);
  337. }
  338. /*
  339. * handle unaligned tail bytes
  340. */
  341. data = 0;
  342. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  343. data = (data << 8) | *src++;
  344. --cnt;
  345. }
  346. for (; i < port_width; ++i, ++cp) {
  347. data = (data << 8) | (*(uchar *) cp);
  348. }
  349. return (write_data (info, wp, SWAP (data)));
  350. }
  351. /*-----------------------------------------------------------------------
  352. * Write a word or halfword to Flash, returns:
  353. * 0 - OK
  354. * 1 - write timeout
  355. * 2 - Flash not erased
  356. */
  357. static int write_data (flash_info_t *info, ulong dest, FPW data)
  358. {
  359. FPWV *addr = (FPWV *) dest;
  360. ulong status;
  361. ulong start;
  362. int flag;
  363. /* Check if Flash is (sufficiently) erased */
  364. if ((*addr & data) != data) {
  365. printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
  366. return (2);
  367. }
  368. /* Disable interrupts which might cause a timeout here */
  369. flag = disable_interrupts ();
  370. *addr = (FPW) 0x00400040; /* write setup */
  371. *addr = data;
  372. /* arm simple, non interrupt dependent timer */
  373. start = get_timer(0);
  374. /* wait while polling the status register */
  375. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  376. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  377. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  378. return (1);
  379. }
  380. }
  381. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  382. return (0);
  383. }
  384. void inline spin_wheel (void)
  385. {
  386. static int p = 0;
  387. static char w[] = "\\/-";
  388. printf ("\010%c", w[p]);
  389. (++p == 3) ? (p = 0) : 0;
  390. }
  391. /*-----------------------------------------------------------------------
  392. * Set/Clear sector's lock bit, returns:
  393. * 0 - OK
  394. * 1 - Error (timeout, voltage problems, etc.)
  395. */
  396. int flash_real_protect(flash_info_t *info, long sector, int prot)
  397. {
  398. ulong start;
  399. int i;
  400. int rc = 0;
  401. vu_long *addr = (vu_long *)(info->start[sector]);
  402. int flag = disable_interrupts();
  403. *addr = INTEL_CLEAR; /* Clear status register */
  404. if (prot) { /* Set sector lock bit */
  405. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  406. *addr = INTEL_PROTECT; /* set */
  407. }
  408. else { /* Clear sector lock bit */
  409. *addr = INTEL_LOCKBIT; /* All sectors lock bits */
  410. *addr = INTEL_CONFIRM; /* clear */
  411. }
  412. start = get_timer(0);
  413. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  414. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
  415. printf("Flash lock bit operation timed out\n");
  416. rc = 1;
  417. break;
  418. }
  419. }
  420. if (*addr != INTEL_OK) {
  421. printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
  422. (uint)addr, (uint)*addr);
  423. rc = 1;
  424. }
  425. if (!rc)
  426. info->protect[sector] = prot;
  427. /*
  428. * Clear lock bit command clears all sectors lock bits, so
  429. * we have to restore lock bits of protected sectors.
  430. */
  431. if (!prot)
  432. {
  433. for (i = 0; i < info->sector_count; i++)
  434. {
  435. if (info->protect[i])
  436. {
  437. start = get_timer(0);
  438. addr = (vu_long *)(info->start[i]);
  439. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  440. *addr = INTEL_PROTECT; /* set */
  441. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED)
  442. {
  443. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT)
  444. {
  445. printf("Flash lock bit operation timed out\n");
  446. rc = 1;
  447. break;
  448. }
  449. }
  450. }
  451. }
  452. }
  453. if (flag)
  454. enable_interrupts();
  455. *addr = INTEL_RESET; /* Reset to read array mode */
  456. return rc;
  457. }