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 struct ziirave_wdt_rev {
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;
85 static int wdt_timeout;
86 module_param(wdt_timeout, int, 0);
87 MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
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");
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) ")");
99 static int ziirave_wdt_revision(struct i2c_client *client,
100 struct ziirave_wdt_rev *rev, u8 command)
104 ret = i2c_smbus_read_byte_data(client, command);
110 ret = i2c_smbus_read_byte_data(client, command + 1);
119 static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
121 struct i2c_client *client = to_i2c_client(wdd->parent);
123 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
126 static int ziirave_wdt_start(struct watchdog_device *wdd)
128 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
131 static int ziirave_wdt_stop(struct watchdog_device *wdd)
133 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
136 static int ziirave_wdt_ping(struct watchdog_device *wdd)
138 struct i2c_client *client = to_i2c_client(wdd->parent);
140 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
144 static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
145 unsigned int timeout)
147 struct i2c_client *client = to_i2c_client(wdd->parent);
150 ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
152 wdd->timeout = timeout;
157 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
159 struct i2c_client *client = to_i2c_client(wdd->parent);
162 ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
169 static int ziirave_firm_read_ack(struct watchdog_device *wdd)
171 struct i2c_client *client = to_i2c_client(wdd->parent);
174 ret = i2c_smbus_read_byte(client);
176 dev_err(&client->dev, "Failed to read status byte\n");
180 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
183 static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
185 struct i2c_client *client = to_i2c_client(wdd->parent);
186 const u16 addr16 = (u16)addr / 2;
189 put_unaligned_le16(addr16, address);
191 return i2c_smbus_write_block_data(client,
192 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
193 sizeof(address), address);
196 static bool ziirave_firm_addr_readonly(u32 addr)
198 return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
199 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
203 * ziirave_firm_write_pkt() - Build and write a firmware packet
205 * A packet to send to the firmware is composed by following bytes:
206 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
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.
214 static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
215 u32 addr, const u8 *data, u8 len)
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];
222 /* Check max data size */
223 if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
224 dev_err(&client->dev, "Firmware packet too long (%d)\n",
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
236 if (len && ziirave_firm_addr_readonly(addr))
242 put_unaligned_le16(addr16, packet + 1);
244 memcpy(packet + 3, data, len);
245 memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
247 /* Packet checksum */
248 for (i = 0; i < len + 3; i++)
249 checksum += packet[i];
250 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
252 ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
253 sizeof(packet), packet);
255 dev_err(&client->dev,
256 "Failed to send DOWNLOAD_PACKET: %d\n", ret);
260 ret = ziirave_firm_read_ack(wdd);
262 dev_err(&client->dev,
263 "Failed to write firmware packet at address 0x%04x: %d\n",
269 static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
270 u32 addr, const u8 *data, u8 len)
272 const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
273 (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
276 if (len > max_write_len) {
278 * If data crossed page boundary we need to split this
281 ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
285 addr += max_write_len;
286 data += max_write_len;
287 len -= max_write_len;
290 return __ziirave_firm_write_pkt(wdd, addr, data, len);
293 static int ziirave_firm_verify(struct watchdog_device *wdd,
294 const struct firmware *fw)
296 struct i2c_client *client = to_i2c_client(wdd->parent);
297 const struct ihex_binrec *rec;
299 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
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);
305 if (ziirave_firm_addr_readonly(addr))
308 ret = ziirave_firm_set_read_addr(wdd, addr);
310 dev_err(&client->dev,
311 "Failed to send SET_READ_ADDR command: %d\n",
316 for (i = 0; i < len; i++) {
317 ret = i2c_smbus_read_byte_data(client,
318 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
320 dev_err(&client->dev,
321 "Failed to READ DATA: %d\n", ret);
327 if (memcmp(data, rec->data, len)) {
328 dev_err(&client->dev,
329 "Firmware mismatch at address 0x%04x\n", addr);
337 static int ziirave_firm_upload(struct watchdog_device *wdd,
338 const struct firmware *fw)
340 struct i2c_client *client = to_i2c_client(wdd->parent);
341 const struct ihex_binrec *rec;
344 ret = i2c_smbus_write_byte_data(client,
345 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
346 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
348 dev_err(&client->dev, "Failed to jump to bootloader\n");
354 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
356 dev_err(&client->dev, "Failed to start download\n");
360 ret = ziirave_firm_read_ack(wdd);
362 dev_err(&client->dev, "No ACK for start download\n");
368 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
369 ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
370 rec->data, be16_to_cpu(rec->len));
376 * Finish firmware download process by sending a zero length
379 ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
381 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
385 /* This sleep seems to be required */
388 /* Start firmware verification */
389 ret = ziirave_firm_verify(wdd, fw);
391 dev_err(&client->dev,
392 "Failed to verify firmware: %d\n", ret);
396 /* End download operation */
397 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
399 dev_err(&client->dev,
400 "Failed to end firmware download: %d\n", ret);
404 /* Reset the processor */
405 ret = i2c_smbus_write_byte_data(client,
406 ZIIRAVE_CMD_RESET_PROCESSOR,
407 ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
409 dev_err(&client->dev,
410 "Failed to reset the watchdog: %d\n", ret);
419 static const struct watchdog_info ziirave_wdt_info = {
420 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
421 .identity = "RAVE Switch Watchdog",
424 static const struct watchdog_ops ziirave_wdt_ops = {
425 .owner = THIS_MODULE,
426 .start = ziirave_wdt_start,
427 .stop = ziirave_wdt_stop,
428 .ping = ziirave_wdt_ping,
429 .set_timeout = ziirave_wdt_set_timeout,
430 .get_timeleft = ziirave_wdt_get_timeleft,
433 static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
434 struct device_attribute *attr,
437 struct i2c_client *client = to_i2c_client(dev->parent);
438 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
441 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
445 ret = sysfs_emit(buf, "02.%02u.%02u\n",
446 w_priv->firmware_rev.major,
447 w_priv->firmware_rev.minor);
449 mutex_unlock(&w_priv->sysfs_mutex);
454 static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
457 static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
458 struct device_attribute *attr,
461 struct i2c_client *client = to_i2c_client(dev->parent);
462 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
465 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
469 ret = sysfs_emit(buf, "01.%02u.%02u\n",
470 w_priv->bootloader_rev.major,
471 w_priv->bootloader_rev.minor);
473 mutex_unlock(&w_priv->sysfs_mutex);
478 static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
481 static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
482 struct device_attribute *attr,
485 struct i2c_client *client = to_i2c_client(dev->parent);
486 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
489 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
493 ret = sysfs_emit(buf, "%s\n", ziirave_reasons[w_priv->reset_reason]);
495 mutex_unlock(&w_priv->sysfs_mutex);
500 static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
503 static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
507 struct i2c_client *client = to_i2c_client(dev->parent);
508 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
509 const struct firmware *fw;
512 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
514 dev_err(&client->dev, "Failed to request ihex firmware\n");
518 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
520 goto release_firmware;
522 err = ziirave_firm_upload(&w_priv->wdd, fw);
524 dev_err(&client->dev, "The firmware update failed: %d\n", err);
528 /* Update firmware version */
529 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
530 ZIIRAVE_WDT_FIRM_VER_MAJOR);
532 dev_err(&client->dev, "Failed to read firmware version: %d\n",
537 dev_info(&client->dev,
538 "Firmware updated to version 02.%02u.%02u\n",
539 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
541 /* Restore the watchdog timeout */
542 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
544 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
547 mutex_unlock(&w_priv->sysfs_mutex);
550 release_firmware(fw);
552 return err ? err : count;
555 static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
556 ziirave_wdt_sysfs_store_firm);
558 static struct attribute *ziirave_wdt_attrs[] = {
559 &dev_attr_firmware_version.attr,
560 &dev_attr_bootloader_version.attr,
561 &dev_attr_reset_reason.attr,
562 &dev_attr_update_firmware.attr,
565 ATTRIBUTE_GROUPS(ziirave_wdt);
567 static int ziirave_wdt_init_duration(struct i2c_client *client)
571 if (!reset_duration) {
572 /* See if the reset pulse duration is provided in an of_node */
573 if (!client->dev.of_node)
576 ret = of_property_read_u32(client->dev.of_node,
580 dev_info(&client->dev,
581 "No reset pulse duration specified, using default\n");
586 if (reset_duration < 1 || reset_duration > 255)
589 dev_info(&client->dev, "Setting reset duration to %dms",
592 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
596 static int ziirave_wdt_probe(struct i2c_client *client,
597 const struct i2c_device_id *id)
600 struct ziirave_wdt_data *w_priv;
603 if (!i2c_check_functionality(client->adapter,
604 I2C_FUNC_SMBUS_BYTE |
605 I2C_FUNC_SMBUS_BYTE_DATA |
606 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
609 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
613 mutex_init(&w_priv->sysfs_mutex);
615 w_priv->wdd.info = &ziirave_wdt_info;
616 w_priv->wdd.ops = &ziirave_wdt_ops;
617 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
618 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
619 w_priv->wdd.parent = &client->dev;
620 w_priv->wdd.groups = ziirave_wdt_groups;
622 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
625 * The default value set in the watchdog should be perfectly valid, so
626 * pass that in if we haven't provided one via the module parameter or
629 if (w_priv->wdd.timeout == 0) {
630 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
632 dev_err(&client->dev, "Failed to read timeout\n");
636 if (val > ZIIRAVE_TIMEOUT_MAX ||
637 val < ZIIRAVE_TIMEOUT_MIN)
638 val = ZIIRAVE_TIMEOUT_DEFAULT;
640 w_priv->wdd.timeout = val;
643 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
645 dev_err(&client->dev, "Failed to set timeout\n");
649 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
651 watchdog_set_nowayout(&w_priv->wdd, nowayout);
653 i2c_set_clientdata(client, w_priv);
655 /* If in unconfigured state, set to stopped */
656 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
658 dev_err(&client->dev, "Failed to read state\n");
662 if (val == ZIIRAVE_STATE_INITIAL)
663 ziirave_wdt_stop(&w_priv->wdd);
665 ret = ziirave_wdt_init_duration(client);
667 dev_err(&client->dev, "Failed to init duration\n");
671 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
672 ZIIRAVE_WDT_FIRM_VER_MAJOR);
674 dev_err(&client->dev, "Failed to read firmware version\n");
678 dev_info(&client->dev,
679 "Firmware version: 02.%02u.%02u\n",
680 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
682 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
683 ZIIRAVE_WDT_BOOT_VER_MAJOR);
685 dev_err(&client->dev, "Failed to read bootloader version\n");
689 dev_info(&client->dev,
690 "Bootloader version: 01.%02u.%02u\n",
691 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
693 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
694 ZIIRAVE_WDT_RESET_REASON);
695 if (w_priv->reset_reason < 0) {
696 dev_err(&client->dev, "Failed to read reset reason\n");
697 return w_priv->reset_reason;
700 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
701 !ziirave_reasons[w_priv->reset_reason]) {
702 dev_err(&client->dev, "Invalid reset reason\n");
706 ret = watchdog_register_device(&w_priv->wdd);
711 static int ziirave_wdt_remove(struct i2c_client *client)
713 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
715 watchdog_unregister_device(&w_priv->wdd);
720 static const struct i2c_device_id ziirave_wdt_id[] = {
724 MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
726 static const struct of_device_id zrv_wdt_of_match[] = {
727 { .compatible = "zii,rave-wdt", },
730 MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
732 static struct i2c_driver ziirave_wdt_driver = {
734 .name = "ziirave_wdt",
735 .of_match_table = zrv_wdt_of_match,
737 .probe = ziirave_wdt_probe,
738 .remove = ziirave_wdt_remove,
739 .id_table = ziirave_wdt_id,
742 module_i2c_driver(ziirave_wdt_driver);
744 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
745 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
746 MODULE_LICENSE("GPL");