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