tlp.c revision 1.1 1 1.1 tsutsui /* $NetBSD: tlp.c,v 1.1 2007/10/30 15:07:08 tsutsui Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by Tohru Nishimura.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
19 1.1 tsutsui * must display the following acknowledgement:
20 1.1 tsutsui * This product includes software developed by the NetBSD
21 1.1 tsutsui * Foundation, Inc. and its contributors.
22 1.1 tsutsui * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 tsutsui * contributors may be used to endorse or promote products derived
24 1.1 tsutsui * from this software without specific prior written permission.
25 1.1 tsutsui *
26 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
37 1.1 tsutsui */
38 1.1 tsutsui
39 1.1 tsutsui #include <sys/param.h>
40 1.1 tsutsui #include <sys/socket.h>
41 1.1 tsutsui
42 1.1 tsutsui #include <netinet/in.h>
43 1.1 tsutsui #include <netinet/in_systm.h>
44 1.1 tsutsui
45 1.1 tsutsui #include <lib/libsa/stand.h>
46 1.1 tsutsui #include <lib/libsa/net.h>
47 1.1 tsutsui
48 1.1 tsutsui #include <mips/cpuregs.h>
49 1.1 tsutsui
50 1.1 tsutsui #include "boot.h"
51 1.1 tsutsui
52 1.1 tsutsui /*
53 1.1 tsutsui * - little endian access for CSR register.
54 1.1 tsutsui * - assume KSEG0 on vtophys() translation.
55 1.1 tsutsui * - PIPT writeback cache aware.
56 1.1 tsutsui */
57 1.1 tsutsui #define CSR_WRITE(l, r, v) \
58 1.1 tsutsui do { \
59 1.1 tsutsui *(volatile uint32_t *)((l)->csr + (r)) = (v); \
60 1.1 tsutsui } while (0)
61 1.1 tsutsui #define CSR_READ(l, r) (*(volatile uint32_t *)((l)->csr + (r)))
62 1.1 tsutsui #define VTOPHYS(va) MIPS_KSEG0_TO_PHYS(va)
63 1.1 tsutsui #define wb(adr, siz) pdcache_wb((uint32_t)(adr), (u_int)(siz))
64 1.1 tsutsui #define wbinv(adr, siz) pdcache_wbinv((uint32_t)(adr), (u_int)(siz))
65 1.1 tsutsui #define inv(adr, siz) pdcache_inv((uint32_t)(adr), (u_int)(siz))
66 1.1 tsutsui #define DELAY(n) delay(n)
67 1.1 tsutsui #define ALLOC(T, A) (T *)((uint32_t)alloc(sizeof(T) + (A)) & ~((A) - 1))
68 1.1 tsutsui
69 1.1 tsutsui #define T0_OWN (1U<<31) /* desc is ready to tx */
70 1.1 tsutsui #define T0_ES (1U<<15) /* Tx error summary */
71 1.1 tsutsui #define T1_LS (1U<<30) /* last segment */
72 1.1 tsutsui #define T1_FS (1U<<29) /* first segment */
73 1.1 tsutsui #define T1_SET (1U<<27) /* "setup packet" */
74 1.1 tsutsui #define T1_TER (1U<<25) /* end of ring mark */
75 1.1 tsutsui #define T1_TBS_MASK 0x7ff /* segment size 10:0 */
76 1.1 tsutsui #define R0_OWN (1U<<31) /* desc is empty */
77 1.1 tsutsui #define R0_FS (1U<<30) /* first desc of frame */
78 1.1 tsutsui #define R0_LS (1U<<8) /* last desc of frame */
79 1.1 tsutsui #define R0_ES (1U<<15) /* Rx error summary */
80 1.1 tsutsui #define R1_RER (1U<<25) /* end of ring mark */
81 1.1 tsutsui #define R0_FL_MASK 0x3fff0000 /* frame length 29:16 */
82 1.1 tsutsui #define R1_RBS_MASK 0x7ff /* segment size 10:0 */
83 1.1 tsutsui
84 1.1 tsutsui struct desc {
85 1.1 tsutsui volatile uint32_t xd0, xd1, xd2, xd3;
86 1.1 tsutsui };
87 1.1 tsutsui
88 1.1 tsutsui #define TLP_BMR 0x000 /* 0: bus mode */
89 1.1 tsutsui #define BMR_RST (1U<< 0) /* software reset */
90 1.1 tsutsui #define TLP_TPD 0x008 /* 1: instruct Tx to start */
91 1.1 tsutsui #define TPD_POLL (1U<< 0) /* transmit poll demand */
92 1.1 tsutsui #define TLP_RPD 0x010 /* 2: instruct Rx to start */
93 1.1 tsutsui #define RPD_POLL (1U<< 0) /* receive poll demand */
94 1.1 tsutsui #define TLP_RRBA 0x018 /* 3: Rx descriptor base */
95 1.1 tsutsui #define TLP_TRBA 0x020 /* 4: Tx descriptor base */
96 1.1 tsutsui #define TLP_STS 0x028 /* 5: status */
97 1.1 tsutsui #define STS_TS 0x00700000 /* Tx status */
98 1.1 tsutsui #define STS_RS 0x000e0000 /* Rx status */
99 1.1 tsutsui #define TLP_OMR 0x030 /* 6: operation mode */
100 1.1 tsutsui #define OMR_SDP (1U<<25) /* always ON */
101 1.1 tsutsui #define OMR_PS (1U<<18) /* port select */
102 1.1 tsutsui #define OMR_PM (1U<< 6) /* promicuous */
103 1.1 tsutsui #define OMR_TEN (1U<<13) /* instruct start/stop Tx */
104 1.1 tsutsui #define OMR_REN (1U<< 1) /* instruct start/stop Rx */
105 1.1 tsutsui #define OMR_FD (1U<< 9) /* FDX */
106 1.1 tsutsui #define TLP_IEN 0x38 /* 7: interrupt enable mask */
107 1.1 tsutsui #define TLP_APROM 0x048 /* 9: SEEPROM and MII management */
108 1.1 tsutsui #define SROM_RD (1U <<14) /* read operation */
109 1.1 tsutsui #define SROM_WR (1U <<13) /* write openration */
110 1.1 tsutsui #define SROM_SR (1U <<11) /* SEEPROM select */
111 1.1 tsutsui #define TLP_CSR12 0x60 /* SIA status */
112 1.1 tsutsui
113 1.1 tsutsui #define TLP_CSR15 0x78 /* SIA general register */
114 1.1 tsutsui #define SIAGEN_MD0 (1U<<16)
115 1.1 tsutsui #define SIAGEN_CWE (1U<<28)
116 1.1 tsutsui
117 1.1 tsutsui #define FRAMESIZE 1536
118 1.1 tsutsui #define BUFSIZE 2048
119 1.1 tsutsui #define NRXBUF 2
120 1.1 tsutsui #define NEXT_RXBUF(x) (((x) + 1) & (NRXBUF - 1))
121 1.1 tsutsui
122 1.1 tsutsui struct local {
123 1.1 tsutsui struct desc TxD;
124 1.1 tsutsui #if CACHELINESIZE > 16
125 1.1 tsutsui uint8_t pad[CACHELINESIZE - sizeof(struct desc)];
126 1.1 tsutsui #endif
127 1.1 tsutsui struct desc RxD[NRXBUF];
128 1.1 tsutsui uint8_t txstore[BUFSIZE];
129 1.1 tsutsui uint8_t rxstore[NRXBUF][BUFSIZE];
130 1.1 tsutsui uint32_t csr, omr;
131 1.1 tsutsui u_int rx;
132 1.1 tsutsui u_int sromsft;
133 1.1 tsutsui u_int phy;
134 1.1 tsutsui uint32_t bmsr, anlpar;
135 1.1 tsutsui };
136 1.1 tsutsui
137 1.1 tsutsui #define COBALT_TLP0_BASE 0x10100000
138 1.1 tsutsui #define SROM_MAC_OFFSET 0
139 1.1 tsutsui
140 1.1 tsutsui static void size_srom(struct local *);
141 1.1 tsutsui static u_int read_srom(struct local *, int);
142 1.1 tsutsui #if 0
143 1.1 tsutsui static u_int tlp_mii_read(struct local *, int, int);
144 1.1 tsutsui static void tlp_mii_write(struct local *, int, int, int);
145 1.1 tsutsui static void mii_initphy(struct local *);
146 1.1 tsutsui #endif
147 1.1 tsutsui
148 1.1 tsutsui void *
149 1.1 tsutsui tlp_init(void *cookie)
150 1.1 tsutsui {
151 1.1 tsutsui uint32_t val;
152 1.1 tsutsui struct local *l;
153 1.1 tsutsui struct desc *TxD, *RxD;
154 1.1 tsutsui uint8_t *en;
155 1.1 tsutsui int i;
156 1.1 tsutsui
157 1.1 tsutsui l = ALLOC(struct local, CACHELINESIZE);
158 1.1 tsutsui memset(l, 0, sizeof(struct local));
159 1.1 tsutsui
160 1.1 tsutsui DPRINTF(("tlp: l = %p, TxD = %p, RxD[0] = %p, RxD[1] = %p\n",
161 1.1 tsutsui l, &l->TxD, &l->RxD[0], &l->RxD[1]));
162 1.1 tsutsui DPRINTF(("tlp: txstore = %p, rxstore[0] = %p, rxstore[1] = %p\n",
163 1.1 tsutsui l->txstore, l->rxstore[0], l->rxstore[1]));
164 1.1 tsutsui
165 1.1 tsutsui #if 0
166 1.1 tsutsui /* XXX assume tlp0 at pci0 dev 7 function 0 */
167 1.1 tsutsui tag = (0 << 16) | ( 7 << 11) | (0 << 8);
168 1.1 tsutsui /* memory map is not initialized by the firmware on cobalt */
169 1.1 tsutsui l->csr = MIPS_PHYS_TO_KSEG1(pcicfgread(tag, 0x10) & 0xfffffffc);
170 1.1 tsutsui DPRINTF(("%s: CSR = 0x%x\n", __func__, l->csr));
171 1.1 tsutsui #else
172 1.1 tsutsui l->csr = MIPS_PHYS_TO_KSEG1(COBALT_TLP0_BASE);
173 1.1 tsutsui #endif
174 1.1 tsutsui
175 1.1 tsutsui val = CSR_READ(l, TLP_BMR);
176 1.1 tsutsui CSR_WRITE(l, TLP_BMR, val | BMR_RST);
177 1.1 tsutsui DELAY(1000);
178 1.1 tsutsui CSR_WRITE(l, TLP_BMR, val);
179 1.1 tsutsui DELAY(1000);
180 1.1 tsutsui (void)CSR_READ(l, TLP_BMR);
181 1.1 tsutsui
182 1.1 tsutsui l->omr = OMR_PS | OMR_SDP;
183 1.1 tsutsui CSR_WRITE(l, TLP_OMR, l->omr);
184 1.1 tsutsui CSR_WRITE(l, TLP_STS, ~0);
185 1.1 tsutsui CSR_WRITE(l, TLP_IEN, 0);
186 1.1 tsutsui
187 1.1 tsutsui #if 0
188 1.1 tsutsui mii_initphy(l);
189 1.1 tsutsui #endif
190 1.1 tsutsui size_srom(l);
191 1.1 tsutsui
192 1.1 tsutsui en = cookie;
193 1.1 tsutsui /* MAC address is stored at offset 0 in SROM on cobalt */
194 1.1 tsutsui val = read_srom(l, SROM_MAC_OFFSET / 2 + 0);
195 1.1 tsutsui en[0] = val;
196 1.1 tsutsui en[1] = val >> 8;
197 1.1 tsutsui val = read_srom(l, SROM_MAC_OFFSET / 2 + 1);
198 1.1 tsutsui en[2] = val;
199 1.1 tsutsui en[3] = val >> 8;
200 1.1 tsutsui val = read_srom(l, SROM_MAC_OFFSET / 2 + 2);
201 1.1 tsutsui en[4] = val;
202 1.1 tsutsui en[5] = val >> 8;
203 1.1 tsutsui
204 1.1 tsutsui DPRINTF(("tlp: MAC address %x:%x:%x:%x:%x:%x\n",
205 1.1 tsutsui en[0], en[1], en[2], en[3], en[4], en[5]));
206 1.1 tsutsui
207 1.1 tsutsui RxD = &l->RxD[0];
208 1.1 tsutsui for (i = 0; i < NRXBUF; i++) {
209 1.1 tsutsui RxD[i].xd3 = htole32(VTOPHYS(&RxD[NEXT_RXBUF(i)]));
210 1.1 tsutsui RxD[i].xd2 = htole32(VTOPHYS(l->rxstore[i]));
211 1.1 tsutsui RxD[i].xd1 = htole32(FRAMESIZE);
212 1.1 tsutsui RxD[i].xd0 = htole32(R0_OWN|R0_FS|R0_LS);
213 1.1 tsutsui }
214 1.1 tsutsui RxD[NRXBUF - 1].xd1 |= htole32(R1_RER);
215 1.1 tsutsui CSR_WRITE(l, TLP_RRBA, VTOPHYS(RxD));
216 1.1 tsutsui
217 1.1 tsutsui /* "setup packet" to have own station address */
218 1.1 tsutsui TxD = &l->TxD;
219 1.1 tsutsui TxD->xd3 = htole32(VTOPHYS(TxD));
220 1.1 tsutsui TxD->xd2 = htole32(VTOPHYS(l->txstore));
221 1.1 tsutsui TxD->xd1 = htole32(T1_SET | T1_TER);
222 1.1 tsutsui TxD->xd0 = htole32(0);
223 1.1 tsutsui CSR_WRITE(l, TLP_TRBA, VTOPHYS(TxD));
224 1.1 tsutsui
225 1.1 tsutsui memset(l->txstore, 0, FRAMESIZE);
226 1.1 tsutsui
227 1.1 tsutsui /* make sure the entire descriptors transfered to memory */
228 1.1 tsutsui wbinv(l, sizeof(struct local));
229 1.1 tsutsui
230 1.1 tsutsui l->rx = 0;
231 1.1 tsutsui l->omr |= OMR_FD | OMR_TEN | OMR_REN;
232 1.1 tsutsui
233 1.1 tsutsui #if 1
234 1.1 tsutsui /* reset PHY (cobalt quirk from if_tlp_pci.c) */
235 1.1 tsutsui CSR_WRITE(l, TLP_CSR15, SIAGEN_CWE | SIAGEN_MD0);
236 1.1 tsutsui DELAY(10);
237 1.1 tsutsui CSR_WRITE(l, TLP_CSR15, SIAGEN_CWE);
238 1.1 tsutsui DELAY(10);
239 1.1 tsutsui #endif
240 1.1 tsutsui
241 1.1 tsutsui /* start Tx/Rx */
242 1.1 tsutsui CSR_WRITE(l, TLP_OMR, l->omr);
243 1.1 tsutsui #if 0
244 1.1 tsutsui CSR_WRITE(l, TLP_TPD, TPD_POLL);
245 1.1 tsutsui #endif
246 1.1 tsutsui CSR_WRITE(l, TLP_RPD, RPD_POLL);
247 1.1 tsutsui
248 1.1 tsutsui return l;
249 1.1 tsutsui }
250 1.1 tsutsui
251 1.1 tsutsui int
252 1.1 tsutsui tlp_send(void *dev, char *buf, u_int len)
253 1.1 tsutsui {
254 1.1 tsutsui struct local *l = dev;
255 1.1 tsutsui struct desc *TxD;
256 1.1 tsutsui u_int loop;
257 1.1 tsutsui
258 1.1 tsutsui #if 0 /* unaligned TX buf might be problematic? */
259 1.1 tsutsui wb(buf, len);
260 1.1 tsutsui TxD = &l->TxD;
261 1.1 tsutsui TxD->xd3 = htole32(VTOPHYS(TxD));
262 1.1 tsutsui TxD->xd2 = htole32(VTOPHYS(buf));
263 1.1 tsutsui TxD->xd1 = htole32(T1_FS | T1_LS | T1_TER | (len & T1_TBS_MASK));
264 1.1 tsutsui #else
265 1.1 tsutsui memcpy(l->txstore, buf, len);
266 1.1 tsutsui wb(l->txstore, len);
267 1.1 tsutsui TxD = &l->TxD;
268 1.1 tsutsui TxD->xd3 = htole32(VTOPHYS(TxD));
269 1.1 tsutsui TxD->xd2 = htole32(VTOPHYS(l->txstore));
270 1.1 tsutsui TxD->xd1 = htole32(T1_FS | T1_LS | T1_TER | (len & T1_TBS_MASK));
271 1.1 tsutsui #endif
272 1.1 tsutsui TxD->xd0 = htole32(T0_OWN);
273 1.1 tsutsui wbinv(TxD, sizeof(struct desc));
274 1.1 tsutsui CSR_WRITE(l, TLP_TPD, TPD_POLL);
275 1.1 tsutsui loop = 100;
276 1.1 tsutsui do {
277 1.1 tsutsui if ((le32toh(TxD->xd0) & T0_OWN) == 0)
278 1.1 tsutsui goto done;
279 1.1 tsutsui inv(TxD, sizeof(struct desc));
280 1.1 tsutsui DELAY(10);
281 1.1 tsutsui } while (--loop > 0);
282 1.1 tsutsui printf("xmit failed\n");
283 1.1 tsutsui return -1;
284 1.1 tsutsui done:
285 1.1 tsutsui return len;
286 1.1 tsutsui }
287 1.1 tsutsui
288 1.1 tsutsui int
289 1.1 tsutsui tlp_recv(void *dev, char *buf, u_int maxlen, u_int timo)
290 1.1 tsutsui {
291 1.1 tsutsui struct local *l = dev;
292 1.1 tsutsui struct desc *RxD;
293 1.1 tsutsui u_int bound, len;
294 1.1 tsutsui uint32_t rxstat;
295 1.1 tsutsui uint8_t *ptr;
296 1.1 tsutsui
297 1.1 tsutsui bound = 1000 * timo;
298 1.1 tsutsui
299 1.1 tsutsui again:
300 1.1 tsutsui RxD = &l->RxD[l->rx];
301 1.1 tsutsui do {
302 1.1 tsutsui rxstat = le32toh(RxD->xd0);
303 1.1 tsutsui inv(RxD, sizeof(struct desc));
304 1.1 tsutsui if ((rxstat & R0_OWN) == 0)
305 1.1 tsutsui goto gotone;
306 1.1 tsutsui DELAY(1000); /* 1 milli second */
307 1.1 tsutsui } while (--bound > 0);
308 1.1 tsutsui errno = 0;
309 1.1 tsutsui CSR_WRITE(l, TLP_RPD, RPD_POLL);
310 1.1 tsutsui return -1;
311 1.1 tsutsui gotone:
312 1.1 tsutsui if (rxstat & R0_ES) {
313 1.1 tsutsui RxD->xd0 = htole32(R0_OWN|R0_FS|R0_LS);
314 1.1 tsutsui wbinv(RxD, sizeof(struct desc));
315 1.1 tsutsui l->rx = NEXT_RXBUF(l->rx);
316 1.1 tsutsui CSR_WRITE(l, TLP_RPD, RPD_POLL);
317 1.1 tsutsui goto again;
318 1.1 tsutsui }
319 1.1 tsutsui /* good frame */
320 1.1 tsutsui len = ((rxstat & R0_FL_MASK) >> 16) - 4; /* HASFCS */
321 1.1 tsutsui if (len > maxlen)
322 1.1 tsutsui len = maxlen;
323 1.1 tsutsui ptr = l->rxstore[l->rx];
324 1.1 tsutsui memcpy(buf, ptr, len);
325 1.1 tsutsui inv(ptr, FRAMESIZE);
326 1.1 tsutsui RxD->xd0 = htole32(R0_OWN|R0_FS|R0_LS);
327 1.1 tsutsui wbinv(RxD, sizeof(struct desc));
328 1.1 tsutsui l->rx = NEXT_RXBUF(l->rx);
329 1.1 tsutsui CSR_WRITE(l, TLP_OMR, l->omr); /* necessary? */
330 1.1 tsutsui return len;
331 1.1 tsutsui }
332 1.1 tsutsui
333 1.1 tsutsui static void
334 1.1 tsutsui size_srom(struct local *l)
335 1.1 tsutsui {
336 1.1 tsutsui /* determine 8/6 bit addressing SEEPROM */
337 1.1 tsutsui l->sromsft = 8;
338 1.1 tsutsui l->sromsft = (read_srom(l, 255) & 0x40000) ? 8 : 6;
339 1.1 tsutsui }
340 1.1 tsutsui
341 1.1 tsutsui /*
342 1.1 tsutsui * bare SEEPROM access with bitbang'ing
343 1.1 tsutsui */
344 1.1 tsutsui #define R110 6 /* SEEPROM read op */
345 1.1 tsutsui #define CS (1U << 0) /* hold chip select */
346 1.1 tsutsui #define CLK (1U << 1) /* clk bit */
347 1.1 tsutsui #define D1 (1U << 2) /* bit existence */
348 1.1 tsutsui #define D0 0 /* bit absence */
349 1.1 tsutsui #define VV (1U << 3) /* taken 0/1 from SEEPROM */
350 1.1 tsutsui
351 1.1 tsutsui static u_int
352 1.1 tsutsui read_srom(struct local *l, int off)
353 1.1 tsutsui {
354 1.1 tsutsui u_int idx, cnt, ret;
355 1.1 tsutsui uint32_t val, x1, x0, bit;
356 1.1 tsutsui
357 1.1 tsutsui idx = off & 0xff; /* A7-A0 */
358 1.1 tsutsui idx |= R110 << l->sromsft; /* 110 for READ */
359 1.1 tsutsui
360 1.1 tsutsui val = SROM_RD | SROM_SR;
361 1.1 tsutsui CSR_WRITE(l, TLP_APROM, val);
362 1.1 tsutsui val |= CS; /* hold CS */
363 1.1 tsutsui CSR_WRITE(l, TLP_APROM, val);
364 1.1 tsutsui
365 1.1 tsutsui x1 = val | D1; /* 1 */
366 1.1 tsutsui x0 = val | D0; /* 0 */
367 1.1 tsutsui /* instruct R110 op. at off in MSB first order */
368 1.1 tsutsui for (cnt = (1 << (l->sromsft + 2)); cnt > 0; cnt >>= 1) {
369 1.1 tsutsui bit = (idx & cnt) ? x1 : x0;
370 1.1 tsutsui CSR_WRITE(l, TLP_APROM, bit);
371 1.1 tsutsui DELAY(10);
372 1.1 tsutsui CSR_WRITE(l, TLP_APROM, bit | CLK);
373 1.1 tsutsui DELAY(10);
374 1.1 tsutsui }
375 1.1 tsutsui /* read 16bit quantity in MSB first order */
376 1.1 tsutsui ret = 0;
377 1.1 tsutsui for (cnt = 16; cnt > 0; cnt--) {
378 1.1 tsutsui CSR_WRITE(l, TLP_APROM, val);
379 1.1 tsutsui DELAY(10);
380 1.1 tsutsui CSR_WRITE(l, TLP_APROM, val | CLK);
381 1.1 tsutsui DELAY(10);
382 1.1 tsutsui ret = (ret << 1) | !!(CSR_READ(l, TLP_APROM) & VV);
383 1.1 tsutsui }
384 1.1 tsutsui val &= ~CS; /* turn off chip select */
385 1.1 tsutsui CSR_WRITE(l, TLP_APROM, val);
386 1.1 tsutsui
387 1.1 tsutsui return ret;
388 1.1 tsutsui }
389 1.1 tsutsui
390 1.1 tsutsui #if 0
391 1.1 tsutsui
392 1.1 tsutsui static u_int
393 1.1 tsutsui tlp_mii_read(struct local *l, int phy, int reg)
394 1.1 tsutsui {
395 1.1 tsutsui /* later ... */
396 1.1 tsutsui return 0;
397 1.1 tsutsui }
398 1.1 tsutsui
399 1.1 tsutsui static void
400 1.1 tsutsui tlp_mii_write(struct local *l, int phy, int reg, int val)
401 1.1 tsutsui {
402 1.1 tsutsui /* later ... */
403 1.1 tsutsui }
404 1.1 tsutsui
405 1.1 tsutsui #define MII_BMCR 0x00 /* Basic mode control register (rw) */
406 1.1 tsutsui #define BMCR_RESET 0x8000 /* reset */
407 1.1 tsutsui #define BMCR_AUTOEN 0x1000 /* autonegotiation enable */
408 1.1 tsutsui #define BMCR_ISO 0x0400 /* isolate */
409 1.1 tsutsui #define BMCR_STARTNEG 0x0200 /* restart autonegotiation */
410 1.1 tsutsui #define MII_BMSR 0x01 /* Basic mode status register (ro) */
411 1.1 tsutsui
412 1.1 tsutsui static void
413 1.1 tsutsui mii_initphy(struct local *l)
414 1.1 tsutsui {
415 1.1 tsutsui int phy, bound;
416 1.1 tsutsui uint32_t ctl, sts;
417 1.1 tsutsui
418 1.1 tsutsui for (phy = 0; phy < 32; phy++) {
419 1.1 tsutsui ctl = tlp_mii_read(l, phy, MII_BMCR);
420 1.1 tsutsui sts = tlp_mii_read(l, phy, MII_BMSR);
421 1.1 tsutsui if (ctl != 0xffff && sts != 0xffff)
422 1.1 tsutsui goto found;
423 1.1 tsutsui }
424 1.1 tsutsui printf("MII: no PHY found\n");
425 1.1 tsutsui return;
426 1.1 tsutsui found:
427 1.1 tsutsui ctl = tlp_mii_read(l, phy, MII_BMCR);
428 1.1 tsutsui tlp_mii_write(l, phy, MII_BMCR, ctl | BMCR_RESET);
429 1.1 tsutsui bound = 100;
430 1.1 tsutsui do {
431 1.1 tsutsui DELAY(10);
432 1.1 tsutsui ctl = tlp_mii_read(l, phy, MII_BMCR);
433 1.1 tsutsui if (ctl == 0xffff) {
434 1.1 tsutsui printf("MII: PHY %d has died after reset\n", phy);
435 1.1 tsutsui return;
436 1.1 tsutsui }
437 1.1 tsutsui } while (bound-- > 0 && (ctl & BMCR_RESET));
438 1.1 tsutsui if (bound == 0) {
439 1.1 tsutsui printf("PHY %d reset failed\n", phy);
440 1.1 tsutsui }
441 1.1 tsutsui ctl &= ~BMCR_ISO;
442 1.1 tsutsui tlp_mii_write(l, phy, MII_BMCR, ctl);
443 1.1 tsutsui sts = tlp_mii_read(l, phy, MII_BMSR) |
444 1.1 tsutsui tlp_mii_read(l, phy, MII_BMSR); /* read twice */
445 1.1 tsutsui l->phy = phy;
446 1.1 tsutsui l->bmsr = sts;
447 1.1 tsutsui }
448 1.1 tsutsui
449 1.1 tsutsui static void
450 1.1 tsutsui mii_dealan(struct local *, u_int timo)
451 1.1 tsutsui {
452 1.1 tsutsui uint32_t anar;
453 1.1 tsutsui u_int bound;
454 1.1 tsutsui
455 1.1 tsutsui anar = ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA;
456 1.1 tsutsui tlp_mii_write(l, l->phy, MII_ANAR, anar);
457 1.1 tsutsui tlp_mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
458 1.1 tsutsui l->anlpar = 0;
459 1.1 tsutsui bound = getsecs() + timo;
460 1.1 tsutsui do {
461 1.1 tsutsui l->bmsr = tlp_mii_read(l, l->phy, MII_BMSR) |
462 1.1 tsutsui tlp_mii_read(l, l->phy, MII_BMSR); /* read twice */
463 1.1 tsutsui if ((l->bmsr & BMSR_LINK) && (l->bmsr & BMSR_ACOMP)) {
464 1.1 tsutsui l->anlpar = tlp_mii_read(l, l->phy, MII_ANLPAR);
465 1.1 tsutsui break;
466 1.1 tsutsui }
467 1.1 tsutsui DELAY(10 * 1000);
468 1.1 tsutsui } while (getsecs() < bound);
469 1.1 tsutsui return;
470 1.1 tsutsui }
471 1.1 tsutsui #endif
472