flash.c 9.6 KB

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