skg.c revision 1.2.2.3 1 1.2.2.2 rmind /* $NetBSD: skg.c,v 1.2.2.3 2011/05/31 03:04:16 rmind Exp $ */
2 1.2.2.2 rmind
3 1.2.2.2 rmind /*-
4 1.2.2.2 rmind * Copyright (c) 2010 Frank Wille.
5 1.2.2.2 rmind * All rights reserved.
6 1.2.2.2 rmind *
7 1.2.2.2 rmind * Written by Frank Wille for The NetBSD Project.
8 1.2.2.2 rmind *
9 1.2.2.2 rmind * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 rmind * modification, are permitted provided that the following conditions
11 1.2.2.2 rmind * are met:
12 1.2.2.2 rmind * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 rmind * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 rmind * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 rmind * documentation and/or other materials provided with the distribution.
17 1.2.2.2 rmind *
18 1.2.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.2.2.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.2.2.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.2.2.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.2.2.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.2.2.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.2.2.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.2.2.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.2.2.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.2.2.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.2.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
29 1.2.2.2 rmind */
30 1.2.2.2 rmind
31 1.2.2.2 rmind #include <sys/param.h>
32 1.2.2.2 rmind
33 1.2.2.2 rmind #include <netinet/in.h>
34 1.2.2.2 rmind #include <netinet/in_systm.h>
35 1.2.2.2 rmind
36 1.2.2.2 rmind #include <lib/libsa/stand.h>
37 1.2.2.2 rmind #include <lib/libsa/net.h>
38 1.2.2.2 rmind
39 1.2.2.2 rmind #include "globals.h"
40 1.2.2.2 rmind
41 1.2.2.2 rmind /*
42 1.2.2.2 rmind * - reverse endian access every CSR.
43 1.2.2.2 rmind * - no vtophys() translation, vaddr_t == paddr_t.
44 1.2.2.2 rmind * - PIPT writeback cache aware.
45 1.2.2.2 rmind */
46 1.2.2.2 rmind #define CSR_WRITE_1(l, r, v) *(volatile uint8_t *)((l)->csr+(r)) = (v)
47 1.2.2.2 rmind #define CSR_READ_1(l, r) *(volatile uint8_t *)((l)->csr+(r))
48 1.2.2.2 rmind #define CSR_WRITE_2(l, r, v) out16rb((l)->csr+(r), (v))
49 1.2.2.2 rmind #define CSR_READ_2(l, r) in16rb((l)->csr+(r))
50 1.2.2.2 rmind #define CSR_WRITE_4(l, r, v) out32rb((l)->csr+(r), (v))
51 1.2.2.2 rmind #define CSR_READ_4(l, r) in32rb((l)->csr+(r))
52 1.2.2.2 rmind #define VTOPHYS(va) (uint32_t)(va)
53 1.2.2.2 rmind #define DEVTOV(pa) (uint32_t)(pa)
54 1.2.2.2 rmind #define wbinv(adr, siz) _wbinv(VTOPHYS(adr), (uint32_t)(siz))
55 1.2.2.2 rmind #define inv(adr, siz) _inv(VTOPHYS(adr), (uint32_t)(siz))
56 1.2.2.2 rmind #define DELAY(n) delay(n)
57 1.2.2.2 rmind #define ALLOC(T,A) (T *)allocaligned(sizeof(T),(A))
58 1.2.2.2 rmind
59 1.2.2.2 rmind struct desc {
60 1.2.2.2 rmind uint32_t xd0, xd1, xd2, xd3, xd4;
61 1.2.2.2 rmind uint32_t rsrvd[3];
62 1.2.2.2 rmind };
63 1.2.2.2 rmind #define CTL_LS 0x20000000
64 1.2.2.2 rmind #define CTL_FS 0x40000000
65 1.2.2.2 rmind #define CTL_OWN 0x80000000
66 1.2.2.2 rmind #define CTL_DEFOPC 0x00550000
67 1.2.2.2 rmind #define FRAMEMASK 0x0000ffff
68 1.2.2.2 rmind #define RXSTAT_RXOK 0x00000100
69 1.2.2.2 rmind
70 1.2.2.2 rmind #define SK_CSR 0x0004
71 1.2.2.2 rmind #define CSR_SW_RESET 0x0001
72 1.2.2.2 rmind #define CSR_SW_UNRESET 0x0002
73 1.2.2.2 rmind #define CSR_MASTER_RESET 0x0004
74 1.2.2.2 rmind #define CSR_MASTER_UNRESET 0x0008
75 1.2.2.2 rmind #define SK_IMR 0x000c
76 1.2.2.2 rmind #define SK_BMU_RX_CSR0 0x0060
77 1.2.2.2 rmind #define SK_BMU_TXS_CSR0 0x0068
78 1.2.2.2 rmind #define SK_MAC0 0x0100
79 1.2.2.2 rmind #define SK_MAC1 0x0108
80 1.2.2.2 rmind #define SK_GPIO 0x015c
81 1.2.2.2 rmind #define SK_RAMCTL 0x01a0
82 1.2.2.2 rmind #define SK_TXAR1_COUNTERCTL 0x0210
83 1.2.2.2 rmind #define TXARCTL_ON 0x02
84 1.2.2.2 rmind #define TXARCTL_FSYNC_ON 0x80
85 1.2.2.2 rmind #define SK_RXQ1_CURADDR_LO 0x0420
86 1.2.2.2 rmind #define SK_RXQ1_CURADDR_HI 0x0424
87 1.2.2.2 rmind #define SK_RXQ1_BMU_CSR 0x0434
88 1.2.2.2 rmind #define RXBMU_CLR_IRQ_EOF 0x00000002
89 1.2.2.2 rmind #define RXBMU_RX_START 0x00000010
90 1.2.2.2 rmind #define RXBMU_RX_STOP 0x00000020
91 1.2.2.2 rmind #define RXBMU_POLL_ON 0x00000080
92 1.2.2.2 rmind #define RXBMU_TRANSFER_SM_UNRESET 0x00000200
93 1.2.2.2 rmind #define RXBMU_DESCWR_SM_UNRESET 0x00000800
94 1.2.2.2 rmind #define RXBMU_DESCRD_SM_UNRESET 0x00002000
95 1.2.2.2 rmind #define RXBMU_SUPERVISOR_SM_UNRESET 0x00008000
96 1.2.2.2 rmind #define RXBMU_PFI_SM_UNRESET 0x00020000
97 1.2.2.2 rmind #define RXBMU_FIFO_UNRESET 0x00080000
98 1.2.2.2 rmind #define RXBMU_DESC_UNRESET 0x00200000
99 1.2.2.2 rmind #define SK_TXQS1_CURADDR_LO 0x0620
100 1.2.2.2 rmind #define SK_TXQS1_CURADDR_HI 0x0624
101 1.2.2.2 rmind #define SK_TXQS1_BMU_CSR 0x0634
102 1.2.2.2 rmind #define TXBMU_CLR_IRQ_EOF 0x00000002
103 1.2.2.2 rmind #define TXBMU_TX_START 0x00000010
104 1.2.2.2 rmind #define TXBMU_TX_STOP 0x00000020
105 1.2.2.2 rmind #define TXBMU_POLL_ON 0x00000080
106 1.2.2.2 rmind #define TXBMU_TRANSFER_SM_UNRESET 0x00000200
107 1.2.2.2 rmind #define TXBMU_DESCWR_SM_UNRESET 0x00000800
108 1.2.2.2 rmind #define TXBMU_DESCRD_SM_UNRESET 0x00002000
109 1.2.2.2 rmind #define TXBMU_SUPERVISOR_SM_UNRESET 0x00008000
110 1.2.2.2 rmind #define TXBMU_PFI_SM_UNRESET 0x00020000
111 1.2.2.2 rmind #define TXBMU_FIFO_UNRESET 0x00080000
112 1.2.2.2 rmind #define TXBMU_DESC_UNRESET 0x00200000
113 1.2.2.2 rmind #define SK_RXRB1_START 0x0800
114 1.2.2.2 rmind #define SK_RXRB1_END 0x0804
115 1.2.2.2 rmind #define SK_RXRB1_WR_PTR 0x0808
116 1.2.2.2 rmind #define SK_RXRB1_RD_PTR 0x080c
117 1.2.2.2 rmind #define SK_RXRB1_CTLTST 0x0828
118 1.2.2.2 rmind #define RBCTL_UNRESET 0x02
119 1.2.2.2 rmind #define RBCTL_ON 0x08
120 1.2.2.2 rmind #define RBCTL_STORENFWD_ON 0x20
121 1.2.2.2 rmind #define SK_TXRBS1_START 0x0a00
122 1.2.2.2 rmind #define SK_TXRBS1_END 0x0a04
123 1.2.2.2 rmind #define SK_TXRBS1_WR_PTR 0x0a08
124 1.2.2.2 rmind #define SK_TXRBS1_RD_PTR 0x0a0c
125 1.2.2.2 rmind #define SK_TXRBS1_CTLTST 0x0a28
126 1.2.2.2 rmind #define SK_RXMF1_CTRL_TEST 0x0c48
127 1.2.2.2 rmind #define RFCTL_OPERATION_ON 0x00000008
128 1.2.2.2 rmind #define RFCTL_RESET_CLEAR 0x00000002
129 1.2.2.2 rmind #define SK_TXMF1_CTRL_TEST 0x0D48
130 1.2.2.2 rmind #define TFCTL_OPERATION_ON 0x00000008
131 1.2.2.2 rmind #define TFCTL_RESET_CLEAR 0x00000002
132 1.2.2.2 rmind #define SK_GMAC_CTRL 0x0f00
133 1.2.2.2 rmind #define GMAC_LOOP_OFF 0x00000010
134 1.2.2.2 rmind #define GMAC_PAUSE_ON 0x00000008
135 1.2.2.2 rmind #define GMAC_RESET_CLEAR 0x00000002
136 1.2.2.2 rmind #define GMAC_RESET_SET 0x00000001
137 1.2.2.2 rmind #define SK_GPHY_CTRL 0x0f04
138 1.2.2.2 rmind #define GPHY_INT_POL_HI 0x08000000
139 1.2.2.2 rmind #define GPHY_DIS_FC 0x02000000
140 1.2.2.2 rmind #define GPHY_DIS_SLEEP 0x01000000
141 1.2.2.2 rmind #define GPHY_ENA_XC 0x00040000
142 1.2.2.2 rmind #define GPHY_ENA_PAUSE 0x00002000
143 1.2.2.2 rmind #define GPHY_RESET_CLEAR 0x00000002
144 1.2.2.2 rmind #define GPHY_RESET_SET 0x00000001
145 1.2.2.2 rmind #define GPHY_ANEG_ALL 0x0009c000
146 1.2.2.2 rmind #define GPHY_COPPER 0x00f00000
147 1.2.2.2 rmind #define SK_LINK_CTRL 0x0f10
148 1.2.2.2 rmind #define LINK_RESET_CLEAR 0x0002
149 1.2.2.2 rmind #define LINK_RESET_SET 0x0001
150 1.2.2.2 rmind
151 1.2.2.2 rmind #define YUKON_GPCR 0x2804
152 1.2.2.2 rmind #define GPCR_TXEN 0x1000
153 1.2.2.2 rmind #define GPCR_RXEN 0x0800
154 1.2.2.2 rmind #define YUKON_SA1 0x281c
155 1.2.2.2 rmind #define YUKON_SA2 0x2828
156 1.2.2.2 rmind #define YUKON_SMICR 0x2880
157 1.2.2.2 rmind #define SMICR_PHYAD(x) (((x) & 0x1f) << 11)
158 1.2.2.2 rmind #define SMICR_REGAD(x) (((x) & 0x1f) << 6)
159 1.2.2.2 rmind #define SMICR_OP_READ 0x0020
160 1.2.2.2 rmind #define SMICR_OP_WRITE 0x0000
161 1.2.2.2 rmind #define SMICR_READ_VALID 0x0010
162 1.2.2.2 rmind #define SMICR_BUSY 0x0008
163 1.2.2.2 rmind #define YUKON_SMIDR 0x2884
164 1.2.2.2 rmind
165 1.2.2.2 rmind #define MII_PSSR 0x11 /* MAKPHY status register */
166 1.2.2.2 rmind #define PSSR_DUPLEX 0x2000 /* FDX */
167 1.2.2.2 rmind #define PSSR_RESOLVED 0x0800 /* speed and duplex resolved */
168 1.2.2.2 rmind #define PSSR_LINK 0x0400 /* link indication */
169 1.2.2.2 rmind #define PSSR_SPEED(x) (((x) >> 14) & 0x3)
170 1.2.2.2 rmind #define SPEED10 0
171 1.2.2.2 rmind #define SPEED100 1
172 1.2.2.2 rmind #define SPEED1000 2
173 1.2.2.2 rmind
174 1.2.2.2 rmind #define FRAMESIZE 1536
175 1.2.2.2 rmind
176 1.2.2.2 rmind struct local {
177 1.2.2.2 rmind struct desc txd[2];
178 1.2.2.2 rmind struct desc rxd[2];
179 1.2.2.2 rmind uint8_t rxstore[2][FRAMESIZE];
180 1.2.2.2 rmind unsigned csr, rx, tx, phy;
181 1.2.2.2 rmind uint16_t pssr, anlpar;
182 1.2.2.2 rmind };
183 1.2.2.2 rmind
184 1.2.2.2 rmind static int mii_read(struct local *, int, int);
185 1.2.2.2 rmind static void mii_write(struct local *, int, int, int);
186 1.2.2.2 rmind static void mii_initphy(struct local *);
187 1.2.2.2 rmind static void mii_dealan(struct local *, unsigned);
188 1.2.2.2 rmind
189 1.2.2.2 rmind int
190 1.2.2.2 rmind skg_match(unsigned tag, void *data)
191 1.2.2.2 rmind {
192 1.2.2.2 rmind unsigned v;
193 1.2.2.2 rmind
194 1.2.2.2 rmind v = pcicfgread(tag, PCI_ID_REG);
195 1.2.2.2 rmind switch (v) {
196 1.2.2.2 rmind case PCI_DEVICE(0x11ab, 0x4320):
197 1.2.2.2 rmind return 1;
198 1.2.2.2 rmind }
199 1.2.2.2 rmind return 0;
200 1.2.2.2 rmind }
201 1.2.2.2 rmind
202 1.2.2.2 rmind void *
203 1.2.2.2 rmind skg_init(unsigned tag, void *data)
204 1.2.2.2 rmind {
205 1.2.2.2 rmind struct local *l;
206 1.2.2.2 rmind struct desc *txd, *rxd;
207 1.2.2.2 rmind uint8_t *en;
208 1.2.2.2 rmind unsigned i;
209 1.2.2.2 rmind uint16_t reg;
210 1.2.2.2 rmind
211 1.2.2.2 rmind l = ALLOC(struct local, 32); /* desc alignment */
212 1.2.2.2 rmind memset(l, 0, sizeof(struct local));
213 1.2.2.2 rmind l->csr = DEVTOV(pcicfgread(tag, 0x10)); /* use mem space */
214 1.2.2.2 rmind
215 1.2.2.3 rmind /* make sure the descriptor bytes are not reversed */
216 1.2.2.3 rmind i = pcicfgread(tag, 0x44);
217 1.2.2.3 rmind pcicfgwrite(tag, 0x44, i & ~4);
218 1.2.2.3 rmind
219 1.2.2.2 rmind /* reset the chip */
220 1.2.2.2 rmind CSR_WRITE_2(l, SK_CSR, CSR_SW_RESET);
221 1.2.2.2 rmind CSR_WRITE_2(l, SK_CSR, CSR_MASTER_RESET);
222 1.2.2.2 rmind CSR_WRITE_2(l, SK_LINK_CTRL, LINK_RESET_SET);
223 1.2.2.2 rmind DELAY(1000);
224 1.2.2.2 rmind CSR_WRITE_2(l, SK_CSR, CSR_SW_UNRESET);
225 1.2.2.2 rmind DELAY(2);
226 1.2.2.2 rmind CSR_WRITE_2(l, SK_CSR, CSR_MASTER_UNRESET);
227 1.2.2.2 rmind CSR_WRITE_2(l, SK_LINK_CTRL, LINK_RESET_CLEAR);
228 1.2.2.2 rmind CSR_WRITE_4(l, SK_RAMCTL, 2); /* enable RAM interface */
229 1.2.2.2 rmind
230 1.2.2.2 rmind mii_initphy(l);
231 1.2.2.2 rmind
232 1.2.2.2 rmind /* read ethernet address */
233 1.2.2.2 rmind en = data;
234 1.2.2.2 rmind if (brdtype == BRD_SYNOLOGY)
235 1.2.2.2 rmind read_mac_from_flash(en);
236 1.2.2.2 rmind else
237 1.2.2.2 rmind for (i = 0; i < 6; i++)
238 1.2.2.2 rmind en[i] = CSR_READ_1(l, SK_MAC0 + i);
239 1.2.2.2 rmind printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
240 1.2.2.2 rmind en[0], en[1], en[2], en[3], en[4], en[5]);
241 1.2.2.2 rmind DPRINTF(("PHY %d (%04x.%04x)\n", l->phy,
242 1.2.2.2 rmind mii_read(l, l->phy, 2), mii_read(l, l->phy, 3)));
243 1.2.2.2 rmind
244 1.2.2.2 rmind /* set station address */
245 1.2.2.2 rmind for (i = 0; i < 3; i++)
246 1.2.2.2 rmind CSR_WRITE_2(l, YUKON_SA1 + i * 4,
247 1.2.2.2 rmind (en[i * 2] << 8) | en[i * 2 + 1]);
248 1.2.2.2 rmind
249 1.2.2.2 rmind /* configure RX and TX MAC FIFO */
250 1.2.2.2 rmind CSR_WRITE_1(l, SK_RXMF1_CTRL_TEST, RFCTL_RESET_CLEAR);
251 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXMF1_CTRL_TEST, RFCTL_OPERATION_ON);
252 1.2.2.2 rmind CSR_WRITE_1(l, SK_TXMF1_CTRL_TEST, TFCTL_RESET_CLEAR);
253 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXMF1_CTRL_TEST, TFCTL_OPERATION_ON);
254 1.2.2.2 rmind
255 1.2.2.2 rmind mii_dealan(l, 5);
256 1.2.2.2 rmind
257 1.2.2.2 rmind switch (PSSR_SPEED(l->pssr)) {
258 1.2.2.2 rmind case SPEED1000:
259 1.2.2.2 rmind printf("1000Mbps");
260 1.2.2.2 rmind break;
261 1.2.2.2 rmind case SPEED100:
262 1.2.2.2 rmind printf("100Mbps");
263 1.2.2.2 rmind break;
264 1.2.2.2 rmind case SPEED10:
265 1.2.2.2 rmind printf("10Mbps");
266 1.2.2.2 rmind break;
267 1.2.2.2 rmind }
268 1.2.2.2 rmind if (l->pssr & PSSR_DUPLEX)
269 1.2.2.2 rmind printf("-FDX");
270 1.2.2.2 rmind printf("\n");
271 1.2.2.2 rmind
272 1.2.2.2 rmind /* configure RAM buffers, assuming 64k RAM */
273 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_CTLTST, RBCTL_UNRESET);
274 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_START, 0);
275 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_WR_PTR, 0);
276 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_RD_PTR, 0);
277 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_END, 0xfff);
278 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXRB1_CTLTST, RBCTL_ON);
279 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_UNRESET);
280 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_STORENFWD_ON);
281 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_START, 0x1000);
282 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_WR_PTR, 0x1000);
283 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_RD_PTR, 0x1000);
284 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_END, 0x1fff);
285 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXRBS1_CTLTST, RBCTL_ON);
286 1.2.2.2 rmind
287 1.2.2.2 rmind /* setup descriptors and BMU */
288 1.2.2.2 rmind CSR_WRITE_1(l, SK_TXAR1_COUNTERCTL, TXARCTL_ON|TXARCTL_FSYNC_ON);
289 1.2.2.2 rmind
290 1.2.2.2 rmind txd = &l->txd[0];
291 1.2.2.2 rmind txd[0].xd1 = htole32(VTOPHYS(&txd[1]));
292 1.2.2.2 rmind txd[1].xd1 = htole32(VTOPHYS(&txd[0]));
293 1.2.2.2 rmind rxd = &l->rxd[0];
294 1.2.2.2 rmind rxd[0].xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
295 1.2.2.2 rmind rxd[0].xd1 = htole32(VTOPHYS(&rxd[1]));
296 1.2.2.2 rmind rxd[0].xd2 = htole32(VTOPHYS(l->rxstore[0]));
297 1.2.2.2 rmind rxd[1].xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
298 1.2.2.2 rmind rxd[1].xd1 = htole32(VTOPHYS(&rxd[0]));
299 1.2.2.2 rmind rxd[1].xd2 = htole32(VTOPHYS(l->rxstore[1]));
300 1.2.2.2 rmind wbinv(l, sizeof(struct local));
301 1.2.2.2 rmind
302 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXQ1_BMU_CSR,
303 1.2.2.2 rmind RXBMU_TRANSFER_SM_UNRESET|RXBMU_DESCWR_SM_UNRESET|
304 1.2.2.2 rmind RXBMU_DESCRD_SM_UNRESET|RXBMU_SUPERVISOR_SM_UNRESET|
305 1.2.2.2 rmind RXBMU_PFI_SM_UNRESET|RXBMU_FIFO_UNRESET|
306 1.2.2.2 rmind RXBMU_DESC_UNRESET);
307 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXQ1_CURADDR_LO, VTOPHYS(rxd));
308 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXQ1_CURADDR_HI, 0);
309 1.2.2.2 rmind
310 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXQS1_BMU_CSR,
311 1.2.2.2 rmind TXBMU_TRANSFER_SM_UNRESET|TXBMU_DESCWR_SM_UNRESET|
312 1.2.2.2 rmind TXBMU_DESCRD_SM_UNRESET|TXBMU_SUPERVISOR_SM_UNRESET|
313 1.2.2.2 rmind TXBMU_PFI_SM_UNRESET|TXBMU_FIFO_UNRESET|
314 1.2.2.2 rmind TXBMU_DESC_UNRESET|TXBMU_POLL_ON);
315 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXQS1_CURADDR_LO, VTOPHYS(txd));
316 1.2.2.2 rmind CSR_WRITE_4(l, SK_TXQS1_CURADDR_HI, 0);
317 1.2.2.2 rmind
318 1.2.2.2 rmind CSR_WRITE_4(l, SK_IMR, 0);
319 1.2.2.2 rmind CSR_WRITE_4(l, SK_RXQ1_BMU_CSR, RXBMU_RX_START);
320 1.2.2.2 rmind reg = CSR_READ_2(l, YUKON_GPCR);
321 1.2.2.2 rmind reg |= GPCR_TXEN | GPCR_RXEN;
322 1.2.2.2 rmind CSR_WRITE_2(l, YUKON_GPCR, reg);
323 1.2.2.2 rmind
324 1.2.2.2 rmind return l;
325 1.2.2.2 rmind }
326 1.2.2.2 rmind
327 1.2.2.2 rmind int
328 1.2.2.2 rmind skg_send(void *dev, char *buf, unsigned len)
329 1.2.2.2 rmind {
330 1.2.2.2 rmind struct local *l = dev;
331 1.2.2.2 rmind volatile struct desc *txd;
332 1.2.2.2 rmind unsigned loop;
333 1.2.2.2 rmind
334 1.2.2.2 rmind wbinv(buf, len);
335 1.2.2.2 rmind txd = &l->txd[l->tx];
336 1.2.2.2 rmind txd->xd2 = htole32(VTOPHYS(buf));
337 1.2.2.2 rmind txd->xd0 = htole32((len & FRAMEMASK)|CTL_DEFOPC|CTL_FS|CTL_LS|CTL_OWN);
338 1.2.2.2 rmind wbinv(txd, sizeof(struct desc));
339 1.2.2.2 rmind CSR_WRITE_4(l, SK_BMU_TXS_CSR0, TXBMU_TX_START);
340 1.2.2.2 rmind loop = 100;
341 1.2.2.2 rmind do {
342 1.2.2.2 rmind if ((le32toh(txd->xd0) & CTL_OWN) == 0)
343 1.2.2.2 rmind goto done;
344 1.2.2.2 rmind DELAY(10);
345 1.2.2.2 rmind inv(txd, sizeof(struct desc));
346 1.2.2.2 rmind } while (--loop > 0);
347 1.2.2.2 rmind printf("xmit failed\n");
348 1.2.2.2 rmind return -1;
349 1.2.2.2 rmind done:
350 1.2.2.2 rmind l->tx ^= 1;
351 1.2.2.2 rmind return len;
352 1.2.2.2 rmind }
353 1.2.2.2 rmind
354 1.2.2.2 rmind int
355 1.2.2.2 rmind skg_recv(void *dev, char *buf, unsigned maxlen, unsigned timo)
356 1.2.2.2 rmind {
357 1.2.2.2 rmind struct local *l = dev;
358 1.2.2.2 rmind volatile struct desc *rxd;
359 1.2.2.2 rmind unsigned bound, ctl, rxstat, len;
360 1.2.2.2 rmind uint8_t *ptr;
361 1.2.2.2 rmind
362 1.2.2.2 rmind bound = 1000 * timo;
363 1.2.2.2 rmind #if 0
364 1.2.2.2 rmind printf("recving with %u sec. timeout\n", timo);
365 1.2.2.2 rmind #endif
366 1.2.2.2 rmind again:
367 1.2.2.2 rmind rxd = &l->rxd[l->rx];
368 1.2.2.2 rmind do {
369 1.2.2.2 rmind inv(rxd, sizeof(struct desc));
370 1.2.2.2 rmind ctl = le32toh(rxd->xd0);
371 1.2.2.2 rmind if ((ctl & CTL_OWN) == 0)
372 1.2.2.2 rmind goto gotone;
373 1.2.2.2 rmind DELAY(1000); /* 1 milli second */
374 1.2.2.2 rmind } while (--bound > 0);
375 1.2.2.2 rmind errno = 0;
376 1.2.2.2 rmind return -1;
377 1.2.2.2 rmind gotone:
378 1.2.2.2 rmind rxstat = le32toh(rxd->xd4);
379 1.2.2.2 rmind if ((rxstat & RXSTAT_RXOK) == 0) {
380 1.2.2.2 rmind rxd->xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
381 1.2.2.2 rmind wbinv(rxd, sizeof(struct desc));
382 1.2.2.2 rmind l->rx ^= 1;
383 1.2.2.2 rmind goto again;
384 1.2.2.2 rmind }
385 1.2.2.2 rmind len = ctl & FRAMEMASK;
386 1.2.2.2 rmind if (len > maxlen)
387 1.2.2.2 rmind len = maxlen;
388 1.2.2.2 rmind ptr = l->rxstore[l->rx];
389 1.2.2.2 rmind inv(ptr, len);
390 1.2.2.2 rmind memcpy(buf, ptr, len);
391 1.2.2.2 rmind rxd->xd0 = htole32(FRAMESIZE|CTL_DEFOPC|CTL_LS|CTL_FS|CTL_OWN);
392 1.2.2.2 rmind wbinv(rxd, sizeof(struct desc));
393 1.2.2.2 rmind l->rx ^= 1;
394 1.2.2.2 rmind return len;
395 1.2.2.2 rmind }
396 1.2.2.2 rmind
397 1.2.2.2 rmind static int
398 1.2.2.2 rmind mii_read(struct local *l, int phy, int reg)
399 1.2.2.2 rmind {
400 1.2.2.2 rmind unsigned loop, v;
401 1.2.2.2 rmind
402 1.2.2.2 rmind CSR_WRITE_2(l, YUKON_SMICR, SMICR_PHYAD(phy) | SMICR_REGAD(reg) |
403 1.2.2.2 rmind SMICR_OP_READ);
404 1.2.2.2 rmind loop = 1000;
405 1.2.2.2 rmind do {
406 1.2.2.2 rmind DELAY(1);
407 1.2.2.2 rmind v = CSR_READ_2(l, YUKON_SMICR);
408 1.2.2.2 rmind } while ((v & SMICR_READ_VALID) == 0 && --loop);
409 1.2.2.2 rmind if (loop == 0) {
410 1.2.2.2 rmind printf("mii_read timeout!\n");
411 1.2.2.2 rmind return 0;
412 1.2.2.2 rmind }
413 1.2.2.2 rmind return CSR_READ_2(l, YUKON_SMIDR);
414 1.2.2.2 rmind }
415 1.2.2.2 rmind
416 1.2.2.2 rmind static void
417 1.2.2.2 rmind mii_write(struct local *l, int phy, int reg, int data)
418 1.2.2.2 rmind {
419 1.2.2.2 rmind unsigned loop, v;
420 1.2.2.2 rmind
421 1.2.2.2 rmind CSR_WRITE_2(l, YUKON_SMIDR, data);
422 1.2.2.2 rmind CSR_WRITE_2(l, YUKON_SMICR, SMICR_PHYAD(phy) | SMICR_REGAD(reg) |
423 1.2.2.2 rmind SMICR_OP_WRITE);
424 1.2.2.2 rmind loop = 1000;
425 1.2.2.2 rmind do {
426 1.2.2.2 rmind DELAY(1);
427 1.2.2.2 rmind v = CSR_READ_2(l, YUKON_SMICR);
428 1.2.2.2 rmind } while ((v & SMICR_BUSY) != 0 && --loop);
429 1.2.2.2 rmind if (loop == 0)
430 1.2.2.2 rmind printf("mii_write timeout!\n");
431 1.2.2.2 rmind }
432 1.2.2.2 rmind
433 1.2.2.2 rmind #define MII_BMCR 0x00 /* Basic mode control register (rw) */
434 1.2.2.2 rmind #define BMCR_AUTOEN 0x1000 /* autonegotiation enable */
435 1.2.2.2 rmind #define BMCR_STARTNEG 0x0200 /* restart autonegotiation */
436 1.2.2.2 rmind #define MII_BMSR 0x01 /* Basic mode status register (ro) */
437 1.2.2.2 rmind #define BMSR_ACOMP 0x0020 /* Autonegotiation complete */
438 1.2.2.2 rmind #define BMSR_LINK 0x0004 /* Link status */
439 1.2.2.2 rmind #define MII_ANAR 0x04 /* Autonegotiation advertisement (rw) */
440 1.2.2.2 rmind #define ANAR_FC 0x0400 /* local device supports PAUSE */
441 1.2.2.2 rmind #define ANAR_TX_FD 0x0100 /* local device supports 100bTx FD */
442 1.2.2.2 rmind #define ANAR_TX 0x0080 /* local device supports 100bTx */
443 1.2.2.2 rmind #define ANAR_10_FD 0x0040 /* local device supports 10bT FD */
444 1.2.2.2 rmind #define ANAR_10 0x0020 /* local device supports 10bT */
445 1.2.2.2 rmind #define ANAR_CSMA 0x0001 /* protocol selector CSMA/CD */
446 1.2.2.2 rmind #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
447 1.2.2.2 rmind #define MII_GTCR 0x09 /* 1000baseT control */
448 1.2.2.2 rmind #define GANA_1000TFDX 0x0200 /* advertise 1000baseT FDX */
449 1.2.2.2 rmind #define GANA_1000THDX 0x0100 /* advertise 1000baseT HDX */
450 1.2.2.2 rmind #define MII_GTSR 0x0a /* 1000baseT status */
451 1.2.2.2 rmind #define GLPA_1000TFDX 0x0800 /* link partner 1000baseT FDX capable */
452 1.2.2.2 rmind #define GLPA_1000THDX 0x0400 /* link partner 1000baseT HDX capable */
453 1.2.2.2 rmind
454 1.2.2.2 rmind static void
455 1.2.2.2 rmind mii_initphy(struct local *l)
456 1.2.2.2 rmind {
457 1.2.2.2 rmind unsigned val;
458 1.2.2.2 rmind
459 1.2.2.2 rmind l->phy = 0;
460 1.2.2.2 rmind
461 1.2.2.2 rmind /* take PHY out of reset */
462 1.2.2.2 rmind val = CSR_READ_4(l, SK_GPIO);
463 1.2.2.2 rmind CSR_WRITE_4(l, SK_GPIO, (val | 0x2000000) & ~0x200);
464 1.2.2.2 rmind
465 1.2.2.2 rmind /* GMAC and GPHY reset */
466 1.2.2.2 rmind CSR_WRITE_4(l, SK_GPHY_CTRL, GPHY_RESET_SET);
467 1.2.2.2 rmind CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_SET);
468 1.2.2.2 rmind DELAY(1000);
469 1.2.2.2 rmind CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_CLEAR);
470 1.2.2.2 rmind CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_RESET_SET);
471 1.2.2.2 rmind DELAY(1000);
472 1.2.2.2 rmind
473 1.2.2.2 rmind val = GPHY_INT_POL_HI | GPHY_DIS_FC | GPHY_DIS_SLEEP | GPHY_ENA_XC |
474 1.2.2.2 rmind GPHY_ANEG_ALL | GPHY_ENA_PAUSE | GPHY_COPPER;
475 1.2.2.2 rmind CSR_WRITE_4(l, SK_GPHY_CTRL, val | GPHY_RESET_SET);
476 1.2.2.2 rmind DELAY(1000);
477 1.2.2.2 rmind CSR_WRITE_4(l, SK_GPHY_CTRL, val | GPHY_RESET_CLEAR);
478 1.2.2.2 rmind CSR_WRITE_4(l, SK_GMAC_CTRL, GMAC_LOOP_OFF | GMAC_PAUSE_ON |
479 1.2.2.2 rmind GMAC_RESET_CLEAR);
480 1.2.2.2 rmind }
481 1.2.2.2 rmind
482 1.2.2.2 rmind static void
483 1.2.2.2 rmind mii_dealan(struct local *l, unsigned timo)
484 1.2.2.2 rmind {
485 1.2.2.2 rmind unsigned bmsr, bound;
486 1.2.2.2 rmind
487 1.2.2.2 rmind mii_write(l, l->phy, MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD |
488 1.2.2.2 rmind ANAR_10 | ANAR_CSMA | ANAR_FC);
489 1.2.2.2 rmind mii_write(l, l->phy, MII_GTCR, GANA_1000TFDX | GANA_1000THDX);
490 1.2.2.2 rmind mii_write(l, l->phy, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
491 1.2.2.2 rmind l->anlpar = 0;
492 1.2.2.2 rmind bound = getsecs() + timo;
493 1.2.2.2 rmind do {
494 1.2.2.2 rmind bmsr = mii_read(l, l->phy, MII_BMSR) |
495 1.2.2.2 rmind mii_read(l, l->phy, MII_BMSR); /* read twice */
496 1.2.2.2 rmind if ((bmsr & BMSR_LINK) && (bmsr & BMSR_ACOMP)) {
497 1.2.2.2 rmind l->pssr = mii_read(l, l->phy, MII_PSSR);
498 1.2.2.2 rmind l->anlpar = mii_read(l, l->phy, MII_ANLPAR);
499 1.2.2.2 rmind if ((l->pssr & PSSR_RESOLVED) == 0)
500 1.2.2.2 rmind continue;
501 1.2.2.2 rmind break;
502 1.2.2.2 rmind }
503 1.2.2.2 rmind DELAY(10 * 1000);
504 1.2.2.2 rmind } while (getsecs() < bound);
505 1.2.2.2 rmind }
506