1/*	$OpenBSD: if_ne_cbus.c,v 1.5 2024/06/01 00:48:16 aoyama Exp $	*/
2/*	$NetBSD: if_ne_isa.c,v 1.6 1998/07/05 06:49:13 jonathan Exp $	*/
3
4/*-
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * Driver for C-bus NE2000 based Ethernet board
36 *  based on: OpenBSD:src/sys/dev/isa/if_ne_isa.c
37 */
38
39/*
40 * Supported boards:
41 * - Allied Telesis  CentreCOM LA-98 series
42 *   Available configuration:
43 *   INT(IRQ): 0(3), 1(5), 2(6), 3(9), 4(10), 5(12), 6(13) (default: INT0)
44 *   I/O address: 0xc8d0, 0xc2d0, 0xc4d0, 0xc460, 0xc9d0, 0xcad0, 0xcbd0
45 *              (default 0xc8d0)
46 */
47
48#include "bpfilter.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/mbuf.h>
53#include <sys/socket.h>
54#include <sys/ioctl.h>
55#include <sys/errno.h>
56#include <sys/syslog.h>
57#include <sys/device.h>
58
59#include <net/if.h>
60#include <net/if_media.h>
61
62#include <netinet/in.h>
63#include <netinet/if_ether.h>
64
65#if NBPFILTER > 0
66#include <net/bpf.h>
67#endif
68
69#include <machine/board.h>		/* PC_BASE */
70#include <machine/bus.h>
71#include <machine/intr.h>
72
73#include <dev/ic/dp8390reg.h>
74#include <dev/ic/dp8390var.h>
75
76#include <dev/ic/ne2000reg.h>
77#include <dev/ic/ne2000var.h>
78
79#include <dev/ic/rtl80x9reg.h>
80#include <dev/ic/rtl80x9var.h>
81
82#include <arch/luna88k/cbus/cbusvar.h>
83
84/* bus space tag for if_ne_cbus */
85struct luna88k_bus_space_tag ne_cbus_io_bst = {
86	.bs_stride_1 = 0,
87	.bs_stride_2 = 0,
88	.bs_stride_4 = 0,
89	.bs_stride_8 = 0,	/* not used */
90	.bs_offset = PCEXIO_BASE,
91	.bs_flags = TAG_LITTLE_ENDIAN
92};
93
94int	ne_cbus_match(struct device *, void *, void *);
95void	ne_cbus_attach(struct device *, struct device *, void *);
96
97struct ne_cbus_softc {
98	struct	ne2000_softc sc_ne2000;		/* real "ne2000" softc */
99
100	/* C-bus specific goo. */
101	void	*sc_ih;				/* interrupt cookie */
102};
103
104const struct cfattach ne_cbus_ca = {
105	sizeof(struct ne_cbus_softc), ne_cbus_match, ne_cbus_attach
106};
107
108int
109ne_cbus_match(struct device *parent, void *match, void *aux)
110{
111	struct ne_cbus_softc *csc = match;
112	struct ne2000_softc *nsc = &csc->sc_ne2000;
113	struct dp8390_softc *dsc = &nsc->sc_dp8390;
114	struct cbus_attach_args *caa = aux;
115	bus_space_tag_t nict = &ne_cbus_io_bst;
116	bus_space_handle_t nich;
117	bus_space_tag_t asict;
118	bus_space_handle_t asich;
119	struct cfdata *cf = match;
120
121	if (strcmp(caa->ca_name, cf->cf_driver->cd_name) != 0)
122		return (0);
123
124	SET_TAG_LITTLE_ENDIAN(nict);
125
126	caa->ca_iobase = cf->cf_iobase;
127	caa->ca_int    = cf->cf_int;
128
129	/* Disallow wildcarded values. */
130	if (caa->ca_int ==  -1)
131		return (0);
132	if (caa->ca_iobase == -1)
133		return (0);
134
135#if 0	/* XXX: Is this not true on PC-9801? */
136	/* Make sure this is a valid NE[12]000 i/o address. */
137	if ((caa->ca_iobase & 0x1f) != 0)
138		return (0);
139#endif
140	/* Map i/o space. */
141	if (bus_space_map(nict, caa->ca_iobase, NE2000_NPORTS, 0, &nich))
142		return (0);
143
144	asict = nict;
145	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
146	    NE2000_ASIC_NPORTS, &asich))
147		goto out;
148
149	dsc->sc_regt = nict;
150	dsc->sc_regh = nich;
151
152	nsc->sc_asict = asict;
153	nsc->sc_asich = asich;
154
155	/* Look for an NE2000-compatible card. */
156	nsc->sc_type = ne2000_detect(nsc);
157
158	if (nsc->sc_type)
159		caa->ca_iosize = NE2000_NPORTS;
160
161 out:
162	bus_space_unmap(nict, nich, NE2000_NPORTS);
163	return (nsc->sc_type);
164}
165
166void
167ne_cbus_attach(struct device *parent, struct device *self, void *aux)
168{
169	struct ne_cbus_softc *csc = (void *)self;
170	struct ne2000_softc *nsc = &csc->sc_ne2000;
171	struct dp8390_softc *dsc = &nsc->sc_dp8390;
172	struct cbus_attach_args *caa = aux;
173	bus_space_tag_t nict = &ne_cbus_io_bst;
174	bus_space_handle_t nich;
175	bus_space_tag_t asict = nict;
176	bus_space_handle_t asich;
177	const char *typestr;
178
179	/* Map i/o space. */
180	if (bus_space_map(nict, caa->ca_iobase, NE2000_NPORTS, 0, &nich)) {
181		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
182		return;
183	}
184
185	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
186	    NE2000_ASIC_NPORTS, &asich)) {
187		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
188		return;
189	}
190
191	dsc->sc_regt = nict;
192	dsc->sc_regh = nich;
193
194	nsc->sc_asict = asict;
195	nsc->sc_asich = asich;
196
197	/* Look for an NE2000-compatible card. */
198	nsc->sc_type = ne2000_detect(nsc);
199
200	switch (nsc->sc_type) {
201	case NE2000_TYPE_NE1000:
202		typestr = "NE1000";
203		break;
204
205	case NE2000_TYPE_NE2000:
206		typestr = "NE2000";
207		/*
208		 * Check for a Realtek 8019.
209		 */
210		bus_space_write_1(nict, nich, ED_P0_CR,
211		    ED_CR_PAGE_0 | ED_CR_STP);
212		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
213								RTL0_8019ID0 &&
214		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
215								RTL0_8019ID1) {
216			typestr = "NE2000 (RTL8019)";
217			dsc->sc_mediachange = rtl80x9_mediachange;
218			dsc->sc_mediastatus = rtl80x9_mediastatus;
219			dsc->init_card = rtl80x9_init_card;
220			dsc->sc_media_init = rtl80x9_media_init;
221		}
222		break;
223
224	default:
225		printf(": where did the card go?!\n");
226		return;
227	}
228
229	printf(", %s", typestr);
230
231	/* This interface is always enabled. */
232	dsc->sc_enabled = 1;
233
234	/*
235	 * Do generic NE2000 attach.  This will read the station address
236	 * from the EEPROM.
237	 */
238	ne2000_attach(nsc, NULL);
239
240	/* Establish the interrupt handler. */
241	if (cbus_isrlink(dp8390_intr, dsc, caa->ca_int, IPL_NET,
242	    dsc->sc_dev.dv_xname) != 0)
243		printf(": couldn't establish interrupt handler\n");
244}
245