Commit | Line | Data |
---|---|---|
28ff2f7a HG |
1 | /*************************************************************************** |
2 | * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com> * | |
3 | * * | |
4 | * This program is free software; you can redistribute it and/or modify * | |
5 | * it under the terms of the GNU General Public License as published by * | |
6 | * the Free Software Foundation; either version 2 of the License, or * | |
7 | * (at your option) any later version. * | |
8 | * * | |
9 | * This program is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU General Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU General Public License * | |
15 | * along with this program; if not, write to the * | |
16 | * Free Software Foundation, Inc., * | |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | |
18 | ***************************************************************************/ | |
19 | ||
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/err.h> | |
26 | #include <linux/io.h> | |
27 | #include <linux/acpi.h> | |
28 | #include <linux/delay.h> | |
29 | #include "sch56xx-common.h" | |
30 | ||
31 | #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */ | |
32 | #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */ | |
33 | #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */ | |
34 | ||
35 | #define SIO_REG_LDSEL 0x07 /* Logical device select */ | |
36 | #define SIO_REG_DEVID 0x20 /* Device ID */ | |
37 | #define SIO_REG_ENABLE 0x30 /* Logical device enable */ | |
38 | #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */ | |
39 | ||
40 | #define SIO_SCH5627_ID 0xC6 /* Chipset ID */ | |
41 | ||
42 | #define REGION_LENGTH 9 | |
43 | ||
44 | #define SCH56XX_CMD_READ 0x02 | |
45 | #define SCH56XX_CMD_WRITE 0x03 | |
46 | ||
47 | static struct platform_device *sch56xx_pdev; | |
48 | ||
49 | /* Super I/O functions */ | |
50 | static inline int superio_inb(int base, int reg) | |
51 | { | |
52 | outb(reg, base); | |
53 | return inb(base + 1); | |
54 | } | |
55 | ||
56 | static inline int superio_enter(int base) | |
57 | { | |
58 | /* Don't step on other drivers' I/O space by accident */ | |
59 | if (!request_muxed_region(base, 2, "sch56xx")) { | |
60 | pr_err("I/O address 0x%04x already in use\n", base); | |
61 | return -EBUSY; | |
62 | } | |
63 | ||
64 | outb(SIO_UNLOCK_KEY, base); | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | static inline void superio_select(int base, int ld) | |
70 | { | |
71 | outb(SIO_REG_LDSEL, base); | |
72 | outb(ld, base + 1); | |
73 | } | |
74 | ||
75 | static inline void superio_exit(int base) | |
76 | { | |
77 | outb(SIO_LOCK_KEY, base); | |
78 | release_region(base, 2); | |
79 | } | |
80 | ||
81 | static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v) | |
82 | { | |
83 | u8 val; | |
84 | int i; | |
85 | /* | |
86 | * According to SMSC for the commands we use the maximum time for | |
87 | * the EM to respond is 15 ms, but testing shows in practice it | |
88 | * responds within 15-32 reads, so we first busy poll, and if | |
89 | * that fails sleep a bit and try again until we are way past | |
90 | * the 15 ms maximum response time. | |
91 | */ | |
92 | const int max_busy_polls = 64; | |
93 | const int max_lazy_polls = 32; | |
94 | ||
95 | /* (Optional) Write-Clear the EC to Host Mailbox Register */ | |
96 | val = inb(addr + 1); | |
97 | outb(val, addr + 1); | |
98 | ||
99 | /* Set Mailbox Address Pointer to first location in Region 1 */ | |
100 | outb(0x00, addr + 2); | |
101 | outb(0x80, addr + 3); | |
102 | ||
103 | /* Write Request Packet Header */ | |
104 | outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */ | |
105 | outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */ | |
106 | outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */ | |
107 | ||
108 | /* Write Value field */ | |
109 | if (cmd == SCH56XX_CMD_WRITE) | |
110 | outb(v, addr + 4); | |
111 | ||
112 | /* Write Address field */ | |
113 | outb(reg & 0xff, addr + 6); | |
114 | outb(reg >> 8, addr + 7); | |
115 | ||
116 | /* Execute the Random Access Command */ | |
117 | outb(0x01, addr); /* Write 01h to the Host-to-EC register */ | |
118 | ||
119 | /* EM Interface Polling "Algorithm" */ | |
120 | for (i = 0; i < max_busy_polls + max_lazy_polls; i++) { | |
121 | if (i >= max_busy_polls) | |
122 | msleep(1); | |
123 | /* Read Interrupt source Register */ | |
124 | val = inb(addr + 8); | |
125 | /* Write Clear the interrupt source bits */ | |
126 | if (val) | |
127 | outb(val, addr + 8); | |
128 | /* Command Completed ? */ | |
129 | if (val & 0x01) | |
130 | break; | |
131 | } | |
132 | if (i == max_busy_polls + max_lazy_polls) { | |
133 | pr_err("Max retries exceeded reading virtual " | |
134 | "register 0x%04hx (%d)\n", reg, 1); | |
135 | return -EIO; | |
136 | } | |
137 | ||
138 | /* | |
139 | * According to SMSC we may need to retry this, but sofar I've always | |
140 | * seen this succeed in 1 try. | |
141 | */ | |
142 | for (i = 0; i < max_busy_polls; i++) { | |
143 | /* Read EC-to-Host Register */ | |
144 | val = inb(addr + 1); | |
145 | /* Command Completed ? */ | |
146 | if (val == 0x01) | |
147 | break; | |
148 | ||
149 | if (i == 0) | |
150 | pr_warn("EC reports: 0x%02x reading virtual register " | |
151 | "0x%04hx\n", (unsigned int)val, reg); | |
152 | } | |
153 | if (i == max_busy_polls) { | |
154 | pr_err("Max retries exceeded reading virtual " | |
155 | "register 0x%04hx (%d)\n", reg, 2); | |
156 | return -EIO; | |
157 | } | |
158 | ||
159 | /* | |
160 | * According to the SMSC app note we should now do: | |
161 | * | |
162 | * Set Mailbox Address Pointer to first location in Region 1 * | |
163 | * outb(0x00, addr + 2); | |
164 | * outb(0x80, addr + 3); | |
165 | * | |
166 | * But if we do that things don't work, so let's not. | |
167 | */ | |
168 | ||
169 | /* Read Value field */ | |
170 | if (cmd == SCH56XX_CMD_READ) | |
171 | return inb(addr + 4); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | int sch56xx_read_virtual_reg(u16 addr, u16 reg) | |
177 | { | |
178 | return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0); | |
179 | } | |
180 | EXPORT_SYMBOL(sch56xx_read_virtual_reg); | |
181 | ||
182 | int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val) | |
183 | { | |
184 | return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val); | |
185 | } | |
186 | EXPORT_SYMBOL(sch56xx_write_virtual_reg); | |
187 | ||
188 | int sch56xx_read_virtual_reg16(u16 addr, u16 reg) | |
189 | { | |
190 | int lsb, msb; | |
191 | ||
192 | /* Read LSB first, this will cause the matching MSB to be latched */ | |
193 | lsb = sch56xx_read_virtual_reg(addr, reg); | |
194 | if (lsb < 0) | |
195 | return lsb; | |
196 | ||
197 | msb = sch56xx_read_virtual_reg(addr, reg + 1); | |
198 | if (msb < 0) | |
199 | return msb; | |
200 | ||
201 | return lsb | (msb << 8); | |
202 | } | |
203 | EXPORT_SYMBOL(sch56xx_read_virtual_reg16); | |
204 | ||
205 | int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg, | |
206 | int high_nibble) | |
207 | { | |
208 | int msb, lsn; | |
209 | ||
210 | /* Read MSB first, this will cause the matching LSN to be latched */ | |
211 | msb = sch56xx_read_virtual_reg(addr, msb_reg); | |
212 | if (msb < 0) | |
213 | return msb; | |
214 | ||
215 | lsn = sch56xx_read_virtual_reg(addr, lsn_reg); | |
216 | if (lsn < 0) | |
217 | return lsn; | |
218 | ||
219 | if (high_nibble) | |
220 | return (msb << 4) | (lsn >> 4); | |
221 | else | |
222 | return (msb << 4) | (lsn & 0x0f); | |
223 | } | |
224 | EXPORT_SYMBOL(sch56xx_read_virtual_reg12); | |
225 | ||
226 | static int __init sch56xx_find(int sioaddr, unsigned short *address, | |
227 | const char **name) | |
228 | { | |
229 | u8 devid; | |
230 | int err; | |
231 | ||
232 | err = superio_enter(sioaddr); | |
233 | if (err) | |
234 | return err; | |
235 | ||
236 | devid = superio_inb(sioaddr, SIO_REG_DEVID); | |
237 | switch (devid) { | |
238 | case SIO_SCH5627_ID: | |
239 | *name = "sch5627"; | |
240 | break; | |
241 | default: | |
242 | pr_debug("Unsupported device id: 0x%02x\n", | |
243 | (unsigned int)devid); | |
244 | err = -ENODEV; | |
245 | goto exit; | |
246 | } | |
247 | ||
248 | superio_select(sioaddr, SIO_SCH56XX_LD_EM); | |
249 | ||
250 | if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) { | |
251 | pr_warn("Device not activated\n"); | |
252 | err = -ENODEV; | |
253 | goto exit; | |
254 | } | |
255 | ||
256 | /* | |
257 | * Warning the order of the low / high byte is the other way around | |
258 | * as on most other superio devices!! | |
259 | */ | |
260 | *address = superio_inb(sioaddr, SIO_REG_ADDR) | | |
261 | superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8; | |
262 | if (*address == 0) { | |
263 | pr_warn("Base address not set\n"); | |
264 | err = -ENODEV; | |
265 | goto exit; | |
266 | } | |
267 | ||
268 | exit: | |
269 | superio_exit(sioaddr); | |
270 | return err; | |
271 | } | |
272 | ||
273 | static int __init sch56xx_device_add(unsigned short address, const char *name) | |
274 | { | |
275 | struct resource res = { | |
276 | .start = address, | |
277 | .end = address + REGION_LENGTH - 1, | |
278 | .flags = IORESOURCE_IO, | |
279 | }; | |
280 | int err; | |
281 | ||
282 | sch56xx_pdev = platform_device_alloc(name, address); | |
283 | if (!sch56xx_pdev) | |
284 | return -ENOMEM; | |
285 | ||
286 | res.name = sch56xx_pdev->name; | |
287 | err = acpi_check_resource_conflict(&res); | |
288 | if (err) | |
289 | goto exit_device_put; | |
290 | ||
291 | err = platform_device_add_resources(sch56xx_pdev, &res, 1); | |
292 | if (err) { | |
293 | pr_err("Device resource addition failed\n"); | |
294 | goto exit_device_put; | |
295 | } | |
296 | ||
297 | err = platform_device_add(sch56xx_pdev); | |
298 | if (err) { | |
299 | pr_err("Device addition failed\n"); | |
300 | goto exit_device_put; | |
301 | } | |
302 | ||
303 | return 0; | |
304 | ||
305 | exit_device_put: | |
306 | platform_device_put(sch56xx_pdev); | |
307 | ||
308 | return err; | |
309 | } | |
310 | ||
311 | static int __init sch56xx_init(void) | |
312 | { | |
313 | int err; | |
314 | unsigned short address; | |
315 | const char *name; | |
316 | ||
317 | err = sch56xx_find(0x4e, &address, &name); | |
318 | if (err) | |
319 | err = sch56xx_find(0x2e, &address, &name); | |
320 | if (err) | |
321 | return err; | |
322 | ||
323 | return sch56xx_device_add(address, name); | |
324 | } | |
325 | ||
326 | static void __exit sch56xx_exit(void) | |
327 | { | |
328 | platform_device_unregister(sch56xx_pdev); | |
329 | } | |
330 | ||
331 | MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code"); | |
332 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); | |
333 | MODULE_LICENSE("GPL"); | |
334 | ||
335 | module_init(sch56xx_init); | |
336 | module_exit(sch56xx_exit); |