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