1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2018 IBM Corporation
5 #include <linux/reset.h>
6 #include <linux/regmap.h>
8 #include <drm/drm_crtc_helper.h>
9 #include <drm/drm_device.h>
10 #include <drm/drm_fb_cma_helper.h>
11 #include <drm/drm_fourcc.h>
12 #include <drm/drm_framebuffer.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_panel.h>
16 #include <drm/drm_simple_kms_helper.h>
17 #include <drm/drm_vblank.h>
19 #include "aspeed_gfx.h"
21 static struct aspeed_gfx *
22 drm_pipe_to_aspeed_gfx(struct drm_simple_display_pipe *pipe)
24 return container_of(pipe, struct aspeed_gfx, pipe);
27 static int aspeed_gfx_set_pixel_fmt(struct aspeed_gfx *priv, u32 *bpp)
29 struct drm_crtc *crtc = &priv->pipe.crtc;
30 struct drm_device *drm = crtc->dev;
31 const u32 format = crtc->primary->state->fb->format->format;
34 ctrl1 = readl(priv->base + CRT_CTRL1);
35 ctrl1 &= ~CRT_CTRL_COLOR_MASK;
38 case DRM_FORMAT_RGB565:
39 dev_dbg(drm->dev, "Setting up RGB565 mode\n");
40 ctrl1 |= CRT_CTRL_COLOR_RGB565;
43 case DRM_FORMAT_XRGB8888:
44 dev_dbg(drm->dev, "Setting up XRGB8888 mode\n");
45 ctrl1 |= CRT_CTRL_COLOR_XRGB8888;
49 dev_err(drm->dev, "Unhandled pixel format %08x\n", format);
53 writel(ctrl1, priv->base + CRT_CTRL1);
58 static void aspeed_gfx_enable_controller(struct aspeed_gfx *priv)
60 u32 ctrl1 = readl(priv->base + CRT_CTRL1);
61 u32 ctrl2 = readl(priv->base + CRT_CTRL2);
63 /* Set DAC source for display output to Graphics CRT (GFX) */
64 regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), BIT(16));
66 writel(ctrl1 | CRT_CTRL_EN, priv->base + CRT_CTRL1);
67 writel(ctrl2 | CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
70 static void aspeed_gfx_disable_controller(struct aspeed_gfx *priv)
72 u32 ctrl1 = readl(priv->base + CRT_CTRL1);
73 u32 ctrl2 = readl(priv->base + CRT_CTRL2);
75 writel(ctrl1 & ~CRT_CTRL_EN, priv->base + CRT_CTRL1);
76 writel(ctrl2 & ~CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
78 regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), 0);
81 static void aspeed_gfx_crtc_mode_set_nofb(struct aspeed_gfx *priv)
83 struct drm_display_mode *m = &priv->pipe.crtc.state->adjusted_mode;
84 u32 ctrl1, d_offset, t_count, bpp;
87 err = aspeed_gfx_set_pixel_fmt(priv, &bpp);
92 /* TODO: we have only been able to test with the 40MHz USB clock. The
93 * clock is fixed, so we cannot adjust it here. */
94 clk_set_rate(priv->pixel_clk, m->crtc_clock * 1000);
97 ctrl1 = readl(priv->base + CRT_CTRL1);
98 ctrl1 &= ~(CRT_CTRL_INTERLACED |
99 CRT_CTRL_HSYNC_NEGATIVE |
100 CRT_CTRL_VSYNC_NEGATIVE);
102 if (m->flags & DRM_MODE_FLAG_INTERLACE)
103 ctrl1 |= CRT_CTRL_INTERLACED;
105 if (!(m->flags & DRM_MODE_FLAG_PHSYNC))
106 ctrl1 |= CRT_CTRL_HSYNC_NEGATIVE;
108 if (!(m->flags & DRM_MODE_FLAG_PVSYNC))
109 ctrl1 |= CRT_CTRL_VSYNC_NEGATIVE;
111 writel(ctrl1, priv->base + CRT_CTRL1);
113 /* Horizontal timing */
114 writel(CRT_H_TOTAL(m->htotal - 1) | CRT_H_DE(m->hdisplay - 1),
115 priv->base + CRT_HORIZ0);
116 writel(CRT_H_RS_START(m->hsync_start - 1) | CRT_H_RS_END(m->hsync_end),
117 priv->base + CRT_HORIZ1);
120 /* Vertical timing */
121 writel(CRT_V_TOTAL(m->vtotal - 1) | CRT_V_DE(m->vdisplay - 1),
122 priv->base + CRT_VERT0);
123 writel(CRT_V_RS_START(m->vsync_start) | CRT_V_RS_END(m->vsync_end),
124 priv->base + CRT_VERT1);
127 * Display Offset: address difference between consecutive scan lines
128 * Terminal Count: memory size of one scan line
130 d_offset = m->hdisplay * bpp / 8;
131 t_count = DIV_ROUND_UP(m->hdisplay * bpp, priv->scan_line_max);
133 writel(CRT_DISP_OFFSET(d_offset) | CRT_TERM_COUNT(t_count),
134 priv->base + CRT_OFFSET);
137 * Threshold: FIFO thresholds of refill and stop (16 byte chunks
138 * per line, rounded up)
140 writel(priv->throd_val, priv->base + CRT_THROD);
143 static void aspeed_gfx_pipe_enable(struct drm_simple_display_pipe *pipe,
144 struct drm_crtc_state *crtc_state,
145 struct drm_plane_state *plane_state)
147 struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
148 struct drm_crtc *crtc = &pipe->crtc;
150 aspeed_gfx_crtc_mode_set_nofb(priv);
151 aspeed_gfx_enable_controller(priv);
152 drm_crtc_vblank_on(crtc);
155 static void aspeed_gfx_pipe_disable(struct drm_simple_display_pipe *pipe)
157 struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
158 struct drm_crtc *crtc = &pipe->crtc;
160 drm_crtc_vblank_off(crtc);
161 aspeed_gfx_disable_controller(priv);
164 static void aspeed_gfx_pipe_update(struct drm_simple_display_pipe *pipe,
165 struct drm_plane_state *plane_state)
167 struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
168 struct drm_crtc *crtc = &pipe->crtc;
169 struct drm_framebuffer *fb = pipe->plane.state->fb;
170 struct drm_pending_vblank_event *event;
171 struct drm_gem_cma_object *gem;
173 spin_lock_irq(&crtc->dev->event_lock);
174 event = crtc->state->event;
176 crtc->state->event = NULL;
178 if (drm_crtc_vblank_get(crtc) == 0)
179 drm_crtc_arm_vblank_event(crtc, event);
181 drm_crtc_send_vblank_event(crtc, event);
183 spin_unlock_irq(&crtc->dev->event_lock);
188 gem = drm_fb_cma_get_gem_obj(fb, 0);
191 writel(gem->paddr, priv->base + CRT_ADDR);
194 static int aspeed_gfx_enable_vblank(struct drm_simple_display_pipe *pipe)
196 struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
197 u32 reg = readl(priv->base + CRT_CTRL1);
199 /* Clear pending VBLANK IRQ */
200 writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
202 reg |= CRT_CTRL_VERTICAL_INTR_EN;
203 writel(reg, priv->base + CRT_CTRL1);
208 static void aspeed_gfx_disable_vblank(struct drm_simple_display_pipe *pipe)
210 struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
211 u32 reg = readl(priv->base + CRT_CTRL1);
213 reg &= ~CRT_CTRL_VERTICAL_INTR_EN;
214 writel(reg, priv->base + CRT_CTRL1);
216 /* Clear pending VBLANK IRQ */
217 writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
220 static const struct drm_simple_display_pipe_funcs aspeed_gfx_funcs = {
221 .enable = aspeed_gfx_pipe_enable,
222 .disable = aspeed_gfx_pipe_disable,
223 .update = aspeed_gfx_pipe_update,
224 .enable_vblank = aspeed_gfx_enable_vblank,
225 .disable_vblank = aspeed_gfx_disable_vblank,
228 static const uint32_t aspeed_gfx_formats[] = {
233 int aspeed_gfx_create_pipe(struct drm_device *drm)
235 struct aspeed_gfx *priv = to_aspeed_gfx(drm);
237 return drm_simple_display_pipe_init(drm, &priv->pipe, &aspeed_gfx_funcs,
239 ARRAY_SIZE(aspeed_gfx_formats),