blob: 82c3b834d3d5ae6d15295dfabdddd4b1487b6d08 [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.
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070014 */
15
16/*
17 * This file provides a common CBFS wrapper for SPI storage. SPI driver
18 * context is expanded with the buffer descriptor used to store data read from
19 * SPI.
20 */
21
Aaron Durbinc6588c52015-05-15 13:15:34 -050022#include <boot_device.h>
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070023#include <spi_flash.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070024#include <symbols.h>
Mary Ruthvenf82e8ab2015-11-13 14:05:27 -080025#include <cbmem.h>
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070026
Aaron Durbinc6588c52015-05-15 13:15:34 -050027static struct spi_flash *spi_flash_info;
28
29static ssize_t spi_readat(const struct region_device *rd, void *b,
30 size_t offset, size_t size)
31{
32 if (spi_flash_info->read(spi_flash_info, offset, size, b))
33 return -1;
34 return size;
35}
36
37static const struct region_device_ops spi_ops = {
38 .mmap = mmap_helper_rdev_mmap,
39 .munmap = mmap_helper_rdev_munmap,
40 .readat = spi_readat,
Vadim Bendeburyadcb0952014-05-01 12:23:09 -070041};
42
Aaron Durbinc6588c52015-05-15 13:15:34 -050043static struct mmap_helper_region_device mdev =
44 MMAP_HELPER_REGION_INIT(&spi_ops, 0, CONFIG_ROM_SIZE);
45
Mary Ruthvenf82e8ab2015-11-13 14:05:27 -080046static void initialize_mdev(int unused)
47{
48 mmap_helper_device_init(&mdev, _dram_cbfs_cache, _dram_cbfs_cache_size);
49}
50ROMSTAGE_CBMEM_INIT_HOOK(initialize_mdev);
51
Aaron Durbinc6588c52015-05-15 13:15:34 -050052void boot_device_init(void)
53{
54 int bus = CONFIG_BOOT_MEDIA_SPI_BUS;
55 int cs = 0;
56
57 if (spi_flash_info != NULL)
58 return;
59
60 spi_flash_info = spi_flash_probe(bus, cs);
61
62 mmap_helper_device_init(&mdev, _cbfs_cache, _cbfs_cache_size);
63}
64
65/* Return the CBFS boot device. */
66const struct region_device *boot_device_ro(void)
67{
68 if (spi_flash_info == NULL)
69 return NULL;
70
71 return &mdev.rdev;
72}