Browse Source

Generalise three-wire interface to generic SPI interface.

Update rtl8139 driver to instantiate an SPI interface with a three-wire
device attached.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
3b51c719d3

+ 165
- 0
src/drivers/bitbash/spi_bit.c View File

@@ -0,0 +1,165 @@
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stddef.h>
20
+#include <stdint.h>
21
+#include <string.h>
22
+#include <errno.h>
23
+#include <assert.h>
24
+#include <timer.h>
25
+#include <gpxe/bitbash.h>
26
+#include <gpxe/spi.h>
27
+
28
+/** @file
29
+ *
30
+ * SPI bit-bashing interface
31
+ *
32
+ */
33
+
34
+/** Delay between SCLK changes and around SS changes */
35
+static void spi_delay ( void ) {
36
+	udelay ( SPI_UDELAY );
37
+}
38
+
39
+/**
40
+ * Select/deselect slave
41
+ *
42
+ * @v spi		SPI bit-bashing interface
43
+ * @v slave		Slave number
44
+ * @v state		Slave select state
45
+ *
46
+ * @c state must be set to zero to select the specified slave, or to
47
+ * @c SPI_MODE_SSPOL to deselect the slave.
48
+ */
49
+static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
50
+				       unsigned int slave,
51
+				       unsigned int state ) {
52
+	struct bit_basher *basher = &spibit->basher;
53
+
54
+	state ^= ( spibit->spi.mode & SPI_MODE_SSPOL );
55
+	DBG ( "Setting slave %d select %s\n", slave,
56
+	      ( state ? "high" : "low" ) );
57
+
58
+	spi_delay();
59
+	write_bit ( basher, SPI_BIT_SS ( slave ), state );
60
+	spi_delay();
61
+}
62
+
63
+/**
64
+ * Select slave
65
+ *
66
+ * @v spi		SPI interface
67
+ * @v slave		Slave number
68
+ */
69
+static void spi_bit_select_slave ( struct spi_interface *spi,
70
+				   unsigned int slave ) {
71
+	struct spi_bit_basher *spibit
72
+		= container_of ( spi, struct spi_bit_basher, spi );
73
+
74
+	spibit->slave = slave;
75
+	spi_bit_set_slave_select ( spibit, slave, 0 );
76
+}
77
+
78
+/**
79
+ * Deselect slave
80
+ *
81
+ * @v spi		SPI interface
82
+ */
83
+static void spi_bit_deselect_slave ( struct spi_interface *spi ) {
84
+	struct spi_bit_basher *spibit
85
+		= container_of ( spi, struct spi_bit_basher, spi );
86
+
87
+	spi_bit_set_slave_select ( spibit, spibit->slave, SPI_MODE_SSPOL );
88
+}
89
+
90
+/**
91
+ * Transfer bits over SPI bit-bashing interface
92
+ *
93
+ * @v spi		SPI interface
94
+ * @v data_out		TX data buffer (or NULL)
95
+ * @v data_in		RX data buffer (or NULL)
96
+ * @v len		Length of transfer (in @b bits)
97
+ *
98
+ * This issues @c len clock cycles on the SPI bus, shifting out data
99
+ * from the @c data_out buffer to the MOSI line and shifting in data
100
+ * from the MISO line to the @c data_in buffer.  If @c data_out is
101
+ * NULL, then the data sent will be all zeroes.  If @c data_in is
102
+ * NULL, then the incoming data will be discarded.
103
+ */
104
+static void spi_bit_transfer ( struct spi_interface *spi, const void *data_out,
105
+			       void *data_in, unsigned int len ) {
106
+	struct spi_bit_basher *spibit
107
+		= container_of ( spi, struct spi_bit_basher, spi );
108
+	struct bit_basher *basher = &spibit->basher;
109
+	unsigned int sclk = ( ( spi->mode & SPI_MODE_CPOL ) ? 1 : 0 );
110
+	unsigned int cpha = ( ( spi->mode & SPI_MODE_CPHA ) ? 1 : 0 );
111
+	unsigned int offset;
112
+	unsigned int mask;
113
+	unsigned int bit;
114
+	int step;
115
+
116
+	DBG ( "Transferring %d bits in mode %x\n", len, spi->mode );
117
+
118
+	for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
119
+		/* Calculate byte offset within data and bit mask */
120
+		offset = ( step / 16 );
121
+		mask = ( 1 << ( ( step % 16 ) / 2 ) );
122
+		
123
+		/* Shift data in or out */
124
+		if ( sclk == cpha ) {
125
+			const uint8_t *byte;
126
+
127
+			/* Shift data out */
128
+			if ( data_out ) {
129
+				byte = ( data_out + offset );
130
+				bit = ( *byte & mask );
131
+			} else {
132
+				bit = 0;
133
+			}
134
+			write_bit ( basher, SPI_BIT_MOSI, bit );
135
+		} else {
136
+			uint8_t *byte;
137
+
138
+			/* Shift data in */
139
+			bit = read_bit ( basher, SPI_BIT_MISO );
140
+			if ( data_in ) {
141
+				byte = ( data_in + offset );
142
+				*byte &= ~mask;
143
+				*byte |= ( bit & mask );
144
+			}
145
+		}
146
+
147
+		/* Toggle clock line */
148
+		spi_delay();
149
+		sclk = ~sclk;
150
+		write_bit ( basher, SPI_BIT_SCLK, sclk );
151
+	}
152
+}
153
+
154
+/**
155
+ * Initialise SPI bit-bashing interface
156
+ *
157
+ * @v spibit		SPI bit-bashing interface
158
+ */
159
+void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
160
+	assert ( &spibit->basher.read != NULL );
161
+	assert ( &spibit->basher.write != NULL );
162
+	spibit->spi.select_slave = spi_bit_select_slave;
163
+	spibit->spi.deselect_slave = spi_bit_deselect_slave;
164
+	spibit->spi.transfer = spi_bit_transfer;
165
+}

+ 38
- 42
src/drivers/net/rtl8139.c View File

@@ -77,7 +77,8 @@
77 77
 #include <gpxe/ethernet.h>
78 78
 #include <gpxe/pkbuff.h>
79 79
 #include <gpxe/netdevice.h>
80
-#include <gpxe/nvs/threewire.h>
80
+#include <gpxe/spi.h>
81
+#include <gpxe/threewire.h>
81 82
 
82 83
 #define TX_RING_SIZE 4
83 84
 
@@ -92,10 +93,11 @@ struct rtl8139_rx {
92 93
 };
93 94
 
94 95
 struct rtl8139_nic {
95
-	struct threewire eeprom;
96 96
 	unsigned short ioaddr;
97 97
 	struct rtl8139_tx tx;
98 98
 	struct rtl8139_rx rx;
99
+	struct spi_bit_basher spibit;
100
+	struct threewire_device eeprom;
99 101
 };
100 102
 
101 103
 /* Tuning Parameters */
@@ -192,8 +194,8 @@ enum RxConfigBits {
192 194
 };
193 195
 
194 196
 /*  EEPROM access */
195
-#define EE_MODE_PROGRAM	0x80
196
-#define EE_MODE_NORMAL	0x00
197
+#define EE_M1		0x80	/* Mode select bit 1 */
198
+#define EE_M0		0x40	/* Mode select bit 0 */
197 199
 #define EE_CS		0x08	/* EEPROM chip select */
198 200
 #define EE_SK		0x04	/* EEPROM shift clock */
199 201
 #define EE_DI		0x02	/* Data in */
@@ -202,51 +204,40 @@ enum RxConfigBits {
202 204
 /* Offsets within EEPROM (these are word offsets) */
203 205
 #define EE_MAC 7
204 206
 
205
-static inline struct rtl8139_nic * three_to_rtl ( struct threewire *three ) {
206
-	return container_of ( three, struct rtl8139_nic, eeprom );
207
+static inline struct rtl8139_nic *
208
+basher_to_rtl ( struct bit_basher *basher ) {
209
+	return container_of ( basher, struct rtl8139_nic, spibit.basher );
207 210
 }
208 211
 
209
-static void rtl_setcs ( struct threewire *three, int cs ) {
210
-	struct rtl8139_nic *rtl = three_to_rtl ( three );
211
-	unsigned int eereg;
212
-
213
-	eereg = cs ? ( EE_MODE_PROGRAM | EE_CS ) : ( EE_MODE_NORMAL );
214
-	outb ( eereg, rtl->ioaddr + Cfg9346 );
215
-}
212
+static const uint8_t rtl_ee_bits[] = {
213
+	[SPI_BIT_SCLK]	= EE_SK,
214
+	[SPI_BIT_MOSI]	= EE_DI,
215
+	[SPI_BIT_MISO]	= EE_DO,
216
+	[SPI_BIT_SS(0)]	= ( EE_CS | EE_M1 ),
217
+};
216 218
 
217
-static void rtl_setsk ( struct threewire *three, int sk ) {
218
-	struct rtl8139_nic *rtl = three_to_rtl ( three );
219
-	unsigned int eereg;
219
+static int rtl_spi_read_bit ( struct bit_basher *basher,
220
+			      unsigned int bit_id ) {
221
+	struct rtl8139_nic *rtl = basher_to_rtl ( basher );
222
+	uint8_t mask = rtl_ee_bits[bit_id];
223
+	uint8_t eereg;
220 224
 
221 225
 	eereg = inb ( rtl->ioaddr + Cfg9346 );
222
-	eereg = ( eereg & ~EE_SK ) | ( sk ? EE_SK : 0 );
223
-	outb ( eereg, rtl->ioaddr + Cfg9346 );
226
+	return ( eereg & mask );
224 227
 }
225 228
 
226
-static void rtl_setdi ( struct threewire *three, int di ) {
227
-	struct rtl8139_nic *rtl = three_to_rtl ( three );
228
-	unsigned int eereg;
229
+static void rtl_spi_write_bit ( struct bit_basher *basher,
230
+				unsigned int bit_id, unsigned long data ) {
231
+	struct rtl8139_nic *rtl = basher_to_rtl ( basher );
232
+	uint8_t mask = rtl_ee_bits[bit_id];
233
+	uint8_t eereg;
229 234
 
230 235
 	eereg = inb ( rtl->ioaddr + Cfg9346 );
231
-	eereg = ( eereg & ~EE_DI ) | ( di ? EE_DI : 0 );
236
+	eereg &= ~mask;
237
+	eereg |= ( data & mask );
232 238
 	outb ( eereg, rtl->ioaddr + Cfg9346 );
233 239
 }
234 240
 
235
-static int rtl_getdo ( struct threewire *three ) {
236
-	struct rtl8139_nic *rtl = three_to_rtl ( three );
237
-	unsigned int eereg;
238
-
239
-	eereg = ( inb ( rtl->ioaddr + Cfg9346 ) & EE_DO );
240
-	return eereg;
241
-}
242
-
243
-static struct threewire_operations rtl_three_ops = {
244
-	.setcs = rtl_setcs,
245
-	.setsk = rtl_setsk,
246
-	.setdi = rtl_setdi,
247
-	.getdo = rtl_getdo,
248
-};
249
-
250 241
 /**
251 242
  * Set up for EEPROM access
252 243
  *
@@ -255,14 +246,20 @@ static struct threewire_operations rtl_three_ops = {
255 246
 static void rtl_init_eeprom ( struct rtl8139_nic *rtl ) {
256 247
 	int ee9356;
257 248
 
249
+	/* Initialise three-wire bus */
250
+	rtl->spibit.basher.read = rtl_spi_read_bit;
251
+	rtl->spibit.basher.write = rtl_spi_write_bit;
252
+	rtl->spibit.spi.mode = SPI_MODE_THREEWIRE;
253
+	init_spi_bit_basher ( &rtl->spibit );
254
+
255
+	/* Detect EEPROM type and initialise three-wire device */
258 256
 	ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
259 257
 	DBG ( "EEPROM is an %s\n", ee9356 ? "AT93C56" : "AT93C46" );
260
-	rtl->eeprom.ops = &rtl_three_ops;
261 258
 	rtl->eeprom.adrsize =
262 259
 		( ee9356 ? AT93C56_ORG16_ADRSIZE : AT93C46_ORG16_ADRSIZE );
263 260
 	rtl->eeprom.datasize =
264 261
 		( ee9356 ? AT93C56_ORG16_DATASIZE : AT93C46_ORG16_DATASIZE );
265
-	rtl->eeprom.udelay = 1;
262
+	rtl->eeprom.spi = &rtl->spibit.spi;
266 263
 }
267 264
 
268 265
 /**
@@ -279,9 +276,8 @@ static void rtl_read_mac ( struct rtl8139_nic *rtl, uint8_t *mac_addr ) {
279 276
 	int i;
280 277
 	
281 278
 	DBG ( "MAC address is " );
282
-	for ( i = 0 ; i < ( ETH_ALEN / 2 ) ; i++ ) {
283
-		u.word = cpu_to_le16 ( threewire_read ( &rtl->eeprom,
284
-							EE_MAC + i ) );
279
+	for ( i = EE_MAC ; i < ( EE_MAC + ( ETH_ALEN / 2 ) ) ; i++ ) {
280
+		u.word = cpu_to_le16 ( threewire_read ( &rtl->eeprom, i ) );
285 281
 		*mac_addr++ = u.bytes[0];
286 282
 		*mac_addr++ = u.bytes[1];
287 283
 		DBG ( "%02x%02x", u.bytes[0], u.bytes[1] );

+ 19
- 30
src/drivers/nvs/threewire.c View File

@@ -16,53 +16,42 @@
16 16
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  */
18 18
 
19
-#include <timer.h>
20
-#include <gpxe/nvs/threewire.h>
19
+#include <stddef.h>
20
+#include <byteswap.h>
21
+#include <gpxe/spi.h>
22
+#include <gpxe/threewire.h>
21 23
 
22 24
 /** @file
23 25
  *
24
- * Three-wire serial interface
26
+ * Three-wire serial devices
25 27
  *
26 28
  */
27 29
 
28 30
 /**
29 31
  * Read from a three-wire device
30 32
  *
31
- * @v three	Three-wire interface
33
+ * @v three	Three-wire device
32 34
  * @v address	Address
33 35
  * @ret data	Data
34 36
  */
35
-unsigned long threewire_read ( struct threewire *three,
37
+unsigned long threewire_read ( struct threewire_device *three,
36 38
 			       unsigned long address ) {
37
-	struct threewire_operations *ops = three->ops;
38
-	unsigned long command;
39
-	unsigned long data;
40
-	int i;
39
+	struct spi_interface *spi = three->spi;
40
+	uint32_t data;
41 41
 
42
-	ops->setcs ( three, 1 );
43
-	
44
-	/* Send command and address */
45
-	command = threewire_cmd_read ( three, address );
46
-	for ( i = ( threewire_cmd_len ( three ) - 1 ) ; i >= 0 ; i-- ) {
47
-		ops->setdi ( three, ( command >> i ) & 0x1 );
48
-		udelay ( three->udelay );
49
-		ops->setsk ( three, 1 );
50
-		udelay ( three->udelay );
51
-		ops->setsk ( three, 0 );
52
-	}
42
+	/* Activate chip select line */
43
+	spi->select_slave ( spi, three->slave );
53 44
 
45
+	/* Send command and address */
46
+	data = cpu_to_le32 ( threewire_cmd_read ( three, address ) );
47
+	spi->transfer ( spi, &data, NULL, threewire_cmd_len ( three ) );
48
+	
54 49
 	/* Read back data */
55 50
 	data = 0;
56
-	for ( i = three->datasize ; i ; i-- ) {
57
-		udelay ( three->udelay );
58
-		ops->setsk ( three, 1 );
59
-		udelay ( three->udelay );
60
-		data <<= 1;
61
-		data |= ops->getdo ( three );
62
-		ops->setsk ( three, 0 );
63
-	}
51
+	spi->transfer ( spi, NULL, &data, three->datasize );
64 52
 
65
-	ops->setcs ( three, 0 );
53
+	/* Deactivate chip select line */
54
+	spi->deselect_slave ( spi );
66 55
 
67
-	return data;
56
+	return le32_to_cpu ( data );;
68 57
 }

+ 0
- 102
src/include/gpxe/nvs/threewire.h View File

@@ -1,102 +0,0 @@
1
-#ifndef _GPXE_NVS_THREEWIRE_H
2
-#define _GPXE_NVS_THREEWIRE_H
3
-
4
-/** @file
5
- *
6
- * Three-wire serial interface
7
- *
8
- */
9
-
10
-struct threewire;
11
-
12
-/** Three-wire interface methods */
13
-struct threewire_operations {
14
-	/**
15
-	 * Set status of Chip Select line
16
-	 *
17
-	 * @v three	Three-wire interface
18
-	 * @v cs	New status for chip select line
19
-	 */
20
-	void ( * setcs ) ( struct threewire *three, int cs );
21
-	/**
22
-	 * Set status of Serial Clock line
23
-	 *
24
-	 * @v three	Three-wire interface
25
-	 * @v sk	New status for serial clock line
26
-	 */
27
-	void ( * setsk ) ( struct threewire *three, int sk );
28
-	/**
29
-	 * Set status of Data Input line
30
-	 *
31
-	 * @v three	Three-wire interface
32
-	 * @v di	New status for data input line
33
-	 */
34
-	void ( * setdi ) ( struct threewire *three, int di );
35
-	/**
36
-	 * Get status of Data Output line
37
-	 *
38
-	 * @v three	Three-wire interface
39
-	 * @ret do	Status of data output line
40
-	 */
41
-	int ( * getdo ) ( struct threewire *three );
42
-};
43
-
44
-/**
45
- * A three-wire serial interface
46
- *
47
- * This interface consists of a clock line (SK), data input (DI) and
48
- * data output (DO).  There is also a chip select line (CS) which is
49
- * integral to the operation of the device, but Atmel still calls it a
50
- * three-wire interface.
51
- *
52
- */
53
-struct threewire {
54
-	/** Interface methods */
55
-	struct threewire_operations *ops;
56
-	/** Address size (in bits) */
57
-	unsigned int adrsize;
58
-	/** Data size (in bits) */
59
-	unsigned int datasize;
60
-	/** Delay between SK transitions (in us) */
61
-	unsigned int udelay;
62
-};
63
-
64
-/**
65
- * Calculate read command for a specified address
66
- *
67
- * @v three	Three-wire interface
68
- * @v address	Address
69
- * @ret cmd	Command
70
- */
71
-static inline __attribute__ (( always_inline )) unsigned long
72
-threewire_cmd_read ( struct threewire *three, unsigned long address ) {
73
-	return ( ( 0x6 << three->adrsize ) | address );
74
-}
75
-
76
-/**
77
- * Calculate command length
78
- *
79
- * @v three	Three-wire interface
80
- * @ret len	Command length, in bits
81
- */
82
-static inline __attribute__ (( always_inline )) int
83
-threewire_cmd_len ( struct threewire *three ) {
84
-	return ( three->adrsize + 3 );
85
-}
86
-
87
-/* Constants for some standard parts */
88
-#define AT93C46_ORG8_ADRSIZE	7
89
-#define AT93C46_ORG8_DATASIZE	8
90
-#define AT93C46_ORG16_ADRSIZE	6
91
-#define AT93C46_ORG16_DATASIZE	16
92
-#define AT93C46_UDELAY		1
93
-#define AT93C56_ORG8_ADRSIZE	9
94
-#define AT93C56_ORG8_DATASIZE	8
95
-#define AT93C56_ORG16_ADRSIZE	8
96
-#define AT93C56_ORG16_DATASIZE	16
97
-#define AT93C56_UDELAY		1
98
-
99
-extern unsigned long threewire_read ( struct threewire *three,
100
-				      unsigned long address );
101
-
102
-#endif /* _GPXE_NVS_THREEWIRE_H */

+ 132
- 0
src/include/gpxe/spi.h View File

@@ -0,0 +1,132 @@
1
+#ifndef _GPXE_SPI_H
2
+#define _GPXE_SPI_H
3
+
4
+/** @file
5
+ *
6
+ * SPI interface
7
+ *
8
+ */
9
+
10
+#include <gpxe/bitbash.h>
11
+
12
+/** An SPI interface */
13
+struct spi_interface {
14
+	/** SPI interface mode
15
+	 *
16
+	 * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
17
+	 * and @c SPI_MODE_CPOL.  It is also the number conventionally
18
+	 * used to describe the SPI interface mode.  For example, SPI
19
+	 * mode 1 is the mode in which CPOL=0 and CPHA=1, which
20
+	 * therefore corresponds to a mode value of (0|SPI_MODE_CPHA)
21
+	 * which, happily, equals 1.
22
+	 */
23
+	unsigned int mode;
24
+	/**
25
+	 * Select slave
26
+	 *
27
+	 * @v spi		SPI interface
28
+	 * @v slave		Slave number
29
+	 */
30
+	void ( * select_slave ) ( struct spi_interface *spi,
31
+				  unsigned int slave );
32
+	/**
33
+	 * Deselect slave
34
+	 *
35
+	 * @v spi		SPI interface
36
+	 */
37
+	void ( * deselect_slave ) ( struct spi_interface *spi );
38
+	/**
39
+	 * Transfer bits over SPI bit-bashing interface
40
+	 *
41
+	 * @v spi		SPI interface
42
+	 * @v data_out		TX data buffer (or NULL)
43
+	 * @v data_in		RX data buffer (or NULL)
44
+	 * @v len		Length of transfer (in @b bits)
45
+	 */
46
+	void ( * transfer ) ( struct spi_interface *spi, const void *data_out,
47
+			      void *data_in, unsigned int len );
48
+};
49
+
50
+/** Clock phase (CPHA) mode bit
51
+ *
52
+ * Phase 0 is sample on rising edge, shift data on falling edge.
53
+ *
54
+ * Phase 1 is shift data on rising edge, sample data on falling edge.
55
+ */
56
+#define SPI_MODE_CPHA 0x01
57
+
58
+/** Clock polarity (CPOL) mode bit
59
+ *
60
+ * This bit reflects the idle state of the clock line (SCLK).
61
+ */
62
+#define SPI_MODE_CPOL 0x02
63
+
64
+/** Slave select polarity mode bit
65
+ *
66
+ * This bit reflects that active state of the slave select lines.  It
67
+ * is not part of the normal SPI mode number (which covers only @c
68
+ * SPI_MODE_CPOL and @c SPI_MODE_CPHA), but is included here for
69
+ * convenience.
70
+ */
71
+#define SPI_MODE_SSPOL 0x10
72
+
73
+/** Microwire-compatible mode
74
+ *
75
+ * This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with
76
+ * the original Microwire protocol.
77
+ */
78
+#define SPI_MODE_MICROWIRE 1
79
+
80
+/** Microwire/Plus-compatible mode
81
+ *
82
+ * This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with
83
+ * the Microwire/Plus protocol
84
+ */
85
+#define SPI_MODE_MICROWIRE_PLUS 0
86
+
87
+/** Threewire-compatible mode
88
+ *
89
+ * This mode is compatible with Atmel's series of "three-wire"
90
+ * interfaces.
91
+ */
92
+#define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
93
+
94
+/** A bit-bashing SPI interface */
95
+struct spi_bit_basher {
96
+	/** SPI interface */
97
+	struct spi_interface spi;
98
+	/** Bit-bashing interface */
99
+	struct bit_basher basher;
100
+	/** Currently selected slave
101
+	 *
102
+	 * Valid only when a slave is actually selected.
103
+	 */
104
+	unsigned int slave;
105
+};
106
+
107
+/** Bit indices used for SPI bit-bashing interface */
108
+enum {
109
+	/** Serial clock */
110
+	SPI_BIT_SCLK = 0,
111
+	/** Master Out Slave In */
112
+	SPI_BIT_MOSI,
113
+	/** Master In Slave Out */
114
+	SPI_BIT_MISO,
115
+	/** Slave 0 select */
116
+	SPI_BIT_SS0,
117
+};
118
+
119
+/**
120
+ * Determine bit index for a particular slave
121
+ *
122
+ * @v slave		Slave number
123
+ * @ret index		Bit index (i.e. SPI_BIT_SSN, where N=slave) 
124
+ */
125
+#define SPI_BIT_SS( slave ) ( SPI_BIT_SS0 + (slave) )
126
+
127
+/** Delay between SCLK transitions */
128
+#define SPI_UDELAY 1
129
+
130
+extern void init_spi_bit_basher ( struct spi_bit_basher *spibit );
131
+
132
+#endif /* _GPXE_SPI_H */

+ 65
- 0
src/include/gpxe/threewire.h View File

@@ -0,0 +1,65 @@
1
+#ifndef _GPXE_THREEWIRE_H
2
+#define _GPXE_THREEWIRE_H
3
+
4
+/** @file
5
+ *
6
+ * Three-wire serial interface
7
+ *
8
+ * The Atmel three-wire interface is a subset of the (newer) SPI
9
+ * interface, and is implemented here as a layer on top of the SPI
10
+ * support.
11
+ */
12
+
13
+struct spi_interface;
14
+
15
+/** A three-wire device */
16
+struct threewire_device {
17
+	/** SPI interface to which device is attached */
18
+	struct spi_interface *spi;
19
+	/** SPI slave number */
20
+	unsigned int slave;
21
+	/** Address size (in bits) */
22
+	unsigned int adrsize;
23
+	/** Data size (in bits) */
24
+	unsigned int datasize;
25
+};
26
+
27
+/**
28
+ * Calculate read command for a specified address
29
+ *
30
+ * @v three	Three-wire interface
31
+ * @v address	Address
32
+ * @ret cmd	Command
33
+ */
34
+static inline __attribute__ (( always_inline )) unsigned long
35
+threewire_cmd_read ( struct threewire_device *three, unsigned long address ) {
36
+	return ( ( 0x6 << three->adrsize ) | address );
37
+}
38
+
39
+/**
40
+ * Calculate command length
41
+ *
42
+ * @v three	Three-wire interface
43
+ * @ret len	Command length, in bits
44
+ */
45
+static inline __attribute__ (( always_inline )) unsigned int
46
+threewire_cmd_len ( struct threewire_device *three ) {
47
+	return ( three->adrsize + 3 );
48
+}
49
+
50
+/* Constants for some standard parts */
51
+#define AT93C46_ORG8_ADRSIZE	7
52
+#define AT93C46_ORG8_DATASIZE	8
53
+#define AT93C46_ORG16_ADRSIZE	6
54
+#define AT93C46_ORG16_DATASIZE	16
55
+#define AT93C46_UDELAY		1
56
+#define AT93C56_ORG8_ADRSIZE	9
57
+#define AT93C56_ORG8_DATASIZE	8
58
+#define AT93C56_ORG16_ADRSIZE	8
59
+#define AT93C56_ORG16_DATASIZE	16
60
+#define AT93C56_UDELAY		1
61
+
62
+extern unsigned long threewire_read ( struct threewire_device *three,
63
+				      unsigned long address );
64
+
65
+#endif /* _GPXE_THREEWIRE_H */

Loading…
Cancel
Save