blob: 1e95c16af9211fc81122933772f94a244446657e [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"
23#include "spi.h"
24#include "flash.h"
25
Nico Huberc3b02dc2023-08-12 01:13:45 +020026#define CONSUMER "flashprog"
Steve Markgraf61899472023-01-09 23:06:52 +010027
28struct linux_gpio_spi {
29 struct gpiod_chip *chip;
30 struct gpiod_line_bulk bulk;
31 struct gpiod_line *cs_line, *sck_line, *mosi_line, *miso_line;
32};
33
34static void linux_gpio_spi_bitbang_set_cs(int val, void *spi_data)
35{
36 struct linux_gpio_spi *data = spi_data;
37 if (gpiod_line_set_value(data->cs_line, val) < 0)
38 msg_perr("Setting cs line failed\n");
39}
40
41static void linux_gpio_spi_bitbang_set_sck(int val, void *spi_data)
42{
43 struct linux_gpio_spi *data = spi_data;
44 if (gpiod_line_set_value(data->sck_line, val) < 0)
45 msg_perr("Setting sck line failed\n");
46}
47
48static void linux_gpio_spi_bitbang_set_mosi(int val, void *spi_data)
49{
50 struct linux_gpio_spi *data = spi_data;
51 if (gpiod_line_set_value(data->mosi_line, val) < 0)
52 msg_perr("Setting sck line failed\n");
53}
54
55static int linux_gpio_spi_bitbang_get_miso(void *spi_data)
56{
57 struct linux_gpio_spi *data = spi_data;
58 int r = gpiod_line_get_value(data->miso_line);
59 if (r < 0)
60 msg_perr("Getting miso line failed\n");
61 return r;
62}
63
64static const struct bitbang_spi_master bitbang_spi_master_gpiod = {
65 .set_cs = linux_gpio_spi_bitbang_set_cs,
66 .set_sck = linux_gpio_spi_bitbang_set_sck,
67 .set_mosi = linux_gpio_spi_bitbang_set_mosi,
68 .get_miso = linux_gpio_spi_bitbang_get_miso,
69};
70
71static int linux_gpio_spi_shutdown(void *spi_data)
72{
73 struct linux_gpio_spi *data = spi_data;
74
75 if (gpiod_line_bulk_num_lines(&data->bulk) > 0)
76 gpiod_line_release_bulk(&data->bulk);
77
78 if (data->chip)
79 gpiod_chip_close(data->chip);
80
81 free(data);
82
83 return 0;
84}
85
86static int linux_gpio_spi_init(void)
87{
88 struct linux_gpio_spi *data = NULL;
89 struct gpiod_chip *chip = NULL;
90 const char *param_str[] = { "cs", "sck", "mosi", "miso", "gpiochip" };
91 const bool param_required[] = { true, true, true, true, false };
92 unsigned int param_int[ARRAY_SIZE(param_str)];
93 unsigned int i;
94 int r;
95
96 data = calloc(1, sizeof(*data));
97 if (!data) {
98 msg_perr("Unable to allocate space for SPI master data\n");
99 return 1;
100 }
101
102 for (i = 0; i < ARRAY_SIZE(param_str); i++) {
103 char *param = extract_programmer_param(param_str[i]);
104 char *endptr;
105 r = 1;
106
107 if (param) {
108 errno = 0;
109 param_int[i] = strtoul(param, &endptr, 10);
110 r = (*endptr != '\0') || (errno != 0);
111 free(param);
112 } else {
113 param_int[i] = UINT_MAX;
114 }
115
116 if ((param_required[i] || param) && r) {
117 msg_perr("Missing or invalid required programmer "
118 "parameter %s=<n>\n", param_str[i]);
119 goto err_exit;
120 }
121 }
122
123 char *const dev = extract_programmer_param("dev");
124 if (!dev && param_int[4] == UINT_MAX) {
125 msg_perr("Either a 'dev' or 'gpiochip' parameter must be specified.\n");
126 goto err_exit;
127 }
128 if (dev && param_int[4] != UINT_MAX) {
129 msg_perr("Only one of 'dev' or 'gpiochip' parameters can be specified.\n");
130 free(dev);
131 goto err_exit;
132 }
133
134 if (dev) {
135 chip = gpiod_chip_open(dev);
136 free(dev);
137 } else {
138 chip = gpiod_chip_open_by_number(param_int[4]);
139 }
140 if (!chip) {
141 msg_perr("Failed to open gpiochip: %s\n", strerror(errno));
142 goto err_exit;
143 }
144
145 data->chip = chip;
146
147 if (gpiod_chip_get_lines(chip, param_int, 4, &data->bulk)) {
148 msg_perr("Error getting GPIO lines\n");
149 goto err_exit;
150 }
151
152 data->cs_line = gpiod_line_bulk_get_line(&data->bulk, 0);
153 data->sck_line = gpiod_line_bulk_get_line(&data->bulk, 1);
154 data->mosi_line = gpiod_line_bulk_get_line(&data->bulk, 2);
155 data->miso_line = gpiod_line_bulk_get_line(&data->bulk, 3);
156
157 r = gpiod_line_request_output(data->cs_line, CONSUMER, 1);
158 r |= gpiod_line_request_output(data->sck_line, CONSUMER, 1);
159 r |= gpiod_line_request_output(data->mosi_line, CONSUMER, 1);
160 r |= gpiod_line_request_input(data->miso_line, CONSUMER);
161
162 if (r < 0) {
163 msg_perr("Requesting GPIO lines failed\n");
164 goto err_exit;
165 }
166
167 if (register_shutdown(linux_gpio_spi_shutdown, data))
168 goto err_exit;
169
170 /* shutdown function does the cleanup */
171 return register_spi_bitbang_master(&bitbang_spi_master_gpiod, data);
172
173err_exit:
174 linux_gpio_spi_shutdown(data);
175 return 1;
176}
177
178const struct programmer_entry programmer_linux_gpio_spi = {
179 .name = "linux_gpio_spi",
180 .type = OTHER,
181 .devs.note = "Device file /dev/gpiochip<n>\n",
182 .init = linux_gpio_spi_init,
183};