1 /* $NetBSD: elink.c,v 1.17 2009/03/14 15:36:18 dsl 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 2009/03/14 15:36:18 dsl 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_NOWAIT); 89 if (er == NULL) 90 panic("elink_reset: can't allocate state storage"); 91 92 er->er_bus = bus; 93 LIST_INSERT_HEAD(&elink_all_resets, er, er_link); 94 95 /* Haven't reset the cards on this bus, yet. */ 96 bus_space_write_1(iot, ioh, 0, ELINK_RESET); 97 98 out: 99 bus_space_write_1(iot, ioh, 0, 0x00); 100 bus_space_write_1(iot, ioh, 0, 0x00); 101 } 102 103 /* 104 * The `ID sequence' is really just snapshots of an 8-bit CRC register as 0 105 * bits are shifted in. Different board types use different polynomials. 106 * 107 * NOTE: the caller MUST provide an access handle for ELINK_ID_PORT! 108 */ 109 void 110 elink_idseq(bus_space_tag_t iot, bus_space_handle_t ioh, u_char p) 111 { 112 int i; 113 u_char c; 114 115 c = 0xff; 116 for (i = 255; i; i--) { 117 bus_space_write_1(iot, ioh, 0, c); 118 if (c & 0x80) { 119 c <<= 1; 120 c ^= p; 121 } else 122 c <<= 1; 123 } 124 } 125