xref: /openbsd-src/sys/dev/sbus/if_hme_sbus.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: if_hme_sbus.c,v 1.13 2009/07/12 21:27:09 kettenis Exp $	*/
2 /*	$NetBSD: if_hme_sbus.c,v 1.6 2001/02/28 14:52:48 mrg Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Paul Kranenburg.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * SBus front-end device driver for the HME ethernet device.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/syslog.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 
44 #include <net/if.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 
48 #ifdef INET
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip.h>
53 #include <netinet/if_ether.h>
54 #endif
55 
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58 
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61 #include <machine/autoconf.h>
62 
63 #include <dev/sbus/sbusvar.h>
64 #include <dev/ic/hmevar.h>
65 #include <dev/ofw/openfirm.h>
66 
67 struct hmesbus_softc {
68 	struct	hme_softc	hsc_hme;	/* HME device */
69 };
70 
71 int	hmematch_sbus(struct device *, void *, void *);
72 void	hmeattach_sbus(struct device *, struct device *, void *);
73 
74 struct cfattach hme_sbus_ca = {
75 	sizeof(struct hmesbus_softc), hmematch_sbus, hmeattach_sbus
76 };
77 
78 int
79 hmematch_sbus(struct device *parent, void *vcf, void *aux)
80 {
81 	struct cfdata *cf = vcf;
82 	struct sbus_attach_args *sa = aux;
83 
84 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
85 	    strcmp("SUNW,qfe", sa->sa_name) == 0 ||
86 	    strcmp("SUNW,hme", sa->sa_name) == 0);
87 }
88 
89 void
90 hmeattach_sbus(struct device *parent, struct device *self, void *aux)
91 {
92 	struct sbus_attach_args *sa = aux;
93 	struct hmesbus_softc *hsc = (void *)self;
94 	struct hme_softc *sc = &hsc->hsc_hme;
95 	u_int32_t burst, sbusburst;
96 	/* XXX the following declaration should be elsewhere */
97 	extern void myetheraddr(u_char *);
98 
99 	/* Pass on the bus tags */
100 	sc->sc_bustag = sa->sa_bustag;
101 	sc->sc_dmatag = sa->sa_dmatag;
102 
103 	if (sa->sa_nintr < 1) {
104 		printf(": no interrupt\n");
105 		return;
106 	}
107 
108 	if (sa->sa_nreg < 5) {
109 		printf(": only %d register sets\n", sa->sa_nreg);
110 		return;
111 	}
112 
113 	/*
114 	 * Map five register banks:
115 	 *
116 	 *	bank 0: HME SEB registers
117 	 *	bank 1: HME ETX registers
118 	 *	bank 2: HME ERX registers
119 	 *	bank 3: HME MAC registers
120 	 *	bank 4: HME MIF registers
121 	 *
122 	 */
123 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
124 	    (bus_addr_t)sa->sa_reg[0].sbr_offset,
125 	    (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0, &sc->sc_seb) != 0) {
126 		printf(": can't map registers\n");
127 		return;
128 	}
129 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[1].sbr_slot,
130 	    (bus_addr_t)sa->sa_reg[1].sbr_offset,
131 	    (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0, &sc->sc_etx) != 0) {
132 		printf(": can't map registers\n");
133 		return;
134 	}
135 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[2].sbr_slot,
136 	    (bus_addr_t)sa->sa_reg[2].sbr_offset,
137 	    (bus_size_t)sa->sa_reg[2].sbr_size, 0, 0, &sc->sc_erx) != 0) {
138 		printf(": can't map registers\n");
139 		return;
140 	}
141 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[3].sbr_slot,
142 	    (bus_addr_t)sa->sa_reg[3].sbr_offset,
143 	    (bus_size_t)sa->sa_reg[3].sbr_size, 0, 0, &sc->sc_mac) != 0) {
144 		printf(": can't map registers\n");
145 		return;
146 	}
147 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[4].sbr_slot,
148 	    (bus_addr_t)sa->sa_reg[4].sbr_offset,
149 	    (bus_size_t)sa->sa_reg[4].sbr_size, 0, 0, &sc->sc_mif) != 0) {
150 		printf(": can't map registers\n");
151 		return;
152 	}
153 
154 	if (OF_getprop(sa->sa_node, "local-mac-address",
155 	    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
156 		myetheraddr(sc->sc_arpcom.ac_enaddr);
157 
158 	/*
159 	 * Get transfer burst size from PROM and pass it on
160 	 * to the back-end driver.
161 	 */
162 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
163 	if (sbusburst == 0)
164 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
165 
166 	burst = getpropint(sa->sa_node, "burst-sizes", -1);
167 	if (burst == -1)
168 		/* take SBus burst sizes */
169 		burst = sbusburst;
170 
171 	/* Clamp at parent's burst sizes */
172 	burst &= sbusburst;
173 
174 	/* Translate into plain numerical format */
175 	if ((burst & SBUS_BURST_64))
176 		sc->sc_burst = 64;
177 	else if ((burst & SBUS_BURST_32))
178 		sc->sc_burst = 32;
179 	else if ((burst & SBUS_BURST_16))
180 		sc->sc_burst = 16;
181 	else
182 		sc->sc_burst = 0;
183 
184 	sc->sc_pci = 0; /* XXXXX should all be done in bus_dma. */
185 
186 	/* Establish interrupt handler */
187 	bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0, hme_intr,
188 	    sc, self->dv_xname);
189 
190 	hme_config(sc);
191 }
192