flash.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <linux/byteorder/swab.h>
  12. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  13. /* Board support for 1 or 2 flash devices */
  14. #define FLASH_PORT_WIDTH32
  15. #undef FLASH_PORT_WIDTH16
  16. #ifdef FLASH_PORT_WIDTH16
  17. #define FLASH_PORT_WIDTH ushort
  18. #define FLASH_PORT_WIDTHV vu_short
  19. #define SWAP(x) __swab16(x)
  20. #else
  21. #define FLASH_PORT_WIDTH ulong
  22. #define FLASH_PORT_WIDTHV vu_long
  23. #define SWAP(x) __swab32(x)
  24. #endif
  25. #define FPW FLASH_PORT_WIDTH
  26. #define FPWV FLASH_PORT_WIDTHV
  27. #define mb() __asm__ __volatile__ ("" : : : "memory")
  28. /*-----------------------------------------------------------------------
  29. * Functions
  30. */
  31. static ulong flash_get_size (FPW *addr, flash_info_t *info);
  32. static int write_data (flash_info_t *info, ulong dest, FPW data);
  33. static void flash_get_offsets (ulong base, flash_info_t *info);
  34. void inline spin_wheel (void);
  35. /*-----------------------------------------------------------------------
  36. */
  37. unsigned long flash_init (void)
  38. {
  39. int i;
  40. ulong size = 0;
  41. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  42. switch (i) {
  43. case 0:
  44. flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
  45. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  46. break;
  47. case 1:
  48. flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
  49. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  50. break;
  51. default:
  52. panic ("configured too many flash banks!\n");
  53. break;
  54. }
  55. size += flash_info[i].size;
  56. }
  57. /* Protect monitor and environment sectors
  58. */
  59. flash_protect ( FLAG_PROTECT_SET,
  60. CONFIG_SYS_FLASH_BASE,
  61. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  62. &flash_info[0] );
  63. flash_protect ( FLAG_PROTECT_SET,
  64. CONFIG_ENV_ADDR,
  65. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
  66. return size;
  67. }
  68. /*-----------------------------------------------------------------------
  69. */
  70. static void flash_get_offsets (ulong base, flash_info_t *info)
  71. {
  72. int i;
  73. if (info->flash_id == FLASH_UNKNOWN) {
  74. return;
  75. }
  76. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  77. for (i = 0; i < info->sector_count; i++) {
  78. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  79. info->protect[i] = 0;
  80. }
  81. }
  82. }
  83. /*-----------------------------------------------------------------------
  84. */
  85. void flash_print_info (flash_info_t *info)
  86. {
  87. int i;
  88. if (info->flash_id == FLASH_UNKNOWN) {
  89. printf ("missing or unknown FLASH type\n");
  90. return;
  91. }
  92. switch (info->flash_id & FLASH_VENDMASK) {
  93. case FLASH_MAN_INTEL:
  94. printf ("INTEL ");
  95. break;
  96. default:
  97. printf ("Unknown Vendor ");
  98. break;
  99. }
  100. switch (info->flash_id & FLASH_TYPEMASK) {
  101. case FLASH_28F128J3A:
  102. printf ("28F128J3A\n");
  103. break;
  104. default:
  105. printf ("Unknown Chip Type\n");
  106. break;
  107. }
  108. printf (" Size: %ld MB in %d Sectors\n",
  109. info->size >> 20, info->sector_count);
  110. printf (" Sector Start Addresses:");
  111. for (i = 0; i < info->sector_count; ++i) {
  112. if ((i % 5) == 0)
  113. printf ("\n ");
  114. printf (" %08lX%s",
  115. info->start[i],
  116. 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 (FPW *addr, flash_info_t *info)
  125. {
  126. volatile FPW value;
  127. /* Write auto select command: read Manufacturer ID */
  128. addr[0x5555] = (FPW) 0x00AA00AA;
  129. addr[0x2AAA] = (FPW) 0x00550055;
  130. addr[0x5555] = (FPW) 0x00900090;
  131. mb ();
  132. value = addr[0];
  133. switch (value) {
  134. case (FPW) 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] = (FPW) 0x00FF00FF; /* restore read mode */
  142. return (0); /* no or unknown flash */
  143. }
  144. mb ();
  145. value = addr[1]; /* device ID */
  146. switch (value) {
  147. case (FPW) INTEL_ID_28F128J3A:
  148. info->flash_id += FLASH_28F128J3A;
  149. info->sector_count = 128;
  150. info->size = 0x02000000;
  151. break; /* => 16 MB */
  152. default:
  153. info->flash_id = FLASH_UNKNOWN;
  154. break;
  155. }
  156. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  157. printf ("** ERROR: sector count %d > max (%d) **\n",
  158. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  159. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  160. }
  161. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  162. return (info->size);
  163. }
  164. /*-----------------------------------------------------------------------
  165. */
  166. int flash_erase (flash_info_t *info, int s_first, int s_last)
  167. {
  168. int prot, sect;
  169. ulong type, start;
  170. int rcode = 0;
  171. if ((s_first < 0) || (s_first > s_last)) {
  172. if (info->flash_id == FLASH_UNKNOWN) {
  173. printf ("- missing\n");
  174. } else {
  175. printf ("- no sectors to erase\n");
  176. }
  177. return 1;
  178. }
  179. type = (info->flash_id & FLASH_VENDMASK);
  180. if ((type != FLASH_MAN_INTEL)) {
  181. printf ("Can't erase unknown flash type %08lx - aborted\n",
  182. info->flash_id);
  183. return 1;
  184. }
  185. prot = 0;
  186. for (sect = s_first; sect <= s_last; ++sect) {
  187. if (info->protect[sect]) {
  188. prot++;
  189. }
  190. }
  191. if (prot) {
  192. printf ("- Warning: %d protected sectors will not be erased!\n",
  193. prot);
  194. } else {
  195. printf ("\n");
  196. }
  197. /* Disable interrupts which might cause a timeout here */
  198. disable_interrupts();
  199. /* Start erase on unprotected sectors */
  200. for (sect = s_first; sect <= s_last; sect++) {
  201. if (info->protect[sect] == 0) { /* not protected */
  202. FPWV *addr = (FPWV *) (info->start[sect]);
  203. FPW status;
  204. printf ("Erasing sector %2d ... ", sect);
  205. /* arm simple, non interrupt dependent timer */
  206. start = get_timer(0);
  207. *addr = (FPW) 0x00500050; /* clear status register */
  208. *addr = (FPW) 0x00200020; /* erase setup */
  209. *addr = (FPW) 0x00D000D0; /* erase confirm */
  210. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  211. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  212. printf ("Timeout\n");
  213. *addr = (FPW) 0x00B000B0; /* suspend erase */
  214. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  215. rcode = 1;
  216. break;
  217. }
  218. }
  219. *addr = 0x00500050; /* clear status register cmd. */
  220. *addr = 0x00FF00FF; /* resest to read mode */
  221. printf (" done\n");
  222. }
  223. }
  224. return rcode;
  225. }
  226. /*-----------------------------------------------------------------------
  227. * Copy memory to flash, returns:
  228. * 0 - OK
  229. * 1 - write timeout
  230. * 2 - Flash not erased
  231. * 4 - Flash not identified
  232. */
  233. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  234. {
  235. ulong cp, wp;
  236. FPW data;
  237. int count, i, l, rc, port_width;
  238. if (info->flash_id == FLASH_UNKNOWN) {
  239. return 4;
  240. }
  241. /* get lower word aligned address */
  242. #ifdef FLASH_PORT_WIDTH16
  243. wp = (addr & ~1);
  244. port_width = 2;
  245. #else
  246. wp = (addr & ~3);
  247. port_width = 4;
  248. #endif
  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, SWAP (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, SWAP (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, SWAP (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, FPW data)
  312. {
  313. FPWV *addr = (FPWV *) 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, *addr);
  319. return (2);
  320. }
  321. /* Disable interrupts which might cause a timeout here */
  322. disable_interrupts();
  323. *addr = (FPW) 0x00400040; /* write setup */
  324. *addr = data;
  325. /* arm simple, non interrupt dependent timer */
  326. start = get_timer(0);
  327. /* wait while polling the status register */
  328. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  329. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  330. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  331. return (1);
  332. }
  333. }
  334. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  335. return (0);
  336. }
  337. void inline spin_wheel (void)
  338. {
  339. static int p = 0;
  340. static char w[] = "\\/-";
  341. printf ("\010%c", w[p]);
  342. (++p == 3) ? (p = 0) : 0;
  343. }