Add errfile definition for new e1000.c
[gpxe.git] / src / drivers / net / 3c509.c
blobe704cfca1e14d19381d41cde4437e5db7ef8a0da
1 /*
2 * Split out into 3c509.c and 3c5x9.c, to make it possible to build a
3 * 3c529 module without including ISA, ISAPnP and EISA code.
5 */
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <io.h>
12 #include <timer.h>
13 #include <gpxe/device.h>
14 #include <gpxe/isa.h>
15 #include "3c509.h"
18 * 3c509 cards have their own method of contention resolution; this
19 * effectively defines another bus type similar to ISAPnP. Even the
20 * original ISA cards can be programatically mapped to any I/O address
21 * in the range 0x200-0x3e0.
23 * However, there is a small problem: once you've activated a card,
24 * the only ways to deactivate it will also wipe its tag, meaning that
25 * you won't be able to subsequently reactivate it without going
26 * through the whole ID sequence again. The solution we adopt is to
27 * isolate and tag all cards at the start, and to immediately
28 * re-isolate and re-tag a card after disabling it.
32 static void t509bus_remove ( struct root_device *rootdev );
34 static unsigned int t509_id_port = 0;
35 static unsigned int t509_max_tag = 0;
37 /** A 3c509 device */
38 struct t509_device {
39 /** Generic device */
40 struct device dev;
41 /** Tag */
42 unsigned int tag;
43 /** I/O address */
44 uint16_t ioaddr;
45 /** Driver-private data
47 * Use t509_set_drvdata() and t509_get_drvdata() to access
48 * this field.
50 void *priv;
53 /**
54 * Set 3c509 driver-private data
56 * @v t509 3c509 device
57 * @v priv Private data
59 static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
60 t509->priv = priv;
63 /**
64 * Get 3c509 driver-private data
66 * @v t509 3c509 device
67 * @ret priv Private data
69 static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
70 return t509->priv;
74 * t509 utility functions
78 static inline void t509_set_id_port ( void ) {
79 outb ( 0x00, t509_id_port );
82 static inline void t509_wait_for_id_sequence ( void ) {
83 outb ( 0x00, t509_id_port );
86 static inline void t509_global_reset ( void ) {
87 outb ( 0xc0, t509_id_port );
90 static inline void t509_reset_tag ( void ) {
91 outb ( 0xd0, t509_id_port );
94 static inline void t509_set_tag ( uint8_t tag ) {
95 outb ( 0xd0 | tag, t509_id_port );
98 static inline void t509_select_tag ( uint8_t tag ) {
99 outb ( 0xd8 | tag, t509_id_port );
102 static inline void t509_activate ( uint16_t ioaddr ) {
103 outb ( 0xe0 | ( ioaddr >> 4 ), t509_id_port );
106 static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr ) {
107 outb ( GLOBAL_RESET, ioaddr + EP_COMMAND );
110 static inline void t509_load_eeprom_word ( uint8_t offset ) {
111 outb ( 0x80 | offset, t509_id_port );
115 * Find a suitable ID port
118 static inline int t509_find_id_port ( void ) {
120 for ( t509_id_port = EP_ID_PORT_START ;
121 t509_id_port < EP_ID_PORT_END ;
122 t509_id_port += EP_ID_PORT_INC ) {
123 t509_set_id_port ();
124 /* See if anything's listening */
125 outb ( 0xff, t509_id_port );
126 if ( inb ( t509_id_port ) & 0x01 ) {
127 /* Found a suitable port */
128 DBG ( "T509 using ID port at %04x\n", t509_id_port );
129 return 0;
132 /* No id port available */
133 DBG ( "T509 found no available ID port\n" );
134 return -ENOENT;
138 * Send ID sequence to the ID port
141 static void t509_send_id_sequence ( void ) {
142 unsigned short lrs_state, i;
144 t509_set_id_port ();
145 /* Reset IDS on cards */
146 t509_wait_for_id_sequence ();
147 lrs_state = 0xff;
148 for ( i = 0; i < 255; i++ ) {
149 outb ( lrs_state, t509_id_port );
150 lrs_state <<= 1;
151 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
156 * We get eeprom data from the id_port given an offset into the eeprom.
157 * Basically; after the ID_sequence is sent to all of the cards; they enter
158 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
159 * the eeprom data. We then read the port 16 times and with every read; the
160 * cards check for contention (ie: if one card writes a 0 bit and another
161 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
162 * compares the data on the bus; if there is a difference then that card goes
163 * into ID_WAIT state again). In the meantime; one bit of data is returned in
164 * the AX register which is conveniently returned to us by inb(). Hence; we
165 * read 16 times getting one bit of data with each read.
167 static uint16_t t509_id_read_eeprom ( int offset ) {
168 int i, data = 0;
170 t509_load_eeprom_word ( offset );
171 /* Do we really need this wait? Won't be noticeable anyway */
172 udelay(10000);
174 for ( i = 0; i < 16; i++ ) {
175 data = ( data << 1 ) | ( inw ( t509_id_port ) & 1 );
177 return data;
181 * Isolate and tag all t509 cards
184 static int t509_isolate ( void ) {
185 unsigned int i;
186 uint16_t contend[3];
187 int rc;
189 /* Find a suitable ID port */
190 if ( ( rc = t509_find_id_port() ) != 0 )
191 return rc;
193 while ( 1 ) {
195 /* All cards are in ID_WAIT state each time we go
196 * through this loop.
199 /* Send the ID sequence */
200 t509_send_id_sequence();
202 /* First time through, reset all tags. On subsequent
203 * iterations, kill off any already-tagged cards
205 if ( t509_max_tag == 0 ) {
206 t509_reset_tag();
207 } else {
208 t509_select_tag ( 0 );
211 /* Read the manufacturer ID, to see if there are any
212 * more cards
214 if ( t509_id_read_eeprom ( EEPROM_MFG_ID ) != MFG_ID ) {
215 DBG ( "T509 saw %s signs of life\n",
216 t509_max_tag ? "no further" : "no" );
217 break;
220 /* Perform contention selection on the MAC address */
221 for ( i = 0 ; i < 3 ; i++ ) {
222 contend[i] = t509_id_read_eeprom ( i );
225 /* Only one device will still be left alive. Tag it. */
226 ++t509_max_tag;
227 DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
228 contend[0], contend[1], contend[2], t509_max_tag );
229 t509_set_tag ( t509_max_tag );
231 /* Return all cards back to ID_WAIT state */
232 t509_wait_for_id_sequence();
235 DBG ( "T509 found %d cards using ID port %04x\n",
236 t509_max_tag, t509_id_port );
237 return 0;
241 * Activate a T509 device
243 * The device will be enabled at whatever ioaddr is specified in the
244 * struct t509_device; there is no need to stick with the default
245 * ioaddr read from the EEPROM.
248 static inline void activate_t509_device ( struct t509_device *t509 ) {
249 t509_send_id_sequence ();
250 t509_select_tag ( t509->tag );
251 t509_activate ( t509->ioaddr );
252 DBG ( "T509 activated device %02x at ioaddr %04x\n",
253 t509->tag, t509->ioaddr );
257 * Deactivate a T509 device
259 * Disabling also clears the tag, so we immediately isolate and re-tag
260 * this card.
263 static inline void deactivate_t509_device ( struct t509_device *t509 ) {
264 t509_deactivate_and_reset_tag ( t509->ioaddr );
265 udelay ( 1000 );
266 t509_send_id_sequence ();
267 t509_select_tag ( 0 );
268 t509_set_tag ( t509->tag );
269 t509_wait_for_id_sequence ();
270 DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
271 t509->ioaddr, t509->tag );
275 * The ISA probe function
278 static int legacy_t509_probe ( struct nic *nic, void *hwdev ) {
279 struct t509_device *t509 = hwdev;
281 /* We could change t509->ioaddr if we wanted to */
282 activate_t509_device ( t509 );
283 nic->ioaddr = t509->ioaddr;
285 /* Hand off to generic t5x9 probe routine */
286 return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
289 static void legacy_t509_disable ( struct nic *nic, void *hwdev ) {
290 struct t509_device *t509 = hwdev;
292 t5x9_disable ( nic );
293 deactivate_t509_device ( t509 );
296 static inline void legacy_t509_set_drvdata ( void *hwdev, void *priv ) {
297 t509_set_drvdata ( hwdev, priv );
300 static inline void * legacy_t509_get_drvdata ( void *hwdev ) {
301 return t509_get_drvdata ( hwdev );
305 * Probe a 3c509 device
307 * @v t509 3c509 device
308 * @ret rc Return status code
310 * Searches for a driver for the 3c509 device. If a driver is found,
311 * its probe() routine is called.
313 static int t509_probe ( struct t509_device *t509 ) {
314 DBG ( "Adding 3c509 device %02x (I/O %04x)\n",
315 t509->tag, t509->ioaddr );
316 return legacy_probe ( t509, legacy_t509_set_drvdata, &t509->dev,
317 legacy_t509_probe, legacy_t509_disable );
321 * Remove a 3c509 device
323 * @v t509 3c509 device
325 static void t509_remove ( struct t509_device *t509 ) {
326 legacy_remove ( t509, legacy_t509_get_drvdata, legacy_t509_disable );
327 DBG ( "Removed 3c509 device %02x\n", t509->tag );
331 * Probe 3c509 root bus
333 * @v rootdev 3c509 bus root device
335 * Scans the 3c509 bus for devices and registers all devices it can
336 * find.
338 static int t509bus_probe ( struct root_device *rootdev ) {
339 struct t509_device *t509 = NULL;
340 unsigned int tag;
341 unsigned int iobase;
342 int rc;
344 /* Perform isolation and tagging */
345 if ( ( rc = t509_isolate() ) != 0 )
346 return rc;
348 for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
349 /* Allocate struct t509_device */
350 if ( ! t509 )
351 t509 = malloc ( sizeof ( *t509 ) );
352 if ( ! t509 ) {
353 rc = -ENOMEM;
354 goto err;
356 memset ( t509, 0, sizeof ( *t509 ) );
357 t509->tag = tag;
359 /* Send the ID sequence */
360 t509_send_id_sequence ();
362 /* Select the specified tag */
363 t509_select_tag ( t509->tag );
365 /* Read the default I/O address */
366 iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
367 t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
369 /* Send card back to ID_WAIT */
370 t509_wait_for_id_sequence();
372 /* Add to device hierarchy */
373 snprintf ( t509->dev.name, sizeof ( t509->dev.name ),
374 "t509%02x", tag );
375 t509->dev.desc.bus_type = BUS_TYPE_ISA;
376 t509->dev.desc.vendor = MFG_ID;
377 t509->dev.desc.device = PROD_ID;
378 t509->dev.parent = &rootdev->dev;
379 list_add ( &t509->dev.siblings, &rootdev->dev.children );
380 INIT_LIST_HEAD ( &t509->dev.children );
382 /* Look for a driver */
383 if ( t509_probe ( t509 ) == 0 ) {
384 /* t509dev registered, we can drop our ref */
385 t509 = NULL;
386 } else {
387 /* Not registered; re-use struct */
388 list_del ( &t509->dev.siblings );
392 free ( t509 );
393 return 0;
395 err:
396 free ( t509 );
397 t509bus_remove ( rootdev );
398 return rc;
402 * Remove 3c509 root bus
404 * @v rootdev 3c509 bus root device
406 static void t509bus_remove ( struct root_device *rootdev ) {
407 struct t509_device *t509;
408 struct t509_device *tmp;
410 list_for_each_entry_safe ( t509, tmp, &rootdev->dev.children,
411 dev.siblings ) {
412 t509_remove ( t509 );
413 list_del ( &t509->dev.siblings );
414 free ( t509 );
418 /** 3c509 bus root device driver */
419 static struct root_driver t509_root_driver = {
420 .probe = t509bus_probe,
421 .remove = t509bus_remove,
424 /** 3c509 bus root device */
425 struct root_device t509_root_device __root_device = {
426 .dev = { .name = "3c509" },
427 .driver = &t509_root_driver,
430 ISA_ROM ( "3c509", "3c509" );