flash.c 9.9 KB

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