light.c revision 1.3 1 /* $Id: light.c,v 1.3 2006/12/29 00:31:48 rumble Exp $ */
2
3 /*
4 * Copyright (c) 2006 Stephen M. Rumble
5 * Copyright (c) 2003 Ilpo Ruotsalainen
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
31 */
32
33 /*
34 * SGI "Light" graphics, a.k.a. "Entry", "Starter", "LG1", and "LG2".
35 *
36 * 1024x768 8bpp at 60Hz.
37 *
38 * This driver supports the boards found in Indigo R3k and R4k machines.
39 * There is a Crimson variant, but the register offsets differ significantly.
40 *
41 * Light's REX chip is the precursor of the REX3 found in "newport", hence
42 * much similarity exists.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: light.c,v 1.3 2006/12/29 00:31:48 rumble Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wscons/wsdisplayvar.h>
55 #include <dev/wsfont/wsfont.h>
56
57 #include <sgimips/gio/giovar.h>
58 #include <sgimips/gio/lightvar.h>
59 #include <sgimips/gio/lightreg.h>
60
61 struct light_softc {
62 struct device sc_dev;
63
64 struct light_devconfig *sc_dc;
65 };
66
67 struct light_devconfig {
68 uint32_t dc_addr;
69
70 bus_space_tag_t dc_st;
71 bus_space_handle_t dc_sh;
72
73 int dc_boardrev;
74 int dc_font;
75 struct wsdisplay_font *dc_fontdata;
76 };
77
78 /* always 1024x768x8 */
79 #define LIGHT_XRES 1024
80 #define LIGHT_YRES 768
81 #define LIGHT_DEPTH 8
82
83 static int light_match(struct device *, struct cfdata *, void *);
84 static void light_attach(struct device *, struct device *, void *);
85
86 CFATTACH_DECL(light, sizeof(struct light_softc), light_match, light_attach,
87 NULL, NULL);
88
89 /* wsdisplay_emulops */
90 static void light_cursor(void *, int, int, int);
91 static int light_mapchar(void *, int, unsigned int *);
92 static void light_putchar(void *, int, int, u_int, long);
93 static void light_copycols(void *, int, int, int, int);
94 static void light_erasecols(void *, int, int, int, long);
95 static void light_copyrows(void *, int, int, int);
96 static void light_eraserows(void *, int, int, long);
97 static int light_allocattr(void *, int, int, int, long *);
98
99 /* wsdisplay_accessops */
100 static int light_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
101 static paddr_t light_mmap(void *, void *, off_t, int);
102 static int light_alloc_screen(void *, const struct wsscreen_descr *,
103 void **, int *, int *, long *);
104 static void light_free_screen(void *, void *);
105 static int light_show_screen(void *, void *, int,
106 void (*)(void *, int, int), void *);
107
108 static const struct wsdisplay_accessops light_accessops = {
109 .ioctl = light_ioctl,
110 .mmap = light_mmap,
111 .alloc_screen = light_alloc_screen,
112 .free_screen = light_free_screen,
113 .show_screen = light_show_screen,
114 .load_font = NULL,
115 .pollc = NULL,
116 .scroll = NULL
117 };
118
119 static const struct wsdisplay_emulops light_emulops = {
120 .cursor = light_cursor,
121 .mapchar = light_mapchar,
122 .putchar = light_putchar,
123 .copycols = light_copycols,
124 .erasecols = light_erasecols,
125 .copyrows = light_copyrows,
126 .eraserows = light_eraserows,
127 .allocattr = light_allocattr,
128 .replaceattr = NULL
129 };
130
131 static const struct wsscreen_descr light_screen = {
132 .name = "1024x768",
133 .ncols = 128,
134 .nrows = 48,
135 .textops = &light_emulops,
136 .fontwidth = 8,
137 .fontheight = 16,
138 .capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_REVERSE
139 };
140
141 const struct wsscreen_descr *_light_screenlist[] = {
142 &light_screen
143 };
144
145 static const struct wsscreen_list light_screenlist = {
146 sizeof(_light_screenlist) / sizeof(_light_screenlist[0]),
147 _light_screenlist
148 };
149
150 static struct light_devconfig light_console_dc;
151 static int light_is_console = 0;
152
153 #define LIGHT_ATTR_ENCODE(fg, bg) (((fg << 8) & 0xff00) | (bg * 0x00ff))
154 #define LIGHT_ATTR_FG(attr) ((attr >> 8) & 0x00ff)
155 #define LIGHT_ATTR_BG(attr) (attr & 0x00ff)
156
157 #define LIGHT_IS_LG1(_rev) ((_rev) < 2) /* else LG2 */
158
159 /*******************************************************************************
160 * REX routines and helper functions
161 ******************************************************************************/
162
163 static uint32_t
164 rex_read(struct light_devconfig *dc, uint32_t rset, uint32_t r)
165 {
166
167 return (bus_space_read_4(dc->dc_st, dc->dc_sh, rset + r));
168 }
169
170 static void
171 rex_write(struct light_devconfig *dc, uint32_t rset, uint32_t r, uint32_t v)
172 {
173
174 bus_space_write_4(dc->dc_st, dc->dc_sh, rset + r, v);
175 }
176
177 static uint8_t
178 rex_vc1_read(struct light_devconfig *dc)
179 {
180
181 rex_write(dc, REX_PAGE1_GO, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_SYSCTL);
182 rex_read(dc, REX_PAGE1_GO, REX_P1REG_VC1_ADDRDATA);
183 return (rex_read(dc, REX_PAGE1_SET, REX_P1REG_VC1_ADDRDATA));
184 }
185
186 static void
187 rex_vc1_write(struct light_devconfig *dc, uint8_t val)
188 {
189
190 rex_write(dc, REX_PAGE1_GO, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_SYSCTL);
191 rex_write(dc, REX_PAGE1_SET, REX_P1REG_VC1_ADDRDATA, val);
192 rex_write(dc, REX_PAGE1_GO, REX_P1REG_VC1_ADDRDATA, val);
193 }
194
195 static void
196 rex_wait(struct light_devconfig *dc)
197 {
198
199 while (rex_read(dc, REX_PAGE1_SET,REX_P1REG_CFGMODE) & REX_CFGMODE_BUSY)
200 ;
201 }
202
203 static int
204 rex_revision(struct light_devconfig *dc)
205 {
206
207 rex_write(dc, REX_PAGE1_SET, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_LADDR);
208 rex_read(dc, REX_PAGE1_GO, REX_P1REG_WCLOCKREV);
209 return (rex_read(dc, REX_PAGE1_SET, REX_P1REG_WCLOCKREV) & 0x7);
210 }
211
212 static void
213 rex_copy_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x,
214 int to_y, int width, int height)
215 {
216 int dx, dy, ystarti, yendi;
217
218 dx = from_x - to_x;
219 dy = from_y - to_y;
220
221 /* adjust for y. NB: STOPONX, STOPONY are inclusive */
222 if (to_y > from_y) {
223 ystarti = to_y + height - 1;
224 yendi = to_y;
225 } else {
226 ystarti = to_y;
227 yendi = to_y + height - 1;
228 }
229
230 rex_wait(dc);
231
232 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, to_x);
233 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, to_x + width);
234 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, ystarti);
235 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, yendi);
236 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW |
237 REX_LOGICOP_SRC | REX_OP_FLG_LOGICSRC | REX_OP_FLG_QUADMODE |
238 REX_OP_FLG_BLOCK | REX_OP_FLG_STOPONX | REX_OP_FLG_STOPONY);
239 rex_write(dc, REX_PAGE0_GO, REX_P0REG_XYMOVE,
240 ((dx << 16) & 0xffff0000) | (dy & 0x0000ffff));
241 }
242
243 static void
244 rex_fill_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x,
245 int to_y, long attr)
246 {
247
248 rex_wait(dc);
249
250 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, from_y);
251 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, to_y);
252 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, from_x);
253 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, to_x);
254 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORREDI, LIGHT_ATTR_BG(attr));
255 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW |
256 REX_LOGICOP_SRC | REX_OP_FLG_QUADMODE | REX_OP_FLG_BLOCK |
257 REX_OP_FLG_STOPONX | REX_OP_FLG_STOPONY);
258 rex_read(dc, REX_PAGE0_GO, REX_P0REG_COMMAND);
259 }
260
261 /*******************************************************************************
262 * match/attach functions
263 ******************************************************************************/
264
265 static int
266 light_match(struct device *parent, struct cfdata *self, void *aux)
267 {
268 struct gio_attach_args *ga = aux;
269
270 if (ga->ga_addr != LIGHT_ADDR_0 && ga->ga_addr != LIGHT_ADDR_1)
271 return (0);
272
273 if (badaddr((void *)(ga->ga_ioh + REX_PAGE1_SET + REX_P1REG_XYOFFSET),
274 sizeof(uint32_t)))
275 return (0);
276
277 if (bus_space_read_4(ga->ga_iot, ga->ga_ioh,
278 REX_PAGE1_SET + REX_P1REG_XYOFFSET) != 0x08000800)
279 return (0);
280
281 return (1);
282 }
283
284 static void
285 light_attach_common(struct light_devconfig *dc, struct gio_attach_args *ga)
286 {
287
288 dc->dc_addr = ga->ga_addr;
289 dc->dc_st = ga->ga_iot;
290 dc->dc_sh = ga->ga_ioh;
291
292 dc->dc_boardrev = rex_revision(dc);
293
294 wsfont_init();
295
296 dc->dc_font = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
297 WSDISPLAY_FONTORDER_L2R);
298
299 if (dc->dc_font < 0)
300 panic("light_attach_common: no suitable fonts");
301
302 if (wsfont_lock(dc->dc_font, &dc->dc_fontdata))
303 panic("light_attach_common: unable to lock font data");
304
305 rex_vc1_write(dc, rex_vc1_read(dc) & ~(VC1_SYSCTL_CURSOR |
306 VC1_SYSCTL_CURSOR_ON));
307 rex_fill_rect(dc, 0, 0, LIGHT_XRES - 1, LIGHT_YRES - 1, 0);
308 }
309
310 static void
311 light_attach(struct device *parent, struct device *self, void *aux)
312 {
313 struct gio_attach_args *ga = aux;
314 struct light_softc *sc = (void *)self;
315 struct wsemuldisplaydev_attach_args wa;
316
317 if (light_is_console && ga->ga_addr == light_console_dc.dc_addr) {
318 wa.console = 1;
319 sc->sc_dc = &light_console_dc;
320 } else {
321 wa.console = 0;
322 sc->sc_dc = malloc(sizeof(struct light_devconfig), M_DEVBUF,
323 M_WAITOK | M_ZERO);
324 if (sc->sc_dc == NULL)
325 panic("light_attach: out of memory");
326
327 light_attach_common(sc->sc_dc, ga);
328 }
329
330 aprint_naive(": Display adapter\n");
331
332 aprint_normal(": SGI LG%d (board revision %d)\n",
333 LIGHT_IS_LG1(sc->sc_dc->dc_boardrev) ? 1 : 2,
334 sc->sc_dc->dc_boardrev);
335
336 wa.scrdata = &light_screenlist;
337 wa.accessops = &light_accessops;
338 wa.accesscookie = sc->sc_dc;
339
340 config_found(&sc->sc_dev, &wa, wsemuldisplaydevprint);
341 }
342
343 int
344 light_cnattach(struct gio_attach_args *ga)
345 {
346
347 if (!light_match(NULL, NULL, ga))
348 return (ENXIO);
349
350 light_attach_common(&light_console_dc, ga);
351
352 wsdisplay_cnattach(&light_screen, &light_console_dc, 0, 0,
353 LIGHT_ATTR_ENCODE(WSCOL_WHITE, WSCOL_BLACK));
354
355 light_is_console = 1;
356
357 return (0);
358 }
359
360 /*******************************************************************************
361 * wsdisplay_emulops
362 ******************************************************************************/
363
364 static void
365 light_cursor(void *c, int on, int row, int col)
366 {
367 /* XXX */
368 }
369
370 static int
371 light_mapchar(void *c, int ch, unsigned int *cp)
372 {
373 struct light_devconfig *dc = (void *)c;
374
375 if (dc->dc_fontdata->encoding != WSDISPLAY_FONTENC_ISO) {
376 ch = wsfont_map_unichar(dc->dc_fontdata, ch);
377
378 if (ch < 0)
379 goto fail;
380 }
381
382 if (ch < dc->dc_fontdata->firstchar ||
383 ch >= dc->dc_fontdata->firstchar + dc->dc_fontdata->numchars)
384 goto fail;
385
386 *cp = ch;
387 return 5;
388
389 fail:
390 *cp = ' ';
391 return 0;
392 }
393
394 static void
395 light_putchar(void *c, int row, int col, u_int ch, long attr)
396 {
397 struct light_devconfig *dc = c;
398 struct wsdisplay_font *font = dc->dc_fontdata;
399 uint8_t *bitmap;
400 uint32_t pattern;
401 int i, x, y;
402
403 bitmap = (u_int8_t *)font->data +
404 ((ch - font->firstchar) * font->fontheight * font->stride);
405 x = col * font->fontwidth;
406 y = row * font->fontheight;
407
408 rex_wait(dc);
409
410 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, y);
411 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, y + font->fontheight - 1);
412 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, x);
413 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, x + font->fontwidth - 1);
414 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORREDI, LIGHT_ATTR_FG(attr));
415 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORBACK, LIGHT_ATTR_BG(attr));
416 rex_write(dc, REX_PAGE0_GO, REX_P0REG_COMMAND, REX_OP_NOP);
417
418 rex_wait(dc);
419
420 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW |
421 REX_LOGICOP_SRC | REX_OP_FLG_ENZPATTERN | REX_OP_FLG_QUADMODE |
422 REX_OP_FLG_XYCONTINUE | REX_OP_FLG_STOPONX | REX_OP_FLG_BLOCK |
423 REX_OP_FLG_LENGTH32 | REX_OP_FLG_ZOPAQUE);
424
425 for (i = 0; i < font->fontheight; i++) {
426 /* XXX assumes font->fontwidth == 8 */
427 pattern = *bitmap << 24;
428 rex_write(dc, REX_PAGE0_GO, REX_P0REG_ZPATTERN, pattern);
429 bitmap += font->stride;
430 }
431 }
432
433 /* copy set of columns within the same line */
434 static void
435 light_copycols(void *c, int row, int srccol, int dstcol, int ncols)
436 {
437 struct light_devconfig *dc = c;
438 struct wsdisplay_font *font = dc->dc_fontdata;
439 int from_x, from_y, to_x, to_y, width, height;
440
441 from_x = srccol * font->fontwidth;
442 from_y = row * font->fontheight;
443 to_x = dstcol * font->fontwidth;
444 to_y = from_y;
445 width = ncols * font->fontwidth;
446 height = font->fontheight;
447
448 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
449 }
450
451 /* erase a set of columns in the same line */
452 static void
453 light_erasecols(void *c, int row, int startcol, int ncols, long attr)
454 {
455 struct light_devconfig *dc = c;
456 struct wsdisplay_font *font = dc->dc_fontdata;
457 int from_x, from_y, to_x, to_y;
458
459 from_x = startcol * font->fontwidth;
460 from_y = row * font->fontheight;
461 to_x = from_x + (ncols * font->fontwidth) - 1;
462 to_y = from_y + font->fontheight - 1;
463
464 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
465 }
466
467 /* copy a set of complete rows */
468 static void
469 light_copyrows(void *c, int srcrow, int dstrow, int nrows)
470 {
471 struct light_devconfig *dc = c;
472 struct wsdisplay_font *font = dc->dc_fontdata;
473 int from_x, from_y, to_x, to_y, width, height;
474
475 from_x = 0;
476 from_y = srcrow * font->fontheight;
477 to_x = 0;
478 to_y = dstrow * font->fontheight;
479 width = LIGHT_XRES;
480 height = nrows * font->fontheight;
481
482 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
483 }
484
485 /* erase a set of complete rows */
486 static void
487 light_eraserows(void *c, int row, int nrows, long attr)
488 {
489 struct light_devconfig *dc = c;
490 struct wsdisplay_font *font = dc->dc_fontdata;
491 int from_x, from_y, to_x, to_y;
492
493 from_x = 0;
494 from_y = row * font->fontheight;
495 to_x = LIGHT_XRES - 1;
496 to_y = from_y + (nrows * font->fontheight) - 1;
497
498 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
499 }
500
501 static int
502 light_allocattr(void *c, int fg, int bg, int flags, long *attr)
503 {
504
505 if (flags & ~(WSATTR_WSCOLORS | WSATTR_HILIT | WSATTR_REVERSE))
506 return (EINVAL);
507
508 if ((flags & WSATTR_WSCOLORS) == 0) {
509 fg = WSCOL_WHITE;
510 bg = WSCOL_BLACK;
511 }
512
513 if (flags & WSATTR_HILIT)
514 fg += 8;
515
516 if (flags & WSATTR_REVERSE) {
517 int tmp = fg;
518 fg = bg;
519 bg = tmp;
520 }
521
522 *attr = LIGHT_ATTR_ENCODE(fg, bg);
523 return (0);
524 }
525
526 /*******************************************************************************
527 * wsdisplay_accessops
528 ******************************************************************************/
529
530 static int
531 light_ioctl(void *c, void *vs, u_long cmd, caddr_t data, int flag,
532 struct lwp *l)
533 {
534 struct wsdisplay_fbinfo *fbinfo = (struct wsdisplay_fbinfo *)data;
535
536 switch (cmd) {
537 case WSDISPLAYIO_GINFO:
538 fbinfo->width = LIGHT_XRES;
539 fbinfo->height = LIGHT_YRES;
540 fbinfo->depth = LIGHT_DEPTH;
541 fbinfo->cmsize = 1 << LIGHT_DEPTH;
542 return (0);
543
544 case WSDISPLAYIO_GMODE:
545 *(u_int *)data = WSDISPLAYIO_MODE_EMUL;
546 break;
547
548 case WSDISPLAYIO_GTYPE:
549 *(u_int *)data = WSDISPLAY_TYPE_LIGHT;
550 return (0);
551
552 case WSDISPLAYIO_SVIDEO:
553 /*
554 * Turning off VC1 will stop refreshing the video ram (or so I
555 * suspect). We'll blank the screen after bringing it back up,
556 * since that's nicer than displaying garbage.
557 */
558 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
559 rex_vc1_write(c,rex_vc1_read(c) & ~VC1_SYSCTL_VIDEO_ON);
560 else {
561 rex_vc1_write(c, rex_vc1_read(c) | VC1_SYSCTL_VIDEO_ON);
562 rex_fill_rect(c, 0, 0, LIGHT_XRES-1, LIGHT_YRES-1, 0);
563 }
564 return (0);
565 }
566
567 return (EPASSTHROUGH);
568 }
569
570 static paddr_t
571 light_mmap(void *c, void *vs, off_t off, int prot)
572 {
573 struct light_devconfig *dc = c;
574
575 if (off >= 0x7fff)
576 return (-1);
577
578 return (mips_btop(dc->dc_addr + off));
579 }
580
581 static int
582 light_alloc_screen(void *c, const struct wsscreen_descr *type, void **cookiep,
583 int *curxp, int *curyp, long *attr)
584 {
585
586 return (ENOMEM);
587 }
588
589 static void
590 light_free_screen(void *c, void *cookie)
591 {
592
593 panic("light_free_screen");
594 }
595
596 static int
597 light_show_screen(void *c, void *cookie, int waitok,
598 void (*cb)(void *, int, int), void *cbarg)
599 {
600
601 return (0);
602 }
603