Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Virgil-Adrian Teaca |
| 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. |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 16 | /* Driver for serial programmers compatible with SI-Prog or AJAWe. |
| 17 | * |
| 18 | * See http://www.lancos.com/siprogsch.html for SI-Prog schematics and instructions. |
| 19 | * See http://www.ajawe.pl/ajawe0208.htm for AJAWe serial programmer documentation. |
| 20 | * |
| 21 | * Pin layout for SI-Prog-like hardware: |
| 22 | * |
| 23 | * MOSI <-------< DTR |
| 24 | * MISO >-------> CTS |
| 25 | * SCK <---+---< RTS |
| 26 | * +---> DSR |
| 27 | * CS# <-------< TXD |
| 28 | * |
| 29 | * and for the AJAWe serial programmer: |
| 30 | * |
| 31 | * MOSI <-------< DTR |
| 32 | * MISO >-------> CTS |
| 33 | * SCK <-------< RTS |
| 34 | * CS# <-------< TXD |
| 35 | * |
| 36 | * DCE >-------> DSR |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 40 | #include <strings.h> |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
| 43 | #include "flash.h" |
| 44 | #include "programmer.h" |
Nico Huber | d16a911 | 2024-01-07 00:11:44 +0100 | [diff] [blame^] | 45 | #include "bitbang_spi.h" |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 46 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 47 | enum pony_type { |
| 48 | TYPE_SI_PROG, |
| 49 | TYPE_SERBANG, |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 50 | TYPE_AJAWE |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 53 | /* Pins for master->slave direction */ |
| 54 | static int pony_negate_cs = 1; |
| 55 | static int pony_negate_sck = 0; |
| 56 | static int pony_negate_mosi = 0; |
| 57 | /* Pins for slave->master direction */ |
| 58 | static int pony_negate_miso = 0; |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 59 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 60 | static void pony_bitbang_set_cs(int val, void *spi_data) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 61 | { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 62 | if (pony_negate_cs) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 63 | val ^= 1; |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 64 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 65 | sp_set_pin(PIN_TXD, val); |
| 66 | } |
| 67 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 68 | static void pony_bitbang_set_sck(int val, void *spi_data) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 69 | { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 70 | if (pony_negate_sck) |
| 71 | val ^= 1; |
| 72 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 73 | sp_set_pin(PIN_RTS, val); |
| 74 | } |
| 75 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 76 | static void pony_bitbang_set_mosi(int val, void *spi_data) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 77 | { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 78 | if (pony_negate_mosi) |
| 79 | val ^= 1; |
| 80 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 81 | sp_set_pin(PIN_DTR, val); |
| 82 | } |
| 83 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 84 | static int pony_bitbang_get_miso(void *spi_data) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 85 | { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 86 | int tmp = sp_get_pin(PIN_CTS); |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 87 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 88 | if (pony_negate_miso) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 89 | tmp ^= 1; |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 90 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 91 | return tmp; |
| 92 | } |
| 93 | |
| 94 | static const struct bitbang_spi_master bitbang_spi_master_pony = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 95 | .set_cs = pony_bitbang_set_cs, |
| 96 | .set_sck = pony_bitbang_set_sck, |
| 97 | .set_mosi = pony_bitbang_set_mosi, |
| 98 | .get_miso = pony_bitbang_get_miso, |
| 99 | .half_period = 0, |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Carl-Daniel Hailfinger | 8b6690c | 2016-03-13 16:24:09 +0000 | [diff] [blame] | 102 | static int pony_spi_shutdown(void *data) |
| 103 | { |
| 104 | /* Shut down serial port communication */ |
| 105 | int ret = serialport_shutdown(NULL); |
| 106 | if (ret) |
| 107 | msg_pdbg("Pony SPI shutdown failed.\n"); |
| 108 | else |
| 109 | msg_pdbg("Pony SPI shutdown completed.\n"); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 114 | static int pony_spi_init(struct flashprog_programmer *const prog) |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 115 | { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 116 | int i, data_out; |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 117 | char *arg = NULL; |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 118 | enum pony_type type = TYPE_SI_PROG; |
Jacob Garber | 4a84ec2 | 2019-07-25 19:12:31 -0600 | [diff] [blame] | 119 | const char *name; |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 120 | int have_device = 0; |
| 121 | int have_prog = 0; |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 122 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 123 | /* The parameter is in format "dev=/dev/device,type=serbang" */ |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 124 | arg = extract_programmer_param("dev"); |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 125 | if (arg && strlen(arg)) { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 126 | sp_fd = sp_openserport(arg, 9600); |
Stefan Tauner | acfc4c6 | 2012-11-30 16:46:45 +0000 | [diff] [blame] | 127 | if (sp_fd == SER_INV_FD) { |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 128 | free(arg); |
| 129 | return 1; |
| 130 | } |
Carl-Daniel Hailfinger | 8b6690c | 2016-03-13 16:24:09 +0000 | [diff] [blame] | 131 | if (register_shutdown(pony_spi_shutdown, NULL) != 0) { |
| 132 | free(arg); |
| 133 | serialport_shutdown(NULL); |
| 134 | return 1; |
| 135 | } |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 136 | have_device++; |
| 137 | } |
| 138 | free(arg); |
| 139 | |
| 140 | if (!have_device) { |
| 141 | msg_perr("Error: No valid device specified.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 142 | "Use flashprog -p pony_spi:dev=/dev/device[,type=name]\n"); |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 143 | return 1; |
| 144 | } |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 145 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 146 | arg = extract_programmer_param("type"); |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 147 | if (arg && !strcasecmp(arg, "serbang")) { |
| 148 | type = TYPE_SERBANG; |
| 149 | } else if (arg && !strcasecmp(arg, "si_prog")) { |
| 150 | type = TYPE_SI_PROG; |
| 151 | } else if (arg && !strcasecmp( arg, "ajawe")) { |
| 152 | type = TYPE_AJAWE; |
| 153 | } else if (arg && !strlen(arg)) { |
| 154 | msg_perr("Error: Missing argument for programmer type.\n"); |
| 155 | free(arg); |
Stefan Reinauer | bbdde55 | 2014-04-26 16:11:30 +0000 | [diff] [blame] | 156 | return 1; |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 157 | } else if (arg){ |
| 158 | msg_perr("Error: Invalid programmer type specified.\n"); |
| 159 | free(arg); |
| 160 | return 1; |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 161 | } |
| 162 | free(arg); |
| 163 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 164 | /* |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 165 | * Configure the serial port pins, depending on the used programmer. |
| 166 | */ |
| 167 | switch (type) { |
| 168 | case TYPE_AJAWE: |
| 169 | pony_negate_cs = 1; |
| 170 | pony_negate_sck = 1; |
| 171 | pony_negate_mosi = 1; |
| 172 | pony_negate_miso = 1; |
| 173 | name = "AJAWe"; |
| 174 | break; |
| 175 | case TYPE_SERBANG: |
| 176 | pony_negate_cs = 0; |
| 177 | pony_negate_sck = 0; |
| 178 | pony_negate_mosi = 0; |
| 179 | pony_negate_miso = 1; |
| 180 | name = "serbang"; |
| 181 | break; |
| 182 | default: |
| 183 | case TYPE_SI_PROG: |
| 184 | pony_negate_cs = 1; |
| 185 | pony_negate_sck = 0; |
| 186 | pony_negate_mosi = 0; |
| 187 | pony_negate_miso = 0; |
| 188 | name = "SI-Prog"; |
| 189 | break; |
| 190 | } |
| 191 | msg_pdbg("Using %s programmer pinout.\n", name); |
| 192 | |
| 193 | /* |
| 194 | * Detect if there is a compatible hardware programmer connected. |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 195 | */ |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 196 | pony_bitbang_set_cs(1, NULL); |
| 197 | pony_bitbang_set_sck(1, NULL); |
| 198 | pony_bitbang_set_mosi(1, NULL); |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 199 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 200 | switch (type) { |
| 201 | case TYPE_AJAWE: |
| 202 | have_prog = 1; |
| 203 | break; |
| 204 | case TYPE_SI_PROG: |
| 205 | case TYPE_SERBANG: |
| 206 | default: |
| 207 | have_prog = 1; |
| 208 | /* We toggle RTS/SCK a few times and see if DSR changes too. */ |
| 209 | for (i = 1; i <= 10; i++) { |
| 210 | data_out = i & 1; |
| 211 | sp_set_pin(PIN_RTS, data_out); |
| 212 | programmer_delay(1000); |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 213 | |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 214 | /* If DSR does not change, we are not connected to what we think */ |
| 215 | if (data_out != sp_get_pin(PIN_DSR)) { |
| 216 | have_prog = 0; |
| 217 | break; |
| 218 | } |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 219 | } |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 220 | break; |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | if (!have_prog) { |
Virgil-Adrian Teaca | dabca20 | 2012-09-01 21:32:04 +0000 | [diff] [blame] | 224 | msg_perr("No programmer compatible with %s detected.\n", name); |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
Anastasia Klimchuk | a447c12 | 2021-05-31 11:20:01 +1000 | [diff] [blame] | 228 | if (register_spi_bitbang_master(&bitbang_spi_master_pony, NULL)) { |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 233 | |
| 234 | const struct programmer_entry programmer_pony_spi = { |
| 235 | .name = "pony_spi", |
| 236 | .type = OTHER, |
| 237 | /* FIXME */ |
| 238 | .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n", |
| 239 | .init = pony_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 240 | }; |