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