xref: /netbsd-src/sys/arch/newsmips/apbus/if_tlp_ap.c (revision 86d41103ec9e9cb2a6100b732537c1a07d1a0279)
1 /*	$NetBSD: if_tlp_ap.c,v 1.16 2023/12/20 04:30:27 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Atsushi Onoe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_tlp_ap.c,v 1.16 2023/12/20 04:30:27 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/errno.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_ether.h>
47 
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50 
51 #include <mips/cache.h>
52 
53 #include <dev/mii/miivar.h>
54 #include <dev/mii/mii_bitbang.h>
55 
56 #include <dev/ic/tulipreg.h>
57 #include <dev/ic/tulipvar.h>
58 
59 #include <newsmips/apbus/apbusvar.h>
60 
61 #define	TLP_AP_ROM	0x00000000	/* AProm entry address */
62 #define	TLP_AP_CFG	0x00040000	/* PCI configuration registers */
63 #define		TLP_AP_CFG_CFID	0x00		/* Identification */
64 #define		TLP_AP_CFG_CFCS	0x04		/* Command and status */
65 #define		TLP_AP_CFG_CFRV	0x08		/* Revision */
66 #define		TLP_AP_CFG_CFLT	0x0c		/* Latency timer */
67 #define		TLP_AP_CFG_CBIO	0x10		/* Base I/O address */
68 #define		TLP_AP_CFG_CBMA	0x14		/* Base memory address */
69 #define		TLP_AP_CFG_CFIT	0x3c		/* Interrupt */
70 #define	TLP_AP_CSR	0x00080000	/* CSR base address */
71 #define	TLP_AP_RST	0x00100000	/* Board Reset */
72 
73 
74 struct tulip_ap_softc {
75 	struct tulip_softc sc_tulip;	/* real Tulip softc */
76 	bus_space_tag_t sc_cfst;
77 	bus_space_handle_t sc_cfsh;
78 };
79 
80 static int	tlp_ap_match(device_t, cfdata_t, void *);
81 static void	tlp_ap_attach(device_t, device_t, void *);
82 
83 CFATTACH_DECL_NEW(tlp_ap, sizeof(struct tulip_ap_softc),
84     tlp_ap_match, tlp_ap_attach, NULL, NULL);
85 
86 static void tlp_ap_preinit(struct tulip_softc *);
87 static void tlp_ap_tmsw_init(struct tulip_softc *);
88 static void tlp_ap_getmedia(struct tulip_softc *, struct ifmediareq *);
89 static int tlp_ap_setmedia(struct tulip_softc *);
90 
91 const struct tulip_mediasw tlp_ap_mediasw = {
92 	tlp_ap_tmsw_init, tlp_ap_getmedia, tlp_ap_setmedia
93 };
94 
95 static int
tlp_ap_match(device_t parent,cfdata_t cf,void * aux)96 tlp_ap_match(device_t parent, cfdata_t cf, void *aux)
97 {
98 	struct apbus_attach_args *apa = aux;
99 
100 	if (strcmp(apa->apa_name, "cbasetx") != 0)
101 		return 0;
102 
103 	return 1;
104 }
105 
106 /*
107  * Install interface into kernel networking data structures
108  */
109 static void
tlp_ap_attach(device_t parent,device_t self,void * aux)110 tlp_ap_attach(device_t parent, device_t self, void *aux)
111 {
112 	struct tulip_ap_softc *psc = device_private(self);
113 	struct tulip_softc *sc = &psc->sc_tulip;
114 	struct apbus_attach_args *apa = aux;
115 	uint8_t enaddr[ETHER_ADDR_LEN];
116 	u_int intrmask;
117 	int i;
118 
119 	sc->sc_dev = self;
120 
121 	aprint_normal(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
122 
123 	/* PCI configuration register */
124 	psc->sc_cfst = 0;
125 	psc->sc_cfsh = apa->apa_hwbase + TLP_AP_CFG;
126 	sc->sc_devno = apa->apa_slotno;
127 	sc->sc_rev = bus_space_read_4(psc->sc_cfst, psc->sc_cfsh,
128 	    TLP_AP_CFG_CFRV);
129 	switch (bus_space_read_4(psc->sc_cfst, psc->sc_cfsh, TLP_AP_CFG_CFID)) {
130 	case 0x00091011:
131 		if (sc->sc_rev >= 0x20)
132 			sc->sc_chip = TULIP_CHIP_21140A;
133 		else
134 			sc->sc_chip = TULIP_CHIP_21140;
135 		break;
136 	default:
137 		aprint_error(": unable to handle your board\n");
138 		return;
139 	}
140 
141 	aprint_normal(": %s Ethernet, pass %d.%d\n",
142 	    tlp_chip_name(sc->sc_chip),
143 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
144 
145 	/* CSR */
146 	sc->sc_st = 0;
147 	sc->sc_sh = apa->apa_hwbase + TLP_AP_CSR;
148 	sc->sc_dmat = apbus_dmatag_init(apa);
149 	if (sc->sc_dmat == NULL) {
150 		aprint_error_dev(self, "cannot allocate memory\n");
151 		return;
152 	}
153 
154 	/*
155 	 * Initialize bus specific parameters.
156 	 */
157 	if (mips_cache_info.mci_sdcache_line_size > 0)
158 		sc->sc_cacheline = mips_cache_info.mci_sdcache_line_size / 4;
159 	else if (mips_cache_info.mci_pdcache_line_size > 0)
160 		sc->sc_cacheline = mips_cache_info.mci_pdcache_line_size / 4;
161 	else
162 		sc->sc_cacheline = 4;
163 	sc->sc_maxburst = sc->sc_cacheline;		/* XXX */
164 	sc->sc_regshift = 3;
165 	sc->sc_flags |= TULIPF_DBO | TULIPF_BLE;	/* Big Endian BUS */
166 	sc->sc_flags |= TULIPF_ENABLED;			/* No Power Mgmt */
167 
168 	/*
169 	 * Reset hardware.
170 	 */
171 	bus_space_write_4(0, apa->apa_hwbase + TLP_AP_RST, 0, 1);
172 	delay(100);
173 
174 	/*
175 	 * Initialize PCI configuration register
176 	 */
177 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
178 	    TLP_AP_CFG_CFCS, 0x00000005);	/* Master, IO */
179 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
180 	    TLP_AP_CFG_CFLT, 0x00000100);
181 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
182 	    TLP_AP_CFG_CBIO, MIPS_KSEG1_TO_PHYS(sc->sc_sh));
183 	bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
184 	    TLP_AP_CFG_CFIT, 0x00000101);
185 
186 	/*
187 	 * Initialize general purpose port register.
188 	 */
189 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xc0);	/* read */
190 	TULIP_WRITE(sc, CSR_GPP, 0x40);			/* dipsw port on */
191 	i = TULIP_READ(sc, CSR_GPP) & GPP_MD;		/* dipsw contents */
192 	TULIP_WRITE(sc, CSR_GPP, 0xc0);			/* dipsw port off */
193 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
194 	if (sc->sc_maxburst == 16) {
195 		TULIP_WRITE(sc, CSR_GPP, 0x8f);		/* 16word burst */
196 		TULIP_WRITE(sc, CSR_GPP, 0xcf);
197 	} else {
198 		TULIP_WRITE(sc, CSR_GPP, 0x8e);		/* 8word burst */
199 		TULIP_WRITE(sc, CSR_GPP, 0xce);
200 	}
201 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf);	/* read write */
202 	TULIP_WRITE(sc, CSR_GPP, 0xc3);			/* mask abort/DMA err */
203 	TULIP_WRITE(sc, CSR_GPP, 0xcf);			/* mask abort/DMA err */
204 
205 	if (tlp_read_srom(sc) == 0) {
206 		aprint_error_dev(self, "srom read failed\n");
207 		free(sc->sc_dmat, M_DEVBUF);
208 		return;
209 	}
210 	for (i = 0; i < ETHER_ADDR_LEN; i++)
211 		enaddr[i] = sc->sc_srom[0x11 + i * 2];
212 
213 	sc->sc_mediasw = &tlp_ap_mediasw;
214 
215 	/*
216 	 * Finish off the attach.
217 	 */
218 	tlp_attach(sc, enaddr);
219 
220 	intrmask = SLOTTOMASK(apa->apa_slotno);
221 	apbus_intr_establish(0, /* interrupt level (0 or 1) */
222 	    intrmask,
223 	    0, /* priority */
224 	    tlp_intr, sc, device_xname(self), apa->apa_ctlnum);
225 }
226 
227 static void
tlp_ap_preinit(struct tulip_softc * sc)228 tlp_ap_preinit(struct tulip_softc *sc)
229 {
230 
231 	sc->sc_opmode |= OPMODE_MBO | OPMODE_SCR | OPMODE_PCS | OPMODE_HBD |
232 	    OPMODE_PS;
233 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
234 }
235 
236 static void
tlp_ap_tmsw_init(struct tulip_softc * sc)237 tlp_ap_tmsw_init(struct tulip_softc *sc)
238 {
239 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
240 	struct mii_data *mii = &sc->sc_mii;
241 
242 	sc->sc_preinit = tlp_ap_preinit;
243 
244 	mii->mii_ifp = ifp;
245 	mii->mii_readreg = NULL;
246 	mii->mii_writereg = NULL;
247 	mii->mii_statchg = sc->sc_statchg;
248 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
249 	ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_100_TX, 0, NULL);
250 	ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
251 	    NULL);
252 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_100_TX);
253 }
254 
255 static void
tlp_ap_getmedia(struct tulip_softc * sc,struct ifmediareq * ifmr)256 tlp_ap_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
257 {
258 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
259 
260 	ifmr->ifm_status = IFM_AVALID;
261 	if (ifp->if_flags & IFF_RUNNING)
262 		ifmr->ifm_status |= IFM_ACTIVE;
263 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
264 }
265 
266 static int
tlp_ap_setmedia(struct tulip_softc * sc)267 tlp_ap_setmedia(struct tulip_softc *sc)
268 {
269 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
270 
271 	sc->sc_mii.mii_media_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
272 
273 	if (ifp->if_flags & IFF_UP)
274 		tlp_idle(sc, OPMODE_ST | OPMODE_SR);
275 	if (sc->sc_mii.mii_media_active & IFM_FDX)
276 		sc->sc_opmode |= OPMODE_FD;
277 	else
278 		sc->sc_opmode &= ~OPMODE_FD;
279 	if (ifp->if_flags & IFF_UP)
280 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
281 	return 0;
282 }
283