mach64_cursor.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:6k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  ATI Mach64 CT/VT/GT/LT Cursor Support
  3.  */
  4. #include <linux/slab.h>
  5. #include <linux/console.h>
  6. #include <linux/fb.h>
  7. #include <linux/init.h>
  8. #include <asm/io.h>
  9. #include <asm/uaccess.h>
  10. #include <video/fbcon.h>
  11. #ifdef __sparc__
  12. #include <asm/pbm.h>
  13. #include <asm/fbio.h>
  14. #endif
  15. #include "mach64.h"
  16. #include "atyfb.h"
  17. #define DEFAULT_CURSOR_BLINK_RATE (20)
  18. #define CURSOR_DRAW_DELAY (2)
  19.     /*
  20.      *  Hardware Cursor support.
  21.      */
  22. static const u8 cursor_pixel_map[2] = { 0, 15 };
  23. static const u8 cursor_color_map[2] = { 0, 0xff };
  24. static const u8 cursor_bits_lookup[16] =
  25. {
  26. 0x00, 0x40, 0x10, 0x50, 0x04, 0x44, 0x14, 0x54,
  27. 0x01, 0x41, 0x11, 0x51, 0x05, 0x45, 0x15, 0x55
  28. };
  29. static const u8 cursor_mask_lookup[16] =
  30. {
  31. 0xaa, 0x2a, 0x8a, 0x0a, 0xa2, 0x22, 0x82, 0x02,
  32. 0xa8, 0x28, 0x88, 0x08, 0xa0, 0x20, 0x80, 0x00
  33. };
  34. void aty_set_cursor_color(struct fb_info_aty *fb)
  35. {
  36. struct aty_cursor *c = fb->cursor;
  37. const u8 *pixel = cursor_pixel_map; /* ++Geert: Why?? */
  38. const u8 *red = cursor_color_map;
  39. const u8 *green = cursor_color_map;
  40. const u8 *blue = cursor_color_map;
  41. int i;
  42. if (!c)
  43. return;
  44. #ifdef __sparc__
  45. if (fb->mmaped && (!fb->fb_info.display_fg
  46.     || fb->fb_info.display_fg->vc_num == fb->vtconsole))
  47. return;
  48. #endif
  49. for (i = 0; i < 2; i++) {
  50. c->color[i] =  (u32)red[i] << 24;
  51. c->color[i] |= (u32)green[i] << 16;
  52.   c->color[i] |= (u32)blue[i] <<  8;
  53.   c->color[i] |= (u32)pixel[i];
  54. }
  55. wait_for_fifo(2, fb);
  56. aty_st_le32(CUR_CLR0, c->color[0], fb);
  57. aty_st_le32(CUR_CLR1, c->color[1], fb);
  58. }
  59. void aty_set_cursor_shape(struct fb_info_aty *fb)
  60. {
  61. struct aty_cursor *c = fb->cursor;
  62. u8 *ram, m, b;
  63. int x, y;
  64. if (!c)
  65. return;
  66. #ifdef __sparc__
  67. if (fb->mmaped && (!fb->fb_info.display_fg
  68.     || fb->fb_info.display_fg->vc_num == fb->vtconsole))
  69. return;
  70. #endif
  71. ram = c->ram;
  72. for (y = 0; y < c->size.y; y++) {
  73. for (x = 0; x < c->size.x >> 2; x++) {
  74. m = c->mask[x][y];
  75. b = c->bits[x][y];
  76. fb_writeb (cursor_mask_lookup[m >> 4] |
  77.    cursor_bits_lookup[(b & m) >> 4],
  78.    ram++);
  79. fb_writeb (cursor_mask_lookup[m & 0x0f] |
  80.    cursor_bits_lookup[(b & m) & 0x0f],
  81.    ram++);
  82. }
  83. for ( ; x < 8; x++) {
  84. fb_writeb (0xaa, ram++);
  85. fb_writeb (0xaa, ram++);
  86. }
  87. }
  88. fb_memset (ram, 0xaa, (64 - c->size.y) * 16);
  89. }
  90. static void
  91. aty_set_cursor(struct fb_info_aty *fb, int on)
  92. {
  93. struct atyfb_par *par = &fb->current_par;
  94. struct aty_cursor *c = fb->cursor;
  95. u16 xoff, yoff;
  96. int x, y;
  97. if (!c)
  98. return;
  99. #ifdef __sparc__
  100. if (fb->mmaped && (!fb->fb_info.display_fg
  101.     || fb->fb_info.display_fg->vc_num == fb->vtconsole))
  102. return;
  103. #endif
  104. if (on) {
  105. x = c->pos.x - c->hot.x - par->crtc.xoffset;
  106. if (x < 0) {
  107. xoff = -x;
  108. x = 0;
  109. } else {
  110. xoff = 0;
  111. }
  112. y = c->pos.y - c->hot.y - par->crtc.yoffset;
  113. if (y < 0) {
  114. yoff = -y;
  115. y = 0;
  116. } else {
  117. yoff = 0;
  118. }
  119. wait_for_fifo(4, fb);
  120. aty_st_le32(CUR_OFFSET, (c->offset >> 3) + (yoff << 1), fb);
  121. aty_st_le32(CUR_HORZ_VERT_OFF,
  122.     ((u32)(64 - c->size.y + yoff) << 16) | xoff, fb);
  123. aty_st_le32(CUR_HORZ_VERT_POSN, ((u32)y << 16) | x, fb);
  124. aty_st_le32(GEN_TEST_CNTL, aty_ld_le32(GEN_TEST_CNTL, fb)
  125.        | HWCURSOR_ENABLE, fb);
  126. } else {
  127. wait_for_fifo(1, fb);
  128. aty_st_le32(GEN_TEST_CNTL,
  129.     aty_ld_le32(GEN_TEST_CNTL, fb) & ~HWCURSOR_ENABLE,
  130.     fb);
  131. }
  132. if (fb->blitter_may_be_busy)
  133. wait_for_idle(fb);
  134. }
  135. static void
  136. aty_cursor_timer_handler(unsigned long dev_addr)
  137. {
  138. struct fb_info_aty *fb = (struct fb_info_aty *)dev_addr;
  139. if (!fb->cursor)
  140. return;
  141. if (!fb->cursor->enable)
  142. goto out;
  143. if (fb->cursor->vbl_cnt && --fb->cursor->vbl_cnt == 0) {
  144. fb->cursor->on ^= 1;
  145. aty_set_cursor(fb, fb->cursor->on);
  146. fb->cursor->vbl_cnt = fb->cursor->blink_rate;
  147. }
  148. out:
  149. fb->cursor->timer->expires = jiffies + (HZ / 50);
  150. add_timer(fb->cursor->timer);
  151. }
  152. void atyfb_cursor(struct display *p, int mode, int x, int y)
  153. {
  154. struct fb_info_aty *fb = (struct fb_info_aty *)p->fb_info;
  155. struct aty_cursor *c = fb->cursor;
  156. if (!c)
  157. return;
  158. #ifdef __sparc__
  159. if (fb->mmaped && (!fb->fb_info.display_fg
  160.     || fb->fb_info.display_fg->vc_num == fb->vtconsole))
  161. return;
  162. #endif
  163. x *= fontwidth(p);
  164. y *= fontheight(p);
  165. if (c->pos.x == x && c->pos.y == y && (mode == CM_ERASE) == !c->enable)
  166. return;
  167. c->enable = 0;
  168. if (c->on)
  169. aty_set_cursor(fb, 0);
  170. c->pos.x = x;
  171. c->pos.y = y;
  172. switch (mode) {
  173. case CM_ERASE:
  174. c->on = 0;
  175. break;
  176. case CM_DRAW:
  177. case CM_MOVE:
  178. if (c->on)
  179. aty_set_cursor(fb, 1);
  180. else
  181. c->vbl_cnt = CURSOR_DRAW_DELAY;
  182. c->enable = 1;
  183. break;
  184. }
  185. }
  186. struct aty_cursor * __init aty_init_cursor(struct fb_info_aty *fb)
  187. {
  188. struct aty_cursor *cursor;
  189. unsigned long addr;
  190. cursor = kmalloc(sizeof(struct aty_cursor), GFP_ATOMIC);
  191. if (!cursor)
  192. return 0;
  193. memset(cursor, 0, sizeof(*cursor));
  194. cursor->timer = kmalloc(sizeof(*cursor->timer), GFP_KERNEL);
  195. if (!cursor->timer) {
  196. kfree(cursor);
  197. return 0;
  198. }
  199. memset(cursor->timer, 0, sizeof(*cursor->timer));
  200. cursor->blink_rate = DEFAULT_CURSOR_BLINK_RATE;
  201. fb->total_vram -= PAGE_SIZE;
  202. cursor->offset = fb->total_vram;
  203. #ifdef __sparc__
  204. addr = fb->frame_buffer - 0x800000 + cursor->offset;
  205. cursor->ram = (u8 *)addr;
  206. #else
  207. #ifdef __BIG_ENDIAN
  208. addr = fb->frame_buffer_phys - 0x800000 + cursor->offset;
  209. cursor->ram = (u8 *)ioremap(addr, 1024);
  210. #else
  211. addr = fb->frame_buffer + cursor->offset;
  212. cursor->ram = (u8 *)addr;
  213. #endif
  214. #endif
  215. if (!cursor->ram) {
  216. kfree(cursor);
  217. return NULL;
  218. }
  219. init_timer(cursor->timer);
  220. cursor->timer->expires = jiffies + (HZ / 50);
  221. cursor->timer->data = (unsigned long)fb;
  222. cursor->timer->function = aty_cursor_timer_handler;
  223. add_timer(cursor->timer);
  224. return cursor;
  225. }
  226. int atyfb_set_font(struct display *d, int width, int height)
  227. {
  228.     struct fb_info_aty *fb = (struct fb_info_aty *)d->fb_info;
  229.     struct aty_cursor *c = fb->cursor;
  230.     int i, j;
  231.     if (c) {
  232. if (!width || !height) {
  233.     width = 8;
  234.     height = 16;
  235. }
  236. c->hot.x = 0;
  237. c->hot.y = 0;
  238. c->size.x = width;
  239. c->size.y = height;
  240. memset(c->bits, 0xff, sizeof(c->bits));
  241. memset(c->mask, 0, sizeof(c->mask));
  242. for (i = 0, j = width; j >= 0; j -= 8, i++) {
  243.     c->mask[i][height-2] = (j >= 8) ? 0xff : (0xff << (8 - j));
  244.     c->mask[i][height-1] = (j >= 8) ? 0xff : (0xff << (8 - j));
  245. }
  246. aty_set_cursor_color(fb);
  247. aty_set_cursor_shape(fb);
  248.     }
  249.     return 1;
  250. }