emac3.c revision 1.1 1 1.1 uch /* $NetBSD: emac3.c,v 1.1 2001/10/16 15:38:33 uch Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.1 uch
39 1.1 uch /*
40 1.1 uch * EMAC3 (Ethernet Media Access Controller)
41 1.1 uch */
42 1.1 uch #include "debug_playstation2.h"
43 1.1 uch
44 1.1 uch #include <sys/param.h>
45 1.1 uch #include <sys/systm.h>
46 1.1 uch
47 1.1 uch #include <sys/socket.h>
48 1.1 uch
49 1.1 uch #include <net/if.h>
50 1.1 uch #include <net/if_ether.h>
51 1.1 uch #include <net/if_media.h>
52 1.1 uch
53 1.1 uch #include <dev/mii/mii.h>
54 1.1 uch #include <dev/mii/miivar.h>
55 1.1 uch
56 1.1 uch #include <playstation2/ee/eevar.h>
57 1.1 uch #include <playstation2/dev/emac3reg.h>
58 1.1 uch #include <playstation2/dev/emac3var.h>
59 1.1 uch
60 1.1 uch #ifdef EMAC3_DEBUG
61 1.1 uch #define STATIC
62 1.1 uch int emac3_debug = 0;
63 1.1 uch #define DPRINTF(fmt, args...) \
64 1.1 uch if (emac3_debug) \
65 1.1 uch printf("%s: " fmt, __FUNCTION__ , ##args)
66 1.1 uch #define DPRINTFN(n, arg) \
67 1.1 uch if (emac3_debug > (n)) \
68 1.1 uch n printf("%s: " fmt, __FUNCTION__ , ##args)
69 1.1 uch #else
70 1.1 uch #define STATIC static
71 1.1 uch #define DPRINTF(arg...) ((void)0)
72 1.1 uch #define DPRINTFN(n, arg...) ((void)0)
73 1.1 uch #endif
74 1.1 uch
75 1.1 uch /* SMAP specific EMAC3 define */
76 1.1 uch #define EMAC3_BASE MIPS_PHYS_TO_KSEG1(0x14002000)
77 1.1 uch static __inline__ u_int32_t
78 1.1 uch _emac3_reg_read_4(int ofs)
79 1.1 uch {
80 1.1 uch bus_addr_t a_ = EMAC3_BASE + ofs;
81 1.1 uch
82 1.1 uch return (_reg_read_2(a_) << 16) | _reg_read_2(a_ + 2);
83 1.1 uch }
84 1.1 uch
85 1.1 uch static __inline__ void
86 1.1 uch _emac3_reg_write_4(int ofs, u_int32_t v)
87 1.1 uch {
88 1.1 uch bus_addr_t a_ = EMAC3_BASE + ofs;
89 1.1 uch
90 1.1 uch _reg_write_2(a_, (v >> 16) & 0xffff);
91 1.1 uch _reg_write_2(a_ + 2, v & 0xffff);
92 1.1 uch }
93 1.1 uch
94 1.1 uch STATIC int emac3_phy_ready(void);
95 1.1 uch STATIC int emac3_soft_reset(void);
96 1.1 uch STATIC void emac3_config(const u_int8_t *);
97 1.1 uch
98 1.1 uch int
99 1.1 uch emac3_init(struct emac3_softc *sc)
100 1.1 uch {
101 1.1 uch u_int32_t r;
102 1.1 uch
103 1.1 uch /* save current mode before reset */
104 1.1 uch r = _emac3_reg_read_4(EMAC3_MR1);
105 1.1 uch
106 1.1 uch if (emac3_soft_reset() != 0) {
107 1.1 uch printf("%s: reset failed.\n", sc->dev.dv_xname);
108 1.1 uch return (1);
109 1.1 uch }
110 1.1 uch
111 1.1 uch /* set operation mode */
112 1.1 uch r |= MR1_RFS_2KB | MR1_TFS_1KB | MR1_TR0_SINGLE | MR1_TR1_SINGLE;
113 1.1 uch _emac3_reg_write_4(EMAC3_MR1, r);
114 1.1 uch
115 1.1 uch sc->mode1_reg = _emac3_reg_read_4(EMAC3_MR1);
116 1.1 uch
117 1.1 uch emac3_intr_clear();
118 1.1 uch emac3_intr_disable();
119 1.1 uch
120 1.1 uch emac3_config(sc->eaddr);
121 1.1 uch
122 1.1 uch return (0);
123 1.1 uch }
124 1.1 uch
125 1.1 uch void
126 1.1 uch emac3_exit(struct emac3_softc *sc)
127 1.1 uch {
128 1.1 uch int retry = 10000;
129 1.1 uch
130 1.1 uch /* wait for kicked transmission */
131 1.1 uch while (((_emac3_reg_read_4(EMAC3_TMR0) & TMR0_GNP0) != 0) &&
132 1.1 uch --retry > 0)
133 1.1 uch ;
134 1.1 uch
135 1.1 uch if (retry == 0)
136 1.1 uch printf("%s: still running.\n", sc->dev.dv_xname);
137 1.1 uch }
138 1.1 uch
139 1.1 uch int
140 1.1 uch emac3_reset(struct emac3_softc *sc)
141 1.1 uch {
142 1.1 uch
143 1.1 uch if (emac3_soft_reset() != 0) {
144 1.1 uch printf("%s: reset failed.\n", sc->dev.dv_xname);
145 1.1 uch return (1);
146 1.1 uch }
147 1.1 uch
148 1.1 uch /* restore previous mode */
149 1.1 uch _emac3_reg_write_4(EMAC3_MR1, sc->mode1_reg);
150 1.1 uch
151 1.1 uch emac3_config(sc->eaddr);
152 1.1 uch
153 1.1 uch return (0);
154 1.1 uch }
155 1.1 uch
156 1.1 uch void
157 1.1 uch emac3_enable()
158 1.1 uch {
159 1.1 uch
160 1.1 uch _emac3_reg_write_4(EMAC3_MR0, MR0_TXE | MR0_RXE);
161 1.1 uch }
162 1.1 uch
163 1.1 uch void
164 1.1 uch emac3_disable()
165 1.1 uch {
166 1.1 uch int retry = 10000;
167 1.1 uch
168 1.1 uch _emac3_reg_write_4(EMAC3_MR0,
169 1.1 uch _emac3_reg_read_4(EMAC3_MR0) & ~(MR0_TXE | MR0_RXE));
170 1.1 uch
171 1.1 uch /* wait for idling state */
172 1.1 uch while (((_emac3_reg_read_4(EMAC3_MR0) & (MR0_RXI | MR0_TXI)) !=
173 1.1 uch (MR0_RXI | MR0_TXI)) && --retry > 0)
174 1.1 uch ;
175 1.1 uch
176 1.1 uch if (retry == 0)
177 1.1 uch printf("emac3 running.\n");
178 1.1 uch }
179 1.1 uch
180 1.1 uch void
181 1.1 uch emac3_intr_enable()
182 1.1 uch {
183 1.1 uch
184 1.1 uch _emac3_reg_write_4(EMAC3_ISER, ~0);
185 1.1 uch }
186 1.1 uch
187 1.1 uch void
188 1.1 uch emac3_intr_disable()
189 1.1 uch {
190 1.1 uch
191 1.1 uch _emac3_reg_write_4(EMAC3_ISER, 0);
192 1.1 uch }
193 1.1 uch
194 1.1 uch void
195 1.1 uch emac3_intr_clear()
196 1.1 uch {
197 1.1 uch
198 1.1 uch _emac3_reg_write_4(EMAC3_ISR, _emac3_reg_read_4(EMAC3_ISR));
199 1.1 uch }
200 1.1 uch
201 1.1 uch int
202 1.1 uch emac3_intr(void *arg)
203 1.1 uch {
204 1.1 uch u_int32_t r = _emac3_reg_read_4(EMAC3_ISR);
205 1.1 uch
206 1.1 uch DPRINTF("%08x\n", r);
207 1.1 uch _emac3_reg_write_4(EMAC3_ISR, r);
208 1.1 uch
209 1.1 uch return (1);
210 1.1 uch }
211 1.1 uch
212 1.1 uch void
213 1.1 uch emac3_tx_kick()
214 1.1 uch {
215 1.1 uch
216 1.1 uch _emac3_reg_write_4(EMAC3_TMR0, TMR0_GNP0);
217 1.1 uch }
218 1.1 uch
219 1.1 uch int
220 1.1 uch emac3_tx_done()
221 1.1 uch {
222 1.1 uch
223 1.1 uch return (_emac3_reg_read_4(EMAC3_TMR0) & TMR0_GNP0);
224 1.1 uch }
225 1.1 uch
226 1.1 uch void
227 1.1 uch emac3_setmulti(struct emac3_softc *sc, struct ethercom *ec)
228 1.1 uch {
229 1.1 uch struct ether_multi *enm;
230 1.1 uch struct ether_multistep step;
231 1.1 uch struct ifnet *ifp = &ec->ec_if;
232 1.1 uch u_int32_t r;
233 1.1 uch
234 1.1 uch r = _emac3_reg_read_4(EMAC3_RMR);
235 1.1 uch r &= ~(RMR_PME | RMR_PMME | RMR_MIAE);
236 1.1 uch
237 1.1 uch if (ifp->if_flags & IFF_PROMISC) {
238 1.1 uch allmulti:
239 1.1 uch ifp->if_flags |= IFF_ALLMULTI;
240 1.1 uch r |= RMR_PME;
241 1.1 uch _emac3_reg_write_4(EMAC3_RMR, r);
242 1.1 uch
243 1.1 uch return;
244 1.1 uch }
245 1.1 uch
246 1.1 uch ETHER_FIRST_MULTI(step, ec, enm);
247 1.1 uch while (enm != NULL) {
248 1.1 uch if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
249 1.1 uch ETHER_ADDR_LEN) != 0)
250 1.1 uch goto allmulti;
251 1.1 uch
252 1.1 uch ETHER_NEXT_MULTI(step, enm)
253 1.1 uch }
254 1.1 uch
255 1.1 uch /* XXX always multicast promiscuous mode. XXX use hash table.. */
256 1.1 uch ifp->if_flags |= IFF_ALLMULTI;
257 1.1 uch r |= RMR_PMME;
258 1.1 uch _emac3_reg_write_4(EMAC3_RMR, r);
259 1.1 uch }
260 1.1 uch
261 1.1 uch int
262 1.1 uch emac3_soft_reset()
263 1.1 uch {
264 1.1 uch int retry = 10000;
265 1.1 uch
266 1.1 uch _emac3_reg_write_4(EMAC3_MR0, MR0_SRST);
267 1.1 uch
268 1.1 uch while ((_emac3_reg_read_4(EMAC3_MR0) & MR0_SRST) == MR0_SRST &&
269 1.1 uch --retry > 0)
270 1.1 uch ;
271 1.1 uch
272 1.1 uch return (retry == 0);
273 1.1 uch }
274 1.1 uch
275 1.1 uch void
276 1.1 uch emac3_config(const u_int8_t *eaddr)
277 1.1 uch {
278 1.1 uch
279 1.1 uch /* set ethernet address */
280 1.1 uch _emac3_reg_write_4(EMAC3_IAHR, (eaddr[0] << 8) | eaddr[1]);
281 1.1 uch _emac3_reg_write_4(EMAC3_IALR, (eaddr[2] << 24) | (eaddr[3] << 16) |
282 1.1 uch (eaddr[4] << 8) | eaddr[5]);
283 1.1 uch
284 1.1 uch /* inter-frame GAP */
285 1.1 uch _emac3_reg_write_4(EMAC3_IPGVR, 4);
286 1.1 uch
287 1.1 uch /* RX mode */
288 1.1 uch _emac3_reg_write_4(EMAC3_RMR,
289 1.1 uch RMR_SP | /* strip padding */
290 1.1 uch RMR_SFCS | /* strip FCS */
291 1.1 uch RMR_IAE | /* individual address enable */
292 1.1 uch RMR_BAE); /* boradcast address enable */
293 1.1 uch
294 1.1 uch /* TX mode */
295 1.1 uch _emac3_reg_write_4(EMAC3_TMR1,
296 1.1 uch ((7 << TMR1_TLR_SHIFT) & TMR1_TLR_MASK) | /* 16 word burst */
297 1.1 uch ((15 << TMR1_TUR_SHIFT) & TMR1_TUR_MASK));
298 1.1 uch
299 1.1 uch /* TX threshold */
300 1.1 uch _emac3_reg_write_4(EMAC3_TRTR,
301 1.1 uch (12 << TRTR_SHIFT) & TRTR_MASK); /* 832 bytes */
302 1.1 uch
303 1.1 uch /* RX watermark */
304 1.1 uch _emac3_reg_write_4(EMAC3_RWMR,
305 1.1 uch ((16 << RWMR_RLWM_SHIFT) & RWMR_RLWM_MASK) |
306 1.1 uch ((128 << RWMR_RHWM_SHIFT) & RWMR_RHWM_MASK));
307 1.1 uch }
308 1.1 uch
309 1.1 uch /*
310 1.1 uch * PHY/MII
311 1.1 uch */
312 1.1 uch int
313 1.1 uch emac3_ifmedia_upd(struct ifnet *ifp)
314 1.1 uch {
315 1.1 uch struct emac3_softc *sc;
316 1.1 uch
317 1.1 uch sc = ifp->if_softc;
318 1.1 uch
319 1.1 uch return (mii_mediachg(&sc->mii));
320 1.1 uch }
321 1.1 uch
322 1.1 uch void
323 1.1 uch emac3_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
324 1.1 uch {
325 1.1 uch struct emac3_softc *sc;
326 1.1 uch
327 1.1 uch sc = ifp->if_softc;
328 1.1 uch
329 1.1 uch mii_pollstat(&sc->mii);
330 1.1 uch ifmr->ifm_status = sc->mii.mii_media_status;
331 1.1 uch ifmr->ifm_active = sc->mii.mii_media_active;
332 1.1 uch }
333 1.1 uch
334 1.1 uch void
335 1.1 uch emac3_phy_writereg(struct device *self, int phy, int reg, int data)
336 1.1 uch {
337 1.1 uch
338 1.1 uch if (emac3_phy_ready() != 0)
339 1.1 uch return;
340 1.1 uch
341 1.1 uch _emac3_reg_write_4(EMAC3_STACR, STACR_WRITE |
342 1.1 uch ((phy << STACR_PCDASHIFT) & STACR_PCDA) | /* command dest addr*/
343 1.1 uch ((reg << STACR_PRASHIFT) & STACR_PRA) | /* register addr */
344 1.1 uch ((data << STACR_PHYDSHIFT) & STACR_PHYD)); /* data */
345 1.1 uch
346 1.1 uch if (emac3_phy_ready() != 0)
347 1.1 uch return;
348 1.1 uch }
349 1.1 uch
350 1.1 uch int
351 1.1 uch emac3_phy_readreg(struct device *self, int phy, int reg)
352 1.1 uch {
353 1.1 uch
354 1.1 uch if (emac3_phy_ready() != 0)
355 1.1 uch return (0);
356 1.1 uch
357 1.1 uch _emac3_reg_write_4(EMAC3_STACR, STACR_READ |
358 1.1 uch ((phy << STACR_PCDASHIFT) & STACR_PCDA) | /* command dest addr*/
359 1.1 uch ((reg << STACR_PRASHIFT) & STACR_PRA)); /* register addr */
360 1.1 uch
361 1.1 uch if (emac3_phy_ready() != 0)
362 1.1 uch return (0);
363 1.1 uch
364 1.1 uch return ((_emac3_reg_read_4(EMAC3_STACR) >> STACR_PHYDSHIFT) & 0xffff);
365 1.1 uch }
366 1.1 uch
367 1.1 uch void
368 1.1 uch emac3_phy_statchg(struct device *dev)
369 1.1 uch {
370 1.1 uch #define EMAC3_FDX (MR1_FDE | MR1_EIFC | MR1_APP)
371 1.1 uch struct emac3_softc *sc = (void *)dev;
372 1.1 uch int media;
373 1.1 uch u_int32_t r;
374 1.1 uch
375 1.1 uch media = sc->mii.mii_media_active;
376 1.1 uch
377 1.1 uch r = _emac3_reg_read_4(EMAC3_MR1);
378 1.1 uch
379 1.1 uch r &= ~(MR1_MF_MASK | MR1_IST | EMAC3_FDX);
380 1.1 uch
381 1.1 uch switch (media & 0x1f) {
382 1.1 uch default:
383 1.1 uch printf("unknown media type. %08x", media);
384 1.1 uch /* FALLTHROUGH */
385 1.1 uch case IFM_100_TX:
386 1.1 uch r |= (MR1_MF_100MBS | MR1_IST);
387 1.1 uch if (media & IFM_FDX)
388 1.1 uch r |= EMAC3_FDX;
389 1.1 uch
390 1.1 uch break;
391 1.1 uch case IFM_10_T:
392 1.1 uch r |= MR1_MF_10MBS;
393 1.1 uch if (media & IFM_FDX)
394 1.1 uch r |= (EMAC3_FDX | MR1_IST);
395 1.1 uch break;
396 1.1 uch }
397 1.1 uch
398 1.1 uch _emac3_reg_write_4(EMAC3_MR1, r);
399 1.1 uch
400 1.1 uch /* store current state for re-initialize */
401 1.1 uch sc->mode1_reg = _emac3_reg_read_4(EMAC3_MR1);
402 1.1 uch #undef EMAC3_FDX
403 1.1 uch }
404 1.1 uch
405 1.1 uch int
406 1.1 uch emac3_phy_ready()
407 1.1 uch {
408 1.1 uch int retry = 10000;
409 1.1 uch
410 1.1 uch while ((_emac3_reg_read_4(EMAC3_STACR) & STACR_OC) == 0 &&
411 1.1 uch --retry > 0)
412 1.1 uch ;
413 1.1 uch if (retry == 0) {
414 1.1 uch printf("emac3: phy busy.\n");
415 1.1 uch return (1);
416 1.1 uch }
417 1.1 uch
418 1.1 uch return (0);
419 1.1 uch }
420 1.1 uch
421