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