1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2008 coresystems GmbH
5  * Copyright (C) 2010 Carl-Daniel Hailfinger
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include "flash.h"
19 #include "chipdrivers.h"
20 
w39_idmode_readb(struct flashctx * flash,unsigned int offset)21 static uint8_t w39_idmode_readb(struct flashctx *flash, unsigned int offset)
22 {
23 	chipaddr bios = flash->virtual_memory;
24 	uint8_t val;
25 
26 	/* Product Identification Entry */
27 	chip_writeb(flash, 0xAA, bios + 0x5555);
28 	chip_writeb(flash, 0x55, bios + 0x2AAA);
29 	chip_writeb(flash, 0x90, bios + 0x5555);
30 	programmer_delay(10);
31 
32 	/* Read something, maybe hardware lock bits */
33 	val = chip_readb(flash, bios + offset);
34 
35 	/* Product Identification Exit */
36 	chip_writeb(flash, 0xAA, bios + 0x5555);
37 	chip_writeb(flash, 0x55, bios + 0x2AAA);
38 	chip_writeb(flash, 0xF0, bios + 0x5555);
39 	programmer_delay(10);
40 
41 	return val;
42 }
43 
printlock_w39_tblwp(uint8_t lock)44 static int printlock_w39_tblwp(uint8_t lock)
45 {
46 	msg_cdbg("Hardware bootblock locking (#TBL) is %sactive.\n",
47 		 (lock & (1 << 2)) ? "" : "not ");
48 	msg_cdbg("Hardware remaining chip locking (#WP) is %sactive..\n",
49 		(lock & (1 << 3)) ? "" : "not ");
50 	if (lock & ((1 << 2) | (1 << 3)))
51 		return -1;
52 
53 	return 0;
54 }
55 
printlock_w39_single_bootblock(uint8_t lock,uint16_t kB)56 static int printlock_w39_single_bootblock(uint8_t lock, uint16_t kB)
57 {
58 	msg_cdbg("Software %d kB bootblock locking is %sactive.\n", kB, (lock & 0x03) ? "" : "not ");
59 	if (lock & 0x03)
60 		return -1;
61 
62 	return 0;
63 }
64 
printlock_w39_bootblock_64k16k(uint8_t lock)65 static int printlock_w39_bootblock_64k16k(uint8_t lock)
66 {
67 	msg_cdbg("Software 64 kB bootblock locking is %sactive.\n",
68 		 (lock & (1 << 0)) ? "" : "not ");
69 	msg_cdbg("Software 16 kB bootblock locking is %sactive.\n",
70 		 (lock & (1 << 1)) ? "" : "not ");
71 	if (lock & ((1 << 1) | (1 << 0)))
72 		return -1;
73 
74 	return 0;
75 }
76 
printlock_w39_common(struct flashctx * flash,unsigned int offset)77 static int printlock_w39_common(struct flashctx *flash, unsigned int offset)
78 {
79 	uint8_t lock;
80 
81 	lock = w39_idmode_readb(flash, offset);
82 	msg_cdbg("Lockout bits:\n");
83 	return printlock_w39_tblwp(lock);
84 }
85 
printlock_w39f010(struct flashctx * flash)86 int printlock_w39f010(struct flashctx *flash)
87 {
88 	uint8_t lock;
89 	int ret;
90 
91 	lock = w39_idmode_readb(flash, 0x00002);
92 	msg_cdbg("Bottom boot block:\n");
93 	ret = printlock_w39_single_bootblock(lock, 16);
94 
95 	lock = w39_idmode_readb(flash, 0x1fff2);
96 	msg_cdbg("Top boot block:\n");
97 	ret |= printlock_w39_single_bootblock(lock, 16);
98 
99 	return ret;
100 }
101 
printlock_w39l010(struct flashctx * flash)102 int printlock_w39l010(struct flashctx *flash)
103 {
104 	uint8_t lock;
105 	int ret;
106 
107 	lock = w39_idmode_readb(flash, 0x00002);
108 	msg_cdbg("Bottom boot block:\n");
109 	ret = printlock_w39_single_bootblock(lock, 8);
110 
111 	lock = w39_idmode_readb(flash, 0x1fff2);
112 	msg_cdbg("Top boot block:\n");
113 	ret |= printlock_w39_single_bootblock(lock, 8);
114 
115 	return ret;
116 }
117 
printlock_w39l020(struct flashctx * flash)118 int printlock_w39l020(struct flashctx *flash)
119 {
120 	uint8_t lock;
121 	int ret;
122 
123 	lock = w39_idmode_readb(flash, 0x00002);
124 	msg_cdbg("Bottom boot block:\n");
125 	ret = printlock_w39_bootblock_64k16k(lock);
126 
127 	lock = w39_idmode_readb(flash, 0x3fff2);
128 	msg_cdbg("Top boot block:\n");
129 	ret |= printlock_w39_bootblock_64k16k(lock);
130 
131 	return ret;
132 }
133 
printlock_w39l040(struct flashctx * flash)134 int printlock_w39l040(struct flashctx *flash)
135 {
136 	uint8_t lock;
137 	int ret;
138 
139 	lock = w39_idmode_readb(flash, 0x00002);
140 	msg_cdbg("Bottom boot block:\n");
141 	ret = printlock_w39_bootblock_64k16k(lock);
142 
143 	lock = w39_idmode_readb(flash, 0x7fff2);
144 	msg_cdbg("Top boot block:\n");
145 	ret |= printlock_w39_bootblock_64k16k(lock);
146 
147 	return ret;
148 }
149 
printlock_w39v040a(struct flashctx * flash)150 int printlock_w39v040a(struct flashctx *flash)
151 {
152 	uint8_t lock;
153 	int ret = 0;
154 
155 	/* The W39V040A datasheet contradicts itself on the lock register
156 	 * location: 0x00002 and 0x7fff2 are both mentioned. Pick the one
157 	 * which is similar to the other chips of the same family.
158 	 */
159 	lock = w39_idmode_readb(flash, 0x7fff2);
160 	msg_cdbg("Lockout bits:\n");
161 
162 	ret = printlock_w39_tblwp(lock);
163 	ret |= printlock_w39_bootblock_64k16k(lock);
164 
165 	return ret;
166 }
167 
printlock_w39v040b(struct flashctx * flash)168 int printlock_w39v040b(struct flashctx *flash)
169 {
170 	return printlock_w39_common(flash, 0x7fff2);
171 }
172 
printlock_w39v040c(struct flashctx * flash)173 int printlock_w39v040c(struct flashctx *flash)
174 {
175 	/* Typo in the datasheet? The other chips use 0x7fff2. */
176 	return printlock_w39_common(flash, 0xfff2);
177 }
178 
printlock_w39v040fa(struct flashctx * flash)179 int printlock_w39v040fa(struct flashctx *flash)
180 {
181 	int ret = 0;
182 
183 	ret = printlock_w39v040a(flash);
184 	ret |= printlock_regspace2_uniform_64k(flash);
185 
186 	return ret;
187 }
188 
printlock_w39v040fb(struct flashctx * flash)189 int printlock_w39v040fb(struct flashctx *flash)
190 {
191 	int ret = 0;
192 
193 	ret = printlock_w39v040b(flash);
194 	ret |= printlock_regspace2_uniform_64k(flash);
195 
196 	return ret;
197 }
198 
printlock_w39v040fc(struct flashctx * flash)199 int printlock_w39v040fc(struct flashctx *flash)
200 {
201 	int ret = 0;
202 
203 	/* W39V040C and W39V040FC use different WP/TBL offsets. */
204 	ret = printlock_w39_common(flash, 0x7fff2);
205 	ret |= printlock_regspace2_uniform_64k(flash);
206 
207 	return ret;
208 }
209 
printlock_w39v080a(struct flashctx * flash)210 int printlock_w39v080a(struct flashctx *flash)
211 {
212 	return printlock_w39_common(flash, 0xffff2);
213 }
214 
printlock_w39v080fa(struct flashctx * flash)215 int printlock_w39v080fa(struct flashctx *flash)
216 {
217 	int ret = 0;
218 
219 	ret = printlock_w39v080a(flash);
220 	ret |= printlock_regspace2_uniform_64k(flash);
221 
222 	return ret;
223 }
224 
printlock_w39v080fa_dual(struct flashctx * flash)225 int printlock_w39v080fa_dual(struct flashctx *flash)
226 {
227 	msg_cinfo("Block locking for W39V080FA in dual mode is "
228 		  "undocumented.\n");
229 	/* Better safe than sorry. */
230 	return -1;
231 }
232 
printlock_at49f(struct flashctx * flash)233 int printlock_at49f(struct flashctx *flash)
234 {
235 	uint8_t lock = w39_idmode_readb(flash, 0x00002);
236 	msg_cdbg("Hardware bootblock lockout is %sactive.\n",
237 		 (lock & 0x01) ? "" : "not ");
238 	return 0;
239 }
240