1/*	$NetBSD: if_ate_mca.c,v 1.20 2008/04/12 06:27:01 tsutsui Exp $	*/
2
3/*-
4 * Copyright (c) 2001 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 ATI AT1720X MCA cards based on Fujitsu MB8696xA controller.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: if_ate_mca.c,v 1.20 2008/04/12 06:27:01 tsutsui Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/device.h>
42#include <sys/socket.h>
43#include <sys/syslog.h>
44
45#include <net/if.h>
46#include <net/if_ether.h>
47#include <net/if_media.h>
48
49#include <sys/bus.h>
50#include <sys/intr.h>
51
52#include <dev/ic/mb86960reg.h>
53#include <dev/ic/mb86960var.h>
54
55#include <dev/mca/mcavar.h>
56#include <dev/mca/mcadevs.h>
57
58int	ate_mca_match(device_t, cfdata_t, void *);
59void	ate_mca_attach(device_t, device_t, void *);
60static void ate_mca_detect(bus_space_tag_t, bus_space_handle_t,
61    uint8_t enaddr[ETHER_ADDR_LEN]);
62
63#define ATE_NPORTS 0x20
64
65struct ate_softc {
66	struct	mb86960_softc sc_mb86960;	/* real "mb86960" softc */
67
68	/* MCA-specific goo. */
69	void	*sc_ih;				/* interrupt cookie */
70};
71
72CFATTACH_DECL_NEW(ate_mca, sizeof(struct ate_softc),
73    ate_mca_match, ate_mca_attach, NULL, NULL);
74
75static const struct ate_mca_product {
76	uint32_t	at_prodid;	/* MCA product ID */
77	const char	*at_name;	/* device name */
78	int		at_type;	/* device type */
79} ate_mca_products[] = {
80	{ MCA_PRODUCT_AT1720T,	"ATI AT1720T",	FE_TYPE_AT1700T		},
81	{ MCA_PRODUCT_AT1720BT,	"ATI AT1720BT",	FE_TYPE_AT1700BT	},
82	{ MCA_PRODUCT_AT1720AT, "ATI AT1720AT",	FE_TYPE_AT1700AT	},
83	{ MCA_PRODUCT_AT1720FT, "ATI AT1720FT",	FE_TYPE_AT1700FT	},
84	{ 0,			NULL,		0			},
85};
86
87static const struct ate_mca_product *ate_mca_lookup(uint32_t);
88
89static const struct ate_mca_product *
90ate_mca_lookup(uint32_t id)
91{
92	const struct ate_mca_product *atp;
93
94	for (atp = ate_mca_products; atp->at_name != NULL; atp++)
95		if (id == atp->at_prodid)
96			return atp;
97
98	return NULL;
99}
100
101int
102ate_mca_match(device_t parent, cfdata_t cf, void *aux)
103{
104	struct mca_attach_args *ma = aux;
105
106	if (ate_mca_lookup(ma->ma_id) != NULL)
107		return 1;
108
109	return 0;
110}
111
112/* see POS diagrams below for explanation of these arrays' contents */
113static const int ats_iobase[] = {
114	0x400, 0x2400, 0x4400, 0x6400, 0x1400, 0x3400, 0x5400, 0x7400
115};
116static const int ats_irq[] = {
117	3, 4, 5, 9, 10, 11, 12, 15
118};
119
120void
121ate_mca_attach(device_t parent, device_t self, void *aux)
122{
123	struct ate_softc *isc = device_private(self);
124	struct mb86960_softc *sc = &isc->sc_mb86960;
125	struct mca_attach_args *ma = aux;
126	bus_space_tag_t iot = ma->ma_iot;
127	bus_space_handle_t ioh;
128	uint8_t myea[ETHER_ADDR_LEN];
129	int pos3, pos4;
130	int iobase, irq;
131	const struct ate_mca_product *atp;
132
133	sc->sc_dev = self;
134
135	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
136	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
137
138	/*
139	 * POS register 2: (adf pos0)
140	 * 7 6 5 4 3 2 1 0
141	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
142	 *
143	 * POS register 3: (adf pos1)
144	 *
145	 * 7 6 5 4 3 2 1 0
146	 * \_/ \___/ \___/
147	 *   \     \     \__ I/O Port Addresses: 000=0x400-0x4FF 100=0x1400-
148	 *    \     \          001=0x2400 101=0x3400 010=0x4400 110=0x5400
149	 *     \     \         011=0x6400 111=0x7400
150	 *      \     \_____ Boot ROM Memory Address
151	 *       \
152	 *        \_________ Lower 2 bit of Interrupt Request Number
153	 *
154	 * POS register 4: (adf pos2)
155	 *
156	 * 7 6 5 4 3 2 1 0
157	 *   \      \_______ Twisted Pair Type: 0=100 ohm, Unshielded
158	 *    \                1=150 ohm, Shielded
159	 *     \____________ Higher 1 bit of Interrupt Request Number:
160	 *                   000=3 001=4 010=5 011=9 100=10 101=11 110=12 111=15
161	 */
162
163	atp = ate_mca_lookup(ma->ma_id);
164#ifdef DIAGNOSTIC
165	if (atp == NULL) {
166		aprint_normal("\n");
167		aprint_error_dev(sc->sc_dev, "where did the card go?\n");
168		return;
169	}
170#endif
171
172	iobase = ats_iobase[pos3 & 0x7];
173	irq = ats_irq[((pos4 & 0x40) >> 4) | ((pos3 & 0xc0) >> 6)];
174
175	aprint_normal(" slot %d irq %d: %s\n",
176	    ma->ma_slot + 1, irq, atp->at_name);
177
178	/* Map i/o space. */
179	if (bus_space_map(iot, iobase, ATE_NPORTS, 0, &ioh)) {
180		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
181		return;
182	}
183
184	sc->sc_bst = iot;
185	sc->sc_bsh = ioh;
186
187	/* Get ethernet address. */
188	ate_mca_detect(iot, ioh, myea);
189
190	/* This interface is always enabled. */
191	sc->sc_stat |= FE_STAT_ENABLED;
192
193	/*
194	 * Do generic MB86960 attach.
195	 */
196	mb86960_attach(sc, myea);
197
198	mb86960_config(sc, NULL, 0, 0);
199
200	/* Establish the interrupt handler. */
201	isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
202	    mb86960_intr, sc);
203	if (isc->sc_ih == NULL) {
204		aprint_error_dev(sc->sc_dev,
205		    "couldn't establish interrupt handler\n");
206		return;
207	}
208}
209
210/*
211 * Very simplified ate_detect() from dev/isa/if_ate.c, only
212 * to determine ethernet address.
213 */
214static void
215ate_mca_detect(bus_space_tag_t iot, bus_space_handle_t ioh,
216    uint8_t enaddr[ETHER_ADDR_LEN])
217{
218	uint8_t eeprom[FE_EEPROM_SIZE];
219
220	/* Get our station address from EEPROM. */
221	mb86965_read_eeprom(iot, ioh, eeprom);
222	memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN);
223}
224