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.
15 #include <gpxe/device.h>
20 * 3c509 cards have their own method of contention resolution; this
21 * effectively defines another bus type similar to ISAPnP. Even the
22 * original ISA cards can be programatically mapped to any I/O address
23 * in the range 0x200-0x3e0.
25 * However, there is a small problem: once you've activated a card,
26 * the only ways to deactivate it will also wipe its tag, meaning that
27 * you won't be able to subsequently reactivate it without going
28 * through the whole ID sequence again. The solution we adopt is to
29 * isolate and tag all cards at the start, and to immediately
30 * re-isolate and re-tag a card after disabling it.
34 static void t509bus_remove ( struct root_device
*rootdev
);
36 static unsigned int t509_id_port
= 0;
37 static unsigned int t509_max_tag
= 0;
47 /** Driver-private data
49 * Use t509_set_drvdata() and t509_get_drvdata() to access
56 * Set 3c509 driver-private data
58 * @v t509 3c509 device
59 * @v priv Private data
61 static inline void t509_set_drvdata ( struct t509_device
*t509
, void *priv
) {
66 * Get 3c509 driver-private data
68 * @v t509 3c509 device
69 * @ret priv Private data
71 static inline void * t509_get_drvdata ( struct t509_device
*t509
) {
76 * t509 utility functions
80 static inline void t509_set_id_port ( void ) {
81 outb ( 0x00, t509_id_port
);
84 static inline void t509_wait_for_id_sequence ( void ) {
85 outb ( 0x00, t509_id_port
);
88 static inline void t509_global_reset ( void ) {
89 outb ( 0xc0, t509_id_port
);
92 static inline void t509_reset_tag ( void ) {
93 outb ( 0xd0, t509_id_port
);
96 static inline void t509_set_tag ( uint8_t tag
) {
97 outb ( 0xd0 | tag
, t509_id_port
);
100 static inline void t509_select_tag ( uint8_t tag
) {
101 outb ( 0xd8 | tag
, t509_id_port
);
104 static inline void t509_activate ( uint16_t ioaddr
) {
105 outb ( 0xe0 | ( ioaddr
>> 4 ), t509_id_port
);
108 static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr
) {
109 outb ( GLOBAL_RESET
, ioaddr
+ EP_COMMAND
);
112 static inline void t509_load_eeprom_word ( uint8_t offset
) {
113 outb ( 0x80 | offset
, t509_id_port
);
117 * Find a suitable ID port
120 static inline int t509_find_id_port ( void ) {
122 for ( t509_id_port
= EP_ID_PORT_START
;
123 t509_id_port
< EP_ID_PORT_END
;
124 t509_id_port
+= EP_ID_PORT_INC
) {
126 /* See if anything's listening */
127 outb ( 0xff, t509_id_port
);
128 if ( inb ( t509_id_port
) & 0x01 ) {
129 /* Found a suitable port */
130 DBG ( "T509 using ID port at %04x\n", t509_id_port
);
134 /* No id port available */
135 DBG ( "T509 found no available ID port\n" );
140 * Send ID sequence to the ID port
143 static void t509_send_id_sequence ( void ) {
144 unsigned short lrs_state
, i
;
147 /* Reset IDS on cards */
148 t509_wait_for_id_sequence ();
150 for ( i
= 0; i
< 255; i
++ ) {
151 outb ( lrs_state
, t509_id_port
);
153 lrs_state
= lrs_state
& 0x100 ? lrs_state
^ 0xcf : lrs_state
;
158 * We get eeprom data from the id_port given an offset into the eeprom.
159 * Basically; after the ID_sequence is sent to all of the cards; they enter
160 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
161 * the eeprom data. We then read the port 16 times and with every read; the
162 * cards check for contention (ie: if one card writes a 0 bit and another
163 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
164 * compares the data on the bus; if there is a difference then that card goes
165 * into ID_WAIT state again). In the meantime; one bit of data is returned in
166 * the AX register which is conveniently returned to us by inb(). Hence; we
167 * read 16 times getting one bit of data with each read.
169 static uint16_t t509_id_read_eeprom ( int offset
) {
172 t509_load_eeprom_word ( offset
);
173 /* Do we really need this wait? Won't be noticeable anyway */
176 for ( i
= 0; i
< 16; i
++ ) {
177 data
= ( data
<< 1 ) | ( inw ( t509_id_port
) & 1 );
183 * Isolate and tag all t509 cards
186 static int t509_isolate ( void ) {
191 /* Find a suitable ID port */
192 if ( ( rc
= t509_find_id_port() ) != 0 )
197 /* All cards are in ID_WAIT state each time we go
201 /* Send the ID sequence */
202 t509_send_id_sequence();
204 /* First time through, reset all tags. On subsequent
205 * iterations, kill off any already-tagged cards
207 if ( t509_max_tag
== 0 ) {
210 t509_select_tag ( 0 );
213 /* Read the manufacturer ID, to see if there are any
216 if ( t509_id_read_eeprom ( EEPROM_MFG_ID
) != MFG_ID
) {
217 DBG ( "T509 saw %s signs of life\n",
218 t509_max_tag
? "no further" : "no" );
222 /* Perform contention selection on the MAC address */
223 for ( i
= 0 ; i
< 3 ; i
++ ) {
224 contend
[i
] = t509_id_read_eeprom ( i
);
227 /* Only one device will still be left alive. Tag it. */
229 DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
230 contend
[0], contend
[1], contend
[2], t509_max_tag
);
231 t509_set_tag ( t509_max_tag
);
233 /* Return all cards back to ID_WAIT state */
234 t509_wait_for_id_sequence();
237 DBG ( "T509 found %d cards using ID port %04x\n",
238 t509_max_tag
, t509_id_port
);
243 * Activate a T509 device
245 * The device will be enabled at whatever ioaddr is specified in the
246 * struct t509_device; there is no need to stick with the default
247 * ioaddr read from the EEPROM.
250 static inline void activate_t509_device ( struct t509_device
*t509
) {
251 t509_send_id_sequence ();
252 t509_select_tag ( t509
->tag
);
253 t509_activate ( t509
->ioaddr
);
254 DBG ( "T509 activated device %02x at ioaddr %04x\n",
255 t509
->tag
, t509
->ioaddr
);
259 * Deactivate a T509 device
261 * Disabling also clears the tag, so we immediately isolate and re-tag
265 static inline void deactivate_t509_device ( struct t509_device
*t509
) {
266 t509_deactivate_and_reset_tag ( t509
->ioaddr
);
268 t509_send_id_sequence ();
269 t509_select_tag ( 0 );
270 t509_set_tag ( t509
->tag
);
271 t509_wait_for_id_sequence ();
272 DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
273 t509
->ioaddr
, t509
->tag
);
277 * The ISA probe function
280 static int legacy_t509_probe ( struct nic
*nic
, void *hwdev
) {
281 struct t509_device
*t509
= hwdev
;
283 /* We could change t509->ioaddr if we wanted to */
284 activate_t509_device ( t509
);
285 nic
->ioaddr
= t509
->ioaddr
;
287 /* Hand off to generic t5x9 probe routine */
288 return t5x9_probe ( nic
, ISA_PROD_ID ( PROD_ID
), ISA_PROD_ID_MASK
);
291 static void legacy_t509_disable ( struct nic
*nic
, void *hwdev
) {
292 struct t509_device
*t509
= hwdev
;
294 t5x9_disable ( nic
);
295 deactivate_t509_device ( t509
);
298 static inline void legacy_t509_set_drvdata ( void *hwdev
, void *priv
) {
299 t509_set_drvdata ( hwdev
, priv
);
302 static inline void * legacy_t509_get_drvdata ( void *hwdev
) {
303 return t509_get_drvdata ( hwdev
);
307 * Probe a 3c509 device
309 * @v t509 3c509 device
310 * @ret rc Return status code
312 * Searches for a driver for the 3c509 device. If a driver is found,
313 * its probe() routine is called.
315 static int t509_probe ( struct t509_device
*t509
) {
316 DBG ( "Adding 3c509 device %02x (I/O %04x)\n",
317 t509
->tag
, t509
->ioaddr
);
318 return legacy_probe ( t509
, legacy_t509_set_drvdata
, &t509
->dev
,
319 legacy_t509_probe
, legacy_t509_disable
);
323 * Remove a 3c509 device
325 * @v t509 3c509 device
327 static void t509_remove ( struct t509_device
*t509
) {
328 legacy_remove ( t509
, legacy_t509_get_drvdata
, legacy_t509_disable
);
329 DBG ( "Removed 3c509 device %02x\n", t509
->tag
);
333 * Probe 3c509 root bus
335 * @v rootdev 3c509 bus root device
337 * Scans the 3c509 bus for devices and registers all devices it can
340 static int t509bus_probe ( struct root_device
*rootdev
) {
341 struct t509_device
*t509
= NULL
;
346 /* Perform isolation and tagging */
347 if ( ( rc
= t509_isolate() ) != 0 )
350 for ( tag
= 1 ; tag
<= t509_max_tag
; tag
++ ) {
351 /* Allocate struct t509_device */
353 t509
= malloc ( sizeof ( *t509
) );
358 memset ( t509
, 0, sizeof ( *t509
) );
361 /* Send the ID sequence */
362 t509_send_id_sequence ();
364 /* Select the specified tag */
365 t509_select_tag ( t509
->tag
);
367 /* Read the default I/O address */
368 iobase
= t509_id_read_eeprom ( EEPROM_ADDR_CFG
);
369 t509
->ioaddr
= 0x200 + ( ( iobase
& 0x1f ) << 4 );
371 /* Send card back to ID_WAIT */
372 t509_wait_for_id_sequence();
374 /* Add to device hierarchy */
375 snprintf ( t509
->dev
.name
, sizeof ( t509
->dev
.name
),
377 t509
->dev
.desc
.bus_type
= BUS_TYPE_ISA
;
378 t509
->dev
.desc
.vendor
= MFG_ID
;
379 t509
->dev
.desc
.device
= PROD_ID
;
380 t509
->dev
.parent
= &rootdev
->dev
;
381 list_add ( &t509
->dev
.siblings
, &rootdev
->dev
.children
);
382 INIT_LIST_HEAD ( &t509
->dev
.children
);
384 /* Look for a driver */
385 if ( t509_probe ( t509
) == 0 ) {
386 /* t509dev registered, we can drop our ref */
389 /* Not registered; re-use struct */
390 list_del ( &t509
->dev
.siblings
);
399 t509bus_remove ( rootdev
);
404 * Remove 3c509 root bus
406 * @v rootdev 3c509 bus root device
408 static void t509bus_remove ( struct root_device
*rootdev
) {
409 struct t509_device
*t509
;
410 struct t509_device
*tmp
;
412 list_for_each_entry_safe ( t509
, tmp
, &rootdev
->dev
.children
,
414 t509_remove ( t509
);
415 list_del ( &t509
->dev
.siblings
);
420 /** 3c509 bus root device driver */
421 static struct root_driver t509_root_driver
= {
422 .probe
= t509bus_probe
,
423 .remove
= t509bus_remove
,
426 /** 3c509 bus root device */
427 struct root_device t509_root_device __root_device
= {
428 .dev
= { .name
= "3c509" },
429 .driver
= &t509_root_driver
,
432 ISA_ROM ( "3c509", "3c509" );