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