2006-06-14 00:22:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2006-12-04 15:36:51 +00:00
|
|
|
#include <byteswap.h>
|
2006-06-14 00:22:50 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <timer.h>
|
|
|
|
#include <gpxe/bitbash.h>
|
2006-12-04 15:36:51 +00:00
|
|
|
#include <gpxe/spi_bit.h>
|
2006-06-14 00:22:50 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* SPI bit-bashing interface
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Delay between SCLK changes and around SS changes */
|
2006-12-04 15:36:51 +00:00
|
|
|
static void spi_bit_delay ( void ) {
|
|
|
|
udelay ( SPI_BIT_UDELAY );
|
2006-06-14 00:22:50 +00:00
|
|
|
}
|
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
/** Chip select line will be asserted */
|
|
|
|
#define SELECT_SLAVE 0
|
|
|
|
|
|
|
|
/** Chip select line will be deasserted */
|
|
|
|
#define DESELECT_SLAVE SPI_MODE_SSPOL
|
|
|
|
|
2006-06-14 00:22:50 +00:00
|
|
|
/**
|
|
|
|
* Select/deselect slave
|
|
|
|
*
|
2006-12-04 15:36:51 +00:00
|
|
|
* @v spibit SPI bit-bashing interface
|
2006-06-14 00:22:50 +00:00
|
|
|
* @v slave Slave number
|
|
|
|
* @v state Slave select state
|
|
|
|
*
|
2006-12-04 15:36:51 +00:00
|
|
|
* @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
|
2006-06-14 00:22:50 +00:00
|
|
|
*/
|
|
|
|
static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
|
|
|
|
unsigned int slave,
|
|
|
|
unsigned int state ) {
|
|
|
|
struct bit_basher *basher = &spibit->basher;
|
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
|
2006-06-14 00:22:50 +00:00
|
|
|
DBG ( "Setting slave %d select %s\n", slave,
|
|
|
|
( state ? "high" : "low" ) );
|
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
spi_bit_delay();
|
2006-06-14 00:22:50 +00:00
|
|
|
write_bit ( basher, SPI_BIT_SS ( slave ), state );
|
2006-12-04 15:36:51 +00:00
|
|
|
spi_bit_delay();
|
2006-06-14 00:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-04 15:36:51 +00:00
|
|
|
* Transfer bits over SPI bit-bashing bus
|
2006-06-14 00:22:50 +00:00
|
|
|
*
|
2006-12-04 15:36:51 +00:00
|
|
|
* @v bus SPI bus
|
2006-06-14 00:22:50 +00:00
|
|
|
* @v data_out TX data buffer (or NULL)
|
|
|
|
* @v data_in RX data buffer (or NULL)
|
|
|
|
* @v len Length of transfer (in @b bits)
|
2007-07-01 14:22:12 +00:00
|
|
|
* @v endianness Endianness of this data transfer
|
2006-06-14 00:22:50 +00:00
|
|
|
*
|
|
|
|
* This issues @c len clock cycles on the SPI bus, shifting out data
|
|
|
|
* from the @c data_out buffer to the MOSI line and shifting in data
|
|
|
|
* from the MISO line to the @c data_in buffer. If @c data_out is
|
|
|
|
* NULL, then the data sent will be all zeroes. If @c data_in is
|
|
|
|
* NULL, then the incoming data will be discarded.
|
|
|
|
*/
|
2006-12-04 15:36:51 +00:00
|
|
|
static void spi_bit_transfer ( struct spi_bit_basher *spibit,
|
|
|
|
const void *data_out, void *data_in,
|
2007-07-01 14:22:12 +00:00
|
|
|
unsigned int len, int endianness ) {
|
2006-12-04 15:36:51 +00:00
|
|
|
struct spi_bus *bus = &spibit->bus;
|
2006-06-14 00:22:50 +00:00
|
|
|
struct bit_basher *basher = &spibit->basher;
|
2006-12-04 15:36:51 +00:00
|
|
|
unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
|
|
|
|
unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
|
2007-07-01 14:22:12 +00:00
|
|
|
unsigned int bit_offset;
|
|
|
|
unsigned int byte_offset;
|
|
|
|
unsigned int byte_mask;
|
2006-06-14 00:22:50 +00:00
|
|
|
unsigned int bit;
|
2007-07-01 14:22:12 +00:00
|
|
|
unsigned int step;
|
2006-06-14 00:22:50 +00:00
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
|
2006-06-14 00:22:50 +00:00
|
|
|
|
2007-07-01 14:22:12 +00:00
|
|
|
for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
|
|
|
|
/* Calculate byte offset and byte mask */
|
|
|
|
bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
|
|
|
|
( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
|
|
|
|
byte_offset = ( bit_offset / 8 );
|
|
|
|
byte_mask = ( 1 << ( bit_offset % 8 ) );
|
|
|
|
|
2006-06-14 00:22:50 +00:00
|
|
|
/* Shift data in or out */
|
|
|
|
if ( sclk == cpha ) {
|
|
|
|
const uint8_t *byte;
|
|
|
|
|
|
|
|
/* Shift data out */
|
|
|
|
if ( data_out ) {
|
2007-07-01 14:22:12 +00:00
|
|
|
byte = ( data_out + byte_offset );
|
|
|
|
bit = ( *byte & byte_mask );
|
2006-06-14 00:22:50 +00:00
|
|
|
} else {
|
|
|
|
bit = 0;
|
|
|
|
}
|
|
|
|
write_bit ( basher, SPI_BIT_MOSI, bit );
|
|
|
|
} else {
|
|
|
|
uint8_t *byte;
|
|
|
|
|
|
|
|
/* Shift data in */
|
|
|
|
bit = read_bit ( basher, SPI_BIT_MISO );
|
|
|
|
if ( data_in ) {
|
2007-07-01 14:22:12 +00:00
|
|
|
byte = ( data_in + byte_offset );
|
|
|
|
*byte &= ~byte_mask;
|
|
|
|
*byte |= ( bit & byte_mask );
|
2006-06-14 00:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Toggle clock line */
|
2006-12-04 15:36:51 +00:00
|
|
|
spi_bit_delay();
|
2006-06-14 00:22:50 +00:00
|
|
|
sclk = ~sclk;
|
|
|
|
write_bit ( basher, SPI_BIT_SCLK, sclk );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
/**
|
|
|
|
* Read/write data via SPI bit-bashing bus
|
|
|
|
*
|
|
|
|
* @v bus SPI bus
|
|
|
|
* @v device SPI device
|
|
|
|
* @v command Command
|
|
|
|
* @v address Address to read/write (<0 for no address)
|
|
|
|
* @v data_out TX data buffer (or NULL)
|
|
|
|
* @v data_in RX data buffer (or NULL)
|
2006-12-04 16:09:59 +00:00
|
|
|
* @v len Length of transfer
|
2006-12-04 15:36:51 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
|
|
|
|
unsigned int command, int address,
|
2006-12-04 16:09:59 +00:00
|
|
|
const void *data_out, void *data_in, size_t len ) {
|
2006-12-04 15:36:51 +00:00
|
|
|
struct spi_bit_basher *spibit
|
|
|
|
= container_of ( bus, struct spi_bit_basher, bus );
|
|
|
|
uint32_t tmp;
|
|
|
|
|
2007-07-28 15:05:41 +00:00
|
|
|
/* Set clock line to idle state */
|
|
|
|
write_bit ( &spibit->basher, SPI_BIT_SCLK,
|
|
|
|
( bus->mode & SPI_MODE_CPOL ) );
|
|
|
|
|
2006-12-04 15:36:51 +00:00
|
|
|
/* Assert chip select on specified slave */
|
|
|
|
spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
|
|
|
|
|
|
|
|
/* Transmit command */
|
2006-12-04 18:23:06 +00:00
|
|
|
assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
|
2006-12-04 15:36:51 +00:00
|
|
|
tmp = cpu_to_le32 ( command );
|
2007-07-01 14:22:12 +00:00
|
|
|
spi_bit_transfer ( spibit, &tmp, NULL, device->command_len,
|
|
|
|
SPI_BIT_BIG_ENDIAN );
|
2006-12-04 15:36:51 +00:00
|
|
|
|
|
|
|
/* Transmit address, if present */
|
|
|
|
if ( address >= 0 ) {
|
2006-12-04 18:23:06 +00:00
|
|
|
assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
|
2006-12-04 15:36:51 +00:00
|
|
|
tmp = cpu_to_le32 ( address );
|
2007-07-01 14:22:12 +00:00
|
|
|
spi_bit_transfer ( spibit, &tmp, NULL, device->address_len,
|
|
|
|
SPI_BIT_BIG_ENDIAN );
|
2006-12-04 15:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmit/receive data */
|
2007-07-01 14:22:12 +00:00
|
|
|
spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
|
|
|
|
spibit->endianness );
|
2006-12-04 15:36:51 +00:00
|
|
|
|
|
|
|
/* Deassert chip select on specified slave */
|
|
|
|
spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-14 00:22:50 +00:00
|
|
|
/**
|
|
|
|
* Initialise SPI bit-bashing interface
|
|
|
|
*
|
|
|
|
* @v spibit SPI bit-bashing interface
|
|
|
|
*/
|
|
|
|
void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
|
2006-12-04 15:36:51 +00:00
|
|
|
assert ( &spibit->basher.op->read != NULL );
|
|
|
|
assert ( &spibit->basher.op->write != NULL );
|
|
|
|
spibit->bus.rw = spi_bit_rw;
|
2006-06-14 00:22:50 +00:00
|
|
|
}
|