| 1 | |
| 2 | |
| 3 | * Copyright (C) 2015 Zodiac Inflight Innovations
|
| 4 | *
|
| 5 | * Author: Martyn Welch <[email protected]>
|
| 6 | *
|
| 7 | * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
|
| 8 | *
|
| 9 | * Copyright (C) Nokia Corporation
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/delay.h>
|
| 13 | #include <linux/i2c.h>
|
| 14 | #include <linux/ihex.h>
|
| 15 | #include <linux/firmware.h>
|
| 16 | #include <linux/kernel.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/slab.h>
|
| 19 | #include <linux/sysfs.h>
|
| 20 | #include <linux/types.h>
|
| 21 | #include <linux/watchdog.h>
|
| 22 |
|
| 23 | #include <linux/unaligned.h>
|
| 24 |
|
| 25 | #define ZIIRAVE_TIMEOUT_MIN 3
|
| 26 | #define ZIIRAVE_TIMEOUT_MAX 255
|
| 27 | #define ZIIRAVE_TIMEOUT_DEFAULT 30
|
| 28 |
|
| 29 | #define ZIIRAVE_PING_VALUE 0x0
|
| 30 |
|
| 31 | #define ZIIRAVE_STATE_INITIAL 0x0
|
| 32 | #define ZIIRAVE_STATE_OFF 0x1
|
| 33 | #define ZIIRAVE_STATE_ON 0x2
|
| 34 |
|
| 35 | #define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
|
| 36 |
|
| 37 | static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
|
| 38 | "host request", NULL, "illegal configuration",
|
| 39 | "illegal instruction", "illegal trap",
|
| 40 | "unknown"};
|
| 41 |
|
| 42 | #define ZIIRAVE_WDT_FIRM_VER_MAJOR 0x1
|
| 43 | #define ZIIRAVE_WDT_BOOT_VER_MAJOR 0x3
|
| 44 | #define ZIIRAVE_WDT_RESET_REASON 0x5
|
| 45 | #define ZIIRAVE_WDT_STATE 0x6
|
| 46 | #define ZIIRAVE_WDT_TIMEOUT 0x7
|
| 47 | #define ZIIRAVE_WDT_TIME_LEFT 0x8
|
| 48 | #define ZIIRAVE_WDT_PING 0x9
|
| 49 | #define ZIIRAVE_WDT_RESET_DURATION 0xa
|
| 50 |
|
| 51 | #define ZIIRAVE_FIRM_PKT_TOTAL_SIZE 20
|
| 52 | #define ZIIRAVE_FIRM_PKT_DATA_SIZE 16
|
| 53 | #define ZIIRAVE_FIRM_FLASH_MEMORY_START (2 * 0x1600)
|
| 54 | #define ZIIRAVE_FIRM_FLASH_MEMORY_END (2 * 0x2bbf)
|
| 55 | #define ZIIRAVE_FIRM_PAGE_SIZE 128
|
| 56 |
|
| 57 |
|
| 58 | #define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
|
| 59 |
|
| 60 |
|
| 61 | #define ZIIRAVE_CMD_DOWNLOAD_START 0x10
|
| 62 | #define ZIIRAVE_CMD_DOWNLOAD_END 0x11
|
| 63 | #define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
|
| 64 | #define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
|
| 65 | #define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
|
| 66 | #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
|
| 67 | #define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
|
| 68 |
|
| 69 | #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC 1
|
| 70 | #define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC 1
|
| 71 |
|
| 72 | struct ziirave_wdt_rev {
|
| 73 | unsigned char major;
|
| 74 | unsigned char minor;
|
| 75 | };
|
| 76 |
|
| 77 | struct ziirave_wdt_data {
|
| 78 | struct mutex sysfs_mutex;
|
| 79 | struct watchdog_device wdd;
|
| 80 | struct ziirave_wdt_rev bootloader_rev;
|
| 81 | struct ziirave_wdt_rev firmware_rev;
|
| 82 | int reset_reason;
|
| 83 | };
|
| 84 |
|
| 85 | static int wdt_timeout;
|
| 86 | module_param(wdt_timeout, int, 0);
|
| 87 | MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
|
| 88 |
|
| 89 | static int reset_duration;
|
| 90 | module_param(reset_duration, int, 0);
|
| 91 | MODULE_PARM_DESC(reset_duration,
|
| 92 | "Watchdog reset pulse duration in milliseconds");
|
| 93 |
|
| 94 | static bool nowayout = WATCHDOG_NOWAYOUT;
|
| 95 | module_param(nowayout, bool, 0);
|
| 96 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
|
| 97 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
| 98 |
|
| 99 | static int ziirave_wdt_revision(struct i2c_client *client,
|
| 100 | struct ziirave_wdt_rev *rev, u8 command)
|
| 101 | {
|
| 102 | int ret;
|
| 103 |
|
| 104 | ret = i2c_smbus_read_byte_data(client, command);
|
| 105 | if (ret < 0)
|
| 106 | return ret;
|
| 107 |
|
| 108 | rev->major = ret;
|
| 109 |
|
| 110 | ret = i2c_smbus_read_byte_data(client, command + 1);
|
| 111 | if (ret < 0)
|
| 112 | return ret;
|
| 113 |
|
| 114 | rev->minor = ret;
|
| 115 |
|
| 116 | return 0;
|
| 117 | }
|
| 118 |
|
| 119 | static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
|
| 120 | {
|
| 121 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 122 |
|
| 123 | return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
|
| 124 | }
|
| 125 |
|
| 126 | static int ziirave_wdt_start(struct watchdog_device *wdd)
|
| 127 | {
|
| 128 | return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
|
| 129 | }
|
| 130 |
|
| 131 | static int ziirave_wdt_stop(struct watchdog_device *wdd)
|
| 132 | {
|
| 133 | return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
|
| 134 | }
|
| 135 |
|
| 136 | static int ziirave_wdt_ping(struct watchdog_device *wdd)
|
| 137 | {
|
| 138 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 139 |
|
| 140 | return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
|
| 141 | ZIIRAVE_PING_VALUE);
|
| 142 | }
|
| 143 |
|
| 144 | static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
|
| 145 | unsigned int timeout)
|
| 146 | {
|
| 147 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 148 | int ret;
|
| 149 |
|
| 150 | ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
|
| 151 | if (!ret)
|
| 152 | wdd->timeout = timeout;
|
| 153 |
|
| 154 | return ret;
|
| 155 | }
|
| 156 |
|
| 157 | static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
|
| 158 | {
|
| 159 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 160 | int ret;
|
| 161 |
|
| 162 | ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
|
| 163 | if (ret < 0)
|
| 164 | ret = 0;
|
| 165 |
|
| 166 | return ret;
|
| 167 | }
|
| 168 |
|
| 169 | static int ziirave_firm_read_ack(struct watchdog_device *wdd)
|
| 170 | {
|
| 171 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 172 | int ret;
|
| 173 |
|
| 174 | ret = i2c_smbus_read_byte(client);
|
| 175 | if (ret < 0) {
|
| 176 | dev_err(&client->dev, "Failed to read status byte\n");
|
| 177 | return ret;
|
| 178 | }
|
| 179 |
|
| 180 | return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
|
| 181 | }
|
| 182 |
|
| 183 | static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
|
| 184 | {
|
| 185 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 186 | const u16 addr16 = (u16)addr / 2;
|
| 187 | u8 address[2];
|
| 188 |
|
| 189 | put_unaligned_le16(addr16, address);
|
| 190 |
|
| 191 | return i2c_smbus_write_block_data(client,
|
| 192 | ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
|
| 193 | sizeof(address), address);
|
| 194 | }
|
| 195 |
|
| 196 | static bool ziirave_firm_addr_readonly(u32 addr)
|
| 197 | {
|
| 198 | return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
|
| 199 | addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | * ziirave_firm_write_pkt() - Build and write a firmware packet
|
| 204 | *
|
| 205 | * A packet to send to the firmware is composed by following bytes:
|
| 206 | * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
|
| 207 | * Where,
|
| 208 | * Length: A data byte containing the length of the data.
|
| 209 | * Addr0: Low byte of the address.
|
| 210 | * Addr1: High byte of the address.
|
| 211 | * Data0 .. Data15: Array of 16 bytes of data.
|
| 212 | * Checksum: Checksum byte to verify data integrity.
|
| 213 |
|
| 214 | static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
|
| 215 | u32 addr, const u8 *data, u8 len)
|
| 216 | {
|
| 217 | const u16 addr16 = (u16)addr / 2;
|
| 218 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 219 | u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
|
| 220 | int ret;
|
| 221 |
|
| 222 |
|
| 223 | if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
|
| 224 | dev_err(&client->dev, "Firmware packet too long (%d)\n",
|
| 225 | len);
|
| 226 | return -EMSGSIZE;
|
| 227 | }
|
| 228 |
|
| 229 | |
| 230 | * Ignore packets that are targeting program memory outisde of
|
| 231 | * app partition, since they will be ignored by the
|
| 232 | * bootloader. At the same time, we need to make sure we'll
|
| 233 | * allow zero length packet that will be sent as the last step
|
| 234 | * of firmware update
|
| 235 |
|
| 236 | if (len && ziirave_firm_addr_readonly(addr))
|
| 237 | return 0;
|
| 238 |
|
| 239 |
|
| 240 | packet[0] = len;
|
| 241 |
|
| 242 | put_unaligned_le16(addr16, packet + 1);
|
| 243 |
|
| 244 | memcpy(packet + 3, data, len);
|
| 245 | memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
|
| 246 |
|
| 247 |
|
| 248 | for (i = 0; i < len + 3; i++)
|
| 249 | checksum += packet[i];
|
| 250 | packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
|
| 251 |
|
| 252 | ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
|
| 253 | sizeof(packet), packet);
|
| 254 | if (ret) {
|
| 255 | dev_err(&client->dev,
|
| 256 | "Failed to send DOWNLOAD_PACKET: %d\n", ret);
|
| 257 | return ret;
|
| 258 | }
|
| 259 |
|
| 260 | ret = ziirave_firm_read_ack(wdd);
|
| 261 | if (ret)
|
| 262 | dev_err(&client->dev,
|
| 263 | "Failed to write firmware packet at address 0x%04x: %d\n",
|
| 264 | addr, ret);
|
| 265 |
|
| 266 | return ret;
|
| 267 | }
|
| 268 |
|
| 269 | static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
|
| 270 | u32 addr, const u8 *data, u8 len)
|
| 271 | {
|
| 272 | const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
|
| 273 | (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
|
| 274 | int ret;
|
| 275 |
|
| 276 | if (len > max_write_len) {
|
| 277 | |
| 278 | * If data crossed page boundary we need to split this
|
| 279 | * write in two
|
| 280 |
|
| 281 | ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
|
| 282 | if (ret)
|
| 283 | return ret;
|
| 284 |
|
| 285 | addr += max_write_len;
|
| 286 | data += max_write_len;
|
| 287 | len -= max_write_len;
|
| 288 | }
|
| 289 |
|
| 290 | return __ziirave_firm_write_pkt(wdd, addr, data, len);
|
| 291 | }
|
| 292 |
|
| 293 | static int ziirave_firm_verify(struct watchdog_device *wdd,
|
| 294 | const struct firmware *fw)
|
| 295 | {
|
| 296 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 297 | const struct ihex_binrec *rec;
|
| 298 | int i, ret;
|
| 299 | u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
|
| 300 |
|
| 301 | for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
|
| 302 | const u16 len = be16_to_cpu(rec->len);
|
| 303 | const u32 addr = be32_to_cpu(rec->addr);
|
| 304 |
|
| 305 | if (len > sizeof(data))
|
| 306 | return -EINVAL;
|
| 307 |
|
| 308 | if (ziirave_firm_addr_readonly(addr))
|
| 309 | continue;
|
| 310 |
|
| 311 | ret = ziirave_firm_set_read_addr(wdd, addr);
|
| 312 | if (ret) {
|
| 313 | dev_err(&client->dev,
|
| 314 | "Failed to send SET_READ_ADDR command: %d\n",
|
| 315 | ret);
|
| 316 | return ret;
|
| 317 | }
|
| 318 |
|
| 319 | for (i = 0; i < len; i++) {
|
| 320 | ret = i2c_smbus_read_byte_data(client,
|
| 321 | ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
|
| 322 | if (ret < 0) {
|
| 323 | dev_err(&client->dev,
|
| 324 | "Failed to READ DATA: %d\n", ret);
|
| 325 | return ret;
|
| 326 | }
|
| 327 | data[i] = ret;
|
| 328 | }
|
| 329 |
|
| 330 | if (memcmp(data, rec->data, len)) {
|
| 331 | dev_err(&client->dev,
|
| 332 | "Firmware mismatch at address 0x%04x\n", addr);
|
| 333 | return -EINVAL;
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | return 0;
|
| 338 | }
|
| 339 |
|
| 340 | static int ziirave_firm_upload(struct watchdog_device *wdd,
|
| 341 | const struct firmware *fw)
|
| 342 | {
|
| 343 | struct i2c_client *client = to_i2c_client(wdd->parent);
|
| 344 | const struct ihex_binrec *rec;
|
| 345 | int ret;
|
| 346 |
|
| 347 | ret = i2c_smbus_write_byte_data(client,
|
| 348 | ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
|
| 349 | ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
|
| 350 | if (ret) {
|
| 351 | dev_err(&client->dev, "Failed to jump to bootloader\n");
|
| 352 | return ret;
|
| 353 | }
|
| 354 |
|
| 355 | msleep(500);
|
| 356 |
|
| 357 | ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
|
| 358 | if (ret) {
|
| 359 | dev_err(&client->dev, "Failed to start download\n");
|
| 360 | return ret;
|
| 361 | }
|
| 362 |
|
| 363 | ret = ziirave_firm_read_ack(wdd);
|
| 364 | if (ret) {
|
| 365 | dev_err(&client->dev, "No ACK for start download\n");
|
| 366 | return ret;
|
| 367 | }
|
| 368 |
|
| 369 | msleep(500);
|
| 370 |
|
| 371 | for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
|
| 372 | ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
|
| 373 | rec->data, be16_to_cpu(rec->len));
|
| 374 | if (ret)
|
| 375 | return ret;
|
| 376 | }
|
| 377 |
|
| 378 | |
| 379 | * Finish firmware download process by sending a zero length
|
| 380 | * payload
|
| 381 |
|
| 382 | ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
|
| 383 | if (ret) {
|
| 384 | dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
|
| 385 | return ret;
|
| 386 | }
|
| 387 |
|
| 388 |
|
| 389 | msleep(20);
|
| 390 |
|
| 391 |
|
| 392 | ret = ziirave_firm_verify(wdd, fw);
|
| 393 | if (ret) {
|
| 394 | dev_err(&client->dev,
|
| 395 | "Failed to verify firmware: %d\n", ret);
|
| 396 | return ret;
|
| 397 | }
|
| 398 |
|
| 399 |
|
| 400 | ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
|
| 401 | if (ret) {
|
| 402 | dev_err(&client->dev,
|
| 403 | "Failed to end firmware download: %d\n", ret);
|
| 404 | return ret;
|
| 405 | }
|
| 406 |
|
| 407 |
|
| 408 | ret = i2c_smbus_write_byte_data(client,
|
| 409 | ZIIRAVE_CMD_RESET_PROCESSOR,
|
| 410 | ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
|
| 411 | if (ret) {
|
| 412 | dev_err(&client->dev,
|
| 413 | "Failed to reset the watchdog: %d\n", ret);
|
| 414 | return ret;
|
| 415 | }
|
| 416 |
|
| 417 | msleep(500);
|
| 418 |
|
| 419 | return 0;
|
| 420 | }
|
| 421 |
|
| 422 | static const struct watchdog_info ziirave_wdt_info = {
|
| 423 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
|
| 424 | .identity = "RAVE Switch Watchdog",
|
| 425 | };
|
| 426 |
|
| 427 | static const struct watchdog_ops ziirave_wdt_ops = {
|
| 428 | .owner = THIS_MODULE,
|
| 429 | .start = ziirave_wdt_start,
|
| 430 | .stop = ziirave_wdt_stop,
|
| 431 | .ping = ziirave_wdt_ping,
|
| 432 | .set_timeout = ziirave_wdt_set_timeout,
|
| 433 | .get_timeleft = ziirave_wdt_get_timeleft,
|
| 434 | };
|
| 435 |
|
| 436 | static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
|
| 437 | struct device_attribute *attr,
|
| 438 | char *buf)
|
| 439 | {
|
| 440 | struct i2c_client *client = to_i2c_client(dev->parent);
|
| 441 | struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
|
| 442 | int ret;
|
| 443 |
|
| 444 | ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
|
| 445 | if (ret)
|
| 446 | return ret;
|
| 447 |
|
| 448 | ret = sysfs_emit(buf, "02.%02u.%02u\n",
|
| 449 | w_priv->firmware_rev.major,
|
| 450 | w_priv->firmware_rev.minor);
|
| 451 |
|
| 452 | mutex_unlock(&w_priv->sysfs_mutex);
|
| 453 |
|
| 454 | return ret;
|
| 455 | }
|
| 456 |
|
| 457 | static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
|
| 458 | NULL);
|
| 459 |
|
| 460 | static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
|
| 461 | struct device_attribute *attr,
|
| 462 | char *buf)
|
| 463 | {
|
| 464 | struct i2c_client *client = to_i2c_client(dev->parent);
|
| 465 | struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
|
| 466 | int ret;
|
| 467 |
|
| 468 | ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
|
| 469 | if (ret)
|
| 470 | return ret;
|
| 471 |
|
| 472 | ret = sysfs_emit(buf, "01.%02u.%02u\n",
|
| 473 | w_priv->bootloader_rev.major,
|
| 474 | w_priv->bootloader_rev.minor);
|
| 475 |
|
| 476 | mutex_unlock(&w_priv->sysfs_mutex);
|
| 477 |
|
| 478 | return ret;
|
| 479 | }
|
| 480 |
|
| 481 | static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
|
| 482 | NULL);
|
| 483 |
|
| 484 | static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
|
| 485 | struct device_attribute *attr,
|
| 486 | char *buf)
|
| 487 | {
|
| 488 | struct i2c_client *client = to_i2c_client(dev->parent);
|
| 489 | struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
|
| 490 | int ret;
|
| 491 |
|
| 492 | ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
|
| 493 | if (ret)
|
| 494 | return ret;
|
| 495 |
|
| 496 | ret = sysfs_emit(buf, "%s\n", ziirave_reasons[w_priv->reset_reason]);
|
| 497 |
|
| 498 | mutex_unlock(&w_priv->sysfs_mutex);
|
| 499 |
|
| 500 | return ret;
|
| 501 | }
|
| 502 |
|
| 503 | static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
|
| 504 | NULL);
|
| 505 |
|
| 506 | static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
|
| 507 | struct device_attribute *attr,
|
| 508 | const char *buf, size_t count)
|
| 509 | {
|
| 510 | struct i2c_client *client = to_i2c_client(dev->parent);
|
| 511 | struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
|
| 512 | const struct firmware *fw;
|
| 513 | int err;
|
| 514 |
|
| 515 | err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
|
| 516 | if (err) {
|
| 517 | dev_err(&client->dev, "Failed to request ihex firmware\n");
|
| 518 | return err;
|
| 519 | }
|
| 520 |
|
| 521 | err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
|
| 522 | if (err)
|
| 523 | goto release_firmware;
|
| 524 |
|
| 525 | err = ziirave_firm_upload(&w_priv->wdd, fw);
|
| 526 | if (err) {
|
| 527 | dev_err(&client->dev, "The firmware update failed: %d\n", err);
|
| 528 | goto unlock_mutex;
|
| 529 | }
|
| 530 |
|
| 531 |
|
| 532 | err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
|
| 533 | ZIIRAVE_WDT_FIRM_VER_MAJOR);
|
| 534 | if (err) {
|
| 535 | dev_err(&client->dev, "Failed to read firmware version: %d\n",
|
| 536 | err);
|
| 537 | goto unlock_mutex;
|
| 538 | }
|
| 539 |
|
| 540 | dev_info(&client->dev,
|
| 541 | "Firmware updated to version 02.%02u.%02u\n",
|
| 542 | w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
|
| 543 |
|
| 544 |
|
| 545 | err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
|
| 546 | if (err)
|
| 547 | dev_err(&client->dev, "Failed to set timeout: %d\n", err);
|
| 548 |
|
| 549 | unlock_mutex:
|
| 550 | mutex_unlock(&w_priv->sysfs_mutex);
|
| 551 |
|
| 552 | release_firmware:
|
| 553 | release_firmware(fw);
|
| 554 |
|
| 555 | return err ? err : count;
|
| 556 | }
|
| 557 |
|
| 558 | static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
|
| 559 | ziirave_wdt_sysfs_store_firm);
|
| 560 |
|
| 561 | static struct attribute *ziirave_wdt_attrs[] = {
|
| 562 | &dev_attr_firmware_version.attr,
|
| 563 | &dev_attr_bootloader_version.attr,
|
| 564 | &dev_attr_reset_reason.attr,
|
| 565 | &dev_attr_update_firmware.attr,
|
| 566 | NULL
|
| 567 | };
|
| 568 | ATTRIBUTE_GROUPS(ziirave_wdt);
|
| 569 |
|
| 570 | static int ziirave_wdt_init_duration(struct i2c_client *client)
|
| 571 | {
|
| 572 | int ret;
|
| 573 |
|
| 574 | if (!reset_duration) {
|
| 575 |
|
| 576 | if (!client->dev.of_node)
|
| 577 | ret = -ENODEV;
|
| 578 | else
|
| 579 | ret = of_property_read_u32(client->dev.of_node,
|
| 580 | "reset-duration-ms",
|
| 581 | &reset_duration);
|
| 582 | if (ret) {
|
| 583 | dev_info(&client->dev,
|
| 584 | "No reset pulse duration specified, using default\n");
|
| 585 | return 0;
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | if (reset_duration < 1 || reset_duration > 255)
|
| 590 | return -EINVAL;
|
| 591 |
|
| 592 | dev_info(&client->dev, "Setting reset duration to %dms",
|
| 593 | reset_duration);
|
| 594 |
|
| 595 | return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
|
| 596 | reset_duration);
|
| 597 | }
|
| 598 |
|
| 599 | static int ziirave_wdt_probe(struct i2c_client *client)
|
| 600 | {
|
| 601 | int ret;
|
| 602 | struct ziirave_wdt_data *w_priv;
|
| 603 | int val;
|
| 604 |
|
| 605 | if (!i2c_check_functionality(client->adapter,
|
| 606 | I2C_FUNC_SMBUS_BYTE |
|
| 607 | I2C_FUNC_SMBUS_BYTE_DATA |
|
| 608 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
|
| 609 | return -ENODEV;
|
| 610 |
|
| 611 | w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
|
| 612 | if (!w_priv)
|
| 613 | return -ENOMEM;
|
| 614 |
|
| 615 | mutex_init(&w_priv->sysfs_mutex);
|
| 616 |
|
| 617 | w_priv->wdd.info = &ziirave_wdt_info;
|
| 618 | w_priv->wdd.ops = &ziirave_wdt_ops;
|
| 619 | w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
|
| 620 | w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
|
| 621 | w_priv->wdd.parent = &client->dev;
|
| 622 | w_priv->wdd.groups = ziirave_wdt_groups;
|
| 623 |
|
| 624 | watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
|
| 625 |
|
| 626 | |
| 627 | * The default value set in the watchdog should be perfectly valid, so
|
| 628 | * pass that in if we haven't provided one via the module parameter or
|
| 629 | * of property.
|
| 630 |
|
| 631 | if (w_priv->wdd.timeout == 0) {
|
| 632 | val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
|
| 633 | if (val < 0) {
|
| 634 | dev_err(&client->dev, "Failed to read timeout\n");
|
| 635 | return val;
|
| 636 | }
|
| 637 |
|
| 638 | if (val > ZIIRAVE_TIMEOUT_MAX ||
|
| 639 | val < ZIIRAVE_TIMEOUT_MIN)
|
| 640 | val = ZIIRAVE_TIMEOUT_DEFAULT;
|
| 641 |
|
| 642 | w_priv->wdd.timeout = val;
|
| 643 | }
|
| 644 |
|
| 645 | ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
|
| 646 | if (ret) {
|
| 647 | dev_err(&client->dev, "Failed to set timeout\n");
|
| 648 | return ret;
|
| 649 | }
|
| 650 |
|
| 651 | dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
|
| 652 |
|
| 653 | watchdog_set_nowayout(&w_priv->wdd, nowayout);
|
| 654 |
|
| 655 | i2c_set_clientdata(client, w_priv);
|
| 656 |
|
| 657 |
|
| 658 | val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
|
| 659 | if (val < 0) {
|
| 660 | dev_err(&client->dev, "Failed to read state\n");
|
| 661 | return val;
|
| 662 | }
|
| 663 |
|
| 664 | if (val == ZIIRAVE_STATE_INITIAL)
|
| 665 | ziirave_wdt_stop(&w_priv->wdd);
|
| 666 |
|
| 667 | ret = ziirave_wdt_init_duration(client);
|
| 668 | if (ret) {
|
| 669 | dev_err(&client->dev, "Failed to init duration\n");
|
| 670 | return ret;
|
| 671 | }
|
| 672 |
|
| 673 | ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
|
| 674 | ZIIRAVE_WDT_FIRM_VER_MAJOR);
|
| 675 | if (ret) {
|
| 676 | dev_err(&client->dev, "Failed to read firmware version\n");
|
| 677 | return ret;
|
| 678 | }
|
| 679 |
|
| 680 | dev_info(&client->dev,
|
| 681 | "Firmware version: 02.%02u.%02u\n",
|
| 682 | w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
|
| 683 |
|
| 684 | ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
|
| 685 | ZIIRAVE_WDT_BOOT_VER_MAJOR);
|
| 686 | if (ret) {
|
| 687 | dev_err(&client->dev, "Failed to read bootloader version\n");
|
| 688 | return ret;
|
| 689 | }
|
| 690 |
|
| 691 | dev_info(&client->dev,
|
| 692 | "Bootloader version: 01.%02u.%02u\n",
|
| 693 | w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
|
| 694 |
|
| 695 | w_priv->reset_reason = i2c_smbus_read_byte_data(client,
|
| 696 | ZIIRAVE_WDT_RESET_REASON);
|
| 697 | if (w_priv->reset_reason < 0) {
|
| 698 | dev_err(&client->dev, "Failed to read reset reason\n");
|
| 699 | return w_priv->reset_reason;
|
| 700 | }
|
| 701 |
|
| 702 | if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
|
| 703 | !ziirave_reasons[w_priv->reset_reason]) {
|
| 704 | dev_err(&client->dev, "Invalid reset reason\n");
|
| 705 | return -ENODEV;
|
| 706 | }
|
| 707 |
|
| 708 | ret = watchdog_register_device(&w_priv->wdd);
|
| 709 |
|
| 710 | return ret;
|
| 711 | }
|
| 712 |
|
| 713 | static void ziirave_wdt_remove(struct i2c_client *client)
|
| 714 | {
|
| 715 | struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
|
| 716 |
|
| 717 | watchdog_unregister_device(&w_priv->wdd);
|
| 718 | }
|
| 719 |
|
| 720 | static const struct i2c_device_id ziirave_wdt_id[] = {
|
| 721 | { .name = "rave-wdt" },
|
| 722 | { }
|
| 723 | };
|
| 724 | MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
|
| 725 |
|
| 726 | static const struct of_device_id zrv_wdt_of_match[] = {
|
| 727 | { .compatible = "zii,rave-wdt", },
|
| 728 | { },
|
| 729 | };
|
| 730 | MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
|
| 731 |
|
| 732 | static struct i2c_driver ziirave_wdt_driver = {
|
| 733 | .driver = {
|
| 734 | .name = "ziirave_wdt",
|
| 735 | .of_match_table = zrv_wdt_of_match,
|
| 736 | },
|
| 737 | .probe = ziirave_wdt_probe,
|
| 738 | .remove = ziirave_wdt_remove,
|
| 739 | .id_table = ziirave_wdt_id,
|
| 740 | };
|
| 741 |
|
| 742 | module_i2c_driver(ziirave_wdt_driver);
|
| 743 |
|
| 744 | MODULE_AUTHOR("Martyn Welch <[email protected]");
|
| 745 | MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
|
| 746 | MODULE_LICENSE("GPL");
|