blob: 189341ad3750e0c94ae7e02d3206de7af0940a21 [file] [log] [blame]
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
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
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000020/* Driver for various LPT adapters.
21 *
22 * This driver uses non-portable direct I/O port accesses which won't work on
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000023 * any non-x86 platform, and even on x86 there is a high chance there will be
24 * collisions with any loaded parallel port drivers.
25 * The big advantage of direct port I/O is OS independence and speed because
26 * most OS parport drivers will perform many unnecessary accesses although
27 * this driver just treats the parallel port as a GPIO set.
28 */
29#if defined(__i386__) || defined(__x86_64__)
30
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000031#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000032#include <strings.h>
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000033#include <string.h>
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000034#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000035#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000036#include "hwaccess.h"
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000037
38/* We have two sets of pins, out and in. The numbers for both sets are
39 * independent and are bitshift values, not real pin numbers.
Paul Menzel018d4822011-10-21 12:33:07 +000040 * Default settings are for the RayeR hardware.
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000041 */
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000042
43struct rayer_programmer {
44 const char *type;
45 const enum test_state status;
46 const char *description;
47 const void *dev_data;
48};
49
50struct rayer_pinout {
51 uint8_t cs_bit;
52 uint8_t sck_bit;
53 uint8_t mosi_bit;
54 uint8_t miso_bit;
55 void (*preinit)(const void *);
56 int (*shutdown)(void *);
57};
58
59static const struct rayer_pinout rayer_spipgm = {
60 .cs_bit = 5,
61 .sck_bit = 6,
62 .mosi_bit = 7,
63 .miso_bit = 6,
64};
65
Kyösti Mälkki1d473792013-10-02 01:22:17 +000066static void dlc5_preinit(const void *);
67static int dlc5_shutdown(void *);
68
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000069static const struct rayer_pinout xilinx_dlc5 = {
70 .cs_bit = 2,
71 .sck_bit = 1,
72 .mosi_bit = 0,
73 .miso_bit = 4,
Kyösti Mälkki1d473792013-10-02 01:22:17 +000074 .preinit = dlc5_preinit,
75 .shutdown = dlc5_shutdown,
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000076};
77
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +000078static void byteblaster_preinit(const void *);
79static int byteblaster_shutdown(void *);
80
81static const struct rayer_pinout altera_byteblastermv = {
82 .cs_bit = 1,
83 .sck_bit = 0,
84 .mosi_bit = 6,
85 .miso_bit = 7,
86 .preinit = byteblaster_preinit,
87 .shutdown = byteblaster_shutdown,
88};
89
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +000090static void stk200_preinit(const void *);
91static int stk200_shutdown(void *);
92
93static const struct rayer_pinout atmel_stk200 = {
94 .cs_bit = 7,
95 .sck_bit = 4,
96 .mosi_bit = 5,
97 .miso_bit = 6,
98 .preinit = stk200_preinit,
99 .shutdown = stk200_shutdown,
100};
101
Maksim Kuleshovacba2ac2013-10-02 01:22:11 +0000102static const struct rayer_pinout wiggler_lpt = {
103 .cs_bit = 1,
104 .sck_bit = 2,
105 .mosi_bit = 3,
106 .miso_bit = 7,
107};
108
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000109static const struct rayer_programmer rayer_spi_types[] = {
110 {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm},
111 {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5},
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +0000112 {"byteblastermv", OK, "Altera ByteBlasterMV", &altera_byteblastermv},
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +0000113 {"stk200", NT, "Atmel STK200/300 adapter", &atmel_stk200},
Maksim Kuleshovacba2ac2013-10-02 01:22:11 +0000114 {"wiggler", OK, "Wiggler LPT", &wiggler_lpt},
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000115 {0},
116};
117
118static const struct rayer_pinout *pinout = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000119
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000120static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000121
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000122/* Cached value of last byte sent. */
123static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000124
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000125static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000126{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000127 lpt_outbyte &= ~(1 << pinout->cs_bit);
128 lpt_outbyte |= (val << pinout->cs_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000129 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000130}
131
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000132static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000133{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000134 lpt_outbyte &= ~(1 << pinout->sck_bit);
135 lpt_outbyte |= (val << pinout->sck_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000136 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000137}
138
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000139static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000140{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000141 lpt_outbyte &= ~(1 << pinout->mosi_bit);
142 lpt_outbyte |= (val << pinout->mosi_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000143 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000144}
145
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000146static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000147{
148 uint8_t tmp;
149
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000150 tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted
151 tmp = (tmp >> pinout->miso_bit) & 0x1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000152 return tmp;
153}
154
155static const struct bitbang_spi_master bitbang_spi_master_rayer = {
156 .type = BITBANG_SPI_MASTER_RAYER,
157 .set_cs = rayer_bitbang_set_cs,
158 .set_sck = rayer_bitbang_set_sck,
159 .set_mosi = rayer_bitbang_set_mosi,
160 .get_miso = rayer_bitbang_get_miso,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000161 .half_period = 0,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000162};
163
164int rayer_spi_init(void)
165{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000166 const struct rayer_programmer *prog = rayer_spi_types;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000167 char *arg = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000168
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000169 /* Non-default port requested? */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000170 arg = extract_programmer_param("iobase");
171 if (arg) {
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000172 char *endptr = NULL;
173 unsigned long tmp;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000174 tmp = strtoul(arg, &endptr, 0);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000175 /* Port 0, port >0x10000, unaligned ports and garbage strings
176 * are rejected.
177 */
178 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
179 (*endptr != '\0')) {
180 /* Using ports below 0x100 is a really bad idea, and
181 * should only be done if no port between 0x100 and
182 * 0xfffc works due to routing issues.
183 */
184 msg_perr("Error: iobase= specified, but the I/O base "
185 "given was invalid.\nIt must be a multiple of "
186 "0x4 and lie between 0x100 and 0xfffc.\n");
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000187 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000188 return 1;
189 } else {
190 lpt_iobase = (uint16_t)tmp;
191 msg_pinfo("Non-default I/O base requested. This will "
192 "not change the hardware settings.\n");
193 }
194 } else {
195 /* Pick a default value for the I/O base. */
196 lpt_iobase = 0x378;
197 }
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000198 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000199
200 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000201 lpt_iobase);
202
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000203 arg = extract_programmer_param("type");
204 if (arg) {
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000205 for (; prog->type != NULL; prog++) {
206 if (strcasecmp(arg, prog->type) == 0) {
207 break;
208 }
209 }
210 if (prog->type == NULL) {
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000211 msg_perr("Error: Invalid device type specified.\n");
212 free(arg);
213 return 1;
214 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000215 free(arg);
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000216 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000217 msg_pinfo("Using %s pinout.\n", prog->description);
218 pinout = (struct rayer_pinout *)prog->dev_data;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000219
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000220 if (rget_io_perms())
221 return 1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000222
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000223 /* Get the initial value before writing to any line. */
224 lpt_outbyte = INB(lpt_iobase);
225
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000226 if (pinout->shutdown)
227 register_shutdown(pinout->shutdown, (void*)pinout);
228 if (pinout->preinit)
229 pinout->preinit(pinout);
230
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000231 if (bitbang_spi_init(&bitbang_spi_master_rayer))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000232 return 1;
233
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000234 return 0;
235}
236
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +0000237static void byteblaster_preinit(const void *data){
238 msg_pdbg("byteblaster_preinit\n");
239 /* Assert #EN signal. */
240 OUTB(2, lpt_iobase + 2 );
241}
242
243static int byteblaster_shutdown(void *data){
244 msg_pdbg("byteblaster_shutdown\n");
245 /* De-Assert #EN signal. */
246 OUTB(0, lpt_iobase + 2 );
247 return 0;
248}
249
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +0000250static void stk200_preinit(const void *data) {
251 msg_pdbg("stk200_init\n");
252 /* Assert #EN signals, set LED signal. */
253 lpt_outbyte = (1 << 6) ;
254 OUTB(lpt_outbyte, lpt_iobase);
255}
256
257static int stk200_shutdown(void *data) {
258 msg_pdbg("stk200_shutdown\n");
259 /* Assert #EN signals, clear LED signal. */
260 lpt_outbyte = (1 << 2) | (1 << 3);
261 OUTB(lpt_outbyte, lpt_iobase);
262 return 0;
263}
264
Kyösti Mälkki1d473792013-10-02 01:22:17 +0000265static void dlc5_preinit(const void *data) {
266 msg_pdbg("dlc5_preinit\n");
267 /* Assert pin 6 to receive MISO. */
268 lpt_outbyte |= (1<<4);
269 OUTB(lpt_outbyte, lpt_iobase);
270}
271
272static int dlc5_shutdown(void *data) {
273 msg_pdbg("dlc5_shutdown\n");
274 /* De-assert pin 6 to force MISO low. */
275 lpt_outbyte &= ~(1<<4);
276 OUTB(lpt_outbyte, lpt_iobase);
277 return 0;
278}
279
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000280#else
281#error PCI port I/O access is not supported on this architecture yet.
282#endif