1 /* $OpenBSD: if_tr_isa.c,v 1.3 2002/03/14 01:26:56 millert Exp $ */
2 /* $NetBSD: if_tr_isa.c,v 1.4 1999/04/30 15:29:24 bad Exp $ */
3
4 #undef TRISADEBUG
5 /*
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Onno van der Linden.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by The NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <sys/device.h>
45 #include <sys/timeout.h>
46
47 #include <net/if.h>
48 #include <net/if_media.h>
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51
52 #include <machine/cpu.h>
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55
56 #include <dev/isa/isavar.h>
57
58 #include <dev/ic/tropicreg.h>
59 #include <dev/ic/tropicvar.h>
60
61
62 int tr_isa_probe(struct device *, void *, void *);
63 int trtcm_isa_probe(struct device *, void *, void *);
64 int tribm_isa_probe(struct device *, void *, void *);
65 void tr_isa_attach(struct device *, struct device *, void *);
66 int tr_isa_map_io(struct isa_attach_args *, bus_space_handle_t *,
67 bus_space_handle_t *);
68 void tr_isa_unmap_io(struct isa_attach_args *, bus_space_handle_t,
69 bus_space_handle_t);
70 int trtcm_isa_mediachange(struct tr_softc *);
71 void trtcm_isa_mediastatus(struct tr_softc *, struct ifmediareq *);
72 #ifdef TRISADEBUG
73 void tr_isa_dumpaip(bus_space_tag_t, bus_space_handle_t);
74 #endif
75
76 /*
77 * List of manufacturer specific probe routines. Order is important.
78 */
79 int (*tr_isa_probe_list[])(struct device *, void *, void *) = {
80 trtcm_isa_probe,
81 tribm_isa_probe,
82 0
83 };
84
85 struct cfattach tr_isa_ca = {
86 sizeof(struct tr_softc), tr_isa_probe, tr_isa_attach
87 };
88
89 int
tr_isa_map_io(ia,pioh,mmioh)90 tr_isa_map_io(ia, pioh, mmioh)
91 struct isa_attach_args *ia;
92 bus_space_handle_t *pioh, *mmioh;
93 {
94 bus_size_t mmio;
95 u_int8_t s;
96
97 if (bus_space_map(ia->ia_iot, ia->ia_iobase, ia->ia_iosize, 0, pioh)) {
98 printf("tr_isa_map_io: can't map PIO ports\n");
99 return 1;
100 }
101
102 /* Read adapter switches and calculate addresses of MMIO. */
103 s = bus_space_read_1(ia->ia_iot, *pioh, TR_SWITCH);
104
105 if ((s & 0xfc) < ((TR_MMIO_MINADDR - TR_MMIO_OFFSET) >> 11) ||
106 (s & 0xfc) > ((TR_MMIO_MAXADDR - TR_MMIO_OFFSET) >> 11)) {
107 bus_space_unmap(ia->ia_iot, *pioh, ia->ia_iosize);
108 return 1;
109 }
110
111 mmio = ((s & 0xfc) << 11) + TR_MMIO_OFFSET;
112 if (bus_space_map(ia->ia_memt, mmio, TR_MMIO_SIZE, 0, mmioh)) {
113 printf("tr_isa_map_io: can't map MMIO region 0x%05lx/%d\n",
114 mmio, TR_MMIO_SIZE);
115 bus_space_unmap(ia->ia_iot, *pioh, ia->ia_iosize);
116 return 1;
117 }
118 return 0;
119 }
120
121 void
tr_isa_unmap_io(ia,pioh,mmioh)122 tr_isa_unmap_io(ia, pioh, mmioh)
123 struct isa_attach_args *ia;
124 bus_space_handle_t pioh, mmioh;
125 {
126 bus_space_unmap(ia->ia_memt, mmioh, TR_MMIO_SIZE);
127 bus_space_unmap(ia->ia_iot, pioh, ia->ia_iosize);
128 }
129
130 static u_char tr_isa_id[] = {
131 5, 0, 4, 9, 4, 3, 4, 15, 3, 6, 3, 1, 3, 1, 3, 0, 3, 9, 3, 9, 3, 0, 2, 0
132 };
133
134 /*
135 * XXX handle multiple IBM TR cards (sram mapping !!)
136 */
137
138 int
tr_isa_probe(parent,match,aux)139 tr_isa_probe(parent, match, aux)
140 struct device *parent;
141 void *match, *aux;
142 {
143 struct isa_attach_args *ia = aux;
144 int i;
145 bus_size_t tr_id;
146 bus_space_handle_t sramh, pioh, mmioh;
147 int probecode;
148 int matched = 0;
149
150 for (i = 0; tr_isa_probe_list[i] != 0; i++) {
151 probecode = tr_isa_probe_list[i](parent, match, aux);
152 if (probecode < 0)
153 return 0; /* Fail instantly. */
154 if (probecode > 0)
155 break; /* We have a match. */
156 }
157 if (tr_isa_probe_list[i] == 0)
158 return 0; /* Nothing matched. */
159 if (tr_isa_map_io(ia, &pioh, &mmioh))
160 return 0;
161 tr_id = TR_ID_OFFSET;
162 matched = 1;
163 for (i = 0; i < sizeof(tr_isa_id); i++) {
164 if (bus_space_read_1(ia->ia_memt, mmioh, tr_id) !=
165 tr_isa_id[i])
166 matched = 0;
167 tr_id += 2;
168 }
169 #ifdef TRISADEBUG
170 tr_isa_dumpaip(ia->ia_memt, mmioh);
171 #endif
172 tr_isa_unmap_io(ia, pioh, mmioh);
173 if (!matched) {
174 return 0;
175 }
176 if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &sramh)) {
177 printf("tr_isa_probe: can't map shared ram\n");
178 return 0;
179 }
180 bus_space_unmap(ia->ia_memt, sramh, ia->ia_msize);
181 return 1;
182 }
183
184 int trtcm_setspeed(struct tr_softc *, int);
185
186 void
tr_isa_attach(parent,self,aux)187 tr_isa_attach(parent, self, aux)
188 struct device *parent, *self;
189 void *aux;
190 {
191 struct tr_softc *sc = (void *) self;
192 struct isa_attach_args *ia = aux;
193
194 sc->sc_piot = ia->ia_iot;
195 sc->sc_memt = ia->ia_memt;
196 if (tr_isa_map_io(ia, &sc->sc_pioh, &sc->sc_mmioh)) {
197 printf("tr_isa_attach: IO space vanished\n");
198 return;
199 }
200 if (bus_space_map(sc->sc_memt, ia->ia_maddr, ia->ia_msize, 0,
201 &sc->sc_sramh)) {
202 printf("tr_isa_attach: shared ram space vanished\n");
203 return;
204 }
205 /* set ACA offset */
206 sc->sc_aca = TR_ACA_OFFSET;
207 sc->sc_memwinsz = ia->ia_msize;
208 sc->sc_maddr = ia->ia_maddr;
209 /*
210 * Determine total RAM on adapter and decide how much to use.
211 * XXX Since we don't use RAM paging, use sc_memwinsz for now.
212 */
213 sc->sc_memsize = sc->sc_memwinsz;
214 sc->sc_memreserved = 0;
215
216 if (tr_reset(sc) != 0)
217 return;
218
219 if (ia->ia_aux != NULL) {
220 sc->sc_mediastatus = trtcm_isa_mediastatus;
221 sc->sc_mediachange = trtcm_isa_mediachange;
222 }
223 else {
224 sc->sc_mediastatus = NULL;
225 sc->sc_mediachange = NULL;
226 }
227
228 if (tr_attach(sc) != 0)
229 return;
230
231 /*
232 * XXX 3Com 619 can use LEVEL intr
233 */
234 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
235 IPL_NET, tr_intr, sc, sc->sc_dev.dv_xname);
236 }
237
238 #ifdef TRISADEBUG
239 /*
240 * Dump the adapters AIP
241 */
242 void
tr_isa_dumpaip(memt,mmioh)243 tr_isa_dumpaip(memt, mmioh)
244 bus_space_tag_t memt;
245 bus_space_handle_t mmioh;
246 {
247 unsigned int off, val;
248 printf("AIP contents:");
249 for (off=0; off < 256; off++) {
250 val = bus_space_read_1(memt, mmioh, TR_MAC_OFFSET + off);
251 if ((off % 16) == 0)
252 printf("\n");
253 printf("%02x ", val);
254 }
255 printf("\n");
256 }
257 #endif
258