blob: 940cae1cefc427c0e7a71e58cf60c1fe3e1d6603 [file] [log] [blame]
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +00001/*
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 Teacada7c5452012-04-30 23:11:06 +000014 */
15
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000016/* 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 Teacada7c5452012-04-30 23:11:06 +000037 */
38
39#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000040#include <strings.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000041#include <string.h>
42
43#include "flash.h"
44#include "programmer.h"
45
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000046enum pony_type {
47 TYPE_SI_PROG,
48 TYPE_SERBANG,
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000049 TYPE_AJAWE
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000050};
51
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000052/* Pins for master->slave direction */
53static int pony_negate_cs = 1;
54static int pony_negate_sck = 0;
55static int pony_negate_mosi = 0;
56/* Pins for slave->master direction */
57static int pony_negate_miso = 0;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000058
Anastasia Klimchuk5f5eaeb2021-05-26 09:54:08 +100059static void pony_bitbang_set_cs(int val, void *spi_data)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000060{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000061 if (pony_negate_cs)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000062 val ^= 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000063
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000064 sp_set_pin(PIN_TXD, val);
65}
66
Anastasia Klimchuk5f5eaeb2021-05-26 09:54:08 +100067static void pony_bitbang_set_sck(int val, void *spi_data)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000068{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000069 if (pony_negate_sck)
70 val ^= 1;
71
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000072 sp_set_pin(PIN_RTS, val);
73}
74
Anastasia Klimchuk5f5eaeb2021-05-26 09:54:08 +100075static void pony_bitbang_set_mosi(int val, void *spi_data)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000076{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000077 if (pony_negate_mosi)
78 val ^= 1;
79
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000080 sp_set_pin(PIN_DTR, val);
81}
82
Anastasia Klimchuk5f5eaeb2021-05-26 09:54:08 +100083static int pony_bitbang_get_miso(void *spi_data)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000084{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000085 int tmp = sp_get_pin(PIN_CTS);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000086
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000087 if (pony_negate_miso)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000088 tmp ^= 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000089
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000090 return tmp;
91}
92
93static const struct bitbang_spi_master bitbang_spi_master_pony = {
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000094 .set_cs = pony_bitbang_set_cs,
95 .set_sck = pony_bitbang_set_sck,
96 .set_mosi = pony_bitbang_set_mosi,
97 .get_miso = pony_bitbang_get_miso,
98 .half_period = 0,
99};
100
Carl-Daniel Hailfinger8b6690c2016-03-13 16:24:09 +0000101static int pony_spi_shutdown(void *data)
102{
103 /* Shut down serial port communication */
104 int ret = serialport_shutdown(NULL);
105 if (ret)
106 msg_pdbg("Pony SPI shutdown failed.\n");
107 else
108 msg_pdbg("Pony SPI shutdown completed.\n");
109
110 return ret;
111}
112
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000113int pony_spi_init(void)
114{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000115 int i, data_out;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000116 char *arg = NULL;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000117 enum pony_type type = TYPE_SI_PROG;
Jacob Garber4a84ec22019-07-25 19:12:31 -0600118 const char *name;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000119 int have_device = 0;
120 int have_prog = 0;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000121
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000122 /* The parameter is in format "dev=/dev/device,type=serbang" */
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000123 arg = extract_programmer_param("dev");
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000124 if (arg && strlen(arg)) {
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000125 sp_fd = sp_openserport(arg, 9600);
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000126 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000127 free(arg);
128 return 1;
129 }
Carl-Daniel Hailfinger8b6690c2016-03-13 16:24:09 +0000130 if (register_shutdown(pony_spi_shutdown, NULL) != 0) {
131 free(arg);
132 serialport_shutdown(NULL);
133 return 1;
134 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000135 have_device++;
136 }
137 free(arg);
138
139 if (!have_device) {
140 msg_perr("Error: No valid device specified.\n"
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000141 "Use flashrom -p pony_spi:dev=/dev/device[,type=name]\n");
142 return 1;
143 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000144
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000145 arg = extract_programmer_param("type");
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000146 if (arg && !strcasecmp(arg, "serbang")) {
147 type = TYPE_SERBANG;
148 } else if (arg && !strcasecmp(arg, "si_prog")) {
149 type = TYPE_SI_PROG;
150 } else if (arg && !strcasecmp( arg, "ajawe")) {
151 type = TYPE_AJAWE;
152 } else if (arg && !strlen(arg)) {
153 msg_perr("Error: Missing argument for programmer type.\n");
154 free(arg);
Stefan Reinauerbbdde552014-04-26 16:11:30 +0000155 return 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000156 } else if (arg){
157 msg_perr("Error: Invalid programmer type specified.\n");
158 free(arg);
159 return 1;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000160 }
161 free(arg);
162
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000163 /*
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000164 * Configure the serial port pins, depending on the used programmer.
165 */
166 switch (type) {
167 case TYPE_AJAWE:
168 pony_negate_cs = 1;
169 pony_negate_sck = 1;
170 pony_negate_mosi = 1;
171 pony_negate_miso = 1;
172 name = "AJAWe";
173 break;
174 case TYPE_SERBANG:
175 pony_negate_cs = 0;
176 pony_negate_sck = 0;
177 pony_negate_mosi = 0;
178 pony_negate_miso = 1;
179 name = "serbang";
180 break;
181 default:
182 case TYPE_SI_PROG:
183 pony_negate_cs = 1;
184 pony_negate_sck = 0;
185 pony_negate_mosi = 0;
186 pony_negate_miso = 0;
187 name = "SI-Prog";
188 break;
189 }
190 msg_pdbg("Using %s programmer pinout.\n", name);
191
192 /*
193 * Detect if there is a compatible hardware programmer connected.
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000194 */
Anastasia Klimchuk5f5eaeb2021-05-26 09:54:08 +1000195 pony_bitbang_set_cs(1, NULL);
196 pony_bitbang_set_sck(1, NULL);
197 pony_bitbang_set_mosi(1, NULL);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000198
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000199 switch (type) {
200 case TYPE_AJAWE:
201 have_prog = 1;
202 break;
203 case TYPE_SI_PROG:
204 case TYPE_SERBANG:
205 default:
206 have_prog = 1;
207 /* We toggle RTS/SCK a few times and see if DSR changes too. */
208 for (i = 1; i <= 10; i++) {
209 data_out = i & 1;
210 sp_set_pin(PIN_RTS, data_out);
211 programmer_delay(1000);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000212
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000213 /* If DSR does not change, we are not connected to what we think */
214 if (data_out != sp_get_pin(PIN_DSR)) {
215 have_prog = 0;
216 break;
217 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000218 }
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000219 break;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000220 }
221
222 if (!have_prog) {
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000223 msg_perr("No programmer compatible with %s detected.\n", name);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000224 return 1;
225 }
226
Anastasia Klimchuk30815fc2021-05-31 11:20:01 +1000227 if (register_spi_bitbang_master(&bitbang_spi_master_pony, NULL)) {
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000228 return 1;
229 }
230 return 0;
231}