Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2016 Siemens AG. |
| 5 | * |
| 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 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))) \ |
| 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; |
| 56 | } |
| 57 | |
| 58 | static void init_fan_ctrl (void *base_adr) |
| 59 | { |
Werner Zeh | 89a7b6b | 2017-06-30 10:38:08 +0200 | [diff] [blame] | 60 | uint8_t mask = 0, freeze_disable = 0, fan_req = 0; |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 61 | volatile fan_ctrl_t *ctrl = (fan_ctrl_t *)base_adr; |
| 62 | |
| 63 | /* Program all needed fields of FAN controller. */ |
| 64 | FPGA_SET_PARAM(FANSensorSelect, ctrl->sensorselect); |
| 65 | FPGA_SET_PARAM(T_Warn, ctrl->t_warn); |
| 66 | FPGA_SET_PARAM(T_Crit, ctrl->t_crit); |
| 67 | FPGA_SET_PARAM(FANSamplingTime, ctrl->samplingtime); |
| 68 | FPGA_SET_PARAM(FANSetPoint, ctrl->setpoint); |
| 69 | FPGA_SET_PARAM(FANHystCtrl, ctrl->hystctrl); |
| 70 | FPGA_SET_PARAM(FANHystVal, ctrl->hystval); |
| 71 | FPGA_SET_PARAM(FANHystThreshold, ctrl->hystthreshold); |
| 72 | FPGA_SET_PARAM(FANKp, ctrl->kp); |
| 73 | FPGA_SET_PARAM(FANKi, ctrl->ki); |
| 74 | FPGA_SET_PARAM(FANKd, ctrl->kd); |
| 75 | FPGA_SET_PARAM(FANMaxSpeed, ctrl->fanmax); |
| 76 | /* Set freeze and FAN configuration. */ |
| 77 | if ((hwilib_get_field(FF_FanReq, &fan_req, 1) == 1) && |
Werner Zeh | 89a7b6b | 2017-06-30 10:38:08 +0200 | [diff] [blame] | 78 | (hwilib_get_field(FF_FreezeDis, &freeze_disable, 1) == 1)) { |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 79 | if (!fan_req) |
| 80 | mask = 1; |
Werner Zeh | 89a7b6b | 2017-06-30 10:38:08 +0200 | [diff] [blame] | 81 | else if (fan_req && !freeze_disable) |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 82 | mask = 2; |
| 83 | else |
| 84 | mask = 3; |
| 85 | ctrl->fanmon = mask << 10; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** \brief This function is the driver entry point for the init phase |
| 90 | * of the PCI bus allocator. It will initialize all the needed parts |
| 91 | * of NC_FPGA. |
| 92 | * @param *dev Pointer to the used PCI device |
| 93 | * @return void Nothing is given back |
| 94 | */ |
| 95 | static void nc_fpga_init(struct device *dev) |
| 96 | { |
| 97 | void *bar0_ptr = NULL; |
| 98 | uint8_t cmd_reg; |
| 99 | uint32_t cap = 0; |
| 100 | |
| 101 | /* All we need is mapped to BAR 0, get the address. */ |
| 102 | bar0_ptr = (void *)(pci_read_config32(dev, PCI_BASE_ADDRESS_0) & |
| 103 | ~PCI_BASE_ADDRESS_MEM_ATTR_MASK); |
| 104 | cmd_reg = pci_read_config8(dev, PCI_COMMAND); |
| 105 | /* Ensure BAR0 has a valid value. */ |
| 106 | if (!bar0_ptr || !(cmd_reg & PCI_COMMAND_MEMORY)) |
| 107 | return; |
| 108 | /* Ensure this is really a NC FPGA by checking magic register. */ |
| 109 | if (read32(bar0_ptr + NC_MAGIC_OFFSET) != NC_FPGA_MAGIC) |
| 110 | return; |
| 111 | /* Open hwinfo block. */ |
| 112 | if (hwilib_find_blocks("hwinfo.hex") != CB_SUCCESS) |
| 113 | return; |
| 114 | /* Set up FAN controller and temperature monitor according to */ |
| 115 | /* capability bits. */ |
| 116 | cap = read32(bar0_ptr + NC_CAP1_OFFSET); |
| 117 | if (cap & (NC_CAP1_TEMP_MON | NC_CAP1_FAN_CTRL)) |
| 118 | init_temp_mon(bar0_ptr + NC_FANMON_CTRL_OFFSET); |
| 119 | if (cap & NC_CAP1_FAN_CTRL) |
| 120 | init_fan_ctrl(bar0_ptr + NC_FANMON_CTRL_OFFSET); |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 121 | if (cap & NC_CAP1_DSAVE_NMI_DELAY) { |
| 122 | uint16_t *dsave_ptr = (uint16_t *)(bar0_ptr + NC_DSAVE_OFFSET); |
| 123 | FPGA_SET_PARAM(NvramVirtTimeDsaveReset, *dsave_ptr); |
| 124 | } |
| 125 | if (cap & NC_CAP1_BL_BRIGHTNESS_CTRL) { |
| 126 | uint8_t *bl_bn_ptr = |
| 127 | (uint8_t *)(bar0_ptr + NC_BL_BRIGHTNESS_OFFSET); |
| 128 | uint8_t *bl_pwm_ptr = (uint8_t *)(bar0_ptr + NC_BL_PWM_OFFSET); |
| 129 | FPGA_SET_PARAM(BL_Brightness, *bl_bn_ptr); |
| 130 | FPGA_SET_PARAM(PF_PwmFreq, *bl_pwm_ptr); |
| 131 | } |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static struct device_operations nc_fpga_ops = { |
| 135 | .read_resources = pci_dev_read_resources, |
| 136 | .set_resources = pci_dev_set_resources, |
| 137 | .enable_resources = pci_dev_enable_resources, |
| 138 | .init = nc_fpga_init, |
| 139 | .scan_bus = 0, |
| 140 | .ops_pci = 0, |
| 141 | }; |
| 142 | |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 143 | static const unsigned short nc_fpga_device_ids[] = { 0x4080, 0x4091, 0 }; |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 144 | |
| 145 | static const struct pci_driver nc_fpga_driver __pci_driver = { |
| 146 | .ops = &nc_fpga_ops, |
Mario Scheithauer | c4ff1de | 2017-06-12 10:02:10 +0200 | [diff] [blame] | 147 | .vendor = PCI_VENDOR_ID_SIEMENS, |
Werner Zeh | 6c57146 | 2016-07-05 07:16:34 +0200 | [diff] [blame] | 148 | .devices = nc_fpga_device_ids, |
| 149 | }; |