light.c revision 1.1 1 /* $Id: light.c,v 1.1 2006/12/26 04:28:16 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 "light", hence
42 * much similarity.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: light.c,v 1.1 2006/12/26 04:28:16 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 void
178 rex_wait(struct light_devconfig *dc)
179 {
180
181 while (rex_read(dc, REX_PAGE1_SET, REX_CFG_CONFIGMODE) & REX_BUSY)
182 ;
183 }
184
185 static int
186 rex_revision(struct light_devconfig *dc)
187 {
188
189 rex_write(dc, REX_PAGE1_SET, REX_CFG_CONFIGSEL, 4);
190 rex_read(dc, REX_PAGE1_GO, REX_CFG_WCLOCK);
191 return (rex_read(dc, REX_PAGE1_SET, REX_CFG_WCLOCK) & 0x7);
192 }
193
194 static void
195 rex_copy_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x,
196 int to_y, int width, int height)
197 {
198 int dx, dy, ystarti, yendi;
199
200 dx = from_x - to_x;
201 dy = from_y - to_y;
202
203 /* adjust for y. NB: STOPONX, STOPONY are inclusive */
204 if (to_y > from_y) {
205 ystarti = to_y + height - 1;
206 yendi = to_y;
207 } else {
208 ystarti = to_y;
209 yendi = to_y + height - 1;
210 }
211
212 rex_wait(dc);
213
214 rex_write(dc, REX_PAGE0_SET, REX_XSTARTI, to_x);
215 rex_write(dc, REX_PAGE0_SET, REX_XENDI, to_x + width);
216 rex_write(dc, REX_PAGE0_SET, REX_YSTARTI, ystarti);
217 rex_write(dc, REX_PAGE0_SET, REX_YENDI, yendi);
218 rex_write(dc, REX_PAGE0_SET, REX_COMMAND, REX_OPCODE_DRAW |
219 REX_LOGICOP_SRC | REX_OPFLG_LOGICSRC | REX_OPFLG_QUADMODE |
220 REX_OPFLG_BLOCK | REX_OPFLG_STOPONX | REX_OPFLG_STOPONY);
221 rex_write(dc, REX_PAGE0_GO, REX_XYMOVE,
222 ((dx << 16) & 0xffff0000) | (dy & 0x0000ffff));
223 }
224
225 static void
226 rex_fill_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x,
227 int to_y, long attr)
228 {
229
230 rex_wait(dc);
231
232 rex_write(dc, REX_PAGE0_SET, REX_YSTARTI, from_y);
233 rex_write(dc, REX_PAGE0_SET, REX_YENDI, to_y);
234 rex_write(dc, REX_PAGE0_SET, REX_XSTARTI, from_x);
235 rex_write(dc, REX_PAGE0_SET, REX_XENDI, to_x);
236 rex_write(dc, REX_PAGE0_SET, REX_COLORREDI, LIGHT_ATTR_BG(attr));
237 rex_write(dc, REX_PAGE0_SET, REX_COMMAND, REX_OPCODE_DRAW |
238 REX_LOGICOP_SRC | REX_OPFLG_QUADMODE | REX_OPFLG_BLOCK |
239 REX_OPFLG_STOPONX | REX_OPFLG_STOPONY);
240 rex_read(dc, REX_PAGE0_GO, REX_COMMAND);
241 }
242
243 /*******************************************************************************
244 * match/attach functions
245 ******************************************************************************/
246
247 static int
248 light_match(struct device *parent, struct cfdata *self, void *aux)
249 {
250 struct gio_attach_args *ga = aux;
251
252 if (ga->ga_addr != LIGHT_ADDR_0 && ga->ga_addr != LIGHT_ADDR_1)
253 return (0);
254
255 /* XXX */
256 return (1);
257 }
258
259 static void
260 light_attach_common(struct light_devconfig *dc, struct gio_attach_args *ga)
261 {
262
263 dc->dc_addr = ga->ga_addr;
264 dc->dc_st = ga->ga_iot;
265 dc->dc_sh = ga->ga_ioh;
266
267 dc->dc_boardrev = rex_revision(dc);
268
269 wsfont_init();
270
271 dc->dc_font = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
272 WSDISPLAY_FONTORDER_L2R);
273
274 if (dc->dc_font < 0)
275 panic("light_attach_common: no suitable fonts");
276
277 if (wsfont_lock(dc->dc_font, &dc->dc_fontdata))
278 panic("light_attach_common: unable to lock font data");
279
280 rex_fill_rect(dc, 0, 0, LIGHT_XRES - 1, LIGHT_YRES - 1, 0);
281 }
282
283 static void
284 light_attach(struct device *parent, struct device *self, void *aux)
285 {
286 struct gio_attach_args *ga = aux;
287 struct light_softc *sc = (void *)self;
288 struct wsemuldisplaydev_attach_args wa;
289
290 if (light_is_console && ga->ga_addr == light_console_dc.dc_addr) {
291 wa.console = 1;
292 sc->sc_dc = &light_console_dc;
293 } else {
294 wa.console = 0;
295 sc->sc_dc = malloc(sizeof(struct light_devconfig), M_DEVBUF,
296 M_WAITOK | M_ZERO);
297 if (sc->sc_dc == NULL)
298 panic("light_attach: out of memory");
299
300 light_attach_common(sc->sc_dc, ga);
301 }
302
303 aprint_naive(": Display adapter\n");
304
305 aprint_normal(": SGI LG%d (board revision %d) 1024x768x8bpp at 60Hz\n",
306 LIGHT_IS_LG1(sc->sc_dc->dc_boardrev) ? 1 : 2,
307 sc->sc_dc->dc_boardrev);
308
309 wa.scrdata = &light_screenlist;
310 wa.accessops = &light_accessops;
311 wa.accesscookie = sc->sc_dc;
312
313 config_found(&sc->sc_dev, &wa, wsemuldisplaydevprint);
314 }
315
316 int
317 light_cnattach(struct gio_attach_args *ga)
318 {
319
320 if (!light_match(NULL, NULL, ga))
321 return (ENXIO);
322
323 light_attach_common(&light_console_dc, ga);
324
325 wsdisplay_cnattach(&light_screen, &light_console_dc, 0, 0,
326 LIGHT_ATTR_ENCODE(WSCOL_WHITE, WSCOL_BLACK));
327
328 light_is_console = 1;
329
330 return (0);
331 }
332
333 /*******************************************************************************
334 * wsdisplay_emulops
335 ******************************************************************************/
336
337 static void
338 light_cursor(void *c, int on, int row, int col)
339 {
340 /* XXX */
341 }
342
343 static int
344 light_mapchar(void *c, int ch, unsigned int *cp)
345 {
346 struct light_devconfig *dc = (void *)c;
347
348 if (dc->dc_fontdata->encoding != WSDISPLAY_FONTENC_ISO) {
349 ch = wsfont_map_unichar(dc->dc_fontdata, ch);
350
351 if (ch < 0)
352 goto fail;
353 }
354
355 if (ch < dc->dc_fontdata->firstchar ||
356 ch >= dc->dc_fontdata->firstchar + dc->dc_fontdata->numchars)
357 goto fail;
358
359 *cp = ch;
360 return 5;
361
362 fail:
363 *cp = ' ';
364 return 0;
365 }
366
367 static void
368 light_putchar(void *c, int row, int col, u_int ch, long attr)
369 {
370 struct light_devconfig *dc = c;
371 struct wsdisplay_font *font = dc->dc_fontdata;
372 uint8_t *bitmap;
373 uint32_t pattern;
374 int i, x, y;
375
376 bitmap = (u_int8_t *)font->data +
377 ((ch - font->firstchar) * font->fontheight * font->stride);
378 x = col * font->fontwidth;
379 y = row * font->fontheight;
380
381 rex_wait(dc);
382
383 rex_write(dc, REX_PAGE0_SET, REX_YSTARTI, y);
384 rex_write(dc, REX_PAGE0_SET, REX_YENDI, y + font->fontheight - 1);
385 rex_write(dc, REX_PAGE0_SET, REX_XSTARTI, x);
386 rex_write(dc, REX_PAGE0_SET, REX_XENDI, x + font->fontwidth - 1);
387 rex_write(dc, REX_PAGE0_SET, REX_COLORREDI, LIGHT_ATTR_FG(attr));
388 rex_write(dc, REX_PAGE0_SET, REX_COLORBACK, LIGHT_ATTR_BG(attr));
389 rex_write(dc, REX_PAGE0_GO, REX_COMMAND, REX_OPCODE_NOP);
390
391 rex_wait(dc);
392
393 rex_write(dc, REX_PAGE0_SET, REX_COMMAND, REX_OPCODE_DRAW |
394 REX_LOGICOP_SRC | REX_OPFLG_ENZPATTERN | REX_OPFLG_QUADMODE |
395 REX_OPFLG_XYCONTINUE | REX_OPFLG_STOPONX | REX_OPFLG_BLOCK |
396 REX_OPFLG_LENGTH32 | REX_OPFLG_ZOPAQUE);
397
398 for (i = 0; i < font->fontheight; i++) {
399 /* XXX assumes font->fontwidth == 8 */
400 pattern = *bitmap << 24;
401 rex_write(dc, REX_PAGE0_GO, REX_ZPATTERN, pattern);
402 bitmap += font->stride;
403 }
404 }
405
406 /* copy set of columns within the same line */
407 static void
408 light_copycols(void *c, int row, int srccol, int dstcol, int ncols)
409 {
410 struct light_devconfig *dc = c;
411 struct wsdisplay_font *font = dc->dc_fontdata;
412 int from_x, from_y, to_x, to_y, width, height;
413
414 from_x = srccol * font->fontwidth;
415 from_y = row * font->fontheight;
416 to_x = dstcol * font->fontwidth;
417 to_y = from_y;
418 width = ncols * font->fontwidth;
419 height = font->fontheight;
420
421 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
422 }
423
424 /* erase a set of columns in the same line */
425 static void
426 light_erasecols(void *c, int row, int startcol, int ncols, long attr)
427 {
428 struct light_devconfig *dc = c;
429 struct wsdisplay_font *font = dc->dc_fontdata;
430 int from_x, from_y, to_x, to_y;
431
432 from_x = startcol * font->fontwidth;
433 from_y = row * font->fontheight;
434 to_x = from_x + (ncols * font->fontwidth) - 1;
435 to_y = from_y + font->fontheight - 1;
436
437 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
438 }
439
440 /* copy a set of complete rows */
441 static void
442 light_copyrows(void *c, int srcrow, int dstrow, int nrows)
443 {
444 struct light_devconfig *dc = c;
445 struct wsdisplay_font *font = dc->dc_fontdata;
446 int from_x, from_y, to_x, to_y, width, height;
447
448 from_x = 0;
449 from_y = srcrow * font->fontheight;
450 to_x = 0;
451 to_y = dstrow * font->fontheight;
452 width = LIGHT_XRES;
453 height = nrows * font->fontheight;
454
455 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height);
456 }
457
458 /* erase a set of complete rows */
459 static void
460 light_eraserows(void *c, int row, int nrows, long attr)
461 {
462 struct light_devconfig *dc = c;
463 struct wsdisplay_font *font = dc->dc_fontdata;
464 int from_x, from_y, to_x, to_y;
465
466 from_x = 0;
467 from_y = row * font->fontheight;
468 to_x = LIGHT_XRES - 1;
469 to_y = from_y + (nrows * font->fontheight) - 1;
470
471 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr);
472 }
473
474 static int
475 light_allocattr(void *c, int fg, int bg, int flags, long *attr)
476 {
477
478 if (flags & ~(WSATTR_WSCOLORS | WSATTR_HILIT | WSATTR_REVERSE)) {
479 printf("allocattr: bad attrs: 0x%08x\n", flags);
480 return (EINVAL);
481 }
482
483 if ((flags & WSATTR_WSCOLORS) == 0) {
484 fg = WSCOL_WHITE;
485 bg = WSCOL_BLACK;
486 }
487
488 if (flags & WSATTR_HILIT)
489 fg += 8;
490
491 if (flags & WSATTR_REVERSE) {
492 int tmp = fg;
493 fg = bg;
494 bg = tmp;
495 }
496
497 *attr = LIGHT_ATTR_ENCODE(fg, bg);
498 return (0);
499 }
500
501 /*******************************************************************************
502 * wsdisplay_accessops
503 ******************************************************************************/
504
505 static int
506 light_ioctl(void *c, void *vs, u_long cmd, caddr_t data, int flag,
507 struct lwp *l)
508 {
509 struct wsdisplay_fbinfo *fbinfo = (struct wsdisplay_fbinfo *)data;
510
511 switch (cmd) {
512 case WSDISPLAYIO_GINFO:
513 fbinfo->width = LIGHT_XRES;
514 fbinfo->height = LIGHT_YRES;
515 fbinfo->depth = LIGHT_DEPTH;
516 fbinfo->cmsize = 1 << LIGHT_DEPTH;
517 return (0);
518
519 case WSDISPLAYIO_GTYPE:
520 *(u_int *)data = WSDISPLAY_TYPE_LIGHT;
521 return (0);
522 }
523
524 return (EPASSTHROUGH);
525 }
526
527 static paddr_t
528 light_mmap(void *c, void *vs, off_t off, int prot)
529 {
530
531 /* XXX */
532 panic("light mmap");
533 }
534
535 static int
536 light_alloc_screen(void *c, const struct wsscreen_descr *type, void **cookiep,
537 int *curxp, int *curyp, long *attr)
538 {
539
540 return (ENOMEM);
541 }
542
543 static void
544 light_free_screen(void *c, void *cookie)
545 {
546
547 panic("light_free_screen");
548 }
549
550 static int
551 light_show_screen(void *c, void *cookie, int waitok,
552 void (*cb)(void *, int, int), void *cbarg)
553 {
554
555 return (0);
556 }
557