1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <console/console.h>
4 #include <device/pnp.h>
5 #include <device/device.h>
6 #include <superio/conf_mode.h>
11 static void set_irq_trigger_type(struct device
*dev
, bool trig_level
)
13 u8 reg10
, reg11
, reg26
;
15 //Before accessing CR10 OR CR11 Bit 4 in CR26 must be set to 1
16 reg26
= pnp_read_config(dev
, GLOBAL_OPTION_CR26
);
17 reg26
|= CR26_LOCK_REG
;
18 pnp_write_config(dev
, GLOBAL_OPTION_CR26
, reg26
);
20 switch (dev
->path
.pnp
.device
) {
21 //SP1 (UARTA) IRQ type selection (1:level,0:edge) is controlled by CR 10, bit 5
23 reg10
= pnp_read_config(dev
, IRQ_TYPE_SEL_CR10
);
28 pnp_write_config(dev
, IRQ_TYPE_SEL_CR10
, reg10
);
30 //SP2 (UARTB) IRQ type selection (1:level,0:edge) is controlled by CR 10, bit 4
32 reg10
= pnp_read_config(dev
, IRQ_TYPE_SEL_CR10
);
37 pnp_write_config(dev
, IRQ_TYPE_SEL_CR10
, reg10
);
39 //SP3 (UARTC) IRQ type selection (1:level,0:edge) is controlled by CR 11, bit 5
41 reg11
= pnp_read_config(dev
,IRQ_TYPE_SEL_CR11
);
46 pnp_write_config(dev
, IRQ_TYPE_SEL_CR11
, reg11
);
48 //SP4 (UARTD) IRQ type selection (1:level,0:edge) is controlled by CR 11, bit 4
50 reg11
= pnp_read_config(dev
,IRQ_TYPE_SEL_CR11
);
55 pnp_write_config(dev
, IRQ_TYPE_SEL_CR11
, reg11
);
61 //Clear access control register
62 reg26
= pnp_read_config(dev
, GLOBAL_OPTION_CR26
);
63 reg26
&= ~CR26_LOCK_REG
;
64 pnp_write_config(dev
, GLOBAL_OPTION_CR26
, reg26
);
67 static void route_pins_to_uart(struct device
*dev
, bool to_uart
)
71 reg
= pnp_read_config(dev
, 0x1c);
73 switch (dev
->path
.pnp
.device
) {
76 /* Route pins 33 - 40. */
84 /* Route pins 41 - 48. */
94 pnp_write_config(dev
, 0x1c, reg
);
97 static void reset_gpio_default_in(struct device
*dev
)
99 pnp_set_logical_device(dev
);
101 * Soft reset GPIOs to default state: IN.
102 * The main GPIO LDN holds registers that configure the pins as output
103 * or input. These registers are located at offset 0xE0 plus the GPIO
104 * bank number multiplied by 4: 0xE0 for GPIO0, 0xE4 for GPIO1 and
107 pnp_write_config(dev
, NCT5104D_GPIO0_IO
+ (dev
->path
.pnp
.device
>> 8) * 4, 0xFF);
110 static void reset_gpio_default_od(struct device
*dev
)
112 struct device
*gpio0
, *gpio1
, *gpio6
;
114 pnp_set_logical_device(dev
);
116 gpio0
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO0
);
117 gpio1
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO1
);
118 gpio6
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO6
);
121 * Soft reset GPIOs to default state: Open-drain.
122 * The NCT5104D_GPIO_PP_OD LDN holds registers (1 for each GPIO bank)
123 * that configure each GPIO pin to be open dain or push pull. System
124 * reset is known to not reset the values in this register. The
125 * registers are located at offsets begginign from 0xE0 plus GPIO bank
126 * number, i.e. 0xE0 for GPIO0, 0xE1 for GPIO1 and 0xE6 for GPIO6.
128 if (gpio0
&& gpio0
->enabled
)
129 pnp_write_config(dev
,
130 (gpio0
->path
.pnp
.device
>> 8) + NCT5104D_GPIO0_PP_OD
, 0xFF);
132 if (gpio1
&& gpio1
->enabled
)
133 pnp_write_config(dev
,
134 (gpio1
->path
.pnp
.device
>> 8) + NCT5104D_GPIO0_PP_OD
, 0xFF);
136 if (gpio6
&& gpio6
->enabled
)
137 pnp_write_config(dev
,
138 (gpio6
->path
.pnp
.device
>> 8) + NCT5104D_GPIO0_PP_OD
, 0xFF);
141 static void disable_gpio_io_port(struct device
*dev
)
143 struct device
*gpio0
, *gpio1
, *gpio6
;
146 * Since UARTC and UARTD share pins with GPIO0 and GPIO1 and the
147 * GPIO/UART can be selected via Kconfig, check whether at least one of
148 * GPIOs is enabled and if yes keep the GPIO IO VLDN enabled. If no
149 * GPIOs are enabled, disable the VLDN in order to protect from invalid
150 * devicetree + Kconfig settings.
152 gpio0
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO0
);
153 gpio1
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO1
);
154 gpio6
= dev_find_slot_pnp(dev
->path
.pnp
.port
, NCT5104D_GPIO6
);
156 if (!((gpio0
&& gpio0
->enabled
) || (gpio1
&& gpio1
->enabled
) ||
157 (gpio6
&& gpio6
->enabled
))) {
159 printk(BIOS_WARNING
, "GPIO IO port configured,"
160 " but no GPIO enabled. Disabling...");
164 static void nct5104d_init(struct device
*dev
)
166 struct superio_nuvoton_nct5104d_config
*conf
= dev
->chip_info
;
171 pnp_enter_conf_mode(dev
);
173 switch (dev
->path
.pnp
.device
) {
176 set_irq_trigger_type(dev
, conf
->irq_trigger_type
!= 0);
180 route_pins_to_uart(dev
, true);
181 set_irq_trigger_type(dev
, conf
->irq_trigger_type
!= 0);
185 route_pins_to_uart(dev
, false);
188 if (conf
->reset_gpios
)
189 reset_gpio_default_in(dev
);
191 case NCT5104D_GPIO_PP_OD
:
192 if (conf
->reset_gpios
)
193 reset_gpio_default_od(dev
);
195 case NCT5104D_GPIO_IO
:
196 disable_gpio_io_port(dev
);
202 pnp_exit_conf_mode(dev
);
205 static struct device_operations ops
= {
206 .read_resources
= pnp_read_resources
,
207 .set_resources
= pnp_set_resources
,
208 .enable_resources
= pnp_enable_resources
,
209 .enable
= pnp_alt_enable
,
210 .init
= nct5104d_init
,
211 .ops_pnp_mode
= &pnp_conf_mode_8787_aa
,
214 static struct pnp_info pnp_dev_info
[] = {
215 { NULL
, NCT5104D_FDC
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
216 { NULL
, NCT5104D_SP1
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
217 { NULL
, NCT5104D_SP2
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
218 { NULL
, NCT5104D_SP3
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
219 { NULL
, NCT5104D_SP4
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
220 { NULL
, NCT5104D_GPIO_WDT
},
221 { NULL
, NCT5104D_GPIO0
},
222 { NULL
, NCT5104D_GPIO1
},
223 { NULL
, NCT5104D_GPIO6
},
224 { NULL
, NCT5104D_GPIO_PP_OD
},
225 { NULL
, NCT5104D_GPIO_IO
, PNP_IO0
, 0x07f8, },
226 { NULL
, NCT5104D_PORT80
},
229 static void enable_dev(struct device
*dev
)
231 pnp_enable_devices(dev
, &ops
, ARRAY_SIZE(pnp_dev_info
), pnp_dev_info
);
234 struct chip_operations superio_nuvoton_nct5104d_ops
= {
235 .name
= "Nuvoton NCT5104D Super I/O",
236 .enable_dev
= enable_dev
,