Home | History | Annotate | Line # | Download | only in gio
newport.c revision 1.14
      1 /*	$NetBSD: newport.c,v 1.14 2009/03/04 01:23:28 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Ilpo Ruotsalainen
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: newport.c,v 1.14 2009/03/04 01:23:28 macallan Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 
     40 #include <machine/sysconf.h>
     41 
     42 #include <dev/wscons/wsconsio.h>
     43 #include <dev/wscons/wsdisplayvar.h>
     44 #include <dev/wsfont/wsfont.h>
     45 #include <dev/rasops/rasops.h>
     46 #include <dev/wscons/wsdisplay_vconsvar.h>
     47 
     48 #include <sgimips/gio/giovar.h>
     49 #include <sgimips/gio/newportvar.h>
     50 #include <sgimips/gio/newportreg.h>
     51 
     52 struct newport_softc {
     53 	device_t sc_dev;
     54 
     55 	struct newport_devconfig *sc_dc;
     56 
     57 };
     58 
     59 struct newport_devconfig {
     60 	uint32_t		dc_addr;
     61 
     62 	bus_space_tag_t		dc_st;
     63 	bus_space_handle_t	dc_sh;
     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) & REX3_STATUS_GFXBUSY)
    175 		;
    176 }
    177 
    178 static void
    179 vc2_write_ireg(struct newport_devconfig *dc, uint8_t ireg, uint16_t val)
    180 {
    181 	rex3_write(dc, REX3_REG_DCBMODE,
    182 	    REX3_DCBMODE_DW_3 |
    183 	    REX3_DCBMODE_ENCRSINC |
    184 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
    185 	    (VC2_DCBCRS_INDEX << REX3_DCBMODE_DCBCRS_SHIFT) |
    186 	    REX3_DCBMODE_ENASYNCACK |
    187 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    188 
    189 	rex3_write(dc, REX3_REG_DCBDATA0, (ireg << 24) | (val << 8));
    190 }
    191 
    192 static uint16_t
    193 vc2_read_ireg(struct newport_devconfig *dc, uint8_t ireg)
    194 {
    195 	rex3_write(dc, REX3_REG_DCBMODE,
    196 	    REX3_DCBMODE_DW_1 |
    197 	    REX3_DCBMODE_ENCRSINC |
    198 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
    199 	    (VC2_DCBCRS_INDEX << REX3_DCBMODE_DCBCRS_SHIFT) |
    200 	    REX3_DCBMODE_ENASYNCACK |
    201 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    202 
    203 	rex3_write(dc, REX3_REG_DCBDATA0, ireg << 24);
    204 
    205 	rex3_write(dc, REX3_REG_DCBMODE,
    206 	    REX3_DCBMODE_DW_2 |
    207 	    REX3_DCBMODE_ENCRSINC |
    208 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
    209 	    (VC2_DCBCRS_IREG << REX3_DCBMODE_DCBCRS_SHIFT) |
    210 	    REX3_DCBMODE_ENASYNCACK |
    211 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    212 
    213 	return (uint16_t)(rex3_read(dc, REX3_REG_DCBDATA0) >> 16);
    214 }
    215 
    216 static uint16_t
    217 vc2_read_ram(struct newport_devconfig *dc, uint16_t addr)
    218 {
    219 	vc2_write_ireg(dc, VC2_IREG_RAM_ADDRESS, addr);
    220 
    221 	rex3_write(dc, REX3_REG_DCBMODE,
    222 	    REX3_DCBMODE_DW_2 |
    223 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
    224 	    (VC2_DCBCRS_RAM << REX3_DCBMODE_DCBCRS_SHIFT) |
    225 	    REX3_DCBMODE_ENASYNCACK |
    226 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    227 
    228 	return (uint16_t)(rex3_read(dc, REX3_REG_DCBDATA0) >> 16);
    229 }
    230 
    231 #if 0
    232 static void
    233 vc2_write_ram(struct newport_devconfig *dc, uint16_t addr, uint16_t val)
    234 {
    235 	vc2_write_ireg(dc, VC2_IREG_RAM_ADDRESS, addr);
    236 
    237 	rex3_write(dc, REX3_REG_DCBMODE,
    238 	    REX3_DCBMODE_DW_2 |
    239 	    (NEWPORT_DCBADDR_VC2 << REX3_DCBMODE_DCBADDR_SHIFT) |
    240 	    (VC2_DCBCRS_RAM << REX3_DCBMODE_DCBCRS_SHIFT) |
    241 	    REX3_DCBMODE_ENASYNCACK |
    242 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    243 
    244 	rex3_write(dc, REX3_REG_DCBDATA0, val << 16);
    245 }
    246 #endif
    247 
    248 static u_int32_t
    249 xmap9_read(struct newport_devconfig *dc, int crs)
    250 {
    251 	rex3_write(dc, REX3_REG_DCBMODE,
    252 		REX3_DCBMODE_DW_1 |
    253 		(NEWPORT_DCBADDR_XMAP_0 << REX3_DCBMODE_DCBADDR_SHIFT) |
    254 		(crs << REX3_DCBMODE_DCBCRS_SHIFT) |
    255 		(3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    256 		(2 << REX3_DCBMODE_CSHOLD_SHIFT) |
    257 		(1 << REX3_DCBMODE_CSSETUP_SHIFT));
    258 	return rex3_read(dc, REX3_REG_DCBDATA0);
    259 }
    260 
    261 static void
    262 xmap9_write(struct newport_devconfig *dc, int crs, uint8_t val)
    263 {
    264 	rex3_write(dc, REX3_REG_DCBMODE,
    265 	    REX3_DCBMODE_DW_1 |
    266 	    (NEWPORT_DCBADDR_XMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
    267 	    (crs << REX3_DCBMODE_DCBCRS_SHIFT) |
    268 	    (3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    269 	    (2 << REX3_DCBMODE_CSHOLD_SHIFT) |
    270 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    271 
    272 	rex3_write(dc, REX3_REG_DCBDATA0, val << 24);
    273 }
    274 
    275 static void
    276 xmap9_write_mode(struct newport_devconfig *dc, uint8_t index, uint32_t mode)
    277 {
    278 	rex3_write(dc, REX3_REG_DCBMODE,
    279 	    REX3_DCBMODE_DW_4 |
    280 	    (NEWPORT_DCBADDR_XMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
    281 	    (XMAP9_DCBCRS_MODE_SETUP << REX3_DCBMODE_DCBCRS_SHIFT) |
    282 	    (3 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    283 	    (2 << REX3_DCBMODE_CSHOLD_SHIFT) |
    284 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    285 
    286 	rex3_write(dc, REX3_REG_DCBDATA0, (index << 24) | mode);
    287 }
    288 
    289 /**** Helper functions ****/
    290 static void
    291 newport_fill_rectangle(struct newport_devconfig *dc, int x1, int y1, int x2,
    292     int y2, uint8_t color)
    293 {
    294 	rex3_wait_gfifo(dc);
    295 
    296 	rex3_write(dc, REX3_REG_DRAWMODE0, REX3_DRAWMODE0_OPCODE_DRAW |
    297 	    REX3_DRAWMODE0_ADRMODE_BLOCK | REX3_DRAWMODE0_DOSETUP |
    298 	    REX3_DRAWMODE0_STOPONX | REX3_DRAWMODE0_STOPONY);
    299 	rex3_write(dc, REX3_REG_DRAWMODE1,
    300 	    REX3_DRAWMODE1_PLANES_CI |
    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_LO_SRC);
    308 	rex3_write(dc, REX3_REG_WRMASK, 0xffffffff);
    309 	rex3_write(dc, REX3_REG_COLORI, color);
    310 	rex3_write(dc, REX3_REG_XYSTARTI, (x1 << REX3_XYSTARTI_XSHIFT) | y1);
    311 
    312 	rex3_write_go(dc, REX3_REG_XYENDI, (x2 << REX3_XYENDI_XSHIFT) | y2);
    313 }
    314 
    315 static void
    316 newport_bitblt(struct newport_devconfig *dc, int xs, int ys, int xd,
    317     int yd, int wi, int he, int rop)
    318 {
    319 	int xe, ye;
    320 	uint32_t tmp;
    321 
    322 	rex3_wait_gfifo(dc);
    323 	if (yd > ys) {
    324 		/* need to copy bottom up */
    325 		ye = ys;
    326 		yd += he - 1;
    327 		ys += he - 1;
    328 	} else
    329 		ye = ys + he - 1;
    330 
    331 	if (xd > xs) {
    332 		/* need to copy right to left */
    333 		xe = xs;
    334 		xd += wi - 1;
    335 		xs += wi - 1;
    336 	} else
    337 		xe = xs + wi - 1;
    338 
    339 	rex3_write(dc, REX3_REG_DRAWMODE0, REX3_DRAWMODE0_OPCODE_SCR2SCR |
    340 	    REX3_DRAWMODE0_ADRMODE_BLOCK | REX3_DRAWMODE0_DOSETUP |
    341 	    REX3_DRAWMODE0_STOPONX | REX3_DRAWMODE0_STOPONY);
    342 	rex3_write(dc, REX3_REG_DRAWMODE1,
    343 	    REX3_DRAWMODE1_PLANES_CI |
    344 	    REX3_DRAWMODE1_DD_DD8 |
    345 	    REX3_DRAWMODE1_RWPACKED |
    346 	    REX3_DRAWMODE1_HD_HD8 |
    347 	    REX3_DRAWMODE1_COMPARE_LT |
    348 	    REX3_DRAWMODE1_COMPARE_EQ |
    349 	    REX3_DRAWMODE1_COMPARE_GT |
    350 	    ((rop << 28) & REX3_DRAWMODE1_LOGICOP_MASK));
    351 	rex3_write(dc, REX3_REG_XYSTARTI, (xs << REX3_XYSTARTI_XSHIFT) | ys);
    352 	rex3_write(dc, REX3_REG_XYENDI, (xe << REX3_XYENDI_XSHIFT) | ye);
    353 
    354 	tmp = (yd - ys) & 0xffff;
    355 	tmp |= (xd - xs) << REX3_XYMOVE_XSHIFT;
    356 
    357 	rex3_write_go(dc, REX3_REG_XYMOVE, tmp);
    358 }
    359 
    360 static void
    361 newport_cmap_setrgb(struct newport_devconfig *dc, int index, uint8_t r,
    362     uint8_t g, uint8_t b)
    363 {
    364 	rex3_write(dc, REX3_REG_DCBMODE,
    365 	    REX3_DCBMODE_DW_2 |
    366 	    REX3_DCBMODE_ENCRSINC |
    367 	    (NEWPORT_DCBADDR_CMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
    368 	    (CMAP_DCBCRS_ADDRESS_LOW << REX3_DCBMODE_DCBCRS_SHIFT) |
    369 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    370 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
    371 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT) |
    372 	    REX3_DCBMODE_SWAPENDIAN);
    373 
    374 	rex3_write(dc, REX3_REG_DCBDATA0, index << 16);
    375 
    376 	rex3_write(dc, REX3_REG_DCBMODE,
    377 	    REX3_DCBMODE_DW_3 |
    378 	    (NEWPORT_DCBADDR_CMAP_BOTH << REX3_DCBMODE_DCBADDR_SHIFT) |
    379 	    (CMAP_DCBCRS_PALETTE << REX3_DCBMODE_DCBCRS_SHIFT) |
    380 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    381 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
    382 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    383 
    384 	rex3_write(dc, REX3_REG_DCBDATA0, (r << 24) + (g << 16) + (b << 8));
    385 }
    386 
    387 static void
    388 newport_get_resolution(struct newport_devconfig *dc)
    389 {
    390 	uint16_t vep,lines;
    391 	uint16_t linep,cols;
    392 	uint16_t data;
    393 
    394 	vep = vc2_read_ireg(dc, VC2_IREG_VIDEO_ENTRY);
    395 
    396 	dc->dc_xres = 0;
    397 	dc->dc_yres = 0;
    398 
    399 	for (;;) {
    400 		/* Iterate over runs in video timing table */
    401 
    402 		cols = 0;
    403 
    404 		linep = vc2_read_ram(dc, vep++);
    405 		lines = vc2_read_ram(dc, vep++);
    406 
    407 		if (lines == 0)
    408 			break;
    409 
    410 		do {
    411 			/* Iterate over state runs in line sequence table */
    412 
    413 			data = vc2_read_ram(dc, linep++);
    414 
    415 			if ((data & 0x0001) == 0)
    416 				cols += (data >> 7) & 0xfe;
    417 
    418 			if ((data & 0x0080) == 0)
    419 				data = vc2_read_ram(dc, linep++);
    420 		} while ((data & 0x8000) == 0);
    421 
    422 		if (cols != 0) {
    423 			if (cols > dc->dc_xres)
    424 				dc->dc_xres = cols;
    425 
    426 			dc->dc_yres += lines;
    427 		}
    428 	}
    429 }
    430 
    431 static void
    432 newport_setup_hw(struct newport_devconfig *dc)
    433 {
    434 	uint16_t curp,tmp;
    435 	int i;
    436 	uint32_t scratch;
    437 
    438 	/* Get various revisions */
    439 	rex3_write(dc, REX3_REG_DCBMODE,
    440 	    REX3_DCBMODE_DW_1 |
    441 	    (NEWPORT_DCBADDR_CMAP_0 << REX3_DCBMODE_DCBADDR_SHIFT) |
    442 	    (CMAP_DCBCRS_REVISION << REX3_DCBMODE_DCBCRS_SHIFT) |
    443 	    (1 << REX3_DCBMODE_CSWIDTH_SHIFT) |
    444 	    (1 << REX3_DCBMODE_CSHOLD_SHIFT) |
    445 	    (1 << REX3_DCBMODE_CSSETUP_SHIFT));
    446 
    447 	scratch = vc2_read_ireg(dc, VC2_IREG_CONFIG);
    448 	dc->dc_vc2rev = (scratch & VC2_IREG_CONFIG_REVISION) >> 5;
    449 
    450 	scratch = rex3_read(dc, REX3_REG_DCBDATA0);
    451 
    452 	dc->dc_boardrev = (scratch >> 28) & 0x07;
    453 	dc->dc_cmaprev = scratch & 0x07;
    454 	dc->dc_xmaprev = xmap9_read(dc, XMAP9_DCBCRS_REVISION) & 0x07;
    455 	dc->dc_depth = ( (dc->dc_boardrev > 1) && (scratch & 0x80)) ? 8 : 24;
    456 
    457 	/* Setup cursor glyph */
    458 	curp = vc2_read_ireg(dc, VC2_IREG_CURSOR_ENTRY);
    459 
    460 	/* Setup VC2 to a known state */
    461 	tmp = vc2_read_ireg(dc, VC2_IREG_CONTROL) & VC2_CONTROL_INTERLACE;
    462 	vc2_write_ireg(dc, VC2_IREG_CONTROL, tmp |
    463 	    VC2_CONTROL_DISPLAY_ENABLE |
    464 	    VC2_CONTROL_VTIMING_ENABLE |
    465 	    VC2_CONTROL_DID_ENABLE |
    466 	    VC2_CONTROL_CURSORFUNC_ENABLE /*|
    467 	    VC2_CONTROL_CURSOR_ENABLE*/);
    468 
    469 	/* Setup XMAP9s */
    470 	xmap9_write(dc, XMAP9_DCBCRS_CONFIG,
    471 	    XMAP9_CONFIG_8BIT_SYSTEM | XMAP9_CONFIG_RGBMAP_CI);
    472 
    473 	xmap9_write(dc, XMAP9_DCBCRS_CURSOR_CMAP, 0);
    474 
    475 	xmap9_write_mode(dc, 0,
    476 	    XMAP9_MODE_GAMMA_BYPASS |
    477 	    XMAP9_MODE_PIXSIZE_8BPP);
    478 	xmap9_write(dc, XMAP9_DCBCRS_MODE_SELECT, 0);
    479 
    480 	/* Setup REX3 */
    481 	rex3_write(dc, REX3_REG_XYWIN, (4096 << 16) | 4096);
    482 	rex3_write(dc, REX3_REG_TOPSCAN, 0x3ff); /* XXX Why? XXX */
    483 
    484 	/* Setup CMAP */
    485 	for (i = 0; i < 256; i++)
    486 		newport_cmap_setrgb(dc, i, rasops_cmap[i * 3],
    487 		    rasops_cmap[i * 3 + 1], rasops_cmap[i * 3 + 2]);
    488 }
    489 
    490 /**** Attach routines ****/
    491 static int
    492 newport_match(device_t parent, struct cfdata *self, void *aux)
    493 {
    494 	struct gio_attach_args *ga = aux;
    495 
    496 	/* newport doesn't decode all addresses */
    497 	if (ga->ga_addr != 0x1f000000 && ga->ga_addr != 0x1f400000 &&
    498 	    ga->ga_addr != 0x1f800000 && ga->ga_addr != 0x1fc00000)
    499 		return 0;
    500 
    501 	/* Don't do the destructive probe if we're already attached */
    502 	if (newport_is_console && ga->ga_addr == newport_console_dc.dc_addr)
    503 		return 1;
    504 
    505 	if (platform.badaddr(
    506 	    (void *)(ga->ga_ioh + NEWPORT_REX3_OFFSET + REX3_REG_XSTARTI),
    507 	    sizeof(uint32_t)))
    508 		return 0;
    509 	if (platform.badaddr(
    510 	    (void *)(ga->ga_ioh + NEWPORT_REX3_OFFSET + REX3_REG_XSTART),
    511 	    sizeof(uint32_t)))
    512 		return 0;
    513 
    514 	/* Ugly, this probe is destructive, blame SGI... */
    515 	/* XXX Should be bus_space_peek/bus_space_poke XXX */
    516 	bus_space_write_4(ga->ga_iot, ga->ga_ioh,
    517 	    NEWPORT_REX3_OFFSET + REX3_REG_XSTARTI, 0x12345678);
    518 	if (bus_space_read_4(ga->ga_iot, ga->ga_ioh,
    519 	      NEWPORT_REX3_OFFSET + REX3_REG_XSTART)
    520 	    != ((0x12345678 & 0xffff) << 11))
    521 		return 0;
    522 
    523 	return 1;
    524 }
    525 
    526 static void
    527 newport_attach_common(struct newport_devconfig *dc, struct gio_attach_args *ga)
    528 {
    529 	dc->dc_addr = ga->ga_addr;
    530 
    531 	dc->dc_st = ga->ga_iot;
    532 	dc->dc_sh = ga->ga_ioh;
    533 
    534 	wsfont_init();
    535 
    536 	dc->dc_font = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
    537 	    WSDISPLAY_FONTORDER_L2R);
    538 	if (dc->dc_font < 0)
    539 		panic("newport_attach_common: no suitable fonts");
    540 
    541 	if (wsfont_lock(dc->dc_font, &dc->dc_fontdata))
    542 		panic("newport_attach_common: unable to lock font data");
    543 
    544 	newport_setup_hw(dc);
    545 
    546 	newport_get_resolution(dc);
    547 
    548 	newport_fill_rectangle(dc, 0, 0, dc->dc_xres, dc->dc_yres, 0);
    549 	dc->dc_screen = &newport_screen;
    550 
    551 	dc->dc_mode = WSDISPLAYIO_MODE_EMUL;
    552 }
    553 
    554 static void
    555 newport_attach(device_t parent, device_t self, void *aux)
    556 {
    557 	struct gio_attach_args *ga = aux;
    558 	struct newport_softc *sc = device_private(self);
    559 	struct wsemuldisplaydev_attach_args wa;
    560 	unsigned long defattr;
    561 
    562 	sc->sc_dev = self;
    563 	if (newport_is_console && ga->ga_addr == newport_console_dc.dc_addr) {
    564 		wa.console = 1;
    565 		sc->sc_dc = &newport_console_dc;
    566 	} else {
    567 		wa.console = 0;
    568 		sc->sc_dc = malloc(sizeof(struct newport_devconfig),
    569 		    M_DEVBUF, M_WAITOK | M_ZERO);
    570 		if (sc->sc_dc == NULL)
    571 			panic("newport_attach: out of memory");
    572 
    573 		newport_attach_common(sc->sc_dc, ga);
    574 	}
    575 
    576 	aprint_naive(": Display adapter\n");
    577 
    578 	aprint_normal(": SGI NG1 (board revision %d, cmap revision %d, xmap revision %d, vc2 revision %d), depth %d\n",
    579 	    sc->sc_dc->dc_boardrev, sc->sc_dc->dc_cmaprev,
    580 	    sc->sc_dc->dc_xmaprev, sc->sc_dc->dc_vc2rev, sc->sc_dc->dc_depth);
    581 	vcons_init(&sc->sc_dc->dc_vd, sc->sc_dc, sc->sc_dc->dc_screen,
    582 	    &newport_accessops);
    583 	sc->sc_dc->dc_vd.init_screen = newport_init_screen;
    584 	if (newport_is_console) {
    585 		newport_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    586 		vcons_init_screen(&sc->sc_dc->dc_vd, &newport_console_screen,
    587 		    1, &defattr);
    588 		sc->sc_dc->dc_screen->textops =
    589 		    &newport_console_screen.scr_ri.ri_ops;
    590 		memcpy(&newport_textops, &newport_console_screen.scr_ri.ri_ops,
    591 		    sizeof(struct wsdisplay_emulops));
    592 	}
    593 	wa.scrdata = &newport_screenlist;
    594 	wa.accessops = &newport_accessops;
    595 	wa.accesscookie = &sc->sc_dc->dc_vd;
    596 
    597 	config_found(sc->sc_dev, &wa, wsemuldisplaydevprint);
    598 }
    599 
    600 int
    601 newport_cnattach(struct gio_attach_args *ga)
    602 {
    603 	struct rasops_info *ri = &newport_console_screen.scr_ri;
    604 	long defattr = NEWPORT_ATTR_ENCODE(WSCOL_WHITE, WSCOL_BLACK);
    605 
    606 	if (!newport_match(NULL, NULL, ga)) {
    607 		return ENXIO;
    608 	}
    609 
    610 	newport_attach_common(&newport_console_dc, ga);
    611 
    612 	newport_screen.ncols = newport_console_dc.dc_xres / 8;
    613 	newport_screen.nrows = newport_console_dc.dc_yres / 16;
    614 
    615 	ri->ri_hw = &newport_console_screen;
    616 	ri->ri_depth = newport_console_dc.dc_depth;
    617 	ri->ri_width = newport_console_dc.dc_xres;
    618 	ri->ri_height = newport_console_dc.dc_yres;
    619 	ri->ri_stride = newport_console_dc.dc_xres; /* XXX */
    620 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    621 	ri->ri_ops.copyrows  = newport_copyrows;
    622 	ri->ri_ops.eraserows = newport_eraserows;
    623 	ri->ri_ops.copycols  = newport_copycols;
    624 	ri->ri_ops.erasecols = newport_erasecols;
    625 	ri->ri_ops.cursor    = newport_cursor_dummy;
    626 	ri->ri_ops.mapchar   = newport_mapchar;
    627 	ri->ri_ops.putchar   = newport_putchar;
    628 	ri->ri_ops.allocattr = newport_allocattr;
    629 	ri->ri_font = newport_console_dc.dc_fontdata;
    630 	newport_console_screen.scr_cookie = &newport_console_dc;
    631 
    632 	newport_putchar(ri, 3, 3, 0x41, defattr);
    633 	wsdisplay_cnattach(&newport_screen, ri, 0, 0, defattr);
    634 	newport_putchar(ri, 4, 3, 0x42, 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, dc->dc_yres / 8, dc->dc_xres / 8);
    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 }
    783 
    784 static void
    785 newport_copycols(void *c, int row, int srccol, int dstcol, int ncols)
    786 {
    787 	struct rasops_info *ri = c;
    788 	struct vcons_screen *scr = ri->ri_hw;
    789 	struct newport_devconfig *dc = scr->scr_cookie;
    790 	int32_t xs, xd, y, width, height;
    791 
    792 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    793 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    794 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    795 	width = ri->ri_font->fontwidth * ncols;
    796 	height = ri->ri_font->fontheight;
    797 	newport_bitblt(dc, xs, y, xd, y, width, height, 3);
    798 }
    799 
    800 static void
    801 newport_erasecols(void *c, int row, int startcol, int ncols,
    802     long attr)
    803 {
    804 	struct rasops_info *ri = c;
    805 	struct vcons_screen *scr = ri->ri_hw;
    806 	struct newport_devconfig *dc = scr->scr_cookie;
    807 	struct wsdisplay_font *font = dc->dc_fontdata;
    808 
    809 	newport_fill_rectangle(dc,
    810 	    startcol * font->fontwidth,				/* x1 */
    811 	    row * font->fontheight,				/* y1 */
    812 	    (startcol + ncols) * font->fontwidth - 1,		/* x2 */
    813 	    (row + 1) * font->fontheight - 1,			/* y2 */
    814 	    NEWPORT_ATTR_BG(attr));
    815 }
    816 
    817 static void
    818 newport_copyrows(void *c, int srcrow, int dstrow, int nrows)
    819 {
    820 	struct rasops_info *ri = c;
    821 	struct vcons_screen *scr = ri->ri_hw;
    822 	struct newport_devconfig *dc = scr->scr_cookie;
    823 	int32_t x, ys, yd, width, height;
    824 
    825 	x = ri->ri_xorigin;
    826 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    827 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    828 	width = ri->ri_emuwidth;
    829 	height = ri->ri_font->fontheight * nrows;
    830 
    831 	newport_bitblt(dc, x, ys, x, yd, width, height, 3);
    832 }
    833 
    834 static void
    835 newport_eraserows(void *c, int startrow, int nrows, long attr)
    836 {
    837 	struct rasops_info *ri = c;
    838 	struct vcons_screen *scr = ri->ri_hw;
    839 	struct newport_devconfig *dc = scr->scr_cookie;
    840 	struct wsdisplay_font *font = dc->dc_fontdata;
    841 
    842 	newport_fill_rectangle(dc,
    843 	    0,							/* x1 */
    844 	    startrow * font->fontheight,			/* y1 */
    845 	    dc->dc_xres,					/* x2 */
    846 	    (startrow + nrows) * font->fontheight - 1,		/* y2 */
    847 	    NEWPORT_ATTR_BG(attr));
    848 }
    849 
    850 static int
    851 newport_allocattr(void *c, int fg, int bg, int flags, long *attr)
    852 {
    853 	if (flags & WSATTR_BLINK)
    854 		return EINVAL;
    855 
    856 	if ((flags & WSATTR_WSCOLORS) == 0) {
    857 		fg = WSCOL_WHITE;
    858 		bg = WSCOL_BLACK;
    859 	}
    860 
    861 	if (flags & WSATTR_HILIT)
    862 		fg += 8;
    863 
    864 	if (flags & WSATTR_REVERSE) {
    865 		int tmp = fg;
    866 		fg = bg;
    867 		bg = tmp;
    868 	}
    869 
    870 	*attr = NEWPORT_ATTR_ENCODE(fg, bg);
    871 
    872 	return 0;
    873 }
    874 
    875 /**** wsdisplay accessops ****/
    876 
    877 static int
    878 newport_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    879 	struct lwp *l)
    880 {
    881 	struct vcons_data *vd;
    882 	struct newport_devconfig *dc;
    883 	struct vcons_screen *ms;
    884 	int nmode;
    885 
    886 	vd = (struct vcons_data *)v;
    887 	dc = (struct newport_devconfig *)vd->cookie;
    888 	ms = (struct vcons_screen *)vd->active;
    889 
    890 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
    891 
    892 	switch (cmd) {
    893 	case WSDISPLAYIO_GINFO:
    894 		FBINFO.width  = dc->dc_xres;
    895 		FBINFO.height = dc->dc_yres;
    896 		FBINFO.depth  = dc->dc_depth;
    897 		FBINFO.cmsize = 1 << FBINFO.depth;
    898 		return 0;
    899 	case WSDISPLAYIO_GTYPE:
    900 		*(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
    901 		return 0;
    902 	case WSDISPLAYIO_SMODE:
    903 		nmode = *(int *)data;
    904 		if (nmode != dc->dc_mode) {
    905 			dc->dc_mode = nmode;
    906 			if (nmode == WSDISPLAYIO_MODE_EMUL) {
    907 				rex3_wait_gfifo(dc);
    908 				newport_setup_hw(dc);
    909 				vcons_redraw_screen(vd->active);
    910 			}
    911 		}
    912 		return 0;
    913 	}
    914 	return EPASSTHROUGH;
    915 }
    916 
    917 static paddr_t
    918 newport_mmap(void *v, void *vs, off_t offset, int prot)
    919 {
    920 	struct vcons_data *vd;
    921 	struct newport_devconfig *dc;
    922 
    923 	vd = (struct vcons_data *)v;
    924 	dc = (struct newport_devconfig *)vd->cookie;
    925 
    926 	if ( offset >= 0xfffff)
    927 		return -1;
    928 
    929 	return mips_btop(dc->dc_addr + offset);
    930 }
    931