196f60e37SRussell King /* 296f60e37SRussell King * Copyright (C) 2012 Russell King 396f60e37SRussell King * Rewritten from the dovefb driver, and Armada510 manuals. 496f60e37SRussell King * 596f60e37SRussell King * This program is free software; you can redistribute it and/or modify 696f60e37SRussell King * it under the terms of the GNU General Public License version 2 as 796f60e37SRussell King * published by the Free Software Foundation. 896f60e37SRussell King */ 996f60e37SRussell King #include <linux/clk.h> 1096f60e37SRussell King #include <drm/drmP.h> 1196f60e37SRussell King #include <drm/drm_crtc_helper.h> 1296f60e37SRussell King #include "armada_crtc.h" 1396f60e37SRussell King #include "armada_drm.h" 1496f60e37SRussell King #include "armada_fb.h" 1596f60e37SRussell King #include "armada_gem.h" 1696f60e37SRussell King #include "armada_hw.h" 1796f60e37SRussell King 1896f60e37SRussell King struct armada_frame_work { 1996f60e37SRussell King struct drm_pending_vblank_event *event; 2096f60e37SRussell King struct armada_regs regs[4]; 2196f60e37SRussell King struct drm_framebuffer *old_fb; 2296f60e37SRussell King }; 2396f60e37SRussell King 2496f60e37SRussell King enum csc_mode { 2596f60e37SRussell King CSC_AUTO = 0, 2696f60e37SRussell King CSC_YUV_CCIR601 = 1, 2796f60e37SRussell King CSC_YUV_CCIR709 = 2, 2896f60e37SRussell King CSC_RGB_COMPUTER = 1, 2996f60e37SRussell King CSC_RGB_STUDIO = 2, 3096f60e37SRussell King }; 3196f60e37SRussell King 3296f60e37SRussell King /* 3396f60e37SRussell King * A note about interlacing. Let's consider HDMI 1920x1080i. 3496f60e37SRussell King * The timing parameters we have from X are: 3596f60e37SRussell King * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot 3696f60e37SRussell King * 1920 2448 2492 2640 1080 1084 1094 1125 3796f60e37SRussell King * Which get translated to: 3896f60e37SRussell King * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot 3996f60e37SRussell King * 1920 2448 2492 2640 540 542 547 562 4096f60e37SRussell King * 4196f60e37SRussell King * This is how it is defined by CEA-861-D - line and pixel numbers are 4296f60e37SRussell King * referenced to the rising edge of VSYNC and HSYNC. Total clocks per 4396f60e37SRussell King * line: 2640. The odd frame, the first active line is at line 21, and 4496f60e37SRussell King * the even frame, the first active line is 584. 4596f60e37SRussell King * 4696f60e37SRussell King * LN: 560 561 562 563 567 568 569 4796f60e37SRussell King * DE: ~~~|____________________________//__________________________ 4896f60e37SRussell King * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____ 4996f60e37SRussell King * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________ 5096f60e37SRussell King * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge). 5196f60e37SRussell King * 5296f60e37SRussell King * LN: 1123 1124 1125 1 5 6 7 5396f60e37SRussell King * DE: ~~~|____________________________//__________________________ 5496f60e37SRussell King * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____ 5596f60e37SRussell King * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________ 5696f60e37SRussell King * 23 blanking lines 5796f60e37SRussell King * 5896f60e37SRussell King * The Armada LCD Controller line and pixel numbers are, like X timings, 5996f60e37SRussell King * referenced to the top left of the active frame. 6096f60e37SRussell King * 6196f60e37SRussell King * So, translating these to our LCD controller: 6296f60e37SRussell King * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128. 6396f60e37SRussell King * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448. 6496f60e37SRussell King * Note: Vsync front porch remains constant! 6596f60e37SRussell King * 6696f60e37SRussell King * if (odd_frame) { 6796f60e37SRussell King * vtotal = mode->crtc_vtotal + 1; 6896f60e37SRussell King * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1; 6996f60e37SRussell King * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2 7096f60e37SRussell King * } else { 7196f60e37SRussell King * vtotal = mode->crtc_vtotal; 7296f60e37SRussell King * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay; 7396f60e37SRussell King * vhorizpos = mode->crtc_hsync_start; 7496f60e37SRussell King * } 7596f60e37SRussell King * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end; 7696f60e37SRussell King * 7796f60e37SRussell King * So, we need to reprogram these registers on each vsync event: 7896f60e37SRussell King * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL 7996f60e37SRussell King * 8096f60e37SRussell King * Note: we do not use the frame done interrupts because these appear 8196f60e37SRussell King * to happen too early, and lead to jitter on the display (presumably 8296f60e37SRussell King * they occur at the end of the last active line, before the vsync back 8396f60e37SRussell King * porch, which we're reprogramming.) 8496f60e37SRussell King */ 8596f60e37SRussell King 8696f60e37SRussell King void 8796f60e37SRussell King armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs) 8896f60e37SRussell King { 8996f60e37SRussell King while (regs->offset != ~0) { 9096f60e37SRussell King void __iomem *reg = dcrtc->base + regs->offset; 9196f60e37SRussell King uint32_t val; 9296f60e37SRussell King 9396f60e37SRussell King val = regs->mask; 9496f60e37SRussell King if (val != 0) 9596f60e37SRussell King val &= readl_relaxed(reg); 9696f60e37SRussell King writel_relaxed(val | regs->val, reg); 9796f60e37SRussell King ++regs; 9896f60e37SRussell King } 9996f60e37SRussell King } 10096f60e37SRussell King 10196f60e37SRussell King #define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON) 10296f60e37SRussell King 10396f60e37SRussell King static void armada_drm_crtc_update(struct armada_crtc *dcrtc) 10496f60e37SRussell King { 10596f60e37SRussell King uint32_t dumb_ctrl; 10696f60e37SRussell King 10796f60e37SRussell King dumb_ctrl = dcrtc->cfg_dumb_ctrl; 10896f60e37SRussell King 10996f60e37SRussell King if (!dpms_blanked(dcrtc->dpms)) 11096f60e37SRussell King dumb_ctrl |= CFG_DUMB_ENA; 11196f60e37SRussell King 11296f60e37SRussell King /* 11396f60e37SRussell King * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might 11496f60e37SRussell King * be using SPI or GPIO. If we set this to DUMB_BLANK, we will 11596f60e37SRussell King * force LCD_D[23:0] to output blank color, overriding the GPIO or 11696f60e37SRussell King * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode. 11796f60e37SRussell King */ 11896f60e37SRussell King if (dpms_blanked(dcrtc->dpms) && 11996f60e37SRussell King (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) { 12096f60e37SRussell King dumb_ctrl &= ~DUMB_MASK; 12196f60e37SRussell King dumb_ctrl |= DUMB_BLANK; 12296f60e37SRussell King } 12396f60e37SRussell King 12496f60e37SRussell King /* 12596f60e37SRussell King * The documentation doesn't indicate what the normal state of 12696f60e37SRussell King * the sync signals are. Sebastian Hesselbart kindly probed 12796f60e37SRussell King * these signals on his board to determine their state. 12896f60e37SRussell King * 12996f60e37SRussell King * The non-inverted state of the sync signals is active high. 13096f60e37SRussell King * Setting these bits makes the appropriate signal active low. 13196f60e37SRussell King */ 13296f60e37SRussell King if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC) 13396f60e37SRussell King dumb_ctrl |= CFG_INV_CSYNC; 13496f60e37SRussell King if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC) 13596f60e37SRussell King dumb_ctrl |= CFG_INV_HSYNC; 13696f60e37SRussell King if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC) 13796f60e37SRussell King dumb_ctrl |= CFG_INV_VSYNC; 13896f60e37SRussell King 13996f60e37SRussell King if (dcrtc->dumb_ctrl != dumb_ctrl) { 14096f60e37SRussell King dcrtc->dumb_ctrl = dumb_ctrl; 14196f60e37SRussell King writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL); 14296f60e37SRussell King } 14396f60e37SRussell King } 14496f60e37SRussell King 14596f60e37SRussell King static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb, 14696f60e37SRussell King int x, int y, struct armada_regs *regs, bool interlaced) 14796f60e37SRussell King { 14896f60e37SRussell King struct armada_gem_object *obj = drm_fb_obj(fb); 14996f60e37SRussell King unsigned pitch = fb->pitches[0]; 15096f60e37SRussell King unsigned offset = y * pitch + x * fb->bits_per_pixel / 8; 15196f60e37SRussell King uint32_t addr_odd, addr_even; 15296f60e37SRussell King unsigned i = 0; 15396f60e37SRussell King 15496f60e37SRussell King DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n", 15596f60e37SRussell King pitch, x, y, fb->bits_per_pixel); 15696f60e37SRussell King 15796f60e37SRussell King addr_odd = addr_even = obj->dev_addr + offset; 15896f60e37SRussell King 15996f60e37SRussell King if (interlaced) { 16096f60e37SRussell King addr_even += pitch; 16196f60e37SRussell King pitch *= 2; 16296f60e37SRussell King } 16396f60e37SRussell King 16496f60e37SRussell King /* write offset, base, and pitch */ 16596f60e37SRussell King armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0); 16696f60e37SRussell King armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1); 16796f60e37SRussell King armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH); 16896f60e37SRussell King 16996f60e37SRussell King return i; 17096f60e37SRussell King } 17196f60e37SRussell King 17296f60e37SRussell King static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc, 17396f60e37SRussell King struct armada_frame_work *work) 17496f60e37SRussell King { 17596f60e37SRussell King struct drm_device *dev = dcrtc->crtc.dev; 17696f60e37SRussell King unsigned long flags; 17796f60e37SRussell King int ret; 17896f60e37SRussell King 17996f60e37SRussell King ret = drm_vblank_get(dev, dcrtc->num); 18096f60e37SRussell King if (ret) { 18196f60e37SRussell King DRM_ERROR("failed to acquire vblank counter\n"); 18296f60e37SRussell King return ret; 18396f60e37SRussell King } 18496f60e37SRussell King 18596f60e37SRussell King spin_lock_irqsave(&dev->event_lock, flags); 18696f60e37SRussell King if (!dcrtc->frame_work) 18796f60e37SRussell King dcrtc->frame_work = work; 18896f60e37SRussell King else 18996f60e37SRussell King ret = -EBUSY; 19096f60e37SRussell King spin_unlock_irqrestore(&dev->event_lock, flags); 19196f60e37SRussell King 19296f60e37SRussell King if (ret) 19396f60e37SRussell King drm_vblank_put(dev, dcrtc->num); 19496f60e37SRussell King 19596f60e37SRussell King return ret; 19696f60e37SRussell King } 19796f60e37SRussell King 19896f60e37SRussell King static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc) 19996f60e37SRussell King { 20096f60e37SRussell King struct drm_device *dev = dcrtc->crtc.dev; 20196f60e37SRussell King struct armada_frame_work *work = dcrtc->frame_work; 20296f60e37SRussell King 20396f60e37SRussell King dcrtc->frame_work = NULL; 20496f60e37SRussell King 20596f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, work->regs); 20696f60e37SRussell King 20796f60e37SRussell King if (work->event) 20896f60e37SRussell King drm_send_vblank_event(dev, dcrtc->num, work->event); 20996f60e37SRussell King 21096f60e37SRussell King drm_vblank_put(dev, dcrtc->num); 21196f60e37SRussell King 21296f60e37SRussell King /* Finally, queue the process-half of the cleanup. */ 21396f60e37SRussell King __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb); 21496f60e37SRussell King kfree(work); 21596f60e37SRussell King } 21696f60e37SRussell King 21796f60e37SRussell King static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc, 21896f60e37SRussell King struct drm_framebuffer *fb, bool force) 21996f60e37SRussell King { 22096f60e37SRussell King struct armada_frame_work *work; 22196f60e37SRussell King 22296f60e37SRussell King if (!fb) 22396f60e37SRussell King return; 22496f60e37SRussell King 22596f60e37SRussell King if (force) { 22696f60e37SRussell King /* Display is disabled, so just drop the old fb */ 22796f60e37SRussell King drm_framebuffer_unreference(fb); 22896f60e37SRussell King return; 22996f60e37SRussell King } 23096f60e37SRussell King 23196f60e37SRussell King work = kmalloc(sizeof(*work), GFP_KERNEL); 23296f60e37SRussell King if (work) { 23396f60e37SRussell King int i = 0; 23496f60e37SRussell King work->event = NULL; 23596f60e37SRussell King work->old_fb = fb; 23696f60e37SRussell King armada_reg_queue_end(work->regs, i); 23796f60e37SRussell King 23896f60e37SRussell King if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0) 23996f60e37SRussell King return; 24096f60e37SRussell King 24196f60e37SRussell King kfree(work); 24296f60e37SRussell King } 24396f60e37SRussell King 24496f60e37SRussell King /* 24596f60e37SRussell King * Oops - just drop the reference immediately and hope for 24696f60e37SRussell King * the best. The worst that will happen is the buffer gets 24796f60e37SRussell King * reused before it has finished being displayed. 24896f60e37SRussell King */ 24996f60e37SRussell King drm_framebuffer_unreference(fb); 25096f60e37SRussell King } 25196f60e37SRussell King 25296f60e37SRussell King static void armada_drm_vblank_off(struct armada_crtc *dcrtc) 25396f60e37SRussell King { 25496f60e37SRussell King struct drm_device *dev = dcrtc->crtc.dev; 25596f60e37SRussell King 25696f60e37SRussell King /* 25796f60e37SRussell King * Tell the DRM core that vblank IRQs aren't going to happen for 25896f60e37SRussell King * a while. This cleans up any pending vblank events for us. 25996f60e37SRussell King */ 26096f60e37SRussell King drm_vblank_off(dev, dcrtc->num); 26196f60e37SRussell King 26296f60e37SRussell King /* Handle any pending flip event. */ 26396f60e37SRussell King spin_lock_irq(&dev->event_lock); 26496f60e37SRussell King if (dcrtc->frame_work) 26596f60e37SRussell King armada_drm_crtc_complete_frame_work(dcrtc); 26696f60e37SRussell King spin_unlock_irq(&dev->event_lock); 26796f60e37SRussell King } 26896f60e37SRussell King 26996f60e37SRussell King void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b, 27096f60e37SRussell King int idx) 27196f60e37SRussell King { 27296f60e37SRussell King } 27396f60e37SRussell King 27496f60e37SRussell King void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b, 27596f60e37SRussell King int idx) 27696f60e37SRussell King { 27796f60e37SRussell King } 27896f60e37SRussell King 27996f60e37SRussell King /* The mode_config.mutex will be held for this call */ 28096f60e37SRussell King static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms) 28196f60e37SRussell King { 28296f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 28396f60e37SRussell King 28496f60e37SRussell King if (dcrtc->dpms != dpms) { 28596f60e37SRussell King dcrtc->dpms = dpms; 28696f60e37SRussell King armada_drm_crtc_update(dcrtc); 28796f60e37SRussell King if (dpms_blanked(dpms)) 28896f60e37SRussell King armada_drm_vblank_off(dcrtc); 28996f60e37SRussell King } 29096f60e37SRussell King } 29196f60e37SRussell King 29296f60e37SRussell King /* 29396f60e37SRussell King * Prepare for a mode set. Turn off overlay to ensure that we don't end 29496f60e37SRussell King * up with the overlay size being bigger than the active screen size. 29596f60e37SRussell King * We rely upon X refreshing this state after the mode set has completed. 29696f60e37SRussell King * 29796f60e37SRussell King * The mode_config.mutex will be held for this call 29896f60e37SRussell King */ 29996f60e37SRussell King static void armada_drm_crtc_prepare(struct drm_crtc *crtc) 30096f60e37SRussell King { 30196f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 30296f60e37SRussell King struct drm_plane *plane; 30396f60e37SRussell King 30496f60e37SRussell King /* 30596f60e37SRussell King * If we have an overlay plane associated with this CRTC, disable 30696f60e37SRussell King * it before the modeset to avoid its coordinates being outside 30796f60e37SRussell King * the new mode parameters. DRM doesn't provide help with this. 30896f60e37SRussell King */ 30996f60e37SRussell King plane = dcrtc->plane; 31096f60e37SRussell King if (plane) { 31196f60e37SRussell King struct drm_framebuffer *fb = plane->fb; 31296f60e37SRussell King 31396f60e37SRussell King plane->funcs->disable_plane(plane); 31496f60e37SRussell King plane->fb = NULL; 31596f60e37SRussell King plane->crtc = NULL; 31696f60e37SRussell King drm_framebuffer_unreference(fb); 31796f60e37SRussell King } 31896f60e37SRussell King } 31996f60e37SRussell King 32096f60e37SRussell King /* The mode_config.mutex will be held for this call */ 32196f60e37SRussell King static void armada_drm_crtc_commit(struct drm_crtc *crtc) 32296f60e37SRussell King { 32396f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 32496f60e37SRussell King 32596f60e37SRussell King if (dcrtc->dpms != DRM_MODE_DPMS_ON) { 32696f60e37SRussell King dcrtc->dpms = DRM_MODE_DPMS_ON; 32796f60e37SRussell King armada_drm_crtc_update(dcrtc); 32896f60e37SRussell King } 32996f60e37SRussell King } 33096f60e37SRussell King 33196f60e37SRussell King /* The mode_config.mutex will be held for this call */ 33296f60e37SRussell King static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc, 33396f60e37SRussell King const struct drm_display_mode *mode, struct drm_display_mode *adj) 33496f60e37SRussell King { 33596f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 33696f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 33796f60e37SRussell King int ret; 33896f60e37SRussell King 33996f60e37SRussell King /* We can't do interlaced modes if we don't have the SPU_ADV_REG */ 34096f60e37SRussell King if (!priv->variant->has_spu_adv_reg && 34196f60e37SRussell King adj->flags & DRM_MODE_FLAG_INTERLACE) 34296f60e37SRussell King return false; 34396f60e37SRussell King 34496f60e37SRussell King /* Check whether the display mode is possible */ 34596f60e37SRussell King ret = priv->variant->crtc_compute_clock(dcrtc, adj, NULL); 34696f60e37SRussell King if (ret) 34796f60e37SRussell King return false; 34896f60e37SRussell King 34996f60e37SRussell King return true; 35096f60e37SRussell King } 35196f60e37SRussell King 35296f60e37SRussell King void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat) 35396f60e37SRussell King { 35496f60e37SRussell King struct armada_vbl_event *e, *n; 35596f60e37SRussell King void __iomem *base = dcrtc->base; 35696f60e37SRussell King 35796f60e37SRussell King if (stat & DMA_FF_UNDERFLOW) 35896f60e37SRussell King DRM_ERROR("video underflow on crtc %u\n", dcrtc->num); 35996f60e37SRussell King if (stat & GRA_FF_UNDERFLOW) 36096f60e37SRussell King DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num); 36196f60e37SRussell King 36296f60e37SRussell King if (stat & VSYNC_IRQ) 36396f60e37SRussell King drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num); 36496f60e37SRussell King 36596f60e37SRussell King spin_lock(&dcrtc->irq_lock); 36696f60e37SRussell King 36796f60e37SRussell King list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) { 36896f60e37SRussell King list_del_init(&e->node); 36996f60e37SRussell King drm_vblank_put(dcrtc->crtc.dev, dcrtc->num); 37096f60e37SRussell King e->fn(dcrtc, e->data); 37196f60e37SRussell King } 37296f60e37SRussell King 37396f60e37SRussell King if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) { 37496f60e37SRussell King int i = stat & GRA_FRAME_IRQ0 ? 0 : 1; 37596f60e37SRussell King uint32_t val; 37696f60e37SRussell King 37796f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH); 37896f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_h_total, 37996f60e37SRussell King base + LCD_SPUT_V_H_TOTAL); 38096f60e37SRussell King 38196f60e37SRussell King val = readl_relaxed(base + LCD_SPU_ADV_REG); 38296f60e37SRussell King val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN); 38396f60e37SRussell King val |= dcrtc->v[i].spu_adv_reg; 384662af0d8SRussell King writel_relaxed(val, base + LCD_SPU_ADV_REG); 38596f60e37SRussell King } 386662af0d8SRussell King 387662af0d8SRussell King if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) { 388662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_pos, 389662af0d8SRussell King base + LCD_SPU_HWC_OVSA_HPXL_VLN); 390662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_sz, 391662af0d8SRussell King base + LCD_SPU_HWC_HPXL_VLN); 392662af0d8SRussell King armada_updatel(CFG_HWC_ENA, 393662af0d8SRussell King CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA, 394662af0d8SRussell King base + LCD_SPU_DMA_CTRL0); 395662af0d8SRussell King dcrtc->cursor_update = false; 396662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 397662af0d8SRussell King } 398662af0d8SRussell King 39996f60e37SRussell King spin_unlock(&dcrtc->irq_lock); 40096f60e37SRussell King 40196f60e37SRussell King if (stat & GRA_FRAME_IRQ) { 40296f60e37SRussell King struct drm_device *dev = dcrtc->crtc.dev; 40396f60e37SRussell King 40496f60e37SRussell King spin_lock(&dev->event_lock); 40596f60e37SRussell King if (dcrtc->frame_work) 40696f60e37SRussell King armada_drm_crtc_complete_frame_work(dcrtc); 40796f60e37SRussell King spin_unlock(&dev->event_lock); 40896f60e37SRussell King 40996f60e37SRussell King wake_up(&dcrtc->frame_wait); 41096f60e37SRussell King } 41196f60e37SRussell King } 41296f60e37SRussell King 41396f60e37SRussell King /* These are locked by dev->vbl_lock */ 41496f60e37SRussell King void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask) 41596f60e37SRussell King { 41696f60e37SRussell King if (dcrtc->irq_ena & mask) { 41796f60e37SRussell King dcrtc->irq_ena &= ~mask; 41896f60e37SRussell King writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 41996f60e37SRussell King } 42096f60e37SRussell King } 42196f60e37SRussell King 42296f60e37SRussell King void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask) 42396f60e37SRussell King { 42496f60e37SRussell King if ((dcrtc->irq_ena & mask) != mask) { 42596f60e37SRussell King dcrtc->irq_ena |= mask; 42696f60e37SRussell King writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 42796f60e37SRussell King if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask) 42896f60e37SRussell King writel(0, dcrtc->base + LCD_SPU_IRQ_ISR); 42996f60e37SRussell King } 43096f60e37SRussell King } 43196f60e37SRussell King 43296f60e37SRussell King static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc) 43396f60e37SRussell King { 43496f60e37SRussell King struct drm_display_mode *adj = &dcrtc->crtc.mode; 43596f60e37SRussell King uint32_t val = 0; 43696f60e37SRussell King 43796f60e37SRussell King if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709) 43896f60e37SRussell King val |= CFG_CSC_YUV_CCIR709; 43996f60e37SRussell King if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO) 44096f60e37SRussell King val |= CFG_CSC_RGB_STUDIO; 44196f60e37SRussell King 44296f60e37SRussell King /* 44396f60e37SRussell King * In auto mode, set the colorimetry, based upon the HDMI spec. 44496f60e37SRussell King * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use 44596f60e37SRussell King * ITU601. It may be more appropriate to set this depending on 44696f60e37SRussell King * the source - but what if the graphic frame is YUV and the 44796f60e37SRussell King * video frame is RGB? 44896f60e37SRussell King */ 44996f60e37SRussell King if ((adj->hdisplay == 1280 && adj->vdisplay == 720 && 45096f60e37SRussell King !(adj->flags & DRM_MODE_FLAG_INTERLACE)) || 45196f60e37SRussell King (adj->hdisplay == 1920 && adj->vdisplay == 1080)) { 45296f60e37SRussell King if (dcrtc->csc_yuv_mode == CSC_AUTO) 45396f60e37SRussell King val |= CFG_CSC_YUV_CCIR709; 45496f60e37SRussell King } 45596f60e37SRussell King 45696f60e37SRussell King /* 45796f60e37SRussell King * We assume we're connected to a TV-like device, so the YUV->RGB 45896f60e37SRussell King * conversion should produce a limited range. We should set this 45996f60e37SRussell King * depending on the connectors attached to this CRTC, and what 46096f60e37SRussell King * kind of device they report being connected. 46196f60e37SRussell King */ 46296f60e37SRussell King if (dcrtc->csc_rgb_mode == CSC_AUTO) 46396f60e37SRussell King val |= CFG_CSC_RGB_STUDIO; 46496f60e37SRussell King 46596f60e37SRussell King return val; 46696f60e37SRussell King } 46796f60e37SRussell King 46896f60e37SRussell King /* The mode_config.mutex will be held for this call */ 46996f60e37SRussell King static int armada_drm_crtc_mode_set(struct drm_crtc *crtc, 47096f60e37SRussell King struct drm_display_mode *mode, struct drm_display_mode *adj, 47196f60e37SRussell King int x, int y, struct drm_framebuffer *old_fb) 47296f60e37SRussell King { 47396f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 47496f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 47596f60e37SRussell King struct armada_regs regs[17]; 47696f60e37SRussell King uint32_t lm, rm, tm, bm, val, sclk; 47796f60e37SRussell King unsigned long flags; 47896f60e37SRussell King unsigned i; 47996f60e37SRussell King bool interlaced; 48096f60e37SRussell King 481f4510a27SMatt Roper drm_framebuffer_reference(crtc->primary->fb); 48296f60e37SRussell King 48396f60e37SRussell King interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE); 48496f60e37SRussell King 485f4510a27SMatt Roper i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb, 486f4510a27SMatt Roper x, y, regs, interlaced); 48796f60e37SRussell King 48896f60e37SRussell King rm = adj->crtc_hsync_start - adj->crtc_hdisplay; 48996f60e37SRussell King lm = adj->crtc_htotal - adj->crtc_hsync_end; 49096f60e37SRussell King bm = adj->crtc_vsync_start - adj->crtc_vdisplay; 49196f60e37SRussell King tm = adj->crtc_vtotal - adj->crtc_vsync_end; 49296f60e37SRussell King 49396f60e37SRussell King DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n", 49496f60e37SRussell King adj->crtc_hdisplay, 49596f60e37SRussell King adj->crtc_hsync_start, 49696f60e37SRussell King adj->crtc_hsync_end, 49796f60e37SRussell King adj->crtc_htotal, lm, rm); 49896f60e37SRussell King DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n", 49996f60e37SRussell King adj->crtc_vdisplay, 50096f60e37SRussell King adj->crtc_vsync_start, 50196f60e37SRussell King adj->crtc_vsync_end, 50296f60e37SRussell King adj->crtc_vtotal, tm, bm); 50396f60e37SRussell King 50496f60e37SRussell King /* Wait for pending flips to complete */ 50596f60e37SRussell King wait_event(dcrtc->frame_wait, !dcrtc->frame_work); 50696f60e37SRussell King 50796f60e37SRussell King drm_vblank_pre_modeset(crtc->dev, dcrtc->num); 50896f60e37SRussell King 50996f60e37SRussell King crtc->mode = *adj; 51096f60e37SRussell King 51196f60e37SRussell King val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA; 51296f60e37SRussell King if (val != dcrtc->dumb_ctrl) { 51396f60e37SRussell King dcrtc->dumb_ctrl = val; 51496f60e37SRussell King writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL); 51596f60e37SRussell King } 51696f60e37SRussell King 51796f60e37SRussell King /* Now compute the divider for real */ 51896f60e37SRussell King priv->variant->crtc_compute_clock(dcrtc, adj, &sclk); 51996f60e37SRussell King 52096f60e37SRussell King /* Ensure graphic fifo is enabled */ 52196f60e37SRussell King armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1); 52296f60e37SRussell King armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV); 52396f60e37SRussell King 52496f60e37SRussell King if (interlaced ^ dcrtc->interlaced) { 52596f60e37SRussell King if (adj->flags & DRM_MODE_FLAG_INTERLACE) 52696f60e37SRussell King drm_vblank_get(dcrtc->crtc.dev, dcrtc->num); 52796f60e37SRussell King else 52896f60e37SRussell King drm_vblank_put(dcrtc->crtc.dev, dcrtc->num); 52996f60e37SRussell King dcrtc->interlaced = interlaced; 53096f60e37SRussell King } 53196f60e37SRussell King 53296f60e37SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 53396f60e37SRussell King 53496f60e37SRussell King /* Even interlaced/progressive frame */ 53596f60e37SRussell King dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 | 53696f60e37SRussell King adj->crtc_htotal; 53796f60e37SRussell King dcrtc->v[1].spu_v_porch = tm << 16 | bm; 53896f60e37SRussell King val = adj->crtc_hsync_start; 539662af0d8SRussell King dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN | 540662af0d8SRussell King priv->variant->spu_adv_reg; 54196f60e37SRussell King 54296f60e37SRussell King if (interlaced) { 54396f60e37SRussell King /* Odd interlaced frame */ 54496f60e37SRussell King dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total + 54596f60e37SRussell King (1 << 16); 54696f60e37SRussell King dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1; 54796f60e37SRussell King val = adj->crtc_hsync_start - adj->crtc_htotal / 2; 548662af0d8SRussell King dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN | 549662af0d8SRussell King priv->variant->spu_adv_reg; 55096f60e37SRussell King } else { 55196f60e37SRussell King dcrtc->v[0] = dcrtc->v[1]; 55296f60e37SRussell King } 55396f60e37SRussell King 55496f60e37SRussell King val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay; 55596f60e37SRussell King 55696f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE); 55796f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN); 55896f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN); 55996f60e37SRussell King armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH); 56096f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH); 56196f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total, 56296f60e37SRussell King LCD_SPUT_V_H_TOTAL); 56396f60e37SRussell King 564662af0d8SRussell King if (priv->variant->has_spu_adv_reg) { 56596f60e37SRussell King armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg, 56696f60e37SRussell King ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | 56796f60e37SRussell King ADV_VSYNCOFFEN, LCD_SPU_ADV_REG); 568662af0d8SRussell King } 56996f60e37SRussell King 57096f60e37SRussell King val = CFG_GRA_ENA | CFG_GRA_HSMOOTH; 571f4510a27SMatt Roper val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt); 572f4510a27SMatt Roper val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod); 57396f60e37SRussell King 574f4510a27SMatt Roper if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420) 57596f60e37SRussell King val |= CFG_PALETTE_ENA; 57696f60e37SRussell King 57796f60e37SRussell King if (interlaced) 57896f60e37SRussell King val |= CFG_GRA_FTOGGLE; 57996f60e37SRussell King 58096f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT | 58196f60e37SRussell King CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV | 58296f60e37SRussell King CFG_SWAPYU | CFG_YUV2RGB) | 58396f60e37SRussell King CFG_PALETTE_ENA | CFG_GRA_FTOGGLE, 58496f60e37SRussell King LCD_SPU_DMA_CTRL0); 58596f60e37SRussell King 58696f60e37SRussell King val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0; 58796f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1); 58896f60e37SRussell King 58996f60e37SRussell King val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc); 59096f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL); 59196f60e37SRussell King armada_reg_queue_end(regs, i); 59296f60e37SRussell King 59396f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 59496f60e37SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 59596f60e37SRussell King 59696f60e37SRussell King armada_drm_crtc_update(dcrtc); 59796f60e37SRussell King 59896f60e37SRussell King drm_vblank_post_modeset(crtc->dev, dcrtc->num); 59996f60e37SRussell King armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms)); 60096f60e37SRussell King 60196f60e37SRussell King return 0; 60296f60e37SRussell King } 60396f60e37SRussell King 60496f60e37SRussell King /* The mode_config.mutex will be held for this call */ 60596f60e37SRussell King static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, 60696f60e37SRussell King struct drm_framebuffer *old_fb) 60796f60e37SRussell King { 60896f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 60996f60e37SRussell King struct armada_regs regs[4]; 61096f60e37SRussell King unsigned i; 61196f60e37SRussell King 612f4510a27SMatt Roper i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs, 61396f60e37SRussell King dcrtc->interlaced); 61496f60e37SRussell King armada_reg_queue_end(regs, i); 61596f60e37SRussell King 61696f60e37SRussell King /* Wait for pending flips to complete */ 61796f60e37SRussell King wait_event(dcrtc->frame_wait, !dcrtc->frame_work); 61896f60e37SRussell King 61996f60e37SRussell King /* Take a reference to the new fb as we're using it */ 620f4510a27SMatt Roper drm_framebuffer_reference(crtc->primary->fb); 62196f60e37SRussell King 62296f60e37SRussell King /* Update the base in the CRTC */ 62396f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 62496f60e37SRussell King 62596f60e37SRussell King /* Drop our previously held reference */ 62696f60e37SRussell King armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms)); 62796f60e37SRussell King 62896f60e37SRussell King return 0; 62996f60e37SRussell King } 63096f60e37SRussell King 63196f60e37SRussell King static void armada_drm_crtc_load_lut(struct drm_crtc *crtc) 63296f60e37SRussell King { 63396f60e37SRussell King } 63496f60e37SRussell King 63596f60e37SRussell King /* The mode_config.mutex will be held for this call */ 63696f60e37SRussell King static void armada_drm_crtc_disable(struct drm_crtc *crtc) 63796f60e37SRussell King { 63896f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 63996f60e37SRussell King 64096f60e37SRussell King armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); 641f4510a27SMatt Roper armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true); 64296f60e37SRussell King 64396f60e37SRussell King /* Power down most RAMs and FIFOs */ 64496f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 64596f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 64696f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 64796f60e37SRussell King } 64896f60e37SRussell King 64996f60e37SRussell King static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = { 65096f60e37SRussell King .dpms = armada_drm_crtc_dpms, 65196f60e37SRussell King .prepare = armada_drm_crtc_prepare, 65296f60e37SRussell King .commit = armada_drm_crtc_commit, 65396f60e37SRussell King .mode_fixup = armada_drm_crtc_mode_fixup, 65496f60e37SRussell King .mode_set = armada_drm_crtc_mode_set, 65596f60e37SRussell King .mode_set_base = armada_drm_crtc_mode_set_base, 65696f60e37SRussell King .load_lut = armada_drm_crtc_load_lut, 65796f60e37SRussell King .disable = armada_drm_crtc_disable, 65896f60e37SRussell King }; 65996f60e37SRussell King 660662af0d8SRussell King static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix, 661662af0d8SRussell King unsigned stride, unsigned width, unsigned height) 662662af0d8SRussell King { 663662af0d8SRussell King uint32_t addr; 664662af0d8SRussell King unsigned y; 665662af0d8SRussell King 666662af0d8SRussell King addr = SRAM_HWC32_RAM1; 667662af0d8SRussell King for (y = 0; y < height; y++) { 668662af0d8SRussell King uint32_t *p = &pix[y * stride]; 669662af0d8SRussell King unsigned x; 670662af0d8SRussell King 671662af0d8SRussell King for (x = 0; x < width; x++, p++) { 672662af0d8SRussell King uint32_t val = *p; 673662af0d8SRussell King 674662af0d8SRussell King val = (val & 0xff00ff00) | 675662af0d8SRussell King (val & 0x000000ff) << 16 | 676662af0d8SRussell King (val & 0x00ff0000) >> 16; 677662af0d8SRussell King 678662af0d8SRussell King writel_relaxed(val, 679662af0d8SRussell King base + LCD_SPU_SRAM_WRDAT); 680662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE, 681662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 682*c39b0695SRussell King readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN); 683662af0d8SRussell King addr += 1; 684662af0d8SRussell King if ((addr & 0x00ff) == 0) 685662af0d8SRussell King addr += 0xf00; 686662af0d8SRussell King if ((addr & 0x30ff) == 0) 687662af0d8SRussell King addr = SRAM_HWC32_RAM2; 688662af0d8SRussell King } 689662af0d8SRussell King } 690662af0d8SRussell King } 691662af0d8SRussell King 692662af0d8SRussell King static void armada_drm_crtc_cursor_tran(void __iomem *base) 693662af0d8SRussell King { 694662af0d8SRussell King unsigned addr; 695662af0d8SRussell King 696662af0d8SRussell King for (addr = 0; addr < 256; addr++) { 697662af0d8SRussell King /* write the default value */ 698662af0d8SRussell King writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT); 699662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN, 700662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 701662af0d8SRussell King } 702662af0d8SRussell King } 703662af0d8SRussell King 704662af0d8SRussell King static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload) 705662af0d8SRussell King { 706662af0d8SRussell King uint32_t xoff, xscr, w = dcrtc->cursor_w, s; 707662af0d8SRussell King uint32_t yoff, yscr, h = dcrtc->cursor_h; 708662af0d8SRussell King uint32_t para1; 709662af0d8SRussell King 710662af0d8SRussell King /* 711662af0d8SRussell King * Calculate the visible width and height of the cursor, 712662af0d8SRussell King * screen position, and the position in the cursor bitmap. 713662af0d8SRussell King */ 714662af0d8SRussell King if (dcrtc->cursor_x < 0) { 715662af0d8SRussell King xoff = -dcrtc->cursor_x; 716662af0d8SRussell King xscr = 0; 717662af0d8SRussell King w -= min(xoff, w); 718662af0d8SRussell King } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) { 719662af0d8SRussell King xoff = 0; 720662af0d8SRussell King xscr = dcrtc->cursor_x; 721662af0d8SRussell King w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0); 722662af0d8SRussell King } else { 723662af0d8SRussell King xoff = 0; 724662af0d8SRussell King xscr = dcrtc->cursor_x; 725662af0d8SRussell King } 726662af0d8SRussell King 727662af0d8SRussell King if (dcrtc->cursor_y < 0) { 728662af0d8SRussell King yoff = -dcrtc->cursor_y; 729662af0d8SRussell King yscr = 0; 730662af0d8SRussell King h -= min(yoff, h); 731662af0d8SRussell King } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) { 732662af0d8SRussell King yoff = 0; 733662af0d8SRussell King yscr = dcrtc->cursor_y; 734662af0d8SRussell King h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0); 735662af0d8SRussell King } else { 736662af0d8SRussell King yoff = 0; 737662af0d8SRussell King yscr = dcrtc->cursor_y; 738662af0d8SRussell King } 739662af0d8SRussell King 740662af0d8SRussell King /* On interlaced modes, the vertical cursor size must be halved */ 741662af0d8SRussell King s = dcrtc->cursor_w; 742662af0d8SRussell King if (dcrtc->interlaced) { 743662af0d8SRussell King s *= 2; 744662af0d8SRussell King yscr /= 2; 745662af0d8SRussell King h /= 2; 746662af0d8SRussell King } 747662af0d8SRussell King 748662af0d8SRussell King if (!dcrtc->cursor_obj || !h || !w) { 749662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 750662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 751662af0d8SRussell King dcrtc->cursor_update = false; 752662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 753662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 754662af0d8SRussell King return 0; 755662af0d8SRussell King } 756662af0d8SRussell King 757662af0d8SRussell King para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1); 758662af0d8SRussell King armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32, 759662af0d8SRussell King dcrtc->base + LCD_SPU_SRAM_PARA1); 760662af0d8SRussell King 761662af0d8SRussell King /* 762662af0d8SRussell King * Initialize the transparency if the SRAM was powered down. 763662af0d8SRussell King * We must also reload the cursor data as well. 764662af0d8SRussell King */ 765662af0d8SRussell King if (!(para1 & CFG_CSB_256x32)) { 766662af0d8SRussell King armada_drm_crtc_cursor_tran(dcrtc->base); 767662af0d8SRussell King reload = true; 768662af0d8SRussell King } 769662af0d8SRussell King 770662af0d8SRussell King if (dcrtc->cursor_hw_sz != (h << 16 | w)) { 771662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 772662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 773662af0d8SRussell King dcrtc->cursor_update = false; 774662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 775662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 776662af0d8SRussell King reload = true; 777662af0d8SRussell King } 778662af0d8SRussell King if (reload) { 779662af0d8SRussell King struct armada_gem_object *obj = dcrtc->cursor_obj; 780662af0d8SRussell King uint32_t *pix; 781662af0d8SRussell King /* Set the top-left corner of the cursor image */ 782662af0d8SRussell King pix = obj->addr; 783662af0d8SRussell King pix += yoff * s + xoff; 784662af0d8SRussell King armada_load_cursor_argb(dcrtc->base, pix, s, w, h); 785662af0d8SRussell King } 786662af0d8SRussell King 787662af0d8SRussell King /* Reload the cursor position, size and enable in the IRQ handler */ 788662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 789662af0d8SRussell King dcrtc->cursor_hw_pos = yscr << 16 | xscr; 790662af0d8SRussell King dcrtc->cursor_hw_sz = h << 16 | w; 791662af0d8SRussell King dcrtc->cursor_update = true; 792662af0d8SRussell King armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 793662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 794662af0d8SRussell King 795662af0d8SRussell King return 0; 796662af0d8SRussell King } 797662af0d8SRussell King 798662af0d8SRussell King static void cursor_update(void *data) 799662af0d8SRussell King { 800662af0d8SRussell King armada_drm_crtc_cursor_update(data, true); 801662af0d8SRussell King } 802662af0d8SRussell King 803662af0d8SRussell King static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc, 804662af0d8SRussell King struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h) 805662af0d8SRussell King { 806662af0d8SRussell King struct drm_device *dev = crtc->dev; 807662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 808662af0d8SRussell King struct armada_private *priv = crtc->dev->dev_private; 809662af0d8SRussell King struct armada_gem_object *obj = NULL; 810662af0d8SRussell King int ret; 811662af0d8SRussell King 812662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 813662af0d8SRussell King if (!priv->variant->has_spu_adv_reg) 814662af0d8SRussell King return -ENXIO; 815662af0d8SRussell King 816662af0d8SRussell King if (handle && w > 0 && h > 0) { 817662af0d8SRussell King /* maximum size is 64x32 or 32x64 */ 818662af0d8SRussell King if (w > 64 || h > 64 || (w > 32 && h > 32)) 819662af0d8SRussell King return -ENOMEM; 820662af0d8SRussell King 821662af0d8SRussell King obj = armada_gem_object_lookup(dev, file, handle); 822662af0d8SRussell King if (!obj) 823662af0d8SRussell King return -ENOENT; 824662af0d8SRussell King 825662af0d8SRussell King /* Must be a kernel-mapped object */ 826662af0d8SRussell King if (!obj->addr) { 827662af0d8SRussell King drm_gem_object_unreference_unlocked(&obj->obj); 828662af0d8SRussell King return -EINVAL; 829662af0d8SRussell King } 830662af0d8SRussell King 831662af0d8SRussell King if (obj->obj.size < w * h * 4) { 832662af0d8SRussell King DRM_ERROR("buffer is too small\n"); 833662af0d8SRussell King drm_gem_object_unreference_unlocked(&obj->obj); 834662af0d8SRussell King return -ENOMEM; 835662af0d8SRussell King } 836662af0d8SRussell King } 837662af0d8SRussell King 838662af0d8SRussell King mutex_lock(&dev->struct_mutex); 839662af0d8SRussell King if (dcrtc->cursor_obj) { 840662af0d8SRussell King dcrtc->cursor_obj->update = NULL; 841662af0d8SRussell King dcrtc->cursor_obj->update_data = NULL; 842662af0d8SRussell King drm_gem_object_unreference(&dcrtc->cursor_obj->obj); 843662af0d8SRussell King } 844662af0d8SRussell King dcrtc->cursor_obj = obj; 845662af0d8SRussell King dcrtc->cursor_w = w; 846662af0d8SRussell King dcrtc->cursor_h = h; 847662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, true); 848662af0d8SRussell King if (obj) { 849662af0d8SRussell King obj->update_data = dcrtc; 850662af0d8SRussell King obj->update = cursor_update; 851662af0d8SRussell King } 852662af0d8SRussell King mutex_unlock(&dev->struct_mutex); 853662af0d8SRussell King 854662af0d8SRussell King return ret; 855662af0d8SRussell King } 856662af0d8SRussell King 857662af0d8SRussell King static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y) 858662af0d8SRussell King { 859662af0d8SRussell King struct drm_device *dev = crtc->dev; 860662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 861662af0d8SRussell King struct armada_private *priv = crtc->dev->dev_private; 862662af0d8SRussell King int ret; 863662af0d8SRussell King 864662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 865662af0d8SRussell King if (!priv->variant->has_spu_adv_reg) 866662af0d8SRussell King return -EFAULT; 867662af0d8SRussell King 868662af0d8SRussell King mutex_lock(&dev->struct_mutex); 869662af0d8SRussell King dcrtc->cursor_x = x; 870662af0d8SRussell King dcrtc->cursor_y = y; 871662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, false); 872662af0d8SRussell King mutex_unlock(&dev->struct_mutex); 873662af0d8SRussell King 874662af0d8SRussell King return ret; 875662af0d8SRussell King } 876662af0d8SRussell King 87796f60e37SRussell King static void armada_drm_crtc_destroy(struct drm_crtc *crtc) 87896f60e37SRussell King { 87996f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 88096f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 88196f60e37SRussell King 882662af0d8SRussell King if (dcrtc->cursor_obj) 883662af0d8SRussell King drm_gem_object_unreference(&dcrtc->cursor_obj->obj); 884662af0d8SRussell King 88596f60e37SRussell King priv->dcrtc[dcrtc->num] = NULL; 88696f60e37SRussell King drm_crtc_cleanup(&dcrtc->crtc); 88796f60e37SRussell King 88896f60e37SRussell King if (!IS_ERR(dcrtc->clk)) 88996f60e37SRussell King clk_disable_unprepare(dcrtc->clk); 89096f60e37SRussell King 89196f60e37SRussell King kfree(dcrtc); 89296f60e37SRussell King } 89396f60e37SRussell King 89496f60e37SRussell King /* 89596f60e37SRussell King * The mode_config lock is held here, to prevent races between this 89696f60e37SRussell King * and a mode_set. 89796f60e37SRussell King */ 89896f60e37SRussell King static int armada_drm_crtc_page_flip(struct drm_crtc *crtc, 8995e4e3ba9SDave Airlie struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags) 90096f60e37SRussell King { 90196f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 90296f60e37SRussell King struct armada_frame_work *work; 90396f60e37SRussell King struct drm_device *dev = crtc->dev; 90496f60e37SRussell King unsigned long flags; 90596f60e37SRussell King unsigned i; 90696f60e37SRussell King int ret; 90796f60e37SRussell King 90896f60e37SRussell King /* We don't support changing the pixel format */ 909f4510a27SMatt Roper if (fb->pixel_format != crtc->primary->fb->pixel_format) 91096f60e37SRussell King return -EINVAL; 91196f60e37SRussell King 91296f60e37SRussell King work = kmalloc(sizeof(*work), GFP_KERNEL); 91396f60e37SRussell King if (!work) 91496f60e37SRussell King return -ENOMEM; 91596f60e37SRussell King 91696f60e37SRussell King work->event = event; 917f4510a27SMatt Roper work->old_fb = dcrtc->crtc.primary->fb; 91896f60e37SRussell King 91996f60e37SRussell King i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs, 92096f60e37SRussell King dcrtc->interlaced); 92196f60e37SRussell King armada_reg_queue_end(work->regs, i); 92296f60e37SRussell King 92396f60e37SRussell King /* 92496f60e37SRussell King * Hold the old framebuffer for the work - DRM appears to drop our 92596f60e37SRussell King * reference to the old framebuffer in drm_mode_page_flip_ioctl(). 92696f60e37SRussell King */ 92796f60e37SRussell King drm_framebuffer_reference(work->old_fb); 92896f60e37SRussell King 92996f60e37SRussell King ret = armada_drm_crtc_queue_frame_work(dcrtc, work); 93096f60e37SRussell King if (ret) { 93196f60e37SRussell King /* 93296f60e37SRussell King * Undo our reference above; DRM does not drop the reference 93396f60e37SRussell King * to this object on error, so that's okay. 93496f60e37SRussell King */ 93596f60e37SRussell King drm_framebuffer_unreference(work->old_fb); 93696f60e37SRussell King kfree(work); 93796f60e37SRussell King return ret; 93896f60e37SRussell King } 93996f60e37SRussell King 94096f60e37SRussell King /* 94196f60e37SRussell King * Don't take a reference on the new framebuffer; 94296f60e37SRussell King * drm_mode_page_flip_ioctl() has already grabbed a reference and 94396f60e37SRussell King * will _not_ drop that reference on successful return from this 94496f60e37SRussell King * function. Simply mark this new framebuffer as the current one. 94596f60e37SRussell King */ 946f4510a27SMatt Roper dcrtc->crtc.primary->fb = fb; 94796f60e37SRussell King 94896f60e37SRussell King /* 94996f60e37SRussell King * Finally, if the display is blanked, we won't receive an 95096f60e37SRussell King * interrupt, so complete it now. 95196f60e37SRussell King */ 95296f60e37SRussell King if (dpms_blanked(dcrtc->dpms)) { 95396f60e37SRussell King spin_lock_irqsave(&dev->event_lock, flags); 95496f60e37SRussell King if (dcrtc->frame_work) 95596f60e37SRussell King armada_drm_crtc_complete_frame_work(dcrtc); 95696f60e37SRussell King spin_unlock_irqrestore(&dev->event_lock, flags); 95796f60e37SRussell King } 95896f60e37SRussell King 95996f60e37SRussell King return 0; 96096f60e37SRussell King } 96196f60e37SRussell King 96296f60e37SRussell King static int 96396f60e37SRussell King armada_drm_crtc_set_property(struct drm_crtc *crtc, 96496f60e37SRussell King struct drm_property *property, uint64_t val) 96596f60e37SRussell King { 96696f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 96796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 96896f60e37SRussell King bool update_csc = false; 96996f60e37SRussell King 97096f60e37SRussell King if (property == priv->csc_yuv_prop) { 97196f60e37SRussell King dcrtc->csc_yuv_mode = val; 97296f60e37SRussell King update_csc = true; 97396f60e37SRussell King } else if (property == priv->csc_rgb_prop) { 97496f60e37SRussell King dcrtc->csc_rgb_mode = val; 97596f60e37SRussell King update_csc = true; 97696f60e37SRussell King } 97796f60e37SRussell King 97896f60e37SRussell King if (update_csc) { 97996f60e37SRussell King uint32_t val; 98096f60e37SRussell King 98196f60e37SRussell King val = dcrtc->spu_iopad_ctrl | 98296f60e37SRussell King armada_drm_crtc_calculate_csc(dcrtc); 98396f60e37SRussell King writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL); 98496f60e37SRussell King } 98596f60e37SRussell King 98696f60e37SRussell King return 0; 98796f60e37SRussell King } 98896f60e37SRussell King 98996f60e37SRussell King static struct drm_crtc_funcs armada_crtc_funcs = { 990662af0d8SRussell King .cursor_set = armada_drm_crtc_cursor_set, 991662af0d8SRussell King .cursor_move = armada_drm_crtc_cursor_move, 99296f60e37SRussell King .destroy = armada_drm_crtc_destroy, 99396f60e37SRussell King .set_config = drm_crtc_helper_set_config, 99496f60e37SRussell King .page_flip = armada_drm_crtc_page_flip, 99596f60e37SRussell King .set_property = armada_drm_crtc_set_property, 99696f60e37SRussell King }; 99796f60e37SRussell King 99896f60e37SRussell King static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = { 99996f60e37SRussell King { CSC_AUTO, "Auto" }, 100096f60e37SRussell King { CSC_YUV_CCIR601, "CCIR601" }, 100196f60e37SRussell King { CSC_YUV_CCIR709, "CCIR709" }, 100296f60e37SRussell King }; 100396f60e37SRussell King 100496f60e37SRussell King static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = { 100596f60e37SRussell King { CSC_AUTO, "Auto" }, 100696f60e37SRussell King { CSC_RGB_COMPUTER, "Computer system" }, 100796f60e37SRussell King { CSC_RGB_STUDIO, "Studio" }, 100896f60e37SRussell King }; 100996f60e37SRussell King 101096f60e37SRussell King static int armada_drm_crtc_create_properties(struct drm_device *dev) 101196f60e37SRussell King { 101296f60e37SRussell King struct armada_private *priv = dev->dev_private; 101396f60e37SRussell King 101496f60e37SRussell King if (priv->csc_yuv_prop) 101596f60e37SRussell King return 0; 101696f60e37SRussell King 101796f60e37SRussell King priv->csc_yuv_prop = drm_property_create_enum(dev, 0, 101896f60e37SRussell King "CSC_YUV", armada_drm_csc_yuv_enum_list, 101996f60e37SRussell King ARRAY_SIZE(armada_drm_csc_yuv_enum_list)); 102096f60e37SRussell King priv->csc_rgb_prop = drm_property_create_enum(dev, 0, 102196f60e37SRussell King "CSC_RGB", armada_drm_csc_rgb_enum_list, 102296f60e37SRussell King ARRAY_SIZE(armada_drm_csc_rgb_enum_list)); 102396f60e37SRussell King 102496f60e37SRussell King if (!priv->csc_yuv_prop || !priv->csc_rgb_prop) 102596f60e37SRussell King return -ENOMEM; 102696f60e37SRussell King 102796f60e37SRussell King return 0; 102896f60e37SRussell King } 102996f60e37SRussell King 103096f60e37SRussell King int armada_drm_crtc_create(struct drm_device *dev, unsigned num, 103196f60e37SRussell King struct resource *res) 103296f60e37SRussell King { 103396f60e37SRussell King struct armada_private *priv = dev->dev_private; 103496f60e37SRussell King struct armada_crtc *dcrtc; 103596f60e37SRussell King void __iomem *base; 103696f60e37SRussell King int ret; 103796f60e37SRussell King 103896f60e37SRussell King ret = armada_drm_crtc_create_properties(dev); 103996f60e37SRussell King if (ret) 104096f60e37SRussell King return ret; 104196f60e37SRussell King 104296f60e37SRussell King base = devm_request_and_ioremap(dev->dev, res); 104396f60e37SRussell King if (!base) { 104496f60e37SRussell King DRM_ERROR("failed to ioremap register\n"); 104596f60e37SRussell King return -ENOMEM; 104696f60e37SRussell King } 104796f60e37SRussell King 104896f60e37SRussell King dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL); 104996f60e37SRussell King if (!dcrtc) { 105096f60e37SRussell King DRM_ERROR("failed to allocate Armada crtc\n"); 105196f60e37SRussell King return -ENOMEM; 105296f60e37SRussell King } 105396f60e37SRussell King 105496f60e37SRussell King dcrtc->base = base; 105596f60e37SRussell King dcrtc->num = num; 105696f60e37SRussell King dcrtc->clk = ERR_PTR(-EINVAL); 105796f60e37SRussell King dcrtc->csc_yuv_mode = CSC_AUTO; 105896f60e37SRussell King dcrtc->csc_rgb_mode = CSC_AUTO; 105996f60e37SRussell King dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0; 106096f60e37SRussell King dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24; 106196f60e37SRussell King spin_lock_init(&dcrtc->irq_lock); 106296f60e37SRussell King dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR; 106396f60e37SRussell King INIT_LIST_HEAD(&dcrtc->vbl_list); 106496f60e37SRussell King init_waitqueue_head(&dcrtc->frame_wait); 106596f60e37SRussell King 106696f60e37SRussell King /* Initialize some registers which we don't otherwise set */ 106796f60e37SRussell King writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV); 106896f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR); 106996f60e37SRussell King writel_relaxed(dcrtc->spu_iopad_ctrl, 107096f60e37SRussell King dcrtc->base + LCD_SPU_IOPAD_CONTROL); 107196f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0); 107296f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 107396f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 107496f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 107596f60e37SRussell King writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1); 107696f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN); 107796f60e37SRussell King 107896f60e37SRussell King if (priv->variant->crtc_init) { 107996f60e37SRussell King ret = priv->variant->crtc_init(dcrtc); 108096f60e37SRussell King if (ret) { 108196f60e37SRussell King kfree(dcrtc); 108296f60e37SRussell King return ret; 108396f60e37SRussell King } 108496f60e37SRussell King } 108596f60e37SRussell King 108696f60e37SRussell King /* Ensure AXI pipeline is enabled */ 108796f60e37SRussell King armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0); 108896f60e37SRussell King 108996f60e37SRussell King priv->dcrtc[dcrtc->num] = dcrtc; 109096f60e37SRussell King 109196f60e37SRussell King drm_crtc_init(dev, &dcrtc->crtc, &armada_crtc_funcs); 109296f60e37SRussell King drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs); 109396f60e37SRussell King 109496f60e37SRussell King drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop, 109596f60e37SRussell King dcrtc->csc_yuv_mode); 109696f60e37SRussell King drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop, 109796f60e37SRussell King dcrtc->csc_rgb_mode); 109896f60e37SRussell King 109996f60e37SRussell King return armada_overlay_plane_create(dev, 1 << dcrtc->num); 110096f60e37SRussell King } 1101