1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Zodiac Inflight Innovations
5 * Author: Martyn Welch <martyn.welch@collabora.co.uk>
7 * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
9 * Copyright (C) Nokia Corporation
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/version.h>
22 #include <linux/watchdog.h>
24 #include <asm/unaligned.h>
26 #define ZIIRAVE_TIMEOUT_MIN 3
27 #define ZIIRAVE_TIMEOUT_MAX 255
28 #define ZIIRAVE_TIMEOUT_DEFAULT 30
30 #define ZIIRAVE_PING_VALUE 0x0
32 #define ZIIRAVE_STATE_INITIAL 0x0
33 #define ZIIRAVE_STATE_OFF 0x1
34 #define ZIIRAVE_STATE_ON 0x2
36 #define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
38 static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
39 "host request", NULL, "illegal configuration",
40 "illegal instruction", "illegal trap",
43 #define ZIIRAVE_WDT_FIRM_VER_MAJOR 0x1
44 #define ZIIRAVE_WDT_BOOT_VER_MAJOR 0x3
45 #define ZIIRAVE_WDT_RESET_REASON 0x5
46 #define ZIIRAVE_WDT_STATE 0x6
47 #define ZIIRAVE_WDT_TIMEOUT 0x7
48 #define ZIIRAVE_WDT_TIME_LEFT 0x8
49 #define ZIIRAVE_WDT_PING 0x9
50 #define ZIIRAVE_WDT_RESET_DURATION 0xa
52 #define ZIIRAVE_FIRM_PKT_TOTAL_SIZE 20
53 #define ZIIRAVE_FIRM_PKT_DATA_SIZE 16
54 #define ZIIRAVE_FIRM_FLASH_MEMORY_START 0x1600
55 #define ZIIRAVE_FIRM_FLASH_MEMORY_END 0x2bbf
56 #define ZIIRAVE_FIRM_PAGE_SIZE 128
58 /* Received and ready for next Download packet. */
59 #define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
60 /* Currently writing to flash. Retry Download status in a moment! */
61 #define ZIIRAVE_FIRM_DOWNLOAD_BUSY 2
63 /* Wait for ACK timeout in ms */
64 #define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT 50
66 /* Firmware commands */
67 #define ZIIRAVE_CMD_DOWNLOAD_START 0x10
68 #define ZIIRAVE_CMD_DOWNLOAD_END 0x11
69 #define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
70 #define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
71 #define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
72 #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
73 #define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
75 #define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u"
76 #define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u"
78 struct ziirave_wdt_rev {
83 struct ziirave_wdt_data {
84 struct mutex sysfs_mutex;
85 struct watchdog_device wdd;
86 struct ziirave_wdt_rev bootloader_rev;
87 struct ziirave_wdt_rev firmware_rev;
91 static int wdt_timeout;
92 module_param(wdt_timeout, int, 0);
93 MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
95 static int reset_duration;
96 module_param(reset_duration, int, 0);
97 MODULE_PARM_DESC(reset_duration,
98 "Watchdog reset pulse duration in milliseconds");
100 static bool nowayout = WATCHDOG_NOWAYOUT;
101 module_param(nowayout, bool, 0);
102 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
103 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
105 static int ziirave_wdt_revision(struct i2c_client *client,
106 struct ziirave_wdt_rev *rev, u8 command)
110 ret = i2c_smbus_read_byte_data(client, command);
116 ret = i2c_smbus_read_byte_data(client, command + 1);
125 static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
127 struct i2c_client *client = to_i2c_client(wdd->parent);
129 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
132 static int ziirave_wdt_start(struct watchdog_device *wdd)
134 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
137 static int ziirave_wdt_stop(struct watchdog_device *wdd)
139 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
142 static int ziirave_wdt_ping(struct watchdog_device *wdd)
144 struct i2c_client *client = to_i2c_client(wdd->parent);
146 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
150 static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
151 unsigned int timeout)
153 struct i2c_client *client = to_i2c_client(wdd->parent);
156 ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
158 wdd->timeout = timeout;
163 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
165 struct i2c_client *client = to_i2c_client(wdd->parent);
168 ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
175 static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd)
177 struct i2c_client *client = to_i2c_client(wdd->parent);
179 unsigned long timeout;
181 timeout = jiffies + msecs_to_jiffies(ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT);
183 if (time_after(jiffies, timeout))
186 usleep_range(5000, 10000);
188 ret = i2c_smbus_read_byte(client);
190 dev_err(&client->dev, "Failed to read byte\n");
193 } while (ret == ZIIRAVE_FIRM_DOWNLOAD_BUSY);
195 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
198 static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr)
200 struct i2c_client *client = to_i2c_client(wdd->parent);
203 put_unaligned_le16(addr, address);
205 return i2c_smbus_write_block_data(client,
206 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
207 sizeof(address), address);
210 static int ziirave_firm_write_block_data(struct watchdog_device *wdd,
211 u8 command, u8 length, const u8 *data,
214 struct i2c_client *client = to_i2c_client(wdd->parent);
217 ret = i2c_smbus_write_block_data(client, command, length, data);
219 dev_err(&client->dev,
220 "Failed to send command 0x%02x: %d\n", command, ret);
225 ret = ziirave_firm_wait_for_ack(wdd);
230 static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
231 u8 byte, bool wait_for_ack)
233 return ziirave_firm_write_block_data(wdd, command, 1, &byte,
238 * ziirave_firm_write_pkt() - Build and write a firmware packet
240 * A packet to send to the firmware is composed by following bytes:
241 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
243 * Length: A data byte containing the length of the data.
244 * Addr0: Low byte of the address.
245 * Addr1: High byte of the address.
246 * Data0 .. Data15: Array of 16 bytes of data.
247 * Checksum: Checksum byte to verify data integrity.
249 static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
250 u32 addr, const u8 *data, u8 len)
252 const u16 addr16 = (u16)addr / 2;
253 struct i2c_client *client = to_i2c_client(wdd->parent);
254 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
257 /* Check max data size */
258 if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
259 dev_err(&client->dev, "Firmware packet too long (%d)\n",
267 put_unaligned_le16(addr16, packet + 1);
269 memcpy(packet + 3, data, len);
270 memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
272 /* Packet checksum */
273 for (i = 0; i < len + 3; i++)
274 checksum += packet[i];
275 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
277 ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
278 sizeof(packet), packet, true);
280 dev_err(&client->dev,
281 "Failed to write firmware packet at address 0x%04x: %d\n",
287 static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
288 u32 addr, const u8 *data, u8 len)
290 const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
291 (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
294 if (len > max_write_len) {
296 * If data crossed page boundary we need to split this
299 ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
303 addr += max_write_len;
304 data += max_write_len;
305 len -= max_write_len;
308 return __ziirave_firm_write_pkt(wdd, addr, data, len);
311 static int ziirave_firm_verify(struct watchdog_device *wdd,
312 const struct firmware *fw)
314 struct i2c_client *client = to_i2c_client(wdd->parent);
315 const struct ihex_binrec *rec;
317 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
320 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
321 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
322 if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
323 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END)
326 ret = ziirave_firm_set_read_addr(wdd, addr);
328 dev_err(&client->dev,
329 "Failed to send SET_READ_ADDR command: %d\n",
334 for (i = 0; i < ARRAY_SIZE(data); i++) {
335 ret = i2c_smbus_read_byte_data(client,
336 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
338 dev_err(&client->dev,
339 "Failed to READ DATA: %d\n", ret);
345 if (memcmp(data, rec->data, be16_to_cpu(rec->len))) {
346 dev_err(&client->dev,
347 "Firmware mismatch at address 0x%04x\n", addr);
355 static int ziirave_firm_upload(struct watchdog_device *wdd,
356 const struct firmware *fw)
358 struct i2c_client *client = to_i2c_client(wdd->parent);
359 const struct ihex_binrec *rec;
362 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
365 dev_err(&client->dev, "Failed to jump to bootloader\n");
371 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
373 dev_err(&client->dev, "Failed to start download\n");
379 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
380 ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
381 rec->data, be16_to_cpu(rec->len));
387 * Finish firmware download process by sending a zero length
390 ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
392 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
396 /* This sleep seems to be required */
399 /* Start firmware verification */
400 ret = ziirave_firm_verify(wdd, fw);
402 dev_err(&client->dev,
403 "Failed to verify firmware: %d\n", ret);
407 /* End download operation */
408 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
410 dev_err(&client->dev,
411 "Failed to end firmware download: %d\n", ret);
415 /* Reset the processor */
416 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
419 dev_err(&client->dev,
420 "Failed to reset the watchdog: %d\n", ret);
429 static const struct watchdog_info ziirave_wdt_info = {
430 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
431 .identity = "Zodiac RAVE Watchdog",
434 static const struct watchdog_ops ziirave_wdt_ops = {
435 .owner = THIS_MODULE,
436 .start = ziirave_wdt_start,
437 .stop = ziirave_wdt_stop,
438 .ping = ziirave_wdt_ping,
439 .set_timeout = ziirave_wdt_set_timeout,
440 .get_timeleft = ziirave_wdt_get_timeleft,
443 static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
444 struct device_attribute *attr,
447 struct i2c_client *client = to_i2c_client(dev->parent);
448 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
451 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
455 ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
456 w_priv->firmware_rev.minor);
458 mutex_unlock(&w_priv->sysfs_mutex);
463 static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
466 static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
467 struct device_attribute *attr,
470 struct i2c_client *client = to_i2c_client(dev->parent);
471 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
474 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
478 ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
479 w_priv->bootloader_rev.minor);
481 mutex_unlock(&w_priv->sysfs_mutex);
486 static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
489 static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
490 struct device_attribute *attr,
493 struct i2c_client *client = to_i2c_client(dev->parent);
494 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
497 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
501 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
503 mutex_unlock(&w_priv->sysfs_mutex);
508 static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
511 static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
512 struct device_attribute *attr,
513 const char *buf, size_t count)
515 struct i2c_client *client = to_i2c_client(dev->parent);
516 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
517 const struct firmware *fw;
520 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
522 dev_err(&client->dev, "Failed to request ihex firmware\n");
526 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
528 goto release_firmware;
530 err = ziirave_firm_upload(&w_priv->wdd, fw);
532 dev_err(&client->dev, "The firmware update failed: %d\n", err);
536 /* Update firmware version */
537 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
538 ZIIRAVE_WDT_FIRM_VER_MAJOR);
540 dev_err(&client->dev, "Failed to read firmware version: %d\n",
545 dev_info(&client->dev,
546 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
547 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
549 /* Restore the watchdog timeout */
550 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
552 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
555 mutex_unlock(&w_priv->sysfs_mutex);
558 release_firmware(fw);
560 return err ? err : count;
563 static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
564 ziirave_wdt_sysfs_store_firm);
566 static struct attribute *ziirave_wdt_attrs[] = {
567 &dev_attr_firmware_version.attr,
568 &dev_attr_bootloader_version.attr,
569 &dev_attr_reset_reason.attr,
570 &dev_attr_update_firmware.attr,
573 ATTRIBUTE_GROUPS(ziirave_wdt);
575 static int ziirave_wdt_init_duration(struct i2c_client *client)
579 if (!reset_duration) {
580 /* See if the reset pulse duration is provided in an of_node */
581 if (!client->dev.of_node)
584 ret = of_property_read_u32(client->dev.of_node,
588 dev_info(&client->dev,
589 "Unable to set reset pulse duration, using default\n");
594 if (reset_duration < 1 || reset_duration > 255)
597 dev_info(&client->dev, "Setting reset duration to %dms",
600 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
604 static int ziirave_wdt_probe(struct i2c_client *client,
605 const struct i2c_device_id *id)
608 struct ziirave_wdt_data *w_priv;
611 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
614 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
618 mutex_init(&w_priv->sysfs_mutex);
620 w_priv->wdd.info = &ziirave_wdt_info;
621 w_priv->wdd.ops = &ziirave_wdt_ops;
622 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
623 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
624 w_priv->wdd.parent = &client->dev;
625 w_priv->wdd.groups = ziirave_wdt_groups;
627 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
630 * The default value set in the watchdog should be perfectly valid, so
631 * pass that in if we haven't provided one via the module parameter or
634 if (w_priv->wdd.timeout == 0) {
635 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
637 dev_err(&client->dev, "Failed to read timeout\n");
641 if (val > ZIIRAVE_TIMEOUT_MAX ||
642 val < ZIIRAVE_TIMEOUT_MIN)
643 val = ZIIRAVE_TIMEOUT_DEFAULT;
645 w_priv->wdd.timeout = val;
648 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
650 dev_err(&client->dev, "Failed to set timeout\n");
654 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
656 watchdog_set_nowayout(&w_priv->wdd, nowayout);
658 i2c_set_clientdata(client, w_priv);
660 /* If in unconfigured state, set to stopped */
661 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
663 dev_err(&client->dev, "Failed to read state\n");
667 if (val == ZIIRAVE_STATE_INITIAL)
668 ziirave_wdt_stop(&w_priv->wdd);
670 ret = ziirave_wdt_init_duration(client);
672 dev_err(&client->dev, "Failed to init duration\n");
676 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
677 ZIIRAVE_WDT_FIRM_VER_MAJOR);
679 dev_err(&client->dev, "Failed to read firmware version\n");
683 dev_info(&client->dev,
684 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
685 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
687 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
688 ZIIRAVE_WDT_BOOT_VER_MAJOR);
690 dev_err(&client->dev, "Failed to read bootloader version\n");
694 dev_info(&client->dev,
695 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
696 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
698 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
699 ZIIRAVE_WDT_RESET_REASON);
700 if (w_priv->reset_reason < 0) {
701 dev_err(&client->dev, "Failed to read reset reason\n");
702 return w_priv->reset_reason;
705 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
706 !ziirave_reasons[w_priv->reset_reason]) {
707 dev_err(&client->dev, "Invalid reset reason\n");
711 ret = watchdog_register_device(&w_priv->wdd);
716 static int ziirave_wdt_remove(struct i2c_client *client)
718 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
720 watchdog_unregister_device(&w_priv->wdd);
725 static const struct i2c_device_id ziirave_wdt_id[] = {
729 MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
731 static const struct of_device_id zrv_wdt_of_match[] = {
732 { .compatible = "zii,rave-wdt", },
735 MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
737 static struct i2c_driver ziirave_wdt_driver = {
739 .name = "ziirave_wdt",
740 .of_match_table = zrv_wdt_of_match,
742 .probe = ziirave_wdt_probe,
743 .remove = ziirave_wdt_remove,
744 .id_table = ziirave_wdt_id,
747 module_i2c_driver(ziirave_wdt_driver);
749 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
750 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
751 MODULE_LICENSE("GPL");