blob: 99dcaac39e1a50597ff58c05f3801fbef0e36055 [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>
Lee Leahyff767092016-04-12 13:01:02 -070018#include <bootmode.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050019#include <console/console.h>
20#include <string.h>
21#include <spi-generic.h>
22#include <spi_flash.h>
Duncan Lauriea32b6b92015-01-15 15:49:07 -080023#include <soc/spi.h>
Duncan Lauriea32b6b92015-01-15 15:49:07 -080024#include <vendorcode/google/chromeos/chromeos.h>
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070025#include "nvm.h"
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050026
27/* This module assumes the flash is memory mapped just below 4GiB in the
28 * address space for reading. Also this module assumes an area it erased
29 * when all bytes read as all 0xff's. */
30
31static struct spi_flash *flash;
32
33static int nvm_init(void)
34{
35 if (flash != NULL)
36 return 0;
37
38 spi_init();
Gabe Black1e187352014-03-27 20:37:03 -070039 flash = spi_flash_probe(0, 0);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050040 if (!flash) {
41 printk(BIOS_DEBUG, "Could not find SPI device\n");
42 return -1;
43 }
44
45 return 0;
46}
47
Alexandru Gagniuc92206412016-03-03 10:58:30 -080048/*
49 * Convert memory mapped pointer to flash offset.
50 *
51 * This is weak because not every platforms memory-maps the NVM media in the
52 * same manner. This is a stop-gap solution.
53 *
54 * The root of the problem is that users of this API work in memory space for
55 * both reads and writes, but erase and write must be done in flash space. This
56 * also only works when the media is memory-mapped, which is no longer
57 * universally true. The long-term approach should be to rewrite this and its
58 * users to work in flash space, while using rdev_read() instead of rdev_mmap().
59 */
60__attribute__((weak))
61uint32_t nvm_mmio_to_flash_offset(void *p)
Kyösti Mälkki9b29aad2015-01-06 07:08:46 +010062{
63 return CONFIG_ROM_SIZE + (uintptr_t)p;
64}
65
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050066int nvm_is_erased(const void *start, size_t size)
67{
Aaron Durbinb013fff2014-01-13 11:39:04 -060068 const uint8_t *cur = start;
69 const uint8_t erased_value = 0xff;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050070
71 while (size > 0) {
Aaron Durbinb013fff2014-01-13 11:39:04 -060072 if (*cur != erased_value)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050073 return 0;
74 cur++;
75 size--;
76 }
77 return 1;
78}
79
80int nvm_erase(void *start, size_t size)
81{
82 if (nvm_init() < 0)
83 return -1;
Alexandru Gagniuc92206412016-03-03 10:58:30 -080084 return flash->erase(flash, nvm_mmio_to_flash_offset(start), size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050085}
86
87/* Write data to NVM. Returns 0 on success < 0 on error. */
88int nvm_write(void *start, const void *data, size_t size)
89{
90 if (nvm_init() < 0)
91 return -1;
Alexandru Gagniuc92206412016-03-03 10:58:30 -080092 return flash->write(flash, nvm_mmio_to_flash_offset(start), size, data);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050093}
Duncan Lauriea32b6b92015-01-15 15:49:07 -080094
95/* Read flash status register to determine if write protect is active */
96int nvm_is_write_protected(void)
97{
Duncan Lauriea32b6b92015-01-15 15:49:07 -080098 if (nvm_init() < 0)
99 return -1;
100
Lee Leahyff767092016-04-12 13:01:02 -0700101 if (IS_ENABLED(CONFIG_CHROMEOS)) {
102 u8 sr1;
103 u8 wp_gpio;
104 u8 wp_spi;
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800105
Lee Leahyff767092016-04-12 13:01:02 -0700106 /* Read Write Protect GPIO if available */
107 wp_gpio = get_write_protect_state();
108
109 /* Read Status Register 1 */
110 if (flash->status(flash, &sr1) < 0) {
111 printk(BIOS_ERR,
112 "Failed to read SPI status register 1\n");
113 return -1;
114 }
115 wp_spi = !!(sr1 & 0x80);
116
117 printk(BIOS_DEBUG, "SPI flash protection: WPSW=%d SRP0=%d\n",
118 wp_gpio, wp_spi);
119
120 return wp_gpio && wp_spi;
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800121 }
Lee Leahyff767092016-04-12 13:01:02 -0700122 return 0;
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800123}
124
125/* Apply protection to a range of flash */
126int nvm_protect(void *start, size_t size)
127{
128#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
129 if (nvm_init() < 0)
130 return -1;
Alexandru Gagniuc92206412016-03-03 10:58:30 -0800131 return spi_flash_protect(nvm_mmio_to_flash_offset(start), size);
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800132#else
133 return -1;
134#endif
135}