Browse Source

Updated 3c509 to current device model

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
62548c9f0d
1 changed files with 185 additions and 165 deletions
  1. 185
    165
      src/drivers/net/3c509.c

+ 185
- 165
src/drivers/net/3c509.c View File

@@ -4,14 +4,13 @@
4 4
  *
5 5
  */
6 6
 
7
-#if 0
8
-
7
+#include <stdint.h>
8
+#include <stdlib.h>
9
+#include <string.h>
10
+#include <io.h>
11
+#include <timer.h>
12
+#include <gpxe/device.h>
9 13
 #include <gpxe/isa.h>
10
-#include "io.h"
11
-#include "timer.h"
12
-#include "string.h"
13
-#include "console.h"
14
-#include "dev.h"
15 14
 #include "3c509.h"
16 15
 
17 16
 /*
@@ -29,25 +28,46 @@
29 28
  *
30 29
  */
31 30
 
32
-static uint16_t t509_id_port = 0;
33
-static uint8_t t509_max_tag = 0;
31
+static void t509bus_remove ( struct root_device *rootdev );
34 32
 
35
-/*
36
- * A location on a t509 bus
33
+static unsigned int t509_id_port = 0;
34
+static unsigned int t509_max_tag = 0;
35
+
36
+/** A 3c509 device */
37
+struct t509_device {
38
+	/** Generic device */
39
+	struct device dev;
40
+	/** Tag */
41
+	unsigned int tag;
42
+	/** I/O address */
43
+	uint16_t ioaddr;
44
+	/** Driver-private data
45
+	 *
46
+	 * Use t509_set_drvdata() and t509_get_drvdata() to access
47
+	 * this field.
48
+	 */
49
+	void *priv;
50
+};
51
+
52
+/**
53
+ * Set 3c509 driver-private data
37 54
  *
55
+ * @v t509		3c509 device
56
+ * @v priv		Private data
38 57
  */
39
-struct t509_loc {
40
-	uint8_t tag;
41
-};
58
+static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
59
+	t509->priv = priv;
60
+}
42 61
 
43
-/*
44
- * A physical t509 device
62
+/**
63
+ * Get 3c509 driver-private data
45 64
  *
65
+ * @v t509		3c509 device
66
+ * @ret priv		Private data
46 67
  */
47
-struct t509_device {
48
-	uint16_t ioaddr;
49
-	uint8_t tag;
50
-};
68
+static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
69
+	return t509->priv;
70
+}
51 71
 
52 72
 /*
53 73
  * t509 utility functions
@@ -105,12 +125,12 @@ static inline int t509_find_id_port ( void ) {
105 125
 		if ( inb ( t509_id_port ) & 0x01 ) {
106 126
 			/* Found a suitable port */
107 127
 			DBG ( "T509 using ID port at %hx\n", t509_id_port );
108
-			return 1;
128
+			return 0;
109 129
 		}
110 130
 	}
111 131
 	/* No id port available */
112 132
 	DBG ( "T509 found no available ID port\n" );
113
-	return 0;
133
+	return -ENOENT;
114 134
 }
115 135
 
116 136
 /*
@@ -160,13 +180,14 @@ static uint16_t t509_id_read_eeprom ( int offset ) {
160 180
  * Isolate and tag all t509 cards
161 181
  *
162 182
  */
163
-static void t509_isolate ( void ) {
183
+static int t509_isolate ( void ) {
164 184
 	unsigned int i;
165 185
 	uint16_t contend[3];
186
+	int rc;
166 187
 
167 188
 	/* Find a suitable ID port */
168
-	if ( ! t509_find_id_port () )
169
-		return;
189
+	if ( ( rc = t509_find_id_port () ) != 0 )
190
+		return rc;
170 191
 
171 192
 	while ( 1 ) {
172 193
 
@@ -202,7 +223,7 @@ static void t509_isolate ( void ) {
202 223
 
203 224
 		/* Only one device will still be left alive.  Tag it. */
204 225
 		++t509_max_tag;
205
-		DBG ( "T509 found card %hx%hx%hx, assigning tag %hhx\n",
226
+		DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
206 227
 		      contend[0], contend[1], contend[2], t509_max_tag );
207 228
 		t509_set_tag ( t509_max_tag );
208 229
 
@@ -210,119 +231,11 @@ static void t509_isolate ( void ) {
210 231
 		t509_wait_for_id_sequence();
211 232
 	}
212 233
 
213
-	DBG ( "T509 found %d cards using ID port %hx\n",
234
+	DBG ( "T509 found %d cards using ID port %04x\n",
214 235
 	      t509_max_tag, t509_id_port );
215
-	return;
216
-}
217
-
218
-/*
219
- * Increment a bus_loc structure to the next possible T509 location.
220
- * Leave the structure zeroed and return 0 if there are no more valid
221
- * locations.
222
- *
223
- */
224
-static int t509_next_location ( struct bus_loc *bus_loc ) {
225
-	struct t509_loc *t509_loc = ( struct t509_loc * ) bus_loc;
226
-	
227
-	/*
228
-	 * Ensure that there is sufficient space in the shared bus
229
-	 * structures for a struct t509_loc and a struct t509_dev,
230
-	 * as mandated by bus.h.
231
-	 *
232
-	 */
233
-	BUS_LOC_CHECK ( struct t509_loc );
234
-	BUS_DEV_CHECK ( struct t509_device );
235
-
236
-	return ( t509_loc->tag = ( ++t509_loc->tag & EP_TAG_MAX ) );
237
-}
238
-
239
-/*
240
- * Fill in parameters for a T509 device based on tag
241
- *
242
- * Return 1 if device present, 0 otherwise
243
- *
244
- */
245
-static int t509_fill_device ( struct bus_dev *bus_dev,
246
-			      struct bus_loc *bus_loc ) {
247
-	struct t509_device *t509 = ( struct t509_device * ) bus_dev;
248
-	struct t509_loc *t509_loc = ( struct t509_loc * ) bus_loc;
249
-	uint16_t iobase;
250
-
251
-	/* Copy tag to struct t509 */
252
-	t509->tag = t509_loc->tag;
253
-
254
-	/* Tag 0 is never valid, but may be passed in */
255
-	if ( ! t509->tag )
256
-		return 0;
257
-
258
-	/* Perform isolation if it hasn't yet been done */
259
-	if ( ! t509_id_port )
260
-		t509_isolate();
261
-
262
-	/* Check tag is in range */
263
-	if ( t509->tag > t509_max_tag )
264
-		return 0;
265
-
266
-	/* Send the ID sequence */
267
-	t509_send_id_sequence ();
268
-
269
-	/* Select the specified tag */
270
-	t509_select_tag ( t509->tag );
271
-
272
-	/* Read the default I/O address */
273
-	iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
274
-	t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
275
-
276
-	/* Send card back to ID_WAIT */
277
-	t509_wait_for_id_sequence();
278
-
279
-	DBG ( "T509 found device %hhx, base %hx\n", t509->tag, t509->ioaddr );
280
-	return 1;
281
-}
282
-
283
-/*
284
- * Test whether or not a driver is capable of driving the device.
285
- * This is a no-op for T509.
286
- *
287
- */
288
-static int t509_check_driver ( struct bus_dev *bus_dev __unused,
289
-			       struct device_driver *device_driver __unused ) {
290
-	return 1;
291
-}
292
-
293
-/*
294
- * Describe a T509 device
295
- *
296
- */
297
-static char * t509_describe_device ( struct bus_dev *bus_dev ) {
298
-	struct t509_device *t509 = ( struct t509_device * ) bus_dev;
299
-	static char t509_description[] = "T509 00";
300
-
301
-	sprintf ( t509_description + 4, "%hhx", t509->tag );
302
-	return t509_description;
303
-}
304
-
305
-/*
306
- * Name a T509 device
307
- *
308
- */
309
-static const char * t509_name_device ( struct bus_dev *bus_dev __unused ) {
310
-	return "T509";
236
+	return 0;
311 237
 }
312 238
 
313
-/*
314
- * T509 bus operations table
315
- *
316
- */
317
-struct bus_driver t509_driver __bus_driver = {
318
-	.name			= "T509",
319
-	.next_location		= t509_next_location,
320
-	.fill_device		= t509_fill_device,
321
-	.check_driver		= t509_check_driver,
322
-	.describe_device	= t509_describe_device,
323
-	.name_device		= t509_name_device,
324
-};
325
-
326 239
 /*
327 240
  * Activate a T509 device
328 241
  *
@@ -335,7 +248,7 @@ static inline void activate_t509_device ( struct t509_device *t509 ) {
335 248
 	t509_send_id_sequence ();
336 249
 	t509_select_tag ( t509->tag );
337 250
 	t509_activate ( t509->ioaddr );
338
-	DBG ( "T509 activated device %hhx at ioaddr %hx\n",
251
+	DBG ( "T509 activated device %02x at ioaddr %04x\n",
339 252
 	      t509->tag, t509->ioaddr );
340 253
 }
341 254
 
@@ -353,53 +266,160 @@ static inline void deactivate_t509_device ( struct t509_device *t509 ) {
353 266
 	t509_select_tag ( 0 );
354 267
 	t509_set_tag ( t509->tag );
355 268
 	t509_wait_for_id_sequence ();
356
-	DBG ( "T509 deactivated device at %hx and re-tagged as %hhx\n",
269
+	DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
357 270
 	      t509->ioaddr, t509->tag );
358 271
 }
359 272
 
360
-/*
361
- * Fill in a nic structure
362
- *
363
- * Called only once, so inlined for efficiency
364
- *
365
- */
366
-static inline void t509_fill_nic ( struct nic *nic,
367
-				   struct t509_device *t509 ) {
368
-
369
-	/* Fill in ioaddr and irqno */
370
-	nic->ioaddr = t509->ioaddr;
371
-	nic->irqno = 0;
372
-
373
-	/* Fill in DHCP device ID structure */
374
-	nic->dhcp_dev_id.bus_type = ISA_BUS_TYPE;
375
-	nic->dhcp_dev_id.vendor_id = htons ( MFG_ID );
376
-	nic->dhcp_dev_id.device_id = htons ( PROD_ID );
377
-}
378
-
379 273
 /*
380 274
  * The ISA probe function
381 275
  *
382 276
  */
383
-static int el3_t509_probe ( struct nic *nic, struct t509_device *t509 ) {
277
+static int legacy_t509_probe ( struct nic *nic, void *t509 ) {
384 278
 	
385 279
 	/* We could change t509->ioaddr if we wanted to */
386 280
 	activate_t509_device ( t509 );
387
-	t509_fill_nic ( nic, t509 );
388 281
 
389 282
 	/* Hand off to generic t5x9 probe routine */
390 283
 	return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
391 284
 }
392 285
 
393
-static void el3_t509_disable ( struct nic *nic, struct t509_device *t509 ) {
286
+static void legacy_t509_disable ( struct nic *nic, void *t509 ) {
394 287
 	t5x9_disable ( nic );
395 288
 	deactivate_t509_device ( t509 );
396 289
 }
397 290
 
398
-struct {} el3_t509_driver;
291
+static inline void legacy_t509_set_drvdata ( void *hwdev, void *priv ) {
292
+	t509_set_drvdata ( hwdev, priv );
293
+}
399 294
 
400
-DRIVER ( "3c509", nic_driver, t509_driver, el3_t509_driver,
401
-	 el3_t509_probe, el3_t509_disable );
295
+static inline void * legacy_t509_get_drvdata ( void *hwdev ) {
296
+	return t509_get_drvdata ( hwdev );
297
+}
402 298
 
403
-ISA_ROM ( "3c509", "3c509" );
299
+/**
300
+ * Probe a 3c509 device
301
+ *
302
+ * @v t509		3c509 device
303
+ * @ret rc		Return status code
304
+ *
305
+ * Searches for a driver for the 3c509 device.  If a driver is found,
306
+ * its probe() routine is called.
307
+ */
308
+static int t509_probe ( struct t509_device *t509 ) {
309
+	DBG ( "Adding 3c509 device %02x (I/O %04x)\n",
310
+	      t509->tag, t509->ioaddr );
311
+	return legacy_probe ( t509, legacy_t509_set_drvdata, &t509->dev,
312
+			      legacy_t509_probe, legacy_t509_disable );
313
+}
404 314
 
405
-#endif
315
+/**
316
+ * Remove a 3c509 device
317
+ *
318
+ * @v t509		3c509 device
319
+ */
320
+static void t509_remove ( struct t509_device *t509 ) {
321
+	legacy_remove ( t509, legacy_t509_get_drvdata, legacy_t509_disable );
322
+	DBG ( "Removed 3c509 device %02x\n", t509->tag );
323
+}
324
+
325
+/**
326
+ * Probe 3c509 root bus
327
+ *
328
+ * @v rootdev		3c509 bus root device
329
+ *
330
+ * Scans the 3c509 bus for devices and registers all devices it can
331
+ * find.
332
+ */
333
+static int t509bus_probe ( struct root_device *rootdev ) {
334
+	struct t509_device *t509 = NULL;
335
+	unsigned int tag;
336
+	unsigned int iobase;
337
+	int rc;
338
+
339
+	/* Perform isolation and tagging */
340
+	if ( ( rc = t509_isolate() ) != 0 )
341
+		return rc;
342
+
343
+	for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
344
+		/* Allocate struct t509_device */
345
+		if ( ! t509 )
346
+			t509 = malloc ( sizeof ( *t509 ) );
347
+		if ( ! t509 ) {
348
+			rc = -ENOMEM;
349
+			goto err;
350
+		}
351
+		memset ( t509, 0, sizeof ( *t509 ) );
352
+		t509->tag = tag;
353
+
354
+		/* Send the ID sequence */
355
+		t509_send_id_sequence ();
356
+
357
+		/* Select the specified tag */
358
+		t509_select_tag ( t509->tag );
359
+
360
+		/* Read the default I/O address */
361
+		iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
362
+		t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
363
+
364
+		/* Send card back to ID_WAIT */
365
+		t509_wait_for_id_sequence();
366
+
367
+		/* Add to device hierarchy */
368
+		snprintf ( t509->dev.name, sizeof ( t509->dev.name ),
369
+			   "t509%02x", tag );
370
+		t509->dev.desc.bus_type = BUS_TYPE_ISA;
371
+		t509->dev.desc.vendor = MFG_ID;
372
+		t509->dev.desc.device = PROD_ID;
373
+		t509->dev.parent = &rootdev->dev;
374
+		list_add ( &t509->dev.siblings, &rootdev->dev.children );
375
+		INIT_LIST_HEAD ( &t509->dev.children );
376
+			
377
+		/* Look for a driver */
378
+		if ( t509_probe ( t509 ) == 0 ) {
379
+			/* t509dev registered, we can drop our ref */
380
+			t509 = NULL;
381
+		} else {
382
+			/* Not registered; re-use struct */
383
+			list_del ( &t509->dev.siblings );
384
+		}
385
+	}
386
+
387
+	free ( t509 );
388
+	return 0;
389
+
390
+ err:
391
+	free ( t509 );
392
+	t509bus_remove ( rootdev );
393
+	return rc;
394
+}
395
+
396
+/**
397
+ * Remove 3c509 root bus
398
+ *
399
+ * @v rootdev		3c509 bus root device
400
+ */
401
+static void t509bus_remove ( struct root_device *rootdev ) {
402
+	struct t509_device *t509;
403
+	struct t509_device *tmp;
404
+
405
+	list_for_each_entry_safe ( t509, tmp, &rootdev->dev.children,
406
+				   dev.siblings ) {
407
+		t509_remove ( t509 );
408
+		list_del ( &t509->dev.siblings );
409
+		free ( t509 );
410
+	}
411
+}
412
+
413
+/** 3c509 bus root device driver */
414
+static struct root_driver t509_root_driver = {
415
+	.probe = t509bus_probe,
416
+	.remove = t509bus_remove,
417
+};
418
+
419
+/** 3c509 bus root device */
420
+struct root_device t509_root_device __root_device = {
421
+	.dev = { .name = "3c509" },
422
+	.driver = &t509_root_driver,
423
+};
424
+
425
+ISA_ROM ( "3c509", "3c509" );

Loading…
Cancel
Save