aboutsummaryrefslogtreecommitdiffstats
path: root/rayer_spi.c
blob: 3d626b2ec51c7778174c2d6083be6835e972ac6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/* Driver for various LPT adapters.
 *
 * This driver uses non-portable direct I/O port accesses which won't work on
 * any non-x86 platform, and even on x86 there is a high chance there will be
 * collisions with any loaded parallel port drivers.
 * The big advantage of direct port I/O is OS independence and speed because
 * most OS parport drivers will perform many unnecessary accesses although
 * this driver just treats the parallel port as a GPIO set.
 */

#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "flash.h"
#include "programmer.h"
#include "hwaccess_x86_io.h"

/* We have two sets of pins, out and in. The numbers for both sets are
 * independent and are bitshift values, not real pin numbers.
 * Default settings are for the RayeR hardware.
 */

struct rayer_programmer {
	const char *type;
	const enum test_state status;
	const char *description;
	const void *dev_data;
};

struct rayer_pinout {
	uint8_t cs_bit;
	uint8_t sck_bit;
	uint8_t mosi_bit;
	uint8_t miso_bit;
	void (*preinit)(void *);
	int (*shutdown)(void *);
};

struct rayer_spi_data {
	uint16_t lpt_iobase;
	/* Cached value of last byte sent. */
	uint8_t lpt_outbyte;

	const struct rayer_pinout *pinout;
};

static const struct rayer_pinout rayer_spipgm = {
	.cs_bit = 5,
	.sck_bit = 6,
	.mosi_bit = 7,
	.miso_bit = 6,
};

static void dlc5_preinit(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("dlc5_preinit\n");
	/* Assert pin 6 to receive MISO. */
	data->lpt_outbyte |= (1<<4);
	OUTB(data->lpt_outbyte, data->lpt_iobase);
}

static int dlc5_shutdown(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("dlc5_shutdown\n");
	/* De-assert pin 6 to force MISO low. */
	data->lpt_outbyte &= ~(1<<4);
	OUTB(data->lpt_outbyte, data->lpt_iobase);

	free(data);
	return 0;
}

static const struct rayer_pinout xilinx_dlc5 = {
	.cs_bit = 2,
	.sck_bit = 1,
	.mosi_bit = 0,
	.miso_bit = 4,
	.preinit =  dlc5_preinit,
	.shutdown = dlc5_shutdown,
};

static void byteblaster_preinit(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("byteblaster_preinit\n");
	/* Assert #EN signal. */
	OUTB(2, data->lpt_iobase + 2 );
}

static int byteblaster_shutdown(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("byteblaster_shutdown\n");
	/* De-Assert #EN signal. */
	OUTB(0, data->lpt_iobase + 2 );

	free(data);
	return 0;
}

static const struct rayer_pinout altera_byteblastermv = {
	.cs_bit = 1,
	.sck_bit = 0,
	.mosi_bit = 6,
	.miso_bit = 7,
	.preinit =  byteblaster_preinit,
	.shutdown = byteblaster_shutdown,
};

static void stk200_preinit(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("stk200_init\n");
	/* Assert #EN signals, set LED signal. */
	data->lpt_outbyte = (1 << 6) ;
	OUTB(data->lpt_outbyte, data->lpt_iobase);
}

static int stk200_shutdown(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	msg_pdbg("stk200_shutdown\n");
	/* Assert #EN signals, clear LED signal. */
	data->lpt_outbyte = (1 << 2) | (1 << 3);
	OUTB(data->lpt_outbyte, data->lpt_iobase);

	free(data);
	return 0;
}

static const struct rayer_pinout atmel_stk200 = {
	.cs_bit = 7,
	.sck_bit = 4,
	.mosi_bit = 5,
	.miso_bit = 6,
	.preinit =  stk200_preinit,
	.shutdown = stk200_shutdown,
};

static const struct rayer_pinout wiggler_lpt = {
	.cs_bit = 1,
	.sck_bit = 2,
	.mosi_bit = 3,
	.miso_bit = 7,
};

static const struct rayer_pinout spi_tt = {
	.cs_bit = 2,
	.sck_bit = 0,
	.mosi_bit = 4,
	.miso_bit = 7,
};

static const struct rayer_programmer rayer_spi_types[] = {
	{"rayer",		NT,	"RayeR SPIPGM",					&rayer_spipgm},
	{"xilinx",		NT,	"Xilinx Parallel Cable III (DLC 5)",		&xilinx_dlc5},
	{"byteblastermv",	OK,	"Altera ByteBlasterMV",				&altera_byteblastermv},
	{"stk200",		NT,	"Atmel STK200/300 adapter",			&atmel_stk200},
	{"wiggler",		OK,	"Wiggler LPT",					&wiggler_lpt},
	{"spi_tt",		NT,	"SPI Tiny Tools (SPI_TT LPT)",			&spi_tt},
	{0},
};

static void rayer_bitbang_set_cs(int val, void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	data->lpt_outbyte &= ~(1 << data->pinout->cs_bit);
	data->lpt_outbyte |= (val << data->pinout->cs_bit);
	OUTB(data->lpt_outbyte, data->lpt_iobase);
}

static void rayer_bitbang_set_sck(int val, void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	data->lpt_outbyte &= ~(1 << data->pinout->sck_bit);
	data->lpt_outbyte |= (val << data->pinout->sck_bit);
	OUTB(data->lpt_outbyte, data->lpt_iobase);
}

static void rayer_bitbang_set_mosi(int val, void *spi_data)
{
	struct rayer_spi_data *data = spi_data;

	data->lpt_outbyte &= ~(1 << data->pinout->mosi_bit);
	data->lpt_outbyte |= (val << data->pinout->mosi_bit);
	OUTB(data->lpt_outbyte, data->lpt_iobase);
}

static int rayer_bitbang_get_miso(void *spi_data)
{
	struct rayer_spi_data *data = spi_data;
	uint8_t tmp;

	tmp = INB(data->lpt_iobase + 1) ^ 0x80; // bit.7 inverted
	tmp = (tmp >> data->pinout->miso_bit) & 0x1;
	return tmp;
}

static int rayer_shutdown(void *spi_data)
{
	free(spi_data);
	return 0;
}

static const struct bitbang_spi_master bitbang_spi_master_rayer = {
	.set_cs		= rayer_bitbang_set_cs,
	.set_sck	= rayer_bitbang_set_sck,
	.set_mosi	= rayer_bitbang_set_mosi,
	.get_miso	= rayer_bitbang_get_miso,
	.half_period	= 0,
};

static int rayer_spi_init(void)
{
	const struct rayer_programmer *prog = rayer_spi_types;
	char *arg = NULL;
	struct rayer_pinout *pinout = NULL;
	uint16_t lpt_iobase;
	uint8_t lpt_outbyte;

	/* Non-default port requested? */
	arg = extract_programmer_param_str("iobase");
	if (arg) {
		char *endptr = NULL;
		unsigned long tmp;
		tmp = strtoul(arg, &endptr, 0);
		/* Port 0, port >0x10000, unaligned ports and garbage strings
		 * are rejected.
		 */
		if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
		    (*endptr != '\0')) {
			/* Using ports below 0x100 is a really bad idea, and
			 * should only be done if no port between 0x100 and
			 * 0xfffc works due to routing issues.
			 */
			msg_perr("Error: iobase= specified, but the I/O base "
				 "given was invalid.\nIt must be a multiple of "
				 "0x4 and lie between 0x100 and 0xfffc.\n");
			free(arg);
			return 1;
		} else {
			lpt_iobase = (uint16_t)tmp;
			msg_pinfo("Non-default I/O base requested. This will "
				  "not change the hardware settings.\n");
		}
	} else {
		/* Pick a default value for the I/O base. */
		lpt_iobase = 0x378;
	}
	free(arg);

	msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
		 lpt_iobase);

	arg = extract_programmer_param_str("type");
	if (arg) {
		for (; prog->type != NULL; prog++) {
			if (strcasecmp(arg, prog->type) == 0) {
				break;
			}
		}
		if (prog->type == NULL) {
			msg_perr("Error: Invalid device type specified.\n");
			free(arg);
			return 1;
		}
		free(arg);
	}
	msg_pinfo("Using %s pinout.\n", prog->description);
	pinout = (struct rayer_pinout *)prog->dev_data;

	if (rget_io_perms())
		return 1;

	/* Get the initial value before writing to any line. */
	lpt_outbyte = INB(lpt_iobase);

	struct rayer_spi_data *data = calloc(1, sizeof(*data));
	if (!data) {
		msg_perr("Unable to allocate space for SPI master data\n");
		return 1;
	}
	data->pinout = pinout;
	data->lpt_iobase = lpt_iobase;
	data->lpt_outbyte = lpt_outbyte;

	if (pinout->shutdown)
		register_shutdown(pinout->shutdown, data);
	else
		register_shutdown(rayer_shutdown, data);

	if (pinout->preinit)
		pinout->preinit(data);

	if (register_spi_bitbang_master(&bitbang_spi_master_rayer, data))
		return 1;

	return 0;
}

const struct programmer_entry programmer_rayer_spi = {
	.name			= "rayer_spi",
	.type			= OTHER,
				/* FIXME */
	.devs.note		= "RayeR parallel port programmer\n",
	.init			= rayer_spi_init,
	.map_flash_region	= fallback_map,
	.unmap_flash_region	= fallback_unmap,
	.delay			= internal_delay,
};