Home | History | Annotate | Line # | Download | only in dev
sti_machdep.c revision 1.3
      1 /*	$NetBSD: sti_machdep.c,v 1.3 2025/05/19 17:34:03 tsutsui Exp $	*/
      2 /*	$OpenBSD: sti_sgc.c,v 1.14 2007/05/26 00:36:03 krw Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2005, Miodrag Vallat
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 /*-
     30  * Copyright (c) 2020, 2025 Izumi Tsutsui.  All rights reserved.
     31  *
     32  * Redistribution and use in source and binary forms, with or without
     33  * modification, are permitted provided that the following conditions
     34  * are met:
     35  * 1. Redistributions of source code must retain the above copyright
     36  *    notice, this list of conditions and the following disclaimer.
     37  * 2. Redistributions in binary form must reproduce the above copyright
     38  *    notice, this list of conditions and the following disclaimer in the
     39  *    documentation and/or other materials provided with the distribution.
     40  *
     41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     44  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     50  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     51  */
     52 
     53 #include <sys/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: sti_machdep.c,v 1.3 2025/05/19 17:34:03 tsutsui Exp $");
     55 
     56 #include <sys/param.h>
     57 #include <sys/device.h>
     58 #include <sys/bus.h>
     59 
     60 #include <dev/wscons/wsconsio.h>
     61 
     62 #include <hp300/dev/sti_machdep.h>
     63 
     64 /*
     65  * 425e EVRX specific hardware
     66  */
     67 /*
     68  * EVRX RAMDAC (Bt458) is found at offset 0x060000 from SGC bus PA and
     69  * offset 0x040000 length 0x1c0000 is mapped in MI sti via ROM region 2
     70  */
     71 #define STI_EVRX_REGNO2OFFSET	0x020000
     72 #define STI_EVRX_FBOFFSET	0x200000
     73 
     74 #define EVRX_BT458_ADDR		(STI_EVRX_REGNO2OFFSET + 0x200 + 2)
     75 #define EVRX_BT458_CMAP		(STI_EVRX_REGNO2OFFSET + 0x204 + 2)
     76 #define EVRX_BT458_CTRL		(STI_EVRX_REGNO2OFFSET + 0x208 + 2)
     77 #define EVRX_BT458_OMAP		(STI_EVRX_REGNO2OFFSET + 0x20C + 2)
     78 
     79 /* from HP-UX /usr/lib/libddevrx.a */
     80 #define EVRX_MAGIC00		(STI_EVRX_REGNO2OFFSET + 0x600)
     81 #define EVRX_MAGIC04		(STI_EVRX_REGNO2OFFSET + 0x604)
     82 #define EVRX_MAGIC08		(STI_EVRX_REGNO2OFFSET + 0x608)
     83 #define EVRX_MAGIC0C		(STI_EVRX_REGNO2OFFSET + 0x60c)
     84 #define EVRX_MAGIC10		(STI_EVRX_REGNO2OFFSET + 0x610)
     85 #define EVRX_MAGIC10_BSY	0x00010000
     86 #define EVRX_MAGIC18		(STI_EVRX_REGNO2OFFSET + 0x618)
     87 #define EVRX_MAGIC1C		(STI_EVRX_REGNO2OFFSET + 0x61c)
     88 
     89 /*
     90  * HP A1659A CRX specific hardware
     91  */
     92 #define STI_CRX_FBOFFSET	0x01000000
     93 
     94 /* 425e EVRX/CRX specific access functions */
     95 static int sti_evrx_putcmap(struct sti_screen *, u_int, u_int);
     96 static void sti_evrx_resetramdac(struct sti_screen *);
     97 static void sti_evrx_resetcmap(struct sti_screen *);
     98 static void sti_evrx_setupfb(struct sti_screen *);
     99 static paddr_t sti_m68k_mmap(void *, void *, off_t, int);
    100 
    101 static struct bus_space_tag sticn_tag;
    102 static struct sti_rom sticn_rom;
    103 static struct sti_screen sticn_scr;
    104 static bus_addr_t sticn_bases[STI_REGION_MAX];
    105 
    106 static const struct wsdisplay_accessops sti_m68k_accessops = {
    107 	sti_ioctl,
    108 	sti_m68k_mmap,
    109 	sti_alloc_screen,
    110 	sti_free_screen,
    111 	sti_show_screen,
    112 	sti_load_font
    113 };
    114 
    115 void
    116 sti_machdep_attach_console(struct sti_machdep_softc *sc)
    117 {
    118 	struct sti_softc *ssc = &sc->sc_sti;
    119 
    120 	ssc->sc_flags |= STI_CONSOLE | STI_ATTACHED;
    121 	ssc->sc_rom = &sticn_rom;
    122 	ssc->sc_rom->rom_softc = ssc;
    123 	ssc->sc_scr = &sticn_scr;
    124 	ssc->sc_scr->scr_rom = ssc->sc_rom;
    125 	memcpy(ssc->bases, sticn_bases, sizeof(ssc->bases));
    126 
    127 	sti_describe(ssc);
    128 }
    129 
    130 void
    131 sti_machdep_attach(struct sti_machdep_softc *sc)
    132 {
    133 	struct sti_softc *ssc = &sc->sc_sti;
    134 	struct sti_screen *scr;
    135 	paddr_t base = sc->sc_base;
    136 	struct wsemuldisplaydev_attach_args waa;
    137 	struct sti_dd *rom_dd;
    138 	uint32_t grid0;
    139 
    140 	/* Identify the board model by dd_grid */
    141 	rom_dd = &ssc->sc_rom->rom_dd;
    142 	grid0 = rom_dd->dd_grid[0];
    143 	scr = ssc->sc_scr;
    144 
    145 	switch (grid0) {
    146 	case STI_DD_EVRX:
    147 		/*
    148 		 * 425e on-board EVRX framebuffer.
    149 		 * bitmap memory can be accessed at offset +0x200000.
    150 		 */
    151 		sc->sc_bitmap = base + STI_EVRX_FBOFFSET;
    152 
    153 		aprint_normal_dev(ssc->sc_dev, "Enable mmap support\n");
    154 
    155 		/*
    156 		 * initialize Bt458 RAMDAC and preserve initial color map
    157 		 */
    158 		sti_evrx_resetramdac(scr);
    159 		sti_evrx_resetcmap(scr);
    160 
    161 		scr->setupfb = sti_evrx_setupfb;
    162 		scr->putcmap = sti_evrx_putcmap;
    163 
    164 		scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
    165 		waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
    166 		waa.scrdata = &scr->scr_screenlist;
    167 		waa.accessops = &sti_m68k_accessops;
    168 		waa.accesscookie = scr;
    169 
    170 		config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
    171 		    CFARGS_NONE);
    172 		break;
    173 
    174 	case STI_DD_CRX:
    175 		/*
    176 		 * HP A1659A CRX on some 425t variants.
    177 		 * bitmap memory can be accessed at offset +0x1000000.
    178 		 */
    179 		sc->sc_bitmap = base + STI_CRX_FBOFFSET;
    180 
    181 		aprint_normal_dev(ssc->sc_dev, "Enable mmap support\n");
    182 
    183 		scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
    184 		waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
    185 		waa.scrdata = &scr->scr_screenlist;
    186 		waa.accessops = &sti_m68k_accessops;
    187 		waa.accesscookie = scr;
    188 
    189 		config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
    190 		    CFARGS_NONE);
    191 		break;
    192 	default:
    193 		/*
    194 		 * Unsupported variants.
    195 		 * Use default common sti(4) attachment (no bitmap support).
    196 		 */
    197 		sti_end_attach(ssc);
    198 		break;
    199 	}
    200 }
    201 
    202 static int
    203 sti_evrx_putcmap(struct sti_screen *scr, u_int index, u_int count)
    204 {
    205 	struct sti_rom *rom = scr->scr_rom;
    206 	bus_space_tag_t bst = rom->memt;
    207 	bus_space_handle_t bsh = rom->regh[2];
    208 	int i;
    209 
    210 	/* magic setup from HP-UX */
    211 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
    212 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
    213 	for (i = index; i < index + count; i++) {
    214 		/* this is what HP-UX woodDownloadCmap() does */
    215 		while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
    216 		    EVRX_MAGIC10_BSY) != 0)
    217 			continue;
    218 		bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
    219 		bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_rcmap[i]);
    220 		bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_gcmap[i]);
    221 		bus_space_write_4(bst, bsh, EVRX_MAGIC10,   scr->scr_bcmap[i]);
    222 	}
    223 	return 0;
    224 }
    225 
    226 static void
    227 sti_evrx_resetramdac(struct sti_screen *scr)
    228 {
    229 	struct sti_rom *rom = scr->scr_rom;
    230 	bus_space_tag_t bst = rom->memt;
    231 	bus_space_handle_t bsh = rom->regh[2];
    232 #if 0
    233 	int i;
    234 #endif
    235 
    236 	/*
    237 	 * Initialize the Bt458.  When we write to control registers,
    238 	 * the address is not incremented automatically. So we specify
    239 	 * it ourselves for each control register.
    240 	 */
    241 
    242 	/* all planes will be read */
    243 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x04);
    244 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0xff);
    245 
    246 	/* all planes have non-blink */
    247 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x05);
    248 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
    249 
    250 	/* palette enabled, ovly plane disabled */
    251 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x06);
    252 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x40);
    253 
    254 	/* no test mode */
    255 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x07);
    256 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
    257 
    258 	/* magic initialization from HP-UX woodInitializeHardware() */
    259 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
    260 	bus_space_write_4(bst, bsh, EVRX_MAGIC04, 0x00000001);
    261 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
    262 	bus_space_write_4(bst, bsh, EVRX_MAGIC0C, 0x00000001);
    263 	bus_space_write_4(bst, bsh, EVRX_MAGIC18, 0xFFFFFFFF);
    264 	bus_space_write_4(bst, bsh, EVRX_MAGIC1C, 0x00000000);
    265 
    266 #if 0
    267 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x00);
    268 	for (i = 0; i < 4; i++) {
    269 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
    270 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
    271 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
    272 	}
    273 #endif
    274 }
    275 
    276 static void
    277 sti_evrx_resetcmap(struct sti_screen *scr)
    278 {
    279 	struct sti_rom *rom = scr->scr_rom;
    280 	bus_space_tag_t bst = rom->memt;
    281 	bus_space_handle_t bsh = rom->regh[2];
    282 	int i;
    283 
    284 	/* magic setup from HP-UX */
    285 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
    286 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
    287 
    288 	/* preserve palette values initialized by STI firmware */
    289 	for (i = 0; i < STI_NCMAP; i++) {
    290 		/* this is what HP-UX woodUploadCmap() does */
    291 		while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
    292 		    EVRX_MAGIC10_BSY) != 0)
    293 			continue;
    294 		bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
    295 		scr->scr_rcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
    296 		scr->scr_gcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
    297 		scr->scr_bcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
    298 	}
    299 }
    300 
    301 static void
    302 sti_evrx_setupfb(struct sti_screen *scr)
    303 {
    304 
    305 	sti_init(scr, 0);
    306 	sti_evrx_resetramdac(scr);
    307 }
    308 
    309 static paddr_t
    310 sti_m68k_mmap(void *v, void *vs, off_t offset, int prot)
    311 {
    312 	struct sti_screen *scr = (struct sti_screen *)v;
    313 	struct sti_rom *rom = scr->scr_rom;
    314 	struct sti_softc *ssc = rom->rom_softc;
    315 	struct sti_machdep_softc *sc = device_private(ssc->sc_dev);
    316 	paddr_t cookie = -1;
    317 
    318 	if ((offset & PAGE_MASK) != 0)
    319 		return -1;
    320 
    321 	switch (scr->scr_wsmode) {
    322 	case WSDISPLAYIO_MODE_MAPPED:
    323 		/* not implemented yet; what should be shown? */
    324 		break;
    325 	case WSDISPLAYIO_MODE_DUMBFB:
    326 		if (offset >= 0 && offset < (scr->fbwidth * scr->fbheight))
    327 			cookie = m68k_btop(sc->sc_bitmap + offset);
    328 		break;
    329 	default:
    330 		break;
    331 	}
    332 
    333 	return cookie;
    334 }
    335 
    336 void
    337 sti_machdep_cnattach(bus_space_tag_t bst, paddr_t base)
    338 {
    339 	int i;
    340 
    341 	sticn_tag = *bst;
    342 
    343 	/* sticn_bases[0] will be fixed in sti_cnattach() */
    344 	for (i = 0; i < STI_REGION_MAX; i++)
    345 		sticn_bases[i] = (bus_addr_t)base;
    346 
    347 	sti_cnattach(&sticn_rom, &sticn_scr, &sticn_tag, sticn_bases,
    348 	    STI_CODEBASE_ALT);
    349 }
    350