Debian Codegraph @

src/mainboard/asrock/imb-1222/superio.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <device/pnp.h>
4 #include <mainboard/superio.h>
5 #include <superio/hwm5_conf.h>
6 #include <superio/fintek/common/fintek.h>
7
8 /* TODO: move this code to superio/fintek driver */
9 #define SIO_PORT 0x2e
10 #define SIO_HWM_BASE 0x290
11 #define SIO_DEV(n) PNP_DEV(SIO_PORT, SIO_LDN_##n)
12
13 /* Fintek F81966 Logical Device Numbers (LDN) */
14 #define SIO_LDN_GLOBAL 0x00
15 #define SIO_LDN_LPT 0x03
16 #define SIO_LDN_HWMON 0x04
17 #define SIO_LDN_KBC 0x05
18 #define SIO_LDN_GPIO 0x06
19 #define SIO_LDN_WDT 0x07
20 #define SIO_LDN_PME_ACPI_ERP 0x0a
21 #define SIO_LDN_SPI 0x0f
22 #define SIO_LDN_UART1 0x10
23 #define SIO_LDN_UART2 0x11
24 #define SIO_LDN_UART3 0x12
25 #define SIO_LDN_UART4 0x13
26 #define SIO_LDN_UART5 0x14
27 #define SIO_LDN_UART6 0x15
28
29 #define SIO_REG(i, v) {.idx = i, .val = v}
30 #define SIO_ESEL(i, v, m) {.extra_selector = true, .idx = i, .val = v, .mask = m}
31 #define SIO_LDN(n) {.idx = 0x07, .val = SIO_LDN_##n}
32 #define SIO_LDN_EN(v) {.idx = 0x30, .val = v}
33 /* f81966 Port Select (27h): BANK_PROG_SEL[3-2] CLK_TUNE_PROG_EN[0] */
34 #define SIO_ESEL_27H(bank, en) { \
35 .extra_selector = true, \
36 .idx = 0x27, \
37 .mask = 0xf2, \
38 .val = esel_27h_##bank | esel_27h_clk_tune_##en, \
39 }
40 /* f81966 Fan Fault Time Register (9Fh): FAN_PROG_SEL[7] */
41 #define SIO_ESEL_9FH(bank) { \
42 .extra_selector = true, \
43 .idx = 0x9f, \
44 .mask = 0x7f, \
45 .val = esel_9fh_fan_prog_##bank, \
46 }
47 /* PWM duty-cycle */
48 #define PWM_DC(x) (((x << 8) - x) / 100)
49
50 enum esel_27h_bank_prog_sel {
51 esel_27h_bank0 = 0 << 2,
52 esel_27h_bank1 = 1 << 2,
53 esel_27h_bank2 = 2 << 2,
54 esel_27h_bank3 = 3 << 2,
55 };
56 enum esel_27h_clk_tune {
57 esel_27h_clk_tune_dis = 0,
58 esel_27h_clk_tune_en = 1,
59 };
60
61 enum esel_9fh_fan_prog {
62 esel_9fh_fan_prog_bank0 = 0 << 7,
63 esel_9fh_fan_prog_bank1 = 1 << 7,
64 };
65
66 struct sio_reg {
67 bool extra_selector;
68 uint8_t idx;
69 uint8_t val;
70 uint8_t mask;
71 };
72
73 static const struct sio_reg ldn_reg_tbl[] = {
74 SIO_LDN(GLOBAL),
75 SIO_REG(0x27, 0x80), /* disable 0x80 port */
76 SIO_REG(0x2d, 0x2e), /* enable KB/Mouse wakeup, 2.8/2.5V hysteresis */
77 SIO_ESEL_27H(bank0, dis),
78 SIO_REG(0x29, 0xf0), /* set UART fn pins, TTL level */
79 SIO_REG(0x2a, 0x45), /* GPIO[10]->LED_VSB, GPIO[11]->LED_VCC */
80 SIO_REG(0x2c, 0xe3), /* en GPIO 00,01 */
81 SIO_ESEL_27H(bank2, dis),
82 SIO_REG(0x2b, 0x03), /* select pin 54 as SCL */
83 SIO_REG(0x2c, 0x01), /* GPIO[20]->ALERT# */
84 SIO_ESEL_27H(bank3, dis),
85 SIO_REG(0x28, 0x59), /* PLL_CNT = (256*48) / 89 = 138 */
86 SIO_ESEL_27H(bank0, en),
87 /*
88 * - GPIO[90]->LDRQ#, GPIO[91]->KBRST#, GPIO[92]->GA20, GPIO[93]->MDATA,
89 * - GPIO[94]->MCLK, GPIO[95]->FANIN1, GPIO[96]->FANIN2, GPIO[97]->SLCT
90 */
91 SIO_REG(0x2c, 0x00),
92 SIO_ESEL_27H(bank0, dis),
93
94 SIO_LDN(GPIO),
95 SIO_REG(0x60, 0x02),
96 SIO_REG(0x61, 0x80), /* base address */
97 SIO_REG(0xe0, 0x10), /* GPIO[14]->output mode */
98 SIO_REG(0xe3, 0x10), /* GPIO[14]->is push pull in output mode */
99 SIO_REG(0xe6, 0x10), /* GPIO[14]->SMI event will set if input is changed */
100 SIO_REG(0x80, 0xe0), /* GPIO[75,76,77]->is in output mode */
101 SIO_REG(0x81, 0xe0), /* GPIO[75,76,77]=1 */
102 SIO_REG(0x88, 0x01), /* GPIO[80]->is in output mode */
103 SIO_REG(0x89, 0x01), /* GPIO[80]=1 */
104 SIO_REG(0x8e, 0xff), /* GPIO[80,81,82,83,84,85,86,87]->use SMI event */
105 SIO_LDN_EN(0x01), /* enable GPIO I/O ports */
106
107 SIO_LDN(HWMON),
108 SIO_REG(0x60, SIO_HWM_BASE >> 8),
109 SIO_REG(0x61, SIO_HWM_BASE & 0xff),
110 SIO_LDN_EN(0x01), /* enable hardware monitor */
111
112 SIO_LDN(UART1),
113 SIO_REG(0xf6, 0x23), /* 128-byte FIFO, FIFO threshold will be 4X of RXFTHR */
114 SIO_LDN_EN(0x01),
115
116 SIO_LDN(UART2),
117 SIO_REG(0xf6, 0x23),
118 SIO_LDN_EN(0x01),
119
120 SIO_LDN(UART3),
121 SIO_REG(0xf6, 0x23),
122 SIO_LDN_EN(0x01),
123
124 SIO_LDN(UART4),
125 SIO_REG(0xf6, 0x23),
126 SIO_LDN_EN(0x01),
127 };
128
129 static const struct sio_reg hwm_reg_tbl[] = {
130 SIO_REG(0x07, 0x4a), /* MXM address */
131 SIO_REG(0x0a, 0x01), /* enable PECI access */
132 SIO_REG(0x0c, 0x64), /* TCC Temperature : CPU_TEMP = TCC_TEMP + PECI Reading */
133 SIO_REG(0x0f, 0x20), /* digital rate selector (Reserved for Fintek use only) */
134 SIO_REG(0x6b, 0x00), /* TEMP[1,2] is connected to a thermistor */
135 SIO_ESEL_9FH(bank0),
136 /*
137 * - FAN[1]->open drain, output PWM mode to control Intel 4-wire fans;
138 * - FAN[2]->push pull, output PWM mode to control fans;
139 * - FAN[3]->use linear fan application circuit to control speed by power terminal
140 */
141 SIO_REG(0x94, 0x12),
142 SIO_REG(0x9b, 0x1f), /* FAN[1,2]->duty update rate 20Hz */
143 SIO_ESEL_9FH(bank1),
144 SIO_REG(0x9b, 0x55), /* direct load enable for manual duty mode */
145 SIO_ESEL_9FH(bank0),
146
147 SIO_REG(0xa3, 0x75), /* FAN[1] expect PWM duty */
148 SIO_REG(0xa6, 70), /* VT[1] boundary 1 temperature */
149 SIO_REG(0xa7, 65), /* VT[1] boundary 2 temperature */
150 SIO_REG(0xa8, 55), /* VT[1] boundary 3 temperature */
151 SIO_REG(0xa9, 45), /* VT[1] boundary 4 temperature */
152 SIO_REG(0xaa, PWM_DC(100)), /* FAN[1] segment 1 speed count */
153 SIO_REG(0xab, PWM_DC(85)), /* FAN[1] segment 2 speed count */
154 SIO_REG(0xac, PWM_DC(70)), /* FAN[1] segment 3 speed count */
155 SIO_REG(0xad, PWM_DC(60)), /* FAN[1] segment 4 speed count */
156 SIO_REG(0xae, PWM_DC(50)), /* FAN[1] segment 5 speed count */
157 /*
158 * - FAN[1] follows PECI temperature;
159 * - FAN[1] duty will directly jump to the value of FAN1_SEG2;
160 * - FAN[1] duty will directly jump to the value of FAN1_SEG1;
161 * - enable the interpolation of the fan expect table;
162 * - [0,0,0]: 23.5 KHz;
163 */
164 SIO_REG(0xaf, 0x1c),
165 SIO_REG(0xb3, 0x75), /* FAN[2] expect PWM duty */
166 SIO_REG(0xb6, 70), /* VT[2] boundary 1 temperature */
167 SIO_REG(0xb7, 65), /* VT[2] boundary 2 temperature */
168 SIO_REG(0xb8, 55), /* VT[2] boundary 3 temperature */
169 SIO_REG(0xb9, 45), /* VT[2] boundary 4 temperature */
170 SIO_REG(0xba, PWM_DC(100)), /* FAN[2] segment 1 speed count 0xff */
171 SIO_REG(0xbb, PWM_DC(85)), /* FAN[2] segment 2 speed count 0xd9 */
172 SIO_REG(0xbc, PWM_DC(70)), /* FAN[2] segment 3 speed count 0xb2 */
173 SIO_REG(0xbd, PWM_DC(60)), /* FAN[2] segment 4 speed count 0x99 */
174 SIO_REG(0xbe, PWM_DC(50)), /* FAN[2] segment 5 speed count 0x75 */
175 /*
176 * - FAN[2] follows PECI temperature;
177 * - FAN[2] duty will directly jump to the value of FAN2_SEG2;
178 * - FAN[2] duty will directly jump to the value of FAN2_SEG1;
179 * - enable the interpolation of the fan expect table;
180 * - [0,0,0]: 23.5 KHz;
181 */
182 SIO_REG(0xbf, 0x1c),
183 };
184
185 static inline u8 sio_read(bool is_hwm, uint8_t idx)
186 {
187 return is_hwm ? pnp_read_hwm5_index(SIO_HWM_BASE, idx) : pnp_read_index(SIO_PORT, idx);
188 }
189
190 static inline void sio_write(bool is_hwm, uint8_t idx, uint8_t val)
191 {
192 is_hwm ? pnp_write_hwm5_index(SIO_HWM_BASE, idx, val) : pnp_write_index(SIO_PORT, idx, val);
193 }
194
195 static void sio_regs_setup(const struct sio_reg reg[], int size, bool is_hwm)
196 {
197 for (int i = 0; i < size; i++) {
198 uint8_t val = reg[i].val;
199 if (reg[i].extra_selector) {
200 val = sio_read(is_hwm, reg[i].idx);
201 val &= reg[i].mask;
202 val |= reg[i].val;
203 }
204 sio_write(is_hwm, reg[i].idx, val);
205 }
206 }
207
208 void mainboard_superio_init(void)
209 {
210 pnp_enter_conf_state(SIO_DEV(GLOBAL));
211 sio_regs_setup(ldn_reg_tbl, sizeof(ldn_reg_tbl)/sizeof(struct sio_reg), false);
212 pnp_exit_conf_state(SIO_DEV(GLOBAL));
213
214 sio_regs_setup(hwm_reg_tbl, sizeof(hwm_reg_tbl)/sizeof(struct sio_reg), true);
215 }