flash.c 9.7 KB

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