machfb.c revision 1.78 1 /* $NetBSD: machfb.c,v 1.78 2012/06/14 00:56:37 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2002 Bang Jun-Young
5 * Copyright (c) 2005, 2006, 2007 Michael Lorenz
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * Some code is derived from ATI Rage Pro and Derivatives Programmer's Guide.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0,
37 "$NetBSD: machfb.c,v 1.78 2012/06/14 00:56:37 macallan Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/callout.h>
45 #include <sys/lwp.h>
46 #include <sys/kauth.h>
47
48 #include <dev/videomode/videomode.h>
49 #include <dev/videomode/edidvar.h>
50
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcidevs.h>
54 #include <dev/pci/pciio.h>
55 #include <dev/pci/machfbreg.h>
56
57 #ifdef __sparc__
58 #include <dev/sun/fbio.h>
59 #include <dev/sun/fbvar.h>
60 #include <sys/conf.h>
61 #else
62 #include <dev/wscons/wsdisplayvar.h>
63 #endif
64
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wsfont/wsfont.h>
67 #include <dev/rasops/rasops.h>
68 #include <dev/pci/wsdisplay_pci.h>
69
70 #include <dev/wscons/wsdisplay_vconsvar.h>
71
72 #include "opt_wsemul.h"
73 #include "opt_machfb.h"
74
75 #define MACH64_REG_SIZE 1024
76 #define MACH64_REG_OFF 0x7ffc00
77
78 #define NBARS 3 /* number of Mach64 PCI BARs */
79
80 struct vga_bar {
81 bus_addr_t vb_base;
82 pcireg_t vb_busaddr;
83 bus_size_t vb_size;
84 pcireg_t vb_type;
85 int vb_flags;
86 };
87
88 struct mach64_softc {
89 device_t sc_dev;
90 #ifdef __sparc__
91 struct fbdevice sc_fb;
92 #endif
93 pci_chipset_tag_t sc_pc;
94 pcitag_t sc_pcitag;
95
96 struct vga_bar sc_bars[NBARS];
97 struct vga_bar sc_rom;
98
99 #define sc_aperbase sc_bars[0].vb_base
100 #define sc_apersize sc_bars[0].vb_size
101 #define sc_aperphys sc_bars[0].vb_busaddr
102
103 #define sc_iobase sc_bars[1].vb_base
104 #define sc_iosize sc_bars[1].vb_size
105
106 #define sc_regbase sc_bars[2].vb_base
107 #define sc_regsize sc_bars[2].vb_size
108 #define sc_regphys sc_bars[2].vb_busaddr
109
110 bus_space_tag_t sc_regt;
111 bus_space_tag_t sc_memt;
112 bus_space_tag_t sc_iot;
113 bus_space_handle_t sc_regh;
114 bus_space_handle_t sc_memh;
115 void *sc_aperture; /* mapped aperture vaddr */
116 void *sc_registers; /* mapped registers vaddr */
117
118 uint32_t sc_nbus, sc_ndev, sc_nfunc;
119 size_t memsize;
120 int memtype;
121
122 int sc_mode;
123 int sc_bg;
124 int sc_locked;
125
126 int has_dsp;
127 int bits_per_pixel;
128 int max_x;
129 int max_y;
130 int virt_x;
131 int virt_y;
132 int color_depth;
133
134 int mem_freq;
135 int ramdac_freq;
136 int ref_freq;
137
138 int ref_div;
139 int log2_vclk_post_div;
140 int vclk_post_div;
141 int vclk_fb_div;
142 int mclk_post_div;
143 int mclk_fb_div;
144 int sc_clock; /* which clock to use */
145
146 struct videomode *sc_my_mode;
147 int sc_edid_size;
148 uint8_t sc_edid_data[1024];
149
150 u_char sc_cmap_red[256];
151 u_char sc_cmap_green[256];
152 u_char sc_cmap_blue[256];
153 int sc_dacw, sc_blanked, sc_console;
154 struct vcons_data vd;
155 struct wsdisplay_accessops sc_accessops;
156 };
157
158 struct mach64_crtcregs {
159 uint32_t h_total_disp;
160 uint32_t h_sync_strt_wid;
161 uint32_t v_total_disp;
162 uint32_t v_sync_strt_wid;
163 uint32_t gen_cntl;
164 uint32_t clock_cntl;
165 uint32_t color_depth;
166 uint32_t dot_clock;
167 };
168
169 static struct {
170 uint16_t chip_id;
171 uint32_t ramdac_freq;
172 } const mach64_info[] = {
173 { PCI_PRODUCT_ATI_MACH64_GX, 135000 },
174 { PCI_PRODUCT_ATI_MACH64_CX, 135000 },
175 { PCI_PRODUCT_ATI_MACH64_CT, 135000 },
176 { PCI_PRODUCT_ATI_RAGE_PRO_AGP, 230000 },
177 { PCI_PRODUCT_ATI_RAGE_PRO_AGP1X, 230000 },
178 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_B, 230000 },
179 { PCI_PRODUCT_ATI_RAGE_XL_AGP, 230000 },
180 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_P, 230000 },
181 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_L, 230000 },
182 { PCI_PRODUCT_ATI_RAGE_XL_PCI, 230000 },
183 { PCI_PRODUCT_ATI_RAGE_XL_PCI66, 230000 },
184 { PCI_PRODUCT_ATI_RAGE_II, 135000 },
185 { PCI_PRODUCT_ATI_RAGE_IIP, 200000 },
186 { PCI_PRODUCT_ATI_RAGE_IIC_PCI, 230000 },
187 { PCI_PRODUCT_ATI_RAGE_IIC_AGP_B, 230000 },
188 { PCI_PRODUCT_ATI_RAGE_IIC_AGP_P, 230000 },
189 #if 0
190 { PCI_PRODUCT_ATI_RAGE_MOB_M3_PCI, 230000 },
191 { PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP, 230000 },
192 { PCI_PRODUCT_ATI_RAGE_MOBILITY, 230000 },
193 #endif
194 { PCI_PRODUCT_ATI_RAGE_LT_PRO_AGP, 230000 },
195 { PCI_PRODUCT_ATI_RAGE_LT_PRO, 230000 },
196 { PCI_PRODUCT_ATI_RAGE_LT, 230000 },
197 { PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI, 230000 },
198 { PCI_PRODUCT_ATI_MACH64_VT, 170000 },
199 { PCI_PRODUCT_ATI_MACH64_VTB, 200000 },
200 { PCI_PRODUCT_ATI_MACH64_VT4, 230000 }
201 };
202
203 static int mach64_chip_id, mach64_chip_rev;
204 static struct videomode default_mode = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
205
206 static const char *mach64_gx_memtype_names[] = {
207 "DRAM", "VRAM", "VRAM", "DRAM",
208 "DRAM", "VRAM", "VRAM", "(unknown type)"
209 };
210
211 static const char *mach64_memtype_names[] = {
212 "(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
213 "(unknown type)"
214 };
215
216 static struct videomode mach64_modes[] = {
217 /* 640x400 @ 70 Hz, 31.5 kHz */
218 { 25175, 640, 664, 760, 800, 400, 409, 411, 450, 0, NULL, },
219 /* 640x480 @ 72 Hz, 36.5 kHz */
220 { 25175, 640, 664, 760, 800, 480, 491, 493, 525, 0, NULL, },
221 /* 800x600 @ 72 Hz, 48.0 kHz */
222 { 50000, 800, 856, 976, 1040, 600, 637, 643, 666,
223 VID_PHSYNC | VID_PVSYNC, NULL, },
224 /* 1024x768 @ 70 Hz, 56.5 kHz */
225 { 75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806,
226 VID_NHSYNC | VID_NVSYNC, NULL, },
227 /* 1152x864 @ 70 Hz, 62.4 kHz */
228 { 92000, 1152, 1208, 1368, 1474, 864, 865, 875, 895, 0, NULL, },
229 /* 1280x1024 @ 70 Hz, 74.59 kHz */
230 { 126500, 1280, 1312, 1472, 1696, 1024, 1032, 1040, 1068,
231 VID_NHSYNC | VID_NVSYNC, NULL, }
232 };
233
234 extern const u_char rasops_cmap[768];
235
236 static int mach64_match(device_t, cfdata_t, void *);
237 static void mach64_attach(device_t, device_t, void *);
238
239 CFATTACH_DECL_NEW(machfb, sizeof(struct mach64_softc), mach64_match, mach64_attach,
240 NULL, NULL);
241
242 static void mach64_init(struct mach64_softc *);
243 static int mach64_get_memsize(struct mach64_softc *);
244 static int mach64_get_max_ramdac(struct mach64_softc *);
245
246 #if defined(__sparc__) || defined(__powerpc__)
247 static void mach64_get_mode(struct mach64_softc *, struct videomode *);
248 #endif
249
250 static int mach64_calc_crtcregs(struct mach64_softc *,
251 struct mach64_crtcregs *,
252 struct videomode *);
253 static void mach64_set_crtcregs(struct mach64_softc *,
254 struct mach64_crtcregs *);
255
256 static int mach64_modeswitch(struct mach64_softc *, struct videomode *);
257 static void mach64_set_dsp(struct mach64_softc *);
258 static void mach64_set_pll(struct mach64_softc *, int);
259 static void mach64_reset_engine(struct mach64_softc *);
260 static void mach64_init_engine(struct mach64_softc *);
261 #if 0
262 static void mach64_adjust_frame(struct mach64_softc *, int, int);
263 #endif
264 static void mach64_init_lut(struct mach64_softc *);
265
266 static void mach64_init_screen(void *, struct vcons_screen *, int, long *);
267 static int mach64_set_screentype(struct mach64_softc *,
268 const struct wsscreen_descr *);
269 static int mach64_is_console(struct mach64_softc *);
270
271 static void mach64_cursor(void *, int, int, int);
272 #if 0
273 static int mach64_mapchar(void *, int, u_int *);
274 #endif
275 static void mach64_putchar(void *, int, int, u_int, long);
276 static void mach64_copycols(void *, int, int, int, int);
277 static void mach64_erasecols(void *, int, int, int, long);
278 static void mach64_copyrows(void *, int, int, int);
279 static void mach64_eraserows(void *, int, int, long);
280 static void mach64_clearscreen(struct mach64_softc *);
281
282 static int mach64_putcmap(struct mach64_softc *, struct wsdisplay_cmap *);
283 static int mach64_getcmap(struct mach64_softc *, struct wsdisplay_cmap *);
284 static int mach64_putpalreg(struct mach64_softc *, uint8_t, uint8_t,
285 uint8_t, uint8_t);
286 static void mach64_bitblt(struct mach64_softc *, int, int, int, int, int,
287 int, int, int) ;
288 static void mach64_rectfill(struct mach64_softc *, int, int, int, int, int);
289 static void mach64_setup_mono(struct mach64_softc *, int, int, int, int,
290 uint32_t, uint32_t);
291 static void mach64_feed_bytes(struct mach64_softc *, int, uint8_t *);
292 #if 0
293 static void mach64_showpal(struct mach64_softc *);
294 #endif
295
296 static void set_address(struct rasops_info *, void *);
297 static void machfb_blank(struct mach64_softc *, int);
298 static int machfb_drm_print(void *, const char *);
299
300 static struct wsscreen_descr mach64_defaultscreen = {
301 "default",
302 80, 30,
303 NULL,
304 8, 16,
305 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
306 &default_mode
307 }, mach64_80x25_screen = {
308 "80x25", 80, 25,
309 NULL,
310 8, 16,
311 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
312 &mach64_modes[0]
313 }, mach64_80x30_screen = {
314 "80x30", 80, 30,
315 NULL,
316 8, 16,
317 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
318 &mach64_modes[1]
319 }, mach64_80x40_screen = {
320 "80x40", 80, 40,
321 NULL,
322 8, 10,
323 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
324 &mach64_modes[0]
325 }, mach64_80x50_screen = {
326 "80x50", 80, 50,
327 NULL,
328 8, 8,
329 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
330 &mach64_modes[0]
331 }, mach64_100x37_screen = {
332 "100x37", 100, 37,
333 NULL,
334 8, 16,
335 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
336 &mach64_modes[2]
337 }, mach64_128x48_screen = {
338 "128x48", 128, 48,
339 NULL,
340 8, 16,
341 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
342 &mach64_modes[3]
343 }, mach64_144x54_screen = {
344 "144x54", 144, 54,
345 NULL,
346 8, 16,
347 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
348 &mach64_modes[4]
349 }, mach64_160x64_screen = {
350 "160x54", 160, 64,
351 NULL,
352 8, 16,
353 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
354 &mach64_modes[5]
355 };
356
357 static const struct wsscreen_descr *_mach64_scrlist[] = {
358 &mach64_defaultscreen,
359 &mach64_80x25_screen,
360 &mach64_80x30_screen,
361 &mach64_80x40_screen,
362 &mach64_80x50_screen,
363 &mach64_100x37_screen,
364 &mach64_128x48_screen,
365 &mach64_144x54_screen,
366 &mach64_160x64_screen
367 };
368
369 static struct wsscreen_list mach64_screenlist = {
370 __arraycount(_mach64_scrlist),
371 _mach64_scrlist
372 };
373
374 static int mach64_ioctl(void *, void *, u_long, void *, int,
375 struct lwp *);
376 static paddr_t mach64_mmap(void *, void *, off_t, int);
377
378 #if 0
379 static int mach64_load_font(void *, void *, struct wsdisplay_font *);
380 #endif
381
382
383 static struct vcons_screen mach64_console_screen;
384
385 /* framebuffer device, SPARC-only so far */
386 #ifdef __sparc__
387
388 static void machfb_unblank(device_t);
389 static void machfb_fbattach(struct mach64_softc *);
390
391 extern struct cfdriver machfb_cd;
392
393 dev_type_open(machfb_fbopen);
394 dev_type_close(machfb_fbclose);
395 dev_type_ioctl(machfb_fbioctl);
396 dev_type_mmap(machfb_fbmmap);
397
398 /* frame buffer generic driver */
399 static struct fbdriver machfb_fbdriver = {
400 machfb_unblank, machfb_fbopen, machfb_fbclose, machfb_fbioctl, nopoll,
401 machfb_fbmmap, nokqfilter
402 };
403
404 #endif /* __sparc__ */
405
406 /*
407 * Inline functions for getting access to register aperture.
408 */
409
410 static inline uint32_t
411 regr(struct mach64_softc *sc, uint32_t index)
412 {
413 return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
414 }
415
416 static inline uint8_t
417 regrb(struct mach64_softc *sc, uint32_t index)
418 {
419 return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
420 }
421
422 static inline void
423 regw(struct mach64_softc *sc, uint32_t index, uint32_t data)
424 {
425 bus_space_write_4(sc->sc_regt, sc->sc_regh, index, data);
426 bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 4,
427 BUS_SPACE_BARRIER_WRITE);
428 }
429
430 static inline void
431 regwb(struct mach64_softc *sc, uint32_t index, uint8_t data)
432 {
433 bus_space_write_1(sc->sc_regt, sc->sc_regh, index, data);
434 bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 1,
435 BUS_SPACE_BARRIER_WRITE);
436 }
437
438 static inline void
439 regwb_pll(struct mach64_softc *sc, uint32_t index, uint8_t data)
440 {
441 uint32_t reg;
442
443 reg = regr(sc, CLOCK_CNTL);
444 reg |= PLL_WR_EN;
445 regw(sc, CLOCK_CNTL, reg);
446 reg &= ~(PLL_ADDR | PLL_DATA);
447 reg |= (index & 0x3f) << PLL_ADDR_SHIFT;
448 reg |= data << PLL_DATA_SHIFT;
449 reg |= CLOCK_STROBE;
450 regw(sc, CLOCK_CNTL, reg);
451 reg &= ~PLL_WR_EN;
452 regw(sc, CLOCK_CNTL, reg);
453 }
454
455 static inline uint8_t
456 regrb_pll(struct mach64_softc *sc, uint32_t index)
457 {
458
459 regwb(sc, CLOCK_CNTL + 1, index << 2);
460 return regrb(sc, CLOCK_CNTL + 2);
461 }
462
463 static inline void
464 wait_for_fifo(struct mach64_softc *sc, uint8_t v)
465 {
466 while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
467 continue;
468 }
469
470 static inline void
471 wait_for_idle(struct mach64_softc *sc)
472 {
473 wait_for_fifo(sc, 16);
474 while ((regr(sc, GUI_STAT) & 1) != 0)
475 continue;
476 }
477
478 static int
479 mach64_match(device_t parent, cfdata_t match, void *aux)
480 {
481 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
482 int i;
483
484 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
485 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
486 return 0;
487
488 for (i = 0; i < __arraycount(mach64_info); i++)
489 if (PCI_PRODUCT(pa->pa_id) == mach64_info[i].chip_id) {
490 mach64_chip_id = PCI_PRODUCT(pa->pa_id);
491 mach64_chip_rev = PCI_REVISION(pa->pa_class);
492 return 100;
493 }
494
495 return 0;
496 }
497
498 static void
499 mach64_attach(device_t parent, device_t self, void *aux)
500 {
501 struct mach64_softc *sc = device_private(self);
502 struct pci_attach_args *pa = aux;
503 struct rasops_info *ri;
504 prop_data_t edid_data;
505 #if defined(__sparc__) || defined(__powerpc__)
506 const struct videomode *mode = NULL;
507 #endif
508 int bar, id, expected_id;
509 int is_gx;
510 const char **memtype_names;
511 struct wsemuldisplaydev_attach_args aa;
512 long defattr;
513 int setmode, width, height;
514 pcireg_t screg;
515 uint32_t reg;
516 const pcireg_t enables = PCI_COMMAND_MEM_ENABLE;
517
518 sc->sc_dev = self;
519 sc->sc_pc = pa->pa_pc;
520 sc->sc_pcitag = pa->pa_tag;
521 sc->sc_dacw = -1;
522 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
523 sc->sc_nbus = pa->pa_bus;
524 sc->sc_ndev = pa->pa_device;
525 sc->sc_nfunc = pa->pa_function;
526 sc->sc_locked = 0;
527 sc->sc_iot = pa->pa_iot;
528 sc->sc_accessops.ioctl = mach64_ioctl;
529 sc->sc_accessops.mmap = mach64_mmap;
530
531 pci_aprint_devinfo(pa, "Graphics processor");
532 #ifdef MACHFB_DEBUG
533 printf(prop_dictionary_externalize(device_properties(self)));
534 #endif
535
536 /* enable memory access */
537 screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
538 if ((screg & enables) != enables) {
539 screg |= enables;
540 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
541 PCI_COMMAND_STATUS_REG, screg);
542 }
543 for (bar = 0; bar < NBARS; bar++) {
544 reg = PCI_MAPREG_START + (bar * 4);
545 sc->sc_bars[bar].vb_type = pci_mapreg_type(sc->sc_pc,
546 sc->sc_pcitag, reg);
547 (void)pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, reg,
548 sc->sc_bars[bar].vb_type, &sc->sc_bars[bar].vb_base,
549 &sc->sc_bars[bar].vb_size, &sc->sc_bars[bar].vb_flags);
550 sc->sc_bars[bar].vb_busaddr = pci_conf_read(sc->sc_pc,
551 sc->sc_pcitag, reg) & 0xfffffff0;
552 }
553 aprint_debug_dev(sc->sc_dev, "aperture size %08x\n",
554 (uint32_t)sc->sc_apersize);
555
556 sc->sc_rom.vb_type = PCI_MAPREG_TYPE_ROM;
557 pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, PCI_MAPREG_ROM,
558 sc->sc_rom.vb_type, &sc->sc_rom.vb_base,
559 &sc->sc_rom.vb_size, &sc->sc_rom.vb_flags);
560 sc->sc_memt = pa->pa_memt;
561
562 if (bus_space_map(sc->sc_memt, sc->sc_aperbase, sc->sc_apersize,
563 BUS_SPACE_MAP_LINEAR, &sc->sc_memh)) {
564 panic("%s: failed to map aperture", device_xname(sc->sc_dev));
565 }
566 sc->sc_aperture = (void *)bus_space_vaddr(sc->sc_memt, sc->sc_memh);
567
568 /* If the BAR was never mapped, fix it up in MMIO. */
569 if(sc->sc_regsize == 0) {
570 sc->sc_regsize = MACH64_REG_SIZE;
571 sc->sc_regbase = sc->sc_aperbase + MACH64_REG_OFF;
572 sc->sc_regphys = sc->sc_aperphys + MACH64_REG_OFF;
573 }
574
575 sc->sc_regt = sc->sc_memt;
576 bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
577 sc->sc_regsize, &sc->sc_regh);
578 sc->sc_registers = (char *)sc->sc_aperture + 0x7ffc00;
579
580 mach64_init(sc);
581
582 aprint_normal_dev(sc->sc_dev,
583 "%d MB aperture at 0x%08x, %d KB registers at 0x%08x\n",
584 (u_int)(sc->sc_apersize / (1024 * 1024)),
585 (u_int)sc->sc_aperphys, (u_int)(sc->sc_regsize / 1024),
586 (u_int)sc->sc_regphys);
587
588 printf("%s: %d KB ROM at 0x%08x\n", device_xname(sc->sc_dev),
589 (int)sc->sc_rom.vb_size >> 10, (uint32_t)sc->sc_rom.vb_base);
590
591 prop_dictionary_get_uint32(device_properties(self), "width", &width);
592 prop_dictionary_get_uint32(device_properties(self), "height", &height);
593
594 if ((edid_data = prop_dictionary_get(device_properties(self), "EDID"))
595 != NULL) {
596 struct edid_info ei;
597
598 sc->sc_edid_size = min(1024, prop_data_size(edid_data));
599 memset(sc->sc_edid_data, 0, sizeof(sc->sc_edid_data));
600 memcpy(sc->sc_edid_data, prop_data_data_nocopy(edid_data),
601 sc->sc_edid_size);
602
603 edid_parse(sc->sc_edid_data, &ei);
604
605 #ifdef MACHFB_DEBUG
606 edid_print(&ei);
607 #endif
608 }
609
610 is_gx = 0;
611 switch(mach64_chip_id) {
612 case PCI_PRODUCT_ATI_MACH64_GX:
613 case PCI_PRODUCT_ATI_MACH64_CX:
614 is_gx = 1;
615 case PCI_PRODUCT_ATI_MACH64_CT:
616 sc->has_dsp = 0;
617 break;
618 case PCI_PRODUCT_ATI_MACH64_VT:
619 case PCI_PRODUCT_ATI_RAGE_II:
620 if((mach64_chip_rev & 0x07) == 0) {
621 sc->has_dsp = 0;
622 break;
623 }
624 /* Otherwise fall through. */
625 default:
626 sc->has_dsp = 1;
627 }
628
629 memtype_names = is_gx ? mach64_gx_memtype_names : mach64_memtype_names;
630
631 sc->memsize = mach64_get_memsize(sc);
632 if (sc->memsize == 8192)
633 /* The last page is used as register aperture. */
634 sc->memsize -= 4;
635 if(is_gx)
636 sc->memtype = (regr(sc, CONFIG_STAT0) >> 3) & 0x07;
637 else
638 sc->memtype = regr(sc, CONFIG_STAT0) & 0x07;
639
640 /*
641 * XXX is there any way to calculate reference frequency from
642 * known values?
643 */
644 if ((mach64_chip_id == PCI_PRODUCT_ATI_RAGE_XL_PCI) ||
645 ((mach64_chip_id >= PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI) &&
646 (mach64_chip_id <= PCI_PRODUCT_ATI_RAGE_LT_PRO))) {
647 aprint_normal_dev(sc->sc_dev, "ref_freq=29.498MHz\n");
648 sc->ref_freq = 29498;
649 } else
650 sc->ref_freq = 14318;
651
652 reg = regr(sc, CLOCK_CNTL);
653 aprint_debug("CLOCK_CNTL: %08x\n", reg);
654 sc->sc_clock = reg & 3;
655 aprint_debug("using clock %d\n", sc->sc_clock);
656
657 sc->ref_div = regrb_pll(sc, PLL_REF_DIV);
658 aprint_debug("ref_div: %d\n", sc->ref_div);
659 sc->mclk_fb_div = regrb_pll(sc, MCLK_FB_DIV);
660 aprint_debug("mclk_fb_div: %d\n", sc->mclk_fb_div);
661 sc->mem_freq = (2 * sc->ref_freq * sc->mclk_fb_div) /
662 (sc->ref_div * 2);
663 sc->mclk_post_div = (sc->mclk_fb_div * 2 * sc->ref_freq) /
664 (sc->mem_freq * sc->ref_div);
665 sc->ramdac_freq = mach64_get_max_ramdac(sc);
666 aprint_normal_dev(sc->sc_dev,
667 "%ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz\n",
668 (u_long)sc->memsize,
669 memtype_names[sc->memtype],
670 sc->mem_freq / 1000, sc->mem_freq % 1000,
671 sc->ramdac_freq / 1000);
672
673 id = regr(sc, CONFIG_CHIP_ID) & 0xffff;
674 switch(mach64_chip_id) {
675 case PCI_PRODUCT_ATI_MACH64_GX:
676 expected_id = 0x00d7;
677 break;
678 case PCI_PRODUCT_ATI_MACH64_CX:
679 expected_id = 0x0057;
680 break;
681 default:
682 /* Most chip IDs match their PCI product ID. */
683 expected_id = mach64_chip_id;
684 }
685
686 if (id != expected_id) {
687 aprint_error_dev(sc->sc_dev,
688 "chip ID mismatch, 0x%x != 0x%x\n", id, expected_id);
689 return;
690 }
691
692 sc->sc_console = mach64_is_console(sc);
693 aprint_debug("gen_cntl: %08x\n", regr(sc, CRTC_GEN_CNTL));
694 #if defined(__sparc__) || defined(__powerpc__)
695 if (sc->sc_console) {
696 if (mode != NULL) {
697 memcpy(&default_mode, mode, sizeof(struct videomode));
698 setmode = 1;
699 } else {
700 mach64_get_mode(sc, &default_mode);
701 setmode = 0;
702 }
703 sc->sc_my_mode = &default_mode;
704 } else {
705 /* fill in default_mode if it's empty */
706 mach64_get_mode(sc, &default_mode);
707 if (default_mode.dot_clock == 0) {
708 memcpy(&default_mode, &mach64_modes[4],
709 sizeof(default_mode));
710 }
711 sc->sc_my_mode = &default_mode;
712 setmode = 1;
713 }
714 #else
715 if (default_mode.dot_clock == 0) {
716 memcpy(&default_mode, &mach64_modes[0],
717 sizeof(default_mode));
718 }
719 sc->sc_my_mode = &mach64_modes[0];
720 setmode = 1;
721 #endif
722
723 sc->bits_per_pixel = 8;
724 sc->virt_x = sc->sc_my_mode->hdisplay;
725 sc->virt_y = sc->sc_my_mode->vdisplay;
726 sc->max_x = sc->virt_x - 1;
727 sc->max_y = (sc->memsize * 1024) /
728 (sc->virt_x * (sc->bits_per_pixel / 8)) - 1;
729
730 sc->color_depth = CRTC_PIX_WIDTH_8BPP;
731
732 mach64_init_engine(sc);
733
734 if (setmode)
735 mach64_modeswitch(sc, sc->sc_my_mode);
736
737 aprint_normal_dev(sc->sc_dev,
738 "initial resolution %dx%d at %d bpp\n",
739 sc->sc_my_mode->hdisplay, sc->sc_my_mode->vdisplay,
740 sc->bits_per_pixel);
741
742 #ifdef __sparc__
743 machfb_fbattach(sc);
744 #endif
745
746 wsfont_init();
747
748 vcons_init(&sc->vd, sc, &mach64_defaultscreen, &sc->sc_accessops);
749 sc->vd.init_screen = mach64_init_screen;
750
751 if (sc->sc_console) {
752
753 vcons_init_screen(&sc->vd, &mach64_console_screen, 1,
754 &defattr);
755 mach64_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
756
757 ri = &mach64_console_screen.scr_ri;
758 mach64_defaultscreen.textops = &ri->ri_ops;
759 mach64_defaultscreen.capabilities = ri->ri_caps;
760 mach64_defaultscreen.nrows = ri->ri_rows;
761 mach64_defaultscreen.ncols = ri->ri_cols;
762
763 wsdisplay_cnattach(&mach64_defaultscreen, ri, 0, 0, defattr);
764 } else {
765 /*
766 * since we're not the console we can postpone the rest
767 * until someone actually allocates a screen for us
768 */
769 mach64_modeswitch(sc, sc->sc_my_mode);
770 if (mach64_console_screen.scr_ri.ri_rows == 0) {
771 /* do some minimal setup to avoid weirdnesses later */
772 vcons_init_screen(&sc->vd, &mach64_console_screen, 1,
773 &defattr);
774 }
775 }
776 sc->sc_bg = mach64_console_screen.scr_ri.ri_devcmap[WS_DEFAULT_BG];
777 mach64_clearscreen(sc);
778 mach64_init_lut(sc);
779
780 if (sc->sc_console)
781 vcons_replay_msgbuf(&mach64_console_screen);
782
783 machfb_blank(sc, 0); /* unblank the screen */
784
785 aa.console = sc->sc_console;
786 aa.scrdata = &mach64_screenlist;
787 aa.accessops = &sc->sc_accessops;
788 aa.accesscookie = &sc->vd;
789
790 config_found(self, &aa, wsemuldisplaydevprint);
791
792 config_found_ia(self, "drm", aux, machfb_drm_print);
793 }
794
795 static int
796 machfb_drm_print(void *aux, const char *pnp)
797 {
798 if (pnp)
799 aprint_normal("direct rendering for %s", pnp);
800 return (UNSUPP);
801 }
802
803 static void
804 mach64_init_screen(void *cookie, struct vcons_screen *scr, int existing,
805 long *defattr)
806 {
807 struct mach64_softc *sc = cookie;
808 struct rasops_info *ri = &scr->scr_ri;
809
810 /* XXX for now */
811 #define setmode 0
812
813 ri->ri_depth = sc->bits_per_pixel;
814 ri->ri_width = sc->sc_my_mode->hdisplay;
815 ri->ri_height = sc->sc_my_mode->vdisplay;
816 ri->ri_stride = ri->ri_width;
817 ri->ri_flg = RI_CENTER;
818 if (ri->ri_depth == 8)
819 ri->ri_flg |= RI_8BIT_IS_RGB/* | RI_ENABLE_ALPHA*/;
820 set_address(ri, sc->sc_aperture);
821
822 #ifdef VCONS_DRAW_INTR
823 scr->scr_flags |= VCONS_DONT_READ;
824 #endif
825
826 if (existing) {
827 if (setmode && mach64_set_screentype(sc, scr->scr_type)) {
828 panic("%s: failed to switch video mode",
829 device_xname(sc->sc_dev));
830 }
831 }
832
833 rasops_init(ri, 0, 0);
834 ri->ri_caps = WSSCREEN_WSCOLORS;
835 rasops_reconfig(ri, sc->sc_my_mode->vdisplay / ri->ri_font->fontheight,
836 sc->sc_my_mode->hdisplay / ri->ri_font->fontwidth);
837
838 /* enable acceleration */
839 ri->ri_hw = scr;
840 ri->ri_ops.copyrows = mach64_copyrows;
841 ri->ri_ops.copycols = mach64_copycols;
842 ri->ri_ops.eraserows = mach64_eraserows;
843 ri->ri_ops.erasecols = mach64_erasecols;
844 ri->ri_ops.cursor = mach64_cursor;
845 ri->ri_ops.putchar = mach64_putchar;
846 }
847
848 static void
849 mach64_init(struct mach64_softc *sc)
850 {
851 uint32_t *p32, saved_value;
852 uint8_t *p;
853 int need_swap;
854
855 /*
856 * Test whether the aperture is byte swapped or not
857 */
858 p32 = (uint32_t*)sc->sc_aperture;
859 saved_value = *p32;
860 p = (uint8_t*)(u_long)sc->sc_aperture;
861 *p32 = 0x12345678;
862 if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
863 need_swap = 0;
864 else
865 need_swap = 1;
866 if (need_swap) {
867 sc->sc_aperture = (char *)sc->sc_aperture + 0x800000;
868 #if 0
869 /* what the fsck is this for? */
870 sc->sc_aperbase += 0x800000;
871 sc->sc_apersize -= 0x800000;
872 #endif
873 }
874 *p32 = saved_value;
875
876 sc->sc_blanked = 0;
877 }
878
879 static int
880 mach64_get_memsize(struct mach64_softc *sc)
881 {
882 int tmp, memsize;
883 int mem_tab[] = {
884 512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
885 };
886 tmp = regr(sc, MEM_CNTL);
887 #ifdef DIAGNOSTIC
888 aprint_debug_dev(sc->sc_dev, "memctl %08x\n", tmp);
889 #endif
890 if (sc->has_dsp) {
891 tmp &= 0x0000000f;
892 if (tmp < 8)
893 memsize = (tmp + 1) * 512;
894 else if (tmp < 12)
895 memsize = (tmp - 3) * 1024;
896 else
897 memsize = (tmp - 7) * 2048;
898 } else {
899 memsize = mem_tab[tmp & 0x07];
900 }
901
902 return memsize;
903 }
904
905 static int
906 mach64_get_max_ramdac(struct mach64_softc *sc)
907 {
908 int i;
909
910 if ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
911 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
912 (mach64_chip_rev & 0x07))
913 return 170000;
914
915 for (i = 0; i < __arraycount(mach64_info); i++)
916 if (mach64_chip_id == mach64_info[i].chip_id)
917 return mach64_info[i].ramdac_freq;
918
919 if (sc->bits_per_pixel == 8)
920 return 135000;
921 else
922 return 80000;
923 }
924
925 #if defined(__sparc__) || defined(__powerpc__)
926 static void
927 mach64_get_mode(struct mach64_softc *sc, struct videomode *mode)
928 {
929 struct mach64_crtcregs crtc;
930
931 crtc.h_total_disp = regr(sc, CRTC_H_TOTAL_DISP);
932 crtc.h_sync_strt_wid = regr(sc, CRTC_H_SYNC_STRT_WID);
933 crtc.v_total_disp = regr(sc, CRTC_V_TOTAL_DISP);
934 crtc.v_sync_strt_wid = regr(sc, CRTC_V_SYNC_STRT_WID);
935
936 mode->htotal = ((crtc.h_total_disp & 0xffff) + 1) << 3;
937 mode->hdisplay = ((crtc.h_total_disp >> 16) + 1) << 3;
938 mode->hsync_start = ((crtc.h_sync_strt_wid & 0xffff) + 1) << 3;
939 mode->hsync_end = ((crtc.h_sync_strt_wid >> 16) << 3) +
940 mode->hsync_start;
941 mode->vtotal = (crtc.v_total_disp & 0xffff) + 1;
942 mode->vdisplay = (crtc.v_total_disp >> 16) + 1;
943 mode->vsync_start = (crtc.v_sync_strt_wid & 0xffff) + 1;
944 mode->vsync_end = (crtc.v_sync_strt_wid >> 16) + mode->vsync_start;
945
946 #ifdef MACHFB_DEBUG
947 printf("mach64_get_mode: %d %d %d %d %d %d %d %d\n",
948 mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
949 mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
950 #endif
951 }
952 #endif
953
954 static int
955 mach64_calc_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc,
956 struct videomode *mode)
957 {
958
959 if (mode->dot_clock > sc->ramdac_freq)
960 /* Clock too high. */
961 return 1;
962
963 crtc->h_total_disp = (((mode->hdisplay >> 3) - 1) << 16) |
964 ((mode->htotal >> 3) - 1);
965 crtc->h_sync_strt_wid =
966 (((mode->hsync_end - mode->hsync_start) >> 3) << 16) |
967 ((mode->hsync_start >> 3) - 1);
968
969 crtc->v_total_disp = ((mode->vdisplay - 1) << 16) |
970 (mode->vtotal - 1);
971 crtc->v_sync_strt_wid =
972 ((mode->vsync_end - mode->vsync_start) << 16) |
973 (mode->vsync_start - 1);
974
975 if (mode->flags & VID_NVSYNC)
976 crtc->v_sync_strt_wid |= CRTC_VSYNC_NEG;
977
978 switch (sc->bits_per_pixel) {
979 case 8:
980 crtc->color_depth = CRTC_PIX_WIDTH_8BPP;
981 break;
982 case 16:
983 crtc->color_depth = CRTC_PIX_WIDTH_16BPP;
984 break;
985 case 32:
986 crtc->color_depth = CRTC_PIX_WIDTH_32BPP;
987 break;
988 }
989
990 crtc->gen_cntl = 0;
991 if (mode->flags & VID_INTERLACE)
992 crtc->gen_cntl |= CRTC_INTERLACE_EN;
993
994 if (mode->flags & VID_CSYNC)
995 crtc->gen_cntl |= CRTC_CSYNC_EN;
996
997 crtc->dot_clock = mode->dot_clock;
998
999 return 0;
1000 }
1001
1002 static void
1003 mach64_set_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc)
1004 {
1005
1006 mach64_set_pll(sc, crtc->dot_clock);
1007
1008 if (sc->has_dsp)
1009 mach64_set_dsp(sc);
1010
1011 regw(sc, CRTC_H_TOTAL_DISP, crtc->h_total_disp);
1012 regw(sc, CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
1013 regw(sc, CRTC_V_TOTAL_DISP, crtc->v_total_disp);
1014 regw(sc, CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
1015
1016 regw(sc, CRTC_VLINE_CRNT_VLINE, 0);
1017
1018 regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
1019
1020 regw(sc, CRTC_GEN_CNTL, crtc->gen_cntl | crtc->color_depth |
1021 /* XXX this unconditionally enables composite sync on SPARC */
1022 #ifdef __sparc__
1023 CRTC_CSYNC_EN |
1024 #endif
1025 CRTC_EXT_DISP_EN | CRTC_EXT_EN);
1026 }
1027
1028 static int
1029 mach64_modeswitch(struct mach64_softc *sc, struct videomode *mode)
1030 {
1031 struct mach64_crtcregs crtc;
1032
1033 memset(&crtc, 0, sizeof crtc); /* XXX gcc */
1034
1035 if (mach64_calc_crtcregs(sc, &crtc, mode))
1036 return 1;
1037 aprint_debug("crtc dot clock: %d\n", crtc.dot_clock);
1038 if (crtc.dot_clock == 0) {
1039 aprint_error("%s: preposterous dot clock (%d)\n",
1040 device_xname(sc->sc_dev), crtc.dot_clock);
1041 return 1;
1042 }
1043 mach64_set_crtcregs(sc, &crtc);
1044 return 0;
1045 }
1046
1047 static void
1048 mach64_reset_engine(struct mach64_softc *sc)
1049 {
1050
1051 /* Reset engine.*/
1052 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1053
1054 /* Enable engine. */
1055 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1056
1057 /* Ensure engine is not locked up by clearing any FIFO or
1058 host errors. */
1059 regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1060 BUS_FIFO_ERR_ACK);
1061 }
1062
1063 static void
1064 mach64_init_engine(struct mach64_softc *sc)
1065 {
1066 uint32_t pitch_value;
1067
1068 pitch_value = sc->virt_x;
1069
1070 if (sc->bits_per_pixel == 24)
1071 pitch_value *= 3;
1072
1073 mach64_reset_engine(sc);
1074
1075 wait_for_fifo(sc, 14);
1076
1077 regw(sc, CONTEXT_MASK, 0xffffffff);
1078
1079 regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1080
1081 /* make sure the visible area starts where we're going to draw */
1082 regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
1083
1084 regw(sc, DST_Y_X, 0);
1085 regw(sc, DST_HEIGHT, 0);
1086 regw(sc, DST_BRES_ERR, 0);
1087 regw(sc, DST_BRES_INC, 0);
1088 regw(sc, DST_BRES_DEC, 0);
1089
1090 regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1091 DST_Y_TOP_TO_BOTTOM);
1092
1093 regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1094
1095 regw(sc, SRC_Y_X, 0);
1096 regw(sc, SRC_HEIGHT1_WIDTH1, 1);
1097 regw(sc, SRC_Y_X_START, 0);
1098 regw(sc, SRC_HEIGHT2_WIDTH2, 1);
1099
1100 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1101
1102 wait_for_fifo(sc, 13);
1103 regw(sc, HOST_CNTL, 0);
1104
1105 regw(sc, PAT_REG0, 0);
1106 regw(sc, PAT_REG1, 0);
1107 regw(sc, PAT_CNTL, 0);
1108
1109 regw(sc, SC_LEFT, 0);
1110 regw(sc, SC_TOP, 0);
1111 regw(sc, SC_BOTTOM, sc->sc_my_mode->vdisplay - 1);
1112 regw(sc, SC_RIGHT, pitch_value - 1);
1113
1114 regw(sc, DP_BKGD_CLR, WS_DEFAULT_BG);
1115 regw(sc, DP_FRGD_CLR, WS_DEFAULT_FG);
1116 regw(sc, DP_WRITE_MASK, 0xffffffff);
1117 regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
1118
1119 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1120
1121 wait_for_fifo(sc, 3);
1122 regw(sc, CLR_CMP_CLR, 0);
1123 regw(sc, CLR_CMP_MASK, 0xffffffff);
1124 regw(sc, CLR_CMP_CNTL, 0);
1125
1126 wait_for_fifo(sc, 2);
1127 switch (sc->bits_per_pixel) {
1128 case 8:
1129 regw(sc, DP_PIX_WIDTH, HOST_1BPP | SRC_8BPP | DST_8BPP);
1130 regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
1131 /* We want 8 bit per channel */
1132 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1133 break;
1134 case 32:
1135 regw(sc, DP_PIX_WIDTH, HOST_1BPP | SRC_32BPP | DST_32BPP);
1136 regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
1137 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1138 break;
1139 }
1140
1141 wait_for_fifo(sc, 5);
1142 regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
1143 regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1144
1145 wait_for_idle(sc);
1146 }
1147
1148 #if 0
1149 static void
1150 mach64_adjust_frame(struct mach64_softc *sc, int x, int y)
1151 {
1152 int offset;
1153
1154 offset = ((x + y * sc->virt_x) * (sc->bits_per_pixel >> 3)) >> 3;
1155
1156 regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1157 offset);
1158 }
1159 #endif
1160
1161 static void
1162 mach64_set_dsp(struct mach64_softc *sc)
1163 {
1164 uint32_t fifo_depth, page_size, dsp_precision, dsp_loop_latency;
1165 uint32_t dsp_off, dsp_on, dsp_xclks_per_qw;
1166 uint32_t xclks_per_qw, y;
1167 uint32_t fifo_off, fifo_on;
1168
1169 aprint_normal_dev(sc->sc_dev, "initializing the DSP\n");
1170
1171 if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
1172 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II ||
1173 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIP ||
1174 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_PCI ||
1175 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_B ||
1176 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_P) {
1177 dsp_loop_latency = 0;
1178 fifo_depth = 24;
1179 } else {
1180 dsp_loop_latency = 2;
1181 fifo_depth = 32;
1182 }
1183
1184 dsp_precision = 0;
1185 xclks_per_qw = (sc->mclk_fb_div * sc->vclk_post_div * 64 << 11) /
1186 (sc->vclk_fb_div * sc->mclk_post_div * sc->bits_per_pixel);
1187 y = (xclks_per_qw * fifo_depth) >> 11;
1188 while (y) {
1189 y >>= 1;
1190 dsp_precision++;
1191 }
1192 dsp_precision -= 5;
1193 fifo_off = ((xclks_per_qw * (fifo_depth - 1)) >> 5) + (3 << 6);
1194
1195 switch (sc->memtype) {
1196 case DRAM:
1197 case EDO_DRAM:
1198 case PSEUDO_EDO:
1199 if (sc->memsize > 1024) {
1200 page_size = 9;
1201 dsp_loop_latency += 6;
1202 } else {
1203 page_size = 10;
1204 if (sc->memtype == DRAM)
1205 dsp_loop_latency += 8;
1206 else
1207 dsp_loop_latency += 7;
1208 }
1209 break;
1210 case SDRAM:
1211 case SGRAM:
1212 if (sc->memsize > 1024) {
1213 page_size = 8;
1214 dsp_loop_latency += 8;
1215 } else {
1216 page_size = 10;
1217 dsp_loop_latency += 9;
1218 }
1219 break;
1220 default:
1221 page_size = 10;
1222 dsp_loop_latency += 9;
1223 break;
1224 }
1225
1226 if (xclks_per_qw >= (page_size << 11))
1227 fifo_on = ((2 * page_size + 1) << 6) + (xclks_per_qw >> 5);
1228 else
1229 fifo_on = (3 * page_size + 2) << 6;
1230
1231 dsp_xclks_per_qw = xclks_per_qw >> dsp_precision;
1232 dsp_on = fifo_on >> dsp_precision;
1233 dsp_off = fifo_off >> dsp_precision;
1234
1235 #ifdef MACHFB_DEBUG
1236 printf("dsp_xclks_per_qw = %d, dsp_on = %d, dsp_off = %d,\n"
1237 "dsp_precision = %d, dsp_loop_latency = %d,\n"
1238 "mclk_fb_div = %d, vclk_fb_div = %d,\n"
1239 "mclk_post_div = %d, vclk_post_div = %d\n",
1240 dsp_xclks_per_qw, dsp_on, dsp_off, dsp_precision, dsp_loop_latency,
1241 sc->mclk_fb_div, sc->vclk_fb_div,
1242 sc->mclk_post_div, sc->vclk_post_div);
1243 #endif
1244
1245 regw(sc, DSP_ON_OFF, ((dsp_on << 16) & DSP_ON) | (dsp_off & DSP_OFF));
1246 regw(sc, DSP_CONFIG, ((dsp_precision << 20) & DSP_PRECISION) |
1247 ((dsp_loop_latency << 16) & DSP_LOOP_LATENCY) |
1248 (dsp_xclks_per_qw & DSP_XCLKS_PER_QW));
1249 }
1250
1251 static void
1252 mach64_set_pll(struct mach64_softc *sc, int clock)
1253 {
1254 uint32_t q, clockreg;
1255 int clockshift = sc->sc_clock << 1;
1256 uint8_t reg, vclk_ctl;
1257
1258 q = (clock * sc->ref_div * 100) / (2 * sc->ref_freq);
1259 #ifdef MACHFB_DEBUG
1260 printf("q = %d\n", q);
1261 #endif
1262 if (q > 25500) {
1263 aprint_error_dev(sc->sc_dev, "Warning: q > 25500\n");
1264 q = 25500;
1265 sc->vclk_post_div = 1;
1266 sc->log2_vclk_post_div = 0;
1267 } else if (q > 12750) {
1268 sc->vclk_post_div = 1;
1269 sc->log2_vclk_post_div = 0;
1270 } else if (q > 6350) {
1271 sc->vclk_post_div = 2;
1272 sc->log2_vclk_post_div = 1;
1273 } else if (q > 3150) {
1274 sc->vclk_post_div = 4;
1275 sc->log2_vclk_post_div = 2;
1276 } else if (q >= 1600) {
1277 sc->vclk_post_div = 8;
1278 sc->log2_vclk_post_div = 3;
1279 } else {
1280 aprint_error_dev(sc->sc_dev, "Warning: q < 1600\n");
1281 sc->vclk_post_div = 8;
1282 sc->log2_vclk_post_div = 3;
1283 }
1284 sc->vclk_fb_div = q * sc->vclk_post_div / 100;
1285 aprint_debug("post_div: %d log2_post_div: %d mclk_div: %d\n",
1286 sc->vclk_post_div, sc->log2_vclk_post_div, sc->mclk_fb_div);
1287
1288 vclk_ctl = regrb_pll(sc, PLL_VCLK_CNTL);
1289 aprint_debug("vclk_ctl: %02x\n", vclk_ctl);
1290 vclk_ctl |= PLL_VCLK_RESET;
1291 regwb_pll(sc, PLL_VCLK_CNTL, vclk_ctl);
1292
1293 regwb_pll(sc, MCLK_FB_DIV, sc->mclk_fb_div);
1294 reg = regrb_pll(sc, VCLK_POST_DIV);
1295 reg &= ~(3 << clockshift);
1296 reg |= (sc->log2_vclk_post_div << clockshift);
1297 regwb_pll(sc, VCLK_POST_DIV, reg);
1298 regwb_pll(sc, VCLK0_FB_DIV + sc->sc_clock, sc->vclk_fb_div);
1299
1300 vclk_ctl &= ~PLL_VCLK_RESET;
1301 regwb_pll(sc, PLL_VCLK_CNTL, vclk_ctl);
1302
1303 clockreg = regr(sc, CLOCK_CNTL);
1304 clockreg &= ~CLOCK_SEL;
1305 clockreg |= sc->sc_clock | CLOCK_STROBE;
1306 regw(sc, CLOCK_CNTL, clockreg);
1307 }
1308
1309 static void
1310 mach64_init_lut(struct mach64_softc *sc)
1311 {
1312 uint8_t cmap[768];
1313 int i, idx;
1314
1315 rasops_get_cmap(&mach64_console_screen.scr_ri, cmap, sizeof(cmap));
1316 idx = 0;
1317 for (i = 0; i < 256; i++) {
1318 mach64_putpalreg(sc, i, cmap[idx], cmap[idx + 1],
1319 cmap[idx + 2]);
1320 idx += 3;
1321 }
1322 }
1323
1324 static int
1325 mach64_putpalreg(struct mach64_softc *sc, uint8_t index, uint8_t r, uint8_t g,
1326 uint8_t b)
1327 {
1328 sc->sc_cmap_red[index] = r;
1329 sc->sc_cmap_green[index] = g;
1330 sc->sc_cmap_blue[index] = b;
1331 /*
1332 * writing the dac index takes a while, in theory we can poll some
1333 * register to see when it's ready - but we better avoid writing it
1334 * unnecessarily
1335 */
1336 if (index != sc->sc_dacw) {
1337 regwb(sc, DAC_MASK, 0xff);
1338 regwb(sc, DAC_WINDEX, index);
1339 }
1340 sc->sc_dacw = index + 1;
1341 regwb(sc, DAC_DATA, r);
1342 regwb(sc, DAC_DATA, g);
1343 regwb(sc, DAC_DATA, b);
1344 return 0;
1345 }
1346
1347 static int
1348 mach64_putcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
1349 {
1350 uint index = cm->index;
1351 uint count = cm->count;
1352 int i, error;
1353 uint8_t rbuf[256], gbuf[256], bbuf[256];
1354 uint8_t *r, *g, *b;
1355
1356 if (cm->index >= 256 || cm->count > 256 ||
1357 (cm->index + cm->count) > 256)
1358 return EINVAL;
1359 error = copyin(cm->red, &rbuf[index], count);
1360 if (error)
1361 return error;
1362 error = copyin(cm->green, &gbuf[index], count);
1363 if (error)
1364 return error;
1365 error = copyin(cm->blue, &bbuf[index], count);
1366 if (error)
1367 return error;
1368
1369 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
1370 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
1371 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
1372
1373 r = &sc->sc_cmap_red[index];
1374 g = &sc->sc_cmap_green[index];
1375 b = &sc->sc_cmap_blue[index];
1376
1377 for (i = 0; i < count; i++) {
1378 mach64_putpalreg(sc, index, *r, *g, *b);
1379 index++;
1380 r++, g++, b++;
1381 }
1382 return 0;
1383 }
1384
1385 static int
1386 mach64_getcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
1387 {
1388 u_int index = cm->index;
1389 u_int count = cm->count;
1390 int error;
1391
1392 if (index >= 255 || count > 256 || index + count > 256)
1393 return EINVAL;
1394
1395 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
1396 if (error)
1397 return error;
1398 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1399 if (error)
1400 return error;
1401 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
1402 if (error)
1403 return error;
1404
1405 return 0;
1406 }
1407
1408 static int
1409 mach64_set_screentype(struct mach64_softc *sc, const struct wsscreen_descr *des)
1410 {
1411 struct mach64_crtcregs regs;
1412
1413 if (mach64_calc_crtcregs(sc, ®s,
1414 (struct videomode *)des->modecookie))
1415 return 1;
1416
1417 mach64_set_crtcregs(sc, ®s);
1418 return 0;
1419 }
1420
1421 static int
1422 mach64_is_console(struct mach64_softc *sc)
1423 {
1424 bool console = 0;
1425
1426 prop_dictionary_get_bool(device_properties(sc->sc_dev),
1427 "is_console", &console);
1428 return console;
1429 }
1430
1431 /*
1432 * wsdisplay_emulops
1433 */
1434
1435 static void
1436 mach64_cursor(void *cookie, int on, int row, int col)
1437 {
1438 struct rasops_info *ri = cookie;
1439 struct vcons_screen *scr = ri->ri_hw;
1440 struct mach64_softc *sc = scr->scr_cookie;
1441 int x, y, wi, he;
1442
1443 wi = ri->ri_font->fontwidth;
1444 he = ri->ri_font->fontheight;
1445
1446 if ((!sc->sc_locked) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1447 x = ri->ri_ccol * wi + ri->ri_xorigin;
1448 y = ri->ri_crow * he + ri->ri_yorigin;
1449 if (ri->ri_flg & RI_CURSOR) {
1450 mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC,
1451 0xff);
1452 ri->ri_flg &= ~RI_CURSOR;
1453 }
1454 ri->ri_crow = row;
1455 ri->ri_ccol = col;
1456 if (on) {
1457 x = ri->ri_ccol * wi + ri->ri_xorigin;
1458 y = ri->ri_crow * he + ri->ri_yorigin;
1459 mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC,
1460 0xff);
1461 ri->ri_flg |= RI_CURSOR;
1462 }
1463 } else {
1464 scr->scr_ri.ri_crow = row;
1465 scr->scr_ri.ri_ccol = col;
1466 scr->scr_ri.ri_flg &= ~RI_CURSOR;
1467 }
1468 }
1469
1470 #if 0
1471 static int
1472 mach64_mapchar(void *cookie, int uni, u_int *index)
1473 {
1474 return 0;
1475 }
1476 #endif
1477
1478 static void
1479 mach64_putchar(void *cookie, int row, int col, u_int c, long attr)
1480 {
1481 struct rasops_info *ri = cookie;
1482 struct wsdisplay_font *font = PICK_FONT(ri, c);
1483 struct vcons_screen *scr = ri->ri_hw;
1484 struct mach64_softc *sc = scr->scr_cookie;
1485
1486 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1487 int fg, bg, uc;
1488 uint8_t *data;
1489 int x, y, wi, he;
1490 wi = font->fontwidth;
1491 he = font->fontheight;
1492
1493 if (!CHAR_IN_FONT(c, font))
1494 return;
1495 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0x0f];
1496 fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0x0f];
1497 x = ri->ri_xorigin + col * wi;
1498 y = ri->ri_yorigin + row * he;
1499 if (c == 0x20) {
1500 mach64_rectfill(sc, x, y, wi, he, bg);
1501 } else {
1502 uc = c - font->firstchar;
1503 data = (uint8_t *)font->data + uc *
1504 ri->ri_fontscale;
1505
1506 mach64_setup_mono(sc, x, y, wi, he, fg, bg);
1507 mach64_feed_bytes(sc, ri->ri_fontscale, data);
1508 }
1509 }
1510 }
1511
1512
1513 static void
1514 mach64_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1515 {
1516 struct rasops_info *ri = cookie;
1517 struct vcons_screen *scr = ri->ri_hw;
1518 struct mach64_softc *sc = scr->scr_cookie;
1519 int32_t xs, xd, y, width, height;
1520
1521 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1522 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1523 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1524 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1525 width = ri->ri_font->fontwidth * ncols;
1526 height = ri->ri_font->fontheight;
1527 mach64_bitblt(sc, xs, y, xd, y, width, height, MIX_SRC, 0xff);
1528 }
1529 }
1530
1531 static void
1532 mach64_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1533 {
1534 struct rasops_info *ri = cookie;
1535 struct vcons_screen *scr = ri->ri_hw;
1536 struct mach64_softc *sc = scr->scr_cookie;
1537 int32_t x, y, width, height, fg, bg, ul;
1538
1539 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1540 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1541 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1542 width = ri->ri_font->fontwidth * ncols;
1543 height = ri->ri_font->fontheight;
1544 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1545
1546 mach64_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1547 }
1548 }
1549
1550 static void
1551 mach64_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1552 {
1553 struct rasops_info *ri = cookie;
1554 struct vcons_screen *scr = ri->ri_hw;
1555 struct mach64_softc *sc = scr->scr_cookie;
1556 int32_t x, ys, yd, width, height;
1557
1558 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1559 x = ri->ri_xorigin;
1560 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1561 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1562 width = ri->ri_emuwidth;
1563 height = ri->ri_font->fontheight*nrows;
1564 mach64_bitblt(sc, x, ys, x, yd, width, height, MIX_SRC, 0xff);
1565 }
1566 }
1567
1568 static void
1569 mach64_eraserows(void *cookie, int row, int nrows, long fillattr)
1570 {
1571 struct rasops_info *ri = cookie;
1572 struct vcons_screen *scr = ri->ri_hw;
1573 struct mach64_softc *sc = scr->scr_cookie;
1574 int32_t x, y, width, height, fg, bg, ul;
1575
1576 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1577 x = ri->ri_xorigin;
1578 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1579 width = ri->ri_emuwidth;
1580 height = ri->ri_font->fontheight * nrows;
1581 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1582
1583 mach64_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1584 }
1585 }
1586
1587 static void
1588 mach64_bitblt(struct mach64_softc *sc, int xs, int ys, int xd, int yd, int width, int height, int rop, int mask)
1589 {
1590 uint32_t dest_ctl = 0;
1591
1592 wait_for_idle(sc);
1593 regw(sc, DP_WRITE_MASK, mask); /* XXX only good for 8 bit */
1594 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
1595 regw(sc, DP_SRC, FRGD_SRC_BLIT);
1596 regw(sc, DP_MIX, (rop & 0xffff) << 16);
1597 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
1598 if (yd < ys) {
1599 dest_ctl = DST_Y_TOP_TO_BOTTOM;
1600 } else {
1601 ys += height - 1;
1602 yd += height - 1;
1603 dest_ctl = DST_Y_BOTTOM_TO_TOP;
1604 }
1605 if (xd < xs) {
1606 dest_ctl |= DST_X_LEFT_TO_RIGHT;
1607 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1608 } else {
1609 dest_ctl |= DST_X_RIGHT_TO_LEFT;
1610 xs += width - 1;
1611 xd += width - 1;
1612 regw(sc, SRC_CNTL, SRC_LINE_X_RIGHT_TO_LEFT);
1613 }
1614 regw(sc, DST_CNTL, dest_ctl);
1615
1616 regw(sc, SRC_Y_X, (xs << 16) | ys);
1617 regw(sc, SRC_WIDTH1, width);
1618 regw(sc, DST_Y_X, (xd << 16) | yd);
1619 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1620 }
1621
1622 static void
1623 mach64_setup_mono(struct mach64_softc *sc, int xd, int yd, int width,
1624 int height, uint32_t fg, uint32_t bg)
1625 {
1626 wait_for_idle(sc);
1627 regw(sc, DP_WRITE_MASK, 0xff); /* XXX only good for 8 bit */
1628 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
1629 regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR);
1630 regw(sc, DP_MIX, ((MIX_SRC & 0xffff) << 16) | MIX_SRC);
1631 regw(sc, CLR_CMP_CNTL ,0); /* no transparency */
1632 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1633 regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
1634 regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
1635 regw(sc, DP_BKGD_CLR, bg);
1636 regw(sc, DP_FRGD_CLR, fg);
1637 regw(sc, SRC_Y_X, 0);
1638 regw(sc, SRC_WIDTH1, width);
1639 regw(sc, DST_Y_X, (xd << 16) | yd);
1640 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1641 /* now feed the data into the chip */
1642 }
1643
1644 static void
1645 mach64_feed_bytes(struct mach64_softc *sc, int count, uint8_t *data)
1646 {
1647 int i;
1648 uint32_t latch = 0, bork;
1649 int shift = 0;
1650 int reg = 0;
1651
1652 for (i = 0; i < count; i++) {
1653 bork = data[i];
1654 latch |= (bork << shift);
1655 if (shift == 24) {
1656 regw(sc, HOST_DATA0 + reg, latch);
1657 latch = 0;
1658 shift = 0;
1659 reg = (reg + 4) & 0x3c;
1660 } else
1661 shift += 8;
1662 }
1663 if (shift != 0) /* 24 */
1664 regw(sc, HOST_DATA0 + reg, latch);
1665 }
1666
1667
1668 static void
1669 mach64_rectfill(struct mach64_softc *sc, int x, int y, int width, int height,
1670 int colour)
1671 {
1672 wait_for_idle(sc);
1673 regw(sc, DP_WRITE_MASK, 0xff);
1674 regw(sc, DP_FRGD_CLR, colour);
1675 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
1676 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1677 regw(sc, DP_MIX, MIX_SRC << 16);
1678 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
1679 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1680 regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1681
1682 regw(sc, SRC_Y_X, (x << 16) | y);
1683 regw(sc, SRC_WIDTH1, width);
1684 regw(sc, DST_Y_X, (x << 16) | y);
1685 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1686 }
1687
1688 static void
1689 mach64_clearscreen(struct mach64_softc *sc)
1690 {
1691 mach64_rectfill(sc, 0, 0, sc->virt_x, sc->virt_y, sc->sc_bg);
1692 }
1693
1694
1695 #if 0
1696 static void
1697 mach64_showpal(struct mach64_softc *sc)
1698 {
1699 int i, x = 0;
1700
1701 for (i = 0; i < 16; i++) {
1702 mach64_rectfill(sc, x, 0, 64, 64, i);
1703 x += 64;
1704 }
1705 }
1706 #endif
1707
1708 /*
1709 * wsdisplay_accessops
1710 */
1711
1712 static int
1713 mach64_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1714 struct lwp *l)
1715 {
1716 struct vcons_data *vd = v;
1717 struct mach64_softc *sc = vd->cookie;
1718 struct wsdisplay_fbinfo *wdf;
1719 struct vcons_screen *ms = vd->active;
1720
1721 switch (cmd) {
1722 case WSDISPLAYIO_GTYPE:
1723 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
1724 return 0;
1725
1726 case WSDISPLAYIO_LINEBYTES:
1727 *(u_int *)data = sc->virt_x * sc->bits_per_pixel / 8;
1728 return 0;
1729
1730 case WSDISPLAYIO_GINFO:
1731 wdf = (void *)data;
1732 wdf->height = sc->virt_y;
1733 wdf->width = sc->virt_x;
1734 wdf->depth = sc->bits_per_pixel;
1735 wdf->cmsize = 256;
1736 return 0;
1737
1738 case WSDISPLAYIO_GETCMAP:
1739 return mach64_getcmap(sc,
1740 (struct wsdisplay_cmap *)data);
1741
1742 case WSDISPLAYIO_PUTCMAP:
1743 return mach64_putcmap(sc,
1744 (struct wsdisplay_cmap *)data);
1745
1746 /* PCI config read/write passthrough. */
1747 case PCI_IOC_CFGREAD:
1748 case PCI_IOC_CFGWRITE:
1749 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
1750 cmd, data, flag, l);
1751
1752 case WSDISPLAYIO_GET_BUSID:
1753 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
1754 sc->sc_pcitag, data);
1755
1756 case WSDISPLAYIO_SMODE: {
1757 int new_mode = *(int*)data;
1758 if (new_mode != sc->sc_mode) {
1759 sc->sc_mode = new_mode;
1760 if ((new_mode == WSDISPLAYIO_MODE_EMUL)
1761 && (ms != NULL))
1762 {
1763 /* restore initial video mode */
1764 mach64_init(sc);
1765 mach64_init_engine(sc);
1766 mach64_init_lut(sc);
1767 mach64_modeswitch(sc, sc->sc_my_mode);
1768 vcons_redraw_screen(ms);
1769 }
1770 }
1771 }
1772 return 0;
1773 case WSDISPLAYIO_GET_EDID: {
1774 struct wsdisplayio_edid_info *d = data;
1775 return wsdisplayio_get_edid(sc->sc_dev, d);
1776 }
1777 }
1778 return EPASSTHROUGH;
1779 }
1780
1781 static paddr_t
1782 mach64_mmap(void *v, void *vs, off_t offset, int prot)
1783 {
1784 struct vcons_data *vd = v;
1785 struct mach64_softc *sc = vd->cookie;
1786 paddr_t pa;
1787 pcireg_t reg;
1788
1789 #ifndef __sparc64__
1790 /*
1791 *'regular' framebuffer mmap()ing
1792 * disabled on sparc64 because some ATI firmware likes to map some PCI
1793 * resources to addresses that would collide with this ( like some Rage
1794 * IIc which uses 0x2000 for the 2nd register block )
1795 * Other 64bit architectures might run into similar problems.
1796 */
1797 if (offset<sc->sc_apersize) {
1798 pa = bus_space_mmap(sc->sc_memt, sc->sc_aperbase, offset,
1799 prot, BUS_SPACE_MAP_LINEAR);
1800 return pa;
1801 }
1802 #endif
1803
1804 /*
1805 * restrict all other mappings to processes with superuser privileges
1806 * or the kernel itself
1807 */
1808 if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
1809 NULL, NULL, NULL, NULL) != 0) {
1810 return -1;
1811 }
1812
1813 reg = (pci_conf_read(sc->sc_pc, sc->sc_pcitag, 0x18) & 0xffffff00);
1814 if (reg != sc->sc_regphys) {
1815 #ifdef DIAGNOSTIC
1816 printf("%s: BAR 0x18 changed! (%x %x)\n",
1817 device_xname(sc->sc_dev), (uint32_t)sc->sc_regphys,
1818 (uint32_t)reg);
1819 #endif
1820 sc->sc_regphys = reg;
1821 }
1822
1823 reg = (pci_conf_read(sc->sc_pc, sc->sc_pcitag, 0x10) & 0xffffff00);
1824 if (reg != sc->sc_aperphys) {
1825 #ifdef DIAGNOSTIC
1826 printf("%s: BAR 0x10 changed! (%x %x)\n",
1827 device_xname(sc->sc_dev), (uint32_t)sc->sc_aperphys,
1828 (uint32_t)reg);
1829 #endif
1830 sc->sc_aperphys = reg;
1831 }
1832
1833 if ((offset >= sc->sc_aperphys) &&
1834 (offset < (sc->sc_aperphys + sc->sc_apersize))) {
1835 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1836 BUS_SPACE_MAP_LINEAR);
1837 return pa;
1838 }
1839
1840 if ((offset >= sc->sc_regphys) &&
1841 (offset < (sc->sc_regphys + sc->sc_regsize))) {
1842 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1843 BUS_SPACE_MAP_LINEAR);
1844 return pa;
1845 }
1846
1847 if ((offset >= sc->sc_rom.vb_base) &&
1848 (offset < (sc->sc_rom.vb_base + sc->sc_rom.vb_size))) {
1849 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1850 BUS_SPACE_MAP_LINEAR);
1851 return pa;
1852 }
1853
1854 #ifdef PCI_MAGIC_IO_RANGE
1855 if ((offset >= PCI_MAGIC_IO_RANGE) &&
1856 (offset <= PCI_MAGIC_IO_RANGE + 0x10000)) {
1857 return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
1858 0, prot, BUS_SPACE_MAP_LINEAR);
1859 }
1860 #endif
1861
1862 return -1;
1863 }
1864
1865 /* set ri->ri_bits according to fb, ri_xorigin and ri_yorigin */
1866 static void
1867 set_address(struct rasops_info *ri, void *fb)
1868 {
1869 #ifdef notdef
1870 printf(" %d %d %d\n", ri->ri_xorigin, ri->ri_yorigin, ri->ri_stride);
1871 #endif
1872 ri->ri_bits = (void *)((char *)fb + ri->ri_stride * ri->ri_yorigin +
1873 ri->ri_xorigin);
1874 }
1875
1876 #if 0
1877 static int
1878 mach64_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1879 {
1880
1881 return 0;
1882 }
1883 #endif
1884
1885 void
1886 machfb_blank(struct mach64_softc *sc, int blank)
1887 {
1888 uint32_t reg;
1889
1890 #define MACH64_BLANK (CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS | CRTC_VSYNC_DIS)
1891
1892 switch (blank)
1893 {
1894 case 0:
1895 reg = regr(sc, CRTC_GEN_CNTL);
1896 regw(sc, CRTC_GEN_CNTL, reg & ~(MACH64_BLANK));
1897 sc->sc_blanked = 0;
1898 break;
1899 case 1:
1900 reg = regr(sc, CRTC_GEN_CNTL);
1901 regw(sc, CRTC_GEN_CNTL, reg | (MACH64_BLANK));
1902 sc->sc_blanked = 1;
1903 break;
1904 default:
1905 break;
1906 }
1907 }
1908
1909 /* framebuffer device support */
1910 #ifdef __sparc__
1911
1912 static void
1913 machfb_unblank(device_t dev)
1914 {
1915 struct mach64_softc *sc = device_private(dev);
1916
1917 machfb_blank(sc, 0);
1918 }
1919
1920 static void
1921 machfb_fbattach(struct mach64_softc *sc)
1922 {
1923 struct fbdevice *fb = &sc->sc_fb;
1924
1925 fb->fb_device = sc->sc_dev;
1926 fb->fb_driver = &machfb_fbdriver;
1927
1928 fb->fb_type.fb_cmsize = 256;
1929 fb->fb_type.fb_size = sc->memsize;
1930
1931 fb->fb_type.fb_type = FBTYPE_GENERIC_PCI;
1932 fb->fb_flags = sc->sc_dev->dv_cfdata->cf_flags & FB_USERMASK;
1933 fb->fb_type.fb_depth = sc->bits_per_pixel;
1934 fb->fb_type.fb_width = sc->virt_x;
1935 fb->fb_type.fb_height = sc->virt_y;
1936
1937 fb->fb_pixels = sc->sc_aperture;
1938 fb_attach(fb, sc->sc_console);
1939 }
1940
1941 int
1942 machfb_fbopen(dev_t dev, int flags, int mode, struct lwp *l)
1943 {
1944 struct mach64_softc *sc;
1945
1946 sc = device_lookup_private(&machfb_cd, minor(dev));
1947 if (sc == NULL)
1948 return ENXIO;
1949 sc->sc_locked = 1;
1950
1951 #ifdef MACHFB_DEBUG
1952 printf("machfb_fbopen(%d)\n", minor(dev));
1953 #endif
1954 return 0;
1955 }
1956
1957 int
1958 machfb_fbclose(dev_t dev, int flags, int mode, struct lwp *l)
1959 {
1960 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
1961
1962 #ifdef MACHFB_DEBUG
1963 printf("machfb_fbclose()\n");
1964 #endif
1965 mach64_init_engine(sc);
1966 mach64_init_lut(sc);
1967 sc->sc_locked = 0;
1968 return 0;
1969 }
1970
1971 int
1972 machfb_fbioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
1973 {
1974 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
1975
1976 #ifdef MACHFB_DEBUG
1977 printf("machfb_fbioctl(%d, %lx)\n", minor(dev), cmd);
1978 #endif
1979 switch (cmd) {
1980 case FBIOGTYPE:
1981 *(struct fbtype *)data = sc->sc_fb.fb_type;
1982 break;
1983
1984 case FBIOGATTR:
1985 #define fba ((struct fbgattr *)data)
1986 fba->real_type = sc->sc_fb.fb_type.fb_type;
1987 fba->owner = 0; /* XXX ??? */
1988 fba->fbtype = sc->sc_fb.fb_type;
1989 fba->sattr.flags = 0;
1990 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
1991 fba->sattr.dev_specific[0] = sc->sc_nbus;
1992 fba->sattr.dev_specific[1] = sc->sc_ndev;
1993 fba->sattr.dev_specific[2] = sc->sc_nfunc;
1994 fba->sattr.dev_specific[3] = -1;
1995 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
1996 fba->emu_types[1] = -1;
1997 #undef fba
1998 break;
1999
2000 #if 0
2001 case FBIOGETCMAP:
2002 #define p ((struct fbcmap *)data)
2003 return bt_getcmap(p, &sc->sc_cmap, 256, 1);
2004
2005 case FBIOPUTCMAP:
2006 /* copy to software map */
2007 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
2008 if (error)
2009 return error;
2010 /* now blast them into the chip */
2011 /* XXX should use retrace interrupt */
2012 cg6_loadcmap(sc, p->index, p->count);
2013 #undef p
2014 break;
2015 #endif
2016 case FBIOGVIDEO:
2017 *(int *)data = sc->sc_blanked;
2018 break;
2019
2020 case FBIOSVIDEO:
2021 machfb_blank(sc, *(int *)data);
2022 break;
2023
2024 #if 0
2025 case FBIOGCURSOR:
2026 break;
2027
2028 case FBIOSCURSOR:
2029 break;
2030
2031 case FBIOGCURPOS:
2032 *(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
2033 break;
2034
2035 case FBIOSCURPOS:
2036 sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
2037 break;
2038
2039 case FBIOGCURMAX:
2040 /* max cursor size is 32x32 */
2041 ((struct fbcurpos *)data)->x = 32;
2042 ((struct fbcurpos *)data)->y = 32;
2043 break;
2044 #endif
2045 case PCI_IOC_CFGREAD:
2046 case PCI_IOC_CFGWRITE: {
2047 int ret;
2048 ret = pci_devioctl(sc->sc_pc, sc->sc_pcitag,
2049 cmd, data, flags, l);
2050
2051 #ifdef MACHFB_DEBUG
2052 printf("pci_devioctl: %d\n", ret);
2053 #endif
2054 return ret;
2055 }
2056
2057 case WSDISPLAYIO_GET_BUSID:
2058 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
2059 sc->sc_pcitag, data);
2060
2061 default:
2062 return ENOTTY;
2063 }
2064 #ifdef MACHFB_DEBUG
2065 printf("machfb_fbioctl done\n");
2066 #endif
2067 return 0;
2068 }
2069
2070 paddr_t
2071 machfb_fbmmap(dev_t dev, off_t off, int prot)
2072 {
2073 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
2074
2075 if (sc != NULL)
2076 return mach64_mmap(&sc->vd, NULL, off, prot);
2077
2078 return 0;
2079 }
2080
2081 #endif /* __sparc__ */
2082