Debian Codegraph @

src/drivers/spi/spi_flash.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <assert.h>
4 #include <boot/coreboot_tables.h>
5 #include <commonlib/region.h>
6 #include <console/console.h>
7 #include <string.h>
8 #include <spi-generic.h>
9 #include <spi_flash.h>
10 #include <timer.h>
11 #include <types.h>
12
13 #include "spi_flash_internal.h"
14
15 #if CONFIG(SPI_FLASH_FORCE_4_BYTE_ADDR_MODE)
16 #define ADDR_MOD 1
17 #else
18 #define ADDR_MOD 0
19 #endif
20
21 #define SPI_FIRST_STAGE \
22 (ENV_INITIAL_STAGE || CONFIG(BOOT_DEVICE_MEMORY_MAPPED))
23
24 static void spi_flash_addr(u32 addr, u8 *cmd)
25 {
26 /* cmd[0] is actual command */
27 if (CONFIG(SPI_FLASH_FORCE_4_BYTE_ADDR_MODE)) {
28 cmd[1] = addr >> 24;
29 cmd[2] = addr >> 16;
30 cmd[3] = addr >> 8;
31 cmd[4] = addr >> 0;
32 } else {
33 cmd[1] = addr >> 16;
34 cmd[2] = addr >> 8;
35 cmd[3] = addr >> 0;
36 }
37 }
38
39 static int do_spi_flash_cmd(const struct spi_slave *spi, const u8 *dout,
40 size_t bytes_out, void *din, size_t bytes_in)
41 {
42 int ret;
43 /*
44 * SPI flash requires command-response kind of behavior. Thus, two
45 * separate SPI vectors are required -- first to transmit dout and other
46 * to receive in din. If some specialized SPI flash controllers
47 * (e.g. x86) can perform both command and response together, it should
48 * be handled at SPI flash controller driver level.
49 */
50 struct spi_op vectors[] = {
51 [0] = { .dout = dout, .bytesout = bytes_out,
52 .din = NULL, .bytesin = 0, },
53 [1] = { .dout = NULL, .bytesout = 0,
54 .din = din, .bytesin = bytes_in },
55 };
56 size_t count = ARRAY_SIZE(vectors);
57 if (!bytes_in)
58 count = 1;
59
60 ret = spi_claim_bus(spi);
61 if (ret)
62 return ret;
63
64 ret = spi_xfer_vector(spi, vectors, count);
65
66 spi_release_bus(spi);
67 return ret;
68 }
69
70 static int do_dual_output_cmd(const struct spi_slave *spi, const u8 *dout,
71 size_t bytes_out, void *din, size_t bytes_in)
72 {
73 int ret;
74
75 /*
76 * spi_xfer_vector() will automatically fall back to .xfer() if
77 * .xfer_vector() is unimplemented. So using vector API here is more
78 * flexible, even though a controller that implements .xfer_vector()
79 * and (the non-vector based) .xfer_dual() but not .xfer() would be
80 * pretty odd.
81 */
82 struct spi_op vector = { .dout = dout, .bytesout = bytes_out,
83 .din = NULL, .bytesin = 0 };
84
85 ret = spi_claim_bus(spi);
86 if (ret)
87 return ret;
88
89 ret = spi_xfer_vector(spi, &vector, 1);
90
91 if (!ret)
92 ret = spi->ctrlr->xfer_dual(spi, NULL, 0, din, bytes_in);
93
94 spi_release_bus(spi);
95 return ret;
96 }
97
98 static int do_dual_io_cmd(const struct spi_slave *spi, const u8 *dout,
99 size_t bytes_out, void *din, size_t bytes_in)
100 {
101 int ret;
102
103 /* Only the very first byte (opcode) is transferred in "single" mode. */
104 struct spi_op vector = { .dout = dout, .bytesout = 1,
105 .din = NULL, .bytesin = 0 };
106
107 ret = spi_claim_bus(spi);
108 if (ret)
109 return ret;
110
111 ret = spi_xfer_vector(spi, &vector, 1);
112
113 if (!ret)
114 ret = spi->ctrlr->xfer_dual(spi, &dout[1], bytes_out - 1, NULL, 0);
115
116 if (!ret)
117 ret = spi->ctrlr->xfer_dual(spi, NULL, 0, din, bytes_in);
118
119 spi_release_bus(spi);
120 return ret;
121 }
122
123 int spi_flash_cmd(const struct spi_slave *spi, u8 cmd, void *response, size_t len)
124 {
125 int ret = do_spi_flash_cmd(spi, &cmd, sizeof(cmd), response, len);
126 if (ret)
127 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
128
129 return ret;
130 }
131
132 int spi_flash_cmd_multi(const struct spi_slave *spi, const u8 *dout, size_t bytes_out,
133 void *din, size_t bytes_in)
134 {
135 int ret = do_spi_flash_cmd(spi, dout, bytes_out, din, bytes_in);
136 if (ret)
137 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", dout[0], ret);
138
139 return ret;
140 }
141
142 /* TODO: This code is quite possibly broken and overflowing stacks. Fix ASAP! */
143 #pragma GCC diagnostic push
144 #if defined(__GNUC__) && !defined(__clang__)
145 #pragma GCC diagnostic ignored "-Wstack-usage="
146 #endif
147 #pragma GCC diagnostic ignored "-Wvla"
148 int spi_flash_cmd_write(const struct spi_slave *spi, const u8 *cmd,
149 size_t cmd_len, const void *data, size_t data_len)
150 {
151 int ret;
152 u8 buff[cmd_len + data_len];
153 memcpy(buff, cmd, cmd_len);
154 memcpy(buff + cmd_len, data, data_len);
155
156 ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0);
157 if (ret) {
158 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
159 data_len, ret);
160 }
161
162 return ret;
163 }
164 #pragma GCC diagnostic pop
165
166 /* Perform the read operation honoring spi controller fifo size, reissuing
167 * the read command until the full request completed. */
168 int spi_flash_cmd_read(const struct spi_flash *flash, u32 offset,
169 size_t len, void *buf)
170 {
171 u8 cmd[5 + ADDR_MOD];
172 int ret, cmd_len;
173 int (*do_cmd)(const struct spi_slave *spi, const u8 *din,
174 size_t in_bytes, void *out, size_t out_bytes);
175
176 if (CONFIG(SPI_FLASH_NO_FAST_READ)) {
177 cmd_len = 4 + ADDR_MOD;
178 cmd[0] = CMD_READ_ARRAY_SLOW;
179 do_cmd = do_spi_flash_cmd;
180 } else if (flash->flags.dual_io && flash->spi.ctrlr->xfer_dual) {
181 cmd_len = 5 + ADDR_MOD;
182 cmd[0] = CMD_READ_FAST_DUAL_IO;
183 cmd[4 + ADDR_MOD] = 0;
184 do_cmd = do_dual_io_cmd;
185 } else if (flash->flags.dual_output && flash->spi.ctrlr->xfer_dual) {
186 cmd_len = 5 + ADDR_MOD;
187 cmd[0] = CMD_READ_FAST_DUAL_OUTPUT;
188 cmd[4 + ADDR_MOD] = 0;
189 do_cmd = do_dual_output_cmd;
190 } else {
191 cmd_len = 5 + ADDR_MOD;
192 cmd[0] = CMD_READ_ARRAY_FAST;
193 cmd[4 + ADDR_MOD] = 0;
194 do_cmd = do_spi_flash_cmd;
195 }
196
197 uint8_t *data = buf;
198 while (len) {
199 size_t xfer_len = spi_crop_chunk(&flash->spi, cmd_len, len);
200 spi_flash_addr(offset, cmd);
201 ret = do_cmd(&flash->spi, cmd, cmd_len, data, xfer_len);
202 if (ret) {
203 printk(BIOS_WARNING,
204 "SF: Failed to send read command %#.2x(%#x, %#zx): %d\n",
205 cmd[0], offset, xfer_len, ret);
206 return ret;
207 }
208 offset += xfer_len;
209 data += xfer_len;
210 len -= xfer_len;
211 }
212
213 return 0;
214 }
215
216 int spi_flash_cmd_poll_bit(const struct spi_flash *flash, unsigned long timeout,
217 u8 cmd, u8 poll_bit)
218 {
219 const struct spi_slave *spi = &flash->spi;
220 int ret;
221 int attempt = 0;
222 u8 status;
223 struct stopwatch sw;
224
225 stopwatch_init_msecs_expire(&sw, timeout);
226 do {
227 attempt++;
228
229 ret = do_spi_flash_cmd(spi, &cmd, 1, &status, 1);
230 if (ret) {
231 printk(BIOS_WARNING,
232 "SF: SPI command failed on attempt %d with rc %d\n", attempt,
233 ret);
234 return -1;
235 }
236
237 if ((status & poll_bit) == 0)
238 return 0;
239 } while (!stopwatch_expired(&sw));
240
241 printk(BIOS_WARNING, "SF: timeout at %lld msec after %d attempts\n",
242 stopwatch_duration_msecs(&sw), attempt);
243
244 return -1;
245 }
246
247 int spi_flash_cmd_wait_ready(const struct spi_flash *flash,
248 unsigned long timeout)
249 {
250 return spi_flash_cmd_poll_bit(flash, timeout,
251 CMD_READ_STATUS, STATUS_WIP);
252 }
253
254 int spi_flash_cmd_erase(const struct spi_flash *flash, u32 offset, size_t len)
255 {
256 u32 start, end, erase_size;
257 int ret = -1;
258 u8 cmd[4 + ADDR_MOD];
259
260 erase_size = flash->sector_size;
261 if (offset % erase_size || len % erase_size) {
262 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
263 return -1;
264 }
265 if (len == 0) {
266 printk(BIOS_WARNING, "SF: Erase length cannot be 0\n");
267 return -1;
268 }
269
270 cmd[0] = flash->erase_cmd;
271 start = offset;
272 end = start + len;
273
274 while (offset < end) {
275 spi_flash_addr(offset, cmd);
276 offset += erase_size;
277
278 if (CONFIG(DEBUG_SPI_FLASH)) {
279 if (ADDR_MOD)
280 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x %2x (%x)\n",
281 cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], offset);
282 else
283 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n",
284 cmd[0], cmd[1], cmd[2], cmd[3], offset);
285 }
286
287 ret = spi_flash_cmd(&flash->spi, CMD_WRITE_ENABLE, NULL, 0);
288 if (ret)
289 goto out;
290
291 ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd), NULL, 0);
292 if (ret)
293 goto out;
294
295 ret = spi_flash_cmd_wait_ready(flash,
296 SPI_FLASH_PAGE_ERASE_TIMEOUT_MS);
297 if (ret)
298 goto out;
299 }
300
301 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
302
303 out:
304 return ret;
305 }
306
307 int spi_flash_cmd_status(const struct spi_flash *flash, u8 *reg)
308 {
309 return spi_flash_cmd(&flash->spi, flash->status_cmd, reg, sizeof(*reg));
310 }
311
312 int spi_flash_cmd_write_page_program(const struct spi_flash *flash, u32 offset,
313 size_t len, const void *buf)
314 {
315 unsigned long byte_addr;
316 unsigned long page_size;
317 size_t chunk_len;
318 size_t actual;
319 int ret = 0;
320 u8 cmd[4 + ADDR_MOD];
321
322 page_size = flash->page_size;
323 cmd[0] = flash->pp_cmd;
324
325 for (actual = 0; actual < len; actual += chunk_len) {
326 byte_addr = offset % page_size;
327 chunk_len = MIN(len - actual, page_size - byte_addr);
328 chunk_len = spi_crop_chunk(&flash->spi, sizeof(cmd), chunk_len);
329
330 spi_flash_addr(offset, cmd);
331 if (CONFIG(DEBUG_SPI_FLASH)) {
332 if (ADDR_MOD)
333 printk(BIOS_SPEW,
334 "PP: %p => cmd = { 0x%02x 0x%02x%02x%02x%02x } chunk_len = %zu\n",
335 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4],
336 chunk_len);
337 else
338 printk(BIOS_SPEW,
339 "PP: %p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
340 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3],
341 chunk_len);
342 }
343
344 ret = spi_flash_cmd(&flash->spi, flash->wren_cmd, NULL, 0);
345 if (ret < 0) {
346 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
347 goto out;
348 }
349
350 ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd),
351 buf + actual, chunk_len);
352 if (ret < 0) {
353 printk(BIOS_WARNING, "SF: Page Program failed\n");
354 goto out;
355 }
356
357 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT_MS);
358 if (ret)
359 goto out;
360
361 offset += chunk_len;
362 }
363
364 if (CONFIG(DEBUG_SPI_FLASH))
365 printk(BIOS_SPEW, "SF: : Successfully programmed %zu bytes @ 0x%lx\n",
366 len, (unsigned long)(offset - len));
367 ret = 0;
368
369 out:
370 return ret;
371 }
372
373 static const struct spi_flash_vendor_info *spi_flash_vendors[] = {
374 #if CONFIG(SPI_FLASH_ADESTO)
375 &spi_flash_adesto_vi,
376 #endif
377 #if CONFIG(SPI_FLASH_AMIC)
378 &spi_flash_amic_vi,
379 #endif
380 #if CONFIG(SPI_FLASH_ATMEL)
381 &spi_flash_atmel_vi,
382 #endif
383 #if CONFIG(SPI_FLASH_EON)
384 &spi_flash_eon_vi,
385 #endif
386 #if CONFIG(SPI_FLASH_GIGADEVICE)
387 &spi_flash_gigadevice_vi,
388 #endif
389 #if CONFIG(SPI_FLASH_MACRONIX)
390 &spi_flash_macronix_vi,
391 #endif
392 #if CONFIG(SPI_FLASH_SPANSION)
393 &spi_flash_spansion_ext1_vi,
394 &spi_flash_spansion_ext2_vi,
395 &spi_flash_spansion_vi,
396 #endif
397 #if CONFIG(SPI_FLASH_SST)
398 &spi_flash_sst_ai_vi,
399 &spi_flash_sst_vi,
400 #endif
401 #if CONFIG(SPI_FLASH_STMICRO)
402 &spi_flash_stmicro1_vi,
403 &spi_flash_stmicro2_vi,
404 &spi_flash_stmicro3_vi,
405 &spi_flash_stmicro4_vi,
406 #endif
407 #if CONFIG(SPI_FLASH_WINBOND)
408 &spi_flash_winbond_vi,
409 #endif
410 #if CONFIG(SPI_FLASH_ISSI)
411 &spi_flash_issi_vi,
412 #endif
413 };
414 #define IDCODE_LEN 5
415
416 static int fill_spi_flash(const struct spi_slave *spi, struct spi_flash *flash,
417 const struct spi_flash_vendor_info *vi,
418 const struct spi_flash_part_id *part)
419 {
420 memcpy(&flash->spi, spi, sizeof(*spi));
421 flash->vendor = vi->id;
422 flash->model = part->id[0];
423
424 flash->page_size = 1U << vi->page_size_shift;
425 flash->sector_size = (1U << vi->sector_size_kib_shift) * KiB;
426 flash->size = flash->sector_size * (1U << part->nr_sectors_shift);
427 flash->erase_cmd = vi->desc->erase_cmd;
428 flash->status_cmd = vi->desc->status_cmd;
429 flash->pp_cmd = vi->desc->pp_cmd;
430 flash->wren_cmd = vi->desc->wren_cmd;
431
432 flash->flags.dual_output = part->fast_read_dual_output_support;
433 flash->flags.dual_io = part->fast_read_dual_io_support;
434
435 flash->ops = &vi->desc->ops;
436 flash->prot_ops = vi->prot_ops;
437 flash->part = part;
438
439 if (vi->after_probe)
440 return vi->after_probe(flash);
441
442 return 0;
443 }
444
445 static const struct spi_flash_part_id *find_part(const struct spi_flash_vendor_info *vi,
446 uint16_t id[2])
447 {
448 size_t i;
449 const uint16_t lid[2] = {
450 [0] = id[0] & vi->match_id_mask[0],
451 [1] = id[1] & vi->match_id_mask[1],
452 };
453
454 for (i = 0; i < vi->nr_part_ids; i++) {
455 const struct spi_flash_part_id *part = &vi->ids[i];
456
457 if (part->id[0] == lid[0] && part->id[1] == lid[1])
458 return part;
459 }
460
461 return NULL;
462 }
463
464 static int find_match(const struct spi_slave *spi, struct spi_flash *flash,
465 uint8_t manuf_id, uint16_t id[2])
466 {
467 int i;
468
469 for (i = 0; i < (int)ARRAY_SIZE(spi_flash_vendors); i++) {
470 const struct spi_flash_vendor_info *vi;
471 const struct spi_flash_part_id *part;
472
473 vi = spi_flash_vendors[i];
474
475 if (manuf_id != vi->id)
476 continue;
477
478 part = find_part(vi, id);
479
480 if (part == NULL)
481 continue;
482
483 return fill_spi_flash(spi, flash, vi, part);
484 }
485
486 printk(BIOS_WARNING, "SF: no match for ID %04x %04x\n", id[0], id[1]);
487 return -1;
488 }
489
490 int spi_flash_generic_probe(const struct spi_slave *spi,
491 struct spi_flash *flash)
492 {
493 int ret, i;
494 u8 idcode[IDCODE_LEN];
495 u8 manuf_id;
496 u16 id[2];
497
498 /* Read the ID codes */
499 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
500 if (ret)
501 return -1;
502
503 if (CONFIG(DEBUG_SPI_FLASH)) {
504 printk(BIOS_SPEW, "SF: Got idcode: ");
505 for (i = 0; i < sizeof(idcode); i++)
506 printk(BIOS_SPEW, "%02x ", idcode[i]);
507 printk(BIOS_SPEW, "\n");
508 }
509
510 manuf_id = idcode[0];
511
512 printk(BIOS_INFO, "Manufacturer: %02x\n", manuf_id);
513
514 /* If no result from RDID command and STMicro parts are enabled attempt
515 to wake the part from deep sleep and obtain alternative id info. */
516 if (CONFIG(SPI_FLASH_STMICRO) && manuf_id == 0xff) {
517 if (stmicro_release_deep_sleep_identify(spi, idcode))
518 return -1;
519 manuf_id = idcode[0];
520 }
521
522 id[0] = (idcode[1] << 8) | idcode[2];
523 id[1] = (idcode[3] << 8) | idcode[4];
524
525 return find_match(spi, flash, manuf_id, id);
526 }
527
528 int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash)
529 {
530 struct spi_slave spi;
531 int ret = -1;
532
533 if (spi_setup_slave(bus, cs, &spi)) {
534 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
535 return -1;
536 }
537
538 /* Try special programmer probe if any. */
539 if (spi.ctrlr->flash_probe)
540 ret = spi.ctrlr->flash_probe(&spi, flash);
541
542 /* If flash is not found, try generic spi flash probe. */
543 if (ret)
544 ret = spi_flash_generic_probe(&spi, flash);
545
546 /* Give up -- nothing more to try if flash is not found. */
547 if (ret) {
548 printk(BIOS_WARNING, "SF: Unsupported manufacturer!\n");
549 return -1;
550 }
551
552 const char *mode_string = "";
553 if (flash->flags.dual_io && spi.ctrlr->xfer_dual)
554 mode_string = " (Dual I/O mode)";
555 else if (flash->flags.dual_output && spi.ctrlr->xfer_dual)
556 mode_string = " (Dual Output mode)";
557 printk(BIOS_INFO,
558 "SF: Detected %02x %04x with sector size 0x%x, total 0x%x%s\n",
559 flash->vendor, flash->model, flash->sector_size, flash->size, mode_string);
560 if (bus == CONFIG_BOOT_DEVICE_SPI_FLASH_BUS
561 && flash->size != CONFIG_ROM_SIZE) {
562 printk(BIOS_ERR, "SF size 0x%x does not correspond to"
563 " CONFIG_ROM_SIZE 0x%x!!\n", flash->size,
564 CONFIG_ROM_SIZE);
565 }
566
567 if (CONFIG(SPI_FLASH_FORCE_4_BYTE_ADDR_MODE) && SPI_FIRST_STAGE) {
568 printk(BIOS_DEBUG, "SF: Entering 4-byte addressing mode\n");
569 spi_flash_cmd(&flash->spi, CMD_ENTER_4BYTE_ADDR_MODE, NULL, 0);
570 }
571
572 if (CONFIG(SPI_FLASH_EXIT_4_BYTE_ADDR_MODE) && SPI_FIRST_STAGE) {
573 printk(BIOS_DEBUG, "SF: Exiting 4-byte addressing mode\n");
574 spi_flash_cmd(&flash->spi, CMD_EXIT_4BYTE_ADDR_MODE, NULL, 0);
575 }
576
577 /* TODO: only do this in stages that will need to call those functions? */
578 if (CONFIG(SPI_FLASH_RPMC)) {
579 spi_flash_fill_rpmc_caps(flash);
580 }
581
582 return 0;
583 }
584
585 int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len,
586 void *buf)
587 {
588 return flash->ops->read(flash, offset, len, buf);
589 }
590
591 int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len,
592 const void *buf)
593 {
594 int ret;
595
596 if (spi_flash_volatile_group_begin(flash))
597 return -1;
598
599 ret = flash->ops->write(flash, offset, len, buf);
600
601 if (spi_flash_volatile_group_end(flash))
602 return -1;
603
604 return ret;
605 }
606
607 int spi_flash_erase(const struct spi_flash *flash, u32 offset, size_t len)
608 {
609 int ret;
610
611 if (spi_flash_volatile_group_begin(flash))
612 return -1;
613
614 ret = flash->ops->erase(flash, offset, len);
615
616 if (spi_flash_volatile_group_end(flash))
617 return -1;
618
619 return ret;
620 }
621
622 int spi_flash_status(const struct spi_flash *flash, u8 *reg)
623 {
624 if (flash->ops->status)
625 return flash->ops->status(flash, reg);
626
627 return -1;
628 }
629
630 int spi_flash_is_write_protected(const struct spi_flash *flash,
631 const struct region *region)
632 {
633 struct region flash_region;
634
635 if (!flash || !region)
636 return -1;
637
638 flash_region = region_create(0, flash->size);
639
640 if (!region_is_subregion(&flash_region, region))
641 return -1;
642
643 if (!flash->prot_ops) {
644 printk(BIOS_WARNING, "SPI: Write-protection gathering not "
645 "implemented for this vendor.\n");
646 return -1;
647 }
648
649 return flash->prot_ops->get_write(flash, region);
650 }
651
652 int spi_flash_set_write_protected(const struct spi_flash *flash,
653 const struct region *region,
654 const enum spi_flash_status_reg_lockdown mode)
655 {
656 struct region flash_region;
657 int ret;
658
659 if (!flash)
660 return -1;
661
662 flash_region = region_create(0, flash->size);
663
664 if (!region_is_subregion(&flash_region, region))
665 return -1;
666
667 if (!flash->prot_ops) {
668 printk(BIOS_WARNING, "SPI: Setting write-protection is not "
669 "implemented for this vendor.\n");
670 return -1;
671 }
672
673 ret = flash->prot_ops->set_write(flash, region, mode);
674
675 if (ret == 0 && mode != SPI_WRITE_PROTECTION_PRESERVE) {
676 printk(BIOS_INFO, "SPI: SREG lock-down was set to ");
677 switch (mode) {
678 case SPI_WRITE_PROTECTION_NONE:
679 printk(BIOS_INFO, "NEVER\n");
680 break;
681 case SPI_WRITE_PROTECTION_PIN:
682 printk(BIOS_INFO, "WP\n");
683 break;
684 case SPI_WRITE_PROTECTION_REBOOT:
685 printk(BIOS_INFO, "REBOOT\n");
686 break;
687 case SPI_WRITE_PROTECTION_PERMANENT:
688 printk(BIOS_INFO, "PERMANENT\n");
689 break;
690 default:
691 printk(BIOS_INFO, "UNKNOWN\n");
692 break;
693 }
694 }
695
696 return ret;
697 }
698
699 static uint32_t volatile_group_count;
700
701 int spi_flash_volatile_group_begin(const struct spi_flash *flash)
702 {
703 uint32_t count;
704 int ret = 0;
705
706 if (!CONFIG(SPI_FLASH_HAS_VOLATILE_GROUP))
707 return ret;
708
709 count = volatile_group_count;
710 if (count == 0)
711 ret = chipset_volatile_group_begin(flash);
712
713 count++;
714 volatile_group_count = count;
715 return ret;
716 }
717
718 int spi_flash_volatile_group_end(const struct spi_flash *flash)
719 {
720 uint32_t count;
721 int ret = 0;
722
723 if (!CONFIG(SPI_FLASH_HAS_VOLATILE_GROUP))
724 return ret;
725
726 count = volatile_group_count;
727 assert(count == 0);
728 count--;
729 volatile_group_count = count;
730
731 if (count == 0)
732 ret = chipset_volatile_group_end(flash);
733
734 return ret;
735 }
736
737 void lb_spi_flash(struct lb_header *header)
738 {
739 struct lb_spi_flash *flash;
740 const struct spi_flash *spi_flash_dev;
741
742 if (!CONFIG(BOOT_DEVICE_SPI_FLASH))
743 return;
744
745 flash = (struct lb_spi_flash *)lb_new_record(header);
746 memset(flash, 0, sizeof(*flash));
747
748 flash->tag = LB_TAG_SPI_FLASH;
749 flash->size = sizeof(*flash);
750
751 spi_flash_dev = boot_device_spi_flash();
752
753 if (spi_flash_dev) {
754 flash->flash_size = spi_flash_dev->size;
755 flash->sector_size = spi_flash_dev->sector_size;
756 flash->erase_cmd = spi_flash_dev->erase_cmd;
757 } else {
758 flash->flash_size = CONFIG_ROM_SIZE;
759 /* Default 64k erase command should work on most flash.
760 * Uniform 4k erase only works on certain devices. */
761 flash->sector_size = 64 * KiB;
762 flash->erase_cmd = CMD_BLOCK_ERASE;
763 }
764
765 if (CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
766 struct flash_mmap_window *table = (struct flash_mmap_window *)(flash + 1);
767 flash->mmap_count = spi_flash_get_mmap_windows(table);
768 flash->size += flash->mmap_count * sizeof(*table);
769 }
770
771 /* Pass 4-byte address mode information to payload */
772 if (CONFIG(SPI_FLASH_FORCE_4_BYTE_ADDR_MODE))
773 flash->flags |= LB_SPI_FLASH_FLAG_IN_4BYTE_ADDR_MODE;
774 }
775
776 int spi_flash_ctrlr_protect_region(const struct spi_flash *flash,
777 const struct region *region,
778 const enum ctrlr_prot_type type)
779 {
780 const struct spi_ctrlr *ctrlr;
781 struct region flash_region;
782
783 if (!flash)
784 return -1;
785
786 flash_region = region_create(0, flash->size);
787
788 if (!region_is_subregion(&flash_region, region))
789 return -1;
790
791 ctrlr = flash->spi.ctrlr;
792
793 if (!ctrlr)
794 return -1;
795
796 if (ctrlr->flash_protect)
797 return ctrlr->flash_protect(flash, region, type);
798
799 return -1;
800 }
801
802 int spi_flash_vector_helper(const struct spi_slave *slave,
803 struct spi_op vectors[], size_t count,
804 int (*func)(const struct spi_slave *slave, const void *dout,
805 size_t bytesout, void *din, size_t bytesin))
806 {
807 int ret;
808 void *din;
809 size_t bytes_in;
810
811 if (count < 1 || count > 2)
812 return -1;
813
814 /* SPI flash commands always have a command first... */
815 if (!vectors[0].dout || !vectors[0].bytesout)
816 return -1;
817 /* And not read any data during the command. */
818 if (vectors[0].din || vectors[0].bytesin)
819 return -1;
820
821 if (count == 2) {
822 /* If response bytes requested ensure the buffer is valid. */
823 if (vectors[1].bytesin && !vectors[1].din)
824 return -1;
825 /* No sends can accompany a receive. */
826 if (vectors[1].dout || vectors[1].bytesout)
827 return -1;
828 din = vectors[1].din;
829 bytes_in = vectors[1].bytesin;
830 } else {
831 din = NULL;
832 bytes_in = 0;
833 }
834
835 ret = func(slave, vectors[0].dout, vectors[0].bytesout, din, bytes_in);
836
837 if (ret) {
838 vectors[0].status = SPI_OP_FAILURE;
839 if (count == 2)
840 vectors[1].status = SPI_OP_FAILURE;
841 } else {
842 vectors[0].status = SPI_OP_SUCCESS;
843 if (count == 2)
844 vectors[1].status = SPI_OP_SUCCESS;
845 }
846
847 return ret;
848 }
849
850 const struct spi_flash_ops_descriptor spi_flash_pp_0x20_sector_desc = {
851 .erase_cmd = 0x20, /* Sector Erase */
852 .status_cmd = 0x05, /* Read Status */
853 .pp_cmd = 0x02, /* Page Program */
854 .wren_cmd = 0x06, /* Write Enable */
855 .ops = {
856 .read = spi_flash_cmd_read,
857 .write = spi_flash_cmd_write_page_program,
858 .erase = spi_flash_cmd_erase,
859 .status = spi_flash_cmd_status,
860 },
861 };
862
863 const struct spi_flash_ops_descriptor spi_flash_pp_0xd8_sector_desc = {
864 .erase_cmd = 0xd8, /* Sector Erase */
865 .status_cmd = 0x05, /* Read Status */
866 .pp_cmd = 0x02, /* Page Program */
867 .wren_cmd = 0x06, /* Write Enable */
868 .ops = {
869 .read = spi_flash_cmd_read,
870 .write = spi_flash_cmd_write_page_program,
871 .erase = spi_flash_cmd_erase,
872 .status = spi_flash_cmd_status,
873 },
874 };