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