]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fb/creator.c
Include eventhandler.h in more compilation units
[FreeBSD/FreeBSD.git] / sys / dev / fb / creator.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Jake Burkholder.
5  * Copyright (c) 2005 - 2006 Marius Strobl <marius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/consio.h>
38 #include <sys/eventhandler.h>
39 #include <sys/fbio.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/resource.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/openfirm.h>
46
47 #include <machine/bus.h>
48 #include <machine/bus_private.h>
49 #include <machine/ofw_machdep.h>
50 #include <machine/resource.h>
51 #include <machine/sc_machdep.h>
52
53 #include <sys/rman.h>
54
55 #include <dev/fb/fbreg.h>
56 #include <dev/fb/creatorreg.h>
57 #include <dev/fb/gfb.h>
58 #include <dev/syscons/syscons.h>
59
60 #define CREATOR_DRIVER_NAME     "creator"
61
62 struct creator_softc {
63         video_adapter_t         sc_va;                  /* XXX must be first */
64
65         phandle_t               sc_node;
66
67         struct cdev             *sc_si;
68
69         struct resource         *sc_reg[FFB_NREG];
70         bus_space_tag_t         sc_bt[FFB_NREG];
71         bus_space_handle_t      sc_bh[FFB_NREG];
72         u_long                  sc_reg_size;
73
74         u_int                   sc_height;
75         u_int                   sc_width;
76
77         u_int                   sc_xmargin;
78         u_int                   sc_ymargin;
79
80         const u_char            *sc_font;
81
82         int                     sc_bg_cache;
83         int                     sc_fg_cache;
84         int                     sc_fifo_cache;
85         int                     sc_fontinc_cache;
86         int                     sc_fontw_cache;
87         int                     sc_pmask_cache;
88
89         u_int                   sc_flags;
90 #define CREATOR_AFB             (1 << 0)
91 #define CREATOR_CONSOLE         (1 << 1)
92 #define CREATOR_CUREN           (1 << 2)
93 #define CREATOR_CURINV          (1 << 3)
94 #define CREATOR_PAC1            (1 << 4)
95 };
96
97 #define FFB_READ(sc, reg, off)                                          \
98         bus_space_read_4((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off))
99 #define FFB_WRITE(sc, reg, off, val)                                    \
100         bus_space_write_4((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val))
101
102 #define C(r, g, b)      ((b << 16) | (g << 8) | (r))
103 static const uint32_t creator_cmap[] = {
104         C(0x00, 0x00, 0x00),            /* black */
105         C(0x00, 0x00, 0xff),            /* blue */
106         C(0x00, 0xff, 0x00),            /* green */
107         C(0x00, 0xc0, 0xc0),            /* cyan */
108         C(0xff, 0x00, 0x00),            /* red */
109         C(0xc0, 0x00, 0xc0),            /* magenta */
110         C(0xc0, 0xc0, 0x00),            /* brown */
111         C(0xc0, 0xc0, 0xc0),            /* light grey */
112         C(0x80, 0x80, 0x80),            /* dark grey */
113         C(0x80, 0x80, 0xff),            /* light blue */
114         C(0x80, 0xff, 0x80),            /* light green */
115         C(0x80, 0xff, 0xff),            /* light cyan */
116         C(0xff, 0x80, 0x80),            /* light red */
117         C(0xff, 0x80, 0xff),            /* light magenta */
118         C(0xff, 0xff, 0x80),            /* yellow */
119         C(0xff, 0xff, 0xff),            /* white */
120 };
121 #undef C
122
123 static const struct {
124         vm_offset_t virt;
125         vm_paddr_t phys;
126         vm_size_t size;
127 } creator_fb_map[] = {
128         { FFB_VIRT_SFB8R,       FFB_PHYS_SFB8R,         FFB_SIZE_SFB8R },
129         { FFB_VIRT_SFB8G,       FFB_PHYS_SFB8G,         FFB_SIZE_SFB8G },
130         { FFB_VIRT_SFB8B,       FFB_PHYS_SFB8B,         FFB_SIZE_SFB8B },
131         { FFB_VIRT_SFB8X,       FFB_PHYS_SFB8X,         FFB_SIZE_SFB8X },
132         { FFB_VIRT_SFB32,       FFB_PHYS_SFB32,         FFB_SIZE_SFB32 },
133         { FFB_VIRT_SFB64,       FFB_PHYS_SFB64,         FFB_SIZE_SFB64 },
134         { FFB_VIRT_FBC,         FFB_PHYS_FBC,           FFB_SIZE_FBC },
135         { FFB_VIRT_FBC_BM,      FFB_PHYS_FBC_BM,        FFB_SIZE_FBC_BM },
136         { FFB_VIRT_DFB8R,       FFB_PHYS_DFB8R,         FFB_SIZE_DFB8R },
137         { FFB_VIRT_DFB8G,       FFB_PHYS_DFB8G,         FFB_SIZE_DFB8G },
138         { FFB_VIRT_DFB8B,       FFB_PHYS_DFB8B,         FFB_SIZE_DFB8B },
139         { FFB_VIRT_DFB8X,       FFB_PHYS_DFB8X,         FFB_SIZE_DFB8X },
140         { FFB_VIRT_DFB24,       FFB_PHYS_DFB24,         FFB_SIZE_DFB24 },
141         { FFB_VIRT_DFB32,       FFB_PHYS_DFB32,         FFB_SIZE_DFB32 },
142         { FFB_VIRT_DFB422A,     FFB_PHYS_DFB422A,       FFB_SIZE_DFB422A },
143         { FFB_VIRT_DFB422AD,    FFB_PHYS_DFB422AD,      FFB_SIZE_DFB422AD },
144         { FFB_VIRT_DFB24B,      FFB_PHYS_DFB24B,        FFB_SIZE_DFB24B },
145         { FFB_VIRT_DFB422B,     FFB_PHYS_DFB422B,       FFB_SIZE_DFB422B },
146         { FFB_VIRT_DFB422BD,    FFB_PHYS_DFB422BD,      FFB_SIZE_DFB422BD },
147         { FFB_VIRT_SFB16Z,      FFB_PHYS_SFB16Z,        FFB_SIZE_SFB16Z },
148         { FFB_VIRT_SFB8Z,       FFB_PHYS_SFB8Z,         FFB_SIZE_SFB8Z },
149         { FFB_VIRT_SFB422,      FFB_PHYS_SFB422,        FFB_SIZE_SFB422 },
150         { FFB_VIRT_SFB422D,     FFB_PHYS_SFB422D,       FFB_SIZE_SFB422D },
151         { FFB_VIRT_FBC_KREG,    FFB_PHYS_FBC_KREG,      FFB_SIZE_FBC_KREG },
152         { FFB_VIRT_DAC,         FFB_PHYS_DAC,           FFB_SIZE_DAC },
153         { FFB_VIRT_PROM,        FFB_PHYS_PROM,          FFB_SIZE_PROM },
154         { FFB_VIRT_EXP,         FFB_PHYS_EXP,           FFB_SIZE_EXP },
155 };
156
157 #define CREATOR_FB_MAP_SIZE     nitems(creator_fb_map)
158
159 extern const struct gfb_font gallant12x22;
160
161 static struct creator_softc creator_softc;
162 static struct bus_space_tag creator_bst_store[FFB_FBC];
163
164 static device_probe_t creator_bus_probe;
165 static device_attach_t creator_bus_attach;
166
167 static device_method_t creator_bus_methods[] = {
168         DEVMETHOD(device_probe,         creator_bus_probe),
169         DEVMETHOD(device_attach,        creator_bus_attach),
170
171         { 0, 0 }
172 };
173
174 static devclass_t creator_devclass;
175
176 DEFINE_CLASS_0(creator, creator_bus_driver, creator_bus_methods,
177     sizeof(struct creator_softc));
178 DRIVER_MODULE(creator, nexus, creator_bus_driver, creator_devclass, 0, 0);
179 DRIVER_MODULE(creator, upa, creator_bus_driver, creator_devclass, 0, 0);
180
181 static d_open_t creator_fb_open;
182 static d_close_t creator_fb_close;
183 static d_ioctl_t creator_fb_ioctl;
184 static d_mmap_t creator_fb_mmap;
185
186 static struct cdevsw creator_fb_devsw = {
187         .d_version =    D_VERSION,
188         .d_flags =      D_NEEDGIANT,
189         .d_open =       creator_fb_open,
190         .d_close =      creator_fb_close,
191         .d_ioctl =      creator_fb_ioctl,
192         .d_mmap =       creator_fb_mmap,
193         .d_name =       "fb",
194 };
195
196 static void creator_cursor_enable(struct creator_softc *sc, int onoff);
197 static void creator_cursor_install(struct creator_softc *sc);
198 static void creator_shutdown(void *xsc);
199
200 static int creator_configure(int flags);
201
202 static vi_probe_t creator_probe;
203 static vi_init_t creator_init;
204 static vi_get_info_t creator_get_info;
205 static vi_query_mode_t creator_query_mode;
206 static vi_set_mode_t creator_set_mode;
207 static vi_save_font_t creator_save_font;
208 static vi_load_font_t creator_load_font;
209 static vi_show_font_t creator_show_font;
210 static vi_save_palette_t creator_save_palette;
211 static vi_load_palette_t creator_load_palette;
212 static vi_set_border_t creator_set_border;
213 static vi_save_state_t creator_save_state;
214 static vi_load_state_t creator_load_state;
215 static vi_set_win_org_t creator_set_win_org;
216 static vi_read_hw_cursor_t creator_read_hw_cursor;
217 static vi_set_hw_cursor_t creator_set_hw_cursor;
218 static vi_set_hw_cursor_shape_t creator_set_hw_cursor_shape;
219 static vi_blank_display_t creator_blank_display;
220 static vi_mmap_t creator_mmap;
221 static vi_ioctl_t creator_ioctl;
222 static vi_clear_t creator_clear;
223 static vi_fill_rect_t creator_fill_rect;
224 static vi_bitblt_t creator_bitblt;
225 static vi_diag_t creator_diag;
226 static vi_save_cursor_palette_t creator_save_cursor_palette;
227 static vi_load_cursor_palette_t creator_load_cursor_palette;
228 static vi_copy_t creator_copy;
229 static vi_putp_t creator_putp;
230 static vi_putc_t creator_putc;
231 static vi_puts_t creator_puts;
232 static vi_putm_t creator_putm;
233
234 static video_switch_t creatorvidsw = {
235         .probe                  = creator_probe,
236         .init                   = creator_init,
237         .get_info               = creator_get_info,
238         .query_mode             = creator_query_mode,
239         .set_mode               = creator_set_mode,
240         .save_font              = creator_save_font,
241         .load_font              = creator_load_font,
242         .show_font              = creator_show_font,
243         .save_palette           = creator_save_palette,
244         .load_palette           = creator_load_palette,
245         .set_border             = creator_set_border,
246         .save_state             = creator_save_state,
247         .load_state             = creator_load_state,
248         .set_win_org            = creator_set_win_org,
249         .read_hw_cursor         = creator_read_hw_cursor,
250         .set_hw_cursor          = creator_set_hw_cursor,
251         .set_hw_cursor_shape    = creator_set_hw_cursor_shape,
252         .blank_display          = creator_blank_display,
253         .mmap                   = creator_mmap,
254         .ioctl                  = creator_ioctl,
255         .clear                  = creator_clear,
256         .fill_rect              = creator_fill_rect,
257         .bitblt                 = creator_bitblt,
258         .diag                   = creator_diag,
259         .save_cursor_palette    = creator_save_cursor_palette,
260         .load_cursor_palette    = creator_load_cursor_palette,
261         .copy                   = creator_copy,
262         .putp                   = creator_putp,
263         .putc                   = creator_putc,
264         .puts                   = creator_puts,
265         .putm                   = creator_putm
266 };
267
268 VIDEO_DRIVER(creator, creatorvidsw, creator_configure);
269
270 extern sc_rndr_sw_t txtrndrsw;
271 RENDERER(creator, 0, txtrndrsw, gfb_set);
272
273 RENDERER_MODULE(creator, gfb_set);
274
275 static const u_char creator_mouse_pointer[64][8] __aligned(8) = {
276         { 0x00, 0x00, },        /* ............ */
277         { 0x80, 0x00, },        /* *........... */
278         { 0xc0, 0x00, },        /* **.......... */
279         { 0xe0, 0x00, },        /* ***......... */
280         { 0xf0, 0x00, },        /* ****........ */
281         { 0xf8, 0x00, },        /* *****....... */
282         { 0xfc, 0x00, },        /* ******...... */
283         { 0xfe, 0x00, },        /* *******..... */
284         { 0xff, 0x00, },        /* ********.... */
285         { 0xff, 0x80, },        /* *********... */
286         { 0xfc, 0xc0, },        /* ******..**.. */
287         { 0xdc, 0x00, },        /* **.***...... */
288         { 0x8e, 0x00, },        /* *...***..... */
289         { 0x0e, 0x00, },        /* ....***..... */
290         { 0x07, 0x00, },        /* .....***.... */
291         { 0x04, 0x00, },        /* .....*...... */
292         { 0x00, 0x00, },        /* ............ */
293         { 0x00, 0x00, },        /* ............ */
294         { 0x00, 0x00, },        /* ............ */
295         { 0x00, 0x00, },        /* ............ */
296         { 0x00, 0x00, },        /* ............ */
297         { 0x00, 0x00, },        /* ............ */
298 };
299
300 static inline void creator_ras_fifo_wait(struct creator_softc *sc, int n);
301 static inline void creator_ras_setfontinc(struct creator_softc *sc, int fontinc);
302 static inline void creator_ras_setfontw(struct creator_softc *sc, int fontw);
303 static inline void creator_ras_setbg(struct creator_softc *sc, int bg);
304 static inline void creator_ras_setfg(struct creator_softc *sc, int fg);
305 static inline void creator_ras_setpmask(struct creator_softc *sc, int pmask);
306 static inline void creator_ras_wait(struct creator_softc *sc);
307
308 static inline void
309 creator_ras_wait(struct creator_softc *sc)
310 {
311         int ucsr;
312         int r;
313
314         for (;;) {
315                 ucsr = FFB_READ(sc, FFB_FBC, FFB_FBC_UCSR);
316                 if ((ucsr & (FBC_UCSR_FB_BUSY | FBC_UCSR_RP_BUSY)) == 0)
317                         break;
318                 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
319                 if (r != 0)
320                         FFB_WRITE(sc, FFB_FBC, FFB_FBC_UCSR, r);
321         }
322 }
323
324 static inline void
325 creator_ras_fifo_wait(struct creator_softc *sc, int n)
326 {
327         int cache;
328
329         cache = sc->sc_fifo_cache;
330         while (cache < n)
331                 cache = (FFB_READ(sc, FFB_FBC, FFB_FBC_UCSR) &
332                     FBC_UCSR_FIFO_MASK) - 8;
333         sc->sc_fifo_cache = cache - n;
334 }
335
336 static inline void
337 creator_ras_setfontinc(struct creator_softc *sc, int fontinc)
338 {
339
340         if (fontinc == sc->sc_fontinc_cache)
341                 return;
342         sc->sc_fontinc_cache = fontinc;
343         creator_ras_fifo_wait(sc, 1);
344         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTINC, fontinc);
345         creator_ras_wait(sc);
346 }
347
348 static inline void
349 creator_ras_setfontw(struct creator_softc *sc, int fontw)
350 {
351
352         if (fontw == sc->sc_fontw_cache)
353                 return;
354         sc->sc_fontw_cache = fontw;
355         creator_ras_fifo_wait(sc, 1);
356         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTW, fontw);
357         creator_ras_wait(sc);
358 }
359
360 static inline void
361 creator_ras_setbg(struct creator_softc *sc, int bg)
362 {
363
364         if (bg == sc->sc_bg_cache)
365                 return;
366         sc->sc_bg_cache = bg;
367         creator_ras_fifo_wait(sc, 1);
368         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BG, bg);
369         creator_ras_wait(sc);
370 }
371
372 static inline void
373 creator_ras_setfg(struct creator_softc *sc, int fg)
374 {
375
376         if (fg == sc->sc_fg_cache)
377                 return;
378         sc->sc_fg_cache = fg;
379         creator_ras_fifo_wait(sc, 1);
380         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FG, fg);
381         creator_ras_wait(sc);
382 }
383
384 static inline void
385 creator_ras_setpmask(struct creator_softc *sc, int pmask)
386 {
387
388         if (pmask == sc->sc_pmask_cache)
389                 return;
390         sc->sc_pmask_cache = pmask;
391         creator_ras_fifo_wait(sc, 1);
392         FFB_WRITE(sc, FFB_FBC, FFB_FBC_PMASK, pmask);
393         creator_ras_wait(sc);
394 }
395
396 /*
397  * video driver interface
398  */
399 static int
400 creator_configure(int flags)
401 {
402         struct creator_softc *sc;
403         phandle_t chosen;
404         phandle_t output;
405         ihandle_t stdout;
406         bus_addr_t addr;
407         char buf[sizeof("SUNW,ffb")];
408         int i;
409         int space;
410
411         /*
412          * For the high-level console probing return the number of
413          * registered adapters.
414          */
415         if (!(flags & VIO_PROBE_ONLY)) {
416                 for (i = 0; vid_find_adapter(CREATOR_DRIVER_NAME, i) >= 0; i++)
417                         ;
418                 return (i);
419         }
420
421         /* Low-level console probing and initialization. */
422
423         sc = &creator_softc;
424         if (sc->sc_va.va_flags & V_ADP_REGISTERED)
425                 goto found;
426
427         if ((chosen = OF_finddevice("/chosen")) == -1)
428                 return (0);
429         if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
430                 return (0);
431         if ((output = OF_instance_to_package(stdout)) == -1)
432                 return (0);
433         if (OF_getprop(output, "name", buf, sizeof(buf)) == -1)
434                 return (0);
435         if (strcmp(buf, "SUNW,ffb") == 0 || strcmp(buf, "SUNW,afb") == 0) {
436                 sc->sc_flags = CREATOR_CONSOLE;
437                 if (strcmp(buf, "SUNW,afb") == 0)
438                         sc->sc_flags |= CREATOR_AFB;
439                 sc->sc_node = output;
440         } else
441                 return (0);
442
443         for (i = FFB_DAC; i <= FFB_FBC; i++) {
444                 if (OF_decode_addr(output, i, &space, &addr) != 0)
445                         return (0);
446                 sc->sc_bt[i] = &creator_bst_store[i - FFB_DAC];
447                 sc->sc_bh[i] = sparc64_fake_bustag(space, addr, sc->sc_bt[i]);
448         }
449
450         if (creator_init(0, &sc->sc_va, 0) < 0)
451                 return (0);
452
453  found:
454         /* Return number of found adapters. */
455         return (1);
456 }
457
458 static int
459 creator_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
460 {
461
462         return (0);
463 }
464
465 static int
466 creator_init(int unit, video_adapter_t *adp, int flags)
467 {
468         struct creator_softc *sc;
469         phandle_t options;
470         video_info_t *vi;
471         char buf[sizeof("screen-#columns")];
472
473         sc = (struct creator_softc *)adp;
474         vi = &adp->va_info;
475
476         vid_init_struct(adp, CREATOR_DRIVER_NAME, -1, unit);
477
478         if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
479             sizeof(sc->sc_height)) == -1)
480                 return (ENXIO);
481         if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
482             sizeof(sc->sc_width)) == -1)
483                 return (ENXIO);
484         if ((options = OF_finddevice("/options")) == -1)
485                 return (ENXIO);
486         if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
487                 return (ENXIO);
488         vi->vi_height = strtol(buf, NULL, 10);
489         if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
490                 return (ENXIO);
491         vi->vi_width = strtol(buf, NULL, 10);
492         vi->vi_cwidth = gallant12x22.width;
493         vi->vi_cheight = gallant12x22.height;
494         vi->vi_flags = V_INFO_COLOR;
495         vi->vi_mem_model = V_INFO_MM_OTHER;
496
497         sc->sc_font = gallant12x22.data;
498         sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
499         sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight)) / 2;
500
501         creator_set_mode(adp, 0);
502
503         if (!(sc->sc_flags & CREATOR_AFB)) {
504                 FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_DID);
505                 if (((FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE) &
506                     FFB_DAC_CFG_DID_PNUM) >> 12) != 0x236e) {
507                         sc->sc_flags |= CREATOR_PAC1;
508                         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_UCTRL);
509                         if (((FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE) &
510                             FFB_DAC_UCTRL_MANREV) >> 8) <= 2)
511                                 sc->sc_flags |= CREATOR_CURINV;
512                 }
513         }
514
515         creator_blank_display(adp, V_DISPLAY_ON);
516         creator_clear(adp);
517
518         /*
519          * Setting V_ADP_MODECHANGE serves as hack so creator_set_mode()
520          * (which will invalidate our caches and restore our settings) is
521          * called when the X server shuts down.  Otherwise screen corruption
522          * happens most of the time.
523          */
524         adp->va_flags |= V_ADP_COLOR | V_ADP_MODECHANGE | V_ADP_BORDER |
525             V_ADP_INITIALIZED;
526         if (vid_register(adp) < 0)
527                 return (ENXIO);
528         adp->va_flags |= V_ADP_REGISTERED;
529
530         return (0);
531 }
532
533 static int
534 creator_get_info(video_adapter_t *adp, int mode, video_info_t *info)
535 {
536
537         bcopy(&adp->va_info, info, sizeof(*info));
538         return (0);
539 }
540
541 static int
542 creator_query_mode(video_adapter_t *adp, video_info_t *info)
543 {
544
545         return (ENODEV);
546 }
547
548 static int
549 creator_set_mode(video_adapter_t *adp, int mode)
550 {
551         struct creator_softc *sc;
552
553         sc = (struct creator_softc *)adp;
554         sc->sc_bg_cache = -1;
555         sc->sc_fg_cache = -1;
556         sc->sc_fontinc_cache = -1;
557         sc->sc_fontw_cache = -1;
558         sc->sc_pmask_cache = -1;
559
560         creator_ras_wait(sc);
561         sc->sc_fifo_cache = 0;
562         creator_ras_fifo_wait(sc, 2);
563         FFB_WRITE(sc, FFB_FBC, FFB_FBC_PPC, FBC_PPC_VCE_DIS |
564             FBC_PPC_TBE_OPAQUE | FBC_PPC_APE_DIS | FBC_PPC_CS_CONST);
565         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FBC, FFB_FBC_WB_A | FFB_FBC_RB_A |
566             FFB_FBC_SB_BOTH | FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK);
567         return (0);
568 }
569
570 static int
571 creator_save_font(video_adapter_t *adp, int page, int size, int width,
572     u_char *data, int c, int count)
573 {
574
575         return (ENODEV);
576 }
577
578 static int
579 creator_load_font(video_adapter_t *adp, int page, int size, int width,
580     u_char *data, int c, int count)
581 {
582
583         return (ENODEV);
584 }
585
586 static int
587 creator_show_font(video_adapter_t *adp, int page)
588 {
589
590         return (ENODEV);
591 }
592
593 static int
594 creator_save_palette(video_adapter_t *adp, u_char *palette)
595 {
596
597         return (ENODEV);
598 }
599
600 static int
601 creator_load_palette(video_adapter_t *adp, u_char *palette)
602 {
603
604         return (ENODEV);
605 }
606
607 static int
608 creator_set_border(video_adapter_t *adp, int border)
609 {
610         struct creator_softc *sc;
611
612         sc = (struct creator_softc *)adp;
613         creator_fill_rect(adp, border, 0, 0, sc->sc_width, sc->sc_ymargin);
614         creator_fill_rect(adp, border, 0, sc->sc_height - sc->sc_ymargin,
615             sc->sc_width, sc->sc_ymargin);
616         creator_fill_rect(adp, border, 0, 0, sc->sc_xmargin, sc->sc_height);
617         creator_fill_rect(adp, border, sc->sc_width - sc->sc_xmargin, 0,
618             sc->sc_xmargin, sc->sc_height);
619         return (0);
620 }
621
622 static int
623 creator_save_state(video_adapter_t *adp, void *p, size_t size)
624 {
625
626         return (ENODEV);
627 }
628
629 static int
630 creator_load_state(video_adapter_t *adp, void *p)
631 {
632
633         return (ENODEV);
634 }
635
636 static int
637 creator_set_win_org(video_adapter_t *adp, off_t offset)
638 {
639
640         return (ENODEV);
641 }
642
643 static int
644 creator_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
645 {
646
647         *col = 0;
648         *row = 0;
649         return (0);
650 }
651
652 static int
653 creator_set_hw_cursor(video_adapter_t *adp, int col, int row)
654 {
655
656         return (ENODEV);
657 }
658
659 static int
660 creator_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
661     int celsize, int blink)
662 {
663
664         return (ENODEV);
665 }
666
667 static int
668 creator_blank_display(video_adapter_t *adp, int mode)
669 {
670         struct creator_softc *sc;
671         uint32_t v;
672         int i;
673
674         sc = (struct creator_softc *)adp;
675         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
676         v = FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE);
677         switch (mode) {
678         case V_DISPLAY_ON:
679                 v |= FFB_DAC_CFG_TGEN_VIDE;
680                 break;
681         case V_DISPLAY_BLANK:
682         case V_DISPLAY_STAND_BY:
683         case V_DISPLAY_SUSPEND:
684                 v &= ~FFB_DAC_CFG_TGEN_VIDE;
685                 break;
686         }
687         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
688         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE, v);
689         for (i = 0; i < 10; i++) {
690                 FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
691                 (void)FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE);
692         }
693         return (0);
694 }
695
696 static int
697 creator_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
698     int prot, vm_memattr_t *memattr)
699 {
700
701         return (EINVAL);
702 }
703
704 static int
705 creator_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
706 {
707         struct creator_softc *sc;
708         struct fbcursor *fbc;
709         struct fbtype *fb;
710
711         sc = (struct creator_softc *)adp;
712         switch (cmd) {
713         case FBIOGTYPE:
714                 fb = (struct fbtype *)data;
715                 fb->fb_type = FBTYPE_CREATOR;
716                 fb->fb_height = sc->sc_height;
717                 fb->fb_width = sc->sc_width;
718                 fb->fb_depth = fb->fb_cmsize = fb->fb_size = 0;
719                 break;
720         case FBIOSCURSOR:
721                 fbc = (struct fbcursor *)data;
722                 if (fbc->set & FB_CUR_SETCUR && fbc->enable == 0) {
723                         creator_cursor_enable(sc, 0);
724                         sc->sc_flags &= ~CREATOR_CUREN;
725                 } else
726                         return (ENODEV);
727                 break;
728                 break;
729         default:
730                 return (fb_commonioctl(adp, cmd, data));
731         }
732         return (0);
733 }
734
735 static int
736 creator_clear(video_adapter_t *adp)
737 {
738         struct creator_softc *sc;
739
740         sc = (struct creator_softc *)adp;
741         creator_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0, sc->sc_width,
742             sc->sc_height);
743         return (0);
744 }
745
746 static int
747 creator_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
748 {
749         struct creator_softc *sc;
750
751         sc = (struct creator_softc *)adp;
752         creator_ras_setpmask(sc, 0xffffffff);
753         creator_ras_fifo_wait(sc, 2);
754         FFB_WRITE(sc, FFB_FBC, FFB_FBC_ROP, FBC_ROP_NEW);
755         FFB_WRITE(sc, FFB_FBC, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
756         creator_ras_setfg(sc, creator_cmap[val & 0xf]);
757         /*
758          * Note that at least the Elite3D cards are sensitive to the order
759          * of operations here.
760          */
761         creator_ras_fifo_wait(sc, 4);
762         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BY, y);
763         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BX, x);
764         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BH, cy);
765         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BW, cx);
766         creator_ras_wait(sc);
767         return (0);
768 }
769
770 static int
771 creator_bitblt(video_adapter_t *adp, ...)
772 {
773
774         return (ENODEV);
775 }
776
777 static int
778 creator_diag(video_adapter_t *adp, int level)
779 {
780         video_info_t info;
781
782         fb_dump_adp_info(adp->va_name, adp, level);
783         creator_get_info(adp, 0, &info);
784         fb_dump_mode_info(adp->va_name, adp, &info, level);
785         return (0);
786 }
787
788 static int
789 creator_save_cursor_palette(video_adapter_t *adp, u_char *palette)
790 {
791
792         return (ENODEV);
793 }
794
795 static int
796 creator_load_cursor_palette(video_adapter_t *adp, u_char *palette)
797 {
798
799         return (ENODEV);
800 }
801
802 static int
803 creator_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
804 {
805
806         return (ENODEV);
807 }
808
809 static int
810 creator_putp(video_adapter_t *adp, vm_offset_t off, u_int32_t p, u_int32_t a,
811     int size, int bpp, int bit_ltor, int byte_ltor)
812 {
813
814         return (ENODEV);
815 }
816
817 static int
818 creator_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
819 {
820         struct creator_softc *sc;
821         const uint16_t *p;
822         int row;
823         int col;
824         int i;
825
826         sc = (struct creator_softc *)adp;
827         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
828         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
829         p = (const uint16_t *)sc->sc_font + (c * adp->va_info.vi_cheight);
830         creator_ras_setfg(sc, creator_cmap[a & 0xf]);
831         creator_ras_setbg(sc, creator_cmap[(a >> 4) & 0xf]);
832         creator_ras_fifo_wait(sc, 1 + adp->va_info.vi_cheight);
833         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTXY,
834             ((row + sc->sc_ymargin) << 16) | (col + sc->sc_xmargin));
835         creator_ras_setfontw(sc, adp->va_info.vi_cwidth);
836         creator_ras_setfontinc(sc, 0x10000);
837         for (i = 0; i < adp->va_info.vi_cheight; i++) {
838                 FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONT, *p++ << 16);
839         }
840         return (0);
841 }
842
843 static int
844 creator_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
845 {
846         int i;
847
848         for (i = 0; i < len; i++) {
849                 vidd_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
850         }
851
852         return (0);
853 }
854
855 static int
856 creator_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
857     u_int32_t pixel_mask, int size, int width)
858 {
859         struct creator_softc *sc;
860
861         sc = (struct creator_softc *)adp;
862         if (!(sc->sc_flags & CREATOR_CUREN)) {
863                 creator_cursor_install(sc);
864                 creator_cursor_enable(sc, 1);
865                 sc->sc_flags |= CREATOR_CUREN;
866         }
867         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE2, FFB_DAC_CUR_POS);
868         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2,
869             ((y + sc->sc_ymargin) << 16) | (x + sc->sc_xmargin));
870         return (0);
871 }
872
873 /*
874  * bus interface
875  */
876 static int
877 creator_bus_probe(device_t dev)
878 {
879         const char *name;
880         phandle_t node;
881         int type;
882
883         name = ofw_bus_get_name(dev);
884         node = ofw_bus_get_node(dev);
885         if (strcmp(name, "SUNW,ffb") == 0) {
886                 if (OF_getprop(node, "board_type", &type, sizeof(type)) == -1)
887                         return (ENXIO);
888                 switch (type & 7) {
889                 case 0x0:
890                         device_set_desc(dev, "Creator");
891                         break;
892                 case 0x3:
893                         device_set_desc(dev, "Creator3D");
894                         break;
895                 default:
896                         return (ENXIO);
897                 }
898         } else if (strcmp(name, "SUNW,afb") == 0)
899                 device_set_desc(dev, "Elite3D");
900         else
901                 return (ENXIO);
902         return (BUS_PROBE_DEFAULT);
903 }
904
905 static int
906 creator_bus_attach(device_t dev)
907 {
908         struct creator_softc *sc;
909         video_adapter_t *adp;
910         video_switch_t *sw;
911         phandle_t node;
912         int error;
913         int rid;
914         int unit;
915         int i;
916
917         node = ofw_bus_get_node(dev);
918         if ((sc = (struct creator_softc *)vid_get_adapter(vid_find_adapter(
919             CREATOR_DRIVER_NAME, 0))) != NULL && sc->sc_node == node) {
920                 device_printf(dev, "console\n");
921                 device_set_softc(dev, sc);
922         } else {
923                 sc = device_get_softc(dev);
924                 sc->sc_node = node;
925         }
926         adp = &sc->sc_va;
927
928         /*
929          * Allocate resources regardless of whether we are the console
930          * and already obtained the bus tags and handles for the FFB_DAC
931          * and FFB_FBC register banks in creator_configure() or not so
932          * the resources are marked as taken in the respective RMAN.
933          * The supported cards use either 15 (Creator, Elite3D?) or 24
934          * (Creator3D?) register banks.  We make sure that we can also
935          * allocate the resources for at least the FFB_DAC and FFB_FBC
936          * banks here.  We try but don't actually care whether we can
937          * allocate more than these two resources and just limit the
938          * range accessible via creator_fb_mmap() accordingly.
939          */
940         for (i = 0; i < FFB_NREG; i++) {
941                 rid = i;
942                 sc->sc_reg[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
943                     &rid, RF_ACTIVE);
944                 if (sc->sc_reg[i] == NULL) {
945                         if (i <= FFB_FBC) {
946                                 device_printf(dev,
947                                     "cannot allocate resources\n");
948                                 error = ENXIO;
949                                 goto fail;
950                         }
951                         break;
952                 }
953                 sc->sc_bt[i] = rman_get_bustag(sc->sc_reg[i]);
954                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_reg[i]);
955         }
956         /*
957          * The XFree86/X.Org sunffb(4) expects to be able to access the
958          * memory spanned by the first and the last resource as one chunk
959          * via creator_fb_mmap(), using offsets from the first resource,
960          * even though the backing resources are actually non-continuous.
961          * So make sure that the memory we provide is at least backed by
962          * increasing resources.
963          */
964         for (i = 1; i < FFB_NREG && sc->sc_reg[i] != NULL &&
965             rman_get_start(sc->sc_reg[i]) > rman_get_start(sc->sc_reg[i - 1]);
966             i++)
967                 ;
968         sc->sc_reg_size = rman_get_end(sc->sc_reg[i - 1]) -
969             rman_get_start(sc->sc_reg[0]) + 1;
970
971         if (!(sc->sc_flags & CREATOR_CONSOLE)) {
972                 if ((sw = vid_get_switch(CREATOR_DRIVER_NAME)) == NULL) {
973                         device_printf(dev, "cannot get video switch\n");
974                         error = ENODEV;
975                         goto fail;
976                 }
977                 /*
978                  * During device configuration we don't necessarily probe
979                  * the adapter which is the console first so we can't use
980                  * the device unit number for the video adapter unit.  The
981                  * worst case would be that we use the video adapter unit
982                  * 0 twice.  As it doesn't really matter which unit number
983                  * the corresponding video adapter has just use the next
984                  * unused one.
985                  */
986                 for (i = 0; i < devclass_get_maxunit(creator_devclass); i++)
987                         if (vid_find_adapter(CREATOR_DRIVER_NAME, i) < 0)
988                                 break;
989                 if (strcmp(ofw_bus_get_name(dev), "SUNW,afb") == 0)
990                         sc->sc_flags |= CREATOR_AFB;
991                 if ((error = sw->init(i, adp, 0)) != 0) {
992                         device_printf(dev, "cannot initialize adapter\n");
993                         goto fail;
994                 }
995         }
996
997         if (bootverbose) {
998                 if (sc->sc_flags & CREATOR_PAC1)
999                         device_printf(dev,
1000                             "BT9068/PAC1 RAMDAC (%s cursor control)\n",
1001                             sc->sc_flags & CREATOR_CURINV ? "inverted" :
1002                             "normal");
1003                 else
1004                         device_printf(dev, "BT498/PAC2 RAMDAC\n");
1005         }
1006         device_printf(dev, "resolution %dx%d\n", sc->sc_width, sc->sc_height);
1007
1008         unit = device_get_unit(dev);
1009         sc->sc_si = make_dev(&creator_fb_devsw, unit, UID_ROOT, GID_WHEEL,
1010             0600, "fb%d", unit);
1011         sc->sc_si->si_drv1 = sc;
1012
1013         EVENTHANDLER_REGISTER(shutdown_final, creator_shutdown, sc,
1014             SHUTDOWN_PRI_DEFAULT);
1015
1016         return (0);
1017
1018  fail:
1019         for (i = 0; i < FFB_NREG && sc->sc_reg[i] != NULL; i++)
1020                 bus_release_resource(dev, SYS_RES_MEMORY,
1021                     rman_get_rid(sc->sc_reg[i]), sc->sc_reg[i]);
1022         return (error);
1023 }
1024
1025 /*
1026  * /dev/fb interface
1027  */
1028 static int
1029 creator_fb_open(struct cdev *dev, int flags, int mode, struct thread *td)
1030 {
1031
1032         return (0);
1033 }
1034
1035 static int
1036 creator_fb_close(struct cdev *dev, int flags, int mode, struct thread *td)
1037 {
1038
1039         return (0);
1040 }
1041
1042 static int
1043 creator_fb_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
1044     struct thread *td)
1045 {
1046         struct creator_softc *sc;
1047
1048         sc = dev->si_drv1;
1049         return (creator_ioctl(&sc->sc_va, cmd, data));
1050 }
1051
1052 static int
1053 creator_fb_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
1054     int prot, vm_memattr_t *memattr)
1055 {
1056         struct creator_softc *sc;
1057         int i;
1058
1059         /*
1060          * NB: This is a special implementation based on the /dev/fb
1061          * requirements of the XFree86/X.Org sunffb(4).
1062          */
1063         sc = dev->si_drv1;
1064         for (i = 0; i < CREATOR_FB_MAP_SIZE; i++) {
1065                 if (offset >= creator_fb_map[i].virt &&
1066                     offset < creator_fb_map[i].virt + creator_fb_map[i].size) {
1067                         offset += creator_fb_map[i].phys -
1068                             creator_fb_map[i].virt;
1069                         if (offset >= sc->sc_reg_size)
1070                                 return (EINVAL);
1071                         *paddr = sc->sc_bh[0] + offset;
1072                         return (0);
1073                 }
1074         }
1075         return (EINVAL);
1076 }
1077
1078 /*
1079  * internal functions
1080  */
1081 static void
1082 creator_cursor_enable(struct creator_softc *sc, int onoff)
1083 {
1084         int v;
1085
1086         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE2, FFB_DAC_CUR_CTRL);
1087         if (sc->sc_flags & CREATOR_CURINV)
1088                 v = onoff ? FFB_DAC_CUR_CTRL_P0 | FFB_DAC_CUR_CTRL_P1 : 0;
1089         else
1090                 v = onoff ? 0 : FFB_DAC_CUR_CTRL_P0 | FFB_DAC_CUR_CTRL_P1;
1091         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2, v);
1092 }
1093
1094 static void
1095 creator_cursor_install(struct creator_softc *sc)
1096 {
1097         int i, j;
1098
1099         creator_cursor_enable(sc, 0);
1100         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE2, FFB_DAC_CUR_COLOR1);
1101         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2, 0xffffff);
1102         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2, 0x0);
1103         for (i = 0; i < 2; i++) {
1104                 FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE2,
1105                     i ? FFB_DAC_CUR_BITMAP_P0 : FFB_DAC_CUR_BITMAP_P1);
1106                 for (j = 0; j < 64; j++) {
1107                         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2,
1108                             *(const uint32_t *)(&creator_mouse_pointer[j][0]));
1109                         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2,
1110                             *(const uint32_t *)(&creator_mouse_pointer[j][4]));
1111                 }
1112         }
1113 }
1114
1115 static void
1116 creator_shutdown(void *xsc)
1117 {
1118         struct creator_softc *sc = xsc;
1119
1120         creator_cursor_enable(sc, 0);
1121         /*
1122          * In case this is the console set the cursor of the stdout
1123          * instance to the start of the last line so OFW output ends
1124          * up beneath what FreeBSD left on the screen.
1125          */
1126         if (sc->sc_flags & CREATOR_CONSOLE) {
1127                 OF_interpret("stdout @ is my-self 0 to column#", 0);
1128                 OF_interpret("stdout @ is my-self #lines 1 - to line#", 0);
1129         }
1130 }