flash.c 28 KB

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