flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * MOUSSE/MPC8240 Board definitions.
  3. * Flash Routines for MOUSSE onboard AMD29LV106DB devices
  4. *
  5. * (C) Copyright 2000
  6. * Marius Groeger <mgroeger@sysgo.de>
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. *
  9. * (C) Copyright 2000
  10. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  11. *
  12. * (C) Copyright 1999, by Curt McDowell, 08-06-99, Broadcom Corp.
  13. * (C) Copyright 2001, James Dougherty, 07/18/01, Broadcom Corp.
  14. *
  15. * See file CREDITS for list of people who contributed to this
  16. * project.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  31. * MA 02111-1307 USA
  32. */
  33. #include <common.h>
  34. #include <mpc8xx.h>
  35. #include <malloc.h>
  36. #include "mousse.h"
  37. #include "flash.h"
  38. int flashLibDebug = 0;
  39. int flashLibInited = 0;
  40. #define OK 0
  41. #define ERROR -1
  42. #define STATUS int
  43. #define PRINTF if (flashLibDebug) printf
  44. #if 0
  45. #define PRIVATE static
  46. #else
  47. #define PRIVATE
  48. #endif
  49. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  50. #define SLEEP_DELAY 166
  51. #define FLASH_SECTOR_SIZE (64*1024)
  52. /***********************************************************************
  53. *
  54. * Virtual Flash Devices on Mousse board
  55. *
  56. * These must be kept in sync with the definitions in flashLib.h.
  57. *
  58. ***********************************************************************/
  59. PRIVATE flash_dev_t flashDev[] = {
  60. /* Bank 0 sector SA0 (16 kB) */
  61. { "SA0",FLASH0_BANK, FLASH0_SEG0_START, 1, 14,
  62. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  63. },
  64. /* Bank 0 sector SA1 (8 kB) */
  65. { "SA1", FLASH0_BANK, FLASH0_SEG0_START + 0x4000, 1, 13,
  66. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  67. },
  68. /* Bank 0 sector SA2 (8 kB) */
  69. { "SA2", FLASH0_BANK, FLASH0_SEG0_START + 0x6000, 1, 13,
  70. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  71. },
  72. /* Bank 0 sector SA3 is occluded by Mousse I/O devices */
  73. /* Bank 0 sectors SA4-SA18, after Mousse devices up to PLCC (960 kB) */
  74. { "KERNEL", FLASH0_BANK, FLASH0_SEG1_START, 15, 16,
  75. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  76. },
  77. /* Bank 0 sectors SA19-SA26, jumper can occlude this by PLCC (512 kB) */
  78. /* This is where the Kahlua boot vector and boot ROM code resides. */
  79. { "BOOT",FLASH0_BANK, FLASH0_SEG2_START, 8, 16,
  80. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  81. },
  82. /* Bank 0 sectors SA27-SA34 (512 kB) */
  83. { "RAMDISK",FLASH0_BANK, FLASH0_SEG3_START, 8, 16,
  84. FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
  85. },
  86. };
  87. int flashDevCount = (sizeof (flashDev) / sizeof (flashDev[0]));
  88. #define DEV(no) (&flashDev[no])
  89. #define DEV_NO(dev) ((dev) - flashDev)
  90. /***********************************************************************
  91. *
  92. * Private Flash Routines
  93. *
  94. ***********************************************************************/
  95. /*
  96. * The convention is:
  97. *
  98. * "addr" is always the PROM raw address, which is the address of an
  99. * 8-bit quantity for flash 0 and 16-bit quantity for flash 1.
  100. *
  101. * "pos" is always a logical byte position from the PROM beginning.
  102. */
  103. #define FLASH0_ADDR(dev, addr) \
  104. ((unsigned char *) ((dev)->base + (addr)))
  105. #define FLASH0_WRITE(dev, addr, value) \
  106. (*FLASH0_ADDR(dev, addr) = (value))
  107. #define FLASH0_READ(dev, addr) \
  108. (*FLASH0_ADDR(dev, addr))
  109. PRIVATE int flashCheck(flash_dev_t *dev)
  110. {
  111. if (! flashLibInited) {
  112. printf("flashCheck: flashLib not initialized\n");
  113. return ERROR;
  114. }
  115. if (dev < &flashDev[0] || dev >= &flashDev[flashDevCount]) {
  116. printf("flashCheck: Bad dev parameter\n");
  117. return ERROR;
  118. }
  119. if (! dev->found) {
  120. printf("flashCheck: Device %d not available\n", DEV_NO(dev));
  121. return ERROR;
  122. }
  123. return OK;
  124. }
  125. PRIVATE void flashReset(flash_dev_t *dev)
  126. {
  127. PRINTF("flashReset: dev=%d\n", DEV_NO(dev));
  128. if (dev->bank == FLASH0_BANK) {
  129. FLASH0_WRITE(dev, 0x555, 0xaa);
  130. FLASH0_WRITE(dev, 0xaaa, 0x55);
  131. FLASH0_WRITE(dev, 0x555, 0xf0);
  132. }
  133. udelay(SLEEP_DELAY);
  134. PRINTF("flashReset: done\n");
  135. }
  136. PRIVATE int flashProbe(flash_dev_t *dev)
  137. {
  138. int rv, deviceID, vendorID;
  139. PRINTF("flashProbe: dev=%d\n", DEV_NO(dev));
  140. if (dev->bank != FLASH0_BANK) {
  141. rv = ERROR;
  142. goto DONE;
  143. }
  144. FLASH0_WRITE(dev, 0xaaa, 0xaa);
  145. FLASH0_WRITE(dev, 0x555, 0x55);
  146. FLASH0_WRITE(dev, 0xaaa, 0x90);
  147. udelay(SLEEP_DELAY);
  148. vendorID = FLASH0_READ(dev, 0);
  149. deviceID = FLASH0_READ(dev, 2);
  150. FLASH0_WRITE(dev, 0, 0xf0);
  151. PRINTF("flashProbe: vendor=0x%x device=0x%x\n", vendorID, deviceID);
  152. if (vendorID == dev->vendorID && deviceID == dev->deviceID)
  153. rv = OK;
  154. else
  155. rv = ERROR;
  156. DONE:
  157. PRINTF("flashProbe: rv=%d\n", rv);
  158. return rv;
  159. }
  160. PRIVATE int flashWait(flash_dev_t *dev, int addr, int expect, int erase)
  161. {
  162. int rv = ERROR;
  163. int i, data;
  164. int polls;
  165. #if 0
  166. PRINTF("flashWait: dev=%d addr=0x%x expect=0x%x erase=%d\n",
  167. DEV_NO(dev), addr, expect, erase);
  168. #endif
  169. if (dev->bank != FLASH0_BANK) {
  170. rv = ERROR;
  171. goto done;
  172. }
  173. if (erase)
  174. polls = FLASH_ERASE_SECTOR_TIMEOUT; /* Ticks */
  175. else
  176. polls = FLASH_PROGRAM_POLLS; /* Loops */
  177. for (i = 0; i < polls; i++) {
  178. if (erase)
  179. udelay(SLEEP_DELAY);
  180. data = FLASH0_READ(dev, addr);
  181. if (((data ^ expect) & 0x80) == 0) {
  182. rv = OK;
  183. goto done;
  184. }
  185. if (data & 0x20) {
  186. /*
  187. * If the 0x20 bit has come on, it could actually be because
  188. * the operation succeeded, so check the done bit again.
  189. */
  190. data = FLASH0_READ(dev, addr);
  191. if (((data ^ expect) & 0x80) == 0) {
  192. rv = OK;
  193. goto done;
  194. }
  195. printf("flashWait: Program error (dev: %d, addr: 0x%x)\n",
  196. DEV_NO(dev), addr);
  197. flashReset(dev);
  198. rv = ERROR;
  199. goto done;
  200. }
  201. }
  202. printf("flashWait: Timeout %s (dev: %d, addr: 0x%x)\n",
  203. erase ? "erasing sector" : "programming byte",
  204. DEV_NO(dev), addr);
  205. done:
  206. #if 0
  207. PRINTF("flashWait: rv=%d\n", rv);
  208. #endif
  209. return rv;
  210. }
  211. /***********************************************************************
  212. *
  213. * Public Flash Routines
  214. *
  215. ***********************************************************************/
  216. STATUS flashLibInit(void)
  217. {
  218. int i;
  219. PRINTF("flashLibInit: devices=%d\n", flashDevCount);
  220. for (i = 0; i < flashDevCount; i++) {
  221. flash_dev_t *dev = &flashDev[i];
  222. /*
  223. * For bank 1, probe both without and with byte swappage,
  224. * so that this module works on both old and new Mousse boards.
  225. */
  226. flashReset(dev);
  227. if (flashProbe(dev) != ERROR)
  228. dev->found = 1;
  229. flashReset(dev);
  230. if (flashProbe(dev) != ERROR)
  231. dev->found = 1;
  232. dev->swap = 0;
  233. if(dev->found){
  234. PRINTF("\n FLASH %s[%d]: iobase=0x%x - %d sectors %d KB",
  235. flashDev[i].name,i,flashDev[i].base, flashDev[i].sectors,
  236. (flashDev[i].sectors * FLASH_SECTOR_SIZE)/1024);
  237. }
  238. }
  239. flashLibInited = 1;
  240. PRINTF("flashLibInit: done\n");
  241. return OK;
  242. }
  243. STATUS flashEraseSector(flash_dev_t *dev, int sector)
  244. {
  245. int pos, addr;
  246. PRINTF("flashErasesector: dev=%d sector=%d\n", DEV_NO(dev), sector);
  247. if (flashCheck(dev) == ERROR)
  248. return ERROR;
  249. if (sector < 0 || sector >= dev->sectors) {
  250. printf("flashEraseSector: Sector out of range (dev: %d, sector: %d)\n",
  251. DEV_NO(dev), sector);
  252. return ERROR;
  253. }
  254. pos = FLASH_SECTOR_POS(dev, sector);
  255. if (dev->bank != FLASH0_BANK) {
  256. return ERROR;
  257. }
  258. addr = pos;
  259. FLASH0_WRITE(dev, 0xaaa, 0xaa);
  260. FLASH0_WRITE(dev, 0x555, 0x55);
  261. FLASH0_WRITE(dev, 0xaaa, 0x80);
  262. FLASH0_WRITE(dev, 0xaaa, 0xaa);
  263. FLASH0_WRITE(dev, 0x555, 0x55);
  264. FLASH0_WRITE(dev, addr, 0x30);
  265. return flashWait(dev, addr, 0xff, 1);
  266. }
  267. /*
  268. * Note: it takes about as long to flash all sectors together with Chip
  269. * Erase as it does to flash them one at a time (about 30 seconds for 2
  270. * MB). Also since we want to be able to treat subsets of sectors as if
  271. * they were complete devices, we don't use Chip Erase.
  272. */
  273. STATUS flashErase(flash_dev_t *dev)
  274. {
  275. int sector;
  276. PRINTF("flashErase: dev=%d sectors=%d\n", DEV_NO(dev), dev->sectors);
  277. if (flashCheck(dev) == ERROR)
  278. return ERROR;
  279. for (sector = 0; sector < dev->sectors; sector++) {
  280. if (flashEraseSector(dev, sector) == ERROR)
  281. return ERROR;
  282. }
  283. return OK;
  284. }
  285. /*
  286. * Read and write bytes
  287. */
  288. STATUS flashRead(flash_dev_t *dev, int pos, char *buf, int len)
  289. {
  290. int addr, words;
  291. PRINTF("flashRead: dev=%d pos=0x%x buf=0x%x len=0x%x\n",
  292. DEV_NO(dev), pos, (int) buf, len);
  293. if (flashCheck(dev) == ERROR)
  294. return ERROR;
  295. if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS(dev)) {
  296. printf("flashRead: Position out of range "
  297. "(dev: %d, pos: 0x%x, len: 0x%x)\n",
  298. DEV_NO(dev), pos, len);
  299. return ERROR;
  300. }
  301. if (len == 0)
  302. return OK;
  303. if (dev->bank == FLASH0_BANK) {
  304. addr = pos;
  305. words = len;
  306. PRINTF("flashRead: memcpy(0x%x, 0x%x, 0x%x)\n",
  307. (int) buf, (int) FLASH0_ADDR(dev, pos), len);
  308. memcpy(buf, FLASH0_ADDR(dev, addr), words);
  309. }
  310. PRINTF("flashRead: rv=OK\n");
  311. return OK;
  312. }
  313. STATUS flashWrite(flash_dev_t *dev, int pos, char *buf, int len)
  314. {
  315. int addr, words;
  316. PRINTF("flashWrite: dev=%d pos=0x%x buf=0x%x len=0x%x\n",
  317. DEV_NO(dev), pos, (int) buf, len);
  318. if (flashCheck(dev) == ERROR)
  319. return ERROR;
  320. if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS(dev)) {
  321. printf("flashWrite: Position out of range "
  322. "(dev: %d, pos: 0x%x, len: 0x%x)\n",
  323. DEV_NO(dev), pos, len);
  324. return ERROR;
  325. }
  326. if (len == 0)
  327. return OK;
  328. if (dev->bank == FLASH0_BANK) {
  329. unsigned char tmp;
  330. addr = pos;
  331. words = len;
  332. while (words--) {
  333. tmp = *buf;
  334. if (~FLASH0_READ(dev, addr) & tmp) {
  335. printf("flashWrite: Attempt to program 0 to 1 "
  336. "(dev: %d, addr: 0x%x, data: 0x%x)\n",
  337. DEV_NO(dev), addr, tmp);
  338. return ERROR;
  339. }
  340. FLASH0_WRITE(dev, 0xaaa, 0xaa);
  341. FLASH0_WRITE(dev, 0x555, 0x55);
  342. FLASH0_WRITE(dev, 0xaaa, 0xa0);
  343. FLASH0_WRITE(dev, addr, tmp);
  344. if (flashWait(dev, addr, tmp, 0) < 0)
  345. return ERROR;
  346. buf++;
  347. addr++;
  348. }
  349. }
  350. PRINTF("flashWrite: rv=OK\n");
  351. return OK;
  352. }
  353. /*
  354. * flashWritable returns TRUE if a range contains all F's.
  355. */
  356. STATUS flashWritable(flash_dev_t *dev, int pos, int len)
  357. {
  358. int addr, words;
  359. int rv = ERROR;
  360. PRINTF("flashWritable: dev=%d pos=0x%x len=0x%x\n",
  361. DEV_NO(dev), pos, len);
  362. if (flashCheck(dev) == ERROR)
  363. goto done;
  364. if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS(dev)) {
  365. printf("flashWritable: Position out of range "
  366. "(dev: %d, pos: 0x%x, len: 0x%x)\n",
  367. DEV_NO(dev), pos, len);
  368. goto done;
  369. }
  370. if (len == 0) {
  371. rv = 1;
  372. goto done;
  373. }
  374. if (dev->bank == FLASH0_BANK) {
  375. addr = pos;
  376. words = len;
  377. while (words--) {
  378. if (FLASH0_READ(dev, addr) != 0xff) {
  379. rv = 0;
  380. goto done;
  381. }
  382. addr++;
  383. }
  384. }
  385. rv = 1;
  386. done:
  387. PRINTF("flashWrite: rv=%d\n", rv);
  388. return rv;
  389. }
  390. /*
  391. * NOTE: the below code cannot run from FLASH!!!
  392. */
  393. /***********************************************************************
  394. *
  395. * Flash Diagnostics
  396. *
  397. ***********************************************************************/
  398. STATUS flashDiag(flash_dev_t *dev)
  399. {
  400. unsigned int *buf = 0;
  401. int i, len, sector;
  402. int rv = ERROR;
  403. if (flashCheck(dev) == ERROR)
  404. return ERROR;
  405. printf("flashDiag: Testing device %d, "
  406. "base: 0x%x, %d sectors @ %d kB = %d kB\n",
  407. DEV_NO(dev), dev->base,
  408. dev->sectors,
  409. 1 << (dev->lgSectorSize - 10),
  410. dev->sectors << (dev->lgSectorSize - 10));
  411. len = 1 << dev->lgSectorSize;
  412. printf("flashDiag: Erasing\n");
  413. if (flashErase(dev) == ERROR) {
  414. printf("flashDiag: Erase failed\n");
  415. goto done;
  416. }
  417. printf("%d bytes requested ...\n", len);
  418. buf = malloc(len);
  419. printf("allocated %d bytes ...\n", len);
  420. if (buf == 0) {
  421. printf("flashDiag: Out of memory\n");
  422. goto done;
  423. }
  424. /*
  425. * Write unique counting pattern to each sector
  426. */
  427. for (sector = 0; sector < dev->sectors; sector++) {
  428. printf("flashDiag: Write sector %d\n", sector);
  429. for (i = 0; i < len / 4; i++)
  430. buf[i] = sector << 24 | i;
  431. if (flashWrite(dev,
  432. sector << dev->lgSectorSize,
  433. (char *) buf,
  434. len) == ERROR) {
  435. printf("flashDiag: Write failed (dev: %d, sector: %d)\n",
  436. DEV_NO(dev), sector);
  437. goto done;
  438. }
  439. }
  440. /*
  441. * Verify
  442. */
  443. for (sector = 0; sector < dev->sectors; sector++) {
  444. printf("flashDiag: Verify sector %d\n", sector);
  445. if (flashRead(dev,
  446. sector << dev->lgSectorSize,
  447. (char *) buf,
  448. len) == ERROR) {
  449. printf("flashDiag: Read failed (dev: %d, sector: %d)\n",
  450. DEV_NO(dev), sector);
  451. goto done;
  452. }
  453. for (i = 0; i < len / 4; i++) {
  454. if (buf[i] != (sector << 24 | i)) {
  455. printf("flashDiag: Verify error "
  456. "(dev: %d, sector: %d, offset: 0x%x)\n",
  457. DEV_NO(dev), sector, i);
  458. printf("flashDiag: Expected 0x%08x, got 0x%08x\n",
  459. sector << 24 | i, buf[i]);
  460. goto done;
  461. }
  462. }
  463. }
  464. printf("flashDiag: Erasing\n");
  465. if (flashErase(dev) == ERROR) {
  466. printf("flashDiag: Final erase failed\n");
  467. goto done;
  468. }
  469. rv = OK;
  470. done:
  471. if (buf)
  472. free(buf);
  473. if (rv == OK)
  474. printf("flashDiag: Device %d passed\n", DEV_NO(dev));
  475. else
  476. printf("flashDiag: Device %d failed\n", DEV_NO(dev));
  477. return rv;
  478. }
  479. STATUS flashDiagAll(void)
  480. {
  481. int i;
  482. int rv = OK;
  483. PRINTF("flashDiagAll: devices=%d\n", flashDevCount);
  484. for (i = 0; i < flashDevCount; i++) {
  485. flash_dev_t *dev = &flashDev[i];
  486. if (dev->found && flashDiag(dev) == ERROR)
  487. rv = ERROR;
  488. }
  489. if (rv == OK)
  490. printf("flashDiagAll: Passed\n");
  491. else
  492. printf("flashDiagAll: Failed because of earlier errors\n");
  493. return OK;
  494. }
  495. /*-----------------------------------------------------------------------
  496. */
  497. unsigned long flash_init (void)
  498. {
  499. unsigned long size = 0;
  500. flash_dev_t *dev = NULL;
  501. flashLibInit();
  502. /*
  503. * Provide info for FLASH (up to 960K) of Kernel Image data.
  504. */
  505. dev = FLASH_DEV_BANK0_LOW;
  506. flash_info[FLASH_BANK_KERNEL].flash_id =
  507. (dev->vendorID << 16) | dev->deviceID;
  508. flash_info[FLASH_BANK_KERNEL].sector_count = dev->sectors;
  509. flash_info[FLASH_BANK_KERNEL].size =
  510. flash_info[FLASH_BANK_KERNEL].sector_count * FLASH_SECTOR_SIZE;
  511. flash_info[FLASH_BANK_KERNEL].start[FIRST_SECTOR] = dev->base;
  512. size += flash_info[FLASH_BANK_KERNEL].size;
  513. /*
  514. * Provide info for 512K PLCC FLASH ROM (U-Boot)
  515. */
  516. dev = FLASH_DEV_BANK0_BOOT;
  517. flash_info[FLASH_BANK_BOOT].flash_id =
  518. (dev->vendorID << 16) | dev->deviceID;
  519. flash_info[FLASH_BANK_BOOT].sector_count = dev->sectors;
  520. flash_info[FLASH_BANK_BOOT].size =
  521. flash_info[FLASH_BANK_BOOT].sector_count * FLASH_SECTOR_SIZE;
  522. flash_info[FLASH_BANK_BOOT].start[FIRST_SECTOR] = dev->base;
  523. size += flash_info[FLASH_BANK_BOOT].size;
  524. /*
  525. * Provide info for 512K FLASH0 segment (U-Boot)
  526. */
  527. dev = FLASH_DEV_BANK0_HIGH;
  528. flash_info[FLASH_BANK_AUX].flash_id =
  529. (dev->vendorID << 16) | dev->deviceID;
  530. flash_info[FLASH_BANK_AUX].sector_count = dev->sectors;
  531. flash_info[FLASH_BANK_AUX].size =
  532. flash_info[FLASH_BANK_AUX].sector_count * FLASH_SECTOR_SIZE;
  533. flash_info[FLASH_BANK_AUX].start[FIRST_SECTOR] = dev->base;
  534. size += flash_info[FLASH_BANK_AUX].size;
  535. return size;
  536. }
  537. /*
  538. * Get flash device from U-Boot flash info.
  539. */
  540. flash_dev_t*
  541. getFlashDevFromInfo(flash_info_t* info)
  542. {
  543. int i;
  544. if(!info)
  545. return NULL;
  546. for (i = 0; i < flashDevCount; i++) {
  547. flash_dev_t *dev = &flashDev[i];
  548. if(dev->found && (dev->base == info->start[0]))
  549. return dev;
  550. }
  551. printf("ERROR: notice, no FLASH mapped at address 0x%x\n",
  552. (unsigned int)info->start[0]);
  553. return NULL;
  554. }
  555. ulong
  556. flash_get_size (vu_long *addr, flash_info_t *info)
  557. {
  558. int i;
  559. for(i = 0; i < flashDevCount; i++) {
  560. flash_dev_t *dev = &flashDev[i];
  561. if(dev->found){
  562. if(dev->base == (unsigned int)addr){
  563. info->flash_id = (dev->vendorID << 16) | dev->deviceID;
  564. info->sector_count = dev->sectors;
  565. info->size = info->sector_count * FLASH_SECTOR_SIZE;
  566. return dev->sectors * FLASH_SECTOR_SIZE;
  567. }
  568. }
  569. }
  570. return 0;
  571. }
  572. void
  573. flash_print_info (flash_info_t *info)
  574. {
  575. int i;
  576. unsigned int chip;
  577. if (info->flash_id == FLASH_UNKNOWN) {
  578. printf ("missing or unknown FLASH type\n");
  579. return;
  580. }
  581. switch ((info->flash_id >> 16) & 0xff) {
  582. case 0x1:
  583. printf ("AMD ");
  584. break;
  585. default:
  586. printf ("Unknown Vendor ");
  587. break;
  588. }
  589. chip = (unsigned int) info->flash_id & 0x000000ff;
  590. switch (chip) {
  591. case AMD_ID_F040B:
  592. printf ("AM29F040B (4 Mbit)\n");
  593. break;
  594. case AMD_ID_LV160B:
  595. case FLASH_AM160LV:
  596. case 0x49:
  597. printf ("AM29LV160B (16 Mbit / 2M x 8bit)\n");
  598. break;
  599. default:
  600. printf ("Unknown Chip Type:0x%x\n", chip);
  601. break;
  602. }
  603. printf (" Size: %ld bytes in %d Sectors\n",
  604. info->size, info->sector_count);
  605. printf (" Sector Start Addresses:");
  606. for (i=0; i<info->sector_count; ++i) {
  607. if ((i % 5) == 0)
  608. printf ("\n ");
  609. printf (" %08lX%s",
  610. info->start[FIRST_SECTOR] + i*FLASH_SECTOR_SIZE,
  611. info->protect[i] ? " (RO)" : " "
  612. );
  613. }
  614. printf ("\n");
  615. }
  616. /*
  617. * Erase a range of flash sectors.
  618. */
  619. int flash_erase (flash_info_t *info, int s_first, int s_last)
  620. {
  621. vu_long *addr = (vu_long*)(info->start[0]);
  622. int prot, sect, l_sect;
  623. flash_dev_t* dev = NULL;
  624. if ((s_first < 0) || (s_first > s_last)) {
  625. if (info->flash_id == FLASH_UNKNOWN) {
  626. printf ("- missing\n");
  627. } else {
  628. printf ("- no sectors to erase\n");
  629. }
  630. return 1;
  631. }
  632. prot = 0;
  633. for (sect = s_first; sect <= s_last; sect++) {
  634. if (info->protect[sect]) {
  635. prot++;
  636. }
  637. }
  638. if (prot) {
  639. printf ("- Warning: %d protected sectors will not be erased!\n",
  640. prot);
  641. } else {
  642. printf ("\n");
  643. }
  644. l_sect = -1;
  645. /* Start erase on unprotected sectors */
  646. dev = getFlashDevFromInfo(info);
  647. if(dev){
  648. printf("Erase FLASH[%s] -%d sectors:", dev->name, dev->sectors);
  649. for (sect = s_first; sect<=s_last; sect++) {
  650. if (info->protect[sect] == 0) { /* not protected */
  651. addr = (vu_long*)(dev->base);
  652. /* printf("erase_sector: sector=%d, addr=0x%x\n",
  653. sect, addr); */
  654. printf(".");
  655. if(ERROR == flashEraseSector(dev, sect)){
  656. printf("ERROR: could not erase sector %d on FLASH[%s]\n",
  657. sect, dev->name);
  658. return 1;
  659. }
  660. }
  661. }
  662. }
  663. printf (" done\n");
  664. return 0;
  665. }
  666. /*-----------------------------------------------------------------------
  667. * Write a word to Flash, returns:
  668. * 0 - OK
  669. * 1 - write timeout
  670. * 2 - Flash not erased
  671. */
  672. static int
  673. write_word (flash_info_t *info, ulong dest, ulong data)
  674. {
  675. flash_dev_t* dev = getFlashDevFromInfo(info);
  676. int addr = dest - info->start[0];
  677. if (! dev)
  678. return 1;
  679. if(OK != flashWrite(dev, addr, (char*)&data, sizeof(ulong))){
  680. printf("ERROR: could not write to addr=0x%x, data=0x%x\n",
  681. (unsigned int)addr, (unsigned)data);
  682. return 1;
  683. }
  684. if((addr % FLASH_SECTOR_SIZE) == 0)
  685. printf(".");
  686. PRINTF("write_word:0x%x, base=0x%x, addr=0x%x, data=0x%x\n",
  687. (unsigned)info->start[0],
  688. (unsigned)dest,
  689. (unsigned)(dest - info->start[0]),
  690. (unsigned)data);
  691. return (0);
  692. }
  693. /*-----------------------------------------------------------------------
  694. * Copy memory to flash, returns:
  695. * 0 - OK
  696. * 1 - write timeout
  697. * 2 - Flash not erased
  698. */
  699. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  700. {
  701. ulong cp, wp, data;
  702. int i, l, rc;
  703. flash_dev_t* dev = getFlashDevFromInfo(info);
  704. if( dev ) {
  705. printf("FLASH[%s]:", dev->name);
  706. wp = (addr & ~3); /* get lower word aligned address */
  707. /*
  708. * handle unaligned start bytes
  709. */
  710. if ((l = addr - wp) != 0) {
  711. data = 0;
  712. for (i=0, cp=wp; i<l; ++i, ++cp) {
  713. data = (data << 8) | (*(uchar *)cp);
  714. }
  715. for (; i<4 && cnt>0; ++i) {
  716. data = (data << 8) | *src++;
  717. --cnt;
  718. ++cp;
  719. }
  720. for (; cnt==0 && i<4; ++i, ++cp) {
  721. data = (data << 8) | (*(uchar *)cp);
  722. }
  723. if ((rc = write_word(info, wp, data)) != 0) {
  724. return (rc);
  725. }
  726. wp += 4;
  727. }
  728. /*
  729. * handle word aligned part
  730. */
  731. while (cnt >= 4) {
  732. data = 0;
  733. for (i=0; i<4; ++i) {
  734. data = (data << 8) | *src++;
  735. }
  736. if ((rc = write_word(info, wp, data)) != 0) {
  737. return (rc);
  738. }
  739. wp += 4;
  740. cnt -= 4;
  741. }
  742. if (cnt == 0) {
  743. return (0);
  744. }
  745. /*
  746. * handle unaligned tail bytes
  747. */
  748. data = 0;
  749. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  750. data = (data << 8) | *src++;
  751. --cnt;
  752. }
  753. for (; i<4; ++i, ++cp) {
  754. data = (data << 8) | (*(uchar *)cp);
  755. }
  756. return (write_word(info, wp, data));
  757. }
  758. return 1;
  759. }
  760. /*-----------------------------------------------------------------------
  761. */