blob: fb90a3350015c6fad204ffb775bb05c83f7fa245 [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
20/* Driver for the SI-Prog like hardware by Lancos.com.
21 * See http://www.lancos.com/siprogsch.html for schematics and instructions.
22 */
23
24#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000025#include <strings.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000026#include <string.h>
27
28#include "flash.h"
29#include "programmer.h"
30
31/* We have:
32
33 MOSI -----> DTR
34 MISO -----> CTS
35 SCK --+--> RTS
36 +--> DSR
37 CS# -----> TXD
38
39*/
40
41enum pony_type {
42 TYPE_SI_PROG,
43 TYPE_SERBANG,
44};
45
46/* The hardware programmer used. */
47static enum pony_type pony_type = TYPE_SI_PROG;
48
49static void pony_bitbang_set_cs(int val)
50{
51 if (pony_type == TYPE_SI_PROG)
52 {
53 /* CS# is negated by the SI-Prog programmer. */
54 val ^= 1;
55 }
56 sp_set_pin(PIN_TXD, val);
57}
58
59static void pony_bitbang_set_sck(int val)
60{
61 sp_set_pin(PIN_RTS, val);
62}
63
64static void pony_bitbang_set_mosi(int val)
65{
66 sp_set_pin(PIN_DTR, val);
67}
68
69static int pony_bitbang_get_miso(void)
70{
71 int tmp;
72
73 tmp = sp_get_pin(PIN_CTS);
74
75 if (pony_type == TYPE_SERBANG)
76 {
77 /* MISO is negated by the SERBANG programmer. */
78 tmp ^= 1;
79 }
80 return tmp;
81}
82
83static const struct bitbang_spi_master bitbang_spi_master_pony = {
84 .type = BITBANG_SPI_MASTER_PONY,
85 .set_cs = pony_bitbang_set_cs,
86 .set_sck = pony_bitbang_set_sck,
87 .set_mosi = pony_bitbang_set_mosi,
88 .get_miso = pony_bitbang_get_miso,
89 .half_period = 0,
90};
91
92int pony_spi_init(void)
93{
94 char *arg = NULL;
95 int i, data_in, data_out, have_device = 0;
96 int have_prog = 1;
97
98 /* The parameter is on format "dev=/dev/device,type=serbang" */
99 arg = extract_programmer_param("dev");
100
101 if (arg && strlen(arg)) {
102 sp_fd = sp_openserport( arg, 9600 );
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000103 if (sp_fd < 0) {
104 free(arg);
105 return 1;
106 }
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000107 have_device++;
108 }
109 free(arg);
110
111 if (!have_device) {
112 msg_perr("Error: No valid device specified.\n"
113 "Use flashrom -p pony_spi:dev=/dev/device\n");
114
115 return 1;
116 }
117 /* Register the shutdown callback. */
118 if (register_shutdown(serialport_shutdown, NULL)) {
119 return 1;
120 }
121 arg = extract_programmer_param("type");
122
123 if (arg) {
124 if (!strcasecmp( arg, "serbang")) {
125 pony_type = TYPE_SERBANG;
126 } else if (!strcasecmp( arg, "si_prog") ) {
127 pony_type = TYPE_SI_PROG;
128 } else {
129 msg_perr("Error: Invalid programmer type specified.\n");
130 free(arg);
131 return 1;
132 }
133 }
134 free(arg);
135
136 msg_pdbg("Using the %s programmer.\n", ((pony_type == TYPE_SI_PROG ) ? "SI-Prog" : "SERBANG"));
137 /*
138 * Detect if there is a SI-Prog compatible programmer connected.
139 */
140 pony_bitbang_set_cs(1);
141 pony_bitbang_set_mosi(1);
142
143 /* We toggle SCK while we keep MOSI and CS# on. */
144 for (i = 1; i <= 10; ++i) {
145 data_out = i & 1;
146 sp_set_pin(PIN_RTS, data_out);
147 programmer_delay( 1000 );
148 data_in = sp_get_pin(PIN_DSR);
149
150 if (data_out != data_in) {
151 have_prog = 0;
152 break;
153 }
154 }
155
156 if (!have_prog) {
157 msg_perr( "No SI-Prog compatible hardware detected.\n" );
158 return 1;
159 }
160
161 if (bitbang_spi_init(&bitbang_spi_master_pony)) {
162 return 1;
163 }
164 return 0;
165}