blob: b5dfc2f7f5309178cd406a9211d962cc12e4aaae [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 );
102 have_device++;
103 }
104 free(arg);
105
106 if (!have_device) {
107 msg_perr("Error: No valid device specified.\n"
108 "Use flashrom -p pony_spi:dev=/dev/device\n");
109
110 return 1;
111 }
112 /* Register the shutdown callback. */
113 if (register_shutdown(serialport_shutdown, NULL)) {
114 return 1;
115 }
116 arg = extract_programmer_param("type");
117
118 if (arg) {
119 if (!strcasecmp( arg, "serbang")) {
120 pony_type = TYPE_SERBANG;
121 } else if (!strcasecmp( arg, "si_prog") ) {
122 pony_type = TYPE_SI_PROG;
123 } else {
124 msg_perr("Error: Invalid programmer type specified.\n");
125 free(arg);
126 return 1;
127 }
128 }
129 free(arg);
130
131 msg_pdbg("Using the %s programmer.\n", ((pony_type == TYPE_SI_PROG ) ? "SI-Prog" : "SERBANG"));
132 /*
133 * Detect if there is a SI-Prog compatible programmer connected.
134 */
135 pony_bitbang_set_cs(1);
136 pony_bitbang_set_mosi(1);
137
138 /* We toggle SCK while we keep MOSI and CS# on. */
139 for (i = 1; i <= 10; ++i) {
140 data_out = i & 1;
141 sp_set_pin(PIN_RTS, data_out);
142 programmer_delay( 1000 );
143 data_in = sp_get_pin(PIN_DSR);
144
145 if (data_out != data_in) {
146 have_prog = 0;
147 break;
148 }
149 }
150
151 if (!have_prog) {
152 msg_perr( "No SI-Prog compatible hardware detected.\n" );
153 return 1;
154 }
155
156 if (bitbang_spi_init(&bitbang_spi_master_pony)) {
157 return 1;
158 }
159 return 0;
160}