Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 1 | /*************************************************************************** |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 2 | * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> * |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 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> |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 29 | #include <linux/fs.h> |
| 30 | #include <linux/watchdog.h> |
| 31 | #include <linux/miscdevice.h> |
| 32 | #include <linux/uaccess.h> |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 33 | #include <linux/slab.h> |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 34 | #include "sch56xx-common.h" |
| 35 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 36 | /* Insmod parameters */ |
| 37 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 38 | module_param(nowayout, int, 0); |
| 39 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 40 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 41 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 42 | #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */ |
| 43 | #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */ |
| 44 | #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */ |
| 45 | |
| 46 | #define SIO_REG_LDSEL 0x07 /* Logical device select */ |
| 47 | #define SIO_REG_DEVID 0x20 /* Device ID */ |
| 48 | #define SIO_REG_ENABLE 0x30 /* Logical device enable */ |
| 49 | #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */ |
| 50 | |
| 51 | #define SIO_SCH5627_ID 0xC6 /* Chipset ID */ |
Hans de Goede | 0772a64 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 52 | #define SIO_SCH5636_ID 0xC7 /* Chipset ID */ |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 53 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 54 | #define REGION_LENGTH 10 |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 55 | |
| 56 | #define SCH56XX_CMD_READ 0x02 |
| 57 | #define SCH56XX_CMD_WRITE 0x03 |
| 58 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 59 | /* Watchdog registers */ |
| 60 | #define SCH56XX_REG_WDOG_PRESET 0x58B |
| 61 | #define SCH56XX_REG_WDOG_CONTROL 0x58C |
| 62 | #define SCH56XX_WDOG_TIME_BASE_SEC 0x01 |
| 63 | #define SCH56XX_REG_WDOG_OUTPUT_ENABLE 0x58E |
| 64 | #define SCH56XX_WDOG_OUTPUT_ENABLE 0x02 |
| 65 | |
| 66 | struct sch56xx_watchdog_data { |
| 67 | u16 addr; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 68 | struct mutex *io_lock; |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 69 | struct watchdog_info wdinfo; |
| 70 | struct watchdog_device wddev; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 71 | u8 watchdog_preset; |
| 72 | u8 watchdog_control; |
| 73 | u8 watchdog_output_enable; |
| 74 | }; |
| 75 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 76 | static struct platform_device *sch56xx_pdev; |
| 77 | |
| 78 | /* Super I/O functions */ |
| 79 | static inline int superio_inb(int base, int reg) |
| 80 | { |
| 81 | outb(reg, base); |
| 82 | return inb(base + 1); |
| 83 | } |
| 84 | |
| 85 | static inline int superio_enter(int base) |
| 86 | { |
| 87 | /* Don't step on other drivers' I/O space by accident */ |
| 88 | if (!request_muxed_region(base, 2, "sch56xx")) { |
| 89 | pr_err("I/O address 0x%04x already in use\n", base); |
| 90 | return -EBUSY; |
| 91 | } |
| 92 | |
| 93 | outb(SIO_UNLOCK_KEY, base); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static inline void superio_select(int base, int ld) |
| 99 | { |
| 100 | outb(SIO_REG_LDSEL, base); |
| 101 | outb(ld, base + 1); |
| 102 | } |
| 103 | |
| 104 | static inline void superio_exit(int base) |
| 105 | { |
| 106 | outb(SIO_LOCK_KEY, base); |
| 107 | release_region(base, 2); |
| 108 | } |
| 109 | |
| 110 | static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v) |
| 111 | { |
| 112 | u8 val; |
| 113 | int i; |
| 114 | /* |
| 115 | * According to SMSC for the commands we use the maximum time for |
| 116 | * the EM to respond is 15 ms, but testing shows in practice it |
| 117 | * responds within 15-32 reads, so we first busy poll, and if |
| 118 | * that fails sleep a bit and try again until we are way past |
| 119 | * the 15 ms maximum response time. |
| 120 | */ |
| 121 | const int max_busy_polls = 64; |
| 122 | const int max_lazy_polls = 32; |
| 123 | |
| 124 | /* (Optional) Write-Clear the EC to Host Mailbox Register */ |
| 125 | val = inb(addr + 1); |
| 126 | outb(val, addr + 1); |
| 127 | |
| 128 | /* Set Mailbox Address Pointer to first location in Region 1 */ |
| 129 | outb(0x00, addr + 2); |
| 130 | outb(0x80, addr + 3); |
| 131 | |
| 132 | /* Write Request Packet Header */ |
| 133 | outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */ |
| 134 | outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */ |
| 135 | outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */ |
| 136 | |
| 137 | /* Write Value field */ |
| 138 | if (cmd == SCH56XX_CMD_WRITE) |
| 139 | outb(v, addr + 4); |
| 140 | |
| 141 | /* Write Address field */ |
| 142 | outb(reg & 0xff, addr + 6); |
| 143 | outb(reg >> 8, addr + 7); |
| 144 | |
| 145 | /* Execute the Random Access Command */ |
| 146 | outb(0x01, addr); /* Write 01h to the Host-to-EC register */ |
| 147 | |
| 148 | /* EM Interface Polling "Algorithm" */ |
| 149 | for (i = 0; i < max_busy_polls + max_lazy_polls; i++) { |
| 150 | if (i >= max_busy_polls) |
| 151 | msleep(1); |
| 152 | /* Read Interrupt source Register */ |
| 153 | val = inb(addr + 8); |
| 154 | /* Write Clear the interrupt source bits */ |
| 155 | if (val) |
| 156 | outb(val, addr + 8); |
| 157 | /* Command Completed ? */ |
| 158 | if (val & 0x01) |
| 159 | break; |
| 160 | } |
| 161 | if (i == max_busy_polls + max_lazy_polls) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 162 | pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n", |
| 163 | reg, 1); |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 164 | return -EIO; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * According to SMSC we may need to retry this, but sofar I've always |
| 169 | * seen this succeed in 1 try. |
| 170 | */ |
| 171 | for (i = 0; i < max_busy_polls; i++) { |
| 172 | /* Read EC-to-Host Register */ |
| 173 | val = inb(addr + 1); |
| 174 | /* Command Completed ? */ |
| 175 | if (val == 0x01) |
| 176 | break; |
| 177 | |
| 178 | if (i == 0) |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 179 | pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n", |
| 180 | (unsigned int)val, reg); |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 181 | } |
| 182 | if (i == max_busy_polls) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 183 | pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n", |
| 184 | reg, 2); |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 185 | return -EIO; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * According to the SMSC app note we should now do: |
| 190 | * |
| 191 | * Set Mailbox Address Pointer to first location in Region 1 * |
| 192 | * outb(0x00, addr + 2); |
| 193 | * outb(0x80, addr + 3); |
| 194 | * |
| 195 | * But if we do that things don't work, so let's not. |
| 196 | */ |
| 197 | |
| 198 | /* Read Value field */ |
| 199 | if (cmd == SCH56XX_CMD_READ) |
| 200 | return inb(addr + 4); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int sch56xx_read_virtual_reg(u16 addr, u16 reg) |
| 206 | { |
| 207 | return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0); |
| 208 | } |
| 209 | EXPORT_SYMBOL(sch56xx_read_virtual_reg); |
| 210 | |
| 211 | int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val) |
| 212 | { |
| 213 | return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val); |
| 214 | } |
| 215 | EXPORT_SYMBOL(sch56xx_write_virtual_reg); |
| 216 | |
| 217 | int sch56xx_read_virtual_reg16(u16 addr, u16 reg) |
| 218 | { |
| 219 | int lsb, msb; |
| 220 | |
| 221 | /* Read LSB first, this will cause the matching MSB to be latched */ |
| 222 | lsb = sch56xx_read_virtual_reg(addr, reg); |
| 223 | if (lsb < 0) |
| 224 | return lsb; |
| 225 | |
| 226 | msb = sch56xx_read_virtual_reg(addr, reg + 1); |
| 227 | if (msb < 0) |
| 228 | return msb; |
| 229 | |
| 230 | return lsb | (msb << 8); |
| 231 | } |
| 232 | EXPORT_SYMBOL(sch56xx_read_virtual_reg16); |
| 233 | |
| 234 | int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg, |
| 235 | int high_nibble) |
| 236 | { |
| 237 | int msb, lsn; |
| 238 | |
| 239 | /* Read MSB first, this will cause the matching LSN to be latched */ |
| 240 | msb = sch56xx_read_virtual_reg(addr, msb_reg); |
| 241 | if (msb < 0) |
| 242 | return msb; |
| 243 | |
| 244 | lsn = sch56xx_read_virtual_reg(addr, lsn_reg); |
| 245 | if (lsn < 0) |
| 246 | return lsn; |
| 247 | |
| 248 | if (high_nibble) |
| 249 | return (msb << 4) | (lsn >> 4); |
| 250 | else |
| 251 | return (msb << 4) | (lsn & 0x0f); |
| 252 | } |
| 253 | EXPORT_SYMBOL(sch56xx_read_virtual_reg12); |
| 254 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 255 | /* |
| 256 | * Watchdog routines |
| 257 | */ |
| 258 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 259 | static int watchdog_set_timeout(struct watchdog_device *wddev, |
| 260 | unsigned int timeout) |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 261 | { |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 262 | struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev); |
| 263 | unsigned int resolution; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 264 | u8 control; |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 265 | int ret; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 266 | |
| 267 | /* 1 second or 60 second resolution? */ |
| 268 | if (timeout <= 255) |
| 269 | resolution = 1; |
| 270 | else |
| 271 | resolution = 60; |
| 272 | |
| 273 | if (timeout < resolution || timeout > (resolution * 255)) |
| 274 | return -EINVAL; |
| 275 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 276 | if (resolution == 1) |
| 277 | control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC; |
| 278 | else |
| 279 | control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC; |
| 280 | |
| 281 | if (data->watchdog_control != control) { |
| 282 | mutex_lock(data->io_lock); |
| 283 | ret = sch56xx_write_virtual_reg(data->addr, |
| 284 | SCH56XX_REG_WDOG_CONTROL, |
| 285 | control); |
| 286 | mutex_unlock(data->io_lock); |
| 287 | if (ret) |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 288 | return ret; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 289 | |
| 290 | data->watchdog_control = control; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Remember new timeout value, but do not write as that (re)starts |
| 295 | * the watchdog countdown. |
| 296 | */ |
| 297 | data->watchdog_preset = DIV_ROUND_UP(timeout, resolution); |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 298 | wddev->timeout = data->watchdog_preset * resolution; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 299 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 300 | return 0; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 303 | static int watchdog_start(struct watchdog_device *wddev) |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 304 | { |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 305 | struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 306 | int ret; |
| 307 | u8 val; |
| 308 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 309 | /* |
| 310 | * The sch56xx's watchdog cannot really be started / stopped |
| 311 | * it is always running, but we can avoid the timer expiring |
| 312 | * from causing a system reset by clearing the output enable bit. |
| 313 | * |
| 314 | * The sch56xx's watchdog will set the watchdog event bit, bit 0 |
| 315 | * of the second interrupt source register (at base-address + 9), |
| 316 | * when the timer expires. |
| 317 | * |
| 318 | * This will only cause a system reset if the 0-1 flank happens when |
| 319 | * output enable is true. Setting output enable after the flank will |
| 320 | * not cause a reset, nor will the timer expiring a second time. |
| 321 | * This means we must clear the watchdog event bit in case it is set. |
| 322 | * |
| 323 | * The timer may still be running (after a recent watchdog_stop) and |
| 324 | * mere milliseconds away from expiring, so the timer must be reset |
| 325 | * first! |
| 326 | */ |
| 327 | |
| 328 | mutex_lock(data->io_lock); |
| 329 | |
| 330 | /* 1. Reset the watchdog countdown counter */ |
| 331 | ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET, |
| 332 | data->watchdog_preset); |
| 333 | if (ret) |
| 334 | goto leave; |
| 335 | |
Hans de Goede | 85a2e40 | 2012-05-22 11:40:25 +0200 | [diff] [blame] | 336 | /* 2. Enable output */ |
| 337 | val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE; |
| 338 | ret = sch56xx_write_virtual_reg(data->addr, |
| 339 | SCH56XX_REG_WDOG_OUTPUT_ENABLE, val); |
| 340 | if (ret) |
| 341 | goto leave; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 342 | |
Hans de Goede | 85a2e40 | 2012-05-22 11:40:25 +0200 | [diff] [blame] | 343 | data->watchdog_output_enable = val; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 344 | |
| 345 | /* 3. Clear the watchdog event bit if set */ |
| 346 | val = inb(data->addr + 9); |
| 347 | if (val & 0x01) |
| 348 | outb(0x01, data->addr + 9); |
| 349 | |
| 350 | leave: |
| 351 | mutex_unlock(data->io_lock); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 352 | return ret; |
| 353 | } |
| 354 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 355 | static int watchdog_trigger(struct watchdog_device *wddev) |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 356 | { |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 357 | struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 358 | int ret; |
| 359 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 360 | /* Reset the watchdog countdown counter */ |
| 361 | mutex_lock(data->io_lock); |
| 362 | ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET, |
| 363 | data->watchdog_preset); |
| 364 | mutex_unlock(data->io_lock); |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 365 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 366 | return ret; |
| 367 | } |
| 368 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 369 | static int watchdog_stop(struct watchdog_device *wddev) |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 370 | { |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 371 | struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 372 | int ret = 0; |
| 373 | u8 val; |
| 374 | |
Hans de Goede | 85a2e40 | 2012-05-22 11:40:25 +0200 | [diff] [blame] | 375 | val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE; |
| 376 | mutex_lock(data->io_lock); |
| 377 | ret = sch56xx_write_virtual_reg(data->addr, |
| 378 | SCH56XX_REG_WDOG_OUTPUT_ENABLE, val); |
| 379 | mutex_unlock(data->io_lock); |
| 380 | if (ret) |
| 381 | return ret; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 382 | |
Hans de Goede | 85a2e40 | 2012-05-22 11:40:25 +0200 | [diff] [blame] | 383 | data->watchdog_output_enable = val; |
| 384 | return 0; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 387 | static const struct watchdog_ops watchdog_ops = { |
| 388 | .owner = THIS_MODULE, |
| 389 | .start = watchdog_start, |
| 390 | .stop = watchdog_stop, |
| 391 | .ping = watchdog_trigger, |
| 392 | .set_timeout = watchdog_set_timeout, |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 393 | }; |
| 394 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 395 | struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent, |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 396 | u16 addr, u32 revision, struct mutex *io_lock, int check_enabled) |
| 397 | { |
| 398 | struct sch56xx_watchdog_data *data; |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 399 | int err, control, output_enable; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 400 | |
| 401 | /* Cache the watchdog registers */ |
| 402 | mutex_lock(io_lock); |
| 403 | control = |
| 404 | sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL); |
| 405 | output_enable = |
| 406 | sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE); |
| 407 | mutex_unlock(io_lock); |
| 408 | |
| 409 | if (control < 0) |
| 410 | return NULL; |
| 411 | if (output_enable < 0) |
| 412 | return NULL; |
| 413 | if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) { |
| 414 | pr_warn("Watchdog not enabled by BIOS, not registering\n"); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | data = kzalloc(sizeof(struct sch56xx_watchdog_data), GFP_KERNEL); |
| 419 | if (!data) |
| 420 | return NULL; |
| 421 | |
| 422 | data->addr = addr; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 423 | data->io_lock = io_lock; |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 424 | |
| 425 | strlcpy(data->wdinfo.identity, "sch56xx watchdog", |
| 426 | sizeof(data->wdinfo.identity)); |
| 427 | data->wdinfo.firmware_version = revision; |
| 428 | data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT; |
| 429 | if (!nowayout) |
| 430 | data->wdinfo.options |= WDIOF_MAGICCLOSE; |
| 431 | |
| 432 | data->wddev.info = &data->wdinfo; |
| 433 | data->wddev.ops = &watchdog_ops; |
| 434 | data->wddev.parent = parent; |
| 435 | data->wddev.timeout = 60; |
| 436 | data->wddev.min_timeout = 1; |
| 437 | data->wddev.max_timeout = 255 * 60; |
| 438 | if (nowayout) |
Dan Carpenter | bb64491 | 2012-05-24 18:58:02 +0300 | [diff] [blame] | 439 | set_bit(WDOG_NO_WAY_OUT, &data->wddev.status); |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 440 | if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE) |
Dan Carpenter | bb64491 | 2012-05-24 18:58:02 +0300 | [diff] [blame] | 441 | set_bit(WDOG_ACTIVE, &data->wddev.status); |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 442 | |
| 443 | /* Since the watchdog uses a downcounter there is no register to read |
| 444 | the BIOS set timeout from (if any was set at all) -> |
| 445 | Choose a preset which will give us a 1 minute timeout */ |
| 446 | if (control & SCH56XX_WDOG_TIME_BASE_SEC) |
| 447 | data->watchdog_preset = 60; /* seconds */ |
| 448 | else |
| 449 | data->watchdog_preset = 1; /* minute */ |
| 450 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 451 | data->watchdog_control = control; |
| 452 | data->watchdog_output_enable = output_enable; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 453 | |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 454 | watchdog_set_drvdata(&data->wddev, data); |
| 455 | err = watchdog_register_device(&data->wddev); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 456 | if (err) { |
| 457 | pr_err("Registering watchdog chardev: %d\n", err); |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 458 | kfree(data); |
| 459 | return NULL; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return data; |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 463 | } |
| 464 | EXPORT_SYMBOL(sch56xx_watchdog_register); |
| 465 | |
| 466 | void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data) |
| 467 | { |
Hans de Goede | fb55140 | 2012-05-22 11:40:24 +0200 | [diff] [blame] | 468 | watchdog_unregister_device(&data->wddev); |
Guenter Roeck | 3b8d058 | 2015-12-25 16:01:45 -0800 | [diff] [blame] | 469 | kfree(data); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL(sch56xx_watchdog_unregister); |
| 472 | |
| 473 | /* |
| 474 | * platform dev find, add and remove functions |
| 475 | */ |
| 476 | |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 477 | static int __init sch56xx_find(int sioaddr, const char **name) |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 478 | { |
| 479 | u8 devid; |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 480 | unsigned short address; |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 481 | int err; |
| 482 | |
| 483 | err = superio_enter(sioaddr); |
| 484 | if (err) |
| 485 | return err; |
| 486 | |
| 487 | devid = superio_inb(sioaddr, SIO_REG_DEVID); |
| 488 | switch (devid) { |
| 489 | case SIO_SCH5627_ID: |
| 490 | *name = "sch5627"; |
| 491 | break; |
Hans de Goede | 0772a64 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 492 | case SIO_SCH5636_ID: |
| 493 | *name = "sch5636"; |
| 494 | break; |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 495 | default: |
| 496 | pr_debug("Unsupported device id: 0x%02x\n", |
| 497 | (unsigned int)devid); |
| 498 | err = -ENODEV; |
| 499 | goto exit; |
| 500 | } |
| 501 | |
| 502 | superio_select(sioaddr, SIO_SCH56XX_LD_EM); |
| 503 | |
| 504 | if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) { |
| 505 | pr_warn("Device not activated\n"); |
| 506 | err = -ENODEV; |
| 507 | goto exit; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Warning the order of the low / high byte is the other way around |
| 512 | * as on most other superio devices!! |
| 513 | */ |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 514 | address = superio_inb(sioaddr, SIO_REG_ADDR) | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 515 | superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8; |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 516 | if (address == 0) { |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 517 | pr_warn("Base address not set\n"); |
| 518 | err = -ENODEV; |
| 519 | goto exit; |
| 520 | } |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 521 | err = address; |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 522 | |
| 523 | exit: |
| 524 | superio_exit(sioaddr); |
| 525 | return err; |
| 526 | } |
| 527 | |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 528 | static int __init sch56xx_device_add(int address, const char *name) |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 529 | { |
| 530 | struct resource res = { |
| 531 | .start = address, |
| 532 | .end = address + REGION_LENGTH - 1, |
| 533 | .flags = IORESOURCE_IO, |
| 534 | }; |
| 535 | int err; |
| 536 | |
| 537 | sch56xx_pdev = platform_device_alloc(name, address); |
| 538 | if (!sch56xx_pdev) |
| 539 | return -ENOMEM; |
| 540 | |
| 541 | res.name = sch56xx_pdev->name; |
| 542 | err = acpi_check_resource_conflict(&res); |
| 543 | if (err) |
| 544 | goto exit_device_put; |
| 545 | |
| 546 | err = platform_device_add_resources(sch56xx_pdev, &res, 1); |
| 547 | if (err) { |
| 548 | pr_err("Device resource addition failed\n"); |
| 549 | goto exit_device_put; |
| 550 | } |
| 551 | |
| 552 | err = platform_device_add(sch56xx_pdev); |
| 553 | if (err) { |
| 554 | pr_err("Device addition failed\n"); |
| 555 | goto exit_device_put; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | exit_device_put: |
| 561 | platform_device_put(sch56xx_pdev); |
| 562 | |
| 563 | return err; |
| 564 | } |
| 565 | |
| 566 | static int __init sch56xx_init(void) |
| 567 | { |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 568 | int address; |
| 569 | const char *name = NULL; |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 570 | |
Guenter Roeck | 313829e | 2012-08-04 09:54:16 -0700 | [diff] [blame] | 571 | address = sch56xx_find(0x4e, &name); |
| 572 | if (address < 0) |
| 573 | address = sch56xx_find(0x2e, &name); |
| 574 | if (address < 0) |
| 575 | return address; |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 576 | |
| 577 | return sch56xx_device_add(address, name); |
| 578 | } |
| 579 | |
| 580 | static void __exit sch56xx_exit(void) |
| 581 | { |
| 582 | platform_device_unregister(sch56xx_pdev); |
| 583 | } |
| 584 | |
| 585 | MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code"); |
| 586 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 587 | MODULE_LICENSE("GPL"); |
| 588 | |
| 589 | module_init(sch56xx_init); |
| 590 | module_exit(sch56xx_exit); |