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