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