flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * (C) Copyright 2002, 2003
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Alex Zuepke <azu@sysgo.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/io.h>
  29. #include <pci.h>
  30. #include <asm/ic/sc520.h>
  31. #define PROBE_BUFFER_SIZE 1024
  32. static unsigned char buffer[PROBE_BUFFER_SIZE];
  33. #define SC520_MAX_FLASH_BANKS 3
  34. #define SC520_FLASH_BANK0_BASE 0x38000000 /* BOOTCS */
  35. #define SC520_FLASH_BANK1_BASE 0x30000000 /* ROMCS0 */
  36. #define SC520_FLASH_BANK2_BASE 0x28000000 /* ROMCS1 */
  37. #define SC520_FLASH_BANKSIZE 0x8000000
  38. #define AMD29LV016B_SIZE 0x200000
  39. #define AMD29LV016B_SECTORS 32
  40. flash_info_t flash_info[SC520_MAX_FLASH_BANKS];
  41. #define READY 1
  42. #define ERR 2
  43. #define TMO 4
  44. /*-----------------------------------------------------------------------
  45. */
  46. static u32 _probe_flash(u32 addr, u32 bw, int il)
  47. {
  48. u32 result=0;
  49. /* First do an unlock cycle for the benefit of
  50. * devices that need it */
  51. switch (bw) {
  52. case 1:
  53. *(volatile u8*)(addr+0x5555) = 0xaa;
  54. *(volatile u8*)(addr+0x2aaa) = 0x55;
  55. *(volatile u8*)(addr+0x5555) = 0x90;
  56. /* Read vendor */
  57. result = *(volatile u8*)addr;
  58. result <<= 16;
  59. /* Read device */
  60. result |= *(volatile u8*)(addr+2);
  61. /* Return device to data mode */
  62. *(volatile u8*)addr = 0xff;
  63. *(volatile u8*)(addr+0x5555), 0xf0;
  64. break;
  65. case 2:
  66. *(volatile u16*)(addr+0xaaaa) = 0xaaaa;
  67. *(volatile u16*)(addr+0x5554) = 0x5555;
  68. /* Issue identification command */
  69. if (il == 2) {
  70. *(volatile u16*)(addr+0xaaaa) = 0x9090;
  71. /* Read vendor */
  72. result = *(volatile u8*)addr;
  73. result <<= 16;
  74. /* Read device */
  75. result |= *(volatile u8*)(addr+2);
  76. /* Return device to data mode */
  77. *(volatile u16*)addr = 0xffff;
  78. *(volatile u16*)(addr+0xaaaa), 0xf0f0;
  79. } else {
  80. *(volatile u8*)(addr+0xaaaa) = 0x90;
  81. /* Read vendor */
  82. result = *(volatile u16*)addr;
  83. result <<= 16;
  84. /* Read device */
  85. result |= *(volatile u16*)(addr+2);
  86. /* Return device to data mode */
  87. *(volatile u8*)addr = 0xff;
  88. *(volatile u8*)(addr+0xaaaa), 0xf0;
  89. }
  90. break;
  91. case 4:
  92. *(volatile u32*)(addr+0x5554) = 0xaaaaaaaa;
  93. *(volatile u32*)(addr+0xaaa8) = 0x55555555;
  94. switch (il) {
  95. case 1:
  96. /* Issue identification command */
  97. *(volatile u8*)(addr+0x5554) = 0x90;
  98. /* Read vendor */
  99. result = *(volatile u16*)addr;
  100. result <<= 16;
  101. /* Read device */
  102. result |= *(volatile u16*)(addr+4);
  103. /* Return device to data mode */
  104. *(volatile u8*)addr = 0xff;
  105. *(volatile u8*)(addr+0x5554), 0xf0;
  106. break;
  107. case 2:
  108. /* Issue identification command */
  109. *(volatile u32*)(addr + 0x5554) = 0x00900090;
  110. /* Read vendor */
  111. result = *(volatile u16*)addr;
  112. result <<= 16;
  113. /* Read device */
  114. result |= *(volatile u16*)(addr+4);
  115. /* Return device to data mode */
  116. *(volatile u32*)addr = 0x00ff00ff;
  117. *(volatile u32*)(addr+0x5554), 0x00f000f0;
  118. break;
  119. case 4:
  120. /* Issue identification command */
  121. *(volatile u32*)(addr+0x5554) = 0x90909090;
  122. /* Read vendor */
  123. result = *(volatile u8*)addr;
  124. result <<= 16;
  125. /* Read device */
  126. result |= *(volatile u8*)(addr+4);
  127. /* Return device to data mode */
  128. *(volatile u32*)addr = 0xffffffff;
  129. *(volatile u32*)(addr+0x5554), 0xf0f0f0f0;
  130. break;
  131. }
  132. break;
  133. }
  134. return result;
  135. }
  136. extern int _probe_flash_end;
  137. asm ("_probe_flash_end:\n"
  138. ".long 0\n");
  139. static int identify_flash(unsigned address, int width)
  140. {
  141. int is;
  142. int device;
  143. int vendor;
  144. int size;
  145. unsigned res;
  146. u32 (*_probe_flash_ptr)(u32 a, u32 bw, int il);
  147. size = (unsigned)&_probe_flash_end - (unsigned)_probe_flash;
  148. if (size > PROBE_BUFFER_SIZE) {
  149. printf("_probe_flash() routine too large (%d) %p - %p\n",
  150. size, &_probe_flash_end, _probe_flash);
  151. return 0;
  152. }
  153. memcpy(buffer, _probe_flash, size);
  154. _probe_flash_ptr = (void*)buffer;
  155. is = disable_interrupts();
  156. res = _probe_flash_ptr(address, width, 1);
  157. if (is) {
  158. enable_interrupts();
  159. }
  160. vendor = res >> 16;
  161. device = res & 0xffff;
  162. return res;
  163. }
  164. ulong flash_init(void)
  165. {
  166. int i, j;
  167. ulong size = 0;
  168. for (i = 0; i < SC520_MAX_FLASH_BANKS; i++) {
  169. unsigned id;
  170. ulong flashbase = 0;
  171. int sectsize = 0;
  172. memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  173. switch (i) {
  174. case 0:
  175. flashbase = SC520_FLASH_BANK0_BASE;
  176. break;
  177. case 1:
  178. flashbase = SC520_FLASH_BANK1_BASE;
  179. break;
  180. case 2:
  181. flashbase = SC520_FLASH_BANK2_BASE;
  182. break;
  183. default:
  184. panic("configured to many flash banks!\n");
  185. }
  186. id = identify_flash(flashbase, 4);
  187. switch (id & 0x00ff00ff) {
  188. case 0x000100c8:
  189. /* 29LV016B/29LV017B */
  190. flash_info[i].flash_id =
  191. (AMD_MANUFACT & FLASH_VENDMASK) |
  192. (AMD_ID_LV016B & FLASH_TYPEMASK);
  193. flash_info[i].size = AMD29LV016B_SIZE*4;
  194. flash_info[i].sector_count = AMD29LV016B_SECTORS;
  195. sectsize = (AMD29LV016B_SIZE*4)/AMD29LV016B_SECTORS;
  196. printf("Bank %d: 4 x AMD 29LV017B\n", i);
  197. break;
  198. default:
  199. printf("Bank %d have unknown flash %08x\n", i, id);
  200. flash_info[i].flash_id = FLASH_UNKNOWN;
  201. continue;
  202. }
  203. for (j = 0; j < flash_info[i].sector_count; j++) {
  204. flash_info[i].start[j] = flashbase + j * sectsize;
  205. }
  206. size += flash_info[i].size;
  207. flash_protect(FLAG_PROTECT_CLEAR,
  208. flash_info[i].start[0],
  209. flash_info[i].start[0] + flash_info[i].size - 1,
  210. &flash_info[i]);
  211. }
  212. /*
  213. * Protect monitor and environment sectors
  214. */
  215. flash_protect(FLAG_PROTECT_SET,
  216. i386boot_start,
  217. i386boot_end,
  218. &flash_info[0]);
  219. #ifdef CFG_ENV_ADDR
  220. flash_protect(FLAG_PROTECT_SET,
  221. CFG_ENV_ADDR,
  222. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  223. &flash_info[0]);
  224. #endif
  225. return size;
  226. }
  227. /*-----------------------------------------------------------------------
  228. */
  229. void flash_print_info(flash_info_t *info)
  230. {
  231. int i;
  232. switch (info->flash_id & FLASH_VENDMASK) {
  233. case (AMD_MANUFACT & FLASH_VENDMASK):
  234. printf("AMD: ");
  235. switch (info->flash_id & FLASH_TYPEMASK) {
  236. case (AMD_ID_LV016B & FLASH_TYPEMASK):
  237. printf("4x AMD29LV017B (4x16Mbit)\n");
  238. break;
  239. default:
  240. printf("Unknown Chip Type\n");
  241. goto done;
  242. break;
  243. }
  244. break;
  245. default:
  246. printf("Unknown Vendor ");
  247. break;
  248. }
  249. printf(" Size: %ld MB in %d Sectors\n",
  250. info->size >> 20, info->sector_count);
  251. printf(" Sector Start Addresses:");
  252. for (i = 0; i < info->sector_count; i++) {
  253. if ((i % 5) == 0) {
  254. printf ("\n ");
  255. }
  256. printf (" %08lX%s", info->start[i],
  257. info->protect[i] ? " (RO)" : " ");
  258. }
  259. printf ("\n");
  260. done: ;
  261. }
  262. /*-----------------------------------------------------------------------
  263. */
  264. /* this needs to be inlined, the SWTMRMMILLI register is reset by each read */
  265. #define __udelay(delay) \
  266. { \
  267. unsigned micro; \
  268. unsigned milli=0; \
  269. \
  270. micro = *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI); \
  271. \
  272. for (;;) { \
  273. \
  274. milli += *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI); \
  275. micro = *(volatile u16*)(0xfffef000+SC520_SWTMRMICRO); \
  276. \
  277. if ((delay) <= (micro + (milli * 1000))) { \
  278. break; \
  279. } \
  280. } \
  281. } while (0)
  282. static u32 _amd_erase_flash(u32 addr, u32 sector)
  283. {
  284. unsigned elapsed;
  285. /* Issue erase */
  286. *(volatile u32*)(addr + 0x5554) = 0xAAAAAAAA;
  287. *(volatile u32*)(addr + 0xaaa8) = 0x55555555;
  288. *(volatile u32*)(addr + 0x5554) = 0x80808080;
  289. /* And one unlock */
  290. *(volatile u32*)(addr + 0x5554) = 0xAAAAAAAA;
  291. *(volatile u32*)(addr + 0xaaa8) = 0x55555555;
  292. /* Sector erase command comes last */
  293. *(volatile u32*)(addr + sector) = 0x30303030;
  294. elapsed = *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI); /* dummy read */
  295. elapsed = 0;
  296. __udelay(50);
  297. while (((*(volatile u32*)(addr + sector)) & 0x80808080) != 0x80808080) {
  298. elapsed += *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI);
  299. if (elapsed > ((CFG_FLASH_ERASE_TOUT/CFG_HZ) * 1000)) {
  300. *(volatile u32*)(addr) = 0xf0f0f0f0;
  301. return 1;
  302. }
  303. }
  304. *(volatile u32*)(addr) = 0xf0f0f0f0;
  305. return 0;
  306. }
  307. extern int _amd_erase_flash_end;
  308. asm ("_amd_erase_flash_end:\n"
  309. ".long 0\n");
  310. int flash_erase(flash_info_t *info, int s_first, int s_last)
  311. {
  312. u32 (*_erase_flash_ptr)(u32 a, u32 so);
  313. int prot;
  314. int sect;
  315. unsigned size;
  316. if ((s_first < 0) || (s_first > s_last)) {
  317. if (info->flash_id == FLASH_UNKNOWN) {
  318. printf("- missing\n");
  319. } else {
  320. printf("- no sectors to erase\n");
  321. }
  322. return 1;
  323. }
  324. if ((info->flash_id & FLASH_VENDMASK) == (AMD_MANUFACT & FLASH_VENDMASK)) {
  325. size = (unsigned)&_amd_erase_flash_end - (unsigned)_amd_erase_flash;
  326. if (size > PROBE_BUFFER_SIZE) {
  327. printf("_amd_erase_flash() routine too large (%d) %p - %p\n",
  328. size, &_amd_erase_flash_end, _amd_erase_flash);
  329. return 0;
  330. }
  331. memcpy(buffer, _amd_erase_flash, size);
  332. _erase_flash_ptr = (void*)buffer;
  333. } else {
  334. printf ("Can't erase unknown flash type - aborted\n");
  335. return 1;
  336. }
  337. prot = 0;
  338. for (sect=s_first; sect<=s_last; ++sect) {
  339. if (info->protect[sect]) {
  340. prot++;
  341. }
  342. }
  343. if (prot) {
  344. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  345. } else {
  346. printf ("\n");
  347. }
  348. /* Start erase on unprotected sectors */
  349. for (sect = s_first; sect<=s_last; sect++) {
  350. if (info->protect[sect] == 0) { /* not protected */
  351. int res;
  352. int flag;
  353. /* Disable interrupts which might cause a timeout here */
  354. flag = disable_interrupts();
  355. res = _erase_flash_ptr(info->start[0], info->start[sect]-info->start[0]);
  356. /* re-enable interrupts if necessary */
  357. if (flag) {
  358. enable_interrupts();
  359. }
  360. if (res) {
  361. printf("Erase timed out, sector %d\n", sect);
  362. return res;
  363. }
  364. putc('.');
  365. }
  366. }
  367. return 0;
  368. }
  369. /*-----------------------------------------------------------------------
  370. * Write a word to Flash, returns:
  371. * 0 - OK
  372. * 1 - write timeout
  373. * 2 - Flash not erased
  374. */
  375. static int _amd_write_word(unsigned start, unsigned dest, unsigned data)
  376. {
  377. volatile u32 *addr2 = (u32*)start;
  378. volatile u32 *dest2 = (u32*)dest;
  379. volatile u32 *data2 = (u32*)&data;
  380. unsigned elapsed;
  381. /* Check if Flash is (sufficiently) erased */
  382. if ((*((volatile u32*)dest) & (u32)data) != (u32)data) {
  383. return 2;
  384. }
  385. addr2[0x5554] = 0xAAAAAAAA;
  386. addr2[0xaaa8] = 0x55555555;
  387. addr2[0x5554] = 0xA0A0A0A0;
  388. dest2[0] = data;
  389. elapsed = *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI); /* dummy read */
  390. elapsed = 0;
  391. /* data polling for D7 */
  392. while ((dest2[0] & 0x80808080) != (data2[0] & 0x80808080)) {
  393. elapsed += *(volatile u16*)(0xfffef000+SC520_SWTMRMILLI);
  394. if (elapsed > ((CFG_FLASH_WRITE_TOUT/CFG_HZ) * 1000)) {
  395. addr2[0] = 0xf0f0f0f0;
  396. return 1;
  397. }
  398. }
  399. addr2[0] = 0xf0f0f0f0;
  400. return 0;
  401. }
  402. extern int _amd_write_word_end;
  403. asm ("_amd_write_word_end:\n"
  404. ".long 0\n");
  405. /*-----------------------------------------------------------------------
  406. * Copy memory to flash, returns:
  407. * 0 - OK
  408. * 1 - write timeout
  409. * 2 - Flash not erased
  410. * 3 - Unsupported flash type
  411. */
  412. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  413. {
  414. ulong cp, wp, data;
  415. int i, l, rc;
  416. int flag;
  417. u32 (*_write_word_ptr)(unsigned start, unsigned dest, unsigned data);
  418. unsigned size;
  419. if ((info->flash_id & FLASH_VENDMASK) == (AMD_MANUFACT & FLASH_VENDMASK)) {
  420. size = (unsigned)&_amd_write_word_end - (unsigned)_amd_write_word;
  421. if (size > PROBE_BUFFER_SIZE) {
  422. printf("_amd_write_word() routine too large (%d) %p - %p\n",
  423. size, &_amd_write_word_end, _amd_write_word);
  424. return 0;
  425. }
  426. memcpy(buffer, _amd_write_word, size);
  427. _write_word_ptr = (void*)buffer;
  428. } else {
  429. printf ("Can't program unknown flash type - aborted\n");
  430. return 3;
  431. }
  432. wp = (addr & ~3); /* get lower word aligned address */
  433. /*
  434. * handle unaligned start bytes
  435. */
  436. if ((l = addr - wp) != 0) {
  437. data = 0;
  438. for (i=0, cp=wp; i<l; ++i, ++cp) {
  439. data |= (*(uchar *)cp) << (8*i);
  440. }
  441. for (; i<4 && cnt>0; ++i) {
  442. data |= *src++ << (8*i);
  443. --cnt;
  444. ++cp;
  445. }
  446. for (; cnt==0 && i<4; ++i, ++cp) {
  447. data |= (*(uchar *)cp) << (8*i);
  448. }
  449. /* Disable interrupts which might cause a timeout here */
  450. flag = disable_interrupts();
  451. rc = _write_word_ptr(info->start[0], wp, data);
  452. /* re-enable interrupts if necessary */
  453. if (flag) {
  454. enable_interrupts();
  455. }
  456. if (rc != 0) {
  457. return rc;
  458. }
  459. wp += 4;
  460. }
  461. /*
  462. * handle word aligned part
  463. */
  464. while (cnt >= 4) {
  465. data = 0;
  466. for (i=0; i<4; ++i) {
  467. data |= *src++ << (8*i);
  468. }
  469. /* Disable interrupts which might cause a timeout here */
  470. flag = disable_interrupts();
  471. rc = _write_word_ptr(info->start[0], wp, data);
  472. /* re-enable interrupts if necessary */
  473. if (flag) {
  474. enable_interrupts();
  475. }
  476. if (rc != 0) {
  477. return rc;
  478. }
  479. wp += 4;
  480. cnt -= 4;
  481. }
  482. if (cnt == 0) {
  483. return 0;
  484. }
  485. /*
  486. * handle unaligned tail bytes
  487. */
  488. data = 0;
  489. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  490. data |= *src++ << (8*i);
  491. --cnt;
  492. }
  493. for (; i<4; ++i, ++cp) {
  494. data |= (*(uchar *)cp) << (8*i);
  495. }
  496. /* Disable interrupts which might cause a timeout here */
  497. flag = disable_interrupts();
  498. rc = _write_word_ptr(info->start[0], wp, data);
  499. /* re-enable interrupts if necessary */
  500. if (flag) {
  501. enable_interrupts();
  502. }
  503. return rc;
  504. }