f9778fe26d6f52465b715e7cece5dba2ac4c5f31
[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  * $DragonFly: src/sys/dev/netif/snc/Attic/if_snc_cbus.c,v 1.3 2003/08/07 21:17:05 dillon Exp $
29  */
30
31 /*
32  *      National Semiconductor  DP8393X SONIC Driver
33  *
34  *      This is the C-bus specific attachment on FreeBSD
35  *              written by Motomichi Matsuzaki <mzaki@e-mail.ne.jp>
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <machine/resource.h>
48
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_media.h>
53 #include <net/if_mib.h>
54
55 #include <bus/isa/isavar.h>
56 #include <bus/isa/pnpvar.h>
57 #include <sys/malloc.h>         /* as dependency for isa/isa_common.h */
58 #include <bus/isa/isa_common.h> /* for snc_isapnp_reconfig() */
59
60 #include "dp83932var.h"
61 #include "dp83932subr.h"
62 #include "if_sncreg.h"
63 #include "if_sncvar.h"
64
65 static void snc_isapnp_reconfig __P((device_t));
66 static int snc_isa_probe        __P((device_t));
67 static int snc_isa_attach       __P((device_t));
68
69 static struct isa_pnp_id snc_ids[] = {
70         { 0x6180a3b8,   NULL },         /* NEC8061 NEC PC-9801-104 */
71         { 0,            NULL }
72 };
73
74 static void
75 snc_isapnp_reconfig(dev)
76         device_t dev;
77 {
78         struct isa_device *idev = DEVTOISA(dev);
79         struct isa_config config;
80         u_long start, count;
81         int rid;
82
83         bzero(&config, sizeof(config));
84
85         for (rid = 0; rid < ISA_NMEM; rid++) {
86                 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count))
87                         break;
88                 config.ic_mem[rid].ir_start = start;
89                 config.ic_mem[rid].ir_end = start;
90                 config.ic_mem[rid].ir_size = count;
91         }
92         config.ic_nmem = rid;
93         for (rid = 0; rid < ISA_NPORT; rid++) {
94                 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count))
95                         break;
96                 config.ic_port[rid].ir_start = start;
97                 config.ic_port[rid].ir_end = start;
98                 config.ic_port[rid].ir_size = count;
99         }
100         config.ic_nport = rid;
101         for (rid = 0; rid < ISA_NIRQ; rid++) {
102                 if (bus_get_resource(dev, SYS_RES_IRQ, rid, &start, &count))
103                         break;
104                 config.ic_irqmask[rid] = 1 << start;
105         }
106         config.ic_nirq = rid;
107         for (rid = 0; rid < ISA_NDRQ; rid++) {
108                 if (bus_get_resource(dev, SYS_RES_DRQ, rid, &start, &count))
109                         break;
110                 config.ic_drqmask[rid] = 1 << start;
111         }
112         config.ic_ndrq = rid;
113         
114         idev->id_config_cb(idev->id_config_arg, &config, 1);
115 }
116
117 static int
118 snc_isa_probe(dev)
119         device_t dev;
120 {
121         struct snc_softc *sc = device_get_softc(dev);
122         int type;
123         int error = 0;
124
125         bzero(sc, sizeof(struct snc_softc));
126
127         /* Check isapnp ids */
128         error = ISA_PNP_PROBE(device_get_parent(dev), dev, snc_ids);
129
130         /* If the card had a PnP ID that didn't match any we know about */
131         if (error == ENXIO) {
132                 return(error);
133         }
134
135         switch (error) {
136         case 0:         /* Matched PnP */
137                 type = SNEC_TYPE_PNP;
138                 break;
139
140         case ENOENT:    /* Legacy ISA */
141                 type = SNEC_TYPE_LEGACY;
142                 break;
143
144         default:        /* If we had some other problem. */
145                 return(error);
146         }
147
148         if (type == SNEC_TYPE_PNP && isa_get_portsize(dev) == 0) {
149                 int port;
150                 int rid = 0;
151                 struct resource *res = NULL;
152
153                 for (port = 0x0888; port <= 0x3888; port += 0x1000) {
154                         bus_set_resource(dev, SYS_RES_IOPORT, rid,
155                                          port, SNEC_NREGS);
156                         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
157                                                  0, ~0, SNEC_NREGS,
158                                                  0 /* !RF_ACTIVE */);
159                         if (res) break;
160                 }
161
162                 printf("snc_isa_probe: broken PnP resource, ");
163                 if (res) {
164                         printf("use port 0x%x\n", port);
165                         bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
166                         snc_isapnp_reconfig(dev);
167                 } else {
168                         printf("and can't find port\n");
169                 }
170         }
171
172         error = snc_alloc_port(dev, 0);
173         error = max(error, snc_alloc_memory(dev, 0));
174         error = max(error, snc_alloc_irq(dev, 0, 0));
175
176         if (!error && !snc_probe(dev, type))
177                 error = ENOENT;
178
179         snc_release_resources(dev);
180         return (error);
181 }
182
183 static int
184 snc_isa_attach(dev)
185         device_t dev;
186 {
187         struct snc_softc *sc = device_get_softc(dev);
188         int error;
189         
190         bzero(sc, sizeof(struct snc_softc));
191
192         snc_alloc_port(dev, 0);
193         snc_alloc_memory(dev, 0);
194         snc_alloc_irq(dev, 0, 0);
195                 
196         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
197                                sncintr, sc, &sc->irq_handle);
198         if (error) {
199                 printf("snc_isa_attach: bus_setup_intr() failed\n");
200                 snc_release_resources(dev);
201                 return (error);
202         }              
203
204         /* This interface is always enabled. */
205         sc->sc_enabled = 1;
206
207         return snc_attach(dev);
208
209
210 static device_method_t snc_isa_methods[] = {
211         /* Device interface */
212         DEVMETHOD(device_probe,         snc_isa_probe),
213         DEVMETHOD(device_attach,        snc_isa_attach),
214         DEVMETHOD(device_shutdown,      snc_shutdown),
215
216         { 0, 0 }
217 };
218
219 static driver_t snc_isa_driver = {
220         "snc",
221         snc_isa_methods,
222         sizeof(struct snc_softc)
223 };
224
225 DRIVER_MODULE(if_snc, isa, snc_isa_driver, snc_devclass, 0, 0);