Home | History | Annotate | Line # | Download | only in rockchip
rk_vop.c revision 1.16
      1  1.16  riastrad /* $NetBSD: rk_vop.c,v 1.16 2021/12/20 00:27:17 riastradh Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.16  riastrad __KERNEL_RCSID(0, "$NetBSD: rk_vop.c,v 1.16 2021/12/20 00:27:17 riastradh Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34  1.12  riastrad #include <sys/conf.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/intr.h>
     37   1.1  jmcneill #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/sysctl.h>
     39  1.12  riastrad #include <sys/systm.h>
     40  1.12  riastrad 
     41  1.12  riastrad #include <dev/fdt/fdt_port.h>
     42  1.12  riastrad #include <dev/fdt/fdtvar.h>
     43  1.12  riastrad 
     44  1.12  riastrad #include <arm/rockchip/rk_drm.h>
     45   1.1  jmcneill 
     46  1.13  riastrad #include <drm/drm_atomic.h>
     47  1.13  riastrad #include <drm/drm_atomic_helper.h>
     48   1.1  jmcneill #include <drm/drm_crtc.h>
     49   1.1  jmcneill #include <drm/drm_crtc_helper.h>
     50  1.12  riastrad #include <drm/drm_drv.h>
     51  1.11  riastrad #include <drm/drm_fourcc.h>
     52   1.1  jmcneill #include <drm/drm_plane_helper.h>
     53  1.16  riastrad #include <drm/drm_vblank.h>
     54   1.1  jmcneill 
     55   1.1  jmcneill #define	VOP_REG_CFG_DONE		0x0000
     56   1.1  jmcneill #define	 REG_LOAD_EN			__BIT(0)
     57   1.1  jmcneill #define	VOP_SYS_CTRL			0x0008
     58   1.1  jmcneill #define	 VOP_STANDBY_EN			__BIT(22)
     59   1.1  jmcneill #define	 MIPI_OUT_EN			__BIT(15)
     60   1.1  jmcneill #define	 EDP_OUT_EN			__BIT(14)
     61   1.1  jmcneill #define	 HDMI_OUT_EN			__BIT(13)
     62   1.1  jmcneill #define	 RGB_OUT_EN			__BIT(12)
     63   1.1  jmcneill #define	VOP_DSP_CTRL0			0x0010
     64   1.1  jmcneill #define	 DSP_OUT_MODE			__BITS(3,0)
     65   1.1  jmcneill #define	  DSP_OUT_MODE_RGB888		0
     66   1.1  jmcneill #define	  DSP_OUT_MODE_RGBaaa		15
     67   1.1  jmcneill #define	VOP_DSP_CTRL1			0x0014
     68   1.1  jmcneill #define	VOP_WIN0_CTRL			0x0030
     69   1.1  jmcneill #define	 WIN0_LB_MODE			__BITS(7,5)
     70   1.1  jmcneill #define	  WIN0_LB_MODE_RGB_3840X2	2
     71   1.1  jmcneill #define	  WIN0_LB_MODE_RGB_2560X4	3
     72   1.1  jmcneill #define	  WIN0_LB_MODE_RGB_1920X5	4
     73   1.1  jmcneill #define	  WIN0_LB_MODE_RGB_1280X8	5
     74   1.1  jmcneill #define	 WIN0_DATA_FMT			__BITS(3,1)
     75   1.1  jmcneill #define	  WIN0_DATA_FMT_ARGB888		0
     76   1.1  jmcneill #define	 WIN0_EN			__BIT(0)
     77   1.1  jmcneill #define	VOP_WIN0_COLOR_KEY		0x0038
     78   1.1  jmcneill #define	VOP_WIN0_VIR			0x003c
     79   1.1  jmcneill #define	 WIN0_VIR_STRIDE		__BITS(13,0)
     80   1.1  jmcneill #define	VOP_WIN0_YRGB_MST		0x0040
     81   1.1  jmcneill #define	VOP_WIN0_ACT_INFO		0x0048
     82   1.1  jmcneill #define	 WIN0_ACT_HEIGHT		__BITS(28,16)
     83   1.1  jmcneill #define	 WIN0_ACT_WIDTH			__BITS(12,0)
     84   1.1  jmcneill #define	VOP_WIN0_DSP_INFO		0x004c
     85   1.1  jmcneill #define	 WIN0_DSP_HEIGHT		__BITS(27,16)
     86   1.1  jmcneill #define	 WIN0_DSP_WIDTH			__BITS(11,0)
     87   1.1  jmcneill #define	VOP_WIN0_DSP_ST			0x0050
     88   1.1  jmcneill #define	 WIN0_DSP_YST			__BITS(28,16)
     89   1.1  jmcneill #define	 WIN0_DSP_XST			__BITS(12,0)
     90   1.1  jmcneill #define	VOP_POST_DSP_HACT_INFO		0x0170
     91   1.1  jmcneill #define	 DSP_HACT_ST_POST		__BITS(28,16)
     92   1.1  jmcneill #define	 DSP_HACT_END_POST		__BITS(12,0)
     93   1.1  jmcneill #define	VOP_POST_DSP_VACT_INFO		0x0174
     94   1.1  jmcneill #define	 DSP_VACT_ST_POST		__BITS(28,16)
     95   1.1  jmcneill #define	 DSP_VACT_END_POST		__BITS(12,0)
     96   1.1  jmcneill #define	VOP_DSP_HTOTAL_HS_END		0x0188
     97   1.2  jmcneill #define	 DSP_HS_END			__BITS(28,16)
     98   1.2  jmcneill #define	 DSP_HTOTAL			__BITS(12,0)
     99   1.1  jmcneill #define	VOP_DSP_HACT_ST_END		0x018c
    100   1.1  jmcneill #define	 DSP_HACT_ST			__BITS(28,16)
    101   1.1  jmcneill #define	 DSP_HACT_END			__BITS(12,0)
    102   1.1  jmcneill #define	VOP_DSP_VTOTAL_VS_END		0x0190
    103   1.2  jmcneill #define	 DSP_VS_END			__BITS(28,16)
    104   1.2  jmcneill #define	 DSP_VTOTAL			__BITS(12,0)
    105   1.1  jmcneill #define	VOP_DSP_VACT_ST_END		0x0194
    106   1.1  jmcneill #define	 DSP_VACT_ST			__BITS(28,16)
    107   1.1  jmcneill #define	 DSP_VACT_END			__BITS(12,0)
    108  1.16  riastrad #define	VOP_INTR_EN0			0x0280
    109  1.16  riastrad #define	VOP_INTR_CLEAR0			0x0284
    110  1.16  riastrad #define	VOP_INTR_STATUS0		0x0288
    111  1.16  riastrad #define	VOP_INTR_RAW_STATUS0		0x028c
    112  1.16  riastrad #define	 VOP_INTR0_DMA_FINISH		__BIT(15)
    113  1.16  riastrad #define	 VOP_INTR0_MMU			__BIT(14)
    114  1.16  riastrad #define	 VOP_INTR0_DSP_HOLD_VALID	__BIT(13)
    115  1.16  riastrad #define	 VOP_INTR0_FS_FIELD		__BIT(12)
    116  1.16  riastrad #define	 VOP_INTR0_POST_BUF_EMPTY	__BIT(11)
    117  1.16  riastrad #define	 VOP_INTR0_HWC_EMPTY		__BIT(10)
    118  1.16  riastrad #define	 VOP_INTR0_WIN3_EMPTY		__BIT(9)
    119  1.16  riastrad #define	 VOP_INTR0_WIN2_EMPTY		__BIT(8)
    120  1.16  riastrad #define	 VOP_INTR0_WIN1_EMPTY		__BIT(7)
    121  1.16  riastrad #define	 VOP_INTR0_WIN0_EMPTY		__BIT(6)
    122  1.16  riastrad #define	 VOP_INTR0_BUS_ERROR		__BIT(5)
    123  1.16  riastrad #define	 VOP_INTR0_LINE_FLAG1		__BIT(4)
    124  1.16  riastrad #define	 VOP_INTR0_LINE_FLAG0		__BIT(3)
    125  1.16  riastrad #define	 VOP_INTR0_ADDR_SAME		__BIT(2)
    126  1.16  riastrad #define	 VOP_INTR0_FS_NEW		__BIT(1)
    127  1.16  riastrad #define	 VOP_INTR0_FS			__BIT(0)
    128   1.1  jmcneill 
    129   1.1  jmcneill /*
    130   1.1  jmcneill  * Polarity fields are in different locations depending on SoC and output type,
    131   1.1  jmcneill  * but always in the same order.
    132   1.1  jmcneill  */
    133   1.1  jmcneill #define	DSP_DCLK_POL			__BIT(3)
    134   1.1  jmcneill #define	DSP_DEN_POL			__BIT(2)
    135   1.1  jmcneill #define	DSP_VSYNC_POL			__BIT(1)
    136   1.1  jmcneill #define	DSP_HSYNC_POL			__BIT(0)
    137   1.1  jmcneill 
    138   1.1  jmcneill enum vop_ep_type {
    139   1.1  jmcneill 	VOP_EP_MIPI,
    140   1.1  jmcneill 	VOP_EP_EDP,
    141   1.1  jmcneill 	VOP_EP_HDMI,
    142   1.1  jmcneill 	VOP_EP_MIPI1,
    143   1.1  jmcneill 	VOP_EP_DP,
    144   1.1  jmcneill 	VOP_NEP
    145   1.1  jmcneill };
    146   1.1  jmcneill 
    147   1.1  jmcneill struct rk_vop_softc;
    148   1.1  jmcneill struct rk_vop_config;
    149   1.1  jmcneill 
    150   1.1  jmcneill struct rk_vop_crtc {
    151   1.1  jmcneill 	struct drm_crtc		base;
    152   1.1  jmcneill 	struct rk_vop_softc	*sc;
    153   1.1  jmcneill };
    154   1.1  jmcneill 
    155  1.13  riastrad struct rk_vop_plane {
    156  1.13  riastrad 	struct drm_plane	base;
    157  1.13  riastrad 	struct rk_vop_softc	*sc;
    158  1.13  riastrad };
    159  1.13  riastrad 
    160   1.1  jmcneill struct rk_vop_softc {
    161   1.1  jmcneill 	device_t		sc_dev;
    162   1.1  jmcneill 	bus_space_tag_t		sc_bst;
    163   1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    164   1.1  jmcneill 	int			sc_phandle;
    165   1.1  jmcneill 
    166   1.1  jmcneill 	struct clk		*sc_dclk;
    167   1.1  jmcneill 
    168  1.13  riastrad 	struct rk_vop_plane	sc_plane;
    169   1.1  jmcneill 	struct rk_vop_crtc	sc_crtc;
    170   1.1  jmcneill 
    171   1.1  jmcneill 	struct fdt_device_ports	sc_ports;
    172   1.1  jmcneill 
    173   1.7   thorpej 	const struct rk_vop_config *sc_conf;
    174  1.16  riastrad 
    175  1.16  riastrad 	/* vblank */
    176  1.16  riastrad 	void			*sc_ih;
    177  1.16  riastrad 	kmutex_t		sc_intr_lock;
    178  1.16  riastrad 	struct drm_pending_vblank_event *sc_event;
    179   1.1  jmcneill };
    180   1.1  jmcneill 
    181   1.1  jmcneill #define	to_rk_vop_crtc(x)	container_of(x, struct rk_vop_crtc, base)
    182  1.13  riastrad #define	to_rk_vop_plane(x)	container_of(x, struct rk_vop_plane, base)
    183   1.1  jmcneill 
    184   1.1  jmcneill #define	RD4(sc, reg)				\
    185   1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    186   1.1  jmcneill #define	WR4(sc, reg, val)			\
    187   1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    188   1.1  jmcneill 
    189  1.16  riastrad static void
    190  1.16  riastrad WR4_MASK(struct rk_vop_softc *sc, bus_size_t reg, uint16_t mask, uint16_t val)
    191  1.16  riastrad {
    192  1.16  riastrad 
    193  1.16  riastrad 	KASSERT(val == (mask & val));
    194  1.16  riastrad 	WR4(sc, reg, ((uint32_t)mask << 16) | val);
    195  1.16  riastrad }
    196  1.16  riastrad 
    197   1.1  jmcneill struct rk_vop_config {
    198   1.1  jmcneill 	const char		*descr;
    199   1.1  jmcneill 	u_int			out_mode;
    200   1.1  jmcneill 	void			(*init)(struct rk_vop_softc *);
    201   1.1  jmcneill 	void			(*set_polarity)(struct rk_vop_softc *,
    202   1.1  jmcneill 						enum vop_ep_type, uint32_t);
    203   1.1  jmcneill };
    204   1.1  jmcneill 
    205  1.13  riastrad static const uint32_t rk_vop_layer_formats[] = {
    206  1.13  riastrad 	DRM_FORMAT_ARGB8888,
    207  1.13  riastrad 	DRM_FORMAT_XRGB8888,
    208  1.13  riastrad };
    209  1.13  riastrad 
    210  1.13  riastrad static const uint64_t rk_vop_layer_modifiers[] = {
    211  1.13  riastrad 	DRM_FORMAT_MOD_LINEAR,
    212  1.13  riastrad 	DRM_FORMAT_MOD_INVALID
    213  1.13  riastrad };
    214  1.13  riastrad 
    215   1.1  jmcneill #define	RK3399_VOP_MIPI_POL	__BITS(31,28)
    216   1.1  jmcneill #define	RK3399_VOP_EDP_POL	__BITS(27,24)
    217   1.1  jmcneill #define	RK3399_VOP_HDMI_POL	__BITS(23,20)
    218   1.1  jmcneill #define	RK3399_VOP_DP_POL	__BITS(19,16)
    219   1.1  jmcneill 
    220   1.1  jmcneill #define	RK3399_VOP_SYS_CTRL_ENABLE	__BIT(11)
    221   1.1  jmcneill 
    222   1.1  jmcneill static void
    223   1.1  jmcneill rk3399_vop_set_polarity(struct rk_vop_softc *sc, enum vop_ep_type ep_type, uint32_t pol)
    224   1.1  jmcneill {
    225   1.1  jmcneill 	uint32_t mask, val;
    226   1.1  jmcneill 
    227   1.1  jmcneill 	switch (ep_type) {
    228   1.1  jmcneill 	case VOP_EP_MIPI:
    229   1.1  jmcneill 	case VOP_EP_MIPI1:
    230   1.1  jmcneill 		mask = RK3399_VOP_MIPI_POL;
    231   1.1  jmcneill 		break;
    232   1.1  jmcneill 	case VOP_EP_EDP:
    233   1.1  jmcneill 		mask = RK3399_VOP_EDP_POL;
    234   1.1  jmcneill 		break;
    235   1.1  jmcneill 	case VOP_EP_HDMI:
    236   1.1  jmcneill 		mask = RK3399_VOP_HDMI_POL;
    237   1.1  jmcneill 		break;
    238   1.1  jmcneill 	case VOP_EP_DP:
    239   1.1  jmcneill 		mask = RK3399_VOP_DP_POL;
    240   1.1  jmcneill 		break;
    241   1.1  jmcneill 	default:
    242   1.1  jmcneill 		return;
    243   1.1  jmcneill 	}
    244   1.1  jmcneill 
    245   1.1  jmcneill 	val = RD4(sc, VOP_DSP_CTRL1);
    246   1.1  jmcneill 	val &= ~mask;
    247   1.1  jmcneill 	val |= __SHIFTIN(pol, mask);
    248   1.1  jmcneill 	WR4(sc, VOP_DSP_CTRL1, val);
    249   1.1  jmcneill }
    250   1.1  jmcneill 
    251   1.1  jmcneill static void
    252   1.1  jmcneill rk3399_vop_init(struct rk_vop_softc *sc)
    253   1.1  jmcneill {
    254   1.1  jmcneill 	uint32_t val;
    255   1.1  jmcneill 
    256   1.1  jmcneill 	val = RD4(sc, VOP_SYS_CTRL);
    257   1.1  jmcneill 	val |= RK3399_VOP_SYS_CTRL_ENABLE;
    258   1.1  jmcneill 	WR4(sc, VOP_SYS_CTRL, val);
    259   1.1  jmcneill }
    260   1.1  jmcneill 
    261   1.1  jmcneill static const struct rk_vop_config rk3399_vop_lit_config = {
    262   1.1  jmcneill 	.descr = "RK3399 VOPL",
    263   1.1  jmcneill 	.out_mode = DSP_OUT_MODE_RGB888,
    264   1.1  jmcneill 	.init = rk3399_vop_init,
    265   1.1  jmcneill 	.set_polarity = rk3399_vop_set_polarity,
    266   1.1  jmcneill };
    267   1.1  jmcneill 
    268   1.1  jmcneill static const struct rk_vop_config rk3399_vop_big_config = {
    269   1.1  jmcneill 	.descr = "RK3399 VOPB",
    270   1.1  jmcneill 	.out_mode = DSP_OUT_MODE_RGBaaa,
    271   1.1  jmcneill 	.init = rk3399_vop_init,
    272   1.1  jmcneill 	.set_polarity = rk3399_vop_set_polarity,
    273   1.1  jmcneill };
    274   1.1  jmcneill 
    275   1.7   thorpej static const struct device_compatible_entry compat_data[] = {
    276   1.7   thorpej 	{ .compat = "rockchip,rk3399-vop-big",
    277   1.7   thorpej 	  .data = &rk3399_vop_big_config },
    278   1.7   thorpej 	{ .compat = "rockchip,rk3399-vop-lit",
    279   1.7   thorpej 	  .data = &rk3399_vop_lit_config },
    280   1.7   thorpej 
    281   1.9   thorpej 	DEVICE_COMPAT_EOL
    282   1.1  jmcneill };
    283   1.1  jmcneill 
    284   1.1  jmcneill static int
    285  1.13  riastrad rk_vop_plane_atomic_check(struct drm_plane *plane,
    286  1.13  riastrad     struct drm_plane_state *state)
    287  1.13  riastrad {
    288  1.13  riastrad 	struct drm_crtc_state *crtc_state;
    289  1.13  riastrad 
    290  1.13  riastrad 	if (state->crtc == NULL)
    291  1.13  riastrad 		return 0;
    292  1.13  riastrad 
    293  1.13  riastrad 	crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
    294  1.13  riastrad 	if (IS_ERR(crtc_state))
    295  1.13  riastrad 		return PTR_ERR(crtc_state);
    296  1.13  riastrad 
    297  1.13  riastrad 	return drm_atomic_helper_check_plane_state(state, crtc_state,
    298  1.13  riastrad 	    DRM_PLANE_HELPER_NO_SCALING, DRM_PLANE_HELPER_NO_SCALING,
    299  1.13  riastrad 	    false, true);
    300  1.13  riastrad }
    301  1.13  riastrad 
    302  1.13  riastrad static void
    303  1.13  riastrad rk_vop_plane_atomic_update(struct drm_plane *plane,
    304  1.13  riastrad     struct drm_plane_state *old_state)
    305   1.1  jmcneill {
    306  1.13  riastrad 	struct rk_vop_plane *vop_plane = to_rk_vop_plane(plane);
    307  1.13  riastrad 	struct rk_vop_softc * const sc = vop_plane->sc;
    308  1.13  riastrad 	struct rk_drm_framebuffer *sfb =
    309  1.13  riastrad 	    to_rk_drm_framebuffer(plane->state->fb);
    310  1.13  riastrad 	struct drm_display_mode *mode = &plane->state->crtc->mode;
    311  1.13  riastrad 	struct drm_rect *src = &plane->state->src;
    312  1.13  riastrad 	struct drm_rect *dst = &plane->state->dst;
    313  1.13  riastrad 	uint32_t act_width, act_height, dsp_width, dsp_height;
    314  1.13  riastrad 	uint32_t htotal, hsync_start;
    315  1.13  riastrad 	uint32_t vtotal, vsync_start;
    316  1.13  riastrad 	uint32_t lb_mode;
    317  1.13  riastrad 	uint32_t block_h, block_w, x, y, block_start_y, num_hblocks;
    318  1.13  riastrad 	uint64_t paddr;
    319  1.13  riastrad 	uint32_t val;
    320  1.13  riastrad 
    321  1.13  riastrad 	act_width = drm_rect_width(src) >> 16;
    322  1.13  riastrad 	act_height = drm_rect_height(src) >> 16;
    323  1.13  riastrad 	val = __SHIFTIN(act_width - 1, WIN0_ACT_WIDTH) |
    324  1.13  riastrad 	      __SHIFTIN(act_height - 1, WIN0_ACT_HEIGHT);
    325  1.13  riastrad 	WR4(sc, VOP_WIN0_ACT_INFO, val);
    326  1.13  riastrad 
    327  1.13  riastrad 	dsp_width = drm_rect_width(dst);
    328  1.13  riastrad 	dsp_height = drm_rect_height(dst);
    329  1.13  riastrad 	val = __SHIFTIN(dsp_width - 1, WIN0_DSP_WIDTH) |
    330  1.13  riastrad 	      __SHIFTIN(dsp_height - 1, WIN0_DSP_HEIGHT);
    331  1.13  riastrad 	WR4(sc, VOP_WIN0_DSP_INFO, val);
    332  1.13  riastrad 
    333  1.13  riastrad 	htotal = mode->htotal;
    334  1.13  riastrad 	hsync_start = mode->hsync_start;
    335  1.13  riastrad 	vtotal = mode->vtotal;
    336  1.13  riastrad 	vsync_start = mode->vsync_start;
    337  1.13  riastrad 	val = __SHIFTIN(dst->x1 + htotal - hsync_start, WIN0_DSP_XST) |
    338  1.13  riastrad 	      __SHIFTIN(dst->y1 + vtotal - vsync_start, WIN0_DSP_YST);
    339  1.13  riastrad 	WR4(sc, VOP_WIN0_DSP_ST, val);
    340  1.13  riastrad 
    341  1.13  riastrad 	WR4(sc, VOP_WIN0_COLOR_KEY, 0);
    342  1.13  riastrad 
    343  1.13  riastrad 	if (act_width > 2560)
    344  1.13  riastrad 		lb_mode = WIN0_LB_MODE_RGB_3840X2;
    345  1.13  riastrad 	else if (act_width > 1920)
    346  1.13  riastrad 		lb_mode = WIN0_LB_MODE_RGB_2560X4;
    347  1.13  riastrad 	else if (act_width > 1280)
    348  1.13  riastrad 		lb_mode = WIN0_LB_MODE_RGB_1920X5;
    349  1.13  riastrad 	else
    350  1.13  riastrad 		lb_mode = WIN0_LB_MODE_RGB_1280X8;
    351  1.13  riastrad 	val = __SHIFTIN(lb_mode, WIN0_LB_MODE) |
    352  1.13  riastrad 	      __SHIFTIN(WIN0_DATA_FMT_ARGB888, WIN0_DATA_FMT) |
    353  1.13  riastrad 	      WIN0_EN;
    354  1.13  riastrad 	WR4(sc, VOP_WIN0_CTRL, val);
    355  1.13  riastrad 
    356  1.13  riastrad 	paddr = (uint64_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
    357  1.13  riastrad 	paddr += sfb->base.offsets[0];
    358   1.1  jmcneill 
    359  1.13  riastrad 	block_h = drm_format_info_block_height(sfb->base.format, 0);
    360  1.13  riastrad 	block_w = drm_format_info_block_width(sfb->base.format, 0);
    361  1.13  riastrad 	x = plane->state->src_x >> 16;
    362  1.13  riastrad 	y = plane->state->src_y >> 16;
    363  1.13  riastrad 	block_start_y = (y / block_h) * block_h;
    364  1.13  riastrad 	num_hblocks = x / block_w;
    365   1.1  jmcneill 
    366  1.13  riastrad 	paddr += block_start_y * sfb->base.pitches[0];
    367  1.13  riastrad 	paddr += sfb->base.format->char_per_block[0] * num_hblocks;
    368   1.7   thorpej 
    369  1.13  riastrad 	DRM_DEBUG_KMS("[PLANE:%s] fb=%p paddr=0x%lx\n", plane->name, sfb, paddr);
    370   1.5  jakllsch 
    371   1.1  jmcneill 	KASSERT((paddr & ~0xffffffff) == 0);
    372   1.1  jmcneill 
    373  1.13  riastrad 	val = __SHIFTIN(sfb->base.pitches[0] / 4, WIN0_VIR_STRIDE);
    374  1.13  riastrad 	WR4(sc, VOP_WIN0_VIR, val);
    375   1.5  jakllsch 
    376   1.1  jmcneill 	/* Framebuffer start address */
    377   1.1  jmcneill 	WR4(sc, VOP_WIN0_YRGB_MST, (uint32_t)paddr);
    378  1.13  riastrad }
    379   1.1  jmcneill 
    380  1.13  riastrad static void
    381  1.15  riastrad rk_vop_plane_atomic_disable(struct drm_plane *plane,
    382  1.15  riastrad     struct drm_plane_state *state)
    383  1.13  riastrad {
    384  1.15  riastrad 	struct rk_vop_plane *vop_plane = to_rk_vop_plane(plane);
    385  1.15  riastrad 	struct rk_vop_softc * const sc = vop_plane->sc;
    386  1.15  riastrad 
    387  1.15  riastrad 	WR4(sc, VOP_WIN0_CTRL, 0);	/* clear WIN0_EN */
    388   1.1  jmcneill }
    389   1.1  jmcneill 
    390  1.13  riastrad static const struct drm_plane_helper_funcs rk_vop_plane_helper_funcs = {
    391  1.13  riastrad 	.atomic_check = rk_vop_plane_atomic_check,
    392  1.13  riastrad 	.atomic_update = rk_vop_plane_atomic_update,
    393  1.13  riastrad 	.atomic_disable = rk_vop_plane_atomic_disable,
    394  1.13  riastrad #if 0
    395  1.13  riastrad 	.prepare_fb = drm_gem_vram_plane_helper_prepare_fb,
    396  1.13  riastrad 	.cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb,
    397  1.13  riastrad #endif
    398  1.13  riastrad };
    399  1.13  riastrad 
    400  1.13  riastrad static bool
    401  1.16  riastrad rk_vop_plane_format_mod_supported(struct drm_plane *plane, uint32_t format,
    402  1.13  riastrad     uint64_t modifier)
    403   1.1  jmcneill {
    404  1.13  riastrad 	return modifier == DRM_FORMAT_MOD_LINEAR;
    405   1.1  jmcneill }
    406   1.1  jmcneill 
    407  1.13  riastrad static const struct drm_plane_funcs rk_vop_plane_funcs = {
    408  1.13  riastrad 	.update_plane = drm_atomic_helper_update_plane,
    409  1.13  riastrad 	.disable_plane = drm_atomic_helper_disable_plane,
    410  1.13  riastrad 	.destroy = drm_plane_cleanup,
    411  1.13  riastrad 	.reset = drm_atomic_helper_plane_reset,
    412  1.13  riastrad 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
    413  1.13  riastrad 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
    414  1.16  riastrad 	.format_mod_supported = rk_vop_plane_format_mod_supported,
    415   1.1  jmcneill };
    416   1.1  jmcneill 
    417   1.1  jmcneill static void
    418  1.16  riastrad rk_vop_crtc_dpms(struct drm_crtc *crtc, int mode)
    419   1.1  jmcneill {
    420   1.6       mrg 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    421   1.6       mrg 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    422   1.6       mrg 	uint32_t val;
    423   1.6       mrg 
    424   1.6       mrg 	val = RD4(sc, VOP_SYS_CTRL);
    425   1.6       mrg 
    426   1.6       mrg 	switch (mode) {
    427   1.6       mrg 	case DRM_MODE_DPMS_ON:
    428   1.6       mrg 		val &= ~VOP_STANDBY_EN;
    429   1.6       mrg 		break;
    430   1.6       mrg 	case DRM_MODE_DPMS_STANDBY:
    431   1.6       mrg 	case DRM_MODE_DPMS_SUSPEND:
    432   1.6       mrg 	case DRM_MODE_DPMS_OFF:
    433   1.6       mrg 		val |= VOP_STANDBY_EN;
    434   1.6       mrg 		break;
    435   1.6       mrg 	}
    436   1.6       mrg 
    437   1.6       mrg 	WR4(sc, VOP_SYS_CTRL, val);
    438   1.6       mrg 
    439   1.6       mrg 	/* Commit settings */
    440   1.6       mrg 	WR4(sc, VOP_REG_CFG_DONE, REG_LOAD_EN);
    441   1.1  jmcneill }
    442   1.1  jmcneill 
    443  1.13  riastrad static int
    444  1.16  riastrad rk_vop_crtc_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
    445   1.1  jmcneill {
    446  1.13  riastrad 	bool enabled = state->plane_mask & drm_plane_mask(crtc->primary);
    447  1.13  riastrad 
    448  1.13  riastrad 	if (enabled != state->enable)
    449  1.13  riastrad 		return -EINVAL;
    450  1.13  riastrad 
    451  1.13  riastrad 	return drm_atomic_add_affected_planes(state->state, crtc);
    452   1.1  jmcneill }
    453   1.1  jmcneill 
    454  1.13  riastrad static void
    455  1.16  riastrad rk_vop_crtc_atomic_enable(struct drm_crtc *crtc, struct drm_crtc_state *state)
    456   1.1  jmcneill {
    457   1.1  jmcneill 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    458   1.1  jmcneill 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    459  1.13  riastrad 	struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
    460   1.1  jmcneill 	uint32_t val;
    461   1.4  jakllsch 	u_int pol;
    462   1.4  jakllsch 	int connector_type = 0;
    463  1.11  riastrad 	struct drm_connector *connector;
    464  1.11  riastrad 	struct drm_connector_list_iter conn_iter;
    465  1.13  riastrad 	int error;
    466   1.1  jmcneill 
    467   1.1  jmcneill 	const u_int hactive = adjusted_mode->hdisplay;
    468   1.1  jmcneill 	const u_int hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
    469   1.1  jmcneill 	const u_int hback_porch = adjusted_mode->htotal - adjusted_mode->hsync_end;
    470   1.4  jakllsch 	const u_int hfront_porch = adjusted_mode->hsync_start - adjusted_mode->hdisplay;
    471   1.1  jmcneill 
    472   1.1  jmcneill 	const u_int vactive = adjusted_mode->vdisplay;
    473   1.1  jmcneill 	const u_int vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
    474   1.1  jmcneill 	const u_int vback_porch = adjusted_mode->vtotal - adjusted_mode->vsync_end;
    475   1.4  jakllsch 	const u_int vfront_porch = adjusted_mode->vsync_start - adjusted_mode->vdisplay;
    476   1.1  jmcneill 
    477   1.1  jmcneill 	error = clk_set_rate(sc->sc_dclk, adjusted_mode->clock * 1000);
    478  1.13  riastrad 	if (error)
    479   1.1  jmcneill 		DRM_ERROR("couldn't set pixel clock: %d\n", error);
    480   1.1  jmcneill 
    481   1.4  jakllsch 	pol = DSP_DCLK_POL;
    482   1.4  jakllsch 	if ((adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) != 0)
    483   1.4  jakllsch 		pol |= DSP_HSYNC_POL;
    484   1.4  jakllsch 	if ((adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) != 0)
    485   1.4  jakllsch 		pol |= DSP_VSYNC_POL;
    486   1.4  jakllsch 
    487  1.11  riastrad 	drm_connector_list_iter_begin(crtc->dev, &conn_iter);
    488  1.11  riastrad 	drm_for_each_connector_iter(connector, &conn_iter) {
    489  1.11  riastrad 		if (connector->encoder == NULL)
    490   1.4  jakllsch 			continue;
    491   1.4  jakllsch 		if (connector->encoder->crtc == crtc) {
    492   1.4  jakllsch 			connector_type = connector->connector_type;
    493   1.4  jakllsch 			break;
    494   1.4  jakllsch 		}
    495   1.4  jakllsch 	}
    496  1.11  riastrad 	drm_connector_list_iter_end(&conn_iter);
    497   1.4  jakllsch 
    498   1.4  jakllsch 	switch (connector_type) {
    499   1.4  jakllsch 	case DRM_MODE_CONNECTOR_HDMIA:
    500   1.4  jakllsch 		sc->sc_conf->set_polarity(sc, VOP_EP_HDMI, pol);
    501   1.4  jakllsch 		break;
    502   1.4  jakllsch 	case DRM_MODE_CONNECTOR_eDP:
    503   1.4  jakllsch 		sc->sc_conf->set_polarity(sc, VOP_EP_EDP, pol);
    504   1.4  jakllsch 		break;
    505   1.4  jakllsch 	}
    506   1.4  jakllsch 
    507   1.4  jakllsch 	val = RD4(sc, VOP_SYS_CTRL);
    508   1.4  jakllsch 	val &= ~VOP_STANDBY_EN;
    509   1.4  jakllsch 	val &= ~(MIPI_OUT_EN|EDP_OUT_EN|HDMI_OUT_EN|RGB_OUT_EN);
    510   1.4  jakllsch 
    511   1.4  jakllsch 	switch (connector_type) {
    512   1.4  jakllsch 	case DRM_MODE_CONNECTOR_HDMIA:
    513   1.4  jakllsch 		val |= HDMI_OUT_EN;
    514   1.4  jakllsch 		break;
    515   1.4  jakllsch 	case DRM_MODE_CONNECTOR_eDP:
    516   1.4  jakllsch 		val |= EDP_OUT_EN;
    517   1.4  jakllsch 		break;
    518   1.4  jakllsch 	}
    519   1.4  jakllsch 	WR4(sc, VOP_SYS_CTRL, val);
    520   1.4  jakllsch 
    521   1.4  jakllsch 	val = RD4(sc, VOP_DSP_CTRL0);
    522   1.4  jakllsch 	val &= ~DSP_OUT_MODE;
    523   1.4  jakllsch 	val |= __SHIFTIN(sc->sc_conf->out_mode, DSP_OUT_MODE);
    524   1.4  jakllsch 	WR4(sc, VOP_DSP_CTRL0, val);
    525   1.4  jakllsch 
    526   1.4  jakllsch 	val = __SHIFTIN(hsync_len + hback_porch, DSP_HACT_ST_POST) |
    527   1.4  jakllsch 	      __SHIFTIN(hsync_len + hback_porch + hactive, DSP_HACT_END_POST);
    528   1.4  jakllsch 	WR4(sc, VOP_POST_DSP_HACT_INFO, val);
    529   1.4  jakllsch 
    530   1.4  jakllsch 	val = __SHIFTIN(hsync_len + hback_porch, DSP_HACT_ST) |
    531   1.4  jakllsch 	      __SHIFTIN(hsync_len + hback_porch + hactive, DSP_HACT_END);
    532   1.4  jakllsch 	WR4(sc, VOP_DSP_HACT_ST_END, val);
    533   1.4  jakllsch 
    534   1.4  jakllsch 	val = __SHIFTIN(hsync_len, DSP_HTOTAL) |
    535   1.4  jakllsch 	      __SHIFTIN(hsync_len + hback_porch + hactive + hfront_porch, DSP_HS_END);
    536   1.4  jakllsch 	WR4(sc, VOP_DSP_HTOTAL_HS_END, val);
    537   1.4  jakllsch 
    538   1.4  jakllsch 	val = __SHIFTIN(vsync_len + vback_porch, DSP_VACT_ST_POST) |
    539   1.4  jakllsch 	      __SHIFTIN(vsync_len + vback_porch + vactive, DSP_VACT_END_POST);
    540   1.4  jakllsch 	WR4(sc, VOP_POST_DSP_VACT_INFO, val);
    541   1.4  jakllsch 
    542   1.4  jakllsch 	val = __SHIFTIN(vsync_len + vback_porch, DSP_VACT_ST) |
    543   1.4  jakllsch 	      __SHIFTIN(vsync_len + vback_porch + vactive, DSP_VACT_END);
    544   1.4  jakllsch 	WR4(sc, VOP_DSP_VACT_ST_END, val);
    545   1.4  jakllsch 
    546   1.4  jakllsch 	val = __SHIFTIN(vsync_len, DSP_VTOTAL) |
    547   1.4  jakllsch 	      __SHIFTIN(vsync_len + vback_porch + vactive + vfront_porch, DSP_VS_END);
    548   1.4  jakllsch 	WR4(sc, VOP_DSP_VTOTAL_VS_END, val);
    549  1.16  riastrad 
    550  1.16  riastrad 	drm_crtc_vblank_on(crtc);
    551   1.1  jmcneill }
    552   1.1  jmcneill 
    553   1.1  jmcneill static void
    554  1.16  riastrad rk_vop_crtc_atomic_disable(struct drm_crtc *crtc, struct drm_crtc_state *state)
    555   1.1  jmcneill {
    556   1.1  jmcneill 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    557   1.1  jmcneill 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    558  1.16  riastrad 	uint32_t val;
    559  1.16  riastrad 
    560  1.16  riastrad 	drm_crtc_vblank_off(crtc);
    561  1.16  riastrad 
    562  1.16  riastrad 	val = RD4(sc, VOP_SYS_CTRL);
    563  1.16  riastrad 	val |= VOP_STANDBY_EN;
    564  1.16  riastrad 	WR4(sc, VOP_SYS_CTRL, val);
    565  1.16  riastrad 
    566  1.16  riastrad 	if (crtc->state->event && !crtc->state->active) {
    567  1.16  riastrad 		spin_lock(&crtc->dev->event_lock);
    568  1.16  riastrad 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
    569  1.16  riastrad 		spin_unlock(&crtc->dev->event_lock);
    570  1.16  riastrad 
    571  1.16  riastrad 		crtc->state->event = NULL;
    572  1.16  riastrad 	}
    573  1.16  riastrad }
    574  1.16  riastrad 
    575  1.16  riastrad static void
    576  1.16  riastrad rk_vop_crtc_atomic_flush(struct drm_crtc *crtc, struct drm_crtc_state *state)
    577  1.16  riastrad {
    578  1.16  riastrad 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    579  1.16  riastrad 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    580  1.16  riastrad 	int ret;
    581   1.1  jmcneill 
    582   1.1  jmcneill 	/* Commit settings */
    583   1.1  jmcneill 	WR4(sc, VOP_REG_CFG_DONE, REG_LOAD_EN);
    584  1.16  riastrad 
    585  1.16  riastrad 	/*
    586  1.16  riastrad 	 * If caller wants a vblank event, tell the vblank interrupt
    587  1.16  riastrad 	 * handler to send it on the next interrupt.
    588  1.16  riastrad 	 */
    589  1.16  riastrad 	spin_lock(&crtc->dev->event_lock);
    590  1.16  riastrad 	if (crtc->state->event) {
    591  1.16  riastrad 		if ((ret = drm_crtc_vblank_get_locked(crtc)) != 0)
    592  1.16  riastrad 			aprint_error_dev(sc->sc_dev,
    593  1.16  riastrad 			    "drm_crtc_vblank_get: %d\n", ret);
    594  1.16  riastrad 		if (sc->sc_event) /* XXX leaky; KASSERT? */
    595  1.16  riastrad 			aprint_error_dev(sc->sc_dev, "unfinished vblank\n");
    596  1.16  riastrad 		sc->sc_event = crtc->state->event;
    597  1.16  riastrad 		crtc->state->event = NULL;
    598  1.16  riastrad 	}
    599  1.16  riastrad 	spin_unlock(&crtc->dev->event_lock);
    600   1.1  jmcneill }
    601   1.1  jmcneill 
    602   1.1  jmcneill static const struct drm_crtc_helper_funcs rk_vop_crtc_helper_funcs = {
    603  1.16  riastrad 	.dpms = rk_vop_crtc_dpms,
    604  1.16  riastrad 	.atomic_check = rk_vop_crtc_atomic_check,
    605  1.16  riastrad 	.atomic_enable = rk_vop_crtc_atomic_enable,
    606  1.16  riastrad 	.atomic_disable = rk_vop_crtc_atomic_disable,
    607  1.16  riastrad 	.atomic_flush = rk_vop_crtc_atomic_flush,
    608  1.13  riastrad };
    609  1.13  riastrad 
    610  1.16  riastrad static int
    611  1.16  riastrad rk_vop_crtc_enable_vblank(struct drm_crtc *crtc)
    612  1.16  riastrad {
    613  1.16  riastrad 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    614  1.16  riastrad 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    615  1.16  riastrad 
    616  1.16  riastrad 	mutex_spin_enter(&sc->sc_intr_lock);
    617  1.16  riastrad 	WR4_MASK(sc, VOP_INTR_CLEAR0, VOP_INTR0_FS_NEW, VOP_INTR0_FS_NEW);
    618  1.16  riastrad 	WR4_MASK(sc, VOP_INTR_EN0, VOP_INTR0_FS_NEW, VOP_INTR0_FS_NEW);
    619  1.16  riastrad 	mutex_spin_exit(&sc->sc_intr_lock);
    620  1.16  riastrad 
    621  1.16  riastrad 	return 0;
    622  1.16  riastrad }
    623  1.16  riastrad 
    624  1.16  riastrad static void
    625  1.16  riastrad rk_vop_crtc_disable_vblank(struct drm_crtc *crtc)
    626  1.16  riastrad {
    627  1.16  riastrad 	struct rk_vop_crtc *mixer_crtc = to_rk_vop_crtc(crtc);
    628  1.16  riastrad 	struct rk_vop_softc * const sc = mixer_crtc->sc;
    629  1.16  riastrad 
    630  1.16  riastrad 	mutex_spin_enter(&sc->sc_intr_lock);
    631  1.16  riastrad 	WR4_MASK(sc, VOP_INTR_EN0, VOP_INTR0_FS_NEW, 0);
    632  1.16  riastrad 	mutex_spin_exit(&sc->sc_intr_lock);
    633  1.16  riastrad }
    634  1.16  riastrad 
    635  1.13  riastrad static const struct drm_crtc_funcs rk_vop_crtc_funcs = {
    636  1.13  riastrad 	.set_config = drm_atomic_helper_set_config,
    637  1.13  riastrad 	.destroy = drm_crtc_cleanup,
    638  1.13  riastrad 	.page_flip = drm_atomic_helper_page_flip,
    639  1.13  riastrad 	.reset = drm_atomic_helper_crtc_reset,
    640  1.13  riastrad 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
    641  1.13  riastrad 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
    642  1.16  riastrad 	.enable_vblank = rk_vop_crtc_enable_vblank,
    643  1.16  riastrad 	.disable_vblank = rk_vop_crtc_disable_vblank,
    644   1.1  jmcneill };
    645   1.1  jmcneill 
    646   1.1  jmcneill static int
    647   1.1  jmcneill rk_vop_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    648   1.1  jmcneill {
    649   1.1  jmcneill 	struct rk_vop_softc * const sc = device_private(dev);
    650   1.1  jmcneill 	struct drm_device *ddev;
    651  1.13  riastrad 	int error;
    652   1.1  jmcneill 
    653   1.1  jmcneill 	if (!activate)
    654   1.1  jmcneill 		return EINVAL;
    655   1.1  jmcneill 
    656   1.1  jmcneill 	ddev = rk_drm_port_device(&sc->sc_ports);
    657   1.1  jmcneill 	if (ddev == NULL) {
    658   1.1  jmcneill 		DRM_ERROR("couldn't find DRM device\n");
    659   1.1  jmcneill 		return ENXIO;
    660   1.1  jmcneill 	}
    661   1.1  jmcneill 
    662  1.13  riastrad 	if (sc->sc_plane.sc == NULL) {
    663  1.13  riastrad 		sc->sc_plane.sc = sc;
    664  1.13  riastrad 
    665  1.13  riastrad 		error = drm_universal_plane_init(ddev, &sc->sc_plane.base, 0x3,
    666  1.13  riastrad 		    &rk_vop_plane_funcs,
    667  1.13  riastrad 		    rk_vop_layer_formats, __arraycount(rk_vop_layer_formats),
    668  1.13  riastrad 		    rk_vop_layer_modifiers,
    669  1.13  riastrad 		    DRM_PLANE_TYPE_PRIMARY,
    670  1.13  riastrad 		    NULL);
    671  1.13  riastrad 		if (error) {
    672  1.13  riastrad 			DRM_ERROR("couldn't initialize plane: %d\n", error);
    673  1.13  riastrad 			return ENXIO;
    674  1.13  riastrad 		}
    675  1.13  riastrad 		drm_plane_helper_add(&sc->sc_plane.base, &rk_vop_plane_helper_funcs);
    676  1.13  riastrad 	}
    677  1.13  riastrad 
    678   1.1  jmcneill 	if (sc->sc_crtc.sc == NULL) {
    679   1.1  jmcneill 		sc->sc_crtc.sc = sc;
    680   1.1  jmcneill 
    681  1.13  riastrad 		drm_crtc_init_with_planes(ddev, &sc->sc_crtc.base,
    682  1.13  riastrad 		    &sc->sc_plane.base, NULL, &rk_vop_crtc_funcs, NULL);
    683   1.1  jmcneill 		drm_crtc_helper_add(&sc->sc_crtc.base, &rk_vop_crtc_helper_funcs);
    684   1.1  jmcneill 
    685   1.1  jmcneill 		aprint_debug_dev(dev, "using CRTC %d for %s\n",
    686   1.1  jmcneill 		    drm_crtc_index(&sc->sc_crtc.base), sc->sc_conf->descr);
    687   1.1  jmcneill 	}
    688   1.1  jmcneill 
    689   1.1  jmcneill 	const u_int ep_index = fdt_endpoint_index(ep);
    690   1.4  jakllsch 	if (ep_index >= VOP_NEP) {
    691   1.3       mrg 		DRM_ERROR("endpoint index %d out of range\n", ep_index);
    692   1.3       mrg 		return ENXIO;
    693   1.1  jmcneill 	}
    694   1.1  jmcneill 
    695   1.1  jmcneill 	return fdt_endpoint_activate(ep, activate);
    696   1.1  jmcneill }
    697   1.1  jmcneill 
    698   1.1  jmcneill static void *
    699   1.1  jmcneill rk_vop_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    700   1.1  jmcneill {
    701   1.1  jmcneill 	struct rk_vop_softc * const sc = device_private(dev);
    702   1.1  jmcneill 
    703   1.4  jakllsch 	return &sc->sc_crtc.base;
    704   1.1  jmcneill }
    705   1.1  jmcneill 
    706   1.1  jmcneill static int
    707  1.16  riastrad rk_vop_intr(void *cookie)
    708  1.16  riastrad {
    709  1.16  riastrad 	struct rk_vop_softc * const sc = cookie;
    710  1.16  riastrad 	struct drm_crtc *crtc = &sc->sc_crtc.base;
    711  1.16  riastrad 	struct drm_device *ddev;
    712  1.16  riastrad 	uint32_t intr;
    713  1.16  riastrad 	int ours = 0;
    714  1.16  riastrad 
    715  1.16  riastrad 	mutex_spin_enter(&sc->sc_intr_lock);
    716  1.16  riastrad 	intr = RD4(sc, VOP_INTR_STATUS0);
    717  1.16  riastrad 	WR4_MASK(sc, VOP_INTR_CLEAR0, intr, intr);
    718  1.16  riastrad 	mutex_spin_exit(&sc->sc_intr_lock);
    719  1.16  riastrad 
    720  1.16  riastrad 	ddev = rk_drm_port_device(&sc->sc_ports);
    721  1.16  riastrad 	KASSERT(ddev);
    722  1.16  riastrad 
    723  1.16  riastrad 	if (intr & VOP_INTR0_FS_NEW) {
    724  1.16  riastrad 		intr &= ~VOP_INTR0_FS_NEW;
    725  1.16  riastrad 		ours = 1;
    726  1.16  riastrad 
    727  1.16  riastrad 		/* XXX defer to softint? */
    728  1.16  riastrad 		drm_crtc_handle_vblank(&sc->sc_crtc.base);
    729  1.16  riastrad 		spin_lock(&ddev->event_lock);
    730  1.16  riastrad 		if (sc->sc_event) {
    731  1.16  riastrad 			drm_crtc_send_vblank_event(crtc, sc->sc_event);
    732  1.16  riastrad 			sc->sc_event = NULL;
    733  1.16  riastrad 			drm_crtc_vblank_put(crtc);
    734  1.16  riastrad 		}
    735  1.16  riastrad 		spin_unlock(&ddev->event_lock);
    736  1.16  riastrad 	}
    737  1.16  riastrad 
    738  1.16  riastrad 	if (intr) {
    739  1.16  riastrad 		aprint_error_dev(sc->sc_dev, "unhandled interrupts: 0x%04x\n",
    740  1.16  riastrad 		    intr);
    741  1.16  riastrad 	}
    742  1.16  riastrad 
    743  1.16  riastrad 	return ours;
    744  1.16  riastrad }
    745  1.16  riastrad 
    746  1.16  riastrad static int
    747   1.1  jmcneill rk_vop_match(device_t parent, cfdata_t cf, void *aux)
    748   1.1  jmcneill {
    749   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    750   1.1  jmcneill 
    751  1.10   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    752   1.1  jmcneill }
    753   1.1  jmcneill 
    754   1.1  jmcneill static void
    755   1.1  jmcneill rk_vop_attach(device_t parent, device_t self, void *aux)
    756   1.1  jmcneill {
    757   1.1  jmcneill 	struct rk_vop_softc * const sc = device_private(self);
    758   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    759   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    760  1.16  riastrad 	char intrstr[128];
    761   1.1  jmcneill 	const char * const reset_names[] = { "axi", "ahb", "dclk" };
    762   1.1  jmcneill 	const char * const clock_names[] = { "aclk_vop", "hclk_vop" };
    763   1.1  jmcneill 	struct fdtbus_reset *rst;
    764   1.1  jmcneill 	bus_addr_t addr;
    765   1.1  jmcneill 	bus_size_t size;
    766   1.1  jmcneill 	u_int n;
    767   1.1  jmcneill 
    768   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    769   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    770   1.1  jmcneill 		return;
    771   1.1  jmcneill 	}
    772   1.1  jmcneill 
    773  1.16  riastrad 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    774  1.16  riastrad 		aprint_error(": failed to decode interrupt\n");
    775  1.16  riastrad 		return;
    776  1.16  riastrad 	}
    777  1.16  riastrad 
    778   1.1  jmcneill 	fdtbus_clock_assign(phandle);
    779   1.1  jmcneill 
    780  1.14  riastrad 	/* assert all the reset signals for 20us */
    781  1.14  riastrad 	for (n = 0; n < __arraycount(reset_names); n++) {
    782  1.14  riastrad 		rst = fdtbus_reset_get(phandle, reset_names[n]);
    783  1.14  riastrad 		if (rst == NULL || fdtbus_reset_assert(rst) != 0) {
    784  1.14  riastrad 			aprint_error(": couldn't assert reset %s\n",
    785  1.14  riastrad 			    reset_names[n]);
    786  1.14  riastrad 			return;
    787  1.14  riastrad 		}
    788  1.14  riastrad 	}
    789  1.14  riastrad 	DELAY(10);
    790   1.1  jmcneill 	for (n = 0; n < __arraycount(reset_names); n++) {
    791   1.1  jmcneill 		rst = fdtbus_reset_get(phandle, reset_names[n]);
    792   1.1  jmcneill 		if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    793  1.14  riastrad 			aprint_error(": couldn't de-assert reset %s\n",
    794  1.14  riastrad 			    reset_names[n]);
    795   1.1  jmcneill 			return;
    796   1.1  jmcneill 		}
    797   1.1  jmcneill 	}
    798  1.14  riastrad 
    799   1.1  jmcneill 	for (n = 0; n < __arraycount(clock_names); n++) {
    800   1.1  jmcneill 		if (fdtbus_clock_enable(phandle, clock_names[n], true) != 0) {
    801   1.1  jmcneill 			aprint_error(": couldn't enable clock %s\n", clock_names[n]);
    802   1.1  jmcneill 			return;
    803   1.1  jmcneill 		}
    804   1.1  jmcneill 	}
    805   1.1  jmcneill 	sc->sc_dclk = fdtbus_clock_get(phandle, "dclk_vop");
    806   1.1  jmcneill 	if (sc->sc_dclk == NULL || clk_enable(sc->sc_dclk) != 0) {
    807   1.1  jmcneill 		aprint_error(": couldn't enable clock %s\n", "dclk_vop");
    808   1.1  jmcneill 		return;
    809   1.1  jmcneill 	}
    810   1.1  jmcneill 
    811   1.1  jmcneill 	sc->sc_dev = self;
    812   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    813   1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    814   1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    815   1.1  jmcneill 		return;
    816   1.1  jmcneill 	}
    817   1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    818  1.10   thorpej 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
    819   1.1  jmcneill 
    820   1.1  jmcneill 	aprint_naive("\n");
    821   1.1  jmcneill 	aprint_normal(": %s\n", sc->sc_conf->descr);
    822   1.1  jmcneill 
    823   1.1  jmcneill 	if (sc->sc_conf->init != NULL)
    824   1.1  jmcneill 		sc->sc_conf->init(sc);
    825   1.1  jmcneill 
    826   1.1  jmcneill 	sc->sc_ports.dp_ep_activate = rk_vop_ep_activate;
    827   1.1  jmcneill 	sc->sc_ports.dp_ep_get_data = rk_vop_ep_get_data;
    828   1.4  jakllsch 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_CRTC);
    829   1.1  jmcneill 
    830   1.1  jmcneill 	const int port_phandle = of_find_firstchild_byname(phandle, "port");
    831   1.1  jmcneill 	if (port_phandle > 0)
    832   1.1  jmcneill 		rk_drm_register_port(port_phandle, &sc->sc_ports);
    833  1.16  riastrad 
    834  1.16  riastrad 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    835  1.16  riastrad 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
    836  1.16  riastrad 	    FDT_INTR_MPSAFE, &rk_vop_intr, sc, device_xname(self));
    837  1.16  riastrad 	if (sc->sc_ih == NULL) {
    838  1.16  riastrad 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    839  1.16  riastrad 		    intrstr);
    840  1.16  riastrad 		return;
    841  1.16  riastrad 	}
    842  1.16  riastrad 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    843   1.1  jmcneill }
    844   1.1  jmcneill 
    845   1.1  jmcneill CFATTACH_DECL_NEW(rk_vop, sizeof(struct rk_vop_softc),
    846   1.1  jmcneill 	rk_vop_match, rk_vop_attach, NULL, NULL);
    847