xafb.c revision 1.6 1 /* $NetBSD: xafb.c,v 1.6 2003/07/15 02:59:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. 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. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* "xa" frame buffer driver. Currently supports 1280x1024x8 only. */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xafb.c,v 1.6 2003/07/15 02:59:29 lukem Exp $");
33
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <machine/adrsmap.h>
44 #include <machine/apcall.h>
45
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/rasops/rasops.h>
49
50 #include <newsmips/apbus/apbusvar.h>
51
52 struct xafb_reg {
53 volatile u_int r0;
54 volatile u_int index;
55 volatile u_int r2;
56 volatile u_int zero;
57 volatile u_int r4;
58 volatile u_int r5;
59 volatile u_int r6;
60 volatile u_int rgb;
61 };
62
63 struct xafb_devconfig {
64 volatile u_char *dc_fbbase; /* VRAM base address */
65 struct xafb_reg *dc_reg; /* register address */
66 struct rasops_info dc_ri;
67 };
68
69 struct xafb_softc {
70 struct device sc_dev;
71 struct xafb_devconfig *sc_dc;
72 int sc_nscreens;
73 u_char sc_cmap_red[256];
74 u_char sc_cmap_green[256];
75 u_char sc_cmap_blue[256];
76 };
77
78 int xafb_match __P((struct device *, struct cfdata *, void *));
79 void xafb_attach __P((struct device *, struct device *, void *));
80
81 int xafb_common_init __P((struct xafb_devconfig *));
82 int xafb_is_console __P((void));
83
84 int xafb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
85 paddr_t xafb_mmap __P((void *, off_t, int));
86 int xafb_alloc_screen __P((void *, const struct wsscreen_descr *,
87 void **, int *, int *, long *));
88 void xafb_free_screen __P((void *, void *));
89 int xafb_show_screen __P((void *, void *, int,
90 void (*) (void *, int, int), void *));
91
92 int xafb_cnattach __P((void));
93 int xafb_getcmap __P((struct xafb_softc *, struct wsdisplay_cmap *));
94 int xafb_putcmap __P((struct xafb_softc *, struct wsdisplay_cmap *));
95
96 static __inline void xafb_setcolor(struct xafb_devconfig *, int, int, int, int);
97
98 CFATTACH_DECL(xafb, sizeof(struct xafb_softc),
99 xafb_match, xafb_attach, NULL, NULL);
100
101 struct xafb_devconfig xafb_console_dc;
102
103 struct wsdisplay_accessops xafb_accessops = {
104 xafb_ioctl,
105 xafb_mmap,
106 xafb_alloc_screen,
107 xafb_free_screen,
108 xafb_show_screen,
109 NULL /* load_font */
110 };
111
112 struct wsscreen_descr xafb_stdscreen = {
113 "std",
114 0, 0,
115 0,
116 0, 0,
117 WSSCREEN_REVERSE
118 };
119
120 const struct wsscreen_descr *xafb_scrlist[] = {
121 &xafb_stdscreen
122 };
123
124 struct wsscreen_list xafb_screenlist = {
125 sizeof(xafb_scrlist) / sizeof(xafb_scrlist[0]), xafb_scrlist
126 };
127
128 int
129 xafb_match(parent, match, aux)
130 struct device *parent;
131 struct cfdata *match;
132 void *aux;
133 {
134 struct apbus_attach_args *apa = aux;
135
136 if (strcmp(apa->apa_name, "xa") != 0)
137 return 0;
138
139 return 1;
140 }
141
142 void
143 xafb_attach(parent, self, aux)
144 struct device *parent, *self;
145 void *aux;
146 {
147 struct xafb_softc *sc = (void *)self;
148 struct apbus_attach_args *apa = aux;
149 struct wsemuldisplaydev_attach_args wsa;
150 struct xafb_devconfig *dc;
151 struct rasops_info *ri;
152 int console, i;
153
154 console = xafb_is_console();
155
156 if (console) {
157 dc = &xafb_console_dc;
158 ri = &dc->dc_ri;
159 sc->sc_nscreens = 1;
160 } else {
161 dc = malloc(sizeof(struct xafb_devconfig), M_DEVBUF, M_WAITOK);
162 bzero(dc, sizeof(struct xafb_devconfig));
163
164 dc->dc_fbbase = (void *)0xb0000000; /* XXX */
165 dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
166 if (xafb_common_init(dc) != 0) {
167 printf(": couldn't initialize device\n");
168 return;
169 }
170
171 ri = &dc->dc_ri;
172
173 /* clear screen */
174 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
175 }
176 sc->sc_dc = dc;
177
178 for (i = 0; i < 256; i++) {
179 sc->sc_cmap_red[i] = i;
180 sc->sc_cmap_green[i] = i;
181 sc->sc_cmap_blue[i] = i;
182 }
183
184 printf(": %d x %d, %dbpp\n", ri->ri_width, ri->ri_height, ri->ri_depth);
185
186 wsa.console = console;
187 wsa.scrdata = &xafb_screenlist;
188 wsa.accessops = &xafb_accessops;
189 wsa.accesscookie = sc;
190
191 config_found(self, &wsa, wsemuldisplaydevprint);
192 }
193
194 void
195 xafb_setcolor(dc, index, r, g, b)
196 struct xafb_devconfig *dc;
197 int index, r, g, b;
198 {
199 volatile struct xafb_reg *reg = dc->dc_reg;
200
201 reg->index = index;
202 reg->zero = 0;
203 reg->rgb = r;
204 reg->rgb = g;
205 reg->rgb = b;
206 }
207
208 int
209 xafb_common_init(dc)
210 struct xafb_devconfig *dc;
211 {
212 struct rasops_info *ri = &dc->dc_ri;
213 int i;
214
215 for (i = 0; i < 256; i++)
216 xafb_setcolor(dc, i, i, i, i);
217
218 /* initialize rasops */
219 ri->ri_width = 1280;
220 ri->ri_height = 1024;
221 ri->ri_depth = 8;
222 ri->ri_stride = 2048;
223 ri->ri_bits = (void *)dc->dc_fbbase;
224 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
225
226 rasops_init(ri, 44, 100);
227
228 /* mono */
229 ri->ri_devcmap[0] = 0; /* bg */
230 ri->ri_devcmap[1] = 0xffffffff; /* fg */
231
232 xafb_stdscreen.nrows = ri->ri_rows;
233 xafb_stdscreen.ncols = ri->ri_cols;
234 xafb_stdscreen.textops = &ri->ri_ops;
235 xafb_stdscreen.capabilities = ri->ri_caps;
236
237 return 0;
238 }
239
240 int
241 xafb_is_console()
242 {
243 volatile u_int *dipsw = (void *)NEWS5000_DIP_SWITCH;
244
245 if (*dipsw & 1) /* XXX right? */
246 return 1;
247
248 return 0;
249 }
250
251 int
252 xafb_ioctl(v, cmd, data, flag, p)
253 void *v;
254 u_long cmd;
255 caddr_t data;
256 int flag;
257 struct proc *p;
258 {
259 struct xafb_softc *sc = v;
260 struct xafb_devconfig *dc = sc->sc_dc;
261 struct wsdisplay_fbinfo *wdf;
262
263 switch (cmd) {
264 case WSDISPLAYIO_GTYPE:
265 *(int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */
266 return 0;
267
268 case WSDISPLAYIO_GINFO:
269 wdf = (void *)data;
270 wdf->height = dc->dc_ri.ri_height;
271 wdf->width = dc->dc_ri.ri_width;
272 wdf->depth = dc->dc_ri.ri_depth;
273 wdf->cmsize = 256;
274 return 0;
275
276 case WSDISPLAYIO_GETCMAP:
277 return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
278
279 case WSDISPLAYIO_PUTCMAP:
280 return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
281
282 /* case WSDISPLAYIO_SVIDEO: */
283 }
284 return EPASSTHROUGH;
285 }
286
287 paddr_t
288 xafb_mmap(v, offset, prot)
289 void *v;
290 off_t offset;
291 int prot;
292 {
293 struct xafb_softc *sc = v;
294 struct xafb_devconfig *dc = sc->sc_dc;
295 struct rasops_info *ri = &dc->dc_ri;
296
297 if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
298 return -1;
299
300 return mips_btop((int)dc->dc_fbbase + offset);
301 }
302
303 int
304 xafb_alloc_screen(v, scrdesc, cookiep, ccolp, crowp, attrp)
305 void *v;
306 const struct wsscreen_descr *scrdesc;
307 void **cookiep;
308 int *ccolp, *crowp;
309 long *attrp;
310 {
311 struct xafb_softc *sc = v;
312 struct rasops_info *ri = &sc->sc_dc->dc_ri;
313 long defattr;
314
315 if (sc->sc_nscreens > 0)
316 return ENOMEM;
317
318 *cookiep = ri;
319 *ccolp = *crowp = 0;
320 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
321 *attrp = defattr;
322 sc->sc_nscreens++;
323
324 return 0;
325 }
326
327 void
328 xafb_free_screen(v, cookie)
329 void *v;
330 void *cookie;
331 {
332 struct xafb_softc *sc = v;
333
334 if (sc->sc_dc == &xafb_console_dc)
335 panic("xafb_free_screen: console");
336
337 sc->sc_nscreens--;
338 }
339
340 int
341 xafb_show_screen(v, cookie, waitok, cb, cbarg)
342 void *v;
343 void *cookie;
344 int waitok;
345 void (*cb) __P((void *, int, int));
346 void *cbarg;
347 {
348 return 0;
349 }
350
351 int
352 xafb_cnattach()
353 {
354 struct xafb_devconfig *dc = &xafb_console_dc;
355 struct rasops_info *ri = &dc->dc_ri;
356 long defattr;
357 int crow = 0;
358
359 if (!xafb_is_console())
360 return -1;
361
362 dc->dc_fbbase = (void *)0xb0000000; /* XXX */
363 dc->dc_reg = (void *)0xb4903000; /* XXX */
364 xafb_common_init(dc);
365
366 crow = 0; /* XXX current cursor pos */
367
368 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
369 wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
370
371 return 0;
372 }
373
374 int
375 xafb_getcmap(sc, cm)
376 struct xafb_softc *sc;
377 struct wsdisplay_cmap *cm;
378 {
379 u_int index = cm->index;
380 u_int count = cm->count;
381 int error;
382
383 if (index >= 256 || count > 256 || index + count > 256)
384 return EINVAL;
385
386 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
387 if (error)
388 return error;
389 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
390 if (error)
391 return error;
392 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
393 if (error)
394 return error;
395
396 return 0;
397 }
398
399 int
400 xafb_putcmap(sc, cm)
401 struct xafb_softc *sc;
402 struct wsdisplay_cmap *cm;
403 {
404 u_int index = cm->index;
405 u_int count = cm->count;
406 int i;
407 u_char *r, *g, *b;
408
409 if (index >= 256 || count > 256 || index + count > 256)
410 return EINVAL;
411 if (!uvm_useracc(cm->red, count, B_READ) ||
412 !uvm_useracc(cm->green, count, B_READ) ||
413 !uvm_useracc(cm->blue, count, B_READ))
414 return EFAULT;
415 copyin(cm->red, &sc->sc_cmap_red[index], count);
416 copyin(cm->green, &sc->sc_cmap_green[index], count);
417 copyin(cm->blue, &sc->sc_cmap_blue[index], count);
418
419 r = &sc->sc_cmap_red[index];
420 g = &sc->sc_cmap_green[index];
421 b = &sc->sc_cmap_blue[index];
422
423 for (i = 0; i < count; i++)
424 xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
425
426 return 0;
427 }
428