flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Changes for MATRIX Vision MVsensor (C) Copyright 2001
  6. * MATRIX Vision GmbH / hg, info@matrix-vision.de
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <mpc8xx.h>
  28. #undef MVDEBUG
  29. #ifdef MVDEBUG
  30. #define mvdebug debug
  31. #else
  32. #define mvdebug(p) do {} while (0)
  33. #endif
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. #ifdef CONFIG_MVS_16BIT_FLASH
  36. #define FLASH_DATA_MASK 0xffff
  37. #define FLASH_SHIFT 0
  38. #else
  39. #define FLASH_DATA_MASK 0xffffffff
  40. #define FLASH_SHIFT 1
  41. #endif
  42. /*-----------------------------------------------------------------------
  43. * Functions
  44. */
  45. static ulong flash_get_size (vu_long *address, flash_info_t *info);
  46. static int write_word (flash_info_t *info, ulong dest, ulong data);
  47. static void flash_get_offsets (ulong base, flash_info_t *info);
  48. /*-----------------------------------------------------------------------
  49. */
  50. unsigned long flash_init (void)
  51. {
  52. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  53. volatile memctl8xx_t *memctl = &immap->im_memctl;
  54. unsigned long size_b0, size_b1;
  55. int i;
  56. /* Init: no FLASHes known */
  57. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  58. flash_info[i].flash_id = FLASH_UNKNOWN;
  59. }
  60. /* Static FLASH Bank configuration here - FIXME XXX */
  61. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  62. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  63. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  64. size_b0, size_b0<<20);
  65. }
  66. #if defined (FLASH_BASE1_PRELIM)
  67. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  68. if (size_b1 > size_b0) {
  69. printf ("## ERROR: "
  70. "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  71. size_b1, size_b1<<20,
  72. size_b0, size_b0<<20
  73. );
  74. flash_info[0].flash_id = FLASH_UNKNOWN;
  75. flash_info[1].flash_id = FLASH_UNKNOWN;
  76. flash_info[0].sector_count = -1;
  77. flash_info[1].sector_count = -1;
  78. flash_info[0].size = 0;
  79. flash_info[1].size = 0;
  80. return (0);
  81. }
  82. #else
  83. size_b1 = 0;
  84. #endif
  85. /* Remap FLASH according to real size */
  86. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  87. #ifdef CONFIG_MVS_16BIT_FLASH
  88. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
  89. #else
  90. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_MS_GPCM | BR_V;
  91. #endif
  92. /* Re-do sizing to get full correct info */
  93. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  94. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  95. /* monitor protection ON by default */
  96. flash_protect(FLAG_PROTECT_SET,
  97. CFG_FLASH_BASE,
  98. CFG_FLASH_BASE+monitor_flash_len-1,
  99. &flash_info[0]);
  100. if (size_b1) {
  101. memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
  102. #ifdef CONFIG_MVS_16BIT_FLASH
  103. memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
  104. BR_PS_16 | BR_MS_GPCM | BR_V;
  105. #else
  106. memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
  107. BR_PS_32 | BR_MS_GPCM | BR_V;
  108. #endif
  109. /* Re-do sizing to get full correct info */
  110. size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
  111. &flash_info[1]);
  112. flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
  113. /* monitor protection ON by default */
  114. flash_protect(FLAG_PROTECT_SET,
  115. CFG_FLASH_BASE,
  116. CFG_FLASH_BASE+monitor_flash_len-1,
  117. &flash_info[1]);
  118. } else {
  119. memctl->memc_br1 = 0; /* invalidate bank */
  120. flash_info[1].flash_id = FLASH_UNKNOWN;
  121. flash_info[1].sector_count = -1;
  122. }
  123. flash_info[0].size = size_b0;
  124. flash_info[1].size = size_b1;
  125. return (size_b0 + size_b1);
  126. }
  127. /*-----------------------------------------------------------------------
  128. */
  129. static void flash_get_offsets (ulong base, flash_info_t *info)
  130. {
  131. int i;
  132. /* set up sector start address table */
  133. if (info->flash_id & FLASH_BTYPE)
  134. { /* bottom boot sector types - these are the useful ones! */
  135. /* set sector offsets for bottom boot block type */
  136. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B)
  137. { /* AMDLV320B has 8 x 8k bottom boot sectors */
  138. for (i = 0; i < 8; i++) /* +8k */
  139. info->start[i] = base + (i * (0x00002000 << FLASH_SHIFT));
  140. for (; i < info->sector_count; i++) /* +64k */
  141. info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT)) - (0x00070000 << FLASH_SHIFT);
  142. }
  143. else
  144. { /* other types have 4 bottom boot sectors (16,8,8,32) */
  145. i = 0;
  146. info->start[i++] = base + 0x00000000; /* - */
  147. info->start[i++] = base + (0x00004000 << FLASH_SHIFT); /* +16k */
  148. info->start[i++] = base + (0x00006000 << FLASH_SHIFT); /* +8k */
  149. info->start[i++] = base + (0x00008000 << FLASH_SHIFT); /* +8k */
  150. info->start[i++] = base + (0x00010000 << FLASH_SHIFT); /* +32k */
  151. for (; i < info->sector_count; i++) /* +64k */
  152. info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT)) - (0x00030000 << FLASH_SHIFT);
  153. }
  154. }
  155. else
  156. { /* top boot sector types - not so useful */
  157. /* set sector offsets for top boot block type */
  158. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T)
  159. { /* AMDLV320T has 8 x 8k top boot sectors */
  160. for (i = 0; i < info->sector_count - 8; i++) /* +64k */
  161. info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT));
  162. for (; i < info->sector_count; i++) /* +8k */
  163. info->start[i] = base + (i * (0x00002000 << FLASH_SHIFT));
  164. }
  165. else
  166. { /* other types have 4 top boot sectors (32,8,8,16) */
  167. for (i = 0; i < info->sector_count - 4; i++) /* +64k */
  168. info->start[i] = base + (i * (0x00010000 << FLASH_SHIFT));
  169. info->start[i++] = base + info->size - (0x00010000 << FLASH_SHIFT); /* -32k */
  170. info->start[i++] = base + info->size - (0x00008000 << FLASH_SHIFT); /* -8k */
  171. info->start[i++] = base + info->size - (0x00006000 << FLASH_SHIFT); /* -8k */
  172. info->start[i] = base + info->size - (0x00004000 << FLASH_SHIFT); /* -16k */
  173. }
  174. }
  175. }
  176. /*-----------------------------------------------------------------------
  177. */
  178. void flash_print_info (flash_info_t *info)
  179. {
  180. int i;
  181. if (info->flash_id == FLASH_UNKNOWN) {
  182. printf ("missing or unknown FLASH type\n");
  183. return;
  184. }
  185. switch (info->flash_id & FLASH_VENDMASK) {
  186. case FLASH_MAN_AMD: printf ("AMD "); break;
  187. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  188. case FLASH_MAN_STM: printf ("ST "); break;
  189. default: printf ("Unknown Vendor "); break;
  190. }
  191. switch (info->flash_id & FLASH_TYPEMASK) {
  192. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  193. break;
  194. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  195. break;
  196. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  197. break;
  198. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  199. break;
  200. case FLASH_STMW320DB: printf ("M29W320B (32 Mbit, bottom boot sect)\n");
  201. break;
  202. case FLASH_STMW320DT: printf ("M29W320T (32 Mbit, top boot sector)\n");
  203. break;
  204. default: printf ("Unknown Chip Type\n");
  205. break;
  206. }
  207. printf (" Size: %ld MB in %d Sectors\n",
  208. info->size >> 20, info->sector_count);
  209. printf (" Sector Start Addresses:");
  210. for (i=0; i<info->sector_count; ++i) {
  211. if ((i % 5) == 0)
  212. printf ("\n ");
  213. printf (" %08lX%s",
  214. info->start[i],
  215. info->protect[i] ? " (RO)" : " "
  216. );
  217. }
  218. printf ("\n");
  219. }
  220. /*-----------------------------------------------------------------------
  221. */
  222. /*-----------------------------------------------------------------------
  223. */
  224. /*
  225. * The following code cannot be run from FLASH!
  226. */
  227. #define AMD_ID_LV160T_MVS (AMD_ID_LV160T & FLASH_DATA_MASK)
  228. #define AMD_ID_LV160B_MVS (AMD_ID_LV160B & FLASH_DATA_MASK)
  229. #define AMD_ID_LV320T_MVS (AMD_ID_LV320T & FLASH_DATA_MASK)
  230. #define AMD_ID_LV320B_MVS (AMD_ID_LV320B & FLASH_DATA_MASK)
  231. #define STM_ID_W320DT_MVS (STM_ID_29W320DT & FLASH_DATA_MASK)
  232. #define STM_ID_W320DB_MVS (STM_ID_29W320DB & FLASH_DATA_MASK)
  233. #define AMD_MANUFACT_MVS (AMD_MANUFACT & FLASH_DATA_MASK)
  234. #define FUJ_MANUFACT_MVS (FUJ_MANUFACT & FLASH_DATA_MASK)
  235. #define STM_MANUFACT_MVS (STM_MANUFACT & FLASH_DATA_MASK)
  236. #define AUTOSELECT_ADDR1 0x0555
  237. #define AUTOSELECT_ADDR2 0x02AA
  238. #define AUTOSELECT_ADDR3 AUTOSELECT_ADDR1
  239. #define AUTOSELECT_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
  240. #define AUTOSELECT_DATA2 (0x00550055 & FLASH_DATA_MASK)
  241. #define AUTOSELECT_DATA3 (0x00900090 & FLASH_DATA_MASK)
  242. #define RESET_BANK_DATA (0x00F000F0 & FLASH_DATA_MASK)
  243. static ulong flash_get_size (vu_long *address, flash_info_t *info)
  244. {
  245. short i;
  246. #ifdef CONFIG_MVS_16BIT_FLASH
  247. ushort value;
  248. vu_short *addr = (vu_short *)address;
  249. #else
  250. ulong value;
  251. vu_long *addr = (vu_long *)address;
  252. #endif
  253. ulong base = (ulong)address;
  254. /* Write auto select command: read Manufacturer ID */
  255. addr[AUTOSELECT_ADDR1] = AUTOSELECT_DATA1;
  256. addr[AUTOSELECT_ADDR2] = AUTOSELECT_DATA2;
  257. addr[AUTOSELECT_ADDR3] = AUTOSELECT_DATA3;
  258. value = addr[0]; /* manufacturer ID */
  259. switch (value) {
  260. case AMD_MANUFACT_MVS:
  261. info->flash_id = FLASH_MAN_AMD;
  262. break;
  263. case FUJ_MANUFACT_MVS:
  264. info->flash_id = FLASH_MAN_FUJ;
  265. break;
  266. case STM_MANUFACT_MVS:
  267. info->flash_id = FLASH_MAN_STM;
  268. break;
  269. default:
  270. info->flash_id = FLASH_UNKNOWN;
  271. info->sector_count = 0;
  272. info->size = 0;
  273. return (0); /* no or unknown flash */
  274. }
  275. value = addr[1]; /* device ID */
  276. switch (value) {
  277. case AMD_ID_LV160T_MVS:
  278. info->flash_id += FLASH_AM160T;
  279. info->sector_count = 37;
  280. info->size = (0x00200000 << FLASH_SHIFT);
  281. break; /* => 2 or 4 MB */
  282. case AMD_ID_LV160B_MVS:
  283. info->flash_id += FLASH_AM160B;
  284. info->sector_count = 37;
  285. info->size = (0x00200000 << FLASH_SHIFT);
  286. break; /* => 2 or 4 MB */
  287. case AMD_ID_LV320T_MVS:
  288. info->flash_id += FLASH_AM320T;
  289. info->sector_count = 71;
  290. info->size = (0x00400000 << FLASH_SHIFT);
  291. break; /* => 4 or 8 MB */
  292. case AMD_ID_LV320B_MVS:
  293. info->flash_id += FLASH_AM320B;
  294. info->sector_count = 71;
  295. info->size = (0x00400000 << FLASH_SHIFT);
  296. break; /* => 4 or 8MB */
  297. case STM_ID_W320DT_MVS:
  298. info->flash_id += FLASH_STMW320DT;
  299. info->sector_count = 67;
  300. info->size = (0x00400000 << FLASH_SHIFT);
  301. break; /* => 4 or 8 MB */
  302. case STM_ID_W320DB_MVS:
  303. info->flash_id += FLASH_STMW320DB;
  304. info->sector_count = 67;
  305. info->size = (0x00400000 << FLASH_SHIFT);
  306. break; /* => 4 or 8MB */
  307. default:
  308. info->flash_id = FLASH_UNKNOWN;
  309. return (0); /* => no or unknown flash */
  310. }
  311. /* set up sector start address table */
  312. flash_get_offsets (base, info);
  313. /* check for protected sectors */
  314. for (i = 0; i < info->sector_count; i++) {
  315. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  316. /* D0 = 1 if protected */
  317. #ifdef CONFIG_MVS_16BIT_FLASH
  318. addr = (vu_short *)(info->start[i]);
  319. #else
  320. addr = (vu_long *)(info->start[i]);
  321. #endif
  322. info->protect[i] = addr[2] & 1;
  323. }
  324. /*
  325. * Prevent writes to uninitialized FLASH.
  326. */
  327. if (info->flash_id != FLASH_UNKNOWN) {
  328. #ifdef CONFIG_MVS_16BIT_FLASH
  329. addr = (vu_short *)info->start[0];
  330. #else
  331. addr = (vu_long *)info->start[0];
  332. #endif
  333. *addr = RESET_BANK_DATA; /* reset bank */
  334. }
  335. return (info->size);
  336. }
  337. /*-----------------------------------------------------------------------
  338. */
  339. #define ERASE_ADDR1 0x0555
  340. #define ERASE_ADDR2 0x02AA
  341. #define ERASE_ADDR3 ERASE_ADDR1
  342. #define ERASE_ADDR4 ERASE_ADDR1
  343. #define ERASE_ADDR5 ERASE_ADDR2
  344. #define ERASE_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
  345. #define ERASE_DATA2 (0x00550055 & FLASH_DATA_MASK)
  346. #define ERASE_DATA3 (0x00800080 & FLASH_DATA_MASK)
  347. #define ERASE_DATA4 ERASE_DATA1
  348. #define ERASE_DATA5 ERASE_DATA2
  349. #define ERASE_SECTOR_DATA (0x00300030 & FLASH_DATA_MASK)
  350. #define ERASE_CHIP_DATA (0x00100010 & FLASH_DATA_MASK)
  351. #define ERASE_CONFIRM_DATA (0x00800080 & FLASH_DATA_MASK)
  352. int flash_erase (flash_info_t *info, int s_first, int s_last)
  353. {
  354. #ifdef CONFIG_MVS_16BIT_FLASH
  355. vu_short *addr = (vu_short *)(info->start[0]);
  356. #else
  357. vu_long *addr = (vu_long *)(info->start[0]);
  358. #endif
  359. int flag, prot, sect, l_sect;
  360. ulong start, now, last;
  361. if ((s_first < 0) || (s_first > s_last)) {
  362. if (info->flash_id == FLASH_UNKNOWN) {
  363. printf ("- missing\n");
  364. } else {
  365. printf ("- no sectors to erase\n");
  366. }
  367. return 1;
  368. }
  369. if ((info->flash_id == FLASH_UNKNOWN) ||
  370. (info->flash_id > FLASH_AMD_COMP)) {
  371. printf ("Can't erase unknown flash type %08lx - aborted\n",
  372. info->flash_id);
  373. return 1;
  374. }
  375. prot = 0;
  376. for (sect=s_first; sect<=s_last; ++sect) {
  377. if (info->protect[sect]) {
  378. prot++;
  379. }
  380. }
  381. if (prot) {
  382. printf ("- Warning: %d protected sectors will not be erased!\n",
  383. prot);
  384. } else {
  385. printf ("\n");
  386. }
  387. l_sect = -1;
  388. /* Disable interrupts which might cause a timeout here */
  389. flag = disable_interrupts();
  390. addr[ERASE_ADDR1] = ERASE_DATA1;
  391. addr[ERASE_ADDR2] = ERASE_DATA2;
  392. addr[ERASE_ADDR3] = ERASE_DATA3;
  393. addr[ERASE_ADDR4] = ERASE_DATA4;
  394. addr[ERASE_ADDR5] = ERASE_DATA5;
  395. /* Start erase on unprotected sectors */
  396. for (sect = s_first; sect<=s_last; sect++) {
  397. if (info->protect[sect] == 0) { /* not protected */
  398. #ifdef CONFIG_MVS_16BIT_FLASH
  399. addr = (vu_short *)(info->start[sect]);
  400. #else
  401. addr = (vu_long *)(info->start[sect]);
  402. #endif
  403. addr[0] = ERASE_SECTOR_DATA;
  404. l_sect = sect;
  405. }
  406. }
  407. /* re-enable interrupts if necessary */
  408. if (flag)
  409. enable_interrupts();
  410. /* wait at least 80us - let's wait 1 ms */
  411. udelay (1000);
  412. /*
  413. * We wait for the last triggered sector
  414. */
  415. if (l_sect < 0)
  416. goto DONE;
  417. start = get_timer (0);
  418. last = start;
  419. #ifdef CONFIG_MVS_16BIT_FLASH
  420. addr = (vu_short *)(info->start[l_sect]);
  421. #else
  422. addr = (vu_long *)(info->start[l_sect]);
  423. #endif
  424. while ((addr[0] & ERASE_CONFIRM_DATA) != ERASE_CONFIRM_DATA) {
  425. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  426. printf ("Timeout\n");
  427. return 1;
  428. }
  429. /* show that we're waiting */
  430. if ((now - last) > 1000) { /* every second */
  431. putc ('.');
  432. last = now;
  433. }
  434. }
  435. DONE:
  436. /* reset to read mode */
  437. #ifdef CONFIG_MVS_16BIT_FLASH
  438. addr = (vu_short *)info->start[0];
  439. #else
  440. addr = (vu_long *)info->start[0];
  441. #endif
  442. addr[0] = RESET_BANK_DATA; /* reset bank */
  443. printf (" done\n");
  444. return 0;
  445. }
  446. /*-----------------------------------------------------------------------
  447. * Copy memory to flash, returns:
  448. * 0 - OK
  449. * 1 - write timeout
  450. * 2 - Flash not erased
  451. */
  452. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  453. {
  454. #define BUFF_INC 4
  455. ulong cp, wp, data;
  456. int i, l, rc;
  457. mvdebug (("+write_buff %p ==> 0x%08lx, count = 0x%08lx\n", src, addr, cnt));
  458. wp = (addr & ~3); /* get lower word aligned address */
  459. /*
  460. * handle unaligned start bytes
  461. */
  462. if ((l = addr - wp) != 0) {
  463. mvdebug ((" handle unaligned start bytes (cnt = 0x%08%lx)\n", cnt));
  464. data = 0;
  465. for (i=0, cp=wp; i<l; ++i, ++cp) {
  466. data = (data << 8) | (*(uchar *)cp);
  467. }
  468. for (; i<BUFF_INC && cnt>0; ++i) {
  469. data = (data << 8) | *src++;
  470. --cnt;
  471. ++cp;
  472. }
  473. for (; cnt==0 && i<BUFF_INC; ++i, ++cp) {
  474. data = (data << 8) | (*(uchar *)cp);
  475. }
  476. if ((rc = write_word(info, wp, data)) != 0) {
  477. return (rc);
  478. }
  479. wp += BUFF_INC;
  480. }
  481. /*
  482. * handle (half)word aligned part
  483. */
  484. mvdebug ((" handle word aligned part (cnt = 0x%08%lx)\n", cnt));
  485. while (cnt >= BUFF_INC) {
  486. data = 0;
  487. for (i=0; i<BUFF_INC; ++i) {
  488. data = (data << 8) | *src++;
  489. }
  490. if ((rc = write_word(info, wp, data)) != 0) {
  491. return (rc);
  492. }
  493. wp += BUFF_INC;
  494. cnt -= BUFF_INC;
  495. }
  496. if (cnt == 0) {
  497. return (0);
  498. }
  499. /*
  500. * handle unaligned tail bytes
  501. */
  502. mvdebug ((" handle unaligned tail bytes (cnt = 0x%08%lx)\n", cnt));
  503. data = 0;
  504. for (i=0, cp=wp; i<BUFF_INC && cnt>0; ++i, ++cp) {
  505. data = (data << 8) | *src++;
  506. --cnt;
  507. }
  508. for (; i<BUFF_INC; ++i, ++cp) {
  509. data = (data << 8) | (*(uchar *)cp);
  510. }
  511. return (write_word(info, wp, data));
  512. }
  513. #define WRITE_ADDR1 0x0555
  514. #define WRITE_ADDR2 0x02AA
  515. #define WRITE_ADDR3 WRITE_ADDR1
  516. #define WRITE_DATA1 (0x00AA00AA & FLASH_DATA_MASK)
  517. #define WRITE_DATA2 (0x00550055 & FLASH_DATA_MASK)
  518. #define WRITE_DATA3 (0x00A000A0 & FLASH_DATA_MASK)
  519. #define WRITE_CONFIRM_DATA ERASE_CONFIRM_DATA
  520. #ifndef CONFIG_MVS_16BIT_FLASH
  521. /*-----------------------------------------------------------------------
  522. * Write a word to Flash, returns:
  523. * 0 - OK
  524. * 1 - write timeout
  525. * 2 - Flash not erased
  526. */
  527. static int write_word (flash_info_t *info, ulong dest, ulong data)
  528. {
  529. vu_long *addr = (vu_long *)(info->start[0]);
  530. ulong start;
  531. int flag;
  532. mvdebug (("+write_word (to 0x%08lx)\n", dest));
  533. /* Check if Flash is (sufficiently) erased */
  534. if ((*((vu_long *)dest) & data) != data) {
  535. return (2);
  536. }
  537. /* Disable interrupts which might cause a timeout here */
  538. flag = disable_interrupts();
  539. addr[WRITE_ADDR1] = WRITE_DATA1;
  540. addr[WRITE_ADDR2] = WRITE_DATA2;
  541. addr[WRITE_ADDR3] = WRITE_DATA3;
  542. *((vu_long *)dest) = data;
  543. /* re-enable interrupts if necessary */
  544. if (flag)
  545. enable_interrupts();
  546. /* data polling for D7 */
  547. start = get_timer (0);
  548. addr = (vu_long *)dest;
  549. while ((*addr & WRITE_CONFIRM_DATA) != (data & WRITE_CONFIRM_DATA)) {
  550. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  551. return (1);
  552. }
  553. }
  554. mvdebug (("-write_word\n"));
  555. return (0);
  556. }
  557. #else /* CONFIG_MVS_16BIT_FLASH */
  558. /*-----------------------------------------------------------------------
  559. * Write a halfword to Flash, returns:
  560. * 0 - OK
  561. * 1 - write timeout
  562. * 2 - Flash not erased
  563. */
  564. static int write_halfword (flash_info_t *info, ulong dest, ushort data)
  565. {
  566. vu_short *addr = (vu_short *)(info->start[0]);
  567. ulong start;
  568. int flag;
  569. mvdebug (("+write_halfword (to 0x%08lx)\n", dest));
  570. /* Check if Flash is (sufficiently) erased */
  571. if ((*((vu_short *)dest) & data) != data) {
  572. return (2);
  573. }
  574. /* Disable interrupts which might cause a timeout here */
  575. flag = disable_interrupts();
  576. addr[WRITE_ADDR1] = WRITE_DATA1;
  577. addr[WRITE_ADDR2] = WRITE_DATA2;
  578. addr[WRITE_ADDR3] = WRITE_DATA3;
  579. *((vu_short *)dest) = data;
  580. /* re-enable interrupts if necessary */
  581. if (flag)
  582. enable_interrupts();
  583. /* data polling for D7 */
  584. start = get_timer (0);
  585. addr = (vu_short *)dest;
  586. while ((*addr & WRITE_CONFIRM_DATA) != (data & WRITE_CONFIRM_DATA)) {
  587. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  588. return (1);
  589. }
  590. }
  591. mvdebug (("-write_halfword\n"));
  592. return (0);
  593. }
  594. /*-----------------------------------------------------------------------
  595. * Write a word to Flash, returns:
  596. * 0 - OK
  597. * 1 - write timeout
  598. * 2 - Flash not erased
  599. */
  600. static int write_word (flash_info_t *info, ulong dest, ulong data)
  601. {
  602. int result = 0;
  603. if (write_halfword (info, dest, (data & ~FLASH_DATA_MASK) >> 16) == 0)
  604. {
  605. dest += 2;
  606. data = data & FLASH_DATA_MASK;
  607. result = write_halfword (info, dest, data);
  608. }
  609. return result;
  610. }
  611. #endif
  612. /*-----------------------------------------------------------------------
  613. */