flashdrv.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:49k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* flashdrv.c - FLASH memory functions and definitions*/
  2. /* Copyright Galileo Technology. */
  3. /*
  4. DESCRIPTION
  5. This flash driver gives the user a convenient interface to FLASH memory located
  6. on the user`s board, it supports various layout configurations such as:
  7. 1. One pure 8 bit device (Such as AMD`s AM29LV040B).
  8. 2. 1,2,4 or 8 devices 16 bit wide configured to operate in 8 bit mode.
  9. 3. 1,2 or 4 devices each 16 bit wide.
  10. Before using the driver you must call the initialization function at least once
  11. or when ever you are changing the FLASH base address.
  12. The list bellow contains the supported FLASH memory devices, new devices can be
  13. added easily in the future.
  14. */
  15. /*includes*/
  16. #ifdef __linux__
  17. #include <asm/galileo-boards/evb64120A/flashdrv.h>
  18. #else
  19. #include "flashdrv.h"
  20. #endif
  21. /* locals */
  22. #ifdef __MIPSEB__ // skranz, add
  23. #define BE // skranz, add
  24. #endif // skranz, add
  25. /******************************************************************************
  26. * Those two tables contain the supported flash devices information needed by
  27. * the driver:
  28. * The first table "flashParametrs" starts with 10 shared fields
  29. *  (currently 6 are reserved):
  30. *   index 0 => Pointer to an entry in the second table list
  31. *   index 1 => baseAddress - Flash memory device base address.
  32. *   index 2 => width - 1, 2, 4 or 8 Bytes.
  33. *   index 3 => mode - PURE8, X8 or X16 flash configuration (for X16 devices only)
  34. * The second table (flashTypes) contains:
  35. * Entry`s structure:
  36. *   Manufacture ID,Device ID,number of sectors,list of sector`s sizes
  37. *   (in Kbytes starting with sector number 0).
  38. * The end of the list is pointed with a zero.
  39. ******************************************************************************/
  40. unsigned int flashParametrs[10]; /* 0  Entry pointer */
  41.  /* 0  Base address  */
  42.  /* 0  Width         */
  43.  /* 0  Mode          */
  44.  /* 0,0,0,0,0,0, spare entries. */
  45. unsigned int flashTypes[] = {
  46. /* 0 */ AMD_FLASH, AM29F400BB, 11, 16, 8, 8, 32, 64, 64, 64, 64,
  47.     64, 64, 64,
  48. /* 1 */ AMD_FLASH, AM29F400BT, 11, 64, 64, 64, 64, 64, 64, 64, 32,
  49.     8, 8, 16,
  50. /* 2 */ ST_FLASH, M29W040, 8, 64, 64, 64, 64, 64, 64, 64, 64,
  51. /* 3 */ AMD_FLASH, AM29LV040B, 8, 64, 64, 64, 64, 64, 64, 64, 64,
  52. /* 4 */ AMD_FLASH, AM29LV800BT, 19, 64, 64, 64, 64, 64, 64, 64, 64,
  53.     64, 64,
  54. 64, 64, 64, 64, 64, 32, 8, 8, 16,
  55. /* 5 */ INTEL_FLASH, I28F320J3A, 32, 128, 128, 128, 128, 128, 128,
  56.     128, 128,
  57. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  58. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  59. 128, 128, 128, 128,
  60. /* 6 */ INTEL_FLASH, I28F640J3A, 64, 128, 128, 128, 128, 128, 128,
  61.     128, 128,
  62. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  63. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  64. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  65. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  66. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  67. 128, 128, 128, 128, 128, 128,
  68. /* 7 */ INTEL_FLASH, I28F128J3A, 128, 128, 128, 128, 128, 128, 128,
  69.     128, 128,
  70. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  71. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  72. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  73. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  74. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  75. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  76. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  77. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  78. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  79. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  80. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  81. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  82. /* 8 */ AMD_FLASH, AM29LV400BB, 11, 16, 8, 8, 32, 64, 64, 64, 64,
  83.     64, 64, 64,
  84. /* 9 */ AMD_FLASH, AM29LV400BT, 11, 64, 64, 64, 64, 64, 64, 64, 32,
  85.     8, 8, 16,
  86. /* 10 */ INTEL_FLASH, I28F320B3_T, 71, 64, 64, 64, 64, 64, 64, 64,
  87.     64, 64,
  88. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  89. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  90. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  91. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  92. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  93. 64, 64, 64, 64, 8, 8, 8, 8, 8, 8, 8, 8,
  94. /* 11 */ INTEL_FLASH, I28F320B3_B, 71, 8, 8, 8, 8, 8, 8, 8, 8, 64,
  95.     64, 64,
  96. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  97. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  98. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  99. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  100. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  101. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  102. /* 12 */ INTEL_FLASH, I28F160B3_B, 39, 8, 8, 8, 8, 8, 8, 8, 8, 64,
  103.     64, 64,
  104. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  105. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  106. 64, 64, 64, 64, 64, 64, 64, 64,
  107. /* 13 */ INTEL_FLASH, I28F160B3_T, 39, 64, 64, 64, 64, 64, 64, 64,
  108.     64, 64,
  109. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  110. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  111. 64, 64, 8, 8, 8, 8, 8, 8, 8, 8,
  112. 0 /* End of list indicator */
  113. };
  114. /********************************************************************
  115. * flashInit - Initializes the FLASH memory driver`s parameters, this function
  116. *             must be called at least once before using the FLASH memory.
  117. *             If you are changing the FLASH base address call this function
  118. *             again.
  119. *
  120. * INPUTS:     unsigned int flashBaseAddress - The flash base Address.
  121. *             unsigned int flashWidth - Flash bus width in Bytes: 1,2,4 or 8.
  122. *             flashMode - PURE8, X8 or X16.
  123. * RETURNS:    Flash Size, zero when operation (flashInit) failed.
  124. *********************************************************************/
  125. unsigned int flashInit(unsigned int flashBaseAddress,
  126.        unsigned int flashWidth, FLASHmode flashMode)
  127. {
  128. unsigned short mfrId = 0;
  129. unsigned short devId = 0xffff;
  130. unsigned int FirstAddr, SecondAddr, ThirdAddr;
  131. unsigned int pArray = 0;
  132. unsigned int counter;
  133. unsigned int flashSize = 0;
  134. /* update the list with relevant parametrs */
  135. flashParametrs[0] = 0; /* Default initialization */
  136. flashParametrs[1] = flashBaseAddress;
  137. flashParametrs[2] = flashWidth;
  138. flashParametrs[3] = flashMode;
  139. /* Get the FLASH`s ID */
  140. switch (FLASH_WIDTH) {
  141. case 1:
  142. /* AMD or ST ?? * */
  143. if (flashMode == PURE8) { /* Boot Flash */
  144. FirstAddr = 0x5555;
  145. SecondAddr = 0x2aaa;
  146. ThirdAddr = 0x5555;
  147. } else { /* X16 device configured to 8bit Mode */
  148. FirstAddr = 0xaaaa;
  149. SecondAddr = 0x5555;
  150. ThirdAddr = 0xaaaa;
  151. }
  152. flashReset();
  153. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xAA);
  154. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  155. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0x90);
  156. READ_CHAR(FLASH_BASE_ADDRESS + 0x0, &mfrId);
  157. if (mfrId == AMD_FLASH || mfrId == ST_FLASH) {
  158. flashReset();
  159. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xAA);
  160. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  161. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0x90);
  162. READ_CHAR(FLASH_BASE_ADDRESS + 0x1, &devId);
  163. break;
  164. }
  165. /* Micron or Intel ?? * */
  166. WRITE_CHAR(FLASH_BASE_ADDRESS, 0xff); /* Read Array */
  167. /* Flash reset for Intel/Micron */
  168. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x90); /* IDENTIFY Device */
  169. READ_CHAR(FLASH_BASE_ADDRESS + 0x0, &mfrId); /*Address for ManufactureID */
  170. if (mfrId == INTEL_FLASH || mfrId == MICRON_FLASH) {
  171. WRITE_CHAR(FLASH_BASE_ADDRESS, 0xff); /* Read Array */
  172. /*Flash reset for Intel/Micron */
  173. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x90); /* IDENTIFY Device */
  174. READ_CHAR(FLASH_BASE_ADDRESS + 0x1, &devId); /*Address for DeviceID */
  175. }
  176. break;
  177. case 2:
  178. case 4:
  179. case 8:
  180. /* AMD or ST ??? */
  181. flashReset();
  182. WRITE_SHORT(FLASH_BASE_ADDRESS + 0x5555 * FLASH_WIDTH,
  183.     0xaa);
  184. WRITE_SHORT(FLASH_BASE_ADDRESS + 0x2aaa * FLASH_WIDTH,
  185.     0x55);
  186. WRITE_SHORT(FLASH_BASE_ADDRESS + 0x5555 * FLASH_WIDTH,
  187.     0x90);
  188. READ_SHORT(FLASH_BASE_ADDRESS, &mfrId);
  189. flashReset();
  190. /* Read the device ID */
  191. if (mfrId == AMD_FLASH || mfrId == ST_FLASH) {
  192. WRITE_SHORT(FLASH_BASE_ADDRESS +
  193.     0x5555 * FLASH_WIDTH, 0xaa);
  194. WRITE_SHORT(FLASH_BASE_ADDRESS +
  195.     0x2aaa * FLASH_WIDTH, 0x55);
  196. WRITE_SHORT(FLASH_BASE_ADDRESS +
  197.     0x5555 * FLASH_WIDTH, 0x90);
  198. READ_SHORT(FLASH_BASE_ADDRESS + 0x1 * FLASH_WIDTH,
  199.    &devId);
  200. break;
  201. }
  202. /* Micron or Intel ?? * */
  203. WRITE_WORD(FLASH_BASE_ADDRESS, 0x00ff00ff);
  204. WRITE_WORD(FLASH_BASE_ADDRESS, 0x00900090);
  205. if ((FLASH_WIDTH == 4) || (FLASH_WIDTH == 8)) { /* 32 or 64 bit */
  206. READ_SHORT(FLASH_BASE_ADDRESS, &mfrId);
  207. } else { /* FLASH_WIDTH = 2 */
  208. READ_SHORT(FLASH_BASE_ADDRESS, &mfrId);
  209. }
  210. if ((mfrId == INTEL_FLASH) || (mfrId == MICRON_FLASH)) {
  211. /* Flash reset for Intel/Micron */
  212. flashReset();
  213. WRITE_WORD(FLASH_BASE_ADDRESS, 0x00ff00ff);
  214. WRITE_WORD(FLASH_BASE_ADDRESS, 0x00900090);
  215. READ_SHORT(FLASH_BASE_ADDRESS + 0x1 * FLASH_WIDTH,
  216.    &devId);
  217. }
  218. break;
  219. }
  220. /* Try to locate the device in the supported flashes list (FLASH_TYPE).
  221.    according to the keys:
  222.    1) mfrId - manufactor ID.
  223.    2) devId - device ID.
  224.  */
  225. while (true) {
  226. if (flashTypes[pArray] == 0) {
  227. flashReset();
  228. return 0; /* Device not in the list */
  229. }
  230. if ((flashTypes[pArray] == mfrId) &&
  231.     (flashTypes[pArray + 1] == devId)) {
  232. POINTER_TO_FLASH = pArray;
  233. for (counter = 0;
  234.      counter < flashTypes[NUMBER_OF_SECTORS];
  235.      counter++) {
  236. flashSize =
  237.     flashSize +
  238.     flashTypes[FIRST_SECTOR_SIZE +
  239.        counter];
  240. }
  241. if (FLASH_MODE != PURE8) {
  242. flashReset();
  243. return (flashSize * _1K *
  244. (FLASH_WIDTH / (FLASH_MODE / 8)));
  245. } else {
  246. flashReset();
  247. return (flashSize * _1K * FLASH_WIDTH);
  248. }
  249. }
  250. pArray += (3 + flashTypes[pArray + 2]); /* Move to next entry */
  251. }
  252. }
  253. /********************************************************************
  254. * flashReset - Resets the Flash memory (FLASH`s internal protocol reset).
  255. *
  256. * INTPUTS:  N/A
  257. * OUTPUT:   N/A
  258. *********************************************************************/
  259. void flashReset()
  260. {
  261. unsigned char ucData;
  262. unsigned short usData;
  263. unsigned int uiData;
  264. if ((flashTypes[POINTER_TO_FLASH] == AMD_FLASH) ||
  265.     (flashTypes[POINTER_TO_FLASH]) == ST_FLASH) {
  266. if (FLASH_MODE == X16) {
  267. ucData = 0xf0;
  268. usData = 0xf0;
  269. uiData = 0x00f000f0;
  270. } else { /* case of PURE8 or X8 */
  271. ucData = 0xf0;
  272. usData = 0xf0f0;
  273. uiData = 0xf0f0f0f0;
  274. }
  275. } else {
  276. if (FLASH_MODE == X16) {
  277. ucData = 0xff;
  278. usData = 0xff;
  279. uiData = 0x00ff00ff;
  280. } else { /* case of PURE8 or X8 */
  281. ucData = 0xff;
  282. usData = 0xffff;
  283. uiData = 0xffffffff;
  284. }
  285. }
  286. switch (FLASH_WIDTH) {
  287. case 1:
  288. WRITE_CHAR(FLASH_BASE_ADDRESS, ucData);
  289. break;
  290. case 2:
  291. WRITE_SHORT(FLASH_BASE_ADDRESS, usData);
  292. break;
  293. case 4:
  294. WRITE_WORD(FLASH_BASE_ADDRESS, uiData);
  295. break;
  296. case 8:
  297. WRITE_WORD(FLASH_BASE_ADDRESS, uiData);
  298. WRITE_WORD(FLASH_BASE_ADDRESS + 0x4, uiData);
  299. break;
  300. }
  301. }
  302. /********************************************************************
  303. * flashErase - The function erases the WHOLE flash memory.
  304. *
  305. *
  306. * RETURNS: true on success,false on failure
  307. *********************************************************************/
  308. bool flashErase()
  309. {
  310. unsigned int totalFlashSize;
  311. unsigned int address;
  312. unsigned int readData;
  313. unsigned int nextSector;
  314. flashReset();
  315. totalFlashSize = flashGetSize();
  316. /* scan all flash memory space. */
  317. address = 0;
  318. while (address < totalFlashSize) {
  319. readData = flashReadWord(address);
  320. if (readData != 0xffffffff) { /* offset with dirty data. */
  321. flashEraseSector(flashInWhichSector(address));
  322. nextSector = flashInWhichSector(address) + 1;
  323. if (nextSector < flashTypes[NUMBER_OF_SECTORS])
  324. /* jump to next sector. */
  325. address = flashGetSectorOffset(nextSector);
  326. else
  327. /* end of erasing. */
  328. address = totalFlashSize;
  329. } else
  330. address += 4;
  331. }
  332. return true;
  333. }
  334. /********************************************************************
  335. * flashEraseSector - The function erases a specific sector in the flash memory.
  336. *
  337. * INPUTS:  Sector number.
  338. * RETURNS: true on success,false on failure.
  339. *********************************************************************/
  340. bool flashEraseSector(unsigned int sectorNumber)
  341. {
  342. volatile unsigned int spin;
  343. unsigned int regValue;
  344. unsigned int sectorBaseAddress = 0;
  345. unsigned int i;
  346. unsigned int data20, dataD0, data70;
  347. unsigned int dataPoll;
  348. unsigned int FirstAddr, SecondAddr, ThirdAddr, FourthAddr,
  349.     FifthAddr;
  350. unsigned int FirstData, SecondData, ThirdData;
  351. unsigned int FourthData, FifthData, SixthData;
  352. /* calculate the sector base Address according to the following parametrs:
  353.    1: FLASH_WIDTH
  354.    2: the size of each sector which it detailed in the table */
  355. /* checking the if the sectorNumber is legal. */
  356. if (sectorNumber > flashTypes[NUMBER_OF_SECTORS] - 1)
  357. return false;
  358. /* now the calculation begining of the sector Address */
  359. for (i = 0; i < sectorNumber; i++) {
  360. sectorBaseAddress =
  361.     sectorBaseAddress + flashTypes[FIRST_SECTOR_SIZE + i];
  362. }
  363. /* In case of X8 wide the address should be */
  364. if (FLASH_MODE == PURE8)
  365. sectorBaseAddress = _1K * sectorBaseAddress;
  366. if (FLASH_MODE == X8)
  367. sectorBaseAddress = _1K * sectorBaseAddress;
  368. /* In case of X16 wide the address should be */
  369. if (FLASH_MODE == X16)
  370. sectorBaseAddress = _1K * sectorBaseAddress / 2;
  371. flashReset();
  372. if ((flashTypes[POINTER_TO_FLASH] == AMD_FLASH) || 
  373.     (flashTypes[POINTER_TO_FLASH] == ST_FLASH)) {
  374. switch (FLASH_WIDTH) {
  375. case 1:
  376. if (FLASH_MODE == PURE8) { /* Boot Flash PURE8 */
  377. FirstAddr = 0x5555;
  378. SecondAddr = 0x2aaa;
  379. ThirdAddr = 0x5555;
  380. FourthAddr = 0x5555;
  381. FifthAddr = 0x2aaa;
  382. } else {
  383. FirstAddr = 0xaaaa;
  384. SecondAddr = 0x5555;
  385. ThirdAddr = 0xaaaa;
  386. FourthAddr = 0xaaaa;
  387. FifthAddr = 0x5555;
  388. }
  389. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xAA);
  390. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  391. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0x80);
  392. WRITE_CHAR(FLASH_BASE_ADDRESS + FourthAddr, 0xAA);
  393. WRITE_CHAR(FLASH_BASE_ADDRESS + FifthAddr, 0x55);
  394. WRITE_CHAR(
  395.    (FLASH_BASE_ADDRESS +
  396.     (sectorBaseAddress & 0xffffff00)),
  397.    0x30);
  398. /* Poll on the flash */
  399. do {
  400. READ_CHAR(FLASH_BASE_ADDRESS +
  401.   sectorBaseAddress, &regValue);
  402. } while ((regValue & 0x80) != 0x80);
  403. break;
  404. case 2:
  405. if (FLASH_MODE == X16) {
  406. FirstData = 0xaa; /* Data for the First  Cycle */
  407. SecondData = 0x55; /* Data for the Second Cycle */
  408. ThirdData = 0x80; /* Data for the Third  Cycle */
  409. FourthData = 0xaa; /* Data for the Fourth Cycle */
  410. FifthData = 0x55; /* Data for the Fifth  Cycle */
  411. SixthData = 0x30; /* Data for the Sixth  Cycle */
  412. FirstAddr = 0x5555; /* Address for the First  Cycle */
  413. SecondAddr = 0x2aaa; /* Address for the Second Cycle */
  414. ThirdAddr = 0x5555; /* Address for the Third  Cycle */
  415. FourthAddr = 0x5555; /* Address for the Fourth Cycle */
  416. FifthAddr = 0x2aaa; /* Address for the Fifth  Cycle */
  417. } else { /* (FLASH_MODE = 8) */
  418. FirstData = 0xaaaa; /* Data for the First  Cycle */
  419. SecondData = 0x5555; /* Data for the Second Cycle */
  420. ThirdData = 0x8080; /* Data for the Third  Cycle */
  421. FourthData = 0xaaaa; /* Data for the Fourth Cycle */
  422. FifthData = 0x5555; /* Data for the Fifth  Cycle */
  423. SixthData = 0x3030; /* Data for the Sixth  Cycle */
  424. FirstAddr = 0xaaaa; /* Address for the First  Cycle */
  425. SecondAddr = 0x5555; /* Address for the Second Cycle */
  426. ThirdAddr = 0xaaaa; /* Address for the Third  Cycle */
  427. FourthAddr = 0xaaaa; /* Address for the Fourth Cycle */
  428. FifthAddr = 0x5555; /* Address for the Fifth  Cycle */
  429. }
  430. WRITE_SHORT(FLASH_BASE_ADDRESS +
  431.     FirstAddr * FLASH_WIDTH, FirstData);
  432. WRITE_SHORT(FLASH_BASE_ADDRESS +
  433.     SecondAddr * FLASH_WIDTH, SecondData);
  434. WRITE_SHORT(FLASH_BASE_ADDRESS +
  435.     ThirdAddr * FLASH_WIDTH, ThirdData);
  436. WRITE_SHORT(FLASH_BASE_ADDRESS +
  437.     FourthAddr * FLASH_WIDTH, FourthData);
  438. WRITE_SHORT(FLASH_BASE_ADDRESS +
  439.     FifthAddr * FLASH_WIDTH, FifthData);
  440. WRITE_SHORT(FLASH_BASE_ADDRESS +
  441.     (sectorBaseAddress & 0xffffff00) *
  442.     FLASH_WIDTH, SixthData);
  443. /* Poll on the flash */
  444. if (FLASH_MODE == X16) { /* 1 device of 16 bit */
  445. dataPoll = 0x0080;
  446. } else { /* (FLASH_MODE = 8) ==> 2 devices , 8 bit each => 16bit */
  447. dataPoll = 0x8080;
  448. }
  449. do {
  450. READ_SHORT(FLASH_BASE_ADDRESS +
  451.    sectorBaseAddress * FLASH_WIDTH,
  452.    &regValue);
  453. for (spin = 0; spin < 100; spin++) {
  454. } // skranz, added spin loop.
  455. } while ((regValue & dataPoll) != dataPoll);
  456. break;
  457. case 4:
  458. if (FLASH_MODE == X16) {
  459. FirstData = 0x00aa00aa; /* Data for the First  Cycle */
  460. SecondData = 0x00550055; /* Data for the Second Cycle */
  461. ThirdData = 0x00800080; /* Data for the Third  Cycle */
  462. FourthData = 0x00aa00aa; /* Data for the Fourth Cycle */
  463. FifthData = 0x00550055; /* Data for the Fifth  Cycle */
  464. SixthData = 0x00300030; /* Data for the Sixth  Cycle */
  465. FirstAddr = 0x5555; /* Address for the First  Cycle */
  466. SecondAddr = 0x2aaa; /* Address for the Second Cycle */
  467. ThirdAddr = 0x5555; /* Address for the Third  Cycle */
  468. FourthAddr = 0x5555; /* Address for the Fourth Cycle */
  469. FifthAddr = 0x2aaa; /* Address for the Fifth  Cycle */
  470. } else { /* if (FLASH_MODE == 8) */
  471. FirstData = 0xaaaaaaaa; /* Data for the First  Cycle */
  472. SecondData = 0x55555555; /* Data for the Second Cycle */
  473. ThirdData = 0x80808080; /* Data for the Third  Cycle */
  474. FourthData = 0xAAAAAAAA; /* Data for the Fourth Cycle */
  475. FifthData = 0x55555555; /* Data for the Fifth  Cycle */
  476. SixthData = 0x30303030; /* Data for the Sixth  Cycle */
  477. FirstAddr = 0xaaaa; /* Address for the First  Cycle */
  478. SecondAddr = 0x5555; /* Address for the Second Cycle */
  479. ThirdAddr = 0xaaaa; /* Address for the Third  Cycle */
  480. FourthAddr = 0xaaaa; /* Address for the Fourth Cycle */
  481. FifthAddr = 0x5555; /* Address for the Fifth  Cycle */
  482. }
  483. WRITE_WORD(FLASH_BASE_ADDRESS +
  484.    FirstAddr * FLASH_WIDTH, FirstData);
  485. WRITE_WORD(FLASH_BASE_ADDRESS +
  486.    SecondAddr * FLASH_WIDTH, SecondData);
  487. WRITE_WORD(FLASH_BASE_ADDRESS +
  488.    ThirdAddr * FLASH_WIDTH, ThirdData);
  489. WRITE_WORD(FLASH_BASE_ADDRESS +
  490.    FourthAddr * FLASH_WIDTH, FourthData);
  491. WRITE_WORD(FLASH_BASE_ADDRESS +
  492.    FifthAddr * FLASH_WIDTH, FifthData);
  493. WRITE_WORD(FLASH_BASE_ADDRESS +
  494.    (sectorBaseAddress & 0xffffff00) *
  495.    FLASH_WIDTH, SixthData);
  496. /* Poll on the flash */
  497. if (FLASH_MODE == X16) { /* 4 devices , 16 bit each => 64bit */
  498. dataPoll = 0x00800080;
  499. } else { /* (FLASH_MODE = 8) ==> 8 devices , 8 bit each => 64bit */
  500. dataPoll = 0x80808080;
  501. }
  502. do {
  503. READ_WORD(FLASH_BASE_ADDRESS +
  504.   sectorBaseAddress * FLASH_WIDTH,
  505.   &regValue);
  506. } while ((regValue & dataPoll) != dataPoll);
  507. break;
  508. case 8: /* In case of 64bit width the transformation is 1->8 */
  509. if (FLASH_MODE == X16) {
  510. FirstData = 0x00aa00aa; /* Data for the First  Cycle */
  511. SecondData = 0x00550055; /* Data for the Second Cycle */
  512. ThirdData = 0x00800080; /* Data for the Third  Cycle */
  513. FourthData = 0x00aa00aa; /* Data for the Fourth Cycle */
  514. FifthData = 0x00550055; /* Data for the Fifth  Cycle */
  515. SixthData = 0x00300030; /* Data for the Sixth  Cycle */
  516. FirstAddr = 0x5555; /* Address for the First  Cycle */
  517. SecondAddr = 0x2aaa; /* Address for the Second Cycle */
  518. ThirdAddr = 0x5555; /* Address for the Third  Cycle */
  519. FourthAddr = 0x5555; /* Address for the Fourth Cycle */
  520. FifthAddr = 0x2aaa; /* Address for the Fifth  Cycle */
  521. } else { /* (FLASH_MODE = 8 */
  522. FirstData = 0xaaaaaaaa; /* Data for the First  Cycle */
  523. SecondData = 0x55555555; /* Data for the Second Cycle */
  524. ThirdData = 0x80808080; /* Data for the Third  Cycle */
  525. FourthData = 0xAAAAAAAA; /* Data for the Fourth Cycle */
  526. FifthData = 0x55555555; /* Data for the Fifth  Cycle */
  527. SixthData = 0x30303030; /* Data for the Sixth  Cycle */
  528. FirstAddr = 0xaaaa; /* Address for the First  Cycle */
  529. SecondAddr = 0x5555; /* Address for the Second Cycle */
  530. ThirdAddr = 0xaaaa; /* Address for the Third  Cycle */
  531. FourthAddr = 0xaaaa; /* Address for the Fourth Cycle */
  532. FifthAddr = 0x5555; /* Address for the Fifth  Cycle */
  533. }
  534. WRITE_WORD(FLASH_BASE_ADDRESS +
  535.    FirstAddr * FLASH_WIDTH, FirstData);
  536. WRITE_WORD(FLASH_BASE_ADDRESS +
  537.    SecondAddr * FLASH_WIDTH, SecondData);
  538. WRITE_WORD(FLASH_BASE_ADDRESS +
  539.    ThirdAddr * FLASH_WIDTH, ThirdData);
  540. WRITE_WORD(FLASH_BASE_ADDRESS +
  541.    FourthAddr * FLASH_WIDTH, FourthData);
  542. WRITE_WORD(FLASH_BASE_ADDRESS +
  543.    FifthAddr * FLASH_WIDTH, FifthData);
  544. WRITE_WORD(FLASH_BASE_ADDRESS +
  545.    (sectorBaseAddress & 0xffffff00) *
  546.    FLASH_WIDTH, SixthData);
  547. WRITE_WORD(FLASH_BASE_ADDRESS +
  548.    FirstAddr * FLASH_WIDTH + 4, FirstData);
  549. WRITE_WORD(FLASH_BASE_ADDRESS +
  550.    SecondAddr * FLASH_WIDTH + 4,
  551.    SecondData);
  552. WRITE_WORD(FLASH_BASE_ADDRESS +
  553.    ThirdAddr * FLASH_WIDTH + 4, ThirdData);
  554. WRITE_WORD(FLASH_BASE_ADDRESS +
  555.    FourthAddr * FLASH_WIDTH + 4,
  556.    FourthData);
  557. WRITE_WORD(FLASH_BASE_ADDRESS +
  558.    FifthAddr * FLASH_WIDTH + 4, FifthData);
  559. WRITE_WORD(FLASH_BASE_ADDRESS +
  560.    (sectorBaseAddress & 0xffffff00)
  561.    * FLASH_WIDTH + 4, SixthData);
  562. /* Poll on the flash */
  563. if (FLASH_MODE == X16) { /* 4 devices , 16 bit each => 64bit */
  564. dataPoll = 0x00800080;
  565. } else { /* (FLASH_MODE = 8) ==> 8 devices , 8 bit each => 64bit */
  566. dataPoll = 0x80808080;
  567. }
  568. do {
  569. READ_WORD(FLASH_BASE_ADDRESS +
  570.   sectorBaseAddress * FLASH_WIDTH,
  571.   &regValue);
  572. } while ((regValue & dataPoll) != dataPoll);
  573. do {
  574. READ_WORD(FLASH_BASE_ADDRESS +
  575.   sectorBaseAddress * FLASH_WIDTH +
  576.   4, &regValue);
  577. } while ((regValue & dataPoll) != dataPoll);
  578. break;
  579. default:
  580. return false;
  581. }
  582. } /* End of 'flash erase sector' for AMD/ST */
  583. else { /* Intel/Micron */
  584. switch (FLASH_WIDTH) {
  585. case 1:
  586. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x20);
  587. WRITE_CHAR(
  588.    (FLASH_BASE_ADDRESS +
  589.     (sectorBaseAddress & 0xffffff00)),
  590.    0xd0);
  591. /* Poll on the flash */
  592. while (true) {
  593. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x70);
  594. READ_CHAR(FLASH_BASE_ADDRESS, &regValue);
  595. if ((regValue & 0x80) == 0x80)
  596. break;
  597. }
  598. break;
  599. case 2:
  600. if (FLASH_MODE == X16) { /* 1 device 16 bit.  */
  601. data20 = 0x0020;;
  602. dataD0 = 0x00d0;;
  603. } else { /* (FLASH_MODE = 8) ==> 2 devices , 8 bit each => 16bit */
  604. data20 = 0x2020;
  605. dataD0 = 0xd0d0;
  606. }
  607. WRITE_SHORT(FLASH_BASE_ADDRESS, data20);
  608. WRITE_SHORT(
  609.     (FLASH_BASE_ADDRESS +
  610.      ((sectorBaseAddress * 2) &
  611.       0xffffff00)), dataD0);
  612. /* Poll on the flash */
  613. if (FLASH_MODE == X16) {
  614. dataPoll = 0x0080;
  615. data70 = 0x0070;
  616. } else { /* (FLASH_MODE = 8) */
  617. dataPoll = 0x8080;
  618. data70 = 0x7070;
  619. }
  620. while (true) {
  621. WRITE_SHORT(FLASH_BASE_ADDRESS +
  622.     sectorBaseAddress * 2, data70);
  623. READ_SHORT(FLASH_BASE_ADDRESS +
  624.    sectorBaseAddress * 2,
  625.    &regValue);
  626. if ((regValue & 0x0080) == 0x0080)
  627. break;
  628. }
  629. break;
  630. case 4:
  631. if (FLASH_MODE == X16) { /* 2 devices , 16 bit each => 32bit */
  632. data20 = 0x00200020;
  633. dataD0 = 0x00d000d0;
  634. } else { /* (FLASH_MODE = 8) ==> 4 devices , 8 bit each => 32bit */
  635. data20 = 0x20202020;
  636. dataD0 = 0xd0d0d0d0;
  637. }
  638. WRITE_WORD(FLASH_BASE_ADDRESS, data20);
  639. WRITE_WORD(
  640.    (FLASH_BASE_ADDRESS +
  641.     ((sectorBaseAddress * 4) &
  642.      0xffffff00)), dataD0);
  643. /* Poll on the flash */
  644. if (FLASH_MODE == X16) {
  645. dataPoll = 0x0080;
  646. data70 = 0x0070;
  647. } else { /* (FLASH_MODE = 8) */
  648. dataPoll = 0x8080;
  649. data70 = 0x7070;
  650. }
  651. while (true) {
  652. WRITE_SHORT(FLASH_BASE_ADDRESS, data70);
  653. READ_SHORT(FLASH_BASE_ADDRESS, &regValue);
  654. if ((regValue & dataPoll) == dataPoll)
  655. break;
  656. }
  657. while (true) {
  658. WRITE_SHORT(FLASH_BASE_ADDRESS + 2,
  659.     data70);
  660. READ_SHORT(FLASH_BASE_ADDRESS + 2,
  661.    &regValue);
  662. if ((regValue & dataPoll) == dataPoll)
  663. break;
  664. }
  665. break;
  666. case 8:
  667. if (FLASH_MODE == X16) { /* 4 devices , 16 bit each => 64bit */
  668. data20 = 0x00200020;
  669. dataD0 = 0x00d000d0;
  670. } else { /* (FLASH_MODE = 8) ==> 8 devices , 8 bit each => 64bit */
  671. data20 = 0x20202020;
  672. dataD0 = 0xd0d0d0d0;
  673. }
  674. WRITE_WORD(FLASH_BASE_ADDRESS, data20);
  675. WRITE_WORD(
  676.    (FLASH_BASE_ADDRESS +
  677.     ((sectorBaseAddress * 8) &
  678.      0xffffff00)), dataD0);
  679. WRITE_WORD(FLASH_BASE_ADDRESS + 4, data20);
  680. WRITE_WORD(
  681.    (FLASH_BASE_ADDRESS +
  682.     ((sectorBaseAddress * 8) & 0xffffff00 +
  683.      4)), dataD0);
  684. /* Poll on the flash */
  685. if (FLASH_MODE == X16) {
  686. dataPoll = 0x0080;
  687. data70 = 0x0070;
  688. } else { /* (FLASH_MODE = 8) */
  689. dataPoll = 0x8080;
  690. data70 = 0x7070;
  691. }
  692. while (true) {
  693. WRITE_SHORT(FLASH_BASE_ADDRESS +
  694.     sectorBaseAddress * 8, data70);
  695. READ_SHORT(FLASH_BASE_ADDRESS +
  696.    sectorBaseAddress * 8,
  697.    &regValue);
  698. if ((regValue & dataPoll) == dataPoll)
  699. break;
  700. }
  701. while (true) {
  702. WRITE_SHORT(FLASH_BASE_ADDRESS + 2,
  703.     data70);
  704. READ_SHORT(FLASH_BASE_ADDRESS + 2,
  705.    &regValue);
  706. if ((regValue & dataPoll) == dataPoll)
  707. break;
  708. }
  709. while (true) {
  710. WRITE_SHORT(FLASH_BASE_ADDRESS + 4,
  711.     data70);
  712. READ_SHORT(FLASH_BASE_ADDRESS + 4,
  713.    &regValue);
  714. if ((regValue & dataPoll) == dataPoll)
  715. break;
  716. }
  717. while (true) {
  718. WRITE_SHORT(FLASH_BASE_ADDRESS + 6,
  719.     data70);
  720. READ_SHORT(FLASH_BASE_ADDRESS + 6,
  721.    &regValue);
  722. if ((regValue & dataPoll) == dataPoll)
  723. break;
  724. }
  725. break;
  726. default:
  727. return false;
  728. }
  729. }
  730. flashReset();
  731. return true;
  732. }
  733. /********************************************************************
  734. * flashWriteWord - Write 32Bit to the FLASH memory at the given offset from the
  735. *                  FLASH base address.
  736. *       address 0 = 0x00000000 !!
  737. *    Attention!!! data "0" cannot be programed back to
  738. *                  "1" (only by first performing an earase operation).
  739. *                  The function takes care of Big/Little endian conversion
  740. *
  741. * INPUTS:  offset - The offset from the flash`s base address.
  742. *          data   - The data that should be written.
  743. * RETURNS: true on success,false on failure
  744. *********************************************************************/
  745. bool flashWriteWord(unsigned int offset, unsigned int data)
  746. {
  747. unsigned char c, rc;
  748. unsigned short s, rs;
  749. register unsigned int rw;
  750. register unsigned int regValue;
  751. register unsigned int FirstAddr, SecondAddr, ThirdAddr;
  752. register unsigned int FirstData, SecondData, ThirdData;
  753. register unsigned int data10, data20, data70, data80;
  754. if ((flashTypes[POINTER_TO_FLASH] == AMD_FLASH) || 
  755.     (flashTypes[POINTER_TO_FLASH] == ST_FLASH)) {
  756. switch (FLASH_WIDTH) {
  757. case 1: /* Split the 32 bit write into four 8bit Writings */
  758. if (FLASH_MODE == PURE8) { /* Boot Flash */
  759. FirstAddr = 0x5555;
  760. SecondAddr = 0x2aaa;
  761. ThirdAddr = 0x5555;
  762. } else {
  763. FirstAddr = 0xaaaa;
  764. SecondAddr = 0x5555;
  765. ThirdAddr = 0xaaaa;
  766. }
  767. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xaa);
  768. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  769. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0xa0);
  770. #ifdef BE
  771. c = (data >> 24);
  772. #else
  773. c = data;
  774. #endif
  775. WRITE_CHAR(FLASH_BASE_ADDRESS + offset, c);
  776. /* Writing first Byte */
  777. while (true) {
  778. READ_CHAR(FLASH_BASE_ADDRESS + offset,
  779.   &rc);
  780. if ((rc & 0x80) == (c & 0x80)) /* DQ7 =? DATA */
  781. break; /* DQ7 =  DATA */
  782. if ((rc & 0x20) == 0x20) { /* DQ5 =? '1'  */
  783. READ_CHAR(FLASH_BASE_ADDRESS +
  784.   offset, &rc);
  785. if ((rc & 0x80) == (c & 0x80))
  786. break; /* DQ7 = DATA  */
  787. else
  788. return false; /* DQ7 != DATA */
  789. }
  790. }
  791. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xaa);
  792. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  793. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0xa0);
  794. #ifdef BE
  795. c = (data >> 16);
  796. #else
  797. c = (data >> 8);
  798. #endif
  799. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 1, c);
  800. /* Writing second Byte */
  801. while (true) {
  802. READ_CHAR(FLASH_BASE_ADDRESS + offset + 1,
  803.   &rc);
  804. if ((rc & 0x80) == (c & 0x80)) /* DQ7 =? DATA */
  805. break; /* DQ7 = DATA  */
  806. if ((rc & 0x20) == 0x20) { /* DQ5 =? '1'  */
  807. READ_CHAR(FLASH_BASE_ADDRESS +
  808.   offset + 1, &rc);
  809. if ((rc & 0x80) == (c & 0x80))
  810. break; /* DQ7 = DATA  */
  811. else
  812. return false; /* DQ7 != DATA */
  813. }
  814. }
  815. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xaa);
  816. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  817. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0xa0);
  818. #ifdef BE
  819. c = (data >> 8);
  820. #else
  821. c = (data >> 16);
  822. #endif
  823. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 2, c);
  824. /* Writing third Byte */
  825. while (true) {
  826. READ_CHAR(FLASH_BASE_ADDRESS + offset + 2,
  827.   &rc);
  828. if ((rc & 0x80) == (c & 0x80)) /* DQ7 =? DATA */
  829. break; /* DQ7 = DATA  */
  830. if ((rc & 0x20) == 0x20) { /* DQ5 =? '1'  */
  831. READ_CHAR(FLASH_BASE_ADDRESS +
  832.   offset + 2, &rc);
  833. if ((rc & 0x80) == (c & 0x80))
  834. break; /* DQ7 = DATA  */
  835. else
  836. return false; /* DQ7 != DATA */
  837. }
  838. }
  839. WRITE_CHAR(FLASH_BASE_ADDRESS + FirstAddr, 0xaa);
  840. WRITE_CHAR(FLASH_BASE_ADDRESS + SecondAddr, 0x55);
  841. WRITE_CHAR(FLASH_BASE_ADDRESS + ThirdAddr, 0xa0);
  842. #ifdef BE
  843. c = data;
  844. #else
  845. c = (data >> 24);
  846. #endif
  847. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 3, c);
  848. /* Writing fourth Byte */
  849. while (true) {
  850. READ_CHAR(FLASH_BASE_ADDRESS + offset + 3,
  851.   &rc);
  852. if ((rc & 0x80) == (c & 0x80)) /* DQ7 =? DATA */
  853. break; /* DQ7 = DATA  */
  854. if ((rc & 0x20) == 0x20) { /* DQ5 =? '1'  */
  855. READ_CHAR(FLASH_BASE_ADDRESS +
  856.   offset + 3, &rc);
  857. if ((rc & 0x80) == (c & 0x80))
  858. break; /* DQ7 = DATA  */
  859. else
  860. return false; /* DQ7 != DATA */
  861. }
  862. }
  863. break;
  864. case 2: /* Split the 32 bit write into two 8/16 bit Writings
  865.    (16bit width). */
  866. if (FLASH_MODE == X16) {
  867. FirstData = 0xaa; /* Data for the First  Cycle    */
  868. SecondData = 0x55; /* Data for the Second Cycle    */
  869. ThirdData = 0xa0; /* Data for the Third  Cycle    */
  870. FirstAddr = 0x5555; /* Address for the First  Cycle */
  871. SecondAddr = 0x2aaa; /* Address for the Second Cycle */
  872. ThirdAddr = 0x5555; /* Address for the Third  Cycle */
  873. } else { /* if (FLASH_MODE == 8) */
  874. FirstData = 0xaaaa; /* Data for the First  Cycle    */
  875. SecondData = 0x5555; /* Data for the Second Cycle    */
  876. ThirdData = 0xa0a0; /* Data for the Third  Cycle    */
  877. FirstAddr = 0xaaaa; /* Address for the First  Cycle */
  878. SecondAddr = 0x5555; /* Address for the Second Cycle */
  879. ThirdAddr = 0xaaaa; /* Address for the Third  Cycle */
  880. }
  881. WRITE_SHORT(FLASH_BASE_ADDRESS +
  882.     FirstAddr * FLASH_WIDTH, FirstData);
  883. WRITE_SHORT(FLASH_BASE_ADDRESS +
  884.     SecondAddr * FLASH_WIDTH, SecondData);
  885. WRITE_SHORT(FLASH_BASE_ADDRESS +
  886.     ThirdAddr * FLASH_WIDTH, ThirdData);
  887. #ifdef BE
  888. s = (data >> 16);
  889. #else
  890. s = data;
  891. #endif
  892. WRITE_SHORT(FLASH_BASE_ADDRESS + offset, s);
  893. /* Writing Two Bytes */
  894. if (FLASH_MODE == X16) {
  895. data80 = 0x80;;
  896. data20 = 0x20;;
  897. } else { /* if (FLASH_MODE == 8) */
  898. data80 = 0x8080;
  899. data20 = 0x2020;
  900. }
  901. while (true) {
  902. READ_SHORT(FLASH_BASE_ADDRESS + offset,
  903.    &rs);
  904. if ((rs & data80) == (s & data80)) /* DQ7 =? DATA */
  905. break; /* DQ7 =  DATA */
  906. if ((rs & data20) == data20) { /* DQ5 =? DATA */
  907. READ_SHORT(FLASH_BASE_ADDRESS +
  908.    offset, &rs);
  909. if ((rs & data80) == (s & data80))
  910. break; /* DQ7 = DATA  */
  911. else {
  912. flashReset();
  913. return false; /* DQ7 != DATA */
  914. }
  915. }
  916. }
  917. WRITE_SHORT(FLASH_BASE_ADDRESS +
  918.     FirstAddr * FLASH_WIDTH, FirstData);
  919. WRITE_SHORT(FLASH_BASE_ADDRESS +
  920.     SecondAddr * FLASH_WIDTH, SecondData);
  921. WRITE_SHORT(FLASH_BASE_ADDRESS +
  922.     ThirdAddr * FLASH_WIDTH, ThirdData);
  923. #ifdef BE
  924. s = data;
  925. #else
  926. s = (data >> 16);
  927. #endif
  928. WRITE_SHORT(FLASH_BASE_ADDRESS + offset + 2, s);
  929. /* Writing Two Bytes */
  930. while (true) {
  931. READ_SHORT(FLASH_BASE_ADDRESS + offset + 2,
  932.    &rs);
  933. if ((rs & data80) == (s & data80)) /* DQ7 =? DATA */
  934. break; /* DQ7 =  DATA */
  935. if ((rs & data20) == data20) { /* DQ5 =? '1'  */
  936. READ_SHORT(FLASH_BASE_ADDRESS +
  937.    offset + 2, &rs);
  938. if ((rs & data80) == (s & data80))
  939. break; /* DQ7 = DATA  */
  940. else {
  941. flashReset();
  942. return false; /* DQ7 != DATA */
  943. }
  944. }
  945. }
  946. return true;
  947. case 4:
  948. case 8:
  949. if (FLASH_MODE == X16) {
  950. FirstData = 0x00aa00aa;
  951. SecondData = 0x00550055;
  952. ThirdData = 0x00a000a0;
  953. FirstAddr = 0x5555;
  954. SecondAddr = 0x2aaa;
  955. ThirdAddr = 0x5555;
  956. } else { /* (FLASH_MODE == 8) */
  957. FirstData = 0xaaaaaaaa; /* Data for the First  Cycle    */
  958. SecondData = 0x55555555; /* Data for the Second Cycle    */
  959. ThirdData = 0xa0a0a0a0; /* Data for the Third  Cycle    */
  960. FirstAddr = 0xaaaaaaaa; /* Address for the First  Cycle */
  961. SecondAddr = 0x55555555; /* Address for the Second Cycle */
  962. ThirdAddr = 0xaaaaaaaa; /* Address for the Third  Cycle */
  963. }
  964. WRITE_WORD(FLASH_BASE_ADDRESS + FirstAddr *
  965.    FLASH_WIDTH + offset % FLASH_WIDTH,
  966.    FirstData);
  967. WRITE_WORD(FLASH_BASE_ADDRESS +
  968.    SecondAddr * FLASH_WIDTH +
  969.    offset % FLASH_WIDTH, SecondData);
  970. WRITE_WORD(FLASH_BASE_ADDRESS +
  971.    ThirdAddr * FLASH_WIDTH +
  972.    offset % FLASH_WIDTH, ThirdData);
  973. /* writting the word. */
  974. WRITE_WORD(FLASH_BASE_ADDRESS + offset, data);
  975. /* preparing the polling patterns. */
  976. if (FLASH_MODE == X16) {
  977. data80 = 0x00800080;
  978. data20 = 0x00200020;
  979. } else { /* (FLASH_MODE == 8) */
  980. data80 = 0x80808080;
  981. data20 = 0x20202020;
  982. }
  983. while (true) { /* polling loop. */
  984. rw = READWORD(FLASH_BASE_ADDRESS + offset);
  985. /* DQ7 =? DATA */
  986. if ((rw & data80) == (data & data80))
  987. break; /* DQ7 =  DATA */
  988. if ((rw & data20) == data20) { /* DQ5 =? '1'  */
  989. rw =
  990.     READWORD(FLASH_BASE_ADDRESS +
  991.      offset);
  992. if ((rw & data80) ==
  993.     (data & data80)) break; /* DQ7 = DATA  */
  994. else
  995. return false; /* DQ7 != DATA */
  996. }
  997. }
  998. return true;
  999. default:
  1000. return false; /* case of invalid flash Width. */
  1001. }
  1002. } else { /* Intel/Micron */
  1003. switch (FLASH_WIDTH) {
  1004. case 1:
  1005. /* Writing First Byte */
  1006. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x10);
  1007. #ifdef BE
  1008. c = (data >> 24);
  1009. #else
  1010. c = data;
  1011. #endif
  1012. WRITE_CHAR(FLASH_BASE_ADDRESS + offset, c);
  1013. while (true) {
  1014. /* Reading STATUS Register */
  1015. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x70);
  1016. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1017. if ((regValue & 0x80) == 0x80)
  1018. break; /* Case of Write-Operation had Ended */
  1019. }
  1020. /* Reading STATUS Register for Writing Verification */
  1021. WRITE_CHAR(FLASH_BASE_ADDRESS, 0x70);
  1022. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1023. if ((regValue & 0x10) == 0x10)
  1024. return false; /* Write failure */
  1025. /* Writing Second Byte */
  1026. WRITE_CHAR(FLASH_BASE_ADDRESS + 1, 0x10);
  1027. #ifdef BE
  1028. c = (data >> 16);
  1029. #else
  1030. c = (data >> 8);
  1031. #endif
  1032. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 1, c);
  1033. while (true) {
  1034. /* Reading STATUS Register */
  1035. WRITE_CHAR(FLASH_BASE_ADDRESS + 1, 0x70);
  1036. regValue =
  1037.     READCHAR(FLASH_BASE_ADDRESS + 1);
  1038. if ((regValue & 0x80) == 0x80)
  1039. break; /* Write operation ended */
  1040. }
  1041. /* Reading STATUS Register for Writing verification */
  1042. WRITE_CHAR(FLASH_BASE_ADDRESS + 1, 0x70);
  1043. regValue = READCHAR(FLASH_BASE_ADDRESS + 1);
  1044. if ((regValue & 0x10) == 0x10)
  1045. return false; /* Write failure */
  1046. /* Writing Third Byte */
  1047. WRITE_CHAR(FLASH_BASE_ADDRESS + 2, 0x10);
  1048. #ifdef BE
  1049. c = (data >> 8);
  1050. #else
  1051. c = (data >> 16);
  1052. #endif
  1053. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 2, c);
  1054. while (true) {
  1055. /* Reading STATUS Register */
  1056. WRITE_CHAR(FLASH_BASE_ADDRESS + 2, 0x70);
  1057. regValue =
  1058.     READCHAR(FLASH_BASE_ADDRESS + 2);
  1059. if ((regValue & 0x80) == 0x80)
  1060. break; /* Write operation ended */
  1061. }
  1062. /* Reading STATUS Register for Writing Verification */
  1063. WRITE_CHAR(FLASH_BASE_ADDRESS + 2, 0x70);
  1064. regValue = READCHAR(FLASH_BASE_ADDRESS + 2);
  1065. if ((regValue & 0x10) == 0x10)
  1066. return false; /* Write failure */
  1067. /* Writing Fourth Byte */
  1068. WRITE_CHAR(FLASH_BASE_ADDRESS + 3, 0x10);
  1069. #ifdef BE
  1070. c = data;
  1071. #else
  1072. c = (data >> 24);
  1073. #endif
  1074. WRITE_CHAR(FLASH_BASE_ADDRESS + offset + 3, c);
  1075. while (true) {
  1076. /* Reading STATUS Register */
  1077. WRITE_CHAR(FLASH_BASE_ADDRESS + 3, 0x70);
  1078. regValue =
  1079.     READCHAR(FLASH_BASE_ADDRESS + 3);
  1080. if ((regValue & 0x80) == 0x80)
  1081. break; /* Write operation ended */
  1082. }
  1083. /* Reading STATUS Register for Writing Verification */
  1084. WRITE_CHAR(FLASH_BASE_ADDRESS + 3, 0x70);
  1085. regValue = READCHAR(FLASH_BASE_ADDRESS + 3);
  1086. if ((regValue & 0x10) == 0x10)
  1087. return false; /* Write failure */
  1088. flashReset();
  1089. return true;
  1090. case 2:
  1091. if (FLASH_MODE == X16) { /* Case of one X16 bit device */
  1092. FirstData = 0x0010; /* Data for the First  Cycle  */
  1093. } else { /* if (FLASH_MODE == 8) ==> Case of two X8 bit devices */
  1094. FirstData = 0x1010; /* Data for the First  Cycle  */
  1095. }
  1096. /* Writing First two Bytes */
  1097. WRITE_SHORT(FLASH_BASE_ADDRESS, FirstData);
  1098. #ifdef BE
  1099. s = (data >> 16);
  1100. #else
  1101. s = data;
  1102. #endif
  1103. WRITE_SHORT(FLASH_BASE_ADDRESS + offset, s);
  1104. if (FLASH_MODE == X16) {
  1105. data70 = 0x0070;
  1106. data80 = 0x0080;
  1107. data10 = 0x0010;
  1108. } else { /* case of (FLASH_MODE == X8) */
  1109. data70 = 0x7070;
  1110. data80 = 0x8080;
  1111. data10 = 0x1010;
  1112. }
  1113. /* polling on writing action => when done break. */
  1114. while (true) {
  1115. WRITE_SHORT(FLASH_BASE_ADDRESS, data70);
  1116. regValue = READSHORT(FLASH_BASE_ADDRESS);
  1117. if ((regValue & data80) == data80)
  1118. break;
  1119. }
  1120. /* Reading STATUS Register for Writing Verification */
  1121. WRITE_CHAR(FLASH_BASE_ADDRESS, data70);
  1122. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1123. if ((regValue & data10) == data10)
  1124. return false; /* Write failure */
  1125. /* Writing Last two Bytes */
  1126. WRITE_SHORT(FLASH_BASE_ADDRESS + 2, FirstData);
  1127. #ifdef BE
  1128. s = data;
  1129. #else
  1130. s = (data >> 16);
  1131. #endif
  1132. WRITE_SHORT(FLASH_BASE_ADDRESS + offset + 2, s);
  1133. /* polling on writing action => when done break. */
  1134. while (true) {
  1135. WRITE_SHORT(FLASH_BASE_ADDRESS + 2,
  1136.     data70);
  1137. regValue =
  1138.     READSHORT(FLASH_BASE_ADDRESS + 2);
  1139. if ((regValue & data80) == data80)
  1140. break;
  1141. }
  1142. /* Reading STATUS Register for Writing Verification */
  1143. WRITE_CHAR(FLASH_BASE_ADDRESS, data70);
  1144. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1145. if ((regValue & data10) == data10)
  1146. return false; /* Write failure */
  1147. flashReset();
  1148. return true;
  1149. case 4:
  1150. case 8:
  1151. if (FLASH_MODE == X16) { /* Case of one X16 bit device */
  1152. FirstData = 0x00100010; /* Data for the First  Cycle  */
  1153. } else { /* (FLASH_MODE == 8) ==> Case of two X8 bit devices */
  1154. FirstData = 0x10101010; /* Data for the First  Cycle  */
  1155. }
  1156. /* Writing First two Bytes */
  1157. WRITE_WORD(FLASH_BASE_ADDRESS +
  1158.    offset % FLASH_WIDTH, FirstData);
  1159. #ifdef BE
  1160. s = (data >> 16);
  1161. #else
  1162. s = data;
  1163. #endif
  1164. /* writing the 32-bit data to flash. */
  1165. WRITE_WORD(FLASH_BASE_ADDRESS + offset, data);
  1166. if (FLASH_MODE == X16) {
  1167. data70 = 0x0070;
  1168. data80 = 0x0080;
  1169. data10 = 0x0010;
  1170. } else { /* (FLASH_MODE == 8) */
  1171. data70 = 0x7070;
  1172. data80 = 0x8080;
  1173. data10 = 0x1010;
  1174. }
  1175. while (true) {
  1176. WRITE_SHORT(FLASH_BASE_ADDRESS +
  1177.     offset % FLASH_WIDTH, data70);
  1178. regValue = READSHORT(FLASH_BASE_ADDRESS);
  1179. if ((regValue & data80) == data80)
  1180. break;
  1181. }
  1182. /* Reading STATUS Register for Writing Verification */
  1183. WRITE_CHAR(FLASH_BASE_ADDRESS, data70);
  1184. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1185. if ((regValue & data10) == data10)
  1186. return false; /* Write failure */
  1187. /* Writing Last two Bytes */
  1188. #ifdef BE
  1189. s = data;
  1190. #else
  1191. s = (data >> 16);
  1192. #endif
  1193. while (true) {
  1194. WRITE_SHORT(FLASH_BASE_ADDRESS +
  1195.     offset % FLASH_WIDTH + 2,
  1196.     data70);
  1197. regValue =
  1198.     READSHORT(FLASH_BASE_ADDRESS +
  1199.       offset % FLASH_WIDTH + 2);
  1200. if ((regValue & data80) == data80)
  1201. break;
  1202. }
  1203. /* Reading STATUS Register for Writing Verification */
  1204. WRITE_CHAR(FLASH_BASE_ADDRESS, data70);
  1205. regValue = READCHAR(FLASH_BASE_ADDRESS);
  1206. if ((regValue & data10) == data10)
  1207. return false; /* Write failure */
  1208. flashReset();
  1209. return true;
  1210. default:
  1211. flashReset();
  1212. return false;
  1213. }
  1214. }
  1215. flashReset();
  1216. return true;
  1217. }
  1218. /********************************************************************
  1219. * flashReadWord - Read 32Bit from the FLASH memory at a given offset
  1220. *                 from the FLASH base address.
  1221. *    address 0 = 0x00000000 !!
  1222. *                 The function takes care of Big/Little endian conversion
  1223. * INPUTS:  offset,the offset from the flash`s base address
  1224. * RETURNS: data
  1225. *********************************************************************/
  1226. unsigned int flashReadWord(unsigned int offset)
  1227. {
  1228. unsigned int regValue;
  1229. flashReset();
  1230. READ_WORD(FLASH_BASE_ADDRESS + offset, &regValue);
  1231. return regValue;
  1232. }
  1233. /********************************************************************
  1234. * flashInWhichSector - Returns the sector`s number at which offset is at.
  1235. *
  1236. * INPUTS:  Offset
  1237. * RETURNS: Sector number,or 0xffffffff in case the address is out of range or
  1238. *          flash wasn't initialize.
  1239. *********************************************************************/
  1240. unsigned int flashInWhichSector(unsigned int offset)
  1241. {
  1242. unsigned int sectorNumber, numberOfDevices;
  1243. unsigned int accMemory = 0;
  1244. if ((FLASH_MODE == PURE8) || (FLASH_MODE == X8)) {
  1245. numberOfDevices = FLASH_WIDTH;
  1246. } else { /* X16 mode */
  1247. numberOfDevices = FLASH_WIDTH / 2;
  1248. }
  1249. for (sectorNumber = 0;
  1250.      sectorNumber < flashTypes[NUMBER_OF_SECTORS]; sectorNumber++) {
  1251. accMemory =
  1252.     accMemory + flashTypes[FIRST_SECTOR_SIZE +
  1253.    sectorNumber];
  1254. if (offset < accMemory * numberOfDevices * 1024)
  1255. return sectorNumber;
  1256. }
  1257. return 0xffffffff;
  1258. }
  1259. /********************************************************************
  1260. * flashGetSectorSize - When given a Valid sector Number returns its Size.
  1261. *
  1262. * INPUTS:  unsigned int sectorNumber.
  1263. * RETURNS: Sector size. (if Sector number isn't valid or flash wasn't
  1264. *          initialize return 0.)
  1265. *********************************************************************/
  1266. unsigned int flashGetSectorSize(unsigned int sectorNumber)
  1267. {
  1268. if (sectorNumber >= flashTypes[NUMBER_OF_SECTORS])
  1269. return 0;
  1270. else {
  1271. if (FLASH_MODE != PURE8)
  1272. return (flashTypes
  1273. [FIRST_SECTOR_SIZE +
  1274.  sectorNumber] * _1K * (FLASH_WIDTH * 8 /
  1275. FLASH_MODE));
  1276. else /* in case of PUR8 */
  1277. return (flashTypes
  1278. [FIRST_SECTOR_SIZE +
  1279.  sectorNumber] * _1K * FLASH_WIDTH);
  1280. }
  1281. }
  1282. /********************************************************************
  1283. * getFlashSize - Return Total flash size.
  1284. *
  1285. * INPUTS:  N/A.
  1286. * RETURNS: Flash size. (If flash wasn't initialize return 0)
  1287. *********************************************************************/
  1288. unsigned int flashGetSize()
  1289. {
  1290. unsigned int sectorNum;
  1291. unsigned int totalSize = 0;
  1292. if (POINTER_TO_FLASH == 0)
  1293. return 0; /* case of flash not initialize */
  1294. for (sectorNum = 0; sectorNum < flashTypes[NUMBER_OF_SECTORS];
  1295.      sectorNum++) {
  1296. totalSize += flashGetSectorSize(sectorNum);
  1297. }
  1298. return (totalSize);
  1299. }
  1300. /********************************************************************
  1301. * flashGetSectorOffset - Returns sector base address.
  1302. *
  1303. * INPUTS:  unsigned int sectorNum.
  1304. * RETURNS: Sector Base Address.
  1305. *********************************************************************/
  1306. unsigned int flashGetSectorOffset(unsigned int sectorNum)
  1307. {
  1308. unsigned int i;
  1309. unsigned int sectorBaseAddress = 0;
  1310. unsigned int numOfDevices;
  1311. if (sectorNum > (flashParametrs[NUMBER_OF_SECTORS] - 1))
  1312. return 0xffffffff;
  1313. for (i = 0; i < sectorNum; i++) {
  1314. sectorBaseAddress =
  1315.     sectorBaseAddress + flashTypes[FIRST_SECTOR_SIZE + i];
  1316. }
  1317. if (FLASH_MODE == X16)
  1318. numOfDevices = FLASH_WIDTH * 8 / FLASH_MODE;
  1319. else
  1320. numOfDevices = FLASH_WIDTH;
  1321. return (_1K * sectorBaseAddress * numOfDevices);
  1322. }
  1323. /********************************************************************
  1324. * flashWriteBlock - Write block of chars to flash.
  1325. *
  1326. * INPUTS:  unsigned int offset - flash destination address.
  1327. *          unsigned int numOfByte - block size.
  1328. *          unsigned char * blockAddress - block source address.
  1329. * RETURNS: Number of Bytes written.
  1330. *********************************************************************/
  1331. unsigned int flashWriteBlock(unsigned int offset, unsigned int numOfByte,
  1332.      unsigned char *blockAddress)
  1333. {
  1334. register unsigned int flashWrite;
  1335. register unsigned int align;
  1336. register unsigned int num;
  1337. register unsigned int i;
  1338. if ((offset + numOfByte) > flashGetSize())
  1339. numOfByte = flashGetSize() - offset; /* getting to flash boundary. */
  1340. num = numOfByte;
  1341. align = offset % 4; /* alignment toward flash.    */
  1342. /* writes chars until the offset toward flash will be align. */
  1343. for (i = align; (i < 4) && (numOfByte > 0) && (align != 0); i++) {
  1344. flashWriteChar(offset, blockAddress[0]);
  1345. numOfByte--;
  1346. offset++;
  1347. blockAddress++;
  1348. }
  1349. while (numOfByte > 3) {
  1350. #ifdef LE
  1351. flashWrite = blockAddress[0] | (blockAddress[1] << 8) |
  1352.     (blockAddress[2] << 16) | (blockAddress[3] << 24);
  1353. #else
  1354. flashWrite = blockAddress[3] | (blockAddress[2] << 8) |
  1355.     (blockAddress[1] << 16) | (blockAddress[0] << 24);
  1356. #endif
  1357. if (flashWrite != 0xffffffff) /* for optimization. */
  1358. flashWriteWord(offset, flashWrite);
  1359. numOfByte -= 4;
  1360. blockAddress += 4;
  1361. offset += 4;
  1362. }
  1363. while (numOfByte > 0) {
  1364. flashWriteChar(offset, blockAddress[0]);
  1365. numOfByte--;
  1366. blockAddress++;
  1367. offset++;
  1368. }
  1369. return num;
  1370. }
  1371. /********************************************************************
  1372. * flashReadBlock - Read block of chars from flash.
  1373. *
  1374. * INPUTS:  unsigned int offset - flash source address.
  1375. *          unsigned int numOfByte - block size.
  1376. *          unsigned char * blockAddress - block destination address.
  1377. * RETURNS: Number of Bytes written.
  1378. *********************************************************************/
  1379. unsigned int flashReadBlock(unsigned int offset, unsigned int numOfByte,
  1380.     unsigned char *blockAddress)
  1381. {
  1382. unsigned int i;
  1383. for (i = 0; i < numOfByte; i++) {
  1384. blockAddress[i] = flashReadChar(offset + i);
  1385. }
  1386. return numOfByte;
  1387. }
  1388. /********************************************************************
  1389. * flashReadChar - read one charecter form given flash offset.
  1390. *
  1391. * INPUTS:  unsigned int offset - required offset to be read from.
  1392. * RETURNS: read charecter.
  1393. *********************************************************************/
  1394. unsigned char flashReadChar(unsigned int offset)
  1395. {
  1396. unsigned char regValue;
  1397. flashReset();
  1398. READ_CHAR(FLASH_BASE_ADDRESS + offset, &regValue);
  1399. return regValue;
  1400. }
  1401. /********************************************************************
  1402. * flashReadShort - read 16bit form given flash offset.
  1403. *
  1404. * INPUTS:  unsigned int offset - required offset to be read from.
  1405. * RETURNS: 16bit data.
  1406. *********************************************************************/
  1407. unsigned short flashReadShort(unsigned int offset)
  1408. {
  1409. unsigned short regValue;
  1410. flashReset();
  1411. READ_SHORT(FLASH_BASE_ADDRESS + offset, &regValue);
  1412. return regValue;
  1413. }
  1414. /********************************************************************
  1415. * flashWriteShort - write 16bit data to a given flash offset.
  1416. *                  It reads the whole word 32bit wide, modify the  short
  1417. *                  and write back the word.
  1418. *
  1419. * INPUTS:  unsigned int offset - required offset to be write to.
  1420. *          unsigned short sdata - data to be written.
  1421. * RETURNS: true if writting successesed false otherwise.
  1422. *********************************************************************/
  1423. bool flashWriteShort(unsigned int offset, unsigned short sdata)
  1424. {
  1425. unsigned int align;
  1426. unsigned int flashWrite;
  1427. unsigned int flashRead;
  1428. align = offset % 4;
  1429. if ((align == 1) || (align == 3))
  1430. return false; /* offset misaligned. */
  1431. flashRead = flashReadWord(offset - align);
  1432. if (align == 0)
  1433. #ifdef BE
  1434. flashWrite = (flashRead & 0x0000ffff) | (sdata << 16);
  1435. #else
  1436. flashWrite = (flashRead & 0xffff0000) | sdata;
  1437. #endif
  1438. else /* (align == 2) */
  1439. #ifdef BE
  1440. flashWrite = (flashRead & 0xffff0000) | sdata;
  1441. #else
  1442. flashWrite = (flashRead & 0x0000ffff) | (sdata << 16);
  1443. #endif
  1444. flashWriteWord(offset - align, flashWrite);
  1445. return true;
  1446. }
  1447. /********************************************************************
  1448. * flashWriteChar - write one charecter (8 bit) to a given flash offset.
  1449. *                  It reads the whole word 32bit wide, modify the charecter
  1450. *                  and write back the word.
  1451. *
  1452. * INPUTS:  unsigned int offset - required offset to be write to.
  1453. *          unsigned short sdata - data to be written.
  1454. * RETURNS: true if writting successed.
  1455. *********************************************************************/
  1456. bool flashWriteChar(unsigned int offset, unsigned char cdata)
  1457. {
  1458. unsigned int align;
  1459. unsigned int flashWrite;
  1460. unsigned int flashRead;
  1461. align = offset % 4;
  1462. flashRead = flashReadWord(offset - align);
  1463. #ifdef BE
  1464. flashWrite = (flashRead & ~(0xff000000 >> (8 * align))) |
  1465.     (cdata << (8 * (3 - align)));
  1466. #else
  1467. flashWrite = (flashRead & ~(0xff000000 << (8 * align))) |
  1468.     (cdata << (8 * align));
  1469. #endif
  1470. flashWriteWord(offset - align, flashWrite);
  1471. return true;
  1472. }
  1473. /********************************************************************
  1474. * flashGetNumOfSectors - write one charecter (8 bit) to a given flash offset.
  1475. *                        It reads the whole word 32bit wide, modify the
  1476. *                        charecter and write back the word.
  1477. *
  1478. * INPUTS:  N/A.
  1479. * RETURNS: Number of sectors.
  1480. *********************************************************************/
  1481. unsigned int flashGetNumOfSectors(void)
  1482. {
  1483. return (flashTypes[NUMBER_OF_SECTORS]);
  1484. }