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/watchdog.h>
23 #include <asm/unaligned.h>
25 #define ZIIRAVE_TIMEOUT_MIN 3
26 #define ZIIRAVE_TIMEOUT_MAX 255
27 #define ZIIRAVE_TIMEOUT_DEFAULT 30
29 #define ZIIRAVE_PING_VALUE 0x0
31 #define ZIIRAVE_STATE_INITIAL 0x0
32 #define ZIIRAVE_STATE_OFF 0x1
33 #define ZIIRAVE_STATE_ON 0x2
35 #define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
37 static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
38 "host request", NULL, "illegal configuration",
39 "illegal instruction", "illegal trap",
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
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
57 /* Received and ready for next Download packet. */
58 #define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
60 /* Firmware commands */
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
69 #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC 1
70 #define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC 1
72 #define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u"
73 #define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u"
75 struct ziirave_wdt_rev {
80 struct ziirave_wdt_data {
81 struct mutex sysfs_mutex;
82 struct watchdog_device wdd;
83 struct ziirave_wdt_rev bootloader_rev;
84 struct ziirave_wdt_rev firmware_rev;
88 static int wdt_timeout;
89 module_param(wdt_timeout, int, 0);
90 MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
92 static int reset_duration;
93 module_param(reset_duration, int, 0);
94 MODULE_PARM_DESC(reset_duration,
95 "Watchdog reset pulse duration in milliseconds");
97 static bool nowayout = WATCHDOG_NOWAYOUT;
98 module_param(nowayout, bool, 0);
99 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
102 static int ziirave_wdt_revision(struct i2c_client *client,
103 struct ziirave_wdt_rev *rev, u8 command)
107 ret = i2c_smbus_read_byte_data(client, command);
113 ret = i2c_smbus_read_byte_data(client, command + 1);
122 static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
124 struct i2c_client *client = to_i2c_client(wdd->parent);
126 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
129 static int ziirave_wdt_start(struct watchdog_device *wdd)
131 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
134 static int ziirave_wdt_stop(struct watchdog_device *wdd)
136 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
139 static int ziirave_wdt_ping(struct watchdog_device *wdd)
141 struct i2c_client *client = to_i2c_client(wdd->parent);
143 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
147 static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
148 unsigned int timeout)
150 struct i2c_client *client = to_i2c_client(wdd->parent);
153 ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
155 wdd->timeout = timeout;
160 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
162 struct i2c_client *client = to_i2c_client(wdd->parent);
165 ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
172 static int ziirave_firm_read_ack(struct watchdog_device *wdd)
174 struct i2c_client *client = to_i2c_client(wdd->parent);
177 ret = i2c_smbus_read_byte(client);
179 dev_err(&client->dev, "Failed to read status byte\n");
183 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
186 static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
188 struct i2c_client *client = to_i2c_client(wdd->parent);
189 const u16 addr16 = (u16)addr / 2;
192 put_unaligned_le16(addr16, address);
194 return i2c_smbus_write_block_data(client,
195 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
196 sizeof(address), address);
199 static bool ziirave_firm_addr_readonly(u32 addr)
201 return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
202 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
206 * ziirave_firm_write_pkt() - Build and write a firmware packet
208 * A packet to send to the firmware is composed by following bytes:
209 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
211 * Length: A data byte containing the length of the data.
212 * Addr0: Low byte of the address.
213 * Addr1: High byte of the address.
214 * Data0 .. Data15: Array of 16 bytes of data.
215 * Checksum: Checksum byte to verify data integrity.
217 static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
218 u32 addr, const u8 *data, u8 len)
220 const u16 addr16 = (u16)addr / 2;
221 struct i2c_client *client = to_i2c_client(wdd->parent);
222 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
225 /* Check max data size */
226 if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
227 dev_err(&client->dev, "Firmware packet too long (%d)\n",
233 * Ignore packets that are targeting program memory outisde of
234 * app partition, since they will be ignored by the
235 * bootloader. At the same time, we need to make sure we'll
236 * allow zero length packet that will be sent as the last step
239 if (len && ziirave_firm_addr_readonly(addr))
245 put_unaligned_le16(addr16, packet + 1);
247 memcpy(packet + 3, data, len);
248 memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
250 /* Packet checksum */
251 for (i = 0; i < len + 3; i++)
252 checksum += packet[i];
253 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
255 ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
256 sizeof(packet), packet);
258 dev_err(&client->dev,
259 "Failed to send DOWNLOAD_PACKET: %d\n", ret);
263 ret = ziirave_firm_read_ack(wdd);
265 dev_err(&client->dev,
266 "Failed to write firmware packet at address 0x%04x: %d\n",
272 static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
273 u32 addr, const u8 *data, u8 len)
275 const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
276 (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
279 if (len > max_write_len) {
281 * If data crossed page boundary we need to split this
284 ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
288 addr += max_write_len;
289 data += max_write_len;
290 len -= max_write_len;
293 return __ziirave_firm_write_pkt(wdd, addr, data, len);
296 static int ziirave_firm_verify(struct watchdog_device *wdd,
297 const struct firmware *fw)
299 struct i2c_client *client = to_i2c_client(wdd->parent);
300 const struct ihex_binrec *rec;
302 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
304 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
305 const u16 len = be16_to_cpu(rec->len);
306 const u32 addr = be32_to_cpu(rec->addr);
308 if (ziirave_firm_addr_readonly(addr))
311 ret = ziirave_firm_set_read_addr(wdd, addr);
313 dev_err(&client->dev,
314 "Failed to send SET_READ_ADDR command: %d\n",
319 for (i = 0; i < len; i++) {
320 ret = i2c_smbus_read_byte_data(client,
321 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
323 dev_err(&client->dev,
324 "Failed to READ DATA: %d\n", ret);
330 if (memcmp(data, rec->data, len)) {
331 dev_err(&client->dev,
332 "Firmware mismatch at address 0x%04x\n", addr);
340 static int ziirave_firm_upload(struct watchdog_device *wdd,
341 const struct firmware *fw)
343 struct i2c_client *client = to_i2c_client(wdd->parent);
344 const struct ihex_binrec *rec;
347 ret = i2c_smbus_write_byte_data(client,
348 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
349 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
351 dev_err(&client->dev, "Failed to jump to bootloader\n");
357 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
359 dev_err(&client->dev, "Failed to start download\n");
363 ret = ziirave_firm_read_ack(wdd);
365 dev_err(&client->dev, "No ACK for start download\n");
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));
379 * Finish firmware download process by sending a zero length
382 ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
384 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
388 /* This sleep seems to be required */
391 /* Start firmware verification */
392 ret = ziirave_firm_verify(wdd, fw);
394 dev_err(&client->dev,
395 "Failed to verify firmware: %d\n", ret);
399 /* End download operation */
400 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
402 dev_err(&client->dev,
403 "Failed to end firmware download: %d\n", ret);
407 /* Reset the processor */
408 ret = i2c_smbus_write_byte_data(client,
409 ZIIRAVE_CMD_RESET_PROCESSOR,
410 ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
412 dev_err(&client->dev,
413 "Failed to reset the watchdog: %d\n", ret);
422 static const struct watchdog_info ziirave_wdt_info = {
423 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
424 .identity = "RAVE Switch Watchdog",
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,
436 static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
437 struct device_attribute *attr,
440 struct i2c_client *client = to_i2c_client(dev->parent);
441 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
444 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
448 ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
449 w_priv->firmware_rev.minor);
451 mutex_unlock(&w_priv->sysfs_mutex);
456 static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
459 static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
460 struct device_attribute *attr,
463 struct i2c_client *client = to_i2c_client(dev->parent);
464 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
467 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
471 ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
472 w_priv->bootloader_rev.minor);
474 mutex_unlock(&w_priv->sysfs_mutex);
479 static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
482 static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
483 struct device_attribute *attr,
486 struct i2c_client *client = to_i2c_client(dev->parent);
487 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
490 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
494 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
496 mutex_unlock(&w_priv->sysfs_mutex);
501 static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
504 static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct i2c_client *client = to_i2c_client(dev->parent);
509 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
510 const struct firmware *fw;
513 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
515 dev_err(&client->dev, "Failed to request ihex firmware\n");
519 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
521 goto release_firmware;
523 err = ziirave_firm_upload(&w_priv->wdd, fw);
525 dev_err(&client->dev, "The firmware update failed: %d\n", err);
529 /* Update firmware version */
530 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
531 ZIIRAVE_WDT_FIRM_VER_MAJOR);
533 dev_err(&client->dev, "Failed to read firmware version: %d\n",
538 dev_info(&client->dev,
539 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
540 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
542 /* Restore the watchdog timeout */
543 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
545 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
548 mutex_unlock(&w_priv->sysfs_mutex);
551 release_firmware(fw);
553 return err ? err : count;
556 static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
557 ziirave_wdt_sysfs_store_firm);
559 static struct attribute *ziirave_wdt_attrs[] = {
560 &dev_attr_firmware_version.attr,
561 &dev_attr_bootloader_version.attr,
562 &dev_attr_reset_reason.attr,
563 &dev_attr_update_firmware.attr,
566 ATTRIBUTE_GROUPS(ziirave_wdt);
568 static int ziirave_wdt_init_duration(struct i2c_client *client)
572 if (!reset_duration) {
573 /* See if the reset pulse duration is provided in an of_node */
574 if (!client->dev.of_node)
577 ret = of_property_read_u32(client->dev.of_node,
581 dev_info(&client->dev,
582 "No reset pulse duration specified, using default\n");
587 if (reset_duration < 1 || reset_duration > 255)
590 dev_info(&client->dev, "Setting reset duration to %dms",
593 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
597 static int ziirave_wdt_probe(struct i2c_client *client,
598 const struct i2c_device_id *id)
601 struct ziirave_wdt_data *w_priv;
604 if (!i2c_check_functionality(client->adapter,
605 I2C_FUNC_SMBUS_BYTE |
606 I2C_FUNC_SMBUS_BYTE_DATA |
607 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
610 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
614 mutex_init(&w_priv->sysfs_mutex);
616 w_priv->wdd.info = &ziirave_wdt_info;
617 w_priv->wdd.ops = &ziirave_wdt_ops;
618 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
619 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
620 w_priv->wdd.parent = &client->dev;
621 w_priv->wdd.groups = ziirave_wdt_groups;
623 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
626 * The default value set in the watchdog should be perfectly valid, so
627 * pass that in if we haven't provided one via the module parameter or
630 if (w_priv->wdd.timeout == 0) {
631 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
633 dev_err(&client->dev, "Failed to read timeout\n");
637 if (val > ZIIRAVE_TIMEOUT_MAX ||
638 val < ZIIRAVE_TIMEOUT_MIN)
639 val = ZIIRAVE_TIMEOUT_DEFAULT;
641 w_priv->wdd.timeout = val;
644 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
646 dev_err(&client->dev, "Failed to set timeout\n");
650 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
652 watchdog_set_nowayout(&w_priv->wdd, nowayout);
654 i2c_set_clientdata(client, w_priv);
656 /* If in unconfigured state, set to stopped */
657 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
659 dev_err(&client->dev, "Failed to read state\n");
663 if (val == ZIIRAVE_STATE_INITIAL)
664 ziirave_wdt_stop(&w_priv->wdd);
666 ret = ziirave_wdt_init_duration(client);
668 dev_err(&client->dev, "Failed to init duration\n");
672 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
673 ZIIRAVE_WDT_FIRM_VER_MAJOR);
675 dev_err(&client->dev, "Failed to read firmware version\n");
679 dev_info(&client->dev,
680 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
681 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
683 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
684 ZIIRAVE_WDT_BOOT_VER_MAJOR);
686 dev_err(&client->dev, "Failed to read bootloader version\n");
690 dev_info(&client->dev,
691 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
692 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
694 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
695 ZIIRAVE_WDT_RESET_REASON);
696 if (w_priv->reset_reason < 0) {
697 dev_err(&client->dev, "Failed to read reset reason\n");
698 return w_priv->reset_reason;
701 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
702 !ziirave_reasons[w_priv->reset_reason]) {
703 dev_err(&client->dev, "Invalid reset reason\n");
707 ret = watchdog_register_device(&w_priv->wdd);
712 static int ziirave_wdt_remove(struct i2c_client *client)
714 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
716 watchdog_unregister_device(&w_priv->wdd);
721 static const struct i2c_device_id ziirave_wdt_id[] = {
725 MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
727 static const struct of_device_id zrv_wdt_of_match[] = {
728 { .compatible = "zii,rave-wdt", },
731 MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
733 static struct i2c_driver ziirave_wdt_driver = {
735 .name = "ziirave_wdt",
736 .of_match_table = zrv_wdt_of_match,
738 .probe = ziirave_wdt_probe,
739 .remove = ziirave_wdt_remove,
740 .id_table = ziirave_wdt_id,
743 module_i2c_driver(ziirave_wdt_driver);
745 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
746 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
747 MODULE_LICENSE("GPL");