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