blob: 8e48e6ef0c695faa5c3cb403f167c2d9c1b54bec [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 Hailfingerae418d82011-09-12 06:17:06 +000034#include <string.h>
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000035#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000036#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000037#include "hwaccess.h"
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000038
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000039enum rayer_type {
40 TYPE_RAYER,
41 TYPE_XILINX_DLC5,
42};
43
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000044/* We have two sets of pins, out and in. The numbers for both sets are
45 * independent and are bitshift values, not real pin numbers.
Paul Menzel018d4822011-10-21 12:33:07 +000046 * Default settings are for the RayeR hardware.
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000047 */
48/* Pins for master->slave direction */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000049static int rayer_cs_bit = 5;
50static int rayer_sck_bit = 6;
51static int rayer_mosi_bit = 7;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000052/* Pins for slave->master direction */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000053static int rayer_miso_bit = 6;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000054
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000055static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000056
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000057/* Cached value of last byte sent. */
58static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000059
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000060static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000061{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000062 lpt_outbyte &= ~(1 << rayer_cs_bit);
63 lpt_outbyte |= (val << rayer_cs_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000064 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000065}
66
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000067static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000068{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000069 lpt_outbyte &= ~(1 << rayer_sck_bit);
70 lpt_outbyte |= (val << rayer_sck_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000071 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000072}
73
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000074static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000075{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000076 lpt_outbyte &= ~(1 << rayer_mosi_bit);
77 lpt_outbyte |= (val << rayer_mosi_bit);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000078 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000079}
80
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000081static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000082{
83 uint8_t tmp;
84
85 tmp = INB(lpt_iobase + 1);
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +000086 tmp = (tmp >> rayer_miso_bit) & 0x1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000087 return tmp;
88}
89
90static const struct bitbang_spi_master bitbang_spi_master_rayer = {
91 .type = BITBANG_SPI_MASTER_RAYER,
92 .set_cs = rayer_bitbang_set_cs,
93 .set_sck = rayer_bitbang_set_sck,
94 .set_mosi = rayer_bitbang_set_mosi,
95 .get_miso = rayer_bitbang_get_miso,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000096 .half_period = 0,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000097};
98
99int rayer_spi_init(void)
100{
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000101 char *arg = NULL;
102 enum rayer_type rayer_type = TYPE_RAYER;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000103
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000104 /* Non-default port requested? */
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000105 arg = extract_programmer_param("iobase");
106 if (arg) {
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000107 char *endptr = NULL;
108 unsigned long tmp;
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000109 tmp = strtoul(arg, &endptr, 0);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000110 /* Port 0, port >0x10000, unaligned ports and garbage strings
111 * are rejected.
112 */
113 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
114 (*endptr != '\0')) {
115 /* Using ports below 0x100 is a really bad idea, and
116 * should only be done if no port between 0x100 and
117 * 0xfffc works due to routing issues.
118 */
119 msg_perr("Error: iobase= specified, but the I/O base "
120 "given was invalid.\nIt must be a multiple of "
121 "0x4 and lie between 0x100 and 0xfffc.\n");
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000122 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000123 return 1;
124 } else {
125 lpt_iobase = (uint16_t)tmp;
126 msg_pinfo("Non-default I/O base requested. This will "
127 "not change the hardware settings.\n");
128 }
129 } else {
130 /* Pick a default value for the I/O base. */
131 lpt_iobase = 0x378;
132 }
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000133 free(arg);
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000134
135 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000136 lpt_iobase);
137
Carl-Daniel Hailfingerae418d82011-09-12 06:17:06 +0000138 arg = extract_programmer_param("type");
139 if (arg) {
140 if (!strcasecmp(arg, "rayer")) {
141 rayer_type = TYPE_RAYER;
142 } else if (!strcasecmp(arg, "xilinx")) {
143 rayer_type = TYPE_XILINX_DLC5;
144 } else {
145 msg_perr("Error: Invalid device type specified.\n");
146 free(arg);
147 return 1;
148 }
149 }
150 free(arg);
151 switch (rayer_type) {
152 case TYPE_RAYER:
153 msg_pdbg("Using RayeR SPIPGM pinout.\n");
154 /* Bits for master->slave direction */
155 rayer_cs_bit = 5;
156 rayer_sck_bit = 6;
157 rayer_mosi_bit = 7;
158 /* Bits for slave->master direction */
159 rayer_miso_bit = 6;
160 break;
161 case TYPE_XILINX_DLC5:
162 msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n");
163 /* Bits for master->slave direction */
164 rayer_cs_bit = 2;
165 rayer_sck_bit = 1;
166 rayer_mosi_bit = 0;
167 /* Bits for slave->master direction */
168 rayer_miso_bit = 4;
169 }
170
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000171 if (rget_io_perms())
172 return 1;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000173
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000174 /* Get the initial value before writing to any line. */
175 lpt_outbyte = INB(lpt_iobase);
176
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000177 if (bitbang_spi_init(&bitbang_spi_master_rayer))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000178 return 1;
179
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000180 return 0;
181}
182
183#else
184#error PCI port I/O access is not supported on this architecture yet.
185#endif