Merge from vendor branch SENDMAIL:
[dragonfly.git] / sys / dev / netif / sr / if_sr_pci.c
1 /*
2  * Copyright (c) 1996 - 2001 John Hay.
3  * Copyright (c) 1996 SDL Communications, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following 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  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/sr/if_sr_pci.c,v 1.15.2.1 2002/06/17 15:10:58 jhay Exp $
31  * $DragonFly: src/sys/dev/netif/sr/if_sr_pci.c,v 1.4 2005/08/29 10:19:52 sephe Exp $
32  */
33
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <machine/bus_pio.h>
43 #include <machine/bus_memio.h>
44 #include <sys/rman.h>
45
46 #include <bus/pci/pcivar.h>
47 #include <machine/md_var.h>
48
49 #include "../ic_layer/hd64570.h"
50 #include "if_srregs.h"
51
52 #ifndef BUGGY
53 #define BUGGY           0
54 #endif
55
56 static int      sr_pci_probe(device_t);
57 static int      sr_pci_attach(device_t);
58
59 static device_method_t sr_pci_methods[] = {
60         /* Device interface */
61         DEVMETHOD(device_probe,         sr_pci_probe),
62         DEVMETHOD(device_attach,        sr_pci_attach),
63         DEVMETHOD(device_detach,        sr_detach),
64         { 0, 0 }
65 };
66
67 static driver_t sr_pci_driver = {
68         "sr",
69         sr_pci_methods,
70         sizeof(struct sr_hardc),
71 };
72
73 DRIVER_MODULE(if_sr, pci, sr_pci_driver, sr_devclass, 0, 0);
74
75 static u_int    src_get8_mem(u_int base, u_int off);
76 static u_int    src_get16_mem(u_int base, u_int off);
77 static void     src_put8_mem(u_int base, u_int off, u_int val);
78 static void     src_put16_mem(u_int base, u_int off, u_int val);
79
80 static int
81 sr_pci_probe(device_t device)
82 {
83         u_int32_t       type = pci_get_devid(device);
84
85         switch(type) {
86         case 0x556812aa:
87                 device_set_desc(device, "RISCom/N2pci");
88                 return (0);
89                 break;
90         case 0x55684778:
91         case 0x55684877:
92                 /*
93                  * XXX This can probably be removed sometime.
94                  */
95                 device_set_desc(device, "RISCom/N2pci (old id)");
96                 return (0);
97                 break;
98         default:
99                 break;
100         }
101         return (ENXIO);
102 }
103
104 static int
105 sr_pci_attach(device_t device)
106 {
107         int numports;
108         u_int fecr, *fecrp;
109         struct sr_hardc *hc;
110
111         hc = (struct sr_hardc *)device_get_softc(device);
112
113         if (sr_allocate_plx_memory(device, 0x10, 1))
114                 goto errexit;
115
116         if (sr_allocate_memory(device, 0x18, 1))
117                 goto errexit;
118
119         if (sr_allocate_irq(device, 0, 1))
120                 goto errexit;
121
122         hc->plx_base = rman_get_virtual(hc->res_plx_memory);
123         hc->sca_base = (vm_offset_t)rman_get_virtual(hc->res_memory);
124
125         hc->cunit = device_get_unit(device);
126
127
128         /*
129          * Configure the PLX. This is magic. I'm doing it just like I'm told
130          * to. :-)
131          * 
132          * offset
133          *  0x00 - Map Range    - Mem-mapped to locate anywhere
134          *  0x04 - Re-Map       - PCI address decode enable
135          *  0x18 - Bus Region   - 32-bit bus, ready enable
136          *  0x1c - Master Range - include all 16 MB
137          *  0x20 - Master RAM   - Map SCA Base at 0
138          *  0x28 - Master Remap - direct master memory enable
139          *  0x68 - Interrupt    - Enable interrupt (0 to disable)
140          * 
141          * Note: This is "cargo cult" stuff.  - jrc
142          */
143         *((u_int *)(hc->plx_base + 0x00)) = 0xfffff000;
144         *((u_int *)(hc->plx_base + 0x04)) = 1;
145         *((u_int *)(hc->plx_base + 0x18)) = 0x40030043;
146         *((u_int *)(hc->plx_base + 0x1c)) = 0xff000000;
147         *((u_int *)(hc->plx_base + 0x20)) = 0;
148         *((u_int *)(hc->plx_base + 0x28)) = 0xe9;
149         *((u_int *)(hc->plx_base + 0x68)) = 0x10900;
150
151         /*
152          * Get info from card.
153          *
154          * Only look for the second port if the first exists. Too many things
155          * will break if we have only a second port.
156          */
157         fecrp = (u_int *)(hc->sca_base + SR_FECR);
158         fecr = *fecrp;
159         numports = 0;
160
161         if (((fecr & SR_FECR_ID0) >> SR_FE_ID0_SHFT) != SR_FE_ID_NONE) {
162                 numports++;
163                 if (((fecr & SR_FECR_ID1) >> SR_FE_ID1_SHFT) != SR_FE_ID_NONE)
164                         numports++;
165         }
166         if (numports == 0)
167                 goto errexit;
168
169         hc->numports = numports;
170         hc->cardtype = SR_CRD_N2PCI;
171
172         hc->src_put8 = src_put8_mem;
173         hc->src_put16 = src_put16_mem;
174         hc->src_get8 = src_get8_mem;
175         hc->src_get16 = src_get16_mem;
176
177         /*
178          * Malloc area for tx and rx buffers. For now allocate SRC_WIN_SIZ
179          * (16k) for each buffer.
180          *
181          * Allocate the block below 16M because the N2pci card can only access
182          * 16M memory at a time.
183          *
184          * (We could actually allocate a contiguous block above the 16MB limit,
185          * but this would complicate card programming more than we want to
186          * right now -jrc)
187          */
188         hc->memsize = 2 * hc->numports * SRC_WIN_SIZ;
189         hc->mem_start = contigmalloc(hc->memsize,
190                                      M_DEVBUF,
191                                      M_NOWAIT,
192                                      0ul,
193                                      0xfffffful,
194                                      0x10000,
195                                      0x1000000);
196
197         if (hc->mem_start == NULL) {
198                 printf("src%d: pci: failed to allocate buffer space.\n",
199                     hc->cunit);
200                 goto errexit;
201         }
202         hc->winmsk = 0xffffffff;
203         hc->mem_end = (caddr_t)((u_int)hc->mem_start + hc->memsize);
204         hc->mem_pstart = kvtop(hc->mem_start);
205         bzero(hc->mem_start, hc->memsize);
206
207         *fecrp = SR_FECR_DTR0
208             | SR_FECR_DTR1
209             | SR_FECR_TE0
210             | SR_FECR_TE1;
211
212         if (sr_attach(device))
213                 goto errexit;
214
215         return (0);
216 errexit:
217         sr_deallocate_resources(device);
218         return (ENXIO);
219 }
220
221 /*
222  * I/O for PCI N2 card(s)
223  */
224 #define SRC_PCI_SCA_REG(y)      ((y & 2) ? ((y & 0xfd) + 0x100) : y)
225
226 static u_int
227 src_get8_mem(u_int base, u_int off)
228 {
229         return *((u_char *)(base + SRC_PCI_SCA_REG(off)));
230 }
231
232 static u_int
233 src_get16_mem(u_int base, u_int off)
234 {
235         return *((u_short *)(base + SRC_PCI_SCA_REG(off)));
236 }
237
238 static void
239 src_put8_mem(u_int base, u_int off, u_int val)
240 {
241         *((u_char *)(base + SRC_PCI_SCA_REG(off))) = (u_char)val;
242 }
243
244 static void
245 src_put16_mem(u_int base, u_int off, u_int val)
246 {
247         *((u_short *)(base + SRC_PCI_SCA_REG(off))) = (u_short)val;
248 }