xref: /netbsd-src/sys/arch/sgimips/gio/newport.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
1 /*	$NetBSD: newport.c,v 1.20 2019/05/10 23:21:42 macallan Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Ilpo Ruotsalainen
5  *               2009 Michael Lorenz
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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: newport.c,v 1.20 2019/05/10 23:21:42 macallan Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 
41 #include <machine/sysconf.h>
42 
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/wsfont/wsfont.h>
46 #include <dev/rasops/rasops.h>
47 #include <dev/wscons/wsdisplay_vconsvar.h>
48 
49 #include <sgimips/gio/giovar.h>
50 #include <sgimips/gio/newportvar.h>
51 #include <sgimips/gio/newportreg.h>
52 
53 struct newport_softc {
54 	device_t sc_dev;
55 
56 	struct newport_devconfig *sc_dc;
57 
58 };
59 
60 struct newport_devconfig {
61 	bus_space_tag_t		dc_st;
62 	bus_space_handle_t	dc_sh;
63 	bus_addr_t		dc_addr;
64 
65 	int			dc_boardrev;
66 	int			dc_vc2rev;
67 	int			dc_cmaprev;
68 	int			dc_xmaprev;
69 	int			dc_rexrev;
70 	int			dc_xres;
71 	int			dc_yres;
72 	int			dc_depth;
73 
74 	int			dc_font;
75 	struct wsscreen_descr	*dc_screen;
76 	int			dc_mode;
77 	struct vcons_data	dc_vd;
78 };
79 
80 static int  newport_match(device_t, struct cfdata *, void *);
81 static void newport_attach(device_t, device_t, void *);
82 
83 CFATTACH_DECL_NEW(newport, sizeof(struct newport_softc),
84     newport_match, newport_attach, NULL, NULL);
85 
86 /* textops */
87 static void newport_cursor(void *, int, int, int);
88 static void newport_cursor_dummy(void *, int, int, int);
89 static void newport_putchar(void *, int, int, u_int, long);
90 static void newport_copycols(void *, int, int, int, int);
91 static void newport_erasecols(void *, int, int, int, long);
92 static void newport_copyrows(void *, int, int, int);
93 static void newport_eraserows(void *, int, int, long);
94 
95 static void newport_init_screen(void *, struct vcons_screen *, int, long *);
96 
97 /* accessops */
98 static int     newport_ioctl(void *, void *, u_long, void *, int,
99     struct lwp *);
100 static paddr_t newport_mmap(void *, void *, off_t, int);
101 
102 static struct wsdisplay_accessops newport_accessops = {
103 	.ioctl		= newport_ioctl,
104 	.mmap		= newport_mmap,
105 };
106 
107 static struct wsscreen_descr newport_screen = {
108 	.name		= "default",
109 	.ncols		= 160,
110 	.nrows		= 64,
111 	.fontwidth	= 8,
112 	.fontheight	= 16,
113 	.capabilities	= WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_REVERSE
114 };
115 
116 static const struct wsscreen_descr *_newport_screenlist[] = {
117 	&newport_screen
118 };
119 
120 static const struct wsscreen_list newport_screenlist = {
121 	sizeof(_newport_screenlist) / sizeof(struct wsscreen_descr *),
122 	_newport_screenlist
123 };
124 
125 static struct vcons_screen newport_console_screen;
126 static struct newport_devconfig newport_console_dc;
127 static int newport_is_console = 0;
128 
129 uint8_t our_cmap[768];
130 
131 /**** Low-level hardware register groveling functions ****/
132 static void
133 rex3_write(struct newport_devconfig *dc, bus_size_t rexreg, uint32_t val)
134 {
135 	bus_space_write_4(dc->dc_st, dc->dc_sh, NEWPORT_REX3_OFFSET + rexreg,
136 	    val);
137 }
138 
139 static void
140 rex3_write_go(struct newport_devconfig *dc, bus_size_t rexreg, uint32_t val)
141 {
142 	rex3_write(dc, rexreg + REX3_REG_GO, val);
143 }
144 
145 static uint32_t
146 rex3_read(struct newport_devconfig *dc, bus_size_t rexreg)
147 {
148 	return bus_space_read_4(dc->dc_st, dc->dc_sh, NEWPORT_REX3_OFFSET +
149 	    rexreg);
150 }
151 
152 static void
153 rex3_wait_gfifo(struct newport_devconfig *dc)
154 {
155 	while (rex3_read(dc, REX3_REG_STATUS) &
156 	    (REX3_STATUS_GFXBUSY | REX3_STATUS_PIPELEVEL_MASK))
157 		;
158 }
159 
160 static void
161 vc2_write_ireg(struct newport_devconfig *dc, uint8_t ireg, uint16_t val)
162 {
163 	rex3_write(dc, REX3_REG_DCBMODE,
164 	    REX3_DCBMODE_DW_3 |
165 	    REX3_DCBMODE_ENCRSINC |
166 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
167 	    (VC2_DCBCRS_INDEX << REX3_DCBMODE_DCBCRS_SHIFT) |
168 	    REX3_DCBMODE_ENASYNCACK |
169 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
170 
171 	rex3_write(dc, REX3_REG_DCBDATA0, (ireg << 24) | (val << 8));
172 }
173 
174 static uint16_t
175 vc2_read_ireg(struct newport_devconfig *dc, uint8_t ireg)
176 {
177 	rex3_write(dc, REX3_REG_DCBMODE,
178 	    REX3_DCBMODE_DW_1 |
179 	    REX3_DCBMODE_ENCRSINC |
180 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
181 	    (VC2_DCBCRS_INDEX << REX3_DCBMODE_DCBCRS_SHIFT) |
182 	    REX3_DCBMODE_ENASYNCACK |
183 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
184 
185 	rex3_write(dc, REX3_REG_DCBDATA0, ireg << 24);
186 
187 	rex3_write(dc, REX3_REG_DCBMODE,
188 	    REX3_DCBMODE_DW_2 |
189 	    REX3_DCBMODE_ENCRSINC |
190 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
191 	    (VC2_DCBCRS_IREG << REX3_DCBMODE_DCBCRS_SHIFT) |
192 	    REX3_DCBMODE_ENASYNCACK |
193 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
194 
195 	return (uint16_t)(rex3_read(dc, REX3_REG_DCBDATA0) >> 16);
196 }
197 
198 static uint16_t
199 vc2_read_ram(struct newport_devconfig *dc, uint16_t addr)
200 {
201 	vc2_write_ireg(dc, VC2_IREG_RAM_ADDRESS, addr);
202 
203 	rex3_write(dc, REX3_REG_DCBMODE,
204 	    REX3_DCBMODE_DW_2 |
205 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
206 	    (VC2_DCBCRS_RAM << REX3_DCBMODE_DCBCRS_SHIFT) |
207 	    REX3_DCBMODE_ENASYNCACK |
208 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
209 
210 	return (uint16_t)(rex3_read(dc, REX3_REG_DCBDATA0) >> 16);
211 }
212 
213 #if 0
214 static void
215 vc2_write_ram(struct newport_devconfig *dc, uint16_t addr, uint16_t val)
216 {
217 	vc2_write_ireg(dc, VC2_IREG_RAM_ADDRESS, addr);
218 
219 	rex3_write(dc, REX3_REG_DCBMODE,
220 	    REX3_DCBMODE_DW_2 |
221 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
222 	    (VC2_DCBCRS_RAM << REX3_DCBMODE_DCBCRS_SHIFT) |
223 	    REX3_DCBMODE_ENASYNCACK |
224 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
225 
226 	rex3_write(dc, REX3_REG_DCBDATA0, val << 16);
227 }
228 #endif
229 
230 static u_int32_t
231 xmap9_read(struct newport_devconfig *dc, int crs)
232 {
233 	rex3_write(dc, REX3_REG_DCBMODE,
234 		REX3_DCBMODE_DW_1 |
235 		(NEWPORT_DCBADDR_XMAP_0 << REX3_DCBMODE_DCBADDR_SHIFT) |
236 		(crs << REX3_DCBMODE_DCBCRS_SHIFT) |
237 		(3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
238 		(2 << REX3_DCBMODE_CSHOLD_SHIFT) |
239 		(1 << REX3_DCBMODE_CSSETUP_SHIFT));
240 	return rex3_read(dc, REX3_REG_DCBDATA0);
241 }
242 
243 static void
244 xmap9_write(struct newport_devconfig *dc, int crs, uint8_t val)
245 {
246 	rex3_write(dc, REX3_REG_DCBMODE,
247 	    REX3_DCBMODE_DW_1 |
248 	    (NEWPORT_DCBADDR_XMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
249 	    (crs << REX3_DCBMODE_DCBCRS_SHIFT) |
250 	    (3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
251 	    (2 << REX3_DCBMODE_CSHOLD_SHIFT) |
252 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
253 
254 	rex3_write(dc, REX3_REG_DCBDATA0, val << 24);
255 }
256 
257 static inline void
258 xmap9_wait(struct newport_devconfig *dc)
259 {
260 	do {} while (xmap9_read(dc, XMAP9_DCBCRS_FIFOAVAIL) == 0);
261 }
262 
263 static void
264 xmap9_write_mode(struct newport_devconfig *dc, uint8_t index, uint32_t mode)
265 {
266 	volatile uint32_t junk;
267 	/* wait for FIFO if needed */
268 	xmap9_wait(dc);
269 
270 	rex3_write(dc, REX3_REG_DCBMODE,
271 	    REX3_DCBMODE_DW_4 |
272 	    (NEWPORT_DCBADDR_XMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
273 	    (XMAP9_DCBCRS_MODE_SETUP << REX3_DCBMODE_DCBCRS_SHIFT) |
274 	    (3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
275 	    (2 << REX3_DCBMODE_CSHOLD_SHIFT) |
276 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
277 
278 	xmap9_wait(dc);
279 
280 	rex3_write(dc, REX3_REG_DCBDATA0, (index << 24) | mode);
281 	junk = rex3_read(dc, REX3_REG_DCBDATA0);
282 	__USE(junk);
283 }
284 
285 /**** Helper functions ****/
286 static void
287 newport_fill_rectangle(struct newport_devconfig *dc, int x1, int y1, int wi,
288     int he, uint32_t color)
289 {
290 	int x2 = x1 + wi - 1;
291 	int y2 = y1 + he - 1;
292 
293 	rex3_wait_gfifo(dc);
294 
295 	rex3_write(dc, REX3_REG_DRAWMODE0, REX3_DRAWMODE0_OPCODE_DRAW |
296 	    REX3_DRAWMODE0_ADRMODE_BLOCK | REX3_DRAWMODE0_DOSETUP |
297 	    REX3_DRAWMODE0_STOPONX | REX3_DRAWMODE0_STOPONY);
298 	rex3_write(dc, REX3_REG_CLIPMODE, 0x1e00);
299 	rex3_write(dc, REX3_REG_DRAWMODE1,
300 	    REX3_DRAWMODE1_PLANES_RGB |
301 	    REX3_DRAWMODE1_DD_DD8 |
302 	    REX3_DRAWMODE1_RWPACKED |
303 	    REX3_DRAWMODE1_HD_HD8 |
304 	    REX3_DRAWMODE1_COMPARE_LT |
305 	    REX3_DRAWMODE1_COMPARE_EQ |
306 	    REX3_DRAWMODE1_COMPARE_GT |
307 	    REX3_DRAWMODE1_RGBMODE |
308 	    REX3_DRAWMODE1_FASTCLEAR |
309 	    REX3_DRAWMODE1_LO_SRC);
310 	rex3_write(dc, REX3_REG_WRMASK, 0xffffffff);
311 	rex3_write(dc, REX3_REG_COLORVRAM, color);
312 	rex3_write(dc, REX3_REG_XYSTARTI, (x1 << REX3_XYSTARTI_XSHIFT) | y1);
313 
314 	rex3_write_go(dc, REX3_REG_XYENDI, (x2 << REX3_XYENDI_XSHIFT) | y2);
315 }
316 
317 static void
318 newport_bitblt(struct newport_devconfig *dc, int xs, int ys, int xd,
319     int yd, int wi, int he, int rop)
320 {
321 	int xe, ye;
322 	uint32_t tmp;
323 
324 	rex3_wait_gfifo(dc);
325 	if (yd > ys) {
326 		/* need to copy bottom up */
327 		ye = ys;
328 		yd += he - 1;
329 		ys += he - 1;
330 	} else
331 		ye = ys + he - 1;
332 
333 	if (xd > xs) {
334 		/* need to copy right to left */
335 		xe = xs;
336 		xd += wi - 1;
337 		xs += wi - 1;
338 	} else
339 		xe = xs + wi - 1;
340 
341 	rex3_write(dc, REX3_REG_DRAWMODE0, REX3_DRAWMODE0_OPCODE_SCR2SCR |
342 	    REX3_DRAWMODE0_ADRMODE_BLOCK | REX3_DRAWMODE0_DOSETUP |
343 	    REX3_DRAWMODE0_STOPONX | REX3_DRAWMODE0_STOPONY);
344 	rex3_write(dc, REX3_REG_DRAWMODE1,
345 	    REX3_DRAWMODE1_PLANES_CI |
346 	    REX3_DRAWMODE1_DD_DD8 |
347 	    REX3_DRAWMODE1_RWPACKED |
348 	    REX3_DRAWMODE1_HD_HD8 |
349 	    REX3_DRAWMODE1_COMPARE_LT |
350 	    REX3_DRAWMODE1_COMPARE_EQ |
351 	    REX3_DRAWMODE1_COMPARE_GT |
352 	    ((rop << 28) & REX3_DRAWMODE1_LOGICOP_MASK));
353 	rex3_write(dc, REX3_REG_XYSTARTI, (xs << REX3_XYSTARTI_XSHIFT) | ys);
354 	rex3_write(dc, REX3_REG_XYENDI, (xe << REX3_XYENDI_XSHIFT) | ye);
355 
356 	tmp = (yd - ys) & 0xffff;
357 	tmp |= (xd - xs) << REX3_XYMOVE_XSHIFT;
358 
359 	rex3_write_go(dc, REX3_REG_XYMOVE, tmp);
360 }
361 
362 static void
363 newport_cmap_setrgb(struct newport_devconfig *dc, int index, uint8_t r,
364     uint8_t g, uint8_t b)
365 {
366 	rex3_write(dc, REX3_REG_DCBMODE,
367 	    REX3_DCBMODE_DW_2 |
368 	    REX3_DCBMODE_ENCRSINC |
369 	    (NEWPORT_DCBADDR_CMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
370 	    (CMAP_DCBCRS_ADDRESS_LOW << REX3_DCBMODE_DCBCRS_SHIFT) |
371 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
372 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
373 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT) |
374 	    REX3_DCBMODE_SWAPENDIAN);
375 
376 	rex3_write(dc, REX3_REG_DCBDATA0, index << 16);
377 
378 	rex3_write(dc, REX3_REG_DCBMODE,
379 	    REX3_DCBMODE_DW_3 |
380 	    (NEWPORT_DCBADDR_CMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
381 	    (CMAP_DCBCRS_PALETTE << REX3_DCBMODE_DCBCRS_SHIFT) |
382 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
383 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
384 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
385 
386 	rex3_write(dc, REX3_REG_DCBDATA0, (r << 24) + (g << 16) + (b << 8));
387 }
388 
389 static void
390 newport_get_resolution(struct newport_devconfig *dc)
391 {
392 	uint16_t vep,lines;
393 	uint16_t linep,cols;
394 	uint16_t data;
395 
396 	vep = vc2_read_ireg(dc, VC2_IREG_VIDEO_ENTRY);
397 
398 	dc->dc_xres = 0;
399 	dc->dc_yres = 0;
400 
401 	for (;;) {
402 		/* Iterate over runs in video timing table */
403 
404 		cols = 0;
405 
406 		linep = vc2_read_ram(dc, vep++);
407 		lines = vc2_read_ram(dc, vep++);
408 
409 		if (lines == 0)
410 			break;
411 
412 		do {
413 			/* Iterate over state runs in line sequence table */
414 
415 			data = vc2_read_ram(dc, linep++);
416 
417 			if ((data & 0x0001) == 0)
418 				cols += (data >> 7) & 0xfe;
419 
420 			if ((data & 0x0080) == 0)
421 				data = vc2_read_ram(dc, linep++);
422 		} while ((data & 0x8000) == 0);
423 
424 		if (cols != 0) {
425 			if (cols > dc->dc_xres)
426 				dc->dc_xres = cols;
427 
428 			dc->dc_yres += lines;
429 		}
430 	}
431 }
432 
433 static void
434 newport_setup_hw(struct newport_devconfig *dc, int depth)
435 {
436 	uint16_t __unused(curp), tmp;
437 	int i;
438 	uint32_t scratch;
439 
440 	/* Get various revisions */
441 	rex3_write(dc, REX3_REG_DCBMODE,
442 	    REX3_DCBMODE_DW_1 |
443 	    (NEWPORT_DCBADDR_CMAP_0 << REX3_DCBMODE_DCBADDR_SHIFT) |
444 	    (CMAP_DCBCRS_REVISION << REX3_DCBMODE_DCBCRS_SHIFT) |
445 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
446 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
447 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
448 
449 	scratch = vc2_read_ireg(dc, VC2_IREG_CONFIG);
450 	dc->dc_vc2rev = (scratch & VC2_IREG_CONFIG_REVISION) >> 5;
451 
452 	scratch = rex3_read(dc, REX3_REG_DCBDATA0);
453 
454 	dc->dc_boardrev = (scratch >> 28) & 0x07;
455 	dc->dc_cmaprev = scratch & 0x07;
456 	dc->dc_xmaprev = xmap9_read(dc, XMAP9_DCBCRS_REVISION) & 0x07;
457 	dc->dc_depth = ( (dc->dc_boardrev > 1) && (scratch & 0x80)) ? 8 : 24;
458 
459 	/* Setup cursor glyph */
460 	curp = vc2_read_ireg(dc, VC2_IREG_CURSOR_ENTRY);
461 
462 	/* Setup VC2 to a known state */
463 	tmp = vc2_read_ireg(dc, VC2_IREG_CONTROL) & VC2_CONTROL_INTERLACE;
464 	vc2_write_ireg(dc, VC2_IREG_CONTROL, tmp |
465 	    VC2_CONTROL_DISPLAY_ENABLE |
466 	    VC2_CONTROL_VTIMING_ENABLE |
467 	    VC2_CONTROL_DID_ENABLE |
468 	    VC2_CONTROL_CURSORFUNC_ENABLE /*|
469 	    VC2_CONTROL_CURSOR_ENABLE*/);
470 
471 	/* disable all clipping */
472 	rex3_write(dc, REX3_REG_CLIPMODE, 0x1e00);
473 
474 	/* Setup XMAP9s */
475 	xmap9_write(dc, XMAP9_DCBCRS_CURSOR_CMAP, 0);
476 
477 	if (depth == 8) {
478 
479 		for (i = 0; i < 32; i++) {
480 			xmap9_write_mode(dc, i,
481 			    XMAP9_MODE_GAMMA_BYPASS |
482 			    XMAP9_MODE_PIXSIZE_8BPP | XMAP9_MODE_PIXMODE_RGB2);
483 		}
484 		xmap9_write(dc, XMAP9_DCBCRS_MODE_SELECT, 0);
485 
486 		/* Setup REX3 */
487 		rex3_write(dc, REX3_REG_XYWIN, (4096 << 16) | 4096);
488 		rex3_write(dc, REX3_REG_TOPSCAN, 0x3ff); /* XXX Why? XXX */
489 
490 		/* Setup CMAP */
491 		uint8_t ctmp;
492 		for (i = 0; i < 256; i++) {
493 			ctmp = i & 0xe0;
494 			/*
495 			 * replicate bits so 0xe0 maps to a red value of 0xff
496 			 * in order to make white look actually white
497 			 */
498 			ctmp |= (ctmp >> 3) | (ctmp >> 6);
499 			our_cmap[i * 3] = ctmp;
500 
501 			ctmp = (i & 0x1c) << 3;
502 			ctmp |= (ctmp >> 3) | (ctmp >> 6);
503 			our_cmap[i * 3 + 1] = ctmp;
504 
505 			ctmp = (i & 0x03) << 6;
506 			ctmp |= ctmp >> 2;
507 			ctmp |= ctmp >> 4;
508 			our_cmap[i * 3 + 2] = ctmp;
509 
510 			newport_cmap_setrgb(dc, i, our_cmap[i * 3],
511 			    our_cmap[i * 3 + 1], our_cmap[i * 3 + 2]);
512 		}
513 		/* Write a ramp into RGB2 cmap */
514 		for (i = 0; i < 256; i++)
515 			newport_cmap_setrgb(dc, 0x1f00 + i, i, i, i);
516 	} else {
517 		uint8_t dcbcfg;
518 
519 		dcbcfg = xmap9_read(dc, XMAP9_DCBCRS_CONFIG);
520 		aprint_debug("dcbcfg: %02x\n", dcbcfg);
521 		dcbcfg &= 0x3c;
522 		xmap9_write(dc, XMAP9_DCBCRS_CONFIG, dcbcfg | XMAP9_CONFIG_RGBMAP_2);
523 
524 		for (i = 0; i < 32; i++) {
525 			xmap9_write_mode(dc, i,
526 			    XMAP9_MODE_GAMMA_BYPASS |
527 			    XMAP9_MODE_PIXSIZE_24BPP |
528 			    XMAP9_MODE_PIXMODE_RGB2);
529 		}
530 
531 		/* Setup REX3 */
532 		rex3_write(dc, REX3_REG_XYWIN, (4096 << 16) | 4096);
533 		rex3_write(dc, REX3_REG_TOPSCAN, 0x3ff); /* XXX Why? XXX */
534 
535 		/* Write a ramp into RGB2 cmap */
536 		for (i = 0; i < 256; i++)
537 			newport_cmap_setrgb(dc, 0x1f00 + i, i, i, i);
538 	}
539 }
540 
541 /**** Attach routines ****/
542 static int
543 newport_match(device_t parent, struct cfdata *self, void *aux)
544 {
545 	struct gio_attach_args *ga = aux;
546 
547 	/* newport doesn't decode all addresses */
548 	if (ga->ga_addr != 0x1f000000 && ga->ga_addr != 0x1f400000 &&
549 	    ga->ga_addr != 0x1f800000 && ga->ga_addr != 0x1fc00000)
550 		return 0;
551 
552 	/* Don't do the destructive probe if we're already attached */
553 	if (newport_is_console && ga->ga_addr == newport_console_dc.dc_addr)
554 		return 1;
555 
556 	if (platform.badaddr(
557 	    (void *)MIPS_PHYS_TO_KSEG1(ga->ga_addr + NEWPORT_REX3_OFFSET + REX3_REG_XSTARTI),
558 	    sizeof(uint32_t)))
559 		return 0;
560 	if (platform.badaddr(
561 	    (void *)MIPS_PHYS_TO_KSEG1(ga->ga_addr + NEWPORT_REX3_OFFSET + REX3_REG_XSTART),
562 	    sizeof(uint32_t)))
563 		return 0;
564 
565 	/* Ugly, this probe is destructive, blame SGI... */
566 	/* XXX Should be bus_space_peek/bus_space_poke XXX */
567 	bus_space_write_4(ga->ga_iot, ga->ga_ioh,
568 	    NEWPORT_REX3_OFFSET + REX3_REG_XSTARTI, 0x12345678);
569 	if (bus_space_read_4(ga->ga_iot, ga->ga_ioh,
570 	      NEWPORT_REX3_OFFSET + REX3_REG_XSTART)
571 	    != ((0x12345678 & 0xffff) << 11))
572 		return 0;
573 
574 	return 1;
575 }
576 
577 static void
578 newport_attach_common(struct newport_devconfig *dc, struct gio_attach_args *ga)
579 {
580 	dc->dc_addr = ga->ga_addr;
581 
582 	dc->dc_st = ga->ga_iot;
583 	dc->dc_sh = ga->ga_ioh;
584 
585 	newport_setup_hw(dc, 8);
586 
587 	newport_get_resolution(dc);
588 
589 	newport_fill_rectangle(dc, 0, 0, dc->dc_xres, dc->dc_yres, 0);
590 
591 #ifdef NEWPORT_DEBUG
592 	int i;
593 	for (i = 0; i < 256; i++)
594 		newport_fill_rectangle(dc, 10, i * 3 + 10, 500, 2, i);
595 	delay(10000000);
596 #endif
597 	dc->dc_screen = &newport_screen;
598 
599 	dc->dc_mode = WSDISPLAYIO_MODE_EMUL;
600 }
601 
602 static void
603 newport_attach(device_t parent, device_t self, void *aux)
604 {
605 	struct gio_attach_args *ga = aux;
606 	struct newport_softc *sc = device_private(self);
607 	struct wsemuldisplaydev_attach_args wa;
608 	unsigned long defattr;
609 
610 	sc->sc_dev = self;
611 	if (newport_is_console && ga->ga_addr == newport_console_dc.dc_addr) {
612 		wa.console = 1;
613 		sc->sc_dc = &newport_console_dc;
614 	} else {
615 		wa.console = 0;
616 		sc->sc_dc = malloc(sizeof(struct newport_devconfig),
617 		    M_DEVBUF, M_WAITOK | M_ZERO);
618 		if (sc->sc_dc == NULL)
619 			panic("newport_attach: out of memory");
620 
621 		newport_attach_common(sc->sc_dc, ga);
622 	}
623 
624 	aprint_naive(": Display adapter\n");
625 
626 	aprint_normal(": SGI NG1 (board revision %d, cmap revision %d, xmap revision %d, vc2 revision %d), depth %d\n",
627 	    sc->sc_dc->dc_boardrev, sc->sc_dc->dc_cmaprev,
628 	    sc->sc_dc->dc_xmaprev, sc->sc_dc->dc_vc2rev, sc->sc_dc->dc_depth);
629 	vcons_init(&sc->sc_dc->dc_vd, sc->sc_dc, sc->sc_dc->dc_screen,
630 	    &newport_accessops);
631 	sc->sc_dc->dc_vd.init_screen = newport_init_screen;
632 	if (newport_is_console) {
633 		newport_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
634 		vcons_init_screen(&sc->sc_dc->dc_vd, &newport_console_screen,
635 		    1, &defattr);
636 		sc->sc_dc->dc_screen->textops =
637 		    &newport_console_screen.scr_ri.ri_ops;
638 		vcons_replay_msgbuf(&newport_console_screen);
639 	}
640 	wa.scrdata = &newport_screenlist;
641 	wa.accessops = &newport_accessops;
642 	wa.accesscookie = &sc->sc_dc->dc_vd;
643 
644 	config_found(sc->sc_dev, &wa, wsemuldisplaydevprint);
645 }
646 
647 int
648 newport_cnattach(struct gio_attach_args *ga)
649 {
650 	struct rasops_info *ri = &newport_console_screen.scr_ri;
651 	long defattr = 0x000f0000;
652 
653 	if (!newport_match(NULL, NULL, ga)) {
654 		return ENXIO;
655 	}
656 
657 	newport_attach_common(&newport_console_dc, ga);
658 
659 	ri->ri_hw = &newport_console_screen;
660 	ri->ri_depth = newport_console_dc.dc_depth;
661 	ri->ri_width = newport_console_dc.dc_xres;
662 	ri->ri_height = newport_console_dc.dc_yres;
663 	ri->ri_stride = newport_console_dc.dc_xres; /* XXX */
664 	ri->ri_flg = RI_CENTER | RI_NO_AUTO | RI_FULLCLEAR | RI_8BIT_IS_RGB;
665 	rasops_init(ri, 0, 0);
666 	ri->ri_caps = WSSCREEN_WSCOLORS;
667 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
668 	    ri->ri_width / ri->ri_font->fontwidth);
669 	ri->ri_ops.copyrows  = newport_copyrows;
670 	ri->ri_ops.eraserows = newport_eraserows;
671 	ri->ri_ops.copycols  = newport_copycols;
672 	ri->ri_ops.erasecols = newport_erasecols;
673 	ri->ri_ops.cursor    = newport_cursor_dummy;
674 	ri->ri_ops.putchar   = newport_putchar;
675 	newport_screen.textops = &ri->ri_ops;
676 	newport_screen.ncols = ri->ri_cols;
677 	newport_screen.nrows = ri->ri_rows;
678 	newport_console_screen.scr_cookie = &newport_console_dc;
679 
680 	wsdisplay_cnattach(&newport_screen, ri, 0, 0, defattr);
681 	newport_is_console = 1;
682 
683 	return 0;
684 }
685 
686 static void
687 newport_init_screen(void *cookie, struct vcons_screen *scr,
688     int existing, long *defattr)
689 {
690 	struct newport_devconfig *dc = cookie;
691 	struct rasops_info *ri = &scr->scr_ri;
692 
693 	ri->ri_depth = 8;
694 	ri->ri_width = dc->dc_xres;
695 	ri->ri_height = dc->dc_yres;
696 	ri->ri_stride = dc->dc_xres; /* XXX */
697 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_8BIT_IS_RGB;
698 
699 	rasops_init(ri, 0, 0);
700 	ri->ri_caps = WSSCREEN_WSCOLORS;
701 
702 	rasops_reconfig(ri, dc->dc_yres / ri->ri_font->fontheight,
703 		    dc->dc_xres / ri->ri_font->fontwidth);
704 
705 	ri->ri_hw = scr;
706 	ri->ri_ops.copyrows  = newport_copyrows;
707 	ri->ri_ops.eraserows = newport_eraserows;
708 	ri->ri_ops.copycols  = newport_copycols;
709 	ri->ri_ops.erasecols = newport_erasecols;
710 	ri->ri_ops.cursor    = newport_cursor;
711 	ri->ri_ops.putchar   = newport_putchar;
712 }
713 
714 /**** wsdisplay textops ****/
715 static void
716 newport_cursor_dummy(void *c, int on, int row, int col)
717 {
718 }
719 
720 static void
721 newport_cursor(void *c, int on, int row, int col)
722 {
723 	struct rasops_info *ri = c;
724 	struct vcons_screen *scr = ri->ri_hw;
725 	struct newport_devconfig *dc = scr->scr_cookie;
726 	int x, y, wi,he;
727 
728 	wi = ri->ri_font->fontwidth;
729 	he = ri->ri_font->fontheight;
730 
731 	if (ri->ri_flg & RI_CURSOR) {
732 		x = ri->ri_ccol * wi + ri->ri_xorigin;
733 		y = ri->ri_crow * he + ri->ri_yorigin;
734 		newport_bitblt(dc, x, y, x, y, wi, he, 12);
735 		ri->ri_flg &= ~RI_CURSOR;
736 	}
737 
738 	ri->ri_crow = row;
739 	ri->ri_ccol = col;
740 
741 	if (on)
742 	{
743 		x = ri->ri_ccol * wi + ri->ri_xorigin;
744 		y = ri->ri_crow * he + ri->ri_yorigin;
745 		newport_bitblt(dc, x, y, x, y, wi, he, 12);
746 		ri->ri_flg |= RI_CURSOR;
747 	}
748 }
749 
750 static void
751 newport_putchar(void *c, int row, int col, u_int ch, long attr)
752 {
753 	struct rasops_info *ri = c;
754 	struct vcons_screen *scr = ri->ri_hw;
755 	struct newport_devconfig *dc = scr->scr_cookie;
756 	struct wsdisplay_font *font = PICK_FONT(ri, ch);
757 	uint8_t *bitmap = (u_int8_t *)font->data + (ch - font->firstchar) *
758 	    font->fontheight * font->stride;
759 	uint32_t pattern;
760 	int i;
761 	int x = col * font->fontwidth + ri->ri_xorigin;
762 	int y = row * font->fontheight + ri->ri_yorigin;
763 
764 	rex3_wait_gfifo(dc);
765 
766 	rex3_write(dc, REX3_REG_DRAWMODE0, REX3_DRAWMODE0_OPCODE_DRAW |
767 	    REX3_DRAWMODE0_ADRMODE_BLOCK | REX3_DRAWMODE0_STOPONX |
768 	    REX3_DRAWMODE0_ENZPATTERN | REX3_DRAWMODE0_ZPOPAQUE);
769 
770 	rex3_write(dc, REX3_REG_DRAWMODE1,
771 	    REX3_DRAWMODE1_PLANES_CI |
772 	    REX3_DRAWMODE1_DD_DD8 |
773 	    REX3_DRAWMODE1_RWPACKED |
774 	    REX3_DRAWMODE1_HD_HD8 |
775 	    REX3_DRAWMODE1_COMPARE_LT |
776 	    REX3_DRAWMODE1_COMPARE_EQ |
777 	    REX3_DRAWMODE1_COMPARE_GT |
778 	    /*REX3_DRAWMODE1_RGBMODE |*/
779 	    REX3_DRAWMODE1_LO_SRC);
780 
781 	rex3_write(dc, REX3_REG_XYSTARTI, (x << REX3_XYSTARTI_XSHIFT) | y);
782 	rex3_write(dc, REX3_REG_XYENDI,
783 	    (x + font->fontwidth - 1) << REX3_XYENDI_XSHIFT);
784 
785 	rex3_write(dc, REX3_REG_COLORI, ri->ri_devcmap[(attr >> 24) & 0xf] & 0xff);
786 	rex3_write(dc, REX3_REG_COLORBACK, ri->ri_devcmap[(attr >> 16) & 0xf] & 0xff);
787 
788 	rex3_write(dc, REX3_REG_WRMASK, 0xff);
789 
790 	if (font->stride == 1) {
791 		for (i = 0; i < font->fontheight; i++) {
792 			pattern = *bitmap << 24;
793 
794 			rex3_write_go(dc, REX3_REG_ZPATTERN, pattern);
795 
796 			bitmap++;
797 		}
798 	} else {
799 		uint16_t *bitmap16 = (uint16_t *)bitmap;
800 		for (i = 0; i < font->fontheight; i++) {
801 			pattern = *bitmap16 << 16;
802 
803 			rex3_write_go(dc, REX3_REG_ZPATTERN, pattern);
804 
805 			bitmap16++;
806 		}
807 
808 	}
809 	rex3_wait_gfifo(dc);
810 }
811 
812 static void
813 newport_copycols(void *c, int row, int srccol, int dstcol, int ncols)
814 {
815 	struct rasops_info *ri = c;
816 	struct vcons_screen *scr = ri->ri_hw;
817 	struct newport_devconfig *dc = scr->scr_cookie;
818 	int32_t xs, xd, y, width, height;
819 
820 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
821 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
822 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
823 	width = ri->ri_font->fontwidth * ncols;
824 	height = ri->ri_font->fontheight;
825 	newport_bitblt(dc, xs, y, xd, y, width, height, 3);
826 }
827 
828 static void
829 newport_erasecols(void *c, int row, int startcol, int ncols,
830     long attr)
831 {
832 	struct rasops_info *ri = c;
833 	struct vcons_screen *scr = ri->ri_hw;
834 	struct newport_devconfig *dc = scr->scr_cookie;
835 	struct wsdisplay_font *font = ri->ri_font;
836 
837 	newport_fill_rectangle(dc,
838 	    startcol * font->fontwidth + ri->ri_xorigin,	/* x1 */
839 	    row * font->fontheight + ri->ri_yorigin,		/* y1 */
840 	    ncols * font->fontwidth,				/* wi */
841 	    font->fontheight,					/* he */
842 	    ri->ri_devcmap[(attr >> 16) & 0xf]);
843 }
844 
845 static void
846 newport_copyrows(void *c, int srcrow, int dstrow, int nrows)
847 {
848 	struct rasops_info *ri = c;
849 	struct vcons_screen *scr = ri->ri_hw;
850 	struct newport_devconfig *dc = scr->scr_cookie;
851 	int32_t x, ys, yd, width, height;
852 
853 	x = ri->ri_xorigin;
854 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
855 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
856 	width = ri->ri_emuwidth;
857 	height = ri->ri_font->fontheight * nrows;
858 
859 	newport_bitblt(dc, x, ys, x, yd, width, height, 3);
860 }
861 
862 static void
863 newport_eraserows(void *c, int startrow, int nrows, long attr)
864 {
865 	struct rasops_info *ri = c;
866 	struct vcons_screen *scr = ri->ri_hw;
867 	struct newport_devconfig *dc = scr->scr_cookie;
868 	struct wsdisplay_font *font = ri->ri_font;
869 
870 	if (startrow == 0 && nrows == ri->ri_rows) {
871 		newport_fill_rectangle(dc,
872 		    0,							/* x1 */
873 		    0,							/* y1 */
874 		    dc->dc_xres,					/* wi */
875 		    dc->dc_yres,					/* he */
876 		    ri->ri_devcmap[(attr >> 16) & 0xf]);
877 	} else {
878 		newport_fill_rectangle(dc,
879 		    0,							/* x1 */
880 		    startrow * font->fontheight + ri->ri_yorigin,	/* y1 */
881 		    dc->dc_xres,					/* wi */
882 		    nrows * font->fontheight,				/* he */
883 		    ri->ri_devcmap[(attr >> 16) & 0xf]);
884 	}
885 }
886 
887 /**** wsdisplay accessops ****/
888 
889 static int
890 newport_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
891 	struct lwp *l)
892 {
893 	struct vcons_data *vd;
894 	struct newport_devconfig *dc;
895 	struct vcons_screen *__unused(ms);
896 	int nmode;
897 
898 	vd = (struct vcons_data *)v;
899 	dc = (struct newport_devconfig *)vd->cookie;
900 	ms = (struct vcons_screen *)vd->active;
901 
902 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
903 
904 	switch (cmd) {
905 	case WSDISPLAYIO_GINFO:
906 		FBINFO.width  = dc->dc_xres;
907 		FBINFO.height = dc->dc_yres;
908 		FBINFO.depth  = dc->dc_depth;
909 		FBINFO.cmsize = 256;
910 		return 0;
911 	case WSDISPLAYIO_GTYPE:
912 		*(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
913 		return 0;
914 	case WSDISPLAYIO_SMODE:
915 		nmode = *(int *)data;
916 		if (nmode != dc->dc_mode) {
917 			dc->dc_mode = nmode;
918 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
919 				rex3_wait_gfifo(dc);
920 				newport_setup_hw(dc, 8);
921 				vcons_redraw_screen(vd->active);
922 			} else {
923 				rex3_wait_gfifo(dc);
924 				newport_setup_hw(dc, dc->dc_depth);
925 			}
926 		}
927 		return 0;
928 	}
929 	return EPASSTHROUGH;
930 }
931 
932 static paddr_t
933 newport_mmap(void *v, void *vs, off_t offset, int prot)
934 {
935 	struct vcons_data *vd;
936 	struct newport_devconfig *dc;
937 
938 	vd = (struct vcons_data *)v;
939 	dc = (struct newport_devconfig *)vd->cookie;
940 
941 	if ( offset >= 0xfffff)
942 		return -1;
943 
944 	return bus_space_mmap(dc->dc_st, dc->dc_addr, offset, prot, 0);
945 }
946