blob: 8e846dc303cdae0c7b736b1a404ab15db03e5557 [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
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000023static int unlock_block_winbond_fwhub(struct flashchip *flash, int offset)
24{
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000025 chipaddr wrprotect = flash->virtual_registers + offset + 2;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000026 uint8_t locking;
27
Uwe Hermann394131e2008-10-18 21:14:13 +000028 printf_debug("Trying to unlock block @0x%08x = 0x%02x\n", offset,
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000029 chip_readb(wrprotect));
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000030
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000031 locking = chip_readb(wrprotect);
Uwe Hermann394131e2008-10-18 21:14:13 +000032 switch (locking & 0x7) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000033 case 0:
34 printf_debug("Full Access.\n");
35 return 0;
36 case 1:
37 printf_debug("Write Lock (Default State).\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000038 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000039 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 Hailfinger0472f3d2009-03-06 22:26:00 +000048 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000049 return 0;
50 case 5:
51 printf_debug("Read/Write Lock.\n");
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000052 chip_writeb(0, wrprotect);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000053 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
66int unlock_winbond_fwhub(struct flashchip *flash)
67{
68 int i, total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +000069 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000070 uint8_t locking;
Uwe Hermann394131e2008-10-18 21:14:13 +000071
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000072 /* 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 Hailfinger0472f3d2009-03-06 22:26:00 +000077 chip_writeb(0xAA, bios + 0x5555);
78 chip_writeb(0x55, bios + 0x2AAA);
79 chip_writeb(0x90, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000080 programmer_delay(10);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000081
82 /* Read Hardware Lock Bits */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000083 locking = chip_readb(bios + 0xffff2);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000084
85 /* Product Identification Exit */
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +000086 chip_writeb(0xAA, bios + 0x5555);
87 chip_writeb(0x55, bios + 0x2AAA);
88 chip_writeb(0xF0, bios + 0x5555);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000089 programmer_delay(10);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000090
91 printf_debug("Lockout bits:\n");
92
Uwe Hermann394131e2008-10-18 21:14:13 +000093 if (locking & (1 << 2))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000094 fprintf(stderr, "Error: hardware bootblock locking (#TBL).\n");
95 else
96 printf_debug("No hardware bootblock locking (good!)\n");
97
Uwe Hermann394131e2008-10-18 21:14:13 +000098 if (locking & (1 << 3))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +000099 fprintf(stderr, "Error: hardware block locking (#WP).\n");
100 else
101 printf_debug("No hardware block locking (good!)\n");
102
Uwe Hermann394131e2008-10-18 21:14:13 +0000103 if (locking & ((1 << 2) | (1 << 3)))
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000104 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 Hailfinger30f7cb22009-06-15 17:23:36 +0000114static int erase_sector_winbond_fwhub(struct flashchip *flash,
Uwe Hermann394131e2008-10-18 21:14:13 +0000115 unsigned int sector)
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000116{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000117 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000118 /* 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 Hailfinger0472f3d2009-03-06 22:26:00 +0000123 chip_writeb(0xAA, bios + 0x5555);
124 chip_writeb(0x55, bios + 0x2AAA);
125 chip_writeb(0x80, bios + 0x5555);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000126
Carl-Daniel Hailfinger0472f3d2009-03-06 22:26:00 +0000127 chip_writeb(0xAA, bios + 0x5555);
128 chip_writeb(0x55, bios + 0x2AAA);
129 chip_writeb(0x30, bios + sector);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000130
131 /* wait for Toggle bit ready */
132 toggle_ready_jedec(bios);
133
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000134 if (check_erased_range(flash, sector, flash->page_size)) {
135 fprintf(stderr, "ERASE FAILED!\n");
136 return -1;
137 }
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000138 return 0;
139}
140
141int erase_winbond_fwhub(struct flashchip *flash)
142{
143 int i, total_size = flash->total_size * 1024;
Uwe Hermann394131e2008-10-18 21:14:13 +0000144
Stefan Reinauer4c390c82008-07-02 13:33:09 +0000145 unlock_winbond_fwhub(flash);
146
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000147 printf("Erasing: ");
148
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000149 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 Reinauerc34ce2e2008-03-18 00:36:18 +0000152 return -1;
153 }
154 }
155
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000156 printf("\n");
157
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000158 return 0;
159}
160
161int write_winbond_fwhub(struct flashchip *flash, uint8_t *buf)
162{
163 int i;
164 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000165 chipaddr bios = flash->virtual_memory;
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000166
167 if (erase_winbond_fwhub(flash))
168 return -1;
169
170 printf("Programming: ");
Uwe Hermann394131e2008-10-18 21:14:13 +0000171 for (i = 0; i < total_size; i += flash->page_size) {
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000172 printf("0x%08x\b\b\b\b\b\b\b\b\b\b", i);
Sean Nelsonc57a9202010-01-04 17:15:23 +0000173 write_sector_jedec_common(flash, buf + i, bios + i, flash->page_size, 0xffff);
Stefan Reinauerc34ce2e2008-03-18 00:36:18 +0000174 }
175 printf("\n");
176
177 return 0;
178}