]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/snc/if_snc.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / snc / if_snc.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  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33  *      National Semiconductor  DP8393X SONIC Driver
34  *
35  *      This is the bus independent attachment on FreeBSD 4.x
36  *              written by Motomichi Matsuzaki <mzaki@e-mail.ne.jp>
37  */
38
39 #include <sys/param.h>
40 #include <sys/socket.h>
41
42 #include <sys/bus.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <machine/resource.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_media.h>
51
52 #include <dev/snc/dp83932reg.h>
53 #include <dev/snc/dp83932var.h>
54 #include <dev/snc/dp83932subr.h>
55 #include <dev/snc/if_sncreg.h>
56 #include <dev/snc/if_sncvar.h>
57
58 /* devclass for "snc" */
59 devclass_t snc_devclass;
60
61 /****************************************************************
62   Resource management functions
63  ****************************************************************/
64
65 /*
66  * Allocate a port resource with the given resource id.
67  */
68 int
69 snc_alloc_port(dev, rid)
70         device_t dev;
71         int rid;
72 {
73         struct snc_softc *sc = device_get_softc(dev);
74         struct resource *res;
75
76         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
77                                  0ul, ~0ul, SNEC_NREGS, RF_ACTIVE);
78         if (res) {
79                 sc->ioport = res;
80                 sc->ioport_rid = rid;
81                 sc->sc_iot = rman_get_bustag(res);
82                 sc->sc_ioh = rman_get_bushandle(res);
83                 return (0);
84         } else {
85                 device_printf(dev, "can't assign port\n");
86                 return (ENOENT);
87         }
88 }
89
90 /*
91  * Allocate a memory resource with the given resource id.
92  */
93 int
94 snc_alloc_memory(dev, rid)
95         device_t dev;
96         int rid;
97 {
98         struct snc_softc *sc = device_get_softc(dev);
99         struct resource *res;
100
101         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
102                                  0ul, ~0ul, SNEC_NMEMS, RF_ACTIVE);
103         if (res) {
104                 sc->iomem = res;
105                 sc->iomem_rid = rid;
106                 sc->sc_memt = rman_get_bustag(res);
107                 sc->sc_memh = rman_get_bushandle(res);
108                 return (0);
109         } else {
110                 device_printf(dev, "can't assign memory\n");
111                 return (ENOENT);
112         }
113 }
114
115 /*
116  * Allocate an irq resource with the given resource id.
117  */
118 int
119 snc_alloc_irq(dev, rid, flags)
120         device_t dev;
121         int rid;
122         int flags;
123 {
124         struct snc_softc *sc = device_get_softc(dev);
125         struct resource *res;
126
127         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
128         if (res) {
129                 sc->irq = res;
130                 sc->irq_rid = rid;
131                 return (0);
132         } else {
133                 device_printf(dev, "can't assign irq\n");
134                 return (ENOENT);
135         }
136 }
137
138 /*
139  * Release all resources
140  */
141 void
142 snc_release_resources(dev)
143         device_t dev;
144 {
145         struct snc_softc *sc = device_get_softc(dev);
146
147         if (sc->ioport) {
148                 bus_release_resource(dev, SYS_RES_IOPORT,
149                                      sc->ioport_rid, sc->ioport);
150                 sc->ioport = 0;
151         }
152         if (sc->iomem) {
153                 bus_release_resource(dev, SYS_RES_MEMORY,
154                                      sc->iomem_rid, sc->iomem);
155                 sc->iomem = 0;
156         }
157         if (sc->irq) {
158                 bus_release_resource(dev, SYS_RES_IRQ,
159                                      sc->irq_rid, sc->irq);
160                 sc->irq = 0;
161         }
162         if (sc->sc_ifp) {
163                 if_free(sc->sc_ifp);
164                 sc->sc_ifp = 0;
165         }
166 }
167
168 /****************************************************************
169   Probe routine
170  ****************************************************************/
171
172 int
173 snc_probe(dev, type)
174      device_t dev;
175      int type;
176 {
177         struct snc_softc *sc = device_get_softc(dev);
178
179         return snc_nec16_detectsubr(sc->sc_iot, sc->sc_ioh,
180                                     sc->sc_memt, sc->sc_memh,
181                                     rman_get_start(sc->irq),
182                                     rman_get_start(sc->iomem),
183                                     type);
184 }
185
186 /****************************************************************
187   Attach routine
188  ****************************************************************/
189
190 int
191 snc_attach(dev)
192         device_t dev;
193 {
194         struct snc_softc *sc = device_get_softc(dev);
195         u_int8_t myea[ETHER_ADDR_LEN];
196         int error;
197
198         if (snc_nec16_register_irq(sc, rman_get_start(sc->irq)) == 0 || 
199             snc_nec16_register_mem(sc, rman_get_start(sc->iomem)) == 0) {
200                 snc_release_resources(dev);
201                 return(ENOENT);
202         }
203
204         snc_nec16_get_enaddr(sc->sc_iot, sc->sc_ioh, myea);
205         device_printf(dev, "%s Ethernet\n", snc_nec16_detect_type(myea));
206
207         sc->sc_dev = dev;
208
209         sc->sncr_dcr = DCR_SYNC | DCR_WAIT0 |
210             DCR_DMABLOCK | DCR_RFT16 | DCR_TFT28;
211         sc->sncr_dcr2 = 0;      /* XXX */
212         sc->bitmode = 0;        /* 16 bit card */
213
214         sc->sc_nic_put = snc_nec16_nic_put;
215         sc->sc_nic_get = snc_nec16_nic_get;
216         sc->sc_writetodesc = snc_nec16_writetodesc;
217         sc->sc_readfromdesc = snc_nec16_readfromdesc;
218         sc->sc_copytobuf = snc_nec16_copytobuf;
219         sc->sc_copyfrombuf = snc_nec16_copyfrombuf;
220         sc->sc_zerobuf = snc_nec16_zerobuf;
221
222         /* sncsetup returns 1 if something fails */
223         if (sncsetup(sc, myea)) {
224                 snc_release_resources(dev);
225                 return(ENOENT);
226         }
227
228         mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
229             MTX_DEF);
230         callout_init_mtx(&sc->sc_timer, &sc->sc_lock, 0);
231         error = sncconfig(sc, NULL, 0, 0, myea);
232         if (error) {
233                 snc_release_resources(dev);
234                 mtx_destroy(&sc->sc_lock);
235                 return (error);
236         }
237
238         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
239                                NULL, sncintr, sc, &sc->irq_handle);
240         if (error) {
241                 printf("snc_isa_attach: bus_setup_intr() failed\n");
242                 ether_ifdetach(sc->sc_ifp);             
243                 snc_release_resources(dev);
244                 mtx_destroy(&sc->sc_lock);
245                 return (error);
246         }
247
248         return 0;
249 }
250
251 /****************************************************************
252   Shutdown routine
253  ****************************************************************/
254
255 int
256 snc_shutdown(dev)
257         device_t dev;
258 {
259         struct snc_softc *sc = device_get_softc(dev);
260
261         SNC_LOCK(sc);
262         sncshutdown(sc);
263         SNC_UNLOCK(sc);
264
265         return (0);
266 }