Home | History | Annotate | Line # | Download | only in altboot
skg.c revision 1.4
      1 /* $NetBSD: skg.c,v 1.4 2011/10/30 21:08:33 phx Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 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 /*
     42  * - reverse endian access every CSR.
     43  * - no vtophys() translation, vaddr_t == paddr_t.
     44  * - PIPT writeback cache aware.
     45  */
     46 #define CSR_WRITE_1(l, r, v)	out8((l)->csr+(r), (v))
     47 #define CSR_READ_1(l, r)	in8((l)->csr+(r))
     48 #define CSR_WRITE_2(l, r, v)	out16rb((l)->csr+(r), (v))
     49 #define CSR_READ_2(l, r)	in16rb((l)->csr+(r))
     50 #define CSR_WRITE_4(l, r, v)	out32rb((l)->csr+(r), (v))
     51 #define CSR_READ_4(l, r)	in32rb((l)->csr+(r))
     52 #define VTOPHYS(va)		(uint32_t)(va)
     53 #define DEVTOV(pa)		(uint32_t)(pa)
     54 #define wbinv(adr, siz)		_wbinv(VTOPHYS(adr), (uint32_t)(siz))
     55 #define inv(adr, siz)		_inv(VTOPHYS(adr), (uint32_t)(siz))
     56 #define DELAY(n)		delay(n)
     57 #define ALLOC(T,A)		(T *)allocaligned(sizeof(T),(A))
     58 
     59 struct desc {
     60 	uint32_t xd0, xd1, xd2, xd3, xd4;
     61 	uint32_t rsrvd[3];
     62 };
     63 #define CTL_LS			0x20000000
     64 #define CTL_FS			0x40000000
     65 #define CTL_OWN			0x80000000
     66 #define CTL_DEFOPC		0x00550000
     67 #define FRAMEMASK		0x0000ffff
     68 #define RXSTAT_RXOK		0x00000100
     69 
     70 #define SK_CSR			0x0004
     71 #define  CSR_SW_RESET		0x0001
     72 #define  CSR_SW_UNRESET		0x0002
     73 #define  CSR_MASTER_RESET	0x0004
     74 #define  CSR_MASTER_UNRESET	0x0008
     75 #define SK_IMR			0x000c
     76 #define SK_BMU_RX_CSR0		0x0060
     77 #define SK_BMU_TXS_CSR0		0x0068
     78 #define SK_MAC0			0x0100
     79 #define SK_MAC1			0x0108
     80 #define SK_GPIO			0x015c
     81 #define SK_RAMCTL		0x01a0
     82 #define SK_TXAR1_COUNTERCTL	0x0210
     83 #define  TXARCTL_ON		0x02
     84 #define  TXARCTL_FSYNC_ON       0x80
     85 #define SK_RXQ1_CURADDR_LO	0x0420
     86 #define SK_RXQ1_CURADDR_HI	0x0424
     87 #define SK_RXQ1_BMU_CSR		0x0434
     88 #define  RXBMU_CLR_IRQ_EOF		0x00000002
     89 #define  RXBMU_RX_START			0x00000010
     90 #define  RXBMU_RX_STOP			0x00000020
     91 #define  RXBMU_POLL_ON			0x00000080
     92 #define  RXBMU_TRANSFER_SM_UNRESET	0x00000200
     93 #define  RXBMU_DESCWR_SM_UNRESET	0x00000800
     94 #define  RXBMU_DESCRD_SM_UNRESET	0x00002000
     95 #define  RXBMU_SUPERVISOR_SM_UNRESET	0x00008000
     96 #define  RXBMU_PFI_SM_UNRESET		0x00020000
     97 #define  RXBMU_FIFO_UNRESET		0x00080000
     98 #define  RXBMU_DESC_UNRESET		0x00200000
     99 #define SK_TXQS1_CURADDR_LO	0x0620
    100 #define SK_TXQS1_CURADDR_HI	0x0624
    101 #define SK_TXQS1_BMU_CSR	0x0634
    102 #define  TXBMU_CLR_IRQ_EOF		0x00000002
    103 #define  TXBMU_TX_START			0x00000010
    104 #define  TXBMU_TX_STOP			0x00000020
    105 #define  TXBMU_POLL_ON			0x00000080
    106 #define  TXBMU_TRANSFER_SM_UNRESET	0x00000200
    107 #define  TXBMU_DESCWR_SM_UNRESET	0x00000800
    108 #define  TXBMU_DESCRD_SM_UNRESET	0x00002000
    109 #define  TXBMU_SUPERVISOR_SM_UNRESET	0x00008000
    110 #define  TXBMU_PFI_SM_UNRESET		0x00020000
    111 #define  TXBMU_FIFO_UNRESET		0x00080000
    112 #define  TXBMU_DESC_UNRESET		0x00200000
    113 #define SK_RXRB1_START		0x0800
    114 #define SK_RXRB1_END		0x0804
    115 #define SK_RXRB1_WR_PTR		0x0808
    116 #define SK_RXRB1_RD_PTR		0x080c
    117 #define SK_RXRB1_CTLTST		0x0828
    118 #define  RBCTL_UNRESET		0x02
    119 #define  RBCTL_ON		0x08
    120 #define  RBCTL_STORENFWD_ON	0x20
    121 #define SK_TXRBS1_START		0x0a00
    122 #define SK_TXRBS1_END		0x0a04
    123 #define SK_TXRBS1_WR_PTR	0x0a08
    124 #define SK_TXRBS1_RD_PTR	0x0a0c
    125 #define SK_TXRBS1_CTLTST	0x0a28
    126 #define SK_RXMF1_CTRL_TEST	0x0c48
    127 #define  RFCTL_OPERATION_ON	0x00000008
    128 #define  RFCTL_RESET_CLEAR	0x00000002
    129 #define SK_TXMF1_CTRL_TEST	0x0D48
    130 #define  TFCTL_OPERATION_ON	0x00000008
    131 #define  TFCTL_RESET_CLEAR	0x00000002
    132 #define SK_GMAC_CTRL		0x0f00
    133 #define  GMAC_LOOP_OFF		0x00000010
    134 #define  GMAC_PAUSE_ON		0x00000008
    135 #define  GMAC_RESET_CLEAR	0x00000002
    136 #define  GMAC_RESET_SET		0x00000001
    137 #define SK_GPHY_CTRL		0x0f04
    138 #define  GPHY_INT_POL_HI	0x08000000
    139 #define  GPHY_DIS_FC		0x02000000
    140 #define  GPHY_DIS_SLEEP		0x01000000
    141 #define  GPHY_ENA_XC		0x00040000
    142 #define  GPHY_ENA_PAUSE		0x00002000
    143 #define  GPHY_RESET_CLEAR	0x00000002
    144 #define  GPHY_RESET_SET		0x00000001
    145 #define  GPHY_ANEG_ALL		0x0009c000
    146 #define  GPHY_COPPER		0x00f00000
    147 #define SK_LINK_CTRL		0x0f10
    148 #define  LINK_RESET_CLEAR	0x0002
    149 #define  LINK_RESET_SET		0x0001
    150 
    151 #define YUKON_GPCR		0x2804
    152 #define  GPCR_TXEN		0x1000
    153 #define  GPCR_RXEN		0x0800
    154 #define YUKON_SA1		0x281c
    155 #define YUKON_SA2		0x2828
    156 #define YUKON_SMICR		0x2880
    157 #define  SMICR_PHYAD(x)		(((x) & 0x1f) << 11)
    158 #define  SMICR_REGAD(x)		(((x) & 0x1f) << 6)
    159 #define  SMICR_OP_READ		0x0020
    160 #define  SMICR_OP_WRITE		0x0000
    161 #define  SMICR_READ_VALID	0x0010
    162 #define  SMICR_BUSY		0x0008
    163 #define YUKON_SMIDR		0x2884
    164 
    165 #define MII_PSSR		0x11	/* MAKPHY status register */
    166 #define  PSSR_DUPLEX		0x2000	/* FDX */
    167 #define  PSSR_RESOLVED		0x0800	/* speed and duplex resolved */
    168 #define  PSSR_LINK		0x0400  /* link indication */
    169 #define  PSSR_SPEED(x)		(((x) >> 14) & 0x3)
    170 #define  SPEED10		0
    171 #define  SPEED100		1
    172 #define  SPEED1000 		2
    173 
    174 #define FRAMESIZE	1536
    175 
    176 struct local {
    177 	struct desc txd[2];
    178 	struct desc rxd[2];
    179 	uint8_t rxstore[2][FRAMESIZE];
    180 	unsigned csr, rx, tx, phy;
    181 	uint16_t pssr, anlpar;
    182 };
    183 
    184 static int mii_read(struct local *, int, int);
    185 static void mii_write(struct local *, int, int, int);
    186 static void mii_initphy(struct local *);
    187 static void mii_dealan(struct local *, unsigned);
    188 
    189 int
    190 skg_match(unsigned tag, void *data)
    191 {
    192 	unsigned v;
    193 
    194 	v = pcicfgread(tag, PCI_ID_REG);
    195 	switch (v) {
    196 	case PCI_DEVICE(0x11ab, 0x4320):
    197 		return 1;
    198 	}
    199 	return 0;
    200 }
    201 
    202 void *
    203 skg_init(unsigned tag, void *data)
    204 {
    205 	struct local *l;
    206 	struct desc *txd, *rxd;
    207 	uint8_t *en;
    208 	unsigned i;
    209 	uint16_t reg;
    210 
    211 	l = ALLOC(struct local, 32);	/* desc alignment */
    212 	memset(l, 0, sizeof(struct local));
    213 	l->csr = DEVTOV(pcicfgread(tag, 0x10)); /* use mem space */
    214 
    215 	/* make sure the descriptor bytes are not reversed */
    216 	i = pcicfgread(tag, 0x44);
    217 	pcicfgwrite(tag, 0x44, i & ~4);
    218 
    219 	/* reset the chip */
    220 	CSR_WRITE_2(l, SK_CSR, CSR_SW_RESET);
    221 	CSR_WRITE_2(l, SK_CSR, CSR_MASTER_RESET);
    222 	CSR_WRITE_2(l, SK_LINK_CTRL, LINK_RESET_SET);
    223 	DELAY(1000);
    224 	CSR_WRITE_2(l, SK_CSR, CSR_SW_UNRESET);
    225 	DELAY(2);
    226 	CSR_WRITE_2(l, SK_CSR, CSR_MASTER_UNRESET);
    227 	CSR_WRITE_2(l, SK_LINK_CTRL, LINK_RESET_CLEAR);
    228 	CSR_WRITE_4(l, SK_RAMCTL, 2);	/* enable RAM interface */
    229 
    230 	mii_initphy(l);
    231 
    232 	/* read ethernet address */
    233 	en = data;
    234 	if (brdtype == BRD_SYNOLOGY)
    235 		read_mac_from_flash(en);
    236 	else
    237 		for (i = 0; i < 6; i++)
    238 			en[i] = CSR_READ_1(l, SK_MAC0 + i);
    239 	printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
    240 	    en[0], en[1], en[2], en[3], en[4], en[5]);
    241 	DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
    242 	    mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
    243 
    244 	/* set station address */
    245 	for (i = 0; i < 3; i++)
    246 		CSR_WRITE_2(l, YUKON_SA1 + i * 4,
    247 		    (en[i * 2] << 8) | en[i * 2 + 1]);
    248 
    249 	/* configure RX and TX MAC FIFO */
    250 	CSR_WRITE_1(l, SK_RXMF1_CTRL_TEST, RFCTL_RESET_CLEAR);
    251 	CSR_WRITE_4(l, SK_RXMF1_CTRL_TEST, RFCTL_OPERATION_ON);
    252 	CSR_WRITE_1(l, SK_TXMF1_CTRL_TEST, TFCTL_RESET_CLEAR);
    253 	CSR_WRITE_4(l, SK_TXMF1_CTRL_TEST, TFCTL_OPERATION_ON);
    254 
    255 	mii_dealan(l, 5);
    256 
    257 	switch (PSSR_SPEED(l->pssr)) {
    258 	case SPEED1000:
    259 		printf("1000Mbps");
    260 		break;
    261 	case SPEED100:
    262 		printf("100Mbps");
    263 		break;
    264 	case SPEED10:
    265 		printf("10Mbps");
    266 		break;
    267 	}
    268 	if (l->pssr & PSSR_DUPLEX)
    269 		printf("-FDX");
    270 	printf("\n");
    271 
    272 	/* configure RAM buffers, assuming 64k RAM */
    273 	CSR_WRITE_4(l, SK_RXRB1_CTLTST, RBCTL_UNRESET);
    274 	CSR_WRITE_4(l, SK_RXRB1_START, 0);
    275 	CSR_WRITE_4(l, SK_RXRB1_WR_PTR, 0);
    276 	CSR_WRITE_4(l, SK_RXRB1_RD_PTR, 0);
    277 	CSR_WRITE_4(l, SK_RXRB1_END, 0xfff);
    278 	CSR_WRITE_4(l, SK_RXRB1_CTLTST, RBCTL_ON);
    279 	CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_UNRESET);
    280 	CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_STORENFWD_ON);
    281 	CSR_WRITE_4(l, SK_TXRBS1_START, 0x1000);
    282 	CSR_WRITE_4(l, SK_TXRBS1_WR_PTR, 0x1000);
    283 	CSR_WRITE_4(l, SK_TXRBS1_RD_PTR, 0x1000);
    284 	CSR_WRITE_4(l, SK_TXRBS1_END, 0x1fff);
    285 	CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_ON);
    286 
    287 	/* setup descriptors and BMU */
    288 	CSR_WRITE_1(l, SK_TXAR1_COUNTERCTL, TXARCTL_ON|TXARCTL_FSYNC_ON);
    289 
    290 	txd = &l->txd[0];
    291 	txd[0].xd1 = htole32(VTOPHYS(&txd[1]));
    292 	txd[1].xd1 = htole32(VTOPHYS(&txd[0]));
    293 	rxd = &l->rxd[0];
    294 	rxd[0].xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
    295 	rxd[0].xd1 = htole32(VTOPHYS(&rxd[1]));
    296 	rxd[0].xd2 = htole32(VTOPHYS(l->rxstore[0]));
    297 	rxd[1].xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
    298 	rxd[1].xd1 = htole32(VTOPHYS(&rxd[0]));
    299 	rxd[1].xd2 = htole32(VTOPHYS(l->rxstore[1]));
    300 	wbinv(l, sizeof(struct local));
    301 
    302 	CSR_WRITE_4(l, SK_RXQ1_BMU_CSR,
    303 	    RXBMU_TRANSFER_SM_UNRESET|RXBMU_DESCWR_SM_UNRESET|
    304 	    RXBMU_DESCRD_SM_UNRESET|RXBMU_SUPERVISOR_SM_UNRESET|
    305 	    RXBMU_PFI_SM_UNRESET|RXBMU_FIFO_UNRESET|
    306 	    RXBMU_DESC_UNRESET);
    307 	CSR_WRITE_4(l, SK_RXQ1_CURADDR_LO, VTOPHYS(rxd));
    308 	CSR_WRITE_4(l, SK_RXQ1_CURADDR_HI, 0);
    309 
    310 	CSR_WRITE_4(l, SK_TXQS1_BMU_CSR,
    311 	    TXBMU_TRANSFER_SM_UNRESET|TXBMU_DESCWR_SM_UNRESET|
    312 	    TXBMU_DESCRD_SM_UNRESET|TXBMU_SUPERVISOR_SM_UNRESET|
    313 	    TXBMU_PFI_SM_UNRESET|TXBMU_FIFO_UNRESET|
    314 	    TXBMU_DESC_UNRESET|TXBMU_POLL_ON);
    315 	CSR_WRITE_4(l, SK_TXQS1_CURADDR_LO, VTOPHYS(txd));
    316 	CSR_WRITE_4(l, SK_TXQS1_CURADDR_HI, 0);
    317 
    318 	CSR_WRITE_4(l, SK_IMR, 0);
    319 	CSR_WRITE_4(l, SK_RXQ1_BMU_CSR, RXBMU_RX_START);
    320 	reg = CSR_READ_2(l, YUKON_GPCR);
    321 	reg |= GPCR_TXEN | GPCR_RXEN;
    322 	CSR_WRITE_2(l, YUKON_GPCR, reg);
    323 
    324 	return l;
    325 }
    326 
    327 int
    328 skg_send(void *dev, char *buf, unsigned len)
    329 {
    330 	struct local *l = dev;
    331 	volatile struct desc *txd;
    332 	unsigned loop;
    333 
    334 	wbinv(buf, len);
    335 	txd = &l->txd[l->tx];
    336 	txd->xd2 = htole32(VTOPHYS(buf));
    337 	txd->xd0 = htole32((len & FRAMEMASK)|CTL_DEFOPC|CTL_FS|CTL_LS|CTL_OWN);
    338 	wbinv(txd, sizeof(struct desc));
    339 	CSR_WRITE_4(l, SK_BMU_TXS_CSR0, TXBMU_TX_START);
    340 	loop = 100;
    341 	do {
    342 		if ((le32toh(txd->xd0) & CTL_OWN) == 0)
    343 			goto done;
    344 		DELAY(10);
    345 		inv(txd, sizeof(struct desc));
    346 	} while (--loop > 0);
    347 	printf("xmit failed\n");
    348 	return -1;
    349   done:
    350 	l->tx ^= 1;
    351 	return len;
    352 }
    353 
    354 int
    355 skg_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
    356 {
    357 	struct local *l = dev;
    358 	volatile struct desc *rxd;
    359 	unsigned bound, ctl, rxstat, len;
    360 	uint8_t *ptr;
    361 
    362 	bound = 1000 * timo;
    363 #if 0
    364 printf("recving with %u sec. timeout\n", timo);
    365 #endif
    366   again:
    367 	rxd = &l->rxd[l->rx];
    368 	do {
    369 		inv(rxd, sizeof(struct desc));
    370 		ctl = le32toh(rxd->xd0);
    371 		if ((ctl & CTL_OWN) == 0)
    372 			goto gotone;
    373 		DELAY(1000);	/* 1 milli second */
    374 	} while (--bound > 0);
    375 	errno = 0;
    376 	return -1;
    377   gotone:
    378   	rxstat = le32toh(rxd->xd4);
    379 	if ((rxstat & RXSTAT_RXOK) == 0) {
    380 		rxd->xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
    381 		wbinv(rxd, sizeof(struct desc));
    382 		l->rx ^= 1;
    383 		goto again;
    384 	}
    385 	len = ctl & FRAMEMASK;
    386 	if (len > maxlen)
    387 		len = maxlen;
    388 	ptr = l->rxstore[l->rx];
    389 	inv(ptr, len);
    390 	memcpy(buf, ptr, len);
    391 	rxd->xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
    392 	wbinv(rxd, sizeof(struct desc));
    393 	l->rx ^= 1;
    394 	return len;
    395 }
    396 
    397 static int
    398 mii_read(struct local *l, int phy, int reg)
    399 {
    400 	unsigned loop, v;
    401 
    402 	CSR_WRITE_2(l, YUKON_SMICR, SMICR_PHYAD(phy) | SMICR_REGAD(reg) |
    403 	    SMICR_OP_READ);
    404 	loop = 1000;
    405 	do {
    406 		DELAY(1);
    407 		v = CSR_READ_2(l, YUKON_SMICR);
    408 	} while ((v & SMICR_READ_VALID) == 0 && --loop);
    409 	if (loop == 0) {
    410 		printf("mii_read timeout!\n");
    411 		return 0;
    412 	}
    413 	return CSR_READ_2(l, YUKON_SMIDR);
    414 }
    415 
    416 static void
    417 mii_write(struct local *l, int phy, int reg, int data)
    418 {
    419 	unsigned loop, v;
    420 
    421 	CSR_WRITE_2(l, YUKON_SMIDR, data);
    422 	CSR_WRITE_2(l, YUKON_SMICR, SMICR_PHYAD(phy) | SMICR_REGAD(reg) |
    423 	    SMICR_OP_WRITE);
    424 	loop = 1000;
    425 	do {
    426 		DELAY(1);
    427 		v = CSR_READ_2(l, YUKON_SMICR);
    428 	} while ((v & SMICR_BUSY) != 0 && --loop);
    429 	if (loop == 0)
    430 		printf("mii_write timeout!\n");
    431 }
    432 
    433 #define MII_BMCR	0x00	/* Basic mode control register (rw) */
    434 #define  BMCR_AUTOEN	0x1000	/* autonegotiation enable */
    435 #define  BMCR_STARTNEG	0x0200	/* restart autonegotiation */
    436 #define MII_BMSR	0x01	/* Basic mode status register (ro) */
    437 #define  BMSR_ACOMP	0x0020	/* Autonegotiation complete */
    438 #define  BMSR_LINK	0x0004	/* Link status */
    439 #define MII_ANAR	0x04	/* Autonegotiation advertisement (rw) */
    440 #define  ANAR_FC	0x0400	/* local device supports PAUSE */
    441 #define  ANAR_TX_FD	0x0100	/* local device supports 100bTx FD */
    442 #define  ANAR_TX	0x0080	/* local device supports 100bTx */
    443 #define  ANAR_10_FD	0x0040	/* local device supports 10bT FD */
    444 #define  ANAR_10	0x0020	/* local device supports 10bT */
    445 #define  ANAR_CSMA	0x0001	/* protocol selector CSMA/CD */
    446 #define MII_ANLPAR	0x05	/* Autonegotiation lnk partner abilities (rw) */
    447 #define MII_GTCR	0x09	/* 1000baseT control */
    448 #define  GANA_1000TFDX	0x0200	/* advertise 1000baseT FDX */
    449 #define  GANA_1000THDX	0x0100	/* advertise 1000baseT HDX */
    450 #define MII_GTSR	0x0a	/* 1000baseT status */
    451 #define  GLPA_1000TFDX	0x0800	/* link partner 1000baseT FDX capable */
    452 #define  GLPA_1000THDX	0x0400	/* link partner 1000baseT HDX capable */
    453 
    454 static void
    455 mii_initphy(struct local *l)
    456 {
    457 	unsigned val;
    458 
    459 	l->phy = 0;
    460 
    461 	/* take PHY out of reset */
    462 	val = CSR_READ_4(l, SK_GPIO);
    463 	CSR_WRITE_4(l, SK_GPIO, (val | 0x2000000) & ~0x200);
    464 
    465 	/* GMAC and GPHY reset */
    466 	CSR_WRITE_4(l, SK_GPHY_CTRL, GPHY_RESET_SET);
    467 	CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_SET);
    468 	DELAY(1000);
    469 	CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_CLEAR);
    470 	CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_SET);
    471 	DELAY(1000);
    472 
    473 	val = GPHY_INT_POL_HI | GPHY_DIS_FC | GPHY_DIS_SLEEP | GPHY_ENA_XC |
    474 	    GPHY_ANEG_ALL | GPHY_ENA_PAUSE | GPHY_COPPER;
    475 	CSR_WRITE_4(l, SK_GPHY_CTRL, val | GPHY_RESET_SET);
    476 	DELAY(1000);
    477 	CSR_WRITE_4(l, SK_GPHY_CTRL, val | GPHY_RESET_CLEAR);
    478 	CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_LOOP_OFF | GMAC_PAUSE_ON |
    479 	    GMAC_RESET_CLEAR);
    480 }
    481 
    482 static void
    483 mii_dealan(struct local *l, unsigned timo)
    484 {
    485 	unsigned bmsr, bound;
    486 
    487 	mii_write(l, l->phy, MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD |
    488 	    ANAR_10 | ANAR_CSMA | ANAR_FC);
    489 	mii_write(l, l->phy, MII_GTCR, GANA_1000TFDX | GANA_1000THDX);
    490 	mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    491 	l->anlpar = 0;
    492 	bound = getsecs() + timo;
    493 	do {
    494 		bmsr = mii_read(l, l->phy, MII_BMSR) |
    495 		   mii_read(l, l->phy, MII_BMSR); /* read twice */
    496 		if ((bmsr & BMSR_LINK) && (bmsr & BMSR_ACOMP)) {
    497 			l->pssr = mii_read(l, l->phy, MII_PSSR);
    498 			l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
    499 			if ((l->pssr & PSSR_RESOLVED) == 0)
    500 				continue;
    501 			break;
    502 		}
    503 		DELAY(10 * 1000);
    504 	} while (getsecs() < bound);
    505 }
    506