flash.c 8.4 KB

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