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. |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 14 | */ |
| 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 Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 22 | #include <boot_device.h> |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 23 | #include <spi_flash.h> |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 24 | #include <symbols.h> |
Mary Ruthven | f82e8ab | 2015-11-13 14:05:27 -0800 | [diff] [blame] | 25 | #include <cbmem.h> |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 26 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 27 | static struct spi_flash *spi_flash_info; |
| 28 | |
| 29 | static 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 | |
| 37 | static const struct region_device_ops spi_ops = { |
| 38 | .mmap = mmap_helper_rdev_mmap, |
| 39 | .munmap = mmap_helper_rdev_munmap, |
| 40 | .readat = spi_readat, |
Vadim Bendebury | adcb095 | 2014-05-01 12:23:09 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 43 | static struct mmap_helper_region_device mdev = |
| 44 | MMAP_HELPER_REGION_INIT(&spi_ops, 0, CONFIG_ROM_SIZE); |
| 45 | |
Mary Ruthven | f82e8ab | 2015-11-13 14:05:27 -0800 | [diff] [blame] | 46 | static void initialize_mdev(int unused) |
| 47 | { |
| 48 | mmap_helper_device_init(&mdev, _dram_cbfs_cache, _dram_cbfs_cache_size); |
| 49 | } |
| 50 | ROMSTAGE_CBMEM_INIT_HOOK(initialize_mdev); |
| 51 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 52 | void 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. */ |
| 66 | const struct region_device *boot_device_ro(void) |
| 67 | { |
| 68 | if (spi_flash_info == NULL) |
| 69 | return NULL; |
| 70 | |
| 71 | return &mdev.rdev; |
| 72 | } |