blob: 580657f960341c8c46f8bebbd1cd6e6556133471 [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 Hailfinger4e9cebb2009-09-05 01:16:30 +000025 int result = probe_jedec(flash);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000026
Carl-Daniel Hailfinger4e9cebb2009-09-05 01:16:30 +000027 if (!result)
28 return result;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000029
30 map_flash_registers(flash);
31
32 return 1;
33}
34
35static int unlock_block_winbond_fwhub(struct flashchip *flash, int offset)
36{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000037 chipaddr wrprotect = flash->virtual_registers + offset + 2;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000038 uint8_t locking;
39
Uwe Hermann394131e2008-10-18 21:14:13 +000040 printf_debug("Trying to unlock block @0x%08x = 0x%02x\n", offset,
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000041 chip_readb(wrprotect));
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000042
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000043 locking = chip_readb(wrprotect);
Uwe Hermann394131e2008-10-18 21:14:13 +000044 switch (locking & 0x7) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000045 case 0:
46 printf_debug("Full Access.\n");
47 return 0;
48 case 1:
49 printf_debug("Write Lock (Default State).\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000050 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000051 return 0;
52 case 2:
53 printf_debug("Locked Open (Full Access, Lock Down).\n");
54 return 0;
55 case 3:
56 fprintf(stderr, "Error: Write Lock, Locked Down.\n");
57 return -1;
58 case 4:
59 printf_debug("Read Lock.\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000060 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000061 return 0;
62 case 5:
63 printf_debug("Read/Write Lock.\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000064 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000065 return 0;
66 case 6:
67 fprintf(stderr, "Error: Read Lock, Locked Down.\n");
68 return -1;
69 case 7:
70 fprintf(stderr, "Error: Read/Write Lock, Locked Down.\n");
71 return -1;
72 }
73
74 /* We will never reach this point, but GCC doesn't know */
75 return -1;
76}
77
78int unlock_winbond_fwhub(struct flashchip *flash)
79{
80 int i, total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000081 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000082 uint8_t locking;
Uwe Hermann394131e2008-10-18 21:14:13 +000083
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000084 /* Are there any hardware restrictions that we can't overcome?
85 * If flashrom fail here, someone's got to check all those GPIOs.
86 */
87
88 /* Product Identification Entry */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000089 chip_writeb(0xAA, bios + 0x5555);
90 chip_writeb(0x55, bios + 0x2AAA);
91 chip_writeb(0x90, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000092 programmer_delay(10);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000093
94 /* Read Hardware Lock Bits */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000095 locking = chip_readb(bios + 0xffff2);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000096
97 /* Product Identification Exit */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000098 chip_writeb(0xAA, bios + 0x5555);
99 chip_writeb(0x55, bios + 0x2AAA);
100 chip_writeb(0xF0, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000101 programmer_delay(10);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000102
103 printf_debug("Lockout bits:\n");
104
Uwe Hermann394131e2008-10-18 21:14:13 +0000105 if (locking & (1 << 2))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000106 fprintf(stderr, "Error: hardware bootblock locking (#TBL).\n");
107 else
108 printf_debug("No hardware bootblock locking (good!)\n");
109
Uwe Hermann394131e2008-10-18 21:14:13 +0000110 if (locking & (1 << 3))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000111 fprintf(stderr, "Error: hardware block locking (#WP).\n");
112 else
113 printf_debug("No hardware block locking (good!)\n");
114
Uwe Hermann394131e2008-10-18 21:14:13 +0000115 if (locking & ((1 << 2) | (1 << 3)))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000116 return -1;
117
118 /* Unlock the complete chip */
119 for (i = 0; i < total_size; i += flash->page_size)
120 if (unlock_block_winbond_fwhub(flash, i))
121 return -1;
122
123 return 0;
124}
125
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000126static int erase_sector_winbond_fwhub(struct flashchip *flash,
Uwe Hermann394131e2008-10-18 21:14:13 +0000127 unsigned int sector)
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000128{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000129 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000130 /* Remember: too much sleep can waste your day. */
131
132 printf("0x%08x\b\b\b\b\b\b\b\b\b\b", sector);
133
134 /* Sector Erase */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000135 chip_writeb(0xAA, bios + 0x5555);
136 chip_writeb(0x55, bios + 0x2AAA);
137 chip_writeb(0x80, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000138
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000139 chip_writeb(0xAA, bios + 0x5555);
140 chip_writeb(0x55, bios + 0x2AAA);
141 chip_writeb(0x30, bios + sector);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000142
143 /* wait for Toggle bit ready */
144 toggle_ready_jedec(bios);
145
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000146 if (check_erased_range(flash, sector, flash->page_size)) {
147 fprintf(stderr, "ERASE FAILED!\n");
148 return -1;
149 }
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000150 return 0;
151}
152
153int erase_winbond_fwhub(struct flashchip *flash)
154{
155 int i, total_size = flash->total_size * 1024;
Uwe Hermann394131e2008-10-18 21:14:13 +0000156
Stefan Reinauer4c390c82008-07-02 13:33:09 +0000157 unlock_winbond_fwhub(flash);
158
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000159 printf("Erasing: ");
160
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000161 for (i = 0; i < total_size; i += flash->page_size) {
162 if (erase_sector_winbond_fwhub(flash, i)) {
163 fprintf(stderr, "ERASE FAILED!\n");
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000164 return -1;
165 }
166 }
167
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000168 printf("\n");
169
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000170 return 0;
171}
172
173int write_winbond_fwhub(struct flashchip *flash, uint8_t *buf)
174{
175 int i;
176 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000177 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000178
179 if (erase_winbond_fwhub(flash))
180 return -1;
181
182 printf("Programming: ");
Uwe Hermann394131e2008-10-18 21:14:13 +0000183 for (i = 0; i < total_size; i += flash->page_size) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000184 printf("0x%08x\b\b\b\b\b\b\b\b\b\b", i);
185 write_sector_jedec(bios, buf + i, bios + i, flash->page_size);
186 }
187 printf("\n");
188
189 return 0;
190}