Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: fb_elb.c,v 1.13 2011/07/01 19:02:32 dyoung Exp $	*/

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Juergen Hannken-Illjes.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fb_elb.c,v 1.13 2011/07/01 19:02:32 dyoung Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/rasops/rasops.h>

#include <machine/explora.h>
#include <sys/bus.h>

#include <evbppc/explora/dev/elbvar.h>

#define FB_NPORTS		65536

struct fb_dev {
	void *fb_vram;
	bus_space_tag_t fb_iot;
	bus_space_handle_t fb_ioh;
	struct rasops_info fb_ri;
};

struct fb_elb_softc {
	device_t sc_dev;
	struct fb_dev *sc_fb;
	int sc_nscreens;
};

static int	fb_elb_probe(device_t, cfdata_t, void *);
static void	fb_elb_attach(device_t, device_t, void *);
void		fb_cnattach(bus_space_tag_t, bus_addr_t, void *);
static void	fb_init(struct fb_dev *, int);
static int	fb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
static paddr_t	fb_mmap(void *, void *, off_t, int);
static int	fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
                    int *, int *, long *);
static void	fb_free_screen(void *, void *);
static int	fb_show_screen(void *, void *, int, void (*)(void *, int, int),
                    void *);

static void	fb_eraserows(void *, int, int, long);
static void	fb_erasecols(void *, int, int, int, long);
static void	fb_copyrows(void *, int, int, int);
static void	fb_copycols(void *, int, int, int, int);

static void	s3_init(struct fb_dev *, int *, int *);
static void	s3_copy(struct fb_dev *, int, int, int, int, int, int, int);
static void	s3_fill(struct fb_dev *, int, int, int, int, int, int);

static struct fb_dev console_dev;

static struct wsdisplay_accessops accessops = {
	fb_ioctl,
	fb_mmap,
	fb_alloc_screen,
	fb_free_screen,
	fb_show_screen,
	NULL
};

static struct wsscreen_descr stdscreen = {
	"std",
	0, 0,
	0,
	0, 0,
	0
};

static const struct wsscreen_descr *scrlist[] = {
	&stdscreen
};

static struct wsscreen_list screenlist = {
	__arraycount(scrlist), scrlist
};

CFATTACH_DECL_NEW(fb_elb, sizeof(struct fb_elb_softc),
    fb_elb_probe, fb_elb_attach, NULL, NULL);

static int
fb_elb_probe(device_t parent, cfdata_t cf, void *aux)
{
	struct elb_attach_args *oaa = aux;

	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
		return 0;

	return (1);
}

static void
fb_elb_attach(device_t parent, device_t self, void *aux)
{
	struct fb_elb_softc *sc = device_private(self);
	struct elb_attach_args *eaa = aux;
	struct wsemuldisplaydev_attach_args waa;
	struct rasops_info *ri;
	bus_space_handle_t ioh;
	int is_console;

	sc->sc_dev = self;

	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);

	if (is_console) {
		sc->sc_fb = &console_dev;
		sc->sc_fb->fb_ri.ri_flg &= ~RI_NO_AUTO;
	} else {
		sc->sc_fb = malloc(sizeof(struct fb_dev), M_DEVBUF, M_WAITOK);
		memset(sc->sc_fb, 0, sizeof(struct fb_dev));
	}

	sc->sc_fb->fb_iot = eaa->elb_bt;
	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB,
	    BUS_SPACE_MAP_LINEAR, &ioh);
	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
	    0, &sc->sc_fb->fb_ioh);

	fb_init(sc->sc_fb, !is_console);

	ri = &sc->sc_fb->fb_ri;

	printf(": %d x %d\n", ri->ri_rows, ri->ri_cols);

	waa.console = is_console;
	waa.scrdata = &screenlist;
	waa.accessops = &accessops;
	waa.accesscookie = sc;

	config_found(self, &waa, wsemuldisplaydevprint);
}

static void
fb_init(struct fb_dev *fb, int full)
{
	struct rasops_info *ri = &fb->fb_ri;

	if (full) {
		s3_init(fb, &ri->ri_width, &ri->ri_height);
		ri->ri_depth = 8;
		ri->ri_stride = ri->ri_width;
		ri->ri_bits = fb->fb_vram;
		ri->ri_flg = RI_CENTER;
		if (ri == &console_dev.fb_ri)
			ri->ri_flg |= RI_NO_AUTO;

		rasops_init(ri, 500, 500);
	} else {
		ri->ri_origbits = fb->fb_vram;	/*XXX*/
		rasops_reconfig(ri, 500, 500);
	}

	/* Replace the copy/erase ops. */
	ri->ri_hw = fb;
	ri->ri_ops.eraserows = fb_eraserows;
	ri->ri_ops.erasecols = fb_erasecols;
	ri->ri_ops.copyrows = fb_copyrows;
	ri->ri_ops.copycols = fb_copycols;

	stdscreen.nrows = ri->ri_rows;
	stdscreen.ncols = ri->ri_cols; 
	stdscreen.textops = &ri->ri_ops;
	stdscreen.capabilities = ri->ri_caps;
}

static int
fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct fb_elb_softc *sc = v;
	struct rasops_info *ri = &sc->sc_fb->fb_ri;
	struct wsdisplay_fbinfo *wdf;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
		return(0);

	case WSDISPLAYIO_GINFO:
		wdf = (void *)data;
		wdf->height = ri->ri_height;
		wdf->width = ri->ri_width;
		wdf->depth = ri->ri_depth;
		wdf->cmsize = 16; /*XXX*/
		return(0);

	case WSDISPLAYIO_SVIDEO:
	case WSDISPLAYIO_GETCMAP:
	case WSDISPLAYIO_PUTCMAP:
		break;
	}

	return(EPASSTHROUGH);
}

static paddr_t
fb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct fb_elb_softc *sc = v;

	if (offset < 0 || offset >= SIZE_FB)
		return -1;

	return bus_space_mmap(sc->sc_fb->fb_iot, BASE_FB, offset, prot,
	    BUS_SPACE_MAP_LINEAR);
}

static int
fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
    int *ccolp, int *crowp, long *attrp)
{
	struct fb_elb_softc *sc = v;
	struct rasops_info *ri = &sc->sc_fb->fb_ri;

	if (sc->sc_nscreens > 0)
		return ENOMEM;

	*cookiep = ri;
	*ccolp = *crowp = 0;
	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
	sc->sc_nscreens++;

	return(0);
}

static void
fb_free_screen(void *v, void *cookie)
{
	struct fb_elb_softc *sc = v;

	if (sc->sc_fb == &console_dev)
		panic("fb_free_screen: freeing console");

	sc->sc_nscreens--;
}

static int
fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
    void *cbarg)
{
	return(0);
}

void
fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
{
	struct rasops_info *ri = &console_dev.fb_ri;
	long defattr;

	console_dev.fb_iot = iot;
	console_dev.fb_ioh = iobase;
	console_dev.fb_vram = vram;

	fb_init(&console_dev, 1);

	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);

	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
}

static void
fb_eraserows(void *v, int row, int nrows, long attr)
{
	struct rasops_info *ri = v;
	struct fb_dev *fb = ri->ri_hw;

	row *= ri->ri_font->fontheight; 
	nrows *= ri->ri_font->fontheight;

	s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f);
}

static void
fb_erasecols(void *v, int row, int startcol, int ncols, long attr)
{
	struct rasops_info *ri = v;
	struct fb_dev *fb = ri->ri_hw;

	row *= ri->ri_font->fontheight; 
	startcol *= ri->ri_font->fontwidth;
	ncols *= ri->ri_font->fontwidth;

	s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight,
	    (attr >> 16)&0x0f, 0x0f);
}

static void
fb_copyrows(void *v, int srcrow, int dstrow, int nrows)
{
	struct rasops_info *ri = v;
	struct fb_dev *fb = ri->ri_hw;

	srcrow *= ri->ri_font->fontheight;
	dstrow *= ri->ri_font->fontheight;
	nrows *= ri->ri_font->fontheight;

	s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f);
}

static void
fb_copycols(void *v, int row, int srccol, int dstcol, int ncols)
{
	struct rasops_info *ri = v;
	struct fb_dev *fb = ri->ri_hw;

	row *= ri->ri_font->fontheight;
	srccol *= ri->ri_font->fontwidth;
	dstcol *= ri->ri_font->fontwidth;
	ncols *= ri->ri_font->fontwidth;

	s3_copy(fb, srccol, row, dstcol, row,
	    ncols, ri->ri_font->fontheight, 0x0f);
}

/*
 * S3 support routines
 */

#define S3_CRTC_INDEX		0x83d4
#define S3_CRTC_DATA		0x83d5

#define S3_DAC_RD_INDEX		0x83c7
#define S3_DAC_WR_INDEX		0x83c8
#define S3_DAC_DATA		0x83c9

#define S3_CUR_Y		0x82e8
#define S3_CUR_X		0x86e8
#define S3_DESTY_AXSTP		0x8ae8
#define S3_DESTX_DIASTP		0x8ee8
#define S3_MAJ_AXIS_PCNT	0x96e8
#define S3_GP_STAT		0x9ae8
#define S3_CMD			0x9ae8
#define S3_BKGD_COLOR		0xa2e8
#define S3_FRGD_COLOR		0xa6e8
#define S3_WRT_MASK		0xaae8
#define S3_RD_MASK		0xaee8
#define S3_BKGD_MIX		0xb6e8
#define S3_FRGD_MIX		0xbae8
#define S3_MULTIFUNC_CNTL	0xbee8

#define S3_GP_STAT_FIFO_1	0x0080
#define S3_GP_STAT_BSY		0x0200

#define S3_CMD_BITBLT		0xc001
#define S3_CMD_RECT		0x4001
#define S3_INC_Y		0x0080
#define S3_INC_X		0x0020 
#define S3_DRAW			0x0010
#define S3_MULTI		0x0002

#define S3_CSRC_BKGDCOL		0x0000
#define S3_CSRC_FRGDCOL		0x0020
#define S3_CSRC_DISPMEM		0x0060
#define S3_MIX_NEW		0x0007

#define CMAP_SIZE		256

static u_int8_t default_cmap[] = {
	/* black */		  0,   0,   0,
	/* blue */		  0,   0, 192,
	/* green */		  0, 192,   0,
	/* cyan */		  0, 192, 192,
	/* red */		192,   0,   0,
	/* magenta */		192,   0, 192,
	/* brown */		192, 192,   0,
	/* lightgrey */		212, 208, 200,
	/* darkgrey */		200, 192, 188,
	/* lightblue */		  0,   0, 255,
	/* lightgreen */	  0, 255,   0,
	/* lightcyan */		  0, 255, 255,
	/* lightred */		255,   0,   0,
	/* lightmagenta */	255,   0, 255,
	/* yellow */		255, 255,   0,
	/* white */		255, 255, 255,
};

static void
s3_init(struct fb_dev *fb, int *width, int *height)
{
	int i, j, w, h;
	bus_space_tag_t iot = fb->fb_iot;
	bus_space_handle_t ioh = fb->fb_ioh;

	/* Initialize colormap */

	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);

	for (i = j = 0; i < CMAP_SIZE*3; i++) {
		bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2);
		j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]);
	}

	/* Retrieve frame buffer geometry */

	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);

	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);

	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);

	h += (i << 7) & 0x100;
	h += (i << 3) & 0x200;

	*width = (w+1) << 3;
	*height = h+1;
}

static void
s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y,
    int width, int height, int mask)
{
	bus_space_tag_t iot = fb->fb_iot;
	bus_space_handle_t ioh = fb->fb_ioh;
	u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW;

	if (src_x > dest_x)
		cmd |= S3_INC_X;
	else {
		src_x += width-1;
		dest_x += width-1;
	}

	if (src_y > dest_y)
		cmd |= S3_INC_Y;
	else {
		src_y += height-1;
		dest_y += height-1;
	}

	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
		;

	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW);
	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
	bus_space_write_2(iot, ioh, S3_CUR_X, src_x);
	bus_space_write_2(iot, ioh, S3_CUR_Y, src_y);
	bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x);
	bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y);
	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
	bus_space_write_2(iot, ioh, S3_CMD, cmd);

	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
		;
}

static void
s3_fill(struct fb_dev *fb, int x, int y, int width, int height,
    int color, int mask)
{
	bus_space_tag_t iot = fb->fb_iot;
	bus_space_handle_t ioh = fb->fb_ioh;
	u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW;

	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
		;

	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW);
	bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color);
	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
	bus_space_write_2(iot, ioh, S3_CUR_X, x);
	bus_space_write_2(iot, ioh, S3_CUR_Y, y);
	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
	bus_space_write_2(iot, ioh, S3_CMD, cmd);

	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
		;
}