stg.c revision 1.6 1 1.6 phx /* $NetBSD: stg.c,v 1.6 2011/10/30 21:08:33 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.6 phx #define CSR_WRITE_1(l, r, v) out8((l)->csr+(r), (v))
42 1.6 phx #define CSR_READ_1(l, r) in8((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.5 phx static void stg_reset(struct local *);
136 1.1 phx static int mii_read(struct local *, int, int);
137 1.1 phx static void mii_write(struct local *, int, int, int);
138 1.1 phx static void mii_initphy(struct local *);
139 1.1 phx static void mii_dealan(struct local *, unsigned);
140 1.1 phx static void mii_bitbang_sync(struct local *);
141 1.1 phx static void mii_bitbang_send(struct local *, uint32_t, int);
142 1.1 phx static void mii_bitbang_clk(struct local *, uint8_t);
143 1.1 phx static int eeprom_wait(struct local *);
144 1.1 phx
145 1.1 phx int
146 1.1 phx stg_match(unsigned tag, void *data)
147 1.1 phx {
148 1.1 phx unsigned v;
149 1.1 phx
150 1.1 phx v = pcicfgread(tag, PCI_ID_REG);
151 1.1 phx switch (v) {
152 1.3 phx case PCI_DEVICE(0x13f0, 0x1023): /* ST1023, IP1000A */
153 1.1 phx return 1;
154 1.1 phx }
155 1.1 phx return 0;
156 1.1 phx }
157 1.1 phx
158 1.1 phx void *
159 1.1 phx stg_init(unsigned tag, void *data)
160 1.1 phx {
161 1.1 phx struct local *l;
162 1.1 phx struct desc *txd, *rxd;
163 1.1 phx uint8_t *en;
164 1.1 phx unsigned i;
165 1.2 phx uint32_t macctl, reg;
166 1.4 phx static uint8_t bad[2][6] = {
167 1.4 phx { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
168 1.4 phx { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
169 1.4 phx };
170 1.1 phx
171 1.1 phx l = ALLOC(struct local, 32); /* desc alignment */
172 1.1 phx memset(l, 0, sizeof(struct local));
173 1.1 phx l->csr = DEVTOV(pcicfgread(tag, 0x14)); /* first try mem space */
174 1.1 phx if (l->csr == 0)
175 1.1 phx l->csr = DEVTOV(PCI_XIOBASE + (pcicfgread(tag, 0x10) & ~01));
176 1.1 phx
177 1.5 phx stg_reset(l);
178 1.1 phx
179 1.1 phx mii_initphy(l);
180 1.1 phx
181 1.1 phx /* read ethernet address */
182 1.1 phx en = data;
183 1.4 phx for (i = 0; i < 6; i++)
184 1.4 phx en[i] = CSR_READ_1(l, STGE_StationAddress0 + i);
185 1.4 phx
186 1.4 phx if (memcmp(en, bad[0], 6) == 0 || memcmp(en, bad[1], 6) == 0) {
187 1.1 phx uint16_t addr[3];
188 1.1 phx
189 1.1 phx for (i = 0; i < 3; i++) {
190 1.1 phx if (eeprom_wait(l) != 0)
191 1.1 phx printf("NIC: serial EEPROM is not ready!\n");
192 1.1 phx CSR_WRITE_2(l, STGE_EepromCtrl,
193 1.1 phx EC_EepromAddress(STGE_EEPROM_SA0 + i) |
194 1.1 phx EC_EepromOpcode(EC_OP_RR));
195 1.1 phx if (eeprom_wait(l) != 0)
196 1.1 phx printf("NIC: serial EEPROM read time out!\n");
197 1.1 phx addr[i] = le16toh(CSR_READ_2(l, STGE_EepromData));
198 1.1 phx }
199 1.1 phx (void)memcpy(en, addr, 6);
200 1.1 phx
201 1.1 phx /* try to read MAC from Flash, when EEPROM is empty/missing */
202 1.1 phx if (memcmp(en, bad[0], 6) == 0 || memcmp(en, bad[1], 6) == 0)
203 1.1 phx read_mac_from_flash(en);
204 1.1 phx
205 1.1 phx /* set the station address now */
206 1.1 phx for (i = 0; i < 6; i++)
207 1.1 phx CSR_WRITE_1(l, STGE_StationAddress0 + i, en[i]);
208 1.1 phx }
209 1.4 phx
210 1.1 phx printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
211 1.1 phx en[0], en[1], en[2], en[3], en[4], en[5]);
212 1.1 phx
213 1.1 phx DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
214 1.1 phx mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
215 1.1 phx
216 1.2 phx /* setup descriptors */
217 1.2 phx txd = &l->txd[0];
218 1.2 phx txd[0].xd0 = htole64(VTOPHYS(&txd[1]));
219 1.2 phx txd[0].xd1 = htole64(T1_EMPTY);
220 1.2 phx txd[1].xd0 = htole64(VTOPHYS(&txd[0]));
221 1.2 phx txd[1].xd1 = htole64(T1_EMPTY);
222 1.2 phx rxd = &l->rxd[0];
223 1.2 phx rxd[0].xd0 = htole64(VTOPHYS(&rxd[1]));
224 1.4 phx rxd[0].xd2 = htole64(VTOPHYS(l->rxstore[0]) |
225 1.2 phx ((uint64_t)FRAMESIZE << 48));
226 1.2 phx rxd[1].xd0 = htole64(VTOPHYS(&rxd[0]));
227 1.4 phx rxd[1].xd2 = htole64(VTOPHYS(l->rxstore[1]) |
228 1.2 phx ((uint64_t)FRAMESIZE << 48));
229 1.2 phx wbinv(l, sizeof(struct local));
230 1.2 phx
231 1.2 phx CSR_WRITE_2(l, STGE_IntEnable, 0);
232 1.2 phx CSR_WRITE_2(l, STGE_ReceiveMode, RM_ReceiveUnicast |
233 1.2 phx RM_ReceiveBroadcast | RM_ReceiveAllFrames | RM_ReceiveMulticast);
234 1.2 phx CSR_WRITE_4(l, STGE_TFDListPtrHi, 0);
235 1.2 phx CSR_WRITE_4(l, STGE_TFDListPtrLo, VTOPHYS(txd));
236 1.2 phx CSR_WRITE_4(l, STGE_RFDListPtrHi, 0);
237 1.2 phx CSR_WRITE_4(l, STGE_RFDListPtrLo, VTOPHYS(rxd));
238 1.2 phx CSR_WRITE_2(l, STGE_MaxFrameSize, FRAMESIZE);
239 1.2 phx CSR_WRITE_4(l, STGE_MACCtrl, 0); /* do IFSSelect(0) first */
240 1.2 phx macctl = MC_StatisticsDisable | MC_TxEnable | MC_RxEnable;
241 1.2 phx
242 1.3 phx if (PCI_REVISION(pcicfgread(tag, PCI_CLASS_REG)) >= 6) {
243 1.2 phx /* some workarounds for revisions >= 6 */
244 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
245 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0200);
246 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
247 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0010);
248 1.2 phx CSR_WRITE_2(l, STGE_DebugCtrl,
249 1.2 phx CSR_READ_2(l, STGE_DebugCtrl) | 0x0020);
250 1.2 phx }
251 1.2 phx
252 1.2 phx /* auto negotiation, set the current media */
253 1.1 phx mii_dealan(l, 5);
254 1.1 phx
255 1.1 phx reg = CSR_READ_1(l, STGE_PhyCtrl);
256 1.1 phx switch (PC_LinkSpeed(reg)) {
257 1.1 phx case PC_LinkSpeed_1000:
258 1.1 phx printf("1000Mbps");
259 1.1 phx break;
260 1.1 phx case PC_LinkSpeed_100:
261 1.1 phx printf("100Mbps");
262 1.1 phx break;
263 1.1 phx case PC_LinkSpeed_10:
264 1.1 phx printf("10Mbps");
265 1.1 phx break;
266 1.1 phx }
267 1.2 phx if (reg & PC_PhyDuplexStatus) {
268 1.2 phx macctl |= MC_DuplexSelect;
269 1.1 phx printf("-FDX");
270 1.2 phx }
271 1.1 phx printf("\n");
272 1.2 phx CSR_WRITE_4(l, STGE_MACCtrl, macctl);
273 1.1 phx
274 1.1 phx return l;
275 1.1 phx }
276 1.1 phx
277 1.5 phx void
278 1.5 phx stg_shutdown(void *dev)
279 1.5 phx {
280 1.5 phx struct local *l = dev;
281 1.5 phx
282 1.5 phx /*
283 1.5 phx * We have to reset the chip, when we don't need it anymore,
284 1.5 phx * otherwise bad things will happen (e.g. the DSM-G600 will no
285 1.5 phx * longer be able to reboot).
286 1.5 phx */
287 1.5 phx stg_reset(l);
288 1.5 phx }
289 1.5 phx
290 1.1 phx int
291 1.1 phx stg_send(void *dev, char *buf, unsigned len)
292 1.1 phx {
293 1.1 phx struct local *l = dev;
294 1.1 phx volatile struct desc *txd;
295 1.1 phx unsigned loop;
296 1.1 phx
297 1.1 phx wbinv(buf, len);
298 1.1 phx txd = &l->txd[l->tx];
299 1.2 phx txd->xd2 = htole64(VTOPHYS(buf) | ((uint64_t)len << 48));
300 1.2 phx txd->xd1 = htole64(T1_NOALIGN | (1 << 24));
301 1.1 phx wbinv(txd, sizeof(struct desc));
302 1.2 phx CSR_WRITE_4(l, STGE_DMACtrl, DMAC_TxDMAPollNow);
303 1.1 phx loop = 100;
304 1.1 phx do {
305 1.2 phx if ((le64toh(txd->xd1) & T1_EMPTY) != 0)
306 1.1 phx goto done;
307 1.1 phx DELAY(10);
308 1.1 phx inv(txd, sizeof(struct desc));
309 1.1 phx } while (--loop > 0);
310 1.1 phx printf("xmit failed\n");
311 1.1 phx return -1;
312 1.1 phx done:
313 1.1 phx l->tx ^= 1;
314 1.1 phx return len;
315 1.1 phx }
316 1.1 phx
317 1.1 phx int
318 1.1 phx stg_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
319 1.1 phx {
320 1.1 phx struct local *l = dev;
321 1.1 phx volatile struct desc *rxd;
322 1.2 phx uint32_t sts;
323 1.1 phx unsigned bound, len;
324 1.1 phx uint8_t *ptr;
325 1.1 phx
326 1.1 phx bound = 1000 * timo;
327 1.1 phx again:
328 1.1 phx rxd = &l->rxd[l->rx];
329 1.1 phx do {
330 1.1 phx inv(rxd, sizeof(struct desc));
331 1.2 phx sts = (uint32_t)le64toh(rxd->xd1);
332 1.2 phx if ((sts & R1_DONE) != 0)
333 1.1 phx goto gotone;
334 1.1 phx DELAY(1000); /* 1 milli second */
335 1.1 phx } while (--bound > 0);
336 1.1 phx errno = 0;
337 1.1 phx return -1;
338 1.1 phx gotone:
339 1.2 phx if ((sts & R1_ER_MASK) != 0) {
340 1.1 phx rxd->xd1 = 0;
341 1.1 phx wbinv(rxd, sizeof(struct desc));
342 1.1 phx l->rx ^= 1;
343 1.1 phx goto again;
344 1.1 phx }
345 1.2 phx len = sts & R1_FL_MASK;
346 1.1 phx if (len > maxlen)
347 1.1 phx len = maxlen;
348 1.1 phx ptr = l->rxstore[l->rx];
349 1.1 phx inv(ptr, len);
350 1.1 phx memcpy(buf, ptr, len);
351 1.1 phx rxd->xd1 = 0;
352 1.1 phx wbinv(rxd, sizeof(struct desc));
353 1.1 phx l->rx ^= 1;
354 1.1 phx return len;
355 1.1 phx }
356 1.1 phx
357 1.5 phx static void
358 1.5 phx stg_reset(struct local *l)
359 1.5 phx {
360 1.5 phx uint32_t reg;
361 1.5 phx int i;
362 1.5 phx
363 1.5 phx reg = CSR_READ_4(l, STGE_AsicCtrl);
364 1.5 phx CSR_WRITE_4(l, STGE_AsicCtrl, reg | AC_GlobalReset | AC_RxReset |
365 1.5 phx AC_TxReset | AC_DMA | AC_FIFO | AC_Network | AC_Host |
366 1.5 phx AC_AutoInit | ((reg & AC_PhyMedia) ? AC_RstOut : 0));
367 1.5 phx DELAY(50000);
368 1.5 phx for (i = 0; i < 1000; i++) {
369 1.5 phx DELAY(5000);
370 1.5 phx if ((CSR_READ_4(l, STGE_AsicCtrl) & AC_ResetBusy) == 0)
371 1.5 phx break;
372 1.5 phx }
373 1.5 phx if (i >= 1000)
374 1.5 phx printf("NIC reset failed to complete!\n");
375 1.5 phx DELAY(1000);
376 1.5 phx }
377 1.5 phx
378 1.2 phx #define R0110 6 /* 0110b read op */
379 1.2 phx #define W0101 5 /* 0101b write op */
380 1.2 phx #define A10 2 /* 10b ack turn around */
381 1.1 phx
382 1.1 phx /* read the MII by bitbanging STGE_PhyCtrl */
383 1.1 phx static int
384 1.1 phx mii_read(struct local *l, int phy, int reg)
385 1.1 phx {
386 1.2 phx unsigned data;
387 1.2 phx int i;
388 1.1 phx uint8_t v;
389 1.1 phx
390 1.1 phx /* initiate read access */
391 1.2 phx data = (R0110 << 10) | (phy << 5) | reg;
392 1.1 phx mii_bitbang_sync(l);
393 1.2 phx mii_bitbang_send(l, data, 14); /* 4OP + 5PHY + 5REG */
394 1.1 phx
395 1.1 phx /* switch direction to PHY->host */
396 1.1 phx v = l->phyctrl_saved;
397 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
398 1.1 phx
399 1.1 phx /* read data */
400 1.2 phx data = 0;
401 1.2 phx for (i = 0; i < 18; i++) { /* 2TA + 16DATA */
402 1.1 phx data <<= 1;
403 1.2 phx data |= !!(CSR_READ_1(l, STGE_PhyCtrl) & PC_MgmtData);
404 1.1 phx mii_bitbang_clk(l, v);
405 1.1 phx }
406 1.2 phx
407 1.2 phx return data & 0xffff;
408 1.1 phx }
409 1.1 phx
410 1.1 phx /* write the MII by bitbanging STGE_PhyCtrl */
411 1.1 phx static void
412 1.1 phx mii_write(struct local *l, int phy, int reg, int val)
413 1.1 phx {
414 1.2 phx unsigned data;
415 1.2 phx
416 1.2 phx data = (W0101 << 28) | (phy << 23) | (reg << 18) | (A10 << 16);
417 1.2 phx data |= val;
418 1.1 phx
419 1.1 phx mii_bitbang_sync(l);
420 1.2 phx mii_bitbang_send(l, data, 32); /* 4OP + 5PHY + 5REG + 2TA + 16DATA */
421 1.1 phx }
422 1.1 phx
423 1.1 phx #define MII_BMCR 0x00 /* Basic mode control register (rw) */
424 1.1 phx #define BMCR_RESET 0x8000 /* reset */
425 1.1 phx #define BMCR_AUTOEN 0x1000 /* autonegotiation enable */
426 1.1 phx #define BMCR_ISO 0x0400 /* isolate */
427 1.1 phx #define BMCR_STARTNEG 0x0200 /* restart autonegotiation */
428 1.1 phx #define MII_BMSR 0x01 /* Basic mode status register (ro) */
429 1.1 phx #define BMSR_ACOMP 0x0020 /* Autonegotiation complete */
430 1.1 phx #define BMSR_LINK 0x0004 /* Link status */
431 1.1 phx #define MII_ANAR 0x04 /* Autonegotiation advertisement (rw) */
432 1.1 phx #define ANAR_FC 0x0400 /* local device supports PAUSE */
433 1.1 phx #define ANAR_TX_FD 0x0100 /* local device supports 100bTx FD */
434 1.1 phx #define ANAR_TX 0x0080 /* local device supports 100bTx */
435 1.1 phx #define ANAR_10_FD 0x0040 /* local device supports 10bT FD */
436 1.1 phx #define ANAR_10 0x0020 /* local device supports 10bT */
437 1.1 phx #define ANAR_CSMA 0x0001 /* protocol selector CSMA/CD */
438 1.1 phx #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
439 1.1 phx
440 1.1 phx static void
441 1.1 phx mii_initphy(struct local *l)
442 1.1 phx {
443 1.1 phx int phy, ctl, sts, bound;
444 1.1 phx
445 1.1 phx l->phyctrl_saved = CSR_READ_1(l, STGE_PhyCtrl) &
446 1.1 phx (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
447 1.1 phx
448 1.1 phx for (phy = 0; phy < 32; phy++) {
449 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
450 1.1 phx sts = mii_read(l, phy, MII_BMSR);
451 1.1 phx if (ctl != 0xffff && sts != 0xffff && sts != 0)
452 1.1 phx goto found;
453 1.1 phx }
454 1.1 phx printf("MII: no PHY found\n");
455 1.1 phx return;
456 1.1 phx
457 1.1 phx found:
458 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
459 1.1 phx mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
460 1.1 phx
461 1.1 phx bound = 100;
462 1.1 phx do {
463 1.1 phx DELAY(10);
464 1.1 phx ctl = mii_read(l, phy, MII_BMCR);
465 1.1 phx if (ctl == 0xffff) {
466 1.1 phx printf("MII: PHY %d has died after reset\n", phy);
467 1.1 phx return;
468 1.1 phx }
469 1.1 phx } while (bound-- > 0 && (ctl & BMCR_RESET));
470 1.1 phx if (bound == 0)
471 1.1 phx printf("PHY %d reset failed\n", phy);
472 1.1 phx
473 1.1 phx ctl &= ~BMCR_ISO;
474 1.1 phx mii_write(l, phy, MII_BMCR, ctl);
475 1.1 phx sts = mii_read(l, phy, MII_BMSR) |
476 1.1 phx mii_read(l, phy, MII_BMSR); /* read twice */
477 1.1 phx l->phy = phy;
478 1.1 phx l->bmsr = sts;
479 1.1 phx }
480 1.1 phx
481 1.1 phx static void
482 1.1 phx mii_dealan(struct local *l, unsigned timo)
483 1.1 phx {
484 1.1 phx unsigned anar, bound;
485 1.1 phx
486 1.1 phx anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
487 1.1 phx mii_write(l, l->phy, MII_ANAR, anar);
488 1.1 phx mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
489 1.1 phx l->anlpar = 0;
490 1.1 phx bound = getsecs() + timo;
491 1.1 phx do {
492 1.1 phx l->bmsr = mii_read(l, l->phy, MII_BMSR) |
493 1.1 phx mii_read(l, l->phy, MII_BMSR); /* read twice */
494 1.1 phx if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
495 1.1 phx l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
496 1.1 phx break;
497 1.1 phx }
498 1.1 phx DELAY(10 * 1000);
499 1.1 phx } while (getsecs() < bound);
500 1.1 phx }
501 1.1 phx
502 1.1 phx static void
503 1.1 phx mii_bitbang_sync(struct local *l)
504 1.1 phx {
505 1.1 phx int i;
506 1.1 phx uint8_t v;
507 1.1 phx
508 1.1 phx v = l->phyctrl_saved | PC_MgmtDir | PC_MgmtData;
509 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
510 1.1 phx DELAY(1);
511 1.1 phx for (i = 0; i < 32; i++)
512 1.1 phx mii_bitbang_clk(l, v);
513 1.1 phx }
514 1.1 phx
515 1.1 phx static void
516 1.1 phx mii_bitbang_send(struct local *l, uint32_t data, int nbits)
517 1.1 phx {
518 1.1 phx uint32_t i;
519 1.1 phx uint8_t v;
520 1.1 phx
521 1.1 phx v = l->phyctrl_saved | PC_MgmtDir;
522 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
523 1.1 phx DELAY(1);
524 1.1 phx for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
525 1.1 phx if (data & i)
526 1.1 phx v |= PC_MgmtData;
527 1.1 phx else
528 1.1 phx v &= ~PC_MgmtData;
529 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
530 1.1 phx DELAY(1);
531 1.1 phx mii_bitbang_clk(l, v);
532 1.1 phx }
533 1.1 phx }
534 1.1 phx
535 1.1 phx static void
536 1.1 phx mii_bitbang_clk(struct local *l, uint8_t v)
537 1.1 phx {
538 1.1 phx
539 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v | PC_MgmtClk);
540 1.1 phx DELAY(1);
541 1.1 phx CSR_WRITE_1(l, STGE_PhyCtrl, v);
542 1.1 phx DELAY(1);
543 1.1 phx }
544 1.1 phx
545 1.1 phx static int
546 1.1 phx eeprom_wait(struct local *l)
547 1.1 phx {
548 1.1 phx int i;
549 1.1 phx
550 1.1 phx for (i = 0; i < 1000; i++) {
551 1.1 phx DELAY(1000);
552 1.1 phx if ((CSR_READ_2(l, STGE_EepromCtrl) & EC_EepromBusy) == 0)
553 1.1 phx return 0;
554 1.1 phx }
555 1.1 phx return 1;
556 1.1 phx }
557