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 | { |
Mary Ruthven | a8aef3a | 2015-11-24 09:43:27 -0800 | [diff] [blame^] | 48 | /* |
| 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 Ruthven | f82e8ab | 2015-11-13 14:05:27 -0800 | [diff] [blame] | 56 | } |
| 57 | ROMSTAGE_CBMEM_INIT_HOOK(initialize_mdev); |
| 58 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame] | 59 | void 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. */ |
| 73 | const struct region_device *boot_device_ro(void) |
| 74 | { |
| 75 | if (spi_flash_info == NULL) |
| 76 | return NULL; |
| 77 | |
| 78 | return &mdev.rdev; |
| 79 | } |