1/*	$NetBSD: elink.c,v 1.19 2022/09/25 17:11:48 thorpej 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.19 2022/09/25 17:11:48 thorpej Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41
42#include <sys/queue.h>
43#include <sys/kmem.h>
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 */
51struct elink_done_reset {
52	LIST_ENTRY(elink_done_reset)	er_link;
53	int				er_bus;
54};
55static LIST_HEAD(, elink_done_reset) elink_all_resets;
56static 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 */
68void
69elink_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 = kmem_alloc(sizeof(*er), KM_SLEEP);
88	er->er_bus = bus;
89	LIST_INSERT_HEAD(&elink_all_resets, er, er_link);
90
91	/* Haven't reset the cards on this bus, yet. */
92	bus_space_write_1(iot, ioh, 0, ELINK_RESET);
93
94 out:
95	bus_space_write_1(iot, ioh, 0, 0x00);
96	bus_space_write_1(iot, ioh, 0, 0x00);
97}
98
99/*
100 * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0
101 * bits are shifted in.  Different board types use different polynomials.
102 *
103 * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
104 */
105void
106elink_idseq(bus_space_tag_t iot, bus_space_handle_t ioh, u_char p)
107{
108	int i;
109	u_char c;
110
111	c = 0xff;
112	for (i = 255; i; i--) {
113		bus_space_write_1(iot, ioh, 0, c);
114		if (c & 0x80) {
115			c <<= 1;
116			c ^= p;
117		} else
118			c <<= 1;
119	}
120}
121