blob: 36abbfdc8cd3e11812f2047c914a8c5e3531eb94 [file] [log] [blame]
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +00001/*
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 Reinauerc34ce2e2008-03-18 00:36:18 +000021#include "flash.h"
22
23int probe_winbond_fwhub(struct flashchip *flash)
24{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000025 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000026 uint8_t vid, did;
27
28 /* Product Identification Entry */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000029 chip_writeb(0xAA, bios + 0x5555);
30 chip_writeb(0x55, bios + 0x2AAA);
31 chip_writeb(0x90, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000032 myusec_delay(10);
33
34 /* Read product ID */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000035 vid = chip_readb(bios);
36 did = chip_readb(bios + 0x01);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000037
38 /* Product Identifixation Exit */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000039 chip_writeb(0xAA, bios + 0x5555);
40 chip_writeb(0x55, bios + 0x2AAA);
41 chip_writeb(0xF0, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000042 myusec_delay(10);
43
44 printf_debug("%s: vid 0x%x, did 0x%x\n", __FUNCTION__, vid, did);
45
46 if (vid != flash->manufacture_id || did != flash->model_id)
47 return 0;
48
49 map_flash_registers(flash);
50
51 return 1;
52}
53
54static int unlock_block_winbond_fwhub(struct flashchip *flash, int offset)
55{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000056 chipaddr wrprotect = flash->virtual_registers + offset + 2;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000057 uint8_t locking;
58
Uwe Hermann394131e2008-10-18 21:14:13 +000059 printf_debug("Trying to unlock block @0x%08x = 0x%02x\n", offset,
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000060 chip_readb(wrprotect));
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000061
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000062 locking = chip_readb(wrprotect);
Uwe Hermann394131e2008-10-18 21:14:13 +000063 switch (locking & 0x7) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000064 case 0:
65 printf_debug("Full Access.\n");
66 return 0;
67 case 1:
68 printf_debug("Write Lock (Default State).\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000069 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000070 return 0;
71 case 2:
72 printf_debug("Locked Open (Full Access, Lock Down).\n");
73 return 0;
74 case 3:
75 fprintf(stderr, "Error: Write Lock, Locked Down.\n");
76 return -1;
77 case 4:
78 printf_debug("Read Lock.\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000079 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000080 return 0;
81 case 5:
82 printf_debug("Read/Write Lock.\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000083 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000084 return 0;
85 case 6:
86 fprintf(stderr, "Error: Read Lock, Locked Down.\n");
87 return -1;
88 case 7:
89 fprintf(stderr, "Error: Read/Write Lock, Locked Down.\n");
90 return -1;
91 }
92
93 /* We will never reach this point, but GCC doesn't know */
94 return -1;
95}
96
97int unlock_winbond_fwhub(struct flashchip *flash)
98{
99 int i, total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000100 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000101 uint8_t locking;
Uwe Hermann394131e2008-10-18 21:14:13 +0000102
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000103 /* Are there any hardware restrictions that we can't overcome?
104 * If flashrom fail here, someone's got to check all those GPIOs.
105 */
106
107 /* Product Identification Entry */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000108 chip_writeb(0xAA, bios + 0x5555);
109 chip_writeb(0x55, bios + 0x2AAA);
110 chip_writeb(0x90, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000111 myusec_delay(10);
112
113 /* Read Hardware Lock Bits */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000114 locking = chip_readb(bios + 0xffff2);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000115
116 /* Product Identification Exit */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000117 chip_writeb(0xAA, bios + 0x5555);
118 chip_writeb(0x55, bios + 0x2AAA);
119 chip_writeb(0xF0, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000120 myusec_delay(10);
121
122 printf_debug("Lockout bits:\n");
123
Uwe Hermann394131e2008-10-18 21:14:13 +0000124 if (locking & (1 << 2))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000125 fprintf(stderr, "Error: hardware bootblock locking (#TBL).\n");
126 else
127 printf_debug("No hardware bootblock locking (good!)\n");
128
Uwe Hermann394131e2008-10-18 21:14:13 +0000129 if (locking & (1 << 3))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000130 fprintf(stderr, "Error: hardware block locking (#WP).\n");
131 else
132 printf_debug("No hardware block locking (good!)\n");
133
Uwe Hermann394131e2008-10-18 21:14:13 +0000134 if (locking & ((1 << 2) | (1 << 3)))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000135 return -1;
136
137 /* Unlock the complete chip */
138 for (i = 0; i < total_size; i += flash->page_size)
139 if (unlock_block_winbond_fwhub(flash, i))
140 return -1;
141
142 return 0;
143}
144
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000145static int erase_sector_winbond_fwhub(chipaddr bios,
Uwe Hermann394131e2008-10-18 21:14:13 +0000146 unsigned int sector)
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000147{
148 /* Remember: too much sleep can waste your day. */
149
150 printf("0x%08x\b\b\b\b\b\b\b\b\b\b", sector);
151
152 /* Sector Erase */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000153 chip_writeb(0xAA, bios + 0x5555);
154 chip_writeb(0x55, bios + 0x2AAA);
155 chip_writeb(0x80, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000156
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000157 chip_writeb(0xAA, bios + 0x5555);
158 chip_writeb(0x55, bios + 0x2AAA);
159 chip_writeb(0x30, bios + sector);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000160
161 /* wait for Toggle bit ready */
162 toggle_ready_jedec(bios);
163
164 return 0;
165}
166
167int erase_winbond_fwhub(struct flashchip *flash)
168{
169 int i, total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000170 chipaddr bios = flash->virtual_memory;
Uwe Hermann394131e2008-10-18 21:14:13 +0000171
Stefan Reinauer4c390c82008-07-02 13:33:09 +0000172 unlock_winbond_fwhub(flash);
173
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000174 printf("Erasing: ");
175
176 for (i = 0; i < total_size; i += flash->page_size)
177 erase_sector_winbond_fwhub(bios, i);
178
179 printf("\n");
180
181 for (i = 0; i < total_size; i++) {
Carl-Daniel Hailfinger01624f42009-05-12 15:38:55 +0000182 if (chip_readb(bios + i) != 0xff) {
183 fprintf(stderr, "Error: Flash chip erase failed at 0x%08x(0x%02x)\n", i, chip_readb(bios + i));
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000184 return -1;
185 }
186 }
187
188 return 0;
189}
190
191int write_winbond_fwhub(struct flashchip *flash, uint8_t *buf)
192{
193 int i;
194 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000195 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000196
197 if (erase_winbond_fwhub(flash))
198 return -1;
199
200 printf("Programming: ");
Uwe Hermann394131e2008-10-18 21:14:13 +0000201 for (i = 0; i < total_size; i += flash->page_size) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000202 printf("0x%08x\b\b\b\b\b\b\b\b\b\b", i);
203 write_sector_jedec(bios, buf + i, bios + i, flash->page_size);
204 }
205 printf("\n");
206
207 return 0;
208}