1*52263b6aSjasper /* $OpenBSD: elink.c,v 1.7 2007/06/29 15:17:02 jasper Exp $ */
24196a412Sderaadt /* $NetBSD: elink.c,v 1.9 1996/05/03 19:06:27 christos Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*
54196a412Sderaadt * Copyright (c) 1996 Jason R. Thorpe. All rights reserved.
6df930be7Sderaadt * Copyright (c) 1994, 1995 Charles Hannum. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt * modification, are permitted provided that the following conditions
10df930be7Sderaadt * are met:
11df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt * documentation and/or other materials provided with the distribution.
16df930be7Sderaadt * 3. All advertising materials mentioning features or use of this software
17df930be7Sderaadt * must display the following acknowledgement:
18df930be7Sderaadt * This product includes software developed by Charles Hannum.
19df930be7Sderaadt * 4. The name of the author may not be used to endorse or promote products
20df930be7Sderaadt * derived from this software without specific prior written permission.
21df930be7Sderaadt *
22df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23df930be7Sderaadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24df930be7Sderaadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25df930be7Sderaadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26df930be7Sderaadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27df930be7Sderaadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28df930be7Sderaadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29df930be7Sderaadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30df930be7Sderaadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31df930be7Sderaadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt */
33df930be7Sderaadt
34df930be7Sderaadt /*
35df930be7Sderaadt * Common code for dealing with 3COM ethernet cards.
36df930be7Sderaadt */
37df930be7Sderaadt
384196a412Sderaadt #include <sys/param.h>
394196a412Sderaadt #include <sys/systm.h>
404196a412Sderaadt #include <sys/malloc.h>
414196a412Sderaadt #include <sys/queue.h>
424196a412Sderaadt
43b9a85264Sniklas #include <machine/bus.h>
444196a412Sderaadt
45df930be7Sderaadt #include <dev/isa/elink.h>
46df930be7Sderaadt
47df930be7Sderaadt /*
484196a412Sderaadt * This list keeps track of which ISAs have gotten an elink_reset().
494196a412Sderaadt */
504196a412Sderaadt struct elink_done_reset {
514196a412Sderaadt LIST_ENTRY(elink_done_reset) er_link;
524196a412Sderaadt int er_bus;
534196a412Sderaadt };
544196a412Sderaadt static LIST_HEAD(, elink_done_reset) elink_all_resets;
554196a412Sderaadt static int elink_all_resets_initialized;
564196a412Sderaadt
574196a412Sderaadt /*
58df930be7Sderaadt * Issue a `global reset' to all cards, and reset the ID state machines. We
59df930be7Sderaadt * have to be careful to do the global reset only once during autoconfig, to
60df930be7Sderaadt * prevent resetting boards that have already been configured.
614196a412Sderaadt *
624196a412Sderaadt * The "bus" argument here is the unit number of the ISA bus, e.g. "0"
634196a412Sderaadt * if the bus is "isa0".
644196a412Sderaadt *
654196a412Sderaadt * NOTE: the caller MUST provide an i/o handle for ELINK_ID_PORT!
66df930be7Sderaadt */
67df930be7Sderaadt void
elink_reset(bus_space_tag_t iot,bus_space_handle_t ioh,int bus)68*52263b6aSjasper elink_reset(bus_space_tag_t iot, bus_space_handle_t ioh, int bus)
69df930be7Sderaadt {
704196a412Sderaadt struct elink_done_reset *er;
71df930be7Sderaadt
724196a412Sderaadt if (elink_all_resets_initialized == 0) {
734196a412Sderaadt LIST_INIT(&elink_all_resets);
744196a412Sderaadt elink_all_resets_initialized = 1;
75df930be7Sderaadt }
764196a412Sderaadt
774196a412Sderaadt /*
784196a412Sderaadt * Reset these cards if we haven't done so already.
794196a412Sderaadt */
809d08f8e5Smiod LIST_FOREACH(er, &elink_all_resets, er_link)
814196a412Sderaadt if (er->er_bus == bus)
824196a412Sderaadt goto out;
834196a412Sderaadt
844196a412Sderaadt /* Mark this bus so we don't do it again. */
854196a412Sderaadt er = (struct elink_done_reset *)malloc(sizeof(struct elink_done_reset),
864196a412Sderaadt M_DEVBUF, M_NOWAIT);
874196a412Sderaadt if (er == NULL)
884196a412Sderaadt panic("elink_reset: can't allocate state storage");
894196a412Sderaadt
904196a412Sderaadt er->er_bus = bus;
914196a412Sderaadt LIST_INSERT_HEAD(&elink_all_resets, er, er_link);
924196a412Sderaadt
934196a412Sderaadt /* Haven't reset the cards on this bus, yet. */
94b9a85264Sniklas bus_space_write_1(iot, ioh, 0, ELINK_RESET);
954196a412Sderaadt
964196a412Sderaadt out:
97b9a85264Sniklas bus_space_write_1(iot, ioh, 0, 0x00);
98b9a85264Sniklas bus_space_write_1(iot, ioh, 0, 0x00);
99df930be7Sderaadt }
100df930be7Sderaadt
101df930be7Sderaadt /*
102df930be7Sderaadt * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0
103df930be7Sderaadt * bits are shifted in. Different board types use different polynomials.
1044196a412Sderaadt *
1054196a412Sderaadt * NOTE: the caller MUST provide an i/o handle for ELINK_ID_PORT!
106df930be7Sderaadt */
107df930be7Sderaadt void
elink_idseq(bus_space_tag_t iot,bus_space_handle_t ioh,u_char p)108*52263b6aSjasper elink_idseq(bus_space_tag_t iot, bus_space_handle_t ioh, u_char p)
109df930be7Sderaadt {
110*52263b6aSjasper int i;
111*52263b6aSjasper u_char c;
112df930be7Sderaadt
113df930be7Sderaadt c = 0xff;
114df930be7Sderaadt for (i = 255; i; i--) {
115b9a85264Sniklas bus_space_write_1(iot, ioh, 0, c);
116df930be7Sderaadt if (c & 0x80) {
117df930be7Sderaadt c <<= 1;
118df930be7Sderaadt c ^= p;
119df930be7Sderaadt } else
120df930be7Sderaadt c <<= 1;
121df930be7Sderaadt }
122df930be7Sderaadt }
123