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