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 | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <ctype.h> |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 25 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 26 | #include "programmer.h" |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 27 | #include "spi.h" |
| 28 | |
| 29 | /* Change this to #define if you want to test without a serial implementation */ |
| 30 | #undef FAKE_COMMUNICATION |
| 31 | |
Carl-Daniel Hailfinger | c422484 | 2011-06-09 20:06:34 +0000 | [diff] [blame] | 32 | struct buspirate_spispeeds { |
| 33 | const char *name; |
| 34 | const int speed; |
| 35 | }; |
| 36 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 37 | #ifndef FAKE_COMMUNICATION |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 38 | static int buspirate_serialport_setup(char *dev) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 39 | { |
| 40 | /* 115200bps, 8 databits, no parity, 1 stopbit */ |
| 41 | sp_fd = sp_openserport(dev, 115200); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 42 | /* FIXME: Error checking */ |
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 | { |
| 79 | int i, ret = 0; |
| 80 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 81 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 82 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 83 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 84 | return 1; |
| 85 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 86 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 87 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 88 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 89 | #ifdef FAKE_COMMUNICATION |
| 90 | /* Placate the caller for now. */ |
| 91 | if (readcnt) { |
| 92 | buf[0] = 0x01; |
| 93 | memset(buf + 1, 0xff, readcnt - 1); |
| 94 | } |
| 95 | ret = 0; |
| 96 | #else |
| 97 | if (writecnt) |
| 98 | ret = serialport_write(buf, writecnt); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | if (readcnt) |
| 102 | ret = serialport_read(buf, readcnt); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | #endif |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 106 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 107 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 108 | msg_pspew(" 0x%02x", buf[i]); |
| 109 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 113 | static int buspirate_spi_send_command(struct flashctx *flash, |
| 114 | unsigned int writecnt, |
| 115 | unsigned int readcnt, |
| 116 | const unsigned char *writearr, |
| 117 | unsigned char *readarr); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 118 | |
| 119 | static const struct spi_programmer spi_programmer_buspirate = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 120 | .type = SPI_CONTROLLER_BUSPIRATE, |
| 121 | .max_data_read = 12, |
| 122 | .max_data_write = 12, |
| 123 | .command = buspirate_spi_send_command, |
| 124 | .multicommand = default_spi_send_multicommand, |
| 125 | .read = default_spi_read, |
| 126 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 127 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 130 | static const struct buspirate_spispeeds spispeeds[] = { |
| 131 | {"30k", 0x0}, |
| 132 | {"125k", 0x1}, |
| 133 | {"250k", 0x2}, |
| 134 | {"1M", 0x3}, |
| 135 | {"2M", 0x4}, |
| 136 | {"2.6M", 0x5}, |
| 137 | {"4M", 0x6}, |
| 138 | {"8M", 0x7}, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 139 | {NULL, 0x0}, |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 142 | static int buspirate_spi_shutdown(void *data) |
| 143 | { |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 144 | int ret = 0, ret2 = 0; |
| 145 | /* 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] | 146 | |
| 147 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 148 | bp_commbuf[0] = 0x00; |
| 149 | ret = buspirate_sendrecv(bp_commbuf, 1, 5); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 150 | if (ret) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 151 | goto out_shutdown; |
| 152 | if (memcmp(bp_commbuf, "BBIO", 4)) { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 153 | msg_perr("Entering raw bitbang mode failed!\n"); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 154 | ret = 1; |
| 155 | goto out_shutdown; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 156 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 157 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]); |
| 158 | if (bp_commbuf[4] != '1') { |
| 159 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[4]); |
| 160 | ret = 1; |
| 161 | goto out_shutdown; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 162 | } |
| 163 | /* Reset Bus Pirate (return to user terminal) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 164 | bp_commbuf[0] = 0x0f; |
| 165 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 166 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 167 | out_shutdown: |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 168 | /* Shut down serial port communication */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 169 | ret2 = serialport_shutdown(NULL); |
| 170 | /* Keep the oldest error, it is probably the best indicator. */ |
| 171 | if (ret2 && !ret) |
| 172 | ret = ret2; |
| 173 | bp_commbufsize = 0; |
| 174 | free(bp_commbuf); |
| 175 | bp_commbuf = NULL; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 176 | if (ret) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 177 | msg_pdbg("Bus Pirate shutdown failed.\n"); |
| 178 | else |
| 179 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 180 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 181 | return ret; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 184 | int buspirate_spi_init(void) |
| 185 | { |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 186 | char *dev = NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 187 | char *speed = NULL; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 188 | int spispeed = 0x7; |
| 189 | int ret = 0; |
| 190 | int i; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 191 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 192 | dev = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 193 | if (!dev || !strlen(dev)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 194 | msg_perr("No serial device given. Use flashrom -p " |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 195 | "buspirate_spi:dev=/dev/ttyUSB0\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 196 | return 1; |
| 197 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 198 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 199 | speed = extract_programmer_param("spispeed"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 200 | if (speed) { |
| 201 | for (i = 0; spispeeds[i].name; i++) |
| 202 | if (!strncasecmp(spispeeds[i].name, speed, |
| 203 | strlen(spispeeds[i].name))) { |
| 204 | spispeed = spispeeds[i].speed; |
| 205 | break; |
| 206 | } |
| 207 | if (!spispeeds[i].name) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 208 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 209 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 210 | free(speed); |
| 211 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 212 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 213 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 214 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 215 | /* Default buffer size is 19: 16 bytes data, 3 bytes control. */ |
| 216 | #define DEFAULT_BUFSIZE (16 + 3) |
| 217 | bp_commbuf = malloc(DEFAULT_BUFSIZE); |
| 218 | if (!bp_commbuf) { |
| 219 | bp_commbufsize = 0; |
| 220 | msg_perr("Out of memory!\n"); |
| 221 | return ERROR_OOM; |
| 222 | } |
| 223 | bp_commbufsize = DEFAULT_BUFSIZE; |
| 224 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 225 | ret = buspirate_serialport_setup(dev); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 226 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 227 | if (ret) { |
| 228 | bp_commbufsize = 0; |
| 229 | free(bp_commbuf); |
| 230 | bp_commbuf = NULL; |
| 231 | return ret; |
| 232 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 233 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 234 | if (register_shutdown(buspirate_spi_shutdown, NULL)) |
| 235 | return 1; |
| 236 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 237 | /* This is the brute force version, but it should work. */ |
| 238 | for (i = 0; i < 19; i++) { |
| 239 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 240 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 241 | /* Send the command, don't read the response. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 242 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 243 | if (ret) |
| 244 | return ret; |
| 245 | /* Read any response and discard it. */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 246 | sp_flush_incoming(); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 247 | } |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 248 | /* USB is slow. The Bus Pirate is even slower. Apparently the flush |
| 249 | * action above is too fast or too early. Some stuff still remains in |
| 250 | * the pipe after the flush above, and one additional flush is not |
| 251 | * sufficient either. Use a 1.5 ms delay inside the loop to make |
| 252 | * mostly sure that at least one USB frame had time to arrive. |
| 253 | * Looping only 5 times is not sufficient and causes the |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 254 | * occasional failure. |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 255 | * Folding the delay into the loop above is not reliable either. |
| 256 | */ |
| 257 | for (i = 0; i < 10; i++) { |
| 258 | usleep(1500); |
| 259 | /* Read any response and discard it. */ |
| 260 | sp_flush_incoming(); |
| 261 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 262 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 263 | bp_commbuf[0] = 0x00; |
| 264 | ret = buspirate_sendrecv(bp_commbuf, 1, 5); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 265 | if (ret) |
| 266 | return ret; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 267 | if (memcmp(bp_commbuf, "BBIO", 4)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 268 | msg_perr("Entering raw bitbang mode failed!\n"); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 269 | msg_pdbg("Got %02x%02x%02x%02x%02x\n", |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 270 | bp_commbuf[0], bp_commbuf[1], bp_commbuf[2], |
| 271 | bp_commbuf[3], bp_commbuf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 272 | return 1; |
| 273 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 274 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]); |
| 275 | if (bp_commbuf[4] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 276 | msg_perr("Can't handle raw bitbang mode version %c!\n", |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 277 | bp_commbuf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 278 | return 1; |
| 279 | } |
| 280 | /* Enter raw SPI mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 281 | bp_commbuf[0] = 0x01; |
| 282 | ret = buspirate_sendrecv(bp_commbuf, 1, 4); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 283 | if (ret) |
| 284 | return ret; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 285 | if (memcmp(bp_commbuf, "SPI", 3)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 286 | msg_perr("Entering raw SPI mode failed!\n"); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 287 | msg_pdbg("Got %02x%02x%02x%02x\n", |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 288 | bp_commbuf[0], bp_commbuf[1], bp_commbuf[2], |
| 289 | bp_commbuf[3]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 290 | return 1; |
| 291 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 292 | msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[3]); |
| 293 | if (bp_commbuf[3] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 294 | msg_perr("Can't handle raw SPI mode version %c!\n", |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 295 | bp_commbuf[3]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 300 | bp_commbuf[0] = 0x40 | 0xb; |
| 301 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 302 | if (ret) |
| 303 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 304 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 305 | msg_perr("Protocol error while setting power/CS/AUX!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 306 | return 1; |
| 307 | } |
| 308 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 309 | /* Set SPI speed */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 310 | bp_commbuf[0] = 0x60 | spispeed; |
| 311 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 312 | if (ret) |
| 313 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 314 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 315 | msg_perr("Protocol error while setting SPI speed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | /* Set SPI config: output type, idle, clock edge, sample */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 320 | bp_commbuf[0] = 0x80 | 0xa; |
| 321 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 322 | if (ret) |
| 323 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 324 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 325 | msg_perr("Protocol error while setting SPI config!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 330 | bp_commbuf[0] = 0x03; |
| 331 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 332 | if (ret) |
| 333 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 334 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 335 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 336 | return 1; |
| 337 | } |
| 338 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 339 | register_spi_programmer(&spi_programmer_buspirate); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 344 | static int buspirate_spi_send_command(struct flashctx *flash, |
| 345 | unsigned int writecnt, |
| 346 | unsigned int readcnt, |
| 347 | const unsigned char *writearr, |
| 348 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 349 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 350 | unsigned int i = 0; |
| 351 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 352 | |
| 353 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 354 | return SPI_INVALID_LENGTH; |
| 355 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 356 | /* 3 bytes extra for CS#, len, CS#. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 357 | if (buspirate_commbuf_grow(writecnt + readcnt + 3)) |
| 358 | return ERROR_OOM; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 359 | |
| 360 | /* Assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 361 | bp_commbuf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 362 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 363 | bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 364 | memcpy(bp_commbuf + i, writearr, writecnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 365 | i += writecnt; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 366 | memset(bp_commbuf + i, 0, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 367 | |
| 368 | i += readcnt; |
| 369 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 370 | bp_commbuf[i++] = 0x03; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 371 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 372 | ret = buspirate_sendrecv(bp_commbuf, i, i); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 373 | |
| 374 | if (ret) { |
| 375 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 376 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 379 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 380 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 381 | return SPI_GENERIC_ERROR; |
| 382 | } |
| 383 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 384 | if (bp_commbuf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 385 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 386 | return SPI_GENERIC_ERROR; |
| 387 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 388 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 389 | if (bp_commbuf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 390 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 391 | return SPI_GENERIC_ERROR; |
| 392 | } |
| 393 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 394 | /* Skip CS#, length, writearr. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 395 | memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 396 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 397 | return ret; |
| 398 | } |