James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2012 James Laird <jhl@mafipulation.org> |
| 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. |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * Device should be connected as per "active serial" mode: |
| 18 | * |
| 19 | * +---------+------+-----------+ |
| 20 | * | SPI | Pin | Altera | |
| 21 | * +---------+------+-----------+ |
| 22 | * | SCLK | 1 | DCLK | |
| 23 | * | GND | 2,10 | GND | |
| 24 | * | VCC | 4 | VCC(TRGT) | |
| 25 | * | MISO | 7 | DATAOUT | |
| 26 | * | /CS | 8 | nCS | |
| 27 | * | MOSI | 9 | ASDI | |
| 28 | * +---------+------+-----------+ |
| 29 | * |
| 30 | * See also the USB-Blaster Download Cable User Guide: http://www.altera.com/literature/ug/ug_usb_blstr.pdf |
| 31 | */ |
| 32 | |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <ctype.h> |
| 37 | #include <ftdi.h> |
| 38 | #include "flash.h" |
| 39 | #include "programmer.h" |
| 40 | #include "spi.h" |
| 41 | |
| 42 | /* Please keep sorted by vendor ID, then device ID. */ |
| 43 | #define ALTERA_VID 0x09fb |
| 44 | #define ALTERA_USBBLASTER_PID 0x6001 |
| 45 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 46 | static const struct dev_entry devs_usbblasterspi[] = { |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 47 | {ALTERA_VID, ALTERA_USBBLASTER_PID, OK, "Altera", "USB-Blaster"}, |
| 48 | |
Evgeny Zinoviev | 83c56b8 | 2019-11-05 17:47:43 +0300 | [diff] [blame] | 49 | {0} |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 52 | static const struct spi_master spi_master_usbblaster; |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 53 | |
| 54 | static struct ftdi_context ftdic; |
| 55 | |
| 56 | // command bytes |
| 57 | #define BIT_BYTE (1<<7) // byte mode (rather than bitbang) |
| 58 | #define BIT_READ (1<<6) // read request |
| 59 | #define BIT_LED (1<<5) |
| 60 | #define BIT_CS (1<<3) |
| 61 | #define BIT_TMS (1<<1) |
| 62 | #define BIT_CLK (1<<0) |
| 63 | |
| 64 | #define BUF_SIZE 64 |
| 65 | |
| 66 | /* The programmer shifts bits in the wrong order for SPI, so we use this method to reverse the bits when needed. |
| 67 | * http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */ |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 68 | static uint8_t reverse(uint8_t b) |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 69 | { |
| 70 | return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* Returns 0 upon success, a negative number upon errors. */ |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 75 | static int usbblaster_spi_init(struct flashprog_programmer *const prog) |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 76 | { |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 77 | uint8_t buf[BUF_SIZE + 1] = { 0 }; |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 78 | |
| 79 | if (ftdi_init(&ftdic) < 0) |
| 80 | return -1; |
| 81 | |
| 82 | if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_USBBLASTER_PID) < 0) { |
| 83 | msg_perr("Failed to open USB-Blaster: %s\n", ftdic.error_str); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | if (ftdi_usb_reset(&ftdic) < 0) { |
| 88 | msg_perr("USB-Blaster reset failed\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | if (ftdi_set_latency_timer(&ftdic, 2) < 0) { |
| 93 | msg_perr("USB-Blaster set latency timer failed\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | if (ftdi_write_data_set_chunksize(&ftdic, 4096) < 0 || |
| 98 | ftdi_read_data_set_chunksize(&ftdic, BUF_SIZE) < 0) { |
| 99 | msg_perr("USB-Blaster set chunk size failed\n"); |
| 100 | return -1; |
| 101 | } |
| 102 | |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 103 | buf[sizeof(buf)-1] = BIT_LED | BIT_CS; |
| 104 | if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) { |
| 105 | msg_perr("USB-Blaster reset write failed\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | if (ftdi_read_data(&ftdic, buf, sizeof(buf)) < 0) { |
| 109 | msg_perr("USB-Blaster reset read failed\n"); |
| 110 | return -1; |
| 111 | } |
| 112 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 113 | register_spi_master(&spi_master_usbblaster, 0, NULL); |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int send_write(unsigned int writecnt, const unsigned char *writearr) |
| 118 | { |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 119 | uint8_t buf[BUF_SIZE] = { 0 }; |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 120 | |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 121 | while (writecnt) { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 122 | unsigned int i; |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 123 | unsigned int n_write = min(writecnt, BUF_SIZE - 1); |
| 124 | msg_pspew("writing %d-byte packet\n", n_write); |
| 125 | |
| 126 | buf[0] = BIT_BYTE | (uint8_t)n_write; |
| 127 | for (i = 0; i < n_write; i++) { |
| 128 | buf[i+1] = reverse(writearr[i]); |
| 129 | } |
| 130 | if (ftdi_write_data(&ftdic, buf, n_write + 1) < 0) { |
| 131 | msg_perr("USB-Blaster write failed\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | writearr += n_write; |
| 136 | writecnt -= n_write; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int send_read(unsigned int readcnt, unsigned char *readarr) |
| 142 | { |
| 143 | int i; |
| 144 | unsigned int n_read; |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 145 | uint8_t buf[BUF_SIZE] = { 0 }; |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 146 | |
| 147 | n_read = readcnt; |
| 148 | while (n_read) { |
| 149 | unsigned int payload_size = min(n_read, BUF_SIZE - 1); |
| 150 | msg_pspew("reading %d-byte packet\n", payload_size); |
| 151 | |
| 152 | buf[0] = BIT_BYTE | BIT_READ | (uint8_t)payload_size; |
| 153 | if (ftdi_write_data(&ftdic, buf, payload_size + 1) < 0) { |
| 154 | msg_perr("USB-Blaster write failed\n"); |
| 155 | return -1; |
| 156 | } |
| 157 | n_read -= payload_size; |
David Hendricks | cdb290e | 2020-06-23 14:16:26 -0700 | [diff] [blame] | 158 | } |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 159 | |
| 160 | n_read = readcnt; |
| 161 | while (n_read) { |
| 162 | int ret = ftdi_read_data(&ftdic, readarr, n_read); |
| 163 | if (ret < 0) { |
| 164 | msg_perr("USB-Blaster read failed\n"); |
| 165 | return -1; |
| 166 | } |
| 167 | for (i = 0; i < ret; i++) { |
| 168 | readarr[i] = reverse(readarr[i]); |
| 169 | } |
| 170 | n_read -= ret; |
| 171 | readarr += ret; |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* Returns 0 upon success, a negative number upon errors. */ |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 177 | static int usbblaster_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 178 | const unsigned char *writearr, unsigned char *readarr) |
| 179 | { |
| 180 | uint8_t cmd; |
| 181 | int ret = 0; |
| 182 | |
| 183 | cmd = BIT_LED; // asserts /CS |
| 184 | if (ftdi_write_data(&ftdic, &cmd, 1) < 0) { |
| 185 | msg_perr("USB-Blaster enable chip select failed\n"); |
| 186 | ret = -1; |
| 187 | } |
| 188 | |
| 189 | if (!ret && writecnt) |
| 190 | ret = send_write(writecnt, writearr); |
| 191 | |
| 192 | if (!ret && readcnt) |
| 193 | ret = send_read(readcnt, readarr); |
| 194 | |
| 195 | cmd = BIT_CS; |
| 196 | if (ftdi_write_data(&ftdic, &cmd, 1) < 0) { |
| 197 | msg_perr("USB-Blaster disable chip select failed\n"); |
| 198 | ret = -1; |
| 199 | } |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 205 | static const struct spi_master spi_master_usbblaster = { |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 206 | .max_data_read = 256, |
| 207 | .max_data_write = 256, |
| 208 | .command = usbblaster_spi_send_command, |
| 209 | .multicommand = default_spi_send_multicommand, |
| 210 | .read = default_spi_read, |
| 211 | .write_256 = default_spi_write_256, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 212 | .probe_opcode = default_spi_probe_opcode, |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 215 | const struct programmer_entry programmer_usbblaster_spi = { |
| 216 | .name = "usbblaster_spi", |
| 217 | .type = USB, |
| 218 | .devs.dev = devs_usbblasterspi, |
| 219 | .init = usbblaster_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 220 | }; |