mach64_cursor.c 5.7 KB

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