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 | |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 20 | /* Driver for various LPT adapters. |
| 21 | * |
| 22 | * This driver uses non-portable direct I/O port accesses which won't work on |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 23 | * any non-x86 platform, and even on x86 there is a high chance there will be |
| 24 | * collisions with any loaded parallel port drivers. |
| 25 | * The big advantage of direct port I/O is OS independence and speed because |
| 26 | * most OS parport drivers will perform many unnecessary accesses although |
| 27 | * this driver just treats the parallel port as a GPIO set. |
| 28 | */ |
| 29 | #if defined(__i386__) || defined(__x86_64__) |
| 30 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 32 | #include <strings.h> |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 33 | #include <string.h> |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 34 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 35 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 36 | #include "hwaccess.h" |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 37 | |
| 38 | /* We have two sets of pins, out and in. The numbers for both sets are |
| 39 | * independent and are bitshift values, not real pin numbers. |
Paul Menzel | 018d482 | 2011-10-21 12:33:07 +0000 | [diff] [blame] | 40 | * Default settings are for the RayeR hardware. |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 41 | */ |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 42 | |
| 43 | struct rayer_programmer { |
| 44 | const char *type; |
| 45 | const enum test_state status; |
| 46 | const char *description; |
| 47 | const void *dev_data; |
| 48 | }; |
| 49 | |
| 50 | struct rayer_pinout { |
| 51 | uint8_t cs_bit; |
| 52 | uint8_t sck_bit; |
| 53 | uint8_t mosi_bit; |
| 54 | uint8_t miso_bit; |
| 55 | void (*preinit)(const void *); |
| 56 | int (*shutdown)(void *); |
| 57 | }; |
| 58 | |
| 59 | static const struct rayer_pinout rayer_spipgm = { |
| 60 | .cs_bit = 5, |
| 61 | .sck_bit = 6, |
| 62 | .mosi_bit = 7, |
| 63 | .miso_bit = 6, |
| 64 | }; |
| 65 | |
Kyösti Mälkki | 1d47379 | 2013-10-02 01:22:17 +0000 | [diff] [blame] | 66 | static void dlc5_preinit(const void *); |
| 67 | static int dlc5_shutdown(void *); |
| 68 | |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 69 | static const struct rayer_pinout xilinx_dlc5 = { |
| 70 | .cs_bit = 2, |
| 71 | .sck_bit = 1, |
| 72 | .mosi_bit = 0, |
| 73 | .miso_bit = 4, |
Kyösti Mälkki | 1d47379 | 2013-10-02 01:22:17 +0000 | [diff] [blame] | 74 | .preinit = dlc5_preinit, |
| 75 | .shutdown = dlc5_shutdown, |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Maksim Kuleshov | 3647b2d | 2013-10-02 01:21:57 +0000 | [diff] [blame] | 78 | static void byteblaster_preinit(const void *); |
| 79 | static int byteblaster_shutdown(void *); |
| 80 | |
| 81 | static const struct rayer_pinout altera_byteblastermv = { |
| 82 | .cs_bit = 1, |
| 83 | .sck_bit = 0, |
| 84 | .mosi_bit = 6, |
| 85 | .miso_bit = 7, |
| 86 | .preinit = byteblaster_preinit, |
| 87 | .shutdown = byteblaster_shutdown, |
| 88 | }; |
| 89 | |
Maksim Kuleshov | 4dab5c1 | 2013-10-02 01:22:02 +0000 | [diff] [blame] | 90 | static void stk200_preinit(const void *); |
| 91 | static int stk200_shutdown(void *); |
| 92 | |
| 93 | static const struct rayer_pinout atmel_stk200 = { |
| 94 | .cs_bit = 7, |
| 95 | .sck_bit = 4, |
| 96 | .mosi_bit = 5, |
| 97 | .miso_bit = 6, |
| 98 | .preinit = stk200_preinit, |
| 99 | .shutdown = stk200_shutdown, |
| 100 | }; |
| 101 | |
Maksim Kuleshov | acba2ac | 2013-10-02 01:22:11 +0000 | [diff] [blame] | 102 | static const struct rayer_pinout wiggler_lpt = { |
| 103 | .cs_bit = 1, |
| 104 | .sck_bit = 2, |
| 105 | .mosi_bit = 3, |
| 106 | .miso_bit = 7, |
| 107 | }; |
| 108 | |
Stefan Tauner | fdb1659 | 2016-02-28 17:04:38 +0000 | [diff] [blame] | 109 | static const struct rayer_pinout spi_tt = { |
| 110 | .cs_bit = 2, |
| 111 | .sck_bit = 0, |
| 112 | .mosi_bit = 4, |
| 113 | .miso_bit = 7, |
| 114 | }; |
| 115 | |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 116 | static const struct rayer_programmer rayer_spi_types[] = { |
| 117 | {"rayer", NT, "RayeR SPIPGM", &rayer_spipgm}, |
| 118 | {"xilinx", NT, "Xilinx Parallel Cable III (DLC 5)", &xilinx_dlc5}, |
Maksim Kuleshov | 3647b2d | 2013-10-02 01:21:57 +0000 | [diff] [blame] | 119 | {"byteblastermv", OK, "Altera ByteBlasterMV", &altera_byteblastermv}, |
Maksim Kuleshov | 4dab5c1 | 2013-10-02 01:22:02 +0000 | [diff] [blame] | 120 | {"stk200", NT, "Atmel STK200/300 adapter", &atmel_stk200}, |
Maksim Kuleshov | acba2ac | 2013-10-02 01:22:11 +0000 | [diff] [blame] | 121 | {"wiggler", OK, "Wiggler LPT", &wiggler_lpt}, |
Stefan Tauner | fdb1659 | 2016-02-28 17:04:38 +0000 | [diff] [blame] | 122 | {"spi_tt", NT, "SPI Tiny Tools (SPI_TT LPT)", &spi_tt}, |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 123 | {0}, |
| 124 | }; |
| 125 | |
| 126 | static const struct rayer_pinout *pinout = NULL; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 127 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 128 | static uint16_t lpt_iobase; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 129 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 130 | /* Cached value of last byte sent. */ |
| 131 | static uint8_t lpt_outbyte; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 132 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 133 | static void rayer_bitbang_set_cs(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 134 | { |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 135 | lpt_outbyte &= ~(1 << pinout->cs_bit); |
| 136 | lpt_outbyte |= (val << pinout->cs_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 137 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 140 | static void rayer_bitbang_set_sck(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 141 | { |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 142 | lpt_outbyte &= ~(1 << pinout->sck_bit); |
| 143 | lpt_outbyte |= (val << pinout->sck_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 144 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 147 | static void rayer_bitbang_set_mosi(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 148 | { |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 149 | lpt_outbyte &= ~(1 << pinout->mosi_bit); |
| 150 | lpt_outbyte |= (val << pinout->mosi_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 151 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 154 | static int rayer_bitbang_get_miso(void) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 155 | { |
| 156 | uint8_t tmp; |
| 157 | |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 158 | tmp = INB(lpt_iobase + 1) ^ 0x80; // bit.7 inverted |
| 159 | tmp = (tmp >> pinout->miso_bit) & 0x1; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 160 | return tmp; |
| 161 | } |
| 162 | |
| 163 | static const struct bitbang_spi_master bitbang_spi_master_rayer = { |
| 164 | .type = BITBANG_SPI_MASTER_RAYER, |
| 165 | .set_cs = rayer_bitbang_set_cs, |
| 166 | .set_sck = rayer_bitbang_set_sck, |
| 167 | .set_mosi = rayer_bitbang_set_mosi, |
| 168 | .get_miso = rayer_bitbang_get_miso, |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 169 | .half_period = 0, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | int rayer_spi_init(void) |
| 173 | { |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 174 | const struct rayer_programmer *prog = rayer_spi_types; |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 175 | char *arg = NULL; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 176 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 177 | /* Non-default port requested? */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 178 | arg = extract_programmer_param("iobase"); |
| 179 | if (arg) { |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 180 | char *endptr = NULL; |
| 181 | unsigned long tmp; |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 182 | tmp = strtoul(arg, &endptr, 0); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 183 | /* Port 0, port >0x10000, unaligned ports and garbage strings |
| 184 | * are rejected. |
| 185 | */ |
| 186 | if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) || |
| 187 | (*endptr != '\0')) { |
| 188 | /* Using ports below 0x100 is a really bad idea, and |
| 189 | * should only be done if no port between 0x100 and |
| 190 | * 0xfffc works due to routing issues. |
| 191 | */ |
| 192 | msg_perr("Error: iobase= specified, but the I/O base " |
| 193 | "given was invalid.\nIt must be a multiple of " |
| 194 | "0x4 and lie between 0x100 and 0xfffc.\n"); |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 195 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 196 | return 1; |
| 197 | } else { |
| 198 | lpt_iobase = (uint16_t)tmp; |
| 199 | msg_pinfo("Non-default I/O base requested. This will " |
| 200 | "not change the hardware settings.\n"); |
| 201 | } |
| 202 | } else { |
| 203 | /* Pick a default value for the I/O base. */ |
| 204 | lpt_iobase = 0x378; |
| 205 | } |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 206 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 207 | |
| 208 | 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] | 209 | lpt_iobase); |
| 210 | |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 211 | arg = extract_programmer_param("type"); |
| 212 | if (arg) { |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 213 | for (; prog->type != NULL; prog++) { |
| 214 | if (strcasecmp(arg, prog->type) == 0) { |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | if (prog->type == NULL) { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 219 | msg_perr("Error: Invalid device type specified.\n"); |
| 220 | free(arg); |
| 221 | return 1; |
| 222 | } |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 223 | free(arg); |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 224 | } |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 225 | msg_pinfo("Using %s pinout.\n", prog->description); |
| 226 | pinout = (struct rayer_pinout *)prog->dev_data; |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 227 | |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 228 | if (rget_io_perms()) |
| 229 | return 1; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 230 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 231 | /* Get the initial value before writing to any line. */ |
| 232 | lpt_outbyte = INB(lpt_iobase); |
| 233 | |
Kyösti Mälkki | 8b1bdf1 | 2013-10-02 01:21:45 +0000 | [diff] [blame] | 234 | if (pinout->shutdown) |
| 235 | register_shutdown(pinout->shutdown, (void*)pinout); |
| 236 | if (pinout->preinit) |
| 237 | pinout->preinit(pinout); |
| 238 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 239 | if (register_spi_bitbang_master(&bitbang_spi_master_rayer)) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 240 | return 1; |
| 241 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Maksim Kuleshov | 3647b2d | 2013-10-02 01:21:57 +0000 | [diff] [blame] | 245 | static void byteblaster_preinit(const void *data){ |
| 246 | msg_pdbg("byteblaster_preinit\n"); |
| 247 | /* Assert #EN signal. */ |
| 248 | OUTB(2, lpt_iobase + 2 ); |
| 249 | } |
| 250 | |
| 251 | static int byteblaster_shutdown(void *data){ |
| 252 | msg_pdbg("byteblaster_shutdown\n"); |
| 253 | /* De-Assert #EN signal. */ |
| 254 | OUTB(0, lpt_iobase + 2 ); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Maksim Kuleshov | 4dab5c1 | 2013-10-02 01:22:02 +0000 | [diff] [blame] | 258 | static void stk200_preinit(const void *data) { |
| 259 | msg_pdbg("stk200_init\n"); |
| 260 | /* Assert #EN signals, set LED signal. */ |
| 261 | lpt_outbyte = (1 << 6) ; |
| 262 | OUTB(lpt_outbyte, lpt_iobase); |
| 263 | } |
| 264 | |
| 265 | static int stk200_shutdown(void *data) { |
| 266 | msg_pdbg("stk200_shutdown\n"); |
| 267 | /* Assert #EN signals, clear LED signal. */ |
| 268 | lpt_outbyte = (1 << 2) | (1 << 3); |
| 269 | OUTB(lpt_outbyte, lpt_iobase); |
| 270 | return 0; |
| 271 | } |
| 272 | |
Kyösti Mälkki | 1d47379 | 2013-10-02 01:22:17 +0000 | [diff] [blame] | 273 | static void dlc5_preinit(const void *data) { |
| 274 | msg_pdbg("dlc5_preinit\n"); |
| 275 | /* Assert pin 6 to receive MISO. */ |
| 276 | lpt_outbyte |= (1<<4); |
| 277 | OUTB(lpt_outbyte, lpt_iobase); |
| 278 | } |
| 279 | |
| 280 | static int dlc5_shutdown(void *data) { |
| 281 | msg_pdbg("dlc5_shutdown\n"); |
| 282 | /* De-assert pin 6 to force MISO low. */ |
| 283 | lpt_outbyte &= ~(1<<4); |
| 284 | OUTB(lpt_outbyte, lpt_iobase); |
| 285 | return 0; |
| 286 | } |
| 287 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 288 | #else |
| 289 | #error PCI port I/O access is not supported on this architecture yet. |
| 290 | #endif |