2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-14 07:11:32 +00:00

Generalise three-wire interface to generic SPI interface.

Update rtl8139 driver to instantiate an SPI interface with a three-wire
device attached.
This commit is contained in:
Michael Brown 2006-06-14 00:22:50 +00:00
parent ab4f96e525
commit 3b51c719d3
6 changed files with 427 additions and 182 deletions

View File

@ -0,0 +1,165 @@
/*
* 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>
#include <errno.h>
#include <assert.h>
#include <timer.h>
#include <gpxe/bitbash.h>
#include <gpxe/spi.h>
/** @file
*
* SPI bit-bashing interface
*
*/
/** Delay between SCLK changes and around SS changes */
static void spi_delay ( void ) {
udelay ( SPI_UDELAY );
}
/**
* Select/deselect slave
*
* @v spi SPI bit-bashing interface
* @v slave Slave number
* @v state Slave select state
*
* @c state must be set to zero to select the specified slave, or to
* @c SPI_MODE_SSPOL to deselect the slave.
*/
static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
unsigned int slave,
unsigned int state ) {
struct bit_basher *basher = &spibit->basher;
state ^= ( spibit->spi.mode & SPI_MODE_SSPOL );
DBG ( "Setting slave %d select %s\n", slave,
( state ? "high" : "low" ) );
spi_delay();
write_bit ( basher, SPI_BIT_SS ( slave ), state );
spi_delay();
}
/**
* Select slave
*
* @v spi SPI interface
* @v slave Slave number
*/
static void spi_bit_select_slave ( struct spi_interface *spi,
unsigned int slave ) {
struct spi_bit_basher *spibit
= container_of ( spi, struct spi_bit_basher, spi );
spibit->slave = slave;
spi_bit_set_slave_select ( spibit, slave, 0 );
}
/**
* Deselect slave
*
* @v spi SPI interface
*/
static void spi_bit_deselect_slave ( struct spi_interface *spi ) {
struct spi_bit_basher *spibit
= container_of ( spi, struct spi_bit_basher, spi );
spi_bit_set_slave_select ( spibit, spibit->slave, SPI_MODE_SSPOL );
}
/**
* Transfer bits over SPI bit-bashing interface
*
* @v spi SPI interface
* @v data_out TX data buffer (or NULL)
* @v data_in RX data buffer (or NULL)
* @v len Length of transfer (in @b bits)
*
* 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.
*/
static void spi_bit_transfer ( struct spi_interface *spi, const void *data_out,
void *data_in, unsigned int len ) {
struct spi_bit_basher *spibit
= container_of ( spi, struct spi_bit_basher, spi );
struct bit_basher *basher = &spibit->basher;
unsigned int sclk = ( ( spi->mode & SPI_MODE_CPOL ) ? 1 : 0 );
unsigned int cpha = ( ( spi->mode & SPI_MODE_CPHA ) ? 1 : 0 );
unsigned int offset;
unsigned int mask;
unsigned int bit;
int step;
DBG ( "Transferring %d bits in mode %x\n", len, spi->mode );
for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
/* Calculate byte offset within data and bit mask */
offset = ( step / 16 );
mask = ( 1 << ( ( step % 16 ) / 2 ) );
/* Shift data in or out */
if ( sclk == cpha ) {
const uint8_t *byte;
/* Shift data out */
if ( data_out ) {
byte = ( data_out + offset );
bit = ( *byte & mask );
} 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 ) {
byte = ( data_in + offset );
*byte &= ~mask;
*byte |= ( bit & mask );
}
}
/* Toggle clock line */
spi_delay();
sclk = ~sclk;
write_bit ( basher, SPI_BIT_SCLK, sclk );
}
}
/**
* Initialise SPI bit-bashing interface
*
* @v spibit SPI bit-bashing interface
*/
void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
assert ( &spibit->basher.read != NULL );
assert ( &spibit->basher.write != NULL );
spibit->spi.select_slave = spi_bit_select_slave;
spibit->spi.deselect_slave = spi_bit_deselect_slave;
spibit->spi.transfer = spi_bit_transfer;
}

View File

@ -77,7 +77,8 @@
#include <gpxe/ethernet.h>
#include <gpxe/pkbuff.h>
#include <gpxe/netdevice.h>
#include <gpxe/nvs/threewire.h>
#include <gpxe/spi.h>
#include <gpxe/threewire.h>
#define TX_RING_SIZE 4
@ -92,10 +93,11 @@ struct rtl8139_rx {
};
struct rtl8139_nic {
struct threewire eeprom;
unsigned short ioaddr;
struct rtl8139_tx tx;
struct rtl8139_rx rx;
struct spi_bit_basher spibit;
struct threewire_device eeprom;
};
/* Tuning Parameters */
@ -192,8 +194,8 @@ enum RxConfigBits {
};
/* EEPROM access */
#define EE_MODE_PROGRAM 0x80
#define EE_MODE_NORMAL 0x00
#define EE_M1 0x80 /* Mode select bit 1 */
#define EE_M0 0x40 /* Mode select bit 0 */
#define EE_CS 0x08 /* EEPROM chip select */
#define EE_SK 0x04 /* EEPROM shift clock */
#define EE_DI 0x02 /* Data in */
@ -202,51 +204,40 @@ enum RxConfigBits {
/* Offsets within EEPROM (these are word offsets) */
#define EE_MAC 7
static inline struct rtl8139_nic * three_to_rtl ( struct threewire *three ) {
return container_of ( three, struct rtl8139_nic, eeprom );
static inline struct rtl8139_nic *
basher_to_rtl ( struct bit_basher *basher ) {
return container_of ( basher, struct rtl8139_nic, spibit.basher );
}
static void rtl_setcs ( struct threewire *three, int cs ) {
struct rtl8139_nic *rtl = three_to_rtl ( three );
unsigned int eereg;
eereg = cs ? ( EE_MODE_PROGRAM | EE_CS ) : ( EE_MODE_NORMAL );
outb ( eereg, rtl->ioaddr + Cfg9346 );
}
static void rtl_setsk ( struct threewire *three, int sk ) {
struct rtl8139_nic *rtl = three_to_rtl ( three );
unsigned int eereg;
eereg = inb ( rtl->ioaddr + Cfg9346 );
eereg = ( eereg & ~EE_SK ) | ( sk ? EE_SK : 0 );
outb ( eereg, rtl->ioaddr + Cfg9346 );
}
static void rtl_setdi ( struct threewire *three, int di ) {
struct rtl8139_nic *rtl = three_to_rtl ( three );
unsigned int eereg;
eereg = inb ( rtl->ioaddr + Cfg9346 );
eereg = ( eereg & ~EE_DI ) | ( di ? EE_DI : 0 );
outb ( eereg, rtl->ioaddr + Cfg9346 );
}
static int rtl_getdo ( struct threewire *three ) {
struct rtl8139_nic *rtl = three_to_rtl ( three );
unsigned int eereg;
eereg = ( inb ( rtl->ioaddr + Cfg9346 ) & EE_DO );
return eereg;
}
static struct threewire_operations rtl_three_ops = {
.setcs = rtl_setcs,
.setsk = rtl_setsk,
.setdi = rtl_setdi,
.getdo = rtl_getdo,
static const uint8_t rtl_ee_bits[] = {
[SPI_BIT_SCLK] = EE_SK,
[SPI_BIT_MOSI] = EE_DI,
[SPI_BIT_MISO] = EE_DO,
[SPI_BIT_SS(0)] = ( EE_CS | EE_M1 ),
};
static int rtl_spi_read_bit ( struct bit_basher *basher,
unsigned int bit_id ) {
struct rtl8139_nic *rtl = basher_to_rtl ( basher );
uint8_t mask = rtl_ee_bits[bit_id];
uint8_t eereg;
eereg = inb ( rtl->ioaddr + Cfg9346 );
return ( eereg & mask );
}
static void rtl_spi_write_bit ( struct bit_basher *basher,
unsigned int bit_id, unsigned long data ) {
struct rtl8139_nic *rtl = basher_to_rtl ( basher );
uint8_t mask = rtl_ee_bits[bit_id];
uint8_t eereg;
eereg = inb ( rtl->ioaddr + Cfg9346 );
eereg &= ~mask;
eereg |= ( data & mask );
outb ( eereg, rtl->ioaddr + Cfg9346 );
}
/**
* Set up for EEPROM access
*
@ -255,14 +246,20 @@ static struct threewire_operations rtl_three_ops = {
static void rtl_init_eeprom ( struct rtl8139_nic *rtl ) {
int ee9356;
/* Initialise three-wire bus */
rtl->spibit.basher.read = rtl_spi_read_bit;
rtl->spibit.basher.write = rtl_spi_write_bit;
rtl->spibit.spi.mode = SPI_MODE_THREEWIRE;
init_spi_bit_basher ( &rtl->spibit );
/* Detect EEPROM type and initialise three-wire device */
ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
DBG ( "EEPROM is an %s\n", ee9356 ? "AT93C56" : "AT93C46" );
rtl->eeprom.ops = &rtl_three_ops;
rtl->eeprom.adrsize =
( ee9356 ? AT93C56_ORG16_ADRSIZE : AT93C46_ORG16_ADRSIZE );
rtl->eeprom.datasize =
( ee9356 ? AT93C56_ORG16_DATASIZE : AT93C46_ORG16_DATASIZE );
rtl->eeprom.udelay = 1;
rtl->eeprom.spi = &rtl->spibit.spi;
}
/**
@ -279,9 +276,8 @@ static void rtl_read_mac ( struct rtl8139_nic *rtl, uint8_t *mac_addr ) {
int i;
DBG ( "MAC address is " );
for ( i = 0 ; i < ( ETH_ALEN / 2 ) ; i++ ) {
u.word = cpu_to_le16 ( threewire_read ( &rtl->eeprom,
EE_MAC + i ) );
for ( i = EE_MAC ; i < ( EE_MAC + ( ETH_ALEN / 2 ) ) ; i++ ) {
u.word = cpu_to_le16 ( threewire_read ( &rtl->eeprom, i ) );
*mac_addr++ = u.bytes[0];
*mac_addr++ = u.bytes[1];
DBG ( "%02x%02x", u.bytes[0], u.bytes[1] );

View File

@ -16,53 +16,42 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <timer.h>
#include <gpxe/nvs/threewire.h>
#include <stddef.h>
#include <byteswap.h>
#include <gpxe/spi.h>
#include <gpxe/threewire.h>
/** @file
*
* Three-wire serial interface
* Three-wire serial devices
*
*/
/**
* Read from a three-wire device
*
* @v three Three-wire interface
* @v three Three-wire device
* @v address Address
* @ret data Data
*/
unsigned long threewire_read ( struct threewire *three,
unsigned long threewire_read ( struct threewire_device *three,
unsigned long address ) {
struct threewire_operations *ops = three->ops;
unsigned long command;
unsigned long data;
int i;
struct spi_interface *spi = three->spi;
uint32_t data;
/* Activate chip select line */
spi->select_slave ( spi, three->slave );
ops->setcs ( three, 1 );
/* Send command and address */
command = threewire_cmd_read ( three, address );
for ( i = ( threewire_cmd_len ( three ) - 1 ) ; i >= 0 ; i-- ) {
ops->setdi ( three, ( command >> i ) & 0x1 );
udelay ( three->udelay );
ops->setsk ( three, 1 );
udelay ( three->udelay );
ops->setsk ( three, 0 );
}
data = cpu_to_le32 ( threewire_cmd_read ( three, address ) );
spi->transfer ( spi, &data, NULL, threewire_cmd_len ( three ) );
/* Read back data */
data = 0;
for ( i = three->datasize ; i ; i-- ) {
udelay ( three->udelay );
ops->setsk ( three, 1 );
udelay ( three->udelay );
data <<= 1;
data |= ops->getdo ( three );
ops->setsk ( three, 0 );
}
spi->transfer ( spi, NULL, &data, three->datasize );
ops->setcs ( three, 0 );
/* Deactivate chip select line */
spi->deselect_slave ( spi );
return data;
return le32_to_cpu ( data );;
}

View File

@ -1,102 +0,0 @@
#ifndef _GPXE_NVS_THREEWIRE_H
#define _GPXE_NVS_THREEWIRE_H
/** @file
*
* Three-wire serial interface
*
*/
struct threewire;
/** Three-wire interface methods */
struct threewire_operations {
/**
* Set status of Chip Select line
*
* @v three Three-wire interface
* @v cs New status for chip select line
*/
void ( * setcs ) ( struct threewire *three, int cs );
/**
* Set status of Serial Clock line
*
* @v three Three-wire interface
* @v sk New status for serial clock line
*/
void ( * setsk ) ( struct threewire *three, int sk );
/**
* Set status of Data Input line
*
* @v three Three-wire interface
* @v di New status for data input line
*/
void ( * setdi ) ( struct threewire *three, int di );
/**
* Get status of Data Output line
*
* @v three Three-wire interface
* @ret do Status of data output line
*/
int ( * getdo ) ( struct threewire *three );
};
/**
* A three-wire serial interface
*
* This interface consists of a clock line (SK), data input (DI) and
* data output (DO). There is also a chip select line (CS) which is
* integral to the operation of the device, but Atmel still calls it a
* three-wire interface.
*
*/
struct threewire {
/** Interface methods */
struct threewire_operations *ops;
/** Address size (in bits) */
unsigned int adrsize;
/** Data size (in bits) */
unsigned int datasize;
/** Delay between SK transitions (in us) */
unsigned int udelay;
};
/**
* Calculate read command for a specified address
*
* @v three Three-wire interface
* @v address Address
* @ret cmd Command
*/
static inline __attribute__ (( always_inline )) unsigned long
threewire_cmd_read ( struct threewire *three, unsigned long address ) {
return ( ( 0x6 << three->adrsize ) | address );
}
/**
* Calculate command length
*
* @v three Three-wire interface
* @ret len Command length, in bits
*/
static inline __attribute__ (( always_inline )) int
threewire_cmd_len ( struct threewire *three ) {
return ( three->adrsize + 3 );
}
/* Constants for some standard parts */
#define AT93C46_ORG8_ADRSIZE 7
#define AT93C46_ORG8_DATASIZE 8
#define AT93C46_ORG16_ADRSIZE 6
#define AT93C46_ORG16_DATASIZE 16
#define AT93C46_UDELAY 1
#define AT93C56_ORG8_ADRSIZE 9
#define AT93C56_ORG8_DATASIZE 8
#define AT93C56_ORG16_ADRSIZE 8
#define AT93C56_ORG16_DATASIZE 16
#define AT93C56_UDELAY 1
extern unsigned long threewire_read ( struct threewire *three,
unsigned long address );
#endif /* _GPXE_NVS_THREEWIRE_H */

132
src/include/gpxe/spi.h Normal file
View File

@ -0,0 +1,132 @@
#ifndef _GPXE_SPI_H
#define _GPXE_SPI_H
/** @file
*
* SPI interface
*
*/
#include <gpxe/bitbash.h>
/** An SPI interface */
struct spi_interface {
/** SPI interface mode
*
* This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
* and @c SPI_MODE_CPOL. It is also the number conventionally
* used to describe the SPI interface mode. For example, SPI
* mode 1 is the mode in which CPOL=0 and CPHA=1, which
* therefore corresponds to a mode value of (0|SPI_MODE_CPHA)
* which, happily, equals 1.
*/
unsigned int mode;
/**
* Select slave
*
* @v spi SPI interface
* @v slave Slave number
*/
void ( * select_slave ) ( struct spi_interface *spi,
unsigned int slave );
/**
* Deselect slave
*
* @v spi SPI interface
*/
void ( * deselect_slave ) ( struct spi_interface *spi );
/**
* Transfer bits over SPI bit-bashing interface
*
* @v spi SPI interface
* @v data_out TX data buffer (or NULL)
* @v data_in RX data buffer (or NULL)
* @v len Length of transfer (in @b bits)
*/
void ( * transfer ) ( struct spi_interface *spi, const void *data_out,
void *data_in, unsigned int len );
};
/** Clock phase (CPHA) mode bit
*
* Phase 0 is sample on rising edge, shift data on falling edge.
*
* Phase 1 is shift data on rising edge, sample data on falling edge.
*/
#define SPI_MODE_CPHA 0x01
/** Clock polarity (CPOL) mode bit
*
* This bit reflects the idle state of the clock line (SCLK).
*/
#define SPI_MODE_CPOL 0x02
/** Slave select polarity mode bit
*
* This bit reflects that active state of the slave select lines. It
* is not part of the normal SPI mode number (which covers only @c
* SPI_MODE_CPOL and @c SPI_MODE_CPHA), but is included here for
* convenience.
*/
#define SPI_MODE_SSPOL 0x10
/** Microwire-compatible mode
*
* This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with
* the original Microwire protocol.
*/
#define SPI_MODE_MICROWIRE 1
/** Microwire/Plus-compatible mode
*
* This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with
* the Microwire/Plus protocol
*/
#define SPI_MODE_MICROWIRE_PLUS 0
/** Threewire-compatible mode
*
* This mode is compatible with Atmel's series of "three-wire"
* interfaces.
*/
#define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
/** A bit-bashing SPI interface */
struct spi_bit_basher {
/** SPI interface */
struct spi_interface spi;
/** Bit-bashing interface */
struct bit_basher basher;
/** Currently selected slave
*
* Valid only when a slave is actually selected.
*/
unsigned int slave;
};
/** Bit indices used for SPI bit-bashing interface */
enum {
/** Serial clock */
SPI_BIT_SCLK = 0,
/** Master Out Slave In */
SPI_BIT_MOSI,
/** Master In Slave Out */
SPI_BIT_MISO,
/** Slave 0 select */
SPI_BIT_SS0,
};
/**
* Determine bit index for a particular slave
*
* @v slave Slave number
* @ret index Bit index (i.e. SPI_BIT_SSN, where N=slave)
*/
#define SPI_BIT_SS( slave ) ( SPI_BIT_SS0 + (slave) )
/** Delay between SCLK transitions */
#define SPI_UDELAY 1
extern void init_spi_bit_basher ( struct spi_bit_basher *spibit );
#endif /* _GPXE_SPI_H */

View File

@ -0,0 +1,65 @@
#ifndef _GPXE_THREEWIRE_H
#define _GPXE_THREEWIRE_H
/** @file
*
* Three-wire serial interface
*
* The Atmel three-wire interface is a subset of the (newer) SPI
* interface, and is implemented here as a layer on top of the SPI
* support.
*/
struct spi_interface;
/** A three-wire device */
struct threewire_device {
/** SPI interface to which device is attached */
struct spi_interface *spi;
/** SPI slave number */
unsigned int slave;
/** Address size (in bits) */
unsigned int adrsize;
/** Data size (in bits) */
unsigned int datasize;
};
/**
* Calculate read command for a specified address
*
* @v three Three-wire interface
* @v address Address
* @ret cmd Command
*/
static inline __attribute__ (( always_inline )) unsigned long
threewire_cmd_read ( struct threewire_device *three, unsigned long address ) {
return ( ( 0x6 << three->adrsize ) | address );
}
/**
* Calculate command length
*
* @v three Three-wire interface
* @ret len Command length, in bits
*/
static inline __attribute__ (( always_inline )) unsigned int
threewire_cmd_len ( struct threewire_device *three ) {
return ( three->adrsize + 3 );
}
/* Constants for some standard parts */
#define AT93C46_ORG8_ADRSIZE 7
#define AT93C46_ORG8_DATASIZE 8
#define AT93C46_ORG16_ADRSIZE 6
#define AT93C46_ORG16_DATASIZE 16
#define AT93C46_UDELAY 1
#define AT93C56_ORG8_ADRSIZE 9
#define AT93C56_ORG8_DATASIZE 8
#define AT93C56_ORG16_ADRSIZE 8
#define AT93C56_ORG16_DATASIZE 16
#define AT93C56_UDELAY 1
extern unsigned long threewire_read ( struct threewire_device *three,
unsigned long address );
#endif /* _GPXE_THREEWIRE_H */