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> |
| 28 | |
| 29 | /* SPI flash as CBFS media. */ |
| 30 | struct cbfs_spi_context { |
| 31 | struct spi_flash *spi_flash_info; |
| 32 | struct cbfs_simple_buffer buffer; |
| 33 | }; |
| 34 | |
| 35 | static struct cbfs_spi_context spi_context; |
| 36 | |
| 37 | static int cbfs_media_open(struct cbfs_media *media) |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int cbfs_media_close(struct cbfs_media *media) |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static size_t cbfs_media_read(struct cbfs_media *media, |
| 48 | void *dest, size_t offset, |
| 49 | size_t count) |
| 50 | { |
| 51 | struct cbfs_spi_context *context = media->context; |
| 52 | |
| 53 | return context->spi_flash_info->read |
| 54 | (context->spi_flash_info, offset, count, dest) ? 0 : count; |
| 55 | } |
| 56 | |
| 57 | static void *cbfs_media_map(struct cbfs_media *media, |
| 58 | size_t offset, size_t count) |
| 59 | { |
| 60 | struct cbfs_spi_context *context = media->context; |
| 61 | |
| 62 | return cbfs_simple_buffer_map(&context->buffer, media, offset, count); |
| 63 | } |
| 64 | |
| 65 | static void *cbfs_media_unmap(struct cbfs_media *media, |
| 66 | const void *address) |
| 67 | { |
| 68 | struct cbfs_spi_context *context = media->context; |
| 69 | |
| 70 | return cbfs_simple_buffer_unmap(&context->buffer, address); |
| 71 | } |
| 72 | |
Vadim Bendebury | a88c919 | 2014-09-22 17:25:37 -0700 | [diff] [blame^] | 73 | static int init_cbfs_media_context(void) |
| 74 | { |
| 75 | if (!spi_context.spi_flash_info) { |
| 76 | |
| 77 | spi_context.spi_flash_info = spi_flash_probe |
| 78 | (CONFIG_BOOT_MEDIA_SPI_BUS, 0); |
| 79 | |
| 80 | if (!spi_context.spi_flash_info) |
| 81 | return -1; |
| 82 | |
| 83 | spi_context.buffer.buffer = (void *)CONFIG_CBFS_CACHE_ADDRESS; |
| 84 | spi_context.buffer.size = CONFIG_CBFS_CACHE_SIZE; |
| 85 | } |
| 86 | return 0; |
| 87 | |
| 88 | } |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 89 | int init_default_cbfs_media(struct cbfs_media *media) |
| 90 | { |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 91 | media->context = &spi_context; |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 92 | media->open = cbfs_media_open; |
| 93 | media->close = cbfs_media_close; |
| 94 | media->read = cbfs_media_read; |
| 95 | media->map = cbfs_media_map; |
| 96 | media->unmap = cbfs_media_unmap; |
| 97 | |
Vadim Bendebury | a88c919 | 2014-09-22 17:25:37 -0700 | [diff] [blame^] | 98 | return init_cbfs_media_context(); |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 99 | } |