1/*	$NetBSD: ite_sti.c,v 1.2 2023/01/15 06:19:46 tsutsui Exp $	*/
2/*	$OpenBSD: ite_sti.c,v 1.2 2011/08/18 20:02:58 miod Exp $	*/
3/*
4 * Copyright (c) 2006, 2011, Miodrag Vallat
5 * Copyright (c) 2000-2003 Michael Shalayeff
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifdef ITECONSOLE
31#include <sys/param.h>
32
33#include <lib/libsa/stand.h>
34
35#include <hp300/stand/common/samachdep.h>
36#include <hp300/stand/common/itevar.h>
37
38#if 0
39#include <hp300/dev/dioreg.h>
40#endif
41#include <hp300/dev/sgcreg.h>
42#include <dev/ic/stireg.h>
43
44/*
45 * sti-specific data not available in the ite_data structure.
46 * Since we will only configure one sti display, it is ok to use a global.
47 */
48static struct {
49	uint32_t	codeptr[STI_CODECNT];
50	uint8_t		*code;
51	uint32_t	fontbase;
52	u_int		firstchar, lastchar;
53	struct sti_cfg	cfg;
54	struct sti_ecfg	ecfg;
55} sti;
56
57#define parseshort1(addr, ofs) \
58	(((addr)[(ofs) +  3] << 8) | ((addr)[(ofs) +  7]))
59#define parseword1(addr, ofs) \
60	(((addr)[(ofs) +  3] << 24) | ((addr)[(ofs) +  7] << 16) | \
61	 ((addr)[(ofs) + 11] <<  8) | ((addr)[(ofs) + 15]))
62
63void	sti_do_cursor(struct ite_data *);
64void	sti_fontinfo(struct ite_data *);
65void	sti_init(int);
66void	sti_inqcfg(struct sti_inqconfout *);
67void	sti_iteinit_common(struct ite_data *);
68
69#if 0 /* not yet */
70/* kinda similar to sti_dio_probe() */
71int
72sti_dio_probe(struct ite_data *ip)
73{
74	int scode = ip->scode;
75	uint8_t *id_reg;
76
77	id_reg = (uint8_t *)sctoaddr(scode);
78	if (id_reg[DIOII_SIZEOFF] < STI_DIO_SIZE - 1)
79		return ENODEV;
80
81	id_reg = (uint8_t *)sctoaddr(scode + STI_DIO_SCODE_OFFSET);
82	if (id_reg[3] != STI_DEVTYPE1)
83		return ENODEV;
84
85	return 0;
86}
87
88void
89sti_iteinit_dio(struct ite_data *ip)
90{
91
92	ip->fbbase = (caddr_t)sctoaddr(ip->scode + STI_DIO_SCODE_OFFSET);
93	sti_iteinit_common(ip);
94}
95#endif
96
97void
98sti_iteinit_sgc(struct ite_data *ip)
99{
100
101	ip->fbbase = (uint8_t *)IIOV(SGC_BASE + (ip->scode * SGC_DEVSIZE));
102	sti_iteinit_common(ip);
103}
104
105/*
106 * Initialize the sti device for ite's needs.
107 * We don't bother to check for failures since
108 * - we are in tight space already
109 * - since romputchar() does not work with sti devices, there is no way we
110 *   can report errors (although we could switch to serial...)
111 */
112void
113sti_iteinit_common(struct ite_data *ip)
114{
115	int i;
116	size_t codesize, memsize;
117	uint8_t *va, *code;
118	u_int addr, eaddr, reglist, tmp;
119	struct sti_inqconfout cfg;
120	struct sti_einqconfout ecfg;
121
122	memset(&sti, 0, sizeof sti);
123	va = (uint8_t *)ip->fbbase;
124
125	/*
126	 * Read the microcode.
127	 */
128
129	for (i = 0; i < STI_CODECNT; i++)
130		sti.codeptr[i] =
131		    parseword1(va, (STI_CODEBASE_M68K << 2) + i * 0x10);
132
133	for (i = STI_END; sti.codeptr[i] == 0; i--)
134		continue;
135	codesize = sti.codeptr[i] - sti.codeptr[STI_BEGIN];
136	codesize = (codesize + 3) / 4;
137
138	sti.code = (uint8_t *)alloc(codesize);
139	code = sti.code;
140	addr = (u_int)va + sti.codeptr[STI_BEGIN];
141	eaddr = addr + codesize * 4;
142	for (; addr < eaddr; addr += 4)
143		*code++ = *(uint8_t *)addr;
144
145	for (i = STI_CODECNT - 1; i != 0; i--)
146		if (sti.codeptr[i] != 0) {
147			sti.codeptr[i] -= sti.codeptr[0];
148			sti.codeptr[i] /= 4;
149		}
150
151	sti.codeptr[0] = 0;
152	for (i = STI_END; sti.codeptr[i] == 0; i--);
153	sti.codeptr[i] = 0;
154
155	/*
156	 * Read the regions list.
157	 */
158
159	reglist = parseword1(va, 0x60);
160	for (i = 0; i < STI_REGION_MAX; i++) {
161		tmp = parseword1(va, (reglist & ~3) + i * 0x10);
162		sti.cfg.regions[i] = (u_int)va + ((tmp >> 18) << 12);
163		if (tmp & 0x4000)
164			break;
165	}
166
167	/*
168	 * Allocate scratch memory for the microcode if it needs it.
169	 */
170
171	sti.cfg.ext_cfg = &sti.ecfg;
172	memsize = parseword1(va, 0xa0);
173	if (memsize != 0)
174		sti.ecfg.addr = alloc(memsize);
175
176	/*
177	 * Initialize the display, and get geometry information.
178	 */
179
180	sti_init(0);
181
182	memset(&cfg, 0, sizeof cfg);
183	memset(&ecfg, 0, sizeof ecfg);
184	cfg.ext = &ecfg;
185	sti_inqcfg(&cfg);
186
187	if (cfg.owidth == cfg.width && cfg.oheight == cfg.height) {
188		sti.cfg.oscr_width = cfg.owidth = cfg.fbwidth - cfg.width;
189		sti.cfg.oscr_height = cfg.oheight = cfg.fbheight - cfg.height;
190	}
191
192	ip->dheight = cfg.height;
193	ip->dwidth = cfg.width;
194	ip->fbheight = cfg.fbheight;
195	ip->fbwidth = cfg.fbwidth;
196
197	/*
198	 * Get ready for ite operation!
199	 */
200
201	sti_init(1);
202	sti_fontinfo(ip);
203	sti_clear(ip, 0, 0, ip->rows, ip->cols);	/* necessary? */
204}
205
206void
207sti_putc(struct ite_data *ip, int c, int dy, int dx)
208{
209	sti_unpmv_t unpmv;
210	struct {
211		struct sti_unpmvflags flags;
212		struct sti_unpmvin in;
213		struct sti_unpmvout out;
214	} a;
215
216	memset(&a, 0, sizeof a);
217	a.flags.flags = STI_UNPMVF_WAIT;
218	a.in.bg_colour = STI_COLOUR_BLACK;
219	a.in.fg_colour = STI_COLOUR_WHITE;
220	a.in.x = dx * ip->ftwidth;
221	a.in.y = dy * ip->ftheight;
222	a.in.font_addr = (uint32_t *)((uint8_t *)ip->fbbase + sti.fontbase);
223	a.in.index = c;
224
225	unpmv = (sti_unpmv_t)(sti.code + sti.codeptr[STI_FONT_UNPMV]);
226	(*unpmv)(&a.flags, &a.in, &a.out, &sti.cfg);
227}
228
229void
230sti_cursor(struct ite_data *ip, int flag)
231{
232	switch (flag) {
233	case MOVE_CURSOR:
234		sti_do_cursor(ip);
235		/* FALLTHROUGH */
236	case DRAW_CURSOR:
237		ip->cursorx = ip->curx;
238		ip->cursory = ip->cury;
239		/* FALLTHROUGH */
240	default:
241		sti_do_cursor(ip);
242		break;
243	}
244}
245
246void
247sti_do_cursor(struct ite_data *ip)
248{
249	sti_blkmv_t blkmv;
250	struct {
251		struct sti_blkmvflags flags;
252		struct sti_blkmvin in;
253		struct sti_blkmvout out;
254	} a;
255
256	memset(&a, 0, sizeof a);
257	a.flags.flags = STI_BLKMVF_WAIT | STI_BLKMVF_COLR;
258	a.in.fg_colour = STI_COLOUR_BLACK;
259	a.in.bg_colour = STI_COLOUR_WHITE;
260	a.in.dstx = a.in.srcx = ip->cursorx * ip->ftwidth;
261	a.in.dsty = a.in.srcy = ip->cursory * ip->ftheight;
262	a.in.width = ip->ftwidth;
263	a.in.height = ip->ftheight;
264
265	blkmv = (sti_blkmv_t)(sti.code + sti.codeptr[STI_BLOCK_MOVE]);
266	(*blkmv)(&a.flags, &a.in, &a.out, &sti.cfg);
267}
268
269void
270sti_clear(struct ite_data *ip, int sy, int sx, int h, int w)
271{
272	sti_blkmv_t blkmv;
273	struct {
274		struct sti_blkmvflags flags;
275		struct sti_blkmvin in;
276		struct sti_blkmvout out;
277	} a;
278
279	memset(&a, 0, sizeof a);
280	a.flags.flags = STI_BLKMVF_WAIT | STI_BLKMVF_CLR;
281	a.in.bg_colour = STI_COLOUR_BLACK;
282	a.in.dstx = a.in.srcx = sx * ip->ftwidth;
283	a.in.dsty = a.in.srcy = sy * ip->ftheight;
284	a.in.width = w * ip->ftwidth;
285	a.in.height = h * ip->ftheight;
286
287	blkmv = (sti_blkmv_t)(sti.code + sti.codeptr[STI_BLOCK_MOVE]);
288	(*blkmv)(&a.flags, &a.in, &a.out, &sti.cfg);
289}
290
291void
292sti_scroll(struct ite_data *ip)
293{
294	sti_blkmv_t blkmv;
295	struct {
296		struct sti_blkmvflags flags;
297		struct sti_blkmvin in;
298		struct sti_blkmvout out;
299	} a;
300
301	memset(&a, 0, sizeof a);
302	a.flags.flags = STI_BLKMVF_WAIT;
303	a.in.bg_colour = STI_COLOUR_BLACK;
304	a.in.fg_colour = STI_COLOUR_WHITE;
305	a.in.dstx = a.in.srcx = 0;
306	a.in.dsty = 0;
307	a.in.srcy = ip->ftheight;
308	a.in.width = ip->dwidth;
309	a.in.height = (ip->rows - 1) * ip->ftheight;
310
311	blkmv = (sti_blkmv_t)(sti.code + sti.codeptr[STI_BLOCK_MOVE]);
312	(*blkmv)(&a.flags, &a.in, &a.out, &sti.cfg);
313}
314
315void
316sti_fontinfo(struct ite_data *ip)
317{
318	uint32_t fontbase;
319	volatile uint8_t *fbbase = ip->fbbase;
320
321	fontbase = sti.fontbase = parseword1(fbbase, 0x30) & ~3;
322	ip->ftwidth = (uint8_t)fbbase[fontbase + 0x13];
323	ip->ftheight = (uint8_t)fbbase[fontbase + 0x17];
324	ip->rows = ip->dheight / ip->ftheight;
325	ip->cols = ip->dwidth / ip->ftwidth;
326}
327
328void
329sti_init(int full)
330{
331	sti_init_t init;
332	struct {
333		struct sti_initflags flags;
334		struct sti_initin in;
335		struct sti_initout out;
336	} a;
337
338	memset(&a, 0, sizeof a);
339	a.flags.flags = STI_INITF_WAIT | STI_INITF_CMB | STI_INITF_EBET;
340	if (full)
341		a.flags.flags |= STI_INITF_TEXT | STI_INITF_PBET |
342		    STI_INITF_PBETI | STI_INITF_ICMT;
343	a.in.text_planes = 1;
344
345	init = (sti_init_t)(sti.code + sti.codeptr[STI_INIT_GRAPH]);
346	(*init)(&a.flags, &a.in, &a.out, &sti.cfg);
347}
348
349void
350sti_inqcfg(struct sti_inqconfout *ico)
351{
352	sti_inqconf_t inqconf;
353	struct {
354		struct sti_inqconfflags flags;
355		struct sti_inqconfin in;
356	} a;
357
358	memset(&a, 0, sizeof a);
359	a.flags.flags = STI_INQCONFF_WAIT;
360
361	inqconf = (sti_inqconf_t)(sti.code + sti.codeptr[STI_INQ_CONF]);
362	(*inqconf)(&a.flags, &a.in, ico, &sti.cfg);
363}
364
365#endif
366