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