1/*	$NetBSD: rtl80x9.c,v 1.19 2023/12/31 03:19:22 isaki Exp $	*/
2
3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: rtl80x9.c,v 1.19 2023/12/31 03:19:22 isaki Exp $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/mbuf.h>
39#include <sys/syslog.h>
40#include <sys/socket.h>
41#include <sys/device.h>
42
43#include <net/if.h>
44#include <net/if_ether.h>
45#include <net/if_media.h>
46
47#include <sys/bus.h>
48#include <sys/intr.h>
49
50#include <dev/ic/dp8390reg.h>
51#include <dev/ic/dp8390var.h>
52
53#include <dev/ic/ne2000reg.h>
54#include <dev/ic/ne2000var.h>
55
56#include <dev/ic/rtl80x9reg.h>
57#include <dev/ic/rtl80x9var.h>
58
59int
60rtl80x9_mediachange(struct dp8390_softc *dsc)
61{
62
63	/*
64	 * Current media is already set up.  Just reset the interface
65	 * to let the new value take hold.  The new media will be
66	 * set up in ne_pci_rtl8029_init_card() called via dp8390_init().
67	 */
68	dp8390_reset(dsc);
69	return 0;
70}
71
72void
73rtl80x9_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
74{
75	struct ifnet *ifp = &sc->sc_ec.ec_if;
76	uint8_t cr_proto = sc->cr_proto |
77	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
78
79	/*
80	 * Sigh, can detect which media is being used, but can't
81	 * detect if we have link or not.
82	 */
83
84	/* Set NIC to page 3 registers. */
85	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
86
87	if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG0) &
88	    RTL3_CONFIG0_BNC)
89		ifmr->ifm_active = IFM_ETHER | IFM_10_2;
90	else {
91		ifmr->ifm_active = IFM_ETHER | IFM_10_T;
92		if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3) &
93		    RTL3_CONFIG3_FUDUP)
94			ifmr->ifm_active |= IFM_FDX;
95		else
96			ifmr->ifm_active |= IFM_HDX;
97	}
98
99	/* Set NIC to page 0 registers. */
100	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
101}
102
103void
104rtl80x9_init_card(struct dp8390_softc *sc)
105{
106	struct ifmedia *ifm = &sc->sc_media;
107	struct ifnet *ifp = &sc->sc_ec.ec_if;
108	uint8_t cr_proto = sc->cr_proto |
109	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
110	uint8_t reg;
111
112	/* Set NIC to page 3 registers. */
113	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
114
115	/* Write enable config1-3. */
116	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR,
117	    RTL3_EECR_EEM1 | RTL3_EECR_EEM0);
118
119	/* First, set basic media type. */
120	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
121	reg &= ~(RTL3_CONFIG2_PL1 | RTL3_CONFIG2_PL0);
122	switch (IFM_SUBTYPE(ifm->ifm_cur->ifm_media)) {
123	case IFM_AUTO:
124		/* Nothing to do; both bits clear == auto-detect. */
125		break;
126
127	case IFM_10_T:
128		/*
129		 * According to docs, this should be:
130		 * reg |= RTL3_CONFIG2_PL0;
131		 * but this doesn't work, so make it the same as AUTO.
132		 */
133		break;
134
135	case IFM_10_2:
136		reg |= RTL3_CONFIG2_PL1 | RTL3_CONFIG2_PL0;
137		break;
138	}
139	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2, reg);
140
141	/* Now, set duplex mode. */
142	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
143	if (ifm->ifm_cur->ifm_media & IFM_FDX)
144		reg |= RTL3_CONFIG3_FUDUP;
145	else
146		reg &= ~RTL3_CONFIG3_FUDUP;
147	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3, reg);
148
149	/* Write disable config1-3 */
150	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR, 0);
151
152	/* Set NIC to page 0 registers. */
153	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
154}
155
156void
157rtl80x9_media_init(struct dp8390_softc *sc)
158{
159	static int rtl80x9_media[] = {
160		IFM_ETHER | IFM_AUTO,
161		IFM_ETHER | IFM_10_T,
162		IFM_ETHER | IFM_10_T | IFM_FDX,
163		IFM_ETHER | IFM_10_2,
164	};
165	static const int rtl80x9_nmedia = __arraycount(rtl80x9_media);
166
167	int i, defmedia;
168	uint8_t conf2, conf3;
169
170	aprint_normal_dev(sc->sc_dev,
171	    "10base2, 10baseT, 10baseT-FDX, auto, default ");
172
173	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR,
174	    sc->cr_proto | ED_CR_PAGE_3);
175
176	conf2 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
177	conf3 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
178	printf("[0x%02x 0x%02x] ", conf2, conf3);
179
180	conf2 &= RTL3_CONFIG2_PL1 | RTL3_CONFIG2_PL0;
181
182	switch (conf2) {
183	default:
184		defmedia = IFM_ETHER | IFM_AUTO;
185		printf("auto\n");
186		break;
187
188	case RTL3_CONFIG2_PL1 | RTL3_CONFIG2_PL0:
189	case RTL3_CONFIG2_PL1:	/* XXX rtl docs sys 10base5, but chip cant do */
190		defmedia = IFM_ETHER | IFM_10_2;
191		printf("10base2\n");
192		break;
193
194	case RTL3_CONFIG2_PL0:
195		if (conf3 & RTL3_CONFIG3_FUDUP) {
196			defmedia = IFM_ETHER | IFM_10_T | IFM_FDX;
197			printf("10baseT-FDX\n");
198		} else {
199			defmedia = IFM_ETHER | IFM_10_T;
200			printf("10baseT\n");
201		}
202		break;
203	}
204
205	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR,
206	    sc->cr_proto | ED_CR_PAGE_0);
207
208	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
209	for (i = 0; i < rtl80x9_nmedia; i++)
210		ifmedia_add(&sc->sc_media, rtl80x9_media[i], 0, NULL);
211	ifmedia_set(&sc->sc_media, defmedia);
212}
213