stg.c revision 1.4 1 1.4 phx /* $NetBSD: stg.c,v 1.4 2011/03/11 17:46:30 phx Exp $ */
2 1.1 phx
3 1.1 phx /*-
4 1.1 phx * Copyright (c) 2011 Frank Wille.
5 1.1 phx * All rights reserved.
6 1.1 phx *
7 1.1 phx * Written by Frank Wille for The NetBSD Project.
8 1.1 phx *
9 1.1 phx * Redistribution and use in source and binary forms, with or without
10 1.1 phx * modification, are permitted provided that the following conditions
11 1.1 phx * are met:
12 1.1 phx * 1. Redistributions of source code must retain the above copyright
13 1.1 phx * notice, this list of conditions and the following disclaimer.
14 1.1 phx * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 phx * notice, this list of conditions and the following disclaimer in the
16 1.1 phx * documentation and/or other materials provided with the distribution.
17 1.1 phx *
18 1.1 phx * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 phx * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 phx * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 phx * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 phx * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 phx * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 phx * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 phx * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 phx * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 phx * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 phx * POSSIBILITY OF SUCH DAMAGE.
29 1.1 phx */
30 1.1 phx
31 1.1 phx #include <sys/param.h>
32 1.1 phx
33 1.1 phx #include <netinet/in.h>
34 1.1 phx #include <netinet/in_systm.h>
35 1.1 phx
36 1.1 phx #include <lib/libsa/stand.h>
37 1.1 phx #include <lib/libsa/net.h>
38 1.1 phx
39 1.1 phx #include "globals.h"
40 1.1 phx
41 1.1 phx #define CSR_WRITE_1(l, r, v) *(volatile uint8_t *)((l)->csr+(r)) = (v)
42 1.1 phx #define CSR_READ_1(l, r) *(volatile uint8_t *)((l)->csr+(r))
43 1.1 phx #define CSR_WRITE_2(l, r, v) out16rb((l)->csr+(r), (v))
44 1.1 phx #define CSR_READ_2(l, r) in16rb((l)->csr+(r))
45 1.1 phx #define CSR_WRITE_4(l, r, v) out32rb((l)->csr+(r), (v))
46 1.1 phx #define CSR_READ_4(l, r) in32rb((l)->csr+(r))
47 1.1 phx #define VTOPHYS(va) (uint32_t)(va)
48 1.1 phx #define DEVTOV(pa) (uint32_t)(pa)
49 1.1 phx #define wbinv(adr, siz) _wbinv(VTOPHYS(adr), (uint32_t)(siz))
50 1.1 phx #define inv(adr, siz) _inv(VTOPHYS(adr), (uint32_t)(siz))
51 1.1 phx #define DELAY(n) delay(n)
52 1.1 phx #define ALLOC(T,A) (T *)allocaligned(sizeof(T),(A))
53 1.1 phx
54 1.1 phx struct desc {
55 1.2 phx uint64_t xd0, xd1, xd2, dummy;
56 1.1 phx };
57 1.2 phx #define T1_EMPTY (1U << 31) /* no Tx frame available */
58 1.4 phx #define T1_NOALIGN (3U << 16) /* allow any Tx alignment */
59 1.2 phx #define T1_CNTSHIFT 24 /* Tx fragment count */
60 1.4 phx #define T2_LENSHIFT 48 /* Tx fragment length */
61 1.2 phx #define R1_DONE (1U << 31) /* desc has a Rx frame */
62 1.2 phx #define R1_FL_MASK 0xffff /* Rx frame length */
63 1.2 phx #define R1_ER_MASK 0x3f0000 /* Rx error indication */
64 1.4 phx #define R2_LENSHIFT 48 /* Rx fragment length */
65 1.1 phx
66 1.1 phx #define STGE_DMACtrl 0x00
67 1.1 phx #define DMAC_RxDMAPollNow (1U << 4)
68 1.1 phx #define DMAC_TxDMAPollNow (1U << 12)
69 1.1 phx #define STGE_TFDListPtrLo 0x10
70 1.1 phx #define STGE_TFDListPtrHi 0x14
71 1.1 phx #define STGE_RFDListPtrLo 0x1c
72 1.1 phx #define STGE_RFDListPtrHi 0x20
73 1.2 phx #define STGE_DebugCtrl 0x2c
74 1.1 phx #define STGE_AsicCtrl 0x30
75 1.1 phx #define AC_PhyMedia (1U << 7)
76 1.1 phx #define AC_GlobalReset (1U << 16)
77 1.1 phx #define AC_RxReset (1U << 17)
78 1.1 phx #define AC_TxReset (1U << 18)
79 1.1 phx #define AC_DMA (1U << 19)
80 1.1 phx #define AC_FIFO (1U << 20)
81 1.1 phx #define AC_Network (1U << 21)
82 1.1 phx #define AC_Host (1U << 22)
83 1.1 phx #define AC_AutoInit (1U << 23)
84 1.1 phx #define AC_RstOut (1U << 24)
85 1.1 phx #define AC_ResetBusy (1U << 26)
86 1.1 phx #define STGE_EepromData 0x48
87 1.1 phx #define STGE_EepromCtrl 0x4a
88 1.1 phx #define EC_EepromAddress(x) ((x) & 0xff)
89 1.1 phx #define EC_EepromOpcode(x) ((x) << 8)
90 1.1 phx #define EC_OP_RR 2
91 1.1 phx #define EC_EepromBusy (1U << 15)
92 1.1 phx #define STGE_IntEnable 0x5c
93 1.1 phx #define STGE_MACCtrl 0x6c
94 1.2 phx #define MC_DuplexSelect (1U << 5)
95 1.2 phx #define MC_StatisticsDisable (1U << 22)
96 1.1 phx #define MC_TxEnable (1U << 24)
97 1.1 phx #define MC_RxEnable (1U << 27)
98 1.1 phx #define STGE_PhyCtrl 0x76
99 1.1 phx #define PC_MgmtClk (1U << 0)
100 1.1 phx #define PC_MgmtData (1U << 1)
101 1.1 phx #define PC_MgmtDir (1U << 2)
102 1.1 phx #define PC_PhyDuplexPolarity (1U << 3)
103 1.1 phx #define PC_PhyDuplexStatus (1U << 4)
104 1.1 phx #define PC_PhyLnkPolarity (1U << 5)
105 1.1 phx #define PC_LinkSpeed(x) (((x) >> 6) & 3)
106 1.1 phx #define PC_LinkSpeed_Down 0
107 1.1 phx #define PC_LinkSpeed_10 1
108 1.1 phx #define PC_LinkSpeed_100 2
109 1.1 phx #define PC_LinkSpeed_1000 3
110 1.1 phx #define STGE_StationAddress0 0x78
111 1.1 phx #define STGE_StationAddress1 0x7a
112 1.1 phx #define STGE_StationAddress2 0x7c
113 1.2 phx #define STGE_MaxFrameSize 0x84
114 1.2 phx #define STGE_ReceiveMode 0x88
115 1.2 phx #define RM_ReceiveUnicast (1U << 0)
116 1.2 phx #define RM_ReceiveMulticast (1U << 1)
117 1.2 phx #define RM_ReceiveBroadcast (1U << 2)
118 1.2 phx #define RM_ReceiveAllFrames (1U << 3)
119 1.2 phx #define RM_ReceiveMulticastHash (1U << 4)
120 1.2 phx #define RM_ReceiveIPMulticast (1U << 5)
121 1.1 phx
122 1.1 phx #define STGE_EEPROM_SA0 0x10
123 1.1 phx
124 1.1 phx #define FRAMESIZE 1536
125 1.1 phx
126 1.1 phx struct local {
127 1.1 phx struct desc txd[2];
128 1.1 phx struct desc rxd[2];
129 1.1 phx uint8_t rxstore[2][FRAMESIZE];
130 1.1 phx unsigned csr, rx, tx, phy;
131 1.1 phx uint16_t bmsr, anlpar;
132 1.1 phx uint8_t phyctrl_saved;
133 1.1 phx };
134 1.1 phx
135 1.1 phx static int mii_read(struct local *, int, int);
136 1.1 phx static void mii_write(struct local *, int, int, int);
137 1.1 phx static void mii_initphy(struct local *);
138 1.1 phx static void mii_dealan(struct local *, unsigned);
139 1.1 phx static void mii_bitbang_sync(struct local *);
140 1.1 phx static void mii_bitbang_send(struct local *, uint32_t, int);
141 1.1 phx static void mii_bitbang_clk(struct local *, uint8_t);
142 1.1 phx static int eeprom_wait(struct local *);
143 1.1 phx
144 1.1 phx int
145 1.1 phx stg_match(unsigned tag, void *data)
146 1.1 phx {
147 1.1 phx unsigned v;
148 1.1 phx
149 1.1 phx v = pcicfgread(tag, PCI_ID_REG);
150 1.1 phx switch (v) {
151 1.3 phx case PCI_DEVICE(0x13f0, 0x1023): /* ST1023, IP1000A */
152 1.1 phx return 1;
153 1.1 phx }
154 1.1 phx return 0;
155 1.1 phx }
156 1.1 phx
157 1.1 phx void *
158 1.1 phx stg_init(unsigned tag, void *data)
159 1.1 phx {
160 1.1 phx struct local *l;
161 1.1 phx struct desc *txd, *rxd;
162 1.1 phx uint8_t *en;
163 1.1 phx unsigned i;
164 1.2 phx uint32_t macctl, reg;
165 1.4 phx static uint8_t bad[2][6] = {
166 1.4 phx { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
167 1.4 phx { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
168 1.4 phx };
169 1.1 phx
170 1.1 phx l = ALLOC(struct local, 32); /* desc alignment */
171 1.1 phx memset(l, 0, sizeof(struct local));
172 1.1 phx l->csr = DEVTOV(pcicfgread(tag, 0x14)); /* first try mem space */
173 1.1 phx if (l->csr == 0)
174 1.1 phx l->csr = DEVTOV(PCI_XIOBASE + (pcicfgread(tag, 0x10) & ~01));
175 1.1 phx
176 1.1 phx /* reset the chip */
177 1.1 phx reg = CSR_READ_4(l, STGE_AsicCtrl);
178 1.1 phx CSR_WRITE_4(l, STGE_AsicCtrl, reg | AC_GlobalReset | AC_RxReset |
179 1.1 phx AC_TxReset | AC_DMA | AC_FIFO | AC_Network | AC_Host |
180 1.1 phx AC_AutoInit | ((reg & AC_PhyMedia) ? AC_RstOut : 0));
181 1.1 phx DELAY(50000);
182 1.1 phx for (i = 0; i < 1000; i++) {
183 1.1 phx DELAY(5000);
184 1.1 phx if ((CSR_READ_4(l, STGE_AsicCtrl) & AC_ResetBusy) == 0)
185 1.1 phx break;
186 1.1 phx }
187 1.1 phx if (i >= 1000)
188 1.1 phx printf("NIC reset failed to complete!\n");
189 1.1 phx DELAY(1000);
190 1.1 phx
191 1.1 phx mii_initphy(l);
192 1.1 phx
193 1.1 phx /* read ethernet address */
194 1.1 phx en = data;
195 1.4 phx for (i = 0; i < 6; i++)
196 1.4 phx en[i] = CSR_READ_1(l, STGE_StationAddress0 + i);
197 1.4 phx
198 1.4 phx if (memcmp(en, bad[0], 6) == 0 || memcmp(en, bad[1], 6) == 0) {
199 1.1 phx uint16_t addr[3];
200 1.1 phx
201 1.1 phx for (i = 0; i < 3; i++) {
202 1.1 phx if (eeprom_wait(l) != 0)
203 1.1 phx printf("NIC: serial EEPROM is not ready!\n");
204 1.1 phx CSR_WRITE_2(l, STGE_EepromCtrl,
205 1.1 phx EC_EepromAddress(STGE_EEPROM_SA0 + i) |
206 1.1 phx EC_EepromOpcode(EC_OP_RR));
207 1.1 phx if (eeprom_wait(l) != 0)
208 1.1 phx printf("NIC: serial EEPROM read time out!\n");
209 1.1 phx addr[i] = le16toh(CSR_READ_2(l, STGE_EepromData));
210 1.1 phx }
211 1.1 phx (void)memcpy(en, addr, 6);
212 1.1 phx
213 1.1 phx /* try to read MAC from Flash, when EEPROM is empty/missing */
214 1.1 phx if (memcmp(en, bad[0], 6) == 0 || memcmp(en, bad[1], 6) == 0)
215 1.1 phx read_mac_from_flash(en);
216 1.1 phx
217 1.1 phx /* set the station address now */
218 1.1 phx for (i = 0; i < 6; i++)
219 1.1 phx CSR_WRITE_1(l, STGE_StationAddress0 + i, en[i]);
220 1.1 phx }
221 1.4 phx
222 1.1 phx printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
223 1.1 phx en[0], en[1], en[2], en[3], en[4], en[5]);
224 1.1 phx
225 1.1 phx DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
226 1.1 phx mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
227 1.1 phx
228 1.2 phx /* setup descriptors */
229 1.2 phx txd = &l->txd[0];
230 1.2 phx txd[0].xd0 = htole64(VTOPHYS(&txd[1]));
231 1.2 phx txd[0].xd1 = htole64(T1_EMPTY);
232 1.2 phx txd[1].xd0 = htole64(VTOPHYS(&txd[0]));
233 1.2 phx txd[1].xd1 = htole64(T1_EMPTY);
234 1.2 phx rxd = &l->rxd[0];
235 1.2 phx rxd[0].xd0 = htole64(VTOPHYS(&rxd[1]));
236 1.4 phx rxd[0].xd2 = htole64(VTOPHYS(l->rxstore[0]) |
237 1.2 phx ((uint64_t)FRAMESIZE << 48));
238 1.2 phx rxd[1].xd0 = htole64(VTOPHYS(&rxd[0]));
239 1.4 phx rxd[1].xd2 = htole64(VTOPHYS(l->rxstore[1]) |
240 1.2 phx ((uint64_t)FRAMESIZE << 48));
241 1.2 phx wbinv(l, sizeof(struct local));
242 1.2 phx
243 1.2 phx CSR_WRITE_2(l, STGE_IntEnable, 0);
244 1.2 phx CSR_WRITE_2(l, STGE_ReceiveMode, RM_ReceiveUnicast |
245 1.2 phx RM_ReceiveBroadcast | RM_ReceiveAllFrames | RM_ReceiveMulticast);
246 1.2 phx CSR_WRITE_4(l, STGE_TFDListPtrHi, 0);
247 1.2 phx CSR_WRITE_4(l, STGE_TFDListPtrLo, VTOPHYS(txd));
248 1.2 phx CSR_WRITE_4(l, STGE_RFDListPtrHi, 0);
249 1.2 phx CSR_WRITE_4(l, STGE_RFDListPtrLo, VTOPHYS(rxd));
250 1.2 phx CSR_WRITE_2(l, STGE_MaxFrameSize, FRAMESIZE);
251 1.2 phx CSR_WRITE_4(l, STGE_MACCtrl, 0); /* do IFSSelect(0) first */
252 1.2 phx macctl = MC_StatisticsDisable | MC_TxEnable | MC_RxEnable;
253 1.2 phx
254 1.3 phx if (PCI_REVISION(pcicfgread(tag, PCI_CLASS_REG)) >= 6) {
255 1.2 phx /* some workarounds for revisions >= 6 */
256 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
257 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0200);
258 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
259 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0010);
260 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
261 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0020);
262 1.2 phx }
263 1.2 phx
264 1.2 phx /* auto negotiation, set the current media */
265 1.1 phx mii_dealan(l, 5);
266 1.1 phx
267 1.1 phx reg = CSR_READ_1(l, STGE_PhyCtrl);
268 1.1 phx switch (PC_LinkSpeed(reg)) {
269 1.1 phx case PC_LinkSpeed_1000:
270 1.1 phx printf("1000Mbps");
271 1.1 phx break;
272 1.1 phx case PC_LinkSpeed_100:
273 1.1 phx printf("100Mbps");
274 1.1 phx break;
275 1.1 phx case PC_LinkSpeed_10:
276 1.1 phx printf("10Mbps");
277 1.1 phx break;
278 1.1 phx }
279 1.2 phx if (reg & PC_PhyDuplexStatus) {
280 1.2 phx macctl |= MC_DuplexSelect;
281 1.1 phx printf("-FDX");
282 1.2 phx }
283 1.1 phx printf("\n");
284 1.2 phx CSR_WRITE_4(l, STGE_MACCtrl, macctl);
285 1.1 phx
286 1.1 phx return l;
287 1.1 phx }
288 1.1 phx
289 1.1 phx int
290 1.1 phx stg_send(void *dev, char *buf, unsigned len)
291 1.1 phx {
292 1.1 phx struct local *l = dev;
293 1.1 phx volatile struct desc *txd;
294 1.1 phx unsigned loop;
295 1.1 phx
296 1.1 phx wbinv(buf, len);
297 1.1 phx txd = &l->txd[l->tx];
298 1.2 phx txd->xd2 = htole64(VTOPHYS(buf) | ((uint64_t)len << 48));
299 1.2 phx txd->xd1 = htole64(T1_NOALIGN | (1 << 24));
300 1.1 phx wbinv(txd, sizeof(struct desc));
301 1.2 phx CSR_WRITE_4(l, STGE_DMACtrl, DMAC_TxDMAPollNow);
302 1.1 phx loop = 100;
303 1.1 phx do {
304 1.2 phx if ((le64toh(txd->xd1) & T1_EMPTY) != 0)
305 1.1 phx goto done;
306 1.1 phx DELAY(10);
307 1.1 phx inv(txd, sizeof(struct desc));
308 1.1 phx } while (--loop > 0);
309 1.1 phx printf("xmit failed\n");
310 1.1 phx return -1;
311 1.1 phx done:
312 1.1 phx l->tx ^= 1;
313 1.1 phx return len;
314 1.1 phx }
315 1.1 phx
316 1.1 phx int
317 1.1 phx stg_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
318 1.1 phx {
319 1.1 phx struct local *l = dev;
320 1.1 phx volatile struct desc *rxd;
321 1.2 phx uint32_t sts;
322 1.1 phx unsigned bound, len;
323 1.1 phx uint8_t *ptr;
324 1.1 phx
325 1.1 phx bound = 1000 * timo;
326 1.1 phx again:
327 1.1 phx rxd = &l->rxd[l->rx];
328 1.1 phx do {
329 1.1 phx inv(rxd, sizeof(struct desc));
330 1.2 phx sts = (uint32_t)le64toh(rxd->xd1);
331 1.2 phx if ((sts & R1_DONE) != 0)
332 1.1 phx goto gotone;
333 1.1 phx DELAY(1000); /* 1 milli second */
334 1.1 phx } while (--bound > 0);
335 1.1 phx errno = 0;
336 1.1 phx return -1;
337 1.1 phx gotone:
338 1.2 phx if ((sts & R1_ER_MASK) != 0) {
339 1.1 phx rxd->xd1 = 0;
340 1.1 phx wbinv(rxd, sizeof(struct desc));
341 1.1 phx l->rx ^= 1;
342 1.1 phx goto again;
343 1.1 phx }
344 1.2 phx len = sts & R1_FL_MASK;
345 1.1 phx if (len > maxlen)
346 1.1 phx len = maxlen;
347 1.1 phx ptr = l->rxstore[l->rx];
348 1.1 phx inv(ptr, len);
349 1.1 phx memcpy(buf, ptr, len);
350 1.1 phx rxd->xd1 = 0;
351 1.1 phx wbinv(rxd, sizeof(struct desc));
352 1.1 phx l->rx ^= 1;
353 1.1 phx return len;
354 1.1 phx }
355 1.1 phx
356 1.2 phx #define R0110 6 /* 0110b read op */
357 1.2 phx #define W0101 5 /* 0101b write op */
358 1.2 phx #define A10 2 /* 10b ack turn around */
359 1.1 phx
360 1.1 phx /* read the MII by bitbanging STGE_PhyCtrl */
361 1.1 phx static int
362 1.1 phx mii_read(struct local *l, int phy, int reg)
363 1.1 phx {
364 1.2 phx unsigned data;
365 1.2 phx int i;
366 1.1 phx uint8_t v;
367 1.1 phx
368 1.1 phx /* initiate read access */
369 1.2 phx data = (R0110 << 10) | (phy << 5) | reg;
370 1.1 phx mii_bitbang_sync(l);
371 1.2 phx mii_bitbang_send(l, data, 14); /* 4OP + 5PHY + 5REG */
372 1.1 phx
373 1.1 phx /* switch direction to PHY->host */
374 1.1 phx v = l->phyctrl_saved;
375 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
376 1.1 phx
377 1.1 phx /* read data */
378 1.2 phx data = 0;
379 1.2 phx for (i = 0; i < 18; i++) { /* 2TA + 16DATA */
380 1.1 phx data <<= 1;
381 1.2 phx data |= !!(CSR_READ_1(l, STGE_PhyCtrl) & PC_MgmtData);
382 1.1 phx mii_bitbang_clk(l, v);
383 1.1 phx }
384 1.2 phx
385 1.2 phx return data & 0xffff;
386 1.1 phx }
387 1.1 phx
388 1.1 phx /* write the MII by bitbanging STGE_PhyCtrl */
389 1.1 phx static void
390 1.1 phx mii_write(struct local *l, int phy, int reg, int val)
391 1.1 phx {
392 1.2 phx unsigned data;
393 1.2 phx
394 1.2 phx data = (W0101 << 28) | (phy << 23) | (reg << 18) | (A10 << 16);
395 1.2 phx data |= val;
396 1.1 phx
397 1.1 phx mii_bitbang_sync(l);
398 1.2 phx mii_bitbang_send(l, data, 32); /* 4OP + 5PHY + 5REG + 2TA + 16DATA */
399 1.1 phx }
400 1.1 phx
401 1.1 phx #define MII_BMCR 0x00 /* Basic mode control register (rw) */
402 1.1 phx #define BMCR_RESET 0x8000 /* reset */
403 1.1 phx #define BMCR_AUTOEN 0x1000 /* autonegotiation enable */
404 1.1 phx #define BMCR_ISO 0x0400 /* isolate */
405 1.1 phx #define BMCR_STARTNEG 0x0200 /* restart autonegotiation */
406 1.1 phx #define MII_BMSR 0x01 /* Basic mode status register (ro) */
407 1.1 phx #define BMSR_ACOMP 0x0020 /* Autonegotiation complete */
408 1.1 phx #define BMSR_LINK 0x0004 /* Link status */
409 1.1 phx #define MII_ANAR 0x04 /* Autonegotiation advertisement (rw) */
410 1.1 phx #define ANAR_FC 0x0400 /* local device supports PAUSE */
411 1.1 phx #define ANAR_TX_FD 0x0100 /* local device supports 100bTx FD */
412 1.1 phx #define ANAR_TX 0x0080 /* local device supports 100bTx */
413 1.1 phx #define ANAR_10_FD 0x0040 /* local device supports 10bT FD */
414 1.1 phx #define ANAR_10 0x0020 /* local device supports 10bT */
415 1.1 phx #define ANAR_CSMA 0x0001 /* protocol selector CSMA/CD */
416 1.1 phx #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
417 1.1 phx
418 1.1 phx static void
419 1.1 phx mii_initphy(struct local *l)
420 1.1 phx {
421 1.1 phx int phy, ctl, sts, bound;
422 1.1 phx
423 1.1 phx l->phyctrl_saved = CSR_READ_1(l, STGE_PhyCtrl) &
424 1.1 phx (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
425 1.1 phx
426 1.1 phx for (phy = 0; phy < 32; phy++) {
427 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
428 1.1 phx sts = mii_read(l, phy, MII_BMSR);
429 1.1 phx if (ctl != 0xffff && sts != 0xffff && sts != 0)
430 1.1 phx goto found;
431 1.1 phx }
432 1.1 phx printf("MII: no PHY found\n");
433 1.1 phx return;
434 1.1 phx
435 1.1 phx found:
436 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
437 1.1 phx mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
438 1.1 phx
439 1.1 phx bound = 100;
440 1.1 phx do {
441 1.1 phx DELAY(10);
442 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
443 1.1 phx if (ctl == 0xffff) {
444 1.1 phx printf("MII: PHY %d has died after reset\n", phy);
445 1.1 phx return;
446 1.1 phx }
447 1.1 phx } while (bound-- > 0 && (ctl & BMCR_RESET));
448 1.1 phx if (bound == 0)
449 1.1 phx printf("PHY %d reset failed\n", phy);
450 1.1 phx
451 1.1 phx ctl &= ~BMCR_ISO;
452 1.1 phx mii_write(l, phy, MII_BMCR, ctl);
453 1.1 phx sts = mii_read(l, phy, MII_BMSR) |
454 1.1 phx mii_read(l, phy, MII_BMSR); /* read twice */
455 1.1 phx l->phy = phy;
456 1.1 phx l->bmsr = sts;
457 1.1 phx }
458 1.1 phx
459 1.1 phx static void
460 1.1 phx mii_dealan(struct local *l, unsigned timo)
461 1.1 phx {
462 1.1 phx unsigned anar, bound;
463 1.1 phx
464 1.1 phx anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
465 1.1 phx mii_write(l, l->phy, MII_ANAR, anar);
466 1.1 phx mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
467 1.1 phx l->anlpar = 0;
468 1.1 phx bound = getsecs() + timo;
469 1.1 phx do {
470 1.1 phx l->bmsr = mii_read(l, l->phy, MII_BMSR) |
471 1.1 phx mii_read(l, l->phy, MII_BMSR); /* read twice */
472 1.1 phx if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
473 1.1 phx l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
474 1.1 phx break;
475 1.1 phx }
476 1.1 phx DELAY(10 * 1000);
477 1.1 phx } while (getsecs() < bound);
478 1.1 phx }
479 1.1 phx
480 1.1 phx static void
481 1.1 phx mii_bitbang_sync(struct local *l)
482 1.1 phx {
483 1.1 phx int i;
484 1.1 phx uint8_t v;
485 1.1 phx
486 1.1 phx v = l->phyctrl_saved | PC_MgmtDir | PC_MgmtData;
487 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
488 1.1 phx DELAY(1);
489 1.1 phx for (i = 0; i < 32; i++)
490 1.1 phx mii_bitbang_clk(l, v);
491 1.1 phx }
492 1.1 phx
493 1.1 phx static void
494 1.1 phx mii_bitbang_send(struct local *l, uint32_t data, int nbits)
495 1.1 phx {
496 1.1 phx uint32_t i;
497 1.1 phx uint8_t v;
498 1.1 phx
499 1.1 phx v = l->phyctrl_saved | PC_MgmtDir;
500 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
501 1.1 phx DELAY(1);
502 1.1 phx for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
503 1.1 phx if (data & i)
504 1.1 phx v |= PC_MgmtData;
505 1.1 phx else
506 1.1 phx v &= ~PC_MgmtData;
507 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
508 1.1 phx DELAY(1);
509 1.1 phx mii_bitbang_clk(l, v);
510 1.1 phx }
511 1.1 phx }
512 1.1 phx
513 1.1 phx static void
514 1.1 phx mii_bitbang_clk(struct local *l, uint8_t v)
515 1.1 phx {
516 1.1 phx
517 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v | PC_MgmtClk);
518 1.1 phx DELAY(1);
519 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
520 1.1 phx DELAY(1);
521 1.1 phx }
522 1.1 phx
523 1.1 phx static int
524 1.1 phx eeprom_wait(struct local *l)
525 1.1 phx {
526 1.1 phx int i;
527 1.1 phx
528 1.1 phx for (i = 0; i < 1000; i++) {
529 1.1 phx DELAY(1000);
530 1.1 phx if ((CSR_READ_2(l, STGE_EepromCtrl) & EC_EepromBusy) == 0)
531 1.1 phx return 0;
532 1.1 phx }
533 1.1 phx return 1;
534 1.1 phx }
535