blob: 4f8e1d6d236928586955e27937764814c69185e4 [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.
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000014 */
15
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000016/* Driver for various LPT adapters.
17 *
18 * This driver uses non-portable direct I/O port accesses which won't work on
Carl-Daniel Hailfingere7fdd6e2010-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
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000027#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000028#include <strings.h>
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000029#include <string.h>
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000030#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000031#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000032#include "hwaccess.h"
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000033
34/* We have two sets of pins, out and in. The numbers for both sets are
35 * independent and are bitshift values, not real pin numbers.
Paul Menzel018d4822011-10-21 12:33:07 +000036 * Default settings are for the RayeR hardware.
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000037 */
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000038
39struct rayer_programmer {
40 const char *type;
41 const enum test_state status;
42 const char *description;
43 const void *dev_data;
44};
45
46struct rayer_pinout {
47 uint8_t cs_bit;
48 uint8_t sck_bit;
49 uint8_t mosi_bit;
50 uint8_t miso_bit;
51 void (*preinit)(const void *);
52 int (*shutdown)(void *);
53};
54
55static const struct rayer_pinout rayer_spipgm = {
56 .cs_bit = 5,
57 .sck_bit = 6,
58 .mosi_bit = 7,
59 .miso_bit = 6,
60};
61
Kyösti Mälkki1d473792013-10-02 01:22:17 +000062static void dlc5_preinit(const void *);
63static int dlc5_shutdown(void *);
64
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000065static const struct rayer_pinout xilinx_dlc5 = {
66 .cs_bit = 2,
67 .sck_bit = 1,
68 .mosi_bit = 0,
69 .miso_bit = 4,
Kyösti Mälkki1d473792013-10-02 01:22:17 +000070 .preinit = dlc5_preinit,
71 .shutdown = dlc5_shutdown,
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000072};
73
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +000074static void byteblaster_preinit(const void *);
75static int byteblaster_shutdown(void *);
76
77static const struct rayer_pinout altera_byteblastermv = {
78 .cs_bit = 1,
79 .sck_bit = 0,
80 .mosi_bit = 6,
81 .miso_bit = 7,
82 .preinit = byteblaster_preinit,
83 .shutdown = byteblaster_shutdown,
84};
85
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +000086static void stk200_preinit(const void *);
87static int stk200_shutdown(void *);
88
89static const struct rayer_pinout atmel_stk200 = {
90 .cs_bit = 7,
91 .sck_bit = 4,
92 .mosi_bit = 5,
93 .miso_bit = 6,
94 .preinit = stk200_preinit,
95 .shutdown = stk200_shutdown,
96};
97
Maksim Kuleshovacba2ac2013-10-02 01:22:11 +000098static const struct rayer_pinout wiggler_lpt = {
99 .cs_bit = 1,
100 .sck_bit = 2,
101 .mosi_bit = 3,
102 .miso_bit = 7,
103};
104
Stefan Taunerfdb16592016-02-28 17:04:38 +0000105static const struct rayer_pinout spi_tt = {
106 .cs_bit = 2,
107 .sck_bit = 0,
108 .mosi_bit = 4,
109 .miso_bit = 7,
110};
111
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000112static const struct rayer_programmer rayer_spi_types[] = {
113 {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm},
114 {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5},
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +0000115 {"byteblastermv", OK, "Altera ByteBlasterMV", &altera_byteblastermv},
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +0000116 {"stk200", NT, "Atmel STK200/300 adapter", &atmel_stk200},
Maksim Kuleshovacba2ac2013-10-02 01:22:11 +0000117 {"wiggler", OK, "Wiggler LPT", &wiggler_lpt},
Stefan Taunerfdb16592016-02-28 17:04:38 +0000118 {"spi_tt", NT, "SPI Tiny Tools (SPI_TT LPT)", &spi_tt},
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000119 {0},
120};
121
122static const struct rayer_pinout *pinout = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000123
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000124static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000125
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000126/* Cached value of last byte sent. */
127static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000128
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000129static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000130{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000131 lpt_outbyte &= ~(1 << pinout->cs_bit);
132 lpt_outbyte |= (val << pinout->cs_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000133 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000134}
135
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000136static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000137{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000138 lpt_outbyte &= ~(1 << pinout->sck_bit);
139 lpt_outbyte |= (val << pinout->sck_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000140 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000141}
142
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000143static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000144{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000145 lpt_outbyte &= ~(1 << pinout->mosi_bit);
146 lpt_outbyte |= (val << pinout->mosi_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000147 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000148}
149
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000150static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000151{
152 uint8_t tmp;
153
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000154 tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted
155 tmp = (tmp >> pinout->miso_bit) & 0x1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000156 return tmp;
157}
158
159static const struct bitbang_spi_master bitbang_spi_master_rayer = {
160 .type = BITBANG_SPI_MASTER_RAYER,
161 .set_cs = rayer_bitbang_set_cs,
162 .set_sck = rayer_bitbang_set_sck,
163 .set_mosi = rayer_bitbang_set_mosi,
164 .get_miso = rayer_bitbang_get_miso,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000165 .half_period = 0,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000166};
167
168int rayer_spi_init(void)
169{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000170 const struct rayer_programmer *prog = rayer_spi_types;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000171 char *arg = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000172
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000173 /* Non-default port requested? */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000174 arg = extract_programmer_param("iobase");
175 if (arg) {
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000176 char *endptr = NULL;
177 unsigned long tmp;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000178 tmp = strtoul(arg, &endptr, 0);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000179 /* Port 0, port >0x10000, unaligned ports and garbage strings
180 * are rejected.
181 */
182 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
183 (*endptr != '\0')) {
184 /* Using ports below 0x100 is a really bad idea, and
185 * should only be done if no port between 0x100 and
186 * 0xfffc works due to routing issues.
187 */
188 msg_perr("Error: iobase= specified, but the I/O base "
189 "given was invalid.\nIt must be a multiple of "
190 "0x4 and lie between 0x100 and 0xfffc.\n");
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000191 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000192 return 1;
193 } else {
194 lpt_iobase = (uint16_t)tmp;
195 msg_pinfo("Non-default I/O base requested. This will "
196 "not change the hardware settings.\n");
197 }
198 } else {
199 /* Pick a default value for the I/O base. */
200 lpt_iobase = 0x378;
201 }
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000202 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000203
204 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000205 lpt_iobase);
206
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000207 arg = extract_programmer_param("type");
208 if (arg) {
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000209 for (; prog->type != NULL; prog++) {
210 if (strcasecmp(arg, prog->type) == 0) {
211 break;
212 }
213 }
214 if (prog->type == NULL) {
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000215 msg_perr("Error: Invalid device type specified.\n");
216 free(arg);
217 return 1;
218 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000219 free(arg);
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000220 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000221 msg_pinfo("Using %s pinout.\n", prog->description);
222 pinout = (struct rayer_pinout *)prog->dev_data;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000223
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000224 if (rget_io_perms())
225 return 1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000226
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000227 /* Get the initial value before writing to any line. */
228 lpt_outbyte = INB(lpt_iobase);
229
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000230 if (pinout->shutdown)
231 register_shutdown(pinout->shutdown, (void*)pinout);
232 if (pinout->preinit)
233 pinout->preinit(pinout);
234
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000235 if (register_spi_bitbang_master(&bitbang_spi_master_rayer))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000236 return 1;
237
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000238 return 0;
239}
240
Maksim Kuleshov3647b2d2013-10-02 01:21:57 +0000241static void byteblaster_preinit(const void *data){
242 msg_pdbg("byteblaster_preinit\n");
243 /* Assert #EN signal. */
244 OUTB(2, lpt_iobase + 2 );
245}
246
247static int byteblaster_shutdown(void *data){
248 msg_pdbg("byteblaster_shutdown\n");
249 /* De-Assert #EN signal. */
250 OUTB(0, lpt_iobase + 2 );
251 return 0;
252}
253
Maksim Kuleshov4dab5c12013-10-02 01:22:02 +0000254static void stk200_preinit(const void *data) {
255 msg_pdbg("stk200_init\n");
256 /* Assert #EN signals, set LED signal. */
257 lpt_outbyte = (1 << 6) ;
258 OUTB(lpt_outbyte, lpt_iobase);
259}
260
261static int stk200_shutdown(void *data) {
262 msg_pdbg("stk200_shutdown\n");
263 /* Assert #EN signals, clear LED signal. */
264 lpt_outbyte = (1 << 2) | (1 << 3);
265 OUTB(lpt_outbyte, lpt_iobase);
266 return 0;
267}
268
Kyösti Mälkki1d473792013-10-02 01:22:17 +0000269static void dlc5_preinit(const void *data) {
270 msg_pdbg("dlc5_preinit\n");
271 /* Assert pin 6 to receive MISO. */
272 lpt_outbyte |= (1<<4);
273 OUTB(lpt_outbyte, lpt_iobase);
274}
275
276static int dlc5_shutdown(void *data) {
277 msg_pdbg("dlc5_shutdown\n");
278 /* De-assert pin 6 to force MISO low. */
279 lpt_outbyte &= ~(1<<4);
280 OUTB(lpt_outbyte, lpt_iobase);
281 return 0;
282}
283
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000284#else
285#error PCI port I/O access is not supported on this architecture yet.
286#endif