Debian Codegraph @

src/drivers/spi/winbond.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <commonlib/helpers.h>
5 #include <spi_flash.h>
6 #include <spi-generic.h>
7 #include <delay.h>
8 #include <lib.h>
9
10 #include "spi_flash_internal.h"
11 #include "spi_winbond.h"
12
13 union status_reg1 {
14 uint8_t u;
15 struct {
16 uint8_t busy : 1;
17 uint8_t wel : 1;
18 uint8_t bp : 3;
19 uint8_t tb : 1;
20 uint8_t sec : 1;
21 uint8_t srp0 : 1;
22 } bp3; /* for example: W25Q128FW */
23 struct {
24 uint8_t busy : 1;
25 uint8_t wel : 1;
26 uint8_t bp : 4;
27 uint8_t tb : 1;
28 uint8_t srp0 : 1;
29 } bp4; /* for example: W25Q256J */
30 };
31
32 union status_reg2 {
33 uint8_t u;
34 struct {
35 uint8_t srp1 : 1;
36 uint8_t qe : 1;
37 uint8_t res : 1;
38 uint8_t lb : 3;
39 uint8_t cmp : 1;
40 uint8_t sus : 1;
41 };
42 };
43
44 struct status_regs {
45 union {
46 struct {
47 #if defined(__BIG_ENDIAN)
48 union status_reg2 reg2;
49 union status_reg1 reg1;
50 #else
51 union status_reg1 reg1;
52 union status_reg2 reg2;
53 #endif
54 };
55 u16 u;
56 };
57 };
58
59 static const struct spi_flash_part_id flash_table[] = {
60 {
61 /* W25P80 */
62 .id[0] = 0x2014,
63 .nr_sectors_shift = 8,
64 },
65 {
66 /* W25P16 */
67 .id[0] = 0x2015,
68 .nr_sectors_shift = 9,
69 },
70 {
71 /* W25P32 */
72 .id[0] = 0x2016,
73 .nr_sectors_shift = 10,
74 },
75 {
76 /* W25X80 */
77 .id[0] = 0x3014,
78 .nr_sectors_shift = 8,
79 .fast_read_dual_output_support = 1,
80 },
81 {
82 /* W25X16 */
83 .id[0] = 0x3015,
84 .nr_sectors_shift = 9,
85 .fast_read_dual_output_support = 1,
86 },
87 {
88 /* W25X32 */
89 .id[0] = 0x3016,
90 .nr_sectors_shift = 10,
91 .fast_read_dual_output_support = 1,
92 },
93 {
94 /* W25X64 */
95 .id[0] = 0x3017,
96 .nr_sectors_shift = 11,
97 .fast_read_dual_output_support = 1,
98 },
99 {
100 /* W25Q80_V */
101 .id[0] = 0x4014,
102 .nr_sectors_shift = 8,
103 .fast_read_dual_output_support = 1,
104 .fast_read_dual_io_support = 1,
105 },
106 {
107 /* W25Q16_V */
108 .id[0] = 0x4015,
109 .nr_sectors_shift = 9,
110 .fast_read_dual_output_support = 1,
111 .fast_read_dual_io_support = 1,
112 .protection_granularity_shift = 16,
113 .bp_bits = 3,
114 },
115 {
116 /* W25Q16DW */
117 .id[0] = 0x6015,
118 .nr_sectors_shift = 9,
119 .fast_read_dual_output_support = 1,
120 .fast_read_dual_io_support = 1,
121 .protection_granularity_shift = 16,
122 .bp_bits = 3,
123 },
124 {
125 /* W25Q32_V */
126 .id[0] = 0x4016,
127 .nr_sectors_shift = 10,
128 .fast_read_dual_output_support = 1,
129 .fast_read_dual_io_support = 1,
130 .protection_granularity_shift = 16,
131 .bp_bits = 3,
132 },
133 {
134 /* W25Q32DW */
135 .id[0] = 0x6016,
136 .nr_sectors_shift = 10,
137 .fast_read_dual_output_support = 1,
138 .fast_read_dual_io_support = 1,
139 .protection_granularity_shift = 16,
140 .bp_bits = 3,
141 },
142 {
143 /* W25Q64_V */
144 .id[0] = 0x4017,
145 .nr_sectors_shift = 11,
146 .fast_read_dual_output_support = 1,
147 .fast_read_dual_io_support = 1,
148 .protection_granularity_shift = 17,
149 .bp_bits = 3,
150 },
151 {
152 /* W25Q64DW */
153 .id[0] = 0x6017,
154 .nr_sectors_shift = 11,
155 .fast_read_dual_output_support = 1,
156 .fast_read_dual_io_support = 1,
157 .protection_granularity_shift = 17,
158 .bp_bits = 3,
159 },
160 {
161 /* W25Q64JV */
162 .id[0] = 0x7017,
163 .nr_sectors_shift = 11,
164 .fast_read_dual_output_support = 1,
165 .fast_read_dual_io_support = 1,
166 .protection_granularity_shift = 17,
167 .bp_bits = 3,
168 },
169 {
170 /* W25Q64JW */
171 .id[0] = 0x8017,
172 .nr_sectors_shift = 11,
173 .fast_read_dual_output_support = 1,
174 .fast_read_dual_io_support = 1,
175 .protection_granularity_shift = 17,
176 .bp_bits = 3,
177 },
178 {
179 /* W25Q128_V */
180 .id[0] = 0x4018,
181 .nr_sectors_shift = 12,
182 .fast_read_dual_output_support = 1,
183 .fast_read_dual_io_support = 1,
184 .protection_granularity_shift = 18,
185 .bp_bits = 3,
186 },
187 {
188 /* W25Q128FW */
189 .id[0] = 0x6018,
190 .nr_sectors_shift = 12,
191 .fast_read_dual_output_support = 1,
192 .fast_read_dual_io_support = 1,
193 .protection_granularity_shift = 18,
194 .bp_bits = 3,
195 },
196 {
197 /* W25Q128J */
198 .id[0] = 0x7018,
199 .nr_sectors_shift = 12,
200 .fast_read_dual_output_support = 1,
201 .fast_read_dual_io_support = 1,
202 .protection_granularity_shift = 18,
203 .bp_bits = 3,
204 },
205 {
206 /* W25Q128JW */
207 .id[0] = 0x8018,
208 .nr_sectors_shift = 12,
209 .fast_read_dual_output_support = 1,
210 .fast_read_dual_io_support = 1,
211 .protection_granularity_shift = 18,
212 .bp_bits = 3,
213 },
214 {
215 /* W25Q512NW-IM */
216 .id[0] = 0x8020,
217 .nr_sectors_shift = 14,
218 .fast_read_dual_output_support = 1,
219 .fast_read_dual_io_support = 1,
220 .protection_granularity_shift = 16,
221 .bp_bits = 4,
222 },
223 {
224 /* W25Q256_V */
225 .id[0] = 0x4019,
226 .nr_sectors_shift = 13,
227 .fast_read_dual_output_support = 1,
228 .fast_read_dual_io_support = 1,
229 .protection_granularity_shift = 16,
230 .bp_bits = 4,
231 },
232 {
233 /* W25Q256J */
234 .id[0] = 0x7019,
235 .nr_sectors_shift = 13,
236 .fast_read_dual_output_support = 1,
237 .fast_read_dual_io_support = 1,
238 .protection_granularity_shift = 16,
239 .bp_bits = 4,
240 },
241 {
242 /* W25Q256JW */
243 .id[0] = 0x6019,
244 .nr_sectors_shift = 13,
245 .fast_read_dual_output_support = 1,
246 .fast_read_dual_io_support = 1,
247 .protection_granularity_shift = 16,
248 .bp_bits = 4,
249 },
250 {
251 /* W25Q256JW_DTR */
252 .id[0] = 0x8019,
253 .nr_sectors_shift = 13,
254 .fast_read_dual_output_support = 1,
255 .fast_read_dual_io_support = 1,
256 .protection_granularity_shift = 16,
257 .bp_bits = 4,
258 },
259 };
260
261 /*
262 * Convert BPx, TB and CMP to a region.
263 * SEC (if available) must be zero.
264 */
265 static void winbond_bpbits_to_region(const size_t granularity,
266 const struct spi_flash_bpbits *bits,
267 const size_t flash_size,
268 struct region *out)
269 {
270 size_t protected_size =
271 MIN(bits->bp ? granularity << (bits->bp - 1) : 0, flash_size);
272
273 int tb = bits->tb;
274 if (bits->cmp) {
275 protected_size = flash_size - protected_size;
276 tb = !tb;
277 }
278
279 *out = region_create(tb ? 0 : flash_size - protected_size, protected_size);
280 }
281
282 /*
283 * Available on all devices.
284 * Read block protect bits from Status/Status2 Reg.
285 * Converts block protection bits to a region.
286 *
287 * Returns:
288 * -1 on error
289 * 1 if region is covered by write protection
290 * 0 if a part of region isn't covered by write protection
291 */
292 static int winbond_get_write_protection(const struct spi_flash *flash,
293 const struct region *region)
294 {
295 const struct spi_flash_part_id *params;
296 struct region wp_region;
297 struct spi_flash_bpbits bpbits;
298 int ret;
299
300 params = flash->part;
301
302 if (!params)
303 return -1;
304
305 const size_t granularity = (1 << params->protection_granularity_shift);
306
307 union status_reg1 reg1 = { .u = 0 };
308 union status_reg2 reg2 = { .u = 0 };
309
310 ret = spi_flash_cmd(&flash->spi, flash->status_cmd, &reg1.u,
311 sizeof(reg1.u));
312 if (ret)
313 return ret;
314
315 ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, &reg2.u,
316 sizeof(reg2.u));
317 if (ret)
318 return ret;
319
320 if (params->bp_bits == 3) {
321 if (reg1.bp3.sec) {
322 // FIXME: not supported
323 return -1;
324 }
325
326 bpbits = (struct spi_flash_bpbits){
327 .bp = reg1.bp3.bp,
328 .cmp = reg2.cmp,
329 .tb = reg1.bp3.tb,
330 /*
331 * For W25Q*{,F}* parts:
332 * srp1 srp0
333 * 0 0 | writable if WEL==1
334 * 0 1 | writable if WEL==1 && #WP==Vcc
335 * 1 0 | not writable until next power-down
336 * 1 1 | not writable, permanently
337 *
338 * checked datasheets: W25Q128FV, (W25Q80, W25Q16,
339 * W25Q32)
340 */
341 .winbond = {
342 .srp0 = reg1.bp3.srp0,
343 .srp1 = reg2.srp1,
344 },
345 };
346 } else if (params->bp_bits == 4) {
347 bpbits = (struct spi_flash_bpbits){
348 .bp = reg1.bp4.bp,
349 .cmp = reg2.cmp,
350 .tb = reg1.bp4.tb,
351 /*
352 * For W25Q*{J,D}* parts:
353 *
354 * srp1 srp0
355 * 0 0 | writable if WEL==1
356 * 0 1 | writable if WEL==1 && #WP==Vcc
357 * 1 x | not writable until next power-down
358 *
359 * checked datasheets: W25Q132JW, W25Q128JW, W25Q256JV.
360 * W25Q16DW
361 *
362 * The srp0/srp1 bits got renamed to srp/srl in the
363 * datasheets, we retain the prior naming
364 * convention for the structs though.
365 */
366 .winbond = {
367 .srp0 = reg1.bp4.srp0,
368 .srp1 = reg2.srp1,
369 },
370 };
371 } else {
372 // FIXME: not supported
373 return -1;
374 }
375
376 winbond_bpbits_to_region(granularity, &bpbits, flash->size,
377 &wp_region);
378
379 if (!region_sz(&wp_region)) {
380 printk(BIOS_DEBUG, "WINBOND: flash isn't protected\n");
381
382 return 0;
383 }
384
385 printk(BIOS_DEBUG, "WINBOND: flash protected range 0x%08zx-0x%08zx\n",
386 region_offset(&wp_region), region_last(&wp_region));
387
388 return region_is_subregion(&wp_region, region);
389 }
390
391 /**
392 * Common method to write some bit of the status register 1 & 2 at the same
393 * time. Only change bits that are one in @mask.
394 * Compare the final result to make sure that the register isn't locked.
395 *
396 * @param mask: The bits that are affected by @val
397 * @param val: The bits to write
398 * @param non_volatile: Make setting permanent
399 *
400 * @return 0 on success
401 */
402 static int winbond_flash_cmd_status(const struct spi_flash *flash,
403 const u16 mask,
404 const u16 val,
405 const bool non_volatile)
406 {
407 struct {
408 u8 cmd;
409 u16 sreg;
410 } __packed cmdbuf;
411 u8 reg8;
412 int ret;
413
414 if (!flash)
415 return -1;
416
417 ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR, &reg8, sizeof(reg8));
418 if (ret)
419 return ret;
420
421 cmdbuf.sreg = reg8;
422
423 ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, &reg8, sizeof(reg8));
424 if (ret)
425 return ret;
426
427 cmdbuf.sreg |= reg8 << 8;
428
429 if ((val & mask) == (cmdbuf.sreg & mask))
430 return 0;
431
432 if (non_volatile) {
433 ret = spi_flash_cmd(&flash->spi, CMD_W25_WREN, NULL, 0);
434 } else {
435 ret = spi_flash_cmd(&flash->spi, CMD_VOLATILE_SREG_WREN, NULL,
436 0);
437 }
438 if (ret)
439 return ret;
440
441 cmdbuf.sreg &= ~mask;
442 cmdbuf.sreg |= val & mask;
443 cmdbuf.cmd = CMD_W25_WRSR;
444
445 /* Legacy method of writing status register 1 & 2 */
446 ret = spi_flash_cmd_write(&flash->spi, (u8 *)&cmdbuf, sizeof(cmdbuf),
447 NULL, 0);
448 if (ret)
449 return ret;
450
451 if (non_volatile) {
452 /* Wait tw */
453 ret = spi_flash_cmd_wait_ready(flash, WINBOND_FLASH_TIMEOUT);
454 if (ret)
455 return ret;
456 } else {
457 /* Wait tSHSL */
458 udelay(1);
459 }
460
461 /* Now read the status register to make sure it's not locked */
462 ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR, &reg8, sizeof(reg8));
463 if (ret)
464 return ret;
465
466 cmdbuf.sreg = reg8;
467
468 ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, &reg8, sizeof(reg8));
469 if (ret)
470 return ret;
471
472 cmdbuf.sreg |= reg8 << 8;
473
474 printk(BIOS_DEBUG, "WINBOND: SREG=%02x SREG2=%02x\n",
475 cmdbuf.sreg & 0xff,
476 cmdbuf.sreg >> 8);
477
478 /* Compare against expected result */
479 if ((val & mask) != (cmdbuf.sreg & mask)) {
480 printk(BIOS_ERR, "WINBOND: SREG is locked!\n");
481 ret = -1;
482 }
483
484 return ret;
485 }
486
487 /*
488 * Available on all devices.
489 * Protect a region starting from start of flash or end of flash.
490 * The caller must provide a supported protected region size.
491 * SEC isn't supported and set to zero.
492 * Write block protect bits to Status/Status2 Reg.
493 * Optionally lock the status register if lock_sreg is set with the provided
494 * mode.
495 *
496 * @param flash: The flash to operate on
497 * @param region: The region to write protect
498 * @param mode: Optional status register lock-down mode
499 *
500 * @return 0 on success
501 */
502 static int
503 winbond_set_write_protection(const struct spi_flash *flash,
504 const struct region *region,
505 const enum spi_flash_status_reg_lockdown mode)
506 {
507 const struct spi_flash_part_id *params;
508 struct status_regs mask, val;
509 struct region wp_region;
510 u8 cmp, bp, tb;
511 int ret;
512
513 /* Need to touch TOP or BOTTOM */
514 if (region_offset(region) != 0 && region_last(region) != flash->size - 1)
515 return -1;
516
517 params = flash->part;
518
519 if (!params)
520 return -1;
521
522 if (params->bp_bits != 3 && params->bp_bits != 4) {
523 /* FIXME: not implemented */
524 return -1;
525 }
526
527 wp_region = *region;
528
529 if (region_offset(&wp_region) == 0)
530 tb = 1;
531 else
532 tb = 0;
533
534 if (region_sz(&wp_region) > flash->size / 2) {
535 cmp = 1;
536 wp_region = region_create(tb ? 0 : region_sz(&wp_region),
537 flash->size - region_sz(&wp_region));
538 tb = !tb;
539 } else {
540 cmp = 0;
541 }
542
543 if (region_sz(&wp_region) == 0) {
544 bp = 0;
545 } else if (IS_POWER_OF_2(region_sz(&wp_region)) &&
546 (region_sz(&wp_region) >=
547 (1 << params->protection_granularity_shift))) {
548 bp = log2(region_sz(&wp_region)) -
549 params->protection_granularity_shift + 1;
550 } else {
551 printk(BIOS_ERR, "WINBOND: ERROR: unsupported region size\n");
552 return -1;
553 }
554
555 /* Write block protection bits */
556
557 if (params->bp_bits == 3) {
558 val.reg1 = (union status_reg1) {
559 .bp3 = { .bp = bp, .tb = tb, .sec = 0 }
560 };
561 mask.reg1 = (union status_reg1) {
562 .bp3 = { .bp = ~0, .tb = 1, .sec = 1 }
563 };
564 } else {
565 val.reg1 = (union status_reg1) {
566 .bp4 = { .bp = bp, .tb = tb }
567 };
568 mask.reg1 = (union status_reg1) {
569 .bp4 = { .bp = ~0, .tb = 1 }
570 };
571 }
572
573 val.reg2 = (union status_reg2) { .cmp = cmp };
574 mask.reg2 = (union status_reg2) { .cmp = 1 };
575
576 if (mode != SPI_WRITE_PROTECTION_PRESERVE) {
577 u8 srp;
578 switch (mode) {
579 case SPI_WRITE_PROTECTION_NONE:
580 srp = 0;
581 break;
582 case SPI_WRITE_PROTECTION_PIN:
583 srp = 1;
584 break;
585 case SPI_WRITE_PROTECTION_REBOOT:
586 srp = 2;
587 break;
588 case SPI_WRITE_PROTECTION_PERMANENT:
589 srp = 3;
590 break;
591 default:
592 return -1;
593 }
594
595 if (params->bp_bits == 3) {
596 val.reg1.bp3.srp0 = !!(srp & 1);
597 mask.reg1.bp3.srp0 = 1;
598 } else {
599 val.reg1.bp4.srp0 = !!(srp & 1);
600 mask.reg1.bp4.srp0 = 1;
601 }
602
603 val.reg2.srp1 = !!(srp & 2);
604 mask.reg2.srp1 = 1;
605 }
606
607 ret = winbond_flash_cmd_status(flash, mask.u, val.u, true);
608 if (ret)
609 return ret;
610
611 printk(BIOS_DEBUG, "WINBOND: write-protection set to range "
612 "0x%08zx-0x%08zx\n", region_offset(region), region_last(region));
613
614 return ret;
615 }
616
617 static const struct spi_flash_protection_ops spi_flash_protection_ops = {
618 .get_write = winbond_get_write_protection,
619 .set_write = winbond_set_write_protection,
620 };
621
622 const struct spi_flash_vendor_info spi_flash_winbond_vi = {
623 .id = VENDOR_ID_WINBOND,
624 .page_size_shift = 8,
625 .sector_size_kib_shift = 2,
626 .match_id_mask[0] = 0xffff,
627 .ids = flash_table,
628 .nr_part_ids = ARRAY_SIZE(flash_table),
629 .desc = &spi_flash_pp_0x20_sector_desc,
630 .prot_ops = &spi_flash_protection_ops,
631 };