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 #define ZIIRAVE_TIMEOUT_MIN 3
25 #define ZIIRAVE_TIMEOUT_MAX 255
27 #define ZIIRAVE_PING_VALUE 0x0
29 #define ZIIRAVE_STATE_INITIAL 0x0
30 #define ZIIRAVE_STATE_OFF 0x1
31 #define ZIIRAVE_STATE_ON 0x2
33 #define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
35 static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
36 "host request", NULL, "illegal configuration",
37 "illegal instruction", "illegal trap",
40 #define ZIIRAVE_WDT_FIRM_VER_MAJOR 0x1
41 #define ZIIRAVE_WDT_BOOT_VER_MAJOR 0x3
42 #define ZIIRAVE_WDT_RESET_REASON 0x5
43 #define ZIIRAVE_WDT_STATE 0x6
44 #define ZIIRAVE_WDT_TIMEOUT 0x7
45 #define ZIIRAVE_WDT_TIME_LEFT 0x8
46 #define ZIIRAVE_WDT_PING 0x9
47 #define ZIIRAVE_WDT_RESET_DURATION 0xa
49 #define ZIIRAVE_FIRM_PKT_TOTAL_SIZE 20
50 #define ZIIRAVE_FIRM_PKT_DATA_SIZE 16
51 #define ZIIRAVE_FIRM_FLASH_MEMORY_START 0x1600
52 #define ZIIRAVE_FIRM_FLASH_MEMORY_END 0x2bbf
54 /* Received and ready for next Download packet. */
55 #define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
56 /* Currently writing to flash. Retry Download status in a moment! */
57 #define ZIIRAVE_FIRM_DOWNLOAD_BUSY 2
59 /* Wait for ACK timeout in ms */
60 #define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT 50
62 /* Firmware commands */
63 #define ZIIRAVE_CMD_DOWNLOAD_START 0x10
64 #define ZIIRAVE_CMD_DOWNLOAD_END 0x11
65 #define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
66 #define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
67 #define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
68 #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
69 #define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
71 struct ziirave_wdt_rev {
76 struct ziirave_wdt_data {
77 struct mutex sysfs_mutex;
78 struct watchdog_device wdd;
79 struct ziirave_wdt_rev bootloader_rev;
80 struct ziirave_wdt_rev firmware_rev;
84 static int wdt_timeout;
85 module_param(wdt_timeout, int, 0);
86 MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
88 static int reset_duration;
89 module_param(reset_duration, int, 0);
90 MODULE_PARM_DESC(reset_duration,
91 "Watchdog reset pulse duration in milliseconds");
93 static bool nowayout = WATCHDOG_NOWAYOUT;
94 module_param(nowayout, bool, 0);
95 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
96 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
98 static int ziirave_wdt_revision(struct i2c_client *client,
99 struct ziirave_wdt_rev *rev, u8 command)
103 ret = i2c_smbus_read_byte_data(client, command);
109 ret = i2c_smbus_read_byte_data(client, command + 1);
118 static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
120 struct i2c_client *client = to_i2c_client(wdd->parent);
122 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
125 static int ziirave_wdt_start(struct watchdog_device *wdd)
127 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
130 static int ziirave_wdt_stop(struct watchdog_device *wdd)
132 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
135 static int ziirave_wdt_ping(struct watchdog_device *wdd)
137 struct i2c_client *client = to_i2c_client(wdd->parent);
139 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
143 static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
144 unsigned int timeout)
146 struct i2c_client *client = to_i2c_client(wdd->parent);
149 ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
151 wdd->timeout = timeout;
156 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
158 struct i2c_client *client = to_i2c_client(wdd->parent);
161 ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
168 static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd)
170 struct i2c_client *client = to_i2c_client(wdd->parent);
172 unsigned long timeout;
174 timeout = jiffies + msecs_to_jiffies(ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT);
176 if (time_after(jiffies, timeout))
179 usleep_range(5000, 10000);
181 ret = i2c_smbus_read_byte(client);
183 dev_err(&client->dev, "Failed to read byte\n");
186 } while (ret == ZIIRAVE_FIRM_DOWNLOAD_BUSY);
188 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
191 static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr)
193 struct i2c_client *client = to_i2c_client(wdd->parent);
196 address[0] = addr & 0xff;
197 address[1] = (addr >> 8) & 0xff;
199 return i2c_smbus_write_block_data(client,
200 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
201 ARRAY_SIZE(address), address);
204 static int ziirave_firm_write_block_data(struct watchdog_device *wdd,
205 u8 command, u8 length, const u8 *data,
208 struct i2c_client *client = to_i2c_client(wdd->parent);
211 ret = i2c_smbus_write_block_data(client, command, length, data);
213 dev_err(&client->dev,
214 "Failed to send command 0x%02x: %d\n", command, ret);
219 ret = ziirave_firm_wait_for_ack(wdd);
224 static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
225 u8 byte, bool wait_for_ack)
227 return ziirave_firm_write_block_data(wdd, command, 1, &byte,
232 * ziirave_firm_write_pkt() - Build and write a firmware packet
234 * A packet to send to the firmware is composed by following bytes:
235 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
237 * Length: A data byte containing the length of the data.
238 * Addr0: Low byte of the address.
239 * Addr1: High byte of the address.
240 * Data0 .. Data15: Array of 16 bytes of data.
241 * Checksum: Checksum byte to verify data integrity.
243 static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
244 const struct ihex_binrec *rec)
246 struct i2c_client *client = to_i2c_client(wdd->parent);
247 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
251 memset(packet, 0, ARRAY_SIZE(packet));
254 packet[0] = (u8)be16_to_cpu(rec->len);
256 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
257 packet[1] = addr & 0xff;
258 packet[2] = (addr & 0xff00) >> 8;
261 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE)
263 memcpy(packet + 3, rec->data, be16_to_cpu(rec->len));
265 /* Packet checksum */
266 for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++)
267 checksum += packet[i];
268 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
270 ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
271 ARRAY_SIZE(packet), packet, true);
273 dev_err(&client->dev,
274 "Failed to write firmware packet at address 0x%04x: %d\n",
280 static int ziirave_firm_verify(struct watchdog_device *wdd,
281 const struct firmware *fw)
283 struct i2c_client *client = to_i2c_client(wdd->parent);
284 const struct ihex_binrec *rec;
286 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
289 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
290 /* Zero length marks end of records */
291 if (!be16_to_cpu(rec->len))
294 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
295 if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
296 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END)
299 ret = ziirave_firm_set_read_addr(wdd, addr);
301 dev_err(&client->dev,
302 "Failed to send SET_READ_ADDR command: %d\n",
307 for (i = 0; i < ARRAY_SIZE(data); i++) {
308 ret = i2c_smbus_read_byte_data(client,
309 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
311 dev_err(&client->dev,
312 "Failed to READ DATA: %d\n", ret);
318 if (memcmp(data, rec->data, be16_to_cpu(rec->len))) {
319 dev_err(&client->dev,
320 "Firmware mismatch at address 0x%04x\n", addr);
328 static int ziirave_firm_upload(struct watchdog_device *wdd,
329 const struct firmware *fw)
331 struct i2c_client *client = to_i2c_client(wdd->parent);
332 int ret, words_till_page_break;
333 const struct ihex_binrec *rec;
334 struct ihex_binrec *rec_new;
336 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
343 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
349 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
350 /* Zero length marks end of records */
351 if (!be16_to_cpu(rec->len))
354 /* Check max data size */
355 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
356 dev_err(&client->dev, "Firmware packet too long (%d)\n",
357 be16_to_cpu(rec->len));
361 /* Calculate words till page break */
362 words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) &
364 if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) {
366 * Data in passes page boundary, so we need to split in
367 * two blocks of data. Create a packet with the first
370 rec_new = kzalloc(sizeof(struct ihex_binrec) +
371 (words_till_page_break << 1),
376 rec_new->len = cpu_to_be16(words_till_page_break << 1);
377 rec_new->addr = rec->addr;
378 memcpy(rec_new->data, rec->data,
379 be16_to_cpu(rec_new->len));
381 ret = ziirave_firm_write_pkt(wdd, rec_new);
386 /* Create a packet with the second block of data */
387 rec_new = kzalloc(sizeof(struct ihex_binrec) +
388 be16_to_cpu(rec->len) -
389 (words_till_page_break << 1),
394 /* Remaining bytes */
395 rec_new->len = rec->len -
396 cpu_to_be16(words_till_page_break << 1);
398 rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) +
399 (words_till_page_break << 1));
401 memcpy(rec_new->data,
402 rec->data + (words_till_page_break << 1),
403 be16_to_cpu(rec_new->len));
405 ret = ziirave_firm_write_pkt(wdd, rec_new);
410 ret = ziirave_firm_write_pkt(wdd, rec);
416 /* For end of download, the length field will be set to 0 */
417 rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL);
421 ret = ziirave_firm_write_pkt(wdd, rec_new);
424 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
428 /* This sleep seems to be required */
431 /* Start firmware verification */
432 ret = ziirave_firm_verify(wdd, fw);
434 dev_err(&client->dev,
435 "Failed to verify firmware: %d\n", ret);
439 /* End download operation */
440 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
444 /* Reset the processor */
445 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
455 static const struct watchdog_info ziirave_wdt_info = {
456 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
457 .identity = "Zodiac RAVE Watchdog",
460 static const struct watchdog_ops ziirave_wdt_ops = {
461 .owner = THIS_MODULE,
462 .start = ziirave_wdt_start,
463 .stop = ziirave_wdt_stop,
464 .ping = ziirave_wdt_ping,
465 .set_timeout = ziirave_wdt_set_timeout,
466 .get_timeleft = ziirave_wdt_get_timeleft,
469 static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
470 struct device_attribute *attr,
473 struct i2c_client *client = to_i2c_client(dev->parent);
474 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
477 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
481 ret = sprintf(buf, "02.%02u.%02u", w_priv->firmware_rev.major,
482 w_priv->firmware_rev.minor);
484 mutex_unlock(&w_priv->sysfs_mutex);
489 static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
492 static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
493 struct device_attribute *attr,
496 struct i2c_client *client = to_i2c_client(dev->parent);
497 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
500 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
504 ret = sprintf(buf, "01.%02u.%02u", w_priv->bootloader_rev.major,
505 w_priv->bootloader_rev.minor);
507 mutex_unlock(&w_priv->sysfs_mutex);
512 static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
515 static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
516 struct device_attribute *attr,
519 struct i2c_client *client = to_i2c_client(dev->parent);
520 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
523 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
527 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
529 mutex_unlock(&w_priv->sysfs_mutex);
534 static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
537 static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
538 struct device_attribute *attr,
539 const char *buf, size_t count)
541 struct i2c_client *client = to_i2c_client(dev->parent);
542 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
543 const struct firmware *fw;
546 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
548 dev_err(&client->dev, "Failed to request ihex firmware\n");
552 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
554 goto release_firmware;
556 err = ziirave_firm_upload(&w_priv->wdd, fw);
558 dev_err(&client->dev, "The firmware update failed: %d\n", err);
562 /* Update firmware version */
563 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
564 ZIIRAVE_WDT_FIRM_VER_MAJOR);
566 dev_err(&client->dev, "Failed to read firmware version: %d\n",
571 dev_info(&client->dev, "Firmware updated to version 02.%02u.%02u\n",
572 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
574 /* Restore the watchdog timeout */
575 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
577 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
580 mutex_unlock(&w_priv->sysfs_mutex);
583 release_firmware(fw);
585 return err ? err : count;
588 static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
589 ziirave_wdt_sysfs_store_firm);
591 static struct attribute *ziirave_wdt_attrs[] = {
592 &dev_attr_firmware_version.attr,
593 &dev_attr_bootloader_version.attr,
594 &dev_attr_reset_reason.attr,
595 &dev_attr_update_firmware.attr,
598 ATTRIBUTE_GROUPS(ziirave_wdt);
600 static int ziirave_wdt_init_duration(struct i2c_client *client)
604 if (!reset_duration) {
605 /* See if the reset pulse duration is provided in an of_node */
606 if (!client->dev.of_node)
609 ret = of_property_read_u32(client->dev.of_node,
613 dev_info(&client->dev,
614 "Unable to set reset pulse duration, using default\n");
619 if (reset_duration < 1 || reset_duration > 255)
622 dev_info(&client->dev, "Setting reset duration to %dms",
625 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
629 static int ziirave_wdt_probe(struct i2c_client *client,
630 const struct i2c_device_id *id)
633 struct ziirave_wdt_data *w_priv;
636 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
639 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
643 mutex_init(&w_priv->sysfs_mutex);
645 w_priv->wdd.info = &ziirave_wdt_info;
646 w_priv->wdd.ops = &ziirave_wdt_ops;
647 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
648 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
649 w_priv->wdd.parent = &client->dev;
650 w_priv->wdd.groups = ziirave_wdt_groups;
652 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
655 * The default value set in the watchdog should be perfectly valid, so
656 * pass that in if we haven't provided one via the module parameter or
659 if (w_priv->wdd.timeout == 0) {
660 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
664 if (val < ZIIRAVE_TIMEOUT_MIN)
667 w_priv->wdd.timeout = val;
669 ret = ziirave_wdt_set_timeout(&w_priv->wdd,
670 w_priv->wdd.timeout);
674 dev_info(&client->dev, "Timeout set to %ds.",
675 w_priv->wdd.timeout);
678 watchdog_set_nowayout(&w_priv->wdd, nowayout);
680 i2c_set_clientdata(client, w_priv);
682 /* If in unconfigured state, set to stopped */
683 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
687 if (val == ZIIRAVE_STATE_INITIAL)
688 ziirave_wdt_stop(&w_priv->wdd);
690 ret = ziirave_wdt_init_duration(client);
694 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
695 ZIIRAVE_WDT_FIRM_VER_MAJOR);
699 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
700 ZIIRAVE_WDT_BOOT_VER_MAJOR);
704 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
705 ZIIRAVE_WDT_RESET_REASON);
706 if (w_priv->reset_reason < 0)
707 return w_priv->reset_reason;
709 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
710 !ziirave_reasons[w_priv->reset_reason])
713 ret = watchdog_register_device(&w_priv->wdd);
718 static int ziirave_wdt_remove(struct i2c_client *client)
720 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
722 watchdog_unregister_device(&w_priv->wdd);
727 static const struct i2c_device_id ziirave_wdt_id[] = {
731 MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
733 static const struct of_device_id zrv_wdt_of_match[] = {
734 { .compatible = "zii,rave-wdt", },
737 MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
739 static struct i2c_driver ziirave_wdt_driver = {
741 .name = "ziirave_wdt",
742 .of_match_table = zrv_wdt_of_match,
744 .probe = ziirave_wdt_probe,
745 .remove = ziirave_wdt_remove,
746 .id_table = ziirave_wdt_id,
749 module_i2c_driver(ziirave_wdt_driver);
751 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
752 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
753 MODULE_LICENSE("GPL");