summaryrefslogtreecommitdiff
path: root/src/drivers/spi/boot_device_rw_nommap.c
blob: cc8fbf7c5d0af0b1cfd07fa41cdc5e55989f78e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2016 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <arch/early_variables.h>
#include <boot_device.h>
#include <spi_flash.h>
#include <spi-generic.h>
#include <stdint.h>

static struct spi_flash sfg CAR_GLOBAL;
static bool sfg_init_done CAR_GLOBAL;

static ssize_t spi_readat(const struct region_device *rd, void *b,
				size_t offset, size_t size)
{
	struct spi_flash *sf = car_get_var_ptr(&sfg);

	if (sf == NULL)
		return -1;

	if (spi_flash_read(sf, offset, size, b))
		return -1;

	return size;
}

static ssize_t spi_writeat(const struct region_device *rd, const void *b,
				size_t offset, size_t size)
{
	struct spi_flash *sf = car_get_var_ptr(&sfg);

	if (sf == NULL)
		return -1;

	if (spi_flash_write(sf, offset, size, b))
		return -1;

	return size;
}

static ssize_t spi_eraseat(const struct region_device *rd,
				size_t offset, size_t size)
{
	struct spi_flash *sf = car_get_var_ptr(&sfg);

	if (sf == NULL)
		return -1;

	if (spi_flash_erase(sf, offset, size))
		return -1;

	return size;
}

static const struct region_device_ops spi_ops = {
	.readat = spi_readat,
	.writeat = spi_writeat,
	.eraseat = spi_eraseat,
};

static const struct region_device spi_rw =
	REGION_DEV_INIT(&spi_ops, 0, CONFIG_ROM_SIZE);

static void boot_device_rw_init(void)
{
	const int bus = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS;
	const int cs = 0;

	if (car_get_var(sfg_init_done) == true)
		return;

	/* Ensure any necessary setup is performed by the drivers. */
	spi_init();

	if (!spi_flash_probe(bus, cs, car_get_var_ptr(&sfg)))
		car_set_var(sfg_init_done, true);
}

const struct region_device *boot_device_rw(void)
{
	/* Probe for the SPI flash device if not already done. */
	boot_device_rw_init();

	if (car_get_var(sfg_init_done) != true)
		return NULL;

	return &spi_rw;
}

const struct spi_flash *boot_device_spi_flash(void)
{
	boot_device_rw_init();

	if (car_get_var(sfg_init_done) != true)
		return NULL;

	return car_get_var_ptr(&sfg);
}

int boot_device_wp_region(const struct region_device *rd,
			  const enum bootdev_prot_type type)
{
	uint32_t ctrlr_pr;

	/* Ensure boot device has been initialized at least once. */
	boot_device_init();

	const struct spi_flash *boot_dev = boot_device_spi_flash();

	if (boot_dev == NULL)
		return -1;

	if (type == MEDIA_WP) {
		if (spi_flash_is_write_protected(boot_dev,
					region_device_region(rd)) != 1) {
			return spi_flash_set_write_protected(boot_dev,
						region_device_region(rd), true,
						SPI_WRITE_PROTECTION_REBOOT);
		}

		/* Already write protected */
		return 0;
	}

	switch (type) {
	case CTRLR_WP:
		ctrlr_pr = WRITE_PROTECT;
		break;
	case CTRLR_RP:
		ctrlr_pr = READ_PROTECT;
		break;
	case CTRLR_RWP:
		ctrlr_pr = READ_WRITE_PROTECT;
		break;
	default:
		return -1;
	}

	return spi_flash_ctrlr_protect_region(boot_dev,
					region_device_region(rd), ctrlr_pr);
}