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