flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <asm/arch/sizes.h>
  32. #include <linux/byteorder/swab.h>
  33. #define PHYS_FLASH_SECT_SIZE SZ_128K
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. /* Board support for 1 or 2 flash devices */
  36. #undef FLASH_PORT_WIDTH32
  37. #define FLASH_PORT_WIDTH16
  38. #ifdef FLASH_PORT_WIDTH16
  39. # define FLASH_PORT_WIDTH ushort
  40. # define FLASH_PORT_WIDTHV vu_short
  41. # define SWAP(x) __swab16(x)
  42. #else
  43. # define FLASH_PORT_WIDTH ulong
  44. # define FLASH_PORT_WIDTHV vu_long
  45. # define SWAP(x) __swab32(x)
  46. #endif
  47. #define FPW FLASH_PORT_WIDTH
  48. #define FPWV FLASH_PORT_WIDTHV
  49. #define mb() __asm__ __volatile__ ("" : : : "memory")
  50. /* Flash Organization Structure */
  51. typedef struct OrgDef {
  52. unsigned int sector_number;
  53. unsigned int sector_size;
  54. } OrgDef;
  55. /* Flash Organizations */
  56. OrgDef OrgIntel_28F256L18T[] = {
  57. {4, SZ_32K}, /* 4 * 32kBytes sectors */
  58. {255, SZ_128K}, /* 255 * 128kBytes sectors */
  59. };
  60. /*-----------------------------------------------------------------------
  61. * Functions
  62. */
  63. unsigned long flash_init (void);
  64. static ulong flash_get_size (FPW * addr, flash_info_t * info);
  65. static int write_data (flash_info_t * info, ulong dest, FPW data);
  66. static void flash_get_offsets (ulong base, flash_info_t * info);
  67. void inline spin_wheel (void);
  68. void flash_print_info (flash_info_t * info);
  69. void flash_unprotect_sectors (FPWV * addr);
  70. int flash_erase (flash_info_t * info, int s_first, int s_last);
  71. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
  72. void flash_unlock(flash_info_t * info, int bank);
  73. int flash_probe(void);
  74. /*-----------------------------------------------------------------------
  75. */
  76. /* see if flash is ok */
  77. int flash_probe(void)
  78. {
  79. return(flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[0]));
  80. }
  81. unsigned long flash_init (void)
  82. {
  83. int i;
  84. ulong size = 0;
  85. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  86. switch (i) {
  87. case 0:
  88. flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
  89. flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
  90. /* to reset the lock bit */
  91. flash_unlock(&flash_info[i],i);
  92. break;
  93. case 1:
  94. flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
  95. flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
  96. /* to reset the lock bit */
  97. flash_unlock(&flash_info[i],i);
  98. break;
  99. default:
  100. panic ("configured too many flash banks!\n");
  101. break;
  102. }
  103. size += flash_info[i].size;
  104. }
  105. #ifdef CFG_ENV_IS_IN_FLASH
  106. /* Protect monitor and environment sectors
  107. */
  108. flash_protect (FLAG_PROTECT_SET,
  109. CFG_FLASH_BASE,
  110. CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
  111. flash_protect (FLAG_PROTECT_SET,
  112. CFG_ENV_ADDR,
  113. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  114. #endif
  115. return size;
  116. }
  117. /*-----------------------------------------------------------------------
  118. */
  119. void flash_unlock(flash_info_t * info, int bank)
  120. {
  121. int j;
  122. if (!bank)
  123. j=2; /* leave 0,1 locked for boot bank */
  124. else
  125. j=0; /* get the whole bank for #2 */
  126. for (;j<CFG_MAX_FLASH_SECT;j++) {
  127. FPWV *addr = (FPWV *) (info->start[j]);
  128. if (addr == NULL) {
  129. printf("Warning Flash probe failed\n");
  130. break;
  131. }
  132. flash_unprotect_sectors (addr);
  133. *addr = (FPW) 0x00500050;/* clear status register */
  134. *addr = (FPW) 0x00FF00FF;/* resest to read mode */
  135. }
  136. }
  137. /*-----------------------------------------------------------------------
  138. */
  139. static void flash_get_offsets (ulong base, flash_info_t * info)
  140. {
  141. int i;
  142. volatile int r; /* gcc 3.4.0-1 strangeness, need to follow up.*/
  143. if (info->flash_id == FLASH_UNKNOWN) {
  144. return;
  145. }
  146. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  147. for (i = 0; i < info->sector_count; i++) {
  148. if (i > 254) { /* 255,256,257,258 */
  149. r=i;
  150. info->start[i] = base + (((r-(int)255) * SZ_32K) + (255*PHYS_FLASH_SECT_SIZE));
  151. info->protect[i] = 0;
  152. } else {
  153. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  154. info->protect[i] = 0;
  155. }
  156. }
  157. }
  158. }
  159. /*-----------------------------------------------------------------------
  160. */
  161. void flash_print_info (flash_info_t * info)
  162. {
  163. int i;
  164. if (info->flash_id == FLASH_UNKNOWN) {
  165. printf ("missing or unknown FLASH type\n");
  166. return;
  167. }
  168. switch (info->flash_id & FLASH_VENDMASK) {
  169. case FLASH_MAN_INTEL:
  170. printf ("INTEL ");
  171. break;
  172. default:
  173. printf ("Unknown Vendor ");
  174. break;
  175. }
  176. switch (info->flash_id & FLASH_TYPEMASK) {
  177. case FLASH_28F256L18T:
  178. printf ("FLASH 28F256L18T\n");
  179. break;
  180. default:
  181. printf ("Unknown Chip Type\n");
  182. break;
  183. }
  184. printf (" Size: %ld MB in %d Sectors\n",
  185. info->size >> 20, info->sector_count);
  186. printf (" Sector Start Addresses:");
  187. for (i = 0; i < info->sector_count; ++i) {
  188. if ((i % 5) == 0)
  189. printf ("\n ");
  190. printf (" %08lX%s",
  191. info->start[i], info->protect[i] ? " (RO)" : " ");
  192. }
  193. printf ("\n");
  194. return;
  195. }
  196. /*
  197. * The following code cannot be run from FLASH!
  198. */
  199. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  200. {
  201. volatile FPW value;
  202. /* mb(); this one makes ARM11 err go away, but I want it :) as a guide to problems */
  203. /* Write auto select command: read Manufacturer ID */
  204. addr[0x5555] = (FPW) 0x00AA00AA;
  205. addr[0x2AAA] = (FPW) 0x00550055;
  206. addr[0x5555] = (FPW) 0x00900090;
  207. mb ();
  208. value = addr[0] & 0xFF; /* just looking for 89 (8989 is hw pat)*/
  209. switch (value) {
  210. case (FPW) INTEL_MANUFACT:
  211. info->flash_id = FLASH_MAN_INTEL;
  212. break;
  213. default:
  214. info->flash_id = FLASH_UNKNOWN;
  215. info->sector_count = 0;
  216. info->size = 0;
  217. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  218. return(0); /* no or unknown flash */
  219. }
  220. mb ();
  221. value = addr[1]; /* device ID */
  222. switch (value) {
  223. case (FPW) (INTEL_ID_28F256L18T): /* 880D */
  224. info->flash_id += FLASH_28F256L18T;
  225. info->sector_count = 259; /*0-258*/
  226. info->size = SZ_32M;
  227. break; /* => 32 MB */
  228. default:
  229. info->flash_id = FLASH_UNKNOWN;
  230. break;
  231. }
  232. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  233. printf ("** ERROR: sector count %d > max (%d) **\n",
  234. info->sector_count, CFG_MAX_FLASH_SECT);
  235. info->sector_count = CFG_MAX_FLASH_SECT;
  236. }
  237. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  238. return(info->size);
  239. }
  240. /* unprotects a sector for write and erase
  241. * on some intel parts, this unprotects the entire chip, but it
  242. * wont hurt to call this additional times per sector...
  243. */
  244. void flash_unprotect_sectors (FPWV * addr)
  245. {
  246. #define PD_FINTEL_WSMS_READY_MASK 0x0080
  247. *addr = (FPW) 0x00500050; /* clear status register */
  248. /* this sends the clear lock bit command */
  249. *addr = (FPW) 0x00600060;
  250. *addr = (FPW) 0x00D000D0;
  251. }
  252. /*-----------------------------------------------------------------------
  253. */
  254. int flash_erase (flash_info_t * info, int s_first, int s_last)
  255. {
  256. int prot, sect;
  257. ulong type, start, last;
  258. int rcode = 0;
  259. #ifdef CONFIG_USE_IRQ
  260. int iflag;
  261. #endif
  262. if ((s_first < 0) || (s_first > s_last)) {
  263. if (info->flash_id == FLASH_UNKNOWN) {
  264. printf ("- missing\n");
  265. } else {
  266. printf ("- no sectors to erase\n");
  267. }
  268. return 1;
  269. }
  270. type = (info->flash_id & FLASH_VENDMASK);
  271. if ((type != FLASH_MAN_INTEL)) {
  272. printf ("Can't erase unknown flash type %08lx - aborted\n",
  273. info->flash_id);
  274. return 1;
  275. }
  276. prot = 0;
  277. for (sect = s_first; sect <= s_last; ++sect) {
  278. if (info->protect[sect]) {
  279. prot++;
  280. }
  281. }
  282. if (prot) {
  283. printf ("- Warning: %d protected sectors will not be erased!\n",
  284. prot);
  285. } else {
  286. printf ("\n");
  287. }
  288. start = get_timer (0);
  289. last = start;
  290. #ifdef CONFIG_USE_IRQ
  291. /* Disable interrupts which might cause a timeout here */
  292. iflag = disable_interrupts ();
  293. #endif
  294. /* Start erase on unprotected sectors */
  295. for (sect = s_first; sect <= s_last; sect++) {
  296. if (info->protect[sect] == 0) { /* not protected */
  297. FPWV *addr = (FPWV *) (info->start[sect]);
  298. FPW status;
  299. printf ("Erasing sector %2d ... ", sect);
  300. flash_unprotect_sectors (addr);
  301. /* arm simple, non interrupt dependent timer */
  302. reset_timer_masked ();
  303. *addr = (FPW) 0x00500050;/* clear status register */
  304. *addr = (FPW) 0x00200020;/* erase setup */
  305. *addr = (FPW) 0x00D000D0;/* erase confirm */
  306. while (((status =
  307. *addr) & (FPW) 0x00800080) !=
  308. (FPW) 0x00800080) {
  309. if (get_timer_masked () >
  310. CFG_FLASH_ERASE_TOUT) {
  311. printf ("Timeout\n");
  312. /* suspend erase */
  313. *addr = (FPW) 0x00B000B0;
  314. /* reset to read mode */
  315. *addr = (FPW) 0x00FF00FF;
  316. rcode = 1;
  317. break;
  318. }
  319. }
  320. /* clear status register cmd. */
  321. *addr = (FPW) 0x00500050;
  322. *addr = (FPW) 0x00FF00FF;/* resest to read mode */
  323. printf (" done\n");
  324. }
  325. }
  326. #ifdef CONFIG_USE_IRQ
  327. if (iflag)
  328. enable_interrupts();
  329. #endif
  330. return rcode;
  331. }
  332. /*-----------------------------------------------------------------------
  333. * Copy memory to flash, returns:
  334. * 0 - OK
  335. * 1 - write timeout
  336. * 2 - Flash not erased
  337. * 4 - Flash not identified
  338. */
  339. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  340. {
  341. ulong cp, wp;
  342. FPW data;
  343. int count, i, l, rc, port_width;
  344. if (info->flash_id == FLASH_UNKNOWN) {
  345. return 4;
  346. }
  347. /* get lower word aligned address */
  348. #ifdef FLASH_PORT_WIDTH16
  349. wp = (addr & ~1);
  350. port_width = 2;
  351. #else
  352. wp = (addr & ~3);
  353. port_width = 4;
  354. #endif
  355. /*
  356. * handle unaligned start bytes
  357. */
  358. if ((l = addr - wp) != 0) {
  359. data = 0;
  360. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  361. data = (data << 8) | (*(uchar *) cp);
  362. }
  363. for (; i < port_width && cnt > 0; ++i) {
  364. data = (data << 8) | *src++;
  365. --cnt;
  366. ++cp;
  367. }
  368. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  369. data = (data << 8) | (*(uchar *) cp);
  370. }
  371. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  372. return(rc);
  373. }
  374. wp += port_width;
  375. }
  376. /*
  377. * handle word aligned part
  378. */
  379. count = 0;
  380. while (cnt >= port_width) {
  381. data = 0;
  382. for (i = 0; i < port_width; ++i) {
  383. data = (data << 8) | *src++;
  384. }
  385. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  386. return(rc);
  387. }
  388. wp += port_width;
  389. cnt -= port_width;
  390. if (count++ > 0x800) {
  391. spin_wheel ();
  392. count = 0;
  393. }
  394. }
  395. if (cnt == 0) {
  396. return(0);
  397. }
  398. /*
  399. * handle unaligned tail bytes
  400. */
  401. data = 0;
  402. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  403. data = (data << 8) | *src++;
  404. --cnt;
  405. }
  406. for (; i < port_width; ++i, ++cp) {
  407. data = (data << 8) | (*(uchar *) cp);
  408. }
  409. return(write_data (info, wp, SWAP (data)));
  410. }
  411. /*-----------------------------------------------------------------------
  412. * Write a word or halfword to Flash, returns:
  413. * 0 - OK
  414. * 1 - write timeout
  415. * 2 - Flash not erased
  416. */
  417. static int write_data (flash_info_t * info, ulong dest, FPW data)
  418. {
  419. FPWV *addr = (FPWV *) dest;
  420. ulong status;
  421. #ifdef CONFIG_USE_IRQ
  422. int iflag;
  423. #endif
  424. /* Check if Flash is (sufficiently) erased */
  425. if ((*addr & data) != data) {
  426. printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
  427. return(2);
  428. }
  429. /* Disable interrupts which might cause a timeout here */
  430. #ifdef CONFIG_USE_IRQ
  431. iflag = disable_interrupts ();
  432. #endif
  433. *addr = (FPW) 0x00400040; /* write setup */
  434. *addr = data;
  435. /* arm simple, non interrupt dependent timer */
  436. reset_timer_masked ();
  437. /* wait while polling the status register */
  438. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  439. if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
  440. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  441. return(1);
  442. }
  443. }
  444. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  445. #ifdef CONFIG_USE_IRQ
  446. if (iflag)
  447. enable_interrupts();
  448. #endif
  449. return(0);
  450. }
  451. void inline spin_wheel (void)
  452. {
  453. static int p = 0;
  454. static char w[] = "\\/-";
  455. printf ("\010%c", w[p]);
  456. (++p == 3) ? (p = 0) : 0;
  457. }