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