blob: cde008f7c87a6e7877914c8d9406716bbd29dd1e [file] [log] [blame]
hailfinger52c4fa02010-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.
Edward O'Callaghande191932019-09-09 00:50:34 +100014 */
15
16/* Driver for various LPT adapters.
hailfinger52c4fa02010-07-21 10:26:01 +000017 *
Edward O'Callaghande191932019-09-09 00:50:34 +100018 * This driver uses non-portable direct I/O port accesses which won't work on
hailfinger52c4fa02010-07-21 10:26:01 +000019 * any non-x86 platform, and even on x86 there is a high chance there will be
20 * collisions with any loaded parallel port drivers.
21 * The big advantage of direct port I/O is OS independence and speed because
22 * most OS parport drivers will perform many unnecessary accesses although
23 * this driver just treats the parallel port as a GPIO set.
24 */
25#if defined(__i386__) || defined(__x86_64__)
26
hailfinger08cf2ed2010-10-05 19:19:48 +000027#include <stdlib.h>
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100028#include <strings.h>
hailfingerb9b9ccf2011-09-12 06:17:06 +000029#include <string.h>
hailfinger52c4fa02010-07-21 10:26:01 +000030#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000031#include "programmer.h"
Mayur Panchalf4796862019-08-05 15:46:12 +100032#include "hwaccess.h"
hailfinger52c4fa02010-07-21 10:26:01 +000033
Anastasia Klimchuk1ae39472021-03-17 12:11:35 +110034static uint16_t lpt_iobase;
35
36/* Cached value of last byte sent. */
37static uint8_t lpt_outbyte;
38
hailfinger52c4fa02010-07-21 10:26:01 +000039/* We have two sets of pins, out and in. The numbers for both sets are
40 * independent and are bitshift values, not real pin numbers.
stefanct98d917c2011-10-21 12:33:07 +000041 * Default settings are for the RayeR hardware.
hailfinger52c4fa02010-07-21 10:26:01 +000042 */
Edward O'Callaghande191932019-09-09 00:50:34 +100043
44struct rayer_programmer {
45 const char *type;
46 const enum test_state status;
47 const char *description;
48 const void *dev_data;
49};
50
51struct rayer_pinout {
52 uint8_t cs_bit;
53 uint8_t sck_bit;
54 uint8_t mosi_bit;
55 uint8_t miso_bit;
56 void (*preinit)(const void *);
57 int (*shutdown)(void *);
58};
59
60static const struct rayer_pinout rayer_spipgm = {
61 .cs_bit = 5,
62 .sck_bit = 6,
63 .mosi_bit = 7,
64 .miso_bit = 6,
65};
66
Anastasia Klimchuk1ae39472021-03-17 12:11:35 +110067static void dlc5_preinit(const void *data) {
68 msg_pdbg("dlc5_preinit\n");
69 /* Assert pin 6 to receive MISO. */
70 lpt_outbyte |= (1<<4);
71 OUTB(lpt_outbyte, lpt_iobase);
72}
73
74static int dlc5_shutdown(void *data) {
75 msg_pdbg("dlc5_shutdown\n");
76 /* De-assert pin 6 to force MISO low. */
77 lpt_outbyte &= ~(1<<4);
78 OUTB(lpt_outbyte, lpt_iobase);
79 return 0;
80}
Edward O'Callaghande191932019-09-09 00:50:34 +100081
82static const struct rayer_pinout xilinx_dlc5 = {
83 .cs_bit = 2,
84 .sck_bit = 1,
85 .mosi_bit = 0,
86 .miso_bit = 4,
87 .preinit = dlc5_preinit,
88 .shutdown = dlc5_shutdown,
89};
90
Anastasia Klimchuk1ae39472021-03-17 12:11:35 +110091static void byteblaster_preinit(const void *data){
92 msg_pdbg("byteblaster_preinit\n");
93 /* Assert #EN signal. */
94 OUTB(2, lpt_iobase + 2 );
95}
96
97static int byteblaster_shutdown(void *data){
98 msg_pdbg("byteblaster_shutdown\n");
99 /* De-Assert #EN signal. */
100 OUTB(0, lpt_iobase + 2 );
101 return 0;
102}
Edward O'Callaghande191932019-09-09 00:50:34 +1000103
104static const struct rayer_pinout altera_byteblastermv = {
105 .cs_bit = 1,
106 .sck_bit = 0,
107 .mosi_bit = 6,
108 .miso_bit = 7,
109 .preinit = byteblaster_preinit,
110 .shutdown = byteblaster_shutdown,
111};
112
Anastasia Klimchuk1ae39472021-03-17 12:11:35 +1100113static void stk200_preinit(const void *data) {
114 msg_pdbg("stk200_init\n");
115 /* Assert #EN signals, set LED signal. */
116 lpt_outbyte = (1 << 6) ;
117 OUTB(lpt_outbyte, lpt_iobase);
118}
119
120static int stk200_shutdown(void *data) {
121 msg_pdbg("stk200_shutdown\n");
122 /* Assert #EN signals, clear LED signal. */
123 lpt_outbyte = (1 << 2) | (1 << 3);
124 OUTB(lpt_outbyte, lpt_iobase);
125 return 0;
126}
Edward O'Callaghande191932019-09-09 00:50:34 +1000127
128static const struct rayer_pinout atmel_stk200 = {
129 .cs_bit = 7,
130 .sck_bit = 4,
131 .mosi_bit = 5,
132 .miso_bit = 6,
133 .preinit = stk200_preinit,
134 .shutdown = stk200_shutdown,
135};
136
137static const struct rayer_pinout wiggler_lpt = {
138 .cs_bit = 1,
139 .sck_bit = 2,
140 .mosi_bit = 3,
141 .miso_bit = 7,
142};
143
144static const struct rayer_pinout spi_tt = {
145 .cs_bit = 2,
146 .sck_bit = 0,
147 .mosi_bit = 4,
148 .miso_bit = 7,
149};
150
151static const struct rayer_programmer rayer_spi_types[] = {
152 {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm},
153 {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5},
154 {"byteblastermv", OK, "Altera ByteBlasterMV", &altera_byteblastermv},
155 {"stk200", NT, "Atmel STK200/300 adapter", &atmel_stk200},
156 {"wiggler", OK, "Wiggler LPT", &wiggler_lpt},
157 {"spi_tt", NT, "SPI Tiny Tools (SPI_TT LPT)", &spi_tt},
158 {0},
159};
160
161static const struct rayer_pinout *pinout = NULL;
hailfinger52c4fa02010-07-21 10:26:01 +0000162
hailfinger08cf2ed2010-10-05 19:19:48 +0000163static void rayer_bitbang_set_cs(int val)
hailfinger52c4fa02010-07-21 10:26:01 +0000164{
Edward O'Callaghande191932019-09-09 00:50:34 +1000165 lpt_outbyte &= ~(1 << pinout->cs_bit);
166 lpt_outbyte |= (val << pinout->cs_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +0000167 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +0000168}
169
hailfinger08cf2ed2010-10-05 19:19:48 +0000170static void rayer_bitbang_set_sck(int val)
hailfinger52c4fa02010-07-21 10:26:01 +0000171{
Edward O'Callaghande191932019-09-09 00:50:34 +1000172 lpt_outbyte &= ~(1 << pinout->sck_bit);
173 lpt_outbyte |= (val << pinout->sck_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +0000174 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +0000175}
176
hailfinger08cf2ed2010-10-05 19:19:48 +0000177static void rayer_bitbang_set_mosi(int val)
hailfinger52c4fa02010-07-21 10:26:01 +0000178{
Edward O'Callaghande191932019-09-09 00:50:34 +1000179 lpt_outbyte &= ~(1 << pinout->mosi_bit);
180 lpt_outbyte |= (val << pinout->mosi_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +0000181 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +0000182}
183
hailfinger08cf2ed2010-10-05 19:19:48 +0000184static int rayer_bitbang_get_miso(void)
hailfinger52c4fa02010-07-21 10:26:01 +0000185{
186 uint8_t tmp;
187
Edward O'Callaghande191932019-09-09 00:50:34 +1000188 tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted
189 tmp = (tmp >> pinout->miso_bit) & 0x1;
hailfinger52c4fa02010-07-21 10:26:01 +0000190 return tmp;
191}
192
193static const struct bitbang_spi_master bitbang_spi_master_rayer = {
hailfinger52c4fa02010-07-21 10:26:01 +0000194 .set_cs = rayer_bitbang_set_cs,
195 .set_sck = rayer_bitbang_set_sck,
196 .set_mosi = rayer_bitbang_set_mosi,
197 .get_miso = rayer_bitbang_get_miso,
Patrick Georgie081d5d2017-03-22 21:18:18 +0100198 .half_period = 0,
hailfinger52c4fa02010-07-21 10:26:01 +0000199};
200
David Hendricksac1d25c2016-08-09 17:00:58 -0700201int rayer_spi_init(void)
hailfinger52c4fa02010-07-21 10:26:01 +0000202{
Edward O'Callaghande191932019-09-09 00:50:34 +1000203 const struct rayer_programmer *prog = rayer_spi_types;
hailfingerb9b9ccf2011-09-12 06:17:06 +0000204 char *arg = NULL;
hailfinger52c4fa02010-07-21 10:26:01 +0000205
hailfinger08cf2ed2010-10-05 19:19:48 +0000206 /* Non-default port requested? */
hailfingerb9b9ccf2011-09-12 06:17:06 +0000207 arg = extract_programmer_param("iobase");
208 if (arg) {
hailfinger08cf2ed2010-10-05 19:19:48 +0000209 char *endptr = NULL;
210 unsigned long tmp;
hailfingerb9b9ccf2011-09-12 06:17:06 +0000211 tmp = strtoul(arg, &endptr, 0);
hailfinger08cf2ed2010-10-05 19:19:48 +0000212 /* Port 0, port >0x10000, unaligned ports and garbage strings
213 * are rejected.
214 */
215 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
216 (*endptr != '\0')) {
217 /* Using ports below 0x100 is a really bad idea, and
218 * should only be done if no port between 0x100 and
219 * 0xfffc works due to routing issues.
220 */
221 msg_perr("Error: iobase= specified, but the I/O base "
222 "given was invalid.\nIt must be a multiple of "
223 "0x4 and lie between 0x100 and 0xfffc.\n");
hailfingerb9b9ccf2011-09-12 06:17:06 +0000224 free(arg);
hailfinger08cf2ed2010-10-05 19:19:48 +0000225 return 1;
226 } else {
227 lpt_iobase = (uint16_t)tmp;
228 msg_pinfo("Non-default I/O base requested. This will "
229 "not change the hardware settings.\n");
230 }
231 } else {
232 /* Pick a default value for the I/O base. */
233 lpt_iobase = 0x378;
234 }
hailfingerb9b9ccf2011-09-12 06:17:06 +0000235 free(arg);
Edward O'Callaghande191932019-09-09 00:50:34 +1000236
hailfinger08cf2ed2010-10-05 19:19:48 +0000237 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
hailfinger52c4fa02010-07-21 10:26:01 +0000238 lpt_iobase);
239
hailfingerb9b9ccf2011-09-12 06:17:06 +0000240 arg = extract_programmer_param("type");
241 if (arg) {
Edward O'Callaghande191932019-09-09 00:50:34 +1000242 for (; prog->type != NULL; prog++) {
243 if (strcasecmp(arg, prog->type) == 0) {
244 break;
245 }
246 }
247 if (prog->type == NULL) {
hailfingerb9b9ccf2011-09-12 06:17:06 +0000248 msg_perr("Error: Invalid device type specified.\n");
249 free(arg);
250 return 1;
251 }
Edward O'Callaghande191932019-09-09 00:50:34 +1000252 free(arg);
hailfingerb9b9ccf2011-09-12 06:17:06 +0000253 }
Edward O'Callaghande191932019-09-09 00:50:34 +1000254 msg_pinfo("Using %s pinout.\n", prog->description);
255 pinout = (struct rayer_pinout *)prog->dev_data;
hailfingerb9b9ccf2011-09-12 06:17:06 +0000256
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100257 if (rget_io_perms())
258 return 1;
hailfinger52c4fa02010-07-21 10:26:01 +0000259
hailfinger08cf2ed2010-10-05 19:19:48 +0000260 /* Get the initial value before writing to any line. */
261 lpt_outbyte = INB(lpt_iobase);
262
Edward O'Callaghande191932019-09-09 00:50:34 +1000263 if (pinout->shutdown)
264 register_shutdown(pinout->shutdown, (void*)pinout);
265 if (pinout->preinit)
266 pinout->preinit(pinout);
267
Craig Hesling65eb8812019-08-01 09:33:56 -0700268 if (register_spi_bitbang_master(&bitbang_spi_master_rayer))
hailfinger52c4fa02010-07-21 10:26:01 +0000269 return 1;
270
hailfinger52c4fa02010-07-21 10:26:01 +0000271 return 0;
272}
273
hailfinger52c4fa02010-07-21 10:26:01 +0000274#else
275#error PCI port I/O access is not supported on this architecture yet.
276#endif