blob: 8a0f340f420fd48149d6e31b9a1956f6cd36092a [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.
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 Teacadabca202012-09-01 21:32:04 +000020/* 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 Teacada7c5452012-04-30 23:11:06 +000041 */
42
43#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000044#include <strings.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000045#include <string.h>
46
47#include "flash.h"
48#include "programmer.h"
49
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000050enum pony_type {
51 TYPE_SI_PROG,
52 TYPE_SERBANG,
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000053 TYPE_AJAWE
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000054};
55
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000056/* Pins for master->slave direction */
57static int pony_negate_cs = 1;
58static int pony_negate_sck = 0;
59static int pony_negate_mosi = 0;
60/* Pins for slave->master direction */
61static int pony_negate_miso = 0;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000062
63static void pony_bitbang_set_cs(int val)
64{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000065 if (pony_negate_cs)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000066 val ^= 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000067
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000068 sp_set_pin(PIN_TXD, val);
69}
70
71static void pony_bitbang_set_sck(int val)
72{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000073 if (pony_negate_sck)
74 val ^= 1;
75
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000076 sp_set_pin(PIN_RTS, val);
77}
78
79static void pony_bitbang_set_mosi(int val)
80{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000081 if (pony_negate_mosi)
82 val ^= 1;
83
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000084 sp_set_pin(PIN_DTR, val);
85}
86
87static int pony_bitbang_get_miso(void)
88{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000089 int tmp = sp_get_pin(PIN_CTS);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000090
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000091 if (pony_negate_miso)
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000092 tmp ^= 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +000093
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000094 return tmp;
95}
96
97static 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
106int pony_spi_init(void)
107{
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000108 int i, data_out;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000109 char *arg = NULL;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000110 enum pony_type type = TYPE_SI_PROG;
111 char *name;
112 int have_device = 0;
113 int have_prog = 0;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000114
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000115 /* The parameter is in format "dev=/dev/device,type=serbang" */
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000116 arg = extract_programmer_param("dev");
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000117 if (arg && strlen(arg)) {
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000118 sp_fd = sp_openserport(arg, 9600);
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000119 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000120 free(arg);
121 return 1;
122 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000123 have_device++;
124 }
125 free(arg);
126
127 if (!have_device) {
128 msg_perr("Error: No valid device specified.\n"
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000129 "Use flashrom -p pony_spi:dev=/dev/device[,type=name]\n");
130 return 1;
131 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000132
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000133 arg = extract_programmer_param("type");
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000134 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 Reinauerbbdde552014-04-26 16:11:30 +0000143 return 1;
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000144 } else if (arg){
145 msg_perr("Error: Invalid programmer type specified.\n");
146 free(arg);
147 return 1;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000148 }
149 free(arg);
150
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000151 /*
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000152 * 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 Teacada7c5452012-04-30 23:11:06 +0000182 */
183 pony_bitbang_set_cs(1);
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000184 pony_bitbang_set_sck(1);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000185 pony_bitbang_set_mosi(1);
186
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000187 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 Teacada7c5452012-04-30 23:11:06 +0000200
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000201 /* 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 Teacada7c5452012-04-30 23:11:06 +0000206 }
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000207 break;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000208 }
209
210 if (!have_prog) {
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000211 msg_perr("No programmer compatible with %s detected.\n", name);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000212 return 1;
213 }
214
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000215 if (register_spi_bitbang_master(&bitbang_spi_master_pony)) {
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000216 return 1;
217 }
218 return 0;
219}