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