blob: 4048311e270e256e028dd8e652629074856e18c2 [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
Jongpil66a96492014-08-14 17:59:06 +0900354static struct w25q_range mx25u6435e_ranges[] = {
355 { X, 0, 0, {0, 0} }, /* none */
356 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
357 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
358 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
359 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
360 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
361 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
362 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
363
364 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
365 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
366 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
367 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
368 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
369 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
370 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
371 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
372};
373
David Hendricksbfa624b2012-07-24 12:47:59 -0700374static struct w25q_range n25q064_ranges[] = {
375 { X, 0, 0, {0, 0} }, /* none */
376
377 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
378 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
379 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
380 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
381 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
382 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
383 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
384
385 { 1, 0, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
386 { 1, 0, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
387 { 1, 0, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
388 { 1, 0, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
389 { 1, 0, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
390 { 1, 0, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
391 { 1, 0, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
392
393 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
394 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
395 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
396 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
397 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
398 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
399 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
400 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
401};
402
David Hendricksf7924d12010-06-10 21:26:44 -0700403static struct w25q_range w25q16_ranges[] = {
404 { X, X, 0, {0, 0} }, /* none */
405 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
406 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
407 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
408 { 0, 0, 0x4, {0x180000, 512 * 1024} },
409 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
410
411 { 0, 1, 0x1, {0x000000, 64 * 1024} },
412 { 0, 1, 0x2, {0x000000, 128 * 1024} },
413 { 0, 1, 0x3, {0x000000, 256 * 1024} },
414 { 0, 1, 0x4, {0x000000, 512 * 1024} },
415 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
416 { X, X, 0x6, {0x000000, 2048 * 1024} },
417 { X, X, 0x7, {0x000000, 2048 * 1024} },
418
419 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
420 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
421 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
422 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
423 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
424
425 { 1, 1, 0x1, {0x000000, 4 * 1024} },
426 { 1, 1, 0x2, {0x000000, 8 * 1024} },
427 { 1, 1, 0x3, {0x000000, 16 * 1024} },
428 { 1, 1, 0x4, {0x000000, 32 * 1024} },
429 { 1, 1, 0x5, {0x000000, 32 * 1024} },
430};
431
432static struct w25q_range w25q32_ranges[] = {
433 { X, X, 0, {0, 0} }, /* none */
434 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
435 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
436 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
437 { 0, 0, 0x4, {0x380000, 512 * 1024} },
438 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700439 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700440
441 { 0, 1, 0x1, {0x000000, 64 * 1024} },
442 { 0, 1, 0x2, {0x000000, 128 * 1024} },
443 { 0, 1, 0x3, {0x000000, 256 * 1024} },
444 { 0, 1, 0x4, {0x000000, 512 * 1024} },
445 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
446 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
447 { X, X, 0x7, {0x000000, 4096 * 1024} },
448
449 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
450 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
451 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
452 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
453 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
454
455 { 1, 1, 0x1, {0x000000, 4 * 1024} },
456 { 1, 1, 0x2, {0x000000, 8 * 1024} },
457 { 1, 1, 0x3, {0x000000, 16 * 1024} },
458 { 1, 1, 0x4, {0x000000, 32 * 1024} },
459 { 1, 1, 0x5, {0x000000, 32 * 1024} },
460};
461
462static struct w25q_range w25q80_ranges[] = {
463 { X, X, 0, {0, 0} }, /* none */
464 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
465 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
466 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
467 { 0, 0, 0x4, {0x080000, 512 * 1024} },
468
469 { 0, 1, 0x1, {0x000000, 64 * 1024} },
470 { 0, 1, 0x2, {0x000000, 128 * 1024} },
471 { 0, 1, 0x3, {0x000000, 256 * 1024} },
472 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700473 { X, X, 0x6, {0x000000, 1024 * 1024} },
474 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700475
476 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
477 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
478 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
479 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
480 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
481
482 { 1, 1, 0x1, {0x000000, 4 * 1024} },
483 { 1, 1, 0x2, {0x000000, 8 * 1024} },
484 { 1, 1, 0x3, {0x000000, 16 * 1024} },
485 { 1, 1, 0x4, {0x000000, 32 * 1024} },
486 { 1, 1, 0x5, {0x000000, 32 * 1024} },
487};
488
David Hendricks2c4a76c2010-06-28 14:00:43 -0700489static struct w25q_range w25q64_ranges[] = {
490 { X, X, 0, {0, 0} }, /* none */
491
492 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
493 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
494 { 0, 0, 0x3, {0x780000, 512 * 1024} },
495 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
496 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
497 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
498
499 { 0, 1, 0x1, {0x000000, 128 * 1024} },
500 { 0, 1, 0x2, {0x000000, 256 * 1024} },
501 { 0, 1, 0x3, {0x000000, 512 * 1024} },
502 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
503 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
504 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
505 { X, X, 0x7, {0x000000, 8192 * 1024} },
506
507 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
508 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
509 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
510 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
511 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
512
513 { 1, 1, 0x1, {0x000000, 4 * 1024} },
514 { 1, 1, 0x2, {0x000000, 8 * 1024} },
515 { 1, 1, 0x3, {0x000000, 16 * 1024} },
516 { 1, 1, 0x4, {0x000000, 32 * 1024} },
517 { 1, 1, 0x5, {0x000000, 32 * 1024} },
518};
519
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800520struct w25q_range w25x10_ranges[] = {
521 { X, X, 0, {0, 0} }, /* none */
522 { 0, 0, 0x1, {0x010000, 64 * 1024} },
523 { 0, 1, 0x1, {0x000000, 64 * 1024} },
524 { X, X, 0x2, {0x000000, 128 * 1024} },
525 { X, X, 0x3, {0x000000, 128 * 1024} },
526};
527
528struct w25q_range w25x20_ranges[] = {
529 { X, X, 0, {0, 0} }, /* none */
530 { 0, 0, 0x1, {0x030000, 64 * 1024} },
531 { 0, 0, 0x2, {0x020000, 128 * 1024} },
532 { 0, 1, 0x1, {0x000000, 64 * 1024} },
533 { 0, 1, 0x2, {0x000000, 128 * 1024} },
534 { 0, X, 0x3, {0x000000, 256 * 1024} },
535};
536
David Hendricks470ca952010-08-13 14:01:53 -0700537struct w25q_range w25x40_ranges[] = {
538 { X, X, 0, {0, 0} }, /* none */
539 { 0, 0, 0x1, {0x070000, 64 * 1024} },
540 { 0, 0, 0x2, {0x060000, 128 * 1024} },
541 { 0, 0, 0x3, {0x040000, 256 * 1024} },
542 { 0, 1, 0x1, {0x000000, 64 * 1024} },
543 { 0, 1, 0x2, {0x000000, 128 * 1024} },
544 { 0, 1, 0x3, {0x000000, 256 * 1024} },
545 { 0, X, 0x4, {0x000000, 512 * 1024} },
546};
547
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800548struct w25q_range w25x80_ranges[] = {
549 { X, X, 0, {0, 0} }, /* none */
550 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
551 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
552 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
553 { 0, 0, 0x4, {0x080000, 512 * 1024} },
554 { 0, 1, 0x1, {0x000000, 64 * 1024} },
555 { 0, 1, 0x2, {0x000000, 128 * 1024} },
556 { 0, 1, 0x3, {0x000000, 256 * 1024} },
557 { 0, 1, 0x4, {0x000000, 512 * 1024} },
558 { 0, X, 0x5, {0x000000, 1024 * 1024} },
559 { 0, X, 0x6, {0x000000, 1024 * 1024} },
560 { 0, X, 0x7, {0x000000, 1024 * 1024} },
561};
562
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700563static struct w25q_range gd25q64_ranges[] = {
564 { X, X, 0, {0, 0} }, /* none */
565 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
566 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
567 { 0, 0, 0x3, {0x780000, 512 * 1024} },
568 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
569 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
570 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
571
572 { 0, 1, 0x1, {0x000000, 128 * 1024} },
573 { 0, 1, 0x2, {0x000000, 256 * 1024} },
574 { 0, 1, 0x3, {0x000000, 512 * 1024} },
575 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
576 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
577 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
578 { X, X, 0x7, {0x000000, 8192 * 1024} },
579
580 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
581 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
582 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
583 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
584 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
585 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
586
587 { 1, 1, 0x1, {0x000000, 4 * 1024} },
588 { 1, 1, 0x2, {0x000000, 8 * 1024} },
589 { 1, 1, 0x3, {0x000000, 16 * 1024} },
590 { 1, 1, 0x4, {0x000000, 32 * 1024} },
591 { 1, 1, 0x5, {0x000000, 32 * 1024} },
592 { 1, 1, 0x6, {0x000000, 32 * 1024} },
593};
594
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800595static struct w25q_range a25l040_ranges[] = {
596 { X, X, 0x0, {0, 0} }, /* none */
597 { X, X, 0x1, {0x70000, 64 * 1024} },
598 { X, X, 0x2, {0x60000, 128 * 1024} },
599 { X, X, 0x3, {0x40000, 256 * 1024} },
600 { X, X, 0x4, {0x00000, 512 * 1024} },
601 { X, X, 0x5, {0x00000, 512 * 1024} },
602 { X, X, 0x6, {0x00000, 512 * 1024} },
603 { X, X, 0x7, {0x00000, 512 * 1024} },
604};
605
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800606/* Given a flash chip, this function returns its range table. */
607static int w25_range_table(const struct flashchip *flash,
608 struct w25q_range **w25q_ranges,
609 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700610{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800611 *w25q_ranges = 0;
612 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700613
David Hendricksd494b0a2010-08-16 16:28:50 -0700614 switch (flash->manufacture_id) {
615 case WINBOND_NEX_ID:
616 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800617 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800618 *w25q_ranges = w25x10_ranges;
619 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800620 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800621 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800622 *w25q_ranges = w25x20_ranges;
623 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800624 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800625 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800626 *w25q_ranges = w25x40_ranges;
627 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700628 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800629 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800630 *w25q_ranges = w25x80_ranges;
631 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800632 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800633 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800634 *w25q_ranges = w25q80_ranges;
635 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700636 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800637 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800638 *w25q_ranges = w25q16_ranges;
639 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700640 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800641 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800642 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800643 *w25q_ranges = w25q32_ranges;
644 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700645 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800646 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800647 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800648 *w25q_ranges = w25q64_ranges;
649 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700650 break;
651 default:
652 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
653 ", aborting\n", __func__, __LINE__,
654 flash->model_id);
655 return -1;
656 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700657 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700658 case EON_ID_NOPREFIX:
659 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800660 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800661 *w25q_ranges = en25f40_ranges;
662 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700663 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700664 case EON_EN25Q40:
665 *w25q_ranges = en25q40_ranges;
666 *num_entries = ARRAY_SIZE(en25q40_ranges);
667 break;
668 case EON_EN25Q80:
669 *w25q_ranges = en25q80_ranges;
670 *num_entries = ARRAY_SIZE(en25q80_ranges);
671 break;
672 case EON_EN25Q32:
673 *w25q_ranges = en25q32_ranges;
674 *num_entries = ARRAY_SIZE(en25q32_ranges);
675 break;
676 case EON_EN25Q64:
677 *w25q_ranges = en25q64_ranges;
678 *num_entries = ARRAY_SIZE(en25q64_ranges);
679 break;
680 case EON_EN25Q128:
681 *w25q_ranges = en25q128_ranges;
682 *num_entries = ARRAY_SIZE(en25q128_ranges);
683 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600684 case EON_EN25S64:
685 *w25q_ranges = en25s64_ranges;
686 *num_entries = ARRAY_SIZE(en25s64_ranges);
687 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700688 default:
689 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
690 ", aborting\n", __func__, __LINE__,
691 flash->model_id);
692 return -1;
693 }
694 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800695 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700696 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800697 case MACRONIX_MX25L1005:
698 *w25q_ranges = mx25l1005_ranges;
699 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
700 break;
701 case MACRONIX_MX25L2005:
702 *w25q_ranges = mx25l2005_ranges;
703 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
704 break;
705 case MACRONIX_MX25L4005:
706 *w25q_ranges = mx25l4005_ranges;
707 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
708 break;
709 case MACRONIX_MX25L8005:
710 *w25q_ranges = mx25l8005_ranges;
711 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
712 break;
713 case MACRONIX_MX25L1605:
714 /* FIXME: MX25L1605 and MX25L1605D have different write
715 * protection capabilities, but share IDs */
716 *w25q_ranges = mx25l1605d_ranges;
717 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
718 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800719 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800720 *w25q_ranges = mx25l3205d_ranges;
721 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700722 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800723 case MACRONIX_MX25U3235E:
724 *w25q_ranges = mx25u3235e_ranges;
725 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
726 break;
Jongpil66a96492014-08-14 17:59:06 +0900727 case MACRONIX_MX25U6435E:
728 *w25q_ranges = mx25u6435e_ranges;
729 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
730 break;
David Hendricksac72e362010-08-16 18:20:03 -0700731 default:
732 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
733 ", aborting\n", __func__, __LINE__,
734 flash->model_id);
735 return -1;
736 }
737 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700738 case ST_ID:
739 switch(flash->model_id) {
740 case ST_N25Q064__1E:
741 case ST_N25Q064__3E:
742 *w25q_ranges = n25q064_ranges;
743 *num_entries = ARRAY_SIZE(n25q064_ranges);
744 break;
745 default:
746 msg_cerr("%s() %d: Micron flash chip mismatch"
747 " (0x%04x), aborting\n", __func__, __LINE__,
748 flash->model_id);
749 return -1;
750 }
751 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700752 case GIGADEVICE_ID:
753 switch(flash->model_id) {
754 case GIGADEVICE_GD25LQ32:
755 *w25q_ranges = w25q32_ranges;
756 *num_entries = ARRAY_SIZE(w25q32_ranges);
757 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700758 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600759 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700760 *w25q_ranges = gd25q64_ranges;
761 *num_entries = ARRAY_SIZE(gd25q64_ranges);
762 break;
763 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700764 default:
765 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
766 " (0x%04x), aborting\n", __func__, __LINE__,
767 flash->model_id);
768 return -1;
769 }
770 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800771 case AMIC_ID_NOPREFIX:
772 switch(flash->model_id) {
773 case AMIC_A25L040:
774 *w25q_ranges = a25l040_ranges;
775 *num_entries = ARRAY_SIZE(a25l040_ranges);
776 break;
777 default:
778 msg_cerr("%s() %d: AMIC flash chip mismatch"
779 " (0x%04x), aborting\n", __func__, __LINE__,
780 flash->model_id);
781 return -1;
782 }
783 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700784 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700785 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
786 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700787 return -1;
788 }
789
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800790 return 0;
791}
792
793int w25_range_to_status(const struct flashchip *flash,
794 unsigned int start, unsigned int len,
795 struct w25q_status *status)
796{
797 struct w25q_range *w25q_ranges;
798 int i, range_found = 0;
799 int num_entries;
800
801 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700802 for (i = 0; i < num_entries; i++) {
803 struct wp_range *r = &w25q_ranges[i].range;
804
805 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
806 start, len, r->start, r->len);
807 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700808 status->bp0 = w25q_ranges[i].bp & 1;
809 status->bp1 = w25q_ranges[i].bp >> 1;
810 status->bp2 = w25q_ranges[i].bp >> 2;
811 status->tb = w25q_ranges[i].tb;
812 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700813
814 range_found = 1;
815 break;
816 }
817 }
818
819 if (!range_found) {
820 msg_cerr("matching range not found\n");
821 return -1;
822 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700823 return 0;
824}
825
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800826int w25_status_to_range(const struct flashchip *flash,
827 const struct w25q_status *status,
828 unsigned int *start, unsigned int *len)
829{
830 struct w25q_range *w25q_ranges;
831 int i, status_found = 0;
832 int num_entries;
833
834 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
835 for (i = 0; i < num_entries; i++) {
836 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800837 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800838
839 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
840 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
841 bp, w25q_ranges[i].bp,
842 status->tb, w25q_ranges[i].tb,
843 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800844 table_bp = w25q_ranges[i].bp;
845 table_tb = w25q_ranges[i].tb;
846 table_sec = w25q_ranges[i].sec;
847 if ((bp == table_bp || table_bp == X) &&
848 (status->tb == table_tb || table_tb == X) &&
849 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800850 *start = w25q_ranges[i].range.start;
851 *len = w25q_ranges[i].range.len;
852
853 status_found = 1;
854 break;
855 }
856 }
857
858 if (!status_found) {
859 msg_cerr("matching status not found\n");
860 return -1;
861 }
862 return 0;
863}
864
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800865/* Since most chips we use must be WREN-ed before WRSR,
866 * we copy a write status function here before we have a good solution. */
867static int spi_write_status_register_WREN(int status)
868{
869 int result;
870 struct spi_command cmds[] = {
871 {
872 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
873 .writecnt = JEDEC_WREN_OUTSIZE,
874 .writearr = (const unsigned char[]){ JEDEC_WREN },
875 .readcnt = 0,
876 .readarr = NULL,
877 }, {
878 .writecnt = JEDEC_WRSR_OUTSIZE,
879 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
880 .readcnt = 0,
881 .readarr = NULL,
882 }, {
883 .writecnt = 0,
884 .writearr = NULL,
885 .readcnt = 0,
886 .readarr = NULL,
887 }};
888
889 result = spi_send_multicommand(cmds);
890 if (result) {
891 msg_cerr("%s failed during command execution\n",
892 __func__);
893 }
Louis Yung-Chieh Lo96222b12010-11-01 11:48:11 +0800894
895 /* WRSR performs a self-timed erase before the changes take effect. */
896 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
897
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +0800898 return result;
899}
900
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800901/* Given a [start, len], this function calls w25_range_to_status() to convert
902 * it to flash-chip-specific range bits, then sets into status register.
903 */
David Hendricks91040832011-07-08 20:01:09 -0700904static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700905 unsigned int start, unsigned int len)
906{
907 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800908 int tmp = 0;
909 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700910
911 memset(&status, 0, sizeof(status));
912 tmp = spi_read_status_register();
913 memcpy(&status, &tmp, 1);
914 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
915
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800916 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700917
918 msg_cdbg("status.busy: %x\n", status.busy);
919 msg_cdbg("status.wel: %x\n", status.wel);
920 msg_cdbg("status.bp0: %x\n", status.bp0);
921 msg_cdbg("status.bp1: %x\n", status.bp1);
922 msg_cdbg("status.bp2: %x\n", status.bp2);
923 msg_cdbg("status.tb: %x\n", status.tb);
924 msg_cdbg("status.sec: %x\n", status.sec);
925 msg_cdbg("status.srp0: %x\n", status.srp0);
926
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800927 memcpy(&expected, &status, sizeof(status));
928 spi_write_status_register_WREN(expected);
David Hendricksf7924d12010-06-10 21:26:44 -0700929
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800930 tmp = spi_read_status_register();
931 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
932 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800933 return 0;
934 } else {
David Hendricksc801adb2010-12-09 16:58:56 -0800935 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800936 expected, tmp);
937 return 1;
938 }
David Hendricksf7924d12010-06-10 21:26:44 -0700939}
940
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800941/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -0700942static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800943{
944 struct w25q_status status;
945 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -0700946 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800947 int ret = 0;
948
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800949 memset(&status, 0, sizeof(status));
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800950 tmp = spi_read_status_register();
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800951 memcpy(&status, &tmp, 1);
952 msg_cinfo("WP: status: 0x%02x\n", tmp);
953 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
954 msg_cinfo("WP: write protect is %s.\n",
955 status.srp0 ? "enabled" : "disabled");
956
957 msg_cinfo("WP: write protect range: ");
958 if (w25_status_to_range(flash, &status, &start, &len)) {
959 msg_cinfo("(cannot resolve the range)\n");
960 ret = -1;
961 } else {
962 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
963 }
964
965 return ret;
966}
967
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800968/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -0700969static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -0700970{
971 struct w25q_status status;
972 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800973 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700974
975 memset(&status, 0, sizeof(status));
976 tmp = spi_read_status_register();
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800977 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -0700978 memcpy(&status, &tmp, 1);
979 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
980
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800981 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800982 memcpy(&expected, &status, sizeof(status));
983 spi_write_status_register_WREN(expected);
984
985 tmp = spi_read_status_register();
986 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
987 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
988 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -0700989
990 return 0;
991}
992
David Hendricks1c09f802012-10-03 11:03:48 -0700993static int w25_enable_writeprotect(const struct flashchip *flash,
994 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +0800995{
996 int ret;
997
David Hendricks1c09f802012-10-03 11:03:48 -0700998 switch (wp_mode) {
999 case WP_MODE_HARDWARE:
1000 ret = w25_set_srp0(flash, 1);
1001 break;
1002 default:
1003 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1004 return 1;
1005 }
1006
David Hendricksc801adb2010-12-09 16:58:56 -08001007 if (ret)
1008 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001009 return ret;
1010}
1011
David Hendricks91040832011-07-08 20:01:09 -07001012static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001013{
1014 int ret;
1015
1016 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001017 if (ret)
1018 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001019 return ret;
1020}
1021
David Hendricks91040832011-07-08 20:01:09 -07001022static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001023{
1024 struct w25q_range *w25q_ranges;
1025 int i, num_entries;
1026
1027 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1028 for (i = 0; i < num_entries; i++) {
1029 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1030 w25q_ranges[i].range.start,
1031 w25q_ranges[i].range.len);
1032 }
1033
1034 return 0;
1035}
1036
David Hendricks1c09f802012-10-03 11:03:48 -07001037/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1038uint8_t w25q_read_status_register_2(void)
1039{
1040 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
1041 unsigned char readarr[2];
1042 int ret;
1043
1044 /* Read Status Register */
1045 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1046 if (ret) {
1047 /*
1048 * FIXME: make this a benign failure for now in case we are
1049 * unable to execute the opcode
1050 */
1051 msg_cdbg("RDSR2 failed!\n");
1052 readarr[0] = 0x00;
1053 }
1054
1055 return readarr[0];
1056}
1057
1058static int w25q_wp_status(const struct flashchip *flash)
1059{
1060 struct w25q_status sr1;
1061 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001062 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001063 unsigned int start, len;
1064 int ret = 0;
1065
1066 memset(&sr1, 0, sizeof(sr1));
David Hendricksf1bd8802012-10-30 11:37:57 -07001067 tmp[0] = spi_read_status_register();
1068 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001069
David Hendricksf1bd8802012-10-30 11:37:57 -07001070 memset(&sr2, 0, sizeof(sr2));
1071 tmp[1] = w25q_read_status_register_2();
1072 memcpy(&sr2, &tmp[1], 1);
1073
1074 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001075 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1076 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1077 msg_cinfo("WP: write protect is %s.\n",
1078 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1079
1080 msg_cinfo("WP: write protect range: ");
1081 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1082 msg_cinfo("(cannot resolve the range)\n");
1083 ret = -1;
1084 } else {
1085 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1086 }
1087
1088 return ret;
1089}
1090
1091/*
1092 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1093 * de-asserted after the first byte, then it acts like a JEDEC-standard
1094 * WRSR command. if /CS is asserted, then the next data byte is written
1095 * into status register 2.
1096 */
1097#define W25Q_WRSR_OUTSIZE 0x03
1098static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1099{
1100 int result;
1101 struct spi_command cmds[] = {
1102 {
1103 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1104 .writecnt = JEDEC_WREN_OUTSIZE,
1105 .writearr = (const unsigned char[]){ JEDEC_WREN },
1106 .readcnt = 0,
1107 .readarr = NULL,
1108 }, {
1109 .writecnt = W25Q_WRSR_OUTSIZE,
1110 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1111 .readcnt = 0,
1112 .readarr = NULL,
1113 }, {
1114 .writecnt = 0,
1115 .writearr = NULL,
1116 .readcnt = 0,
1117 .readarr = NULL,
1118 }};
1119
1120 result = spi_send_multicommand(cmds);
1121 if (result) {
1122 msg_cerr("%s failed during command execution\n",
1123 __func__);
1124 }
1125
1126 /* WRSR performs a self-timed erase before the changes take effect. */
1127 programmer_delay(WRITE_STATUS_REGISTER_DELAY);
1128
1129 return result;
1130}
1131
1132/*
1133 * Set/clear the SRP1 bit in status register 2.
1134 * FIXME: make this more generic if other chips use the same SR2 layout
1135 */
1136static int w25q_set_srp1(const struct flashchip *flash, int enable)
1137{
1138 struct w25q_status sr1;
1139 struct w25q_status_2 sr2;
1140 uint8_t tmp, expected;
1141
1142 tmp = spi_read_status_register();
1143 memcpy(&sr1, &tmp, 1);
1144 tmp = w25q_read_status_register_2();
1145 memcpy(&sr2, &tmp, 1);
1146
1147 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1148
1149 sr2.srp1 = enable ? 1 : 0;
1150
1151 memcpy(&expected, &sr2, 1);
1152 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1153
1154 tmp = w25q_read_status_register_2();
1155 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1156 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1157 return 1;
1158
1159 return 0;
1160}
1161
1162enum wp_mode get_wp_mode(const char *mode_str)
1163{
1164 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1165
1166 if (!strcasecmp(mode_str, "hardware"))
1167 wp_mode = WP_MODE_HARDWARE;
1168 else if (!strcasecmp(mode_str, "power_cycle"))
1169 wp_mode = WP_MODE_POWER_CYCLE;
1170 else if (!strcasecmp(mode_str, "permanent"))
1171 wp_mode = WP_MODE_PERMANENT;
1172
1173 return wp_mode;
1174}
1175
1176static int w25q_disable_writeprotect(const struct flashchip *flash,
1177 enum wp_mode wp_mode)
1178{
1179 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001180 struct w25q_status_2 sr2;
1181 uint8_t tmp;
1182
1183 switch (wp_mode) {
1184 case WP_MODE_HARDWARE:
1185 ret = w25_set_srp0(flash, 0);
1186 break;
1187 case WP_MODE_POWER_CYCLE:
1188 tmp = w25q_read_status_register_2();
1189 memcpy(&sr2, &tmp, 1);
1190 if (sr2.srp1) {
1191 msg_cerr("%s(): must disconnect power to disable "
1192 "write-protection\n", __func__);
1193 } else {
1194 ret = 0;
1195 }
1196 break;
1197 case WP_MODE_PERMANENT:
1198 msg_cerr("%s(): cannot disable permanent write-protection\n",
1199 __func__);
1200 break;
1201 default:
1202 msg_cerr("%s(): invalid mode specified\n", __func__);
1203 break;
1204 }
1205
1206 if (ret)
1207 msg_cerr("%s(): error=%d.\n", __func__, ret);
1208 return ret;
1209}
1210
1211static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1212{
1213 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1214}
1215
1216static int w25q_enable_writeprotect(const struct flashchip *flash,
1217 enum wp_mode wp_mode)
1218{
1219 int ret = 1;
1220 struct w25q_status sr1;
1221 struct w25q_status_2 sr2;
1222 uint8_t tmp;
1223
1224 switch (wp_mode) {
1225 case WP_MODE_HARDWARE:
1226 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1227 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1228 __func__);
1229 break;
1230 }
1231
1232 tmp = spi_read_status_register();
1233 memcpy(&sr1, &tmp, 1);
1234 if (sr1.srp0)
1235 ret = 0;
1236 else
1237 ret = w25_set_srp0(flash, 1);
1238
1239 break;
1240 case WP_MODE_POWER_CYCLE:
1241 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1242 msg_cerr("%s(): cannot disable hardware WP mode\n",
1243 __func__);
1244 break;
1245 }
1246
1247 tmp = w25q_read_status_register_2();
1248 memcpy(&sr2, &tmp, 1);
1249 if (sr2.srp1)
1250 ret = 0;
1251 else
1252 ret = w25q_set_srp1(flash, 1);
1253
1254 break;
1255 case WP_MODE_PERMANENT:
1256 tmp = spi_read_status_register();
1257 memcpy(&sr1, &tmp, 1);
1258 if (sr1.srp0 == 0) {
1259 ret = w25_set_srp0(flash, 1);
1260 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001261 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001262 "permanent WP\n", __func__);
1263 break;
1264 }
1265 }
1266
1267 tmp = w25q_read_status_register_2();
1268 memcpy(&sr2, &tmp, 1);
1269 if (sr2.srp1 == 0) {
1270 ret = w25q_set_srp1(flash, 1);
1271 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001272 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001273 "permanent WP\n", __func__);
1274 break;
1275 }
1276 }
1277
1278 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001279 default:
1280 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1281 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001282 }
1283
1284 if (ret)
1285 msg_cerr("%s(): error=%d.\n", __func__, ret);
1286 return ret;
1287}
1288
1289/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001290struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001291 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001292 .set_range = w25_set_range,
1293 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001294 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001295 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001296
1297};
1298
1299/* W25Q series has features such as a second status register and SFDP */
1300struct wp wp_w25q = {
1301 .list_ranges = w25_list_ranges,
1302 .set_range = w25_set_range,
1303 .enable = w25q_enable_writeprotect,
1304 /*
1305 * By default, disable hardware write-protection. We may change
1306 * this later if we want to add fine-grained write-protect disable
1307 * as a command-line option.
1308 */
1309 .disable = w25q_disable_writeprotect_default,
1310 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001311};
David Hendrickse0512a72014-07-15 20:30:47 -07001312
David Hendricksaf3944a2014-07-28 18:37:40 -07001313struct generic_range gd25q32_cmp0_ranges[] = {
1314 /* none, bp4 and bp3 => don't care */
1315 { 0x00, {0, 0} },
1316 { 0x08, {0, 0} },
1317 { 0x10, {0, 0} },
1318 { 0x18, {0, 0} },
1319
1320 { 0x01, {0x3f0000, 64 * 1024} },
1321 { 0x02, {0x3e0000, 128 * 1024} },
1322 { 0x03, {0x3c0000, 256 * 1024} },
1323 { 0x04, {0x380000, 512 * 1024} },
1324 { 0x05, {0x300000, 1024 * 1024} },
1325 { 0x06, {0x200000, 2048 * 1024} },
1326
1327 { 0x09, {0x000000, 64 * 1024} },
1328 { 0x0a, {0x000000, 128 * 1024} },
1329 { 0x0b, {0x000000, 256 * 1024} },
1330 { 0x0c, {0x000000, 512 * 1024} },
1331 { 0x0d, {0x000000, 1024 * 1024} },
1332 { 0x0e, {0x000000, 2048 * 1024} },
1333
1334 /* all, bp4 and bp3 => don't care */
1335 { 0x07, {0x000000, 4096 * 1024} },
1336 { 0x0f, {0x000000, 4096 * 1024} },
1337 { 0x17, {0x000000, 4096 * 1024} },
1338 { 0x1f, {0x000000, 4096 * 1024} },
1339
1340 { 0x11, {0x3ff000, 4 * 1024} },
1341 { 0x12, {0x3fe000, 8 * 1024} },
1342 { 0x13, {0x3fc000, 16 * 1024} },
1343 { 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1344 { 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1345 { 0x16, {0x3f8000, 32 * 1024} },
1346
1347 { 0x19, {0x000000, 4 * 1024} },
1348 { 0x1a, {0x000000, 8 * 1024} },
1349 { 0x1b, {0x000000, 16 * 1024} },
1350 { 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1351 { 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1352 { 0x1e, {0x000000, 32 * 1024} },
1353};
1354
1355struct generic_range gd25q32_cmp1_ranges[] = {
1356 /* none, bp4 and bp3 => don't care */
1357 { 0x00, {0, 0} },
1358 { 0x08, {0, 0} },
1359 { 0x10, {0, 0} },
1360 { 0x18, {0, 0} },
1361
1362 { 0x01, {0x000000, 4032 * 1024} },
1363 { 0x02, {0x000000, 3968 * 1024} },
1364 { 0x03, {0x000000, 3840 * 1024} },
1365 { 0x04, {0x000000, 3584 * 1024} },
1366 { 0x05, {0x000000, 3 * 1024 * 1024} },
1367 { 0x06, {0x000000, 2 * 1024 * 1024} },
1368
1369 { 0x09, {0x010000, 4032 * 1024} },
1370 { 0x0a, {0x020000, 3968 * 1024} },
1371 { 0x0b, {0x040000, 3840 * 1024} },
1372 { 0x0c, {0x080000, 3584 * 1024} },
1373 { 0x0d, {0x100000, 3 * 1024 * 1024} },
1374 { 0x0e, {0x200000, 2 * 1024 * 1024} },
1375
1376 /* all, bp4 and bp3 => don't care */
1377 { 0x07, {0x000000, 4096 * 1024} },
1378 { 0x0f, {0x000000, 4096 * 1024} },
1379 { 0x17, {0x000000, 4096 * 1024} },
1380 { 0x1f, {0x000000, 4096 * 1024} },
1381
1382 { 0x11, {0x000000, 4092 * 1024} },
1383 { 0x12, {0x000000, 4088 * 1024} },
1384 { 0x13, {0x000000, 4080 * 1024} },
1385 { 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1386 { 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1387 { 0x16, {0x000000, 4064 * 1024} },
1388
1389 { 0x19, {0x001000, 4092 * 1024} },
1390 { 0x1a, {0x002000, 4088 * 1024} },
1391 { 0x1b, {0x040000, 4080 * 1024} },
1392 { 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1393 { 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1394 { 0x1e, {0x080000, 4064 * 1024} },
1395};
1396
1397static struct generic_wp gd25q32_wp = {
1398 /* TODO: map second status register */
1399 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1400};
1401
David Hendricks83541d32014-07-15 20:58:21 -07001402#if 0
1403/* FIXME: MX25L6405D has same ID as MX25L6406 */
1404static struct w25q_range mx25l6405d_ranges[] = {
1405 { X, 0, 0, {0, 0} }, /* none */
1406 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1407 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1408 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1409 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1410 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1411 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1412 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1413
1414 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1415 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1416 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1417 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1418 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1419 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1420 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1421 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1422};
1423#endif
1424
1425/* FIXME: MX25L6406 has same ID as MX25L6405D */
1426struct generic_range mx25l6406e_ranges[] = {
1427 { 0, {0, 0} }, /* none */
1428 { 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1429 { 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1430 { 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1431 { 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1432 { 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1433 { 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1434
1435 { 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1436 { 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1437 { 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1438 { 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1439 { 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1440 { 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1441 { 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1442 { 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1443 { 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
1444};
1445
1446static struct generic_wp mx25l6406e_wp = {
1447 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1448 .ranges = &mx25l6406e_ranges[0],
1449};
David Hendrickse0512a72014-07-15 20:30:47 -07001450
1451/* Given a flash chip, this function returns its writeprotect info. */
1452static int generic_range_table(const struct flashchip *flash,
1453 struct generic_wp **wp,
1454 int *num_entries)
1455{
1456 *wp = NULL;
1457 *num_entries = 0;
1458
1459 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001460 case GIGADEVICE_ID:
1461 switch(flash->model_id) {
1462 case GIGADEVICE_GD25Q32: {
1463 uint8_t sr1 = w25q_read_status_register_2();
1464
1465 *wp = &gd25q32_wp;
1466 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1467 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1468 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1469 } else { /* CMP == 1 */
1470 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1471 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1472 }
1473
1474 break;
1475 /* TODO(shawnn): add support for other GD parts */
1476 }
1477 default:
1478 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1479 " (0x%04x), aborting\n", __func__, __LINE__,
1480 flash->model_id);
1481 return -1;
1482 }
1483 break;
David Hendricks83541d32014-07-15 20:58:21 -07001484 case MACRONIX_ID:
1485 switch (flash->model_id) {
1486 case MACRONIX_MX25L6405:
1487 /* FIXME: MX25L64* chips have mixed capabilities and
1488 share IDs */
1489 *wp = &mx25l6406e_wp;
1490 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1491 break;
1492 default:
1493 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1494 ", aborting\n", __func__, __LINE__,
1495 flash->model_id);
1496 return -1;
1497 }
1498 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001499 default:
1500 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1501 __func__, flash->manufacture_id);
1502 return -1;
1503 }
1504
1505 return 0;
1506}
1507
1508/* Given a [start, len], this function finds a block protect bit combination
1509 * (if possible) and sets the corresponding bits in "status". Remaining bits
1510 * are preserved. */
1511static int generic_range_to_status(const struct flashchip *flash,
1512 unsigned int start, unsigned int len,
1513 uint8_t *status)
1514{
1515 struct generic_wp *wp;
1516 struct generic_range *r;
1517 int i, range_found = 0, num_entries;
1518 uint8_t bp_mask;
1519
1520 if (generic_range_table(flash, &wp, &num_entries))
1521 return -1;
1522
1523 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1524 ((1 << wp->sr1.bp0_pos) - 1);
1525
1526 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1527 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1528 start, len, r->range.start, r->range.len);
1529 if ((start == r->range.start) && (len == r->range.len)) {
1530 *status &= ~(bp_mask);
1531 *status |= r->bp << (wp->sr1.bp0_pos);
1532 range_found = 1;
1533 break;
1534 }
1535 }
1536
1537 if (!range_found) {
1538 msg_cerr("matching range not found\n");
1539 return -1;
1540 }
1541 return 0;
1542}
1543
1544static int generic_status_to_range(const struct flashchip *flash,
1545 const uint8_t sr1, unsigned int *start, unsigned int *len)
1546{
1547 struct generic_wp *wp;
1548 struct generic_range *r;
1549 int num_entries, wp_en, i, status_found = 0;
1550 uint8_t sr1_bp;
1551
1552 if (generic_range_table(flash, &wp, &num_entries))
1553 return -1;
1554
1555 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1556
1557 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1558 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1559 if (sr1_bp == r->bp) {
1560 *start = r->range.start;
1561 *len = r->range.len;
1562 status_found = 1;
1563 break;
1564 }
1565 }
1566
1567 if (!status_found) {
1568 msg_cerr("matching status not found\n");
1569 return -1;
1570 }
1571 return 0;
1572}
1573
1574/* Given a [start, len], this function calls generic_range_to_status() to
1575 * convert it to flash-chip-specific range bits, then sets into status register.
1576 */
1577static int generic_set_range(const struct flashchip *flash,
1578 unsigned int start, unsigned int len)
1579{
1580 uint8_t status, expected;
1581
1582 status = spi_read_status_register();
1583 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1584
1585 expected = status; /* preserve non-bp bits */
1586 if (generic_range_to_status(flash, start, len, &expected))
1587 return -1;
1588
1589 spi_write_status_register_WREN(expected);
1590
1591 status = spi_read_status_register();
1592 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1593 if (status != expected) {
1594 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1595 expected, status);
1596 return 1;
1597 }
1598
1599 return 0;
1600}
1601
1602/* Set/clear the status regsiter write protect bit in SR1. */
1603static int generic_set_srp0(const struct flashchip *flash, int enable)
1604{
1605 uint8_t status, expected;
1606 struct generic_wp *wp;
1607 int num_entries;
1608
1609 if (generic_range_table(flash, &wp, &num_entries))
1610 return -1;
1611
1612 expected = spi_read_status_register();
1613 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1614
1615 if (enable)
1616 expected |= 1 << wp->sr1.srp_pos;
1617 else
1618 expected &= ~(1 << wp->sr1.srp_pos);
1619
1620 spi_write_status_register_WREN(expected);
1621
1622 status = spi_read_status_register();
1623 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1624 if (status != expected)
1625 return -1;
1626
1627 return 0;
1628}
1629
1630static int generic_enable_writeprotect(const struct flashchip *flash,
1631 enum wp_mode wp_mode)
1632{
1633 int ret;
1634
1635 switch (wp_mode) {
1636 case WP_MODE_HARDWARE:
1637 ret = generic_set_srp0(flash, 1);
1638 break;
1639 default:
1640 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1641 return 1;
1642 }
1643
1644 if (ret)
1645 msg_cerr("%s(): error=%d.\n", __func__, ret);
1646 return ret;
1647}
1648
1649static int generic_disable_writeprotect(const struct flashchip *flash)
1650{
1651 int ret;
1652
1653 ret = generic_set_srp0(flash, 0);
1654 if (ret)
1655 msg_cerr("%s(): error=%d.\n", __func__, ret);
1656 return ret;
1657}
1658
1659static int generic_list_ranges(const struct flashchip *flash)
1660{
1661 struct generic_wp *wp;
1662 struct generic_range *r;
1663 int i, num_entries;
1664
1665 if (generic_range_table(flash, &wp, &num_entries))
1666 return -1;
1667
1668 r = &wp->ranges[0];
1669 for (i = 0; i < num_entries; i++) {
1670 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1671 r->range.start, r->range.len);
1672 r++;
1673 }
1674
1675 return 0;
1676}
1677
1678static int generic_wp_status(const struct flashchip *flash)
1679{
1680 uint8_t sr1;
1681 unsigned int start, len;
1682 int ret = 0;
1683 struct generic_wp *wp;
1684 struct generic_range *g;
1685 int num_entries, wp_en;
1686
1687 if (generic_range_table(flash, &wp, &num_entries))
1688 return -1;
1689
1690 sr1 = spi_read_status_register();
1691 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
1692
1693 msg_cinfo("WP: status: 0x%04x\n", sr1);
1694 msg_cinfo("WP: status.srp0: %x\n", wp_en);
1695 /* FIXME: SRP1 is not really generic, but we probably should print
1696 * it anyway to have consistent output. #legacycruft */
1697 msg_cinfo("WP: status.srp1: %x\n", 0);
1698 msg_cinfo("WP: write protect is %s.\n",
1699 wp_en ? "enabled" : "disabled");
1700
1701 msg_cinfo("WP: write protect range: ");
1702 if (generic_status_to_range(flash, sr1, &start, &len)) {
1703 msg_cinfo("(cannot resolve the range)\n");
1704 ret = -1;
1705 } else {
1706 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1707 }
1708
1709 return ret;
1710}
1711
1712struct wp wp_generic = {
1713 .list_ranges = generic_list_ranges,
1714 .set_range = generic_set_range,
1715 .enable = generic_enable_writeprotect,
1716 .disable = generic_disable_writeprotect,
1717 .wp_status = generic_wp_status,
1718};