flash.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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[CONFIG_SYS_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 CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  62. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  63. int prot, sect;
  64. unsigned oldpri;
  65. ulong start;
  66. /* Some sanity checking */
  67. if ((s_first < 0) || (s_first > s_last)) {
  68. printf ("- no sectors to erase\n");
  69. return 1;
  70. }
  71. prot = 0;
  72. for (sect = s_first; sect <= s_last; ++sect) {
  73. if (info->protect[sect]) {
  74. prot++;
  75. }
  76. }
  77. if (prot) {
  78. printf ("- Warning: %d protected sectors will not be erased!\n",
  79. prot);
  80. } else {
  81. printf ("\n");
  82. }
  83. #ifdef DEBUG
  84. for (sect = s_first; sect <= s_last; sect++) {
  85. printf("- Erase: Sect: %i @ 0x%08x\n", sect, info->start[sect]);
  86. }
  87. #endif
  88. /* NOTE: disabling interrupts on Nios can be very bad since it
  89. * also disables the LO_LIMIT exception. It's better here to
  90. * set the interrupt priority to 3 & restore it when we're done.
  91. */
  92. oldpri = ipri (3);
  93. /* It's ok to erase multiple sectors provided we don't delay more
  94. * than 50 usec between cmds ... at which point the erase time-out
  95. * occurs. So don't go and put printf() calls in the loop ... it
  96. * won't be very helpful ;-)
  97. */
  98. for (sect = s_first; sect <= s_last; sect++) {
  99. if (info->protect[sect] == 0) { /* not protected */
  100. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  101. *addr = 0xaa;
  102. *addr = 0x55;
  103. *addr = 0x80;
  104. *addr = 0xaa;
  105. *addr = 0x55;
  106. *addr2 = 0x30;
  107. /* Now just wait for 0xff & provide some user
  108. * feedback while we wait. Here we have to grant
  109. * timer interrupts. Otherwise get_timer() can't
  110. * work right. */
  111. ipri(oldpri);
  112. start = get_timer (0);
  113. while (*addr2 != 0xff) {
  114. udelay (1000 * 1000);
  115. putc ('.');
  116. if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  117. printf ("timeout\n");
  118. return 1;
  119. }
  120. }
  121. oldpri = ipri (3); /* disallow non important irqs again */
  122. }
  123. }
  124. printf ("\n");
  125. /* Restore interrupt priority */
  126. ipri (oldpri);
  127. return 0;
  128. }
  129. /*-----------------------------------------------------------------------
  130. * Copy memory to flash, returns:
  131. * 0 - OK
  132. * 1 - write timeout
  133. * 2 - Flash not erased
  134. */
  135. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  136. {
  137. vu_char *cmd = (vu_char *) info->start[0];
  138. vu_char *dst = (vu_char *) addr;
  139. unsigned char b;
  140. unsigned oldpri;
  141. ulong start;
  142. while (cnt) {
  143. /* Check for sufficient erase */
  144. b = *src;
  145. if ((*dst & b) != b) {
  146. printf ("%02x : %02x\n", *dst, b);
  147. return (2);
  148. }
  149. /* Disable interrupts other than window underflow
  150. * (interrupt priority 2)
  151. */
  152. oldpri = ipri (3);
  153. *cmd = 0xaa;
  154. *cmd = 0x55;
  155. *cmd = 0xa0;
  156. *dst = b;
  157. /* Verify write */
  158. start = get_timer (0);
  159. while (*dst != b) {
  160. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  161. ipri (oldpri);
  162. return 1;
  163. }
  164. }
  165. dst++;
  166. src++;
  167. cnt--;
  168. ipri (oldpri);
  169. }
  170. return (0);
  171. }