Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <common.h> |
| 10 | #if defined(CONFIG_NIOS) |
| 11 | #include <nios.h> |
| 12 | #else |
| 13 | #include <asm/io.h> |
| 14 | #endif |
| 15 | |
| 16 | #define SECTSZ (64 * 1024) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 17 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 18 | |
| 19 | /*----------------------------------------------------------------------*/ |
| 20 | unsigned long flash_init (void) |
| 21 | { |
| 22 | int i; |
| 23 | unsigned long addr; |
| 24 | flash_info_t *fli = &flash_info[0]; |
| 25 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | fli->size = CONFIG_SYS_FLASH_SIZE; |
| 27 | fli->sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 28 | fli->flash_id = FLASH_MAN_AMD + FLASH_AMDLV065D; |
| 29 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 30 | addr = CONFIG_SYS_FLASH_BASE; |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 31 | for (i = 0; i < fli->sector_count; ++i) { |
| 32 | fli->start[i] = addr; |
| 33 | addr += SECTSZ; |
| 34 | fli->protect[i] = 1; |
| 35 | } |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | return (CONFIG_SYS_FLASH_SIZE); |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 38 | } |
| 39 | /*--------------------------------------------------------------------*/ |
| 40 | void flash_print_info (flash_info_t * info) |
| 41 | { |
| 42 | int i, k; |
| 43 | int erased; |
| 44 | unsigned long *addr; |
| 45 | |
| 46 | printf (" Size: %ld KB in %d Sectors\n", |
| 47 | info->size >> 10, info->sector_count); |
| 48 | printf (" Sector Start Addresses:"); |
| 49 | for (i = 0; i < info->sector_count; ++i) { |
| 50 | |
| 51 | /* Check if whole sector is erased */ |
| 52 | erased = 1; |
| 53 | addr = (unsigned long *) info->start[i]; |
| 54 | for (k = 0; k < SECTSZ/sizeof(unsigned long); k++) { |
| 55 | if ( readl(addr++) != (unsigned long)-1) { |
| 56 | erased = 0; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* Print the info */ |
| 62 | if ((i % 5) == 0) |
| 63 | printf ("\n "); |
| 64 | printf (" %08lX%s%s", |
| 65 | info->start[i], |
| 66 | erased ? " E" : " ", |
| 67 | info->protect[i] ? "RO " : " "); |
| 68 | } |
| 69 | printf ("\n"); |
| 70 | } |
| 71 | |
| 72 | /*-------------------------------------------------------------------*/ |
| 73 | |
| 74 | |
| 75 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
| 76 | { |
| 77 | unsigned char *addr = (unsigned char *) info->start[0]; |
| 78 | unsigned char *addr2; |
| 79 | int prot, sect; |
| 80 | ulong start; |
| 81 | |
| 82 | /* Some sanity checking */ |
| 83 | if ((s_first < 0) || (s_first > s_last)) { |
| 84 | printf ("- no sectors to erase\n"); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | prot = 0; |
| 89 | for (sect = s_first; sect <= s_last; ++sect) { |
| 90 | if (info->protect[sect]) { |
| 91 | prot++; |
| 92 | } |
| 93 | } |
| 94 | if (prot) { |
| 95 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 96 | prot); |
| 97 | } else { |
| 98 | printf ("\n"); |
| 99 | } |
| 100 | |
| 101 | /* It's ok to erase multiple sectors provided we don't delay more |
| 102 | * than 50 usec between cmds ... at which point the erase time-out |
| 103 | * occurs. So don't go and put printf() calls in the loop ... it |
| 104 | * won't be very helpful ;-) |
| 105 | */ |
| 106 | for (sect = s_first; sect <= s_last; sect++) { |
| 107 | if (info->protect[sect] == 0) { /* not protected */ |
| 108 | addr2 = (unsigned char *) info->start[sect]; |
Scott McNutt | 3ea0037 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 109 | writeb (0xaa, addr); |
| 110 | writeb (0x55, addr); |
| 111 | writeb (0x80, addr); |
| 112 | writeb (0xaa, addr); |
| 113 | writeb (0x55, addr); |
| 114 | writeb (0x30, addr2); |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 115 | /* Now just wait for 0xff & provide some user |
| 116 | * feedback while we wait. |
| 117 | */ |
| 118 | start = get_timer (0); |
| 119 | while ( readb (addr2) != 0xff) { |
| 120 | udelay (1000 * 1000); |
| 121 | putc ('.'); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 122 | if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 123 | printf ("timeout\n"); |
| 124 | return 1; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | printf ("\n"); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /*----------------------------------------------------------------------- |
| 134 | * Copy memory to flash, returns: |
| 135 | * 0 - OK |
| 136 | * 1 - write timeout |
| 137 | * 2 - Flash not erased |
| 138 | */ |
| 139 | |
| 140 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
| 141 | { |
| 142 | |
| 143 | vu_char *cmd = (vu_char *) info->start[0]; |
| 144 | vu_char *dst = (vu_char *) addr; |
| 145 | unsigned char b; |
| 146 | ulong start; |
| 147 | |
| 148 | while (cnt) { |
| 149 | /* Check for sufficient erase */ |
| 150 | b = *src; |
| 151 | if ((readb (dst) & b) != b) { |
| 152 | printf ("%02x : %02x\n", readb (dst), b); |
| 153 | return (2); |
| 154 | } |
| 155 | |
Scott McNutt | 3ea0037 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 156 | writeb (0xaa, cmd); |
| 157 | writeb (0x55, cmd); |
| 158 | writeb (0xa0, cmd); |
Scott McNutt | 9e486ab | 2010-03-30 20:26:15 -0400 | [diff] [blame] | 159 | writeb (b, dst); |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 160 | |
| 161 | /* Verify write */ |
| 162 | start = get_timer (0); |
| 163 | while (readb (dst) != b) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 164 | if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
Scott McNutt | 9cc8337 | 2006-06-08 13:37:39 -0400 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | dst++; |
| 169 | src++; |
| 170 | cnt--; |
| 171 | } |
| 172 | |
| 173 | return (0); |
| 174 | } |