flash.c 11 KB

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