coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
pmif_clk.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <commonlib/helpers.h>
4 #include <console/console.h>
5 #include <soc/pmif_clk_common.h>
6 #include <soc/pmif_sw.h>
7 
8 int pmif_ulposc_cali(u32 target_val)
9 {
10  u32 current_val, min = 0, max = CAL_MAX_VAL, middle;
11  int diff_by_min, diff_by_max, cal_result;
12 
13  do {
14  middle = (min + max) / 2;
15  if (middle == min)
16  break;
17 
18  current_val = pmif_get_ulposc_freq_mhz(middle);
19  if (current_val > target_val)
20  max = middle;
21  else
22  min = middle;
23  } while (min <= max);
24 
25  diff_by_min = pmif_get_ulposc_freq_mhz(min) - target_val;
26  diff_by_min = ABS(diff_by_min);
27 
28  diff_by_max = pmif_get_ulposc_freq_mhz(max) - target_val;
29  diff_by_max = ABS(diff_by_max);
30 
31  cal_result = (diff_by_min < diff_by_max) ? min : max;
32  current_val = pmif_get_ulposc_freq_mhz(cal_result);
33 
34  /* check if calibrated value is in the range of target value +- 15% */
35  if (current_val < (target_val * (1000 - CAL_TOL_RATE) / 1000) ||
36  current_val > (target_val * (1000 + CAL_TOL_RATE) / 1000)) {
37  printk(BIOS_ERR, "[%s] calibration fail: cur=%d, CAL_RATE=%d, target=%dM\n",
38  __func__, current_val, CAL_TOL_RATE, target_val);
39  return 1;
40  }
41 
42  return 0;
43 }
#define ABS(a)
Definition: helpers.h:44
int pmif_ulposc_cali(u32 target_val)
Definition: pmif_clk.c:8
#define printk(level,...)
Definition: stdlib.h:16
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
u32 pmif_get_ulposc_freq_mhz(u32 cali_val)
Definition: pmif_clk.c:75
@ CAL_MAX_VAL
Definition: pmif_sw.h:23
@ CAL_TOL_RATE
Definition: pmif_sw.h:22
uint32_t u32
Definition: stdint.h:51