Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / snc / if_snc_cbus.c
1 /*
2  * Copyright (c) 1995, David Greenman
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/snc/if_snc_cbus.c,v 1.1.2.1 2000/10/21 03:30:03 nyan Exp $
28  */
29
30 /*
31  *      National Semiconductor  DP8393X SONIC Driver
32  *
33  *      This is the C-bus specific attachment on FreeBSD
34  *              written by Motomichi Matsuzaki <mzaki@e-mail.ne.jp>
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <machine/bus.h>
45 #include <sys/rman.h>
46 #include <machine/resource.h>
47
48 #include <net/ethernet.h>
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/if_media.h>
52 #include <net/if_mib.h>
53
54 #include <isa/isavar.h>
55 #include <isa/pnpvar.h>
56 #include <sys/malloc.h>         /* as dependency for isa/isa_common.h */
57 #include <isa/isa_common.h>     /* for snc_isapnp_reconfig() */
58
59 #include <dev/snc/dp83932var.h>
60 #include <dev/snc/dp83932subr.h>
61 #include <dev/snc/if_sncreg.h>
62 #include <dev/snc/if_sncvar.h>
63
64 static void snc_isapnp_reconfig __P((device_t));
65 static int snc_isa_probe        __P((device_t));
66 static int snc_isa_attach       __P((device_t));
67
68 static struct isa_pnp_id snc_ids[] = {
69         { 0x6180a3b8,   NULL },         /* NEC8061 NEC PC-9801-104 */
70         { 0,            NULL }
71 };
72
73 static void
74 snc_isapnp_reconfig(dev)
75         device_t dev;
76 {
77         struct isa_device *idev = DEVTOISA(dev);
78         struct isa_config config;
79         u_long start, count;
80         int rid;
81
82         bzero(&config, sizeof(config));
83
84         for (rid = 0; rid < ISA_NMEM; rid++) {
85                 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count))
86                         break;
87                 config.ic_mem[rid].ir_start = start;
88                 config.ic_mem[rid].ir_end = start;
89                 config.ic_mem[rid].ir_size = count;
90         }
91         config.ic_nmem = rid;
92         for (rid = 0; rid < ISA_NPORT; rid++) {
93                 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count))
94                         break;
95                 config.ic_port[rid].ir_start = start;
96                 config.ic_port[rid].ir_end = start;
97                 config.ic_port[rid].ir_size = count;
98         }
99         config.ic_nport = rid;
100         for (rid = 0; rid < ISA_NIRQ; rid++) {
101                 if (bus_get_resource(dev, SYS_RES_IRQ, rid, &start, &count))
102                         break;
103                 config.ic_irqmask[rid] = 1 << start;
104         }
105         config.ic_nirq = rid;
106         for (rid = 0; rid < ISA_NDRQ; rid++) {
107                 if (bus_get_resource(dev, SYS_RES_DRQ, rid, &start, &count))
108                         break;
109                 config.ic_drqmask[rid] = 1 << start;
110         }
111         config.ic_ndrq = rid;
112         
113         idev->id_config_cb(idev->id_config_arg, &config, 1);
114 }
115
116 static int
117 snc_isa_probe(dev)
118         device_t dev;
119 {
120         struct snc_softc *sc = device_get_softc(dev);
121         int type;
122         int error = 0;
123
124         bzero(sc, sizeof(struct snc_softc));
125
126         /* Check isapnp ids */
127         error = ISA_PNP_PROBE(device_get_parent(dev), dev, snc_ids);
128
129         /* If the card had a PnP ID that didn't match any we know about */
130         if (error == ENXIO) {
131                 return(error);
132         }
133
134         switch (error) {
135         case 0:         /* Matched PnP */
136                 type = SNEC_TYPE_PNP;
137                 break;
138
139         case ENOENT:    /* Legacy ISA */
140                 type = SNEC_TYPE_LEGACY;
141                 break;
142
143         default:        /* If we had some other problem. */
144                 return(error);
145         }
146
147         if (type == SNEC_TYPE_PNP && isa_get_portsize(dev) == 0) {
148                 int port;
149                 int rid = 0;
150                 struct resource *res = NULL;
151
152                 for (port = 0x0888; port <= 0x3888; port += 0x1000) {
153                         bus_set_resource(dev, SYS_RES_IOPORT, rid,
154                                          port, SNEC_NREGS);
155                         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
156                                                  0, ~0, SNEC_NREGS,
157                                                  0 /* !RF_ACTIVE */);
158                         if (res) break;
159                 }
160
161                 printf("snc_isa_probe: broken PnP resource, ");
162                 if (res) {
163                         printf("use port 0x%x\n", port);
164                         bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
165                         snc_isapnp_reconfig(dev);
166                 } else {
167                         printf("and can't find port\n");
168                 }
169         }
170
171         error = snc_alloc_port(dev, 0);
172         error = max(error, snc_alloc_memory(dev, 0));
173         error = max(error, snc_alloc_irq(dev, 0, 0));
174
175         if (!error && !snc_probe(dev, type))
176                 error = ENOENT;
177
178         snc_release_resources(dev);
179         return (error);
180 }
181
182 static int
183 snc_isa_attach(dev)
184         device_t dev;
185 {
186         struct snc_softc *sc = device_get_softc(dev);
187         int error;
188         
189         bzero(sc, sizeof(struct snc_softc));
190
191         snc_alloc_port(dev, 0);
192         snc_alloc_memory(dev, 0);
193         snc_alloc_irq(dev, 0, 0);
194                 
195         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
196                                sncintr, sc, &sc->irq_handle);
197         if (error) {
198                 printf("snc_isa_attach: bus_setup_intr() failed\n");
199                 snc_release_resources(dev);
200                 return (error);
201         }              
202
203         /* This interface is always enabled. */
204         sc->sc_enabled = 1;
205
206         return snc_attach(dev);
207
208
209 static device_method_t snc_isa_methods[] = {
210         /* Device interface */
211         DEVMETHOD(device_probe,         snc_isa_probe),
212         DEVMETHOD(device_attach,        snc_isa_attach),
213         DEVMETHOD(device_shutdown,      snc_shutdown),
214
215         { 0, 0 }
216 };
217
218 static driver_t snc_isa_driver = {
219         "snc",
220         snc_isa_methods,
221         sizeof(struct snc_softc)
222 };
223
224 DRIVER_MODULE(if_snc, isa, snc_isa_driver, snc_devclass, 0, 0);