Home | History | Annotate | Line # | Download | only in isa
elink.c revision 1.16
      1 /*	$NetBSD: elink.c,v 1.16 2008/04/28 20:23:52 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.16 2008/04/28 20:23:52 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(iot, ioh, bus)
     70 	bus_space_tag_t iot;
     71 	bus_space_handle_t ioh;
     72 	int bus;
     73 {
     74 	struct elink_done_reset *er;
     75 
     76 	if (elink_all_resets_initialized == 0) {
     77 		LIST_INIT(&elink_all_resets);
     78 		elink_all_resets_initialized = 1;
     79 	}
     80 
     81 	/*
     82 	 * Reset these cards if we haven't done so already.
     83 	 */
     84 	for (er = elink_all_resets.lh_first; er != NULL;
     85 	    er = er->er_link.le_next)
     86 		if (er->er_bus == bus)
     87 			goto out;
     88 
     89 	/* Mark this bus so we don't do it again. */
     90 	er = (struct elink_done_reset *)malloc(sizeof(struct elink_done_reset),
     91 	    M_DEVBUF, M_NOWAIT);
     92 	if (er == NULL)
     93 		panic("elink_reset: can't allocate state storage");
     94 
     95 	er->er_bus = bus;
     96 	LIST_INSERT_HEAD(&elink_all_resets, er, er_link);
     97 
     98 	/* Haven't reset the cards on this bus, yet. */
     99 	bus_space_write_1(iot, ioh, 0, ELINK_RESET);
    100 
    101  out:
    102 	bus_space_write_1(iot, ioh, 0, 0x00);
    103 	bus_space_write_1(iot, ioh, 0, 0x00);
    104 }
    105 
    106 /*
    107  * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0
    108  * bits are shifted in.  Different board types use different polynomials.
    109  *
    110  * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT!
    111  */
    112 void
    113 elink_idseq(iot, ioh, p)
    114 	bus_space_tag_t iot;
    115 	bus_space_handle_t ioh;
    116 	u_char p;
    117 {
    118 	int i;
    119 	u_char c;
    120 
    121 	c = 0xff;
    122 	for (i = 255; i; i--) {
    123 		bus_space_write_1(iot, ioh, 0, c);
    124 		if (c & 0x80) {
    125 			c <<= 1;
    126 			c ^= p;
    127 		} else
    128 			c <<= 1;
    129 	}
    130 }
    131