瀏覽代碼

Updated to new bus API, and improved using the 3c5x9 reference manual.

tags/v0.9.3
Michael Brown 19 年之前
父節點
當前提交
139d0fe481
共有 1 個檔案被更改,包括 274 行新增112 行删除
  1. 274
    112
      src/drivers/net/3c509.c

+ 274
- 112
src/drivers/net/3c509.c 查看文件

@@ -13,72 +13,116 @@
13 13
 
14 14
 /*
15 15
  * 3c509 cards have their own method of contention resolution; this
16
- * effectively defines another bus type.
16
+ * effectively defines another bus type similar to ISAPnP.  Even the
17
+ * original ISA cards can be programatically mapped to any I/O address
18
+ * in the range 0x200-0x3e0.
19
+ * 
20
+ * However, there is a small problem: once you've activated a card,
21
+ * the only ways to deactivate it will also wipe its tag, meaning that
22
+ * you won't be able to subsequently reactivate it without going
23
+ * through the whole ID sequence again.  The solution we adopt is to
24
+ * isolate and tag all cards at the start, and to immediately
25
+ * re-isolate and re-tag a card after disabling it.
17 26
  *
18 27
  */
19 28
 
29
+static uint16_t t509_id_port = 0;
30
+static uint8_t t509_max_tag = 0;
31
+
20 32
 /*
21
- * A physical t509 device
33
+ * A location on a t509 bus
22 34
  *
23 35
  */
24
-struct t509_device {
25
-	char *magic; /* must be first */
26
-	uint16_t id_port;
27
-	uint16_t ioaddr;
28
-	unsigned char current_tag;
36
+struct t509_loc {
37
+	uint8_t tag;
29 38
 };
30 39
 
31 40
 /*
32
- * A t509 driver
41
+ * A physical t509 device
33 42
  *
34 43
  */
35
-struct t509_driver {
36
-	const char *name;
44
+struct t509_device {
45
+	uint16_t ioaddr;
46
+	uint8_t tag;
37 47
 };
38 48
 
39 49
 /*
40
- * Ensure that there is sufficient space in the shared dev_bus
41
- * structure for a struct pci_device.
50
+ * t509 utility functions
42 51
  *
43 52
  */
44
-DEV_BUS( struct t509_device, t509_dev );
45
-static char t509_magic[0]; /* guaranteed unique symbol */
53
+
54
+static inline void t509_set_id_port ( void ) {
55
+	outb ( 0x00, t509_id_port );
56
+}
57
+
58
+static inline void t509_wait_for_id_sequence ( void ) {
59
+	outb ( 0x00, t509_id_port );
60
+}
61
+
62
+static inline void t509_global_reset ( void ) {
63
+	outb ( 0xc0, t509_id_port );
64
+}
65
+
66
+static inline void t509_reset_tag ( void ) {
67
+	outb ( 0xd0, t509_id_port );
68
+}
69
+
70
+static inline void t509_set_tag ( uint8_t tag ) {
71
+	outb ( 0xd0 | tag, t509_id_port );
72
+}
73
+
74
+static inline void t509_select_tag ( uint8_t tag ) {
75
+	outb ( 0xd8 | tag, t509_id_port );
76
+}
77
+
78
+static inline void t509_activate ( uint16_t ioaddr ) {
79
+	outb ( 0xe0 | ( ioaddr >> 4 ), t509_id_port );
80
+}
81
+
82
+static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr ) {
83
+	outb ( GLOBAL_RESET, ioaddr + EP_COMMAND );
84
+}
85
+
86
+static inline void t509_load_eeprom_word ( uint8_t offset ) {
87
+	outb ( 0x80 | offset, t509_id_port );
88
+}
46 89
 
47 90
 /*
48
- * Find a port that can be used for contention select
49
- *
50
- * Called only once, so inlined for efficiency.
91
+ * Find a suitable ID port
51 92
  *
52 93
  */
53
-static inline int find_id_port ( struct t509_device *t509 ) {
54
-	for ( t509->id_port = EP_ID_PORT_START ;
55
-	      t509->id_port < EP_ID_PORT_END ;
56
-	      t509->id_port += EP_ID_PORT_INC ) {
57
-		outb ( 0x00, t509->id_port );
58
-		outb ( 0xff, t509->id_port );
59
-		if ( inb ( t509->id_port ) & 0x01 ) {
94
+static inline int t509_find_id_port ( void ) {
95
+
96
+	for ( t509_id_port = EP_ID_PORT_START ;
97
+	      t509_id_port < EP_ID_PORT_END ;
98
+	      t509_id_port += EP_ID_PORT_INC ) {
99
+		t509_set_id_port ();
100
+		/* See if anything's listening */
101
+		outb ( 0xff, t509_id_port );
102
+		if ( inb ( t509_id_port ) & 0x01 ) {
60 103
 			/* Found a suitable port */
104
+			DBG ( "T509 using ID port at %hx\n", t509_id_port );
61 105
 			return 1;
62 106
 		}
63 107
 	}
64 108
 	/* No id port available */
109
+	DBG ( "T509 found no available ID port\n" );
65 110
 	return 0;
66 111
 }
67 112
 
68 113
 /*
69 114
  * Send ID sequence to the ID port
70 115
  *
71
- * Called only once, so inlined for efficiency.
72
- *
73 116
  */
74
-static inline void send_id_sequence ( struct t509_device *t509 ) {
117
+static void t509_send_id_sequence ( void ) {
75 118
 	unsigned short lrs_state, i;
76 119
 
77
-	outb ( 0x00, t509->id_port );
78
-        outb ( 0x00, t509->id_port );
120
+	t509_set_id_port ();
121
+	/* Reset IDS on cards */
122
+	t509_wait_for_id_sequence ();
79 123
 	lrs_state = 0xff;
80 124
         for ( i = 0; i < 255; i++ ) {
81
-                outb ( lrs_state, t509->id_port );
125
+                outb ( lrs_state, t509_id_port );
82 126
                 lrs_state <<= 1;
83 127
                 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
84 128
         }
@@ -96,142 +140,260 @@ static inline void send_id_sequence ( struct t509_device *t509 ) {
96 140
  * the AX register which is conveniently returned to us by inb().  Hence; we
97 141
  * read 16 times getting one bit of data with each read.
98 142
  */
99
-static uint16_t id_read_eeprom ( struct t509_device *t509, int offset ) {
143
+static uint16_t t509_id_read_eeprom ( int offset ) {
100 144
 	int i, data = 0;
101 145
 
102
-	outb ( 0x80 + offset, t509->id_port );
146
+	t509_load_eeprom_word ( offset );
103 147
 	/* Do we really need this wait? Won't be noticeable anyway */
104 148
 	udelay(10000);
105 149
 
106 150
 	for ( i = 0; i < 16; i++ ) {
107
-		data = ( data << 1 ) | ( inw ( t509->id_port ) & 1 );
151
+		data = ( data << 1 ) | ( inw ( t509_id_port ) & 1 );
108 152
 	}
109 153
 	return data;
110 154
 }
111 155
 
112 156
 /*
113
- * Find the next t509 device
114
- *
115
- * Called only once, so inlined for efficiency.
157
+ * Isolate and tag all t509 cards
116 158
  *
117 159
  */
118
-static inline int fill_t509_device ( struct t509_device *t509 ) {
119
-	int i;
120
-	uint16_t iobase;
160
+static void t509_isolate ( void ) {
161
+	unsigned int i;
162
+	uint16_t contend[3];
121 163
 
122
-	/*
123
-	 * We need an ID port, if we don't already have one.
124
-	 *
125
-	 */
126
-	if ( ! t509->id_port ) {
127
-		if ( ! find_id_port ( t509 ) ) {
128
-			DBG ( "No ID port available for contention select\n" );
129
-			return 0;
164
+	/* Find a suitable ID port */
165
+	if ( ! t509_find_id_port () )
166
+		return;
167
+
168
+	while ( 1 ) {
169
+
170
+		/* All cards are in ID_WAIT state each time we go
171
+		 * through this loop.
172
+		 */
173
+
174
+		/* Send the ID sequence */
175
+		t509_send_id_sequence ();
176
+
177
+		/* First time through, reset all tags.  On subsequent
178
+		 * iterations, kill off any already-tagged cards
179
+		 */
180
+		if ( t509_max_tag == 0 ) {
181
+			t509_reset_tag();
182
+		} else {
183
+			t509_select_tag(0);
184
+		}
185
+	
186
+		/* Read the manufacturer ID, to see if there are any
187
+		 * more cards
188
+		 */
189
+		if ( t509_id_read_eeprom ( EEPROM_MFG_ID ) != MFG_ID ) {
190
+			DBG ( "T509 saw %s signs of life\n",
191
+			      t509_max_tag ? "no further" : "no" );
192
+			break;
193
+		}
194
+
195
+		/* Perform contention selection on the MAC address */
196
+		for ( i = 0 ; i < 3 ; i++ ) {
197
+			contend[i] = t509_id_read_eeprom ( i );
130 198
 		}
131
-		DBG ( "T509 scan using ID port at %hx\n", t509->id_port );
199
+
200
+		/* Only one device will still be left alive.  Tag it. */
201
+		++t509_max_tag;
202
+		DBG ( "T509 found card %hx%hx%hx, assigning tag %hhx\n",
203
+		      contend[0], contend[1], contend[2], t509_max_tag );
204
+		t509_set_tag ( t509_max_tag );
205
+
206
+		/* Return all cards back to ID_WAIT state */
207
+		t509_wait_for_id_sequence();
132 208
 	}
133 209
 
134
-	/* 
135
-	 * If this is the start of the scan, clear all tag registers.
136
-	 * Otherwise, tell already-found NICs not to respond.
210
+	DBG ( "T509 found %d cards using ID port %hx\n",
211
+	      t509_max_tag, t509_id_port );
212
+	return;
213
+}
214
+
215
+/*
216
+ * Increment a bus_loc structure to the next possible T509 location.
217
+ * Leave the structure zeroed and return 0 if there are no more valid
218
+ * locations.
219
+ *
220
+ */
221
+static int t509_next_location ( struct bus_loc *bus_loc ) {
222
+	struct t509_loc *t509_loc = ( struct t509_loc * ) bus_loc;
223
+	
224
+	/*
225
+	 * Ensure that there is sufficient space in the shared bus
226
+	 * structures for a struct t509_loc and a struct t509_dev,
227
+	 * as mandated by bus.h.
137 228
 	 *
138 229
 	 */
139
-	if ( t509->current_tag == 0 ) {
140
-		outb ( 0xd0, t509->id_port );
141
-	} else {
142
-		outb ( 0xd8, t509->id_port ) ;
143
-	}
230
+	BUS_LOC_CHECK ( struct t509_loc );
231
+	BUS_DEV_CHECK ( struct t509_device );
144 232
 
145
-	/* Send the ID sequence */
146
-	send_id_sequence ( t509 );
233
+	return ( t509_loc->tag = ( ++t509_loc->tag & EP_TAG_MAX ) );
234
+}
147 235
 
148
-	/* Check the manufacturer ID */
149
-	if ( id_read_eeprom ( t509, EEPROM_MFG_ID ) != MFG_ID ) {
150
-		/* No more t509 devices */
236
+/*
237
+ * Fill in parameters for a T509 device based on tag
238
+ *
239
+ * Return 1 if device present, 0 otherwise
240
+ *
241
+ */
242
+static int t509_fill_device ( struct bus_dev *bus_dev,
243
+			      struct bus_loc *bus_loc ) {
244
+	struct t509_device *t509 = ( struct t509_device * ) bus_dev;
245
+	struct t509_loc *t509_loc = ( struct t509_loc * ) bus_loc;
246
+	uint16_t iobase;
247
+
248
+	/* Copy tag to struct t509 */
249
+	t509->tag = t509_loc->tag;
250
+
251
+	/* Tag 0 is never valid, but may be passed in */
252
+	if ( ! t509->tag )
151 253
 		return 0;
152
-	}
153 254
 
154
-	/* Do contention select by reading the MAC address */
155
-	for ( i = 0 ; i < 3 ; i++ ) {
156
-		id_read_eeprom ( t509, i );
157
-	}
255
+	/* Perform isolation if it hasn't yet been done */
256
+	if ( ! t509_id_port )
257
+		t509_isolate();
158 258
 
159
-	/* By now, only one device will be left active.  Get its I/O
160
-	 * address, tag and activate the adaptor.  Tagging will
161
-	 * prevent it taking part in the next scan, enabling us to see
162
-	 * the next device.
163
-	 */
164
-	iobase = id_read_eeprom ( t509, EEPROM_ADDR_CFG );
259
+	/* Check tag is in range */
260
+	if ( t509->tag > t509_max_tag )
261
+		return 0;
262
+
263
+	/* Send the ID sequence */
264
+	t509_send_id_sequence ();
265
+
266
+	/* Select the specified tag */
267
+	t509_select_tag ( t509->tag );
268
+
269
+	/* Read the default I/O address */
270
+	iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
165 271
 	t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
166
-	outb ( ++t509->current_tag, t509->id_port ); /* tag */
167
-	outb ( ( 0xe0 | iobase ), t509->id_port ); /* activate */
168 272
 
169
-	DBG ( "T509 found at %hx (tagged as %hhx)\n", t509->ioaddr,
170
-	      t509->current_tag );
273
+	/* Send card back to ID_WAIT */
274
+	t509_wait_for_id_sequence();
171 275
 
276
+	DBG ( "T509 found device %hhx, base %hx\n", t509->tag, t509->ioaddr );
172 277
 	return 1;
173 278
 }
174 279
 
175 280
 /*
176
- * Find a t509 device matching the specified driver.  ("Matching the
177
- * specified driver" is, in this case, a no-op, but we want to
178
- * preserve the common bus API).
281
+ * Test whether or not a driver is capable of driving the device.
282
+ * This is a no-op for T509.
179 283
  *
180
- * Called only once, so inlined for efficiency.
284
+ */
285
+static int t509_check_driver ( struct bus_dev *bus_dev __unused,
286
+			       struct device_driver *device_driver __unused ) {
287
+	return 1;
288
+}
289
+
290
+/*
291
+ * Describe a T509 device
181 292
  *
182 293
  */
183
-static inline int find_t509_device ( struct t509_device *t509,
184
-				     struct t509_driver *driver __unused ) {
294
+static char * t509_describe ( struct bus_dev *bus_dev ) {
295
+	struct t509_device *t509 = ( struct t509_device * ) bus_dev;
296
+	static char t509_description[] = "T509 00";
185 297
 
186
-	/* Initialise struct t509 if it's the first time it's been used. */
187
-	if ( t509->magic != t509_magic ) {
188
-		memset ( t509, 0, sizeof ( *t509 ) );
189
-		t509->magic = t509_magic;
190
-	}
298
+	sprintf ( t509_description + 4, "%hhx", t509->tag );
299
+	return t509_description;
300
+}
191 301
 
192
-	/* Find the next t509 device */
193
-	if ( ! fill_t509_device ( t509 ) )
194
-		return 0;
195
-	
196
-	return 1;
302
+/*
303
+ * Name a T509 device
304
+ *
305
+ */
306
+static const char * t509_name ( struct bus_dev *bus_dev __unused ) {
307
+	return "T509";
197 308
 }
198 309
 
199 310
 /*
200
- * Find the next T509 device that can be used to boot using the
201
- * specified driver.
311
+ * T509 bus operations table
202 312
  *
203 313
  */
204
-int find_t509_boot_device ( struct dev *dev, struct t509_driver *driver ) {
205
-	struct t509_device *t509 = ( struct t509_device * )dev->bus;
314
+struct bus_driver t509_driver __bus_driver = {
315
+	.next_location	= t509_next_location,
316
+	.fill_device	= t509_fill_device,
317
+	.check_driver	= t509_check_driver,
318
+	.describe	= t509_describe,
319
+	.name		= t509_name,
320
+};
206 321
 
207
-	if ( ! find_t509_device ( t509, driver ) )
208
-		return 0;
322
+/*
323
+ * Activate a T509 device
324
+ *
325
+ * The device will be enabled at whatever ioaddr is specified in the
326
+ * struct t509_device; there is no need to stick with the default
327
+ * ioaddr read from the EEPROM.
328
+ *
329
+ */
330
+static inline void activate_t509_device ( struct t509_device *t509 ) {
331
+	t509_send_id_sequence ();
332
+	t509_select_tag ( t509->tag );
333
+	t509_activate ( t509->ioaddr );
334
+	DBG ( "T509 activated device %hhx at ioaddr %hx\n",
335
+	      t509->tag, t509->ioaddr );
336
+}
209 337
 
210
-	dev->name = driver->name;
211
-	dev->devid.bus_type = ISA_BUS_TYPE;
212
-	dev->devid.vendor_id = MFG_ID;
213
-	dev->devid.device_id = PROD_ID;
214
-	return 1;
338
+/*
339
+ * Deactivate a T509 device
340
+ *
341
+ * Disabling also clears the tag, so we immediately isolate and re-tag
342
+ * this card.
343
+ *
344
+ */
345
+static inline void deactivate_t509_device ( struct t509_device *t509 ) {
346
+	t509_deactivate_and_reset_tag ( t509->ioaddr );
347
+	udelay ( 1000 );
348
+	t509_send_id_sequence ();
349
+	t509_select_tag ( 0 );
350
+	t509_set_tag ( t509->tag );
351
+	t509_wait_for_id_sequence ();
352
+	DBG ( "T509 deactivated device at %hx and re-tagged as %hhx\n",
353
+	      t509->ioaddr, t509->tag );
215 354
 }
216 355
 
217 356
 /*
218
- * The ISA probe function
357
+ * Fill in a nic structure
358
+ *
359
+ * Called only once, so inlined for efficiency
219 360
  *
220 361
  */
221
-static int el3_t509_probe ( struct dev *dev, struct t509_device *t509 ) {
222
-	struct nic *nic = nic_device ( dev );
223
-	
362
+static inline void t509_fill_nic ( struct nic *nic,
363
+				   struct t509_device *t509 ) {
364
+
365
+	/* Fill in ioaddr and irqno */
224 366
 	nic->ioaddr = t509->ioaddr;
225 367
 	nic->irqno = 0;
226
-	printf ( "3c509 board on ISA at %#hx - ", nic->ioaddr );
368
+
369
+	/* Fill in DHCP device ID structure */
370
+	nic->dhcp_dev_id.bus_type = ISA_BUS_TYPE;
371
+	nic->dhcp_dev_id.vendor_id = htons ( MFG_ID );
372
+	nic->dhcp_dev_id.device_id = htons ( PROD_ID );
373
+}
374
+
375
+/*
376
+ * The ISA probe function
377
+ *
378
+ */
379
+static int el3_t509_probe ( struct nic *nic, struct t509_device *t509 ) {
380
+	
381
+	/* We could change t509->ioaddr if we wanted to */
382
+	activate_t509_device ( t509 );
383
+	t509_fill_nic ( nic, t509 );
227 384
 
228 385
 	/* Hand off to generic t5x9 probe routine */
229 386
 	return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
230 387
 }
231 388
 
232
-static struct t509_driver el3_t509_driver = { "3c509 (ISA)" };
389
+static void el3_t509_disable ( struct nic *nic, struct t509_device *t509 ) {
390
+	t5x9_disable ( nic );
391
+	deactivate_t509_device ( t509 );
392
+}
393
+
394
+static struct {} el3_t509_driver;
233 395
 
234
-BOOT_DRIVER ( "3c509", find_t509_boot_device, el3_t509_driver,
235
-	      el3_t509_probe );
396
+DRIVER ( "3c509", nic_driver, t509_driver, el3_t509_driver,
397
+	 el3_t509_probe, el3_t509_disable );
236 398
 
237
-ISA_ROM ( "3c509","3c509" );
399
+ISA_ROM ( "3c509", "3c509" );

Loading…
取消
儲存