radeonfb.c revision 1.94.2.1 1 /* $NetBSD: radeonfb.c,v 1.94.2.1 2018/06/25 07:26:01 pgoyette 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.94.2.1 2018/06/25 07:26:01 pgoyette 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 /*
2230 * Apple OF hands us R3xx radeons with tiling enabled - explicitly
2231 * disable it here
2232 */
2233 PUT32(sc, RADEON_SURFACE_CNTL, RADEON_SURF_TRANSLATION_DIS);
2234
2235 for (i = 0; i < dp->rd_ncrtcs; i++)
2236 radeonfb_setcrtc(dp, i);
2237
2238 #if 0
2239 /*
2240 * DVO chip voodoo from xf86-video-radeon
2241 * apparently this is needed for some powerbooks with DVI outputs
2242 */
2243
2244 uint8_t data[5][2] = {{0x8, 0x030}, {0x9, 0}, {0xa, 0x90}, {0xc, 0x89}, {0x8, 0x3b}};
2245 int n = 0;
2246 iic_acquire_bus(&sc->sc_i2c[0].ric_controller, 0);
2247 for (i = 0; i < 5; i++)
2248 n += iic_exec(&sc->sc_i2c[0].ric_controller, I2C_OP_WRITE, 0x38, data[i], 2, NULL, 0, 0);
2249 iic_release_bus(&sc->sc_i2c[0].ric_controller, 0);
2250 printf("n = %d\n", n);
2251 #endif
2252
2253 /* activate the display */
2254 radeonfb_blank(dp, 0);
2255 }
2256
2257 void
2258 radeonfb_setcrtc(struct radeonfb_display *dp, int index)
2259 {
2260 int crtc, flags = 0;
2261 struct videomode *mode;
2262 struct radeonfb_softc *sc;
2263 struct radeonfb_crtc *cp;
2264 uint32_t v;
2265 uint32_t gencntl;
2266 uint32_t htotaldisp;
2267 uint32_t hsyncstrt;
2268 uint32_t vtotaldisp;
2269 uint32_t vsyncstrt;
2270 uint32_t fphsyncstrt;
2271 uint32_t fpvsyncstrt;
2272 uint32_t fphtotaldisp;
2273 uint32_t fpvtotaldisp;
2274 uint32_t pitch;
2275
2276 sc = dp->rd_softc;
2277
2278 if ((sc->sc_ports[index].rp_tmds_type == RADEON_TMDS_INT) ||
2279 (sc->sc_ports[index].rp_tmds_type == RADEON_TMDS_EXT)) {
2280 flags |= NO_ODD_FBDIV;
2281 }
2282
2283 cp = &dp->rd_crtcs[index];
2284 crtc = cp->rc_number;
2285 mode = &cp->rc_videomode;
2286
2287 #if 1
2288 pitch = dp->rd_stride / dp->rd_bpp;
2289 #else
2290 pitch = (((sc->sc_maxx * sc->sc_maxbpp) + ((sc->sc_maxbpp * 8) - 1)) /
2291 (sc->sc_maxbpp * 8));
2292 #endif
2293 switch (crtc) {
2294 case 0:
2295 gencntl = RADEON_CRTC_GEN_CNTL;
2296 htotaldisp = RADEON_CRTC_H_TOTAL_DISP;
2297 hsyncstrt = RADEON_CRTC_H_SYNC_STRT_WID;
2298 vtotaldisp = RADEON_CRTC_V_TOTAL_DISP;
2299 vsyncstrt = RADEON_CRTC_V_SYNC_STRT_WID;
2300 /* should probably leave those alone on non-LVDS */
2301 fpvsyncstrt = RADEON_FP_V_SYNC_STRT_WID;
2302 fphsyncstrt = RADEON_FP_H_SYNC_STRT_WID;
2303 fpvtotaldisp = RADEON_FP_CRTC_V_TOTAL_DISP;
2304 fphtotaldisp = RADEON_FP_CRTC_H_TOTAL_DISP;
2305 break;
2306 case 1:
2307 gencntl = RADEON_CRTC2_GEN_CNTL;
2308 htotaldisp = RADEON_CRTC2_H_TOTAL_DISP;
2309 hsyncstrt = RADEON_CRTC2_H_SYNC_STRT_WID;
2310 vtotaldisp = RADEON_CRTC2_V_TOTAL_DISP;
2311 vsyncstrt = RADEON_CRTC2_V_SYNC_STRT_WID;
2312 fpvsyncstrt = RADEON_FP_V2_SYNC_STRT_WID;
2313 fphsyncstrt = RADEON_FP_H2_SYNC_STRT_WID;
2314 /* XXX these registers don't seem to exist */
2315 fpvtotaldisp = 0;//RADEON_FP_CRTC2_V_TOTAL_DISP;
2316 fphtotaldisp = 0;//RADEON_FP_CRTC2_H_TOTAL_DISP;
2317 break;
2318 default:
2319 panic("Bad CRTC!");
2320 break;
2321 }
2322
2323 /*
2324 * CRTC_GEN_CNTL - depth, accelerator mode, etc.
2325 */
2326 /* only bother with 32bpp and 8bpp */
2327 v = dp->rd_format << RADEON_CRTC_PIX_WIDTH_SHIFT;
2328
2329 if (crtc == 1) {
2330 v |= RADEON_CRTC2_CRT2_ON | RADEON_CRTC2_EN;
2331 } else {
2332 v |= RADEON_CRTC_EXT_DISP_EN | RADEON_CRTC_EN;
2333 }
2334
2335 if (mode->flags & VID_DBLSCAN)
2336 v |= RADEON_CRTC2_DBL_SCAN_EN;
2337
2338 if (mode->flags & VID_INTERLACE)
2339 v |= RADEON_CRTC2_INTERLACE_EN;
2340
2341 if (mode->flags & VID_CSYNC) {
2342 v |= RADEON_CRTC2_CSYNC_EN;
2343 if (crtc == 1)
2344 v |= RADEON_CRTC2_VSYNC_TRISTAT;
2345 }
2346
2347 PUT32(sc, gencntl, v);
2348 DPRINTF(("CRTC%s_GEN_CNTL = %08x\n", crtc ? "2" : "", v));
2349
2350 /*
2351 * CRTC_EXT_CNTL - preserve disable flags, set ATI linear and EXT_CNT
2352 */
2353 v = GET32(sc, RADEON_CRTC_EXT_CNTL);
2354 if (crtc == 0) {
2355 v &= (RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS |
2356 RADEON_CRTC_DISPLAY_DIS);
2357 v |= RADEON_XCRT_CNT_EN | RADEON_VGA_ATI_LINEAR;
2358 if (mode->flags & VID_CSYNC)
2359 v |= RADEON_CRTC_VSYNC_TRISTAT;
2360 }
2361 /* unconditional turn on CRT, in case first CRTC is DFP */
2362 v |= RADEON_CRTC_CRT_ON;
2363 PUT32(sc, RADEON_CRTC_EXT_CNTL, v);
2364 PRINTREG(RADEON_CRTC_EXT_CNTL);
2365
2366 /*
2367 * H_TOTAL_DISP
2368 */
2369 v = ((mode->hdisplay / 8) - 1) << 16;
2370 v |= (mode->htotal / 8) - 1;
2371 PUT32(sc, htotaldisp, v);
2372 DPRINTF(("CRTC%s_H_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2373 if (fphtotaldisp) {
2374 PUT32(sc, fphtotaldisp, v);
2375 DPRINTF(("FP_H%s_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2376 }
2377 /*
2378 * H_SYNC_STRT_WID
2379 */
2380 v = (((mode->hsync_end - mode->hsync_start) / 8) << 16);
2381 v |= (mode->hsync_start - 8); /* match xf86-video-radeon */
2382 if (mode->flags & VID_NHSYNC)
2383 v |= RADEON_CRTC_H_SYNC_POL;
2384 PUT32(sc, hsyncstrt, v);
2385 DPRINTF(("CRTC%s_H_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2386 if (fphsyncstrt) {
2387 PUT32(sc, fphsyncstrt, v);
2388 DPRINTF(("FP_H%s_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2389 }
2390
2391 /*
2392 * V_TOTAL_DISP
2393 */
2394 v = ((mode->vdisplay - 1) << 16);
2395 v |= (mode->vtotal - 1);
2396 PUT32(sc, vtotaldisp, v);
2397 DPRINTF(("CRTC%s_V_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2398 if (fpvtotaldisp) {
2399 PUT32(sc, fpvtotaldisp, v);
2400 DPRINTF(("FP_V%s_TOTAL_DISP = %08x\n", crtc ? "2" : "", v));
2401 }
2402
2403 /*
2404 * V_SYNC_STRT_WID
2405 */
2406 v = ((mode->vsync_end - mode->vsync_start) << 16);
2407 v |= (mode->vsync_start - 1);
2408 if (mode->flags & VID_NVSYNC)
2409 v |= RADEON_CRTC_V_SYNC_POL;
2410 PUT32(sc, vsyncstrt, v);
2411 DPRINTF(("CRTC%s_V_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2412 if (fpvsyncstrt) {
2413 PUT32(sc, fpvsyncstrt, v);
2414 DPRINTF(("FP_V%s_SYNC_STRT_WID = %08x\n", crtc ? "2" : "", v));
2415 }
2416
2417 radeonfb_program_vclk(sc, mode->dot_clock, crtc, flags);
2418
2419 switch (crtc) {
2420 case 0:
2421 PUT32(sc, RADEON_CRTC_OFFSET, 0);
2422 PUT32(sc, RADEON_CRTC_OFFSET_CNTL, 0);
2423 PUT32(sc, RADEON_CRTC_PITCH, pitch);
2424 CLR32(sc, RADEON_DISP_MERGE_CNTL, RADEON_DISP_RGB_OFFSET_EN);
2425
2426 CLR32(sc, RADEON_CRTC_EXT_CNTL,
2427 RADEON_CRTC_VSYNC_DIS | RADEON_CRTC_HSYNC_DIS |
2428 RADEON_CRTC_DISPLAY_DIS /* | RADEON_CRTC_DISP_REQ_EN_B */);
2429 CLR32(sc, RADEON_CRTC_GEN_CNTL, RADEON_CRTC_DISP_REQ_EN_B);
2430 PRINTREG(RADEON_CRTC_EXT_CNTL);
2431 PRINTREG(RADEON_CRTC_GEN_CNTL);
2432 PRINTREG(RADEON_CLOCK_CNTL_INDEX);
2433 break;
2434
2435 case 1:
2436 PUT32(sc, RADEON_CRTC2_OFFSET, 0);
2437 PUT32(sc, RADEON_CRTC2_OFFSET_CNTL, 0);
2438 PUT32(sc, RADEON_CRTC2_PITCH, pitch);
2439 CLR32(sc, RADEON_DISP2_MERGE_CNTL, RADEON_DISP2_RGB_OFFSET_EN);
2440 CLR32(sc, RADEON_CRTC2_GEN_CNTL,
2441 RADEON_CRTC2_VSYNC_DIS |
2442 RADEON_CRTC2_HSYNC_DIS |
2443 RADEON_CRTC2_DISP_DIS | RADEON_CRTC2_DISP_REQ_EN_B);
2444 PRINTREG(RADEON_CRTC2_GEN_CNTL);
2445 break;
2446 }
2447 }
2448
2449 int
2450 radeonfb_isblank(struct radeonfb_display *dp)
2451 {
2452 uint32_t reg, mask;
2453
2454 if(!dp->rd_softc->sc_mapped)
2455 return 1;
2456
2457 if (dp->rd_crtcs[0].rc_number) {
2458 reg = RADEON_CRTC2_GEN_CNTL;
2459 mask = RADEON_CRTC2_DISP_DIS;
2460 } else {
2461 reg = RADEON_CRTC_EXT_CNTL;
2462 mask = RADEON_CRTC_DISPLAY_DIS;
2463 }
2464 return ((GET32(dp->rd_softc, reg) & mask) ? 1 : 0);
2465 }
2466
2467 void
2468 radeonfb_blank(struct radeonfb_display *dp, int blank)
2469 {
2470 struct radeonfb_softc *sc = dp->rd_softc;
2471 uint32_t reg, mask;
2472 uint32_t fpreg, fpval;
2473 int i;
2474
2475 if (!sc->sc_mapped)
2476 return;
2477
2478 for (i = 0; i < dp->rd_ncrtcs; i++) {
2479
2480 if (dp->rd_crtcs[i].rc_number) {
2481 reg = RADEON_CRTC2_GEN_CNTL;
2482 mask = RADEON_CRTC2_DISP_DIS;
2483 fpreg = RADEON_FP2_GEN_CNTL;
2484 fpval = RADEON_FP2_ON;
2485 } else {
2486 reg = RADEON_CRTC_EXT_CNTL;
2487 mask = RADEON_CRTC_DISPLAY_DIS;
2488 fpreg = RADEON_FP_GEN_CNTL;
2489 fpval = RADEON_FP_FPON;
2490 }
2491
2492 if (blank) {
2493 SET32(sc, reg, mask);
2494 CLR32(sc, fpreg, fpval);
2495 } else {
2496 CLR32(sc, reg, mask);
2497 SET32(sc, fpreg, fpval);
2498 }
2499 }
2500 PRINTREG(RADEON_FP_GEN_CNTL);
2501 PRINTREG(RADEON_FP2_GEN_CNTL);
2502 }
2503
2504 void
2505 radeonfb_init_screen(void *cookie, struct vcons_screen *scr, int existing,
2506 long *defattr)
2507 {
2508 struct radeonfb_display *dp = cookie;
2509 struct rasops_info *ri = &scr->scr_ri;
2510
2511 /* initialize font subsystem */
2512 wsfont_init();
2513
2514 scr->scr_flags |= VCONS_LOADFONT;
2515
2516 DPRINTF(("init screen called, existing %d\n", existing));
2517
2518 ri->ri_depth = dp->rd_bpp;
2519 ri->ri_width = dp->rd_virtx;
2520 ri->ri_height = dp->rd_virty;
2521 ri->ri_stride = dp->rd_stride;
2522 ri->ri_flg = RI_CENTER;
2523 switch (ri->ri_depth) {
2524 case 8:
2525 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB | RI_PREFER_ALPHA;
2526 break;
2527 case 32:
2528 ri->ri_flg |= RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
2529 /* we run radeons in RGB even on SPARC hardware */
2530 ri->ri_rnum = 8;
2531 ri->ri_gnum = 8;
2532 ri->ri_bnum = 8;
2533 ri->ri_rpos = 16;
2534 ri->ri_gpos = 8;
2535 ri->ri_bpos = 0;
2536 break;
2537 }
2538
2539 ri->ri_bits = (void *)dp->rd_fbptr;
2540
2541 #ifdef VCONS_DRAW_INTR
2542 scr->scr_flags |= VCONS_DONT_READ;
2543 #endif
2544
2545 if (existing) {
2546 ri->ri_flg |= RI_CLEAR;
2547
2548 /* start a modeswitch now */
2549 radeonfb_modeswitch(dp);
2550 }
2551
2552 /*
2553 * XXX: font selection should be based on properties, with some
2554 * normal/reasonable default.
2555 */
2556
2557 /* initialize and look for an initial font */
2558 rasops_init(ri, 0, 0);
2559 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
2560 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE | WSSCREEN_RESIZE;
2561
2562 rasops_reconfig(ri, dp->rd_virty / ri->ri_font->fontheight,
2563 dp->rd_virtx / ri->ri_font->fontwidth);
2564
2565 /* enable acceleration */
2566 dp->rd_putchar = ri->ri_ops.putchar;
2567 ri->ri_ops.copyrows = radeonfb_copyrows;
2568 ri->ri_ops.copycols = radeonfb_copycols;
2569 ri->ri_ops.eraserows = radeonfb_eraserows;
2570 ri->ri_ops.erasecols = radeonfb_erasecols;
2571 /* pick a putchar method based on font and Radeon model */
2572 if (ri->ri_font->stride < ri->ri_font->fontwidth) {
2573 /* got a bitmap font */
2574 #ifndef RADEONFB_ALWAYS_ACCEL_PUTCHAR
2575 if (IS_R300(dp->rd_softc)) {
2576 /*
2577 * radeonfb_putchar() doesn't work right on some R3xx
2578 * so we use software drawing here, the wrapper just
2579 * makes sure the engine is idle before scribbling
2580 * into vram
2581 */
2582 ri->ri_ops.putchar = radeonfb_putchar_wrapper;
2583 } else
2584 #endif
2585 ri->ri_ops.putchar = radeonfb_putchar;
2586 } else {
2587 /* got an alpha font */
2588 switch(ri->ri_depth) {
2589 case 32:
2590 ri->ri_ops.putchar = radeonfb_putchar_aa32;
2591 break;
2592 case 8:
2593 ri->ri_ops.putchar = radeonfb_putchar_aa8;
2594 break;
2595 default:
2596 /* XXX this should never happen */
2597 panic("%s: depth is not 8 or 32 but we got an" \
2598 " alpha font?!", __func__);
2599 }
2600 }
2601 ri->ri_ops.cursor = radeonfb_cursor;
2602 }
2603
2604 void
2605 radeonfb_set_fbloc(struct radeonfb_softc *sc)
2606 {
2607 uint32_t gen, ext, gen2 = 0;
2608 uint32_t agploc, aperbase, apersize, mcfbloc;
2609
2610 gen = GET32(sc, RADEON_CRTC_GEN_CNTL);
2611 /* XXX */
2612 ext = GET32(sc, RADEON_CRTC_EXT_CNTL) & ~RADEON_CRTC_DISPLAY_DIS;
2613 agploc = GET32(sc, RADEON_MC_AGP_LOCATION);
2614 aperbase = GET32(sc, RADEON_CONFIG_APER_0_BASE);
2615 apersize = GET32(sc, RADEON_CONFIG_APER_SIZE);
2616
2617 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen | RADEON_CRTC_DISP_REQ_EN_B);
2618 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext | RADEON_CRTC_DISPLAY_DIS);
2619 #if 0
2620 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen | RADEON_CRTC_DISPLAY_DIS);
2621 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext | RADEON_CRTC_DISP_REQ_EN_B);
2622 #endif
2623
2624 if (HAS_CRTC2(sc)) {
2625 gen2 = GET32(sc, RADEON_CRTC2_GEN_CNTL);
2626 PUT32(sc, RADEON_CRTC2_GEN_CNTL,
2627 gen2 | RADEON_CRTC2_DISP_REQ_EN_B);
2628 }
2629
2630 delay(100000);
2631
2632 mcfbloc = (aperbase >> 16) |
2633 ((aperbase + (apersize - 1)) & 0xffff0000);
2634
2635 sc->sc_aperbase = (mcfbloc & 0xffff) << 16;
2636 sc->sc_memsz = apersize;
2637
2638 if (((agploc & 0xffff) << 16) !=
2639 ((mcfbloc & 0xffff0000U) + 0x10000)) {
2640 agploc = mcfbloc & 0xffff0000U;
2641 agploc |= ((agploc + 0x10000) >> 16);
2642 }
2643
2644 PUT32(sc, RADEON_HOST_PATH_CNTL, 0);
2645
2646 PUT32(sc, RADEON_MC_FB_LOCATION, mcfbloc);
2647 PUT32(sc, RADEON_MC_AGP_LOCATION, agploc);
2648
2649 DPRINTF(("aperbase = %u\n", aperbase));
2650 PRINTREG(RADEON_MC_FB_LOCATION);
2651 PRINTREG(RADEON_MC_AGP_LOCATION);
2652
2653 PUT32(sc, RADEON_DISPLAY_BASE_ADDR, sc->sc_aperbase);
2654
2655 if (HAS_CRTC2(sc))
2656 PUT32(sc, RADEON_DISPLAY2_BASE_ADDR, sc->sc_aperbase);
2657
2658 PUT32(sc, RADEON_OV0_BASE_ADDR, sc->sc_aperbase);
2659
2660 #if 0
2661 /* XXX: what is this AGP garbage? :-) */
2662 PUT32(sc, RADEON_AGP_CNTL, 0x00100000);
2663 #endif
2664
2665 delay(100000);
2666
2667 PUT32(sc, RADEON_CRTC_GEN_CNTL, gen);
2668 PUT32(sc, RADEON_CRTC_EXT_CNTL, ext);
2669
2670 if (HAS_CRTC2(sc))
2671 PUT32(sc, RADEON_CRTC2_GEN_CNTL, gen2);
2672 }
2673
2674 void
2675 radeonfb_init_misc(struct radeonfb_softc *sc)
2676 {
2677 PUT32(sc, RADEON_BUS_CNTL,
2678 RADEON_BUS_MASTER_DIS |
2679 RADEON_BUS_PREFETCH_MODE_ACT |
2680 RADEON_BUS_PCI_READ_RETRY_EN |
2681 RADEON_BUS_PCI_WRT_RETRY_EN |
2682 (3 << RADEON_BUS_RETRY_WS_SHIFT) |
2683 RADEON_BUS_MSTR_RD_MULT |
2684 RADEON_BUS_MSTR_RD_LINE |
2685 RADEON_BUS_RD_DISCARD_EN |
2686 RADEON_BUS_MSTR_DISCONNECT_EN |
2687 RADEON_BUS_READ_BURST);
2688
2689 PUT32(sc, RADEON_BUS_CNTL1, 0xf0);
2690 /* PUT32(sc, RADEON_SEPROM_CNTL1, 0x09ff0000); */
2691 PUT32(sc, RADEON_FCP_CNTL, RADEON_FCP0_SRC_GND);
2692 PUT32(sc, RADEON_RBBM_CNTL,
2693 (3 << RADEON_RB_SETTLE_SHIFT) |
2694 (4 << RADEON_ABORTCLKS_HI_SHIFT) |
2695 (4 << RADEON_ABORTCLKS_CP_SHIFT) |
2696 (4 << RADEON_ABORTCLKS_CFIFO_SHIFT));
2697
2698 /* XXX: figure out what these mean! */
2699 PUT32(sc, RADEON_AGP_CNTL, 0x00100000);
2700 PUT32(sc, RADEON_HOST_PATH_CNTL, 0);
2701 #if 0
2702 PUT32(sc, RADEON_DISP_MISC_CNTL, 0x5bb00400);
2703 #endif
2704
2705 PUT32(sc, RADEON_GEN_INT_CNTL, 0);
2706 PUT32(sc, RADEON_GEN_INT_STATUS, GET32(sc, RADEON_GEN_INT_STATUS));
2707 }
2708
2709 static void
2710 radeonfb_putpal(struct radeonfb_display *dp, int idx, int r, int g, int b)
2711 {
2712 struct radeonfb_softc *sc = dp->rd_softc;
2713 int crtc, cc;
2714 uint32_t vclk;
2715
2716 vclk = GETPLL(sc, RADEON_VCLK_ECP_CNTL);
2717 PUTPLL(sc, RADEON_VCLK_ECP_CNTL, vclk & ~RADEON_PIXCLK_DAC_ALWAYS_ONb);
2718
2719 /* initialize the palette for every CRTC used by this display */
2720 for (cc = 0; cc < dp->rd_ncrtcs; cc++) {
2721 crtc = dp->rd_crtcs[cc].rc_number;
2722
2723 if (crtc)
2724 SET32(sc, RADEON_DAC_CNTL2, RADEON_DAC2_PALETTE_ACC_CTL);
2725 else
2726 CLR32(sc, RADEON_DAC_CNTL2, RADEON_DAC2_PALETTE_ACC_CTL);
2727
2728 PUT32(sc, RADEON_PALETTE_INDEX, idx);
2729 PUT32(sc, RADEON_PALETTE_30_DATA,
2730 (r << 22) | (g << 12) | (b << 2));
2731 }
2732
2733 PUTPLL(sc, RADEON_VCLK_ECP_CNTL, vclk);
2734 }
2735
2736 /*
2737 * This loads a linear color map for true color.
2738 */
2739 void
2740 radeonfb_init_palette(struct radeonfb_display *dp)
2741 {
2742 int i;
2743
2744 #define DAC_WIDTH ((1 << 10) - 1)
2745 #define CLUT_WIDTH ((1 << 8) - 1)
2746 #define CLUT_COLOR(i) ((i * DAC_WIDTH * 2 / CLUT_WIDTH + 1) / 2)
2747
2748 if (dp->rd_bpp == 8) {
2749
2750 /* R3G3B2 palette */
2751 uint32_t tmp, r, g, b;
2752
2753 for (i = 0; i <= CLUT_WIDTH; ++i) {
2754 tmp = i & 0xe0;
2755
2756 /*
2757 * replicate bits so 0xe0 maps to a red value of 0xff
2758 * in order to make white look actually white
2759 */
2760 tmp |= (tmp >> 3) | (tmp >> 6);
2761 r = tmp;
2762
2763 tmp = (i & 0x1c) << 3;
2764 tmp |= (tmp >> 3) | (tmp >> 6);
2765 g = tmp;
2766
2767 tmp = (i & 0x03) << 6;
2768 tmp |= tmp >> 2;
2769 tmp |= tmp >> 4;
2770 b = tmp;
2771
2772 radeonfb_putpal(dp, i, r, g, b);
2773 }
2774 } else {
2775 /* linear ramp */
2776 for (i = 0; i <= CLUT_WIDTH; ++i) {
2777 radeonfb_putpal(dp, i, i, i, i);
2778 }
2779 }
2780 }
2781
2782 static int
2783 radeonfb_putcmap(struct radeonfb_display *dp, struct wsdisplay_cmap *cm)
2784 {
2785 u_char *r, *g, *b;
2786 u_int index = cm->index;
2787 u_int count = cm->count;
2788 int i, error;
2789 u_char rbuf[256], gbuf[256], bbuf[256];
2790
2791 #ifdef GENFB_DEBUG
2792 aprint_debug("putcmap: %d %d\n",index, count);
2793 #endif
2794 if (index >= 256 || count > 256 - index)
2795 return EINVAL;
2796 error = copyin(cm->red, &rbuf[index], count);
2797 if (error)
2798 return error;
2799 error = copyin(cm->green, &gbuf[index], count);
2800 if (error)
2801 return error;
2802 error = copyin(cm->blue, &bbuf[index], count);
2803 if (error)
2804 return error;
2805
2806 memcpy(&dp->rd_cmap_red[index], &rbuf[index], count);
2807 memcpy(&dp->rd_cmap_green[index], &gbuf[index], count);
2808 memcpy(&dp->rd_cmap_blue[index], &bbuf[index], count);
2809
2810 r = &dp->rd_cmap_red[index];
2811 g = &dp->rd_cmap_green[index];
2812 b = &dp->rd_cmap_blue[index];
2813
2814 for (i = 0; i < count; i++) {
2815 radeonfb_putpal(dp, index, *r, *g, *b);
2816 index++;
2817 r++, g++, b++;
2818 }
2819 return 0;
2820 }
2821
2822 static int
2823 radeonfb_getcmap(struct radeonfb_display *dp, struct wsdisplay_cmap *cm)
2824 {
2825 u_int index = cm->index;
2826 u_int count = cm->count;
2827 int error;
2828
2829 if (index >= 256 || count > 256 - index)
2830 return EINVAL;
2831
2832 error = copyout(&dp->rd_cmap_red[index], cm->red, count);
2833 if (error)
2834 return error;
2835 error = copyout(&dp->rd_cmap_green[index], cm->green, count);
2836 if (error)
2837 return error;
2838 error = copyout(&dp->rd_cmap_blue[index], cm->blue, count);
2839 if (error)
2840 return error;
2841
2842 return 0;
2843 }
2844
2845 /*
2846 * Bugs in some R300 hardware requires this when accessing CLOCK_CNTL_INDEX.
2847 */
2848 void
2849 radeonfb_r300cg_workaround(struct radeonfb_softc *sc)
2850 {
2851 uint32_t tmp, save;
2852
2853 save = GET32(sc, RADEON_CLOCK_CNTL_INDEX);
2854 tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
2855 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, tmp);
2856 tmp = GET32(sc, RADEON_CLOCK_CNTL_DATA);
2857 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, save);
2858 }
2859
2860 /*
2861 * Acceleration entry points.
2862 */
2863
2864 /* this one draws characters using bitmap fonts */
2865 static void
2866 radeonfb_putchar(void *cookie, int row, int col, u_int c, long attr)
2867 {
2868 struct rasops_info *ri = cookie;
2869 struct vcons_screen *scr = ri->ri_hw;
2870 struct radeonfb_display *dp = scr->scr_cookie;
2871 struct radeonfb_softc *sc = dp->rd_softc;
2872 struct wsdisplay_font *font = PICK_FONT(ri, c);
2873 uint32_t w, h;
2874 int xd, yd, offset, i;
2875 uint32_t bg, fg, gmc;
2876 uint32_t reg;
2877 uint8_t *data8;
2878 uint16_t *data16;
2879 void *data;
2880
2881 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
2882 return;
2883
2884 if (!CHAR_IN_FONT(c, font))
2885 return;
2886
2887 w = font->fontwidth;
2888 h = font->fontheight;
2889
2890 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
2891 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
2892
2893 xd = ri->ri_xorigin + col * w;
2894 yd = ri->ri_yorigin + row * h;
2895
2896 if (c == 0x20) {
2897 radeonfb_rectfill(dp, xd, yd, w, h, bg);
2898 return;
2899 }
2900 data = WSFONT_GLYPH(c, font);
2901
2902 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
2903
2904 radeonfb_wait_fifo(sc, 9);
2905
2906 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
2907 RADEON_GMC_BRUSH_NONE |
2908 RADEON_GMC_SRC_DATATYPE_MONO_FG_BG |
2909 RADEON_GMC_DST_CLIPPING |
2910 RADEON_ROP3_S |
2911 RADEON_DP_SRC_SOURCE_HOST_DATA |
2912 RADEON_GMC_CLR_CMP_CNTL_DIS |
2913 RADEON_GMC_WR_MSK_DIS |
2914 gmc);
2915
2916 PUT32(sc, RADEON_SC_LEFT, xd);
2917 PUT32(sc, RADEON_SC_RIGHT, xd + w);
2918 PUT32(sc, RADEON_DP_SRC_FRGD_CLR, fg);
2919 PUT32(sc, RADEON_DP_SRC_BKGD_CLR, bg);
2920 PUT32(sc, RADEON_DP_CNTL,
2921 RADEON_DST_X_LEFT_TO_RIGHT |
2922 RADEON_DST_Y_TOP_TO_BOTTOM);
2923
2924 PUT32(sc, RADEON_SRC_X_Y, 0);
2925 offset = 32 - (font->stride << 3);
2926 PUT32(sc, RADEON_DST_X_Y, ((xd - offset) << 16) | yd);
2927 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (32 << 16) | h);
2928
2929 radeonfb_wait_fifo(sc, h);
2930 switch (font->stride) {
2931 case 1: {
2932 data8 = data;
2933 for (i = 0; i < h; i++) {
2934 reg = *data8;
2935 #if BYTE_ORDER == LITTLE_ENDIAN
2936 reg = reg << 24;
2937 #endif
2938 bus_space_write_stream_4(sc->sc_regt,
2939 sc->sc_regh, RADEON_HOST_DATA0, reg);
2940 data8++;
2941 }
2942 break;
2943 }
2944 case 2: {
2945 data16 = data;
2946 for (i = 0; i < h; i++) {
2947 reg = *data16;
2948 #if BYTE_ORDER == LITTLE_ENDIAN
2949 reg = reg << 16;
2950 #endif
2951 bus_space_write_stream_4(sc->sc_regt,
2952 sc->sc_regh, RADEON_HOST_DATA0, reg);
2953 data16++;
2954 }
2955 break;
2956 }
2957 }
2958 if (attr & 1)
2959 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
2960 }
2961
2962 /* ... while this one is for anti-aliased ones */
2963 static void
2964 radeonfb_putchar_aa32(void *cookie, int row, int col, u_int c, long attr)
2965 {
2966 struct rasops_info *ri = cookie;
2967 struct vcons_screen *scr = ri->ri_hw;
2968 struct radeonfb_display *dp = scr->scr_cookie;
2969 struct radeonfb_softc *sc = dp->rd_softc;
2970 struct wsdisplay_font *font = PICK_FONT(ri, c);
2971 uint32_t bg, fg, gmc;
2972 uint8_t *data;
2973 int w, h, xd, yd;
2974 int i, r, g, b, aval;
2975 int rf, gf, bf, rb, gb, bb;
2976 uint32_t pixel;
2977 int rv;
2978
2979 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
2980 return;
2981
2982 if (!CHAR_IN_FONT(c, font))
2983 return;
2984
2985 w = font->fontwidth;
2986 h = font->fontheight;
2987
2988 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
2989 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
2990
2991 xd = ri->ri_xorigin + col * w;
2992 yd = ri->ri_yorigin + row * h;
2993
2994 if (c == 0x20) {
2995 radeonfb_rectfill(dp, xd, yd, w, h, bg);
2996 if (attr & 1)
2997 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
2998 return;
2999 }
3000 rv = glyphcache_try(&dp->rd_gc, c, xd, yd, attr);
3001 if (rv == GC_OK)
3002 return;
3003
3004 data = WSFONT_GLYPH(c, font);
3005
3006 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3007
3008 radeonfb_wait_fifo(sc, 5);
3009
3010 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3011 RADEON_GMC_BRUSH_NONE |
3012 RADEON_GMC_SRC_DATATYPE_COLOR |
3013 RADEON_ROP3_S |
3014 RADEON_DP_SRC_SOURCE_HOST_DATA |
3015 RADEON_GMC_CLR_CMP_CNTL_DIS |
3016 RADEON_GMC_WR_MSK_DIS |
3017 gmc);
3018
3019 PUT32(sc, RADEON_DP_CNTL,
3020 RADEON_DST_X_LEFT_TO_RIGHT |
3021 RADEON_DST_Y_TOP_TO_BOTTOM);
3022
3023 PUT32(sc, RADEON_SRC_X_Y, 0);
3024 PUT32(sc, RADEON_DST_X_Y, (xd << 16) | yd);
3025 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (w << 16) | h);
3026
3027 rf = (fg >> 16) & 0xff;
3028 rb = (bg >> 16) & 0xff;
3029 gf = (fg >> 8) & 0xff;
3030 gb = (bg >> 8) & 0xff;
3031 bf = fg & 0xff;
3032 bb = bg & 0xff;
3033
3034 /*
3035 * I doubt we can upload data faster than even the slowest Radeon
3036 * could process them, especially when doing the alpha blending stuff
3037 * along the way, so just make sure there's some room in the FIFO and
3038 * then hammer away
3039 * As it turns out we can, so make periodic stops to let the FIFO
3040 * drain.
3041 */
3042 radeonfb_wait_fifo(sc, 20);
3043 for (i = 0; i < ri->ri_fontscale; i++) {
3044 aval = *data;
3045 data++;
3046 if (aval == 0) {
3047 pixel = bg;
3048 } else if (aval == 255) {
3049 pixel = fg;
3050 } else {
3051 r = aval * rf + (255 - aval) * rb;
3052 g = aval * gf + (255 - aval) * gb;
3053 b = aval * bf + (255 - aval) * bb;
3054 pixel = (r & 0xff00) << 8 |
3055 (g & 0xff00) |
3056 (b & 0xff00) >> 8;
3057 }
3058 if (i & 16)
3059 radeonfb_wait_fifo(sc, 20);
3060 PUT32(sc, RADEON_HOST_DATA0, pixel);
3061 }
3062 if (rv == GC_ADD) {
3063 glyphcache_add(&dp->rd_gc, c, xd, yd);
3064 } else if (attr & 1)
3065 radeonfb_rectfill(dp, xd, yd + h - 2, w, 1, fg);
3066 }
3067
3068 static void
3069 radeonfb_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
3070 {
3071 struct rasops_info *ri = cookie;
3072 struct vcons_screen *scr = ri->ri_hw;
3073 struct radeonfb_display *dp = scr->scr_cookie;
3074 struct radeonfb_softc *sc = dp->rd_softc;
3075 struct wsdisplay_font *font = PICK_FONT(ri, c);
3076 uint32_t bg, fg, latch = 0, bg8, fg8, pixel, gmc;
3077 int i, x, y, wi, he, r, g, b, aval;
3078 int r1, g1, b1, r0, g0, b0, fgo, bgo;
3079 uint8_t *data8;
3080 int rv, cnt;
3081
3082 if (dp->rd_wsmode != WSDISPLAYIO_MODE_EMUL)
3083 return;
3084
3085 if (!CHAR_IN_FONT(c, font))
3086 return;
3087
3088 wi = font->fontwidth;
3089 he = font->fontheight;
3090
3091 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
3092 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
3093
3094 x = ri->ri_xorigin + col * wi;
3095 y = ri->ri_yorigin + row * he;
3096
3097 if (c == 0x20) {
3098 radeonfb_rectfill(dp, x, y, wi, he, bg);
3099 if (attr & 1)
3100 radeonfb_rectfill(dp, x, y + he - 2, wi, 1, fg);
3101 return;
3102 }
3103 rv = glyphcache_try(&dp->rd_gc, c, x, y, attr);
3104 if (rv == GC_OK)
3105 return;
3106
3107 data8 = WSFONT_GLYPH(c, font);
3108
3109 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3110
3111 radeonfb_wait_fifo(sc, 5);
3112
3113 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3114 RADEON_GMC_BRUSH_NONE |
3115 RADEON_GMC_SRC_DATATYPE_COLOR |
3116 RADEON_ROP3_S |
3117 RADEON_DP_SRC_SOURCE_HOST_DATA |
3118 RADEON_GMC_CLR_CMP_CNTL_DIS |
3119 RADEON_GMC_WR_MSK_DIS |
3120 gmc);
3121
3122 PUT32(sc, RADEON_DP_CNTL,
3123 RADEON_DST_X_LEFT_TO_RIGHT |
3124 RADEON_DST_Y_TOP_TO_BOTTOM);
3125
3126 PUT32(sc, RADEON_SRC_X_Y, 0);
3127 PUT32(sc, RADEON_DST_X_Y, (x << 16) | y);
3128 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (wi << 16) | he);
3129
3130 /*
3131 * we need the RGB colours here, so get offsets into rasops_cmap
3132 */
3133 fgo = ((attr >> 24) & 0xf) * 3;
3134 bgo = ((attr >> 16) & 0xf) * 3;
3135
3136 r0 = rasops_cmap[bgo];
3137 r1 = rasops_cmap[fgo];
3138 g0 = rasops_cmap[bgo + 1];
3139 g1 = rasops_cmap[fgo + 1];
3140 b0 = rasops_cmap[bgo + 2];
3141 b1 = rasops_cmap[fgo + 2];
3142 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
3143 bg8 = R3G3B2(r0, g0, b0);
3144 fg8 = R3G3B2(r1, g1, b1);
3145
3146 radeonfb_wait_fifo(sc, 20);
3147 cnt = 0;
3148 for (i = 0; i < ri->ri_fontscale; i++) {
3149 aval = *data8;
3150 if (aval == 0) {
3151 pixel = bg8;
3152 } else if (aval == 255) {
3153 pixel = fg8;
3154 } else {
3155 r = aval * r1 + (255 - aval) * r0;
3156 g = aval * g1 + (255 - aval) * g0;
3157 b = aval * b1 + (255 - aval) * b0;
3158 pixel = ((r & 0xe000) >> 8) |
3159 ((g & 0xe000) >> 11) |
3160 ((b & 0xc000) >> 14);
3161 }
3162 latch |= pixel << (8 * (i & 3));
3163 /* write in 32bit chunks */
3164 if ((i & 3) == 3) {
3165 PUT32(sc, RADEON_HOST_DATA0, latch);
3166 /*
3167 * not strictly necessary, old data should be shifted
3168 * out
3169 */
3170 latch = 0;
3171 cnt++;
3172 if (cnt > 16) {
3173 cnt = 0;
3174 radeonfb_wait_fifo(sc, 20);
3175 }
3176 }
3177 data8++;
3178 }
3179 /* if we have pixels left in latch write them out */
3180 if ((i & 3) != 0) {
3181 /*
3182 * radeon is weird - apparently leftover pixels are written
3183 * from the middle, not from the left as everything else
3184 */
3185 PUT32(sc, RADEON_HOST_DATA0, latch);
3186 }
3187
3188 if (rv == GC_ADD) {
3189 glyphcache_add(&dp->rd_gc, c, x, y);
3190 } else
3191 if (attr & 1)
3192 radeonfb_rectfill(dp, x, y + he - 2, wi, 1, fg);
3193 }
3194
3195 /*
3196 * wrapper for software character drawing
3197 * just sync the engine and call rasops*_putchar()
3198 */
3199
3200 #ifndef RADEONFB_ALWAYS_ACCEL_PUTCHAR
3201 static void
3202 radeonfb_putchar_wrapper(void *cookie, int row, int col, u_int c, long attr)
3203 {
3204 struct rasops_info *ri = cookie;
3205 struct vcons_screen *scr = ri->ri_hw;
3206 struct radeonfb_display *dp = scr->scr_cookie;
3207
3208 radeonfb_engine_idle(dp->rd_softc);
3209 dp->rd_putchar(ri, row, col, c, attr);
3210 }
3211 #endif
3212
3213 static void
3214 radeonfb_eraserows(void *cookie, int row, int nrows, long fillattr)
3215 {
3216 struct rasops_info *ri = cookie;
3217 struct vcons_screen *scr = ri->ri_hw;
3218 struct radeonfb_display *dp = scr->scr_cookie;
3219 uint32_t x, y, w, h, fg, bg, ul;
3220
3221 /* XXX: check for full emulation mode? */
3222 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3223 x = ri->ri_xorigin;
3224 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3225 w = ri->ri_emuwidth;
3226 h = ri->ri_font->fontheight * nrows;
3227
3228 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
3229 radeonfb_rectfill(dp, x, y, w, h, ri->ri_devcmap[bg & 0xf]);
3230 }
3231 }
3232
3233 static void
3234 radeonfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
3235 {
3236 struct rasops_info *ri = cookie;
3237 struct vcons_screen *scr = ri->ri_hw;
3238 struct radeonfb_display *dp = scr->scr_cookie;
3239 uint32_t x, ys, yd, w, h;
3240
3241 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3242 x = ri->ri_xorigin;
3243 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
3244 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
3245 w = ri->ri_emuwidth;
3246 h = ri->ri_font->fontheight * nrows;
3247 radeonfb_bitblt(dp, x, ys, x, yd, w, h,
3248 RADEON_ROP3_S);
3249 }
3250 }
3251
3252 static void
3253 radeonfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
3254 {
3255 struct rasops_info *ri = cookie;
3256 struct vcons_screen *scr = ri->ri_hw;
3257 struct radeonfb_display *dp = scr->scr_cookie;
3258 uint32_t xs, xd, y, w, h;
3259
3260 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3261 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
3262 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
3263 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3264 w = ri->ri_font->fontwidth * ncols;
3265 h = ri->ri_font->fontheight;
3266 radeonfb_bitblt(dp, xs, y, xd, y, w, h,
3267 RADEON_ROP3_S);
3268 }
3269 }
3270
3271 static void
3272 radeonfb_erasecols(void *cookie, int row, int startcol, int ncols,
3273 long fillattr)
3274 {
3275 struct rasops_info *ri = cookie;
3276 struct vcons_screen *scr = ri->ri_hw;
3277 struct radeonfb_display *dp = scr->scr_cookie;
3278 uint32_t x, y, w, h, fg, bg, ul;
3279
3280 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3281 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
3282 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
3283 w = ri->ri_font->fontwidth * ncols;
3284 h = ri->ri_font->fontheight;
3285
3286 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
3287 radeonfb_rectfill(dp, x, y, w, h, ri->ri_devcmap[bg & 0xf]);
3288 }
3289 }
3290
3291 static void
3292 radeonfb_cursor(void *cookie, int on, int row, int col)
3293 {
3294 struct rasops_info *ri = cookie;
3295 struct vcons_screen *scr = ri->ri_hw;
3296 struct radeonfb_display *dp = scr->scr_cookie;
3297 int x, y, wi, he;
3298
3299 wi = ri->ri_font->fontwidth;
3300 he = ri->ri_font->fontheight;
3301
3302 if (dp->rd_wsmode == WSDISPLAYIO_MODE_EMUL) {
3303 x = ri->ri_ccol * wi + ri->ri_xorigin;
3304 y = ri->ri_crow * he + ri->ri_yorigin;
3305 /* first turn off the old cursor */
3306 if (ri->ri_flg & RI_CURSOR) {
3307 radeonfb_bitblt(dp, x, y, x, y, wi, he,
3308 RADEON_ROP3_Dn);
3309 ri->ri_flg &= ~RI_CURSOR;
3310 }
3311 ri->ri_crow = row;
3312 ri->ri_ccol = col;
3313 /* then (possibly) turn on the new one */
3314 if (on) {
3315 x = ri->ri_ccol * wi + ri->ri_xorigin;
3316 y = ri->ri_crow * he + ri->ri_yorigin;
3317 radeonfb_bitblt(dp, x, y, x, y, wi, he,
3318 RADEON_ROP3_Dn);
3319 ri->ri_flg |= RI_CURSOR;
3320 }
3321 } else {
3322 scr->scr_ri.ri_crow = row;
3323 scr->scr_ri.ri_ccol = col;
3324 scr->scr_ri.ri_flg &= ~RI_CURSOR;
3325 }
3326 }
3327
3328 /*
3329 * Underlying acceleration support.
3330 */
3331
3332 static void
3333 radeonfb_rectfill(struct radeonfb_display *dp, int dstx, int dsty,
3334 int width, int height, uint32_t color)
3335 {
3336 struct radeonfb_softc *sc = dp->rd_softc;
3337 uint32_t gmc;
3338
3339 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3340
3341 radeonfb_wait_fifo(sc, 6);
3342
3343 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3344 RADEON_GMC_BRUSH_SOLID_COLOR |
3345 RADEON_GMC_SRC_DATATYPE_COLOR |
3346 RADEON_GMC_CLR_CMP_CNTL_DIS |
3347 RADEON_ROP3_P | gmc);
3348
3349 PUT32(sc, RADEON_DP_BRUSH_FRGD_CLR, color);
3350 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3351 PUT32(sc, RADEON_DP_CNTL,
3352 RADEON_DST_X_LEFT_TO_RIGHT |
3353 RADEON_DST_Y_TOP_TO_BOTTOM);
3354 PUT32(sc, RADEON_DST_Y_X, (dsty << 16) | dstx);
3355 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (width << 16) | (height));
3356 }
3357
3358 static void
3359 radeonfb_rectfill_a(void *cookie, int dstx, int dsty,
3360 int width, int height, long attr)
3361 {
3362 struct radeonfb_display *dp = cookie;
3363
3364 radeonfb_rectfill(dp, dstx, dsty, width, height,
3365 dp->rd_vscreen.scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
3366 }
3367
3368 static void
3369 radeonfb_bitblt(void *cookie, int srcx, int srcy,
3370 int dstx, int dsty, int width, int height, int rop)
3371 {
3372 struct radeonfb_display *dp = cookie;
3373 struct radeonfb_softc *sc = dp->rd_softc;
3374 uint32_t gmc;
3375 uint32_t dir;
3376
3377 if (dsty < srcy) {
3378 dir = RADEON_DST_Y_TOP_TO_BOTTOM;
3379 } else {
3380 srcy += height - 1;
3381 dsty += height - 1;
3382 dir = 0;
3383 }
3384 if (dstx < srcx) {
3385 dir |= RADEON_DST_X_LEFT_TO_RIGHT;
3386 } else {
3387 srcx += width - 1;
3388 dstx += width - 1;
3389 }
3390
3391 gmc = dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT;
3392
3393 radeonfb_wait_fifo(sc, 6);
3394
3395 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3396 RADEON_GMC_BRUSH_SOLID_COLOR |
3397 RADEON_GMC_SRC_DATATYPE_COLOR |
3398 RADEON_GMC_CLR_CMP_CNTL_DIS |
3399 RADEON_DP_SRC_SOURCE_MEMORY |
3400 rop | gmc);
3401
3402 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3403 PUT32(sc, RADEON_DP_CNTL, dir);
3404 PUT32(sc, RADEON_SRC_Y_X, (srcy << 16) | srcx);
3405 PUT32(sc, RADEON_DST_Y_X, (dsty << 16) | dstx);
3406 PUT32(sc, RADEON_DST_WIDTH_HEIGHT, (width << 16) | (height));
3407 }
3408
3409 static void
3410 radeonfb_engine_idle(struct radeonfb_softc *sc)
3411 {
3412
3413 radeonfb_wait_fifo(sc, 64);
3414 while ((GET32(sc, RADEON_RBBM_STATUS) &
3415 RADEON_RBBM_ACTIVE) != 0);
3416 radeonfb_engine_flush(sc);
3417 }
3418
3419 static inline void
3420 radeonfb_wait_fifo(struct radeonfb_softc *sc, int n)
3421 {
3422 int i;
3423
3424 for (i = RADEON_TIMEOUT; i; i--) {
3425 if ((GET32(sc, RADEON_RBBM_STATUS) &
3426 RADEON_RBBM_FIFOCNT_MASK) >= n)
3427 return;
3428 }
3429 #ifdef DIAGNOSTIC
3430 if (!i)
3431 printf("%s: timed out waiting for fifo (%x)\n",
3432 XNAME(sc), GET32(sc, RADEON_RBBM_STATUS));
3433 #endif
3434 }
3435
3436 static void
3437 radeonfb_engine_flush(struct radeonfb_softc *sc)
3438 {
3439 int i = 0;
3440
3441 if (IS_R300(sc)) {
3442 SET32(sc, R300_DSTCACHE_CTLSTAT, R300_RB2D_DC_FLUSH_ALL);
3443 while (GET32(sc, R300_DSTCACHE_CTLSTAT) & R300_RB2D_DC_BUSY) {
3444 i++;
3445 }
3446 } else {
3447 SET32(sc, RADEON_RB2D_DSTCACHE_CTLSTAT,
3448 RADEON_RB2D_DC_FLUSH_ALL);
3449 while (GET32(sc, RADEON_RB2D_DSTCACHE_CTLSTAT) &
3450 RADEON_RB2D_DC_BUSY) {
3451 i++;
3452 }
3453 }
3454 #ifdef DIAGNOSTIC
3455 if (i > RADEON_TIMEOUT)
3456 printf("%s: engine flush timed out!\n", XNAME(sc));
3457 #endif
3458 }
3459
3460 static inline void
3461 radeonfb_unclip(struct radeonfb_softc *sc)
3462 {
3463
3464 radeonfb_wait_fifo(sc, 2);
3465 PUT32(sc, RADEON_SC_TOP_LEFT, 0);
3466 PUT32(sc, RADEON_SC_BOTTOM_RIGHT, 0x1fff1fff);
3467 }
3468
3469 static void
3470 radeonfb_engine_init(struct radeonfb_display *dp)
3471 {
3472 struct radeonfb_softc *sc = dp->rd_softc;
3473 uint32_t pitch;
3474
3475 /* no 3D */
3476 PUT32(sc, RADEON_RB3D_CNTL, 0);
3477
3478 radeonfb_engine_reset(sc);
3479 pitch = ((dp->rd_virtx * (dp->rd_bpp / 8) + 0x3f)) >> 6;
3480
3481 radeonfb_wait_fifo(sc, 1);
3482 if (!IS_R300(sc))
3483 PUT32(sc, RADEON_RB2D_DSTCACHE_MODE, 0);
3484
3485 radeonfb_wait_fifo(sc, 3);
3486 PUT32(sc, RADEON_DEFAULT_PITCH_OFFSET,
3487 (pitch << 22) | (sc->sc_aperbase >> 10));
3488
3489
3490 PUT32(sc, RADEON_DST_PITCH_OFFSET,
3491 (pitch << 22) | (sc->sc_aperbase >> 10));
3492 PUT32(sc, RADEON_SRC_PITCH_OFFSET,
3493 (pitch << 22) | (sc->sc_aperbase >> 10));
3494
3495 (void)GET32(sc, RADEON_DP_DATATYPE);
3496
3497 /* default scissors -- no clipping */
3498 radeonfb_wait_fifo(sc, 1);
3499 PUT32(sc, RADEON_DEFAULT_SC_BOTTOM_RIGHT,
3500 RADEON_DEFAULT_SC_RIGHT_MAX | RADEON_DEFAULT_SC_BOTTOM_MAX);
3501
3502 radeonfb_wait_fifo(sc, 1);
3503 PUT32(sc, RADEON_DP_GUI_MASTER_CNTL,
3504 (dp->rd_format << RADEON_GMC_DST_DATATYPE_SHIFT) |
3505 RADEON_GMC_CLR_CMP_CNTL_DIS |
3506 RADEON_GMC_BRUSH_SOLID_COLOR |
3507 RADEON_GMC_SRC_DATATYPE_COLOR);
3508
3509 radeonfb_wait_fifo(sc, 10);
3510 PUT32(sc, RADEON_DST_LINE_START, 0);
3511 PUT32(sc, RADEON_DST_LINE_END, 0);
3512 PUT32(sc, RADEON_DP_BRUSH_FRGD_CLR, 0xffffffff);
3513 PUT32(sc, RADEON_DP_BRUSH_BKGD_CLR, 0);
3514 PUT32(sc, RADEON_DP_SRC_FRGD_CLR, 0xffffffff);
3515 PUT32(sc, RADEON_DP_SRC_BKGD_CLR, 0);
3516 PUT32(sc, RADEON_DP_WRITE_MASK, 0xffffffff);
3517 PUT32(sc, RADEON_SC_TOP_LEFT, 0);
3518 PUT32(sc, RADEON_SC_BOTTOM_RIGHT, 0x1fff1fff);
3519 PUT32(sc, RADEON_AUX_SC_CNTL, 0);
3520 radeonfb_engine_idle(sc);
3521 }
3522
3523 static void
3524 radeonfb_engine_reset(struct radeonfb_softc *sc)
3525 {
3526 uint32_t hpc, rbbm, mclkcntl, clkindex;
3527
3528 radeonfb_engine_flush(sc);
3529
3530 clkindex = GET32(sc, RADEON_CLOCK_CNTL_INDEX);
3531 if (HAS_R300CG(sc))
3532 radeonfb_r300cg_workaround(sc);
3533 mclkcntl = GETPLL(sc, RADEON_MCLK_CNTL);
3534
3535 /*
3536 * According to comments in XFree code, resetting the HDP via
3537 * the RBBM_SOFT_RESET can cause bad behavior on some systems.
3538 * So we use HOST_PATH_CNTL instead.
3539 */
3540
3541 hpc = GET32(sc, RADEON_HOST_PATH_CNTL);
3542 rbbm = GET32(sc, RADEON_RBBM_SOFT_RESET);
3543 if (IS_R300(sc)) {
3544 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm |
3545 RADEON_SOFT_RESET_CP |
3546 RADEON_SOFT_RESET_HI |
3547 RADEON_SOFT_RESET_E2);
3548 GET32(sc, RADEON_RBBM_SOFT_RESET);
3549 PUT32(sc, RADEON_RBBM_SOFT_RESET, 0);
3550 /*
3551 * XXX: this bit is not defined in any ATI docs I have,
3552 * nor in the XFree code, but XFree does it. Why?
3553 */
3554 SET32(sc, RADEON_RB2D_DSTCACHE_MODE, (1<<17));
3555 } else {
3556 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm |
3557 RADEON_SOFT_RESET_CP |
3558 RADEON_SOFT_RESET_SE |
3559 RADEON_SOFT_RESET_RE |
3560 RADEON_SOFT_RESET_PP |
3561 RADEON_SOFT_RESET_E2 |
3562 RADEON_SOFT_RESET_RB);
3563 GET32(sc, RADEON_RBBM_SOFT_RESET);
3564 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm &
3565 ~(RADEON_SOFT_RESET_CP |
3566 RADEON_SOFT_RESET_SE |
3567 RADEON_SOFT_RESET_RE |
3568 RADEON_SOFT_RESET_PP |
3569 RADEON_SOFT_RESET_E2 |
3570 RADEON_SOFT_RESET_RB));
3571 GET32(sc, RADEON_RBBM_SOFT_RESET);
3572 }
3573
3574 PUT32(sc, RADEON_HOST_PATH_CNTL, hpc | RADEON_HDP_SOFT_RESET);
3575 GET32(sc, RADEON_HOST_PATH_CNTL);
3576 PUT32(sc, RADEON_HOST_PATH_CNTL, hpc);
3577
3578 if (IS_R300(sc))
3579 PUT32(sc, RADEON_RBBM_SOFT_RESET, rbbm);
3580
3581 PUT32(sc, RADEON_CLOCK_CNTL_INDEX, clkindex);
3582 PRINTREG(RADEON_CLOCK_CNTL_INDEX);
3583 PUTPLL(sc, RADEON_MCLK_CNTL, mclkcntl);
3584
3585 if (HAS_R300CG(sc))
3586 radeonfb_r300cg_workaround(sc);
3587 }
3588
3589 static int
3590 radeonfb_set_curpos(struct radeonfb_display *dp, struct wsdisplay_curpos *pos)
3591 {
3592 int x, y;
3593
3594 x = pos->x;
3595 y = pos->y;
3596
3597 /*
3598 * This doesn't let a cursor move off the screen. I'm not
3599 * sure if this will have negative effects for e.g. Xinerama.
3600 * I'd guess Xinerama handles it by changing the cursor shape,
3601 * but that needs verification.
3602 */
3603 if (x >= dp->rd_virtx)
3604 x = dp->rd_virtx - 1;
3605 if (x < 0)
3606 x = 0;
3607 if (y >= dp->rd_virty)
3608 y = dp->rd_virty - 1;
3609 if (y < 0)
3610 y = 0;
3611
3612 dp->rd_cursor.rc_pos.x = x;
3613 dp->rd_cursor.rc_pos.y = y;
3614
3615 radeonfb_cursor_position(dp);
3616 return 0;
3617 }
3618
3619 static int
3620 radeonfb_set_cursor(struct radeonfb_display *dp, struct wsdisplay_cursor *wc)
3621 {
3622 unsigned flags;
3623
3624 uint8_t r[2], g[2], b[2];
3625 unsigned index, count;
3626 int i, err;
3627 int pitch, size;
3628 struct radeonfb_cursor nc;
3629
3630 flags = wc->which;
3631
3632 /* copy old values */
3633 nc = dp->rd_cursor;
3634
3635 if (flags & WSDISPLAY_CURSOR_DOCMAP) {
3636 index = wc->cmap.index;
3637 count = wc->cmap.count;
3638
3639 if (index >= 2 || count > 2 - index)
3640 return EINVAL;
3641
3642 err = copyin(wc->cmap.red, &r[index], count);
3643 if (err)
3644 return err;
3645 err = copyin(wc->cmap.green, &g[index], count);
3646 if (err)
3647 return err;
3648 err = copyin(wc->cmap.blue, &b[index], count);
3649 if (err)
3650 return err;
3651
3652 for (i = index; i < index + count; i++) {
3653 nc.rc_cmap[i] =
3654 (r[i] << 16) + (g[i] << 8) + (b[i] << 0);
3655 }
3656 }
3657
3658 if (flags & WSDISPLAY_CURSOR_DOSHAPE) {
3659 if ((wc->size.x > RADEON_CURSORMAXX) ||
3660 (wc->size.y > RADEON_CURSORMAXY))
3661 return EINVAL;
3662
3663 /* figure bytes per line */
3664 pitch = (wc->size.x + 7) / 8;
3665 size = pitch * wc->size.y;
3666
3667 /* clear the old cursor and mask */
3668 memset(nc.rc_image, 0, 512);
3669 memset(nc.rc_mask, 0, 512);
3670
3671 nc.rc_size = wc->size;
3672
3673 if ((err = copyin(wc->image, nc.rc_image, size)) != 0)
3674 return err;
3675
3676 if ((err = copyin(wc->mask, nc.rc_mask, size)) != 0)
3677 return err;
3678 }
3679
3680 if (flags & WSDISPLAY_CURSOR_DOHOT) {
3681 nc.rc_hot = wc->hot;
3682 if (nc.rc_hot.x >= nc.rc_size.x)
3683 nc.rc_hot.x = nc.rc_size.x - 1;
3684 if (nc.rc_hot.y >= nc.rc_size.y)
3685 nc.rc_hot.y = nc.rc_size.y - 1;
3686 }
3687
3688 if (flags & WSDISPLAY_CURSOR_DOPOS) {
3689 nc.rc_pos = wc->pos;
3690 if (nc.rc_pos.x >= dp->rd_virtx)
3691 nc.rc_pos.x = dp->rd_virtx - 1;
3692 #if 0
3693 if (nc.rc_pos.x < 0)
3694 nc.rc_pos.x = 0;
3695 #endif
3696 if (nc.rc_pos.y >= dp->rd_virty)
3697 nc.rc_pos.y = dp->rd_virty - 1;
3698 #if 0
3699 if (nc.rc_pos.y < 0)
3700 nc.rc_pos.y = 0;
3701 #endif
3702 }
3703 if (flags & WSDISPLAY_CURSOR_DOCUR) {
3704 nc.rc_visible = wc->enable;
3705 }
3706
3707 dp->rd_cursor = nc;
3708 radeonfb_cursor_update(dp, wc->which);
3709
3710 return 0;
3711 }
3712
3713 static uint8_t
3714 radeonfb_backwards(uint8_t d)
3715 {
3716 uint8_t l;
3717
3718 l = d << 7;
3719 l |= ((d & 0x02) << 5);
3720 l |= ((d & 0x04) << 3);
3721 l |= ((d & 0x08) << 1);
3722 l |= ((d & 0x10) >> 1);
3723 l |= ((d & 0x20) >> 3);
3724 l |= ((d & 0x40) >> 5);
3725 l |= ((d & 0x80) >> 7);
3726 return l;
3727 }
3728
3729 /*
3730 * Change the cursor shape. Call this with the cursor locked to avoid
3731 * flickering/tearing.
3732 */
3733 static void
3734 radeonfb_cursor_shape(struct radeonfb_display *dp)
3735 {
3736 uint8_t and[512], xor[512];
3737 int i, j, src, dst /* , pitch */;
3738 const uint8_t *msk = dp->rd_cursor.rc_mask;
3739 const uint8_t *img = dp->rd_cursor.rc_image;
3740
3741 /*
3742 * Radeon cursor data interleaves one line of AND data followed
3743 * by a line of XOR data. (Each line corresponds to a whole hardware
3744 * pitch - i.e. 64 pixels or 8 bytes.)
3745 *
3746 * The cursor is displayed using the following table:
3747 *
3748 * AND XOR Result
3749 * ----------------------
3750 * 0 0 Cursor color 0
3751 * 0 1 Cursor color 1
3752 * 1 0 Transparent
3753 * 1 1 Complement of background
3754 *
3755 * Our masks are therefore different from what we were passed.
3756 * Passed in, I'm assuming the data represents either color 0 or 1,
3757 * and a mask, so the passed in table looks like:
3758 *
3759 * IMG Mask Result
3760 * -----------------------
3761 * 0 0 Transparent
3762 * 0 1 Cursor color 0
3763 * 1 0 Transparent
3764 * 1 1 Cursor color 1
3765 *
3766 * IF mask bit == 1, AND = 0, XOR = color.
3767 * IF mask bit == 0, AND = 1, XOR = 0.
3768 *
3769 * hence: AND = ~(mask); XOR = color & ~(mask);
3770 */
3771
3772 /* pitch = ((dp->rd_cursor.rc_size.x + 7) / 8); */
3773
3774 /* start by assuming all bits are transparent */
3775 memset(and, 0xff, 512);
3776 memset(xor, 0x00, 512);
3777
3778 src = 0;
3779 dst = 0;
3780 for (i = 0; i < 64; i++) {
3781 for (j = 0; j < 64; j += 8) {
3782 if ((i < dp->rd_cursor.rc_size.y) &&
3783 (j < dp->rd_cursor.rc_size.x)) {
3784
3785 /* take care to leave odd bits alone */
3786 and[dst] &= ~(msk[src]);
3787 xor[dst] = img[src] & msk[src];
3788 src++;
3789 }
3790 dst++;
3791 }
3792 }
3793
3794 for (i = 0; i < 512; i++) {
3795 and[i] = radeonfb_backwards(and[i]);
3796 xor[i] = radeonfb_backwards(xor[i]);
3797 }
3798
3799 /* copy the image into place */
3800 for (i = 0; i < 64; i++) {
3801 memcpy((uint8_t *)dp->rd_curptr + (i * 16),
3802 &and[i * 8], 8);
3803 memcpy((uint8_t *)dp->rd_curptr + (i * 16) + 8,
3804 &xor[i * 8], 8);
3805 }
3806 }
3807
3808 static void
3809 radeonfb_cursor_position(struct radeonfb_display *dp)
3810 {
3811 struct radeonfb_softc *sc = dp->rd_softc;
3812 uint32_t offset, hvoff, hvpos; /* registers */
3813 uint32_t coff; /* cursor offset */
3814 int i, x, y, xoff, yoff, crtcoff;
3815
3816 /*
3817 * XXX: this also needs to handle pan/scan
3818 */
3819 for (i = 0; i < dp->rd_ncrtcs; i++) {
3820
3821 struct radeonfb_crtc *rcp = &dp->rd_crtcs[i];
3822
3823 if (rcp->rc_number) {
3824 offset = RADEON_CUR2_OFFSET;
3825 hvoff = RADEON_CUR2_HORZ_VERT_OFF;
3826 hvpos = RADEON_CUR2_HORZ_VERT_POSN;
3827 crtcoff = RADEON_CRTC2_OFFSET;
3828 } else {
3829 offset = RADEON_CUR_OFFSET;
3830 hvoff = RADEON_CUR_HORZ_VERT_OFF;
3831 hvpos = RADEON_CUR_HORZ_VERT_POSN;
3832 crtcoff = RADEON_CRTC_OFFSET;
3833 }
3834
3835 x = dp->rd_cursor.rc_pos.x;
3836 y = dp->rd_cursor.rc_pos.y;
3837
3838 while (y < rcp->rc_yoffset) {
3839 rcp->rc_yoffset -= RADEON_PANINCREMENT;
3840 }
3841 while (y >= (rcp->rc_yoffset + rcp->rc_videomode.vdisplay)) {
3842 rcp->rc_yoffset += RADEON_PANINCREMENT;
3843 }
3844 while (x < rcp->rc_xoffset) {
3845 rcp->rc_xoffset -= RADEON_PANINCREMENT;
3846 }
3847 while (x >= (rcp->rc_xoffset + rcp->rc_videomode.hdisplay)) {
3848 rcp->rc_xoffset += RADEON_PANINCREMENT;
3849 }
3850
3851 /* adjust for the cursor's hotspot */
3852 x -= dp->rd_cursor.rc_hot.x;
3853 y -= dp->rd_cursor.rc_hot.y;
3854 xoff = yoff = 0;
3855
3856 if (x >= dp->rd_virtx)
3857 x = dp->rd_virtx - 1;
3858 if (y >= dp->rd_virty)
3859 y = dp->rd_virty - 1;
3860
3861 /* now adjust cursor so it is relative to viewport */
3862 x -= rcp->rc_xoffset;
3863 y -= rcp->rc_yoffset;
3864
3865 /*
3866 * no need to check for fall off, because we should
3867 * never move off the screen entirely!
3868 */
3869 coff = 0;
3870 if (x < 0) {
3871 xoff = -x;
3872 x = 0;
3873 }
3874 if (y < 0) {
3875 yoff = -y;
3876 y = 0;
3877 coff = (yoff * 2) * 8;
3878 }
3879
3880 /* pan the display */
3881 PUT32(sc, crtcoff, (rcp->rc_yoffset * dp->rd_stride) +
3882 rcp->rc_xoffset);
3883
3884 PUT32(sc, offset, (dp->rd_curoff + coff) | RADEON_CUR_LOCK);
3885 PUT32(sc, hvoff, (xoff << 16) | (yoff) | RADEON_CUR_LOCK);
3886 /* NB: this unlocks the cursor */
3887 PUT32(sc, hvpos, (x << 16) | y);
3888 }
3889 }
3890
3891 static void
3892 radeonfb_cursor_visible(struct radeonfb_display *dp)
3893 {
3894 int i;
3895 uint32_t gencntl, bit;
3896
3897 for (i = 0; i < dp->rd_ncrtcs; i++) {
3898 if (dp->rd_crtcs[i].rc_number) {
3899 gencntl = RADEON_CRTC2_GEN_CNTL;
3900 bit = RADEON_CRTC2_CUR_EN;
3901 } else {
3902 gencntl = RADEON_CRTC_GEN_CNTL;
3903 bit = RADEON_CRTC_CUR_EN;
3904 }
3905
3906 if (dp->rd_cursor.rc_visible)
3907 SET32(dp->rd_softc, gencntl, bit);
3908 else
3909 CLR32(dp->rd_softc, gencntl, bit);
3910 }
3911 }
3912
3913 static void
3914 radeonfb_cursor_cmap(struct radeonfb_display *dp)
3915 {
3916 int i;
3917 uint32_t c0reg, c1reg;
3918 struct radeonfb_softc *sc = dp->rd_softc;
3919
3920 for (i = 0; i < dp->rd_ncrtcs; i++) {
3921 if (dp->rd_crtcs[i].rc_number) {
3922 c0reg = RADEON_CUR2_CLR0;
3923 c1reg = RADEON_CUR2_CLR1;
3924 } else {
3925 c0reg = RADEON_CUR_CLR0;
3926 c1reg = RADEON_CUR_CLR1;
3927 }
3928
3929 PUT32(sc, c0reg, dp->rd_cursor.rc_cmap[0]);
3930 PUT32(sc, c1reg, dp->rd_cursor.rc_cmap[1]);
3931 }
3932 }
3933
3934 static void
3935 radeonfb_cursor_update(struct radeonfb_display *dp, unsigned which)
3936 {
3937 struct radeonfb_softc *sc;
3938 int i;
3939
3940 sc = dp->rd_softc;
3941 for (i = 0; i < dp->rd_ncrtcs; i++) {
3942 if (dp->rd_crtcs[i].rc_number) {
3943 SET32(sc, RADEON_CUR2_OFFSET, RADEON_CUR_LOCK);
3944 } else {
3945 SET32(sc, RADEON_CUR_OFFSET,RADEON_CUR_LOCK);
3946 }
3947 }
3948
3949 if (which & WSDISPLAY_CURSOR_DOCMAP)
3950 radeonfb_cursor_cmap(dp);
3951
3952 if (which & WSDISPLAY_CURSOR_DOSHAPE)
3953 radeonfb_cursor_shape(dp);
3954
3955 if (which & WSDISPLAY_CURSOR_DOCUR)
3956 radeonfb_cursor_visible(dp);
3957
3958 /* this one is unconditional, because it updates other stuff */
3959 radeonfb_cursor_position(dp);
3960 }
3961
3962 static struct videomode *
3963 radeonfb_best_refresh(struct videomode *m1, struct videomode *m2)
3964 {
3965 int r1, r2;
3966
3967 /* otherwise pick the higher refresh rate */
3968 r1 = DIVIDE(DIVIDE(m1->dot_clock, m1->htotal), m1->vtotal);
3969 r2 = DIVIDE(DIVIDE(m2->dot_clock, m2->htotal), m2->vtotal);
3970
3971 return (r1 < r2 ? m2 : m1);
3972 }
3973
3974 static const struct videomode *
3975 radeonfb_port_mode(struct radeonfb_softc *sc, struct radeonfb_port *rp,
3976 int x, int y)
3977 {
3978 struct edid_info *ep = &rp->rp_edid;
3979 struct videomode *vmp = NULL;
3980 int i;
3981
3982 if (!rp->rp_edid_valid) {
3983 /* fallback to safe mode */
3984 return radeonfb_modelookup(sc->sc_defaultmode);
3985 }
3986
3987 /* always choose the preferred mode first! */
3988 if (ep->edid_preferred_mode) {
3989
3990 /* XXX: add auto-stretching support for native mode */
3991
3992 /* this may want panning to occur, btw */
3993 if ((ep->edid_preferred_mode->hdisplay <= x) &&
3994 (ep->edid_preferred_mode->vdisplay <= y))
3995 return ep->edid_preferred_mode;
3996 }
3997
3998 for (i = 0; i < ep->edid_nmodes; i++) {
3999 /*
4000 * We elect to pick a resolution that is too large for
4001 * the monitor than one that is too small. This means
4002 * that we will prefer to pan rather than to try to
4003 * center a smaller display on a larger screen. In
4004 * practice, this shouldn't matter because if a
4005 * monitor can support a larger resolution, it can
4006 * probably also support the smaller. A specific
4007 * exception is fixed format panels, but hopefully
4008 * they are properly dealt with by the "autostretch"
4009 * logic above.
4010 */
4011 if ((ep->edid_modes[i].hdisplay > x) ||
4012 (ep->edid_modes[i].vdisplay > y)) {
4013 continue;
4014 }
4015
4016 /*
4017 * at this point, the display mode is no larger than
4018 * what we've requested.
4019 */
4020 if (vmp == NULL)
4021 vmp = &ep->edid_modes[i];
4022
4023 /* eliminate smaller modes */
4024 if ((vmp->hdisplay >= ep->edid_modes[i].hdisplay) ||
4025 (vmp->vdisplay >= ep->edid_modes[i].vdisplay))
4026 continue;
4027
4028 if ((vmp->hdisplay < ep->edid_modes[i].hdisplay) ||
4029 (vmp->vdisplay < ep->edid_modes[i].vdisplay)) {
4030 vmp = &ep->edid_modes[i];
4031 continue;
4032 }
4033
4034 KASSERT(vmp->hdisplay == ep->edid_modes[i].hdisplay);
4035 KASSERT(vmp->vdisplay == ep->edid_modes[i].vdisplay);
4036
4037 vmp = radeonfb_best_refresh(vmp, &ep->edid_modes[i]);
4038 }
4039
4040 return (vmp ? vmp : radeonfb_modelookup(sc->sc_defaultmode));
4041 }
4042
4043 static int
4044 radeonfb_hasres(struct videomode *list, int nlist, int x, int y)
4045 {
4046 int i;
4047
4048 for (i = 0; i < nlist; i++) {
4049 if ((x == list[i].hdisplay) &&
4050 (y == list[i].vdisplay)) {
4051 return 1;
4052 }
4053 }
4054 return 0;
4055 }
4056
4057 static void
4058 radeonfb_pickres(struct radeonfb_display *dp, uint16_t *x, uint16_t *y,
4059 int pan)
4060 {
4061 struct radeonfb_port *rp;
4062 struct edid_info *ep;
4063 int i, j;
4064
4065 *x = 0;
4066 *y = 0;
4067
4068 if (pan) {
4069 for (i = 0; i < dp->rd_ncrtcs; i++) {
4070 rp = dp->rd_crtcs[i].rc_port;
4071 ep = &rp->rp_edid;
4072 if (!rp->rp_edid_valid) {
4073 /* monitor not present */
4074 continue;
4075 }
4076
4077 /*
4078 * For now we are ignoring "conflict" that
4079 * could occur when mixing some modes like
4080 * 1280x1024 and 1400x800. It isn't clear
4081 * which is better, so the first one wins.
4082 */
4083 for (j = 0; j < ep->edid_nmodes; j++) {
4084 /*
4085 * ignore resolutions that are too big for
4086 * the radeon
4087 */
4088 if (ep->edid_modes[j].hdisplay >
4089 dp->rd_softc->sc_maxx)
4090 continue;
4091 if (ep->edid_modes[j].vdisplay >
4092 dp->rd_softc->sc_maxy)
4093 continue;
4094
4095 /*
4096 * pick largest resolution, the
4097 * smaller monitor will pan
4098 */
4099 if ((ep->edid_modes[j].hdisplay >= *x) &&
4100 (ep->edid_modes[j].vdisplay >= *y)) {
4101 *x = ep->edid_modes[j].hdisplay;
4102 *y = ep->edid_modes[j].vdisplay;
4103 }
4104 }
4105 }
4106
4107 } else {
4108 struct videomode modes[64];
4109 int nmodes = 0;
4110 int valid = 0;
4111
4112 for (i = 0; i < dp->rd_ncrtcs; i++) {
4113 /*
4114 * pick the largest resolution in common.
4115 */
4116 rp = dp->rd_crtcs[i].rc_port;
4117 ep = &rp->rp_edid;
4118
4119 if (!rp->rp_edid_valid)
4120 continue;
4121
4122 if (!valid) {
4123 /*
4124 * Pick the preferred mode for this port
4125 * if available.
4126 */
4127 if (ep->edid_preferred_mode) {
4128 struct videomode *vmp =
4129 ep->edid_preferred_mode;
4130
4131 if ((vmp->hdisplay <=
4132 dp->rd_softc->sc_maxx) &&
4133 (vmp->vdisplay <=
4134 dp->rd_softc->sc_maxy))
4135 modes[nmodes++] = *vmp;
4136 } else {
4137
4138 /* initialize starting list */
4139 for (j = 0; j < ep->edid_nmodes; j++) {
4140 /*
4141 * ignore resolutions that are
4142 * too big for the radeon
4143 */
4144 if (ep->edid_modes[j].hdisplay >
4145 dp->rd_softc->sc_maxx)
4146 continue;
4147 if (ep->edid_modes[j].vdisplay >
4148 dp->rd_softc->sc_maxy)
4149 continue;
4150
4151 modes[nmodes] =
4152 ep->edid_modes[j];
4153 nmodes++;
4154 }
4155 }
4156 valid = 1;
4157 } else {
4158 /* merge into preexisting list */
4159 for (j = 0; j < nmodes; j++) {
4160 if (!radeonfb_hasres(ep->edid_modes,
4161 ep->edid_nmodes,
4162 modes[j].hdisplay,
4163 modes[j].vdisplay)) {
4164 modes[j] = modes[nmodes];
4165 j--;
4166 nmodes--;
4167 }
4168 }
4169 }
4170 }
4171
4172 /* now we have to pick from the merged list */
4173 for (i = 0; i < nmodes; i++) {
4174 if ((modes[i].hdisplay >= *x) &&
4175 (modes[i].vdisplay >= *y)) {
4176 *x = modes[i].hdisplay;
4177 *y = modes[i].vdisplay;
4178 }
4179 }
4180 }
4181
4182 if ((*x == 0) || (*y == 0)) {
4183 /* fallback to safe mode */
4184 *x = 640;
4185 *y = 480;
4186 }
4187 }
4188
4189 /*
4190 * backlight levels are linear on:
4191 * - RV200, RV250, RV280, RV350
4192 * - but NOT on PowerBook4,3 6,3 6,5
4193 * according to Linux' radeonfb
4194 */
4195
4196 /* Get the current backlight level for the display. */
4197
4198 static int
4199 radeonfb_get_backlight(struct radeonfb_display *dp)
4200 {
4201 int s;
4202 uint32_t level;
4203
4204 s = spltty();
4205
4206 level = radeonfb_get32(dp->rd_softc, RADEON_LVDS_GEN_CNTL);
4207 level &= RADEON_LVDS_BL_MOD_LEV_MASK;
4208 level >>= RADEON_LVDS_BL_MOD_LEV_SHIFT;
4209
4210 /*
4211 * On some chips, we should negate the backlight level.
4212 * XXX Find out on which chips.
4213 */
4214 if (dp->rd_softc->sc_flags & RFB_INV_BLIGHT)
4215 level = RADEONFB_BACKLIGHT_MAX - level;
4216
4217 splx(s);
4218
4219 return level;
4220 }
4221
4222 /* Set the backlight to the given level for the display. */
4223 static void
4224 radeonfb_switch_backlight(struct radeonfb_display *dp, int on)
4225 {
4226 if (dp->rd_bl_on == on)
4227 return;
4228 dp->rd_bl_on = on;
4229 radeonfb_set_backlight(dp, dp->rd_bl_level);
4230 }
4231
4232 static int
4233 radeonfb_set_backlight(struct radeonfb_display *dp, int level)
4234 {
4235 struct radeonfb_softc *sc = dp->rd_softc;
4236 int rlevel, s;
4237 uint32_t lvds;
4238
4239 if(!sc->sc_mapped)
4240 return 0;
4241
4242 s = spltty();
4243
4244 dp->rd_bl_level = level;
4245 if (dp->rd_bl_on == 0)
4246 level = 0;
4247
4248 if (level < 0)
4249 level = 0;
4250 else if (level >= RADEONFB_BACKLIGHT_MAX)
4251 level = RADEONFB_BACKLIGHT_MAX;
4252
4253 /* On some chips, we should negate the backlight level. */
4254 if (dp->rd_softc->sc_flags & RFB_INV_BLIGHT) {
4255 rlevel = RADEONFB_BACKLIGHT_MAX - level;
4256 } else
4257 rlevel = level;
4258
4259 callout_stop(&dp->rd_bl_lvds_co);
4260 radeonfb_engine_idle(sc);
4261
4262 /*
4263 * Turn off the display if the backlight is set to 0, since the
4264 * display is useless without backlight anyway.
4265 */
4266 if (level == 0)
4267 radeonfb_blank(dp, 1);
4268 else if (radeonfb_get_backlight(dp) == 0)
4269 radeonfb_blank(dp, 0);
4270
4271 lvds = radeonfb_get32(sc, RADEON_LVDS_GEN_CNTL);
4272 lvds &= ~RADEON_LVDS_DISPLAY_DIS;
4273 if (!(lvds & RADEON_LVDS_BLON) || !(lvds & RADEON_LVDS_ON)) {
4274 lvds |= dp->rd_bl_lvds_val & RADEON_LVDS_DIGON;
4275 lvds |= RADEON_LVDS_BLON | RADEON_LVDS_EN;
4276 radeonfb_put32(sc, RADEON_LVDS_GEN_CNTL, lvds);
4277 lvds &= ~RADEON_LVDS_BL_MOD_LEV_MASK;
4278 lvds |= rlevel << RADEON_LVDS_BL_MOD_LEV_SHIFT;
4279 lvds |= RADEON_LVDS_ON;
4280 lvds |= dp->rd_bl_lvds_val & RADEON_LVDS_BL_MOD_EN;
4281 } else {
4282 lvds &= ~RADEON_LVDS_BL_MOD_LEV_MASK;
4283 lvds |= rlevel << RADEON_LVDS_BL_MOD_LEV_SHIFT;
4284 radeonfb_put32(sc, RADEON_LVDS_GEN_CNTL, lvds);
4285 }
4286
4287 dp->rd_bl_lvds_val &= ~RADEON_LVDS_STATE_MASK;
4288 dp->rd_bl_lvds_val |= lvds & RADEON_LVDS_STATE_MASK;
4289 /* XXX What is the correct delay? */
4290 callout_schedule(&dp->rd_bl_lvds_co, 200 * hz);
4291
4292 splx(s);
4293
4294 return 0;
4295 }
4296
4297 /*
4298 * Callout function for delayed operations on the LVDS_GEN_CNTL register.
4299 * Set the delayed bits in the register, and clear the stored delayed
4300 * value.
4301 */
4302
4303 static void radeonfb_lvds_callout(void *arg)
4304 {
4305 struct radeonfb_display *dp = arg;
4306 int s;
4307
4308 s = splhigh();
4309
4310 radeonfb_mask32(dp->rd_softc, RADEON_LVDS_GEN_CNTL, ~0,
4311 dp->rd_bl_lvds_val);
4312 dp->rd_bl_lvds_val = 0;
4313
4314 splx(s);
4315 }
4316
4317 static void
4318 radeonfb_brightness_up(device_t dev)
4319 {
4320 struct radeonfb_softc *sc = device_private(dev);
4321 struct radeonfb_display *dp = &sc->sc_displays[0];
4322 int level;
4323
4324 /* we assume the main display is the first one - need a better way */
4325 if (sc->sc_ndisplays < 1) return;
4326 /* make sure pushing the hotkeys always has an effect */
4327 dp->rd_bl_on = 1;
4328 level = dp->rd_bl_level;
4329 level = min(RADEONFB_BACKLIGHT_MAX, level + 5);
4330 radeonfb_set_backlight(dp, level);
4331 }
4332
4333 static void
4334 radeonfb_brightness_down(device_t dev)
4335 {
4336 struct radeonfb_softc *sc = device_private(dev);
4337 struct radeonfb_display *dp = &sc->sc_displays[0];
4338 int level;
4339
4340 /* we assume the main display is the first one - need a better way */
4341 if (sc->sc_ndisplays < 1) return;
4342 /* make sure pushing the hotkeys always has an effect */
4343 dp->rd_bl_on = 1;
4344 level = dp->rd_bl_level;
4345 level = max(0, level - 5);
4346 radeonfb_set_backlight(dp, level);
4347 }
4348