xref: /netbsd-src/sys/dev/podulebus/if_ei.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /* $NetBSD: if_ei.c,v 1.3 2001/11/13 13:32:35 lukem Exp $ */
2 
3 /*-
4  * Copyright (c) 2000, 2001 Ben Harris
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * if_ei.c -- driver for Acorn AKA25 Ether1 card
31  */
32 /*
33  * This is not directly based on the arm32 ie driver, as that doesn't
34  * use the MI 82586 driver, and generally looks a mess (not to mention
35  * having dodgy copyright status).  Let's try again.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_ei.c,v 1.3 2001/11/13 13:32:35 lukem Exp $");
40 
41 #include <sys/param.h>
42 
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/reboot.h>	/* For bootverbose */
46 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #include <sys/systm.h>
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52 #include <machine/bus.h>
53 #include <dev/ic/i82586reg.h>
54 #include <dev/ic/i82586var.h>
55 #include <dev/podulebus/podulebus.h>
56 #include <dev/podulebus/podules.h>
57 #include <dev/podulebus/if_eireg.h>
58 
59 /* Callbacks from the MI 82586 driver */
60 static void ei_hwreset(struct ie_softc *, int);
61 /* static void ei_hwinit(struct ie_softc *); */
62 static void ei_attn(struct ie_softc *, int);
63 static int ei_intrhook(struct ie_softc *, int);
64 
65 static void ei_copyin(struct ie_softc *, void *, int, size_t);
66 static void ei_copyout(struct ie_softc *, const void *, int, size_t);
67 static u_int16_t ei_read16(struct ie_softc *, int);
68 static void ei_write16(struct ie_softc *, int, u_int16_t);
69 static void ei_write24(struct ie_softc *, int, int);
70 
71 /* autoconfiguration glue */
72 static int ei_match(struct device *, struct cfdata *, void *);
73 static void ei_attach(struct device *, struct device *, void *);
74 
75 struct ei_softc {
76 	struct	ie_softc sc_ie;
77 	bus_space_tag_t		sc_ctl_t;
78 	bus_space_handle_t	sc_ctl_h;
79 	bus_space_tag_t		sc_mem_t;
80 	bus_space_handle_t	sc_mem_h;
81 	bus_space_tag_t		sc_rom_t;
82 	bus_space_handle_t	sc_rom_h;
83 	u_int8_t	sc_idrom[EI_ROMSIZE];
84 	void			*sc_ih;
85 	struct		evcnt	sc_intrcnt;
86 };
87 
88 struct cfattach ei_ca = {
89 	sizeof(struct ei_softc), ei_match, ei_attach
90 };
91 
92 static inline void
93 ei_setpage(struct ei_softc *sc, int page)
94 {
95 
96 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_PAGE, page);
97 }
98 
99 static inline void
100 ei_cli(struct ei_softc *sc)
101 {
102 
103 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CLI);
104 }
105 
106 static int
107 ei_match(struct device *parent, struct cfdata *cf, void *aux)
108 {
109 	struct podulebus_attach_args *pa = aux;
110 
111 	return IS_PODULE(pa, MANUFACTURER_ACORN, PODULE_ACORN_ETHER1);
112 }
113 
114 static void
115 ei_attach(struct device *parent, struct device *self, void *aux)
116 {
117 	struct podulebus_attach_args *pa = aux;
118 	struct ei_softc *sc = (struct ei_softc *)self;
119 	int i;
120 	char descr[16];
121 
122 	/* Set up bus spaces */
123 	sc->sc_ctl_t = sc->sc_mem_t = pa->pa_fast_t;
124 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base, EI_MEMOFF, 0,
125 	    &sc->sc_ctl_h);
126 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base + EI_MEMOFF,
127 	    EI_PAGESIZE * 2, 0, &sc->sc_mem_h);
128 	sc->sc_rom_t = pa->pa_sync_t;
129 	bus_space_map(pa->pa_sync_t, pa->pa_sync_base, EI_ROMSIZE, 0,
130 	    &sc->sc_rom_h);
131 
132 	/* Set up callbacks */
133 	sc->sc_ie.hwinit = NULL;
134 	sc->sc_ie.intrhook = ei_intrhook;
135 	sc->sc_ie.hwreset = ei_hwreset;
136 	sc->sc_ie.chan_attn = ei_attn;
137 
138 	sc->sc_ie.memcopyin = ei_copyin;
139 	sc->sc_ie.memcopyout = ei_copyout;
140 
141 	sc->sc_ie.ie_bus_barrier = NULL;
142 	sc->sc_ie.ie_bus_read16 = ei_read16;
143 	sc->sc_ie.ie_bus_write16 = ei_write16;
144 	sc->sc_ie.ie_bus_write24 = ei_write24;
145 
146 	sc->sc_ie.do_xmitnopchain = 0; /* Does it listen? */
147 
148 	sc->sc_ie.sc_mediachange = NULL;
149 	sc->sc_ie.sc_mediastatus = NULL;
150 
151 	sc->sc_ie.bt = sc->sc_mem_t; /* XXX hope it doesn't use them */
152 	sc->sc_ie.bh = sc->sc_mem_h;
153 
154 	printf(":");
155 
156 	/* All Ether1s are 64k (I believe) */
157 	sc->sc_ie.sc_msize = EI_MEMSIZE;
158 
159 	/* set up pointers to important on-card control structures */
160 	sc->sc_ie.iscp = 0;
161 	sc->sc_ie.scb = IE_ISCP_SZ;
162 	sc->sc_ie.scp = sc->sc_ie.sc_msize + IE_SCP_ADDR - (1 << 24);
163 
164 	sc->sc_ie.buf_area = sc->sc_ie.scb + IE_SCB_SZ;
165 	sc->sc_ie.buf_area_sz =
166 	    sc->sc_ie.sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
167 
168 	/* Wipe the whole of the card's memory */
169 	for (i = 0; i < EI_NPAGES; i++) {
170 		ei_setpage(sc, i);
171 		bus_space_set_region_2(sc->sc_mem_t, sc->sc_mem_h,
172 				       0, 0,EI_PAGESIZE / 2);
173 	}
174 
175 	/* set up pointers to key structures */
176 	ei_write24(&sc->sc_ie, IE_SCP_ISCP((u_long)sc->sc_ie.scp),
177 		    (u_long) sc->sc_ie.iscp);
178 	ei_write16(&sc->sc_ie, IE_ISCP_SCB((u_long)sc->sc_ie.iscp),
179 		    (u_long) sc->sc_ie.scb);
180 	ei_write24(&sc->sc_ie, IE_ISCP_BASE((u_long)sc->sc_ie.iscp),
181 		    (u_long) sc->sc_ie.iscp);
182 
183 	/* check if chip answers */
184 	if (i82586_proberam(&sc->sc_ie) == 0) {
185 		printf(" memory probe failed\n");
186 		return;
187 	}
188 
189 	/* Read ROM */
190 	bus_space_read_region_1(sc->sc_rom_t, sc->sc_rom_h, 0,
191 				sc->sc_idrom, EI_ROMSIZE);
192 
193 	snprintf(descr, 15, "AKA25 iss. %d", sc->sc_idrom[EI_ROM_HWREV]);
194 	i82586_attach(&sc->sc_ie, descr, sc->sc_idrom + EI_ROM_EADDR,
195 		      NULL, 0, 0);
196 
197 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
198 	    self->dv_xname, "intr");
199 	sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_NET, i82586_intr,
200 	    self, &sc->sc_intrcnt);
201 	ei_cli(sc);
202 }
203 
204 static void
205 ei_hwreset(struct ie_softc *sc_ie, int why)
206 {
207 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
208 
209 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h,
210 			  EI_CONTROL, EI_CTL_RESET);
211 	DELAY(1000);
212 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, 0);
213 	DELAY(1000);
214 	ei_cli(sc);
215 }
216 
217 static int
218 ei_intrhook(struct ie_softc *sc_ie, int why)
219 {
220 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
221 
222 	switch (why) {
223 	case INTR_EXIT:
224 	case INTR_ACK:
225 		ei_cli(sc);
226 		break;
227 	}
228 	return 0;
229 }
230 
231 static void
232 ei_attn(struct ie_softc *sc_ie, int why)
233 {
234 	struct ei_softc *sc = (void *)sc_ie;
235 
236 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_ATTN);
237 }
238 
239 /*
240  * Various access functions to allow the MI 82586 driver to get at the
241  * board's memory.  All memory accesses must go through these, as
242  * nothing else knows how to manipulate the page register.  Note that
243  * all addresses and lengths passed in are in bytes of actual data.
244  * The bus_space functions deal in words, though, so we have to
245  * convert on the way through.
246  */
247 
248 static void
249 ei_copyin(struct ie_softc *sc_ie, void *dest, int src, size_t size)
250 {
251 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
252 	int cnt, s, extra_byte;
253 	u_int16_t *wptr;
254 
255 #ifdef DIAGNOSTIC
256 	if (src % 2 != 0 || !ALIGNED_POINTER(dest, u_int16_t))
257 		panic("%s: unaligned copyin", sc_ie->sc_dev.dv_xname);
258 #endif
259 	wptr = dest;
260 	extra_byte = size % 2;
261 	size -= extra_byte;
262 	while (size > 0) {
263 		cnt = EI_PAGESIZE - src % EI_PAGESIZE;
264 		if (cnt > size)
265 			cnt = size;
266 		s = splnet();
267 		ei_setpage(sc, ei_atop(src));
268 		/* bus ops are in words */
269 		bus_space_read_region_2(sc->sc_mem_t, sc->sc_mem_h,
270 					ei_atopo(src) / 2, wptr, cnt / 2);
271 		splx(s);
272 		src += cnt;
273 		wptr += cnt / 2;
274 		size -= cnt;
275 	}
276 	if (extra_byte) {
277 		/* Do we need to be this careful? */
278 		s = splnet();
279 		ei_setpage(sc, ei_atop(src));
280 		*(u_int8_t *)wptr =
281 		    bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h,
282 				     ei_atopo(src) / 2) & 0xff;
283 		splx(s);
284 	}
285 }
286 
287 static void
288 ei_copyout(struct ie_softc *sc_ie, const void *src, int dest, size_t size)
289 {
290 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
291 	int cnt, s;
292 	const u_int16_t *wptr;
293 	u_int16_t *bounce = NULL;
294 
295 #ifdef DIAGNOSTIC
296 	if (dest % 2 != 0)
297 		panic("%s: unaligned copyout", sc_ie->sc_dev.dv_xname);
298 #endif
299 	if (!ALIGNED_POINTER(src, u_int16_t)) {
300 		MALLOC(bounce, u_int16_t *, size, M_DEVBUF, M_NOWAIT);
301 		if (bounce == NULL)
302 			panic("%s: no memory to align copyout",
303 			      sc_ie->sc_dev.dv_xname);
304 		memcpy(bounce, src, size);
305 		src = bounce;
306 	}
307 	wptr = src;
308 	if (size % 2 != 0)
309 		size++; /* This is safe, since the buffer is 16bit aligned */
310 	while (size > 0) {
311 		cnt = EI_PAGESIZE - dest % EI_PAGESIZE;
312 		if (cnt > size)
313 			cnt = size;
314 		s = splnet();
315 		ei_setpage(sc, ei_atop(dest));
316 		/* bus ops are in words */
317 		bus_space_write_region_2(sc->sc_mem_t, sc->sc_mem_h,
318 					 ei_atopo(dest) / 2, wptr, cnt / 2);
319 		splx(s);
320 		wptr += cnt / 2;
321 		dest += cnt;
322 		size -= cnt;
323 	}
324 	if (bounce != NULL)
325 		FREE(bounce, M_DEVBUF);
326 }
327 
328 static u_int16_t
329 ei_read16(struct ie_softc *sc_ie, int addr)
330 {
331 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
332 	int result, s;
333 
334 #ifdef DIAGNOSTIC
335 	if (addr % 2 != 0)
336 		panic("%s: unaligned read16", sc_ie->sc_dev.dv_xname);
337 #endif
338 	s = splnet();
339 	ei_setpage(sc, ei_atop(addr));
340 	result = bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h,
341 				  ei_atopo(addr) / 2);
342 	splx(s);
343 	return result;
344 }
345 
346 static void
347 ei_write16(struct ie_softc *sc_ie, int addr, u_int16_t value)
348 {
349 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
350 	int s;
351 
352 #ifdef DIAGNOSTIC
353 	if (addr % 2 != 0)
354 		panic("%s: unaligned write16", sc_ie->sc_dev.dv_xname);
355 #endif
356 	s = splnet();
357 	ei_setpage(sc, ei_atop(addr));
358 	bus_space_write_2(sc->sc_mem_t, sc->sc_mem_h, ei_atopo(addr) / 2,
359 			  value);
360 	splx(s);
361 }
362 
363 static void
364 ei_write24(struct ie_softc *sc_ie, int addr, int value)
365 {
366 	int s;
367 
368 #ifdef DIAGNOSTIC
369 	if (addr % 2 != 0)
370 		panic("%s: unaligned write24", sc_ie->sc_dev.dv_xname);
371 #endif
372 	s = splnet();
373 	ei_write16(sc_ie, addr, value & 0xffff);
374 	ei_write16(sc_ie, addr + 2, (value >> 16) & 0xff);
375 	splx(s);
376 }
377 
378