blob: 447153295f6482b89761c7ed2f5f1062cb547ebe [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>
28
29/* SPI flash as CBFS media. */
30struct cbfs_spi_context {
31 struct spi_flash *spi_flash_info;
32 struct cbfs_simple_buffer buffer;
33};
34
35static struct cbfs_spi_context spi_context;
36
37static int cbfs_media_open(struct cbfs_media *media)
38{
39 return 0;
40}
41
42static int cbfs_media_close(struct cbfs_media *media)
43{
44 return 0;
45}
46
47static 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
57static 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
65static 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
73int init_default_cbfs_media(struct cbfs_media *media)
74{
75 if (spi_context.buffer.buffer)
76 return 0; /* It has been already initialized. */
77
78 spi_context.spi_flash_info = spi_flash_probe
79 (CONFIG_BOOT_MEDIA_SPI_BUS, 0);
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 media->context = &spi_context;
87
88 media->open = cbfs_media_open;
89 media->close = cbfs_media_close;
90 media->read = cbfs_media_read;
91 media->map = cbfs_media_map;
92 media->unmap = cbfs_media_unmap;
93
94 return 0;
95}