blob: 81e6ec3ce674b8076a6f34a890d27062d449a82f [file] [log] [blame]
Vadim Bendeburyadcb0952014-05-01 12:23:09 -07001/*
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 Wernerec5e5e02014-08-20 15:29:56 -070028#include <symbols.h>
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070029
30/* SPI flash as CBFS media. */
31struct cbfs_spi_context {
32 struct spi_flash *spi_flash_info;
33 struct cbfs_simple_buffer buffer;
34};
35
36static struct cbfs_spi_context spi_context;
37
38static int cbfs_media_open(struct cbfs_media *media)
39{
40 return 0;
41}
42
43static int cbfs_media_close(struct cbfs_media *media)
44{
45 return 0;
46}
47
48static 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
58static 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
66static 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 Bendeburya88c9192014-09-22 17:25:37 -070074static 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 Wernerec5e5e02014-08-20 15:29:56 -070084 spi_context.buffer.buffer = (void *)_cbfs_cache;
85 spi_context.buffer.size = _cbfs_cache_size;
Vadim Bendeburya88c9192014-09-22 17:25:37 -070086 }
87 return 0;
88
89}
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070090int init_default_cbfs_media(struct cbfs_media *media)
91{
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070092 media->context = &spi_context;
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070093 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 Bendeburya88c9192014-09-22 17:25:37 -070099 return init_cbfs_media_context();
Vadim Bendeburyadcb0952014-05-01 12:23:09 -0700100}