blob: fe73408b51763220cc1ef9c1b54c27f506f8993a [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
Duncan Lauried8c4f2b2014-04-22 10:46:06 -07004 * Copyright (C) 2014 Google Inc.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05005 *
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.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050014 */
15
16#include <stdint.h>
17#include <stddef.h>
18#include <console/console.h>
19#include <string.h>
20#include <spi-generic.h>
21#include <spi_flash.h>
Duncan Lauriea32b6b92015-01-15 15:49:07 -080022#include <soc/spi.h>
23#if CONFIG_CHROMEOS
24#include <vendorcode/google/chromeos/chromeos.h>
25#endif
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070026#include "nvm.h"
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050027
28/* This module assumes the flash is memory mapped just below 4GiB in the
29 * address space for reading. Also this module assumes an area it erased
30 * when all bytes read as all 0xff's. */
31
32static struct spi_flash *flash;
33
34static int nvm_init(void)
35{
36 if (flash != NULL)
37 return 0;
38
39 spi_init();
Gabe Black1e187352014-03-27 20:37:03 -070040 flash = spi_flash_probe(0, 0);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050041 if (!flash) {
42 printk(BIOS_DEBUG, "Could not find SPI device\n");
43 return -1;
44 }
45
46 return 0;
47}
48
Kyösti Mälkki9b29aad2015-01-06 07:08:46 +010049/* Convert memory mapped pointer to flash offset. */
50static inline uint32_t to_flash_offset(void *p)
51{
52 return CONFIG_ROM_SIZE + (uintptr_t)p;
53}
54
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050055int nvm_is_erased(const void *start, size_t size)
56{
Aaron Durbinb013fff2014-01-13 11:39:04 -060057 const uint8_t *cur = start;
58 const uint8_t erased_value = 0xff;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050059
60 while (size > 0) {
Aaron Durbinb013fff2014-01-13 11:39:04 -060061 if (*cur != erased_value)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050062 return 0;
63 cur++;
64 size--;
65 }
66 return 1;
67}
68
69int nvm_erase(void *start, size_t size)
70{
71 if (nvm_init() < 0)
72 return -1;
Kyösti Mälkki9b29aad2015-01-06 07:08:46 +010073 return flash->erase(flash, to_flash_offset(start), size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050074}
75
76/* Write data to NVM. Returns 0 on success < 0 on error. */
77int nvm_write(void *start, const void *data, size_t size)
78{
79 if (nvm_init() < 0)
80 return -1;
Kyösti Mälkki9b29aad2015-01-06 07:08:46 +010081 return flash->write(flash, to_flash_offset(start), size, data);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050082}
Duncan Lauriea32b6b92015-01-15 15:49:07 -080083
84/* Read flash status register to determine if write protect is active */
85int nvm_is_write_protected(void)
86{
87 u8 sr1;
88 u8 wp_gpio = 0;
89 u8 wp_spi;
90
91 if (nvm_init() < 0)
92 return -1;
93
94#if IS_ENABLED(CONFIG_CHROMEOS)
95 /* Read Write Protect GPIO if available */
96 wp_gpio = get_write_protect_state();
97#endif
98
99 /* Read Status Register 1 */
100 if (flash->status(flash, &sr1) < 0) {
101 printk(BIOS_ERR, "Failed to read SPI status register 1\n");
102 return -1;
103 }
104 wp_spi = !!(sr1 & 0x80);
105
106 printk(BIOS_DEBUG, "SPI flash protection: WPSW=%d SRP0=%d\n",
107 wp_gpio, wp_spi);
108
109 return wp_gpio && wp_spi;
110}
111
112/* Apply protection to a range of flash */
113int nvm_protect(void *start, size_t size)
114{
115#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
116 if (nvm_init() < 0)
117 return -1;
118 return spi_flash_protect(to_flash_offset(start), size);
119#else
120 return -1;
121#endif
122}