blob: bec94574667b2e241f790da0acc2dbd8fb086f54 [file] [log] [blame]
Justin Chevrier66e554b2015-02-08 21:58:10 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 * Copyright (C) 2014 Justin Chevrier
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
Justin Chevrier66e554b2015-02-08 21:58:10 +000015 */
16
17/*
18 * Connections are as follows:
19 *
20 * +------+-----+----------+
21 * | SPI | Pin | PICkit2 |
22 * +------+-----+----------+
23 * | /CS | 1 | VPP/MCLR |
24 * | VCC | 2 | VDD |
25 * | GND | 3 | GND |
26 * | MISO | 4 | PGD |
27 * | SCLK | 5 | PDC |
28 * | MOSI | 6 | AUX |
29 * +------+-----+----------+
30 *
31 * Inspiration and some specifics of the interface came via the AVRDude
32 * PICkit2 code: https://github.com/steve-m/avrdude/blob/master/pickit2.c
33 */
34
35#include "platform.h"
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <limits.h>
41#include <errno.h>
Thomas Heijligenb221cd72019-04-05 15:08:35 +020042#include <libusb.h>
Justin Chevrier66e554b2015-02-08 21:58:10 +000043
44#include "flash.h"
45#include "chipdrivers.h"
46#include "programmer.h"
47#include "spi.h"
48
Thomas Heijligen4f5169d2021-05-04 15:32:17 +020049static const struct dev_entry devs_pickit2_spi[] = {
Stefan Taunerf31fe842016-02-22 08:59:15 +000050 {0x04D8, 0x0033, OK, "Microchip", "PICkit 2"},
51
Evgeny Zinoviev83c56b82019-11-05 17:47:43 +030052 {0}
Stefan Taunerf31fe842016-02-22 08:59:15 +000053};
54
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +100055struct pickit2_spi_data {
56 libusb_device_handle *pickit2_handle;
57};
Justin Chevrier66e554b2015-02-08 21:58:10 +000058
59/* Default USB transaction timeout in ms */
60#define DFLT_TIMEOUT 10000
61
62#define CMD_LENGTH 64
63#define ENDPOINT_OUT 0x01
64#define ENDPOINT_IN 0x81
65
Justin Chevrier66e554b2015-02-08 21:58:10 +000066#define CMD_GET_VERSION 0x76
67#define CMD_SET_VDD 0xA0
68#define CMD_SET_VPP 0xA1
69#define CMD_READ_VDD_VPP 0xA3
70#define CMD_EXEC_SCRIPT 0xA6
71#define CMD_CLR_DLOAD_BUFF 0xA7
72#define CMD_DOWNLOAD_DATA 0xA8
73#define CMD_CLR_ULOAD_BUFF 0xA9
74#define CMD_UPLOAD_DATA 0xAA
75#define CMD_END_OF_BUFFER 0xAD
76
77#define SCR_SPI_READ_BUF 0xC5
78#define SCR_SPI_WRITE_BUF 0xC6
79#define SCR_SET_AUX 0xCF
80#define SCR_LOOP 0xE9
81#define SCR_SET_ICSP_CLK_PERIOD 0xEA
82#define SCR_SET_PINS 0xF3
83#define SCR_BUSY_LED_OFF 0xF4
84#define SCR_BUSY_LED_ON 0xF5
85#define SCR_MCLR_GND_OFF 0xF6
86#define SCR_MCLR_GND_ON 0xF7
87#define SCR_VPP_PWM_OFF 0xF8
88#define SCR_VPP_PWM_ON 0xF9
89#define SCR_VPP_OFF 0xFA
90#define SCR_VPP_ON 0xFB
91#define SCR_VDD_OFF 0xFE
92#define SCR_VDD_ON 0xFF
93
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +100094static int pickit2_get_firmware_version(libusb_device_handle *pickit2_handle)
Justin Chevrier66e554b2015-02-08 21:58:10 +000095{
96 int ret;
97 uint8_t command[CMD_LENGTH] = {CMD_GET_VERSION, CMD_END_OF_BUFFER};
Thomas Heijligenb221cd72019-04-05 15:08:35 +020098 int transferred;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +010099
Elyes HAOUAS3384fb62019-07-18 14:00:13 +0200100 ret = libusb_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
101
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200102 if (ret != 0) {
103 msg_perr("Command Get Firmware Version failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000104 return 1;
105 }
106
Elyes HAOUAS3384fb62019-07-18 14:00:13 +0200107 ret = libusb_interrupt_transfer(pickit2_handle, ENDPOINT_IN, command, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
108
109 if (ret != 0) {
110 msg_perr("Command Get Firmware Version failed!\n");
111 return 1;
112 }
113
114 msg_pdbg("PICkit2 Firmware Version: %d.%d\n", (int)command[0], (int)command[1]);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000115 return 0;
116}
117
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000118static int pickit2_set_spi_voltage(libusb_device_handle *pickit2_handle, int millivolt)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000119{
120 double voltage_selector;
121 switch (millivolt) {
122 case 0:
123 /* Admittedly this one is an assumption. */
124 voltage_selector = 0;
125 break;
126 case 1800:
127 voltage_selector = 1.8;
128 break;
129 case 2500:
130 voltage_selector = 2.5;
131 break;
132 case 3500:
133 voltage_selector = 3.5;
134 break;
135 default:
136 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
137 return 1;
138 }
139 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
140 millivolt % 1000);
141
142 uint8_t command[CMD_LENGTH] = {
143 CMD_SET_VDD,
144 voltage_selector * 2048 + 672,
145 (voltage_selector * 2048 + 672) / 256,
146 voltage_selector * 36,
147 CMD_SET_VPP,
148 0x40,
149 voltage_selector * 18.61,
150 voltage_selector * 13,
151 CMD_END_OF_BUFFER
152 };
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200153 int transferred;
154 int ret = libusb_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000155
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200156 if (ret != 0) {
157 msg_perr("Command Set Voltage failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000158 return 1;
159 }
160
161 return 0;
162}
163
164struct pickit2_spispeeds {
165 const char *const name;
166 const int speed;
167};
168
169static const struct pickit2_spispeeds spispeeds[] = {
170 { "1M", 0x1 },
171 { "500k", 0x2 },
172 { "333k", 0x3 },
173 { "250k", 0x4 },
174 { NULL, 0x0 },
175};
176
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000177static int pickit2_set_spi_speed(libusb_device_handle *pickit2_handle, unsigned int spispeed_idx)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000178{
179 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed_idx].name);
180
181 uint8_t command[CMD_LENGTH] = {
182 CMD_EXEC_SCRIPT,
183 2,
184 SCR_SET_ICSP_CLK_PERIOD,
185 spispeed_idx,
186 CMD_END_OF_BUFFER
187 };
188
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200189 int transferred;
190 int ret = libusb_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000191
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200192 if (ret != 0) {
193 msg_perr("Command Set SPI Speed failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000194 return 1;
195 }
196
197 return 0;
198}
199
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000200static int pickit2_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000201 const unsigned char *writearr, unsigned char *readarr)
202{
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000203 struct pickit2_spi_data *pickit2_data = flash->mst->spi.data;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000204
205 /* Maximum number of bytes per transaction (including command overhead) is 64. Lets play it safe
206 * and always assume the worst case scenario of 20 bytes command overhead.
207 */
208 if (writecnt + readcnt + 20 > CMD_LENGTH) {
209 msg_perr("\nTotal packetsize (%i) is greater than 64 supported, aborting.\n",
210 writecnt + readcnt + 20);
211 return 1;
212 }
213
214 uint8_t buf[CMD_LENGTH] = {CMD_DOWNLOAD_DATA, writecnt};
Nico Huber519be662018-12-23 20:03:35 +0100215 unsigned int i = 2;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000216 for (; i < writecnt + 2; i++) {
217 buf[i] = writearr[i - 2];
218 }
219
220 buf[i++] = CMD_CLR_ULOAD_BUFF;
221 buf[i++] = CMD_EXEC_SCRIPT;
222
223 /* Determine script length based on number of bytes to be read or written */
224 if (writecnt == 1 && readcnt == 1)
225 buf[i++] = 7;
226 else if (writecnt == 1 || readcnt == 1)
227 buf[i++] = 10;
228 else
229 buf[i++] = 13;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100230
Justin Chevrier66e554b2015-02-08 21:58:10 +0000231 /* Assert CS# */
232 buf[i++] = SCR_VPP_OFF;
233 buf[i++] = SCR_MCLR_GND_ON;
234
235 buf[i++] = SCR_SPI_WRITE_BUF;
236
237 if (writecnt > 1) {
238 buf[i++] = SCR_LOOP;
239 buf[i++] = 1; /* Loop back one instruction */
240 buf[i++] = writecnt - 1; /* Number of times to loop */
241 }
242
243 if (readcnt)
244 buf[i++] = SCR_SPI_READ_BUF;
245
246 if (readcnt > 1) {
247 buf[i++] = SCR_LOOP;
248 buf[i++] = 1; /* Loop back one instruction */
249 buf[i++] = readcnt - 1; /* Number of times to loop */
250 }
251
252 /* De-assert CS# */
253 buf[i++] = SCR_MCLR_GND_OFF;
254 buf[i++] = SCR_VPP_PWM_ON;
255 buf[i++] = SCR_VPP_ON;
256
257 buf[i++] = CMD_UPLOAD_DATA;
258 buf[i++] = CMD_END_OF_BUFFER;
259
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200260 int transferred;
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000261 int ret = libusb_interrupt_transfer(pickit2_data->pickit2_handle,
262 ENDPOINT_OUT, buf, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000263
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200264 if (ret != 0) {
265 msg_perr("Send SPI failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000266 return 1;
267 }
268
269 if (readcnt) {
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200270 int length = 0;
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000271 ret = libusb_interrupt_transfer(pickit2_data->pickit2_handle,
272 ENDPOINT_IN, buf, CMD_LENGTH, &length, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000273
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200274 if (length == 0 || ret != 0) {
275 msg_perr("Receive SPI failed\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000276 return 1;
277 }
278
279 /* First byte indicates number of bytes transferred from upload buffer */
280 if (buf[0] != readcnt) {
281 msg_perr("Unexpected number of bytes transferred, expected %i, got %i!\n",
282 readcnt, ret);
283 return 1;
284 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100285
Justin Chevrier66e554b2015-02-08 21:58:10 +0000286 /* Actual data starts at byte number two */
287 memcpy(readarr, &buf[1], readcnt);
288 }
289
290 return 0;
291}
292
293/* Copied from dediprog.c */
294/* Might be useful for other USB devices as well. static for now. */
295static int parse_voltage(char *voltage)
296{
297 char *tmp = NULL;
298 int i;
299 int millivolt = 0, fraction = 0;
300
301 if (!voltage || !strlen(voltage)) {
302 msg_perr("Empty voltage= specified.\n");
303 return -1;
304 }
305 millivolt = (int)strtol(voltage, &tmp, 0);
306 voltage = tmp;
307 /* Handle "," and "." as decimal point. Everything after it is assumed
308 * to be in decimal notation.
309 */
310 if ((*voltage == '.') || (*voltage == ',')) {
311 voltage++;
312 for (i = 0; i < 3; i++) {
313 fraction *= 10;
314 /* Don't advance if the current character is invalid,
315 * but continue multiplying.
316 */
317 if ((*voltage < '0') || (*voltage > '9'))
318 continue;
319 fraction += *voltage - '0';
320 voltage++;
321 }
322 /* Throw away remaining digits. */
323 voltage += strspn(voltage, "0123456789");
324 }
325 /* The remaining string must be empty or "mV" or "V". */
326 tolower_string(voltage);
327
328 /* No unit or "V". */
329 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
330 millivolt *= 1000;
331 millivolt += fraction;
332 } else if (!strncmp(voltage, "mv", 2) ||
333 !strncmp(voltage, "millivolt", 9)) {
334 /* No adjustment. fraction is discarded. */
335 } else {
336 /* Garbage at the end of the string. */
337 msg_perr("Garbage voltage= specified.\n");
338 return -1;
339 }
340 return millivolt;
341}
342
Nico Huber90739d12021-05-11 17:53:34 +0200343static const struct spi_master spi_master_pickit2 = {
Justin Chevrier66e554b2015-02-08 21:58:10 +0000344 .max_data_read = 40,
345 .max_data_write = 40,
346 .command = pickit2_spi_send_command,
347 .multicommand = default_spi_send_multicommand,
348 .read = default_spi_read,
349 .write_256 = default_spi_write_256,
350 .write_aai = default_spi_write_aai,
351};
352
353static int pickit2_shutdown(void *data)
354{
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000355 struct pickit2_spi_data *pickit2_data = data;
356
Justin Chevrier66e554b2015-02-08 21:58:10 +0000357 /* Set all pins to float and turn voltages off */
358 uint8_t command[CMD_LENGTH] = {
359 CMD_EXEC_SCRIPT,
360 8,
361 SCR_SET_PINS,
362 3, /* Bit-0=1(PDC In), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
363 SCR_SET_AUX,
364 1, /* Bit-0=1(Aux In), Bit-1=0(Aux LL) */
365 SCR_MCLR_GND_OFF,
366 SCR_VPP_OFF,
367 SCR_VDD_OFF,
368 SCR_BUSY_LED_OFF,
369 CMD_END_OF_BUFFER
370 };
371
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200372 int transferred;
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000373 int ret = libusb_interrupt_transfer(pickit2_data->pickit2_handle,
374 ENDPOINT_OUT, command, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000375
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200376 if (ret != 0) {
377 msg_perr("Command Shutdown failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000378 ret = 1;
379 }
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000380 if (libusb_release_interface(pickit2_data->pickit2_handle, 0) != 0) {
Justin Chevrier66e554b2015-02-08 21:58:10 +0000381 msg_perr("Could not release USB interface!\n");
382 ret = 1;
383 }
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000384 libusb_close(pickit2_data->pickit2_handle);
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200385 libusb_exit(NULL);
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000386
387 free(data);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000388 return ret;
389}
390
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200391static int pickit2_spi_init(void)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000392{
Justin Chevrier66e554b2015-02-08 21:58:10 +0000393 uint8_t buf[CMD_LENGTH] = {
394 CMD_EXEC_SCRIPT,
395 10, /* Script length */
396 SCR_SET_PINS,
397 2, /* Bit-0=0(PDC Out), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
398 SCR_SET_AUX,
399 0, /* Bit-0=0(Aux Out), Bit-1=0(Aux LL) */
400 SCR_VDD_ON,
401 SCR_MCLR_GND_OFF, /* Let CS# float */
402 SCR_VPP_PWM_ON,
403 SCR_VPP_ON, /* Pull CS# high */
404 SCR_BUSY_LED_ON,
405 CMD_CLR_DLOAD_BUFF,
406 CMD_CLR_ULOAD_BUFF,
407 CMD_END_OF_BUFFER
408 };
409
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000410 libusb_device_handle *pickit2_handle;
411 struct pickit2_spi_data *pickit2_data;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000412 int spispeed_idx = 0;
413 char *spispeed = extract_programmer_param("spispeed");
414 if (spispeed != NULL) {
415 int i = 0;
416 for (; spispeeds[i].name; i++) {
417 if (strcasecmp(spispeeds[i].name, spispeed) == 0) {
418 spispeed_idx = i;
419 break;
420 }
421 }
422 if (spispeeds[i].name == NULL) {
423 msg_perr("Error: Invalid 'spispeed' value.\n");
424 free(spispeed);
425 return 1;
426 }
427 free(spispeed);
428 }
429
430 int millivolt = 3500;
431 char *voltage = extract_programmer_param("voltage");
432 if (voltage != NULL) {
433 millivolt = parse_voltage(voltage);
434 free(voltage);
435 if (millivolt < 0)
436 return 1;
437 }
438
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200439 if (libusb_init(NULL) < 0) {
440 msg_perr("Couldn't initialize libusb!\n");
441 return -1;
442 }
443
444#if LIBUSB_API_VERSION < 0x01000106
445 libusb_set_debug(NULL, 3);
446#else
447 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
448#endif
449
Stefan Taunerf31fe842016-02-22 08:59:15 +0000450 const uint16_t vid = devs_pickit2_spi[0].vendor_id;
451 const uint16_t pid = devs_pickit2_spi[0].device_id;
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200452 pickit2_handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
453 if (pickit2_handle == NULL) {
454 msg_perr("Could not open device PICkit2!\n");
455 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000456 return 1;
457 }
Justin Chevrier66e554b2015-02-08 21:58:10 +0000458
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200459 if (libusb_set_configuration(pickit2_handle, 1) != 0) {
460 msg_perr("Could not set USB device configuration.\n");
461 libusb_close(pickit2_handle);
462 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000463 return 1;
464 }
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200465 if (libusb_claim_interface(pickit2_handle, 0) != 0) {
466 msg_perr("Could not claim USB device interface\n");
467 libusb_close(pickit2_handle);
468 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000469 return 1;
470 }
471
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000472 pickit2_data = calloc(1, sizeof(*pickit2_data));
473 if (!pickit2_data) {
474 msg_perr("Unable to allocate space for SPI master data\n");
475 libusb_close(pickit2_handle);
476 libusb_exit(NULL);
477 return 1;
478 }
479 pickit2_data->pickit2_handle = pickit2_handle;
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000480
481 if (pickit2_get_firmware_version(pickit2_handle))
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000482 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000483
484 /* Command Set SPI Speed */
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000485 if (pickit2_set_spi_speed(pickit2_handle, spispeed_idx))
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000486 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000487
488 /* Command Set SPI Voltage */
489 msg_pdbg("Setting voltage to %i mV.\n", millivolt);
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000490 if (pickit2_set_spi_voltage(pickit2_handle, millivolt) != 0)
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000491 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000492
493 /* Perform basic setup.
494 * Configure pin directions and logic levels, turn Vdd on, turn busy LED on and clear buffers. */
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200495 int transferred;
496 if (libusb_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, buf, CMD_LENGTH, &transferred, DFLT_TIMEOUT) != 0) {
497 msg_perr("Command Setup failed!\n");
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000498 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000499 }
500
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000501 if (register_shutdown(pickit2_shutdown, pickit2_data))
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000502 goto init_err_cleanup_exit;
Nico Huber90739d12021-05-11 17:53:34 +0200503 register_spi_master(&spi_master_pickit2, pickit2_data);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000504
505 return 0;
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000506
507init_err_cleanup_exit:
Anastasia Klimchuk4a9a66f2021-04-30 14:10:04 +1000508 pickit2_shutdown(pickit2_data);
Anastasia Klimchuke4261e32021-04-30 11:25:44 +1000509 return 1;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000510}
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200511
512const struct programmer_entry programmer_pickit2_spi = {
513 .name = "pickit2_spi",
514 .type = USB,
515 .devs.dev = devs_pickit2_spi,
516 .init = pickit2_spi_init,
517 .map_flash_region = fallback_map,
518 .unmap_flash_region = fallback_unmap,
519 .delay = internal_delay,
520};