]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/fb/creator.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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
70         int                     sc_height;
71         int                     sc_width;
72
73         int                     sc_xmargin;
74         int                     sc_ymargin;
75
76         const u_char            *sc_font;
77
78         int                     sc_bg_cache;
79         int                     sc_fg_cache;
80         int                     sc_fifo_cache;
81         int                     sc_fontinc_cache;
82         int                     sc_fontw_cache;
83         int                     sc_pmask_cache;
84
85         int                     sc_flags;
86 #define CREATOR_AFB             (1 << 0)
87 #define CREATOR_CONSOLE         (1 << 1)
88 #define CREATOR_CUREN           (1 << 2)
89 #define CREATOR_CURINV          (1 << 3)
90 #define CREATOR_PAC1            (1 << 4)
91 };
92
93 #define FFB_READ(sc, reg, off)                                          \
94         bus_space_read_4((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off))
95 #define FFB_WRITE(sc, reg, off, val)                                    \
96         bus_space_write_4((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val))
97
98 #define C(r, g, b)      ((b << 16) | (g << 8) | (r))
99 static const uint32_t creator_cmap[] = {
100         C(0x00, 0x00, 0x00),            /* black */
101         C(0x00, 0x00, 0xff),            /* blue */
102         C(0x00, 0xff, 0x00),            /* green */
103         C(0x00, 0xc0, 0xc0),            /* cyan */
104         C(0xff, 0x00, 0x00),            /* red */
105         C(0xc0, 0x00, 0xc0),            /* magenta */
106         C(0xc0, 0xc0, 0x00),            /* brown */
107         C(0xc0, 0xc0, 0xc0),            /* light grey */
108         C(0x80, 0x80, 0x80),            /* dark grey */
109         C(0x80, 0x80, 0xff),            /* light blue */
110         C(0x80, 0xff, 0x80),            /* light green */
111         C(0x80, 0xff, 0xff),            /* light cyan */
112         C(0xff, 0x80, 0x80),            /* light red */
113         C(0xff, 0x80, 0xff),            /* light magenta */
114         C(0xff, 0xff, 0x80),            /* yellow */
115         C(0xff, 0xff, 0xff),            /* white */
116 };
117 #undef C
118
119 static const struct {
120         vm_offset_t virt;
121         vm_paddr_t phys;
122         vm_size_t size;
123 } creator_fb_map[] = {
124         { FFB_VIRT_SFB8R,       FFB_PHYS_SFB8R,         FFB_SIZE_SFB8R },
125         { FFB_VIRT_SFB8G,       FFB_PHYS_SFB8G,         FFB_SIZE_SFB8G },
126         { FFB_VIRT_SFB8B,       FFB_PHYS_SFB8B,         FFB_SIZE_SFB8B },
127         { FFB_VIRT_SFB8X,       FFB_PHYS_SFB8X,         FFB_SIZE_SFB8X },
128         { FFB_VIRT_SFB32,       FFB_PHYS_SFB32,         FFB_SIZE_SFB32 },
129         { FFB_VIRT_SFB64,       FFB_PHYS_SFB64,         FFB_SIZE_SFB64 },
130         { FFB_VIRT_FBC,         FFB_PHYS_FBC,           FFB_SIZE_FBC },
131         { FFB_VIRT_FBC_BM,      FFB_PHYS_FBC_BM,        FFB_SIZE_FBC_BM },
132         { FFB_VIRT_DFB8R,       FFB_PHYS_DFB8R,         FFB_SIZE_DFB8R },
133         { FFB_VIRT_DFB8G,       FFB_PHYS_DFB8G,         FFB_SIZE_DFB8G },
134         { FFB_VIRT_DFB8B,       FFB_PHYS_DFB8B,         FFB_SIZE_DFB8B },
135         { FFB_VIRT_DFB8X,       FFB_PHYS_DFB8X,         FFB_SIZE_DFB8X },
136         { FFB_VIRT_DFB24,       FFB_PHYS_DFB24,         FFB_SIZE_DFB24 },
137         { FFB_VIRT_DFB32,       FFB_PHYS_DFB32,         FFB_SIZE_DFB32 },
138         { FFB_VIRT_DFB422A,     FFB_PHYS_DFB422A,       FFB_SIZE_DFB422A },
139         { FFB_VIRT_DFB422AD,    FFB_PHYS_DFB422AD,      FFB_SIZE_DFB422AD },
140         { FFB_VIRT_DFB24B,      FFB_PHYS_DFB24B,        FFB_SIZE_DFB24B },
141         { FFB_VIRT_DFB422B,     FFB_PHYS_DFB422B,       FFB_SIZE_DFB422B },
142         { FFB_VIRT_DFB422BD,    FFB_PHYS_DFB422BD,      FFB_SIZE_DFB422BD },
143         { FFB_VIRT_SFB16Z,      FFB_PHYS_SFB16Z,        FFB_SIZE_SFB16Z },
144         { FFB_VIRT_SFB8Z,       FFB_PHYS_SFB8Z,         FFB_SIZE_SFB8Z },
145         { FFB_VIRT_SFB422,      FFB_PHYS_SFB422,        FFB_SIZE_SFB422 },
146         { FFB_VIRT_SFB422D,     FFB_PHYS_SFB422D,       FFB_SIZE_SFB422D },
147         { FFB_VIRT_FBC_KREG,    FFB_PHYS_FBC_KREG,      FFB_SIZE_FBC_KREG },
148         { FFB_VIRT_DAC,         FFB_PHYS_DAC,           FFB_SIZE_DAC },
149         { FFB_VIRT_PROM,        FFB_PHYS_PROM,          FFB_SIZE_PROM },
150         { FFB_VIRT_EXP,         FFB_PHYS_EXP,           FFB_SIZE_EXP },
151 };
152
153 #define CREATOR_FB_MAP_SIZE                                             \
154         (sizeof(creator_fb_map) / sizeof(creator_fb_map[0]))
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         NULL,                                           /* XXX brain damage */
256         NULL,                                           /* XXX brain damage */
257         .diag                   = creator_diag,
258         .save_cursor_palette    = creator_save_cursor_palette,
259         .load_cursor_palette    = creator_load_cursor_palette,
260         .copy                   = creator_copy,
261         .putp                   = creator_putp,
262         .putc                   = creator_putc,
263         .puts                   = creator_puts,
264         .putm                   = creator_putm
265 };
266
267 VIDEO_DRIVER(creator, creatorvidsw, creator_configure);
268
269 extern sc_rndr_sw_t txtrndrsw;
270 RENDERER(creator, 0, txtrndrsw, gfb_set);
271
272 RENDERER_MODULE(creator, gfb_set);
273
274 static const u_char creator_mouse_pointer[64][8] __aligned(8) = {
275         { 0x00, 0x00, },        /* ............ */
276         { 0x80, 0x00, },        /* *........... */
277         { 0xc0, 0x00, },        /* **.......... */
278         { 0xe0, 0x00, },        /* ***......... */
279         { 0xf0, 0x00, },        /* ****........ */
280         { 0xf8, 0x00, },        /* *****....... */
281         { 0xfc, 0x00, },        /* ******...... */
282         { 0xfe, 0x00, },        /* *******..... */
283         { 0xff, 0x00, },        /* ********.... */
284         { 0xff, 0x80, },        /* *********... */
285         { 0xfc, 0xc0, },        /* ******..**.. */
286         { 0xdc, 0x00, },        /* **.***...... */
287         { 0x8e, 0x00, },        /* *...***..... */
288         { 0x0e, 0x00, },        /* ....***..... */
289         { 0x07, 0x00, },        /* .....***.... */
290         { 0x04, 0x00, },        /* .....*...... */
291         { 0x00, 0x00, },        /* ............ */
292         { 0x00, 0x00, },        /* ............ */
293         { 0x00, 0x00, },        /* ............ */
294         { 0x00, 0x00, },        /* ............ */
295         { 0x00, 0x00, },        /* ............ */
296         { 0x00, 0x00, },        /* ............ */
297 };
298
299 static inline void creator_ras_fifo_wait(struct creator_softc *sc, int n);
300 static inline void creator_ras_setfontinc(struct creator_softc *sc, int fontinc);
301 static inline void creator_ras_setfontw(struct creator_softc *sc, int fontw);
302 static inline void creator_ras_setbg(struct creator_softc *sc, int bg);
303 static inline void creator_ras_setfg(struct creator_softc *sc, int fg);
304 static inline void creator_ras_setpmask(struct creator_softc *sc, int pmask);
305 static inline void creator_ras_wait(struct creator_softc *sc);
306
307 static inline void
308 creator_ras_wait(struct creator_softc *sc)
309 {
310         int ucsr;
311         int r;
312
313         for (;;) {
314                 ucsr = FFB_READ(sc, FFB_FBC, FFB_FBC_UCSR);
315                 if ((ucsr & (FBC_UCSR_FB_BUSY | FBC_UCSR_RP_BUSY)) == 0)
316                         break;
317                 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
318                 if (r != 0)
319                         FFB_WRITE(sc, FFB_FBC, FFB_FBC_UCSR, r);
320         }
321 }
322
323 static inline void
324 creator_ras_fifo_wait(struct creator_softc *sc, int n)
325 {
326         int cache;
327
328         cache = sc->sc_fifo_cache;
329         while (cache < n)
330                 cache = (FFB_READ(sc, FFB_FBC, FFB_FBC_UCSR) &
331                     FBC_UCSR_FIFO_MASK) - 8;
332         sc->sc_fifo_cache = cache - n;
333 }
334
335 static inline void
336 creator_ras_setfontinc(struct creator_softc *sc, int fontinc)
337 {
338
339         if (fontinc == sc->sc_fontinc_cache)
340                 return;
341         sc->sc_fontinc_cache = fontinc;
342         creator_ras_fifo_wait(sc, 1);
343         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTINC, fontinc);
344         creator_ras_wait(sc);
345 }
346
347 static inline void
348 creator_ras_setfontw(struct creator_softc *sc, int fontw)
349 {
350
351         if (fontw == sc->sc_fontw_cache)
352                 return;
353         sc->sc_fontw_cache = fontw;
354         creator_ras_fifo_wait(sc, 1);
355         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTW, fontw);
356         creator_ras_wait(sc);
357 }
358
359 static inline void
360 creator_ras_setbg(struct creator_softc *sc, int bg)
361 {
362
363         if (bg == sc->sc_bg_cache)
364                 return;
365         sc->sc_bg_cache = bg;
366         creator_ras_fifo_wait(sc, 1);
367         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BG, bg);
368         creator_ras_wait(sc);
369 }
370
371 static inline void
372 creator_ras_setfg(struct creator_softc *sc, int fg)
373 {
374
375         if (fg == sc->sc_fg_cache)
376                 return;
377         sc->sc_fg_cache = fg;
378         creator_ras_fifo_wait(sc, 1);
379         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FG, fg);
380         creator_ras_wait(sc);
381 }
382
383 static inline void
384 creator_ras_setpmask(struct creator_softc *sc, int pmask)
385 {
386
387         if (pmask == sc->sc_pmask_cache)
388                 return;
389         sc->sc_pmask_cache = pmask;
390         creator_ras_fifo_wait(sc, 1);
391         FFB_WRITE(sc, FFB_FBC, FFB_FBC_PMASK, pmask);
392         creator_ras_wait(sc);
393 }
394
395 /*
396  * video driver interface
397  */
398 static int
399 creator_configure(int flags)
400 {
401         struct creator_softc *sc;
402         phandle_t chosen;
403         phandle_t output;
404         ihandle_t stdout;
405         bus_addr_t addr;
406         char buf[sizeof("SUNW,ffb")];
407         int i;
408         int space;
409
410         /*
411          * For the high-level console probing return the number of
412          * registered adapters.
413          */
414         if (!(flags & VIO_PROBE_ONLY)) {
415                 for (i = 0; vid_find_adapter(CREATOR_DRIVER_NAME, i) >= 0; i++)
416                         ;
417                 return (i);
418         }
419
420         /* Low-level console probing and initialization. */
421
422         sc = &creator_softc;
423         if (sc->sc_va.va_flags & V_ADP_REGISTERED)
424                 goto found;
425
426         if ((chosen = OF_finddevice("/chosen")) == -1)
427                 return (0);
428         if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
429                 return (0);
430         if ((output = OF_instance_to_package(stdout)) == -1)
431                 return (0);
432         if (OF_getprop(output, "name", buf, sizeof(buf)) == -1)
433                 return (0);
434         if (strcmp(buf, "SUNW,ffb") == 0 || strcmp(buf, "SUNW,afb") == 0) {
435                 sc->sc_flags = CREATOR_CONSOLE;
436                 if (strcmp(buf, "SUNW,afb") == 0)
437                         sc->sc_flags |= CREATOR_AFB;
438                 sc->sc_node = output;
439         } else
440                 return (0);
441
442         for (i = FFB_DAC; i <= FFB_FBC; i++) {
443                 if (OF_decode_addr(output, i, &space, &addr) != 0)
444                         return (0);
445                 sc->sc_bt[i] = &creator_bst_store[i - FFB_DAC];
446                 sc->sc_bh[i] = sparc64_fake_bustag(space, addr, sc->sc_bt[i]);
447         }
448
449         if (creator_init(0, &sc->sc_va, 0) < 0)
450                 return (0);
451
452  found:
453         /* Return number of found adapters. */
454         return (1);
455 }
456
457 static int
458 creator_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
459 {
460
461         return (0);
462 }
463
464 static int
465 creator_init(int unit, video_adapter_t *adp, int flags)
466 {
467         struct creator_softc *sc;
468         phandle_t options;
469         video_info_t *vi;
470         char buf[sizeof("screen-#columns")];
471
472         sc = (struct creator_softc *)adp;
473         vi = &adp->va_info;
474
475         vid_init_struct(adp, CREATOR_DRIVER_NAME, -1, unit);
476
477         if (OF_getprop(sc->sc_node, "height", &sc->sc_height,
478             sizeof(sc->sc_height)) == -1)
479                 return (ENXIO);
480         if (OF_getprop(sc->sc_node, "width", &sc->sc_width,
481             sizeof(sc->sc_width)) == -1)
482                 return (ENXIO);
483         if ((options = OF_finddevice("/options")) == -1)
484                 return (ENXIO);
485         if (OF_getprop(options, "screen-#rows", buf, sizeof(buf)) == -1)
486                 return (ENXIO);
487         vi->vi_height = strtol(buf, NULL, 10);
488         if (OF_getprop(options, "screen-#columns", buf, sizeof(buf)) == -1)
489                 return (ENXIO);
490         vi->vi_width = strtol(buf, NULL, 10);
491         vi->vi_cwidth = gallant12x22.width;
492         vi->vi_cheight = gallant12x22.height;
493         vi->vi_flags = V_INFO_COLOR;
494         vi->vi_mem_model = V_INFO_MM_OTHER;
495
496         sc->sc_font = gallant12x22.data;
497         sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
498         sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight)) / 2;
499
500         creator_set_mode(adp, 0);
501
502         if (!(sc->sc_flags & CREATOR_AFB)) {
503                 FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_DID);
504                 if (((FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE) &
505                     FFB_DAC_CFG_DID_PNUM) >> 12) != 0x236e) {
506                         sc->sc_flags |= CREATOR_PAC1;
507                         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_UCTRL);
508                         if (((FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE) &
509                             FFB_DAC_UCTRL_MANREV) >> 8) <= 2)
510                                 sc->sc_flags |= CREATOR_CURINV;
511                 }
512         }
513
514         creator_blank_display(adp, V_DISPLAY_ON);
515         creator_clear(adp);
516
517         /*
518          * Setting V_ADP_MODECHANGE serves as hack so creator_set_mode()
519          * (which will invalidate our caches and restore our settings) is
520          * called when the X server shuts down. Otherwise screen corruption
521          * happens most of the time.
522          */
523         adp->va_flags |= V_ADP_COLOR | V_ADP_MODECHANGE | V_ADP_BORDER |
524             V_ADP_INITIALIZED;
525         if (vid_register(adp) < 0)
526                 return (ENXIO);
527         adp->va_flags |= V_ADP_REGISTERED;
528
529         return (0);
530 }
531
532 static int
533 creator_get_info(video_adapter_t *adp, int mode, video_info_t *info)
534 {
535
536         bcopy(&adp->va_info, info, sizeof(*info));
537         return (0);
538 }
539
540 static int
541 creator_query_mode(video_adapter_t *adp, video_info_t *info)
542 {
543
544         return (ENODEV);
545 }
546
547 static int
548 creator_set_mode(video_adapter_t *adp, int mode)
549 {
550         struct creator_softc *sc;
551
552         sc = (struct creator_softc *)adp;
553         sc->sc_bg_cache = -1;
554         sc->sc_fg_cache = -1;
555         sc->sc_fontinc_cache = -1;
556         sc->sc_fontw_cache = -1;
557         sc->sc_pmask_cache = -1;
558
559         creator_ras_wait(sc);
560         sc->sc_fifo_cache = 0;
561         creator_ras_fifo_wait(sc, 2);
562         FFB_WRITE(sc, FFB_FBC, FFB_FBC_PPC, FBC_PPC_VCE_DIS |
563             FBC_PPC_TBE_OPAQUE | FBC_PPC_APE_DIS | FBC_PPC_CS_CONST);
564         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FBC, FFB_FBC_WB_A | FFB_FBC_RB_A |
565             FFB_FBC_SB_BOTH | FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK);
566         return (0);
567 }
568
569 static int
570 creator_save_font(video_adapter_t *adp, int page, int size, int width,
571     u_char *data, int c, int count)
572 {
573
574         return (ENODEV);
575 }
576
577 static int
578 creator_load_font(video_adapter_t *adp, int page, int size, int width,
579     u_char *data, int c, int count)
580 {
581
582         return (ENODEV);
583 }
584
585 static int
586 creator_show_font(video_adapter_t *adp, int page)
587 {
588
589         return (ENODEV);
590 }
591
592 static int
593 creator_save_palette(video_adapter_t *adp, u_char *palette)
594 {
595
596         return (ENODEV);
597 }
598
599 static int
600 creator_load_palette(video_adapter_t *adp, u_char *palette)
601 {
602
603         return (ENODEV);
604 }
605
606 static int
607 creator_set_border(video_adapter_t *adp, int border)
608 {
609         struct creator_softc *sc;
610
611         sc = (struct creator_softc *)adp;
612         creator_fill_rect(adp, border, 0, 0, sc->sc_width, sc->sc_ymargin);
613         creator_fill_rect(adp, border, 0, sc->sc_height - sc->sc_ymargin,
614             sc->sc_width, sc->sc_ymargin);
615         creator_fill_rect(adp, border, 0, 0, sc->sc_xmargin, sc->sc_height);
616         creator_fill_rect(adp, border, sc->sc_width - sc->sc_xmargin, 0,
617             sc->sc_xmargin, sc->sc_height);
618         return (0);
619 }
620
621 static int
622 creator_save_state(video_adapter_t *adp, void *p, size_t size)
623 {
624
625         return (ENODEV);
626 }
627
628 static int
629 creator_load_state(video_adapter_t *adp, void *p)
630 {
631
632         return (ENODEV);
633 }
634
635 static int
636 creator_set_win_org(video_adapter_t *adp, off_t offset)
637 {
638
639         return (ENODEV);
640 }
641
642 static int
643 creator_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
644 {
645
646         *col = 0;
647         *row = 0;
648         return (0);
649 }
650
651 static int
652 creator_set_hw_cursor(video_adapter_t *adp, int col, int row)
653 {
654
655         return (ENODEV);
656 }
657
658 static int
659 creator_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
660     int celsize, int blink)
661 {
662
663         return (ENODEV);
664 }
665
666 static int
667 creator_blank_display(video_adapter_t *adp, int mode)
668 {
669         struct creator_softc *sc;
670         uint32_t v;
671         int i;
672
673         sc = (struct creator_softc *)adp;
674         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
675         v = FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE);
676         switch (mode) {
677         case V_DISPLAY_ON:
678                 v |= FFB_DAC_CFG_TGEN_VIDE;
679                 break;
680         case V_DISPLAY_BLANK:
681         case V_DISPLAY_STAND_BY:
682         case V_DISPLAY_SUSPEND:
683                 v &= ~FFB_DAC_CFG_TGEN_VIDE;
684                 break;
685         }
686         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
687         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE, v);
688         for (i = 0; i < 10; i++) {
689                 FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE, FFB_DAC_CFG_TGEN);
690                 (void)FFB_READ(sc, FFB_DAC, FFB_DAC_VALUE);
691         }
692         return (0);
693 }
694
695 static int
696 creator_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
697     int prot)
698 {
699
700         return (EINVAL);
701 }
702
703 static int
704 creator_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
705 {
706         struct creator_softc *sc;
707         struct fbcursor *fbc;
708         struct fbtype *fb;
709
710         sc = (struct creator_softc *)adp;
711         switch (cmd) {
712         case FBIOGTYPE:
713                 fb = (struct fbtype *)data;
714                 fb->fb_type = FBTYPE_CREATOR;
715                 fb->fb_height = sc->sc_height;
716                 fb->fb_width = sc->sc_width;
717                 fb->fb_depth = fb->fb_cmsize = fb->fb_size = 0;
718                 break;
719         case FBIOSCURSOR:
720                 fbc = (struct fbcursor *)data;
721                 if (fbc->set & FB_CUR_SETCUR && fbc->enable == 0) {
722                         creator_cursor_enable(sc, 0);
723                         sc->sc_flags &= ~CREATOR_CUREN;
724                 } else
725                         return (ENODEV);
726                 break;
727                 break;
728         default:
729                 return (fb_commonioctl(adp, cmd, data));
730         }
731         return (0);
732 }
733
734 static int
735 creator_clear(video_adapter_t *adp)
736 {
737         struct creator_softc *sc;
738
739         sc = (struct creator_softc *)adp;
740         creator_fill_rect(adp, (SC_NORM_ATTR >> 4) & 0xf, 0, 0, sc->sc_width,
741             sc->sc_height);
742         return (0);
743 }
744
745 static int
746 creator_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
747 {
748         struct creator_softc *sc;
749
750         sc = (struct creator_softc *)adp;
751         creator_ras_setpmask(sc, 0xffffffff);
752         creator_ras_fifo_wait(sc, 2);
753         FFB_WRITE(sc, FFB_FBC, FFB_FBC_ROP, FBC_ROP_NEW);
754         FFB_WRITE(sc, FFB_FBC, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
755         creator_ras_setfg(sc, creator_cmap[val & 0xf]);
756         /*
757          * Note that at least the Elite3D cards are sensitive to the order
758          * of operations here.
759          */
760         creator_ras_fifo_wait(sc, 4);
761         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BY, y);
762         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BX, x);
763         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BH, cy);
764         FFB_WRITE(sc, FFB_FBC, FFB_FBC_BW, cx);
765         creator_ras_wait(sc);
766         return (0);
767 }
768
769 static int
770 creator_bitblt(video_adapter_t *adp, ...)
771 {
772
773         return (ENODEV);
774 }
775
776 static int
777 creator_diag(video_adapter_t *adp, int level)
778 {
779         video_info_t info;
780
781         fb_dump_adp_info(adp->va_name, adp, level);
782         creator_get_info(adp, 0, &info);
783         fb_dump_mode_info(adp->va_name, adp, &info, level);
784         return (0);
785 }
786
787 static int
788 creator_save_cursor_palette(video_adapter_t *adp, u_char *palette)
789 {
790
791         return (ENODEV);
792 }
793
794 static int
795 creator_load_cursor_palette(video_adapter_t *adp, u_char *palette)
796 {
797
798         return (ENODEV);
799 }
800
801 static int
802 creator_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
803 {
804
805         return (ENODEV);
806 }
807
808 static int
809 creator_putp(video_adapter_t *adp, vm_offset_t off, u_int32_t p, u_int32_t a,
810     int size, int bpp, int bit_ltor, int byte_ltor)
811 {
812
813         return (ENODEV);
814 }
815
816 static int
817 creator_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
818 {
819         struct creator_softc *sc;
820         const uint16_t *p;
821         int row;
822         int col;
823         int i;
824
825         sc = (struct creator_softc *)adp;
826         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
827         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
828         p = (const uint16_t *)sc->sc_font + (c * adp->va_info.vi_cheight);
829         creator_ras_setfg(sc, creator_cmap[a & 0xf]);
830         creator_ras_setbg(sc, creator_cmap[(a >> 4) & 0xf]);
831         creator_ras_fifo_wait(sc, 1 + adp->va_info.vi_cheight);
832         FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONTXY,
833             ((row + sc->sc_ymargin) << 16) | (col + sc->sc_xmargin));
834         creator_ras_setfontw(sc, adp->va_info.vi_cwidth);
835         creator_ras_setfontinc(sc, 0x10000);
836         for (i = 0; i < adp->va_info.vi_cheight; i++) {
837                 FFB_WRITE(sc, FFB_FBC, FFB_FBC_FONT, *p++ << 16);
838         }
839         return (0);
840 }
841
842 static int
843 creator_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
844 {
845         int i;
846
847         for (i = 0; i < len; i++) {
848                 (*vidsw[adp->va_index]->putc)(adp, off + i, s[i] & 0xff,
849                     (s[i] & 0xff00) >> 8);
850         }
851         return (0);
852 }
853
854 static int
855 creator_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
856     u_int32_t pixel_mask, int size, int width)
857 {
858         struct creator_softc *sc;
859
860         sc = (struct creator_softc *)adp;
861         if (!(sc->sc_flags & CREATOR_CUREN)) {
862                 creator_cursor_install(sc);
863                 creator_cursor_enable(sc, 1);
864                 sc->sc_flags |= CREATOR_CUREN;
865         }
866         FFB_WRITE(sc, FFB_DAC, FFB_DAC_TYPE2, FFB_DAC_CUR_POS);
867         FFB_WRITE(sc, FFB_DAC, FFB_DAC_VALUE2,
868             ((y + sc->sc_ymargin) << 16) | (x + sc->sc_xmargin));
869         return (0);
870 }
871
872 /*
873  * bus interface
874  */
875 static int
876 creator_bus_probe(device_t dev)
877 {
878         const char *name;
879         phandle_t node;
880         int type;
881
882         name = ofw_bus_get_name(dev);
883         node = ofw_bus_get_node(dev);
884         if (strcmp(name, "SUNW,ffb") == 0) {
885                 if (OF_getprop(node, "board_type", &type, sizeof(type)) == -1)
886                         return (ENXIO);
887                 switch (type & 7) {
888                 case 0x0:
889                         device_set_desc(dev, "Creator");
890                         break;
891                 case 0x3:
892                         device_set_desc(dev, "Creator3D");
893                         break;
894                 default:
895                         return (ENXIO);
896                 }
897         } else if (strcmp(name, "SUNW,afb") == 0)
898                 device_set_desc(dev, "Elite3D");
899         else
900                 return (ENXIO);
901         return (BUS_PROBE_DEFAULT);
902 }
903
904 static int
905 creator_bus_attach(device_t dev)
906 {
907         struct creator_softc *sc;
908         video_adapter_t *adp;
909         video_switch_t *sw;
910         phandle_t node;
911         int error;
912         int rid;
913         int unit;
914         int i;
915
916         node = ofw_bus_get_node(dev);
917         if ((sc = (struct creator_softc *)vid_get_adapter(vid_find_adapter(
918             CREATOR_DRIVER_NAME, 0))) != NULL && sc->sc_node == node) {
919                 device_printf(dev, "console\n");
920                 device_set_softc(dev, sc);
921         } else {
922                 sc = device_get_softc(dev);
923                 sc->sc_node = node;
924         }
925         adp = &sc->sc_va;
926
927         /*
928          * Allocate resources regardless of whether we are the console
929          * and already obtained the bus tags and handles for the FFB_DAC
930          * and FFB_FBC register banks in creator_configure() or not so
931          * the resources are marked as taken in the respective RMAN.
932          * The supported cards use either 15 (Creator, Elite3D?) or 24
933          * (Creator3D?) register banks. We make sure that we can also
934          * allocate the resources for at least the FFB_DAC and FFB_FBC
935          * banks here. We try but don't actually care whether we can
936          * allocate more than these two resources and just limit the
937          * range accessible via creator_fb_mmap() accordingly.
938          */
939         for (i = 0; i < FFB_NREG; i++) {
940                 rid = i;
941                 sc->sc_reg[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
942                     &rid, RF_ACTIVE);
943                 if (sc->sc_reg[i] == NULL) {
944                         if (i <= FFB_FBC) {
945                                 device_printf(dev,
946                                     "cannot allocate resources\n");
947                                 error = ENXIO;
948                                 goto fail;
949                         }
950                         break;
951                 }
952                 sc->sc_bt[i] = rman_get_bustag(sc->sc_reg[i]);
953                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_reg[i]);
954         }
955         /*
956          * The XFree86/Xorg sunffb(4) expects to be able to access the
957          * memory spanned by the first and the last resource as one chunk
958          * via creator_fb_mmap(), using offsets from the first resource,
959          * even though the backing resources are actually non-continuous.
960          * So make sure that the memory we provide is at least backed by
961          * increasing resources.
962          */
963         adp->va_mem_base = rman_get_start(sc->sc_reg[0]);
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         adp->va_mem_size = rman_get_end(sc->sc_reg[i - 1]) -
969             adp->va_mem_base + 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_offset_t offset, vm_paddr_t *paddr,
1054     int prot)
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/Xorg 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_va.va_mem_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 }