hailfinger | 52c4fa0 | 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 | * |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* Driver for the SPIPGM hardware by "RayeR" Martin Rehak. |
| 18 | * See http://rayer.ic.cz/elektro/spipgm.htm for schematics and instructions. |
| 19 | */ |
| 20 | |
| 21 | /* This driver uses non-portable direct I/O port accesses which won't work on |
| 22 | * any non-x86 platform, and even on x86 there is a high chance there will be |
| 23 | * collisions with any loaded parallel port drivers. |
| 24 | * The big advantage of direct port I/O is OS independence and speed because |
| 25 | * most OS parport drivers will perform many unnecessary accesses although |
| 26 | * this driver just treats the parallel port as a GPIO set. |
| 27 | */ |
| 28 | #if defined(__i386__) || defined(__x86_64__) |
| 29 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 31 | #include <string.h> |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 32 | #include "flash.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 33 | #include "programmer.h" |
Mayur Panchal | f479686 | 2019-08-05 15:46:12 +1000 | [diff] [blame^] | 34 | #include "hwaccess.h" |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 35 | |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 36 | enum rayer_type { |
| 37 | TYPE_RAYER, |
| 38 | TYPE_XILINX_DLC5, |
| 39 | }; |
| 40 | |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 41 | /* We have two sets of pins, out and in. The numbers for both sets are |
| 42 | * independent and are bitshift values, not real pin numbers. |
stefanct | 98d917c | 2011-10-21 12:33:07 +0000 | [diff] [blame] | 43 | * Default settings are for the RayeR hardware. |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 44 | */ |
| 45 | /* Pins for master->slave direction */ |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 46 | static int rayer_cs_bit = 5; |
| 47 | static int rayer_sck_bit = 6; |
| 48 | static int rayer_mosi_bit = 7; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 49 | /* Pins for slave->master direction */ |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 50 | static int rayer_miso_bit = 6; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 51 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 52 | static uint16_t lpt_iobase; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 53 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 54 | /* Cached value of last byte sent. */ |
| 55 | static uint8_t lpt_outbyte; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 56 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 57 | static void rayer_bitbang_set_cs(int val) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 58 | { |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 59 | lpt_outbyte &= ~(1 << rayer_cs_bit); |
| 60 | lpt_outbyte |= (val << rayer_cs_bit); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 61 | OUTB(lpt_outbyte, lpt_iobase); |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 64 | static void rayer_bitbang_set_sck(int val) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 65 | { |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 66 | lpt_outbyte &= ~(1 << rayer_sck_bit); |
| 67 | lpt_outbyte |= (val << rayer_sck_bit); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 68 | OUTB(lpt_outbyte, lpt_iobase); |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 69 | } |
| 70 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 71 | static void rayer_bitbang_set_mosi(int val) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 72 | { |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 73 | lpt_outbyte &= ~(1 << rayer_mosi_bit); |
| 74 | lpt_outbyte |= (val << rayer_mosi_bit); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 75 | OUTB(lpt_outbyte, lpt_iobase); |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 76 | } |
| 77 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 78 | static int rayer_bitbang_get_miso(void) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 79 | { |
| 80 | uint8_t tmp; |
| 81 | |
| 82 | tmp = INB(lpt_iobase + 1); |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 83 | tmp = (tmp >> rayer_miso_bit) & 0x1; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 84 | return tmp; |
| 85 | } |
| 86 | |
| 87 | static const struct bitbang_spi_master bitbang_spi_master_rayer = { |
| 88 | .type = BITBANG_SPI_MASTER_RAYER, |
| 89 | .set_cs = rayer_bitbang_set_cs, |
| 90 | .set_sck = rayer_bitbang_set_sck, |
| 91 | .set_mosi = rayer_bitbang_set_mosi, |
| 92 | .get_miso = rayer_bitbang_get_miso, |
Patrick Georgi | e081d5d | 2017-03-22 21:18:18 +0100 | [diff] [blame] | 93 | .half_period = 0, |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 96 | int rayer_spi_init(void) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 97 | { |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 98 | char *arg = NULL; |
| 99 | enum rayer_type rayer_type = TYPE_RAYER; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 100 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 101 | /* Non-default port requested? */ |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 102 | arg = extract_programmer_param("iobase"); |
| 103 | if (arg) { |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 104 | char *endptr = NULL; |
| 105 | unsigned long tmp; |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 106 | tmp = strtoul(arg, &endptr, 0); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 107 | /* Port 0, port >0x10000, unaligned ports and garbage strings |
| 108 | * are rejected. |
| 109 | */ |
| 110 | if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) || |
| 111 | (*endptr != '\0')) { |
| 112 | /* Using ports below 0x100 is a really bad idea, and |
| 113 | * should only be done if no port between 0x100 and |
| 114 | * 0xfffc works due to routing issues. |
| 115 | */ |
| 116 | msg_perr("Error: iobase= specified, but the I/O base " |
| 117 | "given was invalid.\nIt must be a multiple of " |
| 118 | "0x4 and lie between 0x100 and 0xfffc.\n"); |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 119 | free(arg); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 120 | return 1; |
| 121 | } else { |
| 122 | lpt_iobase = (uint16_t)tmp; |
| 123 | msg_pinfo("Non-default I/O base requested. This will " |
| 124 | "not change the hardware settings.\n"); |
| 125 | } |
| 126 | } else { |
| 127 | /* Pick a default value for the I/O base. */ |
| 128 | lpt_iobase = 0x378; |
| 129 | } |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 130 | free(arg); |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 131 | |
| 132 | msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n", |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 133 | lpt_iobase); |
| 134 | |
hailfinger | b9b9ccf | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 135 | arg = extract_programmer_param("type"); |
| 136 | if (arg) { |
| 137 | if (!strcasecmp(arg, "rayer")) { |
| 138 | rayer_type = TYPE_RAYER; |
| 139 | } else if (!strcasecmp(arg, "xilinx")) { |
| 140 | rayer_type = TYPE_XILINX_DLC5; |
| 141 | } else { |
| 142 | msg_perr("Error: Invalid device type specified.\n"); |
| 143 | free(arg); |
| 144 | return 1; |
| 145 | } |
| 146 | } |
| 147 | free(arg); |
| 148 | switch (rayer_type) { |
| 149 | case TYPE_RAYER: |
| 150 | msg_pdbg("Using RayeR SPIPGM pinout.\n"); |
| 151 | /* Bits for master->slave direction */ |
| 152 | rayer_cs_bit = 5; |
| 153 | rayer_sck_bit = 6; |
| 154 | rayer_mosi_bit = 7; |
| 155 | /* Bits for slave->master direction */ |
| 156 | rayer_miso_bit = 6; |
| 157 | break; |
| 158 | case TYPE_XILINX_DLC5: |
| 159 | msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n"); |
| 160 | /* Bits for master->slave direction */ |
| 161 | rayer_cs_bit = 2; |
| 162 | rayer_sck_bit = 1; |
| 163 | rayer_mosi_bit = 0; |
| 164 | /* Bits for slave->master direction */ |
| 165 | rayer_miso_bit = 4; |
| 166 | } |
| 167 | |
Patrick Georgi | 2a2d67f | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 168 | if (rget_io_perms()) |
| 169 | return 1; |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 170 | |
hailfinger | 08cf2ed | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 171 | /* Get the initial value before writing to any line. */ |
| 172 | lpt_outbyte = INB(lpt_iobase); |
| 173 | |
Craig Hesling | 65eb881 | 2019-08-01 09:33:56 -0700 | [diff] [blame] | 174 | if (register_spi_bitbang_master(&bitbang_spi_master_rayer)) |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 175 | return 1; |
| 176 | |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | #else |
| 181 | #error PCI port I/O access is not supported on this architecture yet. |
| 182 | #endif |