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. #include <asm/sections.h>
  32. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  33. #define mb() __asm__ __volatile__ ("" : : : "memory")
  34. /*-----------------------------------------------------------------------
  35. * Functions
  36. */
  37. static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
  38. static int write_data (flash_info_t * info, ulong dest, unsigned char data);
  39. static void flash_get_offsets (ulong base, flash_info_t * info);
  40. void inline spin_wheel (void);
  41. /*-----------------------------------------------------------------------
  42. */
  43. unsigned long flash_init (void)
  44. {
  45. int i;
  46. ulong size = 0;
  47. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  48. switch (i) {
  49. case 0:
  50. flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
  51. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  52. break;
  53. case 1:
  54. /* ignore for now */
  55. flash_info[i].flash_id = FLASH_UNKNOWN;
  56. break;
  57. default:
  58. panic ("configured too many flash banks!\n");
  59. break;
  60. }
  61. size += flash_info[i].size;
  62. }
  63. /* Protect monitor and environment sectors
  64. */
  65. flash_protect (FLAG_PROTECT_SET,
  66. CONFIG_SYS_FLASH_BASE,
  67. CONFIG_SYS_FLASH_BASE + _bss_start_ofs,
  68. &flash_info[0]);
  69. return size;
  70. }
  71. /*-----------------------------------------------------------------------
  72. */
  73. static void flash_get_offsets (ulong base, flash_info_t * info)
  74. {
  75. int i;
  76. if (info->flash_id == FLASH_UNKNOWN)
  77. return;
  78. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  79. for (i = 0; i < info->sector_count; i++) {
  80. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  81. info->protect[i] = 0;
  82. }
  83. }
  84. }
  85. /*-----------------------------------------------------------------------
  86. */
  87. void flash_print_info (flash_info_t * info)
  88. {
  89. int i;
  90. if (info->flash_id == FLASH_UNKNOWN) {
  91. printf ("missing or unknown FLASH type\n");
  92. return;
  93. }
  94. switch (info->flash_id & FLASH_VENDMASK) {
  95. case FLASH_MAN_INTEL:
  96. printf ("INTEL ");
  97. break;
  98. default:
  99. printf ("Unknown Vendor ");
  100. break;
  101. }
  102. switch (info->flash_id & FLASH_TYPEMASK) {
  103. case FLASH_28F128J3A:
  104. printf ("28F128J3A\n");
  105. break;
  106. default:
  107. printf ("Unknown Chip Type\n");
  108. break;
  109. }
  110. printf (" Size: %ld MB in %d Sectors\n",
  111. info->size >> 20, info->sector_count);
  112. printf (" Sector Start Addresses:");
  113. for (i = 0; i < info->sector_count; ++i) {
  114. if ((i % 5) == 0)
  115. printf ("\n ");
  116. printf (" %08lX%s",
  117. info->start[i], info->protect[i] ? " (RO)" : " ");
  118. }
  119. printf ("\n");
  120. return;
  121. }
  122. /*
  123. * The following code cannot be run from FLASH!
  124. */
  125. static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
  126. {
  127. volatile unsigned char value;
  128. /* Write auto select command: read Manufacturer ID */
  129. addr[0x5555] = 0xAA;
  130. addr[0x2AAA] = 0x55;
  131. addr[0x5555] = 0x90;
  132. mb ();
  133. value = addr[0];
  134. switch (value) {
  135. case (unsigned char)INTEL_MANUFACT:
  136. info->flash_id = FLASH_MAN_INTEL;
  137. break;
  138. default:
  139. info->flash_id = FLASH_UNKNOWN;
  140. info->sector_count = 0;
  141. info->size = 0;
  142. addr[0] = 0xFF; /* restore read mode */
  143. return (0); /* no or unknown flash */
  144. }
  145. mb ();
  146. value = addr[2]; /* device ID */
  147. switch (value) {
  148. case (unsigned char)INTEL_ID_28F640J3A:
  149. info->flash_id += FLASH_28F640J3A;
  150. info->sector_count = 64;
  151. info->size = 0x00800000;
  152. break; /* => 8 MB */
  153. case (unsigned char)INTEL_ID_28F128J3A:
  154. info->flash_id += FLASH_28F128J3A;
  155. info->sector_count = 128;
  156. info->size = 0x01000000;
  157. break; /* => 16 MB */
  158. default:
  159. info->flash_id = FLASH_UNKNOWN;
  160. break;
  161. }
  162. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  163. printf ("** ERROR: sector count %d > max (%d) **\n",
  164. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  165. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  166. }
  167. addr[0] = 0xFF; /* restore read mode */
  168. return (info->size);
  169. }
  170. /*-----------------------------------------------------------------------
  171. */
  172. int flash_erase (flash_info_t * info, int s_first, int s_last)
  173. {
  174. int prot, sect;
  175. ulong type;
  176. int rcode = 0;
  177. ulong start;
  178. if ((s_first < 0) || (s_first > s_last)) {
  179. if (info->flash_id == FLASH_UNKNOWN) {
  180. printf ("- missing\n");
  181. } else {
  182. printf ("- no sectors to erase\n");
  183. }
  184. return 1;
  185. }
  186. type = (info->flash_id & FLASH_VENDMASK);
  187. if ((type != FLASH_MAN_INTEL)) {
  188. printf ("Can't erase unknown flash type %08lx - aborted\n",
  189. info->flash_id);
  190. return 1;
  191. }
  192. prot = 0;
  193. for (sect = s_first; sect <= s_last; ++sect) {
  194. if (info->protect[sect]) {
  195. prot++;
  196. }
  197. }
  198. if (prot)
  199. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  200. else
  201. printf ("\n");
  202. /* Disable interrupts which might cause a timeout here */
  203. disable_interrupts();
  204. /* Start erase on unprotected sectors */
  205. for (sect = s_first; sect <= s_last; sect++) {
  206. if (info->protect[sect] == 0) { /* not protected */
  207. volatile unsigned char *addr;
  208. unsigned char status;
  209. printf ("Erasing sector %2d ... ", sect);
  210. /* arm simple, non interrupt dependent timer */
  211. start = get_timer(0);
  212. addr = (volatile unsigned char *) (info->start[sect]);
  213. *addr = 0x50; /* clear status register */
  214. *addr = 0x20; /* erase setup */
  215. *addr = 0xD0; /* erase confirm */
  216. while (((status = *addr) & 0x80) != 0x80) {
  217. if (get_timer(start) >
  218. CONFIG_SYS_FLASH_ERASE_TOUT) {
  219. printf ("Timeout\n");
  220. *addr = 0xB0; /* suspend erase */
  221. *addr = 0xFF; /* reset to read mode */
  222. rcode = 1;
  223. break;
  224. }
  225. }
  226. *addr = 0x50; /* clear status register cmd */
  227. *addr = 0xFF; /* resest to read mode */
  228. printf (" done\n");
  229. }
  230. }
  231. return rcode;
  232. }
  233. /*-----------------------------------------------------------------------
  234. * Copy memory to flash, returns:
  235. * 0 - OK
  236. * 1 - write timeout
  237. * 2 - Flash not erased
  238. * 4 - Flash not identified
  239. */
  240. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  241. {
  242. ulong cp, wp;
  243. unsigned char data;
  244. int count, i, l, rc, port_width;
  245. if (info->flash_id == FLASH_UNKNOWN)
  246. return 4;
  247. wp = addr;
  248. port_width = 1;
  249. /*
  250. * handle unaligned start bytes
  251. */
  252. if ((l = addr - wp) != 0) {
  253. data = 0;
  254. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  255. data = (data << 8) | (*(uchar *) cp);
  256. }
  257. for (; i < port_width && cnt > 0; ++i) {
  258. data = (data << 8) | *src++;
  259. --cnt;
  260. ++cp;
  261. }
  262. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  263. data = (data << 8) | (*(uchar *) cp);
  264. }
  265. if ((rc = write_data (info, wp, data)) != 0) {
  266. return (rc);
  267. }
  268. wp += port_width;
  269. }
  270. /*
  271. * handle word aligned part
  272. */
  273. count = 0;
  274. while (cnt >= port_width) {
  275. data = 0;
  276. for (i = 0; i < port_width; ++i) {
  277. data = (data << 8) | *src++;
  278. }
  279. if ((rc = write_data (info, wp, data)) != 0) {
  280. return (rc);
  281. }
  282. wp += port_width;
  283. cnt -= port_width;
  284. if (count++ > 0x800) {
  285. spin_wheel ();
  286. count = 0;
  287. }
  288. }
  289. if (cnt == 0) {
  290. return (0);
  291. }
  292. /*
  293. * handle unaligned tail bytes
  294. */
  295. data = 0;
  296. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  297. data = (data << 8) | *src++;
  298. --cnt;
  299. }
  300. for (; i < port_width; ++i, ++cp) {
  301. data = (data << 8) | (*(uchar *) cp);
  302. }
  303. return (write_data (info, wp, data));
  304. }
  305. /*-----------------------------------------------------------------------
  306. * Write a word or halfword to Flash, returns:
  307. * 0 - OK
  308. * 1 - write timeout
  309. * 2 - Flash not erased
  310. */
  311. static int write_data (flash_info_t * info, ulong dest, unsigned char data)
  312. {
  313. volatile unsigned char *addr = (volatile unsigned char *) dest;
  314. ulong status;
  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. 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. }