Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010, 2011, 2012 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 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; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 17 | #include <strings.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <ctype.h> |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 22 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 23 | #include "programmer.h" |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 24 | #include "spi.h" |
| 25 | |
| 26 | /* Change this to #define if you want to test without a serial implementation */ |
| 27 | #undef FAKE_COMMUNICATION |
| 28 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 29 | struct buspirate_speeds { |
Carl-Daniel Hailfinger | c422484 | 2011-06-09 20:06:34 +0000 | [diff] [blame] | 30 | const char *name; |
| 31 | const int speed; |
| 32 | }; |
| 33 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 34 | #define BP_DEFAULTBAUD 115200 |
| 35 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 36 | #ifndef FAKE_COMMUNICATION |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 37 | static int buspirate_serialport_setup(char *dev) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 38 | { |
| 39 | /* 115200bps, 8 databits, no parity, 1 stopbit */ |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 40 | sp_fd = sp_openserport(dev, BP_DEFAULTBAUD); |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 41 | if (sp_fd == SER_INV_FD) |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 42 | return 1; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 45 | #else |
| 46 | #define buspirate_serialport_setup(...) 0 |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 47 | #define serialport_shutdown(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 48 | #define serialport_write(...) 0 |
| 49 | #define serialport_read(...) 0 |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 50 | #define sp_flush_incoming(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 53 | static unsigned char *bp_commbuf = NULL; |
| 54 | static int bp_commbufsize = 0; |
| 55 | |
| 56 | static int buspirate_commbuf_grow(int bufsize) |
| 57 | { |
| 58 | unsigned char *tmpbuf; |
| 59 | |
| 60 | /* Never shrink. realloc() calls are expensive. */ |
| 61 | if (bufsize <= bp_commbufsize) |
| 62 | return 0; |
| 63 | |
| 64 | tmpbuf = realloc(bp_commbuf, bufsize); |
| 65 | if (!tmpbuf) { |
| 66 | /* Keep the existing buffer because memory is already tight. */ |
| 67 | msg_perr("Out of memory!\n"); |
| 68 | return ERROR_OOM; |
| 69 | } |
| 70 | |
| 71 | bp_commbuf = tmpbuf; |
| 72 | bp_commbufsize = bufsize; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 76 | static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, |
| 77 | unsigned int readcnt) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 78 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 79 | unsigned int i; |
| 80 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 81 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 82 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 83 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 84 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 85 | return 1; |
| 86 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 87 | if (writecnt) |
| 88 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 89 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 90 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 91 | #ifdef FAKE_COMMUNICATION |
| 92 | /* Placate the caller for now. */ |
| 93 | if (readcnt) { |
| 94 | buf[0] = 0x01; |
| 95 | memset(buf + 1, 0xff, readcnt - 1); |
| 96 | } |
| 97 | ret = 0; |
| 98 | #else |
| 99 | if (writecnt) |
| 100 | ret = serialport_write(buf, writecnt); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | if (readcnt) |
| 104 | ret = serialport_read(buf, readcnt); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | #endif |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 108 | if (readcnt) |
| 109 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 110 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 111 | msg_pspew(" 0x%02x", buf[i]); |
| 112 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Jacob Garber | 4a84ec2 | 2019-07-25 19:12:31 -0600 | [diff] [blame] | 116 | static int buspirate_wait_for_string(unsigned char *buf, const char *key) |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 117 | { |
| 118 | unsigned int keylen = strlen(key); |
| 119 | int ret; |
| 120 | |
| 121 | ret = buspirate_sendrecv(buf, 0, keylen); |
| 122 | while (!ret) { |
| 123 | if (!memcmp(buf, key, keylen)) |
| 124 | return 0; |
| 125 | memmove(buf, buf + 1, keylen - 1); |
| 126 | ret = buspirate_sendrecv(buf + keylen - 1, 0, 1); |
| 127 | } |
| 128 | return ret; |
| 129 | } |
| 130 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 131 | static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 132 | const unsigned char *writearr, unsigned char *readarr); |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 133 | static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 134 | const unsigned char *writearr, unsigned char *readarr); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 135 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 136 | static struct spi_master spi_master_buspirate = { |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 137 | .features = SPI_MASTER_4BA, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 138 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 139 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 140 | .command = NULL, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 141 | .multicommand = default_spi_send_multicommand, |
| 142 | .read = default_spi_read, |
| 143 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 144 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 147 | static const struct buspirate_speeds spispeeds[] = { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 148 | {"30k", 0x0}, |
| 149 | {"125k", 0x1}, |
| 150 | {"250k", 0x2}, |
| 151 | {"1M", 0x3}, |
| 152 | {"2M", 0x4}, |
| 153 | {"2.6M", 0x5}, |
| 154 | {"4M", 0x6}, |
| 155 | {"8M", 0x7}, |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 156 | {NULL, 0x0} |
| 157 | }; |
| 158 | |
| 159 | static const struct buspirate_speeds serialspeeds[] = { |
| 160 | {"115200", 115200}, |
| 161 | {"230400", 230400}, |
| 162 | {"250000", 250000}, |
| 163 | {"2000000", 2000000}, |
| 164 | {"2M", 2000000}, |
| 165 | {NULL, 0} |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 168 | static int buspirate_spi_shutdown(void *data) |
| 169 | { |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 170 | int ret = 0, ret2 = 0; |
| 171 | /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 172 | |
| 173 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 174 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 175 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 176 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 177 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 178 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 179 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 180 | goto out_shutdown; |
| 181 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 182 | if (bp_commbuf[0] != '1') { |
| 183 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 184 | ret = 1; |
| 185 | goto out_shutdown; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 186 | } |
| 187 | /* Reset Bus Pirate (return to user terminal) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 188 | bp_commbuf[0] = 0x0f; |
| 189 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 190 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 191 | out_shutdown: |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 192 | /* Shut down serial port communication */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 193 | ret2 = serialport_shutdown(NULL); |
| 194 | /* Keep the oldest error, it is probably the best indicator. */ |
| 195 | if (ret2 && !ret) |
| 196 | ret = ret2; |
| 197 | bp_commbufsize = 0; |
| 198 | free(bp_commbuf); |
| 199 | bp_commbuf = NULL; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 200 | if (ret) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 201 | msg_pdbg("Bus Pirate shutdown failed.\n"); |
| 202 | else |
| 203 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 204 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 205 | return ret; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 208 | #define BP_FWVERSION(a,b) ((a) << 8 | (b)) |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 209 | #define BP_HWVERSION(a,b) BP_FWVERSION(a,b) |
| 210 | |
| 211 | /** |
| 212 | * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a |
| 213 | * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1. |
| 214 | */ |
| 215 | #define BP_DIVISOR(baud) ((4000000/(baud)) - 1) |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 216 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 217 | static int buspirate_spi_init(void) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 218 | { |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 219 | char *tmp; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 220 | char *dev; |
| 221 | int i; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 222 | int cnt; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 223 | unsigned int fw_version_major = 0; |
| 224 | unsigned int fw_version_minor = 0; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 225 | unsigned int hw_version_major = 0; |
| 226 | unsigned int hw_version_minor = 0; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 227 | int spispeed = 0x7; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 228 | int serialspeed_index = -1; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 229 | int ret = 0; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 230 | int pullup = 0; |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 231 | int psu = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 232 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 233 | dev = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 234 | if (dev && !strlen(dev)) { |
| 235 | free(dev); |
| 236 | dev = NULL; |
| 237 | } |
| 238 | if (!dev) { |
| 239 | msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 240 | return 1; |
| 241 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 242 | |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 243 | tmp = extract_programmer_param("spispeed"); |
| 244 | if (tmp) { |
| 245 | for (i = 0; spispeeds[i].name; i++) { |
| 246 | if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 247 | spispeed = spispeeds[i].speed; |
| 248 | break; |
| 249 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 250 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 251 | if (!spispeeds[i].name) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 252 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 253 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 254 | free(tmp); |
| 255 | |
Elyes HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 256 | /* Extract serialspeed parameter */ |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 257 | tmp = extract_programmer_param("serialspeed"); |
| 258 | if (tmp) { |
| 259 | for (i = 0; serialspeeds[i].name; i++) { |
| 260 | if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) { |
| 261 | serialspeed_index = i; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | if (!serialspeeds[i].name) |
| 266 | msg_perr("Invalid serial speed %s, using default.\n", tmp); |
| 267 | } |
| 268 | free(tmp); |
| 269 | |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 270 | tmp = extract_programmer_param("pullups"); |
| 271 | if (tmp) { |
| 272 | if (strcasecmp("on", tmp) == 0) |
| 273 | pullup = 1; |
| 274 | else if (strcasecmp("off", tmp) == 0) |
| 275 | ; // ignore |
| 276 | else |
| 277 | msg_perr("Invalid pullups state, not using them.\n"); |
| 278 | } |
| 279 | free(tmp); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 280 | |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 281 | tmp = extract_programmer_param("psus"); |
| 282 | if (tmp) { |
| 283 | if (strcasecmp("on", tmp) == 0) |
| 284 | psu = 1; |
| 285 | else if (strcasecmp("off", tmp) == 0) |
| 286 | ; // ignore |
| 287 | else |
| 288 | msg_perr("Invalid psus state, not enabling.\n"); |
| 289 | } |
| 290 | free(tmp); |
| 291 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 292 | /* Default buffer size is 19: 16 bytes data, 3 bytes control. */ |
| 293 | #define DEFAULT_BUFSIZE (16 + 3) |
| 294 | bp_commbuf = malloc(DEFAULT_BUFSIZE); |
| 295 | if (!bp_commbuf) { |
| 296 | bp_commbufsize = 0; |
| 297 | msg_perr("Out of memory!\n"); |
Stefan Reinauer | 1838591 | 2014-04-26 16:12:45 +0000 | [diff] [blame] | 298 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 299 | return ERROR_OOM; |
| 300 | } |
| 301 | bp_commbufsize = DEFAULT_BUFSIZE; |
| 302 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 303 | ret = buspirate_serialport_setup(dev); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 304 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 305 | if (ret) { |
| 306 | bp_commbufsize = 0; |
| 307 | free(bp_commbuf); |
| 308 | bp_commbuf = NULL; |
| 309 | return ret; |
| 310 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 311 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 312 | /* This is the brute force version, but it should work. |
| 313 | * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands |
| 314 | * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to |
| 315 | * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays. |
| 316 | */ |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 317 | for (i = 0; i < 20; i++) { |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 318 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 319 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 320 | /* Send the command, don't read the response. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 321 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 322 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 323 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 324 | /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any |
| 325 | * response which came in over serial. Unfortunately that does not work reliably on Linux |
| 326 | * with FTDI USB-serial. |
| 327 | */ |
| 328 | //sp_flush_incoming(); |
| 329 | /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence |
| 330 | * of 0x00 too fast apparently triggers such an UART input buffer overflow. |
| 331 | */ |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 332 | internal_sleep(10000); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 333 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 334 | /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */ |
| 335 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 336 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 337 | |
| 338 | /* Reset the Bus Pirate. */ |
| 339 | bp_commbuf[0] = 0x0f; |
| 340 | /* Send the command, don't read the response. */ |
| 341 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 342 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 343 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irate "))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 344 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 345 | /* Read the hardware version string. Last byte of the buffer is reserved for \0. */ |
| 346 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 347 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 348 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 349 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 350 | break; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 351 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 352 | bp_commbuf[i] = '\0'; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 353 | msg_pdbg("Detected Bus Pirate hardware "); |
| 354 | if (bp_commbuf[0] != 'v') |
| 355 | msg_pdbg("(unknown version number format)"); |
| 356 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 357 | msg_pdbg("(unknown version number format)"); |
| 358 | else { |
| 359 | hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 360 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 361 | tmp++; |
| 362 | hw_version_minor = strtoul(tmp, NULL, 10); |
| 363 | msg_pdbg("%u.%u", hw_version_major, hw_version_minor); |
| 364 | } |
| 365 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 366 | msg_pdbg("\n"); |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 367 | |
| 368 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware "))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 369 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 370 | /* Read the firmware version string. Last byte of the buffer is reserved for \0. */ |
| 371 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 372 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 373 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 374 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 375 | break; |
| 376 | } |
| 377 | bp_commbuf[i] = '\0'; |
| 378 | msg_pdbg("Detected Bus Pirate firmware "); |
| 379 | if (bp_commbuf[0] != 'v') |
| 380 | msg_pdbg("(unknown version number format)"); |
| 381 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 382 | msg_pdbg("(unknown version number format)"); |
| 383 | else { |
| 384 | fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 385 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 386 | tmp++; |
| 387 | fw_version_minor = strtoul(tmp, NULL, 10); |
| 388 | msg_pdbg("%u.%u", fw_version_major, fw_version_minor); |
| 389 | } |
| 390 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 391 | msg_pdbg("\n"); |
| 392 | |
| 393 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 394 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 395 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 396 | /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */ |
| 397 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) { |
| 398 | msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n"); |
| 399 | msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 400 | ret = SPI_PROGRAMMER_ERROR; |
| 401 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 404 | /* Use fast SPI mode in firmware 5.5 and newer. */ |
| 405 | if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) { |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 406 | msg_pdbg("Using SPI command set v2.\n"); |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 407 | /* Sensible default buffer size. */ |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 408 | if (buspirate_commbuf_grow(260 + 5)) { |
| 409 | ret = ERROR_OOM; |
| 410 | goto init_err_cleanup_exit; |
| 411 | } |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 412 | spi_master_buspirate.max_data_read = 2048; |
| 413 | spi_master_buspirate.max_data_write = 256; |
| 414 | spi_master_buspirate.command = buspirate_spi_send_command_v2; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 415 | } else { |
| 416 | msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n"); |
| 417 | msg_pinfo("Reading/writing a flash chip may take hours.\n"); |
| 418 | msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n"); |
| 419 | /* Sensible default buffer size. */ |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 420 | if (buspirate_commbuf_grow(16 + 3)) { |
| 421 | ret = ERROR_OOM; |
| 422 | goto init_err_cleanup_exit; |
| 423 | } |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 424 | spi_master_buspirate.max_data_read = 12; |
| 425 | spi_master_buspirate.max_data_write = 12; |
| 426 | spi_master_buspirate.command = buspirate_spi_send_command_v1; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 429 | /* Workaround for broken speed settings in firmware 6.1 and older. */ |
| 430 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2)) |
| 431 | if (spispeed > 0x4) { |
| 432 | msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. " |
| 433 | "Limiting speed to 2 MHz.\n"); |
| 434 | msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n"); |
| 435 | spispeed = 0x4; |
| 436 | } |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 437 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 438 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
| 439 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
| 440 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 441 | /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */ |
| 442 | if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) { |
| 443 | serialspeed_index = ARRAY_SIZE(serialspeeds) - 2; |
| 444 | msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n"); |
| 445 | } |
| 446 | |
| 447 | /* Set custom serial speed if specified */ |
| 448 | if (serialspeed_index != -1) { |
| 449 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) { |
| 450 | /* This feature requires firmware 5.5 or newer */ |
| 451 | msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds." |
| 452 | "Using default speed of 115200 baud.\n"); |
| 453 | } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) { |
| 454 | /* Set the serial speed to match the user's choice if it doesn't already */ |
| 455 | |
| 456 | if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0)) |
| 457 | msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates." |
| 458 | " Continue at your own risk.\n"); |
| 459 | |
| 460 | /* Enter baud rate configuration mode */ |
| 461 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n"); |
| 462 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 463 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 464 | if ((ret = buspirate_wait_for_string(bp_commbuf, ">"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 465 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 466 | |
| 467 | /* Enter manual clock divisor entry mode */ |
| 468 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n"); |
| 469 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 470 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 471 | if ((ret = buspirate_wait_for_string(bp_commbuf, ">"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 472 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 473 | |
| 474 | /* Set the clock divisor to the value calculated from the user's input */ |
| 475 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n", |
| 476 | BP_DIVISOR(serialspeeds[serialspeed_index].speed)); |
| 477 | |
| 478 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 479 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 480 | sleep(1); |
| 481 | |
| 482 | /* Reconfigure the host's serial baud rate to the new value */ |
| 483 | if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) { |
| 484 | msg_perr("Unable to configure system baud rate to specified value."); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 485 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Return to the main prompt */ |
| 489 | bp_commbuf[0] = ' '; |
| 490 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 491 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 492 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 493 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 494 | |
| 495 | msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed); |
| 496 | } |
| 497 | |
| 498 | } |
| 499 | |
| 500 | |
| 501 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 502 | /* Enter raw bitbang mode */ |
| 503 | for (i = 0; i < 20; i++) { |
| 504 | bp_commbuf[0] = 0x00; |
| 505 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 506 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 507 | } |
| 508 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 509 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 510 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 511 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 512 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 513 | if (bp_commbuf[0] != '1') { |
| 514 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 515 | ret = 1; |
| 516 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 517 | } |
| 518 | /* Enter raw SPI mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 519 | bp_commbuf[0] = 0x01; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 520 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame^] | 521 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 522 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 523 | if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 524 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 525 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 526 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 527 | msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]); |
| 528 | if (bp_commbuf[0] != '1') { |
| 529 | msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 530 | ret = 1; |
| 531 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 535 | bp_commbuf[0] = 0x40 | 0x0b; |
| 536 | if (pullup == 1) { |
| 537 | bp_commbuf[0] |= (1 << 2); |
| 538 | msg_pdbg("Enabling pull-up resistors.\n"); |
| 539 | } |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 540 | if (psu == 1) { |
| 541 | bp_commbuf[0] |= (1 << 3); |
| 542 | msg_pdbg("Enabling PSUs.\n"); |
| 543 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 544 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame^] | 545 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 546 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 547 | if (bp_commbuf[0] != 0x01) { |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 548 | msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 549 | ret = 1; |
| 550 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 553 | /* Set SPI speed */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 554 | bp_commbuf[0] = 0x60 | spispeed; |
| 555 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame^] | 556 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 557 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 558 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 559 | msg_perr("Protocol error while setting SPI speed!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 560 | ret = 1; |
| 561 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 562 | } |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 563 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 564 | /* Set SPI config: output type, idle, clock edge, sample */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 565 | bp_commbuf[0] = 0x80 | 0xa; |
Maxime Vincent | 2099c64 | 2017-11-24 13:04:08 +0100 | [diff] [blame] | 566 | if (pullup == 1) { |
| 567 | bp_commbuf[0] &= ~(1 << 3); |
| 568 | msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n"); |
| 569 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 570 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame^] | 571 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 572 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 573 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 574 | msg_perr("Protocol error while setting SPI config!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 575 | ret = 1; |
| 576 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 580 | bp_commbuf[0] = 0x03; |
| 581 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame^] | 582 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 583 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 584 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 585 | msg_perr("Protocol error while raising CS#!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 586 | ret = 1; |
| 587 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 590 | if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) { |
| 591 | ret = 1; |
| 592 | goto init_err_cleanup_exit; |
| 593 | } |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 594 | register_spi_master(&spi_master_buspirate); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 595 | |
| 596 | return 0; |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 597 | |
| 598 | init_err_cleanup_exit: |
| 599 | buspirate_spi_shutdown(NULL); |
| 600 | return ret; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 603 | static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 604 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 605 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 606 | unsigned int i = 0; |
| 607 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 608 | |
| 609 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 610 | return SPI_INVALID_LENGTH; |
| 611 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 612 | /* 3 bytes extra for CS#, len, CS#. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 613 | if (buspirate_commbuf_grow(writecnt + readcnt + 3)) |
| 614 | return ERROR_OOM; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 615 | |
| 616 | /* Assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 617 | bp_commbuf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 618 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 619 | bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 620 | memcpy(bp_commbuf + i, writearr, writecnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 621 | i += writecnt; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 622 | memset(bp_commbuf + i, 0, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 623 | |
| 624 | i += readcnt; |
| 625 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 626 | bp_commbuf[i++] = 0x03; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 627 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 628 | ret = buspirate_sendrecv(bp_commbuf, i, i); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 629 | |
| 630 | if (ret) { |
| 631 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 632 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 635 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 636 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 637 | return SPI_GENERIC_ERROR; |
| 638 | } |
| 639 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 640 | if (bp_commbuf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 641 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 642 | return SPI_GENERIC_ERROR; |
| 643 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 644 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 645 | if (bp_commbuf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 646 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 647 | return SPI_GENERIC_ERROR; |
| 648 | } |
| 649 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 650 | /* Skip CS#, length, writearr. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 651 | memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 652 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 653 | return ret; |
| 654 | } |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 655 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 656 | static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 657 | const unsigned char *writearr, unsigned char *readarr) |
| 658 | { |
| 659 | int i = 0, ret = 0; |
| 660 | |
| 661 | if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096) |
| 662 | return SPI_INVALID_LENGTH; |
| 663 | |
| 664 | /* 5 bytes extra for command, writelen, readlen. |
| 665 | * 1 byte extra for Ack/Nack. |
| 666 | */ |
| 667 | if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1))) |
| 668 | return ERROR_OOM; |
| 669 | |
| 670 | /* Combined SPI write/read. */ |
| 671 | bp_commbuf[i++] = 0x04; |
| 672 | bp_commbuf[i++] = (writecnt >> 8) & 0xff; |
| 673 | bp_commbuf[i++] = writecnt & 0xff; |
| 674 | bp_commbuf[i++] = (readcnt >> 8) & 0xff; |
| 675 | bp_commbuf[i++] = readcnt & 0xff; |
| 676 | memcpy(bp_commbuf + i, writearr, writecnt); |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 677 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 678 | ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt); |
| 679 | |
| 680 | if (ret) { |
| 681 | msg_perr("Bus Pirate communication error!\n"); |
| 682 | return SPI_GENERIC_ERROR; |
| 683 | } |
| 684 | |
| 685 | if (bp_commbuf[0] != 0x01) { |
| 686 | msg_perr("Protocol error while sending SPI write/read!\n"); |
| 687 | return SPI_GENERIC_ERROR; |
| 688 | } |
| 689 | |
| 690 | /* Skip Ack. */ |
| 691 | memcpy(readarr, bp_commbuf + 1, readcnt); |
| 692 | |
| 693 | return ret; |
Nico Huber | deeac7e | 2017-04-22 00:09:42 +0200 | [diff] [blame] | 694 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 695 | |
| 696 | const struct programmer_entry programmer_buspirate_spi = { |
| 697 | .name = "buspirate_spi", |
| 698 | .type = OTHER, |
| 699 | /* FIXME */ |
| 700 | .devs.note = "Dangerous Prototypes Bus Pirate\n", |
| 701 | .init = buspirate_spi_init, |
| 702 | .map_flash_region = fallback_map, |
| 703 | .unmap_flash_region = fallback_unmap, |
| 704 | .delay = internal_delay, |
| 705 | }; |