1 /* $NetBSD: sip.c,v 1.15 2008/05/30 14:54:16 nisimura Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
37 #include <lib/libsa/stand.h>
38 #include <lib/libsa/net.h>
43 * - reverse endian access every CSR.
44 * - no VTOPHYS() translation, vaddr_t == paddr_t.
45 * - PIPT writeback cache aware.
47 #define CSR_READ(l, r) in32rb((l)->csr+(r))
48 #define CSR_WRITE(l, r, v) out32rb((l)->csr+(r), (v))
49 #define VTOPHYS(va) (uint32_t)(va)
50 #define DEVTOV(pa) (uint32_t)(pa)
51 #define wbinv(adr, siz) _wbinv(VTOPHYS(adr), (uint32_t)(siz))
52 #define inv(adr, siz) _inv(VTOPHYS(adr), (uint32_t)(siz))
53 #define DELAY(n) delay(n)
54 #define ALLOC(T,A) (T *)((unsigned)alloc(sizeof(T) + (A)) &~ ((A) - 1))
57 uint32_t xd0
, xd1
, xd2
;
60 #define XD1_OWN (1U << 31)
61 #define XD1_OK (1U << 27)
64 #define CR_RST (1U << 8) /* software reset */
65 #define CR_RXR (1U << 5) /* Rx abort and reset */
66 #define CR_TXR (1U << 4) /* Tx abort and reset */
67 #define CR_RXD (1U << 3) /* graceful Rx stop */
68 #define CR_RXE (1U << 2) /* run and activate Rx */
69 #define CR_TXD (1U << 1) /* graceful Tx stop */
70 #define CR_TXE (1U << 0) /* run and activate Tx */
73 #define MEAR_EESEL (1U << 3) /* SEEP chipselect */
74 #define MEAR_EECLK (1U << 2) /* clock */
75 #define MEAR_EEDO (1U << 1) /* bit retrieve */
76 #define MEAR_EEDI (1U << 0) /* bit feed */
80 #define SIP_TXCFG 0x24
81 #define TXCFG_CSI (1U << 31)
82 #define TXCFG_HBI (1U << 30)
83 #define TXCFG_ATP (1U << 28)
84 #define TXCFG_DMA256 0x300000
86 #define SIP_RXCFG 0x34
87 #define RXCFG_ATX (1U << 28)
88 #define RXCFG_DMA256 0x300000
90 #define RFCR_RFEN (1U << 31) /* activate Rx filter */
91 #define RFCR_APM (1U << 27) /* accept perfect match */
95 #define SIP_PHYSTS 0xc0
96 #define SIP_PHYCR 0xe4
98 #define FRAMESIZE 1536
103 uint8_t store
[2][FRAMESIZE
];
105 unsigned phy
, bmsr
, anlpar
;
109 static int read_eeprom(struct local
*, int);
110 static unsigned mii_read(struct local
*, int, int);
111 static void mii_write(struct local
*, int, int, int);
112 static void mii_initphy(struct local
*);
113 static void mii_dealan(struct local
*, unsigned);
115 /* Table and macro to bit-reverse an octet. */
116 static const uint8_t bbr4
[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
117 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
120 sip_match(unsigned tag
, void *data
)
124 v
= pcicfgread(tag
, PCI_ID_REG
);
126 case PCI_DEVICE(0x100b, 0x0020):
133 sip_init(unsigned tag
, void *data
)
135 unsigned val
, i
, fdx
, txc
, rxc
;
137 struct desc
*txd
, *rxd
;
138 uint16_t eedata
[4], *ee
;
141 val
= pcicfgread(tag
, PCI_ID_REG
);
142 if (PCI_DEVICE(0x100b, 0x0020) != val
)
145 l
= ALLOC(struct local
, sizeof(struct desc
)); /* desc alignment */
146 memset(l
, 0, sizeof(struct local
));
147 l
->csr
= DEVTOV(pcicfgread(tag
, 0x14)); /* use mem space */
149 CSR_WRITE(l
, SIP_IER
, 0);
150 CSR_WRITE(l
, SIP_IMR
, 0);
151 CSR_WRITE(l
, SIP_RFCR
, 0);
152 CSR_WRITE(l
, SIP_CR
, CR_RST
);
154 val
= CSR_READ(l
, SIP_CR
);
155 } while (val
& CR_RST
); /* S1C */
159 ee
= eedata
; en
= data
;
160 ee
[0] = read_eeprom(l
, 6);
161 ee
[1] = read_eeprom(l
, 7);
162 ee
[2] = read_eeprom(l
, 8);
163 ee
[3] = read_eeprom(l
, 9);
164 en
[0] = ((*ee
& 0x1) << 7);
166 en
[0] |= ((*ee
& 0xFE00) >> 9);
167 en
[1] = ((*ee
& 0x1FE) >> 1);
168 en
[2] = ((*ee
& 0x1) << 7);
170 en
[2] |= ((*ee
& 0xFE00) >> 9);
171 en
[3] = ((*ee
& 0x1FE) >> 1);
172 en
[4] = ((*ee
& 0x1) << 7);
174 en
[4] |= ((*ee
& 0xFE00) >> 9);
175 en
[5] = ((*ee
& 0x1FE) >> 1);
176 for (i
= 0; i
< 6; i
++)
179 printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x, ",
180 en
[0], en
[1], en
[2], en
[3], en
[4], en
[5]);
181 printf("PHY %d (%04x.%04x)\n", l
->phy
,
182 mii_read(l
, l
->phy
, 2), mii_read(l
, l
->phy
, 3));
186 /* speed and duplexity are found in CFG */
187 val
= CSR_READ(l
, SIP_CFG
);
188 fdx
= !!(val
& (1U << 29));
189 printf("%s", (val
& (1U << 30)) ? "100Mbps" : "10Mbps");
195 txd
->xd0
= htole32(VTOPHYS(txd
));
197 rxd
[0].xd0
= htole32(VTOPHYS(&rxd
[1]));
198 rxd
[0].xd1
= htole32(XD1_OWN
| FRAMESIZE
);
199 rxd
[0].xd2
= htole32(VTOPHYS(l
->store
[0]));
200 rxd
[1].xd0
= htole32(VTOPHYS(&rxd
[0]));
201 rxd
[1].xd1
= htole32(XD1_OWN
| FRAMESIZE
);
202 rxd
[1].xd2
= htole32(VTOPHYS(l
->store
[1]));
203 wbinv(l
, sizeof(struct local
));
206 CSR_WRITE(l
, SIP_RFCR
, 0);
207 CSR_WRITE(l
, SIP_RFDR
, (en
[1] << 8) | en
[0]);
208 CSR_WRITE(l
, SIP_RFCR
, 2);
209 CSR_WRITE(l
, SIP_RFDR
, (en
[3] << 8) | en
[2]);
210 CSR_WRITE(l
, SIP_RFCR
, 4);
211 CSR_WRITE(l
, SIP_RFDR
, (en
[5] << 8) | en
[4]);
212 CSR_WRITE(l
, SIP_RFCR
, RFCR_RFEN
| RFCR_APM
);
214 txc
= TXCFG_ATP
| TXCFG_DMA256
| 0x1002;
215 rxc
= RXCFG_DMA256
| 0x20;
217 txc
|= TXCFG_CSI
| TXCFG_HBI
;
221 CSR_WRITE(l
, SIP_TXDP
, VTOPHYS(txd
));
222 CSR_WRITE(l
, SIP_RXDP
, VTOPHYS(rxd
));
223 CSR_WRITE(l
, SIP_TXCFG
, txc
);
224 CSR_WRITE(l
, SIP_RXCFG
, rxc
);
225 CSR_WRITE(l
, SIP_CR
, l
->cr
);
231 sip_send(void *dev
, char *buf
, unsigned len
)
233 struct local
*l
= dev
;
234 volatile struct desc
*txd
;
239 txd
->xd2
= htole32(VTOPHYS(buf
));
240 txd
->xd1
= htole32(XD1_OWN
| (len
& 0xfff));
241 wbinv(txd
, sizeof(struct desc
));
242 CSR_WRITE(l
, SIP_CR
, l
->cr
| CR_TXE
);
245 if ((le32toh(txd
->xd1
) & XD1_OWN
) == 0)
248 inv(txd
, sizeof(struct desc
));
249 } while (--loop
!= 0);
250 printf("xmit failed\n");
257 sip_recv(void *dev
, char *buf
, unsigned maxlen
, unsigned timo
)
259 struct local
*l
= dev
;
260 volatile struct desc
*rxd
;
261 unsigned bound
, rxstat
, len
;
265 printf("recving with %u sec. timeout\n", timo
);
267 rxd
= &l
->rxd
[l
->rx
];
269 inv(rxd
, sizeof(struct desc
));
270 rxstat
= le32toh(rxd
->xd1
);
271 if ((rxstat
& XD1_OWN
) == 0)
273 DELAY(1000); /* 1 milli second */
274 } while (--bound
> 0);
278 if ((rxstat
& XD1_OK
) == 0) {
279 rxd
->xd1
= htole32(XD1_OWN
| FRAMESIZE
);
280 wbinv(rxd
, sizeof(struct desc
));
285 len
= (rxstat
& 0xfff) - 4 /* HASFCS */;
288 ptr
= l
->store
[l
->rx
];
290 memcpy(buf
, ptr
, len
);
291 rxd
->xd1
= htole32(XD1_OWN
| FRAMESIZE
);
292 wbinv(rxd
, sizeof(struct desc
));
294 CSR_WRITE(l
, SIP_CR
, l
->cr
);
299 read_eeprom(struct local
*l
, int loc
)
301 #define R110 06 /* SEEPROM READ op. */
304 /* hold chip select */
306 CSR_WRITE(l
, SIP_MEAR
, v
);
308 data
= (R110
<< 6) | (loc
& 0x3f); /* 6 bit addressing */
309 /* instruct R110 op. at loc in MSB first order */
310 for (i
= (1 << 8); i
!= 0; i
>>= 1) {
315 CSR_WRITE(l
, SIP_MEAR
, v
);
316 CSR_WRITE(l
, SIP_MEAR
, v
| MEAR_EECLK
);
318 CSR_WRITE(l
, SIP_MEAR
, v
);
322 /* read 16bit quantity in MSB first order */
324 for (i
= 0; i
< 16; i
++) {
325 CSR_WRITE(l
, SIP_MEAR
, v
| MEAR_EECLK
);
327 data
= (data
<< 1) | !!(CSR_READ(l
, SIP_MEAR
) & MEAR_EEDO
);
328 CSR_WRITE(l
, SIP_MEAR
, v
);
331 /* turn off chip select */
332 CSR_WRITE(l
, SIP_MEAR
, 0);
337 #define MII_BMCR 0x00 /* Basic mode control register (rw) */
338 #define BMCR_RESET 0x8000 /* reset */
339 #define BMCR_AUTOEN 0x1000 /* autonegotiation enable */
340 #define BMCR_ISO 0x0400 /* isolate */
341 #define BMCR_STARTNEG 0x0200 /* restart autonegotiation */
342 #define MII_BMSR 0x01 /* Basic mode status register (ro) */
343 #define BMSR_ACOMP 0x0020 /* Autonegotiation complete */
344 #define BMSR_LINK 0x0004 /* Link status */
345 #define MII_ANAR 0x04 /* Autonegotiation advertisement (rw) */
346 #define ANAR_FC 0x0400 /* local device supports PAUSE */
347 #define ANAR_TX_FD 0x0100 /* local device supports 100bTx FD */
348 #define ANAR_TX 0x0080 /* local device supports 100bTx */
349 #define ANAR_10_FD 0x0040 /* local device supports 10bT FD */
350 #define ANAR_10 0x0020 /* local device supports 10bT */
351 #define ANAR_CSMA 0x0001 /* protocol selector CSMA/CD */
352 #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
355 mii_read(struct local
*l
, int phy
, int reg
)
360 val
= CSR_READ(l
, SIP_BMCR
+ (reg
<< 2));
361 } while (reg
== MII_BMSR
&& val
== 0);
366 mii_write(struct local
*l
, int phy
, int reg
, int val
)
369 CSR_WRITE(l
, SIP_BMCR
+ (reg
<< 2), val
);
373 mii_initphy(struct local
*l
)
375 int phy
, ctl
, sts
, bound
;
377 for (phy
= 0; phy
< 32; phy
++) {
378 ctl
= mii_read(l
, phy
, MII_BMCR
);
379 sts
= mii_read(l
, phy
, MII_BMSR
);
380 if (ctl
!= 0xffff && sts
!= 0xffff)
383 printf("MII: no PHY found\n");
386 ctl
= mii_read(l
, phy
, MII_BMCR
);
387 mii_write(l
, phy
, MII_BMCR
, ctl
| BMCR_RESET
);
391 ctl
= mii_read(l
, phy
, MII_BMCR
);
393 printf("MII: PHY %d has died after reset\n", phy
);
396 } while (bound
-- > 0 && (ctl
& BMCR_RESET
));
398 printf("PHY %d reset failed\n", phy
);
401 mii_write(l
, phy
, MII_BMCR
, ctl
);
402 sts
= mii_read(l
, phy
, MII_BMSR
) |
403 mii_read(l
, phy
, MII_BMSR
); /* read twice */
404 l
->phy
= phy
; /* should be 0 */
409 mii_dealan(struct local
*l
, unsigned timo
)
411 unsigned anar
, bound
;
413 anar
= ANAR_TX_FD
| ANAR_TX
| ANAR_10_FD
| ANAR_10
| ANAR_CSMA
;
414 mii_write(l
, l
->phy
, MII_ANAR
, anar
);
415 mii_write(l
, l
->phy
, MII_BMCR
, BMCR_AUTOEN
| BMCR_STARTNEG
);
417 bound
= getsecs() + timo
;
419 l
->bmsr
= mii_read(l
, l
->phy
, MII_BMSR
) |
420 mii_read(l
, l
->phy
, MII_BMSR
); /* read twice */
421 if ((l
->bmsr
& BMSR_LINK
) && (l
->bmsr
& BMSR_ACOMP
)) {
422 l
->anlpar
= mii_read(l
, l
->phy
, MII_ANLPAR
);
426 } while (getsecs() < bound
);