Home | History | Annotate | Line # | Download | only in boot
tlp.c revision 1.3.2.2
      1  1.3.2.2  joerg /*	$NetBSD: tlp.c,v 1.3.2.2 2007/10/31 23:13:58 joerg Exp $	*/
      2  1.3.2.2  joerg 
      3  1.3.2.2  joerg /*-
      4  1.3.2.2  joerg  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.3.2.2  joerg  * All rights reserved.
      6  1.3.2.2  joerg  *
      7  1.3.2.2  joerg  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.2.2  joerg  * by Tohru Nishimura.
      9  1.3.2.2  joerg  *
     10  1.3.2.2  joerg  * Redistribution and use in source and binary forms, with or without
     11  1.3.2.2  joerg  * modification, are permitted provided that the following conditions
     12  1.3.2.2  joerg  * are met:
     13  1.3.2.2  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.3.2.2  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.3.2.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.2.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.2.2  joerg  *    documentation and/or other materials provided with the distribution.
     18  1.3.2.2  joerg  * 3. All advertising materials mentioning features or use of this software
     19  1.3.2.2  joerg  *    must display the following acknowledgement:
     20  1.3.2.2  joerg  *        This product includes software developed by the NetBSD
     21  1.3.2.2  joerg  *        Foundation, Inc. and its contributors.
     22  1.3.2.2  joerg  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.3.2.2  joerg  *    contributors may be used to endorse or promote products derived
     24  1.3.2.2  joerg  *    from this software without specific prior written permission.
     25  1.3.2.2  joerg  *
     26  1.3.2.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.3.2.2  joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.3.2.2  joerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.3.2.2  joerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.3.2.2  joerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.3.2.2  joerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.3.2.2  joerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.3.2.2  joerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.3.2.2  joerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.3.2.2  joerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.3.2.2  joerg  * POSSIBILITY OF SUCH DAMAGE.
     37  1.3.2.2  joerg  */
     38  1.3.2.2  joerg 
     39  1.3.2.2  joerg #include <sys/param.h>
     40  1.3.2.2  joerg #include <sys/socket.h>
     41  1.3.2.2  joerg 
     42  1.3.2.2  joerg #include <netinet/in.h>
     43  1.3.2.2  joerg #include <netinet/in_systm.h>
     44  1.3.2.2  joerg 
     45  1.3.2.2  joerg #include <lib/libsa/stand.h>
     46  1.3.2.2  joerg #include <lib/libsa/net.h>
     47  1.3.2.2  joerg 
     48  1.3.2.2  joerg #include <mips/cpuregs.h>
     49  1.3.2.2  joerg 
     50  1.3.2.2  joerg #include "boot.h"
     51  1.3.2.2  joerg 
     52  1.3.2.2  joerg /*
     53  1.3.2.2  joerg  * - little endian access for CSR register.
     54  1.3.2.2  joerg  * - assume KSEG0 on vtophys() translation.
     55  1.3.2.2  joerg  * - PIPT writeback cache aware.
     56  1.3.2.2  joerg  */
     57  1.3.2.2  joerg #define CSR_WRITE(l, r, v)					 	\
     58  1.3.2.2  joerg do {									\
     59  1.3.2.2  joerg 	*(volatile uint32_t *)((l)->csr + (r)) = (v);			\
     60  1.3.2.2  joerg } while (0)
     61  1.3.2.2  joerg #define CSR_READ(l, r)		(*(volatile uint32_t *)((l)->csr + (r)))
     62  1.3.2.2  joerg #define VTOPHYS(va) 		MIPS_KSEG0_TO_PHYS(va)
     63  1.3.2.2  joerg #define wb(adr, siz)		pdcache_wb((uint32_t)(adr), (u_int)(siz))
     64  1.3.2.2  joerg #define wbinv(adr, siz)		pdcache_wbinv((uint32_t)(adr), (u_int)(siz))
     65  1.3.2.2  joerg #define inv(adr, siz)		pdcache_inv((uint32_t)(adr), (u_int)(siz))
     66  1.3.2.2  joerg #define DELAY(n)		delay(n)
     67  1.3.2.2  joerg #define ALLOC(T, A)	(T *)((uint32_t)alloc(sizeof(T) + (A)) & ~((A) - 1))
     68  1.3.2.2  joerg 
     69  1.3.2.2  joerg #define T0_OWN		(1U<<31)	/* desc is ready to tx */
     70  1.3.2.2  joerg #define T0_ES		(1U<<15)	/* Tx error summary */
     71  1.3.2.2  joerg #define T1_LS		(1U<<30)	/* last segment */
     72  1.3.2.2  joerg #define T1_FS		(1U<<29)	/* first segment */
     73  1.3.2.2  joerg #define T1_SET		(1U<<27)	/* "setup packet" */
     74  1.3.2.2  joerg #define T1_TER		(1U<<25)	/* end of ring mark */
     75  1.3.2.2  joerg #define T1_TBS_MASK	0x7ff		/* segment size 10:0 */
     76  1.3.2.2  joerg #define R0_OWN		(1U<<31)	/* desc is empty */
     77  1.3.2.2  joerg #define R0_FS		(1U<<30)	/* first desc of frame */
     78  1.3.2.2  joerg #define R0_LS		(1U<<8)		/* last desc of frame */
     79  1.3.2.2  joerg #define R0_ES		(1U<<15)	/* Rx error summary */
     80  1.3.2.2  joerg #define R1_RCH		(1U<<24)	/* Second address chained */
     81  1.3.2.2  joerg #define R1_RER		(1U<<25)	/* end of ring mark */
     82  1.3.2.2  joerg #define R0_FL_MASK	0x3fff0000	/* frame length 29:16 */
     83  1.3.2.2  joerg #define R1_RBS_MASK	0x7ff		/* segment size 10:0 */
     84  1.3.2.2  joerg 
     85  1.3.2.2  joerg #define DESCSIZE	16
     86  1.3.2.2  joerg struct desc {
     87  1.3.2.2  joerg 	volatile uint32_t xd0, xd1, xd2, xd3;
     88  1.3.2.2  joerg #if CACHELINESIZE > DESCSIZE
     89  1.3.2.2  joerg 	uint8_t pad[CACHELINESIZE - DESCSIZE];
     90  1.3.2.2  joerg #endif
     91  1.3.2.2  joerg };
     92  1.3.2.2  joerg 
     93  1.3.2.2  joerg #define TLP_BMR		0x000		/* 0: bus mode */
     94  1.3.2.2  joerg #define  BMR_RST	(1U<< 0)	/* software reset */
     95  1.3.2.2  joerg #define TLP_TPD		0x008		/* 1: instruct Tx to start */
     96  1.3.2.2  joerg #define  TPD_POLL	(1U<< 0)	/* transmit poll demand */
     97  1.3.2.2  joerg #define TLP_RPD		0x010		/* 2: instruct Rx to start */
     98  1.3.2.2  joerg #define  RPD_POLL	(1U<< 0)	/* receive poll demand */
     99  1.3.2.2  joerg #define TLP_RRBA	0x018		/* 3: Rx descriptor base */
    100  1.3.2.2  joerg #define TLP_TRBA	0x020		/* 4: Tx descriptor base */
    101  1.3.2.2  joerg #define TLP_STS		0x028		/* 5: status */
    102  1.3.2.2  joerg #define  STS_TS		0x00700000	/* Tx status */
    103  1.3.2.2  joerg #define  STS_RS		0x000e0000	/* Rx status */
    104  1.3.2.2  joerg #define TLP_OMR		0x030		/* 6: operation mode */
    105  1.3.2.2  joerg #define  OMR_SDP	(1U<<25)	/* always ON */
    106  1.3.2.2  joerg #define  OMR_PS		(1U<<18)	/* port select */
    107  1.3.2.2  joerg #define  OMR_PM		(1U<< 6)	/* promicuous */
    108  1.3.2.2  joerg #define  OMR_TEN	(1U<<13)	/* instruct start/stop Tx */
    109  1.3.2.2  joerg #define  OMR_REN	(1U<< 1)	/* instruct start/stop Rx */
    110  1.3.2.2  joerg #define  OMR_FD		(1U<< 9)	/* FDX */
    111  1.3.2.2  joerg #define TLP_IEN		0x38		/* 7: interrupt enable mask */
    112  1.3.2.2  joerg #define TLP_APROM	0x048		/* 9: SEEPROM and MII management */
    113  1.3.2.2  joerg #define  SROM_RD	(1U <<14)	/* read operation */
    114  1.3.2.2  joerg #define  SROM_WR	(1U <<13)	/* write openration */
    115  1.3.2.2  joerg #define  SROM_SR	(1U <<11)	/* SEEPROM select */
    116  1.3.2.2  joerg #define TLP_CSR12	0x60		/* SIA status */
    117  1.3.2.2  joerg 
    118  1.3.2.2  joerg #define TLP_CSR15	0x78		/* SIA general register */
    119  1.3.2.2  joerg #define  SIAGEN_MD0	(1U<<16)
    120  1.3.2.2  joerg #define  SIAGEN_CWE	(1U<<28)
    121  1.3.2.2  joerg 
    122  1.3.2.2  joerg #define FRAMESIZE	1536
    123  1.3.2.2  joerg #define BUFSIZE		2048
    124  1.3.2.2  joerg #define NRXBUF		2
    125  1.3.2.2  joerg #define NEXT_RXBUF(x)	(((x) + 1) & (NRXBUF - 1))
    126  1.3.2.2  joerg 
    127  1.3.2.2  joerg struct local {
    128  1.3.2.2  joerg 	struct desc TxD;
    129  1.3.2.2  joerg 	struct desc RxD[NRXBUF];
    130  1.3.2.2  joerg 	uint8_t txstore[BUFSIZE];
    131  1.3.2.2  joerg 	uint8_t rxstore[NRXBUF][BUFSIZE];
    132  1.3.2.2  joerg 	uint32_t csr, omr;
    133  1.3.2.2  joerg 	u_int rx;
    134  1.3.2.2  joerg 	u_int sromsft;
    135  1.3.2.2  joerg 	u_int phy;
    136  1.3.2.2  joerg 	uint32_t bmsr, anlpar;
    137  1.3.2.2  joerg };
    138  1.3.2.2  joerg 
    139  1.3.2.2  joerg #define COBALT_TLP0_BASE	0x10100000
    140  1.3.2.2  joerg #define SROM_MAC_OFFSET		0
    141  1.3.2.2  joerg 
    142  1.3.2.2  joerg static void size_srom(struct local *);
    143  1.3.2.2  joerg static u_int read_srom(struct local *, int);
    144  1.3.2.2  joerg #if 0
    145  1.3.2.2  joerg static u_int tlp_mii_read(struct local *, int, int);
    146  1.3.2.2  joerg static void tlp_mii_write(struct local *, int, int, int);
    147  1.3.2.2  joerg static void mii_initphy(struct local *);
    148  1.3.2.2  joerg #endif
    149  1.3.2.2  joerg 
    150  1.3.2.2  joerg void *
    151  1.3.2.2  joerg tlp_init(void *cookie)
    152  1.3.2.2  joerg {
    153  1.3.2.2  joerg 	uint32_t val;
    154  1.3.2.2  joerg 	struct local *l;
    155  1.3.2.2  joerg 	struct desc *TxD, *RxD;
    156  1.3.2.2  joerg 	uint8_t *en;
    157  1.3.2.2  joerg 	int i;
    158  1.3.2.2  joerg 
    159  1.3.2.2  joerg 	l = ALLOC(struct local, CACHELINESIZE);
    160  1.3.2.2  joerg 	memset(l, 0, sizeof(struct local));
    161  1.3.2.2  joerg 
    162  1.3.2.2  joerg 	DPRINTF(("tlp: l = %p, TxD = %p, RxD[0] = %p, RxD[1] = %p\n",
    163  1.3.2.2  joerg 	    l, &l->TxD, &l->RxD[0], &l->RxD[1]));
    164  1.3.2.2  joerg 	DPRINTF(("tlp: txstore = %p, rxstore[0] = %p, rxstore[1] = %p\n",
    165  1.3.2.2  joerg 	    l->txstore, l->rxstore[0], l->rxstore[1]));
    166  1.3.2.2  joerg 
    167  1.3.2.2  joerg #if 0
    168  1.3.2.2  joerg 	/* XXX assume tlp0 at pci0 dev 7 function 0 */
    169  1.3.2.2  joerg 	tag = (0 << 16) | ( 7 << 11) | (0 << 8);
    170  1.3.2.2  joerg 	/* memory map is not initialized by the firmware on cobalt */
    171  1.3.2.2  joerg 	l->csr = MIPS_PHYS_TO_KSEG1(pcicfgread(tag, 0x10) & 0xfffffffc);
    172  1.3.2.2  joerg 	DPRINTF(("%s: CSR = 0x%x\n", __func__, l->csr));
    173  1.3.2.2  joerg #else
    174  1.3.2.2  joerg 	l->csr = MIPS_PHYS_TO_KSEG1(COBALT_TLP0_BASE);
    175  1.3.2.2  joerg #endif
    176  1.3.2.2  joerg 
    177  1.3.2.2  joerg 	val = CSR_READ(l, TLP_BMR);
    178  1.3.2.2  joerg 	CSR_WRITE(l, TLP_BMR, val | BMR_RST);
    179  1.3.2.2  joerg 	DELAY(1000);
    180  1.3.2.2  joerg 	CSR_WRITE(l, TLP_BMR, val);
    181  1.3.2.2  joerg 	DELAY(1000);
    182  1.3.2.2  joerg 	(void)CSR_READ(l, TLP_BMR);
    183  1.3.2.2  joerg 
    184  1.3.2.2  joerg 	l->omr = OMR_PS | OMR_SDP;
    185  1.3.2.2  joerg 	CSR_WRITE(l, TLP_OMR, l->omr);
    186  1.3.2.2  joerg 	CSR_WRITE(l, TLP_STS, ~0);
    187  1.3.2.2  joerg 	CSR_WRITE(l, TLP_IEN, 0);
    188  1.3.2.2  joerg 
    189  1.3.2.2  joerg #if 0
    190  1.3.2.2  joerg 	mii_initphy(l);
    191  1.3.2.2  joerg #endif
    192  1.3.2.2  joerg 	size_srom(l);
    193  1.3.2.2  joerg 
    194  1.3.2.2  joerg 	en = cookie;
    195  1.3.2.2  joerg 	/* MAC address is stored at offset 0 in SROM on cobalt */
    196  1.3.2.2  joerg 	val = read_srom(l, SROM_MAC_OFFSET / 2 + 0);
    197  1.3.2.2  joerg 	en[0] = val;
    198  1.3.2.2  joerg 	en[1] = val >> 8;
    199  1.3.2.2  joerg 	val = read_srom(l, SROM_MAC_OFFSET / 2 + 1);
    200  1.3.2.2  joerg 	en[2] = val;
    201  1.3.2.2  joerg 	en[3] = val >> 8;
    202  1.3.2.2  joerg 	val = read_srom(l, SROM_MAC_OFFSET / 2 + 2);
    203  1.3.2.2  joerg 	en[4] = val;
    204  1.3.2.2  joerg 	en[5] = val >> 8;
    205  1.3.2.2  joerg 
    206  1.3.2.2  joerg 	DPRINTF(("tlp: MAC address %x:%x:%x:%x:%x:%x\n",
    207  1.3.2.2  joerg 	    en[0], en[1], en[2], en[3], en[4], en[5]));
    208  1.3.2.2  joerg 
    209  1.3.2.2  joerg 	RxD = &l->RxD[0];
    210  1.3.2.2  joerg 	for (i = 0; i < NRXBUF; i++) {
    211  1.3.2.2  joerg 		RxD[i].xd3 = htole32(VTOPHYS(&RxD[NEXT_RXBUF(i)]));
    212  1.3.2.2  joerg 		RxD[i].xd2 = htole32(VTOPHYS(l->rxstore[i]));
    213  1.3.2.2  joerg 		RxD[i].xd1 = htole32(R1_RCH|FRAMESIZE);
    214  1.3.2.2  joerg 		RxD[i].xd0 = htole32(R0_OWN);
    215  1.3.2.2  joerg 	}
    216  1.3.2.2  joerg 	CSR_WRITE(l, TLP_RRBA, VTOPHYS(RxD));
    217  1.3.2.2  joerg 
    218  1.3.2.2  joerg 	/* "setup packet" to have own station address */
    219  1.3.2.2  joerg 	TxD = &l->TxD;
    220  1.3.2.2  joerg 	TxD->xd3 = htole32(VTOPHYS(TxD));
    221  1.3.2.2  joerg 	TxD->xd2 = htole32(VTOPHYS(l->txstore));
    222  1.3.2.2  joerg 	TxD->xd1 = htole32(T1_SET | T1_TER);
    223  1.3.2.2  joerg 	TxD->xd0 = htole32(0);
    224  1.3.2.2  joerg 	CSR_WRITE(l, TLP_TRBA, VTOPHYS(TxD));
    225  1.3.2.2  joerg 
    226  1.3.2.2  joerg 	memset(l->txstore, 0, FRAMESIZE);
    227  1.3.2.2  joerg 
    228  1.3.2.2  joerg 	/* make sure the entire descriptors transfered to memory */
    229  1.3.2.2  joerg 	wbinv(l, sizeof(struct local));
    230  1.3.2.2  joerg 
    231  1.3.2.2  joerg 	l->rx = 0;
    232  1.3.2.2  joerg 	l->omr |= OMR_FD | OMR_TEN | OMR_REN;
    233  1.3.2.2  joerg 
    234  1.3.2.2  joerg #if 1
    235  1.3.2.2  joerg 	/* reset PHY (cobalt quirk from if_tlp_pci.c) */
    236  1.3.2.2  joerg 	CSR_WRITE(l, TLP_CSR15, SIAGEN_CWE | SIAGEN_MD0);
    237  1.3.2.2  joerg 	DELAY(10);
    238  1.3.2.2  joerg 	CSR_WRITE(l, TLP_CSR15, SIAGEN_CWE);
    239  1.3.2.2  joerg 	DELAY(10);
    240  1.3.2.2  joerg #endif
    241  1.3.2.2  joerg 
    242  1.3.2.2  joerg 	/* start Tx/Rx */
    243  1.3.2.2  joerg 	CSR_WRITE(l, TLP_OMR, l->omr);
    244  1.3.2.2  joerg #if 0
    245  1.3.2.2  joerg 	CSR_WRITE(l, TLP_TPD, TPD_POLL);
    246  1.3.2.2  joerg #endif
    247  1.3.2.2  joerg 	CSR_WRITE(l, TLP_RPD, RPD_POLL);
    248  1.3.2.2  joerg 
    249  1.3.2.2  joerg 	return l;
    250  1.3.2.2  joerg }
    251  1.3.2.2  joerg 
    252  1.3.2.2  joerg int
    253  1.3.2.2  joerg tlp_send(void *dev, char *buf, u_int len)
    254  1.3.2.2  joerg {
    255  1.3.2.2  joerg 	struct local *l = dev;
    256  1.3.2.2  joerg 	struct desc *TxD;
    257  1.3.2.2  joerg 	u_int loop;
    258  1.3.2.2  joerg 
    259  1.3.2.2  joerg #if 1
    260  1.3.2.2  joerg 	wb(buf, len);
    261  1.3.2.2  joerg 	TxD = &l->TxD;
    262  1.3.2.2  joerg 	TxD->xd3 = htole32(VTOPHYS(TxD));
    263  1.3.2.2  joerg 	TxD->xd2 = htole32(VTOPHYS(buf));
    264  1.3.2.2  joerg 	TxD->xd1 = htole32(T1_FS | T1_LS | T1_TER | (len & T1_TBS_MASK));
    265  1.3.2.2  joerg #else
    266  1.3.2.2  joerg 	memcpy(l->txstore, buf, len);
    267  1.3.2.2  joerg 	wb(l->txstore, len);
    268  1.3.2.2  joerg 	TxD = &l->TxD;
    269  1.3.2.2  joerg 	TxD->xd3 = htole32(VTOPHYS(TxD));
    270  1.3.2.2  joerg 	TxD->xd2 = htole32(VTOPHYS(l->txstore));
    271  1.3.2.2  joerg 	TxD->xd1 = htole32(T1_FS | T1_LS | T1_TER | (len & T1_TBS_MASK));
    272  1.3.2.2  joerg #endif
    273  1.3.2.2  joerg 	TxD->xd0 = htole32(T0_OWN);
    274  1.3.2.2  joerg 	wbinv(TxD, sizeof(struct desc));
    275  1.3.2.2  joerg 	CSR_WRITE(l, TLP_TPD, TPD_POLL);
    276  1.3.2.2  joerg 	loop = 100;
    277  1.3.2.2  joerg 	do {
    278  1.3.2.2  joerg 		if ((le32toh(TxD->xd0) & T0_OWN) == 0)
    279  1.3.2.2  joerg 			goto done;
    280  1.3.2.2  joerg 		inv(TxD, sizeof(struct desc));
    281  1.3.2.2  joerg 		DELAY(10);
    282  1.3.2.2  joerg 	} while (--loop > 0);
    283  1.3.2.2  joerg 	printf("xmit failed\n");
    284  1.3.2.2  joerg 	return -1;
    285  1.3.2.2  joerg   done:
    286  1.3.2.2  joerg 	return len;
    287  1.3.2.2  joerg }
    288  1.3.2.2  joerg 
    289  1.3.2.2  joerg int
    290  1.3.2.2  joerg tlp_recv(void *dev, char *buf, u_int maxlen, u_int timo)
    291  1.3.2.2  joerg {
    292  1.3.2.2  joerg 	struct local *l = dev;
    293  1.3.2.2  joerg 	struct desc *RxD;
    294  1.3.2.2  joerg 	u_int bound, len;
    295  1.3.2.2  joerg 	uint32_t rxstat;
    296  1.3.2.2  joerg 	uint8_t *ptr;
    297  1.3.2.2  joerg 
    298  1.3.2.2  joerg 	bound = 1000 * timo;
    299  1.3.2.2  joerg 
    300  1.3.2.2  joerg   again:
    301  1.3.2.2  joerg 	RxD = &l->RxD[l->rx];
    302  1.3.2.2  joerg 	do {
    303  1.3.2.2  joerg 		rxstat = le32toh(RxD->xd0);
    304  1.3.2.2  joerg 		inv(RxD, sizeof(struct desc));
    305  1.3.2.2  joerg 		if ((rxstat & R0_OWN) == 0)
    306  1.3.2.2  joerg 			goto gotone;
    307  1.3.2.2  joerg 		DELAY(1000); /* 1 milli second */
    308  1.3.2.2  joerg 	} while (--bound > 0);
    309  1.3.2.2  joerg 	errno = 0;
    310  1.3.2.2  joerg 	CSR_WRITE(l, TLP_RPD, RPD_POLL);
    311  1.3.2.2  joerg 	return -1;
    312  1.3.2.2  joerg   gotone:
    313  1.3.2.2  joerg 	if (rxstat & R0_ES) {
    314  1.3.2.2  joerg 		RxD->xd0 = htole32(R0_OWN);
    315  1.3.2.2  joerg 		wbinv(RxD, sizeof(struct desc));
    316  1.3.2.2  joerg 		l->rx = NEXT_RXBUF(l->rx);
    317  1.3.2.2  joerg 		CSR_WRITE(l, TLP_RPD, RPD_POLL);
    318  1.3.2.2  joerg 		goto again;
    319  1.3.2.2  joerg 	}
    320  1.3.2.2  joerg 	/* good frame */
    321  1.3.2.2  joerg 	len = ((rxstat & R0_FL_MASK) >> 16) - 4; /* HASFCS */
    322  1.3.2.2  joerg         if (len > maxlen)
    323  1.3.2.2  joerg                 len = maxlen;
    324  1.3.2.2  joerg 	ptr = l->rxstore[l->rx];
    325  1.3.2.2  joerg 	memcpy(buf, ptr, len);
    326  1.3.2.2  joerg 	inv(ptr, FRAMESIZE);
    327  1.3.2.2  joerg 	RxD->xd0 = htole32(R0_OWN);
    328  1.3.2.2  joerg 	wbinv(RxD, sizeof(struct desc));
    329  1.3.2.2  joerg 	l->rx = NEXT_RXBUF(l->rx);
    330  1.3.2.2  joerg 	CSR_WRITE(l, TLP_OMR, l->omr); /* necessary? */
    331  1.3.2.2  joerg 	return len;
    332  1.3.2.2  joerg }
    333  1.3.2.2  joerg 
    334  1.3.2.2  joerg static void
    335  1.3.2.2  joerg size_srom(struct local *l)
    336  1.3.2.2  joerg {
    337  1.3.2.2  joerg 	/* determine 8/6 bit addressing SEEPROM */
    338  1.3.2.2  joerg 	l->sromsft = 8;
    339  1.3.2.2  joerg 	l->sromsft = (read_srom(l, 255) & 0x40000) ? 8 : 6;
    340  1.3.2.2  joerg }
    341  1.3.2.2  joerg 
    342  1.3.2.2  joerg /*
    343  1.3.2.2  joerg  * bare SEEPROM access with bitbang'ing
    344  1.3.2.2  joerg  */
    345  1.3.2.2  joerg #define R110	6		/* SEEPROM read op */
    346  1.3.2.2  joerg #define CS  	(1U << 0)	/* hold chip select */
    347  1.3.2.2  joerg #define CLK	(1U << 1)	/* clk bit */
    348  1.3.2.2  joerg #define D1	(1U << 2)	/* bit existence */
    349  1.3.2.2  joerg #define D0	0		/* bit absence */
    350  1.3.2.2  joerg #define VV 	(1U << 3)	/* taken 0/1 from SEEPROM */
    351  1.3.2.2  joerg 
    352  1.3.2.2  joerg static u_int
    353  1.3.2.2  joerg read_srom(struct local *l, int off)
    354  1.3.2.2  joerg {
    355  1.3.2.2  joerg 	u_int idx, cnt, ret;
    356  1.3.2.2  joerg 	uint32_t val, x1, x0, bit;
    357  1.3.2.2  joerg 
    358  1.3.2.2  joerg 	idx = off & 0xff;		/* A7-A0 */
    359  1.3.2.2  joerg 	idx |= R110 << l->sromsft;	/* 110 for READ */
    360  1.3.2.2  joerg 
    361  1.3.2.2  joerg 	val = SROM_RD | SROM_SR;
    362  1.3.2.2  joerg 	CSR_WRITE(l, TLP_APROM, val);
    363  1.3.2.2  joerg 	val |= CS;			/* hold CS */
    364  1.3.2.2  joerg 	CSR_WRITE(l, TLP_APROM, val);
    365  1.3.2.2  joerg 
    366  1.3.2.2  joerg 	x1 = val | D1;			/* 1 */
    367  1.3.2.2  joerg 	x0 = val | D0;			/* 0 */
    368  1.3.2.2  joerg 	/* instruct R110 op. at off in MSB first order */
    369  1.3.2.2  joerg 	for (cnt = (1 << (l->sromsft + 2)); cnt > 0; cnt >>= 1) {
    370  1.3.2.2  joerg 		bit = (idx & cnt) ? x1 : x0;
    371  1.3.2.2  joerg 		CSR_WRITE(l, TLP_APROM, bit);
    372  1.3.2.2  joerg 		DELAY(10);
    373  1.3.2.2  joerg 		CSR_WRITE(l, TLP_APROM, bit | CLK);
    374  1.3.2.2  joerg 		DELAY(10);
    375  1.3.2.2  joerg 	}
    376  1.3.2.2  joerg 	/* read 16bit quantity in MSB first order */
    377  1.3.2.2  joerg 	ret = 0;
    378  1.3.2.2  joerg 	for (cnt = 16; cnt > 0; cnt--) {
    379  1.3.2.2  joerg 		CSR_WRITE(l, TLP_APROM, val);
    380  1.3.2.2  joerg 		DELAY(10);
    381  1.3.2.2  joerg 		CSR_WRITE(l, TLP_APROM, val | CLK);
    382  1.3.2.2  joerg 		DELAY(10);
    383  1.3.2.2  joerg 		ret = (ret << 1) | !!(CSR_READ(l, TLP_APROM) & VV);
    384  1.3.2.2  joerg 	}
    385  1.3.2.2  joerg 	val &= ~CS; /* turn off chip select */
    386  1.3.2.2  joerg 	CSR_WRITE(l, TLP_APROM, val);
    387  1.3.2.2  joerg 
    388  1.3.2.2  joerg 	return ret;
    389  1.3.2.2  joerg }
    390  1.3.2.2  joerg 
    391  1.3.2.2  joerg #if 0
    392  1.3.2.2  joerg 
    393  1.3.2.2  joerg static u_int
    394  1.3.2.2  joerg tlp_mii_read(struct local *l, int phy, int reg)
    395  1.3.2.2  joerg {
    396  1.3.2.2  joerg 	/* later ... */
    397  1.3.2.2  joerg 	return 0;
    398  1.3.2.2  joerg }
    399  1.3.2.2  joerg 
    400  1.3.2.2  joerg static void
    401  1.3.2.2  joerg tlp_mii_write(struct local *l, int phy, int reg, int val)
    402  1.3.2.2  joerg {
    403  1.3.2.2  joerg 	/* later ... */
    404  1.3.2.2  joerg }
    405  1.3.2.2  joerg 
    406  1.3.2.2  joerg #define MII_BMCR	0x00 	/* Basic mode control register (rw) */
    407  1.3.2.2  joerg #define  BMCR_RESET	0x8000	/* reset */
    408  1.3.2.2  joerg #define  BMCR_AUTOEN	0x1000	/* autonegotiation enable */
    409  1.3.2.2  joerg #define  BMCR_ISO	0x0400	/* isolate */
    410  1.3.2.2  joerg #define  BMCR_STARTNEG	0x0200	/* restart autonegotiation */
    411  1.3.2.2  joerg #define MII_BMSR	0x01	/* Basic mode status register (ro) */
    412  1.3.2.2  joerg 
    413  1.3.2.2  joerg static void
    414  1.3.2.2  joerg mii_initphy(struct local *l)
    415  1.3.2.2  joerg {
    416  1.3.2.2  joerg 	int phy, bound;
    417  1.3.2.2  joerg 	uint32_t ctl, sts;
    418  1.3.2.2  joerg 
    419  1.3.2.2  joerg 	for (phy = 0; phy < 32; phy++) {
    420  1.3.2.2  joerg 		ctl = tlp_mii_read(l, phy, MII_BMCR);
    421  1.3.2.2  joerg 		sts = tlp_mii_read(l, phy, MII_BMSR);
    422  1.3.2.2  joerg 		if (ctl != 0xffff && sts != 0xffff)
    423  1.3.2.2  joerg 			goto found;
    424  1.3.2.2  joerg 	}
    425  1.3.2.2  joerg 	printf("MII: no PHY found\n");
    426  1.3.2.2  joerg 	return;
    427  1.3.2.2  joerg   found:
    428  1.3.2.2  joerg 	ctl = tlp_mii_read(l, phy, MII_BMCR);
    429  1.3.2.2  joerg 	tlp_mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
    430  1.3.2.2  joerg 	bound = 100;
    431  1.3.2.2  joerg 	do {
    432  1.3.2.2  joerg 		DELAY(10);
    433  1.3.2.2  joerg 		ctl = tlp_mii_read(l, phy, MII_BMCR);
    434  1.3.2.2  joerg 		if (ctl == 0xffff) {
    435  1.3.2.2  joerg 			printf("MII: PHY %d has died after reset\n", phy);
    436  1.3.2.2  joerg 			return;
    437  1.3.2.2  joerg 		}
    438  1.3.2.2  joerg 	} while (bound-- > 0 && (ctl & BMCR_RESET));
    439  1.3.2.2  joerg 	if (bound == 0) {
    440  1.3.2.2  joerg 		printf("PHY %d reset failed\n", phy);
    441  1.3.2.2  joerg 	}
    442  1.3.2.2  joerg 	ctl &= ~BMCR_ISO;
    443  1.3.2.2  joerg 	tlp_mii_write(l, phy, MII_BMCR, ctl);
    444  1.3.2.2  joerg 	sts = tlp_mii_read(l, phy, MII_BMSR) |
    445  1.3.2.2  joerg 	    tlp_mii_read(l, phy, MII_BMSR); /* read twice */
    446  1.3.2.2  joerg 	l->phy = phy;
    447  1.3.2.2  joerg 	l->bmsr = sts;
    448  1.3.2.2  joerg }
    449  1.3.2.2  joerg 
    450  1.3.2.2  joerg static void
    451  1.3.2.2  joerg mii_dealan(struct local *, u_int timo)
    452  1.3.2.2  joerg {
    453  1.3.2.2  joerg 	uint32_t anar;
    454  1.3.2.2  joerg 	u_int bound;
    455  1.3.2.2  joerg 
    456  1.3.2.2  joerg 	anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
    457  1.3.2.2  joerg 	tlp_mii_write(l, l->phy, MII_ANAR, anar);
    458  1.3.2.2  joerg 	tlp_mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    459  1.3.2.2  joerg 	l->anlpar = 0;
    460  1.3.2.2  joerg 	bound = getsecs() + timo;
    461  1.3.2.2  joerg 	do {
    462  1.3.2.2  joerg 		l->bmsr = tlp_mii_read(l, l->phy, MII_BMSR) |
    463  1.3.2.2  joerg 		   tlp_mii_read(l, l->phy, MII_BMSR); /* read twice */
    464  1.3.2.2  joerg 		if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
    465  1.3.2.2  joerg 			l->anlpar = tlp_mii_read(l, l->phy, MII_ANLPAR);
    466  1.3.2.2  joerg 			break;
    467  1.3.2.2  joerg 		}
    468  1.3.2.2  joerg 		DELAY(10 * 1000);
    469  1.3.2.2  joerg 	} while (getsecs() < bound);
    470  1.3.2.2  joerg 	return;
    471  1.3.2.2  joerg }
    472  1.3.2.2  joerg #endif
    473