flash.c 7.9 KB

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