blob: 72bec23b66f40720c89baf7025bd388c268893c6 [file] [log] [blame]
Dave Airlie312fec12012-02-29 13:40:04 +00001/*
2 * Copyright 2012 Red Hat Inc.
3 * Parts based on xf86-video-ast
4 * Copyright (c) 2005 ASPEED Technology Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 */
27/*
28 * Authors: Dave Airlie <airlied@redhat.com>
29 */
30#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_crtc_helper.h>
Dave Airlie312fec12012-02-29 13:40:04 +000034#include "ast_drv.h"
35
36#include "ast_tables.h"
37
38static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
39static void ast_i2c_destroy(struct ast_i2c_chan *i2c);
40static int ast_cursor_set(struct drm_crtc *crtc,
41 struct drm_file *file_priv,
42 uint32_t handle,
43 uint32_t width,
44 uint32_t height);
45static int ast_cursor_move(struct drm_crtc *crtc,
46 int x, int y);
47
48static inline void ast_load_palette_index(struct ast_private *ast,
49 u8 index, u8 red, u8 green,
50 u8 blue)
51{
52 ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
53 ast_io_read8(ast, AST_IO_SEQ_PORT);
54 ast_io_write8(ast, AST_IO_DAC_DATA, red);
55 ast_io_read8(ast, AST_IO_SEQ_PORT);
56 ast_io_write8(ast, AST_IO_DAC_DATA, green);
57 ast_io_read8(ast, AST_IO_SEQ_PORT);
58 ast_io_write8(ast, AST_IO_DAC_DATA, blue);
59 ast_io_read8(ast, AST_IO_SEQ_PORT);
60}
61
62static void ast_crtc_load_lut(struct drm_crtc *crtc)
63{
64 struct ast_private *ast = crtc->dev->dev_private;
65 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
66 int i;
67
68 if (!crtc->enabled)
69 return;
70
71 for (i = 0; i < 256; i++)
72 ast_load_palette_index(ast, i, ast_crtc->lut_r[i],
73 ast_crtc->lut_g[i], ast_crtc->lut_b[i]);
74}
75
76static bool ast_get_vbios_mode_info(struct drm_crtc *crtc, struct drm_display_mode *mode,
77 struct drm_display_mode *adjusted_mode,
78 struct ast_vbios_mode_info *vbios_mode)
79{
80 struct ast_private *ast = crtc->dev->dev_private;
81 u32 refresh_rate_index = 0, mode_id, color_index, refresh_rate;
82 u32 hborder, vborder;
83
84 switch (crtc->fb->bits_per_pixel) {
85 case 8:
86 vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
87 color_index = VGAModeIndex - 1;
88 break;
89 case 16:
90 vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
91 color_index = HiCModeIndex;
92 break;
93 case 24:
94 case 32:
95 vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
96 color_index = TrueCModeIndex;
97 break;
98 default:
99 return false;
100 }
101
102 switch (crtc->mode.crtc_hdisplay) {
103 case 640:
104 vbios_mode->enh_table = &res_640x480[refresh_rate_index];
105 break;
106 case 800:
107 vbios_mode->enh_table = &res_800x600[refresh_rate_index];
108 break;
109 case 1024:
110 vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
111 break;
112 case 1280:
113 if (crtc->mode.crtc_vdisplay == 800)
114 vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
115 else
116 vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
117 break;
Dave Airlief1f62f22014-01-17 10:56:09 +1000118 case 1360:
119 vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
120 break;
Dave Airlie312fec12012-02-29 13:40:04 +0000121 case 1440:
122 vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
123 break;
124 case 1600:
Dave Airlief1f62f22014-01-17 10:56:09 +1000125 if (crtc->mode.crtc_vdisplay == 900)
126 vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
127 else
128 vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
Dave Airlie312fec12012-02-29 13:40:04 +0000129 break;
130 case 1680:
131 vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
132 break;
133 case 1920:
134 if (crtc->mode.crtc_vdisplay == 1080)
135 vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
136 else
137 vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
138 break;
139 default:
140 return false;
141 }
142
143 refresh_rate = drm_mode_vrefresh(mode);
144 while (vbios_mode->enh_table->refresh_rate < refresh_rate) {
145 vbios_mode->enh_table++;
146 if ((vbios_mode->enh_table->refresh_rate > refresh_rate) ||
147 (vbios_mode->enh_table->refresh_rate == 0xff)) {
148 vbios_mode->enh_table--;
149 break;
150 }
151 }
152
153 hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
154 vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
155
156 adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
157 adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
158 adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
159 adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
160 vbios_mode->enh_table->hfp;
161 adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
162 vbios_mode->enh_table->hfp +
163 vbios_mode->enh_table->hsync);
164
165 adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
166 adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
167 adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
168 adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
169 vbios_mode->enh_table->vfp;
170 adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
171 vbios_mode->enh_table->vfp +
172 vbios_mode->enh_table->vsync);
173
174 refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
175 mode_id = vbios_mode->enh_table->mode_id;
176
177 if (ast->chip == AST1180) {
178 /* TODO 1180 */
179 } else {
180 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0xf) << 4));
181 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
182 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);
183
Dave Airlief1f62f22014-01-17 10:56:09 +1000184 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
185 if (vbios_mode->enh_table->flags & NewModeInfo) {
186 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
187 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, crtc->fb->bits_per_pixel);
188 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
189 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
190 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
Dave Airlie312fec12012-02-29 13:40:04 +0000191
Dave Airlief1f62f22014-01-17 10:56:09 +1000192 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
193 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
194 }
Dave Airlie312fec12012-02-29 13:40:04 +0000195 }
196
197 return true;
198
199
200}
201static void ast_set_std_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
202 struct ast_vbios_mode_info *vbios_mode)
203{
204 struct ast_private *ast = crtc->dev->dev_private;
205 struct ast_vbios_stdtable *stdtable;
206 u32 i;
207 u8 jreg;
208
209 stdtable = vbios_mode->std_table;
210
211 jreg = stdtable->misc;
212 ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
213
214 /* Set SEQ */
215 ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
216 for (i = 0; i < 4; i++) {
217 jreg = stdtable->seq[i];
218 if (!i)
219 jreg |= 0x20;
220 ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1) , jreg);
221 }
222
223 /* Set CRTC */
224 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
225 for (i = 0; i < 25; i++)
226 ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
227
228 /* set AR */
229 jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
230 for (i = 0; i < 20; i++) {
231 jreg = stdtable->ar[i];
232 ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
233 ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
234 }
235 ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
236 ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);
237
238 jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
239 ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);
240
241 /* Set GR */
242 for (i = 0; i < 9; i++)
243 ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
244}
245
246static void ast_set_crtc_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
247 struct ast_vbios_mode_info *vbios_mode)
248{
249 struct ast_private *ast = crtc->dev->dev_private;
250 u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
251 u16 temp;
252
253 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
254
255 temp = (mode->crtc_htotal >> 3) - 5;
256 if (temp & 0x100)
257 jregAC |= 0x01; /* HT D[8] */
258 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);
259
260 temp = (mode->crtc_hdisplay >> 3) - 1;
261 if (temp & 0x100)
262 jregAC |= 0x04; /* HDE D[8] */
263 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);
264
265 temp = (mode->crtc_hblank_start >> 3) - 1;
266 if (temp & 0x100)
267 jregAC |= 0x10; /* HBS D[8] */
268 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);
269
270 temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
271 if (temp & 0x20)
272 jreg05 |= 0x80; /* HBE D[5] */
273 if (temp & 0x40)
274 jregAD |= 0x01; /* HBE D[5] */
275 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));
276
277 temp = (mode->crtc_hsync_start >> 3) - 1;
278 if (temp & 0x100)
279 jregAC |= 0x40; /* HRS D[5] */
280 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);
281
282 temp = ((mode->crtc_hsync_end >> 3) - 1) & 0x3f;
283 if (temp & 0x20)
284 jregAD |= 0x04; /* HRE D[5] */
285 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
286
287 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
288 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);
289
290 /* vert timings */
291 temp = (mode->crtc_vtotal) - 2;
292 if (temp & 0x100)
293 jreg07 |= 0x01;
294 if (temp & 0x200)
295 jreg07 |= 0x20;
296 if (temp & 0x400)
297 jregAE |= 0x01;
298 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);
299
300 temp = (mode->crtc_vsync_start) - 1;
301 if (temp & 0x100)
302 jreg07 |= 0x04;
303 if (temp & 0x200)
304 jreg07 |= 0x80;
305 if (temp & 0x400)
306 jregAE |= 0x08;
307 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);
308
309 temp = (mode->crtc_vsync_end - 1) & 0x3f;
310 if (temp & 0x10)
311 jregAE |= 0x20;
312 if (temp & 0x20)
313 jregAE |= 0x40;
314 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);
315
316 temp = mode->crtc_vdisplay - 1;
317 if (temp & 0x100)
318 jreg07 |= 0x02;
319 if (temp & 0x200)
320 jreg07 |= 0x40;
321 if (temp & 0x400)
322 jregAE |= 0x02;
323 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);
324
325 temp = mode->crtc_vblank_start - 1;
326 if (temp & 0x100)
327 jreg07 |= 0x08;
328 if (temp & 0x200)
329 jreg09 |= 0x20;
330 if (temp & 0x400)
331 jregAE |= 0x04;
332 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);
333
334 temp = mode->crtc_vblank_end - 1;
335 if (temp & 0x100)
336 jregAE |= 0x10;
337 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);
338
339 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
340 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
341 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));
342
343 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
344}
345
346static void ast_set_offset_reg(struct drm_crtc *crtc)
347{
348 struct ast_private *ast = crtc->dev->dev_private;
349
350 u16 offset;
351
352 offset = crtc->fb->pitches[0] >> 3;
353 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
354 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
355}
356
357static void ast_set_dclk_reg(struct drm_device *dev, struct drm_display_mode *mode,
358 struct ast_vbios_mode_info *vbios_mode)
359{
360 struct ast_private *ast = dev->dev_private;
361 struct ast_vbios_dclk_info *clk_info;
362
363 clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
364
365 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
366 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
367 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
368 (clk_info->param3 & 0x80) | ((clk_info->param3 & 0x3) << 4));
369}
370
371static void ast_set_ext_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
372 struct ast_vbios_mode_info *vbios_mode)
373{
374 struct ast_private *ast = crtc->dev->dev_private;
375 u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
376
377 switch (crtc->fb->bits_per_pixel) {
378 case 8:
379 jregA0 = 0x70;
380 jregA3 = 0x01;
381 jregA8 = 0x00;
382 break;
383 case 15:
384 case 16:
385 jregA0 = 0x70;
386 jregA3 = 0x04;
387 jregA8 = 0x02;
388 break;
389 case 32:
390 jregA0 = 0x70;
391 jregA3 = 0x08;
392 jregA8 = 0x02;
393 break;
394 }
395
396 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
397 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
398 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
399
400 /* Set Threshold */
401 if (ast->chip == AST2300) {
402 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
403 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
404 } else if (ast->chip == AST2100 ||
405 ast->chip == AST1100 ||
406 ast->chip == AST2200 ||
407 ast->chip == AST2150) {
408 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
409 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
410 } else {
411 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
412 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
413 }
414}
415
Rashika7f5ccd42014-01-06 20:02:39 +0530416static void ast_set_sync_reg(struct drm_device *dev, struct drm_display_mode *mode,
Dave Airlie312fec12012-02-29 13:40:04 +0000417 struct ast_vbios_mode_info *vbios_mode)
418{
419 struct ast_private *ast = dev->dev_private;
420 u8 jreg;
421
422 jreg = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
423 jreg |= (vbios_mode->enh_table->flags & SyncNN);
424 ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
425}
426
Rashika7f5ccd42014-01-06 20:02:39 +0530427static bool ast_set_dac_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
Dave Airlie312fec12012-02-29 13:40:04 +0000428 struct ast_vbios_mode_info *vbios_mode)
429{
430 switch (crtc->fb->bits_per_pixel) {
431 case 8:
432 break;
433 default:
434 return false;
435 }
436 return true;
437}
438
Rashika7f5ccd42014-01-06 20:02:39 +0530439static void ast_set_start_address_crt1(struct drm_crtc *crtc, unsigned offset)
Dave Airlie312fec12012-02-29 13:40:04 +0000440{
441 struct ast_private *ast = crtc->dev->dev_private;
442 u32 addr;
443
444 addr = offset >> 2;
445 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
446 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
447 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
448
449}
450
451static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
452{
453 struct ast_private *ast = crtc->dev->dev_private;
454
455 if (ast->chip == AST1180)
456 return;
457
458 switch (mode) {
459 case DRM_MODE_DPMS_ON:
460 case DRM_MODE_DPMS_STANDBY:
461 case DRM_MODE_DPMS_SUSPEND:
462 ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
463 ast_crtc_load_lut(crtc);
464 break;
465 case DRM_MODE_DPMS_OFF:
466 ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
467 break;
468 }
469}
470
471static bool ast_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200472 const struct drm_display_mode *mode,
473 struct drm_display_mode *adjusted_mode)
Dave Airlie312fec12012-02-29 13:40:04 +0000474{
475 return true;
476}
477
478/* ast is different - we will force move buffers out of VRAM */
479static int ast_crtc_do_set_base(struct drm_crtc *crtc,
480 struct drm_framebuffer *fb,
481 int x, int y, int atomic)
482{
483 struct ast_private *ast = crtc->dev->dev_private;
484 struct drm_gem_object *obj;
485 struct ast_framebuffer *ast_fb;
486 struct ast_bo *bo;
487 int ret;
488 u64 gpu_addr;
489
490 /* push the previous fb to system ram */
491 if (!atomic && fb) {
492 ast_fb = to_ast_framebuffer(fb);
493 obj = ast_fb->obj;
494 bo = gem_to_ast_bo(obj);
495 ret = ast_bo_reserve(bo, false);
496 if (ret)
497 return ret;
498 ast_bo_push_sysram(bo);
499 ast_bo_unreserve(bo);
500 }
501
502 ast_fb = to_ast_framebuffer(crtc->fb);
503 obj = ast_fb->obj;
504 bo = gem_to_ast_bo(obj);
505
506 ret = ast_bo_reserve(bo, false);
507 if (ret)
508 return ret;
509
510 ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
511 if (ret) {
512 ast_bo_unreserve(bo);
513 return ret;
514 }
515
516 if (&ast->fbdev->afb == ast_fb) {
517 /* if pushing console in kmap it */
518 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
519 if (ret)
520 DRM_ERROR("failed to kmap fbcon\n");
521 }
522 ast_bo_unreserve(bo);
523
524 ast_set_start_address_crt1(crtc, (u32)gpu_addr);
525
526 return 0;
527}
528
529static int ast_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
530 struct drm_framebuffer *old_fb)
531{
532 return ast_crtc_do_set_base(crtc, old_fb, x, y, 0);
533}
534
535static int ast_crtc_mode_set(struct drm_crtc *crtc,
536 struct drm_display_mode *mode,
537 struct drm_display_mode *adjusted_mode,
538 int x, int y,
539 struct drm_framebuffer *old_fb)
540{
541 struct drm_device *dev = crtc->dev;
542 struct ast_private *ast = crtc->dev->dev_private;
543 struct ast_vbios_mode_info vbios_mode;
544 bool ret;
545 if (ast->chip == AST1180) {
546 DRM_ERROR("AST 1180 modesetting not supported\n");
547 return -EINVAL;
548 }
549
550 ret = ast_get_vbios_mode_info(crtc, mode, adjusted_mode, &vbios_mode);
551 if (ret == false)
552 return -EINVAL;
553 ast_open_key(ast);
554
555 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x04);
556
557 ast_set_std_reg(crtc, adjusted_mode, &vbios_mode);
558 ast_set_crtc_reg(crtc, adjusted_mode, &vbios_mode);
559 ast_set_offset_reg(crtc);
560 ast_set_dclk_reg(dev, adjusted_mode, &vbios_mode);
561 ast_set_ext_reg(crtc, adjusted_mode, &vbios_mode);
562 ast_set_sync_reg(dev, adjusted_mode, &vbios_mode);
563 ast_set_dac_reg(crtc, adjusted_mode, &vbios_mode);
564
565 ast_crtc_mode_set_base(crtc, x, y, old_fb);
566
567 return 0;
568}
569
570static void ast_crtc_disable(struct drm_crtc *crtc)
571{
572
573}
574
575static void ast_crtc_prepare(struct drm_crtc *crtc)
576{
577
578}
579
580static void ast_crtc_commit(struct drm_crtc *crtc)
581{
582 struct ast_private *ast = crtc->dev->dev_private;
583 ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
584}
585
586
587static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
588 .dpms = ast_crtc_dpms,
589 .mode_fixup = ast_crtc_mode_fixup,
590 .mode_set = ast_crtc_mode_set,
591 .mode_set_base = ast_crtc_mode_set_base,
592 .disable = ast_crtc_disable,
593 .load_lut = ast_crtc_load_lut,
Dave Airlie312fec12012-02-29 13:40:04 +0000594 .prepare = ast_crtc_prepare,
595 .commit = ast_crtc_commit,
596
597};
598
599static void ast_crtc_reset(struct drm_crtc *crtc)
600{
601
602}
603
604static void ast_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
605 u16 *blue, uint32_t start, uint32_t size)
606{
607 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
608 int end = (start + size > 256) ? 256 : start + size, i;
609
610 /* userspace palettes are always correct as is */
611 for (i = start; i < end; i++) {
612 ast_crtc->lut_r[i] = red[i] >> 8;
613 ast_crtc->lut_g[i] = green[i] >> 8;
614 ast_crtc->lut_b[i] = blue[i] >> 8;
615 }
616 ast_crtc_load_lut(crtc);
617}
618
619
620static void ast_crtc_destroy(struct drm_crtc *crtc)
621{
622 drm_crtc_cleanup(crtc);
623 kfree(crtc);
624}
625
626static const struct drm_crtc_funcs ast_crtc_funcs = {
627 .cursor_set = ast_cursor_set,
628 .cursor_move = ast_cursor_move,
629 .reset = ast_crtc_reset,
630 .set_config = drm_crtc_helper_set_config,
631 .gamma_set = ast_crtc_gamma_set,
632 .destroy = ast_crtc_destroy,
633};
634
Rashika7f5ccd42014-01-06 20:02:39 +0530635static int ast_crtc_init(struct drm_device *dev)
Dave Airlie312fec12012-02-29 13:40:04 +0000636{
637 struct ast_crtc *crtc;
638 int i;
639
640 crtc = kzalloc(sizeof(struct ast_crtc), GFP_KERNEL);
641 if (!crtc)
642 return -ENOMEM;
643
644 drm_crtc_init(dev, &crtc->base, &ast_crtc_funcs);
645 drm_mode_crtc_set_gamma_size(&crtc->base, 256);
646 drm_crtc_helper_add(&crtc->base, &ast_crtc_helper_funcs);
647
648 for (i = 0; i < 256; i++) {
649 crtc->lut_r[i] = i;
650 crtc->lut_g[i] = i;
651 crtc->lut_b[i] = i;
652 }
653 return 0;
654}
655
656static void ast_encoder_destroy(struct drm_encoder *encoder)
657{
658 drm_encoder_cleanup(encoder);
659 kfree(encoder);
660}
661
662
663static struct drm_encoder *ast_best_single_encoder(struct drm_connector *connector)
664{
665 int enc_id = connector->encoder_ids[0];
666 struct drm_mode_object *obj;
667 struct drm_encoder *encoder;
668
669 /* pick the encoder ids */
670 if (enc_id) {
671 obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
672 if (!obj)
673 return NULL;
674 encoder = obj_to_encoder(obj);
675 return encoder;
676 }
677 return NULL;
678}
679
680
681static const struct drm_encoder_funcs ast_enc_funcs = {
682 .destroy = ast_encoder_destroy,
683};
684
685static void ast_encoder_dpms(struct drm_encoder *encoder, int mode)
686{
687
688}
689
690static bool ast_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200691 const struct drm_display_mode *mode,
Dave Airlie312fec12012-02-29 13:40:04 +0000692 struct drm_display_mode *adjusted_mode)
693{
694 return true;
695}
696
697static void ast_encoder_mode_set(struct drm_encoder *encoder,
698 struct drm_display_mode *mode,
699 struct drm_display_mode *adjusted_mode)
700{
701}
702
703static void ast_encoder_prepare(struct drm_encoder *encoder)
704{
705
706}
707
708static void ast_encoder_commit(struct drm_encoder *encoder)
709{
710
711}
712
713
714static const struct drm_encoder_helper_funcs ast_enc_helper_funcs = {
715 .dpms = ast_encoder_dpms,
716 .mode_fixup = ast_mode_fixup,
717 .prepare = ast_encoder_prepare,
718 .commit = ast_encoder_commit,
719 .mode_set = ast_encoder_mode_set,
720};
721
Rashika7f5ccd42014-01-06 20:02:39 +0530722static int ast_encoder_init(struct drm_device *dev)
Dave Airlie312fec12012-02-29 13:40:04 +0000723{
724 struct ast_encoder *ast_encoder;
725
726 ast_encoder = kzalloc(sizeof(struct ast_encoder), GFP_KERNEL);
727 if (!ast_encoder)
728 return -ENOMEM;
729
730 drm_encoder_init(dev, &ast_encoder->base, &ast_enc_funcs,
731 DRM_MODE_ENCODER_DAC);
732 drm_encoder_helper_add(&ast_encoder->base, &ast_enc_helper_funcs);
733
734 ast_encoder->base.possible_crtcs = 1;
735 return 0;
736}
737
738static int ast_get_modes(struct drm_connector *connector)
739{
740 struct ast_connector *ast_connector = to_ast_connector(connector);
741 struct edid *edid;
742 int ret;
743
744 edid = drm_get_edid(connector, &ast_connector->i2c->adapter);
745 if (edid) {
746 drm_mode_connector_update_edid_property(&ast_connector->base, edid);
747 ret = drm_add_edid_modes(connector, edid);
Jani Nikula993dcb02012-08-15 09:32:37 +0000748 kfree(edid);
Dave Airlie312fec12012-02-29 13:40:04 +0000749 return ret;
750 } else
751 drm_mode_connector_update_edid_property(&ast_connector->base, NULL);
752 return 0;
753}
754
755static int ast_mode_valid(struct drm_connector *connector,
756 struct drm_display_mode *mode)
757{
Dave Airlief1f62f22014-01-17 10:56:09 +1000758 struct ast_private *ast = connector->dev->dev_private;
759 int flags = MODE_NOMODE;
760 uint32_t jtemp;
761
762 if (ast->support_wide_screen) {
763 if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
764 return MODE_OK;
765 if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
766 return MODE_OK;
767 if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
768 return MODE_OK;
769 if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
770 return MODE_OK;
771 if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
772 return MODE_OK;
773
774 if ((ast->chip == AST2100) || (ast->chip == AST2200) || (ast->chip == AST2300) || (ast->chip == AST1180)) {
775 if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
776 return MODE_OK;
777
778 if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
779 jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
780 if (jtemp & 0x01)
781 return MODE_NOMODE;
782 else
783 return MODE_OK;
784 }
785 }
786 }
787 switch (mode->hdisplay) {
788 case 640:
789 if (mode->vdisplay == 480) flags = MODE_OK;
790 break;
791 case 800:
792 if (mode->vdisplay == 600) flags = MODE_OK;
793 break;
794 case 1024:
795 if (mode->vdisplay == 768) flags = MODE_OK;
796 break;
797 case 1280:
798 if (mode->vdisplay == 1024) flags = MODE_OK;
799 break;
800 case 1600:
801 if (mode->vdisplay == 1200) flags = MODE_OK;
802 break;
803 default:
804 return flags;
805 }
806
807 return flags;
Dave Airlie312fec12012-02-29 13:40:04 +0000808}
809
810static void ast_connector_destroy(struct drm_connector *connector)
811{
812 struct ast_connector *ast_connector = to_ast_connector(connector);
813 ast_i2c_destroy(ast_connector->i2c);
814 drm_sysfs_connector_remove(connector);
815 drm_connector_cleanup(connector);
816 kfree(connector);
817}
818
819static enum drm_connector_status
820ast_connector_detect(struct drm_connector *connector, bool force)
821{
822 return connector_status_connected;
823}
824
825static const struct drm_connector_helper_funcs ast_connector_helper_funcs = {
826 .mode_valid = ast_mode_valid,
827 .get_modes = ast_get_modes,
828 .best_encoder = ast_best_single_encoder,
829};
830
831static const struct drm_connector_funcs ast_connector_funcs = {
832 .dpms = drm_helper_connector_dpms,
833 .detect = ast_connector_detect,
834 .fill_modes = drm_helper_probe_single_connector_modes,
835 .destroy = ast_connector_destroy,
836};
837
Rashika7f5ccd42014-01-06 20:02:39 +0530838static int ast_connector_init(struct drm_device *dev)
Dave Airlie312fec12012-02-29 13:40:04 +0000839{
840 struct ast_connector *ast_connector;
841 struct drm_connector *connector;
842 struct drm_encoder *encoder;
843
844 ast_connector = kzalloc(sizeof(struct ast_connector), GFP_KERNEL);
845 if (!ast_connector)
846 return -ENOMEM;
847
848 connector = &ast_connector->base;
849 drm_connector_init(dev, connector, &ast_connector_funcs, DRM_MODE_CONNECTOR_VGA);
850
851 drm_connector_helper_add(connector, &ast_connector_helper_funcs);
852
853 connector->interlace_allowed = 0;
854 connector->doublescan_allowed = 0;
855
856 drm_sysfs_connector_add(connector);
857
858 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
859
860 encoder = list_first_entry(&dev->mode_config.encoder_list, struct drm_encoder, head);
861 drm_mode_connector_attach_encoder(connector, encoder);
862
863 ast_connector->i2c = ast_i2c_create(dev);
864 if (!ast_connector->i2c)
865 DRM_ERROR("failed to add ddc bus for connector\n");
866
867 return 0;
868}
869
870/* allocate cursor cache and pin at start of VRAM */
Rashika7f5ccd42014-01-06 20:02:39 +0530871static int ast_cursor_init(struct drm_device *dev)
Dave Airlie312fec12012-02-29 13:40:04 +0000872{
873 struct ast_private *ast = dev->dev_private;
874 int size;
875 int ret;
876 struct drm_gem_object *obj;
877 struct ast_bo *bo;
878 uint64_t gpu_addr;
879
880 size = (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE) * AST_DEFAULT_HWC_NUM;
881
882 ret = ast_gem_create(dev, size, true, &obj);
883 if (ret)
884 return ret;
885 bo = gem_to_ast_bo(obj);
886 ret = ast_bo_reserve(bo, false);
887 if (unlikely(ret != 0))
888 goto fail;
889
890 ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
891 ast_bo_unreserve(bo);
892 if (ret)
893 goto fail;
894
895 /* kmap the object */
896 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &ast->cache_kmap);
897 if (ret)
898 goto fail;
899
900 ast->cursor_cache = obj;
901 ast->cursor_cache_gpu_addr = gpu_addr;
Dave Airlie0273de02012-09-03 07:22:16 +1000902 DRM_DEBUG_KMS("pinned cursor cache at %llx\n", ast->cursor_cache_gpu_addr);
Dave Airlie312fec12012-02-29 13:40:04 +0000903 return 0;
904fail:
905 return ret;
906}
907
Rashika7f5ccd42014-01-06 20:02:39 +0530908static void ast_cursor_fini(struct drm_device *dev)
Dave Airlie312fec12012-02-29 13:40:04 +0000909{
910 struct ast_private *ast = dev->dev_private;
911 ttm_bo_kunmap(&ast->cache_kmap);
912 drm_gem_object_unreference_unlocked(ast->cursor_cache);
913}
914
915int ast_mode_init(struct drm_device *dev)
916{
917 ast_cursor_init(dev);
918 ast_crtc_init(dev);
919 ast_encoder_init(dev);
920 ast_connector_init(dev);
921 return 0;
922}
923
924void ast_mode_fini(struct drm_device *dev)
925{
926 ast_cursor_fini(dev);
927}
928
929static int get_clock(void *i2c_priv)
930{
931 struct ast_i2c_chan *i2c = i2c_priv;
932 struct ast_private *ast = i2c->dev->dev_private;
933 uint32_t val;
934
935 val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4;
936 return val & 1 ? 1 : 0;
937}
938
939static int get_data(void *i2c_priv)
940{
941 struct ast_i2c_chan *i2c = i2c_priv;
942 struct ast_private *ast = i2c->dev->dev_private;
943 uint32_t val;
944
945 val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5;
946 return val & 1 ? 1 : 0;
947}
948
949static void set_clock(void *i2c_priv, int clock)
950{
951 struct ast_i2c_chan *i2c = i2c_priv;
952 struct ast_private *ast = i2c->dev->dev_private;
953 int i;
954 u8 ujcrb7, jtemp;
955
956 for (i = 0; i < 0x10000; i++) {
957 ujcrb7 = ((clock & 0x01) ? 0 : 1);
958 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfe, ujcrb7);
959 jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
960 if (ujcrb7 == jtemp)
961 break;
962 }
963}
964
965static void set_data(void *i2c_priv, int data)
966{
967 struct ast_i2c_chan *i2c = i2c_priv;
968 struct ast_private *ast = i2c->dev->dev_private;
969 int i;
970 u8 ujcrb7, jtemp;
971
972 for (i = 0; i < 0x10000; i++) {
973 ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
974 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfb, ujcrb7);
975 jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
976 if (ujcrb7 == jtemp)
977 break;
978 }
979}
980
981static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
982{
983 struct ast_i2c_chan *i2c;
984 int ret;
985
986 i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
987 if (!i2c)
988 return NULL;
989
990 i2c->adapter.owner = THIS_MODULE;
991 i2c->adapter.class = I2C_CLASS_DDC;
992 i2c->adapter.dev.parent = &dev->pdev->dev;
993 i2c->dev = dev;
994 i2c_set_adapdata(&i2c->adapter, i2c);
995 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
996 "AST i2c bit bus");
997 i2c->adapter.algo_data = &i2c->bit;
998
999 i2c->bit.udelay = 20;
1000 i2c->bit.timeout = 2;
1001 i2c->bit.data = i2c;
1002 i2c->bit.setsda = set_data;
1003 i2c->bit.setscl = set_clock;
1004 i2c->bit.getsda = get_data;
1005 i2c->bit.getscl = get_clock;
1006 ret = i2c_bit_add_bus(&i2c->adapter);
1007 if (ret) {
1008 DRM_ERROR("Failed to register bit i2c\n");
1009 goto out_free;
1010 }
1011
1012 return i2c;
1013out_free:
1014 kfree(i2c);
1015 return NULL;
1016}
1017
1018static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
1019{
1020 if (!i2c)
1021 return;
1022 i2c_del_adapter(&i2c->adapter);
1023 kfree(i2c);
1024}
1025
Rashika7f5ccd42014-01-06 20:02:39 +05301026static void ast_show_cursor(struct drm_crtc *crtc)
Dave Airlie312fec12012-02-29 13:40:04 +00001027{
1028 struct ast_private *ast = crtc->dev->dev_private;
1029 u8 jreg;
1030
1031 jreg = 0x2;
1032 /* enable ARGB cursor */
1033 jreg |= 1;
1034 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
1035}
1036
Rashika7f5ccd42014-01-06 20:02:39 +05301037static void ast_hide_cursor(struct drm_crtc *crtc)
Dave Airlie312fec12012-02-29 13:40:04 +00001038{
1039 struct ast_private *ast = crtc->dev->dev_private;
1040 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
1041}
1042
1043static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
1044{
1045 union {
1046 u32 ul;
1047 u8 b[4];
1048 } srcdata32[2], data32;
1049 union {
1050 u16 us;
1051 u8 b[2];
1052 } data16;
1053 u32 csum = 0;
1054 s32 alpha_dst_delta, last_alpha_dst_delta;
1055 u8 *srcxor, *dstxor;
1056 int i, j;
1057 u32 per_pixel_copy, two_pixel_copy;
1058
1059 alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
1060 last_alpha_dst_delta = alpha_dst_delta - (width << 1);
1061
1062 srcxor = src;
1063 dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
1064 per_pixel_copy = width & 1;
1065 two_pixel_copy = width >> 1;
1066
1067 for (j = 0; j < height; j++) {
1068 for (i = 0; i < two_pixel_copy; i++) {
1069 srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
1070 srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
1071 data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
1072 data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
1073 data32.b[2] = srcdata32[0].b[1] | (srcdata32[1].b[0] >> 4);
1074 data32.b[3] = srcdata32[0].b[3] | (srcdata32[1].b[2] >> 4);
1075
1076 writel(data32.ul, dstxor);
1077 csum += data32.ul;
1078
1079 dstxor += 4;
1080 srcxor += 8;
1081
1082 }
1083
1084 for (i = 0; i < per_pixel_copy; i++) {
1085 srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
1086 data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
1087 data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
1088 writew(data16.us, dstxor);
1089 csum += (u32)data16.us;
1090
1091 dstxor += 2;
1092 srcxor += 4;
1093 }
1094 dstxor += last_alpha_dst_delta;
1095 }
1096 return csum;
1097}
1098
1099static int ast_cursor_set(struct drm_crtc *crtc,
1100 struct drm_file *file_priv,
1101 uint32_t handle,
1102 uint32_t width,
1103 uint32_t height)
1104{
1105 struct ast_private *ast = crtc->dev->dev_private;
1106 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
1107 struct drm_gem_object *obj;
1108 struct ast_bo *bo;
1109 uint64_t gpu_addr;
1110 u32 csum;
1111 int ret;
1112 struct ttm_bo_kmap_obj uobj_map;
1113 u8 *src, *dst;
1114 bool src_isiomem, dst_isiomem;
1115 if (!handle) {
1116 ast_hide_cursor(crtc);
1117 return 0;
1118 }
1119
1120 if (width > AST_MAX_HWC_WIDTH || height > AST_MAX_HWC_HEIGHT)
1121 return -EINVAL;
1122
1123 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
1124 if (!obj) {
1125 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
1126 return -ENOENT;
1127 }
1128 bo = gem_to_ast_bo(obj);
1129
1130 ret = ast_bo_reserve(bo, false);
1131 if (ret)
1132 goto fail;
1133
1134 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
1135
1136 src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
1137 dst = ttm_kmap_obj_virtual(&ast->cache_kmap, &dst_isiomem);
1138
1139 if (src_isiomem == true)
1140 DRM_ERROR("src cursor bo should be in main memory\n");
1141 if (dst_isiomem == false)
1142 DRM_ERROR("dst bo should be in VRAM\n");
1143
1144 dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
1145
1146 /* do data transfer to cursor cache */
1147 csum = copy_cursor_image(src, dst, width, height);
1148
1149 /* write checksum + signature */
1150 ttm_bo_kunmap(&uobj_map);
1151 ast_bo_unreserve(bo);
1152 {
1153 u8 *dst = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
1154 writel(csum, dst);
1155 writel(width, dst + AST_HWC_SIGNATURE_SizeX);
1156 writel(height, dst + AST_HWC_SIGNATURE_SizeY);
1157 writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
1158 writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
1159
1160 /* set pattern offset */
1161 gpu_addr = ast->cursor_cache_gpu_addr;
1162 gpu_addr += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
1163 gpu_addr >>= 3;
1164 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, gpu_addr & 0xff);
1165 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, (gpu_addr >> 8) & 0xff);
1166 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, (gpu_addr >> 16) & 0xff);
1167 }
1168 ast_crtc->cursor_width = width;
1169 ast_crtc->cursor_height = height;
1170 ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
1171 ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
1172
1173 ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
1174
1175 ast_show_cursor(crtc);
1176
1177 drm_gem_object_unreference_unlocked(obj);
1178 return 0;
1179fail:
1180 drm_gem_object_unreference_unlocked(obj);
1181 return ret;
1182}
1183
1184static int ast_cursor_move(struct drm_crtc *crtc,
1185 int x, int y)
1186{
1187 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
1188 struct ast_private *ast = crtc->dev->dev_private;
1189 int x_offset, y_offset;
1190 u8 *sig;
1191
1192 sig = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
1193 writel(x, sig + AST_HWC_SIGNATURE_X);
1194 writel(y, sig + AST_HWC_SIGNATURE_Y);
1195
1196 x_offset = ast_crtc->offset_x;
1197 y_offset = ast_crtc->offset_y;
1198 if (x < 0) {
1199 x_offset = (-x) + ast_crtc->offset_x;
1200 x = 0;
1201 }
1202
1203 if (y < 0) {
1204 y_offset = (-y) + ast_crtc->offset_y;
1205 y = 0;
1206 }
1207 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
1208 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
1209 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, (x & 0xff));
1210 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, ((x >> 8) & 0x0f));
1211 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, (y & 0xff));
1212 ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, ((y >> 8) & 0x07));
1213
1214 /* dummy write to fire HWC */
1215 ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xCB, 0xFF, 0x00);
1216
1217 return 0;
1218}