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