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