blob: ffa0628b056118e9a5fb2ff1406d7e5fc9461a63 [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{
Mary Ruthvena8aef3a2015-11-24 09:43:27 -080048 /*
49 * Call boot_device_init() to ensure spi_flash is initialized before
50 * backing mdev with postram cache. This prevents the mdev backing from
51 * being overwritten if spi_flash was not accessed before dram was up.
52 */
53 boot_device_init();
54 mmap_helper_device_init(&mdev, _postram_cbfs_cache,
55 _postram_cbfs_cache_size);
Mary Ruthvenf82e8ab2015-11-13 14:05:27 -080056}
57ROMSTAGE_CBMEM_INIT_HOOK(initialize_mdev);
58
Aaron Durbinc6588c52015-05-15 13:15:34 -050059void boot_device_init(void)
60{
61 int bus = CONFIG_BOOT_MEDIA_SPI_BUS;
62 int cs = 0;
63
64 if (spi_flash_info != NULL)
65 return;
66
67 spi_flash_info = spi_flash_probe(bus, cs);
68
69 mmap_helper_device_init(&mdev, _cbfs_cache, _cbfs_cache_size);
70}
71
72/* Return the CBFS boot device. */
73const struct region_device *boot_device_ro(void)
74{
75 if (spi_flash_info == NULL)
76 return NULL;
77
78 return &mdev.rdev;
79}