blob: 985e1f58cbf8a05fde55abe05aaf21366ce98ef2 [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.
14 *
hailfinger52c4fa02010-07-21 10:26:01 +000015 */
16
17/* Driver for the SPIPGM hardware by "RayeR" Martin Rehak.
18 * See http://rayer.ic.cz/elektro/spipgm.htm for schematics and instructions.
19 */
20
21/* This driver uses non-portable direct I/O port accesses which won't work on
22 * any non-x86 platform, and even on x86 there is a high chance there will be
23 * collisions with any loaded parallel port drivers.
24 * The big advantage of direct port I/O is OS independence and speed because
25 * most OS parport drivers will perform many unnecessary accesses although
26 * this driver just treats the parallel port as a GPIO set.
27 */
28#if defined(__i386__) || defined(__x86_64__)
29
hailfinger08cf2ed2010-10-05 19:19:48 +000030#include <stdlib.h>
hailfingerb9b9ccf2011-09-12 06:17:06 +000031#include <string.h>
hailfinger52c4fa02010-07-21 10:26:01 +000032#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000033#include "programmer.h"
hailfinger52c4fa02010-07-21 10:26:01 +000034
hailfingerb9b9ccf2011-09-12 06:17:06 +000035enum rayer_type {
36 TYPE_RAYER,
37 TYPE_XILINX_DLC5,
38};
39
hailfinger52c4fa02010-07-21 10:26:01 +000040/* We have two sets of pins, out and in. The numbers for both sets are
41 * independent and are bitshift values, not real pin numbers.
stefanct98d917c2011-10-21 12:33:07 +000042 * Default settings are for the RayeR hardware.
hailfinger52c4fa02010-07-21 10:26:01 +000043 */
44/* Pins for master->slave direction */
hailfingerb9b9ccf2011-09-12 06:17:06 +000045static int rayer_cs_bit = 5;
46static int rayer_sck_bit = 6;
47static int rayer_mosi_bit = 7;
hailfinger52c4fa02010-07-21 10:26:01 +000048/* Pins for slave->master direction */
hailfingerb9b9ccf2011-09-12 06:17:06 +000049static int rayer_miso_bit = 6;
hailfinger52c4fa02010-07-21 10:26:01 +000050
hailfinger08cf2ed2010-10-05 19:19:48 +000051static uint16_t lpt_iobase;
hailfinger52c4fa02010-07-21 10:26:01 +000052
hailfinger08cf2ed2010-10-05 19:19:48 +000053/* Cached value of last byte sent. */
54static uint8_t lpt_outbyte;
hailfinger52c4fa02010-07-21 10:26:01 +000055
hailfinger08cf2ed2010-10-05 19:19:48 +000056static void rayer_bitbang_set_cs(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000057{
hailfingerb9b9ccf2011-09-12 06:17:06 +000058 lpt_outbyte &= ~(1 << rayer_cs_bit);
59 lpt_outbyte |= (val << rayer_cs_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000060 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000061}
62
hailfinger08cf2ed2010-10-05 19:19:48 +000063static void rayer_bitbang_set_sck(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000064{
hailfingerb9b9ccf2011-09-12 06:17:06 +000065 lpt_outbyte &= ~(1 << rayer_sck_bit);
66 lpt_outbyte |= (val << rayer_sck_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000067 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000068}
69
hailfinger08cf2ed2010-10-05 19:19:48 +000070static void rayer_bitbang_set_mosi(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000071{
hailfingerb9b9ccf2011-09-12 06:17:06 +000072 lpt_outbyte &= ~(1 << rayer_mosi_bit);
73 lpt_outbyte |= (val << rayer_mosi_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000074 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000075}
76
hailfinger08cf2ed2010-10-05 19:19:48 +000077static int rayer_bitbang_get_miso(void)
hailfinger52c4fa02010-07-21 10:26:01 +000078{
79 uint8_t tmp;
80
81 tmp = INB(lpt_iobase + 1);
hailfingerb9b9ccf2011-09-12 06:17:06 +000082 tmp = (tmp >> rayer_miso_bit) & 0x1;
hailfinger52c4fa02010-07-21 10:26:01 +000083 return tmp;
84}
85
86static const struct bitbang_spi_master bitbang_spi_master_rayer = {
87 .type = BITBANG_SPI_MASTER_RAYER,
88 .set_cs = rayer_bitbang_set_cs,
89 .set_sck = rayer_bitbang_set_sck,
90 .set_mosi = rayer_bitbang_set_mosi,
91 .get_miso = rayer_bitbang_get_miso,
Patrick Georgie081d5d2017-03-22 21:18:18 +010092 .half_period = 0,
hailfinger52c4fa02010-07-21 10:26:01 +000093};
94
David Hendricksac1d25c2016-08-09 17:00:58 -070095int rayer_spi_init(void)
hailfinger52c4fa02010-07-21 10:26:01 +000096{
hailfingerb9b9ccf2011-09-12 06:17:06 +000097 char *arg = NULL;
98 enum rayer_type rayer_type = TYPE_RAYER;
hailfinger52c4fa02010-07-21 10:26:01 +000099
hailfinger08cf2ed2010-10-05 19:19:48 +0000100 /* Non-default port requested? */
hailfingerb9b9ccf2011-09-12 06:17:06 +0000101 arg = extract_programmer_param("iobase");
102 if (arg) {
hailfinger08cf2ed2010-10-05 19:19:48 +0000103 char *endptr = NULL;
104 unsigned long tmp;
hailfingerb9b9ccf2011-09-12 06:17:06 +0000105 tmp = strtoul(arg, &endptr, 0);
hailfinger08cf2ed2010-10-05 19:19:48 +0000106 /* Port 0, port >0x10000, unaligned ports and garbage strings
107 * are rejected.
108 */
109 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
110 (*endptr != '\0')) {
111 /* Using ports below 0x100 is a really bad idea, and
112 * should only be done if no port between 0x100 and
113 * 0xfffc works due to routing issues.
114 */
115 msg_perr("Error: iobase= specified, but the I/O base "
116 "given was invalid.\nIt must be a multiple of "
117 "0x4 and lie between 0x100 and 0xfffc.\n");
hailfingerb9b9ccf2011-09-12 06:17:06 +0000118 free(arg);
hailfinger08cf2ed2010-10-05 19:19:48 +0000119 return 1;
120 } else {
121 lpt_iobase = (uint16_t)tmp;
122 msg_pinfo("Non-default I/O base requested. This will "
123 "not change the hardware settings.\n");
124 }
125 } else {
126 /* Pick a default value for the I/O base. */
127 lpt_iobase = 0x378;
128 }
hailfingerb9b9ccf2011-09-12 06:17:06 +0000129 free(arg);
hailfinger08cf2ed2010-10-05 19:19:48 +0000130
131 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
hailfinger52c4fa02010-07-21 10:26:01 +0000132 lpt_iobase);
133
hailfingerb9b9ccf2011-09-12 06:17:06 +0000134 arg = extract_programmer_param("type");
135 if (arg) {
136 if (!strcasecmp(arg, "rayer")) {
137 rayer_type = TYPE_RAYER;
138 } else if (!strcasecmp(arg, "xilinx")) {
139 rayer_type = TYPE_XILINX_DLC5;
140 } else {
141 msg_perr("Error: Invalid device type specified.\n");
142 free(arg);
143 return 1;
144 }
145 }
146 free(arg);
147 switch (rayer_type) {
148 case TYPE_RAYER:
149 msg_pdbg("Using RayeR SPIPGM pinout.\n");
150 /* Bits for master->slave direction */
151 rayer_cs_bit = 5;
152 rayer_sck_bit = 6;
153 rayer_mosi_bit = 7;
154 /* Bits for slave->master direction */
155 rayer_miso_bit = 6;
156 break;
157 case TYPE_XILINX_DLC5:
158 msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n");
159 /* Bits for master->slave direction */
160 rayer_cs_bit = 2;
161 rayer_sck_bit = 1;
162 rayer_mosi_bit = 0;
163 /* Bits for slave->master direction */
164 rayer_miso_bit = 4;
165 }
166
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100167 if (rget_io_perms())
168 return 1;
hailfinger52c4fa02010-07-21 10:26:01 +0000169
hailfinger08cf2ed2010-10-05 19:19:48 +0000170 /* Get the initial value before writing to any line. */
171 lpt_outbyte = INB(lpt_iobase);
172
Edward O'Callaghanbcae3752018-12-19 13:11:57 +1100173 if (bitbang_spi_init(&bitbang_spi_master_rayer))
hailfinger52c4fa02010-07-21 10:26:01 +0000174 return 1;
175
hailfinger52c4fa02010-07-21 10:26:01 +0000176 return 0;
177}
178
179#else
180#error PCI port I/O access is not supported on this architecture yet.
181#endif