blob: 647f813372b87ce09771bfaf33a6c865f6160c9f [file] [log] [blame]
Steve Markgraf61899472023-01-09 23:06:52 +01001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2023 Steve Markgraf <steve@steve-m.de>
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <stdlib.h>
18#include <string.h>
19#include <limits.h>
20#include <errno.h>
21#include <gpiod.h>
22#include "programmer.h"
Nico Huberd16a9112024-01-07 00:11:44 +010023#include "bitbang_spi.h"
Steve Markgraf61899472023-01-09 23:06:52 +010024#include "spi.h"
25#include "flash.h"
26
Nico Huberc3b02dc2023-08-12 01:13:45 +020027#define CONSUMER "flashprog"
Steve Markgraf61899472023-01-09 23:06:52 +010028
29struct linux_gpio_spi {
30 struct gpiod_chip *chip;
31 struct gpiod_line_bulk bulk;
32 struct gpiod_line *cs_line, *sck_line, *mosi_line, *miso_line;
33};
34
35static void linux_gpio_spi_bitbang_set_cs(int val, void *spi_data)
36{
37 struct linux_gpio_spi *data = spi_data;
38 if (gpiod_line_set_value(data->cs_line, val) < 0)
39 msg_perr("Setting cs line failed\n");
40}
41
42static void linux_gpio_spi_bitbang_set_sck(int val, void *spi_data)
43{
44 struct linux_gpio_spi *data = spi_data;
45 if (gpiod_line_set_value(data->sck_line, val) < 0)
46 msg_perr("Setting sck line failed\n");
47}
48
49static void linux_gpio_spi_bitbang_set_mosi(int val, void *spi_data)
50{
51 struct linux_gpio_spi *data = spi_data;
52 if (gpiod_line_set_value(data->mosi_line, val) < 0)
53 msg_perr("Setting sck line failed\n");
54}
55
56static int linux_gpio_spi_bitbang_get_miso(void *spi_data)
57{
58 struct linux_gpio_spi *data = spi_data;
59 int r = gpiod_line_get_value(data->miso_line);
60 if (r < 0)
61 msg_perr("Getting miso line failed\n");
62 return r;
63}
64
65static const struct bitbang_spi_master bitbang_spi_master_gpiod = {
66 .set_cs = linux_gpio_spi_bitbang_set_cs,
67 .set_sck = linux_gpio_spi_bitbang_set_sck,
68 .set_mosi = linux_gpio_spi_bitbang_set_mosi,
69 .get_miso = linux_gpio_spi_bitbang_get_miso,
70};
71
72static int linux_gpio_spi_shutdown(void *spi_data)
73{
74 struct linux_gpio_spi *data = spi_data;
75
76 if (gpiod_line_bulk_num_lines(&data->bulk) > 0)
77 gpiod_line_release_bulk(&data->bulk);
78
79 if (data->chip)
80 gpiod_chip_close(data->chip);
81
82 free(data);
83
84 return 0;
85}
86
Nico Hubere3a26882023-01-11 21:45:51 +010087static int linux_gpio_spi_init(struct flashprog_programmer *const prog)
Steve Markgraf61899472023-01-09 23:06:52 +010088{
89 struct linux_gpio_spi *data = NULL;
90 struct gpiod_chip *chip = NULL;
91 const char *param_str[] = { "cs", "sck", "mosi", "miso", "gpiochip" };
92 const bool param_required[] = { true, true, true, true, false };
93 unsigned int param_int[ARRAY_SIZE(param_str)];
94 unsigned int i;
95 int r;
96
97 data = calloc(1, sizeof(*data));
98 if (!data) {
99 msg_perr("Unable to allocate space for SPI master data\n");
100 return 1;
101 }
102
103 for (i = 0; i < ARRAY_SIZE(param_str); i++) {
104 char *param = extract_programmer_param(param_str[i]);
105 char *endptr;
106 r = 1;
107
108 if (param) {
109 errno = 0;
110 param_int[i] = strtoul(param, &endptr, 10);
111 r = (*endptr != '\0') || (errno != 0);
112 free(param);
113 } else {
114 param_int[i] = UINT_MAX;
115 }
116
117 if ((param_required[i] || param) && r) {
118 msg_perr("Missing or invalid required programmer "
119 "parameter %s=<n>\n", param_str[i]);
120 goto err_exit;
121 }
122 }
123
124 char *const dev = extract_programmer_param("dev");
125 if (!dev && param_int[4] == UINT_MAX) {
126 msg_perr("Either a 'dev' or 'gpiochip' parameter must be specified.\n");
127 goto err_exit;
128 }
129 if (dev && param_int[4] != UINT_MAX) {
130 msg_perr("Only one of 'dev' or 'gpiochip' parameters can be specified.\n");
131 free(dev);
132 goto err_exit;
133 }
134
135 if (dev) {
136 chip = gpiod_chip_open(dev);
137 free(dev);
138 } else {
139 chip = gpiod_chip_open_by_number(param_int[4]);
140 }
141 if (!chip) {
142 msg_perr("Failed to open gpiochip: %s\n", strerror(errno));
143 goto err_exit;
144 }
145
146 data->chip = chip;
147
148 if (gpiod_chip_get_lines(chip, param_int, 4, &data->bulk)) {
149 msg_perr("Error getting GPIO lines\n");
150 goto err_exit;
151 }
152
153 data->cs_line = gpiod_line_bulk_get_line(&data->bulk, 0);
154 data->sck_line = gpiod_line_bulk_get_line(&data->bulk, 1);
155 data->mosi_line = gpiod_line_bulk_get_line(&data->bulk, 2);
156 data->miso_line = gpiod_line_bulk_get_line(&data->bulk, 3);
157
158 r = gpiod_line_request_output(data->cs_line, CONSUMER, 1);
159 r |= gpiod_line_request_output(data->sck_line, CONSUMER, 1);
160 r |= gpiod_line_request_output(data->mosi_line, CONSUMER, 1);
161 r |= gpiod_line_request_input(data->miso_line, CONSUMER);
162
163 if (r < 0) {
164 msg_perr("Requesting GPIO lines failed\n");
165 goto err_exit;
166 }
167
168 if (register_shutdown(linux_gpio_spi_shutdown, data))
169 goto err_exit;
170
171 /* shutdown function does the cleanup */
172 return register_spi_bitbang_master(&bitbang_spi_master_gpiod, data);
173
174err_exit:
175 linux_gpio_spi_shutdown(data);
176 return 1;
177}
178
179const struct programmer_entry programmer_linux_gpio_spi = {
180 .name = "linux_gpio_spi",
181 .type = OTHER,
182 .devs.note = "Device file /dev/gpiochip<n>\n",
183 .init = linux_gpio_spi_init,
184};