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