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