]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lnc/if_lnc_cbus.c
This commit was generated by cvs2svn to compensate for changes in r102528,
[FreeBSD/FreeBSD.git] / sys / dev / lnc / if_lnc_cbus.c
1 /*
2  * Copyright (c) 1994-2000
3  *      Paul Richards. All rights reserved.
4  *
5  * PC-98 port by Chiharu Shibata & FreeBSD(98) porting team.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    verbatim and that no modifications are made prior to this
13  *    point in the file.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name Paul Richards may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41
42 #include <machine/clock.h>      /* DELAY() */
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50
51 #include <isa/isavar.h>
52
53 #include <dev/lnc/if_lncvar.h>
54 #include <dev/lnc/if_lncreg.h>
55
56 static struct isa_pnp_id lnc_pnp_ids[] = {
57         {0,     NULL}
58 };
59
60 static bus_addr_t lnc_ioaddr_cnet98s[CNET98S_IOSIZE] = {
61         0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007,
62         0x008, 0x009, 0x00a, 0x00b, 0x00c, 0x00d, 0x00e, 0x00f,
63         0x400, 0x401, 0x402, 0x403, 0x404, 0x405, 0x406, 0x407,
64         0x408, 0x409, 0x40a, 0x40b, 0x40c, 0x40d, 0x40e, 0x40f,
65 };
66
67 static int
68 lnc_legacy_probe(device_t dev)
69 {
70         struct lnc_softc *sc = device_get_softc(dev);
71         u_int16_t tmp;
72
73         sc->portrid = 0;
74         sc->portres = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &sc->portrid,
75                                           lnc_ioaddr_cnet98s, CNET98S_IOSIZE,
76                                           RF_ACTIVE);
77
78         if (! sc->portres) {
79                 device_printf(dev, "Failed to allocate I/O ports\n");
80                 lnc_release_resources(dev);
81                 return (ENXIO);
82         }
83
84         isa_load_resourcev(sc->portres, lnc_ioaddr_cnet98s, CNET98S_IOSIZE);
85
86         sc->lnc_btag = rman_get_bustag(sc->portres);
87         sc->lnc_bhandle = rman_get_bushandle(sc->portres);
88
89         /* Reset */
90         tmp = lnc_inw(CNET98S_RESET);
91         lnc_outw(CNET98S_RESET, tmp);
92         DELAY(500);
93
94         sc->rap = CNET98S_RAP;
95         sc->rdp = CNET98S_RDP;
96         sc->nic.mem_mode = DMA_FIXED;
97
98         if ((sc->nic.ic = lance_probe(sc))) {
99                 sc->nic.ident = CNET98S;
100                 device_set_desc(dev, "C-NET(98)S");
101                 lnc_release_resources(dev);
102                 return (0);
103         } else {
104                 lnc_release_resources(dev);
105                 return (ENXIO);
106         }
107 }
108
109 static int
110 lnc_isa_probe(device_t dev)
111 {
112         int pnp;
113
114         pnp = ISA_PNP_PROBE(device_get_parent(dev), dev, lnc_pnp_ids);
115         if (pnp == ENOENT) {
116                 /* It's not a PNP card, see if we support it by probing it */
117                 return (lnc_legacy_probe(dev));
118         } else if (pnp == ENXIO) {
119                 return (ENXIO);
120         } else {
121                 /* Found PNP card we support */
122                 return (0);
123         }
124 }
125
126 static void
127 lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
128 {
129         /* Do nothing */
130         return;
131 }
132
133 static int
134 lnc_isa_attach(device_t dev)
135 {
136         lnc_softc_t *sc = device_get_softc(dev);
137         int err = 0;
138         bus_size_t lnc_mem_size;
139
140         device_printf(dev, "Attaching %s\n", device_get_desc(dev));
141
142         sc->portrid = 0;
143         sc->portres = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &sc->portrid,
144                                           lnc_ioaddr_cnet98s, CNET98S_IOSIZE,
145                                           RF_ACTIVE);
146
147         if (! sc->portres) {
148                 device_printf(dev, "Failed to allocate I/O ports\n");
149                 lnc_release_resources(dev);
150                 return (ENXIO);
151         }
152
153         isa_load_resourcev(sc->portres, lnc_ioaddr_cnet98s, CNET98S_IOSIZE);
154
155         sc->lnc_btag = rman_get_bustag(sc->portres);
156         sc->lnc_bhandle = rman_get_bushandle(sc->portres);
157
158         sc->drqrid = 0;
159         sc->drqres = NULL;
160
161         if (isa_get_irq(dev) == -1)
162                 bus_set_resource(dev, SYS_RES_IRQ, 0,  6, 1);
163
164         sc->irqrid = 0;
165         sc->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irqrid, 0, ~0, 1,
166                                         RF_ACTIVE);
167
168         if (! sc->irqres) {
169                 device_printf(dev, "Failed to allocate irq\n");
170                 lnc_release_resources(dev);
171                 return (ENXIO);
172         }
173
174         err = bus_setup_intr(dev, sc->irqres, INTR_TYPE_NET, lncintr,
175                              sc, &sc->intrhand);
176
177         if (err) {
178                 device_printf(dev, "Failed to setup irq handler\n");
179                 lnc_release_resources(dev);
180                 return (err);
181         }
182
183         /* XXX temp setting for nic */
184         sc->nic.mem_mode = DMA_FIXED;
185         sc->nrdre  = NRDRE;
186         sc->ntdre  = NTDRE;
187
188         if (sc->nic.ident == CNET98S) {
189             sc->rap = CNET98S_RAP;
190             sc->rdp = CNET98S_RDP;
191         } else if (sc->nic.ident == NE2100) {
192             sc->rap = PCNET_RAP;
193             sc->rdp = PCNET_RDP;
194             sc->bdp = PCNET_BDP;
195         } else {
196             sc->rap = BICC_RAP;
197             sc->rdp = BICC_RDP;
198         }
199
200         /* Create a DMA tag describing the ring memory we need */
201
202         lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
203                          sizeof(struct host_ring_entry));
204
205         lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) +
206                         (NDESC(sc->ntdre) * TRANSBUFSIZE);
207
208         err = bus_dma_tag_create(NULL,                  /* parent */
209                                  4,                     /* alignement */
210                                  0,                     /* boundary */
211                                  BUS_SPACE_MAXADDR_24BIT,       /* lowaddr */
212                                  BUS_SPACE_MAXADDR,     /* highaddr */
213                                  NULL, NULL,            /* filter, filterarg */
214                                  lnc_mem_size,          /* segsize */
215                                  1,                     /* nsegments */
216                                  BUS_SPACE_MAXSIZE_32BIT,       /* maxsegsize */
217                                  0,                     /* flags */
218                                  &sc->dmat);
219
220         if (err) {
221                 device_printf(dev, "Can't create DMA tag\n");
222                 lnc_release_resources(dev);
223                 return (ENOMEM);
224         }
225
226         err = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
227                                BUS_DMA_NOWAIT, &sc->dmamap);
228
229         if (err) {
230                 device_printf(dev, "Couldn't allocate memory\n");
231                 lnc_release_resources(dev);
232                 return (ENOMEM);
233         }
234
235         err = bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring, lnc_mem_size,
236                         lnc_alloc_callback, sc->recv_ring, BUS_DMA_NOWAIT);
237
238         if (err) {
239                 device_printf(dev, "Couldn't load DMA map\n");
240                 lnc_release_resources(dev);
241                 return (ENOMEM);
242         }
243
244         /* Call generic attach code */
245         if (! lnc_attach_common(dev)) {
246                 device_printf(dev, "Generic attach code failed\n");
247                 lnc_release_resources(dev);
248                 return (ENXIO);
249         }
250
251         /*
252          * ISA Configuration
253          *
254          * XXX - Following parameters are Contec C-NET(98)S only.
255          *       So, check the Ethernet address here.
256          *
257          *       Contec uses 00 80 4c ?? ?? ??
258          */ 
259         if (sc->arpcom.ac_enaddr[0] == (u_char)0x00 &&
260             sc->arpcom.ac_enaddr[1] == (u_char)0x80 &&
261             sc->arpcom.ac_enaddr[2] == (u_char)0x4c) {
262                 lnc_outw(sc->rap, MSRDA);
263                 lnc_outw(CNET98S_IDP, 0x0006);
264                 lnc_outw(sc->rap, MSWRA);
265                 lnc_outw(CNET98S_IDP, 0x0006);
266 #ifdef DIAGNOSTIC
267                 lnc_outw(sc->rap, MC);
268                 printf("ISACSR2 = %x\n", lnc_inw(CNET98S_IDP));
269 #endif
270                 lnc_outw(sc->rap, LED1);
271                 lnc_outw(CNET98S_IDP, LED_PSE | LED_XMTE);
272                 lnc_outw(sc->rap, LED2);
273                 lnc_outw(CNET98S_IDP, LED_PSE | LED_RCVE);
274                 lnc_outw(sc->rap, LED3);
275                 lnc_outw(CNET98S_IDP, LED_PSE | LED_COLE);
276         }
277
278         return (0);
279 }
280
281 static int
282 lnc_isa_detach(device_t dev)
283 {
284         lnc_softc_t *sc = device_get_softc(dev);
285         int s = splimp();
286
287         ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
288         splx(s);
289
290         lnc_stop(sc);
291         lnc_release_resources(dev);
292
293         return (0);
294 }
295
296 static device_method_t lnc_isa_methods[] = {
297 /*      DEVMETHOD(device_identify,      lnc_isa_identify), */
298         DEVMETHOD(device_probe,         lnc_isa_probe),
299         DEVMETHOD(device_attach,        lnc_isa_attach),
300         DEVMETHOD(device_detach,        lnc_isa_detach),
301 #ifdef notyet
302         DEVMETHOD(device_suspend,       lnc_isa_suspend),
303         DEVMETHOD(device_resume,        lnc_isa_resume),
304         DEVMETHOD(device_shutdown,      lnc_isa_shutdown),
305 #endif
306         { 0, 0 }
307 };
308
309 static driver_t lnc_isa_driver = {
310         "lnc",
311         lnc_isa_methods,
312         sizeof(struct lnc_softc),
313 };
314
315 DRIVER_MODULE(if_lnc, isa, lnc_isa_driver, lnc_devclass, 0, 0);