kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / ar / if_ar_pci.c
1 /*
2  * Copyright (c) 1999 - 2001 John Hay.
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  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/ar/if_ar_pci.c,v 1.6.2.1 2002/06/17 15:10:57 jhay Exp $
30  * $DragonFly: src/sys/dev/netif/ar/if_ar_pci.c,v 1.3 2003/08/07 21:16:59 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <machine/bus_pio.h>
41 #include <machine/bus_memio.h>
42 #include <sys/rman.h>
43
44 #include <bus/pci/pcireg.h>
45 #include <bus/pci/pcivar.h>
46
47 #include "../ic_layer/hd64570.h"
48 #include "if_arregs.h"
49
50 #ifdef TRACE
51 #define TRC(x)               x
52 #else
53 #define TRC(x)
54 #endif
55
56 #define TRCL(x)              x
57
58 static int      ar_pci_probe(device_t);
59 static int      ar_pci_attach(device_t);
60
61 static device_method_t ar_pci_methods[] = {
62         /* Device interface */
63         DEVMETHOD(device_probe,         ar_pci_probe),
64         DEVMETHOD(device_attach,        ar_pci_attach),
65         DEVMETHOD(device_detach,        ar_detach),
66         { 0, 0 }
67 };
68
69 static driver_t ar_pci_driver = {
70         "ar",
71         ar_pci_methods,
72         sizeof(struct ar_hardc),
73 };
74
75 DRIVER_MODULE(if_ar, pci, ar_pci_driver, ar_devclass, 0, 0);
76
77 static int
78 ar_pci_probe(device_t device)
79 {
80         u_int32_t       type = pci_get_devid(device);
81
82         switch(type) {
83         case 0x5012114f:
84                 device_set_desc(device, "Digi SYNC/570i-PCI 2 port");
85                 return (0);
86                 break;
87         case 0x5010114f:
88                 printf("Digi SYNC/570i-PCI 2 port (mapped below 1M)\n");
89                 printf("Please change the jumper to select linear mode.\n");
90                 break;
91         case 0x5013114f:
92                 device_set_desc(device, "Digi SYNC/570i-PCI 4 port");
93                 return (0);
94                 break;
95         case 0x5011114f:
96                 printf("Digi SYNC/570i-PCI 4 port (mapped below 1M)\n");
97                 printf("Please change the jumper to select linear mode.\n");
98                 break;
99         default:
100                 break;
101         }
102         return (ENXIO);
103 }
104
105 static int
106 ar_pci_attach(device_t device)
107 {
108         int error;
109         u_int i, tmp;
110         u_char *inten;
111         struct ar_hardc *hc;
112
113         hc = (struct ar_hardc *)device_get_softc(device);
114         bzero(hc, sizeof(struct ar_hardc));
115
116         error = ar_allocate_plx_memory(device, 0x10, 1);
117         if(error)
118                 goto errexit;
119
120         error = ar_allocate_memory(device, 0x18, 1);
121         if(error)
122                 goto errexit;
123
124         error = ar_allocate_irq(device, 0, 1);
125         if(error)
126                 goto errexit;
127
128         hc->plx_mem = rman_get_virtual(hc->res_plx_memory);
129         hc->mem_start = rman_get_virtual(hc->res_memory);
130
131         hc->cunit = device_get_unit(device);
132         hc->sca[0] = (sca_regs *)(hc->mem_start + AR_PCI_SCA_1_OFFSET);
133         hc->sca[1] = (sca_regs *)(hc->mem_start + AR_PCI_SCA_2_OFFSET);
134         hc->iobase = 0;
135         hc->orbase = (u_char *)(hc->mem_start + AR_PCI_ORBASE_OFFSET);
136
137         tmp = hc->orbase[AR_BMI * 4];
138         hc->bustype = tmp & AR_BUS_MSK;
139         hc->memsize = (tmp & AR_MEM_MSK) >> AR_MEM_SHFT;
140         hc->memsize = 1 << hc->memsize;
141         hc->memsize <<= 16;
142         hc->interface[0] = (tmp & AR_IFACE_MSK);
143         tmp = hc->orbase[AR_REV * 4];
144         hc->revision = tmp & AR_REV_MSK;
145         hc->winsize = (1 << ((tmp & AR_WSIZ_MSK) >> AR_WSIZ_SHFT)) * 16 * 1024;
146         hc->mem_end = (caddr_t)(hc->mem_start + hc->winsize);
147         hc->winmsk = hc->winsize - 1;
148         hc->numports = hc->orbase[AR_PNUM * 4];
149         hc->handshake = hc->orbase[AR_HNDSH * 4];
150
151         for(i = 1; i < hc->numports; i++)
152                 hc->interface[i] = hc->interface[0];
153
154         TRC(printf("arp%d: bus %x, rev %d, memstart %p, winsize %d, "
155             "winmsk %x, interface %x\n",
156             unit, hc->bustype, hc->revision, hc->mem_start, hc->winsize,
157             hc->winmsk, hc->interface[0]));
158
159         ar_attach(device);
160
161         /* Magic to enable the card to generate interrupts. */
162         inten = (u_char *)hc->plx_mem;
163         inten[0x69] = 0x09;
164
165         return (0);
166
167 errexit:
168         ar_deallocate_resources(device);
169         return (ENXIO);
170 }
171