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 | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 34 | #include <string.h> |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 35 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 36 | #include "programmer.h" |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 37 | |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 38 | enum rayer_type { |
| 39 | TYPE_RAYER, |
| 40 | TYPE_XILINX_DLC5, |
| 41 | }; |
| 42 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 43 | /* We have two sets of pins, out and in. The numbers for both sets are |
| 44 | * independent and are bitshift values, not real pin numbers. |
Paul Menzel | 018d482 | 2011-10-21 12:33:07 +0000 | [diff] [blame] | 45 | * Default settings are for the RayeR hardware. |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 46 | */ |
| 47 | /* Pins for master->slave direction */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 48 | static int rayer_cs_bit = 5; |
| 49 | static int rayer_sck_bit = 6; |
| 50 | static int rayer_mosi_bit = 7; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 51 | /* Pins for slave->master direction */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 52 | static int rayer_miso_bit = 6; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 53 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 54 | static uint16_t lpt_iobase; |
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 | /* Cached value of last byte sent. */ |
| 57 | static uint8_t lpt_outbyte; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 58 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 59 | static void rayer_bitbang_set_cs(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 60 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 61 | lpt_outbyte &= ~(1 << rayer_cs_bit); |
| 62 | lpt_outbyte |= (val << rayer_cs_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 63 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 66 | static void rayer_bitbang_set_sck(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 67 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 68 | lpt_outbyte &= ~(1 << rayer_sck_bit); |
| 69 | lpt_outbyte |= (val << rayer_sck_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 70 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 73 | static void rayer_bitbang_set_mosi(int val) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 74 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 75 | lpt_outbyte &= ~(1 << rayer_mosi_bit); |
| 76 | lpt_outbyte |= (val << rayer_mosi_bit); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 77 | OUTB(lpt_outbyte, lpt_iobase); |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 80 | static int rayer_bitbang_get_miso(void) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 81 | { |
| 82 | uint8_t tmp; |
| 83 | |
| 84 | tmp = INB(lpt_iobase + 1); |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 85 | tmp = (tmp >> rayer_miso_bit) & 0x1; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 86 | return tmp; |
| 87 | } |
| 88 | |
| 89 | static const struct bitbang_spi_master bitbang_spi_master_rayer = { |
| 90 | .type = BITBANG_SPI_MASTER_RAYER, |
| 91 | .set_cs = rayer_bitbang_set_cs, |
| 92 | .set_sck = rayer_bitbang_set_sck, |
| 93 | .set_mosi = rayer_bitbang_set_mosi, |
| 94 | .get_miso = rayer_bitbang_get_miso, |
| 95 | }; |
| 96 | |
| 97 | int rayer_spi_init(void) |
| 98 | { |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 99 | char *arg = NULL; |
| 100 | enum rayer_type rayer_type = TYPE_RAYER; |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 102 | /* Non-default port requested? */ |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 103 | arg = extract_programmer_param("iobase"); |
| 104 | if (arg) { |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 105 | char *endptr = NULL; |
| 106 | unsigned long tmp; |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 107 | tmp = strtoul(arg, &endptr, 0); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 108 | /* Port 0, port >0x10000, unaligned ports and garbage strings |
| 109 | * are rejected. |
| 110 | */ |
| 111 | if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) || |
| 112 | (*endptr != '\0')) { |
| 113 | /* Using ports below 0x100 is a really bad idea, and |
| 114 | * should only be done if no port between 0x100 and |
| 115 | * 0xfffc works due to routing issues. |
| 116 | */ |
| 117 | msg_perr("Error: iobase= specified, but the I/O base " |
| 118 | "given was invalid.\nIt must be a multiple of " |
| 119 | "0x4 and lie between 0x100 and 0xfffc.\n"); |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 120 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 121 | return 1; |
| 122 | } else { |
| 123 | lpt_iobase = (uint16_t)tmp; |
| 124 | msg_pinfo("Non-default I/O base requested. This will " |
| 125 | "not change the hardware settings.\n"); |
| 126 | } |
| 127 | } else { |
| 128 | /* Pick a default value for the I/O base. */ |
| 129 | lpt_iobase = 0x378; |
| 130 | } |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 131 | free(arg); |
Carl-Daniel Hailfinger | 37c4252 | 2010-10-05 19:19:48 +0000 | [diff] [blame] | 132 | |
| 133 | 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] | 134 | lpt_iobase); |
| 135 | |
Carl-Daniel Hailfinger | ae418d8 | 2011-09-12 06:17:06 +0000 | [diff] [blame] | 136 | arg = extract_programmer_param("type"); |
| 137 | if (arg) { |
| 138 | if (!strcasecmp(arg, "rayer")) { |
| 139 | rayer_type = TYPE_RAYER; |
| 140 | } else if (!strcasecmp(arg, "xilinx")) { |
| 141 | rayer_type = TYPE_XILINX_DLC5; |
| 142 | } else { |
| 143 | msg_perr("Error: Invalid device type specified.\n"); |
| 144 | free(arg); |
| 145 | return 1; |
| 146 | } |
| 147 | } |
| 148 | free(arg); |
| 149 | switch (rayer_type) { |
| 150 | case TYPE_RAYER: |
| 151 | msg_pdbg("Using RayeR SPIPGM pinout.\n"); |
| 152 | /* Bits for master->slave direction */ |
| 153 | rayer_cs_bit = 5; |
| 154 | rayer_sck_bit = 6; |
| 155 | rayer_mosi_bit = 7; |
| 156 | /* Bits for slave->master direction */ |
| 157 | rayer_miso_bit = 6; |
| 158 | break; |
| 159 | case TYPE_XILINX_DLC5: |
| 160 | msg_pdbg("Using Xilinx Parallel Cable III (DLC 5) pinout.\n"); |
| 161 | /* Bits for master->slave direction */ |
| 162 | rayer_cs_bit = 2; |
| 163 | rayer_sck_bit = 1; |
| 164 | rayer_mosi_bit = 0; |
| 165 | /* Bits for slave->master direction */ |
| 166 | rayer_miso_bit = 4; |
| 167 | } |
| 168 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 169 | get_io_perms(); |
| 170 | |
Carl-Daniel Hailfinger | 37c4252 | 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 | |
| 174 | /* Zero halfperiod delay. */ |
| 175 | if (bitbang_spi_init(&bitbang_spi_master_rayer, 0)) |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 176 | return 1; |
| 177 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | #else |
| 182 | #error PCI port I/O access is not supported on this architecture yet. |
| 183 | #endif |