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