xref: /netbsd-src/sys/dev/isa/elink.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: elink.c,v 1.13 2000/03/30 12:45:32 augustss Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe and Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Common code for dealing with 3COM ethernet cards.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 
48 #include <machine/bus.h>
49 
50 #include <dev/isa/elink.h>
51 
52 /*
53  * This list keeps track of which ISAs have gotten an elink_reset().
54  */
55 struct elink_done_reset {
56 	LIST_ENTRY(elink_done_reset)	er_link;
57 	int				er_bus;
58 };
59 static LIST_HEAD(, elink_done_reset) elink_all_resets;
60 static int elink_all_resets_initialized;
61 
62 /*
63  * Issue a `global reset' to all cards, and reset the ID state machines.  We
64  * have to be careful to do the global reset only once during autoconfig, to
65  * prevent resetting boards that have already been configured.
66  *
67  * The "bus" argument here is the unit number of the ISA bus, e.g. "0"
68  * if the bus is "isa0".
69  *
70  * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
71  */
72 void
73 elink_reset(iot, ioh, bus)
74 	bus_space_tag_t iot;
75 	bus_space_handle_t ioh;
76 	int bus;
77 {
78 	struct elink_done_reset *er;
79 
80 	if (elink_all_resets_initialized == 0) {
81 		LIST_INIT(&elink_all_resets);
82 		elink_all_resets_initialized = 1;
83 	}
84 
85 	/*
86 	 * Reset these cards if we haven't done so already.
87 	 */
88 	for (er = elink_all_resets.lh_first; er != NULL;
89 	    er = er->er_link.le_next)
90 		if (er->er_bus == bus)
91 			goto out;
92 
93 	/* Mark this bus so we don't do it again. */
94 	er = (struct elink_done_reset *)malloc(sizeof(struct elink_done_reset),
95 	    M_DEVBUF, M_NOWAIT);
96 	if (er == NULL)
97 		panic("elink_reset: can't allocate state storage");
98 
99 	er->er_bus = bus;
100 	LIST_INSERT_HEAD(&elink_all_resets, er, er_link);
101 
102 	/* Haven't reset the cards on this bus, yet. */
103 	bus_space_write_1(iot, ioh, 0, ELINK_RESET);
104 
105  out:
106 	bus_space_write_1(iot, ioh, 0, 0x00);
107 	bus_space_write_1(iot, ioh, 0, 0x00);
108 }
109 
110 /*
111  * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0
112  * bits are shifted in.  Different board types use different polynomials.
113  *
114  * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
115  */
116 void
117 elink_idseq(iot, ioh, p)
118 	bus_space_tag_t iot;
119 	bus_space_handle_t ioh;
120 	u_char p;
121 {
122 	int i;
123 	u_char c;
124 
125 	c = 0xff;
126 	for (i = 255; i; i--) {
127 		bus_space_write_1(iot, ioh, 0, c);
128 		if (c & 0x80) {
129 			c <<= 1;
130 			c ^= p;
131 		} else
132 			c <<= 1;
133 	}
134 }
135