flash.c 9.7 KB

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