flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * (C) Copyright 2004
  3. * Elmeg Communications Systems GmbH, Juergen Selent (j.selent@elmeg.de)
  4. *
  5. * Support for the Elmeg VoVPN Gateway Module
  6. * ------------------------------------------
  7. * This is a signle bank flashdriver for INTEL 28F320J3, 28F640J3
  8. * and 28F128J3A flashs working in 8 Bit mode.
  9. *
  10. * Most of this code is taken from existing u-boot source code.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  29. #define FLASH_CMD_READ_ID 0x90
  30. #define FLASH_CMD_READ_STATUS 0x70
  31. #define FLASH_CMD_RESET 0xff
  32. #define FLASH_CMD_BLOCK_ERASE 0x20
  33. #define FLASH_CMD_ERASE_CONFIRM 0xd0
  34. #define FLASH_CMD_CLEAR_STATUS 0x50
  35. #define FLASH_CMD_SUSPEND_ERASE 0xb0
  36. #define FLASH_CMD_WRITE 0x40
  37. #define FLASH_CMD_WRITE_BUFF 0xe8
  38. #define FLASH_CMD_PROG_RESUME 0xd0
  39. #define FLASH_CMD_PROTECT 0x60
  40. #define FLASH_CMD_PROTECT_SET 0x01
  41. #define FLASH_CMD_PROTECT_CLEAR 0xd0
  42. #define FLASH_STATUS_DONE 0x80
  43. #define FLASH_WRITE_BUFFER_SIZE 32
  44. #ifdef CFG_FLASH_16BIT
  45. #define FLASH_WORD_SIZE unsigned short
  46. #define FLASH_ID_MASK 0xffff
  47. #define FLASH_CMD_ADDR_SHIFT 0
  48. #else
  49. #define FLASH_WORD_SIZE unsigned char
  50. #define FLASH_ID_MASK 0xff
  51. /* A0 is not used in either 8x or 16x for READ ID */
  52. #define FLASH_CMD_ADDR_SHIFT 1
  53. #endif
  54. static unsigned long
  55. flash_get(volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
  56. {
  57. volatile FLASH_WORD_SIZE *p;
  58. FLASH_WORD_SIZE value;
  59. int i;
  60. addr[0] = FLASH_CMD_READ_ID;
  61. /* manufactor */
  62. value = addr[0 << FLASH_CMD_ADDR_SHIFT];
  63. switch (value) {
  64. case (INTEL_MANUFACT & FLASH_ID_MASK):
  65. info->flash_id = FLASH_MAN_INTEL;
  66. break;
  67. default:
  68. info->flash_id = FLASH_UNKNOWN;
  69. info->sector_count = 0;
  70. info->size = 0;
  71. *addr = FLASH_CMD_RESET;
  72. return (0);
  73. }
  74. /* device */
  75. value = addr[1 << FLASH_CMD_ADDR_SHIFT];
  76. switch (value) {
  77. case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
  78. info->flash_id += FLASH_28F320J3A;
  79. info->sector_count = 32;
  80. info->size = 0x00400000;
  81. break;
  82. case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
  83. info->flash_id += FLASH_28F640J3A;
  84. info->sector_count = 64;
  85. info->size = 0x00800000;
  86. break;
  87. case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
  88. info->flash_id += FLASH_28F128J3A;
  89. info->sector_count = 128;
  90. info->size = 0x01000000;
  91. break;
  92. default:
  93. info->flash_id = FLASH_UNKNOWN;
  94. info->sector_count = 0;
  95. info->size = 0;
  96. *addr = FLASH_CMD_RESET;
  97. return (0);
  98. }
  99. /* setup sectors */
  100. for (i = 0; i < info->sector_count; i++) {
  101. info->start[i] = (unsigned long)addr + (i * info->size/info->sector_count);
  102. }
  103. /* check protected sectors */
  104. for (i = 0; i < info->sector_count; i++) {
  105. p = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  106. info->protect[i] = p[2 << FLASH_CMD_ADDR_SHIFT] & 1;
  107. }
  108. /* reset bank */
  109. *addr = FLASH_CMD_RESET;
  110. return (info->size);
  111. }
  112. unsigned long
  113. flash_init(void)
  114. {
  115. unsigned long size;
  116. int i;
  117. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  118. flash_info[i].flash_id = FLASH_UNKNOWN;
  119. }
  120. size = flash_get((volatile FLASH_WORD_SIZE *)CFG_FLASH_BASE, &flash_info[0]);
  121. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  122. printf ("## Unknown FLASH Size=0x%08lx\n", size);
  123. return (0);
  124. }
  125. /* always protect 1 sector containing the HRCW */
  126. flash_protect(FLAG_PROTECT_SET,
  127. flash_info[0].start[0],
  128. flash_info[0].start[1] - 1,
  129. &flash_info[0]);
  130. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  131. flash_protect(FLAG_PROTECT_SET,
  132. CFG_MONITOR_FLASH,
  133. CFG_MONITOR_FLASH+CFG_MONITOR_LEN-1,
  134. &flash_info[0]);
  135. #endif
  136. #ifdef CONFIG_ENV_IS_IN_FLASH
  137. flash_protect(FLAG_PROTECT_SET,
  138. CONFIG_ENV_ADDR,
  139. CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
  140. &flash_info[0]);
  141. #endif
  142. return (size);
  143. }
  144. void
  145. flash_print_info(flash_info_t *info)
  146. {
  147. int i;
  148. if (info->flash_id == FLASH_UNKNOWN) {
  149. printf ("missing or unknown FLASH type\n");
  150. return;
  151. }
  152. switch (info->flash_id & FLASH_VENDMASK) {
  153. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  154. default: printf ("Unknown Vendor "); break;
  155. }
  156. switch (info->flash_id & FLASH_TYPEMASK) {
  157. case FLASH_28F320J3A: printf ("28F320JA3 (32 Mbit)\n");
  158. break;
  159. case FLASH_28F640J3A: printf ("28F640JA3 (64 Mbit)\n");
  160. break;
  161. case FLASH_28F128J3A: printf ("28F128JA3 (128 Mbit)\n");
  162. break;
  163. default: printf ("Unknown Chip Type");
  164. break;
  165. }
  166. printf (" Size: %ld MB in %d Sectors\n",
  167. info->size >> 20, info->sector_count);
  168. printf (" Sector Start Addresses:");
  169. for (i=0; i<info->sector_count; ++i) {
  170. if ((i % 5) == 0)
  171. printf ("\n ");
  172. printf (" %08lX%s",
  173. info->start[i],
  174. info->protect[i] ? " (RO)" : " "
  175. );
  176. }
  177. printf ("\n");
  178. }
  179. int
  180. flash_erase(flash_info_t *info, int s_first, int s_last)
  181. {
  182. unsigned long start, now, last;
  183. int flag, prot, sect;
  184. volatile FLASH_WORD_SIZE *addr;
  185. FLASH_WORD_SIZE status;
  186. if ((s_first < 0) || (s_first > s_last)) {
  187. if (info->flash_id == FLASH_UNKNOWN) {
  188. printf ("- missing\n");
  189. } else {
  190. printf ("- no sectors to erase\n");
  191. }
  192. return (1);
  193. }
  194. if (info->flash_id == FLASH_UNKNOWN) {
  195. printf ("Cannot erase unknown flash - aborted\n");
  196. return (1);
  197. }
  198. prot = 0;
  199. for (sect=s_first; sect<=s_last; ++sect) {
  200. if (info->protect[sect]) {
  201. prot++;
  202. }
  203. }
  204. if (prot) {
  205. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  206. } else {
  207. printf ("\n");
  208. }
  209. start = get_timer (0);
  210. last = start;
  211. for (sect = s_first; sect<=s_last; sect++) {
  212. if (info->protect[sect]) {
  213. continue;
  214. }
  215. addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
  216. /* Disable interrupts which might cause a timeout here */
  217. flag = disable_interrupts();
  218. #ifdef DEBUG
  219. printf("Erase sector %d at start addr 0x%08X", sect, (unsigned int)info->start[sect]);
  220. #endif
  221. *addr = FLASH_CMD_CLEAR_STATUS;
  222. *addr = FLASH_CMD_BLOCK_ERASE;
  223. *addr = FLASH_CMD_ERASE_CONFIRM;
  224. /* re-enable interrupts if necessary */
  225. if (flag) {
  226. enable_interrupts();
  227. }
  228. /* wait at least 80us - let's wait 1 ms */
  229. udelay (1000);
  230. while (((status = *addr) & FLASH_STATUS_DONE) != FLASH_STATUS_DONE) {
  231. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  232. printf("Flash erase timeout at address %lx\n", info->start[sect]);
  233. *addr = FLASH_CMD_SUSPEND_ERASE;
  234. *addr = FLASH_CMD_RESET;
  235. return (1);
  236. }
  237. /* show that we're waiting */
  238. if ((now - last) > 1000) { /* every second */
  239. putc ('.');
  240. last = now;
  241. }
  242. }
  243. *addr = FLASH_CMD_RESET;
  244. }
  245. printf (" done\n");
  246. return (0);
  247. }
  248. static int
  249. write_buff2( volatile FLASH_WORD_SIZE *dst,
  250. volatile FLASH_WORD_SIZE *src,
  251. unsigned long cnt )
  252. {
  253. unsigned long start;
  254. FLASH_WORD_SIZE status;
  255. int flag, i;
  256. start = get_timer (0);
  257. while (1) {
  258. /* Disable interrupts which might cause a timeout here */
  259. flag = disable_interrupts();
  260. dst[0] = FLASH_CMD_WRITE_BUFF;
  261. if ((status = *dst) & FLASH_STATUS_DONE) {
  262. break;
  263. }
  264. /* re-enable interrupts if necessary */
  265. if (flag) {
  266. enable_interrupts();
  267. }
  268. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  269. return (-1);
  270. }
  271. }
  272. dst[0] = (FLASH_WORD_SIZE)(cnt - 1);
  273. for (i=0; i<cnt; i++) {
  274. dst[i] = src[i];
  275. }
  276. dst[0] = FLASH_CMD_PROG_RESUME;
  277. if (flag) {
  278. enable_interrupts();
  279. }
  280. return( 0 );
  281. }
  282. static int
  283. poll_status( volatile FLASH_WORD_SIZE *addr )
  284. {
  285. unsigned long start;
  286. start = get_timer (0);
  287. /* wait for error or finish */
  288. while (1) {
  289. if (*addr == FLASH_STATUS_DONE) {
  290. if (*addr == FLASH_STATUS_DONE) {
  291. break;
  292. }
  293. }
  294. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  295. *addr = FLASH_CMD_RESET;
  296. return (-1);
  297. }
  298. }
  299. *addr = FLASH_CMD_RESET;
  300. return (0);
  301. }
  302. /*
  303. * write_buff return values:
  304. * 0 - OK
  305. * 1 - write timeout
  306. * 2 - Flash not erased
  307. * 4 - Flash not identified
  308. */
  309. int
  310. write_buff(flash_info_t *info, uchar *src, ulong udst, ulong cnt)
  311. {
  312. volatile FLASH_WORD_SIZE *addr, *dst;
  313. unsigned long bcnt;
  314. int flag, i;
  315. if (info->flash_id == FLASH_UNKNOWN) {
  316. return (4);
  317. }
  318. addr = (volatile FLASH_WORD_SIZE *)(info->start[0]);
  319. dst = (volatile FLASH_WORD_SIZE *) udst;
  320. #ifdef CFG_FLASH_16BIT
  321. #error NYI
  322. #else
  323. while (cnt > 0) {
  324. /* Check if buffer write is possible */
  325. if (cnt > 1 && (((unsigned long)dst & (FLASH_WRITE_BUFFER_SIZE - 1)) == 0)) {
  326. bcnt = cnt > FLASH_WRITE_BUFFER_SIZE ? FLASH_WRITE_BUFFER_SIZE : cnt;
  327. /* Check if Flash is (sufficiently) erased */
  328. for (i=0; i<bcnt; i++) {
  329. if ((dst[i] & src[i]) != src[i]) {
  330. return (2);
  331. }
  332. }
  333. if (write_buff2( dst,src,bcnt ) != 0) {
  334. addr[0] = FLASH_CMD_READ_STATUS;
  335. }
  336. if (poll_status( dst ) != 0) {
  337. return (1);
  338. }
  339. cnt -= bcnt;
  340. dst += bcnt;
  341. src += bcnt;
  342. continue;
  343. }
  344. /* Check if Flash is (sufficiently) erased */
  345. if ((*dst & *src) != *src) {
  346. return (2);
  347. }
  348. /* Disable interrupts which might cause a timeout here */
  349. flag = disable_interrupts();
  350. addr[0] = FLASH_CMD_ERASE_CONFIRM;
  351. addr[0] = FLASH_CMD_WRITE;
  352. *dst++ = *src++;
  353. /* re-enable interrupts if necessary */
  354. if (flag) {
  355. enable_interrupts();
  356. }
  357. if (poll_status( dst ) != 0) {
  358. return (1);
  359. }
  360. cnt --;
  361. }
  362. #endif
  363. return (0);
  364. }
  365. int
  366. flash_real_protect(flash_info_t *info, long sector, int prot)
  367. {
  368. volatile FLASH_WORD_SIZE *addr;
  369. unsigned long start;
  370. addr = (volatile FLASH_WORD_SIZE *)(info->start[sector]);
  371. *addr = FLASH_CMD_CLEAR_STATUS;
  372. *addr = FLASH_CMD_PROTECT;
  373. if(prot) {
  374. *addr = FLASH_CMD_PROTECT_SET;
  375. } else {
  376. *addr = FLASH_CMD_PROTECT_CLEAR;
  377. }
  378. /* wait for error or finish */
  379. start = get_timer (0);
  380. while(!(addr[0] & FLASH_STATUS_DONE)){
  381. if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
  382. printf("Flash protect timeout at address %lx\n", info->start[sector]);
  383. addr[0] = FLASH_CMD_RESET;
  384. return (1);
  385. }
  386. }
  387. /* Set software protect flag */
  388. info->protect[sector] = prot;
  389. *addr = FLASH_CMD_RESET;
  390. return (0);
  391. }