flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001-2004
  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. #define FLASH_PORT_WIDTH32
  36. #undef 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_28F256K3[] = {
  56. {256, 128 * 1024}, /* 256 * 128kBytes sectors */
  57. };
  58. /*-----------------------------------------------------------------------
  59. * Functions
  60. */
  61. unsigned long flash_init (void);
  62. static ulong flash_get_size (FPW * addr, flash_info_t * info);
  63. static int write_data (flash_info_t * info, ulong dest, FPW data);
  64. static void flash_get_offsets (ulong base, flash_info_t * info);
  65. void inline spin_wheel (void);
  66. void flash_print_info (flash_info_t * info);
  67. void flash_unprotect_sectors (FPWV * addr);
  68. int flash_erase (flash_info_t * info, int s_first, int s_last);
  69. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
  70. /*-----------------------------------------------------------------------
  71. */
  72. static void flash_vpp(int on)
  73. {
  74. unsigned int tmp;
  75. tmp = *(unsigned int *)(VERSATILE_FLASHCTRL);
  76. if (on)
  77. tmp |= VERSATILE_FLASHPROG_FLVPPEN;
  78. else
  79. tmp &= ~VERSATILE_FLASHPROG_FLVPPEN;
  80. *(unsigned int *)(VERSATILE_FLASHCTRL) = tmp;
  81. }
  82. unsigned long flash_init (void)
  83. {
  84. int i;
  85. ulong size = 0;
  86. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  87. switch (i) {
  88. case 0:
  89. flash_vpp(1);
  90. flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
  91. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  92. flash_vpp(0);
  93. break;
  94. default:
  95. panic ("configured too many flash banks!\n");
  96. break;
  97. }
  98. size += flash_info[i].size;
  99. }
  100. /* Protect monitor and environment sectors
  101. */
  102. flash_protect (FLAG_PROTECT_SET,
  103. CONFIG_SYS_FLASH_BASE,
  104. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
  105. return size;
  106. }
  107. /*-----------------------------------------------------------------------
  108. */
  109. static void flash_get_offsets (ulong base, flash_info_t * info)
  110. {
  111. int i;
  112. OrgDef *pOrgDef;
  113. pOrgDef = OrgIntel_28F256K3;
  114. if (info->flash_id == FLASH_UNKNOWN) {
  115. return;
  116. }
  117. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  118. for (i = 0; i < info->sector_count; i++) {
  119. if (i > 255) {
  120. info->start[i] = base + (i * 0x8000);
  121. info->protect[i] = 0;
  122. } else {
  123. info->start[i] = base +
  124. (i * PHYS_FLASH_SECT_SIZE);
  125. info->protect[i] = 0;
  126. }
  127. }
  128. }
  129. }
  130. /*-----------------------------------------------------------------------
  131. */
  132. void flash_print_info (flash_info_t * info)
  133. {
  134. int i;
  135. if (info->flash_id == FLASH_UNKNOWN) {
  136. printf ("missing or unknown FLASH type\n");
  137. return;
  138. }
  139. switch (info->flash_id & FLASH_VENDMASK) {
  140. case FLASH_MAN_INTEL:
  141. printf ("INTEL ");
  142. break;
  143. default:
  144. printf ("Unknown Vendor ");
  145. break;
  146. }
  147. switch (info->flash_id & FLASH_TYPEMASK) {
  148. case FLASH_28F256L18T:
  149. printf ("FLASH 28F256L18T\n");
  150. break;
  151. case FLASH_28F256K3:
  152. printf ("FLASH 28F256K3\n");
  153. break;
  154. default:
  155. printf ("Unknown Chip Type\n");
  156. break;
  157. }
  158. printf (" Size: %ld MB in %d Sectors\n",
  159. info->size >> 20, info->sector_count);
  160. printf (" Sector Start Addresses:");
  161. for (i = 0; i < info->sector_count; ++i) {
  162. if ((i % 5) == 0)
  163. printf ("\n ");
  164. printf (" %08lX%s",
  165. info->start[i], info->protect[i] ? " (RO)" : " ");
  166. }
  167. printf ("\n");
  168. return;
  169. }
  170. /*
  171. * The following code cannot be run from FLASH!
  172. */
  173. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  174. {
  175. volatile FPW value;
  176. /* Write auto select command: read Manufacturer ID */
  177. addr[0x5555] = (FPW) 0x00AA00AA;
  178. addr[0x2AAA] = (FPW) 0x00550055;
  179. addr[0x5555] = (FPW) 0x00900090;
  180. mb ();
  181. value = addr[0];
  182. switch (value) {
  183. case (FPW) INTEL_MANUFACT:
  184. info->flash_id = FLASH_MAN_INTEL;
  185. break;
  186. default:
  187. info->flash_id = FLASH_UNKNOWN;
  188. info->sector_count = 0;
  189. info->size = 0;
  190. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  191. return (0); /* no or unknown flash */
  192. }
  193. mb ();
  194. value = addr[1]; /* device ID */
  195. switch (value) {
  196. case (FPW) (INTEL_ID_28F256L18T):
  197. info->flash_id += FLASH_28F256L18T;
  198. info->sector_count = 259;
  199. info->size = 0x02000000;
  200. break; /* => 32 MB */
  201. case (FPW)(INTEL_ID_28F256K3):
  202. info->flash_id += FLASH_28F256K3;
  203. info->sector_count = 256;
  204. info->size = 0x02000000;
  205. break;
  206. default:
  207. info->flash_id = FLASH_UNKNOWN;
  208. break;
  209. }
  210. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  211. printf ("** ERROR: sector count %d > max (%d) **\n",
  212. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  213. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  214. }
  215. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  216. return (info->size);
  217. }
  218. /* unprotects a sector for write and erase
  219. * on some intel parts, this unprotects the entire chip, but it
  220. * wont hurt to call this additional times per sector...
  221. */
  222. void flash_unprotect_sectors (FPWV * addr)
  223. {
  224. #define PD_FINTEL_WSMS_READY_MASK 0x0080
  225. *addr = (FPW) 0x00500050; /* clear status register */
  226. /* this sends the clear lock bit command */
  227. *addr = (FPW) 0x00600060;
  228. *addr = (FPW) 0x00D000D0;
  229. }
  230. /*-----------------------------------------------------------------------
  231. */
  232. int flash_erase (flash_info_t * info, int s_first, int s_last)
  233. {
  234. int flag, prot, sect;
  235. ulong type, start, last;
  236. int rcode = 0;
  237. if ((s_first < 0) || (s_first > s_last)) {
  238. if (info->flash_id == FLASH_UNKNOWN) {
  239. printf ("- missing\n");
  240. } else {
  241. printf ("- no sectors to erase\n");
  242. }
  243. return 1;
  244. }
  245. type = (info->flash_id & FLASH_VENDMASK);
  246. if ((type != FLASH_MAN_INTEL)) {
  247. printf ("Can't erase unknown flash type %08lx - aborted\n",
  248. info->flash_id);
  249. return 1;
  250. }
  251. prot = 0;
  252. for (sect = s_first; sect <= s_last; ++sect) {
  253. if (info->protect[sect]) {
  254. prot++;
  255. }
  256. }
  257. if (prot) {
  258. printf ("- Warning: %d protected sectors will not be erased!\n",
  259. prot);
  260. } else {
  261. printf ("\n");
  262. }
  263. flash_vpp(1);
  264. start = get_timer (0);
  265. last = start;
  266. /* Disable interrupts which might cause a timeout here */
  267. flag = disable_interrupts ();
  268. /* Start erase on unprotected sectors */
  269. for (sect = s_first; sect <= s_last; sect++) {
  270. if (info->protect[sect] == 0) { /* not protected */
  271. FPWV *addr = (FPWV *) (info->start[sect]);
  272. FPW status;
  273. printf ("Erasing sector %2d ... ", sect);
  274. flash_unprotect_sectors (addr);
  275. /* arm simple, non interrupt dependent timer */
  276. reset_timer_masked ();
  277. *addr = (FPW) 0x00500050;/* clear status register */
  278. *addr = (FPW) 0x00200020;/* erase setup */
  279. *addr = (FPW) 0x00D000D0;/* erase confirm */
  280. while (((status =
  281. *addr) & (FPW) 0x00800080) !=
  282. (FPW) 0x00800080) {
  283. if (get_timer_masked () >
  284. CONFIG_SYS_FLASH_ERASE_TOUT) {
  285. printf ("Timeout\n");
  286. /* suspend erase */
  287. *addr = (FPW) 0x00B000B0;
  288. /* reset to read mode */
  289. *addr = (FPW) 0x00FF00FF;
  290. rcode = 1;
  291. break;
  292. }
  293. }
  294. /* clear status register cmd. */
  295. *addr = (FPW) 0x00500050;
  296. *addr = (FPW) 0x00FF00FF;/* resest to read mode */
  297. printf (" done\n");
  298. }
  299. }
  300. flash_vpp(0);
  301. return rcode;
  302. }
  303. /*-----------------------------------------------------------------------
  304. * Copy memory to flash, returns:
  305. * 0 - OK
  306. * 1 - write timeout
  307. * 2 - Flash not erased
  308. * 4 - Flash not identified
  309. */
  310. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  311. {
  312. ulong cp, wp;
  313. FPW data;
  314. int count, i, l, rc, port_width;
  315. if (info->flash_id == FLASH_UNKNOWN) {
  316. return 4;
  317. }
  318. /* get lower word aligned address */
  319. #ifdef FLASH_PORT_WIDTH16
  320. wp = (addr & ~1);
  321. port_width = 2;
  322. #else
  323. wp = (addr & ~3);
  324. port_width = 4;
  325. #endif
  326. flash_vpp(1);
  327. /*
  328. * handle unaligned start bytes
  329. */
  330. if ((l = addr - wp) != 0) {
  331. data = 0;
  332. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  333. data = (data << 8) | (*(uchar *) cp);
  334. }
  335. for (; i < port_width && cnt > 0; ++i) {
  336. data = (data << 8) | *src++;
  337. --cnt;
  338. ++cp;
  339. }
  340. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  341. data = (data << 8) | (*(uchar *) cp);
  342. }
  343. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  344. flash_vpp(0);
  345. return (rc);
  346. }
  347. wp += port_width;
  348. }
  349. /*
  350. * handle word aligned part
  351. */
  352. count = 0;
  353. while (cnt >= port_width) {
  354. data = 0;
  355. for (i = 0; i < port_width; ++i) {
  356. data = (data << 8) | *src++;
  357. }
  358. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  359. flash_vpp(0);
  360. return (rc);
  361. }
  362. wp += port_width;
  363. cnt -= port_width;
  364. if (count++ > 0x800) {
  365. spin_wheel ();
  366. count = 0;
  367. }
  368. }
  369. if (cnt == 0) {
  370. flash_vpp(0);
  371. return (0);
  372. }
  373. /*
  374. * handle unaligned tail bytes
  375. */
  376. data = 0;
  377. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  378. data = (data << 8) | *src++;
  379. --cnt;
  380. }
  381. for (; i < port_width; ++i, ++cp) {
  382. data = (data << 8) | (*(uchar *) cp);
  383. }
  384. rc = write_data (info, wp, SWAP (data));
  385. flash_vpp(0);
  386. return rc;
  387. }
  388. /*-----------------------------------------------------------------------
  389. * Write a word or halfword to Flash, returns:
  390. * 0 - OK
  391. * 1 - write timeout
  392. * 2 - Flash not erased
  393. */
  394. static int write_data (flash_info_t * info, ulong dest, FPW data)
  395. {
  396. FPWV *addr = (FPWV *) dest;
  397. ulong status;
  398. int flag;
  399. /* Check if Flash is (sufficiently) erased */
  400. if ((*addr & data) != data) {
  401. printf ("not erased at %08lx (%lx)\n", (ulong) addr, (ulong) *addr);
  402. return (2);
  403. }
  404. flash_vpp(1);
  405. flash_unprotect_sectors (addr);
  406. /* Disable interrupts which might cause a timeout here */
  407. flag = disable_interrupts ();
  408. *addr = (FPW) 0x00400040; /* write setup */
  409. *addr = data;
  410. /* arm simple, non interrupt dependent timer */
  411. reset_timer_masked ();
  412. /* wait while polling the status register */
  413. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  414. if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
  415. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  416. flash_vpp(0);
  417. return (1);
  418. }
  419. }
  420. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  421. flash_vpp(0);
  422. return (0);
  423. }
  424. void inline spin_wheel (void)
  425. {
  426. static int p = 0;
  427. static char w[] = "\\/-";
  428. printf ("\010%c", w[p]);
  429. (++p == 3) ? (p = 0) : 0;
  430. }