armada_crtc.c revision 3ecea269959afaae50b001deb294cfb9539dbea3
1/*
2 * Copyright (C) 2012 Russell King
3 *  Rewritten from the dovefb driver, and Armada510 manuals.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/clk.h>
10#include <drm/drmP.h>
11#include <drm/drm_crtc_helper.h>
12#include "armada_crtc.h"
13#include "armada_drm.h"
14#include "armada_fb.h"
15#include "armada_gem.h"
16#include "armada_hw.h"
17
18struct armada_frame_work {
19	struct drm_pending_vblank_event *event;
20	struct armada_regs regs[4];
21	struct drm_framebuffer *old_fb;
22};
23
24enum csc_mode {
25	CSC_AUTO = 0,
26	CSC_YUV_CCIR601 = 1,
27	CSC_YUV_CCIR709 = 2,
28	CSC_RGB_COMPUTER = 1,
29	CSC_RGB_STUDIO = 2,
30};
31
32/*
33 * A note about interlacing.  Let's consider HDMI 1920x1080i.
34 * The timing parameters we have from X are:
35 *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
36 *  1920 2448 2492 2640  1080 1084 1094 1125
37 * Which get translated to:
38 *  Hact HsyA HsyI Htot  Vact VsyA VsyI Vtot
39 *  1920 2448 2492 2640   540  542  547  562
40 *
41 * This is how it is defined by CEA-861-D - line and pixel numbers are
42 * referenced to the rising edge of VSYNC and HSYNC.  Total clocks per
43 * line: 2640.  The odd frame, the first active line is at line 21, and
44 * the even frame, the first active line is 584.
45 *
46 * LN:    560     561     562     563             567     568    569
47 * DE:    ~~~|____________________________//__________________________
48 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
49 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
50 *  22 blanking lines.  VSYNC at 1320 (referenced to the HSYNC rising edge).
51 *
52 * LN:    1123   1124    1125      1               5       6      7
53 * DE:    ~~~|____________________________//__________________________
54 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
55 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
56 *  23 blanking lines
57 *
58 * The Armada LCD Controller line and pixel numbers are, like X timings,
59 * referenced to the top left of the active frame.
60 *
61 * So, translating these to our LCD controller:
62 *  Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
63 *  Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
64 * Note: Vsync front porch remains constant!
65 *
66 * if (odd_frame) {
67 *   vtotal = mode->crtc_vtotal + 1;
68 *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
69 *   vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
70 * } else {
71 *   vtotal = mode->crtc_vtotal;
72 *   vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
73 *   vhorizpos = mode->crtc_hsync_start;
74 * }
75 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
76 *
77 * So, we need to reprogram these registers on each vsync event:
78 *  LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
79 *
80 * Note: we do not use the frame done interrupts because these appear
81 * to happen too early, and lead to jitter on the display (presumably
82 * they occur at the end of the last active line, before the vsync back
83 * porch, which we're reprogramming.)
84 */
85
86void
87armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
88{
89	while (regs->offset != ~0) {
90		void __iomem *reg = dcrtc->base + regs->offset;
91		uint32_t val;
92
93		val = regs->mask;
94		if (val != 0)
95			val &= readl_relaxed(reg);
96		writel_relaxed(val | regs->val, reg);
97		++regs;
98	}
99}
100
101#define dpms_blanked(dpms)	((dpms) != DRM_MODE_DPMS_ON)
102
103static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
104{
105	uint32_t dumb_ctrl;
106
107	dumb_ctrl = dcrtc->cfg_dumb_ctrl;
108
109	if (!dpms_blanked(dcrtc->dpms))
110		dumb_ctrl |= CFG_DUMB_ENA;
111
112	/*
113	 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
114	 * be using SPI or GPIO.  If we set this to DUMB_BLANK, we will
115	 * force LCD_D[23:0] to output blank color, overriding the GPIO or
116	 * SPI usage.  So leave it as-is unless in DUMB24_RGB888_0 mode.
117	 */
118	if (dpms_blanked(dcrtc->dpms) &&
119	    (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
120		dumb_ctrl &= ~DUMB_MASK;
121		dumb_ctrl |= DUMB_BLANK;
122	}
123
124	/*
125	 * The documentation doesn't indicate what the normal state of
126	 * the sync signals are.  Sebastian Hesselbart kindly probed
127	 * these signals on his board to determine their state.
128	 *
129	 * The non-inverted state of the sync signals is active high.
130	 * Setting these bits makes the appropriate signal active low.
131	 */
132	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
133		dumb_ctrl |= CFG_INV_CSYNC;
134	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
135		dumb_ctrl |= CFG_INV_HSYNC;
136	if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
137		dumb_ctrl |= CFG_INV_VSYNC;
138
139	if (dcrtc->dumb_ctrl != dumb_ctrl) {
140		dcrtc->dumb_ctrl = dumb_ctrl;
141		writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
142	}
143}
144
145static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
146	int x, int y, struct armada_regs *regs, bool interlaced)
147{
148	struct armada_gem_object *obj = drm_fb_obj(fb);
149	unsigned pitch = fb->pitches[0];
150	unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
151	uint32_t addr_odd, addr_even;
152	unsigned i = 0;
153
154	DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
155		pitch, x, y, fb->bits_per_pixel);
156
157	addr_odd = addr_even = obj->dev_addr + offset;
158
159	if (interlaced) {
160		addr_even += pitch;
161		pitch *= 2;
162	}
163
164	/* write offset, base, and pitch */
165	armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
166	armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
167	armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
168
169	return i;
170}
171
172static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
173	struct armada_frame_work *work)
174{
175	struct drm_device *dev = dcrtc->crtc.dev;
176	unsigned long flags;
177	int ret;
178
179	ret = drm_vblank_get(dev, dcrtc->num);
180	if (ret) {
181		DRM_ERROR("failed to acquire vblank counter\n");
182		return ret;
183	}
184
185	spin_lock_irqsave(&dev->event_lock, flags);
186	if (!dcrtc->frame_work)
187		dcrtc->frame_work = work;
188	else
189		ret = -EBUSY;
190	spin_unlock_irqrestore(&dev->event_lock, flags);
191
192	if (ret)
193		drm_vblank_put(dev, dcrtc->num);
194
195	return ret;
196}
197
198static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc)
199{
200	struct drm_device *dev = dcrtc->crtc.dev;
201	struct armada_frame_work *work = dcrtc->frame_work;
202
203	dcrtc->frame_work = NULL;
204
205	armada_drm_crtc_update_regs(dcrtc, work->regs);
206
207	if (work->event)
208		drm_send_vblank_event(dev, dcrtc->num, work->event);
209
210	drm_vblank_put(dev, dcrtc->num);
211
212	/* Finally, queue the process-half of the cleanup. */
213	__armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
214	kfree(work);
215}
216
217static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
218	struct drm_framebuffer *fb, bool force)
219{
220	struct armada_frame_work *work;
221
222	if (!fb)
223		return;
224
225	if (force) {
226		/* Display is disabled, so just drop the old fb */
227		drm_framebuffer_unreference(fb);
228		return;
229	}
230
231	work = kmalloc(sizeof(*work), GFP_KERNEL);
232	if (work) {
233		int i = 0;
234		work->event = NULL;
235		work->old_fb = fb;
236		armada_reg_queue_end(work->regs, i);
237
238		if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
239			return;
240
241		kfree(work);
242	}
243
244	/*
245	 * Oops - just drop the reference immediately and hope for
246	 * the best.  The worst that will happen is the buffer gets
247	 * reused before it has finished being displayed.
248	 */
249	drm_framebuffer_unreference(fb);
250}
251
252static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
253{
254	struct drm_device *dev = dcrtc->crtc.dev;
255
256	/*
257	 * Tell the DRM core that vblank IRQs aren't going to happen for
258	 * a while.  This cleans up any pending vblank events for us.
259	 */
260	drm_vblank_off(dev, dcrtc->num);
261
262	/* Handle any pending flip event. */
263	spin_lock_irq(&dev->event_lock);
264	if (dcrtc->frame_work)
265		armada_drm_crtc_complete_frame_work(dcrtc);
266	spin_unlock_irq(&dev->event_lock);
267}
268
269void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
270	int idx)
271{
272}
273
274void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
275	int idx)
276{
277}
278
279/* The mode_config.mutex will be held for this call */
280static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
281{
282	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
283
284	if (dcrtc->dpms != dpms) {
285		dcrtc->dpms = dpms;
286		armada_drm_crtc_update(dcrtc);
287		if (dpms_blanked(dpms))
288			armada_drm_vblank_off(dcrtc);
289	}
290}
291
292/*
293 * Prepare for a mode set.  Turn off overlay to ensure that we don't end
294 * up with the overlay size being bigger than the active screen size.
295 * We rely upon X refreshing this state after the mode set has completed.
296 *
297 * The mode_config.mutex will be held for this call
298 */
299static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
300{
301	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
302	struct drm_plane *plane;
303
304	/*
305	 * If we have an overlay plane associated with this CRTC, disable
306	 * it before the modeset to avoid its coordinates being outside
307	 * the new mode parameters.  DRM doesn't provide help with this.
308	 */
309	plane = dcrtc->plane;
310	if (plane) {
311		struct drm_framebuffer *fb = plane->fb;
312
313		plane->funcs->disable_plane(plane);
314		plane->fb = NULL;
315		plane->crtc = NULL;
316		drm_framebuffer_unreference(fb);
317	}
318}
319
320/* The mode_config.mutex will be held for this call */
321static void armada_drm_crtc_commit(struct drm_crtc *crtc)
322{
323	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
324
325	if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
326		dcrtc->dpms = DRM_MODE_DPMS_ON;
327		armada_drm_crtc_update(dcrtc);
328	}
329}
330
331/* The mode_config.mutex will be held for this call */
332static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
333	const struct drm_display_mode *mode, struct drm_display_mode *adj)
334{
335	struct armada_private *priv = crtc->dev->dev_private;
336	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
337	int ret;
338
339	/* We can't do interlaced modes if we don't have the SPU_ADV_REG */
340	if (!priv->variant->has_spu_adv_reg &&
341	    adj->flags & DRM_MODE_FLAG_INTERLACE)
342		return false;
343
344	/* Check whether the display mode is possible */
345	ret = priv->variant->crtc_compute_clock(dcrtc, adj, NULL);
346	if (ret)
347		return false;
348
349	return true;
350}
351
352static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
353{
354	struct armada_vbl_event *e, *n;
355	void __iomem *base = dcrtc->base;
356
357	if (stat & DMA_FF_UNDERFLOW)
358		DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
359	if (stat & GRA_FF_UNDERFLOW)
360		DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
361
362	if (stat & VSYNC_IRQ)
363		drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
364
365	spin_lock(&dcrtc->irq_lock);
366
367	list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
368		list_del_init(&e->node);
369		drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
370		e->fn(dcrtc, e->data);
371	}
372
373	if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
374		int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
375		uint32_t val;
376
377		writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
378		writel_relaxed(dcrtc->v[i].spu_v_h_total,
379			       base + LCD_SPUT_V_H_TOTAL);
380
381		val = readl_relaxed(base + LCD_SPU_ADV_REG);
382		val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
383		val |= dcrtc->v[i].spu_adv_reg;
384		writel_relaxed(val, base + LCD_SPU_ADV_REG);
385	}
386
387	if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
388		writel_relaxed(dcrtc->cursor_hw_pos,
389			       base + LCD_SPU_HWC_OVSA_HPXL_VLN);
390		writel_relaxed(dcrtc->cursor_hw_sz,
391			       base + LCD_SPU_HWC_HPXL_VLN);
392		armada_updatel(CFG_HWC_ENA,
393			       CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
394			       base + LCD_SPU_DMA_CTRL0);
395		dcrtc->cursor_update = false;
396		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
397	}
398
399	spin_unlock(&dcrtc->irq_lock);
400
401	if (stat & GRA_FRAME_IRQ) {
402		struct drm_device *dev = dcrtc->crtc.dev;
403
404		spin_lock(&dev->event_lock);
405		if (dcrtc->frame_work)
406			armada_drm_crtc_complete_frame_work(dcrtc);
407		spin_unlock(&dev->event_lock);
408
409		wake_up(&dcrtc->frame_wait);
410	}
411}
412
413static irqreturn_t armada_drm_irq(int irq, void *arg)
414{
415	struct armada_crtc *dcrtc = arg;
416	u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
417
418	/*
419	 * This is rediculous - rather than writing bits to clear, we
420	 * have to set the actual status register value.  This is racy.
421	 */
422	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
423
424	/* Mask out those interrupts we haven't enabled */
425	v = stat & dcrtc->irq_ena;
426
427	if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
428		armada_drm_crtc_irq(dcrtc, stat);
429		return IRQ_HANDLED;
430	}
431	return IRQ_NONE;
432}
433
434/* These are locked by dev->vbl_lock */
435void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
436{
437	if (dcrtc->irq_ena & mask) {
438		dcrtc->irq_ena &= ~mask;
439		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
440	}
441}
442
443void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
444{
445	if ((dcrtc->irq_ena & mask) != mask) {
446		dcrtc->irq_ena |= mask;
447		writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
448		if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
449			writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
450	}
451}
452
453static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
454{
455	struct drm_display_mode *adj = &dcrtc->crtc.mode;
456	uint32_t val = 0;
457
458	if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
459		val |= CFG_CSC_YUV_CCIR709;
460	if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
461		val |= CFG_CSC_RGB_STUDIO;
462
463	/*
464	 * In auto mode, set the colorimetry, based upon the HDMI spec.
465	 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
466	 * ITU601.  It may be more appropriate to set this depending on
467	 * the source - but what if the graphic frame is YUV and the
468	 * video frame is RGB?
469	 */
470	if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
471	     !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
472	    (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
473		if (dcrtc->csc_yuv_mode == CSC_AUTO)
474			val |= CFG_CSC_YUV_CCIR709;
475	}
476
477	/*
478	 * We assume we're connected to a TV-like device, so the YUV->RGB
479	 * conversion should produce a limited range.  We should set this
480	 * depending on the connectors attached to this CRTC, and what
481	 * kind of device they report being connected.
482	 */
483	if (dcrtc->csc_rgb_mode == CSC_AUTO)
484		val |= CFG_CSC_RGB_STUDIO;
485
486	return val;
487}
488
489/* The mode_config.mutex will be held for this call */
490static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
491	struct drm_display_mode *mode, struct drm_display_mode *adj,
492	int x, int y, struct drm_framebuffer *old_fb)
493{
494	struct armada_private *priv = crtc->dev->dev_private;
495	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
496	struct armada_regs regs[17];
497	uint32_t lm, rm, tm, bm, val, sclk;
498	unsigned long flags;
499	unsigned i;
500	bool interlaced;
501
502	drm_framebuffer_reference(crtc->primary->fb);
503
504	interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
505
506	i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
507				    x, y, regs, interlaced);
508
509	rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
510	lm = adj->crtc_htotal - adj->crtc_hsync_end;
511	bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
512	tm = adj->crtc_vtotal - adj->crtc_vsync_end;
513
514	DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
515		adj->crtc_hdisplay,
516		adj->crtc_hsync_start,
517		adj->crtc_hsync_end,
518		adj->crtc_htotal, lm, rm);
519	DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
520		adj->crtc_vdisplay,
521		adj->crtc_vsync_start,
522		adj->crtc_vsync_end,
523		adj->crtc_vtotal, tm, bm);
524
525	/* Wait for pending flips to complete */
526	wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
527
528	drm_vblank_pre_modeset(crtc->dev, dcrtc->num);
529
530	crtc->mode = *adj;
531
532	val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
533	if (val != dcrtc->dumb_ctrl) {
534		dcrtc->dumb_ctrl = val;
535		writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
536	}
537
538	/* Now compute the divider for real */
539	priv->variant->crtc_compute_clock(dcrtc, adj, &sclk);
540
541	/* Ensure graphic fifo is enabled */
542	armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
543	armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
544
545	if (interlaced ^ dcrtc->interlaced) {
546		if (adj->flags & DRM_MODE_FLAG_INTERLACE)
547			drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
548		else
549			drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
550		dcrtc->interlaced = interlaced;
551	}
552
553	spin_lock_irqsave(&dcrtc->irq_lock, flags);
554
555	/* Even interlaced/progressive frame */
556	dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
557				    adj->crtc_htotal;
558	dcrtc->v[1].spu_v_porch = tm << 16 | bm;
559	val = adj->crtc_hsync_start;
560	dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
561		priv->variant->spu_adv_reg;
562
563	if (interlaced) {
564		/* Odd interlaced frame */
565		dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
566						(1 << 16);
567		dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
568		val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
569		dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
570			priv->variant->spu_adv_reg;
571	} else {
572		dcrtc->v[0] = dcrtc->v[1];
573	}
574
575	val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
576
577	armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
578	armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
579	armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
580	armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
581	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
582	armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
583			   LCD_SPUT_V_H_TOTAL);
584
585	if (priv->variant->has_spu_adv_reg) {
586		armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
587				     ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
588				     ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
589	}
590
591	val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
592	val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
593	val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
594
595	if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
596		val |= CFG_PALETTE_ENA;
597
598	if (interlaced)
599		val |= CFG_GRA_FTOGGLE;
600
601	armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
602			     CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
603					 CFG_SWAPYU | CFG_YUV2RGB) |
604			     CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
605			     LCD_SPU_DMA_CTRL0);
606
607	val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
608	armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
609
610	val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
611	armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
612	armada_reg_queue_end(regs, i);
613
614	armada_drm_crtc_update_regs(dcrtc, regs);
615	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
616
617	armada_drm_crtc_update(dcrtc);
618
619	drm_vblank_post_modeset(crtc->dev, dcrtc->num);
620	armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
621
622	return 0;
623}
624
625/* The mode_config.mutex will be held for this call */
626static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
627	struct drm_framebuffer *old_fb)
628{
629	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
630	struct armada_regs regs[4];
631	unsigned i;
632
633	i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
634				    dcrtc->interlaced);
635	armada_reg_queue_end(regs, i);
636
637	/* Wait for pending flips to complete */
638	wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
639
640	/* Take a reference to the new fb as we're using it */
641	drm_framebuffer_reference(crtc->primary->fb);
642
643	/* Update the base in the CRTC */
644	armada_drm_crtc_update_regs(dcrtc, regs);
645
646	/* Drop our previously held reference */
647	armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
648
649	return 0;
650}
651
652static void armada_drm_crtc_load_lut(struct drm_crtc *crtc)
653{
654}
655
656/* The mode_config.mutex will be held for this call */
657static void armada_drm_crtc_disable(struct drm_crtc *crtc)
658{
659	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
660
661	armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
662	armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true);
663
664	/* Power down most RAMs and FIFOs */
665	writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
666		       CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
667		       CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
668}
669
670static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
671	.dpms		= armada_drm_crtc_dpms,
672	.prepare	= armada_drm_crtc_prepare,
673	.commit		= armada_drm_crtc_commit,
674	.mode_fixup	= armada_drm_crtc_mode_fixup,
675	.mode_set	= armada_drm_crtc_mode_set,
676	.mode_set_base	= armada_drm_crtc_mode_set_base,
677	.load_lut	= armada_drm_crtc_load_lut,
678	.disable	= armada_drm_crtc_disable,
679};
680
681static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
682	unsigned stride, unsigned width, unsigned height)
683{
684	uint32_t addr;
685	unsigned y;
686
687	addr = SRAM_HWC32_RAM1;
688	for (y = 0; y < height; y++) {
689		uint32_t *p = &pix[y * stride];
690		unsigned x;
691
692		for (x = 0; x < width; x++, p++) {
693			uint32_t val = *p;
694
695			val = (val & 0xff00ff00) |
696			      (val & 0x000000ff) << 16 |
697			      (val & 0x00ff0000) >> 16;
698
699			writel_relaxed(val,
700				       base + LCD_SPU_SRAM_WRDAT);
701			writel_relaxed(addr | SRAM_WRITE,
702				       base + LCD_SPU_SRAM_CTRL);
703			readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
704			addr += 1;
705			if ((addr & 0x00ff) == 0)
706				addr += 0xf00;
707			if ((addr & 0x30ff) == 0)
708				addr = SRAM_HWC32_RAM2;
709		}
710	}
711}
712
713static void armada_drm_crtc_cursor_tran(void __iomem *base)
714{
715	unsigned addr;
716
717	for (addr = 0; addr < 256; addr++) {
718		/* write the default value */
719		writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
720		writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
721			       base + LCD_SPU_SRAM_CTRL);
722	}
723}
724
725static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
726{
727	uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
728	uint32_t yoff, yscr, h = dcrtc->cursor_h;
729	uint32_t para1;
730
731	/*
732	 * Calculate the visible width and height of the cursor,
733	 * screen position, and the position in the cursor bitmap.
734	 */
735	if (dcrtc->cursor_x < 0) {
736		xoff = -dcrtc->cursor_x;
737		xscr = 0;
738		w -= min(xoff, w);
739	} else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
740		xoff = 0;
741		xscr = dcrtc->cursor_x;
742		w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
743	} else {
744		xoff = 0;
745		xscr = dcrtc->cursor_x;
746	}
747
748	if (dcrtc->cursor_y < 0) {
749		yoff = -dcrtc->cursor_y;
750		yscr = 0;
751		h -= min(yoff, h);
752	} else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
753		yoff = 0;
754		yscr = dcrtc->cursor_y;
755		h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
756	} else {
757		yoff = 0;
758		yscr = dcrtc->cursor_y;
759	}
760
761	/* On interlaced modes, the vertical cursor size must be halved */
762	s = dcrtc->cursor_w;
763	if (dcrtc->interlaced) {
764		s *= 2;
765		yscr /= 2;
766		h /= 2;
767	}
768
769	if (!dcrtc->cursor_obj || !h || !w) {
770		spin_lock_irq(&dcrtc->irq_lock);
771		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
772		dcrtc->cursor_update = false;
773		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
774		spin_unlock_irq(&dcrtc->irq_lock);
775		return 0;
776	}
777
778	para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
779	armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
780		       dcrtc->base + LCD_SPU_SRAM_PARA1);
781
782	/*
783	 * Initialize the transparency if the SRAM was powered down.
784	 * We must also reload the cursor data as well.
785	 */
786	if (!(para1 & CFG_CSB_256x32)) {
787		armada_drm_crtc_cursor_tran(dcrtc->base);
788		reload = true;
789	}
790
791	if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
792		spin_lock_irq(&dcrtc->irq_lock);
793		armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
794		dcrtc->cursor_update = false;
795		armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
796		spin_unlock_irq(&dcrtc->irq_lock);
797		reload = true;
798	}
799	if (reload) {
800		struct armada_gem_object *obj = dcrtc->cursor_obj;
801		uint32_t *pix;
802		/* Set the top-left corner of the cursor image */
803		pix = obj->addr;
804		pix += yoff * s + xoff;
805		armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
806	}
807
808	/* Reload the cursor position, size and enable in the IRQ handler */
809	spin_lock_irq(&dcrtc->irq_lock);
810	dcrtc->cursor_hw_pos = yscr << 16 | xscr;
811	dcrtc->cursor_hw_sz = h << 16 | w;
812	dcrtc->cursor_update = true;
813	armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
814	spin_unlock_irq(&dcrtc->irq_lock);
815
816	return 0;
817}
818
819static void cursor_update(void *data)
820{
821	armada_drm_crtc_cursor_update(data, true);
822}
823
824static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
825	struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
826{
827	struct drm_device *dev = crtc->dev;
828	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
829	struct armada_private *priv = crtc->dev->dev_private;
830	struct armada_gem_object *obj = NULL;
831	int ret;
832
833	/* If no cursor support, replicate drm's return value */
834	if (!priv->variant->has_spu_adv_reg)
835		return -ENXIO;
836
837	if (handle && w > 0 && h > 0) {
838		/* maximum size is 64x32 or 32x64 */
839		if (w > 64 || h > 64 || (w > 32 && h > 32))
840			return -ENOMEM;
841
842		obj = armada_gem_object_lookup(dev, file, handle);
843		if (!obj)
844			return -ENOENT;
845
846		/* Must be a kernel-mapped object */
847		if (!obj->addr) {
848			drm_gem_object_unreference_unlocked(&obj->obj);
849			return -EINVAL;
850		}
851
852		if (obj->obj.size < w * h * 4) {
853			DRM_ERROR("buffer is too small\n");
854			drm_gem_object_unreference_unlocked(&obj->obj);
855			return -ENOMEM;
856		}
857	}
858
859	mutex_lock(&dev->struct_mutex);
860	if (dcrtc->cursor_obj) {
861		dcrtc->cursor_obj->update = NULL;
862		dcrtc->cursor_obj->update_data = NULL;
863		drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
864	}
865	dcrtc->cursor_obj = obj;
866	dcrtc->cursor_w = w;
867	dcrtc->cursor_h = h;
868	ret = armada_drm_crtc_cursor_update(dcrtc, true);
869	if (obj) {
870		obj->update_data = dcrtc;
871		obj->update = cursor_update;
872	}
873	mutex_unlock(&dev->struct_mutex);
874
875	return ret;
876}
877
878static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
879{
880	struct drm_device *dev = crtc->dev;
881	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
882	struct armada_private *priv = crtc->dev->dev_private;
883	int ret;
884
885	/* If no cursor support, replicate drm's return value */
886	if (!priv->variant->has_spu_adv_reg)
887		return -EFAULT;
888
889	mutex_lock(&dev->struct_mutex);
890	dcrtc->cursor_x = x;
891	dcrtc->cursor_y = y;
892	ret = armada_drm_crtc_cursor_update(dcrtc, false);
893	mutex_unlock(&dev->struct_mutex);
894
895	return ret;
896}
897
898static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
899{
900	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
901	struct armada_private *priv = crtc->dev->dev_private;
902
903	if (dcrtc->cursor_obj)
904		drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
905
906	priv->dcrtc[dcrtc->num] = NULL;
907	drm_crtc_cleanup(&dcrtc->crtc);
908
909	if (!IS_ERR(dcrtc->clk))
910		clk_disable_unprepare(dcrtc->clk);
911
912	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
913
914	kfree(dcrtc);
915}
916
917/*
918 * The mode_config lock is held here, to prevent races between this
919 * and a mode_set.
920 */
921static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
922	struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
923{
924	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
925	struct armada_frame_work *work;
926	struct drm_device *dev = crtc->dev;
927	unsigned long flags;
928	unsigned i;
929	int ret;
930
931	/* We don't support changing the pixel format */
932	if (fb->pixel_format != crtc->primary->fb->pixel_format)
933		return -EINVAL;
934
935	work = kmalloc(sizeof(*work), GFP_KERNEL);
936	if (!work)
937		return -ENOMEM;
938
939	work->event = event;
940	work->old_fb = dcrtc->crtc.primary->fb;
941
942	i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
943				    dcrtc->interlaced);
944	armada_reg_queue_end(work->regs, i);
945
946	/*
947	 * Hold the old framebuffer for the work - DRM appears to drop our
948	 * reference to the old framebuffer in drm_mode_page_flip_ioctl().
949	 */
950	drm_framebuffer_reference(work->old_fb);
951
952	ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
953	if (ret) {
954		/*
955		 * Undo our reference above; DRM does not drop the reference
956		 * to this object on error, so that's okay.
957		 */
958		drm_framebuffer_unreference(work->old_fb);
959		kfree(work);
960		return ret;
961	}
962
963	/*
964	 * Don't take a reference on the new framebuffer;
965	 * drm_mode_page_flip_ioctl() has already grabbed a reference and
966	 * will _not_ drop that reference on successful return from this
967	 * function.  Simply mark this new framebuffer as the current one.
968	 */
969	dcrtc->crtc.primary->fb = fb;
970
971	/*
972	 * Finally, if the display is blanked, we won't receive an
973	 * interrupt, so complete it now.
974	 */
975	if (dpms_blanked(dcrtc->dpms)) {
976		spin_lock_irqsave(&dev->event_lock, flags);
977		if (dcrtc->frame_work)
978			armada_drm_crtc_complete_frame_work(dcrtc);
979		spin_unlock_irqrestore(&dev->event_lock, flags);
980	}
981
982	return 0;
983}
984
985static int
986armada_drm_crtc_set_property(struct drm_crtc *crtc,
987	struct drm_property *property, uint64_t val)
988{
989	struct armada_private *priv = crtc->dev->dev_private;
990	struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
991	bool update_csc = false;
992
993	if (property == priv->csc_yuv_prop) {
994		dcrtc->csc_yuv_mode = val;
995		update_csc = true;
996	} else if (property == priv->csc_rgb_prop) {
997		dcrtc->csc_rgb_mode = val;
998		update_csc = true;
999	}
1000
1001	if (update_csc) {
1002		uint32_t val;
1003
1004		val = dcrtc->spu_iopad_ctrl |
1005		      armada_drm_crtc_calculate_csc(dcrtc);
1006		writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1007	}
1008
1009	return 0;
1010}
1011
1012static struct drm_crtc_funcs armada_crtc_funcs = {
1013	.cursor_set	= armada_drm_crtc_cursor_set,
1014	.cursor_move	= armada_drm_crtc_cursor_move,
1015	.destroy	= armada_drm_crtc_destroy,
1016	.set_config	= drm_crtc_helper_set_config,
1017	.page_flip	= armada_drm_crtc_page_flip,
1018	.set_property	= armada_drm_crtc_set_property,
1019};
1020
1021static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1022	{ CSC_AUTO,        "Auto" },
1023	{ CSC_YUV_CCIR601, "CCIR601" },
1024	{ CSC_YUV_CCIR709, "CCIR709" },
1025};
1026
1027static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1028	{ CSC_AUTO,         "Auto" },
1029	{ CSC_RGB_COMPUTER, "Computer system" },
1030	{ CSC_RGB_STUDIO,   "Studio" },
1031};
1032
1033static int armada_drm_crtc_create_properties(struct drm_device *dev)
1034{
1035	struct armada_private *priv = dev->dev_private;
1036
1037	if (priv->csc_yuv_prop)
1038		return 0;
1039
1040	priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1041				"CSC_YUV", armada_drm_csc_yuv_enum_list,
1042				ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1043	priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1044				"CSC_RGB", armada_drm_csc_rgb_enum_list,
1045				ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1046
1047	if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1048		return -ENOMEM;
1049
1050	return 0;
1051}
1052
1053int armada_drm_crtc_create(struct drm_device *dev, struct resource *res,
1054	int irq)
1055{
1056	struct armada_private *priv = dev->dev_private;
1057	struct armada_crtc *dcrtc;
1058	void __iomem *base;
1059	int ret;
1060
1061	ret = armada_drm_crtc_create_properties(dev);
1062	if (ret)
1063		return ret;
1064
1065	base = devm_request_and_ioremap(dev->dev, res);
1066	if (!base) {
1067		DRM_ERROR("failed to ioremap register\n");
1068		return -ENOMEM;
1069	}
1070
1071	dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1072	if (!dcrtc) {
1073		DRM_ERROR("failed to allocate Armada crtc\n");
1074		return -ENOMEM;
1075	}
1076
1077	dcrtc->base = base;
1078	dcrtc->num = dev->mode_config.num_crtc;
1079	dcrtc->clk = ERR_PTR(-EINVAL);
1080	dcrtc->csc_yuv_mode = CSC_AUTO;
1081	dcrtc->csc_rgb_mode = CSC_AUTO;
1082	dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1083	dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1084	spin_lock_init(&dcrtc->irq_lock);
1085	dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1086	INIT_LIST_HEAD(&dcrtc->vbl_list);
1087	init_waitqueue_head(&dcrtc->frame_wait);
1088
1089	/* Initialize some registers which we don't otherwise set */
1090	writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1091	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1092	writel_relaxed(dcrtc->spu_iopad_ctrl,
1093		       dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1094	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1095	writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1096		       CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1097		       CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1098	writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1099	writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
1100	writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1101	writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
1102
1103	ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1104			       dcrtc);
1105	if (ret < 0) {
1106		kfree(dcrtc);
1107		return ret;
1108	}
1109
1110	if (priv->variant->crtc_init) {
1111		ret = priv->variant->crtc_init(dcrtc, dev->dev);
1112		if (ret) {
1113			kfree(dcrtc);
1114			return ret;
1115		}
1116	}
1117
1118	/* Ensure AXI pipeline is enabled */
1119	armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1120
1121	priv->dcrtc[dcrtc->num] = dcrtc;
1122
1123	drm_crtc_init(dev, &dcrtc->crtc, &armada_crtc_funcs);
1124	drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1125
1126	drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1127				   dcrtc->csc_yuv_mode);
1128	drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1129				   dcrtc->csc_rgb_mode);
1130
1131	return armada_overlay_plane_create(dev, 1 << dcrtc->num);
1132}
1133