flash.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. volatile u16 *addr =
  142. (volatile u16 *) (info->start[sector]);
  143. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  144. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  145. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  146. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  147. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  148. *addr = CMD_ERASE_CONFIRM;
  149. /* wait until flash is ready */
  150. state = 0;
  151. set_timer (0);
  152. do {
  153. result = *addr;
  154. /* check timeout */
  155. if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  156. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  157. state = ERR_TIMOUT;
  158. }
  159. if (!state && (result & 0xFFFF) & BIT_ERASE_DONE)
  160. state = ERR_READY;
  161. }
  162. while (!state);
  163. if (state == ERR_READY)
  164. state = ERR_OK;
  165. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  166. return state;
  167. }
  168. int flash_erase (flash_info_t * info, int s_first, int s_last)
  169. {
  170. int iflag, cflag;
  171. int sector;
  172. int rc;
  173. rc = ERR_OK;
  174. if (info->flash_id == FLASH_UNKNOWN)
  175. {
  176. rc = ERR_UNKNOWN_FLASH_TYPE;
  177. } /* (info->flash_id == FLASH_UNKNOWN) */
  178. if ((s_first < 0) || (s_first > s_last) || s_last >= info->sector_count)
  179. {
  180. rc = ERR_INVAL;
  181. }
  182. cflag = icache_status ();
  183. icache_disable ();
  184. iflag = disable_interrupts ();
  185. for (sector = s_first; (sector <= s_last) && (rc == ERR_OK); sector++) {
  186. if (info->protect[sector])
  187. {
  188. putc('P'); /* protected sector will not erase */
  189. }
  190. else
  191. {
  192. /* erase on unprotected sector */
  193. puts("E\b");
  194. switch (info->flash_id & FLASH_VENDMASK)
  195. {
  196. case (AMD_MANUFACT & FLASH_VENDMASK):
  197. rc = amd_flash_erase_sector(info,sector);
  198. break;
  199. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  200. rc = cfm_flash_erase_sector(info,sector);
  201. break;
  202. default:
  203. return ERR_UNKNOWN_FLASH_VENDOR;
  204. }
  205. putc('.');
  206. }
  207. }
  208. if (rc!=ERR_OK)
  209. {
  210. printf ("\n ");
  211. flash_perror (rc);
  212. }
  213. else
  214. {
  215. printf (" done\n");
  216. }
  217. udelay (10000); /* allow flash to settle - wait 10 ms */
  218. if (iflag)
  219. enable_interrupts ();
  220. if (cflag)
  221. icache_enable ();
  222. return rc;
  223. }
  224. volatile static int amd_write_word (flash_info_t * info, ulong dest, u16 data)
  225. {
  226. volatile u16 *addr;
  227. ulong result;
  228. int cflag, iflag;
  229. int state;
  230. /*
  231. * Check if Flash is (sufficiently) erased
  232. */
  233. addr = (volatile u16 *) dest;
  234. result = *addr;
  235. if ((result & data) != data)
  236. return ERR_NOT_ERASED;
  237. /*
  238. * Disable interrupts which might cause a timeout
  239. * here. Remember that our exception vectors are
  240. * at address 0 in the flash, and we don't want a
  241. * (ticker) exception to happen while the flash
  242. * chip is in programming mode.
  243. */
  244. cflag = icache_status ();
  245. icache_disable ();
  246. iflag = disable_interrupts ();
  247. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  248. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  249. MEM_FLASH_ADDR1 = CMD_PROGRAM;
  250. *addr = data;
  251. /* arm simple, non interrupt dependent timer */
  252. set_timer (0);
  253. /* wait until flash is ready */
  254. state = 0;
  255. do {
  256. result = *addr;
  257. /* check timeout */
  258. if (get_timer (0) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  259. state = ERR_TIMOUT;
  260. }
  261. if (!state && ((result & BIT_RDY_MASK) == (data & BIT_RDY_MASK)))
  262. state = ERR_READY;
  263. } while (!state);
  264. *addr = CMD_READ_ARRAY;
  265. if (state == ERR_READY)
  266. state = ERR_OK;
  267. if ((*addr != data) && (state != ERR_TIMOUT))
  268. state = ERR_PROG_ERROR;
  269. if (iflag)
  270. enable_interrupts ();
  271. if (cflag)
  272. icache_enable ();
  273. return state;
  274. }
  275. int amd_flash_write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  276. {
  277. int rc;
  278. ulong dest;
  279. u16 data;
  280. rc = ERR_OK;
  281. if (addr & 1)
  282. {
  283. debug ("Byte alignment not supported\n");
  284. rc = ERR_ALIGN;
  285. }
  286. if (cnt & 1)
  287. {
  288. debug ("Byte transfer not supported\n");
  289. rc = ERR_ALIGN;
  290. }
  291. dest = addr;
  292. while ((cnt>=2) && (rc == ERR_OK))
  293. {
  294. data = *((volatile u16 *) src);
  295. rc=amd_write_word (info,dest,data);
  296. src +=2;
  297. dest +=2;
  298. cnt -=2;
  299. }
  300. return rc;
  301. }
  302. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  303. {
  304. int rc;
  305. switch (info->flash_id & FLASH_VENDMASK)
  306. {
  307. case (AMD_MANUFACT & FLASH_VENDMASK):
  308. rc = amd_flash_write_buff(info,src,addr,cnt);
  309. break;
  310. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  311. rc = cfm_flash_write_buff(info,src,addr,cnt);
  312. break;
  313. default:
  314. rc = ERR_UNKNOWN_FLASH_VENDOR;
  315. }
  316. return rc;
  317. }
  318. int amd_flash_protect(flash_info_t * info,long sector,int prot)
  319. {
  320. int rc;
  321. rc= ERR_OK;
  322. if (prot)
  323. {
  324. info->protect[sector]=1;
  325. }
  326. else
  327. {
  328. info->protect[sector]=0;
  329. }
  330. return rc;
  331. }
  332. #ifdef CONFIG_SYS_FLASH_PROTECTION
  333. int flash_real_protect(flash_info_t * info,long sector,int prot)
  334. {
  335. int rc;
  336. switch (info->flash_id & FLASH_VENDMASK)
  337. {
  338. case (AMD_MANUFACT & FLASH_VENDMASK):
  339. rc = amd_flash_protect(info,sector,prot);
  340. break;
  341. case (FREESCALE_MANUFACT & FLASH_VENDMASK):
  342. rc = cfm_flash_protect(info,sector,prot);
  343. break;
  344. default:
  345. rc = ERR_UNKNOWN_FLASH_VENDOR;
  346. }
  347. return rc;
  348. }
  349. #endif