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
/*	$NetBSD: xafb.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $	*/

/*-
 * Copyright (c) 2000 Tsubai Masanari.  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. 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.
 */

/* "xa" frame buffer driver.  Currently supports 1280x1024x8 only. */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xafb.c,v 1.21 2021/08/07 16:19:01 thorpej Exp $");

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/kmem.h>
#include <sys/systm.h>

#include <uvm/uvm_extern.h>

#include <mips/locore.h>

#include <machine/adrsmap.h>
#include <machine/apcall.h>
#include <machine/wsconsio.h>

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

#include <newsmips/apbus/apbusvar.h>

struct xafb_reg {
	volatile uint32_t r0;
	volatile uint32_t index;
	volatile uint32_t r2;
	volatile uint32_t zero;
	volatile uint32_t r4;
	volatile uint32_t r5;
	volatile uint32_t r6;
	volatile uint32_t rgb;
};

struct xafb_devconfig {
	uint8_t *dc_fbbase;		/* VRAM base address */
	paddr_t dc_fbpaddr;		/* VRAM physical address */
	struct xafb_reg *dc_reg;	/* register address */
	struct rasops_info dc_ri;
};

struct xafb_softc {
	device_t sc_dev;
	struct xafb_devconfig *sc_dc;
	int sc_nscreens;
	uint8_t sc_cmap_red[256];
	uint8_t sc_cmap_green[256];
	uint8_t sc_cmap_blue[256];
};

int xafb_match(device_t, cfdata_t, void *);
void xafb_attach(device_t, device_t, void *);

int xafb_common_init(struct xafb_devconfig *);
int xafb_is_console(void);

int xafb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
paddr_t xafb_mmap(void *, void *, off_t, int);
int xafb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
    int *, long *);
void xafb_free_screen(void *, void *);
int xafb_show_screen(void *, void *, int, void (*) (void *, int, int), void *);

int xafb_cnattach(void);
int xafb_getcmap(struct xafb_softc *, struct wsdisplay_cmap *);
int xafb_putcmap(struct xafb_softc *, struct wsdisplay_cmap *);

static inline void xafb_setcolor(struct xafb_devconfig *, int, int, int, int);

CFATTACH_DECL_NEW(xafb, sizeof(struct xafb_softc),
    xafb_match, xafb_attach, NULL, NULL);

struct xafb_devconfig xafb_console_dc;

struct wsdisplay_accessops xafb_accessops = {
	xafb_ioctl,
	xafb_mmap,
	xafb_alloc_screen,
	xafb_free_screen,
	xafb_show_screen,
	NULL	/* load_font */
};

struct wsscreen_descr xafb_stdscreen = {
	"std",
	0, 0,
	0,
	0, 0,
	WSSCREEN_REVERSE
};

const struct wsscreen_descr *xafb_scrlist[] = {
	&xafb_stdscreen
};

struct wsscreen_list xafb_screenlist = {
	__arraycount(xafb_scrlist), xafb_scrlist
};

int
xafb_match(device_t parent, cfdata_t cf, void *aux)
{
	struct apbus_attach_args *apa = aux;

	if (strcmp(apa->apa_name, "xa") != 0)
		return 0;

	return 1;
}

void
xafb_attach(device_t parent, device_t self, void *aux)
{
	struct xafb_softc *sc = device_private(self);
	struct apbus_attach_args *apa = aux;
	struct wsemuldisplaydev_attach_args wsa;
	struct xafb_devconfig *dc;
	struct rasops_info *ri;
	int console, i;

	sc->sc_dev = self;
	console = xafb_is_console();

	if (console) {
		dc = &xafb_console_dc;
		ri = &dc->dc_ri;
		ri->ri_flg &= ~RI_NO_AUTO;
		sc->sc_nscreens = 1;
	} else {
		dc = kmem_zalloc(sizeof(struct xafb_devconfig), KM_SLEEP);
		dc->dc_fbpaddr = (paddr_t)0x10000000;
		dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
		dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
		if (xafb_common_init(dc) != 0) {
			aprint_error(": couldn't initialize device\n");
			return;
		}

		ri = &dc->dc_ri;

		/* clear screen */
		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
	}
	sc->sc_dc = dc;

	for (i = 0; i < 256; i++) {
		sc->sc_cmap_red[i] = i;
		sc->sc_cmap_green[i] = i;
		sc->sc_cmap_blue[i] = i;
	}

	aprint_normal(": %d x %d, %dbpp\n",
	    ri->ri_width, ri->ri_height, ri->ri_depth);

	wsa.console = console;
	wsa.scrdata = &xafb_screenlist;
	wsa.accessops = &xafb_accessops;
	wsa.accesscookie = sc;

	config_found(self, &wsa, wsemuldisplaydevprint, CFARGS_NONE);
}

void
xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
{
	volatile struct xafb_reg *reg = dc->dc_reg;

	reg->index = index;
	reg->zero = 0;
	reg->rgb = r;
	reg->rgb = g;
	reg->rgb = b;
}

int
xafb_common_init(struct xafb_devconfig *dc)
{
	struct rasops_info *ri = &dc->dc_ri;
	int i;

	for (i = 0; i < 256; i++)
		xafb_setcolor(dc, i, i, i, i);

	/* initialize rasops */
	ri->ri_width = 1280;
	ri->ri_height = 1024;
	ri->ri_depth = 8;
	ri->ri_stride = 2048;
	ri->ri_bits = (void *)dc->dc_fbbase;
	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
	if (dc == &xafb_console_dc)
		ri->ri_flg |= RI_NO_AUTO;

	rasops_init(ri, 44, 100);

	/* mono */
	ri->ri_devcmap[0] = 0;				/* bg */
	ri->ri_devcmap[1] = 0xffffffff;			/* fg */

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

	return 0;
}

int
xafb_is_console(void)
{
	volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;

	if (*dipsw & 1)					/* XXX right? */
		return 1;

	return 0;
}

int
xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct xafb_softc *sc = v;
	struct xafb_devconfig *dc = sc->sc_dc;
	struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
	struct wsdisplay_fbinfo *wdf = (void *)data;

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

	case NEWSMIPS_WSDISPLAYIO_GINFO:
		nwdf->stride = dc->dc_ri.ri_stride;
		/* FALLTHROUGH */
	case WSDISPLAYIO_GINFO:
		wdf->height = dc->dc_ri.ri_height;
		wdf->width = dc->dc_ri.ri_width;
		wdf->depth = dc->dc_ri.ri_depth;
		wdf->cmsize = 256;
		return 0;

	case WSDISPLAYIO_LINEBYTES:
		*(u_int *)data = dc->dc_ri.ri_stride;
		return 0;
		
	case WSDISPLAYIO_GETCMAP:
		return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_PUTCMAP:
		return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);

	/* case WSDISPLAYIO_SVIDEO: */
	}
	return EPASSTHROUGH;
}

paddr_t
xafb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct xafb_softc *sc = v;
	struct xafb_devconfig *dc = sc->sc_dc;
	struct rasops_info *ri = &dc->dc_ri;

	if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
		return -1;

	return mips_btop(dc->dc_fbpaddr + offset);
}

int
xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
    void **cookiep, int *ccolp, int *crowp, long *attrp)
{
	struct xafb_softc *sc = v;
	struct rasops_info *ri = &sc->sc_dc->dc_ri;
	long defattr;

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

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

	return 0;
}

void
xafb_free_screen(void *v, void *cookie)
{
	struct xafb_softc *sc = v;

	if (sc->sc_dc == &xafb_console_dc)
		panic("xafb_free_screen: console");

	sc->sc_nscreens--;
}

int
xafb_show_screen(void *v, void *cookie, int waitok,
    void (*cb)(void *, int, int), void *cbarg)
{

	return 0;
}

int
xafb_cnattach(void)
{
	struct xafb_devconfig *dc = &xafb_console_dc;
	struct rasops_info *ri = &dc->dc_ri;
	long defattr;
	int crow = 0;

	if (!xafb_is_console())
		return -1;

	dc->dc_fbpaddr = (paddr_t)0x10000000;
	dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
	dc->dc_reg = (void *)0xb4903000;			/* XXX */
	xafb_common_init(dc);

	crow = 0;			/* XXX current cursor pos */

	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
	wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);

	return 0;
}

int
xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
{
	u_int index = cm->index;
	u_int count = cm->count;
	int error;

	if (index >= 256 || count > 256 || index + count > 256)
		return EINVAL;

	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
	if (error)
		return error;
	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
	if (error)
		return error;
	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
	if (error)
		return error;

	return 0;
}

int
xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
{
	u_int index = cm->index;
	u_int count = cm->count;
	int i, error;
	u_char rbuf[256], gbuf[256], bbuf[256];
	u_char *r, *g, *b;

	if (cm->index >= 256 || cm->count > 256 ||
	    (cm->index + cm->count) > 256)
		return EINVAL;
	error = copyin(cm->red, &rbuf[index], count);
	if (error)
		return error;
	error = copyin(cm->green, &gbuf[index], count);
	if (error)
		return error;
	error = copyin(cm->blue, &bbuf[index], count);
	if (error)
		return error;

	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);

	r = &sc->sc_cmap_red[index];
	g = &sc->sc_cmap_green[index];
	b = &sc->sc_cmap_blue[index];
	for (i = 0; i < count; i++)
		xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
	return 0;
}