blob: 37cf752267faad5305a032c1df67c1295c3e7929 [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
33#include "flash.h"
34
35/* We have two sets of pins, out and in. The numbers for both sets are
36 * independent and are bitshift values, not real pin numbers.
37 */
38/* Pins for master->slave direction */
39#define SPI_CS_PIN 5
40#define SPI_SCK_PIN 6
41#define SPI_MOSI_PIN 7
42/* Pins for slave->master direction */
43#define SPI_MISO_PIN 6
44
45static int lpt_iobase;
46
47/* FIXME: All rayer_bitbang_set_* functions could use caching of the value
48 * stored at port lpt_iobase to avoid unnecessary INB. In theory, only one
49 * INB(lpt_iobase) would be needed on programmer init to get the initial
50 * value.
51 */
52
53void rayer_bitbang_set_cs(int val)
54{
55 uint8_t tmp;
56
57 tmp = INB(lpt_iobase);
58 tmp &= ~(1 << SPI_CS_PIN);
59 tmp |= (val << SPI_CS_PIN);
60 OUTB(tmp, lpt_iobase);
61}
62
63void rayer_bitbang_set_sck(int val)
64{
65 uint8_t tmp;
66
67 tmp = INB(lpt_iobase);
68 tmp &= ~(1 << SPI_SCK_PIN);
69 tmp |= (val << SPI_SCK_PIN);
70 OUTB(tmp, lpt_iobase);
71}
72
73void rayer_bitbang_set_mosi(int val)
74{
75 uint8_t tmp;
76
77 tmp = INB(lpt_iobase);
78 tmp &= ~(1 << SPI_MOSI_PIN);
79 tmp |= (val << SPI_MOSI_PIN);
80 OUTB(tmp, lpt_iobase);
81}
82
83int rayer_bitbang_get_miso(void)
84{
85 uint8_t tmp;
86
87 tmp = INB(lpt_iobase + 1);
88 tmp = (tmp >> SPI_MISO_PIN) & 0x1;
89 return tmp;
90}
91
92static const struct bitbang_spi_master bitbang_spi_master_rayer = {
93 .type = BITBANG_SPI_MASTER_RAYER,
94 .set_cs = rayer_bitbang_set_cs,
95 .set_sck = rayer_bitbang_set_sck,
96 .set_mosi = rayer_bitbang_set_mosi,
97 .get_miso = rayer_bitbang_get_miso,
98};
99
100int rayer_spi_init(void)
101{
102 /* Pick a default value for now. */
103 lpt_iobase = 0x378;
104
105 msg_pdbg("Using port 0x%x as I/O base for parallel port access.\n",
106 lpt_iobase);
107
108 get_io_perms();
109
110 /* 1 usec halfperiod delay for now. */
111 if (bitbang_spi_init(&bitbang_spi_master_rayer, 1))
112 return 1;
113
114 buses_supported = CHIP_BUSTYPE_SPI;
115 spi_controller = SPI_CONTROLLER_RAYER;
116
117 return 0;
118}
119
120#else
121#error PCI port I/O access is not supported on this architecture yet.
122#endif