Debian Codegraph @

src/superio/smsc/lpc47n227/superio.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /* RAM-based driver for SMSC LPC47N227 Super I/O chip. */
4
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <console/console.h>
8 #include <assert.h>
9 #include <pc80/keyboard.h>
10 #include <superio/conf_mode.h>
11
12 #include "lpc47n227.h"
13
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);
25
26 struct chip_operations superio_smsc_lpc47n227_ops = {
27 .name = "SMSC LPC47N227 Super I/O",
28 .enable_dev = enable_dev,
29 };
30
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,
38 };
39
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, },
45 };
46
47 /**
48 * Create device structures and allocate resources to devices specified in the
49 * pnp_dev_info array (above).
50 *
51 * @param dev Pointer to structure describing a Super I/O device.
52 */
53 static void enable_dev(struct device *dev)
54 {
55 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
56 }
57
58 /**
59 * Configure the specified Super I/O device with the resources (I/O space,
60 * etc.) that have been allocate for it.
61 *
62 * NOTE: Cannot use pnp_set_resources() here because it assumes chip
63 * support for logical devices, which the LPC47N227 doesn't have.
64 *
65 * @param dev Pointer to structure describing a Super I/O device.
66 */
67 void lpc47n227_pnp_set_resources(struct device *dev)
68 {
69 struct resource *res;
70
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);
75 }
76
77 /*
78 * NOTE: Cannot use pnp_enable_resources() here because it assumes chip
79 * support for logical devices, which the LPC47N227 doesn't have.
80 */
81 void lpc47n227_pnp_enable_resources(struct device *dev)
82 {
83 pnp_enter_conf_mode(dev);
84 lpc47n227_pnp_set_enable(dev, 1);
85 pnp_exit_conf_mode(dev);
86 }
87
88 /*
89 * NOTE: Cannot use pnp_set_enable() here because it assumes chip
90 * support for logical devices, which the LPC47N227 doesn't have.
91 */
92 void lpc47n227_pnp_enable(struct device *dev)
93 {
94 pnp_enter_conf_mode(dev);
95 lpc47n227_pnp_set_enable(dev, !!dev->enabled);
96 pnp_exit_conf_mode(dev);
97 }
98
99 /**
100 * Initialize the specified Super I/O device.
101 *
102 * Devices other than COM ports and keyboard controller are ignored.
103 * For COM ports, we configure the baud rate.
104 *
105 * @param dev Pointer to structure describing a Super I/O device.
106 */
107 static void lpc47n227_init(struct device *dev)
108 {
109 if (!dev->enabled)
110 return;
111
112 switch (dev->path.pnp.device) {
113 case LPC47N227_KBDC:
114 printk(BIOS_DEBUG, "LPC47N227: Initializing keyboard.\n");
115 pc_keyboard_init(NO_AUX_DEVICE);
116 break;
117 }
118 }
119
120 static void lpc47n227_pnp_set_resource(struct device *dev, struct resource *resource)
121 {
122 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
123 printk(BIOS_ERR, "%s %02lx not allocated\n",
124 dev_path(dev), resource->index);
125 return;
126 }
127
128 /* Now store the resource. */
129 /*
130 * NOTE: Cannot use pnp_set_XXX() here because they assume chip
131 * support for logical devices, which the LPC47N227 doesn't have.
132 */
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);
139 } else {
140 printk(BIOS_ERR, "%s %02lx unknown resource type\n",
141 dev_path(dev), resource->index);
142 return;
143 }
144 resource->flags |= IORESOURCE_STORED;
145
146 report_resource_stored(dev, resource, "");
147 }
148
149 void lpc47n227_pnp_set_iobase(struct device *dev, u16 iobase)
150 {
151 ASSERT(!(iobase & 0x3));
152
153 switch (dev->path.pnp.device) {
154 case LPC47N227_PP:
155 pnp_write_config(dev, 0x23, (iobase >> 2) & 0xff);
156 break;
157 case LPC47N227_SP1:
158 pnp_write_config(dev, 0x24, (iobase >> 2) & 0xff);
159 break;
160 case LPC47N227_SP2:
161 pnp_write_config(dev, 0x25, (iobase >> 2) & 0xff);
162 break;
163 case LPC47N227_KBDC:
164 break;
165 default:
166 BUG();
167 break;
168 }
169 }
170
171 void lpc47n227_pnp_set_drq(struct device *dev, u8 drq)
172 {
173 const u8 PP_DMA_MASK = 0x0F;
174 const u8 PP_DMA_SELECTION_REGISTER = 0x26;
175 u8 current_config, new_config;
176
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);
183 } else {
184 BUG();
185 }
186 }
187
188 void lpc47n227_pnp_set_irq(struct device *dev, u8 irq)
189 {
190 u8 irq_config_register = 0, irq_config_mask = 0;
191 u8 current_config, new_config;
192
193 switch (dev->path.pnp.device) {
194 case LPC47N227_PP:
195 irq_config_register = 0x27;
196 irq_config_mask = 0x0F;
197 break;
198 case LPC47N227_SP1:
199 irq_config_register = 0x28;
200 irq_config_mask = 0xF0;
201 irq <<= 4;
202 break;
203 case LPC47N227_SP2:
204 irq_config_register = 0x28;
205 irq_config_mask = 0x0F;
206 break;
207 case LPC47N227_KBDC:
208 break;
209 default:
210 BUG();
211 return;
212 }
213
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);
217 }
218
219 void lpc47n227_pnp_set_enable(struct device *dev, int enable)
220 {
221 u8 power_register = 0, power_mask = 0, current_power, new_power;
222
223 switch (dev->path.pnp.device) {
224 case LPC47N227_PP:
225 power_register = 0x01;
226 power_mask = 0x04;
227 break;
228 case LPC47N227_SP1:
229 power_register = 0x02;
230 power_mask = 0x08;
231 break;
232 case LPC47N227_SP2:
233 power_register = 0x02;
234 power_mask = 0x80;
235 break;
236 case LPC47N227_KBDC:
237 break;
238 default:
239 BUG();
240 return;
241 }
242
243 current_power = pnp_read_config(dev, power_register);
244 new_power = current_power & ~power_mask; /* Disable by default. */
245 if (enable) {
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. */
250 } else {
251 lpc47n227_pnp_set_iobase(dev, 0);
252 }
253 pnp_write_config(dev, power_register, new_power);
254 }