flash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * (C) Copyright 2000
  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. #include <nios.h>
  25. /*---------------------------------------------------------------------*/
  26. #define BANKSZ (8 * 1024 * 1024)
  27. #define SECTSZ (64 * 1024)
  28. #define USERFLASH (2 * 1024 * 1024) /* bottom 2 MB for user */
  29. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  30. #define FLASH_WORD_SIZE unsigned char
  31. /*---------------------------------------------------------------------*/
  32. unsigned long flash_init (void)
  33. {
  34. int i;
  35. unsigned long addr;
  36. flash_info_t *fli = &flash_info[0];
  37. fli->size = BANKSZ;
  38. fli->sector_count = CFG_MAX_FLASH_SECT;
  39. fli->flash_id = FLASH_MAN_AMD;
  40. addr = CFG_FLASH_BASE;
  41. for (i = 0; i < fli->sector_count; ++i) {
  42. fli->start[i] = addr;
  43. addr += SECTSZ;
  44. /* Protect all but 2 MByte user area */
  45. if (addr < (CFG_FLASH_BASE + USERFLASH))
  46. fli->protect[i] = 0;
  47. else
  48. fli->protect[i] = 1;
  49. }
  50. return (BANKSZ);
  51. }
  52. /*--------------------------------------------------------------------*/
  53. void flash_print_info (flash_info_t * info)
  54. {
  55. int i, k;
  56. unsigned long size;
  57. int erased;
  58. volatile unsigned char *flash;
  59. printf (" Size: %ld KB in %d Sectors\n",
  60. info->size >> 10, info->sector_count);
  61. printf (" Sector Start Addresses:");
  62. for (i = 0; i < info->sector_count; ++i) {
  63. /* Check if whole sector is erased */
  64. if (i != (info->sector_count - 1))
  65. size = info->start[i + 1] - info->start[i];
  66. else
  67. size = info->start[0] + info->size - info->start[i];
  68. erased = 1;
  69. flash = (volatile unsigned char *) info->start[i];
  70. for (k = 0; k < size; k++) {
  71. if (*flash++ != 0xff) {
  72. erased = 0;
  73. break;
  74. }
  75. }
  76. /* Print the info */
  77. if ((i % 5) == 0)
  78. printf ("\n ");
  79. printf (" %08lX%s%s", info->start[i], erased ? " E" : " ",
  80. info->protect[i] ? "RO " : " ");
  81. }
  82. printf ("\n");
  83. }
  84. /*-------------------------------------------------------------------*/
  85. int flash_erase (flash_info_t * info, int s_first, int s_last)
  86. {
  87. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
  88. volatile FLASH_WORD_SIZE *addr2;
  89. int prot, sect;
  90. int any = 0;
  91. unsigned oldpri;
  92. ulong start;
  93. /* Some sanity checking */
  94. if ((s_first < 0) || (s_first > s_last)) {
  95. printf ("- no sectors to erase\n");
  96. return 1;
  97. }
  98. prot = 0;
  99. for (sect = s_first; sect <= s_last; ++sect) {
  100. if (info->protect[sect]) {
  101. prot++;
  102. }
  103. }
  104. if (prot) {
  105. printf ("- Warning: %d protected sectors will not be erased!\n",
  106. prot);
  107. } else {
  108. printf ("\n");
  109. }
  110. /* NOTE: disabling interrupts on Nios can be very bad since it
  111. * also disables the LO_LIMIT exception. It's better here to
  112. * set the interrupt priority to 3 & restore it when we're done.
  113. */
  114. oldpri = ipri (3);
  115. /* It's ok to erase multiple sectors provided we don't delay more
  116. * than 50 usec between cmds ... at which point the erase time-out
  117. * occurs. So don't go and put printf() calls in the loop ... it
  118. * won't be very helpful ;-)
  119. */
  120. for (sect = s_first; sect <= s_last; sect++) {
  121. if (info->protect[sect] == 0) { /* not protected */
  122. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  123. *addr = 0xaa;
  124. *addr = 0x55;
  125. *addr = 0x80;
  126. *addr = 0xaa;
  127. *addr = 0x55;
  128. *addr2 = 0x30;
  129. any = 1;
  130. }
  131. }
  132. /* Now just wait for 0xff & provide some user feedback while
  133. * we wait.
  134. */
  135. if (any) {
  136. addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
  137. start = get_timer (0);
  138. while (*addr2 != 0xff) {
  139. udelay (1000 * 1000);
  140. putc ('.');
  141. if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
  142. printf ("timeout\n");
  143. return 1;
  144. }
  145. }
  146. printf ("\n");
  147. }
  148. /* Restore interrupt priority */
  149. ipri (oldpri);
  150. return 0;
  151. }
  152. /*-----------------------------------------------------------------------
  153. * Copy memory to flash, returns:
  154. * 0 - OK
  155. * 1 - write timeout
  156. * 2 - Flash not erased
  157. */
  158. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  159. {
  160. vu_char *cmd = (vu_char *) info->start[0];
  161. vu_char *dst = (vu_char *) addr;
  162. unsigned char b;
  163. unsigned oldpri;
  164. ulong start;
  165. while (cnt) {
  166. /* Check for sufficient erase */
  167. b = *src;
  168. if ((*dst & b) != b) {
  169. printf ("%02x : %02x\n", *dst, b);
  170. return (2);
  171. }
  172. /* Disable interrupts other than window underflow
  173. * (interrupt priority 2)
  174. */
  175. oldpri = ipri (3);
  176. *cmd = 0xaa;
  177. *cmd = 0x55;
  178. *cmd = 0xa0;
  179. *dst = b;
  180. /* Verify write */
  181. start = get_timer (0);
  182. while (*dst != b) {
  183. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  184. ipri (oldpri);
  185. return 1;
  186. }
  187. }
  188. dst++;
  189. src++;
  190. cnt--;
  191. ipri (oldpri);
  192. }
  193. return (0);
  194. }