blob: 3c805493c002fd3be772f651128893006c3ee08f [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"
Mayur Panchalf4796862019-08-05 15:46:12 +100034#include "hwaccess.h"
hailfinger52c4fa02010-07-21 10:26:01 +000035
hailfingerb9b9ccf2011-09-12 06:17:06 +000036enum rayer_type {
37 TYPE_RAYER,
38 TYPE_XILINX_DLC5,
39};
40
hailfinger52c4fa02010-07-21 10:26:01 +000041/* We have two sets of pins, out and in. The numbers for both sets are
42 * independent and are bitshift values, not real pin numbers.
stefanct98d917c2011-10-21 12:33:07 +000043 * Default settings are for the RayeR hardware.
hailfinger52c4fa02010-07-21 10:26:01 +000044 */
45/* Pins for master->slave direction */
hailfingerb9b9ccf2011-09-12 06:17:06 +000046static int rayer_cs_bit = 5;
47static int rayer_sck_bit = 6;
48static int rayer_mosi_bit = 7;
hailfinger52c4fa02010-07-21 10:26:01 +000049/* Pins for slave->master direction */
hailfingerb9b9ccf2011-09-12 06:17:06 +000050static int rayer_miso_bit = 6;
hailfinger52c4fa02010-07-21 10:26:01 +000051
hailfinger08cf2ed2010-10-05 19:19:48 +000052static uint16_t lpt_iobase;
hailfinger52c4fa02010-07-21 10:26:01 +000053
hailfinger08cf2ed2010-10-05 19:19:48 +000054/* Cached value of last byte sent. */
55static uint8_t lpt_outbyte;
hailfinger52c4fa02010-07-21 10:26:01 +000056
hailfinger08cf2ed2010-10-05 19:19:48 +000057static void rayer_bitbang_set_cs(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000058{
hailfingerb9b9ccf2011-09-12 06:17:06 +000059 lpt_outbyte &= ~(1 << rayer_cs_bit);
60 lpt_outbyte |= (val << rayer_cs_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000061 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000062}
63
hailfinger08cf2ed2010-10-05 19:19:48 +000064static void rayer_bitbang_set_sck(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000065{
hailfingerb9b9ccf2011-09-12 06:17:06 +000066 lpt_outbyte &= ~(1 << rayer_sck_bit);
67 lpt_outbyte |= (val << rayer_sck_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000068 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000069}
70
hailfinger08cf2ed2010-10-05 19:19:48 +000071static void rayer_bitbang_set_mosi(int val)
hailfinger52c4fa02010-07-21 10:26:01 +000072{
hailfingerb9b9ccf2011-09-12 06:17:06 +000073 lpt_outbyte &= ~(1 << rayer_mosi_bit);
74 lpt_outbyte |= (val << rayer_mosi_bit);
hailfinger08cf2ed2010-10-05 19:19:48 +000075 OUTB(lpt_outbyte, lpt_iobase);
hailfinger52c4fa02010-07-21 10:26:01 +000076}
77
hailfinger08cf2ed2010-10-05 19:19:48 +000078static int rayer_bitbang_get_miso(void)
hailfinger52c4fa02010-07-21 10:26:01 +000079{
80 uint8_t tmp;
81
82 tmp = INB(lpt_iobase + 1);
hailfingerb9b9ccf2011-09-12 06:17:06 +000083 tmp = (tmp >> rayer_miso_bit) & 0x1;
hailfinger52c4fa02010-07-21 10:26:01 +000084 return tmp;
85}
86
87static const struct bitbang_spi_master bitbang_spi_master_rayer = {
88 .type = BITBANG_SPI_MASTER_RAYER,
89 .set_cs = rayer_bitbang_set_cs,
90 .set_sck = rayer_bitbang_set_sck,
91 .set_mosi = rayer_bitbang_set_mosi,
92 .get_miso = rayer_bitbang_get_miso,
Patrick Georgie081d5d2017-03-22 21:18:18 +010093 .half_period = 0,
hailfinger52c4fa02010-07-21 10:26:01 +000094};
95
David Hendricksac1d25c2016-08-09 17:00:58 -070096int rayer_spi_init(void)
hailfinger52c4fa02010-07-21 10:26:01 +000097{
hailfingerb9b9ccf2011-09-12 06:17:06 +000098 char *arg = NULL;
99 enum rayer_type rayer_type = TYPE_RAYER;
hailfinger52c4fa02010-07-21 10:26:01 +0000100
hailfinger08cf2ed2010-10-05 19:19:48 +0000101 /* Non-default port requested? */
hailfingerb9b9ccf2011-09-12 06:17:06 +0000102 arg = extract_programmer_param("iobase");
103 if (arg) {
hailfinger08cf2ed2010-10-05 19:19:48 +0000104 char *endptr = NULL;
105 unsigned long tmp;
hailfingerb9b9ccf2011-09-12 06:17:06 +0000106 tmp = strtoul(arg, &endptr, 0);
hailfinger08cf2ed2010-10-05 19:19:48 +0000107 /* Port 0, port >0x10000, unaligned ports and garbage strings
108 * are rejected.
109 */
110 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
111 (*endptr != '\0')) {
112 /* Using ports below 0x100 is a really bad idea, and
113 * should only be done if no port between 0x100 and
114 * 0xfffc works due to routing issues.
115 */
116 msg_perr("Error: iobase= specified, but the I/O base "
117 "given was invalid.\nIt must be a multiple of "
118 "0x4 and lie between 0x100 and 0xfffc.\n");
hailfingerb9b9ccf2011-09-12 06:17:06 +0000119 free(arg);
hailfinger08cf2ed2010-10-05 19:19:48 +0000120 return 1;
121 } else {
122 lpt_iobase = (uint16_t)tmp;
123 msg_pinfo("Non-default I/O base requested. This will "
124 "not change the hardware settings.\n");
125 }
126 } else {
127 /* Pick a default value for the I/O base. */
128 lpt_iobase = 0x378;
129 }
hailfingerb9b9ccf2011-09-12 06:17:06 +0000130 free(arg);
hailfinger08cf2ed2010-10-05 19:19:48 +0000131
132 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
hailfinger52c4fa02010-07-21 10:26:01 +0000133 lpt_iobase);
134
hailfingerb9b9ccf2011-09-12 06:17:06 +0000135 arg = extract_programmer_param("type");
136 if (arg) {
137 if (!strcasecmp(arg, "rayer")) {
138 rayer_type = TYPE_RAYER;
139 } else if (!strcasecmp(arg, "xilinx")) {
140 rayer_type = TYPE_XILINX_DLC5;
141 } else {
142 msg_perr("Error: Invalid device type specified.\n");
143 free(arg);
144 return 1;
145 }
146 }
147 free(arg);
148 switch (rayer_type) {
149 case TYPE_RAYER:
150 msg_pdbg("Using RayeR SPIPGM pinout.\n");
151 /* Bits for master->slave direction */
152 rayer_cs_bit = 5;
153 rayer_sck_bit = 6;
154 rayer_mosi_bit = 7;
155 /* Bits for slave->master direction */
156 rayer_miso_bit = 6;
157 break;
158 case TYPE_XILINX_DLC5:
159 msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n");
160 /* Bits for master->slave direction */
161 rayer_cs_bit = 2;
162 rayer_sck_bit = 1;
163 rayer_mosi_bit = 0;
164 /* Bits for slave->master direction */
165 rayer_miso_bit = 4;
166 }
167
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100168 if (rget_io_perms())
169 return 1;
hailfinger52c4fa02010-07-21 10:26:01 +0000170
hailfinger08cf2ed2010-10-05 19:19:48 +0000171 /* Get the initial value before writing to any line. */
172 lpt_outbyte = INB(lpt_iobase);
173
Craig Hesling65eb8812019-08-01 09:33:56 -0700174 if (register_spi_bitbang_master(&bitbang_spi_master_rayer))
hailfinger52c4fa02010-07-21 10:26:01 +0000175 return 1;
176
hailfinger52c4fa02010-07-21 10:26:01 +0000177 return 0;
178}
179
180#else
181#error PCI port I/O access is not supported on this architecture yet.
182#endif