flash.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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[CFG_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 < CFG_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. CFG_FLASH_BASE,
  66. CFG_FLASH_BASE + _bss_start - _armboot_start,
  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 > CFG_MAX_FLASH_SECT) {
  162. printf ("** ERROR: sector count %d > max (%d) **\n",
  163. info->sector_count, CFG_MAX_FLASH_SECT);
  164. info->sector_count = CFG_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. if ((s_first < 0) || (s_first > s_last)) {
  177. if (info->flash_id == FLASH_UNKNOWN) {
  178. printf ("- missing\n");
  179. } else {
  180. printf ("- no sectors to erase\n");
  181. }
  182. return 1;
  183. }
  184. type = (info->flash_id & FLASH_VENDMASK);
  185. if ((type != FLASH_MAN_INTEL)) {
  186. printf ("Can't erase unknown flash type %08lx - aborted\n",
  187. info->flash_id);
  188. return 1;
  189. }
  190. prot = 0;
  191. for (sect = s_first; sect <= s_last; ++sect) {
  192. if (info->protect[sect]) {
  193. prot++;
  194. }
  195. }
  196. if (prot)
  197. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  198. else
  199. printf ("\n");
  200. /* Disable interrupts which might cause a timeout here */
  201. flag = disable_interrupts ();
  202. /* Start erase on unprotected sectors */
  203. for (sect = s_first; sect <= s_last; sect++) {
  204. if (info->protect[sect] == 0) { /* not protected */
  205. volatile unsigned char *addr;
  206. unsigned char status;
  207. printf ("Erasing sector %2d ... ", sect);
  208. /* arm simple, non interrupt dependent timer */
  209. reset_timer_masked ();
  210. addr = (volatile unsigned char *) (info->start[sect]);
  211. *addr = 0x50; /* clear status register */
  212. *addr = 0x20; /* erase setup */
  213. *addr = 0xD0; /* erase confirm */
  214. while (((status = *addr) & 0x80) != 0x80) {
  215. if (get_timer_masked () >
  216. CFG_FLASH_ERASE_TOUT) {
  217. printf ("Timeout\n");
  218. *addr = 0xB0; /* suspend erase */
  219. *addr = 0xFF; /* reset to read mode */
  220. rcode = 1;
  221. break;
  222. }
  223. }
  224. *addr = 0x50; /* clear status register cmd */
  225. *addr = 0xFF; /* resest to read mode */
  226. printf (" done\n");
  227. }
  228. }
  229. return rcode;
  230. }
  231. /*-----------------------------------------------------------------------
  232. * Copy memory to flash, returns:
  233. * 0 - OK
  234. * 1 - write timeout
  235. * 2 - Flash not erased
  236. * 4 - Flash not identified
  237. */
  238. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  239. {
  240. ulong cp, wp;
  241. unsigned char data;
  242. int count, i, l, rc, port_width;
  243. if (info->flash_id == FLASH_UNKNOWN)
  244. return 4;
  245. wp = addr;
  246. port_width = 1;
  247. /*
  248. * handle unaligned start bytes
  249. */
  250. if ((l = addr - wp) != 0) {
  251. data = 0;
  252. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  253. data = (data << 8) | (*(uchar *) cp);
  254. }
  255. for (; i < port_width && cnt > 0; ++i) {
  256. data = (data << 8) | *src++;
  257. --cnt;
  258. ++cp;
  259. }
  260. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  261. data = (data << 8) | (*(uchar *) cp);
  262. }
  263. if ((rc = write_data (info, wp, data)) != 0) {
  264. return (rc);
  265. }
  266. wp += port_width;
  267. }
  268. /*
  269. * handle word aligned part
  270. */
  271. count = 0;
  272. while (cnt >= port_width) {
  273. data = 0;
  274. for (i = 0; i < port_width; ++i) {
  275. data = (data << 8) | *src++;
  276. }
  277. if ((rc = write_data (info, wp, data)) != 0) {
  278. return (rc);
  279. }
  280. wp += port_width;
  281. cnt -= port_width;
  282. if (count++ > 0x800) {
  283. spin_wheel ();
  284. count = 0;
  285. }
  286. }
  287. if (cnt == 0) {
  288. return (0);
  289. }
  290. /*
  291. * handle unaligned tail bytes
  292. */
  293. data = 0;
  294. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  295. data = (data << 8) | *src++;
  296. --cnt;
  297. }
  298. for (; i < port_width; ++i, ++cp) {
  299. data = (data << 8) | (*(uchar *) cp);
  300. }
  301. return (write_data (info, wp, data));
  302. }
  303. /*-----------------------------------------------------------------------
  304. * Write a word or halfword to Flash, returns:
  305. * 0 - OK
  306. * 1 - write timeout
  307. * 2 - Flash not erased
  308. */
  309. static int write_data (flash_info_t * info, ulong dest, unsigned char data)
  310. {
  311. volatile unsigned char *addr = (volatile unsigned char *) dest;
  312. ulong status;
  313. int flag;
  314. /* Check if Flash is (sufficiently) erased */
  315. if ((*addr & data) != data) {
  316. printf ("not erased at %08lx (%lx)\n", (ulong) addr,
  317. (ulong) * addr);
  318. return (2);
  319. }
  320. /* Disable interrupts which might cause a timeout here */
  321. flag = disable_interrupts ();
  322. *addr = 0x40; /* write setup */
  323. *addr = data;
  324. /* arm simple, non interrupt dependent timer */
  325. reset_timer_masked ();
  326. /* wait while polling the status register */
  327. while (((status = *addr) & 0x80) != 0x80) {
  328. if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
  329. *addr = 0xFF; /* restore read mode */
  330. return (1);
  331. }
  332. }
  333. *addr = 0xFF; /* restore read mode */
  334. return (0);
  335. }
  336. void inline spin_wheel (void)
  337. {
  338. static int p = 0;
  339. static char w[] = "\\/-";
  340. printf ("\010%c", w[p]);
  341. (++p == 3) ? (p = 0) : 0;
  342. }