1/* r128_irq.c -- IRQ handling for radeon -*- linux-c -*- */
2/*
3 * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
4 *
5 * The Weather Channel (TM) funded Tungsten Graphics to develop the
6 * initial release of the Radeon 8500 driver under the XFree86 license.
7 * This notice must be preserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 *    Keith Whitwell <keith@tungstengraphics.com>
30 *    Eric Anholt <anholt@FreeBSD.org>
31 */
32
33#include <drm/drmP.h>
34#include <drm/r128_drm.h>
35#include "r128_drv.h"
36
37u32 r128_get_vblank_counter(struct drm_device *dev, int crtc)
38{
39	const drm_r128_private_t *dev_priv = dev->dev_private;
40
41	if (crtc != 0)
42		return 0;
43
44	return atomic_read(&dev_priv->vbl_received);
45}
46
47irqreturn_t r128_driver_irq_handler(int irq, void *arg)
48{
49	struct drm_device *dev = (struct drm_device *) arg;
50	drm_r128_private_t *dev_priv = (drm_r128_private_t *) dev->dev_private;
51	int status;
52
53	status = R128_READ(R128_GEN_INT_STATUS);
54
55	/* VBLANK interrupt */
56	if (status & R128_CRTC_VBLANK_INT) {
57		R128_WRITE(R128_GEN_INT_STATUS, R128_CRTC_VBLANK_INT_AK);
58		atomic_inc(&dev_priv->vbl_received);
59		drm_handle_vblank(dev, 0);
60		return IRQ_HANDLED;
61	}
62	return IRQ_NONE;
63}
64
65int r128_enable_vblank(struct drm_device *dev, int crtc)
66{
67	drm_r128_private_t *dev_priv = dev->dev_private;
68
69	if (crtc != 0) {
70		DRM_ERROR("%s:  bad crtc %d\n", __func__, crtc);
71		return -EINVAL;
72	}
73
74	R128_WRITE(R128_GEN_INT_CNTL, R128_CRTC_VBLANK_INT_EN);
75	return 0;
76}
77
78void r128_disable_vblank(struct drm_device *dev, int crtc)
79{
80	if (crtc != 0)
81		DRM_ERROR("%s:  bad crtc %d\n", __func__, crtc);
82
83	/*
84	 * FIXME: implement proper interrupt disable by using the vblank
85	 * counter register (if available)
86	 *
87	 * R128_WRITE(R128_GEN_INT_CNTL,
88	 *            R128_READ(R128_GEN_INT_CNTL) & ~R128_CRTC_VBLANK_INT_EN);
89	 */
90}
91
92void r128_driver_irq_preinstall(struct drm_device *dev)
93{
94	drm_r128_private_t *dev_priv = (drm_r128_private_t *) dev->dev_private;
95
96	/* Disable *all* interrupts */
97	R128_WRITE(R128_GEN_INT_CNTL, 0);
98	/* Clear vblank bit if it's already high */
99	R128_WRITE(R128_GEN_INT_STATUS, R128_CRTC_VBLANK_INT_AK);
100}
101
102int r128_driver_irq_postinstall(struct drm_device *dev)
103{
104	return 0;
105}
106
107void r128_driver_irq_uninstall(struct drm_device *dev)
108{
109	drm_r128_private_t *dev_priv = (drm_r128_private_t *) dev->dev_private;
110	if (!dev_priv)
111		return;
112
113	/* Disable *all* interrupts */
114	R128_WRITE(R128_GEN_INT_CNTL, 0);
115}
116