3 * Copyright 2012 Red Hat
5 * This file is subject to the terms and conditions of the GNU General
6 * Public License version 2. See the file COPYING in the main
7 * directory of this archive for more details.
9 * Authors: Matthew Garrett
12 * Portions of this code derived from cirrusfb.c:
13 * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
15 * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_plane_helper.h>
21 #include <video/cirrus.h>
23 #include "cirrus_drv.h"
25 #define CIRRUS_LUT_SIZE 256
27 #define PALETTE_INDEX 0x8
28 #define PALETTE_DATA 0x9
31 * This file contains setup code for the CRTC.
34 static void cirrus_crtc_load_lut(struct drm_crtc
*crtc
)
36 struct cirrus_crtc
*cirrus_crtc
= to_cirrus_crtc(crtc
);
37 struct drm_device
*dev
= crtc
->dev
;
38 struct cirrus_device
*cdev
= dev
->dev_private
;
44 for (i
= 0; i
< CIRRUS_LUT_SIZE
; i
++) {
46 WREG8(PALETTE_INDEX
, i
);
47 WREG8(PALETTE_DATA
, cirrus_crtc
->lut_r
[i
]);
48 WREG8(PALETTE_DATA
, cirrus_crtc
->lut_g
[i
]);
49 WREG8(PALETTE_DATA
, cirrus_crtc
->lut_b
[i
]);
54 * The DRM core requires DPMS functions, but they make little sense in our
55 * case and so are just stubs
58 static void cirrus_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
60 struct drm_device
*dev
= crtc
->dev
;
61 struct cirrus_device
*cdev
= dev
->dev_private
;
65 case DRM_MODE_DPMS_ON
:
69 case DRM_MODE_DPMS_STANDBY
:
73 case DRM_MODE_DPMS_SUSPEND
:
77 case DRM_MODE_DPMS_OFF
:
85 WREG8(SEQ_INDEX
, 0x1);
86 sr01
|= RREG8(SEQ_DATA
) & ~0x20;
89 WREG8(GFX_INDEX
, 0xe);
90 gr0e
|= RREG8(GFX_DATA
) & ~0x06;
95 * The core passes the desired mode to the CRTC code to see whether any
96 * CRTC-specific modifications need to be made to it. We're in a position
97 * to just pass that straight through, so this does nothing
99 static bool cirrus_crtc_mode_fixup(struct drm_crtc
*crtc
,
100 const struct drm_display_mode
*mode
,
101 struct drm_display_mode
*adjusted_mode
)
106 static void cirrus_set_start_address(struct drm_crtc
*crtc
, unsigned offset
)
108 struct cirrus_device
*cdev
= crtc
->dev
->dev_private
;
113 WREG_CRT(0x0c, (u8
)((addr
>> 8) & 0xff));
114 WREG_CRT(0x0d, (u8
)(addr
& 0xff));
116 WREG8(CRT_INDEX
, 0x1b);
117 tmp
= RREG8(CRT_DATA
);
119 tmp
|= (addr
>> 16) & 0x01;
120 tmp
|= (addr
>> 15) & 0x0c;
122 WREG8(CRT_INDEX
, 0x1d);
123 tmp
= RREG8(CRT_DATA
);
125 tmp
|= (addr
>> 12) & 0x80;
129 /* cirrus is different - we will force move buffers out of VRAM */
130 static int cirrus_crtc_do_set_base(struct drm_crtc
*crtc
,
131 struct drm_framebuffer
*fb
,
132 int x
, int y
, int atomic
)
134 struct cirrus_device
*cdev
= crtc
->dev
->dev_private
;
135 struct drm_gem_object
*obj
;
136 struct cirrus_framebuffer
*cirrus_fb
;
137 struct cirrus_bo
*bo
;
141 /* push the previous fb to system ram */
143 cirrus_fb
= to_cirrus_framebuffer(fb
);
144 obj
= cirrus_fb
->obj
;
145 bo
= gem_to_cirrus_bo(obj
);
146 ret
= cirrus_bo_reserve(bo
, false);
149 cirrus_bo_push_sysram(bo
);
150 cirrus_bo_unreserve(bo
);
153 cirrus_fb
= to_cirrus_framebuffer(crtc
->primary
->fb
);
154 obj
= cirrus_fb
->obj
;
155 bo
= gem_to_cirrus_bo(obj
);
157 ret
= cirrus_bo_reserve(bo
, false);
161 ret
= cirrus_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
163 cirrus_bo_unreserve(bo
);
167 if (&cdev
->mode_info
.gfbdev
->gfb
== cirrus_fb
) {
168 /* if pushing console in kmap it */
169 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
171 DRM_ERROR("failed to kmap fbcon\n");
173 cirrus_bo_unreserve(bo
);
175 cirrus_set_start_address(crtc
, (u32
)gpu_addr
);
179 static int cirrus_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
180 struct drm_framebuffer
*old_fb
)
182 return cirrus_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
186 * The meat of this driver. The core passes us a mode and we have to program
187 * it. The modesetting here is the bare minimum required to satisfy the qemu
188 * emulation of this hardware, and running this against a real device is
189 * likely to result in an inadequately programmed mode. We've already had
190 * the opportunity to modify the mode, so whatever we receive here should
191 * be something that can be correctly programmed and displayed
193 static int cirrus_crtc_mode_set(struct drm_crtc
*crtc
,
194 struct drm_display_mode
*mode
,
195 struct drm_display_mode
*adjusted_mode
,
196 int x
, int y
, struct drm_framebuffer
*old_fb
)
198 struct drm_device
*dev
= crtc
->dev
;
199 struct cirrus_device
*cdev
= dev
->dev_private
;
200 int hsyncstart
, hsyncend
, htotal
, hdispend
;
201 int vtotal
, vdispend
;
203 int sr07
= 0, hdr
= 0;
205 htotal
= mode
->htotal
/ 8;
206 hsyncend
= mode
->hsync_end
/ 8;
207 hsyncstart
= mode
->hsync_start
/ 8;
208 hdispend
= mode
->hdisplay
/ 8;
210 vtotal
= mode
->vtotal
;
211 vdispend
= mode
->vdisplay
;
221 WREG_CRT(VGA_CRTC_V_SYNC_END
, 0x20);
222 WREG_CRT(VGA_CRTC_H_TOTAL
, htotal
);
223 WREG_CRT(VGA_CRTC_H_DISP
, hdispend
);
224 WREG_CRT(VGA_CRTC_H_SYNC_START
, hsyncstart
);
225 WREG_CRT(VGA_CRTC_H_SYNC_END
, hsyncend
);
226 WREG_CRT(VGA_CRTC_V_TOTAL
, vtotal
& 0xff);
227 WREG_CRT(VGA_CRTC_V_DISP_END
, vdispend
& 0xff);
230 if ((vdispend
+ 1) & 512)
232 WREG_CRT(VGA_CRTC_MAX_SCAN
, tmp
);
235 * Overflow bits for values that don't fit in the standard registers
242 if ((vdispend
+ 1) & 256)
248 WREG_CRT(VGA_CRTC_OVERFLOW
, tmp
);
252 /* More overflow bits */
254 if ((htotal
+ 5) & 64)
256 if ((htotal
+ 5) & 128)
263 WREG_CRT(CL_CRT1A
, tmp
);
265 /* Disable Hercules/CGA compatibility */
266 WREG_CRT(VGA_CRTC_MODE
, 0x03);
268 WREG8(SEQ_INDEX
, 0x7);
269 sr07
= RREG8(SEQ_DATA
);
272 switch (crtc
->primary
->fb
->bits_per_pixel
) {
294 /* Program the pitch */
295 tmp
= crtc
->primary
->fb
->pitches
[0] / 8;
296 WREG_CRT(VGA_CRTC_OFFSET
, tmp
);
298 /* Enable extended blanking and pitch bits, and enable full memory */
300 tmp
|= (crtc
->primary
->fb
->pitches
[0] >> 7) & 0x10;
301 tmp
|= (crtc
->primary
->fb
->pitches
[0] >> 6) & 0x40;
304 /* Enable high-colour modes */
305 WREG_GFX(VGA_GFX_MODE
, 0x40);
307 /* And set graphics mode */
308 WREG_GFX(VGA_GFX_MISC
, 0x01);
311 cirrus_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
313 /* Unblank (needed on S3 resume, vgabios doesn't do it then) */
319 * This is called before a mode is programmed. A typical use might be to
320 * enable DPMS during the programming to avoid seeing intermediate stages,
321 * but that's not relevant to us
323 static void cirrus_crtc_prepare(struct drm_crtc
*crtc
)
328 * This is called after a mode is programmed. It should reverse anything done
329 * by the prepare function
331 static void cirrus_crtc_commit(struct drm_crtc
*crtc
)
336 * The core can pass us a set of gamma values to program. We actually only
337 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
338 * but it's a requirement that we provide the function
340 static void cirrus_crtc_gamma_set(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
341 u16
*blue
, uint32_t start
, uint32_t size
)
343 struct cirrus_crtc
*cirrus_crtc
= to_cirrus_crtc(crtc
);
346 if (size
!= CIRRUS_LUT_SIZE
)
349 for (i
= 0; i
< CIRRUS_LUT_SIZE
; i
++) {
350 cirrus_crtc
->lut_r
[i
] = red
[i
];
351 cirrus_crtc
->lut_g
[i
] = green
[i
];
352 cirrus_crtc
->lut_b
[i
] = blue
[i
];
354 cirrus_crtc_load_lut(crtc
);
357 /* Simple cleanup function */
358 static void cirrus_crtc_destroy(struct drm_crtc
*crtc
)
360 struct cirrus_crtc
*cirrus_crtc
= to_cirrus_crtc(crtc
);
362 drm_crtc_cleanup(crtc
);
366 /* These provide the minimum set of functions required to handle a CRTC */
367 static const struct drm_crtc_funcs cirrus_crtc_funcs
= {
368 .gamma_set
= cirrus_crtc_gamma_set
,
369 .set_config
= drm_crtc_helper_set_config
,
370 .destroy
= cirrus_crtc_destroy
,
373 static const struct drm_crtc_helper_funcs cirrus_helper_funcs
= {
374 .dpms
= cirrus_crtc_dpms
,
375 .mode_fixup
= cirrus_crtc_mode_fixup
,
376 .mode_set
= cirrus_crtc_mode_set
,
377 .mode_set_base
= cirrus_crtc_mode_set_base
,
378 .prepare
= cirrus_crtc_prepare
,
379 .commit
= cirrus_crtc_commit
,
380 .load_lut
= cirrus_crtc_load_lut
,
384 static void cirrus_crtc_init(struct drm_device
*dev
)
386 struct cirrus_device
*cdev
= dev
->dev_private
;
387 struct cirrus_crtc
*cirrus_crtc
;
390 cirrus_crtc
= kzalloc(sizeof(struct cirrus_crtc
) +
391 (CIRRUSFB_CONN_LIMIT
* sizeof(struct drm_connector
*)),
394 if (cirrus_crtc
== NULL
)
397 drm_crtc_init(dev
, &cirrus_crtc
->base
, &cirrus_crtc_funcs
);
399 drm_mode_crtc_set_gamma_size(&cirrus_crtc
->base
, CIRRUS_LUT_SIZE
);
400 cdev
->mode_info
.crtc
= cirrus_crtc
;
402 for (i
= 0; i
< CIRRUS_LUT_SIZE
; i
++) {
403 cirrus_crtc
->lut_r
[i
] = i
;
404 cirrus_crtc
->lut_g
[i
] = i
;
405 cirrus_crtc
->lut_b
[i
] = i
;
408 drm_crtc_helper_add(&cirrus_crtc
->base
, &cirrus_helper_funcs
);
411 /** Sets the color ramps on behalf of fbcon */
412 void cirrus_crtc_fb_gamma_set(struct drm_crtc
*crtc
, u16 red
, u16 green
,
415 struct cirrus_crtc
*cirrus_crtc
= to_cirrus_crtc(crtc
);
417 cirrus_crtc
->lut_r
[regno
] = red
;
418 cirrus_crtc
->lut_g
[regno
] = green
;
419 cirrus_crtc
->lut_b
[regno
] = blue
;
422 /** Gets the color ramps on behalf of fbcon */
423 void cirrus_crtc_fb_gamma_get(struct drm_crtc
*crtc
, u16
*red
, u16
*green
,
424 u16
*blue
, int regno
)
426 struct cirrus_crtc
*cirrus_crtc
= to_cirrus_crtc(crtc
);
428 *red
= cirrus_crtc
->lut_r
[regno
];
429 *green
= cirrus_crtc
->lut_g
[regno
];
430 *blue
= cirrus_crtc
->lut_b
[regno
];
434 static bool cirrus_encoder_mode_fixup(struct drm_encoder
*encoder
,
435 const struct drm_display_mode
*mode
,
436 struct drm_display_mode
*adjusted_mode
)
441 static void cirrus_encoder_mode_set(struct drm_encoder
*encoder
,
442 struct drm_display_mode
*mode
,
443 struct drm_display_mode
*adjusted_mode
)
447 static void cirrus_encoder_dpms(struct drm_encoder
*encoder
, int state
)
452 static void cirrus_encoder_prepare(struct drm_encoder
*encoder
)
456 static void cirrus_encoder_commit(struct drm_encoder
*encoder
)
460 static void cirrus_encoder_destroy(struct drm_encoder
*encoder
)
462 struct cirrus_encoder
*cirrus_encoder
= to_cirrus_encoder(encoder
);
463 drm_encoder_cleanup(encoder
);
464 kfree(cirrus_encoder
);
467 static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs
= {
468 .dpms
= cirrus_encoder_dpms
,
469 .mode_fixup
= cirrus_encoder_mode_fixup
,
470 .mode_set
= cirrus_encoder_mode_set
,
471 .prepare
= cirrus_encoder_prepare
,
472 .commit
= cirrus_encoder_commit
,
475 static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs
= {
476 .destroy
= cirrus_encoder_destroy
,
479 static struct drm_encoder
*cirrus_encoder_init(struct drm_device
*dev
)
481 struct drm_encoder
*encoder
;
482 struct cirrus_encoder
*cirrus_encoder
;
484 cirrus_encoder
= kzalloc(sizeof(struct cirrus_encoder
), GFP_KERNEL
);
488 encoder
= &cirrus_encoder
->base
;
489 encoder
->possible_crtcs
= 0x1;
491 drm_encoder_init(dev
, encoder
, &cirrus_encoder_encoder_funcs
,
492 DRM_MODE_ENCODER_DAC
);
493 drm_encoder_helper_add(encoder
, &cirrus_encoder_helper_funcs
);
499 static int cirrus_vga_get_modes(struct drm_connector
*connector
)
503 /* Just add a static list of modes */
504 if (cirrus_bpp
<= 24) {
505 count
= drm_add_modes_noedid(connector
, 1280, 1024);
506 drm_set_preferred_mode(connector
, 1024, 768);
508 count
= drm_add_modes_noedid(connector
, 800, 600);
509 drm_set_preferred_mode(connector
, 800, 600);
514 static struct drm_encoder
*cirrus_connector_best_encoder(struct drm_connector
517 int enc_id
= connector
->encoder_ids
[0];
518 /* pick the encoder ids */
520 return drm_encoder_find(connector
->dev
, enc_id
);
524 static enum drm_connector_status
cirrus_vga_detect(struct drm_connector
525 *connector
, bool force
)
527 return connector_status_connected
;
530 static void cirrus_connector_destroy(struct drm_connector
*connector
)
532 drm_connector_cleanup(connector
);
536 struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs
= {
537 .get_modes
= cirrus_vga_get_modes
,
538 .best_encoder
= cirrus_connector_best_encoder
,
541 struct drm_connector_funcs cirrus_vga_connector_funcs
= {
542 .dpms
= drm_helper_connector_dpms
,
543 .detect
= cirrus_vga_detect
,
544 .fill_modes
= drm_helper_probe_single_connector_modes
,
545 .destroy
= cirrus_connector_destroy
,
548 static struct drm_connector
*cirrus_vga_init(struct drm_device
*dev
)
550 struct drm_connector
*connector
;
551 struct cirrus_connector
*cirrus_connector
;
553 cirrus_connector
= kzalloc(sizeof(struct cirrus_connector
), GFP_KERNEL
);
554 if (!cirrus_connector
)
557 connector
= &cirrus_connector
->base
;
559 drm_connector_init(dev
, connector
,
560 &cirrus_vga_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
562 drm_connector_helper_add(connector
, &cirrus_vga_connector_helper_funcs
);
564 drm_connector_register(connector
);
569 int cirrus_modeset_init(struct cirrus_device
*cdev
)
571 struct drm_encoder
*encoder
;
572 struct drm_connector
*connector
;
575 drm_mode_config_init(cdev
->dev
);
576 cdev
->mode_info
.mode_config_initialized
= true;
578 cdev
->dev
->mode_config
.max_width
= CIRRUS_MAX_FB_WIDTH
;
579 cdev
->dev
->mode_config
.max_height
= CIRRUS_MAX_FB_HEIGHT
;
581 cdev
->dev
->mode_config
.fb_base
= cdev
->mc
.vram_base
;
582 cdev
->dev
->mode_config
.preferred_depth
= 24;
583 /* don't prefer a shadow on virt GPU */
584 cdev
->dev
->mode_config
.prefer_shadow
= 0;
586 cirrus_crtc_init(cdev
->dev
);
588 encoder
= cirrus_encoder_init(cdev
->dev
);
590 DRM_ERROR("cirrus_encoder_init failed\n");
594 connector
= cirrus_vga_init(cdev
->dev
);
596 DRM_ERROR("cirrus_vga_init failed\n");
600 drm_mode_connector_attach_encoder(connector
, encoder
);
602 ret
= cirrus_fbdev_init(cdev
);
604 DRM_ERROR("cirrus_fbdev_init failed\n");
611 void cirrus_modeset_fini(struct cirrus_device
*cdev
)
613 cirrus_fbdev_fini(cdev
);
615 if (cdev
->mode_info
.mode_config_initialized
) {
616 drm_mode_config_cleanup(cdev
->dev
);
617 cdev
->mode_info
.mode_config_initialized
= false;