flash.c 4.5 KB

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