flash.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * (C) Copyright 2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
  6. * Add support for Am29F016D and dynamic switch setting.
  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. /*
  27. * Modified 4/5/2001
  28. * Wait for completion of each sector erase command issued
  29. * 4/5/2001
  30. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  31. */
  32. #include <common.h>
  33. #include <asm/ppc4xx.h>
  34. #include <asm/processor.h>
  35. #include <asm/ppc440.h>
  36. #include "yucca.h"
  37. #ifdef DEBUG
  38. #define DEBUGF(x...) printf(x)
  39. #else
  40. #define DEBUGF(x...)
  41. #endif /* DEBUG */
  42. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  43. /*
  44. * Mark big flash bank (16 bit instead of 8 bit access) in address with bit 0
  45. */
  46. static unsigned long flash_addr_table[][CONFIG_SYS_MAX_FLASH_BANKS] = {
  47. {0xfff00000, 0xfff80000, 0xe7c00001}, /* 0:boot from small flash */
  48. {0x00000000, 0x00000000, 0x00000000}, /* 1:boot from pci 66 */
  49. {0x00000000, 0x00000000, 0x00000000}, /* 2:boot from nand flash */
  50. {0xe7F00000, 0xe7F80000, 0xFFC00001}, /* 3:boot from big flash 33*/
  51. {0xe7F00000, 0xe7F80000, 0xFFC00001}, /* 4:boot from big flash 66*/
  52. {0x00000000, 0x00000000, 0x00000000}, /* 5:boot from */
  53. {0x00000000, 0x00000000, 0x00000000}, /* 6:boot from pci 66 */
  54. {0x00000000, 0x00000000, 0x00000000}, /* 7:boot from */
  55. {0xfff00000, 0xfff80000, 0xe7c00001}, /* 8:boot from small flash */
  56. };
  57. /*
  58. * include common flash code (for amcc boards)
  59. */
  60. /*-----------------------------------------------------------------------
  61. * Functions
  62. */
  63. static int write_word(flash_info_t * info, ulong dest, ulong data);
  64. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  65. static int write_word_1(flash_info_t * info, ulong dest, ulong data);
  66. static int write_word_2(flash_info_t * info, ulong dest, ulong data);
  67. static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
  68. static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
  69. static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
  70. static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
  71. #endif
  72. void flash_print_info(flash_info_t * info)
  73. {
  74. int i;
  75. int k;
  76. int size;
  77. int erased;
  78. volatile unsigned long *flash;
  79. if (info->flash_id == FLASH_UNKNOWN) {
  80. printf("missing or unknown FLASH type\n");
  81. return;
  82. }
  83. switch (info->flash_id & FLASH_VENDMASK) {
  84. case FLASH_MAN_AMD:
  85. printf("AMD ");
  86. break;
  87. case FLASH_MAN_STM:
  88. printf("STM ");
  89. break;
  90. case FLASH_MAN_FUJ:
  91. printf("FUJITSU ");
  92. break;
  93. case FLASH_MAN_SST:
  94. printf("SST ");
  95. break;
  96. case FLASH_MAN_MX:
  97. printf("MIXC ");
  98. break;
  99. default:
  100. printf("Unknown Vendor ");
  101. break;
  102. }
  103. switch (info->flash_id & FLASH_TYPEMASK) {
  104. case FLASH_AM040:
  105. printf("AM29F040 (512 Kbit, uniform sector size)\n");
  106. break;
  107. case FLASH_AM400B:
  108. printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
  109. break;
  110. case FLASH_AM400T:
  111. printf("AM29LV400T (4 Mbit, top boot sector)\n");
  112. break;
  113. case FLASH_AM800B:
  114. printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
  115. break;
  116. case FLASH_AM800T:
  117. printf("AM29LV800T (8 Mbit, top boot sector)\n");
  118. break;
  119. case FLASH_AMD016:
  120. printf("AM29F016D (16 Mbit, uniform sector size)\n");
  121. break;
  122. case FLASH_AM160B:
  123. printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
  124. break;
  125. case FLASH_AM160T:
  126. printf("AM29LV160T (16 Mbit, top boot sector)\n");
  127. break;
  128. case FLASH_AM320B:
  129. printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
  130. break;
  131. case FLASH_AM320T:
  132. printf("AM29LV320T (32 Mbit, top boot sector)\n");
  133. break;
  134. case FLASH_AM033C:
  135. printf("AM29LV033C (32 Mbit, top boot sector)\n");
  136. break;
  137. case FLASH_SST800A:
  138. printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  139. break;
  140. case FLASH_SST160A:
  141. printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  142. break;
  143. case FLASH_STMW320DT:
  144. printf ("M29W320DT (32 M, top sector)\n");
  145. break;
  146. case FLASH_MXLV320T:
  147. printf ("MXLV320T (32 Mbit, top sector)\n");
  148. break;
  149. default:
  150. printf("Unknown Chip Type\n");
  151. break;
  152. }
  153. printf(" Size: %ld KB in %d Sectors\n",
  154. info->size >> 10, info->sector_count);
  155. printf(" Sector Start Addresses:");
  156. for (i = 0; i < info->sector_count; ++i) {
  157. /*
  158. * Check if whole sector is erased
  159. */
  160. if (i != (info->sector_count - 1))
  161. size = info->start[i + 1] - info->start[i];
  162. else
  163. size = info->start[0] + info->size - info->start[i];
  164. erased = 1;
  165. flash = (volatile unsigned long *)info->start[i];
  166. size = size >> 2; /* divide by 4 for longword access */
  167. for (k = 0; k < size; k++) {
  168. if (*flash++ != 0xffffffff) {
  169. erased = 0;
  170. break;
  171. }
  172. }
  173. if ((i % 5) == 0)
  174. printf("\n ");
  175. printf(" %08lX%s%s",
  176. info->start[i],
  177. erased ? " E" : " ",
  178. info->protect[i] ? "RO " : " ");
  179. }
  180. printf("\n");
  181. return;
  182. }
  183. /*
  184. * The following code cannot be run from FLASH!
  185. */
  186. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  187. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  188. {
  189. /* bit 0 used for big flash marking */
  190. if ((ulong)addr & 0x1)
  191. return flash_get_size_2((vu_long *)((ulong)addr & 0xfffffffe), info);
  192. else
  193. return flash_get_size_1(addr, info);
  194. }
  195. static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
  196. #else
  197. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  198. #endif
  199. {
  200. short i;
  201. CONFIG_SYS_FLASH_WORD_SIZE value;
  202. ulong base = (ulong) addr;
  203. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
  204. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
  205. /* Write auto select command: read Manufacturer ID */
  206. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  207. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  208. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
  209. udelay(1000);
  210. value = addr2[0];
  211. DEBUGF("FLASH MANUFACT: %x\n", value);
  212. switch (value) {
  213. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
  214. info->flash_id = FLASH_MAN_AMD;
  215. break;
  216. case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
  217. info->flash_id = FLASH_MAN_FUJ;
  218. break;
  219. case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
  220. info->flash_id = FLASH_MAN_SST;
  221. break;
  222. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
  223. info->flash_id = FLASH_MAN_STM;
  224. break;
  225. default:
  226. info->flash_id = FLASH_UNKNOWN;
  227. info->sector_count = 0;
  228. info->size = 0;
  229. return (0); /* no or unknown flash */
  230. }
  231. value = addr2[1]; /* device ID */
  232. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  233. switch (value) {
  234. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
  235. info->flash_id += FLASH_AM040;
  236. info->sector_count = 8;
  237. info->size = 0x0080000; /* => 512 ko */
  238. break;
  239. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
  240. info->flash_id += FLASH_AM040;
  241. info->sector_count = 8;
  242. info->size = 0x0080000; /* => 512 ko */
  243. break;
  244. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
  245. info->flash_id += FLASH_AM040;
  246. info->sector_count = 8;
  247. info->size = 0x0080000; /* => 512 ko */
  248. break;
  249. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
  250. info->flash_id += FLASH_AMD016;
  251. info->sector_count = 32;
  252. info->size = 0x00200000;
  253. break; /* => 2 MB */
  254. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
  255. info->flash_id += FLASH_AMDLV033C;
  256. info->sector_count = 64;
  257. info->size = 0x00400000;
  258. break; /* => 4 MB */
  259. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
  260. info->flash_id += FLASH_AM400T;
  261. info->sector_count = 11;
  262. info->size = 0x00080000;
  263. break; /* => 0.5 MB */
  264. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
  265. info->flash_id += FLASH_AM400B;
  266. info->sector_count = 11;
  267. info->size = 0x00080000;
  268. break; /* => 0.5 MB */
  269. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
  270. info->flash_id += FLASH_AM800T;
  271. info->sector_count = 19;
  272. info->size = 0x00100000;
  273. break; /* => 1 MB */
  274. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
  275. info->flash_id += FLASH_AM800B;
  276. info->sector_count = 19;
  277. info->size = 0x00100000;
  278. break; /* => 1 MB */
  279. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
  280. info->flash_id += FLASH_AM160T;
  281. info->sector_count = 35;
  282. info->size = 0x00200000;
  283. break; /* => 2 MB */
  284. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
  285. info->flash_id += FLASH_AM160B;
  286. info->sector_count = 35;
  287. info->size = 0x00200000;
  288. break; /* => 2 MB */
  289. default:
  290. info->flash_id = FLASH_UNKNOWN;
  291. return (0); /* => no or unknown flash */
  292. }
  293. /* set up sector start address table */
  294. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  295. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
  296. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
  297. for (i = 0; i < info->sector_count; i++)
  298. info->start[i] = base + (i * 0x00010000);
  299. } else {
  300. if (info->flash_id & FLASH_BTYPE) {
  301. /* set sector offsets for bottom boot block type */
  302. info->start[0] = base + 0x00000000;
  303. info->start[1] = base + 0x00004000;
  304. info->start[2] = base + 0x00006000;
  305. info->start[3] = base + 0x00008000;
  306. for (i = 4; i < info->sector_count; i++) {
  307. info->start[i] =
  308. base + (i * 0x00010000) - 0x00030000;
  309. }
  310. } else {
  311. /* set sector offsets for top boot block type */
  312. i = info->sector_count - 1;
  313. info->start[i--] = base + info->size - 0x00004000;
  314. info->start[i--] = base + info->size - 0x00006000;
  315. info->start[i--] = base + info->size - 0x00008000;
  316. for (; i >= 0; i--) {
  317. info->start[i] = base + i * 0x00010000;
  318. }
  319. }
  320. }
  321. /* check for protected sectors */
  322. for (i = 0; i < info->sector_count; i++) {
  323. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  324. /* D0 = 1 if protected */
  325. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  326. /* For AMD29033C flash we need to resend the command of *
  327. * reading flash protection for upper 8 Mb of flash */
  328. if (i == 32) {
  329. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
  330. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
  331. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
  332. }
  333. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  334. info->protect[i] = 0;
  335. else
  336. info->protect[i] = addr2[2] & 1;
  337. }
  338. /* issue bank reset to return to read mode */
  339. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  340. return (info->size);
  341. }
  342. static int wait_for_DQ7_1(flash_info_t * info, int sect)
  343. {
  344. ulong start, now, last;
  345. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
  346. (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  347. start = get_timer(0);
  348. last = start;
  349. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  350. (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
  351. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  352. printf("Timeout\n");
  353. return -1;
  354. }
  355. /* show that we're waiting */
  356. if ((now - last) > 1000) { /* every second */
  357. putc('.');
  358. last = now;
  359. }
  360. }
  361. return 0;
  362. }
  363. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  364. int flash_erase(flash_info_t * info, int s_first, int s_last)
  365. {
  366. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  367. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  368. ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) ||
  369. ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T)) {
  370. return flash_erase_2(info, s_first, s_last);
  371. } else {
  372. return flash_erase_1(info, s_first, s_last);
  373. }
  374. }
  375. static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
  376. #else
  377. int flash_erase(flash_info_t * info, int s_first, int s_last)
  378. #endif
  379. {
  380. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  381. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  382. int flag, prot, sect, l_sect;
  383. int i;
  384. if ((s_first < 0) || (s_first > s_last)) {
  385. if (info->flash_id == FLASH_UNKNOWN)
  386. printf("- missing\n");
  387. else
  388. printf("- no sectors to erase\n");
  389. return 1;
  390. }
  391. if (info->flash_id == FLASH_UNKNOWN) {
  392. printf("Can't erase unknown flash type - aborted\n");
  393. return 1;
  394. }
  395. prot = 0;
  396. for (sect = s_first; sect <= s_last; ++sect) {
  397. if (info->protect[sect])
  398. prot++;
  399. }
  400. if (prot)
  401. printf("- Warning: %d protected sectors will not be erased!", prot);
  402. printf("\n");
  403. l_sect = -1;
  404. /* Disable interrupts which might cause a timeout here */
  405. flag = disable_interrupts();
  406. /* Start erase on unprotected sectors */
  407. for (sect = s_first; sect <= s_last; sect++) {
  408. if (info->protect[sect] == 0) { /* not protected */
  409. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  410. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  411. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  412. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  413. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  414. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  415. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  416. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
  417. for (i = 0; i < 50; i++)
  418. udelay(1000); /* wait 1 ms */
  419. } else {
  420. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  421. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  422. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  423. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  424. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  425. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  426. }
  427. l_sect = sect;
  428. /*
  429. * Wait for each sector to complete, it's more
  430. * reliable. According to AMD Spec, you must
  431. * issue all erase commands within a specified
  432. * timeout. This has been seen to fail, especially
  433. * if printf()s are included (for debug)!!
  434. */
  435. wait_for_DQ7_1(info, sect);
  436. }
  437. }
  438. /* re-enable interrupts if necessary */
  439. if (flag)
  440. enable_interrupts();
  441. /* wait at least 80us - let's wait 1 ms */
  442. udelay(1000);
  443. /* reset to read mode */
  444. addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
  445. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  446. printf(" done\n");
  447. return 0;
  448. }
  449. /*-----------------------------------------------------------------------
  450. * Copy memory to flash, returns:
  451. * 0 - OK
  452. * 1 - write timeout
  453. * 2 - Flash not erased
  454. */
  455. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  456. {
  457. ulong cp, wp, data;
  458. int i, l, rc;
  459. wp = (addr & ~3); /* get lower word aligned address */
  460. /*
  461. * handle unaligned start bytes
  462. */
  463. if ((l = addr - wp) != 0) {
  464. data = 0;
  465. for (i = 0, cp = wp; i < l; ++i, ++cp)
  466. data = (data << 8) | (*(uchar *) cp);
  467. for (; i < 4 && cnt > 0; ++i) {
  468. data = (data << 8) | *src++;
  469. --cnt;
  470. ++cp;
  471. }
  472. for (; cnt == 0 && i < 4; ++i, ++cp)
  473. data = (data << 8) | (*(uchar *) cp);
  474. if ((rc = write_word(info, wp, data)) != 0)
  475. return (rc);
  476. wp += 4;
  477. }
  478. /*
  479. * handle word aligned part
  480. */
  481. while (cnt >= 4) {
  482. data = 0;
  483. for (i = 0; i < 4; ++i)
  484. data = (data << 8) | *src++;
  485. if ((rc = write_word(info, wp, data)) != 0)
  486. return (rc);
  487. wp += 4;
  488. cnt -= 4;
  489. }
  490. if (cnt == 0)
  491. return (0);
  492. /*
  493. * handle unaligned tail bytes
  494. */
  495. data = 0;
  496. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  497. data = (data << 8) | *src++;
  498. --cnt;
  499. }
  500. for (; i < 4; ++i, ++cp)
  501. data = (data << 8) | (*(uchar *) cp);
  502. return (write_word(info, wp, data));
  503. }
  504. /*-----------------------------------------------------------------------
  505. * Copy memory to flash, returns:
  506. * 0 - OK
  507. * 1 - write timeout
  508. * 2 - Flash not erased
  509. */
  510. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  511. static int write_word(flash_info_t * info, ulong dest, ulong data)
  512. {
  513. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  514. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  515. ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) ||
  516. ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T)) {
  517. return write_word_2(info, dest, data);
  518. } else {
  519. return write_word_1(info, dest, data);
  520. }
  521. }
  522. static int write_word_1(flash_info_t * info, ulong dest, ulong data)
  523. #else
  524. static int write_word(flash_info_t * info, ulong dest, ulong data)
  525. #endif
  526. {
  527. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  528. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
  529. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
  530. ulong start;
  531. int i, flag;
  532. /* Check if Flash is (sufficiently) erased */
  533. if ((*((vu_long *)dest) & data) != data)
  534. return (2);
  535. for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
  536. /* Disable interrupts which might cause a timeout here */
  537. flag = disable_interrupts();
  538. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  539. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  540. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
  541. dest2[i] = data2[i];
  542. /* re-enable interrupts if necessary */
  543. if (flag)
  544. enable_interrupts();
  545. /* data polling for D7 */
  546. start = get_timer(0);
  547. while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  548. (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
  549. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
  550. return (1);
  551. }
  552. }
  553. return (0);
  554. }
  555. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  556. #undef CONFIG_SYS_FLASH_WORD_SIZE
  557. #define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
  558. /*
  559. * The following code cannot be run from FLASH!
  560. */
  561. static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
  562. {
  563. short i;
  564. int n;
  565. CONFIG_SYS_FLASH_WORD_SIZE value;
  566. ulong base = (ulong) addr;
  567. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
  568. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
  569. /* issue bank reset to return to read mode */
  570. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  571. /* Write auto select command: read Manufacturer ID */
  572. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  573. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  574. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
  575. udelay(1000);
  576. value = addr2[0];
  577. DEBUGF("FLASH MANUFACT: %x\n", value);
  578. switch (value) {
  579. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
  580. info->flash_id = FLASH_MAN_AMD;
  581. break;
  582. case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
  583. info->flash_id = FLASH_MAN_FUJ;
  584. break;
  585. case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
  586. info->flash_id = FLASH_MAN_SST;
  587. break;
  588. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
  589. info->flash_id = FLASH_MAN_STM;
  590. break;
  591. case (CONFIG_SYS_FLASH_WORD_SIZE) MX_MANUFACT:
  592. info->flash_id = FLASH_MAN_MX;
  593. break;
  594. default:
  595. info->flash_id = FLASH_UNKNOWN;
  596. info->sector_count = 0;
  597. info->size = 0;
  598. return (0); /* no or unknown flash */
  599. }
  600. value = addr2[1]; /* device ID */
  601. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  602. switch (value) {
  603. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
  604. info->flash_id += FLASH_AM320T;
  605. info->sector_count = 71;
  606. info->size = 0x00400000;
  607. break; /* => 4 MB */
  608. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
  609. info->flash_id += FLASH_AM320B;
  610. info->sector_count = 71;
  611. info->size = 0x00400000;
  612. break; /* => 4 MB */
  613. case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
  614. info->flash_id += FLASH_STMW320DT;
  615. info->sector_count = 67;
  616. info->size = 0x00400000;
  617. break; /* => 4 MB */
  618. case (CONFIG_SYS_FLASH_WORD_SIZE)MX_ID_LV320T:
  619. info->flash_id += FLASH_MXLV320T;
  620. info->sector_count = 71;
  621. info->size = 0x00400000;
  622. break; /* => 4 MB */
  623. default:
  624. info->flash_id = FLASH_UNKNOWN;
  625. return (0); /* => no or unknown flash */
  626. }
  627. /* set up sector start address table */
  628. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  629. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
  630. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
  631. for (i = 0; i < info->sector_count; i++)
  632. info->start[i] = base + (i * 0x00010000);
  633. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
  634. /* set sector offsets for top boot block type */
  635. base += info->size;
  636. i = info->sector_count;
  637. /* 1 x 16k boot sector */
  638. base -= 16 << 10;
  639. --i;
  640. info->start[i] = base;
  641. /* 2 x 8k boot sectors */
  642. for (n = 0; n < 2; ++n) {
  643. base -= 8 << 10;
  644. --i;
  645. info->start[i] = base;
  646. }
  647. /* 1 x 32k boot sector */
  648. base -= 32 << 10;
  649. --i;
  650. info->start[i] = base;
  651. while (i > 0) { /* 64k regular sectors */
  652. base -= 64 << 10;
  653. --i;
  654. info->start[i] = base;
  655. }
  656. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) {
  657. i = info->sector_count - 1;
  658. info->start[i--] = base + info->size - 0x00002000;
  659. info->start[i--] = base + info->size - 0x00004000;
  660. info->start[i--] = base + info->size - 0x00006000;
  661. info->start[i--] = base + info->size - 0x00008000;
  662. info->start[i--] = base + info->size - 0x0000a000;
  663. info->start[i--] = base + info->size - 0x0000c000;
  664. info->start[i--] = base + info->size - 0x0000e000;
  665. info->start[i--] = base + info->size - 0x00010000;
  666. for (; i >= 0; i--)
  667. info->start[i] = base + i * 0x00010000;
  668. } else {
  669. if (info->flash_id & FLASH_BTYPE) {
  670. /* set sector offsets for bottom boot block type */
  671. info->start[0] = base + 0x00000000;
  672. info->start[1] = base + 0x00004000;
  673. info->start[2] = base + 0x00006000;
  674. info->start[3] = base + 0x00008000;
  675. for (i = 4; i < info->sector_count; i++)
  676. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  677. } else {
  678. /* set sector offsets for top boot block type */
  679. i = info->sector_count - 1;
  680. info->start[i--] = base + info->size - 0x00004000;
  681. info->start[i--] = base + info->size - 0x00006000;
  682. info->start[i--] = base + info->size - 0x00008000;
  683. for (; i >= 0; i--)
  684. info->start[i] = base + i * 0x00010000;
  685. }
  686. }
  687. /* check for protected sectors */
  688. for (i = 0; i < info->sector_count; i++) {
  689. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  690. /* D0 = 1 if protected */
  691. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  692. /* For AMD29033C flash we need to resend the command of *
  693. * reading flash protection for upper 8 Mb of flash */
  694. if (i == 32) {
  695. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
  696. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
  697. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
  698. }
  699. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  700. info->protect[i] = 0;
  701. else
  702. info->protect[i] = addr2[2] & 1;
  703. }
  704. /* issue bank reset to return to read mode */
  705. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  706. return (info->size);
  707. }
  708. static int wait_for_DQ7_2(flash_info_t * info, int sect)
  709. {
  710. ulong start, now, last;
  711. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
  712. (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  713. start = get_timer(0);
  714. last = start;
  715. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  716. (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
  717. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  718. printf("Timeout\n");
  719. return -1;
  720. }
  721. /* show that we're waiting */
  722. if ((now - last) > 1000) { /* every second */
  723. putc('.');
  724. last = now;
  725. }
  726. }
  727. return 0;
  728. }
  729. static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
  730. {
  731. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  732. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  733. int flag, prot, sect, l_sect;
  734. int i;
  735. if ((s_first < 0) || (s_first > s_last)) {
  736. if (info->flash_id == FLASH_UNKNOWN)
  737. printf("- missing\n");
  738. else
  739. printf("- no sectors to erase\n");
  740. return 1;
  741. }
  742. if (info->flash_id == FLASH_UNKNOWN) {
  743. printf("Can't erase unknown flash type - aborted\n");
  744. return 1;
  745. }
  746. prot = 0;
  747. for (sect = s_first; sect <= s_last; ++sect) {
  748. if (info->protect[sect])
  749. prot++;
  750. }
  751. if (prot)
  752. printf("- Warning: %d protected sectors will not be erased!", prot);
  753. printf("\n");
  754. l_sect = -1;
  755. /* Disable interrupts which might cause a timeout here */
  756. flag = disable_interrupts();
  757. /* Start erase on unprotected sectors */
  758. for (sect = s_first; sect <= s_last; sect++) {
  759. if (info->protect[sect] == 0) { /* not protected */
  760. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  761. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  762. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  763. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  764. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  765. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  766. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  767. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
  768. for (i = 0; i < 50; i++)
  769. udelay(1000); /* wait 1 ms */
  770. } else {
  771. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  772. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  773. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  774. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  775. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  776. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  777. }
  778. l_sect = sect;
  779. /*
  780. * Wait for each sector to complete, it's more
  781. * reliable. According to AMD Spec, you must
  782. * issue all erase commands within a specified
  783. * timeout. This has been seen to fail, especially
  784. * if printf()s are included (for debug)!!
  785. */
  786. wait_for_DQ7_2(info, sect);
  787. }
  788. }
  789. /* re-enable interrupts if necessary */
  790. if (flag)
  791. enable_interrupts();
  792. /* wait at least 80us - let's wait 1 ms */
  793. udelay(1000);
  794. /* reset to read mode */
  795. addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
  796. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  797. printf(" done\n");
  798. return 0;
  799. }
  800. static int write_word_2(flash_info_t * info, ulong dest, ulong data)
  801. {
  802. ulong *data_ptr = &data;
  803. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
  804. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
  805. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
  806. ulong start;
  807. int i;
  808. /* Check if Flash is (sufficiently) erased */
  809. if ((*((vu_long *)dest) & data) != data)
  810. return (2);
  811. for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
  812. int flag;
  813. /* Disable interrupts which might cause a timeout here */
  814. flag = disable_interrupts();
  815. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  816. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  817. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
  818. dest2[i] = data2[i];
  819. /* re-enable interrupts if necessary */
  820. if (flag)
  821. enable_interrupts();
  822. /* data polling for D7 */
  823. start = get_timer(0);
  824. while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  825. (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
  826. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
  827. return (1);
  828. }
  829. }
  830. return (0);
  831. }
  832. #endif /* CONFIG_SYS_FLASH_2ND_16BIT_DEV */
  833. /*-----------------------------------------------------------------------
  834. * Functions
  835. */
  836. static ulong flash_get_size(vu_long * addr, flash_info_t * info);
  837. static int write_word(flash_info_t * info, ulong dest, ulong data);
  838. /*-----------------------------------------------------------------------
  839. */
  840. unsigned long flash_init(void)
  841. {
  842. unsigned long total_b = 0;
  843. unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
  844. unsigned short index = 0;
  845. int i;
  846. unsigned long val;
  847. unsigned long ebc_boot_size;
  848. unsigned long boot_selection;
  849. mfsdr(sdr_pstrp0, val);
  850. index = (val & SDR0_PSTRP0_BOOTSTRAP_MASK) >> 28;
  851. if ((index == 0xc) || (index == 8)) {
  852. /*
  853. * Boot Settings in IIC EEprom address 0xA8 or 0xA0
  854. * Read Serial Device Strap Register1 in PPC440SPe
  855. */
  856. mfsdr(SDR0_SDSTP1, val);
  857. boot_selection = val & SDR0_SDSTP1_BOOT_SEL_MASK;
  858. ebc_boot_size = val & SDR0_SDSTP1_EBC_ROM_BS_MASK;
  859. switch(boot_selection) {
  860. case SDR0_SDSTP1_BOOT_SEL_EBC:
  861. switch(ebc_boot_size) {
  862. case SDR0_SDSTP1_EBC_ROM_BS_16BIT:
  863. index = 3;
  864. break;
  865. case SDR0_SDSTP1_EBC_ROM_BS_8BIT:
  866. index = 0;
  867. break;
  868. }
  869. break;
  870. case SDR0_SDSTP1_BOOT_SEL_PCI:
  871. index = 1;
  872. break;
  873. }
  874. } /*else if (index == 0) {*/
  875. /* if (in8(FPGA_SETTING_REG) & FPGA_SET_REG_OP_CODE_FLASH_ABOVE)*/
  876. /* index = 8;*/ /* sram below op code flash -> new index 8*/
  877. /* }*/
  878. DEBUGF("\n");
  879. DEBUGF("FLASH: Index: %d\n", index);
  880. /* Init: no FLASHes known */
  881. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  882. flash_info[i].flash_id = FLASH_UNKNOWN;
  883. flash_info[i].sector_count = -1;
  884. flash_info[i].size = 0;
  885. /* check whether the address is 0 */
  886. if (flash_addr_table[index][i] == 0)
  887. continue;
  888. /* call flash_get_size() to initialize sector address */
  889. size_b[i] = flash_get_size((vu_long *) flash_addr_table[index][i],
  890. &flash_info[i]);
  891. flash_info[i].size = size_b[i];
  892. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  893. printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  894. i, size_b[i], size_b[i] << 20);
  895. flash_info[i].sector_count = -1;
  896. flash_info[i].size = 0;
  897. }
  898. /* Monitor protection ON by default */
  899. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE,
  900. CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
  901. &flash_info[i]);
  902. #if defined(CONFIG_ENV_IS_IN_FLASH)
  903. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
  904. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  905. &flash_info[i]);
  906. #if defined(CONFIG_ENV_ADDR_REDUND)
  907. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
  908. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
  909. &flash_info[i]);
  910. #endif
  911. #endif
  912. total_b += flash_info[i].size;
  913. }
  914. return total_b;
  915. }