flash.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #if 0
  56. int i;
  57. ulong size = 0;
  58. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  59. switch (i) {
  60. case 0:
  61. flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
  62. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  63. break;
  64. case 1:
  65. flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
  66. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  67. break;
  68. default:
  69. panic ("configured too many flash banks!\n");
  70. break;
  71. }
  72. size += flash_info[i].size;
  73. }
  74. /* Protect monitor and environment sectors
  75. */
  76. flash_protect ( FLAG_PROTECT_SET,
  77. CONFIG_SYS_FLASH_BASE,
  78. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  79. &flash_info[0] );
  80. flash_protect ( FLAG_PROTECT_SET,
  81. CONFIG_ENV_ADDR,
  82. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
  83. return size;
  84. #endif
  85. return 0;
  86. }
  87. /*-----------------------------------------------------------------------
  88. */
  89. static void flash_get_offsets (ulong base, flash_info_t *info)
  90. {
  91. int i;
  92. if (info->flash_id == FLASH_UNKNOWN) {
  93. return;
  94. }
  95. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  96. for (i = 0; i < info->sector_count; i++) {
  97. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  98. info->protect[i] = 0;
  99. }
  100. }
  101. }
  102. /*-----------------------------------------------------------------------
  103. */
  104. void flash_print_info (flash_info_t *info)
  105. {
  106. int i;
  107. if (info->flash_id == FLASH_UNKNOWN) {
  108. printf ("missing or unknown FLASH type\n");
  109. return;
  110. }
  111. switch (info->flash_id & FLASH_VENDMASK) {
  112. case FLASH_MAN_INTEL:
  113. printf ("INTEL ");
  114. break;
  115. default:
  116. printf ("Unknown Vendor ");
  117. break;
  118. }
  119. switch (info->flash_id & FLASH_TYPEMASK) {
  120. case FLASH_28F128J3A:
  121. printf ("28F128J3A\n");
  122. break;
  123. default:
  124. printf ("Unknown Chip Type\n");
  125. break;
  126. }
  127. printf (" Size: %ld MB in %d Sectors\n",
  128. info->size >> 20, info->sector_count);
  129. printf (" Sector Start Addresses:");
  130. for (i = 0; i < info->sector_count; ++i) {
  131. if ((i % 5) == 0)
  132. printf ("\n ");
  133. printf (" %08lX%s",
  134. info->start[i],
  135. info->protect[i] ? " (RO)" : " ");
  136. }
  137. printf ("\n");
  138. return;
  139. }
  140. /*
  141. * The following code cannot be run from FLASH!
  142. */
  143. static ulong flash_get_size (FPW *addr, flash_info_t *info)
  144. {
  145. volatile FPW value;
  146. /* Write auto select command: read Manufacturer ID */
  147. addr[0x5555] = (FPW) 0x00AA00AA;
  148. addr[0x2AAA] = (FPW) 0x00550055;
  149. addr[0x5555] = (FPW) 0x00900090;
  150. mb ();
  151. value = addr[0];
  152. switch (value) {
  153. case (FPW) INTEL_MANUFACT:
  154. info->flash_id = FLASH_MAN_INTEL;
  155. break;
  156. default:
  157. info->flash_id = FLASH_UNKNOWN;
  158. info->sector_count = 0;
  159. info->size = 0;
  160. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  161. return (0); /* no or unknown flash */
  162. }
  163. mb ();
  164. value = addr[1]; /* device ID */
  165. switch (value) {
  166. case (FPW) INTEL_ID_28F128J3A:
  167. info->flash_id += FLASH_28F128J3A;
  168. info->sector_count = 128;
  169. info->size = 0x02000000;
  170. break; /* => 16 MB */
  171. default:
  172. info->flash_id = FLASH_UNKNOWN;
  173. break;
  174. }
  175. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  176. printf ("** ERROR: sector count %d > max (%d) **\n",
  177. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  178. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  179. }
  180. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  181. return (info->size);
  182. }
  183. /*-----------------------------------------------------------------------
  184. */
  185. int flash_erase (flash_info_t *info, int s_first, int s_last)
  186. {
  187. int flag, prot, sect;
  188. ulong type, start;
  189. int rcode = 0;
  190. if ((s_first < 0) || (s_first > s_last)) {
  191. if (info->flash_id == FLASH_UNKNOWN) {
  192. printf ("- missing\n");
  193. } else {
  194. printf ("- no sectors to erase\n");
  195. }
  196. return 1;
  197. }
  198. type = (info->flash_id & FLASH_VENDMASK);
  199. if ((type != FLASH_MAN_INTEL)) {
  200. printf ("Can't erase unknown flash type %08lx - aborted\n",
  201. info->flash_id);
  202. return 1;
  203. }
  204. prot = 0;
  205. for (sect = s_first; sect <= s_last; ++sect) {
  206. if (info->protect[sect]) {
  207. prot++;
  208. }
  209. }
  210. if (prot) {
  211. printf ("- Warning: %d protected sectors will not be erased!\n",
  212. prot);
  213. } else {
  214. printf ("\n");
  215. }
  216. /* Disable interrupts which might cause a timeout here */
  217. flag = disable_interrupts ();
  218. /* Start erase on unprotected sectors */
  219. for (sect = s_first; sect <= s_last; sect++) {
  220. if (info->protect[sect] == 0) { /* not protected */
  221. FPWV *addr = (FPWV *) (info->start[sect]);
  222. FPW status;
  223. printf ("Erasing sector %2d ... ", sect);
  224. /* arm simple, non interrupt dependent timer */
  225. start = get_timer(0);
  226. *addr = (FPW) 0x00500050; /* clear status register */
  227. *addr = (FPW) 0x00200020; /* erase setup */
  228. *addr = (FPW) 0x00D000D0; /* erase confirm */
  229. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  230. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  231. printf ("Timeout\n");
  232. *addr = (FPW) 0x00B000B0; /* suspend erase */
  233. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  234. rcode = 1;
  235. break;
  236. }
  237. }
  238. *addr = 0x00500050; /* clear status register cmd. */
  239. *addr = 0x00FF00FF; /* resest to read mode */
  240. printf (" done\n");
  241. }
  242. }
  243. return rcode;
  244. }
  245. /*-----------------------------------------------------------------------
  246. * Copy memory to flash, returns:
  247. * 0 - OK
  248. * 1 - write timeout
  249. * 2 - Flash not erased
  250. * 4 - Flash not identified
  251. */
  252. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  253. {
  254. ulong cp, wp;
  255. FPW data;
  256. int count, i, l, rc, port_width;
  257. if (info->flash_id == FLASH_UNKNOWN) {
  258. return 4;
  259. }
  260. /* get lower word aligned address */
  261. #ifdef FLASH_PORT_WIDTH16
  262. wp = (addr & ~1);
  263. port_width = 2;
  264. #else
  265. wp = (addr & ~3);
  266. port_width = 4;
  267. #endif
  268. /*
  269. * handle unaligned start bytes
  270. */
  271. if ((l = addr - wp) != 0) {
  272. data = 0;
  273. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  274. data = (data << 8) | (*(uchar *) cp);
  275. }
  276. for (; i < port_width && cnt > 0; ++i) {
  277. data = (data << 8) | *src++;
  278. --cnt;
  279. ++cp;
  280. }
  281. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  282. data = (data << 8) | (*(uchar *) cp);
  283. }
  284. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  285. return (rc);
  286. }
  287. wp += port_width;
  288. }
  289. /*
  290. * handle word aligned part
  291. */
  292. count = 0;
  293. while (cnt >= port_width) {
  294. data = 0;
  295. for (i = 0; i < port_width; ++i) {
  296. data = (data << 8) | *src++;
  297. }
  298. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  299. return (rc);
  300. }
  301. wp += port_width;
  302. cnt -= port_width;
  303. if (count++ > 0x800) {
  304. spin_wheel ();
  305. count = 0;
  306. }
  307. }
  308. if (cnt == 0) {
  309. return (0);
  310. }
  311. /*
  312. * handle unaligned tail bytes
  313. */
  314. data = 0;
  315. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  316. data = (data << 8) | *src++;
  317. --cnt;
  318. }
  319. for (; i < port_width; ++i, ++cp) {
  320. data = (data << 8) | (*(uchar *) cp);
  321. }
  322. return (write_data (info, wp, SWAP (data)));
  323. }
  324. /*-----------------------------------------------------------------------
  325. * Write a word or halfword to Flash, returns:
  326. * 0 - OK
  327. * 1 - write timeout
  328. * 2 - Flash not erased
  329. */
  330. static int write_data (flash_info_t *info, ulong dest, FPW data)
  331. {
  332. FPWV *addr = (FPWV *) dest;
  333. ulong status;
  334. int flag;
  335. ulong start;
  336. /* Check if Flash is (sufficiently) erased */
  337. if ((*addr & data) != data) {
  338. printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
  339. return (2);
  340. }
  341. /* Disable interrupts which might cause a timeout here */
  342. flag = disable_interrupts ();
  343. *addr = (FPW) 0x00400040; /* write setup */
  344. *addr = data;
  345. /* arm simple, non interrupt dependent timer */
  346. start = get_timer(0);
  347. /* wait while polling the status register */
  348. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  349. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  350. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  351. return (1);
  352. }
  353. }
  354. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  355. return (0);
  356. }
  357. void inline spin_wheel (void)
  358. {
  359. static int p = 0;
  360. static char w[] = "\\/-";
  361. printf ("\010%c", w[p]);
  362. (++p == 3) ? (p = 0) : 0;
  363. }