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