if_ie_obio.c revision 1.44 1 1.44 thorpej /* $NetBSD: if_ie_obio.c,v 1.44 2023/12/20 05:33:18 thorpej Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 pk * All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pk * by Paul Kranenburg.
9 1.1 pk *
10 1.1 pk * Redistribution and use in source and binary forms, with or without
11 1.1 pk * modification, are permitted provided that the following conditions
12 1.1 pk * are met:
13 1.1 pk * 1. Redistributions of source code must retain the above copyright
14 1.1 pk * notice, this list of conditions and the following disclaimer.
15 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pk * notice, this list of conditions and the following disclaimer in the
17 1.1 pk * documentation and/or other materials provided with the distribution.
18 1.1 pk *
19 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pk */
31 1.1 pk
32 1.38 chuck /*
33 1.1 pk * Copyright (c) 1995 Charles D. Cranor
34 1.1 pk * All rights reserved.
35 1.1 pk *
36 1.1 pk * Redistribution and use in source and binary forms, with or without
37 1.1 pk * modification, are permitted provided that the following conditions
38 1.1 pk * are met:
39 1.1 pk * 1. Redistributions of source code must retain the above copyright
40 1.1 pk * notice, this list of conditions and the following disclaimer.
41 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 pk * notice, this list of conditions and the following disclaimer in the
43 1.1 pk * documentation and/or other materials provided with the distribution.
44 1.1 pk *
45 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 1.1 pk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 1.1 pk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 1.1 pk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 1.1 pk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 1.1 pk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 1.1 pk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 1.1 pk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 1.1 pk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 1.1 pk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 1.1 pk */
56 1.1 pk
57 1.1 pk
58 1.1 pk
59 1.1 pk /*
60 1.1 pk * Sparc OBIO front-end for the Intel 82586 Ethernet driver
61 1.1 pk *
62 1.1 pk * Converted to SUN ie driver by Charles D. Cranor,
63 1.1 pk * October 1994, January 1995.
64 1.1 pk */
65 1.1 pk
66 1.1 pk /*
67 1.1 pk * The i82586 is a very painful chip, found in sun3's, sun-4/100's
68 1.1 pk * sun-4/200's, and VME based suns. The byte order is all wrong for a
69 1.1 pk * SUN, making life difficult. Programming this chip is mostly the same,
70 1.1 pk * but certain details differ from system to system. This driver is
71 1.43 andvar * written so that different "ie" interfaces can be controlled by the same
72 1.1 pk * driver.
73 1.1 pk */
74 1.30 lukem
75 1.30 lukem #include <sys/cdefs.h>
76 1.44 thorpej __KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.44 2023/12/20 05:33:18 thorpej Exp $");
77 1.1 pk
78 1.1 pk #include <sys/param.h>
79 1.1 pk #include <sys/systm.h>
80 1.1 pk #include <sys/errno.h>
81 1.1 pk #include <sys/device.h>
82 1.1 pk #include <sys/protosw.h>
83 1.1 pk #include <sys/socket.h>
84 1.1 pk
85 1.1 pk #include <net/if.h>
86 1.1 pk #include <net/if_types.h>
87 1.1 pk #include <net/if_dl.h>
88 1.1 pk #include <net/if_media.h>
89 1.1 pk #include <net/if_ether.h>
90 1.1 pk
91 1.16 mrg #include <uvm/uvm_extern.h>
92 1.1 pk
93 1.40 dyoung #include <sys/bus.h>
94 1.17 pk #include <machine/intr.h>
95 1.1 pk #include <machine/autoconf.h>
96 1.1 pk
97 1.1 pk #include <dev/ic/i82586reg.h>
98 1.1 pk #include <dev/ic/i82586var.h>
99 1.1 pk
100 1.1 pk /*
101 1.1 pk * the on-board interface
102 1.1 pk */
103 1.1 pk struct ieob {
104 1.1 pk u_char obctrl;
105 1.1 pk };
106 1.1 pk #define IEOB_NORSET 0x80 /* don't reset the board */
107 1.1 pk #define IEOB_ONAIR 0x40 /* put us on the air */
108 1.1 pk #define IEOB_ATTEN 0x20 /* attention! */
109 1.1 pk #define IEOB_IENAB 0x10 /* interrupt enable */
110 1.1 pk #define IEOB_XXXXX 0x08 /* free bit */
111 1.1 pk #define IEOB_XCVRL2 0x04 /* level 2 transceiver? */
112 1.1 pk #define IEOB_BUSERR 0x02 /* bus error */
113 1.1 pk #define IEOB_INT 0x01 /* interrupt */
114 1.1 pk
115 1.1 pk #define IEOB_ADBASE 0xff000000 /* KVA base addr of 24 bit address space */
116 1.1 pk
117 1.1 pk
118 1.33 uwe static void ie_obreset(struct ie_softc *, int);
119 1.33 uwe static void ie_obattend(struct ie_softc *, int);
120 1.33 uwe static void ie_obrun(struct ie_softc *);
121 1.1 pk
122 1.37 tsutsui int ie_obio_match(device_t, cfdata_t, void *);
123 1.37 tsutsui void ie_obio_attach(device_t, device_t, void *);
124 1.1 pk
125 1.39 tsutsui CFATTACH_DECL_NEW(ie_obio, sizeof(struct ie_softc),
126 1.27 thorpej ie_obio_match, ie_obio_attach, NULL, NULL);
127 1.1 pk
128 1.1 pk /* Supported media */
129 1.1 pk static int media[] = {
130 1.1 pk IFM_ETHER | IFM_10_2,
131 1.33 uwe };
132 1.42 msaitoh #define NMEDIA __arraycount(media)
133 1.1 pk
134 1.1 pk
135 1.1 pk /*
136 1.1 pk * OBIO ie support routines
137 1.1 pk */
138 1.33 uwe static void
139 1.33 uwe ie_obreset(struct ie_softc *sc, int what)
140 1.1 pk {
141 1.1 pk volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
142 1.33 uwe
143 1.1 pk ieo->obctrl = 0;
144 1.1 pk delay(100); /* XXX could be shorter? */
145 1.1 pk ieo->obctrl = IEOB_NORSET;
146 1.1 pk }
147 1.33 uwe
148 1.33 uwe static void
149 1.33 uwe ie_obattend(struct ie_softc *sc, int why)
150 1.1 pk {
151 1.1 pk volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
152 1.1 pk
153 1.1 pk ieo->obctrl |= IEOB_ATTEN; /* flag! */
154 1.1 pk ieo->obctrl &= ~IEOB_ATTEN; /* down. */
155 1.1 pk }
156 1.1 pk
157 1.33 uwe static void
158 1.33 uwe ie_obrun(struct ie_softc *sc)
159 1.1 pk {
160 1.1 pk volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
161 1.1 pk
162 1.1 pk ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
163 1.1 pk }
164 1.1 pk
165 1.33 uwe void ie_obio_memcopyin(struct ie_softc *, void *, int, size_t);
166 1.33 uwe void ie_obio_memcopyout(struct ie_softc *, const void *, int, size_t);
167 1.1 pk
168 1.1 pk /*
169 1.1 pk * Copy board memory to kernel.
170 1.1 pk */
171 1.1 pk void
172 1.33 uwe ie_obio_memcopyin(struct ie_softc *sc, void *p, int offset, size_t size)
173 1.1 pk {
174 1.1 pk void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
175 1.33 uwe
176 1.1 pk wcopy(addr, p, size);
177 1.1 pk }
178 1.1 pk
179 1.1 pk /*
180 1.1 pk * Copy from kernel space to naord memory.
181 1.1 pk */
182 1.1 pk void
183 1.33 uwe ie_obio_memcopyout(struct ie_softc *sc, const void *p, int offset, size_t size)
184 1.1 pk {
185 1.1 pk void *addr = (void *)((u_long)sc->bh + offset);/*XXX - not MI!*/
186 1.33 uwe
187 1.1 pk wcopy(p, addr, size);
188 1.1 pk }
189 1.1 pk
190 1.42 msaitoh /* Read a 16-bit value at BH offset */
191 1.33 uwe uint16_t ie_obio_read16(struct ie_softc *, int);
192 1.42 msaitoh /* Write a 16-bit value at BH offset */
193 1.33 uwe void ie_obio_write16(struct ie_softc *, int, uint16_t);
194 1.33 uwe void ie_obio_write24(struct ie_softc *, int, int);
195 1.1 pk
196 1.33 uwe uint16_t
197 1.33 uwe ie_obio_read16(struct ie_softc *sc, int offset)
198 1.1 pk {
199 1.33 uwe uint16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
200 1.33 uwe
201 1.1 pk return (((v&0xff)<<8) | ((v>>8)&0xff));
202 1.1 pk }
203 1.1 pk
204 1.1 pk void
205 1.33 uwe ie_obio_write16(struct ie_softc *sc, int offset, uint16_t v)
206 1.1 pk {
207 1.42 msaitoh
208 1.1 pk v = (((v&0xff)<<8) | ((v>>8)&0xff));
209 1.1 pk bus_space_write_2(sc->bt, sc->bh, offset, v);
210 1.1 pk }
211 1.1 pk
212 1.1 pk void
213 1.33 uwe ie_obio_write24(struct ie_softc *sc, int offset, int addr)
214 1.1 pk {
215 1.9 pk u_char *f = (u_char *)&addr;
216 1.33 uwe uint16_t v0, v1;
217 1.9 pk u_char *t;
218 1.9 pk
219 1.9 pk t = (u_char *)&v0;
220 1.9 pk t[0] = f[3]; t[1] = f[2];
221 1.9 pk bus_space_write_2(sc->bt, sc->bh, offset, v0);
222 1.9 pk
223 1.9 pk t = (u_char *)&v1;
224 1.9 pk t[0] = f[1]; t[1] = 0;
225 1.9 pk bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
226 1.1 pk }
227 1.1 pk
228 1.1 pk int
229 1.37 tsutsui ie_obio_match(device_t parent, cfdata_t cf, void *aux)
230 1.1 pk {
231 1.4 pk union obio_attach_args *uoba = aux;
232 1.4 pk struct obio4_attach_args *oba;
233 1.1 pk
234 1.4 pk if (uoba->uoba_isobio4 == 0)
235 1.42 msaitoh return 0;
236 1.1 pk
237 1.4 pk oba = &uoba->uoba_oba4;
238 1.42 msaitoh return bus_space_probe(oba->oba_bustag, oba->oba_paddr,
239 1.6 pk 1, /* probe size */
240 1.6 pk 0, /* offset */
241 1.6 pk 0, /* flags */
242 1.42 msaitoh NULL, NULL);
243 1.1 pk }
244 1.1 pk
245 1.1 pk void
246 1.37 tsutsui ie_obio_attach(device_t parent, device_t self, void *aux)
247 1.1 pk {
248 1.4 pk union obio_attach_args *uoba = aux;
249 1.4 pk struct obio4_attach_args *oba = &uoba->uoba_oba4;
250 1.37 tsutsui struct ie_softc *sc = device_private(self);
251 1.14 pk bus_dma_tag_t dmatag = oba->oba_dmatag;
252 1.4 pk bus_space_handle_t bh;
253 1.8 pk bus_dma_segment_t seg;
254 1.8 pk int rseg;
255 1.14 pk int error;
256 1.7 pk paddr_t pa;
257 1.32 tsutsui bus_size_t memsize;
258 1.9 pk u_long iebase;
259 1.33 uwe uint8_t myaddr[ETHER_ADDR_LEN];
260 1.1 pk
261 1.39 tsutsui sc->sc_dev = self;
262 1.8 pk sc->bt = oba->oba_bustag;
263 1.1 pk
264 1.1 pk sc->hwreset = ie_obreset;
265 1.1 pk sc->chan_attn = ie_obattend;
266 1.1 pk sc->hwinit = ie_obrun;
267 1.1 pk sc->memcopyout = ie_obio_memcopyout;
268 1.1 pk sc->memcopyin = ie_obio_memcopyin;
269 1.18 bjh21
270 1.18 bjh21 sc->ie_bus_barrier = NULL;
271 1.1 pk sc->ie_bus_read16 = ie_obio_read16;
272 1.1 pk sc->ie_bus_write16 = ie_obio_write16;
273 1.1 pk sc->ie_bus_write24 = ie_obio_write24;
274 1.32 tsutsui sc->sc_msize = memsize = 65536; /* XXX */
275 1.1 pk
276 1.24 pk if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
277 1.42 msaitoh sizeof(struct ieob), BUS_SPACE_MAP_LINEAR, &bh) != 0) {
278 1.37 tsutsui printf("%s: cannot map registers\n", device_xname(self));
279 1.4 pk return;
280 1.4 pk }
281 1.4 pk sc->sc_reg = (void *)bh;
282 1.1 pk
283 1.42 msaitoh /* Allocate control & buffer memory. */
284 1.32 tsutsui if ((error = bus_dmamap_create(dmatag, memsize, 1, memsize, 0,
285 1.42 msaitoh BUS_DMA_NOWAIT|BUS_DMA_24BIT, &sc->sc_dmamap)) != 0) {
286 1.14 pk printf("%s: DMA map create error %d\n",
287 1.37 tsutsui device_xname(self), error);
288 1.14 pk return;
289 1.14 pk }
290 1.42 msaitoh if ((error = bus_dmamem_alloc(dmatag, memsize, 64*1024, 0, &seg, 1,
291 1.42 msaitoh &rseg, BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
292 1.14 pk printf("%s: DMA memory allocation error %d\n",
293 1.37 tsutsui device_xname(self), error);
294 1.14 pk return;
295 1.14 pk }
296 1.14 pk
297 1.14 pk /* Map DMA buffer in CPU addressable space */
298 1.32 tsutsui if ((error = bus_dmamem_map(dmatag, &seg, rseg, memsize,
299 1.42 msaitoh (void **)&sc->sc_maddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
300 1.14 pk printf("%s: DMA buffer map error %d\n",
301 1.37 tsutsui device_xname(self), error);
302 1.14 pk bus_dmamem_free(dmatag, &seg, rseg);
303 1.8 pk return;
304 1.8 pk }
305 1.14 pk
306 1.14 pk /* Load the segment */
307 1.42 msaitoh if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap, sc->sc_maddr,
308 1.42 msaitoh memsize, NULL, BUS_DMA_NOWAIT)) != 0) {
309 1.14 pk printf("%s: DMA buffer map load error %d\n",
310 1.37 tsutsui device_xname(self), error);
311 1.32 tsutsui bus_dmamem_unmap(dmatag, sc->sc_maddr, memsize);
312 1.14 pk bus_dmamem_free(dmatag, &seg, rseg);
313 1.8 pk return;
314 1.8 pk }
315 1.1 pk
316 1.32 tsutsui wzero(sc->sc_maddr, memsize);
317 1.1 pk sc->bh = (bus_space_handle_t)(sc->sc_maddr);
318 1.1 pk
319 1.1 pk /*
320 1.5 pk * The i82586's 24-bit address space maps to the last 16MB of
321 1.5 pk * KVA space (). In addition, the SCP must appear
322 1.5 pk * at IE_SCP_ADDR within the 24-bit address space,
323 1.5 pk * i.e. at KVA IEOB_ADBASE+IE_SCP_ADDR, at the very top of
324 1.5 pk * kernel space. We double-map this last page to the first
325 1.5 pk * page (starting at `maddr') of the memory we allocate to the chip.
326 1.5 pk * (a side-effect of this double-map is that the ISCP and SCB
327 1.5 pk * structures also get aliased there, but we ignore this). The
328 1.5 pk * first page at `maddr' is only used for ISCP, SCB and the aliased
329 1.29 thorpej * SCP; the actual buffers start at maddr+PAGE_SIZE.
330 1.5 pk *
331 1.5 pk * In a picture:
332 1.42 msaitoh
333 1.4 pk |---//--- ISCP-SCB-----scp-|--//- buffers -//-|... |iscp-scb-----SCP-|
334 1.5 pk | | | | | | |
335 1.29 thorpej | |<---PAGE_SIZE-->| | |<--PAGE_SIZE-+-->|
336 1.32 tsutsui | |<------------ memsize ------------>| | ^ |
337 1.5 pk | | | |
338 1.4 pk | \@maddr (last page dbl mapped)
339 1.5 pk | |
340 1.5 pk \@IEOB_ADBASE @IEOB_ADBASE+IE_SCP_ADDR-+
341 1.5 pk
342 1.5 pk *
343 1.5 pk */
344 1.4 pk
345 1.8 pk /* Double map the SCP */
346 1.34 thorpej if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == false)
347 1.1 pk panic("ie pmap_extract");
348 1.1 pk
349 1.1 pk pmap_enter(pmap_kernel(), trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
350 1.10 mycroft pa | PMAP_NC /*| PMAP_IOC*/,
351 1.12 thorpej VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
352 1.23 chris pmap_update(pmap_kernel());
353 1.1 pk
354 1.8 pk /* Map iscp at location 0 (relative to `maddr') */
355 1.5 pk sc->iscp = 0;
356 1.5 pk
357 1.5 pk /* scb follows iscp */
358 1.5 pk sc->scb = IE_ISCP_SZ;
359 1.5 pk
360 1.9 pk /* scp is at the fixed location IE_SCP_ADDR (modulo the page size) */
361 1.9 pk sc->scp = IE_SCP_ADDR & PGOFSET;
362 1.5 pk
363 1.9 pk /* Calculate the 24-bit base of i82586 operations */
364 1.14 pk iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
365 1.14 pk (u_long)IEOB_ADBASE;
366 1.8 pk ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
367 1.9 pk ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
368 1.9 pk ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
369 1.1 pk
370 1.1 pk /*
371 1.9 pk * Rest of first page is unused (wasted!); the other pages
372 1.9 pk * are used for buffers.
373 1.1 pk */
374 1.29 thorpej sc->buf_area = PAGE_SIZE;
375 1.32 tsutsui sc->buf_area_sz = memsize - PAGE_SIZE;
376 1.9 pk
377 1.9 pk if (i82586_proberam(sc) == 0) {
378 1.9 pk printf(": memory probe failed\n");
379 1.9 pk return;
380 1.9 pk }
381 1.1 pk
382 1.31 pk prom_getether(0, myaddr);
383 1.1 pk i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
384 1.1 pk
385 1.4 pk /* Establish interrupt channel */
386 1.42 msaitoh (void)bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_NET,
387 1.42 msaitoh i82586_intr, sc);
388 1.1 pk }
389