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