Home | History | Annotate | Line # | Download | only in altboot
stg.c revision 1.2
      1 /* $NetBSD: stg.c,v 1.2 2011/03/08 19:00:38 phx Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 Frank Wille.
      5  * All rights reserved.
      6  *
      7  * Written by Frank Wille for The NetBSD Project.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/param.h>
     32 
     33 #include <netinet/in.h>
     34 #include <netinet/in_systm.h>
     35 
     36 #include <lib/libsa/stand.h>
     37 #include <lib/libsa/net.h>
     38 
     39 #include "globals.h"
     40 
     41 #define CSR_WRITE_1(l, r, v)	*(volatile uint8_t *)((l)->csr+(r)) = (v)
     42 #define CSR_READ_1(l, r)	*(volatile uint8_t *)((l)->csr+(r))
     43 #define CSR_WRITE_2(l, r, v)	out16rb((l)->csr+(r), (v))
     44 #define CSR_READ_2(l, r)	in16rb((l)->csr+(r))
     45 #define CSR_WRITE_4(l, r, v)	out32rb((l)->csr+(r), (v))
     46 #define CSR_READ_4(l, r)	in32rb((l)->csr+(r))
     47 #define VTOPHYS(va)		(uint32_t)(va)
     48 #define DEVTOV(pa)		(uint32_t)(pa)
     49 #define wbinv(adr, siz)		_wbinv(VTOPHYS(adr), (uint32_t)(siz))
     50 #define inv(adr, siz)		_inv(VTOPHYS(adr), (uint32_t)(siz))
     51 #define DELAY(n)		delay(n)
     52 #define ALLOC(T,A)		(T *)allocaligned(sizeof(T),(A))
     53 
     54 struct desc {
     55 	uint64_t xd0, xd1, xd2, dummy;
     56 };
     57 #define T1_EMPTY		(1U << 31)	/* no Tx frame available */
     58 #define T1_NOALIGN		(03 << 16)	/* allow any Tx alignment */
     59 #define T1_CNTSHIFT		24		/* Tx fragment count */
     60 #define T2_LENSHIFT		48		/* Tx frame length */
     61 #define R1_DONE			(1U << 31)	/* desc has a Rx frame */
     62 #define R1_FL_MASK		0xffff		/* Rx frame length */
     63 #define R1_ER_MASK		0x3f0000	/* Rx error indication */
     64 
     65 #define STGE_DMACtrl		0x00
     66 #define  DMAC_RxDMAComplete	(1U << 3)
     67 #define  DMAC_RxDMAPollNow	(1U << 4)
     68 #define  DMAC_TxDMAComplete	(1U << 11)
     69 #define  DMAC_TxDMAPollNow	(1U << 12)
     70 #define STGE_TFDListPtrLo	0x10
     71 #define STGE_TFDListPtrHi	0x14
     72 #define STGE_RFDListPtrLo	0x1c
     73 #define STGE_RFDListPtrHi	0x20
     74 #define STGE_DebugCtrl		0x2c
     75 #define STGE_AsicCtrl		0x30
     76 #define  AC_PhyMedia		(1U << 7)
     77 #define  AC_GlobalReset		(1U << 16)
     78 #define  AC_RxReset		(1U << 17)
     79 #define  AC_TxReset		(1U << 18)
     80 #define  AC_DMA			(1U << 19)
     81 #define  AC_FIFO		(1U << 20)
     82 #define  AC_Network		(1U << 21)
     83 #define  AC_Host		(1U << 22)
     84 #define  AC_AutoInit		(1U << 23)
     85 #define  AC_RstOut		(1U << 24)
     86 #define  AC_ResetBusy		(1U << 26)
     87 #define STGE_EepromData		0x48
     88 #define STGE_EepromCtrl		0x4a
     89 #define  EC_EepromAddress(x)	((x) & 0xff)
     90 #define  EC_EepromOpcode(x)	((x) << 8)
     91 #define  EC_OP_RR		2
     92 #define  EC_EepromBusy		(1U << 15)
     93 #define STGE_IntEnable		0x5c
     94 #define STGE_MACCtrl		0x6c
     95 #define  MC_DuplexSelect	(1U << 5)
     96 #define  MC_StatisticsDisable	(1U << 22)
     97 #define  MC_TxEnable		(1U << 24)
     98 #define  MC_RxEnable		(1U << 27)
     99 #define STGE_PhyCtrl		0x76
    100 #define  PC_MgmtClk		(1U << 0)
    101 #define  PC_MgmtData		(1U << 1)
    102 #define  PC_MgmtDir		(1U << 2)
    103 #define  PC_PhyDuplexPolarity	(1U << 3)
    104 #define  PC_PhyDuplexStatus	(1U << 4)
    105 #define  PC_PhyLnkPolarity	(1U << 5)
    106 #define  PC_LinkSpeed(x)	(((x) >> 6) & 3)
    107 #define  PC_LinkSpeed_Down	0
    108 #define  PC_LinkSpeed_10	1
    109 #define  PC_LinkSpeed_100	2
    110 #define  PC_LinkSpeed_1000	3
    111 #define STGE_StationAddress0	0x78
    112 #define STGE_StationAddress1	0x7a
    113 #define STGE_StationAddress2	0x7c
    114 #define STGE_MaxFrameSize	0x84
    115 #define STGE_ReceiveMode	0x88
    116 #define  RM_ReceiveUnicast	(1U << 0)
    117 #define  RM_ReceiveMulticast	(1U << 1)
    118 #define  RM_ReceiveBroadcast	(1U << 2)
    119 #define  RM_ReceiveAllFrames	(1U << 3)
    120 #define  RM_ReceiveMulticastHash (1U << 4)
    121 #define  RM_ReceiveIPMulticast	(1U << 5)
    122 
    123 #define STGE_EEPROM_SA0		0x10
    124 
    125 #define FRAMESIZE	1536
    126 
    127 struct local {
    128 	struct desc txd[2];
    129 	struct desc rxd[2];
    130 	uint8_t rxstore[2][FRAMESIZE];
    131 	unsigned csr, rx, tx, phy;
    132 	uint16_t bmsr, anlpar;
    133 	uint8_t phyctrl_saved;
    134 };
    135 
    136 static int mii_read(struct local *, int, int);
    137 static void mii_write(struct local *, int, int, int);
    138 static void mii_initphy(struct local *);
    139 static void mii_dealan(struct local *, unsigned);
    140 static void mii_bitbang_sync(struct local *);
    141 static void mii_bitbang_send(struct local *, uint32_t, int);
    142 static void mii_bitbang_clk(struct local *, uint8_t);
    143 static int eeprom_wait(struct local *);
    144 
    145 int
    146 stg_match(unsigned tag, void *data)
    147 {
    148 	unsigned v;
    149 
    150 	v = pcicfgread(tag, PCI_ID_REG);
    151 	switch (v) {
    152 	case PCI_DEVICE(0x13f0, 0x1023):	/* ST1023 */
    153 		return 1;
    154 	}
    155 	return 0;
    156 }
    157 
    158 void *
    159 stg_init(unsigned tag, void *data)
    160 {
    161 	struct local *l;
    162 	struct desc *txd, *rxd;
    163 	uint8_t *en;
    164 	unsigned i;
    165 	uint32_t macctl, reg;
    166 
    167 	l = ALLOC(struct local, 32);		/* desc alignment */
    168 	memset(l, 0, sizeof(struct local));
    169 	l->csr = DEVTOV(pcicfgread(tag, 0x14));	/* first try mem space */
    170 	if (l->csr == 0)
    171 		l->csr = DEVTOV(PCI_XIOBASE + (pcicfgread(tag, 0x10) & ~01));
    172 
    173 	/* reset the chip */
    174 	reg = CSR_READ_4(l, STGE_AsicCtrl);
    175 	CSR_WRITE_4(l, STGE_AsicCtrl, reg | AC_GlobalReset | AC_RxReset |
    176 	    AC_TxReset | AC_DMA | AC_FIFO | AC_Network | AC_Host |
    177 	    AC_AutoInit | ((reg & AC_PhyMedia) ? AC_RstOut : 0));
    178 	DELAY(50000);
    179 	for (i = 0; i < 1000; i++) {
    180 		DELAY(5000);
    181 		if ((CSR_READ_4(l, STGE_AsicCtrl) & AC_ResetBusy) == 0)
    182 			break;
    183 	}
    184 	if (i >= 1000)
    185 		printf("NIC reset failed to complete!\n");
    186 	DELAY(1000);
    187 
    188 	mii_initphy(l);
    189 
    190 	/* read ethernet address */
    191 	en = data;
    192 	if (PCI_PRODUCT(pcicfgread(tag, PCI_ID_REG)) != 0x1023) {
    193 		/* read from station address registers when not ST1023 */
    194 		en[0] = CSR_READ_2(l, STGE_StationAddress0) & 0xff;
    195 		en[1] = CSR_READ_2(l, STGE_StationAddress0) >> 8;
    196 		en[2] = CSR_READ_2(l, STGE_StationAddress1) & 0xff;
    197 		en[3] = CSR_READ_2(l, STGE_StationAddress1) >> 8;
    198 		en[4] = CSR_READ_2(l, STGE_StationAddress2) & 0xff;
    199 		en[5] = CSR_READ_2(l, STGE_StationAddress2) >> 8;
    200 	} else {
    201 		/* ST1023: read the address from the serial EEPROM */
    202 		static uint8_t bad[2][6] = {
    203 			{ 0x00,0x00,0x00,0x00,0x00,0x00 },
    204 			{ 0xff,0xff,0xff,0xff,0xff,0xff }
    205 		};
    206 		uint16_t addr[3];
    207 
    208 		for (i = 0; i < 3; i++) {
    209 			if (eeprom_wait(l) != 0)
    210 				printf("NIC: serial EEPROM is not ready!\n");
    211 			CSR_WRITE_2(l, STGE_EepromCtrl,
    212 			    EC_EepromAddress(STGE_EEPROM_SA0 + i) |
    213 			    EC_EepromOpcode(EC_OP_RR));
    214 			if (eeprom_wait(l) != 0)
    215 				printf("NIC: serial EEPROM read time out!\n");
    216 			addr[i] = le16toh(CSR_READ_2(l, STGE_EepromData));
    217 		}
    218 		(void)memcpy(en, addr, 6);
    219 
    220 		/* try to read MAC from Flash, when EEPROM is empty/missing */
    221 		if (memcmp(en, bad[0], 6) == 0 || memcmp(en, bad[1], 6) == 0)
    222 			read_mac_from_flash(en);
    223 
    224 		/* set the station address now */
    225 		for (i = 0; i < 6; i++)
    226 			CSR_WRITE_1(l, STGE_StationAddress0 + i, en[i]);
    227 	}
    228 	printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
    229 	    en[0], en[1], en[2], en[3], en[4], en[5]);
    230 
    231 	DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
    232 	    mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
    233 
    234 	/* setup descriptors */
    235 	txd = &l->txd[0];
    236 	txd[0].xd0 = htole64(VTOPHYS(&txd[1]));
    237 	txd[0].xd1 = htole64(T1_EMPTY);
    238 	txd[1].xd0 = htole64(VTOPHYS(&txd[0]));
    239 	txd[1].xd1 = htole64(T1_EMPTY);
    240 	rxd = &l->rxd[0];
    241 	rxd[0].xd0 = htole64(VTOPHYS(&rxd[1]));
    242 	rxd[0].xd2 = htole64((uint64_t)VTOPHYS(l->rxstore[0]) |
    243 	    ((uint64_t)FRAMESIZE << 48));
    244 	rxd[1].xd0 = htole64(VTOPHYS(&rxd[0]));
    245 	rxd[1].xd2 = htole64((uint64_t)VTOPHYS(l->rxstore[1]) |
    246 	    ((uint64_t)FRAMESIZE << 48));
    247 	wbinv(l, sizeof(struct local));
    248 
    249 	CSR_WRITE_2(l, STGE_IntEnable, 0);
    250 	CSR_WRITE_2(l, STGE_ReceiveMode, RM_ReceiveUnicast |
    251 	    RM_ReceiveBroadcast | RM_ReceiveAllFrames | RM_ReceiveMulticast);
    252 	CSR_WRITE_4(l, STGE_TFDListPtrHi, 0);
    253 	CSR_WRITE_4(l, STGE_TFDListPtrLo, VTOPHYS(txd));
    254 	CSR_WRITE_4(l, STGE_RFDListPtrHi, 0);
    255 	CSR_WRITE_4(l, STGE_RFDListPtrLo, VTOPHYS(rxd));
    256 	CSR_WRITE_2(l, STGE_MaxFrameSize, FRAMESIZE);
    257 	CSR_WRITE_4(l, STGE_MACCtrl, 0);	/* do IFSSelect(0) first */
    258 	macctl = MC_StatisticsDisable | MC_TxEnable | MC_RxEnable;
    259 
    260 	if ((pcicfgread(tag, PCI_CLASS_REG) & 0xff) >= 6) {
    261 		/* some workarounds for revisions >= 6 */
    262 		CSR_WRITE_2(l, STGE_DebugCtrl,
    263 		    CSR_READ_2(l, STGE_DebugCtrl) | 0x0200);
    264 		CSR_WRITE_2(l, STGE_DebugCtrl,
    265 		    CSR_READ_2(l, STGE_DebugCtrl) | 0x0010);
    266 		CSR_WRITE_2(l, STGE_DebugCtrl,
    267 		    CSR_READ_2(l, STGE_DebugCtrl) | 0x0020);
    268 	}
    269 
    270 	/* auto negotiation, set the current media */
    271 	mii_dealan(l, 5);
    272 
    273 	reg = CSR_READ_1(l, STGE_PhyCtrl);
    274 	switch (PC_LinkSpeed(reg)) {
    275 	case PC_LinkSpeed_1000:
    276 		printf("1000Mbps");
    277 		break;
    278 	case PC_LinkSpeed_100:
    279 		printf("100Mbps");
    280 		break;
    281 	case PC_LinkSpeed_10:
    282 		printf("10Mbps");
    283 		break;
    284 	}
    285 	if (reg & PC_PhyDuplexStatus) {
    286 		macctl |= MC_DuplexSelect;
    287 		printf("-FDX");
    288 	}
    289 	printf("\n");
    290 	CSR_WRITE_4(l, STGE_MACCtrl, macctl);
    291 
    292 	return l;
    293 }
    294 
    295 int
    296 stg_send(void *dev, char *buf, unsigned len)
    297 {
    298 	struct local *l = dev;
    299 	volatile struct desc *txd;
    300 	unsigned loop;
    301 
    302 	wbinv(buf, len);
    303 	txd = &l->txd[l->tx];
    304 	txd->xd2 = htole64(VTOPHYS(buf) | ((uint64_t)len << 48));
    305 	txd->xd1 = htole64(T1_NOALIGN | (1 << 24));
    306 	wbinv(txd, sizeof(struct desc));
    307 	CSR_WRITE_4(l, STGE_DMACtrl, DMAC_TxDMAPollNow);
    308 	loop = 100;
    309 	do {
    310 		if ((le64toh(txd->xd1) & T1_EMPTY) != 0)
    311 			goto done;
    312 		DELAY(10);
    313 		inv(txd, sizeof(struct desc));
    314 	} while (--loop > 0);
    315 	printf("xmit failed\n");
    316 	return -1;
    317   done:
    318 	l->tx ^= 1;
    319 	return len;
    320 }
    321 
    322 int
    323 stg_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
    324 {
    325 	struct local *l = dev;
    326 	volatile struct desc *rxd;
    327 	uint32_t sts;
    328 	unsigned bound, len;
    329 	uint8_t *ptr;
    330 
    331 	bound = 1000 * timo;
    332   again:
    333 	rxd = &l->rxd[l->rx];
    334 	do {
    335 		inv(rxd, sizeof(struct desc));
    336 		sts = (uint32_t)le64toh(rxd->xd1);
    337 		if ((sts & R1_DONE) != 0)
    338 			goto gotone;
    339 		DELAY(1000);	/* 1 milli second */
    340 	} while (--bound > 0);
    341 	errno = 0;
    342 	return -1;
    343   gotone:
    344 	if ((sts & R1_ER_MASK) != 0) {
    345 		rxd->xd1 = 0;
    346 		wbinv(rxd, sizeof(struct desc));
    347 		l->rx ^= 1;
    348 		goto again;
    349 	}
    350 	len = sts & R1_FL_MASK;
    351 	if (len > maxlen)
    352 		len = maxlen;
    353 	ptr = l->rxstore[l->rx];
    354 	inv(ptr, len);
    355 	memcpy(buf, ptr, len);
    356 	rxd->xd1 = 0;
    357 	wbinv(rxd, sizeof(struct desc));
    358 	l->rx ^= 1;
    359 	return len;
    360 }
    361 
    362 #define R0110	6		/* 0110b read op */
    363 #define W0101	5		/* 0101b write op */
    364 #define A10	2		/* 10b ack turn around */
    365 
    366 /* read the MII by bitbanging STGE_PhyCtrl */
    367 static int
    368 mii_read(struct local *l, int phy, int reg)
    369 {
    370 	unsigned data;
    371 	int i;
    372 	uint8_t v;
    373 
    374 	/* initiate read access */
    375 	data = (R0110 << 10) | (phy << 5) | reg;
    376 	mii_bitbang_sync(l);
    377 	mii_bitbang_send(l, data, 14); /* 4OP + 5PHY + 5REG */
    378 
    379 	/* switch direction to PHY->host */
    380 	v = l->phyctrl_saved;
    381 	CSR_WRITE_1(l, STGE_PhyCtrl, v);
    382 
    383 	/* read data */
    384 	data = 0;
    385 	for (i = 0; i < 18; i++) { /* 2TA + 16DATA */
    386 		data <<= 1;
    387 		data |= !!(CSR_READ_1(l, STGE_PhyCtrl) & PC_MgmtData);
    388 		mii_bitbang_clk(l, v);
    389 	}
    390 
    391 	return data & 0xffff;
    392 }
    393 
    394 /* write the MII by bitbanging STGE_PhyCtrl */
    395 static void
    396 mii_write(struct local *l, int phy, int reg, int val)
    397 {
    398 	unsigned data;
    399 
    400 	data = (W0101 << 28) | (phy << 23) | (reg << 18) | (A10 << 16);
    401 	data |= val;
    402 
    403 	mii_bitbang_sync(l);
    404 	mii_bitbang_send(l, data, 32); /* 4OP + 5PHY + 5REG + 2TA + 16DATA */
    405 }
    406 
    407 #define MII_BMCR	0x00	/* Basic mode control register (rw) */
    408 #define  BMCR_RESET	0x8000	/* reset */
    409 #define  BMCR_AUTOEN	0x1000	/* autonegotiation enable */
    410 #define  BMCR_ISO	0x0400	/* isolate */
    411 #define  BMCR_STARTNEG	0x0200	/* restart autonegotiation */
    412 #define MII_BMSR	0x01	/* Basic mode status register (ro) */
    413 #define  BMSR_ACOMP	0x0020	/* Autonegotiation complete */
    414 #define  BMSR_LINK	0x0004	/* Link status */
    415 #define MII_ANAR	0x04	/* Autonegotiation advertisement (rw) */
    416 #define  ANAR_FC	0x0400	/* local device supports PAUSE */
    417 #define  ANAR_TX_FD	0x0100	/* local device supports 100bTx FD */
    418 #define  ANAR_TX	0x0080	/* local device supports 100bTx */
    419 #define  ANAR_10_FD	0x0040	/* local device supports 10bT FD */
    420 #define  ANAR_10	0x0020	/* local device supports 10bT */
    421 #define  ANAR_CSMA	0x0001	/* protocol selector CSMA/CD */
    422 #define MII_ANLPAR	0x05	/* Autonegotiation lnk partner abilities (rw) */
    423 
    424 static void
    425 mii_initphy(struct local *l)
    426 {
    427 	int phy, ctl, sts, bound;
    428 
    429 	l->phyctrl_saved = CSR_READ_1(l, STGE_PhyCtrl) &
    430 	    (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
    431 
    432 	for (phy = 0; phy < 32; phy++) {
    433 		ctl = mii_read(l, phy, MII_BMCR);
    434 		sts = mii_read(l, phy, MII_BMSR);
    435 		if (ctl != 0xffff && sts != 0xffff && sts != 0)
    436 			goto found;
    437 	}
    438 	printf("MII: no PHY found\n");
    439 	return;
    440 
    441   found:
    442 	ctl = mii_read(l, phy, MII_BMCR);
    443 	mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
    444 
    445 	bound = 100;
    446 	do {
    447 		DELAY(10);
    448 		ctl = mii_read(l, phy, MII_BMCR);
    449 		if (ctl == 0xffff) {
    450 			printf("MII: PHY %d has died after reset\n", phy);
    451 			return;
    452 		}
    453 	} while (bound-- > 0 && (ctl & BMCR_RESET));
    454 	if (bound == 0)
    455 		printf("PHY %d reset failed\n", phy);
    456 
    457 	ctl &= ~BMCR_ISO;
    458 	mii_write(l, phy, MII_BMCR, ctl);
    459 	sts = mii_read(l, phy, MII_BMSR) |
    460 	    mii_read(l, phy, MII_BMSR); /* read twice */
    461 	l->phy = phy;
    462 	l->bmsr = sts;
    463 }
    464 
    465 static void
    466 mii_dealan(struct local *l, unsigned timo)
    467 {
    468 	unsigned anar, bound;
    469 
    470 	anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
    471 	mii_write(l, l->phy, MII_ANAR, anar);
    472 	mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    473 	l->anlpar = 0;
    474 	bound = getsecs() + timo;
    475 	do {
    476 		l->bmsr = mii_read(l, l->phy, MII_BMSR) |
    477 		   mii_read(l, l->phy, MII_BMSR); /* read twice */
    478 		if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
    479 			l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
    480 			break;
    481 		}
    482 		DELAY(10 * 1000);
    483 	} while (getsecs() < bound);
    484 }
    485 
    486 static void
    487 mii_bitbang_sync(struct local *l)
    488 {
    489 	int i;
    490 	uint8_t v;
    491 
    492 	v = l->phyctrl_saved | PC_MgmtDir | PC_MgmtData;
    493 	CSR_WRITE_1(l, STGE_PhyCtrl, v);
    494 	DELAY(1);
    495 	for (i = 0; i < 32; i++)
    496 		mii_bitbang_clk(l, v);
    497 }
    498 
    499 static void
    500 mii_bitbang_send(struct local *l, uint32_t data, int nbits)
    501 {
    502 	uint32_t i;
    503 	uint8_t v;
    504 
    505 	v = l->phyctrl_saved | PC_MgmtDir;
    506 	CSR_WRITE_1(l, STGE_PhyCtrl, v);
    507 	DELAY(1);
    508 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
    509 		if (data & i)
    510 			v |= PC_MgmtData;
    511 		else
    512 			v &= ~PC_MgmtData;
    513 		CSR_WRITE_1(l, STGE_PhyCtrl, v);
    514 		DELAY(1);
    515 		mii_bitbang_clk(l, v);
    516 	}
    517 }
    518 
    519 static void
    520 mii_bitbang_clk(struct local *l, uint8_t v)
    521 {
    522 
    523 	CSR_WRITE_1(l, STGE_PhyCtrl, v | PC_MgmtClk);
    524 	DELAY(1);
    525 	CSR_WRITE_1(l, STGE_PhyCtrl, v);
    526 	DELAY(1);
    527 }
    528 
    529 static int
    530 eeprom_wait(struct local *l)
    531 {
    532 	int i;
    533 
    534 	for (i = 0; i < 1000; i++) {
    535 		DELAY(1000);
    536 		if ((CSR_READ_2(l, STGE_EepromCtrl) & EC_EepromBusy) == 0)
    537 			return 0;
    538 	}
    539 	return 1;
    540 }
    541