mirror of
https://github.com/ipxe/ipxe
synced 2026-04-04 03:00:20 +03: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:
165
src/drivers/bitbash/spi_bit.c
Normal file
165
src/drivers/bitbash/spi_bit.c
Normal 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;
|
||||
}
|
||||
@@ -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] );
|
||||
|
||||
@@ -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 );;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user