Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2018 Miklós Márton martonmiklosqdev@gmail.com |
| 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 | |
| 18 | #include <ctype.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <ni845x.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "flash.h" |
| 27 | #include "programmer.h" |
| 28 | #include "spi.h" |
| 29 | |
| 30 | #define NI845x_FIND_DEVICE_NO_DEVICE_FOUND -301701 |
| 31 | |
| 32 | enum USB845x_type { |
| 33 | USB8451 = 0x7166, |
| 34 | USB8452 = 0x7514, |
| 35 | Unknown_NI845X_Device |
| 36 | }; |
| 37 | |
| 38 | enum voltage_coerce_mode { |
| 39 | USE_LOWER, |
| 40 | USE_HIGHER |
| 41 | }; |
| 42 | |
| 43 | static const struct spi_master spi_programmer_ni845x; |
| 44 | |
| 45 | static unsigned char CS_number; // use chip select 0 as default |
| 46 | static enum USB845x_type device_pid = Unknown_NI845X_Device; |
| 47 | |
| 48 | static uInt32 device_handle; |
| 49 | static NiHandle configuration_handle; |
| 50 | static uint16_t io_voltage_in_mV; |
| 51 | static bool ignore_io_voltage_limits; |
| 52 | |
| 53 | static int ni845x_spi_shutdown(void *data); |
| 54 | static int32 ni845x_spi_open_resource(char *resource_handle, uInt32 *opened_handle); |
| 55 | static void ni845x_spi_print_available_devices(void); |
| 56 | |
| 57 | // USB-8452 supported voltages, keep this array in ascending order! |
| 58 | static const uint8_t usb8452_io_voltages_in_100mV[5] = { |
| 59 | kNi845x12Volts, |
| 60 | kNi845x15Volts, |
| 61 | kNi845x18Volts, |
| 62 | kNi845x25Volts, |
| 63 | kNi845x33Volts |
| 64 | }; |
| 65 | |
| 66 | /* Copied from dediprog.c */ |
| 67 | /* Might be useful for other USB devices as well. static for now. */ |
| 68 | static int parse_voltage(char *voltage) |
| 69 | { |
| 70 | char *tmp = NULL; |
| 71 | int i; |
| 72 | int millivolt = 0, fraction = 0; |
| 73 | |
| 74 | if (!voltage || !strlen(voltage)) { |
| 75 | msg_perr("Empty voltage= specified.\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 79 | voltage = tmp; |
| 80 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 81 | * to be in decimal notation. |
| 82 | */ |
| 83 | if ((*voltage == '.') || (*voltage == ',')) { |
| 84 | voltage++; |
| 85 | for (i = 0; i < 3; i++) { |
| 86 | fraction *= 10; |
| 87 | /* Don't advance if the current character is invalid, |
| 88 | * but continue multiplying. |
| 89 | */ |
| 90 | if ((*voltage < '0') || (*voltage > '9')) |
| 91 | continue; |
| 92 | fraction += *voltage - '0'; |
| 93 | voltage++; |
| 94 | } |
| 95 | /* Throw away remaining digits. */ |
| 96 | voltage += strspn(voltage, "0123456789"); |
| 97 | } |
| 98 | /* The remaining string must be empty or "mV" or "V". */ |
| 99 | tolower_string(voltage); |
| 100 | |
| 101 | /* No unit or "V". */ |
| 102 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 103 | millivolt *= 1000; |
| 104 | millivolt += fraction; |
| 105 | } else if (!strncmp(voltage, "mv", 2) || !strncmp(voltage, "millivolt", 9)) { |
| 106 | /* No adjustment. fraction is discarded. */ |
| 107 | } else { |
| 108 | /* Garbage at the end of the string. */ |
| 109 | msg_perr("Garbage voltage= specified.\n"); |
| 110 | return -1; |
| 111 | } |
| 112 | return millivolt; |
| 113 | } |
| 114 | |
| 115 | static void ni845x_report_error(const char *const func, const int32 err) |
| 116 | { |
| 117 | static char buf[1024]; |
| 118 | |
| 119 | ni845xStatusToString(err, sizeof(buf), buf); |
| 120 | msg_perr("%s failed with: %s (%d)\n", func, buf, (int)err); |
| 121 | } |
| 122 | |
| 123 | static void ni845x_report_warning(const char *const func, const int32 err) |
| 124 | { |
| 125 | static char buf[1024]; |
| 126 | |
| 127 | ni845xStatusToString(err, sizeof(buf), buf); |
| 128 | msg_pwarn("%s failed with: %s (%d)\n", func, buf, (int)err); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param serial a null terminated string containing the serial number of the specific device or NULL |
Martin Roth | f6c1cb1 | 2022-03-15 10:55:25 -0600 | [diff] [blame] | 133 | * @return the 0 on successful completion, negative error code on failure |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 134 | */ |
| 135 | static int ni845x_spi_open(const char *serial, uInt32 *return_handle) |
| 136 | { |
| 137 | char resource_name[256]; |
| 138 | NiHandle device_find_handle; |
| 139 | uInt32 found_devices_count = 0; |
| 140 | int32 tmp = 0; |
| 141 | |
| 142 | unsigned int vid, pid, usb_bus; |
| 143 | unsigned long int serial_as_number; |
| 144 | int ret = -1; |
| 145 | |
| 146 | tmp = ni845xFindDevice(resource_name, &device_find_handle, &found_devices_count); |
| 147 | if (tmp != 0) { |
Martin Roth | f6c1cb1 | 2022-03-15 10:55:25 -0600 | [diff] [blame] | 148 | // suppress warning if no device found |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 149 | if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND) |
| 150 | ni845x_report_error("ni845xFindDevice", tmp); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | for (; found_devices_count; --found_devices_count) { |
| 155 | // Read the serial number and the PID here |
| 156 | // VISA resource name format example: |
| 157 | // USB0::0x3923::0x7514::DEADBEEF::RAW |
| 158 | // where the 0x7514 is the PID |
| 159 | // and the DEADBEEF is the serial of the device |
| 160 | if (sscanf(resource_name, |
| 161 | "USB%u::0x%04X::0x%04X::%08lX::RAW", |
| 162 | &usb_bus, &vid, &pid, &serial_as_number) != 4) { |
| 163 | // malformed resource string detected |
| 164 | msg_pwarn("Warning: Unable to parse the %s NI-845x resource string.\n", |
| 165 | resource_name); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 166 | msg_pwarn("Please report a bug at flashprog@flashprog.org\n"); |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 167 | continue; |
| 168 | } |
| 169 | |
| 170 | device_pid = pid; |
| 171 | |
| 172 | if (!serial || strtol(serial, NULL, 16) == serial_as_number) |
| 173 | break; |
| 174 | |
| 175 | if (found_devices_count > 1) { |
| 176 | tmp = ni845xFindDeviceNext(device_find_handle, resource_name); |
| 177 | if (tmp) { |
| 178 | ni845x_report_error("ni845xFindDeviceNext", tmp); |
| 179 | goto _close_ret; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (found_devices_count) |
| 185 | ret = ni845x_spi_open_resource(resource_name, return_handle); |
| 186 | |
| 187 | _close_ret: |
| 188 | tmp = ni845xCloseFindDeviceHandle(device_find_handle); |
| 189 | if (tmp) { |
| 190 | ni845x_report_error("ni845xCloseFindDeviceHandle", tmp); |
| 191 | return -1; |
| 192 | } |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @brief ni845x_spi_open_resource |
| 198 | * @param resource_handle the resource handle returned by the ni845xFindDevice or ni845xFindDeviceNext |
| 199 | * @param opened_handle the opened handle from the ni845xOpen |
| 200 | * @return the 0 on successful competition, negative error code on failure positive code on warning |
| 201 | */ |
| 202 | static int32 ni845x_spi_open_resource(char *resource_handle, uInt32 *opened_handle) |
| 203 | { |
| 204 | // NI-845x driver loads the FPGA bitfile at the first time |
| 205 | // which can take couple seconds |
| 206 | if (device_pid == USB8452) |
| 207 | msg_pwarn("Opening NI-8452, this might take a while for the first time\n"); |
| 208 | |
| 209 | int32 tmp = ni845xOpen(resource_handle, opened_handle); |
| 210 | |
| 211 | if (tmp < 0) |
| 212 | ni845x_report_error("ni845xOpen", tmp); |
| 213 | else if (tmp > 0) |
| 214 | ni845x_report_warning("ni845xOpen", tmp); |
| 215 | return tmp; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @brief usb8452_spi_set_io_voltage sets the IO voltage for the USB-8452 devices |
| 220 | * @param requested_io_voltage_mV the desired IO voltage in mVolts |
| 221 | * @param set_io_voltage_mV the IO voltage which was set in mVolts |
| 222 | * @param coerce_mode if set to USE_LOWER the closest supported IO voltage which is lower or equal to |
Martin Roth | f6c1cb1 | 2022-03-15 10:55:25 -0600 | [diff] [blame] | 223 | * the requested_io_voltage_mV will be selected. Otherwise the next closest supported voltage will be chosen |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 224 | * which is higher or equal to the requested_io_voltage_mV. |
| 225 | * @return 0 on success, negative on error, positive on warning |
| 226 | */ |
| 227 | static int usb8452_spi_set_io_voltage(uint16_t requested_io_voltage_mV, |
| 228 | uint16_t *set_io_voltage_mV, |
| 229 | enum voltage_coerce_mode coerce_mode) |
| 230 | { |
| 231 | int i = 0; |
| 232 | uint8_t selected_voltage_100mV = 0; |
| 233 | uint8_t requested_io_voltage_100mV = 0; |
| 234 | |
| 235 | if (device_pid == USB8451) { |
| 236 | io_voltage_in_mV = 3300; |
| 237 | msg_pwarn("USB-8451 does not support the changing of the SPI IO voltage\n"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | // limit the IO voltage to 3.3V |
| 242 | if (requested_io_voltage_mV > 3300) { |
| 243 | msg_pinfo("USB-8452 maximum IO voltage is 3.3V\n"); |
| 244 | return -1; |
| 245 | } |
| 246 | requested_io_voltage_100mV = (requested_io_voltage_mV / 100.0f); |
| 247 | |
| 248 | // usb8452_io_voltages_in_100mV contains the supported voltage levels in increasing order |
| 249 | for (i = (ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1); i > 0; --i) { |
| 250 | if (requested_io_voltage_100mV >= usb8452_io_voltages_in_100mV[i]) |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | if (coerce_mode == USE_LOWER) { |
| 255 | if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) { |
| 256 | msg_perr("Unable to set the USB-8452 IO voltage below %.1fV " |
| 257 | "(the minimum supported IO voltage is %.1fV)\n", |
| 258 | (float)requested_io_voltage_100mV / 10.0f, |
| 259 | (float)usb8452_io_voltages_in_100mV[0] / 10.0f); |
| 260 | return -1; |
| 261 | } |
| 262 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i]; |
| 263 | } else { |
| 264 | if (i == ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1) |
| 265 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i]; |
| 266 | else |
| 267 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i + 1]; |
| 268 | } |
| 269 | |
| 270 | if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) { |
| 271 | /* unsupported / would have to round up */ |
| 272 | msg_pwarn("The USB-8452 does not support the %.1fV IO voltage\n", |
| 273 | requested_io_voltage_mV / 1000.0f); |
| 274 | selected_voltage_100mV = kNi845x12Volts; |
| 275 | msg_pwarn("The output voltage is set to 1.2V (this is the lowest voltage)\n"); |
| 276 | msg_pwarn("Supported IO voltages:\n"); |
| 277 | for (i = 0; i < ARRAY_SIZE(usb8452_io_voltages_in_100mV); i++) { |
| 278 | msg_pwarn("%.1fV", (float)usb8452_io_voltages_in_100mV[i] / 10.0f); |
| 279 | if (i != ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1) |
| 280 | msg_pwarn(", "); |
| 281 | } |
| 282 | msg_pwarn("\n"); |
| 283 | } else if (selected_voltage_100mV != requested_io_voltage_100mV) { |
| 284 | /* we rounded down/up */ |
| 285 | msg_pwarn("USB-8452 IO voltage forced to: %.1f V\n", |
| 286 | (float)selected_voltage_100mV / 10.0f); |
| 287 | } else { |
| 288 | /* exact match */ |
| 289 | msg_pinfo("USB-8452 IO voltage set to: %.1f V\n", |
| 290 | (float)selected_voltage_100mV / 10.0f); |
| 291 | } |
| 292 | |
| 293 | if (set_io_voltage_mV) |
| 294 | *set_io_voltage_mV = (selected_voltage_100mV * 100); |
| 295 | |
| 296 | i = ni845xSetIoVoltageLevel(device_handle, selected_voltage_100mV); |
| 297 | if (i != 0) { |
| 298 | ni845x_report_error("ni845xSetIoVoltageLevel", i); |
| 299 | return -1; |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @brief ni845x_spi_set_speed sets the SPI SCK speed |
| 306 | * @param SCK_freq_in_KHz SCK speed in KHz |
| 307 | * @return |
| 308 | */ |
| 309 | static int ni845x_spi_set_speed(uint16_t SCK_freq_in_KHz) |
| 310 | { |
| 311 | int32 i = ni845xSpiConfigurationSetClockRate(configuration_handle, SCK_freq_in_KHz); |
| 312 | uInt16 clock_freq_read_KHz; |
| 313 | |
| 314 | if (i != 0) { |
| 315 | ni845x_report_error("ni845xSpiConfigurationSetClockRate", i); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | // read back the clock frequency and notify the user if it is not the same as it was requested |
| 320 | i = ni845xSpiConfigurationGetClockRate(configuration_handle, &clock_freq_read_KHz); |
| 321 | if (i != 0) { |
| 322 | ni845x_report_error("ni845xSpiConfigurationGetClockRate", i); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | if (clock_freq_read_KHz != SCK_freq_in_KHz) { |
| 327 | msg_pinfo("SPI clock frequency forced to: %d KHz (requested: %d KHz)\n", |
| 328 | (int)clock_freq_read_KHz, (int)SCK_freq_in_KHz); |
| 329 | } else { |
| 330 | msg_pinfo("SPI clock frequency set to: %d KHz\n", (int)SCK_freq_in_KHz); |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @brief ni845x_spi_print_available_devices prints a list of the available devices |
| 337 | */ |
| 338 | static void ni845x_spi_print_available_devices(void) |
| 339 | { |
| 340 | char resource_handle[256], device_type_string[16]; |
| 341 | NiHandle device_find_handle; |
| 342 | uInt32 found_devices_count = 0; |
| 343 | int32 tmp = 0; |
| 344 | unsigned int pid, vid, usb_bus; |
| 345 | unsigned long int serial_as_number; |
| 346 | |
| 347 | tmp = ni845xFindDevice(resource_handle, &device_find_handle, &found_devices_count); |
| 348 | if (tmp != 0) { |
Martin Roth | f6c1cb1 | 2022-03-15 10:55:25 -0600 | [diff] [blame] | 349 | // suppress warning if no device found |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 350 | if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND) |
| 351 | ni845x_report_error("ni845xFindDevice", tmp); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (found_devices_count) { |
| 356 | msg_pinfo("Available devices:\n"); |
| 357 | do { |
| 358 | tmp = sscanf(resource_handle, "USB%d::0x%04X::0x%04X::%08lX::RAW", |
| 359 | &usb_bus, &vid, &pid, &serial_as_number); |
| 360 | if (tmp == 4) { |
| 361 | switch (pid) { |
| 362 | case USB8451: |
| 363 | snprintf(device_type_string, |
| 364 | ARRAY_SIZE(device_type_string), "USB-8451"); |
| 365 | break; |
| 366 | case USB8452: |
| 367 | snprintf(device_type_string, |
| 368 | ARRAY_SIZE(device_type_string), "USB-8452"); |
| 369 | break; |
| 370 | default: |
| 371 | snprintf(device_type_string, |
| 372 | ARRAY_SIZE(device_type_string), "Unknown device"); |
| 373 | break; |
| 374 | } |
| 375 | msg_pinfo("- %lX (%s)\n", serial_as_number, device_type_string); |
| 376 | |
| 377 | found_devices_count--; |
| 378 | if (found_devices_count) { |
| 379 | tmp = ni845xFindDeviceNext(device_find_handle, resource_handle); |
| 380 | if (tmp) |
| 381 | ni845x_report_error("ni845xFindDeviceNext", tmp); |
| 382 | } |
| 383 | } |
| 384 | } while (found_devices_count); |
| 385 | } |
| 386 | |
| 387 | tmp = ni845xCloseFindDeviceHandle(device_find_handle); |
| 388 | if (tmp) |
| 389 | ni845x_report_error("ni845xCloseFindDeviceHandle", tmp); |
| 390 | } |
| 391 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 392 | int ni845x_spi_init(struct flashprog_programmer *const prog) |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 393 | { |
| 394 | char *speed_str = NULL; |
| 395 | char *CS_str = NULL; |
| 396 | char *voltage = NULL; |
| 397 | char *endptr = NULL; |
| 398 | int requested_io_voltage_mV = 1200; // default the IO voltage to 1.2V |
| 399 | int spi_speed_KHz = 1000; // selecting 1 MHz SCK is a good bet |
| 400 | char *serial_number = NULL; // by default open the first connected device |
| 401 | char *ignore_io_voltage_limits_str = NULL; |
| 402 | int32 tmp = 0; |
| 403 | |
| 404 | // read the cs parameter (which Chip select should we use) |
| 405 | CS_str = extract_programmer_param("cs"); |
| 406 | if (CS_str) { |
| 407 | CS_number = CS_str[0] - '0'; |
| 408 | free(CS_str); |
| 409 | if (strlen(CS_str) > 1 || CS_number < 0 || 7 < CS_number) { |
| 410 | msg_perr("Only CS 0-7 supported\n"); |
| 411 | return 1; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | voltage = extract_programmer_param("voltage"); |
| 416 | if (voltage != NULL) { |
| 417 | requested_io_voltage_mV = parse_voltage(voltage); |
| 418 | free(voltage); |
| 419 | if (requested_io_voltage_mV < 0) |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | serial_number = extract_programmer_param("serial"); |
| 424 | |
| 425 | speed_str = extract_programmer_param("spispeed"); |
| 426 | if (speed_str) { |
| 427 | spi_speed_KHz = strtoul(speed_str, &endptr, 0); |
| 428 | if (*endptr) { |
| 429 | msg_perr("The spispeed parameter passed with invalid format: %s\n", |
| 430 | speed_str); |
| 431 | msg_perr("Please pass the parameter with a simple number in kHz\n"); |
| 432 | return 1; |
| 433 | } |
| 434 | free(speed_str); |
| 435 | } |
| 436 | |
| 437 | ignore_io_voltage_limits = false; |
| 438 | ignore_io_voltage_limits_str = extract_programmer_param("ignore_io_voltage_limits"); |
| 439 | if (ignore_io_voltage_limits_str |
| 440 | && strcmp(ignore_io_voltage_limits_str, "yes") == 0) { |
| 441 | ignore_io_voltage_limits = true; |
| 442 | } |
| 443 | |
| 444 | if (ni845x_spi_open(serial_number, &device_handle)) { |
| 445 | if (serial_number) { |
| 446 | msg_pinfo("Could not find any connected NI USB-8451/8452 with serialnumber: %s!\n", |
| 447 | serial_number); |
| 448 | ni845x_spi_print_available_devices(); |
| 449 | msg_pinfo("Check the S/N field on the bottom of the device,\n" |
| 450 | "or use 'lsusb -v -d 3923:7166 | grep Serial' for USB-8451\n" |
| 451 | "or 'lsusb -v -d 3923:7514 | grep Serial' for USB-8452\n"); |
| 452 | free(serial_number); |
| 453 | } else { |
| 454 | msg_pinfo("Could not find any connected NI USB-845x device!\n"); |
| 455 | } |
| 456 | return 1; |
| 457 | } |
| 458 | free(serial_number); |
| 459 | |
| 460 | // open the SPI config handle |
| 461 | tmp = ni845xSpiConfigurationOpen(&configuration_handle); |
| 462 | if (tmp != 0) { |
| 463 | ni845x_report_error("ni845xSpiConfigurationOpen", tmp); |
| 464 | ni845x_spi_shutdown(NULL); |
| 465 | return 1; |
| 466 | } |
| 467 | |
| 468 | if (usb8452_spi_set_io_voltage(requested_io_voltage_mV, &io_voltage_in_mV, USE_LOWER) < 0) { |
| 469 | ni845x_spi_shutdown(NULL); |
| 470 | return 1; // no alert here usb8452_spi_set_io_voltage already printed that |
| 471 | } |
| 472 | |
| 473 | if (ni845x_spi_set_speed(spi_speed_KHz)) { |
| 474 | msg_perr("Unable to set SPI speed\n"); |
| 475 | ni845x_spi_shutdown(NULL); |
| 476 | return 1; |
| 477 | } |
| 478 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 479 | return register_spi_master(&spi_programmer_ni845x, 0, NULL); |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static int ni845x_spi_shutdown(void *data) |
| 483 | { |
| 484 | int32 ret = 0; |
| 485 | |
| 486 | if (configuration_handle != 0) { |
| 487 | ret = ni845xSpiConfigurationClose(configuration_handle); |
| 488 | if (ret) |
| 489 | ni845x_report_error("ni845xSpiConfigurationClose", ret); |
| 490 | } |
| 491 | |
| 492 | if (device_handle != 0) { |
| 493 | ret = ni845xClose(device_handle); |
| 494 | if (ret) |
| 495 | ni845x_report_error("ni845xClose", ret); |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static void ni845x_warn_over_max_voltage(const struct flashctx *flash) |
| 501 | { |
| 502 | if (device_pid == USB8451) { |
| 503 | msg_pwarn("The %s chip maximum voltage is %.1fV, while the USB-8451 " |
| 504 | "IO voltage levels are 3.3V.\n" |
| 505 | "Ignoring this because ignore_io_voltage_limits parameter is set.\n", |
| 506 | flash->chip->name, |
| 507 | flash->chip->voltage.max / 1000.0f); |
| 508 | } else if (device_pid == USB8452) { |
| 509 | msg_pwarn("The %s chip maximum voltage is %.1fV, while the USB-8452 " |
| 510 | "IO voltage is set to %.1fV.\n" |
| 511 | "Ignoring this because ignore_io_voltage_limits parameter is set.\n", |
| 512 | flash->chip->name, |
| 513 | flash->chip->voltage.max / 1000.0f, |
| 514 | io_voltage_in_mV / 1000.0f); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static int ni845x_spi_io_voltage_check(const struct flashctx *flash) |
| 519 | { |
| 520 | static bool first_transmit = true; |
| 521 | |
| 522 | if (first_transmit && flash->chip) { |
| 523 | first_transmit = false; |
| 524 | if (io_voltage_in_mV > flash->chip->voltage.max) { |
| 525 | if (ignore_io_voltage_limits) { |
| 526 | ni845x_warn_over_max_voltage(flash); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | if (device_pid == USB8451) { |
| 531 | msg_perr("The %s chip maximum voltage is %.1fV, while the USB-8451 " |
| 532 | "IO voltage levels are 3.3V.\nAborting operations\n", |
| 533 | flash->chip->name, |
| 534 | flash->chip->voltage.max / 1000.0f); |
| 535 | return -1; |
| 536 | } else if (device_pid == USB8452) { |
| 537 | msg_perr("Lowering IO voltage because the %s chip maximum voltage is %.1fV, " |
| 538 | "(%.1fV was set)\n", |
| 539 | flash->chip->name, |
| 540 | flash->chip->voltage.max / 1000.0f, |
| 541 | io_voltage_in_mV / 1000.0f); |
| 542 | if (usb8452_spi_set_io_voltage(flash->chip->voltage.max, |
| 543 | &io_voltage_in_mV, |
| 544 | USE_LOWER)) { |
| 545 | msg_perr("Unable to lower the IO voltage below " |
| 546 | "the chip's maximum voltage\n"); |
| 547 | return -1; |
| 548 | } |
| 549 | } |
| 550 | } else if (io_voltage_in_mV < flash->chip->voltage.min) { |
| 551 | if (device_pid == USB8451) { |
| 552 | msg_pwarn("Flash operations might be unreliable, because the %s chip's " |
| 553 | "minimum voltage is %.1fV, while the USB-8451's " |
| 554 | "IO voltage levels are 3.3V.\n", |
| 555 | flash->chip->name, |
| 556 | flash->chip->voltage.min / 1000.0f); |
| 557 | return ignore_io_voltage_limits ? 0 : -1; |
| 558 | } else if (device_pid == USB8452) { |
| 559 | msg_pwarn("Raising the IO voltage because the %s chip's " |
| 560 | "minimum voltage is %.1fV, " |
| 561 | "(%.1fV was set)\n", |
| 562 | flash->chip->name, |
| 563 | flash->chip->voltage.min / 1000.0f, |
| 564 | io_voltage_in_mV / 1000.0f); |
| 565 | if (usb8452_spi_set_io_voltage(flash->chip->voltage.min, |
| 566 | &io_voltage_in_mV, |
| 567 | USE_HIGHER)) { |
| 568 | msg_pwarn("Unable to raise the IO voltage above the chip's " |
| 569 | "minimum voltage\n" |
| 570 | "Flash operations might be unreliable.\n"); |
| 571 | return ignore_io_voltage_limits ? 0 : -1; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
Miklós Márton | a4e63e7 | 2021-08-17 22:53:40 +0200 | [diff] [blame] | 579 | static int ni845x_spi_transmit(const struct flashctx *flash, |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 580 | unsigned int write_cnt, |
| 581 | unsigned int read_cnt, |
| 582 | const unsigned char *write_arr, |
| 583 | unsigned char *read_arr) |
| 584 | { |
| 585 | uInt32 read_size = 0; |
| 586 | uInt8 *transfer_buffer = NULL; |
| 587 | int32 ret = 0; |
| 588 | |
| 589 | if (ni845x_spi_io_voltage_check(flash)) |
| 590 | return -1; |
| 591 | |
| 592 | transfer_buffer = calloc(write_cnt + read_cnt, sizeof(uInt8)); |
| 593 | if (transfer_buffer == NULL) { |
| 594 | msg_gerr("Memory allocation failed!\n"); |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | memcpy(transfer_buffer, write_arr, write_cnt); |
| 599 | |
| 600 | ret = ni845xSpiWriteRead(device_handle, |
| 601 | configuration_handle, |
| 602 | (write_cnt + read_cnt), transfer_buffer, &read_size, transfer_buffer); |
| 603 | if (ret < 0) { |
| 604 | // Negative specifies an error, meaning the function did not perform the expected behavior. |
| 605 | ni845x_report_error("ni845xSpiWriteRead", ret); |
| 606 | free(transfer_buffer); |
| 607 | return -1; |
| 608 | } else if (ret > 0) { |
| 609 | // Positive specifies a warning, meaning the function performed as expected, |
| 610 | // but a condition arose that might require attention. |
| 611 | ni845x_report_warning("ni845xSpiWriteRead", ret); |
| 612 | } |
| 613 | |
| 614 | if (read_cnt != 0 && read_arr != NULL) { |
| 615 | if ((read_cnt + write_cnt) != read_size) { |
Martin Roth | f6c1cb1 | 2022-03-15 10:55:25 -0600 | [diff] [blame] | 616 | msg_perr("%s: expected and returned read count mismatch: %u expected, %ld received\n", |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 617 | __func__, read_cnt, read_size); |
| 618 | free(transfer_buffer); |
| 619 | return -1; |
| 620 | } |
| 621 | memcpy(read_arr, &transfer_buffer[write_cnt], read_cnt); |
| 622 | } |
| 623 | free(transfer_buffer); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static const struct spi_master spi_programmer_ni845x = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 628 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 629 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 630 | .command = ni845x_spi_transmit, |
| 631 | .multicommand = default_spi_send_multicommand, |
| 632 | .read = default_spi_read, |
| 633 | .write_256 = default_spi_write_256, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 634 | .shutdown = ni845x_spi_shutdown, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 635 | .probe_opcode = default_spi_probe_opcode, |
Miklós Márton | 2d20d6d | 2018-01-30 20:20:15 +0100 | [diff] [blame] | 636 | }; |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 637 | |
| 638 | const struct programmer_entry programmer_ni845x_spi = { |
| 639 | .name = "ni845x_spi", |
| 640 | .type = OTHER, // choose other because NI-845x uses own USB implementation |
| 641 | .devs.note = "National Instruments USB-845x\n", |
| 642 | .init = ni845x_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 643 | }; |