flash.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. #include <common.h>
  24. #include <ppc4xx.h>
  25. #include <asm/processor.h>
  26. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  27. #ifdef CFG_FLASH_16BIT
  28. #define FLASH_WORD_SIZE unsigned short
  29. #define FLASH_ID_MASK 0xFFFF
  30. #else
  31. #define FLASH_WORD_SIZE unsigned long
  32. #define FLASH_ID_MASK 0xFFFFFFFF
  33. #endif
  34. /*-----------------------------------------------------------------------
  35. * Functions
  36. */
  37. /* stolen from esteem192e/flash.c */
  38. ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info);
  39. #ifndef CFG_FLASH_16BIT
  40. static int write_word (flash_info_t *info, ulong dest, ulong data);
  41. #else
  42. static int write_short (flash_info_t *info, ulong dest, ushort data);
  43. #endif
  44. static void flash_get_offsets (ulong base, flash_info_t *info);
  45. /*-----------------------------------------------------------------------
  46. */
  47. unsigned long flash_init (void)
  48. {
  49. unsigned long size_b0, size_b1;
  50. int i;
  51. uint pbcr;
  52. unsigned long base_b0, base_b1;
  53. /* Init: no FLASHes known */
  54. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  55. flash_info[i].flash_id = FLASH_UNKNOWN;
  56. }
  57. /* Static FLASH Bank configuration here - FIXME XXX */
  58. size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE0_PRELIM, &flash_info[0]);
  59. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  60. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  61. size_b0, size_b0<<20);
  62. }
  63. /* Only one bank */
  64. if (CFG_MAX_FLASH_BANKS == 1)
  65. {
  66. /* Setup offsets */
  67. flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
  68. /* Monitor protection ON by default */
  69. #if 0 /* sand: */
  70. (void)flash_protect(FLAG_PROTECT_SET,
  71. FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
  72. FLASH_BASE0_PRELIM-1+size_b0,
  73. &flash_info[0]);
  74. #else
  75. (void)flash_protect(FLAG_PROTECT_SET,
  76. CFG_MONITOR_BASE,
  77. CFG_MONITOR_BASE+monitor_flash_len-1,
  78. &flash_info[0]);
  79. #endif
  80. size_b1 = 0 ;
  81. flash_info[0].size = size_b0;
  82. }
  83. /* 2 banks */
  84. else
  85. {
  86. size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM, &flash_info[1]);
  87. /* Re-do sizing to get full correct info */
  88. if (size_b1)
  89. {
  90. mtdcr(ebccfga, pb0cr);
  91. pbcr = mfdcr(ebccfgd);
  92. mtdcr(ebccfga, pb0cr);
  93. base_b1 = -size_b1;
  94. pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
  95. mtdcr(ebccfgd, pbcr);
  96. /* printf("pb1cr = %x\n", pbcr); */
  97. }
  98. if (size_b0)
  99. {
  100. mtdcr(ebccfga, pb1cr);
  101. pbcr = mfdcr(ebccfgd);
  102. mtdcr(ebccfga, pb1cr);
  103. base_b0 = base_b1 - size_b0;
  104. pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
  105. mtdcr(ebccfgd, pbcr);
  106. /* printf("pb0cr = %x\n", pbcr); */
  107. }
  108. size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b0, &flash_info[0]);
  109. flash_get_offsets (base_b0, &flash_info[0]);
  110. /* monitor protection ON by default */
  111. #if 0 /* sand: */
  112. (void)flash_protect(FLAG_PROTECT_SET,
  113. FLASH_BASE0_PRELIM-monitor_flash_len+size_b0,
  114. FLASH_BASE0_PRELIM-1+size_b0,
  115. &flash_info[0]);
  116. #else
  117. (void)flash_protect(FLAG_PROTECT_SET,
  118. CFG_MONITOR_BASE,
  119. CFG_MONITOR_BASE+monitor_flash_len-1,
  120. &flash_info[0]);
  121. #endif
  122. if (size_b1) {
  123. /* Re-do sizing to get full correct info */
  124. size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b1, &flash_info[1]);
  125. flash_get_offsets (base_b1, &flash_info[1]);
  126. /* monitor protection ON by default */
  127. (void)flash_protect(FLAG_PROTECT_SET,
  128. base_b1+size_b1-monitor_flash_len,
  129. base_b1+size_b1-1,
  130. &flash_info[1]);
  131. /* monitor protection OFF by default (one is enough) */
  132. (void)flash_protect(FLAG_PROTECT_CLEAR,
  133. base_b0+size_b0-monitor_flash_len,
  134. base_b0+size_b0-1,
  135. &flash_info[0]);
  136. } else {
  137. flash_info[1].flash_id = FLASH_UNKNOWN;
  138. flash_info[1].sector_count = -1;
  139. }
  140. flash_info[0].size = size_b0;
  141. flash_info[1].size = size_b1;
  142. }/* else 2 banks */
  143. return (size_b0 + size_b1);
  144. }
  145. /*-----------------------------------------------------------------------
  146. */
  147. static void flash_get_offsets (ulong base, flash_info_t *info)
  148. {
  149. int i;
  150. /* set up sector start adress table */
  151. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
  152. (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
  153. (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
  154. for (i = 0; i < info->sector_count; i++) {
  155. info->start[i] = base + (i * info->size/info->sector_count);
  156. }
  157. } else if (info->flash_id & FLASH_BTYPE) {
  158. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  159. #ifndef CFG_FLASH_16BIT
  160. /* set sector offsets for bottom boot block type */
  161. info->start[0] = base + 0x00000000;
  162. info->start[1] = base + 0x00004000;
  163. info->start[2] = base + 0x00008000;
  164. info->start[3] = base + 0x0000C000;
  165. info->start[4] = base + 0x00010000;
  166. info->start[5] = base + 0x00014000;
  167. info->start[6] = base + 0x00018000;
  168. info->start[7] = base + 0x0001C000;
  169. for (i = 8; i < info->sector_count; i++) {
  170. info->start[i] = base + (i * 0x00020000) - 0x000E0000;
  171. }
  172. }
  173. else {
  174. /* set sector offsets for bottom boot block type */
  175. info->start[0] = base + 0x00000000;
  176. info->start[1] = base + 0x00008000;
  177. info->start[2] = base + 0x0000C000;
  178. info->start[3] = base + 0x00010000;
  179. for (i = 4; i < info->sector_count; i++) {
  180. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  181. }
  182. }
  183. #else
  184. /* set sector offsets for bottom boot block type */
  185. info->start[0] = base + 0x00000000;
  186. info->start[1] = base + 0x00002000;
  187. info->start[2] = base + 0x00004000;
  188. info->start[3] = base + 0x00006000;
  189. info->start[4] = base + 0x00008000;
  190. info->start[5] = base + 0x0000A000;
  191. info->start[6] = base + 0x0000C000;
  192. info->start[7] = base + 0x0000E000;
  193. for (i = 8; i < info->sector_count; i++) {
  194. info->start[i] = base + (i * 0x00010000) - 0x00070000;
  195. }
  196. }
  197. else {
  198. /* set sector offsets for bottom boot block type */
  199. info->start[0] = base + 0x00000000;
  200. info->start[1] = base + 0x00004000;
  201. info->start[2] = base + 0x00006000;
  202. info->start[3] = base + 0x00008000;
  203. for (i = 4; i < info->sector_count; i++) {
  204. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  205. }
  206. }
  207. #endif
  208. } else {
  209. /* set sector offsets for top boot block type */
  210. i = info->sector_count - 1;
  211. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  212. #ifndef CFG_FLASH_16BIT
  213. info->start[i--] = base + info->size - 0x00004000;
  214. info->start[i--] = base + info->size - 0x00008000;
  215. info->start[i--] = base + info->size - 0x0000C000;
  216. info->start[i--] = base + info->size - 0x00010000;
  217. info->start[i--] = base + info->size - 0x00014000;
  218. info->start[i--] = base + info->size - 0x00018000;
  219. info->start[i--] = base + info->size - 0x0001C000;
  220. for (; i >= 0; i--) {
  221. info->start[i] = base + i * 0x00020000;
  222. }
  223. } else {
  224. info->start[i--] = base + info->size - 0x00008000;
  225. info->start[i--] = base + info->size - 0x0000C000;
  226. info->start[i--] = base + info->size - 0x00010000;
  227. for (; i >= 0; i--) {
  228. info->start[i] = base + i * 0x00020000;
  229. }
  230. }
  231. #else
  232. info->start[i--] = base + info->size - 0x00002000;
  233. info->start[i--] = base + info->size - 0x00004000;
  234. info->start[i--] = base + info->size - 0x00006000;
  235. info->start[i--] = base + info->size - 0x00008000;
  236. info->start[i--] = base + info->size - 0x0000A000;
  237. info->start[i--] = base + info->size - 0x0000C000;
  238. info->start[i--] = base + info->size - 0x0000E000;
  239. for (; i >= 0; i--) {
  240. info->start[i] = base + i * 0x00010000;
  241. }
  242. } else {
  243. info->start[i--] = base + info->size - 0x00004000;
  244. info->start[i--] = base + info->size - 0x00006000;
  245. info->start[i--] = base + info->size - 0x00008000;
  246. for (; i >= 0; i--) {
  247. info->start[i] = base + i * 0x00010000;
  248. }
  249. }
  250. #endif
  251. }
  252. }
  253. /*-----------------------------------------------------------------------
  254. */
  255. void flash_print_info (flash_info_t *info)
  256. {
  257. int i;
  258. uchar *boottype;
  259. uchar botboot[]=", bottom boot sect)\n";
  260. uchar topboot[]=", top boot sector)\n";
  261. if (info->flash_id == FLASH_UNKNOWN) {
  262. printf ("missing or unknown FLASH type\n");
  263. return;
  264. }
  265. switch (info->flash_id & FLASH_VENDMASK) {
  266. case FLASH_MAN_AMD: printf ("AMD "); break;
  267. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  268. case FLASH_MAN_SST: printf ("SST "); break;
  269. case FLASH_MAN_STM: printf ("STM "); break;
  270. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  271. default: printf ("Unknown Vendor "); break;
  272. }
  273. if (info->flash_id & 0x0001 ) {
  274. boottype = botboot;
  275. } else {
  276. boottype = topboot;
  277. }
  278. switch (info->flash_id & FLASH_TYPEMASK) {
  279. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit%s",boottype);
  280. break;
  281. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit%s",boottype);
  282. break;
  283. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit%s",boottype);
  284. break;
  285. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit%s",boottype);
  286. break;
  287. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit%s",boottype);
  288. break;
  289. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit%s",boottype);
  290. break;
  291. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit%s",boottype);
  292. break;
  293. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit%s",boottype);
  294. break;
  295. case FLASH_INTEL800B: printf ("INTEL28F800B (8 Mbit%s",boottype);
  296. break;
  297. case FLASH_INTEL800T: printf ("INTEL28F800T (8 Mbit%s",boottype);
  298. break;
  299. case FLASH_INTEL160B: printf ("INTEL28F160B (16 Mbit%s",boottype);
  300. break;
  301. case FLASH_INTEL160T: printf ("INTEL28F160T (16 Mbit%s",boottype);
  302. break;
  303. case FLASH_INTEL320B: printf ("INTEL28F320B (32 Mbit%s",boottype);
  304. break;
  305. case FLASH_INTEL320T: printf ("INTEL28F320T (32 Mbit%s",boottype);
  306. break;
  307. #if 0 /* enable when devices are available */
  308. case FLASH_INTEL640B: printf ("INTEL28F640B (64 Mbit%s",boottype);
  309. break;
  310. case FLASH_INTEL640T: printf ("INTEL28F640T (64 Mbit%s",boottype);
  311. break;
  312. #endif
  313. case FLASH_28F320J3A: printf ("INTEL28F320J3A (32 Mbit%s",boottype);
  314. break;
  315. case FLASH_28F640J3A: printf ("INTEL28F640J3A (64 Mbit%s",boottype);
  316. break;
  317. case FLASH_28F128J3A: printf ("INTEL28F128J3A (128 Mbit%s",boottype);
  318. break;
  319. default: printf ("Unknown Chip Type\n");
  320. break;
  321. }
  322. printf (" Size: %ld MB in %d Sectors\n",
  323. info->size >> 20, info->sector_count);
  324. printf (" Sector Start Addresses:");
  325. for (i=0; i<info->sector_count; ++i) {
  326. if ((i % 5) == 0)
  327. printf ("\n ");
  328. printf (" %08lX%s",
  329. info->start[i],
  330. info->protect[i] ? " (RO)" : " "
  331. );
  332. }
  333. printf ("\n");
  334. return;
  335. }
  336. /*-----------------------------------------------------------------------
  337. */
  338. /*-----------------------------------------------------------------------
  339. */
  340. /*
  341. * The following code cannot be run from FLASH!
  342. */
  343. ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
  344. {
  345. short i;
  346. ulong base = (ulong)addr;
  347. FLASH_WORD_SIZE value;
  348. /* Write auto select command: read Manufacturer ID */
  349. #ifndef CFG_FLASH_16BIT
  350. /*
  351. * Note: if it is an AMD flash and the word at addr[0000]
  352. * is 0x00890089 this routine will think it is an Intel
  353. * flash device and may(most likely) cause trouble.
  354. */
  355. addr[0x0000] = 0x00900090;
  356. if(addr[0x0000] != 0x00890089){
  357. addr[0x0555] = 0x00AA00AA;
  358. addr[0x02AA] = 0x00550055;
  359. addr[0x0555] = 0x00900090;
  360. #else
  361. /*
  362. * Note: if it is an AMD flash and the word at addr[0000]
  363. * is 0x0089 this routine will think it is an Intel
  364. * flash device and may(most likely) cause trouble.
  365. */
  366. addr[0x0000] = 0x0090;
  367. if(addr[0x0000] != 0x0089){
  368. addr[0x0555] = 0x00AA;
  369. addr[0x02AA] = 0x0055;
  370. addr[0x0555] = 0x0090;
  371. #endif
  372. }
  373. value = addr[0];
  374. switch (value) {
  375. case (AMD_MANUFACT & FLASH_ID_MASK):
  376. info->flash_id = FLASH_MAN_AMD;
  377. break;
  378. case (FUJ_MANUFACT & FLASH_ID_MASK):
  379. info->flash_id = FLASH_MAN_FUJ;
  380. break;
  381. case (STM_MANUFACT & FLASH_ID_MASK):
  382. info->flash_id = FLASH_MAN_STM;
  383. break;
  384. case (SST_MANUFACT & FLASH_ID_MASK):
  385. info->flash_id = FLASH_MAN_SST;
  386. break;
  387. case (INTEL_MANUFACT & FLASH_ID_MASK):
  388. info->flash_id = FLASH_MAN_INTEL;
  389. break;
  390. default:
  391. info->flash_id = FLASH_UNKNOWN;
  392. info->sector_count = 0;
  393. info->size = 0;
  394. return (0); /* no or unknown flash */
  395. }
  396. value = addr[1]; /* device ID */
  397. switch (value) {
  398. case (AMD_ID_LV400T & FLASH_ID_MASK):
  399. info->flash_id += FLASH_AM400T;
  400. info->sector_count = 11;
  401. info->size = 0x00100000;
  402. break; /* => 1 MB */
  403. case (AMD_ID_LV400B & FLASH_ID_MASK):
  404. info->flash_id += FLASH_AM400B;
  405. info->sector_count = 11;
  406. info->size = 0x00100000;
  407. break; /* => 1 MB */
  408. case (AMD_ID_LV800T & FLASH_ID_MASK):
  409. info->flash_id += FLASH_AM800T;
  410. info->sector_count = 19;
  411. info->size = 0x00200000;
  412. break; /* => 2 MB */
  413. case (AMD_ID_LV800B & FLASH_ID_MASK):
  414. info->flash_id += FLASH_AM800B;
  415. info->sector_count = 19;
  416. info->size = 0x00200000;
  417. break; /* => 2 MB */
  418. case (AMD_ID_LV160T & FLASH_ID_MASK):
  419. info->flash_id += FLASH_AM160T;
  420. info->sector_count = 35;
  421. info->size = 0x00400000;
  422. break; /* => 4 MB */
  423. case (AMD_ID_LV160B & FLASH_ID_MASK):
  424. info->flash_id += FLASH_AM160B;
  425. info->sector_count = 35;
  426. info->size = 0x00400000;
  427. break; /* => 4 MB */
  428. #if 0 /* enable when device IDs are available */
  429. case (AMD_ID_LV320T & FLASH_ID_MASK):
  430. info->flash_id += FLASH_AM320T;
  431. info->sector_count = 67;
  432. info->size = 0x00800000;
  433. break; /* => 8 MB */
  434. case (AMD_ID_LV320B & FLASH_ID_MASK):
  435. info->flash_id += FLASH_AM320B;
  436. info->sector_count = 67;
  437. info->size = 0x00800000;
  438. break; /* => 8 MB */
  439. #endif
  440. case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
  441. info->flash_id += FLASH_INTEL800T;
  442. info->sector_count = 23;
  443. info->size = 0x00200000;
  444. break; /* => 2 MB */
  445. case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
  446. info->flash_id += FLASH_INTEL800B;
  447. info->sector_count = 23;
  448. info->size = 0x00200000;
  449. break; /* => 2 MB */
  450. case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
  451. info->flash_id += FLASH_INTEL160T;
  452. info->sector_count = 39;
  453. info->size = 0x00400000;
  454. break; /* => 4 MB */
  455. case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
  456. info->flash_id += FLASH_INTEL160B;
  457. info->sector_count = 39;
  458. info->size = 0x00400000;
  459. break; /* => 4 MB */
  460. case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
  461. info->flash_id += FLASH_INTEL320T;
  462. info->sector_count = 71;
  463. info->size = 0x00800000;
  464. break; /* => 8 MB */
  465. case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
  466. info->flash_id += FLASH_AM320B;
  467. info->sector_count = 71;
  468. info->size = 0x00800000;
  469. break; /* => 8 MB */
  470. #if 0 /* enable when devices are available */
  471. case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
  472. info->flash_id += FLASH_INTEL320T;
  473. info->sector_count = 135;
  474. info->size = 0x01000000;
  475. break; /* => 16 MB */
  476. case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
  477. info->flash_id += FLASH_AM320B;
  478. info->sector_count = 135;
  479. info->size = 0x01000000;
  480. break; /* => 16 MB */
  481. #endif
  482. case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
  483. info->flash_id += FLASH_28F320J3A;
  484. info->sector_count = 32;
  485. info->size = 0x00400000;
  486. break; /* => 32 MBit */
  487. case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
  488. info->flash_id += FLASH_28F640J3A;
  489. info->sector_count = 64;
  490. info->size = 0x00800000;
  491. break; /* => 64 MBit */
  492. case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
  493. info->flash_id += FLASH_28F128J3A;
  494. info->sector_count = 128;
  495. info->size = 0x01000000;
  496. break; /* => 128 MBit */
  497. default:
  498. /* FIXME*/
  499. info->flash_id = FLASH_UNKNOWN;
  500. return (0); /* => no or unknown flash */
  501. }
  502. flash_get_offsets(base, info);
  503. /* check for protected sectors */
  504. for (i = 0; i < info->sector_count; i++) {
  505. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  506. /* D0 = 1 if protected */
  507. addr = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  508. info->protect[i] = addr[2] & 1;
  509. }
  510. /*
  511. * Prevent writes to uninitialized FLASH.
  512. */
  513. if (info->flash_id != FLASH_UNKNOWN) {
  514. addr = (volatile FLASH_WORD_SIZE *)info->start[0];
  515. if( (info->flash_id & 0xFF00) == FLASH_MAN_INTEL){
  516. *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
  517. } else {
  518. *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
  519. }
  520. }
  521. return (info->size);
  522. }
  523. /*-----------------------------------------------------------------------
  524. */
  525. int flash_erase (flash_info_t *info, int s_first, int s_last)
  526. {
  527. volatile FLASH_WORD_SIZE *addr=(volatile FLASH_WORD_SIZE*)(info->start[0]);
  528. int flag, prot, sect, l_sect, barf;
  529. ulong start, now, last;
  530. int rcode = 0;
  531. if ((s_first < 0) || (s_first > s_last)) {
  532. if (info->flash_id == FLASH_UNKNOWN) {
  533. printf ("- missing\n");
  534. } else {
  535. printf ("- no sectors to erase\n");
  536. }
  537. return 1;
  538. }
  539. if ((info->flash_id == FLASH_UNKNOWN) ||
  540. ((info->flash_id > FLASH_AMD_COMP) &&
  541. ( (info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL ) ) ){
  542. printf ("Can't erase unknown flash type - aborted\n");
  543. return 1;
  544. }
  545. prot = 0;
  546. for (sect=s_first; sect<=s_last; ++sect) {
  547. if (info->protect[sect]) {
  548. prot++;
  549. }
  550. }
  551. if (prot) {
  552. printf ("- Warning: %d protected sectors will not be erased!\n",
  553. prot);
  554. } else {
  555. printf ("\n");
  556. }
  557. l_sect = -1;
  558. /* Disable interrupts which might cause a timeout here */
  559. flag = disable_interrupts();
  560. if(info->flash_id < FLASH_AMD_COMP) {
  561. #ifndef CFG_FLASH_16BIT
  562. addr[0x0555] = 0x00AA00AA;
  563. addr[0x02AA] = 0x00550055;
  564. addr[0x0555] = 0x00800080;
  565. addr[0x0555] = 0x00AA00AA;
  566. addr[0x02AA] = 0x00550055;
  567. #else
  568. addr[0x0555] = 0x00AA;
  569. addr[0x02AA] = 0x0055;
  570. addr[0x0555] = 0x0080;
  571. addr[0x0555] = 0x00AA;
  572. addr[0x02AA] = 0x0055;
  573. #endif
  574. /* Start erase on unprotected sectors */
  575. for (sect = s_first; sect<=s_last; sect++) {
  576. if (info->protect[sect] == 0) { /* not protected */
  577. addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
  578. addr[0] = (0x00300030 & FLASH_ID_MASK);
  579. l_sect = sect;
  580. }
  581. }
  582. /* re-enable interrupts if necessary */
  583. if (flag)
  584. enable_interrupts();
  585. /* wait at least 80us - let's wait 1 ms */
  586. udelay (1000);
  587. /*
  588. * We wait for the last triggered sector
  589. */
  590. if (l_sect < 0)
  591. goto DONE;
  592. start = get_timer (0);
  593. last = start;
  594. addr = (volatile FLASH_WORD_SIZE*)(info->start[l_sect]);
  595. while ((addr[0] & (0x00800080&FLASH_ID_MASK)) !=
  596. (0x00800080&FLASH_ID_MASK) )
  597. {
  598. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  599. printf ("Timeout\n");
  600. return 1;
  601. }
  602. /* show that we're waiting */
  603. if ((now - last) > 1000) { /* every second */
  604. serial_putc ('.');
  605. last = now;
  606. }
  607. }
  608. DONE:
  609. /* reset to read mode */
  610. addr = (volatile FLASH_WORD_SIZE *)info->start[0];
  611. addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
  612. } else {
  613. for (sect = s_first; sect<=s_last; sect++) {
  614. if (info->protect[sect] == 0) { /* not protected */
  615. barf = 0;
  616. #ifndef CFG_FLASH_16BIT
  617. addr = (vu_long*)(info->start[sect]);
  618. addr[0] = 0x00200020;
  619. addr[0] = 0x00D000D0;
  620. while(!(addr[0] & 0x00800080)); /* wait for error or finish */
  621. if( addr[0] & 0x003A003A) { /* check for error */
  622. barf = addr[0] & 0x003A0000;
  623. if( barf ) {
  624. barf >>=16;
  625. } else {
  626. barf = addr[0] & 0x0000003A;
  627. }
  628. }
  629. #else
  630. addr = (vu_short*)(info->start[sect]);
  631. addr[0] = 0x0020;
  632. addr[0] = 0x00D0;
  633. while(!(addr[0] & 0x0080)); /* wait for error or finish */
  634. if( addr[0] & 0x003A) /* check for error */
  635. barf = addr[0] & 0x003A;
  636. #endif
  637. if(barf) {
  638. printf("\nFlash error in sector at %lx\n",(unsigned long)addr);
  639. if(barf & 0x0002) printf("Block locked, not erased.\n");
  640. if((barf & 0x0030) == 0x0030)
  641. printf("Command Sequence error.\n");
  642. if((barf & 0x0030) == 0x0020)
  643. printf("Block Erase error.\n");
  644. if(barf & 0x0008) printf("Vpp Low error.\n");
  645. rcode = 1;
  646. } else printf(".");
  647. l_sect = sect;
  648. }
  649. addr = (volatile FLASH_WORD_SIZE *)info->start[0];
  650. addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
  651. }
  652. }
  653. printf (" done\n");
  654. return rcode;
  655. }
  656. /*-----------------------------------------------------------------------
  657. */
  658. /*flash_info_t *addr2info (ulong addr)
  659. {
  660. flash_info_t *info;
  661. int i;
  662. for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
  663. if ((addr >= info->start[0]) &&
  664. (addr < (info->start[0] + info->size)) ) {
  665. return (info);
  666. }
  667. }
  668. return (NULL);
  669. }
  670. */
  671. /*-----------------------------------------------------------------------
  672. * Copy memory to flash.
  673. * Make sure all target addresses are within Flash bounds,
  674. * and no protected sectors are hit.
  675. * Returns:
  676. * 0 - OK
  677. * 1 - write timeout
  678. * 2 - Flash not erased
  679. * 4 - target range includes protected sectors
  680. * 8 - target address not in Flash memory
  681. */
  682. /*int flash_write (uchar *src, ulong addr, ulong cnt)
  683. {
  684. int i;
  685. ulong end = addr + cnt - 1;
  686. flash_info_t *info_first = addr2info (addr);
  687. flash_info_t *info_last = addr2info (end );
  688. flash_info_t *info;
  689. if (cnt == 0) {
  690. return (0);
  691. }
  692. if (!info_first || !info_last) {
  693. return (8);
  694. }
  695. for (info = info_first; info <= info_last; ++info) {
  696. ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
  697. /* short s_end = info->sector_count - 1;
  698. for (i=0; i<info->sector_count; ++i) {
  699. ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
  700. if ((end >= info->start[i]) && (addr < e_addr) &&
  701. (info->protect[i] != 0) ) {
  702. return (4);
  703. }
  704. }
  705. }
  706. */ /* finally write data to flash */
  707. /* for (info = info_first; info <= info_last && cnt>0; ++info) {
  708. ulong len;
  709. len = info->start[0] + info->size - addr;
  710. if (len > cnt)
  711. len = cnt;
  712. if ((i = write_buff(info, src, addr, len)) != 0) {
  713. return (i);
  714. }
  715. cnt -= len;
  716. addr += len;
  717. src += len;
  718. }
  719. return (0);
  720. }
  721. */
  722. /*-----------------------------------------------------------------------
  723. * Copy memory to flash, returns:
  724. * 0 - OK
  725. * 1 - write timeout
  726. * 2 - Flash not erased
  727. */
  728. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  729. {
  730. #ifndef CFG_FLASH_16BIT
  731. ulong cp, wp, data;
  732. int l;
  733. #else
  734. ulong cp, wp;
  735. ushort data;
  736. #endif
  737. int i, rc;
  738. #ifndef CFG_FLASH_16BIT
  739. wp = (addr & ~3); /* get lower word aligned address */
  740. /*
  741. * handle unaligned start bytes
  742. */
  743. if ((l = addr - wp) != 0) {
  744. data = 0;
  745. for (i=0, cp=wp; i<l; ++i, ++cp) {
  746. data = (data << 8) | (*(uchar *)cp);
  747. }
  748. for (; i<4 && cnt>0; ++i) {
  749. data = (data << 8) | *src++;
  750. --cnt;
  751. ++cp;
  752. }
  753. for (; cnt==0 && i<4; ++i, ++cp) {
  754. data = (data << 8) | (*(uchar *)cp);
  755. }
  756. if ((rc = write_word(info, wp, data)) != 0) {
  757. return (rc);
  758. }
  759. wp += 4;
  760. }
  761. /*
  762. * handle word aligned part
  763. */
  764. while (cnt >= 4) {
  765. data = 0;
  766. for (i=0; i<4; ++i) {
  767. data = (data << 8) | *src++;
  768. }
  769. if ((rc = write_word(info, wp, data)) != 0) {
  770. return (rc);
  771. }
  772. wp += 4;
  773. cnt -= 4;
  774. }
  775. if (cnt == 0) {
  776. return (0);
  777. }
  778. /*
  779. * handle unaligned tail bytes
  780. */
  781. data = 0;
  782. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  783. data = (data << 8) | *src++;
  784. --cnt;
  785. }
  786. for (; i<4; ++i, ++cp) {
  787. data = (data << 8) | (*(uchar *)cp);
  788. }
  789. return (write_word(info, wp, data));
  790. #else
  791. wp = (addr & ~1); /* get lower word aligned address */
  792. /*
  793. * handle unaligned start byte
  794. */
  795. if (addr - wp) {
  796. data = 0;
  797. data = (data << 8) | *src++;
  798. --cnt;
  799. if ((rc = write_short(info, wp, data)) != 0) {
  800. return (rc);
  801. }
  802. wp += 2;
  803. }
  804. /*
  805. * handle word aligned part
  806. */
  807. /* l = 0; used for debuging */
  808. while (cnt >= 2) {
  809. data = 0;
  810. for (i=0; i<2; ++i) {
  811. data = (data << 8) | *src++;
  812. }
  813. /* if(!l){
  814. printf("%x",data);
  815. l = 1;
  816. } used for debuging */
  817. if ((rc = write_short(info, wp, data)) != 0) {
  818. return (rc);
  819. }
  820. wp += 2;
  821. cnt -= 2;
  822. }
  823. if (cnt == 0) {
  824. return (0);
  825. }
  826. /*
  827. * handle unaligned tail bytes
  828. */
  829. data = 0;
  830. for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
  831. data = (data << 8) | *src++;
  832. --cnt;
  833. }
  834. for (; i<2; ++i, ++cp) {
  835. data = (data << 8) | (*(uchar *)cp);
  836. }
  837. return (write_short(info, wp, data));
  838. #endif
  839. }
  840. /*-----------------------------------------------------------------------
  841. * Write a word to Flash, returns:
  842. * 0 - OK
  843. * 1 - write timeout
  844. * 2 - Flash not erased
  845. */
  846. #ifndef CFG_FLASH_16BIT
  847. static int write_word (flash_info_t *info, ulong dest, ulong data)
  848. {
  849. vu_long *addr = (vu_long*)(info->start[0]);
  850. ulong start,barf;
  851. int flag;
  852. /* Check if Flash is (sufficiently) erased */
  853. if ((*((vu_long *)dest) & data) != data) {
  854. return (2);
  855. }
  856. /* Disable interrupts which might cause a timeout here */
  857. flag = disable_interrupts();
  858. if(info->flash_id > FLASH_AMD_COMP) {
  859. /* AMD stuff */
  860. addr[0x0555] = 0x00AA00AA;
  861. addr[0x02AA] = 0x00550055;
  862. addr[0x0555] = 0x00A000A0;
  863. } else {
  864. /* intel stuff */
  865. *addr = 0x00400040;
  866. }
  867. *((vu_long *)dest) = data;
  868. /* re-enable interrupts if necessary */
  869. if (flag)
  870. enable_interrupts();
  871. /* data polling for D7 */
  872. start = get_timer (0);
  873. if(info->flash_id > FLASH_AMD_COMP) {
  874. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  875. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  876. return (1);
  877. }
  878. }
  879. } else {
  880. while(!(addr[0] & 0x00800080)){ /* wait for error or finish */
  881. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  882. return (1);
  883. }
  884. if( addr[0] & 0x003A003A) { /* check for error */
  885. barf = addr[0] & 0x003A0000;
  886. if( barf ) {
  887. barf >>=16;
  888. } else {
  889. barf = addr[0] & 0x0000003A;
  890. }
  891. printf("\nFlash write error at address %lx\n",(unsigned long)dest);
  892. if(barf & 0x0002) printf("Block locked, not erased.\n");
  893. if(barf & 0x0010) printf("Programming error.\n");
  894. if(barf & 0x0008) printf("Vpp Low error.\n");
  895. return(2);
  896. }
  897. }
  898. return (0);
  899. }
  900. #else
  901. static int write_short (flash_info_t *info, ulong dest, ushort data)
  902. {
  903. vu_short *addr = (vu_short*)(info->start[0]);
  904. ulong start,barf;
  905. int flag;
  906. /* Check if Flash is (sufficiently) erased */
  907. if ((*((vu_short *)dest) & data) != data) {
  908. return (2);
  909. }
  910. /* Disable interrupts which might cause a timeout here */
  911. flag = disable_interrupts();
  912. if(info->flash_id < FLASH_AMD_COMP) {
  913. /* AMD stuff */
  914. addr[0x0555] = 0x00AA;
  915. addr[0x02AA] = 0x0055;
  916. addr[0x0555] = 0x00A0;
  917. } else {
  918. /* intel stuff */
  919. *addr = 0x00D0;
  920. *addr = 0x0040;
  921. }
  922. *((vu_short *)dest) = data;
  923. /* re-enable interrupts if necessary */
  924. if (flag)
  925. enable_interrupts();
  926. /* data polling for D7 */
  927. start = get_timer (0);
  928. if(info->flash_id < FLASH_AMD_COMP) {
  929. /* AMD stuff */
  930. while ((*((vu_short *)dest) & 0x0080) != (data & 0x0080)) {
  931. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  932. return (1);
  933. }
  934. }
  935. } else {
  936. /* intel stuff */
  937. while(!(addr[0] & 0x0080)){ /* wait for error or finish */
  938. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
  939. }
  940. if( addr[0] & 0x003A) { /* check for error */
  941. barf = addr[0] & 0x003A;
  942. printf("\nFlash write error at address %lx\n",(unsigned long)dest);
  943. if(barf & 0x0002) printf("Block locked, not erased.\n");
  944. if(barf & 0x0010) printf("Programming error.\n");
  945. if(barf & 0x0008) printf("Vpp Low error.\n");
  946. return(2);
  947. }
  948. *addr = 0x00B0;
  949. *addr = 0x0070;
  950. while(!(addr[0] & 0x0080)){ /* wait for error or finish */
  951. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
  952. }
  953. *addr = 0x00FF;
  954. }
  955. return (0);
  956. }
  957. #endif
  958. /*-----------------------------------------------------------------------
  959. */