blob: 074a5468fdb6e9836cb39dc84e55ee3f9526fb2c [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
Alex Lu831c6092017-11-02 23:19:34 -0700378static struct w25q_range mx25u12835f_ranges[] = {
379 { X, 0, 0, {0, 0} }, /* none */
380 { 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 */
387
388 { 0, 1, 0x0, {0x800000, 128 * 64 * 1024} }, /* blocks 128-255 */
389 { 0, 1, 0x1, {0x000000, 256 * 64 * 1024} }, /* blocks all */
390 { 0, 1, 0x2, {0x000000, 256 * 64 * 1024} }, /* blocks all */
391 { 0, 1, 0x3, {0x000000, 256 * 64 * 1024} }, /* blocks all */
392 { 0, 1, 0x4, {0x000000, 256 * 64 * 1024} }, /* blocks all */
393 { 0, 1, 0x5, {0x000000, 256 * 64 * 1024} }, /* blocks all */
394 { 0, 1, 0x6, {0x000000, 256 * 64 * 1024} }, /* blocks all */
395 { 0, 1, 0x7, {0x000000, 256 * 64 * 1024} }, /* blocks all */
396};
397
David Hendricksbfa624b2012-07-24 12:47:59 -0700398static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700399 /*
400 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
401 * (block protect bit 3). It is only useful when all blocks are to
402 * be write-protected.
403 */
David Hendricks42a549a2015-04-22 11:25:07 -0700404 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700405
406 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
407 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
408 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
409 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
410 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
411 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
412 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
413
David Hendricksfe9123b2015-04-21 13:18:31 -0700414 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
415 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
416 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
417 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
418 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
419 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
420 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700421
422 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
423 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
424 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
425 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
426 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
427 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
428 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
429 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
430};
431
David Hendricksf7924d12010-06-10 21:26:44 -0700432static struct w25q_range w25q16_ranges[] = {
433 { X, X, 0, {0, 0} }, /* none */
434 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
435 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
436 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
437 { 0, 0, 0x4, {0x180000, 512 * 1024} },
438 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
439
440 { 0, 1, 0x1, {0x000000, 64 * 1024} },
441 { 0, 1, 0x2, {0x000000, 128 * 1024} },
442 { 0, 1, 0x3, {0x000000, 256 * 1024} },
443 { 0, 1, 0x4, {0x000000, 512 * 1024} },
444 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
445 { X, X, 0x6, {0x000000, 2048 * 1024} },
446 { X, X, 0x7, {0x000000, 2048 * 1024} },
447
448 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
449 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
450 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
451 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
452 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
453
454 { 1, 1, 0x1, {0x000000, 4 * 1024} },
455 { 1, 1, 0x2, {0x000000, 8 * 1024} },
456 { 1, 1, 0x3, {0x000000, 16 * 1024} },
457 { 1, 1, 0x4, {0x000000, 32 * 1024} },
458 { 1, 1, 0x5, {0x000000, 32 * 1024} },
459};
460
461static struct w25q_range w25q32_ranges[] = {
462 { X, X, 0, {0, 0} }, /* none */
463 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
464 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
465 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
466 { 0, 0, 0x4, {0x380000, 512 * 1024} },
467 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700468 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700469
470 { 0, 1, 0x1, {0x000000, 64 * 1024} },
471 { 0, 1, 0x2, {0x000000, 128 * 1024} },
472 { 0, 1, 0x3, {0x000000, 256 * 1024} },
473 { 0, 1, 0x4, {0x000000, 512 * 1024} },
474 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
475 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
476 { X, X, 0x7, {0x000000, 4096 * 1024} },
477
478 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
479 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
480 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
481 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
482 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
483
484 { 1, 1, 0x1, {0x000000, 4 * 1024} },
485 { 1, 1, 0x2, {0x000000, 8 * 1024} },
486 { 1, 1, 0x3, {0x000000, 16 * 1024} },
487 { 1, 1, 0x4, {0x000000, 32 * 1024} },
488 { 1, 1, 0x5, {0x000000, 32 * 1024} },
489};
490
491static struct w25q_range w25q80_ranges[] = {
492 { X, X, 0, {0, 0} }, /* none */
493 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
494 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
495 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
496 { 0, 0, 0x4, {0x080000, 512 * 1024} },
497
498 { 0, 1, 0x1, {0x000000, 64 * 1024} },
499 { 0, 1, 0x2, {0x000000, 128 * 1024} },
500 { 0, 1, 0x3, {0x000000, 256 * 1024} },
501 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700502 { X, X, 0x6, {0x000000, 1024 * 1024} },
503 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700504
505 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
506 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
507 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
508 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
509 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
510
511 { 1, 1, 0x1, {0x000000, 4 * 1024} },
512 { 1, 1, 0x2, {0x000000, 8 * 1024} },
513 { 1, 1, 0x3, {0x000000, 16 * 1024} },
514 { 1, 1, 0x4, {0x000000, 32 * 1024} },
515 { 1, 1, 0x5, {0x000000, 32 * 1024} },
516};
517
David Hendricks2c4a76c2010-06-28 14:00:43 -0700518static struct w25q_range w25q64_ranges[] = {
519 { X, X, 0, {0, 0} }, /* none */
520
521 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
522 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
523 { 0, 0, 0x3, {0x780000, 512 * 1024} },
524 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
525 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
526 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
527
528 { 0, 1, 0x1, {0x000000, 128 * 1024} },
529 { 0, 1, 0x2, {0x000000, 256 * 1024} },
530 { 0, 1, 0x3, {0x000000, 512 * 1024} },
531 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
532 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
533 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
534 { X, X, 0x7, {0x000000, 8192 * 1024} },
535
536 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
537 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
538 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
539 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
540 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
541
542 { 1, 1, 0x1, {0x000000, 4 * 1024} },
543 { 1, 1, 0x2, {0x000000, 8 * 1024} },
544 { 1, 1, 0x3, {0x000000, 16 * 1024} },
545 { 1, 1, 0x4, {0x000000, 32 * 1024} },
546 { 1, 1, 0x5, {0x000000, 32 * 1024} },
547};
548
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700549static struct w25q_range w25rq128_cmp0_ranges[] = {
550 { X, X, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530551
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700552 { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
553 { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
554 { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
555 { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
556 { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
557 { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530558
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700559 { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
560 { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
561 { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
562 { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
563 { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
564 { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530565
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700566 { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530567
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700568 { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
569 { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
570 { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
571 { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
572 { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
573
574 { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
575 { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
576 { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
577 { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
578 { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
579};
580
581static struct w25q_range w25rq128_cmp1_ranges[] = {
582 { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
583
584 { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
585 { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
586 { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
587 { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
588 { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
589 { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
590
591 { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
592 { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
593 { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
594 { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
595 { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
596 { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
597
598 { X, X, 0x7, {0x000000, 0} }, /* NONE */
599
600 { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
601 { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
602 { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
603 { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
604 { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
605
606 { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
607 { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
608 { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
609 { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
610 { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530611};
612
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800613static struct w25q_range w25rq256_cmp0_ranges[] = {
614 { X, X, 0x0, {0x0000000, 0x0000000} }, /* NONE */
615
616 { X, 0, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* Upper 1/512 */
617 { X, 0, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* Upper 1/256 */
618 { X, 0, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* Upper 1/128 */
619 { X, 0, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* Upper 1/64 */
620 { X, 0, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* Upper 1/32 */
621 { X, 0, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* Upper 1/16 */
622 { X, 0, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* Upper 1/8 */
623 { X, 0, 0x8, {0x1800000, 64 * 128 * 1024} }, /* Upper 1/4 */
624 { X, 0, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
625
626 { X, 1, 0x1, {0x0000000, 64 * 1 * 1024} }, /* Lower 1/512 */
627 { X, 1, 0x2, {0x0000000, 64 * 2 * 1024} }, /* Lower 1/256 */
628 { X, 1, 0x3, {0x0000000, 64 * 4 * 1024} }, /* Lower 1/128 */
629 { X, 1, 0x4, {0x0000000, 64 * 8 * 1024} }, /* Lower 1/64 */
630 { X, 1, 0x5, {0x0000000, 64 * 16 * 1024} }, /* Lower 1/32 */
631 { X, 1, 0x6, {0x0000000, 64 * 32 * 1024} }, /* Lower 1/16 */
632 { X, 1, 0x7, {0x0000000, 64 * 64 * 1024} }, /* Lower 1/8 */
633 { X, 1, 0x8, {0x0000000, 64 * 128 * 1024} }, /* Lower 1/4 */
634 { X, 1, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
635
636 { X, X, 0xa, {0x0000000, 64 * 512 * 1024} }, /* ALL */
637 { X, X, 0xb, {0x0000000, 64 * 512 * 1024} }, /* ALL */
638 { X, X, 0xc, {0x0000000, 64 * 512 * 1024} }, /* ALL */
639 { X, X, 0xd, {0x0000000, 64 * 512 * 1024} }, /* ALL */
640 { X, X, 0xe, {0x0000000, 64 * 512 * 1024} }, /* ALL */
641 { X, X, 0xf, {0x0000000, 64 * 512 * 1024} }, /* ALL */
642};
643
644static struct w25q_range w25rq256_cmp1_ranges[] = {
645 { X, X, 0x0, {0x0000000, 64 * 512 * 1024} }, /* ALL */
646
647 { X, 0, 0x1, {0x0000000, 64 * 511 * 1024} }, /* Lower 511/512 */
648 { X, 0, 0x2, {0x0000000, 64 * 510 * 1024} }, /* Lower 255/256 */
649 { X, 0, 0x3, {0x0000000, 64 * 508 * 1024} }, /* Lower 127/128 */
650 { X, 0, 0x4, {0x0000000, 64 * 504 * 1024} }, /* Lower 63/64 */
651 { X, 0, 0x5, {0x0000000, 64 * 496 * 1024} }, /* Lower 31/32 */
652 { X, 0, 0x6, {0x0000000, 64 * 480 * 1024} }, /* Lower 15/16 */
653 { X, 0, 0x7, {0x0000000, 64 * 448 * 1024} }, /* Lower 7/8 */
654 { X, 0, 0x8, {0x0000000, 64 * 384 * 1024} }, /* Lower 3/4 */
655 { X, 0, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
656
657 { X, 1, 0x1, {0x0010000, 64 * 511 * 1024} }, /* Upper 511/512 */
658 { X, 1, 0x2, {0x0020000, 64 * 510 * 1024} }, /* Upper 255/256 */
659 { X, 1, 0x3, {0x0040000, 64 * 508 * 1024} }, /* Upper 127/128 */
660 { X, 1, 0x4, {0x0080000, 64 * 504 * 1024} }, /* Upper 63/64 */
661 { X, 1, 0x5, {0x0100000, 64 * 496 * 1024} }, /* Upper 31/32 */
662 { X, 1, 0x6, {0x0200000, 64 * 480 * 1024} }, /* Upper 15/16 */
663 { X, 1, 0x7, {0x0400000, 64 * 448 * 1024} }, /* Upper 7/8 */
664 { X, 1, 0x8, {0x0800000, 64 * 384 * 1024} }, /* Upper 3/4 */
665 { X, 1, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
666
667 { X, X, 0xa, {0x0000000, 0x0000000} }, /* NONE */
668 { X, X, 0xb, {0x0000000, 0x0000000} }, /* NONE */
669 { X, X, 0xc, {0x0000000, 0x0000000} }, /* NONE */
670 { X, X, 0xd, {0x0000000, 0x0000000} }, /* NONE */
671 { X, X, 0xe, {0x0000000, 0x0000000} }, /* NONE */
672 { X, X, 0xf, {0x0000000, 0x0000000} }, /* NONE */
673};
674
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800675struct w25q_range w25x10_ranges[] = {
676 { X, X, 0, {0, 0} }, /* none */
677 { 0, 0, 0x1, {0x010000, 64 * 1024} },
678 { 0, 1, 0x1, {0x000000, 64 * 1024} },
679 { X, X, 0x2, {0x000000, 128 * 1024} },
680 { X, X, 0x3, {0x000000, 128 * 1024} },
681};
682
683struct w25q_range w25x20_ranges[] = {
684 { X, X, 0, {0, 0} }, /* none */
685 { 0, 0, 0x1, {0x030000, 64 * 1024} },
686 { 0, 0, 0x2, {0x020000, 128 * 1024} },
687 { 0, 1, 0x1, {0x000000, 64 * 1024} },
688 { 0, 1, 0x2, {0x000000, 128 * 1024} },
689 { 0, X, 0x3, {0x000000, 256 * 1024} },
690};
691
David Hendricks470ca952010-08-13 14:01:53 -0700692struct w25q_range w25x40_ranges[] = {
693 { X, X, 0, {0, 0} }, /* none */
694 { 0, 0, 0x1, {0x070000, 64 * 1024} },
695 { 0, 0, 0x2, {0x060000, 128 * 1024} },
696 { 0, 0, 0x3, {0x040000, 256 * 1024} },
697 { 0, 1, 0x1, {0x000000, 64 * 1024} },
698 { 0, 1, 0x2, {0x000000, 128 * 1024} },
699 { 0, 1, 0x3, {0x000000, 256 * 1024} },
700 { 0, X, 0x4, {0x000000, 512 * 1024} },
David Hendricksb389abb2016-06-17 16:47:00 -0700701 { 0, X, 0x5, {0x000000, 512 * 1024} },
702 { 0, X, 0x6, {0x000000, 512 * 1024} },
703 { 0, X, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700704};
705
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800706struct w25q_range w25x80_ranges[] = {
707 { X, X, 0, {0, 0} }, /* none */
708 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
709 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
710 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
711 { 0, 0, 0x4, {0x080000, 512 * 1024} },
712 { 0, 1, 0x1, {0x000000, 64 * 1024} },
713 { 0, 1, 0x2, {0x000000, 128 * 1024} },
714 { 0, 1, 0x3, {0x000000, 256 * 1024} },
715 { 0, 1, 0x4, {0x000000, 512 * 1024} },
716 { 0, X, 0x5, {0x000000, 1024 * 1024} },
717 { 0, X, 0x6, {0x000000, 1024 * 1024} },
718 { 0, X, 0x7, {0x000000, 1024 * 1024} },
719};
720
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600721static struct w25q_range gd25q40_cmp0_ranges[] = {
722 { X, X, 0, {0, 0} }, /* None */
723 { 0, 0, 0x1, {0x070000, 64 * 1024} },
724 { 0, 0, 0x2, {0x060000, 128 * 1024} },
725 { 0, 0, 0x3, {0x040000, 256 * 1024} },
726 { 0, 1, 0x1, {0x000000, 64 * 1024} },
727 { 0, 1, 0x2, {0x000000, 128 * 1024} },
728 { 0, 1, 0x3, {0x000000, 256 * 1024} },
729 { 0, X, 0x4, {0x000000, 512 * 1024} }, /* All */
730 { 0, X, 0x5, {0x000000, 512 * 1024} }, /* All */
731 { 0, X, 0x6, {0x000000, 512 * 1024} }, /* All */
732 { 0, X, 0x7, {0x000000, 512 * 1024} }, /* All */
733 { 1, 0, 0x1, {0x07F000, 4 * 1024} },
734 { 1, 0, 0x2, {0x07E000, 8 * 1024} },
735 { 1, 0, 0x3, {0x07C000, 16 * 1024} },
736 { 1, 0, 0x4, {0x078000, 32 * 1024} },
737 { 1, 0, 0x5, {0x078000, 32 * 1024} },
738 { 1, 0, 0x6, {0x078000, 32 * 1024} },
739 { 1, 1, 0x1, {0x000000, 4 * 1024} },
740 { 1, 1, 0x2, {0x000000, 8 * 1024} },
741 { 1, 1, 0x3, {0x000000, 16 * 1024} },
742 { 1, 1, 0x4, {0x000000, 32 * 1024} },
743 { 1, 1, 0x5, {0x000000, 32 * 1024} },
744 { 1, 1, 0x6, {0x000000, 32 * 1024} },
745 { 1, X, 0x7, {0x000000, 512 * 1024} }, /* All */
746};
747
748static struct w25q_range gd25q40_cmp1_ranges[] = {
749 { X, X, 0x0, {0x000000, 512 * 1024} }, /* ALL */
750 { 0, 0, 0x1, {0x000000, 448 * 1024} },
751 { 0, 0, 0x2, {0x000000, 384 * 1024} },
752 { 0, 0, 0x3, {0x000000, 256 * 1024} },
753
754 { 0, 1, 0x1, {0x010000, 448 * 1024} },
755 { 0, 1, 0x2, {0x020000, 384 * 1024} },
756 { 0, 1, 0x3, {0x040000, 256 * 1024} },
757
758 { 0, X, 0x4, {0x000000, 0} }, /* None */
759 { 0, X, 0x5, {0x000000, 0} }, /* None */
760 { 0, X, 0x6, {0x000000, 0} }, /* None */
761 { 0, X, 0x7, {0x000000, 0} }, /* None */
762
763 { 1, 0, 0x1, {0x000000, 508 * 1024} },
764 { 1, 0, 0x2, {0x000000, 504 * 1024} },
765 { 1, 0, 0x3, {0x000000, 496 * 1024} },
766 { 1, 0, 0x4, {0x000000, 480 * 1024} },
767 { 1, 0, 0x5, {0x000000, 480 * 1024} },
768 { 1, 0, 0x6, {0x000000, 480 * 1024} },
769
770 { 1, 1, 0x1, {0x001000, 508 * 1024} },
771 { 1, 1, 0x2, {0x002000, 504 * 1024} },
772 { 1, 1, 0x3, {0x004000, 496 * 1024} },
773 { 1, 1, 0x4, {0x008000, 480 * 1024} },
774 { 1, 1, 0x5, {0x008000, 480 * 1024} },
775 { 1, 1, 0x6, {0x008000, 480 * 1024} },
776
777 { 1, X, 0x7, {0x000000, 0} }, /* None */
778};
779
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700780static struct w25q_range gd25q64_ranges[] = {
781 { X, X, 0, {0, 0} }, /* none */
782 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
783 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
784 { 0, 0, 0x3, {0x780000, 512 * 1024} },
785 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
786 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
787 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
788
789 { 0, 1, 0x1, {0x000000, 128 * 1024} },
790 { 0, 1, 0x2, {0x000000, 256 * 1024} },
791 { 0, 1, 0x3, {0x000000, 512 * 1024} },
792 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
793 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
794 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
795 { X, X, 0x7, {0x000000, 8192 * 1024} },
796
797 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
798 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
799 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
800 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
801 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
802 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
803
804 { 1, 1, 0x1, {0x000000, 4 * 1024} },
805 { 1, 1, 0x2, {0x000000, 8 * 1024} },
806 { 1, 1, 0x3, {0x000000, 16 * 1024} },
807 { 1, 1, 0x4, {0x000000, 32 * 1024} },
808 { 1, 1, 0x5, {0x000000, 32 * 1024} },
809 { 1, 1, 0x6, {0x000000, 32 * 1024} },
810};
811
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800812static struct w25q_range a25l040_ranges[] = {
813 { X, X, 0x0, {0, 0} }, /* none */
814 { X, X, 0x1, {0x70000, 64 * 1024} },
815 { X, X, 0x2, {0x60000, 128 * 1024} },
816 { X, X, 0x3, {0x40000, 256 * 1024} },
817 { X, X, 0x4, {0x00000, 512 * 1024} },
818 { X, X, 0x5, {0x00000, 512 * 1024} },
819 { X, X, 0x6, {0x00000, 512 * 1024} },
820 { X, X, 0x7, {0x00000, 512 * 1024} },
821};
822
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700823static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530824{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100825 if (flash->chip->read_status)
826 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530827 else
828 return spi_read_status_register(flash);
829}
830
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700831static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530832{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100833 if (flash->chip->write_status)
834 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530835 else
836 return spi_write_status_register(flash, status);
837}
838
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700839/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700840static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700841{
842 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
843 unsigned char readarr[2];
844 int ret;
845
846 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700847 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700848 if (ret) {
849 /*
850 * FIXME: make this a benign failure for now in case we are
851 * unable to execute the opcode
852 */
853 msg_cdbg("RDSR2 failed!\n");
854 readarr[0] = 0x00;
855 }
856
857 return readarr[0];
858}
859
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800860/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700861static int w25_range_table(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800862 struct w25q_range **w25q_ranges,
863 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700864{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800865 *w25q_ranges = 0;
866 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700867
Patrick Georgif3fa2992017-02-02 16:24:44 +0100868 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700869 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100870 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800871 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800872 *w25q_ranges = w25x10_ranges;
873 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800874 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800875 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800876 *w25q_ranges = w25x20_ranges;
877 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800878 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800879 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800880 *w25q_ranges = w25x40_ranges;
881 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700882 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800883 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800884 *w25q_ranges = w25x80_ranges;
885 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800886 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100887 case WINBOND_NEX_W25Q80_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800888 *w25q_ranges = w25q80_ranges;
889 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700890 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100891 case WINBOND_NEX_W25Q16_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800892 *w25q_ranges = w25q16_ranges;
893 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700894 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100895 case WINBOND_NEX_W25Q32_V:
896 case WINBOND_NEX_W25Q32_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800897 *w25q_ranges = w25q32_ranges;
898 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700899 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100900 case WINBOND_NEX_W25Q64_V:
901 case WINBOND_NEX_W25Q64_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800902 *w25q_ranges = w25q64_ranges;
903 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700904 break;
Martin Rothee8dcf92017-05-10 19:16:19 -0600905 case WINBOND_NEX_W25Q128J:
Patrick Georgicc04a452017-02-06 12:14:43 +0100906 case WINBOND_NEX_W25Q128_V:
907 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700908 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700909 /* CMP == 1 */
910 *w25q_ranges = w25rq128_cmp1_ranges;
911 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
912 } else {
913 /* CMP == 0 */
914 *w25q_ranges = w25rq128_cmp0_ranges;
915 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
916 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530917 break;
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800918 case WINBOND_NEX_W25Q256JV:
919 if (w25q_read_status_register_2(flash) & (1 << 6)) {
920 /* CMP == 1 */
921 *w25q_ranges = w25rq256_cmp1_ranges;
922 *num_entries = ARRAY_SIZE(w25rq256_cmp1_ranges);
923 } else {
924 /* CMP == 0 */
925 *w25q_ranges = w25rq256_cmp0_ranges;
926 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
927 }
928 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700929 default:
930 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
931 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100932 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700933 return -1;
934 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700935 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700936 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100937 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800938 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800939 *w25q_ranges = en25f40_ranges;
940 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700941 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700942 case EON_EN25Q40:
943 *w25q_ranges = en25q40_ranges;
944 *num_entries = ARRAY_SIZE(en25q40_ranges);
945 break;
946 case EON_EN25Q80:
947 *w25q_ranges = en25q80_ranges;
948 *num_entries = ARRAY_SIZE(en25q80_ranges);
949 break;
950 case EON_EN25Q32:
951 *w25q_ranges = en25q32_ranges;
952 *num_entries = ARRAY_SIZE(en25q32_ranges);
953 break;
954 case EON_EN25Q64:
955 *w25q_ranges = en25q64_ranges;
956 *num_entries = ARRAY_SIZE(en25q64_ranges);
957 break;
958 case EON_EN25Q128:
959 *w25q_ranges = en25q128_ranges;
960 *num_entries = ARRAY_SIZE(en25q128_ranges);
961 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600962 case EON_EN25S64:
963 *w25q_ranges = en25s64_ranges;
964 *num_entries = ARRAY_SIZE(en25s64_ranges);
965 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700966 default:
967 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
968 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100969 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -0700970 return -1;
971 }
972 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800973 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100974 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800975 case MACRONIX_MX25L1005:
976 *w25q_ranges = mx25l1005_ranges;
977 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
978 break;
979 case MACRONIX_MX25L2005:
980 *w25q_ranges = mx25l2005_ranges;
981 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
982 break;
983 case MACRONIX_MX25L4005:
984 *w25q_ranges = mx25l4005_ranges;
985 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
986 break;
987 case MACRONIX_MX25L8005:
988 *w25q_ranges = mx25l8005_ranges;
989 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
990 break;
991 case MACRONIX_MX25L1605:
992 /* FIXME: MX25L1605 and MX25L1605D have different write
993 * protection capabilities, but share IDs */
994 *w25q_ranges = mx25l1605d_ranges;
995 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
996 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800997 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800998 *w25q_ranges = mx25l3205d_ranges;
999 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -07001000 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001001 case MACRONIX_MX25U3235E:
1002 *w25q_ranges = mx25u3235e_ranges;
1003 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
1004 break;
Jongpil66a96492014-08-14 17:59:06 +09001005 case MACRONIX_MX25U6435E:
1006 *w25q_ranges = mx25u6435e_ranges;
1007 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
1008 break;
Alex Lu831c6092017-11-02 23:19:34 -07001009 case MACRONIX_MX25U12835F:
1010 *w25q_ranges = mx25u12835f_ranges;
1011 *num_entries = ARRAY_SIZE(mx25u12835f_ranges);
1012 break;
David Hendricksac72e362010-08-16 18:20:03 -07001013 default:
1014 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1015 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001016 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -07001017 return -1;
1018 }
1019 break;
David Hendricksbfa624b2012-07-24 12:47:59 -07001020 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001021 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -07001022 case ST_N25Q064__1E:
1023 case ST_N25Q064__3E:
1024 *w25q_ranges = n25q064_ranges;
1025 *num_entries = ARRAY_SIZE(n25q064_ranges);
1026 break;
1027 default:
1028 msg_cerr("%s() %d: Micron flash chip mismatch"
1029 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001030 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -07001031 return -1;
1032 }
1033 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001034 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001035 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -07001036 case GIGADEVICE_GD25LQ32:
1037 *w25q_ranges = w25q32_ranges;
1038 *num_entries = ARRAY_SIZE(w25q32_ranges);
1039 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001040 case GIGADEVICE_GD25Q40:
1041 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1042 /* CMP == 1 */
1043 *w25q_ranges = gd25q40_cmp1_ranges;
1044 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
1045 } else {
1046 *w25q_ranges = gd25q40_cmp0_ranges;
1047 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
1048 }
1049 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001050 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -06001051 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001052 *w25q_ranges = gd25q64_ranges;
1053 *num_entries = ARRAY_SIZE(gd25q64_ranges);
1054 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001055 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06001056 case GIGADEVICE_GD25LQ128CD:
Martin Roth1fd87ed2017-02-27 20:50:50 -07001057 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1058 /* CMP == 1 */
1059 *w25q_ranges = w25rq128_cmp1_ranges;
1060 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1061 } else {
1062 /* CMP == 0 */
1063 *w25q_ranges = w25rq128_cmp0_ranges;
1064 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1065 }
1066 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001067 default:
1068 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1069 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001070 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -07001071 return -1;
1072 }
1073 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001074 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001075 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001076 case AMIC_A25L040:
1077 *w25q_ranges = a25l040_ranges;
1078 *num_entries = ARRAY_SIZE(a25l040_ranges);
1079 break;
1080 default:
1081 msg_cerr("%s() %d: AMIC flash chip mismatch"
1082 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001083 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001084 return -1;
1085 }
1086 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001087 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001088 switch(flash->chip->model_id) {
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001089 case ATMEL_AT25SL128A:
1090 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1091 /* CMP == 1 */
1092 *w25q_ranges = w25rq128_cmp1_ranges;
1093 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1094 } else {
1095 /* CMP == 0 */
1096 *w25q_ranges = w25rq128_cmp0_ranges;
1097 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1098 }
1099 break;
1100 default:
1101 msg_cerr("%s() %d: Atmel flash chip mismatch"
1102 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001103 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001104 return -1;
1105 }
1106 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001107 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001108 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001109 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001110 return -1;
1111 }
1112
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001113 return 0;
1114}
1115
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001116int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001117 unsigned int start, unsigned int len,
1118 struct w25q_status *status)
1119{
1120 struct w25q_range *w25q_ranges;
1121 int i, range_found = 0;
1122 int num_entries;
1123
1124 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001125 for (i = 0; i < num_entries; i++) {
1126 struct wp_range *r = &w25q_ranges[i].range;
1127
1128 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1129 start, len, r->start, r->len);
1130 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -07001131 status->bp0 = w25q_ranges[i].bp & 1;
1132 status->bp1 = w25q_ranges[i].bp >> 1;
1133 status->bp2 = w25q_ranges[i].bp >> 2;
1134 status->tb = w25q_ranges[i].tb;
1135 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001136
1137 range_found = 1;
1138 break;
1139 }
1140 }
1141
1142 if (!range_found) {
1143 msg_cerr("matching range not found\n");
1144 return -1;
1145 }
David Hendricksd494b0a2010-08-16 16:28:50 -07001146 return 0;
1147}
1148
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001149int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001150 const struct w25q_status *status,
1151 unsigned int *start, unsigned int *len)
1152{
1153 struct w25q_range *w25q_ranges;
1154 int i, status_found = 0;
1155 int num_entries;
1156
1157 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1158 for (i = 0; i < num_entries; i++) {
1159 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001160 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001161
1162 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1163 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
1164 bp, w25q_ranges[i].bp,
1165 status->tb, w25q_ranges[i].tb,
1166 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001167 table_bp = w25q_ranges[i].bp;
1168 table_tb = w25q_ranges[i].tb;
1169 table_sec = w25q_ranges[i].sec;
1170 if ((bp == table_bp || table_bp == X) &&
1171 (status->tb == table_tb || table_tb == X) &&
1172 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001173 *start = w25q_ranges[i].range.start;
1174 *len = w25q_ranges[i].range.len;
1175
1176 status_found = 1;
1177 break;
1178 }
1179 }
1180
1181 if (!status_found) {
1182 msg_cerr("matching status not found\n");
1183 return -1;
1184 }
1185 return 0;
1186}
1187
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001188/* Given a [start, len], this function calls w25_range_to_status() to convert
1189 * it to flash-chip-specific range bits, then sets into status register.
1190 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001191static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001192 unsigned int start, unsigned int len)
1193{
1194 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001195 int tmp = 0;
1196 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001197
1198 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301199 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001200 memcpy(&status, &tmp, 1);
1201 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1202
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001203 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001204
1205 msg_cdbg("status.busy: %x\n", status.busy);
1206 msg_cdbg("status.wel: %x\n", status.wel);
1207 msg_cdbg("status.bp0: %x\n", status.bp0);
1208 msg_cdbg("status.bp1: %x\n", status.bp1);
1209 msg_cdbg("status.bp2: %x\n", status.bp2);
1210 msg_cdbg("status.tb: %x\n", status.tb);
1211 msg_cdbg("status.sec: %x\n", status.sec);
1212 msg_cdbg("status.srp0: %x\n", status.srp0);
1213
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001214 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301215 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001216
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301217 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001218 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1219 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001220 return 0;
1221 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001222 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001223 expected, tmp);
1224 return 1;
1225 }
David Hendricksf7924d12010-06-10 21:26:44 -07001226}
1227
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001228/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001229static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001230{
1231 struct w25q_status status;
1232 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001233 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001234 int ret = 0;
1235
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001236 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301237 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001238 memcpy(&status, &tmp, 1);
1239 msg_cinfo("WP: status: 0x%02x\n", tmp);
1240 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1241 msg_cinfo("WP: write protect is %s.\n",
1242 status.srp0 ? "enabled" : "disabled");
1243
1244 msg_cinfo("WP: write protect range: ");
1245 if (w25_status_to_range(flash, &status, &start, &len)) {
1246 msg_cinfo("(cannot resolve the range)\n");
1247 ret = -1;
1248 } else {
1249 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1250 }
1251
1252 return ret;
1253}
1254
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001255static int w25q_large_range_to_status(const struct flashctx *flash,
1256 unsigned int start, unsigned int len,
1257 struct w25q_status_large *status)
1258{
1259 struct w25q_range *w25q_ranges;
1260 int i, range_found = 0;
1261 int num_entries;
1262
1263 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1264 return -1;
1265 for (i = 0; i < num_entries; i++) {
1266 struct wp_range *r = &w25q_ranges[i].range;
1267
1268 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1269 start, len, r->start, r->len);
1270 if ((start == r->start) && (len == r->len)) {
1271 status->bp0 = w25q_ranges[i].bp & 1;
1272 status->bp1 = w25q_ranges[i].bp >> 1;
1273 status->bp2 = w25q_ranges[i].bp >> 2;
1274 status->bp3 = w25q_ranges[i].bp >> 3;
1275 status->tb = w25q_ranges[i].tb;
1276
1277 range_found = 1;
1278 break;
1279 }
1280 }
1281
1282 if (!range_found) {
1283 msg_cerr("matching range not found\n");
1284 return -1;
1285 }
1286 return 0;
1287}
1288
1289static int w25_large_status_to_range(const struct flashctx *flash,
1290 const struct w25q_status_large *status,
1291 unsigned int *start, unsigned int *len)
1292{
1293 struct w25q_range *w25q_ranges;
1294 int i, status_found = 0;
1295 int num_entries;
1296
1297 if (w25_range_table(flash, &w25q_ranges, &num_entries))
1298 return -1;
1299 for (i = 0; i < num_entries; i++) {
1300 int bp;
1301 int table_bp, table_tb;
1302
1303 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2) |
1304 (status->bp3 << 3);
1305 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x\n",
1306 bp, w25q_ranges[i].bp,
1307 status->tb, w25q_ranges[i].tb);
1308 table_bp = w25q_ranges[i].bp;
1309 table_tb = w25q_ranges[i].tb;
1310 if ((bp == table_bp || table_bp == X) &&
1311 (status->tb == table_tb || table_tb == X)) {
1312 *start = w25q_ranges[i].range.start;
1313 *len = w25q_ranges[i].range.len;
1314
1315 status_found = 1;
1316 break;
1317 }
1318 }
1319
1320 if (!status_found) {
1321 msg_cerr("matching status not found\n");
1322 return -1;
1323 }
1324 return 0;
1325}
1326
1327/* Given a [start, len], this function calls w25_range_to_status() to convert
1328 * it to flash-chip-specific range bits, then sets into status register.
1329 * Returns 0 if successful, -1 on error, and 1 if reading back was different.
1330 */
1331static int w25q_large_set_range(const struct flashctx *flash,
1332 unsigned int start, unsigned int len)
1333{
1334 struct w25q_status_large status;
1335 int tmp;
1336 int expected = 0;
1337
1338 memset(&status, 0, sizeof(status));
1339 tmp = do_read_status(flash);
1340 memcpy(&status, &tmp, 1);
1341 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1342
1343 if (w25q_large_range_to_status(flash, start, len, &status))
1344 return -1;
1345
1346 msg_cdbg("status.busy: %x\n", status.busy);
1347 msg_cdbg("status.wel: %x\n", status.wel);
1348 msg_cdbg("status.bp0: %x\n", status.bp0);
1349 msg_cdbg("status.bp1: %x\n", status.bp1);
1350 msg_cdbg("status.bp2: %x\n", status.bp2);
1351 msg_cdbg("status.bp3: %x\n", status.bp3);
1352 msg_cdbg("status.tb: %x\n", status.tb);
1353 msg_cdbg("status.srp0: %x\n", status.srp0);
1354
1355 memcpy(&expected, &status, sizeof(status));
1356 do_write_status(flash, expected);
1357
1358 tmp = do_read_status(flash);
1359 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1360 if ((tmp & MASK_WP_AREA_LARGE) == (expected & MASK_WP_AREA_LARGE)) {
1361 return 0;
1362 } else {
1363 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1364 expected, tmp);
1365 return 1;
1366 }
1367}
1368
1369static int w25q_large_wp_status(const struct flashctx *flash)
1370{
1371 struct w25q_status_large sr1;
1372 struct w25q_status_2 sr2;
1373 uint8_t tmp[2];
1374 unsigned int start, len;
1375 int ret = 0;
1376
1377 memset(&sr1, 0, sizeof(sr1));
1378 tmp[0] = do_read_status(flash);
1379 memcpy(&sr1, &tmp[0], 1);
1380
1381 memset(&sr2, 0, sizeof(sr2));
1382 tmp[1] = w25q_read_status_register_2(flash);
1383 memcpy(&sr2, &tmp[1], 1);
1384
1385 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
1386 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1387 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1388 msg_cinfo("WP: write protect is %s.\n",
1389 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1390
1391 msg_cinfo("WP: write protect range: ");
1392 if (w25_large_status_to_range(flash, &sr1, &start, &len)) {
1393 msg_cinfo("(cannot resolve the range)\n");
1394 ret = -1;
1395 } else {
1396 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1397 }
1398
1399 return ret;
1400}
1401
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001402/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001403static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001404{
1405 struct w25q_status status;
1406 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001407 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001408
1409 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301410 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001411 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001412 memcpy(&status, &tmp, 1);
1413 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1414
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001415 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001416 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301417 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001418
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301419 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001420 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1421 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1422 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001423
1424 return 0;
1425}
1426
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001427static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001428 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001429{
1430 int ret;
1431
David Hendricks1c09f802012-10-03 11:03:48 -07001432 switch (wp_mode) {
1433 case WP_MODE_HARDWARE:
1434 ret = w25_set_srp0(flash, 1);
1435 break;
1436 default:
1437 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1438 return 1;
1439 }
1440
David Hendricksc801adb2010-12-09 16:58:56 -08001441 if (ret)
1442 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001443 return ret;
1444}
1445
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001446static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001447{
1448 int ret;
1449
1450 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001451 if (ret)
1452 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001453 return ret;
1454}
1455
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001456static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001457{
1458 struct w25q_range *w25q_ranges;
1459 int i, num_entries;
1460
1461 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1462 for (i = 0; i < num_entries; i++) {
1463 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1464 w25q_ranges[i].range.start,
1465 w25q_ranges[i].range.len);
1466 }
1467
1468 return 0;
1469}
1470
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001471static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001472{
1473 struct w25q_status sr1;
1474 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001475 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001476 unsigned int start, len;
1477 int ret = 0;
1478
1479 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301480 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001481 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001482
David Hendricksf1bd8802012-10-30 11:37:57 -07001483 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001484 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001485 memcpy(&sr2, &tmp[1], 1);
1486
1487 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001488 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1489 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1490 msg_cinfo("WP: write protect is %s.\n",
1491 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1492
1493 msg_cinfo("WP: write protect range: ");
1494 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1495 msg_cinfo("(cannot resolve the range)\n");
1496 ret = -1;
1497 } else {
1498 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1499 }
1500
1501 return ret;
1502}
1503
1504/*
1505 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1506 * de-asserted after the first byte, then it acts like a JEDEC-standard
1507 * WRSR command. if /CS is asserted, then the next data byte is written
1508 * into status register 2.
1509 */
1510#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001511static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001512{
1513 int result;
1514 struct spi_command cmds[] = {
1515 {
1516 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1517 .writecnt = JEDEC_WREN_OUTSIZE,
1518 .writearr = (const unsigned char[]){ JEDEC_WREN },
1519 .readcnt = 0,
1520 .readarr = NULL,
1521 }, {
1522 .writecnt = W25Q_WRSR_OUTSIZE,
1523 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1524 .readcnt = 0,
1525 .readarr = NULL,
1526 }, {
1527 .writecnt = 0,
1528 .writearr = NULL,
1529 .readcnt = 0,
1530 .readarr = NULL,
1531 }};
1532
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001533 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001534 if (result) {
1535 msg_cerr("%s failed during command execution\n",
1536 __func__);
1537 }
1538
1539 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001540 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001541
1542 return result;
1543}
1544
1545/*
1546 * Set/clear the SRP1 bit in status register 2.
1547 * FIXME: make this more generic if other chips use the same SR2 layout
1548 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001549static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001550{
1551 struct w25q_status sr1;
1552 struct w25q_status_2 sr2;
1553 uint8_t tmp, expected;
1554
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301555 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001556 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001557 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001558 memcpy(&sr2, &tmp, 1);
1559
1560 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1561
1562 sr2.srp1 = enable ? 1 : 0;
1563
1564 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001565 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001566
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001567 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001568 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1569 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1570 return 1;
1571
1572 return 0;
1573}
1574
1575enum wp_mode get_wp_mode(const char *mode_str)
1576{
1577 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1578
1579 if (!strcasecmp(mode_str, "hardware"))
1580 wp_mode = WP_MODE_HARDWARE;
1581 else if (!strcasecmp(mode_str, "power_cycle"))
1582 wp_mode = WP_MODE_POWER_CYCLE;
1583 else if (!strcasecmp(mode_str, "permanent"))
1584 wp_mode = WP_MODE_PERMANENT;
1585
1586 return wp_mode;
1587}
1588
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001589static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001590 enum wp_mode wp_mode)
1591{
1592 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001593 struct w25q_status_2 sr2;
1594 uint8_t tmp;
1595
1596 switch (wp_mode) {
1597 case WP_MODE_HARDWARE:
1598 ret = w25_set_srp0(flash, 0);
1599 break;
1600 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001601 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001602 memcpy(&sr2, &tmp, 1);
1603 if (sr2.srp1) {
1604 msg_cerr("%s(): must disconnect power to disable "
1605 "write-protection\n", __func__);
1606 } else {
1607 ret = 0;
1608 }
1609 break;
1610 case WP_MODE_PERMANENT:
1611 msg_cerr("%s(): cannot disable permanent write-protection\n",
1612 __func__);
1613 break;
1614 default:
1615 msg_cerr("%s(): invalid mode specified\n", __func__);
1616 break;
1617 }
1618
1619 if (ret)
1620 msg_cerr("%s(): error=%d.\n", __func__, ret);
1621 return ret;
1622}
1623
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001624static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001625{
1626 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1627}
1628
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001629static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001630 enum wp_mode wp_mode)
1631{
1632 int ret = 1;
1633 struct w25q_status sr1;
1634 struct w25q_status_2 sr2;
1635 uint8_t tmp;
1636
1637 switch (wp_mode) {
1638 case WP_MODE_HARDWARE:
1639 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1640 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1641 __func__);
1642 break;
1643 }
1644
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301645 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001646 memcpy(&sr1, &tmp, 1);
1647 if (sr1.srp0)
1648 ret = 0;
1649 else
1650 ret = w25_set_srp0(flash, 1);
1651
1652 break;
1653 case WP_MODE_POWER_CYCLE:
1654 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1655 msg_cerr("%s(): cannot disable hardware WP mode\n",
1656 __func__);
1657 break;
1658 }
1659
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001660 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001661 memcpy(&sr2, &tmp, 1);
1662 if (sr2.srp1)
1663 ret = 0;
1664 else
1665 ret = w25q_set_srp1(flash, 1);
1666
1667 break;
1668 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301669 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001670 memcpy(&sr1, &tmp, 1);
1671 if (sr1.srp0 == 0) {
1672 ret = w25_set_srp0(flash, 1);
1673 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001674 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001675 "permanent WP\n", __func__);
1676 break;
1677 }
1678 }
1679
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001680 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001681 memcpy(&sr2, &tmp, 1);
1682 if (sr2.srp1 == 0) {
1683 ret = w25q_set_srp1(flash, 1);
1684 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001685 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001686 "permanent WP\n", __func__);
1687 break;
1688 }
1689 }
1690
1691 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001692 default:
1693 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1694 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001695 }
1696
1697 if (ret)
1698 msg_cerr("%s(): error=%d.\n", __func__, ret);
1699 return ret;
1700}
1701
David Hendricksc3496092014-11-13 17:20:55 -08001702/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001703uint8_t mx25l_read_config_register(const struct flashctx *flash)
David Hendricksc3496092014-11-13 17:20:55 -08001704{
1705 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1706 unsigned char readarr[2]; /* leave room for dummy byte */
1707 int ret;
1708
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001709 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
David Hendricksc3496092014-11-13 17:20:55 -08001710 if (ret) {
Duncan Laurie870d8af2019-01-09 18:05:23 -08001711 msg_cdbg("RDCR failed!\n");
David Hendricksc3496092014-11-13 17:20:55 -08001712 readarr[0] = 0x00;
1713 }
1714
1715 return readarr[0];
1716}
David Hendricks1c09f802012-10-03 11:03:48 -07001717/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001718struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001719 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001720 .set_range = w25_set_range,
1721 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001722 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001723 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001724
1725};
1726
1727/* W25Q series has features such as a second status register and SFDP */
1728struct wp wp_w25q = {
1729 .list_ranges = w25_list_ranges,
1730 .set_range = w25_set_range,
1731 .enable = w25q_enable_writeprotect,
1732 /*
1733 * By default, disable hardware write-protection. We may change
1734 * this later if we want to add fine-grained write-protect disable
1735 * as a command-line option.
1736 */
1737 .disable = w25q_disable_writeprotect_default,
1738 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001739};
David Hendrickse0512a72014-07-15 20:30:47 -07001740
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001741/* W25Q large series has 4 block-protect bits */
1742struct wp wp_w25q_large = {
1743 .list_ranges = w25_list_ranges,
1744 .set_range = w25q_large_set_range,
1745 .enable = w25q_enable_writeprotect,
1746 /*
1747 * By default, disable hardware write-protection. We may change
1748 * this later if we want to add fine-grained write-protect disable
1749 * as a command-line option.
1750 */
1751 .disable = w25q_disable_writeprotect_default,
1752 .wp_status = w25q_large_wp_status,
1753};
1754
David Hendricksaf3944a2014-07-28 18:37:40 -07001755struct generic_range gd25q32_cmp0_ranges[] = {
1756 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001757 { { }, 0x00, {0, 0} },
1758 { { }, 0x08, {0, 0} },
1759 { { }, 0x10, {0, 0} },
1760 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001761
David Hendricks148a4bf2015-03-13 21:02:42 -07001762 { { }, 0x01, {0x3f0000, 64 * 1024} },
1763 { { }, 0x02, {0x3e0000, 128 * 1024} },
1764 { { }, 0x03, {0x3c0000, 256 * 1024} },
1765 { { }, 0x04, {0x380000, 512 * 1024} },
1766 { { }, 0x05, {0x300000, 1024 * 1024} },
1767 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001768
David Hendricks148a4bf2015-03-13 21:02:42 -07001769 { { }, 0x09, {0x000000, 64 * 1024} },
1770 { { }, 0x0a, {0x000000, 128 * 1024} },
1771 { { }, 0x0b, {0x000000, 256 * 1024} },
1772 { { }, 0x0c, {0x000000, 512 * 1024} },
1773 { { }, 0x0d, {0x000000, 1024 * 1024} },
1774 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001775
1776 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001777 { { }, 0x07, {0x000000, 4096 * 1024} },
1778 { { }, 0x0f, {0x000000, 4096 * 1024} },
1779 { { }, 0x17, {0x000000, 4096 * 1024} },
1780 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001781
David Hendricks148a4bf2015-03-13 21:02:42 -07001782 { { }, 0x11, {0x3ff000, 4 * 1024} },
1783 { { }, 0x12, {0x3fe000, 8 * 1024} },
1784 { { }, 0x13, {0x3fc000, 16 * 1024} },
1785 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1786 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1787 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001788
David Hendricks148a4bf2015-03-13 21:02:42 -07001789 { { }, 0x19, {0x000000, 4 * 1024} },
1790 { { }, 0x1a, {0x000000, 8 * 1024} },
1791 { { }, 0x1b, {0x000000, 16 * 1024} },
1792 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1793 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1794 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001795};
1796
1797struct generic_range gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001798 /* All, bp4 and bp3 => don't care */
1799 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1800 { { }, 0x08, {0x000000, 4096 * 1024} },
1801 { { }, 0x10, {0x000000, 4096 * 1024} },
1802 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001803
David Hendricks148a4bf2015-03-13 21:02:42 -07001804 { { }, 0x01, {0x000000, 4032 * 1024} },
1805 { { }, 0x02, {0x000000, 3968 * 1024} },
1806 { { }, 0x03, {0x000000, 3840 * 1024} },
1807 { { }, 0x04, {0x000000, 3584 * 1024} },
1808 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1809 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001810
David Hendricks148a4bf2015-03-13 21:02:42 -07001811 { { }, 0x09, {0x010000, 4032 * 1024} },
1812 { { }, 0x0a, {0x020000, 3968 * 1024} },
1813 { { }, 0x0b, {0x040000, 3840 * 1024} },
1814 { { }, 0x0c, {0x080000, 3584 * 1024} },
1815 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1816 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001817
Martin Roth563a1fe2017-04-18 14:26:27 -06001818 /* None, bp4 and bp3 => don't care */
1819 { { }, 0x07, {0, 0} }, /* None */
1820 { { }, 0x0f, {0, 0} },
1821 { { }, 0x17, {0, 0} },
1822 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001823
David Hendricks148a4bf2015-03-13 21:02:42 -07001824 { { }, 0x11, {0x000000, 4092 * 1024} },
1825 { { }, 0x12, {0x000000, 4088 * 1024} },
1826 { { }, 0x13, {0x000000, 4080 * 1024} },
1827 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1828 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1829 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001830
David Hendricks148a4bf2015-03-13 21:02:42 -07001831 { { }, 0x19, {0x001000, 4092 * 1024} },
1832 { { }, 0x1a, {0x002000, 4088 * 1024} },
1833 { { }, 0x1b, {0x040000, 4080 * 1024} },
1834 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1835 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1836 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001837};
1838
1839static struct generic_wp gd25q32_wp = {
1840 /* TODO: map second status register */
1841 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1842};
1843
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001844struct generic_range gd25q128_cmp0_ranges[] = {
1845 /* none, bp4 and bp3 => don't care, others = 0 */
1846 { { .tb = 0 }, 0x00, {0, 0} },
1847 { { .tb = 0 }, 0x08, {0, 0} },
1848 { { .tb = 0 }, 0x10, {0, 0} },
1849 { { .tb = 0 }, 0x18, {0, 0} },
1850
1851 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1852 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1853 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1854 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1855 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1856 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1857
1858 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1859 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1860 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1861 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1862 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1863 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1864
1865 /* all, bp4 and bp3 => don't care, others = 1 */
1866 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1867 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1868 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1869 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1870
1871 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1872 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1873 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1874 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1875 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1876
1877 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1878 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1879 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1880 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1881 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1882 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1883};
1884
1885struct generic_range gd25q128_cmp1_ranges[] = {
1886 /* none, bp4 and bp3 => don't care, others = 0 */
1887 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1888 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1889 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1890 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1891
1892 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1893 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1894 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1895 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1896 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1897 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1898
1899 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1900 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1901 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1902 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1903 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1904 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1905
1906 /* none, bp4 and bp3 => don't care, others = 1 */
1907 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1908 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1909 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1910 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1911 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1912
1913 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1914 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1915 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1916 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1917 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1918
1919 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1920 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1921 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1922 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1923 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1924 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1925};
1926
1927static struct generic_wp gd25q128_wp = {
1928 /* TODO: map second and third status registers */
1929 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1930};
1931
David Hendricks83541d32014-07-15 20:58:21 -07001932#if 0
1933/* FIXME: MX25L6405D has same ID as MX25L6406 */
1934static struct w25q_range mx25l6405d_ranges[] = {
1935 { X, 0, 0, {0, 0} }, /* none */
1936 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1937 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1938 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1939 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1940 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1941 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1942 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1943
1944 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1945 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1946 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1947 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1948 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1949 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1950 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1951 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1952};
1953#endif
1954
1955/* FIXME: MX25L6406 has same ID as MX25L6405D */
1956struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001957 { { }, 0, {0, 0} }, /* none */
1958 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1959 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1960 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1961 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1962 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1963 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001964
David Hendricks148a4bf2015-03-13 21:02:42 -07001965 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1966 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1967 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1968 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1969 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1970 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1971 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1972 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1973 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001974};
1975
1976static struct generic_wp mx25l6406e_wp = {
1977 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1978 .ranges = &mx25l6406e_ranges[0],
1979};
David Hendrickse0512a72014-07-15 20:30:47 -07001980
David Hendricksc3496092014-11-13 17:20:55 -08001981struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001982 { { }, 0, {0, 0} }, /* none */
1983 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1984 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1985 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001986
David Hendricks148a4bf2015-03-13 21:02:42 -07001987 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1988 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1989 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1990 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1991 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1992 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1993 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1994 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1995 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1996 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1997 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1998 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001999};
2000
2001struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002002 { { }, 0, {0, 0} }, /* none */
2003 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2004 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2005 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2006 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2007 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2008 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2009 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2010 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2011 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2012 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2013 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2014 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2015 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2016 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2017 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002018};
2019
2020static struct generic_wp mx25l6495f_wp = {
2021 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2022};
2023
Vic Yang848bfd12018-03-23 10:24:07 -07002024struct generic_range mx25l25635f_tb0_ranges[] = {
2025 { { }, 0, {0, 0} }, /* none */
2026 { { }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* block 511 */
2027 { { }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* blocks 510-511 */
2028 { { }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* blocks 508-511 */
2029 { { }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* blocks 504-511 */
2030 { { }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* blocks 496-511 */
2031 { { }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* blocks 480-511 */
2032 { { }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* blocks 448-511 */
2033 { { }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* blocks 384-511 */
2034 { { }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* blocks 256-511 */
2035 { { }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* all */
2036 { { }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* all */
2037 { { }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* all */
2038 { { }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* all */
2039 { { }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* all */
2040 { { }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* all */
2041};
2042
2043struct generic_range mx25l25635f_tb1_ranges[] = {
2044 { { }, 0, {0, 0} }, /* none */
2045 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2046 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2047 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2048 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2049 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2050 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2051 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2052 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
2053 { { }, 0x9, {0x000000, 64 * 256 * 1024} }, /* blocks 0-255 */
2054 { { }, 0xa, {0x000000, 64 * 512 * 1024} }, /* all */
2055 { { }, 0xb, {0x000000, 64 * 512 * 1024} }, /* all */
2056 { { }, 0xc, {0x000000, 64 * 512 * 1024} }, /* all */
2057 { { }, 0xd, {0x000000, 64 * 512 * 1024} }, /* all */
2058 { { }, 0xe, {0x000000, 64 * 512 * 1024} }, /* all */
2059 { { }, 0xf, {0x000000, 64 * 512 * 1024} }, /* all */
2060};
2061
2062static struct generic_wp mx25l25635f_wp = {
2063 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2064};
2065
David Hendricks148a4bf2015-03-13 21:02:42 -07002066struct generic_range s25fs128s_ranges[] = {
2067 { { .tb = 1 }, 0, {0, 0} }, /* none */
2068 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
2069 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
2070 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
2071 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
2072 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
2073 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
2074 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002075
David Hendricks148a4bf2015-03-13 21:02:42 -07002076 { { .tb = 0 }, 0, {0, 0} }, /* none */
2077 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
2078 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
2079 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
2080 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
2081 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
2082 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
2083 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002084};
2085
2086static struct generic_wp s25fs128s_wp = {
2087 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002088 .get_modifier_bits = s25f_get_modifier_bits,
2089 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08002090};
2091
David Hendricksc694bb82015-02-25 14:52:17 -08002092
David Hendricks148a4bf2015-03-13 21:02:42 -07002093struct generic_range s25fl256s_ranges[] = {
2094 { { .tb = 1 }, 0, {0, 0} }, /* none */
2095 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
2096 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
2097 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
2098 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
2099 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
2100 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
2101 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
2102
2103 { { .tb = 0 }, 0, {0, 0} }, /* none */
2104 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
2105 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
2106 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
2107 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
2108 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
2109 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
2110 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08002111};
2112
2113static struct generic_wp s25fl256s_wp = {
2114 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002115 .get_modifier_bits = s25f_get_modifier_bits,
2116 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08002117};
2118
David Hendrickse0512a72014-07-15 20:30:47 -07002119/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002120static int generic_range_table(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002121 struct generic_wp **wp,
2122 int *num_entries)
2123{
2124 *wp = NULL;
2125 *num_entries = 0;
2126
Patrick Georgif3fa2992017-02-02 16:24:44 +01002127 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07002128 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002129 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002130
Martin Roth563a1fe2017-04-18 14:26:27 -06002131 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07002132 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002133 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07002134 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002135
David Hendricksaf3944a2014-07-28 18:37:40 -07002136 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2137 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
2138 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
2139 } else { /* CMP == 1 */
2140 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
2141 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
2142 }
2143
2144 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002145 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07002146 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06002147 case GIGADEVICE_GD25LQ128CD: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002148 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002149 *wp = &gd25q128_wp;
2150
2151 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
2152 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
2153 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
2154 } else { /* CMP == 1 */
2155 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
2156 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
2157 }
2158
2159 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07002160 }
2161 default:
2162 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
2163 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002164 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07002165 return -1;
2166 }
2167 break;
David Hendricks83541d32014-07-15 20:58:21 -07002168 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002169 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07002170 case MACRONIX_MX25L6405:
2171 /* FIXME: MX25L64* chips have mixed capabilities and
2172 share IDs */
2173 *wp = &mx25l6406e_wp;
2174 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
2175 break;
David Hendricksc3496092014-11-13 17:20:55 -08002176 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002177 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08002178
2179 *wp = &mx25l6495f_wp;
2180 if (!(cr & (1 << 3))) { /* T/B == 0 */
2181 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
2182 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
2183 } else { /* T/B == 1 */
2184 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
2185 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
2186 }
2187 break;
2188 }
Vic Yang848bfd12018-03-23 10:24:07 -07002189 case MACRONIX_MX25L25635F: {
2190 uint8_t cr = mx25l_read_config_register(flash);
2191
2192 *wp = &mx25l25635f_wp;
2193 if (!(cr & (1 << 3))) { /* T/B == 0 */
2194 (*wp)->ranges = &mx25l25635f_tb0_ranges[0];
2195 *num_entries = ARRAY_SIZE(mx25l25635f_tb0_ranges);
2196 } else { /* T/B == 1 */
2197 (*wp)->ranges = &mx25l25635f_tb1_ranges[0];
2198 *num_entries = ARRAY_SIZE(mx25l25635f_tb1_ranges);
2199 }
2200 break;
2201 }
David Hendricks83541d32014-07-15 20:58:21 -07002202 default:
2203 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
2204 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002205 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07002206 return -1;
2207 }
2208 break;
David Hendricksa9884852014-12-11 15:31:12 -08002209 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002210 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08002211 case SPANSION_S25FS128S_L:
2212 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08002213 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002214 (*wp)->ranges = s25fs128s_ranges;
2215 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08002216 break;
2217 }
David Hendricksc694bb82015-02-25 14:52:17 -08002218 case SPANSION_S25FL256S_UL:
2219 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08002220 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002221 (*wp)->ranges = s25fl256s_ranges;
2222 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08002223 break;
2224 }
David Hendricksa9884852014-12-11 15:31:12 -08002225 default:
2226 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01002227 ", aborting\n", __func__, __LINE__,
2228 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08002229 return -1;
2230 }
2231 break;
David Hendrickse0512a72014-07-15 20:30:47 -07002232 default:
2233 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01002234 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07002235 return -1;
2236 }
2237
2238 return 0;
2239}
2240
2241/* Given a [start, len], this function finds a block protect bit combination
2242 * (if possible) and sets the corresponding bits in "status". Remaining bits
2243 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002244static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002245 unsigned int start, unsigned int len,
2246 uint8_t *status)
2247{
2248 struct generic_wp *wp;
2249 struct generic_range *r;
2250 int i, range_found = 0, num_entries;
2251 uint8_t bp_mask;
2252
2253 if (generic_range_table(flash, &wp, &num_entries))
2254 return -1;
2255
2256 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
2257 ((1 << wp->sr1.bp0_pos) - 1);
2258
2259 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
2260 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
2261 start, len, r->range.start, r->range.len);
2262 if ((start == r->range.start) && (len == r->range.len)) {
2263 *status &= ~(bp_mask);
2264 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07002265
2266 if (wp->set_modifier_bits) {
2267 if (wp->set_modifier_bits(flash, &r->m) < 0) {
2268 msg_cerr("error setting modifier "
2269 "bits for range.\n");
2270 return -1;
2271 }
2272 }
2273
David Hendrickse0512a72014-07-15 20:30:47 -07002274 range_found = 1;
2275 break;
2276 }
2277 }
2278
2279 if (!range_found) {
2280 msg_cerr("matching range not found\n");
2281 return -1;
2282 }
2283 return 0;
2284}
2285
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002286static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002287 const uint8_t sr1, unsigned int *start, unsigned int *len)
2288{
2289 struct generic_wp *wp;
2290 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07002291 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07002292 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07002293 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07002294
2295 if (generic_range_table(flash, &wp, &num_entries))
2296 return -1;
2297
David Hendricks148a4bf2015-03-13 21:02:42 -07002298 /* modifier bits may be compared more than once, so get them here */
2299 if (wp->get_modifier_bits) {
2300 if (wp->get_modifier_bits(flash, &m) < 0)
2301 return -1;
2302 }
2303
David Hendrickse0512a72014-07-15 20:30:47 -07002304 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
2305
2306 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07002307 if (wp->get_modifier_bits) {
2308 if (memcmp(&m, &r->m, sizeof(m)))
2309 continue;
2310 }
David Hendrickse0512a72014-07-15 20:30:47 -07002311 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
2312 if (sr1_bp == r->bp) {
2313 *start = r->range.start;
2314 *len = r->range.len;
2315 status_found = 1;
2316 break;
2317 }
2318 }
2319
2320 if (!status_found) {
2321 msg_cerr("matching status not found\n");
2322 return -1;
2323 }
2324 return 0;
2325}
2326
2327/* Given a [start, len], this function calls generic_range_to_status() to
2328 * convert it to flash-chip-specific range bits, then sets into status register.
2329 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002330static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002331 unsigned int start, unsigned int len)
2332{
2333 uint8_t status, expected;
2334
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302335 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002336 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2337
2338 expected = status; /* preserve non-bp bits */
2339 if (generic_range_to_status(flash, start, len, &expected))
2340 return -1;
2341
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302342 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002343
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302344 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002345 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2346 if (status != expected) {
2347 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
2348 expected, status);
2349 return 1;
2350 }
2351
2352 return 0;
2353}
2354
2355/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002356static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002357{
2358 uint8_t status, expected;
2359 struct generic_wp *wp;
2360 int num_entries;
2361
2362 if (generic_range_table(flash, &wp, &num_entries))
2363 return -1;
2364
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302365 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002366 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2367
2368 if (enable)
2369 expected |= 1 << wp->sr1.srp_pos;
2370 else
2371 expected &= ~(1 << wp->sr1.srp_pos);
2372
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302373 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002374
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302375 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002376 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2377 if (status != expected)
2378 return -1;
2379
2380 return 0;
2381}
2382
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002383static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002384 enum wp_mode wp_mode)
2385{
2386 int ret;
2387
2388 switch (wp_mode) {
2389 case WP_MODE_HARDWARE:
2390 ret = generic_set_srp0(flash, 1);
2391 break;
2392 default:
2393 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2394 return 1;
2395 }
2396
2397 if (ret)
2398 msg_cerr("%s(): error=%d.\n", __func__, ret);
2399 return ret;
2400}
2401
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002402static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002403{
2404 int ret;
2405
2406 ret = generic_set_srp0(flash, 0);
2407 if (ret)
2408 msg_cerr("%s(): error=%d.\n", __func__, ret);
2409 return ret;
2410}
2411
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002412static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002413{
2414 struct generic_wp *wp;
2415 struct generic_range *r;
2416 int i, num_entries;
2417
2418 if (generic_range_table(flash, &wp, &num_entries))
2419 return -1;
2420
2421 r = &wp->ranges[0];
2422 for (i = 0; i < num_entries; i++) {
2423 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2424 r->range.start, r->range.len);
2425 r++;
2426 }
2427
2428 return 0;
2429}
2430
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002431static int generic_wp_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002432{
2433 uint8_t sr1;
2434 unsigned int start, len;
2435 int ret = 0;
2436 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002437 int num_entries, wp_en;
2438
2439 if (generic_range_table(flash, &wp, &num_entries))
2440 return -1;
2441
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302442 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002443 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2444
2445 msg_cinfo("WP: status: 0x%04x\n", sr1);
2446 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2447 /* FIXME: SRP1 is not really generic, but we probably should print
2448 * it anyway to have consistent output. #legacycruft */
2449 msg_cinfo("WP: status.srp1: %x\n", 0);
2450 msg_cinfo("WP: write protect is %s.\n",
2451 wp_en ? "enabled" : "disabled");
2452
2453 msg_cinfo("WP: write protect range: ");
2454 if (generic_status_to_range(flash, sr1, &start, &len)) {
2455 msg_cinfo("(cannot resolve the range)\n");
2456 ret = -1;
2457 } else {
2458 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2459 }
2460
2461 return ret;
2462}
2463
2464struct wp wp_generic = {
2465 .list_ranges = generic_list_ranges,
2466 .set_range = generic_set_range,
2467 .enable = generic_enable_writeprotect,
2468 .disable = generic_disable_writeprotect,
2469 .wp_status = generic_wp_status,
2470};