Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <types.h> |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 15 | #include <device/pci.h> |
| 16 | #include <device/pci_ids.h> |
| 17 | #include <device/pci_ops.h> |
| 18 | #include <device/pci_def.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 19 | #include <device/mmio.h> |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 20 | #include <hwilib.h> |
Werner Zeh | f1f67c3 | 2017-10-19 07:21:54 +0200 | [diff] [blame] | 21 | #include <bootstate.h> |
Elyes HAOUAS | bf0970e | 2019-03-21 11:10:03 +0100 | [diff] [blame] | 22 | |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 23 | #include "nc_fpga.h" |
| 24 | |
Werner Zeh | f1f67c3 | 2017-10-19 07:21:54 +0200 | [diff] [blame] | 25 | static void *nc_fpga_bar0; |
| 26 | |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 27 | #define FPGA_SET_PARAM(src, dst) \ |
| 28 | { \ |
Werner Zeh | 2db7922 | 2017-07-05 15:52:52 +0200 | [diff] [blame] | 29 | uint32_t var; \ |
| 30 | if (hwilib_get_field(src, (uint8_t *)&var, sizeof(var))) \ |
Werner Zeh | d5960c4 | 2017-07-14 10:24:00 +0200 | [diff] [blame] | 31 | dst = ((typeof(dst))var); \ |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static 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 Zeh | 2db7922 | 2017-07-05 15:52:52 +0200 | [diff] [blame] | 43 | if ((hwilib_get_field(FANSensorNum, &num, 1) != 1) || |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 44 | (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 Scheithauer | 0b42c8a | 2017-09-26 13:37:49 +0200 | [diff] [blame] | 56 | |
| 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 Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void init_fan_ctrl (void *base_adr) |
| 64 | { |
Werner Zeh | 89a7b6b | 2017-06-30 10:38:08 +0200 | [diff] [blame] | 65 | uint8_t mask = 0, freeze_disable = 0, fan_req = 0; |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 66 | volatile fan_ctrl_t *ctrl = (fan_ctrl_t *)base_adr; |
| 67 | |
| 68 | /* Program all needed fields of FAN controller. */ |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 69 | 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 Zeh | c38ab85 | 2017-07-27 13:48:18 +0200 | [diff] [blame] | 78 | FPGA_SET_PARAM(FANStartSpeed, ctrl->fanmin); |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 79 | /* Set freeze and FAN configuration. */ |
| 80 | if ((hwilib_get_field(FF_FanReq, &fan_req, 1) == 1) && |
Werner Zeh | 89a7b6b | 2017-06-30 10:38:08 +0200 | [diff] [blame] | 81 | (hwilib_get_field(FF_FreezeDis, &freeze_disable, 1) == 1)) { |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 82 | if (!fan_req) |
| 83 | mask = 1; |
Elyes HAOUAS | 1943f37 | 2018-05-04 16:30:39 +0200 | [diff] [blame] | 84 | else if (fan_req && !freeze_disable) |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 85 | 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 | */ |
| 98 | static 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; |
Werner Zeh | f1f67c3 | 2017-10-19 07:21:54 +0200 | [diff] [blame] | 114 | /* Save BAR0 address so that it can be used on all NC_FPGA devices to |
| 115 | set the FW_DONE bit before jumping to payload. */ |
| 116 | nc_fpga_bar0 = bar0_ptr; |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 117 | /* Open hwinfo block. */ |
| 118 | if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS) |
| 119 | return; |
| 120 | /* Set up FAN controller and temperature monitor according to */ |
| 121 | /* capability bits. */ |
| 122 | cap = read32(bar0_ptr + NC_CAP1_OFFSET); |
| 123 | if (cap & (NC_CAP1_TEMP_MON | NC_CAP1_FAN_CTRL)) |
| 124 | init_temp_mon(bar0_ptr + NC_FANMON_CTRL_OFFSET); |
| 125 | if (cap & NC_CAP1_FAN_CTRL) |
| 126 | init_fan_ctrl(bar0_ptr + NC_FANMON_CTRL_OFFSET); |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 127 | if (cap & NC_CAP1_DSAVE_NMI_DELAY) { |
| 128 | uint16_t *dsave_ptr = (uint16_t *)(bar0_ptr + NC_DSAVE_OFFSET); |
| 129 | FPGA_SET_PARAM(NvramVirtTimeDsaveReset, *dsave_ptr); |
| 130 | } |
| 131 | if (cap & NC_CAP1_BL_BRIGHTNESS_CTRL) { |
| 132 | uint8_t *bl_bn_ptr = |
| 133 | (uint8_t *)(bar0_ptr + NC_BL_BRIGHTNESS_OFFSET); |
| 134 | uint8_t *bl_pwm_ptr = (uint8_t *)(bar0_ptr + NC_BL_PWM_OFFSET); |
| 135 | FPGA_SET_PARAM(BL_Brightness, *bl_bn_ptr); |
| 136 | FPGA_SET_PARAM(PF_PwmFreq, *bl_pwm_ptr); |
| 137 | } |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 140 | #if CONFIG(NC_FPGA_NOTIFY_CB_READY) |
Werner Zeh | f1f67c3 | 2017-10-19 07:21:54 +0200 | [diff] [blame] | 141 | /* Set FW_DONE bit in FPGA before jumping to payload. */ |
| 142 | static void set_fw_done(void *unused) |
| 143 | { |
| 144 | uint32_t reg; |
| 145 | |
| 146 | if (nc_fpga_bar0) { |
| 147 | reg = read32(nc_fpga_bar0 + NC_DIAG_CTRL_OFFSET); |
| 148 | reg |= NC_DIAG_FW_DONE; |
| 149 | write32(nc_fpga_bar0 + NC_DIAG_CTRL_OFFSET, reg); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, set_fw_done, NULL); |
| 154 | #endif |
| 155 | |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 156 | static struct device_operations nc_fpga_ops = { |
| 157 | .read_resources = pci_dev_read_resources, |
| 158 | .set_resources = pci_dev_set_resources, |
| 159 | .enable_resources = pci_dev_enable_resources, |
| 160 | .init = nc_fpga_init, |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 163 | static const unsigned short nc_fpga_device_ids[] = { 0x4080, 0x4091, 0 }; |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 164 | |
| 165 | static const struct pci_driver nc_fpga_driver __pci_driver = { |
| 166 | .ops = &nc_fpga_ops, |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 167 | .vendor = PCI_VENDOR_ID_SIEMENS, |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 168 | .devices = nc_fpga_device_ids, |
| 169 | }; |