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; 384*662af0d8SRussell King writel_relaxed(val, base + LCD_SPU_ADV_REG); 38596f60e37SRussell King } 386*662af0d8SRussell King 387*662af0d8SRussell King if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) { 388*662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_pos, 389*662af0d8SRussell King base + LCD_SPU_HWC_OVSA_HPXL_VLN); 390*662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_sz, 391*662af0d8SRussell King base + LCD_SPU_HWC_HPXL_VLN); 392*662af0d8SRussell King armada_updatel(CFG_HWC_ENA, 393*662af0d8SRussell King CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA, 394*662af0d8SRussell King base + LCD_SPU_DMA_CTRL0); 395*662af0d8SRussell King dcrtc->cursor_update = false; 396*662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 397*662af0d8SRussell King } 398*662af0d8SRussell 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 48196f60e37SRussell King drm_framebuffer_reference(crtc->fb); 48296f60e37SRussell King 48396f60e37SRussell King interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE); 48496f60e37SRussell King 48596f60e37SRussell King i = armada_drm_crtc_calc_fb(dcrtc->crtc.fb, x, y, regs, interlaced); 48696f60e37SRussell King 48796f60e37SRussell King rm = adj->crtc_hsync_start - adj->crtc_hdisplay; 48896f60e37SRussell King lm = adj->crtc_htotal - adj->crtc_hsync_end; 48996f60e37SRussell King bm = adj->crtc_vsync_start - adj->crtc_vdisplay; 49096f60e37SRussell King tm = adj->crtc_vtotal - adj->crtc_vsync_end; 49196f60e37SRussell King 49296f60e37SRussell King DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n", 49396f60e37SRussell King adj->crtc_hdisplay, 49496f60e37SRussell King adj->crtc_hsync_start, 49596f60e37SRussell King adj->crtc_hsync_end, 49696f60e37SRussell King adj->crtc_htotal, lm, rm); 49796f60e37SRussell King DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n", 49896f60e37SRussell King adj->crtc_vdisplay, 49996f60e37SRussell King adj->crtc_vsync_start, 50096f60e37SRussell King adj->crtc_vsync_end, 50196f60e37SRussell King adj->crtc_vtotal, tm, bm); 50296f60e37SRussell King 50396f60e37SRussell King /* Wait for pending flips to complete */ 50496f60e37SRussell King wait_event(dcrtc->frame_wait, !dcrtc->frame_work); 50596f60e37SRussell King 50696f60e37SRussell King drm_vblank_pre_modeset(crtc->dev, dcrtc->num); 50796f60e37SRussell King 50896f60e37SRussell King crtc->mode = *adj; 50996f60e37SRussell King 51096f60e37SRussell King val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA; 51196f60e37SRussell King if (val != dcrtc->dumb_ctrl) { 51296f60e37SRussell King dcrtc->dumb_ctrl = val; 51396f60e37SRussell King writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL); 51496f60e37SRussell King } 51596f60e37SRussell King 51696f60e37SRussell King /* Now compute the divider for real */ 51796f60e37SRussell King priv->variant->crtc_compute_clock(dcrtc, adj, &sclk); 51896f60e37SRussell King 51996f60e37SRussell King /* Ensure graphic fifo is enabled */ 52096f60e37SRussell King armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1); 52196f60e37SRussell King armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV); 52296f60e37SRussell King 52396f60e37SRussell King if (interlaced ^ dcrtc->interlaced) { 52496f60e37SRussell King if (adj->flags & DRM_MODE_FLAG_INTERLACE) 52596f60e37SRussell King drm_vblank_get(dcrtc->crtc.dev, dcrtc->num); 52696f60e37SRussell King else 52796f60e37SRussell King drm_vblank_put(dcrtc->crtc.dev, dcrtc->num); 52896f60e37SRussell King dcrtc->interlaced = interlaced; 52996f60e37SRussell King } 53096f60e37SRussell King 53196f60e37SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 53296f60e37SRussell King 53396f60e37SRussell King /* Even interlaced/progressive frame */ 53496f60e37SRussell King dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 | 53596f60e37SRussell King adj->crtc_htotal; 53696f60e37SRussell King dcrtc->v[1].spu_v_porch = tm << 16 | bm; 53796f60e37SRussell King val = adj->crtc_hsync_start; 538*662af0d8SRussell King dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN | 539*662af0d8SRussell King priv->variant->spu_adv_reg; 54096f60e37SRussell King 54196f60e37SRussell King if (interlaced) { 54296f60e37SRussell King /* Odd interlaced frame */ 54396f60e37SRussell King dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total + 54496f60e37SRussell King (1 << 16); 54596f60e37SRussell King dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1; 54696f60e37SRussell King val = adj->crtc_hsync_start - adj->crtc_htotal / 2; 547*662af0d8SRussell King dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN | 548*662af0d8SRussell King priv->variant->spu_adv_reg; 54996f60e37SRussell King } else { 55096f60e37SRussell King dcrtc->v[0] = dcrtc->v[1]; 55196f60e37SRussell King } 55296f60e37SRussell King 55396f60e37SRussell King val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay; 55496f60e37SRussell King 55596f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE); 55696f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN); 55796f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN); 55896f60e37SRussell King armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH); 55996f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH); 56096f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total, 56196f60e37SRussell King LCD_SPUT_V_H_TOTAL); 56296f60e37SRussell King 563*662af0d8SRussell King if (priv->variant->has_spu_adv_reg) { 56496f60e37SRussell King armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg, 56596f60e37SRussell King ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | 56696f60e37SRussell King ADV_VSYNCOFFEN, LCD_SPU_ADV_REG); 567*662af0d8SRussell King } 56896f60e37SRussell King 56996f60e37SRussell King val = CFG_GRA_ENA | CFG_GRA_HSMOOTH; 57096f60e37SRussell King val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.fb)->fmt); 57196f60e37SRussell King val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.fb)->mod); 57296f60e37SRussell King 57396f60e37SRussell King if (drm_fb_to_armada_fb(dcrtc->crtc.fb)->fmt > CFG_420) 57496f60e37SRussell King val |= CFG_PALETTE_ENA; 57596f60e37SRussell King 57696f60e37SRussell King if (interlaced) 57796f60e37SRussell King val |= CFG_GRA_FTOGGLE; 57896f60e37SRussell King 57996f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT | 58096f60e37SRussell King CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV | 58196f60e37SRussell King CFG_SWAPYU | CFG_YUV2RGB) | 58296f60e37SRussell King CFG_PALETTE_ENA | CFG_GRA_FTOGGLE, 58396f60e37SRussell King LCD_SPU_DMA_CTRL0); 58496f60e37SRussell King 58596f60e37SRussell King val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0; 58696f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1); 58796f60e37SRussell King 58896f60e37SRussell King val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc); 58996f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL); 59096f60e37SRussell King armada_reg_queue_end(regs, i); 59196f60e37SRussell King 59296f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 59396f60e37SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 59496f60e37SRussell King 59596f60e37SRussell King armada_drm_crtc_update(dcrtc); 59696f60e37SRussell King 59796f60e37SRussell King drm_vblank_post_modeset(crtc->dev, dcrtc->num); 59896f60e37SRussell King armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms)); 59996f60e37SRussell King 60096f60e37SRussell King return 0; 60196f60e37SRussell King } 60296f60e37SRussell King 60396f60e37SRussell King /* The mode_config.mutex will be held for this call */ 60496f60e37SRussell King static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, 60596f60e37SRussell King struct drm_framebuffer *old_fb) 60696f60e37SRussell King { 60796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 60896f60e37SRussell King struct armada_regs regs[4]; 60996f60e37SRussell King unsigned i; 61096f60e37SRussell King 61196f60e37SRussell King i = armada_drm_crtc_calc_fb(crtc->fb, crtc->x, crtc->y, regs, 61296f60e37SRussell King dcrtc->interlaced); 61396f60e37SRussell King armada_reg_queue_end(regs, i); 61496f60e37SRussell King 61596f60e37SRussell King /* Wait for pending flips to complete */ 61696f60e37SRussell King wait_event(dcrtc->frame_wait, !dcrtc->frame_work); 61796f60e37SRussell King 61896f60e37SRussell King /* Take a reference to the new fb as we're using it */ 61996f60e37SRussell King drm_framebuffer_reference(crtc->fb); 62096f60e37SRussell King 62196f60e37SRussell King /* Update the base in the CRTC */ 62296f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 62396f60e37SRussell King 62496f60e37SRussell King /* Drop our previously held reference */ 62596f60e37SRussell King armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms)); 62696f60e37SRussell King 62796f60e37SRussell King return 0; 62896f60e37SRussell King } 62996f60e37SRussell King 63096f60e37SRussell King static void armada_drm_crtc_load_lut(struct drm_crtc *crtc) 63196f60e37SRussell King { 63296f60e37SRussell King } 63396f60e37SRussell King 63496f60e37SRussell King /* The mode_config.mutex will be held for this call */ 63596f60e37SRussell King static void armada_drm_crtc_disable(struct drm_crtc *crtc) 63696f60e37SRussell King { 63796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 63896f60e37SRussell King 63996f60e37SRussell King armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); 64096f60e37SRussell King armada_drm_crtc_finish_fb(dcrtc, crtc->fb, true); 64196f60e37SRussell King 64296f60e37SRussell King /* Power down most RAMs and FIFOs */ 64396f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 64496f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 64596f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 64696f60e37SRussell King } 64796f60e37SRussell King 64896f60e37SRussell King static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = { 64996f60e37SRussell King .dpms = armada_drm_crtc_dpms, 65096f60e37SRussell King .prepare = armada_drm_crtc_prepare, 65196f60e37SRussell King .commit = armada_drm_crtc_commit, 65296f60e37SRussell King .mode_fixup = armada_drm_crtc_mode_fixup, 65396f60e37SRussell King .mode_set = armada_drm_crtc_mode_set, 65496f60e37SRussell King .mode_set_base = armada_drm_crtc_mode_set_base, 65596f60e37SRussell King .load_lut = armada_drm_crtc_load_lut, 65696f60e37SRussell King .disable = armada_drm_crtc_disable, 65796f60e37SRussell King }; 65896f60e37SRussell King 659*662af0d8SRussell King static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix, 660*662af0d8SRussell King unsigned stride, unsigned width, unsigned height) 661*662af0d8SRussell King { 662*662af0d8SRussell King uint32_t addr; 663*662af0d8SRussell King unsigned y; 664*662af0d8SRussell King 665*662af0d8SRussell King addr = SRAM_HWC32_RAM1; 666*662af0d8SRussell King for (y = 0; y < height; y++) { 667*662af0d8SRussell King uint32_t *p = &pix[y * stride]; 668*662af0d8SRussell King unsigned x; 669*662af0d8SRussell King 670*662af0d8SRussell King for (x = 0; x < width; x++, p++) { 671*662af0d8SRussell King uint32_t val = *p; 672*662af0d8SRussell King 673*662af0d8SRussell King val = (val & 0xff00ff00) | 674*662af0d8SRussell King (val & 0x000000ff) << 16 | 675*662af0d8SRussell King (val & 0x00ff0000) >> 16; 676*662af0d8SRussell King 677*662af0d8SRussell King writel_relaxed(val, 678*662af0d8SRussell King base + LCD_SPU_SRAM_WRDAT); 679*662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE, 680*662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 681*662af0d8SRussell King addr += 1; 682*662af0d8SRussell King if ((addr & 0x00ff) == 0) 683*662af0d8SRussell King addr += 0xf00; 684*662af0d8SRussell King if ((addr & 0x30ff) == 0) 685*662af0d8SRussell King addr = SRAM_HWC32_RAM2; 686*662af0d8SRussell King } 687*662af0d8SRussell King } 688*662af0d8SRussell King } 689*662af0d8SRussell King 690*662af0d8SRussell King static void armada_drm_crtc_cursor_tran(void __iomem *base) 691*662af0d8SRussell King { 692*662af0d8SRussell King unsigned addr; 693*662af0d8SRussell King 694*662af0d8SRussell King for (addr = 0; addr < 256; addr++) { 695*662af0d8SRussell King /* write the default value */ 696*662af0d8SRussell King writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT); 697*662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN, 698*662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 699*662af0d8SRussell King } 700*662af0d8SRussell King } 701*662af0d8SRussell King 702*662af0d8SRussell King static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload) 703*662af0d8SRussell King { 704*662af0d8SRussell King uint32_t xoff, xscr, w = dcrtc->cursor_w, s; 705*662af0d8SRussell King uint32_t yoff, yscr, h = dcrtc->cursor_h; 706*662af0d8SRussell King uint32_t para1; 707*662af0d8SRussell King 708*662af0d8SRussell King /* 709*662af0d8SRussell King * Calculate the visible width and height of the cursor, 710*662af0d8SRussell King * screen position, and the position in the cursor bitmap. 711*662af0d8SRussell King */ 712*662af0d8SRussell King if (dcrtc->cursor_x < 0) { 713*662af0d8SRussell King xoff = -dcrtc->cursor_x; 714*662af0d8SRussell King xscr = 0; 715*662af0d8SRussell King w -= min(xoff, w); 716*662af0d8SRussell King } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) { 717*662af0d8SRussell King xoff = 0; 718*662af0d8SRussell King xscr = dcrtc->cursor_x; 719*662af0d8SRussell King w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0); 720*662af0d8SRussell King } else { 721*662af0d8SRussell King xoff = 0; 722*662af0d8SRussell King xscr = dcrtc->cursor_x; 723*662af0d8SRussell King } 724*662af0d8SRussell King 725*662af0d8SRussell King if (dcrtc->cursor_y < 0) { 726*662af0d8SRussell King yoff = -dcrtc->cursor_y; 727*662af0d8SRussell King yscr = 0; 728*662af0d8SRussell King h -= min(yoff, h); 729*662af0d8SRussell King } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) { 730*662af0d8SRussell King yoff = 0; 731*662af0d8SRussell King yscr = dcrtc->cursor_y; 732*662af0d8SRussell King h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0); 733*662af0d8SRussell King } else { 734*662af0d8SRussell King yoff = 0; 735*662af0d8SRussell King yscr = dcrtc->cursor_y; 736*662af0d8SRussell King } 737*662af0d8SRussell King 738*662af0d8SRussell King /* On interlaced modes, the vertical cursor size must be halved */ 739*662af0d8SRussell King s = dcrtc->cursor_w; 740*662af0d8SRussell King if (dcrtc->interlaced) { 741*662af0d8SRussell King s *= 2; 742*662af0d8SRussell King yscr /= 2; 743*662af0d8SRussell King h /= 2; 744*662af0d8SRussell King } 745*662af0d8SRussell King 746*662af0d8SRussell King if (!dcrtc->cursor_obj || !h || !w) { 747*662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 748*662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 749*662af0d8SRussell King dcrtc->cursor_update = false; 750*662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 751*662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 752*662af0d8SRussell King return 0; 753*662af0d8SRussell King } 754*662af0d8SRussell King 755*662af0d8SRussell King para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1); 756*662af0d8SRussell King armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32, 757*662af0d8SRussell King dcrtc->base + LCD_SPU_SRAM_PARA1); 758*662af0d8SRussell King 759*662af0d8SRussell King /* 760*662af0d8SRussell King * Initialize the transparency if the SRAM was powered down. 761*662af0d8SRussell King * We must also reload the cursor data as well. 762*662af0d8SRussell King */ 763*662af0d8SRussell King if (!(para1 & CFG_CSB_256x32)) { 764*662af0d8SRussell King armada_drm_crtc_cursor_tran(dcrtc->base); 765*662af0d8SRussell King reload = true; 766*662af0d8SRussell King } 767*662af0d8SRussell King 768*662af0d8SRussell King if (dcrtc->cursor_hw_sz != (h << 16 | w)) { 769*662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 770*662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 771*662af0d8SRussell King dcrtc->cursor_update = false; 772*662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 773*662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 774*662af0d8SRussell King reload = true; 775*662af0d8SRussell King } 776*662af0d8SRussell King if (reload) { 777*662af0d8SRussell King struct armada_gem_object *obj = dcrtc->cursor_obj; 778*662af0d8SRussell King uint32_t *pix; 779*662af0d8SRussell King /* Set the top-left corner of the cursor image */ 780*662af0d8SRussell King pix = obj->addr; 781*662af0d8SRussell King pix += yoff * s + xoff; 782*662af0d8SRussell King armada_load_cursor_argb(dcrtc->base, pix, s, w, h); 783*662af0d8SRussell King } 784*662af0d8SRussell King 785*662af0d8SRussell King /* Reload the cursor position, size and enable in the IRQ handler */ 786*662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 787*662af0d8SRussell King dcrtc->cursor_hw_pos = yscr << 16 | xscr; 788*662af0d8SRussell King dcrtc->cursor_hw_sz = h << 16 | w; 789*662af0d8SRussell King dcrtc->cursor_update = true; 790*662af0d8SRussell King armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 791*662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 792*662af0d8SRussell King 793*662af0d8SRussell King return 0; 794*662af0d8SRussell King } 795*662af0d8SRussell King 796*662af0d8SRussell King static void cursor_update(void *data) 797*662af0d8SRussell King { 798*662af0d8SRussell King armada_drm_crtc_cursor_update(data, true); 799*662af0d8SRussell King } 800*662af0d8SRussell King 801*662af0d8SRussell King static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc, 802*662af0d8SRussell King struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h) 803*662af0d8SRussell King { 804*662af0d8SRussell King struct drm_device *dev = crtc->dev; 805*662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 806*662af0d8SRussell King struct armada_private *priv = crtc->dev->dev_private; 807*662af0d8SRussell King struct armada_gem_object *obj = NULL; 808*662af0d8SRussell King int ret; 809*662af0d8SRussell King 810*662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 811*662af0d8SRussell King if (!priv->variant->has_spu_adv_reg) 812*662af0d8SRussell King return -ENXIO; 813*662af0d8SRussell King 814*662af0d8SRussell King if (handle && w > 0 && h > 0) { 815*662af0d8SRussell King /* maximum size is 64x32 or 32x64 */ 816*662af0d8SRussell King if (w > 64 || h > 64 || (w > 32 && h > 32)) 817*662af0d8SRussell King return -ENOMEM; 818*662af0d8SRussell King 819*662af0d8SRussell King obj = armada_gem_object_lookup(dev, file, handle); 820*662af0d8SRussell King if (!obj) 821*662af0d8SRussell King return -ENOENT; 822*662af0d8SRussell King 823*662af0d8SRussell King /* Must be a kernel-mapped object */ 824*662af0d8SRussell King if (!obj->addr) { 825*662af0d8SRussell King drm_gem_object_unreference_unlocked(&obj->obj); 826*662af0d8SRussell King return -EINVAL; 827*662af0d8SRussell King } 828*662af0d8SRussell King 829*662af0d8SRussell King if (obj->obj.size < w * h * 4) { 830*662af0d8SRussell King DRM_ERROR("buffer is too small\n"); 831*662af0d8SRussell King drm_gem_object_unreference_unlocked(&obj->obj); 832*662af0d8SRussell King return -ENOMEM; 833*662af0d8SRussell King } 834*662af0d8SRussell King } 835*662af0d8SRussell King 836*662af0d8SRussell King mutex_lock(&dev->struct_mutex); 837*662af0d8SRussell King if (dcrtc->cursor_obj) { 838*662af0d8SRussell King dcrtc->cursor_obj->update = NULL; 839*662af0d8SRussell King dcrtc->cursor_obj->update_data = NULL; 840*662af0d8SRussell King drm_gem_object_unreference(&dcrtc->cursor_obj->obj); 841*662af0d8SRussell King } 842*662af0d8SRussell King dcrtc->cursor_obj = obj; 843*662af0d8SRussell King dcrtc->cursor_w = w; 844*662af0d8SRussell King dcrtc->cursor_h = h; 845*662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, true); 846*662af0d8SRussell King if (obj) { 847*662af0d8SRussell King obj->update_data = dcrtc; 848*662af0d8SRussell King obj->update = cursor_update; 849*662af0d8SRussell King } 850*662af0d8SRussell King mutex_unlock(&dev->struct_mutex); 851*662af0d8SRussell King 852*662af0d8SRussell King return ret; 853*662af0d8SRussell King } 854*662af0d8SRussell King 855*662af0d8SRussell King static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y) 856*662af0d8SRussell King { 857*662af0d8SRussell King struct drm_device *dev = crtc->dev; 858*662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 859*662af0d8SRussell King struct armada_private *priv = crtc->dev->dev_private; 860*662af0d8SRussell King int ret; 861*662af0d8SRussell King 862*662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 863*662af0d8SRussell King if (!priv->variant->has_spu_adv_reg) 864*662af0d8SRussell King return -EFAULT; 865*662af0d8SRussell King 866*662af0d8SRussell King mutex_lock(&dev->struct_mutex); 867*662af0d8SRussell King dcrtc->cursor_x = x; 868*662af0d8SRussell King dcrtc->cursor_y = y; 869*662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, false); 870*662af0d8SRussell King mutex_unlock(&dev->struct_mutex); 871*662af0d8SRussell King 872*662af0d8SRussell King return ret; 873*662af0d8SRussell King } 874*662af0d8SRussell King 87596f60e37SRussell King static void armada_drm_crtc_destroy(struct drm_crtc *crtc) 87696f60e37SRussell King { 87796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 87896f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 87996f60e37SRussell King 880*662af0d8SRussell King if (dcrtc->cursor_obj) 881*662af0d8SRussell King drm_gem_object_unreference(&dcrtc->cursor_obj->obj); 882*662af0d8SRussell King 88396f60e37SRussell King priv->dcrtc[dcrtc->num] = NULL; 88496f60e37SRussell King drm_crtc_cleanup(&dcrtc->crtc); 88596f60e37SRussell King 88696f60e37SRussell King if (!IS_ERR(dcrtc->clk)) 88796f60e37SRussell King clk_disable_unprepare(dcrtc->clk); 88896f60e37SRussell King 88996f60e37SRussell King kfree(dcrtc); 89096f60e37SRussell King } 89196f60e37SRussell King 89296f60e37SRussell King /* 89396f60e37SRussell King * The mode_config lock is held here, to prevent races between this 89496f60e37SRussell King * and a mode_set. 89596f60e37SRussell King */ 89696f60e37SRussell King static int armada_drm_crtc_page_flip(struct drm_crtc *crtc, 89796f60e37SRussell King struct drm_framebuffer *fb, struct drm_pending_vblank_event *event) 89896f60e37SRussell King { 89996f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 90096f60e37SRussell King struct armada_frame_work *work; 90196f60e37SRussell King struct drm_device *dev = crtc->dev; 90296f60e37SRussell King unsigned long flags; 90396f60e37SRussell King unsigned i; 90496f60e37SRussell King int ret; 90596f60e37SRussell King 90696f60e37SRussell King /* We don't support changing the pixel format */ 90796f60e37SRussell King if (fb->pixel_format != crtc->fb->pixel_format) 90896f60e37SRussell King return -EINVAL; 90996f60e37SRussell King 91096f60e37SRussell King work = kmalloc(sizeof(*work), GFP_KERNEL); 91196f60e37SRussell King if (!work) 91296f60e37SRussell King return -ENOMEM; 91396f60e37SRussell King 91496f60e37SRussell King work->event = event; 91596f60e37SRussell King work->old_fb = dcrtc->crtc.fb; 91696f60e37SRussell King 91796f60e37SRussell King i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs, 91896f60e37SRussell King dcrtc->interlaced); 91996f60e37SRussell King armada_reg_queue_end(work->regs, i); 92096f60e37SRussell King 92196f60e37SRussell King /* 92296f60e37SRussell King * Hold the old framebuffer for the work - DRM appears to drop our 92396f60e37SRussell King * reference to the old framebuffer in drm_mode_page_flip_ioctl(). 92496f60e37SRussell King */ 92596f60e37SRussell King drm_framebuffer_reference(work->old_fb); 92696f60e37SRussell King 92796f60e37SRussell King ret = armada_drm_crtc_queue_frame_work(dcrtc, work); 92896f60e37SRussell King if (ret) { 92996f60e37SRussell King /* 93096f60e37SRussell King * Undo our reference above; DRM does not drop the reference 93196f60e37SRussell King * to this object on error, so that's okay. 93296f60e37SRussell King */ 93396f60e37SRussell King drm_framebuffer_unreference(work->old_fb); 93496f60e37SRussell King kfree(work); 93596f60e37SRussell King return ret; 93696f60e37SRussell King } 93796f60e37SRussell King 93896f60e37SRussell King /* 93996f60e37SRussell King * Don't take a reference on the new framebuffer; 94096f60e37SRussell King * drm_mode_page_flip_ioctl() has already grabbed a reference and 94196f60e37SRussell King * will _not_ drop that reference on successful return from this 94296f60e37SRussell King * function. Simply mark this new framebuffer as the current one. 94396f60e37SRussell King */ 94496f60e37SRussell King dcrtc->crtc.fb = fb; 94596f60e37SRussell King 94696f60e37SRussell King /* 94796f60e37SRussell King * Finally, if the display is blanked, we won't receive an 94896f60e37SRussell King * interrupt, so complete it now. 94996f60e37SRussell King */ 95096f60e37SRussell King if (dpms_blanked(dcrtc->dpms)) { 95196f60e37SRussell King spin_lock_irqsave(&dev->event_lock, flags); 95296f60e37SRussell King if (dcrtc->frame_work) 95396f60e37SRussell King armada_drm_crtc_complete_frame_work(dcrtc); 95496f60e37SRussell King spin_unlock_irqrestore(&dev->event_lock, flags); 95596f60e37SRussell King } 95696f60e37SRussell King 95796f60e37SRussell King return 0; 95896f60e37SRussell King } 95996f60e37SRussell King 96096f60e37SRussell King static int 96196f60e37SRussell King armada_drm_crtc_set_property(struct drm_crtc *crtc, 96296f60e37SRussell King struct drm_property *property, uint64_t val) 96396f60e37SRussell King { 96496f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 96596f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 96696f60e37SRussell King bool update_csc = false; 96796f60e37SRussell King 96896f60e37SRussell King if (property == priv->csc_yuv_prop) { 96996f60e37SRussell King dcrtc->csc_yuv_mode = val; 97096f60e37SRussell King update_csc = true; 97196f60e37SRussell King } else if (property == priv->csc_rgb_prop) { 97296f60e37SRussell King dcrtc->csc_rgb_mode = val; 97396f60e37SRussell King update_csc = true; 97496f60e37SRussell King } 97596f60e37SRussell King 97696f60e37SRussell King if (update_csc) { 97796f60e37SRussell King uint32_t val; 97896f60e37SRussell King 97996f60e37SRussell King val = dcrtc->spu_iopad_ctrl | 98096f60e37SRussell King armada_drm_crtc_calculate_csc(dcrtc); 98196f60e37SRussell King writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL); 98296f60e37SRussell King } 98396f60e37SRussell King 98496f60e37SRussell King return 0; 98596f60e37SRussell King } 98696f60e37SRussell King 98796f60e37SRussell King static struct drm_crtc_funcs armada_crtc_funcs = { 988*662af0d8SRussell King .cursor_set = armada_drm_crtc_cursor_set, 989*662af0d8SRussell King .cursor_move = armada_drm_crtc_cursor_move, 99096f60e37SRussell King .destroy = armada_drm_crtc_destroy, 99196f60e37SRussell King .set_config = drm_crtc_helper_set_config, 99296f60e37SRussell King .page_flip = armada_drm_crtc_page_flip, 99396f60e37SRussell King .set_property = armada_drm_crtc_set_property, 99496f60e37SRussell King }; 99596f60e37SRussell King 99696f60e37SRussell King static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = { 99796f60e37SRussell King { CSC_AUTO, "Auto" }, 99896f60e37SRussell King { CSC_YUV_CCIR601, "CCIR601" }, 99996f60e37SRussell King { CSC_YUV_CCIR709, "CCIR709" }, 100096f60e37SRussell King }; 100196f60e37SRussell King 100296f60e37SRussell King static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = { 100396f60e37SRussell King { CSC_AUTO, "Auto" }, 100496f60e37SRussell King { CSC_RGB_COMPUTER, "Computer system" }, 100596f60e37SRussell King { CSC_RGB_STUDIO, "Studio" }, 100696f60e37SRussell King }; 100796f60e37SRussell King 100896f60e37SRussell King static int armada_drm_crtc_create_properties(struct drm_device *dev) 100996f60e37SRussell King { 101096f60e37SRussell King struct armada_private *priv = dev->dev_private; 101196f60e37SRussell King 101296f60e37SRussell King if (priv->csc_yuv_prop) 101396f60e37SRussell King return 0; 101496f60e37SRussell King 101596f60e37SRussell King priv->csc_yuv_prop = drm_property_create_enum(dev, 0, 101696f60e37SRussell King "CSC_YUV", armada_drm_csc_yuv_enum_list, 101796f60e37SRussell King ARRAY_SIZE(armada_drm_csc_yuv_enum_list)); 101896f60e37SRussell King priv->csc_rgb_prop = drm_property_create_enum(dev, 0, 101996f60e37SRussell King "CSC_RGB", armada_drm_csc_rgb_enum_list, 102096f60e37SRussell King ARRAY_SIZE(armada_drm_csc_rgb_enum_list)); 102196f60e37SRussell King 102296f60e37SRussell King if (!priv->csc_yuv_prop || !priv->csc_rgb_prop) 102396f60e37SRussell King return -ENOMEM; 102496f60e37SRussell King 102596f60e37SRussell King return 0; 102696f60e37SRussell King } 102796f60e37SRussell King 102896f60e37SRussell King int armada_drm_crtc_create(struct drm_device *dev, unsigned num, 102996f60e37SRussell King struct resource *res) 103096f60e37SRussell King { 103196f60e37SRussell King struct armada_private *priv = dev->dev_private; 103296f60e37SRussell King struct armada_crtc *dcrtc; 103396f60e37SRussell King void __iomem *base; 103496f60e37SRussell King int ret; 103596f60e37SRussell King 103696f60e37SRussell King ret = armada_drm_crtc_create_properties(dev); 103796f60e37SRussell King if (ret) 103896f60e37SRussell King return ret; 103996f60e37SRussell King 104096f60e37SRussell King base = devm_request_and_ioremap(dev->dev, res); 104196f60e37SRussell King if (!base) { 104296f60e37SRussell King DRM_ERROR("failed to ioremap register\n"); 104396f60e37SRussell King return -ENOMEM; 104496f60e37SRussell King } 104596f60e37SRussell King 104696f60e37SRussell King dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL); 104796f60e37SRussell King if (!dcrtc) { 104896f60e37SRussell King DRM_ERROR("failed to allocate Armada crtc\n"); 104996f60e37SRussell King return -ENOMEM; 105096f60e37SRussell King } 105196f60e37SRussell King 105296f60e37SRussell King dcrtc->base = base; 105396f60e37SRussell King dcrtc->num = num; 105496f60e37SRussell King dcrtc->clk = ERR_PTR(-EINVAL); 105596f60e37SRussell King dcrtc->csc_yuv_mode = CSC_AUTO; 105696f60e37SRussell King dcrtc->csc_rgb_mode = CSC_AUTO; 105796f60e37SRussell King dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0; 105896f60e37SRussell King dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24; 105996f60e37SRussell King spin_lock_init(&dcrtc->irq_lock); 106096f60e37SRussell King dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR; 106196f60e37SRussell King INIT_LIST_HEAD(&dcrtc->vbl_list); 106296f60e37SRussell King init_waitqueue_head(&dcrtc->frame_wait); 106396f60e37SRussell King 106496f60e37SRussell King /* Initialize some registers which we don't otherwise set */ 106596f60e37SRussell King writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV); 106696f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR); 106796f60e37SRussell King writel_relaxed(dcrtc->spu_iopad_ctrl, 106896f60e37SRussell King dcrtc->base + LCD_SPU_IOPAD_CONTROL); 106996f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0); 107096f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 107196f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 107296f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 107396f60e37SRussell King writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1); 107496f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN); 107596f60e37SRussell King 107696f60e37SRussell King if (priv->variant->crtc_init) { 107796f60e37SRussell King ret = priv->variant->crtc_init(dcrtc); 107896f60e37SRussell King if (ret) { 107996f60e37SRussell King kfree(dcrtc); 108096f60e37SRussell King return ret; 108196f60e37SRussell King } 108296f60e37SRussell King } 108396f60e37SRussell King 108496f60e37SRussell King /* Ensure AXI pipeline is enabled */ 108596f60e37SRussell King armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0); 108696f60e37SRussell King 108796f60e37SRussell King priv->dcrtc[dcrtc->num] = dcrtc; 108896f60e37SRussell King 108996f60e37SRussell King drm_crtc_init(dev, &dcrtc->crtc, &armada_crtc_funcs); 109096f60e37SRussell King drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs); 109196f60e37SRussell King 109296f60e37SRussell King drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop, 109396f60e37SRussell King dcrtc->csc_yuv_mode); 109496f60e37SRussell King drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop, 109596f60e37SRussell King dcrtc->csc_rgb_mode); 109696f60e37SRussell King 109796f60e37SRussell King return armada_overlay_plane_create(dev, 1 << dcrtc->num); 109896f60e37SRussell King } 1099