Nico Huber | 8d2c0df | 2024-01-14 23:39:40 +0100 | [diff] [blame] | 1 | /* |
| 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" |
| 26 | #include "spi.h" |
| 27 | #include "flash.h" |
| 28 | |
| 29 | #define CONSUMER "flashprog" |
| 30 | |
| 31 | enum { |
| 32 | CS = 0, |
| 33 | SCK = 1, |
| 34 | MOSI = 2, |
| 35 | MISO = 3, |
| 36 | MAX_LINES |
| 37 | }; |
| 38 | |
| 39 | struct linux_gpio_spi { |
| 40 | struct gpiod_chip *chip; |
| 41 | struct gpiod_line_request *lines; |
| 42 | unsigned int offsets[MAX_LINES]; |
| 43 | }; |
| 44 | |
| 45 | static void linux_gpio_spi_bitbang_set_cs(int val, void *data) |
| 46 | { |
| 47 | const struct linux_gpio_spi *const gpio_spi = data; |
| 48 | |
| 49 | if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[CS], val) < 0) |
| 50 | msg_perr("Setting cs line failed: %s\n", strerror(errno)); |
| 51 | } |
| 52 | |
| 53 | static void linux_gpio_spi_bitbang_set_sck(int val, void *data) |
| 54 | { |
| 55 | const struct linux_gpio_spi *const gpio_spi = data; |
| 56 | |
| 57 | if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[SCK], val) < 0) |
| 58 | msg_perr("Setting sck line failed: %s\n", strerror(errno)); |
| 59 | } |
| 60 | |
| 61 | static void linux_gpio_spi_bitbang_set_mosi(int val, void *data) |
| 62 | { |
| 63 | struct linux_gpio_spi *const gpio_spi = data; |
| 64 | |
| 65 | if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[MOSI], val) < 0) |
| 66 | msg_perr("Setting mosi line failed: %s\n", strerror(errno)); |
| 67 | } |
| 68 | |
| 69 | static int linux_gpio_spi_bitbang_get_miso(void *data) |
| 70 | { |
| 71 | struct linux_gpio_spi *const gpio_spi = data; |
| 72 | |
| 73 | const enum gpiod_line_value ret = |
| 74 | gpiod_line_request_get_value(gpio_spi->lines, gpio_spi->offsets[MISO]); |
| 75 | if (ret < 0) |
| 76 | msg_perr("Getting miso line failed: %s\n", strerror(errno)); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | static void linux_gpio_spi_bitbang_set_sck_set_mosi(int sck, int mosi, void *data) |
| 81 | { |
| 82 | struct linux_gpio_spi *const gpio_spi = data; |
| 83 | |
| 84 | enum gpiod_line_value vals[] = { sck, mosi }; |
| 85 | if (gpiod_line_request_set_values_subset(gpio_spi->lines, 2, &gpio_spi->offsets[SCK], vals) < 0) |
| 86 | msg_perr("Setting sck/mosi lines failed: %s\n", strerror(errno)); |
| 87 | } |
| 88 | |
| 89 | static struct bitbang_spi_master bitbang_spi_master_gpiod = { |
| 90 | .set_cs = linux_gpio_spi_bitbang_set_cs, |
| 91 | .set_sck = linux_gpio_spi_bitbang_set_sck, |
| 92 | .set_mosi = linux_gpio_spi_bitbang_set_mosi, |
| 93 | .get_miso = linux_gpio_spi_bitbang_get_miso, |
| 94 | .set_sck_set_mosi = linux_gpio_spi_bitbang_set_sck_set_mosi, |
| 95 | }; |
| 96 | |
| 97 | static int linux_gpio_spi_shutdown(void *data) |
| 98 | { |
| 99 | struct linux_gpio_spi *gpio_spi = data; |
| 100 | |
| 101 | if (gpio_spi->lines) |
| 102 | gpiod_line_request_release(gpio_spi->lines); |
| 103 | if (gpio_spi->chip) |
| 104 | gpiod_chip_close(gpio_spi->chip); |
| 105 | |
| 106 | free(data); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame^] | 111 | static int linux_gpio_spi_init(struct flashprog_programmer *const prog) |
Nico Huber | 8d2c0df | 2024-01-14 23:39:40 +0100 | [diff] [blame] | 112 | { |
| 113 | struct linux_gpio_spi *gpio_spi = NULL; |
| 114 | const char *param_str[] = { "cs", "sck", "mosi", "miso", "gpiochip" }; |
| 115 | const bool param_required[] = { true, true, true, true, false }; |
| 116 | unsigned int param_int[ARRAY_SIZE(param_str)]; |
| 117 | unsigned int i; |
| 118 | int r; |
| 119 | |
| 120 | gpio_spi = calloc(1, sizeof(*gpio_spi)); |
| 121 | if (!gpio_spi) { |
| 122 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 123 | return SPI_GENERIC_ERROR; |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < ARRAY_SIZE(param_str); i++) { |
| 127 | char *param = extract_programmer_param(param_str[i]); |
| 128 | char *endptr; |
| 129 | r = 1; |
| 130 | |
| 131 | if (param) { |
| 132 | errno = 0; |
| 133 | param_int[i] = strtoul(param, &endptr, 10); |
| 134 | r = (*endptr != '\0') || (errno != 0); |
| 135 | free(param); |
| 136 | } else { |
| 137 | param_int[i] = UINT_MAX; |
| 138 | } |
| 139 | |
| 140 | if ((param_required[i] || param) && r) { |
| 141 | msg_perr("Missing or invalid required programmer " |
| 142 | "parameter %s=<n>\n", param_str[i]); |
| 143 | goto err_exit; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | char *const dev = extract_programmer_param("dev"); |
| 148 | const unsigned int gpiochip = param_int[MAX_LINES]; |
| 149 | if (!dev && gpiochip == UINT_MAX) { |
| 150 | msg_perr("Either a `dev' or `gpiochip' parameter must be specified.\n" |
| 151 | "e.g. `-p linux_gpio2_spi:dev=/dev/gpiochip0'.\n"); |
| 152 | goto err_exit; |
| 153 | } |
| 154 | if (dev && gpiochip != UINT_MAX) { |
| 155 | msg_perr("Only one of `dev' or `gpiochip' parameters can be specified.\n"); |
| 156 | goto free_dev_exit; |
| 157 | } |
| 158 | |
| 159 | const char *devpath; |
| 160 | char devpath_template[] = "/dev/gpiochipX"; |
| 161 | if (dev) { |
| 162 | devpath = dev; |
| 163 | } else if (gpiochip > 9) { |
| 164 | msg_perr("Maximum `gpiochip' number supported is 9.\n"); |
| 165 | goto err_exit; |
| 166 | } else { |
| 167 | devpath_template[13] = '0' + gpiochip; |
| 168 | devpath = devpath_template; |
| 169 | } |
| 170 | |
| 171 | gpio_spi->chip = gpiod_chip_open(devpath); |
| 172 | if (!gpio_spi->chip) { |
| 173 | msg_perr("Failed to open gpiochip `%s': %s\n", devpath, strerror(errno)); |
| 174 | goto free_dev_exit; |
| 175 | } |
| 176 | free(dev); |
| 177 | |
| 178 | struct gpiod_line_settings *const in = gpiod_line_settings_new(); |
| 179 | struct gpiod_line_settings *const out = gpiod_line_settings_new(); |
| 180 | struct gpiod_line_config *const cfg = gpiod_line_config_new(); |
| 181 | if (!in || !out || !cfg) { |
| 182 | msg_perr("Unable to allocate space for GPIO line config\n"); |
| 183 | if (cfg) |
| 184 | gpiod_line_config_free(cfg); |
| 185 | if (out) |
| 186 | gpiod_line_settings_free(out); |
| 187 | if (in) |
| 188 | gpiod_line_settings_free(in); |
| 189 | goto err_exit; |
| 190 | } |
| 191 | |
| 192 | gpiod_line_settings_set_direction(in, GPIOD_LINE_DIRECTION_INPUT); |
| 193 | gpiod_line_settings_set_direction(out, GPIOD_LINE_DIRECTION_OUTPUT); |
| 194 | |
| 195 | gpiod_line_config_add_line_settings(cfg, ¶m_int[CS], 3, out); |
| 196 | gpiod_line_config_add_line_settings(cfg, ¶m_int[MISO], 1, in); |
| 197 | |
| 198 | gpiod_line_settings_free(out); |
| 199 | gpiod_line_settings_free(in); |
| 200 | |
| 201 | gpio_spi->lines = gpiod_chip_request_lines(gpio_spi->chip, NULL, cfg); |
| 202 | if (!gpio_spi->lines) { |
| 203 | msg_perr("Failed to acquire GPIO lines\n"); |
| 204 | goto err_exit; |
| 205 | } |
| 206 | |
| 207 | gpiod_line_config_free(cfg); |
| 208 | |
| 209 | memcpy(gpio_spi->offsets, param_int, sizeof(gpio_spi->offsets)); |
| 210 | |
| 211 | if (register_shutdown(linux_gpio_spi_shutdown, gpio_spi)) |
| 212 | goto err_exit; |
| 213 | |
| 214 | /* shutdown function does the cleanup */ |
| 215 | return register_spi_bitbang_master(&bitbang_spi_master_gpiod, gpio_spi); |
| 216 | |
| 217 | free_dev_exit: |
| 218 | free(dev); |
| 219 | err_exit: |
| 220 | linux_gpio_spi_shutdown(gpio_spi); |
| 221 | return SPI_GENERIC_ERROR; |
| 222 | } |
| 223 | |
| 224 | const struct programmer_entry programmer_linux_gpio_spi = { |
| 225 | .name = "linux_gpio_spi", |
| 226 | .type = OTHER, |
| 227 | .devs.note = "Device file /dev/gpiochip<n>\n", |
| 228 | .init = linux_gpio_spi_init, |
| 229 | }; |