iPXE
spi_bit.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stddef.h>
28#include <stdint.h>
29#include <string.h>
30#include <byteswap.h>
31#include <errno.h>
32#include <assert.h>
33#include <unistd.h>
34#include <ipxe/bitbash.h>
35#include <ipxe/spi_bit.h>
36
37/** @file
38 *
39 * SPI bit-bashing interface
40 *
41 */
42
43/** Delay between SCLK changes and around SS changes */
44static void spi_bit_delay ( void ) {
46}
47
48/** Chip select line will be asserted */
49#define SELECT_SLAVE 0
50
51/** Chip select line will be deasserted */
52#define DESELECT_SLAVE SPI_MODE_SSPOL
53
54/**
55 * Select/deselect slave
56 *
57 * @v spibit SPI bit-bashing interface
58 * @v slave Slave number
59 * @v state Slave select state
60 *
61 * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
62 */
63static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
64 unsigned int slave,
65 unsigned int state ) {
66 struct bit_basher *basher = &spibit->basher;
67
68 state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
69 DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
70 spibit, slave, ( state ? "high" : "low" ) );
71
73 write_bit ( basher, SPI_BIT_SS ( slave ), state );
75}
76
77/**
78 * Transfer bits over SPI bit-bashing bus
79 *
80 * @v bus SPI bus
81 * @v data_out TX data buffer (or NULL)
82 * @v data_in RX data buffer (or NULL)
83 * @v len Length of transfer (in @b bits)
84 * @v endianness Endianness of this data transfer
85 *
86 * This issues @c len clock cycles on the SPI bus, shifting out data
87 * from the @c data_out buffer to the MOSI line and shifting in data
88 * from the MISO line to the @c data_in buffer. If @c data_out is
89 * NULL, then the data sent will be all zeroes. If @c data_in is
90 * NULL, then the incoming data will be discarded.
91 */
92static void spi_bit_transfer ( struct spi_bit_basher *spibit,
93 const void *data_out, void *data_in,
94 unsigned int len, int endianness ) {
95 struct spi_bus *bus = &spibit->bus;
96 struct bit_basher *basher = &spibit->basher;
97 unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
98 unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
99 unsigned int bit_offset;
100 unsigned int byte_offset;
101 unsigned int byte_mask;
102 unsigned int bit;
103 unsigned int step;
104
105 DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
106 spibit, len, bus->mode );
107
108 for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
109 /* Calculate byte offset and byte mask */
110 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
111 ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
112 byte_offset = ( bit_offset / 8 );
113 byte_mask = ( 1 << ( bit_offset % 8 ) );
114
115 /* Shift data in or out */
116 if ( sclk == cpha ) {
117 const uint8_t *byte;
118
119 /* Shift data out */
120 if ( data_out ) {
121 byte = ( data_out + byte_offset );
122 bit = ( *byte & byte_mask );
123 DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
124 spibit, ( bit ? 1 : 0 ) );
125 } else {
126 bit = 0;
127 }
128 write_bit ( basher, SPI_BIT_MOSI, bit );
129 } else {
130 uint8_t *byte;
131
132 /* Shift data in */
133 bit = read_bit ( basher, SPI_BIT_MISO );
134 if ( data_in ) {
135 DBGCP ( spibit, "SPIBIT %p read bit %d\n",
136 spibit, ( bit ? 1 : 0 ) );
137 byte = ( data_in + byte_offset );
138 *byte &= ~byte_mask;
139 *byte |= ( bit & byte_mask );
140 }
141 }
142
143 /* Toggle clock line */
145 sclk ^= 1;
146 write_bit ( basher, SPI_BIT_SCLK, sclk );
147 }
148}
149
150/**
151 * Read/write data via SPI bit-bashing bus
152 *
153 * @v bus SPI bus
154 * @v device SPI device
155 * @v command Command
156 * @v address Address to read/write (<0 for no address)
157 * @v data_out TX data buffer (or NULL)
158 * @v data_in RX data buffer (or NULL)
159 * @v len Length of transfer
160 * @ret rc Return status code
161 */
162static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
163 unsigned int command, int address,
164 const void *data_out, void *data_in, size_t len ) {
165 struct spi_bit_basher *spibit
166 = container_of ( bus, struct spi_bit_basher, bus );
167 uint32_t tmp_command;
168 uint32_t tmp_address;
169 uint32_t tmp_address_detect;
170
171 /* Open bit-bashing interface */
172 open_bit ( &spibit->basher );
173
174 /* Deassert chip select to reset specified slave */
176
177 /* Set clock line to idle state */
178 write_bit ( &spibit->basher, SPI_BIT_SCLK,
179 ( bus->mode & SPI_MODE_CPOL ) );
180
181 /* Assert chip select on specified slave */
182 spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
183
184 /* Transmit command */
185 assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
186 tmp_command = cpu_to_le32 ( command );
187 spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
189
190 /* Transmit address, if present */
191 if ( address >= 0 ) {
192 assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
193 tmp_address = cpu_to_le32 ( address );
194 if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
195 /* Autodetect address length. This relies on
196 * the device responding with a dummy zero
197 * data bit before the first real data bit.
198 */
199 DBGC ( spibit, "SPIBIT %p autodetecting device "
200 "address length\n", spibit );
201 assert ( address == 0 );
202 device->address_len = 0;
203 do {
204 spi_bit_transfer ( spibit, &tmp_address,
205 &tmp_address_detect, 1,
207 device->address_len++;
208 } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
209 DBGC ( spibit, "SPIBIT %p autodetected device address "
210 "length %d\n", spibit, device->address_len );
211 } else {
212 spi_bit_transfer ( spibit, &tmp_address, NULL,
213 device->address_len,
215 }
216 }
217
218 /* Transmit/receive data */
219 spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
220 spibit->endianness );
221
222 /* Deassert chip select on specified slave */
224
225 /* Close bit-bashing interface */
226 close_bit ( &spibit->basher );
227
228 return 0;
229}
230
231/**
232 * Initialise SPI bit-bashing interface
233 *
234 * @v spibit SPI bit-bashing interface
235 */
236void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
237 assert ( &spibit->basher.op->read != NULL );
238 assert ( &spibit->basher.op->write != NULL );
239 spibit->bus.rw = spi_bit_rw;
240}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
int read_bit(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition bitbash.c:61
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition bitbash.c:45
Bit-bashing interfaces.
static void open_bit(struct bit_basher *basher)
Open bit-bashing interface.
Definition bitbash.h:66
static void close_bit(struct bit_basher *basher)
Close bit-bashing interface.
Definition bitbash.h:76
ring len
Length.
Definition dwmac.h:226
uint8_t bus
Bus.
Definition edd.h:1
uint8_t slave
Slave.
Definition edd.h:1
uint64_t address
Base address.
Definition ena.h:13
Error codes.
uint8_t state
State.
Definition eth_slow.h:36
#define DBGC2(...)
Definition compiler.h:522
#define DBGCP(...)
Definition compiler.h:539
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define le32_to_cpu(value)
Definition byteswap.h:114
#define cpu_to_le32(value)
Definition byteswap.h:108
static unsigned int unsigned int bit
Definition bigint.h:392
String functions.
void step(void)
Single-step a single process.
Definition process.c:99
unsigned char byte
Definition smc9000.h:38
#define SPI_MODE_CPHA
Clock phase (CPHA) mode bit.
Definition spi.h:164
#define SPI_MODE_CPOL
Clock polarity (CPOL) mode bit.
Definition spi.h:170
#define SPI_MODE_SSPOL
Slave select polarity mode bit.
Definition spi.h:179
#define SPI_AUTODETECT_ADDRESS_LEN
SPI magic autodetection address length.
Definition spi.h:114
#define SELECT_SLAVE
Chip select line will be asserted.
Definition spi_bit.c:49
static void spi_bit_delay(void)
Delay between SCLK changes and around SS changes.
Definition spi_bit.c:44
static void spi_bit_set_slave_select(struct spi_bit_basher *spibit, unsigned int slave, unsigned int state)
Select/deselect slave.
Definition spi_bit.c:63
void init_spi_bit_basher(struct spi_bit_basher *spibit)
Initialise SPI bit-bashing interface.
Definition spi_bit.c:236
#define DESELECT_SLAVE
Chip select line will be deasserted.
Definition spi_bit.c:52
static void spi_bit_transfer(struct spi_bit_basher *spibit, const void *data_out, void *data_in, unsigned int len, int endianness)
Transfer bits over SPI bit-bashing bus.
Definition spi_bit.c:92
static int spi_bit_rw(struct spi_bus *bus, struct spi_device *device, unsigned int command, int address, const void *data_out, void *data_in, size_t len)
Read/write data via SPI bit-bashing bus.
Definition spi_bit.c:162
SPI bit-bashing interface.
#define SPI_BIT_UDELAY
Delay between SCLK transitions.
Definition spi_bit.h:54
@ SPI_BIT_MOSI
Master Out Slave In.
Definition spi_bit.h:38
@ SPI_BIT_MISO
Master In Slave Out.
Definition spi_bit.h:40
@ SPI_BIT_SCLK
Serial clock.
Definition spi_bit.h:36
#define SPI_BIT_SS(slave)
Determine bit index for a particular slave.
Definition spi_bit.h:51
#define SPI_BIT_BIG_ENDIAN
SPI bit basher treats data as big-endian.
Definition spi_bit.h:57
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
void(* write)(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition bitbash.h:42
int(* read)(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition bitbash.h:52
A bit-bashing interface.
Definition bitbash.h:56
struct bit_basher_operations * op
Bit-bashing operations.
Definition bitbash.h:58
A command-line command.
Definition command.h:10
A hardware device.
Definition device.h:77
A bit-bashing SPI bus.
Definition spi_bit.h:17
struct spi_bus bus
SPI bus.
Definition spi_bit.h:19
int endianness
Endianness of data.
Definition spi_bit.h:30
struct bit_basher basher
Bit-bashing interface.
Definition spi_bit.h:21
An SPI bus.
Definition spi.h:127
int(* rw)(struct spi_bus *bus, struct spi_device *device, unsigned int command, int address, const void *data_out, void *data_in, size_t len)
Read/write data via SPI bus.
Definition spi.h:153
unsigned int mode
SPI interface mode.
Definition spi.h:137
An SPI device.
Definition spi.h:87
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition timer.c:61