flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * U-boot - flash.c Flash driver for PSD4256GV
  3. *
  4. * Copyright (c) 2005 blackfin.uclinux.org
  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., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include "flash-defines.h"
  29. void flash_reset(void)
  30. {
  31. reset_flash();
  32. }
  33. unsigned long flash_get_size(ulong baseaddr, flash_info_t * info,
  34. 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,(flash_info[0].start[2] - 1),&flash_info[0]);
  97. return (size_b0 + size_b1 + size_b2);
  98. }
  99. void flash_print_info(flash_info_t * info)
  100. {
  101. int i;
  102. if (info->flash_id == FLASH_UNKNOWN) {
  103. printf("missing or unknown FLASH type\n");
  104. return;
  105. }
  106. switch (info->flash_id) {
  107. case FLASH_PSD4256GV:
  108. printf("ST Microelectronics ");
  109. break;
  110. default:
  111. printf("Unknown Vendor ");
  112. break;
  113. }
  114. for (i = 0; i < info->sector_count; ++i) {
  115. if ((i % 5) == 0)
  116. printf("\n ");
  117. printf(" %08lX%s",
  118. info->start[i],
  119. 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", prot);
  135. else
  136. printf ("\n");
  137. cnt = s_last - s_first + 1;
  138. if (cnt == FLASH_TOT_SECT) {
  139. printf("Erasing flash, Please Wait \n");
  140. if(erase_flash() < 0) {
  141. printf("Erasing flash failed \n");
  142. return FLASH_FAIL;
  143. }
  144. } else {
  145. printf("Erasing Flash locations, Please Wait\n");
  146. for (i = s_first; i <= s_last; i++) {
  147. if (info->protect[i] == 0) { /* not protected */
  148. if(erase_block_flash(i, info->start[i]) < 0) {
  149. printf("Error Sector erasing \n");
  150. return FLASH_FAIL;
  151. }
  152. }
  153. }
  154. }
  155. return FLASH_SUCCESS;
  156. }
  157. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  158. {
  159. int ret;
  160. ret = write_data(addr, cnt, 1, (int *) src);
  161. if(ret == FLASH_FAIL)
  162. return ERR_NOT_ERASED;
  163. return FLASH_SUCCESS;
  164. }
  165. int write_data(long lStart, long lCount, long lStride, int *pnData)
  166. {
  167. long i = 0;
  168. int j = 0;
  169. unsigned long ulOffset = lStart - CFG_FLASH_BASE;
  170. int d;
  171. int iShift = 0;
  172. int iNumWords = 2;
  173. int nLeftover = lCount % 4;
  174. int nSector = 0;
  175. for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
  176. for (iShift = 0, j = 0; (j < iNumWords);
  177. j++, ulOffset += (lStride * 2)) {
  178. if ((ulOffset >= INVALIDLOCNSTART)
  179. && (ulOffset < INVALIDLOCNEND)) {
  180. printf("Invalid locations, Try writing to another location \n");
  181. return FLASH_FAIL;
  182. }
  183. get_sector_number(ulOffset, &nSector);
  184. read_flash(ulOffset,&d);
  185. if(d != 0xffff) {
  186. printf("Flash not erased at offset 0x%x Please erase to reprogram \n",ulOffset);
  187. return FLASH_FAIL;
  188. }
  189. unlock_flash(ulOffset);
  190. if(write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
  191. printf("Error programming the flash \n");
  192. return FLASH_FAIL;
  193. }
  194. iShift += 16;
  195. }
  196. }
  197. if (nLeftover > 0) {
  198. if ((ulOffset >= INVALIDLOCNSTART)
  199. && (ulOffset < INVALIDLOCNEND))
  200. return FLASH_FAIL;
  201. get_sector_number(ulOffset, &nSector);
  202. read_flash(ulOffset,&d);
  203. if(d != 0xffff) {
  204. printf("Flash already programmed. Please erase to reprogram \n");
  205. printf("uloffset = 0x%x \t d = 0x%x\n",ulOffset,d);
  206. return FLASH_FAIL;
  207. }
  208. unlock_flash(ulOffset);
  209. if(write_flash(ulOffset, pnData[i]) < 0) {
  210. printf("Error programming the flash \n");
  211. return FLASH_FAIL;
  212. }
  213. }
  214. return FLASH_SUCCESS;
  215. }
  216. int read_data(long ulStart, long lCount, long lStride, int *pnData)
  217. {
  218. long i = 0;
  219. int j = 0;
  220. long ulOffset = ulStart;
  221. int iShift = 0;
  222. int iNumWords = 2;
  223. int nLeftover = lCount % 4;
  224. int nHi, nLow;
  225. int nSector = 0;
  226. for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
  227. for (iShift = 0, j = 0; j < iNumWords; j += 2) {
  228. if ((ulOffset >= INVALIDLOCNSTART)
  229. && (ulOffset < INVALIDLOCNEND))
  230. return FLASH_FAIL;
  231. get_sector_number(ulOffset, &nSector);
  232. read_flash(ulOffset, &nLow);
  233. ulOffset += (lStride * 2);
  234. read_flash(ulOffset, &nHi);
  235. ulOffset += (lStride * 2);
  236. pnData[i] = (nHi << 16) | nLow;
  237. }
  238. }
  239. if (nLeftover > 0) {
  240. if ((ulOffset >= INVALIDLOCNSTART)
  241. && (ulOffset < INVALIDLOCNEND))
  242. return FLASH_FAIL;
  243. get_sector_number(ulOffset, &nSector);
  244. read_flash(ulOffset, &pnData[i]);
  245. }
  246. return FLASH_SUCCESS;
  247. }
  248. int write_flash(long nOffset, int nValue)
  249. {
  250. long addr;
  251. addr = (CFG_FLASH_BASE + nOffset);
  252. asm("ssync;");
  253. *(unsigned volatile short *) addr = nValue;
  254. asm("ssync;");
  255. if(poll_toggle_bit(nOffset) < 0)
  256. return FLASH_FAIL;
  257. return FLASH_SUCCESS;
  258. }
  259. int read_flash(long nOffset, int *pnValue)
  260. {
  261. int nValue = 0x0;
  262. long addr = (CFG_FLASH_BASE + nOffset);
  263. if (nOffset != 0x2)
  264. reset_flash();
  265. asm("ssync;");
  266. nValue = *(volatile unsigned short *) addr;
  267. asm("ssync;");
  268. *pnValue = nValue;
  269. return TRUE;
  270. }
  271. int poll_toggle_bit(long lOffset)
  272. {
  273. unsigned int u1,u2;
  274. unsigned long timeout = 0xFFFFFFFF;
  275. volatile unsigned long *FB = (volatile unsigned long *)(0x20000000 + lOffset);
  276. while(1) {
  277. if(timeout < 0)
  278. break;
  279. u1 = *(volatile unsigned short *)FB;
  280. u2 = *(volatile unsigned short *)FB;
  281. if((u1 & 0x0040) == (u2 & 0x0040))
  282. return FLASH_SUCCESS;
  283. if((u2 & 0x0020) == 0x0000)
  284. continue;
  285. u1 = *(volatile unsigned short *)FB;
  286. if((u2 & 0x0040) == (u1 & 0x0040))
  287. return FLASH_SUCCESS;
  288. else {
  289. reset_flash();
  290. return FLASH_FAIL;
  291. }
  292. timeout--;
  293. }
  294. printf("Time out occured \n");
  295. if(timeout <0) return FLASH_FAIL;
  296. }
  297. void reset_flash(void)
  298. {
  299. write_flash(WRITESEQ1, RESET_VAL);
  300. /* Wait for 10 micro seconds */
  301. udelay(10);
  302. }
  303. int erase_flash(void)
  304. {
  305. write_flash(WRITESEQ1, WRITEDATA1);
  306. write_flash(WRITESEQ2, WRITEDATA2);
  307. write_flash(WRITESEQ3, WRITEDATA3);
  308. write_flash(WRITESEQ4, WRITEDATA4);
  309. write_flash(WRITESEQ5, WRITEDATA5);
  310. write_flash(WRITESEQ6, WRITEDATA6);
  311. if(poll_toggle_bit(0x0000) < 0)
  312. return FLASH_FAIL;
  313. write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
  314. write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
  315. write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
  316. write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
  317. write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
  318. write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
  319. if(poll_toggle_bit(SecFlashASec1Off) < 0)
  320. return FLASH_FAIL;
  321. write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
  322. write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
  323. write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
  324. write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
  325. write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
  326. write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
  327. if(poll_toggle_bit(PriFlashBOff) <0)
  328. return FLASH_FAIL;
  329. write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
  330. write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
  331. write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
  332. write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
  333. write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
  334. write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
  335. if(poll_toggle_bit(SecFlashBOff) < 0)
  336. return FLASH_FAIL;
  337. return FLASH_SUCCESS;
  338. }
  339. int erase_block_flash(int nBlock, unsigned long address)
  340. {
  341. long ulSectorOff = 0x0;
  342. if ((nBlock < 0) || (nBlock > AFP_NumSectors))
  343. return FALSE;
  344. ulSectorOff = (address - CFG_FLASH_BASE);
  345. write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
  346. write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
  347. write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
  348. write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
  349. write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
  350. write_flash(ulSectorOff, BlockEraseVal);
  351. if(poll_toggle_bit(ulSectorOff) < 0)
  352. return FLASH_FAIL;
  353. return FLASH_SUCCESS;
  354. }
  355. void unlock_flash(long ulOffset)
  356. {
  357. unsigned long ulOffsetAddr = ulOffset;
  358. ulOffsetAddr &= 0xFFFF0000;
  359. write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
  360. write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
  361. write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
  362. }
  363. int get_codes()
  364. {
  365. int dev_id = 0;
  366. write_flash(WRITESEQ1, GETCODEDATA1);
  367. write_flash(WRITESEQ2, GETCODEDATA2);
  368. write_flash(WRITESEQ3, GETCODEDATA3);
  369. read_flash(0x0002, &dev_id);
  370. dev_id &= 0x00FF;
  371. reset_flash();
  372. return dev_id;
  373. }
  374. void get_sector_number(long ulOffset, int *pnSector)
  375. {
  376. int nSector = 0;
  377. if (ulOffset >= SecFlashAOff) {
  378. if ((ulOffset < SecFlashASec1Off)
  379. && (ulOffset < SecFlashASec2Off)) {
  380. nSector = SECT32;
  381. } else if ((ulOffset >= SecFlashASec2Off)
  382. && (ulOffset < SecFlashASec3Off)) {
  383. nSector = SECT33;
  384. } else if ((ulOffset >= SecFlashASec3Off)
  385. && (ulOffset < SecFlashASec4Off)) {
  386. nSector = SECT34;
  387. } else if ((ulOffset >= SecFlashASec4Off)
  388. && (ulOffset < SecFlashAEndOff)) {
  389. nSector = SECT35;
  390. }
  391. } else if (ulOffset >= SecFlashBOff) {
  392. if ((ulOffset < SecFlashBSec1Off)
  393. && (ulOffset < SecFlashBSec2Off)) {
  394. nSector = SECT36;
  395. }
  396. if ((ulOffset < SecFlashBSec2Off)
  397. && (ulOffset < SecFlashBSec3Off)) {
  398. nSector = SECT37;
  399. }
  400. if ((ulOffset < SecFlashBSec3Off)
  401. && (ulOffset < SecFlashBSec4Off)) {
  402. nSector = SECT38;
  403. }
  404. if ((ulOffset < SecFlashBSec4Off)
  405. && (ulOffset < SecFlashBEndOff)) {
  406. nSector = SECT39;
  407. }
  408. } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
  409. nSector = ulOffset & 0xffff0000;
  410. nSector = ulOffset >> 16;
  411. nSector = nSector & 0x000ff;
  412. }
  413. if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
  414. *pnSector = nSector;
  415. }
  416. }