flash.c 9.8 KB

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