GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / sm750fb / sm750_cursor.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/errno.h>
5 #include <linux/string.h>
6 #include <linux/mm.h>
7 #include <linux/slab.h>
8 #include <linux/delay.h>
9 #include <linux/fb.h>
10 #include <linux/ioport.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/vmalloc.h>
14 #include <linux/pagemap.h>
15 #include <linux/console.h>
16 #include <linux/platform_device.h>
17 #include <linux/screen_info.h>
18
19 #include "sm750.h"
20 #include "sm750_cursor.h"
21
22
23
24 #define poke32(addr, data) \
25 writel((data), cursor->mmio + (addr))
26
27 /* cursor control for voyager and 718/750*/
28 #define HWC_ADDRESS                         0x0
29 #define HWC_ADDRESS_ENABLE                  BIT(31)
30 #define HWC_ADDRESS_EXT                     BIT(27)
31 #define HWC_ADDRESS_CS                      BIT(26)
32 #define HWC_ADDRESS_ADDRESS_MASK            0x3ffffff
33
34 #define HWC_LOCATION                        0x4
35 #define HWC_LOCATION_TOP                    BIT(27)
36 #define HWC_LOCATION_Y_SHIFT                16
37 #define HWC_LOCATION_Y_MASK                 (0x7ff << 16)
38 #define HWC_LOCATION_LEFT                   BIT(11)
39 #define HWC_LOCATION_X_MASK                 0x7ff
40
41 #define HWC_COLOR_12                        0x8
42 #define HWC_COLOR_12_2_RGB565_SHIFT         16
43 #define HWC_COLOR_12_2_RGB565_MASK          (0xffff << 16)
44 #define HWC_COLOR_12_1_RGB565_MASK          0xffff
45
46 #define HWC_COLOR_3                         0xC
47 #define HWC_COLOR_3_RGB565_MASK             0xffff
48
49
50 /* hw_cursor_xxx works for voyager,718 and 750 */
51 void sm750_hw_cursor_enable(struct lynx_cursor *cursor)
52 {
53         u32 reg;
54
55         reg = (cursor->offset & HWC_ADDRESS_ADDRESS_MASK) | HWC_ADDRESS_ENABLE;
56         poke32(HWC_ADDRESS, reg);
57 }
58
59 void sm750_hw_cursor_disable(struct lynx_cursor *cursor)
60 {
61         poke32(HWC_ADDRESS, 0);
62 }
63
64 void sm750_hw_cursor_setSize(struct lynx_cursor *cursor, int w, int h)
65 {
66         cursor->w = w;
67         cursor->h = h;
68 }
69
70 void sm750_hw_cursor_setPos(struct lynx_cursor *cursor, int x, int y)
71 {
72         u32 reg;
73
74         reg = ((y << HWC_LOCATION_Y_SHIFT) & HWC_LOCATION_Y_MASK) |
75                (x & HWC_LOCATION_X_MASK);
76         poke32(HWC_LOCATION, reg);
77 }
78
79 void sm750_hw_cursor_setColor(struct lynx_cursor *cursor, u32 fg, u32 bg)
80 {
81         u32 reg = (fg << HWC_COLOR_12_2_RGB565_SHIFT) &
82                 HWC_COLOR_12_2_RGB565_MASK;
83
84         poke32(HWC_COLOR_12, reg | (bg & HWC_COLOR_12_1_RGB565_MASK));
85         poke32(HWC_COLOR_3, 0xffe0);
86 }
87
88 void sm750_hw_cursor_setData(struct lynx_cursor *cursor, u16 rop,
89                              const u8 *pcol, const u8 *pmsk)
90 {
91         int i, j, count, pitch, offset;
92         u8 color, mask, opr;
93         u16 data;
94         void __iomem *pbuffer, *pstart;
95
96         /*  in byte*/
97         pitch = cursor->w >> 3;
98
99         /* in byte      */
100         count = pitch * cursor->h;
101
102         /* in byte */
103         offset = cursor->maxW * 2 / 8;
104
105         data = 0;
106         pstart = cursor->vstart;
107         pbuffer = pstart;
108
109         for (i = 0; i < count; i++) {
110                 color = *pcol++;
111                 mask = *pmsk++;
112                 data = 0;
113
114                 for (j = 0; j < 8; j++) {
115                         if (mask & (0x80 >> j)) {
116                                 if (rop == ROP_XOR)
117                                         opr = mask ^ color;
118                                 else
119                                         opr = mask & color;
120
121                                 /* 2 stands for forecolor and 1 for backcolor */
122                                 data |= ((opr & (0x80 >> j)) ? 2 : 1) << (j * 2);
123                         }
124                 }
125                 iowrite16(data, pbuffer);
126
127                 /* assume pitch is 1,2,4,8,...*/
128                 if ((i + 1) % pitch == 0) {
129                         /* need a return */
130                         pstart += offset;
131                         pbuffer = pstart;
132                 } else {
133                         pbuffer += sizeof(u16);
134                 }
135         }
136 }
137
138
139 void sm750_hw_cursor_setData2(struct lynx_cursor *cursor, u16 rop,
140                               const u8 *pcol, const u8 *pmsk)
141 {
142         int i, j, count, pitch, offset;
143         u8 color, mask;
144         u16 data;
145         void __iomem *pbuffer, *pstart;
146
147         /*  in byte*/
148         pitch = cursor->w >> 3;
149
150         /* in byte      */
151         count = pitch * cursor->h;
152
153         /* in byte */
154         offset = cursor->maxW * 2 / 8;
155
156         data = 0;
157         pstart = cursor->vstart;
158         pbuffer = pstart;
159
160         for (i = 0; i < count; i++) {
161                 color = *pcol++;
162                 mask = *pmsk++;
163                 data = 0;
164
165                 for (j = 0; j < 8; j++) {
166                         if (mask & (1 << j))
167                                 data |= ((color & (1 << j)) ? 1 : 2) << (j * 2);
168                 }
169                 iowrite16(data, pbuffer);
170
171                 /* assume pitch is 1,2,4,8,...*/
172                 if (!(i & (pitch - 1))) {
173                         /* need a return */
174                         pstart += offset;
175                         pbuffer = pstart;
176                 } else {
177                         pbuffer += sizeof(u16);
178                 }
179         }
180 }