mach64_cursor.c 6.0 KB

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