blob: 2bbd576815d6d0a011e3dfab35d67c41c5bac285 [file] [log] [blame]
Nico Huber8d2c0df2024-01-14 23:39:40 +01001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2024 Nico Huber <nico.h@gmx.de>
5 *
6 * based on linux_gpio_spi.c
7 * Copyright (C) 2023 Steve Markgraf <steve@steve-m.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <stdlib.h>
21#include <string.h>
22#include <limits.h>
23#include <errno.h>
24#include <gpiod.h>
25#include "programmer.h"
Nico Huberd16a9112024-01-07 00:11:44 +010026#include "bitbang_spi.h"
Nico Huber8d2c0df2024-01-14 23:39:40 +010027#include "spi.h"
28#include "flash.h"
29
30#define CONSUMER "flashprog"
31
32enum {
33 CS = 0,
34 SCK = 1,
35 MOSI = 2,
36 MISO = 3,
Nico Huberfc7c13c2024-01-14 23:39:40 +010037 IO0 = 2,
38 IO1 = 3,
39 IO2 = 4,
40 IO3 = 5,
Nico Huber8d2c0df2024-01-14 23:39:40 +010041 MAX_LINES
42};
43
44struct linux_gpio_spi {
45 struct gpiod_chip *chip;
46 struct gpiod_line_request *lines;
Nico Huberfc7c13c2024-01-14 23:39:40 +010047 struct gpiod_line_config *single;
48 struct gpiod_line_config *multi_in;
49 struct gpiod_line_config *multi_out;
50 struct gpiod_line_config *current_config;
Nico Huber8d2c0df2024-01-14 23:39:40 +010051 unsigned int offsets[MAX_LINES];
Nico Huberfc7c13c2024-01-14 23:39:40 +010052 unsigned int io_lines;
Nico Huber8d2c0df2024-01-14 23:39:40 +010053};
54
Nico Huberfc7c13c2024-01-14 23:39:40 +010055static int ensure_spi_mode(struct linux_gpio_spi *gpio_spi, struct gpiod_line_config *config)
56{
57 if (gpio_spi->current_config == config)
58 return 0;
59
60 const int ret = gpiod_line_request_reconfigure_lines(gpio_spi->lines, config);
61 if (ret < 0)
62 msg_perr("Switching line config failed: %s\n", strerror(errno));
63 else
64 gpio_spi->current_config = config;
65
66 return ret;
67}
68
Nico Huber8d2c0df2024-01-14 23:39:40 +010069static void linux_gpio_spi_bitbang_set_cs(int val, void *data)
70{
71 const struct linux_gpio_spi *const gpio_spi = data;
72
73 if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[CS], val) < 0)
74 msg_perr("Setting cs line failed: %s\n", strerror(errno));
75}
76
77static void linux_gpio_spi_bitbang_set_sck(int val, void *data)
78{
79 const struct linux_gpio_spi *const gpio_spi = data;
80
81 if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[SCK], val) < 0)
82 msg_perr("Setting sck line failed: %s\n", strerror(errno));
83}
84
85static void linux_gpio_spi_bitbang_set_mosi(int val, void *data)
86{
87 struct linux_gpio_spi *const gpio_spi = data;
88
Nico Huberfc7c13c2024-01-14 23:39:40 +010089 if (ensure_spi_mode(gpio_spi, gpio_spi->single) < 0)
90 return;
91
Nico Huber8d2c0df2024-01-14 23:39:40 +010092 if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[MOSI], val) < 0)
93 msg_perr("Setting mosi line failed: %s\n", strerror(errno));
94}
95
96static int linux_gpio_spi_bitbang_get_miso(void *data)
97{
98 struct linux_gpio_spi *const gpio_spi = data;
99
Nico Huberfc7c13c2024-01-14 23:39:40 +0100100 if (ensure_spi_mode(gpio_spi, gpio_spi->single) < 0)
101 return -1;
102
Nico Huber8d2c0df2024-01-14 23:39:40 +0100103 const enum gpiod_line_value ret =
104 gpiod_line_request_get_value(gpio_spi->lines, gpio_spi->offsets[MISO]);
105 if (ret < 0)
106 msg_perr("Getting miso line failed: %s\n", strerror(errno));
107 return ret;
108}
109
110static void linux_gpio_spi_bitbang_set_sck_set_mosi(int sck, int mosi, void *data)
111{
112 struct linux_gpio_spi *const gpio_spi = data;
113
Nico Huberfc7c13c2024-01-14 23:39:40 +0100114 if (ensure_spi_mode(gpio_spi, gpio_spi->single) < 0)
115 return;
116
Nico Huber8d2c0df2024-01-14 23:39:40 +0100117 enum gpiod_line_value vals[] = { sck, mosi };
118 if (gpiod_line_request_set_values_subset(gpio_spi->lines, 2, &gpio_spi->offsets[SCK], vals) < 0)
119 msg_perr("Setting sck/mosi lines failed: %s\n", strerror(errno));
120}
121
Nico Huberfc7c13c2024-01-14 23:39:40 +0100122static void linux_gpio_spi_bitbang_set_sck_set_multi_io(int sck, int io, void *data)
123{
124 struct linux_gpio_spi *const gpio_spi = data;
125
126 if (ensure_spi_mode(gpio_spi, gpio_spi->multi_out) < 0)
127 return;
128
129 enum gpiod_line_value vals[] = { sck, io & 1, io >> 1 & 1, io >> 2 & 1, io >> 3 & 1 };
130 if (gpiod_line_request_set_values_subset(gpio_spi->lines,
131 gpio_spi->io_lines + 1, &gpio_spi->offsets[SCK], vals) < 0)
132 msg_perr("Setting sck/io lines failed: %s\n", strerror(errno));
133}
134
135static int linux_gpio_spi_bitbang_set_sck_get_multi_io(int sck, void *data)
136{
137 struct linux_gpio_spi *const gpio_spi = data;
138
139 if (ensure_spi_mode(gpio_spi, gpio_spi->multi_in) < 0)
140 return -1;
141
142 linux_gpio_spi_bitbang_set_sck(sck, data);
143
144 enum gpiod_line_value vals[4] = { 0, };
145 const int ret = gpiod_line_request_get_values_subset(gpio_spi->lines,
146 gpio_spi->io_lines, &gpio_spi->offsets[IO0], vals);
147 if (ret < 0) {
148 msg_perr("Getting io lines failed: %s\n", strerror(errno));
149 return -1;
150 }
151
152 return vals[0] | vals[1] << 1 | vals[2] << 2 | vals[3] << 3;
153}
154
155static void linux_gpio_spi_bitbang_set_idle_io(void *data)
156{
157 struct linux_gpio_spi *const gpio_spi = data;
158
159 (void)ensure_spi_mode(gpio_spi, gpio_spi->multi_in);
160}
161
Nico Huber8d2c0df2024-01-14 23:39:40 +0100162static struct bitbang_spi_master bitbang_spi_master_gpiod = {
163 .set_cs = linux_gpio_spi_bitbang_set_cs,
164 .set_sck = linux_gpio_spi_bitbang_set_sck,
165 .set_mosi = linux_gpio_spi_bitbang_set_mosi,
166 .get_miso = linux_gpio_spi_bitbang_get_miso,
167 .set_sck_set_mosi = linux_gpio_spi_bitbang_set_sck_set_mosi,
Nico Huberfc7c13c2024-01-14 23:39:40 +0100168 .set_sck_set_dual_io = linux_gpio_spi_bitbang_set_sck_set_multi_io,
169 .set_sck_get_dual_io = linux_gpio_spi_bitbang_set_sck_get_multi_io,
170 .set_idle_io = linux_gpio_spi_bitbang_set_idle_io,
Nico Huber8d2c0df2024-01-14 23:39:40 +0100171};
172
173static int linux_gpio_spi_shutdown(void *data)
174{
175 struct linux_gpio_spi *gpio_spi = data;
176
Nico Huberfc7c13c2024-01-14 23:39:40 +0100177 if (gpio_spi->multi_out)
178 gpiod_line_config_free(gpio_spi->multi_out);
179 if (gpio_spi->multi_in)
180 gpiod_line_config_free(gpio_spi->multi_in);
181 if (gpio_spi->single)
182 gpiod_line_config_free(gpio_spi->single);
Nico Huber8d2c0df2024-01-14 23:39:40 +0100183 if (gpio_spi->lines)
184 gpiod_line_request_release(gpio_spi->lines);
185 if (gpio_spi->chip)
186 gpiod_chip_close(gpio_spi->chip);
187
188 free(data);
189
190 return 0;
191}
192
Nico Hubere3a26882023-01-11 21:45:51 +0100193static int linux_gpio_spi_init(struct flashprog_programmer *const prog)
Nico Huber8d2c0df2024-01-14 23:39:40 +0100194{
Nico Huberfc7c13c2024-01-14 23:39:40 +0100195 struct param {
196 const char *names[2];
197 bool required;
198 };
199 static const struct param int_params[] = {
200 { .names = { "cs", }, .required = true, },
201 { .names = { "sck", }, .required = true, },
202 { .names = { "mosi", "io0", }, .required = true, },
203 { .names = { "miso", "io1", }, .required = true, },
204 { .names = { "io2", }, .required = false, },
205 { .names = { "io3", }, .required = false, },
206 { .names = { "gpiochip", }, .required = false, },
207 };
208
Nico Huber8d2c0df2024-01-14 23:39:40 +0100209 struct linux_gpio_spi *gpio_spi = NULL;
Nico Huberfc7c13c2024-01-14 23:39:40 +0100210 unsigned int param_int[ARRAY_SIZE(int_params)];
211 unsigned int i, j;
Nico Huber8d2c0df2024-01-14 23:39:40 +0100212 int r;
213
214 gpio_spi = calloc(1, sizeof(*gpio_spi));
215 if (!gpio_spi) {
216 msg_perr("Unable to allocate space for SPI master data\n");
217 return SPI_GENERIC_ERROR;
218 }
219
Nico Huberfc7c13c2024-01-14 23:39:40 +0100220 for (i = 0; i < ARRAY_SIZE(int_params); i++) {
221 const char *param_name = int_params[i].names[0];
222 char *param = NULL, *endptr;
223
224 for (j = 0; j < 2 && int_params[i].names[j]; ++j) {
225 char *const p = extract_programmer_param(int_params[i].names[j]);
226 if (param && p) {
227 msg_perr("Parameters `%s' and `%s' are mutually exclusive.\n",
228 int_params[i].names[0], int_params[i].names[1]);
229 free(param);
230 free(p);
231 goto err_exit;
232 }
233 if (p) {
234 param_name = int_params[i].names[j];
235 param = p;
236 }
237 }
238
Nico Huber8d2c0df2024-01-14 23:39:40 +0100239 r = 1;
240
241 if (param) {
242 errno = 0;
243 param_int[i] = strtoul(param, &endptr, 10);
244 r = (*endptr != '\0') || (errno != 0);
245 free(param);
246 } else {
247 param_int[i] = UINT_MAX;
248 }
249
Nico Huberfc7c13c2024-01-14 23:39:40 +0100250 if ((int_params[i].required || param) && r) {
251 msg_perr("Invalid or missing required programmer parameter "
252 "%s=<n>\n", param_name);
Nico Huber8d2c0df2024-01-14 23:39:40 +0100253 goto err_exit;
254 }
255 }
256
257 char *const dev = extract_programmer_param("dev");
258 const unsigned int gpiochip = param_int[MAX_LINES];
259 if (!dev && gpiochip == UINT_MAX) {
260 msg_perr("Either a `dev' or `gpiochip' parameter must be specified.\n"
261 "e.g. `-p linux_gpio2_spi:dev=/dev/gpiochip0'.\n");
262 goto err_exit;
263 }
264 if (dev && gpiochip != UINT_MAX) {
265 msg_perr("Only one of `dev' or `gpiochip' parameters can be specified.\n");
266 goto free_dev_exit;
267 }
268
Nico Huberfc7c13c2024-01-14 23:39:40 +0100269 if ((param_int[IO2] == UINT_MAX) ^ (param_int[IO3] == UINT_MAX)) {
270 msg_perr("Both `io2' and `io3' are required for quad i/o.\n");
271 goto free_dev_exit;
272 }
273
Nico Huber8d2c0df2024-01-14 23:39:40 +0100274 const char *devpath;
275 char devpath_template[] = "/dev/gpiochipX";
276 if (dev) {
277 devpath = dev;
278 } else if (gpiochip > 9) {
279 msg_perr("Maximum `gpiochip' number supported is 9.\n");
280 goto err_exit;
281 } else {
282 devpath_template[13] = '0' + gpiochip;
283 devpath = devpath_template;
284 }
285
286 gpio_spi->chip = gpiod_chip_open(devpath);
287 if (!gpio_spi->chip) {
288 msg_perr("Failed to open gpiochip `%s': %s\n", devpath, strerror(errno));
289 goto free_dev_exit;
290 }
291 free(dev);
292
Nico Huberfc7c13c2024-01-14 23:39:40 +0100293 if (param_int[IO2] != UINT_MAX) {
294 bitbang_spi_master_gpiod.set_sck_set_quad_io = linux_gpio_spi_bitbang_set_sck_set_multi_io;
295 bitbang_spi_master_gpiod.set_sck_get_quad_io = linux_gpio_spi_bitbang_set_sck_get_multi_io;
296 gpio_spi->io_lines = 4;
297 } else {
298 gpio_spi->io_lines = 2;
299 }
300
Nico Huber8d2c0df2024-01-14 23:39:40 +0100301 struct gpiod_line_settings *const in = gpiod_line_settings_new();
302 struct gpiod_line_settings *const out = gpiod_line_settings_new();
Nico Huberfc7c13c2024-01-14 23:39:40 +0100303 gpio_spi->multi_out = gpiod_line_config_new();
304 gpio_spi->multi_in = gpiod_line_config_new();
305 gpio_spi->single = gpiod_line_config_new();
306 if (!in || !out || !gpio_spi->multi_out || !gpio_spi->multi_in || !gpio_spi->single) {
Nico Huber8d2c0df2024-01-14 23:39:40 +0100307 msg_perr("Unable to allocate space for GPIO line config\n");
Nico Huber8d2c0df2024-01-14 23:39:40 +0100308 if (out)
309 gpiod_line_settings_free(out);
310 if (in)
311 gpiod_line_settings_free(in);
312 goto err_exit;
313 }
314
315 gpiod_line_settings_set_direction(in, GPIOD_LINE_DIRECTION_INPUT);
316 gpiod_line_settings_set_direction(out, GPIOD_LINE_DIRECTION_OUTPUT);
317
Nico Huberfc7c13c2024-01-14 23:39:40 +0100318 gpiod_line_config_add_line_settings(gpio_spi->single, &param_int[CS], 3, out);
319 gpiod_line_config_add_line_settings(gpio_spi->single, &param_int[MISO], gpio_spi->io_lines - 1, in);
320
321 gpiod_line_config_add_line_settings(gpio_spi->multi_in, &param_int[CS], 2, out);
322 gpiod_line_config_add_line_settings(gpio_spi->multi_in, &param_int[IO0], gpio_spi->io_lines, in);
323
324 gpiod_line_config_add_line_settings(gpio_spi->multi_out, &param_int[CS], 2 + gpio_spi->io_lines, out);
Nico Huber8d2c0df2024-01-14 23:39:40 +0100325
326 gpiod_line_settings_free(out);
327 gpiod_line_settings_free(in);
328
Nico Huberfc7c13c2024-01-14 23:39:40 +0100329 gpio_spi->lines = gpiod_chip_request_lines(gpio_spi->chip, NULL, gpio_spi->single);
Nico Huber8d2c0df2024-01-14 23:39:40 +0100330 if (!gpio_spi->lines) {
331 msg_perr("Failed to acquire GPIO lines\n");
332 goto err_exit;
333 }
Nico Huberfc7c13c2024-01-14 23:39:40 +0100334 gpio_spi->current_config = gpio_spi->single;
Nico Huber8d2c0df2024-01-14 23:39:40 +0100335
336 memcpy(gpio_spi->offsets, param_int, sizeof(gpio_spi->offsets));
337
338 if (register_shutdown(linux_gpio_spi_shutdown, gpio_spi))
339 goto err_exit;
340
341 /* shutdown function does the cleanup */
342 return register_spi_bitbang_master(&bitbang_spi_master_gpiod, gpio_spi);
343
344free_dev_exit:
345 free(dev);
346err_exit:
347 linux_gpio_spi_shutdown(gpio_spi);
348 return SPI_GENERIC_ERROR;
349}
350
351const struct programmer_entry programmer_linux_gpio_spi = {
352 .name = "linux_gpio_spi",
353 .type = OTHER,
354 .devs.note = "Device file /dev/gpiochip<n>\n",
355 .init = linux_gpio_spi_init,
356};