1 /*
2  * Copyright 2010 Matt Turner.
3  * Copyright 2012 Red Hat
4  *
5  * This file is subject to the terms and conditions of the GNU General
6  * Public License version 2. See the file COPYING in the main
7  * directory of this archive for more details.
8  *
9  * Authors: Matthew Garrett
10  * 	    Matt Turner
11  *	    Dave Airlie
12  */
13 #ifndef __MGAG200_DRV_H__
14 #define __MGAG200_DRV_H__
15 
16 #include <video/vga.h>
17 
18 #include <drm/drm_encoder.h>
19 #include <drm/drm_fb_helper.h>
20 
21 #include <drm/drm_gem.h>
22 #include <drm/drm_gem_vram_helper.h>
23 
24 #include <drm/drm_vram_mm_helper.h>
25 
26 #include <linux/i2c.h>
27 #include <linux/i2c-algo-bit.h>
28 
29 #include "mgag200_reg.h"
30 
31 #define DRIVER_AUTHOR		"Matthew Garrett"
32 
33 #define DRIVER_NAME		"mgag200"
34 #define DRIVER_DESC		"MGA G200 SE"
35 #define DRIVER_DATE		"20110418"
36 
37 #define DRIVER_MAJOR		1
38 #define DRIVER_MINOR		0
39 #define DRIVER_PATCHLEVEL	0
40 
41 #define MGAG200FB_CONN_LIMIT 1
42 
43 #define RREG8(reg) ioread8(((void __iomem *)mdev->rmmio) + (reg))
44 #define WREG8(reg, v) iowrite8(v, ((void __iomem *)mdev->rmmio) + (reg))
45 #define RREG32(reg) ioread32(((void __iomem *)mdev->rmmio) + (reg))
46 #define WREG32(reg, v) iowrite32(v, ((void __iomem *)mdev->rmmio) + (reg))
47 
48 #define ATTR_INDEX 0x1fc0
49 #define ATTR_DATA 0x1fc1
50 
51 #define WREG_ATTR(reg, v)					\
52 	do {							\
53 		RREG8(0x1fda);					\
54 		WREG8(ATTR_INDEX, reg);				\
55 		WREG8(ATTR_DATA, v);				\
56 	} while (0)						\
57 
58 #define WREG_SEQ(reg, v)					\
59 	do {							\
60 		WREG8(MGAREG_SEQ_INDEX, reg);			\
61 		WREG8(MGAREG_SEQ_DATA, v);			\
62 	} while (0)						\
63 
64 #define WREG_CRT(reg, v)					\
65 	do {							\
66 		WREG8(MGAREG_CRTC_INDEX, reg);			\
67 		WREG8(MGAREG_CRTC_DATA, v);			\
68 	} while (0)						\
69 
70 
71 #define WREG_ECRT(reg, v)					\
72 	do {							\
73 		WREG8(MGAREG_CRTCEXT_INDEX, reg);				\
74 		WREG8(MGAREG_CRTCEXT_DATA, v);				\
75 	} while (0)						\
76 
77 #define GFX_INDEX 0x1fce
78 #define GFX_DATA 0x1fcf
79 
80 #define WREG_GFX(reg, v)					\
81 	do {							\
82 		WREG8(GFX_INDEX, reg);				\
83 		WREG8(GFX_DATA, v);				\
84 	} while (0)						\
85 
86 #define DAC_INDEX 0x3c00
87 #define DAC_DATA 0x3c0a
88 
89 #define WREG_DAC(reg, v)					\
90 	do {							\
91 		WREG8(DAC_INDEX, reg);				\
92 		WREG8(DAC_DATA, v);				\
93 	} while (0)						\
94 
95 #define MGA_MISC_OUT 0x1fc2
96 #define MGA_MISC_IN 0x1fcc
97 
98 #define MGAG200_MAX_FB_HEIGHT 4096
99 #define MGAG200_MAX_FB_WIDTH 4096
100 
101 #define MATROX_DPMS_CLEARED (-1)
102 
103 #define to_mga_crtc(x) container_of(x, struct mga_crtc, base)
104 #define to_mga_encoder(x) container_of(x, struct mga_encoder, base)
105 #define to_mga_connector(x) container_of(x, struct mga_connector, base)
106 #define to_mga_framebuffer(x) container_of(x, struct mga_framebuffer, base)
107 
108 struct mga_framebuffer {
109 	struct drm_framebuffer base;
110 	struct drm_gem_object *obj;
111 };
112 
113 struct mga_fbdev {
114 	struct drm_fb_helper helper; /* must be first */
115 	struct mga_framebuffer mfb;
116 	void *sysram;
117 	int size;
118 	int x1, y1, x2, y2; /* dirty rect */
119 	spinlock_t dirty_lock;
120 };
121 
122 struct mga_crtc {
123 	struct drm_crtc base;
124 	u8 lut_r[256], lut_g[256], lut_b[256];
125 	int last_dpms;
126 	bool enabled;
127 };
128 
129 struct mga_mode_info {
130 	bool mode_config_initialized;
131 	struct mga_crtc *crtc;
132 };
133 
134 struct mga_encoder {
135 	struct drm_encoder base;
136 	int last_dpms;
137 };
138 
139 
140 struct mga_i2c_chan {
141 	struct i2c_adapter adapter;
142 	struct drm_device *dev;
143 	struct i2c_algo_bit_data bit;
144 	int data, clock;
145 };
146 
147 struct mga_connector {
148 	struct drm_connector base;
149 	struct mga_i2c_chan *i2c;
150 };
151 
152 struct mga_cursor {
153 	/*
154 	   We have to have 2 buffers for the cursor to avoid occasional
155 	   corruption while switching cursor icons.
156 	   If either of these is NULL, then don't do hardware cursors, and
157 	   fall back to software.
158 	*/
159 	struct drm_gem_vram_object *pixels_1;
160 	struct drm_gem_vram_object *pixels_2;
161 	u64 pixels_1_gpu_addr, pixels_2_gpu_addr;
162 	/* The currently displayed icon, this points to one of pixels_1, or pixels_2 */
163 	struct drm_gem_vram_object *pixels_current;
164 	/* The previously displayed icon */
165 	struct drm_gem_vram_object *pixels_prev;
166 };
167 
168 struct mga_mc {
169 	resource_size_t			vram_size;
170 	resource_size_t			vram_base;
171 	resource_size_t			vram_window;
172 };
173 
174 enum mga_type {
175 	G200_SE_A,
176 	G200_SE_B,
177 	G200_WB,
178 	G200_EV,
179 	G200_EH,
180 	G200_EH3,
181 	G200_ER,
182 	G200_EW3,
183 };
184 
185 #define IS_G200_SE(mdev) (mdev->type == G200_SE_A || mdev->type == G200_SE_B)
186 
187 struct mga_device {
188 	struct drm_device		*dev;
189 	unsigned long			flags;
190 
191 	resource_size_t			rmmio_base;
192 	resource_size_t			rmmio_size;
193 	void __iomem			*rmmio;
194 
195 	struct mga_mc			mc;
196 	struct mga_mode_info		mode_info;
197 
198 	struct mga_fbdev *mfbdev;
199 	struct mga_cursor cursor;
200 
201 	bool				suspended;
202 	int				num_crtc;
203 	enum mga_type			type;
204 	int				has_sdram;
205 	struct drm_display_mode		mode;
206 
207 	int bpp_shifts[4];
208 
209 	int fb_mtrr;
210 
211 	/* SE model number stored in reg 0x1e24 */
212 	u32 unique_rev_id;
213 };
214 
215 				/* mgag200_mode.c */
216 int mgag200_modeset_init(struct mga_device *mdev);
217 void mgag200_modeset_fini(struct mga_device *mdev);
218 
219 				/* mgag200_fb.c */
220 int mgag200_fbdev_init(struct mga_device *mdev);
221 void mgag200_fbdev_fini(struct mga_device *mdev);
222 
223 				/* mgag200_main.c */
224 int mgag200_framebuffer_init(struct drm_device *dev,
225 			     struct mga_framebuffer *mfb,
226 			     const struct drm_mode_fb_cmd2 *mode_cmd,
227 			     struct drm_gem_object *obj);
228 
229 
230 int mgag200_driver_load(struct drm_device *dev, unsigned long flags);
231 void mgag200_driver_unload(struct drm_device *dev);
232 int mgag200_gem_create(struct drm_device *dev,
233 		   u32 size, bool iskernel,
234 		       struct drm_gem_object **obj);
235 int mgag200_dumb_create(struct drm_file *file,
236 			struct drm_device *dev,
237 			struct drm_mode_create_dumb *args);
238 
239 				/* mgag200_i2c.c */
240 struct mga_i2c_chan *mgag200_i2c_create(struct drm_device *dev);
241 void mgag200_i2c_destroy(struct mga_i2c_chan *i2c);
242 
243 int mgag200_mm_init(struct mga_device *mdev);
244 void mgag200_mm_fini(struct mga_device *mdev);
245 int mgag200_mmap(struct file *filp, struct vm_area_struct *vma);
246 
247 int mga_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
248 						uint32_t handle, uint32_t width, uint32_t height);
249 int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
250 
251 #endif				/* __MGAG200_DRV_H__ */
252