light.c revision 1.2 1 /* $Id: light.c,v 1.2 2006/12/28 16:33:49 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.2 2006/12/28 16:33:49 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 /* XXX */
274 return (1);
275 }
276
277 static void
278 light_attach_common(struct light_devconfig *dc, struct gio_attach_args *ga)
279 {
280
281 dc->dc_addr = ga->ga_addr;
282 dc->dc_st = ga->ga_iot;
283 dc->dc_sh = ga->ga_ioh;
284
285 dc->dc_boardrev = rex_revision(dc);
286
287 wsfont_init();
288
289 dc->dc_font = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
290 WSDISPLAY_FONTORDER_L2R);
291
292 if (dc->dc_font < 0)
293 panic("light_attach_common: no suitable fonts");
294
295 if (wsfont_lock(dc->dc_font, &dc->dc_fontdata))
296 panic("light_attach_common: unable to lock font data");
297
298 rex_vc1_write(dc, rex_vc1_read(dc) & ~(VC1_SYSCTL_CURSOR |
299 VC1_SYSCTL_CURSOR_ON));
300 rex_fill_rect(dc, 0, 0, LIGHT_XRES - 1, LIGHT_YRES - 1, 0);
301 }
302
303 static void
304 light_attach(struct device *parent, struct device *self, void *aux)
305 {
306 struct gio_attach_args *ga = aux;
307 struct light_softc *sc = (void *)self;
308 struct wsemuldisplaydev_attach_args wa;
309
310 if (light_is_console && ga->ga_addr == light_console_dc.dc_addr) {
311 wa.console = 1;
312 sc->sc_dc = &light_console_dc;
313 } else {
314 wa.console = 0;
315 sc->sc_dc = malloc(sizeof(struct light_devconfig), M_DEVBUF,
316 M_WAITOK | M_ZERO);
317 if (sc->sc_dc == NULL)
318 panic("light_attach: out of memory");
319
320 light_attach_common(sc->sc_dc, ga);
321 }
322
323 aprint_naive(": Display adapter\n");
324
325 aprint_normal(": SGI LG%d (board revision %d)\n",
326 LIGHT_IS_LG1(sc->sc_dc->dc_boardrev) ? 1 : 2,
327 sc->sc_dc->dc_boardrev);
328
329 wa.scrdata = &light_screenlist;
330 wa.accessops = &light_accessops;
331 wa.accesscookie = sc->sc_dc;
332
333 config_found(&sc->sc_dev, &wa, wsemuldisplaydevprint);
334 }
335
336 int
337 light_cnattach(struct gio_attach_args *ga)
338 {
339
340 if (!light_match(NULL, NULL, ga))
341 return (ENXIO);
342
343 light_attach_common(&light_console_dc, ga);
344
345 wsdisplay_cnattach(&light_screen, &light_console_dc, 0, 0,
346 LIGHT_ATTR_ENCODE(WSCOL_WHITE, WSCOL_BLACK));
347
348 light_is_console = 1;
349
350 return (0);
351 }
352
353 /*******************************************************************************
354 * wsdisplay_emulops
355 ******************************************************************************/
356
357 static void
358 light_cursor(void *c, int on, int row, int col)
359 {
360 /* XXX */
361 }
362
363 static int
364 light_mapchar(void *c, int ch, unsigned int *cp)
365 {
366 struct light_devconfig *dc = (void *)c;
367
368 if (dc->dc_fontdata->encoding != WSDISPLAY_FONTENC_ISO) {
369 ch = wsfont_map_unichar(dc->dc_fontdata, ch);
370
371 if (ch < 0)
372 goto fail;
373 }
374
375 if (ch < dc->dc_fontdata->firstchar ||
376 ch >= dc->dc_fontdata->firstchar + dc->dc_fontdata->numchars)
377 goto fail;
378
379 *cp = ch;
380 return 5;
381
382 fail:
383 *cp = ' ';
384 return 0;
385 }
386
387 static void
388 light_putchar(void *c, int row, int col, u_int ch, long attr)
389 {
390 struct light_devconfig *dc = c;
391 struct wsdisplay_font *font = dc->dc_fontdata;
392 uint8_t *bitmap;
393 uint32_t pattern;
394 int i, x, y;
395
396 bitmap = (u_int8_t *)font->data +
397 ((ch - font->firstchar) * font->fontheight * font->stride);
398 x = col * font->fontwidth;
399 y = row * font->fontheight;
400
401 rex_wait(dc);
402
403 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, y);
404 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, y + font->fontheight - 1);
405 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, x);
406 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, x + font->fontwidth - 1);
407 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORREDI, LIGHT_ATTR_FG(attr));
408 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORBACK, LIGHT_ATTR_BG(attr));
409 rex_write(dc, REX_PAGE0_GO, REX_P0REG_COMMAND, REX_OP_NOP);
410
411 rex_wait(dc);
412
413 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW |
414 REX_LOGICOP_SRC | REX_OP_FLG_ENZPATTERN | REX_OP_FLG_QUADMODE |
415 REX_OP_FLG_XYCONTINUE | REX_OP_FLG_STOPONX | REX_OP_FLG_BLOCK |
416 REX_OP_FLG_LENGTH32 | REX_OP_FLG_ZOPAQUE);
417
418 for (i = 0; i < font->fontheight; i++) {
419 /* XXX assumes font->fontwidth == 8 */
420 pattern = *bitmap << 24;
421 rex_write(dc, REX_PAGE0_GO, REX_P0REG_ZPATTERN, pattern);
422 bitmap += font->stride;
423 }
424 }
425
426 /* copy set of columns within the same line */
427 static void
428 light_copycols(void *c, int row, int srccol, int dstcol, int ncols)
429 {
430 struct light_devconfig *dc = c;
431 struct wsdisplay_font *font = dc->dc_fontdata;
432 int from_x, from_y, to_x, to_y, width, height;
433
434 from_x = srccol * font->fontwidth;
435 from_y = row * font->fontheight;
436 to_x = dstcol * font->fontwidth;
437 to_y = from_y;
438 width = ncols * font->fontwidth;
439 height = font->fontheight;
440
441 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
442 }
443
444 /* erase a set of columns in the same line */
445 static void
446 light_erasecols(void *c, int row, int startcol, int ncols, long attr)
447 {
448 struct light_devconfig *dc = c;
449 struct wsdisplay_font *font = dc->dc_fontdata;
450 int from_x, from_y, to_x, to_y;
451
452 from_x = startcol * font->fontwidth;
453 from_y = row * font->fontheight;
454 to_x = from_x + (ncols * font->fontwidth) - 1;
455 to_y = from_y + font->fontheight - 1;
456
457 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
458 }
459
460 /* copy a set of complete rows */
461 static void
462 light_copyrows(void *c, int srcrow, int dstrow, int nrows)
463 {
464 struct light_devconfig *dc = c;
465 struct wsdisplay_font *font = dc->dc_fontdata;
466 int from_x, from_y, to_x, to_y, width, height;
467
468 from_x = 0;
469 from_y = srcrow * font->fontheight;
470 to_x = 0;
471 to_y = dstrow * font->fontheight;
472 width = LIGHT_XRES;
473 height = nrows * font->fontheight;
474
475 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
476 }
477
478 /* erase a set of complete rows */
479 static void
480 light_eraserows(void *c, int row, int nrows, long attr)
481 {
482 struct light_devconfig *dc = c;
483 struct wsdisplay_font *font = dc->dc_fontdata;
484 int from_x, from_y, to_x, to_y;
485
486 from_x = 0;
487 from_y = row * font->fontheight;
488 to_x = LIGHT_XRES - 1;
489 to_y = from_y + (nrows * font->fontheight) - 1;
490
491 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
492 }
493
494 static int
495 light_allocattr(void *c, int fg, int bg, int flags, long *attr)
496 {
497
498 if (flags & ~(WSATTR_WSCOLORS | WSATTR_HILIT | WSATTR_REVERSE))
499 return (EINVAL);
500
501 if ((flags & WSATTR_WSCOLORS) == 0) {
502 fg = WSCOL_WHITE;
503 bg = WSCOL_BLACK;
504 }
505
506 if (flags & WSATTR_HILIT)
507 fg += 8;
508
509 if (flags & WSATTR_REVERSE) {
510 int tmp = fg;
511 fg = bg;
512 bg = tmp;
513 }
514
515 *attr = LIGHT_ATTR_ENCODE(fg, bg);
516 return (0);
517 }
518
519 /*******************************************************************************
520 * wsdisplay_accessops
521 ******************************************************************************/
522
523 static int
524 light_ioctl(void *c, void *vs, u_long cmd, caddr_t data, int flag,
525 struct lwp *l)
526 {
527 struct wsdisplay_fbinfo *fbinfo = (struct wsdisplay_fbinfo *)data;
528
529 switch (cmd) {
530 case WSDISPLAYIO_GINFO:
531 fbinfo->width = LIGHT_XRES;
532 fbinfo->height = LIGHT_YRES;
533 fbinfo->depth = LIGHT_DEPTH;
534 fbinfo->cmsize = 1 << LIGHT_DEPTH;
535 return (0);
536
537 case WSDISPLAYIO_GMODE:
538 *(u_int *)data = WSDISPLAYIO_MODE_EMUL;
539 break;
540
541 case WSDISPLAYIO_GTYPE:
542 *(u_int *)data = WSDISPLAY_TYPE_LIGHT;
543 return (0);
544
545 case WSDISPLAYIO_SVIDEO:
546 /*
547 * Turning off VC1 will stop refreshing the video ram (or so I
548 * suspect). We'll blank the screen after bringing it back up,
549 * since that's nicer than displaying garbage.
550 */
551 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
552 rex_vc1_write(c,rex_vc1_read(c) & ~VC1_SYSCTL_VIDEO_ON);
553 else {
554 rex_vc1_write(c, rex_vc1_read(c) | VC1_SYSCTL_VIDEO_ON);
555 rex_fill_rect(c, 0, 0, LIGHT_XRES-1, LIGHT_YRES-1, 0);
556 }
557 return (0);
558 }
559
560 return (EPASSTHROUGH);
561 }
562
563 static paddr_t
564 light_mmap(void *c, void *vs, off_t off, int prot)
565 {
566 struct light_devconfig *dc = c;
567
568 if (off >= 0x7fff)
569 return (-1);
570
571 return (mips_btop(dc->dc_addr + off));
572 }
573
574 static int
575 light_alloc_screen(void *c, const struct wsscreen_descr *type, void **cookiep,
576 int *curxp, int *curyp, long *attr)
577 {
578
579 return (ENOMEM);
580 }
581
582 static void
583 light_free_screen(void *c, void *cookie)
584 {
585
586 panic("light_free_screen");
587 }
588
589 static int
590 light_show_screen(void *c, void *cookie, int waitok,
591 void (*cb)(void *, int, int), void *cbarg)
592 {
593
594 return (0);
595 }
596