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.
12 #include <gpxe/device.h>
17 * 3c509 cards have their own method of contention resolution; this
18 * effectively defines another bus type similar to ISAPnP. Even the
19 * original ISA cards can be programatically mapped to any I/O address
20 * in the range 0x200-0x3e0.
22 * However, there is a small problem: once you've activated a card,
23 * the only ways to deactivate it will also wipe its tag, meaning that
24 * you won't be able to subsequently reactivate it without going
25 * through the whole ID sequence again. The solution we adopt is to
26 * isolate and tag all cards at the start, and to immediately
27 * re-isolate and re-tag a card after disabling it.
31 static void t509bus_remove ( struct root_device *rootdev );
33 static unsigned int t509_id_port = 0;
34 static unsigned int t509_max_tag = 0;
44 /** Driver-private data
46 * Use t509_set_drvdata() and t509_get_drvdata() to access
53 * Set 3c509 driver-private data
55 * @v t509 3c509 device
56 * @v priv Private data
58 static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
63 * Get 3c509 driver-private data
65 * @v t509 3c509 device
66 * @ret priv Private data
68 static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
73 * t509 utility functions
77 static inline void t509_set_id_port ( void ) {
78 outb ( 0x00, t509_id_port );
81 static inline void t509_wait_for_id_sequence ( void ) {
82 outb ( 0x00, t509_id_port );
85 static inline void t509_global_reset ( void ) {
86 outb ( 0xc0, t509_id_port );
89 static inline void t509_reset_tag ( void ) {
90 outb ( 0xd0, t509_id_port );
93 static inline void t509_set_tag ( uint8_t tag ) {
94 outb ( 0xd0 | tag, t509_id_port );
97 static inline void t509_select_tag ( uint8_t tag ) {
98 outb ( 0xd8 | tag, t509_id_port );
101 static inline void t509_activate ( uint16_t ioaddr ) {
102 outb ( 0xe0 | ( ioaddr >> 4 ), t509_id_port );
105 static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr ) {
106 outb ( GLOBAL_RESET, ioaddr + EP_COMMAND );
109 static inline void t509_load_eeprom_word ( uint8_t offset ) {
110 outb ( 0x80 | offset, t509_id_port );
114 * Find a suitable ID port
117 static inline int t509_find_id_port ( void ) {
119 for ( t509_id_port = EP_ID_PORT_START ;
120 t509_id_port < EP_ID_PORT_END ;
121 t509_id_port += EP_ID_PORT_INC ) {
123 /* See if anything's listening */
124 outb ( 0xff, t509_id_port );
125 if ( inb ( t509_id_port ) & 0x01 ) {
126 /* Found a suitable port */
127 DBG ( "T509 using ID port at %hx\n", t509_id_port );
131 /* No id port available */
132 DBG ( "T509 found no available ID port\n" );
137 * Send ID sequence to the ID port
140 static void t509_send_id_sequence ( void ) {
141 unsigned short lrs_state, i;
144 /* Reset IDS on cards */
145 t509_wait_for_id_sequence ();
147 for ( i = 0; i < 255; i++ ) {
148 outb ( lrs_state, t509_id_port );
150 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
155 * We get eeprom data from the id_port given an offset into the eeprom.
156 * Basically; after the ID_sequence is sent to all of the cards; they enter
157 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
158 * the eeprom data. We then read the port 16 times and with every read; the
159 * cards check for contention (ie: if one card writes a 0 bit and another
160 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
161 * compares the data on the bus; if there is a difference then that card goes
162 * into ID_WAIT state again). In the meantime; one bit of data is returned in
163 * the AX register which is conveniently returned to us by inb(). Hence; we
164 * read 16 times getting one bit of data with each read.
166 static uint16_t t509_id_read_eeprom ( int offset ) {
169 t509_load_eeprom_word ( offset );
170 /* Do we really need this wait? Won't be noticeable anyway */
173 for ( i = 0; i < 16; i++ ) {
174 data = ( data << 1 ) | ( inw ( t509_id_port ) & 1 );
180 * Isolate and tag all t509 cards
183 static int t509_isolate ( void ) {
188 /* Find a suitable ID port */
189 if ( ( rc = t509_find_id_port () ) != 0 )
194 /* All cards are in ID_WAIT state each time we go
198 /* Send the ID sequence */
199 t509_send_id_sequence ();
201 /* First time through, reset all tags. On subsequent
202 * iterations, kill off any already-tagged cards
204 if ( t509_max_tag == 0 ) {
210 /* Read the manufacturer ID, to see if there are any
213 if ( t509_id_read_eeprom ( EEPROM_MFG_ID ) != MFG_ID ) {
214 DBG ( "T509 saw %s signs of life\n",
215 t509_max_tag ? "no further" : "no" );
219 /* Perform contention selection on the MAC address */
220 for ( i = 0 ; i < 3 ; i++ ) {
221 contend[i] = t509_id_read_eeprom ( i );
224 /* Only one device will still be left alive. Tag it. */
226 DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
227 contend[0], contend[1], contend[2], t509_max_tag );
228 t509_set_tag ( t509_max_tag );
230 /* Return all cards back to ID_WAIT state */
231 t509_wait_for_id_sequence();
234 DBG ( "T509 found %d cards using ID port %04x\n",
235 t509_max_tag, t509_id_port );
240 * Activate a T509 device
242 * The device will be enabled at whatever ioaddr is specified in the
243 * struct t509_device; there is no need to stick with the default
244 * ioaddr read from the EEPROM.
247 static inline void activate_t509_device ( struct t509_device *t509 ) {
248 t509_send_id_sequence ();
249 t509_select_tag ( t509->tag );
250 t509_activate ( t509->ioaddr );
251 DBG ( "T509 activated device %02x at ioaddr %04x\n",
252 t509->tag, t509->ioaddr );
256 * Deactivate a T509 device
258 * Disabling also clears the tag, so we immediately isolate and re-tag
262 static inline void deactivate_t509_device ( struct t509_device *t509 ) {
263 t509_deactivate_and_reset_tag ( t509->ioaddr );
265 t509_send_id_sequence ();
266 t509_select_tag ( 0 );
267 t509_set_tag ( t509->tag );
268 t509_wait_for_id_sequence ();
269 DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
270 t509->ioaddr, t509->tag );
274 * The ISA probe function
277 static int legacy_t509_probe ( struct nic *nic, void *t509 ) {
279 /* We could change t509->ioaddr if we wanted to */
280 activate_t509_device ( t509 );
282 /* Hand off to generic t5x9 probe routine */
283 return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
286 static void legacy_t509_disable ( struct nic *nic, void *t509 ) {
287 t5x9_disable ( nic );
288 deactivate_t509_device ( t509 );
291 static inline void legacy_t509_set_drvdata ( void *hwdev, void *priv ) {
292 t509_set_drvdata ( hwdev, priv );
295 static inline void * legacy_t509_get_drvdata ( void *hwdev ) {
296 return t509_get_drvdata ( hwdev );
300 * Probe a 3c509 device
302 * @v t509 3c509 device
303 * @ret rc Return status code
305 * Searches for a driver for the 3c509 device. If a driver is found,
306 * its probe() routine is called.
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 );
316 * Remove a 3c509 device
318 * @v t509 3c509 device
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 );
326 * Probe 3c509 root bus
328 * @v rootdev 3c509 bus root device
330 * Scans the 3c509 bus for devices and registers all devices it can
333 static int t509bus_probe ( struct root_device *rootdev ) {
334 struct t509_device *t509 = NULL;
339 /* Perform isolation and tagging */
340 if ( ( rc = t509_isolate() ) != 0 )
343 for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
344 /* Allocate struct t509_device */
346 t509 = malloc ( sizeof ( *t509 ) );
351 memset ( t509, 0, sizeof ( *t509 ) );
354 /* Send the ID sequence */
355 t509_send_id_sequence ();
357 /* Select the specified tag */
358 t509_select_tag ( t509->tag );
360 /* Read the default I/O address */
361 iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
362 t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
364 /* Send card back to ID_WAIT */
365 t509_wait_for_id_sequence();
367 /* Add to device hierarchy */
368 snprintf ( t509->dev.name, sizeof ( t509->dev.name ),
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 );
377 /* Look for a driver */
378 if ( t509_probe ( t509 ) == 0 ) {
379 /* t509dev registered, we can drop our ref */
382 /* Not registered; re-use struct */
383 list_del ( &t509->dev.siblings );
392 t509bus_remove ( rootdev );
397 * Remove 3c509 root bus
399 * @v rootdev 3c509 bus root device
401 static void t509bus_remove ( struct root_device *rootdev ) {
402 struct t509_device *t509;
403 struct t509_device *tmp;
405 list_for_each_entry_safe ( t509, tmp, &rootdev->dev.children,
407 t509_remove ( t509 );
408 list_del ( &t509->dev.siblings );
413 /** 3c509 bus root device driver */
414 static struct root_driver t509_root_driver = {
415 .probe = t509bus_probe,
416 .remove = t509bus_remove,
419 /** 3c509 bus root device */
420 struct root_device t509_root_device __root_device = {
421 .dev = { .name = "3c509" },
422 .driver = &t509_root_driver,
425 ISA_ROM ( "3c509", "3c509" );