crmfb.c revision 1.1 1 /* $NetBSD: crmfb.c,v 1.1 2007/04/12 13:10:20 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jared D. McNeill.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * SGI-CRM (O2) Framebuffer driver
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: crmfb.c,v 1.1 2007/04/12 13:10:20 jmcneill Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46
47 #define _SGIMIPS_BUS_DMA_PRIVATE
48 #include <machine/autoconf.h>
49 #include <machine/bus.h>
50 #include <machine/machtype.h>
51 #include <machine/vmparam.h>
52
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wsfont/wsfont.h>
56 #include <dev/rasops/rasops.h>
57 #include <dev/wscons/wsdisplay_vconsvar.h>
58
59 struct wsscreen_descr crmfb_defaultscreen = {
60 "default",
61 0, 0,
62 NULL,
63 8, 16,
64 WSSCREEN_WSCOLORS,
65 NULL,
66 };
67
68 const struct wsscreen_descr *_crmfb_scrlist[] = {
69 &crmfb_defaultscreen,
70 };
71
72 struct wsscreen_list crmfb_screenlist = {
73 sizeof(_crmfb_scrlist) / sizeof(struct wsscreen_descr *),
74 _crmfb_scrlist
75 };
76
77 static struct vcons_screen crmfb_console_screen;
78
79 static int crmfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
80 static paddr_t crmfb_mmap(void *, void *, off_t, int);
81 static void crmfb_init_screen(void *, struct vcons_screen *, int, long *);
82
83 struct wsdisplay_accessops crmfb_accessops = {
84 crmfb_ioctl,
85 crmfb_mmap,
86 NULL, /* alloc_screen */
87 NULL, /* free_screen */
88 NULL, /* show_screen */
89 NULL, /* load_font */
90 NULL, /* pollc */
91 NULL, /* scroll */
92 };
93
94 /* Memory to allocate to SGI-CRM -- remember, this is stolen from
95 * host memory!
96 */
97 #define CRMFB_SIZE (4 * 1024 * 1024)
98 #define CRMFB_TILESIZE (512*128)
99
100 #define CRMFB_DOTCLOCK 0x00000004
101 #define CRMFB_DOTCLOCK_CLKRUN_SHIFT 20
102 #define CRMFB_VT_XY 0x00010000
103 #define CRMFB_VT_XY_FREEZE_SHIFT 31
104 #define CRMFB_VT_INTR01 0x00010020
105 #define CRMFB_VT_INTR01_EN 0xffffffff
106 #define CRMFB_VT_INTR23 0x00010024
107 #define CRMFB_VT_INTR23_EN 0xffffffff
108 #define CRMFB_VT_VPIX_EN 0x00010038
109 #define CRMFB_VT_VPIX_EN_OFF_SHIFT 0
110 #define CRMFB_VT_VCMAP 0x0001003c
111 #define CRMFB_VT_VCMAP_ON_SHIFT 12
112 #define CRMFB_VT_HCMAP 0x00010040
113 #define CRMFB_VT_HCMAP_ON_SHIFT 12
114 #define CRMFB_OVR_WIDTH_TILE 0x00020000
115 #define CRMFB_OVR_CONTROL 0x00020008
116 #define CRMFB_OVR_CONTROL_DMAEN_SHIFT 0
117 #define CRMFB_FRM_TILESIZE 0x00030000
118 #define CRMFB_FRM_TILESIZE_RHS_SHIFT 0
119 #define CRMFB_FRM_TILESIZE_WIDTH_SHIFT 5
120 #define CRMFB_FRM_TILESIZE_DEPTH_SHIFT 13
121 #define CRMFB_FRM_TILESIZE_DEPTH_8 0
122 #define CRMFB_FRM_TILESIZE_DEPTH_16 1
123 #define CRMFB_FRM_TILESIZE_DEPTH_32 2
124 #define CRMFB_FRM_TILESIZE_FIFOR_SHIFT 15
125 #define CRMFB_FRM_PIXSIZE 0x00030004
126 #define CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT 16
127 #define CRMFB_FRM_CONTROL 0x0003000c
128 #define CRMFB_FRM_CONTROL_DMAEN_SHIFT 0
129 #define CRMFB_FRM_CONTROL_LINEAR_SHIFT 1
130 #define CRMFB_FRM_CONTROL_TILEPTR_SHIFT 9
131 #define CRMFB_DID_CONTROL 0x00040004
132 #define CRMFB_DID_CONTROL_DMAEN_SHIFT 0
133 #define CRMFB_MODE 0x00048000
134 #define CRMFB_MODE_TYP_SHIFT 2
135 #define CRMFB_MODE_TYP_I8 0
136 #define CRMFB_MODE_TYP_ARGB5 4
137 #define CRMFB_MODE_TYP_RGB8 5
138 #define CRMFB_MODE_BUF_SHIFT 0
139 #define CRMFB_MODE_BUF_BOTH 3
140 #define CRMFB_CMAP 0x00050000
141 #define CRMFB_CMAP_FIFO 0x00058000
142 #define CRMFB_GMAP 0x00060000
143 #define CRMFB_CRS_CONTROL 0x00070004
144
145 static int crmfb_match(struct device *, struct cfdata *, void *);
146 static void crmfb_attach(struct device *, struct device *, void *);
147 int crmfb_probe(void);
148
149 #define KERNADDR(p) ((void *)((p).addr))
150 #define DMAADDR(p) ((p).map->dm_segs[0].ds_addr)
151
152 struct crmfb_dma {
153 bus_dmamap_t map;
154 void *addr;
155 bus_dma_segment_t segs[1];
156 int nsegs;
157 size_t size;
158 };
159
160 struct crmfb_softc {
161 struct device sc_dev;
162 struct vcons_data sc_vd;
163
164 bus_space_tag_t sc_iot;
165 bus_space_handle_t sc_ioh;
166 bus_dma_tag_t sc_dmat;
167
168 struct crmfb_dma sc_dma;
169 struct crmfb_dma sc_dmai;
170
171 int sc_width;
172 int sc_height;
173 int sc_depth;
174 uint32_t sc_fbsize;
175
176 int sc_wsmode;
177 u_char sc_cmap_red[256];
178 u_char sc_cmap_green[256];
179 u_char sc_cmap_blue[256];
180 };
181
182 static int crmfb_putcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
183 static int crmfb_getcmap(struct crmfb_softc *, struct wsdisplay_cmap *);
184 static void crmfb_set_palette(struct crmfb_softc *,
185 int, uint8_t, uint8_t, uint8_t);
186
187 CFATTACH_DECL(crmfb, sizeof(struct crmfb_softc),
188 crmfb_match, crmfb_attach, NULL, NULL);
189
190 static int
191 crmfb_match(struct device *parent, struct cfdata *cf, void *opaque)
192 {
193 return crmfb_probe();
194 }
195
196 static void
197 crmfb_attach(struct device *parent, struct device *self, void *opaque)
198 {
199 struct mainbus_attach_args *ma;
200 struct crmfb_softc *sc;
201 struct rasops_info *ri;
202 struct wsemuldisplaydev_attach_args aa;
203 uint32_t d, h;
204 uint16_t *p;
205 unsigned long v;
206 long defattr;
207 int rv, i;
208
209 sc = (struct crmfb_softc *)self;
210 ma = (struct mainbus_attach_args *)opaque;
211
212 sc->sc_iot = SGIMIPS_BUS_SPACE_CRIME;
213 sc->sc_dmat = &sgimips_default_bus_dma_tag;
214 sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
215
216 aprint_normal(": SGI CRIME Graphics Display Engine\n");
217 rv = bus_space_map(sc->sc_iot, ma->ma_addr, 0 /* XXX */,
218 BUS_SPACE_MAP_LINEAR, &sc->sc_ioh);
219 if (rv)
220 panic("crmfb_attach: can't map I/O space");
221
222 /* determine mode configured by firmware */
223 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_VCMAP);
224 sc->sc_width = (d >> CRMFB_VT_VCMAP_ON_SHIFT) & 0xfff;
225 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_HCMAP);
226 sc->sc_height = (d >> CRMFB_VT_HCMAP_ON_SHIFT) & 0xfff;
227 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
228 h = (d >> CRMFB_FRM_TILESIZE_DEPTH_SHIFT) & 0x3;
229 if (h == 0)
230 sc->sc_depth = 8;
231 else if (h == 1)
232 sc->sc_depth = 16;
233 else
234 sc->sc_depth = 32;
235
236 printf("%s: initial resolution %dx%d %d bpp\n",
237 sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
238
239 #if 0
240 /* Changing the colour depth is easy... */
241 sc->sc_depth = 32;
242 printf("%s: using resolution %dx%d %d bpp\n",
243 sc->sc_dev.dv_xname, sc->sc_width, sc->sc_height, sc->sc_depth);
244 #endif
245
246 #if 0
247 /* XXX revisit later */
248 sc->sc_fbsize = sc->sc_width * sc->sc_height * sc->sc_depth / 8;
249 sc->sc_fbsize = (sc->sc_fbsize / CRMFB_TILESIZE) + CRMFB_TILESIZE;
250 #else
251 sc->sc_fbsize = CRMFB_SIZE;
252 #endif
253
254 sc->sc_dmai.size = 128 * sizeof(uint16_t);
255 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmai.size, 65536, 0,
256 sc->sc_dmai.segs,
257 sizeof(sc->sc_dmai.segs) / sizeof(sc->sc_dmai.segs[0]),
258 &sc->sc_dmai.nsegs, BUS_DMA_NOWAIT);
259 if (rv)
260 panic("crmfb_attach: can't allocate DMA memory");
261 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dmai.segs, sc->sc_dmai.nsegs,
262 sc->sc_dmai.size, &sc->sc_dmai.addr,
263 BUS_DMA_NOWAIT);
264 if (rv)
265 panic("crmfb_attach: can't map DMA memory");
266 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dmai.size, 1,
267 sc->sc_dmai.size, 0, BUS_DMA_NOWAIT, &sc->sc_dmai.map);
268 if (rv)
269 panic("crmfb_attach: can't create DMA map");
270 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmai.map, sc->sc_dmai.addr,
271 sc->sc_dmai.size, NULL, BUS_DMA_NOWAIT);
272 if (rv)
273 panic("crmfb_attach: can't load DMA map");
274
275 sc->sc_dma.size = sc->sc_fbsize;
276 rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma.size, 65536, 0,
277 sc->sc_dma.segs,
278 sizeof(sc->sc_dma.segs) / sizeof(sc->sc_dma.segs[0]),
279 &sc->sc_dma.nsegs, BUS_DMA_NOWAIT);
280 if (rv)
281 panic("crmfb_attach: can't allocate DMA memory");
282 rv = bus_dmamem_map(sc->sc_dmat, sc->sc_dma.segs, sc->sc_dma.nsegs,
283 sc->sc_dma.size, &sc->sc_dma.addr,
284 BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
285 if (rv)
286 panic("crmfb_attach: can't map DMA memory");
287 rv = bus_dmamap_create(sc->sc_dmat, sc->sc_dma.size, 1,
288 sc->sc_dma.size, 0, BUS_DMA_NOWAIT, &sc->sc_dma.map);
289 if (rv)
290 panic("crmfb_attach: can't create DMA map");
291 rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dma.map, sc->sc_dma.addr,
292 sc->sc_dma.size, NULL, BUS_DMA_NOWAIT);
293 if (rv)
294 panic("crmfb_attach: can't load DMA map");
295
296 p = KERNADDR(sc->sc_dmai);
297 v = (unsigned long)DMAADDR(sc->sc_dma);
298 for (i = 0; i < (sc->sc_fbsize >> 16); i++) {
299 p[i] = ((uint32_t)v >> 16) + i;
300 }
301
302 memset(KERNADDR(sc->sc_dma), 0x00, sc->sc_fbsize);
303
304 printf("%s: allocated %d byte fb @ %p (%p)\n", sc->sc_dev.dv_xname,
305 sc->sc_fbsize, KERNADDR(sc->sc_dmai), KERNADDR(sc->sc_dma));
306
307 ri = &crmfb_console_screen.scr_ri;
308 memset(ri, 0, sizeof(struct rasops_info));
309
310 vcons_init(&sc->sc_vd, sc, &crmfb_defaultscreen, &crmfb_accessops);
311 sc->sc_vd.init_screen = crmfb_init_screen;
312 crmfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
313 vcons_init_screen(&sc->sc_vd, &crmfb_console_screen, 1, &defattr);
314
315 crmfb_defaultscreen.ncols = ri->ri_cols;
316 crmfb_defaultscreen.nrows = ri->ri_rows;
317 crmfb_defaultscreen.textops = &ri->ri_ops;
318 crmfb_defaultscreen.capabilities = ri->ri_caps;
319 crmfb_defaultscreen.modecookie = NULL;
320
321 /* disable DMA */
322 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL);
323 d &= ~(1 << CRMFB_OVR_CONTROL_DMAEN_SHIFT);
324 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_CONTROL, d);
325 DELAY(10000);
326 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
327 d &= ~(1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
328 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
329 DELAY(10000);
330 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DID_CONTROL, 0);
331 DELAY(10000);
332
333 /* ensure that CRM starts drawing at the top left of the screen
334 * when we re-enable DMA later
335 */
336 d = (1 << CRMFB_VT_XY_FREEZE_SHIFT);
337 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, d);
338 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
339 d &= ~(1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
340 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
341
342 /* reset FIFO */
343 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE);
344 d |= (1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
345 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
346 d &= ~(1 << CRMFB_FRM_TILESIZE_FIFOR_SHIFT);
347 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
348
349 /* setup colour mode */
350 switch (sc->sc_depth) {
351 case 8:
352 h = CRMFB_MODE_TYP_I8;
353 break;
354 case 16:
355 h = CRMFB_MODE_TYP_ARGB5;
356 break;
357 case 32:
358 h = CRMFB_MODE_TYP_RGB8;
359 break;
360 default:
361 panic("Unsupported depth");
362 }
363 d = h << CRMFB_MODE_TYP_SHIFT;
364 d |= CRMFB_MODE_BUF_BOTH << CRMFB_MODE_BUF_SHIFT;
365 for (i = 0; i < (32 * 4); i += 4)
366 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_MODE + i, d);
367
368 /* setup tile pointer, but don't turn on DMA yet! */
369 h = DMAADDR(sc->sc_dmai);
370 d = (h >> 9) << CRMFB_FRM_CONTROL_TILEPTR_SHIFT;
371 //d &= ~(0 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
372 //d &= ~(0 << CRMFB_FRM_CONTROL_LINEAR_SHIFT);
373 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
374
375 /* init framebuffer width and pixel size */
376 d = (1 << CRMFB_FRM_TILESIZE_WIDTH_SHIFT);
377 switch (sc->sc_depth) {
378 case 8:
379 h = CRMFB_FRM_TILESIZE_DEPTH_8;
380 break;
381 case 16:
382 h = CRMFB_FRM_TILESIZE_DEPTH_16;
383 break;
384 case 32:
385 h = CRMFB_FRM_TILESIZE_DEPTH_32;
386 break;
387 default:
388 panic("Unsupported depth");
389 }
390 d |= (h << CRMFB_FRM_TILESIZE_DEPTH_SHIFT);
391 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_TILESIZE, d);
392
393 /* init framebuffer height, we use the trick that the Linux
394 * driver uses to fool the CRM out of tiled mode and into
395 * linear mode
396 */
397 h = sc->sc_width * sc->sc_height / (512 / (sc->sc_depth / 8));
398 d = h << CRMFB_FRM_PIXSIZE_HEIGHT_SHIFT;
399 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_PIXSIZE, d);
400
401 /* turn off firmware overlay and hardware cursor */
402 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_OVR_WIDTH_TILE, 0);
403 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CRS_CONTROL, 0);
404
405 /* enable drawing again */
406 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK);
407 d |= (1 << CRMFB_DOTCLOCK_CLKRUN_SHIFT);
408 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_DOTCLOCK, d);
409 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_VT_XY, 0);
410
411 /* turn on DMA for the framebuffer */
412 d = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL);
413 d |= (1 << CRMFB_FRM_CONTROL_DMAEN_SHIFT);
414 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_FRM_CONTROL, d);
415
416 for (i = 0; i < 256; i++) {
417 crmfb_set_palette(sc, i, rasops_cmap[(i * 3) + 2],
418 rasops_cmap[(i * 3) + 1], rasops_cmap[(i * 3) + 0]);
419 sc->sc_cmap_red[i] = rasops_cmap[(i * 3) + 2];
420 sc->sc_cmap_green[i] = rasops_cmap[(i * 3) + 1];
421 sc->sc_cmap_blue[i] = rasops_cmap[(i * 3) + 0];
422 }
423
424 wsdisplay_cnattach(&crmfb_defaultscreen, ri, 0, 0, defattr);
425
426 aa.console = 1;
427 aa.scrdata = &crmfb_screenlist;
428 aa.accessops = &crmfb_accessops;
429 aa.accesscookie = &sc->sc_vd;
430
431 config_found(self, &aa, wsemuldisplaydevprint);
432
433 return;
434 }
435
436 int
437 crmfb_probe(void)
438 {
439
440 if (mach_type != MACH_SGI_IP32)
441 return 0;
442
443 return 1;
444 }
445
446 static int
447 crmfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
448 {
449 struct vcons_data *vd;
450 struct crmfb_softc *sc;
451 struct vcons_screen *ms;
452 struct wsdisplay_fbinfo *wdf;
453 int nmode;
454
455 vd = (struct vcons_data *)v;
456 sc = (struct crmfb_softc *)vd->cookie;
457 ms = (struct vcons_screen *)vd->active;
458
459 switch (cmd) {
460 case WSDISPLAYIO_GTYPE:
461 /* not really, but who cares? */
462 *(u_int *)data = WSDISPLAY_TYPE_NEWPORT;
463 return 0;
464 case WSDISPLAYIO_GINFO:
465 if (vd->active != NULL) {
466 wdf = (void *)data;
467 wdf->height = sc->sc_height;
468 wdf->width = sc->sc_width;
469 wdf->depth = sc->sc_depth;
470 wdf->cmsize = 256;
471 return 0;
472 } else
473 return ENODEV;
474 case WSDISPLAYIO_GETCMAP:
475 if (sc->sc_depth == 8)
476 return crmfb_getcmap(sc, (struct wsdisplay_cmap *)data);
477 else
478 return EINVAL;
479 case WSDISPLAYIO_PUTCMAP:
480 if (sc->sc_depth == 8)
481 return crmfb_putcmap(sc, (struct wsdisplay_cmap *)data);
482 else
483 return EINVAL;
484 case WSDISPLAYIO_LINEBYTES:
485 *(u_int *)data = sc->sc_width * sc->sc_depth / 8;
486 return 0;
487 case WSDISPLAYIO_SMODE:
488 nmode = *(int *)data;
489 if (nmode != sc->sc_wsmode) {
490 sc->sc_wsmode = nmode;
491 if (nmode == WSDISPLAYIO_MODE_EMUL)
492 vcons_redraw_screen(vd->active);
493 }
494 return 0;
495 }
496
497 return EPASSTHROUGH;
498 }
499
500 static paddr_t
501 crmfb_mmap(void *v, void *vs, off_t offset, int prot)
502 {
503 struct vcons_data *vd;
504 struct crmfb_softc *sc;
505 paddr_t pa;
506
507 vd = (struct vcons_data *)v;
508 sc = (struct crmfb_softc *)vd->cookie;
509
510 if (offset >= 0 && offset < sc->sc_fbsize) {
511 pa = bus_dmamem_mmap(sc->sc_dmat, sc->sc_dma.segs,
512 sc->sc_dma.nsegs, offset, prot,
513 BUS_DMA_WAITOK | BUS_DMA_NOCACHE);
514 return pa;
515 }
516
517 return -1;
518 }
519
520 static void
521 crmfb_init_screen(void *c, struct vcons_screen *scr, int existing,
522 long *defattr)
523 {
524 struct crmfb_softc *sc;
525 struct rasops_info *ri;
526
527 sc = (struct crmfb_softc *)c;
528 ri = &scr->scr_ri;
529
530 ri->ri_flg = RI_CENTER;
531 ri->ri_depth = sc->sc_depth;
532 ri->ri_width = sc->sc_width;
533 ri->ri_height = sc->sc_height;
534 ri->ri_stride = ri->ri_width * (ri->ri_depth / 8);
535
536 switch (ri->ri_depth) {
537 case 16:
538 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 5;
539 ri->ri_rpos = 10;
540 ri->ri_gpos = 5;
541 ri->ri_bpos = 0;
542 break;
543 case 32:
544 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
545 ri->ri_rpos = 8;
546 ri->ri_gpos = 16;
547 ri->ri_bpos = 24;
548 break;
549 }
550
551 ri->ri_bits = KERNADDR(sc->sc_dma);
552
553 if (existing)
554 ri->ri_flg |= RI_CLEAR;
555
556 rasops_init(ri, ri->ri_height / 16, ri->ri_width / 8);
557 ri->ri_caps = WSSCREEN_WSCOLORS;
558 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
559 ri->ri_width / ri->ri_font->fontwidth);
560 ri->ri_hw = scr;
561
562 return;
563 }
564
565 static int
566 crmfb_putcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
567 {
568 u_int idx, cnt;
569 u_char r[256], g[256], b[256];
570 u_char *rp, *gp, *bp;
571 int rv, i;
572
573 idx = cm->index;
574 cnt = cm->count;
575
576 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
577 return EINVAL;
578
579 rv = copyin(cm->red, &r[idx], cnt);
580 if (rv)
581 return rv;
582 rv = copyin(cm->green, &g[idx], cnt);
583 if (rv)
584 return rv;
585 rv = copyin(cm->blue, &b[idx], cnt);
586 if (rv)
587 return rv;
588
589 memcpy(&sc->sc_cmap_red[idx], &r[idx], cnt);
590 memcpy(&sc->sc_cmap_green[idx], &g[idx], cnt);
591 memcpy(&sc->sc_cmap_blue[idx], &b[idx], cnt);
592
593 rp = &sc->sc_cmap_red[idx];
594 gp = &sc->sc_cmap_green[idx];
595 bp = &sc->sc_cmap_blue[idx];
596
597 for (i = 0; i < cnt; i++) {
598 crmfb_set_palette(sc, idx, *rp, *gp, *bp);
599 idx++;
600 rp++, gp++, bp++;
601 }
602
603 return 0;
604 }
605
606 static int
607 crmfb_getcmap(struct crmfb_softc *sc, struct wsdisplay_cmap *cm)
608 {
609 u_int idx, cnt;
610 int rv;
611
612 idx = cm->index;
613 cnt = cm->count;
614
615 if (idx >= 255 || cnt > 256 || idx + cnt > 256)
616 return EINVAL;
617
618 rv = copyout(&sc->sc_cmap_red[idx], cm->red, cnt);
619 if (rv)
620 return rv;
621 rv = copyout(&sc->sc_cmap_green[idx], cm->green, cnt);
622 if (rv)
623 return rv;
624 rv = copyout(&sc->sc_cmap_blue[idx], cm->blue, cnt);
625 if (rv)
626 return rv;
627
628 return 0;
629 }
630
631 static void
632 crmfb_set_palette(struct crmfb_softc *sc, int reg, uint8_t r, uint8_t g,
633 uint8_t b)
634 {
635 uint32_t val;
636
637 if (reg > 255 || sc->sc_depth != 8)
638 return;
639
640 while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP_FIFO) >= 63)
641 DELAY(10);
642
643 val = (r << 24) | (g << 16) | (b << 8);
644 bus_space_write_4(sc->sc_iot, sc->sc_ioh, CRMFB_CMAP + (reg * 4), val);
645
646 return;
647 }
648