elink.c revision 1.17.64.1 1 /* $NetBSD: elink.c,v 1.17.64.1 2020/04/13 08:04:22 martin 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Common code for dealing with 3COM ethernet cards.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: elink.c,v 1.17.64.1 2020/04/13 08:04:22 martin Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/queue.h>
43
44 #include <sys/bus.h>
45
46 #include <dev/isa/elink.h>
47
48 /*
49 * This list keeps track of which ISAs have gotten an elink_reset().
50 */
51 struct elink_done_reset {
52 LIST_ENTRY(elink_done_reset) er_link;
53 int er_bus;
54 };
55 static LIST_HEAD(, elink_done_reset) elink_all_resets;
56 static int elink_all_resets_initialized;
57
58 /*
59 * Issue a `global reset' to all cards, and reset the ID state machines. We
60 * have to be careful to do the global reset only once during autoconfig, to
61 * prevent resetting boards that have already been configured.
62 *
63 * The "bus" argument here is the unit number of the ISA bus, e.g. "0"
64 * if the bus is "isa0".
65 *
66 * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
67 */
68 void
69 elink_reset(bus_space_tag_t iot, bus_space_handle_t ioh, int bus)
70 {
71 struct elink_done_reset *er;
72
73 if (elink_all_resets_initialized == 0) {
74 LIST_INIT(&elink_all_resets);
75 elink_all_resets_initialized = 1;
76 }
77
78 /*
79 * Reset these cards if we haven't done so already.
80 */
81 for (er = elink_all_resets.lh_first; er != NULL;
82 er = er->er_link.le_next)
83 if (er->er_bus == bus)
84 goto out;
85
86 /* Mark this bus so we don't do it again. */
87 er = (struct elink_done_reset *)malloc(sizeof(struct elink_done_reset),
88 M_DEVBUF, M_WAITOK);
89 er->er_bus = bus;
90 LIST_INSERT_HEAD(&elink_all_resets, er, er_link);
91
92 /* Haven't reset the cards on this bus, yet. */
93 bus_space_write_1(iot, ioh, 0, ELINK_RESET);
94
95 out:
96 bus_space_write_1(iot, ioh, 0, 0x00);
97 bus_space_write_1(iot, ioh, 0, 0x00);
98 }
99
100 /*
101 * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0
102 * bits are shifted in. Different board types use different polynomials.
103 *
104 * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
105 */
106 void
107 elink_idseq(bus_space_tag_t iot, bus_space_handle_t ioh, u_char p)
108 {
109 int i;
110 u_char c;
111
112 c = 0xff;
113 for (i = 255; i; i--) {
114 bus_space_write_1(iot, ioh, 0, c);
115 if (c & 0x80) {
116 c <<= 1;
117 c ^= p;
118 } else
119 c <<= 1;
120 }
121 }
122