Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include "flash.h" |
| 23 | |
| 24 | int probe_winbond_fwhub(struct flashchip *flash) |
| 25 | { |
| 26 | volatile uint8_t *bios = flash->virtual_memory; |
| 27 | uint8_t vid, did; |
| 28 | |
| 29 | /* Product Identification Entry */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 30 | chip_writeb(0xAA, bios + 0x5555); |
| 31 | chip_writeb(0x55, bios + 0x2AAA); |
| 32 | chip_writeb(0x90, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 33 | myusec_delay(10); |
| 34 | |
| 35 | /* Read product ID */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 36 | vid = chip_readb(bios); |
| 37 | did = chip_readb(bios + 0x01); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 38 | |
| 39 | /* Product Identifixation Exit */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 40 | chip_writeb(0xAA, bios + 0x5555); |
| 41 | chip_writeb(0x55, bios + 0x2AAA); |
| 42 | chip_writeb(0xF0, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 43 | myusec_delay(10); |
| 44 | |
| 45 | printf_debug("%s: vid 0x%x, did 0x%x\n", __FUNCTION__, vid, did); |
| 46 | |
| 47 | if (vid != flash->manufacture_id || did != flash->model_id) |
| 48 | return 0; |
| 49 | |
| 50 | map_flash_registers(flash); |
| 51 | |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static int unlock_block_winbond_fwhub(struct flashchip *flash, int offset) |
| 56 | { |
| 57 | volatile uint8_t *wrprotect = flash->virtual_registers + offset + 2; |
| 58 | uint8_t locking; |
| 59 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 60 | printf_debug("Trying to unlock block @0x%08x = 0x%02x\n", offset, |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 61 | chip_readb(wrprotect)); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 62 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 63 | locking = chip_readb(wrprotect); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 64 | switch (locking & 0x7) { |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 65 | case 0: |
| 66 | printf_debug("Full Access.\n"); |
| 67 | return 0; |
| 68 | case 1: |
| 69 | printf_debug("Write Lock (Default State).\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 70 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | case 2: |
| 73 | printf_debug("Locked Open (Full Access, Lock Down).\n"); |
| 74 | return 0; |
| 75 | case 3: |
| 76 | fprintf(stderr, "Error: Write Lock, Locked Down.\n"); |
| 77 | return -1; |
| 78 | case 4: |
| 79 | printf_debug("Read Lock.\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 80 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 81 | return 0; |
| 82 | case 5: |
| 83 | printf_debug("Read/Write Lock.\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 84 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | case 6: |
| 87 | fprintf(stderr, "Error: Read Lock, Locked Down.\n"); |
| 88 | return -1; |
| 89 | case 7: |
| 90 | fprintf(stderr, "Error: Read/Write Lock, Locked Down.\n"); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | /* We will never reach this point, but GCC doesn't know */ |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | int unlock_winbond_fwhub(struct flashchip *flash) |
| 99 | { |
| 100 | int i, total_size = flash->total_size * 1024; |
| 101 | volatile uint8_t *bios = flash->virtual_memory; |
| 102 | uint8_t locking; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 103 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 104 | /* Are there any hardware restrictions that we can't overcome? |
| 105 | * If flashrom fail here, someone's got to check all those GPIOs. |
| 106 | */ |
| 107 | |
| 108 | /* Product Identification Entry */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 109 | chip_writeb(0xAA, bios + 0x5555); |
| 110 | chip_writeb(0x55, bios + 0x2AAA); |
| 111 | chip_writeb(0x90, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 112 | myusec_delay(10); |
| 113 | |
| 114 | /* Read Hardware Lock Bits */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 115 | locking = chip_readb(bios + 0xffff2); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 116 | |
| 117 | /* Product Identification Exit */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 118 | chip_writeb(0xAA, bios + 0x5555); |
| 119 | chip_writeb(0x55, bios + 0x2AAA); |
| 120 | chip_writeb(0xF0, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 121 | myusec_delay(10); |
| 122 | |
| 123 | printf_debug("Lockout bits:\n"); |
| 124 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 125 | if (locking & (1 << 2)) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 126 | fprintf(stderr, "Error: hardware bootblock locking (#TBL).\n"); |
| 127 | else |
| 128 | printf_debug("No hardware bootblock locking (good!)\n"); |
| 129 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 130 | if (locking & (1 << 3)) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 131 | fprintf(stderr, "Error: hardware block locking (#WP).\n"); |
| 132 | else |
| 133 | printf_debug("No hardware block locking (good!)\n"); |
| 134 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 135 | if (locking & ((1 << 2) | (1 << 3))) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 136 | return -1; |
| 137 | |
| 138 | /* Unlock the complete chip */ |
| 139 | for (i = 0; i < total_size; i += flash->page_size) |
| 140 | if (unlock_block_winbond_fwhub(flash, i)) |
| 141 | return -1; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 146 | static int erase_sector_winbond_fwhub(volatile uint8_t *bios, |
| 147 | unsigned int sector) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 148 | { |
| 149 | /* Remember: too much sleep can waste your day. */ |
| 150 | |
| 151 | printf("0x%08x\b\b\b\b\b\b\b\b\b\b", sector); |
| 152 | |
| 153 | /* Sector Erase */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 154 | chip_writeb(0xAA, bios + 0x5555); |
| 155 | chip_writeb(0x55, bios + 0x2AAA); |
| 156 | chip_writeb(0x80, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 157 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 158 | chip_writeb(0xAA, bios + 0x5555); |
| 159 | chip_writeb(0x55, bios + 0x2AAA); |
| 160 | chip_writeb(0x30, bios + sector); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 161 | |
| 162 | /* wait for Toggle bit ready */ |
| 163 | toggle_ready_jedec(bios); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int erase_winbond_fwhub(struct flashchip *flash) |
| 169 | { |
| 170 | int i, total_size = flash->total_size * 1024; |
| 171 | volatile uint8_t *bios = flash->virtual_memory; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 172 | |
Stefan Reinauer | 4c390c8 | 2008-07-02 13:33:09 +0000 | [diff] [blame] | 173 | unlock_winbond_fwhub(flash); |
| 174 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 175 | printf("Erasing: "); |
| 176 | |
| 177 | for (i = 0; i < total_size; i += flash->page_size) |
| 178 | erase_sector_winbond_fwhub(bios, i); |
| 179 | |
| 180 | printf("\n"); |
| 181 | |
| 182 | for (i = 0; i < total_size; i++) { |
Carl-Daniel Hailfinger | 01624f4 | 2009-05-12 15:38:55 +0000 | [diff] [blame] | 183 | if (chip_readb(bios + i) != 0xff) { |
| 184 | fprintf(stderr, "Error: Flash chip erase failed at 0x%08x(0x%02x)\n", i, chip_readb(bios + i)); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int write_winbond_fwhub(struct flashchip *flash, uint8_t *buf) |
| 193 | { |
| 194 | int i; |
| 195 | int total_size = flash->total_size * 1024; |
| 196 | volatile uint8_t *bios = flash->virtual_memory; |
| 197 | |
| 198 | if (erase_winbond_fwhub(flash)) |
| 199 | return -1; |
| 200 | |
| 201 | printf("Programming: "); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 202 | for (i = 0; i < total_size; i += flash->page_size) { |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 203 | printf("0x%08x\b\b\b\b\b\b\b\b\b\b", i); |
| 204 | write_sector_jedec(bios, buf + i, bios + i, flash->page_size); |
| 205 | } |
| 206 | printf("\n"); |
| 207 | |
| 208 | return 0; |
| 209 | } |