]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/versatile/versatile_clcd.c
Merge ACPICA 20170531.
[FreeBSD/FreeBSD.git] / sys / arm / versatile / versatile_clcd.c
1 /*
2  * Copyright (c) 2012-2017 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37 #include <sys/fbio.h>
38 #include <sys/consio.h>
39 #include <sys/kdb.h>
40
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/intr.h>
44
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48
49 #include <dev/fb/fbreg.h>
50 #include <dev/syscons/syscons.h>
51
52 #include <arm/versatile/versatile_scm.h>
53
54 #include <machine/bus.h>
55
56 #define PL110_VENDOR_ARM926PXP  1
57
58 #define CLCD_MODE_RGB888        0x0
59 #define CLCD_MODE_RGB555        0x01
60 #define CLCD_MODE_RBG565        0x02
61 #define CLCD_MODE_RGB565        0x03
62
63 #define CLCDC_TIMING0           0x00
64 #define CLCDC_TIMING1           0x04
65 #define CLCDC_TIMING2           0x08
66 #define CLCDC_TIMING3           0x0C
67 #define CLCDC_TIMING3           0x0C
68 #define CLCDC_UPBASE            0x10
69 #define CLCDC_LPBASE            0x14
70 #ifdef PL110_VENDOR_ARM926PXP
71 #define CLCDC_CONTROL           0x18
72 #define CLCDC_IMSC              0x1C
73 #else
74 #define CLCDC_IMSC              0x18
75 #define CLCDC_CONTROL           0x1C
76 #endif
77 #define         CONTROL_WATERMARK       (1 << 16)
78 #define         CONTROL_VCOMP_VS        (0 << 12)
79 #define         CONTROL_VCOMP_BP        (1 << 12)
80 #define         CONTROL_VCOMP_SAV       (2 << 12)
81 #define         CONTROL_VCOMP_FP        (3 << 12)
82 #define         CONTROL_PWR             (1 << 11)
83 #define         CONTROL_BEPO            (1 << 10)
84 #define         CONTROL_BEBO            (1 << 9)
85 #define         CONTROL_BGR             (1 << 8)
86 #define         CONTROL_DUAL            (1 << 7)
87 #define         CONTROL_MONO8           (1 << 6)
88 #define         CONTROL_TFT             (1 << 5)
89 #define         CONTROL_BW              (1 << 4)
90 #define         CONTROL_BPP1            (0x00 << 1)
91 #define         CONTROL_BPP2            (0x01 << 1)
92 #define         CONTROL_BPP4            (0x02 << 1)
93 #define         CONTROL_BPP8            (0x03 << 1)
94 #define         CONTROL_BPP16           (0x04 << 1)
95 #define         CONTROL_BPP24           (0x05 << 1)
96 #define         CONTROL_EN      (1 << 0)
97 #define CLCDC_RIS               0x20
98 #define CLCDC_MIS               0x24
99 #define         INTR_MBERR              (1 << 4)
100 #define         INTR_VCOMP              (1 << 3)
101 #define         INTR_LNB                (1 << 2)
102 #define         INTR_FUF                (1 << 1)
103 #define CLCDC_ICR               0x28
104
105 #ifdef DEBUG
106 #define dprintf(fmt, args...) do { printf("%s(): ", __func__);   \
107     printf(fmt,##args); } while (0)
108 #else
109 #define dprintf(fmt, args...)
110 #endif
111
112 #define versatile_clcdc_read_4(sc, reg) \
113         bus_read_4((sc)->mem_res, (reg))
114 #define versatile_clcdc_write_4(sc, reg, val)   \
115         bus_write_4((sc)->mem_res, (reg), (val))
116
117 struct versatile_clcdc_softc {
118         struct resource*        mem_res;
119
120         struct mtx              mtx;
121
122         int                     width;
123         int                     height;
124         int                     mode;
125
126         bus_dma_tag_t           dma_tag;
127         bus_dmamap_t            dma_map;
128         bus_addr_t              fb_phys;
129         uint8_t                 *fb_base;
130
131 };
132
133 struct video_adapter_softc {
134         /* Videoadpater part */
135         video_adapter_t va;
136         int             console;
137
138         intptr_t        fb_addr;
139         unsigned int    fb_size;
140
141         unsigned int    height;
142         unsigned int    width;
143         unsigned int    depth;
144         unsigned int    stride;
145
146         unsigned int    xmargin;
147         unsigned int    ymargin;
148
149         unsigned char   *font;
150         int             initialized;
151 };
152
153 struct argb {
154         uint8_t         a;
155         uint8_t         r;
156         uint8_t         g;
157         uint8_t         b;
158 };
159
160 static struct argb versatilefb_palette[16] = {
161         {0x00, 0x00, 0x00, 0x00},
162         {0x00, 0x00, 0x00, 0xaa},
163         {0x00, 0x00, 0xaa, 0x00},
164         {0x00, 0x00, 0xaa, 0xaa},
165         {0x00, 0xaa, 0x00, 0x00},
166         {0x00, 0xaa, 0x00, 0xaa},
167         {0x00, 0xaa, 0x55, 0x00},
168         {0x00, 0xaa, 0xaa, 0xaa},
169         {0x00, 0x55, 0x55, 0x55},
170         {0x00, 0x55, 0x55, 0xff},
171         {0x00, 0x55, 0xff, 0x55},
172         {0x00, 0x55, 0xff, 0xff},
173         {0x00, 0xff, 0x55, 0x55},
174         {0x00, 0xff, 0x55, 0xff},
175         {0x00, 0xff, 0xff, 0x55},
176         {0x00, 0xff, 0xff, 0xff}
177 };
178
179 /* mouse pointer from dev/syscons/scgfbrndr.c */
180 static u_char mouse_pointer[16] = {
181         0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
182         0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
183 };
184
185 #define FB_WIDTH                640
186 #define FB_HEIGHT               480
187 #define FB_DEPTH                16
188
189 #define VERSATILE_FONT_HEIGHT   16
190
191 static struct video_adapter_softc va_softc;
192
193 static int versatilefb_configure(int);
194 static void versatilefb_update_margins(video_adapter_t *adp);
195
196 static void
197 versatile_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
198 {
199         bus_addr_t *addr;
200
201         if (err)
202                 return;
203
204         addr = (bus_addr_t*)arg;
205         *addr = segs[0].ds_addr;
206 }
207
208 static int
209 versatile_clcdc_probe(device_t dev)
210 {
211
212         if (!ofw_bus_status_okay(dev))
213                 return (ENXIO);
214
215         if (ofw_bus_is_compatible(dev, "arm,pl110")) {
216                 device_set_desc(dev, "PL110 CLCD controller");
217                 return (BUS_PROBE_DEFAULT);
218         }
219
220         return (ENXIO);
221 }
222
223 static int
224 versatile_clcdc_attach(device_t dev)
225 {
226         struct versatile_clcdc_softc *sc = device_get_softc(dev);
227         struct video_adapter_softc *va_sc = &va_softc;
228         int err, rid;
229         uint32_t reg;
230         int clcdid;
231         int dma_size;
232
233         /* Request memory resources */
234         rid = 0;
235         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
236         if (sc->mem_res == NULL) {
237                 device_printf(dev, "could not allocate memory resources\n");
238                 return (ENXIO);
239         }
240
241         err = versatile_scm_reg_read_4(SCM_CLCD, &reg);
242         if (err) {
243                 device_printf(dev, "failed to read SCM register\n");
244                 goto fail;
245         }
246         clcdid = (reg >> SCM_CLCD_CLCDID_SHIFT) & SCM_CLCD_CLCDID_MASK;
247         switch (clcdid) {
248                 case 31:
249                         device_printf(dev, "QEMU VGA 640x480\n");
250                         sc->width = 640;
251                         sc->height = 480;
252                         break;
253                 default:
254                         device_printf(dev, "Unsupported: %d\n", clcdid);
255                         goto fail;
256         }
257
258         reg &= ~SCM_CLCD_LCD_MODE_MASK;
259         reg |= CLCD_MODE_RGB565;
260         sc->mode = CLCD_MODE_RGB565;
261         versatile_scm_reg_write_4(SCM_CLCD, reg);
262         dma_size = sc->width*sc->height*2;
263  
264         /*
265          * Power on LCD
266          */
267         reg |= SCM_CLCD_PWR3V5VSWITCH | SCM_CLCD_NLCDIOON;
268         versatile_scm_reg_write_4(SCM_CLCD, reg);
269
270         /*
271          * XXX: hardcoded timing for VGA. For other modes/panels
272          * we need to keep table of timing register values
273          */
274         /*
275          * XXX: set SYS_OSC1 
276          */
277         versatile_clcdc_write_4(sc, CLCDC_TIMING0, 0x3F1F3F9C);
278         versatile_clcdc_write_4(sc, CLCDC_TIMING1, 0x090B61DF);
279         versatile_clcdc_write_4(sc, CLCDC_TIMING2, 0x067F1800);
280         /* XXX: timing 3? */
281
282         /*
283          * Now allocate framebuffer memory
284          */
285         err = bus_dma_tag_create(
286             bus_get_dma_tag(dev),
287             4, 0,               /* alignment, boundary */
288             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
289             BUS_SPACE_MAXADDR,          /* highaddr */
290             NULL, NULL,                 /* filter, filterarg */
291             dma_size, 1,                /* maxsize, nsegments */
292             dma_size, 0,                /* maxsegsize, flags */
293             NULL, NULL,                 /* lockfunc, lockarg */
294             &sc->dma_tag);
295
296         err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_base,
297             0, &sc->dma_map);
298         if (err) {
299                 device_printf(dev, "cannot allocate framebuffer\n");
300                 goto fail;
301         }
302
303         err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_base,
304             dma_size, versatile_fb_dmamap_cb, &sc->fb_phys, BUS_DMA_NOWAIT);
305
306         if (err) {
307                 device_printf(dev, "cannot load DMA map\n");
308                 goto fail;
309         }
310
311         /* Make sure it's blank */
312         memset(sc->fb_base, 0x00, dma_size);
313
314         versatile_clcdc_write_4(sc, CLCDC_UPBASE, sc->fb_phys);
315
316         err = (sc_attach_unit(device_get_unit(dev),
317             device_get_flags(dev) | SC_AUTODETECT_KBD));
318
319         if (err) {
320                 device_printf(dev, "failed to attach syscons\n");
321                 goto fail;
322         }
323
324         /*
325          * XXX: hardcoded for VGA
326          */
327         reg = CONTROL_VCOMP_BP | CONTROL_TFT | CONTROL_BGR | CONTROL_EN;
328         reg |= CONTROL_BPP16;
329         versatile_clcdc_write_4(sc, CLCDC_CONTROL, reg);
330         DELAY(20);
331         reg |= CONTROL_PWR;
332         versatile_clcdc_write_4(sc, CLCDC_CONTROL, reg);
333
334         va_sc->fb_addr = (vm_offset_t)sc->fb_base;
335         va_sc->fb_size = dma_size;
336         va_sc->width = sc->width;
337         va_sc->height = sc->height;
338         va_sc->depth = 16;
339         va_sc->stride = sc->width * 2;
340         versatilefb_update_margins(&va_sc->va);
341
342         return (0);
343
344 fail:
345         if (sc->fb_base)
346                 bus_dmamem_free(sc->dma_tag, sc->fb_base, sc->dma_map);
347         if (sc->dma_tag)
348                 bus_dma_tag_destroy(sc->dma_tag);
349         return (err);
350 }
351
352 static device_method_t versatile_clcdc_methods[] = {
353         DEVMETHOD(device_probe,         versatile_clcdc_probe),
354         DEVMETHOD(device_attach,        versatile_clcdc_attach),
355
356         DEVMETHOD_END
357 };
358
359 static driver_t versatile_clcdc_driver = {
360         "clcdc",
361         versatile_clcdc_methods,
362         sizeof(struct versatile_clcdc_softc),
363 };
364
365 static devclass_t versatile_clcdc_devclass;
366
367 DRIVER_MODULE(versatile_clcdc, simplebus, versatile_clcdc_driver, versatile_clcdc_devclass, 0, 0);
368
369 /*
370  * Video driver routines and glue.
371  */
372 static vi_probe_t               versatilefb_probe;
373 static vi_init_t                versatilefb_init;
374 static vi_get_info_t            versatilefb_get_info;
375 static vi_query_mode_t          versatilefb_query_mode;
376 static vi_set_mode_t            versatilefb_set_mode;
377 static vi_save_font_t           versatilefb_save_font;
378 static vi_load_font_t           versatilefb_load_font;
379 static vi_show_font_t           versatilefb_show_font;
380 static vi_save_palette_t        versatilefb_save_palette;
381 static vi_load_palette_t        versatilefb_load_palette;
382 static vi_set_border_t          versatilefb_set_border;
383 static vi_save_state_t          versatilefb_save_state;
384 static vi_load_state_t          versatilefb_load_state;
385 static vi_set_win_org_t         versatilefb_set_win_org;
386 static vi_read_hw_cursor_t      versatilefb_read_hw_cursor;
387 static vi_set_hw_cursor_t       versatilefb_set_hw_cursor;
388 static vi_set_hw_cursor_shape_t versatilefb_set_hw_cursor_shape;
389 static vi_blank_display_t       versatilefb_blank_display;
390 static vi_mmap_t                versatilefb_mmap;
391 static vi_ioctl_t               versatilefb_ioctl;
392 static vi_clear_t               versatilefb_clear;
393 static vi_fill_rect_t           versatilefb_fill_rect;
394 static vi_bitblt_t              versatilefb_bitblt;
395 static vi_diag_t                versatilefb_diag;
396 static vi_save_cursor_palette_t versatilefb_save_cursor_palette;
397 static vi_load_cursor_palette_t versatilefb_load_cursor_palette;
398 static vi_copy_t                versatilefb_copy;
399 static vi_putp_t                versatilefb_putp;
400 static vi_putc_t                versatilefb_putc;
401 static vi_puts_t                versatilefb_puts;
402 static vi_putm_t                versatilefb_putm;
403
404 static video_switch_t versatilefbvidsw = {
405         .probe                  = versatilefb_probe,
406         .init                   = versatilefb_init,
407         .get_info               = versatilefb_get_info,
408         .query_mode             = versatilefb_query_mode,
409         .set_mode               = versatilefb_set_mode,
410         .save_font              = versatilefb_save_font,
411         .load_font              = versatilefb_load_font,
412         .show_font              = versatilefb_show_font,
413         .save_palette           = versatilefb_save_palette,
414         .load_palette           = versatilefb_load_palette,
415         .set_border             = versatilefb_set_border,
416         .save_state             = versatilefb_save_state,
417         .load_state             = versatilefb_load_state,
418         .set_win_org            = versatilefb_set_win_org,
419         .read_hw_cursor         = versatilefb_read_hw_cursor,
420         .set_hw_cursor          = versatilefb_set_hw_cursor,
421         .set_hw_cursor_shape    = versatilefb_set_hw_cursor_shape,
422         .blank_display          = versatilefb_blank_display,
423         .mmap                   = versatilefb_mmap,
424         .ioctl                  = versatilefb_ioctl,
425         .clear                  = versatilefb_clear,
426         .fill_rect              = versatilefb_fill_rect,
427         .bitblt                 = versatilefb_bitblt,
428         .diag                   = versatilefb_diag,
429         .save_cursor_palette    = versatilefb_save_cursor_palette,
430         .load_cursor_palette    = versatilefb_load_cursor_palette,
431         .copy                   = versatilefb_copy,
432         .putp                   = versatilefb_putp,
433         .putc                   = versatilefb_putc,
434         .puts                   = versatilefb_puts,
435         .putm                   = versatilefb_putm,
436 };
437
438 VIDEO_DRIVER(versatilefb, versatilefbvidsw, versatilefb_configure);
439
440 static vr_init_t clcdr_init;
441 static vr_clear_t clcdr_clear;
442 static vr_draw_border_t clcdr_draw_border;
443 static vr_draw_t clcdr_draw;
444 static vr_set_cursor_t clcdr_set_cursor;
445 static vr_draw_cursor_t clcdr_draw_cursor;
446 static vr_blink_cursor_t clcdr_blink_cursor;
447 static vr_set_mouse_t clcdr_set_mouse;
448 static vr_draw_mouse_t clcdr_draw_mouse;
449
450 /*
451  * We use our own renderer; this is because we must emulate a hardware
452  * cursor.
453  */
454 static sc_rndr_sw_t clcdrend = {
455         clcdr_init,
456         clcdr_clear,
457         clcdr_draw_border,
458         clcdr_draw,
459         clcdr_set_cursor,
460         clcdr_draw_cursor,
461         clcdr_blink_cursor,
462         clcdr_set_mouse,
463         clcdr_draw_mouse
464 };
465
466 RENDERER(versatilefb, 0, clcdrend, gfb_set);
467 RENDERER_MODULE(versatilefb, gfb_set);
468
469 static void
470 clcdr_init(scr_stat* scp)
471 {
472 }
473
474 static void
475 clcdr_clear(scr_stat* scp, int c, int attr)
476 {
477 }
478
479 static void
480 clcdr_draw_border(scr_stat* scp, int color)
481 {
482 }
483
484 static void
485 clcdr_draw(scr_stat* scp, int from, int count, int flip)
486 {
487         video_adapter_t* adp = scp->sc->adp;
488         int i, c, a;
489
490         if (!flip) {
491                 /* Normal printing */
492                 vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
493         } else {        
494                 /* This is for selections and such: invert the color attribute */
495                 for (i = count; i-- > 0; ++from) {
496                         c = sc_vtb_getc(&scp->vtb, from);
497                         a = sc_vtb_geta(&scp->vtb, from) >> 8;
498                         vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
499                 }
500         }
501 }
502
503 static void
504 clcdr_set_cursor(scr_stat* scp, int base, int height, int blink)
505 {
506 }
507
508 static void
509 clcdr_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
510 {
511         video_adapter_t* adp = scp->sc->adp;
512         struct video_adapter_softc *sc;
513         int row, col;
514         uint8_t *addr;
515         int i,j;
516
517         sc = (struct video_adapter_softc *)adp;
518
519         if (scp->curs_attr.height <= 0)
520                 return;
521
522         if (sc->fb_addr == 0)
523                 return;
524
525         if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
526                 return;
527
528         /* calculate the coordinates in the video buffer */
529         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
530         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
531
532         addr = (uint8_t *)sc->fb_addr
533             + (row + sc->ymargin)*(sc->stride)
534             + (sc->depth/8) * (col + sc->xmargin);
535
536         /* our cursor consists of simply inverting the char under it */
537         for (i = 0; i < adp->va_info.vi_cheight; i++) {
538                 for (j = 0; j < adp->va_info.vi_cwidth; j++) {
539
540                         addr[2*j] ^= 0xff;
541                         addr[2*j + 1] ^= 0xff;
542                 }
543
544                 addr += sc->stride;
545         }
546 }
547
548 static void
549 clcdr_blink_cursor(scr_stat* scp, int at, int flip)
550 {
551 }
552
553 static void
554 clcdr_set_mouse(scr_stat* scp)
555 {
556 }
557
558 static void
559 clcdr_draw_mouse(scr_stat* scp, int x, int y, int on)
560 {
561         vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
562
563 }
564
565 static uint16_t versatilefb_static_window[ROW*COL];
566 extern u_char dflt_font_16[];
567
568 /*
569  * Update videoadapter settings after changing resolution
570  */
571 static void
572 versatilefb_update_margins(video_adapter_t *adp)
573 {
574         struct video_adapter_softc *sc;
575         video_info_t *vi;
576
577         sc = (struct video_adapter_softc *)adp;
578         vi = &adp->va_info;
579
580         sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
581         sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
582 }
583
584 static int
585 versatilefb_configure(int flags)
586 {
587         struct video_adapter_softc *va_sc;
588
589         va_sc = &va_softc;
590
591         if (va_sc->initialized)
592                 return (0);
593
594         va_sc->width = FB_WIDTH;
595         va_sc->height = FB_HEIGHT;
596         va_sc->depth = FB_DEPTH;
597
598         versatilefb_init(0, &va_sc->va, 0);
599
600         va_sc->initialized = 1;
601
602         return (0);
603 }
604
605 static int
606 versatilefb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
607 {
608
609         return (0);
610 }
611
612 static int
613 versatilefb_init(int unit, video_adapter_t *adp, int flags)
614 {
615         struct video_adapter_softc *sc;
616         video_info_t *vi;
617
618         sc = (struct video_adapter_softc *)adp;
619         vi = &adp->va_info;
620
621         vid_init_struct(adp, "versatilefb", -1, unit);
622
623         sc->font = dflt_font_16;
624         vi->vi_cheight = VERSATILE_FONT_HEIGHT;
625         vi->vi_cwidth = 8;
626
627         vi->vi_width = sc->width/8;
628         vi->vi_height = sc->height/vi->vi_cheight;
629
630         /*
631          * Clamp width/height to syscons maximums
632          */
633         if (vi->vi_width > COL)
634                 vi->vi_width = COL;
635         if (vi->vi_height > ROW)
636                 vi->vi_height = ROW;
637
638         sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
639         sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
640
641         adp->va_window = (vm_offset_t) versatilefb_static_window;
642         adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
643
644         vid_register(&sc->va);
645
646         return (0);
647 }
648
649 static int
650 versatilefb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
651 {
652         bcopy(&adp->va_info, info, sizeof(*info));
653         return (0);
654 }
655
656 static int
657 versatilefb_query_mode(video_adapter_t *adp, video_info_t *info)
658 {
659         return (0);
660 }
661
662 static int
663 versatilefb_set_mode(video_adapter_t *adp, int mode)
664 {
665         return (0);
666 }
667
668 static int
669 versatilefb_save_font(video_adapter_t *adp, int page, int size, int width,
670     u_char *data, int c, int count)
671 {
672         return (0);
673 }
674
675 static int
676 versatilefb_load_font(video_adapter_t *adp, int page, int size, int width,
677     u_char *data, int c, int count)
678 {
679         struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;
680
681         sc->font = data;
682
683         return (0);
684 }
685
686 static int
687 versatilefb_show_font(video_adapter_t *adp, int page)
688 {
689         return (0);
690 }
691
692 static int
693 versatilefb_save_palette(video_adapter_t *adp, u_char *palette)
694 {
695         return (0);
696 }
697
698 static int
699 versatilefb_load_palette(video_adapter_t *adp, u_char *palette)
700 {
701         return (0);
702 }
703
704 static int
705 versatilefb_set_border(video_adapter_t *adp, int border)
706 {
707         return (versatilefb_blank_display(adp, border));
708 }
709
710 static int
711 versatilefb_save_state(video_adapter_t *adp, void *p, size_t size)
712 {
713         return (0);
714 }
715
716 static int
717 versatilefb_load_state(video_adapter_t *adp, void *p)
718 {
719         return (0);
720 }
721
722 static int
723 versatilefb_set_win_org(video_adapter_t *adp, off_t offset)
724 {
725         return (0);
726 }
727
728 static int
729 versatilefb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
730 {
731         *col = *row = 0;
732
733         return (0);
734 }
735
736 static int
737 versatilefb_set_hw_cursor(video_adapter_t *adp, int col, int row)
738 {
739
740         return (0);
741 }
742
743 static int
744 versatilefb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
745     int celsize, int blink)
746 {
747         return (0);
748 }
749
750 static int
751 versatilefb_blank_display(video_adapter_t *adp, int mode)
752 {
753
754         struct video_adapter_softc *sc;
755
756         sc = (struct video_adapter_softc *)adp;
757         if (sc && sc->fb_addr)
758                 memset((void*)sc->fb_addr, 0, sc->fb_size);
759
760         return (0);
761 }
762
763 static int
764 versatilefb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
765     int prot, vm_memattr_t *memattr)
766 {
767         struct video_adapter_softc *sc;
768
769         sc = (struct video_adapter_softc *)adp;
770
771         /*
772          * This might be a legacy VGA mem request: if so, just point it at the
773          * framebuffer, since it shouldn't be touched
774          */
775         if (offset < sc->stride*sc->height) {
776                 *paddr = sc->fb_addr + offset;
777                 return (0);
778         }
779
780         return (EINVAL);
781 }
782
783 static int
784 versatilefb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
785 {
786
787         return (0);
788 }
789
790 static int
791 versatilefb_clear(video_adapter_t *adp)
792 {
793
794         return (versatilefb_blank_display(adp, 0));
795 }
796
797 static int
798 versatilefb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
799 {
800
801         return (0);
802 }
803
804 static int
805 versatilefb_bitblt(video_adapter_t *adp, ...)
806 {
807
808         return (0);
809 }
810
811 static int
812 versatilefb_diag(video_adapter_t *adp, int level)
813 {
814
815         return (0);
816 }
817
818 static int
819 versatilefb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
820 {
821
822         return (0);
823 }
824
825 static int
826 versatilefb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
827 {
828
829         return (0);
830 }
831
832 static int
833 versatilefb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
834 {
835
836         return (0);
837 }
838
839 static int
840 versatilefb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
841     int size, int bpp, int bit_ltor, int byte_ltor)
842 {
843
844         return (0);
845 }
846
847 static int
848 versatilefb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
849 {
850         struct video_adapter_softc *sc;
851         int row;
852         int col;
853         int i, j, k;
854         uint8_t *addr;
855         u_char *p;
856         uint8_t fg, bg, color;
857         uint16_t rgb;
858
859         sc = (struct video_adapter_softc *)adp;
860
861         if (sc->fb_addr == 0)
862                 return (0);
863
864         if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
865                 return (0);
866
867         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
868         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
869         p = sc->font + c*VERSATILE_FONT_HEIGHT;
870         addr = (uint8_t *)sc->fb_addr
871             + (row + sc->ymargin)*(sc->stride)
872             + (sc->depth/8) * (col + sc->xmargin);
873
874         fg = a & 0xf ;
875         bg = (a >> 4) & 0xf;
876
877         for (i = 0; i < VERSATILE_FONT_HEIGHT; i++) {
878                 for (j = 0, k = 7; j < 8; j++, k--) {
879                         if ((p[i] & (1 << k)) == 0)
880                                 color = bg;
881                         else
882                                 color = fg;
883
884                         switch (sc->depth) {
885                         case 16:
886                                 rgb = (versatilefb_palette[color].r >> 3) << 11;
887                                 rgb |= (versatilefb_palette[color].g >> 2) << 5;
888                                 rgb |= (versatilefb_palette[color].b >> 3);
889                                 addr[2*j] = rgb & 0xff;
890                                 addr[2*j + 1] = (rgb >> 8) & 0xff;
891                         default:
892                                 /* Not supported yet */
893                                 break;
894                         }
895                 }
896
897                 addr += (sc->stride);
898         }
899
900         return (0);
901 }
902
903 static int
904 versatilefb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
905 {
906         int i;
907
908         for (i = 0; i < len; i++) 
909                 versatilefb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
910
911         return (0);
912 }
913
914 static int
915 versatilefb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
916     uint32_t pixel_mask, int size, int width)
917 {
918
919         return (0);
920 }
921
922 /*
923  * Define a stub keyboard driver in case one hasn't been
924  * compiled into the kernel
925  */
926 #include <sys/kbio.h>
927 #include <dev/kbd/kbdreg.h>
928
929 static int dummy_kbd_configure(int flags);
930
931 keyboard_switch_t bcmdummysw;
932
933 static int
934 dummy_kbd_configure(int flags)
935 {
936
937         return (0);
938 }
939 KEYBOARD_DRIVER(bcmdummy, bcmdummysw, dummy_kbd_configure);