Home | History | Annotate | Line # | Download | only in altboot
rge.c revision 1.4
      1 /* $NetBSD: rge.c,v 1.4 2011/04/04 16:41:34 phx Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 
     34 #include <netinet/in.h>
     35 #include <netinet/in_systm.h>
     36 
     37 #include <lib/libsa/stand.h>
     38 #include <lib/libsa/net.h>
     39 
     40 #include "globals.h"
     41 
     42 /*
     43  * - reverse endian access every CSR.
     44  * - no vtophys() translation, vaddr_t == paddr_t.
     45  * - PIPT writeback cache aware.
     46  */
     47 #define CSR_WRITE_1(l, r, v)	*(volatile uint8_t *)((l)->csr+(r)) = (v)
     48 #define CSR_READ_1(l, r)	*(volatile uint8_t *)((l)->csr+(r))
     49 #define CSR_WRITE_2(l, r, v)	out16rb((l)->csr+(r), (v))
     50 #define CSR_READ_2(l, r)	in16rb((l)->csr+(r))
     51 #define CSR_WRITE_4(l, r, v)	out32rb((l)->csr+(r), (v))
     52 #define CSR_READ_4(l, r)	in32rb((l)->csr+(r))
     53 #define VTOPHYS(va)		(uint32_t)(va)
     54 #define DEVTOV(pa)		(uint32_t)(pa)
     55 #define wbinv(adr, siz)		_wbinv(VTOPHYS(adr), (uint32_t)(siz))
     56 #define inv(adr, siz)		_inv(VTOPHYS(adr), (uint32_t)(siz))
     57 #define DELAY(n)		delay(n)
     58 #define ALLOC(T,A)		(T *)allocaligned(sizeof(T),(A))
     59 
     60 struct desc {
     61 	uint32_t xd0, xd1, xd2, xd3;
     62 };
     63 #define T0_OWN		0x80000000	/* loaded for HW to send */
     64 #define T0_EOR		0x40000000	/* end of ring */
     65 #define T0_FS		0x20000000	/* first descriptor */
     66 #define T0_LS		0x10000000	/* last descriptor */
     67 #define T0_FRMASK	0x0000ffff
     68 
     69 #define R0_OWN		0x80000000	/* empty for HW to load anew */
     70 #define R0_EOR		0x40000000	/* end mark to form a ring */
     71 #define R0_BUFLEN	0x00003ff8	/* max frag. size to receive */
     72 #define R0_FS		0x20000000	/* start of frame */
     73 #define R0_LS		0x10000000	/* end of frame */
     74 #define R0_RES		0x00200000	/* Rx error summary */
     75 #define R0_RUNT		0x00100000	/* runt frame received */
     76 #define R0_CRC		0x00080000	/* CRC error found */
     77 #define R0_FRMASK	0x00003fff	/* 13:0 frame length */
     78 
     79 #define RGE_IDR0	0x00		/* MAC address [0] */
     80 #define RGE_IDR1	0x01		/* MAC address [1] */
     81 #define RGE_IDR2	0x02		/* MAC address [2] */
     82 #define RGE_IDR3	0x03		/* MAC address [3] */
     83 #define RGE_IDR4	0x04		/* MAC address [4] */
     84 #define RGE_IDR5	0x05		/* MAC address [5] */
     85 #define RGE_TNPDS	0x20		/* Tx descriptor base paddr */
     86 #define RGE_THPDS	0x28		/* high pro. Tx des. base paddr */
     87 #define RGE_CR		0x37		/* command */
     88 #define	 CR_RESET	(1U << 4)	/* reset S1C */
     89 #define	 CR_RXEN	(1U << 3)	/* Rx enable */
     90 #define	 CR_TXEN	(1U << 2)	/* Tx enable */
     91 #define RGE_TPPOLL	0x38		/* activate desc polling */
     92 #define RGE_IMR		0x3c		/* interrupt mask */
     93 #define RGE_ISR		0x3e		/* interrupt status */
     94 #define RGE_TCR		0x40		/* Tx control */
     95 #define	 TCR_MAXDMA	0x0700		/* 10:8 Tx DMA burst size */
     96 #define RGE_RCR		0x44		/* Rx control */
     97 #define	 RCR_RXTFH	0xe000		/* 15:13 Rx FIFO threshold */
     98 #define	 RCR_MAXDMA	0x0700		/* 10:8 Rx DMA burst size */
     99 #define	 RCR_AE		(1U << 5)	/* accept error frame */
    100 #define	 RCR_RE		(1U << 4)	/* accept runt frame */
    101 #define	 RCR_AB		(1U << 3)	/* accept broadcast frame */
    102 #define	 RCR_AM		(1U << 2)	/* accept multicast frame */
    103 #define	 RCR_APM	(1U << 1)	/* accept unicast frame */
    104 #define	 RCR_AAP	(1U << 0)	/* promiscuous */
    105 #define RGE_PHYAR	0x60		/* PHY access */
    106 #define RGE_PHYSR	0x6c		/* PHY status */
    107 #define RGE_RMS		0xda		/* Rx maximum frame size */
    108 #define RGE_RDSAR	0xe4		/* Rx descriptor base paddr */
    109 #define RGE_ETTHR	0xec		/* Tx threshold */
    110 
    111 #define FRAMESIZE	1536
    112 
    113 struct local {
    114 	struct desc txd[2]; /* 256B align */
    115 	  uint8_t _hole0[256 - 2 * sizeof(struct desc)];
    116 	struct desc rxd[2]; /* 256B align */
    117 	  uint8_t _hole1[256 - 2 * sizeof(struct desc)];
    118 	uint8_t rxstore[2][FRAMESIZE];
    119 	unsigned csr, tx, rx;
    120 	unsigned phy, bmsr, anlpar;
    121 	unsigned tcr, rcr;
    122 };
    123 
    124 static int mii_read(struct local *, int, int);
    125 static void mii_write(struct local *, int, int, int);
    126 static void mii_initphy(struct local *);
    127 static void mii_dealan(struct local *, unsigned);
    128 
    129 int
    130 rge_match(unsigned tag, void *data)
    131 {
    132 	unsigned v;
    133 
    134 	v = pcicfgread(tag, PCI_ID_REG);
    135 	switch (v) {
    136 	case PCI_DEVICE(0x10ec, 0x8167):
    137 	case PCI_DEVICE(0x10ec, 0x8169):
    138 		return 1;
    139 	}
    140 	return 0;
    141 }
    142 
    143 void *
    144 rge_init(unsigned tag, void *data)
    145 {
    146 	unsigned val;
    147 	struct local *l;
    148 	struct desc *txd, *rxd;
    149 	uint8_t *en = data;
    150 
    151 	l = ALLOC(struct local, 256);	/* desc alignment */
    152 	memset(l, 0, sizeof(struct local));
    153 	l->csr = DEVTOV(pcicfgread(tag, 0x14)); /* use mem space */
    154 
    155 	CSR_WRITE_1(l, RGE_CR, CR_RESET);
    156 	do {
    157 		val = CSR_READ_1(l, RGE_CR);
    158 	} while (val & CR_RESET);
    159 
    160 	mii_initphy(l);
    161 
    162 	en = data;
    163 	en[0] = CSR_READ_1(l, RGE_IDR0);
    164 	en[1] = CSR_READ_1(l, RGE_IDR1);
    165 	en[2] = CSR_READ_1(l, RGE_IDR2);
    166 	en[3] = CSR_READ_1(l, RGE_IDR3);
    167 	en[4] = CSR_READ_1(l, RGE_IDR4);
    168 	en[5] = CSR_READ_1(l, RGE_IDR5);
    169 
    170 	printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
    171 	    en[0], en[1], en[2], en[3], en[4], en[5]);
    172 	DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
    173 	    mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
    174 
    175 	mii_dealan(l, 5);
    176 
    177 	/* speed and duplexity can be seen in PHYSR */
    178 	val = CSR_READ_1(l, RGE_PHYSR);
    179 	if (val & (1U << 4))
    180 		printf("1000Mbps");
    181 	if (val & (1U << 3))
    182 		printf("100Mbps");
    183 	if (val & (1U << 2))
    184 		printf("10Mbps");
    185 	if (val & (1U << 0))
    186 		printf("-FDX");
    187 	printf("\n");
    188 
    189 	txd = &l->txd[0];
    190 	txd[1].xd0 = htole32(T0_EOR);
    191 	rxd = &l->rxd[0];
    192 	rxd[0].xd0 = htole32(R0_OWN | FRAMESIZE);
    193 	rxd[0].xd2 = htole32(VTOPHYS(l->rxstore[0]));
    194 	rxd[1].xd0 = htole32(R0_OWN | R0_EOR | FRAMESIZE);
    195 	rxd[1].xd2 = htole32(VTOPHYS(l->rxstore[1]));
    196 	wbinv(l, sizeof(struct local));
    197 	l->tx = l->rx = 0;
    198 
    199 	l->tcr = (03 << 24) | (07 << 8);
    200 	l->rcr = (07 << 13) | (07 << 8) | RCR_APM;
    201 	CSR_WRITE_1(l, RGE_CR, CR_TXEN | CR_RXEN);
    202 	CSR_WRITE_1(l, RGE_ETTHR, 0x3f);
    203 	CSR_WRITE_2(l, RGE_RMS, FRAMESIZE);
    204 	CSR_WRITE_4(l, RGE_TCR, l->tcr);
    205 	CSR_WRITE_4(l, RGE_RCR, l->rcr);
    206 	CSR_WRITE_4(l, RGE_TNPDS, VTOPHYS(txd));
    207 	CSR_WRITE_4(l, RGE_RDSAR, VTOPHYS(rxd));
    208 	CSR_WRITE_4(l, RGE_TNPDS + 4, 0);
    209 	CSR_WRITE_4(l, RGE_RDSAR + 4, 0);
    210 	CSR_WRITE_2(l, RGE_ISR, ~0);
    211 	CSR_WRITE_2(l, RGE_IMR, 0);
    212 	return l;
    213 }
    214 
    215 int
    216 rge_send(void *dev, char *buf, unsigned len)
    217 {
    218 	struct local *l = dev;
    219 	volatile struct desc *txd;
    220 	unsigned loop;
    221 
    222 	if (len < 60) {
    223 		memset(buf + len, 0, 60 - len);
    224 		len = 60; /* RTL does not stretch <60 Tx frame */
    225 	}
    226 	wbinv(buf, len);
    227 	txd = &l->txd[l->tx];
    228 	txd->xd2 = htole32(VTOPHYS(buf));
    229 	txd->xd0 &= htole32(T0_EOR);
    230 	txd->xd0 |= htole32(T0_OWN | T0_FS | T0_LS | (len & T0_FRMASK));
    231 	wbinv(txd, sizeof(struct desc));
    232 	CSR_WRITE_1(l, RGE_TPPOLL, 0x40);
    233 	loop = 100;
    234 	do {
    235 		if ((le32toh(txd->xd0) & T0_OWN) == 0)
    236 			goto done;
    237 		DELAY(10);
    238 		inv(txd, sizeof(struct desc));
    239 	} while (--loop > 0);
    240 	printf("xmit failed\n");
    241 	return -1;
    242   done:
    243 	l->tx ^= 1;
    244 	return len;
    245 }
    246 
    247 int
    248 rge_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
    249 {
    250 	struct local *l = dev;
    251 	volatile struct desc *rxd;
    252 	unsigned bound, rxstat, len;
    253 	uint8_t *ptr;
    254 
    255 	bound = 1000 * timo;
    256 #if 0
    257 printf("recving with %u sec. timeout\n", timo);
    258 #endif
    259   again:
    260 	rxd = &l->rxd[l->rx];
    261 	do {
    262 		inv(rxd, sizeof(struct desc));
    263 		rxstat = le32toh(rxd->xd0);
    264 		if ((rxstat & R0_OWN) == 0)
    265 			goto gotone;
    266 		DELAY(1000);	/* 1 milli second */
    267 	} while (--bound > 0);
    268 	errno = 0;
    269 	return -1;
    270   gotone:
    271 	if (rxstat & R0_RES) {
    272 		rxd->xd0 &= htole32(R0_EOR);
    273 		rxd->xd0 |= htole32(R0_OWN | FRAMESIZE);
    274 		wbinv(rxd, sizeof(struct desc));
    275 		l->rx ^= 1;
    276 		goto again;
    277 	}
    278 	len = rxstat & R0_FRMASK;
    279 	if (len > maxlen)
    280 		len = maxlen;
    281 	ptr = l->rxstore[l->rx];
    282 	inv(ptr, len);
    283 	memcpy(buf, ptr, len);
    284 	rxd->xd0 &= htole32(R0_EOR);
    285 	rxd->xd0 |= htole32(R0_OWN | FRAMESIZE);
    286 	wbinv(rxd, sizeof(struct desc));
    287 	l->rx ^= 1;
    288 	return len;
    289 }
    290 
    291 static int
    292 mii_read(struct local *l, int phy, int reg)
    293 {
    294 	unsigned v;
    295 
    296 	v = reg << 16;
    297 	CSR_WRITE_4(l, RGE_PHYAR, v);
    298 	DELAY(1000);
    299 	do {
    300 		DELAY(100);
    301 		v = CSR_READ_4(l, RGE_PHYAR);
    302 	} while ((v & (1U << 31)) == 0); /* wait for 0 -> 1 */
    303 	return v & 0xffff;
    304 }
    305 
    306 static void
    307 mii_write(struct local *l, int phy, int reg, int data)
    308 {
    309 	unsigned v;
    310 
    311 	v = (reg << 16) | (data & 0xffff) | (1U << 31);
    312 	CSR_WRITE_4(l, RGE_PHYAR, v);
    313 	DELAY(1000);
    314 	do {
    315 		DELAY(100);
    316 		v = CSR_READ_4(l, RGE_PHYAR);
    317 	} while (v & (1U << 31)); /* wait for 1 -> 0 */
    318 }
    319 
    320 #define MII_BMCR	0x00	/* Basic mode control register (rw) */
    321 #define  BMCR_RESET	0x8000	/* reset */
    322 #define  BMCR_AUTOEN	0x1000	/* autonegotiation enable */
    323 #define  BMCR_ISO	0x0400	/* isolate */
    324 #define  BMCR_STARTNEG	0x0200	/* restart autonegotiation */
    325 #define MII_BMSR	0x01	/* Basic mode status register (ro) */
    326 #define  BMSR_ACOMP	0x0020	/* Autonegotiation complete */
    327 #define  BMSR_LINK	0x0004	/* Link status */
    328 #define MII_ANAR	0x04	/* Autonegotiation advertisement (rw) */
    329 #define  ANAR_FC	0x0400	/* local device supports PAUSE */
    330 #define  ANAR_TX_FD	0x0100	/* local device supports 100bTx FD */
    331 #define  ANAR_TX	0x0080	/* local device supports 100bTx */
    332 #define  ANAR_10_FD	0x0040	/* local device supports 10bT FD */
    333 #define  ANAR_10	0x0020	/* local device supports 10bT */
    334 #define  ANAR_CSMA	0x0001	/* protocol selector CSMA/CD */
    335 #define MII_ANLPAR	0x05	/* Autonegotiation lnk partner abilities (rw) */
    336 #define MII_GTCR	0x09	/* 1000baseT control */
    337 #define  GANA_1000TFDX	0x0200	/* advertise 1000baseT FDX */
    338 #define  GANA_1000THDX	0x0100	/* advertise 1000baseT HDX */
    339 #define MII_GTSR	0x0a	/* 1000baseT status */
    340 #define  GLPA_1000TFDX	0x0800	/* link partner 1000baseT FDX capable */
    341 #define  GLPA_1000THDX	0x0400	/* link partner 1000baseT HDX capable */
    342 #define  GLPA_ASM_DIR	0x0200	/* link partner asym. pause dir. capable */
    343 
    344 static void
    345 mii_initphy(struct local *l)
    346 {
    347 	int bound, ctl, phy, sts;
    348 
    349 	phy = 7;	/* internal rgephy, always at 7 */
    350 	ctl = mii_read(l, phy, MII_BMCR);
    351 	mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
    352 	bound = 100;
    353 	do {
    354 		DELAY(10);
    355 		ctl = mii_read(l, phy, MII_BMCR);
    356 		if (ctl == 0xffff) {
    357 			printf("MII: PHY %d has died after reset\n", phy);
    358 			return;
    359 		}
    360 	} while (bound-- > 0 && (ctl & BMCR_RESET));
    361 	if (bound == 0) {
    362 		printf("PHY %d reset failed\n", phy);
    363 	}
    364 	ctl &= ~BMCR_ISO;
    365 	mii_write(l, phy, MII_BMCR, ctl);
    366 	sts = mii_read(l, phy, MII_BMSR) |
    367 	    mii_read(l, phy, MII_BMSR); /* read twice */
    368 	l->phy = phy;
    369 	l->bmsr = sts;
    370 }
    371 
    372 void
    373 mii_dealan(struct local *l, unsigned timo)
    374 {
    375 	unsigned anar, gtcr, bound;
    376 
    377 	anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
    378 	anar |= ANAR_FC;
    379 	gtcr = GANA_1000TFDX | GANA_1000THDX;
    380 	mii_write(l, l->phy, MII_ANAR, anar);
    381 	mii_write(l, l->phy, MII_GTCR, gtcr);
    382 	mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    383 	l->anlpar = 0;
    384 	bound = getsecs() + timo;
    385 	do {
    386 		l->bmsr = mii_read(l, l->phy, MII_BMSR) |
    387 		   mii_read(l, l->phy, MII_BMSR); /* read twice */
    388 		if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
    389 			l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
    390 			break;
    391 		}
    392 		DELAY(10 * 1000);
    393 	} while (getsecs() < bound);
    394 	return;
    395 }
    396