1 /*
2  * Copyright 2010 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #ifndef __NOUVEAU_HWSQ_H__
26 #define __NOUVEAU_HWSQ_H__
27 
28 struct hwsq_ucode {
29 	u8 data[0x200];
30 	union {
31 		u8  *u08;
32 		u16 *u16;
33 		u32 *u32;
34 	} ptr;
35 	u16 len;
36 
37 	u32 reg;
38 	u32 val;
39 };
40 
41 static inline void
hwsq_init(struct hwsq_ucode * hwsq)42 hwsq_init(struct hwsq_ucode *hwsq)
43 {
44 	hwsq->ptr.u08 = hwsq->data;
45 	hwsq->reg = 0xffffffff;
46 	hwsq->val = 0xffffffff;
47 }
48 
49 static inline void
hwsq_fini(struct hwsq_ucode * hwsq)50 hwsq_fini(struct hwsq_ucode *hwsq)
51 {
52 	do {
53 		*hwsq->ptr.u08++ = 0x7f;
54 		hwsq->len = hwsq->ptr.u08 - hwsq->data;
55 	} while (hwsq->len & 3);
56 	hwsq->ptr.u08 = hwsq->data;
57 }
58 
59 static inline void
hwsq_usec(struct hwsq_ucode * hwsq,u8 usec)60 hwsq_usec(struct hwsq_ucode *hwsq, u8 usec)
61 {
62 	u32 shift = 0;
63 	while (usec & ~3) {
64 		usec >>= 2;
65 		shift++;
66 	}
67 
68 	*hwsq->ptr.u08++ = (shift << 2) | usec;
69 }
70 
71 static inline void
hwsq_setf(struct hwsq_ucode * hwsq,u8 flag,int val)72 hwsq_setf(struct hwsq_ucode *hwsq, u8 flag, int val)
73 {
74 	flag += 0x80;
75 	if (val >= 0)
76 		flag += 0x20;
77 	if (val >= 1)
78 		flag += 0x20;
79 	*hwsq->ptr.u08++ = flag;
80 }
81 
82 static inline void
hwsq_op5f(struct hwsq_ucode * hwsq,u8 v0,u8 v1)83 hwsq_op5f(struct hwsq_ucode *hwsq, u8 v0, u8 v1)
84 {
85 	*hwsq->ptr.u08++ = 0x5f;
86 	*hwsq->ptr.u08++ = v0;
87 	*hwsq->ptr.u08++ = v1;
88 }
89 
90 static inline void
hwsq_wr32(struct hwsq_ucode * hwsq,u32 reg,u32 val)91 hwsq_wr32(struct hwsq_ucode *hwsq, u32 reg, u32 val)
92 {
93 	if (val != hwsq->val) {
94 		if ((val & 0xffff0000) == (hwsq->val & 0xffff0000)) {
95 			*hwsq->ptr.u08++ = 0x42;
96 			*hwsq->ptr.u16++ = (val & 0x0000ffff);
97 		} else {
98 			*hwsq->ptr.u08++ = 0xe2;
99 			*hwsq->ptr.u32++ = val;
100 		}
101 
102 		hwsq->val = val;
103 	}
104 
105 	if ((reg & 0xffff0000) == (hwsq->reg & 0xffff0000)) {
106 		*hwsq->ptr.u08++ = 0x40;
107 		*hwsq->ptr.u16++ = (reg & 0x0000ffff);
108 	} else {
109 		*hwsq->ptr.u08++ = 0xe0;
110 		*hwsq->ptr.u32++ = reg;
111 	}
112 	hwsq->reg = reg;
113 }
114 
115 #endif
116