blob: 5bbc6068e28e9014cba0be15d53467ebbf10ac9d [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 *
David Hendricksd1c55d72010-08-24 15:14:19 -070016 */
17
David Hendricksf7924d12010-06-10 21:26:44 -070018#include <stdlib.h>
19#include <string.h>
20
21#include "flash.h"
22#include "flashchips.h"
23#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080024#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070025#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070026
David Hendricks1c09f802012-10-03 11:03:48 -070027/*
David Hendricksf7924d12010-06-10 21:26:44 -070028 * The following procedures rely on look-up tables to match the user-specified
29 * range with the chip's supported ranges. This turned out to be the most
30 * elegant approach since diferent flash chips use different levels of
31 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070032 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070033 */
34
35struct wp_range {
36 unsigned int start; /* starting address */
37 unsigned int len; /* len */
38};
39
40enum bit_state {
41 OFF = 0,
42 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080043 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070044};
45
David Hendrickse0512a72014-07-15 20:30:47 -070046/*
47 * Generic write-protection schema for 25-series SPI flash chips. This assumes
48 * there is a status register that contains one or more consecutive bits which
49 * determine which address range is protected.
50 */
51
52struct status_register_layout {
53 int bp0_pos; /* position of BP0 */
54 int bp_bits; /* number of block protect bits */
55 int srp_pos; /* position of status register protect enable bit */
56};
57
58struct generic_range {
David Hendricks148a4bf2015-03-13 21:02:42 -070059 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070060 unsigned int bp; /* block protect bitfield */
61 struct wp_range range;
62};
63
64struct generic_wp {
65 struct status_register_layout sr1; /* status register 1 */
66 struct generic_range *ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -070067
68 /*
69 * Some chips store modifier bits in one or more special control
70 * registers instead of the status register like many older SPI NOR
71 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
72 * any chip-specific operations necessary to get/set these bit values.
73 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074 int (*get_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070075 struct generic_modifier_bits *m);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070076 int (*set_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070077 struct generic_modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070078};
79
80/*
81 * The following ranges and functions are useful for representing Winbond-
82 * style writeprotect schema in which there are typically 5 bits of
83 * relevant information stored in status register 1:
84 * sec: This bit indicates the units (sectors vs. blocks)
85 * tb: The top-bottom bit indicates if the affected range is at the top of
86 * the flash memory's address space or at the bottom.
Duncan Laurie1801f7c2019-01-09 18:02:51 -080087 * bp: Bitmask representing the number of affected sectors/blocks.
David Hendrickse0512a72014-07-15 20:30:47 -070088 */
David Hendricksf7924d12010-06-10 21:26:44 -070089struct w25q_range {
Duncan Laurie1801f7c2019-01-09 18:02:51 -080090 enum bit_state sec; /* if 1, bp bits describe sectors */
David Hendricksf7924d12010-06-10 21:26:44 -070091 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080092 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070093 struct wp_range range;
94};
95
David Hendrickse0512a72014-07-15 20:30:47 -070096/*
97 * Mask to extract write-protect enable and range bits
98 * Status register 1:
99 * SRP0: bit 7
100 * range(BP2-BP0): bit 4-2
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800101 * range(BP3-BP0): bit 5-2 (large chips)
David Hendrickse0512a72014-07-15 20:30:47 -0700102 * Status register 2:
103 * SRP1: bit 1
104 */
105#define MASK_WP_AREA (0x9C)
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800106#define MASK_WP_AREA_LARGE (0x9C)
David Hendrickse0512a72014-07-15 20:30:47 -0700107#define MASK_WP2_AREA (0x01)
108
David Hendricks57566ed2010-08-16 18:24:45 -0700109struct w25q_range en25f40_ranges[] = {
110 { X, X, 0, {0, 0} }, /* none */
111 { 0, 0, 0x1, {0x000000, 504 * 1024} },
112 { 0, 0, 0x2, {0x000000, 496 * 1024} },
113 { 0, 0, 0x3, {0x000000, 480 * 1024} },
114 { 0, 0, 0x4, {0x000000, 448 * 1024} },
115 { 0, 0, 0x5, {0x000000, 384 * 1024} },
116 { 0, 0, 0x6, {0x000000, 256 * 1024} },
117 { 0, 0, 0x7, {0x000000, 512 * 1024} },
118};
119
David Hendrickse185bf22011-05-24 15:34:18 -0700120struct w25q_range en25q40_ranges[] = {
121 { 0, 0, 0, {0, 0} }, /* none */
122 { 0, 0, 0x1, {0x000000, 504 * 1024} },
123 { 0, 0, 0x2, {0x000000, 496 * 1024} },
124 { 0, 0, 0x3, {0x000000, 480 * 1024} },
125
126 { 0, 1, 0x0, {0x000000, 448 * 1024} },
127 { 0, 1, 0x1, {0x000000, 384 * 1024} },
128 { 0, 1, 0x2, {0x000000, 256 * 1024} },
129 { 0, 1, 0x3, {0x000000, 512 * 1024} },
130};
131
132struct w25q_range en25q80_ranges[] = {
133 { 0, 0, 0, {0, 0} }, /* none */
134 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
135 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
136 { 0, 0, 0x3, {0x000000, 992 * 1024} },
137 { 0, 0, 0x4, {0x000000, 960 * 1024} },
138 { 0, 0, 0x5, {0x000000, 896 * 1024} },
139 { 0, 0, 0x6, {0x000000, 768 * 1024} },
140 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
141};
142
143struct w25q_range en25q32_ranges[] = {
144 { 0, 0, 0, {0, 0} }, /* none */
145 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
146 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
147 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
148 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
149 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
150 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
151 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
152
153 { 0, 1, 0, {0, 0} }, /* none */
154 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
155 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
156 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
157 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
158 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
159 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
160 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
161};
162
163struct w25q_range en25q64_ranges[] = {
164 { 0, 0, 0, {0, 0} }, /* none */
165 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
166 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
167 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
168 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
169 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
170 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
171 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
172
173 { 0, 1, 0, {0, 0} }, /* none */
174 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
175 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
176 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
177 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
178 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
179 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
180 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
181};
182
183struct w25q_range en25q128_ranges[] = {
184 { 0, 0, 0, {0, 0} }, /* none */
185 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
186 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
187 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
188 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
189 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
190 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
191 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
192
193 { 0, 1, 0, {0, 0} }, /* none */
194 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
195 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
196 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
197 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
198 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
199 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
200 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
201};
202
Marc Jonesb2f90022014-04-29 17:37:23 -0600203struct w25q_range en25s64_ranges[] = {
204 { 0, 0, 0, {0, 0} }, /* none */
205 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
206 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
207 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
208 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
209 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
210 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
211 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
212
213 { 0, 1, 0, {0, 0} }, /* none */
214 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
215 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
216 { 0, 1, 0x3, {0x780000, 512 * 1024} },
217 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
218 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
219 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
220 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
221};
222
David Hendricksf8f00c72011-02-01 12:39:46 -0800223/* mx25l1005 ranges also work for the mx25l1005c */
224static struct w25q_range mx25l1005_ranges[] = {
225 { X, X, 0, {0, 0} }, /* none */
226 { X, X, 0x1, {0x010000, 64 * 1024} },
227 { X, X, 0x2, {0x000000, 128 * 1024} },
228 { X, X, 0x3, {0x000000, 128 * 1024} },
229};
230
231static struct w25q_range mx25l2005_ranges[] = {
232 { X, X, 0, {0, 0} }, /* none */
233 { X, X, 0x1, {0x030000, 64 * 1024} },
234 { X, X, 0x2, {0x020000, 128 * 1024} },
235 { X, X, 0x3, {0x000000, 256 * 1024} },
236};
237
238static struct w25q_range mx25l4005_ranges[] = {
239 { X, X, 0, {0, 0} }, /* none */
240 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
241 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
242 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
243 { X, X, 0x4, {0x000000, 512 * 1024} },
244 { X, X, 0x5, {0x000000, 512 * 1024} },
245 { X, X, 0x6, {0x000000, 512 * 1024} },
246 { X, X, 0x7, {0x000000, 512 * 1024} },
247};
248
249static struct w25q_range mx25l8005_ranges[] = {
250 { X, X, 0, {0, 0} }, /* none */
251 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
252 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
253 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
254 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
255 { X, X, 0x5, {0x000000, 1024 * 1024} },
256 { X, X, 0x6, {0x000000, 1024 * 1024} },
257 { X, X, 0x7, {0x000000, 1024 * 1024} },
258};
259
260#if 0
261/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
262static struct w25q_range mx25l1605_ranges[] = {
263 { X, X, 0, {0, 0} }, /* none */
264 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
265 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
266 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
267 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
268 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
269 { X, X, 0x6, {0x000000, 2048 * 1024} },
270 { X, X, 0x7, {0x000000, 2048 * 1024} },
271};
272#endif
273
274#if 0
275/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
276static struct w25q_range mx25l6405_ranges[] = {
277 { X, 0, 0, {0, 0} }, /* none */
278 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
279 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
280 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
281 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
282 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
283 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
284 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
285
286 { X, 1, 0x0, {0x000000, 8192 * 1024} },
287 { X, 1, 0x1, {0x000000, 8192 * 1024} },
288 { X, 1, 0x2, {0x000000, 8192 * 1024} },
289 { X, 1, 0x3, {0x000000, 8192 * 1024} },
290 { X, 1, 0x4, {0x000000, 8192 * 1024} },
291 { X, 1, 0x5, {0x000000, 8192 * 1024} },
292 { X, 1, 0x6, {0x000000, 8192 * 1024} },
293 { X, 1, 0x7, {0x000000, 8192 * 1024} },
294};
295#endif
296
297static struct w25q_range mx25l1605d_ranges[] = {
298 { X, 0, 0, {0, 0} }, /* none */
299 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
300 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
301 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
302 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
303 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
304 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
305 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
306
307 { X, 1, 0x0, {0x000000, 2048 * 1024} },
308 { X, 1, 0x1, {0x000000, 2048 * 1024} },
309 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
310 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
311 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
312 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
313 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
314 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
315};
316
317/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700318static struct w25q_range mx25l3205d_ranges[] = {
319 { X, 0, 0, {0, 0} }, /* none */
320 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
321 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
322 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
323 { X, 0, 0x4, {0x380000, 512 * 1024} },
324 { X, 0, 0x5, {0x300000, 1024 * 1024} },
325 { X, 0, 0x6, {0x200000, 2048 * 1024} },
326 { X, 0, 0x7, {0x000000, 4096 * 1024} },
327
328 { X, 1, 0x0, {0x000000, 4096 * 1024} },
329 { X, 1, 0x1, {0x000000, 2048 * 1024} },
330 { X, 1, 0x2, {0x000000, 3072 * 1024} },
331 { X, 1, 0x3, {0x000000, 3584 * 1024} },
332 { X, 1, 0x4, {0x000000, 3840 * 1024} },
333 { X, 1, 0x5, {0x000000, 3968 * 1024} },
334 { X, 1, 0x6, {0x000000, 4032 * 1024} },
335 { X, 1, 0x7, {0x000000, 4096 * 1024} },
336};
337
Vincent Palatin87e092a2013-02-28 15:46:14 -0800338static struct w25q_range mx25u3235e_ranges[] = {
339 { X, 0, 0, {0, 0} }, /* none */
340 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
341 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
342 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
343 { 0, 0, 0x4, {0x380000, 512 * 1024} },
344 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
345 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
346 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
347
348 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
349 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
350 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
351 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
352 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
353 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
354 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
355 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
356};
357
Jongpil66a96492014-08-14 17:59:06 +0900358static struct w25q_range mx25u6435e_ranges[] = {
359 { X, 0, 0, {0, 0} }, /* none */
360 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
361 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
362 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
363 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
364 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
365 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
366 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
367
368 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
369 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
370 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
371 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
372 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
373 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
374 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
375 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
376};
377
Alan Greendc0792e2019-07-01 15:01:34 +1000378static struct w25q_range mx25u12835e_ranges[] = {
Paul Fagerburg90571582019-03-15 11:32:57 -0600379 { X, X, 0, {0, 0} }, /* none */
Alex Lu831c6092017-11-02 23:19:34 -0700380 { 0, 0, 0x1, {0xff0000, 1 * 64 * 1024} }, /* block 255 */
381 { 0, 0, 0x2, {0xfe0000, 2 * 64 * 1024} }, /* blocks 254-255 */
382 { 0, 0, 0x3, {0xfc0000, 4 * 64 * 1024} }, /* blocks 252-255 */
383 { 0, 0, 0x4, {0xf80000, 8 * 64 * 1024} }, /* blocks 248-255 */
384 { 0, 0, 0x5, {0xf00000, 16 * 64 * 1024} }, /* blocks 240-255 */
385 { 0, 0, 0x6, {0xe00000, 32 * 64 * 1024} }, /* blocks 224-255 */
386 { 0, 0, 0x7, {0xc00000, 64 * 64 * 1024} }, /* blocks 192-255 */
Paul Fagerburg90571582019-03-15 11:32:57 -0600387 { 0, 0, 0x8, {0x800000, 128 * 64 * 1024} }, /* blocks 128-255 */
388 { 0, 0, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
389 { 0, 0, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
390 { 0, 0, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
391 { 0, 0, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
392 { 0, 0, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
393 { 0, 0, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
394 { 0, 0, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Alex Lu831c6092017-11-02 23:19:34 -0700395
Paul Fagerburg90571582019-03-15 11:32:57 -0600396 { 0, 1, 0x1, {0x000000, 1 * 64 * 1024} }, /* block 0 */
397 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
398 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
399 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
400 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
401 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
402 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
403 { 0, 1, 0x8, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
404 { 0, 1, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
405 { 0, 1, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
406 { 0, 1, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
407 { 0, 1, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
408 { 0, 1, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
409 { 0, 1, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
410 { 0, 1, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Alex Lu831c6092017-11-02 23:19:34 -0700411};
412
David Hendricksbfa624b2012-07-24 12:47:59 -0700413static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700414 /*
415 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
416 * (block protect bit 3). It is only useful when all blocks are to
417 * be write-protected.
418 */
David Hendricks42a549a2015-04-22 11:25:07 -0700419 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700420
421 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
422 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
423 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
424 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
425 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
426 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
427 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
428
David Hendricksfe9123b2015-04-21 13:18:31 -0700429 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
430 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
431 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
432 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
433 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
434 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
435 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700436
437 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
438 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
439 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
440 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
441 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
442 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
443 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
444 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
445};
446
David Hendricksf7924d12010-06-10 21:26:44 -0700447static struct w25q_range w25q16_ranges[] = {
448 { X, X, 0, {0, 0} }, /* none */
449 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
450 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
451 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
452 { 0, 0, 0x4, {0x180000, 512 * 1024} },
453 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
454
455 { 0, 1, 0x1, {0x000000, 64 * 1024} },
456 { 0, 1, 0x2, {0x000000, 128 * 1024} },
457 { 0, 1, 0x3, {0x000000, 256 * 1024} },
458 { 0, 1, 0x4, {0x000000, 512 * 1024} },
459 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
460 { X, X, 0x6, {0x000000, 2048 * 1024} },
461 { X, X, 0x7, {0x000000, 2048 * 1024} },
462
463 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
464 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
465 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
466 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600467 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700468
469 { 1, 1, 0x1, {0x000000, 4 * 1024} },
470 { 1, 1, 0x2, {0x000000, 8 * 1024} },
471 { 1, 1, 0x3, {0x000000, 16 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600472 { 1, 1, 0x4, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700473 { 1, 1, 0x5, {0x000000, 32 * 1024} },
474};
475
476static struct w25q_range w25q32_ranges[] = {
477 { X, X, 0, {0, 0} }, /* none */
478 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
479 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
480 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
481 { 0, 0, 0x4, {0x380000, 512 * 1024} },
482 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700483 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700484
485 { 0, 1, 0x1, {0x000000, 64 * 1024} },
486 { 0, 1, 0x2, {0x000000, 128 * 1024} },
487 { 0, 1, 0x3, {0x000000, 256 * 1024} },
488 { 0, 1, 0x4, {0x000000, 512 * 1024} },
489 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
490 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
491 { X, X, 0x7, {0x000000, 4096 * 1024} },
492
493 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
494 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
495 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
496 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
Paul Fagerburg90571582019-03-15 11:32:57 -0600497 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700498
499 { 1, 1, 0x1, {0x000000, 4 * 1024} },
500 { 1, 1, 0x2, {0x000000, 8 * 1024} },
501 { 1, 1, 0x3, {0x000000, 16 * 1024} },
502 { 1, 1, 0x4, {0x000000, 32 * 1024} },
503 { 1, 1, 0x5, {0x000000, 32 * 1024} },
504};
505
506static struct w25q_range w25q80_ranges[] = {
507 { X, X, 0, {0, 0} }, /* none */
508 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
509 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
510 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
511 { 0, 0, 0x4, {0x080000, 512 * 1024} },
512
513 { 0, 1, 0x1, {0x000000, 64 * 1024} },
514 { 0, 1, 0x2, {0x000000, 128 * 1024} },
515 { 0, 1, 0x3, {0x000000, 256 * 1024} },
516 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700517 { X, X, 0x6, {0x000000, 1024 * 1024} },
518 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700519
520 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
521 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
522 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
523 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
524 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
525
526 { 1, 1, 0x1, {0x000000, 4 * 1024} },
527 { 1, 1, 0x2, {0x000000, 8 * 1024} },
528 { 1, 1, 0x3, {0x000000, 16 * 1024} },
529 { 1, 1, 0x4, {0x000000, 32 * 1024} },
530 { 1, 1, 0x5, {0x000000, 32 * 1024} },
531};
532
David Hendricks2c4a76c2010-06-28 14:00:43 -0700533static struct w25q_range w25q64_ranges[] = {
534 { X, X, 0, {0, 0} }, /* none */
535
536 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
537 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
538 { 0, 0, 0x3, {0x780000, 512 * 1024} },
539 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
540 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
541 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
542
543 { 0, 1, 0x1, {0x000000, 128 * 1024} },
544 { 0, 1, 0x2, {0x000000, 256 * 1024} },
545 { 0, 1, 0x3, {0x000000, 512 * 1024} },
546 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
547 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
548 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
549 { X, X, 0x7, {0x000000, 8192 * 1024} },
550
551 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
552 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
553 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
554 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
555 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
556
557 { 1, 1, 0x1, {0x000000, 4 * 1024} },
558 { 1, 1, 0x2, {0x000000, 8 * 1024} },
559 { 1, 1, 0x3, {0x000000, 16 * 1024} },
560 { 1, 1, 0x4, {0x000000, 32 * 1024} },
561 { 1, 1, 0x5, {0x000000, 32 * 1024} },
562};
563
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700564static struct w25q_range w25rq128_cmp0_ranges[] = {
565 { X, X, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530566
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700567 { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
568 { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
569 { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
570 { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
571 { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
572 { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530573
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700574 { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
575 { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
576 { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
577 { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
578 { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
579 { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530580
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700581 { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530582
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700583 { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
584 { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
585 { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
586 { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
587 { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
588
589 { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
590 { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
591 { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
592 { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
593 { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
594};
595
596static struct w25q_range w25rq128_cmp1_ranges[] = {
597 { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
598
599 { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
600 { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
601 { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
602 { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
603 { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
604 { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
605
606 { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
607 { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
608 { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
609 { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
610 { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
611 { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
612
613 { X, X, 0x7, {0x000000, 0} }, /* NONE */
614
615 { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
616 { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
617 { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
618 { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
619 { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
620
621 { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
622 { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
623 { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
624 { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
625 { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530626};
627
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800628static struct w25q_range w25rq256_cmp0_ranges[] = {
629 { X, X, 0x0, {0x0000000, 0x0000000} }, /* NONE */
630
631 { X, 0, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* Upper 1/512 */
632 { X, 0, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* Upper 1/256 */
633 { X, 0, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* Upper 1/128 */
634 { X, 0, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* Upper 1/64 */
635 { X, 0, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* Upper 1/32 */
636 { X, 0, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* Upper 1/16 */
637 { X, 0, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* Upper 1/8 */
638 { X, 0, 0x8, {0x1800000, 64 * 128 * 1024} }, /* Upper 1/4 */
639 { X, 0, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
640
641 { X, 1, 0x1, {0x0000000, 64 * 1 * 1024} }, /* Lower 1/512 */
642 { X, 1, 0x2, {0x0000000, 64 * 2 * 1024} }, /* Lower 1/256 */
643 { X, 1, 0x3, {0x0000000, 64 * 4 * 1024} }, /* Lower 1/128 */
644 { X, 1, 0x4, {0x0000000, 64 * 8 * 1024} }, /* Lower 1/64 */
645 { X, 1, 0x5, {0x0000000, 64 * 16 * 1024} }, /* Lower 1/32 */
646 { X, 1, 0x6, {0x0000000, 64 * 32 * 1024} }, /* Lower 1/16 */
647 { X, 1, 0x7, {0x0000000, 64 * 64 * 1024} }, /* Lower 1/8 */
648 { X, 1, 0x8, {0x0000000, 64 * 128 * 1024} }, /* Lower 1/4 */
649 { X, 1, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
650
651 { X, X, 0xa, {0x0000000, 64 * 512 * 1024} }, /* ALL */
652 { X, X, 0xb, {0x0000000, 64 * 512 * 1024} }, /* ALL */
653 { X, X, 0xc, {0x0000000, 64 * 512 * 1024} }, /* ALL */
654 { X, X, 0xd, {0x0000000, 64 * 512 * 1024} }, /* ALL */
655 { X, X, 0xe, {0x0000000, 64 * 512 * 1024} }, /* ALL */
656 { X, X, 0xf, {0x0000000, 64 * 512 * 1024} }, /* ALL */
657};
658
659static struct w25q_range w25rq256_cmp1_ranges[] = {
660 { X, X, 0x0, {0x0000000, 64 * 512 * 1024} }, /* ALL */
661
662 { X, 0, 0x1, {0x0000000, 64 * 511 * 1024} }, /* Lower 511/512 */
663 { X, 0, 0x2, {0x0000000, 64 * 510 * 1024} }, /* Lower 255/256 */
664 { X, 0, 0x3, {0x0000000, 64 * 508 * 1024} }, /* Lower 127/128 */
665 { X, 0, 0x4, {0x0000000, 64 * 504 * 1024} }, /* Lower 63/64 */
666 { X, 0, 0x5, {0x0000000, 64 * 496 * 1024} }, /* Lower 31/32 */
667 { X, 0, 0x6, {0x0000000, 64 * 480 * 1024} }, /* Lower 15/16 */
668 { X, 0, 0x7, {0x0000000, 64 * 448 * 1024} }, /* Lower 7/8 */
669 { X, 0, 0x8, {0x0000000, 64 * 384 * 1024} }, /* Lower 3/4 */
670 { X, 0, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
671
672 { X, 1, 0x1, {0x0010000, 64 * 511 * 1024} }, /* Upper 511/512 */
673 { X, 1, 0x2, {0x0020000, 64 * 510 * 1024} }, /* Upper 255/256 */
674 { X, 1, 0x3, {0x0040000, 64 * 508 * 1024} }, /* Upper 127/128 */
675 { X, 1, 0x4, {0x0080000, 64 * 504 * 1024} }, /* Upper 63/64 */
676 { X, 1, 0x5, {0x0100000, 64 * 496 * 1024} }, /* Upper 31/32 */
677 { X, 1, 0x6, {0x0200000, 64 * 480 * 1024} }, /* Upper 15/16 */
678 { X, 1, 0x7, {0x0400000, 64 * 448 * 1024} }, /* Upper 7/8 */
679 { X, 1, 0x8, {0x0800000, 64 * 384 * 1024} }, /* Upper 3/4 */
680 { X, 1, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
681
682 { X, X, 0xa, {0x0000000, 0x0000000} }, /* NONE */
683 { X, X, 0xb, {0x0000000, 0x0000000} }, /* NONE */
684 { X, X, 0xc, {0x0000000, 0x0000000} }, /* NONE */
685 { X, X, 0xd, {0x0000000, 0x0000000} }, /* NONE */
686 { X, X, 0xe, {0x0000000, 0x0000000} }, /* NONE */
687 { X, X, 0xf, {0x0000000, 0x0000000} }, /* NONE */
688};
689
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800690struct w25q_range w25x10_ranges[] = {
691 { X, X, 0, {0, 0} }, /* none */
692 { 0, 0, 0x1, {0x010000, 64 * 1024} },
693 { 0, 1, 0x1, {0x000000, 64 * 1024} },
694 { X, X, 0x2, {0x000000, 128 * 1024} },
695 { X, X, 0x3, {0x000000, 128 * 1024} },
696};
697
698struct w25q_range w25x20_ranges[] = {
699 { X, X, 0, {0, 0} }, /* none */
700 { 0, 0, 0x1, {0x030000, 64 * 1024} },
701 { 0, 0, 0x2, {0x020000, 128 * 1024} },
702 { 0, 1, 0x1, {0x000000, 64 * 1024} },
703 { 0, 1, 0x2, {0x000000, 128 * 1024} },
704 { 0, X, 0x3, {0x000000, 256 * 1024} },
705};
706
David Hendricks470ca952010-08-13 14:01:53 -0700707struct w25q_range w25x40_ranges[] = {
708 { X, X, 0, {0, 0} }, /* none */
709 { 0, 0, 0x1, {0x070000, 64 * 1024} },
710 { 0, 0, 0x2, {0x060000, 128 * 1024} },
711 { 0, 0, 0x3, {0x040000, 256 * 1024} },
712 { 0, 1, 0x1, {0x000000, 64 * 1024} },
713 { 0, 1, 0x2, {0x000000, 128 * 1024} },
714 { 0, 1, 0x3, {0x000000, 256 * 1024} },
715 { 0, X, 0x4, {0x000000, 512 * 1024} },
David Hendricksb389abb2016-06-17 16:47:00 -0700716 { 0, X, 0x5, {0x000000, 512 * 1024} },
717 { 0, X, 0x6, {0x000000, 512 * 1024} },
718 { 0, X, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700719};
720
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800721struct w25q_range w25x80_ranges[] = {
722 { X, X, 0, {0, 0} }, /* none */
723 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
724 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
725 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
726 { 0, 0, 0x4, {0x080000, 512 * 1024} },
727 { 0, 1, 0x1, {0x000000, 64 * 1024} },
728 { 0, 1, 0x2, {0x000000, 128 * 1024} },
729 { 0, 1, 0x3, {0x000000, 256 * 1024} },
730 { 0, 1, 0x4, {0x000000, 512 * 1024} },
731 { 0, X, 0x5, {0x000000, 1024 * 1024} },
732 { 0, X, 0x6, {0x000000, 1024 * 1024} },
733 { 0, X, 0x7, {0x000000, 1024 * 1024} },
734};
735
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600736static struct w25q_range gd25q40_cmp0_ranges[] = {
737 { X, X, 0, {0, 0} }, /* None */
738 { 0, 0, 0x1, {0x070000, 64 * 1024} },
739 { 0, 0, 0x2, {0x060000, 128 * 1024} },
740 { 0, 0, 0x3, {0x040000, 256 * 1024} },
741 { 0, 1, 0x1, {0x000000, 64 * 1024} },
742 { 0, 1, 0x2, {0x000000, 128 * 1024} },
743 { 0, 1, 0x3, {0x000000, 256 * 1024} },
744 { 0, X, 0x4, {0x000000, 512 * 1024} }, /* All */
745 { 0, X, 0x5, {0x000000, 512 * 1024} }, /* All */
746 { 0, X, 0x6, {0x000000, 512 * 1024} }, /* All */
747 { 0, X, 0x7, {0x000000, 512 * 1024} }, /* All */
748 { 1, 0, 0x1, {0x07F000, 4 * 1024} },
749 { 1, 0, 0x2, {0x07E000, 8 * 1024} },
750 { 1, 0, 0x3, {0x07C000, 16 * 1024} },
751 { 1, 0, 0x4, {0x078000, 32 * 1024} },
752 { 1, 0, 0x5, {0x078000, 32 * 1024} },
753 { 1, 0, 0x6, {0x078000, 32 * 1024} },
754 { 1, 1, 0x1, {0x000000, 4 * 1024} },
755 { 1, 1, 0x2, {0x000000, 8 * 1024} },
756 { 1, 1, 0x3, {0x000000, 16 * 1024} },
757 { 1, 1, 0x4, {0x000000, 32 * 1024} },
758 { 1, 1, 0x5, {0x000000, 32 * 1024} },
759 { 1, 1, 0x6, {0x000000, 32 * 1024} },
760 { 1, X, 0x7, {0x000000, 512 * 1024} }, /* All */
761};
762
763static struct w25q_range gd25q40_cmp1_ranges[] = {
764 { X, X, 0x0, {0x000000, 512 * 1024} }, /* ALL */
765 { 0, 0, 0x1, {0x000000, 448 * 1024} },
766 { 0, 0, 0x2, {0x000000, 384 * 1024} },
767 { 0, 0, 0x3, {0x000000, 256 * 1024} },
768
769 { 0, 1, 0x1, {0x010000, 448 * 1024} },
770 { 0, 1, 0x2, {0x020000, 384 * 1024} },
771 { 0, 1, 0x3, {0x040000, 256 * 1024} },
772
773 { 0, X, 0x4, {0x000000, 0} }, /* None */
774 { 0, X, 0x5, {0x000000, 0} }, /* None */
775 { 0, X, 0x6, {0x000000, 0} }, /* None */
776 { 0, X, 0x7, {0x000000, 0} }, /* None */
777
778 { 1, 0, 0x1, {0x000000, 508 * 1024} },
779 { 1, 0, 0x2, {0x000000, 504 * 1024} },
780 { 1, 0, 0x3, {0x000000, 496 * 1024} },
781 { 1, 0, 0x4, {0x000000, 480 * 1024} },
782 { 1, 0, 0x5, {0x000000, 480 * 1024} },
783 { 1, 0, 0x6, {0x000000, 480 * 1024} },
784
785 { 1, 1, 0x1, {0x001000, 508 * 1024} },
786 { 1, 1, 0x2, {0x002000, 504 * 1024} },
787 { 1, 1, 0x3, {0x004000, 496 * 1024} },
788 { 1, 1, 0x4, {0x008000, 480 * 1024} },
789 { 1, 1, 0x5, {0x008000, 480 * 1024} },
790 { 1, 1, 0x6, {0x008000, 480 * 1024} },
791
792 { 1, X, 0x7, {0x000000, 0} }, /* None */
793};
794
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700795static struct w25q_range gd25q64_ranges[] = {
796 { X, X, 0, {0, 0} }, /* none */
797 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
798 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
799 { 0, 0, 0x3, {0x780000, 512 * 1024} },
800 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
801 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
802 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
803
804 { 0, 1, 0x1, {0x000000, 128 * 1024} },
805 { 0, 1, 0x2, {0x000000, 256 * 1024} },
806 { 0, 1, 0x3, {0x000000, 512 * 1024} },
807 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
808 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
809 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
810 { X, X, 0x7, {0x000000, 8192 * 1024} },
811
812 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
813 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
814 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
815 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
816 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
817 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
818
819 { 1, 1, 0x1, {0x000000, 4 * 1024} },
820 { 1, 1, 0x2, {0x000000, 8 * 1024} },
821 { 1, 1, 0x3, {0x000000, 16 * 1024} },
822 { 1, 1, 0x4, {0x000000, 32 * 1024} },
823 { 1, 1, 0x5, {0x000000, 32 * 1024} },
824 { 1, 1, 0x6, {0x000000, 32 * 1024} },
825};
826
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800827static struct w25q_range a25l040_ranges[] = {
828 { X, X, 0x0, {0, 0} }, /* none */
829 { X, X, 0x1, {0x70000, 64 * 1024} },
830 { X, X, 0x2, {0x60000, 128 * 1024} },
831 { X, X, 0x3, {0x40000, 256 * 1024} },
832 { X, X, 0x4, {0x00000, 512 * 1024} },
833 { X, X, 0x5, {0x00000, 512 * 1024} },
834 { X, X, 0x6, {0x00000, 512 * 1024} },
835 { X, X, 0x7, {0x00000, 512 * 1024} },
836};
837
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700838static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530839{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100840 if (flash->chip->read_status)
841 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530842 else
843 return spi_read_status_register(flash);
844}
845
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700846static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530847{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100848 if (flash->chip->write_status)
849 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530850 else
851 return spi_write_status_register(flash, status);
852}
853
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700854/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700855static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700856{
857 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
858 unsigned char readarr[2];
859 int ret;
860
861 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700862 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700863 if (ret) {
864 /*
865 * FIXME: make this a benign failure for now in case we are
866 * unable to execute the opcode
867 */
868 msg_cdbg("RDSR2 failed!\n");
869 readarr[0] = 0x00;
870 }
871
872 return readarr[0];
873}
874
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800875/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700876static int w25_range_table(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800877 struct w25q_range **w25q_ranges,
878 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700879{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800880 *w25q_ranges = 0;
881 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700882
Patrick Georgif3fa2992017-02-02 16:24:44 +0100883 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700884 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100885 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800886 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800887 *w25q_ranges = w25x10_ranges;
888 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800889 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800890 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800891 *w25q_ranges = w25x20_ranges;
892 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800893 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800894 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800895 *w25q_ranges = w25x40_ranges;
896 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700897 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800898 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800899 *w25q_ranges = w25x80_ranges;
900 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800901 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100902 case WINBOND_NEX_W25Q80_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800903 *w25q_ranges = w25q80_ranges;
904 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700905 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100906 case WINBOND_NEX_W25Q16_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800907 *w25q_ranges = w25q16_ranges;
908 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700909 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100910 case WINBOND_NEX_W25Q32_V:
911 case WINBOND_NEX_W25Q32_W:
Edward O'Callaghand80cf712019-05-24 22:06:36 +1000912 case WINBOND_NEX_W25Q32JW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800913 *w25q_ranges = w25q32_ranges;
914 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700915 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100916 case WINBOND_NEX_W25Q64_V:
917 case WINBOND_NEX_W25Q64_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800918 *w25q_ranges = w25q64_ranges;
919 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700920 break;
Alan Green77a95de2019-07-01 16:40:39 +1000921 case WINBOND_NEX_W25Q128_V_M:
Patrick Georgicc04a452017-02-06 12:14:43 +0100922 case WINBOND_NEX_W25Q128_V:
923 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700924 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700925 /* CMP == 1 */
926 *w25q_ranges = w25rq128_cmp1_ranges;
927 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
928 } else {
929 /* CMP == 0 */
930 *w25q_ranges = w25rq128_cmp0_ranges;
931 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
932 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530933 break;
Alan Green77a95de2019-07-01 16:40:39 +1000934 case WINBOND_NEX_W25Q256JV_M:
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800935 if (w25q_read_status_register_2(flash) & (1 << 6)) {
936 /* CMP == 1 */
937 *w25q_ranges = w25rq256_cmp1_ranges;
938 *num_entries = ARRAY_SIZE(w25rq256_cmp1_ranges);
939 } else {
940 /* CMP == 0 */
941 *w25q_ranges = w25rq256_cmp0_ranges;
942 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
943 }
944 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700945 default:
946 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
947 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100948 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700949 return -1;
950 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700951 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700952 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100953 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800954 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800955 *w25q_ranges = en25f40_ranges;
956 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700957 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700958 case EON_EN25Q40:
959 *w25q_ranges = en25q40_ranges;
960 *num_entries = ARRAY_SIZE(en25q40_ranges);
961 break;
962 case EON_EN25Q80:
963 *w25q_ranges = en25q80_ranges;
964 *num_entries = ARRAY_SIZE(en25q80_ranges);
965 break;
966 case EON_EN25Q32:
967 *w25q_ranges = en25q32_ranges;
968 *num_entries = ARRAY_SIZE(en25q32_ranges);
969 break;
970 case EON_EN25Q64:
971 *w25q_ranges = en25q64_ranges;
972 *num_entries = ARRAY_SIZE(en25q64_ranges);
973 break;
974 case EON_EN25Q128:
975 *w25q_ranges = en25q128_ranges;
976 *num_entries = ARRAY_SIZE(en25q128_ranges);
977 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600978 case EON_EN25S64:
979 *w25q_ranges = en25s64_ranges;
980 *num_entries = ARRAY_SIZE(en25s64_ranges);
981 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700982 default:
983 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
984 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100985 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -0700986 return -1;
987 }
988 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800989 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100990 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800991 case MACRONIX_MX25L1005:
992 *w25q_ranges = mx25l1005_ranges;
993 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
994 break;
995 case MACRONIX_MX25L2005:
996 *w25q_ranges = mx25l2005_ranges;
997 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
998 break;
999 case MACRONIX_MX25L4005:
1000 *w25q_ranges = mx25l4005_ranges;
1001 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
1002 break;
1003 case MACRONIX_MX25L8005:
1004 *w25q_ranges = mx25l8005_ranges;
1005 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
1006 break;
1007 case MACRONIX_MX25L1605:
1008 /* FIXME: MX25L1605 and MX25L1605D have different write
1009 * protection capabilities, but share IDs */
1010 *w25q_ranges = mx25l1605d_ranges;
1011 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
1012 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001013 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001014 *w25q_ranges = mx25l3205d_ranges;
1015 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -07001016 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001017 case MACRONIX_MX25U3235E:
1018 *w25q_ranges = mx25u3235e_ranges;
1019 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
1020 break;
Jongpil66a96492014-08-14 17:59:06 +09001021 case MACRONIX_MX25U6435E:
1022 *w25q_ranges = mx25u6435e_ranges;
1023 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
1024 break;
Alan Greendc0792e2019-07-01 15:01:34 +10001025 case MACRONIX_MX25U12835E:
1026 *w25q_ranges = mx25u12835e_ranges;
1027 *num_entries = ARRAY_SIZE(mx25u12835e_ranges);
Alex Lu831c6092017-11-02 23:19:34 -07001028 break;
David Hendricksac72e362010-08-16 18:20:03 -07001029 default:
1030 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1031 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001032 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -07001033 return -1;
1034 }
1035 break;
David Hendricksbfa624b2012-07-24 12:47:59 -07001036 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001037 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -07001038 case ST_N25Q064__1E:
1039 case ST_N25Q064__3E:
1040 *w25q_ranges = n25q064_ranges;
1041 *num_entries = ARRAY_SIZE(n25q064_ranges);
1042 break;
1043 default:
1044 msg_cerr("%s() %d: Micron flash chip mismatch"
1045 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001046 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -07001047 return -1;
1048 }
1049 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001050 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001051 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -07001052 case GIGADEVICE_GD25LQ32:
1053 *w25q_ranges = w25q32_ranges;
1054 *num_entries = ARRAY_SIZE(w25q32_ranges);
1055 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001056 case GIGADEVICE_GD25Q40:
1057 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1058 /* CMP == 1 */
1059 *w25q_ranges = gd25q40_cmp1_ranges;
1060 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
1061 } else {
1062 *w25q_ranges = gd25q40_cmp0_ranges;
1063 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
1064 }
1065 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001066 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -06001067 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001068 *w25q_ranges = gd25q64_ranges;
1069 *num_entries = ARRAY_SIZE(gd25q64_ranges);
1070 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001071 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06001072 case GIGADEVICE_GD25LQ128CD:
Martin Roth1fd87ed2017-02-27 20:50:50 -07001073 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1074 /* CMP == 1 */
1075 *w25q_ranges = w25rq128_cmp1_ranges;
1076 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1077 } else {
1078 /* CMP == 0 */
1079 *w25q_ranges = w25rq128_cmp0_ranges;
1080 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1081 }
1082 break;
Duncan Laurie0c383552019-03-16 12:35:16 -07001083 case GIGADEVICE_GD25Q256D:
1084 *w25q_ranges = w25rq256_cmp0_ranges;
1085 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
1086 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001087 default:
1088 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1089 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001090 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -07001091 return -1;
1092 }
1093 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001094 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001095 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001096 case AMIC_A25L040:
1097 *w25q_ranges = a25l040_ranges;
1098 *num_entries = ARRAY_SIZE(a25l040_ranges);
1099 break;
1100 default:
1101 msg_cerr("%s() %d: AMIC flash chip mismatch"
1102 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001103 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001104 return -1;
1105 }
1106 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001107 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001108 switch(flash->chip->model_id) {
Edward O'Callaghan1fa87e02019-05-03 02:27:24 -04001109 case ATMEL_AT25SF128A:
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001110 case ATMEL_AT25SL128A:
1111 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1112 /* CMP == 1 */
1113 *w25q_ranges = w25rq128_cmp1_ranges;
1114 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1115 } else {
1116 /* CMP == 0 */
1117 *w25q_ranges = w25rq128_cmp0_ranges;
1118 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1119 }
1120 break;
1121 default:
1122 msg_cerr("%s() %d: Atmel flash chip mismatch"
1123 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001124 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001125 return -1;
1126 }
1127 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001128 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001129 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001130 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001131 return -1;
1132 }
1133
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001134 return 0;
1135}
1136
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001137int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001138 unsigned int start, unsigned int len,
1139 struct w25q_status *status)
1140{
1141 struct w25q_range *w25q_ranges;
1142 int i, range_found = 0;
1143 int num_entries;
1144
1145 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001146 for (i = 0; i < num_entries; i++) {
1147 struct wp_range *r = &w25q_ranges[i].range;
1148
1149 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1150 start, len, r->start, r->len);
1151 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -07001152 status->bp0 = w25q_ranges[i].bp & 1;
1153 status->bp1 = w25q_ranges[i].bp >> 1;
1154 status->bp2 = w25q_ranges[i].bp >> 2;
1155 status->tb = w25q_ranges[i].tb;
1156 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001157
1158 range_found = 1;
1159 break;
1160 }
1161 }
1162
1163 if (!range_found) {
1164 msg_cerr("matching range not found\n");
1165 return -1;
1166 }
David Hendricksd494b0a2010-08-16 16:28:50 -07001167 return 0;
1168}
1169
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001170int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001171 const struct w25q_status *status,
1172 unsigned int *start, unsigned int *len)
1173{
1174 struct w25q_range *w25q_ranges;
1175 int i, status_found = 0;
1176 int num_entries;
1177
1178 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1179 for (i = 0; i < num_entries; i++) {
1180 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001181 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001182
1183 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1184 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
1185 bp, w25q_ranges[i].bp,
1186 status->tb, w25q_ranges[i].tb,
1187 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001188 table_bp = w25q_ranges[i].bp;
1189 table_tb = w25q_ranges[i].tb;
1190 table_sec = w25q_ranges[i].sec;
1191 if ((bp == table_bp || table_bp == X) &&
1192 (status->tb == table_tb || table_tb == X) &&
1193 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001194 *start = w25q_ranges[i].range.start;
1195 *len = w25q_ranges[i].range.len;
1196
1197 status_found = 1;
1198 break;
1199 }
1200 }
1201
1202 if (!status_found) {
1203 msg_cerr("matching status not found\n");
1204 return -1;
1205 }
1206 return 0;
1207}
1208
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001209/* Given a [start, len], this function calls w25_range_to_status() to convert
1210 * it to flash-chip-specific range bits, then sets into status register.
1211 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001212static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001213 unsigned int start, unsigned int len)
1214{
1215 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001216 int tmp = 0;
1217 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001218
1219 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301220 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001221 memcpy(&status, &tmp, 1);
1222 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1223
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001224 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001225
1226 msg_cdbg("status.busy: %x\n", status.busy);
1227 msg_cdbg("status.wel: %x\n", status.wel);
1228 msg_cdbg("status.bp0: %x\n", status.bp0);
1229 msg_cdbg("status.bp1: %x\n", status.bp1);
1230 msg_cdbg("status.bp2: %x\n", status.bp2);
1231 msg_cdbg("status.tb: %x\n", status.tb);
1232 msg_cdbg("status.sec: %x\n", status.sec);
1233 msg_cdbg("status.srp0: %x\n", status.srp0);
1234
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001235 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301236 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001237
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301238 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001239 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1240 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001241 return 0;
1242 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001243 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001244 expected, tmp);
1245 return 1;
1246 }
David Hendricksf7924d12010-06-10 21:26:44 -07001247}
1248
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001249/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001250static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001251{
1252 struct w25q_status status;
1253 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001254 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001255 int ret = 0;
1256
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001257 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301258 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001259 memcpy(&status, &tmp, 1);
1260 msg_cinfo("WP: status: 0x%02x\n", tmp);
1261 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1262 msg_cinfo("WP: write protect is %s.\n",
1263 status.srp0 ? "enabled" : "disabled");
1264
1265 msg_cinfo("WP: write protect range: ");
1266 if (w25_status_to_range(flash, &status, &start, &len)) {
1267 msg_cinfo("(cannot resolve the range)\n");
1268 ret = -1;
1269 } else {
1270 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1271 }
1272
1273 return ret;
1274}
1275
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001276static int w25q_large_range_to_status(const struct flashctx *flash,
1277 unsigned int start, unsigned int len,
1278 struct w25q_status_large *status)
1279{
1280 struct w25q_range *w25q_ranges;
1281 int i, range_found = 0;
1282 int num_entries;
1283
1284 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1285 return -1;
1286 for (i = 0; i < num_entries; i++) {
1287 struct wp_range *r = &w25q_ranges[i].range;
1288
1289 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1290 start, len, r->start, r->len);
1291 if ((start == r->start) && (len == r->len)) {
1292 status->bp0 = w25q_ranges[i].bp & 1;
1293 status->bp1 = w25q_ranges[i].bp >> 1;
1294 status->bp2 = w25q_ranges[i].bp >> 2;
1295 status->bp3 = w25q_ranges[i].bp >> 3;
1296 status->tb = w25q_ranges[i].tb;
1297
1298 range_found = 1;
1299 break;
1300 }
1301 }
1302
1303 if (!range_found) {
1304 msg_cerr("matching range not found\n");
1305 return -1;
1306 }
1307 return 0;
1308}
1309
1310static int w25_large_status_to_range(const struct flashctx *flash,
1311 const struct w25q_status_large *status,
1312 unsigned int *start, unsigned int *len)
1313{
1314 struct w25q_range *w25q_ranges;
1315 int i, status_found = 0;
1316 int num_entries;
1317
1318 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1319 return -1;
1320 for (i = 0; i < num_entries; i++) {
1321 int bp;
1322 int table_bp, table_tb;
1323
1324 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2) |
1325 (status->bp3 << 3);
1326 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x\n",
1327 bp, w25q_ranges[i].bp,
1328 status->tb, w25q_ranges[i].tb);
1329 table_bp = w25q_ranges[i].bp;
1330 table_tb = w25q_ranges[i].tb;
1331 if ((bp == table_bp || table_bp == X) &&
1332 (status->tb == table_tb || table_tb == X)) {
1333 *start = w25q_ranges[i].range.start;
1334 *len = w25q_ranges[i].range.len;
1335
1336 status_found = 1;
1337 break;
1338 }
1339 }
1340
1341 if (!status_found) {
1342 msg_cerr("matching status not found\n");
1343 return -1;
1344 }
1345 return 0;
1346}
1347
1348/* Given a [start, len], this function calls w25_range_to_status() to convert
1349 * it to flash-chip-specific range bits, then sets into status register.
1350 * Returns 0 if successful, -1 on error, and 1 if reading back was different.
1351 */
1352static int w25q_large_set_range(const struct flashctx *flash,
1353 unsigned int start, unsigned int len)
1354{
1355 struct w25q_status_large status;
1356 int tmp;
1357 int expected = 0;
1358
1359 memset(&status, 0, sizeof(status));
1360 tmp = do_read_status(flash);
1361 memcpy(&status, &tmp, 1);
1362 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1363
1364 if (w25q_large_range_to_status(flash, start, len, &status))
1365 return -1;
1366
1367 msg_cdbg("status.busy: %x\n", status.busy);
1368 msg_cdbg("status.wel: %x\n", status.wel);
1369 msg_cdbg("status.bp0: %x\n", status.bp0);
1370 msg_cdbg("status.bp1: %x\n", status.bp1);
1371 msg_cdbg("status.bp2: %x\n", status.bp2);
1372 msg_cdbg("status.bp3: %x\n", status.bp3);
1373 msg_cdbg("status.tb: %x\n", status.tb);
1374 msg_cdbg("status.srp0: %x\n", status.srp0);
1375
1376 memcpy(&expected, &status, sizeof(status));
1377 do_write_status(flash, expected);
1378
1379 tmp = do_read_status(flash);
1380 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1381 if ((tmp & MASK_WP_AREA_LARGE) == (expected & MASK_WP_AREA_LARGE)) {
1382 return 0;
1383 } else {
1384 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1385 expected, tmp);
1386 return 1;
1387 }
1388}
1389
1390static int w25q_large_wp_status(const struct flashctx *flash)
1391{
1392 struct w25q_status_large sr1;
1393 struct w25q_status_2 sr2;
1394 uint8_t tmp[2];
1395 unsigned int start, len;
1396 int ret = 0;
1397
1398 memset(&sr1, 0, sizeof(sr1));
1399 tmp[0] = do_read_status(flash);
1400 memcpy(&sr1, &tmp[0], 1);
1401
1402 memset(&sr2, 0, sizeof(sr2));
1403 tmp[1] = w25q_read_status_register_2(flash);
1404 memcpy(&sr2, &tmp[1], 1);
1405
1406 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
1407 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1408 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1409 msg_cinfo("WP: write protect is %s.\n",
1410 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1411
1412 msg_cinfo("WP: write protect range: ");
1413 if (w25_large_status_to_range(flash, &sr1, &start, &len)) {
1414 msg_cinfo("(cannot resolve the range)\n");
1415 ret = -1;
1416 } else {
1417 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1418 }
1419
1420 return ret;
1421}
1422
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001423/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001424static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001425{
1426 struct w25q_status status;
1427 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001428 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001429
1430 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301431 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001432 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001433 memcpy(&status, &tmp, 1);
1434 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1435
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001436 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001437 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301438 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001439
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301440 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001441 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1442 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1443 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001444
1445 return 0;
1446}
1447
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001448static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001449 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001450{
1451 int ret;
1452
David Hendricks1c09f802012-10-03 11:03:48 -07001453 switch (wp_mode) {
1454 case WP_MODE_HARDWARE:
1455 ret = w25_set_srp0(flash, 1);
1456 break;
1457 default:
1458 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1459 return 1;
1460 }
1461
David Hendricksc801adb2010-12-09 16:58:56 -08001462 if (ret)
1463 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001464 return ret;
1465}
1466
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001467static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001468{
1469 int ret;
1470
1471 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001472 if (ret)
1473 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001474 return ret;
1475}
1476
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001477static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001478{
1479 struct w25q_range *w25q_ranges;
1480 int i, num_entries;
1481
1482 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1483 for (i = 0; i < num_entries; i++) {
1484 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1485 w25q_ranges[i].range.start,
1486 w25q_ranges[i].range.len);
1487 }
1488
1489 return 0;
1490}
1491
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001492static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001493{
1494 struct w25q_status sr1;
1495 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001496 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001497 unsigned int start, len;
1498 int ret = 0;
1499
1500 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301501 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001502 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001503
David Hendricksf1bd8802012-10-30 11:37:57 -07001504 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001505 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001506 memcpy(&sr2, &tmp[1], 1);
1507
1508 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001509 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1510 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1511 msg_cinfo("WP: write protect is %s.\n",
1512 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1513
1514 msg_cinfo("WP: write protect range: ");
1515 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1516 msg_cinfo("(cannot resolve the range)\n");
1517 ret = -1;
1518 } else {
1519 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1520 }
1521
1522 return ret;
1523}
1524
1525/*
1526 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1527 * de-asserted after the first byte, then it acts like a JEDEC-standard
1528 * WRSR command. if /CS is asserted, then the next data byte is written
1529 * into status register 2.
1530 */
1531#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001532static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001533{
1534 int result;
1535 struct spi_command cmds[] = {
1536 {
1537 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1538 .writecnt = JEDEC_WREN_OUTSIZE,
1539 .writearr = (const unsigned char[]){ JEDEC_WREN },
1540 .readcnt = 0,
1541 .readarr = NULL,
1542 }, {
1543 .writecnt = W25Q_WRSR_OUTSIZE,
1544 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1545 .readcnt = 0,
1546 .readarr = NULL,
1547 }, {
1548 .writecnt = 0,
1549 .writearr = NULL,
1550 .readcnt = 0,
1551 .readarr = NULL,
1552 }};
1553
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001554 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001555 if (result) {
1556 msg_cerr("%s failed during command execution\n",
1557 __func__);
1558 }
1559
1560 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001561 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001562
1563 return result;
1564}
1565
1566/*
1567 * Set/clear the SRP1 bit in status register 2.
1568 * FIXME: make this more generic if other chips use the same SR2 layout
1569 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001570static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001571{
1572 struct w25q_status sr1;
1573 struct w25q_status_2 sr2;
1574 uint8_t tmp, expected;
1575
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301576 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001577 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001578 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001579 memcpy(&sr2, &tmp, 1);
1580
1581 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1582
1583 sr2.srp1 = enable ? 1 : 0;
1584
1585 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001586 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001587
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001588 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001589 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1590 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1591 return 1;
1592
1593 return 0;
1594}
1595
1596enum wp_mode get_wp_mode(const char *mode_str)
1597{
1598 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1599
1600 if (!strcasecmp(mode_str, "hardware"))
1601 wp_mode = WP_MODE_HARDWARE;
1602 else if (!strcasecmp(mode_str, "power_cycle"))
1603 wp_mode = WP_MODE_POWER_CYCLE;
1604 else if (!strcasecmp(mode_str, "permanent"))
1605 wp_mode = WP_MODE_PERMANENT;
1606
1607 return wp_mode;
1608}
1609
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001610static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001611 enum wp_mode wp_mode)
1612{
1613 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001614 struct w25q_status_2 sr2;
1615 uint8_t tmp;
1616
1617 switch (wp_mode) {
1618 case WP_MODE_HARDWARE:
1619 ret = w25_set_srp0(flash, 0);
1620 break;
1621 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001622 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001623 memcpy(&sr2, &tmp, 1);
1624 if (sr2.srp1) {
1625 msg_cerr("%s(): must disconnect power to disable "
1626 "write-protection\n", __func__);
1627 } else {
1628 ret = 0;
1629 }
1630 break;
1631 case WP_MODE_PERMANENT:
1632 msg_cerr("%s(): cannot disable permanent write-protection\n",
1633 __func__);
1634 break;
1635 default:
1636 msg_cerr("%s(): invalid mode specified\n", __func__);
1637 break;
1638 }
1639
1640 if (ret)
1641 msg_cerr("%s(): error=%d.\n", __func__, ret);
1642 return ret;
1643}
1644
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001645static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001646{
1647 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1648}
1649
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001650static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001651 enum wp_mode wp_mode)
1652{
1653 int ret = 1;
1654 struct w25q_status sr1;
1655 struct w25q_status_2 sr2;
1656 uint8_t tmp;
1657
1658 switch (wp_mode) {
1659 case WP_MODE_HARDWARE:
1660 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1661 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1662 __func__);
1663 break;
1664 }
1665
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301666 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001667 memcpy(&sr1, &tmp, 1);
1668 if (sr1.srp0)
1669 ret = 0;
1670 else
1671 ret = w25_set_srp0(flash, 1);
1672
1673 break;
1674 case WP_MODE_POWER_CYCLE:
1675 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1676 msg_cerr("%s(): cannot disable hardware WP mode\n",
1677 __func__);
1678 break;
1679 }
1680
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001681 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001682 memcpy(&sr2, &tmp, 1);
1683 if (sr2.srp1)
1684 ret = 0;
1685 else
1686 ret = w25q_set_srp1(flash, 1);
1687
1688 break;
1689 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301690 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001691 memcpy(&sr1, &tmp, 1);
1692 if (sr1.srp0 == 0) {
1693 ret = w25_set_srp0(flash, 1);
1694 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001695 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001696 "permanent WP\n", __func__);
1697 break;
1698 }
1699 }
1700
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001701 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001702 memcpy(&sr2, &tmp, 1);
1703 if (sr2.srp1 == 0) {
1704 ret = w25q_set_srp1(flash, 1);
1705 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001706 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001707 "permanent WP\n", __func__);
1708 break;
1709 }
1710 }
1711
1712 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001713 default:
1714 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1715 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001716 }
1717
1718 if (ret)
1719 msg_cerr("%s(): error=%d.\n", __func__, ret);
1720 return ret;
1721}
1722
David Hendricksc3496092014-11-13 17:20:55 -08001723/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001724uint8_t mx25l_read_config_register(const struct flashctx *flash)
David Hendricksc3496092014-11-13 17:20:55 -08001725{
1726 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1727 unsigned char readarr[2]; /* leave room for dummy byte */
1728 int ret;
1729
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001730 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
David Hendricksc3496092014-11-13 17:20:55 -08001731 if (ret) {
Duncan Laurie870d8af2019-01-09 18:05:23 -08001732 msg_cdbg("RDCR failed!\n");
David Hendricksc3496092014-11-13 17:20:55 -08001733 readarr[0] = 0x00;
1734 }
1735
1736 return readarr[0];
1737}
David Hendricks1c09f802012-10-03 11:03:48 -07001738/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001739struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001740 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001741 .set_range = w25_set_range,
1742 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001743 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001744 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001745
1746};
1747
1748/* W25Q series has features such as a second status register and SFDP */
1749struct wp wp_w25q = {
1750 .list_ranges = w25_list_ranges,
1751 .set_range = w25_set_range,
1752 .enable = w25q_enable_writeprotect,
1753 /*
1754 * By default, disable hardware write-protection. We may change
1755 * this later if we want to add fine-grained write-protect disable
1756 * as a command-line option.
1757 */
1758 .disable = w25q_disable_writeprotect_default,
1759 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001760};
David Hendrickse0512a72014-07-15 20:30:47 -07001761
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001762/* W25Q large series has 4 block-protect bits */
1763struct wp wp_w25q_large = {
1764 .list_ranges = w25_list_ranges,
1765 .set_range = w25q_large_set_range,
1766 .enable = w25q_enable_writeprotect,
1767 /*
1768 * By default, disable hardware write-protection. We may change
1769 * this later if we want to add fine-grained write-protect disable
1770 * as a command-line option.
1771 */
1772 .disable = w25q_disable_writeprotect_default,
1773 .wp_status = w25q_large_wp_status,
1774};
1775
David Hendricksaf3944a2014-07-28 18:37:40 -07001776struct generic_range gd25q32_cmp0_ranges[] = {
1777 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001778 { { }, 0x00, {0, 0} },
1779 { { }, 0x08, {0, 0} },
1780 { { }, 0x10, {0, 0} },
1781 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001782
David Hendricks148a4bf2015-03-13 21:02:42 -07001783 { { }, 0x01, {0x3f0000, 64 * 1024} },
1784 { { }, 0x02, {0x3e0000, 128 * 1024} },
1785 { { }, 0x03, {0x3c0000, 256 * 1024} },
1786 { { }, 0x04, {0x380000, 512 * 1024} },
1787 { { }, 0x05, {0x300000, 1024 * 1024} },
1788 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001789
David Hendricks148a4bf2015-03-13 21:02:42 -07001790 { { }, 0x09, {0x000000, 64 * 1024} },
1791 { { }, 0x0a, {0x000000, 128 * 1024} },
1792 { { }, 0x0b, {0x000000, 256 * 1024} },
1793 { { }, 0x0c, {0x000000, 512 * 1024} },
1794 { { }, 0x0d, {0x000000, 1024 * 1024} },
1795 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001796
1797 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001798 { { }, 0x07, {0x000000, 4096 * 1024} },
1799 { { }, 0x0f, {0x000000, 4096 * 1024} },
1800 { { }, 0x17, {0x000000, 4096 * 1024} },
1801 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001802
David Hendricks148a4bf2015-03-13 21:02:42 -07001803 { { }, 0x11, {0x3ff000, 4 * 1024} },
1804 { { }, 0x12, {0x3fe000, 8 * 1024} },
1805 { { }, 0x13, {0x3fc000, 16 * 1024} },
1806 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1807 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1808 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001809
David Hendricks148a4bf2015-03-13 21:02:42 -07001810 { { }, 0x19, {0x000000, 4 * 1024} },
1811 { { }, 0x1a, {0x000000, 8 * 1024} },
1812 { { }, 0x1b, {0x000000, 16 * 1024} },
1813 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1814 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1815 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001816};
1817
1818struct generic_range gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001819 /* All, bp4 and bp3 => don't care */
1820 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1821 { { }, 0x08, {0x000000, 4096 * 1024} },
1822 { { }, 0x10, {0x000000, 4096 * 1024} },
1823 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001824
David Hendricks148a4bf2015-03-13 21:02:42 -07001825 { { }, 0x01, {0x000000, 4032 * 1024} },
1826 { { }, 0x02, {0x000000, 3968 * 1024} },
1827 { { }, 0x03, {0x000000, 3840 * 1024} },
1828 { { }, 0x04, {0x000000, 3584 * 1024} },
1829 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1830 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001831
David Hendricks148a4bf2015-03-13 21:02:42 -07001832 { { }, 0x09, {0x010000, 4032 * 1024} },
1833 { { }, 0x0a, {0x020000, 3968 * 1024} },
1834 { { }, 0x0b, {0x040000, 3840 * 1024} },
1835 { { }, 0x0c, {0x080000, 3584 * 1024} },
1836 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1837 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001838
Martin Roth563a1fe2017-04-18 14:26:27 -06001839 /* None, bp4 and bp3 => don't care */
1840 { { }, 0x07, {0, 0} }, /* None */
1841 { { }, 0x0f, {0, 0} },
1842 { { }, 0x17, {0, 0} },
1843 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001844
David Hendricks148a4bf2015-03-13 21:02:42 -07001845 { { }, 0x11, {0x000000, 4092 * 1024} },
1846 { { }, 0x12, {0x000000, 4088 * 1024} },
1847 { { }, 0x13, {0x000000, 4080 * 1024} },
1848 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1849 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1850 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001851
David Hendricks148a4bf2015-03-13 21:02:42 -07001852 { { }, 0x19, {0x001000, 4092 * 1024} },
1853 { { }, 0x1a, {0x002000, 4088 * 1024} },
1854 { { }, 0x1b, {0x040000, 4080 * 1024} },
1855 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1856 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1857 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001858};
1859
1860static struct generic_wp gd25q32_wp = {
1861 /* TODO: map second status register */
1862 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1863};
1864
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001865struct generic_range gd25q128_cmp0_ranges[] = {
1866 /* none, bp4 and bp3 => don't care, others = 0 */
1867 { { .tb = 0 }, 0x00, {0, 0} },
1868 { { .tb = 0 }, 0x08, {0, 0} },
1869 { { .tb = 0 }, 0x10, {0, 0} },
1870 { { .tb = 0 }, 0x18, {0, 0} },
1871
1872 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1873 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1874 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1875 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1876 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1877 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1878
1879 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1880 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1881 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1882 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1883 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1884 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1885
1886 /* all, bp4 and bp3 => don't care, others = 1 */
1887 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1888 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1889 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1890 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1891
1892 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1893 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1894 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1895 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1896 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1897
1898 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1899 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1900 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1901 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1902 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1903 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1904};
1905
1906struct generic_range gd25q128_cmp1_ranges[] = {
1907 /* none, bp4 and bp3 => don't care, others = 0 */
1908 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1909 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1910 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1911 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1912
1913 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1914 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1915 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1916 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1917 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1918 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1919
1920 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1921 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1922 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1923 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1924 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1925 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1926
1927 /* none, bp4 and bp3 => don't care, others = 1 */
1928 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1929 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1930 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1931 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1932 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1933
1934 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1935 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1936 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1937 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1938 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1939
1940 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1941 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1942 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1943 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1944 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1945 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1946};
1947
1948static struct generic_wp gd25q128_wp = {
1949 /* TODO: map second and third status registers */
1950 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1951};
1952
David Hendricks83541d32014-07-15 20:58:21 -07001953#if 0
1954/* FIXME: MX25L6405D has same ID as MX25L6406 */
1955static struct w25q_range mx25l6405d_ranges[] = {
1956 { X, 0, 0, {0, 0} }, /* none */
1957 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1958 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1959 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1960 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1961 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1962 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1963 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1964
1965 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1966 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1967 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1968 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1969 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1970 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1971 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1972 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1973};
1974#endif
1975
1976/* FIXME: MX25L6406 has same ID as MX25L6405D */
1977struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001978 { { }, 0, {0, 0} }, /* none */
1979 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1980 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1981 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1982 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1983 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1984 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001985
David Hendricks148a4bf2015-03-13 21:02:42 -07001986 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1987 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1988 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1989 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1990 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1991 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1992 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1993 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1994 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001995};
1996
1997static struct generic_wp mx25l6406e_wp = {
1998 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1999 .ranges = &mx25l6406e_ranges[0],
2000};
David Hendrickse0512a72014-07-15 20:30:47 -07002001
David Hendricksc3496092014-11-13 17:20:55 -08002002struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002003 { { }, 0, {0, 0} }, /* none */
2004 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
2005 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
2006 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08002007
David Hendricks148a4bf2015-03-13 21:02:42 -07002008 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
2009 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2010 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2011 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
2012 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2013 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2014 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2015 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2016 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2017 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2018 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2019 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002020};
2021
2022struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002023 { { }, 0, {0, 0} }, /* none */
2024 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2025 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2026 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2027 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2028 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2029 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2030 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2031 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2032 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2033 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2034 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2035 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2036 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2037 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2038 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002039};
2040
2041static struct generic_wp mx25l6495f_wp = {
2042 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2043};
2044
Vic Yang848bfd12018-03-23 10:24:07 -07002045struct generic_range mx25l25635f_tb0_ranges[] = {
2046 { { }, 0, {0, 0} }, /* none */
2047 { { }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* block 511 */
2048 { { }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* blocks 510-511 */
2049 { { }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* blocks 508-511 */
2050 { { }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* blocks 504-511 */
2051 { { }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* blocks 496-511 */
2052 { { }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* blocks 480-511 */
2053 { { }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* blocks 448-511 */
2054 { { }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* blocks 384-511 */
2055 { { }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* blocks 256-511 */
2056 { { }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* all */
2057 { { }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* all */
2058 { { }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* all */
2059 { { }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* all */
2060 { { }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* all */
2061 { { }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* all */
2062};
2063
2064struct generic_range mx25l25635f_tb1_ranges[] = {
2065 { { }, 0, {0, 0} }, /* none */
2066 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2067 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2068 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2069 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2070 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2071 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2072 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2073 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
2074 { { }, 0x9, {0x000000, 64 * 256 * 1024} }, /* blocks 0-255 */
2075 { { }, 0xa, {0x000000, 64 * 512 * 1024} }, /* all */
2076 { { }, 0xb, {0x000000, 64 * 512 * 1024} }, /* all */
2077 { { }, 0xc, {0x000000, 64 * 512 * 1024} }, /* all */
2078 { { }, 0xd, {0x000000, 64 * 512 * 1024} }, /* all */
2079 { { }, 0xe, {0x000000, 64 * 512 * 1024} }, /* all */
2080 { { }, 0xf, {0x000000, 64 * 512 * 1024} }, /* all */
2081};
2082
2083static struct generic_wp mx25l25635f_wp = {
2084 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2085};
2086
David Hendricks148a4bf2015-03-13 21:02:42 -07002087struct generic_range s25fs128s_ranges[] = {
2088 { { .tb = 1 }, 0, {0, 0} }, /* none */
2089 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
2090 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
2091 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
2092 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
2093 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
2094 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
2095 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002096
David Hendricks148a4bf2015-03-13 21:02:42 -07002097 { { .tb = 0 }, 0, {0, 0} }, /* none */
2098 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
2099 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
2100 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
2101 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
2102 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
2103 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
2104 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002105};
2106
2107static struct generic_wp s25fs128s_wp = {
2108 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002109 .get_modifier_bits = s25f_get_modifier_bits,
2110 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08002111};
2112
David Hendricksc694bb82015-02-25 14:52:17 -08002113
David Hendricks148a4bf2015-03-13 21:02:42 -07002114struct generic_range s25fl256s_ranges[] = {
2115 { { .tb = 1 }, 0, {0, 0} }, /* none */
2116 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
2117 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
2118 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
2119 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
2120 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
2121 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
2122 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
2123
2124 { { .tb = 0 }, 0, {0, 0} }, /* none */
2125 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
2126 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
2127 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
2128 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
2129 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
2130 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
2131 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08002132};
2133
2134static struct generic_wp s25fl256s_wp = {
2135 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002136 .get_modifier_bits = s25f_get_modifier_bits,
2137 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08002138};
2139
David Hendrickse0512a72014-07-15 20:30:47 -07002140/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002141static int generic_range_table(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002142 struct generic_wp **wp,
2143 int *num_entries)
2144{
2145 *wp = NULL;
2146 *num_entries = 0;
2147
Patrick Georgif3fa2992017-02-02 16:24:44 +01002148 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07002149 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002150 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002151
Martin Roth563a1fe2017-04-18 14:26:27 -06002152 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07002153 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002154 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07002155 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002156
David Hendricksaf3944a2014-07-28 18:37:40 -07002157 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2158 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
2159 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
2160 } else { /* CMP == 1 */
2161 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
2162 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
2163 }
2164
2165 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002166 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07002167 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06002168 case GIGADEVICE_GD25LQ128CD: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002169 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002170 *wp = &gd25q128_wp;
2171
2172 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2173 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
2174 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
2175 } else { /* CMP == 1 */
2176 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
2177 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
2178 }
2179
2180 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07002181 }
2182 default:
2183 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
2184 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002185 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07002186 return -1;
2187 }
2188 break;
David Hendricks83541d32014-07-15 20:58:21 -07002189 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002190 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07002191 case MACRONIX_MX25L6405:
2192 /* FIXME: MX25L64* chips have mixed capabilities and
2193 share IDs */
2194 *wp = &mx25l6406e_wp;
2195 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
2196 break;
David Hendricksc3496092014-11-13 17:20:55 -08002197 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002198 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08002199
2200 *wp = &mx25l6495f_wp;
2201 if (!(cr & (1 << 3))) { /* T/B == 0 */
2202 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
2203 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
2204 } else { /* T/B == 1 */
2205 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
2206 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
2207 }
2208 break;
2209 }
Vic Yang848bfd12018-03-23 10:24:07 -07002210 case MACRONIX_MX25L25635F: {
2211 uint8_t cr = mx25l_read_config_register(flash);
2212
2213 *wp = &mx25l25635f_wp;
2214 if (!(cr & (1 << 3))) { /* T/B == 0 */
2215 (*wp)->ranges = &mx25l25635f_tb0_ranges[0];
2216 *num_entries = ARRAY_SIZE(mx25l25635f_tb0_ranges);
2217 } else { /* T/B == 1 */
2218 (*wp)->ranges = &mx25l25635f_tb1_ranges[0];
2219 *num_entries = ARRAY_SIZE(mx25l25635f_tb1_ranges);
2220 }
2221 break;
2222 }
David Hendricks83541d32014-07-15 20:58:21 -07002223 default:
2224 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
2225 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002226 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07002227 return -1;
2228 }
2229 break;
David Hendricksa9884852014-12-11 15:31:12 -08002230 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002231 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08002232 case SPANSION_S25FS128S_L:
2233 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08002234 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002235 (*wp)->ranges = s25fs128s_ranges;
2236 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08002237 break;
2238 }
David Hendricksc694bb82015-02-25 14:52:17 -08002239 case SPANSION_S25FL256S_UL:
2240 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08002241 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002242 (*wp)->ranges = s25fl256s_ranges;
2243 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08002244 break;
2245 }
David Hendricksa9884852014-12-11 15:31:12 -08002246 default:
2247 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01002248 ", aborting\n", __func__, __LINE__,
2249 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08002250 return -1;
2251 }
2252 break;
David Hendrickse0512a72014-07-15 20:30:47 -07002253 default:
2254 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01002255 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07002256 return -1;
2257 }
2258
2259 return 0;
2260}
2261
2262/* Given a [start, len], this function finds a block protect bit combination
2263 * (if possible) and sets the corresponding bits in "status". Remaining bits
2264 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002265static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002266 unsigned int start, unsigned int len,
2267 uint8_t *status)
2268{
2269 struct generic_wp *wp;
2270 struct generic_range *r;
2271 int i, range_found = 0, num_entries;
2272 uint8_t bp_mask;
2273
2274 if (generic_range_table(flash, &wp, &num_entries))
2275 return -1;
2276
2277 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
2278 ((1 << wp->sr1.bp0_pos) - 1);
2279
2280 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
2281 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
2282 start, len, r->range.start, r->range.len);
2283 if ((start == r->range.start) && (len == r->range.len)) {
2284 *status &= ~(bp_mask);
2285 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07002286
2287 if (wp->set_modifier_bits) {
2288 if (wp->set_modifier_bits(flash, &r->m) < 0) {
2289 msg_cerr("error setting modifier "
2290 "bits for range.\n");
2291 return -1;
2292 }
2293 }
2294
David Hendrickse0512a72014-07-15 20:30:47 -07002295 range_found = 1;
2296 break;
2297 }
2298 }
2299
2300 if (!range_found) {
2301 msg_cerr("matching range not found\n");
2302 return -1;
2303 }
2304 return 0;
2305}
2306
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002307static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002308 const uint8_t sr1, unsigned int *start, unsigned int *len)
2309{
2310 struct generic_wp *wp;
2311 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07002312 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07002313 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002314 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07002315
2316 if (generic_range_table(flash, &wp, &num_entries))
2317 return -1;
2318
David Hendricks148a4bf2015-03-13 21:02:42 -07002319 /* modifier bits may be compared more than once, so get them here */
2320 if (wp->get_modifier_bits) {
2321 if (wp->get_modifier_bits(flash, &m) < 0)
2322 return -1;
2323 }
2324
David Hendrickse0512a72014-07-15 20:30:47 -07002325 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
2326
2327 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07002328 if (wp->get_modifier_bits) {
2329 if (memcmp(&m, &r->m, sizeof(m)))
2330 continue;
2331 }
David Hendrickse0512a72014-07-15 20:30:47 -07002332 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
2333 if (sr1_bp == r->bp) {
2334 *start = r->range.start;
2335 *len = r->range.len;
2336 status_found = 1;
2337 break;
2338 }
2339 }
2340
2341 if (!status_found) {
2342 msg_cerr("matching status not found\n");
2343 return -1;
2344 }
2345 return 0;
2346}
2347
2348/* Given a [start, len], this function calls generic_range_to_status() to
2349 * convert it to flash-chip-specific range bits, then sets into status register.
2350 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002351static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002352 unsigned int start, unsigned int len)
2353{
2354 uint8_t status, expected;
2355
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302356 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002357 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2358
2359 expected = status; /* preserve non-bp bits */
2360 if (generic_range_to_status(flash, start, len, &expected))
2361 return -1;
2362
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302363 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002364
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302365 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002366 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2367 if (status != expected) {
2368 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
2369 expected, status);
2370 return 1;
2371 }
2372
2373 return 0;
2374}
2375
2376/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002377static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002378{
2379 uint8_t status, expected;
2380 struct generic_wp *wp;
2381 int num_entries;
2382
2383 if (generic_range_table(flash, &wp, &num_entries))
2384 return -1;
2385
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302386 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002387 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2388
2389 if (enable)
2390 expected |= 1 << wp->sr1.srp_pos;
2391 else
2392 expected &= ~(1 << wp->sr1.srp_pos);
2393
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302394 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002395
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302396 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002397 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2398 if (status != expected)
2399 return -1;
2400
2401 return 0;
2402}
2403
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002404static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002405 enum wp_mode wp_mode)
2406{
2407 int ret;
2408
2409 switch (wp_mode) {
2410 case WP_MODE_HARDWARE:
2411 ret = generic_set_srp0(flash, 1);
2412 break;
2413 default:
2414 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2415 return 1;
2416 }
2417
2418 if (ret)
2419 msg_cerr("%s(): error=%d.\n", __func__, ret);
2420 return ret;
2421}
2422
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002423static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002424{
2425 int ret;
2426
2427 ret = generic_set_srp0(flash, 0);
2428 if (ret)
2429 msg_cerr("%s(): error=%d.\n", __func__, ret);
2430 return ret;
2431}
2432
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002433static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002434{
2435 struct generic_wp *wp;
2436 struct generic_range *r;
2437 int i, num_entries;
2438
2439 if (generic_range_table(flash, &wp, &num_entries))
2440 return -1;
2441
2442 r = &wp->ranges[0];
2443 for (i = 0; i < num_entries; i++) {
2444 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2445 r->range.start, r->range.len);
2446 r++;
2447 }
2448
2449 return 0;
2450}
2451
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002452static int generic_wp_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002453{
2454 uint8_t sr1;
2455 unsigned int start, len;
2456 int ret = 0;
2457 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002458 int num_entries, wp_en;
2459
2460 if (generic_range_table(flash, &wp, &num_entries))
2461 return -1;
2462
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302463 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002464 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2465
2466 msg_cinfo("WP: status: 0x%04x\n", sr1);
2467 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2468 /* FIXME: SRP1 is not really generic, but we probably should print
2469 * it anyway to have consistent output. #legacycruft */
2470 msg_cinfo("WP: status.srp1: %x\n", 0);
2471 msg_cinfo("WP: write protect is %s.\n",
2472 wp_en ? "enabled" : "disabled");
2473
2474 msg_cinfo("WP: write protect range: ");
2475 if (generic_status_to_range(flash, sr1, &start, &len)) {
2476 msg_cinfo("(cannot resolve the range)\n");
2477 ret = -1;
2478 } else {
2479 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2480 }
2481
2482 return ret;
2483}
2484
2485struct wp wp_generic = {
2486 .list_ranges = generic_list_ranges,
2487 .set_range = generic_set_range,
2488 .enable = generic_enable_writeprotect,
2489 .disable = generic_disable_writeprotect,
2490 .wp_status = generic_wp_status,
2491};