flash.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. * (C) Copyright 2001
  3. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * flash.c - flash support for the 512k, 8bit boot flash
  25. and the 8MB 32bit extra flash on the DB64360
  26. * most of this file was based on the existing U-Boot
  27. * flash drivers.
  28. *
  29. * written or collected and sometimes rewritten by
  30. * Ingo Assmus <ingo.assmus@keymile.com>
  31. *
  32. */
  33. #include <common.h>
  34. #include <mpc8xx.h>
  35. #include "../include/mv_gen_reg.h"
  36. #include "../include/memory.h"
  37. #include "intel_flash.h"
  38. #define FLASH_ROM 0xFFFD /* unknown flash type */
  39. #define FLASH_RAM 0xFFFE /* unknown flash type */
  40. #define FLASH_MAN_UNKNOWN 0xFFFF0000
  41. /* #define DEBUG */
  42. /* Intel flash commands */
  43. int flash_erase_intel (flash_info_t * info, int s_first, int s_last);
  44. int write_word_intel (bank_addr_t addr, bank_word_t value);
  45. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  46. /*-----------------------------------------------------------------------
  47. * Functions
  48. */
  49. static ulong flash_get_size (int portwidth, vu_long * addr,
  50. flash_info_t * info);
  51. static int write_word (flash_info_t * info, ulong dest, ulong data);
  52. static void flash_get_offsets (ulong base, flash_info_t * info);
  53. /*-----------------------------------------------------------------------
  54. */
  55. unsigned long flash_init (void)
  56. {
  57. unsigned int i;
  58. unsigned long size_b0 = 0, size_b1 = 0;
  59. unsigned long base, flash_size;
  60. /* Init: no FLASHes known */
  61. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  62. flash_info[i].flash_id = FLASH_UNKNOWN;
  63. }
  64. /* the boot flash */
  65. base = CONFIG_SYS_FLASH_BASE;
  66. size_b0 =
  67. flash_get_size (CONFIG_SYS_BOOT_FLASH_WIDTH, (vu_long *) base,
  68. &flash_info[0]);
  69. printf ("[%ldkB@%lx] ", size_b0 / 1024, base);
  70. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  71. printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n", base, size_b0, size_b0 << 20);
  72. }
  73. base = memoryGetDeviceBaseAddress (CONFIG_SYS_EXTRA_FLASH_DEVICE);
  74. /* base = memoryGetDeviceBaseAddress(DEV_CS3_BASE_ADDR);*/
  75. for (i = 1; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  76. unsigned long size =
  77. flash_get_size (CONFIG_SYS_EXTRA_FLASH_WIDTH,
  78. (vu_long *) base, &flash_info[i]);
  79. printf ("[%ldMB@%lx] ", size >> 20, base);
  80. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  81. if (i == 1) {
  82. printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n", base, size_b1, size_b1 << 20);
  83. }
  84. break;
  85. }
  86. size_b1 += size;
  87. base += size;
  88. }
  89. flash_size = size_b0 + size_b1;
  90. return flash_size;
  91. }
  92. /*-----------------------------------------------------------------------
  93. */
  94. static void flash_get_offsets (ulong base, flash_info_t * info)
  95. {
  96. int i;
  97. int sector_size;
  98. if (!info->sector_count)
  99. return;
  100. /* set up sector start address table */
  101. switch (info->flash_id & FLASH_TYPEMASK) {
  102. case FLASH_AM040:
  103. case FLASH_28F128J3A:
  104. case FLASH_28F640J3A:
  105. case FLASH_RAM:
  106. /* this chip has uniformly spaced sectors */
  107. sector_size = info->size / info->sector_count;
  108. for (i = 0; i < info->sector_count; i++)
  109. info->start[i] = base + (i * sector_size);
  110. break;
  111. default:
  112. if (info->flash_id & FLASH_BTYPE) {
  113. /* set sector offsets for bottom boot block type */
  114. info->start[0] = base + 0x00000000;
  115. info->start[1] = base + 0x00008000;
  116. info->start[2] = base + 0x0000C000;
  117. info->start[3] = base + 0x00010000;
  118. for (i = 4; i < info->sector_count; i++) {
  119. info->start[i] =
  120. base + (i * 0x00020000) - 0x00060000;
  121. }
  122. } else {
  123. /* set sector offsets for top boot block type */
  124. i = info->sector_count - 1;
  125. info->start[i--] = base + info->size - 0x00008000;
  126. info->start[i--] = base + info->size - 0x0000C000;
  127. info->start[i--] = base + info->size - 0x00010000;
  128. for (; i >= 0; i--) {
  129. info->start[i] = base + i * 0x00020000;
  130. }
  131. }
  132. }
  133. }
  134. /*-----------------------------------------------------------------------
  135. */
  136. void flash_print_info (flash_info_t * info)
  137. {
  138. int i;
  139. if (info->flash_id == FLASH_UNKNOWN) {
  140. printf ("missing or unknown FLASH type\n");
  141. return;
  142. }
  143. switch (info->flash_id & FLASH_VENDMASK) {
  144. case FLASH_MAN_STM:
  145. printf ("STM ");
  146. break;
  147. case FLASH_MAN_AMD:
  148. printf ("AMD ");
  149. break;
  150. case FLASH_MAN_FUJ:
  151. printf ("FUJITSU ");
  152. break;
  153. case FLASH_MAN_INTEL:
  154. printf ("INTEL ");
  155. break;
  156. default:
  157. printf ("Unknown Vendor ");
  158. break;
  159. }
  160. switch (info->flash_id & FLASH_TYPEMASK) {
  161. case FLASH_AM040:
  162. printf ("AM29LV040B (4 Mbit, bottom boot sect)\n");
  163. break;
  164. case FLASH_AM400B:
  165. printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  166. break;
  167. case FLASH_AM400T:
  168. printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  169. break;
  170. case FLASH_AM800B:
  171. printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  172. break;
  173. case FLASH_AM800T:
  174. printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  175. break;
  176. case FLASH_AM160B:
  177. printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  178. break;
  179. case FLASH_AM160T:
  180. printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  181. break;
  182. case FLASH_AM320B:
  183. printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  184. break;
  185. case FLASH_AM320T:
  186. printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  187. break;
  188. case FLASH_28F640J3A:
  189. printf ("28F640J3A (64 Mbit)\n");
  190. break;
  191. case FLASH_28F128J3A:
  192. printf ("28F128J3A (128 Mbit)\n");
  193. break;
  194. case FLASH_ROM:
  195. printf ("ROM\n");
  196. break;
  197. case FLASH_RAM:
  198. printf ("RAM\n");
  199. break;
  200. default:
  201. printf ("Unknown Chip Type\n");
  202. break;
  203. }
  204. if ((info->size >> 20) > 0) {
  205. printf (" Size: %ld MB in %d Sectors\n",
  206. info->size >> 20, info->sector_count);
  207. } else {
  208. printf (" Size: %ld kB in %d Sectors\n",
  209. info->size >> 10, info->sector_count);
  210. }
  211. printf (" Sector Start Addresses:");
  212. for (i = 0; i < info->sector_count; ++i) {
  213. if ((i % 5) == 0)
  214. printf ("\n ");
  215. printf (" %08lX%s",
  216. info->start[i], info->protect[i] ? " (RO)" : " ");
  217. }
  218. printf ("\n");
  219. return;
  220. }
  221. /*-----------------------------------------------------------------------
  222. */
  223. /*-----------------------------------------------------------------------
  224. */
  225. /*
  226. * The following code cannot be run from FLASH!
  227. */
  228. static inline void flash_cmd (int width, volatile unsigned char *addr,
  229. int offset, unsigned char cmd)
  230. {
  231. /* supports 1x8, 1x16, and 2x16 */
  232. /* 2x8 and 4x8 are not supported */
  233. if (width == 4) {
  234. /* assuming chips are in 16 bit mode */
  235. /* 2x16 */
  236. unsigned long cmd32 = (cmd << 16) | cmd;
  237. *(volatile unsigned long *) (addr + offset * 2) = cmd32;
  238. } else {
  239. /* 1x16 or 1x8 */
  240. *(volatile unsigned char *) (addr + offset) = cmd;
  241. }
  242. }
  243. static ulong
  244. flash_get_size (int portwidth, vu_long * addr, flash_info_t * info)
  245. {
  246. short i;
  247. volatile unsigned char *caddr = (unsigned char *) addr;
  248. volatile unsigned short *saddr = (unsigned short *) addr;
  249. volatile unsigned long *laddr = (unsigned long *) addr;
  250. char old[2], save;
  251. ulong id = 0, manu = 0, base = (ulong) addr;
  252. #ifdef DEBUG
  253. printf ("%s: enter\n", __FUNCTION__);
  254. #endif
  255. info->portwidth = portwidth;
  256. save = *caddr;
  257. flash_cmd (portwidth, caddr, 0, 0xf0);
  258. flash_cmd (portwidth, caddr, 0, 0xf0);
  259. udelay (10);
  260. old[0] = caddr[0];
  261. old[1] = caddr[1];
  262. if (old[0] != 0xf0) {
  263. flash_cmd (portwidth, caddr, 0, 0xf0);
  264. flash_cmd (portwidth, caddr, 0, 0xf0);
  265. udelay (10);
  266. if (*caddr == 0xf0) {
  267. /* this area is ROM */
  268. *caddr = save;
  269. info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
  270. info->sector_count = 8;
  271. info->size = 0x80000;
  272. flash_get_offsets (base, info);
  273. return info->size;
  274. }
  275. } else {
  276. *caddr = 0;
  277. udelay (10);
  278. if (*caddr == 0) {
  279. /* this area is RAM */
  280. *caddr = save;
  281. info->flash_id = FLASH_RAM + FLASH_MAN_UNKNOWN;
  282. info->sector_count = 8;
  283. info->size = 0x80000;
  284. flash_get_offsets (base, info);
  285. return info->size;
  286. }
  287. flash_cmd (portwidth, caddr, 0, 0xf0);
  288. udelay (10);
  289. }
  290. /* Write auto select command: read Manufacturer ID */
  291. flash_cmd (portwidth, caddr, 0x555, 0xAA);
  292. flash_cmd (portwidth, caddr, 0x2AA, 0x55);
  293. flash_cmd (portwidth, caddr, 0x555, 0x90);
  294. udelay (10);
  295. if ((caddr[0] == old[0]) && (caddr[1] == old[1])) {
  296. /* this area is ROM */
  297. info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
  298. info->sector_count = 8;
  299. info->size = 0x80000;
  300. flash_get_offsets (base, info);
  301. return info->size;
  302. #ifdef DEBUG
  303. } else {
  304. printf ("%px%d: %02x:%02x -> %02x:%02x\n",
  305. caddr, portwidth, old[0], old[1], caddr[0], caddr[1]);
  306. #endif
  307. }
  308. switch (portwidth) {
  309. case 1:
  310. manu = caddr[0];
  311. manu |= manu << 16;
  312. id = caddr[1];
  313. break;
  314. case 2:
  315. manu = saddr[0];
  316. manu |= manu << 16;
  317. id = saddr[1];
  318. id |= id << 16;
  319. break;
  320. case 4:
  321. manu = laddr[0];
  322. id = laddr[1];
  323. break;
  324. }
  325. #ifdef DEBUG
  326. flash_cmd (portwidth, caddr, 0, 0xf0);
  327. printf ("\n%08lx:%08lx:%08lx\n", base, manu, id);
  328. printf ("%08lx %08lx %08lx %08lx\n",
  329. laddr[0], laddr[1], laddr[2], laddr[3]);
  330. #endif
  331. switch (manu) {
  332. case STM_MANUFACT:
  333. info->flash_id = FLASH_MAN_STM;
  334. break;
  335. case AMD_MANUFACT:
  336. info->flash_id = FLASH_MAN_AMD;
  337. break;
  338. case FUJ_MANUFACT:
  339. info->flash_id = FLASH_MAN_FUJ;
  340. break;
  341. case INTEL_MANUFACT:
  342. info->flash_id = FLASH_MAN_INTEL;
  343. break;
  344. default:
  345. flash_cmd (portwidth, caddr, 0, 0xf0);
  346. printf ("Unknown Mfr [%08lx]:%08lx\n", manu, id);
  347. info->flash_id = FLASH_UNKNOWN;
  348. info->sector_count = 0;
  349. info->size = 0;
  350. return (0); /* no or unknown flash */
  351. }
  352. switch (id) {
  353. case AMD_ID_LV400T:
  354. info->flash_id += FLASH_AM400T;
  355. info->sector_count = 11;
  356. info->size = 0x00100000;
  357. info->chipwidth = 1;
  358. break; /* => 1 MB */
  359. case AMD_ID_LV400B:
  360. info->flash_id += FLASH_AM400B;
  361. info->sector_count = 11;
  362. info->size = 0x00100000;
  363. info->chipwidth = 1;
  364. break; /* => 1 MB */
  365. case AMD_ID_LV800T:
  366. info->flash_id += FLASH_AM800T;
  367. info->sector_count = 19;
  368. info->size = 0x00200000;
  369. info->chipwidth = 1;
  370. break; /* => 2 MB */
  371. case AMD_ID_LV800B:
  372. info->flash_id += FLASH_AM800B;
  373. info->sector_count = 19;
  374. info->size = 0x00200000;
  375. info->chipwidth = 1;
  376. break; /* => 2 MB */
  377. case AMD_ID_LV160T:
  378. info->flash_id += FLASH_AM160T;
  379. info->sector_count = 35;
  380. info->size = 0x00400000;
  381. info->chipwidth = 1;
  382. break; /* => 4 MB */
  383. case AMD_ID_LV160B:
  384. info->flash_id += FLASH_AM160B;
  385. info->sector_count = 35;
  386. info->size = 0x00400000;
  387. info->chipwidth = 1;
  388. break; /* => 4 MB */
  389. #if 0 /* enable when device IDs are available */
  390. case AMD_ID_LV320T:
  391. info->flash_id += FLASH_AM320T;
  392. info->sector_count = 67;
  393. info->size = 0x00800000;
  394. break; /* => 8 MB */
  395. case AMD_ID_LV320B:
  396. info->flash_id += FLASH_AM320B;
  397. info->sector_count = 67;
  398. info->size = 0x00800000;
  399. break; /* => 8 MB */
  400. #endif
  401. case AMD_ID_LV040B:
  402. info->flash_id += FLASH_AM040;
  403. info->sector_count = 8;
  404. info->size = 0x80000;
  405. info->chipwidth = 1;
  406. break; /* => 512 kB */
  407. case INTEL_ID_28F640J3A:
  408. info->flash_id += FLASH_28F640J3A;
  409. info->sector_count = 64;
  410. info->size = 128 * 1024 * 64; /* 128kbytes x 64 blocks */
  411. info->chipwidth = 2;
  412. if (portwidth == 4)
  413. info->size *= 2; /* 2x16 */
  414. break;
  415. case INTEL_ID_28F128J3A:
  416. info->flash_id += FLASH_28F128J3A;
  417. info->sector_count = 128;
  418. info->size = 128 * 1024 * 128; /* 128kbytes x 128 blocks */
  419. info->chipwidth = 2;
  420. if (portwidth == 4)
  421. info->size *= 2; /* 2x16 */
  422. break;
  423. default:
  424. flash_cmd (portwidth, caddr, 0, 0xf0);
  425. printf ("Unknown id %lx:[%lx]\n", manu, id);
  426. info->flash_id = FLASH_UNKNOWN;
  427. info->chipwidth = 1;
  428. return (0); /* => no or unknown flash */
  429. }
  430. flash_get_offsets (base, info);
  431. /* check for protected sectors */
  432. for (i = 0; i < info->sector_count; i++) {
  433. /* read sector protection at sector address, (A7 .. A0)=0x02 */
  434. /* D0 = 1 if protected */
  435. caddr = (volatile unsigned char *) (info->start[i]);
  436. saddr = (volatile unsigned short *) (info->start[i]);
  437. laddr = (volatile unsigned long *) (info->start[i]);
  438. if (portwidth == 1)
  439. info->protect[i] = caddr[2] & 1;
  440. else if (portwidth == 2)
  441. info->protect[i] = saddr[2] & 1;
  442. else
  443. info->protect[i] = laddr[2] & 1;
  444. }
  445. /*
  446. * Prevent writes to uninitialized FLASH.
  447. */
  448. if (info->flash_id != FLASH_UNKNOWN) {
  449. caddr = (volatile unsigned char *) info->start[0];
  450. flash_cmd (portwidth, caddr, 0, 0xF0); /* reset bank */
  451. }
  452. return (info->size);
  453. }
  454. int flash_erase (flash_info_t * info, int s_first, int s_last)
  455. {
  456. volatile unsigned char *addr = (uchar *) (info->start[0]);
  457. int flag, prot, sect, l_sect;
  458. ulong start, now, last;
  459. /* modified to support 2x16 Intel flash */
  460. /* Note that the code will not exit on a flash erasure error or timeout */
  461. /* but will print and error message and continue processing sectors */
  462. /* until they are all erased. */
  463. /* 10-16-2002 P. Marchese */
  464. ulong mask;
  465. int timeout;
  466. if (info->portwidth == 4)
  467. /* {
  468. printf ("- Warning: erasing of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
  469. return 1;
  470. }*/
  471. {
  472. /* make sure it's Intel flash */
  473. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  474. /* yup! it's an Intel flash */
  475. /* is it 16-bits wide? */
  476. if (info->chipwidth == 2) {
  477. /* yup! it's 16-bits wide */
  478. /* are there any sectors to process? */
  479. if ((s_first < 0) || (s_first > s_last)) {
  480. printf ("Error: There are no sectors to erase\n");
  481. printf ("Either sector %d is less than zero\n", s_first);
  482. printf ("or sector %d is greater than sector %d\n", s_first, s_last);
  483. return 1;
  484. }
  485. /* check for protected sectors */
  486. prot = 0;
  487. for (sect = s_first; sect <= s_last; ++sect)
  488. if (info->protect[sect])
  489. prot++;
  490. /* if variable "prot" is nonzero, there are protected sectors */
  491. if (prot)
  492. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  493. /* reset the flash */
  494. flash_cmd (info->portwidth, addr, 0,
  495. CHIP_CMD_RST);
  496. /* Disable interrupts which might cause a timeout here */
  497. flag = disable_interrupts ();
  498. /* Clear the status register */
  499. flash_cmd (info->portwidth, addr, 0,
  500. CHIP_CMD_CLR_STAT);
  501. flash_cmd (info->portwidth, addr, 0,
  502. CHIP_CMD_RST);
  503. /* Start erase on unprotected sectors */
  504. for (sect = s_first; sect <= s_last; sect++) {
  505. /* is the sector unprotected? */
  506. if (info->protect[sect] == 0) { /* not protected */
  507. /* issue the single block erase command, 0x20 */
  508. flash_cmd (info->portwidth,
  509. (volatile unsigned
  510. char *) info->
  511. start[sect], 0,
  512. CHIP_CMD_ERASE1);
  513. /* issue the erase confirm command, 0xD0 */
  514. flash_cmd (info->portwidth,
  515. (volatile unsigned
  516. char *) info->
  517. start[sect], 0,
  518. CHIP_CMD_ERASE2);
  519. l_sect = sect;
  520. /* re-enable interrupts if necessary */
  521. if (flag)
  522. enable_interrupts ();
  523. /* poll for erasure completion */
  524. /* put flash into read status mode by writing 0x70 to it */
  525. flash_cmd (info->portwidth,
  526. addr, 0,
  527. CHIP_CMD_RD_STAT);
  528. /* setup the status register mask */
  529. mask = CHIP_STAT_RDY |
  530. (CHIP_STAT_RDY << 16);
  531. /* init. the timeout counter */
  532. start = get_timer (0);
  533. /* keep looping while the flash is not ready */
  534. /* exit the loop by timing out or the flash */
  535. /* becomes ready again */
  536. timeout = 0;
  537. while ((*
  538. (volatile unsigned
  539. long *) info->
  540. start[sect] & mask) !=
  541. mask) {
  542. /* has the timeout limit been reached? */
  543. if (get_timer (start)
  544. >
  545. CONFIG_SYS_FLASH_ERASE_TOUT)
  546. {
  547. /* timeout limit reached */
  548. printf ("Time out limit reached erasing sector at address %08lx\n", info->start[sect]);
  549. printf ("Continuing with next sector\n");
  550. timeout = 1;
  551. goto timed_out_error;
  552. }
  553. /* put flash into read status mode by writing 0x70 to it */
  554. flash_cmd (info->
  555. portwidth,
  556. addr, 0,
  557. CHIP_CMD_RD_STAT);
  558. }
  559. /* did we timeout? */
  560. timed_out_error:if (timeout == 0)
  561. {
  562. /* didn't timeout, so check the status register */
  563. /* create the status mask to check for errors */
  564. mask = CHIP_STAT_ECLBS;
  565. mask = mask | (mask <<
  566. 16);
  567. /* put flash into read status mode by writing 0x70 to it */
  568. flash_cmd (info->
  569. portwidth,
  570. addr, 0,
  571. CHIP_CMD_RD_STAT);
  572. /* are there any errors? */
  573. if ((*
  574. (volatile
  575. unsigned long *)
  576. info->
  577. start[sect] &
  578. mask) != 0) {
  579. /* We got an erasure error */
  580. printf ("Flash erasure error at address 0x%08lx\n", info->start[sect]);
  581. printf ("Continuing with next sector\n");
  582. /* reset the flash */
  583. flash_cmd
  584. (info->
  585. portwidth,
  586. addr,
  587. 0,
  588. CHIP_CMD_RST);
  589. }
  590. }
  591. /* erasure completed without errors */
  592. /* reset the flash */
  593. flash_cmd (info->portwidth,
  594. addr, 0,
  595. CHIP_CMD_RST);
  596. } /* end if not protected */
  597. } /* end for loop */
  598. printf ("Flash erasure done\n");
  599. return 0;
  600. } else {
  601. /* The Intel flash is not 16-bit wide */
  602. /* print and error message and return */
  603. /* NOTE: you can add routines here to handle other size flash */
  604. printf ("Error: Intel flash device is only %d-bits wide\n", info->chipwidth * 8);
  605. printf ("The erasure code only handles Intel 16-bit wide flash memory\n");
  606. return 1;
  607. }
  608. } else {
  609. /* Not Intel flash so return an error as a write timeout */
  610. /* NOTE: if it's another type flash, stick its routine here */
  611. printf ("Error: The flash device is not Intel type\n");
  612. printf ("The erasure code only supports Intel flash in a 32-bit port width\n");
  613. return 1;
  614. }
  615. }
  616. /* end 32-bit wide flash code */
  617. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM)
  618. return 1; /* Rom can not be erased */
  619. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) { /* RAM just copy 0s to RAM */
  620. for (sect = s_first; sect <= s_last; sect++) {
  621. int sector_size = info->size / info->sector_count;
  622. addr = (uchar *) (info->start[sect]);
  623. memset ((void *) addr, 0, sector_size);
  624. }
  625. return 0;
  626. }
  627. if ((s_first < 0) || (s_first > s_last)) {
  628. if (info->flash_id == FLASH_UNKNOWN) {
  629. printf ("- missing\n");
  630. } else {
  631. printf ("- no sectors to erase\n");
  632. }
  633. return 1;
  634. }
  635. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { /* Intel works spezial */
  636. return flash_erase_intel (info,
  637. (unsigned short) s_first,
  638. (unsigned short) s_last);
  639. }
  640. #if 0
  641. if ((info->flash_id == FLASH_UNKNOWN) || /* Flash is unknown to PPCBoot */
  642. (info->flash_id > FLASH_AMD_COMP)) {
  643. printf ("Can't erase unknown flash type %08lx - aborted\n",
  644. info->flash_id);
  645. return 1;
  646. }
  647. #endif
  648. prot = 0;
  649. for (sect = s_first; sect <= s_last; ++sect) {
  650. if (info->protect[sect]) {
  651. prot++;
  652. }
  653. }
  654. if (prot) {
  655. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  656. } else {
  657. printf ("\n");
  658. }
  659. l_sect = -1;
  660. /* Disable interrupts which might cause a timeout here */
  661. flag = disable_interrupts ();
  662. flash_cmd (info->portwidth, addr, 0x555, 0xAA); /* start erase routine */
  663. flash_cmd (info->portwidth, addr, 0x2AA, 0x55);
  664. flash_cmd (info->portwidth, addr, 0x555, 0x80);
  665. flash_cmd (info->portwidth, addr, 0x555, 0xAA);
  666. flash_cmd (info->portwidth, addr, 0x2AA, 0x55);
  667. /* Start erase on unprotected sectors */
  668. for (sect = s_first; sect <= s_last; sect++) {
  669. if (info->protect[sect] == 0) { /* not protected */
  670. addr = (uchar *) (info->start[sect]);
  671. flash_cmd (info->portwidth, addr, 0, 0x30);
  672. l_sect = sect;
  673. }
  674. }
  675. /* re-enable interrupts if necessary */
  676. if (flag)
  677. enable_interrupts ();
  678. /* wait at least 80us - let's wait 1 ms */
  679. udelay (1000);
  680. /*
  681. * We wait for the last triggered sector
  682. */
  683. if (l_sect < 0)
  684. goto DONE;
  685. start = get_timer (0);
  686. last = start;
  687. addr = (volatile unsigned char *) (info->start[l_sect]);
  688. /* broken for 2x16: TODO */
  689. while ((addr[0] & 0x80) != 0x80) {
  690. if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  691. printf ("Timeout\n");
  692. return 1;
  693. }
  694. /* show that we're waiting */
  695. if ((now - last) > 1000) { /* every second */
  696. putc ('.');
  697. last = now;
  698. }
  699. }
  700. DONE:
  701. /* reset to read mode */
  702. addr = (volatile unsigned char *) info->start[0];
  703. flash_cmd (info->portwidth, addr, 0, 0xf0);
  704. flash_cmd (info->portwidth, addr, 0, 0xf0);
  705. printf (" done\n");
  706. return 0;
  707. }
  708. /*-----------------------------------------------------------------------
  709. * Copy memory to flash, returns:
  710. * 0 - OK
  711. * 1 - write timeout
  712. * 2 - Flash not erased
  713. */
  714. /* broken for 2x16: TODO */
  715. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  716. {
  717. ulong cp, wp, data;
  718. int i, l, rc;
  719. /* Commented out since the below code should work for 32-bit(2x 16 flash) */
  720. /* 10-16-2002 P. Marchese */
  721. /* if(info->portwidth==4) return 1; */
  722. /* if(info->portwidth==4) {
  723. printf ("- Warning: writting of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
  724. return 1;
  725. }*/
  726. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM)
  727. return 0;
  728. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
  729. memcpy ((void *) addr, src, cnt);
  730. return 0;
  731. }
  732. wp = (addr & ~3); /* get lower word aligned address */
  733. /*
  734. * handle unaligned start bytes
  735. */
  736. if ((l = addr - wp) != 0) {
  737. data = 0;
  738. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  739. data = (data << 8) | (*(uchar *) cp);
  740. }
  741. for (; i < 4 && cnt > 0; ++i) {
  742. data = (data << 8) | *src++;
  743. --cnt;
  744. ++cp;
  745. }
  746. for (; cnt == 0 && i < 4; ++i, ++cp) {
  747. data = (data << 8) | (*(uchar *) cp);
  748. }
  749. if ((rc = write_word (info, wp, data)) != 0) {
  750. return (rc);
  751. }
  752. wp += 4;
  753. }
  754. /*
  755. * handle word aligned part
  756. */
  757. while (cnt >= 4) {
  758. data = 0;
  759. for (i = 0; i < 4; ++i) {
  760. data = (data << 8) | *src++;
  761. }
  762. if ((rc = write_word (info, wp, data)) != 0) {
  763. return (rc);
  764. }
  765. wp += 4;
  766. cnt -= 4;
  767. }
  768. if (cnt == 0) {
  769. return (0);
  770. }
  771. /*
  772. * handle unaligned tail bytes
  773. */
  774. data = 0;
  775. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  776. data = (data << 8) | *src++;
  777. --cnt;
  778. }
  779. for (; i < 4; ++i, ++cp) {
  780. data = (data << 8) | (*(uchar *) cp);
  781. }
  782. return (write_word (info, wp, data));
  783. }
  784. /*-----------------------------------------------------------------------
  785. * Write a word to Flash, returns:
  786. * 0 - OK
  787. * 1 - write timeout
  788. * 2 - Flash not erased
  789. */
  790. /* broken for 2x16: TODO */
  791. static int write_word (flash_info_t * info, ulong dest, ulong data)
  792. {
  793. volatile unsigned char *addr = (uchar *) (info->start[0]);
  794. ulong start;
  795. int flag, i;
  796. ulong mask;
  797. /* modified so that it handles 32-bit(2x16 Intel flash programming */
  798. /* 10-16-2002 P. Marchese */
  799. if (info->portwidth == 4)
  800. /* {
  801. printf ("- Warning: writting of 32Bit (2*16Bit i.e. 2*28F640J3A) not supported yet !!!! \n");
  802. return 1;
  803. }*/
  804. {
  805. /* make sure it's Intel flash */
  806. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  807. /* yup! it's an Intel flash */
  808. /* is it 16-bits wide? */
  809. if (info->chipwidth == 2) {
  810. /* yup! it's 16-bits wide */
  811. /* so we know how to program it */
  812. /* reset the flash */
  813. flash_cmd (info->portwidth, addr, 0,
  814. CHIP_CMD_RST);
  815. /* Disable interrupts which might cause a timeout here */
  816. flag = disable_interrupts ();
  817. /* Clear the status register */
  818. flash_cmd (info->portwidth, addr, 0,
  819. CHIP_CMD_CLR_STAT);
  820. flash_cmd (info->portwidth, addr, 0,
  821. CHIP_CMD_RST);
  822. /* 1st cycle of word/byte program */
  823. /* write 0x40 to the location to program */
  824. flash_cmd (info->portwidth, (uchar *) dest, 0,
  825. CHIP_CMD_PROG);
  826. /* 2nd cycle of word/byte program */
  827. /* write the data to the destination address */
  828. *(ulong *) dest = data;
  829. /* re-enable interrupts if necessary */
  830. if (flag)
  831. enable_interrupts ();
  832. /* setup the status register mask */
  833. mask = CHIP_STAT_RDY | (CHIP_STAT_RDY << 16);
  834. /* put flash into read status mode by writing 0x70 to it */
  835. flash_cmd (info->portwidth, addr, 0,
  836. CHIP_CMD_RD_STAT);
  837. /* init. the timeout counter */
  838. start = get_timer (0);
  839. /* keep looping while the flash is not ready */
  840. /* exit the loop by timing out or the flash */
  841. /* becomes ready again */
  842. /* 11-13-2002 Paul Marchese */
  843. /* modified while loop conditional statement */
  844. /* because we were always timing out. */
  845. /* there is a type mismatch, "addr[0]" */
  846. /* returns a byte but "mask" is a 32-bit value */
  847. while ((*(volatile unsigned long *) info->
  848. start[0] & mask) != mask)
  849. /* original code */
  850. /* while (addr[0] & mask) != mask) */
  851. {
  852. /* has the timeout limit been reached? */
  853. if (get_timer (start) >
  854. CONFIG_SYS_FLASH_WRITE_TOUT) {
  855. /* timeout limit reached */
  856. printf ("Time out limit reached programming address %08lx with data %08lx\n", dest, data);
  857. /* reset the flash */
  858. flash_cmd (info->portwidth,
  859. addr, 0,
  860. CHIP_CMD_RST);
  861. return (1);
  862. }
  863. /* put flash into read status mode by writing 0x70 to it */
  864. flash_cmd (info->portwidth, addr, 0,
  865. CHIP_CMD_RD_STAT);
  866. }
  867. /* flash is ready, so check the status */
  868. /* create the status mask to check for errors */
  869. mask = CHIP_STAT_DPS | CHIP_STAT_VPPS |
  870. CHIP_STAT_PSLBS;
  871. mask = mask | (mask << 16);
  872. /* put flash into read status mode by writing 0x70 to it */
  873. flash_cmd (info->portwidth, addr, 0,
  874. CHIP_CMD_RD_STAT);
  875. /* are there any errors? */
  876. if ((addr[0] & mask) != 0) {
  877. /* We got a one of the following errors: */
  878. /* Voltage range, Device protect, or programming */
  879. /* return the error as a device timeout */
  880. /* put flash into read status mode by writing 0x70 to it */
  881. flash_cmd (info->portwidth, addr, 0,
  882. CHIP_CMD_RD_STAT);
  883. printf ("Flash programming error at address 0x%08lx\n", dest);
  884. printf ("Flash status register contains 0x%08lx\n", (unsigned long) addr[0]);
  885. /* reset the flash */
  886. flash_cmd (info->portwidth, addr, 0,
  887. CHIP_CMD_RST);
  888. return 1;
  889. }
  890. /* write completed without errors */
  891. /* reset the flash */
  892. flash_cmd (info->portwidth, addr, 0,
  893. CHIP_CMD_RST);
  894. return 0;
  895. } else {
  896. /* it's not 16-bits wide, so return an error as a write timeout */
  897. /* NOTE: you can add routines here to handle other size flash */
  898. printf ("Error: Intel flash device is only %d-bits wide\n", info->chipwidth * 8);
  899. printf ("The write code only handles Intel 16-bit wide flash memory\n");
  900. return 1;
  901. }
  902. } else {
  903. /* not Intel flash so return an error as a write timeout */
  904. /* NOTE: if it's another type flash, stick its routine here */
  905. printf ("Error: The flash device is not Intel type\n");
  906. printf ("The code only supports Intel flash in a 32-bit port width\n");
  907. return 1;
  908. }
  909. }
  910. /* end of 32-bit flash code */
  911. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM)
  912. return 1;
  913. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
  914. *(unsigned long *) dest = data;
  915. return 0;
  916. }
  917. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  918. unsigned short low = data & 0xffff;
  919. unsigned short hi = (data >> 16) & 0xffff;
  920. int ret = write_word_intel ((bank_addr_t) dest, hi);
  921. if (!ret)
  922. ret = write_word_intel ((bank_addr_t) (dest + 2),
  923. low);
  924. return ret;
  925. }
  926. /* Check if Flash is (sufficiently) erased */
  927. if ((*((vu_long *) dest) & data) != data) {
  928. return (2);
  929. }
  930. /* Disable interrupts which might cause a timeout here */
  931. flag = disable_interrupts ();
  932. /* first, perform an unlock bypass command to speed up flash writes */
  933. addr[0x555] = 0xAA;
  934. addr[0x2AA] = 0x55;
  935. addr[0x555] = 0x20;
  936. /* write each byte out */
  937. for (i = 0; i < 4; i++) {
  938. char *data_ch = (char *) &data;
  939. addr[0] = 0xA0;
  940. *(((char *) dest) + i) = data_ch[i];
  941. udelay (10); /* XXX */
  942. }
  943. /* we're done, now do an unlock bypass reset */
  944. addr[0] = 0x90;
  945. addr[0] = 0x00;
  946. /* re-enable interrupts if necessary */
  947. if (flag)
  948. enable_interrupts ();
  949. /* data polling for D7 */
  950. start = get_timer (0);
  951. while ((*((vu_long *) dest) & 0x00800080) != (data & 0x00800080)) {
  952. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  953. return (1);
  954. }
  955. }
  956. return (0);
  957. }