flash.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * (C) Copyright 2005
  3. * BuS Elektronik GmbH & Co.KG <esw@bus-elektonik.de>
  4. *
  5. * Based On
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include "cfm_flash.h"
  28. #define PHYS_FLASH_1 CONFIG_SYS_FLASH_BASE
  29. #define FLASH_BANK_SIZE 0x200000
  30. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  31. void flash_print_info (flash_info_t * info)
  32. {
  33. int i;
  34. switch (info->flash_id & FLASH_VENDMASK) {
  35. case (AMD_MANUFACT & FLASH_VENDMASK):
  36. printf ("AMD: ");
  37. switch (info->flash_id & FLASH_TYPEMASK) {
  38. case (AMD_ID_LV160B & FLASH_TYPEMASK):
  39. printf ("AM29LV160B (16Bit)\n");
  40. break;
  41. default:
  42. printf ("Unknown Chip Type\n");
  43. break;
  44. }
  45. break;
  46. case FREESCALE_MANUFACT & FLASH_VENDMASK:
  47. cfm_flash_print_info (info);
  48. break;
  49. default:
  50. printf ("Unknown Vendor ");
  51. break;
  52. }
  53. puts (" Size: ");
  54. if ((info->size >> 20) > 0)
  55. {
  56. printf ("%ld MiB",info->size >> 20);
  57. }
  58. else
  59. {
  60. printf ("%ld KiB",info->size >> 10);
  61. }
  62. printf (" in %d Sectors\n", info->sector_count);
  63. printf (" Sector Start Addresses:");
  64. for (i = 0; i < info->sector_count; i++) {
  65. if ((i % 4) == 0) {
  66. printf ("\n ");
  67. }
  68. printf ("%02d: %08lX%s ", i,info->start[i],
  69. info->protect[i] ? " P" : " ");
  70. }
  71. printf ("\n\n");
  72. }
  73. unsigned long flash_init (void)
  74. {
  75. int i, j;
  76. ulong size = 0;
  77. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  78. ulong flashbase = 0;
  79. switch (i)
  80. {
  81. case 1:
  82. flash_info[i].flash_id =
  83. (AMD_MANUFACT & FLASH_VENDMASK) |
  84. (AMD_ID_LV160B & FLASH_TYPEMASK);
  85. flash_info[i].size = FLASH_BANK_SIZE;
  86. flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  87. memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  88. flashbase = PHYS_FLASH_1;
  89. for (j = 0; j < flash_info[i].sector_count; j++) {
  90. if (j == 0) {
  91. /* 1st is 16 KiB */
  92. flash_info[i].start[j] = flashbase;
  93. }
  94. if ((j >= 1) && (j <= 2)) {
  95. /* 2nd and 3rd are 8 KiB */
  96. flash_info[i].start[j] =
  97. flashbase + 0x4000 + 0x2000 * (j - 1);
  98. }
  99. if (j == 3) {
  100. /* 4th is 32 KiB */
  101. flash_info[i].start[j] = flashbase + 0x8000;
  102. }
  103. if ((j >= 4) && (j <= 34)) {
  104. /* rest is 256 KiB */
  105. flash_info[i].start[j] =
  106. flashbase + 0x10000 + 0x10000 * (j - 4);
  107. }
  108. }
  109. break;
  110. case 0:
  111. cfm_flash_init (&flash_info[i]);
  112. break;
  113. default:
  114. panic ("configured to many flash banks!\n");
  115. }
  116. size += flash_info[i].size;
  117. }
  118. flash_protect (FLAG_PROTECT_SET,
  119. CONFIG_SYS_FLASH_BASE,
  120. CONFIG_SYS_FLASH_BASE + 0xffff, &flash_info[0]);
  121. return size;
  122. }
  123. #define CMD_READ_ARRAY 0x00F0
  124. #define CMD_UNLOCK1 0x00AA
  125. #define CMD_UNLOCK2 0x0055
  126. #define CMD_ERASE_SETUP 0x0080
  127. #define CMD_ERASE_CONFIRM 0x0030
  128. #define CMD_PROGRAM 0x00A0
  129. #define CMD_UNLOCK_BYPASS 0x0020
  130. #define MEM_FLASH_ADDR1 (*(volatile u16 *)(info->start[0] + (0x00000555<<1)))
  131. #define MEM_FLASH_ADDR2 (*(volatile u16 *)(info->start[0] + (0x000002AA<<1)))
  132. #define BIT_ERASE_DONE 0x0080
  133. #define BIT_RDY_MASK 0x0080
  134. #define BIT_PROGRAM_ERROR 0x0020
  135. #define BIT_TIMEOUT 0x80000000 /* our flag */
  136. #define ERR_READY -1
  137. int amd_flash_erase_sector(flash_info_t * info, int sector)
  138. {
  139. int state;
  140. ulong result;
  141. ulong start;
  142. volatile u16 *addr =
  143. (volatile u16 *) (info->start[sector]);
  144. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  145. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  146. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  147. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  148. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  149. *addr = CMD_ERASE_CONFIRM;
  150. /* wait until flash is ready */
  151. state = 0;
  152. start = get_timer(0);
  153. do {
  154. result = *addr;
  155. /* check timeout */
  156. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  157. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  158. state = ERR_TIMOUT;
  159. }
  160. if (!state && (result & 0xFFFF) & BIT_ERASE_DONE)
  161. state = ERR_READY;
  162. }
  163. while (!state);
  164. if (state == ERR_READY)
  165. state = ERR_OK;
  166. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  167. return state;
  168. }
  169. int flash_erase (flash_info_t * info, int s_first, int s_last)
  170. {
  171. int iflag, cflag;
  172. int sector;
  173. int rc;
  174. rc = ERR_OK;
  175. if (info->flash_id == FLASH_UNKNOWN)
  176. {
  177. rc = ERR_UNKNOWN_FLASH_TYPE;
  178. } /* (info->flash_id == FLASH_UNKNOWN) */
  179. if ((s_first < 0) || (s_first > s_last) || s_last >= info->sector_count)
  180. {
  181. rc = ERR_INVAL;
  182. }
  183. cflag = icache_status ();
  184. icache_disable ();
  185. iflag = disable_interrupts ();
  186. for (sector = s_first; (sector <= s_last) && (rc == ERR_OK); sector++) {
  187. if (info->protect[sector])
  188. {
  189. putc('P'); /* protected sector will not erase */
  190. }
  191. else
  192. {
  193. /* erase on unprotected sector */
  194. puts("E\b");
  195. switch (info->flash_id & FLASH_VENDMASK)
  196. {
  197. case (AMD_MANUFACT & FLASH_VENDMASK):
  198. rc = amd_flash_erase_sector(info,sector);
  199. break;
  200. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  201. rc = cfm_flash_erase_sector(info,sector);
  202. break;
  203. default:
  204. return ERR_UNKNOWN_FLASH_VENDOR;
  205. }
  206. putc('.');
  207. }
  208. }
  209. if (rc!=ERR_OK)
  210. {
  211. printf ("\n ");
  212. flash_perror (rc);
  213. }
  214. else
  215. {
  216. printf (" done\n");
  217. }
  218. udelay (10000); /* allow flash to settle - wait 10 ms */
  219. if (iflag)
  220. enable_interrupts ();
  221. if (cflag)
  222. icache_enable ();
  223. return rc;
  224. }
  225. volatile static int amd_write_word (flash_info_t * info, ulong dest, u16 data)
  226. {
  227. volatile u16 *addr;
  228. ulong result;
  229. int cflag, iflag;
  230. int state;
  231. ulong start;
  232. /*
  233. * Check if Flash is (sufficiently) erased
  234. */
  235. addr = (volatile u16 *) dest;
  236. result = *addr;
  237. if ((result & data) != data)
  238. return ERR_NOT_ERASED;
  239. /*
  240. * Disable interrupts which might cause a timeout
  241. * here. Remember that our exception vectors are
  242. * at address 0 in the flash, and we don't want a
  243. * (ticker) exception to happen while the flash
  244. * chip is in programming mode.
  245. */
  246. cflag = icache_status ();
  247. icache_disable ();
  248. iflag = disable_interrupts ();
  249. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  250. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  251. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  252. *addr = data;
  253. /* arm simple, non interrupt dependent timer */
  254. start = get_timer(0);
  255. /* wait until flash is ready */
  256. state = 0;
  257. do {
  258. result = *addr;
  259. /* check timeout */
  260. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  261. state = ERR_TIMOUT;
  262. }
  263. if (!state && ((result & BIT_RDY_MASK) == (data & BIT_RDY_MASK)))
  264. state = ERR_READY;
  265. } while (!state);
  266. *addr = CMD_READ_ARRAY;
  267. if (state == ERR_READY)
  268. state = ERR_OK;
  269. if ((*addr != data) && (state != ERR_TIMOUT))
  270. state = ERR_PROG_ERROR;
  271. if (iflag)
  272. enable_interrupts ();
  273. if (cflag)
  274. icache_enable ();
  275. return state;
  276. }
  277. int amd_flash_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  278. {
  279. int rc;
  280. ulong dest;
  281. u16 data;
  282. rc = ERR_OK;
  283. if (addr & 1)
  284. {
  285. debug ("Byte alignment not supported\n");
  286. rc = ERR_ALIGN;
  287. }
  288. if (cnt & 1)
  289. {
  290. debug ("Byte transfer not supported\n");
  291. rc = ERR_ALIGN;
  292. }
  293. dest = addr;
  294. while ((cnt>=2) && (rc == ERR_OK))
  295. {
  296. data = *((volatile u16 *) src);
  297. rc=amd_write_word (info,dest,data);
  298. src +=2;
  299. dest +=2;
  300. cnt -=2;
  301. }
  302. return rc;
  303. }
  304. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  305. {
  306. int rc;
  307. switch (info->flash_id & FLASH_VENDMASK)
  308. {
  309. case (AMD_MANUFACT & FLASH_VENDMASK):
  310. rc = amd_flash_write_buff(info,src,addr,cnt);
  311. break;
  312. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  313. rc = cfm_flash_write_buff(info,src,addr,cnt);
  314. break;
  315. default:
  316. rc = ERR_UNKNOWN_FLASH_VENDOR;
  317. }
  318. return rc;
  319. }
  320. int amd_flash_protect(flash_info_t * info,long sector,int prot)
  321. {
  322. int rc;
  323. rc= ERR_OK;
  324. if (prot)
  325. {
  326. info->protect[sector]=1;
  327. }
  328. else
  329. {
  330. info->protect[sector]=0;
  331. }
  332. return rc;
  333. }
  334. #ifdef CONFIG_SYS_FLASH_PROTECTION
  335. int flash_real_protect(flash_info_t * info,long sector,int prot)
  336. {
  337. int rc;
  338. switch (info->flash_id & FLASH_VENDMASK)
  339. {
  340. case (AMD_MANUFACT & FLASH_VENDMASK):
  341. rc = amd_flash_protect(info,sector,prot);
  342. break;
  343. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  344. rc = cfm_flash_protect(info,sector,prot);
  345. break;
  346. default:
  347. rc = ERR_UNKNOWN_FLASH_VENDOR;
  348. }
  349. return rc;
  350. }
  351. #endif