Home | History | Annotate | Line # | Download | only in podulebus
      1 /* $NetBSD: if_ei.c,v 1.17 2011/06/03 16:28:40 tsutsui 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.17 2011/06/03 16:28:40 tsutsui 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 <sys/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(device_t, cfdata_t, void *);
     73 static void ei_attach(device_t, device_t, 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 CFATTACH_DECL_NEW(ei, sizeof(struct ei_softc),
     89     ei_match, ei_attach, NULL, NULL);
     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(device_t parent, cfdata_t cf, void *aux)
    107 {
    108 	struct podulebus_attach_args *pa = aux;
    109 
    110 	return pa->pa_product == PODULE_ETHER1;
    111 }
    112 
    113 static void
    114 ei_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct podulebus_attach_args *pa = aux;
    117 	struct ei_softc *sc = device_private(self);
    118 	int i;
    119 	char descr[16];
    120 
    121 	sc->sc_ie.sc_dev = self;
    122 
    123 	/* Set up bus spaces */
    124 	sc->sc_ctl_t = sc->sc_mem_t = pa->pa_fast_t;
    125 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base, EI_MEMOFF, 0,
    126 	    &sc->sc_ctl_h);
    127 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base + EI_MEMOFF,
    128 	    EI_PAGESIZE * 2, 0, &sc->sc_mem_h);
    129 	sc->sc_rom_t = pa->pa_sync_t;
    130 	bus_space_map(pa->pa_sync_t, pa->pa_sync_base, EI_ROMSIZE, 0,
    131 	    &sc->sc_rom_h);
    132 
    133 	/* Set up callbacks */
    134 	sc->sc_ie.hwinit = NULL;
    135 	sc->sc_ie.intrhook = ei_intrhook;
    136 	sc->sc_ie.hwreset = ei_hwreset;
    137 	sc->sc_ie.chan_attn = ei_attn;
    138 
    139 	sc->sc_ie.memcopyin = ei_copyin;
    140 	sc->sc_ie.memcopyout = ei_copyout;
    141 
    142 	sc->sc_ie.ie_bus_barrier = NULL;
    143 	sc->sc_ie.ie_bus_read16 = ei_read16;
    144 	sc->sc_ie.ie_bus_write16 = ei_write16;
    145 	sc->sc_ie.ie_bus_write24 = ei_write24;
    146 
    147 	sc->sc_ie.do_xmitnopchain = 0; /* Does it listen? */
    148 
    149 	sc->sc_ie.sc_mediachange = NULL;
    150 	sc->sc_ie.sc_mediastatus = NULL;
    151 
    152 	sc->sc_ie.bt = sc->sc_mem_t; /* XXX hope it doesn't use them */
    153 	sc->sc_ie.bh = sc->sc_mem_h;
    154 
    155 	printf(":");
    156 
    157 	/* All Ether1s are 64k (I believe) */
    158 	sc->sc_ie.sc_msize = EI_MEMSIZE;
    159 
    160 	/* set up pointers to important on-card control structures */
    161 	sc->sc_ie.iscp = 0;
    162 	sc->sc_ie.scb = IE_ISCP_SZ;
    163 	sc->sc_ie.scp = sc->sc_ie.sc_msize + IE_SCP_ADDR - (1 << 24);
    164 
    165 	sc->sc_ie.buf_area = sc->sc_ie.scb + IE_SCB_SZ;
    166 	sc->sc_ie.buf_area_sz =
    167 	    sc->sc_ie.sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
    168 
    169 	/* Wipe the whole of the card's memory */
    170 	for (i = 0; i < EI_NPAGES; i++) {
    171 		ei_setpage(sc, i);
    172 		bus_space_set_region_2(sc->sc_mem_t, sc->sc_mem_h,
    173 				       0, 0,EI_PAGESIZE / 2);
    174 	}
    175 
    176 	/* set up pointers to key structures */
    177 	ei_write24(&sc->sc_ie, IE_SCP_ISCP((u_long)sc->sc_ie.scp),
    178 		    (u_long) sc->sc_ie.iscp);
    179 	ei_write16(&sc->sc_ie, IE_ISCP_SCB((u_long)sc->sc_ie.iscp),
    180 		    (u_long) sc->sc_ie.scb);
    181 	ei_write24(&sc->sc_ie, IE_ISCP_BASE((u_long)sc->sc_ie.iscp),
    182 		    (u_long) sc->sc_ie.iscp);
    183 
    184 	/* check if chip answers */
    185 	if (i82586_proberam(&sc->sc_ie) == 0) {
    186 		printf(" memory probe failed\n");
    187 		return;
    188 	}
    189 
    190 	/* Read ROM */
    191 	bus_space_read_region_1(sc->sc_rom_t, sc->sc_rom_h, 0,
    192 				sc->sc_idrom, EI_ROMSIZE);
    193 
    194 	snprintf(descr, 15, "AKA25 iss. %d", sc->sc_idrom[EI_ROM_HWREV]);
    195 	i82586_attach(&sc->sc_ie, descr, sc->sc_idrom + EI_ROM_EADDR,
    196 		      NULL, 0, 0);
    197 
    198 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    199 	    device_xname(self), "intr");
    200 	sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_NET, i82586_intr,
    201 	    self, &sc->sc_intrcnt);
    202 	ei_cli(sc);
    203 }
    204 
    205 static void
    206 ei_hwreset(struct ie_softc *sc_ie, int why)
    207 {
    208 	struct ei_softc *sc = (struct ei_softc *)sc_ie;
    209 
    210 	bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_RST);
    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_CA);
    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", device_xname(sc_ie->sc_dev));
    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", device_xname(sc_ie->sc_dev));
    298 #endif
    299 	if (!ALIGNED_POINTER(src, u_int16_t)) {
    300 		bounce = (u_int16_t *) malloc(size, M_DEVBUF, M_NOWAIT);
    301 		if (bounce == NULL)
    302 			panic("%s: no memory to align copyout",
    303 			      device_xname(sc_ie->sc_dev));
    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", device_xname(sc_ie->sc_dev));
    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", device_xname(sc_ie->sc_dev));
    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", device_xname(sc_ie->sc_dev));
    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