blob: 6113031a799691ea434c9a0f01c765d0b1b9c482 [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>
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100020#include <strings.h>
David Hendricksf7924d12010-06-10 21:26:44 -070021
22#include "flash.h"
23#include "flashchips.h"
24#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080025#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070026#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070027
David Hendricks1c09f802012-10-03 11:03:48 -070028/*
David Hendricksf7924d12010-06-10 21:26:44 -070029 * The following procedures rely on look-up tables to match the user-specified
30 * range with the chip's supported ranges. This turned out to be the most
31 * elegant approach since diferent flash chips use different levels of
32 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070033 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070034 */
35
36struct wp_range {
37 unsigned int start; /* starting address */
38 unsigned int len; /* len */
39};
40
41enum bit_state {
42 OFF = 0,
43 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080044 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070045};
46
David Hendrickse0512a72014-07-15 20:30:47 -070047/*
48 * Generic write-protection schema for 25-series SPI flash chips. This assumes
49 * there is a status register that contains one or more consecutive bits which
50 * determine which address range is protected.
51 */
52
53struct status_register_layout {
54 int bp0_pos; /* position of BP0 */
55 int bp_bits; /* number of block protect bits */
56 int srp_pos; /* position of status register protect enable bit */
57};
58
Edward O'Callaghan91b38272019-12-04 17:12:43 +110059/*
60 * The following ranges and functions are useful for representing the
61 * writeprotect schema in which there are typically 5 bits of
62 * relevant information stored in status register 1:
63 * m.sec: This bit indicates the units (sectors vs. blocks)
64 * m.tb: The top-bottom bit indicates if the affected range is at the top of
65 * the flash memory's address space or at the bottom.
66 * bp: Bitmask representing the number of affected sectors/blocks.
67 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +110068struct wp_range_descriptor {
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110069 struct modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070070 unsigned int bp; /* block protect bitfield */
71 struct wp_range range;
72};
73
Edward O'Callaghana3edcb22019-12-05 14:30:50 +110074struct wp_context {
David Hendrickse0512a72014-07-15 20:30:47 -070075 struct status_register_layout sr1; /* status register 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +110076 struct wp_range_descriptor *descrs;
David Hendricks148a4bf2015-03-13 21:02:42 -070077
78 /*
79 * Some chips store modifier bits in one or more special control
80 * registers instead of the status register like many older SPI NOR
81 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
82 * any chip-specific operations necessary to get/set these bit values.
83 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 int (*get_modifier_bits)(const struct flashctx *flash,
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110085 struct modifier_bits *m);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070086 int (*set_modifier_bits)(const struct flashctx *flash,
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +110087 struct modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070088};
89
Edward O'Callaghanc69f6b82019-12-05 16:49:21 +110090struct w25q_status {
91 /* this maps to register layout -- do not change ordering */
92 unsigned char busy : 1;
93 unsigned char wel : 1;
94 unsigned char bp0 : 1;
95 unsigned char bp1 : 1;
96 unsigned char bp2 : 1;
97 unsigned char tb : 1;
98 unsigned char sec : 1;
99 unsigned char srp0 : 1;
100} __attribute__ ((packed));
101
102/* Status register for large flash layouts with 4 BP bits */
103struct w25q_status_large {
104 unsigned char busy : 1;
105 unsigned char wel : 1;
106 unsigned char bp0 : 1;
107 unsigned char bp1 : 1;
108 unsigned char bp2 : 1;
109 unsigned char bp3 : 1;
110 unsigned char tb : 1;
111 unsigned char srp0 : 1;
112} __attribute__ ((packed));
113
114struct w25q_status_2 {
115 unsigned char srp1 : 1;
116 unsigned char qe : 1;
117 unsigned char rsvd : 6;
118} __attribute__ ((packed));
119
120int w25_range_to_status(const struct flashctx *flash,
121 unsigned int start, unsigned int len,
122 struct w25q_status *status);
123int w25_status_to_range(const struct flashctx *flash,
124 const struct w25q_status *status,
125 unsigned int *start, unsigned int *len);
126
David Hendrickse0512a72014-07-15 20:30:47 -0700127/*
David Hendrickse0512a72014-07-15 20:30:47 -0700128 * Mask to extract write-protect enable and range bits
129 * Status register 1:
130 * SRP0: bit 7
131 * range(BP2-BP0): bit 4-2
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800132 * range(BP3-BP0): bit 5-2 (large chips)
David Hendrickse0512a72014-07-15 20:30:47 -0700133 * Status register 2:
134 * SRP1: bit 1
135 */
136#define MASK_WP_AREA (0x9C)
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800137#define MASK_WP_AREA_LARGE (0x9C)
David Hendrickse0512a72014-07-15 20:30:47 -0700138#define MASK_WP2_AREA (0x01)
139
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000140static struct wp_range_descriptor en25f40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100141 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
142 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 504 * 1024} },
143 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 496 * 1024} },
144 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 480 * 1024} },
145 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 448 * 1024} },
146 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 384 * 1024} },
147 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 256 * 1024} },
148 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 512 * 1024} },
David Hendricks57566ed2010-08-16 18:24:45 -0700149};
150
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000151static struct wp_range_descriptor en25q40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100152 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
153 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 504 * 1024} },
154 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 496 * 1024} },
155 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 480 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700156
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100157 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 448 * 1024} },
158 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 384 * 1024} },
159 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
160 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700161};
162
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000163static struct wp_range_descriptor en25q80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100164 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
165 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 1016 * 1024} },
166 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 1008 * 1024} },
167 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 992 * 1024} },
168 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 960 * 1024} },
169 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 896 * 1024} },
170 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 768 * 1024} },
171 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 1024 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700172};
173
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000174static struct wp_range_descriptor en25q32_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100175 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
176 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 4032 * 1024} },
177 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 3968 * 1024} },
178 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 3840 * 1024} },
179 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 3584 * 1024} },
180 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 3072 * 1024} },
181 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 2048 * 1024} },
182 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700183
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100184 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
185 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 4032 * 1024} },
186 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 3968 * 1024} },
187 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 3840 * 1024} },
188 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 3584 * 1024} },
189 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 3072 * 1024} },
190 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 2048 * 1024} },
191 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700192};
193
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000194static struct wp_range_descriptor en25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100195 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
196 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 8128 * 1024} },
197 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 8064 * 1024} },
198 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 7936 * 1024} },
199 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 7680 * 1024} },
200 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 7168 * 1024} },
201 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 6144 * 1024} },
202 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 8192 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700203
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100204 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
205 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 8128 * 1024} },
206 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 8064 * 1024} },
207 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 7936 * 1024} },
208 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 7680 * 1024} },
209 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 7168 * 1024} },
210 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 6144 * 1024} },
211 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 8192 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700212};
213
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000214static struct wp_range_descriptor en25q128_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100215 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
216 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 16320 * 1024} },
217 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 16256 * 1024} },
218 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 16128 * 1024} },
219 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 15872 * 1024} },
220 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 15360 * 1024} },
221 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 14336 * 1024} },
222 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 16384 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700223
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100224 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
225 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 16320 * 1024} },
226 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 16256 * 1024} },
227 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 16128 * 1024} },
228 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x080000, 15872 * 1024} },
229 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x100000, 15360 * 1024} },
230 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x200000, 14336 * 1024} },
231 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 16384 * 1024} },
David Hendrickse185bf22011-05-24 15:34:18 -0700232};
233
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000234static struct wp_range_descriptor en25s64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100235 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
236 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 8064 * 1024} },
237 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 7936 * 1024} },
238 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 7680 * 1024} },
239 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 7168 * 1024} },
240 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 6144 * 1024} },
241 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 4096 * 1024} },
242 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 8192 * 1024} },
Marc Jonesb2f90022014-04-29 17:37:23 -0600243
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100244 { .m = { .sec = 0, .tb = 1 }, 0, {0, 0} }, /* none */
245 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x7e0000, 128 * 1024} },
246 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x7c0000, 256 * 1024} },
247 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x780000, 512 * 1024} },
248 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x700000, 1024 * 1024} },
249 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x600000, 2048 * 1024} },
250 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x400000, 4096 * 1024} },
251 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 8192 * 1024} },
Marc Jonesb2f90022014-04-29 17:37:23 -0600252};
253
David Hendricksf8f00c72011-02-01 12:39:46 -0800254/* mx25l1005 ranges also work for the mx25l1005c */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100255static struct wp_range_descriptor mx25l1005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100256 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
257 { .m = { .sec = X, .tb = X }, 0x1, {0x010000, 64 * 1024} },
258 { .m = { .sec = X, .tb = X }, 0x2, {0x000000, 128 * 1024} },
259 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 128 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800260};
261
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100262static struct wp_range_descriptor mx25l2005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100263 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
264 { .m = { .sec = X, .tb = X }, 0x1, {0x030000, 64 * 1024} },
265 { .m = { .sec = X, .tb = X }, 0x2, {0x020000, 128 * 1024} },
266 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 256 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800267};
268
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100269static struct wp_range_descriptor mx25l4005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100270 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
271 { .m = { .sec = X, .tb = X }, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
272 { .m = { .sec = X, .tb = X }, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
273 { .m = { .sec = X, .tb = X }, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
274 { .m = { .sec = X, .tb = X }, 0x4, {0x000000, 512 * 1024} },
275 { .m = { .sec = X, .tb = X }, 0x5, {0x000000, 512 * 1024} },
276 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 512 * 1024} },
277 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 512 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800278};
279
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100280static struct wp_range_descriptor mx25l8005_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100281 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
282 { .m = { .sec = X, .tb = X }, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
283 { .m = { .sec = X, .tb = X }, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
284 { .m = { .sec = X, .tb = X }, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
285 { .m = { .sec = X, .tb = X }, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
286 { .m = { .sec = X, .tb = X }, 0x5, {0x000000, 1024 * 1024} },
287 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
288 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf8f00c72011-02-01 12:39:46 -0800289};
290
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100291static struct wp_range_descriptor mx25l1605d_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100292 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
293 { .m = { .sec = X, .tb = 0 }, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
294 { .m = { .sec = X, .tb = 0 }, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
295 { .m = { .sec = X, .tb = 0 }, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
296 { .m = { .sec = X, .tb = 0 }, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
297 { .m = { .sec = X, .tb = 0 }, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
298 { .m = { .sec = X, .tb = 0 }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
299 { .m = { .sec = X, .tb = 0 }, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800300
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100301 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 2048 * 1024} },
302 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
303 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
304 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
305 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
306 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
307 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
308 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
David Hendricksf8f00c72011-02-01 12:39:46 -0800309};
310
311/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100312static struct wp_range_descriptor mx25l3205d_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100313 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
314 { .m = { .sec = X, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
315 { .m = { .sec = X, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
316 { .m = { .sec = X, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
317 { .m = { .sec = X, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
318 { .m = { .sec = X, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
319 { .m = { .sec = X, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
320 { .m = { .sec = X, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksac72e362010-08-16 18:20:03 -0700321
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100322 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 4096 * 1024} },
323 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
324 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 3072 * 1024} },
325 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 3584 * 1024} },
326 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 3840 * 1024} },
327 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 3968 * 1024} },
328 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 4032 * 1024} },
329 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksac72e362010-08-16 18:20:03 -0700330};
331
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100332static struct wp_range_descriptor mx25u3235e_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100333 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
334 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
335 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
336 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
337 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
338 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
339 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
340 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x000000, 4096 * 1024} },
Vincent Palatin87e092a2013-02-28 15:46:14 -0800341
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100342 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 4096 * 1024} },
343 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 2048 * 1024} },
344 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 3072 * 1024} },
345 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 3584 * 1024} },
346 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 3840 * 1024} },
347 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 3968 * 1024} },
348 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4032 * 1024} },
349 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 4096 * 1024} },
Vincent Palatin87e092a2013-02-28 15:46:14 -0800350};
351
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100352static struct wp_range_descriptor mx25u6435e_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100353 { .m = { .sec = X, .tb = 0 }, 0, {0, 0} }, /* none */
354 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
355 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
356 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
357 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
358 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
359 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
360 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
Jongpil66a96492014-08-14 17:59:06 +0900361
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100362 { .m = { .sec = 0, .tb = 1 }, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
363 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
364 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
365 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
366 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
367 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
368 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
369 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
Jongpil66a96492014-08-14 17:59:06 +0900370};
371
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600372#define MX25U12835E_TB (1 << 3)
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100373static struct wp_range_descriptor mx25u12835e_tb0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100374 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
375 { .m = { .sec = 0, .tb = 0 }, 0x1, {0xff0000, 1 * 64 * 1024} }, /* block 255 */
376 { .m = { .sec = 0, .tb = 0 }, 0x2, {0xfe0000, 2 * 64 * 1024} }, /* blocks 254-255 */
377 { .m = { .sec = 0, .tb = 0 }, 0x3, {0xfc0000, 4 * 64 * 1024} }, /* blocks 252-255 */
378 { .m = { .sec = 0, .tb = 0 }, 0x4, {0xf80000, 8 * 64 * 1024} }, /* blocks 248-255 */
379 { .m = { .sec = 0, .tb = 0 }, 0x5, {0xf00000, 16 * 64 * 1024} }, /* blocks 240-255 */
380 { .m = { .sec = 0, .tb = 0 }, 0x6, {0xe00000, 32 * 64 * 1024} }, /* blocks 224-255 */
381 { .m = { .sec = 0, .tb = 0 }, 0x7, {0xc00000, 64 * 64 * 1024} }, /* blocks 192-255 */
382 { .m = { .sec = 0, .tb = 0 }, 0x8, {0x800000, 128 * 64 * 1024} }, /* blocks 128-255 */
383 { .m = { .sec = 0, .tb = 0 }, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
384 { .m = { .sec = 0, .tb = 0 }, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
385 { .m = { .sec = 0, .tb = 0 }, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
386 { .m = { .sec = 0, .tb = 0 }, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
387 { .m = { .sec = 0, .tb = 0 }, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
388 { .m = { .sec = 0, .tb = 0 }, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
389 { .m = { .sec = 0, .tb = 0 }, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -0600390};
Alex Lu831c6092017-11-02 23:19:34 -0700391
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100392static struct wp_range_descriptor mx25u12835e_tb1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100393 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 1 * 64 * 1024} }, /* block 0 */
394 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
395 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
396 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
397 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
398 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
399 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
400 { .m = { .sec = 0, .tb = 1 }, 0x8, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
401 { .m = { .sec = 0, .tb = 1 }, 0x9, {0x000000, 256 * 64 * 1024} }, /* blocks all */
402 { .m = { .sec = 0, .tb = 1 }, 0xa, {0x000000, 256 * 64 * 1024} }, /* blocks all */
403 { .m = { .sec = 0, .tb = 1 }, 0xb, {0x000000, 256 * 64 * 1024} }, /* blocks all */
404 { .m = { .sec = 0, .tb = 1 }, 0xc, {0x000000, 256 * 64 * 1024} }, /* blocks all */
405 { .m = { .sec = 0, .tb = 1 }, 0xd, {0x000000, 256 * 64 * 1024} }, /* blocks all */
406 { .m = { .sec = 0, .tb = 1 }, 0xe, {0x000000, 256 * 64 * 1024} }, /* blocks all */
407 { .m = { .sec = 0, .tb = 1 }, 0xf, {0x000000, 256 * 64 * 1024} }, /* blocks all */
Alex Lu831c6092017-11-02 23:19:34 -0700408};
409
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100410static struct wp_range_descriptor n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700411 /*
412 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
413 * (block protect bit 3). It is only useful when all blocks are to
414 * be write-protected.
415 */
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100416 { .m = { .sec = 0, .tb = 0 }, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700417
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100418 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
419 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
420 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
421 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
422 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
423 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
424 { .m = { .sec = 0, .tb = 0 }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700425
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100426 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
427 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
428 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
429 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
430 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
431 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
432 { .m = { .sec = 0, .tb = 1 }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700433
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100434 { .m = { .sec = X, .tb = 1 }, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
435 { .m = { .sec = X, .tb = 1 }, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
436 { .m = { .sec = X, .tb = 1 }, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
437 { .m = { .sec = X, .tb = 1 }, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
438 { .m = { .sec = X, .tb = 1 }, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
439 { .m = { .sec = X, .tb = 1 }, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
440 { .m = { .sec = X, .tb = 1 }, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
441 { .m = { .sec = X, .tb = 1 }, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
David Hendricksbfa624b2012-07-24 12:47:59 -0700442};
443
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100444static struct wp_range_descriptor w25q16_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100445 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
446 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x1f0000, 64 * 1024} },
447 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x1e0000, 128 * 1024} },
448 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x1c0000, 256 * 1024} },
449 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x180000, 512 * 1024} },
450 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x100000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700451
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100452 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
453 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
454 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
455 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
456 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 1024 * 1024} },
457 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 2048 * 1024} },
458 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700459
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100460 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x1ff000, 4 * 1024} },
461 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x1fe000, 8 * 1024} },
462 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x1fc000, 16 * 1024} },
463 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x1f8000, 32 * 1024} },
464 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700465
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100466 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
467 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
468 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
469 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
470 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700471};
472
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100473static struct wp_range_descriptor w25q32_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100474 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
475 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x3f0000, 64 * 1024} },
476 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x3e0000, 128 * 1024} },
477 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x3c0000, 256 * 1024} },
478 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x380000, 512 * 1024} },
479 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x300000, 1024 * 1024} },
480 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700481
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100482 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
483 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
484 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
485 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
486 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 1024 * 1024} },
487 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 2048 * 1024} },
488 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 4096 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700489
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100490 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x3ff000, 4 * 1024} },
491 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x3fe000, 8 * 1024} },
492 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x3fc000, 16 * 1024} },
493 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x3f8000, 32 * 1024} },
494 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x3f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700495
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100496 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
497 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
498 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
499 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
500 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700501};
502
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100503static struct wp_range_descriptor w25q80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100504 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
505 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x0f0000, 64 * 1024} },
506 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x0e0000, 128 * 1024} },
507 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x0c0000, 256 * 1024} },
508 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x080000, 512 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700509
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100510 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
511 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
512 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
513 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
514 { .m = { .sec = X, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
515 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700516
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100517 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x1ff000, 4 * 1024} },
518 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x1fe000, 8 * 1024} },
519 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x1fc000, 16 * 1024} },
520 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x1f8000, 32 * 1024} },
521 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x1f8000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700522
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100523 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
524 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
525 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
526 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
527 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700528};
529
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100530static struct wp_range_descriptor w25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100531 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
David Hendricks2c4a76c2010-06-28 14:00:43 -0700532
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100533 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7e0000, 128 * 1024} },
534 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7c0000, 256 * 1024} },
535 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x780000, 512 * 1024} },
536 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x700000, 1024 * 1024} },
537 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x600000, 2048 * 1024} },
538 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x400000, 4096 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700539
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100540 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 128 * 1024} },
541 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
542 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
543 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 1024 * 1024} },
544 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 2048 * 1024} },
545 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4096 * 1024} },
546 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 8192 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700547
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100548 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x7ff000, 4 * 1024} },
549 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x7fe000, 8 * 1024} },
550 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x7fc000, 16 * 1024} },
551 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x7f8000, 32 * 1024} },
552 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x7f8000, 32 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700553
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100554 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
555 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
556 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
557 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
558 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
David Hendricks2c4a76c2010-06-28 14:00:43 -0700559};
560
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100561static struct wp_range_descriptor w25rq128_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100562 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530563
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100564 { .m = { .sec = 0, .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
565 { .m = { .sec = 0, .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
566 { .m = { .sec = 0, .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
567 { .m = { .sec = 0, .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
568 { .m = { .sec = 0, .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
569 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530570
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100571 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
572 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
573 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
574 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
575 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
576 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530577
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100578 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530579
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100580 { .m = { .sec = 1, .tb = 0 }, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
581 { .m = { .sec = 1, .tb = 0 }, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
582 { .m = { .sec = 1, .tb = 0 }, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
583 { .m = { .sec = 1, .tb = 0 }, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
584 { .m = { .sec = 1, .tb = 0 }, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700585
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100586 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
587 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
588 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
589 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
590 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700591};
592
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100593static struct wp_range_descriptor w25rq128_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100594 { .m = { .sec = X, .tb = X }, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700595
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100596 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
597 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
598 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
599 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
600 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
601 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700602
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100603 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
604 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
605 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
606 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
607 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
608 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700609
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100610 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 0} }, /* NONE */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700611
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100612 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
613 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
614 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
615 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
616 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700617
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100618 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
619 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
620 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
621 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
622 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530623};
624
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100625static struct wp_range_descriptor w25rq256_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100626 { .m = { .sec = X, .tb = X }, 0x0, {0x0000000, 0x0000000} }, /* NONE */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800627
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100628 { .m = { .sec = X, .tb = 0 }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* Upper 1/512 */
629 { .m = { .sec = X, .tb = 0 }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* Upper 1/256 */
630 { .m = { .sec = X, .tb = 0 }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* Upper 1/128 */
631 { .m = { .sec = X, .tb = 0 }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* Upper 1/64 */
632 { .m = { .sec = X, .tb = 0 }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* Upper 1/32 */
633 { .m = { .sec = X, .tb = 0 }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* Upper 1/16 */
634 { .m = { .sec = X, .tb = 0 }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* Upper 1/8 */
635 { .m = { .sec = X, .tb = 0 }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* Upper 1/4 */
636 { .m = { .sec = X, .tb = 0 }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800637
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100638 { .m = { .sec = X, .tb = 1 }, 0x1, {0x0000000, 64 * 1 * 1024} }, /* Lower 1/512 */
639 { .m = { .sec = X, .tb = 1 }, 0x2, {0x0000000, 64 * 2 * 1024} }, /* Lower 1/256 */
640 { .m = { .sec = X, .tb = 1 }, 0x3, {0x0000000, 64 * 4 * 1024} }, /* Lower 1/128 */
641 { .m = { .sec = X, .tb = 1 }, 0x4, {0x0000000, 64 * 8 * 1024} }, /* Lower 1/64 */
642 { .m = { .sec = X, .tb = 1 }, 0x5, {0x0000000, 64 * 16 * 1024} }, /* Lower 1/32 */
643 { .m = { .sec = X, .tb = 1 }, 0x6, {0x0000000, 64 * 32 * 1024} }, /* Lower 1/16 */
644 { .m = { .sec = X, .tb = 1 }, 0x7, {0x0000000, 64 * 64 * 1024} }, /* Lower 1/8 */
645 { .m = { .sec = X, .tb = 1 }, 0x8, {0x0000000, 64 * 128 * 1024} }, /* Lower 1/4 */
646 { .m = { .sec = X, .tb = 1 }, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800647
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100648 { .m = { .sec = X, .tb = X }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* ALL */
649 { .m = { .sec = X, .tb = X }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* ALL */
650 { .m = { .sec = X, .tb = X }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* ALL */
651 { .m = { .sec = X, .tb = X }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* ALL */
652 { .m = { .sec = X, .tb = X }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* ALL */
653 { .m = { .sec = X, .tb = X }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* ALL */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800654};
655
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100656static struct wp_range_descriptor w25rq256_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100657 { .m = { .sec = X, .tb = X }, 0x0, {0x0000000, 64 * 512 * 1024} }, /* ALL */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800658
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100659 { .m = { .sec = X, .tb = 0 }, 0x1, {0x0000000, 64 * 511 * 1024} }, /* Lower 511/512 */
660 { .m = { .sec = X, .tb = 0 }, 0x2, {0x0000000, 64 * 510 * 1024} }, /* Lower 255/256 */
661 { .m = { .sec = X, .tb = 0 }, 0x3, {0x0000000, 64 * 508 * 1024} }, /* Lower 127/128 */
662 { .m = { .sec = X, .tb = 0 }, 0x4, {0x0000000, 64 * 504 * 1024} }, /* Lower 63/64 */
663 { .m = { .sec = X, .tb = 0 }, 0x5, {0x0000000, 64 * 496 * 1024} }, /* Lower 31/32 */
664 { .m = { .sec = X, .tb = 0 }, 0x6, {0x0000000, 64 * 480 * 1024} }, /* Lower 15/16 */
665 { .m = { .sec = X, .tb = 0 }, 0x7, {0x0000000, 64 * 448 * 1024} }, /* Lower 7/8 */
666 { .m = { .sec = X, .tb = 0 }, 0x8, {0x0000000, 64 * 384 * 1024} }, /* Lower 3/4 */
667 { .m = { .sec = X, .tb = 0 }, 0x9, {0x0000000, 64 * 256 * 1024} }, /* Lower 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800668
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100669 { .m = { .sec = X, .tb = 1 }, 0x1, {0x0010000, 64 * 511 * 1024} }, /* Upper 511/512 */
670 { .m = { .sec = X, .tb = 1 }, 0x2, {0x0020000, 64 * 510 * 1024} }, /* Upper 255/256 */
671 { .m = { .sec = X, .tb = 1 }, 0x3, {0x0040000, 64 * 508 * 1024} }, /* Upper 127/128 */
672 { .m = { .sec = X, .tb = 1 }, 0x4, {0x0080000, 64 * 504 * 1024} }, /* Upper 63/64 */
673 { .m = { .sec = X, .tb = 1 }, 0x5, {0x0100000, 64 * 496 * 1024} }, /* Upper 31/32 */
674 { .m = { .sec = X, .tb = 1 }, 0x6, {0x0200000, 64 * 480 * 1024} }, /* Upper 15/16 */
675 { .m = { .sec = X, .tb = 1 }, 0x7, {0x0400000, 64 * 448 * 1024} }, /* Upper 7/8 */
676 { .m = { .sec = X, .tb = 1 }, 0x8, {0x0800000, 64 * 384 * 1024} }, /* Upper 3/4 */
677 { .m = { .sec = X, .tb = 1 }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* Upper 1/2 */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800678
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100679 { .m = { .sec = X, .tb = X }, 0xa, {0x0000000, 0x0000000} }, /* NONE */
680 { .m = { .sec = X, .tb = X }, 0xb, {0x0000000, 0x0000000} }, /* NONE */
681 { .m = { .sec = X, .tb = X }, 0xc, {0x0000000, 0x0000000} }, /* NONE */
682 { .m = { .sec = X, .tb = X }, 0xd, {0x0000000, 0x0000000} }, /* NONE */
683 { .m = { .sec = X, .tb = X }, 0xe, {0x0000000, 0x0000000} }, /* NONE */
684 { .m = { .sec = X, .tb = X }, 0xf, {0x0000000, 0x0000000} }, /* NONE */
Duncan Laurie1801f7c2019-01-09 18:02:51 -0800685};
686
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000687static struct wp_range_descriptor w25x10_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100688 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
689 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x010000, 64 * 1024} },
690 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
691 { .m = { .sec = X, .tb = X }, 0x2, {0x000000, 128 * 1024} },
692 { .m = { .sec = X, .tb = X }, 0x3, {0x000000, 128 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800693};
694
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000695static struct wp_range_descriptor w25x20_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100696 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
697 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x030000, 64 * 1024} },
698 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x020000, 128 * 1024} },
699 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
700 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
701 { .m = { .sec = 0, .tb = X }, 0x3, {0x000000, 256 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800702};
703
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000704static struct wp_range_descriptor w25x40_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100705 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
706 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x070000, 64 * 1024} },
707 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x060000, 128 * 1024} },
708 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x040000, 256 * 1024} },
709 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
710 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
711 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
712 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 512 * 1024} },
713 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 512 * 1024} },
714 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 512 * 1024} },
715 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700716};
717
Edward O'Callaghan3b996502020-04-12 20:46:51 +1000718static struct wp_range_descriptor w25x80_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100719 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
720 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x0F0000, 64 * 1024} },
721 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x0E0000, 128 * 1024} },
722 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x0C0000, 256 * 1024} },
723 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x080000, 512 * 1024} },
724 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
725 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
726 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
727 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 512 * 1024} },
728 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 1024 * 1024} },
729 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 1024 * 1024} },
730 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 1024 * 1024} },
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800731};
732
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100733static struct wp_range_descriptor gd25q40_cmp0_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100734 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* None */
735 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x070000, 64 * 1024} },
736 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x060000, 128 * 1024} },
737 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x040000, 256 * 1024} },
738 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 64 * 1024} },
739 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 128 * 1024} },
740 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 256 * 1024} },
741 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 512 * 1024} }, /* All */
742 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 512 * 1024} }, /* All */
743 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 512 * 1024} }, /* All */
744 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 512 * 1024} }, /* All */
745 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x07F000, 4 * 1024} },
746 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x07E000, 8 * 1024} },
747 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x07C000, 16 * 1024} },
748 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x078000, 32 * 1024} },
749 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x078000, 32 * 1024} },
750 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x078000, 32 * 1024} },
751 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
752 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
753 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
754 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
755 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
756 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x000000, 32 * 1024} },
757 { .m = { .sec = 1, .tb = X }, 0x7, {0x000000, 512 * 1024} }, /* All */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600758};
759
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100760static struct wp_range_descriptor gd25q40_cmp1_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100761 { .m = { .sec = X, .tb = X }, 0x0, {0x000000, 512 * 1024} }, /* ALL */
762 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x000000, 448 * 1024} },
763 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x000000, 384 * 1024} },
764 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x000000, 256 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600765
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100766 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x010000, 448 * 1024} },
767 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x020000, 384 * 1024} },
768 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x040000, 256 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600769
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100770 { .m = { .sec = 0, .tb = X }, 0x4, {0x000000, 0} }, /* None */
771 { .m = { .sec = 0, .tb = X }, 0x5, {0x000000, 0} }, /* None */
772 { .m = { .sec = 0, .tb = X }, 0x6, {0x000000, 0} }, /* None */
773 { .m = { .sec = 0, .tb = X }, 0x7, {0x000000, 0} }, /* None */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600774
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100775 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x000000, 508 * 1024} },
776 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x000000, 504 * 1024} },
777 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x000000, 496 * 1024} },
778 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x000000, 480 * 1024} },
779 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x000000, 480 * 1024} },
780 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x000000, 480 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600781
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100782 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x001000, 508 * 1024} },
783 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x002000, 504 * 1024} },
784 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x004000, 496 * 1024} },
785 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x008000, 480 * 1024} },
786 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x008000, 480 * 1024} },
787 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x008000, 480 * 1024} },
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600788
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100789 { .m = { .sec = 1, .tb = X }, 0x7, {0x000000, 0} }, /* None */
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600790};
791
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100792static struct wp_range_descriptor gd25q64_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100793 { .m = { .sec = X, .tb = X }, 0, {0, 0} }, /* none */
794 { .m = { .sec = 0, .tb = 0 }, 0x1, {0x7e0000, 128 * 1024} },
795 { .m = { .sec = 0, .tb = 0 }, 0x2, {0x7c0000, 256 * 1024} },
796 { .m = { .sec = 0, .tb = 0 }, 0x3, {0x780000, 512 * 1024} },
797 { .m = { .sec = 0, .tb = 0 }, 0x4, {0x700000, 1024 * 1024} },
798 { .m = { .sec = 0, .tb = 0 }, 0x5, {0x600000, 2048 * 1024} },
799 { .m = { .sec = 0, .tb = 0 }, 0x6, {0x400000, 4096 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700800
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100801 { .m = { .sec = 0, .tb = 1 }, 0x1, {0x000000, 128 * 1024} },
802 { .m = { .sec = 0, .tb = 1 }, 0x2, {0x000000, 256 * 1024} },
803 { .m = { .sec = 0, .tb = 1 }, 0x3, {0x000000, 512 * 1024} },
804 { .m = { .sec = 0, .tb = 1 }, 0x4, {0x000000, 1024 * 1024} },
805 { .m = { .sec = 0, .tb = 1 }, 0x5, {0x000000, 2048 * 1024} },
806 { .m = { .sec = 0, .tb = 1 }, 0x6, {0x000000, 4096 * 1024} },
807 { .m = { .sec = X, .tb = X }, 0x7, {0x000000, 8192 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700808
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100809 { .m = { .sec = 1, .tb = 0 }, 0x1, {0x7ff000, 4 * 1024} },
810 { .m = { .sec = 1, .tb = 0 }, 0x2, {0x7fe000, 8 * 1024} },
811 { .m = { .sec = 1, .tb = 0 }, 0x3, {0x7fc000, 16 * 1024} },
812 { .m = { .sec = 1, .tb = 0 }, 0x4, {0x7f8000, 32 * 1024} },
813 { .m = { .sec = 1, .tb = 0 }, 0x5, {0x7f8000, 32 * 1024} },
814 { .m = { .sec = 1, .tb = 0 }, 0x6, {0x7f8000, 32 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700815
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100816 { .m = { .sec = 1, .tb = 1 }, 0x1, {0x000000, 4 * 1024} },
817 { .m = { .sec = 1, .tb = 1 }, 0x2, {0x000000, 8 * 1024} },
818 { .m = { .sec = 1, .tb = 1 }, 0x3, {0x000000, 16 * 1024} },
819 { .m = { .sec = 1, .tb = 1 }, 0x4, {0x000000, 32 * 1024} },
820 { .m = { .sec = 1, .tb = 1 }, 0x5, {0x000000, 32 * 1024} },
821 { .m = { .sec = 1, .tb = 1 }, 0x6, {0x000000, 32 * 1024} },
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700822};
823
Edward O'Callaghane146f9a2019-12-05 14:27:24 +1100824static struct wp_range_descriptor a25l040_ranges[] = {
Edward O'Callaghan91b38272019-12-04 17:12:43 +1100825 { .m = { .sec = X, .tb = X }, 0x0, {0, 0} }, /* none */
826 { .m = { .sec = X, .tb = X }, 0x1, {0x70000, 64 * 1024} },
827 { .m = { .sec = X, .tb = X }, 0x2, {0x60000, 128 * 1024} },
828 { .m = { .sec = X, .tb = X }, 0x3, {0x40000, 256 * 1024} },
829 { .m = { .sec = X, .tb = X }, 0x4, {0x00000, 512 * 1024} },
830 { .m = { .sec = X, .tb = X }, 0x5, {0x00000, 512 * 1024} },
831 { .m = { .sec = X, .tb = X }, 0x6, {0x00000, 512 * 1024} },
832 { .m = { .sec = X, .tb = X }, 0x7, {0x00000, 512 * 1024} },
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800833};
834
Nikolai Artemiev9d3980e2021-03-30 22:26:37 +1100835struct wp *get_wp_for_flashchip(const struct flashchip *chip) {
836 // FIXME: The .wp field should be deleted from from struct flashchip
837 // completly, but linux_mtd and cros_ec still assign their own values
838 // to it. When they are cleaned up we can delete this.
839 if(chip->wp) return chip->wp;
840
841 switch (chip->manufacture_id) {
842 case WINBOND_NEX_ID:
843 switch(chip->model_id) {
844 case WINBOND_NEX_W25X10:
845 case WINBOND_NEX_W25X20:
846 case WINBOND_NEX_W25X40:
847 case WINBOND_NEX_W25X80:
848 case WINBOND_NEX_W25Q128_V_M:
849 return &wp_w25;
850 case WINBOND_NEX_W25Q80_V:
851 case WINBOND_NEX_W25Q16_V:
852 case WINBOND_NEX_W25Q32_V:
853 case WINBOND_NEX_W25Q32_W:
854 case WINBOND_NEX_W25Q32JW:
855 case WINBOND_NEX_W25Q64_V:
856 case WINBOND_NEX_W25Q64_W:
857 // W25Q64JW does not have a range table entry, but the flashchip
858 // set .wp to wp_25q, so keep it here until the issue is resolved
859 case WINBOND_NEX_W25Q64JW:
860 case WINBOND_NEX_W25Q128_DTR:
861 case WINBOND_NEX_W25Q128_V:
862 case WINBOND_NEX_W25Q128_W:
863 return &wp_w25q;
864 case WINBOND_NEX_W25Q256_V:
865 case WINBOND_NEX_W25Q256JV_M:
866 return &wp_w25q_large;
867 }
868 break;
869 case EON_ID_NOPREFIX:
870 switch (chip->model_id) {
871 case EON_EN25F40:
872 case EON_EN25Q40:
873 case EON_EN25Q80:
874 case EON_EN25Q32:
875 case EON_EN25Q64:
876 case EON_EN25Q128:
877 case EON_EN25QH128:
878 case EON_EN25S64:
879 return &wp_w25;
880 }
881 break;
882 case MACRONIX_ID:
883 switch (chip->model_id) {
884 case MACRONIX_MX25L1005:
885 case MACRONIX_MX25L2005:
886 case MACRONIX_MX25L4005:
887 case MACRONIX_MX25L8005:
888 case MACRONIX_MX25L1605:
889 case MACRONIX_MX25L3205:
890 case MACRONIX_MX25U3235E:
891 case MACRONIX_MX25U6435E:
892 return &wp_w25;
893 case MACRONIX_MX25U12835E:
894 return &wp_w25q_large;
895 case MACRONIX_MX25L6405:
896 case MACRONIX_MX25L6495F:
897 case MACRONIX_MX25L25635F:
898 return &wp_generic;
899 }
900 break;
901 case ST_ID:
902 switch(chip->model_id) {
903 case ST_N25Q064__1E:
904 case ST_N25Q064__3E:
905 return &wp_w25;
906 }
907 break;
908 case GIGADEVICE_ID:
909 switch(chip->model_id) {
910 case GIGADEVICE_GD25LQ32:
911 // GD25Q40 does not have a .wp field in flashchips.c, but
912 // it is in the w25 range table function, so note it here
913 // until the issue is resolved:
914 // case GIGADEVICE_GD25Q40:
915 case GIGADEVICE_GD25Q64:
916 case GIGADEVICE_GD25LQ64:
917 // Ranges for GD25Q128 are defined in both the generic and
918 // w25 range table functions. The .wp field in the flashchip
919 // pointed to wp_w25, so use that here as well.
920 case GIGADEVICE_GD25Q128:
921 return &wp_w25;
922 case GIGADEVICE_GD25Q256D:
923 return &wp_w25q_large;
924 // Ranges for GD25Q128CD are defined in both the generic and
925 // w25 range table functions. The .wp field in the flashchip
926 // pointed to wp_generic, so use that here as well.
927 case GIGADEVICE_GD25LQ128CD:
928 case GIGADEVICE_GD25Q32:
929 return &wp_generic;
930 }
931 break;
932 case AMIC_ID_NOPREFIX:
933 switch(chip->model_id) {
934 case AMIC_A25L040:
935 return &wp_w25;
936 }
937 break;
938 case ATMEL_ID:
939 switch(chip->model_id) {
940 case ATMEL_AT25SF128A:
941 case ATMEL_AT25SL128A:
942 return &wp_w25q;
943 }
944 break;
945 case PROGMANUF_ID:
946 switch(chip->model_id) {
947 case PROGDEV_ID:
948 return &wp_w25;
949 }
950 break;
951 case SPANSION_ID:
952 switch (chip->model_id) {
953 case SPANSION_S25FS128S_L:
954 case SPANSION_S25FS128S_S:
955 case SPANSION_S25FL256S_UL:
956 case SPANSION_S25FL256S_US:
957 // SPANSION_S25FL128S_UL does not have a range table entry,
958 // but its flashchip set .wp to wp_generic, so keep it here
959 // until the issue resolved
960 case SPANSION_S25FL128S_UL:
961 // SPANSION_S25FL128S_US does not have a range table entry,
962 // but its flashchip set .wp to wp_generic, so keep it here
963 // until the issue resolved
964 case SPANSION_S25FL128S_US:
965 return &wp_generic;
966 }
967 break;
968 }
969
970
971 return NULL;
972}
973
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700974/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700975static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700976{
977 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
978 unsigned char readarr[2];
979 int ret;
980
Edward O'Callaghan70f3e8f2020-12-21 12:50:52 +1100981 if (flash->chip->read_status) {
982 msg_cdbg("RDSR2 failed! cmd=0x35 unimpl for opaque chips\n");
983 return 0;
984 }
985
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700986 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700987 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700988 if (ret) {
989 /*
990 * FIXME: make this a benign failure for now in case we are
991 * unable to execute the opcode
992 */
993 msg_cdbg("RDSR2 failed!\n");
994 readarr[0] = 0x00;
995 }
996
997 return readarr[0];
998}
999
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001000/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Edward O'Callaghandf43e902020-11-13 23:08:26 +11001001static uint8_t mx25l_read_config_register(const struct flashctx *flash)
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001002{
1003 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1004 unsigned char readarr[2]; /* leave room for dummy byte */
1005 int ret;
1006
Edward O'Callaghan70f3e8f2020-12-21 12:50:52 +11001007 if (flash->chip->read_status) {
1008 msg_cdbg("RDCR failed! cmd=0x15 unimpl for opaque chips\n");
1009 return 0;
1010 }
1011
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001012 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
1013 if (ret) {
1014 msg_cdbg("RDCR failed!\n");
1015 readarr[0] = 0x00;
1016 }
1017
1018 return readarr[0];
1019}
1020
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001021/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001022static int w25_range_table(const struct flashctx *flash,
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001023 struct wp_range_descriptor **descrs,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001024 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -07001025{
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001026 uint8_t cr;
1027
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001028 *descrs = 0;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001029 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001030
Patrick Georgif3fa2992017-02-02 16:24:44 +01001031 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -07001032 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001033 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -08001034 case WINBOND_NEX_W25X10:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001035 *descrs = w25x10_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001036 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +08001037 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001038 case WINBOND_NEX_W25X20:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001039 *descrs = w25x20_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001040 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +08001041 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001042 case WINBOND_NEX_W25X40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001043 *descrs = w25x40_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001044 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -07001045 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001046 case WINBOND_NEX_W25X80:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001047 *descrs = w25x80_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001048 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +08001049 break;
Patrick Georgicc04a452017-02-06 12:14:43 +01001050 case WINBOND_NEX_W25Q80_V:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001051 *descrs = w25q80_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001052 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -07001053 break;
Patrick Georgicc04a452017-02-06 12:14:43 +01001054 case WINBOND_NEX_W25Q16_V:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001055 *descrs = w25q16_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001056 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -07001057 break;
Patrick Georgicc04a452017-02-06 12:14:43 +01001058 case WINBOND_NEX_W25Q32_V:
1059 case WINBOND_NEX_W25Q32_W:
Edward O'Callaghand80cf712019-05-24 22:06:36 +10001060 case WINBOND_NEX_W25Q32JW:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001061 *descrs = w25q32_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001062 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -07001063 break;
Patrick Georgicc04a452017-02-06 12:14:43 +01001064 case WINBOND_NEX_W25Q64_V:
1065 case WINBOND_NEX_W25Q64_W:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001066 *descrs = w25q64_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001067 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -07001068 break;
Edward O'Callaghan517cb822019-11-21 14:08:32 +11001069 case WINBOND_NEX_W25Q128_DTR:
Alan Green77a95de2019-07-01 16:40:39 +10001070 case WINBOND_NEX_W25Q128_V_M:
Patrick Georgicc04a452017-02-06 12:14:43 +01001071 case WINBOND_NEX_W25Q128_V:
1072 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001073 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -07001074 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001075 *descrs = w25rq128_cmp1_ranges;
Duncan Laurieed32d7b2015-05-27 11:28:18 -07001076 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1077 } else {
1078 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001079 *descrs = w25rq128_cmp0_ranges;
Duncan Laurieed32d7b2015-05-27 11:28:18 -07001080 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1081 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301082 break;
Justin TerAvest40083232020-08-17 16:34:46 -06001083 case WINBOND_NEX_W25Q256_V:
Alan Green77a95de2019-07-01 16:40:39 +10001084 case WINBOND_NEX_W25Q256JV_M:
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001085 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1086 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001087 *descrs = w25rq256_cmp1_ranges;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001088 *num_entries = ARRAY_SIZE(w25rq256_cmp1_ranges);
1089 } else {
1090 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001091 *descrs = w25rq256_cmp0_ranges;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001092 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
1093 }
1094 break;
David Hendricksd494b0a2010-08-16 16:28:50 -07001095 default:
1096 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
1097 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001098 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -07001099 return -1;
1100 }
David Hendricks2c4a76c2010-06-28 14:00:43 -07001101 break;
David Hendricks57566ed2010-08-16 18:24:45 -07001102 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001103 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -08001104 case EON_EN25F40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001105 *descrs = en25f40_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001106 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -07001107 break;
David Hendrickse185bf22011-05-24 15:34:18 -07001108 case EON_EN25Q40:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001109 *descrs = en25q40_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -07001110 *num_entries = ARRAY_SIZE(en25q40_ranges);
1111 break;
1112 case EON_EN25Q80:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001113 *descrs = en25q80_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -07001114 *num_entries = ARRAY_SIZE(en25q80_ranges);
1115 break;
1116 case EON_EN25Q32:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001117 *descrs = en25q32_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -07001118 *num_entries = ARRAY_SIZE(en25q32_ranges);
1119 break;
1120 case EON_EN25Q64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001121 *descrs = en25q64_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -07001122 *num_entries = ARRAY_SIZE(en25q64_ranges);
1123 break;
1124 case EON_EN25Q128:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001125 *descrs = en25q128_ranges;
David Hendrickse185bf22011-05-24 15:34:18 -07001126 *num_entries = ARRAY_SIZE(en25q128_ranges);
1127 break;
Tim Chen136fd0a2020-06-30 19:12:50 +08001128 case EON_EN25QH128:
1129 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1130 /* CMP == 1 */
1131 *descrs = w25rq128_cmp1_ranges;
1132 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1133 } else {
1134 /* CMP == 0 */
1135 *descrs = w25rq128_cmp0_ranges;
1136 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1137 }
1138 break;
Marc Jonesb2f90022014-04-29 17:37:23 -06001139 case EON_EN25S64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001140 *descrs = en25s64_ranges;
Marc Jonesb2f90022014-04-29 17:37:23 -06001141 *num_entries = ARRAY_SIZE(en25s64_ranges);
1142 break;
David Hendricks57566ed2010-08-16 18:24:45 -07001143 default:
1144 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
1145 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001146 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -07001147 return -1;
1148 }
1149 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001150 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001151 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -08001152 case MACRONIX_MX25L1005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001153 *descrs = mx25l1005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001154 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
1155 break;
1156 case MACRONIX_MX25L2005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001157 *descrs = mx25l2005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001158 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
1159 break;
1160 case MACRONIX_MX25L4005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001161 *descrs = mx25l4005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001162 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
1163 break;
1164 case MACRONIX_MX25L8005:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001165 *descrs = mx25l8005_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001166 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
1167 break;
1168 case MACRONIX_MX25L1605:
1169 /* FIXME: MX25L1605 and MX25L1605D have different write
1170 * protection capabilities, but share IDs */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001171 *descrs = mx25l1605d_ranges;
David Hendricksf8f00c72011-02-01 12:39:46 -08001172 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
1173 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001174 case MACRONIX_MX25L3205:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001175 *descrs = mx25l3205d_ranges;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001176 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -07001177 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001178 case MACRONIX_MX25U3235E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001179 *descrs = mx25u3235e_ranges;
Vincent Palatin87e092a2013-02-28 15:46:14 -08001180 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
1181 break;
Jongpil66a96492014-08-14 17:59:06 +09001182 case MACRONIX_MX25U6435E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001183 *descrs = mx25u6435e_ranges;
Jongpil66a96492014-08-14 17:59:06 +09001184 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
1185 break;
Alan Greendc0792e2019-07-01 15:01:34 +10001186 case MACRONIX_MX25U12835E:
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001187 cr = mx25l_read_config_register(flash);
1188 if (cr & MX25U12835E_TB) { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001189 *descrs = mx25u12835e_tb1_ranges;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001190 *num_entries = ARRAY_SIZE(mx25u12835e_tb1_ranges);
1191 } else { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001192 *descrs = mx25u12835e_tb0_ranges;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001193 *num_entries = ARRAY_SIZE(mx25u12835e_tb0_ranges);
1194 }
Alex Lu831c6092017-11-02 23:19:34 -07001195 break;
David Hendricksac72e362010-08-16 18:20:03 -07001196 default:
1197 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1198 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001199 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -07001200 return -1;
1201 }
1202 break;
David Hendricksbfa624b2012-07-24 12:47:59 -07001203 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001204 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -07001205 case ST_N25Q064__1E:
1206 case ST_N25Q064__3E:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001207 *descrs = n25q064_ranges;
David Hendricksbfa624b2012-07-24 12:47:59 -07001208 *num_entries = ARRAY_SIZE(n25q064_ranges);
1209 break;
1210 default:
1211 msg_cerr("%s() %d: Micron flash chip mismatch"
1212 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001213 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -07001214 return -1;
1215 }
1216 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001217 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001218 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -07001219 case GIGADEVICE_GD25LQ32:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001220 *descrs = w25q32_ranges;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001221 *num_entries = ARRAY_SIZE(w25q32_ranges);
1222 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001223 case GIGADEVICE_GD25Q40:
1224 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1225 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001226 *descrs = gd25q40_cmp1_ranges;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001227 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
1228 } else {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001229 *descrs = gd25q40_cmp0_ranges;
Martin Rothf3c3d5f2017-04-28 14:56:41 -06001230 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
1231 }
1232 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001233 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -06001234 case GIGADEVICE_GD25LQ64:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001235 *descrs = gd25q64_ranges;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -07001236 *num_entries = ARRAY_SIZE(gd25q64_ranges);
1237 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001238 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06001239 case GIGADEVICE_GD25LQ128CD:
Martin Roth1fd87ed2017-02-27 20:50:50 -07001240 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1241 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001242 *descrs = w25rq128_cmp1_ranges;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001243 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1244 } else {
1245 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001246 *descrs = w25rq128_cmp0_ranges;
Martin Roth1fd87ed2017-02-27 20:50:50 -07001247 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1248 }
1249 break;
Duncan Laurie0c383552019-03-16 12:35:16 -07001250 case GIGADEVICE_GD25Q256D:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001251 *descrs = w25rq256_cmp0_ranges;
Duncan Laurie0c383552019-03-16 12:35:16 -07001252 *num_entries = ARRAY_SIZE(w25rq256_cmp0_ranges);
1253 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -07001254 default:
1255 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1256 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001257 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -07001258 return -1;
1259 }
1260 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001261 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001262 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001263 case AMIC_A25L040:
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001264 *descrs = a25l040_ranges;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001265 *num_entries = ARRAY_SIZE(a25l040_ranges);
1266 break;
1267 default:
1268 msg_cerr("%s() %d: AMIC flash chip mismatch"
1269 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001270 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +08001271 return -1;
1272 }
1273 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001274 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001275 switch(flash->chip->model_id) {
Edward O'Callaghan1fa87e02019-05-03 02:27:24 -04001276 case ATMEL_AT25SF128A:
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001277 case ATMEL_AT25SL128A:
1278 if (w25q_read_status_register_2(flash) & (1 << 6)) {
1279 /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001280 *descrs = w25rq128_cmp1_ranges;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001281 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
1282 } else {
1283 /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001284 *descrs = w25rq128_cmp0_ranges;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001285 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1286 }
1287 break;
1288 default:
1289 msg_cerr("%s() %d: Atmel flash chip mismatch"
1290 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001291 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001292 return -1;
1293 }
1294 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001295 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001296 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001297 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001298 return -1;
1299 }
1300
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001301 return 0;
1302}
1303
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001304int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001305 unsigned int start, unsigned int len,
1306 struct w25q_status *status)
1307{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001308 struct wp_range_descriptor *descrs;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001309 int i, range_found = 0;
1310 int num_entries;
1311
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001312 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001313 return -1;
1314
David Hendricksf7924d12010-06-10 21:26:44 -07001315 for (i = 0; i < num_entries; i++) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001316 struct wp_range *r = &descrs[i].range;
David Hendricksf7924d12010-06-10 21:26:44 -07001317
1318 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1319 start, len, r->start, r->len);
1320 if ((start == r->start) && (len == r->len)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001321 status->bp0 = descrs[i].bp & 1;
1322 status->bp1 = descrs[i].bp >> 1;
1323 status->bp2 = descrs[i].bp >> 2;
1324 status->tb = descrs[i].m.tb;
1325 status->sec = descrs[i].m.sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001326
1327 range_found = 1;
1328 break;
1329 }
1330 }
1331
1332 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11001333 msg_cerr("%s: matching range not found\n", __func__);
David Hendricksf7924d12010-06-10 21:26:44 -07001334 return -1;
1335 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001336
David Hendricksd494b0a2010-08-16 16:28:50 -07001337 return 0;
1338}
1339
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001340int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001341 const struct w25q_status *status,
1342 unsigned int *start, unsigned int *len)
1343{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001344 struct wp_range_descriptor *descrs;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001345 int i, status_found = 0;
1346 int num_entries;
1347
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001348 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001349 return -1;
1350
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001351 for (i = 0; i < num_entries; i++) {
1352 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001353 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001354
1355 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1356 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001357 bp, descrs[i].bp,
1358 status->tb, descrs[i].m.tb,
1359 status->sec, descrs[i].m.sec);
1360 table_bp = descrs[i].bp;
1361 table_tb = descrs[i].m.tb;
1362 table_sec = descrs[i].m.sec;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001363 if ((bp == table_bp || table_bp == X) &&
1364 (status->tb == table_tb || table_tb == X) &&
1365 (status->sec == table_sec || table_sec == X)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001366 *start = descrs[i].range.start;
1367 *len = descrs[i].range.len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001368
1369 status_found = 1;
1370 break;
1371 }
1372 }
1373
1374 if (!status_found) {
1375 msg_cerr("matching status not found\n");
1376 return -1;
1377 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001378
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001379 return 0;
1380}
1381
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001382/* Given a [start, len], this function calls w25_range_to_status() to convert
1383 * it to flash-chip-specific range bits, then sets into status register.
1384 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001385static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001386 unsigned int start, unsigned int len)
1387{
1388 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001389 int tmp = 0;
1390 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001391
1392 memset(&status, 0, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001393 tmp = spi_read_status_register(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001394 memcpy(&status, &tmp, 1);
1395 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1396
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001397 if (w25_range_to_status(flash, start, len, &status))
1398 return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001399
1400 msg_cdbg("status.busy: %x\n", status.busy);
1401 msg_cdbg("status.wel: %x\n", status.wel);
1402 msg_cdbg("status.bp0: %x\n", status.bp0);
1403 msg_cdbg("status.bp1: %x\n", status.bp1);
1404 msg_cdbg("status.bp2: %x\n", status.bp2);
1405 msg_cdbg("status.tb: %x\n", status.tb);
1406 msg_cdbg("status.sec: %x\n", status.sec);
1407 msg_cdbg("status.srp0: %x\n", status.srp0);
1408
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001409 memcpy(&expected, &status, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001410 spi_write_status_register(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001411
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001412 tmp = spi_read_status_register(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001413 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001414 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001415 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001416 expected, tmp);
1417 return 1;
1418 }
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001419
1420 return 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001421}
1422
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001423/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001424static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001425{
1426 struct w25q_status status;
1427 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001428 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001429 int ret = 0;
1430
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001431 memset(&status, 0, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001432 tmp = spi_read_status_register(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001433 memcpy(&status, &tmp, 1);
1434 msg_cinfo("WP: status: 0x%02x\n", tmp);
1435 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1436 msg_cinfo("WP: write protect is %s.\n",
1437 status.srp0 ? "enabled" : "disabled");
1438
1439 msg_cinfo("WP: write protect range: ");
1440 if (w25_status_to_range(flash, &status, &start, &len)) {
1441 msg_cinfo("(cannot resolve the range)\n");
1442 ret = -1;
1443 } else {
1444 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1445 }
1446
1447 return ret;
1448}
1449
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001450static int w25q_large_range_to_status(const struct flashctx *flash,
1451 unsigned int start, unsigned int len,
1452 struct w25q_status_large *status)
1453{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001454 struct wp_range_descriptor *descrs;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001455 int i, range_found = 0;
1456 int num_entries;
1457
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001458 if (w25_range_table(flash, &descrs, &num_entries))
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001459 return -1;
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001460
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001461 for (i = 0; i < num_entries; i++) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001462 struct wp_range *r = &descrs[i].range;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001463
1464 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1465 start, len, r->start, r->len);
1466 if ((start == r->start) && (len == r->len)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001467 status->bp0 = descrs[i].bp & 1;
1468 status->bp1 = descrs[i].bp >> 1;
1469 status->bp2 = descrs[i].bp >> 2;
1470 status->bp3 = descrs[i].bp >> 3;
Karthikeyan Ramasubramanianfb166b72019-06-24 12:38:55 -06001471 /*
1472 * For MX25U12835E chip, Top/Bottom (T/B) bit is not
1473 * part of status register and in that bit position is
1474 * Quad Enable (QE)
1475 */
1476 if (flash->chip->manufacture_id != MACRONIX_ID ||
1477 flash->chip->model_id != MACRONIX_MX25U12835E)
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001478 status->tb = descrs[i].m.tb;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001479
1480 range_found = 1;
1481 break;
1482 }
1483 }
1484
1485 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11001486 msg_cerr("%s: matching range not found\n", __func__);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001487 return -1;
1488 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001489
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001490 return 0;
1491}
1492
1493static int w25_large_status_to_range(const struct flashctx *flash,
1494 const struct w25q_status_large *status,
1495 unsigned int *start, unsigned int *len)
1496{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001497 struct wp_range_descriptor *descrs;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001498 int i, status_found = 0;
1499 int num_entries;
1500
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001501 if (w25_range_table(flash, &descrs, &num_entries))
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001502 return -1;
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001503
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001504 for (i = 0; i < num_entries; i++) {
1505 int bp;
1506 int table_bp, table_tb;
1507
1508 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2) |
1509 (status->bp3 << 3);
1510 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001511 bp, descrs[i].bp,
1512 status->tb, descrs[i].m.tb);
1513 table_bp = descrs[i].bp;
1514 table_tb = descrs[i].m.tb;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001515 if ((bp == table_bp || table_bp == X) &&
1516 (status->tb == table_tb || table_tb == X)) {
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001517 *start = descrs[i].range.start;
1518 *len = descrs[i].range.len;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001519
1520 status_found = 1;
1521 break;
1522 }
1523 }
1524
1525 if (!status_found) {
1526 msg_cerr("matching status not found\n");
1527 return -1;
1528 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001529
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001530 return 0;
1531}
1532
1533/* Given a [start, len], this function calls w25_range_to_status() to convert
1534 * it to flash-chip-specific range bits, then sets into status register.
1535 * Returns 0 if successful, -1 on error, and 1 if reading back was different.
1536 */
1537static int w25q_large_set_range(const struct flashctx *flash,
1538 unsigned int start, unsigned int len)
1539{
1540 struct w25q_status_large status;
1541 int tmp;
1542 int expected = 0;
1543
1544 memset(&status, 0, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001545 tmp = spi_read_status_register(flash);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001546 memcpy(&status, &tmp, 1);
1547 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1548
1549 if (w25q_large_range_to_status(flash, start, len, &status))
1550 return -1;
1551
1552 msg_cdbg("status.busy: %x\n", status.busy);
1553 msg_cdbg("status.wel: %x\n", status.wel);
1554 msg_cdbg("status.bp0: %x\n", status.bp0);
1555 msg_cdbg("status.bp1: %x\n", status.bp1);
1556 msg_cdbg("status.bp2: %x\n", status.bp2);
1557 msg_cdbg("status.bp3: %x\n", status.bp3);
1558 msg_cdbg("status.tb: %x\n", status.tb);
1559 msg_cdbg("status.srp0: %x\n", status.srp0);
1560
1561 memcpy(&expected, &status, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001562 spi_write_status_register(flash, expected);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001563
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001564 tmp = spi_read_status_register(flash);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001565 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001566 if ((tmp & MASK_WP_AREA_LARGE) != (expected & MASK_WP_AREA_LARGE)) {
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001567 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1568 expected, tmp);
1569 return 1;
1570 }
Edward O'Callaghan2672fb92019-12-04 14:47:58 +11001571
1572 return 0;
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001573}
1574
1575static int w25q_large_wp_status(const struct flashctx *flash)
1576{
1577 struct w25q_status_large sr1;
1578 struct w25q_status_2 sr2;
1579 uint8_t tmp[2];
1580 unsigned int start, len;
1581 int ret = 0;
1582
1583 memset(&sr1, 0, sizeof(sr1));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001584 tmp[0] = spi_read_status_register(flash);
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001585 memcpy(&sr1, &tmp[0], 1);
1586
1587 memset(&sr2, 0, sizeof(sr2));
1588 tmp[1] = w25q_read_status_register_2(flash);
1589 memcpy(&sr2, &tmp[1], 1);
1590
1591 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
1592 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1593 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1594 msg_cinfo("WP: write protect is %s.\n",
1595 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1596
1597 msg_cinfo("WP: write protect range: ");
1598 if (w25_large_status_to_range(flash, &sr1, &start, &len)) {
1599 msg_cinfo("(cannot resolve the range)\n");
1600 ret = -1;
1601 } else {
1602 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1603 }
1604
1605 return ret;
1606}
1607
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001608/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001609static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001610{
1611 struct w25q_status status;
1612 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001613 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001614
1615 memset(&status, 0, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001616 tmp = spi_read_status_register(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001617 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001618 memcpy(&status, &tmp, 1);
1619 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1620
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001621 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001622 memcpy(&expected, &status, sizeof(status));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001623 spi_write_status_register(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001624
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001625 tmp = spi_read_status_register(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001626 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1627 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1628 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001629
1630 return 0;
1631}
1632
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001633static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001634 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001635{
1636 int ret;
1637
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11001638 if (wp_mode != WP_MODE_HARDWARE) {
David Hendricks1c09f802012-10-03 11:03:48 -07001639 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1640 return 1;
1641 }
1642
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11001643 ret = w25_set_srp0(flash, 1);
David Hendricksc801adb2010-12-09 16:58:56 -08001644 if (ret)
1645 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001646 return ret;
1647}
1648
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001649static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001650{
1651 int ret;
1652
1653 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001654 if (ret)
1655 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001656
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001657 return ret;
1658}
1659
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001660static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001661{
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001662 struct wp_range_descriptor *descrs;
David Hendricks0f7f5382011-02-11 18:12:31 -08001663 int i, num_entries;
1664
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001665 if (w25_range_table(flash, &descrs, &num_entries))
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001666 return -1;
1667
David Hendricks0f7f5382011-02-11 18:12:31 -08001668 for (i = 0; i < num_entries; i++) {
1669 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11001670 descrs[i].range.start,
1671 descrs[i].range.len);
David Hendricks0f7f5382011-02-11 18:12:31 -08001672 }
1673
1674 return 0;
1675}
1676
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001677static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001678{
1679 struct w25q_status sr1;
1680 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001681 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001682 unsigned int start, len;
1683 int ret = 0;
1684
1685 memset(&sr1, 0, sizeof(sr1));
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001686 tmp[0] = spi_read_status_register(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001687 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001688
David Hendricksf1bd8802012-10-30 11:37:57 -07001689 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001690 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001691 memcpy(&sr2, &tmp[1], 1);
1692
1693 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001694 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1695 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1696 msg_cinfo("WP: write protect is %s.\n",
1697 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1698
1699 msg_cinfo("WP: write protect range: ");
1700 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1701 msg_cinfo("(cannot resolve the range)\n");
1702 ret = -1;
1703 } else {
1704 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1705 }
1706
1707 return ret;
1708}
1709
1710/*
1711 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1712 * de-asserted after the first byte, then it acts like a JEDEC-standard
1713 * WRSR command. if /CS is asserted, then the next data byte is written
1714 * into status register 2.
1715 */
1716#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001717static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001718{
1719 int result;
1720 struct spi_command cmds[] = {
1721 {
1722 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1723 .writecnt = JEDEC_WREN_OUTSIZE,
1724 .writearr = (const unsigned char[]){ JEDEC_WREN },
1725 .readcnt = 0,
1726 .readarr = NULL,
1727 }, {
1728 .writecnt = W25Q_WRSR_OUTSIZE,
1729 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1730 .readcnt = 0,
1731 .readarr = NULL,
1732 }, {
1733 .writecnt = 0,
1734 .writearr = NULL,
1735 .readcnt = 0,
1736 .readarr = NULL,
1737 }};
1738
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001739 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001740 if (result) {
1741 msg_cerr("%s failed during command execution\n",
1742 __func__);
1743 }
1744
1745 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001746 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001747
1748 return result;
1749}
1750
1751/*
1752 * Set/clear the SRP1 bit in status register 2.
1753 * FIXME: make this more generic if other chips use the same SR2 layout
1754 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001755static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001756{
1757 struct w25q_status sr1;
1758 struct w25q_status_2 sr2;
1759 uint8_t tmp, expected;
1760
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001761 tmp = spi_read_status_register(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001762 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001763 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001764 memcpy(&sr2, &tmp, 1);
1765
1766 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1767
1768 sr2.srp1 = enable ? 1 : 0;
1769
1770 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001771 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001772
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001773 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001774 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1775 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1776 return 1;
1777
1778 return 0;
1779}
1780
1781enum wp_mode get_wp_mode(const char *mode_str)
1782{
1783 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1784
1785 if (!strcasecmp(mode_str, "hardware"))
1786 wp_mode = WP_MODE_HARDWARE;
1787 else if (!strcasecmp(mode_str, "power_cycle"))
1788 wp_mode = WP_MODE_POWER_CYCLE;
1789 else if (!strcasecmp(mode_str, "permanent"))
1790 wp_mode = WP_MODE_PERMANENT;
1791
1792 return wp_mode;
1793}
1794
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001795static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001796 enum wp_mode wp_mode)
1797{
1798 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001799 struct w25q_status_2 sr2;
1800 uint8_t tmp;
1801
1802 switch (wp_mode) {
1803 case WP_MODE_HARDWARE:
1804 ret = w25_set_srp0(flash, 0);
1805 break;
1806 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001807 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001808 memcpy(&sr2, &tmp, 1);
1809 if (sr2.srp1) {
1810 msg_cerr("%s(): must disconnect power to disable "
1811 "write-protection\n", __func__);
1812 } else {
1813 ret = 0;
1814 }
1815 break;
1816 case WP_MODE_PERMANENT:
1817 msg_cerr("%s(): cannot disable permanent write-protection\n",
1818 __func__);
1819 break;
1820 default:
1821 msg_cerr("%s(): invalid mode specified\n", __func__);
1822 break;
1823 }
1824
1825 if (ret)
1826 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11001827
David Hendricks1c09f802012-10-03 11:03:48 -07001828 return ret;
1829}
1830
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001831static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001832{
1833 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1834}
1835
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001836static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001837 enum wp_mode wp_mode)
1838{
1839 int ret = 1;
1840 struct w25q_status sr1;
1841 struct w25q_status_2 sr2;
1842 uint8_t tmp;
1843
1844 switch (wp_mode) {
1845 case WP_MODE_HARDWARE:
1846 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1847 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1848 __func__);
1849 break;
1850 }
1851
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001852 tmp = spi_read_status_register(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001853 memcpy(&sr1, &tmp, 1);
1854 if (sr1.srp0)
1855 ret = 0;
1856 else
1857 ret = w25_set_srp0(flash, 1);
1858
1859 break;
1860 case WP_MODE_POWER_CYCLE:
1861 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1862 msg_cerr("%s(): cannot disable hardware WP mode\n",
1863 __func__);
1864 break;
1865 }
1866
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001867 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001868 memcpy(&sr2, &tmp, 1);
1869 if (sr2.srp1)
1870 ret = 0;
1871 else
1872 ret = w25q_set_srp1(flash, 1);
1873
1874 break;
1875 case WP_MODE_PERMANENT:
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10001876 tmp = spi_read_status_register(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001877 memcpy(&sr1, &tmp, 1);
1878 if (sr1.srp0 == 0) {
1879 ret = w25_set_srp0(flash, 1);
1880 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001881 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001882 "permanent WP\n", __func__);
1883 break;
1884 }
1885 }
1886
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001887 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001888 memcpy(&sr2, &tmp, 1);
1889 if (sr2.srp1 == 0) {
1890 ret = w25q_set_srp1(flash, 1);
1891 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001892 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001893 "permanent WP\n", __func__);
1894 break;
1895 }
1896 }
1897
1898 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001899 default:
1900 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1901 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001902 }
1903
1904 if (ret)
1905 msg_cerr("%s(): error=%d.\n", __func__, ret);
1906 return ret;
1907}
1908
1909/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001910struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001911 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001912 .set_range = w25_set_range,
1913 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001914 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001915 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001916
1917};
1918
1919/* W25Q series has features such as a second status register and SFDP */
1920struct wp wp_w25q = {
1921 .list_ranges = w25_list_ranges,
1922 .set_range = w25_set_range,
1923 .enable = w25q_enable_writeprotect,
1924 /*
1925 * By default, disable hardware write-protection. We may change
1926 * this later if we want to add fine-grained write-protect disable
1927 * as a command-line option.
1928 */
1929 .disable = w25q_disable_writeprotect_default,
1930 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001931};
David Hendrickse0512a72014-07-15 20:30:47 -07001932
Duncan Laurie1801f7c2019-01-09 18:02:51 -08001933/* W25Q large series has 4 block-protect bits */
1934struct wp wp_w25q_large = {
1935 .list_ranges = w25_list_ranges,
1936 .set_range = w25q_large_set_range,
1937 .enable = w25q_enable_writeprotect,
1938 /*
1939 * By default, disable hardware write-protection. We may change
1940 * this later if we want to add fine-grained write-protect disable
1941 * as a command-line option.
1942 */
1943 .disable = w25q_disable_writeprotect_default,
1944 .wp_status = w25q_large_wp_status,
1945};
1946
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001947static struct wp_range_descriptor gd25q32_cmp0_ranges[] = {
David Hendricksaf3944a2014-07-28 18:37:40 -07001948 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001949 { { }, 0x00, {0, 0} },
1950 { { }, 0x08, {0, 0} },
1951 { { }, 0x10, {0, 0} },
1952 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001953
David Hendricks148a4bf2015-03-13 21:02:42 -07001954 { { }, 0x01, {0x3f0000, 64 * 1024} },
1955 { { }, 0x02, {0x3e0000, 128 * 1024} },
1956 { { }, 0x03, {0x3c0000, 256 * 1024} },
1957 { { }, 0x04, {0x380000, 512 * 1024} },
1958 { { }, 0x05, {0x300000, 1024 * 1024} },
1959 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001960
David Hendricks148a4bf2015-03-13 21:02:42 -07001961 { { }, 0x09, {0x000000, 64 * 1024} },
1962 { { }, 0x0a, {0x000000, 128 * 1024} },
1963 { { }, 0x0b, {0x000000, 256 * 1024} },
1964 { { }, 0x0c, {0x000000, 512 * 1024} },
1965 { { }, 0x0d, {0x000000, 1024 * 1024} },
1966 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001967
1968 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001969 { { }, 0x07, {0x000000, 4096 * 1024} },
1970 { { }, 0x0f, {0x000000, 4096 * 1024} },
1971 { { }, 0x17, {0x000000, 4096 * 1024} },
1972 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001973
David Hendricks148a4bf2015-03-13 21:02:42 -07001974 { { }, 0x11, {0x3ff000, 4 * 1024} },
1975 { { }, 0x12, {0x3fe000, 8 * 1024} },
1976 { { }, 0x13, {0x3fc000, 16 * 1024} },
1977 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1978 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1979 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001980
David Hendricks148a4bf2015-03-13 21:02:42 -07001981 { { }, 0x19, {0x000000, 4 * 1024} },
1982 { { }, 0x1a, {0x000000, 8 * 1024} },
1983 { { }, 0x1b, {0x000000, 16 * 1024} },
1984 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1985 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1986 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001987};
1988
Edward O'Callaghan3b996502020-04-12 20:46:51 +10001989static struct wp_range_descriptor gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001990 /* All, bp4 and bp3 => don't care */
1991 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1992 { { }, 0x08, {0x000000, 4096 * 1024} },
1993 { { }, 0x10, {0x000000, 4096 * 1024} },
1994 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001995
David Hendricks148a4bf2015-03-13 21:02:42 -07001996 { { }, 0x01, {0x000000, 4032 * 1024} },
1997 { { }, 0x02, {0x000000, 3968 * 1024} },
1998 { { }, 0x03, {0x000000, 3840 * 1024} },
1999 { { }, 0x04, {0x000000, 3584 * 1024} },
2000 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
2001 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07002002
David Hendricks148a4bf2015-03-13 21:02:42 -07002003 { { }, 0x09, {0x010000, 4032 * 1024} },
2004 { { }, 0x0a, {0x020000, 3968 * 1024} },
2005 { { }, 0x0b, {0x040000, 3840 * 1024} },
2006 { { }, 0x0c, {0x080000, 3584 * 1024} },
2007 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
2008 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07002009
Martin Roth563a1fe2017-04-18 14:26:27 -06002010 /* None, bp4 and bp3 => don't care */
2011 { { }, 0x07, {0, 0} }, /* None */
2012 { { }, 0x0f, {0, 0} },
2013 { { }, 0x17, {0, 0} },
2014 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07002015
David Hendricks148a4bf2015-03-13 21:02:42 -07002016 { { }, 0x11, {0x000000, 4092 * 1024} },
2017 { { }, 0x12, {0x000000, 4088 * 1024} },
2018 { { }, 0x13, {0x000000, 4080 * 1024} },
2019 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
2020 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
2021 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07002022
David Hendricks148a4bf2015-03-13 21:02:42 -07002023 { { }, 0x19, {0x001000, 4092 * 1024} },
2024 { { }, 0x1a, {0x002000, 4088 * 1024} },
2025 { { }, 0x1b, {0x040000, 4080 * 1024} },
2026 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
2027 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
2028 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07002029};
2030
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002031static struct wp_context gd25q32_wp = {
David Hendricksaf3944a2014-07-28 18:37:40 -07002032 /* TODO: map second status register */
2033 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
2034};
2035
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002036static struct wp_range_descriptor gd25q128_cmp0_ranges[] = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002037 /* none, bp4 and bp3 => don't care, others = 0 */
2038 { { .tb = 0 }, 0x00, {0, 0} },
2039 { { .tb = 0 }, 0x08, {0, 0} },
2040 { { .tb = 0 }, 0x10, {0, 0} },
2041 { { .tb = 0 }, 0x18, {0, 0} },
2042
2043 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
2044 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
2045 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
2046 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
2047 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
2048 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
2049
2050 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
2051 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
2052 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
2053 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
2054 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
2055 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
2056
2057 /* all, bp4 and bp3 => don't care, others = 1 */
2058 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
2059 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
2060 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
2061 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
2062
2063 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
2064 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
2065 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
2066 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
2067 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
2068
2069 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
2070 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
2071 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
2072 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
2073 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
2074 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
2075};
2076
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002077static struct wp_range_descriptor gd25q128_cmp1_ranges[] = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002078 /* none, bp4 and bp3 => don't care, others = 0 */
2079 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
2080 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
2081 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
2082 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
2083
2084 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
2085 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
2086 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
2087 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
2088 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
2089 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
2090
2091 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
2092 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
2093 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
2094 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
2095 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
2096 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
2097
2098 /* none, bp4 and bp3 => don't care, others = 1 */
2099 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
2100 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
2101 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
2102 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
2103 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
2104
2105 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
2106 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
2107 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
2108 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
2109 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
2110
2111 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
2112 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
2113 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
2114 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
2115 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
2116 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
2117};
2118
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002119static struct wp_context gd25q128_wp = {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002120 /* TODO: map second and third status registers */
2121 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
2122};
2123
David Hendricks83541d32014-07-15 20:58:21 -07002124/* FIXME: MX25L6406 has same ID as MX25L6405D */
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002125static struct wp_range_descriptor mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002126 { { }, 0, {0, 0} }, /* none */
2127 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
2128 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
2129 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
2130 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2131 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2132 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07002133
David Hendricks148a4bf2015-03-13 21:02:42 -07002134 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
2135 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2136 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2137 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
2138 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
2139 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
2140 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
2141 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
2142 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07002143};
2144
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002145static struct wp_context mx25l6406e_wp = {
David Hendricks83541d32014-07-15 20:58:21 -07002146 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002147 .descrs = &mx25l6406e_ranges[0],
David Hendricks83541d32014-07-15 20:58:21 -07002148};
David Hendrickse0512a72014-07-15 20:30:47 -07002149
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002150static struct wp_range_descriptor mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002151 { { }, 0, {0, 0} }, /* none */
2152 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
2153 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
2154 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08002155
David Hendricks148a4bf2015-03-13 21:02:42 -07002156 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
2157 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
2158 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
2159 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
2160 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2161 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2162 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2163 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2164 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2165 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2166 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2167 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002168};
2169
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002170static struct wp_range_descriptor mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002171 { { }, 0, {0, 0} }, /* none */
2172 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2173 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2174 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2175 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2176 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2177 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2178 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2179 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
2180 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
2181 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
2182 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
2183 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
2184 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
2185 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
2186 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08002187};
2188
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002189static struct wp_context mx25l6495f_wp = {
David Hendricksc3496092014-11-13 17:20:55 -08002190 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2191};
2192
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002193static struct wp_range_descriptor mx25l25635f_tb0_ranges[] = {
Vic Yang848bfd12018-03-23 10:24:07 -07002194 { { }, 0, {0, 0} }, /* none */
2195 { { }, 0x1, {0x1ff0000, 64 * 1 * 1024} }, /* block 511 */
2196 { { }, 0x2, {0x1fe0000, 64 * 2 * 1024} }, /* blocks 510-511 */
2197 { { }, 0x3, {0x1fc0000, 64 * 4 * 1024} }, /* blocks 508-511 */
2198 { { }, 0x4, {0x1f80000, 64 * 8 * 1024} }, /* blocks 504-511 */
2199 { { }, 0x5, {0x1f00000, 64 * 16 * 1024} }, /* blocks 496-511 */
2200 { { }, 0x6, {0x1e00000, 64 * 32 * 1024} }, /* blocks 480-511 */
2201 { { }, 0x7, {0x1c00000, 64 * 64 * 1024} }, /* blocks 448-511 */
2202 { { }, 0x8, {0x1800000, 64 * 128 * 1024} }, /* blocks 384-511 */
2203 { { }, 0x9, {0x1000000, 64 * 256 * 1024} }, /* blocks 256-511 */
2204 { { }, 0xa, {0x0000000, 64 * 512 * 1024} }, /* all */
2205 { { }, 0xb, {0x0000000, 64 * 512 * 1024} }, /* all */
2206 { { }, 0xc, {0x0000000, 64 * 512 * 1024} }, /* all */
2207 { { }, 0xd, {0x0000000, 64 * 512 * 1024} }, /* all */
2208 { { }, 0xe, {0x0000000, 64 * 512 * 1024} }, /* all */
2209 { { }, 0xf, {0x0000000, 64 * 512 * 1024} }, /* all */
2210};
2211
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002212static struct wp_range_descriptor mx25l25635f_tb1_ranges[] = {
Vic Yang848bfd12018-03-23 10:24:07 -07002213 { { }, 0, {0, 0} }, /* none */
2214 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
2215 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
2216 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
2217 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
2218 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
2219 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
2220 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
2221 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
2222 { { }, 0x9, {0x000000, 64 * 256 * 1024} }, /* blocks 0-255 */
2223 { { }, 0xa, {0x000000, 64 * 512 * 1024} }, /* all */
2224 { { }, 0xb, {0x000000, 64 * 512 * 1024} }, /* all */
2225 { { }, 0xc, {0x000000, 64 * 512 * 1024} }, /* all */
2226 { { }, 0xd, {0x000000, 64 * 512 * 1024} }, /* all */
2227 { { }, 0xe, {0x000000, 64 * 512 * 1024} }, /* all */
2228 { { }, 0xf, {0x000000, 64 * 512 * 1024} }, /* all */
2229};
2230
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002231static struct wp_context mx25l25635f_wp = {
Vic Yang848bfd12018-03-23 10:24:07 -07002232 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
2233};
2234
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002235static struct wp_range_descriptor s25fs128s_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002236 { { .tb = 1 }, 0, {0, 0} }, /* none */
2237 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
2238 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
2239 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
2240 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
2241 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
2242 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
2243 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002244
David Hendricks148a4bf2015-03-13 21:02:42 -07002245 { { .tb = 0 }, 0, {0, 0} }, /* none */
2246 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
2247 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
2248 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
2249 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
2250 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
2251 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
2252 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08002253};
2254
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002255static struct wp_context s25fs128s_wp = {
David Hendricksa9884852014-12-11 15:31:12 -08002256 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002257 .get_modifier_bits = s25f_get_modifier_bits,
2258 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08002259};
2260
David Hendricksc694bb82015-02-25 14:52:17 -08002261
Edward O'Callaghan3b996502020-04-12 20:46:51 +10002262static struct wp_range_descriptor s25fl256s_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07002263 { { .tb = 1 }, 0, {0, 0} }, /* none */
2264 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
2265 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
2266 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
2267 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
2268 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
2269 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
2270 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
2271
2272 { { .tb = 0 }, 0, {0, 0} }, /* none */
2273 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
2274 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
2275 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
2276 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
2277 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
2278 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
2279 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08002280};
2281
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002282static struct wp_context s25fl256s_wp = {
David Hendricksc694bb82015-02-25 14:52:17 -08002283 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07002284 .get_modifier_bits = s25f_get_modifier_bits,
2285 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08002286};
2287
David Hendrickse0512a72014-07-15 20:30:47 -07002288/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002289static int generic_range_table(const struct flashctx *flash,
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002290 struct wp_context **wp,
David Hendrickse0512a72014-07-15 20:30:47 -07002291 int *num_entries)
2292{
2293 *wp = NULL;
2294 *num_entries = 0;
2295
Patrick Georgif3fa2992017-02-02 16:24:44 +01002296 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07002297 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002298 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002299
Martin Roth563a1fe2017-04-18 14:26:27 -06002300 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07002301 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002302 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07002303 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002304
David Hendricksaf3944a2014-07-28 18:37:40 -07002305 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002306 (*wp)->descrs = &gd25q32_cmp0_ranges[0];
David Hendricksaf3944a2014-07-28 18:37:40 -07002307 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
2308 } else { /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002309 (*wp)->descrs = &gd25q32_cmp1_ranges[0];
David Hendricksaf3944a2014-07-28 18:37:40 -07002310 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
2311 }
2312
2313 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002314 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07002315 case GIGADEVICE_GD25Q128:
Aaron Durbin6c957d72018-08-20 09:31:01 -06002316 case GIGADEVICE_GD25LQ128CD: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002317 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002318 *wp = &gd25q128_wp;
2319
2320 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002321 (*wp)->descrs = &gd25q128_cmp0_ranges[0];
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002322 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
2323 } else { /* CMP == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002324 (*wp)->descrs = &gd25q128_cmp1_ranges[0];
David Hendricks1e9d7ca2016-03-14 15:50:34 -07002325 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
2326 }
2327
2328 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07002329 }
2330 default:
2331 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
2332 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002333 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07002334 return -1;
2335 }
2336 break;
David Hendricks83541d32014-07-15 20:58:21 -07002337 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002338 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07002339 case MACRONIX_MX25L6405:
2340 /* FIXME: MX25L64* chips have mixed capabilities and
2341 share IDs */
2342 *wp = &mx25l6406e_wp;
2343 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
2344 break;
David Hendricksc3496092014-11-13 17:20:55 -08002345 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002346 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08002347
2348 *wp = &mx25l6495f_wp;
2349 if (!(cr & (1 << 3))) { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002350 (*wp)->descrs = &mx25l6495f_tb0_ranges[0];
David Hendricksc3496092014-11-13 17:20:55 -08002351 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
2352 } else { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002353 (*wp)->descrs = &mx25l6495f_tb1_ranges[0];
David Hendricksc3496092014-11-13 17:20:55 -08002354 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
2355 }
2356 break;
2357 }
Vic Yang848bfd12018-03-23 10:24:07 -07002358 case MACRONIX_MX25L25635F: {
2359 uint8_t cr = mx25l_read_config_register(flash);
2360
2361 *wp = &mx25l25635f_wp;
2362 if (!(cr & (1 << 3))) { /* T/B == 0 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002363 (*wp)->descrs = &mx25l25635f_tb0_ranges[0];
Vic Yang848bfd12018-03-23 10:24:07 -07002364 *num_entries = ARRAY_SIZE(mx25l25635f_tb0_ranges);
2365 } else { /* T/B == 1 */
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002366 (*wp)->descrs = &mx25l25635f_tb1_ranges[0];
Vic Yang848bfd12018-03-23 10:24:07 -07002367 *num_entries = ARRAY_SIZE(mx25l25635f_tb1_ranges);
2368 }
2369 break;
2370 }
David Hendricks83541d32014-07-15 20:58:21 -07002371 default:
2372 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
2373 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01002374 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07002375 return -1;
2376 }
2377 break;
David Hendricksa9884852014-12-11 15:31:12 -08002378 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01002379 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08002380 case SPANSION_S25FS128S_L:
2381 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08002382 *wp = &s25fs128s_wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002383 (*wp)->descrs = s25fs128s_ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -07002384 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08002385 break;
2386 }
David Hendricksc694bb82015-02-25 14:52:17 -08002387 case SPANSION_S25FL256S_UL:
2388 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08002389 *wp = &s25fl256s_wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002390 (*wp)->descrs = s25fl256s_ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -07002391 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08002392 break;
2393 }
David Hendricksa9884852014-12-11 15:31:12 -08002394 default:
2395 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01002396 ", aborting\n", __func__, __LINE__,
2397 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08002398 return -1;
2399 }
2400 break;
David Hendrickse0512a72014-07-15 20:30:47 -07002401 default:
2402 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01002403 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07002404 return -1;
2405 }
2406
2407 return 0;
2408}
2409
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002410static uint8_t generic_get_bp_mask(struct wp_context *wp)
Marco Chen9d5bddb2020-02-11 17:12:56 +08002411{
2412 return ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) ^ \
2413 ((1 << wp->sr1.bp0_pos) - 1);
2414}
2415
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002416static uint8_t generic_get_status_check_mask(struct wp_context *wp)
Marco Chen9d5bddb2020-02-11 17:12:56 +08002417{
2418 return generic_get_bp_mask(wp) | 1 << wp->sr1.srp_pos;
2419}
2420
David Hendrickse0512a72014-07-15 20:30:47 -07002421/* Given a [start, len], this function finds a block protect bit combination
2422 * (if possible) and sets the corresponding bits in "status". Remaining bits
2423 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002424static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002425 unsigned int start, unsigned int len,
Marco Chen9d5bddb2020-02-11 17:12:56 +08002426 uint8_t *status, uint8_t *check_mask)
David Hendrickse0512a72014-07-15 20:30:47 -07002427{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002428 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002429 struct wp_range_descriptor *r;
David Hendrickse0512a72014-07-15 20:30:47 -07002430 int i, range_found = 0, num_entries;
2431 uint8_t bp_mask;
2432
2433 if (generic_range_table(flash, &wp, &num_entries))
2434 return -1;
2435
Marco Chen9d5bddb2020-02-11 17:12:56 +08002436 bp_mask = generic_get_bp_mask(wp);
David Hendrickse0512a72014-07-15 20:30:47 -07002437
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002438 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
David Hendrickse0512a72014-07-15 20:30:47 -07002439 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
2440 start, len, r->range.start, r->range.len);
2441 if ((start == r->range.start) && (len == r->range.len)) {
2442 *status &= ~(bp_mask);
2443 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07002444
2445 if (wp->set_modifier_bits) {
2446 if (wp->set_modifier_bits(flash, &r->m) < 0) {
Edward O'Callaghan0b662c12021-01-22 00:30:24 +11002447 msg_cerr("error setting modifier bits for range.\n");
David Hendricks148a4bf2015-03-13 21:02:42 -07002448 return -1;
2449 }
2450 }
2451
David Hendrickse0512a72014-07-15 20:30:47 -07002452 range_found = 1;
2453 break;
2454 }
2455 }
2456
2457 if (!range_found) {
Edward O'Callaghan3be63e02020-03-27 14:44:24 +11002458 msg_cerr("%s: matching range not found\n", __func__);
David Hendrickse0512a72014-07-15 20:30:47 -07002459 return -1;
2460 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002461
Marco Chen9d5bddb2020-02-11 17:12:56 +08002462 *check_mask = generic_get_status_check_mask(wp);
David Hendrickse0512a72014-07-15 20:30:47 -07002463 return 0;
2464}
2465
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002466static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002467 const uint8_t sr1, unsigned int *start, unsigned int *len)
2468{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002469 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002470 struct wp_range_descriptor *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07002471 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07002472 uint8_t sr1_bp;
Edward O'Callaghan9c4c9a52019-12-04 18:18:01 +11002473 struct modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07002474
2475 if (generic_range_table(flash, &wp, &num_entries))
2476 return -1;
2477
David Hendricks148a4bf2015-03-13 21:02:42 -07002478 /* modifier bits may be compared more than once, so get them here */
Edward O'Callaghanadcc7782019-12-04 14:50:14 +11002479 if (wp->get_modifier_bits && wp->get_modifier_bits(flash, &m) < 0)
David Hendricks148a4bf2015-03-13 21:02:42 -07002480 return -1;
David Hendricks148a4bf2015-03-13 21:02:42 -07002481
David Hendrickse0512a72014-07-15 20:30:47 -07002482 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
2483
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002484 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07002485 if (wp->get_modifier_bits) {
2486 if (memcmp(&m, &r->m, sizeof(m)))
2487 continue;
2488 }
David Hendrickse0512a72014-07-15 20:30:47 -07002489 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
2490 if (sr1_bp == r->bp) {
2491 *start = r->range.start;
2492 *len = r->range.len;
2493 status_found = 1;
2494 break;
2495 }
2496 }
2497
2498 if (!status_found) {
2499 msg_cerr("matching status not found\n");
2500 return -1;
2501 }
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002502
David Hendrickse0512a72014-07-15 20:30:47 -07002503 return 0;
2504}
2505
2506/* Given a [start, len], this function calls generic_range_to_status() to
2507 * convert it to flash-chip-specific range bits, then sets into status register.
2508 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002509static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002510 unsigned int start, unsigned int len)
2511{
Marco Chen9d5bddb2020-02-11 17:12:56 +08002512 uint8_t status, expected, check_mask;
David Hendrickse0512a72014-07-15 20:30:47 -07002513
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002514 status = spi_read_status_register(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002515 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2516
2517 expected = status; /* preserve non-bp bits */
Marco Chen9d5bddb2020-02-11 17:12:56 +08002518 if (generic_range_to_status(flash, start, len, &expected, &check_mask))
David Hendrickse0512a72014-07-15 20:30:47 -07002519 return -1;
2520
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002521 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002522
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002523 status = spi_read_status_register(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002524 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
Marco Chen9d5bddb2020-02-11 17:12:56 +08002525 if ((status & check_mask) != (expected & check_mask)) {
2526 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
2527 expected, status, check_mask);
David Hendrickse0512a72014-07-15 20:30:47 -07002528 return 1;
2529 }
David Hendrickse0512a72014-07-15 20:30:47 -07002530 return 0;
2531}
2532
2533/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002534static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002535{
Marco Chen9d5bddb2020-02-11 17:12:56 +08002536 uint8_t status, expected, check_mask;
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002537 struct wp_context *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002538 int num_entries;
2539
2540 if (generic_range_table(flash, &wp, &num_entries))
2541 return -1;
2542
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002543 expected = spi_read_status_register(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002544 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2545
2546 if (enable)
2547 expected |= 1 << wp->sr1.srp_pos;
2548 else
2549 expected &= ~(1 << wp->sr1.srp_pos);
2550
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002551 spi_write_status_register(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002552
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002553 status = spi_read_status_register(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002554 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
Marco Chen9d5bddb2020-02-11 17:12:56 +08002555
2556 check_mask = generic_get_status_check_mask(wp);
2557 msg_cdbg("%s: check mask: 0x%02x\n", __func__, check_mask);
2558 if ((status & check_mask) != (expected & check_mask)) {
2559 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
2560 expected, status, check_mask);
David Hendrickse0512a72014-07-15 20:30:47 -07002561 return -1;
Marco Chen9d5bddb2020-02-11 17:12:56 +08002562 }
David Hendrickse0512a72014-07-15 20:30:47 -07002563
2564 return 0;
2565}
2566
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002567static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002568 enum wp_mode wp_mode)
2569{
2570 int ret;
2571
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002572 if (wp_mode != WP_MODE_HARDWARE) {
David Hendrickse0512a72014-07-15 20:30:47 -07002573 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2574 return 1;
2575 }
2576
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002577 ret = generic_set_srp0(flash, 1);
David Hendrickse0512a72014-07-15 20:30:47 -07002578 if (ret)
2579 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanca44e5c2019-12-04 14:23:54 +11002580
David Hendrickse0512a72014-07-15 20:30:47 -07002581 return ret;
2582}
2583
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002584static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002585{
2586 int ret;
2587
2588 ret = generic_set_srp0(flash, 0);
2589 if (ret)
2590 msg_cerr("%s(): error=%d.\n", __func__, ret);
Edward O'Callaghanbea239e2019-12-04 14:42:54 +11002591
David Hendrickse0512a72014-07-15 20:30:47 -07002592 return ret;
2593}
2594
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002595static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002596{
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002597 struct wp_context *wp;
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002598 struct wp_range_descriptor *r;
David Hendrickse0512a72014-07-15 20:30:47 -07002599 int i, num_entries;
2600
2601 if (generic_range_table(flash, &wp, &num_entries))
2602 return -1;
2603
Edward O'Callaghane146f9a2019-12-05 14:27:24 +11002604 r = &wp->descrs[0];
David Hendrickse0512a72014-07-15 20:30:47 -07002605 for (i = 0; i < num_entries; i++) {
2606 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2607 r->range.start, r->range.len);
2608 r++;
2609 }
2610
2611 return 0;
2612}
2613
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002614static int wp_context_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002615{
2616 uint8_t sr1;
2617 unsigned int start, len;
2618 int ret = 0;
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002619 struct wp_context *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002620 int num_entries, wp_en;
2621
2622 if (generic_range_table(flash, &wp, &num_entries))
2623 return -1;
2624
Nikolai Artemiev48b424b2021-04-06 14:12:02 +10002625 sr1 = spi_read_status_register(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002626 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2627
2628 msg_cinfo("WP: status: 0x%04x\n", sr1);
2629 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2630 /* FIXME: SRP1 is not really generic, but we probably should print
2631 * it anyway to have consistent output. #legacycruft */
2632 msg_cinfo("WP: status.srp1: %x\n", 0);
2633 msg_cinfo("WP: write protect is %s.\n",
2634 wp_en ? "enabled" : "disabled");
2635
2636 msg_cinfo("WP: write protect range: ");
2637 if (generic_status_to_range(flash, sr1, &start, &len)) {
2638 msg_cinfo("(cannot resolve the range)\n");
2639 ret = -1;
2640 } else {
2641 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2642 }
2643
2644 return ret;
2645}
2646
2647struct wp wp_generic = {
2648 .list_ranges = generic_list_ranges,
2649 .set_range = generic_set_range,
2650 .enable = generic_enable_writeprotect,
2651 .disable = generic_disable_writeprotect,
Edward O'Callaghana3edcb22019-12-05 14:30:50 +11002652 .wp_status = wp_context_status,
David Hendrickse0512a72014-07-15 20:30:47 -07002653};