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