blob: b312610e33ffce2d3ade2cae7f5246e46651d197 [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
20/* Driver for the SPIPGM hardware by "RayeR" Martin Rehak.
21 * See http://rayer.ic.cz/elektro/spipgm.htm for schematics and instructions.
22 */
23
24/* This driver uses non-portable direct I/O port accesses which won't work on
25 * any non-x86 platform, and even on x86 there is a high chance there will be
26 * collisions with any loaded parallel port drivers.
27 * The big advantage of direct port I/O is OS independence and speed because
28 * most OS parport drivers will perform many unnecessary accesses although
29 * this driver just treats the parallel port as a GPIO set.
30 */
31#if defined(__i386__) || defined(__x86_64__)
32
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000033#include <stdlib.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000034#include <strings.h>
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000035#include <string.h>
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000036#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000037#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000038#include "hwaccess.h"
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000039
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000040enum rayer_type {
41 TYPE_RAYER,
42 TYPE_XILINX_DLC5,
43};
44
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000045/* We have two sets of pins, out and in. The numbers for both sets are
46 * independent and are bitshift values, not real pin numbers.
Paul Menzel018d4822011-10-21 12:33:07 +000047 * Default settings are for the RayeR hardware.
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000048 */
49/* Pins for master->slave direction */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000050static int rayer_cs_bit = 5;
51static int rayer_sck_bit = 6;
52static int rayer_mosi_bit = 7;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000053/* Pins for slave->master direction */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000054static int rayer_miso_bit = 6;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000055
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000056static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000057
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000058/* Cached value of last byte sent. */
59static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000060
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000061static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000062{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000063 lpt_outbyte &= ~(1 << rayer_cs_bit);
64 lpt_outbyte |= (val << rayer_cs_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000065 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000066}
67
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000068static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000069{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000070 lpt_outbyte &= ~(1 << rayer_sck_bit);
71 lpt_outbyte |= (val << rayer_sck_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000072 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000073}
74
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000075static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000076{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000077 lpt_outbyte &= ~(1 << rayer_mosi_bit);
78 lpt_outbyte |= (val << rayer_mosi_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000079 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000080}
81
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000082static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000083{
84 uint8_t tmp;
85
86 tmp = INB(lpt_iobase + 1);
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000087 tmp = (tmp >> rayer_miso_bit) & 0x1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000088 return tmp;
89}
90
91static const struct bitbang_spi_master bitbang_spi_master_rayer = {
92 .type = BITBANG_SPI_MASTER_RAYER,
93 .set_cs = rayer_bitbang_set_cs,
94 .set_sck = rayer_bitbang_set_sck,
95 .set_mosi = rayer_bitbang_set_mosi,
96 .get_miso = rayer_bitbang_get_miso,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000097 .half_period = 0,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000098};
99
100int rayer_spi_init(void)
101{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000102 char *arg = NULL;
103 enum rayer_type rayer_type = TYPE_RAYER;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000104
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000105 /* Non-default port requested? */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000106 arg = extract_programmer_param("iobase");
107 if (arg) {
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000108 char *endptr = NULL;
109 unsigned long tmp;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000110 tmp = strtoul(arg, &endptr, 0);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000111 /* Port 0, port >0x10000, unaligned ports and garbage strings
112 * are rejected.
113 */
114 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
115 (*endptr != '\0')) {
116 /* Using ports below 0x100 is a really bad idea, and
117 * should only be done if no port between 0x100 and
118 * 0xfffc works due to routing issues.
119 */
120 msg_perr("Error: iobase= specified, but the I/O base "
121 "given was invalid.\nIt must be a multiple of "
122 "0x4 and lie between 0x100 and 0xfffc.\n");
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000123 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000124 return 1;
125 } else {
126 lpt_iobase = (uint16_t)tmp;
127 msg_pinfo("Non-default I/O base requested. This will "
128 "not change the hardware settings.\n");
129 }
130 } else {
131 /* Pick a default value for the I/O base. */
132 lpt_iobase = 0x378;
133 }
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000134 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000135
136 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000137 lpt_iobase);
138
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000139 arg = extract_programmer_param("type");
140 if (arg) {
141 if (!strcasecmp(arg, "rayer")) {
142 rayer_type = TYPE_RAYER;
143 } else if (!strcasecmp(arg, "xilinx")) {
144 rayer_type = TYPE_XILINX_DLC5;
145 } else {
146 msg_perr("Error: Invalid device type specified.\n");
147 free(arg);
148 return 1;
149 }
150 }
151 free(arg);
152 switch (rayer_type) {
153 case TYPE_RAYER:
154 msg_pdbg("Using RayeR SPIPGM pinout.\n");
155 /* Bits for master->slave direction */
156 rayer_cs_bit = 5;
157 rayer_sck_bit = 6;
158 rayer_mosi_bit = 7;
159 /* Bits for slave->master direction */
160 rayer_miso_bit = 6;
161 break;
162 case TYPE_XILINX_DLC5:
163 msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n");
164 /* Bits for master->slave direction */
165 rayer_cs_bit = 2;
166 rayer_sck_bit = 1;
167 rayer_mosi_bit = 0;
168 /* Bits for slave->master direction */
169 rayer_miso_bit = 4;
170 }
171
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000172 if (rget_io_perms())
173 return 1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000174
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000175 /* Get the initial value before writing to any line. */
176 lpt_outbyte = INB(lpt_iobase);
177
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000178 if (bitbang_spi_init(&bitbang_spi_master_rayer))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000179 return 1;
180
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000181 return 0;
182}
183
184#else
185#error PCI port I/O access is not supported on this architecture yet.
186#endif