xref: /aosp_15_r20/external/coreboot/src/vendorcode/cavium/bdk/libbdk-hal/qlm/bdk-qlm-margin-cn8xxx.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /***********************license start***********************************
2 * Copyright (c) 2003-2017  Cavium Inc. ([email protected]). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17 *
18 *   * Neither the name of Cavium Inc. nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22 *
23 * This Software, including technical data, may be subject to U.S. export
24 * control laws, including the U.S. Export Administration Act and its
25 * associated regulations, and may be subject to export or import
26 * regulations in other countries.
27 *
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
31 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
32 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
33 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
34 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
35 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
36 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK
37 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39 #include <bdk.h>
40 #include "libbdk-arch/bdk-csrs-gser.h"
41 #include "libbdk-hal/if/bdk-if.h"
42 #include "libbdk-hal/bdk-qlm.h"
43 #include "libbdk-hal/bdk-utils.h"
44 
45 /* This code is an optional part of the BDK. It is only linked in
46     if BDK_REQUIRE() needs it */
47 BDK_REQUIRE_DEFINE(QLM_MARGIN);
48 
49 typedef union
50 {
51     struct
52     {
53         uint64_t rx_os_mvalbbd_2 :16;
54         uint64_t rx_os_mvalbbd_1 :16;
55         uint64_t reserved_63_32 :32;
56 
57     } s;
58     struct
59     {
60         uint64_t Qb :6;
61         uint64_t Q :6;
62         uint64_t Lb :6; // Spans the two registers
63         uint64_t L :6;
64         uint64_t qerr0 :6;
65         int64_t reserved_63_30 :34;
66     } f;
67     uint64_t u;
68 } rx_os_mvalbbd_t;
69 
70 int __bdk_disable_ccpi_error_report = 0;
71 
convert_to_signed_mag(int source)72 static int convert_to_signed_mag(int source)
73 {
74     /* Synopsis encoded sign in an unexpected way. 0=negative and 1=positive
75        So bit 5 should be 0 for negative numbers, 1 for positive numbers */
76     if (source < 0)
77         source = -source;
78     else
79         source |= 0x20;
80     return source;
81 }
82 
get_current_settings(bdk_node_t node,int qlm,int qlm_lane)83 static rx_os_mvalbbd_t get_current_settings(bdk_node_t node, int qlm, int qlm_lane)
84 {
85     rx_os_mvalbbd_t mvalbbd;
86     mvalbbd.u = 0;
87 
88     BDK_CSR_INIT(rx_cfg_1, node, BDK_GSERX_LANEX_RX_CFG_1(qlm, qlm_lane));
89     if (!rx_cfg_1.s.pcs_sds_rx_os_men)
90     {
91         /* Get the current settings */
92         BDK_CSR_INIT(rx_os_out_1, node, BDK_GSERX_LANEX_RX_OS_OUT_1(qlm, qlm_lane));
93         BDK_CSR_INIT(rx_os_out_2, node, BDK_GSERX_LANEX_RX_OS_OUT_2(qlm, qlm_lane));
94         BDK_CSR_INIT(rx_os_out_3, node, BDK_GSERX_LANEX_RX_OS_OUT_3(qlm, qlm_lane));
95         int qerr0 = bdk_extracts(rx_os_out_1.u, 0, 6);
96         int lb = bdk_extracts(rx_os_out_2.u, 0, 6);
97         int l = bdk_extracts(rx_os_out_2.u, 6, 6);
98         int qb = bdk_extracts(rx_os_out_3.u, 0, 6);
99         int q = bdk_extracts(rx_os_out_3.u, 6, 6);
100         /* Enable the override with the current values */
101         mvalbbd.f.Qb = convert_to_signed_mag(qb);
102         mvalbbd.f.Q = convert_to_signed_mag(q);
103         mvalbbd.f.Lb = convert_to_signed_mag(lb);
104         mvalbbd.f.L = convert_to_signed_mag(l);
105         mvalbbd.f.qerr0 = convert_to_signed_mag(qerr0);
106     }
107     else
108     {
109         BDK_CSR_INIT(mvalbbd_1, node, BDK_GSERX_LANEX_RX_OS_MVALBBD_1(qlm, qlm_lane));
110         mvalbbd.s.rx_os_mvalbbd_1 = mvalbbd_1.s.pcs_sds_rx_os_mval;
111         BDK_CSR_INIT(mvalbbd_2, node, BDK_GSERX_LANEX_RX_OS_MVALBBD_2(qlm, qlm_lane));
112         mvalbbd.s.rx_os_mvalbbd_2 = mvalbbd_2.s.pcs_sds_rx_os_mval;
113     }
114     //printf("qerr0=%d, lb=%d, l=%d, qb=%d, q=%d\n",
115     //    mvalbbd.f.qerr0, mvalbbd.f.Lb, mvalbbd.f.L, mvalbbd.f.Qb, mvalbbd.f.Q);
116     return mvalbbd;
117 }
118 
119 /**
120  * Get the current RX margining parameter
121  *
122  * @param node     Node to read margin value from
123  * @param qlm      QLM to read from
124  * @param qlm_lane Lane to read
125  * @param margin_type
126  *                 Type of margining parameter to read
127  *
128  * @return Current margining parameter value
129  */
bdk_qlm_margin_rx_get(bdk_node_t node,int qlm,int qlm_lane,bdk_qlm_margin_t margin_type)130 int64_t bdk_qlm_margin_rx_get(bdk_node_t node, int qlm, int qlm_lane, bdk_qlm_margin_t margin_type)
131 {
132     rx_os_mvalbbd_t mvalbbd = get_current_settings(node, qlm, qlm_lane);
133 
134     switch (margin_type)
135     {
136         case BDK_QLM_MARGIN_VERTICAL:
137             if (mvalbbd.f.Q & 0x20) /* Check if sign bit says positive */
138                 return mvalbbd.f.Q & 0x1f; /* positive, strip off sign */
139             else
140                 return -mvalbbd.f.Q; /* negative */
141         case BDK_QLM_MARGIN_HORIZONTAL:
142             return 0;
143     }
144     return 0;
145 }
146 
147 /**
148  * Get the current RX margining parameter minimum value
149  *
150  * @param node     Node to read margin value from
151  * @param qlm      QLM to read from
152  * @param qlm_lane Lane to read
153  * @param margin_type
154  *                 Type of margining parameter to read
155  *
156  * @return Current margining parameter minimum value
157  */
bdk_qlm_margin_rx_get_min(bdk_node_t node,int qlm,int qlm_lane,bdk_qlm_margin_t margin_type)158 int64_t bdk_qlm_margin_rx_get_min(bdk_node_t node, int qlm, int qlm_lane, bdk_qlm_margin_t margin_type)
159 {
160     switch (margin_type)
161     {
162         case BDK_QLM_MARGIN_VERTICAL:
163             return -31;
164         case BDK_QLM_MARGIN_HORIZONTAL:
165             return 0;
166     }
167     return 0;
168 }
169 
170 /**
171  * Get the current RX margining parameter maximum value
172  *
173  * @param node     Node to read margin value from
174  * @param qlm      QLM to read from
175  * @param qlm_lane Lane to read
176  * @param margin_type
177  *                 Type of margining parameter to read
178  *
179  * @return Current margining parameter maximum value
180  */
bdk_qlm_margin_rx_get_max(bdk_node_t node,int qlm,int qlm_lane,bdk_qlm_margin_t margin_type)181 int64_t bdk_qlm_margin_rx_get_max(bdk_node_t node, int qlm, int qlm_lane, bdk_qlm_margin_t margin_type)
182 {
183     switch (margin_type)
184     {
185         case BDK_QLM_MARGIN_VERTICAL:
186             return 31;
187         case BDK_QLM_MARGIN_HORIZONTAL:
188             return 0;
189     }
190     return 0;
191 }
192 
193 /**
194  * Set the current RX margining parameter value
195  *
196  * @param node     Node to set margin value on
197  * @param qlm      QLM to set
198  * @param qlm_lane Lane to set
199  * @param margin_type
200  *                 Type of margining parameter to set
201  * @param value    Value of margining parameter
202  *
203  * @return Zero on success, negative on failure
204  */
bdk_qlm_margin_rx_set(bdk_node_t node,int qlm,int qlm_lane,bdk_qlm_margin_t margin_type,int value)205 int bdk_qlm_margin_rx_set(bdk_node_t node, int qlm, int qlm_lane, bdk_qlm_margin_t margin_type, int value)
206 {
207     rx_os_mvalbbd_t mvalbbd = get_current_settings(node, qlm, qlm_lane);
208 
209     switch (margin_type)
210     {
211         case BDK_QLM_MARGIN_VERTICAL:
212             if (value < 0)
213                 mvalbbd.f.Q = -value; /* Sign bit is zero, weird Synopsys */
214             else
215                 mvalbbd.f.Q = value | 0x20; /* Sign bit is one, weird Synopsys */
216             break;
217         case BDK_QLM_MARGIN_HORIZONTAL:
218             return -1;
219     }
220 
221     BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_RX_OS_MVALBBD_1(qlm, qlm_lane),
222         c.s.pcs_sds_rx_os_mval = mvalbbd.s.rx_os_mvalbbd_1);
223     BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_RX_OS_MVALBBD_2(qlm, qlm_lane),
224         c.s.pcs_sds_rx_os_mval = mvalbbd.s.rx_os_mvalbbd_2);
225     BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_RX_CFG_1(qlm, qlm_lane),
226         c.s.pcs_sds_rx_os_men = 1);
227 
228     /* Disable the DFE(s), gives a better eye measurement */
229     BDK_CSR_INIT(pwr_ctrl, node, BDK_GSERX_LANEX_PWR_CTRL(qlm, qlm_lane));
230     if (!pwr_ctrl.s.rx_lctrl_ovrrd_en)
231     {
232         BDK_CSR_WRITE(node, BDK_GSERX_LANEX_RX_LOOP_CTRL(qlm, qlm_lane), 0xF1);
233         BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_PWR_CTRL(qlm, qlm_lane),
234              c.s.rx_lctrl_ovrrd_en =  1);
235     }
236 
237     if (qlm >= 8)
238         __bdk_disable_ccpi_error_report = 1;
239 
240     return 0;
241 }
242 
243 /**
244  * Restore the supplied RX margining parameter value as if it was never set. This
245  * disables any overrides in the SERDES need to perform margining
246  *
247  * @param node     Node to restore margin value on
248  * @param qlm      QLM to restore
249  * @param qlm_lane Lane to restore
250  * @param margin_type
251  *                 Type of margining parameter to restore
252  * @param value    Value of margining parameter
253  *
254  * @return Zero on success, negative on failure
255  */
bdk_qlm_margin_rx_restore(bdk_node_t node,int qlm,int qlm_lane,bdk_qlm_margin_t margin_type,int value)256 int bdk_qlm_margin_rx_restore(bdk_node_t node, int qlm, int qlm_lane, bdk_qlm_margin_t margin_type, int value)
257 {
258     BDK_CSR_INIT(rx_cfg_1, node, BDK_GSERX_LANEX_RX_CFG_1(qlm, qlm_lane));
259     /* Return if no overrides have been applied */
260     if (!rx_cfg_1.s.pcs_sds_rx_os_men)
261         return 0;
262     bdk_qlm_margin_rx_set(node, qlm, qlm_lane, margin_type, value);
263     BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_RX_CFG_1(qlm, qlm_lane),
264         c.s.pcs_sds_rx_os_men = 0);
265     /* Enable the DFE(s) */
266     BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_PWR_CTRL(qlm, qlm_lane),
267          c.s.rx_lctrl_ovrrd_en =  0);
268     __bdk_disable_ccpi_error_report = 0;
269     return 0;
270 }
271 
272