blob: 1d20bed815444dcb1c321f97466df04d0dc97810 [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
David Hendricks1c09f802012-10-03 11:03:48 -070030/*
David Hendricksf7924d12010-06-10 21:26:44 -070031 * The following procedures rely on look-up tables to match the user-specified
32 * range with the chip's supported ranges. This turned out to be the most
33 * elegant approach since diferent flash chips use different levels of
34 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070035 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070036 */
37
38struct wp_range {
39 unsigned int start; /* starting address */
40 unsigned int len; /* len */
41};
42
43enum bit_state {
44 OFF = 0,
45 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080046 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070047};
48
David Hendrickse0512a72014-07-15 20:30:47 -070049/*
50 * Generic write-protection schema for 25-series SPI flash chips. This assumes
51 * there is a status register that contains one or more consecutive bits which
52 * determine which address range is protected.
53 */
54
55struct status_register_layout {
56 int bp0_pos; /* position of BP0 */
57 int bp_bits; /* number of block protect bits */
58 int srp_pos; /* position of status register protect enable bit */
59};
60
61struct generic_range {
David Hendricks148a4bf2015-03-13 21:02:42 -070062 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070063 unsigned int bp; /* block protect bitfield */
64 struct wp_range range;
65};
66
67struct generic_wp {
68 struct status_register_layout sr1; /* status register 1 */
69 struct generic_range *ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -070070
71 /*
72 * Some chips store modifier bits in one or more special control
73 * registers instead of the status register like many older SPI NOR
74 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
75 * any chip-specific operations necessary to get/set these bit values.
76 */
77 int (*get_modifier_bits)(const struct flashchip *flash,
78 struct generic_modifier_bits *m);
79 int (*set_modifier_bits)(const struct flashchip *flash,
80 struct generic_modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070081};
82
83/*
84 * The following ranges and functions are useful for representing Winbond-
85 * style writeprotect schema in which there are typically 5 bits of
86 * relevant information stored in status register 1:
87 * sec: This bit indicates the units (sectors vs. blocks)
88 * tb: The top-bottom bit indicates if the affected range is at the top of
89 * the flash memory's address space or at the bottom.
90 * bp[2:0]: The number of affected sectors/blocks.
91 */
David Hendricksf7924d12010-06-10 21:26:44 -070092struct w25q_range {
93 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
94 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080095 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070096 struct wp_range range;
97};
98
David Hendrickse0512a72014-07-15 20:30:47 -070099/*
100 * Mask to extract write-protect enable and range bits
101 * Status register 1:
102 * SRP0: bit 7
103 * range(BP2-BP0): bit 4-2
104 * Status register 2:
105 * SRP1: bit 1
106 */
107#define MASK_WP_AREA (0x9C)
108#define MASK_WP2_AREA (0x01)
109
David Hendricks57566ed2010-08-16 18:24:45 -0700110struct w25q_range en25f40_ranges[] = {
111 { X, X, 0, {0, 0} }, /* none */
112 { 0, 0, 0x1, {0x000000, 504 * 1024} },
113 { 0, 0, 0x2, {0x000000, 496 * 1024} },
114 { 0, 0, 0x3, {0x000000, 480 * 1024} },
115 { 0, 0, 0x4, {0x000000, 448 * 1024} },
116 { 0, 0, 0x5, {0x000000, 384 * 1024} },
117 { 0, 0, 0x6, {0x000000, 256 * 1024} },
118 { 0, 0, 0x7, {0x000000, 512 * 1024} },
119};
120
David Hendrickse185bf22011-05-24 15:34:18 -0700121struct w25q_range en25q40_ranges[] = {
122 { 0, 0, 0, {0, 0} }, /* none */
123 { 0, 0, 0x1, {0x000000, 504 * 1024} },
124 { 0, 0, 0x2, {0x000000, 496 * 1024} },
125 { 0, 0, 0x3, {0x000000, 480 * 1024} },
126
127 { 0, 1, 0x0, {0x000000, 448 * 1024} },
128 { 0, 1, 0x1, {0x000000, 384 * 1024} },
129 { 0, 1, 0x2, {0x000000, 256 * 1024} },
130 { 0, 1, 0x3, {0x000000, 512 * 1024} },
131};
132
133struct w25q_range en25q80_ranges[] = {
134 { 0, 0, 0, {0, 0} }, /* none */
135 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
136 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
137 { 0, 0, 0x3, {0x000000, 992 * 1024} },
138 { 0, 0, 0x4, {0x000000, 960 * 1024} },
139 { 0, 0, 0x5, {0x000000, 896 * 1024} },
140 { 0, 0, 0x6, {0x000000, 768 * 1024} },
141 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
142};
143
144struct w25q_range en25q32_ranges[] = {
145 { 0, 0, 0, {0, 0} }, /* none */
146 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
147 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
148 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
149 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
150 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
151 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
152 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
153
154 { 0, 1, 0, {0, 0} }, /* none */
155 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
156 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
157 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
158 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
159 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
160 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
161 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
162};
163
164struct w25q_range en25q64_ranges[] = {
165 { 0, 0, 0, {0, 0} }, /* none */
166 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
167 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
168 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
169 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
170 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
171 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
172 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
173
174 { 0, 1, 0, {0, 0} }, /* none */
175 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
176 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
177 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
178 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
179 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
180 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
181 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
182};
183
184struct w25q_range en25q128_ranges[] = {
185 { 0, 0, 0, {0, 0} }, /* none */
186 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
187 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
188 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
189 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
190 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
191 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
192 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
193
194 { 0, 1, 0, {0, 0} }, /* none */
195 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
196 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
197 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
198 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
199 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
200 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
201 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
202};
203
Marc Jonesb2f90022014-04-29 17:37:23 -0600204struct w25q_range en25s64_ranges[] = {
205 { 0, 0, 0, {0, 0} }, /* none */
206 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
207 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
208 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
209 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
210 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
211 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
212 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
213
214 { 0, 1, 0, {0, 0} }, /* none */
215 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
216 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
217 { 0, 1, 0x3, {0x780000, 512 * 1024} },
218 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
219 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
220 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
221 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
222};
223
David Hendricksf8f00c72011-02-01 12:39:46 -0800224/* mx25l1005 ranges also work for the mx25l1005c */
225static struct w25q_range mx25l1005_ranges[] = {
226 { X, X, 0, {0, 0} }, /* none */
227 { X, X, 0x1, {0x010000, 64 * 1024} },
228 { X, X, 0x2, {0x000000, 128 * 1024} },
229 { X, X, 0x3, {0x000000, 128 * 1024} },
230};
231
232static struct w25q_range mx25l2005_ranges[] = {
233 { X, X, 0, {0, 0} }, /* none */
234 { X, X, 0x1, {0x030000, 64 * 1024} },
235 { X, X, 0x2, {0x020000, 128 * 1024} },
236 { X, X, 0x3, {0x000000, 256 * 1024} },
237};
238
239static struct w25q_range mx25l4005_ranges[] = {
240 { X, X, 0, {0, 0} }, /* none */
241 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
242 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
243 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
244 { X, X, 0x4, {0x000000, 512 * 1024} },
245 { X, X, 0x5, {0x000000, 512 * 1024} },
246 { X, X, 0x6, {0x000000, 512 * 1024} },
247 { X, X, 0x7, {0x000000, 512 * 1024} },
248};
249
250static struct w25q_range mx25l8005_ranges[] = {
251 { X, X, 0, {0, 0} }, /* none */
252 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
253 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
254 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
255 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
256 { X, X, 0x5, {0x000000, 1024 * 1024} },
257 { X, X, 0x6, {0x000000, 1024 * 1024} },
258 { X, X, 0x7, {0x000000, 1024 * 1024} },
259};
260
261#if 0
262/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
263static struct w25q_range mx25l1605_ranges[] = {
264 { X, X, 0, {0, 0} }, /* none */
265 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
266 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
267 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
268 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
269 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
270 { X, X, 0x6, {0x000000, 2048 * 1024} },
271 { X, X, 0x7, {0x000000, 2048 * 1024} },
272};
273#endif
274
275#if 0
276/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
277static struct w25q_range mx25l6405_ranges[] = {
278 { X, 0, 0, {0, 0} }, /* none */
279 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
280 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
281 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
282 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
283 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
284 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
285 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
286
287 { X, 1, 0x0, {0x000000, 8192 * 1024} },
288 { X, 1, 0x1, {0x000000, 8192 * 1024} },
289 { X, 1, 0x2, {0x000000, 8192 * 1024} },
290 { X, 1, 0x3, {0x000000, 8192 * 1024} },
291 { X, 1, 0x4, {0x000000, 8192 * 1024} },
292 { X, 1, 0x5, {0x000000, 8192 * 1024} },
293 { X, 1, 0x6, {0x000000, 8192 * 1024} },
294 { X, 1, 0x7, {0x000000, 8192 * 1024} },
295};
296#endif
297
298static struct w25q_range mx25l1605d_ranges[] = {
299 { X, 0, 0, {0, 0} }, /* none */
300 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
301 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
302 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
303 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
304 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
305 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
306 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
307
308 { X, 1, 0x0, {0x000000, 2048 * 1024} },
309 { X, 1, 0x1, {0x000000, 2048 * 1024} },
310 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
311 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
312 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
313 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
314 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
315 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
316};
317
318/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700319static struct w25q_range mx25l3205d_ranges[] = {
320 { X, 0, 0, {0, 0} }, /* none */
321 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
322 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
323 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
324 { X, 0, 0x4, {0x380000, 512 * 1024} },
325 { X, 0, 0x5, {0x300000, 1024 * 1024} },
326 { X, 0, 0x6, {0x200000, 2048 * 1024} },
327 { X, 0, 0x7, {0x000000, 4096 * 1024} },
328
329 { X, 1, 0x0, {0x000000, 4096 * 1024} },
330 { X, 1, 0x1, {0x000000, 2048 * 1024} },
331 { X, 1, 0x2, {0x000000, 3072 * 1024} },
332 { X, 1, 0x3, {0x000000, 3584 * 1024} },
333 { X, 1, 0x4, {0x000000, 3840 * 1024} },
334 { X, 1, 0x5, {0x000000, 3968 * 1024} },
335 { X, 1, 0x6, {0x000000, 4032 * 1024} },
336 { X, 1, 0x7, {0x000000, 4096 * 1024} },
337};
338
Vincent Palatin87e092a2013-02-28 15:46:14 -0800339static struct w25q_range mx25u3235e_ranges[] = {
340 { X, 0, 0, {0, 0} }, /* none */
341 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
342 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
343 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
344 { 0, 0, 0x4, {0x380000, 512 * 1024} },
345 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
346 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
347 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
348
349 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
350 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
351 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
352 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
353 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
354 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
355 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
356 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
357};
358
Jongpil66a96492014-08-14 17:59:06 +0900359static struct w25q_range mx25u6435e_ranges[] = {
360 { X, 0, 0, {0, 0} }, /* none */
361 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
362 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
363 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
364 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
365 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
366 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
367 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
368
369 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
370 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
371 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
372 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
373 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
374 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
375 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
376 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
377};
378
David Hendricksbfa624b2012-07-24 12:47:59 -0700379static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700380 /*
381 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
382 * (block protect bit 3). It is only useful when all blocks are to
383 * be write-protected.
384 */
David Hendricks42a549a2015-04-22 11:25:07 -0700385 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700386
387 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
388 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
389 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
390 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
391 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
392 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
393 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
394
David Hendricksfe9123b2015-04-21 13:18:31 -0700395 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
396 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
397 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
398 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
399 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
400 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
401 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700402
403 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
404 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
405 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
406 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
407 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
408 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
409 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
410 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
411};
412
David Hendricksf7924d12010-06-10 21:26:44 -0700413static struct w25q_range w25q16_ranges[] = {
414 { X, X, 0, {0, 0} }, /* none */
415 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
416 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
417 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
418 { 0, 0, 0x4, {0x180000, 512 * 1024} },
419 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
420
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 { X, X, 0x6, {0x000000, 2048 * 1024} },
427 { X, X, 0x7, {0x000000, 2048 * 1024} },
428
429 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
430 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
431 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
432 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
433 { 1, 0, 0x5, {0x1f8000, 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 w25q32_ranges[] = {
443 { X, X, 0, {0, 0} }, /* none */
444 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
445 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
446 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
447 { 0, 0, 0x4, {0x380000, 512 * 1024} },
448 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700449 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700450
451 { 0, 1, 0x1, {0x000000, 64 * 1024} },
452 { 0, 1, 0x2, {0x000000, 128 * 1024} },
453 { 0, 1, 0x3, {0x000000, 256 * 1024} },
454 { 0, 1, 0x4, {0x000000, 512 * 1024} },
455 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
456 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
457 { X, X, 0x7, {0x000000, 4096 * 1024} },
458
459 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
460 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
461 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
462 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
463 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
464
465 { 1, 1, 0x1, {0x000000, 4 * 1024} },
466 { 1, 1, 0x2, {0x000000, 8 * 1024} },
467 { 1, 1, 0x3, {0x000000, 16 * 1024} },
468 { 1, 1, 0x4, {0x000000, 32 * 1024} },
469 { 1, 1, 0x5, {0x000000, 32 * 1024} },
470};
471
472static struct w25q_range w25q80_ranges[] = {
473 { X, X, 0, {0, 0} }, /* none */
474 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
475 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
476 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
477 { 0, 0, 0x4, {0x080000, 512 * 1024} },
478
479 { 0, 1, 0x1, {0x000000, 64 * 1024} },
480 { 0, 1, 0x2, {0x000000, 128 * 1024} },
481 { 0, 1, 0x3, {0x000000, 256 * 1024} },
482 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700483 { X, X, 0x6, {0x000000, 1024 * 1024} },
484 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700485
486 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
487 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
488 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
489 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
490 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
491
492 { 1, 1, 0x1, {0x000000, 4 * 1024} },
493 { 1, 1, 0x2, {0x000000, 8 * 1024} },
494 { 1, 1, 0x3, {0x000000, 16 * 1024} },
495 { 1, 1, 0x4, {0x000000, 32 * 1024} },
496 { 1, 1, 0x5, {0x000000, 32 * 1024} },
497};
498
David Hendricks2c4a76c2010-06-28 14:00:43 -0700499static struct w25q_range w25q64_ranges[] = {
500 { X, X, 0, {0, 0} }, /* none */
501
502 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
503 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
504 { 0, 0, 0x3, {0x780000, 512 * 1024} },
505 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
506 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
507 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
508
509 { 0, 1, 0x1, {0x000000, 128 * 1024} },
510 { 0, 1, 0x2, {0x000000, 256 * 1024} },
511 { 0, 1, 0x3, {0x000000, 512 * 1024} },
512 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
513 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
514 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
515 { X, X, 0x7, {0x000000, 8192 * 1024} },
516
517 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
518 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
519 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
520 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
521 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
522
523 { 1, 1, 0x1, {0x000000, 4 * 1024} },
524 { 1, 1, 0x2, {0x000000, 8 * 1024} },
525 { 1, 1, 0x3, {0x000000, 16 * 1024} },
526 { 1, 1, 0x4, {0x000000, 32 * 1024} },
527 { 1, 1, 0x5, {0x000000, 32 * 1024} },
528};
529
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800530struct w25q_range w25x10_ranges[] = {
531 { X, X, 0, {0, 0} }, /* none */
532 { 0, 0, 0x1, {0x010000, 64 * 1024} },
533 { 0, 1, 0x1, {0x000000, 64 * 1024} },
534 { X, X, 0x2, {0x000000, 128 * 1024} },
535 { X, X, 0x3, {0x000000, 128 * 1024} },
536};
537
538struct w25q_range w25x20_ranges[] = {
539 { X, X, 0, {0, 0} }, /* none */
540 { 0, 0, 0x1, {0x030000, 64 * 1024} },
541 { 0, 0, 0x2, {0x020000, 128 * 1024} },
542 { 0, 1, 0x1, {0x000000, 64 * 1024} },
543 { 0, 1, 0x2, {0x000000, 128 * 1024} },
544 { 0, X, 0x3, {0x000000, 256 * 1024} },
545};
546
David Hendricks470ca952010-08-13 14:01:53 -0700547struct w25q_range w25x40_ranges[] = {
548 { X, X, 0, {0, 0} }, /* none */
549 { 0, 0, 0x1, {0x070000, 64 * 1024} },
550 { 0, 0, 0x2, {0x060000, 128 * 1024} },
551 { 0, 0, 0x3, {0x040000, 256 * 1024} },
552 { 0, 1, 0x1, {0x000000, 64 * 1024} },
553 { 0, 1, 0x2, {0x000000, 128 * 1024} },
554 { 0, 1, 0x3, {0x000000, 256 * 1024} },
555 { 0, X, 0x4, {0x000000, 512 * 1024} },
556};
557
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800558struct w25q_range w25x80_ranges[] = {
559 { X, X, 0, {0, 0} }, /* none */
560 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
561 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
562 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
563 { 0, 0, 0x4, {0x080000, 512 * 1024} },
564 { 0, 1, 0x1, {0x000000, 64 * 1024} },
565 { 0, 1, 0x2, {0x000000, 128 * 1024} },
566 { 0, 1, 0x3, {0x000000, 256 * 1024} },
567 { 0, 1, 0x4, {0x000000, 512 * 1024} },
568 { 0, X, 0x5, {0x000000, 1024 * 1024} },
569 { 0, X, 0x6, {0x000000, 1024 * 1024} },
570 { 0, X, 0x7, {0x000000, 1024 * 1024} },
571};
572
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700573static struct w25q_range gd25q64_ranges[] = {
574 { X, X, 0, {0, 0} }, /* none */
575 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
576 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
577 { 0, 0, 0x3, {0x780000, 512 * 1024} },
578 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
579 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
580 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
581
582 { 0, 1, 0x1, {0x000000, 128 * 1024} },
583 { 0, 1, 0x2, {0x000000, 256 * 1024} },
584 { 0, 1, 0x3, {0x000000, 512 * 1024} },
585 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
586 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
587 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
588 { X, X, 0x7, {0x000000, 8192 * 1024} },
589
590 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
591 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
592 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
593 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
594 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
595 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
596
597 { 1, 1, 0x1, {0x000000, 4 * 1024} },
598 { 1, 1, 0x2, {0x000000, 8 * 1024} },
599 { 1, 1, 0x3, {0x000000, 16 * 1024} },
600 { 1, 1, 0x4, {0x000000, 32 * 1024} },
601 { 1, 1, 0x5, {0x000000, 32 * 1024} },
602 { 1, 1, 0x6, {0x000000, 32 * 1024} },
603};
604
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800605static struct w25q_range a25l040_ranges[] = {
606 { X, X, 0x0, {0, 0} }, /* none */
607 { X, X, 0x1, {0x70000, 64 * 1024} },
608 { X, X, 0x2, {0x60000, 128 * 1024} },
609 { X, X, 0x3, {0x40000, 256 * 1024} },
610 { X, X, 0x4, {0x00000, 512 * 1024} },
611 { X, X, 0x5, {0x00000, 512 * 1024} },
612 { X, X, 0x6, {0x00000, 512 * 1024} },
613 { X, X, 0x7, {0x00000, 512 * 1024} },
614};
615
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800616/* Given a flash chip, this function returns its range table. */
617static int w25_range_table(const struct flashchip *flash,
618 struct w25q_range **w25q_ranges,
619 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700620{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800621 *w25q_ranges = 0;
622 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700623
David Hendricksd494b0a2010-08-16 16:28:50 -0700624 switch (flash->manufacture_id) {
625 case WINBOND_NEX_ID:
626 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800627 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800628 *w25q_ranges = w25x10_ranges;
629 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800630 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800631 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800632 *w25q_ranges = w25x20_ranges;
633 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800634 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800635 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800636 *w25q_ranges = w25x40_ranges;
637 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700638 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800639 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800640 *w25q_ranges = w25x80_ranges;
641 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800642 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800643 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800644 *w25q_ranges = w25q80_ranges;
645 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700646 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800647 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800648 *w25q_ranges = w25q16_ranges;
649 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700650 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800651 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800652 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800653 *w25q_ranges = w25q32_ranges;
654 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700655 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800656 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800657 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800658 *w25q_ranges = w25q64_ranges;
659 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700660 break;
661 default:
662 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
663 ", aborting\n", __func__, __LINE__,
664 flash->model_id);
665 return -1;
666 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700667 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700668 case EON_ID_NOPREFIX:
669 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800670 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800671 *w25q_ranges = en25f40_ranges;
672 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700673 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700674 case EON_EN25Q40:
675 *w25q_ranges = en25q40_ranges;
676 *num_entries = ARRAY_SIZE(en25q40_ranges);
677 break;
678 case EON_EN25Q80:
679 *w25q_ranges = en25q80_ranges;
680 *num_entries = ARRAY_SIZE(en25q80_ranges);
681 break;
682 case EON_EN25Q32:
683 *w25q_ranges = en25q32_ranges;
684 *num_entries = ARRAY_SIZE(en25q32_ranges);
685 break;
686 case EON_EN25Q64:
687 *w25q_ranges = en25q64_ranges;
688 *num_entries = ARRAY_SIZE(en25q64_ranges);
689 break;
690 case EON_EN25Q128:
691 *w25q_ranges = en25q128_ranges;
692 *num_entries = ARRAY_SIZE(en25q128_ranges);
693 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600694 case EON_EN25S64:
695 *w25q_ranges = en25s64_ranges;
696 *num_entries = ARRAY_SIZE(en25s64_ranges);
697 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700698 default:
699 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
700 ", aborting\n", __func__, __LINE__,
701 flash->model_id);
702 return -1;
703 }
704 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800705 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700706 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800707 case MACRONIX_MX25L1005:
708 *w25q_ranges = mx25l1005_ranges;
709 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
710 break;
711 case MACRONIX_MX25L2005:
712 *w25q_ranges = mx25l2005_ranges;
713 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
714 break;
715 case MACRONIX_MX25L4005:
716 *w25q_ranges = mx25l4005_ranges;
717 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
718 break;
719 case MACRONIX_MX25L8005:
720 *w25q_ranges = mx25l8005_ranges;
721 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
722 break;
723 case MACRONIX_MX25L1605:
724 /* FIXME: MX25L1605 and MX25L1605D have different write
725 * protection capabilities, but share IDs */
726 *w25q_ranges = mx25l1605d_ranges;
727 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
728 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800729 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800730 *w25q_ranges = mx25l3205d_ranges;
731 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700732 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800733 case MACRONIX_MX25U3235E:
734 *w25q_ranges = mx25u3235e_ranges;
735 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
736 break;
Jongpil66a96492014-08-14 17:59:06 +0900737 case MACRONIX_MX25U6435E:
738 *w25q_ranges = mx25u6435e_ranges;
739 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
740 break;
David Hendricksac72e362010-08-16 18:20:03 -0700741 default:
742 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
743 ", aborting\n", __func__, __LINE__,
744 flash->model_id);
745 return -1;
746 }
747 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700748 case ST_ID:
749 switch(flash->model_id) {
750 case ST_N25Q064__1E:
751 case ST_N25Q064__3E:
752 *w25q_ranges = n25q064_ranges;
753 *num_entries = ARRAY_SIZE(n25q064_ranges);
754 break;
755 default:
756 msg_cerr("%s() %d: Micron flash chip mismatch"
757 " (0x%04x), aborting\n", __func__, __LINE__,
758 flash->model_id);
759 return -1;
760 }
761 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700762 case GIGADEVICE_ID:
763 switch(flash->model_id) {
764 case GIGADEVICE_GD25LQ32:
765 *w25q_ranges = w25q32_ranges;
766 *num_entries = ARRAY_SIZE(w25q32_ranges);
767 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700768 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600769 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700770 *w25q_ranges = gd25q64_ranges;
771 *num_entries = ARRAY_SIZE(gd25q64_ranges);
772 break;
773 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700774 default:
775 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
776 " (0x%04x), aborting\n", __func__, __LINE__,
777 flash->model_id);
778 return -1;
779 }
780 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800781 case AMIC_ID_NOPREFIX:
782 switch(flash->model_id) {
783 case AMIC_A25L040:
784 *w25q_ranges = a25l040_ranges;
785 *num_entries = ARRAY_SIZE(a25l040_ranges);
786 break;
787 default:
788 msg_cerr("%s() %d: AMIC flash chip mismatch"
789 " (0x%04x), aborting\n", __func__, __LINE__,
790 flash->model_id);
791 return -1;
792 }
793 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700794 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700795 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
796 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700797 return -1;
798 }
799
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800800 return 0;
801}
802
803int w25_range_to_status(const struct flashchip *flash,
804 unsigned int start, unsigned int len,
805 struct w25q_status *status)
806{
807 struct w25q_range *w25q_ranges;
808 int i, range_found = 0;
809 int num_entries;
810
811 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700812 for (i = 0; i < num_entries; i++) {
813 struct wp_range *r = &w25q_ranges[i].range;
814
815 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
816 start, len, r->start, r->len);
817 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700818 status->bp0 = w25q_ranges[i].bp & 1;
819 status->bp1 = w25q_ranges[i].bp >> 1;
820 status->bp2 = w25q_ranges[i].bp >> 2;
821 status->tb = w25q_ranges[i].tb;
822 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700823
824 range_found = 1;
825 break;
826 }
827 }
828
829 if (!range_found) {
830 msg_cerr("matching range not found\n");
831 return -1;
832 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700833 return 0;
834}
835
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800836int w25_status_to_range(const struct flashchip *flash,
837 const struct w25q_status *status,
838 unsigned int *start, unsigned int *len)
839{
840 struct w25q_range *w25q_ranges;
841 int i, status_found = 0;
842 int num_entries;
843
844 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
845 for (i = 0; i < num_entries; i++) {
846 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800847 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800848
849 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
850 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
851 bp, w25q_ranges[i].bp,
852 status->tb, w25q_ranges[i].tb,
853 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800854 table_bp = w25q_ranges[i].bp;
855 table_tb = w25q_ranges[i].tb;
856 table_sec = w25q_ranges[i].sec;
857 if ((bp == table_bp || table_bp == X) &&
858 (status->tb == table_tb || table_tb == X) &&
859 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800860 *start = w25q_ranges[i].range.start;
861 *len = w25q_ranges[i].range.len;
862
863 status_found = 1;
864 break;
865 }
866 }
867
868 if (!status_found) {
869 msg_cerr("matching status not found\n");
870 return -1;
871 }
872 return 0;
873}
874
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800875/* Given a [start, len], this function calls w25_range_to_status() to convert
876 * it to flash-chip-specific range bits, then sets into status register.
877 */
David Hendricks91040832011-07-08 20:01:09 -0700878static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700879 unsigned int start, unsigned int len)
880{
881 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800882 int tmp = 0;
883 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700884
885 memset(&status, 0, sizeof(status));
886 tmp = spi_read_status_register();
887 memcpy(&status, &tmp, 1);
888 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
889
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800890 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700891
892 msg_cdbg("status.busy: %x\n", status.busy);
893 msg_cdbg("status.wel: %x\n", status.wel);
894 msg_cdbg("status.bp0: %x\n", status.bp0);
895 msg_cdbg("status.bp1: %x\n", status.bp1);
896 msg_cdbg("status.bp2: %x\n", status.bp2);
897 msg_cdbg("status.tb: %x\n", status.tb);
898 msg_cdbg("status.sec: %x\n", status.sec);
899 msg_cdbg("status.srp0: %x\n", status.srp0);
900
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800901 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -0800902 spi_write_status_register(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700903
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800904 tmp = spi_read_status_register();
905 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
906 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800907 return 0;
908 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800909 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800910 expected, tmp);
911 return 1;
912 }
David Hendricksf7924d12010-06-10 21:26:44 -0700913}
914
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800915/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700916static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800917{
918 struct w25q_status status;
919 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700920 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800921 int ret = 0;
922
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800923 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800924 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800925 memcpy(&status, &tmp, 1);
926 msg_cinfo("WP: status: 0x%02x\n", tmp);
927 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
928 msg_cinfo("WP: write protect is %s.\n",
929 status.srp0 ? "enabled" : "disabled");
930
931 msg_cinfo("WP: write protect range: ");
932 if (w25_status_to_range(flash, &status, &start, &len)) {
933 msg_cinfo("(cannot resolve the range)\n");
934 ret = -1;
935 } else {
936 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
937 }
938
939 return ret;
940}
941
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800942/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700943static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700944{
945 struct w25q_status status;
946 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800947 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700948
949 memset(&status, 0, sizeof(status));
950 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800951 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700952 memcpy(&status, &tmp, 1);
953 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
954
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800955 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800956 memcpy(&expected, &status, sizeof(status));
David Hendricks60824042014-12-11 17:22:06 -0800957 spi_write_status_register(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800958
959 tmp = spi_read_status_register();
960 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
961 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
962 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700963
964 return 0;
965}
966
David Hendricks1c09f802012-10-03 11:03:48 -0700967static int w25_enable_writeprotect(const struct flashchip *flash,
968 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800969{
970 int ret;
971
David Hendricks1c09f802012-10-03 11:03:48 -0700972 switch (wp_mode) {
973 case WP_MODE_HARDWARE:
974 ret = w25_set_srp0(flash, 1);
975 break;
976 default:
977 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
978 return 1;
979 }
980
David Hendricksc801adb2010-12-09 16:58:56 -0800981 if (ret)
982 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800983 return ret;
984}
985
David Hendricks91040832011-07-08 20:01:09 -0700986static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800987{
988 int ret;
989
990 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -0800991 if (ret)
992 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800993 return ret;
994}
995
David Hendricks91040832011-07-08 20:01:09 -0700996static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -0800997{
998 struct w25q_range *w25q_ranges;
999 int i, num_entries;
1000
1001 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1002 for (i = 0; i < num_entries; i++) {
1003 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1004 w25q_ranges[i].range.start,
1005 w25q_ranges[i].range.len);
1006 }
1007
1008 return 0;
1009}
1010
David Hendricks1c09f802012-10-03 11:03:48 -07001011/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1012uint8_t w25q_read_status_register_2(void)
1013{
1014 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1015 unsigned char readarr[2];
1016 int ret;
1017
1018 /* Read Status Register */
1019 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1020 if (ret) {
1021 /*
1022 * FIXME: make this a benign failure for now in case we are
1023 * unable to execute the opcode
1024 */
1025 msg_cdbg("RDSR2 failed!\n");
1026 readarr[0] = 0x00;
1027 }
1028
1029 return readarr[0];
1030}
1031
1032static int w25q_wp_status(const struct flashchip *flash)
1033{
1034 struct w25q_status sr1;
1035 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001036 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001037 unsigned int start, len;
1038 int ret = 0;
1039
1040 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001041 tmp[0] = spi_read_status_register();
1042 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001043
David Hendricksf1bd8802012-10-30 11:37:57 -07001044 memset(&sr2, 0, sizeof(sr2));
1045 tmp[1] = w25q_read_status_register_2();
1046 memcpy(&sr2, &tmp[1], 1);
1047
1048 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001049 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1050 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1051 msg_cinfo("WP: write protect is %s.\n",
1052 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1053
1054 msg_cinfo("WP: write protect range: ");
1055 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1056 msg_cinfo("(cannot resolve the range)\n");
1057 ret = -1;
1058 } else {
1059 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1060 }
1061
1062 return ret;
1063}
1064
1065/*
1066 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1067 * de-asserted after the first byte, then it acts like a JEDEC-standard
1068 * WRSR command. if /CS is asserted, then the next data byte is written
1069 * into status register 2.
1070 */
1071#define W25Q_WRSR_OUTSIZE 0x03
1072static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1073{
1074 int result;
1075 struct spi_command cmds[] = {
1076 {
1077 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1078 .writecnt = JEDEC_WREN_OUTSIZE,
1079 .writearr = (const unsigned char[]){ JEDEC_WREN },
1080 .readcnt = 0,
1081 .readarr = NULL,
1082 }, {
1083 .writecnt = W25Q_WRSR_OUTSIZE,
1084 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1085 .readcnt = 0,
1086 .readarr = NULL,
1087 }, {
1088 .writecnt = 0,
1089 .writearr = NULL,
1090 .readcnt = 0,
1091 .readarr = NULL,
1092 }};
1093
1094 result = spi_send_multicommand(cmds);
1095 if (result) {
1096 msg_cerr("%s failed during command execution\n",
1097 __func__);
1098 }
1099
1100 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001101 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001102
1103 return result;
1104}
1105
1106/*
1107 * Set/clear the SRP1 bit in status register 2.
1108 * FIXME: make this more generic if other chips use the same SR2 layout
1109 */
1110static int w25q_set_srp1(const struct flashchip *flash, int enable)
1111{
1112 struct w25q_status sr1;
1113 struct w25q_status_2 sr2;
1114 uint8_t tmp, expected;
1115
1116 tmp = spi_read_status_register();
1117 memcpy(&sr1, &tmp, 1);
1118 tmp = w25q_read_status_register_2();
1119 memcpy(&sr2, &tmp, 1);
1120
1121 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1122
1123 sr2.srp1 = enable ? 1 : 0;
1124
1125 memcpy(&expected, &sr2, 1);
1126 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1127
1128 tmp = w25q_read_status_register_2();
1129 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1130 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1131 return 1;
1132
1133 return 0;
1134}
1135
1136enum wp_mode get_wp_mode(const char *mode_str)
1137{
1138 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1139
1140 if (!strcasecmp(mode_str, "hardware"))
1141 wp_mode = WP_MODE_HARDWARE;
1142 else if (!strcasecmp(mode_str, "power_cycle"))
1143 wp_mode = WP_MODE_POWER_CYCLE;
1144 else if (!strcasecmp(mode_str, "permanent"))
1145 wp_mode = WP_MODE_PERMANENT;
1146
1147 return wp_mode;
1148}
1149
1150static int w25q_disable_writeprotect(const struct flashchip *flash,
1151 enum wp_mode wp_mode)
1152{
1153 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001154 struct w25q_status_2 sr2;
1155 uint8_t tmp;
1156
1157 switch (wp_mode) {
1158 case WP_MODE_HARDWARE:
1159 ret = w25_set_srp0(flash, 0);
1160 break;
1161 case WP_MODE_POWER_CYCLE:
1162 tmp = w25q_read_status_register_2();
1163 memcpy(&sr2, &tmp, 1);
1164 if (sr2.srp1) {
1165 msg_cerr("%s(): must disconnect power to disable "
1166 "write-protection\n", __func__);
1167 } else {
1168 ret = 0;
1169 }
1170 break;
1171 case WP_MODE_PERMANENT:
1172 msg_cerr("%s(): cannot disable permanent write-protection\n",
1173 __func__);
1174 break;
1175 default:
1176 msg_cerr("%s(): invalid mode specified\n", __func__);
1177 break;
1178 }
1179
1180 if (ret)
1181 msg_cerr("%s(): error=%d.\n", __func__, ret);
1182 return ret;
1183}
1184
1185static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1186{
1187 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1188}
1189
1190static int w25q_enable_writeprotect(const struct flashchip *flash,
1191 enum wp_mode wp_mode)
1192{
1193 int ret = 1;
1194 struct w25q_status sr1;
1195 struct w25q_status_2 sr2;
1196 uint8_t tmp;
1197
1198 switch (wp_mode) {
1199 case WP_MODE_HARDWARE:
1200 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1201 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1202 __func__);
1203 break;
1204 }
1205
1206 tmp = spi_read_status_register();
1207 memcpy(&sr1, &tmp, 1);
1208 if (sr1.srp0)
1209 ret = 0;
1210 else
1211 ret = w25_set_srp0(flash, 1);
1212
1213 break;
1214 case WP_MODE_POWER_CYCLE:
1215 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1216 msg_cerr("%s(): cannot disable hardware WP mode\n",
1217 __func__);
1218 break;
1219 }
1220
1221 tmp = w25q_read_status_register_2();
1222 memcpy(&sr2, &tmp, 1);
1223 if (sr2.srp1)
1224 ret = 0;
1225 else
1226 ret = w25q_set_srp1(flash, 1);
1227
1228 break;
1229 case WP_MODE_PERMANENT:
1230 tmp = spi_read_status_register();
1231 memcpy(&sr1, &tmp, 1);
1232 if (sr1.srp0 == 0) {
1233 ret = w25_set_srp0(flash, 1);
1234 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001235 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001236 "permanent WP\n", __func__);
1237 break;
1238 }
1239 }
1240
1241 tmp = w25q_read_status_register_2();
1242 memcpy(&sr2, &tmp, 1);
1243 if (sr2.srp1 == 0) {
1244 ret = w25q_set_srp1(flash, 1);
1245 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001246 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001247 "permanent WP\n", __func__);
1248 break;
1249 }
1250 }
1251
1252 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001253 default:
1254 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1255 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001256 }
1257
1258 if (ret)
1259 msg_cerr("%s(): error=%d.\n", __func__, ret);
1260 return ret;
1261}
1262
David Hendricksc3496092014-11-13 17:20:55 -08001263/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1264uint8_t mx25l_read_config_register(void)
1265{
1266 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1267 unsigned char readarr[2]; /* leave room for dummy byte */
1268 int ret;
1269
1270 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1271 if (ret) {
1272 msg_cerr("RDCR failed!\n");
1273 readarr[0] = 0x00;
1274 }
1275
1276 return readarr[0];
1277}
David Hendricks1c09f802012-10-03 11:03:48 -07001278/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001279struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001280 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001281 .set_range = w25_set_range,
1282 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001283 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001284 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001285
1286};
1287
1288/* W25Q series has features such as a second status register and SFDP */
1289struct wp wp_w25q = {
1290 .list_ranges = w25_list_ranges,
1291 .set_range = w25_set_range,
1292 .enable = w25q_enable_writeprotect,
1293 /*
1294 * By default, disable hardware write-protection. We may change
1295 * this later if we want to add fine-grained write-protect disable
1296 * as a command-line option.
1297 */
1298 .disable = w25q_disable_writeprotect_default,
1299 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001300};
David Hendrickse0512a72014-07-15 20:30:47 -07001301
David Hendricksaf3944a2014-07-28 18:37:40 -07001302struct generic_range gd25q32_cmp0_ranges[] = {
1303 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001304 { { }, 0x00, {0, 0} },
1305 { { }, 0x08, {0, 0} },
1306 { { }, 0x10, {0, 0} },
1307 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001308
David Hendricks148a4bf2015-03-13 21:02:42 -07001309 { { }, 0x01, {0x3f0000, 64 * 1024} },
1310 { { }, 0x02, {0x3e0000, 128 * 1024} },
1311 { { }, 0x03, {0x3c0000, 256 * 1024} },
1312 { { }, 0x04, {0x380000, 512 * 1024} },
1313 { { }, 0x05, {0x300000, 1024 * 1024} },
1314 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001315
David Hendricks148a4bf2015-03-13 21:02:42 -07001316 { { }, 0x09, {0x000000, 64 * 1024} },
1317 { { }, 0x0a, {0x000000, 128 * 1024} },
1318 { { }, 0x0b, {0x000000, 256 * 1024} },
1319 { { }, 0x0c, {0x000000, 512 * 1024} },
1320 { { }, 0x0d, {0x000000, 1024 * 1024} },
1321 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001322
1323 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001324 { { }, 0x07, {0x000000, 4096 * 1024} },
1325 { { }, 0x0f, {0x000000, 4096 * 1024} },
1326 { { }, 0x17, {0x000000, 4096 * 1024} },
1327 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001328
David Hendricks148a4bf2015-03-13 21:02:42 -07001329 { { }, 0x11, {0x3ff000, 4 * 1024} },
1330 { { }, 0x12, {0x3fe000, 8 * 1024} },
1331 { { }, 0x13, {0x3fc000, 16 * 1024} },
1332 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1333 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1334 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001335
David Hendricks148a4bf2015-03-13 21:02:42 -07001336 { { }, 0x19, {0x000000, 4 * 1024} },
1337 { { }, 0x1a, {0x000000, 8 * 1024} },
1338 { { }, 0x1b, {0x000000, 16 * 1024} },
1339 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1340 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1341 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001342};
1343
1344struct generic_range gd25q32_cmp1_ranges[] = {
1345 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001346 { { }, 0x00, {0, 0} },
1347 { { }, 0x08, {0, 0} },
1348 { { }, 0x10, {0, 0} },
1349 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001350
David Hendricks148a4bf2015-03-13 21:02:42 -07001351 { { }, 0x01, {0x000000, 4032 * 1024} },
1352 { { }, 0x02, {0x000000, 3968 * 1024} },
1353 { { }, 0x03, {0x000000, 3840 * 1024} },
1354 { { }, 0x04, {0x000000, 3584 * 1024} },
1355 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1356 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001357
David Hendricks148a4bf2015-03-13 21:02:42 -07001358 { { }, 0x09, {0x010000, 4032 * 1024} },
1359 { { }, 0x0a, {0x020000, 3968 * 1024} },
1360 { { }, 0x0b, {0x040000, 3840 * 1024} },
1361 { { }, 0x0c, {0x080000, 3584 * 1024} },
1362 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1363 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001364
1365 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001366 { { }, 0x07, {0x000000, 4096 * 1024} },
1367 { { }, 0x0f, {0x000000, 4096 * 1024} },
1368 { { }, 0x17, {0x000000, 4096 * 1024} },
1369 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001370
David Hendricks148a4bf2015-03-13 21:02:42 -07001371 { { }, 0x11, {0x000000, 4092 * 1024} },
1372 { { }, 0x12, {0x000000, 4088 * 1024} },
1373 { { }, 0x13, {0x000000, 4080 * 1024} },
1374 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1375 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1376 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001377
David Hendricks148a4bf2015-03-13 21:02:42 -07001378 { { }, 0x19, {0x001000, 4092 * 1024} },
1379 { { }, 0x1a, {0x002000, 4088 * 1024} },
1380 { { }, 0x1b, {0x040000, 4080 * 1024} },
1381 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1382 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1383 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001384};
1385
1386static struct generic_wp gd25q32_wp = {
1387 /* TODO: map second status register */
1388 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1389};
1390
David Hendricks83541d32014-07-15 20:58:21 -07001391#if 0
1392/* FIXME: MX25L6405D has same ID as MX25L6406 */
1393static struct w25q_range mx25l6405d_ranges[] = {
1394 { X, 0, 0, {0, 0} }, /* none */
1395 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1396 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1397 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1398 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1399 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1400 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1401 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1402
1403 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1404 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1405 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1406 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1407 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1408 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1409 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1410 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1411};
1412#endif
1413
1414/* FIXME: MX25L6406 has same ID as MX25L6405D */
1415struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001416 { { }, 0, {0, 0} }, /* none */
1417 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1418 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1419 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1420 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1421 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1422 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001423
David Hendricks148a4bf2015-03-13 21:02:42 -07001424 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1425 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1426 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1427 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1428 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1429 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1430 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1431 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1432 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001433};
1434
1435static struct generic_wp mx25l6406e_wp = {
1436 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1437 .ranges = &mx25l6406e_ranges[0],
1438};
David Hendrickse0512a72014-07-15 20:30:47 -07001439
David Hendricksc3496092014-11-13 17:20:55 -08001440struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001441 { { }, 0, {0, 0} }, /* none */
1442 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1443 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1444 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001445
David Hendricks148a4bf2015-03-13 21:02:42 -07001446 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1447 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1448 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1449 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1450 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1451 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1452 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1453 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1454 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1455 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1456 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1457 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001458};
1459
1460struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001461 { { }, 0, {0, 0} }, /* none */
1462 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1463 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1464 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1465 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1466 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1467 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1468 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1469 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1470 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1471 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1472 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1473 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1474 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1475 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1476 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001477};
1478
1479static struct generic_wp mx25l6495f_wp = {
1480 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1481};
1482
David Hendricks148a4bf2015-03-13 21:02:42 -07001483struct generic_range s25fs128s_ranges[] = {
1484 { { .tb = 1 }, 0, {0, 0} }, /* none */
1485 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1486 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1487 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1488 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1489 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1490 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1491 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001492
David Hendricks148a4bf2015-03-13 21:02:42 -07001493 { { .tb = 0 }, 0, {0, 0} }, /* none */
1494 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1495 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1496 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1497 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1498 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1499 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1500 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001501};
1502
1503static struct generic_wp s25fs128s_wp = {
1504 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001505 .get_modifier_bits = s25f_get_modifier_bits,
1506 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08001507};
1508
David Hendricksc694bb82015-02-25 14:52:17 -08001509
David Hendricks148a4bf2015-03-13 21:02:42 -07001510struct generic_range s25fl256s_ranges[] = {
1511 { { .tb = 1 }, 0, {0, 0} }, /* none */
1512 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
1513 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
1514 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
1515 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
1516 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
1517 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
1518 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
1519
1520 { { .tb = 0 }, 0, {0, 0} }, /* none */
1521 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
1522 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
1523 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
1524 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
1525 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
1526 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
1527 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08001528};
1529
1530static struct generic_wp s25fl256s_wp = {
1531 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001532 .get_modifier_bits = s25f_get_modifier_bits,
1533 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08001534};
1535
David Hendrickse0512a72014-07-15 20:30:47 -07001536/* Given a flash chip, this function returns its writeprotect info. */
1537static int generic_range_table(const struct flashchip *flash,
1538 struct generic_wp **wp,
1539 int *num_entries)
1540{
1541 *wp = NULL;
1542 *num_entries = 0;
1543
1544 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001545 case GIGADEVICE_ID:
1546 switch(flash->model_id) {
1547 case GIGADEVICE_GD25Q32: {
1548 uint8_t sr1 = w25q_read_status_register_2();
1549
1550 *wp = &gd25q32_wp;
1551 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1552 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1553 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1554 } else { /* CMP == 1 */
1555 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1556 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1557 }
1558
1559 break;
1560 /* TODO(shawnn): add support for other GD parts */
1561 }
1562 default:
1563 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1564 " (0x%04x), aborting\n", __func__, __LINE__,
1565 flash->model_id);
1566 return -1;
1567 }
1568 break;
David Hendricks83541d32014-07-15 20:58:21 -07001569 case MACRONIX_ID:
1570 switch (flash->model_id) {
1571 case MACRONIX_MX25L6405:
1572 /* FIXME: MX25L64* chips have mixed capabilities and
1573 share IDs */
1574 *wp = &mx25l6406e_wp;
1575 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1576 break;
David Hendricksc3496092014-11-13 17:20:55 -08001577 case MACRONIX_MX25L6495F: {
1578 uint8_t cr = mx25l_read_config_register();
1579
1580 *wp = &mx25l6495f_wp;
1581 if (!(cr & (1 << 3))) { /* T/B == 0 */
1582 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1583 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1584 } else { /* T/B == 1 */
1585 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1586 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1587 }
1588 break;
1589 }
David Hendricks83541d32014-07-15 20:58:21 -07001590 default:
1591 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1592 ", aborting\n", __func__, __LINE__,
1593 flash->model_id);
1594 return -1;
1595 }
1596 break;
David Hendricksa9884852014-12-11 15:31:12 -08001597 case SPANSION_ID:
1598 switch (flash->model_id) {
1599 case SPANSION_S25FS128S_L:
1600 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08001601 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001602 (*wp)->ranges = s25fs128s_ranges;
1603 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08001604 break;
1605 }
David Hendricksc694bb82015-02-25 14:52:17 -08001606 case SPANSION_S25FL256S_UL:
1607 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08001608 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001609 (*wp)->ranges = s25fl256s_ranges;
1610 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08001611 break;
1612 }
David Hendricksa9884852014-12-11 15:31:12 -08001613 default:
1614 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
1615 ", aborting\n", __func__, __LINE__, flash->model_id);
1616 return -1;
1617 }
1618 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001619 default:
1620 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1621 __func__, flash->manufacture_id);
1622 return -1;
1623 }
1624
1625 return 0;
1626}
1627
1628/* Given a [start, len], this function finds a block protect bit combination
1629 * (if possible) and sets the corresponding bits in "status". Remaining bits
1630 * are preserved. */
1631static int generic_range_to_status(const struct flashchip *flash,
1632 unsigned int start, unsigned int len,
1633 uint8_t *status)
1634{
1635 struct generic_wp *wp;
1636 struct generic_range *r;
1637 int i, range_found = 0, num_entries;
1638 uint8_t bp_mask;
1639
1640 if (generic_range_table(flash, &wp, &num_entries))
1641 return -1;
1642
1643 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1644 ((1 << wp->sr1.bp0_pos) - 1);
1645
1646 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1647 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1648 start, len, r->range.start, r->range.len);
1649 if ((start == r->range.start) && (len == r->range.len)) {
1650 *status &= ~(bp_mask);
1651 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07001652
1653 if (wp->set_modifier_bits) {
1654 if (wp->set_modifier_bits(flash, &r->m) < 0) {
1655 msg_cerr("error setting modifier "
1656 "bits for range.\n");
1657 return -1;
1658 }
1659 }
1660
David Hendrickse0512a72014-07-15 20:30:47 -07001661 range_found = 1;
1662 break;
1663 }
1664 }
1665
1666 if (!range_found) {
1667 msg_cerr("matching range not found\n");
1668 return -1;
1669 }
1670 return 0;
1671}
1672
1673static int generic_status_to_range(const struct flashchip *flash,
1674 const uint8_t sr1, unsigned int *start, unsigned int *len)
1675{
1676 struct generic_wp *wp;
1677 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07001678 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07001679 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001680 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07001681
1682 if (generic_range_table(flash, &wp, &num_entries))
1683 return -1;
1684
David Hendricks148a4bf2015-03-13 21:02:42 -07001685 /* modifier bits may be compared more than once, so get them here */
1686 if (wp->get_modifier_bits) {
1687 if (wp->get_modifier_bits(flash, &m) < 0)
1688 return -1;
1689 }
1690
David Hendrickse0512a72014-07-15 20:30:47 -07001691 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1692
1693 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07001694 if (wp->get_modifier_bits) {
1695 if (memcmp(&m, &r->m, sizeof(m)))
1696 continue;
1697 }
David Hendrickse0512a72014-07-15 20:30:47 -07001698 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1699 if (sr1_bp == r->bp) {
1700 *start = r->range.start;
1701 *len = r->range.len;
1702 status_found = 1;
1703 break;
1704 }
1705 }
1706
1707 if (!status_found) {
1708 msg_cerr("matching status not found\n");
1709 return -1;
1710 }
1711 return 0;
1712}
1713
1714/* Given a [start, len], this function calls generic_range_to_status() to
1715 * convert it to flash-chip-specific range bits, then sets into status register.
1716 */
1717static int generic_set_range(const struct flashchip *flash,
1718 unsigned int start, unsigned int len)
1719{
1720 uint8_t status, expected;
1721
1722 status = spi_read_status_register();
1723 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1724
1725 expected = status; /* preserve non-bp bits */
1726 if (generic_range_to_status(flash, start, len, &expected))
1727 return -1;
1728
David Hendricks60824042014-12-11 17:22:06 -08001729 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001730
1731 status = spi_read_status_register();
1732 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1733 if (status != expected) {
1734 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1735 expected, status);
1736 return 1;
1737 }
1738
1739 return 0;
1740}
1741
1742/* Set/clear the status regsiter write protect bit in SR1. */
1743static int generic_set_srp0(const struct flashchip *flash, int enable)
1744{
1745 uint8_t status, expected;
1746 struct generic_wp *wp;
1747 int num_entries;
1748
1749 if (generic_range_table(flash, &wp, &num_entries))
1750 return -1;
1751
1752 expected = spi_read_status_register();
1753 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1754
1755 if (enable)
1756 expected |= 1 << wp->sr1.srp_pos;
1757 else
1758 expected &= ~(1 << wp->sr1.srp_pos);
1759
David Hendricks60824042014-12-11 17:22:06 -08001760 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001761
1762 status = spi_read_status_register();
1763 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1764 if (status != expected)
1765 return -1;
1766
1767 return 0;
1768}
1769
1770static int generic_enable_writeprotect(const struct flashchip *flash,
1771 enum wp_mode wp_mode)
1772{
1773 int ret;
1774
1775 switch (wp_mode) {
1776 case WP_MODE_HARDWARE:
1777 ret = generic_set_srp0(flash, 1);
1778 break;
1779 default:
1780 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1781 return 1;
1782 }
1783
1784 if (ret)
1785 msg_cerr("%s(): error=%d.\n", __func__, ret);
1786 return ret;
1787}
1788
1789static int generic_disable_writeprotect(const struct flashchip *flash)
1790{
1791 int ret;
1792
1793 ret = generic_set_srp0(flash, 0);
1794 if (ret)
1795 msg_cerr("%s(): error=%d.\n", __func__, ret);
1796 return ret;
1797}
1798
1799static int generic_list_ranges(const struct flashchip *flash)
1800{
1801 struct generic_wp *wp;
1802 struct generic_range *r;
1803 int i, num_entries;
1804
1805 if (generic_range_table(flash, &wp, &num_entries))
1806 return -1;
1807
1808 r = &wp->ranges[0];
1809 for (i = 0; i < num_entries; i++) {
1810 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1811 r->range.start, r->range.len);
1812 r++;
1813 }
1814
1815 return 0;
1816}
1817
1818static int generic_wp_status(const struct flashchip *flash)
1819{
1820 uint8_t sr1;
1821 unsigned int start, len;
1822 int ret = 0;
1823 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07001824 int num_entries, wp_en;
1825
1826 if (generic_range_table(flash, &wp, &num_entries))
1827 return -1;
1828
1829 sr1 = spi_read_status_register();
1830 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
1831
1832 msg_cinfo("WP: status: 0x%04x\n", sr1);
1833 msg_cinfo("WP: status.srp0: %x\n", wp_en);
1834 /* FIXME: SRP1 is not really generic, but we probably should print
1835 * it anyway to have consistent output. #legacycruft */
1836 msg_cinfo("WP: status.srp1: %x\n", 0);
1837 msg_cinfo("WP: write protect is %s.\n",
1838 wp_en ? "enabled" : "disabled");
1839
1840 msg_cinfo("WP: write protect range: ");
1841 if (generic_status_to_range(flash, sr1, &start, &len)) {
1842 msg_cinfo("(cannot resolve the range)\n");
1843 ret = -1;
1844 } else {
1845 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1846 }
1847
1848 return ret;
1849}
1850
1851struct wp wp_generic = {
1852 .list_ranges = generic_list_ranges,
1853 .set_range = generic_set_range,
1854 .enable = generic_enable_writeprotect,
1855 .disable = generic_disable_writeprotect,
1856 .wp_status = generic_wp_status,
1857};