1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev * Copyright 2017 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev *
4b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev *
11b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev *
14b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev *
22b843c749SSergey Zigachev */
23b843c749SSergey Zigachev /*
24b843c749SSergey Zigachev * dc_helper.c
25b843c749SSergey Zigachev *
26b843c749SSergey Zigachev * Created on: Aug 30, 2016
27b843c749SSergey Zigachev * Author: agrodzov
28b843c749SSergey Zigachev */
29b843c749SSergey Zigachev #include "dm_services.h"
30b843c749SSergey Zigachev #include <stdarg.h>
31b843c749SSergey Zigachev
generic_reg_update_ex(const struct dc_context * ctx,uint32_t addr,uint32_t reg_val,int n,uint8_t shift1,uint32_t mask1,uint32_t field_value1,...)32b843c749SSergey Zigachev uint32_t generic_reg_update_ex(const struct dc_context *ctx,
33b843c749SSergey Zigachev uint32_t addr, uint32_t reg_val, int n,
34b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t field_value1,
35b843c749SSergey Zigachev ...)
36b843c749SSergey Zigachev {
37b843c749SSergey Zigachev uint32_t shift, mask, field_value;
38b843c749SSergey Zigachev int i = 1;
39b843c749SSergey Zigachev
40b843c749SSergey Zigachev va_list ap;
41b843c749SSergey Zigachev va_start(ap, field_value1);
42b843c749SSergey Zigachev
43b843c749SSergey Zigachev reg_val = set_reg_field_value_ex(reg_val, field_value1, mask1, shift1);
44b843c749SSergey Zigachev
45b843c749SSergey Zigachev while (i < n) {
46b843c749SSergey Zigachev shift = va_arg(ap, uint32_t);
47b843c749SSergey Zigachev mask = va_arg(ap, uint32_t);
48b843c749SSergey Zigachev field_value = va_arg(ap, uint32_t);
49b843c749SSergey Zigachev
50b843c749SSergey Zigachev reg_val = set_reg_field_value_ex(reg_val, field_value, mask, shift);
51b843c749SSergey Zigachev i++;
52b843c749SSergey Zigachev }
53b843c749SSergey Zigachev
54b843c749SSergey Zigachev dm_write_reg(ctx, addr, reg_val);
55b843c749SSergey Zigachev va_end(ap);
56b843c749SSergey Zigachev
57b843c749SSergey Zigachev return reg_val;
58b843c749SSergey Zigachev }
59b843c749SSergey Zigachev
60b843c749SSergey Zigachev uint32_t generic_reg_get(const struct dc_context *ctx, uint32_t addr,
61*78973132SSergey Zigachev uint8_t shift, uint32_t mask, uint32_t *field_value);
generic_reg_get(const struct dc_context * ctx,uint32_t addr,uint8_t shift,uint32_t mask,uint32_t * field_value)62*78973132SSergey Zigachev uint32_t generic_reg_get(const struct dc_context *ctx, uint32_t addr,
63b843c749SSergey Zigachev uint8_t shift, uint32_t mask, uint32_t *field_value)
64b843c749SSergey Zigachev {
65b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
66b843c749SSergey Zigachev *field_value = get_reg_field_value_ex(reg_val, mask, shift);
67b843c749SSergey Zigachev return reg_val;
68b843c749SSergey Zigachev }
69b843c749SSergey Zigachev
70b843c749SSergey Zigachev uint32_t generic_reg_get2(const struct dc_context *ctx, uint32_t addr,
71b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
72*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2);
generic_reg_get2(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2)73*78973132SSergey Zigachev uint32_t generic_reg_get2(const struct dc_context *ctx, uint32_t addr,
74*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
75b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2)
76b843c749SSergey Zigachev {
77b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
78b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
79b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
80b843c749SSergey Zigachev return reg_val;
81b843c749SSergey Zigachev }
82b843c749SSergey Zigachev
83b843c749SSergey Zigachev uint32_t generic_reg_get3(const struct dc_context *ctx, uint32_t addr,
84b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
85b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
86*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3);
generic_reg_get3(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3)87*78973132SSergey Zigachev uint32_t generic_reg_get3(const struct dc_context *ctx, uint32_t addr,
88*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
89*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
90b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3)
91b843c749SSergey Zigachev {
92b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
93b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
94b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
95b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
96b843c749SSergey Zigachev return reg_val;
97b843c749SSergey Zigachev }
98b843c749SSergey Zigachev
99b843c749SSergey Zigachev uint32_t generic_reg_get4(const struct dc_context *ctx, uint32_t addr,
100b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
101b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
102b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
103*78973132SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4);
generic_reg_get4(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3,uint8_t shift4,uint32_t mask4,uint32_t * field_value4)104*78973132SSergey Zigachev uint32_t generic_reg_get4(const struct dc_context *ctx, uint32_t addr,
105*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
106*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
107*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
108b843c749SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4)
109b843c749SSergey Zigachev {
110b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
111b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
112b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
113b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
114b843c749SSergey Zigachev *field_value4 = get_reg_field_value_ex(reg_val, mask4, shift4);
115b843c749SSergey Zigachev return reg_val;
116b843c749SSergey Zigachev }
117b843c749SSergey Zigachev
118b843c749SSergey Zigachev uint32_t generic_reg_get5(const struct dc_context *ctx, uint32_t addr,
119b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
120b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
121b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
122b843c749SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
123*78973132SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5);
generic_reg_get5(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3,uint8_t shift4,uint32_t mask4,uint32_t * field_value4,uint8_t shift5,uint32_t mask5,uint32_t * field_value5)124*78973132SSergey Zigachev uint32_t generic_reg_get5(const struct dc_context *ctx, uint32_t addr,
125*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
126*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
127*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
128*78973132SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
129b843c749SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5)
130b843c749SSergey Zigachev {
131b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
132b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
133b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
134b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
135b843c749SSergey Zigachev *field_value4 = get_reg_field_value_ex(reg_val, mask4, shift4);
136b843c749SSergey Zigachev *field_value5 = get_reg_field_value_ex(reg_val, mask5, shift5);
137b843c749SSergey Zigachev return reg_val;
138b843c749SSergey Zigachev }
139b843c749SSergey Zigachev
140b843c749SSergey Zigachev uint32_t generic_reg_get6(const struct dc_context *ctx, uint32_t addr,
141b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
142b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
143b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
144b843c749SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
145b843c749SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
146*78973132SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6);
generic_reg_get6(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3,uint8_t shift4,uint32_t mask4,uint32_t * field_value4,uint8_t shift5,uint32_t mask5,uint32_t * field_value5,uint8_t shift6,uint32_t mask6,uint32_t * field_value6)147*78973132SSergey Zigachev uint32_t generic_reg_get6(const struct dc_context *ctx, uint32_t addr,
148*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
149*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
150*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
151*78973132SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
152*78973132SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
153b843c749SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6)
154b843c749SSergey Zigachev {
155b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
156b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
157b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
158b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
159b843c749SSergey Zigachev *field_value4 = get_reg_field_value_ex(reg_val, mask4, shift4);
160b843c749SSergey Zigachev *field_value5 = get_reg_field_value_ex(reg_val, mask5, shift5);
161b843c749SSergey Zigachev *field_value6 = get_reg_field_value_ex(reg_val, mask6, shift6);
162b843c749SSergey Zigachev return reg_val;
163b843c749SSergey Zigachev }
164b843c749SSergey Zigachev
165b843c749SSergey Zigachev uint32_t generic_reg_get7(const struct dc_context *ctx, uint32_t addr,
166b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
167b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
168b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
169b843c749SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
170b843c749SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
171b843c749SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6,
172*78973132SSergey Zigachev uint8_t shift7, uint32_t mask7, uint32_t *field_value7);
generic_reg_get7(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3,uint8_t shift4,uint32_t mask4,uint32_t * field_value4,uint8_t shift5,uint32_t mask5,uint32_t * field_value5,uint8_t shift6,uint32_t mask6,uint32_t * field_value6,uint8_t shift7,uint32_t mask7,uint32_t * field_value7)173*78973132SSergey Zigachev uint32_t generic_reg_get7(const struct dc_context *ctx, uint32_t addr,
174*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
175*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
176*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
177*78973132SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
178*78973132SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
179*78973132SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6,
180b843c749SSergey Zigachev uint8_t shift7, uint32_t mask7, uint32_t *field_value7)
181b843c749SSergey Zigachev {
182b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
183b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
184b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
185b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
186b843c749SSergey Zigachev *field_value4 = get_reg_field_value_ex(reg_val, mask4, shift4);
187b843c749SSergey Zigachev *field_value5 = get_reg_field_value_ex(reg_val, mask5, shift5);
188b843c749SSergey Zigachev *field_value6 = get_reg_field_value_ex(reg_val, mask6, shift6);
189b843c749SSergey Zigachev *field_value7 = get_reg_field_value_ex(reg_val, mask7, shift7);
190b843c749SSergey Zigachev return reg_val;
191b843c749SSergey Zigachev }
192b843c749SSergey Zigachev
193b843c749SSergey Zigachev uint32_t generic_reg_get8(const struct dc_context *ctx, uint32_t addr,
194b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
195b843c749SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
196b843c749SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
197b843c749SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
198b843c749SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
199b843c749SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6,
200b843c749SSergey Zigachev uint8_t shift7, uint32_t mask7, uint32_t *field_value7,
201*78973132SSergey Zigachev uint8_t shift8, uint32_t mask8, uint32_t *field_value8);
generic_reg_get8(const struct dc_context * ctx,uint32_t addr,uint8_t shift1,uint32_t mask1,uint32_t * field_value1,uint8_t shift2,uint32_t mask2,uint32_t * field_value2,uint8_t shift3,uint32_t mask3,uint32_t * field_value3,uint8_t shift4,uint32_t mask4,uint32_t * field_value4,uint8_t shift5,uint32_t mask5,uint32_t * field_value5,uint8_t shift6,uint32_t mask6,uint32_t * field_value6,uint8_t shift7,uint32_t mask7,uint32_t * field_value7,uint8_t shift8,uint32_t mask8,uint32_t * field_value8)202*78973132SSergey Zigachev uint32_t generic_reg_get8(const struct dc_context *ctx, uint32_t addr,
203*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t *field_value1,
204*78973132SSergey Zigachev uint8_t shift2, uint32_t mask2, uint32_t *field_value2,
205*78973132SSergey Zigachev uint8_t shift3, uint32_t mask3, uint32_t *field_value3,
206*78973132SSergey Zigachev uint8_t shift4, uint32_t mask4, uint32_t *field_value4,
207*78973132SSergey Zigachev uint8_t shift5, uint32_t mask5, uint32_t *field_value5,
208*78973132SSergey Zigachev uint8_t shift6, uint32_t mask6, uint32_t *field_value6,
209*78973132SSergey Zigachev uint8_t shift7, uint32_t mask7, uint32_t *field_value7,
210b843c749SSergey Zigachev uint8_t shift8, uint32_t mask8, uint32_t *field_value8)
211b843c749SSergey Zigachev {
212b843c749SSergey Zigachev uint32_t reg_val = dm_read_reg(ctx, addr);
213b843c749SSergey Zigachev *field_value1 = get_reg_field_value_ex(reg_val, mask1, shift1);
214b843c749SSergey Zigachev *field_value2 = get_reg_field_value_ex(reg_val, mask2, shift2);
215b843c749SSergey Zigachev *field_value3 = get_reg_field_value_ex(reg_val, mask3, shift3);
216b843c749SSergey Zigachev *field_value4 = get_reg_field_value_ex(reg_val, mask4, shift4);
217b843c749SSergey Zigachev *field_value5 = get_reg_field_value_ex(reg_val, mask5, shift5);
218b843c749SSergey Zigachev *field_value6 = get_reg_field_value_ex(reg_val, mask6, shift6);
219b843c749SSergey Zigachev *field_value7 = get_reg_field_value_ex(reg_val, mask7, shift7);
220b843c749SSergey Zigachev *field_value8 = get_reg_field_value_ex(reg_val, mask8, shift8);
221b843c749SSergey Zigachev return reg_val;
222b843c749SSergey Zigachev }
223b843c749SSergey Zigachev /* note: va version of this is pretty bad idea, since there is a output parameter pass by pointer
224b843c749SSergey Zigachev * compiler won't be able to check for size match and is prone to stack corruption type of bugs
225b843c749SSergey Zigachev
226b843c749SSergey Zigachev uint32_t generic_reg_get(const struct dc_context *ctx,
227b843c749SSergey Zigachev uint32_t addr, int n, ...)
228b843c749SSergey Zigachev {
229b843c749SSergey Zigachev uint32_t shift, mask;
230b843c749SSergey Zigachev uint32_t *field_value;
231b843c749SSergey Zigachev uint32_t reg_val;
232b843c749SSergey Zigachev int i = 0;
233b843c749SSergey Zigachev
234b843c749SSergey Zigachev reg_val = dm_read_reg(ctx, addr);
235b843c749SSergey Zigachev
236b843c749SSergey Zigachev va_list ap;
237b843c749SSergey Zigachev va_start(ap, n);
238b843c749SSergey Zigachev
239b843c749SSergey Zigachev while (i < n) {
240b843c749SSergey Zigachev shift = va_arg(ap, uint32_t);
241b843c749SSergey Zigachev mask = va_arg(ap, uint32_t);
242b843c749SSergey Zigachev field_value = va_arg(ap, uint32_t *);
243b843c749SSergey Zigachev
244b843c749SSergey Zigachev *field_value = get_reg_field_value_ex(reg_val, mask, shift);
245b843c749SSergey Zigachev i++;
246b843c749SSergey Zigachev }
247b843c749SSergey Zigachev
248b843c749SSergey Zigachev va_end(ap);
249b843c749SSergey Zigachev
250b843c749SSergey Zigachev return reg_val;
251b843c749SSergey Zigachev }
252b843c749SSergey Zigachev */
253b843c749SSergey Zigachev
generic_reg_wait(const struct dc_context * ctx,uint32_t addr,uint32_t shift,uint32_t mask,uint32_t condition_value,unsigned int delay_between_poll_us,unsigned int time_out_num_tries,const char * func_name,int line)254b843c749SSergey Zigachev uint32_t generic_reg_wait(const struct dc_context *ctx,
255b843c749SSergey Zigachev uint32_t addr, uint32_t shift, uint32_t mask, uint32_t condition_value,
256b843c749SSergey Zigachev unsigned int delay_between_poll_us, unsigned int time_out_num_tries,
257b843c749SSergey Zigachev const char *func_name, int line)
258b843c749SSergey Zigachev {
259b843c749SSergey Zigachev uint32_t field_value;
260*78973132SSergey Zigachev uint32_t reg_val = 0;
261b843c749SSergey Zigachev int i;
262b843c749SSergey Zigachev
263b843c749SSergey Zigachev /* something is terribly wrong if time out is > 200ms. (5Hz) */
264b843c749SSergey Zigachev ASSERT(delay_between_poll_us * time_out_num_tries <= 200000);
265b843c749SSergey Zigachev
266b843c749SSergey Zigachev if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment)) {
267b843c749SSergey Zigachev /* 35 seconds */
268b843c749SSergey Zigachev delay_between_poll_us = 35000;
269b843c749SSergey Zigachev time_out_num_tries = 1000;
270b843c749SSergey Zigachev }
271b843c749SSergey Zigachev
272b843c749SSergey Zigachev for (i = 0; i <= time_out_num_tries; i++) {
273b843c749SSergey Zigachev if (i) {
274b843c749SSergey Zigachev if (delay_between_poll_us >= 1000)
275b843c749SSergey Zigachev msleep(delay_between_poll_us/1000);
276b843c749SSergey Zigachev else if (delay_between_poll_us > 0)
277b843c749SSergey Zigachev udelay(delay_between_poll_us);
278b843c749SSergey Zigachev }
279b843c749SSergey Zigachev
280b843c749SSergey Zigachev reg_val = dm_read_reg(ctx, addr);
281b843c749SSergey Zigachev
282b843c749SSergey Zigachev field_value = get_reg_field_value_ex(reg_val, mask, shift);
283b843c749SSergey Zigachev
284b843c749SSergey Zigachev if (field_value == condition_value) {
285b843c749SSergey Zigachev if (i * delay_between_poll_us > 1000)
286b843c749SSergey Zigachev dm_output_to_console("REG_WAIT taking a while: %dms in %s line:%d\n",
287b843c749SSergey Zigachev delay_between_poll_us * i / 1000,
288b843c749SSergey Zigachev func_name, line);
289b843c749SSergey Zigachev return reg_val;
290b843c749SSergey Zigachev }
291b843c749SSergey Zigachev }
292b843c749SSergey Zigachev
293b843c749SSergey Zigachev dm_error("REG_WAIT timeout %dus * %d tries - %s line:%d\n",
294b843c749SSergey Zigachev delay_between_poll_us, time_out_num_tries,
295b843c749SSergey Zigachev func_name, line);
296b843c749SSergey Zigachev
297b843c749SSergey Zigachev if (!IS_FPGA_MAXIMUS_DC(ctx->dce_environment))
298b843c749SSergey Zigachev BREAK_TO_DEBUGGER();
299b843c749SSergey Zigachev
300b843c749SSergey Zigachev return reg_val;
301b843c749SSergey Zigachev }
302b843c749SSergey Zigachev
303b843c749SSergey Zigachev void generic_write_indirect_reg(const struct dc_context *ctx,
304b843c749SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
305*78973132SSergey Zigachev uint32_t index, uint32_t data);
generic_write_indirect_reg(const struct dc_context * ctx,uint32_t addr_index,uint32_t addr_data,uint32_t index,uint32_t data)306*78973132SSergey Zigachev void generic_write_indirect_reg(const struct dc_context *ctx,
307*78973132SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
308b843c749SSergey Zigachev uint32_t index, uint32_t data)
309b843c749SSergey Zigachev {
310b843c749SSergey Zigachev dm_write_reg(ctx, addr_index, index);
311b843c749SSergey Zigachev dm_write_reg(ctx, addr_data, data);
312b843c749SSergey Zigachev }
313b843c749SSergey Zigachev
314b843c749SSergey Zigachev uint32_t generic_read_indirect_reg(const struct dc_context *ctx,
315b843c749SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
316*78973132SSergey Zigachev uint32_t index);
generic_read_indirect_reg(const struct dc_context * ctx,uint32_t addr_index,uint32_t addr_data,uint32_t index)317*78973132SSergey Zigachev uint32_t generic_read_indirect_reg(const struct dc_context *ctx,
318*78973132SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
319b843c749SSergey Zigachev uint32_t index)
320b843c749SSergey Zigachev {
321b843c749SSergey Zigachev uint32_t value = 0;
322b843c749SSergey Zigachev
323b843c749SSergey Zigachev dm_write_reg(ctx, addr_index, index);
324b843c749SSergey Zigachev value = dm_read_reg(ctx, addr_data);
325b843c749SSergey Zigachev
326b843c749SSergey Zigachev return value;
327b843c749SSergey Zigachev }
328b843c749SSergey Zigachev
329*78973132SSergey Zigachev uint32_t generic_indirect_reg_update_ex(const struct dc_context *ctx,
330*78973132SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
331*78973132SSergey Zigachev uint32_t index, uint32_t reg_val, int n,
332*78973132SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t field_value1,
333*78973132SSergey Zigachev ...);
generic_indirect_reg_update_ex(const struct dc_context * ctx,uint32_t addr_index,uint32_t addr_data,uint32_t index,uint32_t reg_val,int n,uint8_t shift1,uint32_t mask1,uint32_t field_value1,...)334b843c749SSergey Zigachev uint32_t generic_indirect_reg_update_ex(const struct dc_context *ctx,
335b843c749SSergey Zigachev uint32_t addr_index, uint32_t addr_data,
336b843c749SSergey Zigachev uint32_t index, uint32_t reg_val, int n,
337b843c749SSergey Zigachev uint8_t shift1, uint32_t mask1, uint32_t field_value1,
338b843c749SSergey Zigachev ...)
339b843c749SSergey Zigachev {
340b843c749SSergey Zigachev uint32_t shift, mask, field_value;
341b843c749SSergey Zigachev int i = 1;
342b843c749SSergey Zigachev
343b843c749SSergey Zigachev va_list ap;
344b843c749SSergey Zigachev
345b843c749SSergey Zigachev va_start(ap, field_value1);
346b843c749SSergey Zigachev
347b843c749SSergey Zigachev reg_val = set_reg_field_value_ex(reg_val, field_value1, mask1, shift1);
348b843c749SSergey Zigachev
349b843c749SSergey Zigachev while (i < n) {
350b843c749SSergey Zigachev shift = va_arg(ap, uint32_t);
351b843c749SSergey Zigachev mask = va_arg(ap, uint32_t);
352b843c749SSergey Zigachev field_value = va_arg(ap, uint32_t);
353b843c749SSergey Zigachev
354b843c749SSergey Zigachev reg_val = set_reg_field_value_ex(reg_val, field_value, mask, shift);
355b843c749SSergey Zigachev i++;
356b843c749SSergey Zigachev }
357b843c749SSergey Zigachev
358b843c749SSergey Zigachev generic_write_indirect_reg(ctx, addr_index, addr_data, index, reg_val);
359b843c749SSergey Zigachev va_end(ap);
360b843c749SSergey Zigachev
361b843c749SSergey Zigachev return reg_val;
362b843c749SSergey Zigachev }
363