Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 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; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * This file provides a common CBFS wrapper for SPI storage. SPI driver |
| 22 | * context is expanded with the buffer descriptor used to store data read from |
| 23 | * SPI. |
| 24 | */ |
| 25 | |
| 26 | #include <cbfs.h> |
| 27 | #include <spi_flash.h> |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 28 | #include <symbols.h> |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 29 | |
| 30 | /* SPI flash as CBFS media. */ |
| 31 | struct cbfs_spi_context { |
| 32 | struct spi_flash *spi_flash_info; |
| 33 | struct cbfs_simple_buffer buffer; |
| 34 | }; |
| 35 | |
| 36 | static struct cbfs_spi_context spi_context; |
| 37 | |
| 38 | static int cbfs_media_open(struct cbfs_media *media) |
| 39 | { |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int cbfs_media_close(struct cbfs_media *media) |
| 44 | { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static size_t cbfs_media_read(struct cbfs_media *media, |
| 49 | void *dest, size_t offset, |
| 50 | size_t count) |
| 51 | { |
| 52 | struct cbfs_spi_context *context = media->context; |
| 53 | |
| 54 | return context->spi_flash_info->read |
| 55 | (context->spi_flash_info, offset, count, dest) ? 0 : count; |
| 56 | } |
| 57 | |
| 58 | static void *cbfs_media_map(struct cbfs_media *media, |
| 59 | size_t offset, size_t count) |
| 60 | { |
| 61 | struct cbfs_spi_context *context = media->context; |
| 62 | |
| 63 | return cbfs_simple_buffer_map(&context->buffer, media, offset, count); |
| 64 | } |
| 65 | |
| 66 | static void *cbfs_media_unmap(struct cbfs_media *media, |
| 67 | const void *address) |
| 68 | { |
| 69 | struct cbfs_spi_context *context = media->context; |
| 70 | |
| 71 | return cbfs_simple_buffer_unmap(&context->buffer, address); |
| 72 | } |
| 73 | |
Vadim Bendebury | a88c919 | 2014-09-22 17:25:37 -0700 | [diff] [blame] | 74 | static int init_cbfs_media_context(void) |
| 75 | { |
| 76 | if (!spi_context.spi_flash_info) { |
| 77 | |
| 78 | spi_context.spi_flash_info = spi_flash_probe |
| 79 | (CONFIG_BOOT_MEDIA_SPI_BUS, 0); |
| 80 | |
| 81 | if (!spi_context.spi_flash_info) |
| 82 | return -1; |
| 83 | |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 84 | spi_context.buffer.buffer = (void *)_cbfs_cache; |
| 85 | spi_context.buffer.size = _cbfs_cache_size; |
Vadim Bendebury | a88c919 | 2014-09-22 17:25:37 -0700 | [diff] [blame] | 86 | } |
| 87 | return 0; |
| 88 | |
| 89 | } |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 90 | int init_default_cbfs_media(struct cbfs_media *media) |
| 91 | { |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 92 | media->context = &spi_context; |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 93 | media->open = cbfs_media_open; |
| 94 | media->close = cbfs_media_close; |
| 95 | media->read = cbfs_media_read; |
| 96 | media->map = cbfs_media_map; |
| 97 | media->unmap = cbfs_media_unmap; |
| 98 | |
Vadim Bendebury | a88c919 | 2014-09-22 17:25:37 -0700 | [diff] [blame] | 99 | return init_cbfs_media_context(); |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 100 | } |