1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /* RAM-based driver for SMSC LPC47N227 Super I/O chip. */
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <console/console.h>
9 #include <pc80/keyboard.h>
10 #include <superio/conf_mode.h>
12 #include "lpc47n227.h"
14 /* Forward declarations. */
15 static void enable_dev(struct device
*dev
);
16 void lpc47n227_pnp_set_resources(struct device
*dev
);
17 void lpc47n227_pnp_enable_resources(struct device
*dev
);
18 void lpc47n227_pnp_enable(struct device
*dev
);
19 static void lpc47n227_init(struct device
*dev
);
20 static void lpc47n227_pnp_set_resource(struct device
*dev
, struct resource
*resource
);
21 void lpc47n227_pnp_set_iobase(struct device
*dev
, u16 iobase
);
22 void lpc47n227_pnp_set_drq(struct device
*dev
, u8 drq
);
23 void lpc47n227_pnp_set_irq(struct device
*dev
, u8 irq
);
24 void lpc47n227_pnp_set_enable(struct device
*dev
, int enable
);
26 struct chip_operations superio_smsc_lpc47n227_ops
= {
27 .name
= "SMSC LPC47N227 Super I/O",
28 .enable_dev
= enable_dev
,
31 static struct device_operations ops
= {
32 .read_resources
= pnp_read_resources
,
33 .set_resources
= lpc47n227_pnp_set_resources
,
34 .enable_resources
= lpc47n227_pnp_enable_resources
,
35 .enable
= lpc47n227_pnp_enable
,
36 .init
= lpc47n227_init
,
37 .ops_pnp_mode
= &pnp_conf_mode_55_aa
,
40 static struct pnp_info pnp_dev_info
[] = {
41 { NULL
, LPC47N227_PP
, PNP_IO0
| PNP_IRQ0
| PNP_DRQ0
, 0x07f8, },
42 { NULL
, LPC47N227_SP1
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
43 { NULL
, LPC47N227_SP2
, PNP_IO0
| PNP_IRQ0
, 0x07f8, },
44 { NULL
, LPC47N227_KBDC
, PNP_IO0
| PNP_IO1
| PNP_IRQ0
, 0x07f8, 0x07f8, },
48 * Create device structures and allocate resources to devices specified in the
49 * pnp_dev_info array (above).
51 * @param dev Pointer to structure describing a Super I/O device.
53 static void enable_dev(struct device
*dev
)
55 pnp_enable_devices(dev
, &ops
, ARRAY_SIZE(pnp_dev_info
), pnp_dev_info
);
59 * Configure the specified Super I/O device with the resources (I/O space,
60 * etc.) that have been allocate for it.
62 * NOTE: Cannot use pnp_set_resources() here because it assumes chip
63 * support for logical devices, which the LPC47N227 doesn't have.
65 * @param dev Pointer to structure describing a Super I/O device.
67 void lpc47n227_pnp_set_resources(struct device
*dev
)
71 pnp_enter_conf_mode(dev
);
72 for (res
= dev
->resource_list
; res
; res
= res
->next
)
73 lpc47n227_pnp_set_resource(dev
, res
);
74 pnp_exit_conf_mode(dev
);
78 * NOTE: Cannot use pnp_enable_resources() here because it assumes chip
79 * support for logical devices, which the LPC47N227 doesn't have.
81 void lpc47n227_pnp_enable_resources(struct device
*dev
)
83 pnp_enter_conf_mode(dev
);
84 lpc47n227_pnp_set_enable(dev
, 1);
85 pnp_exit_conf_mode(dev
);
89 * NOTE: Cannot use pnp_set_enable() here because it assumes chip
90 * support for logical devices, which the LPC47N227 doesn't have.
92 void lpc47n227_pnp_enable(struct device
*dev
)
94 pnp_enter_conf_mode(dev
);
95 lpc47n227_pnp_set_enable(dev
, !!dev
->enabled
);
96 pnp_exit_conf_mode(dev
);
100 * Initialize the specified Super I/O device.
102 * Devices other than COM ports and keyboard controller are ignored.
103 * For COM ports, we configure the baud rate.
105 * @param dev Pointer to structure describing a Super I/O device.
107 static void lpc47n227_init(struct device
*dev
)
112 switch (dev
->path
.pnp
.device
) {
114 printk(BIOS_DEBUG
, "LPC47N227: Initializing keyboard.\n");
115 pc_keyboard_init(NO_AUX_DEVICE
);
120 static void lpc47n227_pnp_set_resource(struct device
*dev
, struct resource
*resource
)
122 if (!(resource
->flags
& IORESOURCE_ASSIGNED
)) {
123 printk(BIOS_ERR
, "%s %02lx not allocated\n",
124 dev_path(dev
), resource
->index
);
128 /* Now store the resource. */
130 * NOTE: Cannot use pnp_set_XXX() here because they assume chip
131 * support for logical devices, which the LPC47N227 doesn't have.
133 if (resource
->flags
& IORESOURCE_IO
) {
134 lpc47n227_pnp_set_iobase(dev
, resource
->base
);
135 } else if (resource
->flags
& IORESOURCE_DRQ
) {
136 lpc47n227_pnp_set_drq(dev
, resource
->base
);
137 } else if (resource
->flags
& IORESOURCE_IRQ
) {
138 lpc47n227_pnp_set_irq(dev
, resource
->base
);
140 printk(BIOS_ERR
, "%s %02lx unknown resource type\n",
141 dev_path(dev
), resource
->index
);
144 resource
->flags
|= IORESOURCE_STORED
;
146 report_resource_stored(dev
, resource
, "");
149 void lpc47n227_pnp_set_iobase(struct device
*dev
, u16 iobase
)
151 ASSERT(!(iobase
& 0x3));
153 switch (dev
->path
.pnp
.device
) {
155 pnp_write_config(dev
, 0x23, (iobase
>> 2) & 0xff);
158 pnp_write_config(dev
, 0x24, (iobase
>> 2) & 0xff);
161 pnp_write_config(dev
, 0x25, (iobase
>> 2) & 0xff);
171 void lpc47n227_pnp_set_drq(struct device
*dev
, u8 drq
)
173 const u8 PP_DMA_MASK
= 0x0F;
174 const u8 PP_DMA_SELECTION_REGISTER
= 0x26;
175 u8 current_config
, new_config
;
177 if (dev
->path
.pnp
.device
== LPC47N227_PP
) {
178 current_config
= pnp_read_config(dev
,
179 PP_DMA_SELECTION_REGISTER
);
180 ASSERT(!(drq
& ~PP_DMA_MASK
)); // DRQ out of range??
181 new_config
= (current_config
& ~PP_DMA_MASK
) | drq
;
182 pnp_write_config(dev
, PP_DMA_SELECTION_REGISTER
, new_config
);
188 void lpc47n227_pnp_set_irq(struct device
*dev
, u8 irq
)
190 u8 irq_config_register
= 0, irq_config_mask
= 0;
191 u8 current_config
, new_config
;
193 switch (dev
->path
.pnp
.device
) {
195 irq_config_register
= 0x27;
196 irq_config_mask
= 0x0F;
199 irq_config_register
= 0x28;
200 irq_config_mask
= 0xF0;
204 irq_config_register
= 0x28;
205 irq_config_mask
= 0x0F;
214 current_config
= pnp_read_config(dev
, irq_config_register
);
215 new_config
= (current_config
& ~irq_config_mask
) | irq
;
216 pnp_write_config(dev
, irq_config_register
, new_config
);
219 void lpc47n227_pnp_set_enable(struct device
*dev
, int enable
)
221 u8 power_register
= 0, power_mask
= 0, current_power
, new_power
;
223 switch (dev
->path
.pnp
.device
) {
225 power_register
= 0x01;
229 power_register
= 0x02;
233 power_register
= 0x02;
243 current_power
= pnp_read_config(dev
, power_register
);
244 new_power
= current_power
& ~power_mask
; /* Disable by default. */
246 struct resource
*ioport_resource
;
247 ioport_resource
= find_resource(dev
, PNP_IDX_IO0
);
248 lpc47n227_pnp_set_iobase(dev
, ioport_resource
->base
);
249 new_power
|= power_mask
; /* Enable. */
251 lpc47n227_pnp_set_iobase(dev
, 0);
253 pnp_write_config(dev
, power_register
, new_power
);