Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 1 | /* |
| 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 Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame^] | 34 | #include <strings.h> |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 35 | #include <string.h> |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 36 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 37 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 38 | #include "hwaccess.h" |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 39 | |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 40 | enum rayer_type { |
| 41 | TYPE_RAYER, |
| 42 | TYPE_XILINX_DLC5, |
| 43 | }; |
| 44 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 45 | /* 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 Menzel | 018d482 | 2011-10-21 12:33:07 +0000 | [diff] [blame] | 47 | * Default settings are for the RayeR hardware. |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 48 | */ |
| 49 | /* Pins for master->slave direction */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 50 | static int rayer_cs_bit = 5; |
| 51 | static int rayer_sck_bit = 6; |
| 52 | static int rayer_mosi_bit = 7; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 53 | /* Pins for slave->master direction */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 54 | static int rayer_miso_bit = 6; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 55 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 56 | static uint16_t lpt_iobase; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 57 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 58 | /* Cached value of last byte sent. */ |
| 59 | static uint8_t lpt_outbyte; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 60 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 61 | static void rayer_bitbang_set_cs(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 62 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 63 | lpt_outbyte &= ~(1 << rayer_cs_bit); |
| 64 | lpt_outbyte |= (val << rayer_cs_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 65 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 68 | static void rayer_bitbang_set_sck(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 69 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 70 | lpt_outbyte &= ~(1 << rayer_sck_bit); |
| 71 | lpt_outbyte |= (val << rayer_sck_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 72 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 75 | static void rayer_bitbang_set_mosi(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 76 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 77 | lpt_outbyte &= ~(1 << rayer_mosi_bit); |
| 78 | lpt_outbyte |= (val << rayer_mosi_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 79 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 82 | static int rayer_bitbang_get_miso(void) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 83 | { |
| 84 | uint8_t tmp; |
| 85 | |
| 86 | tmp = INB(lpt_iobase + 1); |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 87 | tmp = (tmp >> rayer_miso_bit) & 0x1; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 88 | return tmp; |
| 89 | } |
| 90 | |
| 91 | static 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 Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 97 | .half_period = 0, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | int rayer_spi_init(void) |
| 101 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 102 | char *arg = NULL; |
| 103 | enum rayer_type rayer_type = TYPE_RAYER; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 104 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 105 | /* Non-default port requested? */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 106 | arg = extract_programmer_param("iobase"); |
| 107 | if (arg) { |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 108 | char *endptr = NULL; |
| 109 | unsigned long tmp; |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 110 | tmp = strtoul(arg, &endptr, 0); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 111 | /* 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 Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 123 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 124 | 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 Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 134 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 135 | |
| 136 | msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n", |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 137 | lpt_iobase); |
| 138 | |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 139 | 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 Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 172 | if (rget_io_perms()) |
| 173 | return 1; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 174 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 175 | /* Get the initial value before writing to any line. */ |
| 176 | lpt_outbyte = INB(lpt_iobase); |
| 177 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 178 | if (bitbang_spi_init(&bitbang_spi_master_rayer)) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 179 | return 1; |
| 180 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | #else |
| 185 | #error PCI port I/O access is not supported on this architecture yet. |
| 186 | #endif |