1/*	$NetBSD: dp83905.c,v 1.3 2005/12/11 12:21:26 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2001 Ben Harris
5 * Copyright (c) 1998 Mike Pumford
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the NetBSD
18 *	Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 *    contributors may be used to endorse or promote products derived
21 *    from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <sys/param.h>
37
38__KERNEL_RCSID(0, "$NetBSD: dp83905.c,v 1.3 2005/12/11 12:21:26 christos Exp $");
39
40#include <sys/device.h>
41#include <sys/mbuf.h>
42#include <sys/socket.h>
43#include <sys/systm.h>
44
45#include <net/if.h>
46#include <net/if_ether.h>
47#include <net/if_media.h>
48
49#include <sys/bus.h>
50
51#include <dev/ic/dp8390reg.h>
52#include <dev/ic/dp8390var.h>
53
54#include <dev/ic/dp83905reg.h>
55#include <dev/ic/dp83905var.h>
56
57int
58dp83905_mediachange(struct dp8390_softc *sc)
59{
60
61	/* Media already set up.  Reset the interface to make it stick. */
62	dp8390_reset(sc);
63	return (0);
64}
65
66void
67dp83905_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
68{
69	bus_space_tag_t nict = sc->sc_regt;
70	bus_space_handle_t nich = sc->sc_regh;
71	u_int8_t mcrb;
72
73	bus_space_write_1(nict, nich, ED_P0_CR,
74	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
75	mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
76	/* Random op so next RBCR1 write doesn't go to MCRB. */
77	bus_space_write_1(nict, nich, ED_P0_CR,
78	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
79	switch (mcrb & DP83905_MCRB_PHY_MASK) {
80	case DP83905_MCRB_PHY_10_T:
81	case DP83905_MCRB_PHY_TPI_NONSPEC: /* XXX */
82		ifmr->ifm_active = IFM_ETHER | IFM_10_T;
83		ifmr->ifm_status = IFM_AVALID;
84		if (mcrb & DP83905_MCRB_GDLNK)
85			ifmr->ifm_status |= IFM_ACTIVE;
86		break;
87	case DP83905_MCRB_PHY_10_2:
88		ifmr->ifm_active = IFM_ETHER | IFM_10_2;
89		break;
90	case DP83905_MCRB_PHY_AUI:
91		ifmr->ifm_active = IFM_ETHER | IFM_10_5;
92		break;
93	}
94}
95
96void dp83905_init_card(struct dp8390_softc *sc)
97{
98	struct ifmedia *ifm = &sc->sc_media;
99	bus_space_tag_t nict = sc->sc_regt;
100	bus_space_handle_t nich = sc->sc_regh;
101	u_int8_t mcrb;
102
103	/* Set basic media type. */
104	switch (IFM_SUBTYPE(ifm->ifm_cur->ifm_media)) {
105	case IFM_AUTO:
106		/* software auto detect the media */
107		mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
108		mcrb &= ~(DP83905_MCRB_PHY_MASK | DP83905_MCRB_GDLNK |
109		    DP83905_MCRB_BE);
110		mcrb |= DP83905_MCRB_PHY_10_T;
111		bus_space_write_1(nict, nich, DP83905_MCRB, mcrb);
112		mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
113		if (mcrb & DP83905_MCRB_GDLNK)
114			break;
115		/* No UTP use BNC */
116		/* FALLTHROUGH */
117
118	case IFM_10_2:
119		mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
120		mcrb &= ~(DP83905_MCRB_PHY_MASK | DP83905_MCRB_GDLNK |
121		    DP83905_MCRB_BE);
122		mcrb |= DP83905_MCRB_PHY_10_2;
123		bus_space_write_1(nict, nich, DP83905_MCRB, mcrb);
124		/*
125		 * seems that re-reading config B here is required to
126	         * prevent the interface hanging when manually selecting.
127		 */
128		bus_space_read_1(nict, nich, DP83905_MCRB);
129		break;
130
131	case IFM_10_T:
132		mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
133		mcrb &= ~(DP83905_MCRB_PHY_MASK | DP83905_MCRB_GDLNK |
134		    DP83905_MCRB_BE);
135		mcrb |= DP83905_MCRB_PHY_10_T;
136		bus_space_write_1(nict, nich, DP83905_MCRB, mcrb);
137		/*
138		 * seems that re-reading config B here is required to
139	         * prevent the interface hanging when manually selecting.
140		 */
141		bus_space_read_1(nict, nich, DP83905_MCRB);
142		break;
143
144	case IFM_10_5:
145		mcrb = bus_space_read_1(nict, nich, DP83905_MCRB);
146		mcrb &= ~(DP83905_MCRB_PHY_MASK | DP83905_MCRB_GDLNK |
147		    DP83905_MCRB_BE);
148		mcrb |= DP83905_MCRB_PHY_AUI;
149		bus_space_write_1(nict, nich, DP83905_MCRB, mcrb);
150		/*
151		 * seems that re-reading config B here is required to
152	         * prevent the interface hanging when manually selecting.
153		 */
154		bus_space_read_1(nict, nich, DP83905_MCRB);
155		break;
156	}
157}
158