Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * This file provides a common CBFS wrapper for SPI storage. SPI driver |
| 5 | * context is expanded with the buffer descriptor used to store data read from |
| 6 | * SPI. |
| 7 | */ |
| 8 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 9 | #include <boot_device.h> |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 10 | #include <cbfs.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 11 | #include <console/console.h> |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 12 | #include <spi_flash.h> |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 13 | #include <symbols.h> |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 14 | #include <stdint.h> |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 15 | #include <timer.h> |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 16 | |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 17 | static struct spi_flash spi_flash_info; |
| 18 | static bool spi_flash_init_done; |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 19 | |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 20 | /* |
Julius Werner | 8dcf24f | 2019-06-06 17:35:25 -0700 | [diff] [blame] | 21 | * SPI speed logging for big transfers available with BIOS_DEBUG. The format is: |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 22 | * |
Julius Werner | 8dcf24f | 2019-06-06 17:35:25 -0700 | [diff] [blame] | 23 | * read SPI 0x62854 0x7db7: 10416 us, 3089 KB/s, 24.712 Mbps |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 24 | * |
| 25 | * The important number is the last one. It should roughly match your SPI |
| 26 | * clock. If it doesn't, your driver might need a little tuning. |
| 27 | */ |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 28 | static ssize_t spi_readat(const struct region_device *rd, void *b, |
| 29 | size_t offset, size_t size) |
| 30 | { |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 31 | struct stopwatch sw; |
Julius Werner | 8dcf24f | 2019-06-06 17:35:25 -0700 | [diff] [blame] | 32 | bool show = size >= 4 * KiB && console_log_level(BIOS_DEBUG); |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 33 | |
| 34 | if (show) |
| 35 | stopwatch_init(&sw); |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 36 | if (spi_flash_read(&spi_flash_info, offset, size, b)) |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 37 | return -1; |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 38 | if (show) { |
| 39 | long usecs; |
| 40 | |
| 41 | usecs = stopwatch_duration_usecs(&sw); |
| 42 | u64 speed; /* KiB/s */ |
| 43 | int bps; /* Bits per second */ |
| 44 | |
Julius Werner | f96d905 | 2019-08-16 15:35:39 -0700 | [diff] [blame] | 45 | speed = size * (u64)1000 / usecs; |
Simon Glass | aa58a9e | 2016-08-27 15:03:02 -0600 | [diff] [blame] | 46 | bps = speed * 8; |
| 47 | |
| 48 | printk(BIOS_DEBUG, "read SPI %#zx %#zx: %ld us, %lld KB/s, %d.%03d Mbps\n", |
| 49 | offset, size, usecs, speed, bps / 1000, bps % 1000); |
| 50 | } |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 51 | return size; |
| 52 | } |
| 53 | |
Aaron Durbin | 504b8f2 | 2016-08-11 11:10:42 -0500 | [diff] [blame] | 54 | static ssize_t spi_writeat(const struct region_device *rd, const void *b, |
| 55 | size_t offset, size_t size) |
| 56 | { |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 57 | if (spi_flash_write(&spi_flash_info, offset, size, b)) |
Aaron Durbin | 504b8f2 | 2016-08-11 11:10:42 -0500 | [diff] [blame] | 58 | return -1; |
| 59 | return size; |
| 60 | } |
| 61 | |
| 62 | static ssize_t spi_eraseat(const struct region_device *rd, |
| 63 | size_t offset, size_t size) |
| 64 | { |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 65 | if (spi_flash_erase(&spi_flash_info, offset, size)) |
Aaron Durbin | 504b8f2 | 2016-08-11 11:10:42 -0500 | [diff] [blame] | 66 | return -1; |
| 67 | return size; |
| 68 | } |
| 69 | |
| 70 | /* Provide all operations on the same device. */ |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 71 | static const struct region_device_ops spi_ops = { |
| 72 | .mmap = mmap_helper_rdev_mmap, |
| 73 | .munmap = mmap_helper_rdev_munmap, |
| 74 | .readat = spi_readat, |
Aaron Durbin | 504b8f2 | 2016-08-11 11:10:42 -0500 | [diff] [blame] | 75 | .writeat = spi_writeat, |
| 76 | .eraseat = spi_eraseat, |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 79 | static struct mmap_helper_region_device mdev = |
Julius Werner | 9b1f3cc | 2020-12-30 17:30:12 -0800 | [diff] [blame] | 80 | MMAP_HELPER_DEV_INIT(&spi_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache); |
Mary Ruthven | f82e8ab | 2015-11-13 14:05:27 -0800 | [diff] [blame] | 81 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 82 | void boot_device_init(void) |
| 83 | { |
Aaron Durbin | 08e842c | 2016-08-11 14:40:09 -0500 | [diff] [blame] | 84 | int bus = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS; |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 85 | int cs = 0; |
| 86 | |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 87 | if (spi_flash_init_done == true) |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 88 | return; |
| 89 | |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 90 | if (spi_flash_probe(bus, cs, &spi_flash_info)) |
| 91 | return; |
| 92 | |
| 93 | spi_flash_init_done = true; |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Return the CBFS boot device. */ |
| 97 | const struct region_device *boot_device_ro(void) |
| 98 | { |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 99 | if (spi_flash_init_done != true) |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 100 | return NULL; |
| 101 | |
| 102 | return &mdev.rdev; |
| 103 | } |
Aaron Durbin | 504b8f2 | 2016-08-11 11:10:42 -0500 | [diff] [blame] | 104 | |
| 105 | /* The read-only and read-write implementations are symmetric. */ |
| 106 | const struct region_device *boot_device_rw(void) |
| 107 | { |
| 108 | return boot_device_ro(); |
| 109 | } |
Furquan Shaikh | 78bc6dd | 2017-05-16 13:32:34 -0700 | [diff] [blame] | 110 | |
| 111 | const struct spi_flash *boot_device_spi_flash(void) |
| 112 | { |
| 113 | boot_device_init(); |
Furquan Shaikh | 30221b4 | 2017-05-15 14:35:15 -0700 | [diff] [blame] | 114 | |
| 115 | if (spi_flash_init_done != true) |
| 116 | return NULL; |
| 117 | |
| 118 | return &spi_flash_info; |
Furquan Shaikh | 78bc6dd | 2017-05-16 13:32:34 -0700 | [diff] [blame] | 119 | } |