Browse Source

Generalised the SPI abstraction layer to also be able to handle interfaces

that don't provide the full flexibility of a bit-bashing interface.

Temporarily hacked rtl8139.c to use the new interface.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
931f94dca3

+ 2
- 2
src/drivers/bitbash/bitbash.c View File

37
  */
37
  */
38
 void write_bit ( struct bit_basher *basher, unsigned int bit_id,
38
 void write_bit ( struct bit_basher *basher, unsigned int bit_id,
39
 		 unsigned long data ) {
39
 		 unsigned long data ) {
40
-	basher->write ( basher, bit_id, ( data ? -1UL : 0 ) );
40
+	basher->op->write ( basher, bit_id, ( data ? -1UL : 0 ) );
41
 }
41
 }
42
 
42
 
43
 /**
43
 /**
52
  * it needs to apply.
52
  * it needs to apply.
53
  */
53
  */
54
 int read_bit ( struct bit_basher *basher, unsigned int bit_id ) {
54
 int read_bit ( struct bit_basher *basher, unsigned int bit_id ) {
55
-	return ( basher->read ( basher, bit_id ) ? -1UL : 0 );
55
+	return ( basher->op->read ( basher, bit_id ) ? -1UL : 0 );
56
 }
56
 }

+ 2
- 2
src/drivers/bitbash/i2c_bit.c View File

314
 void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
314
 void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
315
 	struct bit_basher *basher = &i2cbit->basher;
315
 	struct bit_basher *basher = &i2cbit->basher;
316
 	
316
 	
317
-	assert ( basher->read != NULL );
318
-	assert ( basher->write != NULL );
317
+	assert ( basher->op->read != NULL );
318
+	assert ( basher->op->write != NULL );
319
 	i2cbit->i2c.read = i2c_bit_read;
319
 	i2cbit->i2c.read = i2c_bit_read;
320
 	i2cbit->i2c.write = i2c_bit_write;
320
 	i2cbit->i2c.write = i2c_bit_write;
321
 	i2c_stop ( basher );
321
 	i2c_stop ( basher );

+ 74
- 51
src/drivers/bitbash/spi_bit.c View File

19
 #include <stddef.h>
19
 #include <stddef.h>
20
 #include <stdint.h>
20
 #include <stdint.h>
21
 #include <string.h>
21
 #include <string.h>
22
+#include <byteswap.h>
22
 #include <errno.h>
23
 #include <errno.h>
23
 #include <assert.h>
24
 #include <assert.h>
24
 #include <timer.h>
25
 #include <timer.h>
25
 #include <gpxe/bitbash.h>
26
 #include <gpxe/bitbash.h>
26
-#include <gpxe/spi.h>
27
+#include <gpxe/spi_bit.h>
27
 
28
 
28
 /** @file
29
 /** @file
29
  *
30
  *
32
  */
33
  */
33
 
34
 
34
 /** Delay between SCLK changes and around SS changes */
35
 /** Delay between SCLK changes and around SS changes */
35
-static void spi_delay ( void ) {
36
-	udelay ( SPI_UDELAY );
36
+static void spi_bit_delay ( void ) {
37
+	udelay ( SPI_BIT_UDELAY );
37
 }
38
 }
38
 
39
 
40
+/** Chip select line will be asserted */
41
+#define SELECT_SLAVE 0
42
+
43
+/** Chip select line will be deasserted */
44
+#define DESELECT_SLAVE SPI_MODE_SSPOL
45
+
39
 /**
46
 /**
40
  * Select/deselect slave
47
  * Select/deselect slave
41
  *
48
  *
42
- * @v spi		SPI bit-bashing interface
49
+ * @v spibit		SPI bit-bashing interface
43
  * @v slave		Slave number
50
  * @v slave		Slave number
44
  * @v state		Slave select state
51
  * @v state		Slave select state
45
  *
52
  *
46
- * @c state must be set to zero to select the specified slave, or to
47
- * @c SPI_MODE_SSPOL to deselect the slave.
53
+ * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
48
  */
54
  */
49
 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
55
 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
50
 				       unsigned int slave,
56
 				       unsigned int slave,
51
 				       unsigned int state ) {
57
 				       unsigned int state ) {
52
 	struct bit_basher *basher = &spibit->basher;
58
 	struct bit_basher *basher = &spibit->basher;
53
 
59
 
54
-	state ^= ( spibit->spi.mode & SPI_MODE_SSPOL );
60
+	state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
55
 	DBG ( "Setting slave %d select %s\n", slave,
61
 	DBG ( "Setting slave %d select %s\n", slave,
56
 	      ( state ? "high" : "low" ) );
62
 	      ( state ? "high" : "low" ) );
57
 
63
 
58
-	spi_delay();
64
+	spi_bit_delay();
59
 	write_bit ( basher, SPI_BIT_SS ( slave ), state );
65
 	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 );
66
+	spi_bit_delay();
76
 }
67
 }
77
 
68
 
78
 /**
69
 /**
79
- * Deselect slave
70
+ * Transfer bits over SPI bit-bashing bus
80
  *
71
  *
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
72
+ * @v bus		SPI bus
94
  * @v data_out		TX data buffer (or NULL)
73
  * @v data_out		TX data buffer (or NULL)
95
  * @v data_in		RX data buffer (or NULL)
74
  * @v data_in		RX data buffer (or NULL)
96
  * @v len		Length of transfer (in @b bits)
75
  * @v len		Length of transfer (in @b bits)
101
  * NULL, then the data sent will be all zeroes.  If @c data_in is
80
  * NULL, then the data sent will be all zeroes.  If @c data_in is
102
  * NULL, then the incoming data will be discarded.
81
  * NULL, then the incoming data will be discarded.
103
  */
82
  */
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 );
83
+static void spi_bit_transfer ( struct spi_bit_basher *spibit,
84
+			       const void *data_out, void *data_in,
85
+			       unsigned int len ) {
86
+	struct spi_bus *bus = &spibit->bus;
108
 	struct bit_basher *basher = &spibit->basher;
87
 	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 );
88
+	unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
89
+	unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
111
 	unsigned int offset;
90
 	unsigned int offset;
112
 	unsigned int mask;
91
 	unsigned int mask;
113
 	unsigned int bit;
92
 	unsigned int bit;
114
 	int step;
93
 	int step;
115
 
94
 
116
-	DBG ( "Transferring %d bits in mode %x\n", len, spi->mode );
95
+	DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
117
 
96
 
118
 	for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
97
 	for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
119
 		/* Calculate byte offset within data and bit mask */
98
 		/* Calculate byte offset within data and bit mask */
145
 		}
124
 		}
146
 
125
 
147
 		/* Toggle clock line */
126
 		/* Toggle clock line */
148
-		spi_delay();
127
+		spi_bit_delay();
149
 		sclk = ~sclk;
128
 		sclk = ~sclk;
150
 		write_bit ( basher, SPI_BIT_SCLK, sclk );
129
 		write_bit ( basher, SPI_BIT_SCLK, sclk );
151
 	}
130
 	}
152
 }
131
 }
153
 
132
 
133
+/**
134
+ * Read/write data via SPI bit-bashing bus
135
+ *
136
+ * @v bus		SPI bus
137
+ * @v device		SPI device
138
+ * @v command		Command
139
+ * @v address		Address to read/write (<0 for no address)
140
+ * @v data_out		TX data buffer (or NULL)
141
+ * @v data_in		RX data buffer (or NULL)
142
+ * @v len		Length of transfer (in @b words)
143
+ * @ret rc		Return status code
144
+ */
145
+static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
146
+			unsigned int command, int address,
147
+			const void *data_out, void *data_in,
148
+			unsigned int len ) {
149
+	struct spi_bit_basher *spibit
150
+		= container_of ( bus, struct spi_bit_basher, bus );
151
+	struct spi_device_type *devtype = device->type;
152
+	uint32_t tmp;
153
+
154
+	/* Assert chip select on specified slave */
155
+	spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
156
+
157
+	/* Transmit command */
158
+	assert ( devtype->command_len <= ( 8 * sizeof ( tmp ) ) );
159
+	tmp = cpu_to_le32 ( command );
160
+	spi_bit_transfer ( spibit, &tmp, NULL, devtype->command_len );
161
+
162
+	/* Transmit address, if present */
163
+	if ( address >= 0 ) {
164
+		assert ( devtype->address_len <= ( 8 * sizeof ( tmp ) ) );
165
+		tmp = cpu_to_le32 ( address );
166
+		spi_bit_transfer ( spibit, &tmp, NULL, devtype->address_len );
167
+	}
168
+
169
+	/* Transmit/receive data */
170
+	spi_bit_transfer ( spibit, data_out, data_in,
171
+			   ( len * devtype->word_len ) );
172
+
173
+	/* Deassert chip select on specified slave */
174
+	spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
175
+
176
+	return 0;
177
+}
178
+
154
 /**
179
 /**
155
  * Initialise SPI bit-bashing interface
180
  * Initialise SPI bit-bashing interface
156
  *
181
  *
157
  * @v spibit		SPI bit-bashing interface
182
  * @v spibit		SPI bit-bashing interface
158
  */
183
  */
159
 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
184
 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;
185
+	assert ( &spibit->basher.op->read != NULL );
186
+	assert ( &spibit->basher.op->write != NULL );
187
+	spibit->bus.rw = spi_bit_rw;
165
 }
188
 }

+ 6
- 2
src/drivers/net/etherfabric.c View File

1058
 	return ( EFAB_DWORD_FIELD ( reg, EF1_EEPROM ) & mask );
1058
 	return ( EFAB_DWORD_FIELD ( reg, EF1_EEPROM ) & mask );
1059
 }
1059
 }
1060
 
1060
 
1061
+static struct bit_basher_operations ef1002_basher_ops = {
1062
+	.read = ef1002_i2c_read_bit,
1063
+	.write = ef1002_i2c_write_bit,
1064
+};
1065
+
1061
 static void ef1002_init_eeprom ( struct efab_nic *efab ) {
1066
 static void ef1002_init_eeprom ( struct efab_nic *efab ) {
1062
-	efab->ef1002_i2c.basher.write = ef1002_i2c_write_bit;
1063
-	efab->ef1002_i2c.basher.read = ef1002_i2c_read_bit;
1067
+	efab->ef1002_i2c.basher.op = &ef1002_basher_ops;
1064
 	init_i2c_bit_basher ( &efab->ef1002_i2c );
1068
 	init_i2c_bit_basher ( &efab->ef1002_i2c );
1065
 	efab->ef1002_eeprom.address = EF1_EEPROM_I2C_ID;
1069
 	efab->ef1002_eeprom.address = EF1_EEPROM_I2C_ID;
1066
 }
1070
 }

+ 24
- 26
src/drivers/net/rtl8139.c View File

77
 #include <gpxe/ethernet.h>
77
 #include <gpxe/ethernet.h>
78
 #include <gpxe/pkbuff.h>
78
 #include <gpxe/pkbuff.h>
79
 #include <gpxe/netdevice.h>
79
 #include <gpxe/netdevice.h>
80
-#include <gpxe/spi.h>
80
+#include <gpxe/spi_bit.h>
81
 #include <gpxe/threewire.h>
81
 #include <gpxe/threewire.h>
82
 
82
 
83
 #define TX_RING_SIZE 4
83
 #define TX_RING_SIZE 4
97
 	struct rtl8139_tx tx;
97
 	struct rtl8139_tx tx;
98
 	struct rtl8139_rx rx;
98
 	struct rtl8139_rx rx;
99
 	struct spi_bit_basher spibit;
99
 	struct spi_bit_basher spibit;
100
-	struct threewire_device eeprom;
100
+	struct spi_device eeprom;
101
 };
101
 };
102
 
102
 
103
 /* Tuning Parameters */
103
 /* Tuning Parameters */
204
 /* Offsets within EEPROM (these are word offsets) */
204
 /* Offsets within EEPROM (these are word offsets) */
205
 #define EE_MAC 7
205
 #define EE_MAC 7
206
 
206
 
207
-static inline struct rtl8139_nic *
208
-basher_to_rtl ( struct bit_basher *basher ) {
209
-	return container_of ( basher, struct rtl8139_nic, spibit.basher );
210
-}
211
-
212
 static const uint8_t rtl_ee_bits[] = {
207
 static const uint8_t rtl_ee_bits[] = {
213
 	[SPI_BIT_SCLK]	= EE_SK,
208
 	[SPI_BIT_SCLK]	= EE_SK,
214
 	[SPI_BIT_MOSI]	= EE_DI,
209
 	[SPI_BIT_MOSI]	= EE_DI,
218
 
213
 
219
 static int rtl_spi_read_bit ( struct bit_basher *basher,
214
 static int rtl_spi_read_bit ( struct bit_basher *basher,
220
 			      unsigned int bit_id ) {
215
 			      unsigned int bit_id ) {
221
-	struct rtl8139_nic *rtl = basher_to_rtl ( basher );
216
+	struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
217
+						 spibit.basher );
222
 	uint8_t mask = rtl_ee_bits[bit_id];
218
 	uint8_t mask = rtl_ee_bits[bit_id];
223
 	uint8_t eereg;
219
 	uint8_t eereg;
224
 
220
 
228
 
224
 
229
 static void rtl_spi_write_bit ( struct bit_basher *basher,
225
 static void rtl_spi_write_bit ( struct bit_basher *basher,
230
 				unsigned int bit_id, unsigned long data ) {
226
 				unsigned int bit_id, unsigned long data ) {
231
-	struct rtl8139_nic *rtl = basher_to_rtl ( basher );
227
+	struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
228
+						 spibit.basher );
232
 	uint8_t mask = rtl_ee_bits[bit_id];
229
 	uint8_t mask = rtl_ee_bits[bit_id];
233
 	uint8_t eereg;
230
 	uint8_t eereg;
234
 
231
 
238
 	outb ( eereg, rtl->ioaddr + Cfg9346 );
235
 	outb ( eereg, rtl->ioaddr + Cfg9346 );
239
 }
236
 }
240
 
237
 
238
+static struct bit_basher_operations rtl_basher_ops = {
239
+	.read = rtl_spi_read_bit,
240
+	.write = rtl_spi_write_bit,
241
+};
242
+
243
+static struct spi_device_type rtl_ee9346 = AT93C46 ( 16 );
244
+static struct spi_device_type rtl_ee9356 = AT93C56 ( 16 );
245
+
241
 /**
246
 /**
242
  * Set up for EEPROM access
247
  * Set up for EEPROM access
243
  *
248
  *
247
 	int ee9356;
252
 	int ee9356;
248
 
253
 
249
 	/* Initialise three-wire bus */
254
 	/* 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;
255
+	rtl->spibit.basher.op = &rtl_basher_ops;
256
+	rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
253
 	init_spi_bit_basher ( &rtl->spibit );
257
 	init_spi_bit_basher ( &rtl->spibit );
254
 
258
 
255
 	/* Detect EEPROM type and initialise three-wire device */
259
 	/* Detect EEPROM type and initialise three-wire device */
256
 	ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
260
 	ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
257
-	DBG ( "EEPROM is an %s\n", ee9356 ? "AT93C56" : "AT93C46" );
258
-	rtl->eeprom.adrsize =
259
-		( ee9356 ? AT93C56_ORG16_ADRSIZE : AT93C46_ORG16_ADRSIZE );
260
-	rtl->eeprom.datasize =
261
-		( ee9356 ? AT93C56_ORG16_DATASIZE : AT93C46_ORG16_DATASIZE );
262
-	rtl->eeprom.spi = &rtl->spibit.spi;
261
+	DBG ( "EEPROM is an %s\n", ( ee9356 ? "AT93C56" : "AT93C46" ) );
262
+	rtl->eeprom.type = ( ee9356 ? &rtl_ee9356 : &rtl_ee9346 );
263
+	rtl->eeprom.bus = &rtl->spibit.bus;
263
 }
264
 }
264
 
265
 
265
 /**
266
 /**
269
  * @v mac_addr		Buffer to contain MAC address (ETH_ALEN bytes)
270
  * @v mac_addr		Buffer to contain MAC address (ETH_ALEN bytes)
270
  */
271
  */
271
 static void rtl_read_mac ( struct rtl8139_nic *rtl, uint8_t *mac_addr ) {
272
 static void rtl_read_mac ( struct rtl8139_nic *rtl, uint8_t *mac_addr ) {
272
-	union {
273
-		uint16_t word;
274
-		uint8_t bytes[2];
275
-	} u;
273
+
274
+	struct spi_device *device = &rtl->eeprom;
276
 	int i;
275
 	int i;
277
 	
276
 	
278
 	DBG ( "MAC address is " );
277
 	DBG ( "MAC address is " );
279
 	for ( i = EE_MAC ; i < ( EE_MAC + ( ETH_ALEN / 2 ) ) ; i++ ) {
278
 	for ( i = EE_MAC ; i < ( EE_MAC + ( ETH_ALEN / 2 ) ) ; i++ ) {
280
-		u.word = cpu_to_le16 ( threewire_read ( &rtl->eeprom, i ) );
281
-		*mac_addr++ = u.bytes[0];
282
-		*mac_addr++ = u.bytes[1];
283
-		DBG ( "%02x%02x", u.bytes[0], u.bytes[1] );
279
+		device->type->read ( device, i, mac_addr, 1 );
280
+		DBG ( "%02x%02x", mac_addr[0], mac_addr[1] );
281
+		mac_addr += 2;
284
 	}
282
 	}
285
 	DBG ( "\n" );
283
 	DBG ( "\n" );
286
 }
284
 }

+ 15
- 24
src/drivers/nvs/threewire.c View File

17
  */
17
  */
18
 
18
 
19
 #include <stddef.h>
19
 #include <stddef.h>
20
-#include <byteswap.h>
21
-#include <gpxe/spi.h>
20
+#include <assert.h>
22
 #include <gpxe/threewire.h>
21
 #include <gpxe/threewire.h>
23
 
22
 
24
 /** @file
23
 /** @file
27
  *
26
  *
28
  */
27
  */
29
 
28
 
30
-/**
31
- * Read from a three-wire device
29
+/** Read data from three-wire device
32
  *
30
  *
33
- * @v three	Three-wire device
34
- * @v address	Address
35
- * @ret data	Data
31
+ * @v device		SPI device
32
+ * @v address		Address from which to read
33
+ * @v data		Data buffer
34
+ * @v len		Length of data to read, in @b words
35
+ * @ret rc		Return status code
36
  */
36
  */
37
-unsigned long threewire_read ( struct threewire_device *three,
38
-			       unsigned long address ) {
39
-	struct spi_interface *spi = three->spi;
40
-	uint32_t data;
37
+int threewire_read ( struct spi_device *device, unsigned int address,
38
+		     void *data, unsigned int len ) {
39
+	struct spi_bus *bus = device->bus;
41
 
40
 
42
-	/* Activate chip select line */
43
-	spi->select_slave ( spi, three->slave );
41
+	assert ( bus->mode == SPI_MODE_THREEWIRE );
44
 
42
 
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
-	
49
-	/* Read back data */
50
-	data = 0;
51
-	spi->transfer ( spi, NULL, &data, three->datasize );
43
+	DBG ( "3wire %p reading words [%04x,%04x)\n", device,
44
+	      address, ( address + len ) );
52
 
45
 
53
-	/* Deactivate chip select line */
54
-	spi->deselect_slave ( spi );
55
-
56
-	return le32_to_cpu ( data );;
46
+	return bus->rw ( bus, device, THREEWIRE_READ, address,
47
+			 NULL, data, len );
57
 }
48
 }

+ 10
- 2
src/include/gpxe/bitbash.h View File

7
  *
7
  *
8
  */
8
  */
9
 
9
 
10
-/** A bit-bashing interface */
11
-struct bit_basher {
10
+struct bit_basher;
11
+
12
+/** Bit-bashing operations */
13
+struct bit_basher_operations {
12
 	/**
14
 	/**
13
 	 * Set/clear output bit
15
 	 * Set/clear output bit
14
 	 *
16
 	 *
35
 	int ( * read ) ( struct bit_basher *basher, unsigned int bit_id );
37
 	int ( * read ) ( struct bit_basher *basher, unsigned int bit_id );
36
 };
38
 };
37
 
39
 
40
+/** A bit-bashing interface */
41
+struct bit_basher {
42
+	/** Bit-bashing operations */
43
+	struct bit_basher_operations *op;
44
+};
45
+
38
 extern void write_bit ( struct bit_basher *basher, unsigned int bit_id,
46
 extern void write_bit ( struct bit_basher *basher, unsigned int bit_id,
39
 			unsigned long data );
47
 			unsigned long data );
40
 extern int read_bit ( struct bit_basher *basher, unsigned int bit_id );
48
 extern int read_bit ( struct bit_basher *basher, unsigned int bit_id );

+ 180
- 59
src/include/gpxe/spi.h View File

9
 
9
 
10
 #include <gpxe/bitbash.h>
10
 #include <gpxe/bitbash.h>
11
 
11
 
12
-/** An SPI interface */
13
-struct spi_interface {
12
+/**
13
+ * @defgroup spicmds SPI commands
14
+ * @{
15
+ */
16
+
17
+/** Write status register */
18
+#define SPI_WRSR 0x01
19
+
20
+/** Write data to memory array */
21
+#define SPI_WRITE 0x02
22
+
23
+/** Read data from memory array */
24
+#define SPI_READ 0x03
25
+
26
+/** Reset write enable latch */
27
+#define SPI_WRDI 0x04
28
+
29
+/** Read status register */
30
+#define SPI_RDSR 0x05
31
+
32
+/** Set write enable latch */
33
+#define SPI_WREN 0x06
34
+
35
+/**
36
+ * @defgroup atmelcmds Atmel-specific SPI commands
37
+ * @{
38
+ */
39
+
40
+/** Erase one sector in memory array (Not supported on all devices) */
41
+#define ATMEL_SECTOR_ERASE 0x52
42
+
43
+/** Erase all sections in memory array (Not supported on all devices) */
44
+#define ATMEL_CHIP_ERASE 0x62
45
+
46
+/** Read manufacturer and product ID (Not supported on all devices) */
47
+#define ATMEL_RDID 0x15
48
+
49
+/** @} */
50
+
51
+/** @} */
52
+
53
+/**
54
+ * @defgroup spistatus SPI status register bits (not present on all devices)
55
+ * @{
56
+ */
57
+
58
+/** Write-protect pin enabled */
59
+#define SPI_STATUS_WPEN 0x80
60
+
61
+/** Block protection bit 2 */
62
+#define SPI_STATUS_BP2 0x10
63
+
64
+/** Block protection bit 1 */
65
+#define SPI_STATUS_BP1 0x08
66
+
67
+/** Block protection bit 0 */
68
+#define SPI_STATUS_BP0 0x04
69
+
70
+/** State of the write enable latch */
71
+#define SPI_STATUS_WEN 0x02
72
+
73
+/** Device busy flag */
74
+#define SPI_STATUS_NRDY 0x01
75
+
76
+/** @} */
77
+
78
+struct spi_device;
79
+
80
+/**
81
+ * An SPI device type
82
+ *
83
+ * This data structure represents all the characteristics belonging to
84
+ * a particular type of SPI device, e.g. "an Atmel 251024 serial flash",
85
+ * or "a Microchip 25040 serial EEPROM".
86
+ */
87
+struct spi_device_type {
88
+	/** Word length, in bits */
89
+	unsigned int word_len;
90
+	/** Device size (in words) */
91
+	unsigned int size;
92
+	/** Data block size (in words)
93
+	 *
94
+	 * This is the block size used by the device.  It must be a
95
+	 * power of two.  Data reads and writes must not cross a block
96
+	 * boundary.
97
+	 *
98
+	 * Many devices allow reads to cross a block boundary, and
99
+	 * restrict only writes.  For the sake of simplicity, we
100
+	 * assume that the same restriction applies to both reads and
101
+	 * writes.
102
+	 */
103
+	unsigned int block_size;
104
+	/** Command length, in bits */
105
+	unsigned int command_len;
106
+	/** Address length, in bits */
107
+	unsigned int address_len;
108
+	/** Address is munged
109
+	 *
110
+	 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM)
111
+	 * use bit 3 of the command byte as address bit A8, rather
112
+	 * than having a two-byte address.  If this flag is set, then
113
+	 * commands should be munged in this way.
114
+	 */
115
+	unsigned int munge_address : 1;
116
+	/** Read data from device
117
+	 *
118
+	 * @v device		SPI device
119
+	 * @v address		Address from which to read
120
+	 * @v data		Data buffer
121
+	 * @v len		Length of data to read, in @b words
122
+	 * @ret rc		Return status code
123
+	 */
124
+	int ( * read ) ( struct spi_device *device, unsigned int address,
125
+			 void *data, unsigned int len );
126
+	/** Write data to device
127
+	 *
128
+	 * @v device		SPI device
129
+	 * @v address		Address to which to write
130
+	 * @v data		Data buffer
131
+	 * @v len		Length of data to write, in @b words
132
+	 * @ret rc		Return status code
133
+	 */
134
+	int ( * write ) ( struct spi_device *device, unsigned int address,
135
+			  const void *data, unsigned int len );
136
+};
137
+
138
+/**
139
+ * @defgroup spidevs SPI device types
140
+ * @{
141
+ */
142
+
143
+/** Atmel AT25010 serial EEPROM */
144
+#define AT25010 {		\
145
+	.word_len = 8,		\
146
+	.size = 128,		\
147
+	.block_size = 8,	\
148
+	.command_len = 8,	\
149
+	.address_len = 8,	\
150
+	}
151
+
152
+/** @} */
153
+
154
+/**
155
+ * An SPI device
156
+ *
157
+ * This data structure represents a real, physical SPI device attached
158
+ * to an SPI controller.  It comprises the device type plus
159
+ * instantiation-specific information such as the slave number.
160
+ */
161
+struct spi_device {
162
+	/** SPI device type */
163
+	struct spi_device_type *type;
164
+	/** SPI bus to which device is attached */
165
+	struct spi_bus *bus;
166
+	/** Slave number */
167
+	unsigned int slave;
168
+};
169
+
170
+/**
171
+ * An SPI bus
172
+ *
173
+ * 
174
+ */
175
+struct spi_bus {
14
 	/** SPI interface mode
176
 	/** SPI interface mode
15
 	 *
177
 	 *
16
 	 * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
178
 	 * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
22
 	 */
184
 	 */
23
 	unsigned int mode;
185
 	unsigned int mode;
24
 	/**
186
 	/**
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
187
+	 * Read/write data via SPI bus
40
 	 *
188
 	 *
41
-	 * @v spi		SPI interface
189
+	 * @v bus		SPI bus
190
+	 * @v device		SPI device
191
+	 * @v command		Command
192
+	 * @v address		Address to read/write (<0 for no address)
42
 	 * @v data_out		TX data buffer (or NULL)
193
 	 * @v data_out		TX data buffer (or NULL)
43
 	 * @v data_in		RX data buffer (or NULL)
194
 	 * @v data_in		RX data buffer (or NULL)
44
-	 * @v len		Length of transfer (in @b bits)
195
+	 * @v len		Length of transfer (in @b words)
196
+	 *
197
+	 * This issues the specified command and optional address to
198
+	 * the SPI device, then reads and/or writes data to/from the
199
+	 * data buffers.  Note that the transfer length is measured in
200
+	 * words, not in bytes.  Some SPI devices have 16-bit word
201
+	 * lengths; take care with these devices not to accidentally
202
+	 * read or write twice as much data as intended.
45
 	 */
203
 	 */
46
-	void ( * transfer ) ( struct spi_interface *spi, const void *data_out,
47
-			      void *data_in, unsigned int len );
204
+	int ( * rw ) ( struct spi_bus *bus, struct spi_device *device,
205
+		       unsigned int command, int address,
206
+		       const void *data_out, void *data_in, unsigned int len );
48
 };
207
 };
49
 
208
 
50
 /** Clock phase (CPHA) mode bit
209
 /** Clock phase (CPHA) mode bit
91
  */
250
  */
92
 #define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
251
 #define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
93
 
252
 
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 */
253
 #endif /* _GPXE_SPI_H */

+ 45
- 0
src/include/gpxe/spi_bit.h View File

1
+#ifndef _GPXE_SPI_BIT_H
2
+#define _GPXE_SPI_BIT_H
3
+
4
+/** @file
5
+ *
6
+ * SPI bit-bashing interface
7
+ *
8
+ */
9
+
10
+#include <gpxe/spi.h>
11
+
12
+/** A bit-bashing SPI bus */
13
+struct spi_bit_basher {
14
+	/** SPI bus */
15
+	struct spi_bus bus;
16
+	/** Bit-bashing interface */
17
+	struct bit_basher basher;
18
+};
19
+
20
+/** Bit indices used for SPI bit-bashing interface */
21
+enum {
22
+	/** Serial clock */
23
+	SPI_BIT_SCLK = 0,
24
+	/** Master Out Slave In */
25
+	SPI_BIT_MOSI,
26
+	/** Master In Slave Out */
27
+	SPI_BIT_MISO,
28
+	/** Slave 0 select */
29
+	SPI_BIT_SS0,
30
+};
31
+
32
+/**
33
+ * Determine bit index for a particular slave
34
+ *
35
+ * @v slave		Slave number
36
+ * @ret index		Bit index (i.e. SPI_BIT_SSN, where N=slave) 
37
+ */
38
+#define SPI_BIT_SS( slave ) ( SPI_BIT_SS0 + (slave) )
39
+
40
+/** Delay between SCLK transitions */
41
+#define SPI_BIT_UDELAY 1
42
+
43
+extern void init_spi_bit_basher ( struct spi_bit_basher *spibit );
44
+
45
+#endif /* _GPXE_SPI_BIT_H */

+ 40
- 44
src/include/gpxe/threewire.h View File

10
  * support.
10
  * support.
11
  */
11
  */
12
 
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
-};
13
+#include <gpxe/spi.h>
26
 
14
 
27
 /**
15
 /**
28
- * Calculate read command for a specified address
29
- *
30
- * @v three	Three-wire interface
31
- * @v address	Address
32
- * @ret cmd	Command
16
+ * @defgroup tcmds Three-wire commands
17
+ * @{
33
  */
18
  */
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
-}
19
+
20
+/** Read data from memory array */
21
+#define THREEWIRE_READ 0x6
22
+
23
+/** @} */
38
 
24
 
39
 /**
25
 /**
40
- * Calculate command length
41
- *
42
- * @v three	Three-wire interface
43
- * @ret len	Command length, in bits
26
+ * @defgroup spidevs SPI device types
27
+ * @{
44
  */
28
  */
45
-static inline __attribute__ (( always_inline )) unsigned int
46
-threewire_cmd_len ( struct threewire_device *three ) {
47
-	return ( three->adrsize + 3 );
48
-}
49
 
29
 
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 );
30
+/** Atmel AT93C46 serial EEPROM
31
+ *
32
+ * @v org	Word size (8 or 16)
33
+ */
34
+#define AT93C46( org ) {				\
35
+	.word_len = (org),				\
36
+	.size = ( 1024 / (org) ),			\
37
+	.block_size = 1,				\
38
+	.command_len = 3,				\
39
+	.address_len = ( ( (org) == 8 ) ? 7 : 6 ),	\
40
+	.read = threewire_read,				\
41
+	}
42
+
43
+/** Atmel AT93C56 serial EEPROM
44
+ *
45
+ * @v org	Word size (8 or 16)
46
+ */
47
+#define AT93C56( org ) {				\
48
+	.word_len = (org),				\
49
+	.size = ( 2048 / (org) ),			\
50
+	.block_size = 1,				\
51
+	.command_len = 3,				\
52
+	.address_len = ( ( (org) == 8 ) ? 9 : 8 ),	\
53
+	.read = threewire_read,				\
54
+	}
55
+
56
+/** @} */
57
+
58
+extern int threewire_read ( struct spi_device *device, unsigned int address,
59
+			    void *data, unsigned int len );
64
 
60
 
65
 #endif /* _GPXE_THREEWIRE_H */
61
 #endif /* _GPXE_THREEWIRE_H */

Loading…
Cancel
Save