xref: /netbsd-src/sys/arch/ews4800mips/sbd/fb_sbdio.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: fb_sbdio.c,v 1.13 2014/01/31 15:41:48 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #define WIRED_FB_TLB
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: fb_sbdio.c,v 1.13 2014/01/31 15:41:48 tsutsui Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <dev/cons.h>
41 
42 #include <uvm/uvm_extern.h>     /* pmap function to remap FB */
43 
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wsdisplayvar.h>
46 #include <dev/wsfont/wsfont.h>
47 #include <dev/rasops/rasops.h>
48 
49 #include <mips/pte.h>
50 
51 #include <machine/locore.h>
52 #include <machine/sbdiovar.h>
53 
54 #include <machine/gareg.h>
55 #include <machine/gavar.h>
56 
57 #include <machine/vmparam.h>
58 #include <machine/wired_map.h>
59 
60 
61 struct fb_softc {
62 	device_t sc_dev;
63 	struct rasops_info *sc_ri;
64 	struct ga *sc_ga;
65 	int sc_nscreens;
66 };
67 
68 int fb_sbdio_match(device_t, cfdata_t, void *);
69 void fb_sbdio_attach(device_t, device_t, void *);
70 
71 CFATTACH_DECL_NEW(fb_sbdio, sizeof(struct fb_softc),
72     fb_sbdio_match, fb_sbdio_attach, NULL, NULL);
73 
74 int _fb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
75 paddr_t _fb_mmap(void *, void *, off_t, int);
76 int _fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
77     int *, int *, long *);
78 void _fb_free_screen(void *, void *);
79 int _fb_show_screen(void *, void *, int, void (*)(void *, int, int), void *);
80 void _fb_pollc(void *, int);
81 void fb_common_init(struct rasops_info *, struct ga *);
82 int fb_sbdio_cnattach(uint32_t, uint32_t, int);
83 void fb_pmap_enter(paddr_t, paddr_t, vaddr_t *, vaddr_t *);
84 
85 struct wsscreen_descr _fb_std_screen = {
86 	"std", 0, 0,
87 	0, /* textops */
88 	0, 0,
89 	0
90 };
91 
92 const struct wsscreen_descr *_fb_screen_table[] = {
93 	&_fb_std_screen,
94 };
95 
96 struct wsscreen_list _fb_screen_list = {
97 	.nscreens	=
98 	    sizeof(_fb_screen_table) / sizeof(_fb_screen_table[0]),
99 	.screens	= _fb_screen_table
100 };
101 
102 struct wsdisplay_accessops _fb_accessops = {
103 	.ioctl		= _fb_ioctl,
104 	.mmap		= _fb_mmap,
105 	.alloc_screen	= _fb_alloc_screen,
106 	.free_screen	= _fb_free_screen,
107 	.show_screen	= _fb_show_screen,
108 	.load_font	= 0,
109 	.pollc		= _fb_pollc
110 };
111 
112 /* console stuff */
113 static struct rasops_info fb_console_ri;
114 static struct ga fb_console_ga;
115 static paddr_t fb_consaddr;
116 
117 
118 int
119 fb_sbdio_match(device_t parent, cfdata_t cf, void *aux)
120 {
121 	struct sbdio_attach_args *sa = aux;
122 
123 	return strcmp(sa->sa_name, "fb") ? 0 : 1;
124 }
125 
126 void
127 fb_sbdio_attach(device_t parent, device_t self, void *aux)
128 {
129 	struct fb_softc *sc = device_private(self);
130 	struct sbdio_attach_args *sa = aux;
131 	struct wsemuldisplaydev_attach_args wa;
132 	struct rasops_info *ri;
133 	struct ga *ga;
134 	vaddr_t memva, regva;
135 	int console;
136 
137 	sc->sc_dev = self;
138 	aprint_normal("\n");
139 
140 	console = (sa->sa_addr1 == fb_consaddr);
141 	if (console) {
142 		/* already initialized in fb_cnattach() */
143 		sc->sc_ri = ri = &fb_console_ri;
144 		ri->ri_flg &= ~RI_NO_AUTO;
145 		sc->sc_ga = &fb_console_ga;
146 		sc->sc_nscreens = 1;
147 	} else {
148 		ri = malloc(sizeof(struct rasops_info), M_DEVBUF,
149 		    M_NOWAIT | M_ZERO);
150 		if (ri == NULL) {
151 			printf(":can't allocate rasops memory\n");
152 			return;
153 		}
154 		ga = malloc(sizeof(struct ga), M_DEVBUF, M_NOWAIT | M_ZERO);
155 		if (ga == NULL) {
156 			printf(":can't allocate ga memory\n");
157 			return;
158 		}
159 		ga->reg_paddr = sa->sa_addr2;
160 		ga->flags = sa->sa_flags;
161 		fb_pmap_enter(sa->sa_addr1, sa->sa_addr2,
162 		    &memva, &regva);
163 		ri->ri_bits = (void *)memva;
164 		ga->reg_addr = regva;
165 		fb_common_init(ri, ga);
166 		sc->sc_ri = ri;
167 		sc->sc_ga = ga;
168 	}
169 
170 	wa.console = console;
171 	wa.scrdata = &_fb_screen_list;
172 	wa.accessops = &_fb_accessops;
173 	wa.accesscookie	= (void *)sc;
174 
175 	config_found(self, &wa, wsdisplaydevprint);
176 }
177 
178 void
179 fb_common_init(struct rasops_info *ri, struct ga *ga)
180 {
181 	int ga_active, cookie;
182 
183 	/* XXX */
184 	ga_active = 0;
185 	if (ga->flags == 0x0000 || ga->flags == 0x0001)
186 		ga_active = ga_init(ga);
187 
188 	/*
189 	 * TR2 IPL CLUT.
190 	 * 0     black
191 	 * 1	 red
192 	 * 2	 green
193 	 * 4	 blue
194 	 * 8	 yellow
195 	 * 16	 cyan
196 	 * 32	 magenta
197 	 * 64	 light gray
198 	 * 128	 dark gray
199 	 * 255	 white
200 	 * other black
201 	 * When CLUT isn't initialized for NetBSD, use black-red pair.
202 	 */
203 	ri->ri_flg = RI_CENTER | RI_CLEAR;
204 	if (!ga_active)
205 		ri->ri_flg |= RI_FORCEMONO;
206 	if (ri == &fb_console_ri)
207 		ri->ri_flg |= RI_NO_AUTO;
208 
209 	ri->ri_depth = 8;
210 	ri->ri_width = 1280;
211 	ri->ri_height = 1024;
212 	ri->ri_stride = 2048;
213 	ri->ri_hw = (void *)ga;
214 
215 	wsfont_init();
216 	/* prefer 12 pixel wide font */
217 	cookie = wsfont_find(NULL, 12, 0, 0, 0, 0, WSFONT_FIND_BITMAP);
218 	if (cookie <= 0)
219 		cookie = wsfont_find(NULL, 0, 0, 0, 0, 0, WSFONT_FIND_BITMAP);
220 	if (cookie <= 0) {
221 		printf("sfb: font table is empty\n");
222 		return;
223 	}
224 
225 	if (wsfont_lock(cookie, &ri->ri_font)) {
226 		printf("fb: can't lock font\n");
227 		return;
228 	}
229 	ri->ri_wsfcookie = cookie;
230 
231 	rasops_init(ri, 34, 80);
232 
233 #if 0
234 	/* XXX no accelarated functions yet */
235 	ri->ri_ops.putchar = xxx_putchar;
236 	ri->ri_ops.erasecols = xxx_erasecols;
237 	ri->ri_ops.copyrows = xxx_copyrows;
238 	ri->ri_ops.eraserows = xxx_eraserows;
239 	ir->ri_do_cursor = xxx_do_cursor;
240 #endif
241 
242 	/* XXX shouldn't be global */
243 	_fb_std_screen.nrows = ri->ri_rows;
244 	_fb_std_screen.ncols = ri->ri_cols;
245 	_fb_std_screen.textops = &ri->ri_ops;
246 	_fb_std_screen.capabilities = ri->ri_caps;
247 }
248 
249 int
250 fb_sbdio_cnattach(uint32_t mem, uint32_t reg, int flags)
251 {
252 	struct rasops_info *ri;
253 	struct ga *ga;
254 	vaddr_t memva, regva;
255 	long defattr;
256 
257 	ri = &fb_console_ri;
258 	ga = &fb_console_ga;
259 
260 	ga->reg_paddr = reg;
261 	ga->flags = flags;
262 	fb_pmap_enter((paddr_t)mem, (paddr_t)reg, &memva, &regva);
263 	ri->ri_bits = (void *)memva;
264 	ga->reg_addr = regva;
265 
266 	fb_common_init(ri, ga);
267 
268 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
269 	wsdisplay_cnattach(&_fb_std_screen, ri, 0, 0, defattr);
270 	fb_consaddr = mem;
271 
272 	return 0;
273 }
274 
275 int
276 _fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
277 {
278 	struct fb_softc *sc = v;
279 	struct wsdisplay_fbinfo *fbinfo = (void *)data;
280 	struct wsdisplay_cmap *cmap = (void *)data;
281 	struct rasops_info *ri = sc->sc_ri;
282 	struct ga *ga = sc->sc_ga;
283 	int i;
284 
285 	switch (cmd) {
286 	case WSDISPLAYIO_GTYPE:
287 		*(uint32_t *)data = WSDISPLAY_TYPE_UNKNOWN;
288 		return 0;
289 
290 	case WSDISPLAYIO_GINFO:
291 		fbinfo->height = ri->ri_height;
292 		fbinfo->width  = ri->ri_width;
293 		fbinfo->depth  = ri->ri_depth;
294 		fbinfo->cmsize = 256;
295 		return 0;
296 
297 	case WSDISPLAYIO_LINEBYTES:
298 		*(u_int *)data = ri->ri_stride;
299 		return 0;
300 
301 	case WSDISPLAYIO_GETCMAP:
302 		if (ri->ri_flg == RI_FORCEMONO)
303 			break;
304 		ga_clut_get(ga);
305 		for (i = 0; i < cmap->count; i++) {
306 			cmap->red[i] = ga->clut[cmap->index + i][0];
307 			cmap->green[i] = ga->clut[cmap->index + i][1];
308 			cmap->blue[i] = ga->clut[cmap->index + i][2];
309 		}
310 		return 0;
311 
312 	case WSDISPLAYIO_PUTCMAP:
313 		if (ri->ri_flg == RI_FORCEMONO)
314 			break;
315 		for (i = 0; i < cmap->count; i++) {
316 			ga->clut[cmap->index + i][0] = cmap->red[i];
317 			ga->clut[cmap->index + i][1] = cmap->green[i];
318 			ga->clut[cmap->index + i][2] = cmap->blue[i];
319 		}
320 		ga_clut_set(ga);
321 		return 0;
322 	}
323 
324 	return EPASSTHROUGH;
325 }
326 
327 paddr_t
328 _fb_mmap(void *v, void *vs, off_t offset, int prot)
329 {
330 
331 	if (offset < 0 || offset >= GA_FRB_SIZE)
332 		return -1;
333 
334 	return mips_btop(GA_FRB_ADDR + offset);
335 }
336 
337 int
338 _fb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookie,
339     int *curx, int *cury, long *attr)
340 {
341 	struct fb_softc *sc = v;
342 	struct rasops_info *ri = sc->sc_ri;
343 	long defattr;
344 
345 #if 0
346 	if (sc->sc_nscreens > 0)
347 		return ENOMEM;
348 #endif
349 
350 	*cookie = ri;
351 	*curx = *cury = 0;
352 	ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
353 	*attr = defattr;
354 	sc->sc_nscreens++;
355 
356 	return 0;
357 }
358 
359 void
360 _fb_free_screen(void *v, void *cookie)
361 {
362 	struct fb_softc *sc = v;
363 
364 	sc->sc_nscreens--;
365 }
366 
367 int
368 _fb_show_screen(void *v, void *cookie, int waitok,
369     void (*cb)(void *, int, int), void *cbarg)
370 {
371 
372 	return 0;
373 }
374 
375 void
376 _fb_pollc(void *v, int on)
377 {
378 
379 	/* no interrupt used. */
380 }
381 
382 void
383 fb_pmap_enter(paddr_t fb_paddr, paddr_t reg_paddr,
384     vaddr_t *fb_vaddr, vaddr_t *reg_vaddr)
385 {
386 #ifdef WIRED_FB_TLB	/* Make wired 16MPage for frame buffer */
387 	vaddr_t va;
388 	vsize_t fb_off, reg_off, pgsize;
389 
390 	pgsize = MIPS3_WIRED_SIZE;
391 	fb_off  = fb_paddr  & MIPS3_WIRED_OFFMASK;
392 	reg_off = reg_paddr & MIPS3_WIRED_OFFMASK;
393 	fb_paddr  = fb_paddr  & ~MIPS3_WIRED_OFFMASK;
394 	reg_paddr = reg_paddr & ~MIPS3_WIRED_OFFMASK;
395 	va = GA_FRB_ADDR;
396 
397 	if (mips3_wired_enter_page(va, fb_paddr, pgsize) == false) {
398 		printf("cannot allocate fb memory\n");
399 		return;
400 	}
401 
402 	if (mips3_wired_enter_page(va + pgsize, reg_paddr, pgsize) == false) {
403 		printf("cannot allocate fb register\n");
404 		return;
405 	}
406 
407 	*fb_vaddr  = va + fb_off;
408 	*reg_vaddr = va + pgsize + reg_off;
409 #else /* WIRED_FB_TLB */
410 	paddr_t pa, epa;
411 	vaddr_t va, tva;
412 
413 	pa = addr;
414 	epa = pa + size;
415 
416 	va = uvm_km_alloc(kernel_map, epa - pa, 0, UVM_KMF_VAONLY);
417 	if (va == 0)
418 		for (;;)
419 			ROM_MONITOR();
420 
421 	for (tva = va; pa < epa; pa += PAGE_SIZE, tva += PAGE_SIZE)
422 		pmap_kenter_pa(tva, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
423 
424 	pmap_update(pmap_kernel());
425 
426 	*fb_vaddr  = va;
427 #endif /* WIRED_FB_TLB */
428 }
429