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 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 21 | #include "flash.h" |
| 22 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 23 | static int unlock_block_winbond_fwhub(struct flashchip *flash, int offset) |
| 24 | { |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 25 | chipaddr wrprotect = flash->virtual_registers + offset + 2; |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 26 | uint8_t locking; |
| 27 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 28 | 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] | 29 | chip_readb(wrprotect)); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 30 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 31 | locking = chip_readb(wrprotect); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 32 | switch (locking & 0x7) { |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 33 | case 0: |
| 34 | printf_debug("Full Access.\n"); |
| 35 | return 0; |
| 36 | case 1: |
| 37 | printf_debug("Write Lock (Default State).\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 38 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 39 | return 0; |
| 40 | case 2: |
| 41 | printf_debug("Locked Open (Full Access, Lock Down).\n"); |
| 42 | return 0; |
| 43 | case 3: |
| 44 | fprintf(stderr, "Error: Write Lock, Locked Down.\n"); |
| 45 | return -1; |
| 46 | case 4: |
| 47 | printf_debug("Read Lock.\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 48 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | case 5: |
| 51 | printf_debug("Read/Write Lock.\n"); |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 52 | chip_writeb(0, wrprotect); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | case 6: |
| 55 | fprintf(stderr, "Error: Read Lock, Locked Down.\n"); |
| 56 | return -1; |
| 57 | case 7: |
| 58 | fprintf(stderr, "Error: Read/Write Lock, Locked Down.\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | /* We will never reach this point, but GCC doesn't know */ |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | int unlock_winbond_fwhub(struct flashchip *flash) |
| 67 | { |
| 68 | int i, total_size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 69 | chipaddr bios = flash->virtual_memory; |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 70 | uint8_t locking; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 71 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 72 | /* Are there any hardware restrictions that we can't overcome? |
| 73 | * If flashrom fail here, someone's got to check all those GPIOs. |
| 74 | */ |
| 75 | |
| 76 | /* Product Identification Entry */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 77 | chip_writeb(0xAA, bios + 0x5555); |
| 78 | chip_writeb(0x55, bios + 0x2AAA); |
| 79 | chip_writeb(0x90, bios + 0x5555); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 80 | programmer_delay(10); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 81 | |
| 82 | /* Read Hardware Lock Bits */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 83 | locking = chip_readb(bios + 0xffff2); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 84 | |
| 85 | /* Product Identification Exit */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 86 | chip_writeb(0xAA, bios + 0x5555); |
| 87 | chip_writeb(0x55, bios + 0x2AAA); |
| 88 | chip_writeb(0xF0, bios + 0x5555); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 89 | programmer_delay(10); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 90 | |
| 91 | printf_debug("Lockout bits:\n"); |
| 92 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 93 | if (locking & (1 << 2)) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 94 | fprintf(stderr, "Error: hardware bootblock locking (#TBL).\n"); |
| 95 | else |
| 96 | printf_debug("No hardware bootblock locking (good!)\n"); |
| 97 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 98 | if (locking & (1 << 3)) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 99 | fprintf(stderr, "Error: hardware block locking (#WP).\n"); |
| 100 | else |
| 101 | printf_debug("No hardware block locking (good!)\n"); |
| 102 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 103 | if (locking & ((1 << 2) | (1 << 3))) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 104 | return -1; |
| 105 | |
| 106 | /* Unlock the complete chip */ |
| 107 | for (i = 0; i < total_size; i += flash->page_size) |
| 108 | if (unlock_block_winbond_fwhub(flash, i)) |
| 109 | return -1; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 114 | static int erase_sector_winbond_fwhub(struct flashchip *flash, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 115 | unsigned int sector) |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 116 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 117 | chipaddr bios = flash->virtual_memory; |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 118 | /* Remember: too much sleep can waste your day. */ |
| 119 | |
| 120 | printf("0x%08x\b\b\b\b\b\b\b\b\b\b", sector); |
| 121 | |
| 122 | /* Sector Erase */ |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 123 | chip_writeb(0xAA, bios + 0x5555); |
| 124 | chip_writeb(0x55, bios + 0x2AAA); |
| 125 | chip_writeb(0x80, bios + 0x5555); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 126 | |
Carl-Daniel Hailfinger | 0472f3d | 2009-03-06 22:26:00 +0000 | [diff] [blame] | 127 | chip_writeb(0xAA, bios + 0x5555); |
| 128 | chip_writeb(0x55, bios + 0x2AAA); |
| 129 | chip_writeb(0x30, bios + sector); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 130 | |
| 131 | /* wait for Toggle bit ready */ |
| 132 | toggle_ready_jedec(bios); |
| 133 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 134 | if (check_erased_range(flash, sector, flash->page_size)) { |
| 135 | fprintf(stderr, "ERASE FAILED!\n"); |
| 136 | return -1; |
| 137 | } |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int erase_winbond_fwhub(struct flashchip *flash) |
| 142 | { |
| 143 | int i, total_size = flash->total_size * 1024; |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 144 | |
Stefan Reinauer | 4c390c8 | 2008-07-02 13:33:09 +0000 | [diff] [blame] | 145 | unlock_winbond_fwhub(flash); |
| 146 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 147 | printf("Erasing: "); |
| 148 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 149 | for (i = 0; i < total_size; i += flash->page_size) { |
| 150 | if (erase_sector_winbond_fwhub(flash, i)) { |
| 151 | fprintf(stderr, "ERASE FAILED!\n"); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | } |
| 155 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 156 | printf("\n"); |
| 157 | |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int write_winbond_fwhub(struct flashchip *flash, uint8_t *buf) |
| 162 | { |
| 163 | int i; |
| 164 | int total_size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 165 | chipaddr bios = flash->virtual_memory; |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 166 | |
| 167 | if (erase_winbond_fwhub(flash)) |
| 168 | return -1; |
| 169 | |
| 170 | printf("Programming: "); |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 171 | for (i = 0; i < total_size; i += flash->page_size) { |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 172 | printf("0x%08x\b\b\b\b\b\b\b\b\b\b", i); |
Sean Nelson | c57a920 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 173 | write_sector_jedec_common(flash, buf + i, bios + i, flash->page_size, 0xffff); |
Stefan Reinauer | c34ce2e | 2008-03-18 00:36:18 +0000 | [diff] [blame] | 174 | } |
| 175 | printf("\n"); |
| 176 | |
| 177 | return 0; |
| 178 | } |