blob: ed0f774dfd8b6f95899591a068bcc826da5ee042 [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
66static const struct rayer_pinout xilinx_dlc5 = {
67 .cs_bit = 2,
68 .sck_bit = 1,
69 .mosi_bit = 0,
70 .miso_bit = 4,
71};
72
73static const struct rayer_programmer rayer_spi_types[] = {
74 {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm},
75 {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5},
76 {0},
77};
78
79static const struct rayer_pinout *pinout = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000080
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000081static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000082
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000083/* Cached value of last byte sent. */
84static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000085
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000086static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000087{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000088 lpt_outbyte &= ~(1 << pinout->cs_bit);
89 lpt_outbyte |= (val << pinout->cs_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000090 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000091}
92
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000093static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000094{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +000095 lpt_outbyte &= ~(1 << pinout->sck_bit);
96 lpt_outbyte |= (val << pinout->sck_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000097 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000098}
99
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000100static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000101{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000102 lpt_outbyte &= ~(1 << pinout->mosi_bit);
103 lpt_outbyte |= (val << pinout->mosi_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000104 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000105}
106
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000107static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000108{
109 uint8_t tmp;
110
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000111 tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted
112 tmp = (tmp >> pinout->miso_bit) & 0x1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000113 return tmp;
114}
115
116static const struct bitbang_spi_master bitbang_spi_master_rayer = {
117 .type = BITBANG_SPI_MASTER_RAYER,
118 .set_cs = rayer_bitbang_set_cs,
119 .set_sck = rayer_bitbang_set_sck,
120 .set_mosi = rayer_bitbang_set_mosi,
121 .get_miso = rayer_bitbang_get_miso,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000122 .half_period = 0,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000123};
124
125int rayer_spi_init(void)
126{
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000127 const struct rayer_programmer *prog = rayer_spi_types;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000128 char *arg = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000129
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000130 /* Non-default port requested? */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000131 arg = extract_programmer_param("iobase");
132 if (arg) {
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000133 char *endptr = NULL;
134 unsigned long tmp;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000135 tmp = strtoul(arg, &endptr, 0);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000136 /* Port 0, port >0x10000, unaligned ports and garbage strings
137 * are rejected.
138 */
139 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
140 (*endptr != '\0')) {
141 /* Using ports below 0x100 is a really bad idea, and
142 * should only be done if no port between 0x100 and
143 * 0xfffc works due to routing issues.
144 */
145 msg_perr("Error: iobase= specified, but the I/O base "
146 "given was invalid.\nIt must be a multiple of "
147 "0x4 and lie between 0x100 and 0xfffc.\n");
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000148 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000149 return 1;
150 } else {
151 lpt_iobase = (uint16_t)tmp;
152 msg_pinfo("Non-default I/O base requested. This will "
153 "not change the hardware settings.\n");
154 }
155 } else {
156 /* Pick a default value for the I/O base. */
157 lpt_iobase = 0x378;
158 }
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000159 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000160
161 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000162 lpt_iobase);
163
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000164 arg = extract_programmer_param("type");
165 if (arg) {
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000166 for (; prog->type != NULL; prog++) {
167 if (strcasecmp(arg, prog->type) == 0) {
168 break;
169 }
170 }
171 if (prog->type == NULL) {
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000172 msg_perr("Error: Invalid device type specified.\n");
173 free(arg);
174 return 1;
175 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000176 free(arg);
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000177 }
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000178 msg_pinfo("Using %s pinout.\n", prog->description);
179 pinout = (struct rayer_pinout *)prog->dev_data;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000180
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000181 if (rget_io_perms())
182 return 1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000183
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000184 /* Get the initial value before writing to any line. */
185 lpt_outbyte = INB(lpt_iobase);
186
Kyösti Mälkki8b1bdf12013-10-02 01:21:45 +0000187 if (pinout->shutdown)
188 register_shutdown(pinout->shutdown, (void*)pinout);
189 if (pinout->preinit)
190 pinout->preinit(pinout);
191
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000192 if (bitbang_spi_init(&bitbang_spi_master_rayer))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000193 return 1;
194
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000195 return 0;
196}
197
198#else
199#error PCI port I/O access is not supported on this architecture yet.
200#endif