flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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, int bank_flag)
  34. {
  35. int id = 0, i = 0;
  36. static int FlagDev = 1;
  37. id = get_codes();
  38. if (FlagDev) {
  39. #ifdef DEBUG
  40. printf("Device ID of the Flash is %x\n", id);
  41. #endif
  42. FlagDev = 0;
  43. }
  44. info->flash_id = id;
  45. switch (bank_flag) {
  46. case 0:
  47. for (i = PriFlashABegin; i < SecFlashABegin; i++)
  48. info->start[i] = (baseaddr + (i * AFP_SectorSize1));
  49. info->size = 0x200000;
  50. info->sector_count = 32;
  51. break;
  52. case 1:
  53. info->start[0] = baseaddr + SecFlashASec1Off;
  54. info->start[1] = baseaddr + SecFlashASec2Off;
  55. info->start[2] = baseaddr + SecFlashASec3Off;
  56. info->start[3] = baseaddr + SecFlashASec4Off;
  57. info->size = 0x10000;
  58. info->sector_count = 4;
  59. break;
  60. case 2:
  61. info->start[0] = baseaddr + SecFlashBSec1Off;
  62. info->start[1] = baseaddr + SecFlashBSec2Off;
  63. info->start[2] = baseaddr + SecFlashBSec3Off;
  64. info->start[3] = baseaddr + SecFlashBSec4Off;
  65. info->size = 0x10000;
  66. info->sector_count = 4;
  67. break;
  68. }
  69. return (info->size);
  70. }
  71. unsigned long flash_init(void)
  72. {
  73. unsigned long size_b0, size_b1, size_b2;
  74. int i;
  75. size_b0 = size_b1 = size_b2 = 0;
  76. #ifdef DEBUG
  77. printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
  78. printf("Memory Map for the Flash\n");
  79. printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
  80. printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
  81. printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
  82. printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
  83. printf("Please type command flinfo for information on Sectors \n");
  84. #endif
  85. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  86. flash_info[i].flash_id = FLASH_UNKNOWN;
  87. }
  88. size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
  89. size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
  90. size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
  91. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  92. printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  93. size_b0, size_b0 >> 20);
  94. }
  95. (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
  96. (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: (0x%08X) ", info->flash_id);
  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], info->protect[i] ? " (RO)" : " ");
  119. }
  120. printf("\n");
  121. return;
  122. }
  123. int flash_erase(flash_info_t * info, int s_first, int s_last)
  124. {
  125. int cnt = 0, i;
  126. int prot, sect;
  127. prot = 0;
  128. for (sect = s_first; sect <= s_last; ++sect) {
  129. if (info->protect[sect])
  130. prot++;
  131. }
  132. if (prot)
  133. printf("- Warning: %d protected sectors will not be erased!\n",
  134. 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
  181. ("Invalid locations, Try writing to another location \n");
  182. return FLASH_FAIL;
  183. }
  184. get_sector_number(ulOffset, &nSector);
  185. read_flash(ulOffset, &d);
  186. if (d != 0xffff) {
  187. printf
  188. ("Flash not erased at offset 0x%x Please erase to reprogram \n",
  189. ulOffset);
  190. return FLASH_FAIL;
  191. }
  192. unlock_flash(ulOffset);
  193. if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
  194. printf("Error programming the flash \n");
  195. return FLASH_FAIL;
  196. }
  197. iShift += 16;
  198. }
  199. }
  200. if (nLeftover > 0) {
  201. if ((ulOffset >= INVALIDLOCNSTART)
  202. && (ulOffset < INVALIDLOCNEND))
  203. return FLASH_FAIL;
  204. get_sector_number(ulOffset, &nSector);
  205. read_flash(ulOffset, &d);
  206. if (d != 0xffff) {
  207. printf
  208. ("Flash already programmed. Please erase to reprogram \n");
  209. printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
  210. return FLASH_FAIL;
  211. }
  212. unlock_flash(ulOffset);
  213. if (write_flash(ulOffset, pnData[i]) < 0) {
  214. printf("Error programming the flash \n");
  215. return FLASH_FAIL;
  216. }
  217. }
  218. return FLASH_SUCCESS;
  219. }
  220. int read_data(long ulStart, long lCount, long lStride, int *pnData)
  221. {
  222. long i = 0;
  223. int j = 0;
  224. long ulOffset = ulStart;
  225. int iShift = 0;
  226. int iNumWords = 2;
  227. int nLeftover = lCount % 4;
  228. int nHi, nLow;
  229. int nSector = 0;
  230. for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
  231. for (iShift = 0, j = 0; j < iNumWords; j += 2) {
  232. if ((ulOffset >= INVALIDLOCNSTART)
  233. && (ulOffset < INVALIDLOCNEND))
  234. return FLASH_FAIL;
  235. get_sector_number(ulOffset, &nSector);
  236. read_flash(ulOffset, &nLow);
  237. ulOffset += (lStride * 2);
  238. read_flash(ulOffset, &nHi);
  239. ulOffset += (lStride * 2);
  240. pnData[i] = (nHi << 16) | nLow;
  241. }
  242. }
  243. if (nLeftover > 0) {
  244. if ((ulOffset >= INVALIDLOCNSTART)
  245. && (ulOffset < INVALIDLOCNEND))
  246. return FLASH_FAIL;
  247. get_sector_number(ulOffset, &nSector);
  248. read_flash(ulOffset, &pnData[i]);
  249. }
  250. return FLASH_SUCCESS;
  251. }
  252. int write_flash(long nOffset, int nValue)
  253. {
  254. long addr;
  255. addr = (CFG_FLASH_BASE + nOffset);
  256. __builtin_bfin_ssync();
  257. *(unsigned volatile short *)addr = nValue;
  258. __builtin_bfin_ssync();
  259. if (poll_toggle_bit(nOffset) < 0)
  260. return FLASH_FAIL;
  261. return FLASH_SUCCESS;
  262. }
  263. int read_flash(long nOffset, int *pnValue)
  264. {
  265. int nValue = 0x0;
  266. long addr = (CFG_FLASH_BASE + nOffset);
  267. if (nOffset != 0x2)
  268. reset_flash();
  269. __builtin_bfin_ssync();
  270. nValue = *(volatile unsigned short *)addr;
  271. __builtin_bfin_ssync();
  272. *pnValue = nValue;
  273. return TRUE;
  274. }
  275. int poll_toggle_bit(long lOffset)
  276. {
  277. unsigned int u1, u2;
  278. unsigned long timeout = 0xFFFFFFFF;
  279. volatile unsigned long *FB =
  280. (volatile unsigned long *)(0x20000000 + lOffset);
  281. while (1) {
  282. if (timeout < 0)
  283. break;
  284. u1 = *(volatile unsigned short *)FB;
  285. u2 = *(volatile unsigned short *)FB;
  286. if ((u1 & 0x0040) == (u2 & 0x0040))
  287. return FLASH_SUCCESS;
  288. if ((u2 & 0x0020) == 0x0000)
  289. continue;
  290. u1 = *(volatile unsigned short *)FB;
  291. if ((u2 & 0x0040) == (u1 & 0x0040))
  292. return FLASH_SUCCESS;
  293. else {
  294. reset_flash();
  295. return FLASH_FAIL;
  296. }
  297. timeout--;
  298. }
  299. printf("Time out occured \n");
  300. if (timeout < 0)
  301. return FLASH_FAIL;
  302. }
  303. void reset_flash(void)
  304. {
  305. write_flash(WRITESEQ1, RESET_VAL);
  306. /* Wait for 10 micro seconds */
  307. udelay(10);
  308. }
  309. int erase_flash(void)
  310. {
  311. write_flash(WRITESEQ1, WRITEDATA1);
  312. write_flash(WRITESEQ2, WRITEDATA2);
  313. write_flash(WRITESEQ3, WRITEDATA3);
  314. write_flash(WRITESEQ4, WRITEDATA4);
  315. write_flash(WRITESEQ5, WRITEDATA5);
  316. write_flash(WRITESEQ6, WRITEDATA6);
  317. if (poll_toggle_bit(0x0000) < 0)
  318. return FLASH_FAIL;
  319. write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
  320. write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
  321. write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
  322. write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
  323. write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
  324. write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
  325. if (poll_toggle_bit(SecFlashASec1Off) < 0)
  326. return FLASH_FAIL;
  327. write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
  328. write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
  329. write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
  330. write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
  331. write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
  332. write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
  333. if (poll_toggle_bit(PriFlashBOff) < 0)
  334. return FLASH_FAIL;
  335. write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
  336. write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
  337. write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
  338. write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
  339. write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
  340. write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
  341. if (poll_toggle_bit(SecFlashBOff) < 0)
  342. return FLASH_FAIL;
  343. return FLASH_SUCCESS;
  344. }
  345. int erase_block_flash(int nBlock, unsigned long address)
  346. {
  347. long ulSectorOff = 0x0;
  348. if ((nBlock < 0) || (nBlock > AFP_NumSectors))
  349. return FALSE;
  350. ulSectorOff = (address - CFG_FLASH_BASE);
  351. write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
  352. write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
  353. write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
  354. write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
  355. write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
  356. write_flash(ulSectorOff, BlockEraseVal);
  357. if (poll_toggle_bit(ulSectorOff) < 0)
  358. return FLASH_FAIL;
  359. return FLASH_SUCCESS;
  360. }
  361. void unlock_flash(long ulOffset)
  362. {
  363. unsigned long ulOffsetAddr = ulOffset;
  364. ulOffsetAddr &= 0xFFFF0000;
  365. write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
  366. write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
  367. write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
  368. }
  369. int get_codes()
  370. {
  371. int dev_id = 0;
  372. write_flash(WRITESEQ1, GETCODEDATA1);
  373. write_flash(WRITESEQ2, GETCODEDATA2);
  374. write_flash(WRITESEQ3, GETCODEDATA3);
  375. read_flash(0x0002, &dev_id);
  376. dev_id &= 0x00FF;
  377. reset_flash();
  378. return dev_id;
  379. }
  380. void get_sector_number(long ulOffset, int *pnSector)
  381. {
  382. int nSector = 0;
  383. if (ulOffset >= SecFlashAOff) {
  384. if ((ulOffset < SecFlashASec1Off)
  385. && (ulOffset < SecFlashASec2Off)) {
  386. nSector = SECT32;
  387. } else if ((ulOffset >= SecFlashASec2Off)
  388. && (ulOffset < SecFlashASec3Off)) {
  389. nSector = SECT33;
  390. } else if ((ulOffset >= SecFlashASec3Off)
  391. && (ulOffset < SecFlashASec4Off)) {
  392. nSector = SECT34;
  393. } else if ((ulOffset >= SecFlashASec4Off)
  394. && (ulOffset < SecFlashAEndOff)) {
  395. nSector = SECT35;
  396. }
  397. } else if (ulOffset >= SecFlashBOff) {
  398. if ((ulOffset < SecFlashBSec1Off)
  399. && (ulOffset < SecFlashBSec2Off)) {
  400. nSector = SECT36;
  401. }
  402. if ((ulOffset < SecFlashBSec2Off)
  403. && (ulOffset < SecFlashBSec3Off)) {
  404. nSector = SECT37;
  405. }
  406. if ((ulOffset < SecFlashBSec3Off)
  407. && (ulOffset < SecFlashBSec4Off)) {
  408. nSector = SECT38;
  409. }
  410. if ((ulOffset < SecFlashBSec4Off)
  411. && (ulOffset < SecFlashBEndOff)) {
  412. nSector = SECT39;
  413. }
  414. } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
  415. nSector = ulOffset & 0xffff0000;
  416. nSector = ulOffset >> 16;
  417. nSector = nSector & 0x000ff;
  418. }
  419. if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
  420. *pnSector = nSector;
  421. }
  422. }