blob: a01ee801fe73d671e6f1bf2ffb4b54df2b9e3591 [file] [log] [blame]
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +00001/*
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 Hailfinger37c42522010-10-05 19:19:48 +000033#include <stdlib.h>
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000034#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000035#include "programmer.h"
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000036
37/* We have two sets of pins, out and in. The numbers for both sets are
38 * independent and are bitshift values, not real pin numbers.
39 */
40/* Pins for master->slave direction */
41#define SPI_CS_PIN 5
42#define SPI_SCK_PIN 6
43#define SPI_MOSI_PIN 7
44/* Pins for slave->master direction */
45#define SPI_MISO_PIN 6
46
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000047static uint16_t lpt_iobase;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000048
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000049/* Cached value of last byte sent. */
50static uint8_t lpt_outbyte;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000051
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000052static void rayer_bitbang_set_cs(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000053{
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000054 lpt_outbyte &= ~(1 << SPI_CS_PIN);
55 lpt_outbyte |= (val << SPI_CS_PIN);
56 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000057}
58
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000059static void rayer_bitbang_set_sck(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000060{
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000061 lpt_outbyte &= ~(1 << SPI_SCK_PIN);
62 lpt_outbyte |= (val << SPI_SCK_PIN);
63 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000064}
65
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000066static void rayer_bitbang_set_mosi(int val)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000067{
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000068 lpt_outbyte &= ~(1 << SPI_MOSI_PIN);
69 lpt_outbyte |= (val << SPI_MOSI_PIN);
70 OUTB(lpt_outbyte, lpt_iobase);
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000071}
72
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000073static int rayer_bitbang_get_miso(void)
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000074{
75 uint8_t tmp;
76
77 tmp = INB(lpt_iobase + 1);
78 tmp = (tmp >> SPI_MISO_PIN) & 0x1;
79 return tmp;
80}
81
82static const struct bitbang_spi_master bitbang_spi_master_rayer = {
83 .type = BITBANG_SPI_MASTER_RAYER,
84 .set_cs = rayer_bitbang_set_cs,
85 .set_sck = rayer_bitbang_set_sck,
86 .set_mosi = rayer_bitbang_set_mosi,
87 .get_miso = rayer_bitbang_get_miso,
88};
89
90int rayer_spi_init(void)
91{
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000092 char *portpos = NULL;
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000093
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +000094 /* Non-default port requested? */
95 portpos = extract_programmer_param("iobase");
96 if (portpos) {
97 char *endptr = NULL;
98 unsigned long tmp;
99 tmp = strtoul(portpos, &endptr, 0);
100 /* Port 0, port >0x10000, unaligned ports and garbage strings
101 * are rejected.
102 */
103 if (!tmp || (tmp >= 0x10000) || (tmp & 0x3) ||
104 (*endptr != '\0')) {
105 /* Using ports below 0x100 is a really bad idea, and
106 * should only be done if no port between 0x100 and
107 * 0xfffc works due to routing issues.
108 */
109 msg_perr("Error: iobase= specified, but the I/O base "
110 "given was invalid.\nIt must be a multiple of "
111 "0x4 and lie between 0x100 and 0xfffc.\n");
112 free(portpos);
113 return 1;
114 } else {
115 lpt_iobase = (uint16_t)tmp;
116 msg_pinfo("Non-default I/O base requested. This will "
117 "not change the hardware settings.\n");
118 }
119 } else {
120 /* Pick a default value for the I/O base. */
121 lpt_iobase = 0x378;
122 }
123 free(portpos);
124
125 msg_pdbg("Using address 0x%x as I/O base for parallel port access.\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000126 lpt_iobase);
127
128 get_io_perms();
129
Carl-Daniel Hailfinger37c42522010-10-05 19:19:48 +0000130 /* Get the initial value before writing to any line. */
131 lpt_outbyte = INB(lpt_iobase);
132
133 /* Zero halfperiod delay. */
134 if (bitbang_spi_init(&bitbang_spi_master_rayer, 0))
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000135 return 1;
136
137 buses_supported = CHIP_BUSTYPE_SPI;
138 spi_controller = SPI_CONTROLLER_RAYER;
139
140 return 0;
141}
142
143#else
144#error PCI port I/O access is not supported on this architecture yet.
145#endif