aspeed_gfx_crtc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2018 IBM Corporation
  3. #include <linux/clk.h>
  4. #include <linux/reset.h>
  5. #include <linux/regmap.h>
  6. #include <drm/drm_device.h>
  7. #include <drm/drm_fb_dma_helper.h>
  8. #include <drm/drm_fourcc.h>
  9. #include <drm/drm_framebuffer.h>
  10. #include <drm/drm_gem_atomic_helper.h>
  11. #include <drm/drm_gem_dma_helper.h>
  12. #include <drm/drm_panel.h>
  13. #include <drm/drm_simple_kms_helper.h>
  14. #include <drm/drm_vblank.h>
  15. #include "aspeed_gfx.h"
  16. static struct aspeed_gfx *
  17. drm_pipe_to_aspeed_gfx(struct drm_simple_display_pipe *pipe)
  18. {
  19. return container_of(pipe, struct aspeed_gfx, pipe);
  20. }
  21. static int aspeed_gfx_set_pixel_fmt(struct aspeed_gfx *priv, u32 *bpp)
  22. {
  23. struct drm_crtc *crtc = &priv->pipe.crtc;
  24. struct drm_device *drm = crtc->dev;
  25. const u32 format = crtc->primary->state->fb->format->format;
  26. u32 ctrl1;
  27. ctrl1 = readl(priv->base + CRT_CTRL1);
  28. ctrl1 &= ~CRT_CTRL_COLOR_MASK;
  29. switch (format) {
  30. case DRM_FORMAT_RGB565:
  31. dev_dbg(drm->dev, "Setting up RGB565 mode\n");
  32. ctrl1 |= CRT_CTRL_COLOR_RGB565;
  33. *bpp = 16;
  34. break;
  35. case DRM_FORMAT_XRGB8888:
  36. dev_dbg(drm->dev, "Setting up XRGB8888 mode\n");
  37. ctrl1 |= CRT_CTRL_COLOR_XRGB8888;
  38. *bpp = 32;
  39. break;
  40. default:
  41. dev_err(drm->dev, "Unhandled pixel format %08x\n", format);
  42. return -EINVAL;
  43. }
  44. writel(ctrl1, priv->base + CRT_CTRL1);
  45. return 0;
  46. }
  47. static void aspeed_gfx_enable_controller(struct aspeed_gfx *priv)
  48. {
  49. u32 ctrl1 = readl(priv->base + CRT_CTRL1);
  50. u32 ctrl2 = readl(priv->base + CRT_CTRL2);
  51. /* Set DAC source for display output to Graphics CRT (GFX) */
  52. regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), BIT(16));
  53. writel(ctrl1 | CRT_CTRL_EN, priv->base + CRT_CTRL1);
  54. writel(ctrl2 | CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
  55. }
  56. static void aspeed_gfx_disable_controller(struct aspeed_gfx *priv)
  57. {
  58. u32 ctrl1 = readl(priv->base + CRT_CTRL1);
  59. u32 ctrl2 = readl(priv->base + CRT_CTRL2);
  60. writel(ctrl1 & ~CRT_CTRL_EN, priv->base + CRT_CTRL1);
  61. writel(ctrl2 & ~CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
  62. regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), 0);
  63. }
  64. static void aspeed_gfx_crtc_mode_set_nofb(struct aspeed_gfx *priv)
  65. {
  66. struct drm_display_mode *m = &priv->pipe.crtc.state->adjusted_mode;
  67. u32 ctrl1, d_offset, t_count, bpp;
  68. int err;
  69. err = aspeed_gfx_set_pixel_fmt(priv, &bpp);
  70. if (err)
  71. return;
  72. #if 0
  73. /* TODO: we have only been able to test with the 40MHz USB clock. The
  74. * clock is fixed, so we cannot adjust it here. */
  75. clk_set_rate(priv->pixel_clk, m->crtc_clock * 1000);
  76. #endif
  77. ctrl1 = readl(priv->base + CRT_CTRL1);
  78. ctrl1 &= ~(CRT_CTRL_INTERLACED |
  79. CRT_CTRL_HSYNC_NEGATIVE |
  80. CRT_CTRL_VSYNC_NEGATIVE);
  81. if (m->flags & DRM_MODE_FLAG_INTERLACE)
  82. ctrl1 |= CRT_CTRL_INTERLACED;
  83. if (!(m->flags & DRM_MODE_FLAG_PHSYNC))
  84. ctrl1 |= CRT_CTRL_HSYNC_NEGATIVE;
  85. if (!(m->flags & DRM_MODE_FLAG_PVSYNC))
  86. ctrl1 |= CRT_CTRL_VSYNC_NEGATIVE;
  87. writel(ctrl1, priv->base + CRT_CTRL1);
  88. /* Horizontal timing */
  89. writel(CRT_H_TOTAL(m->htotal - 1) | CRT_H_DE(m->hdisplay - 1),
  90. priv->base + CRT_HORIZ0);
  91. writel(CRT_H_RS_START(m->hsync_start - 1) | CRT_H_RS_END(m->hsync_end),
  92. priv->base + CRT_HORIZ1);
  93. /* Vertical timing */
  94. writel(CRT_V_TOTAL(m->vtotal - 1) | CRT_V_DE(m->vdisplay - 1),
  95. priv->base + CRT_VERT0);
  96. writel(CRT_V_RS_START(m->vsync_start) | CRT_V_RS_END(m->vsync_end),
  97. priv->base + CRT_VERT1);
  98. /*
  99. * Display Offset: address difference between consecutive scan lines
  100. * Terminal Count: memory size of one scan line
  101. */
  102. d_offset = m->hdisplay * bpp / 8;
  103. t_count = DIV_ROUND_UP(m->hdisplay * bpp, priv->scan_line_max);
  104. writel(CRT_DISP_OFFSET(d_offset) | CRT_TERM_COUNT(t_count),
  105. priv->base + CRT_OFFSET);
  106. /*
  107. * Threshold: FIFO thresholds of refill and stop (16 byte chunks
  108. * per line, rounded up)
  109. */
  110. writel(priv->throd_val, priv->base + CRT_THROD);
  111. }
  112. static void aspeed_gfx_pipe_enable(struct drm_simple_display_pipe *pipe,
  113. struct drm_crtc_state *crtc_state,
  114. struct drm_plane_state *plane_state)
  115. {
  116. struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
  117. struct drm_crtc *crtc = &pipe->crtc;
  118. aspeed_gfx_crtc_mode_set_nofb(priv);
  119. aspeed_gfx_enable_controller(priv);
  120. drm_crtc_vblank_on(crtc);
  121. }
  122. static void aspeed_gfx_pipe_disable(struct drm_simple_display_pipe *pipe)
  123. {
  124. struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
  125. struct drm_crtc *crtc = &pipe->crtc;
  126. drm_crtc_vblank_off(crtc);
  127. aspeed_gfx_disable_controller(priv);
  128. }
  129. static void aspeed_gfx_pipe_update(struct drm_simple_display_pipe *pipe,
  130. struct drm_plane_state *plane_state)
  131. {
  132. struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
  133. struct drm_crtc *crtc = &pipe->crtc;
  134. struct drm_framebuffer *fb = pipe->plane.state->fb;
  135. struct drm_pending_vblank_event *event;
  136. struct drm_gem_dma_object *gem;
  137. spin_lock_irq(&crtc->dev->event_lock);
  138. event = crtc->state->event;
  139. if (event) {
  140. crtc->state->event = NULL;
  141. if (drm_crtc_vblank_get(crtc) == 0)
  142. drm_crtc_arm_vblank_event(crtc, event);
  143. else
  144. drm_crtc_send_vblank_event(crtc, event);
  145. }
  146. spin_unlock_irq(&crtc->dev->event_lock);
  147. if (!fb)
  148. return;
  149. gem = drm_fb_dma_get_gem_obj(fb, 0);
  150. if (!gem)
  151. return;
  152. writel(gem->dma_addr, priv->base + CRT_ADDR);
  153. }
  154. static int aspeed_gfx_enable_vblank(struct drm_simple_display_pipe *pipe)
  155. {
  156. struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
  157. u32 reg = readl(priv->base + CRT_CTRL1);
  158. /* Clear pending VBLANK IRQ */
  159. writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
  160. reg |= CRT_CTRL_VERTICAL_INTR_EN;
  161. writel(reg, priv->base + CRT_CTRL1);
  162. return 0;
  163. }
  164. static void aspeed_gfx_disable_vblank(struct drm_simple_display_pipe *pipe)
  165. {
  166. struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
  167. u32 reg = readl(priv->base + CRT_CTRL1);
  168. reg &= ~CRT_CTRL_VERTICAL_INTR_EN;
  169. writel(reg, priv->base + CRT_CTRL1);
  170. /* Clear pending VBLANK IRQ */
  171. writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
  172. }
  173. static const struct drm_simple_display_pipe_funcs aspeed_gfx_funcs = {
  174. .enable = aspeed_gfx_pipe_enable,
  175. .disable = aspeed_gfx_pipe_disable,
  176. .update = aspeed_gfx_pipe_update,
  177. .enable_vblank = aspeed_gfx_enable_vblank,
  178. .disable_vblank = aspeed_gfx_disable_vblank,
  179. };
  180. static const uint32_t aspeed_gfx_formats[] = {
  181. DRM_FORMAT_XRGB8888,
  182. DRM_FORMAT_RGB565,
  183. };
  184. int aspeed_gfx_create_pipe(struct drm_device *drm)
  185. {
  186. struct aspeed_gfx *priv = to_aspeed_gfx(drm);
  187. return drm_simple_display_pipe_init(drm, &priv->pipe, &aspeed_gfx_funcs,
  188. aspeed_gfx_formats,
  189. ARRAY_SIZE(aspeed_gfx_formats),
  190. NULL,
  191. &priv->connector);
  192. }