summaryrefslogtreecommitdiff
path: root/sys/arch/hppa/dev/sti.c
blob: 90b04af7230691bdb58629a2ba890dd596a1b29f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*	$OpenBSD: sti.c,v 1.1 1998/12/31 03:20:44 mickey Exp $	*/

/*
 * Copyright (c) 1998 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Michael Shalayeff.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define STIDEBUG

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <vm/vm.h>
#include <vm/vm_kern.h>
#include <vm/pmap.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/iomod.h>
#include <machine/autoconf.h>

#include <dev/wscons/wsconsvar.h>
#include <dev/wscons/wscons_emul.h>

#include <hppa/dev/stireg.h>

#include <hppa/dev/cpudevs.h>

struct sti_softc {
	struct device sc_dev;

	u_int sc_flags;
	u_int sc_devtype;
	u_int sc_regs;
	u_int sc_rom;

	int sc_attr;
	struct sti_config sti_config;
	struct sti_fontcfg sti_fontcfg;
	vm_offset_t sc_code;			/* code region allocated */
	void	(*sti_init)   __P((struct sti_initflags *,
				   struct sti_initin *,
				   struct sti_initout *,
				   struct sti_config *));
	void	(*sti_state)  __P((void));
	void	(*sti_font)   __P((struct sti_fontflags *,
				   struct sti_fontin *,
				   struct sti_fontout *,
				   struct sti_config *));
	void	(*sti_bmove)  __P((struct sti_moveflags *,
				   struct sti_movein *,
				   struct sti_moveout *,
				   struct sti_config *));
	void	(*sti_test)   __P((void));
	void	(*sti_fault)  __P((void));
	void	(*sti_inqcfg) __P((struct sti_inquireflags *,
				   struct sti_inquirein *,
				   struct sti_inquireout *,
				   struct sti_config *));
};

/* sti_init() flags */
#define	STI_TEXTMODE	0x01
#define	STI_CLEARSCR	0x02

enum sti_bmove_funcs {
	bmf_clear, bmf_copy, bmf_invert
};

int	stiprobe __P((struct device *, void *, void *));
void	stiattach __P((struct device *, struct device *, void *));

struct cfattach sti_ca = {
	sizeof(struct sti_softc), stiprobe, stiattach
};

struct cfdriver sti_cd = {
	NULL, "sti", DV_DULL
};

void	sti_cursor __P((void *, int, int, int));
void	sti_putstr __P((void *, int, int, char *, int));
void	sti_copycols __P((void *, int, int, int, int));
void	sti_erasecols __P((void *, int, int, int));
void	sti_copyrows __P((void *, int, int, int));
void	sti_eraserows __P((void *, int, int));
void	sti_set_attr __P((void *, int));

struct wscons_emulfuncs	sti_emulfuncs = {
	sti_cursor,
	sti_putstr,
	sti_copycols,
	sti_erasecols,
	sti_copyrows,
	sti_eraserows,
	sti_set_attr
};

u_int stiload __P((vm_offset_t dst, vm_offset_t scode,
		   vm_offset_t ecode, int t));
int sti_init __P((struct sti_softc *sc, int mode));
int sti_inqcfg __P((struct sti_softc *sc, struct sti_inquireout *out));
void sti_bmove __P((struct sti_softc *sc, int, int, int, int, int, int,
		    enum sti_bmove_funcs));
int sti_print __P((void *aux, const char *pnp));
int stiioctl __P((void *v, u_long cmd, caddr_t data, int flag, struct proc *));
int stimmap __P((void *v, off_t offset, int prot));

int
stiprobe(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{
	register struct confargs *ca = aux;
	u_int rom;
	u_char devtype;
	int rv = 0;

	if (ca->ca_type.iodc_type != HPPA_TYPE_FIO ||
	    (ca->ca_type.iodc_sv_model != HPPA_FIO_GSGC &&
	     ca->ca_type.iodc_sv_model != HPPA_FIO_SGC))
		return 0;

	/*
	 * Locate STI ROM.
	 * On some machines it may not be part of the HPA space.
	 */
	if (!PAGE0->pd_resv2[1])
		rom = ca->ca_hpa;
	else
		rom = PAGE0->pd_resv2[1];

	devtype = STI_DEVTYP(STI_TYPE_BWGRF, rom);
	if ((ca->ca_type.iodc_sv_model == HPPA_FIO_SGC &&
	     STI_ID_HI(STI_TYPE_BWGRF, ca->ca_hpa) == STI_ID_FDDI) ||
	    (devtype != STI_TYPE_BWGRF && devtype != STI_TYPE_WWGRF)) {
#ifdef DEBUG
		printf("sti: not a graphics device (:%x)\n", devtype);
#endif
	} else
		rv = 1;

	return rv;
}

int
sti_init(sc, mode)
	struct sti_softc *sc;
	int mode;
{
	struct sti_initflags flags;
	struct sti_initin input;
	struct sti_initout output;

	bzero(&flags,  sizeof(flags));
	bzero(&input,  sizeof(input));
	bzero(&output, sizeof(output));

	flags.wait = 1;
	flags.hardreset = 0;
	flags.clear = 0;
	flags.cmap_black = 1;
	flags.bus_error_timer = 1;
	if (mode & STI_TEXTMODE) {
		flags.texton = 1;
		flags.no_change_bet = 1;
		flags.no_change_bei = 1;
		flags.init_text_cmap = 1;
	}
	input.text_planes = 1;
	sc->sti_init(&flags, &input, &output, &sc->sti_config);
	return (output.text_planes != input.text_planes || output.errno);
}

int
sti_inqcfg(sc, out)
	struct sti_softc *sc;
	struct sti_inquireout *out;
{
	struct sti_inquireflags flags;
	struct sti_inquirein input;

	bzero(&flags,  sizeof(flags));
	bzero(&input,  sizeof(input));
	bzero(out, sizeof(*out));

	flags.wait = 1;
	sc->sti_inqcfg(&flags, &input, out, &sc->sti_config);

	return out->errno;
}

void
sti_bmove(sc, x1, y1, x2, y2, h, w, f)
	struct sti_softc *sc;
	int x1, y1, x2, y2, h, w;
	enum sti_bmove_funcs f;
{
	struct sti_moveflags flags;
	struct sti_movein input;
	struct sti_moveout output;

	bzero(&flags,  sizeof(flags));
	bzero(&input,  sizeof(input));
	bzero(&output, sizeof(output));

	flags.wait = 1;
	switch (f) {
	case bmf_clear:
		flags.clear = 1;
		input.bg_color = 0;
		break;
	case bmf_copy:
		input.fg_color = 1;
		input.bg_color = 0;
		break;
	case bmf_invert:
		input.fg_color = 0;
		input.bg_color = 1;
		break;
	}
	input.src_x = x1;
	input.src_y = y1;
	input.dest_x = x2;
	input.dest_y = y2;
	input.wheight = h;
	input.wwidth = w;

	sc->sti_bmove(&flags, &input, &output, &sc->sti_config);
#ifdef STIDEBUG
	if (output.errno)
		printf ("%s: sti_bmove returned %d\n",
			sc->sc_dev.dv_xname, output.errno);
#endif
}

void
stiattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct wscons_attach_args waa;
	struct sti_inquireout cfg;
	register struct sti_softc *sc = (void *)self;
	register struct confargs *ca = aux;
	register struct wscons_odev_spec *wo;
	register u_int dt, addr;
	int error;

	sc->sc_regs = ca->ca_hpa;
	if (!PAGE0->pd_resv2[1])
		sc->sc_rom = ca->ca_hpa;
	else
		sc->sc_rom = PAGE0->pd_resv2[1];

	dt = sc->sc_devtype = STI_DEVTYP(STI_TYPE_BWGRF, sc->sc_rom);
#if 0
	sc->sti_init = (void *)STI_IGADDR(dt, sc->sc_rom);
	sc->sti_state = (void *)STI_SMADDR(dt, sc->sc_rom);
	sc->sti_font = (void *)STI_FUADDR(dt, sc->sc_rom);
	sc->sti_bmove = (void *)STI_BMADDR(dt, sc->sc_rom);
	sc->sti_test = (void *)STI_STADDR(dt, sc->sc_rom);
	sc->sti_fault = (void *)STI_EHADDR(dt, sc->sc_rom);
	sc->sti_inqcfg = (void *)STI_ICADDR(dt, sc->sc_rom);
#else
	{
		u_int t;
		t = STI_EADDR(dt, sc->sc_rom) - STI_IGADDR(dt, sc->sc_rom);
		t = hppa_round_page(t);
		if (!(sc->sc_code = kmem_malloc(kmem_map, t, 0))) {
			printf(": cannot allocate %d bytes for code\n", t);
			return;
		} else
			pmap_protect(kmem_map->pmap, sc->sc_code,
				     sc->sc_code + t, VM_PROT_ALL);

		t = 0;
		sc->sti_init = (void *)sc->sc_code;
		t += stiload(sc->sc_code + t, STI_IGADDR(dt, sc->sc_rom),
			     STI_SMADDR(dt, sc->sc_rom), dt);
		sc->sti_state = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_SMADDR(dt, sc->sc_rom),
			     STI_FUADDR(dt, sc->sc_rom), dt);
		sc->sti_font = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_FUADDR(dt, sc->sc_rom),
			     STI_BMADDR(dt, sc->sc_rom), dt);
		sc->sti_bmove = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_BMADDR(dt, sc->sc_rom),
			     STI_STADDR(dt, sc->sc_rom), dt);
		sc->sti_test = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_STADDR(dt, sc->sc_rom),
			     STI_EHADDR(dt, sc->sc_rom), dt);
		sc->sti_fault = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_EHADDR(dt, sc->sc_rom),
			     STI_ICADDR(dt, sc->sc_rom), dt);
		sc->sti_inqcfg = (void *)(sc->sc_code + t);
		t += stiload(sc->sc_code + t, STI_ICADDR(dt, sc->sc_rom),
			     STI_EADDR(dt, sc->sc_rom), dt);
		fcacheall();
	}
#endif

	/* fill out sti_config */
	bzero(&sc->sti_config, sizeof(sc->sti_config));
	{
		register u_int *p;
		register u_int *q = (u_int *)STI_MMAP(dt, sc->sc_rom);

		for (p = sc->sti_config.regions;
		     p < &sc->sti_config.regions[STI_REGIONS]; p++) {
			struct sti_region r;
			if (dt == STI_TYPE_BWGRF) {
				/* we know that sti_region is 4 bytes */
				*((u_char *)&r + 0) = *q++;
				*((u_char *)&r + 1) = *q++;
				*((u_char *)&r + 2) = *q++;
				*((u_char *)&r + 3) = *q++;
			} else
				*(u_int *)&r = *q++;

			*p = ((p == sc->sti_config.regions)? sc->sc_rom :
			      sc->sc_regs) + (r.offset << PGSHIFT);

			if (r.last)
				break;
#ifdef STIDEBUG
			printf("@0x%05x, sys %d, cache %d, btlb %d, len %d\n",
			       r.offset, r.sysonly, r.cache, r.btlb, r.length);
#endif
		}
	}


	if ((error = sti_init(sc, 0))) {
		printf (": can not initialize (%d)\n", error);
		return;
	}

	if ((error = sti_inqcfg(sc, &cfg))) {
		printf (": error %d inquiring config\n", error);
		return;
	}

	if ((error = sti_init(sc, STI_TEXTMODE))) {
		printf (": can not initialize (%d)\n", error);
		return;
	}

	/* fill out sti_fontcfg */
	addr = STI_FONTAD(dt, sc->sc_rom) & ~3;
	sc->sti_fontcfg.firstc   = STIF_FIRSTC (dt, addr);
	sc->sti_fontcfg.lastc    = STIF_LASTC  (dt, addr);
	sc->sti_fontcfg.ftheight = STIF_FHEIGHT(dt, addr);
	sc->sti_fontcfg.ftwidth  = STIF_FWIDTH (dt, addr);
	sc->sti_fontcfg.ftype    = STIF_FTYPE  (dt, addr);
	sc->sti_fontcfg.bpc      = STIF_BPC    (dt, addr);
	sc->sti_fontcfg.uheight  = STIF_UHEIGHT(dt, addr);
	sc->sti_fontcfg.uoffset  - STIF_UOFFSET(dt, addr);

	printf (": %s rev %d.%d\n"
		"%s: %dx%d frame buffer, %dx%dx%d display, offset %dx%d\n"
		"%s: %dx%d font type %d, %d bpc, charset %d-%d\n",
		cfg.devname, STI_GLOREV(dt, sc->sc_rom) >> 4,
		STI_GLOREV(dt, sc->sc_rom) & 0xf,
		sc->sc_dev.dv_xname, cfg.fbwidth, cfg.fbheight,
		cfg.dwidth, cfg.dheight, cfg.bpp, cfg.owidth, cfg.oheight,
		sc->sc_dev.dv_xname,
		sc->sti_fontcfg.ftwidth, sc->sti_fontcfg.ftheight,
		sc->sti_fontcfg.ftype, sc->sti_fontcfg.bpc,
		sc->sti_fontcfg.firstc, sc->sti_fontcfg.lastc);

	/* attach WSCONS */
	wo = &waa.waa_odev_spec;

	wo->wo_emulfuncs = &sti_emulfuncs;
	wo->wo_emulfuncs_cookie = sc;

	wo->wo_ioctl = stiioctl;
	wo->wo_mmap = stimmap;
	wo->wo_miscfuncs_cookie = sc;

	wo->wo_nrows = 64;
	wo->wo_ncols = 80;
	wo->wo_crow = 64;
	wo->wo_ccol = 0;

	config_found(parent, &waa, sti_print);

	return;
}

u_int
stiload(dst, scode, ecode, t)
	vm_offset_t dst, scode, ecode;
	int t;
{
	vm_offset_t sdst = dst;

#ifdef STIDEBUG
	printf("stiload(%x, %x, %x, %d)\n", dst, scode, ecode, t);
#endif
	while (scode < ecode) {
		if (t == STI_TYPE_BWGRF)
			*((u_char *)dst)++ = *(u_char *)scode;
		else
			*((u_int *)dst)++ = *(u_int *)scode;
		scode += sizeof(u_int);
	}

	return dst - sdst;
}

int
sti_print(aux, pnp)
	void *aux;
	const char *pnp;
{
	if (pnp)
		printf("wscons at %s", pnp);
	return UNCONF;
}

int
stiioctl(v, cmd, data, flag, p)
	void *v;
	u_long cmd;
	caddr_t data;
	int flag;
	struct proc *p;
{
	/* register struct sti_softc *sc; */

	return -1;
}

int
stimmap(v, offset, prot)
	void *v;
	off_t offset;
	int prot;
{
	/* XXX not finished */
	return offset;
}

void
sti_cursor(v, on, row, col)
	void *v;
	int on, row, col;
{
	register struct sti_softc *sc = v;

	sti_bmove(sc, row * sc->sti_fontcfg.ftheight, col * sc->sti_fontcfg.ftwidth,
		  row * sc->sti_fontcfg.ftheight, col * sc->sti_fontcfg.ftwidth,
		  sc->sti_fontcfg.ftwidth, sc->sti_fontcfg.ftheight, bmf_invert);
}

void
sti_putstr(v, row, col, cp, len)
	void *v;
	int row, col;
	char *cp;
	int len;
{
	register struct sti_softc *sc = v;
	struct sti_fontflags flags;
	struct sti_fontin input;
	struct sti_fontout output;

	bzero(&flags,  sizeof(flags));
	bzero(&input,  sizeof(input));
	bzero(&output, sizeof(output));

	flags.wait = 1;
	/* XXX does not handle text attributes */
	input.fg_color = 1;
	input.bg_color = 0;
	input.dest_x = col * sc->sti_fontcfg.ftwidth;
	input.dest_y = row * sc->sti_fontcfg.ftheight;
	input.startaddr = STI_FONTAD(sc->sc_devtype, sc->sc_rom);
	for (; *cp; cp++, input.dest_x += sc->sti_fontcfg.ftwidth) {
		input.index = *cp;
		sc->sti_font(&flags, &input, &output, &sc->sti_config);
	}
}

void
sti_copycols(v, row, srccol, dstcol, ncols)
	void *v;
	int row, srccol, dstcol, ncols;
{
	register struct sti_softc *sc = v;

	sti_bmove(sc,
		  row * sc->sti_fontcfg.ftheight, srccol * sc->sti_fontcfg.ftwidth,
		  row * sc->sti_fontcfg.ftheight, dstcol * sc->sti_fontcfg.ftwidth,
		  ncols * sc->sti_fontcfg.ftwidth, sc->sti_fontcfg.ftheight,
		  bmf_copy);
}

void
sti_erasecols(v, row, startcol, ncols)
	void *v;
	int row, startcol, ncols;
{
	register struct sti_softc *sc = v;

	sti_bmove(sc,
		  row * sc->sti_fontcfg.ftheight, startcol * sc->sti_fontcfg.ftwidth,
		  row * sc->sti_fontcfg.ftheight, startcol * sc->sti_fontcfg.ftwidth,
		  ncols * sc->sti_fontcfg.ftwidth, sc->sti_fontcfg.ftheight,
		  bmf_clear);
}

void
sti_copyrows(v, srcrow, dstrow, nrows)
	void *v;
	int srcrow, dstrow, nrows;
{
	register struct sti_softc *sc = v;

	sti_bmove(sc,
		  srcrow * sc->sti_fontcfg.ftheight, 0,
		  dstrow * sc->sti_fontcfg.ftheight, 0,
		  sc->sti_config.fbwidth, nrows + sc->sti_fontcfg.ftheight,
		  bmf_copy);
}

void
sti_eraserows(v, srcrow, nrows)
	void *v;
	int srcrow, nrows;
{
	register struct sti_softc *sc = v;

	sti_bmove(sc,
		  srcrow * sc->sti_fontcfg.ftheight, 0,
		  srcrow * sc->sti_fontcfg.ftheight, 0,
		  sc->sti_config.fbwidth, nrows + sc->sti_fontcfg.ftheight,
		  bmf_clear);
}

void
sti_set_attr(v, val)
	void *v;
	int val;
{
	register struct sti_softc *sc = v;

	sc->sc_attr = val;
}