blob: f1f60a2fbbd30f036831566b57a806eb51510aeb [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Connections are as follows:
23 *
24 * +------+-----+----------+
25 * | SPI | Pin | PICkit2 |
26 * +------+-----+----------+
27 * | /CS | 1 | VPP/MCLR |
28 * | VCC | 2 | VDD |
29 * | GND | 3 | GND |
30 * | MISO | 4 | PGD |
31 * | SCLK | 5 | PDC |
32 * | MOSI | 6 | AUX |
33 * +------+-----+----------+
34 *
35 * Inspiration and some specifics of the interface came via the AVRDude
36 * PICkit2 code: https://github.com/steve-m/avrdude/blob/master/pickit2.c
37 */
38
39#include "platform.h"
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44#include <limits.h>
45#include <errno.h>
46
47#if IS_WINDOWS
48#include <lusb0_usb.h>
49#else
50#include <usb.h>
51#endif
52
53#include "flash.h"
54#include "chipdrivers.h"
55#include "programmer.h"
56#include "spi.h"
57
Stefan Taunerf31fe842016-02-22 08:59:15 +000058const struct dev_entry devs_pickit2_spi[] = {
59 {0x04D8, 0x0033, OK, "Microchip", "PICkit 2"},
60
61 {}
62};
63
Justin Chevrier66e554b2015-02-08 21:58:10 +000064static usb_dev_handle *pickit2_handle;
65
66/* Default USB transaction timeout in ms */
67#define DFLT_TIMEOUT 10000
68
69#define CMD_LENGTH 64
70#define ENDPOINT_OUT 0x01
71#define ENDPOINT_IN 0x81
72
Justin Chevrier66e554b2015-02-08 21:58:10 +000073#define CMD_GET_VERSION 0x76
74#define CMD_SET_VDD 0xA0
75#define CMD_SET_VPP 0xA1
76#define CMD_READ_VDD_VPP 0xA3
77#define CMD_EXEC_SCRIPT 0xA6
78#define CMD_CLR_DLOAD_BUFF 0xA7
79#define CMD_DOWNLOAD_DATA 0xA8
80#define CMD_CLR_ULOAD_BUFF 0xA9
81#define CMD_UPLOAD_DATA 0xAA
82#define CMD_END_OF_BUFFER 0xAD
83
84#define SCR_SPI_READ_BUF 0xC5
85#define SCR_SPI_WRITE_BUF 0xC6
86#define SCR_SET_AUX 0xCF
87#define SCR_LOOP 0xE9
88#define SCR_SET_ICSP_CLK_PERIOD 0xEA
89#define SCR_SET_PINS 0xF3
90#define SCR_BUSY_LED_OFF 0xF4
91#define SCR_BUSY_LED_ON 0xF5
92#define SCR_MCLR_GND_OFF 0xF6
93#define SCR_MCLR_GND_ON 0xF7
94#define SCR_VPP_PWM_OFF 0xF8
95#define SCR_VPP_PWM_ON 0xF9
96#define SCR_VPP_OFF 0xFA
97#define SCR_VPP_ON 0xFB
98#define SCR_VDD_OFF 0xFE
99#define SCR_VDD_ON 0xFF
100
Nico Huberd99a2bd2016-02-18 21:42:49 +0000101/* Might be useful for other USB devices as well. static for now.
102 * device parameter allows user to specify one device of multiple installed */
Justin Chevrier66e554b2015-02-08 21:58:10 +0000103static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
104{
105 struct usb_bus *bus;
106 struct usb_device *dev;
107
108 for (bus = usb_get_busses(); bus; bus = bus->next)
109 for (dev = bus->devices; dev; dev = dev->next)
110 if ((dev->descriptor.idVendor == vid) &&
111 (dev->descriptor.idProduct == pid)) {
112 if (device == 0)
113 return dev;
114 device--;
115 }
116
117 return NULL;
118}
119
120static int pickit2_get_firmware_version(void)
121{
122 int ret;
123 uint8_t command[CMD_LENGTH] = {CMD_GET_VERSION, CMD_END_OF_BUFFER};
124
125 ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)command, CMD_LENGTH, DFLT_TIMEOUT);
126 ret = usb_interrupt_read(pickit2_handle, ENDPOINT_IN, (char *)command, CMD_LENGTH, DFLT_TIMEOUT);
127
128 msg_pdbg("PICkit2 Firmware Version: %d.%d\n", (int)command[0], (int)command[1]);
129 if (ret != CMD_LENGTH) {
130 msg_perr("Command Get Firmware Version failed (%s)!\n", usb_strerror());
131 return 1;
132 }
133
134 return 0;
135}
136
137static int pickit2_set_spi_voltage(int millivolt)
138{
139 double voltage_selector;
140 switch (millivolt) {
141 case 0:
142 /* Admittedly this one is an assumption. */
143 voltage_selector = 0;
144 break;
145 case 1800:
146 voltage_selector = 1.8;
147 break;
148 case 2500:
149 voltage_selector = 2.5;
150 break;
151 case 3500:
152 voltage_selector = 3.5;
153 break;
154 default:
155 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
156 return 1;
157 }
158 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
159 millivolt % 1000);
160
161 uint8_t command[CMD_LENGTH] = {
162 CMD_SET_VDD,
163 voltage_selector * 2048 + 672,
164 (voltage_selector * 2048 + 672) / 256,
165 voltage_selector * 36,
166 CMD_SET_VPP,
167 0x40,
168 voltage_selector * 18.61,
169 voltage_selector * 13,
170 CMD_END_OF_BUFFER
171 };
172
173 int ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)command, CMD_LENGTH, DFLT_TIMEOUT);
174
175 if (ret != CMD_LENGTH) {
176 msg_perr("Command Set Voltage failed (%s)!\n", usb_strerror());
177 return 1;
178 }
179
180 return 0;
181}
182
183struct pickit2_spispeeds {
184 const char *const name;
185 const int speed;
186};
187
188static const struct pickit2_spispeeds spispeeds[] = {
189 { "1M", 0x1 },
190 { "500k", 0x2 },
191 { "333k", 0x3 },
192 { "250k", 0x4 },
193 { NULL, 0x0 },
194};
195
196static int pickit2_set_spi_speed(unsigned int spispeed_idx)
197{
198 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed_idx].name);
199
200 uint8_t command[CMD_LENGTH] = {
201 CMD_EXEC_SCRIPT,
202 2,
203 SCR_SET_ICSP_CLK_PERIOD,
204 spispeed_idx,
205 CMD_END_OF_BUFFER
206 };
207
208 int ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)command, CMD_LENGTH, DFLT_TIMEOUT);
209
210 if (ret != CMD_LENGTH) {
211 msg_perr("Command Set SPI Speed failed (%s)!\n", usb_strerror());
212 return 1;
213 }
214
215 return 0;
216}
217
218static int pickit2_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
219 const unsigned char *writearr, unsigned char *readarr)
220{
221
222 /* Maximum number of bytes per transaction (including command overhead) is 64. Lets play it safe
223 * and always assume the worst case scenario of 20 bytes command overhead.
224 */
225 if (writecnt + readcnt + 20 > CMD_LENGTH) {
226 msg_perr("\nTotal packetsize (%i) is greater than 64 supported, aborting.\n",
227 writecnt + readcnt + 20);
228 return 1;
229 }
230
231 uint8_t buf[CMD_LENGTH] = {CMD_DOWNLOAD_DATA, writecnt};
232 int i = 2;
233 for (; i < writecnt + 2; i++) {
234 buf[i] = writearr[i - 2];
235 }
236
237 buf[i++] = CMD_CLR_ULOAD_BUFF;
238 buf[i++] = CMD_EXEC_SCRIPT;
239
240 /* Determine script length based on number of bytes to be read or written */
241 if (writecnt == 1 && readcnt == 1)
242 buf[i++] = 7;
243 else if (writecnt == 1 || readcnt == 1)
244 buf[i++] = 10;
245 else
246 buf[i++] = 13;
247
248 /* Assert CS# */
249 buf[i++] = SCR_VPP_OFF;
250 buf[i++] = SCR_MCLR_GND_ON;
251
252 buf[i++] = SCR_SPI_WRITE_BUF;
253
254 if (writecnt > 1) {
255 buf[i++] = SCR_LOOP;
256 buf[i++] = 1; /* Loop back one instruction */
257 buf[i++] = writecnt - 1; /* Number of times to loop */
258 }
259
260 if (readcnt)
261 buf[i++] = SCR_SPI_READ_BUF;
262
263 if (readcnt > 1) {
264 buf[i++] = SCR_LOOP;
265 buf[i++] = 1; /* Loop back one instruction */
266 buf[i++] = readcnt - 1; /* Number of times to loop */
267 }
268
269 /* De-assert CS# */
270 buf[i++] = SCR_MCLR_GND_OFF;
271 buf[i++] = SCR_VPP_PWM_ON;
272 buf[i++] = SCR_VPP_ON;
273
274 buf[i++] = CMD_UPLOAD_DATA;
275 buf[i++] = CMD_END_OF_BUFFER;
276
277 int ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)buf, CMD_LENGTH, DFLT_TIMEOUT);
278
279 if (ret != CMD_LENGTH) {
280 msg_perr("Send SPI failed, expected %i, got %i %s!\n", writecnt, ret, usb_strerror());
281 return 1;
282 }
283
284 if (readcnt) {
285 ret = usb_interrupt_read(pickit2_handle, ENDPOINT_IN, (char *)buf, CMD_LENGTH, DFLT_TIMEOUT);
286
287 if (ret != CMD_LENGTH) {
288 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, usb_strerror());
289 return 1;
290 }
291
292 /* First byte indicates number of bytes transferred from upload buffer */
293 if (buf[0] != readcnt) {
294 msg_perr("Unexpected number of bytes transferred, expected %i, got %i!\n",
295 readcnt, ret);
296 return 1;
297 }
298
299 /* Actual data starts at byte number two */
300 memcpy(readarr, &buf[1], readcnt);
301 }
302
303 return 0;
304}
305
306/* Copied from dediprog.c */
307/* Might be useful for other USB devices as well. static for now. */
308static int parse_voltage(char *voltage)
309{
310 char *tmp = NULL;
311 int i;
312 int millivolt = 0, fraction = 0;
313
314 if (!voltage || !strlen(voltage)) {
315 msg_perr("Empty voltage= specified.\n");
316 return -1;
317 }
318 millivolt = (int)strtol(voltage, &tmp, 0);
319 voltage = tmp;
320 /* Handle "," and "." as decimal point. Everything after it is assumed
321 * to be in decimal notation.
322 */
323 if ((*voltage == '.') || (*voltage == ',')) {
324 voltage++;
325 for (i = 0; i < 3; i++) {
326 fraction *= 10;
327 /* Don't advance if the current character is invalid,
328 * but continue multiplying.
329 */
330 if ((*voltage < '0') || (*voltage > '9'))
331 continue;
332 fraction += *voltage - '0';
333 voltage++;
334 }
335 /* Throw away remaining digits. */
336 voltage += strspn(voltage, "0123456789");
337 }
338 /* The remaining string must be empty or "mV" or "V". */
339 tolower_string(voltage);
340
341 /* No unit or "V". */
342 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
343 millivolt *= 1000;
344 millivolt += fraction;
345 } else if (!strncmp(voltage, "mv", 2) ||
346 !strncmp(voltage, "millivolt", 9)) {
347 /* No adjustment. fraction is discarded. */
348 } else {
349 /* Garbage at the end of the string. */
350 msg_perr("Garbage voltage= specified.\n");
351 return -1;
352 }
353 return millivolt;
354}
355
356static const struct spi_master spi_master_pickit2 = {
357 .type = SPI_CONTROLLER_PICKIT2,
358 .max_data_read = 40,
359 .max_data_write = 40,
360 .command = pickit2_spi_send_command,
361 .multicommand = default_spi_send_multicommand,
362 .read = default_spi_read,
363 .write_256 = default_spi_write_256,
364 .write_aai = default_spi_write_aai,
365};
366
367static int pickit2_shutdown(void *data)
368{
369 /* Set all pins to float and turn voltages off */
370 uint8_t command[CMD_LENGTH] = {
371 CMD_EXEC_SCRIPT,
372 8,
373 SCR_SET_PINS,
374 3, /* Bit-0=1(PDC In), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
375 SCR_SET_AUX,
376 1, /* Bit-0=1(Aux In), Bit-1=0(Aux LL) */
377 SCR_MCLR_GND_OFF,
378 SCR_VPP_OFF,
379 SCR_VDD_OFF,
380 SCR_BUSY_LED_OFF,
381 CMD_END_OF_BUFFER
382 };
383
384 int ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)command, CMD_LENGTH, DFLT_TIMEOUT);
385
386 if (ret != CMD_LENGTH) {
387 msg_perr("Command Shutdown failed (%s)!\n", usb_strerror());
388 ret = 1;
389 }
390 if (usb_release_interface(pickit2_handle, 0) != 0) {
391 msg_perr("Could not release USB interface!\n");
392 ret = 1;
393 }
394 if (usb_close(pickit2_handle) != 0) {
395 msg_perr("Could not close USB device!\n");
396 ret = 1;
397 }
398 return ret;
399}
400
401int pickit2_spi_init(void)
402{
403 unsigned int usedevice = 0; // FIXME: allow to select one of multiple devices
404
405 uint8_t buf[CMD_LENGTH] = {
406 CMD_EXEC_SCRIPT,
407 10, /* Script length */
408 SCR_SET_PINS,
409 2, /* Bit-0=0(PDC Out), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
410 SCR_SET_AUX,
411 0, /* Bit-0=0(Aux Out), Bit-1=0(Aux LL) */
412 SCR_VDD_ON,
413 SCR_MCLR_GND_OFF, /* Let CS# float */
414 SCR_VPP_PWM_ON,
415 SCR_VPP_ON, /* Pull CS# high */
416 SCR_BUSY_LED_ON,
417 CMD_CLR_DLOAD_BUFF,
418 CMD_CLR_ULOAD_BUFF,
419 CMD_END_OF_BUFFER
420 };
421
422
423 int spispeed_idx = 0;
424 char *spispeed = extract_programmer_param("spispeed");
425 if (spispeed != NULL) {
426 int i = 0;
427 for (; spispeeds[i].name; i++) {
428 if (strcasecmp(spispeeds[i].name, spispeed) == 0) {
429 spispeed_idx = i;
430 break;
431 }
432 }
433 if (spispeeds[i].name == NULL) {
434 msg_perr("Error: Invalid 'spispeed' value.\n");
435 free(spispeed);
436 return 1;
437 }
438 free(spispeed);
439 }
440
441 int millivolt = 3500;
442 char *voltage = extract_programmer_param("voltage");
443 if (voltage != NULL) {
444 millivolt = parse_voltage(voltage);
445 free(voltage);
446 if (millivolt < 0)
447 return 1;
448 }
449
450 /* Here comes the USB stuff */
451 usb_init();
452 (void)usb_find_busses();
453 (void)usb_find_devices();
Stefan Taunerf31fe842016-02-22 08:59:15 +0000454 const uint16_t vid = devs_pickit2_spi[0].vendor_id;
455 const uint16_t pid = devs_pickit2_spi[0].device_id;
456 struct usb_device *dev = get_device_by_vid_pid(vid, pid, usedevice);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000457 if (dev == NULL) {
458 msg_perr("Could not find a PICkit2 on USB!\n");
459 return 1;
460 }
461 msg_pdbg("Found USB device (%04x:%04x).\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
462
463 pickit2_handle = usb_open(dev);
464 int ret = usb_set_configuration(pickit2_handle, 1);
465 if (ret != 0) {
466 msg_perr("Could not set USB device configuration: %i %s\n", ret, usb_strerror());
467 if (usb_close(pickit2_handle) != 0)
468 msg_perr("Could not close USB device!\n");
469 return 1;
470 }
471 ret = usb_claim_interface(pickit2_handle, 0);
472 if (ret != 0) {
473 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, usb_strerror());
474 if (usb_close(pickit2_handle) != 0)
475 msg_perr("Could not close USB device!\n");
476 return 1;
477 }
478
479 if (register_shutdown(pickit2_shutdown, NULL) != 0) {
480 return 1;
481 }
482
483 if (pickit2_get_firmware_version()) {
484 return 1;
485 }
486
487 /* Command Set SPI Speed */
488 if (pickit2_set_spi_speed(spispeed_idx)) {
489 return 1;
490 }
491
492 /* Command Set SPI Voltage */
493 msg_pdbg("Setting voltage to %i mV.\n", millivolt);
494 if (pickit2_set_spi_voltage(millivolt) != 0) {
495 return 1;
496 }
497
498 /* Perform basic setup.
499 * Configure pin directions and logic levels, turn Vdd on, turn busy LED on and clear buffers. */
500 ret = usb_interrupt_write(pickit2_handle, ENDPOINT_OUT, (char *)buf, CMD_LENGTH, DFLT_TIMEOUT);
501 if (ret != CMD_LENGTH) {
502 msg_perr("Command Setup failed (%s)!\n", usb_strerror());
503 return 1;
504 }
505
506 register_spi_master(&spi_master_pickit2);
507
508 return 0;
509}