blob: 28fc6e2edfcd890c3ab5ec7de422a9a423e74fb8 [file] [log] [blame]
Werner Zeh6c571462016-07-05 07:16:34 +02001/*
2 * This file is part of the coreboot project.
3 *
Werner Zehc38ab852017-07-27 13:48:18 +02004 * Copyright (C) 2016-2017 Siemens AG.
Werner Zeh6c571462016-07-05 07:16:34 +02005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <types.h>
17#include <console/console.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
20#include <device/pci_ops.h>
21#include <device/pci_def.h>
22#include <string.h>
23#include <delay.h>
24#include <hwilib.h>
25#include "nc_fpga.h"
26
27#define FPGA_SET_PARAM(src, dst) \
28{ \
Werner Zeh2db79222017-07-05 15:52:52 +020029 uint32_t var; \
30 if (hwilib_get_field(src, (uint8_t *)&var, sizeof(var))) \
Werner Zehd5960c42017-07-14 10:24:00 +020031 dst = ((typeof(dst))var); \
Werner Zeh6c571462016-07-05 07:16:34 +020032}
33
34static void init_temp_mon (void *base_adr)
35{
36 uint32_t cc[5], i = 0;
37 uint8_t num = 0;
38 volatile fan_ctrl_t *ctrl = (fan_ctrl_t *)base_adr;
39
40 /* Program sensor delay first. */
41 FPGA_SET_PARAM(FANSensorDelay, ctrl->sensordelay);
42 /* Program correction curve for every used sensor. */
Werner Zeh2db79222017-07-05 15:52:52 +020043 if ((hwilib_get_field(FANSensorNum, &num, 1) != 1) ||
Werner Zeh6c571462016-07-05 07:16:34 +020044 (num == 0) || (num > MAX_NUM_SENSORS))
45 return;
46 for (i = 0; i < num; i ++) {
47 if (hwilib_get_field(FANSensorCfg0 + i, (uint8_t *)&cc[0],
48 sizeof(cc)) == sizeof(cc)) {
49 ctrl->sensorcfg[cc[0]].rmin = cc[1] & 0xffff;
50 ctrl->sensorcfg[cc[0]].rmax = cc[2] & 0xffff;
51 ctrl->sensorcfg[cc[0]].nmin = cc[3] & 0xffff;
52 ctrl->sensorcfg[cc[0]].nmax = cc[4] & 0xffff;
53 }
54 }
55 ctrl->sensornum = num;
Mario Scheithauer0b42c8a2017-09-26 13:37:49 +020056
57 /* Program sensor selection and temperature thresholds. */
58 FPGA_SET_PARAM(FANSensorSelect, ctrl->sensorselect);
59 FPGA_SET_PARAM(T_Warn, ctrl->t_warn);
60 FPGA_SET_PARAM(T_Crit, ctrl->t_crit);
Werner Zeh6c571462016-07-05 07:16:34 +020061}
62
63static void init_fan_ctrl (void *base_adr)
64{
Werner Zeh89a7b6b2017-06-30 10:38:08 +020065 uint8_t mask = 0, freeze_disable = 0, fan_req = 0;
Werner Zeh6c571462016-07-05 07:16:34 +020066 volatile fan_ctrl_t *ctrl = (fan_ctrl_t *)base_adr;
67
68 /* Program all needed fields of FAN controller. */
Werner Zeh6c571462016-07-05 07:16:34 +020069 FPGA_SET_PARAM(FANSamplingTime, ctrl->samplingtime);
70 FPGA_SET_PARAM(FANSetPoint, ctrl->setpoint);
71 FPGA_SET_PARAM(FANHystCtrl, ctrl->hystctrl);
72 FPGA_SET_PARAM(FANHystVal, ctrl->hystval);
73 FPGA_SET_PARAM(FANHystThreshold, ctrl->hystthreshold);
74 FPGA_SET_PARAM(FANKp, ctrl->kp);
75 FPGA_SET_PARAM(FANKi, ctrl->ki);
76 FPGA_SET_PARAM(FANKd, ctrl->kd);
77 FPGA_SET_PARAM(FANMaxSpeed, ctrl->fanmax);
Werner Zehc38ab852017-07-27 13:48:18 +020078 FPGA_SET_PARAM(FANStartSpeed, ctrl->fanmin);
Werner Zeh6c571462016-07-05 07:16:34 +020079 /* Set freeze and FAN configuration. */
80 if ((hwilib_get_field(FF_FanReq, &fan_req, 1) == 1) &&
Werner Zeh89a7b6b2017-06-30 10:38:08 +020081 (hwilib_get_field(FF_FreezeDis, &freeze_disable, 1) == 1)) {
Werner Zeh6c571462016-07-05 07:16:34 +020082 if (!fan_req)
83 mask = 1;
Werner Zeh89a7b6b2017-06-30 10:38:08 +020084 else if (fan_req && !freeze_disable)
Werner Zeh6c571462016-07-05 07:16:34 +020085 mask = 2;
86 else
87 mask = 3;
88 ctrl->fanmon = mask << 10;
89 }
90}
91
92/** \brief This function is the driver entry point for the init phase
93 * of the PCI bus allocator. It will initialize all the needed parts
94 * of NC_FPGA.
95 * @param *dev Pointer to the used PCI device
96 * @return void Nothing is given back
97 */
98static void nc_fpga_init(struct device *dev)
99{
100 void *bar0_ptr = NULL;
101 uint8_t cmd_reg;
102 uint32_t cap = 0;
103
104 /* All we need is mapped to BAR 0, get the address. */
105 bar0_ptr = (void *)(pci_read_config32(dev, PCI_BASE_ADDRESS_0) &
106 ~PCI_BASE_ADDRESS_MEM_ATTR_MASK);
107 cmd_reg = pci_read_config8(dev, PCI_COMMAND);
108 /* Ensure BAR0 has a valid value. */
109 if (!bar0_ptr || !(cmd_reg & PCI_COMMAND_MEMORY))
110 return;
111 /* Ensure this is really a NC FPGA by checking magic register. */
112 if (read32(bar0_ptr + NC_MAGIC_OFFSET) != NC_FPGA_MAGIC)
113 return;
114 /* Open hwinfo block. */
115 if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS)
116 return;
117 /* Set up FAN controller and temperature monitor according to */
118 /* capability bits. */
119 cap = read32(bar0_ptr + NC_CAP1_OFFSET);
120 if (cap & (NC_CAP1_TEMP_MON | NC_CAP1_FAN_CTRL))
121 init_temp_mon(bar0_ptr + NC_FANMON_CTRL_OFFSET);
122 if (cap & NC_CAP1_FAN_CTRL)
123 init_fan_ctrl(bar0_ptr + NC_FANMON_CTRL_OFFSET);
Mario Scheithauerc4ff1de2017-06-12 10:02:10 +0200124 if (cap & NC_CAP1_DSAVE_NMI_DELAY) {
125 uint16_t *dsave_ptr = (uint16_t *)(bar0_ptr + NC_DSAVE_OFFSET);
126 FPGA_SET_PARAM(NvramVirtTimeDsaveReset, *dsave_ptr);
127 }
128 if (cap & NC_CAP1_BL_BRIGHTNESS_CTRL) {
129 uint8_t *bl_bn_ptr =
130 (uint8_t *)(bar0_ptr + NC_BL_BRIGHTNESS_OFFSET);
131 uint8_t *bl_pwm_ptr = (uint8_t *)(bar0_ptr + NC_BL_PWM_OFFSET);
132 FPGA_SET_PARAM(BL_Brightness, *bl_bn_ptr);
133 FPGA_SET_PARAM(PF_PwmFreq, *bl_pwm_ptr);
134 }
Werner Zeh6c571462016-07-05 07:16:34 +0200135}
136
137static struct device_operations nc_fpga_ops = {
138 .read_resources = pci_dev_read_resources,
139 .set_resources = pci_dev_set_resources,
140 .enable_resources = pci_dev_enable_resources,
141 .init = nc_fpga_init,
142 .scan_bus = 0,
143 .ops_pci = 0,
144};
145
Mario Scheithauerc4ff1de2017-06-12 10:02:10 +0200146static const unsigned short nc_fpga_device_ids[] = { 0x4080, 0x4091, 0 };
Werner Zeh6c571462016-07-05 07:16:34 +0200147
148static const struct pci_driver nc_fpga_driver __pci_driver = {
149 .ops = &nc_fpga_ops,
Mario Scheithauerc4ff1de2017-06-12 10:02:10 +0200150 .vendor = PCI_VENDOR_ID_SIEMENS,
Werner Zeh6c571462016-07-05 07:16:34 +0200151 .devices = nc_fpga_device_ids,
152};