if_tra_mca.c revision 1.10
1/*	$NetBSD: if_tra_mca.c,v 1.10 2008/04/28 20:23:53 martin Exp $	*/
2
3/*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek.
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/*
33 * Driver for Tiara LANCard/E II and friends adapted from if_ate_mca.c
34 * by Dave J. Barnes 2004.
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: if_tra_mca.c,v 1.10 2008/04/28 20:23:53 martin Exp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/device.h>
43#include <sys/socket.h>
44#include <sys/syslog.h>
45
46#include <net/if.h>
47#include <net/if_ether.h>
48#include <net/if_media.h>
49
50#include <sys/bus.h>
51#include <sys/intr.h>
52
53#include <dev/ic/mb86950reg.h>
54#include <dev/ic/mb86950var.h>
55
56#include <dev/mca/mcavar.h>
57#include <dev/mca/mcadevs.h>
58
59int	tiara_mca_match __P((struct device *, struct cfdata *, void *));
60void	tiara_mca_attach __P((struct device *, struct device *, void *));
61
62#define TIARA_NPORTS 0x20 /* 32 */
63#define TIARA_PROM_ID 24 /* offset to mac addr stored in prom */
64
65struct tiara_softc {
66	struct	mb86950_softc sc_mb86950;	/* real "mb86950" softc */
67
68	/* MCA-specific goo. */
69	void	*sc_ih;				/* interrupt cookie */
70};
71
72CFATTACH_DECL(tra_mca, sizeof(struct tiara_softc),
73    tiara_mca_match, tiara_mca_attach, NULL, NULL);
74
75static const struct tiara_mca_product {
76	u_int32_t	tra_prodid;	/* MCA product ID */
77	const char	*tra_name;	/* device name */
78} tiara_mca_products[] = {
79	{ MCA_PRODUCT_TIARA,	"Tiara LANCard/E2"},
80	{ MCA_PRODUCT_TIARA_TP,	"Tiara LANCard/E2 TP"},
81	{ MCA_PRODUCT_SMC3016,  "SMC 3016/MC"},
82	{ 0,			NULL },
83};
84
85static const struct tiara_mca_product *tiara_mca_lookup __P((u_int32_t));
86
87static const struct tiara_mca_product *
88tiara_mca_lookup(id)
89	u_int32_t id;
90{
91	const struct tiara_mca_product *tra_p;
92
93	for (tra_p = tiara_mca_products; tra_p->tra_name != NULL; tra_p++)
94		if (id == tra_p->tra_prodid)
95			return (tra_p);
96
97	return (NULL);
98}
99
100int
101tiara_mca_match(struct device *parent, struct cfdata *match,
102    void *aux)
103{
104	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
105
106	if (tiara_mca_lookup(ma->ma_id) != NULL)
107		return (1);
108
109	return (0);
110}
111
112/* see POS diagrams below for explanation */
113static const int tiara_irq[] = {
114	3, 4, 7, 9
115};
116static const int smc_iobase[] = {
117	0x300, 0x340, 0x360, 0x1980, 0x2000, 0x5680, 0x5900, 0x8080
118};
119static const int smc_irq[] = {
120	9, 10, 11, 15, 3, 5, 7, 4
121};
122
123void
124tiara_mca_attach(struct device *parent, struct device *self, void *aux)
125{
126	struct tiara_softc *isc = device_private(self);
127	struct mb86950_softc *sc = &isc->sc_mb86950;
128	struct mca_attach_args *ma = aux;
129	bus_space_tag_t iot = ma->ma_iot;
130	bus_space_handle_t ioh;
131	u_int8_t myea[ETHER_ADDR_LEN];
132	int pos2;
133	int iobase = 0, irq = 0;
134	const struct tiara_mca_product *tra_p;
135
136	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
137
138	tra_p = tiara_mca_lookup(ma->ma_id);
139
140	switch (tra_p->tra_prodid) {
141
142	case MCA_PRODUCT_TIARA:
143	case MCA_PRODUCT_TIARA_TP:
144		/*
145		 * POS register 2: (adf pos0)
146		 * 7 6 5 4 3 2 1 0
147		 * \_____/ \_/  \ \__ enable: 0=disabled, 1=enabled
148		 *       \   \   \___ boot rom: 0=disabled, 1=enabled
149		 *        \   \______ IRQ 00=3 01=4 10=7 11=9
150		 *         \_________ Base I/O Port
151		 *			0000=0x1200 0001=0x1220 ... 1110=0x13c0 1111=0x13e0
152		 *
153		 * POS register 3: (adf pos1) not used
154		 * POS register 4: (adf pos2) not used
155		 *
156		 * POS register 5: (adf pos3) ignored
157		 *
158		 * 7 6 5 4 3 2 1 0
159		 * 1 1 0 X \____/
160		 *              \____EPROM Address
161		 */
162		iobase = 0x1200 + ((pos2 & 0xf0) << 1);
163		irq = tiara_irq[((pos2 & 0x0c) >> 2)];
164
165		/* XXX SWAG for number pkts. */
166		/* My Tiara LANCard has 128K memory ?!? */
167		sc->txb_num_pkt = 4;
168		sc->rxb_num_pkt = (65535 - 8192 - 4) / 64;
169		/* XXX                       */
170
171		break;
172
173	case MCA_PRODUCT_SMC3016:
174		/*
175		 * POS register 2: (adf pos0)
176		 * 7 6 5 4 3 2 1 0
177		 * \_____/ \___/  \__ enable: 0=disabled, 1=enabled
178		 *       \     \_____ I/O Address (see ioaddr table)
179		 *        \__________ IRQ (see irq table)
180		 *
181		 * POS register 3: (adf pos1) ignored
182		 * 7 6 5 4 3 2 1 0
183		 * X X X X \____/
184		 *              \____EPROM Address (0000 = not used)
185		 */
186		iobase = smc_iobase[((pos2 & 0x0e) >> 1)];
187		if ((pos2 & 0x80) != 0)
188			irq = smc_irq[((pos2 & 0x70) >> 4)];
189		else {
190			aprint_error_dev(&sc->sc_dev, "unsupported irq selected\n");
191			return;
192		}
193
194		/* XXX SWAG for number pkts. */
195		/* The SMC3016 has a 12K rx buffer and a 4k tx buffer */
196		sc->txb_num_pkt = 2;
197		sc->rxb_num_pkt = (12288 - 4) / 64;
198		/* XXX                       */
199
200		break;
201	}
202
203
204#ifdef DIAGNOSTIC
205	tra_p = tiara_mca_lookup(ma->ma_id);
206	if (tra_p == NULL) {
207		aprint_normal("\n");
208		aprint_error_dev(&sc->sc_dev, "where did the card go?\n");
209		return;
210	}
211#endif
212
213	printf(" slot %d ports %#x-%#x irq %d: %s\n", ma->ma_slot + 1,iobase, iobase + TIARA_NPORTS, irq, tra_p->tra_name);
214
215	/* Map i/o space. */
216	if (bus_space_map(iot, iobase, TIARA_NPORTS, 0, &ioh)) {
217		aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
218		return;
219	}
220
221	sc->sc_bst = iot;
222	sc->sc_bsh = ioh;
223
224	/* Get ethernet address from PROM */
225	bus_space_read_region_1(iot, ioh, TIARA_PROM_ID, myea, ETHER_ADDR_LEN);
226
227	/* This interface is always enabled. */
228	/* XXX
229	sc->sc_stat |= NIC_STAT_ENABLED;
230	*/
231
232	/*
233	 * Do generic MB86950 attach.
234	 */
235	mb86950_attach(sc, myea);
236
237
238	mb86950_config(sc, NULL, 0, 0);
239
240	/* Establish the interrupt handler. */
241	isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
242			mb86950_intr, sc);
243	if (isc->sc_ih == NULL) {
244		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt handler\n");
245		return;
246	}
247}
248