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