Home | History | Annotate | Line # | Download | only in dev
if_cs_mainbus.c revision 1.4.10.1
      1  1.4.10.1   cherry /*	$NetBSD: if_cs_mainbus.c,v 1.4.10.1 2011/06/23 14:19:10 cherry Exp $	*/
      2       1.2  garbled 
      3       1.2  garbled /*
      4       1.2  garbled  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5       1.2  garbled  * All rights reserved.
      6       1.2  garbled  *
      7       1.2  garbled  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2  garbled  * by Lennart Augustsson (lennart (at) augustsson.net) at Sandburst Corp.
      9       1.2  garbled  *
     10       1.2  garbled  * Redistribution and use in source and binary forms, with or without
     11       1.2  garbled  * modification, are permitted provided that the following conditions
     12       1.2  garbled  * are met:
     13       1.2  garbled  * 1. Redistributions of source code must retain the above copyright
     14       1.2  garbled  *    notice, this list of conditions and the following disclaimer.
     15       1.2  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2  garbled  *    notice, this list of conditions and the following disclaimer in the
     17       1.2  garbled  *    documentation and/or other materials provided with the distribution.
     18       1.2  garbled  *
     19       1.2  garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2  garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2  garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2  garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2  garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2  garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2  garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2  garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2  garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2  garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2  garbled  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2  garbled  */
     31       1.2  garbled 
     32       1.2  garbled #include <sys/cdefs.h>
     33  1.4.10.1   cherry __KERNEL_RCSID(0, "$NetBSD: if_cs_mainbus.c,v 1.4.10.1 2011/06/23 14:19:10 cherry Exp $");
     34       1.2  garbled 
     35       1.2  garbled #include <sys/param.h>
     36       1.2  garbled #include <sys/device.h>
     37       1.2  garbled #include <sys/systm.h>
     38       1.2  garbled #include <sys/socket.h>
     39  1.4.10.1   cherry #include <sys/bus.h>
     40       1.2  garbled 
     41       1.2  garbled #include "rnd.h"
     42       1.2  garbled #if NRND > 0
     43       1.2  garbled #include <sys/rnd.h>
     44       1.2  garbled #endif
     45       1.2  garbled 
     46       1.2  garbled #include <net/if.h>
     47       1.2  garbled #include <net/if_ether.h>
     48       1.2  garbled #include <net/if_media.h>
     49       1.2  garbled #ifdef INET
     50       1.2  garbled #include <netinet/in.h>
     51       1.2  garbled #include <netinet/if_inarp.h>
     52       1.2  garbled #endif
     53       1.2  garbled 
     54  1.4.10.1   cherry #include <powerpc/psl.h>
     55  1.4.10.1   cherry 
     56       1.2  garbled #include <machine/pio.h>
     57       1.2  garbled #include <machine/pmppc.h>
     58  1.4.10.1   cherry #include <evbppc/pmppc/dev/mainbus.h>
     59       1.2  garbled 
     60       1.2  garbled #include <dev/ic/cs89x0reg.h>
     61       1.2  garbled #include <dev/ic/cs89x0var.h>
     62       1.2  garbled 
     63       1.2  garbled #include <sys/callout.h>
     64       1.2  garbled 
     65       1.2  garbled #define ATSN_EEPROM_MAC_OFFSET           0x20
     66       1.2  garbled 
     67       1.2  garbled 
     68       1.2  garbled static void	cs_check_eeprom(struct cs_softc *sc);
     69       1.2  garbled 
     70  1.4.10.1   cherry static int	cs_mainbus_match(device_t, cfdata_t, void *);
     71  1.4.10.1   cherry static void	cs_mainbus_attach(device_t, device_t, void *);
     72       1.2  garbled 
     73       1.4  tsutsui CFATTACH_DECL_NEW(cs_mainbus, sizeof(struct cs_softc),
     74       1.2  garbled     cs_mainbus_match, cs_mainbus_attach, NULL, NULL);
     75       1.2  garbled 
     76       1.2  garbled int
     77       1.4  tsutsui cs_mainbus_match(device_t parent, cfdata_t cf, void *aux)
     78       1.2  garbled {
     79       1.2  garbled 	struct mainbus_attach_args *maa = aux;
     80       1.2  garbled 
     81       1.2  garbled 	return (strcmp(maa->mb_name, "cs") == 0);
     82       1.2  garbled }
     83       1.2  garbled 
     84       1.2  garbled #if 0
     85       1.2  garbled static u_int64_t
     86       1.2  garbled in64(uint a)
     87       1.2  garbled {
     88       1.2  garbled 	union {
     89       1.2  garbled 		double d;
     90       1.2  garbled 		u_int64_t i;
     91       1.2  garbled 	} u;
     92       1.2  garbled 	double save, *dp = (double *)a;
     93       1.2  garbled 	u_int32_t msr, nmsr;
     94       1.2  garbled 
     95       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(msr));
     96       1.2  garbled 	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
     97       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(nmsr));
     98       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
     99       1.2  garbled 	__asm volatile(
    100       1.2  garbled        "stfd 0,%0\n\
    101       1.2  garbled 	lfd 0,%1\n\
    102       1.2  garbled 	stfd 0,%2\n\
    103       1.2  garbled 	lfd 0,%0"
    104       1.2  garbled 		 : "=m"(save), "=m"(*dp)
    105       1.2  garbled 		 : "m"(u.d)
    106       1.2  garbled 		);
    107       1.2  garbled 	__asm volatile ("eieio; sync");
    108       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(msr));
    109       1.2  garbled 	return (u.i);
    110       1.2  garbled }
    111       1.2  garbled #endif
    112       1.2  garbled 
    113       1.2  garbled static void
    114       1.2  garbled out64(uint a, u_int64_t v)
    115       1.2  garbled {
    116       1.2  garbled 	union {
    117       1.2  garbled 		double d;
    118       1.2  garbled 		u_int64_t i;
    119       1.2  garbled 	} u;
    120       1.2  garbled 	double save, *dp = (double *)a;
    121       1.2  garbled 	u_int32_t msr, nmsr;
    122       1.2  garbled 	int s;
    123       1.2  garbled 
    124       1.2  garbled 	s = splhigh();
    125       1.2  garbled 	u.i = v;
    126       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(msr));
    127       1.2  garbled 	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
    128       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(nmsr));
    129       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
    130       1.2  garbled 	__asm volatile(
    131       1.2  garbled        "stfd 0,%0\n\
    132       1.2  garbled 	lfd 0,%2\n\
    133       1.2  garbled 	stfd 0,%1\n\
    134       1.2  garbled 	lfd 0,%0"
    135       1.2  garbled 		 : "=m"(save), "=m"(*dp)
    136       1.2  garbled 		 : "m"(u.d)
    137       1.2  garbled 		);
    138       1.2  garbled 	__asm volatile ("eieio; sync");
    139       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(msr));
    140       1.2  garbled 	splx(s);
    141       1.2  garbled }
    142       1.2  garbled 
    143       1.2  garbled static u_int8_t
    144       1.2  garbled cs_io_read_1(struct cs_softc *sc, bus_size_t offs)
    145       1.2  garbled {
    146       1.2  garbled 	u_int32_t a, v;
    147       1.2  garbled 
    148       1.2  garbled 	a = sc->sc_ioh + (offs << 2);
    149       1.2  garbled 	v = in8(a);
    150       1.2  garbled 	return v;
    151       1.2  garbled }
    152       1.2  garbled 
    153       1.2  garbled static u_int16_t
    154       1.2  garbled cs_io_read_2(struct cs_softc *sc, bus_size_t offs)
    155       1.2  garbled {
    156       1.2  garbled 	u_int32_t a, v;
    157       1.2  garbled 
    158       1.2  garbled 	a = sc->sc_ioh + (offs << 2);
    159       1.2  garbled 	v = in16(a);
    160       1.2  garbled 	return v;
    161       1.2  garbled }
    162       1.2  garbled 
    163       1.2  garbled static void
    164       1.2  garbled cs_io_read_multi_2(struct cs_softc *sc, bus_size_t offs, u_int16_t *buf,
    165       1.2  garbled 		   bus_size_t cnt)
    166       1.2  garbled {
    167       1.2  garbled 	u_int32_t a, v;
    168       1.2  garbled 
    169       1.2  garbled 	a = sc->sc_ioh + (offs << 2);
    170       1.2  garbled 	while (cnt--) {
    171       1.2  garbled 		v = in16(a);
    172       1.2  garbled 		*buf++ = bswap16(v);
    173       1.2  garbled 	}
    174       1.2  garbled }
    175       1.2  garbled 
    176       1.2  garbled static void
    177       1.2  garbled cs_io_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
    178       1.2  garbled {
    179       1.2  garbled 	u_int32_t a;
    180       1.2  garbled 	u_int64_t v;
    181       1.2  garbled 
    182       1.2  garbled 	a = sc->sc_ioh + (offs << 2);
    183       1.2  garbled 	v = (u_int64_t)data << 48;
    184       1.2  garbled 	out64(a, v);
    185       1.2  garbled 
    186       1.2  garbled 	(void)in16(a);		/* CPC700 write post bug */
    187       1.2  garbled }
    188       1.2  garbled 
    189       1.2  garbled static void
    190       1.2  garbled cs_io_write_multi_2(struct cs_softc *sc, bus_size_t offs,
    191       1.2  garbled 		    const u_int16_t *buf, bus_size_t cnt)
    192       1.2  garbled {
    193       1.2  garbled 	u_int16_t v;
    194       1.2  garbled 	double save, *dp;
    195       1.2  garbled 	union {
    196       1.2  garbled 		double d;
    197       1.2  garbled 		u_int64_t i;
    198       1.2  garbled 	} u;
    199       1.2  garbled 	u_int32_t msr, nmsr;
    200       1.2  garbled 	int s;
    201       1.2  garbled 
    202       1.2  garbled 	dp = (double *)(sc->sc_ioh + (offs << 2));
    203       1.2  garbled 
    204       1.2  garbled 	s = splhigh();
    205       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(msr));
    206       1.2  garbled 	nmsr = (msr | PSL_FP) & ~(PSL_FE0 | PSL_FE1);
    207       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(nmsr));
    208       1.2  garbled 	__asm volatile("mfmsr %0" : "=r"(nmsr)); /* some interlock nonsense */
    209       1.2  garbled 	__asm volatile("stfd 0,%0" : "=m"(save));
    210       1.2  garbled 
    211       1.2  garbled 	while (cnt--) {
    212       1.2  garbled 		v = *buf++;
    213       1.2  garbled 		v = bswap16(v);
    214       1.2  garbled 		u.i = (u_int64_t)v << 48;
    215       1.2  garbled 		__asm volatile("lfd 0,%1\nstfd 0,%0" : "=m"(*dp) : "m"(u.d) );
    216       1.2  garbled 		__asm volatile ("eieio; sync");
    217       1.2  garbled 	}
    218       1.2  garbled 	__asm volatile("lfd 0,%0" :: "m"(save));
    219       1.2  garbled 	__asm volatile("mtmsr %0" :: "r"(msr));
    220       1.2  garbled 	splx(s);
    221       1.2  garbled }
    222       1.2  garbled 
    223       1.2  garbled static u_int16_t
    224       1.2  garbled cs_mem_read_2(struct cs_softc *sc, bus_size_t offs)
    225       1.2  garbled {
    226       1.2  garbled 	panic("cs_mem_read_2");
    227       1.2  garbled }
    228       1.2  garbled 
    229       1.2  garbled static void
    230       1.2  garbled cs_mem_write_2(struct cs_softc *sc, bus_size_t offs, u_int16_t data)
    231       1.2  garbled {
    232       1.2  garbled 	panic("cs_mem_write_2");
    233       1.2  garbled }
    234       1.2  garbled 
    235       1.2  garbled static void
    236       1.2  garbled cs_mem_write_region_2(struct cs_softc *sc, bus_size_t offs,
    237       1.2  garbled 		      const u_int16_t *buf, bus_size_t cnt)
    238       1.2  garbled {
    239       1.2  garbled 	panic("cs_mem_write_region_2");
    240       1.2  garbled }
    241       1.2  garbled 
    242       1.2  garbled void
    243       1.4  tsutsui cs_mainbus_attach(device_t parent, device_t self, void *aux)
    244       1.2  garbled {
    245       1.4  tsutsui 	struct cs_softc *sc = device_private(self);
    246       1.2  garbled 	struct mainbus_attach_args *maa = aux;
    247       1.2  garbled 	int media[1] = { IFM_ETHER | IFM_10_T };
    248       1.2  garbled 
    249       1.2  garbled 	printf("\n");
    250       1.2  garbled 
    251       1.4  tsutsui 	sc->sc_dev = self;
    252       1.2  garbled 	sc->sc_iot = maa->mb_bt;
    253       1.2  garbled 	sc->sc_memt = maa->mb_bt;
    254       1.2  garbled 	sc->sc_irq = maa->mb_irq;
    255       1.2  garbled 
    256       1.2  garbled 	if (bus_space_map(sc->sc_iot, PMPPC_CS_IO, CS8900_IOSIZE*4,
    257       1.2  garbled 			  0, &sc->sc_ioh)) {
    258       1.4  tsutsui 		printf("%s: failed to map io\n", device_xname(self));
    259       1.2  garbled 		return;
    260       1.2  garbled 	}
    261       1.2  garbled 
    262       1.2  garbled 	cs_check_eeprom(sc);
    263       1.2  garbled 
    264       1.2  garbled 	sc->sc_ih = intr_establish(sc->sc_irq, IST_LEVEL, IPL_NET, cs_intr, sc);
    265       1.2  garbled 	if (!sc->sc_ih) {
    266       1.2  garbled 		printf("%s: unable to establish interrupt\n",
    267       1.4  tsutsui 		    device_xname(self));
    268       1.2  garbled 		goto fail;
    269       1.2  garbled 	}
    270       1.2  garbled 
    271       1.2  garbled 	sc->sc_cfgflags = CFGFLG_NOT_EEPROM;
    272       1.2  garbled 
    273       1.2  garbled 	sc->sc_io_read_1 = cs_io_read_1;
    274       1.2  garbled 	sc->sc_io_read_2 = cs_io_read_2;
    275       1.2  garbled 	sc->sc_io_read_multi_2 = cs_io_read_multi_2;
    276       1.2  garbled 	sc->sc_io_write_2 = cs_io_write_2;
    277       1.2  garbled 	sc->sc_io_write_multi_2 = cs_io_write_multi_2;
    278       1.2  garbled 	sc->sc_mem_read_2 = cs_mem_read_2;
    279       1.2  garbled 	sc->sc_mem_write_2 = cs_mem_write_2;
    280       1.2  garbled 	sc->sc_mem_write_region_2 = cs_mem_write_region_2;
    281       1.2  garbled 
    282       1.2  garbled 	/*
    283       1.2  garbled 	 * We need interrupt on INTRQ0 from the CS8900 (that's what wired
    284       1.2  garbled 	 * to the UIC).  The MI driver subtracts 10 from the irq, so
    285       1.2  garbled 	 * use 10 as the irq.
    286       1.2  garbled 	 */
    287       1.2  garbled 	sc->sc_irq = 10;
    288       1.2  garbled 
    289       1.2  garbled 	/* Use half duplex 10baseT. */
    290       1.2  garbled 	if (cs_attach(sc, NULL, media, 1, IFM_ETHER | IFM_10_T)) {
    291       1.4  tsutsui 		printf("%s: unable to attach\n", device_xname(self));
    292       1.2  garbled 		goto fail;
    293       1.2  garbled 	}
    294       1.2  garbled 
    295       1.2  garbled 	return;
    296       1.2  garbled 
    297       1.2  garbled  fail:
    298       1.2  garbled 	/* XXX disestablish, unmap */
    299       1.2  garbled 	return;
    300       1.2  garbled }
    301       1.2  garbled 
    302       1.2  garbled 
    303       1.2  garbled /*
    304       1.2  garbled  * EEPROM initialization code.
    305       1.2  garbled  */
    306       1.2  garbled 
    307       1.2  garbled static uint16_t default_eeprom_cfg[] =
    308       1.2  garbled { 0xA100, 0x2020, 0x0300, 0x0000, 0x0000,
    309       1.2  garbled   0x102C, 0x1000, 0x0008, 0x2158, 0x0000,
    310       1.2  garbled   0x0000, 0x0000 };
    311       1.2  garbled 
    312       1.2  garbled static uint16_t
    313       1.2  garbled cs_readreg(struct cs_softc *sc, uint pp_offset)
    314       1.2  garbled {
    315       1.2  garbled 	cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
    316       1.2  garbled 	(void)cs_io_read_2(sc, PORT_PKTPG_PTR);
    317       1.2  garbled 	return (cs_io_read_2(sc, PORT_PKTPG_DATA));
    318       1.2  garbled }
    319       1.2  garbled 
    320       1.2  garbled static void
    321       1.2  garbled cs_writereg(struct cs_softc *sc, uint pp_offset, uint16_t value)
    322       1.2  garbled {
    323       1.2  garbled 	cs_io_write_2(sc, PORT_PKTPG_PTR, pp_offset);
    324       1.2  garbled 	(void)cs_io_read_2(sc, PORT_PKTPG_PTR);
    325       1.2  garbled 	cs_io_write_2(sc, PORT_PKTPG_DATA, value);
    326       1.2  garbled 	(void)cs_io_read_2(sc, PORT_PKTPG_DATA);
    327       1.2  garbled }
    328       1.2  garbled 
    329       1.2  garbled static int
    330       1.2  garbled cs_wait_eeprom_ready(struct cs_softc *sc)
    331       1.2  garbled {
    332       1.2  garbled 	int ms;
    333       1.2  garbled 
    334       1.2  garbled 	/*
    335       1.2  garbled 	 * Check to see if the EEPROM is ready, a timeout is used -
    336       1.2  garbled 	 * just in case EEPROM is ready when SI_BUSY in the
    337       1.2  garbled 	 * PP_SelfST is clear.
    338       1.2  garbled 	 */
    339       1.2  garbled 	ms = 0;
    340       1.2  garbled 	while(cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_SI_BUSY) {
    341       1.2  garbled 		delay(1000);
    342       1.2  garbled 		if (ms++ > 20)
    343       1.2  garbled 			return 0;
    344       1.2  garbled 	}
    345       1.2  garbled 	return 1;
    346       1.2  garbled }
    347       1.2  garbled 
    348       1.2  garbled static void
    349       1.2  garbled cs_wr_eeprom(struct cs_softc *sc, uint16_t offset, uint16_t data)
    350       1.2  garbled {
    351       1.2  garbled 
    352       1.2  garbled 	/* Check to make sure EEPROM is ready. */
    353       1.2  garbled 	if (!cs_wait_eeprom_ready(sc)) {
    354       1.4  tsutsui 		printf("%s: write EEPROM not ready\n",
    355       1.4  tsutsui 		    device_xname(sc->sc_dev));
    356       1.2  garbled 		return;
    357       1.2  garbled 	}
    358       1.2  garbled 
    359       1.2  garbled 	/* Enable writing. */
    360       1.2  garbled 	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_ENABLE);
    361       1.2  garbled 
    362       1.2  garbled 	/* Wait for WRITE_ENABLE command to complete. */
    363       1.2  garbled 	if (!cs_wait_eeprom_ready(sc)) {
    364       1.4  tsutsui 		printf("%s: EEPROM WRITE_ENABLE timeout",
    365       1.4  tsutsui 		    device_xname(sc->sc_dev));
    366       1.2  garbled 	} else {
    367       1.2  garbled 		/* Write data into EEPROM_DATA register. */
    368       1.2  garbled 		cs_writereg(sc, PKTPG_EEPROM_DATA, data);
    369       1.2  garbled 		delay(1000);
    370       1.2  garbled 		cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_WRITE | offset);
    371       1.2  garbled 
    372       1.2  garbled 		/* Wait for WRITE_REGISTER command to complete. */
    373       1.2  garbled 		if (!cs_wait_eeprom_ready(sc)) {
    374       1.2  garbled 			printf("%s: EEPROM WRITE_REGISTER timeout\n",
    375       1.4  tsutsui 			    device_xname(sc->sc_dev));
    376       1.2  garbled 		}
    377       1.2  garbled 	}
    378       1.2  garbled 
    379       1.2  garbled 	/* Disable writing. */
    380       1.2  garbled 	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_WRITE_DISABLE);
    381       1.2  garbled 
    382       1.2  garbled 	/* Wait for WRITE_DISABLE command to complete. */
    383       1.2  garbled 	if (!cs_wait_eeprom_ready(sc)) {
    384       1.4  tsutsui 		printf("%s: WRITE_DISABLE timeout\n", device_xname(sc->sc_dev));
    385       1.2  garbled 	}
    386       1.2  garbled }
    387       1.2  garbled 
    388       1.2  garbled static uint16_t
    389       1.2  garbled cs_rd_eeprom(struct cs_softc *sc, uint16_t offset)
    390       1.2  garbled {
    391       1.2  garbled 
    392       1.2  garbled 	if (!cs_wait_eeprom_ready(sc)) {
    393       1.4  tsutsui 		printf("%s: read EEPROM not ready\n", device_xname(sc->sc_dev));
    394       1.2  garbled 		return 0;
    395       1.2  garbled 	}
    396       1.2  garbled 	cs_writereg(sc, PKTPG_EEPROM_CMD, EEPROM_CMD_READ | offset);
    397       1.2  garbled 
    398       1.2  garbled 	if (!cs_wait_eeprom_ready(sc)) {
    399       1.4  tsutsui 		printf("%s: EEPROM_READ timeout\n", device_xname(sc->sc_dev));
    400       1.2  garbled 		return 0;
    401       1.2  garbled 	}
    402       1.2  garbled 	return cs_readreg(sc, PKTPG_EEPROM_DATA);
    403       1.2  garbled }
    404       1.2  garbled 
    405       1.2  garbled static void
    406       1.2  garbled cs_check_eeprom(struct cs_softc *sc)
    407       1.2  garbled {
    408       1.2  garbled 	uint8_t checksum;
    409       1.2  garbled 	int i;
    410       1.2  garbled         uint16_t tmp;
    411       1.2  garbled 
    412       1.2  garbled 	/*
    413       1.2  garbled 	 * If the SELFST[EEPROMOK] is set, then assume EEPROM configuration
    414       1.2  garbled 	 * is valid.
    415       1.2  garbled 	 */
    416       1.2  garbled 	if (cs_readreg(sc, PKTPG_SELF_ST) & SELF_ST_EEP_OK) {
    417       1.2  garbled 		printf("%s: EEPROM OK, skipping initialization\n",
    418       1.4  tsutsui 		    device_xname(sc->sc_dev));
    419       1.2  garbled 		return;
    420       1.2  garbled 	}
    421       1.4  tsutsui 	printf("%s: updating EEPROM\n", device_xname(sc->sc_dev));
    422       1.2  garbled 
    423       1.2  garbled 	/*
    424       1.2  garbled 	 * Calculate the size (in bytes) of the default config array and write
    425       1.2  garbled 	 * it to the lower byte of the array itself.
    426       1.2  garbled 	 */
    427       1.2  garbled 	default_eeprom_cfg[0] |= sizeof(default_eeprom_cfg);
    428       1.2  garbled 
    429       1.2  garbled 	/*
    430       1.2  garbled 	 * Read the MAC address from its Artesyn-specified offset in the EEPROM.
    431       1.2  garbled 	 */
    432       1.2  garbled 	for (i = 0; i < 3; i++) {
    433       1.2  garbled 		tmp = cs_rd_eeprom(sc, ATSN_EEPROM_MAC_OFFSET + i);
    434       1.2  garbled 		default_eeprom_cfg[EEPROM_MAC + i] = bswap16(tmp);
    435       1.2  garbled 	}
    436       1.2  garbled 
    437       1.2  garbled 	/*
    438       1.2  garbled 	 * Program the EEPROM with our default configuration,
    439       1.2  garbled 	 * calculating checksum as we proceed.
    440       1.2  garbled 	 */
    441       1.2  garbled 	checksum = 0;
    442       1.2  garbled 	for (i = 0; i < sizeof(default_eeprom_cfg)/2 ; i++) {
    443       1.2  garbled 		tmp = default_eeprom_cfg[i];
    444       1.2  garbled 		cs_wr_eeprom(sc, i, tmp);
    445       1.2  garbled 		checksum += tmp >> 8;
    446       1.2  garbled 		checksum += tmp & 0xff;
    447       1.2  garbled 	}
    448       1.2  garbled 
    449       1.2  garbled 	/*
    450       1.2  garbled 	 * The CS8900a datasheet calls for the two's complement of the checksum
    451       1.2  garbled 	 * to be prgrammed in the most significant byte of the last word of the
    452       1.2  garbled 	 * header.
    453       1.2  garbled 	 */
    454       1.2  garbled 	checksum = ~checksum + 1;
    455       1.2  garbled 	cs_wr_eeprom(sc, i++, checksum << 8);
    456       1.2  garbled 	/* write "end of data" flag */
    457       1.2  garbled 	cs_wr_eeprom(sc, i, 0xffff);
    458       1.2  garbled }
    459