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