xref: /aosp_15_r20/external/flashrom/linux_spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2011 Sven Schnelle <[email protected]>
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.
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <inttypes.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <unistd.h>
25 #include <sys/ioctl.h>
26 #include <linux/types.h>
27 #include "flash.h"
28 #include "chipdrivers.h"
29 #include "programmer.h"
30 #include "spi.h"
31 /*
32  * Linux versions prior to v4.14-rc7 may need linux/ioctl.h included here due
33  * to missing from linux/spi/spidev.h. This was fixed in the following commit:
34  * a2b4a79b88b2 spi: uapi: spidev: add missing ioctl header
35  */
36 #include <linux/ioctl.h>
37 #include <linux/spi/spidev.h>
38 
39 /* Devices known to work with this module (FIXME: export as struct dev_entry):
40  * Beagle Bone Black
41  * Raspberry Pi
42  * HummingBoard
43  */
44 
45 #define BUF_SIZE_FROM_SYSFS	"/sys/module/spidev/parameters/bufsiz"
46 
47 struct linux_spi_data {
48 	int fd;
49 	size_t max_kernel_buf_size;
50 };
51 
linux_spi_read(struct flashctx * flash,uint8_t * buf,unsigned int start,unsigned int len)52 static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
53 {
54 	struct linux_spi_data *spi_data = flash->mst->spi.data;
55 	/* Older kernels use a single buffer for combined input and output
56 	   data. So account for longest possible command + address, too. */
57 	return spi_read_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
58 }
59 
linux_spi_write_256(struct flashctx * flash,const uint8_t * buf,unsigned int start,unsigned int len)60 static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
61 {
62 	struct linux_spi_data *spi_data = flash->mst->spi.data;
63 	/* 5 bytes must be reserved for longest possible command + address. */
64 	return spi_write_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
65 }
66 
linux_spi_shutdown(void * data)67 static int linux_spi_shutdown(void *data)
68 {
69 	struct linux_spi_data *spi_data = data;
70 	close(spi_data->fd);
71 	free(spi_data);
72 	return 0;
73 }
74 
linux_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * txbuf,unsigned char * rxbuf)75 static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
76 				  unsigned int readcnt,
77 				  const unsigned char *txbuf,
78 				  unsigned char *rxbuf)
79 {
80 	struct linux_spi_data *spi_data = flash->mst->spi.data;
81 	int iocontrol_code;
82 	struct spi_ioc_transfer msg[2] = {
83 		{
84 			.tx_buf = (uint64_t)(uintptr_t)txbuf,
85 			.len = writecnt,
86 		},
87 		{
88 			.rx_buf = (uint64_t)(uintptr_t)rxbuf,
89 			.len = readcnt,
90 		},
91 	};
92 
93 	if (spi_data->fd == -1)
94 		return -1;
95 	/* The implementation currently does not support requests that
96 	   don't start with sending a command. */
97 	if (writecnt == 0)
98 		return SPI_INVALID_LENGTH;
99 
100 	/* Just submit the first (write) request in case there is nothing
101 	   to read. Otherwise submit both requests. */
102 	if (readcnt == 0)
103 		iocontrol_code = SPI_IOC_MESSAGE(1);
104 	else
105 		iocontrol_code = SPI_IOC_MESSAGE(2);
106 
107 	if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) {
108 		msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
109 		return -1;
110 	}
111 	return 0;
112 }
113 
114 static const struct spi_master spi_master_linux = {
115 	.features	= SPI_MASTER_4BA,
116 	.max_data_read	= MAX_DATA_UNSPECIFIED, /* TODO? */
117 	.max_data_write	= MAX_DATA_UNSPECIFIED, /* TODO? */
118 	.command	= linux_spi_send_command,
119 	.read		= linux_spi_read,
120 	.write_256	= linux_spi_write_256,
121 	.shutdown	= linux_spi_shutdown,
122 };
123 
124 /* Read max buffer size from sysfs, or use page size as fallback. */
get_max_kernel_buf_size()125 static size_t get_max_kernel_buf_size()
126 {
127 	size_t result = 0;
128 	FILE *fp;
129 	fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
130 	if (!fp) {
131 		msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
132 		goto out;
133 	}
134 
135 	char buf[10];
136 	if (!fgets(buf, sizeof(buf), fp)) {
137 		if (feof(fp))
138 			msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
139 		else
140 			msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
141 		goto out;
142 	}
143 
144 	long int tmp;
145 	errno = 0;
146 	tmp = strtol(buf, NULL, 0);
147 	if ((tmp < 0) || errno) {
148 		msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
149 	} else {
150 		msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
151 		result = (size_t)tmp;
152 	}
153 
154 out:
155 	if (fp)
156 		fclose(fp);
157 
158 	if (!result) {
159 		msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
160 		result = (size_t)getpagesize();
161 	}
162 	return result;
163 }
164 
linux_spi_init(const struct programmer_cfg * cfg)165 static int linux_spi_init(const struct programmer_cfg *cfg)
166 {
167 	char *param_str, *endp;
168 	uint32_t speed_hz = 2 * 1000 * 1000;
169 	/* FIXME: make the following configurable by CLI options. */
170 	/* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
171 	const uint8_t mode = SPI_MODE_0;
172 	const uint8_t bits = 8;
173 	int fd;
174 	size_t max_kernel_buf_size;
175 	struct linux_spi_data *spi_data;
176 
177 	param_str = extract_programmer_param_str(cfg, "spispeed");
178 	if (param_str && strlen(param_str)) {
179 		speed_hz = (uint32_t)strtoul(param_str, &endp, 10) * 1000;
180 		if (param_str == endp || speed_hz == 0) {
181 			msg_perr("%s: invalid clock: %s kHz\n", __func__, param_str);
182 			free(param_str);
183 			return 1;
184 		}
185 	} else {
186 		msg_pinfo("Using default %"PRIu32
187 			  "kHz clock. Use 'spispeed' parameter to override.\n",
188 			  speed_hz / 1000);
189 	}
190 	free(param_str);
191 
192 	param_str = extract_programmer_param_str(cfg, "dev");
193 	if (!param_str || !strlen(param_str)) {
194 		msg_perr("No SPI device given. Use flashrom -p "
195 			 "linux_spi:dev=/dev/spidevX.Y\n");
196 		free(param_str);
197 		return 1;
198 	}
199 
200 	msg_pdbg("Using device %s\n", param_str);
201 	if ((fd = open(param_str, O_RDWR)) == -1) {
202 		msg_perr("%s: failed to open %s: %s\n", __func__,
203 			 param_str, strerror(errno));
204 		free(param_str);
205 		return 1;
206 	}
207 	free(param_str);
208 
209 	if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
210 		msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
211 			 __func__, speed_hz, strerror(errno));
212 		goto init_err;
213 	}
214 	msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
215 
216 	if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
217 		msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
218 			 __func__, mode, strerror(errno));
219 		goto init_err;
220 	}
221 
222 	if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
223 		msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
224 			 __func__, bits == 0 ? 8 : bits, strerror(errno));
225 		goto init_err;
226 	}
227 
228 	max_kernel_buf_size = get_max_kernel_buf_size();
229 	msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
230 
231 	spi_data = calloc(1, sizeof(*spi_data));
232 	if (!spi_data) {
233 		msg_perr("Unable to allocated space for SPI master data\n");
234 		goto init_err;
235 	}
236 	spi_data->fd = fd;
237 	spi_data->max_kernel_buf_size = max_kernel_buf_size;
238 
239 	return register_spi_master(&spi_master_linux, spi_data);
240 
241 init_err:
242 	close(fd);
243 	return 1;
244 }
245 
246 const struct programmer_entry programmer_linux_spi = {
247 	.name			= "linux_spi",
248 	.type			= OTHER,
249 	.devs.note		= "Device files /dev/spidev*.*\n",
250 	.init			= linux_spi_init,
251 };
252