xref: /netbsd-src/sys/dev/mca/if_tra_mca.c (revision 71fbb921c37470c5e74fa34a26169d8da0db6c1b)
1 /*	$NetBSD: if_tra_mca.c,v 1.18 2016/07/11 11:31:51 msaitoh 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.18 2016/07/11 11:31:51 msaitoh 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 
59 int	tiara_mca_match(device_t, cfdata_t, void *);
60 void	tiara_mca_attach(device_t, device_t, void *);
61 
62 #define TIARA_NPORTS 0x20 /* 32 */
63 #define TIARA_PROM_ID 24 /* offset to mac addr stored in prom */
64 
65 struct tiara_softc {
66 	struct	mb86950_softc sc_mb86950;	/* real "mb86950" softc */
67 
68 	/* MCA-specific goo. */
69 	void	*sc_ih;				/* interrupt cookie */
70 };
71 
72 CFATTACH_DECL_NEW(tra_mca, sizeof(struct tiara_softc),
73     tiara_mca_match, tiara_mca_attach, NULL, NULL);
74 
75 static 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 
85 static const struct tiara_mca_product *tiara_mca_lookup(u_int32_t);
86 
87 static const struct tiara_mca_product *
tiara_mca_lookup(u_int32_t id)88 tiara_mca_lookup(u_int32_t id)
89 {
90 	const struct tiara_mca_product *tra_p;
91 
92 	for (tra_p = tiara_mca_products; tra_p->tra_name != NULL; tra_p++)
93 		if (id == tra_p->tra_prodid)
94 			return (tra_p);
95 
96 	return (NULL);
97 }
98 
99 int
tiara_mca_match(device_t parent,cfdata_t match,void * aux)100 tiara_mca_match(device_t parent, cfdata_t match, void *aux)
101 {
102 	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
103 
104 	if (tiara_mca_lookup(ma->ma_id) != NULL)
105 		return (1);
106 
107 	return (0);
108 }
109 
110 /* see POS diagrams below for explanation */
111 static const int tiara_irq[] = {
112 	3, 4, 7, 9
113 };
114 static const int smc_iobase[] = {
115 	0x300, 0x340, 0x360, 0x1980, 0x2000, 0x5680, 0x5900, 0x8080
116 };
117 static const int smc_irq[] = {
118 	9, 10, 11, 15, 3, 5, 7, 4
119 };
120 
121 void
tiara_mca_attach(device_t parent,device_t self,void * aux)122 tiara_mca_attach(device_t parent, device_t self, void *aux)
123 {
124 	struct tiara_softc *isc = device_private(self);
125 	struct mb86950_softc *sc = &isc->sc_mb86950;
126 	struct mca_attach_args *ma = aux;
127 	bus_space_tag_t iot = ma->ma_iot;
128 	bus_space_handle_t ioh;
129 	u_int8_t myea[ETHER_ADDR_LEN];
130 	int pos2;
131 	int iobase = 0, irq = 0;
132 	const struct tiara_mca_product *tra_p;
133 
134 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
135 
136 	tra_p = tiara_mca_lookup(ma->ma_id);
137 
138 	switch (tra_p->tra_prodid) {
139 
140 	case MCA_PRODUCT_TIARA:
141 	case MCA_PRODUCT_TIARA_TP:
142 		/*
143 		 * POS register 2: (adf pos0)
144 		 * 7 6 5 4 3 2 1 0
145 		 * \_____/ \_/  \ \__ enable: 0=disabled, 1=enabled
146 		 *       \   \   \___ boot rom: 0=disabled, 1=enabled
147 		 *        \   \______ IRQ 00=3 01=4 10=7 11=9
148 		 *         \_________ Base I/O Port
149 		 *			0000=0x1200 0001=0x1220 ... 1110=0x13c0 1111=0x13e0
150 		 *
151 		 * POS register 3: (adf pos1) not used
152 		 * POS register 4: (adf pos2) not used
153 		 *
154 		 * POS register 5: (adf pos3) ignored
155 		 *
156 		 * 7 6 5 4 3 2 1 0
157 		 * 1 1 0 X \____/
158 		 *              \____EPROM Address
159 		 */
160 		iobase = 0x1200 + ((pos2 & 0xf0) << 1);
161 		irq = tiara_irq[((pos2 & 0x0c) >> 2)];
162 
163 		/* XXX SWAG for number pkts. */
164 		/* My Tiara LANCard has 128K memory ?!? */
165 		sc->txb_num_pkt = 4;
166 		sc->rxb_num_pkt = (65535 - 8192 - 4) / 64;
167 		/* XXX                       */
168 
169 		break;
170 
171 	case MCA_PRODUCT_SMC3016:
172 		/*
173 		 * POS register 2: (adf pos0)
174 		 * 7 6 5 4 3 2 1 0
175 		 * \_____/ \___/  \__ enable: 0=disabled, 1=enabled
176 		 *       \     \_____ I/O Address (see ioaddr table)
177 		 *        \__________ IRQ (see irq table)
178 		 *
179 		 * POS register 3: (adf pos1) ignored
180 		 * 7 6 5 4 3 2 1 0
181 		 * X X X X \____/
182 		 *              \____EPROM Address (0000 = not used)
183 		 */
184 		iobase = smc_iobase[((pos2 & 0x0e) >> 1)];
185 		if ((pos2 & 0x80) != 0)
186 			irq = smc_irq[((pos2 & 0x70) >> 4)];
187 		else {
188 			aprint_error_dev(self, "unsupported irq selected\n");
189 			return;
190 		}
191 
192 		/* XXX SWAG for number pkts. */
193 		/* The SMC3016 has a 12K rx buffer and a 4k tx buffer */
194 		sc->txb_num_pkt = 2;
195 		sc->rxb_num_pkt = (12288 - 4) / 64;
196 		/* XXX                       */
197 
198 		break;
199 	}
200 
201 
202 #ifdef DIAGNOSTIC
203 	tra_p = tiara_mca_lookup(ma->ma_id);
204 	if (tra_p == NULL) {
205 		aprint_normal("\n");
206 		aprint_error_dev(self, "where did the card go?\n");
207 		return;
208 	}
209 #endif
210 
211 	printf(" slot %d ports %#x-%#x irq %d: %s\n", ma->ma_slot + 1,iobase,
212 	    iobase + TIARA_NPORTS, irq, tra_p->tra_name);
213 
214 	/* Map i/o space. */
215 	if (bus_space_map(iot, iobase, TIARA_NPORTS, 0, &ioh)) {
216 		aprint_error_dev(self, "can't map i/o space\n");
217 		return;
218 	}
219 
220 	sc->sc_dev = self;
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(self,
245 		    "couldn't establish interrupt handler\n");
246 		return;
247 	}
248 }
249