Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / netif / ep / if_ep_mca.c
1 /*-
2  * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ep/if_ep_mca.c,v 1.2 1999/10/27 06:25:15 mdodd Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33
34 #include <sys/module.h>
35 #include <sys/bus.h>
36
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40  
41 #include <net/if.h> 
42 #include <net/if_arp.h>
43 #include <net/if_media.h>
44
45 #include <dev/mca/mca_busreg.h>
46 #include <dev/mca/mca_busvar.h>
47
48 #include <dev/ep/if_epreg.h>
49 #include <dev/ep/if_epvar.h>
50
51 #define EP_MCA_627C     0x627C
52 #define EP_MCA_627D     0x627D
53 #define EP_MCA_62DB     0x62db
54 #define EP_MCA_62F6     0x62f6
55 #define EP_MCA_62F7     0x62f7
56
57 static struct mca_ident ep_mca_devs[] = {
58         { EP_MCA_627C, "3Com 3C529 Network Adapter" },
59         { EP_MCA_627D, "3Com 3C529-TP Network Adapter" },
60
61         /*
62          * These are from the linux 3c509 driver.
63          * I have not seen the ADFs for them and have 
64          * not tested or even seen the hardware.
65          * Someone with the ADFs should replace the names with
66          * whatever is in the AdapterName field of the ADF.
67          * (and fix the media setup for the cards as well.)
68          */
69         { EP_MCA_62DB, "3Com 3c529 EtherLink III (test mode)" },
70         { EP_MCA_62F6, "3Com 3c529 EtherLink III (TP or coax)" },
71         { EP_MCA_62F7, "3Com 3c529 EtherLink III (TP)" },
72
73         { 0, NULL },
74 };
75
76 #define EP_MCA_IOPORT_POS       MCA_ADP_POS(MCA_POS2)
77 #define EP_MCA_IOPORT_MASK      0xfc
78 #define EP_MCA_IOPORT_SIZE      EP_IOSIZE
79 #define EP_MCA_IOPORT(pos)      ((((u_int32_t)pos & EP_MCA_IOPORT_MASK) \
80                                         | 0x02) << 8)
81
82 #define EP_MCA_IRQ_POS          MCA_ADP_POS(MCA_POS3)
83 #define EP_MCA_IRQ_MASK         0x0f
84 #define EP_MCA_IRQ(pos)         (pos & EP_MCA_IRQ_MASK)
85
86 #define EP_MCA_MEDIA_POS        MCA_ADP_POS(MCA_POS2)
87 #define EP_MCA_MEDIA_MASK       0x03
88 #define EP_MCA_MEDIA(pos)       (pos & EP_MCA_MEDIA_MASK)
89
90 static int
91 ep_mca_probe (device_t dev)
92 {
93         const char *    desc;
94         u_int32_t       iobase = 0;
95         u_int8_t        irq = 0;
96         u_int8_t        pos;
97
98         desc = mca_match_id(mca_get_id(dev), ep_mca_devs);
99         if (!desc)
100                 return (ENXIO);
101         device_set_desc(dev, desc);
102
103         pos = mca_pos_read(dev, EP_MCA_IOPORT_POS);
104         iobase = EP_MCA_IOPORT(pos);
105
106         pos = mca_pos_read(dev, EP_MCA_IRQ_POS);
107         irq = EP_MCA_IRQ(pos);
108
109         mca_add_iospace(dev, iobase, EP_MCA_IOPORT_SIZE);
110         mca_add_irq(dev, irq);
111
112         return (0);
113 }
114
115 static int
116 ep_mca_attach (device_t dev)
117 {
118         struct ep_softc *       sc = device_get_softc(dev);
119         int                     error = 0;
120
121         if ((error = ep_alloc(dev))) {
122                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
123                 goto bad;
124         }
125         sc->stat = F_ACCESS_32_BITS;
126
127         ep_get_media(sc);
128
129         GO_WINDOW(0);
130         SET_IRQ(BASE, rman_get_start(sc->irq));
131
132         if ((error = ep_attach(sc))) {
133                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
134                 goto bad;
135         }
136
137         if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
138                                    sc, &sc->ep_intrhand))) {
139                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
140                 goto bad;
141         }
142
143         return (0);
144 bad:
145         ep_free(dev);
146         return (error);
147 }
148
149 static device_method_t ep_mca_methods[] = {
150         /* Device interface */
151         DEVMETHOD(device_probe,         ep_mca_probe),
152         DEVMETHOD(device_attach,        ep_mca_attach),
153
154         { 0, 0 }
155 };
156
157 static driver_t ep_mca_driver = {
158         "ep",
159         ep_mca_methods,
160         sizeof(struct ep_softc),
161 };
162
163 static devclass_t ep_devclass;
164
165 DRIVER_MODULE(ep, mca, ep_mca_driver, ep_devclass, 0, 0);