proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / dev / video / tga / tga_pci.c
1 /*
2  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
3  * All rights reserved.
4  *
5  * Author: Chris G. Demetriou
6  * 
7  * Permission to use, copy, modify and distribute this software and
8  * its documentation is hereby granted, provided that both the copyright
9  * notice and this permission notice appear in all copies of the
10  * software, derivative works or modified versions, and any portions
11  * thereof, and that both notices appear in supporting documentation.
12  * 
13  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 
14  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 
15  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16  * 
17  * Carnegie Mellon requests users of this software to return to
18  *
19  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
20  *  School of Computer Science
21  *  Carnegie Mellon University
22  *  Pittsburgh PA 15213-3890
23  *
24  * any improvements or extensions that they make and grant Carnegie the
25  * rights to redistribute these changes.
26  *
27  * Copyright (c) 2000 Andrew Miklic, Andrew Gallatin, and Thomas V. Crimi
28  *
29  * $FreeBSD: src/sys/dev/tga/tga_pci.c,v 1.1.2.1 2001/11/01 08:33:15 obrien Exp $
30  * $DragonFly: src/sys/dev/video/tga/Attic/tga_pci.c,v 1.2 2003/06/17 04:28:32 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/proc.h>
38 #include <sys/fcntl.h>
39 #include <sys/malloc.h>
40 #include <sys/fbio.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45
46 #include <machine/md_var.h>
47 #include <machine/pc/bios.h>
48 #include <machine/clock.h>
49 #include <machine/bus_memio.h>
50 #include <machine/bus.h>
51 #include <machine/pc/vesa.h>
52 #include <machine/resource.h>
53
54 #include <sys/bus.h>
55 #include <sys/rman.h>
56
57 #include <pci/pcireg.h>
58 #include <pci/pcivar.h>
59
60 #include <dev/fb/fbreg.h>
61 #include <dev/fb/tga.h>
62 #include <dev/tga/tga_pci.h>
63 #include <dev/fb/gfb.h>
64 #include <dev/gfb/gfb_pci.h>
65
66 #include "opt_fb.h"
67
68 static int tga_probe(device_t);
69 static int tga_attach(device_t);
70 static void tga_intr(void *);
71
72 static device_method_t tga_methods[] = {
73         DEVMETHOD(device_probe, tga_probe),
74         DEVMETHOD(device_attach, tga_attach),
75         DEVMETHOD(device_detach, pcigfb_detach),
76         { 0, 0 }
77 };
78
79 static driver_t tga_driver = {
80         "tga",
81         tga_methods,
82         sizeof(struct gfb_softc)
83 };
84
85 static devclass_t tga_devclass;
86
87 DRIVER_MODULE(tga, pci, tga_driver, tga_devclass, 0, 0);
88
89 static struct gfb_type tga_devs[] = {
90         { DEC_VENDORID, DEC_DEVICEID_TGA,
91         "DEC TGA (21030) 2D Graphics Accelerator" },
92         { DEC_VENDORID, DEC_DEVICEID_TGA2,
93         "DEC TGA2 (21130) 3D Graphics Accelerator" },
94         { 0, 0, NULL }
95 };
96
97 #ifdef FB_INSTALL_CDEV
98
99 static struct cdevsw tga_cdevsw = {
100         /* open */      pcigfb_open,
101         /* close */     pcigfb_close,
102         /* read */      pcigfb_read,
103         /* write */     pcigfb_write,
104         /* ioctl */     pcigfb_ioctl,
105         /* poll */      nopoll,
106         /* mmap */      pcigfb_mmap,
107         /* strategy */  nostrategy,
108         /* name */      "tga",
109         /* maj */       -1,
110         /* dump */      nodump,
111         /* psize */     nopsize,
112         /* flags */     0,
113         /* bmaj */      -1
114 };
115
116 #endif /*FB_INSTALL_CDEV*/
117
118 static int
119 tga_probe(device_t dev)
120 {
121         int error;
122         gfb_type_t t;
123
124         t = tga_devs;
125         error = ENXIO;
126         while(t->name != NULL) {
127                 if((pci_get_vendor(dev) == t->vendor_id) &&
128                    (pci_get_device(dev) == t->device_id)) {
129                         device_set_desc(dev, t->name);
130                         error = 0;
131                         break;
132                 }
133                 t++;
134         }
135         return(error);
136 }
137
138 static int
139 tga_attach(device_t dev)
140 {
141         gfb_softc_t sc;
142         int unit, error, rid;
143
144         error = 0;
145         unit = device_get_unit(dev);
146         sc = device_get_softc(dev);
147         bzero(sc, sizeof(struct gfb_softc));
148         sc->driver_name = TGA_DRIVER_NAME;
149         switch(pci_get_device(dev)) {
150         case DEC_DEVICEID_TGA2:
151                 sc->model = 1;
152                 sc->type = KD_TGA2;
153                 break;
154         case DEC_DEVICEID_TGA:
155                 sc->model = 0;
156                 sc->type = KD_TGA;
157                 break;
158         default:
159                 device_printf(dev, "Unrecognized TGA type\n");
160                 goto fail;
161         }
162         if((error = pcigfb_attach(dev))) {
163                 goto fail;
164         }
165         sc->regs = sc->bhandle + TGA_MEM_CREGS;
166         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_TTY, tga_intr, sc,
167                     &sc->intrhand);
168         if(error) {
169                 device_printf(dev, "couldn't set up irq\n");
170                 goto fail;
171         }
172         switch(sc->rev) {
173         case 0x1:
174         case 0x2:
175         case 0x3:
176                 device_printf(dev, "TGA (21030) step %c\n", 'A' + sc->rev - 1);
177                 break;
178
179         case 0x20:
180                 device_printf(dev, "TGA2 (21130) abstract software model\n");
181                 break;
182
183         case 0x21:
184         case 0x22:
185                 device_printf(dev, "TGA2 (21130) pass %d\n", sc->rev - 0x20);
186                 break;
187
188         default:
189                 device_printf(dev, "Unknown stepping (0x%x)\n", sc->rev);
190                 break;
191         }
192 #ifdef FB_INSTALL_CDEV
193         sc->cdevsw = &tga_cdevsw;
194         sc->devt = make_dev(sc->cdevsw, unit, 0, 0, 02660, "tga%x", unit);
195 #endif /*FB_INSTALL_CDEV*/
196         goto done;
197 fail:
198         if(sc->intrhand != NULL) {
199                 bus_teardown_intr(dev, sc->irq, sc->intrhand);
200                 sc->intrhand = NULL;
201         }
202         if(sc->irq != NULL) {
203                 rid = 0x0;
204                 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq);
205                 sc->irq = NULL;
206         }
207         if(sc->res != NULL) {
208                 rid = GFB_MEM_BASE_RID;
209                 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res);
210                 sc->res = NULL;
211         }
212         error = ENXIO;
213 done:
214         return(error);
215 }
216
217 static void 
218 tga_intr(void *v)
219 {
220         struct gfb_softc *sc = (struct gfb_softc *)v;
221         u_int32_t reg;
222
223         reg = READ_GFB_REGISTER(sc->adp, TGA_REG_SISR);
224         if((reg & 0x00010001) != 0x00010001) {
225
226                 /* Odd. We never set any of the other interrupt enables. */
227                 if((reg & 0x1f) != 0) {
228
229                         /* Clear the mysterious pending interrupts. */
230                         WRITE_GFB_REGISTER(sc->adp, TGA_REG_SISR, (reg & 0x1f));
231                         GFB_REGISTER_WRITE_BARRIER(sc, TGA_REG_SISR, 1);
232
233                         /* This was our interrupt, even if we're puzzled as to
234                          * why we got it.  Don't make the interrupt handler
235                          * think it was a stray.
236                          */
237                 }
238         }
239
240         /* Call the scheduled handler... */
241         sc->gfbc->ramdac_intr(sc);
242
243         /*
244            Clear interrupt field (this way, we will force a
245            memory error if we get an unexpected interrupt)...
246         */
247         sc->gfbc->ramdac_intr = NULL;
248
249         /* Disable the interrupt... */
250         WRITE_GFB_REGISTER(sc->adp, TGA_REG_SISR, 0x00000001);
251         GFB_REGISTER_WRITE_BARRIER(sc, TGA_REG_SISR, 1);
252 }