flash.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * (C) Copyright 2005
  3. * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
  4. *
  5. * (C) Copyright 2001
  6. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  7. *
  8. * (C) Copyright 2001
  9. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <linux/byteorder/swab.h>
  31. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  32. #define mb() __asm__ __volatile__ ("" : : : "memory")
  33. /*-----------------------------------------------------------------------
  34. * Functions
  35. */
  36. static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
  37. static int write_data (flash_info_t * info, ulong dest, unsigned char data);
  38. static void flash_get_offsets (ulong base, flash_info_t * info);
  39. void inline spin_wheel (void);
  40. /*-----------------------------------------------------------------------
  41. */
  42. unsigned long flash_init (void)
  43. {
  44. int i;
  45. ulong size = 0;
  46. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  47. switch (i) {
  48. case 0:
  49. flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
  50. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  51. break;
  52. case 1:
  53. /* ignore for now */
  54. flash_info[i].flash_id = FLASH_UNKNOWN;
  55. break;
  56. default:
  57. panic ("configured too many flash banks!\n");
  58. break;
  59. }
  60. size += flash_info[i].size;
  61. }
  62. /* Protect monitor and environment sectors
  63. */
  64. flash_protect (FLAG_PROTECT_SET,
  65. CONFIG_SYS_FLASH_BASE,
  66. CONFIG_SYS_FLASH_BASE + _bss_start_ofs,
  67. &flash_info[0]);
  68. return size;
  69. }
  70. /*-----------------------------------------------------------------------
  71. */
  72. static void flash_get_offsets (ulong base, flash_info_t * info)
  73. {
  74. int i;
  75. if (info->flash_id == FLASH_UNKNOWN)
  76. return;
  77. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  78. for (i = 0; i < info->sector_count; i++) {
  79. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  80. info->protect[i] = 0;
  81. }
  82. }
  83. }
  84. /*-----------------------------------------------------------------------
  85. */
  86. void flash_print_info (flash_info_t * info)
  87. {
  88. int i;
  89. if (info->flash_id == FLASH_UNKNOWN) {
  90. printf ("missing or unknown FLASH type\n");
  91. return;
  92. }
  93. switch (info->flash_id & FLASH_VENDMASK) {
  94. case FLASH_MAN_INTEL:
  95. printf ("INTEL ");
  96. break;
  97. default:
  98. printf ("Unknown Vendor ");
  99. break;
  100. }
  101. switch (info->flash_id & FLASH_TYPEMASK) {
  102. case FLASH_28F128J3A:
  103. printf ("28F128J3A\n");
  104. break;
  105. default:
  106. printf ("Unknown Chip Type\n");
  107. break;
  108. }
  109. printf (" Size: %ld MB in %d Sectors\n",
  110. info->size >> 20, info->sector_count);
  111. printf (" Sector Start Addresses:");
  112. for (i = 0; i < info->sector_count; ++i) {
  113. if ((i % 5) == 0)
  114. printf ("\n ");
  115. printf (" %08lX%s",
  116. info->start[i], info->protect[i] ? " (RO)" : " ");
  117. }
  118. printf ("\n");
  119. return;
  120. }
  121. /*
  122. * The following code cannot be run from FLASH!
  123. */
  124. static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
  125. {
  126. volatile unsigned char value;
  127. /* Write auto select command: read Manufacturer ID */
  128. addr[0x5555] = 0xAA;
  129. addr[0x2AAA] = 0x55;
  130. addr[0x5555] = 0x90;
  131. mb ();
  132. value = addr[0];
  133. switch (value) {
  134. case (unsigned char)INTEL_MANUFACT:
  135. info->flash_id = FLASH_MAN_INTEL;
  136. break;
  137. default:
  138. info->flash_id = FLASH_UNKNOWN;
  139. info->sector_count = 0;
  140. info->size = 0;
  141. addr[0] = 0xFF; /* restore read mode */
  142. return (0); /* no or unknown flash */
  143. }
  144. mb ();
  145. value = addr[2]; /* device ID */
  146. switch (value) {
  147. case (unsigned char)INTEL_ID_28F640J3A:
  148. info->flash_id += FLASH_28F640J3A;
  149. info->sector_count = 64;
  150. info->size = 0x00800000;
  151. break; /* => 8 MB */
  152. case (unsigned char)INTEL_ID_28F128J3A:
  153. info->flash_id += FLASH_28F128J3A;
  154. info->sector_count = 128;
  155. info->size = 0x01000000;
  156. break; /* => 16 MB */
  157. default:
  158. info->flash_id = FLASH_UNKNOWN;
  159. break;
  160. }
  161. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  162. printf ("** ERROR: sector count %d > max (%d) **\n",
  163. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  164. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  165. }
  166. addr[0] = 0xFF; /* restore read mode */
  167. return (info->size);
  168. }
  169. /*-----------------------------------------------------------------------
  170. */
  171. int flash_erase (flash_info_t * info, int s_first, int s_last)
  172. {
  173. int flag, prot, sect;
  174. ulong type;
  175. int rcode = 0;
  176. ulong start;
  177. if ((s_first < 0) || (s_first > s_last)) {
  178. if (info->flash_id == FLASH_UNKNOWN) {
  179. printf ("- missing\n");
  180. } else {
  181. printf ("- no sectors to erase\n");
  182. }
  183. return 1;
  184. }
  185. type = (info->flash_id & FLASH_VENDMASK);
  186. if ((type != FLASH_MAN_INTEL)) {
  187. printf ("Can't erase unknown flash type %08lx - aborted\n",
  188. info->flash_id);
  189. return 1;
  190. }
  191. prot = 0;
  192. for (sect = s_first; sect <= s_last; ++sect) {
  193. if (info->protect[sect]) {
  194. prot++;
  195. }
  196. }
  197. if (prot)
  198. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  199. else
  200. printf ("\n");
  201. /* Disable interrupts which might cause a timeout here */
  202. flag = disable_interrupts ();
  203. /* Start erase on unprotected sectors */
  204. for (sect = s_first; sect <= s_last; sect++) {
  205. if (info->protect[sect] == 0) { /* not protected */
  206. volatile unsigned char *addr;
  207. unsigned char status;
  208. printf ("Erasing sector %2d ... ", sect);
  209. /* arm simple, non interrupt dependent timer */
  210. start = get_timer(0);
  211. addr = (volatile unsigned char *) (info->start[sect]);
  212. *addr = 0x50; /* clear status register */
  213. *addr = 0x20; /* erase setup */
  214. *addr = 0xD0; /* erase confirm */
  215. while (((status = *addr) & 0x80) != 0x80) {
  216. if (get_timer(start) >
  217. CONFIG_SYS_FLASH_ERASE_TOUT) {
  218. printf ("Timeout\n");
  219. *addr = 0xB0; /* suspend erase */
  220. *addr = 0xFF; /* reset to read mode */
  221. rcode = 1;
  222. break;
  223. }
  224. }
  225. *addr = 0x50; /* clear status register cmd */
  226. *addr = 0xFF; /* resest to read mode */
  227. printf (" done\n");
  228. }
  229. }
  230. return rcode;
  231. }
  232. /*-----------------------------------------------------------------------
  233. * Copy memory to flash, returns:
  234. * 0 - OK
  235. * 1 - write timeout
  236. * 2 - Flash not erased
  237. * 4 - Flash not identified
  238. */
  239. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  240. {
  241. ulong cp, wp;
  242. unsigned char data;
  243. int count, i, l, rc, port_width;
  244. if (info->flash_id == FLASH_UNKNOWN)
  245. return 4;
  246. wp = addr;
  247. port_width = 1;
  248. /*
  249. * handle unaligned start bytes
  250. */
  251. if ((l = addr - wp) != 0) {
  252. data = 0;
  253. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  254. data = (data << 8) | (*(uchar *) cp);
  255. }
  256. for (; i < port_width && cnt > 0; ++i) {
  257. data = (data << 8) | *src++;
  258. --cnt;
  259. ++cp;
  260. }
  261. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  262. data = (data << 8) | (*(uchar *) cp);
  263. }
  264. if ((rc = write_data (info, wp, data)) != 0) {
  265. return (rc);
  266. }
  267. wp += port_width;
  268. }
  269. /*
  270. * handle word aligned part
  271. */
  272. count = 0;
  273. while (cnt >= port_width) {
  274. data = 0;
  275. for (i = 0; i < port_width; ++i) {
  276. data = (data << 8) | *src++;
  277. }
  278. if ((rc = write_data (info, wp, data)) != 0) {
  279. return (rc);
  280. }
  281. wp += port_width;
  282. cnt -= port_width;
  283. if (count++ > 0x800) {
  284. spin_wheel ();
  285. count = 0;
  286. }
  287. }
  288. if (cnt == 0) {
  289. return (0);
  290. }
  291. /*
  292. * handle unaligned tail bytes
  293. */
  294. data = 0;
  295. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  296. data = (data << 8) | *src++;
  297. --cnt;
  298. }
  299. for (; i < port_width; ++i, ++cp) {
  300. data = (data << 8) | (*(uchar *) cp);
  301. }
  302. return (write_data (info, wp, data));
  303. }
  304. /*-----------------------------------------------------------------------
  305. * Write a word or halfword to Flash, returns:
  306. * 0 - OK
  307. * 1 - write timeout
  308. * 2 - Flash not erased
  309. */
  310. static int write_data (flash_info_t * info, ulong dest, unsigned char data)
  311. {
  312. volatile unsigned char *addr = (volatile unsigned char *) dest;
  313. ulong status;
  314. int flag;
  315. ulong start;
  316. /* Check if Flash is (sufficiently) erased */
  317. if ((*addr & data) != data) {
  318. printf ("not erased at %08lx (%lx)\n", (ulong) addr,
  319. (ulong) * addr);
  320. return (2);
  321. }
  322. /* Disable interrupts which might cause a timeout here */
  323. flag = disable_interrupts ();
  324. *addr = 0x40; /* write setup */
  325. *addr = data;
  326. /* arm simple, non interrupt dependent timer */
  327. start = get_timer(0);
  328. /* wait while polling the status register */
  329. while (((status = *addr) & 0x80) != 0x80) {
  330. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  331. *addr = 0xFF; /* restore read mode */
  332. return (1);
  333. }
  334. }
  335. *addr = 0xFF; /* restore read mode */
  336. return (0);
  337. }
  338. void inline spin_wheel (void)
  339. {
  340. static int p = 0;
  341. static char w[] = "\\/-";
  342. printf ("\010%c", w[p]);
  343. (++p == 3) ? (p = 0) : 0;
  344. }