blob: 289a2bd6c5c8568ad7f0a62e39bb4441e39318e9 [file] [log] [blame]
David Hendricksd1c55d72010-08-24 15:14:19 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
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
David Hendricksf7924d12010-06-10 21:26:44 -070021#include <stdlib.h>
22#include <string.h>
23
24#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080027#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070028#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070029
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +080030/* When update flash's status register, it takes few time to erase register.
31 * After surveying some flash vendor specs, such as Winbond, MXIC, EON,
32 * all of their update time are less than 20ms. After refering the spi25.c,
33 * use 100ms delay.
34 */
35#define WRITE_STATUS_REGISTER_DELAY 100 * 1000 /* unit: us */
36
David Hendricks1c09f802012-10-03 11:03:48 -070037/*
David Hendricksf7924d12010-06-10 21:26:44 -070038 * The following procedures rely on look-up tables to match the user-specified
39 * range with the chip's supported ranges. This turned out to be the most
40 * elegant approach since diferent flash chips use different levels of
41 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070042 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070043 */
44
45struct wp_range {
46 unsigned int start; /* starting address */
47 unsigned int len; /* len */
48};
49
50enum bit_state {
51 OFF = 0,
52 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080053 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070054};
55
David Hendrickse0512a72014-07-15 20:30:47 -070056/*
57 * Generic write-protection schema for 25-series SPI flash chips. This assumes
58 * there is a status register that contains one or more consecutive bits which
59 * determine which address range is protected.
60 */
61
62struct status_register_layout {
63 int bp0_pos; /* position of BP0 */
64 int bp_bits; /* number of block protect bits */
65 int srp_pos; /* position of status register protect enable bit */
66};
67
68struct generic_range {
69 unsigned int bp; /* block protect bitfield */
70 struct wp_range range;
71};
72
73struct generic_wp {
74 struct status_register_layout sr1; /* status register 1 */
75 struct generic_range *ranges;
76};
77
78/*
79 * The following ranges and functions are useful for representing Winbond-
80 * style writeprotect schema in which there are typically 5 bits of
81 * relevant information stored in status register 1:
82 * sec: This bit indicates the units (sectors vs. blocks)
83 * tb: The top-bottom bit indicates if the affected range is at the top of
84 * the flash memory's address space or at the bottom.
85 * bp[2:0]: The number of affected sectors/blocks.
86 */
David Hendricksf7924d12010-06-10 21:26:44 -070087struct w25q_range {
88 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
89 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080090 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070091 struct wp_range range;
92};
93
David Hendrickse0512a72014-07-15 20:30:47 -070094/*
95 * Mask to extract write-protect enable and range bits
96 * Status register 1:
97 * SRP0: bit 7
98 * range(BP2-BP0): bit 4-2
99 * Status register 2:
100 * SRP1: bit 1
101 */
102#define MASK_WP_AREA (0x9C)
103#define MASK_WP2_AREA (0x01)
104
David Hendricks57566ed2010-08-16 18:24:45 -0700105struct w25q_range en25f40_ranges[] = {
106 { X, X, 0, {0, 0} }, /* none */
107 { 0, 0, 0x1, {0x000000, 504 * 1024} },
108 { 0, 0, 0x2, {0x000000, 496 * 1024} },
109 { 0, 0, 0x3, {0x000000, 480 * 1024} },
110 { 0, 0, 0x4, {0x000000, 448 * 1024} },
111 { 0, 0, 0x5, {0x000000, 384 * 1024} },
112 { 0, 0, 0x6, {0x000000, 256 * 1024} },
113 { 0, 0, 0x7, {0x000000, 512 * 1024} },
114};
115
David Hendrickse185bf22011-05-24 15:34:18 -0700116struct w25q_range en25q40_ranges[] = {
117 { 0, 0, 0, {0, 0} }, /* none */
118 { 0, 0, 0x1, {0x000000, 504 * 1024} },
119 { 0, 0, 0x2, {0x000000, 496 * 1024} },
120 { 0, 0, 0x3, {0x000000, 480 * 1024} },
121
122 { 0, 1, 0x0, {0x000000, 448 * 1024} },
123 { 0, 1, 0x1, {0x000000, 384 * 1024} },
124 { 0, 1, 0x2, {0x000000, 256 * 1024} },
125 { 0, 1, 0x3, {0x000000, 512 * 1024} },
126};
127
128struct w25q_range en25q80_ranges[] = {
129 { 0, 0, 0, {0, 0} }, /* none */
130 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
131 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
132 { 0, 0, 0x3, {0x000000, 992 * 1024} },
133 { 0, 0, 0x4, {0x000000, 960 * 1024} },
134 { 0, 0, 0x5, {0x000000, 896 * 1024} },
135 { 0, 0, 0x6, {0x000000, 768 * 1024} },
136 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
137};
138
139struct w25q_range en25q32_ranges[] = {
140 { 0, 0, 0, {0, 0} }, /* none */
141 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
142 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
143 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
144 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
145 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
146 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
147 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
148
149 { 0, 1, 0, {0, 0} }, /* none */
150 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
151 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
152 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
153 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
154 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
155 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
156 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
157};
158
159struct w25q_range en25q64_ranges[] = {
160 { 0, 0, 0, {0, 0} }, /* none */
161 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
162 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
163 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
164 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
165 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
166 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
167 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
168
169 { 0, 1, 0, {0, 0} }, /* none */
170 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
171 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
172 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
173 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
174 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
175 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
176 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
177};
178
179struct w25q_range en25q128_ranges[] = {
180 { 0, 0, 0, {0, 0} }, /* none */
181 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
182 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
183 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
184 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
185 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
186 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
187 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
188
189 { 0, 1, 0, {0, 0} }, /* none */
190 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
191 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
192 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
193 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
194 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
195 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
196 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
197};
198
Marc Jonesb2f90022014-04-29 17:37:23 -0600199struct w25q_range en25s64_ranges[] = {
200 { 0, 0, 0, {0, 0} }, /* none */
201 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
202 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
203 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
204 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
205 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
206 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
207 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
208
209 { 0, 1, 0, {0, 0} }, /* none */
210 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
211 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
212 { 0, 1, 0x3, {0x780000, 512 * 1024} },
213 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
214 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
215 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
216 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
217};
218
David Hendricksf8f00c72011-02-01 12:39:46 -0800219/* mx25l1005 ranges also work for the mx25l1005c */
220static struct w25q_range mx25l1005_ranges[] = {
221 { X, X, 0, {0, 0} }, /* none */
222 { X, X, 0x1, {0x010000, 64 * 1024} },
223 { X, X, 0x2, {0x000000, 128 * 1024} },
224 { X, X, 0x3, {0x000000, 128 * 1024} },
225};
226
227static struct w25q_range mx25l2005_ranges[] = {
228 { X, X, 0, {0, 0} }, /* none */
229 { X, X, 0x1, {0x030000, 64 * 1024} },
230 { X, X, 0x2, {0x020000, 128 * 1024} },
231 { X, X, 0x3, {0x000000, 256 * 1024} },
232};
233
234static struct w25q_range mx25l4005_ranges[] = {
235 { X, X, 0, {0, 0} }, /* none */
236 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
237 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
238 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
239 { X, X, 0x4, {0x000000, 512 * 1024} },
240 { X, X, 0x5, {0x000000, 512 * 1024} },
241 { X, X, 0x6, {0x000000, 512 * 1024} },
242 { X, X, 0x7, {0x000000, 512 * 1024} },
243};
244
245static struct w25q_range mx25l8005_ranges[] = {
246 { X, X, 0, {0, 0} }, /* none */
247 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
248 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
249 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
250 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
251 { X, X, 0x5, {0x000000, 1024 * 1024} },
252 { X, X, 0x6, {0x000000, 1024 * 1024} },
253 { X, X, 0x7, {0x000000, 1024 * 1024} },
254};
255
256#if 0
257/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
258static struct w25q_range mx25l1605_ranges[] = {
259 { X, X, 0, {0, 0} }, /* none */
260 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
261 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
262 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
263 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
264 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
265 { X, X, 0x6, {0x000000, 2048 * 1024} },
266 { X, X, 0x7, {0x000000, 2048 * 1024} },
267};
268#endif
269
270#if 0
271/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
272static struct w25q_range mx25l6405_ranges[] = {
273 { X, 0, 0, {0, 0} }, /* none */
274 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
275 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
276 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
277 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
278 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
279 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
280 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
281
282 { X, 1, 0x0, {0x000000, 8192 * 1024} },
283 { X, 1, 0x1, {0x000000, 8192 * 1024} },
284 { X, 1, 0x2, {0x000000, 8192 * 1024} },
285 { X, 1, 0x3, {0x000000, 8192 * 1024} },
286 { X, 1, 0x4, {0x000000, 8192 * 1024} },
287 { X, 1, 0x5, {0x000000, 8192 * 1024} },
288 { X, 1, 0x6, {0x000000, 8192 * 1024} },
289 { X, 1, 0x7, {0x000000, 8192 * 1024} },
290};
291#endif
292
293static struct w25q_range mx25l1605d_ranges[] = {
294 { X, 0, 0, {0, 0} }, /* none */
295 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
296 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
297 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
298 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
299 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
300 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
301 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
302
303 { X, 1, 0x0, {0x000000, 2048 * 1024} },
304 { X, 1, 0x1, {0x000000, 2048 * 1024} },
305 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
306 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
307 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
308 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
309 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
310 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
311};
312
313/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700314static struct w25q_range mx25l3205d_ranges[] = {
315 { X, 0, 0, {0, 0} }, /* none */
316 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
317 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
318 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
319 { X, 0, 0x4, {0x380000, 512 * 1024} },
320 { X, 0, 0x5, {0x300000, 1024 * 1024} },
321 { X, 0, 0x6, {0x200000, 2048 * 1024} },
322 { X, 0, 0x7, {0x000000, 4096 * 1024} },
323
324 { X, 1, 0x0, {0x000000, 4096 * 1024} },
325 { X, 1, 0x1, {0x000000, 2048 * 1024} },
326 { X, 1, 0x2, {0x000000, 3072 * 1024} },
327 { X, 1, 0x3, {0x000000, 3584 * 1024} },
328 { X, 1, 0x4, {0x000000, 3840 * 1024} },
329 { X, 1, 0x5, {0x000000, 3968 * 1024} },
330 { X, 1, 0x6, {0x000000, 4032 * 1024} },
331 { X, 1, 0x7, {0x000000, 4096 * 1024} },
332};
333
Vincent Palatin87e092a2013-02-28 15:46:14 -0800334static struct w25q_range mx25u3235e_ranges[] = {
335 { X, 0, 0, {0, 0} }, /* none */
336 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
337 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
338 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
339 { 0, 0, 0x4, {0x380000, 512 * 1024} },
340 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
341 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
342 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
343
344 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
345 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
346 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
347 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
348 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
349 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
350 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
351 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
352};
353
David Hendricksbfa624b2012-07-24 12:47:59 -0700354static struct w25q_range n25q064_ranges[] = {
355 { X, 0, 0, {0, 0} }, /* none */
356
357 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
358 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
359 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
360 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
361 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
362 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
363 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
364
365 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
366 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
367 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
368 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
369 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
370 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
371 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
372
373 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
374 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
375 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
376 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
377 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
378 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
379 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
380 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
381};
382
David Hendricksf7924d12010-06-10 21:26:44 -0700383static struct w25q_range w25q16_ranges[] = {
384 { X, X, 0, {0, 0} }, /* none */
385 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
386 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
387 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
388 { 0, 0, 0x4, {0x180000, 512 * 1024} },
389 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
390
391 { 0, 1, 0x1, {0x000000, 64 * 1024} },
392 { 0, 1, 0x2, {0x000000, 128 * 1024} },
393 { 0, 1, 0x3, {0x000000, 256 * 1024} },
394 { 0, 1, 0x4, {0x000000, 512 * 1024} },
395 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
396 { X, X, 0x6, {0x000000, 2048 * 1024} },
397 { X, X, 0x7, {0x000000, 2048 * 1024} },
398
399 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
400 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
401 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
402 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
403 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
404
405 { 1, 1, 0x1, {0x000000, 4 * 1024} },
406 { 1, 1, 0x2, {0x000000, 8 * 1024} },
407 { 1, 1, 0x3, {0x000000, 16 * 1024} },
408 { 1, 1, 0x4, {0x000000, 32 * 1024} },
409 { 1, 1, 0x5, {0x000000, 32 * 1024} },
410};
411
412static struct w25q_range w25q32_ranges[] = {
413 { X, X, 0, {0, 0} }, /* none */
414 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
415 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
416 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
417 { 0, 0, 0x4, {0x380000, 512 * 1024} },
418 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700419 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700420
421 { 0, 1, 0x1, {0x000000, 64 * 1024} },
422 { 0, 1, 0x2, {0x000000, 128 * 1024} },
423 { 0, 1, 0x3, {0x000000, 256 * 1024} },
424 { 0, 1, 0x4, {0x000000, 512 * 1024} },
425 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
426 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
427 { X, X, 0x7, {0x000000, 4096 * 1024} },
428
429 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
430 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
431 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
432 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
433 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
434
435 { 1, 1, 0x1, {0x000000, 4 * 1024} },
436 { 1, 1, 0x2, {0x000000, 8 * 1024} },
437 { 1, 1, 0x3, {0x000000, 16 * 1024} },
438 { 1, 1, 0x4, {0x000000, 32 * 1024} },
439 { 1, 1, 0x5, {0x000000, 32 * 1024} },
440};
441
442static struct w25q_range w25q80_ranges[] = {
443 { X, X, 0, {0, 0} }, /* none */
444 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
445 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
446 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
447 { 0, 0, 0x4, {0x080000, 512 * 1024} },
448
449 { 0, 1, 0x1, {0x000000, 64 * 1024} },
450 { 0, 1, 0x2, {0x000000, 128 * 1024} },
451 { 0, 1, 0x3, {0x000000, 256 * 1024} },
452 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700453 { X, X, 0x6, {0x000000, 1024 * 1024} },
454 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700455
456 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
457 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
458 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
459 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
460 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
461
462 { 1, 1, 0x1, {0x000000, 4 * 1024} },
463 { 1, 1, 0x2, {0x000000, 8 * 1024} },
464 { 1, 1, 0x3, {0x000000, 16 * 1024} },
465 { 1, 1, 0x4, {0x000000, 32 * 1024} },
466 { 1, 1, 0x5, {0x000000, 32 * 1024} },
467};
468
David Hendricks2c4a76c2010-06-28 14:00:43 -0700469static struct w25q_range w25q64_ranges[] = {
470 { X, X, 0, {0, 0} }, /* none */
471
472 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
473 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
474 { 0, 0, 0x3, {0x780000, 512 * 1024} },
475 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
476 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
477 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
478
479 { 0, 1, 0x1, {0x000000, 128 * 1024} },
480 { 0, 1, 0x2, {0x000000, 256 * 1024} },
481 { 0, 1, 0x3, {0x000000, 512 * 1024} },
482 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
483 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
484 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
485 { X, X, 0x7, {0x000000, 8192 * 1024} },
486
487 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
488 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
489 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
490 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
491 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
492
493 { 1, 1, 0x1, {0x000000, 4 * 1024} },
494 { 1, 1, 0x2, {0x000000, 8 * 1024} },
495 { 1, 1, 0x3, {0x000000, 16 * 1024} },
496 { 1, 1, 0x4, {0x000000, 32 * 1024} },
497 { 1, 1, 0x5, {0x000000, 32 * 1024} },
498};
499
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800500struct w25q_range w25x10_ranges[] = {
501 { X, X, 0, {0, 0} }, /* none */
502 { 0, 0, 0x1, {0x010000, 64 * 1024} },
503 { 0, 1, 0x1, {0x000000, 64 * 1024} },
504 { X, X, 0x2, {0x000000, 128 * 1024} },
505 { X, X, 0x3, {0x000000, 128 * 1024} },
506};
507
508struct w25q_range w25x20_ranges[] = {
509 { X, X, 0, {0, 0} }, /* none */
510 { 0, 0, 0x1, {0x030000, 64 * 1024} },
511 { 0, 0, 0x2, {0x020000, 128 * 1024} },
512 { 0, 1, 0x1, {0x000000, 64 * 1024} },
513 { 0, 1, 0x2, {0x000000, 128 * 1024} },
514 { 0, X, 0x3, {0x000000, 256 * 1024} },
515};
516
David Hendricks470ca952010-08-13 14:01:53 -0700517struct w25q_range w25x40_ranges[] = {
518 { X, X, 0, {0, 0} }, /* none */
519 { 0, 0, 0x1, {0x070000, 64 * 1024} },
520 { 0, 0, 0x2, {0x060000, 128 * 1024} },
521 { 0, 0, 0x3, {0x040000, 256 * 1024} },
522 { 0, 1, 0x1, {0x000000, 64 * 1024} },
523 { 0, 1, 0x2, {0x000000, 128 * 1024} },
524 { 0, 1, 0x3, {0x000000, 256 * 1024} },
525 { 0, X, 0x4, {0x000000, 512 * 1024} },
526};
527
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800528struct w25q_range w25x80_ranges[] = {
529 { X, X, 0, {0, 0} }, /* none */
530 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
531 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
532 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
533 { 0, 0, 0x4, {0x080000, 512 * 1024} },
534 { 0, 1, 0x1, {0x000000, 64 * 1024} },
535 { 0, 1, 0x2, {0x000000, 128 * 1024} },
536 { 0, 1, 0x3, {0x000000, 256 * 1024} },
537 { 0, 1, 0x4, {0x000000, 512 * 1024} },
538 { 0, X, 0x5, {0x000000, 1024 * 1024} },
539 { 0, X, 0x6, {0x000000, 1024 * 1024} },
540 { 0, X, 0x7, {0x000000, 1024 * 1024} },
541};
542
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700543static struct w25q_range gd25q64_ranges[] = {
544 { X, X, 0, {0, 0} }, /* none */
545 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
546 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
547 { 0, 0, 0x3, {0x780000, 512 * 1024} },
548 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
549 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
550 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
551
552 { 0, 1, 0x1, {0x000000, 128 * 1024} },
553 { 0, 1, 0x2, {0x000000, 256 * 1024} },
554 { 0, 1, 0x3, {0x000000, 512 * 1024} },
555 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
556 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
557 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
558 { X, X, 0x7, {0x000000, 8192 * 1024} },
559
560 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
561 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
562 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
563 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
564 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
565 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
566
567 { 1, 1, 0x1, {0x000000, 4 * 1024} },
568 { 1, 1, 0x2, {0x000000, 8 * 1024} },
569 { 1, 1, 0x3, {0x000000, 16 * 1024} },
570 { 1, 1, 0x4, {0x000000, 32 * 1024} },
571 { 1, 1, 0x5, {0x000000, 32 * 1024} },
572 { 1, 1, 0x6, {0x000000, 32 * 1024} },
573};
574
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800575static struct w25q_range a25l040_ranges[] = {
576 { X, X, 0x0, {0, 0} }, /* none */
577 { X, X, 0x1, {0x70000, 64 * 1024} },
578 { X, X, 0x2, {0x60000, 128 * 1024} },
579 { X, X, 0x3, {0x40000, 256 * 1024} },
580 { X, X, 0x4, {0x00000, 512 * 1024} },
581 { X, X, 0x5, {0x00000, 512 * 1024} },
582 { X, X, 0x6, {0x00000, 512 * 1024} },
583 { X, X, 0x7, {0x00000, 512 * 1024} },
584};
585
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800586/* Given a flash chip, this function returns its range table. */
587static int w25_range_table(const struct flashchip *flash,
588 struct w25q_range **w25q_ranges,
589 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700590{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800591 *w25q_ranges = 0;
592 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700593
David Hendricksd494b0a2010-08-16 16:28:50 -0700594 switch (flash->manufacture_id) {
595 case WINBOND_NEX_ID:
596 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800597 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800598 *w25q_ranges = w25x10_ranges;
599 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800600 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800601 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800602 *w25q_ranges = w25x20_ranges;
603 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800604 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800605 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800606 *w25q_ranges = w25x40_ranges;
607 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700608 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800609 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800610 *w25q_ranges = w25x80_ranges;
611 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800612 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800613 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800614 *w25q_ranges = w25q80_ranges;
615 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700616 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800617 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800618 *w25q_ranges = w25q16_ranges;
619 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700620 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800621 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800622 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800623 *w25q_ranges = w25q32_ranges;
624 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700625 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800626 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800627 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800628 *w25q_ranges = w25q64_ranges;
629 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700630 break;
631 default:
632 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
633 ", aborting\n", __func__, __LINE__,
634 flash->model_id);
635 return -1;
636 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700637 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700638 case EON_ID_NOPREFIX:
639 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800640 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800641 *w25q_ranges = en25f40_ranges;
642 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700643 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700644 case EON_EN25Q40:
645 *w25q_ranges = en25q40_ranges;
646 *num_entries = ARRAY_SIZE(en25q40_ranges);
647 break;
648 case EON_EN25Q80:
649 *w25q_ranges = en25q80_ranges;
650 *num_entries = ARRAY_SIZE(en25q80_ranges);
651 break;
652 case EON_EN25Q32:
653 *w25q_ranges = en25q32_ranges;
654 *num_entries = ARRAY_SIZE(en25q32_ranges);
655 break;
656 case EON_EN25Q64:
657 *w25q_ranges = en25q64_ranges;
658 *num_entries = ARRAY_SIZE(en25q64_ranges);
659 break;
660 case EON_EN25Q128:
661 *w25q_ranges = en25q128_ranges;
662 *num_entries = ARRAY_SIZE(en25q128_ranges);
663 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600664 case EON_EN25S64:
665 *w25q_ranges = en25s64_ranges;
666 *num_entries = ARRAY_SIZE(en25s64_ranges);
667 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700668 default:
669 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
670 ", aborting\n", __func__, __LINE__,
671 flash->model_id);
672 return -1;
673 }
674 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800675 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700676 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800677 case MACRONIX_MX25L1005:
678 *w25q_ranges = mx25l1005_ranges;
679 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
680 break;
681 case MACRONIX_MX25L2005:
682 *w25q_ranges = mx25l2005_ranges;
683 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
684 break;
685 case MACRONIX_MX25L4005:
686 *w25q_ranges = mx25l4005_ranges;
687 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
688 break;
689 case MACRONIX_MX25L8005:
690 *w25q_ranges = mx25l8005_ranges;
691 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
692 break;
693 case MACRONIX_MX25L1605:
694 /* FIXME: MX25L1605 and MX25L1605D have different write
695 * protection capabilities, but share IDs */
696 *w25q_ranges = mx25l1605d_ranges;
697 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
698 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800699 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800700 *w25q_ranges = mx25l3205d_ranges;
701 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700702 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800703 case MACRONIX_MX25U3235E:
704 *w25q_ranges = mx25u3235e_ranges;
705 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
706 break;
David Hendricksac72e362010-08-16 18:20:03 -0700707 default:
708 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
709 ", aborting\n", __func__, __LINE__,
710 flash->model_id);
711 return -1;
712 }
713 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700714 case ST_ID:
715 switch(flash->model_id) {
716 case ST_N25Q064__1E:
717 case ST_N25Q064__3E:
718 *w25q_ranges = n25q064_ranges;
719 *num_entries = ARRAY_SIZE(n25q064_ranges);
720 break;
721 default:
722 msg_cerr("%s() %d: Micron flash chip mismatch"
723 " (0x%04x), aborting\n", __func__, __LINE__,
724 flash->model_id);
725 return -1;
726 }
727 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700728 case GIGADEVICE_ID:
729 switch(flash->model_id) {
730 case GIGADEVICE_GD25LQ32:
731 *w25q_ranges = w25q32_ranges;
732 *num_entries = ARRAY_SIZE(w25q32_ranges);
733 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700734 case GIGADEVICE_GD25Q64:
735 *w25q_ranges = gd25q64_ranges;
736 *num_entries = ARRAY_SIZE(gd25q64_ranges);
737 break;
738 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700739 default:
740 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
741 " (0x%04x), aborting\n", __func__, __LINE__,
742 flash->model_id);
743 return -1;
744 }
745 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800746 case AMIC_ID_NOPREFIX:
747 switch(flash->model_id) {
748 case AMIC_A25L040:
749 *w25q_ranges = a25l040_ranges;
750 *num_entries = ARRAY_SIZE(a25l040_ranges);
751 break;
752 default:
753 msg_cerr("%s() %d: AMIC flash chip mismatch"
754 " (0x%04x), aborting\n", __func__, __LINE__,
755 flash->model_id);
756 return -1;
757 }
758 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700759 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700760 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
761 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700762 return -1;
763 }
764
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800765 return 0;
766}
767
768int w25_range_to_status(const struct flashchip *flash,
769 unsigned int start, unsigned int len,
770 struct w25q_status *status)
771{
772 struct w25q_range *w25q_ranges;
773 int i, range_found = 0;
774 int num_entries;
775
776 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700777 for (i = 0; i < num_entries; i++) {
778 struct wp_range *r = &w25q_ranges[i].range;
779
780 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
781 start, len, r->start, r->len);
782 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700783 status->bp0 = w25q_ranges[i].bp & 1;
784 status->bp1 = w25q_ranges[i].bp >> 1;
785 status->bp2 = w25q_ranges[i].bp >> 2;
786 status->tb = w25q_ranges[i].tb;
787 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700788
789 range_found = 1;
790 break;
791 }
792 }
793
794 if (!range_found) {
795 msg_cerr("matching range not found\n");
796 return -1;
797 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700798 return 0;
799}
800
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800801int w25_status_to_range(const struct flashchip *flash,
802 const struct w25q_status *status,
803 unsigned int *start, unsigned int *len)
804{
805 struct w25q_range *w25q_ranges;
806 int i, status_found = 0;
807 int num_entries;
808
809 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
810 for (i = 0; i < num_entries; i++) {
811 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800812 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800813
814 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
815 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
816 bp, w25q_ranges[i].bp,
817 status->tb, w25q_ranges[i].tb,
818 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800819 table_bp = w25q_ranges[i].bp;
820 table_tb = w25q_ranges[i].tb;
821 table_sec = w25q_ranges[i].sec;
822 if ((bp == table_bp || table_bp == X) &&
823 (status->tb == table_tb || table_tb == X) &&
824 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800825 *start = w25q_ranges[i].range.start;
826 *len = w25q_ranges[i].range.len;
827
828 status_found = 1;
829 break;
830 }
831 }
832
833 if (!status_found) {
834 msg_cerr("matching status not found\n");
835 return -1;
836 }
837 return 0;
838}
839
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800840/* Since most chips we use must be WREN-ed before WRSR,
841 * we copy a write status function here before we have a good solution. */
842static int spi_write_status_register_WREN(int status)
843{
844 int result;
845 struct spi_command cmds[] = {
846 {
847 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
848 .writecnt = JEDEC_WREN_OUTSIZE,
849 .writearr = (const unsigned char[]){ JEDEC_WREN },
850 .readcnt = 0,
851 .readarr = NULL,
852 }, {
853 .writecnt = JEDEC_WRSR_OUTSIZE,
854 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
855 .readcnt = 0,
856 .readarr = NULL,
857 }, {
858 .writecnt = 0,
859 .writearr = NULL,
860 .readcnt = 0,
861 .readarr = NULL,
862 }};
863
864 result = spi_send_multicommand(cmds);
865 if (result) {
866 msg_cerr("%s failed during command execution\n",
867 __func__);
868 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800869
870 /* WRSR performs a self-timed erase before the changes take effect. */
871 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
872
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800873 return result;
874}
875
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800876/* Given a [start, len], this function calls w25_range_to_status() to convert
877 * it to flash-chip-specific range bits, then sets into status register.
878 */
David Hendricks91040832011-07-08 20:01:09 -0700879static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700880 unsigned int start, unsigned int len)
881{
882 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800883 int tmp = 0;
884 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700885
886 memset(&status, 0, sizeof(status));
887 tmp = spi_read_status_register();
888 memcpy(&status, &tmp, 1);
889 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
890
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800891 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700892
893 msg_cdbg("status.busy: %x\n", status.busy);
894 msg_cdbg("status.wel: %x\n", status.wel);
895 msg_cdbg("status.bp0: %x\n", status.bp0);
896 msg_cdbg("status.bp1: %x\n", status.bp1);
897 msg_cdbg("status.bp2: %x\n", status.bp2);
898 msg_cdbg("status.tb: %x\n", status.tb);
899 msg_cdbg("status.sec: %x\n", status.sec);
900 msg_cdbg("status.srp0: %x\n", status.srp0);
901
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800902 memcpy(&expected, &status, sizeof(status));
903 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700904
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800905 tmp = spi_read_status_register();
906 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
907 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800908 return 0;
909 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800910 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800911 expected, tmp);
912 return 1;
913 }
David Hendricksf7924d12010-06-10 21:26:44 -0700914}
915
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800916/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700917static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800918{
919 struct w25q_status status;
920 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700921 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800922 int ret = 0;
923
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800924 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800925 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800926 memcpy(&status, &tmp, 1);
927 msg_cinfo("WP: status: 0x%02x\n", tmp);
928 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
929 msg_cinfo("WP: write protect is %s.\n",
930 status.srp0 ? "enabled" : "disabled");
931
932 msg_cinfo("WP: write protect range: ");
933 if (w25_status_to_range(flash, &status, &start, &len)) {
934 msg_cinfo("(cannot resolve the range)\n");
935 ret = -1;
936 } else {
937 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
938 }
939
940 return ret;
941}
942
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800943/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700944static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700945{
946 struct w25q_status status;
947 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800948 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700949
950 memset(&status, 0, sizeof(status));
951 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800952 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700953 memcpy(&status, &tmp, 1);
954 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
955
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800956 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800957 memcpy(&expected, &status, sizeof(status));
958 spi_write_status_register_WREN(expected);
959
960 tmp = spi_read_status_register();
961 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
962 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
963 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700964
965 return 0;
966}
967
David Hendricks1c09f802012-10-03 11:03:48 -0700968static int w25_enable_writeprotect(const struct flashchip *flash,
969 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800970{
971 int ret;
972
David Hendricks1c09f802012-10-03 11:03:48 -0700973 switch (wp_mode) {
974 case WP_MODE_HARDWARE:
975 ret = w25_set_srp0(flash, 1);
976 break;
977 default:
978 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
979 return 1;
980 }
981
David Hendricksc801adb2010-12-09 16:58:56 -0800982 if (ret)
983 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800984 return ret;
985}
986
David Hendricks91040832011-07-08 20:01:09 -0700987static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800988{
989 int ret;
990
991 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800992 if (ret)
993 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800994 return ret;
995}
996
David Hendricks91040832011-07-08 20:01:09 -0700997static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800998{
999 struct w25q_range *w25q_ranges;
1000 int i, num_entries;
1001
1002 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1003 for (i = 0; i < num_entries; i++) {
1004 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1005 w25q_ranges[i].range.start,
1006 w25q_ranges[i].range.len);
1007 }
1008
1009 return 0;
1010}
1011
David Hendricks1c09f802012-10-03 11:03:48 -07001012/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1013uint8_t w25q_read_status_register_2(void)
1014{
1015 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1016 unsigned char readarr[2];
1017 int ret;
1018
1019 /* Read Status Register */
1020 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1021 if (ret) {
1022 /*
1023 * FIXME: make this a benign failure for now in case we are
1024 * unable to execute the opcode
1025 */
1026 msg_cdbg("RDSR2 failed!\n");
1027 readarr[0] = 0x00;
1028 }
1029
1030 return readarr[0];
1031}
1032
1033static int w25q_wp_status(const struct flashchip *flash)
1034{
1035 struct w25q_status sr1;
1036 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001037 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001038 unsigned int start, len;
1039 int ret = 0;
1040
1041 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001042 tmp[0] = spi_read_status_register();
1043 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001044
David Hendricksf1bd8802012-10-30 11:37:57 -07001045 memset(&sr2, 0, sizeof(sr2));
1046 tmp[1] = w25q_read_status_register_2();
1047 memcpy(&sr2, &tmp[1], 1);
1048
1049 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001050 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1051 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1052 msg_cinfo("WP: write protect is %s.\n",
1053 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1054
1055 msg_cinfo("WP: write protect range: ");
1056 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1057 msg_cinfo("(cannot resolve the range)\n");
1058 ret = -1;
1059 } else {
1060 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1061 }
1062
1063 return ret;
1064}
1065
1066/*
1067 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1068 * de-asserted after the first byte, then it acts like a JEDEC-standard
1069 * WRSR command. if /CS is asserted, then the next data byte is written
1070 * into status register 2.
1071 */
1072#define W25Q_WRSR_OUTSIZE 0x03
1073static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1074{
1075 int result;
1076 struct spi_command cmds[] = {
1077 {
1078 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1079 .writecnt = JEDEC_WREN_OUTSIZE,
1080 .writearr = (const unsigned char[]){ JEDEC_WREN },
1081 .readcnt = 0,
1082 .readarr = NULL,
1083 }, {
1084 .writecnt = W25Q_WRSR_OUTSIZE,
1085 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1086 .readcnt = 0,
1087 .readarr = NULL,
1088 }, {
1089 .writecnt = 0,
1090 .writearr = NULL,
1091 .readcnt = 0,
1092 .readarr = NULL,
1093 }};
1094
1095 result = spi_send_multicommand(cmds);
1096 if (result) {
1097 msg_cerr("%s failed during command execution\n",
1098 __func__);
1099 }
1100
1101 /* WRSR performs a self-timed erase before the changes take effect. */
1102 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1103
1104 return result;
1105}
1106
1107/*
1108 * Set/clear the SRP1 bit in status register 2.
1109 * FIXME: make this more generic if other chips use the same SR2 layout
1110 */
1111static int w25q_set_srp1(const struct flashchip *flash, int enable)
1112{
1113 struct w25q_status sr1;
1114 struct w25q_status_2 sr2;
1115 uint8_t tmp, expected;
1116
1117 tmp = spi_read_status_register();
1118 memcpy(&sr1, &tmp, 1);
1119 tmp = w25q_read_status_register_2();
1120 memcpy(&sr2, &tmp, 1);
1121
1122 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1123
1124 sr2.srp1 = enable ? 1 : 0;
1125
1126 memcpy(&expected, &sr2, 1);
1127 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1128
1129 tmp = w25q_read_status_register_2();
1130 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1131 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1132 return 1;
1133
1134 return 0;
1135}
1136
1137enum wp_mode get_wp_mode(const char *mode_str)
1138{
1139 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1140
1141 if (!strcasecmp(mode_str, "hardware"))
1142 wp_mode = WP_MODE_HARDWARE;
1143 else if (!strcasecmp(mode_str, "power_cycle"))
1144 wp_mode = WP_MODE_POWER_CYCLE;
1145 else if (!strcasecmp(mode_str, "permanent"))
1146 wp_mode = WP_MODE_PERMANENT;
1147
1148 return wp_mode;
1149}
1150
1151static int w25q_disable_writeprotect(const struct flashchip *flash,
1152 enum wp_mode wp_mode)
1153{
1154 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001155 struct w25q_status_2 sr2;
1156 uint8_t tmp;
1157
1158 switch (wp_mode) {
1159 case WP_MODE_HARDWARE:
1160 ret = w25_set_srp0(flash, 0);
1161 break;
1162 case WP_MODE_POWER_CYCLE:
1163 tmp = w25q_read_status_register_2();
1164 memcpy(&sr2, &tmp, 1);
1165 if (sr2.srp1) {
1166 msg_cerr("%s(): must disconnect power to disable "
1167 "write-protection\n", __func__);
1168 } else {
1169 ret = 0;
1170 }
1171 break;
1172 case WP_MODE_PERMANENT:
1173 msg_cerr("%s(): cannot disable permanent write-protection\n",
1174 __func__);
1175 break;
1176 default:
1177 msg_cerr("%s(): invalid mode specified\n", __func__);
1178 break;
1179 }
1180
1181 if (ret)
1182 msg_cerr("%s(): error=%d.\n", __func__, ret);
1183 return ret;
1184}
1185
1186static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1187{
1188 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1189}
1190
1191static int w25q_enable_writeprotect(const struct flashchip *flash,
1192 enum wp_mode wp_mode)
1193{
1194 int ret = 1;
1195 struct w25q_status sr1;
1196 struct w25q_status_2 sr2;
1197 uint8_t tmp;
1198
1199 switch (wp_mode) {
1200 case WP_MODE_HARDWARE:
1201 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1202 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1203 __func__);
1204 break;
1205 }
1206
1207 tmp = spi_read_status_register();
1208 memcpy(&sr1, &tmp, 1);
1209 if (sr1.srp0)
1210 ret = 0;
1211 else
1212 ret = w25_set_srp0(flash, 1);
1213
1214 break;
1215 case WP_MODE_POWER_CYCLE:
1216 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1217 msg_cerr("%s(): cannot disable hardware WP mode\n",
1218 __func__);
1219 break;
1220 }
1221
1222 tmp = w25q_read_status_register_2();
1223 memcpy(&sr2, &tmp, 1);
1224 if (sr2.srp1)
1225 ret = 0;
1226 else
1227 ret = w25q_set_srp1(flash, 1);
1228
1229 break;
1230 case WP_MODE_PERMANENT:
1231 tmp = spi_read_status_register();
1232 memcpy(&sr1, &tmp, 1);
1233 if (sr1.srp0 == 0) {
1234 ret = w25_set_srp0(flash, 1);
1235 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001236 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001237 "permanent WP\n", __func__);
1238 break;
1239 }
1240 }
1241
1242 tmp = w25q_read_status_register_2();
1243 memcpy(&sr2, &tmp, 1);
1244 if (sr2.srp1 == 0) {
1245 ret = w25q_set_srp1(flash, 1);
1246 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001247 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001248 "permanent WP\n", __func__);
1249 break;
1250 }
1251 }
1252
1253 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001254 default:
1255 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1256 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001257 }
1258
1259 if (ret)
1260 msg_cerr("%s(): error=%d.\n", __func__, ret);
1261 return ret;
1262}
1263
1264/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001265struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001266 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001267 .set_range = w25_set_range,
1268 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001269 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001270 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001271
1272};
1273
1274/* W25Q series has features such as a second status register and SFDP */
1275struct wp wp_w25q = {
1276 .list_ranges = w25_list_ranges,
1277 .set_range = w25_set_range,
1278 .enable = w25q_enable_writeprotect,
1279 /*
1280 * By default, disable hardware write-protection. We may change
1281 * this later if we want to add fine-grained write-protect disable
1282 * as a command-line option.
1283 */
1284 .disable = w25q_disable_writeprotect_default,
1285 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001286};
David Hendrickse0512a72014-07-15 20:30:47 -07001287
David Hendricksaf3944a2014-07-28 18:37:40 -07001288struct generic_range gd25q32_cmp0_ranges[] = {
1289 /* none, bp4 and bp3 => don't care */
1290 { 0x00, {0, 0} },
1291 { 0x08, {0, 0} },
1292 { 0x10, {0, 0} },
1293 { 0x18, {0, 0} },
1294
1295 { 0x01, {0x3f0000, 64 * 1024} },
1296 { 0x02, {0x3e0000, 128 * 1024} },
1297 { 0x03, {0x3c0000, 256 * 1024} },
1298 { 0x04, {0x380000, 512 * 1024} },
1299 { 0x05, {0x300000, 1024 * 1024} },
1300 { 0x06, {0x200000, 2048 * 1024} },
1301
1302 { 0x09, {0x000000, 64 * 1024} },
1303 { 0x0a, {0x000000, 128 * 1024} },
1304 { 0x0b, {0x000000, 256 * 1024} },
1305 { 0x0c, {0x000000, 512 * 1024} },
1306 { 0x0d, {0x000000, 1024 * 1024} },
1307 { 0x0e, {0x000000, 2048 * 1024} },
1308
1309 /* all, bp4 and bp3 => don't care */
1310 { 0x07, {0x000000, 4096 * 1024} },
1311 { 0x0f, {0x000000, 4096 * 1024} },
1312 { 0x17, {0x000000, 4096 * 1024} },
1313 { 0x1f, {0x000000, 4096 * 1024} },
1314
1315 { 0x11, {0x3ff000, 4 * 1024} },
1316 { 0x12, {0x3fe000, 8 * 1024} },
1317 { 0x13, {0x3fc000, 16 * 1024} },
1318 { 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1319 { 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1320 { 0x16, {0x3f8000, 32 * 1024} },
1321
1322 { 0x19, {0x000000, 4 * 1024} },
1323 { 0x1a, {0x000000, 8 * 1024} },
1324 { 0x1b, {0x000000, 16 * 1024} },
1325 { 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1326 { 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1327 { 0x1e, {0x000000, 32 * 1024} },
1328};
1329
1330struct generic_range gd25q32_cmp1_ranges[] = {
1331 /* none, bp4 and bp3 => don't care */
1332 { 0x00, {0, 0} },
1333 { 0x08, {0, 0} },
1334 { 0x10, {0, 0} },
1335 { 0x18, {0, 0} },
1336
1337 { 0x01, {0x000000, 4032 * 1024} },
1338 { 0x02, {0x000000, 3968 * 1024} },
1339 { 0x03, {0x000000, 3840 * 1024} },
1340 { 0x04, {0x000000, 3584 * 1024} },
1341 { 0x05, {0x000000, 3 * 1024 * 1024} },
1342 { 0x06, {0x000000, 2 * 1024 * 1024} },
1343
1344 { 0x09, {0x010000, 4032 * 1024} },
1345 { 0x0a, {0x020000, 3968 * 1024} },
1346 { 0x0b, {0x040000, 3840 * 1024} },
1347 { 0x0c, {0x080000, 3584 * 1024} },
1348 { 0x0d, {0x100000, 3 * 1024 * 1024} },
1349 { 0x0e, {0x200000, 2 * 1024 * 1024} },
1350
1351 /* all, bp4 and bp3 => don't care */
1352 { 0x07, {0x000000, 4096 * 1024} },
1353 { 0x0f, {0x000000, 4096 * 1024} },
1354 { 0x17, {0x000000, 4096 * 1024} },
1355 { 0x1f, {0x000000, 4096 * 1024} },
1356
1357 { 0x11, {0x000000, 4092 * 1024} },
1358 { 0x12, {0x000000, 4088 * 1024} },
1359 { 0x13, {0x000000, 4080 * 1024} },
1360 { 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1361 { 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1362 { 0x16, {0x000000, 4064 * 1024} },
1363
1364 { 0x19, {0x001000, 4092 * 1024} },
1365 { 0x1a, {0x002000, 4088 * 1024} },
1366 { 0x1b, {0x040000, 4080 * 1024} },
1367 { 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1368 { 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1369 { 0x1e, {0x080000, 4064 * 1024} },
1370};
1371
1372static struct generic_wp gd25q32_wp = {
1373 /* TODO: map second status register */
1374 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1375};
1376
David Hendricks83541d32014-07-15 20:58:21 -07001377#if 0
1378/* FIXME: MX25L6405D has same ID as MX25L6406 */
1379static struct w25q_range mx25l6405d_ranges[] = {
1380 { X, 0, 0, {0, 0} }, /* none */
1381 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1382 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1383 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1384 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1385 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1386 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1387 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1388
1389 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1390 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1391 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1392 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1393 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1394 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1395 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1396 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1397};
1398#endif
1399
1400/* FIXME: MX25L6406 has same ID as MX25L6405D */
1401struct generic_range mx25l6406e_ranges[] = {
1402 { 0, {0, 0} }, /* none */
1403 { 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1404 { 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1405 { 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1406 { 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1407 { 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1408 { 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1409
1410 { 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1411 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1412 { 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1413 { 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1414 { 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1415 { 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1416 { 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1417 { 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1418 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1419};
1420
1421static struct generic_wp mx25l6406e_wp = {
1422 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1423 .ranges = &mx25l6406e_ranges[0],
1424};
David Hendrickse0512a72014-07-15 20:30:47 -07001425
1426/* Given a flash chip, this function returns its writeprotect info. */
1427static int generic_range_table(const struct flashchip *flash,
1428 struct generic_wp **wp,
1429 int *num_entries)
1430{
1431 *wp = NULL;
1432 *num_entries = 0;
1433
1434 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001435 case GIGADEVICE_ID:
1436 switch(flash->model_id) {
1437 case GIGADEVICE_GD25Q32: {
1438 uint8_t sr1 = w25q_read_status_register_2();
1439
1440 *wp = &gd25q32_wp;
1441 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1442 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1443 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1444 } else { /* CMP == 1 */
1445 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1446 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1447 }
1448
1449 break;
1450 /* TODO(shawnn): add support for other GD parts */
1451 }
1452 default:
1453 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1454 " (0x%04x), aborting\n", __func__, __LINE__,
1455 flash->model_id);
1456 return -1;
1457 }
1458 break;
David Hendricks83541d32014-07-15 20:58:21 -07001459 case MACRONIX_ID:
1460 switch (flash->model_id) {
1461 case MACRONIX_MX25L6405:
1462 /* FIXME: MX25L64* chips have mixed capabilities and
1463 share IDs */
1464 *wp = &mx25l6406e_wp;
1465 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1466 break;
1467 default:
1468 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1469 ", aborting\n", __func__, __LINE__,
1470 flash->model_id);
1471 return -1;
1472 }
1473 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001474 default:
1475 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1476 __func__, flash->manufacture_id);
1477 return -1;
1478 }
1479
1480 return 0;
1481}
1482
1483/* Given a [start, len], this function finds a block protect bit combination
1484 * (if possible) and sets the corresponding bits in "status". Remaining bits
1485 * are preserved. */
1486static int generic_range_to_status(const struct flashchip *flash,
1487 unsigned int start, unsigned int len,
1488 uint8_t *status)
1489{
1490 struct generic_wp *wp;
1491 struct generic_range *r;
1492 int i, range_found = 0, num_entries;
1493 uint8_t bp_mask;
1494
1495 if (generic_range_table(flash, &wp, &num_entries))
1496 return -1;
1497
1498 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1499 ((1 << wp->sr1.bp0_pos) - 1);
1500
1501 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1502 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1503 start, len, r->range.start, r->range.len);
1504 if ((start == r->range.start) && (len == r->range.len)) {
1505 *status &= ~(bp_mask);
1506 *status |= r->bp << (wp->sr1.bp0_pos);
1507 range_found = 1;
1508 break;
1509 }
1510 }
1511
1512 if (!range_found) {
1513 msg_cerr("matching range not found\n");
1514 return -1;
1515 }
1516 return 0;
1517}
1518
1519static int generic_status_to_range(const struct flashchip *flash,
1520 const uint8_t sr1, unsigned int *start, unsigned int *len)
1521{
1522 struct generic_wp *wp;
1523 struct generic_range *r;
1524 int num_entries, wp_en, i, status_found = 0;
1525 uint8_t sr1_bp;
1526
1527 if (generic_range_table(flash, &wp, &num_entries))
1528 return -1;
1529
1530 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1531
1532 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1533 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1534 if (sr1_bp == r->bp) {
1535 *start = r->range.start;
1536 *len = r->range.len;
1537 status_found = 1;
1538 break;
1539 }
1540 }
1541
1542 if (!status_found) {
1543 msg_cerr("matching status not found\n");
1544 return -1;
1545 }
1546 return 0;
1547}
1548
1549/* Given a [start, len], this function calls generic_range_to_status() to
1550 * convert it to flash-chip-specific range bits, then sets into status register.
1551 */
1552static int generic_set_range(const struct flashchip *flash,
1553 unsigned int start, unsigned int len)
1554{
1555 uint8_t status, expected;
1556
1557 status = spi_read_status_register();
1558 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1559
1560 expected = status; /* preserve non-bp bits */
1561 if (generic_range_to_status(flash, start, len, &expected))
1562 return -1;
1563
1564 spi_write_status_register_WREN(expected);
1565
1566 status = spi_read_status_register();
1567 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1568 if (status != expected) {
1569 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1570 expected, status);
1571 return 1;
1572 }
1573
1574 return 0;
1575}
1576
1577/* Set/clear the status regsiter write protect bit in SR1. */
1578static int generic_set_srp0(const struct flashchip *flash, int enable)
1579{
1580 uint8_t status, expected;
1581 struct generic_wp *wp;
1582 int num_entries;
1583
1584 if (generic_range_table(flash, &wp, &num_entries))
1585 return -1;
1586
1587 expected = spi_read_status_register();
1588 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1589
1590 if (enable)
1591 expected |= 1 << wp->sr1.srp_pos;
1592 else
1593 expected &= ~(1 << wp->sr1.srp_pos);
1594
1595 spi_write_status_register_WREN(expected);
1596
1597 status = spi_read_status_register();
1598 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1599 if (status != expected)
1600 return -1;
1601
1602 return 0;
1603}
1604
1605static int generic_enable_writeprotect(const struct flashchip *flash,
1606 enum wp_mode wp_mode)
1607{
1608 int ret;
1609
1610 switch (wp_mode) {
1611 case WP_MODE_HARDWARE:
1612 ret = generic_set_srp0(flash, 1);
1613 break;
1614 default:
1615 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1616 return 1;
1617 }
1618
1619 if (ret)
1620 msg_cerr("%s(): error=%d.\n", __func__, ret);
1621 return ret;
1622}
1623
1624static int generic_disable_writeprotect(const struct flashchip *flash)
1625{
1626 int ret;
1627
1628 ret = generic_set_srp0(flash, 0);
1629 if (ret)
1630 msg_cerr("%s(): error=%d.\n", __func__, ret);
1631 return ret;
1632}
1633
1634static int generic_list_ranges(const struct flashchip *flash)
1635{
1636 struct generic_wp *wp;
1637 struct generic_range *r;
1638 int i, num_entries;
1639
1640 if (generic_range_table(flash, &wp, &num_entries))
1641 return -1;
1642
1643 r = &wp->ranges[0];
1644 for (i = 0; i < num_entries; i++) {
1645 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1646 r->range.start, r->range.len);
1647 r++;
1648 }
1649
1650 return 0;
1651}
1652
1653static int generic_wp_status(const struct flashchip *flash)
1654{
1655 uint8_t sr1;
1656 unsigned int start, len;
1657 int ret = 0;
1658 struct generic_wp *wp;
1659 struct generic_range *g;
1660 int num_entries, wp_en;
1661
1662 if (generic_range_table(flash, &wp, &num_entries))
1663 return -1;
1664
1665 sr1 = spi_read_status_register();
1666 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
1667
1668 msg_cinfo("WP: status: 0x%04x\n", sr1);
1669 msg_cinfo("WP: status.srp0: %x\n", wp_en);
1670 /* FIXME: SRP1 is not really generic, but we probably should print
1671 * it anyway to have consistent output. #legacycruft */
1672 msg_cinfo("WP: status.srp1: %x\n", 0);
1673 msg_cinfo("WP: write protect is %s.\n",
1674 wp_en ? "enabled" : "disabled");
1675
1676 msg_cinfo("WP: write protect range: ");
1677 if (generic_status_to_range(flash, sr1, &start, &len)) {
1678 msg_cinfo("(cannot resolve the range)\n");
1679 ret = -1;
1680 } else {
1681 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1682 }
1683
1684 return ret;
1685}
1686
1687struct wp wp_generic = {
1688 .list_ranges = generic_list_ranges,
1689 .set_range = generic_set_range,
1690 .enable = generic_enable_writeprotect,
1691 .disable = generic_disable_writeprotect,
1692 .wp_status = generic_wp_status,
1693};