flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * U-boot - flash.c Flash driver for PSD4256GV
  3. *
  4. * Copyright (c) 2005-2007 Analog Devices Inc.
  5. * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
  6. *
  7. * (C) Copyright 2000-2004
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  26. * MA 02110-1301 USA
  27. */
  28. #include <asm/io.h>
  29. #include "flash-defines.h"
  30. void flash_reset(void)
  31. {
  32. reset_flash();
  33. }
  34. unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
  35. {
  36. int id = 0, i = 0;
  37. static int FlagDev = 1;
  38. id = get_codes();
  39. if (FlagDev) {
  40. #ifdef DEBUG
  41. printf("Device ID of the Flash is %x\n", id);
  42. #endif
  43. FlagDev = 0;
  44. }
  45. info->flash_id = id;
  46. switch (bank_flag) {
  47. case 0:
  48. for (i = PriFlashABegin; i < SecFlashABegin; i++)
  49. info->start[i] = (baseaddr + (i * AFP_SectorSize1));
  50. info->size = 0x200000;
  51. info->sector_count = 32;
  52. break;
  53. case 1:
  54. info->start[0] = baseaddr + SecFlashASec1Off;
  55. info->start[1] = baseaddr + SecFlashASec2Off;
  56. info->start[2] = baseaddr + SecFlashASec3Off;
  57. info->start[3] = baseaddr + SecFlashASec4Off;
  58. info->size = 0x10000;
  59. info->sector_count = 4;
  60. break;
  61. case 2:
  62. info->start[0] = baseaddr + SecFlashBSec1Off;
  63. info->start[1] = baseaddr + SecFlashBSec2Off;
  64. info->start[2] = baseaddr + SecFlashBSec3Off;
  65. info->start[3] = baseaddr + SecFlashBSec4Off;
  66. info->size = 0x10000;
  67. info->sector_count = 4;
  68. break;
  69. }
  70. return (info->size);
  71. }
  72. unsigned long flash_init(void)
  73. {
  74. unsigned long size_b0, size_b1, size_b2;
  75. int i;
  76. size_b0 = size_b1 = size_b2 = 0;
  77. #ifdef DEBUG
  78. printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
  79. printf("Memory Map for the Flash\n");
  80. printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
  81. printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
  82. printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
  83. printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
  84. printf("Please type command flinfo for information on Sectors \n");
  85. #endif
  86. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  87. flash_info[i].flash_id = FLASH_UNKNOWN;
  88. }
  89. size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
  90. size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
  91. size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
  92. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  93. printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  94. size_b0, size_b0 >> 20);
  95. }
  96. (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
  97. (flash_info[0].start[2] - 1), &flash_info[0]);
  98. return (size_b0 + size_b1 + size_b2);
  99. }
  100. void flash_print_info(flash_info_t * info)
  101. {
  102. int i;
  103. if (info->flash_id == FLASH_UNKNOWN) {
  104. printf("missing or unknown FLASH type\n");
  105. return;
  106. }
  107. switch (info->flash_id) {
  108. case FLASH_PSD4256GV:
  109. printf("ST Microelectronics ");
  110. break;
  111. default:
  112. printf("Unknown Vendor: (0x%08X) ", info->flash_id);
  113. break;
  114. }
  115. for (i = 0; i < info->sector_count; ++i) {
  116. if ((i % 5) == 0)
  117. printf("\n ");
  118. printf(" %08lX%s",
  119. info->start[i], info->protect[i] ? " (RO)" : " ");
  120. }
  121. printf("\n");
  122. return;
  123. }
  124. int flash_erase(flash_info_t * info, int s_first, int s_last)
  125. {
  126. int cnt = 0, i;
  127. int prot, sect;
  128. prot = 0;
  129. for (sect = s_first; sect <= s_last; ++sect) {
  130. if (info->protect[sect])
  131. prot++;
  132. }
  133. if (prot)
  134. printf("- Warning: %d protected sectors will not be erased!\n",
  135. prot);
  136. else
  137. printf("\n");
  138. cnt = s_last - s_first + 1;
  139. if (cnt == FLASH_TOT_SECT) {
  140. printf("Erasing flash, Please Wait \n");
  141. if (erase_flash() < 0) {
  142. printf("Erasing flash failed \n");
  143. return FLASH_FAIL;
  144. }
  145. } else {
  146. printf("Erasing Flash locations, Please Wait\n");
  147. for (i = s_first; i <= s_last; i++) {
  148. if (info->protect[i] == 0) { /* not protected */
  149. if (erase_block_flash(i, info->start[i]) < 0) {
  150. printf("Error Sector erasing \n");
  151. return FLASH_FAIL;
  152. }
  153. }
  154. }
  155. }
  156. return FLASH_SUCCESS;
  157. }
  158. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  159. {
  160. int ret;
  161. int d;
  162. if (addr % 2) {
  163. read_flash(addr - 1 - CFG_FLASH_BASE, &d);
  164. d = (int)((d & 0x00FF) | (*src++ << 8));
  165. ret = write_data(addr - 1, 2, (uchar *) & d);
  166. if (ret == FLASH_FAIL)
  167. return ERR_NOT_ERASED;
  168. ret = write_data(addr + 1, cnt - 1, src);
  169. } else
  170. ret = write_data(addr, cnt, src);
  171. if (ret == FLASH_FAIL)
  172. return ERR_NOT_ERASED;
  173. return FLASH_SUCCESS;
  174. }
  175. int write_data(long lStart, long lCount, uchar * pnData)
  176. {
  177. long i = 0;
  178. unsigned long ulOffset = lStart - CFG_FLASH_BASE;
  179. int d;
  180. int nSector = 0;
  181. int flag = 0;
  182. if (lCount % 2) {
  183. flag = 1;
  184. lCount = lCount - 1;
  185. }
  186. for (i = 0; i < lCount - 1; i += 2, ulOffset += 2) {
  187. get_sector_number(ulOffset, &nSector);
  188. read_flash(ulOffset, &d);
  189. if (d != 0xffff) {
  190. printf
  191. ("Flash not erased at offset 0x%x Please erase to reprogram \n",
  192. ulOffset);
  193. return FLASH_FAIL;
  194. }
  195. unlock_flash(ulOffset);
  196. d = (int)(pnData[i] | pnData[i + 1] << 8);
  197. write_flash(ulOffset, d);
  198. if (poll_toggle_bit(ulOffset) < 0) {
  199. printf("Error programming the flash \n");
  200. return FLASH_FAIL;
  201. }
  202. if ((i > 0) && (!(i % AFP_SectorSize2)))
  203. printf(".");
  204. }
  205. if (flag) {
  206. get_sector_number(ulOffset, &nSector);
  207. read_flash(ulOffset, &d);
  208. if (d != 0xffff) {
  209. printf
  210. ("Flash not erased at offset 0x%x Please erase to reprogram \n",
  211. ulOffset);
  212. return FLASH_FAIL;
  213. }
  214. unlock_flash(ulOffset);
  215. d = (int)(pnData[i] | (d & 0xFF00));
  216. write_flash(ulOffset, d);
  217. if (poll_toggle_bit(ulOffset) < 0) {
  218. printf("Error programming the flash \n");
  219. return FLASH_FAIL;
  220. }
  221. }
  222. return FLASH_SUCCESS;
  223. }
  224. int read_data(long ulStart, long lCount, long lStride, int *pnData)
  225. {
  226. long i = 0;
  227. int j = 0;
  228. long ulOffset = ulStart;
  229. int iShift = 0;
  230. int iNumWords = 2;
  231. int nLeftover = lCount % 4;
  232. int nHi, nLow;
  233. int nSector = 0;
  234. for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
  235. for (iShift = 0, j = 0; j < iNumWords; j += 2) {
  236. if ((ulOffset >= INVALIDLOCNSTART)
  237. && (ulOffset < INVALIDLOCNEND))
  238. return FLASH_FAIL;
  239. get_sector_number(ulOffset, &nSector);
  240. read_flash(ulOffset, &nLow);
  241. ulOffset += (lStride * 2);
  242. read_flash(ulOffset, &nHi);
  243. ulOffset += (lStride * 2);
  244. pnData[i] = (nHi << 16) | nLow;
  245. }
  246. }
  247. if (nLeftover > 0) {
  248. if ((ulOffset >= INVALIDLOCNSTART)
  249. && (ulOffset < INVALIDLOCNEND))
  250. return FLASH_FAIL;
  251. get_sector_number(ulOffset, &nSector);
  252. read_flash(ulOffset, &pnData[i]);
  253. }
  254. return FLASH_SUCCESS;
  255. }
  256. int write_flash(long nOffset, int nValue)
  257. {
  258. long addr;
  259. addr = (CFG_FLASH_BASE + nOffset);
  260. sync();
  261. *(unsigned volatile short *)addr = nValue;
  262. sync();
  263. if (poll_toggle_bit(nOffset) < 0)
  264. return FLASH_FAIL;
  265. return FLASH_SUCCESS;
  266. }
  267. int read_flash(long nOffset, int *pnValue)
  268. {
  269. int nValue = 0x0;
  270. long addr = (CFG_FLASH_BASE + nOffset);
  271. if (nOffset != 0x2)
  272. reset_flash();
  273. sync();
  274. nValue = *(volatile unsigned short *)addr;
  275. sync();
  276. *pnValue = nValue;
  277. return TRUE;
  278. }
  279. int poll_toggle_bit(long lOffset)
  280. {
  281. unsigned int u1, u2;
  282. unsigned long timeout = 0xFFFFFFFF;
  283. volatile unsigned long *FB =
  284. (volatile unsigned long *)(0x20000000 + lOffset);
  285. while (1) {
  286. if (timeout < 0)
  287. break;
  288. u1 = *(volatile unsigned short *)FB;
  289. u2 = *(volatile unsigned short *)FB;
  290. if ((u1 & 0x0040) == (u2 & 0x0040))
  291. return FLASH_SUCCESS;
  292. if ((u2 & 0x0020) == 0x0000)
  293. continue;
  294. u1 = *(volatile unsigned short *)FB;
  295. if ((u2 & 0x0040) == (u1 & 0x0040))
  296. return FLASH_SUCCESS;
  297. else {
  298. reset_flash();
  299. return FLASH_FAIL;
  300. }
  301. timeout--;
  302. }
  303. printf("Time out occured \n");
  304. if (timeout < 0)
  305. return FLASH_FAIL;
  306. }
  307. void reset_flash(void)
  308. {
  309. write_flash(WRITESEQ1, RESET_VAL);
  310. /* Wait for 10 micro seconds */
  311. udelay(10);
  312. }
  313. int erase_flash(void)
  314. {
  315. write_flash(WRITESEQ1, WRITEDATA1);
  316. write_flash(WRITESEQ2, WRITEDATA2);
  317. write_flash(WRITESEQ3, WRITEDATA3);
  318. write_flash(WRITESEQ4, WRITEDATA4);
  319. write_flash(WRITESEQ5, WRITEDATA5);
  320. write_flash(WRITESEQ6, WRITEDATA6);
  321. if (poll_toggle_bit(0x0000) < 0)
  322. return FLASH_FAIL;
  323. write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
  324. write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
  325. write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
  326. write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
  327. write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
  328. write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
  329. if (poll_toggle_bit(SecFlashASec1Off) < 0)
  330. return FLASH_FAIL;
  331. write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
  332. write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
  333. write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
  334. write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
  335. write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
  336. write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
  337. if (poll_toggle_bit(PriFlashBOff) < 0)
  338. return FLASH_FAIL;
  339. write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
  340. write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
  341. write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
  342. write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
  343. write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
  344. write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
  345. if (poll_toggle_bit(SecFlashBOff) < 0)
  346. return FLASH_FAIL;
  347. return FLASH_SUCCESS;
  348. }
  349. int erase_block_flash(int nBlock, unsigned long address)
  350. {
  351. long ulSectorOff = 0x0;
  352. if ((nBlock < 0) || (nBlock > AFP_NumSectors))
  353. return FALSE;
  354. ulSectorOff = (address - CFG_FLASH_BASE);
  355. write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
  356. write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
  357. write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
  358. write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
  359. write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
  360. write_flash(ulSectorOff, BlockEraseVal);
  361. if (poll_toggle_bit(ulSectorOff) < 0)
  362. return FLASH_FAIL;
  363. return FLASH_SUCCESS;
  364. }
  365. void unlock_flash(long ulOffset)
  366. {
  367. unsigned long ulOffsetAddr = ulOffset;
  368. ulOffsetAddr &= 0xFFFF0000;
  369. write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
  370. write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
  371. write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
  372. }
  373. int get_codes()
  374. {
  375. int dev_id = 0;
  376. write_flash(WRITESEQ1, GETCODEDATA1);
  377. write_flash(WRITESEQ2, GETCODEDATA2);
  378. write_flash(WRITESEQ3, GETCODEDATA3);
  379. read_flash(0x0002, &dev_id);
  380. dev_id &= 0x00FF;
  381. reset_flash();
  382. return dev_id;
  383. }
  384. void get_sector_number(long ulOffset, int *pnSector)
  385. {
  386. int nSector = 0;
  387. if (ulOffset >= SecFlashAOff) {
  388. if ((ulOffset < SecFlashASec1Off)
  389. && (ulOffset < SecFlashASec2Off)) {
  390. nSector = SECT32;
  391. } else if ((ulOffset >= SecFlashASec2Off)
  392. && (ulOffset < SecFlashASec3Off)) {
  393. nSector = SECT33;
  394. } else if ((ulOffset >= SecFlashASec3Off)
  395. && (ulOffset < SecFlashASec4Off)) {
  396. nSector = SECT34;
  397. } else if ((ulOffset >= SecFlashASec4Off)
  398. && (ulOffset < SecFlashAEndOff)) {
  399. nSector = SECT35;
  400. }
  401. } else if (ulOffset >= SecFlashBOff) {
  402. if ((ulOffset < SecFlashBSec1Off)
  403. && (ulOffset < SecFlashBSec2Off)) {
  404. nSector = SECT36;
  405. }
  406. if ((ulOffset < SecFlashBSec2Off)
  407. && (ulOffset < SecFlashBSec3Off)) {
  408. nSector = SECT37;
  409. }
  410. if ((ulOffset < SecFlashBSec3Off)
  411. && (ulOffset < SecFlashBSec4Off)) {
  412. nSector = SECT38;
  413. }
  414. if ((ulOffset < SecFlashBSec4Off)
  415. && (ulOffset < SecFlashBEndOff)) {
  416. nSector = SECT39;
  417. }
  418. } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
  419. nSector = ulOffset & 0xffff0000;
  420. nSector = ulOffset >> 16;
  421. nSector = nSector & 0x000ff;
  422. }
  423. if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
  424. *pnSector = nSector;
  425. }
  426. }