radeonfb.c revision 1.97 1 /* $NetBSD: radeonfb.c,v 1.97 2018/06/15 21:22:35 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Garrett D'Amore for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * ATI Technologies Inc. ("ATI") has not assisted in the creation of, and
36 * does not endorse, this software. ATI will not be responsible or liable
37 * for any actual or alleged damage or loss caused by or in connection with
38 * the use of or reliance on this software.
39 */
40
41 /*
42 * Portions of this code were taken from XFree86's Radeon driver, which bears
43 * this notice:
44 *
45 * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
46 * VA Linux Systems Inc., Fremont, California.
47 *
48 * All Rights Reserved.
49 *
50 * Permission is hereby granted, free of charge, to any person obtaining
51 * a copy of this software and associated documentation files (the
52 * "Software"), to deal in the Software without restriction, including
53 * without limitation on the rights to use, copy, modify, merge,
54 * publish, distribute, sublicense, and/or sell copies of the Software,
55 * and to permit persons to whom the Software is furnished to do so,
56 * subject to the following conditions:
57 *
58 * The above copyright notice and this permission notice (including the
59 * next paragraph) shall be included in all copies or substantial
60 * portions of the Software.
61 *
62 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
63 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
64 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
65 * NON-INFRINGEMENT. IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
66 * THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
67 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
68 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
69 * DEALINGS IN THE SOFTWARE.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: radeonfb.c,v 1.97 2018/06/15 21:22:35 macallan Exp $");
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/device.h>
78 #include <sys/malloc.h>
79 #include <sys/bus.h>
80 #include <sys/kernel.h>
81 #include <sys/lwp.h>
82 #include <sys/kauth.h>
83
84 #include <dev/wscons/wsdisplayvar.h>
85 #include <dev/wscons/wsconsio.h>
86 #include <dev/wsfont/wsfont.h>
87 #include <dev/rasops/rasops.h>
88 #include <dev/videomode/videomode.h>
89 #include <dev/videomode/edidvar.h>
90 #include <dev/wscons/wsdisplay_vconsvar.h>
91 #include <dev/pci/wsdisplay_pci.h>
92 #include <dev/wscons/wsdisplay_glyphcachevar.h>
93
94 #include <dev/pci/pcidevs.h>
95 #include <dev/pci/pcireg.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/pci/pciio.h>
98 #include <dev/pci/radeonfbreg.h>
99 #include <dev/pci/radeonfbvar.h>
100 #include "opt_radeonfb.h"
101 #include "opt_vcons.h"
102
103 #ifdef RADEONFB_DEPTH_32
104 #define RADEONFB_DEFAULT_DEPTH 32
105 #else
106 #define RADEONFB_DEFAULT_DEPTH 8
107 #endif
108
109 static int radeonfb_match(device_t, cfdata_t, void *);
110 static void radeonfb_attach(device_t, device_t, void *);
111 static int radeonfb_ioctl(void *, void *, unsigned long, void *, int,
112 struct lwp *);
113 static paddr_t radeonfb_mmap(void *, void *, off_t, int);
114 static int radeonfb_scratch_test(struct radeonfb_softc *, int, uint32_t);
115 static void radeonfb_loadbios(struct radeonfb_softc *,
116 const struct pci_attach_args *);
117
118 static uintmax_t radeonfb_getprop_num(struct radeonfb_softc *, const char *,
119 uintmax_t);
120 static int radeonfb_getclocks(struct radeonfb_softc *);
121 static int radeonfb_gettmds(struct radeonfb_softc *);
122 static int radeonfb_calc_dividers(struct radeonfb_softc *, uint32_t,
123 uint32_t *, uint32_t *, int);
124 /* flags for radeonfb_calc_dividers */
125 #define NO_ODD_FBDIV 1
126
127 static int radeonfb_getconnectors(struct radeonfb_softc *);
128 static const struct videomode *radeonfb_modelookup(const char *);
129 static void radeonfb_init_screen(void *, struct vcons_screen *, int, long *);
130 static void radeonfb_pllwriteupdate(struct radeonfb_softc *, int);
131 static void radeonfb_pllwaitatomicread(struct radeonfb_softc *, int);
132 static void radeonfb_program_vclk(struct radeonfb_softc *, int, int, int);
133 static void radeonfb_modeswitch(struct radeonfb_display *);
134 static void radeonfb_setcrtc(struct radeonfb_display *, int);
135 static void radeonfb_init_misc(struct radeonfb_softc *);
136 static void radeonfb_set_fbloc(struct radeonfb_softc *);
137 static void radeonfb_init_palette(struct radeonfb_display *);
138 static void radeonfb_r300cg_workaround(struct radeonfb_softc *);
139
140 static int radeonfb_isblank(struct radeonfb_display *);
141 static void radeonfb_blank(struct radeonfb_display *, int);
142 static int radeonfb_set_cursor(struct radeonfb_display *,
143 struct wsdisplay_cursor *);
144 static int radeonfb_set_curpos(struct radeonfb_display *,
145 struct wsdisplay_curpos *);
146 static void radeonfb_putpal(struct radeonfb_display *, int, int, int, int);
147 static int radeonfb_putcmap(struct radeonfb_display *, struct wsdisplay_cmap *);
148 static int radeonfb_getcmap(struct radeonfb_display *, struct wsdisplay_cmap *);
149
150 /* acceleration support */
151 static void radeonfb_rectfill(struct radeonfb_display *, int dstx, int dsty,
152 int width, int height, uint32_t color);
153 static void radeonfb_rectfill_a(void *, int, int, int, int, long);
154 static void radeonfb_bitblt(void *, int srcx, int srcy,
155 int dstx, int dsty, int width, int height, int rop);
156
157 /* hw cursor support */
158 static void radeonfb_cursor_cmap(struct radeonfb_display *);
159 static void radeonfb_cursor_shape(struct radeonfb_display *);
160 static void radeonfb_cursor_position(struct radeonfb_display *);
161 static void radeonfb_cursor_visible(struct radeonfb_display *);
162 static void radeonfb_cursor_update(struct radeonfb_display *, unsigned);
163
164 static inline void radeonfb_wait_fifo(struct radeonfb_softc *, int);
165 static void radeonfb_engine_idle(struct radeonfb_softc *);
166 static void radeonfb_engine_flush(struct radeonfb_softc *);
167 static void radeonfb_engine_reset(struct radeonfb_softc *);
168 static void radeonfb_engine_init(struct radeonfb_display *);
169 static inline void radeonfb_unclip(struct radeonfb_softc *) __unused;
170
171 static void radeonfb_eraserows(void *, int, int, long);
172 static void radeonfb_erasecols(void *, int, int, int, long);
173 static void radeonfb_copyrows(void *, int, int, int);
174 static void radeonfb_copycols(void *, int, int, int, int);
175 static void radeonfb_cursor(void *, int, int, int);
176 static void radeonfb_putchar(void *, int, int, unsigned, long);
177 static void radeonfb_putchar_aa32(void *, int, int, unsigned, long);
178 static void radeonfb_putchar_aa8(void *, int, int, unsigned, long);
179 #ifndef RADEONFB_ALWAYS_ACCEL_PUTCHAR
180 static void radeonfb_putchar_wrapper(void *, int, int, unsigned, long);
181 #endif
182
183 static int radeonfb_set_backlight(struct radeonfb_display *, int);
184 static int radeonfb_get_backlight(struct radeonfb_display *);
185 static void radeonfb_switch_backlight(struct radeonfb_display *, int);
186 static void radeonfb_lvds_callout(void *);
187
188 static void radeonfb_brightness_up(device_t);
189 static void radeonfb_brightness_down(device_t);
190
191 static struct videomode *radeonfb_best_refresh(struct videomode *,
192 struct videomode *);
193 static void radeonfb_pickres(struct radeonfb_display *, uint16_t *,
194 uint16_t *, int);
195 static const struct videomode *radeonfb_port_mode(struct radeonfb_softc *,
196 struct radeonfb_port *, int, int);
197
198 static int radeonfb_drm_print(void *, const char *);
199
200 #ifdef RADEONFB_DEBUG
201 int radeon_debug = 1;
202 #define DPRINTF(x) \
203 if (radeon_debug) printf x
204 #define PRINTREG(r) DPRINTF((#r " = %08x\n", GET32(sc, r)))
205 #define PRINTPLL(r) DPRINTF((#r " = %08x\n", GETPLL(sc, r)))
206 #else
207 #define DPRINTF(x)
208 #define PRINTREG(r)
209 #define PRINTPLL(r)
210 #endif
211
212 #define ROUNDUP(x,y) (((x) + ((y) - 1)) & ~((y) - 1))
213
214 #ifndef RADEON_DEFAULT_MODE
215 /* any reasonably modern display should handle this */
216 #define RADEON_DEFAULT_MODE "1024x768x60"
217 #endif
218
219 extern const u_char rasops_cmap[768];
220
221 const char *radeonfb_default_mode = RADEON_DEFAULT_MODE;
222
223 static struct {
224 int size; /* minimum memory size (MB) */
225 int maxx; /* maximum x dimension */
226 int maxy; /* maximum y dimension */
227 int maxbpp; /* maximum bpp */
228 int maxdisp; /* maximum logical display count */
229 } radeonfb_limits[] = {
230 { 32, 2048, 1536, 32, 2 },
231 { 16, 1600, 1200, 32, 2 },
232 { 8, 1600, 1200, 32, 1 },
233 { 0, 0, 0, 0, 0 },
234 };
235
236 static struct wsscreen_descr radeonfb_stdscreen = {
237 "fb", /* name */
238 0, 0, /* ncols, nrows */
239 NULL, /* textops */
240 8, 16, /* fontwidth, fontheight */
241 WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE | WSSCREEN_RESIZE, /* capabilities */
242 0, /* modecookie */
243 };
244
245 struct wsdisplay_accessops radeonfb_accessops = {
246 radeonfb_ioctl,
247 radeonfb_mmap,
248 NULL, /* vcons_alloc_screen */
249 NULL, /* vcons_free_screen */
250 NULL, /* vcons_show_screen */
251 NULL, /* load_font */
252 NULL, /* pollc */
253 NULL, /* scroll */
254 };
255
256 static struct {
257 uint16_t devid;
258 uint16_t family;
259 uint16_t flags;
260 } radeonfb_devices[] =
261 {
262 /* R100 family */
263 { PCI_PRODUCT_ATI_RADEON_R100_QD, RADEON_R100, 0 },
264 { PCI_PRODUCT_ATI_RADEON_R100_QE, RADEON_R100, 0 },
265 { PCI_PRODUCT_ATI_RADEON_R100_QF, RADEON_R100, 0 },
266 { PCI_PRODUCT_ATI_RADEON_R100_QG, RADEON_R100, 0 },
267
268 /* RV100 family */
269 { PCI_PRODUCT_ATI_RADEON_RV100_LY, RADEON_RV100, RFB_MOB },
270 { PCI_PRODUCT_ATI_RADEON_RV100_LZ, RADEON_RV100, RFB_MOB },
271 { PCI_PRODUCT_ATI_RADEON_RV100_QY, RADEON_RV100, 0 },
272 { PCI_PRODUCT_ATI_RADEON_RV100_QZ, RADEON_RV100, 0 },
273
274 /* RS100 family */
275 { PCI_PRODUCT_ATI_RADEON_RS100_4136, RADEON_RS100, 0 },
276 { PCI_PRODUCT_ATI_RADEON_RS100_4336, RADEON_RS100, RFB_MOB },
277
278 /* RS200/RS250 family */
279 { PCI_PRODUCT_ATI_RADEON_RS200_4337, RADEON_RS200, RFB_MOB },
280 { PCI_PRODUCT_ATI_RADEON_RS200_A7, RADEON_RS200, 0 },
281 { PCI_PRODUCT_ATI_RADEON_RS250_B7, RADEON_RS200, RFB_MOB },
282 { PCI_PRODUCT_ATI_RADEON_RS250_D7, RADEON_RS200, 0 },
283
284 /* R200 family */
285 /* add more R200 products? , 5148 */
286 { PCI_PRODUCT_ATI_RADEON_R200_BB, RADEON_R200, 0 },
287 { PCI_PRODUCT_ATI_RADEON_R200_BC, RADEON_R200, 0 },
288 { PCI_PRODUCT_ATI_RADEON_R200_QH, RADEON_R200, 0 },
289 { PCI_PRODUCT_ATI_RADEON_R200_QL, RADEON_R200, 0 },
290 { PCI_PRODUCT_ATI_RADEON_R200_QM, RADEON_R200, 0 },
291
292 /* RV200 family */
293 { PCI_PRODUCT_ATI_RADEON_RV200_LW, RADEON_RV200, RFB_MOB },
294 { PCI_PRODUCT_ATI_RADEON_RV200_LX, RADEON_RV200, RFB_MOB },
295 { PCI_PRODUCT_ATI_RADEON_RV200_QW, RADEON_RV200, 0 },
296 { PCI_PRODUCT_ATI_RADEON_RV200_QX, RADEON_RV200, 0 },
297
298 /* RV250 family */
299 { PCI_PRODUCT_ATI_RADEON_RV250_4966, RADEON_RV250, 0 },
300 { PCI_PRODUCT_ATI_RADEON_RV250_4967, RADEON_RV250, 0 },
301 { PCI_PRODUCT_ATI_RADEON_RV250_4C64, RADEON_RV250, RFB_MOB },
302 { PCI_PRODUCT_ATI_RADEON_RV250_4C66, RADEON_RV250, RFB_MOB },
303 { PCI_PRODUCT_ATI_RADEON_RV250_4C67, RADEON_RV250, RFB_MOB },
304
305 /* RS300 family */
306 { PCI_PRODUCT_ATI_RADEON_RS300_X5, RADEON_RS300, 0 },
307 { PCI_PRODUCT_ATI_RADEON_RS300_X4, RADEON_RS300, 0 },
308 { PCI_PRODUCT_ATI_RADEON_RS300_7834, RADEON_RS300, 0 },
309 { PCI_PRODUCT_ATI_RADEON_RS300_7835, RADEON_RS300, RFB_MOB },
310
311 /* RV280 family */
312 { PCI_PRODUCT_ATI_RADEON_RV280_5960, RADEON_RV280, 0 },
313 { PCI_PRODUCT_ATI_RADEON_RV280_5961, RADEON_RV280, 0 },
314 { PCI_PRODUCT_ATI_RADEON_RV280_5962, RADEON_RV280, 0 },
315 { PCI_PRODUCT_ATI_RADEON_RV280_5963, RADEON_RV280, 0 },
316 { PCI_PRODUCT_ATI_RADEON_RV280_5964, RADEON_RV280, 0 },
317 { PCI_PRODUCT_ATI_RADEON_RV280_5C61, RADEON_RV280, RFB_MOB },
318 { PCI_PRODUCT_ATI_RADEON_RV280_5C63, RADEON_RV280, RFB_MOB },
319
320 /* R300 family */
321 { PCI_PRODUCT_ATI_RADEON_R300_AD, RADEON_R300, 0 },
322 { PCI_PRODUCT_ATI_RADEON_R300_AE, RADEON_R300, 0 },
323 { PCI_PRODUCT_ATI_RADEON_R300_AF, RADEON_R300, 0 },
324 { PCI_PRODUCT_ATI_RADEON_R300_AG, RADEON_R300, 0 },
325 { PCI_PRODUCT_ATI_RADEON_R300_ND, RADEON_R300, 0 },
326 { PCI_PRODUCT_ATI_RADEON_R300_NE, RADEON_R300, 0 },
327 { PCI_PRODUCT_ATI_RADEON_R300_NF, RADEON_R300, 0 },
328 { PCI_PRODUCT_ATI_RADEON_R300_NG, RADEON_R300, 0 },
329
330 /* RV350/RV360 family */
331 { PCI_PRODUCT_ATI_RADEON_RV350_AP, RADEON_RV350, 0 },
332 { PCI_PRODUCT_ATI_RADEON_RV350_AQ, RADEON_RV350, 0 },
333 { PCI_PRODUCT_ATI_RADEON_RV360_AR, RADEON_RV350, 0 },
334 { PCI_PRODUCT_ATI_RADEON_RV350_AS, RADEON_RV350, 0 },
335 { PCI_PRODUCT_ATI_RADEON_RV350_AT, RADEON_RV350, 0 },
336 { PCI_PRODUCT_ATI_RADEON_RV350_AV, RADEON_RV350, 0 },
337 { PCI_PRODUCT_ATI_RADEON_RV350_NP, RADEON_RV350, RFB_MOB },
338 { PCI_PRODUCT_ATI_RADEON_RV350_NQ, RADEON_RV350, RFB_MOB },
339 { PCI_PRODUCT_ATI_RADEON_RV350_NR, RADEON_RV350, RFB_MOB },
340 { PCI_PRODUCT_ATI_RADEON_RV350_NS, RADEON_RV350, RFB_MOB },
341 { PCI_PRODUCT_ATI_RADEON_RV350_NT, RADEON_RV350, RFB_MOB },
342 { PCI_PRODUCT_ATI_RADEON_RV350_NV, RADEON_RV350, RFB_MOB },
343
344 /* R350/R360 family */
345 { PCI_PRODUCT_ATI_RADEON_R350_AH, RADEON_R350, 0 },
346 { PCI_PRODUCT_ATI_RADEON_R350_AI, RADEON_R350, 0 },
347 { PCI_PRODUCT_ATI_RADEON_R350_AJ, RADEON_R350, 0 },
348 { PCI_PRODUCT_ATI_RADEON_R350_AK, RADEON_R350, 0 },
349 { PCI_PRODUCT_ATI_RADEON_R350_NH, RADEON_R350, 0 },
350 { PCI_PRODUCT_ATI_RADEON_R350_NI, RADEON_R350, 0 },
351 { PCI_PRODUCT_ATI_RADEON_R350_NK, RADEON_R350, 0 },
352 { PCI_PRODUCT_ATI_RADEON_R360_NJ, RADEON_R350, 0 },
353
354 /* RV380/RV370 family */
355 { PCI_PRODUCT_ATI_RADEON_RV380_3150, RADEON_RV380, RFB_MOB },
356 { PCI_PRODUCT_ATI_RADEON_RV380_3154, RADEON_RV380, RFB_MOB },
357 { PCI_PRODUCT_ATI_RADEON_RV380_3E50, RADEON_RV380, 0 },
358 { PCI_PRODUCT_ATI_RADEON_RV380_3E54, RADEON_RV380, 0 },
359 { PCI_PRODUCT_ATI_RADEON_RV370_5460, RADEON_RV380, RFB_MOB },
360 { PCI_PRODUCT_ATI_RADEON_RV370_5464, RADEON_RV380, RFB_MOB },
361 { PCI_PRODUCT_ATI_RADEON_RV370_5B60, RADEON_RV380, 0 },
362 { PCI_PRODUCT_ATI_RADEON_RV370_5B63, RADEON_RV380, 0 },
363 { PCI_PRODUCT_ATI_RADEON_RV370_5B64, RADEON_RV380, 0 },
364 { PCI_PRODUCT_ATI_RADEON_RV370_5B65, RADEON_RV380, 0 },
365
366 #if notyet
367 /* R420/R423 family */
368 { PCI_PRODUCT_ATI_RADEON_R420_JH, RADEON_R420, 0 },
369 { PCI_PRODUCT_ATI_RADEON_R420_JI, RADEON_R420, 0 },
370 { PCI_PRODUCT_ATI_RADEON_R420_JJ, RADEON_R420, 0 },
371 { PCI_PRODUCT_ATI_RADEON_R420_JK, RADEON_R420, 0 },
372 { PCI_PRODUCT_ATI_RADEON_R420_JL, RADEON_R420, 0 },
373 { PCI_PRODUCT_ATI_RADEON_R420_JM, RADEON_R420, 0 },
374 { PCI_PRODUCT_ATI_RADEON_R420_JN, RADEON_R420, RFB_MOB },
375 { PCI_PRODUCT_ATI_RADEON_R420_JP, RADEON_R420, 0 },
376 { PCI_PRODUCT_ATI_RADEON_R423_UH, RADEON_R420, 0 },
377 { PCI_PRODUCT_ATI_RADEON_R423_UI, RADEON_R420, 0 },
378 { PCI_PRODUCT_ATI_RADEON_R423_UJ, RADEON_R420, 0 },
379 { PCI_PRODUCT_ATI_RADEON_R423_UK, RADEON_R420, 0 },
380 { PCI_PRODUCT_ATI_RADEON_R423_UQ, RADEON_R420, 0 },
381 { PCI_PRODUCT_ATI_RADEON_R423_UR, RADEON_R420, 0 },
382 { PCI_PRODUCT_ATI_RADEON_R423_UT, RADEON_R420, 0 },
383 { PCI_PRODUCT_ATI_RADEON_R423_5D57, RADEON_R420, 0 },
384 { PCI_PRODUCT_ATI_RADEON_R430_554F, RADEON_R420, 0 },
385
386 /* R5xx family */
387 { 0x7240, RADEON_R420, 0 },
388 #endif
389 { 0, 0, 0 }
390 };
391
392 static struct {
393 int divider;
394 int mask;
395 } radeonfb_dividers[] = {
396 { 16, 5 },
397 { 12, 7 },
398 { 8, 3 },
399 { 6, 6 },
400 { 4, 2 },
401 { 3, 4 },
402 { 2, 1 },
403 { 1, 0 },
404 { 0, 0 }
405 };
406
407 /*
408 * This table taken from X11.
409 */
410 static const struct {
411 int family;
412 struct radeon_tmds_pll plls[4];
413 } radeonfb_tmds_pll[] = {
414 { RADEON_R100, {{12000, 0xa1b}, {-1, 0xa3f}}},
415 { RADEON_RV100, {{12000, 0xa1b}, {-1, 0xa3f}}},
416 { RADEON_RS100, {{0, 0}}},
417 { RADEON_RV200, {{15000, 0xa1b}, {-1, 0xa3f}}},
418 { RADEON_RS200, {{15000, 0xa1b}, {-1, 0xa3f}}},
419 { RADEON_R200, {{15000, 0xa1b}, {-1, 0xa3f}}},
420 { RADEON_RV250, {{15500, 0x81b}, {-1, 0x83f}}},
421 { RADEON_RS300, {{0, 0}}},
422 { RADEON_RV280, {{13000, 0x400f4}, {15000, 0x400f7}, {-1, 0x40111}}},
423 { RADEON_R300, {{-1, 0xb01cb}}},
424 { RADEON_R350, {{-1, 0xb01cb}}},
425 { RADEON_RV350, {{15000, 0xb0155}, {-1, 0xb01cb}}},
426 { RADEON_RV380, {{15000, 0xb0155}, {-1, 0xb01cb}}},
427 { RADEON_R420, {{-1, 0xb01cb}}},
428 };
429
430 #define RADEONFB_BACKLIGHT_MAX 255 /* Maximum backlight level. */
431
432
433 CFATTACH_DECL_NEW(radeonfb, sizeof (struct radeonfb_softc),
434 radeonfb_match, radeonfb_attach, NULL, NULL);
435
436 static int
437 radeonfb_match(device_t parent, cfdata_t match, void *aux)
438 {
439 const struct pci_attach_args *pa = aux;
440 int i;
441
442 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
443 return 0;
444
445 for (i = 0; radeonfb_devices[i].devid; i++) {
446 if (PCI_PRODUCT(pa->pa_id) == radeonfb_devices[i].devid)
447 return 100; /* high to defeat VGA/VESA */
448 }
449
450 return 0;
451 }
452
453 static void
454 radeonfb_attach(device_t parent, device_t dev, void *aux)
455 {
456 struct radeonfb_softc *sc = device_private(dev);
457 const struct pci_attach_args *pa = aux;
458 const char *mptr;
459 bus_size_t bsz;
460 pcireg_t screg;
461 int i, j, fg, bg, ul, flags;
462 uint32_t v;
463
464 sc->sc_dev = dev;
465 sc->sc_id = pa->pa_id;
466 for (i = 0; radeonfb_devices[i].devid; i++) {
467 if (PCI_PRODUCT(sc->sc_id) == radeonfb_devices[i].devid)
468 break;
469 }
470
471 pci_aprint_devinfo(pa, NULL);
472
473 DPRINTF(("%s", prop_dictionary_externalize(device_properties(dev))));
474
475 KASSERT(radeonfb_devices[i].devid != 0);
476 sc->sc_pt = pa->pa_tag;
477 sc->sc_iot = pa->pa_iot;
478 sc->sc_pc = pa->pa_pc;
479 sc->sc_family = radeonfb_devices[i].family;
480 sc->sc_flags = radeonfb_devices[i].flags;
481
482 /* enable memory and IO access */
483 screg = pci_conf_read(sc->sc_pc, sc->sc_pt, PCI_COMMAND_STATUS_REG);
484 screg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
485 pci_conf_write(sc->sc_pc, sc->sc_pt, PCI_COMMAND_STATUS_REG, screg);
486
487 /*
488 * Some flags are general to entire chip families, and rather
489 * than clutter up the table with them, we go ahead and set
490 * them here.
491 */
492 switch (sc->sc_family) {
493 case RADEON_RS100:
494 case RADEON_RS200:
495 sc->sc_flags |= RFB_IGP | RFB_RV100;
496 break;
497
498 case RADEON_RV100:
499 case RADEON_RV200:
500 case RADEON_RV250:
501 case RADEON_RV280:
502 sc->sc_flags |= RFB_RV100;
503 break;
504
505 case RADEON_RS300:
506 sc->sc_flags |= RFB_SDAC | RFB_IGP | RFB_RV100;
507 break;
508
509 case RADEON_R300:
510 case RADEON_RV350:
511 case RADEON_R350:
512 case RADEON_RV380:
513 case RADEON_R420:
514 /* newer chips */
515 sc->sc_flags |= RFB_R300;
516 break;
517
518 case RADEON_R100:
519 sc->sc_flags |= RFB_NCRTC2;
520 break;
521 }
522
523 if ((sc->sc_family == RADEON_RV200) ||
524 (sc->sc_family == RADEON_RV250) ||
525 (sc->sc_family == RADEON_RV280) ||
526 (sc->sc_family == RADEON_RV350)) {
527 bool inverted = 0;
528 /* backlight level is linear */
529 DPRINTF(("found RV* chip, backlight is supposedly linear\n"));
530 prop_dictionary_get_bool(device_properties(sc->sc_dev),
531 "backlight_level_reverted", &inverted);
532 if (inverted) {
533 DPRINTF(("nope, it's inverted\n"));
534 sc->sc_flags |= RFB_INV_BLIGHT;
535 }
536 } else
537 sc->sc_flags |= RFB_INV_BLIGHT;
538
539 /*
540 * XXX: to support true multihead, this must change.
541 */
542 sc->sc_ndisplays = 1;
543
544 /* XXX: */
545 if (!HAS_CRTC2(sc)) {
546 sc->sc_ndisplays = 1;
547 }
548
549 if (pci_mapreg_map(pa, RADEON_MAPREG_MMIO, PCI_MAPREG_TYPE_MEM, 0,
550 &sc->sc_regt, &sc->sc_regh, &sc->sc_regaddr,
551 &sc->sc_regsz) != 0) {
552 aprint_error("%s: unable to map registers!\n", XNAME(sc));
553 goto error;
554 }
555
556 if (pci_mapreg_info(sc->sc_pc, sc->sc_pt, PCI_MAPREG_ROM,
557 PCI_MAPREG_TYPE_ROM, &sc->sc_romaddr, &sc->sc_romsz, &flags) != 0)
558 {
559 aprint_error("%s: unable to find ROM!\n", XNAME(sc));
560 goto error;
561 }
562 sc->sc_romt = sc->sc_memt;
563
564 sc->sc_mapped = TRUE;
565
566 /* scratch register test... */
567 if (radeonfb_scratch_test(sc, RADEON_BIOS_0_SCRATCH, 0x55555555) ||
568 radeonfb_scratch_test(sc, RADEON_BIOS_0_SCRATCH, 0xaaaaaaaa)) {
569 aprint_error("%s: scratch register test failed!\n", XNAME(sc));
570 goto error;
571 }
572
573 PRINTREG(RADEON_CRTC_EXT_CNTL);
574 PRINTREG(RADEON_CRTC_GEN_CNTL);
575 PRINTREG(RADEON_CRTC2_GEN_CNTL);
576 PRINTREG(RADEON_DISP_OUTPUT_CNTL);
577 PRINTREG(RADEON_DAC_CNTL2);
578 PRINTREG(RADEON_BIOS_4_SCRATCH);
579 PRINTREG(RADEON_FP_GEN_CNTL);
580 sc->sc_fp_gen_cntl = GET32(sc, RADEON_FP_GEN_CNTL);
581 PRINTREG(RADEON_FP2_GEN_CNTL);
582 PRINTREG(RADEON_TMDS_CNTL);
583 PRINTREG(RADEON_TMDS_TRANSMITTER_CNTL);
584 PRINTREG(RADEON_TMDS_PLL_CNTL);
585 PRINTREG(RADEON_LVDS_GEN_CNTL);
586 PRINTREG(RADEON_DISP_HW_DEBUG);
587 PRINTREG(RADEON_PIXCLKS_CNTL);
588 PRINTREG(RADEON_CRTC_H_SYNC_STRT_WID);
589 PRINTREG(RADEON_FP_H_SYNC_STRT_WID);
590 PRINTREG(RADEON_CRTC2_H_SYNC_STRT_WID);
591 PRINTREG(RADEON_FP_H2_SYNC_STRT_WID);
592 if (IS_RV100(sc))
593 PUT32(sc, RADEON_TMDS_PLL_CNTL, 0xa27);
594
595 /* XXX
596 * according to xf86-video-radeon R3xx has this bit backwards
597 */
598 if (IS_R300(sc)) {
599 PATCH32(sc, RADEON_TMDS_TRANSMITTER_CNTL,
600 0,
601 ~(RADEON_TMDS_TRANSMITTER_PLLEN | RADEON_TMDS_TRANSMITTER_PLLRST));
602 } else {
603 PATCH32(sc, RADEON_TMDS_TRANSMITTER_CNTL,
604 RADEON_TMDS_TRANSMITTER_PLLEN,
605 ~(RADEON_TMDS_TRANSMITTER_PLLEN | RADEON_TMDS_TRANSMITTER_PLLRST));
606 }
607
608 radeonfb_i2c_init(sc);
609
610 radeonfb_loadbios(sc, pa);
611
612 #ifdef RADEONFB_BIOS_INIT
613 if (radeonfb_bios_init(sc)) {
614 aprint_error("%s: BIOS inititialization failed\n", XNAME(sc));
615 }
616 #endif
617
618 if (radeonfb_getclocks(sc)) {
619 aprint_error("%s: Unable to get reference clocks from BIOS\n",
620 XNAME(sc));
621 goto error;
622 }
623
624 if (radeonfb_gettmds(sc)) {
625 aprint_error("%s: Unable to identify TMDS PLL settings\n",
626 XNAME(sc));
627 goto error;
628 }
629
630 aprint_verbose("%s: refclk = %d.%03d MHz, refdiv = %d "
631 "minpll = %d, maxpll = %d\n", XNAME(sc),
632 (int)sc->sc_refclk / 1000, (int)sc->sc_refclk % 1000,
633 (int)sc->sc_refdiv, (int)sc->sc_minpll, (int)sc->sc_maxpll);
634
635 radeonfb_getconnectors(sc);
636
637 radeonfb_set_fbloc(sc);
638
639 /* 64 MB should be enough -- more just wastes map entries */
640 if (sc->sc_memsz > (64 << 20))
641 sc->sc_memsz = (64 << 20);
642
643 for (i = 0; radeonfb_limits[i].size; i++) {
644 if (sc->sc_memsz >= radeonfb_limits[i].size) {
645 sc->sc_maxx = radeonfb_limits[i].maxx;
646 sc->sc_maxy = radeonfb_limits[i].maxy;
647 sc->sc_maxbpp = radeonfb_limits[i].maxbpp;
648 /* framebuffer offset, start at a 4K page */
649 sc->sc_fboffset = sc->sc_memsz /
650 radeonfb_limits[i].maxdisp;
651 /*
652 * we use the fbsize to figure out where we can store
653 * things like cursor data.
654 */
655 sc->sc_fbsize =
656 ROUNDUP(ROUNDUP(sc->sc_maxx * sc->sc_maxbpp / 8 ,
657 RADEON_STRIDEALIGN) * sc->sc_maxy,
658 4096);
659 break;
660 }
661 }
662
663
664 radeonfb_init_misc(sc);
665
666 /* program the DAC wirings */
667 for (i = 0; i < (HAS_CRTC2(sc) ? 2 : 1); i++) {
668 switch (sc->sc_ports[i].rp_dac_type) {
669 case RADEON_DAC_PRIMARY:
670 PATCH32(sc, RADEON_DAC_CNTL2,
671 i ? RADEON_DAC2_DAC_CLK_SEL : 0,
672 ~RADEON_DAC2_DAC_CLK_SEL);
673 break;
674 case RADEON_DAC_TVDAC:
675 /* we always use the TVDAC to drive a secondary analog
676 * CRT for now. if we ever support TV-out this will
677 * have to change.
678 */
679 SET32(sc, RADEON_DAC_CNTL2,
680 RADEON_DAC2_DAC2_CLK_SEL);
681 PATCH32(sc, RADEON_DISP_HW_DEBUG,
682 i ? 0 : RADEON_CRT2_DISP1_SEL,
683 ~RADEON_CRT2_DISP1_SEL);
684 /* we're using CRTC2 for the 2nd port */
685 if (sc->sc_ports[i].rp_number == 1) {
686 PATCH32(sc, RADEON_DISP_OUTPUT_CNTL,
687 RADEON_DISP_DAC2_SOURCE_CRTC2,
688 ~RADEON_DISP_DAC2_SOURCE_MASK);
689 }
690
691 break;
692 }
693 DPRINTF(("%s: port %d tmds type %d\n", __func__, i,
694 sc->sc_ports[i].rp_tmds_type));
695 switch (sc->sc_ports[i].rp_tmds_type) {
696 case RADEON_TMDS_INT:
697 /* point FP0 at the CRTC this port uses */
698 DPRINTF(("%s: plugging internal TMDS into CRTC %d\n",
699 __func__, sc->sc_ports[i].rp_number));
700 if (IS_R300(sc)) {
701 PATCH32(sc, RADEON_FP_GEN_CNTL,
702 sc->sc_ports[i].rp_number ?
703 R200_FP_SOURCE_SEL_CRTC2 :
704 R200_FP_SOURCE_SEL_CRTC1,
705 ~R200_FP_SOURCE_SEL_MASK);
706 } else {
707 PATCH32(sc, RADEON_FP_GEN_CNTL,
708 sc->sc_ports[i].rp_number ?
709 RADEON_FP_SEL_CRTC2 :
710 RADEON_FP_SEL_CRTC1,
711 ~RADEON_FP_SEL_MASK);
712 }
713 break;
714 case RADEON_TMDS_EXT:
715 /* point FP2 at the CRTC this port uses */
716 DPRINTF(("%s: plugging external TMDS into CRTC %d\n",
717 __func__, sc->sc_ports[i].rp_number));
718 if (IS_R300(sc)) {
719 PATCH32(sc, RADEON_FP2_GEN_CNTL,
720 sc->sc_ports[i].rp_number ?
721 R200_FP2_SOURCE_SEL_CRTC2 :
722 R200_FP2_SOURCE_SEL_CRTC1,
723 ~R200_FP2_SOURCE_SEL_CRTC2);
724 } else {
725 PATCH32(sc, RADEON_FP2_GEN_CNTL,
726 sc->sc_ports[i].rp_number ?
727 RADEON_FP2_SRC_SEL_CRTC2 :
728 RADEON_FP2_SRC_SEL_CRTC1,
729 ~RADEON_FP2_SRC_SEL_CRTC2);
730 }
731 break;
732 }
733 }
734 PRINTREG(RADEON_DAC_CNTL2);
735 PRINTREG(RADEON_DISP_HW_DEBUG);
736
737 PRINTREG(RADEON_DAC_CNTL);
738 /* other DAC programming */
739 v = GET32(sc, RADEON_DAC_CNTL);
740 v &= (RADEON_DAC_RANGE_CNTL_MASK | RADEON_DAC_BLANKING);
741 v |= RADEON_DAC_MASK_ALL | RADEON_DAC_8BIT_EN;
742 PUT32(sc, RADEON_DAC_CNTL, v);
743 PRINTREG(RADEON_DAC_CNTL);
744
745 /* XXX: this may need more investigation */
746 PUT32(sc, RADEON_TV_DAC_CNTL, 0x00280203);
747 PRINTREG(RADEON_TV_DAC_CNTL);
748
749 /* enable TMDS */
750 SET32(sc, RADEON_FP_GEN_CNTL,
751 RADEON_FP_TMDS_EN |
752 RADEON_FP_CRTC_DONT_SHADOW_VPAR |
753 RADEON_FP_CRTC_DONT_SHADOW_HEND);
754 /*
755 * XXX
756 * no idea why this is necessary - if I do not clear this bit on my
757 * iBook G4 the screen remains black, even though it's already clear.
758 * It needs to be set on my Sun XVR-100 for the DVI port to work
759 * TODO:
760 * see if this is still necessary now that CRTCs, DACs and outputs are
761 * getting wired up in a halfway sane way
762 */
763 if (sc->sc_fp_gen_cntl & RADEON_FP_SEL_CRTC2) {
764 SET32(sc, RADEON_FP_GEN_CNTL, RADEON_FP_SEL_CRTC2);
765 } else {
766 CLR32(sc, RADEON_FP_GEN_CNTL, RADEON_FP_SEL_CRTC2);
767 }
768
769 /*
770 * we use bus_space_map instead of pci_mapreg, because we don't
771 * need the full aperature space. no point in wasting virtual
772 * address space we don't intend to use, right?
773 */
774 if ((sc->sc_memsz < (4096 * 1024)) ||
775 (pci_mapreg_info(sc->sc_pc, sc->sc_pt, RADEON_MAPREG_VRAM,
776 PCI_MAPREG_TYPE_MEM, &sc->sc_memaddr, &bsz, NULL) != 0) ||
777 (bsz < sc->sc_memsz)) {
778 sc->sc_memsz = 0;
779 aprint_error("%s: Bad frame buffer configuration\n",
780 XNAME(sc));
781 goto error;
782 }
783
784 sc->sc_memt = pa->pa_memt;
785 if (bus_space_map(sc->sc_memt, sc->sc_memaddr, sc->sc_memsz,
786 BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
787 sc->sc_memsz = 0;
788 aprint_error("%s: Unable to map frame buffer\n", XNAME(sc));
789 goto error;
790 }
791
792 aprint_normal("%s: %d MB aperture at 0x%08x, "
793 "%d KB registers at 0x%08x\n", XNAME(sc),
794 (int)sc->sc_memsz >> 20, (unsigned)sc->sc_memaddr,
795 (int)sc->sc_regsz >> 10, (unsigned)sc->sc_regaddr);
796
797 /* setup default video mode from devprop (allows PROM override) */
798 sc->sc_defaultmode = radeonfb_default_mode;
799 if (prop_dictionary_get_cstring_nocopy(device_properties(sc->sc_dev),
800 "videomode", &mptr)) {
801
802 strncpy(sc->sc_modebuf, mptr, sizeof(sc->sc_modebuf));
803 sc->sc_defaultmode = sc->sc_modebuf;
804 }
805
806 /* initialize some basic display parameters */
807 for (i = 0; i < sc->sc_ndisplays; i++) {
808 struct radeonfb_display *dp = &sc->sc_displays[i];
809 struct rasops_info *ri;
810 long defattr;
811 struct wsemuldisplaydev_attach_args aa;
812
813 /*
814 * Figure out how many "displays" (desktops) we are going to
815 * support. If more than one, then each CRTC gets its own
816 * programming.
817 *
818 * XXX: this code needs to change to support mergedfb.
819 * XXX: would be nice to allow this to be overridden
820 */
821 if (HAS_CRTC2(sc) && (sc->sc_ndisplays == 1)) {
822 DPRINTF(("dual crtcs!\n"));
823 dp->rd_ncrtcs = 2;
824 dp->rd_crtcs[0].rc_port =
825 &sc->sc_ports[0];
826 dp->rd_crtcs[0].rc_number = sc->sc_ports[0].rp_number;
827 dp->rd_crtcs[1].rc_port =
828 &sc->sc_ports[1];
829 dp->rd_crtcs[1].rc_number = sc->sc_ports[1].rp_number;
830 } else {
831 dp->rd_ncrtcs = 1;
832 dp->rd_crtcs[0].rc_port =
833 &sc->sc_ports[i];
834 dp->rd_crtcs[0].rc_number = sc->sc_ports[i].rp_number;
835 }
836
837 dp->rd_softc = sc;
838 dp->rd_wsmode = WSDISPLAYIO_MODE_EMUL;
839 dp->rd_bpp = RADEONFB_DEFAULT_DEPTH; /* XXX */
840
841 /* for text mode, we pick a resolution that won't
842 * require panning */
843 radeonfb_pickres(dp, &dp->rd_virtx, &dp->rd_virty, 0);
844
845 aprint_normal("%s: display %d: "
846 "initial virtual resolution %dx%d at %d bpp\n",
847 XNAME(sc), i, dp->rd_virtx, dp->rd_virty, dp->rd_bpp);
848 aprint_normal_dev(sc->sc_dev, "using %d MB per display\n",
849 sc->sc_fboffset >> 20);
850 /* now select the *video mode* that we will use */
851 for (j = 0; j < dp->rd_ncrtcs; j++) {
852 const struct videomode *vmp;
853 vmp = radeonfb_port_mode(sc, dp->rd_crtcs[j].rc_port,
854 dp->rd_virtx, dp->rd_virty);
855
856 /*
857 * virtual resolution should be at least as high as
858 * physical
859 */
860 if (dp->rd_virtx < vmp->hdisplay ||
861 dp->rd_virty < vmp->vdisplay) {
862 dp->rd_virtx = vmp->hdisplay;
863 dp->rd_virty = vmp->vdisplay;
864 }
865
866 dp->rd_crtcs[j].rc_videomode = *vmp;
867 printf("%s: port %d: physical %dx%d %dHz\n",
868 XNAME(sc), j, vmp->hdisplay, vmp->vdisplay,
869 DIVIDE(DIVIDE(vmp->dot_clock * 1000,
870 vmp->htotal), vmp->vtotal));
871 }
872
873 /* N.B.: radeon wants 64-byte aligned stride */
874 dp->rd_stride = dp->rd_virtx * dp->rd_bpp / 8;
875 dp->rd_stride = ROUNDUP(dp->rd_stride, RADEON_STRIDEALIGN);
876 DPRINTF(("stride: %d %d\n", dp->rd_stride, dp->rd_virtx));
877
878 dp->rd_offset = sc->sc_fboffset * i;
879 dp->rd_fbptr = (vaddr_t)bus_space_vaddr(sc->sc_memt,
880 sc->sc_memh) + dp->rd_offset;
881 dp->rd_curoff = sc->sc_fboffset - 4096; /* 4KB cursor space */
882 dp->rd_curptr = dp->rd_fbptr + dp->rd_curoff;
883
884 DPRINTF(("fpbtr = %p\n", (void *)dp->rd_fbptr));
885
886 switch (dp->rd_bpp) {
887 case 8:
888 dp->rd_format = 2;
889 break;
890 case 32:
891 dp->rd_format = 6;
892 break;
893 default:
894 aprint_error("%s: bad depth %d\n", XNAME(sc),
895 dp->rd_bpp);
896 goto error;
897 }
898
899 DPRINTF(("init engine\n"));
900 /* XXX: this seems suspicious - per display engine
901 initialization? */
902 radeonfb_engine_init(dp);
903
904 /* copy the template into place */
905 dp->rd_wsscreens_storage[0] = radeonfb_stdscreen;
906 dp->rd_wsscreens = dp->rd_wsscreens_storage;
907
908 /* and make up the list */
909 dp->rd_wsscreenlist.nscreens = 1;
910 dp->rd_wsscreenlist.screens = (void *)&dp->rd_wsscreens;
911
912 vcons_init(&dp->rd_vd, dp, dp->rd_wsscreens,
913 &radeonfb_accessops);
914
915 dp->rd_vd.init_screen = radeonfb_init_screen;
916
917 #ifdef RADEONFB_DEBUG
918 dp->rd_virty -= 200;
919 #endif
920
921 dp->rd_console = 0;
922 prop_dictionary_get_bool(device_properties(sc->sc_dev),
923 "is_console", &dp->rd_console);
924
925 dp->rd_vscreen.scr_flags |= VCONS_SCREEN_IS_STATIC;
926
927
928 vcons_init_screen(&dp->rd_vd, &dp->rd_vscreen,
929 dp->rd_console, &defattr);
930
931 ri = &dp->rd_vscreen.scr_ri;
932
933 /* clear the screen */
934 rasops_unpack_attr(defattr, &fg, &bg, &ul);
935 dp->rd_bg = ri->ri_devcmap[bg & 0xf];
936 radeonfb_rectfill(dp, 0, 0, ri->ri_width, ri->ri_height,
937 dp->rd_bg);
938
939 dp->rd_wsscreens->textops = &ri->ri_ops;
940 dp->rd_wsscreens->capabilities = ri->ri_caps;
941 dp->rd_wsscreens->nrows = ri->ri_rows;
942 dp->rd_wsscreens->ncols = ri->ri_cols;
943
944 #ifdef SPLASHSCREEN
945 dp->rd_splash.si_depth = ri->ri_depth;
946 dp->rd_splash.si_bits = ri->ri_bits;
947 dp->rd_splash.si_hwbits = ri->ri_hwbits;
948 dp->rd_splash.si_width = ri->ri_width;
949 dp->rd_splash.si_height = ri->ri_height;
950 dp->rd_splash.si_stride = ri->ri_stride;
951 dp->rd_splash.si_fillrect = NULL;
952 #endif
953 dp->rd_gc.gc_bitblt = radeonfb_bitblt;
954 dp->rd_gc.gc_rectfill = radeonfb_rectfill_a;
955 dp->rd_gc.gc_rop = RADEON_ROP3_S;
956 dp->rd_gc.gc_blitcookie = dp;
957 /*
958 * use memory between framebuffer and cursor area as glyph
959 * cache, cap at 4096 lines
960 */
961 glyphcache_init(&dp->rd_gc, dp->rd_virty + 4,
962 min(4096,
963 (dp->rd_curoff / dp->rd_stride) - (dp->rd_virty + 4)),
964 dp->rd_virtx,
965 ri->ri_font->fontwidth,
966 ri->ri_font->fontheight,
967 defattr);
968 dp->rd_vd.show_screen_cookie = &dp->rd_gc;
969 dp->rd_vd.show_screen_cb = glyphcache_adapt;
970
971 if (dp->rd_console) {
972
973 radeonfb_modeswitch(dp);
974 wsdisplay_cnattach(dp->rd_wsscreens, ri, 0, 0,
975 defattr);
976 #ifdef SPLASHSCREEN
977 if (splash_render(&dp->rd_splash,
978 SPLASH_F_CENTER|SPLASH_F_FILL) == 0)
979 SCREEN_DISABLE_DRAWING(&dp->rd_vscreen);
980 else
981 #endif
982 vcons_replay_msgbuf(&dp->rd_vscreen);
983 } else {
984
985 /*
986 * since we're not the console we can postpone
987 * the rest until someone actually allocates a
988 * screen for us. but we do clear the screen
989 * at least.
990 */
991 memset(ri->ri_bits, 0, 1024);
992
993 radeonfb_modeswitch(dp);
994 #ifdef SPLASHSCREEN
995 if (splash_render(&dp->rd_splash,
996 SPLASH_F_CENTER|SPLASH_F_FILL) == 0)
997 SCREEN_DISABLE_DRAWING(&dp->rd_vscreen);
998 #endif
999 }
1000
1001 aa.console = dp->rd_console;
1002 aa.scrdata = &dp->rd_wsscreenlist;
1003 aa.accessops = &radeonfb_accessops;
1004 aa.accesscookie = &dp->rd_vd;
1005
1006 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
1007
1008 radeonfb_blank(dp, 0);
1009
1010 /* Initialise delayed lvds operations for backlight. */
1011 callout_init(&dp->rd_bl_lvds_co, 0);
1012 callout_setfunc(&dp->rd_bl_lvds_co,
1013 radeonfb_lvds_callout, dp);
1014 dp->rd_bl_on = 1;
1015 dp->rd_bl_level = radeonfb_get_backlight(dp);
1016 radeonfb_set_backlight(dp, dp->rd_bl_level);
1017 }
1018
1019 for (i = 0; i < RADEON_NDISPLAYS; i++)
1020 radeonfb_init_palette(&sc->sc_displays[i]);
1021
1022 if (HAS_CRTC2(sc)) {
1023 CLR32(sc, RADEON_CRTC2_GEN_CNTL, RADEON_CRTC2_DISP_DIS);
1024 }
1025
1026 CLR32(sc, RADEON_CRTC_EXT_CNTL, RADEON_CRTC_DISPLAY_DIS);
1027 SET32(sc, RADEON_FP_GEN_CNTL, RADEON_FP_FPON);
1028 pmf_event_register(dev, PMFE_DISPLAY_BRIGHTNESS_UP,
1029 radeonfb_brightness_up, TRUE);
1030 pmf_event_register(dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
1031 radeonfb_brightness_down, TRUE);
1032
1033 /*
1034 * if we attach a DRM we need to unmap registers in
1035 * WSDISPLAYIO_MODE_MAPPED, since this keeps us from doing things like
1036 * screen blanking we only do it if needed
1037 */
1038 sc->sc_needs_unmap =
1039 (config_found_ia(dev, "drm", aux, radeonfb_drm_print) != 0);
1040 DPRINTF(("needs_unmap: %d\n", sc->sc_needs_unmap));
1041
1042 PRINTREG(RADEON_CRTC_EXT_CNTL);
1043 PRINTREG(RADEON_CRTC_GEN_CNTL);
1044 PRINTREG(RADEON_CRTC2_GEN_CNTL);
1045 PRINTREG(RADEON_DISP_OUTPUT_CNTL);
1046 PRINTREG(RADEON_DAC_CNTL2);
1047 PRINTREG(RADEON_FP_GEN_CNTL);
1048 PRINTREG(RADEON_FP2_GEN_CNTL);
1049 PRINTREG(RADEON_TMDS_CNTL);
1050 PRINTREG(RADEON_TMDS_TRANSMITTER_CNTL);
1051 PRINTREG(RADEON_TMDS_PLL_CNTL);
1052 PRINTREG(RADEON_PIXCLKS_CNTL);
1053
1054 return;
1055
1056 error:
1057 if (sc->sc_biossz)
1058 free(sc->sc_bios, M_DEVBUF);
1059
1060 if (sc->sc_regsz)
1061 bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsz);
1062
1063 if (sc->sc_memsz)
1064 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsz);
1065 }
1066
1067 static void
1068 radeonfb_map(struct radeonfb_softc *sc)
1069 {
1070 if (!sc->sc_mapped) {
1071 if (bus_space_map(sc->sc_regt, sc->sc_regaddr, sc->sc_regsz, 0,
1072 &sc->sc_regh) != 0) {
1073 aprint_error_dev(sc->sc_dev,
1074 "unable to map registers!\n");
1075 return;
1076 }
1077 if (bus_space_map(sc->sc_memt, sc->sc_memaddr, sc->sc_memsz,
1078 BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
1079 sc->sc_memsz = 0;
1080 aprint_error_dev(sc->sc_dev,
1081 "Unable to map frame buffer\n");
1082 return;
1083 }
1084 sc->sc_mapped = TRUE;
1085 }
1086 }
1087
1088 static void
1089 radeonfb_unmap(struct radeonfb_softc *sc)
1090 {
1091 if (!sc->sc_needs_unmap)
1092 return;
1093
1094 if (sc->sc_mapped) {
1095 bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsz);
1096 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsz);
1097 sc->sc_mapped = FALSE;
1098 }
1099 }
1100
1101 static int
1102 radeonfb_drm_print(void *aux, const char *pnp)
1103 {
1104 if (pnp)
1105 aprint_normal("drm at %s", pnp);
1106 return (UNCONF);
1107 }
1108
1109 int
1110 radeonfb_ioctl(void *v, void *vs,
1111 unsigned long cmd, void *d, int flag, struct lwp *l)
1112 {
1113 struct vcons_data *vd;
1114 struct radeonfb_display *dp;
1115 struct radeonfb_softc *sc;
1116 struct wsdisplay_param *param;
1117 struct vcons_screen *ms;
1118
1119 vd = (struct vcons_data *)v;
1120 ms = vd->active;
1121 dp = (struct radeonfb_display *)vd->cookie;
1122 sc = dp->rd_softc;
1123
1124 /* can't do these without registers being mapped */
1125 if (!sc->sc_mapped) {
1126 switch (cmd) {
1127 case WSDISPLAYIO_GVIDEO:
1128 case WSDISPLAYIO_SVIDEO:
1129 case WSDISPLAYIO_GETCMAP:
1130 case WSDISPLAYIO_PUTCMAP:
1131 case WSDISPLAYIO_SCURSOR:
1132 case WSDISPLAYIO_GCURPOS:
1133 case WSDISPLAYIO_SCURPOS:
1134 case WSDISPLAYIO_SETPARAM:
1135 return EINVAL;
1136 }
1137 }
1138
1139 switch (cmd) {
1140 case WSDISPLAYIO_GTYPE:
1141 *(unsigned *)d = WSDISPLAY_TYPE_PCIMISC;
1142 return 0;
1143
1144 case WSDISPLAYIO_GINFO:
1145 if (vd->active != NULL) {
1146 struct wsdisplay_fbinfo *fb;
1147 fb = (struct wsdisplay_fbinfo *)d;
1148 fb->width = dp->rd_virtx;
1149 fb->height = dp->rd_virty;
1150 fb->depth = dp->rd_bpp;
1151 fb->cmsize = 256;
1152 return 0;
1153 } else
1154 return ENODEV;
1155 case WSDISPLAYIO_GVIDEO:
1156 if (radeonfb_isblank(dp))
1157 *(unsigned *)d = WSDISPLAYIO_VIDEO_OFF;
1158 else
1159 *(unsigned *)d = WSDISPLAYIO_VIDEO_ON;
1160 return 0;
1161
1162 case WSDISPLAYIO_SVIDEO:
1163 radeonfb_blank(dp,
1164 (*(unsigned int *)d == WSDISPLAYIO_VIDEO_OFF));
1165 radeonfb_switch_backlight(dp,
1166 (*(unsigned int *)d == WSDISPLAYIO_VIDEO_ON));
1167 return 0;
1168
1169 case WSDISPLAYIO_GETCMAP:
1170 if (dp->rd_bpp == 8)
1171 return radeonfb_getcmap(dp,
1172 (struct wsdisplay_cmap *)d);
1173 return EINVAL;
1174
1175 case WSDISPLAYIO_PUTCMAP:
1176 if (dp->rd_bpp == 8)
1177 return radeonfb_putcmap(dp,
1178 (struct wsdisplay_cmap *)d);
1179 return EINVAL;
1180
1181 case WSDISPLAYIO_LINEBYTES:
1182 *(unsigned *)d = dp->rd_stride;
1183 return 0;
1184
1185 case WSDISPLAYIO_SMODE:
1186 if (*(int *)d != dp->rd_wsmode) {
1187 dp->rd_wsmode = *(int *)d;
1188 if ((dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) ||
1189 (dp->rd_wsmode == WSDISPLAYIO_MODE_DUMBFB))
1190 radeonfb_map(sc);
1191
1192 if ((dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) &&
1193 (dp->rd_vd.active)) {
1194 radeonfb_engine_init(dp);
1195 glyphcache_wipe(&dp->rd_gc);
1196 radeonfb_init_palette(dp);
1197 radeonfb_modeswitch(dp);
1198 radeonfb_rectfill(dp, 0, 0, dp->rd_virtx,
1199 dp->rd_virty, dp->rd_bg);
1200 vcons_redraw_screen(dp->rd_vd.active);
1201 }
1202 if (dp->rd_wsmode == WSDISPLAYIO_MODE_MAPPED)
1203 radeonfb_unmap(sc);
1204 }
1205 return 0;
1206
1207 case WSDISPLAYIO_GCURMAX:
1208 ((struct wsdisplay_curpos *)d)->x = RADEON_CURSORMAXX;
1209 ((struct wsdisplay_curpos *)d)->y = RADEON_CURSORMAXY;
1210 return 0;
1211
1212 case WSDISPLAYIO_SCURSOR:
1213 return radeonfb_set_cursor(dp, (struct wsdisplay_cursor *)d);
1214
1215 case WSDISPLAYIO_GCURSOR:
1216 return EPASSTHROUGH;
1217
1218 case WSDISPLAYIO_GCURPOS:
1219 ((struct wsdisplay_curpos *)d)->x = dp->rd_cursor.rc_pos.x;
1220 ((struct wsdisplay_curpos *)d)->y = dp->rd_cursor.rc_pos.y;
1221 return 0;
1222
1223 case WSDISPLAYIO_SCURPOS:
1224 return radeonfb_set_curpos(dp, (struct wsdisplay_curpos *)d);
1225
1226 case WSDISPLAYIO_SSPLASH:
1227 #if defined(SPLASHSCREEN)
1228 if (*(int *)d == 1) {
1229 SCREEN_DISABLE_DRAWING(&dp->rd_vscreen);
1230 splash_render(&dp->rd_splash,
1231 SPLASH_F_CENTER|SPLASH_F_FILL);
1232 } else
1233 SCREEN_ENABLE_DRAWING(&dp->rd_vscreen);
1234 return 0;
1235 #else
1236 return ENODEV;
1237 #endif
1238 case WSDISPLAYIO_GETPARAM:
1239 param = (struct wsdisplay_param *)d;
1240 switch (param->param) {
1241 case WSDISPLAYIO_PARAM_BRIGHTNESS:
1242 param->min = 0;
1243 param->max = 255;
1244 param->curval = dp->rd_bl_level;
1245 return 0;
1246 case WSDISPLAYIO_PARAM_BACKLIGHT:
1247 param->min = 0;
1248 param->max = RADEONFB_BACKLIGHT_MAX;
1249 param->curval = dp->rd_bl_on;
1250 return 0;
1251 }
1252 return EPASSTHROUGH;
1253
1254 case WSDISPLAYIO_SETPARAM:
1255 param = (struct wsdisplay_param *)d;
1256 switch (param->param) {
1257 case WSDISPLAYIO_PARAM_BRIGHTNESS:
1258 radeonfb_set_backlight(dp, param->curval);
1259 return 0;
1260 case WSDISPLAYIO_PARAM_BACKLIGHT:
1261 radeonfb_switch_backlight(dp, param->curval);
1262 return 0;
1263 }
1264 return EPASSTHROUGH;
1265
1266 /* PCI config read/write passthrough. */
1267 case PCI_IOC_CFGREAD:
1268 case PCI_IOC_CFGWRITE:
1269 return pci_devioctl(sc->sc_pc, sc->sc_pt, cmd, d, flag, l);
1270
1271 case WSDISPLAYIO_GET_BUSID:
1272 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
1273 sc->sc_pt, d);
1274
1275 case WSDISPLAYIO_GET_EDID: {
1276 struct wsdisplayio_edid_info *ei = d;
1277 return wsdisplayio_get_edid(sc->sc_dev, ei);
1278 }
1279
1280 case WSDISPLAYIO_GET_FBINFO: {
1281 struct wsdisplayio_fbinfo *fbi = d;
1282 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
1283 }
1284
1285 default:
1286 return EPASSTHROUGH;
1287 }
1288 }
1289
1290 paddr_t
1291 radeonfb_mmap(void *v, void *vs, off_t offset, int prot)
1292 {
1293 struct vcons_data *vd;
1294 struct radeonfb_display *dp;
1295 struct radeonfb_softc *sc;
1296 paddr_t pa;
1297
1298 vd = (struct vcons_data *)v;
1299 dp = (struct radeonfb_display *)vd->cookie;
1300 sc = dp->rd_softc;
1301
1302 if ((offset >= 0) && (offset < (dp->rd_virty * dp->rd_stride))) {
1303 pa = bus_space_mmap(sc->sc_memt,
1304 sc->sc_memaddr + dp->rd_offset + offset, 0,
1305 prot, BUS_SPACE_MAP_LINEAR);
1306 return pa;
1307 }
1308
1309 /*
1310 * restrict all other mappings to processes with superuser privileges
1311 * or the kernel itself
1312 */
1313 if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
1314 NULL, NULL, NULL, NULL) != 0) {
1315 aprint_error_dev(sc->sc_dev, "mmap() rejected.\n");
1316 return -1;
1317 }
1318
1319 if ((offset >= sc->sc_regaddr) &&
1320 (offset < sc->sc_regaddr + sc->sc_regsz)) {
1321 return bus_space_mmap(sc->sc_regt, offset, 0, prot,
1322 BUS_SPACE_MAP_LINEAR);
1323 }
1324
1325 if ((offset >= sc->sc_memaddr) &&
1326 (offset < sc->sc_memaddr + sc->sc_memsz)) {
1327 return bus_space_mmap(sc->sc_memt, offset, 0, prot,
1328 BUS_SPACE_MAP_LINEAR);
1329 }
1330
1331 if ((offset >= sc->sc_romaddr) &&
1332 (offset < sc->sc_romaddr + sc->sc_romsz)) {
1333 return bus_space_mmap(sc->sc_memt, offset, 0, prot,
1334 BUS_SPACE_MAP_LINEAR);
1335 }
1336
1337 #ifdef PCI_MAGIC_IO_RANGE
1338 /* allow mapping of IO space */
1339 if ((offset >= PCI_MAGIC_IO_RANGE) &&
1340 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
1341 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
1342 0, prot, 0);
1343 return pa;
1344 }
1345 #endif /* PCI_MAGIC_IO_RANGE */
1346
1347 return -1;
1348 }
1349
1350 static void
1351 radeonfb_loadbios(struct radeonfb_softc *sc, const struct pci_attach_args *pa)
1352 {
1353 bus_space_tag_t romt;
1354 bus_space_handle_t romh, biosh;
1355 bus_size_t romsz;
1356 bus_addr_t ptr;
1357
1358 if (pci_mapreg_map(pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
1359 BUS_SPACE_MAP_PREFETCHABLE, &romt, &romh, NULL, &romsz) != 0) {
1360 aprint_verbose("%s: unable to map BIOS!\n", XNAME(sc));
1361 return;
1362 }
1363
1364 pci_find_rom(pa, romt, romh, romsz, PCI_ROM_CODE_TYPE_X86, &biosh,
1365 &sc->sc_biossz);
1366 if (sc->sc_biossz == 0) {
1367 aprint_verbose("%s: Video BIOS not present\n", XNAME(sc));
1368 return;
1369 }
1370
1371 sc->sc_bios = malloc(sc->sc_biossz, M_DEVBUF, M_WAITOK);
1372 bus_space_read_region_1(romt, biosh, 0, sc->sc_bios, sc->sc_biossz);
1373
1374 /* unmap the PCI expansion rom */
1375 bus_space_unmap(romt, romh, romsz);
1376
1377 /* turn off rom decoder now */
1378 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
1379 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM) &
1380 ~PCI_MAPREG_ROM_ENABLE);
1381
1382 ptr = GETBIOS16(sc, 0x48);
1383 if ((GETBIOS32(sc, ptr + 4) == 0x41544f4d /* "ATOM" */) ||
1384 (GETBIOS32(sc, ptr + 4) == 0x4d4f5441 /* "MOTA" */)) {
1385 sc->sc_flags |= RFB_ATOM;
1386 }
1387
1388 aprint_verbose("%s: Found %d KB %s BIOS\n", XNAME(sc),
1389 (unsigned)sc->sc_biossz >> 10, IS_ATOM(sc) ? "ATOM" : "Legacy");
1390 }
1391
1392
1393 uint32_t
1394 radeonfb_get32(struct radeonfb_softc *sc, uint32_t reg)
1395 {
1396
1397 return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
1398 }
1399
1400 void
1401 radeonfb_put32(struct radeonfb_softc *sc, uint32_t reg, uint32_t val)
1402 {
1403
1404 bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
1405 }
1406
1407 void
1408 radeonfb_put32s(struct radeonfb_softc *sc, uint32_t reg, uint32_t val)
1409 {
1410
1411 bus_space_write_stream_4(sc->sc_regt, sc->sc_regh, reg, val);
1412 }
1413
1414 void
1415 radeonfb_mask32(struct radeonfb_softc *sc, uint32_t reg,
1416 uint32_t andmask, uint32_t ormask)
1417 {
1418 int s;
1419 uint32_t val;
1420
1421 s = splhigh();
1422 val = radeonfb_get32(sc, reg);
1423 val = (val & andmask) | ormask;
1424 radeonfb_put32(sc, reg, val);
1425 splx(s);
1426 }
1427
1428 uint32_t
1429 radeonfb_getindex(struct radeonfb_softc *sc, uint32_t idx)
1430 {
1431 int s;
1432 uint32_t val;
1433
1434 s = splhigh();
1435 radeonfb_put32(sc, RADEON_MM_INDEX, idx);
1436 val = radeonfb_get32(sc, RADEON_MM_DATA);
1437 splx(s);
1438
1439 return (val);
1440 }
1441
1442 void
1443 radeonfb_putindex(struct radeonfb_softc *sc, uint32_t idx, uint32_t val)
1444 {
1445 int s;
1446
1447 s = splhigh();
1448 radeonfb_put32(sc, RADEON_MM_INDEX, idx);
1449 radeonfb_put32(sc, RADEON_MM_DATA, val);
1450 splx(s);
1451 }
1452
1453 void
1454 radeonfb_maskindex(struct radeonfb_softc *sc, uint32_t idx,
1455 uint32_t andmask, uint32_t ormask)
1456 {
1457 int s;
1458 uint32_t val;
1459
1460 s = splhigh();
1461 radeonfb_put32(sc, RADEON_MM_INDEX, idx);
1462 val = radeonfb_get32(sc, RADEON_MM_DATA);
1463 val = (val & andmask) | ormask;
1464 radeonfb_put32(sc, RADEON_MM_DATA, val);
1465 splx(s);
1466 }
1467
1468 uint32_t
1469 radeonfb_getpll(struct radeonfb_softc *sc, uint32_t idx)
1470 {
1471 int s;
1472 uint32_t val;
1473
1474 s = splhigh();
1475 radeonfb_put32(sc, RADEON_CLOCK_CNTL_INDEX, (idx & 0x3f));
1476 val = radeonfb_get32(sc, RADEON_CLOCK_CNTL_DATA);
1477 if (HAS_R300CG(sc))
1478 radeonfb_r300cg_workaround(sc);
1479 splx(s);
1480
1481 return (val);
1482 }
1483
1484 void
1485 radeonfb_putpll(struct radeonfb_softc *sc, uint32_t idx, uint32_t val)
1486 {
1487 int s;
1488
1489 s = splhigh();
1490 radeonfb_put32(sc, RADEON_CLOCK_CNTL_INDEX, (idx & 0x3f) |
1491 RADEON_PLL_WR_EN);
1492 radeonfb_put32(sc, RADEON_CLOCK_CNTL_DATA, val);
1493 radeonfb_put32(sc, RADEON_CLOCK_CNTL_INDEX, 0);
1494 splx(s);
1495 }
1496
1497 void
1498 radeonfb_maskpll(struct radeonfb_softc *sc, uint32_t idx,
1499 uint32_t andmask, uint32_t ormask)
1500 {
1501 int s;
1502 uint32_t val;
1503
1504 s = splhigh();
1505 radeonfb_put32(sc, RADEON_CLOCK_CNTL_INDEX, (idx & 0x3f) |
1506 RADEON_PLL_WR_EN);
1507 val = radeonfb_get32(sc, RADEON_CLOCK_CNTL_DATA);
1508 val = (val & andmask) | ormask;
1509 radeonfb_put32(sc, RADEON_CLOCK_CNTL_DATA, val);
1510 radeonfb_put32(sc, RADEON_CLOCK_CNTL_INDEX, 0);
1511 splx(s);
1512 }
1513
1514 int
1515 radeonfb_scratch_test(struct radeonfb_softc *sc, int reg, uint32_t v)
1516 {
1517 uint32_t saved;
1518
1519 saved = GET32(sc, reg);
1520 PUT32(sc, reg, v);
1521 if (GET32(sc, reg) != v) {
1522 return -1;
1523 }
1524 PUT32(sc, reg, saved);
1525 return 0;
1526 }
1527
1528 uintmax_t
1529 radeonfb_getprop_num(struct radeonfb_softc *sc, const char *name,
1530 uintmax_t defval)
1531 {
1532 prop_number_t pn;
1533 pn = prop_dictionary_get(device_properties(sc->sc_dev), name);
1534 if (pn == NULL) {
1535 return defval;
1536 }
1537 KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
1538 return (prop_number_integer_value(pn));
1539 }
1540
1541 int
1542 radeonfb_getclocks(struct radeonfb_softc *sc)
1543 {
1544 bus_addr_t ptr;
1545 int refclk = 0;
1546 int refdiv = 0;
1547 int minpll = 0;
1548 int maxpll = 0;
1549
1550 /* load initial property values if port/board provides them */
1551 refclk = radeonfb_getprop_num(sc, "refclk", 0) & 0xffff;
1552 refdiv = radeonfb_getprop_num(sc, "refdiv", 0) & 0xffff;
1553 minpll = radeonfb_getprop_num(sc, "minpll", 0) & 0xffffffffU;
1554 maxpll = radeonfb_getprop_num(sc, "maxpll", 0) & 0xffffffffU;
1555
1556 PRINTPLL(RADEON_PPLL_REF_DIV);
1557 PRINTPLL(RADEON_PPLL_DIV_0);
1558 PRINTPLL(RADEON_PPLL_DIV_1);
1559 PRINTPLL(RADEON_PPLL_DIV_2);
1560 PRINTPLL(RADEON_PPLL_DIV_3);
1561 PRINTREG(RADEON_CLOCK_CNTL_INDEX);
1562 PRINTPLL(RADEON_P2PLL_REF_DIV);
1563 PRINTPLL(RADEON_P2PLL_DIV_0);
1564
1565 if (refclk && refdiv && minpll && maxpll)
1566 goto dontprobe;
1567
1568 if (!sc->sc_biossz) {
1569 /* no BIOS */
1570 aprint_verbose("%s: No video BIOS, using default clocks\n",
1571 XNAME(sc));
1572 if (IS_IGP(sc))
1573 refclk = refclk ? refclk : 1432;
1574 else
1575 refclk = refclk ? refclk : 2700;
1576 refdiv = refdiv ? refdiv : 12;
1577 minpll = minpll ? minpll : 12500;
1578 /* XXX
1579 * Need to check if the firmware or something programmed a
1580 * higher value than this, and if so, bump it.
1581 * The RV280 in my iBook is unhappy if the PLL input is less
1582 * than 360MHz
1583 */
1584 maxpll = maxpll ? maxpll : 40000/*35000*/;
1585 } else if (IS_ATOM(sc)) {
1586 /* ATOM BIOS */
1587 ptr = GETBIOS16(sc, 0x48);
1588 ptr = GETBIOS16(sc, ptr + 32); /* aka MasterDataStart */
1589 ptr = GETBIOS16(sc, ptr + 12); /* pll info block */
1590 refclk = refclk ? refclk : GETBIOS16(sc, ptr + 82);
1591 minpll = minpll ? minpll : GETBIOS16(sc, ptr + 78);
1592 maxpll = maxpll ? maxpll : GETBIOS16(sc, ptr + 32);
1593 /*
1594 * ATOM BIOS doesn't supply a reference divider, so we
1595 * have to probe for it.
1596 */
1597 if (refdiv < 2)
1598 refdiv = GETPLL(sc, RADEON_PPLL_REF_DIV) &
1599 RADEON_PPLL_REF_DIV_MASK;
1600 /*
1601 * if probe is zero, just assume one that should work
1602 * for most parts
1603 */
1604 if (refdiv < 2)
1605 refdiv = 12;
1606
1607 } else {
1608 uint32_t tmp = GETPLL(sc, RADEON_PPLL_REF_DIV);
1609 /* Legacy BIOS */
1610 ptr = GETBIOS16(sc, 0x48);
1611 ptr = GETBIOS16(sc, ptr + 0x30);
1612 if (IS_R300(sc)) {
1613 refdiv = refdiv ? refdiv :
1614 (tmp & R300_PPLL_REF_DIV_ACC_MASK) >>
1615 R300_PPLL_REF_DIV_ACC_SHIFT;
1616 } else {
1617 refdiv = refdiv ? refdiv :
1618 tmp & RADEON_PPLL_REF_DIV_MASK;
1619 }
1620 refclk = refclk ? refclk : GETBIOS16(sc, ptr + 0x0E);
1621 refdiv = refdiv ? refdiv : GETBIOS16(sc, ptr + 0x10);
1622 minpll = minpll ? minpll : GETBIOS32(sc, ptr + 0x12);
1623 maxpll = maxpll ? maxpll : GETBIOS32(sc, ptr + 0x16);
1624 }
1625
1626
1627 dontprobe:
1628 sc->sc_refclk = refclk * 10;
1629 sc->sc_refdiv = refdiv;
1630 sc->sc_minpll = minpll * 10;
1631 sc->sc_maxpll = maxpll * 10;
1632 return 0;
1633 }
1634
1635 int
1636 radeonfb_calc_dividers(struct radeonfb_softc *sc, uint32_t dotclock,
1637 uint32_t *postdivbit, uint32_t *feedbackdiv, int flags)
1638 {
1639 int i;
1640 uint32_t outfreq;
1641 int div;
1642
1643 DPRINTF(("dot clock: %u\n", dotclock));
1644 for (i = 0; (div = radeonfb_dividers[i].divider) != 0; i++) {
1645
1646 if ((flags & NO_ODD_FBDIV) && ((div & 1) != 0))
1647 continue;
1648
1649 /*
1650 * XXX
1651 * the rv350 in my last generation 14" iBook G4 produces
1652 * garbage with dividers > 4. No idea if this is a hardware
1653 * limitation or an error in the divider table.
1654 */
1655 if ((sc->sc_family == RADEON_RV350) && (div > 4))
1656 continue;
1657
1658 outfreq = div * dotclock;
1659 if ((outfreq >= sc->sc_minpll) &&
1660 (outfreq <= sc->sc_maxpll)) {
1661 DPRINTF(("outfreq: %u\n", outfreq));
1662 *postdivbit =
1663 ((uint32_t)radeonfb_dividers[i].mask << 16);
1664 DPRINTF(("post divider: %d (mask %x)\n", div,
1665 *postdivbit));
1666 break;
1667 }
1668 }
1669
1670 if (div == 0)
1671 return 1;
1672
1673 *feedbackdiv = DIVIDE(sc->sc_refdiv * outfreq, sc->sc_refclk);
1674 DPRINTF(("feedback divider: %d\n", *feedbackdiv));
1675 return 0;
1676 }
1677
1678 #if 0
1679 #ifdef RADEONFB_DEBUG
1680 static void
1681 dump_buffer(const char *pfx, void *buffer, unsigned int size)
1682 {
1683 char asc[17];
1684 unsigned ptr = (unsigned)buffer;
1685 char *start = (char *)(ptr & ~0xf);
1686 char *end = (char *)(ptr + size);
1687
1688 end = (char *)(((unsigned)end + 0xf) & ~0xf);
1689
1690 if (pfx == NULL) {
1691 pfx = "";
1692 }
1693
1694 while (start < end) {
1695 unsigned offset = (unsigned)start & 0xf;
1696 if (offset == 0) {
1697 printf("%s%x: ", pfx, (unsigned)start);
1698 }
1699 if (((unsigned)start < ptr) ||
1700 ((unsigned)start >= (ptr + size))) {
1701 printf(" ");
1702 asc[offset] = ' ';
1703 } else {
1704 printf("%02x", *(unsigned char *)start);
1705 if ((*start >= ' ') && (*start <= '~')) {
1706 asc[offset] = *start;
1707 } else {
1708 asc[offset] = '.';
1709 }
1710 }
1711 asc[offset + 1] = 0;
1712 if (offset % 2) {
1713 printf(" ");
1714 }
1715 if (offset == 15) {
1716 printf(" %s\n", asc);
1717 }
1718 start++;
1719 }
1720 }
1721 #endif
1722 #endif
1723
1724 int
1725 radeonfb_getconnectors(struct radeonfb_softc *sc)
1726 {
1727 int i;
1728 int found = 0;
1729
1730 for (i = 0; i < 2; i++) {
1731 sc->sc_ports[i].rp_mon_type = RADEON_MT_UNKNOWN;
1732 sc->sc_ports[i].rp_ddc_type = RADEON_DDC_NONE;
1733 sc->sc_ports[i].rp_dac_type = RADEON_DAC_UNKNOWN;
1734 sc->sc_ports[i].rp_conn_type = RADEON_CONN_NONE;
1735 sc->sc_ports[i].rp_tmds_type = RADEON_TMDS_UNKNOWN;
1736 }
1737
1738 /*
1739 * This logic is borrowed from Xorg's radeon driver.
1740 */
1741 if (!sc->sc_biossz)
1742 goto nobios;
1743
1744 if (IS_ATOM(sc)) {
1745 /* not done yet */
1746 } else {
1747 uint16_t ptr;
1748 int port = 0;
1749
1750 ptr = GETBIOS16(sc, 0x48);
1751 ptr = GETBIOS16(sc, ptr + 0x50);
1752 for (i = 1; i < 4; i++) {
1753 uint16_t entry;
1754 uint8_t conn, ddc, dac, tmds;
1755
1756 /*
1757 * Parse the connector table. From reading the code,
1758 * it appears to made up of 16-bit entries for each
1759 * connector. The 16-bits are defined as:
1760 *
1761 * bits 12-15 - connector type (0 == end of table)
1762 * bits 8-11 - DDC type
1763 * bits 5-7 - ???
1764 * bit 4 - TMDS type (1 = EXT, 0 = INT)
1765 * bits 1-3 - ???
1766 * bit 0 - DAC, 1 = TVDAC, 0 = primary
1767 */
1768 if (!GETBIOS8(sc, ptr + i * 2) && i > 1)
1769 break;
1770 entry = GETBIOS16(sc, ptr + i * 2);
1771
1772 conn = (entry >> 12) & 0xf;
1773 ddc = (entry >> 8) & 0xf;
1774 dac = (entry & 0x1) ? RADEON_DAC_TVDAC :
1775 RADEON_DAC_PRIMARY;
1776 tmds = ((entry >> 4) & 0x1) ? RADEON_TMDS_EXT :
1777 RADEON_TMDS_INT;
1778
1779 if (conn == RADEON_CONN_NONE)
1780 continue; /* no connector */
1781
1782
1783
1784 /*
1785 * XXX
1786 * both Mac Mini variants have both outputs wired to
1787 * the same connector and share the DDC lines
1788 */
1789 if ((found > 0) &&
1790 (sc->sc_ports[port].rp_ddc_type == ddc)) {
1791 /* duplicate entry for same connector */
1792 continue;
1793 }
1794
1795 /* internal DDC_DVI port gets priority */
1796 if ((ddc == RADEON_DDC_DVI) || (port == 1))
1797 port = 0;
1798 else
1799 port = 1;
1800
1801 sc->sc_ports[port].rp_ddc_type =
1802 ddc > RADEON_DDC_CRT2 ? RADEON_DDC_NONE : ddc;
1803 sc->sc_ports[port].rp_dac_type = dac;
1804 sc->sc_ports[port].rp_conn_type =
1805 min(conn, RADEON_CONN_UNSUPPORTED) ;
1806
1807 sc->sc_ports[port].rp_tmds_type = tmds;
1808
1809 if ((conn != RADEON_CONN_DVI_I) &&
1810 (conn != RADEON_CONN_DVI_D) &&
1811 (tmds == RADEON_TMDS_INT))
1812 sc->sc_ports[port].rp_tmds_type =
1813 RADEON_TMDS_UNKNOWN;
1814 sc->sc_ports[port].rp_number = i - 1;
1815
1816 found += (port + 1);
1817 }
1818 }
1819
1820 nobios:
1821 if (!found) {
1822 bool dvi_ext = FALSE, dvi_int = FALSE;
1823 DPRINTF(("No connector info in BIOS!\n"));
1824 prop_dictionary_get_bool(device_properties(sc->sc_dev),
1825 "dvi-internal", &dvi_int);
1826 prop_dictionary_get_bool(device_properties(sc->sc_dev),
1827 "dvi-external", &dvi_ext);
1828 if (dvi_ext) {
1829 sc->sc_ports[0].rp_mon_type = RADEON_MT_UNKNOWN;
1830 sc->sc_ports[0].rp_ddc_type = RADEON_DDC_CRT2;
1831 sc->sc_ports[0].rp_dac_type = RADEON_DAC_PRIMARY;
1832 sc->sc_ports[0].rp_conn_type = RADEON_CONN_DVI_I;
1833 sc->sc_ports[0].rp_tmds_type = RADEON_TMDS_EXT; /* output to fp2 */
1834 sc->sc_ports[0].rp_number = 0;
1835 sc->sc_ports[1].rp_mon_type = RADEON_MT_UNKNOWN;
1836 sc->sc_ports[1].rp_ddc_type = RADEON_DDC_NONE;
1837 sc->sc_ports[1].rp_dac_type = RADEON_DAC_UNKNOWN;
1838 sc->sc_ports[1].rp_conn_type = RADEON_CONN_NONE;
1839 sc->sc_ports[1].rp_tmds_type = RADEON_TMDS_UNKNOWN;
1840 sc->sc_ports[1].rp_number = 1;
1841 } else if (dvi_int) {
1842 sc->sc_ports[0].rp_mon_type = RADEON_MT_UNKNOWN;
1843 sc->sc_ports[0].rp_ddc_type = RADEON_DDC_CRT2;
1844 sc->sc_ports[0].rp_dac_type = RADEON_DAC_PRIMARY;
1845 sc->sc_ports[0].rp_conn_type = RADEON_CONN_DVI_I;
1846 sc->sc_ports[0].rp_tmds_type = RADEON_TMDS_INT;
1847 sc->sc_ports[0].rp_number = 0;
1848 sc->sc_ports[1].rp_mon_type = RADEON_MT_UNKNOWN;
1849 sc->sc_ports[1].rp_ddc_type = RADEON_DDC_NONE;
1850 sc->sc_ports[1].rp_dac_type = RADEON_DAC_UNKNOWN;
1851 sc->sc_ports[1].rp_conn_type = RADEON_CONN_NONE;
1852 sc->sc_ports[1].rp_tmds_type = RADEON_TMDS_UNKNOWN;
1853 sc->sc_ports[1].rp_number = 1;
1854 } else if IS_MOBILITY(sc) {
1855 /* default, port 0 = internal TMDS, port 1 = CRT */
1856 sc->sc_ports[0].rp_mon_type = RADEON_MT_UNKNOWN;
1857 sc->sc_ports[0].rp_ddc_type = RADEON_DDC_DVI;
1858 sc->sc_ports[0].rp_dac_type = RADEON_DAC_TVDAC;
1859 sc->sc_ports[0].rp_conn_type = RADEON_CONN_DVI_D;
1860 sc->sc_ports[0].rp_tmds_type = RADEON_TMDS_INT;
1861 sc->sc_ports[0].rp_number = 0;
1862
1863 sc->sc_ports[1].rp_mon_type = RADEON_MT_UNKNOWN;
1864 sc->sc_ports[1].rp_ddc_type = RADEON_DDC_VGA;
1865 sc->sc_ports[1].rp_dac_type = RADEON_DAC_PRIMARY;
1866 sc->sc_ports[1].rp_conn_type = RADEON_CONN_CRT;
1867 sc->sc_ports[1].rp_tmds_type = RADEON_TMDS_EXT;
1868 sc->sc_ports[1].rp_number = 1;
1869 } else {
1870 /* default, port 0 = DVI, port 1 = CRT */
1871 sc->sc_ports[0].rp_mon_type = RADEON_MT_UNKNOWN;
1872 sc->sc_ports[0].rp_ddc_type = RADEON_DDC_DVI;
1873 sc->sc_ports[0].rp_dac_type = RADEON_DAC_TVDAC;
1874 sc->sc_ports[0].rp_conn_type = RADEON_CONN_DVI_D;
1875 sc->sc_ports[0].rp_tmds_type = RADEON_TMDS_INT;
1876 sc->sc_ports[0].rp_number = 1;
1877
1878 sc->sc_ports[1].rp_mon_type = RADEON_MT_UNKNOWN;
1879 sc->sc_ports[1].rp_ddc_type = RADEON_DDC_VGA;
1880 sc->sc_ports[1].rp_dac_type = RADEON_DAC_PRIMARY;
1881 sc->sc_ports[1].rp_conn_type = RADEON_CONN_CRT;
1882 sc->sc_ports[1].rp_tmds_type = RADEON_TMDS_UNKNOWN;
1883 sc->sc_ports[1].rp_number = 0;
1884 }
1885 }
1886
1887 /*
1888 * Fixup for RS300/RS350/RS400 chips, that lack a primary DAC.
1889 * these chips should use TVDAC for the VGA port.
1890 */
1891 if (HAS_SDAC(sc)) {
1892 if (sc->sc_ports[0].rp_conn_type == RADEON_CONN_CRT) {
1893 sc->sc_ports[0].rp_dac_type = RADEON_DAC_TVDAC;
1894 sc->sc_ports[1].rp_dac_type = RADEON_DAC_PRIMARY;
1895 } else {
1896 sc->sc_ports[1].rp_dac_type = RADEON_DAC_TVDAC;
1897 sc->sc_ports[0].rp_dac_type = RADEON_DAC_PRIMARY;
1898 }
1899 } else if (!HAS_CRTC2(sc)) {
1900 sc->sc_ports[0].rp_dac_type = RADEON_DAC_PRIMARY;
1901 }
1902
1903 for (i = 0; i < 2; i++) {
1904 char edid[128];
1905 uint8_t ddc;
1906 struct edid_info *eip = &sc->sc_ports[i].rp_edid;
1907 prop_data_t edid_data;
1908
1909 DPRINTF(("Port #%d:\n", i));
1910 DPRINTF((" conn = %d\n", sc->sc_ports[i].rp_conn_type));
1911 DPRINTF((" ddc = %d\n", sc->sc_ports[i].rp_ddc_type));
1912 DPRINTF((" dac = %d\n", sc->sc_ports[i].rp_dac_type));
1913 DPRINTF((" tmds = %d\n", sc->sc_ports[i].rp_tmds_type));
1914 DPRINTF((" crtc = %d\n", sc->sc_ports[i].rp_number));
1915
1916 sc->sc_ports[i].rp_edid_valid = 0;
1917 /* first look for static EDID data */
1918 if ((edid_data = prop_dictionary_get(device_properties(
1919 sc->sc_dev), "EDID")) != NULL) {
1920
1921 aprint_debug_dev(sc->sc_dev, "using static EDID\n");
1922 memcpy(edid, prop_data_data_nocopy(edid_data), 128);
1923 if (edid_parse(edid, eip) == 0) {
1924
1925 sc->sc_ports[i].rp_edid_valid = 1;
1926 }
1927 }
1928 /* if we didn't find any we'll try to talk to the monitor */
1929 if (sc->sc_ports[i].rp_edid_valid != 1) {
1930
1931 ddc = sc->sc_ports[i].rp_ddc_type;
1932 if (ddc != RADEON_DDC_NONE) {
1933 if ((radeonfb_i2c_read_edid(sc, ddc, edid)
1934 == 0) && (edid_parse(edid, eip) == 0)) {
1935
1936 sc->sc_ports[i].rp_edid_valid = 1;
1937 #ifdef RADEONFB_DEBUG
1938 edid_print(eip);
1939 #endif
1940 }
1941 }
1942 }
1943 }
1944
1945 return found;
1946 }
1947
1948 int
1949 radeonfb_gettmds(struct radeonfb_softc *sc)
1950 {
1951 int i;
1952
1953 if (!sc->sc_biossz) {
1954 goto nobios;
1955 }
1956
1957 if (IS_ATOM(sc)) {
1958 /* XXX: not done yet */
1959 } else {
1960 uint16_t ptr;
1961 int n;
1962
1963 ptr = GETBIOS16(sc, 0x48);
1964 ptr = GETBIOS16(sc, ptr + 0x34);
1965 DPRINTF(("DFP table revision %d\n", GETBIOS8(sc, ptr)));
1966 if (GETBIOS8(sc, ptr) == 3) {
1967 /* revision three table */
1968 n = GETBIOS8(sc, ptr + 5) + 1;
1969 n = min(n, 4);
1970
1971 memset(sc->sc_tmds_pll, 0, sizeof (sc->sc_tmds_pll));
1972 for (i = 0; i < n; i++) {
1973 sc->sc_tmds_pll[i].rtp_pll = GETBIOS32(sc,
1974 ptr + i * 10 + 8);
1975 sc->sc_tmds_pll[i].rtp_freq = GETBIOS16(sc,
1976 ptr + i * 10 + 0x10);
1977 DPRINTF(("TMDS_PLL dot clock %d pll %x\n",
1978 sc->sc_tmds_pll[i].rtp_freq,
1979 sc->sc_tmds_pll[i].rtp_pll));
1980 }
1981 return 0;
1982 }
1983 }
1984
1985 nobios:
1986 DPRINTF(("no suitable DFP table present\n"));
1987 for (i = 0;
1988 i < sizeof (radeonfb_tmds_pll) / sizeof (radeonfb_tmds_pll[0]);
1989 i++) {
1990 int j;
1991
1992 if (radeonfb_tmds_pll[i].family != sc->sc_family)
1993 continue;
1994
1995 for (j = 0; j < 4; j++) {
1996 sc->sc_tmds_pll[j] = radeonfb_tmds_pll[i].plls[j];
1997 DPRINTF(("TMDS_PLL dot clock %d pll %x\n",
1998 sc->sc_tmds_pll[j].rtp_freq,
1999 sc->sc_tmds_pll[j].rtp_pll));
2000 }
2001 return 0;
2002 }
2003
2004 return -1;
2005 }
2006
2007 const struct videomode *
2008 radeonfb_modelookup(const char *name)
2009 {
2010 int i;
2011
2012 for (i = 0; i < videomode_count; i++)
2013 if (!strcmp(name, videomode_list[i].name))
2014 return &videomode_list[i];
2015
2016 return NULL;
2017 }
2018
2019 void
2020 radeonfb_pllwriteupdate(struct radeonfb_softc *sc, int crtc)
2021 {
2022 if (crtc) {
2023 while (GETPLL(sc, RADEON_P2PLL_REF_DIV) &
2024 RADEON_P2PLL_ATOMIC_UPDATE_R);
2025 SETPLL(sc, RADEON_P2PLL_REF_DIV, RADEON_P2PLL_ATOMIC_UPDATE_W);
2026 } else {
2027 while (GETPLL(sc, RADEON_PPLL_REF_DIV) &
2028 RADEON_PPLL_ATOMIC_UPDATE_R);
2029 SETPLL(sc, RADEON_PPLL_REF_DIV, RADEON_PPLL_ATOMIC_UPDATE_W);
2030 }
2031 }
2032
2033 void
2034 radeonfb_pllwaitatomicread(struct radeonfb_softc *sc, int crtc)
2035 {
2036 int i;
2037
2038 for (i = 10000; i; i--) {
2039 if (crtc) {
2040 if (GETPLL(sc, RADEON_P2PLL_REF_DIV) &
2041 RADEON_P2PLL_ATOMIC_UPDATE_R)
2042 break;
2043 } else {
2044 if (GETPLL(sc, RADEON_PPLL_REF_DIV) &
2045 RADEON_PPLL_ATOMIC_UPDATE_R)
2046 break;
2047 }
2048 }
2049 }
2050
2051 void
2052 radeonfb_program_vclk(struct radeonfb_softc *sc, int dotclock, int crtc, int flags)
2053 {
2054 uint32_t pbit = 0;
2055 uint32_t feed = 0;
2056 uint32_t data, refdiv, div0, r2xxref;
2057
2058 radeonfb_calc_dividers(sc, dotclock, &pbit, &feed, flags);
2059
2060 if (crtc == 0) {
2061
2062 refdiv = GETPLL(sc, RADEON_PPLL_REF_DIV);
2063
2064 /*
2065 * XXX
2066 * the RV350 in my last generation iBook G4 behaves like an
2067 * r2xx here - try to detect that and not screw up the reference
2068 * divider.
2069 * xf86-video-radeon just skips PLL programming altogether
2070 * on iBooks, probably for this reason.
2071 */
2072 r2xxref = (refdiv & ~RADEON_PPLL_REF_DIV_MASK) | sc->sc_refdiv;
2073 if (IS_R300(sc) && (r2xxref != refdiv)) {
2074 refdiv = (refdiv & ~R300_PPLL_REF_DIV_ACC_MASK) |
2075 (sc->sc_refdiv << R300_PPLL_REF_DIV_ACC_SHIFT);
2076 } else {
2077 refdiv = (refdiv & ~RADEON_PPLL_REF_DIV_MASK) |
2078 sc->sc_refdiv;
2079 }
2080 DPRINTF(("refdiv %08x\n", refdiv));
2081 div0 = GETPLL(sc, RADEON_PPLL_DIV_0);
2082 DPRINTF(("div0 %08x\n", div0));
2083 div0 &= ~(RADEON_PPLL_FB3_DIV_MASK |
2084 RADEON_PPLL_POST3_DIV_MASK);
2085 div0 |= pbit;
2086 div0 |= (feed & RADEON_PPLL_FB3_DIV_MASK);
2087 DPRINTF(("div0 %08x\n", div0));
2088
2089 if ((refdiv == GETPLL(sc, RADEON_PPLL_REF_DIV)) &&
2090 (div0 == GETPLL(sc, RADEON_PPLL_DIV_0))) {
2091 /*
2092 * nothing to do here, the PLL is already where we
2093 * want it
2094 */
2095 PATCH32(sc, RADEON_CLOCK_CNTL_INDEX, 0,
2096 ~RADEON_PLL_DIV_SEL);
2097 aprint_debug_dev(sc->sc_dev, "no need to touch the PLL\n");
2098 return;
2099 }
2100
2101 /* alright, we do need to reprogram stuff */
2102 PATCHPLL(sc, RADEON_VCLK_ECP_CNTL,
2103 RADEON_VCLK_SRC_SEL_CPUCLK,
2104 ~RADEON_VCLK_SRC_SEL_MASK);
2105
2106 /* put vclk into reset, use atomic updates */
2107 SETPLL(sc, RADEON_PPLL_CNTL,
2108 RADEON_PPLL_REFCLK_SEL |
2109 RADEON_PPLL_FBCLK_SEL |
2110 RADEON_PPLL_RESET |
2111 RADEON_PPLL_ATOMIC_UPDATE_EN |
2112 RADEON_PPLL_VGA_ATOMIC_UPDATE_EN);
2113
2114 /* select clock 0 */
2115 PATCH32(sc, RADEON_CLOCK_CNTL_INDEX, 0,
2116 ~RADEON_PLL_DIV_SEL);
2117
2118 PUTPLL(sc, RADEON_PPLL_REF_DIV, refdiv);
2119
2120 /* xf86-video-radeon does this, not sure why */
2121 PUTPLL(sc, RADEON_PPLL_DIV_0, div0);
2122 PUTPLL(sc, RADEON_PPLL_DIV_0, div0);
2123
2124 /* use the atomic update */
2125 radeonfb_pllwriteupdate(sc, crtc);
2126
2127 /* and wait for it to complete */
2128 radeonfb_pllwaitatomicread(sc, crtc);
2129
2130 /* program HTOTAL (why?) */
2131 PUTPLL(sc, RADEON_HTOTAL_CNTL, 0);
2132
2133 /* drop reset */
2134 CLRPLL(sc, RADEON_PPLL_CNTL,
2135 RADEON_PPLL_RESET | RADEON_PPLL_SLEEP |
2136 RADEON_PPLL_ATOMIC_UPDATE_EN |
2137 RADEON_PPLL_VGA_ATOMIC_UPDATE_EN);
2138
2139 PRINTPLL(RADEON_PPLL_CNTL);
2140 PRINTPLL(RADEON_PPLL_REF_DIV);
2141 PRINTPLL(RADEON_PPLL_DIV_3);
2142
2143 /* give clock time to lock */
2144 delay(50000);
2145
2146 PATCHPLL(sc, RADEON_VCLK_ECP_CNTL,
2147 RADEON_VCLK_SRC_SEL_PPLLCLK,
2148 ~RADEON_VCLK_SRC_SEL_MASK);
2149
2150 } else {
2151
2152 PATCHPLL(sc, RADEON_PIXCLKS_CNTL,
2153 RADEON_PIX2CLK_SRC_SEL_CPUCLK,
2154 ~RADEON_PIX2CLK_SRC_SEL_MASK);
2155
2156 /* put vclk into reset, use atomic updates */
2157 SETPLL(sc, RADEON_P2PLL_CNTL,
2158 RADEON_P2PLL_RESET |
2159 RADEON_P2PLL_ATOMIC_UPDATE_EN |
2160 RADEON_P2PLL_VGA_ATOMIC_UPDATE_EN);
2161
2162 /* program reference divider */
2163 PATCHPLL(sc, RADEON_P2PLL_REF_DIV, sc->sc_refdiv,
2164 ~RADEON_P2PLL_REF_DIV_MASK);
2165
2166 /* program feedback and post dividers */
2167 data = GETPLL(sc, RADEON_P2PLL_DIV_0);
2168 data &= ~(RADEON_P2PLL_FB0_DIV_MASK |
2169 RADEON_P2PLL_POST0_DIV_MASK);
2170 data |= pbit;
2171 data |= (feed & RADEON_P2PLL_FB0_DIV_MASK);
2172 PUTPLL(sc, RADEON_P2PLL_DIV_0, data);
2173 PUTPLL(sc, RADEON_P2PLL_DIV_0, data);
2174
2175 PRINTPLL(RADEON_P2PLL_REF_DIV);
2176 PRINTPLL(RADEON_P2PLL_DIV_0);
2177
2178 /* use the atomic update */
2179 radeonfb_pllwriteupdate(sc, crtc);
2180
2181 /* and wait for it to complete */
2182 radeonfb_pllwaitatomicread(sc, crtc);
2183
2184 /* program HTOTAL (why?) */
2185 PUTPLL(sc, RADEON_HTOTAL2_CNTL, 0);
2186
2187 /* drop reset */
2188 CLRPLL(sc, RADEON_P2PLL_CNTL,
2189 RADEON_P2PLL_RESET | RADEON_P2PLL_SLEEP |
2190 RADEON_P2PLL_ATOMIC_UPDATE_EN |
2191 RADEON_P2PLL_VGA_ATOMIC_UPDATE_EN);
2192
2193 /* allow time for clock to lock */
2194 delay(50000);
2195
2196 PATCHPLL(sc, RADEON_PIXCLKS_CNTL,
2197 RADEON_PIX2CLK_SRC_SEL_P2PLLCLK,
2198 ~RADEON_PIX2CLK_SRC_SEL_MASK);
2199 }
2200 PRINTREG(RADEON_CRTC_MORE_CNTL);
2201 }
2202
2203 void
2204 radeonfb_modeswitch(struct radeonfb_display *dp)
2205 {
2206 struct radeonfb_softc *sc = dp->rd_softc;
2207 int i;
2208
2209 /* blank the display while we switch modes */
2210 radeonfb_blank(dp, 1);
2211
2212 #if 0
2213 SET32(sc, RADEON_CRTC_EXT_CNTL,
2214 RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS |
2215 RADEON_CRTC_DISPLAY_DIS /* | RADEON_CRTC_DISP_REQ_EN_B */);
2216 #endif
2217
2218 /* these registers might get in the way... */
2219 PUT32(sc, RADEON_OVR_CLR, 0);
2220 PUT32(sc, RADEON_OVR_WID_LEFT_RIGHT, 0);
2221 PUT32(sc, RADEON_OVR_WID_TOP_BOTTOM, 0);
2222 PUT32(sc, RADEON_OV0_SCALE_CNTL, 0);
2223 PUT32(sc, RADEON_SUBPIC_CNTL, 0);
2224 PUT32(sc, RADEON_VIPH_CONTROL, 0);
2225 PUT32(sc, RADEON_I2C_CNTL_1, 0);
2226 PUT32(sc, RADEON_GEN_INT_CNTL, 0);
2227 PUT32(sc, RADEON_CAP0_TRIG_CNTL, 0);
2228 PUT32(sc, RADEON_CAP1_TRIG_CNTL, 0);
2229 PUT32(sc, RADEON_SURFACE_CNTL, 0);
2230
2231 for (i = 0; i < dp->rd_ncrtcs; i++)
2232 radeonfb_setcrtc(dp, i);
2233
2234 #if 0
2235 /*
2236 * DVO chip voodoo from xf86-video-radeon
2237 * apparently this is needed for some powerbooks with DVI outputs
2238 */
2239
2240 uint8_t data[5][2] = {{0x8, 0x030}, {0x9, 0}, {0xa, 0x90}, {0xc, 0x89}, {0x8, 0x3b}};
2241 int n = 0;
2242 iic_acquire_bus(&sc->sc_i2c[0].ric_controller, 0);
2243 for (i = 0; i < 5; i++)
2244 n += iic_exec(&sc->sc_i2c[0].ric_controller, I2C_OP_WRITE, 0x38, data[i], 2, NULL, 0, 0);
2245 iic_release_bus(&sc->sc_i2c[0].ric_controller, 0);
2246 printf("n = %d\n", n);
2247 #endif
2248
2249 /* activate the display */
2250 radeonfb_blank(dp, 0);
2251 }
2252
2253 void
2254 radeonfb_setcrtc(struct radeonfb_display *dp, int index)
2255 {
2256 int crtc, flags = 0;
2257 struct videomode *mode;
2258 struct radeonfb_softc *sc;
2259 struct radeonfb_crtc *cp;
2260 uint32_t v;
2261 uint32_t gencntl;
2262 uint32_t htotaldisp;
2263 uint32_t hsyncstrt;
2264 uint32_t vtotaldisp;
2265 uint32_t vsyncstrt;
2266 uint32_t fphsyncstrt;
2267 uint32_t fpvsyncstrt;
2268 uint32_t fphtotaldisp;
2269 uint32_t fpvtotaldisp;
2270 uint32_t pitch;
2271
2272 sc = dp->rd_softc;
2273
2274 if ((sc->sc_ports[index].rp_tmds_type == RADEON_TMDS_INT) ||
2275 (sc->sc_ports[index].rp_tmds_type == RADEON_TMDS_EXT)) {
2276 flags |= NO_ODD_FBDIV;
2277 }
2278
2279 cp = &dp->rd_crtcs[index];
2280 crtc = cp->rc_number;
2281 mode = &cp->rc_videomode;
2282
2283 #if 1
2284 pitch = dp->rd_stride / dp->rd_bpp;
2285 #else
2286 pitch = (((sc->sc_maxx * sc->sc_maxbpp) + ((sc->sc_maxbpp * 8) - 1)) /
2287 (sc->sc_maxbpp * 8));
2288 #endif
2289 switch (crtc) {
2290 case 0:
2291 gencntl = RADEON_CRTC_GEN_CNTL;
2292 htotaldisp = RADEON_CRTC_H_TOTAL_DISP;
2293 hsyncstrt = RADEON_CRTC_H_SYNC_STRT_WID;
2294 vtotaldisp = RADEON_CRTC_V_TOTAL_DISP;
2295 vsyncstrt = RADEON_CRTC_V_SYNC_STRT_WID;
2296 /* should probably leave those alone on non-LVDS */
2297 fpvsyncstrt = RADEON_FP_V_SYNC_STRT_WID;
2298 fphsyncstrt = RADEON_FP_H_SYNC_STRT_WID;
2299 fpvtotaldisp = RADEON_FP_CRTC_V_TOTAL_DISP;
2300 fphtotaldisp = RADEON_FP_CRTC_H_TOTAL_DISP;
2301 break;
2302 case 1:
2303 gencntl = RADEON_CRTC2_GEN_CNTL;
2304 htotaldisp = RADEON_CRTC2_H_TOTAL_DISP;
2305 hsyncstrt = RADEON_CRTC2_H_SYNC_STRT_WID;
2306 vtotaldisp = RADEON_CRTC2_V_TOTAL_DISP;
2307 vsyncstrt = RADEON_CRTC2_V_SYNC_STRT_WID;
2308 fpvsyncstrt = RADEON_FP_V2_SYNC_STRT_WID;
2309 fphsyncstrt = RADEON_FP_H2_SYNC_STRT_WID;
2310 /* XXX these registers don't seem to exist */
2311 fpvtotaldisp = 0;//RADEON_FP_CRTC2_V_TOTAL_DISP;
2312 fphtotaldisp = 0;//RADEON_FP_CRTC2_H_TOTAL_DISP;
2313 break;
2314 default:
2315 panic("Bad CRTC!");
2316 break;
2317 }
2318
2319 /*
2320 * CRTC_GEN_CNTL - depth, accelerator mode, etc.
2321 */
2322 /* only bother with 32bpp and 8bpp */
2323 v = dp->rd_format << RADEON_CRTC_PIX_WIDTH_SHIFT;
2324
2325 if (crtc == 1) {
2326 v |= RADEON_CRTC2_CRT2_ON | RADEON_CRTC2_EN;
2327 } else {
2328 v |= RADEON_CRTC_EXT_DISP_EN | RADEON_CRTC_EN;
2329 }
2330
2331 if (mode->flags & VID_DBLSCAN)
2332 v |= RADEON_CRTC2_DBL_SCAN_EN;
2333
2334 if (mode->flags & VID_INTERLACE)
2335 v |= RADEON_CRTC2_INTERLACE_EN;
2336
2337 if (mode->flags & VID_CSYNC) {
2338 v |= RADEON_CRTC2_CSYNC_EN;
2339 if (crtc == 1)
2340 v |= RADEON_CRTC2_VSYNC_TRISTAT;
2341 }
2342
2343 PUT32(sc, gencntl, v);
2344 DPRINTF(("CRTC%s_GEN_CNTL = %08x\n", crtc ? "2" : "", v));
2345
2346 /*
2347 * CRTC_EXT_CNTL - preserve disable flags, set ATI linear and EXT_CNT
2348 */
2349 v = GET32(sc, RADEON_CRTC_EXT_CNTL);
2350 if (crtc == 0) {
2351 v &= (RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS |
2352 RADEON_CRTC_DISPLAY_DIS);
2353 v |= RADEON_XCRT_CNT_EN | RADEON_VGA_ATI_LINEAR;
2354 if (mode->flags & VID_CSYNC)
2355 v |= RADEON_CRTC_VSYNC_TRISTAT;
2356 }
2357 /* unconditional turn on CRT, in case first CRTC is DFP */
2358 v |= RADEON_CRTC_CRT_ON;
2359 PUT32(sc, RADEON_CRTC_EXT_CNTL, v);
2360 PRINTREG(RADEON_CRTC_EXT_CNTL);
2361
2362 /*
2363 * H_TOTAL_DISP
2364 */
2365 v = ((mode->hdisplay / 8) - 1) << 16;
2366 v |= (mode->htotal / 8) - 1;
2367 PUT32(sc, htotaldisp, v);
2368 DPRINTF(("CRTC%s_H_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2369 if (fphtotaldisp) {
2370 PUT32(sc, fphtotaldisp, v);
2371 DPRINTF(("FP_H%s_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2372 }
2373 /*
2374 * H_SYNC_STRT_WID
2375 */
2376 v = (((mode->hsync_end - mode->hsync_start) / 8) << 16);
2377 v |= (mode->hsync_start - 8); /* match xf86-video-radeon */
2378 if (mode->flags & VID_NHSYNC)
2379 v |= RADEON_CRTC_H_SYNC_POL;
2380 PUT32(sc, hsyncstrt, v);
2381 DPRINTF(("CRTC%s_H_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2382 if (fphsyncstrt) {
2383 PUT32(sc, fphsyncstrt, v);
2384 DPRINTF(("FP_H%s_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2385 }
2386
2387 /*
2388 * V_TOTAL_DISP
2389 */
2390 v = ((mode->vdisplay - 1) << 16);
2391 v |= (mode->vtotal - 1);
2392 PUT32(sc, vtotaldisp, v);
2393 DPRINTF(("CRTC%s_V_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2394 if (fpvtotaldisp) {
2395 PUT32(sc, fpvtotaldisp, v);
2396 DPRINTF(("FP_V%s_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2397 }
2398
2399 /*
2400 * V_SYNC_STRT_WID
2401 */
2402 v = ((mode->vsync_end - mode->vsync_start) << 16);
2403 v |= (mode->vsync_start - 1);
2404 if (mode->flags & VID_NVSYNC)
2405 v |= RADEON_CRTC_V_SYNC_POL;
2406 PUT32(sc, vsyncstrt, v);
2407 DPRINTF(("CRTC%s_V_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2408 if (fpvsyncstrt) {
2409 PUT32(sc, fpvsyncstrt, v);
2410 DPRINTF(("FP_V%s_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2411 }
2412
2413 radeonfb_program_vclk(sc, mode->dot_clock, crtc, flags);
2414
2415 switch (crtc) {
2416 case 0:
2417 PUT32(sc, RADEON_CRTC_OFFSET, 0);
2418 PUT32(sc, RADEON_CRTC_OFFSET_CNTL, 0);
2419 PUT32(sc, RADEON_CRTC_PITCH, pitch);
2420 CLR32(sc, RADEON_DISP_MERGE_CNTL, RADEON_DISP_RGB_OFFSET_EN);
2421
2422 CLR32(sc, RADEON_CRTC_EXT_CNTL,
2423 RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS |
2424 RADEON_CRTC_DISPLAY_DIS /* | RADEON_CRTC_DISP_REQ_EN_B */);
2425 CLR32(sc, RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B);
2426 PRINTREG(RADEON_CRTC_EXT_CNTL);
2427 PRINTREG(RADEON_CRTC_GEN_CNTL);
2428 PRINTREG(RADEON_CLOCK_CNTL_INDEX);
2429 break;
2430
2431 case 1:
2432 PUT32(sc, RADEON_CRTC2_OFFSET, 0);
2433 PUT32(sc, RADEON_CRTC2_OFFSET_CNTL, 0);
2434 PUT32(sc, RADEON_CRTC2_PITCH, pitch);
2435 CLR32(sc, RADEON_DISP2_MERGE_CNTL, RADEON_DISP2_RGB_OFFSET_EN);
2436 CLR32(sc, RADEON_CRTC2_GEN_CNTL,
2437 RADEON_CRTC2_VSYNC_DIS |
2438 RADEON_CRTC2_HSYNC_DIS |
2439 RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_DISP_REQ_EN_B);
2440 PRINTREG(RADEON_CRTC2_GEN_CNTL);
2441 break;
2442 }
2443 }
2444
2445 int
2446 radeonfb_isblank(struct radeonfb_display *dp)
2447 {
2448 uint32_t reg, mask;
2449
2450 if(!dp->rd_softc->sc_mapped)
2451 return 1;
2452
2453 if (dp->rd_crtcs[0].rc_number) {
2454 reg = RADEON_CRTC2_GEN_CNTL;
2455 mask = RADEON_CRTC2_DISP_DIS;
2456 } else {
2457 reg = RADEON_CRTC_EXT_CNTL;
2458 mask = RADEON_CRTC_DISPLAY_DIS;
2459 }
2460 return ((GET32(dp->rd_softc, reg) & mask) ? 1 : 0);
2461 }
2462
2463 void
2464 radeonfb_blank(struct radeonfb_display *dp, int blank)
2465 {
2466 struct radeonfb_softc *sc = dp->rd_softc;
2467 uint32_t reg, mask;
2468 uint32_t fpreg, fpval;
2469 int i;
2470
2471 if (!sc->sc_mapped)
2472 return;
2473
2474 for (i = 0; i < dp->rd_ncrtcs; i++) {
2475
2476 if (dp->rd_crtcs[i].rc_number) {
2477 reg = RADEON_CRTC2_GEN_CNTL;
2478 mask = RADEON_CRTC2_DISP_DIS;
2479 fpreg = RADEON_FP2_GEN_CNTL;
2480 fpval = RADEON_FP2_ON;
2481 } else {
2482 reg = RADEON_CRTC_EXT_CNTL;
2483 mask = RADEON_CRTC_DISPLAY_DIS;
2484 fpreg = RADEON_FP_GEN_CNTL;
2485 fpval = RADEON_FP_FPON;
2486 }
2487
2488 if (blank) {
2489 SET32(sc, reg, mask);
2490 CLR32(sc, fpreg, fpval);
2491 } else {
2492 CLR32(sc, reg, mask);
2493 SET32(sc, fpreg, fpval);
2494 }
2495 }
2496 PRINTREG(RADEON_FP_GEN_CNTL);
2497 PRINTREG(RADEON_FP2_GEN_CNTL);
2498 }
2499
2500 void
2501 radeonfb_init_screen(void *cookie, struct vcons_screen *scr, int existing,
2502 long *defattr)
2503 {
2504 struct radeonfb_display *dp = cookie;
2505 struct rasops_info *ri = &scr->scr_ri;
2506
2507 /* initialize font subsystem */
2508 wsfont_init();
2509
2510 scr->scr_flags |= VCONS_LOADFONT;
2511
2512 DPRINTF(("init screen called, existing %d\n", existing));
2513
2514 ri->ri_depth = dp->rd_bpp;
2515 ri->ri_width = dp->rd_virtx;
2516 ri->ri_height = dp->rd_virty;
2517 ri->ri_stride = dp->rd_stride;
2518 ri->ri_flg = RI_CENTER;
2519 switch (ri->ri_depth) {
2520 case 8:
2521 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB | RI_PREFER_ALPHA;
2522 break;
2523 case 32:
2524 ri->ri_flg |= RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
2525 /* we run radeons in RGB even on SPARC hardware */
2526 ri->ri_rnum = 8;
2527 ri->ri_gnum = 8;
2528 ri->ri_bnum = 8;
2529 ri->ri_rpos = 16;
2530 ri->ri_gpos = 8;
2531 ri->ri_bpos = 0;
2532 break;
2533 }
2534
2535 ri->ri_bits = (void *)dp->rd_fbptr;
2536
2537 #ifdef VCONS_DRAW_INTR
2538 scr->scr_flags |= VCONS_DONT_READ;
2539 #endif
2540
2541 if (existing) {
2542 ri->ri_flg |= RI_CLEAR;
2543
2544 /* start a modeswitch now */
2545 radeonfb_modeswitch(dp);
2546 }
2547
2548 /*
2549 * XXX: font selection should be based on properties, with some
2550 * normal/reasonable default.
2551 */
2552
2553 /* initialize and look for an initial font */
2554 rasops_init(ri, 0, 0);
2555 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
2556 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE | WSSCREEN_RESIZE;
2557
2558 rasops_reconfig(ri, dp->rd_virty / ri->ri_font->fontheight,
2559 dp->rd_virtx / ri->ri_font->fontwidth);
2560
2561 /* enable acceleration */
2562 dp->rd_putchar = ri->ri_ops.putchar;
2563 ri->ri_ops.copyrows = radeonfb_copyrows;
2564 ri->ri_ops.copycols = radeonfb_copycols;
2565 ri->ri_ops.eraserows = radeonfb_eraserows;
2566 ri->ri_ops.erasecols = radeonfb_erasecols;
2567 /* pick a putchar method based on font and Radeon model */
2568 if (ri->ri_font->stride < ri->ri_font->fontwidth) {
2569 /* got a bitmap font */
2570 #ifndef RADEONFB_ALWAYS_ACCEL_PUTCHAR
2571 if (IS_R300(dp->rd_softc)) {
2572 /*
2573 * radeonfb_putchar() doesn't work right on some R3xx
2574 * so we use software drawing here, the wrapper just
2575 * makes sure the engine is idle before scribbling
2576 * into vram
2577 */
2578 ri->ri_ops.putchar = radeonfb_putchar_wrapper;
2579 } else
2580 #endif
2581 ri->ri_ops.putchar = radeonfb_putchar;
2582 } else {
2583 /* got an alpha font */
2584 switch(ri->ri_depth) {
2585 case 32:
2586 ri->ri_ops.putchar = radeonfb_putchar_aa32;
2587 break;
2588 case 8:
2589 ri->ri_ops.putchar = radeonfb_putchar_aa8;
2590 break;
2591 default:
2592 /* XXX this should never happen */
2593 panic("%s: depth is not 8 or 32 but we got an" \
2594 " alpha font?!", __func__);
2595 }
2596 }
2597 ri->ri_ops.cursor = radeonfb_cursor;
2598 }
2599
2600 void
2601 radeonfb_set_fbloc(struct radeonfb_softc *sc)
2602 {
2603 uint32_t gen, ext, gen2 = 0;
2604 uint32_t agploc, aperbase, apersize, mcfbloc;
2605
2606 gen = GET32(sc, RADEON_CRTC_GEN_CNTL);
2607 /* XXX */
2608 ext = GET32(sc, RADEON_CRTC_EXT_CNTL) & ~RADEON_CRTC_DISPLAY_DIS;
2609 agploc = GET32(sc, RADEON_MC_AGP_LOCATION);
2610 aperbase = GET32(sc, RADEON_CONFIG_APER_0_BASE);
2611 apersize = GET32(sc, RADEON_CONFIG_APER_SIZE);
2612
2613 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen | RADEON_CRTC_DISP_REQ_EN_B);
2614 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext | RADEON_CRTC_DISPLAY_DIS);
2615 #if 0
2616 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen | RADEON_CRTC_DISPLAY_DIS);
2617 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext | RADEON_CRTC_DISP_REQ_EN_B);
2618 #endif
2619
2620 if (HAS_CRTC2(sc)) {
2621 gen2 = GET32(sc, RADEON_CRTC2_GEN_CNTL);
2622 PUT32(sc, RADEON_CRTC2_GEN_CNTL,
2623 gen2 | RADEON_CRTC2_DISP_REQ_EN_B);
2624 }
2625
2626 delay(100000);
2627
2628 mcfbloc = (aperbase >> 16) |
2629 ((aperbase + (apersize - 1)) & 0xffff0000);
2630
2631 sc->sc_aperbase = (mcfbloc & 0xffff) << 16;
2632 sc->sc_memsz = apersize;
2633
2634 if (((agploc & 0xffff) << 16) !=
2635 ((mcfbloc & 0xffff0000U) + 0x10000)) {
2636 agploc = mcfbloc & 0xffff0000U;
2637 agploc |= ((agploc + 0x10000) >> 16);
2638 }
2639
2640 PUT32(sc, RADEON_HOST_PATH_CNTL, 0);
2641
2642 PUT32(sc, RADEON_MC_FB_LOCATION, mcfbloc);
2643 PUT32(sc, RADEON_MC_AGP_LOCATION, agploc);
2644
2645 DPRINTF(("aperbase = %u\n", aperbase));
2646 PRINTREG(RADEON_MC_FB_LOCATION);
2647 PRINTREG(RADEON_MC_AGP_LOCATION);
2648
2649 PUT32(sc, RADEON_DISPLAY_BASE_ADDR, sc->sc_aperbase);
2650
2651 if (HAS_CRTC2(sc))
2652 PUT32(sc, RADEON_DISPLAY2_BASE_ADDR, sc->sc_aperbase);
2653
2654 PUT32(sc, RADEON_OV0_BASE_ADDR, sc->sc_aperbase);
2655
2656 #if 0
2657 /* XXX: what is this AGP garbage? :-) */
2658 PUT32(sc, RADEON_AGP_CNTL, 0x00100000);
2659 #endif
2660
2661 delay(100000);
2662
2663 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen);
2664 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext);
2665
2666 if (HAS_CRTC2(sc))
2667 PUT32(sc, RADEON_CRTC2_GEN_CNTL, gen2);
2668 }
2669
2670 void
2671 radeonfb_init_misc(struct radeonfb_softc *sc)
2672 {
2673 PUT32(sc, RADEON_BUS_CNTL,
2674 RADEON_BUS_MASTER_DIS |
2675 RADEON_BUS_PREFETCH_MODE_ACT |
2676 RADEON_BUS_PCI_READ_RETRY_EN |
2677 RADEON_BUS_PCI_WRT_RETRY_EN |
2678 (3 << RADEON_BUS_RETRY_WS_SHIFT) |
2679 RADEON_BUS_MSTR_RD_MULT |
2680 RADEON_BUS_MSTR_RD_LINE |
2681 RADEON_BUS_RD_DISCARD_EN |
2682 RADEON_BUS_MSTR_DISCONNECT_EN |
2683 RADEON_BUS_READ_BURST);
2684
2685 PUT32(sc, RADEON_BUS_CNTL1, 0xf0);
2686 /* PUT32(sc, RADEON_SEPROM_CNTL1, 0x09ff0000); */
2687 PUT32(sc, RADEON_FCP_CNTL, RADEON_FCP0_SRC_GND);
2688 PUT32(sc, RADEON_RBBM_CNTL,
2689 (3 << RADEON_RB_SETTLE_SHIFT) |
2690 (4 << RADEON_ABORTCLKS_HI_SHIFT) |
2691 (4 << RADEON_ABORTCLKS_CP_SHIFT) |
2692 (4 << RADEON_ABORTCLKS_CFIFO_SHIFT));
2693
2694 /* XXX: figure out what these mean! */
2695 PUT32(sc, RADEON_AGP_CNTL, 0x00100000);
2696 PUT32(sc, RADEON_HOST_PATH_CNTL, 0);
2697 #if 0
2698 PUT32(sc, RADEON_DISP_MISC_CNTL, 0x5bb00400);
2699 #endif
2700
2701 PUT32(sc, RADEON_GEN_INT_CNTL, 0);
2702 PUT32(sc, RADEON_GEN_INT_STATUS, GET32(sc, RADEON_GEN_INT_STATUS));
2703 }
2704
2705 static void
2706 radeonfb_putpal(struct radeonfb_display *dp, int idx, int r, int g, int b)
2707 {
2708 struct radeonfb_softc *sc = dp->rd_softc;
2709 int crtc, cc;
2710 uint32_t vclk;
2711
2712 vclk = GETPLL(sc, RADEON_VCLK_ECP_CNTL);
2713 PUTPLL(sc, RADEON_VCLK_ECP_CNTL, vclk & ~RADEON_PIXCLK_DAC_ALWAYS_ONb);
2714
2715 /* initialize the palette for every CRTC used by this display */
2716 for (cc = 0; cc < dp->rd_ncrtcs; cc++) {
2717 crtc = dp->rd_crtcs[cc].rc_number;
2718
2719 if (crtc)
2720 SET32(sc, RADEON_DAC_CNTL2, RADEON_DAC2_PALETTE_ACC_CTL);
2721 else
2722 CLR32(sc, RADEON_DAC_CNTL2, RADEON_DAC2_PALETTE_ACC_CTL);
2723
2724 PUT32(sc, RADEON_PALETTE_INDEX, idx);
2725 PUT32(sc, RADEON_PALETTE_30_DATA,
2726 (r << 22) | (g << 12) | (b << 2));
2727 }
2728
2729 PUTPLL(sc, RADEON_VCLK_ECP_CNTL, vclk);
2730 }
2731
2732 /*
2733 * This loads a linear color map for true color.
2734 */
2735 void
2736 radeonfb_init_palette(struct radeonfb_display *dp)
2737 {
2738 int i;
2739
2740 #define DAC_WIDTH ((1 << 10) - 1)
2741 #define CLUT_WIDTH ((1 << 8) - 1)
2742 #define CLUT_COLOR(i) ((i * DAC_WIDTH * 2 / CLUT_WIDTH + 1) / 2)
2743
2744 if (dp->rd_bpp == 8) {
2745
2746 /* R3G3B2 palette */
2747 uint32_t tmp, r, g, b;
2748
2749 for (i = 0; i <= CLUT_WIDTH; ++i) {
2750 tmp = i & 0xe0;
2751
2752 /*
2753 * replicate bits so 0xe0 maps to a red value of 0xff
2754 * in order to make white look actually white
2755 */
2756 tmp |= (tmp >> 3) | (tmp >> 6);
2757 r = tmp;
2758
2759 tmp = (i & 0x1c) << 3;
2760 tmp |= (tmp >> 3) | (tmp >> 6);
2761 g = tmp;
2762
2763 tmp = (i & 0x03) << 6;
2764 tmp |= tmp >> 2;
2765 tmp |= tmp >> 4;
2766 b = tmp;
2767
2768 radeonfb_putpal(dp, i, r, g, b);
2769 }
2770 } else {
2771 /* linear ramp */
2772 for (i = 0; i <= CLUT_WIDTH; ++i) {
2773 radeonfb_putpal(dp, i, i, i, i);
2774 }
2775 }
2776 }
2777
2778 static int
2779 radeonfb_putcmap(struct radeonfb_display *dp, struct wsdisplay_cmap *cm)
2780 {
2781 u_char *r, *g, *b;
2782 u_int index = cm->index;
2783 u_int count = cm->count;
2784 int i, error;
2785 u_char rbuf[256], gbuf[256], bbuf[256];
2786
2787 #ifdef GENFB_DEBUG
2788 aprint_debug("putcmap: %d %d\n",index, count);
2789 #endif
2790 if (index >= 256 || count > 256 - index)
2791 return EINVAL;
2792 error = copyin(cm->red, &rbuf[index], count);
2793 if (error)
2794 return error;
2795 error = copyin(cm->green, &gbuf[index], count);
2796 if (error)
2797 return error;
2798 error = copyin(cm->blue, &bbuf[index], count);
2799 if (error)
2800 return error;
2801
2802 memcpy(&dp->rd_cmap_red[index], &rbuf[index], count);
2803 memcpy(&dp->rd_cmap_green[index], &gbuf[index], count);
2804 memcpy(&dp->rd_cmap_blue[index], &bbuf[index], count);
2805
2806 r = &dp->rd_cmap_red[index];
2807 g = &dp->rd_cmap_green[index];
2808 b = &dp->rd_cmap_blue[index];
2809
2810 for (i = 0; i < count; i++) {
2811 radeonfb_putpal(dp, index, *r, *g, *b);
2812 index++;
2813 r++, g++, b++;
2814 }
2815 return 0;
2816 }
2817
2818 static int
2819 radeonfb_getcmap(struct radeonfb_display *dp, struct wsdisplay_cmap *cm)
2820 {
2821 u_int index = cm->index;
2822 u_int count = cm->count;
2823 int error;
2824
2825 if (index >= 256 || count > 256 - index)
2826 return EINVAL;
2827
2828 error = copyout(&dp->rd_cmap_red[index], cm->red, count);
2829 if (error)
2830 return error;
2831 error = copyout(&dp->rd_cmap_green[index], cm->green, count);
2832 if (error)
2833 return error;
2834 error = copyout(&dp->rd_cmap_blue[index], cm->blue, count);
2835 if (error)
2836 return error;
2837
2838 return 0;
2839 }
2840
2841 /*
2842 * Bugs in some R300 hardware requires this when accessing CLOCK_CNTL_INDEX.
2843 */
2844 void
2845 radeonfb_r300cg_workaround(struct radeonfb_softc *sc)
2846 {
2847 uint32_t tmp, save;
2848
2849 save = GET32(sc, RADEON_CLOCK_CNTL_INDEX);
2850 tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
2851 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, tmp);
2852 tmp = GET32(sc, RADEON_CLOCK_CNTL_DATA);
2853 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, save);
2854 }
2855
2856 /*
2857 * Acceleration entry points.
2858 */
2859
2860 /* this one draws characters using bitmap fonts */
2861 static void
2862 radeonfb_putchar(void *cookie, int row, int col, u_int c, long attr)
2863 {
2864 struct rasops_info *ri = cookie;
2865 struct vcons_screen *scr = ri->ri_hw;
2866 struct radeonfb_display *dp = scr->scr_cookie;
2867 struct radeonfb_softc *sc = dp->rd_softc;
2868 struct wsdisplay_font *font = PICK_FONT(ri, c);
2869 uint32_t w, h;
2870 int xd, yd, offset, i;
2871 uint32_t bg, fg, gmc;
2872 uint32_t reg;
2873 uint8_t *data8;
2874 uint16_t *data16;
2875 void *data;
2876
2877 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
2878 return;
2879
2880 if (!CHAR_IN_FONT(c, font))
2881 return;
2882
2883 w = font->fontwidth;
2884 h = font->fontheight;
2885
2886 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
2887 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
2888
2889 xd = ri->ri_xorigin + col * w;
2890 yd = ri->ri_yorigin + row * h;
2891
2892 if (c == 0x20) {
2893 radeonfb_rectfill(dp, xd, yd, w, h, bg);
2894 return;
2895 }
2896 data = WSFONT_GLYPH(c, font);
2897
2898 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
2899
2900 radeonfb_wait_fifo(sc, 9);
2901
2902 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
2903 RADEON_GMC_BRUSH_NONE |
2904 RADEON_GMC_SRC_DATATYPE_MONO_FG_BG |
2905 RADEON_GMC_DST_CLIPPING |
2906 RADEON_ROP3_S |
2907 RADEON_DP_SRC_SOURCE_HOST_DATA |
2908 RADEON_GMC_CLR_CMP_CNTL_DIS |
2909 RADEON_GMC_WR_MSK_DIS |
2910 gmc);
2911
2912 PUT32(sc, RADEON_SC_LEFT, xd);
2913 PUT32(sc, RADEON_SC_RIGHT, xd + w);
2914 PUT32(sc, RADEON_DP_SRC_FRGD_CLR, fg);
2915 PUT32(sc, RADEON_DP_SRC_BKGD_CLR, bg);
2916 PUT32(sc, RADEON_DP_CNTL,
2917 RADEON_DST_X_LEFT_TO_RIGHT |
2918 RADEON_DST_Y_TOP_TO_BOTTOM);
2919
2920 PUT32(sc, RADEON_SRC_X_Y, 0);
2921 offset = 32 - (font->stride << 3);
2922 PUT32(sc, RADEON_DST_X_Y, ((xd - offset) << 16) | yd);
2923 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (32 << 16) | h);
2924
2925 radeonfb_wait_fifo(sc, h);
2926 switch (font->stride) {
2927 case 1: {
2928 data8 = data;
2929 for (i = 0; i < h; i++) {
2930 reg = *data8;
2931 #if BYTE_ORDER == LITTLE_ENDIAN
2932 reg = reg << 24;
2933 #endif
2934 bus_space_write_stream_4(sc->sc_regt,
2935 sc->sc_regh, RADEON_HOST_DATA0, reg);
2936 data8++;
2937 }
2938 break;
2939 }
2940 case 2: {
2941 data16 = data;
2942 for (i = 0; i < h; i++) {
2943 reg = *data16;
2944 #if BYTE_ORDER == LITTLE_ENDIAN
2945 reg = reg << 16;
2946 #endif
2947 bus_space_write_stream_4(sc->sc_regt,
2948 sc->sc_regh, RADEON_HOST_DATA0, reg);
2949 data16++;
2950 }
2951 break;
2952 }
2953 }
2954 if (attr & 1)
2955 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
2956 }
2957
2958 /* ... while this one is for anti-aliased ones */
2959 static void
2960 radeonfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
2961 {
2962 struct rasops_info *ri = cookie;
2963 struct vcons_screen *scr = ri->ri_hw;
2964 struct radeonfb_display *dp = scr->scr_cookie;
2965 struct radeonfb_softc *sc = dp->rd_softc;
2966 struct wsdisplay_font *font = PICK_FONT(ri, c);
2967 uint32_t bg, fg, gmc;
2968 uint8_t *data;
2969 int w, h, xd, yd;
2970 int i, r, g, b, aval;
2971 int rf, gf, bf, rb, gb, bb;
2972 uint32_t pixel;
2973 int rv;
2974
2975 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
2976 return;
2977
2978 if (!CHAR_IN_FONT(c, font))
2979 return;
2980
2981 w = font->fontwidth;
2982 h = font->fontheight;
2983
2984 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
2985 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
2986
2987 xd = ri->ri_xorigin + col * w;
2988 yd = ri->ri_yorigin + row * h;
2989
2990 if (c == 0x20) {
2991 radeonfb_rectfill(dp, xd, yd, w, h, bg);
2992 if (attr & 1)
2993 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
2994 return;
2995 }
2996 rv = glyphcache_try(&dp->rd_gc, c, xd, yd, attr);
2997 if (rv == GC_OK)
2998 return;
2999
3000 data = WSFONT_GLYPH(c, font);
3001
3002 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3003
3004 radeonfb_wait_fifo(sc, 5);
3005
3006 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3007 RADEON_GMC_BRUSH_NONE |
3008 RADEON_GMC_SRC_DATATYPE_COLOR |
3009 RADEON_ROP3_S |
3010 RADEON_DP_SRC_SOURCE_HOST_DATA |
3011 RADEON_GMC_CLR_CMP_CNTL_DIS |
3012 RADEON_GMC_WR_MSK_DIS |
3013 gmc);
3014
3015 PUT32(sc, RADEON_DP_CNTL,
3016 RADEON_DST_X_LEFT_TO_RIGHT |
3017 RADEON_DST_Y_TOP_TO_BOTTOM);
3018
3019 PUT32(sc, RADEON_SRC_X_Y, 0);
3020 PUT32(sc, RADEON_DST_X_Y, (xd << 16) | yd);
3021 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (w << 16) | h);
3022
3023 rf = (fg >> 16) & 0xff;
3024 rb = (bg >> 16) & 0xff;
3025 gf = (fg >> 8) & 0xff;
3026 gb = (bg >> 8) & 0xff;
3027 bf = fg & 0xff;
3028 bb = bg & 0xff;
3029
3030 /*
3031 * I doubt we can upload data faster than even the slowest Radeon
3032 * could process them, especially when doing the alpha blending stuff
3033 * along the way, so just make sure there's some room in the FIFO and
3034 * then hammer away
3035 * As it turns out we can, so make periodic stops to let the FIFO
3036 * drain.
3037 */
3038 radeonfb_wait_fifo(sc, 20);
3039 for (i = 0; i < ri->ri_fontscale; i++) {
3040 aval = *data;
3041 data++;
3042 if (aval == 0) {
3043 pixel = bg;
3044 } else if (aval == 255) {
3045 pixel = fg;
3046 } else {
3047 r = aval * rf + (255 - aval) * rb;
3048 g = aval * gf + (255 - aval) * gb;
3049 b = aval * bf + (255 - aval) * bb;
3050 pixel = (r & 0xff00) << 8 |
3051 (g & 0xff00) |
3052 (b & 0xff00) >> 8;
3053 }
3054 if (i & 16)
3055 radeonfb_wait_fifo(sc, 20);
3056 PUT32(sc, RADEON_HOST_DATA0, pixel);
3057 }
3058 if (rv == GC_ADD) {
3059 glyphcache_add(&dp->rd_gc, c, xd, yd);
3060 } else if (attr & 1)
3061 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
3062 }
3063
3064 static void
3065 radeonfb_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
3066 {
3067 struct rasops_info *ri = cookie;
3068 struct vcons_screen *scr = ri->ri_hw;
3069 struct radeonfb_display *dp = scr->scr_cookie;
3070 struct radeonfb_softc *sc = dp->rd_softc;
3071 struct wsdisplay_font *font = PICK_FONT(ri, c);
3072 uint32_t bg, fg, latch = 0, bg8, fg8, pixel, gmc;
3073 int i, x, y, wi, he, r, g, b, aval;
3074 int r1, g1, b1, r0, g0, b0, fgo, bgo;
3075 uint8_t *data8;
3076 int rv, cnt;
3077
3078 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
3079 return;
3080
3081 if (!CHAR_IN_FONT(c, font))
3082 return;
3083
3084 wi = font->fontwidth;
3085 he = font->fontheight;
3086
3087 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
3088 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
3089
3090 x = ri->ri_xorigin + col * wi;
3091 y = ri->ri_yorigin + row * he;
3092
3093 if (c == 0x20) {
3094 radeonfb_rectfill(dp, x, y, wi, he, bg);
3095 if (attr & 1)
3096 radeonfb_rectfill(dp, x, y + he - 2, wi, 1, fg);
3097 return;
3098 }
3099 rv = glyphcache_try(&dp->rd_gc, c, x, y, attr);
3100 if (rv == GC_OK)
3101 return;
3102
3103 data8 = WSFONT_GLYPH(c, font);
3104
3105 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3106
3107 radeonfb_wait_fifo(sc, 5);
3108
3109 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3110 RADEON_GMC_BRUSH_NONE |
3111 RADEON_GMC_SRC_DATATYPE_COLOR |
3112 RADEON_ROP3_S |
3113 RADEON_DP_SRC_SOURCE_HOST_DATA |
3114 RADEON_GMC_CLR_CMP_CNTL_DIS |
3115 RADEON_GMC_WR_MSK_DIS |
3116 gmc);
3117
3118 PUT32(sc, RADEON_DP_CNTL,
3119 RADEON_DST_X_LEFT_TO_RIGHT |
3120 RADEON_DST_Y_TOP_TO_BOTTOM);
3121
3122 PUT32(sc, RADEON_SRC_X_Y, 0);
3123 PUT32(sc, RADEON_DST_X_Y, (x << 16) | y);
3124 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (wi << 16) | he);
3125
3126 /*
3127 * we need the RGB colours here, so get offsets into rasops_cmap
3128 */
3129 fgo = ((attr >> 24) & 0xf) * 3;
3130 bgo = ((attr >> 16) & 0xf) * 3;
3131
3132 r0 = rasops_cmap[bgo];
3133 r1 = rasops_cmap[fgo];
3134 g0 = rasops_cmap[bgo + 1];
3135 g1 = rasops_cmap[fgo + 1];
3136 b0 = rasops_cmap[bgo + 2];
3137 b1 = rasops_cmap[fgo + 2];
3138 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
3139 bg8 = R3G3B2(r0, g0, b0);
3140 fg8 = R3G3B2(r1, g1, b1);
3141
3142 radeonfb_wait_fifo(sc, 20);
3143 cnt = 0;
3144 for (i = 0; i < ri->ri_fontscale; i++) {
3145 aval = *data8;
3146 if (aval == 0) {
3147 pixel = bg8;
3148 } else if (aval == 255) {
3149 pixel = fg8;
3150 } else {
3151 r = aval * r1 + (255 - aval) * r0;
3152 g = aval * g1 + (255 - aval) * g0;
3153 b = aval * b1 + (255 - aval) * b0;
3154 pixel = ((r & 0xe000) >> 8) |
3155 ((g & 0xe000) >> 11) |
3156 ((b & 0xc000) >> 14);
3157 }
3158 latch |= pixel << (8 * (i & 3));
3159 /* write in 32bit chunks */
3160 if ((i & 3) == 3) {
3161 PUT32(sc, RADEON_HOST_DATA0, latch);
3162 /*
3163 * not strictly necessary, old data should be shifted
3164 * out
3165 */
3166 latch = 0;
3167 cnt++;
3168 if (cnt > 16) {
3169 cnt = 0;
3170 radeonfb_wait_fifo(sc, 20);
3171 }
3172 }
3173 data8++;
3174 }
3175 /* if we have pixels left in latch write them out */
3176 if ((i & 3) != 0) {
3177 /*
3178 * radeon is weird - apparently leftover pixels are written
3179 * from the middle, not from the left as everything else
3180 */
3181 PUT32(sc, RADEON_HOST_DATA0, latch);
3182 }
3183
3184 if (rv == GC_ADD) {
3185 glyphcache_add(&dp->rd_gc, c, x, y);
3186 } else
3187 if (attr & 1)
3188 radeonfb_rectfill(dp, x, y + he - 2, wi, 1, fg);
3189 }
3190
3191 /*
3192 * wrapper for software character drawing
3193 * just sync the engine and call rasops*_putchar()
3194 */
3195
3196 #ifndef RADEONFB_ALWAYS_ACCEL_PUTCHAR
3197 static void
3198 radeonfb_putchar_wrapper(void *cookie, int row, int col, u_int c, long attr)
3199 {
3200 struct rasops_info *ri = cookie;
3201 struct vcons_screen *scr = ri->ri_hw;
3202 struct radeonfb_display *dp = scr->scr_cookie;
3203
3204 radeonfb_engine_idle(dp->rd_softc);
3205 dp->rd_putchar(ri, row, col, c, attr);
3206 }
3207 #endif
3208
3209 static void
3210 radeonfb_eraserows(void *cookie, int row, int nrows, long fillattr)
3211 {
3212 struct rasops_info *ri = cookie;
3213 struct vcons_screen *scr = ri->ri_hw;
3214 struct radeonfb_display *dp = scr->scr_cookie;
3215 uint32_t x, y, w, h, fg, bg, ul;
3216
3217 /* XXX: check for full emulation mode? */
3218 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3219 x = ri->ri_xorigin;
3220 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3221 w = ri->ri_emuwidth;
3222 h = ri->ri_font->fontheight * nrows;
3223
3224 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
3225 radeonfb_rectfill(dp, x, y, w, h, ri->ri_devcmap[bg & 0xf]);
3226 }
3227 }
3228
3229 static void
3230 radeonfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
3231 {
3232 struct rasops_info *ri = cookie;
3233 struct vcons_screen *scr = ri->ri_hw;
3234 struct radeonfb_display *dp = scr->scr_cookie;
3235 uint32_t x, ys, yd, w, h;
3236
3237 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3238 x = ri->ri_xorigin;
3239 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
3240 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
3241 w = ri->ri_emuwidth;
3242 h = ri->ri_font->fontheight * nrows;
3243 radeonfb_bitblt(dp, x, ys, x, yd, w, h,
3244 RADEON_ROP3_S);
3245 }
3246 }
3247
3248 static void
3249 radeonfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
3250 {
3251 struct rasops_info *ri = cookie;
3252 struct vcons_screen *scr = ri->ri_hw;
3253 struct radeonfb_display *dp = scr->scr_cookie;
3254 uint32_t xs, xd, y, w, h;
3255
3256 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3257 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
3258 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
3259 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3260 w = ri->ri_font->fontwidth * ncols;
3261 h = ri->ri_font->fontheight;
3262 radeonfb_bitblt(dp, xs, y, xd, y, w, h,
3263 RADEON_ROP3_S);
3264 }
3265 }
3266
3267 static void
3268 radeonfb_erasecols(void *cookie, int row, int startcol, int ncols,
3269 long fillattr)
3270 {
3271 struct rasops_info *ri = cookie;
3272 struct vcons_screen *scr = ri->ri_hw;
3273 struct radeonfb_display *dp = scr->scr_cookie;
3274 uint32_t x, y, w, h, fg, bg, ul;
3275
3276 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3277 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
3278 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3279 w = ri->ri_font->fontwidth * ncols;
3280 h = ri->ri_font->fontheight;
3281
3282 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
3283 radeonfb_rectfill(dp, x, y, w, h, ri->ri_devcmap[bg & 0xf]);
3284 }
3285 }
3286
3287 static void
3288 radeonfb_cursor(void *cookie, int on, int row, int col)
3289 {
3290 struct rasops_info *ri = cookie;
3291 struct vcons_screen *scr = ri->ri_hw;
3292 struct radeonfb_display *dp = scr->scr_cookie;
3293 int x, y, wi, he;
3294
3295 wi = ri->ri_font->fontwidth;
3296 he = ri->ri_font->fontheight;
3297
3298 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3299 x = ri->ri_ccol * wi + ri->ri_xorigin;
3300 y = ri->ri_crow * he + ri->ri_yorigin;
3301 /* first turn off the old cursor */
3302 if (ri->ri_flg & RI_CURSOR) {
3303 radeonfb_bitblt(dp, x, y, x, y, wi, he,
3304 RADEON_ROP3_Dn);
3305 ri->ri_flg &= ~RI_CURSOR;
3306 }
3307 ri->ri_crow = row;
3308 ri->ri_ccol = col;
3309 /* then (possibly) turn on the new one */
3310 if (on) {
3311 x = ri->ri_ccol * wi + ri->ri_xorigin;
3312 y = ri->ri_crow * he + ri->ri_yorigin;
3313 radeonfb_bitblt(dp, x, y, x, y, wi, he,
3314 RADEON_ROP3_Dn);
3315 ri->ri_flg |= RI_CURSOR;
3316 }
3317 } else {
3318 scr->scr_ri.ri_crow = row;
3319 scr->scr_ri.ri_ccol = col;
3320 scr->scr_ri.ri_flg &= ~RI_CURSOR;
3321 }
3322 }
3323
3324 /*
3325 * Underlying acceleration support.
3326 */
3327
3328 static void
3329 radeonfb_rectfill(struct radeonfb_display *dp, int dstx, int dsty,
3330 int width, int height, uint32_t color)
3331 {
3332 struct radeonfb_softc *sc = dp->rd_softc;
3333 uint32_t gmc;
3334
3335 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3336
3337 radeonfb_wait_fifo(sc, 6);
3338
3339 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3340 RADEON_GMC_BRUSH_SOLID_COLOR |
3341 RADEON_GMC_SRC_DATATYPE_COLOR |
3342 RADEON_GMC_CLR_CMP_CNTL_DIS |
3343 RADEON_ROP3_P | gmc);
3344
3345 PUT32(sc, RADEON_DP_BRUSH_FRGD_CLR, color);
3346 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3347 PUT32(sc, RADEON_DP_CNTL,
3348 RADEON_DST_X_LEFT_TO_RIGHT |
3349 RADEON_DST_Y_TOP_TO_BOTTOM);
3350 PUT32(sc, RADEON_DST_Y_X, (dsty << 16) | dstx);
3351 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (width << 16) | (height));
3352 }
3353
3354 static void
3355 radeonfb_rectfill_a(void *cookie, int dstx, int dsty,
3356 int width, int height, long attr)
3357 {
3358 struct radeonfb_display *dp = cookie;
3359
3360 radeonfb_rectfill(dp, dstx, dsty, width, height,
3361 dp->rd_vscreen.scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
3362 }
3363
3364 static void
3365 radeonfb_bitblt(void *cookie, int srcx, int srcy,
3366 int dstx, int dsty, int width, int height, int rop)
3367 {
3368 struct radeonfb_display *dp = cookie;
3369 struct radeonfb_softc *sc = dp->rd_softc;
3370 uint32_t gmc;
3371 uint32_t dir;
3372
3373 if (dsty < srcy) {
3374 dir = RADEON_DST_Y_TOP_TO_BOTTOM;
3375 } else {
3376 srcy += height - 1;
3377 dsty += height - 1;
3378 dir = 0;
3379 }
3380 if (dstx < srcx) {
3381 dir |= RADEON_DST_X_LEFT_TO_RIGHT;
3382 } else {
3383 srcx += width - 1;
3384 dstx += width - 1;
3385 }
3386
3387 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3388
3389 radeonfb_wait_fifo(sc, 6);
3390
3391 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3392 RADEON_GMC_BRUSH_SOLID_COLOR |
3393 RADEON_GMC_SRC_DATATYPE_COLOR |
3394 RADEON_GMC_CLR_CMP_CNTL_DIS |
3395 RADEON_DP_SRC_SOURCE_MEMORY |
3396 rop | gmc);
3397
3398 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3399 PUT32(sc, RADEON_DP_CNTL, dir);
3400 PUT32(sc, RADEON_SRC_Y_X, (srcy << 16) | srcx);
3401 PUT32(sc, RADEON_DST_Y_X, (dsty << 16) | dstx);
3402 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (width << 16) | (height));
3403 }
3404
3405 static void
3406 radeonfb_engine_idle(struct radeonfb_softc *sc)
3407 {
3408
3409 radeonfb_wait_fifo(sc, 64);
3410 while ((GET32(sc, RADEON_RBBM_STATUS) &
3411 RADEON_RBBM_ACTIVE) != 0);
3412 radeonfb_engine_flush(sc);
3413 }
3414
3415 static inline void
3416 radeonfb_wait_fifo(struct radeonfb_softc *sc, int n)
3417 {
3418 int i;
3419
3420 for (i = RADEON_TIMEOUT; i; i--) {
3421 if ((GET32(sc, RADEON_RBBM_STATUS) &
3422 RADEON_RBBM_FIFOCNT_MASK) >= n)
3423 return;
3424 }
3425 #ifdef DIAGNOSTIC
3426 if (!i)
3427 printf("%s: timed out waiting for fifo (%x)\n",
3428 XNAME(sc), GET32(sc, RADEON_RBBM_STATUS));
3429 #endif
3430 }
3431
3432 static void
3433 radeonfb_engine_flush(struct radeonfb_softc *sc)
3434 {
3435 int i = 0;
3436
3437 if (IS_R300(sc)) {
3438 SET32(sc, R300_DSTCACHE_CTLSTAT, R300_RB2D_DC_FLUSH_ALL);
3439 while (GET32(sc, R300_DSTCACHE_CTLSTAT) & R300_RB2D_DC_BUSY) {
3440 i++;
3441 }
3442 } else {
3443 SET32(sc, RADEON_RB2D_DSTCACHE_CTLSTAT,
3444 RADEON_RB2D_DC_FLUSH_ALL);
3445 while (GET32(sc, RADEON_RB2D_DSTCACHE_CTLSTAT) &
3446 RADEON_RB2D_DC_BUSY) {
3447 i++;
3448 }
3449 }
3450 #ifdef DIAGNOSTIC
3451 if (i > RADEON_TIMEOUT)
3452 printf("%s: engine flush timed out!\n", XNAME(sc));
3453 #endif
3454 }
3455
3456 static inline void
3457 radeonfb_unclip(struct radeonfb_softc *sc)
3458 {
3459
3460 radeonfb_wait_fifo(sc, 2);
3461 PUT32(sc, RADEON_SC_TOP_LEFT, 0);
3462 PUT32(sc, RADEON_SC_BOTTOM_RIGHT, 0x1fff1fff);
3463 }
3464
3465 static void
3466 radeonfb_engine_init(struct radeonfb_display *dp)
3467 {
3468 struct radeonfb_softc *sc = dp->rd_softc;
3469 uint32_t pitch;
3470
3471 /* no 3D */
3472 PUT32(sc, RADEON_RB3D_CNTL, 0);
3473
3474 radeonfb_engine_reset(sc);
3475 pitch = ((dp->rd_virtx * (dp->rd_bpp / 8) + 0x3f)) >> 6;
3476
3477 radeonfb_wait_fifo(sc, 1);
3478 if (!IS_R300(sc))
3479 PUT32(sc, RADEON_RB2D_DSTCACHE_MODE, 0);
3480
3481 radeonfb_wait_fifo(sc, 3);
3482 PUT32(sc, RADEON_DEFAULT_PITCH_OFFSET,
3483 (pitch << 22) | (sc->sc_aperbase >> 10));
3484
3485
3486 PUT32(sc, RADEON_DST_PITCH_OFFSET,
3487 (pitch << 22) | (sc->sc_aperbase >> 10));
3488 PUT32(sc, RADEON_SRC_PITCH_OFFSET,
3489 (pitch << 22) | (sc->sc_aperbase >> 10));
3490
3491 (void)GET32(sc, RADEON_DP_DATATYPE);
3492
3493 /* default scissors -- no clipping */
3494 radeonfb_wait_fifo(sc, 1);
3495 PUT32(sc, RADEON_DEFAULT_SC_BOTTOM_RIGHT,
3496 RADEON_DEFAULT_SC_RIGHT_MAX | RADEON_DEFAULT_SC_BOTTOM_MAX);
3497
3498 radeonfb_wait_fifo(sc, 1);
3499 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3500 (dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT) |
3501 RADEON_GMC_CLR_CMP_CNTL_DIS |
3502 RADEON_GMC_BRUSH_SOLID_COLOR |
3503 RADEON_GMC_SRC_DATATYPE_COLOR);
3504
3505 radeonfb_wait_fifo(sc, 10);
3506 PUT32(sc, RADEON_DST_LINE_START, 0);
3507 PUT32(sc, RADEON_DST_LINE_END, 0);
3508 PUT32(sc, RADEON_DP_BRUSH_FRGD_CLR, 0xffffffff);
3509 PUT32(sc, RADEON_DP_BRUSH_BKGD_CLR, 0);
3510 PUT32(sc, RADEON_DP_SRC_FRGD_CLR, 0xffffffff);
3511 PUT32(sc, RADEON_DP_SRC_BKGD_CLR, 0);
3512 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3513 PUT32(sc, RADEON_SC_TOP_LEFT, 0);
3514 PUT32(sc, RADEON_SC_BOTTOM_RIGHT, 0x1fff1fff);
3515 PUT32(sc, RADEON_AUX_SC_CNTL, 0);
3516 radeonfb_engine_idle(sc);
3517 }
3518
3519 static void
3520 radeonfb_engine_reset(struct radeonfb_softc *sc)
3521 {
3522 uint32_t hpc, rbbm, mclkcntl, clkindex;
3523
3524 radeonfb_engine_flush(sc);
3525
3526 clkindex = GET32(sc, RADEON_CLOCK_CNTL_INDEX);
3527 if (HAS_R300CG(sc))
3528 radeonfb_r300cg_workaround(sc);
3529 mclkcntl = GETPLL(sc, RADEON_MCLK_CNTL);
3530
3531 /*
3532 * According to comments in XFree code, resetting the HDP via
3533 * the RBBM_SOFT_RESET can cause bad behavior on some systems.
3534 * So we use HOST_PATH_CNTL instead.
3535 */
3536
3537 hpc = GET32(sc, RADEON_HOST_PATH_CNTL);
3538 rbbm = GET32(sc, RADEON_RBBM_SOFT_RESET);
3539 if (IS_R300(sc)) {
3540 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm |
3541 RADEON_SOFT_RESET_CP |
3542 RADEON_SOFT_RESET_HI |
3543 RADEON_SOFT_RESET_E2);
3544 GET32(sc, RADEON_RBBM_SOFT_RESET);
3545 PUT32(sc, RADEON_RBBM_SOFT_RESET, 0);
3546 /*
3547 * XXX: this bit is not defined in any ATI docs I have,
3548 * nor in the XFree code, but XFree does it. Why?
3549 */
3550 SET32(sc, RADEON_RB2D_DSTCACHE_MODE, (1<<17));
3551 } else {
3552 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm |
3553 RADEON_SOFT_RESET_CP |
3554 RADEON_SOFT_RESET_SE |
3555 RADEON_SOFT_RESET_RE |
3556 RADEON_SOFT_RESET_PP |
3557 RADEON_SOFT_RESET_E2 |
3558 RADEON_SOFT_RESET_RB);
3559 GET32(sc, RADEON_RBBM_SOFT_RESET);
3560 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm &
3561 ~(RADEON_SOFT_RESET_CP |
3562 RADEON_SOFT_RESET_SE |
3563 RADEON_SOFT_RESET_RE |
3564 RADEON_SOFT_RESET_PP |
3565 RADEON_SOFT_RESET_E2 |
3566 RADEON_SOFT_RESET_RB));
3567 GET32(sc, RADEON_RBBM_SOFT_RESET);
3568 }
3569
3570 PUT32(sc, RADEON_HOST_PATH_CNTL, hpc | RADEON_HDP_SOFT_RESET);
3571 GET32(sc, RADEON_HOST_PATH_CNTL);
3572 PUT32(sc, RADEON_HOST_PATH_CNTL, hpc);
3573
3574 if (IS_R300(sc))
3575 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm);
3576
3577 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, clkindex);
3578 PRINTREG(RADEON_CLOCK_CNTL_INDEX);
3579 PUTPLL(sc, RADEON_MCLK_CNTL, mclkcntl);
3580
3581 if (HAS_R300CG(sc))
3582 radeonfb_r300cg_workaround(sc);
3583 }
3584
3585 static int
3586 radeonfb_set_curpos(struct radeonfb_display *dp, struct wsdisplay_curpos *pos)
3587 {
3588 int x, y;
3589
3590 x = pos->x;
3591 y = pos->y;
3592
3593 /*
3594 * This doesn't let a cursor move off the screen. I'm not
3595 * sure if this will have negative effects for e.g. Xinerama.
3596 * I'd guess Xinerama handles it by changing the cursor shape,
3597 * but that needs verification.
3598 */
3599 if (x >= dp->rd_virtx)
3600 x = dp->rd_virtx - 1;
3601 if (x < 0)
3602 x = 0;
3603 if (y >= dp->rd_virty)
3604 y = dp->rd_virty - 1;
3605 if (y < 0)
3606 y = 0;
3607
3608 dp->rd_cursor.rc_pos.x = x;
3609 dp->rd_cursor.rc_pos.y = y;
3610
3611 radeonfb_cursor_position(dp);
3612 return 0;
3613 }
3614
3615 static int
3616 radeonfb_set_cursor(struct radeonfb_display *dp, struct wsdisplay_cursor *wc)
3617 {
3618 unsigned flags;
3619
3620 uint8_t r[2], g[2], b[2];
3621 unsigned index, count;
3622 int i, err;
3623 int pitch, size;
3624 struct radeonfb_cursor nc;
3625
3626 flags = wc->which;
3627
3628 /* copy old values */
3629 nc = dp->rd_cursor;
3630
3631 if (flags & WSDISPLAY_CURSOR_DOCMAP) {
3632 index = wc->cmap.index;
3633 count = wc->cmap.count;
3634
3635 if (index >= 2 || count > 2 - index)
3636 return EINVAL;
3637
3638 err = copyin(wc->cmap.red, &r[index], count);
3639 if (err)
3640 return err;
3641 err = copyin(wc->cmap.green, &g[index], count);
3642 if (err)
3643 return err;
3644 err = copyin(wc->cmap.blue, &b[index], count);
3645 if (err)
3646 return err;
3647
3648 for (i = index; i < index + count; i++) {
3649 nc.rc_cmap[i] =
3650 (r[i] << 16) + (g[i] << 8) + (b[i] << 0);
3651 }
3652 }
3653
3654 if (flags & WSDISPLAY_CURSOR_DOSHAPE) {
3655 if ((wc->size.x > RADEON_CURSORMAXX) ||
3656 (wc->size.y > RADEON_CURSORMAXY))
3657 return EINVAL;
3658
3659 /* figure bytes per line */
3660 pitch = (wc->size.x + 7) / 8;
3661 size = pitch * wc->size.y;
3662
3663 /* clear the old cursor and mask */
3664 memset(nc.rc_image, 0, 512);
3665 memset(nc.rc_mask, 0, 512);
3666
3667 nc.rc_size = wc->size;
3668
3669 if ((err = copyin(wc->image, nc.rc_image, size)) != 0)
3670 return err;
3671
3672 if ((err = copyin(wc->mask, nc.rc_mask, size)) != 0)
3673 return err;
3674 }
3675
3676 if (flags & WSDISPLAY_CURSOR_DOHOT) {
3677 nc.rc_hot = wc->hot;
3678 if (nc.rc_hot.x >= nc.rc_size.x)
3679 nc.rc_hot.x = nc.rc_size.x - 1;
3680 if (nc.rc_hot.y >= nc.rc_size.y)
3681 nc.rc_hot.y = nc.rc_size.y - 1;
3682 }
3683
3684 if (flags & WSDISPLAY_CURSOR_DOPOS) {
3685 nc.rc_pos = wc->pos;
3686 if (nc.rc_pos.x >= dp->rd_virtx)
3687 nc.rc_pos.x = dp->rd_virtx - 1;
3688 #if 0
3689 if (nc.rc_pos.x < 0)
3690 nc.rc_pos.x = 0;
3691 #endif
3692 if (nc.rc_pos.y >= dp->rd_virty)
3693 nc.rc_pos.y = dp->rd_virty - 1;
3694 #if 0
3695 if (nc.rc_pos.y < 0)
3696 nc.rc_pos.y = 0;
3697 #endif
3698 }
3699 if (flags & WSDISPLAY_CURSOR_DOCUR) {
3700 nc.rc_visible = wc->enable;
3701 }
3702
3703 dp->rd_cursor = nc;
3704 radeonfb_cursor_update(dp, wc->which);
3705
3706 return 0;
3707 }
3708
3709 static uint8_t
3710 radeonfb_backwards(uint8_t d)
3711 {
3712 uint8_t l;
3713
3714 l = d << 7;
3715 l |= ((d & 0x02) << 5);
3716 l |= ((d & 0x04) << 3);
3717 l |= ((d & 0x08) << 1);
3718 l |= ((d & 0x10) >> 1);
3719 l |= ((d & 0x20) >> 3);
3720 l |= ((d & 0x40) >> 5);
3721 l |= ((d & 0x80) >> 7);
3722 return l;
3723 }
3724
3725 /*
3726 * Change the cursor shape. Call this with the cursor locked to avoid
3727 * flickering/tearing.
3728 */
3729 static void
3730 radeonfb_cursor_shape(struct radeonfb_display *dp)
3731 {
3732 uint8_t and[512], xor[512];
3733 int i, j, src, dst /* , pitch */;
3734 const uint8_t *msk = dp->rd_cursor.rc_mask;
3735 const uint8_t *img = dp->rd_cursor.rc_image;
3736
3737 /*
3738 * Radeon cursor data interleaves one line of AND data followed
3739 * by a line of XOR data. (Each line corresponds to a whole hardware
3740 * pitch - i.e. 64 pixels or 8 bytes.)
3741 *
3742 * The cursor is displayed using the following table:
3743 *
3744 * AND XOR Result
3745 * ----------------------
3746 * 0 0 Cursor color 0
3747 * 0 1 Cursor color 1
3748 * 1 0 Transparent
3749 * 1 1 Complement of background
3750 *
3751 * Our masks are therefore different from what we were passed.
3752 * Passed in, I'm assuming the data represents either color 0 or 1,
3753 * and a mask, so the passed in table looks like:
3754 *
3755 * IMG Mask Result
3756 * -----------------------
3757 * 0 0 Transparent
3758 * 0 1 Cursor color 0
3759 * 1 0 Transparent
3760 * 1 1 Cursor color 1
3761 *
3762 * IF mask bit == 1, AND = 0, XOR = color.
3763 * IF mask bit == 0, AND = 1, XOR = 0.
3764 *
3765 * hence: AND = ~(mask); XOR = color & ~(mask);
3766 */
3767
3768 /* pitch = ((dp->rd_cursor.rc_size.x + 7) / 8); */
3769
3770 /* start by assuming all bits are transparent */
3771 memset(and, 0xff, 512);
3772 memset(xor, 0x00, 512);
3773
3774 src = 0;
3775 dst = 0;
3776 for (i = 0; i < 64; i++) {
3777 for (j = 0; j < 64; j += 8) {
3778 if ((i < dp->rd_cursor.rc_size.y) &&
3779 (j < dp->rd_cursor.rc_size.x)) {
3780
3781 /* take care to leave odd bits alone */
3782 and[dst] &= ~(msk[src]);
3783 xor[dst] = img[src] & msk[src];
3784 src++;
3785 }
3786 dst++;
3787 }
3788 }
3789
3790 for (i = 0; i < 512; i++) {
3791 and[i] = radeonfb_backwards(and[i]);
3792 xor[i] = radeonfb_backwards(xor[i]);
3793 }
3794
3795 /* copy the image into place */
3796 for (i = 0; i < 64; i++) {
3797 memcpy((uint8_t *)dp->rd_curptr + (i * 16),
3798 &and[i * 8], 8);
3799 memcpy((uint8_t *)dp->rd_curptr + (i * 16) + 8,
3800 &xor[i * 8], 8);
3801 }
3802 }
3803
3804 static void
3805 radeonfb_cursor_position(struct radeonfb_display *dp)
3806 {
3807 struct radeonfb_softc *sc = dp->rd_softc;
3808 uint32_t offset, hvoff, hvpos; /* registers */
3809 uint32_t coff; /* cursor offset */
3810 int i, x, y, xoff, yoff, crtcoff;
3811
3812 /*
3813 * XXX: this also needs to handle pan/scan
3814 */
3815 for (i = 0; i < dp->rd_ncrtcs; i++) {
3816
3817 struct radeonfb_crtc *rcp = &dp->rd_crtcs[i];
3818
3819 if (rcp->rc_number) {
3820 offset = RADEON_CUR2_OFFSET;
3821 hvoff = RADEON_CUR2_HORZ_VERT_OFF;
3822 hvpos = RADEON_CUR2_HORZ_VERT_POSN;
3823 crtcoff = RADEON_CRTC2_OFFSET;
3824 } else {
3825 offset = RADEON_CUR_OFFSET;
3826 hvoff = RADEON_CUR_HORZ_VERT_OFF;
3827 hvpos = RADEON_CUR_HORZ_VERT_POSN;
3828 crtcoff = RADEON_CRTC_OFFSET;
3829 }
3830
3831 x = dp->rd_cursor.rc_pos.x;
3832 y = dp->rd_cursor.rc_pos.y;
3833
3834 while (y < rcp->rc_yoffset) {
3835 rcp->rc_yoffset -= RADEON_PANINCREMENT;
3836 }
3837 while (y >= (rcp->rc_yoffset + rcp->rc_videomode.vdisplay)) {
3838 rcp->rc_yoffset += RADEON_PANINCREMENT;
3839 }
3840 while (x < rcp->rc_xoffset) {
3841 rcp->rc_xoffset -= RADEON_PANINCREMENT;
3842 }
3843 while (x >= (rcp->rc_xoffset + rcp->rc_videomode.hdisplay)) {
3844 rcp->rc_xoffset += RADEON_PANINCREMENT;
3845 }
3846
3847 /* adjust for the cursor's hotspot */
3848 x -= dp->rd_cursor.rc_hot.x;
3849 y -= dp->rd_cursor.rc_hot.y;
3850 xoff = yoff = 0;
3851
3852 if (x >= dp->rd_virtx)
3853 x = dp->rd_virtx - 1;
3854 if (y >= dp->rd_virty)
3855 y = dp->rd_virty - 1;
3856
3857 /* now adjust cursor so it is relative to viewport */
3858 x -= rcp->rc_xoffset;
3859 y -= rcp->rc_yoffset;
3860
3861 /*
3862 * no need to check for fall off, because we should
3863 * never move off the screen entirely!
3864 */
3865 coff = 0;
3866 if (x < 0) {
3867 xoff = -x;
3868 x = 0;
3869 }
3870 if (y < 0) {
3871 yoff = -y;
3872 y = 0;
3873 coff = (yoff * 2) * 8;
3874 }
3875
3876 /* pan the display */
3877 PUT32(sc, crtcoff, (rcp->rc_yoffset * dp->rd_stride) +
3878 rcp->rc_xoffset);
3879
3880 PUT32(sc, offset, (dp->rd_curoff + coff) | RADEON_CUR_LOCK);
3881 PUT32(sc, hvoff, (xoff << 16) | (yoff) | RADEON_CUR_LOCK);
3882 /* NB: this unlocks the cursor */
3883 PUT32(sc, hvpos, (x << 16) | y);
3884 }
3885 }
3886
3887 static void
3888 radeonfb_cursor_visible(struct radeonfb_display *dp)
3889 {
3890 int i;
3891 uint32_t gencntl, bit;
3892
3893 for (i = 0; i < dp->rd_ncrtcs; i++) {
3894 if (dp->rd_crtcs[i].rc_number) {
3895 gencntl = RADEON_CRTC2_GEN_CNTL;
3896 bit = RADEON_CRTC2_CUR_EN;
3897 } else {
3898 gencntl = RADEON_CRTC_GEN_CNTL;
3899 bit = RADEON_CRTC_CUR_EN;
3900 }
3901
3902 if (dp->rd_cursor.rc_visible)
3903 SET32(dp->rd_softc, gencntl, bit);
3904 else
3905 CLR32(dp->rd_softc, gencntl, bit);
3906 }
3907 }
3908
3909 static void
3910 radeonfb_cursor_cmap(struct radeonfb_display *dp)
3911 {
3912 int i;
3913 uint32_t c0reg, c1reg;
3914 struct radeonfb_softc *sc = dp->rd_softc;
3915
3916 for (i = 0; i < dp->rd_ncrtcs; i++) {
3917 if (dp->rd_crtcs[i].rc_number) {
3918 c0reg = RADEON_CUR2_CLR0;
3919 c1reg = RADEON_CUR2_CLR1;
3920 } else {
3921 c0reg = RADEON_CUR_CLR0;
3922 c1reg = RADEON_CUR_CLR1;
3923 }
3924
3925 PUT32(sc, c0reg, dp->rd_cursor.rc_cmap[0]);
3926 PUT32(sc, c1reg, dp->rd_cursor.rc_cmap[1]);
3927 }
3928 }
3929
3930 static void
3931 radeonfb_cursor_update(struct radeonfb_display *dp, unsigned which)
3932 {
3933 struct radeonfb_softc *sc;
3934 int i;
3935
3936 sc = dp->rd_softc;
3937 for (i = 0; i < dp->rd_ncrtcs; i++) {
3938 if (dp->rd_crtcs[i].rc_number) {
3939 SET32(sc, RADEON_CUR2_OFFSET, RADEON_CUR_LOCK);
3940 } else {
3941 SET32(sc, RADEON_CUR_OFFSET,RADEON_CUR_LOCK);
3942 }
3943 }
3944
3945 if (which & WSDISPLAY_CURSOR_DOCMAP)
3946 radeonfb_cursor_cmap(dp);
3947
3948 if (which & WSDISPLAY_CURSOR_DOSHAPE)
3949 radeonfb_cursor_shape(dp);
3950
3951 if (which & WSDISPLAY_CURSOR_DOCUR)
3952 radeonfb_cursor_visible(dp);
3953
3954 /* this one is unconditional, because it updates other stuff */
3955 radeonfb_cursor_position(dp);
3956 }
3957
3958 static struct videomode *
3959 radeonfb_best_refresh(struct videomode *m1, struct videomode *m2)
3960 {
3961 int r1, r2;
3962
3963 /* otherwise pick the higher refresh rate */
3964 r1 = DIVIDE(DIVIDE(m1->dot_clock, m1->htotal), m1->vtotal);
3965 r2 = DIVIDE(DIVIDE(m2->dot_clock, m2->htotal), m2->vtotal);
3966
3967 return (r1 < r2 ? m2 : m1);
3968 }
3969
3970 static const struct videomode *
3971 radeonfb_port_mode(struct radeonfb_softc *sc, struct radeonfb_port *rp,
3972 int x, int y)
3973 {
3974 struct edid_info *ep = &rp->rp_edid;
3975 struct videomode *vmp = NULL;
3976 int i;
3977
3978 if (!rp->rp_edid_valid) {
3979 /* fallback to safe mode */
3980 return radeonfb_modelookup(sc->sc_defaultmode);
3981 }
3982
3983 /* always choose the preferred mode first! */
3984 if (ep->edid_preferred_mode) {
3985
3986 /* XXX: add auto-stretching support for native mode */
3987
3988 /* this may want panning to occur, btw */
3989 if ((ep->edid_preferred_mode->hdisplay <= x) &&
3990 (ep->edid_preferred_mode->vdisplay <= y))
3991 return ep->edid_preferred_mode;
3992 }
3993
3994 for (i = 0; i < ep->edid_nmodes; i++) {
3995 /*
3996 * We elect to pick a resolution that is too large for
3997 * the monitor than one that is too small. This means
3998 * that we will prefer to pan rather than to try to
3999 * center a smaller display on a larger screen. In
4000 * practice, this shouldn't matter because if a
4001 * monitor can support a larger resolution, it can
4002 * probably also support the smaller. A specific
4003 * exception is fixed format panels, but hopefully
4004 * they are properly dealt with by the "autostretch"
4005 * logic above.
4006 */
4007 if ((ep->edid_modes[i].hdisplay > x) ||
4008 (ep->edid_modes[i].vdisplay > y)) {
4009 continue;
4010 }
4011
4012 /*
4013 * at this point, the display mode is no larger than
4014 * what we've requested.
4015 */
4016 if (vmp == NULL)
4017 vmp = &ep->edid_modes[i];
4018
4019 /* eliminate smaller modes */
4020 if ((vmp->hdisplay >= ep->edid_modes[i].hdisplay) ||
4021 (vmp->vdisplay >= ep->edid_modes[i].vdisplay))
4022 continue;
4023
4024 if ((vmp->hdisplay < ep->edid_modes[i].hdisplay) ||
4025 (vmp->vdisplay < ep->edid_modes[i].vdisplay)) {
4026 vmp = &ep->edid_modes[i];
4027 continue;
4028 }
4029
4030 KASSERT(vmp->hdisplay == ep->edid_modes[i].hdisplay);
4031 KASSERT(vmp->vdisplay == ep->edid_modes[i].vdisplay);
4032
4033 vmp = radeonfb_best_refresh(vmp, &ep->edid_modes[i]);
4034 }
4035
4036 return (vmp ? vmp : radeonfb_modelookup(sc->sc_defaultmode));
4037 }
4038
4039 static int
4040 radeonfb_hasres(struct videomode *list, int nlist, int x, int y)
4041 {
4042 int i;
4043
4044 for (i = 0; i < nlist; i++) {
4045 if ((x == list[i].hdisplay) &&
4046 (y == list[i].vdisplay)) {
4047 return 1;
4048 }
4049 }
4050 return 0;
4051 }
4052
4053 static void
4054 radeonfb_pickres(struct radeonfb_display *dp, uint16_t *x, uint16_t *y,
4055 int pan)
4056 {
4057 struct radeonfb_port *rp;
4058 struct edid_info *ep;
4059 int i, j;
4060
4061 *x = 0;
4062 *y = 0;
4063
4064 if (pan) {
4065 for (i = 0; i < dp->rd_ncrtcs; i++) {
4066 rp = dp->rd_crtcs[i].rc_port;
4067 ep = &rp->rp_edid;
4068 if (!rp->rp_edid_valid) {
4069 /* monitor not present */
4070 continue;
4071 }
4072
4073 /*
4074 * For now we are ignoring "conflict" that
4075 * could occur when mixing some modes like
4076 * 1280x1024 and 1400x800. It isn't clear
4077 * which is better, so the first one wins.
4078 */
4079 for (j = 0; j < ep->edid_nmodes; j++) {
4080 /*
4081 * ignore resolutions that are too big for
4082 * the radeon
4083 */
4084 if (ep->edid_modes[j].hdisplay >
4085 dp->rd_softc->sc_maxx)
4086 continue;
4087 if (ep->edid_modes[j].vdisplay >
4088 dp->rd_softc->sc_maxy)
4089 continue;
4090
4091 /*
4092 * pick largest resolution, the
4093 * smaller monitor will pan
4094 */
4095 if ((ep->edid_modes[j].hdisplay >= *x) &&
4096 (ep->edid_modes[j].vdisplay >= *y)) {
4097 *x = ep->edid_modes[j].hdisplay;
4098 *y = ep->edid_modes[j].vdisplay;
4099 }
4100 }
4101 }
4102
4103 } else {
4104 struct videomode modes[64];
4105 int nmodes = 0;
4106 int valid = 0;
4107
4108 for (i = 0; i < dp->rd_ncrtcs; i++) {
4109 /*
4110 * pick the largest resolution in common.
4111 */
4112 rp = dp->rd_crtcs[i].rc_port;
4113 ep = &rp->rp_edid;
4114
4115 if (!rp->rp_edid_valid)
4116 continue;
4117
4118 if (!valid) {
4119 /*
4120 * Pick the preferred mode for this port
4121 * if available.
4122 */
4123 if (ep->edid_preferred_mode) {
4124 struct videomode *vmp =
4125 ep->edid_preferred_mode;
4126
4127 if ((vmp->hdisplay <=
4128 dp->rd_softc->sc_maxx) &&
4129 (vmp->vdisplay <=
4130 dp->rd_softc->sc_maxy))
4131 modes[nmodes++] = *vmp;
4132 } else {
4133
4134 /* initialize starting list */
4135 for (j = 0; j < ep->edid_nmodes; j++) {
4136 /*
4137 * ignore resolutions that are
4138 * too big for the radeon
4139 */
4140 if (ep->edid_modes[j].hdisplay >
4141 dp->rd_softc->sc_maxx)
4142 continue;
4143 if (ep->edid_modes[j].vdisplay >
4144 dp->rd_softc->sc_maxy)
4145 continue;
4146
4147 modes[nmodes] =
4148 ep->edid_modes[j];
4149 nmodes++;
4150 }
4151 }
4152 valid = 1;
4153 } else {
4154 /* merge into preexisting list */
4155 for (j = 0; j < nmodes; j++) {
4156 if (!radeonfb_hasres(ep->edid_modes,
4157 ep->edid_nmodes,
4158 modes[j].hdisplay,
4159 modes[j].vdisplay)) {
4160 modes[j] = modes[nmodes];
4161 j--;
4162 nmodes--;
4163 }
4164 }
4165 }
4166 }
4167
4168 /* now we have to pick from the merged list */
4169 for (i = 0; i < nmodes; i++) {
4170 if ((modes[i].hdisplay >= *x) &&
4171 (modes[i].vdisplay >= *y)) {
4172 *x = modes[i].hdisplay;
4173 *y = modes[i].vdisplay;
4174 }
4175 }
4176 }
4177
4178 if ((*x == 0) || (*y == 0)) {
4179 /* fallback to safe mode */
4180 *x = 640;
4181 *y = 480;
4182 }
4183 }
4184
4185 /*
4186 * backlight levels are linear on:
4187 * - RV200, RV250, RV280, RV350
4188 * - but NOT on PowerBook4,3 6,3 6,5
4189 * according to Linux' radeonfb
4190 */
4191
4192 /* Get the current backlight level for the display. */
4193
4194 static int
4195 radeonfb_get_backlight(struct radeonfb_display *dp)
4196 {
4197 int s;
4198 uint32_t level;
4199
4200 s = spltty();
4201
4202 level = radeonfb_get32(dp->rd_softc, RADEON_LVDS_GEN_CNTL);
4203 level &= RADEON_LVDS_BL_MOD_LEV_MASK;
4204 level >>= RADEON_LVDS_BL_MOD_LEV_SHIFT;
4205
4206 /*
4207 * On some chips, we should negate the backlight level.
4208 * XXX Find out on which chips.
4209 */
4210 if (dp->rd_softc->sc_flags & RFB_INV_BLIGHT)
4211 level = RADEONFB_BACKLIGHT_MAX - level;
4212
4213 splx(s);
4214
4215 return level;
4216 }
4217
4218 /* Set the backlight to the given level for the display. */
4219 static void
4220 radeonfb_switch_backlight(struct radeonfb_display *dp, int on)
4221 {
4222 if (dp->rd_bl_on == on)
4223 return;
4224 dp->rd_bl_on = on;
4225 radeonfb_set_backlight(dp, dp->rd_bl_level);
4226 }
4227
4228 static int
4229 radeonfb_set_backlight(struct radeonfb_display *dp, int level)
4230 {
4231 struct radeonfb_softc *sc = dp->rd_softc;
4232 int rlevel, s;
4233 uint32_t lvds;
4234
4235 if(!sc->sc_mapped)
4236 return 0;
4237
4238 s = spltty();
4239
4240 dp->rd_bl_level = level;
4241 if (dp->rd_bl_on == 0)
4242 level = 0;
4243
4244 if (level < 0)
4245 level = 0;
4246 else if (level >= RADEONFB_BACKLIGHT_MAX)
4247 level = RADEONFB_BACKLIGHT_MAX;
4248
4249 /* On some chips, we should negate the backlight level. */
4250 if (dp->rd_softc->sc_flags & RFB_INV_BLIGHT) {
4251 rlevel = RADEONFB_BACKLIGHT_MAX - level;
4252 } else
4253 rlevel = level;
4254
4255 callout_stop(&dp->rd_bl_lvds_co);
4256 radeonfb_engine_idle(sc);
4257
4258 /*
4259 * Turn off the display if the backlight is set to 0, since the
4260 * display is useless without backlight anyway.
4261 */
4262 if (level == 0)
4263 radeonfb_blank(dp, 1);
4264 else if (radeonfb_get_backlight(dp) == 0)
4265 radeonfb_blank(dp, 0);
4266
4267 lvds = radeonfb_get32(sc, RADEON_LVDS_GEN_CNTL);
4268 lvds &= ~RADEON_LVDS_DISPLAY_DIS;
4269 if (!(lvds & RADEON_LVDS_BLON) || !(lvds & RADEON_LVDS_ON)) {
4270 lvds |= dp->rd_bl_lvds_val & RADEON_LVDS_DIGON;
4271 lvds |= RADEON_LVDS_BLON | RADEON_LVDS_EN;
4272 radeonfb_put32(sc, RADEON_LVDS_GEN_CNTL, lvds);
4273 lvds &= ~RADEON_LVDS_BL_MOD_LEV_MASK;
4274 lvds |= rlevel << RADEON_LVDS_BL_MOD_LEV_SHIFT;
4275 lvds |= RADEON_LVDS_ON;
4276 lvds |= dp->rd_bl_lvds_val & RADEON_LVDS_BL_MOD_EN;
4277 } else {
4278 lvds &= ~RADEON_LVDS_BL_MOD_LEV_MASK;
4279 lvds |= rlevel << RADEON_LVDS_BL_MOD_LEV_SHIFT;
4280 radeonfb_put32(sc, RADEON_LVDS_GEN_CNTL, lvds);
4281 }
4282
4283 dp->rd_bl_lvds_val &= ~RADEON_LVDS_STATE_MASK;
4284 dp->rd_bl_lvds_val |= lvds & RADEON_LVDS_STATE_MASK;
4285 /* XXX What is the correct delay? */
4286 callout_schedule(&dp->rd_bl_lvds_co, 200 * hz);
4287
4288 splx(s);
4289
4290 return 0;
4291 }
4292
4293 /*
4294 * Callout function for delayed operations on the LVDS_GEN_CNTL register.
4295 * Set the delayed bits in the register, and clear the stored delayed
4296 * value.
4297 */
4298
4299 static void radeonfb_lvds_callout(void *arg)
4300 {
4301 struct radeonfb_display *dp = arg;
4302 int s;
4303
4304 s = splhigh();
4305
4306 radeonfb_mask32(dp->rd_softc, RADEON_LVDS_GEN_CNTL, ~0,
4307 dp->rd_bl_lvds_val);
4308 dp->rd_bl_lvds_val = 0;
4309
4310 splx(s);
4311 }
4312
4313 static void
4314 radeonfb_brightness_up(device_t dev)
4315 {
4316 struct radeonfb_softc *sc = device_private(dev);
4317 struct radeonfb_display *dp = &sc->sc_displays[0];
4318 int level;
4319
4320 /* we assume the main display is the first one - need a better way */
4321 if (sc->sc_ndisplays < 1) return;
4322 /* make sure pushing the hotkeys always has an effect */
4323 dp->rd_bl_on = 1;
4324 level = dp->rd_bl_level;
4325 level = min(RADEONFB_BACKLIGHT_MAX, level + 5);
4326 radeonfb_set_backlight(dp, level);
4327 }
4328
4329 static void
4330 radeonfb_brightness_down(device_t dev)
4331 {
4332 struct radeonfb_softc *sc = device_private(dev);
4333 struct radeonfb_display *dp = &sc->sc_displays[0];
4334 int level;
4335
4336 /* we assume the main display is the first one - need a better way */
4337 if (sc->sc_ndisplays < 1) return;
4338 /* make sure pushing the hotkeys always has an effect */
4339 dp->rd_bl_on = 1;
4340 level = dp->rd_bl_level;
4341 level = max(0, level - 5);
4342 radeonfb_set_backlight(dp, level);
4343 }
4344