xref: /aosp_15_r20/external/flashrom/rayer_spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
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 
16 /* Driver for various LPT adapters.
17  *
18  * This driver uses non-portable direct I/O port accesses which won't work on
19  * 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 
26 #include <stdlib.h>
27 #include <strings.h>
28 #include <string.h>
29 #include "flash.h"
30 #include "programmer.h"
31 #include "hwaccess_x86_io.h"
32 
33 /* We have two sets of pins, out and in. The numbers for both sets are
34  * independent and are bitshift values, not real pin numbers.
35  * Default settings are for the RayeR hardware.
36  */
37 
38 struct rayer_programmer {
39 	const char *type;
40 	const enum test_state status;
41 	const char *description;
42 	const void *dev_data;
43 };
44 
45 struct rayer_pinout {
46 	uint8_t cs_bit;
47 	uint8_t sck_bit;
48 	uint8_t mosi_bit;
49 	uint8_t miso_bit;
50 	void (*preinit)(void *);
51 	int (*shutdown)(void *);
52 };
53 
54 struct rayer_spi_data {
55 	uint16_t lpt_iobase;
56 	/* Cached value of last byte sent. */
57 	uint8_t lpt_outbyte;
58 
59 	const struct rayer_pinout *pinout;
60 };
61 
62 static const struct rayer_pinout rayer_spipgm = {
63 	.cs_bit = 5,
64 	.sck_bit = 6,
65 	.mosi_bit = 7,
66 	.miso_bit = 6,
67 };
68 
dlc5_preinit(void * spi_data)69 static void dlc5_preinit(void *spi_data)
70 {
71 	struct rayer_spi_data *data = spi_data;
72 
73 	msg_pdbg("dlc5_preinit\n");
74 	/* Assert pin 6 to receive MISO. */
75 	data->lpt_outbyte |= (1<<4);
76 	OUTB(data->lpt_outbyte, data->lpt_iobase);
77 }
78 
dlc5_shutdown(void * spi_data)79 static int dlc5_shutdown(void *spi_data)
80 {
81 	struct rayer_spi_data *data = spi_data;
82 
83 	msg_pdbg("dlc5_shutdown\n");
84 	/* De-assert pin 6 to force MISO low. */
85 	data->lpt_outbyte &= ~(1<<4);
86 	OUTB(data->lpt_outbyte, data->lpt_iobase);
87 
88 	free(data);
89 	return 0;
90 }
91 
92 static const struct rayer_pinout xilinx_dlc5 = {
93 	.cs_bit = 2,
94 	.sck_bit = 1,
95 	.mosi_bit = 0,
96 	.miso_bit = 4,
97 	.preinit =  dlc5_preinit,
98 	.shutdown = dlc5_shutdown,
99 };
100 
byteblaster_preinit(void * spi_data)101 static void byteblaster_preinit(void *spi_data)
102 {
103 	struct rayer_spi_data *data = spi_data;
104 
105 	msg_pdbg("byteblaster_preinit\n");
106 	/* Assert #EN signal. */
107 	OUTB(2, data->lpt_iobase + 2 );
108 }
109 
byteblaster_shutdown(void * spi_data)110 static int byteblaster_shutdown(void *spi_data)
111 {
112 	struct rayer_spi_data *data = spi_data;
113 
114 	msg_pdbg("byteblaster_shutdown\n");
115 	/* De-Assert #EN signal. */
116 	OUTB(0, data->lpt_iobase + 2 );
117 
118 	free(data);
119 	return 0;
120 }
121 
122 static const struct rayer_pinout altera_byteblastermv = {
123 	.cs_bit = 1,
124 	.sck_bit = 0,
125 	.mosi_bit = 6,
126 	.miso_bit = 7,
127 	.preinit =  byteblaster_preinit,
128 	.shutdown = byteblaster_shutdown,
129 };
130 
stk200_preinit(void * spi_data)131 static void stk200_preinit(void *spi_data)
132 {
133 	struct rayer_spi_data *data = spi_data;
134 
135 	msg_pdbg("stk200_init\n");
136 	/* Assert #EN signals, set LED signal. */
137 	data->lpt_outbyte = (1 << 6) ;
138 	OUTB(data->lpt_outbyte, data->lpt_iobase);
139 }
140 
stk200_shutdown(void * spi_data)141 static int stk200_shutdown(void *spi_data)
142 {
143 	struct rayer_spi_data *data = spi_data;
144 
145 	msg_pdbg("stk200_shutdown\n");
146 	/* Assert #EN signals, clear LED signal. */
147 	data->lpt_outbyte = (1 << 2) | (1 << 3);
148 	OUTB(data->lpt_outbyte, data->lpt_iobase);
149 
150 	free(data);
151 	return 0;
152 }
153 
154 static const struct rayer_pinout atmel_stk200 = {
155 	.cs_bit = 7,
156 	.sck_bit = 4,
157 	.mosi_bit = 5,
158 	.miso_bit = 6,
159 	.preinit =  stk200_preinit,
160 	.shutdown = stk200_shutdown,
161 };
162 
163 static const struct rayer_pinout wiggler_lpt = {
164 	.cs_bit = 1,
165 	.sck_bit = 2,
166 	.mosi_bit = 3,
167 	.miso_bit = 7,
168 };
169 
170 static const struct rayer_pinout spi_tt = {
171 	.cs_bit = 2,
172 	.sck_bit = 0,
173 	.mosi_bit = 4,
174 	.miso_bit = 7,
175 };
176 
rayer_bitbang_set_cs(int val,void * spi_data)177 static void rayer_bitbang_set_cs(int val, void *spi_data)
178 {
179 	struct rayer_spi_data *data = spi_data;
180 
181 	data->lpt_outbyte &= ~(1 << data->pinout->cs_bit);
182 	data->lpt_outbyte |= (val << data->pinout->cs_bit);
183 	OUTB(data->lpt_outbyte, data->lpt_iobase);
184 }
185 
rayer_bitbang_set_sck(int val,void * spi_data)186 static void rayer_bitbang_set_sck(int val, void *spi_data)
187 {
188 	struct rayer_spi_data *data = spi_data;
189 
190 	data->lpt_outbyte &= ~(1 << data->pinout->sck_bit);
191 	data->lpt_outbyte |= (val << data->pinout->sck_bit);
192 	OUTB(data->lpt_outbyte, data->lpt_iobase);
193 }
194 
rayer_bitbang_set_mosi(int val,void * spi_data)195 static void rayer_bitbang_set_mosi(int val, void *spi_data)
196 {
197 	struct rayer_spi_data *data = spi_data;
198 
199 	data->lpt_outbyte &= ~(1 << data->pinout->mosi_bit);
200 	data->lpt_outbyte |= (val << data->pinout->mosi_bit);
201 	OUTB(data->lpt_outbyte, data->lpt_iobase);
202 }
203 
rayer_bitbang_get_miso(void * spi_data)204 static int rayer_bitbang_get_miso(void *spi_data)
205 {
206 	struct rayer_spi_data *data = spi_data;
207 	uint8_t tmp;
208 
209 	tmp = INB(data->lpt_iobase + 1) ^ 0x80; // bit.7 inverted
210 	tmp = (tmp >> data->pinout->miso_bit) & 0x1;
211 	return tmp;
212 }
213 
rayer_shutdown(void * spi_data)214 static int rayer_shutdown(void *spi_data)
215 {
216 	free(spi_data);
217 	return 0;
218 }
219 
220 static const struct bitbang_spi_master bitbang_spi_master_rayer = {
221 	.set_cs		= rayer_bitbang_set_cs,
222 	.set_sck	= rayer_bitbang_set_sck,
223 	.set_mosi	= rayer_bitbang_set_mosi,
224 	.get_miso	= rayer_bitbang_get_miso,
225 	.half_period	= 0,
226 };
227 
find_progtype(const char * prog_type)228 static const struct rayer_programmer *find_progtype(const char *prog_type)
229 {
230 	static const struct rayer_programmer rayer_spi_types[] = {
231 		{"rayer",		NT,	"RayeR SPIPGM",					&rayer_spipgm},
232 		{"xilinx",		NT,	"Xilinx Parallel Cable III (DLC 5)",		&xilinx_dlc5},
233 		{"byteblastermv",	OK,	"Altera ByteBlasterMV",				&altera_byteblastermv},
234 		{"stk200",		NT,	"Atmel STK200/300 adapter",			&atmel_stk200},
235 		{"wiggler",		OK,	"Wiggler LPT",					&wiggler_lpt},
236 		{"spi_tt",		NT,	"SPI Tiny Tools (SPI_TT LPT)",			&spi_tt},
237 		{0},
238 	};
239 	if (!prog_type)
240 		return &rayer_spi_types[0];
241 
242 	const struct rayer_programmer *prog = rayer_spi_types;
243 	for (; prog->type != NULL; prog++) {
244 		if (strcasecmp(prog_type, prog->type) == 0) {
245 			break;
246 		}
247 	}
248 
249 	if (!prog->type) {
250 		msg_perr("Error: Invalid device type specified.\n");
251 		return NULL;
252 	}
253 
254 	return prog;
255 }
256 
get_params(const struct programmer_cfg * cfg,uint16_t * lpt_iobase,const struct rayer_programmer ** prog)257 static int get_params(const struct programmer_cfg *cfg, uint16_t *lpt_iobase,
258 		      const struct rayer_programmer **prog)
259 {
260 	/* Pick a default value for the I/O base. */
261 	*lpt_iobase = 0x378;
262 	/* no programmer type specified. */
263 	*prog = NULL;
264 
265 	/* Non-default port requested? */
266 	char *arg = extract_programmer_param_str(cfg, "iobase");
267 	if (arg) {
268 		char *endptr = NULL;
269 		unsigned long tmp = strtoul(arg, &endptr, 0);
270 		/* Port 0, port >0x10000, unaligned ports and garbage strings
271 		 * are rejected.
272 		 */
273 		if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
274 		    (*endptr != '\0')) {
275 			/* Using ports below 0x100 is a really bad idea, and
276 			 * should only be done if no port between 0x100 and
277 			 * 0xfffc works due to routing issues.
278 			 */
279 			msg_perr("Error: iobase= specified, but the I/O base "
280 				 "given was invalid.\nIt must be a multiple of "
281 				 "0x4 and lie between 0x100 and 0xfffc.\n");
282 			free(arg);
283 			return -1;
284 		} else {
285 			*lpt_iobase = (uint16_t)tmp;
286 			msg_pinfo("Non-default I/O base requested. This will "
287 				  "not change the hardware settings.\n");
288 		}
289 		free(arg);
290 	}
291 
292 	arg = extract_programmer_param_str(cfg, "type");
293 	*prog = find_progtype(arg);
294 	free(arg);
295 
296 	return *prog ? 0 : -1;
297 }
298 
rayer_spi_init(const struct programmer_cfg * cfg)299 static int rayer_spi_init(const struct programmer_cfg *cfg)
300 {
301 	const struct rayer_programmer *prog;
302 	struct rayer_pinout *pinout = NULL;
303 	uint16_t lpt_iobase;
304 
305 	if (get_params(cfg, &lpt_iobase, &prog) < 0)
306 		return 1;
307 
308 	msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
309 		 lpt_iobase);
310 
311 	msg_pinfo("Using %s pinout.\n", prog->description);
312 	pinout = (struct rayer_pinout *)prog->dev_data;
313 
314 	if (rget_io_perms())
315 		return 1;
316 
317 	struct rayer_spi_data *data = calloc(1, sizeof(*data));
318 	if (!data) {
319 		msg_perr("Unable to allocate space for SPI master data\n");
320 		return 1;
321 	}
322 	data->pinout = pinout;
323 	data->lpt_iobase = lpt_iobase;
324 	/* Get the initial value before writing to any line. */
325 	data->lpt_outbyte = INB(lpt_iobase);
326 
327 	if (pinout->shutdown)
328 		register_shutdown(pinout->shutdown, data);
329 	else
330 		register_shutdown(rayer_shutdown, data);
331 
332 	if (pinout->preinit)
333 		pinout->preinit(data);
334 
335 	if (register_spi_bitbang_master(&bitbang_spi_master_rayer, data))
336 		return 1;
337 
338 	return 0;
339 }
340 
341 const struct programmer_entry programmer_rayer_spi = {
342 	.name			= "rayer_spi",
343 	.type			= OTHER,
344 				/* FIXME */
345 	.devs.note		= "RayeR parallel port programmer\n",
346 	.init			= rayer_spi_init,
347 };
348