flash.c 7.1 KB

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