gten.c revision 1.2.10.4 1 1.2.10.2 nathanw /* $NetBSD: gten.c,v 1.2.10.4 2002/10/18 02:39:34 nathanw Exp $ */
2 1.2 matt
3 1.2 matt /*-
4 1.2 matt * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.2 matt * All rights reserved.
6 1.2 matt *
7 1.2 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.2 matt * by Matt Thomas <matt (at) 3am-software.com>
9 1.2 matt *
10 1.2 matt * Redistribution and use in source and binary forms, with or without
11 1.2 matt * modification, are permitted provided that the following conditions
12 1.2 matt * are met:
13 1.2 matt * 1. Redistributions of source code must retain the above copyright
14 1.2 matt * notice, this list of conditions and the following disclaimer.
15 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 matt * notice, this list of conditions and the following disclaimer in the
17 1.2 matt * documentation and/or other materials provided with the distribution.
18 1.2 matt * 3. All advertising materials mentioning features or use of this software
19 1.2 matt * must display the following acknowledgement:
20 1.2 matt * This product includes software developed by the NetBSD
21 1.2 matt * Foundation, Inc. and its contributors.
22 1.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 matt * contributors may be used to endorse or promote products derived
24 1.2 matt * from this software without specific prior written permission.
25 1.2 matt *
26 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
37 1.2 matt */
38 1.1 matt
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/buf.h>
41 1.1 matt #include <sys/conf.h>
42 1.1 matt #include <sys/device.h>
43 1.1 matt #include <sys/ioctl.h>
44 1.1 matt #include <sys/kernel.h>
45 1.1 matt #include <sys/malloc.h>
46 1.1 matt #include <sys/systm.h>
47 1.1 matt
48 1.1 matt #include <uvm/uvm_extern.h>
49 1.1 matt
50 1.1 matt #include <dev/pci/pcidevs.h>
51 1.1 matt #include <dev/pci/pcireg.h>
52 1.1 matt #include <dev/pci/pcivar.h>
53 1.1 matt
54 1.1 matt #include <dev/wscons/wsconsio.h>
55 1.1 matt #include <dev/wscons/wsdisplayvar.h>
56 1.1 matt #include <dev/rasops/rasops.h>
57 1.1 matt
58 1.1 matt #include <machine/bus.h>
59 1.1 matt #include <machine/gtenvar.h>
60 1.1 matt
61 1.2 matt static int gten_match (struct device *, struct cfdata *, void *);
62 1.2 matt static void gten_attach (struct device *, struct device *, void *);
63 1.2 matt static int gten_print (void *, const char *);
64 1.1 matt
65 1.2.10.4 nathanw CFATTACH_DECL(gten, sizeof(struct gten_softc),
66 1.2.10.4 nathanw gten_match, gten_attach, NULL, NULL);
67 1.1 matt
68 1.1 matt static struct rasops_info gten_console_ri;
69 1.1 matt static pcitag_t gten_console_pcitag;
70 1.1 matt
71 1.1 matt static struct wsscreen_descr gten_stdscreen = {
72 1.1 matt "std",
73 1.1 matt 0, 0,
74 1.1 matt 0,
75 1.1 matt 0, 0,
76 1.1 matt WSSCREEN_REVERSE
77 1.1 matt };
78 1.1 matt
79 1.1 matt static const struct wsscreen_descr *_gten_scrlist[] = {
80 1.1 matt >en_stdscreen,
81 1.1 matt /* XXX other formats, graphics screen? */
82 1.1 matt };
83 1.1 matt
84 1.1 matt static struct wsscreen_list gten_screenlist = {
85 1.1 matt sizeof(_gten_scrlist) / sizeof(struct wsscreen_descr *), _gten_scrlist
86 1.1 matt };
87 1.1 matt
88 1.1 matt static int gten_ioctl (void *, u_long, caddr_t, int, struct proc *);
89 1.1 matt static paddr_t gten_mmap (void *, off_t, int);
90 1.1 matt static int gten_alloc_screen (void *, const struct wsscreen_descr *,
91 1.1 matt void **, int *, int *, long *);
92 1.1 matt static void gten_free_screen (void *, void *);
93 1.1 matt static int gten_show_screen (void *, void *, int,
94 1.1 matt void (*) (void *, int, int), void *);
95 1.1 matt
96 1.2 matt static struct wsdisplay_accessops gten_accessops = {
97 1.1 matt gten_ioctl,
98 1.1 matt gten_mmap,
99 1.1 matt gten_alloc_screen,
100 1.1 matt gten_free_screen,
101 1.1 matt gten_show_screen,
102 1.1 matt 0 /* load_font */
103 1.1 matt };
104 1.1 matt
105 1.1 matt static void gten_common_init (struct rasops_info *);
106 1.1 matt static int gten_getcmap (struct gten_softc *, struct wsdisplay_cmap *);
107 1.1 matt static int gten_putcmap (struct gten_softc *, struct wsdisplay_cmap *);
108 1.1 matt
109 1.2 matt #define GTEN_VRAM_OFFSET 0xf00000
110 1.2 matt
111 1.2 matt static int
112 1.1 matt gten_match(struct device *parent, struct cfdata *match, void *aux)
113 1.1 matt {
114 1.1 matt struct pci_attach_args *pa = aux;
115 1.1 matt
116 1.1 matt if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_WD &&
117 1.1 matt PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_WD_90C)
118 1.1 matt return 2;
119 1.1 matt
120 1.1 matt return 0;
121 1.1 matt }
122 1.1 matt
123 1.2 matt static void
124 1.1 matt gten_attach(struct device *parent, struct device *self, void *aux)
125 1.1 matt {
126 1.1 matt struct gten_softc *gt = (struct gten_softc *)self;
127 1.1 matt struct pci_attach_args *pa = aux;
128 1.1 matt struct wsemuldisplaydev_attach_args a;
129 1.1 matt int console = (pa->pa_tag == gten_console_pcitag);
130 1.1 matt int error;
131 1.1 matt char devinfo[256], pbuf[10];
132 1.1 matt
133 1.1 matt error = pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14,
134 1.1 matt PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
135 1.1 matt >->gt_memaddr, >->gt_memsize, NULL);
136 1.1 matt if (error) {
137 1.1 matt printf(": can't determine memory size: error=%d\n",
138 1.1 matt error);
139 1.1 matt return;
140 1.1 matt }
141 1.1 matt if (console) {
142 1.1 matt gt->gt_ri = >en_console_ri;
143 1.1 matt gt->gt_nscreens = 1;
144 1.1 matt } else {
145 1.1 matt MALLOC(gt->gt_ri, struct rasops_info *, sizeof(*gt->gt_ri),
146 1.1 matt M_DEVBUF, M_NOWAIT);
147 1.1 matt if (gt->gt_ri == NULL) {
148 1.1 matt printf(": can't alloc memory\n");
149 1.1 matt return;
150 1.1 matt }
151 1.1 matt memset(gt->gt_ri, 0, sizeof(*gt->gt_ri));
152 1.1 matt #if 0
153 1.1 matt error = pci_mapreg_map(pa, 0x14,
154 1.1 matt PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
155 1.1 matt BUS_SPACE_MAP_LINEAR, NULL,
156 1.1 matt (bus_space_handle_t *) >->gt_ri->ri_bits,
157 1.1 matt NULL, NULL);
158 1.1 matt #else
159 1.2 matt error = bus_space_map(pa->pa_memt, gt->gt_memaddr + GTEN_VRAM_OFFSET,
160 1.2 matt 960*1024, BUS_SPACE_MAP_LINEAR,
161 1.1 matt (bus_space_handle_t *) >->gt_ri->ri_bits);
162 1.1 matt #endif
163 1.1 matt if (error) {
164 1.1 matt printf(": can't map frame buffer: error=%d\n", error);
165 1.1 matt return;
166 1.1 matt }
167 1.1 matt
168 1.1 matt gten_common_init(gt->gt_ri);
169 1.1 matt }
170 1.1 matt
171 1.1 matt gt->gt_paddr = vtophys((vaddr_t)gt->gt_ri->ri_bits);
172 1.1 matt if (gt->gt_paddr == 0) {
173 1.1 matt printf(": cannot map framebuffer\n");
174 1.1 matt return;
175 1.1 matt }
176 1.2 matt gt->gt_psize = gt->gt_memsize - GTEN_VRAM_OFFSET;
177 1.1 matt
178 1.1 matt pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
179 1.1 matt printf(": %s\n", devinfo);
180 1.2 matt format_bytes(pbuf, sizeof(pbuf), gt->gt_psize);
181 1.2 matt printf("%s: %s, %dx%d, %dbpp\n", self->dv_xname, pbuf,
182 1.2 matt gt->gt_ri->ri_width, gt->gt_ri->ri_height,
183 1.2 matt gt->gt_ri->ri_depth);
184 1.2 matt #if defined(DEBUG)
185 1.2 matt printf("%s: text %dx%d, =+%d+%d\n", self->dv_xname,
186 1.2 matt gt->gt_ri->ri_cols, gt->gt_ri->ri_rows,
187 1.2 matt gt->gt_ri->ri_xorigin, gt->gt_ri->ri_yorigin);
188 1.2 matt
189 1.2 matt { int i;
190 1.2 matt struct rasops_info *ri = gt->gt_ri;
191 1.2 matt for (i = 0; i < 64; i++) {
192 1.2 matt int j = i * ri->ri_stride;
193 1.2 matt int k = (ri->ri_height - i - 1) * gt->gt_ri->ri_stride;
194 1.2 matt memset(ri->ri_bits + j, 0, 64 - i);
195 1.2 matt memset(ri->ri_bits + j + 64 - i, 255, i);
196 1.2 matt
197 1.2 matt memset(ri->ri_bits + j + ri->ri_width - 64 + i, 0, 64 - i);
198 1.2 matt memset(ri->ri_bits + j + ri->ri_width - 64, 255, i);
199 1.2 matt
200 1.2 matt memset(ri->ri_bits + k, 0, 64 - i);
201 1.2 matt memset(ri->ri_bits + k + 64 - i, 255, i);
202 1.2 matt
203 1.2 matt memset(ri->ri_bits + k + ri->ri_width - 64 + i, 0, 64 - i);
204 1.2 matt memset(ri->ri_bits + k + ri->ri_width - 64, 255, i);
205 1.2 matt }}
206 1.2 matt #endif
207 1.1 matt
208 1.1 matt gt->gt_cmap_red[0] = gt->gt_cmap_green[0] = gt->gt_cmap_blue[0] = 0;
209 1.1 matt gt->gt_cmap_red[15] = gt->gt_cmap_red[255] = 0xff;
210 1.1 matt gt->gt_cmap_green[15] = gt->gt_cmap_green[255] = 0xff;
211 1.1 matt gt->gt_cmap_blue[15] = gt->gt_cmap_blue[255] = 0xff;
212 1.1 matt
213 1.1 matt a.console = console;
214 1.1 matt a.scrdata = >en_screenlist;
215 1.1 matt a.accessops = >en_accessops;
216 1.1 matt a.accesscookie = gt;
217 1.1 matt
218 1.1 matt config_found(self, &a, wsemuldisplaydevprint);
219 1.1 matt }
220 1.1 matt
221 1.1 matt static void
222 1.1 matt gten_common_init(struct rasops_info *ri)
223 1.1 matt {
224 1.1 matt int32_t addr, width, height, linebytes, depth;
225 1.1 matt int i, screenbytes;
226 1.1 matt
227 1.1 matt /* initialize rasops */
228 1.1 matt ri->ri_width = 640;
229 1.1 matt ri->ri_height = 480;
230 1.1 matt ri->ri_depth = 8;
231 1.1 matt ri->ri_stride = 640;
232 1.2 matt ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
233 1.1 matt
234 1.2 matt rasops_init(ri, 30, 80);
235 1.1 matt /* black on white */
236 1.1 matt ri->ri_devcmap[0] = 0xffffffff; /* bg */
237 1.1 matt ri->ri_devcmap[1] = 0; /* fg */
238 1.1 matt
239 1.2 matt memset(ri->ri_bits, 0xff, ri->ri_stride * ri->ri_height);
240 1.2 matt
241 1.1 matt gten_stdscreen.nrows = ri->ri_rows;
242 1.1 matt gten_stdscreen.ncols = ri->ri_cols;
243 1.1 matt gten_stdscreen.textops = &ri->ri_ops;
244 1.1 matt gten_stdscreen.capabilities = ri->ri_caps;
245 1.1 matt }
246 1.1 matt
247 1.2 matt static int
248 1.1 matt gten_ioctl(v, cmd, data, flag, p)
249 1.1 matt void *v;
250 1.1 matt u_long cmd;
251 1.1 matt caddr_t data;
252 1.1 matt int flag;
253 1.1 matt struct proc *p;
254 1.1 matt {
255 1.1 matt struct gten_softc *gt = v;
256 1.1 matt struct wsdisplay_fbinfo *wdf;
257 1.1 matt struct grfinfo *gm;
258 1.1 matt
259 1.1 matt switch (cmd) {
260 1.1 matt case WSDISPLAYIO_GTYPE:
261 1.1 matt *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
262 1.1 matt return 0;
263 1.1 matt
264 1.1 matt case WSDISPLAYIO_GINFO:
265 1.1 matt wdf = (void *)data;
266 1.1 matt wdf->height = gt->gt_ri->ri_height;
267 1.1 matt wdf->width = gt->gt_ri->ri_width;
268 1.1 matt wdf->depth = gt->gt_ri->ri_depth;
269 1.1 matt wdf->cmsize = 256;
270 1.1 matt return 0;
271 1.1 matt
272 1.1 matt case WSDISPLAYIO_GETCMAP:
273 1.1 matt return gten_getcmap(gt, (struct wsdisplay_cmap *)data);
274 1.1 matt
275 1.1 matt case WSDISPLAYIO_PUTCMAP:
276 1.1 matt return gten_putcmap(gt, (struct wsdisplay_cmap *)data);
277 1.1 matt }
278 1.2.10.2 nathanw return EPASSTHROUGH;
279 1.1 matt }
280 1.1 matt
281 1.2 matt static paddr_t
282 1.1 matt gten_mmap(v, offset, prot)
283 1.1 matt void *v;
284 1.1 matt off_t offset;
285 1.1 matt int prot;
286 1.1 matt {
287 1.1 matt struct gten_softc *gt = v;
288 1.1 matt
289 1.2 matt if (offset >= 0 && offset < gt->gt_psize)
290 1.1 matt return gt->gt_paddr + offset;
291 1.2 matt if (offset >= 0x1000000 && offset < gt->gt_memsize)
292 1.2 matt return gt->gt_memaddr + offset - 0x1000000;
293 1.1 matt
294 1.1 matt return -1;
295 1.1 matt }
296 1.1 matt
297 1.2 matt static int
298 1.1 matt gten_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
299 1.1 matt void *v;
300 1.1 matt const struct wsscreen_descr *type;
301 1.1 matt void **cookiep;
302 1.1 matt int *curxp, *curyp;
303 1.1 matt long *attrp;
304 1.1 matt {
305 1.1 matt struct gten_softc *gt = v;
306 1.1 matt struct rasops_info *ri = gt->gt_ri;
307 1.1 matt long defattr;
308 1.1 matt
309 1.1 matt if (gt->gt_nscreens > 0)
310 1.1 matt return (ENOMEM);
311 1.1 matt
312 1.1 matt *cookiep = ri; /* one and only for now */
313 1.1 matt *curxp = 0;
314 1.1 matt *curyp = 0;
315 1.2.10.3 nathanw (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
316 1.1 matt *attrp = defattr;
317 1.1 matt gt->gt_nscreens++;
318 1.1 matt return 0;
319 1.1 matt }
320 1.1 matt
321 1.2 matt static void
322 1.1 matt gten_free_screen(v, cookie)
323 1.1 matt void *v;
324 1.1 matt void *cookie;
325 1.1 matt {
326 1.1 matt struct gten_softc *gt = v;
327 1.1 matt
328 1.1 matt if (gt->gt_ri == >en_console_ri)
329 1.1 matt panic("gten_free_screen: console");
330 1.1 matt
331 1.1 matt gt->gt_nscreens--;
332 1.1 matt }
333 1.1 matt
334 1.2 matt static int
335 1.1 matt gten_show_screen(v, cookie, waitok, cb, cbarg)
336 1.1 matt void *v;
337 1.1 matt void *cookie;
338 1.1 matt int waitok;
339 1.1 matt void (*cb) (void *, int, int);
340 1.1 matt void *cbarg;
341 1.1 matt {
342 1.1 matt return (0);
343 1.1 matt }
344 1.1 matt
345 1.1 matt int
346 1.2.10.1 nathanw gten_cnattach(pci_chipset_tag_t pc, bus_space_tag_t memt)
347 1.1 matt {
348 1.1 matt struct rasops_info *ri = >en_console_ri;
349 1.1 matt u_int32_t mapreg, id, mask, mapsize;
350 1.1 matt long defattr;
351 1.1 matt pcitag_t tag;
352 1.1 matt int s, error;
353 1.1 matt bus_size_t bussize;
354 1.1 matt bus_addr_t busaddr;
355 1.1 matt
356 1.2.10.1 nathanw tag = pci_make_tag(pc, 0, 14, 0);
357 1.1 matt
358 1.2.10.1 nathanw id = pci_conf_read(pc, tag, PCI_ID_REG);
359 1.1 matt if (PCI_VENDOR(id) != PCI_VENDOR_WD ||
360 1.1 matt PCI_PRODUCT(id) != PCI_PRODUCT_WD_90C)
361 1.1 matt return ENXIO;
362 1.1 matt
363 1.2.10.1 nathanw mapreg = pci_conf_read(pc, tag, 0x14);
364 1.1 matt if (PCI_MAPREG_TYPE(mapreg) != PCI_MAPREG_TYPE_MEM ||
365 1.1 matt PCI_MAPREG_MEM_TYPE(mapreg) != PCI_MAPREG_MEM_TYPE_32BIT)
366 1.1 matt return ENXIO;
367 1.1 matt
368 1.1 matt s = splhigh();
369 1.2.10.1 nathanw pci_conf_write(pc, tag, 0x14, 0xffffffff);
370 1.2.10.1 nathanw mask = pci_conf_read(pc, tag, 0x14);
371 1.2.10.1 nathanw pci_conf_write(pc, tag, 0x14, mapreg);
372 1.1 matt splx(s);
373 1.1 matt bussize = PCI_MAPREG_MEM_SIZE(mask);
374 1.1 matt busaddr = PCI_MAPREG_MEM_ADDR(mapreg);
375 1.1 matt
376 1.2 matt error = bus_space_map(memt, busaddr + GTEN_VRAM_OFFSET, 960*1024,
377 1.1 matt BUS_SPACE_MAP_LINEAR, (bus_space_handle_t *) &ri->ri_bits);
378 1.1 matt if (error)
379 1.1 matt return error;
380 1.1 matt
381 1.1 matt gten_common_init(ri);
382 1.1 matt
383 1.2.10.3 nathanw (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
384 1.1 matt wsdisplay_cnattach(>en_stdscreen, ri, 0, 0, defattr);
385 1.1 matt
386 1.1 matt gten_console_pcitag = tag;
387 1.1 matt
388 1.1 matt return 0;
389 1.1 matt }
390 1.1 matt
391 1.2 matt static int
392 1.1 matt gten_getcmap(gt, cm)
393 1.1 matt struct gten_softc *gt;
394 1.1 matt struct wsdisplay_cmap *cm;
395 1.1 matt {
396 1.1 matt u_int index = cm->index;
397 1.1 matt u_int count = cm->count;
398 1.1 matt int error;
399 1.1 matt
400 1.1 matt if (index >= 256 || count > 256 || index + count > 256)
401 1.1 matt return EINVAL;
402 1.1 matt
403 1.1 matt error = copyout(>->gt_cmap_red[index], cm->red, count);
404 1.1 matt if (error)
405 1.1 matt return error;
406 1.1 matt error = copyout(>->gt_cmap_green[index], cm->green, count);
407 1.1 matt if (error)
408 1.1 matt return error;
409 1.1 matt error = copyout(>->gt_cmap_blue[index], cm->blue, count);
410 1.1 matt if (error)
411 1.1 matt return error;
412 1.1 matt
413 1.1 matt return 0;
414 1.1 matt }
415 1.1 matt
416 1.2 matt static int
417 1.1 matt gten_putcmap(gt, cm)
418 1.1 matt struct gten_softc *gt;
419 1.1 matt struct wsdisplay_cmap *cm;
420 1.1 matt {
421 1.1 matt int index = cm->index;
422 1.1 matt int count = cm->count;
423 1.1 matt int i;
424 1.1 matt u_char *r, *g, *b;
425 1.1 matt
426 1.1 matt if (cm->index >= 256 || cm->count > 256 ||
427 1.1 matt (cm->index + cm->count) > 256)
428 1.1 matt return EINVAL;
429 1.1 matt if (!uvm_useracc(cm->red, cm->count, B_READ) ||
430 1.1 matt !uvm_useracc(cm->green, cm->count, B_READ) ||
431 1.1 matt !uvm_useracc(cm->blue, cm->count, B_READ))
432 1.1 matt return EFAULT;
433 1.1 matt copyin(cm->red, >->gt_cmap_red[index], count);
434 1.1 matt copyin(cm->green, >->gt_cmap_green[index], count);
435 1.1 matt copyin(cm->blue, >->gt_cmap_blue[index], count);
436 1.1 matt
437 1.1 matt r = >->gt_cmap_red[index];
438 1.1 matt g = >->gt_cmap_green[index];
439 1.1 matt b = >->gt_cmap_blue[index];
440 1.1 matt
441 1.1 matt #if 0
442 1.1 matt for (i = 0; i < count; i++) {
443 1.1 matt OF_call_method_1("color!", dc->dc_ih, 4, *r, *g, *b, index);
444 1.1 matt r++, g++, b++, index++;
445 1.1 matt }
446 1.1 matt #endif
447 1.1 matt
448 1.1 matt return 0;
449 1.1 matt }
450