Home | History | Annotate | Line # | Download | only in ic
am79c930.c revision 1.9
      1 /* $NetBSD: am79c930.c,v 1.9 2004/01/15 09:33:48 onoe Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Am79c930 chip driver.
     41  *
     42  * This is used by the awi driver to use the shared
     43  * memory attached to the 79c930 to communicate with the firmware running
     44  * in the 930's on-board 80188 core.
     45  *
     46  * The 79c930 can be mapped into just I/O space, or also have a
     47  * memory mapping; the mapping must be set up by the bus front-end
     48  * before am79c930_init is called.
     49  */
     50 
     51 /*
     52  * operations:
     53  *
     54  * read_8, read_16, read_32, read_64, read_bytes
     55  * write_8, write_16, write_32, write_64, write_bytes
     56  * (two versions, depending on whether memory-space or i/o space is in use).
     57  *
     58  * interrupt E.C.
     59  * start isr
     60  * end isr
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 #ifdef __NetBSD__
     65 __KERNEL_RCSID(0, "$NetBSD: am79c930.c,v 1.9 2004/01/15 09:33:48 onoe Exp $");
     66 #endif
     67 #ifdef __FreeBSD__
     68 __FBSDID("$FreeBSD$");
     69 #endif
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/endian.h>
     74 #ifndef __FreeBSD__
     75 #include <sys/device.h>
     76 #endif
     77 
     78 #include <machine/cpu.h>
     79 #ifdef __FreeBSD__
     80 #include <machine/bus_pio.h>
     81 #include <machine/bus_memio.h>
     82 #endif
     83 #include <machine/bus.h>
     84 #ifdef __NetBSD__
     85 #include <machine/intr.h>
     86 #endif
     87 
     88 #ifdef __NetBSD__
     89 #include <dev/ic/am79c930reg.h>
     90 #include <dev/ic/am79c930var.h>
     91 #endif
     92 #ifdef __FreeBSD__
     93 #include <dev/awi/am79c930reg.h>
     94 #include <dev/awi/am79c930var.h>
     95 #endif
     96 
     97 #define AM930_DELAY(x) /*nothing*/
     98 
     99 void am79c930_regdump(struct am79c930_softc *sc);
    100 
    101 static void io_write_1(struct am79c930_softc *, u_int32_t, u_int8_t);
    102 static void io_write_2(struct am79c930_softc *, u_int32_t, u_int16_t);
    103 static void io_write_4(struct am79c930_softc *, u_int32_t, u_int32_t);
    104 static void io_write_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);
    105 
    106 static u_int8_t io_read_1(struct am79c930_softc *, u_int32_t);
    107 static u_int16_t io_read_2(struct am79c930_softc *, u_int32_t);
    108 static u_int32_t io_read_4(struct am79c930_softc *, u_int32_t);
    109 static void io_read_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);
    110 
    111 static void mem_write_1(struct am79c930_softc *, u_int32_t, u_int8_t);
    112 static void mem_write_2(struct am79c930_softc *, u_int32_t, u_int16_t);
    113 static void mem_write_4(struct am79c930_softc *, u_int32_t, u_int32_t);
    114 static void mem_write_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);
    115 
    116 static u_int8_t mem_read_1(struct am79c930_softc *, u_int32_t);
    117 static u_int16_t mem_read_2(struct am79c930_softc *, u_int32_t);
    118 static u_int32_t mem_read_4(struct am79c930_softc *, u_int32_t);
    119 static void mem_read_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);
    120 
    121 static struct am79c930_ops iospace_ops = {
    122 	io_write_1,
    123 	io_write_2,
    124 	io_write_4,
    125 	io_write_bytes,
    126 	io_read_1,
    127 	io_read_2,
    128 	io_read_4,
    129 	io_read_bytes
    130 };
    131 
    132 struct am79c930_ops memspace_ops = {
    133 	mem_write_1,
    134 	mem_write_2,
    135 	mem_write_4,
    136 	mem_write_bytes,
    137 	mem_read_1,
    138 	mem_read_2,
    139 	mem_read_4,
    140 	mem_read_bytes
    141 };
    142 
    143 static void
    144 io_write_1( struct am79c930_softc *sc, u_int32_t off, u_int8_t val)
    145 {
    146 	AM930_DELAY(1);
    147 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    148 	    ((off>>8)& 0x7f));
    149 	AM930_DELAY(1);
    150 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
    151 	AM930_DELAY(1);
    152 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA, val);
    153 	AM930_DELAY(1);
    154 }
    155 
    156 static void
    157 io_write_2(struct am79c930_softc *sc, u_int32_t off, u_int16_t val)
    158 {
    159 	AM930_DELAY(1);
    160 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    161 	    ((off>>8)& 0x7f));
    162 	AM930_DELAY(1);
    163 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
    164 	AM930_DELAY(1);
    165 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, val & 0xff);
    166 	AM930_DELAY(1);
    167 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, (val>>8)&0xff);
    168 	AM930_DELAY(1);
    169 }
    170 
    171 static void
    172 io_write_4(struct am79c930_softc *sc, u_int32_t off, u_int32_t val)
    173 {
    174 	AM930_DELAY(1);
    175 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    176 	    ((off>>8)& 0x7f));
    177 	AM930_DELAY(1);
    178 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
    179 	AM930_DELAY(1);
    180 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,val & 0xff);
    181 	AM930_DELAY(1);
    182 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>8)&0xff);
    183 	AM930_DELAY(1);
    184 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>16)&0xff);
    185 	AM930_DELAY(1);
    186 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>24)&0xff);
    187 	AM930_DELAY(1);
    188 }
    189 
    190 static void
    191 io_write_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    192     size_t len)
    193 {
    194 	int i;
    195 
    196 	AM930_DELAY(1);
    197 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    198 	    ((off>>8)& 0x7f));
    199 	AM930_DELAY(1);
    200 	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
    201 	AM930_DELAY(1);
    202 	for (i=0; i<len; i++)
    203 		bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,ptr[i]);
    204 }
    205 
    206 static u_int8_t
    207 io_read_1(struct am79c930_softc *sc, u_int32_t off)
    208 {
    209 	u_int8_t val;
    210 
    211 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    212 	    ((off>>8)& 0x7f));
    213 	AM930_DELAY(1);
    214 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
    215 	AM930_DELAY(1);
    216 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
    217 	AM930_DELAY(1);
    218 	return val;
    219 }
    220 
    221 static u_int16_t
    222 io_read_2(struct am79c930_softc *sc, u_int32_t off)
    223 {
    224 	u_int16_t val;
    225 
    226 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    227 	    ((off>>8)& 0x7f));
    228 	AM930_DELAY(1);
    229 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
    230 	AM930_DELAY(1);
    231 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
    232 	AM930_DELAY(1);
    233 	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
    234 	AM930_DELAY(1);
    235 	return val;
    236 }
    237 
    238 static u_int32_t
    239 io_read_4(struct am79c930_softc *sc, u_int32_t off)
    240 {
    241 	u_int32_t val;
    242 
    243 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    244 	    ((off>>8)& 0x7f));
    245 	AM930_DELAY(1);
    246 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
    247 	AM930_DELAY(1);
    248 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
    249 	AM930_DELAY(1);
    250 	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
    251 	AM930_DELAY(1);
    252 	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 16;
    253 	AM930_DELAY(1);
    254 	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 24;
    255 	AM930_DELAY(1);
    256 	return val;
    257 }
    258 
    259 static void
    260 io_read_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    261     size_t len)
    262 {
    263 	int i;
    264 
    265 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
    266 	    ((off>>8)& 0x7f));
    267 	AM930_DELAY(1);
    268 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
    269 	AM930_DELAY(1);
    270 	for (i=0; i<len; i++)
    271 		ptr[i] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    272 		    AM79C930_IODPA);
    273 }
    274 
    275 static void
    276 mem_write_1(struct am79c930_softc *sc, u_int32_t off, u_int8_t val)
    277 {
    278 	bus_space_write_1(sc->sc_memt, sc->sc_memh, off, val);
    279 }
    280 
    281 static
    282 void mem_write_2(struct am79c930_softc *sc, u_int32_t off, u_int16_t val)
    283 {
    284 	bus_space_tag_t t = sc->sc_memt;
    285 	bus_space_handle_t h = sc->sc_memh;
    286 
    287 	/* could be unaligned */
    288 	if ((off & 0x1) == 0)
    289 		bus_space_write_2(t, h, off,    htole16(val));
    290 	else {
    291 		bus_space_write_1(t, h, off,    val        & 0xff);
    292 		bus_space_write_1(t, h, off+1, (val >>  8) & 0xff);
    293 	}
    294 }
    295 
    296 static void
    297 mem_write_4(struct am79c930_softc *sc, u_int32_t off, u_int32_t val)
    298 {
    299 	bus_space_tag_t t = sc->sc_memt;
    300 	bus_space_handle_t h = sc->sc_memh;
    301 
    302 	/* could be unaligned */
    303 	if ((off & 0x3) == 0)
    304 		bus_space_write_4(t, h, off,    htole32(val));
    305 	else {
    306 		bus_space_write_1(t, h, off,    val        & 0xff);
    307 		bus_space_write_1(t, h, off+1, (val >>  8) & 0xff);
    308 		bus_space_write_1(t, h, off+2, (val >> 16) & 0xff);
    309 		bus_space_write_1(t, h, off+3, (val >> 24) & 0xff);
    310 	}
    311 }
    312 
    313 static void
    314 mem_write_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    315     size_t len)
    316 {
    317 	bus_space_write_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
    318 }
    319 
    320 static u_int8_t
    321 mem_read_1(struct am79c930_softc *sc, u_int32_t off)
    322 {
    323 	return bus_space_read_1(sc->sc_memt, sc->sc_memh, off);
    324 }
    325 
    326 static u_int16_t
    327 mem_read_2(struct am79c930_softc *sc, u_int32_t off)
    328 {
    329 	/* could be unaligned */
    330 	if ((off & 0x1) == 0)
    331 		return le16toh(bus_space_read_2(sc->sc_memt, sc->sc_memh, off));
    332 	else
    333 		return
    334 		     bus_space_read_1(sc->sc_memt, sc->sc_memh, off  )       |
    335 		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+1) << 8);
    336 }
    337 
    338 static u_int32_t
    339 mem_read_4(struct am79c930_softc *sc, u_int32_t off)
    340 {
    341 	/* could be unaligned */
    342 	if ((off & 0x3) == 0)
    343 		return le32toh(bus_space_read_4(sc->sc_memt, sc->sc_memh, off));
    344 	else
    345 		return
    346 		     bus_space_read_1(sc->sc_memt, sc->sc_memh, off  )       |
    347 		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+1) << 8) |
    348 		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+2) <<16) |
    349 		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+3) <<24);
    350 }
    351 
    352 static void
    353 mem_read_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    354     size_t len)
    355 {
    356 	bus_space_read_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
    357 }
    358 
    359 
    360 /*
    361  * Set bits in GCR.
    362  */
    363 
    364 void
    365 am79c930_gcr_setbits(struct am79c930_softc *sc, u_int8_t bits)
    366 {
    367 	u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
    368 
    369 	gcr |= bits;
    370 
    371 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
    372 }
    373 
    374 /*
    375  * Clear bits in GCR.
    376  */
    377 
    378 void
    379 am79c930_gcr_clearbits(struct am79c930_softc *sc, u_int8_t bits)
    380 {
    381 	u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
    382 
    383 	gcr &= ~bits;
    384 
    385 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
    386 }
    387 
    388 u_int8_t
    389 am79c930_gcr_read(struct am79c930_softc *sc)
    390 {
    391 	return bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
    392 }
    393 
    394 #if 0
    395 void
    396 am79c930_regdump(struct am79c930_softc *sc)
    397 {
    398 	u_int8_t buf[8];
    399 	int i;
    400 
    401 	AM930_DELAY(5);
    402 	for (i=0; i<8; i++) {
    403 		buf[i] = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, i);
    404 		AM930_DELAY(5);
    405 	}
    406 	printf("am79c930: regdump:");
    407 	for (i=0; i<8; i++) {
    408 		printf(" %02x", buf[i]);
    409 	}
    410 	printf("\n");
    411 }
    412 #endif
    413 
    414 void
    415 am79c930_chip_init(struct am79c930_softc *sc, int how)
    416 {
    417 	/* zero the bank select register, and leave it that way.. */
    418 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_BSS, 0);
    419 	if (how)
    420 	  	sc->sc_ops = &memspace_ops;
    421 	else
    422 	  	sc->sc_ops = &iospace_ops;
    423 }
    424