Home | History | Annotate | Line # | Download | only in podulebus
if_ei.c revision 1.1
      1 /* $NetBSD: if_ei.c,v 1.1 2001/03/19 23:58:12 bjh21 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/param.h>
     39 
     40 __RCSID("$NetBSD: if_ei.c,v 1.1 2001/03/19 23:58:12 bjh21 Exp $");
     41 
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/reboot.h>	/* For bootverbose */
     45 #include <sys/socket.h>
     46 #include <sys/syslog.h>
     47 #include <sys/systm.h>
     48 #include <net/if.h>
     49 #include <net/if_ether.h>
     50 #include <net/if_media.h>
     51 #include <machine/bus.h>
     52 #include <dev/ic/i82586reg.h>
     53 #include <dev/ic/i82586var.h>
     54 #include <dev/podulebus/podulebus.h>
     55 #include <dev/podulebus/podules.h>
     56 #include <dev/podulebus/if_eireg.h>
     57 
     58 /* Callbacks from the MI 82586 driver */
     59 static void ei_hwreset(struct ie_softc *, int);
     60 /* static void ei_hwinit(struct ie_softc *); */
     61 static void ei_attn(struct ie_softc *, int);
     62 static int ei_intrhook(struct ie_softc *, int);
     63 
     64 static void ei_copyin(struct ie_softc *, void *, int, size_t);
     65 static void ei_copyout(struct ie_softc *, const void *, int, size_t);
     66 static u_int16_t ei_read16(struct ie_softc *, int);
     67 static void ei_write16(struct ie_softc *, int, u_int16_t);
     68 static void ei_write24(struct ie_softc *, int, int);
     69 
     70 /* autoconfiguration glue */
     71 static int ei_match(struct device *, struct cfdata *, void *);
     72 static void ei_attach(struct device *, struct device *, void *);
     73 
     74 struct ei_softc {
     75 	struct	ie_softc sc_ie;
     76 	bus_space_tag_t		sc_ctl_t;
     77 	bus_space_handle_t	sc_ctl_h;
     78 	bus_space_tag_t		sc_mem_t;
     79 	bus_space_handle_t	sc_mem_h;
     80 	bus_space_tag_t		sc_rom_t;
     81 	bus_space_handle_t	sc_rom_h;
     82 	u_int8_t	sc_idrom[EI_ROMSIZE];
     83 	void			*sc_ih;
     84 	struct		evcnt	sc_intrcnt;
     85 };
     86 
     87 struct cfattach ei_ca = {
     88 	sizeof(struct ei_softc), ei_match, ei_attach
     89 };
     90 
     91 static inline void
     92 ei_setpage(struct ei_softc *sc, int page)
     93 {
     94 
     95 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_PAGE, page);
     96 }
     97 
     98 static inline void
     99 ei_cli(struct ei_softc *sc)
    100 {
    101 
    102 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CLI);
    103 }
    104 
    105 static int
    106 ei_match(struct device *parent, struct cfdata *cf, void *aux)
    107 {
    108 	struct podulebus_attach_args *pa = aux;
    109 
    110 	return IS_PODULE(pa, MANUFACTURER_ACORN, PODULE_ACORN_ETHER1);
    111 }
    112 
    113 static void
    114 ei_attach(struct device *parent, struct device *self, void *aux)
    115 {
    116 	struct podulebus_attach_args *pa = aux;
    117 	struct ei_softc *sc = (struct ei_softc *)self;
    118 	int i;
    119 	char descr[16];
    120 
    121 	/* Set up bus spaces */
    122 	sc->sc_ctl_t = sc->sc_mem_t = pa->pa_fast_t;
    123 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base, EI_MEMOFF, 0,
    124 	    &sc->sc_ctl_h);
    125 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base + EI_MEMOFF,
    126 	    EI_PAGESIZE * 2, 0, &sc->sc_mem_h);
    127 	sc->sc_rom_t = pa->pa_sync_t;
    128 	bus_space_map(pa->pa_sync_t, pa->pa_sync_base, EI_ROMSIZE, 0,
    129 	    &sc->sc_rom_h);
    130 
    131 	/* Set up callbacks */
    132 	sc->sc_ie.hwinit = NULL;
    133 	sc->sc_ie.intrhook = ei_intrhook;
    134 	sc->sc_ie.hwreset = ei_hwreset;
    135 	sc->sc_ie.chan_attn = ei_attn;
    136 
    137 	sc->sc_ie.memcopyin = ei_copyin;
    138 	sc->sc_ie.memcopyout = ei_copyout;
    139 
    140 	sc->sc_ie.ie_bus_barrier = NULL;
    141 	sc->sc_ie.ie_bus_read16 = ei_read16;
    142 	sc->sc_ie.ie_bus_write16 = ei_write16;
    143 	sc->sc_ie.ie_bus_write24 = ei_write24;
    144 
    145 	sc->sc_ie.do_xmitnopchain = 0; /* Does it listen? */
    146 
    147 	sc->sc_ie.sc_mediachange = NULL;
    148 	sc->sc_ie.sc_mediastatus = NULL;
    149 
    150 	sc->sc_ie.bt = sc->sc_mem_t; /* XXX hope it doesn't use them */
    151 	sc->sc_ie.bh = sc->sc_mem_h;
    152 
    153 	printf(":");
    154 
    155 	/* All Ether1s are 64k (I believe) */
    156 	sc->sc_ie.sc_msize = EI_MEMSIZE;
    157 
    158 	/* set up pointers to important on-card control structures */
    159 	sc->sc_ie.iscp = 0;
    160 	sc->sc_ie.scb = IE_ISCP_SZ;
    161 	sc->sc_ie.scp = sc->sc_ie.sc_msize + IE_SCP_ADDR - (1 << 24);
    162 
    163 	sc->sc_ie.buf_area = sc->sc_ie.scb + IE_SCB_SZ;
    164 	sc->sc_ie.buf_area_sz =
    165 	    sc->sc_ie.sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
    166 
    167 	/* Wipe the whole of the card's memory */
    168 	for (i = 0; i < EI_NPAGES; i++) {
    169 		ei_setpage(sc, i);
    170 		bus_space_set_region_2(sc->sc_mem_t, sc->sc_mem_h,
    171 				       0, 0,EI_PAGESIZE / 2);
    172 	}
    173 
    174 	/* set up pointers to key structures */
    175 	ei_write24(&sc->sc_ie, IE_SCP_ISCP((u_long)sc->sc_ie.scp),
    176 		    (u_long) sc->sc_ie.iscp);
    177 	ei_write16(&sc->sc_ie, IE_ISCP_SCB((u_long)sc->sc_ie.iscp),
    178 		    (u_long) sc->sc_ie.scb);
    179 	ei_write24(&sc->sc_ie, IE_ISCP_BASE((u_long)sc->sc_ie.iscp),
    180 		    (u_long) sc->sc_ie.iscp);
    181 
    182 	/* check if chip answers */
    183 	if (i82586_proberam(&sc->sc_ie) == 0) {
    184 		printf(" memory probe failed\n");
    185 		return;
    186 	}
    187 
    188 	/* Read ROM */
    189 	bus_space_read_region_1(sc->sc_rom_t, sc->sc_rom_h, 0,
    190 				sc->sc_idrom, EI_ROMSIZE);
    191 
    192 	snprintf(descr, 15, "AKA25 iss. %d", sc->sc_idrom[EI_ROM_HWREV]);
    193 	i82586_attach(&sc->sc_ie, descr, sc->sc_idrom + EI_ROM_EADDR,
    194 		      NULL, 0, 0);
    195 
    196 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    197 	    self->dv_xname, "intr");
    198 	sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_NET, i82586_intr,
    199 	    self, &sc->sc_intrcnt);
    200 	ei_cli(sc);
    201 }
    202 
    203 static void
    204 ei_hwreset(struct ie_softc *sc_ie, int why)
    205 {
    206 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    207 
    208 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h,
    209 			  EI_CONTROL, EI_CTL_RESET);
    210 	DELAY(1000);
    211 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, 0);
    212 	DELAY(1000);
    213 	ei_cli(sc);
    214 }
    215 
    216 static int
    217 ei_intrhook(struct ie_softc *sc_ie, int why)
    218 {
    219 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    220 
    221 	switch (why) {
    222 	case INTR_EXIT:
    223 	case INTR_ACK:
    224 		ei_cli(sc);
    225 		break;
    226 	}
    227 	return 0;
    228 }
    229 
    230 static void
    231 ei_attn(struct ie_softc *sc_ie, int why)
    232 {
    233 	struct ei_softc *sc = (void *)sc_ie;
    234 
    235 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_ATTN);
    236 }
    237 
    238 /*
    239  * Various access functions to allow the MI 82586 driver to get at the
    240  * board's memory.  All memory accesses must go through these, as
    241  * nothing else knows how to manipulate the page register.  Note that
    242  * all addresses and lengths passed in are in bytes of actual data.
    243  * The bus_space functions deal in words, though, so we have to
    244  * convert on the way through.
    245  */
    246 
    247 static void
    248 ei_copyin(struct ie_softc *sc_ie, void *dest, int src, size_t size)
    249 {
    250 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    251 	int cnt, s, extra_byte;
    252 	u_int16_t *wptr;
    253 
    254 #ifdef DIAGNOSTIC
    255 	if (src % 2 != 0 || !ALIGNED_POINTER(dest, u_int16_t))
    256 		panic("%s: unaligned copyin", sc_ie->sc_dev.dv_xname);
    257 #endif
    258 	wptr = dest;
    259 	extra_byte = size % 2;
    260 	size -= extra_byte;
    261 	while (size > 0) {
    262 		cnt = EI_PAGESIZE - src % EI_PAGESIZE;
    263 		if (cnt > size)
    264 			cnt = size;
    265 		s = splnet();
    266 		ei_setpage(sc, ei_atop(src));
    267 		/* bus ops are in words */
    268 		bus_space_read_region_2(sc->sc_mem_t, sc->sc_mem_h,
    269 					ei_atopo(src) / 2, wptr, cnt / 2);
    270 		splx(s);
    271 		src += cnt;
    272 		wptr += cnt / 2;
    273 		size -= cnt;
    274 	}
    275 	if (extra_byte) {
    276 		/* Do we need to be this careful? */
    277 		s = splnet();
    278 		ei_setpage(sc, ei_atop(src));
    279 		*(u_int8_t *)wptr =
    280 		    bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h,
    281 				     ei_atopo(src) / 2) & 0xff;
    282 		splx(s);
    283 	}
    284 }
    285 
    286 static void
    287 ei_copyout(struct ie_softc *sc_ie, const void *src, int dest, size_t size)
    288 {
    289 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    290 	int cnt, s;
    291 	const u_int16_t *wptr;
    292 	u_int16_t *bounce = NULL;
    293 
    294 #ifdef DIAGNOSTIC
    295 	if (dest % 2 != 0)
    296 		panic("%s: unaligned copyout", sc_ie->sc_dev.dv_xname);
    297 #endif
    298 	if (!ALIGNED_POINTER(src, u_int16_t)) {
    299 		MALLOC(bounce, u_int16_t *, size, M_DEVBUF, M_NOWAIT);
    300 		if (bounce == NULL)
    301 			panic("%s: no memory to align copyout",
    302 			      sc_ie->sc_dev.dv_xname);
    303 		memcpy(bounce, src, size);
    304 		src = bounce;
    305 	}
    306 	wptr = src;
    307 	if (size % 2 != 0)
    308 		size++; /* This is safe, since the buffer is 16bit aligned */
    309 	while (size > 0) {
    310 		cnt = EI_PAGESIZE - dest % EI_PAGESIZE;
    311 		if (cnt > size)
    312 			cnt = size;
    313 		s = splnet();
    314 		ei_setpage(sc, ei_atop(dest));
    315 		/* bus ops are in words */
    316 		bus_space_write_region_2(sc->sc_mem_t, sc->sc_mem_h,
    317 					 ei_atopo(dest) / 2, wptr, cnt / 2);
    318 		splx(s);
    319 		wptr += cnt / 2;
    320 		dest += cnt;
    321 		size -= cnt;
    322 	}
    323 	if (bounce != NULL)
    324 		FREE(bounce, M_DEVBUF);
    325 }
    326 
    327 static u_int16_t
    328 ei_read16(struct ie_softc *sc_ie, int addr)
    329 {
    330 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    331 	int result, s;
    332 
    333 #ifdef DIAGNOSTIC
    334 	if (addr % 2 != 0)
    335 		panic("%s: unaligned read16", sc_ie->sc_dev.dv_xname);
    336 #endif
    337 	s = splnet();
    338 	ei_setpage(sc, ei_atop(addr));
    339 	result = bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h,
    340 				  ei_atopo(addr) / 2);
    341 	splx(s);
    342 	return result;
    343 }
    344 
    345 static void
    346 ei_write16(struct ie_softc *sc_ie, int addr, u_int16_t value)
    347 {
    348 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    349 	int s;
    350 
    351 #ifdef DIAGNOSTIC
    352 	if (addr % 2 != 0)
    353 		panic("%s: unaligned write16", sc_ie->sc_dev.dv_xname);
    354 #endif
    355 	s = splnet();
    356 	ei_setpage(sc, ei_atop(addr));
    357 	bus_space_write_2(sc->sc_mem_t, sc->sc_mem_h, ei_atopo(addr) / 2,
    358 			  value);
    359 	splx(s);
    360 }
    361 
    362 static void
    363 ei_write24(struct ie_softc *sc_ie, int addr, int value)
    364 {
    365 	int s;
    366 
    367 #ifdef DIAGNOSTIC
    368 	if (addr % 2 != 0)
    369 		panic("%s: unaligned write24", sc_ie->sc_dev.dv_xname);
    370 #endif
    371 	s = splnet();
    372 	ei_write16(sc_ie, addr, value & 0xffff);
    373 	ei_write16(sc_ie, addr + 2, (value >> 16) & 0xff);
    374 	splx(s);
    375 }
    376 
    377