1/*	$OpenBSD: if_hme_sbus.c,v 1.17 2022/03/13 13:34:54 mpi Exp $	*/
2/*	$NetBSD: if_hme_sbus.c,v 1.6 2001/02/28 14:52:48 mrg Exp $	*/
3
4/*-
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg.
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/*
34 * SBus front-end device driver for the HME ethernet device.
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/syslog.h>
40#include <sys/device.h>
41#include <sys/malloc.h>
42#include <sys/socket.h>
43
44#include <net/if.h>
45#include <net/if_media.h>
46
47#include <netinet/in.h>
48#include <netinet/if_ether.h>
49
50#include <dev/mii/mii.h>
51#include <dev/mii/miivar.h>
52
53#include <machine/bus.h>
54#include <machine/intr.h>
55#include <machine/autoconf.h>
56
57#include <dev/sbus/sbusvar.h>
58#include <dev/ic/hmevar.h>
59#include <dev/ofw/openfirm.h>
60
61struct hmesbus_softc {
62	struct	hme_softc	hsc_hme;	/* HME device */
63};
64
65int	hmematch_sbus(struct device *, void *, void *);
66void	hmeattach_sbus(struct device *, struct device *, void *);
67
68const struct cfattach hme_sbus_ca = {
69	sizeof(struct hmesbus_softc), hmematch_sbus, hmeattach_sbus
70};
71
72int
73hmematch_sbus(struct device *parent, void *vcf, void *aux)
74{
75	struct cfdata *cf = vcf;
76	struct sbus_attach_args *sa = aux;
77
78	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
79	    strcmp("SUNW,qfe", sa->sa_name) == 0 ||
80	    strcmp("SUNW,hme", sa->sa_name) == 0);
81}
82
83void
84hmeattach_sbus(struct device *parent, struct device *self, void *aux)
85{
86	struct sbus_attach_args *sa = aux;
87	struct hmesbus_softc *hsc = (void *)self;
88	struct hme_softc *sc = &hsc->hsc_hme;
89	u_int32_t burst, sbusburst;
90	/* XXX the following declaration should be elsewhere */
91	extern void myetheraddr(u_char *);
92
93	/* Pass on the bus tags */
94	sc->sc_bustag = sa->sa_bustag;
95	sc->sc_dmatag = sa->sa_dmatag;
96
97	if (sa->sa_nintr < 1) {
98		printf(": no interrupt\n");
99		return;
100	}
101
102	if (sa->sa_nreg < 5) {
103		printf(": only %d register sets\n", sa->sa_nreg);
104		return;
105	}
106
107	/*
108	 * Map five register banks:
109	 *
110	 *	bank 0: HME SEB registers
111	 *	bank 1: HME ETX registers
112	 *	bank 2: HME ERX registers
113	 *	bank 3: HME MAC registers
114	 *	bank 4: HME MIF registers
115	 *
116	 */
117	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
118	    (bus_addr_t)sa->sa_reg[0].sbr_offset,
119	    (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0, &sc->sc_seb) != 0) {
120		printf(": can't map registers\n");
121		return;
122	}
123	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[1].sbr_slot,
124	    (bus_addr_t)sa->sa_reg[1].sbr_offset,
125	    (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0, &sc->sc_etx) != 0) {
126		printf(": can't map registers\n");
127		return;
128	}
129	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[2].sbr_slot,
130	    (bus_addr_t)sa->sa_reg[2].sbr_offset,
131	    (bus_size_t)sa->sa_reg[2].sbr_size, 0, 0, &sc->sc_erx) != 0) {
132		printf(": can't map registers\n");
133		return;
134	}
135	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[3].sbr_slot,
136	    (bus_addr_t)sa->sa_reg[3].sbr_offset,
137	    (bus_size_t)sa->sa_reg[3].sbr_size, 0, 0, &sc->sc_mac) != 0) {
138		printf(": can't map registers\n");
139		return;
140	}
141	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[4].sbr_slot,
142	    (bus_addr_t)sa->sa_reg[4].sbr_offset,
143	    (bus_size_t)sa->sa_reg[4].sbr_size, 0, 0, &sc->sc_mif) != 0) {
144		printf(": can't map registers\n");
145		return;
146	}
147
148	if (OF_getprop(sa->sa_node, "local-mac-address",
149	    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
150		myetheraddr(sc->sc_arpcom.ac_enaddr);
151
152	/*
153	 * Get transfer burst size from PROM and pass it on
154	 * to the back-end driver.
155	 */
156	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
157	if (sbusburst == 0)
158		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
159
160	burst = getpropint(sa->sa_node, "burst-sizes", -1);
161	if (burst == -1)
162		/* take SBus burst sizes */
163		burst = sbusburst;
164
165	/* Clamp at parent's burst sizes */
166	burst &= sbusburst;
167
168	/* Translate into plain numerical format */
169	if ((burst & SBUS_BURST_64))
170		sc->sc_burst = 64;
171	else if ((burst & SBUS_BURST_32))
172		sc->sc_burst = 32;
173	else if ((burst & SBUS_BURST_16))
174		sc->sc_burst = 16;
175	else
176		sc->sc_burst = 0;
177
178	sc->sc_pci = 0; /* XXXXX should all be done in bus_dma. */
179
180	/* Establish interrupt handler */
181	bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0, hme_intr,
182	    sc, self->dv_xname);
183
184	hme_config(sc);
185}
186