blob: d8b5e49665e1b813abc2faa977bd9ba35451c86b [file] [log] [blame]
David Hendricksf7924d12010-06-10 21:26:44 -07001#include <stdlib.h>
2#include <string.h>
3
4#include "flash.h"
5#include "flashchips.h"
6#include "chipdrivers.h"
7
8/*
9 * The following procedures rely on look-up tables to match the user-specified
10 * range with the chip's supported ranges. This turned out to be the most
11 * elegant approach since diferent flash chips use different levels of
12 * granularity and methods to determine protected ranges. In other words,
13 * be stupid and simple since clever arithmetic will not for many chips.
14 */
15
16struct wp_range {
17 unsigned int start; /* starting address */
18 unsigned int len; /* len */
19};
20
21enum bit_state {
22 OFF = 0,
23 ON = 1,
24 X = 0 /* don't care */
25};
26
27struct w25q_range {
28 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
29 enum bit_state tb; /* top/bottom select */
30 unsigned short int bp : 3; /* block protect bitfield */
31 struct wp_range range;
32};
33
34static struct w25q_range w25q16_ranges[] = {
35 { X, X, 0, {0, 0} }, /* none */
36 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
37 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
38 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
39 { 0, 0, 0x4, {0x180000, 512 * 1024} },
40 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
41
42 { 0, 1, 0x1, {0x000000, 64 * 1024} },
43 { 0, 1, 0x2, {0x000000, 128 * 1024} },
44 { 0, 1, 0x3, {0x000000, 256 * 1024} },
45 { 0, 1, 0x4, {0x000000, 512 * 1024} },
46 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
47 { X, X, 0x6, {0x000000, 2048 * 1024} },
48 { X, X, 0x7, {0x000000, 2048 * 1024} },
49
50 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
51 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
52 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
53 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
54 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
55
56 { 1, 1, 0x1, {0x000000, 4 * 1024} },
57 { 1, 1, 0x2, {0x000000, 8 * 1024} },
58 { 1, 1, 0x3, {0x000000, 16 * 1024} },
59 { 1, 1, 0x4, {0x000000, 32 * 1024} },
60 { 1, 1, 0x5, {0x000000, 32 * 1024} },
61};
62
63static struct w25q_range w25q32_ranges[] = {
64 { X, X, 0, {0, 0} }, /* none */
65 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
66 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
67 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
68 { 0, 0, 0x4, {0x380000, 512 * 1024} },
69 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -070070 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -070071
72 { 0, 1, 0x1, {0x000000, 64 * 1024} },
73 { 0, 1, 0x2, {0x000000, 128 * 1024} },
74 { 0, 1, 0x3, {0x000000, 256 * 1024} },
75 { 0, 1, 0x4, {0x000000, 512 * 1024} },
76 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
77 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
78 { X, X, 0x7, {0x000000, 4096 * 1024} },
79
80 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
81 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
82 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
83 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
84 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
85
86 { 1, 1, 0x1, {0x000000, 4 * 1024} },
87 { 1, 1, 0x2, {0x000000, 8 * 1024} },
88 { 1, 1, 0x3, {0x000000, 16 * 1024} },
89 { 1, 1, 0x4, {0x000000, 32 * 1024} },
90 { 1, 1, 0x5, {0x000000, 32 * 1024} },
91};
92
93static struct w25q_range w25q80_ranges[] = {
94 { X, X, 0, {0, 0} }, /* none */
95 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
96 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
97 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
98 { 0, 0, 0x4, {0x080000, 512 * 1024} },
99
100 { 0, 1, 0x1, {0x000000, 64 * 1024} },
101 { 0, 1, 0x2, {0x000000, 128 * 1024} },
102 { 0, 1, 0x3, {0x000000, 256 * 1024} },
103 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700104 { X, X, 0x6, {0x000000, 1024 * 1024} },
105 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700106
107 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
108 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
109 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
110 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
111 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
112
113 { 1, 1, 0x1, {0x000000, 4 * 1024} },
114 { 1, 1, 0x2, {0x000000, 8 * 1024} },
115 { 1, 1, 0x3, {0x000000, 16 * 1024} },
116 { 1, 1, 0x4, {0x000000, 32 * 1024} },
117 { 1, 1, 0x5, {0x000000, 32 * 1024} },
118};
119
David Hendricks2c4a76c2010-06-28 14:00:43 -0700120static struct w25q_range w25q64_ranges[] = {
121 { X, X, 0, {0, 0} }, /* none */
122
123 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
124 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
125 { 0, 0, 0x3, {0x780000, 512 * 1024} },
126 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
127 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
128 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
129
130 { 0, 1, 0x1, {0x000000, 128 * 1024} },
131 { 0, 1, 0x2, {0x000000, 256 * 1024} },
132 { 0, 1, 0x3, {0x000000, 512 * 1024} },
133 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
134 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
135 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
136 { X, X, 0x7, {0x000000, 8192 * 1024} },
137
138 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
139 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
140 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
141 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
142 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
143
144 { 1, 1, 0x1, {0x000000, 4 * 1024} },
145 { 1, 1, 0x2, {0x000000, 8 * 1024} },
146 { 1, 1, 0x3, {0x000000, 16 * 1024} },
147 { 1, 1, 0x4, {0x000000, 32 * 1024} },
148 { 1, 1, 0x5, {0x000000, 32 * 1024} },
149};
150
David Hendricksf7924d12010-06-10 21:26:44 -0700151struct w25q_status {
152 /* this maps to register layout -- do not change ordering */
153 unsigned char busy : 1;
154 unsigned char wel : 1;
155 unsigned char bp0 : 1;
156 unsigned char bp1 : 1;
157 unsigned char bp2 : 1;
158 unsigned char tb : 1;
159 unsigned char sec : 1;
160 unsigned char srp0 : 1;
161 /* FIXME: what about the second status register? */
162// unsigned char srp1 : 1;
163// unsigned char qe : 1;
164} __attribute__ ((packed));
165
166static int w25_set_range(struct flashchip *flash,
167 unsigned int start, unsigned int len)
168{
169 struct w25q_status status;
170 struct w25q_range *w25q_ranges;
171 int i, num_entries = 0;
172 int tmp = 0, range_found = 0;
173
174 if (flash->manufacture_id != WINBOND_NEX_ID)
175 return -1;
176 switch(flash->model_id) {
177 case W_25Q80:
178 w25q_ranges = w25q80_ranges;
179 num_entries = ARRAY_SIZE(w25q80_ranges);
180 break;
181 case W_25Q16:
182 w25q_ranges = w25q16_ranges;
183 num_entries = ARRAY_SIZE(w25q16_ranges);
184 break;
185 case W_25Q32:
186 w25q_ranges = w25q32_ranges;
187 num_entries = ARRAY_SIZE(w25q32_ranges);
188 break;
David Hendricks2c4a76c2010-06-28 14:00:43 -0700189 case W_25Q64:
190 w25q_ranges = w25q64_ranges;
191 num_entries = ARRAY_SIZE(w25q64_ranges);
192 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700193 default:
194 msg_cerr("%s: flash chip mismatch, aborting\n", __func__);
195 return -1;
196 }
197
198 memset(&status, 0, sizeof(status));
199 tmp = spi_read_status_register();
200 memcpy(&status, &tmp, 1);
201 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
202
203 for (i = 0; i < num_entries; i++) {
204 struct wp_range *r = &w25q_ranges[i].range;
205
206 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
207 start, len, r->start, r->len);
208 if ((start == r->start) && (len == r->len)) {
209 status.bp0 = w25q_ranges[i].bp & 1;
210 status.bp1 = w25q_ranges[i].bp >> 1;
211 status.bp2 = w25q_ranges[i].bp >> 2;
212 status.tb = w25q_ranges[i].tb;
213 status.sec = w25q_ranges[i].sec;
214
215 range_found = 1;
216 break;
217 }
218 }
219
220 if (!range_found) {
221 msg_cerr("matching range not found\n");
222 return -1;
223 }
224
225 msg_cdbg("status.busy: %x\n", status.busy);
226 msg_cdbg("status.wel: %x\n", status.wel);
227 msg_cdbg("status.bp0: %x\n", status.bp0);
228 msg_cdbg("status.bp1: %x\n", status.bp1);
229 msg_cdbg("status.bp2: %x\n", status.bp2);
230 msg_cdbg("status.tb: %x\n", status.tb);
231 msg_cdbg("status.sec: %x\n", status.sec);
232 msg_cdbg("status.srp0: %x\n", status.srp0);
233
234 memcpy(&tmp, &status, sizeof(status));
235 spi_write_status_enable();
David Hendricks82fd8ae2010-08-04 14:34:54 -0700236 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700237 msg_cdbg("%s: new status: 0x%02x\n",
238 __func__, spi_read_status_register());
239
240 return 0;
241}
242
243static int w25_enable_writeprotect(struct flashchip *flash)
244{
245 struct w25q_status status;
246 int tmp = 0;
247
248 memset(&status, 0, sizeof(status));
249 tmp = spi_read_status_register();
250 memcpy(&status, &tmp, 1);
251 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
252
253 status.srp0 = 1;
254 memcpy(&tmp, &status, sizeof(status));
255 spi_write_status_enable();
David Hendricks82fd8ae2010-08-04 14:34:54 -0700256 spi_write_status_register(flash, tmp);
David Hendricksf7924d12010-06-10 21:26:44 -0700257 msg_cdbg("%s: new status: 0x%02x\n",
258 __func__, spi_read_status_register());
259
260 return 0;
261}
262
263struct wp wp_w25 = {
264 .set_range = w25_set_range,
265 .enable = w25_enable_writeprotect,
266};