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