blob: 7875200de17bc0cdfe059ee5d577400d29245471 [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);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000119 if (sp_fd < 0) {
120 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);
143 } else if (arg){
144 msg_perr("Error: Invalid programmer type specified.\n");
145 free(arg);
146 return 1;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000147 }
148 free(arg);
149
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000150 /*
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000151 * Configure the serial port pins, depending on the used programmer.
152 */
153 switch (type) {
154 case TYPE_AJAWE:
155 pony_negate_cs = 1;
156 pony_negate_sck = 1;
157 pony_negate_mosi = 1;
158 pony_negate_miso = 1;
159 name = "AJAWe";
160 break;
161 case TYPE_SERBANG:
162 pony_negate_cs = 0;
163 pony_negate_sck = 0;
164 pony_negate_mosi = 0;
165 pony_negate_miso = 1;
166 name = "serbang";
167 break;
168 default:
169 case TYPE_SI_PROG:
170 pony_negate_cs = 1;
171 pony_negate_sck = 0;
172 pony_negate_mosi = 0;
173 pony_negate_miso = 0;
174 name = "SI-Prog";
175 break;
176 }
177 msg_pdbg("Using %s programmer pinout.\n", name);
178
179 /*
180 * Detect if there is a compatible hardware programmer connected.
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000181 */
182 pony_bitbang_set_cs(1);
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000183 pony_bitbang_set_sck(1);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000184 pony_bitbang_set_mosi(1);
185
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000186 switch (type) {
187 case TYPE_AJAWE:
188 have_prog = 1;
189 break;
190 case TYPE_SI_PROG:
191 case TYPE_SERBANG:
192 default:
193 have_prog = 1;
194 /* We toggle RTS/SCK a few times and see if DSR changes too. */
195 for (i = 1; i <= 10; i++) {
196 data_out = i & 1;
197 sp_set_pin(PIN_RTS, data_out);
198 programmer_delay(1000);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000199
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000200 /* If DSR does not change, we are not connected to what we think */
201 if (data_out != sp_get_pin(PIN_DSR)) {
202 have_prog = 0;
203 break;
204 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000205 }
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000206 break;
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000207 }
208
209 if (!have_prog) {
Virgil-Adrian Teacadabca202012-09-01 21:32:04 +0000210 msg_perr("No programmer compatible with %s detected.\n", name);
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000211 return 1;
212 }
213
214 if (bitbang_spi_init(&bitbang_spi_master_pony)) {
215 return 1;
216 }
217 return 0;
218}