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