blob: faf95da8fcbc074ae7878944f66c2973436ea815 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ATI Mach64 CT/VT/GT/LT Cursor Support
3 */
4
5#include <linux/slab.h>
6#include <linux/fb.h>
7#include <linux/init.h>
8#include <linux/string.h>
9
10#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#ifdef __sparc__
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/fbio.h>
14#endif
15
16#include <video/mach64.h>
17#include "atyfb.h"
18
19/*
20 * The hardware cursor definition requires 2 bits per pixel. The
21 * Cursor size reguardless of the visible cursor size is 64 pixels
22 * by 64 lines. The total memory required to define the cursor is
23 * 16 bytes / line for 64 lines or 1024 bytes of data. The data
24 * must be in a contigiuos format. The 2 bit cursor code values are
25 * as follows:
26 *
27 * 00 - pixel colour = CURSOR_CLR_0
28 * 01 - pixel colour = CURSOR_CLR_1
29 * 10 - pixel colour = transparent (current display pixel)
30 * 11 - pixel colour = 1's complement of current display pixel
31 *
32 * Cursor Offset 64 pixels Actual Displayed Area
33 * \_________________________/
34 * | | | |
35 * |<--------------->| | |
36 * | CURS_HORZ_OFFSET| | |
37 * | |_______| | 64 Lines
38 * | ^ | |
39 * | | | |
40 * | CURS_VERT_OFFSET| |
41 * | | | |
42 * |____________________|____| |
43 *
44 *
45 * The Screen position of the top left corner of the displayed
46 * cursor is specificed by CURS_HORZ_VERT_POSN. Care must be taken
47 * when the cursor hot spot is not the top left corner and the
48 * physical cursor position becomes negative. It will be be displayed
49 * if either the horizontal or vertical cursor position is negative
50 *
51 * If x becomes negative the cursor manager must adjust the CURS_HORZ_OFFSET
52 * to a larger number and saturate CUR_HORZ_POSN to zero.
53 *
54 * if Y becomes negative, CUR_VERT_OFFSET must be adjusted to a larger number,
55 * CUR_OFFSET must be adjusted to a point to the appropraite line in the cursor
56 * definitation and CUR_VERT_POSN must be saturated to zero.
57 */
58
59 /*
60 * Hardware Cursor support.
61 */
62static const u8 cursor_bits_lookup[16] = {
63 0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
64 0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
65};
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int atyfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
68{
69 struct atyfb_par *par = (struct atyfb_par *) info->par;
70 u16 xoff, yoff;
71 int x, y, h;
72
73#ifdef __sparc__
74 if (par->mmaped)
75 return -EPERM;
76#endif
77 if (par->asleep)
78 return -EPERM;
79
80 /* Hide cursor */
81 wait_for_fifo(1, par);
82 aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par) & ~HWCURSOR_ENABLE, par);
83
84 /* set position */
85 if (cursor->set & FB_CUR_SETPOS) {
86 x = cursor->image.dx - cursor->hot.x - info->var.xoffset;
87 if (x < 0) {
88 xoff = -x;
89 x = 0;
90 } else {
91 xoff = 0;
92 }
93
94 y = cursor->image.dy - cursor->hot.y - info->var.yoffset;
95 if (y < 0) {
96 yoff = -y;
97 y = 0;
98 } else {
99 yoff = 0;
100 }
101
102 h = cursor->image.height;
103
104 /*
105 * In doublescan mode, the cursor location
106 * and heigh also needs to be doubled.
107 */
108 if (par->crtc.gen_cntl & CRTC_DBL_SCAN_EN) {
109 y<<=1;
110 h<<=1;
111 }
112 wait_for_fifo(4, par);
113 aty_st_le32(CUR_OFFSET, (info->fix.smem_len >> 3) + (yoff << 1), par);
114 aty_st_le32(CUR_HORZ_VERT_OFF,
115 ((u32) (64 - h + yoff) << 16) | xoff, par);
116 aty_st_le32(CUR_HORZ_VERT_POSN, ((u32) y << 16) | x, par);
117 }
118
119 /* Set color map */
120 if (cursor->set & FB_CUR_SETCMAP) {
121 u32 fg_idx, bg_idx, fg, bg;
122
123 fg_idx = cursor->image.fg_color;
124 bg_idx = cursor->image.bg_color;
125
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700126 fg = ((info->cmap.red[fg_idx] & 0xff) << 24) |
127 ((info->cmap.green[fg_idx] & 0xff) << 16) |
128 ((info->cmap.blue[fg_idx] & 0xff) << 8) | 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700130 bg = ((info->cmap.red[bg_idx] & 0xff) << 24) |
131 ((info->cmap.green[bg_idx] & 0xff) << 16) |
132 ((info->cmap.blue[bg_idx] & 0xff) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 wait_for_fifo(2, par);
135 aty_st_le32(CUR_CLR0, bg, par);
136 aty_st_le32(CUR_CLR1, fg, par);
137 }
138
139 if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
140 u8 *src = (u8 *)cursor->image.data;
141 u8 *msk = (u8 *)cursor->mask;
142 u8 __iomem *dst = (u8 __iomem *)info->sprite.addr;
143 unsigned int width = (cursor->image.width + 7) >> 3;
144 unsigned int height = cursor->image.height;
145 unsigned int align = info->sprite.scan_align;
146
147 unsigned int i, j, offset;
148 u8 m, b;
149
150 // Clear cursor image with 1010101010...
151 fb_memset(dst, 0xaa, 1024);
152
153 offset = align - width*2;
154
155 for (i = 0; i < height; i++) {
156 for (j = 0; j < width; j++) {
157 b = *src++;
158 m = *msk++;
159 switch (cursor->rop) {
160 case ROP_XOR:
161 // Upper 4 bits of mask data
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700162 fb_writeb(cursor_bits_lookup[(b ^ m) >> 4], dst++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 // Lower 4 bits of mask
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700164 fb_writeb(cursor_bits_lookup[(b ^ m) & 0x0f],
165 dst++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167 case ROP_COPY:
168 // Upper 4 bits of mask data
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700169 fb_writeb(cursor_bits_lookup[(b & m) >> 4], dst++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 // Lower 4 bits of mask
Antonino A. Daplas72c24cc2006-06-26 00:26:31 -0700171 fb_writeb(cursor_bits_lookup[(b & m) & 0x0f],
172 dst++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 }
175 }
176 dst += offset;
177 }
178 }
179
180 if (cursor->enable) {
181 wait_for_fifo(1, par);
182 aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, par)
183 | HWCURSOR_ENABLE, par);
184 }
185 return 0;
186}
187
Antonino A. Daplas1a8c9792006-06-26 00:26:58 -0700188int __devinit aty_init_cursor(struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 unsigned long addr;
191
192 info->fix.smem_len -= PAGE_SIZE;
193
194#ifdef __sparc__
195 addr = (unsigned long) info->screen_base - 0x800000 + info->fix.smem_len;
196 info->sprite.addr = (u8 *) addr;
197#else
198#ifdef __BIG_ENDIAN
199 addr = info->fix.smem_start - 0x800000 + info->fix.smem_len;
200 info->sprite.addr = (u8 *) ioremap(addr, 1024);
201#else
202 addr = (unsigned long) info->screen_base + info->fix.smem_len;
203 info->sprite.addr = (u8 *) addr;
204#endif
205#endif
206 if (!info->sprite.addr)
207 return -ENXIO;
208 info->sprite.size = PAGE_SIZE;
209 info->sprite.scan_align = 16; /* Scratch pad 64 bytes wide */
210 info->sprite.buf_align = 16; /* and 64 lines tall. */
211 info->sprite.flags = FB_PIXMAP_IO;
212
213 info->fbops->fb_cursor = atyfb_cursor;
214
215 return 0;
216}
217